(mouse-drag-region-1): When remapping mouse-1 to mouse-2, go back to
[bpt/emacs.git] / src / xdisp.c
CommitLineData
a2889657 1/* Display generation from window structure and buffer text.
0b5538bd
TTN
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005 Free Software Foundation, Inc.
a2889657
JB
5
6This file is part of GNU Emacs.
7
8GNU Emacs is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
b1d1124b 10the Free Software Foundation; either version 2, or (at your option)
a2889657
JB
11any later version.
12
13GNU Emacs is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GNU Emacs; see the file COPYING. If not, write to
4fc5845f
LK
20the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21Boston, MA 02110-1301, USA. */
a2889657 22
5f5c8ee5
GM
23/* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
24
25 Redisplay.
26
27 Emacs separates the task of updating the display from code
28 modifying global state, e.g. buffer text. This way functions
29 operating on buffers don't also have to be concerned with updating
30 the display.
31
32 Updating the display is triggered by the Lisp interpreter when it
33 decides it's time to do it. This is done either automatically for
34 you as part of the interpreter's command loop or as the result of
35 calling Lisp functions like `sit-for'. The C function `redisplay'
36 in xdisp.c is the only entry into the inner redisplay code. (Or,
48400103 37 let's say almost---see the description of direct update
4b41cebb 38 operations, below.)
5f5c8ee5
GM
39
40 The following diagram shows how redisplay code is invoked. As you
41 can see, Lisp calls redisplay and vice versa. Under window systems
42 like X, some portions of the redisplay code are also called
43 asynchronously during mouse movement or expose events. It is very
44 important that these code parts do NOT use the C library (malloc,
45 free) because many C libraries under Unix are not reentrant. They
46 may also NOT call functions of the Lisp interpreter which could
47 change the interpreter's state. If you don't follow these rules,
48 you will encounter bugs which are very hard to explain.
49
50 (Direct functions, see below)
2311178e 51 direct_output_for_insert,
5f5c8ee5
GM
52 direct_forward_char (dispnew.c)
53 +---------------------------------+
54 | |
55 | V
82a7ab23 56 +--------------+ redisplay +----------------+
5f5c8ee5
GM
57 | Lisp machine |---------------->| Redisplay code |<--+
58 +--------------+ (xdisp.c) +----------------+ |
59 ^ | |
60 +----------------------------------+ |
61 Don't use this path when called |
62 asynchronously! |
63 |
64 expose_window (asynchronous) |
65 |
66 X expose events -----+
67
959bc044 68 What does redisplay do? Obviously, it has to figure out somehow what
5f5c8ee5
GM
69 has been changed since the last time the display has been updated,
70 and to make these changes visible. Preferably it would do that in
71 a moderately intelligent way, i.e. fast.
72
73 Changes in buffer text can be deduced from window and buffer
74 structures, and from some global variables like `beg_unchanged' and
75 `end_unchanged'. The contents of the display are additionally
76 recorded in a `glyph matrix', a two-dimensional matrix of glyph
77 structures. Each row in such a matrix corresponds to a line on the
78 display, and each glyph in a row corresponds to a column displaying
79 a character, an image, or what else. This matrix is called the
80 `current glyph matrix' or `current matrix' in redisplay
81 terminology.
82
83 For buffer parts that have been changed since the last update, a
84 second glyph matrix is constructed, the so called `desired glyph
85 matrix' or short `desired matrix'. Current and desired matrix are
86 then compared to find a cheap way to update the display, e.g. by
87 reusing part of the display by scrolling lines.
88
89
90 Direct operations.
91
48400103 92 You will find a lot of redisplay optimizations when you start
5f5c8ee5
GM
93 looking at the innards of redisplay. The overall goal of all these
94 optimizations is to make redisplay fast because it is done
95 frequently.
96
97 Two optimizations are not found in xdisp.c. These are the direct
98 operations mentioned above. As the name suggests they follow a
99 different principle than the rest of redisplay. Instead of
100 building a desired matrix and then comparing it with the current
101 display, they perform their actions directly on the display and on
102 the current matrix.
103
104 One direct operation updates the display after one character has
105 been entered. The other one moves the cursor by one position
106 forward or backward. You find these functions under the names
107 `direct_output_for_insert' and `direct_output_forward_char' in
108 dispnew.c.
109
2311178e 110
5f5c8ee5
GM
111 Desired matrices.
112
113 Desired matrices are always built per Emacs window. The function
114 `display_line' is the central function to look at if you are
115 interested. It constructs one row in a desired matrix given an
116 iterator structure containing both a buffer position and a
117 description of the environment in which the text is to be
118 displayed. But this is too early, read on.
119
120 Characters and pixmaps displayed for a range of buffer text depend
121 on various settings of buffers and windows, on overlays and text
122 properties, on display tables, on selective display. The good news
123 is that all this hairy stuff is hidden behind a small set of
959bc044 124 interface functions taking an iterator structure (struct it)
5f5c8ee5
GM
125 argument.
126
48400103 127 Iteration over things to be displayed is then simple. It is
1178ddde
GM
128 started by initializing an iterator with a call to init_iterator.
129 Calls to get_next_display_element fill the iterator structure with
130 relevant information about the next thing to display. Calls to
5f5c8ee5
GM
131 set_iterator_to_next move the iterator to the next thing.
132
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
139
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
147
148
149 Frame matrices.
150
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
157
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
a2889657 169
18160b98 170#include <config.h>
a2889657 171#include <stdio.h>
7ee72033 172
a2889657 173#include "lisp.h"
546a4f00 174#include "keyboard.h"
44fa5b1e 175#include "frame.h"
a2889657
JB
176#include "window.h"
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
AC
201#include "macterm.h"
202#endif
a2889657 203
1dabd0cf
KS
204#ifndef FRAME_X_OUTPUT
205#define FRAME_X_OUTPUT(f) ((f)->output_data.x)
206#endif
207
5f5c8ee5
GM
208#define INFINITY 10000000
209
488dd4c4
JD
210#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
211 || defined (USE_GTK)
2e621225 212extern void set_frame_menubar P_ ((struct frame *f, int, int));
cd6dfed6 213extern int pending_menu_activation;
76412d64
RS
214#endif
215
a2889657
JB
216extern int interrupt_input;
217extern int command_loop_level;
218
562020fe
KS
219extern Lisp_Object do_mouse_tracking;
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;
e2b64074 235Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_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
493fdc3c
KS
246/* Pointer shapes */
247Lisp_Object Qarrow, Qhand, Qtext;
248
c53a1624
RS
249Lisp_Object Qrisky_local_variable;
250
7033d6df
RS
251/* Holds the list (error). */
252Lisp_Object list_of_error;
253
5f5c8ee5
GM
254/* Functions called to fontify regions of text. */
255
256Lisp_Object Vfontification_functions;
257Lisp_Object Qfontification_functions;
258
fa3c6b4d
KS
259/* Non-zero means automatically select any window when the mouse
260 cursor moves into it. */
261int mouse_autoselect_window;
262
e037b9ec 263/* Non-zero means draw tool bar buttons raised when the mouse moves
5f5c8ee5
GM
264 over them. */
265
e037b9ec 266int auto_raise_tool_bar_buttons_p;
5f5c8ee5 267
8232c913
KS
268/* Non-zero means to reposition window if cursor line is only partially visible. */
269
270int make_cursor_line_fully_visible_p;
271
e037b9ec 272/* Margin around tool bar buttons in pixels. */
5f5c8ee5 273
35a41507 274Lisp_Object Vtool_bar_button_margin;
5f5c8ee5 275
e037b9ec 276/* Thickness of shadow to draw around tool bar buttons. */
5f5c8ee5 277
31ade731 278EMACS_INT tool_bar_button_relief;
5f5c8ee5 279
e037b9ec 280/* Non-zero means automatically resize tool-bars so that all tool-bar
5f5c8ee5
GM
281 items are visible, and no blank lines remain. */
282
e037b9ec 283int auto_resize_tool_bars_p;
399164b4 284
fa3c6b4d
KS
285/* Non-zero means draw block and hollow cursor as wide as the glyph
286 under it. For example, if a block cursor is over a tab, it will be
287 drawn as wide as that tab on the display. */
288
289int x_stretch_cursor_p;
290
735c094c
KH
291/* Non-nil means don't actually do any redisplay. */
292
293Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
294
30a3f61c
GM
295/* Non-zero means Lisp evaluation during redisplay is inhibited. */
296
869fb12c 297int inhibit_eval_during_redisplay;
30a3f61c 298
5f5c8ee5
GM
299/* Names of text properties relevant for redisplay. */
300
493fdc3c 301Lisp_Object Qdisplay;
4b41cebb 302extern Lisp_Object Qface, Qinvisible, Qwidth;
5f5c8ee5
GM
303
304/* Symbols used in text property values. */
305
f65536fa 306Lisp_Object Vdisplay_pixels_per_inch;
5f5c8ee5 307Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
a7e27ef7 308Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
e893970b 309Lisp_Object Qslice;
da06e4d7 310Lisp_Object Qcenter;
493fdc3c 311Lisp_Object Qmargin, Qpointer;
99c71e51 312Lisp_Object Qline_height;
a7e27ef7 313extern Lisp_Object Qheight;
133c764e 314extern Lisp_Object QCwidth, QCheight, QCascent;
f65536fa 315extern Lisp_Object Qscroll_bar;
cc2734ee 316extern Lisp_Object Qcursor;
5f5c8ee5 317
8f897821 318/* Non-nil means highlight trailing whitespace. */
5f5c8ee5 319
8f897821 320Lisp_Object Vshow_trailing_whitespace;
5f5c8ee5 321
fe1c2006
KS
322/* Non-nil means escape non-break space and hyphens. */
323
9c69e6e1 324Lisp_Object Vnobreak_char_display;
fe1c2006 325
7af0e8d7 326#ifdef HAVE_WINDOW_SYSTEM
6d925726 327extern Lisp_Object Voverflow_newline_into_fringe;
88e6b646
KS
328
329/* Test if overflow newline into fringe. Called with iterator IT
be21b925 330 at or past right window margin, and with IT->current_x set. */
88e6b646
KS
331
332#define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
333 (!NILP (Voverflow_newline_into_fringe) \
334 && FRAME_WINDOW_P (it->f) \
335 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
336 && it->current_x == it->last_visible_x)
337
6d925726
KS
338#endif /* HAVE_WINDOW_SYSTEM */
339
f65536fa
KS
340/* Non-nil means show the text cursor in void text areas
341 i.e. in blank areas after eol and eob. This used to be
342 the default in 21.3. */
343
493fdc3c 344Lisp_Object Vvoid_text_area_pointer;
f65536fa 345
5f5c8ee5
GM
346/* Name of the face used to highlight trailing whitespace. */
347
348Lisp_Object Qtrailing_whitespace;
349
30374745
RS
350/* Name and number of the face used to highlight escape glyphs. */
351
352Lisp_Object Qescape_glyph;
30374745 353
44e582b8
JL
354/* Name and number of the face used to highlight non-breaking spaces. */
355
9c69e6e1 356Lisp_Object Qnobreak_space;
44e582b8 357
5f5c8ee5
GM
358/* The symbol `image' which is the car of the lists used to represent
359 images in Lisp. */
360
361Lisp_Object Qimage;
362
493fdc3c
KS
363/* The image map types. */
364Lisp_Object QCmap, QCpointer;
365Lisp_Object Qrect, Qcircle, Qpoly;
366
5f5c8ee5
GM
367/* Non-zero means print newline to stdout before next mini-buffer
368 message. */
a2889657
JB
369
370int noninteractive_need_newline;
371
5f5c8ee5 372/* Non-zero means print newline to message log before next message. */
f88eb0b6 373
3c6595e0 374static int message_log_need_newline;
f88eb0b6 375
b14bc55e
RS
376/* Three markers that message_dolog uses.
377 It could allocate them itself, but that causes trouble
378 in handling memory-full errors. */
379static Lisp_Object message_dolog_marker1;
380static Lisp_Object message_dolog_marker2;
381static Lisp_Object message_dolog_marker3;
5f5c8ee5
GM
382\f
383/* The buffer position of the first character appearing entirely or
384 partially on the line of the selected window which contains the
385 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
386 redisplay optimization in redisplay_internal. */
a2889657 387
5f5c8ee5 388static struct text_pos this_line_start_pos;
a2889657 389
5f5c8ee5
GM
390/* Number of characters past the end of the line above, including the
391 terminating newline. */
392
393static struct text_pos this_line_end_pos;
394
395/* The vertical positions and the height of this line. */
a2889657 396
a2889657 397static int this_line_vpos;
5f5c8ee5
GM
398static int this_line_y;
399static int this_line_pixel_height;
400
401/* X position at which this display line starts. Usually zero;
402 negative if first character is partially visible. */
403
404static int this_line_start_x;
a2889657 405
5f5c8ee5 406/* Buffer that this_line_.* variables are referring to. */
a2889657 407
a2889657
JB
408static struct buffer *this_line_buffer;
409
5f5c8ee5
GM
410/* Nonzero means truncate lines in all windows less wide than the
411 frame. */
a2889657 412
a2889657
JB
413int truncate_partial_width_windows;
414
7bbe686f 415/* A flag to control how to display unibyte 8-bit character. */
5f5c8ee5 416
7bbe686f 417int unibyte_display_via_language_environment;
2311178e 418
5f5c8ee5
GM
419/* Nonzero means we have more than one non-mini-buffer-only frame.
420 Not guaranteed to be accurate except while parsing
421 frame-title-format. */
7bbe686f 422
d39b6696
KH
423int multiple_frames;
424
a2889657
JB
425Lisp_Object Vglobal_mode_string;
426
351b5434
KS
427
428/* List of variables (symbols) which hold markers for overlay arrows.
429 The symbols on this list are examined during redisplay to determine
be21b925 430 where to display overlay arrows. */
351b5434
KS
431
432Lisp_Object Voverlay_arrow_variable_list;
433
a2889657 434/* Marker for where to display an arrow on top of the buffer text. */
5f5c8ee5 435
a2889657
JB
436Lisp_Object Voverlay_arrow_position;
437
5f5c8ee5
GM
438/* String to display for the arrow. Only used on terminal frames. */
439
a2889657
JB
440Lisp_Object Voverlay_arrow_string;
441
351b5434
KS
442/* Values of those variables at last redisplay are stored as
443 properties on `overlay-arrow-position' symbol. However, if
444 Voverlay_arrow_position is a marker, last-arrow-position is its
5f5c8ee5
GM
445 numerical position. */
446
351b5434
KS
447Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
448
449/* Alternative overlay-arrow-string and overlay-arrow-bitmap
450 properties on a symbol in overlay-arrow-variable-list. */
451
452Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
d45de95b 453
5f5c8ee5
GM
454/* Like mode-line-format, but for the title bar on a visible frame. */
455
d39b6696
KH
456Lisp_Object Vframe_title_format;
457
5f5c8ee5
GM
458/* Like mode-line-format, but for the title bar on an iconified frame. */
459
d39b6696
KH
460Lisp_Object Vicon_title_format;
461
08b610e4
RS
462/* List of functions to call when a window's size changes. These
463 functions get one arg, a frame on which one or more windows' sizes
464 have changed. */
5f5c8ee5 465
08b610e4
RS
466static Lisp_Object Vwindow_size_change_functions;
467
0bca8940 468Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
cf074754 469
39df553f 470/* Nonzero if an overlay arrow has been displayed in this window. */
a2889657 471
5f5c8ee5 472static int overlay_arrow_seen;
ca26e1c8 473
fba9ce76 474/* Nonzero means highlight the region even in nonselected windows. */
fba9ce76 475
5f5c8ee5
GM
476int highlight_nonselected_windows;
477
478/* If cursor motion alone moves point off frame, try scrolling this
479 many lines up or down if that will bring it back. */
480
31ade731 481static EMACS_INT scroll_step;
a2889657 482
4b41cebb 483/* Nonzero means scroll just far enough to bring point back on the
5f5c8ee5
GM
484 screen, when appropriate. */
485
31ade731 486static EMACS_INT scroll_conservatively;
0789adb2 487
5f5c8ee5
GM
488/* Recenter the window whenever point gets within this many lines of
489 the top or bottom of the window. This value is translated into a
da8b7f4f
KS
490 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
491 that there is really a fixed pixel height scroll margin. */
5f5c8ee5 492
31ade731 493EMACS_INT scroll_margin;
9afd2168 494
5f5c8ee5
GM
495/* Number of windows showing the buffer of the selected window (or
496 another buffer with the same base buffer). keyboard.c refers to
497 this. */
a2889657 498
a2889657
JB
499int buffer_shared;
500
5f5c8ee5 501/* Vector containing glyphs for an ellipsis `...'. */
a2889657 502
5f5c8ee5 503static Lisp_Object default_invis_vector[3];
a2889657 504
1862a24e
MB
505/* Zero means display the mode-line/header-line/menu-bar in the default face
506 (this slightly odd definition is for compatibility with previous versions
507 of emacs), non-zero means display them using their respective faces.
508
509 This variable is deprecated. */
a2889657 510
a2889657
JB
511int mode_line_inverse_video;
512
5f5c8ee5
GM
513/* Prompt to display in front of the mini-buffer contents. */
514
8c5b6a0a 515Lisp_Object minibuf_prompt;
a2889657 516
5f5c8ee5
GM
517/* Width of current mini-buffer prompt. Only set after display_line
518 of the line that contains the prompt. */
519
a2889657 520int minibuf_prompt_width;
5f5c8ee5 521
5f5c8ee5
GM
522/* This is the window where the echo area message was displayed. It
523 is always a mini-buffer window, but it may not be the same window
524 currently active as a mini-buffer. */
525
73af359d
RS
526Lisp_Object echo_area_window;
527
c6e89d6c
GM
528/* List of pairs (MESSAGE . MULTIBYTE). The function save_message
529 pushes the current message and the value of
530 message_enable_multibyte on the stack, the function restore_message
531 pops the stack and displays MESSAGE again. */
532
533Lisp_Object Vmessage_stack;
534
a3788d53
RS
535/* Nonzero means multibyte characters were enabled when the echo area
536 message was specified. */
5f5c8ee5 537
a3788d53
RS
538int message_enable_multibyte;
539
4b41cebb 540/* Nonzero if we should redraw the mode lines on the next redisplay. */
5f5c8ee5 541
a2889657
JB
542int update_mode_lines;
543
5f5c8ee5 544/* Nonzero if window sizes or contents have changed since last
4b41cebb 545 redisplay that finished. */
5f5c8ee5 546
a2889657
JB
547int windows_or_buffers_changed;
548
5fb96e96
RS
549/* Nonzero means a frame's cursor type has been changed. */
550
551int cursor_type_changed;
552
5f5c8ee5
GM
553/* Nonzero after display_mode_line if %l was used and it displayed a
554 line number. */
555
aa6d10fa
RS
556int line_number_displayed;
557
558/* Maximum buffer size for which to display line numbers. */
5f5c8ee5 559
090703f4 560Lisp_Object Vline_number_display_limit;
5992c4f7 561
4b41cebb 562/* Line width to consider when repositioning for line number display. */
5d121aec 563
31ade731 564static EMACS_INT line_number_display_limit_width;
5d121aec 565
5f5c8ee5
GM
566/* Number of lines to keep in the message log buffer. t means
567 infinite. nil means don't log at all. */
568
5992c4f7 569Lisp_Object Vmessage_log_max;
d45de95b 570
6a94510a
GM
571/* The name of the *Messages* buffer, a string. */
572
573static Lisp_Object Vmessages_buffer_name;
574
a8a1c0ee
RS
575/* Index 0 is the buffer that holds the current (desired) echo area message,
576 or nil if none is desired right now.
577
578 Index 1 is the buffer that holds the previously displayed echo area message,
579 or nil to indicate no message. This is normally what's on the screen now.
580
581 These two can point to the same buffer. That happens when the last
582 message output by the user (or made by echoing) has been displayed. */
c6e89d6c
GM
583
584Lisp_Object echo_area_buffer[2];
585
a8a1c0ee
RS
586/* Permanent pointers to the two buffers that are used for echo area
587 purposes. Once the two buffers are made, and their pointers are
588 placed here, these two slots remain unchanged unless those buffers
589 need to be created afresh. */
c6e89d6c
GM
590
591static Lisp_Object echo_buffer[2];
592
593/* A vector saved used in with_area_buffer to reduce consing. */
594
595static Lisp_Object Vwith_echo_area_save_vector;
596
597/* Non-zero means display_echo_area should display the last echo area
598 message again. Set by redisplay_preserve_echo_area. */
599
600static int display_last_displayed_message_p;
601
602/* Nonzero if echo area is being used by print; zero if being used by
603 message. */
604
605int message_buf_print;
606
e1477f43
GM
607/* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
608
609Lisp_Object Qinhibit_menubar_update;
610int inhibit_menubar_update;
611
9142dd5b
GM
612/* Maximum height for resizing mini-windows. Either a float
613 specifying a fraction of the available height, or an integer
614 specifying a number of lines. */
c6e89d6c 615
ad4f174e
GM
616Lisp_Object Vmax_mini_window_height;
617
618/* Non-zero means messages should be displayed with truncated
619 lines instead of being continued. */
620
621int message_truncate_lines;
622Lisp_Object Qmessage_truncate_lines;
c6e89d6c 623
6e019995
GM
624/* Set to 1 in clear_message to make redisplay_internal aware
625 of an emptied echo area. */
626
627static int message_cleared_p;
628
cfe03a41
KS
629/* How to blink the default frame cursor off. */
630Lisp_Object Vblink_cursor_alist;
631
5f5c8ee5
GM
632/* A scratch glyph row with contents used for generating truncation
633 glyphs. Also used in direct_output_for_insert. */
12adba34 634
5f5c8ee5
GM
635#define MAX_SCRATCH_GLYPHS 100
636struct glyph_row scratch_glyph_row;
637static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
1adc55de 638
5f5c8ee5
GM
639/* Ascent and height of the last line processed by move_it_to. */
640
641static int last_max_ascent, last_height;
642
21fdfb65
GM
643/* Non-zero if there's a help-echo in the echo area. */
644
645int help_echo_showing_p;
646
04612a64
GM
647/* If >= 0, computed, exact values of mode-line and header-line height
648 to use in the macros CURRENT_MODE_LINE_HEIGHT and
649 CURRENT_HEADER_LINE_HEIGHT. */
650
651int current_mode_line_height, current_header_line_height;
652
5f5c8ee5 653/* The maximum distance to look ahead for text properties. Values
2311178e 654 that are too small let us call compute_char_face and similar
5f5c8ee5
GM
655 functions too often which is expensive. Values that are too large
656 let us call compute_char_face and alike too often because we
657 might not be interested in text properties that far away. */
658
659#define TEXT_PROP_DISTANCE_LIMIT 100
660
47589c8c
GM
661#if GLYPH_DEBUG
662
76cb5e06
GM
663/* Variables to turn off display optimizations from Lisp. */
664
665int inhibit_try_window_id, inhibit_try_window_reusing;
666int inhibit_try_cursor_movement;
667
5f5c8ee5
GM
668/* Non-zero means print traces of redisplay if compiled with
669 GLYPH_DEBUG != 0. */
670
5f5c8ee5 671int trace_redisplay_p;
47589c8c 672
546a4f00 673#endif /* GLYPH_DEBUG */
47589c8c 674
546a4f00
AI
675#ifdef DEBUG_TRACE_MOVE
676/* Non-zero means trace with TRACE_MOVE to stderr. */
47589c8c
GM
677int trace_move;
678
47589c8c
GM
679#define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
680#else
91c3f500 681#define TRACE_MOVE(x) (void) 0
5f5c8ee5 682#endif
2311178e 683
d475bcb8
GM
684/* Non-zero means automatically scroll windows horizontally to make
685 point visible. */
686
687int automatic_hscrolling_p;
688
1df7e8f0
EZ
689/* How close to the margin can point get before the window is scrolled
690 horizontally. */
5d335845 691EMACS_INT hscroll_margin;
1df7e8f0
EZ
692
693/* How much to scroll horizontally when point is inside the above margin. */
e76d28d5 694Lisp_Object Vhscroll_step;
1df7e8f0 695
6422c1d7 696/* The variable `resize-mini-windows'. If nil, don't resize
67526daf 697 mini-windows. If t, always resize them to fit the text they
6422c1d7
GM
698 display. If `grow-only', let mini-windows grow only until they
699 become empty. */
700
701Lisp_Object Vresize_mini_windows;
702
82a7ab23
RS
703/* Buffer being redisplayed -- for redisplay_window_error. */
704
705struct buffer *displayed_buffer;
706
5f5c8ee5
GM
707/* Value returned from text property handlers (see below). */
708
709enum prop_handled
3c6595e0 710{
5f5c8ee5
GM
711 HANDLED_NORMALLY,
712 HANDLED_RECOMPUTE_PROPS,
713 HANDLED_OVERLAY_STRING_CONSUMED,
714 HANDLED_RETURN
715};
3c6595e0 716
5f5c8ee5
GM
717/* A description of text properties that redisplay is interested
718 in. */
3c6595e0 719
5f5c8ee5
GM
720struct props
721{
722 /* The name of the property. */
723 Lisp_Object *name;
90adcf20 724
5f5c8ee5
GM
725 /* A unique index for the property. */
726 enum prop_idx idx;
727
728 /* A handler function called to set up iterator IT from the property
729 at IT's current position. Value is used to steer handle_stop. */
730 enum prop_handled (*handler) P_ ((struct it *it));
731};
732
733static enum prop_handled handle_face_prop P_ ((struct it *));
734static enum prop_handled handle_invisible_prop P_ ((struct it *));
735static enum prop_handled handle_display_prop P_ ((struct it *));
260a86a0 736static enum prop_handled handle_composition_prop P_ ((struct it *));
5f5c8ee5
GM
737static enum prop_handled handle_overlay_change P_ ((struct it *));
738static enum prop_handled handle_fontified_prop P_ ((struct it *));
739
740/* Properties handled by iterators. */
741
742static struct props it_props[] =
5992c4f7 743{
5f5c8ee5
GM
744 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
745 /* Handle `face' before `display' because some sub-properties of
746 `display' need to know the face. */
747 {&Qface, FACE_PROP_IDX, handle_face_prop},
748 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
749 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
260a86a0 750 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
5f5c8ee5
GM
751 {NULL, 0, NULL}
752};
5992c4f7 753
5f5c8ee5
GM
754/* Value is the position described by X. If X is a marker, value is
755 the marker_position of X. Otherwise, value is X. */
12adba34 756
5f5c8ee5 757#define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
12adba34 758
5f5c8ee5 759/* Enumeration returned by some move_it_.* functions internally. */
12adba34 760
5f5c8ee5
GM
761enum move_it_result
762{
763 /* Not used. Undefined value. */
764 MOVE_UNDEFINED,
bab29e15 765
5f5c8ee5
GM
766 /* Move ended at the requested buffer position or ZV. */
767 MOVE_POS_MATCH_OR_ZV,
bab29e15 768
5f5c8ee5
GM
769 /* Move ended at the requested X pixel position. */
770 MOVE_X_REACHED,
12adba34 771
5f5c8ee5
GM
772 /* Move within a line ended at the end of a line that must be
773 continued. */
774 MOVE_LINE_CONTINUED,
2311178e 775
5f5c8ee5
GM
776 /* Move within a line ended at the end of a line that would
777 be displayed truncated. */
778 MOVE_LINE_TRUNCATED,
ff6c30e5 779
5f5c8ee5
GM
780 /* Move within a line ended at a line end. */
781 MOVE_NEWLINE_OR_CR
782};
12adba34 783
1987b083
RS
784/* This counter is used to clear the face cache every once in a while
785 in redisplay_internal. It is incremented for each redisplay.
786 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
787 cleared. */
788
789#define CLEAR_FACE_CACHE_COUNT 500
790static int clear_face_cache_count;
791
418b5052
KS
792/* Similarly for the image cache. */
793
794#ifdef HAVE_WINDOW_SYSTEM
795#define CLEAR_IMAGE_CACHE_COUNT 101
796static int clear_image_cache_count;
797#endif
798
1987b083
RS
799/* Record the previous terminal frame we displayed. */
800
801static struct frame *previous_terminal_frame;
802
803/* Non-zero while redisplay_internal is in progress. */
804
805int redisplaying_p;
806
26683087
RS
807/* Non-zero means don't free realized faces. Bound while freeing
808 realized faces is dangerous because glyph matrices might still
809 reference them. */
1987b083 810
26683087
RS
811int inhibit_free_realized_faces;
812Lisp_Object Qinhibit_free_realized_faces;
ff6c30e5 813
fa3c6b4d
KS
814/* If a string, XTread_socket generates an event to display that string.
815 (The display is done in read_char.) */
816
817Lisp_Object help_echo_string;
818Lisp_Object help_echo_window;
819Lisp_Object help_echo_object;
820int help_echo_pos;
821
822/* Temporary variable for XTread_socket. */
823
824Lisp_Object previous_help_echo_string;
825
2883e85a
KS
826/* Null glyph slice */
827
828static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
fa3c6b4d 829
5f5c8ee5
GM
830\f
831/* Function prototypes. */
832
064fd8ec 833static void setup_for_ellipsis P_ ((struct it *, int));
43c09969 834static void mark_window_display_accurate_1 P_ ((struct window *, int));
b2683be4 835static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
74bd6d65 836static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
cafafe0b 837static int cursor_row_p P_ ((struct window *, struct glyph_row *));
715e84c9 838static int redisplay_mode_lines P_ ((Lisp_Object, int));
2e621225 839static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
c0a53abb
PJ
840
841#if 0
2e621225 842static int invisible_text_between_p P_ ((struct it *, int, int));
c0a53abb
PJ
843#endif
844
2e621225 845static void pint2str P_ ((char *, int, int));
525b8b09 846static void pint2hrstr P_ ((char *, int, int));
2e621225
GM
847static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
848 struct text_pos));
849static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
850static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
4c0e4b80
KS
851static void store_mode_line_noprop_char P_ ((char));
852static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
2e621225
GM
853static void x_consider_frame_title P_ ((Lisp_Object));
854static void handle_stop P_ ((struct it *));
855static int tool_bar_lines_needed P_ ((struct frame *));
b2683be4 856static int single_display_spec_intangible_p P_ ((Lisp_Object));
5bcfeb49 857static void ensure_echo_area_buffers P_ ((void));
c6e89d6c
GM
858static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
859static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
23a96c77 860static int with_echo_area_buffer P_ ((struct window *, int,
23dd2d97
KR
861 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
862 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
c6e89d6c 863static void clear_garbaged_frames P_ ((void));
23dd2d97
KR
864static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
865static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
866static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
c6e89d6c 867static int display_echo_area P_ ((struct window *));
23dd2d97
KR
868static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
869static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
28514cd9 870static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
50f80c2f 871static int string_char_and_length P_ ((const unsigned char *, int, int *));
5f5c8ee5
GM
872static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
873 struct text_pos));
874static int compute_window_start_on_continuation_line P_ ((struct window *));
116d6f5c 875static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
5f5c8ee5 876static void insert_left_trunc_glyphs P_ ((struct it *));
351b5434
KS
877static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
878 Lisp_Object));
5f5c8ee5 879static void extend_face_to_end_of_line P_ ((struct it *));
4933c603 880static int append_space_for_newline P_ ((struct it *, int));
e10ee30c 881static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
03b0a4b4 882static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
47589c8c 883static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
5f5c8ee5
GM
884static int trailing_whitespace_p P_ ((int));
885static int message_log_check_duplicate P_ ((int, int, int, int));
5f5c8ee5
GM
886static void push_it P_ ((struct it *));
887static void pop_it P_ ((struct it *));
888static void sync_frame_with_window_matrix_rows P_ ((struct window *));
dd429b03 889static void select_frame_for_redisplay P_ ((Lisp_Object));
5f5c8ee5 890static void redisplay_internal P_ ((int));
c6e89d6c 891static int echo_area_display P_ ((int));
5f5c8ee5
GM
892static void redisplay_windows P_ ((Lisp_Object));
893static void redisplay_window P_ ((Lisp_Object, int));
82a7ab23
RS
894static Lisp_Object redisplay_window_error ();
895static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
896static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
5f5c8ee5
GM
897static void update_menu_bar P_ ((struct frame *, int));
898static int try_window_reusing_current_matrix P_ ((struct window *));
899static int try_window_id P_ ((struct window *));
900static int display_line P_ ((struct it *));
715e84c9 901static int display_mode_lines P_ ((struct window *));
04612a64 902static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
c53a1624 903static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
fec8f23e 904static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
72f62cb5 905static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
5f5c8ee5
GM
906static void display_menu_bar P_ ((struct window *));
907static int display_count_lines P_ ((int, int, int, int, int *));
908static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
909 int, int, struct it *, int, int, int, int));
910static void compute_line_metrics P_ ((struct it *));
911static void run_redisplay_end_trigger_hook P_ ((struct it *));
5a08cbaf 912static int get_overlay_strings P_ ((struct it *, int));
5f5c8ee5 913static void next_overlay_string P_ ((struct it *));
5f5c8ee5
GM
914static void reseat P_ ((struct it *, struct text_pos, int));
915static void reseat_1 P_ ((struct it *, struct text_pos, int));
916static void back_to_previous_visible_line_start P_ ((struct it *));
0b2e177e 917void reseat_at_previous_visible_line_start P_ ((struct it *));
312246d1 918static void reseat_at_next_visible_line_start P_ ((struct it *, int));
a6f82cc4 919static int next_element_from_ellipsis P_ ((struct it *));
5f5c8ee5
GM
920static int next_element_from_display_vector P_ ((struct it *));
921static int next_element_from_string P_ ((struct it *));
922static int next_element_from_c_string P_ ((struct it *));
923static int next_element_from_buffer P_ ((struct it *));
260a86a0 924static int next_element_from_composition P_ ((struct it *));
5f5c8ee5
GM
925static int next_element_from_image P_ ((struct it *));
926static int next_element_from_stretch P_ ((struct it *));
5a08cbaf 927static void load_overlay_strings P_ ((struct it *, int));
47d57b22
GM
928static int init_from_display_pos P_ ((struct it *, struct window *,
929 struct display_pos *));
5f5c8ee5
GM
930static void reseat_to_string P_ ((struct it *, unsigned char *,
931 Lisp_Object, int, int, int, int));
5f5c8ee5
GM
932static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
933 int, int, int));
934void move_it_vertically_backward P_ ((struct it *, int));
935static void init_to_row_start P_ ((struct it *, struct window *,
936 struct glyph_row *));
47d57b22
GM
937static int init_to_row_end P_ ((struct it *, struct window *,
938 struct glyph_row *));
5f5c8ee5 939static void back_to_previous_line_start P_ ((struct it *));
cafafe0b 940static int forward_to_next_line_start P_ ((struct it *, int *));
5f5c8ee5
GM
941static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
942 Lisp_Object, int));
943static struct text_pos string_pos P_ ((int, Lisp_Object));
944static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
945static int number_of_chars P_ ((unsigned char *, int));
946static void compute_stop_pos P_ ((struct it *));
947static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
948 Lisp_Object));
949static int face_before_or_after_it_pos P_ ((struct it *, int));
950static int next_overlay_change P_ ((int));
b2683be4 951static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
a61b7058
GM
952 Lisp_Object, struct text_pos *,
953 int));
06a12811 954static int underlying_face_id P_ ((struct it *));
4bde0ebb
GM
955static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
956 struct window *));
5f5c8ee5
GM
957
958#define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
959#define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
ff6c30e5 960
5f5c8ee5 961#ifdef HAVE_WINDOW_SYSTEM
12adba34 962
e037b9ec
GM
963static void update_tool_bar P_ ((struct frame *, int));
964static void build_desired_tool_bar_string P_ ((struct frame *f));
965static int redisplay_tool_bar P_ ((struct frame *));
966static void display_tool_bar_line P_ ((struct it *));
fa3c6b4d
KS
967static void notice_overwritten_cursor P_ ((struct window *,
968 enum glyph_row_area,
969 int, int, int, int));
970
971
12adba34 972
5f5c8ee5 973#endif /* HAVE_WINDOW_SYSTEM */
12adba34 974
5f5c8ee5
GM
975\f
976/***********************************************************************
977 Window display dimensions
978 ***********************************************************************/
12adba34 979
40a301b3
RS
980/* Return the bottom boundary y-position for text lines in window W.
981 This is the first y position at which a line cannot start.
982 It is relative to the top of the window.
983
984 This is the height of W minus the height of a mode line, if any. */
5f5c8ee5
GM
985
986INLINE int
987window_text_bottom_y (w)
988 struct window *w;
989{
da8b7f4f 990 int height = WINDOW_TOTAL_HEIGHT (w);
1a578e9b 991
5f5c8ee5
GM
992 if (WINDOW_WANTS_MODELINE_P (w))
993 height -= CURRENT_MODE_LINE_HEIGHT (w);
994 return height;
f88eb0b6
KH
995}
996
5f5c8ee5 997/* Return the pixel width of display area AREA of window W. AREA < 0
b46952ae 998 means return the total width of W, not including fringes to
5f5c8ee5 999 the left and right of the window. */
ff6c30e5 1000
5f5c8ee5
GM
1001INLINE int
1002window_box_width (w, area)
1003 struct window *w;
1004 int area;
1005{
da8b7f4f
KS
1006 int cols = XFASTINT (w->total_cols);
1007 int pixels = 0;
2311178e 1008
5f5c8ee5 1009 if (!w->pseudo_window_p)
ff6c30e5 1010 {
da8b7f4f 1011 cols -= WINDOW_SCROLL_BAR_COLS (w);
2311178e 1012
5f5c8ee5
GM
1013 if (area == TEXT_AREA)
1014 {
da8b7f4f
KS
1015 if (INTEGERP (w->left_margin_cols))
1016 cols -= XFASTINT (w->left_margin_cols);
1017 if (INTEGERP (w->right_margin_cols))
1018 cols -= XFASTINT (w->right_margin_cols);
1019 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
5f5c8ee5
GM
1020 }
1021 else if (area == LEFT_MARGIN_AREA)
da8b7f4f
KS
1022 {
1023 cols = (INTEGERP (w->left_margin_cols)
1024 ? XFASTINT (w->left_margin_cols) : 0);
1025 pixels = 0;
1026 }
5f5c8ee5 1027 else if (area == RIGHT_MARGIN_AREA)
da8b7f4f
KS
1028 {
1029 cols = (INTEGERP (w->right_margin_cols)
1030 ? XFASTINT (w->right_margin_cols) : 0);
1031 pixels = 0;
1032 }
ff6c30e5 1033 }
5f5c8ee5 1034
da8b7f4f 1035 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
ff6c30e5 1036}
1adc55de 1037
1adc55de 1038
5f5c8ee5 1039/* Return the pixel height of the display area of window W, not
4b41cebb 1040 including mode lines of W, if any. */
f88eb0b6 1041
5f5c8ee5
GM
1042INLINE int
1043window_box_height (w)
1044 struct window *w;
f88eb0b6 1045{
5f5c8ee5 1046 struct frame *f = XFRAME (w->frame);
da8b7f4f 1047 int height = WINDOW_TOTAL_HEIGHT (w);
7ecd4937
GM
1048
1049 xassert (height >= 0);
2311178e 1050
d9c9e99c
MB
1051 /* Note: the code below that determines the mode-line/header-line
1052 height is essentially the same as that contained in the macro
1053 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1054 the appropriate glyph row has its `mode_line_p' flag set,
1055 and if it doesn't, uses estimate_mode_line_height instead. */
1056
5f5c8ee5 1057 if (WINDOW_WANTS_MODELINE_P (w))
97dff879
MB
1058 {
1059 struct glyph_row *ml_row
1060 = (w->current_matrix && w->current_matrix->rows
1061 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1062 : 0);
1063 if (ml_row && ml_row->mode_line_p)
1064 height -= ml_row->height;
1065 else
96d2320f 1066 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
97dff879 1067 }
5f5c8ee5 1068
045dee35 1069 if (WINDOW_WANTS_HEADER_LINE_P (w))
97dff879
MB
1070 {
1071 struct glyph_row *hl_row
1072 = (w->current_matrix && w->current_matrix->rows
1073 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1074 : 0);
1075 if (hl_row && hl_row->mode_line_p)
1076 height -= hl_row->height;
1077 else
1078 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1079 }
5f5c8ee5 1080
7c75be36
PJ
1081 /* With a very small font and a mode-line that's taller than
1082 default, we might end up with a negative height. */
1083 return max (0, height);
5992c4f7
KH
1084}
1085
da8b7f4f
KS
1086/* Return the window-relative coordinate of the left edge of display
1087 area AREA of window W. AREA < 0 means return the left edge of the
1088 whole window, to the right of the left fringe of W. */
1089
1090INLINE int
1091window_box_left_offset (w, area)
1092 struct window *w;
1093 int area;
1094{
1095 int x;
1096
1097 if (w->pseudo_window_p)
1098 return 0;
1099
1100 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1101
1102 if (area == TEXT_AREA)
1103 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1104 + window_box_width (w, LEFT_MARGIN_AREA));
1105 else if (area == RIGHT_MARGIN_AREA)
1106 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1107 + window_box_width (w, LEFT_MARGIN_AREA)
1108 + window_box_width (w, TEXT_AREA)
1109 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1110 ? 0
1111 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1112 else if (area == LEFT_MARGIN_AREA
1113 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1114 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1115
1116 return x;
1117}
1118
1119
1120/* Return the window-relative coordinate of the right edge of display
1121 area AREA of window W. AREA < 0 means return the left edge of the
1122 whole window, to the left of the right fringe of W. */
1123
1124INLINE int
1125window_box_right_offset (w, area)
1126 struct window *w;
1127 int area;
1128{
1129 return window_box_left_offset (w, area) + window_box_width (w, area);
1130}
5992c4f7 1131
5f5c8ee5
GM
1132/* Return the frame-relative coordinate of the left edge of display
1133 area AREA of window W. AREA < 0 means return the left edge of the
b46952ae 1134 whole window, to the right of the left fringe of W. */
5992c4f7 1135
5f5c8ee5
GM
1136INLINE int
1137window_box_left (w, area)
1138 struct window *w;
1139 int area;
90adcf20 1140{
5f5c8ee5 1141 struct frame *f = XFRAME (w->frame);
da8b7f4f 1142 int x;
a3788d53 1143
da8b7f4f
KS
1144 if (w->pseudo_window_p)
1145 return FRAME_INTERNAL_BORDER_WIDTH (f);
2311178e 1146
da8b7f4f
KS
1147 x = (WINDOW_LEFT_EDGE_X (w)
1148 + window_box_left_offset (w, area));
73af359d 1149
5f5c8ee5 1150 return x;
2311178e 1151}
90adcf20 1152
b6436d4e 1153
5f5c8ee5
GM
1154/* Return the frame-relative coordinate of the right edge of display
1155 area AREA of window W. AREA < 0 means return the left edge of the
b46952ae 1156 whole window, to the left of the right fringe of W. */
ded34426 1157
5f5c8ee5
GM
1158INLINE int
1159window_box_right (w, area)
1160 struct window *w;
1161 int area;
1162{
1163 return window_box_left (w, area) + window_box_width (w, area);
2311178e
TTN
1164}
1165
5f5c8ee5
GM
1166/* Get the bounding box of the display area AREA of window W, without
1167 mode lines, in frame-relative coordinates. AREA < 0 means the
b46952ae 1168 whole window, not including the left and right fringes of
5f5c8ee5
GM
1169 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1170 coordinates of the upper-left corner of the box. Return in
1171 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1172
1173INLINE void
1174window_box (w, area, box_x, box_y, box_width, box_height)
1175 struct window *w;
1176 int area;
1177 int *box_x, *box_y, *box_width, *box_height;
1178{
da8b7f4f
KS
1179 if (box_width)
1180 *box_width = window_box_width (w, area);
1181 if (box_height)
1182 *box_height = window_box_height (w);
1183 if (box_x)
1184 *box_x = window_box_left (w, area);
1185 if (box_y)
1186 {
1187 *box_y = WINDOW_TOP_EDGE_Y (w);
1188 if (WINDOW_WANTS_HEADER_LINE_P (w))
1189 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1190 }
ded34426 1191}
1adc55de 1192
1adc55de 1193
5f5c8ee5 1194/* Get the bounding box of the display area AREA of window W, without
b46952ae
KS
1195 mode lines. AREA < 0 means the whole window, not including the
1196 left and right fringe of the window. Return in *TOP_LEFT_X
5f5c8ee5
GM
1197 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1198 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1199 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1200 box. */
ded34426 1201
5f5c8ee5
GM
1202INLINE void
1203window_box_edges (w, area, top_left_x, top_left_y,
1204 bottom_right_x, bottom_right_y)
1205 struct window *w;
1206 int area;
1207 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
48ae5f0a 1208{
5f5c8ee5
GM
1209 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1210 bottom_right_y);
1211 *bottom_right_x += *top_left_x;
1212 *bottom_right_y += *top_left_y;
48ae5f0a
KH
1213}
1214
5f5c8ee5
GM
1215
1216\f
1217/***********************************************************************
1218 Utilities
1219 ***********************************************************************/
1220
8b6ea97f
GM
1221/* Return the bottom y-position of the line the iterator IT is in.
1222 This can modify IT's settings. */
1223
1224int
1225line_bottom_y (it)
1226 struct it *it;
1227{
1228 int line_height = it->max_ascent + it->max_descent;
1229 int line_top_y = it->current_y;
2311178e 1230
8b6ea97f
GM
1231 if (line_height == 0)
1232 {
1233 if (last_height)
1234 line_height = last_height;
1235 else if (IT_CHARPOS (*it) < ZV)
1236 {
1237 move_it_by_lines (it, 1, 1);
1238 line_height = (it->max_ascent || it->max_descent
1239 ? it->max_ascent + it->max_descent
1240 : last_height);
1241 }
1242 else
1243 {
1244 struct glyph_row *row = it->glyph_row;
2311178e 1245
8b6ea97f
GM
1246 /* Use the default character height. */
1247 it->glyph_row = NULL;
1248 it->what = IT_CHARACTER;
1249 it->c = ' ';
1250 it->len = 1;
1251 PRODUCE_GLYPHS (it);
1252 line_height = it->ascent + it->descent;
1253 it->glyph_row = row;
1254 }
1255 }
1256
1257 return line_top_y + line_height;
1258}
1259
1260
f15f5495
KS
1261/* Return 1 if position CHARPOS is visible in window W.
1262 If visible, set *X and *Y to pixel coordinates of top left corner.
1263 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
0db95684
GM
1264 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1265 and header-lines heights. */
3a641a69
GM
1266
1267int
f15f5495 1268pos_visible_p (w, charpos, x, y, rtop, rbot, exact_mode_line_heights_p)
3a641a69 1269 struct window *w;
f15f5495 1270 int charpos, *x, *y, *rtop, *rbot, exact_mode_line_heights_p;
3a641a69
GM
1271{
1272 struct it it;
1273 struct text_pos top;
a2962d06 1274 int visible_p = 0;
fcab1954
GM
1275 struct buffer *old_buffer = NULL;
1276
a2962d06
KS
1277 if (noninteractive)
1278 return visible_p;
1279
fcab1954
GM
1280 if (XBUFFER (w->buffer) != current_buffer)
1281 {
1282 old_buffer = current_buffer;
1283 set_buffer_internal_1 (XBUFFER (w->buffer));
1284 }
3a641a69 1285
3a641a69 1286 SET_TEXT_POS_FROM_MARKER (top, w->start);
2311178e 1287
04612a64
GM
1288 /* Compute exact mode line heights, if requested. */
1289 if (exact_mode_line_heights_p)
1290 {
1291 if (WINDOW_WANTS_MODELINE_P (w))
1292 current_mode_line_height
96d2320f 1293 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
04612a64 1294 current_buffer->mode_line_format);
2311178e 1295
04612a64
GM
1296 if (WINDOW_WANTS_HEADER_LINE_P (w))
1297 current_header_line_height
1298 = display_mode_line (w, HEADER_LINE_FACE_ID,
1299 current_buffer->header_line_format);
1300 }
3a641a69 1301
04612a64 1302 start_display (&it, w, top);
e10ee30c
KS
1303 move_it_to (&it, charpos, -1, it.last_visible_y, -1,
1304 MOVE_TO_POS | MOVE_TO_Y);
a13be207
GM
1305
1306 /* Note that we may overshoot because of invisible text. */
1307 if (IT_CHARPOS (it) >= charpos)
3a641a69 1308 {
8964fd83 1309 int top_x = it.current_x;
8b6ea97f 1310 int top_y = it.current_y;
78465f3a 1311 int bottom_y = (last_height = 0, line_bottom_y (&it));
da8b7f4f 1312 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
2311178e 1313
8b6ea97f
GM
1314 if (top_y < window_top_y)
1315 visible_p = bottom_y > window_top_y;
1316 else if (top_y < it.last_visible_y)
fcab1954 1317 visible_p = 1;
8964fd83 1318 if (visible_p)
e893970b 1319 {
8964fd83 1320 *x = top_x;
78465f3a 1321 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
8964fd83
KS
1322 *rtop = max (0, window_top_y - top_y);
1323 *rbot = max (0, bottom_y - it.last_visible_y);
e893970b 1324 }
3a641a69 1325 }
e10ee30c 1326 else
3a641a69 1327 {
e893970b
KS
1328 struct it it2;
1329
1330 it2 = it;
e10ee30c
KS
1331 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1332 move_it_by_lines (&it, 1, 0);
3a641a69
GM
1333 if (charpos < IT_CHARPOS (it))
1334 {
1335 visible_p = 1;
8964fd83
KS
1336 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1337 *x = it2.current_x;
1338 *y = it2.current_y + it2.max_ascent - it2.ascent;
1339 *rtop = max (0, -it2.current_y);
1340 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1341 - it.last_visible_y));
3a641a69
GM
1342 }
1343 }
fcab1954
GM
1344
1345 if (old_buffer)
1346 set_buffer_internal_1 (old_buffer);
04612a64
GM
1347
1348 current_header_line_height = current_mode_line_height = -1;
e893970b 1349
d182d87c 1350 if (visible_p && XFASTINT (w->hscroll) > 0)
8460eb1e 1351 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
69036b87 1352
3a641a69
GM
1353 return visible_p;
1354}
1355
1356
4fdb80f2
GM
1357/* Return the next character from STR which is MAXLEN bytes long.
1358 Return in *LEN the length of the character. This is like
1359 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
9ab8560d 1360 we find one, we return a `?', but with the length of the invalid
4fdb80f2
GM
1361 character. */
1362
1363static INLINE int
7a5b8a93 1364string_char_and_length (str, maxlen, len)
50f80c2f 1365 const unsigned char *str;
7a5b8a93 1366 int maxlen, *len;
4fdb80f2
GM
1367{
1368 int c;
1369
1370 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1371 if (!CHAR_VALID_P (c, 1))
1372 /* We may not change the length here because other places in Emacs
9ab8560d 1373 don't use this function, i.e. they silently accept invalid
4fdb80f2
GM
1374 characters. */
1375 c = '?';
1376
1377 return c;
1378}
1379
1380
1381
5f5c8ee5
GM
1382/* Given a position POS containing a valid character and byte position
1383 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1384
1385static struct text_pos
1386string_pos_nchars_ahead (pos, string, nchars)
1387 struct text_pos pos;
1388 Lisp_Object string;
1389 int nchars;
0b1005ef 1390{
5f5c8ee5
GM
1391 xassert (STRINGP (string) && nchars >= 0);
1392
1393 if (STRING_MULTIBYTE (string))
1394 {
2051c264 1395 int rest = SBYTES (string) - BYTEPOS (pos);
50f80c2f 1396 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
5f5c8ee5
GM
1397 int len;
1398
1399 while (nchars--)
1400 {
4fdb80f2 1401 string_char_and_length (p, rest, &len);
5f5c8ee5
GM
1402 p += len, rest -= len;
1403 xassert (rest >= 0);
1404 CHARPOS (pos) += 1;
1405 BYTEPOS (pos) += len;
1406 }
1407 }
1408 else
1409 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1410
1411 return pos;
0a9dc68b
RS
1412}
1413
0a9dc68b 1414
5f5c8ee5
GM
1415/* Value is the text position, i.e. character and byte position,
1416 for character position CHARPOS in STRING. */
1417
1418static INLINE struct text_pos
1419string_pos (charpos, string)
1420 int charpos;
0a9dc68b 1421 Lisp_Object string;
0a9dc68b 1422{
5f5c8ee5
GM
1423 struct text_pos pos;
1424 xassert (STRINGP (string));
1425 xassert (charpos >= 0);
1426 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1427 return pos;
1428}
1429
1430
1431/* Value is a text position, i.e. character and byte position, for
1432 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1433 means recognize multibyte characters. */
1434
1435static struct text_pos
1436c_string_pos (charpos, s, multibyte_p)
1437 int charpos;
1438 unsigned char *s;
1439 int multibyte_p;
1440{
1441 struct text_pos pos;
1442
1443 xassert (s != NULL);
1444 xassert (charpos >= 0);
1445
1446 if (multibyte_p)
0a9dc68b 1447 {
5f5c8ee5
GM
1448 int rest = strlen (s), len;
1449
1450 SET_TEXT_POS (pos, 0, 0);
1451 while (charpos--)
0a9dc68b 1452 {
4fdb80f2 1453 string_char_and_length (s, rest, &len);
5f5c8ee5
GM
1454 s += len, rest -= len;
1455 xassert (rest >= 0);
1456 CHARPOS (pos) += 1;
1457 BYTEPOS (pos) += len;
0a9dc68b
RS
1458 }
1459 }
5f5c8ee5
GM
1460 else
1461 SET_TEXT_POS (pos, charpos, charpos);
0a9dc68b 1462
5f5c8ee5
GM
1463 return pos;
1464}
0a9dc68b 1465
0a9dc68b 1466
5f5c8ee5
GM
1467/* Value is the number of characters in C string S. MULTIBYTE_P
1468 non-zero means recognize multibyte characters. */
0a9dc68b 1469
5f5c8ee5
GM
1470static int
1471number_of_chars (s, multibyte_p)
1472 unsigned char *s;
1473 int multibyte_p;
1474{
1475 int nchars;
2311178e 1476
5f5c8ee5
GM
1477 if (multibyte_p)
1478 {
1479 int rest = strlen (s), len;
1480 unsigned char *p = (unsigned char *) s;
0a9dc68b 1481
5f5c8ee5
GM
1482 for (nchars = 0; rest > 0; ++nchars)
1483 {
4fdb80f2 1484 string_char_and_length (p, rest, &len);
5f5c8ee5 1485 rest -= len, p += len;
0a9dc68b
RS
1486 }
1487 }
5f5c8ee5
GM
1488 else
1489 nchars = strlen (s);
1490
1491 return nchars;
0b1005ef
KH
1492}
1493
2311178e 1494
5f5c8ee5
GM
1495/* Compute byte position NEWPOS->bytepos corresponding to
1496 NEWPOS->charpos. POS is a known position in string STRING.
1497 NEWPOS->charpos must be >= POS.charpos. */
76412d64 1498
5f5c8ee5
GM
1499static void
1500compute_string_pos (newpos, pos, string)
1501 struct text_pos *newpos, pos;
1502 Lisp_Object string;
76412d64 1503{
5f5c8ee5
GM
1504 xassert (STRINGP (string));
1505 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
2311178e 1506
5f5c8ee5 1507 if (STRING_MULTIBYTE (string))
6fc556fd
KR
1508 *newpos = string_pos_nchars_ahead (pos, string,
1509 CHARPOS (*newpos) - CHARPOS (pos));
5f5c8ee5
GM
1510 else
1511 BYTEPOS (*newpos) = CHARPOS (*newpos);
76412d64
RS
1512}
1513
fa3c6b4d
KS
1514/* EXPORT:
1515 Return an estimation of the pixel height of mode or top lines on
1516 frame F. FACE_ID specifies what line's height to estimate. */
1517
1518int
1519estimate_mode_line_height (f, face_id)
1520 struct frame *f;
1521 enum face_id face_id;
1522{
1523#ifdef HAVE_WINDOW_SYSTEM
1524 if (FRAME_WINDOW_P (f))
1525 {
1526 int height = FONT_HEIGHT (FRAME_FONT (f));
1527
1528 /* This function is called so early when Emacs starts that the face
1529 cache and mode line face are not yet initialized. */
1530 if (FRAME_FACE_CACHE (f))
1531 {
1532 struct face *face = FACE_FROM_ID (f, face_id);
1533 if (face)
1534 {
1535 if (face->font)
1536 height = FONT_HEIGHT (face->font);
1537 if (face->box_line_width > 0)
1538 height += 2 * face->box_line_width;
1539 }
1540 }
1541
1542 return height;
1543 }
1544#endif
1545
1546 return 1;
1547}
1548
e080d3eb
KS
1549/* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1550 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1551 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1552 not force the value into range. */
1553
1554void
1555pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1556 FRAME_PTR f;
1557 register int pix_x, pix_y;
1558 int *x, *y;
1559 NativeRectangle *bounds;
1560 int noclip;
1561{
1562
1563#ifdef HAVE_WINDOW_SYSTEM
1564 if (FRAME_WINDOW_P (f))
1565 {
da8b7f4f 1566 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
e080d3eb
KS
1567 even for negative values. */
1568 if (pix_x < 0)
da8b7f4f 1569 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
e080d3eb 1570 if (pix_y < 0)
da8b7f4f 1571 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
e080d3eb 1572
da8b7f4f
KS
1573 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1574 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
e080d3eb
KS
1575
1576 if (bounds)
1577 STORE_NATIVE_RECT (*bounds,
da8b7f4f
KS
1578 FRAME_COL_TO_PIXEL_X (f, pix_x),
1579 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1580 FRAME_COLUMN_WIDTH (f) - 1,
1581 FRAME_LINE_HEIGHT (f) - 1);
e080d3eb
KS
1582
1583 if (!noclip)
1584 {
1585 if (pix_x < 0)
1586 pix_x = 0;
da8b7f4f
KS
1587 else if (pix_x > FRAME_TOTAL_COLS (f))
1588 pix_x = FRAME_TOTAL_COLS (f);
e080d3eb
KS
1589
1590 if (pix_y < 0)
1591 pix_y = 0;
da8b7f4f
KS
1592 else if (pix_y > FRAME_LINES (f))
1593 pix_y = FRAME_LINES (f);
e080d3eb
KS
1594 }
1595 }
1596#endif
1597
1598 *x = pix_x;
1599 *y = pix_y;
1600}
1601
1602
1603/* Given HPOS/VPOS in the current matrix of W, return corresponding
1604 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1605 can't tell the positions because W's display is not up to date,
1606 return 0. */
1607
1608int
1609glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1610 struct window *w;
1611 int hpos, vpos;
1612 int *frame_x, *frame_y;
1613{
1614#ifdef HAVE_WINDOW_SYSTEM
1615 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1616 {
1617 int success_p;
1618
1619 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1620 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1621
1622 if (display_completed)
1623 {
1624 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1625 struct glyph *glyph = row->glyphs[TEXT_AREA];
1626 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1627
1628 hpos = row->x;
1629 vpos = row->y;
1630 while (glyph < end)
1631 {
1632 hpos += glyph->pixel_width;
1633 ++glyph;
1634 }
1635
f65536fa
KS
1636 /* If first glyph is partially visible, its first visible position is still 0. */
1637 if (hpos < 0)
1638 hpos = 0;
1639
e080d3eb
KS
1640 success_p = 1;
1641 }
1642 else
1643 {
1644 hpos = vpos = 0;
1645 success_p = 0;
1646 }
1647
1648 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1649 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1650 return success_p;
1651 }
1652#endif
1653
1654 *frame_x = hpos;
1655 *frame_y = vpos;
1656 return 1;
1657}
1658
1659
fa3c6b4d
KS
1660#ifdef HAVE_WINDOW_SYSTEM
1661
1662/* Find the glyph under window-relative coordinates X/Y in window W.
1663 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1664 strings. Return in *HPOS and *VPOS the row and column number of
1665 the glyph found. Return in *AREA the glyph area containing X.
1666 Value is a pointer to the glyph found or null if X/Y is not on
1667 text, or we can't tell because W's current matrix is not up to
1668 date. */
1669
1670static struct glyph *
493fdc3c 1671x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
fa3c6b4d
KS
1672 struct window *w;
1673 int x, y;
493fdc3c 1674 int *hpos, *vpos, *dx, *dy, *area;
fa3c6b4d
KS
1675{
1676 struct glyph *glyph, *end;
1677 struct glyph_row *row = NULL;
da8b7f4f 1678 int x0, i;
fa3c6b4d
KS
1679
1680 /* Find row containing Y. Give up if some row is not enabled. */
1681 for (i = 0; i < w->current_matrix->nrows; ++i)
1682 {
1683 row = MATRIX_ROW (w->current_matrix, i);
1684 if (!row->enabled_p)
1685 return NULL;
1686 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1687 break;
1688 }
1689
1690 *vpos = i;
1691 *hpos = 0;
1692
1693 /* Give up if Y is not in the window. */
1694 if (i == w->current_matrix->nrows)
1695 return NULL;
1696
1697 /* Get the glyph area containing X. */
1698 if (w->pseudo_window_p)
1699 {
1700 *area = TEXT_AREA;
1701 x0 = 0;
1702 }
1703 else
1704 {
da8b7f4f 1705 if (x < window_box_left_offset (w, TEXT_AREA))
fa3c6b4d
KS
1706 {
1707 *area = LEFT_MARGIN_AREA;
da8b7f4f 1708 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
fa3c6b4d 1709 }
da8b7f4f 1710 else if (x < window_box_right_offset (w, TEXT_AREA))
fa3c6b4d
KS
1711 {
1712 *area = TEXT_AREA;
f65536fa 1713 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
fa3c6b4d
KS
1714 }
1715 else
1716 {
1717 *area = RIGHT_MARGIN_AREA;
da8b7f4f 1718 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
fa3c6b4d
KS
1719 }
1720 }
1721
1722 /* Find glyph containing X. */
1723 glyph = row->glyphs[*area];
1724 end = glyph + row->used[*area];
493fdc3c
KS
1725 x -= x0;
1726 while (glyph < end && x >= glyph->pixel_width)
fa3c6b4d 1727 {
493fdc3c 1728 x -= glyph->pixel_width;
fa3c6b4d
KS
1729 ++glyph;
1730 }
1731
1732 if (glyph == end)
1733 return NULL;
1734
493fdc3c
KS
1735 if (dx)
1736 {
1737 *dx = x;
1738 *dy = y - (row->y + row->ascent - glyph->ascent);
1739 }
1740
fa3c6b4d
KS
1741 *hpos = glyph - row->glyphs[*area];
1742 return glyph;
1743}
1744
1745
1746/* EXPORT:
1747 Convert frame-relative x/y to coordinates relative to window W.
1748 Takes pseudo-windows into account. */
1749
1750void
1751frame_to_window_pixel_xy (w, x, y)
1752 struct window *w;
1753 int *x, *y;
1754{
1755 if (w->pseudo_window_p)
1756 {
1757 /* A pseudo-window is always full-width, and starts at the
1758 left edge of the frame, plus a frame border. */
1759 struct frame *f = XFRAME (w->frame);
da8b7f4f 1760 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
fa3c6b4d
KS
1761 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1762 }
1763 else
1764 {
da8b7f4f 1765 *x -= WINDOW_LEFT_EDGE_X (w);
fa3c6b4d
KS
1766 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1767 }
1768}
1769
1770/* EXPORT:
a66678fd
YM
1771 Return in RECTS[] at most N clipping rectangles for glyph string S.
1772 Return the number of stored rectangles. */
fa3c6b4d 1773
a66678fd
YM
1774int
1775get_glyph_string_clip_rects (s, rects, n)
fa3c6b4d 1776 struct glyph_string *s;
a66678fd
YM
1777 NativeRectangle *rects;
1778 int n;
fa3c6b4d
KS
1779{
1780 XRectangle r;
1781
a66678fd
YM
1782 if (n <= 0)
1783 return 0;
1784
fa3c6b4d
KS
1785 if (s->row->full_width_p)
1786 {
da8b7f4f
KS
1787 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1788 r.x = WINDOW_LEFT_EDGE_X (s->w);
1789 r.width = WINDOW_TOTAL_WIDTH (s->w);
fa3c6b4d
KS
1790
1791 /* Unless displaying a mode or menu bar line, which are always
1792 fully visible, clip to the visible part of the row. */
1793 if (s->w->pseudo_window_p)
1794 r.height = s->row->visible_height;
1795 else
1796 r.height = s->height;
1797 }
1798 else
1799 {
1800 /* This is a text line that may be partially visible. */
da8b7f4f 1801 r.x = window_box_left (s->w, s->area);
fa3c6b4d
KS
1802 r.width = window_box_width (s->w, s->area);
1803 r.height = s->row->visible_height;
1804 }
1805
8c2da0fa
ST
1806 if (s->clip_head)
1807 if (r.x < s->clip_head->x)
1808 {
1809 if (r.width >= s->clip_head->x - r.x)
1810 r.width -= s->clip_head->x - r.x;
1811 else
1812 r.width = 0;
1813 r.x = s->clip_head->x;
1814 }
1815 if (s->clip_tail)
1816 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1817 {
1818 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1819 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1820 else
1821 r.width = 0;
1822 }
1823
fa3c6b4d
KS
1824 /* If S draws overlapping rows, it's sufficient to use the top and
1825 bottom of the window for clipping because this glyph string
1826 intentionally draws over other lines. */
a66678fd 1827 if (s->for_overlaps)
fa3c6b4d 1828 {
da8b7f4f 1829 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
fa3c6b4d 1830 r.height = window_text_bottom_y (s->w) - r.y;
a66678fd
YM
1831
1832 /* Alas, the above simple strategy does not work for the
1833 environments with anti-aliased text: if the same text is
1834 drawn onto the same place multiple times, it gets thicker.
1835 If the overlap we are processing is for the erased cursor, we
1836 take the intersection with the rectagle of the cursor. */
1837 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1838 {
1839 XRectangle rc, r_save = r;
1840
1841 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1842 rc.y = s->w->phys_cursor.y;
1843 rc.width = s->w->phys_cursor_width;
1844 rc.height = s->w->phys_cursor_height;
1845
1846 x_intersect_rectangles (&r_save, &rc, &r);
1847 }
fa3c6b4d
KS
1848 }
1849 else
1850 {
1851 /* Don't use S->y for clipping because it doesn't take partially
1852 visible lines into account. For example, it can be negative for
1853 partially visible lines at the top of a window. */
1854 if (!s->row->full_width_p
1855 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
da8b7f4f 1856 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
fa3c6b4d
KS
1857 else
1858 r.y = max (0, s->row->y);
1859
1860 /* If drawing a tool-bar window, draw it over the internal border
1861 at the top of the window. */
94c4db3c
SM
1862 if (WINDOWP (s->f->tool_bar_window)
1863 && s->w == XWINDOW (s->f->tool_bar_window))
b4ebbb12 1864 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
fa3c6b4d
KS
1865 }
1866
1867 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1868
fa3c6b4d
KS
1869 /* If drawing the cursor, don't let glyph draw outside its
1870 advertised boundaries. Cleartype does this under some circumstances. */
1871 if (s->hl == DRAW_CURSOR)
1872 {
493fdc3c 1873 struct glyph *glyph = s->first_glyph;
999f5a60 1874 int height, max_y;
493fdc3c 1875
fa3c6b4d
KS
1876 if (s->x > r.x)
1877 {
1878 r.width -= s->x - r.x;
1879 r.x = s->x;
1880 }
493fdc3c
KS
1881 r.width = min (r.width, glyph->pixel_width);
1882
999f5a60
KS
1883 /* If r.y is below window bottom, ensure that we still see a cursor. */
1884 height = min (glyph->ascent + glyph->descent,
1885 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1886 max_y = window_text_bottom_y (s->w) - height;
1887 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1888 if (s->ybase - glyph->ascent > max_y)
493fdc3c 1889 {
999f5a60
KS
1890 r.y = max_y;
1891 r.height = height;
1892 }
1893 else
1894 {
1895 /* Don't draw cursor glyph taller than our actual glyph. */
1896 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1897 if (height < r.height)
1898 {
1899 max_y = r.y + r.height;
1900 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1901 r.height = min (max_y - r.y, height);
1902 }
493fdc3c 1903 }
fa3c6b4d 1904 }
fa3c6b4d 1905
a66678fd
YM
1906 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1907 || (s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1)
1908 {
fa3c6b4d 1909#ifdef CONVERT_FROM_XRECT
a66678fd 1910 CONVERT_FROM_XRECT (r, *rects);
fa3c6b4d 1911#else
a66678fd
YM
1912 *rects = r;
1913#endif
1914 return 1;
1915 }
1916 else
1917 {
1918 /* If we are processing overlapping and allowed to return
1919 multiple clipping rectangles, we exclude the row of the glyph
1920 string from the clipping rectangle. This is to avoid drawing
1921 the same text on the environment with anti-aliasing. */
1922#ifdef CONVERT_FROM_XRECT
1923 XRectangle rs[2];
1924#else
1925 XRectangle *rs = rects;
1926#endif
1927 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1928
1929 if (s->for_overlaps & OVERLAPS_PRED)
1930 {
1931 rs[i] = r;
1932 if (r.y + r.height > row_y)
1933 if (r.y < row_y)
1934 rs[i].height = row_y - r.y;
1935 else
1936 rs[i].height = 0;
1937 i++;
1938 }
1939 if (s->for_overlaps & OVERLAPS_SUCC)
1940 {
1941 rs[i] = r;
1942 if (r.y < row_y + s->row->visible_height)
1943 if (r.y + r.height > row_y + s->row->visible_height)
1944 {
1945 rs[i].y = row_y + s->row->visible_height;
1946 rs[i].height = r.y + r.height - rs[i].y;
1947 }
1948 else
1949 rs[i].height = 0;
1950 i++;
1951 }
1952
1953 n = i;
1954#ifdef CONVERT_FROM_XRECT
1955 for (i = 0; i < n; i++)
1956 CONVERT_FROM_XRECT (rs[i], rects[i]);
fd4c9408 1957#endif
a66678fd
YM
1958 return n;
1959 }
1960}
1961
1962/* EXPORT:
1963 Return in *NR the clipping rectangle for glyph string S. */
1964
1965void
1966get_glyph_string_clip_rect (s, nr)
1967 struct glyph_string *s;
1968 NativeRectangle *nr;
1969{
1970 get_glyph_string_clip_rects (s, nr, 1);
fa3c6b4d
KS
1971}
1972
2049265a
KS
1973
1974/* EXPORT:
1975 Return the position and height of the phys cursor in window W.
1976 Set w->phys_cursor_width to width of phys cursor.
1977*/
1978
1979int
1980get_phys_cursor_geometry (w, row, glyph, heightp)
1981 struct window *w;
1982 struct glyph_row *row;
1983 struct glyph *glyph;
1984 int *heightp;
1985{
1986 struct frame *f = XFRAME (WINDOW_FRAME (w));
b78ab259 1987 int y, wd, h, h0, y0;
2049265a
KS
1988
1989 /* Compute the width of the rectangle to draw. If on a stretch
1990 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1991 rectangle as wide as the glyph, but use a canonical character
1992 width instead. */
1993 wd = glyph->pixel_width - 1;
1994#ifdef HAVE_NTGUI
1995 wd++; /* Why? */
1996#endif
1997 if (glyph->type == STRETCH_GLYPH
1998 && !x_stretch_cursor_p)
1999 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2000 w->phys_cursor_width = wd;
2001
2002 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2003
2004 /* If y is below window bottom, ensure that we still see a cursor. */
2005 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2006
2007 h = max (h0, glyph->ascent + glyph->descent);
2008 h0 = min (h0, glyph->ascent + glyph->descent);
2009
2010 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2011 if (y < y0)
2012 {
2013 h = max (h - (y0 - y) + 1, h0);
2014 y = y0 - 1;
2015 }
2016 else
2017 {
2018 y0 = window_text_bottom_y (w) - h0;
2019 if (y > y0)
2020 {
2021 h += y - y0;
2022 y = y0;
2023 }
2024 }
2025
2026 *heightp = h - 1;
2027 return WINDOW_TO_FRAME_PIXEL_Y (w, y);
2028}
2029
984c107d
KS
2030/*
2031 * Remember which glyph the mouse is over.
2032 */
2033
2034void
2035remember_mouse_glyph (f, gx, gy, rect)
2036 struct frame *f;
2037 int gx, gy;
2038 NativeRectangle *rect;
2039{
2040 Lisp_Object window;
2041 struct window *w;
2042 struct glyph_row *r, *gr, *end_row;
2043 enum window_part part;
2044 enum glyph_row_area area;
2045 int x, y, width, height;
2046
2047 /* Try to determine frame pixel position and size of the glyph under
2048 frame pixel coordinates X/Y on frame F. */
2049
2050 window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0);
2051 if (NILP (window))
2052 {
2053 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2054 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2055 goto virtual_glyph;
2056 }
2057
2058 w = XWINDOW (window);
2059 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2060 height = WINDOW_FRAME_LINE_HEIGHT (w);
2061
2062 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
76d8f04c 2063 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
984c107d
KS
2064
2065 if (w->pseudo_window_p)
2066 {
2067 area = TEXT_AREA;
2068 part = ON_MODE_LINE; /* Don't adjust margin. */
2069 goto text_glyph;
2070 }
2071
2072 switch (part)
2073 {
2074 case ON_LEFT_MARGIN:
2075 area = LEFT_MARGIN_AREA;
2076 goto text_glyph;
2077
2078 case ON_RIGHT_MARGIN:
2079 area = RIGHT_MARGIN_AREA;
2080 goto text_glyph;
2081
984c107d 2082 case ON_HEADER_LINE:
76d8f04c
YM
2083 case ON_MODE_LINE:
2084 gr = (part == ON_HEADER_LINE
2085 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2086 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2087 gy = gr->y;
2088 area = TEXT_AREA;
2089 goto text_glyph_row_found;
2090
2091 case ON_TEXT:
984c107d
KS
2092 area = TEXT_AREA;
2093
2094 text_glyph:
2095 gr = 0; gy = 0;
76d8f04c 2096 for (; r <= end_row && r->enabled_p; ++r)
984c107d
KS
2097 if (r->y + r->height > y)
2098 {
2099 gr = r; gy = r->y;
2100 break;
2101 }
2102
76d8f04c 2103 text_glyph_row_found:
984c107d
KS
2104 if (gr && gy <= y)
2105 {
2106 struct glyph *g = gr->glyphs[area];
2107 struct glyph *end = g + gr->used[area];
2108
2109 height = gr->height;
2110 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2111 if (gx + g->pixel_width > x)
2112 break;
2113
2114 if (g < end)
8460eb1e
KS
2115 {
2116 if (g->type == IMAGE_GLYPH)
2117 {
2118 /* Don't remember when mouse is over image, as
2119 image may have hot-spots. */
2120 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2121 return;
2122 }
2123 width = g->pixel_width;
2124 }
984c107d
KS
2125 else
2126 {
2127 /* Use nominal char spacing at end of line. */
2128 x -= gx;
2129 gx += (x / width) * width;
2130 }
2131
2132 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2133 gx += window_box_left_offset (w, area);
2134 }
2135 else
2136 {
2137 /* Use nominal line height at end of window. */
2138 gx = (x / width) * width;
2139 y -= gy;
2140 gy += (y / height) * height;
2141 }
2142 break;
2143
2144 case ON_LEFT_FRINGE:
2145 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2146 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2147 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2148 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2149 goto row_glyph;
2150
2151 case ON_RIGHT_FRINGE:
2152 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2153 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2154 : window_box_right_offset (w, TEXT_AREA));
2155 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2156 goto row_glyph;
2157
2158 case ON_SCROLL_BAR:
2159 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2160 ? 0
2161 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2162 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2163 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2164 : 0)));
2165 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2166
2167 row_glyph:
2168 gr = 0, gy = 0;
76d8f04c 2169 for (; r <= end_row && r->enabled_p; ++r)
984c107d
KS
2170 if (r->y + r->height > y)
2171 {
2172 gr = r; gy = r->y;
2173 break;
2174 }
2175
2176 if (gr && gy <= y)
2177 height = gr->height;
2178 else
2179 {
2180 /* Use nominal line height at end of window. */
2181 y -= gy;
2182 gy += (y / height) * height;
2183 }
2184 break;
2185
2186 default:
2187 ;
2188 virtual_glyph:
2189 /* If there is no glyph under the mouse, then we divide the screen
2190 into a grid of the smallest glyph in the frame, and use that
2191 as our "glyph". */
2192
2193 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2194 round down even for negative values. */
2195 if (gx < 0)
2196 gx -= width - 1;
2197 if (gy < 0)
2198 gy -= height - 1;
2199
2200 gx = (gx / width) * width;
2201 gy = (gy / height) * height;
2202
2203 goto store_rect;
2204 }
2205
2206 gx += WINDOW_LEFT_EDGE_X (w);
2207 gy += WINDOW_TOP_EDGE_Y (w);
2208
2209 store_rect:
2210 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2211
2212 /* Visible feedback for debugging. */
2213#if 0
2214#if HAVE_X_WINDOWS
2215 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2216 f->output_data.x->normal_gc,
2217 gx, gy, width, height);
2218#endif
2219#endif
2220}
2221
2049265a 2222
fa3c6b4d 2223#endif /* HAVE_WINDOW_SYSTEM */
9c74a0dd 2224
5f5c8ee5
GM
2225\f
2226/***********************************************************************
2227 Lisp form evaluation
2228 ***********************************************************************/
2229
116d6f5c 2230/* Error handler for safe_eval and safe_call. */
5f5c8ee5
GM
2231
2232static Lisp_Object
116d6f5c 2233safe_eval_handler (arg)
5f5c8ee5
GM
2234 Lisp_Object arg;
2235{
0dbf9fd2 2236 add_to_log ("Error during redisplay: %s", arg, Qnil);
5f5c8ee5
GM
2237 return Qnil;
2238}
2239
2240
2241/* Evaluate SEXPR and return the result, or nil if something went
2913a9c0 2242 wrong. Prevent redisplay during the evaluation. */
5f5c8ee5 2243
71e5b1b8 2244Lisp_Object
116d6f5c 2245safe_eval (sexpr)
5f5c8ee5
GM
2246 Lisp_Object sexpr;
2247{
5f5c8ee5 2248 Lisp_Object val;
2311178e 2249
30a3f61c
GM
2250 if (inhibit_eval_during_redisplay)
2251 val = Qnil;
2252 else
2253 {
331379bf 2254 int count = SPECPDL_INDEX ();
30a3f61c 2255 struct gcpro gcpro1;
0d8b31c0 2256
30a3f61c
GM
2257 GCPRO1 (sexpr);
2258 specbind (Qinhibit_redisplay, Qt);
7033d6df
RS
2259 /* Use Qt to ensure debugger does not run,
2260 so there is no possibility of wanting to redisplay. */
2261 val = internal_condition_case_1 (Feval, sexpr, Qt,
30a3f61c
GM
2262 safe_eval_handler);
2263 UNGCPRO;
2264 val = unbind_to (count, val);
2265 }
2311178e 2266
30a3f61c 2267 return val;
0d8b31c0
GM
2268}
2269
2270
2271/* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2913a9c0
GM
2272 Return the result, or nil if something went wrong. Prevent
2273 redisplay during the evaluation. */
0d8b31c0
GM
2274
2275Lisp_Object
116d6f5c 2276safe_call (nargs, args)
0d8b31c0
GM
2277 int nargs;
2278 Lisp_Object *args;
2279{
0d8b31c0 2280 Lisp_Object val;
2311178e 2281
30a3f61c
GM
2282 if (inhibit_eval_during_redisplay)
2283 val = Qnil;
2284 else
2285 {
331379bf 2286 int count = SPECPDL_INDEX ();
30a3f61c 2287 struct gcpro gcpro1;
0d8b31c0 2288
30a3f61c
GM
2289 GCPRO1 (args[0]);
2290 gcpro1.nvars = nargs;
2291 specbind (Qinhibit_redisplay, Qt);
7033d6df
RS
2292 /* Use Qt to ensure debugger does not run,
2293 so there is no possibility of wanting to redisplay. */
2294 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
30a3f61c
GM
2295 safe_eval_handler);
2296 UNGCPRO;
2297 val = unbind_to (count, val);
2298 }
2299
2300 return val;
5f5c8ee5
GM
2301}
2302
2303
116d6f5c
GM
2304/* Call function FN with one argument ARG.
2305 Return the result, or nil if something went wrong. */
2306
2307Lisp_Object
2308safe_call1 (fn, arg)
2309 Lisp_Object fn, arg;
2310{
2311 Lisp_Object args[2];
2312 args[0] = fn;
2313 args[1] = arg;
2314 return safe_call (2, args);
2315}
2316
2317
5f5c8ee5
GM
2318\f
2319/***********************************************************************
2320 Debugging
2321 ***********************************************************************/
2322
2323#if 0
2324
2325/* Define CHECK_IT to perform sanity checks on iterators.
2326 This is for debugging. It is too slow to do unconditionally. */
2327
2328static void
2329check_it (it)
2330 struct it *it;
2331{
a6f82cc4 2332 if (it->method == GET_FROM_STRING)
a2889657 2333 {
5f5c8ee5
GM
2334 xassert (STRINGP (it->string));
2335 xassert (IT_STRING_CHARPOS (*it) >= 0);
2336 }
7d8a0b55 2337 else
5f5c8ee5 2338 {
7d8a0b55 2339 xassert (IT_STRING_CHARPOS (*it) < 0);
a6f82cc4 2340 if (it->method == GET_FROM_BUFFER)
7d8a0b55
MB
2341 {
2342 /* Check that character and byte positions agree. */
2343 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2344 }
5f5c8ee5 2345 }
73af359d 2346
5f5c8ee5
GM
2347 if (it->dpvec)
2348 xassert (it->current.dpvec_index >= 0);
2349 else
2350 xassert (it->current.dpvec_index < 0);
2351}
1f40cad2 2352
5f5c8ee5
GM
2353#define CHECK_IT(IT) check_it ((IT))
2354
2355#else /* not 0 */
2356
2357#define CHECK_IT(IT) (void) 0
2358
2359#endif /* not 0 */
2360
2361
2362#if GLYPH_DEBUG
2363
2364/* Check that the window end of window W is what we expect it
2365 to be---the last row in the current matrix displaying text. */
2366
2367static void
2368check_window_end (w)
2369 struct window *w;
2370{
2371 if (!MINI_WINDOW_P (w)
2372 && !NILP (w->window_end_valid))
2373 {
2374 struct glyph_row *row;
2375 xassert ((row = MATRIX_ROW (w->current_matrix,
2376 XFASTINT (w->window_end_vpos)),
2377 !row->enabled_p
2378 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2379 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2380 }
2381}
2382
2383#define CHECK_WINDOW_END(W) check_window_end ((W))
2384
2385#else /* not GLYPH_DEBUG */
2386
2387#define CHECK_WINDOW_END(W) (void) 0
2388
2389#endif /* not GLYPH_DEBUG */
2390
2391
2392\f
2393/***********************************************************************
2394 Iterator initialization
2395 ***********************************************************************/
2396
2397/* Initialize IT for displaying current_buffer in window W, starting
2398 at character position CHARPOS. CHARPOS < 0 means that no buffer
2399 position is specified which is useful when the iterator is assigned
2400 a position later. BYTEPOS is the byte position corresponding to
3ebf0ea9 2401 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
5f5c8ee5
GM
2402
2403 If ROW is not null, calls to produce_glyphs with IT as parameter
2404 will produce glyphs in that row.
2405
2406 BASE_FACE_ID is the id of a base face to use. It must be one of
96d2320f
KS
2407 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2408 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2409 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2311178e 2410
96d2320f
KS
2411 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2412 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2413 will be initialized to use the corresponding mode line glyph row of
2414 the desired matrix of W. */
5f5c8ee5
GM
2415
2416void
2417init_iterator (it, w, charpos, bytepos, row, base_face_id)
2418 struct it *it;
2419 struct window *w;
2420 int charpos, bytepos;
2421 struct glyph_row *row;
2422 enum face_id base_face_id;
2423{
2424 int highlight_region_p;
5f5c8ee5
GM
2425
2426 /* Some precondition checks. */
2427 xassert (w != NULL && it != NULL);
3b6b6db7
SM
2428 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2429 && charpos <= ZV));
5f5c8ee5
GM
2430
2431 /* If face attributes have been changed since the last redisplay,
2432 free realized faces now because they depend on face definitions
7d0393cf 2433 that might have changed. Don't free faces while there might be
1987b083 2434 desired matrices pending which reference these faces. */
26683087 2435 if (face_change_count && !inhibit_free_realized_faces)
5f5c8ee5
GM
2436 {
2437 face_change_count = 0;
2438 free_all_realized_faces (Qnil);
2439 }
2440
2441 /* Use one of the mode line rows of W's desired matrix if
2442 appropriate. */
2443 if (row == NULL)
2444 {
96d2320f
KS
2445 if (base_face_id == MODE_LINE_FACE_ID
2446 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
5f5c8ee5 2447 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
045dee35
GM
2448 else if (base_face_id == HEADER_LINE_FACE_ID)
2449 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
5f5c8ee5 2450 }
2311178e 2451
5f5c8ee5
GM
2452 /* Clear IT. */
2453 bzero (it, sizeof *it);
2454 it->current.overlay_string_index = -1;
2455 it->current.dpvec_index = -1;
5f5c8ee5 2456 it->base_face_id = base_face_id;
7d8a0b55
MB
2457 it->string = Qnil;
2458 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5f5c8ee5
GM
2459
2460 /* The window in which we iterate over current_buffer: */
2461 XSETWINDOW (it->window, w);
2462 it->w = w;
2463 it->f = XFRAME (w->frame);
2464
d475bcb8
GM
2465 /* Extra space between lines (on window systems only). */
2466 if (base_face_id == DEFAULT_FACE_ID
2467 && FRAME_WINDOW_P (it->f))
2468 {
2469 if (NATNUMP (current_buffer->extra_line_spacing))
2470 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
a9e5c932
KS
2471 else if (FLOATP (current_buffer->extra_line_spacing))
2472 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2473 * FRAME_LINE_HEIGHT (it->f));
d475bcb8
GM
2474 else if (it->f->extra_line_spacing > 0)
2475 it->extra_line_spacing = it->f->extra_line_spacing;
546f98b8 2476 it->max_extra_line_spacing = 0;
d475bcb8
GM
2477 }
2478
5f5c8ee5 2479 /* If realized faces have been removed, e.g. because of face
62be9979
GM
2480 attribute changes of named faces, recompute them. When running
2481 in batch mode, the face cache of Vterminal_frame is null. If
2482 we happen to get called, make a dummy face cache. */
a24d4cb2 2483 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
62be9979 2484 init_frame_faces (it->f);
5f5c8ee5
GM
2485 if (FRAME_FACE_CACHE (it->f)->used == 0)
2486 recompute_basic_faces (it->f);
2487
e893970b
KS
2488 /* Current value of the `slice', `space-width', and 'height' properties. */
2489 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
5f5c8ee5
GM
2490 it->space_width = Qnil;
2491 it->font_height = Qnil;
a9e5c932 2492 it->override_ascent = -1;
2311178e 2493
5f5c8ee5
GM
2494 /* Are control characters displayed as `^C'? */
2495 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2496
2497 /* -1 means everything between a CR and the following line end
2498 is invisible. >0 means lines indented more than this value are
2499 invisible. */
2500 it->selective = (INTEGERP (current_buffer->selective_display)
2501 ? XFASTINT (current_buffer->selective_display)
2311178e 2502 : (!NILP (current_buffer->selective_display)
5f5c8ee5
GM
2503 ? -1 : 0));
2504 it->selective_display_ellipsis_p
2505 = !NILP (current_buffer->selective_display_ellipses);
2506
2507 /* Display table to use. */
2508 it->dp = window_display_table (w);
2509
2510 /* Are multibyte characters enabled in current_buffer? */
2511 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2512
2513 /* Non-zero if we should highlight the region. */
2514 highlight_region_p
3ebf0ea9 2515 = (!NILP (Vtransient_mark_mode)
5f5c8ee5
GM
2516 && !NILP (current_buffer->mark_active)
2517 && XMARKER (current_buffer->mark)->buffer != 0);
2518
2519 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2520 start and end of a visible region in window IT->w. Set both to
2521 -1 to indicate no region. */
2522 if (highlight_region_p
2523 /* Maybe highlight only in selected window. */
2311178e 2524 && (/* Either show region everywhere. */
5f5c8ee5
GM
2525 highlight_nonselected_windows
2526 /* Or show region in the selected window. */
2527 || w == XWINDOW (selected_window)
2528 /* Or show the region if we are in the mini-buffer and W is
2529 the window the mini-buffer refers to. */
2530 || (MINI_WINDOW_P (XWINDOW (selected_window))
5705966b
KS
2531 && WINDOWP (minibuf_selected_window)
2532 && w == XWINDOW (minibuf_selected_window))))
5f5c8ee5
GM
2533 {
2534 int charpos = marker_position (current_buffer->mark);
2535 it->region_beg_charpos = min (PT, charpos);
2536 it->region_end_charpos = max (PT, charpos);
2537 }
2538 else
2539 it->region_beg_charpos = it->region_end_charpos = -1;
2540
2541 /* Get the position at which the redisplay_end_trigger hook should
2542 be run, if it is to be run at all. */
2543 if (MARKERP (w->redisplay_end_trigger)
2544 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2545 it->redisplay_end_trigger_charpos
2546 = marker_position (w->redisplay_end_trigger);
2547 else if (INTEGERP (w->redisplay_end_trigger))
2548 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2549
2550 /* Correct bogus values of tab_width. */
2551 it->tab_width = XINT (current_buffer->tab_width);
2552 if (it->tab_width <= 0 || it->tab_width > 1000)
2553 it->tab_width = 8;
2554
2555 /* Are lines in the display truncated? */
2556 it->truncate_lines_p
2557 = (base_face_id != DEFAULT_FACE_ID
2558 || XINT (it->w->hscroll)
2559 || (truncate_partial_width_windows
2560 && !WINDOW_FULL_WIDTH_P (it->w))
2561 || !NILP (current_buffer->truncate_lines));
2562
2563 /* Get dimensions of truncation and continuation glyphs. These are
b46952ae 2564 displayed as fringe bitmaps under X, so we don't need them for such
5f5c8ee5
GM
2565 frames. */
2566 if (!FRAME_WINDOW_P (it->f))
2567 {
2568 if (it->truncate_lines_p)
2569 {
2570 /* We will need the truncation glyph. */
2571 xassert (it->glyph_row == NULL);
2572 produce_special_glyphs (it, IT_TRUNCATION);
2573 it->truncation_pixel_width = it->pixel_width;
2574 }
2575 else
2576 {
2577 /* We will need the continuation glyph. */
2578 xassert (it->glyph_row == NULL);
2579 produce_special_glyphs (it, IT_CONTINUATION);
2580 it->continuation_pixel_width = it->pixel_width;
2581 }
2582
153c2160 2583 /* Reset these values to zero because the produce_special_glyphs
5f5c8ee5
GM
2584 above has changed them. */
2585 it->pixel_width = it->ascent = it->descent = 0;
312246d1 2586 it->phys_ascent = it->phys_descent = 0;
5f5c8ee5
GM
2587 }
2588
2589 /* Set this after getting the dimensions of truncation and
2590 continuation glyphs, so that we don't produce glyphs when calling
2591 produce_special_glyphs, above. */
2592 it->glyph_row = row;
2593 it->area = TEXT_AREA;
2594
2595 /* Get the dimensions of the display area. The display area
2596 consists of the visible window area plus a horizontally scrolled
2597 part to the left of the window. All x-values are relative to the
2598 start of this total display area. */
2599 if (base_face_id != DEFAULT_FACE_ID)
2600 {
2601 /* Mode lines, menu bar in terminal frames. */
2602 it->first_visible_x = 0;
da8b7f4f 2603 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
5f5c8ee5
GM
2604 }
2605 else
2606 {
2607 it->first_visible_x
da8b7f4f 2608 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
5f5c8ee5
GM
2609 it->last_visible_x = (it->first_visible_x
2610 + window_box_width (w, TEXT_AREA));
2611
2612 /* If we truncate lines, leave room for the truncator glyph(s) at
2613 the right margin. Otherwise, leave room for the continuation
2614 glyph(s). Truncation and continuation glyphs are not inserted
2615 for window-based redisplay. */
2616 if (!FRAME_WINDOW_P (it->f))
2617 {
2618 if (it->truncate_lines_p)
2619 it->last_visible_x -= it->truncation_pixel_width;
2620 else
2621 it->last_visible_x -= it->continuation_pixel_width;
2622 }
2623
045dee35 2624 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
da8b7f4f 2625 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
5f5c8ee5
GM
2626 }
2627
2628 /* Leave room for a border glyph. */
2629 if (!FRAME_WINDOW_P (it->f)
2630 && !WINDOW_RIGHTMOST_P (it->w))
2631 it->last_visible_x -= 1;
2632
2633 it->last_visible_y = window_text_bottom_y (w);
2634
2635 /* For mode lines and alike, arrange for the first glyph having a
2636 left box line if the face specifies a box. */
2637 if (base_face_id != DEFAULT_FACE_ID)
2638 {
2639 struct face *face;
2311178e 2640
5f5c8ee5
GM
2641 it->face_id = base_face_id;
2642
2643 /* If we have a boxed mode line, make the first character appear
2644 with a left box line. */
2645 face = FACE_FROM_ID (it->f, base_face_id);
2646 if (face->box != FACE_NO_BOX)
2647 it->start_of_box_run_p = 1;
2648 }
2649
2650 /* If a buffer position was specified, set the iterator there,
2651 getting overlays and face properties from that position. */
3ebf0ea9 2652 if (charpos >= BUF_BEG (current_buffer))
5f5c8ee5
GM
2653 {
2654 it->end_charpos = ZV;
2655 it->face_id = -1;
2656 IT_CHARPOS (*it) = charpos;
2311178e 2657
5f5c8ee5 2658 /* Compute byte position if not specified. */
3ebf0ea9 2659 if (bytepos < charpos)
5f5c8ee5
GM
2660 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2661 else
2662 IT_BYTEPOS (*it) = bytepos;
2663
29b3ea63
KS
2664 it->start = it->current;
2665
5f5c8ee5
GM
2666 /* Compute faces etc. */
2667 reseat (it, it->current.pos, 1);
2668 }
2311178e 2669
5f5c8ee5
GM
2670 CHECK_IT (it);
2671}
2672
2673
2674/* Initialize IT for the display of window W with window start POS. */
2675
2676void
2677start_display (it, w, pos)
2678 struct it *it;
2679 struct window *w;
2680 struct text_pos pos;
2681{
5f5c8ee5 2682 struct glyph_row *row;
045dee35 2683 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
5f5c8ee5
GM
2684
2685 row = w->desired_matrix->rows + first_vpos;
2686 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3ccb8bcf 2687 it->first_vpos = first_vpos;
17fdcfc8 2688
e0e9a80e
KS
2689 /* Don't reseat to previous visible line start if current start
2690 position is in a string or image. */
2691 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
5f5c8ee5 2692 {
17fdcfc8
GM
2693 int start_at_line_beg_p;
2694 int first_y = it->current_y;
2311178e 2695
17fdcfc8
GM
2696 /* If window start is not at a line start, skip forward to POS to
2697 get the correct continuation lines width. */
2698 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2699 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2700 if (!start_at_line_beg_p)
5f5c8ee5 2701 {
0e47bbf7
RS
2702 int new_x;
2703
17fdcfc8
GM
2704 reseat_at_previous_visible_line_start (it);
2705 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2706
0e47bbf7
RS
2707 new_x = it->current_x + it->pixel_width;
2708
17fdcfc8
GM
2709 /* If lines are continued, this line may end in the middle
2710 of a multi-glyph character (e.g. a control character
2711 displayed as \003, or in the middle of an overlay
2712 string). In this case move_it_to above will not have
2713 taken us to the start of the continuation line but to the
2714 end of the continued line. */
0e47bbf7
RS
2715 if (it->current_x > 0
2716 && !it->truncate_lines_p /* Lines are continued. */
2717 && (/* And glyph doesn't fit on the line. */
2718 new_x > it->last_visible_x
2719 /* Or it fits exactly and we're on a window
2720 system frame. */
2721 || (new_x == it->last_visible_x
2722 && FRAME_WINDOW_P (it->f))))
5f5c8ee5 2723 {
b28cb6ed
GM
2724 if (it->current.dpvec_index >= 0
2725 || it->current.overlay_string_index >= 0)
2726 {
cafafe0b 2727 set_iterator_to_next (it, 1);
b28cb6ed
GM
2728 move_it_in_display_line_to (it, -1, -1, 0);
2729 }
2311178e 2730
b28cb6ed 2731 it->continuation_lines_width += it->current_x;
5f5c8ee5 2732 }
b28cb6ed
GM
2733
2734 /* We're starting a new display line, not affected by the
2735 height of the continued line, so clear the appropriate
2736 fields in the iterator structure. */
2737 it->max_ascent = it->max_descent = 0;
2738 it->max_phys_ascent = it->max_phys_descent = 0;
2311178e 2739
17fdcfc8
GM
2740 it->current_y = first_y;
2741 it->vpos = 0;
2742 it->current_x = it->hpos = 0;
2743 }
5f5c8ee5
GM
2744 }
2745
2746#if 0 /* Don't assert the following because start_display is sometimes
2747 called intentionally with a window start that is not at a
2748 line start. Please leave this code in as a comment. */
2311178e 2749
5f5c8ee5
GM
2750 /* Window start should be on a line start, now. */
2751 xassert (it->continuation_lines_width
2752 || IT_CHARPOS (it) == BEGV
2753 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2754#endif /* 0 */
2755}
2756
2757
4bde0ebb
GM
2758/* Return 1 if POS is a position in ellipses displayed for invisible
2759 text. W is the window we display, for text property lookup. */
5f5c8ee5 2760
4bde0ebb
GM
2761static int
2762in_ellipses_for_invisible_text_p (pos, w)
5f5c8ee5 2763 struct display_pos *pos;
4bde0ebb 2764 struct window *w;
5f5c8ee5 2765{
ac90c44f 2766 Lisp_Object prop, window;
4bde0ebb
GM
2767 int ellipses_p = 0;
2768 int charpos = CHARPOS (pos->pos);
2311178e 2769
ac90c44f
GM
2770 /* If POS specifies a position in a display vector, this might
2771 be for an ellipsis displayed for invisible text. We won't
2772 get the iterator set up for delivering that ellipsis unless
2773 we make sure that it gets aware of the invisible text. */
2774 if (pos->dpvec_index >= 0
2775 && pos->overlay_string_index < 0
2776 && CHARPOS (pos->string_pos) < 0
2777 && charpos > BEGV
2778 && (XSETWINDOW (window, w),
2779 prop = Fget_char_property (make_number (charpos),
2780 Qinvisible, window),
20fbd925 2781 !TEXT_PROP_MEANS_INVISIBLE (prop)))
ac90c44f
GM
2782 {
2783 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2784 window);
8580a4e3 2785 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
4bde0ebb
GM
2786 }
2787
2788 return ellipses_p;
2789}
2790
2791
2792/* Initialize IT for stepping through current_buffer in window W,
2793 starting at position POS that includes overlay string and display
47d57b22
GM
2794 vector/ control character translation position information. Value
2795 is zero if there are overlay strings with newlines at POS. */
4bde0ebb 2796
47d57b22 2797static int
4bde0ebb
GM
2798init_from_display_pos (it, w, pos)
2799 struct it *it;
2800 struct window *w;
2801 struct display_pos *pos;
2802{
2803 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
47d57b22 2804 int i, overlay_strings_with_newlines = 0;
2311178e 2805
4bde0ebb
GM
2806 /* If POS specifies a position in a display vector, this might
2807 be for an ellipsis displayed for invisible text. We won't
2808 get the iterator set up for delivering that ellipsis unless
2809 we make sure that it gets aware of the invisible text. */
2810 if (in_ellipses_for_invisible_text_p (pos, w))
2811 {
2812 --charpos;
2813 bytepos = 0;
ac90c44f 2814 }
2311178e 2815
5f5c8ee5
GM
2816 /* Keep in mind: the call to reseat in init_iterator skips invisible
2817 text, so we might end up at a position different from POS. This
2818 is only a problem when POS is a row start after a newline and an
2819 overlay starts there with an after-string, and the overlay has an
2820 invisible property. Since we don't skip invisible text in
2821 display_line and elsewhere immediately after consuming the
2822 newline before the row start, such a POS will not be in a string,
2823 but the call to init_iterator below will move us to the
2824 after-string. */
ac90c44f 2825 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
5f5c8ee5 2826
fcd9b9d0
KS
2827 /* This only scans the current chunk -- it should scan all chunks.
2828 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2829 to 16 in 22.1 to make this a lesser problem. */
2830 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
1e1e5daf 2831 {
50f80c2f
KR
2832 const char *s = SDATA (it->overlay_strings[i]);
2833 const char *e = s + SBYTES (it->overlay_strings[i]);
2311178e 2834
1e1e5daf
GM
2835 while (s < e && *s != '\n')
2836 ++s;
2837
2838 if (s < e)
2839 {
2840 overlay_strings_with_newlines = 1;
2841 break;
2842 }
2843 }
47d57b22 2844
75c5350a
GM
2845 /* If position is within an overlay string, set up IT to the right
2846 overlay string. */
5f5c8ee5
GM
2847 if (pos->overlay_string_index >= 0)
2848 {
2849 int relative_index;
75c5350a
GM
2850
2851 /* If the first overlay string happens to have a `display'
2852 property for an image, the iterator will be set up for that
2853 image, and we have to undo that setup first before we can
2854 correct the overlay string index. */
a6f82cc4 2855 if (it->method == GET_FROM_IMAGE)
75c5350a 2856 pop_it (it);
2311178e 2857
5f5c8ee5
GM
2858 /* We already have the first chunk of overlay strings in
2859 IT->overlay_strings. Load more until the one for
2860 pos->overlay_string_index is in IT->overlay_strings. */
2861 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2862 {
2863 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2864 it->current.overlay_string_index = 0;
2865 while (n--)
2866 {
5a08cbaf 2867 load_overlay_strings (it, 0);
5f5c8ee5
GM
2868 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2869 }
2870 }
2311178e 2871
5f5c8ee5
GM
2872 it->current.overlay_string_index = pos->overlay_string_index;
2873 relative_index = (it->current.overlay_string_index
2874 % OVERLAY_STRING_CHUNK_SIZE);
2875 it->string = it->overlay_strings[relative_index];
cafafe0b 2876 xassert (STRINGP (it->string));
5f5c8ee5 2877 it->current.string_pos = pos->string_pos;
a6f82cc4 2878 it->method = GET_FROM_STRING;
5f5c8ee5 2879 }
2311178e 2880
1e1e5daf
GM
2881#if 0 /* This is bogus because POS not having an overlay string
2882 position does not mean it's after the string. Example: A
2883 line starting with a before-string and initialization of IT
2884 to the previous row's end position. */
2be8f184
GM
2885 else if (it->current.overlay_string_index >= 0)
2886 {
2887 /* If POS says we're already after an overlay string ending at
2888 POS, make sure to pop the iterator because it will be in
2889 front of that overlay string. When POS is ZV, we've thereby
2890 also ``processed'' overlay strings at ZV. */
aeb2b8fc
GM
2891 while (it->sp)
2892 pop_it (it);
2be8f184 2893 it->current.overlay_string_index = -1;
a6f82cc4 2894 it->method = GET_FROM_BUFFER;
2be8f184
GM
2895 if (CHARPOS (pos->pos) == ZV)
2896 it->overlay_strings_at_end_processed_p = 1;
2897 }
1e1e5daf 2898#endif /* 0 */
2311178e 2899
2be8f184 2900 if (CHARPOS (pos->string_pos) >= 0)
5f5c8ee5
GM
2901 {
2902 /* Recorded position is not in an overlay string, but in another
2903 string. This can only be a string from a `display' property.
2904 IT should already be filled with that string. */
2905 it->current.string_pos = pos->string_pos;
2906 xassert (STRINGP (it->string));
2907 }
2908
ac90c44f
GM
2909 /* Restore position in display vector translations, control
2910 character translations or ellipses. */
5f5c8ee5
GM
2911 if (pos->dpvec_index >= 0)
2912 {
ac90c44f
GM
2913 if (it->dpvec == NULL)
2914 get_next_display_element (it);
5f5c8ee5
GM
2915 xassert (it->dpvec && it->current.dpvec_index == 0);
2916 it->current.dpvec_index = pos->dpvec_index;
2917 }
2311178e 2918
5f5c8ee5 2919 CHECK_IT (it);
47d57b22 2920 return !overlay_strings_with_newlines;
5f5c8ee5
GM
2921}
2922
2923
2924/* Initialize IT for stepping through current_buffer in window W
2925 starting at ROW->start. */
2926
2927static void
2928init_to_row_start (it, w, row)
2929 struct it *it;
2930 struct window *w;
2931 struct glyph_row *row;
2932{
2933 init_from_display_pos (it, w, &row->start);
29b3ea63 2934 it->start = row->start;
5f5c8ee5
GM
2935 it->continuation_lines_width = row->continuation_lines_width;
2936 CHECK_IT (it);
2937}
2938
2311178e 2939
5f5c8ee5 2940/* Initialize IT for stepping through current_buffer in window W
47d57b22
GM
2941 starting in the line following ROW, i.e. starting at ROW->end.
2942 Value is zero if there are overlay strings with newlines at ROW's
2943 end position. */
5f5c8ee5 2944
47d57b22 2945static int
5f5c8ee5
GM
2946init_to_row_end (it, w, row)
2947 struct it *it;
2948 struct window *w;
2949 struct glyph_row *row;
2950{
47d57b22 2951 int success = 0;
2311178e 2952
47d57b22
GM
2953 if (init_from_display_pos (it, w, &row->end))
2954 {
2955 if (row->continued_p)
2956 it->continuation_lines_width
2957 = row->continuation_lines_width + row->pixel_width;
2958 CHECK_IT (it);
2959 success = 1;
2960 }
2311178e 2961
47d57b22 2962 return success;
5f5c8ee5
GM
2963}
2964
2965
2966
2967\f
2968/***********************************************************************
2969 Text properties
2970 ***********************************************************************/
2971
2972/* Called when IT reaches IT->stop_charpos. Handle text property and
2973 overlay changes. Set IT->stop_charpos to the next position where
2974 to stop. */
2975
2976static void
2977handle_stop (it)
2978 struct it *it;
2979{
2980 enum prop_handled handled;
310316cf 2981 int handle_overlay_change_p;
5f5c8ee5
GM
2982 struct props *p;
2983
2984 it->dpvec = NULL;
2985 it->current.dpvec_index = -1;
310316cf
KS
2986 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
2987 it->ignore_overlay_strings_at_pos_p = 0;
5f5c8ee5 2988
62af9c24
KS
2989 /* Use face of preceding text for ellipsis (if invisible) */
2990 if (it->selective_display_ellipsis_p)
2991 it->saved_face_id = it->face_id;
2992
5f5c8ee5
GM
2993 do
2994 {
2995 handled = HANDLED_NORMALLY;
2311178e 2996
5f5c8ee5
GM
2997 /* Call text property handlers. */
2998 for (p = it_props; p->handler; ++p)
2999 {
3000 handled = p->handler (it);
2970b9be 3001
5f5c8ee5
GM
3002 if (handled == HANDLED_RECOMPUTE_PROPS)
3003 break;
3004 else if (handled == HANDLED_RETURN)
3005 return;
3006 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3007 handle_overlay_change_p = 0;
3008 }
3009
3010 if (handled != HANDLED_RECOMPUTE_PROPS)
3011 {
3012 /* Don't check for overlay strings below when set to deliver
3013 characters from a display vector. */
a6f82cc4 3014 if (it->method == GET_FROM_DISPLAY_VECTOR)
5f5c8ee5
GM
3015 handle_overlay_change_p = 0;
3016
3017 /* Handle overlay changes. */
3018 if (handle_overlay_change_p)
3019 handled = handle_overlay_change (it);
2311178e 3020
5f5c8ee5
GM
3021 /* Determine where to stop next. */
3022 if (handled == HANDLED_NORMALLY)
3023 compute_stop_pos (it);
3024 }
3025 }
3026 while (handled == HANDLED_RECOMPUTE_PROPS);
3027}
3028
3029
3030/* Compute IT->stop_charpos from text property and overlay change
3031 information for IT's current position. */
3032
3033static void
3034compute_stop_pos (it)
3035 struct it *it;
3036{
3037 register INTERVAL iv, next_iv;
3038 Lisp_Object object, limit, position;
3039
3040 /* If nowhere else, stop at the end. */
3041 it->stop_charpos = it->end_charpos;
2311178e 3042
5f5c8ee5
GM
3043 if (STRINGP (it->string))
3044 {
3045 /* Strings are usually short, so don't limit the search for
3046 properties. */
3047 object = it->string;
3048 limit = Qnil;
ac90c44f 3049 position = make_number (IT_STRING_CHARPOS (*it));
5f5c8ee5
GM
3050 }
3051 else
3052 {
3053 int charpos;
3054
3055 /* If next overlay change is in front of the current stop pos
3056 (which is IT->end_charpos), stop there. Note: value of
3057 next_overlay_change is point-max if no overlay change
3058 follows. */
3059 charpos = next_overlay_change (IT_CHARPOS (*it));
3060 if (charpos < it->stop_charpos)
3061 it->stop_charpos = charpos;
3062
3063 /* If showing the region, we have to stop at the region
3064 start or end because the face might change there. */
3065 if (it->region_beg_charpos > 0)
3066 {
3067 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3068 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3069 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3070 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3071 }
2311178e 3072
5f5c8ee5
GM
3073 /* Set up variables for computing the stop position from text
3074 property changes. */
3075 XSETBUFFER (object, current_buffer);
ac90c44f
GM
3076 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3077 position = make_number (IT_CHARPOS (*it));
5f5c8ee5
GM
3078
3079 }
3080
3081 /* Get the interval containing IT's position. Value is a null
3082 interval if there isn't such an interval. */
3083 iv = validate_interval_range (object, &position, &position, 0);
3084 if (!NULL_INTERVAL_P (iv))
3085 {
3086 Lisp_Object values_here[LAST_PROP_IDX];
3087 struct props *p;
3088
3089 /* Get properties here. */
3090 for (p = it_props; p->handler; ++p)
3091 values_here[p->idx] = textget (iv->plist, *p->name);
3092
3093 /* Look for an interval following iv that has different
3094 properties. */
3095 for (next_iv = next_interval (iv);
3096 (!NULL_INTERVAL_P (next_iv)
3097 && (NILP (limit)
3098 || XFASTINT (limit) > next_iv->position));
3099 next_iv = next_interval (next_iv))
3100 {
3101 for (p = it_props; p->handler; ++p)
3102 {
3103 Lisp_Object new_value;
3104
3105 new_value = textget (next_iv->plist, *p->name);
3106 if (!EQ (values_here[p->idx], new_value))
3107 break;
3108 }
2311178e 3109
5f5c8ee5
GM
3110 if (p->handler)
3111 break;
3112 }
3113
3114 if (!NULL_INTERVAL_P (next_iv))
3115 {
3116 if (INTEGERP (limit)
3117 && next_iv->position >= XFASTINT (limit))
3118 /* No text property change up to limit. */
3119 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3120 else
3121 /* Text properties change in next_iv. */
3122 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3123 }
3124 }
3125
3126 xassert (STRINGP (it->string)
3127 || (it->stop_charpos >= BEGV
3128 && it->stop_charpos >= IT_CHARPOS (*it)));
3129}
3130
3131
3132/* Return the position of the next overlay change after POS in
3133 current_buffer. Value is point-max if no overlay change
3134 follows. This is like `next-overlay-change' but doesn't use
3135 xmalloc. */
3136
3137static int
3138next_overlay_change (pos)
3139 int pos;
3140{
3141 int noverlays;
3142 int endpos;
3143 Lisp_Object *overlays;
5f5c8ee5
GM
3144 int i;
3145
3146 /* Get all overlays at the given position. */
7994a991 3147 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
5f5c8ee5
GM
3148
3149 /* If any of these overlays ends before endpos,
3150 use its ending point instead. */
3151 for (i = 0; i < noverlays; ++i)
3152 {
3153 Lisp_Object oend;
3154 int oendpos;
3155
3156 oend = OVERLAY_END (overlays[i]);
3157 oendpos = OVERLAY_POSITION (oend);
3158 endpos = min (endpos, oendpos);
3159 }
3160
3161 return endpos;
3162}
3163
3164
3165\f
3166/***********************************************************************
3167 Fontification
3168 ***********************************************************************/
3169
3170/* Handle changes in the `fontified' property of the current buffer by
3171 calling hook functions from Qfontification_functions to fontify
3172 regions of text. */
3173
3174static enum prop_handled
3175handle_fontified_prop (it)
3176 struct it *it;
3177{
3178 Lisp_Object prop, pos;
3179 enum prop_handled handled = HANDLED_NORMALLY;
3180
f5b8d5f2
RS
3181 if (!NILP (Vmemory_full))
3182 return handled;
3183
5f5c8ee5
GM
3184 /* Get the value of the `fontified' property at IT's current buffer
3185 position. (The `fontified' property doesn't have a special
3186 meaning in strings.) If the value is nil, call functions from
3187 Qfontification_functions. */
3188 if (!STRINGP (it->string)
3189 && it->s == NULL
3190 && !NILP (Vfontification_functions)
085536c2 3191 && !NILP (Vrun_hooks)
5f5c8ee5
GM
3192 && (pos = make_number (IT_CHARPOS (*it)),
3193 prop = Fget_char_property (pos, Qfontified, Qnil),
3194 NILP (prop)))
3195 {
331379bf 3196 int count = SPECPDL_INDEX ();
085536c2
GM
3197 Lisp_Object val;
3198
3199 val = Vfontification_functions;
3200 specbind (Qfontification_functions, Qnil);
2311178e 3201
085536c2 3202 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
116d6f5c 3203 safe_call1 (val, pos);
085536c2
GM
3204 else
3205 {
3206 Lisp_Object globals, fn;
3207 struct gcpro gcpro1, gcpro2;
5f5c8ee5 3208
085536c2
GM
3209 globals = Qnil;
3210 GCPRO2 (val, globals);
2311178e 3211
085536c2
GM
3212 for (; CONSP (val); val = XCDR (val))
3213 {
3214 fn = XCAR (val);
2311178e 3215
085536c2
GM
3216 if (EQ (fn, Qt))
3217 {
3218 /* A value of t indicates this hook has a local
3219 binding; it means to run the global binding too.
3220 In a global value, t should not occur. If it
3221 does, we must ignore it to avoid an endless
3222 loop. */
3223 for (globals = Fdefault_value (Qfontification_functions);
3224 CONSP (globals);
3225 globals = XCDR (globals))
3226 {
3227 fn = XCAR (globals);
3228 if (!EQ (fn, Qt))
116d6f5c 3229 safe_call1 (fn, pos);
085536c2
GM
3230 }
3231 }
3232 else
116d6f5c 3233 safe_call1 (fn, pos);
085536c2
GM
3234 }
3235
3236 UNGCPRO;
3237 }
3238
3239 unbind_to (count, Qnil);
5f5c8ee5
GM
3240
3241 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3242 something. This avoids an endless loop if they failed to
3243 fontify the text for which reason ever. */
3244 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3245 handled = HANDLED_RECOMPUTE_PROPS;
3246 }
3247
3248 return handled;
3249}
3250
3251
3252\f
3253/***********************************************************************
3254 Faces
3255 ***********************************************************************/
3256
3257/* Set up iterator IT from face properties at its current position.
3258 Called from handle_stop. */
3259
3260static enum prop_handled
3261handle_face_prop (it)
3262 struct it *it;
3263{
3264 int new_face_id, next_stop;
2311178e 3265
5f5c8ee5
GM
3266 if (!STRINGP (it->string))
3267 {
3268 new_face_id
3269 = face_at_buffer_position (it->w,
3270 IT_CHARPOS (*it),
3271 it->region_beg_charpos,
3272 it->region_end_charpos,
3273 &next_stop,
3274 (IT_CHARPOS (*it)
3275 + TEXT_PROP_DISTANCE_LIMIT),
3276 0);
2311178e 3277
5f5c8ee5
GM
3278 /* Is this a start of a run of characters with box face?
3279 Caveat: this can be called for a freshly initialized
4b41cebb 3280 iterator; face_id is -1 in this case. We know that the new
5f5c8ee5
GM
3281 face will not change until limit, i.e. if the new face has a
3282 box, all characters up to limit will have one. But, as
3283 usual, we don't know whether limit is really the end. */
3284 if (new_face_id != it->face_id)
3285 {
3286 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2311178e 3287
5f5c8ee5
GM
3288 /* If new face has a box but old face has not, this is
3289 the start of a run of characters with box, i.e. it has
3290 a shadow on the left side. The value of face_id of the
3291 iterator will be -1 if this is the initial call that gets
3292 the face. In this case, we have to look in front of IT's
3293 position and see whether there is a face != new_face_id. */
3294 it->start_of_box_run_p
3295 = (new_face->box != FACE_NO_BOX
3296 && (it->face_id >= 0
3297 || IT_CHARPOS (*it) == BEG
3298 || new_face_id != face_before_it_pos (it)));
3299 it->face_box_p = new_face->box != FACE_NO_BOX;
3300 }
3301 }
3302 else
3303 {
06a12811
GM
3304 int base_face_id, bufpos;
3305
3306 if (it->current.overlay_string_index >= 0)
3307 bufpos = IT_CHARPOS (*it);
3308 else
3309 bufpos = 0;
2311178e 3310
06a12811
GM
3311 /* For strings from a buffer, i.e. overlay strings or strings
3312 from a `display' property, use the face at IT's current
3313 buffer position as the base face to merge with, so that
3314 overlay strings appear in the same face as surrounding
3315 text, unless they specify their own faces. */
3316 base_face_id = underlying_face_id (it);
2311178e 3317
06a12811
GM
3318 new_face_id = face_at_string_position (it->w,
3319 it->string,
3320 IT_STRING_CHARPOS (*it),
3321 bufpos,
3322 it->region_beg_charpos,
3323 it->region_end_charpos,
3324 &next_stop,
5de7c6f2 3325 base_face_id, 0);
2311178e 3326
5f5c8ee5
GM
3327#if 0 /* This shouldn't be neccessary. Let's check it. */
3328 /* If IT is used to display a mode line we would really like to
3329 use the mode line face instead of the frame's default face. */
3330 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3331 && new_face_id == DEFAULT_FACE_ID)
96d2320f 3332 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
5f5c8ee5 3333#endif
2311178e 3334
5f5c8ee5
GM
3335 /* Is this a start of a run of characters with box? Caveat:
3336 this can be called for a freshly allocated iterator; face_id
3337 is -1 is this case. We know that the new face will not
3338 change until the next check pos, i.e. if the new face has a
3339 box, all characters up to that position will have a
3340 box. But, as usual, we don't know whether that position
3341 is really the end. */
3342 if (new_face_id != it->face_id)
3343 {
3344 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3345 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2311178e 3346
5f5c8ee5
GM
3347 /* If new face has a box but old face hasn't, this is the
3348 start of a run of characters with box, i.e. it has a
3349 shadow on the left side. */
3350 it->start_of_box_run_p
3351 = new_face->box && (old_face == NULL || !old_face->box);
3352 it->face_box_p = new_face->box != FACE_NO_BOX;
3353 }
3354 }
2311178e 3355
5f5c8ee5 3356 it->face_id = new_face_id;
5f5c8ee5
GM
3357 return HANDLED_NORMALLY;
3358}
3359
3360
06a12811
GM
3361/* Return the ID of the face ``underlying'' IT's current position,
3362 which is in a string. If the iterator is associated with a
3363 buffer, return the face at IT's current buffer position.
3364 Otherwise, use the iterator's base_face_id. */
3365
3366static int
3367underlying_face_id (it)
3368 struct it *it;
3369{
3370 int face_id = it->base_face_id, i;
3371
3372 xassert (STRINGP (it->string));
3373
3374 for (i = it->sp - 1; i >= 0; --i)
3375 if (NILP (it->stack[i].string))
3376 face_id = it->stack[i].face_id;
3377
3378 return face_id;
3379}
3380
3381
5f5c8ee5
GM
3382/* Compute the face one character before or after the current position
3383 of IT. BEFORE_P non-zero means get the face in front of IT's
3384 position. Value is the id of the face. */
3385
3386static int
3387face_before_or_after_it_pos (it, before_p)
3388 struct it *it;
3389 int before_p;
3390{
3391 int face_id, limit;
3392 int next_check_charpos;
3393 struct text_pos pos;
3394
3395 xassert (it->s == NULL);
2311178e 3396
5f5c8ee5
GM
3397 if (STRINGP (it->string))
3398 {
06a12811 3399 int bufpos, base_face_id;
2311178e 3400
5f5c8ee5
GM
3401 /* No face change past the end of the string (for the case
3402 we are padding with spaces). No face change before the
3403 string start. */
2051c264 3404 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
5f5c8ee5
GM
3405 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3406 return it->face_id;
3407
3408 /* Set pos to the position before or after IT's current position. */
3409 if (before_p)
3410 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3411 else
260a86a0
KH
3412 /* For composition, we must check the character after the
3413 composition. */
3414 pos = (it->what == IT_COMPOSITION
3415 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3416 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
5f5c8ee5 3417
06a12811
GM
3418 if (it->current.overlay_string_index >= 0)
3419 bufpos = IT_CHARPOS (*it);
3420 else
3421 bufpos = 0;
3422
3423 base_face_id = underlying_face_id (it);
3424
5f5c8ee5 3425 /* Get the face for ASCII, or unibyte. */
06a12811
GM
3426 face_id = face_at_string_position (it->w,
3427 it->string,
3428 CHARPOS (pos),
3429 bufpos,
3430 it->region_beg_charpos,
3431 it->region_end_charpos,
3432 &next_check_charpos,
5de7c6f2 3433 base_face_id, 0);
5f5c8ee5
GM
3434
3435 /* Correct the face for charsets different from ASCII. Do it
3436 for the multibyte case only. The face returned above is
3437 suitable for unibyte text if IT->string is unibyte. */
3438 if (STRING_MULTIBYTE (it->string))
3439 {
50f80c2f 3440 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
2051c264 3441 int rest = SBYTES (it->string) - BYTEPOS (pos);
980806b6
KH
3442 int c, len;
3443 struct face *face = FACE_FROM_ID (it->f, face_id);
2311178e 3444
4fdb80f2 3445 c = string_char_and_length (p, rest, &len);
980806b6 3446 face_id = FACE_FOR_CHAR (it->f, face, c);
5f5c8ee5
GM
3447 }
3448 }
3449 else
3450 {
70851746
GM
3451 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3452 || (IT_CHARPOS (*it) <= BEGV && before_p))
3453 return it->face_id;
2311178e 3454
5f5c8ee5
GM
3455 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3456 pos = it->current.pos;
2311178e 3457
5f5c8ee5 3458 if (before_p)
c1005d06 3459 DEC_TEXT_POS (pos, it->multibyte_p);
5f5c8ee5 3460 else
260a86a0
KH
3461 {
3462 if (it->what == IT_COMPOSITION)
3463 /* For composition, we must check the position after the
3464 composition. */
3465 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3466 else
c1005d06 3467 INC_TEXT_POS (pos, it->multibyte_p);
260a86a0 3468 }
2311178e 3469
5f5c8ee5
GM
3470 /* Determine face for CHARSET_ASCII, or unibyte. */
3471 face_id = face_at_buffer_position (it->w,
3472 CHARPOS (pos),
3473 it->region_beg_charpos,
3474 it->region_end_charpos,
3475 &next_check_charpos,
3476 limit, 0);
3477
3478 /* Correct the face for charsets different from ASCII. Do it
3479 for the multibyte case only. The face returned above is
3480 suitable for unibyte text if current_buffer is unibyte. */
3481 if (it->multibyte_p)
3482 {
1d1b6e6a 3483 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
980806b6
KH
3484 struct face *face = FACE_FROM_ID (it->f, face_id);
3485 face_id = FACE_FOR_CHAR (it->f, face, c);
5f5c8ee5
GM
3486 }
3487 }
2311178e 3488
5f5c8ee5
GM
3489 return face_id;
3490}
3491
3492
3493\f
3494/***********************************************************************
3495 Invisible text
3496 ***********************************************************************/
3497
3498/* Set up iterator IT from invisible properties at its current
3499 position. Called from handle_stop. */
3500
3501static enum prop_handled
3502handle_invisible_prop (it)
3503 struct it *it;
3504{
3505 enum prop_handled handled = HANDLED_NORMALLY;
3506
3507 if (STRINGP (it->string))
3508 {
3509 extern Lisp_Object Qinvisible;
3510 Lisp_Object prop, end_charpos, limit, charpos;
3511
3512 /* Get the value of the invisible text property at the
3513 current position. Value will be nil if there is no such
3514 property. */
ac90c44f 3515 charpos = make_number (IT_STRING_CHARPOS (*it));
5f5c8ee5
GM
3516 prop = Fget_text_property (charpos, Qinvisible, it->string);
3517
eadc0bf8
GM
3518 if (!NILP (prop)
3519 && IT_STRING_CHARPOS (*it) < it->end_charpos)
5f5c8ee5
GM
3520 {
3521 handled = HANDLED_RECOMPUTE_PROPS;
2311178e 3522
5f5c8ee5
GM
3523 /* Get the position at which the next change of the
3524 invisible text property can be found in IT->string.
3525 Value will be nil if the property value is the same for
3526 all the rest of IT->string. */
2051c264 3527 XSETINT (limit, SCHARS (it->string));
5f5c8ee5 3528 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2051c264 3529 it->string, limit);
2311178e 3530
5f5c8ee5
GM
3531 /* Text at current position is invisible. The next
3532 change in the property is at position end_charpos.
3533 Move IT's current position to that position. */
3534 if (INTEGERP (end_charpos)
3535 && XFASTINT (end_charpos) < XFASTINT (limit))
3536 {
3537 struct text_pos old;
3538 old = it->current.string_pos;
3539 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3540 compute_string_pos (&it->current.string_pos, old, it->string);
3541 }
3542 else
3543 {
3544 /* The rest of the string is invisible. If this is an
3545 overlay string, proceed with the next overlay string
3546 or whatever comes and return a character from there. */
3547 if (it->current.overlay_string_index >= 0)
3548 {
3549 next_overlay_string (it);
3550 /* Don't check for overlay strings when we just
3551 finished processing them. */
3552 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3553 }
3554 else
3555 {
2051c264
GM
3556 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3557 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
5f5c8ee5
GM
3558 }
3559 }
3560 }
3561 }
3562 else
3563 {
8580a4e3 3564 int invis_p, newpos, next_stop, start_charpos;
5a08cbaf 3565 Lisp_Object pos, prop, overlay;
5f5c8ee5
GM
3566
3567 /* First of all, is there invisible text at this position? */
5a08cbaf 3568 start_charpos = IT_CHARPOS (*it);
ac90c44f 3569 pos = make_number (IT_CHARPOS (*it));
5a08cbaf
GM
3570 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3571 &overlay);
8580a4e3
SM
3572 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3573
5f5c8ee5 3574 /* If we are on invisible text, skip over it. */
8580a4e3 3575 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
5f5c8ee5
GM
3576 {
3577 /* Record whether we have to display an ellipsis for the
3578 invisible text. */
8580a4e3 3579 int display_ellipsis_p = invis_p == 2;
5f5c8ee5
GM
3580
3581 handled = HANDLED_RECOMPUTE_PROPS;
2311178e 3582
5f5c8ee5
GM
3583 /* Loop skipping over invisible text. The loop is left at
3584 ZV or with IT on the first char being visible again. */
3585 do
3586 {
3587 /* Try to skip some invisible text. Return value is the
3588 position reached which can be equal to IT's position
3589 if there is nothing invisible here. This skips both
3590 over invisible text properties and overlays with
3591 invisible property. */
3592 newpos = skip_invisible (IT_CHARPOS (*it),
3593 &next_stop, ZV, it->window);
3594
3595 /* If we skipped nothing at all we weren't at invisible
3596 text in the first place. If everything to the end of
3597 the buffer was skipped, end the loop. */
3598 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
8580a4e3 3599 invis_p = 0;
5f5c8ee5
GM
3600 else
3601 {
3602 /* We skipped some characters but not necessarily
3603 all there are. Check if we ended up on visible
3604 text. Fget_char_property returns the property of
3605 the char before the given position, i.e. if we
8580a4e3 3606 get invis_p = 0, this means that the char at
5f5c8ee5 3607 newpos is visible. */
ac90c44f 3608 pos = make_number (newpos);
5f5c8ee5 3609 prop = Fget_char_property (pos, Qinvisible, it->window);
8580a4e3 3610 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
5f5c8ee5 3611 }
2311178e 3612
5f5c8ee5
GM
3613 /* If we ended up on invisible text, proceed to
3614 skip starting with next_stop. */
8580a4e3 3615 if (invis_p)
5f5c8ee5
GM
3616 IT_CHARPOS (*it) = next_stop;
3617 }
8580a4e3 3618 while (invis_p);
5a08cbaf 3619
5f5c8ee5
GM
3620 /* The position newpos is now either ZV or on visible text. */
3621 IT_CHARPOS (*it) = newpos;
3622 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2311178e 3623
5a08cbaf
GM
3624 /* If there are before-strings at the start of invisible
3625 text, and the text is invisible because of a text
3626 property, arrange to show before-strings because 20.x did
3627 it that way. (If the text is invisible because of an
3628 overlay property instead of a text property, this is
3629 already handled in the overlay code.) */
3630 if (NILP (overlay)
3631 && get_overlay_strings (it, start_charpos))
5f5c8ee5 3632 {
5a08cbaf
GM
3633 handled = HANDLED_RECOMPUTE_PROPS;
3634 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
5f5c8ee5 3635 }
5a08cbaf 3636 else if (display_ellipsis_p)
064fd8ec 3637 setup_for_ellipsis (it, 0);
5f5c8ee5
GM
3638 }
3639 }
3640
3641 return handled;
3642}
3643
3644
064fd8ec
KS
3645/* Make iterator IT return `...' next.
3646 Replaces LEN characters from buffer. */
5a08cbaf
GM
3647
3648static void
064fd8ec 3649setup_for_ellipsis (it, len)
5a08cbaf 3650 struct it *it;
064fd8ec 3651 int len;
5a08cbaf 3652{
064fd8ec
KS
3653 /* Use the display table definition for `...'. Invalid glyphs
3654 will be handled by the method returning elements from dpvec. */
3655 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
5a08cbaf
GM
3656 {
3657 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3658 it->dpvec = v->contents;
3659 it->dpend = v->contents + v->size;
3660 }
2311178e 3661 else
5a08cbaf
GM
3662 {
3663 /* Default `...'. */
3664 it->dpvec = default_invis_vector;
3665 it->dpend = default_invis_vector + 3;
3666 }
2311178e 3667
064fd8ec 3668 it->dpvec_char_len = len;
5a08cbaf 3669 it->current.dpvec_index = 0;
fe1c2006 3670 it->dpvec_face_id = -1;
064fd8ec
KS
3671
3672 /* Remember the current face id in case glyphs specify faces.
ffd80825
KS
3673 IT's face is restored in set_iterator_to_next.
3674 saved_face_id was set to preceding char's face in handle_stop. */
3675 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3676 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
62af9c24 3677
a6f82cc4 3678 it->method = GET_FROM_DISPLAY_VECTOR;
f323e507 3679 it->ellipsis_p = 1;
5a08cbaf
GM
3680}
3681
3682
5f5c8ee5
GM
3683\f
3684/***********************************************************************
3685 'display' property
3686 ***********************************************************************/
3687
3688/* Set up iterator IT from `display' property at its current position.
b2683be4
RS
3689 Called from handle_stop.
3690 We return HANDLED_RETURN if some part of the display property
3691 overrides the display of the buffer text itself.
3692 Otherwise we return HANDLED_NORMALLY. */
5f5c8ee5
GM
3693
3694static enum prop_handled
3695handle_display_prop (it)
3696 struct it *it;
3697{
3698 Lisp_Object prop, object;
3699 struct text_pos *position;
99c71e51 3700 /* Nonzero if some property replaces the display of the text itself. */
a61b7058 3701 int display_replaced_p = 0;
5f5c8ee5
GM
3702
3703 if (STRINGP (it->string))
3704 {
3705 object = it->string;
3706 position = &it->current.string_pos;
3707 }
3708 else
3709 {
d9eef135 3710 XSETWINDOW (object, it->w);
5f5c8ee5
GM
3711 position = &it->current.pos;
3712 }
3713
3714 /* Reset those iterator values set from display property values. */
e893970b 3715 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
5f5c8ee5 3716 it->space_width = Qnil;
e893970b 3717 it->font_height = Qnil;
5f5c8ee5
GM
3718 it->voffset = 0;
3719
3720 /* We don't support recursive `display' properties, i.e. string
3721 values that have a string `display' property, that have a string
3722 `display' property etc. */
3723 if (!it->string_from_display_prop_p)
3724 it->area = TEXT_AREA;
3725
3726 prop = Fget_char_property (make_number (position->charpos),
3727 Qdisplay, object);
3728 if (NILP (prop))
3729 return HANDLED_NORMALLY;
3730
d9eef135
KS
3731 if (!STRINGP (it->string))
3732 object = it->w->buffer;
3733
f3751a65 3734 if (CONSP (prop)
12700f40
GM
3735 /* Simple properties. */
3736 && !EQ (XCAR (prop), Qimage)
3737 && !EQ (XCAR (prop), Qspace)
3738 && !EQ (XCAR (prop), Qwhen)
e893970b 3739 && !EQ (XCAR (prop), Qslice)
12700f40
GM
3740 && !EQ (XCAR (prop), Qspace_width)
3741 && !EQ (XCAR (prop), Qheight)
3742 && !EQ (XCAR (prop), Qraise)
3743 /* Marginal area specifications. */
3744 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
6d925726
KS
3745 && !EQ (XCAR (prop), Qleft_fringe)
3746 && !EQ (XCAR (prop), Qright_fringe)
12700f40 3747 && !NILP (XCAR (prop)))
5f5c8ee5 3748 {
a61b7058 3749 for (; CONSP (prop); prop = XCDR (prop))
5f5c8ee5 3750 {
b2683be4 3751 if (handle_single_display_spec (it, XCAR (prop), object,
a61b7058
GM
3752 position, display_replaced_p))
3753 display_replaced_p = 1;
5f5c8ee5
GM
3754 }
3755 }
3756 else if (VECTORP (prop))
3757 {
3758 int i;
a61b7058 3759 for (i = 0; i < ASIZE (prop); ++i)
b2683be4 3760 if (handle_single_display_spec (it, AREF (prop, i), object,
a61b7058
GM
3761 position, display_replaced_p))
3762 display_replaced_p = 1;
5f5c8ee5
GM
3763 }
3764 else
3765 {
a605fefc
KS
3766 int ret = handle_single_display_spec (it, prop, object, position, 0);
3767 if (ret < 0) /* Replaced by "", i.e. nothing. */
3768 return HANDLED_RECOMPUTE_PROPS;
3769 if (ret)
a61b7058 3770 display_replaced_p = 1;
5f5c8ee5
GM
3771 }
3772
a61b7058 3773 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
5f5c8ee5
GM
3774}
3775
3776
6c577098 3777/* Value is the position of the end of the `display' property starting
5f5c8ee5
GM
3778 at START_POS in OBJECT. */
3779
3780static struct text_pos
3781display_prop_end (it, object, start_pos)
3782 struct it *it;
3783 Lisp_Object object;
3784 struct text_pos start_pos;
3785{
3786 Lisp_Object end;
3787 struct text_pos end_pos;
5f5c8ee5 3788
016b5642
MB
3789 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3790 Qdisplay, object, Qnil);
6c577098
GM
3791 CHARPOS (end_pos) = XFASTINT (end);
3792 if (STRINGP (object))
5f5c8ee5
GM
3793 compute_string_pos (&end_pos, start_pos, it->string);
3794 else
6c577098 3795 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
5f5c8ee5
GM
3796
3797 return end_pos;
3798}
3799
3800
b2683be4 3801/* Set up IT from a single `display' specification PROP. OBJECT
5f5c8ee5 3802 is the object in which the `display' property was found. *POSITION
a61b7058 3803 is the position at which it was found. DISPLAY_REPLACED_P non-zero
b2683be4 3804 means that we previously saw a display specification which already
a61b7058 3805 replaced text display with something else, for example an image;
b2683be4 3806 we ignore such properties after the first one has been processed.
5f5c8ee5 3807
b2683be4
RS
3808 If PROP is a `space' or `image' specification, and in some other
3809 cases too, set *POSITION to the position where the `display'
3810 property ends.
5f5c8ee5 3811
4b41cebb 3812 Value is non-zero if something was found which replaces the display
a605fefc
KS
3813 of buffer or string text. Specifically, the value is -1 if that
3814 "something" is "nothing". */
5f5c8ee5
GM
3815
3816static int
b2683be4 3817handle_single_display_spec (it, spec, object, position,
a61b7058 3818 display_replaced_before_p)
5f5c8ee5 3819 struct it *it;
b2683be4 3820 Lisp_Object spec;
5f5c8ee5
GM
3821 Lisp_Object object;
3822 struct text_pos *position;
a61b7058 3823 int display_replaced_before_p;
5f5c8ee5 3824{
5f5c8ee5 3825 Lisp_Object form;
b2683be4
RS
3826 Lisp_Object location, value;
3827 struct text_pos start_pos;
3828 int valid_p;
5f5c8ee5 3829
b2683be4
RS
3830 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3831 If the result is non-nil, use VALUE instead of SPEC. */
5f5c8ee5 3832 form = Qt;
b2683be4 3833 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
5f5c8ee5 3834 {
b2683be4
RS
3835 spec = XCDR (spec);
3836 if (!CONSP (spec))
5f5c8ee5 3837 return 0;
b2683be4
RS
3838 form = XCAR (spec);
3839 spec = XCDR (spec);
5f5c8ee5
GM
3840 }
3841
3842 if (!NILP (form) && !EQ (form, Qt))
3843 {
331379bf 3844 int count = SPECPDL_INDEX ();
5f5c8ee5 3845 struct gcpro gcpro1;
5f5c8ee5 3846
b384d6f8
GM
3847 /* Bind `object' to the object having the `display' property, a
3848 buffer or string. Bind `position' to the position in the
3849 object where the property was found, and `buffer-position'
3850 to the current position in the buffer. */
3851 specbind (Qobject, object);
31ac723b
SM
3852 specbind (Qposition, make_number (CHARPOS (*position)));
3853 specbind (Qbuffer_position,
3854 make_number (STRINGP (object)
3855 ? IT_CHARPOS (*it) : CHARPOS (*position)));
b384d6f8 3856 GCPRO1 (form);
116d6f5c 3857 form = safe_eval (form);
b384d6f8
GM
3858 UNGCPRO;
3859 unbind_to (count, Qnil);
5f5c8ee5 3860 }
2311178e 3861
5f5c8ee5
GM
3862 if (NILP (form))
3863 return 0;
3864
b2683be4
RS
3865 /* Handle `(height HEIGHT)' specifications. */
3866 if (CONSP (spec)
3867 && EQ (XCAR (spec), Qheight)
3868 && CONSP (XCDR (spec)))
5f5c8ee5 3869 {
e4093f38 3870 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
5f5c8ee5 3871 return 0;
99c71e51 3872
b2683be4 3873 it->font_height = XCAR (XCDR (spec));
5f5c8ee5
GM
3874 if (!NILP (it->font_height))
3875 {
3876 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3877 int new_height = -1;
3878
3879 if (CONSP (it->font_height)
3880 && (EQ (XCAR (it->font_height), Qplus)
3881 || EQ (XCAR (it->font_height), Qminus))
3882 && CONSP (XCDR (it->font_height))
3883 && INTEGERP (XCAR (XCDR (it->font_height))))
3884 {
3885 /* `(+ N)' or `(- N)' where N is an integer. */
3886 int steps = XINT (XCAR (XCDR (it->font_height)));
3887 if (EQ (XCAR (it->font_height), Qplus))
3888 steps = - steps;
3889 it->face_id = smaller_face (it->f, it->face_id, steps);
3890 }
0d8b31c0 3891 else if (FUNCTIONP (it->font_height))
5f5c8ee5
GM
3892 {
3893 /* Call function with current height as argument.
3894 Value is the new height. */
116d6f5c
GM
3895 Lisp_Object height;
3896 height = safe_call1 (it->font_height,
3897 face->lface[LFACE_HEIGHT_INDEX]);
5f5c8ee5
GM
3898 if (NUMBERP (height))
3899 new_height = XFLOATINT (height);
5f5c8ee5
GM
3900 }
3901 else if (NUMBERP (it->font_height))
3902 {
3903 /* Value is a multiple of the canonical char height. */
3904 struct face *face;
2311178e 3905
5f5c8ee5
GM
3906 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3907 new_height = (XFLOATINT (it->font_height)
3908 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3909 }
3910 else
3911 {
3912 /* Evaluate IT->font_height with `height' bound to the
3913 current specified height to get the new height. */
331379bf 3914 int count = SPECPDL_INDEX ();
2311178e 3915
5f5c8ee5 3916 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
116d6f5c 3917 value = safe_eval (it->font_height);
5f5c8ee5 3918 unbind_to (count, Qnil);
2311178e 3919
5f5c8ee5
GM
3920 if (NUMBERP (value))
3921 new_height = XFLOATINT (value);
3922 }
2311178e 3923
5f5c8ee5
GM
3924 if (new_height > 0)
3925 it->face_id = face_with_height (it->f, it->face_id, new_height);
3926 }
b2683be4
RS
3927
3928 return 0;
5f5c8ee5 3929 }
b2683be4
RS
3930
3931 /* Handle `(space_width WIDTH)'. */
3932 if (CONSP (spec)
3933 && EQ (XCAR (spec), Qspace_width)
3934 && CONSP (XCDR (spec)))
5f5c8ee5 3935 {
e4093f38 3936 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
bafb434c 3937 return 0;
2311178e 3938
b2683be4 3939 value = XCAR (XCDR (spec));
5f5c8ee5
GM
3940 if (NUMBERP (value) && XFLOATINT (value) > 0)
3941 it->space_width = value;
b2683be4
RS
3942
3943 return 0;
5f5c8ee5 3944 }
b2683be4
RS
3945
3946 /* Handle `(slice X Y WIDTH HEIGHT)'. */
3947 if (CONSP (spec)
3948 && EQ (XCAR (spec), Qslice))
e893970b 3949 {
e893970b
KS
3950 Lisp_Object tem;
3951
3952 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3953 return 0;
3954
b2683be4 3955 if (tem = XCDR (spec), CONSP (tem))
e893970b
KS
3956 {
3957 it->slice.x = XCAR (tem);
3958 if (tem = XCDR (tem), CONSP (tem))
3959 {
3960 it->slice.y = XCAR (tem);
3961 if (tem = XCDR (tem), CONSP (tem))
3962 {
3963 it->slice.width = XCAR (tem);
3964 if (tem = XCDR (tem), CONSP (tem))
3965 it->slice.height = XCAR (tem);
3966 }
3967 }
3968 }
b2683be4
RS
3969
3970 return 0;
e893970b 3971 }
b2683be4
RS
3972
3973 /* Handle `(raise FACTOR)'. */
3974 if (CONSP (spec)
3975 && EQ (XCAR (spec), Qraise)
3976 && CONSP (XCDR (spec)))
5f5c8ee5 3977 {
e4093f38 3978 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
bafb434c 3979 return 0;
2311178e 3980
fb3842a8 3981#ifdef HAVE_WINDOW_SYSTEM
b2683be4 3982 value = XCAR (XCDR (spec));
5f5c8ee5
GM
3983 if (NUMBERP (value))
3984 {
3985 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3986 it->voffset = - (XFLOATINT (value)
1ab3e082 3987 * (FONT_HEIGHT (face->font)));
5f5c8ee5
GM
3988 }
3989#endif /* HAVE_WINDOW_SYSTEM */
b2683be4
RS
3990
3991 return 0;
5f5c8ee5 3992 }
5f5c8ee5 3993
b2683be4
RS
3994 /* Don't handle the other kinds of display specifications
3995 inside a string that we got from a `display' property. */
3996 if (it->string_from_display_prop_p)
3997 return 0;
5f5c8ee5 3998
b2683be4
RS
3999 /* Characters having this form of property are not displayed, so
4000 we have to find the end of the property. */
4001 start_pos = *position;
4002 *position = display_prop_end (it, object, start_pos);
4003 value = Qnil;
5f5c8ee5 4004
b2683be4
RS
4005 /* Stop the scan at that end position--we assume that all
4006 text properties change there. */
4007 it->stop_charpos = position->charpos;
dd341dd9 4008
b2683be4
RS
4009 /* Handle `(left-fringe BITMAP [FACE])'
4010 and `(right-fringe BITMAP [FACE])'. */
4011 if (CONSP (spec)
4012 && (EQ (XCAR (spec), Qleft_fringe)
4013 || EQ (XCAR (spec), Qright_fringe))
4014 && CONSP (XCDR (spec)))
4015 {
4016 int face_id = DEFAULT_FACE_ID;
4017 int fringe_bitmap;
dd341dd9 4018
b2683be4
RS
4019 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4020 /* If we return here, POSITION has been advanced
4021 across the text with this property. */
4022 return 0;
dd341dd9
KS
4023
4024#ifdef HAVE_WINDOW_SYSTEM
b2683be4
RS
4025 value = XCAR (XCDR (spec));
4026 if (!SYMBOLP (value)
4027 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4028 /* If we return here, POSITION has been advanced
4029 across the text with this property. */
4030 return 0;
dd341dd9 4031
b2683be4
RS
4032 if (CONSP (XCDR (XCDR (spec))))
4033 {
4034 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
3a2b4c84
KS
4035 int face_id2 = lookup_derived_face (it->f, face_name,
4036 'A', FRINGE_FACE_ID, 0);
b2683be4
RS
4037 if (face_id2 >= 0)
4038 face_id = face_id2;
4039 }
dd341dd9 4040
b2683be4
RS
4041 /* Save current settings of IT so that we can restore them
4042 when we are finished with the glyph property value. */
dd341dd9 4043
b2683be4 4044 push_it (it);
dd341dd9 4045
b2683be4
RS
4046 it->area = TEXT_AREA;
4047 it->what = IT_IMAGE;
4048 it->image_id = -1; /* no image */
4049 it->position = start_pos;
4050 it->object = NILP (object) ? it->w->buffer : object;
a6f82cc4 4051 it->method = GET_FROM_IMAGE;
b2683be4 4052 it->face_id = face_id;
dd341dd9 4053
b2683be4
RS
4054 /* Say that we haven't consumed the characters with
4055 `display' property yet. The call to pop_it in
4056 set_iterator_to_next will clean this up. */
4057 *position = start_pos;
dd341dd9 4058
b2683be4 4059 if (EQ (XCAR (spec), Qleft_fringe))
5f5c8ee5 4060 {
b2683be4
RS
4061 it->left_user_fringe_bitmap = fringe_bitmap;
4062 it->left_user_fringe_face_id = face_id;
5f5c8ee5 4063 }
b2683be4 4064 else
5f5c8ee5 4065 {
b2683be4
RS
4066 it->right_user_fringe_bitmap = fringe_bitmap;
4067 it->right_user_fringe_face_id = face_id;
5f5c8ee5 4068 }
b2683be4
RS
4069#endif /* HAVE_WINDOW_SYSTEM */
4070 return 1;
4071 }
5f5c8ee5 4072
b2683be4
RS
4073 /* Prepare to handle `((margin left-margin) ...)',
4074 `((margin right-margin) ...)' and `((margin nil) ...)'
4075 prefixes for display specifications. */
4076 location = Qunbound;
4077 if (CONSP (spec) && CONSP (XCAR (spec)))
4078 {
4079 Lisp_Object tem;
2311178e 4080
b2683be4
RS
4081 value = XCDR (spec);
4082 if (CONSP (value))
4083 value = XCAR (value);
2311178e 4084
b2683be4
RS
4085 tem = XCAR (spec);
4086 if (EQ (XCAR (tem), Qmargin)
4087 && (tem = XCDR (tem),
4088 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4089 (NILP (tem)
4090 || EQ (tem, Qleft_margin)
4091 || EQ (tem, Qright_margin))))
4092 location = tem;
4093 }
2311178e 4094
b2683be4
RS
4095 if (EQ (location, Qunbound))
4096 {
4097 location = Qnil;
4098 value = spec;
4099 }
2311178e 4100
b2683be4
RS
4101 /* After this point, VALUE is the property after any
4102 margin prefix has been stripped. It must be a string,
4103 an image specification, or `(space ...)'.
4104
4105 LOCATION specifies where to display: `left-margin',
4106 `right-margin' or nil. */
4107
4108 valid_p = (STRINGP (value)
5f5c8ee5 4109#ifdef HAVE_WINDOW_SYSTEM
b2683be4
RS
4110 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
4111#endif /* not HAVE_WINDOW_SYSTEM */
4112 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4113
4114 if (valid_p && !display_replaced_before_p)
4115 {
4116 /* Save current settings of IT so that we can restore them
4117 when we are finished with the glyph property value. */
4118 push_it (it);
4119
4120 if (NILP (location))
4121 it->area = TEXT_AREA;
4122 else if (EQ (location, Qleft_margin))
4123 it->area = LEFT_MARGIN_AREA;
4124 else
4125 it->area = RIGHT_MARGIN_AREA;
4126
4127 if (STRINGP (value))
4128 {
a605fefc
KS
4129 if (SCHARS (value) == 0)
4130 {
4131 pop_it (it);
4132 return -1; /* Replaced by "", i.e. nothing. */
4133 }
b2683be4
RS
4134 it->string = value;
4135 it->multibyte_p = STRING_MULTIBYTE (it->string);
4136 it->current.overlay_string_index = -1;
4137 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4138 it->end_charpos = it->string_nchars = SCHARS (it->string);
a6f82cc4 4139 it->method = GET_FROM_STRING;
b2683be4
RS
4140 it->stop_charpos = 0;
4141 it->string_from_display_prop_p = 1;
4142 /* Say that we haven't consumed the characters with
4143 `display' property yet. The call to pop_it in
4144 set_iterator_to_next will clean this up. */
4145 *position = start_pos;
4146 }
4147 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4148 {
a6f82cc4 4149 it->method = GET_FROM_STRETCH;
b2683be4
RS
4150 it->object = value;
4151 it->current.pos = it->position = start_pos;
5f5c8ee5 4152 }
b2683be4 4153#ifdef HAVE_WINDOW_SYSTEM
a21a3943 4154 else
b2683be4
RS
4155 {
4156 it->what = IT_IMAGE;
4157 it->image_id = lookup_image (it->f, value);
4158 it->position = start_pos;
4159 it->object = NILP (object) ? it->w->buffer : object;
a6f82cc4 4160 it->method = GET_FROM_IMAGE;
b2683be4
RS
4161
4162 /* Say that we haven't consumed the characters with
4163 `display' property yet. The call to pop_it in
4164 set_iterator_to_next will clean this up. */
4165 *position = start_pos;
4166 }
4167#endif /* HAVE_WINDOW_SYSTEM */
4168
4169 return 1;
5f5c8ee5
GM
4170 }
4171
b2683be4
RS
4172 /* Invalid property or property not supported. Restore
4173 POSITION to what it was before. */
4174 *position = start_pos;
4175 return 0;
5f5c8ee5
GM
4176}
4177
4178
b2683be4 4179/* Check if SPEC is a display specification value whose text should be
06568bbf
GM
4180 treated as intangible. */
4181
4182static int
b2683be4 4183single_display_spec_intangible_p (prop)
06568bbf
GM
4184 Lisp_Object prop;
4185{
4186 /* Skip over `when FORM'. */
4187 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4188 {
4189 prop = XCDR (prop);
4190 if (!CONSP (prop))
4191 return 0;
4192 prop = XCDR (prop);
4193 }
4194
959bc044
SM
4195 if (STRINGP (prop))
4196 return 1;
4197
06568bbf
GM
4198 if (!CONSP (prop))
4199 return 0;
4200
4201 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4202 we don't need to treat text as intangible. */
4203 if (EQ (XCAR (prop), Qmargin))
4204 {
4205 prop = XCDR (prop);
4206 if (!CONSP (prop))
4207 return 0;
4208
4209 prop = XCDR (prop);
4210 if (!CONSP (prop)
4211 || EQ (XCAR (prop), Qleft_margin)
4212 || EQ (XCAR (prop), Qright_margin))
4213 return 0;
4214 }
2311178e 4215
6cd83a9a
SM
4216 return (CONSP (prop)
4217 && (EQ (XCAR (prop), Qimage)
4218 || EQ (XCAR (prop), Qspace)));
06568bbf
GM
4219}
4220
4221
4222/* Check if PROP is a display property value whose text should be
4223 treated as intangible. */
4224
4225int
4226display_prop_intangible_p (prop)
4227 Lisp_Object prop;
4228{
4229 if (CONSP (prop)
4230 && CONSP (XCAR (prop))
4231 && !EQ (Qmargin, XCAR (XCAR (prop))))
4232 {
4233 /* A list of sub-properties. */
4234 while (CONSP (prop))
4235 {
b2683be4 4236 if (single_display_spec_intangible_p (XCAR (prop)))
06568bbf
GM
4237 return 1;
4238 prop = XCDR (prop);
4239 }
4240 }
4241 else if (VECTORP (prop))
4242 {
4243 /* A vector of sub-properties. */
4244 int i;
a61b7058 4245 for (i = 0; i < ASIZE (prop); ++i)
b2683be4 4246 if (single_display_spec_intangible_p (AREF (prop, i)))
06568bbf
GM
4247 return 1;
4248 }
4249 else
b2683be4 4250 return single_display_spec_intangible_p (prop);
06568bbf
GM
4251
4252 return 0;
4253}
4254
74bd6d65
GM
4255
4256/* Return 1 if PROP is a display sub-property value containing STRING. */
4257
4258static int
b2683be4 4259single_display_spec_string_p (prop, string)
74bd6d65
GM
4260 Lisp_Object prop, string;
4261{
74bd6d65
GM
4262 if (EQ (string, prop))
4263 return 1;
2311178e 4264
74bd6d65
GM
4265 /* Skip over `when FORM'. */
4266 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4267 {
4268 prop = XCDR (prop);
4269 if (!CONSP (prop))
4270 return 0;
4271 prop = XCDR (prop);
4272 }
4273
4274 if (CONSP (prop))
4275 /* Skip over `margin LOCATION'. */
4276 if (EQ (XCAR (prop), Qmargin))
4277 {
4278 prop = XCDR (prop);
4279 if (!CONSP (prop))
4280 return 0;
4281
4282 prop = XCDR (prop);
4283 if (!CONSP (prop))
4284 return 0;
4285 }
2311178e 4286
74bd6d65
GM
4287 return CONSP (prop) && EQ (XCAR (prop), string);
4288}
4289
4290
4291/* Return 1 if STRING appears in the `display' property PROP. */
4292
4293static int
4294display_prop_string_p (prop, string)
4295 Lisp_Object prop, string;
4296{
74bd6d65
GM
4297 if (CONSP (prop)
4298 && CONSP (XCAR (prop))
4299 && !EQ (Qmargin, XCAR (XCAR (prop))))
4300 {
4301 /* A list of sub-properties. */
4302 while (CONSP (prop))
4303 {
b2683be4 4304 if (single_display_spec_string_p (XCAR (prop), string))
74bd6d65
GM
4305 return 1;
4306 prop = XCDR (prop);
4307 }
4308 }
4309 else if (VECTORP (prop))
4310 {
4311 /* A vector of sub-properties. */
4312 int i;
4313 for (i = 0; i < ASIZE (prop); ++i)
b2683be4 4314 if (single_display_spec_string_p (AREF (prop, i), string))
74bd6d65
GM
4315 return 1;
4316 }
4317 else
b2683be4 4318 return single_display_spec_string_p (prop, string);
74bd6d65
GM
4319
4320 return 0;
4321}
4322
4323
4324/* Determine from which buffer position in W's buffer STRING comes
4325 from. AROUND_CHARPOS is an approximate position where it could
4326 be from. Value is the buffer position or 0 if it couldn't be
4327 determined.
4328
4329 W's buffer must be current.
4330
4331 This function is necessary because we don't record buffer positions
4332 in glyphs generated from strings (to keep struct glyph small).
4333 This function may only use code that doesn't eval because it is
4334 called asynchronously from note_mouse_highlight. */
4335
4336int
4337string_buffer_position (w, string, around_charpos)
4338 struct window *w;
4339 Lisp_Object string;
4340 int around_charpos;
4341{
74bd6d65
GM
4342 Lisp_Object limit, prop, pos;
4343 const int MAX_DISTANCE = 1000;
4344 int found = 0;
4345
d8731202 4346 pos = make_number (around_charpos);
74bd6d65
GM
4347 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4348 while (!found && !EQ (pos, limit))
4349 {
4350 prop = Fget_char_property (pos, Qdisplay, Qnil);
4351 if (!NILP (prop) && display_prop_string_p (prop, string))
4352 found = 1;
4353 else
134d9283 4354 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
74bd6d65
GM
4355 }
4356
4357 if (!found)
4358 {
d8731202 4359 pos = make_number (around_charpos);
74bd6d65
GM
4360 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4361 while (!found && !EQ (pos, limit))
4362 {
4363 prop = Fget_char_property (pos, Qdisplay, Qnil);
4364 if (!NILP (prop) && display_prop_string_p (prop, string))
4365 found = 1;
4366 else
134d9283
GM
4367 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4368 limit);
74bd6d65
GM
4369 }
4370 }
4371
4372 return found ? XINT (pos) : 0;
4373}
4374
4375
5f5c8ee5 4376\f
260a86a0
KH
4377/***********************************************************************
4378 `composition' property
4379 ***********************************************************************/
4380
4381/* Set up iterator IT from `composition' property at its current
4382 position. Called from handle_stop. */
4383
4384static enum prop_handled
4385handle_composition_prop (it)
4386 struct it *it;
4387{
4388 Lisp_Object prop, string;
4389 int pos, pos_byte, end;
4390 enum prop_handled handled = HANDLED_NORMALLY;
4391
4392 if (STRINGP (it->string))
4393 {
4394 pos = IT_STRING_CHARPOS (*it);
4395 pos_byte = IT_STRING_BYTEPOS (*it);
4396 string = it->string;
4397 }
4398 else
4399 {
4400 pos = IT_CHARPOS (*it);
4401 pos_byte = IT_BYTEPOS (*it);
4402 string = Qnil;
4403 }
4404
4405 /* If there's a valid composition and point is not inside of the
4406 composition (in the case that the composition is from the current
4407 buffer), draw a glyph composed from the composition components. */
4408 if (find_composition (pos, -1, &pos, &end, &prop, string)
4409 && COMPOSITION_VALID_P (pos, end, prop)
4410 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4411 {
4412 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4413
4414 if (id >= 0)
4415 {
a6f82cc4 4416 it->method = GET_FROM_COMPOSITION;
260a86a0
KH
4417 it->cmp_id = id;
4418 it->cmp_len = COMPOSITION_LENGTH (prop);
4419 /* For a terminal, draw only the first character of the
4420 components. */
4421 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4422 it->len = (STRINGP (it->string)
4423 ? string_char_to_byte (it->string, end)
4424 : CHAR_TO_BYTE (end)) - pos_byte;
4425 it->stop_charpos = end;
4426 handled = HANDLED_RETURN;
4427 }
4428 }
4429
4430 return handled;
4431}
4432
4433
4434\f
5f5c8ee5
GM
4435/***********************************************************************
4436 Overlay strings
4437 ***********************************************************************/
4438
4439/* The following structure is used to record overlay strings for
4440 later sorting in load_overlay_strings. */
4441
4442struct overlay_entry
4443{
2970b9be 4444 Lisp_Object overlay;
5f5c8ee5
GM
4445 Lisp_Object string;
4446 int priority;
4447 int after_string_p;
4448};
4449
4450
4451/* Set up iterator IT from overlay strings at its current position.
4452 Called from handle_stop. */
4453
4454static enum prop_handled
4455handle_overlay_change (it)
4456 struct it *it;
4457{
5a08cbaf 4458 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
2970b9be 4459 return HANDLED_RECOMPUTE_PROPS;
5f5c8ee5 4460 else
2970b9be 4461 return HANDLED_NORMALLY;
5f5c8ee5
GM
4462}
4463
4464
4465/* Set up the next overlay string for delivery by IT, if there is an
4466 overlay string to deliver. Called by set_iterator_to_next when the
4467 end of the current overlay string is reached. If there are more
4468 overlay strings to display, IT->string and
4469 IT->current.overlay_string_index are set appropriately here.
4470 Otherwise IT->string is set to nil. */
2311178e 4471
5f5c8ee5
GM
4472static void
4473next_overlay_string (it)
4474 struct it *it;
4475{
4476 ++it->current.overlay_string_index;
4477 if (it->current.overlay_string_index == it->n_overlay_strings)
4478 {
4479 /* No more overlay strings. Restore IT's settings to what
4480 they were before overlay strings were processed, and
4481 continue to deliver from current_buffer. */
5a08cbaf 4482 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
2311178e 4483
5f5c8ee5
GM
4484 pop_it (it);
4485 xassert (it->stop_charpos >= BEGV
4486 && it->stop_charpos <= it->end_charpos);
4487 it->string = Qnil;
4488 it->current.overlay_string_index = -1;
4489 SET_TEXT_POS (it->current.string_pos, -1, -1);
4490 it->n_overlay_strings = 0;
a6f82cc4 4491 it->method = GET_FROM_BUFFER;
2970b9be
GM
4492
4493 /* If we're at the end of the buffer, record that we have
4494 processed the overlay strings there already, so that
4495 next_element_from_buffer doesn't try it again. */
4496 if (IT_CHARPOS (*it) >= it->end_charpos)
4497 it->overlay_strings_at_end_processed_p = 1;
5a08cbaf
GM
4498
4499 /* If we have to display `...' for invisible text, set
4500 the iterator up for that. */
4501 if (display_ellipsis_p)
064fd8ec 4502 setup_for_ellipsis (it, 0);
5f5c8ee5
GM
4503 }
4504 else
4505 {
4506 /* There are more overlay strings to process. If
4507 IT->current.overlay_string_index has advanced to a position
4508 where we must load IT->overlay_strings with more strings, do
4509 it. */
4510 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
2311178e 4511
5f5c8ee5 4512 if (it->current.overlay_string_index && i == 0)
5a08cbaf 4513 load_overlay_strings (it, 0);
5f5c8ee5
GM
4514
4515 /* Initialize IT to deliver display elements from the overlay
4516 string. */
4517 it->string = it->overlay_strings[i];
4518 it->multibyte_p = STRING_MULTIBYTE (it->string);
4519 SET_TEXT_POS (it->current.string_pos, 0, 0);
a6f82cc4 4520 it->method = GET_FROM_STRING;
5f5c8ee5
GM
4521 it->stop_charpos = 0;
4522 }
2311178e 4523
5f5c8ee5
GM
4524 CHECK_IT (it);
4525}
4526
4527
4528/* Compare two overlay_entry structures E1 and E2. Used as a
4529 comparison function for qsort in load_overlay_strings. Overlay
4530 strings for the same position are sorted so that
4531
2970b9be
GM
4532 1. All after-strings come in front of before-strings, except
4533 when they come from the same overlay.
2311178e 4534
5f5c8ee5
GM
4535 2. Within after-strings, strings are sorted so that overlay strings
4536 from overlays with higher priorities come first.
4537
4538 2. Within before-strings, strings are sorted so that overlay
4539 strings from overlays with higher priorities come last.
4540
4541 Value is analogous to strcmp. */
4542
2311178e 4543
5f5c8ee5
GM
4544static int
4545compare_overlay_entries (e1, e2)
4546 void *e1, *e2;
4547{
4548 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4549 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4550 int result;
4551
4552 if (entry1->after_string_p != entry2->after_string_p)
2970b9be
GM
4553 {
4554 /* Let after-strings appear in front of before-strings if
4555 they come from different overlays. */
4556 if (EQ (entry1->overlay, entry2->overlay))
4557 result = entry1->after_string_p ? 1 : -1;
4558 else
4559 result = entry1->after_string_p ? -1 : 1;
4560 }
5f5c8ee5
GM
4561 else if (entry1->after_string_p)
4562 /* After-strings sorted in order of decreasing priority. */
4563 result = entry2->priority - entry1->priority;
4564 else
4565 /* Before-strings sorted in order of increasing priority. */
4566 result = entry1->priority - entry2->priority;
4567
4568 return result;
4569}
4570
4571
4572/* Load the vector IT->overlay_strings with overlay strings from IT's
5a08cbaf
GM
4573 current buffer position, or from CHARPOS if that is > 0. Set
4574 IT->n_overlays to the total number of overlay strings found.
5f5c8ee5
GM
4575
4576 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4577 a time. On entry into load_overlay_strings,
4578 IT->current.overlay_string_index gives the number of overlay
4579 strings that have already been loaded by previous calls to this
4580 function.
4581
2970b9be
GM
4582 IT->add_overlay_start contains an additional overlay start
4583 position to consider for taking overlay strings from, if non-zero.
4584 This position comes into play when the overlay has an `invisible'
4585 property, and both before and after-strings. When we've skipped to
4586 the end of the overlay, because of its `invisible' property, we
4587 nevertheless want its before-string to appear.
4588 IT->add_overlay_start will contain the overlay start position
4589 in this case.
4590
5f5c8ee5
GM
4591 Overlay strings are sorted so that after-string strings come in
4592 front of before-string strings. Within before and after-strings,
4593 strings are sorted by overlay priority. See also function
4594 compare_overlay_entries. */
2311178e 4595
5f5c8ee5 4596static void
5a08cbaf 4597load_overlay_strings (it, charpos)
5f5c8ee5 4598 struct it *it;
5a08cbaf 4599 int charpos;
5f5c8ee5
GM
4600{
4601 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
b7253a3e
SM
4602 Lisp_Object overlay, window, str, invisible;
4603 struct Lisp_Overlay *ov;
5f5c8ee5
GM
4604 int start, end;
4605 int size = 20;
cafafe0b 4606 int n = 0, i, j, invis_p;
5f5c8ee5
GM
4607 struct overlay_entry *entries
4608 = (struct overlay_entry *) alloca (size * sizeof *entries);
5a08cbaf
GM
4609
4610 if (charpos <= 0)
4611 charpos = IT_CHARPOS (*it);
5f5c8ee5
GM
4612
4613 /* Append the overlay string STRING of overlay OVERLAY to vector
4614 `entries' which has size `size' and currently contains `n'
4615 elements. AFTER_P non-zero means STRING is an after-string of
4616 OVERLAY. */
4617#define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4618 do \
4619 { \
4620 Lisp_Object priority; \
4621 \
4622 if (n == size) \
4623 { \
4624 int new_size = 2 * size; \
4625 struct overlay_entry *old = entries; \
4626 entries = \
4627 (struct overlay_entry *) alloca (new_size \
4628 * sizeof *entries); \
4629 bcopy (old, entries, size * sizeof *entries); \
4630 size = new_size; \
4631 } \
4632 \
4633 entries[n].string = (STRING); \
2970b9be 4634 entries[n].overlay = (OVERLAY); \
5f5c8ee5 4635 priority = Foverlay_get ((OVERLAY), Qpriority); \
2970b9be 4636 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5f5c8ee5
GM
4637 entries[n].after_string_p = (AFTER_P); \
4638 ++n; \
4639 } \
4640 while (0)
4641
4642 /* Process overlay before the overlay center. */
b7253a3e 4643 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5f5c8ee5 4644 {
b7253a3e 4645 XSETMISC (overlay, ov);
5f5c8ee5
GM
4646 xassert (OVERLAYP (overlay));
4647 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4648 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2311178e 4649
cafafe0b 4650 if (end < charpos)
5f5c8ee5
GM
4651 break;
4652
4653 /* Skip this overlay if it doesn't start or end at IT's current
4654 position. */
cafafe0b 4655 if (end != charpos && start != charpos)
5f5c8ee5 4656 continue;
2311178e 4657
5f5c8ee5
GM
4658 /* Skip this overlay if it doesn't apply to IT->w. */
4659 window = Foverlay_get (overlay, Qwindow);
4660 if (WINDOWP (window) && XWINDOW (window) != it->w)
4661 continue;
4662
cafafe0b
GM
4663 /* If the text ``under'' the overlay is invisible, both before-
4664 and after-strings from this overlay are visible; start and
4665 end position are indistinguishable. */
4666 invisible = Foverlay_get (overlay, Qinvisible);
4667 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4668
5f5c8ee5 4669 /* If overlay has a non-empty before-string, record it. */
cafafe0b 4670 if ((start == charpos || (end == charpos && invis_p))
5f5c8ee5 4671 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2051c264 4672 && SCHARS (str))
5f5c8ee5 4673 RECORD_OVERLAY_STRING (overlay, str, 0);
2311178e 4674
5f5c8ee5 4675 /* If overlay has a non-empty after-string, record it. */
cafafe0b 4676 if ((end == charpos || (start == charpos && invis_p))
5f5c8ee5 4677 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2051c264 4678 && SCHARS (str))
5f5c8ee5
GM
4679 RECORD_OVERLAY_STRING (overlay, str, 1);
4680 }
2311178e 4681
5f5c8ee5 4682 /* Process overlays after the overlay center. */
b7253a3e 4683 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5f5c8ee5 4684 {
b7253a3e 4685 XSETMISC (overlay, ov);
5f5c8ee5
GM
4686 xassert (OVERLAYP (overlay));
4687 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4688 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4689
cafafe0b 4690 if (start > charpos)
5f5c8ee5 4691 break;
2311178e 4692
5f5c8ee5
GM
4693 /* Skip this overlay if it doesn't start or end at IT's current
4694 position. */
cafafe0b 4695 if (end != charpos && start != charpos)
5f5c8ee5
GM
4696 continue;
4697
4698 /* Skip this overlay if it doesn't apply to IT->w. */
4699 window = Foverlay_get (overlay, Qwindow);
4700 if (WINDOWP (window) && XWINDOW (window) != it->w)
4701 continue;
2311178e 4702
cafafe0b
GM
4703 /* If the text ``under'' the overlay is invisible, it has a zero
4704 dimension, and both before- and after-strings apply. */
4705 invisible = Foverlay_get (overlay, Qinvisible);
4706 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4707
5f5c8ee5 4708 /* If overlay has a non-empty before-string, record it. */
cafafe0b 4709 if ((start == charpos || (end == charpos && invis_p))
5f5c8ee5 4710 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2051c264 4711 && SCHARS (str))
5f5c8ee5 4712 RECORD_OVERLAY_STRING (overlay, str, 0);
2311178e 4713
5f5c8ee5 4714 /* If overlay has a non-empty after-string, record it. */
cafafe0b 4715 if ((end == charpos || (start == charpos && invis_p))
5f5c8ee5 4716 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2051c264 4717 && SCHARS (str))
5f5c8ee5
GM
4718 RECORD_OVERLAY_STRING (overlay, str, 1);
4719 }
4720
4721#undef RECORD_OVERLAY_STRING
2311178e 4722
5f5c8ee5 4723 /* Sort entries. */
cafafe0b 4724 if (n > 1)
2970b9be 4725 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5f5c8ee5
GM
4726
4727 /* Record the total number of strings to process. */
4728 it->n_overlay_strings = n;
4729
4730 /* IT->current.overlay_string_index is the number of overlay strings
4731 that have already been consumed by IT. Copy some of the
4732 remaining overlay strings to IT->overlay_strings. */
4733 i = 0;
4734 j = it->current.overlay_string_index;
4735 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4736 it->overlay_strings[i++] = entries[j++].string;
2970b9be 4737
5f5c8ee5
GM
4738 CHECK_IT (it);
4739}
4740
4741
4742/* Get the first chunk of overlay strings at IT's current buffer
5a08cbaf
GM
4743 position, or at CHARPOS if that is > 0. Value is non-zero if at
4744 least one overlay string was found. */
5f5c8ee5
GM
4745
4746static int
5a08cbaf 4747get_overlay_strings (it, charpos)
5f5c8ee5 4748 struct it *it;
5a08cbaf 4749 int charpos;
5f5c8ee5
GM
4750{
4751 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4752 process. This fills IT->overlay_strings with strings, and sets
4753 IT->n_overlay_strings to the total number of strings to process.
4754 IT->pos.overlay_string_index has to be set temporarily to zero
4755 because load_overlay_strings needs this; it must be set to -1
4756 when no overlay strings are found because a zero value would
4757 indicate a position in the first overlay string. */
4758 it->current.overlay_string_index = 0;
5a08cbaf 4759 load_overlay_strings (it, charpos);
5f5c8ee5
GM
4760
4761 /* If we found overlay strings, set up IT to deliver display
4762 elements from the first one. Otherwise set up IT to deliver
4763 from current_buffer. */
4764 if (it->n_overlay_strings)
4765 {
4766 /* Make sure we know settings in current_buffer, so that we can
4767 restore meaningful values when we're done with the overlay
4768 strings. */
4769 compute_stop_pos (it);
4770 xassert (it->face_id >= 0);
2311178e 4771
5f5c8ee5
GM
4772 /* Save IT's settings. They are restored after all overlay
4773 strings have been processed. */
4774 xassert (it->sp == 0);
4775 push_it (it);
4776
4777 /* Set up IT to deliver display elements from the first overlay
4778 string. */
4779 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5f5c8ee5 4780 it->string = it->overlay_strings[0];
b2046df8 4781 it->stop_charpos = 0;
5f5c8ee5 4782 xassert (STRINGP (it->string));
2051c264 4783 it->end_charpos = SCHARS (it->string);
d5db4077 4784 it->multibyte_p = STRING_MULTIBYTE (it->string);
a6f82cc4 4785 it->method = GET_FROM_STRING;
5f5c8ee5
GM
4786 }
4787 else
4788 {
4789 it->string = Qnil;
4790 it->current.overlay_string_index = -1;
a6f82cc4 4791 it->method = GET_FROM_BUFFER;
5f5c8ee5
GM
4792 }
4793
4794 CHECK_IT (it);
4795
4796 /* Value is non-zero if we found at least one overlay string. */
4797 return STRINGP (it->string);
4798}
4799
4800
4801\f
4802/***********************************************************************
4803 Saving and restoring state
4804 ***********************************************************************/
4805
4806/* Save current settings of IT on IT->stack. Called, for example,
4807 before setting up IT for an overlay string, to be able to restore
4808 IT's settings to what they were after the overlay string has been
4809 processed. */
4810
4811static void
4812push_it (it)
4813 struct it *it;
4814{
4815 struct iterator_stack_entry *p;
2311178e 4816
5f5c8ee5
GM
4817 xassert (it->sp < 2);
4818 p = it->stack + it->sp;
4819
4820 p->stop_charpos = it->stop_charpos;
4821 xassert (it->face_id >= 0);
4822 p->face_id = it->face_id;
4823 p->string = it->string;
4824 p->pos = it->current;
4825 p->end_charpos = it->end_charpos;
4826 p->string_nchars = it->string_nchars;
4827 p->area = it->area;
4828 p->multibyte_p = it->multibyte_p;
e893970b 4829 p->slice = it->slice;
5f5c8ee5
GM
4830 p->space_width = it->space_width;
4831 p->font_height = it->font_height;
4832 p->voffset = it->voffset;
4833 p->string_from_display_prop_p = it->string_from_display_prop_p;
5a08cbaf 4834 p->display_ellipsis_p = 0;
5f5c8ee5
GM
4835 ++it->sp;
4836}
4837
4838
4839/* Restore IT's settings from IT->stack. Called, for example, when no
4840 more overlay strings must be processed, and we return to delivering
4841 display elements from a buffer, or when the end of a string from a
4842 `display' property is reached and we return to delivering display
4843 elements from an overlay string, or from a buffer. */
4844
4845static void
4846pop_it (it)
4847 struct it *it;
4848{
4849 struct iterator_stack_entry *p;
2311178e 4850
5f5c8ee5
GM
4851 xassert (it->sp > 0);
4852 --it->sp;
4853 p = it->stack + it->sp;
4854 it->stop_charpos = p->stop_charpos;
4855 it->face_id = p->face_id;
4856 it->string = p->string;
4857 it->current = p->pos;
4858 it->end_charpos = p->end_charpos;
4859 it->string_nchars = p->string_nchars;
4860 it->area = p->area;
4861 it->multibyte_p = p->multibyte_p;
e893970b 4862 it->slice = p->slice;
5f5c8ee5
GM
4863 it->space_width = p->space_width;
4864 it->font_height = p->font_height;
4865 it->voffset = p->voffset;
4866 it->string_from_display_prop_p = p->string_from_display_prop_p;
4867}
4868
4869
4870\f
4871/***********************************************************************
4872 Moving over lines
4873 ***********************************************************************/
4874
4875/* Set IT's current position to the previous line start. */
4876
4877static void
4878back_to_previous_line_start (it)
4879 struct it *it;
4880{
4881 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4882 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4883}
4884
4885
cafafe0b 4886/* Move IT to the next line start.
2311178e 4887
cafafe0b
GM
4888 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4889 we skipped over part of the text (as opposed to moving the iterator
4890 continuously over the text). Otherwise, don't change the value
4891 of *SKIPPED_P.
2311178e 4892
cafafe0b
GM
4893 Newlines may come from buffer text, overlay strings, or strings
4894 displayed via the `display' property. That's the reason we can't
0fd37545
GM
4895 simply use find_next_newline_no_quit.
4896
4897 Note that this function may not skip over invisible text that is so
4898 because of text properties and immediately follows a newline. If
4899 it would, function reseat_at_next_visible_line_start, when called
4900 from set_iterator_to_next, would effectively make invisible
4901 characters following a newline part of the wrong glyph row, which
4902 leads to wrong cursor motion. */
5f5c8ee5 4903
cafafe0b
GM
4904static int
4905forward_to_next_line_start (it, skipped_p)
5f5c8ee5 4906 struct it *it;
cafafe0b 4907 int *skipped_p;
5f5c8ee5 4908{
54918e2b 4909 int old_selective, newline_found_p, n;
cafafe0b
GM
4910 const int MAX_NEWLINE_DISTANCE = 500;
4911
0fd37545
GM
4912 /* If already on a newline, just consume it to avoid unintended
4913 skipping over invisible text below. */
51695746
GM
4914 if (it->what == IT_CHARACTER
4915 && it->c == '\n'
4916 && CHARPOS (it->position) == IT_CHARPOS (*it))
0fd37545
GM
4917 {
4918 set_iterator_to_next (it, 0);
a77dc1ec 4919 it->c = 0;
0fd37545
GM
4920 return 1;
4921 }
4922
54918e2b 4923 /* Don't handle selective display in the following. It's (a)
0fd37545
GM
4924 unnecessary because it's done by the caller, and (b) leads to an
4925 infinite recursion because next_element_from_ellipsis indirectly
4926 calls this function. */
54918e2b
GM
4927 old_selective = it->selective;
4928 it->selective = 0;
4929
cafafe0b
GM
4930 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4931 from buffer text. */
3aec8722
GM
4932 for (n = newline_found_p = 0;
4933 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4934 n += STRINGP (it->string) ? 0 : 1)
cafafe0b 4935 {
8692ca92 4936 if (!get_next_display_element (it))
d43be70c 4937 return 0;
d02f1cb8 4938 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
cafafe0b 4939 set_iterator_to_next (it, 0);
cafafe0b
GM
4940 }
4941
4942 /* If we didn't find a newline near enough, see if we can use a
4943 short-cut. */
db0bb807 4944 if (!newline_found_p)
cafafe0b
GM
4945 {
4946 int start = IT_CHARPOS (*it);
4947 int limit = find_next_newline_no_quit (start, 1);
4948 Lisp_Object pos;
4949
4950 xassert (!STRINGP (it->string));
4951
4952 /* If there isn't any `display' property in sight, and no
4953 overlays, we can just use the position of the newline in
4954 buffer text. */
4955 if (it->stop_charpos >= limit
4956 || ((pos = Fnext_single_property_change (make_number (start),
4957 Qdisplay,
4958 Qnil, make_number (limit)),
4959 NILP (pos))
4960 && next_overlay_change (start) == ZV))
4961 {
4962 IT_CHARPOS (*it) = limit;
4963 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4964 *skipped_p = newline_found_p = 1;
4965 }
4966 else
4967 {
4968 while (get_next_display_element (it)
4969 && !newline_found_p)
4970 {
4971 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4972 set_iterator_to_next (it, 0);
4973 }
4974 }
4975 }
4976
54918e2b 4977 it->selective = old_selective;
cafafe0b 4978 return newline_found_p;
5f5c8ee5
GM
4979}
4980
4981
4982/* Set IT's current position to the previous visible line start. Skip
4983 invisible text that is so either due to text properties or due to
4984 selective display. Caution: this does not change IT->current_x and
4985 IT->hpos. */
4986
4987static void
4988back_to_previous_visible_line_start (it)
4989 struct it *it;
4990{
6512b56d 4991 while (IT_CHARPOS (*it) > BEGV)
5f5c8ee5 4992 {
6512b56d
KS
4993 back_to_previous_line_start (it);
4994 if (IT_CHARPOS (*it) <= BEGV)
4995 break;
5f5c8ee5
GM
4996
4997 /* If selective > 0, then lines indented more than that values
4998 are invisible. */
4999 if (it->selective > 0
5000 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
a67e162b 5001 (double) it->selective)) /* iftc */
6512b56d 5002 continue;
5f5c8ee5 5003
6512b56d
KS
5004 /* Check the newline before point for invisibility. */
5005 {
5006 Lisp_Object prop;
5007 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6fc556fd 5008 Qinvisible, it->window);
6512b56d
KS
5009 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5010 continue;
5011 }
6c0afe12 5012
6512b56d
KS
5013 /* If newline has a display property that replaces the newline with something
5014 else (image or text), find start of overlay or interval and continue search
5015 from that point. */
e10ee30c
KS
5016 if (IT_CHARPOS (*it) > BEGV)
5017 {
5018 struct it it2 = *it;
5019 int pos;
5020 int beg, end;
5021 Lisp_Object val, overlay;
5022
5023 pos = --IT_CHARPOS (it2);
5024 --IT_BYTEPOS (it2);
5025 it2.sp = 0;
5026 if (handle_display_prop (&it2) == HANDLED_RETURN
5027 && !NILP (val = get_char_property_and_overlay
5028 (make_number (pos), Qdisplay, Qnil, &overlay))
5029 && (OVERLAYP (overlay)
5030 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5031 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5032 {
5033 if (beg < BEGV)
5034 beg = BEGV;
5035 IT_CHARPOS (*it) = beg;
5036 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5037 continue;
5038 }
5039 }
5040
6512b56d 5041 break;
5f5c8ee5
GM
5042 }
5043
5044 xassert (IT_CHARPOS (*it) >= BEGV);
5045 xassert (IT_CHARPOS (*it) == BEGV
5046 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5047 CHECK_IT (it);
5048}
5049
5050
5051/* Reseat iterator IT at the previous visible line start. Skip
5052 invisible text that is so either due to text properties or due to
5053 selective display. At the end, update IT's overlay information,
5054 face information etc. */
5055
0b2e177e 5056void
5f5c8ee5
GM
5057reseat_at_previous_visible_line_start (it)
5058 struct it *it;
5059{
5060 back_to_previous_visible_line_start (it);
5061 reseat (it, it->current.pos, 1);
5062 CHECK_IT (it);
5063}
5064
5065
5066/* Reseat iterator IT on the next visible line start in the current
312246d1
GM
5067 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5068 preceding the line start. Skip over invisible text that is so
5069 because of selective display. Compute faces, overlays etc at the
5070 new position. Note that this function does not skip over text that
5071 is invisible because of text properties. */
5f5c8ee5
GM
5072
5073static void
312246d1 5074reseat_at_next_visible_line_start (it, on_newline_p)
5f5c8ee5 5075 struct it *it;
312246d1 5076 int on_newline_p;
5f5c8ee5 5077{
cafafe0b
GM
5078 int newline_found_p, skipped_p = 0;
5079
5080 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5081
5082 /* Skip over lines that are invisible because they are indented
5083 more than the value of IT->selective. */
5084 if (it->selective > 0)
5085 while (IT_CHARPOS (*it) < ZV
a77dc1ec 5086 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
a67e162b 5087 (double) it->selective)) /* iftc */
a77dc1ec 5088 {
ad5afd5b
KS
5089 xassert (IT_BYTEPOS (*it) == BEGV
5090 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
a77dc1ec
GM
5091 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5092 }
cafafe0b
GM
5093
5094 /* Position on the newline if that's what's requested. */
5095 if (on_newline_p && newline_found_p)
5f5c8ee5 5096 {
cafafe0b 5097 if (STRINGP (it->string))
5f5c8ee5 5098 {
cafafe0b
GM
5099 if (IT_STRING_CHARPOS (*it) > 0)
5100 {
5101 --IT_STRING_CHARPOS (*it);
5102 --IT_STRING_BYTEPOS (*it);
5103 }
5f5c8ee5 5104 }
cafafe0b 5105 else if (IT_CHARPOS (*it) > BEGV)
312246d1
GM
5106 {
5107 --IT_CHARPOS (*it);
cafafe0b
GM
5108 --IT_BYTEPOS (*it);
5109 reseat (it, it->current.pos, 0);
312246d1 5110 }
5f5c8ee5 5111 }
cafafe0b
GM
5112 else if (skipped_p)
5113 reseat (it, it->current.pos, 0);
2311178e 5114
5f5c8ee5
GM
5115 CHECK_IT (it);
5116}
5117
5118
5119\f
5120/***********************************************************************
5121 Changing an iterator's position
5122***********************************************************************/
5123
5124/* Change IT's current position to POS in current_buffer. If FORCE_P
5125 is non-zero, always check for text properties at the new position.
5126 Otherwise, text properties are only looked up if POS >=
5127 IT->check_charpos of a property. */
5128
5129static void
5130reseat (it, pos, force_p)
5131 struct it *it;
5132 struct text_pos pos;
5133 int force_p;
5134{
5135 int original_pos = IT_CHARPOS (*it);
5136
5137 reseat_1 (it, pos, 0);
5138
5139 /* Determine where to check text properties. Avoid doing it
5140 where possible because text property lookup is very expensive. */
5141 if (force_p
5142 || CHARPOS (pos) > it->stop_charpos
5143 || CHARPOS (pos) < original_pos)
5144 handle_stop (it);
5145
5146 CHECK_IT (it);
5147}
5148
5149
5150/* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5151 IT->stop_pos to POS, also. */
5152
5153static void
5154reseat_1 (it, pos, set_stop_p)
5155 struct it *it;
5156 struct text_pos pos;
5157 int set_stop_p;
5158{
5159 /* Don't call this function when scanning a C string. */
5160 xassert (it->s == NULL);
5161
5162 /* POS must be a reasonable value. */
5163 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5164
5165 it->current.pos = it->position = pos;
5166 XSETBUFFER (it->object, current_buffer);
78c663d8 5167 it->end_charpos = ZV;
5f5c8ee5
GM
5168 it->dpvec = NULL;
5169 it->current.dpvec_index = -1;
5170 it->current.overlay_string_index = -1;
5171 IT_STRING_CHARPOS (*it) = -1;
5172 IT_STRING_BYTEPOS (*it) = -1;
5173 it->string = Qnil;
a6f82cc4 5174 it->method = GET_FROM_BUFFER;
5905025c
RS
5175 /* RMS: I added this to fix a bug in move_it_vertically_backward
5176 where it->area continued to relate to the starting point
5177 for the backward motion. Bug report from
5178 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
5179 However, I am not sure whether reseat still does the right thing
5180 in general after this change. */
5181 it->area = TEXT_AREA;
06fd3792 5182 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5f5c8ee5 5183 it->sp = 0;
4aad61f8 5184 it->face_before_selective_p = 0;
5f5c8ee5
GM
5185
5186 if (set_stop_p)
5187 it->stop_charpos = CHARPOS (pos);
5188}
5189
5190
5191/* Set up IT for displaying a string, starting at CHARPOS in window W.
5192 If S is non-null, it is a C string to iterate over. Otherwise,
5193 STRING gives a Lisp string to iterate over.
2311178e 5194
5f5c8ee5
GM
5195 If PRECISION > 0, don't return more then PRECISION number of
5196 characters from the string.
5197
5198 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5199 characters have been returned. FIELD_WIDTH < 0 means an infinite
5200 field width.
5201
5202 MULTIBYTE = 0 means disable processing of multibyte characters,
5203 MULTIBYTE > 0 means enable it,
5204 MULTIBYTE < 0 means use IT->multibyte_p.
5205
5206 IT must be initialized via a prior call to init_iterator before
5207 calling this function. */
5208
5209static void
5210reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5211 struct it *it;
5212 unsigned char *s;
5213 Lisp_Object string;
5214 int charpos;
5215 int precision, field_width, multibyte;
5216{
5217 /* No region in strings. */
5218 it->region_beg_charpos = it->region_end_charpos = -1;
5219
5220 /* No text property checks performed by default, but see below. */
5221 it->stop_charpos = -1;
5222
5223 /* Set iterator position and end position. */
5224 bzero (&it->current, sizeof it->current);
5225 it->current.overlay_string_index = -1;
5226 it->current.dpvec_index = -1;
5f5c8ee5 5227 xassert (charpos >= 0);
2311178e 5228
e719f5ae
GM
5229 /* If STRING is specified, use its multibyteness, otherwise use the
5230 setting of MULTIBYTE, if specified. */
cabf45da 5231 if (multibyte >= 0)
5f5c8ee5 5232 it->multibyte_p = multibyte > 0;
2311178e 5233
5f5c8ee5
GM
5234 if (s == NULL)
5235 {
5236 xassert (STRINGP (string));
5237 it->string = string;
5238 it->s = NULL;
2051c264 5239 it->end_charpos = it->string_nchars = SCHARS (string);
a6f82cc4 5240 it->method = GET_FROM_STRING;
5f5c8ee5
GM
5241 it->current.string_pos = string_pos (charpos, string);
5242 }
5243 else
5244 {
5245 it->s = s;
5246 it->string = Qnil;
5247
5248 /* Note that we use IT->current.pos, not it->current.string_pos,
5249 for displaying C strings. */
5250 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5251 if (it->multibyte_p)
5252 {
5253 it->current.pos = c_string_pos (charpos, s, 1);
5254 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5255 }
5256 else
5257 {
5258 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5259 it->end_charpos = it->string_nchars = strlen (s);
5260 }
2311178e 5261
a6f82cc4 5262 it->method = GET_FROM_C_STRING;
5f5c8ee5
GM
5263 }
5264
5265 /* PRECISION > 0 means don't return more than PRECISION characters
5266 from the string. */
5267 if (precision > 0 && it->end_charpos - charpos > precision)
5268 it->end_charpos = it->string_nchars = charpos + precision;
5269
5270 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5271 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5272 FIELD_WIDTH < 0 means infinite field width. This is useful for
5273 padding with `-' at the end of a mode line. */
5274 if (field_width < 0)
5275 field_width = INFINITY;
5276 if (field_width > it->end_charpos - charpos)
5277 it->end_charpos = charpos + field_width;
5278
5279 /* Use the standard display table for displaying strings. */
5280 if (DISP_TABLE_P (Vstandard_display_table))
5281 it->dp = XCHAR_TABLE (Vstandard_display_table);
5282
5283 it->stop_charpos = charpos;
5284 CHECK_IT (it);
5285}
5286
5287
5288\f
5289/***********************************************************************
5290 Iteration
5291 ***********************************************************************/
5292
a6f82cc4
KS
5293/* Map enum it_method value to corresponding next_element_from_* function. */
5294
5295static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5296{
5297 next_element_from_buffer,
5298 next_element_from_display_vector,
5299 next_element_from_composition,
5300 next_element_from_string,
5301 next_element_from_c_string,
5302 next_element_from_image,
5303 next_element_from_stretch
5304};
5305
5306
5f5c8ee5
GM
5307/* Load IT's display element fields with information about the next
5308 display element from the current position of IT. Value is zero if
5309 end of buffer (or C string) is reached. */
5310
5311int
5312get_next_display_element (it)
5313 struct it *it;
5314{
7d0393cf 5315 /* Non-zero means that we found a display element. Zero means that
5f5c8ee5
GM
5316 we hit the end of what we iterate over. Performance note: the
5317 function pointer `method' used here turns out to be faster than
5318 using a sequence of if-statements. */
064fd8ec
KS
5319 int success_p;
5320
5321 get_next:
a6f82cc4 5322 success_p = (*get_next_element[it->method]) (it);
5f5c8ee5
GM
5323
5324 if (it->what == IT_CHARACTER)
5325 {
5326 /* Map via display table or translate control characters.
5327 IT->c, IT->len etc. have been set to the next character by
5328 the function call above. If we have a display table, and it
5329 contains an entry for IT->c, translate it. Don't do this if
5330 IT->c itself comes from a display table, otherwise we could
5331 end up in an infinite recursion. (An alternative could be to
5332 count the recursion depth of this function and signal an
5333 error when a certain maximum depth is reached.) Is it worth
5334 it? */
5335 if (success_p && it->dpvec == NULL)
5336 {
5337 Lisp_Object dv;
5338
5339 if (it->dp
5340 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5341 VECTORP (dv)))
5342 {
5343 struct Lisp_Vector *v = XVECTOR (dv);
5344
5345 /* Return the first character from the display table
5346 entry, if not empty. If empty, don't display the
5347 current character. */
5348 if (v->size)
5349 {
5350 it->dpvec_char_len = it->len;
5351 it->dpvec = v->contents;
5352 it->dpend = v->contents + v->size;
5353 it->current.dpvec_index = 0;
fe1c2006 5354 it->dpvec_face_id = -1;
064fd8ec 5355 it->saved_face_id = it->face_id;
a6f82cc4 5356 it->method = GET_FROM_DISPLAY_VECTOR;
f323e507 5357 it->ellipsis_p = 0;
7eee47cc
GM
5358 }
5359 else
5360 {
5361 set_iterator_to_next (it, 0);
5f5c8ee5 5362 }
064fd8ec 5363 goto get_next;
5f5c8ee5
GM
5364 }
5365
5366 /* Translate control characters into `\003' or `^C' form.
5367 Control characters coming from a display table entry are
5368 currently not translated because we use IT->dpvec to hold
5369 the translation. This could easily be changed but I
197516c2
KH
5370 don't believe that it is worth doing.
5371
fa831cf8
KH
5372 If it->multibyte_p is nonzero, eight-bit characters and
5373 non-printable multibyte characters are also translated to
5374 octal form.
5375
5376 If it->multibyte_p is zero, eight-bit characters that
5377 don't have corresponding multibyte char code are also
5378 translated to octal form. */
ba197fe6 5379 else if ((it->c < ' '
5f5c8ee5 5380 && (it->area != TEXT_AREA
0b2e177e 5381 /* In mode line, treat \n like other crl chars. */
6407f07b 5382 || (it->c != '\t'
0b2e177e 5383 && it->glyph_row && it->glyph_row->mode_line_p)
c6e89d6c 5384 || (it->c != '\n' && it->c != '\t')))
fa831cf8
KH
5385 || (it->multibyte_p
5386 ? ((it->c >= 127
5387 && it->len == 1)
6df99033 5388 || !CHAR_PRINTABLE_P (it->c)
9c69e6e1 5389 || (!NILP (Vnobreak_char_display)
44e582b8
JL
5390 && (it->c == 0x8a0 || it->c == 0x8ad
5391 || it->c == 0x920 || it->c == 0x92d
5392 || it->c == 0xe20 || it->c == 0xe2d
5393 || it->c == 0xf20 || it->c == 0xf2d)))
ba197fe6 5394 : (it->c >= 127
170b174c
KH
5395 && (!unibyte_display_via_language_environment
5396 || it->c == unibyte_char_to_multibyte (it->c)))))
5f5c8ee5
GM
5397 {
5398 /* IT->c is a control character which must be displayed
5399 either as '\003' or as `^C' where the '\\' and '^'
5400 can be defined in the display table. Fill
5401 IT->ctl_chars with glyphs for what we have to
5402 display. Then, set IT->dpvec to these glyphs. */
5403 GLYPH g;
064fd8ec 5404 int ctl_len;
1868c863 5405 int face_id, lface_id = 0 ;
fe1c2006 5406 GLYPH escape_glyph;
5f5c8ee5 5407
547d2b37
RS
5408 /* Handle control characters with ^. */
5409
54c85a23 5410 if (it->c < 128 && it->ctl_arrow_p)
5f5c8ee5 5411 {
1868c863 5412 g = '^'; /* default glyph for Control */
5f5c8ee5
GM
5413 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5414 if (it->dp
5415 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5416 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
fe1c2006
KS
5417 {
5418 g = XINT (DISP_CTRL_GLYPH (it->dp));
5419 lface_id = FAST_GLYPH_FACE (g);
1868c863
KS
5420 }
5421 if (lface_id)
5422 {
5423 g = FAST_GLYPH_CHAR (g);
5424 face_id = merge_faces (it->f, Qt, lface_id,
5425 it->face_id);
fe1c2006 5426 }
5f5c8ee5 5427 else
fe1c2006
KS
5428 {
5429 /* Merge the escape-glyph face into the current face. */
539f1217
KS
5430 face_id = merge_faces (it->f, Qescape_glyph, 0,
5431 it->face_id);
fe1c2006 5432 }
5f5c8ee5 5433
fe1c2006
KS
5434 XSETINT (it->ctl_chars[0], g);
5435 g = it->c ^ 0100;
5f5c8ee5 5436 XSETINT (it->ctl_chars[1], g);
064fd8ec 5437 ctl_len = 2;
fe1c2006 5438 goto display_control;
5f5c8ee5 5439 }
fe1c2006 5440
547d2b37
RS
5441 /* Handle non-break space in the mode where it only gets
5442 highlighting. */
5443
9c69e6e1 5444 if (EQ (Vnobreak_char_display, Qt)
547d2b37
RS
5445 && (it->c == 0x8a0 || it->c == 0x920
5446 || it->c == 0xe20 || it->c == 0xf20))
5447 {
5448 /* Merge the no-break-space face into the current face. */
9c69e6e1 5449 face_id = merge_faces (it->f, Qnobreak_space, 0,
547d2b37
RS
5450 it->face_id);
5451
5452 g = it->c = ' ';
5453 XSETINT (it->ctl_chars[0], g);
5454 ctl_len = 1;
5455 goto display_control;
5456 }
5457
5458 /* Handle sequences that start with the "escape glyph". */
5459
5460 /* the default escape glyph is \. */
5461 escape_glyph = '\\';
5462
fe1c2006
KS
5463 if (it->dp
5464 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5465 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
6df99033 5466 {
fe1c2006
KS
5467 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5468 lface_id = FAST_GLYPH_FACE (escape_glyph);
1868c863
KS
5469 }
5470 if (lface_id)
5471 {
547d2b37
RS
5472 /* The display table specified a face.
5473 Merge it into face_id and also into escape_glyph. */
1868c863
KS
5474 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5475 face_id = merge_faces (it->f, Qt, lface_id,
5476 it->face_id);
fe1c2006
KS
5477 }
5478 else
5479 {
5480 /* Merge the escape-glyph face into the current face. */
539f1217
KS
5481 face_id = merge_faces (it->f, Qescape_glyph, 0,
5482 it->face_id);
fe1c2006 5483 }
6df99033 5484
547d2b37
RS
5485 /* Handle soft hyphens in the mode where they only get
5486 highlighting. */
44e582b8 5487
9c69e6e1 5488 if (EQ (Vnobreak_char_display, Qt)
547d2b37
RS
5489 && (it->c == 0x8ad || it->c == 0x92d
5490 || it->c == 0xe2d || it->c == 0xf2d))
5491 {
5492 g = it->c = '-';
44e582b8
JL
5493 XSETINT (it->ctl_chars[0], g);
5494 ctl_len = 1;
5495 goto display_control;
5496 }
5497
547d2b37
RS
5498 /* Handle non-break space and soft hyphen
5499 with the escape glyph. */
5500
5501 if (it->c == 0x8a0 || it->c == 0x8ad
5502 || it->c == 0x920 || it->c == 0x92d
5503 || it->c == 0xe20 || it->c == 0xe2d
5504 || it->c == 0xf20 || it->c == 0xf2d)
44e582b8 5505 {
547d2b37
RS
5506 XSETINT (it->ctl_chars[0], escape_glyph);
5507 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5508 XSETINT (it->ctl_chars[1], g);
5509 ctl_len = 2;
fe1c2006 5510 goto display_control;
6df99033 5511 }
197516c2 5512
fe1c2006
KS
5513 {
5514 unsigned char str[MAX_MULTIBYTE_LENGTH];
5515 int len;
5516 int i;
197516c2 5517
fe1c2006
KS
5518 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5519 if (SINGLE_BYTE_CHAR_P (it->c))
5520 str[0] = it->c, len = 1;
5521 else
5522 {
5523 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5524 if (len < 0)
5525 {
5526 /* It's an invalid character, which shouldn't
5527 happen actually, but due to bugs it may
5528 happen. Let's print the char as is, there's
5529 not much meaningful we can do with it. */
ea9dd091
GM
5530 str[0] = it->c;
5531 str[1] = it->c >> 8;
5532 str[2] = it->c >> 16;
5533 str[3] = it->c >> 24;
5534 len = 4;
5535 }
5536 }
c5924f47 5537
fe1c2006
KS
5538 for (i = 0; i < len; i++)
5539 {
5540 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5541 /* Insert three more glyphs into IT->ctl_chars for
5542 the octal display of the character. */
5543 g = ((str[i] >> 6) & 7) + '0';
5544 XSETINT (it->ctl_chars[i * 4 + 1], g);
5545 g = ((str[i] >> 3) & 7) + '0';
5546 XSETINT (it->ctl_chars[i * 4 + 2], g);
5547 g = (str[i] & 7) + '0';
5548 XSETINT (it->ctl_chars[i * 4 + 3], g);
5549 }
5550 ctl_len = len * 4;
5551 }
064fd8ec 5552
fe1c2006 5553 display_control:
064fd8ec
KS
5554 /* Set up IT->dpvec and return first character from it. */
5555 it->dpvec_char_len = it->len;
5556 it->dpvec = it->ctl_chars;
5557 it->dpend = it->dpvec + ctl_len;
5558 it->current.dpvec_index = 0;
fe1c2006 5559 it->dpvec_face_id = face_id;
064fd8ec 5560 it->saved_face_id = it->face_id;
a6f82cc4 5561 it->method = GET_FROM_DISPLAY_VECTOR;
f323e507 5562 it->ellipsis_p = 0;
064fd8ec 5563 goto get_next;
5f5c8ee5
GM
5564 }
5565 }
5566
980806b6
KH
5567 /* Adjust face id for a multibyte character. There are no
5568 multibyte character in unibyte text. */
5f5c8ee5
GM
5569 if (it->multibyte_p
5570 && success_p
980806b6 5571 && FRAME_WINDOW_P (it->f))
5f5c8ee5 5572 {
980806b6
KH
5573 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5574 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5f5c8ee5
GM
5575 }
5576 }
5577
5578 /* Is this character the last one of a run of characters with
5579 box? If yes, set IT->end_of_box_run_p to 1. */
5580 if (it->face_box_p
5581 && it->s == NULL)
5582 {
5583 int face_id;
5584 struct face *face;
5585
5586 it->end_of_box_run_p
5587 = ((face_id = face_after_it_pos (it),
5588 face_id != it->face_id)
5589 && (face = FACE_FROM_ID (it->f, face_id),
5590 face->box == FACE_NO_BOX));
5591 }
5592
5593 /* Value is 0 if end of buffer or string reached. */
5594 return success_p;
5595}
5596
5597
5598/* Move IT to the next display element.
5599
cafafe0b
GM
5600 RESEAT_P non-zero means if called on a newline in buffer text,
5601 skip to the next visible line start.
5602
5f5c8ee5
GM
5603 Functions get_next_display_element and set_iterator_to_next are
5604 separate because I find this arrangement easier to handle than a
5605 get_next_display_element function that also increments IT's
5606 position. The way it is we can first look at an iterator's current
5607 display element, decide whether it fits on a line, and if it does,
5608 increment the iterator position. The other way around we probably
5609 would either need a flag indicating whether the iterator has to be
5610 incremented the next time, or we would have to implement a
5611 decrement position function which would not be easy to write. */
5612
5613void
cafafe0b 5614set_iterator_to_next (it, reseat_p)
5f5c8ee5 5615 struct it *it;
cafafe0b 5616 int reseat_p;
5f5c8ee5 5617{
483de32b
GM
5618 /* Reset flags indicating start and end of a sequence of characters
5619 with box. Reset them at the start of this function because
5620 moving the iterator to a new position might set them. */
5621 it->start_of_box_run_p = it->end_of_box_run_p = 0;
2311178e 5622
a6f82cc4 5623 switch (it->method)
5f5c8ee5 5624 {
a6f82cc4 5625 case GET_FROM_BUFFER:
5f5c8ee5
GM
5626 /* The current display element of IT is a character from
5627 current_buffer. Advance in the buffer, and maybe skip over
5628 invisible lines that are so because of selective display. */
cafafe0b 5629 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
312246d1 5630 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
5631 else
5632 {
5633 xassert (it->len != 0);
5634 IT_BYTEPOS (*it) += it->len;
5635 IT_CHARPOS (*it) += 1;
5636 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5637 }
a6f82cc4
KS
5638 break;
5639
5640 case GET_FROM_COMPOSITION:
5641 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
260a86a0
KH
5642 if (STRINGP (it->string))
5643 {
5644 IT_STRING_BYTEPOS (*it) += it->len;
5645 IT_STRING_CHARPOS (*it) += it->cmp_len;
a6f82cc4 5646 it->method = GET_FROM_STRING;
260a86a0
KH
5647 goto consider_string_end;
5648 }
5649 else
5650 {
5651 IT_BYTEPOS (*it) += it->len;
5652 IT_CHARPOS (*it) += it->cmp_len;
a6f82cc4 5653 it->method = GET_FROM_BUFFER;
260a86a0 5654 }
a6f82cc4
KS
5655 break;
5656
5657 case GET_FROM_C_STRING:
5f5c8ee5
GM
5658 /* Current display element of IT is from a C string. */
5659 IT_BYTEPOS (*it) += it->len;
5660 IT_CHARPOS (*it) += 1;
a6f82cc4
KS
5661 break;
5662
5663 case GET_FROM_DISPLAY_VECTOR:
5f5c8ee5
GM
5664 /* Current display element of IT is from a display table entry.
5665 Advance in the display table definition. Reset it to null if
5666 end reached, and continue with characters from buffers/
5667 strings. */
5668 ++it->current.dpvec_index;
286bcbc9 5669
980806b6
KH
5670 /* Restore face of the iterator to what they were before the
5671 display vector entry (these entries may contain faces). */
5f5c8ee5 5672 it->face_id = it->saved_face_id;
2311178e 5673
5f5c8ee5
GM
5674 if (it->dpvec + it->current.dpvec_index == it->dpend)
5675 {
5676 if (it->s)
a6f82cc4 5677 it->method = GET_FROM_C_STRING;
5f5c8ee5 5678 else if (STRINGP (it->string))
a6f82cc4 5679 it->method = GET_FROM_STRING;
5f5c8ee5 5680 else
a6f82cc4 5681 it->method = GET_FROM_BUFFER;
5f5c8ee5
GM
5682
5683 it->dpvec = NULL;
5684 it->current.dpvec_index = -1;
5685
312246d1
GM
5686 /* Skip over characters which were displayed via IT->dpvec. */
5687 if (it->dpvec_char_len < 0)
5688 reseat_at_next_visible_line_start (it, 1);
5689 else if (it->dpvec_char_len > 0)
5f5c8ee5 5690 {
310316cf
KS
5691 if (it->method == GET_FROM_STRING
5692 && it->n_overlay_strings > 0)
5693 it->ignore_overlay_strings_at_pos_p = 1;
5f5c8ee5 5694 it->len = it->dpvec_char_len;
cafafe0b 5695 set_iterator_to_next (it, reseat_p);
5f5c8ee5 5696 }
60227bf4
KS
5697
5698 /* Recheck faces after display vector */
5699 it->stop_charpos = IT_CHARPOS (*it);
5f5c8ee5 5700 }
a6f82cc4
KS
5701 break;
5702
5703 case GET_FROM_STRING:
5f5c8ee5
GM
5704 /* Current display element is a character from a Lisp string. */
5705 xassert (it->s == NULL && STRINGP (it->string));
5706 IT_STRING_BYTEPOS (*it) += it->len;
5707 IT_STRING_CHARPOS (*it) += 1;
2311178e 5708
5f5c8ee5
GM
5709 consider_string_end:
5710
5711 if (it->current.overlay_string_index >= 0)
5712 {
5713 /* IT->string is an overlay string. Advance to the
5714 next, if there is one. */
2051c264 5715 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5f5c8ee5
GM
5716 next_overlay_string (it);
5717 }
5718 else
5719 {
5720 /* IT->string is not an overlay string. If we reached
5721 its end, and there is something on IT->stack, proceed
5722 with what is on the stack. This can be either another
5723 string, this time an overlay string, or a buffer. */
2051c264 5724 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5f5c8ee5
GM
5725 && it->sp > 0)
5726 {
5727 pop_it (it);
a6f82cc4 5728 if (STRINGP (it->string))
b2046df8 5729 goto consider_string_end;
a6f82cc4 5730 it->method = GET_FROM_BUFFER;
5f5c8ee5
GM
5731 }
5732 }
a6f82cc4
KS
5733 break;
5734
5735 case GET_FROM_IMAGE:
5736 case GET_FROM_STRETCH:
5f5c8ee5
GM
5737 /* The position etc with which we have to proceed are on
5738 the stack. The position may be at the end of a string,
5739 if the `display' property takes up the whole string. */
a6f82cc4 5740 xassert (it->sp > 0);
5f5c8ee5
GM
5741 pop_it (it);
5742 it->image_id = 0;
5743 if (STRINGP (it->string))
5744 {
a6f82cc4 5745 it->method = GET_FROM_STRING;
5f5c8ee5
GM
5746 goto consider_string_end;
5747 }
a6f82cc4
KS
5748 it->method = GET_FROM_BUFFER;
5749 break;
5750
5751 default:
5752 /* There are no other methods defined, so this should be a bug. */
5753 abort ();
5f5c8ee5 5754 }
5f5c8ee5 5755
a6f82cc4 5756 xassert (it->method != GET_FROM_STRING
5f5c8ee5
GM
5757 || (STRINGP (it->string)
5758 && IT_STRING_CHARPOS (*it) >= 0));
5759}
5760
5f5c8ee5
GM
5761/* Load IT's display element fields with information about the next
5762 display element which comes from a display table entry or from the
5763 result of translating a control character to one of the forms `^C'
064fd8ec
KS
5764 or `\003'.
5765
5766 IT->dpvec holds the glyphs to return as characters.
5767 IT->saved_face_id holds the face id before the display vector--
5768 it is restored into IT->face_idin set_iterator_to_next. */
5f5c8ee5
GM
5769
5770static int
5771next_element_from_display_vector (it)
5772 struct it *it;
5773{
5774 /* Precondition. */
5775 xassert (it->dpvec && it->current.dpvec_index >= 0);
5776
62af9c24
KS
5777 it->face_id = it->saved_face_id;
5778
5f5c8ee5
GM
5779 if (INTEGERP (*it->dpvec)
5780 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5781 {
5f5c8ee5
GM
5782 GLYPH g;
5783
5784 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5785 it->c = FAST_GLYPH_CHAR (g);
c5924f47 5786 it->len = CHAR_BYTES (it->c);
5f5c8ee5
GM
5787
5788 /* The entry may contain a face id to use. Such a face id is
5789 the id of a Lisp face, not a realized face. A face id of
969065c3 5790 zero means no face is specified. */
fe1c2006
KS
5791 if (it->dpvec_face_id >= 0)
5792 it->face_id = it->dpvec_face_id;
5793 else
5f5c8ee5 5794 {
fe1c2006 5795 int lface_id = FAST_GLYPH_FACE (g);
539f1217
KS
5796 if (lface_id > 0)
5797 it->face_id = merge_faces (it->f, Qt, lface_id,
5798 it->saved_face_id);
5f5c8ee5
GM
5799 }
5800 }
5801 else
5802 /* Display table entry is invalid. Return a space. */
5803 it->c = ' ', it->len = 1;
5804
5805 /* Don't change position and object of the iterator here. They are
5806 still the values of the character that had this display table
5807 entry or was translated, and that's what we want. */
5808 it->what = IT_CHARACTER;
5809 return 1;
5810}
5811
5812
5813/* Load IT with the next display element from Lisp string IT->string.
5814 IT->current.string_pos is the current position within the string.
5815 If IT->current.overlay_string_index >= 0, the Lisp string is an
5816 overlay string. */
5817
5818static int
5819next_element_from_string (it)
5820 struct it *it;
5821{
5822 struct text_pos position;
5823
5824 xassert (STRINGP (it->string));
5825 xassert (IT_STRING_CHARPOS (*it) >= 0);
5826 position = it->current.string_pos;
5827
5828 /* Time to check for invisible text? */
5829 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5830 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5831 {
5832 handle_stop (it);
5833
5834 /* Since a handler may have changed IT->method, we must
5835 recurse here. */
5836 return get_next_display_element (it);
5837 }
5838
5839 if (it->current.overlay_string_index >= 0)
5840 {
5841 /* Get the next character from an overlay string. In overlay
5842 strings, There is no field width or padding with spaces to
5843 do. */
2051c264 5844 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5f5c8ee5
GM
5845 {
5846 it->what = IT_EOB;
5847 return 0;
5848 }
5849 else if (STRING_MULTIBYTE (it->string))
5850 {
2051c264 5851 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
50f80c2f
KR
5852 const unsigned char *s = (SDATA (it->string)
5853 + IT_STRING_BYTEPOS (*it));
4fdb80f2 5854 it->c = string_char_and_length (s, remaining, &it->len);
5f5c8ee5
GM
5855 }
5856 else
5857 {
2051c264 5858 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5f5c8ee5
GM
5859 it->len = 1;
5860 }
5861 }
5862 else
5863 {
5864 /* Get the next character from a Lisp string that is not an
5865 overlay string. Such strings come from the mode line, for
5866 example. We may have to pad with spaces, or truncate the
5867 string. See also next_element_from_c_string. */
5868 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5869 {
5870 it->what = IT_EOB;
5871 return 0;
5872 }
5873 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5874 {
5875 /* Pad with spaces. */
5876 it->c = ' ', it->len = 1;
5877 CHARPOS (position) = BYTEPOS (position) = -1;
5878 }
5879 else if (STRING_MULTIBYTE (it->string))
5880 {
2051c264 5881 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
50f80c2f
KR
5882 const unsigned char *s = (SDATA (it->string)
5883 + IT_STRING_BYTEPOS (*it));
4fdb80f2 5884 it->c = string_char_and_length (s, maxlen, &it->len);
5f5c8ee5
GM
5885 }
5886 else
5887 {
2051c264 5888 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5f5c8ee5
GM
5889 it->len = 1;
5890 }
5891 }
5892
5893 /* Record what we have and where it came from. Note that we store a
5894 buffer position in IT->position although it could arguably be a
5895 string position. */
5896 it->what = IT_CHARACTER;
5897 it->object = it->string;
5898 it->position = position;
5899 return 1;
5900}
5901
5902
5903/* Load IT with next display element from C string IT->s.
5904 IT->string_nchars is the maximum number of characters to return
5905 from the string. IT->end_charpos may be greater than
5906 IT->string_nchars when this function is called, in which case we
5907 may have to return padding spaces. Value is zero if end of string
5908 reached, including padding spaces. */
5909
5910static int
5911next_element_from_c_string (it)
5912 struct it *it;
5913{
5914 int success_p = 1;
2311178e 5915
5f5c8ee5
GM
5916 xassert (it->s);
5917 it->what = IT_CHARACTER;
5918 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5919 it->object = Qnil;
2311178e 5920
5f5c8ee5
GM
5921 /* IT's position can be greater IT->string_nchars in case a field
5922 width or precision has been specified when the iterator was
5923 initialized. */
5924 if (IT_CHARPOS (*it) >= it->end_charpos)
5925 {
5926 /* End of the game. */
5927 it->what = IT_EOB;
5928 success_p = 0;
5929 }
5930 else if (IT_CHARPOS (*it) >= it->string_nchars)
5931 {
5932 /* Pad with spaces. */
5933 it->c = ' ', it->len = 1;
5934 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5935 }
5936 else if (it->multibyte_p)
5937 {
5938 /* Implementation note: The calls to strlen apparently aren't a
5939 performance problem because there is no noticeable performance
5940 difference between Emacs running in unibyte or multibyte mode. */
5941 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4fdb80f2
GM
5942 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5943 maxlen, &it->len);
5f5c8ee5
GM
5944 }
5945 else
5946 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
2311178e 5947
5f5c8ee5
GM
5948 return success_p;
5949}
5950
5951
5952/* Set up IT to return characters from an ellipsis, if appropriate.
5953 The definition of the ellipsis glyphs may come from a display table
5954 entry. This function Fills IT with the first glyph from the
5955 ellipsis if an ellipsis is to be displayed. */
5956
13f19968 5957static int
5f5c8ee5
GM
5958next_element_from_ellipsis (it)
5959 struct it *it;
5960{
13f19968 5961 if (it->selective_display_ellipsis_p)
064fd8ec 5962 setup_for_ellipsis (it, it->len);
13f19968 5963 else
54918e2b 5964 {
4aad61f8
GM
5965 /* The face at the current position may be different from the
5966 face we find after the invisible text. Remember what it
5967 was in IT->saved_face_id, and signal that it's there by
5968 setting face_before_selective_p. */
5969 it->saved_face_id = it->face_id;
a6f82cc4 5970 it->method = GET_FROM_BUFFER;
54918e2b 5971 reseat_at_next_visible_line_start (it, 1);
4aad61f8 5972 it->face_before_selective_p = 1;
54918e2b 5973 }
2311178e 5974
13f19968 5975 return get_next_display_element (it);
5f5c8ee5
GM
5976}
5977
5978
5979/* Deliver an image display element. The iterator IT is already
5980 filled with image information (done in handle_display_prop). Value
5981 is always 1. */
2311178e 5982
5f5c8ee5
GM
5983
5984static int
5985next_element_from_image (it)
5986 struct it *it;
5987{
5988 it->what = IT_IMAGE;
5989 return 1;
5990}
5991
5992
5993/* Fill iterator IT with next display element from a stretch glyph
5994 property. IT->object is the value of the text property. Value is
5995 always 1. */
5996
5997static int
5998next_element_from_stretch (it)
5999 struct it *it;
6000{
6001 it->what = IT_STRETCH;
6002 return 1;
6003}
6004
6005
6006/* Load IT with the next display element from current_buffer. Value
6007 is zero if end of buffer reached. IT->stop_charpos is the next
6008 position at which to stop and check for text properties or buffer
6009 end. */
6010
6011static int
6012next_element_from_buffer (it)
6013 struct it *it;
6014{
6015 int success_p = 1;
6016
6017 /* Check this assumption, otherwise, we would never enter the
6018 if-statement, below. */
6019 xassert (IT_CHARPOS (*it) >= BEGV
6020 && IT_CHARPOS (*it) <= it->stop_charpos);
6021
6022 if (IT_CHARPOS (*it) >= it->stop_charpos)
6023 {
6024 if (IT_CHARPOS (*it) >= it->end_charpos)
6025 {
6026 int overlay_strings_follow_p;
2311178e 6027
5f5c8ee5
GM
6028 /* End of the game, except when overlay strings follow that
6029 haven't been returned yet. */
6030 if (it->overlay_strings_at_end_processed_p)
6031 overlay_strings_follow_p = 0;
6032 else
6033 {
6034 it->overlay_strings_at_end_processed_p = 1;
5a08cbaf 6035 overlay_strings_follow_p = get_overlay_strings (it, 0);
5f5c8ee5
GM
6036 }
6037
6038 if (overlay_strings_follow_p)
6039 success_p = get_next_display_element (it);
6040 else
6041 {
6042 it->what = IT_EOB;
6043 it->position = it->current.pos;
6044 success_p = 0;
6045 }
6046 }
6047 else
6048 {
6049 handle_stop (it);
6050 return get_next_display_element (it);
6051 }
6052 }
6053 else
6054 {
6055 /* No face changes, overlays etc. in sight, so just return a
6056 character from current_buffer. */
6057 unsigned char *p;
6058
6059 /* Maybe run the redisplay end trigger hook. Performance note:
6060 This doesn't seem to cost measurable time. */
6061 if (it->redisplay_end_trigger_charpos
6062 && it->glyph_row
6063 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6064 run_redisplay_end_trigger_hook (it);
6065
6066 /* Get the next character, maybe multibyte. */
6067 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
260a86a0 6068 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5f5c8ee5
GM
6069 {
6070 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6071 - IT_BYTEPOS (*it));
4fdb80f2 6072 it->c = string_char_and_length (p, maxlen, &it->len);
5f5c8ee5
GM
6073 }
6074 else
6075 it->c = *p, it->len = 1;
6076
6077 /* Record what we have and where it came from. */
6078 it->what = IT_CHARACTER;;
6079 it->object = it->w->buffer;
6080 it->position = it->current.pos;
6081
6082 /* Normally we return the character found above, except when we
6083 really want to return an ellipsis for selective display. */
6084 if (it->selective)
6085 {
6086 if (it->c == '\n')
6087 {
6088 /* A value of selective > 0 means hide lines indented more
6089 than that number of columns. */
6090 if (it->selective > 0
6091 && IT_CHARPOS (*it) + 1 < ZV
6092 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6093 IT_BYTEPOS (*it) + 1,
a67e162b 6094 (double) it->selective)) /* iftc */
312246d1 6095 {
13f19968 6096 success_p = next_element_from_ellipsis (it);
312246d1
GM
6097 it->dpvec_char_len = -1;
6098 }
5f5c8ee5
GM
6099 }
6100 else if (it->c == '\r' && it->selective == -1)
6101 {
6102 /* A value of selective == -1 means that everything from the
6103 CR to the end of the line is invisible, with maybe an
6104 ellipsis displayed for it. */
13f19968 6105 success_p = next_element_from_ellipsis (it);
312246d1 6106 it->dpvec_char_len = -1;
5f5c8ee5
GM
6107 }
6108 }
6109 }
6110
6111 /* Value is zero if end of buffer reached. */
c880678e 6112 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5f5c8ee5
GM
6113 return success_p;
6114}
6115
2311178e 6116
5f5c8ee5
GM
6117/* Run the redisplay end trigger hook for IT. */
6118
6119static void
6120run_redisplay_end_trigger_hook (it)
6121 struct it *it;
6122{
6123 Lisp_Object args[3];
6124
6125 /* IT->glyph_row should be non-null, i.e. we should be actually
6126 displaying something, or otherwise we should not run the hook. */
6127 xassert (it->glyph_row);
6128
6129 /* Set up hook arguments. */
6130 args[0] = Qredisplay_end_trigger_functions;
6131 args[1] = it->window;
6132 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6133 it->redisplay_end_trigger_charpos = 0;
6134
6135 /* Since we are *trying* to run these functions, don't try to run
6136 them again, even if they get an error. */
6137 it->w->redisplay_end_trigger = Qnil;
6138 Frun_hook_with_args (3, args);
2311178e 6139
5f5c8ee5
GM
6140 /* Notice if it changed the face of the character we are on. */
6141 handle_face_prop (it);
6142}
6143
6144
260a86a0
KH
6145/* Deliver a composition display element. The iterator IT is already
6146 filled with composition information (done in
6147 handle_composition_prop). Value is always 1. */
6148
6149static int
6150next_element_from_composition (it)
6151 struct it *it;
6152{
6153 it->what = IT_COMPOSITION;
6154 it->position = (STRINGP (it->string)
6155 ? it->current.string_pos
6156 : it->current.pos);
6157 return 1;
6158}
6159
6160
5f5c8ee5
GM
6161\f
6162/***********************************************************************
6163 Moving an iterator without producing glyphs
6164 ***********************************************************************/
6165
367d949f
KS
6166/* Check if iterator is at a position corresponding to a valid buffer
6167 position after some move_it_ call. */
6168
6169#define IT_POS_VALID_AFTER_MOVE_P(it) \
6170 ((it)->method == GET_FROM_STRING \
6171 ? IT_STRING_CHARPOS (*it) == 0 \
6172 : 1)
6173
6174
5f5c8ee5
GM
6175/* Move iterator IT to a specified buffer or X position within one
6176 line on the display without producing glyphs.
6177
c53a1624
RS
6178 OP should be a bit mask including some or all of these bits:
6179 MOVE_TO_X: Stop on reaching x-position TO_X.
6180 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6181 Regardless of OP's value, stop in reaching the end of the display line.
5f5c8ee5 6182
c53a1624
RS
6183 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6184 This means, in particular, that TO_X includes window's horizontal
6185 scroll amount.
5f5c8ee5 6186
c53a1624
RS
6187 The return value has several possible values that
6188 say what condition caused the scan to stop:
5f5c8ee5
GM
6189
6190 MOVE_POS_MATCH_OR_ZV
6191 - when TO_POS or ZV was reached.
2311178e 6192
5f5c8ee5
GM
6193 MOVE_X_REACHED
6194 -when TO_X was reached before TO_POS or ZV were reached.
2311178e 6195
5f5c8ee5
GM
6196 MOVE_LINE_CONTINUED
6197 - when we reached the end of the display area and the line must
6198 be continued.
2311178e 6199
5f5c8ee5
GM
6200 MOVE_LINE_TRUNCATED
6201 - when we reached the end of the display area and the line is
6202 truncated.
6203
6204 MOVE_NEWLINE_OR_CR
6205 - when we stopped at a line end, i.e. a newline or a CR and selective
6206 display is on. */
6207
701552dd 6208static enum move_it_result
5f5c8ee5
GM
6209move_it_in_display_line_to (it, to_charpos, to_x, op)
6210 struct it *it;
6211 int to_charpos, to_x, op;
6212{
6213 enum move_it_result result = MOVE_UNDEFINED;
6214 struct glyph_row *saved_glyph_row;
6215
6216 /* Don't produce glyphs in produce_glyphs. */
6217 saved_glyph_row = it->glyph_row;
6218 it->glyph_row = NULL;
6219
d02d0705
KS
6220#define BUFFER_POS_REACHED_P() \
6221 ((op & MOVE_TO_POS) != 0 \
6222 && BUFFERP (it->object) \
6223 && IT_CHARPOS (*it) >= to_charpos \
e6d09683
RS
6224 && (it->method == GET_FROM_BUFFER \
6225 || (it->method == GET_FROM_DISPLAY_VECTOR \
6226 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
d02d0705 6227
4540d74c 6228
5f5c8ee5
GM
6229 while (1)
6230 {
ae26e27d 6231 int x, i, ascent = 0, descent = 0;
2311178e 6232
20b44bb8
KS
6233 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6234 if ((op & MOVE_TO_POS) != 0
6235 && BUFFERP (it->object)
5fd5f17c 6236 && it->method == GET_FROM_BUFFER
20b44bb8
KS
6237 && IT_CHARPOS (*it) > to_charpos)
6238 {
6239 result = MOVE_POS_MATCH_OR_ZV;
6240 break;
6241 }
6242
35531849
KS
6243 /* Stop when ZV reached.
6244 We used to stop here when TO_CHARPOS reached as well, but that is
6245 too soon if this glyph does not fit on this line. So we handle it
6246 explicitly below. */
5f5c8ee5 6247 if (!get_next_display_element (it)
35531849
KS
6248 || (it->truncate_lines_p
6249 && BUFFER_POS_REACHED_P ()))
5f5c8ee5
GM
6250 {
6251 result = MOVE_POS_MATCH_OR_ZV;
6252 break;
6253 }
2311178e 6254
5f5c8ee5
GM
6255 /* The call to produce_glyphs will get the metrics of the
6256 display element IT is loaded with. We record in x the
6257 x-position before this display element in case it does not
6258 fit on the line. */
6259 x = it->current_x;
2311178e 6260
47589c8c
GM
6261 /* Remember the line height so far in case the next element doesn't
6262 fit on the line. */
6263 if (!it->truncate_lines_p)
6264 {
6265 ascent = it->max_ascent;
6266 descent = it->max_descent;
6267 }
2311178e 6268
5f5c8ee5
GM
6269 PRODUCE_GLYPHS (it);
6270
6271 if (it->area != TEXT_AREA)
6272 {
cafafe0b 6273 set_iterator_to_next (it, 1);
5f5c8ee5
GM
6274 continue;
6275 }
6276
6277 /* The number of glyphs we get back in IT->nglyphs will normally
6278 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6279 character on a terminal frame, or (iii) a line end. For the
6280 second case, IT->nglyphs - 1 padding glyphs will be present
6281 (on X frames, there is only one glyph produced for a
6282 composite character.
6283
6284 The behavior implemented below means, for continuation lines,
6285 that as many spaces of a TAB as fit on the current line are
6286 displayed there. For terminal frames, as many glyphs of a
6287 multi-glyph character are displayed in the current line, too.
6288 This is what the old redisplay code did, and we keep it that
6289 way. Under X, the whole shape of a complex character must
6290 fit on the line or it will be completely displayed in the
6291 next line.
6292
6293 Note that both for tabs and padding glyphs, all glyphs have
6294 the same width. */
6295 if (it->nglyphs)
6296 {
6297 /* More than one glyph or glyph doesn't fit on line. All
6298 glyphs have the same width. */
6299 int single_glyph_width = it->pixel_width / it->nglyphs;
6300 int new_x;
366717cf
RS
6301 int x_before_this_char = x;
6302 int hpos_before_this_char = it->hpos;
2311178e 6303
5f5c8ee5
GM
6304 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6305 {
6306 new_x = x + single_glyph_width;
6307
6308 /* We want to leave anything reaching TO_X to the caller. */
6309 if ((op & MOVE_TO_X) && new_x > to_x)
6310 {
35531849
KS
6311 if (BUFFER_POS_REACHED_P ())
6312 goto buffer_pos_reached;
5f5c8ee5
GM
6313 it->current_x = x;
6314 result = MOVE_X_REACHED;
6315 break;
6316 }
6317 else if (/* Lines are continued. */
6318 !it->truncate_lines_p
6319 && (/* And glyph doesn't fit on the line. */
6320 new_x > it->last_visible_x
6321 /* Or it fits exactly and we're on a window
6322 system frame. */
6323 || (new_x == it->last_visible_x
6324 && FRAME_WINDOW_P (it->f))))
6325 {
6326 if (/* IT->hpos == 0 means the very first glyph
6327 doesn't fit on the line, e.g. a wide image. */
6328 it->hpos == 0
6329 || (new_x == it->last_visible_x
6330 && FRAME_WINDOW_P (it->f)))
6331 {
6332 ++it->hpos;
6333 it->current_x = new_x;
366717cf
RS
6334
6335 /* The character's last glyph just barely fits
6336 in this row. */
5f5c8ee5 6337 if (i == it->nglyphs - 1)
88e6b646 6338 {
366717cf
RS
6339 /* If this is the destination position,
6340 return a position *before* it in this row,
6341 now that we know it fits in this row. */
6342 if (BUFFER_POS_REACHED_P ())
6343 {
6344 it->hpos = hpos_before_this_char;
6345 it->current_x = x_before_this_char;
6346 result = MOVE_POS_MATCH_OR_ZV;
6347 break;
6348 }
6349
88e6b646 6350 set_iterator_to_next (it, 1);
7af0e8d7 6351#ifdef HAVE_WINDOW_SYSTEM
88e6b646
KS
6352 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6353 {
f516e857 6354 if (!get_next_display_element (it))
6d925726
KS
6355 {
6356 result = MOVE_POS_MATCH_OR_ZV;
6357 break;
6358 }
f516e857
KS
6359 if (BUFFER_POS_REACHED_P ())
6360 {
6361 if (ITERATOR_AT_END_OF_LINE_P (it))
6362 result = MOVE_POS_MATCH_OR_ZV;
6363 else
6364 result = MOVE_LINE_CONTINUED;
6365 break;
6366 }
88e6b646
KS
6367 if (ITERATOR_AT_END_OF_LINE_P (it))
6368 {
6369 result = MOVE_NEWLINE_OR_CR;
6370 break;
6371 }
6372 }
7af0e8d7 6373#endif /* HAVE_WINDOW_SYSTEM */
88e6b646 6374 }
5f5c8ee5
GM
6375 }
6376 else
47589c8c
GM
6377 {
6378 it->current_x = x;
6379 it->max_ascent = ascent;
6380 it->max_descent = descent;
6381 }
2311178e 6382
47589c8c
GM
6383 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6384 IT_CHARPOS (*it)));
5f5c8ee5
GM
6385 result = MOVE_LINE_CONTINUED;
6386 break;
6387 }
35531849
KS
6388 else if (BUFFER_POS_REACHED_P ())
6389 goto buffer_pos_reached;
5f5c8ee5
GM
6390 else if (new_x > it->first_visible_x)
6391 {
6392 /* Glyph is visible. Increment number of glyphs that
6393 would be displayed. */
6394 ++it->hpos;
6395 }
6396 else
6397 {
2311178e 6398 /* Glyph is completely off the left margin of the display
5f5c8ee5
GM
6399 area. Nothing to do. */
6400 }
6401 }
6402
6403 if (result != MOVE_UNDEFINED)
6404 break;
6405 }
35531849
KS
6406 else if (BUFFER_POS_REACHED_P ())
6407 {
6408 buffer_pos_reached:
6409 it->current_x = x;
6410 it->max_ascent = ascent;
6411 it->max_descent = descent;
6412 result = MOVE_POS_MATCH_OR_ZV;
6413 break;
6414 }
5f5c8ee5
GM
6415 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6416 {
6417 /* Stop when TO_X specified and reached. This check is
6418 necessary here because of lines consisting of a line end,
6419 only. The line end will not produce any glyphs and we
6420 would never get MOVE_X_REACHED. */
6421 xassert (it->nglyphs == 0);
6422 result = MOVE_X_REACHED;
6423 break;
6424 }
2311178e 6425
5f5c8ee5
GM
6426 /* Is this a line end? If yes, we're done. */
6427 if (ITERATOR_AT_END_OF_LINE_P (it))
6428 {
6429 result = MOVE_NEWLINE_OR_CR;
6430 break;
6431 }
2311178e 6432
5f5c8ee5
GM
6433 /* The current display element has been consumed. Advance
6434 to the next. */
cafafe0b 6435 set_iterator_to_next (it, 1);
2311178e 6436
5f5c8ee5
GM
6437 /* Stop if lines are truncated and IT's current x-position is
6438 past the right edge of the window now. */
6439 if (it->truncate_lines_p
6440 && it->current_x >= it->last_visible_x)
6441 {
7af0e8d7 6442#ifdef HAVE_WINDOW_SYSTEM
88e6b646
KS
6443 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6444 {
4540d74c
KS
6445 if (!get_next_display_element (it)
6446 || BUFFER_POS_REACHED_P ())
6d925726
KS
6447 {
6448 result = MOVE_POS_MATCH_OR_ZV;
6449 break;
6450 }
88e6b646
KS
6451 if (ITERATOR_AT_END_OF_LINE_P (it))
6452 {
6453 result = MOVE_NEWLINE_OR_CR;
6454 break;
6455 }
6456 }
7af0e8d7 6457#endif /* HAVE_WINDOW_SYSTEM */
5f5c8ee5
GM
6458 result = MOVE_LINE_TRUNCATED;
6459 break;
6460 }
6461 }
6462
4540d74c
KS
6463#undef BUFFER_POS_REACHED_P
6464
5f5c8ee5
GM
6465 /* Restore the iterator settings altered at the beginning of this
6466 function. */
6467 it->glyph_row = saved_glyph_row;
6468 return result;
6469}
6470
6471
9b2bba76
RS
6472/* Move IT forward until it satisfies one or more of the criteria in
6473 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6474
6475 OP is a bit-mask that specifies where to stop, and in particular,
6476 which of those four position arguments makes a difference. See the
6477 description of enum move_operation_enum.
2311178e 6478
5f5c8ee5
GM
6479 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6480 screen line, this function will set IT to the next position >
6481 TO_CHARPOS. */
6482
6483void
6484move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6485 struct it *it;
6486 int to_charpos, to_x, to_y, to_vpos;
6487 int op;
6488{
6489 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6490 int line_height;
47589c8c 6491 int reached = 0;
5f5c8ee5 6492
47589c8c 6493 for (;;)
5f5c8ee5
GM
6494 {
6495 if (op & MOVE_TO_VPOS)
6496 {
6497 /* If no TO_CHARPOS and no TO_X specified, stop at the
6498 start of the line TO_VPOS. */
6499 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6500 {
6501 if (it->vpos == to_vpos)
47589c8c
GM
6502 {
6503 reached = 1;
6504 break;
6505 }
6506 else
6507 skip = move_it_in_display_line_to (it, -1, -1, 0);
5f5c8ee5
GM
6508 }
6509 else
6510 {
6511 /* TO_VPOS >= 0 means stop at TO_X in the line at
6512 TO_VPOS, or at TO_POS, whichever comes first. */
47589c8c
GM
6513 if (it->vpos == to_vpos)
6514 {
6515 reached = 2;
6516 break;
6517 }
2311178e 6518
5f5c8ee5
GM
6519 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6520
6521 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
47589c8c
GM
6522 {
6523 reached = 3;
6524 break;
6525 }
5f5c8ee5
GM
6526 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6527 {
6528 /* We have reached TO_X but not in the line we want. */
6529 skip = move_it_in_display_line_to (it, to_charpos,
6530 -1, MOVE_TO_POS);
6531 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c
GM
6532 {
6533 reached = 4;
6534 break;
6535 }
5f5c8ee5
GM
6536 }
6537 }
6538 }
6539 else if (op & MOVE_TO_Y)
6540 {
6541 struct it it_backup;
2311178e 6542
5f5c8ee5
GM
6543 /* TO_Y specified means stop at TO_X in the line containing
6544 TO_Y---or at TO_CHARPOS if this is reached first. The
6545 problem is that we can't really tell whether the line
6546 contains TO_Y before we have completely scanned it, and
6547 this may skip past TO_X. What we do is to first scan to
6548 TO_X.
6549
6550 If TO_X is not specified, use a TO_X of zero. The reason
6551 is to make the outcome of this function more predictable.
6552 If we didn't use TO_X == 0, we would stop at the end of
6553 the line which is probably not what a caller would expect
6554 to happen. */
6555 skip = move_it_in_display_line_to (it, to_charpos,
6556 ((op & MOVE_TO_X)
6557 ? to_x : 0),
6558 (MOVE_TO_X
6559 | (op & MOVE_TO_POS)));
6560
6561 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6562 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c
GM
6563 {
6564 reached = 5;
6565 break;
6566 }
2311178e 6567
5f5c8ee5
GM
6568 /* If TO_X was reached, we would like to know whether TO_Y
6569 is in the line. This can only be said if we know the
6570 total line height which requires us to scan the rest of
6571 the line. */
5f5c8ee5
GM
6572 if (skip == MOVE_X_REACHED)
6573 {
6574 it_backup = *it;
47589c8c 6575 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
6576 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6577 op & MOVE_TO_POS);
47589c8c 6578 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
6579 }
6580
6581 /* Now, decide whether TO_Y is in this line. */
6582 line_height = it->max_ascent + it->max_descent;
47589c8c 6583 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
2311178e 6584
5f5c8ee5
GM
6585 if (to_y >= it->current_y
6586 && to_y < it->current_y + line_height)
6587 {
6588 if (skip == MOVE_X_REACHED)
6589 /* If TO_Y is in this line and TO_X was reached above,
6590 we scanned too far. We have to restore IT's settings
6591 to the ones before skipping. */
6592 *it = it_backup;
47589c8c 6593 reached = 6;
5f5c8ee5
GM
6594 }
6595 else if (skip == MOVE_X_REACHED)
6596 {
6597 skip = skip2;
6598 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c 6599 reached = 7;
5f5c8ee5
GM
6600 }
6601
47589c8c 6602 if (reached)
5f5c8ee5
GM
6603 break;
6604 }
6605 else
6606 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6607
6608 switch (skip)
6609 {
6610 case MOVE_POS_MATCH_OR_ZV:
47589c8c
GM
6611 reached = 8;
6612 goto out;
5f5c8ee5
GM
6613
6614 case MOVE_NEWLINE_OR_CR:
cafafe0b 6615 set_iterator_to_next (it, 1);
5f5c8ee5
GM
6616 it->continuation_lines_width = 0;
6617 break;
6618
6619 case MOVE_LINE_TRUNCATED:
6620 it->continuation_lines_width = 0;
312246d1 6621 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
6622 if ((op & MOVE_TO_POS) != 0
6623 && IT_CHARPOS (*it) > to_charpos)
47589c8c
GM
6624 {
6625 reached = 9;
6626 goto out;
6627 }
5f5c8ee5
GM
6628 break;
6629
6630 case MOVE_LINE_CONTINUED:
6631 it->continuation_lines_width += it->current_x;
6632 break;
6633
6634 default:
6635 abort ();
6636 }
6637
6638 /* Reset/increment for the next run. */
6639 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6640 it->current_x = it->hpos = 0;
6641 it->current_y += it->max_ascent + it->max_descent;
6642 ++it->vpos;
6643 last_height = it->max_ascent + it->max_descent;
6644 last_max_ascent = it->max_ascent;
6645 it->max_ascent = it->max_descent = 0;
6646 }
2311178e 6647
47589c8c
GM
6648 out:
6649
6650 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5f5c8ee5
GM
6651}
6652
6653
6654/* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6655
6656 If DY > 0, move IT backward at least that many pixels. DY = 0
6657 means move IT backward to the preceding line start or BEGV. This
6658 function may move over more than DY pixels if IT->current_y - DY
6659 ends up in the middle of a line; in this case IT->current_y will be
6660 set to the top of the line moved to. */
6661
6662void
6663move_it_vertically_backward (it, dy)
6664 struct it *it;
6665 int dy;
6666{
79ddf6f7
GM
6667 int nlines, h;
6668 struct it it2, it3;
546f98b8 6669 int start_pos;
2311178e 6670
546f98b8 6671 move_further_back:
5f5c8ee5
GM
6672 xassert (dy >= 0);
6673
546f98b8
KS
6674 start_pos = IT_CHARPOS (*it);
6675
5f5c8ee5 6676 /* Estimate how many newlines we must move back. */
da8b7f4f 6677 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
5f5c8ee5
GM
6678
6679 /* Set the iterator's position that many lines back. */
6680 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6681 back_to_previous_visible_line_start (it);
6682
6683 /* Reseat the iterator here. When moving backward, we don't want
6684 reseat to skip forward over invisible text, set up the iterator
6685 to deliver from overlay strings at the new position etc. So,
6686 use reseat_1 here. */
6687 reseat_1 (it, it->current.pos, 1);
6688
6689 /* We are now surely at a line start. */
6690 it->current_x = it->hpos = 0;
0e47bbf7 6691 it->continuation_lines_width = 0;
5f5c8ee5
GM
6692
6693 /* Move forward and see what y-distance we moved. First move to the
6694 start of the next line so that we get its height. We need this
6695 height to be able to tell whether we reached the specified
6696 y-distance. */
6697 it2 = *it;
6698 it2.max_ascent = it2.max_descent = 0;
e0e9a80e
KS
6699 do
6700 {
6701 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6702 MOVE_TO_POS | MOVE_TO_VPOS);
6703 }
367d949f 6704 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
5f5c8ee5 6705 xassert (IT_CHARPOS (*it) >= BEGV);
79ddf6f7 6706 it3 = it2;
2311178e 6707
5f5c8ee5
GM
6708 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6709 xassert (IT_CHARPOS (*it) >= BEGV);
ccbb9ed2
RS
6710 /* H is the actual vertical distance from the position in *IT
6711 and the starting position. */
5f5c8ee5 6712 h = it2.current_y - it->current_y;
ccbb9ed2 6713 /* NLINES is the distance in number of lines. */
5f5c8ee5
GM
6714 nlines = it2.vpos - it->vpos;
6715
ccbb9ed2
RS
6716 /* Correct IT's y and vpos position
6717 so that they are relative to the starting point. */
5f5c8ee5
GM
6718 it->vpos -= nlines;
6719 it->current_y -= h;
2311178e 6720
5f5c8ee5
GM
6721 if (dy == 0)
6722 {
6723 /* DY == 0 means move to the start of the screen line. The
6724 value of nlines is > 0 if continuation lines were involved. */
6725 if (nlines > 0)
6726 move_it_by_lines (it, nlines, 1);
d782c348
KS
6727#if 0
6728 /* I think this assert is bogus if buffer contains
6729 invisible text or images. KFS. */
5f5c8ee5 6730 xassert (IT_CHARPOS (*it) <= start_pos);
d782c348 6731#endif
5f5c8ee5 6732 }
ccbb9ed2 6733 else
5f5c8ee5 6734 {
ccbb9ed2
RS
6735 /* The y-position we try to reach, relative to *IT.
6736 Note that H has been subtracted in front of the if-statement. */
5f5c8ee5 6737 int target_y = it->current_y + h - dy;
79ddf6f7
GM
6738 int y0 = it3.current_y;
6739 int y1 = line_bottom_y (&it3);
6740 int line_height = y1 - y0;
f7ccfc8c 6741
5f5c8ee5
GM
6742 /* If we did not reach target_y, try to move further backward if
6743 we can. If we moved too far backward, try to move forward. */
6744 if (target_y < it->current_y
79ddf6f7
GM
6745 /* This is heuristic. In a window that's 3 lines high, with
6746 a line height of 13 pixels each, recentering with point
6747 on the bottom line will try to move -39/2 = 19 pixels
6748 backward. Try to avoid moving into the first line. */
e10ee30c
KS
6749 && (it->current_y - target_y
6750 > min (window_box_height (it->w), line_height * 2 / 3))
5f5c8ee5
GM
6751 && IT_CHARPOS (*it) > BEGV)
6752 {
f7ccfc8c
GM
6753 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6754 target_y - it->current_y));
546f98b8
KS
6755 dy = it->current_y - target_y;
6756 goto move_further_back;
5f5c8ee5
GM
6757 }
6758 else if (target_y >= it->current_y + line_height
6759 && IT_CHARPOS (*it) < ZV)
6760 {
55591976 6761 /* Should move forward by at least one line, maybe more.
2311178e 6762
55591976
GM
6763 Note: Calling move_it_by_lines can be expensive on
6764 terminal frames, where compute_motion is used (via
6765 vmotion) to do the job, when there are very long lines
6766 and truncate-lines is nil. That's the reason for
6767 treating terminal frames specially here. */
2311178e 6768
55591976
GM
6769 if (!FRAME_WINDOW_P (it->f))
6770 move_it_vertically (it, target_y - (it->current_y + line_height));
6771 else
f7ccfc8c 6772 {
55591976
GM
6773 do
6774 {
6775 move_it_by_lines (it, 1, 1);
6776 }
6777 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
f7ccfc8c 6778 }
f7ccfc8c 6779
d782c348
KS
6780#if 0
6781 /* I think this assert is bogus if buffer contains
6782 invisible text or images. KFS. */
5f5c8ee5 6783 xassert (IT_CHARPOS (*it) >= BEGV);
d782c348 6784#endif
5f5c8ee5
GM
6785 }
6786 }
6787}
6788
6789
6790/* Move IT by a specified amount of pixel lines DY. DY negative means
6791 move backwards. DY = 0 means move to start of screen line. At the
6792 end, IT will be on the start of a screen line. */
6793
2311178e 6794void
5f5c8ee5
GM
6795move_it_vertically (it, dy)
6796 struct it *it;
6797 int dy;
6798{
6799 if (dy <= 0)
6800 move_it_vertically_backward (it, -dy);
546f98b8 6801 else
5f5c8ee5 6802 {
47589c8c 6803 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5f5c8ee5
GM
6804 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6805 MOVE_TO_POS | MOVE_TO_Y);
47589c8c 6806 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
6807
6808 /* If buffer ends in ZV without a newline, move to the start of
6809 the line to satisfy the post-condition. */
6810 if (IT_CHARPOS (*it) == ZV
ad5afd5b 6811 && ZV > BEGV
5f5c8ee5
GM
6812 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6813 move_it_by_lines (it, 0, 0);
6814 }
6815}
6816
6817
47fc2c10
GM
6818/* Move iterator IT past the end of the text line it is in. */
6819
6820void
6821move_it_past_eol (it)
6822 struct it *it;
6823{
6824 enum move_it_result rc;
2311178e 6825
47fc2c10
GM
6826 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6827 if (rc == MOVE_NEWLINE_OR_CR)
6828 set_iterator_to_next (it, 0);
6829}
6830
6831
2c79b732
GM
6832#if 0 /* Currently not used. */
6833
5f5c8ee5
GM
6834/* Return non-zero if some text between buffer positions START_CHARPOS
6835 and END_CHARPOS is invisible. IT->window is the window for text
6836 property lookup. */
6837
6838static int
6839invisible_text_between_p (it, start_charpos, end_charpos)
6840 struct it *it;
6841 int start_charpos, end_charpos;
6842{
5f5c8ee5
GM
6843 Lisp_Object prop, limit;
6844 int invisible_found_p;
2311178e 6845
5f5c8ee5
GM
6846 xassert (it != NULL && start_charpos <= end_charpos);
6847
6848 /* Is text at START invisible? */
6849 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6850 it->window);
6851 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6852 invisible_found_p = 1;
6853 else
6854 {
016b5642
MB
6855 limit = Fnext_single_char_property_change (make_number (start_charpos),
6856 Qinvisible, Qnil,
6857 make_number (end_charpos));
5f5c8ee5
GM
6858 invisible_found_p = XFASTINT (limit) < end_charpos;
6859 }
6860
6861 return invisible_found_p;
5f5c8ee5
GM
6862}
6863
2c79b732
GM
6864#endif /* 0 */
6865
5f5c8ee5
GM
6866
6867/* Move IT by a specified number DVPOS of screen lines down. DVPOS
6868 negative means move up. DVPOS == 0 means move to the start of the
6869 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6870 NEED_Y_P is zero, IT->current_y will be left unchanged.
6871
6872 Further optimization ideas: If we would know that IT->f doesn't use
6873 a face with proportional font, we could be faster for
6874 truncate-lines nil. */
6875
6876void
6877move_it_by_lines (it, dvpos, need_y_p)
6878 struct it *it;
6879 int dvpos, need_y_p;
6880{
6881 struct position pos;
2311178e 6882
5f5c8ee5
GM
6883 if (!FRAME_WINDOW_P (it->f))
6884 {
6885 struct text_pos textpos;
2311178e 6886
5f5c8ee5
GM
6887 /* We can use vmotion on frames without proportional fonts. */
6888 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6889 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6890 reseat (it, textpos, 1);
6891 it->vpos += pos.vpos;
6892 it->current_y += pos.vpos;
6893 }
6894 else if (dvpos == 0)
6895 {
6896 /* DVPOS == 0 means move to the start of the screen line. */
6897 move_it_vertically_backward (it, 0);
6898 xassert (it->current_x == 0 && it->hpos == 0);
546f98b8
KS
6899 /* Let next call to line_bottom_y calculate real line height */
6900 last_height = 0;
5f5c8ee5
GM
6901 }
6902 else if (dvpos > 0)
e0e9a80e
KS
6903 {
6904 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
367d949f 6905 if (!IT_POS_VALID_AFTER_MOVE_P (it))
e0e9a80e
KS
6906 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
6907 }
5f5c8ee5
GM
6908 else
6909 {
6910 struct it it2;
6911 int start_charpos, i;
2311178e 6912
e8660d73 6913 /* Start at the beginning of the screen line containing IT's
e0e9a80e
KS
6914 position. This may actually move vertically backwards,
6915 in case of overlays, so adjust dvpos accordingly. */
6916 dvpos += it->vpos;
e8660d73 6917 move_it_vertically_backward (it, 0);
e0e9a80e 6918 dvpos -= it->vpos;
2311178e 6919
5f5c8ee5
GM
6920 /* Go back -DVPOS visible lines and reseat the iterator there. */
6921 start_charpos = IT_CHARPOS (*it);
e0e9a80e 6922 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
5f5c8ee5
GM
6923 back_to_previous_visible_line_start (it);
6924 reseat (it, it->current.pos, 1);
e0e9a80e
KS
6925
6926 /* Move further back if we end up in a string or an image. */
367d949f 6927 while (!IT_POS_VALID_AFTER_MOVE_P (it))
e0e9a80e
KS
6928 {
6929 /* First try to move to start of display line. */
6930 dvpos += it->vpos;
6931 move_it_vertically_backward (it, 0);
6932 dvpos -= it->vpos;
367d949f 6933 if (IT_POS_VALID_AFTER_MOVE_P (it))
e0e9a80e
KS
6934 break;
6935 /* If start of line is still in string or image,
6936 move further back. */
6937 back_to_previous_visible_line_start (it);
6938 reseat (it, it->current.pos, 1);
6939 dvpos--;
6940 }
6941
5f5c8ee5
GM
6942 it->current_x = it->hpos = 0;
6943
6944 /* Above call may have moved too far if continuation lines
6945 are involved. Scan forward and see if it did. */
6946 it2 = *it;
6947 it2.vpos = it2.current_y = 0;
6948 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6949 it->vpos -= it2.vpos;
6950 it->current_y -= it2.current_y;
6951 it->current_x = it->hpos = 0;
6952
6adb6f01 6953 /* If we moved too far back, move IT some lines forward. */
5f5c8ee5
GM
6954 if (it2.vpos > -dvpos)
6955 {
6956 int delta = it2.vpos + dvpos;
6adb6f01 6957 it2 = *it;
5f5c8ee5 6958 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6adb6f01
KS
6959 /* Move back again if we got too far ahead. */
6960 if (IT_CHARPOS (*it) >= start_charpos)
6961 *it = it2;
5f5c8ee5
GM
6962 }
6963 }
6964}
6965
3824b1a6
AS
6966/* Return 1 if IT points into the middle of a display vector. */
6967
6968int
6969in_display_vector_p (it)
6970 struct it *it;
6971{
a6f82cc4 6972 return (it->method == GET_FROM_DISPLAY_VECTOR
3824b1a6
AS
6973 && it->current.dpvec_index > 0
6974 && it->dpvec + it->current.dpvec_index != it->dpend);
6975}
5f5c8ee5
GM
6976
6977\f
6978/***********************************************************************
6979 Messages
6980 ***********************************************************************/
6981
6982
937248bc
GM
6983/* Add a message with format string FORMAT and arguments ARG1 and ARG2
6984 to *Messages*. */
6985
6986void
6987add_to_log (format, arg1, arg2)
6988 char *format;
6989 Lisp_Object arg1, arg2;
6990{
6991 Lisp_Object args[3];
6992 Lisp_Object msg, fmt;
6993 char *buffer;
6994 int len;
6995 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
dbadb64b 6996 USE_SAFE_ALLOCA;
937248bc 6997
ae794295
GM
6998 /* Do nothing if called asynchronously. Inserting text into
6999 a buffer may call after-change-functions and alike and
7000 that would means running Lisp asynchronously. */
7001 if (handling_signal)
7002 return;
7003
937248bc
GM
7004 fmt = msg = Qnil;
7005 GCPRO4 (fmt, msg, arg1, arg2);
2311178e 7006
937248bc
GM
7007 args[0] = fmt = build_string (format);
7008 args[1] = arg1;
7009 args[2] = arg2;
6fc556fd 7010 msg = Fformat (3, args);
937248bc 7011
2051c264 7012 len = SBYTES (msg) + 1;
dbadb64b 7013 SAFE_ALLOCA (buffer, char *, len);
2051c264 7014 bcopy (SDATA (msg), buffer, len);
2311178e 7015
796184bc 7016 message_dolog (buffer, len - 1, 1, 0);
233f3db6 7017 SAFE_FREE ();
dbadb64b 7018
937248bc
GM
7019 UNGCPRO;
7020}
7021
7022
5f5c8ee5
GM
7023/* Output a newline in the *Messages* buffer if "needs" one. */
7024
7025void
7026message_log_maybe_newline ()
7027{
7028 if (message_log_need_newline)
7029 message_dolog ("", 0, 1, 0);
7030}
7031
7032
1e313f28 7033/* Add a string M of length NBYTES to the message log, optionally
5f5c8ee5
GM
7034 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7035 nonzero, means interpret the contents of M as multibyte. This
7036 function calls low-level routines in order to bypass text property
f291069a
KS
7037 hooks, etc. which might not be safe to run.
7038
7039 This may GC (insert may run before/after change hooks),
7040 so the buffer M must NOT point to a Lisp string. */
5f5c8ee5
GM
7041
7042void
1e313f28 7043message_dolog (m, nbytes, nlflag, multibyte)
50f80c2f 7044 const char *m;
1e313f28 7045 int nbytes, nlflag, multibyte;
5f5c8ee5 7046{
a67e162b
RS
7047 if (!NILP (Vmemory_full))
7048 return;
7049
5f5c8ee5
GM
7050 if (!NILP (Vmessage_log_max))
7051 {
7052 struct buffer *oldbuf;
7053 Lisp_Object oldpoint, oldbegv, oldzv;
7054 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7055 int point_at_end = 0;
7056 int zv_at_end = 0;
7057 Lisp_Object old_deactivate_mark, tem;
6052529b 7058 struct gcpro gcpro1;
5f5c8ee5
GM
7059
7060 old_deactivate_mark = Vdeactivate_mark;
7061 oldbuf = current_buffer;
6a94510a 7062 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5f5c8ee5
GM
7063 current_buffer->undo_list = Qt;
7064
b14bc55e
RS
7065 oldpoint = message_dolog_marker1;
7066 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7067 oldbegv = message_dolog_marker2;
7068 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7069 oldzv = message_dolog_marker3;
7070 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7071 GCPRO1 (old_deactivate_mark);
5f5c8ee5
GM
7072
7073 if (PT == Z)
7074 point_at_end = 1;
7075 if (ZV == Z)
7076 zv_at_end = 1;
7077
7078 BEGV = BEG;
7079 BEGV_BYTE = BEG_BYTE;
7080 ZV = Z;
7081 ZV_BYTE = Z_BYTE;
7082 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7083
7084 /* Insert the string--maybe converting multibyte to single byte
7085 or vice versa, so that all the text fits the buffer. */
7086 if (multibyte
7087 && NILP (current_buffer->enable_multibyte_characters))
7088 {
1e313f28 7089 int i, c, char_bytes;
5f5c8ee5 7090 unsigned char work[1];
2311178e 7091
5f5c8ee5
GM
7092 /* Convert a multibyte string to single-byte
7093 for the *Message* buffer. */
6e57ec5e 7094 for (i = 0; i < nbytes; i += char_bytes)
5f5c8ee5 7095 {
1e313f28 7096 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5f5c8ee5
GM
7097 work[0] = (SINGLE_BYTE_CHAR_P (c)
7098 ? c
7099 : multibyte_char_to_unibyte (c, Qnil));
7100 insert_1_both (work, 1, 1, 1, 0, 0);
7101 }
7102 }
7103 else if (! multibyte
7104 && ! NILP (current_buffer->enable_multibyte_characters))
7105 {
1e313f28 7106 int i, c, char_bytes;
5f5c8ee5 7107 unsigned char *msg = (unsigned char *) m;
260a86a0 7108 unsigned char str[MAX_MULTIBYTE_LENGTH];
5f5c8ee5
GM
7109 /* Convert a single-byte string to multibyte
7110 for the *Message* buffer. */
1e313f28 7111 for (i = 0; i < nbytes; i++)
5f5c8ee5
GM
7112 {
7113 c = unibyte_char_to_multibyte (msg[i]);
1e313f28
GM
7114 char_bytes = CHAR_STRING (c, str);
7115 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5f5c8ee5
GM
7116 }
7117 }
1e313f28
GM
7118 else if (nbytes)
7119 insert_1 (m, nbytes, 1, 0, 0);
5f5c8ee5
GM
7120
7121 if (nlflag)
7122 {
7123 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7124 insert_1 ("\n", 1, 1, 0, 0);
7125
7126 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7127 this_bol = PT;
7128 this_bol_byte = PT_BYTE;
7129
b14bc55e
RS
7130 /* See if this line duplicates the previous one.
7131 If so, combine duplicates. */
5f5c8ee5
GM
7132 if (this_bol > BEG)
7133 {
7134 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7135 prev_bol = PT;
7136 prev_bol_byte = PT_BYTE;
7137
7138 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7139 this_bol, this_bol_byte);
7140 if (dup)
7141 {
7142 del_range_both (prev_bol, prev_bol_byte,
7143 this_bol, this_bol_byte, 0);
7144 if (dup > 1)
7145 {
7146 char dupstr[40];
7147 int duplen;
7148
7149 /* If you change this format, don't forget to also
7150 change message_log_check_duplicate. */
7151 sprintf (dupstr, " [%d times]", dup);
7152 duplen = strlen (dupstr);
7153 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7154 insert_1 (dupstr, duplen, 1, 0, 1);
7155 }
7156 }
7157 }
7158
b14bc55e
RS
7159 /* If we have more than the desired maximum number of lines
7160 in the *Messages* buffer now, delete the oldest ones.
7161 This is safe because we don't have undo in this buffer. */
7162
5f5c8ee5
GM
7163 if (NATNUMP (Vmessage_log_max))
7164 {
7165 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7166 -XFASTINT (Vmessage_log_max) - 1, 0);
7167 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7168 }
7169 }
7170 BEGV = XMARKER (oldbegv)->charpos;
7171 BEGV_BYTE = marker_byte_position (oldbegv);
7172
7173 if (zv_at_end)
7174 {
7175 ZV = Z;
7176 ZV_BYTE = Z_BYTE;
7177 }
7178 else
7179 {
7180 ZV = XMARKER (oldzv)->charpos;
7181 ZV_BYTE = marker_byte_position (oldzv);
7182 }
7183
7184 if (point_at_end)
7185 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7186 else
7187 /* We can't do Fgoto_char (oldpoint) because it will run some
7188 Lisp code. */
7189 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7190 XMARKER (oldpoint)->bytepos);
7191
7192 UNGCPRO;
cfea0546
SM
7193 unchain_marker (XMARKER (oldpoint));
7194 unchain_marker (XMARKER (oldbegv));
7195 unchain_marker (XMARKER (oldzv));
5f5c8ee5
GM
7196
7197 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7198 set_buffer_internal (oldbuf);
7199 if (NILP (tem))
7200 windows_or_buffers_changed = old_windows_or_buffers_changed;
7201 message_log_need_newline = !nlflag;
7202 Vdeactivate_mark = old_deactivate_mark;
7203 }
7204}
7205
7206
7207/* We are at the end of the buffer after just having inserted a newline.
7208 (Note: We depend on the fact we won't be crossing the gap.)
7209 Check to see if the most recent message looks a lot like the previous one.
7210 Return 0 if different, 1 if the new one should just replace it, or a
7211 value N > 1 if we should also append " [N times]". */
7212
7213static int
7214message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7215 int prev_bol, this_bol;
7216 int prev_bol_byte, this_bol_byte;
7217{
7218 int i;
7219 int len = Z_BYTE - 1 - this_bol_byte;
7220 int seen_dots = 0;
7221 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7222 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7223
7224 for (i = 0; i < len; i++)
7225 {
509633e3 7226 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5f5c8ee5
GM
7227 seen_dots = 1;
7228 if (p1[i] != p2[i])
7229 return seen_dots;
7230 }
7231 p1 += len;
7232 if (*p1 == '\n')
7233 return 2;
7234 if (*p1++ == ' ' && *p1++ == '[')
7235 {
7236 int n = 0;
7237 while (*p1 >= '0' && *p1 <= '9')
7238 n = n * 10 + *p1++ - '0';
7239 if (strncmp (p1, " times]\n", 8) == 0)
7240 return n+1;
7241 }
7242 return 0;
7243}
b2683be4 7244\f
5f5c8ee5 7245
1e313f28
GM
7246/* Display an echo area message M with a specified length of NBYTES
7247 bytes. The string may include null characters. If M is 0, clear
7248 out any existing message, and let the mini-buffer text show
7249 through.
5f5c8ee5 7250
f291069a 7251 This may GC, so the buffer M must NOT point to a Lisp string. */
5f5c8ee5
GM
7252
7253void
1e313f28 7254message2 (m, nbytes, multibyte)
50f80c2f 7255 const char *m;
1e313f28 7256 int nbytes;
5f5c8ee5
GM
7257 int multibyte;
7258{
7259 /* First flush out any partial line written with print. */
7260 message_log_maybe_newline ();
7261 if (m)
1e313f28
GM
7262 message_dolog (m, nbytes, 1, multibyte);
7263 message2_nolog (m, nbytes, multibyte);
5f5c8ee5
GM
7264}
7265
7266
7267/* The non-logging counterpart of message2. */
7268
7269void
1e313f28 7270message2_nolog (m, nbytes, multibyte)
50f80c2f 7271 const char *m;
e3383b6f 7272 int nbytes, multibyte;
5f5c8ee5 7273{
886bd6f2 7274 struct frame *sf = SELECTED_FRAME ();
5f5c8ee5
GM
7275 message_enable_multibyte = multibyte;
7276
7277 if (noninteractive)
7278 {
7279 if (noninteractive_need_newline)
7280 putc ('\n', stderr);
7281 noninteractive_need_newline = 0;
7282 if (m)
1e313f28 7283 fwrite (m, nbytes, 1, stderr);
5f5c8ee5
GM
7284 if (cursor_in_echo_area == 0)
7285 fprintf (stderr, "\n");
7286 fflush (stderr);
7287 }
7288 /* A null message buffer means that the frame hasn't really been
7289 initialized yet. Error messages get reported properly by
7290 cmd_error, so this must be just an informative message; toss it. */
2311178e 7291 else if (INTERACTIVE
886bd6f2
GM
7292 && sf->glyphs_initialized_p
7293 && FRAME_MESSAGE_BUF (sf))
5f5c8ee5
GM
7294 {
7295 Lisp_Object mini_window;
7296 struct frame *f;
7297
7298 /* Get the frame containing the mini-buffer
7299 that the selected frame is using. */
886bd6f2 7300 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
7301 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7302
7303 FRAME_SAMPLE_VISIBILITY (f);
886bd6f2 7304 if (FRAME_VISIBLE_P (sf)
5f5c8ee5
GM
7305 && ! FRAME_VISIBLE_P (f))
7306 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7307
7308 if (m)
7309 {
1e313f28 7310 set_message (m, Qnil, nbytes, multibyte);
5f5c8ee5
GM
7311 if (minibuffer_auto_raise)
7312 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7313 }
7314 else
c6e89d6c 7315 clear_message (1, 1);
5f5c8ee5 7316
c6e89d6c 7317 do_pending_window_change (0);
5f5c8ee5 7318 echo_area_display (1);
c6e89d6c 7319 do_pending_window_change (0);
5f5c8ee5
GM
7320 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7321 (*frame_up_to_date_hook) (f);
7322 }
7323}
7324
7325
c6e89d6c
GM
7326/* Display an echo area message M with a specified length of NBYTES
7327 bytes. The string may include null characters. If M is not a
5f5c8ee5 7328 string, clear out any existing message, and let the mini-buffer
0644322d
RS
7329 text show through.
7330
7331 This function cancels echoing. */
5f5c8ee5
GM
7332
7333void
c6e89d6c 7334message3 (m, nbytes, multibyte)
5f5c8ee5 7335 Lisp_Object m;
c6e89d6c 7336 int nbytes;
5f5c8ee5
GM
7337 int multibyte;
7338{
7339 struct gcpro gcpro1;
7340
7341 GCPRO1 (m);
802c6e56 7342 clear_message (1,1);
0644322d 7343 cancel_echoing ();
2311178e 7344
5f5c8ee5
GM
7345 /* First flush out any partial line written with print. */
7346 message_log_maybe_newline ();
7347 if (STRINGP (m))
e4dd3419
KS
7348 {
7349 char *buffer;
7350 USE_SAFE_ALLOCA;
7351
7352 SAFE_ALLOCA (buffer, char *, nbytes);
7353 bcopy (SDATA (m), buffer, nbytes);
7354 message_dolog (buffer, nbytes, 1, multibyte);
7355 SAFE_FREE ();
7356 }
c6e89d6c 7357 message3_nolog (m, nbytes, multibyte);
5f5c8ee5
GM
7358
7359 UNGCPRO;
7360}
7361
7362
0644322d
RS
7363/* The non-logging version of message3.
7364 This does not cancel echoing, because it is used for echoing.
7365 Perhaps we need to make a separate function for echoing
7366 and make this cancel echoing. */
5f5c8ee5
GM
7367
7368void
c6e89d6c 7369message3_nolog (m, nbytes, multibyte)
5f5c8ee5 7370 Lisp_Object m;
c6e89d6c 7371 int nbytes, multibyte;
5f5c8ee5 7372{
886bd6f2 7373 struct frame *sf = SELECTED_FRAME ();
5f5c8ee5
GM
7374 message_enable_multibyte = multibyte;
7375
7376 if (noninteractive)
7377 {
7378 if (noninteractive_need_newline)
7379 putc ('\n', stderr);
7380 noninteractive_need_newline = 0;
7381 if (STRINGP (m))
2051c264 7382 fwrite (SDATA (m), nbytes, 1, stderr);
5f5c8ee5
GM
7383 if (cursor_in_echo_area == 0)
7384 fprintf (stderr, "\n");
7385 fflush (stderr);
7386 }
7387 /* A null message buffer means that the frame hasn't really been
7388 initialized yet. Error messages get reported properly by
7389 cmd_error, so this must be just an informative message; toss it. */
2311178e 7390 else if (INTERACTIVE
886bd6f2
GM
7391 && sf->glyphs_initialized_p
7392 && FRAME_MESSAGE_BUF (sf))
5f5c8ee5
GM
7393 {
7394 Lisp_Object mini_window;
c6e89d6c 7395 Lisp_Object frame;
5f5c8ee5
GM
7396 struct frame *f;
7397
7398 /* Get the frame containing the mini-buffer
7399 that the selected frame is using. */
886bd6f2 7400 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
7401 frame = XWINDOW (mini_window)->frame;
7402 f = XFRAME (frame);
5f5c8ee5
GM
7403
7404 FRAME_SAMPLE_VISIBILITY (f);
886bd6f2 7405 if (FRAME_VISIBLE_P (sf)
c6e89d6c
GM
7406 && !FRAME_VISIBLE_P (f))
7407 Fmake_frame_visible (frame);
5f5c8ee5 7408
2051c264 7409 if (STRINGP (m) && SCHARS (m) > 0)
5f5c8ee5 7410 {
c6e89d6c 7411 set_message (NULL, m, nbytes, multibyte);
468155d7
GM
7412 if (minibuffer_auto_raise)
7413 Fraise_frame (frame);
2eca855f
RS
7414 /* Assume we are not echoing.
7415 (If we are, echo_now will override this.) */
7416 echo_message_buffer = Qnil;
5f5c8ee5
GM
7417 }
7418 else
c6e89d6c 7419 clear_message (1, 1);
5f5c8ee5 7420
c6e89d6c 7421 do_pending_window_change (0);
5f5c8ee5 7422 echo_area_display (1);
c6e89d6c 7423 do_pending_window_change (0);
5f5c8ee5
GM
7424 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7425 (*frame_up_to_date_hook) (f);
7426 }
7427}
7428
7429
7430/* Display a null-terminated echo area message M. If M is 0, clear
7431 out any existing message, and let the mini-buffer text show through.
7432
7433 The buffer M must continue to exist until after the echo area gets
7434 cleared or some other message gets displayed there. Do not pass
7435 text that is stored in a Lisp string. Do not pass text in a buffer
7436 that was alloca'd. */
7437
7438void
7439message1 (m)
7440 char *m;
7441{
7442 message2 (m, (m ? strlen (m) : 0), 0);
7443}
7444
7445
7446/* The non-logging counterpart of message1. */
7447
7448void
7449message1_nolog (m)
7450 char *m;
7451{
7452 message2_nolog (m, (m ? strlen (m) : 0), 0);
7453}
7454
7455/* Display a message M which contains a single %s
7456 which gets replaced with STRING. */
7457
7458void
7459message_with_string (m, string, log)
7460 char *m;
7461 Lisp_Object string;
7462 int log;
7463{
5b6d51b6
RS
7464 CHECK_STRING (string);
7465
5f5c8ee5
GM
7466 if (noninteractive)
7467 {
7468 if (m)
7469 {
7470 if (noninteractive_need_newline)
7471 putc ('\n', stderr);
7472 noninteractive_need_newline = 0;
2051c264 7473 fprintf (stderr, m, SDATA (string));
5f5c8ee5
GM
7474 if (cursor_in_echo_area == 0)
7475 fprintf (stderr, "\n");
7476 fflush (stderr);
7477 }
7478 }
7479 else if (INTERACTIVE)
7480 {
7481 /* The frame whose minibuffer we're going to display the message on.
7482 It may be larger than the selected frame, so we need
7483 to use its buffer, not the selected frame's buffer. */
7484 Lisp_Object mini_window;
886bd6f2 7485 struct frame *f, *sf = SELECTED_FRAME ();
5f5c8ee5
GM
7486
7487 /* Get the frame containing the minibuffer
7488 that the selected frame is using. */
886bd6f2 7489 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
7490 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7491
7492 /* A null message buffer means that the frame hasn't really been
7493 initialized yet. Error messages get reported properly by
7494 cmd_error, so this must be just an informative message; toss it. */
7495 if (FRAME_MESSAGE_BUF (f))
7496 {
eb484132
GM
7497 Lisp_Object args[2], message;
7498 struct gcpro gcpro1, gcpro2;
5f5c8ee5 7499
eb484132
GM
7500 args[0] = build_string (m);
7501 args[1] = message = string;
78e17433 7502 GCPRO2 (args[0], message);
eb484132 7503 gcpro1.nvars = 2;
2311178e 7504
eb484132 7505 message = Fformat (2, args);
5f5c8ee5
GM
7506
7507 if (log)
d5db4077 7508 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
5f5c8ee5 7509 else
d5db4077 7510 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
eb484132
GM
7511
7512 UNGCPRO;
5f5c8ee5
GM
7513
7514 /* Print should start at the beginning of the message
7515 buffer next time. */
7516 message_buf_print = 0;
7517 }
7518 }
7519}
7520
7521
5f5c8ee5
GM
7522/* Dump an informative message to the minibuf. If M is 0, clear out
7523 any existing message, and let the mini-buffer text show through. */
7524
7525/* VARARGS 1 */
7526void
7527message (m, a1, a2, a3)
7528 char *m;
7529 EMACS_INT a1, a2, a3;
7530{
7531 if (noninteractive)
7532 {
7533 if (m)
7534 {
7535 if (noninteractive_need_newline)
7536 putc ('\n', stderr);
7537 noninteractive_need_newline = 0;
7538 fprintf (stderr, m, a1, a2, a3);
7539 if (cursor_in_echo_area == 0)
7540 fprintf (stderr, "\n");
7541 fflush (stderr);
7542 }
7543 }
7544 else if (INTERACTIVE)
7545 {
7546 /* The frame whose mini-buffer we're going to display the message
7547 on. It may be larger than the selected frame, so we need to
7548 use its buffer, not the selected frame's buffer. */
7549 Lisp_Object mini_window;
886bd6f2 7550 struct frame *f, *sf = SELECTED_FRAME ();
5f5c8ee5
GM
7551
7552 /* Get the frame containing the mini-buffer
7553 that the selected frame is using. */
886bd6f2 7554 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
7555 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7556
7557 /* A null message buffer means that the frame hasn't really been
7558 initialized yet. Error messages get reported properly by
7559 cmd_error, so this must be just an informative message; toss
7560 it. */
7561 if (FRAME_MESSAGE_BUF (f))
7562 {
7563 if (m)
7564 {
7565 int len;
7566#ifdef NO_ARG_ARRAY
7567 char *a[3];
7568 a[0] = (char *) a1;
7569 a[1] = (char *) a2;
7570 a[2] = (char *) a3;
7571
7572 len = doprnt (FRAME_MESSAGE_BUF (f),
7573 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7574#else
7575 len = doprnt (FRAME_MESSAGE_BUF (f),
7576 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7577 (char **) &a1);
7578#endif /* NO_ARG_ARRAY */
7579
7580 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7581 }
7582 else
7583 message1 (0);
7584
7585 /* Print should start at the beginning of the message
7586 buffer next time. */
7587 message_buf_print = 0;
7588 }
7589 }
7590}
7591
7592
7593/* The non-logging version of message. */
7594
7595void
7596message_nolog (m, a1, a2, a3)
7597 char *m;
7598 EMACS_INT a1, a2, a3;
7599{
7600 Lisp_Object old_log_max;
7601 old_log_max = Vmessage_log_max;
7602 Vmessage_log_max = Qnil;
7603 message (m, a1, a2, a3);
7604 Vmessage_log_max = old_log_max;
7605}
7606
7607
c6e89d6c
GM
7608/* Display the current message in the current mini-buffer. This is
7609 only called from error handlers in process.c, and is not time
7610 critical. */
5f5c8ee5
GM
7611
7612void
7613update_echo_area ()
7614{
c6e89d6c
GM
7615 if (!NILP (echo_area_buffer[0]))
7616 {
7617 Lisp_Object string;
7618 string = Fcurrent_message ();
2311178e 7619 message3 (string, SBYTES (string),
c6e89d6c
GM
7620 !NILP (current_buffer->enable_multibyte_characters));
7621 }
7622}
7623
7624
a67e162b
RS
7625/* Make sure echo area buffers in `echo_buffers' are live.
7626 If they aren't, make new ones. */
5bcfeb49
GM
7627
7628static void
7629ensure_echo_area_buffers ()
7630{
7631 int i;
7632
7633 for (i = 0; i < 2; ++i)
7634 if (!BUFFERP (echo_buffer[i])
7635 || NILP (XBUFFER (echo_buffer[i])->name))
7636 {
7637 char name[30];
ff3d9573
GM
7638 Lisp_Object old_buffer;
7639 int j;
7640
7641 old_buffer = echo_buffer[i];
5bcfeb49
GM
7642 sprintf (name, " *Echo Area %d*", i);
7643 echo_buffer[i] = Fget_buffer_create (build_string (name));
ad4f174e 7644 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
ff3d9573
GM
7645
7646 for (j = 0; j < 2; ++j)
7647 if (EQ (old_buffer, echo_area_buffer[j]))
7648 echo_area_buffer[j] = echo_buffer[i];
5bcfeb49
GM
7649 }
7650}
7651
7652
23a96c77 7653/* Call FN with args A1..A4 with either the current or last displayed
c6e89d6c
GM
7654 echo_area_buffer as current buffer.
7655
7656 WHICH zero means use the current message buffer
7657 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7658 from echo_buffer[] and clear it.
7659
7660 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7661 suitable buffer from echo_buffer[] and clear it.
7662
4b41cebb 7663 Value is what FN returns. */
c6e89d6c
GM
7664
7665static int
23a96c77 7666with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
c6e89d6c
GM
7667 struct window *w;
7668 int which;
23dd2d97
KR
7669 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7670 EMACS_INT a1;
7671 Lisp_Object a2;
7672 EMACS_INT a3, a4;
c6e89d6c
GM
7673{
7674 Lisp_Object buffer;
15e26c76 7675 int this_one, the_other, clear_buffer_p, rc;
331379bf 7676 int count = SPECPDL_INDEX ();
c6e89d6c 7677
9583b2bb 7678 /* If buffers aren't live, make new ones. */
5bcfeb49 7679 ensure_echo_area_buffers ();
c6e89d6c
GM
7680
7681 clear_buffer_p = 0;
2311178e 7682
c6e89d6c
GM
7683 if (which == 0)
7684 this_one = 0, the_other = 1;
7685 else if (which > 0)
7686 this_one = 1, the_other = 0;
c6e89d6c
GM
7687
7688 /* Choose a suitable buffer from echo_buffer[] is we don't
7689 have one. */
7690 if (NILP (echo_area_buffer[this_one]))
7691 {
7692 echo_area_buffer[this_one]
7693 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7694 ? echo_buffer[the_other]
7695 : echo_buffer[this_one]);
7696 clear_buffer_p = 1;
7697 }
7698
7699 buffer = echo_area_buffer[this_one];
7700
1013f4e3
GM
7701 /* Don't get confused by reusing the buffer used for echoing
7702 for a different purpose. */
032906b1 7703 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
1013f4e3
GM
7704 cancel_echoing ();
7705
c6e89d6c
GM
7706 record_unwind_protect (unwind_with_echo_area_buffer,
7707 with_echo_area_buffer_unwind_data (w));
7708
7709 /* Make the echo area buffer current. Note that for display
7710 purposes, it is not necessary that the displayed window's buffer
7711 == current_buffer, except for text property lookup. So, let's
7712 only set that buffer temporarily here without doing a full
7713 Fset_window_buffer. We must also change w->pointm, though,
7714 because otherwise an assertions in unshow_buffer fails, and Emacs
7715 aborts. */
9142dd5b 7716 set_buffer_internal_1 (XBUFFER (buffer));
c6e89d6c
GM
7717 if (w)
7718 {
7719 w->buffer = buffer;
7720 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7721 }
ad4f174e 7722
c6e89d6c
GM
7723 current_buffer->undo_list = Qt;
7724 current_buffer->read_only = Qnil;
bbbf6d06 7725 specbind (Qinhibit_read_only, Qt);
0b04fa5f 7726 specbind (Qinhibit_modification_hooks, Qt);
c6e89d6c
GM
7727
7728 if (clear_buffer_p && Z > BEG)
7729 del_range (BEG, Z);
7730
7731 xassert (BEGV >= BEG);
7732 xassert (ZV <= Z && ZV >= BEGV);
7733
23a96c77 7734 rc = fn (a1, a2, a3, a4);
c6e89d6c
GM
7735
7736 xassert (BEGV >= BEG);
7737 xassert (ZV <= Z && ZV >= BEGV);
7738
7739 unbind_to (count, Qnil);
7740 return rc;
5f5c8ee5
GM
7741}
7742
7743
c6e89d6c
GM
7744/* Save state that should be preserved around the call to the function
7745 FN called in with_echo_area_buffer. */
5f5c8ee5 7746
c6e89d6c
GM
7747static Lisp_Object
7748with_echo_area_buffer_unwind_data (w)
7749 struct window *w;
5f5c8ee5 7750{
c6e89d6c
GM
7751 int i = 0;
7752 Lisp_Object vector;
5f5c8ee5 7753
c6e89d6c
GM
7754 /* Reduce consing by keeping one vector in
7755 Vwith_echo_area_save_vector. */
7756 vector = Vwith_echo_area_save_vector;
7757 Vwith_echo_area_save_vector = Qnil;
2311178e 7758
c6e89d6c 7759 if (NILP (vector))
9142dd5b 7760 vector = Fmake_vector (make_number (7), Qnil);
2311178e 7761
a61b7058
GM
7762 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7763 AREF (vector, i) = Vdeactivate_mark, ++i;
7764 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
2311178e 7765
c6e89d6c
GM
7766 if (w)
7767 {
a61b7058
GM
7768 XSETWINDOW (AREF (vector, i), w); ++i;
7769 AREF (vector, i) = w->buffer; ++i;
7770 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7771 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
c6e89d6c
GM
7772 }
7773 else
7774 {
7775 int end = i + 4;
a61b7058
GM
7776 for (; i < end; ++i)
7777 AREF (vector, i) = Qnil;
c6e89d6c 7778 }
5f5c8ee5 7779
a61b7058 7780 xassert (i == ASIZE (vector));
c6e89d6c
GM
7781 return vector;
7782}
5f5c8ee5 7783
5f5c8ee5 7784
c6e89d6c
GM
7785/* Restore global state from VECTOR which was created by
7786 with_echo_area_buffer_unwind_data. */
7787
7788static Lisp_Object
7789unwind_with_echo_area_buffer (vector)
7790 Lisp_Object vector;
7791{
bbbf6d06
GM
7792 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7793 Vdeactivate_mark = AREF (vector, 1);
7794 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
c6e89d6c 7795
bbbf6d06 7796 if (WINDOWP (AREF (vector, 3)))
c6e89d6c
GM
7797 {
7798 struct window *w;
7799 Lisp_Object buffer, charpos, bytepos;
2311178e 7800
bbbf6d06
GM
7801 w = XWINDOW (AREF (vector, 3));
7802 buffer = AREF (vector, 4);
7803 charpos = AREF (vector, 5);
7804 bytepos = AREF (vector, 6);
2311178e 7805
c6e89d6c
GM
7806 w->buffer = buffer;
7807 set_marker_both (w->pointm, buffer,
7808 XFASTINT (charpos), XFASTINT (bytepos));
7809 }
7810
7811 Vwith_echo_area_save_vector = vector;
7812 return Qnil;
7813}
7814
7815
7816/* Set up the echo area for use by print functions. MULTIBYTE_P
7817 non-zero means we will print multibyte. */
7818
7819void
7820setup_echo_area_for_printing (multibyte_p)
7821 int multibyte_p;
7822{
03b0a4b4
RS
7823 /* If we can't find an echo area any more, exit. */
7824 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7825 Fkill_emacs (Qnil);
7826
5bcfeb49
GM
7827 ensure_echo_area_buffers ();
7828
c6e89d6c
GM
7829 if (!message_buf_print)
7830 {
7831 /* A message has been output since the last time we printed.
7832 Choose a fresh echo area buffer. */
7833 if (EQ (echo_area_buffer[1], echo_buffer[0]))
2311178e 7834 echo_area_buffer[0] = echo_buffer[1];
c6e89d6c
GM
7835 else
7836 echo_area_buffer[0] = echo_buffer[0];
7837
7838 /* Switch to that buffer and clear it. */
7839 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
ab2c5f0a 7840 current_buffer->truncate_lines = Qnil;
2311178e 7841
c6e89d6c 7842 if (Z > BEG)
bbbf6d06 7843 {
331379bf 7844 int count = SPECPDL_INDEX ();
bbbf6d06 7845 specbind (Qinhibit_read_only, Qt);
a67e162b 7846 /* Note that undo recording is always disabled. */
bbbf6d06
GM
7847 del_range (BEG, Z);
7848 unbind_to (count, Qnil);
7849 }
c6e89d6c
GM
7850 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7851
7852 /* Set up the buffer for the multibyteness we need. */
7853 if (multibyte_p
7854 != !NILP (current_buffer->enable_multibyte_characters))
7855 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7856
7857 /* Raise the frame containing the echo area. */
7858 if (minibuffer_auto_raise)
7859 {
886bd6f2 7860 struct frame *sf = SELECTED_FRAME ();
c6e89d6c 7861 Lisp_Object mini_window;
886bd6f2 7862 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
7863 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7864 }
7865
8a4e3c0c 7866 message_log_maybe_newline ();
c6e89d6c
GM
7867 message_buf_print = 1;
7868 }
fa77249f
GM
7869 else
7870 {
7871 if (NILP (echo_area_buffer[0]))
7872 {
7873 if (EQ (echo_area_buffer[1], echo_buffer[0]))
2311178e 7874 echo_area_buffer[0] = echo_buffer[1];
fa77249f
GM
7875 else
7876 echo_area_buffer[0] = echo_buffer[0];
7877 }
2311178e 7878
fa77249f 7879 if (current_buffer != XBUFFER (echo_area_buffer[0]))
ab2c5f0a
GM
7880 {
7881 /* Someone switched buffers between print requests. */
7882 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7883 current_buffer->truncate_lines = Qnil;
7884 }
fa77249f 7885 }
c6e89d6c
GM
7886}
7887
7888
dd2eb166
GM
7889/* Display an echo area message in window W. Value is non-zero if W's
7890 height is changed. If display_last_displayed_message_p is
7891 non-zero, display the message that was last displayed, otherwise
7892 display the current message. */
c6e89d6c
GM
7893
7894static int
7895display_echo_area (w)
7896 struct window *w;
7897{
25edb08f
GM
7898 int i, no_message_p, window_height_changed_p, count;
7899
7900 /* Temporarily disable garbage collections while displaying the echo
7901 area. This is done because a GC can print a message itself.
7902 That message would modify the echo area buffer's contents while a
7903 redisplay of the buffer is going on, and seriously confuse
7904 redisplay. */
7905 count = inhibit_garbage_collection ();
dd2eb166
GM
7906
7907 /* If there is no message, we must call display_echo_area_1
7908 nevertheless because it resizes the window. But we will have to
7909 reset the echo_area_buffer in question to nil at the end because
7910 with_echo_area_buffer will sets it to an empty buffer. */
7911 i = display_last_displayed_message_p ? 1 : 0;
7912 no_message_p = NILP (echo_area_buffer[i]);
2311178e 7913
dd2eb166
GM
7914 window_height_changed_p
7915 = with_echo_area_buffer (w, display_last_displayed_message_p,
23a96c77 7916 display_echo_area_1,
23dd2d97 7917 (EMACS_INT) w, Qnil, 0, 0);
dd2eb166
GM
7918
7919 if (no_message_p)
7920 echo_area_buffer[i] = Qnil;
25edb08f
GM
7921
7922 unbind_to (count, Qnil);
dd2eb166 7923 return window_height_changed_p;
c6e89d6c
GM
7924}
7925
7926
7927/* Helper for display_echo_area. Display the current buffer which
23a96c77
GM
7928 contains the current echo area message in window W, a mini-window,
7929 a pointer to which is passed in A1. A2..A4 are currently not used.
c6e89d6c
GM
7930 Change the height of W so that all of the message is displayed.
7931 Value is non-zero if height of W was changed. */
7932
7933static int
23a96c77 7934display_echo_area_1 (a1, a2, a3, a4)
23dd2d97
KR
7935 EMACS_INT a1;
7936 Lisp_Object a2;
7937 EMACS_INT a3, a4;
c6e89d6c 7938{
23a96c77 7939 struct window *w = (struct window *) a1;
c6e89d6c 7940 Lisp_Object window;
c6e89d6c
GM
7941 struct text_pos start;
7942 int window_height_changed_p = 0;
7943
7944 /* Do this before displaying, so that we have a large enough glyph
40b388f9
RS
7945 matrix for the display. If we can't get enough space for the
7946 whole text, display the last N lines. That works by setting w->start. */
92a90e89 7947 window_height_changed_p = resize_mini_window (w, 0);
c6e89d6c 7948
40b388f9
RS
7949 /* Use the starting position chosen by resize_mini_window. */
7950 SET_TEXT_POS_FROM_MARKER (start, w->start);
7951
c6e89d6c
GM
7952 /* Display. */
7953 clear_glyph_matrix (w->desired_matrix);
7954 XSETWINDOW (window, w);
493c1d49 7955 try_window (window, start, 0);
c6e89d6c 7956
c6e89d6c
GM
7957 return window_height_changed_p;
7958}
7959
7960
92a90e89 7961/* Resize the echo area window to exactly the size needed for the
6d004fea
GM
7962 currently displayed message, if there is one. If a mini-buffer
7963 is active, don't shrink it. */
92a90e89
GM
7964
7965void
308a74d8 7966resize_echo_area_exactly ()
92a90e89
GM
7967{
7968 if (BUFFERP (echo_area_buffer[0])
7969 && WINDOWP (echo_area_window))
7970 {
7971 struct window *w = XWINDOW (echo_area_window);
7972 int resized_p;
6d004fea
GM
7973 Lisp_Object resize_exactly;
7974
7975 if (minibuf_level == 0)
7976 resize_exactly = Qt;
7977 else
7978 resize_exactly = Qnil;
2311178e 7979
23a96c77 7980 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6d004fea 7981 (EMACS_INT) w, resize_exactly, 0, 0);
92a90e89
GM
7982 if (resized_p)
7983 {
7984 ++windows_or_buffers_changed;
7985 ++update_mode_lines;
7986 redisplay_internal (0);
7987 }
7988 }
7989}
7990
7991
23a96c77 7992/* Callback function for with_echo_area_buffer, when used from
308a74d8 7993 resize_echo_area_exactly. A1 contains a pointer to the window to
6d004fea
GM
7994 resize, EXACTLY non-nil means resize the mini-window exactly to the
7995 size of the text displayed. A3 and A4 are not used. Value is what
7996 resize_mini_window returns. */
23a96c77
GM
7997
7998static int
6d004fea 7999resize_mini_window_1 (a1, exactly, a3, a4)
23dd2d97 8000 EMACS_INT a1;
6d004fea 8001 Lisp_Object exactly;
23dd2d97 8002 EMACS_INT a3, a4;
23a96c77 8003{
6d004fea 8004 return resize_mini_window ((struct window *) a1, !NILP (exactly));
23a96c77
GM
8005}
8006
8007
92a90e89
GM
8008/* Resize mini-window W to fit the size of its contents. EXACT:P
8009 means size the window exactly to the size needed. Otherwise, it's
40b388f9 8010 only enlarged until W's buffer is empty.
f6c05015 8011
40b388f9
RS
8012 Set W->start to the right place to begin display. If the whole
8013 contents fit, start at the beginning. Otherwise, start so as
8014 to make the end of the contents appear. This is particularly
8015 important for y-or-n-p, but seems desirable generally.
8016
8017 Value is non-zero if the window height has been changed. */
c6e89d6c 8018
9472f927 8019int
92a90e89 8020resize_mini_window (w, exact_p)
c6e89d6c 8021 struct window *w;
92a90e89 8022 int exact_p;
c6e89d6c
GM
8023{
8024 struct frame *f = XFRAME (w->frame);
8025 int window_height_changed_p = 0;
8026
8027 xassert (MINI_WINDOW_P (w));
97cafc0f 8028
40b388f9
RS
8029 /* By default, start display at the beginning. */
8030 set_marker_both (w->start, w->buffer,
8031 BUF_BEGV (XBUFFER (w->buffer)),
8032 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8033
2913a9c0
GM
8034 /* Don't resize windows while redisplaying a window; it would
8035 confuse redisplay functions when the size of the window they are
8036 displaying changes from under them. Such a resizing can happen,
8037 for instance, when which-func prints a long message while
8038 we are running fontification-functions. We're running these
8039 functions with safe_call which binds inhibit-redisplay to t. */
8040 if (!NILP (Vinhibit_redisplay))
64c5be50 8041 return 0;
2311178e 8042
97cafc0f 8043 /* Nil means don't try to resize. */
6422c1d7 8044 if (NILP (Vresize_mini_windows)
fa3c6b4d 8045 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
97cafc0f 8046 return 0;
2311178e 8047
c6e89d6c
GM
8048 if (!FRAME_MINIBUF_ONLY_P (f))
8049 {
8050 struct it it;
dd2eb166 8051 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
da8b7f4f 8052 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
dd2eb166 8053 int height, max_height;
da8b7f4f 8054 int unit = FRAME_LINE_HEIGHT (f);
dd2eb166 8055 struct text_pos start;
1bfdbe43
GM
8056 struct buffer *old_current_buffer = NULL;
8057
8058 if (current_buffer != XBUFFER (w->buffer))
8059 {
8060 old_current_buffer = current_buffer;
8061 set_buffer_internal (XBUFFER (w->buffer));
8062 }
9142dd5b 8063
c6e89d6c 8064 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
c6e89d6c 8065
dd2eb166
GM
8066 /* Compute the max. number of lines specified by the user. */
8067 if (FLOATP (Vmax_mini_window_height))
da8b7f4f 8068 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
dd2eb166
GM
8069 else if (INTEGERP (Vmax_mini_window_height))
8070 max_height = XINT (Vmax_mini_window_height);
97cafc0f
GM
8071 else
8072 max_height = total_height / 4;
2311178e 8073
4b41cebb 8074 /* Correct that max. height if it's bogus. */
dd2eb166
GM
8075 max_height = max (1, max_height);
8076 max_height = min (total_height, max_height);
2311178e 8077
dd2eb166 8078 /* Find out the height of the text in the window. */
ad4f174e
GM
8079 if (it.truncate_lines_p)
8080 height = 1;
55b064bd 8081 else
ad4f174e
GM
8082 {
8083 last_height = 0;
8084 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8085 if (it.max_ascent == 0 && it.max_descent == 0)
8086 height = it.current_y + last_height;
8087 else
8088 height = it.current_y + it.max_ascent + it.max_descent;
546f98b8 8089 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
ad4f174e
GM
8090 height = (height + unit - 1) / unit;
8091 }
2311178e 8092
dd2eb166
GM
8093 /* Compute a suitable window start. */
8094 if (height > max_height)
8095 {
8096 height = max_height;
40b388f9 8097 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
dd2eb166
GM
8098 move_it_vertically_backward (&it, (height - 1) * unit);
8099 start = it.current.pos;
8100 }
8101 else
8102 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8103 SET_MARKER_FROM_TEXT_POS (w->start, start);
c59c668a 8104
6422c1d7 8105 if (EQ (Vresize_mini_windows, Qgrow_only))
dd2eb166 8106 {
6422c1d7
GM
8107 /* Let it grow only, until we display an empty message, in which
8108 case the window shrinks again. */
da8b7f4f 8109 if (height > WINDOW_TOTAL_LINES (w))
6422c1d7 8110 {
da8b7f4f 8111 int old_height = WINDOW_TOTAL_LINES (w);
6422c1d7 8112 freeze_window_starts (f, 1);
da8b7f4f
KS
8113 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8114 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
6422c1d7 8115 }
da8b7f4f 8116 else if (height < WINDOW_TOTAL_LINES (w)
6422c1d7
GM
8117 && (exact_p || BEGV == ZV))
8118 {
da8b7f4f 8119 int old_height = WINDOW_TOTAL_LINES (w);
6422c1d7
GM
8120 freeze_window_starts (f, 0);
8121 shrink_mini_window (w);
da8b7f4f 8122 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
6422c1d7 8123 }
da448723 8124 }
2311178e 8125 else
da448723 8126 {
6422c1d7 8127 /* Always resize to exact size needed. */
da8b7f4f 8128 if (height > WINDOW_TOTAL_LINES (w))
6422c1d7 8129 {
da8b7f4f 8130 int old_height = WINDOW_TOTAL_LINES (w);
6422c1d7 8131 freeze_window_starts (f, 1);
da8b7f4f
KS
8132 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8133 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
6422c1d7 8134 }
da8b7f4f 8135 else if (height < WINDOW_TOTAL_LINES (w))
6422c1d7 8136 {
da8b7f4f 8137 int old_height = WINDOW_TOTAL_LINES (w);
6422c1d7
GM
8138 freeze_window_starts (f, 0);
8139 shrink_mini_window (w);
8140
8141 if (height)
8142 {
8143 freeze_window_starts (f, 1);
da8b7f4f 8144 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
6422c1d7 8145 }
2311178e 8146
da8b7f4f 8147 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
6422c1d7 8148 }
9142dd5b 8149 }
1bfdbe43
GM
8150
8151 if (old_current_buffer)
8152 set_buffer_internal (old_current_buffer);
c6e89d6c
GM
8153 }
8154
8155 return window_height_changed_p;
8156}
8157
8158
8159/* Value is the current message, a string, or nil if there is no
8160 current message. */
8161
8162Lisp_Object
8163current_message ()
8164{
8165 Lisp_Object msg;
8166
8167 if (NILP (echo_area_buffer[0]))
8168 msg = Qnil;
8169 else
8170 {
23a96c77 8171 with_echo_area_buffer (0, 0, current_message_1,
23dd2d97 8172 (EMACS_INT) &msg, Qnil, 0, 0);
c6e89d6c
GM
8173 if (NILP (msg))
8174 echo_area_buffer[0] = Qnil;
8175 }
2311178e 8176
c6e89d6c
GM
8177 return msg;
8178}
8179
8180
8181static int
23a96c77 8182current_message_1 (a1, a2, a3, a4)
23dd2d97
KR
8183 EMACS_INT a1;
8184 Lisp_Object a2;
8185 EMACS_INT a3, a4;
c6e89d6c 8186{
23a96c77 8187 Lisp_Object *msg = (Lisp_Object *) a1;
2311178e 8188
c6e89d6c
GM
8189 if (Z > BEG)
8190 *msg = make_buffer_string (BEG, Z, 1);
8191 else
8192 *msg = Qnil;
8193 return 0;
8194}
8195
8196
8197/* Push the current message on Vmessage_stack for later restauration
8198 by restore_message. Value is non-zero if the current message isn't
8199 empty. This is a relatively infrequent operation, so it's not
8200 worth optimizing. */
8201
8202int
8203push_message ()
8204{
8205 Lisp_Object msg;
8206 msg = current_message ();
8207 Vmessage_stack = Fcons (msg, Vmessage_stack);
8208 return STRINGP (msg);
8209}
8210
8211
8212/* Restore message display from the top of Vmessage_stack. */
8213
8214void
8215restore_message ()
8216{
8217 Lisp_Object msg;
2311178e 8218
c6e89d6c
GM
8219 xassert (CONSP (Vmessage_stack));
8220 msg = XCAR (Vmessage_stack);
8221 if (STRINGP (msg))
d5db4077 8222 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
c6e89d6c
GM
8223 else
8224 message3_nolog (msg, 0, 0);
8225}
8226
8227
37d66095
RS
8228/* Handler for record_unwind_protect calling pop_message. */
8229
8230Lisp_Object
8231pop_message_unwind (dummy)
8232 Lisp_Object dummy;
8233{
8234 pop_message ();
8235 return Qnil;
8236}
8237
c6e89d6c
GM
8238/* Pop the top-most entry off Vmessage_stack. */
8239
8240void
8241pop_message ()
8242{
8243 xassert (CONSP (Vmessage_stack));
8244 Vmessage_stack = XCDR (Vmessage_stack);
8245}
8246
8247
8248/* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8249 exits. If the stack is not empty, we have a missing pop_message
8250 somewhere. */
8251
8252void
8253check_message_stack ()
8254{
8255 if (!NILP (Vmessage_stack))
8256 abort ();
8257}
8258
8259
8260/* Truncate to NCHARS what will be displayed in the echo area the next
8261 time we display it---but don't redisplay it now. */
8262
8263void
8264truncate_echo_area (nchars)
8265 int nchars;
8266{
8267 if (nchars == 0)
8268 echo_area_buffer[0] = Qnil;
8269 /* A null message buffer means that the frame hasn't really been
8270 initialized yet. Error messages get reported properly by
8271 cmd_error, so this must be just an informative message; toss it. */
8272 else if (!noninteractive
8273 && INTERACTIVE
c6e89d6c 8274 && !NILP (echo_area_buffer[0]))
886bd6f2
GM
8275 {
8276 struct frame *sf = SELECTED_FRAME ();
8277 if (FRAME_MESSAGE_BUF (sf))
23dd2d97 8278 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
886bd6f2 8279 }
c6e89d6c
GM
8280}
8281
8282
8283/* Helper function for truncate_echo_area. Truncate the current
8284 message to at most NCHARS characters. */
8285
8286static int
23a96c77 8287truncate_message_1 (nchars, a2, a3, a4)
23dd2d97
KR
8288 EMACS_INT nchars;
8289 Lisp_Object a2;
8290 EMACS_INT a3, a4;
c6e89d6c
GM
8291{
8292 if (BEG + nchars < Z)
8293 del_range (BEG + nchars, Z);
8294 if (Z == BEG)
8295 echo_area_buffer[0] = Qnil;
8296 return 0;
8297}
8298
8299
8300/* Set the current message to a substring of S or STRING.
8301
8302 If STRING is a Lisp string, set the message to the first NBYTES
8303 bytes from STRING. NBYTES zero means use the whole string. If
8304 STRING is multibyte, the message will be displayed multibyte.
8305
8306 If S is not null, set the message to the first LEN bytes of S. LEN
8307 zero means use the whole string. MULTIBYTE_P non-zero means S is
f291069a
KS
8308 multibyte. Display the message multibyte in that case.
8309
8310 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8311 to t before calling set_message_1 (which calls insert).
8312 */
c6e89d6c
GM
8313
8314void
8315set_message (s, string, nbytes, multibyte_p)
50f80c2f 8316 const char *s;
c6e89d6c 8317 Lisp_Object string;
e3383b6f 8318 int nbytes, multibyte_p;
c6e89d6c
GM
8319{
8320 message_enable_multibyte
8321 = ((s && multibyte_p)
8322 || (STRINGP (string) && STRING_MULTIBYTE (string)));
2311178e 8323
a8a1c0ee 8324 with_echo_area_buffer (0, 0, set_message_1,
23a96c77 8325 (EMACS_INT) s, string, nbytes, multibyte_p);
c6e89d6c 8326 message_buf_print = 0;
21fdfb65 8327 help_echo_showing_p = 0;
c6e89d6c
GM
8328}
8329
8330
8331/* Helper function for set_message. Arguments have the same meaning
23a96c77
GM
8332 as there, with A1 corresponding to S and A2 corresponding to STRING
8333 This function is called with the echo area buffer being
c6e89d6c
GM
8334 current. */
8335
8336static int
23a96c77 8337set_message_1 (a1, a2, nbytes, multibyte_p)
23dd2d97
KR
8338 EMACS_INT a1;
8339 Lisp_Object a2;
8340 EMACS_INT nbytes, multibyte_p;
c6e89d6c 8341{
50f80c2f 8342 const char *s = (const char *) a1;
23dd2d97 8343 Lisp_Object string = a2;
2311178e 8344
c6e89d6c
GM
8345 /* Change multibyteness of the echo buffer appropriately. */
8346 if (message_enable_multibyte
8347 != !NILP (current_buffer->enable_multibyte_characters))
8348 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8349
ad4f174e 8350 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
2311178e 8351
c6e89d6c
GM
8352 /* Insert new message at BEG. */
8353 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
a8a1c0ee 8354 Ferase_buffer ();
c6e89d6c
GM
8355
8356 if (STRINGP (string))
8357 {
8358 int nchars;
2311178e 8359
c6e89d6c 8360 if (nbytes == 0)
2051c264 8361 nbytes = SBYTES (string);
c6e89d6c 8362 nchars = string_byte_to_char (string, nbytes);
2311178e 8363
c6e89d6c
GM
8364 /* This function takes care of single/multibyte conversion. We
8365 just have to ensure that the echo area buffer has the right
8366 setting of enable_multibyte_characters. */
8367 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8368 }
8369 else if (s)
8370 {
8371 if (nbytes == 0)
8372 nbytes = strlen (s);
2311178e 8373
c6e89d6c
GM
8374 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8375 {
8376 /* Convert from multi-byte to single-byte. */
8377 int i, c, n;
8378 unsigned char work[1];
2311178e 8379
c6e89d6c
GM
8380 /* Convert a multibyte string to single-byte. */
8381 for (i = 0; i < nbytes; i += n)
8382 {
8383 c = string_char_and_length (s + i, nbytes - i, &n);
8384 work[0] = (SINGLE_BYTE_CHAR_P (c)
8385 ? c
8386 : multibyte_char_to_unibyte (c, Qnil));
8387 insert_1_both (work, 1, 1, 1, 0, 0);
8388 }
8389 }
8390 else if (!multibyte_p
8391 && !NILP (current_buffer->enable_multibyte_characters))
8392 {
8393 /* Convert from single-byte to multi-byte. */
8394 int i, c, n;
50f80c2f 8395 const unsigned char *msg = (const unsigned char *) s;
260a86a0 8396 unsigned char str[MAX_MULTIBYTE_LENGTH];
2311178e 8397
c6e89d6c
GM
8398 /* Convert a single-byte string to multibyte. */
8399 for (i = 0; i < nbytes; i++)
8400 {
8401 c = unibyte_char_to_multibyte (msg[i]);
260a86a0
KH
8402 n = CHAR_STRING (c, str);
8403 insert_1_both (str, 1, n, 1, 0, 0);
c6e89d6c
GM
8404 }
8405 }
8406 else
8407 insert_1 (s, nbytes, 1, 0, 0);
8408 }
8409
8410 return 0;
8411}
8412
8413
8414/* Clear messages. CURRENT_P non-zero means clear the current
8415 message. LAST_DISPLAYED_P non-zero means clear the message
8416 last displayed. */
8417
8418void
8419clear_message (current_p, last_displayed_p)
8420 int current_p, last_displayed_p;
8421{
8422 if (current_p)
6e019995
GM
8423 {
8424 echo_area_buffer[0] = Qnil;
8425 message_cleared_p = 1;
8426 }
2311178e 8427
c6e89d6c
GM
8428 if (last_displayed_p)
8429 echo_area_buffer[1] = Qnil;
2311178e 8430
c6e89d6c
GM
8431 message_buf_print = 0;
8432}
8433
8434/* Clear garbaged frames.
8435
8436 This function is used where the old redisplay called
8437 redraw_garbaged_frames which in turn called redraw_frame which in
8438 turn called clear_frame. The call to clear_frame was a source of
8439 flickering. I believe a clear_frame is not necessary. It should
8440 suffice in the new redisplay to invalidate all current matrices,
8441 and ensure a complete redisplay of all windows. */
8442
8443static void
8444clear_garbaged_frames ()
8445{
5f5c8ee5
GM
8446 if (frame_garbaged)
8447 {
5f5c8ee5 8448 Lisp_Object tail, frame;
5fb96e96 8449 int changed_count = 0;
2311178e 8450
5f5c8ee5
GM
8451 FOR_EACH_FRAME (tail, frame)
8452 {
8453 struct frame *f = XFRAME (frame);
2311178e 8454
5f5c8ee5
GM
8455 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8456 {
6bb95882 8457 if (f->resized_p)
6d925726
KS
8458 {
8459 Fredraw_frame (frame);
8460 f->force_flush_display_p = 1;
8461 }
5f5c8ee5 8462 clear_current_matrices (f);
5fb96e96 8463 changed_count++;
5f5c8ee5 8464 f->garbaged = 0;
6bb95882 8465 f->resized_p = 0;
5f5c8ee5
GM
8466 }
8467 }
8468
8469 frame_garbaged = 0;
5fb96e96
RS
8470 if (changed_count)
8471 ++windows_or_buffers_changed;
5f5c8ee5 8472 }
c6e89d6c 8473}
5f5c8ee5 8474
5f5c8ee5 8475
886bd6f2
GM
8476/* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8477 is non-zero update selected_frame. Value is non-zero if the
c6e89d6c 8478 mini-windows height has been changed. */
5f5c8ee5 8479
c6e89d6c
GM
8480static int
8481echo_area_display (update_frame_p)
8482 int update_frame_p;
8483{
8484 Lisp_Object mini_window;
8485 struct window *w;
8486 struct frame *f;
8487 int window_height_changed_p = 0;
886bd6f2 8488 struct frame *sf = SELECTED_FRAME ();
c6e89d6c 8489
886bd6f2 8490 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
8491 w = XWINDOW (mini_window);
8492 f = XFRAME (WINDOW_FRAME (w));
8493
8494 /* Don't display if frame is invisible or not yet initialized. */
8495 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8496 return 0;
5f5c8ee5 8497
1a578e9b 8498/* The terminal frame is used as the first Emacs frame on the Mac OS. */
e0f712ba 8499#ifndef MAC_OS8
1ab3e082 8500#ifdef HAVE_WINDOW_SYSTEM
c6e89d6c
GM
8501 /* When Emacs starts, selected_frame may be a visible terminal
8502 frame, even if we run under a window system. If we let this
8503 through, a message would be displayed on the terminal. */
2311178e 8504 if (EQ (selected_frame, Vterminal_frame)
622e3754 8505 && !NILP (Vwindow_system))
c6e89d6c 8506 return 0;
1ab3e082 8507#endif /* HAVE_WINDOW_SYSTEM */
1a578e9b 8508#endif
c6e89d6c
GM
8509
8510 /* Redraw garbaged frames. */
8511 if (frame_garbaged)
8512 clear_garbaged_frames ();
8513
8514 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8515 {
8516 echo_area_window = mini_window;
8517 window_height_changed_p = display_echo_area (w);
5f5c8ee5 8518 w->must_be_updated_p = 1;
c59c668a 8519
d4358b37
GM
8520 /* Update the display, unless called from redisplay_internal.
8521 Also don't update the screen during redisplay itself. The
8522 update will happen at the end of redisplay, and an update
8523 here could cause confusion. */
8524 if (update_frame_p && !redisplaying_p)
5f5c8ee5 8525 {
715e84c9 8526 int n = 0;
edc68111 8527
715e84c9
GM
8528 /* If the display update has been interrupted by pending
8529 input, update mode lines in the frame. Due to the
8530 pending input, it might have been that redisplay hasn't
8531 been called, so that mode lines above the echo area are
8532 garbaged. This looks odd, so we prevent it here. */
8533 if (!display_completed)
8534 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
2311178e 8535
8af1308e
GM
8536 if (window_height_changed_p
8537 /* Don't do this if Emacs is shutting down. Redisplay
8538 needs to run hooks. */
8539 && !NILP (Vrun_hooks))
c59c668a 8540 {
c06017fb
GM
8541 /* Must update other windows. Likewise as in other
8542 cases, don't let this update be interrupted by
8543 pending input. */
331379bf 8544 int count = SPECPDL_INDEX ();
c06017fb 8545 specbind (Qredisplay_dont_pause, Qt);
edc68111 8546 windows_or_buffers_changed = 1;
c59c668a 8547 redisplay_internal (0);
c06017fb 8548 unbind_to (count, Qnil);
c59c668a 8549 }
715e84c9 8550 else if (FRAME_WINDOW_P (f) && n == 0)
5f5c8ee5 8551 {
edc68111 8552 /* Window configuration is the same as before.
715e84c9
GM
8553 Can do with a display update of the echo area,
8554 unless we displayed some mode lines. */
5f5c8ee5
GM
8555 update_single_window (w, 1);
8556 rif->flush_display (f);
8557 }
8558 else
8559 update_frame (f, 1, 1);
31b6671b
GM
8560
8561 /* If cursor is in the echo area, make sure that the next
8562 redisplay displays the minibuffer, so that the cursor will
8563 be replaced with what the minibuffer wants. */
8564 if (cursor_in_echo_area)
8565 ++windows_or_buffers_changed;
5f5c8ee5
GM
8566 }
8567 }
8568 else if (!EQ (mini_window, selected_window))
8569 windows_or_buffers_changed++;
c59c668a 8570
a8a1c0ee 8571 /* The current message is now also the last one displayed. */
dd2eb166 8572 echo_area_buffer[1] = echo_area_buffer[0];
2311178e 8573
5f5c8ee5
GM
8574 /* Prevent redisplay optimization in redisplay_internal by resetting
8575 this_line_start_pos. This is done because the mini-buffer now
8576 displays the message instead of its buffer text. */
8577 if (EQ (mini_window, selected_window))
8578 CHARPOS (this_line_start_pos) = 0;
c6e89d6c
GM
8579
8580 return window_height_changed_p;
5f5c8ee5
GM
8581}
8582
8583
8584\f
8585/***********************************************************************
4c0e4b80 8586 Mode Lines and Frame Titles
5f5c8ee5
GM
8587 ***********************************************************************/
8588
4c0e4b80
KS
8589/* A buffer for constructing non-propertized mode-line strings and
8590 frame titles in it; allocated from the heap in init_xdisp and
8591 resized as needed in store_mode_line_noprop_char. */
5f5c8ee5 8592
4c0e4b80 8593static char *mode_line_noprop_buf;
5f5c8ee5 8594
4c0e4b80 8595/* The buffer's end, and a current output position in it. */
5f5c8ee5 8596
4c0e4b80
KS
8597static char *mode_line_noprop_buf_end;
8598static char *mode_line_noprop_ptr;
5f5c8ee5 8599
d06e3847
KS
8600#define MODE_LINE_NOPROP_LEN(start) \
8601 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8602
4c0e4b80
KS
8603static enum {
8604 MODE_LINE_DISPLAY = 0,
8605 MODE_LINE_TITLE,
8606 MODE_LINE_NOPROP,
8607 MODE_LINE_STRING
8608} mode_line_target;
8609
8610/* Alist that caches the results of :propertize.
8611 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8612static Lisp_Object mode_line_proptrans_alist;
8613
8614/* List of strings making up the mode-line. */
8615static Lisp_Object mode_line_string_list;
8616
8617/* Base face property when building propertized mode line string. */
8618static Lisp_Object mode_line_string_face;
8619static Lisp_Object mode_line_string_face_prop;
8620
8621
8622/* Unwind data for mode line strings */
8623
8624static Lisp_Object Vmode_line_unwind_vector;
8625
8626static Lisp_Object
f5b8d5f2 8627format_mode_line_unwind_data (obuf, save_proptrans)
4c0e4b80
KS
8628 struct buffer *obuf;
8629{
4c0e4b80
KS
8630 Lisp_Object vector;
8631
8632 /* Reduce consing by keeping one vector in
8633 Vwith_echo_area_save_vector. */
8634 vector = Vmode_line_unwind_vector;
8635 Vmode_line_unwind_vector = Qnil;
8636
8637 if (NILP (vector))
8638 vector = Fmake_vector (make_number (7), Qnil);
8639
8640 AREF (vector, 0) = make_number (mode_line_target);
d06e3847 8641 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
4c0e4b80 8642 AREF (vector, 2) = mode_line_string_list;
f5b8d5f2 8643 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
4c0e4b80
KS
8644 AREF (vector, 4) = mode_line_string_face;
8645 AREF (vector, 5) = mode_line_string_face_prop;
8646
8647 if (obuf)
8648 XSETBUFFER (AREF (vector, 6), obuf);
8649 else
8650 AREF (vector, 6) = Qnil;
8651
8652 return vector;
8653}
8654
8655static Lisp_Object
8656unwind_format_mode_line (vector)
8657 Lisp_Object vector;
8658{
8659 mode_line_target = XINT (AREF (vector, 0));
8660 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8661 mode_line_string_list = AREF (vector, 2);
f5b8d5f2
RS
8662 if (! EQ (AREF (vector, 3), Qt))
8663 mode_line_proptrans_alist = AREF (vector, 3);
4c0e4b80
KS
8664 mode_line_string_face = AREF (vector, 4);
8665 mode_line_string_face_prop = AREF (vector, 5);
5f5c8ee5 8666
4c0e4b80
KS
8667 if (!NILP (AREF (vector, 6)))
8668 {
8669 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8670 AREF (vector, 6) = Qnil;
8671 }
5f5c8ee5 8672
4c0e4b80
KS
8673 Vmode_line_unwind_vector = vector;
8674 return Qnil;
8675}
5f5c8ee5 8676
4c0e4b80
KS
8677
8678/* Store a single character C for the frame title in mode_line_noprop_buf.
8679 Re-allocate mode_line_noprop_buf if necessary. */
5f5c8ee5
GM
8680
8681static void
5d15cc8f 8682#ifdef PROTOTYPES
4c0e4b80 8683store_mode_line_noprop_char (char c)
5d15cc8f 8684#else
4c0e4b80 8685store_mode_line_noprop_char (c)
5f5c8ee5 8686 char c;
5d15cc8f 8687#endif
5f5c8ee5
GM
8688{
8689 /* If output position has reached the end of the allocated buffer,
8690 double the buffer's size. */
4c0e4b80 8691 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
5f5c8ee5 8692 {
d06e3847 8693 int len = MODE_LINE_NOPROP_LEN (0);
4c0e4b80
KS
8694 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8695 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8696 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8697 mode_line_noprop_ptr = mode_line_noprop_buf + len;
5f5c8ee5
GM
8698 }
8699
4c0e4b80 8700 *mode_line_noprop_ptr++ = c;
5f5c8ee5
GM
8701}
8702
8703
4c0e4b80
KS
8704/* Store part of a frame title in mode_line_noprop_buf, beginning at
8705 mode_line_noprop_ptr. STR is the string to store. Do not copy
d26b89b8
KH
8706 characters that yield more columns than PRECISION; PRECISION <= 0
8707 means copy the whole string. Pad with spaces until FIELD_WIDTH
8708 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8709 pad. Called from display_mode_element when it is used to build a
8710 frame title. */
5f5c8ee5
GM
8711
8712static int
4c0e4b80 8713store_mode_line_noprop (str, field_width, precision)
50f80c2f 8714 const unsigned char *str;
5f5c8ee5
GM
8715 int field_width, precision;
8716{
8717 int n = 0;
3b552d56 8718 int dummy, nbytes;
5f5c8ee5
GM
8719
8720 /* Copy at most PRECISION chars from STR. */
d26b89b8 8721 nbytes = strlen (str);
7a6b6cce 8722 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
d26b89b8 8723 while (nbytes--)
4c0e4b80 8724 store_mode_line_noprop_char (*str++);
5f5c8ee5
GM
8725
8726 /* Fill up with spaces until FIELD_WIDTH reached. */
8727 while (field_width > 0
8728 && n < field_width)
8729 {
4c0e4b80 8730 store_mode_line_noprop_char (' ');
5f5c8ee5
GM
8731 ++n;
8732 }
8733
8734 return n;
8735}
8736
4c0e4b80
KS
8737/***********************************************************************
8738 Frame Titles
8739 ***********************************************************************/
8740
8143e6ab 8741#ifdef HAVE_WINDOW_SYSTEM
5f5c8ee5
GM
8742
8743/* Set the title of FRAME, if it has changed. The title format is
8744 Vicon_title_format if FRAME is iconified, otherwise it is
8745 frame_title_format. */
8746
8747static void
8748x_consider_frame_title (frame)
8749 Lisp_Object frame;
8750{
8751 struct frame *f = XFRAME (frame);
8752
8753 if (FRAME_WINDOW_P (f)
8754 || FRAME_MINIBUF_ONLY_P (f)
8755 || f->explicit_name)
8756 {
8757 /* Do we have more than one visible frame on this X display? */
8758 Lisp_Object tail;
8759 Lisp_Object fmt;
d06e3847
KS
8760 int title_start;
8761 char *title;
5f5c8ee5
GM
8762 int len;
8763 struct it it;
4c0e4b80 8764 int count = SPECPDL_INDEX ();
5f5c8ee5 8765
9472f927 8766 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
5f5c8ee5 8767 {
74779f52
JR
8768 Lisp_Object other_frame = XCAR (tail);
8769 struct frame *tf = XFRAME (other_frame);
5f5c8ee5 8770
2311178e 8771 if (tf != f
5f5c8ee5
GM
8772 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8773 && !FRAME_MINIBUF_ONLY_P (tf)
74779f52 8774 && !EQ (other_frame, tip_frame)
5f5c8ee5
GM
8775 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8776 break;
8777 }
8778
8779 /* Set global variable indicating that multiple frames exist. */
8780 multiple_frames = CONSP (tail);
8781
8782 /* Switch to the buffer of selected window of the frame. Set up
53088ce8
KS
8783 mode_line_target so that display_mode_element will output into
8784 mode_line_noprop_buf; then display the title. */
4c0e4b80 8785 record_unwind_protect (unwind_format_mode_line,
f5b8d5f2 8786 format_mode_line_unwind_data (current_buffer, 0));
4c0e4b80 8787
bb336f8d 8788 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
5f5c8ee5 8789 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
4c0e4b80
KS
8790
8791 mode_line_target = MODE_LINE_TITLE;
d06e3847 8792 title_start = MODE_LINE_NOPROP_LEN (0);
5f5c8ee5
GM
8793 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8794 NULL, DEFAULT_FACE_ID);
c53a1624 8795 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
d06e3847
KS
8796 len = MODE_LINE_NOPROP_LEN (title_start);
8797 title = mode_line_noprop_buf + title_start;
4c0e4b80 8798 unbind_to (count, Qnil);
5f5c8ee5
GM
8799
8800 /* Set the title only if it's changed. This avoids consing in
8801 the common case where it hasn't. (If it turns out that we've
8802 already wasted too much time by walking through the list with
8803 display_mode_element, then we might need to optimize at a
8804 higher level than this.) */
2311178e 8805 if (! STRINGP (f->name)
2051c264 8806 || SBYTES (f->name) != len
d06e3847
KS
8807 || bcmp (title, SDATA (f->name), len) != 0)
8808 x_implicitly_set_name (f, make_string (title, len), Qnil);
5f5c8ee5
GM
8809 }
8810}
8811
5f5c8ee5
GM
8812#endif /* not HAVE_WINDOW_SYSTEM */
8813
8814
8815
8816\f
8817/***********************************************************************
8818 Menu Bars
8819 ***********************************************************************/
8820
8821
8822/* Prepare for redisplay by updating menu-bar item lists when
8823 appropriate. This can call eval. */
8824
8825void
8826prepare_menu_bars ()
8827{
8828 int all_windows;
8829 struct gcpro gcpro1, gcpro2;
8830 struct frame *f;
6b5c4794 8831 Lisp_Object tooltip_frame;
5f5c8ee5 8832
86ffe5cd 8833#ifdef HAVE_WINDOW_SYSTEM
5f5c8ee5
GM
8834 tooltip_frame = tip_frame;
8835#else
6b5c4794 8836 tooltip_frame = Qnil;
5f5c8ee5
GM
8837#endif
8838
8839 /* Update all frame titles based on their buffer names, etc. We do
8840 this before the menu bars so that the buffer-menu will show the
8841 up-to-date frame titles. */
8842#ifdef HAVE_WINDOW_SYSTEM
8843 if (windows_or_buffers_changed || update_mode_lines)
8844 {
8845 Lisp_Object tail, frame;
8846
8847 FOR_EACH_FRAME (tail, frame)
8848 {
8849 f = XFRAME (frame);
6b5c4794 8850 if (!EQ (frame, tooltip_frame)
5f5c8ee5
GM
8851 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8852 x_consider_frame_title (frame);
8853 }
8854 }
8855#endif /* HAVE_WINDOW_SYSTEM */
8856
8857 /* Update the menu bar item lists, if appropriate. This has to be
8858 done before any actual redisplay or generation of display lines. */
2311178e 8859 all_windows = (update_mode_lines
5f5c8ee5
GM
8860 || buffer_shared > 1
8861 || windows_or_buffers_changed);
8862 if (all_windows)
8863 {
8864 Lisp_Object tail, frame;
331379bf 8865 int count = SPECPDL_INDEX ();
5f5c8ee5 8866
89f2614d 8867 record_unwind_save_match_data ();
5f5c8ee5
GM
8868
8869 FOR_EACH_FRAME (tail, frame)
8870 {
8871 f = XFRAME (frame);
8872
8873 /* Ignore tooltip frame. */
6b5c4794 8874 if (EQ (frame, tooltip_frame))
5f5c8ee5 8875 continue;
2311178e 8876
5f5c8ee5
GM
8877 /* If a window on this frame changed size, report that to
8878 the user and clear the size-change flag. */
8879 if (FRAME_WINDOW_SIZES_CHANGED (f))
8880 {
8881 Lisp_Object functions;
2311178e 8882
5f5c8ee5
GM
8883 /* Clear flag first in case we get an error below. */
8884 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8885 functions = Vwindow_size_change_functions;
8886 GCPRO2 (tail, functions);
2311178e 8887
5f5c8ee5
GM
8888 while (CONSP (functions))
8889 {
8890 call1 (XCAR (functions), frame);
8891 functions = XCDR (functions);
8892 }
8893 UNGCPRO;
8894 }
2311178e 8895
5f5c8ee5
GM
8896 GCPRO1 (tail);
8897 update_menu_bar (f, 0);
8898#ifdef HAVE_WINDOW_SYSTEM
e037b9ec 8899 update_tool_bar (f, 0);
5f5c8ee5
GM
8900#endif
8901 UNGCPRO;
8902 }
8903
8904 unbind_to (count, Qnil);
8905 }
8906 else
8907 {
886bd6f2
GM
8908 struct frame *sf = SELECTED_FRAME ();
8909 update_menu_bar (sf, 1);
5f5c8ee5 8910#ifdef HAVE_WINDOW_SYSTEM
886bd6f2 8911 update_tool_bar (sf, 1);
5f5c8ee5
GM
8912#endif
8913 }
8914
8915 /* Motif needs this. See comment in xmenu.c. Turn it off when
8916 pending_menu_activation is not defined. */
8917#ifdef USE_X_TOOLKIT
8918 pending_menu_activation = 0;
8919#endif
8920}
8921
8922
8923/* Update the menu bar item list for frame F. This has to be done
8924 before we start to fill in any display lines, because it can call
8925 eval.
8926
8927 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8928
8929static void
8930update_menu_bar (f, save_match_data)
8931 struct frame *f;
8932 int save_match_data;
8933{
8934 Lisp_Object window;
8935 register struct window *w;
8936
e1477f43
GM
8937 /* If called recursively during a menu update, do nothing. This can
8938 happen when, for instance, an activate-menubar-hook causes a
8939 redisplay. */
8940 if (inhibit_menubar_update)
8941 return;
8942
5f5c8ee5
GM
8943 window = FRAME_SELECTED_WINDOW (f);
8944 w = XWINDOW (window);
2311178e 8945
84f2e615 8946#if 0 /* The if statement below this if statement used to include the
a5f08374
RS
8947 condition !NILP (w->update_mode_line), rather than using
8948 update_mode_lines directly, and this if statement may have
8949 been added to make that condition work. Now the if
2311178e 8950 statement below matches its comment, this isn't needed. */
5f5c8ee5
GM
8951 if (update_mode_lines)
8952 w->update_mode_line = Qt;
a5f08374 8953#endif
5f5c8ee5
GM
8954
8955 if (FRAME_WINDOW_P (f)
8956 ?
488dd4c4
JD
8957#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8958 || defined (USE_GTK)
2311178e 8959 FRAME_EXTERNAL_MENU_BAR (f)
5f5c8ee5
GM
8960#else
8961 FRAME_MENU_BAR_LINES (f) > 0
8962#endif
8963 : FRAME_MENU_BAR_LINES (f) > 0)
8964 {
8965 /* If the user has switched buffers or windows, we need to
8966 recompute to reflect the new bindings. But we'll
8967 recompute when update_mode_lines is set too; that means
8968 that people can use force-mode-line-update to request
8969 that the menu bar be recomputed. The adverse effect on
8970 the rest of the redisplay algorithm is about the same as
8971 windows_or_buffers_changed anyway. */
8972 if (windows_or_buffers_changed
a5f08374
RS
8973 /* This used to test w->update_mode_line, but we believe
8974 there is no need to recompute the menu in that case. */
8975 || update_mode_lines
5f5c8ee5
GM
8976 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8977 < BUF_MODIFF (XBUFFER (w->buffer)))
8978 != !NILP (w->last_had_star))
8979 || ((!NILP (Vtransient_mark_mode)
8980 && !NILP (XBUFFER (w->buffer)->mark_active))
8981 != !NILP (w->region_showing)))
8982 {
8983 struct buffer *prev = current_buffer;
331379bf 8984 int count = SPECPDL_INDEX ();
5f5c8ee5 8985
e1477f43
GM
8986 specbind (Qinhibit_menubar_update, Qt);
8987
5f5c8ee5
GM
8988 set_buffer_internal_1 (XBUFFER (w->buffer));
8989 if (save_match_data)
89f2614d 8990 record_unwind_save_match_data ();
5f5c8ee5
GM
8991 if (NILP (Voverriding_local_map_menu_flag))
8992 {
8993 specbind (Qoverriding_terminal_local_map, Qnil);
8994 specbind (Qoverriding_local_map, Qnil);
8995 }
8996
8997 /* Run the Lucid hook. */
65048e97 8998 safe_run_hooks (Qactivate_menubar_hook);
2311178e 8999
5f5c8ee5
GM
9000 /* If it has changed current-menubar from previous value,
9001 really recompute the menu-bar from the value. */
9002 if (! NILP (Vlucid_menu_bar_dirty_flag))
9003 call0 (Qrecompute_lucid_menubar);
2311178e 9004
5f5c8ee5
GM
9005 safe_run_hooks (Qmenu_bar_update_hook);
9006 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
2311178e 9007
5f5c8ee5 9008 /* Redisplay the menu bar in case we changed it. */
488dd4c4
JD
9009#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9010 || defined (USE_GTK)
1a578e9b 9011 if (FRAME_WINDOW_P (f)
e0f712ba 9012#if defined (MAC_OS)
1a578e9b
AC
9013 /* All frames on Mac OS share the same menubar. So only the
9014 selected frame should be allowed to set it. */
9015 && f == SELECTED_FRAME ()
9016#endif
9017 )
5f5c8ee5
GM
9018 set_frame_menubar (f, 0, 0);
9019 else
9020 /* On a terminal screen, the menu bar is an ordinary screen
9021 line, and this makes it get updated. */
9022 w->update_mode_line = Qt;
488dd4c4 9023#else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
5f5c8ee5
GM
9024 /* In the non-toolkit version, the menu bar is an ordinary screen
9025 line, and this makes it get updated. */
9026 w->update_mode_line = Qt;
488dd4c4 9027#endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
5f5c8ee5
GM
9028
9029 unbind_to (count, Qnil);
9030 set_buffer_internal_1 (prev);
9031 }
9032 }
9033}
9034
9035
9036\f
fa3c6b4d
KS
9037/***********************************************************************
9038 Output Cursor
9039 ***********************************************************************/
9040
1dabd0cf
KS
9041#ifdef HAVE_WINDOW_SYSTEM
9042
fa3c6b4d
KS
9043/* EXPORT:
9044 Nominal cursor position -- where to draw output.
9045 HPOS and VPOS are window relative glyph matrix coordinates.
9046 X and Y are window relative pixel coordinates. */
9047
9048struct cursor_pos output_cursor;
9049
9050
9051/* EXPORT:
9052 Set the global variable output_cursor to CURSOR. All cursor
9053 positions are relative to updated_window. */
9054
9055void
9056set_output_cursor (cursor)
9057 struct cursor_pos *cursor;
9058{
9059 output_cursor.hpos = cursor->hpos;
9060 output_cursor.vpos = cursor->vpos;
9061 output_cursor.x = cursor->x;
9062 output_cursor.y = cursor->y;
9063}
9064
9065
9066/* EXPORT for RIF:
9067 Set a nominal cursor position.
9068
9069 HPOS and VPOS are column/row positions in a window glyph matrix. X
9070 and Y are window text area relative pixel positions.
9071
9072 If this is done during an update, updated_window will contain the
9073 window that is being updated and the position is the future output
9074 cursor position for that window. If updated_window is null, use
9075 selected_window and display the cursor at the given position. */
9076
9077void
9078x_cursor_to (vpos, hpos, y, x)
9079 int vpos, hpos, y, x;
9080{
9081 struct window *w;
9082
9083 /* If updated_window is not set, work on selected_window. */
9084 if (updated_window)
9085 w = updated_window;
9086 else
9087 w = XWINDOW (selected_window);
9088
9089 /* Set the output cursor. */
9090 output_cursor.hpos = hpos;
9091 output_cursor.vpos = vpos;
9092 output_cursor.x = x;
9093 output_cursor.y = y;
9094
9095 /* If not called as part of an update, really display the cursor.
9096 This will also set the cursor position of W. */
9097 if (updated_window == NULL)
9098 {
9099 BLOCK_INPUT;
9100 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9101 if (rif->flush_display_optional)
9102 rif->flush_display_optional (SELECTED_FRAME ());
9103 UNBLOCK_INPUT;
9104 }
9105}
9106
1dabd0cf 9107#endif /* HAVE_WINDOW_SYSTEM */
fa3c6b4d
KS
9108
9109\f
5f5c8ee5 9110/***********************************************************************
e037b9ec 9111 Tool-bars
5f5c8ee5
GM
9112 ***********************************************************************/
9113
9114#ifdef HAVE_WINDOW_SYSTEM
9115
fa3c6b4d
KS
9116/* Where the mouse was last time we reported a mouse event. */
9117
9118FRAME_PTR last_mouse_frame;
9119
9120/* Tool-bar item index of the item on which a mouse button was pressed
9121 or -1. */
9122
9123int last_tool_bar_item;
9124
9125
e037b9ec 9126/* Update the tool-bar item list for frame F. This has to be done
5f5c8ee5
GM
9127 before we start to fill in any display lines. Called from
9128 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9129 and restore it here. */
9130
9131static void
e037b9ec 9132update_tool_bar (f, save_match_data)
5f5c8ee5
GM
9133 struct frame *f;
9134 int save_match_data;
9135{
488dd4c4 9136#ifdef USE_GTK
810f2256 9137 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
488dd4c4
JD
9138#else
9139 int do_update = WINDOWP (f->tool_bar_window)
da8b7f4f 9140 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
488dd4c4
JD
9141#endif
9142
9143 if (do_update)
5f5c8ee5
GM
9144 {
9145 Lisp_Object window;
9146 struct window *w;
9147
9148 window = FRAME_SELECTED_WINDOW (f);
9149 w = XWINDOW (window);
2311178e 9150
5f5c8ee5
GM
9151 /* If the user has switched buffers or windows, we need to
9152 recompute to reflect the new bindings. But we'll
9153 recompute when update_mode_lines is set too; that means
9154 that people can use force-mode-line-update to request
9155 that the menu bar be recomputed. The adverse effect on
9156 the rest of the redisplay algorithm is about the same as
9157 windows_or_buffers_changed anyway. */
9158 if (windows_or_buffers_changed
9159 || !NILP (w->update_mode_line)
84f2e615 9160 || update_mode_lines
5f5c8ee5
GM
9161 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9162 < BUF_MODIFF (XBUFFER (w->buffer)))
9163 != !NILP (w->last_had_star))
9164 || ((!NILP (Vtransient_mark_mode)
9165 && !NILP (XBUFFER (w->buffer)->mark_active))
9166 != !NILP (w->region_showing)))
9167 {
9168 struct buffer *prev = current_buffer;
331379bf 9169 int count = SPECPDL_INDEX ();
12e6566a
JD
9170 Lisp_Object new_tool_bar;
9171 int new_n_tool_bar;
84f2e615 9172 struct gcpro gcpro1;
a2889657 9173
5f5c8ee5
GM
9174 /* Set current_buffer to the buffer of the selected
9175 window of the frame, so that we get the right local
9176 keymaps. */
9177 set_buffer_internal_1 (XBUFFER (w->buffer));
1f40cad2 9178
5f5c8ee5
GM
9179 /* Save match data, if we must. */
9180 if (save_match_data)
89f2614d 9181 record_unwind_save_match_data ();
5f5c8ee5
GM
9182
9183 /* Make sure that we don't accidentally use bogus keymaps. */
9184 if (NILP (Voverriding_local_map_menu_flag))
9185 {
9186 specbind (Qoverriding_terminal_local_map, Qnil);
9187 specbind (Qoverriding_local_map, Qnil);
1f40cad2 9188 }
1f40cad2 9189
12e6566a 9190 GCPRO1 (new_tool_bar);
84f2e615 9191
e037b9ec 9192 /* Build desired tool-bar items from keymaps. */
12e6566a
JD
9193 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9194 &new_n_tool_bar);
2311178e 9195
84f2e615 9196 /* Redisplay the tool-bar if we changed it. */
12e6566a
JD
9197 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9198 {
9199 /* Redisplay that happens asynchronously due to an expose event
9200 may access f->tool_bar_items. Make sure we update both
9201 variables within BLOCK_INPUT so no such event interrupts. */
9202 BLOCK_INPUT;
9203 f->tool_bar_items = new_tool_bar;
9204 f->n_tool_bar_items = new_n_tool_bar;
9205 w->update_mode_line = Qt;
9206 UNBLOCK_INPUT;
9207 }
be21b925 9208
1e802fb5 9209 UNGCPRO;
5f5c8ee5
GM
9210
9211 unbind_to (count, Qnil);
9212 set_buffer_internal_1 (prev);
81d478f3 9213 }
a2889657
JB
9214 }
9215}
9216
6c4429a5 9217
e037b9ec 9218/* Set F->desired_tool_bar_string to a Lisp string representing frame
7464726e 9219 F's desired tool-bar contents. F->tool_bar_items must have
5f5c8ee5
GM
9220 been set up previously by calling prepare_menu_bars. */
9221
a2889657 9222static void
e037b9ec 9223build_desired_tool_bar_string (f)
5f5c8ee5 9224 struct frame *f;
a2889657 9225{
a23887b9 9226 int i, size, size_needed;
5f5c8ee5
GM
9227 struct gcpro gcpro1, gcpro2, gcpro3;
9228 Lisp_Object image, plist, props;
a2889657 9229
5f5c8ee5
GM
9230 image = plist = props = Qnil;
9231 GCPRO3 (image, plist, props);
a2889657 9232
e037b9ec 9233 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
5f5c8ee5 9234 Otherwise, make a new string. */
2311178e 9235
5f5c8ee5 9236 /* The size of the string we might be able to reuse. */
e037b9ec 9237 size = (STRINGP (f->desired_tool_bar_string)
2051c264 9238 ? SCHARS (f->desired_tool_bar_string)
5f5c8ee5
GM
9239 : 0);
9240
b94c0d9c 9241 /* We need one space in the string for each image. */
a23887b9 9242 size_needed = f->n_tool_bar_items;
2311178e 9243
b94c0d9c 9244 /* Reuse f->desired_tool_bar_string, if possible. */
cb2ddc53 9245 if (size < size_needed || NILP (f->desired_tool_bar_string))
6fc556fd
KR
9246 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9247 make_number (' '));
5f5c8ee5
GM
9248 else
9249 {
9250 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9251 Fremove_text_properties (make_number (0), make_number (size),
e037b9ec 9252 props, f->desired_tool_bar_string);
5f5c8ee5 9253 }
a2889657 9254
5f5c8ee5 9255 /* Put a `display' property on the string for the images to display,
e037b9ec
GM
9256 put a `menu_item' property on tool-bar items with a value that
9257 is the index of the item in F's tool-bar item vector. */
a23887b9 9258 for (i = 0; i < f->n_tool_bar_items; ++i)
a2889657 9259 {
7464726e 9260#define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
5f5c8ee5 9261
e037b9ec
GM
9262 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9263 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
35a41507 9264 int hmargin, vmargin, relief, idx, end;
493fdc3c 9265 extern Lisp_Object QCrelief, QCmargin, QCconversion;
5f5c8ee5
GM
9266
9267 /* If image is a vector, choose the image according to the
9268 button state. */
e037b9ec 9269 image = PROP (TOOL_BAR_ITEM_IMAGES);
5f5c8ee5
GM
9270 if (VECTORP (image))
9271 {
5f5c8ee5
GM
9272 if (enabled_p)
9273 idx = (selected_p
e037b9ec
GM
9274 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9275 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
5f5c8ee5
GM
9276 else
9277 idx = (selected_p
e037b9ec
GM
9278 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9279 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
2311178e 9280
37e4e482
GM
9281 xassert (ASIZE (image) >= idx);
9282 image = AREF (image, idx);
5f5c8ee5 9283 }
37e4e482
GM
9284 else
9285 idx = -1;
5f5c8ee5
GM
9286
9287 /* Ignore invalid image specifications. */
9288 if (!valid_image_p (image))
9289 continue;
9290
e037b9ec 9291 /* Display the tool-bar button pressed, or depressed. */
5f5c8ee5
GM
9292 plist = Fcopy_sequence (XCDR (image));
9293
9294 /* Compute margin and relief to draw. */
9f864bf9 9295 relief = (tool_bar_button_relief >= 0
c3d76173
GM
9296 ? tool_bar_button_relief
9297 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
35a41507
GM
9298 hmargin = vmargin = relief;
9299
9300 if (INTEGERP (Vtool_bar_button_margin)
9301 && XINT (Vtool_bar_button_margin) > 0)
9302 {
9303 hmargin += XFASTINT (Vtool_bar_button_margin);
9304 vmargin += XFASTINT (Vtool_bar_button_margin);
9305 }
9306 else if (CONSP (Vtool_bar_button_margin))
9307 {
9308 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9309 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9310 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
2311178e 9311
35a41507
GM
9312 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9313 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9314 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9315 }
2311178e 9316
e037b9ec 9317 if (auto_raise_tool_bar_buttons_p)
5f5c8ee5
GM
9318 {
9319 /* Add a `:relief' property to the image spec if the item is
9320 selected. */
9321 if (selected_p)
9322 {
9323 plist = Fplist_put (plist, QCrelief, make_number (-relief));
35a41507
GM
9324 hmargin -= relief;
9325 vmargin -= relief;
5f5c8ee5
GM
9326 }
9327 }
9328 else
9329 {
9330 /* If image is selected, display it pressed, i.e. with a
9331 negative relief. If it's not selected, display it with a
9332 raised relief. */
9333 plist = Fplist_put (plist, QCrelief,
9334 (selected_p
9335 ? make_number (-relief)
9336 : make_number (relief)));
35a41507
GM
9337 hmargin -= relief;
9338 vmargin -= relief;
5f5c8ee5
GM
9339 }
9340
9341 /* Put a margin around the image. */
35a41507
GM
9342 if (hmargin || vmargin)
9343 {
9344 if (hmargin == vmargin)
9345 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9346 else
9347 plist = Fplist_put (plist, QCmargin,
9348 Fcons (make_number (hmargin),
9349 make_number (vmargin)));
9350 }
2311178e 9351
37e4e482
GM
9352 /* If button is not enabled, and we don't have special images
9353 for the disabled state, make the image appear disabled by
5f5c8ee5 9354 applying an appropriate algorithm to it. */
37e4e482 9355 if (!enabled_p && idx < 0)
ecd01a0e 9356 plist = Fplist_put (plist, QCconversion, Qdisabled);
2311178e 9357
5f5c8ee5
GM
9358 /* Put a `display' text property on the string for the image to
9359 display. Put a `menu-item' property on the string that gives
e037b9ec 9360 the start of this item's properties in the tool-bar items
5f5c8ee5
GM
9361 vector. */
9362 image = Fcons (Qimage, plist);
9363 props = list4 (Qdisplay, image,
a23887b9
GM
9364 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9365
9366 /* Let the last image hide all remaining spaces in the tool bar
9367 string. The string can be longer than needed when we reuse a
9368 previous string. */
9369 if (i + 1 == f->n_tool_bar_items)
2051c264 9370 end = SCHARS (f->desired_tool_bar_string);
a23887b9
GM
9371 else
9372 end = i + 1;
9373 Fadd_text_properties (make_number (i), make_number (end),
e037b9ec 9374 props, f->desired_tool_bar_string);
5f5c8ee5 9375#undef PROP
a2889657
JB
9376 }
9377
5f5c8ee5
GM
9378 UNGCPRO;
9379}
9380
9381
e037b9ec 9382/* Display one line of the tool-bar of frame IT->f. */
5f5c8ee5
GM
9383
9384static void
e037b9ec 9385display_tool_bar_line (it)
5f5c8ee5
GM
9386 struct it *it;
9387{
9388 struct glyph_row *row = it->glyph_row;
9389 int max_x = it->last_visible_x;
9390 struct glyph *last;
2311178e 9391
5f5c8ee5
GM
9392 prepare_desired_row (row);
9393 row->y = it->current_y;
90aa2856
GM
9394
9395 /* Note that this isn't made use of if the face hasn't a box,
9396 so there's no need to check the face here. */
9397 it->start_of_box_run_p = 1;
2311178e 9398
5f5c8ee5 9399 while (it->current_x < max_x)
a2889657 9400 {
5f5c8ee5 9401 int x_before, x, n_glyphs_before, i, nglyphs;
a2f016e3 9402
5f5c8ee5
GM
9403 /* Get the next display element. */
9404 if (!get_next_display_element (it))
9405 break;
73af359d 9406
5f5c8ee5
GM
9407 /* Produce glyphs. */
9408 x_before = it->current_x;
9409 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
9410 PRODUCE_GLYPHS (it);
daa37602 9411
5f5c8ee5
GM
9412 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
9413 i = 0;
9414 x = x_before;
9415 while (i < nglyphs)
9416 {
9417 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
2311178e 9418
5f5c8ee5
GM
9419 if (x + glyph->pixel_width > max_x)
9420 {
9421 /* Glyph doesn't fit on line. */
9422 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
9423 it->current_x = x;
9424 goto out;
9425 }
daa37602 9426
5f5c8ee5
GM
9427 ++it->hpos;
9428 x += glyph->pixel_width;
9429 ++i;
9430 }
9431
9432 /* Stop at line ends. */
9433 if (ITERATOR_AT_END_OF_LINE_P (it))
9434 break;
9435
cafafe0b 9436 set_iterator_to_next (it, 1);
a2889657 9437 }
a2889657 9438
5f5c8ee5 9439 out:;
a2889657 9440
5f5c8ee5
GM
9441 row->displays_text_p = row->used[TEXT_AREA] != 0;
9442 extend_face_to_end_of_line (it);
9443 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9444 last->right_box_line_p = 1;
90aa2856
GM
9445 if (last == row->glyphs[TEXT_AREA])
9446 last->left_box_line_p = 1;
5f5c8ee5 9447 compute_line_metrics (it);
2311178e 9448
e037b9ec 9449 /* If line is empty, make it occupy the rest of the tool-bar. */
5f5c8ee5
GM
9450 if (!row->displays_text_p)
9451 {
312246d1
GM
9452 row->height = row->phys_height = it->last_visible_y - row->y;
9453 row->ascent = row->phys_ascent = 0;
546f98b8 9454 row->extra_line_spacing = 0;
5f5c8ee5 9455 }
2311178e 9456
5f5c8ee5
GM
9457 row->full_width_p = 1;
9458 row->continued_p = 0;
9459 row->truncated_on_left_p = 0;
9460 row->truncated_on_right_p = 0;
9461
9462 it->current_x = it->hpos = 0;
9463 it->current_y += row->height;
9464 ++it->vpos;
9465 ++it->glyph_row;
a2889657 9466}
96a410bc 9467
5f5c8ee5 9468
e037b9ec 9469/* Value is the number of screen lines needed to make all tool-bar
5f5c8ee5 9470 items of frame F visible. */
96a410bc 9471
d39b6696 9472static int
e037b9ec 9473tool_bar_lines_needed (f)
5f5c8ee5 9474 struct frame *f;
d39b6696 9475{
e037b9ec 9476 struct window *w = XWINDOW (f->tool_bar_window);
5f5c8ee5 9477 struct it it;
2311178e 9478
e037b9ec
GM
9479 /* Initialize an iterator for iteration over
9480 F->desired_tool_bar_string in the tool-bar window of frame F. */
9481 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
5f5c8ee5 9482 it.first_visible_x = 0;
da8b7f4f 9483 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
e037b9ec 9484 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
5f5c8ee5
GM
9485
9486 while (!ITERATOR_AT_END_P (&it))
9487 {
9488 it.glyph_row = w->desired_matrix->rows;
9489 clear_glyph_row (it.glyph_row);
e037b9ec 9490 display_tool_bar_line (&it);
5f5c8ee5
GM
9491 }
9492
da8b7f4f 9493 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
d39b6696 9494}
96a410bc 9495
5f5c8ee5 9496
57c28064
GM
9497DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9498 0, 1, 0,
7ee72033
MB
9499 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9500 (frame)
57c28064
GM
9501 Lisp_Object frame;
9502{
9503 struct frame *f;
9504 struct window *w;
9505 int nlines = 0;
9506
9507 if (NILP (frame))
9508 frame = selected_frame;
9509 else
b7826503 9510 CHECK_FRAME (frame);
57c28064 9511 f = XFRAME (frame);
2311178e 9512
57c28064
GM
9513 if (WINDOWP (f->tool_bar_window)
9514 || (w = XWINDOW (f->tool_bar_window),
da8b7f4f 9515 WINDOW_TOTAL_LINES (w) > 0))
57c28064
GM
9516 {
9517 update_tool_bar (f, 1);
9518 if (f->n_tool_bar_items)
9519 {
9520 build_desired_tool_bar_string (f);
9521 nlines = tool_bar_lines_needed (f);
9522 }
9523 }
9524
9525 return make_number (nlines);
9526}
9527
9528
e037b9ec 9529/* Display the tool-bar of frame F. Value is non-zero if tool-bar's
5f5c8ee5
GM
9530 height should be changed. */
9531
9532static int
e037b9ec 9533redisplay_tool_bar (f)
5f5c8ee5 9534 struct frame *f;
96a410bc 9535{
5f5c8ee5
GM
9536 struct window *w;
9537 struct it it;
9538 struct glyph_row *row;
9539 int change_height_p = 0;
177c0ea7 9540
488dd4c4 9541#ifdef USE_GTK
810f2256 9542 if (FRAME_EXTERNAL_TOOL_BAR (f))
488dd4c4
JD
9543 update_frame_tool_bar (f);
9544 return 0;
9545#endif
2311178e 9546
e037b9ec
GM
9547 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9548 do anything. This means you must start with tool-bar-lines
5f5c8ee5 9549 non-zero to get the auto-sizing effect. Or in other words, you
e037b9ec
GM
9550 can turn off tool-bars by specifying tool-bar-lines zero. */
9551 if (!WINDOWP (f->tool_bar_window)
9552 || (w = XWINDOW (f->tool_bar_window),
da8b7f4f 9553 WINDOW_TOTAL_LINES (w) == 0))
5f5c8ee5 9554 return 0;
96a410bc 9555
e037b9ec
GM
9556 /* Set up an iterator for the tool-bar window. */
9557 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
5f5c8ee5 9558 it.first_visible_x = 0;
da8b7f4f 9559 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
5f5c8ee5 9560 row = it.glyph_row;
3450d04c 9561
e037b9ec
GM
9562 /* Build a string that represents the contents of the tool-bar. */
9563 build_desired_tool_bar_string (f);
9564 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
3450d04c 9565
e037b9ec 9566 /* Display as many lines as needed to display all tool-bar items. */
5f5c8ee5 9567 while (it.current_y < it.last_visible_y)
e037b9ec 9568 display_tool_bar_line (&it);
3450d04c 9569
e037b9ec 9570 /* It doesn't make much sense to try scrolling in the tool-bar
5f5c8ee5
GM
9571 window, so don't do it. */
9572 w->desired_matrix->no_scrolling_p = 1;
9573 w->must_be_updated_p = 1;
3450d04c 9574
e037b9ec 9575 if (auto_resize_tool_bars_p)
5f5c8ee5
GM
9576 {
9577 int nlines;
6e33e6f0
GM
9578
9579 /* If we couldn't display everything, change the tool-bar's
9580 height. */
9581 if (IT_STRING_CHARPOS (it) < it.end_charpos)
9582 change_height_p = 1;
2311178e 9583
5f5c8ee5
GM
9584 /* If there are blank lines at the end, except for a partially
9585 visible blank line at the end that is smaller than
da8b7f4f 9586 FRAME_LINE_HEIGHT, change the tool-bar's height. */
5f5c8ee5
GM
9587 row = it.glyph_row - 1;
9588 if (!row->displays_text_p
da8b7f4f 9589 && row->height >= FRAME_LINE_HEIGHT (f))
5f5c8ee5
GM
9590 change_height_p = 1;
9591
e037b9ec
GM
9592 /* If row displays tool-bar items, but is partially visible,
9593 change the tool-bar's height. */
5f5c8ee5
GM
9594 if (row->displays_text_p
9595 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
9596 change_height_p = 1;
9597
e037b9ec 9598 /* Resize windows as needed by changing the `tool-bar-lines'
5f5c8ee5
GM
9599 frame parameter. */
9600 if (change_height_p
e037b9ec 9601 && (nlines = tool_bar_lines_needed (f),
da8b7f4f 9602 nlines != WINDOW_TOTAL_LINES (w)))
5f5c8ee5 9603 {
e037b9ec 9604 extern Lisp_Object Qtool_bar_lines;
5f5c8ee5 9605 Lisp_Object frame;
da8b7f4f 9606 int old_height = WINDOW_TOTAL_LINES (w);
2311178e 9607
5f5c8ee5
GM
9608 XSETFRAME (frame, f);
9609 clear_glyph_matrix (w->desired_matrix);
9610 Fmodify_frame_parameters (frame,
e037b9ec 9611 Fcons (Fcons (Qtool_bar_lines,
5f5c8ee5
GM
9612 make_number (nlines)),
9613 Qnil));
da8b7f4f 9614 if (WINDOW_TOTAL_LINES (w) != old_height)
e85ee976 9615 fonts_changed_p = 1;
5f5c8ee5
GM
9616 }
9617 }
3450d04c 9618
5f5c8ee5 9619 return change_height_p;
96a410bc 9620}
90adcf20 9621
5f5c8ee5 9622
e037b9ec
GM
9623/* Get information about the tool-bar item which is displayed in GLYPH
9624 on frame F. Return in *PROP_IDX the index where tool-bar item
7464726e 9625 properties start in F->tool_bar_items. Value is zero if
e037b9ec 9626 GLYPH doesn't display a tool-bar item. */
5f5c8ee5 9627
fa3c6b4d 9628static int
e037b9ec 9629tool_bar_item_info (f, glyph, prop_idx)
5f5c8ee5
GM
9630 struct frame *f;
9631 struct glyph *glyph;
9632 int *prop_idx;
90adcf20 9633{
5f5c8ee5
GM
9634 Lisp_Object prop;
9635 int success_p;
be676094
GM
9636 int charpos;
9637
9638 /* This function can be called asynchronously, which means we must
9639 exclude any possibility that Fget_text_property signals an
9640 error. */
2051c264 9641 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
be676094 9642 charpos = max (0, charpos);
2311178e 9643
5f5c8ee5
GM
9644 /* Get the text property `menu-item' at pos. The value of that
9645 property is the start index of this item's properties in
7464726e 9646 F->tool_bar_items. */
be676094 9647 prop = Fget_text_property (make_number (charpos),
e037b9ec 9648 Qmenu_item, f->current_tool_bar_string);
5f5c8ee5
GM
9649 if (INTEGERP (prop))
9650 {
9651 *prop_idx = XINT (prop);
9652 success_p = 1;
9653 }
9654 else
9655 success_p = 0;
90adcf20 9656
5f5c8ee5
GM
9657 return success_p;
9658}
2311178e 9659
fa3c6b4d
KS
9660\f
9661/* Get information about the tool-bar item at position X/Y on frame F.
9662 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9663 the current matrix of the tool-bar window of F, or NULL if not
9664 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9665 item in F->tool_bar_items. Value is
9666
9667 -1 if X/Y is not on a tool-bar item
9668 0 if X/Y is on the same item that was highlighted before.
9669 1 otherwise. */
9670
9671static int
9672get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9673 struct frame *f;
9674 int x, y;
9675 struct glyph **glyph;
9676 int *hpos, *vpos, *prop_idx;
9677{
9678 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9679 struct window *w = XWINDOW (f->tool_bar_window);
9680 int area;
9681
9682 /* Find the glyph under X/Y. */
493fdc3c 9683 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
fa3c6b4d
KS
9684 if (*glyph == NULL)
9685 return -1;
9686
9687 /* Get the start of this tool-bar item's properties in
9688 f->tool_bar_items. */
9689 if (!tool_bar_item_info (f, *glyph, prop_idx))
9690 return -1;
9691
9692 /* Is mouse on the highlighted item? */
9693 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9694 && *vpos >= dpyinfo->mouse_face_beg_row
9695 && *vpos <= dpyinfo->mouse_face_end_row
9696 && (*vpos > dpyinfo->mouse_face_beg_row
9697 || *hpos >= dpyinfo->mouse_face_beg_col)
9698 && (*vpos < dpyinfo->mouse_face_end_row
9699 || *hpos < dpyinfo->mouse_face_end_col
9700 || dpyinfo->mouse_face_past_end))
9701 return 0;
9702
9703 return 1;
9704}
9705
9706
9707/* EXPORT:
9708 Handle mouse button event on the tool-bar of frame F, at
9709 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9710 0 for button release. MODIFIERS is event modifiers for button
9711 release. */
9712
9713void
9714handle_tool_bar_click (f, x, y, down_p, modifiers)
9715 struct frame *f;
9716 int x, y, down_p;
9717 unsigned int modifiers;
9718{
9719 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9720 struct window *w = XWINDOW (f->tool_bar_window);
9721 int hpos, vpos, prop_idx;
9722 struct glyph *glyph;
9723 Lisp_Object enabled_p;
9724
9725 /* If not on the highlighted tool-bar item, return. */
9726 frame_to_window_pixel_xy (w, &x, &y);
9727 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9728 return;
9729
9730 /* If item is disabled, do nothing. */
9731 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9732 if (NILP (enabled_p))
9733 return;
9734
9735 if (down_p)
9736 {
9737 /* Show item in pressed state. */
9738 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9739 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9740 last_tool_bar_item = prop_idx;
9741 }
9742 else
9743 {
9744 Lisp_Object key, frame;
9745 struct input_event event;
d86705ec 9746 EVENT_INIT (event);
fa3c6b4d
KS
9747
9748 /* Show item in released state. */
9749 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9750 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9751
9752 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9753
9754 XSETFRAME (frame, f);
9755 event.kind = TOOL_BAR_EVENT;
9756 event.frame_or_window = frame;
9757 event.arg = frame;
9758 kbd_buffer_store_event (&event);
9759
9760 event.kind = TOOL_BAR_EVENT;
9761 event.frame_or_window = frame;
9762 event.arg = key;
9763 event.modifiers = modifiers;
9764 kbd_buffer_store_event (&event);
9765 last_tool_bar_item = -1;
9766 }
9767}
9768
9769
9770/* Possibly highlight a tool-bar item on frame F when mouse moves to
9771 tool-bar window-relative coordinates X/Y. Called from
9772 note_mouse_highlight. */
9773
9774static void
9775note_tool_bar_highlight (f, x, y)
9776 struct frame *f;
9777 int x, y;
9778{
9779 Lisp_Object window = f->tool_bar_window;
9780 struct window *w = XWINDOW (window);
9781 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9782 int hpos, vpos;
9783 struct glyph *glyph;
9784 struct glyph_row *row;
9785 int i;
9786 Lisp_Object enabled_p;
9787 int prop_idx;
9788 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9789 int mouse_down_p, rc;
9790
9791 /* Function note_mouse_highlight is called with negative x(y
9792 values when mouse moves outside of the frame. */
9793 if (x <= 0 || y <= 0)
9794 {
9795 clear_mouse_face (dpyinfo);
9796 return;
9797 }
9798
9799 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9800 if (rc < 0)
9801 {
9802 /* Not on tool-bar item. */
9803 clear_mouse_face (dpyinfo);
9804 return;
9805 }
9806 else if (rc == 0)
9807 /* On same tool-bar item as before. */
9808 goto set_help_echo;
9809
9810 clear_mouse_face (dpyinfo);
9811
9812 /* Mouse is down, but on different tool-bar item? */
9813 mouse_down_p = (dpyinfo->grabbed
9814 && f == last_mouse_frame
9815 && FRAME_LIVE_P (f));
9816 if (mouse_down_p
9817 && last_tool_bar_item != prop_idx)
9818 return;
9819
9820 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9821 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9822
9823 /* If tool-bar item is not enabled, don't highlight it. */
9824 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9825 if (!NILP (enabled_p))
9826 {
9827 /* Compute the x-position of the glyph. In front and past the
da8b7f4f 9828 image is a space. We include this in the highlighted area. */
fa3c6b4d
KS
9829 row = MATRIX_ROW (w->current_matrix, vpos);
9830 for (i = x = 0; i < hpos; ++i)
9831 x += row->glyphs[TEXT_AREA][i].pixel_width;
9832
9833 /* Record this as the current active region. */
9834 dpyinfo->mouse_face_beg_col = hpos;
9835 dpyinfo->mouse_face_beg_row = vpos;
9836 dpyinfo->mouse_face_beg_x = x;
9837 dpyinfo->mouse_face_beg_y = row->y;
9838 dpyinfo->mouse_face_past_end = 0;
9839
9840 dpyinfo->mouse_face_end_col = hpos + 1;
9841 dpyinfo->mouse_face_end_row = vpos;
9842 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9843 dpyinfo->mouse_face_end_y = row->y;
9844 dpyinfo->mouse_face_window = window;
9845 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9846
9847 /* Display it as active. */
9848 show_mouse_face (dpyinfo, draw);
9849 dpyinfo->mouse_face_image_state = draw;
9850 }
9851
9852 set_help_echo:
9853
9854 /* Set help_echo_string to a help string to display for this tool-bar item.
9855 XTread_socket does the rest. */
9856 help_echo_object = help_echo_window = Qnil;
9857 help_echo_pos = -1;
9858 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9859 if (NILP (help_echo_string))
9860 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9861}
9862
5f5c8ee5 9863#endif /* HAVE_WINDOW_SYSTEM */
ffbbc941
KS
9864
9865
9866\f
5f5c8ee5
GM
9867/************************************************************************
9868 Horizontal scrolling
9869 ************************************************************************/
feb0c42f 9870
5f5c8ee5
GM
9871static int hscroll_window_tree P_ ((Lisp_Object));
9872static int hscroll_windows P_ ((Lisp_Object));
feb0c42f 9873
5f5c8ee5
GM
9874/* For all leaf windows in the window tree rooted at WINDOW, set their
9875 hscroll value so that PT is (i) visible in the window, and (ii) so
9876 that it is not within a certain margin at the window's left and
9877 right border. Value is non-zero if any window's hscroll has been
9878 changed. */
9879
9880static int
9881hscroll_window_tree (window)
9882 Lisp_Object window;
9883{
9884 int hscrolled_p = 0;
e76d28d5 9885 int hscroll_relative_p = FLOATP (Vhscroll_step);
1df7e8f0
EZ
9886 int hscroll_step_abs = 0;
9887 double hscroll_step_rel = 0;
9888
9889 if (hscroll_relative_p)
9890 {
e76d28d5 9891 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
1df7e8f0
EZ
9892 if (hscroll_step_rel < 0)
9893 {
9894 hscroll_relative_p = 0;
9895 hscroll_step_abs = 0;
9896 }
9897 }
e76d28d5 9898 else if (INTEGERP (Vhscroll_step))
1df7e8f0 9899 {
e76d28d5 9900 hscroll_step_abs = XINT (Vhscroll_step);
1df7e8f0
EZ
9901 if (hscroll_step_abs < 0)
9902 hscroll_step_abs = 0;
9903 }
9904 else
9905 hscroll_step_abs = 0;
9906
5f5c8ee5 9907 while (WINDOWP (window))
90adcf20 9908 {
5f5c8ee5 9909 struct window *w = XWINDOW (window);
1df7e8f0 9910
5f5c8ee5
GM
9911 if (WINDOWP (w->hchild))
9912 hscrolled_p |= hscroll_window_tree (w->hchild);
9913 else if (WINDOWP (w->vchild))
9914 hscrolled_p |= hscroll_window_tree (w->vchild);
9915 else if (w->cursor.vpos >= 0)
9916 {
da8b7f4f
KS
9917 int h_margin;
9918 int text_area_width;
92a90e89
GM
9919 struct glyph_row *current_cursor_row
9920 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9921 struct glyph_row *desired_cursor_row
9922 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9923 struct glyph_row *cursor_row
9924 = (desired_cursor_row->enabled_p
9925 ? desired_cursor_row
9926 : current_cursor_row);
a2725ab2 9927
da8b7f4f 9928 text_area_width = window_box_width (w, TEXT_AREA);
90adcf20 9929
5f5c8ee5 9930 /* Scroll when cursor is inside this scroll margin. */
da8b7f4f 9931 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
1df7e8f0 9932
5f5c8ee5 9933 if ((XFASTINT (w->hscroll)
e76d28d5 9934 && w->cursor.x <= h_margin)
92a90e89
GM
9935 || (cursor_row->enabled_p
9936 && cursor_row->truncated_on_right_p
e76d28d5 9937 && (w->cursor.x >= text_area_width - h_margin)))
08b610e4 9938 {
5f5c8ee5
GM
9939 struct it it;
9940 int hscroll;
9941 struct buffer *saved_current_buffer;
9942 int pt;
1df7e8f0 9943 int wanted_x;
5f5c8ee5
GM
9944
9945 /* Find point in a display of infinite width. */
9946 saved_current_buffer = current_buffer;
9947 current_buffer = XBUFFER (w->buffer);
1df7e8f0 9948
5f5c8ee5
GM
9949 if (w == XWINDOW (selected_window))
9950 pt = BUF_PT (current_buffer);
9951 else
08b610e4 9952 {
5f5c8ee5
GM
9953 pt = marker_position (w->pointm);
9954 pt = max (BEGV, pt);
9955 pt = min (ZV, pt);
9956 }
9957
9958 /* Move iterator to pt starting at cursor_row->start in
9959 a line with infinite width. */
9960 init_to_row_start (&it, w, cursor_row);
9961 it.last_visible_x = INFINITY;
9962 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9963 current_buffer = saved_current_buffer;
9964
1df7e8f0
EZ
9965 /* Position cursor in window. */
9966 if (!hscroll_relative_p && hscroll_step_abs == 0)
ec110e9e
KS
9967 hscroll = max (0, (it.current_x
9968 - (ITERATOR_AT_END_OF_LINE_P (&it)
9969 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9970 : (text_area_width / 2))))
da8b7f4f 9971 / FRAME_COLUMN_WIDTH (it.f);
e76d28d5 9972 else if (w->cursor.x >= text_area_width - h_margin)
1df7e8f0
EZ
9973 {
9974 if (hscroll_relative_p)
9975 wanted_x = text_area_width * (1 - hscroll_step_rel)
e76d28d5 9976 - h_margin;
1df7e8f0
EZ
9977 else
9978 wanted_x = text_area_width
da8b7f4f 9979 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
e76d28d5 9980 - h_margin;
1df7e8f0 9981 hscroll
da8b7f4f 9982 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
1df7e8f0
EZ
9983 }
9984 else
9985 {
9986 if (hscroll_relative_p)
9987 wanted_x = text_area_width * hscroll_step_rel
e76d28d5 9988 + h_margin;
1df7e8f0 9989 else
da8b7f4f 9990 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
e76d28d5 9991 + h_margin;
1df7e8f0 9992 hscroll
da8b7f4f 9993 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
1df7e8f0 9994 }
3172f70b 9995 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
5f5c8ee5
GM
9996
9997 /* Don't call Fset_window_hscroll if value hasn't
9998 changed because it will prevent redisplay
9999 optimizations. */
10000 if (XFASTINT (w->hscroll) != hscroll)
10001 {
3172f70b
GM
10002 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10003 w->hscroll = make_number (hscroll);
5f5c8ee5 10004 hscrolled_p = 1;
08b610e4 10005 }
08b610e4 10006 }
08b610e4 10007 }
a2725ab2 10008
5f5c8ee5 10009 window = w->next;
90adcf20 10010 }
cd6dfed6 10011
5f5c8ee5
GM
10012 /* Value is non-zero if hscroll of any leaf window has been changed. */
10013 return hscrolled_p;
10014}
10015
10016
10017/* Set hscroll so that cursor is visible and not inside horizontal
10018 scroll margins for all windows in the tree rooted at WINDOW. See
10019 also hscroll_window_tree above. Value is non-zero if any window's
10020 hscroll has been changed. If it has, desired matrices on the frame
10021 of WINDOW are cleared. */
10022
10023static int
10024hscroll_windows (window)
10025 Lisp_Object window;
10026{
d475bcb8 10027 int hscrolled_p;
2311178e 10028
d475bcb8
GM
10029 if (automatic_hscrolling_p)
10030 {
10031 hscrolled_p = hscroll_window_tree (window);
10032 if (hscrolled_p)
10033 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10034 }
10035 else
10036 hscrolled_p = 0;
5f5c8ee5 10037 return hscrolled_p;
90adcf20 10038}
5f5c8ee5
GM
10039
10040
90adcf20 10041\f
5f5c8ee5
GM
10042/************************************************************************
10043 Redisplay
10044 ************************************************************************/
10045
10046/* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10047 to a non-zero value. This is sometimes handy to have in a debugger
10048 session. */
10049
10050#if GLYPH_DEBUG
a2889657 10051
5f5c8ee5
GM
10052/* First and last unchanged row for try_window_id. */
10053
10054int debug_first_unchanged_at_end_vpos;
10055int debug_last_unchanged_at_beg_vpos;
10056
10057/* Delta vpos and y. */
10058
10059int debug_dvpos, debug_dy;
10060
10061/* Delta in characters and bytes for try_window_id. */
10062
10063int debug_delta, debug_delta_bytes;
10064
10065/* Values of window_end_pos and window_end_vpos at the end of
10066 try_window_id. */
10067
31ade731 10068EMACS_INT debug_end_pos, debug_end_vpos;
5f5c8ee5
GM
10069
10070/* Append a string to W->desired_matrix->method. FMT is a printf
10071 format string. A1...A9 are a supplement for a variable-length
10072 argument list. If trace_redisplay_p is non-zero also printf the
10073 resulting string to stderr. */
10074
10075static void
10076debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10077 struct window *w;
10078 char *fmt;
10079 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10080{
10081 char buffer[512];
10082 char *method = w->desired_matrix->method;
10083 int len = strlen (method);
10084 int size = sizeof w->desired_matrix->method;
10085 int remaining = size - len - 1;
10086
10087 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10088 if (len && remaining)
10089 {
10090 method[len] = '|';
10091 --remaining, ++len;
10092 }
2311178e 10093
5f5c8ee5
GM
10094 strncpy (method + len, buffer, remaining);
10095
10096 if (trace_redisplay_p)
10097 fprintf (stderr, "%p (%s): %s\n",
10098 w,
10099 ((BUFFERP (w->buffer)
10100 && STRINGP (XBUFFER (w->buffer)->name))
2051c264 10101 ? (char *) SDATA (XBUFFER (w->buffer)->name)
5f5c8ee5
GM
10102 : "no buffer"),
10103 buffer);
10104}
a2889657 10105
5f5c8ee5 10106#endif /* GLYPH_DEBUG */
90adcf20 10107
a2889657 10108
5f5c8ee5
GM
10109/* Value is non-zero if all changes in window W, which displays
10110 current_buffer, are in the text between START and END. START is a
10111 buffer position, END is given as a distance from Z. Used in
10112 redisplay_internal for display optimization. */
10113
10114static INLINE int
10115text_outside_line_unchanged_p (w, start, end)
10116 struct window *w;
10117 int start, end;
10118{
10119 int unchanged_p = 1;
2311178e 10120
5f5c8ee5
GM
10121 /* If text or overlays have changed, see where. */
10122 if (XFASTINT (w->last_modified) < MODIFF
10123 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10124 {
10125 /* Gap in the line? */
10126 if (GPT < start || Z - GPT < end)
10127 unchanged_p = 0;
10128
10129 /* Changes start in front of the line, or end after it? */
10130 if (unchanged_p
9142dd5b
GM
10131 && (BEG_UNCHANGED < start - 1
10132 || END_UNCHANGED < end))
5f5c8ee5 10133 unchanged_p = 0;
2311178e 10134
5f5c8ee5
GM
10135 /* If selective display, can't optimize if changes start at the
10136 beginning of the line. */
10137 if (unchanged_p
10138 && INTEGERP (current_buffer->selective_display)
10139 && XINT (current_buffer->selective_display) > 0
9142dd5b 10140 && (BEG_UNCHANGED < start || GPT <= start))
5f5c8ee5 10141 unchanged_p = 0;
47d57b22
GM
10142
10143 /* If there are overlays at the start or end of the line, these
10144 may have overlay strings with newlines in them. A change at
10145 START, for instance, may actually concern the display of such
10146 overlay strings as well, and they are displayed on different
10147 lines. So, quickly rule out this case. (For the future, it
10148 might be desirable to implement something more telling than
10149 just BEG/END_UNCHANGED.) */
10150 if (unchanged_p)
10151 {
10152 if (BEG + BEG_UNCHANGED == start
10153 && overlay_touches_p (start))
10154 unchanged_p = 0;
10155 if (END_UNCHANGED == end
10156 && overlay_touches_p (Z - end))
10157 unchanged_p = 0;
10158 }
5f5c8ee5
GM
10159 }
10160
10161 return unchanged_p;
10162}
10163
10164
10165/* Do a frame update, taking possible shortcuts into account. This is
10166 the main external entry point for redisplay.
10167
10168 If the last redisplay displayed an echo area message and that message
10169 is no longer requested, we clear the echo area or bring back the
10170 mini-buffer if that is in use. */
20de20dc 10171
a2889657
JB
10172void
10173redisplay ()
e9874cee
RS
10174{
10175 redisplay_internal (0);
10176}
10177
47d57b22 10178
351b5434 10179static Lisp_Object
25bcb353 10180overlay_arrow_string_or_property (var)
351b5434 10181 Lisp_Object var;
351b5434 10182{
25bcb353 10183 Lisp_Object val;
351b5434 10184
25bcb353
KS
10185 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10186 return val;
351b5434 10187
351b5434
KS
10188 return Voverlay_arrow_string;
10189}
10190
10191/* Return 1 if there are any overlay-arrows in current_buffer. */
10192static int
10193overlay_arrow_in_current_buffer_p ()
10194{
10195 Lisp_Object vlist;
10196
10197 for (vlist = Voverlay_arrow_variable_list;
10198 CONSP (vlist);
10199 vlist = XCDR (vlist))
10200 {
10201 Lisp_Object var = XCAR (vlist);
10202 Lisp_Object val;
10203
10204 if (!SYMBOLP (var))
10205 continue;
10206 val = find_symbol_value (var);
10207 if (MARKERP (val)
10208 && current_buffer == XMARKER (val)->buffer)
10209 return 1;
10210 }
10211 return 0;
10212}
10213
10214
10215/* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10216 has changed. */
10217
10218static int
10219overlay_arrows_changed_p ()
10220{
10221 Lisp_Object vlist;
10222
10223 for (vlist = Voverlay_arrow_variable_list;
10224 CONSP (vlist);
10225 vlist = XCDR (vlist))
10226 {
10227 Lisp_Object var = XCAR (vlist);
10228 Lisp_Object val, pstr;
10229
10230 if (!SYMBOLP (var))
10231 continue;
10232 val = find_symbol_value (var);
10233 if (!MARKERP (val))
10234 continue;
10235 if (! EQ (COERCE_MARKER (val),
10236 Fget (var, Qlast_arrow_position))
25bcb353 10237 || ! (pstr = overlay_arrow_string_or_property (var),
351b5434
KS
10238 EQ (pstr, Fget (var, Qlast_arrow_string))))
10239 return 1;
10240 }
10241 return 0;
10242}
10243
10244/* Mark overlay arrows to be updated on next redisplay. */
10245
10246static void
10247update_overlay_arrows (up_to_date)
10248 int up_to_date;
10249{
10250 Lisp_Object vlist;
10251
10252 for (vlist = Voverlay_arrow_variable_list;
10253 CONSP (vlist);
10254 vlist = XCDR (vlist))
10255 {
10256 Lisp_Object var = XCAR (vlist);
351b5434
KS
10257
10258 if (!SYMBOLP (var))
10259 continue;
10260
409368b9 10261 if (up_to_date > 0)
351b5434 10262 {
7d90b587 10263 Lisp_Object val = find_symbol_value (var);
351b5434 10264 Fput (var, Qlast_arrow_position,
7d90b587 10265 COERCE_MARKER (val));
351b5434 10266 Fput (var, Qlast_arrow_string,
25bcb353 10267 overlay_arrow_string_or_property (var));
351b5434
KS
10268 }
10269 else if (up_to_date < 0
10270 || !NILP (Fget (var, Qlast_arrow_position)))
10271 {
10272 Fput (var, Qlast_arrow_position, Qt);
10273 Fput (var, Qlast_arrow_string, Qt);
10274 }
10275 }
10276}
10277
10278
c514efbb 10279/* Return overlay arrow string to display at row.
25bcb353 10280 Return integer (bitmap number) for arrow bitmap in left fringe.
c514efbb 10281 Return nil if no overlay arrow. */
351b5434
KS
10282
10283static Lisp_Object
25bcb353 10284overlay_arrow_at_row (it, row)
c514efbb 10285 struct it *it;
351b5434 10286 struct glyph_row *row;
351b5434
KS
10287{
10288 Lisp_Object vlist;
10289
10290 for (vlist = Voverlay_arrow_variable_list;
10291 CONSP (vlist);
10292 vlist = XCDR (vlist))
10293 {
10294 Lisp_Object var = XCAR (vlist);
10295 Lisp_Object val;
10296
10297 if (!SYMBOLP (var))
10298 continue;
10299
10300 val = find_symbol_value (var);
be21b925 10301
351b5434
KS
10302 if (MARKERP (val)
10303 && current_buffer == XMARKER (val)->buffer
10304 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10305 {
c514efbb
KS
10306 if (FRAME_WINDOW_P (it->f)
10307 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
25bcb353 10308 {
025568d9 10309#ifdef HAVE_WINDOW_SYSTEM
25bcb353
KS
10310 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10311 {
10312 int fringe_bitmap;
10313 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10314 return make_number (fringe_bitmap);
10315 }
025568d9 10316#endif
25bcb353
KS
10317 return make_number (-1); /* Use default arrow bitmap */
10318 }
10319 return overlay_arrow_string_or_property (var);
351b5434
KS
10320 }
10321 }
10322
351b5434
KS
10323 return Qnil;
10324}
10325
260a86a0
KH
10326/* Return 1 if point moved out of or into a composition. Otherwise
10327 return 0. PREV_BUF and PREV_PT are the last point buffer and
10328 position. BUF and PT are the current point buffer and position. */
10329
10330int
10331check_point_in_composition (prev_buf, prev_pt, buf, pt)
10332 struct buffer *prev_buf, *buf;
10333 int prev_pt, pt;
10334{
10335 int start, end;
10336 Lisp_Object prop;
10337 Lisp_Object buffer;
10338
10339 XSETBUFFER (buffer, buf);
10340 /* Check a composition at the last point if point moved within the
10341 same buffer. */
10342 if (prev_buf == buf)
10343 {
10344 if (prev_pt == pt)
10345 /* Point didn't move. */
10346 return 0;
2311178e 10347
260a86a0
KH
10348 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10349 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10350 && COMPOSITION_VALID_P (start, end, prop)
10351 && start < prev_pt && end > prev_pt)
10352 /* The last point was within the composition. Return 1 iff
10353 point moved out of the composition. */
10354 return (pt <= start || pt >= end);
10355 }
10356
10357 /* Check a composition at the current point. */
10358 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10359 && find_composition (pt, -1, &start, &end, &prop, buffer)
10360 && COMPOSITION_VALID_P (start, end, prop)
10361 && start < pt && end > pt);
10362}
5f5c8ee5 10363
47d57b22 10364
9142dd5b
GM
10365/* Reconsider the setting of B->clip_changed which is displayed
10366 in window W. */
10367
10368static INLINE void
10369reconsider_clip_changes (w, b)
10370 struct window *w;
10371 struct buffer *b;
10372{
c62c1bb5 10373 if (b->clip_changed
9142dd5b
GM
10374 && !NILP (w->window_end_valid)
10375 && w->current_matrix->buffer == b
10376 && w->current_matrix->zv == BUF_ZV (b)
10377 && w->current_matrix->begv == BUF_BEGV (b))
10378 b->clip_changed = 0;
260a86a0
KH
10379
10380 /* If display wasn't paused, and W is not a tool bar window, see if
10381 point has been moved into or out of a composition. In that case,
10382 we set b->clip_changed to 1 to force updating the screen. If
10383 b->clip_changed has already been set to 1, we can skip this
10384 check. */
10385 if (!b->clip_changed
10386 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10387 {
10388 int pt;
10389
10390 if (w == XWINDOW (selected_window))
10391 pt = BUF_PT (current_buffer);
10392 else
10393 pt = marker_position (w->pointm);
10394
10395 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
6fc556fd 10396 || pt != XINT (w->last_point))
260a86a0 10397 && check_point_in_composition (w->current_matrix->buffer,
6fc556fd 10398 XINT (w->last_point),
260a86a0
KH
10399 XBUFFER (w->buffer), pt))
10400 b->clip_changed = 1;
10401 }
9142dd5b 10402}
aac2d8b2 10403\f
dd429b03
KH
10404
10405/* Select FRAME to forward the values of frame-local variables into C
10406 variables so that the redisplay routines can access those values
10407 directly. */
10408
10409static void
10410select_frame_for_redisplay (frame)
10411 Lisp_Object frame;
10412{
10413 Lisp_Object tail, sym, val;
10414 Lisp_Object old = selected_frame;
be21b925 10415
dd429b03
KH
10416 selected_frame = frame;
10417
10418 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10419 if (CONSP (XCAR (tail))
10420 && (sym = XCAR (XCAR (tail)),
10421 SYMBOLP (sym))
10422 && (sym = indirect_variable (sym),
10423 val = SYMBOL_VALUE (sym),
10424 (BUFFER_LOCAL_VALUEP (val)
10425 || SOME_BUFFER_LOCAL_VALUEP (val)))
10426 && XBUFFER_LOCAL_VALUE (val)->check_frame)
673b8d0e
RS
10427 /* Use find_symbol_value rather than Fsymbol_value
10428 to avoid an error if it is void. */
10429 find_symbol_value (sym);
dd429b03
KH
10430
10431 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10432 if (CONSP (XCAR (tail))
10433 && (sym = XCAR (XCAR (tail)),
10434 SYMBOLP (sym))
10435 && (sym = indirect_variable (sym),
10436 val = SYMBOL_VALUE (sym),
10437 (BUFFER_LOCAL_VALUEP (val)
10438 || SOME_BUFFER_LOCAL_VALUEP (val)))
10439 && XBUFFER_LOCAL_VALUE (val)->check_frame)
673b8d0e 10440 find_symbol_value (sym);
dd429b03
KH
10441}
10442
10443
aac2d8b2
RS
10444#define STOP_POLLING \
10445do { if (! polling_stopped_here) stop_polling (); \
10446 polling_stopped_here = 1; } while (0)
10447
10448#define RESUME_POLLING \
10449do { if (polling_stopped_here) start_polling (); \
10450 polling_stopped_here = 0; } while (0)
9142dd5b
GM
10451
10452
5f5c8ee5
GM
10453/* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10454 response to any user action; therefore, we should preserve the echo
10455 area. (Actually, our caller does that job.) Perhaps in the future
10456 avoid recentering windows if it is not necessary; currently that
10457 causes some problems. */
e9874cee
RS
10458
10459static void
10460redisplay_internal (preserve_echo_area)
10461 int preserve_echo_area;
a2889657 10462{
5f5c8ee5
GM
10463 struct window *w = XWINDOW (selected_window);
10464 struct frame *f = XFRAME (w->frame);
10465 int pause;
a2889657 10466 int must_finish = 0;
5f5c8ee5 10467 struct text_pos tlbufpos, tlendpos;
89819bdd 10468 int number_of_visible_frames;
28514cd9 10469 int count;
886bd6f2 10470 struct frame *sf = SELECTED_FRAME ();
aac2d8b2 10471 int polling_stopped_here = 0;
a2889657 10472
5f5c8ee5
GM
10473 /* Non-zero means redisplay has to consider all windows on all
10474 frames. Zero means, only selected_window is considered. */
10475 int consider_all_windows_p;
2311178e 10476
5f5c8ee5
GM
10477 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10478
10479 /* No redisplay if running in batch mode or frame is not yet fully
10480 initialized, or redisplay is explicitly turned off by setting
10481 Vinhibit_redisplay. */
10482 if (noninteractive
10483 || !NILP (Vinhibit_redisplay)
10484 || !f->glyphs_initialized_p)
a2889657
JB
10485 return;
10486
5f5c8ee5
GM
10487 /* The flag redisplay_performed_directly_p is set by
10488 direct_output_for_insert when it already did the whole screen
10489 update necessary. */
10490 if (redisplay_performed_directly_p)
10491 {
10492 redisplay_performed_directly_p = 0;
10493 if (!hscroll_windows (selected_window))
10494 return;
10495 }
10496
488dd4c4 10497#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15f0cf78
RS
10498 if (popup_activated ())
10499 return;
10500#endif
10501
28514cd9 10502 /* I don't think this happens but let's be paranoid. */
5f5c8ee5 10503 if (redisplaying_p)
735c094c
KH
10504 return;
10505
28514cd9
GM
10506 /* Record a function that resets redisplaying_p to its old value
10507 when we leave this function. */
331379bf 10508 count = SPECPDL_INDEX ();
dd429b03
KH
10509 record_unwind_protect (unwind_redisplay,
10510 Fcons (make_number (redisplaying_p), selected_frame));
28514cd9 10511 ++redisplaying_p;
26683087 10512 specbind (Qinhibit_free_realized_faces, Qnil);
2311178e 10513
a61fed91
KS
10514 {
10515 Lisp_Object tail, frame;
10516
10517 FOR_EACH_FRAME (tail, frame)
10518 {
10519 struct frame *f = XFRAME (frame);
10520 f->already_hscrolled_p = 0;
10521 }
10522 }
10523
8b32d885 10524 retry:
bd9d0f3f 10525 pause = 0;
9142dd5b
GM
10526 reconsider_clip_changes (w, current_buffer);
10527
5f5c8ee5
GM
10528 /* If new fonts have been loaded that make a glyph matrix adjustment
10529 necessary, do it. */
10530 if (fonts_changed_p)
10531 {
10532 adjust_glyphs (NULL);
10533 ++windows_or_buffers_changed;
10534 fonts_changed_p = 0;
10535 }
10536
6961e0c1
GM
10537 /* If face_change_count is non-zero, init_iterator will free all
10538 realized faces, which includes the faces referenced from current
10539 matrices. So, we can't reuse current matrices in this case. */
10540 if (face_change_count)
10541 ++windows_or_buffers_changed;
10542
886bd6f2
GM
10543 if (! FRAME_WINDOW_P (sf)
10544 && previous_terminal_frame != sf)
20de20dc 10545 {
5f5c8ee5
GM
10546 /* Since frames on an ASCII terminal share the same display
10547 area, displaying a different frame means redisplay the whole
10548 thing. */
20de20dc 10549 windows_or_buffers_changed++;
886bd6f2
GM
10550 SET_FRAME_GARBAGED (sf);
10551 XSETFRAME (Vterminal_frame, sf);
20de20dc 10552 }
886bd6f2 10553 previous_terminal_frame = sf;
20de20dc 10554
5f5c8ee5
GM
10555 /* Set the visible flags for all frames. Do this before checking
10556 for resized or garbaged frames; they want to know if their frames
10557 are visible. See the comment in frame.h for
10558 FRAME_SAMPLE_VISIBILITY. */
d724d989 10559 {
35f56f96 10560 Lisp_Object tail, frame;
d724d989 10561
89819bdd
RS
10562 number_of_visible_frames = 0;
10563
35f56f96 10564 FOR_EACH_FRAME (tail, frame)
f82aff7c 10565 {
5f5c8ee5 10566 struct frame *f = XFRAME (frame);
2311178e 10567
5f5c8ee5
GM
10568 FRAME_SAMPLE_VISIBILITY (f);
10569 if (FRAME_VISIBLE_P (f))
10570 ++number_of_visible_frames;
10571 clear_desired_matrices (f);
f82aff7c 10572 }
d724d989
JB
10573 }
10574
44fa5b1e 10575 /* Notice any pending interrupt request to change frame size. */
c6e89d6c 10576 do_pending_window_change (1);
a2889657 10577
5f5c8ee5 10578 /* Clear frames marked as garbaged. */
44fa5b1e 10579 if (frame_garbaged)
c6e89d6c 10580 clear_garbaged_frames ();
a2889657 10581
e037b9ec 10582 /* Build menubar and tool-bar items. */
f5b8d5f2
RS
10583 if (NILP (Vmemory_full))
10584 prepare_menu_bars ();
f82aff7c 10585
28995e67 10586 if (windows_or_buffers_changed)
a2889657
JB
10587 update_mode_lines++;
10588
538f13d4
RS
10589 /* Detect case that we need to write or remove a star in the mode line. */
10590 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
a2889657
JB
10591 {
10592 w->update_mode_line = Qt;
10593 if (buffer_shared > 1)
10594 update_mode_lines++;
10595 }
10596
5f5c8ee5 10597 /* If %c is in the mode line, update it if needed. */
28995e67
RS
10598 if (!NILP (w->column_number_displayed)
10599 /* This alternative quickly identifies a common case
10600 where no change is needed. */
10601 && !(PT == XFASTINT (w->last_point)
8850a573
RS
10602 && XFASTINT (w->last_modified) >= MODIFF
10603 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
2311178e
TTN
10604 && (XFASTINT (w->column_number_displayed)
10605 != (int) current_column ())) /* iftc */
10606 w->update_mode_line = Qt;
28995e67 10607
44fa5b1e 10608 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
a2889657 10609
5f5c8ee5
GM
10610 /* The variable buffer_shared is set in redisplay_window and
10611 indicates that we redisplay a buffer in different windows. See
10612 there. */
5fb96e96
RS
10613 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10614 || cursor_type_changed);
a2889657
JB
10615
10616 /* If specs for an arrow have changed, do thorough redisplay
10617 to ensure we remove any arrow that should no longer exist. */
351b5434 10618 if (overlay_arrows_changed_p ())
5f5c8ee5 10619 consider_all_windows_p = windows_or_buffers_changed = 1;
a2889657 10620
90adcf20
RS
10621 /* Normally the message* functions will have already displayed and
10622 updated the echo area, but the frame may have been trashed, or
10623 the update may have been preempted, so display the echo area
6e019995 10624 again here. Checking message_cleared_p captures the case that
c6e89d6c 10625 the echo area should be cleared. */
96d5e9a0
GM
10626 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10627 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
3fa69ee7
GM
10628 || (message_cleared_p
10629 && minibuf_level == 0
10630 /* If the mini-window is currently selected, this means the
10631 echo-area doesn't show through. */
10632 && !MINI_WINDOW_P (XWINDOW (selected_window))))
90adcf20 10633 {
c6e89d6c 10634 int window_height_changed_p = echo_area_display (0);
90adcf20 10635 must_finish = 1;
6e019995
GM
10636
10637 /* If we don't display the current message, don't clear the
10638 message_cleared_p flag, because, if we did, we wouldn't clear
10639 the echo area in the next redisplay which doesn't preserve
10640 the echo area. */
10641 if (!display_last_displayed_message_p)
10642 message_cleared_p = 0;
2311178e 10643
c6e89d6c
GM
10644 if (fonts_changed_p)
10645 goto retry;
10646 else if (window_height_changed_p)
10647 {
10648 consider_all_windows_p = 1;
10649 ++update_mode_lines;
10650 ++windows_or_buffers_changed;
2311178e 10651
9142dd5b
GM
10652 /* If window configuration was changed, frames may have been
10653 marked garbaged. Clear them or we will experience
10654 surprises wrt scrolling. */
10655 if (frame_garbaged)
10656 clear_garbaged_frames ();
c6e89d6c 10657 }
90adcf20 10658 }
97a36635 10659 else if (EQ (selected_window, minibuf_window)
dd2eb166
GM
10660 && (current_buffer->clip_changed
10661 || XFASTINT (w->last_modified) < MODIFF
10662 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
92a90e89 10663 && resize_mini_window (w, 0))
c6e89d6c
GM
10664 {
10665 /* Resized active mini-window to fit the size of what it is
dd2eb166
GM
10666 showing if its contents might have changed. */
10667 must_finish = 1;
10668 consider_all_windows_p = 1;
c6e89d6c 10669 ++windows_or_buffers_changed;
dd2eb166 10670 ++update_mode_lines;
2311178e 10671
9142dd5b
GM
10672 /* If window configuration was changed, frames may have been
10673 marked garbaged. Clear them or we will experience
10674 surprises wrt scrolling. */
10675 if (frame_garbaged)
10676 clear_garbaged_frames ();
c6e89d6c 10677 }
2311178e 10678
90adcf20 10679
5f5c8ee5
GM
10680 /* If showing the region, and mark has changed, we must redisplay
10681 the whole window. The assignment to this_line_start_pos prevents
10682 the optimization directly below this if-statement. */
bd66d1ba
RS
10683 if (((!NILP (Vtransient_mark_mode)
10684 && !NILP (XBUFFER (w->buffer)->mark_active))
10685 != !NILP (w->region_showing))
82d04750
JB
10686 || (!NILP (w->region_showing)
10687 && !EQ (w->region_showing,
10688 Fmarker_position (XBUFFER (w->buffer)->mark))))
5f5c8ee5
GM
10689 CHARPOS (this_line_start_pos) = 0;
10690
10691 /* Optimize the case that only the line containing the cursor in the
10692 selected window has changed. Variables starting with this_ are
10693 set in display_line and record information about the line
10694 containing the cursor. */
10695 tlbufpos = this_line_start_pos;
10696 tlendpos = this_line_end_pos;
10697 if (!consider_all_windows_p
10698 && CHARPOS (tlbufpos) > 0
10699 && NILP (w->update_mode_line)
73af359d 10700 && !current_buffer->clip_changed
c62c1bb5 10701 && !current_buffer->prevent_redisplay_optimizations_p
44fa5b1e 10702 && FRAME_VISIBLE_P (XFRAME (w->frame))
f21ef775 10703 && !FRAME_OBSCURED_P (XFRAME (w->frame))
5f5c8ee5 10704 /* Make sure recorded data applies to current buffer, etc. */
a2889657
JB
10705 && this_line_buffer == current_buffer
10706 && current_buffer == XBUFFER (w->buffer)
265a9e55 10707 && NILP (w->force_start)
acda20e1 10708 && NILP (w->optional_new_start)
5f5c8ee5
GM
10709 /* Point must be on the line that we have info recorded about. */
10710 && PT >= CHARPOS (tlbufpos)
10711 && PT <= Z - CHARPOS (tlendpos)
a2889657
JB
10712 /* All text outside that line, including its final newline,
10713 must be unchanged */
5f5c8ee5
GM
10714 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10715 CHARPOS (tlendpos)))
10716 {
10717 if (CHARPOS (tlbufpos) > BEGV
10718 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10719 && (CHARPOS (tlbufpos) == ZV
10720 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
a2889657
JB
10721 /* Former continuation line has disappeared by becoming empty */
10722 goto cancel;
10723 else if (XFASTINT (w->last_modified) < MODIFF
8850a573 10724 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
a2889657
JB
10725 || MINI_WINDOW_P (w))
10726 {
1c9241f5
KH
10727 /* We have to handle the case of continuation around a
10728 wide-column character (See the comment in indent.c around
10729 line 885).
10730
10731 For instance, in the following case:
10732
10733 -------- Insert --------
10734 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10735 J_I_ ==> J_I_ `^^' are cursors.
10736 ^^ ^^
10737 -------- --------
10738
10739 As we have to redraw the line above, we should goto cancel. */
10740
5f5c8ee5
GM
10741 struct it it;
10742 int line_height_before = this_line_pixel_height;
10743
10744 /* Note that start_display will handle the case that the
10745 line starting at tlbufpos is a continuation lines. */
10746 start_display (&it, w, tlbufpos);
10747
10748 /* Implementation note: It this still necessary? */
10749 if (it.current_x != this_line_start_x)
1c9241f5
KH
10750 goto cancel;
10751
5f5c8ee5
GM
10752 TRACE ((stderr, "trying display optimization 1\n"));
10753 w->cursor.vpos = -1;
a2889657 10754 overlay_arrow_seen = 0;
5f5c8ee5
GM
10755 it.vpos = this_line_vpos;
10756 it.current_y = this_line_y;
10757 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10758 display_line (&it);
10759
a2889657 10760 /* If line contains point, is not continued,
5f5c8ee5 10761 and ends at same distance from eob as before, we win */
2311178e 10762 if (w->cursor.vpos >= 0
5f5c8ee5
GM
10763 /* Line is not continued, otherwise this_line_start_pos
10764 would have been set to 0 in display_line. */
10765 && CHARPOS (this_line_start_pos)
10766 /* Line ends as before. */
10767 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10768 /* Line has same height as before. Otherwise other lines
10769 would have to be shifted up or down. */
10770 && this_line_pixel_height == line_height_before)
a2889657 10771 {
5f5c8ee5
GM
10772 /* If this is not the window's last line, we must adjust
10773 the charstarts of the lines below. */
10774 if (it.current_y < it.last_visible_y)
10775 {
10776 struct glyph_row *row
10777 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10778 int delta, delta_bytes;
2311178e 10779
5f5c8ee5
GM
10780 if (Z - CHARPOS (tlendpos) == ZV)
10781 {
10782 /* This line ends at end of (accessible part of)
10783 buffer. There is no newline to count. */
10784 delta = (Z
10785 - CHARPOS (tlendpos)
10786 - MATRIX_ROW_START_CHARPOS (row));
10787 delta_bytes = (Z_BYTE
10788 - BYTEPOS (tlendpos)
10789 - MATRIX_ROW_START_BYTEPOS (row));
10790 }
10791 else
10792 {
10793 /* This line ends in a newline. Must take
10794 account of the newline and the rest of the
10795 text that follows. */
10796 delta = (Z
10797 - CHARPOS (tlendpos)
10798 - MATRIX_ROW_START_CHARPOS (row));
10799 delta_bytes = (Z_BYTE
10800 - BYTEPOS (tlendpos)
10801 - MATRIX_ROW_START_BYTEPOS (row));
10802 }
2311178e 10803
f2d86d7a
GM
10804 increment_matrix_positions (w->current_matrix,
10805 this_line_vpos + 1,
10806 w->current_matrix->nrows,
10807 delta, delta_bytes);
85bcef6c 10808 }
46db8486 10809
5f5c8ee5
GM
10810 /* If this row displays text now but previously didn't,
10811 or vice versa, w->window_end_vpos may have to be
10812 adjusted. */
10813 if ((it.glyph_row - 1)->displays_text_p)
10814 {
10815 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10816 XSETINT (w->window_end_vpos, this_line_vpos);
10817 }
10818 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10819 && this_line_vpos > 0)
10820 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10821 w->window_end_valid = Qnil;
2311178e 10822
5f5c8ee5
GM
10823 /* Update hint: No need to try to scroll in update_window. */
10824 w->desired_matrix->no_scrolling_p = 1;
10825
10826#if GLYPH_DEBUG
10827 *w->desired_matrix->method = 0;
10828 debug_method_add (w, "optimization 1");
10829#endif
7af0e8d7 10830#ifdef HAVE_WINDOW_SYSTEM
88e6b646 10831 update_window_fringes (w, 0);
7af0e8d7 10832#endif
a2889657
JB
10833 goto update;
10834 }
10835 else
10836 goto cancel;
10837 }
5f5c8ee5
GM
10838 else if (/* Cursor position hasn't changed. */
10839 PT == XFASTINT (w->last_point)
b6f0fe04
RS
10840 /* Make sure the cursor was last displayed
10841 in this window. Otherwise we have to reposition it. */
5f5c8ee5 10842 && 0 <= w->cursor.vpos
da8b7f4f 10843 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
a2889657
JB
10844 {
10845 if (!must_finish)
10846 {
c6e89d6c 10847 do_pending_window_change (1);
5f5c8ee5 10848
2311178e 10849 /* We used to always goto end_of_redisplay here, but this
5f5c8ee5
GM
10850 isn't enough if we have a blinking cursor. */
10851 if (w->cursor_off_p == w->last_cursor_off_p)
10852 goto end_of_redisplay;
a2889657
JB
10853 }
10854 goto update;
10855 }
8b51f1e3
KH
10856 /* If highlighting the region, or if the cursor is in the echo area,
10857 then we can't just move the cursor. */
bd66d1ba
RS
10858 else if (! (!NILP (Vtransient_mark_mode)
10859 && !NILP (current_buffer->mark_active))
97a36635 10860 && (EQ (selected_window, current_buffer->last_selected_window)
293a54ce 10861 || highlight_nonselected_windows)
8b51f1e3 10862 && NILP (w->region_showing)
8f897821 10863 && NILP (Vshow_trailing_whitespace)
8b51f1e3 10864 && !cursor_in_echo_area)
a2889657 10865 {
5f5c8ee5
GM
10866 struct it it;
10867 struct glyph_row *row;
10868
10869 /* Skip from tlbufpos to PT and see where it is. Note that
10870 PT may be in invisible text. If so, we will end at the
10871 next visible position. */
10872 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10873 NULL, DEFAULT_FACE_ID);
10874 it.current_x = this_line_start_x;
10875 it.current_y = this_line_y;
10876 it.vpos = this_line_vpos;
2311178e 10877
5f5c8ee5
GM
10878 /* The call to move_it_to stops in front of PT, but
10879 moves over before-strings. */
10880 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10881
10882 if (it.vpos == this_line_vpos
10883 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10884 row->enabled_p))
a2889657 10885 {
5f5c8ee5
GM
10886 xassert (this_line_vpos == it.vpos);
10887 xassert (this_line_y == it.current_y);
10888 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
ade0bee1
GM
10889#if GLYPH_DEBUG
10890 *w->desired_matrix->method = 0;
10891 debug_method_add (w, "optimization 3");
10892#endif
a2889657
JB
10893 goto update;
10894 }
10895 else
10896 goto cancel;
10897 }
5f5c8ee5 10898
a2889657 10899 cancel:
5f5c8ee5
GM
10900 /* Text changed drastically or point moved off of line. */
10901 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
a2889657
JB
10902 }
10903
5f5c8ee5
GM
10904 CHARPOS (this_line_start_pos) = 0;
10905 consider_all_windows_p |= buffer_shared > 1;
10906 ++clear_face_cache_count;
418b5052
KS
10907#ifdef HAVE_WINDOW_SYSTEM
10908 ++clear_image_cache_count;
10909#endif
2311178e 10910
bd9d0f3f
GM
10911 /* Build desired matrices, and update the display. If
10912 consider_all_windows_p is non-zero, do it for all windows on all
10913 frames. Otherwise do it for selected_window, only. */
463f6b91 10914
5f5c8ee5 10915 if (consider_all_windows_p)
a2889657 10916 {
35f56f96 10917 Lisp_Object tail, frame;
366717cf
RS
10918
10919 FOR_EACH_FRAME (tail, frame)
2bb212bd 10920 XFRAME (frame)->updated_p = 0;
a2889657 10921
5f5c8ee5
GM
10922 /* Recompute # windows showing selected buffer. This will be
10923 incremented each time such a window is displayed. */
a2889657
JB
10924 buffer_shared = 0;
10925
35f56f96 10926 FOR_EACH_FRAME (tail, frame)
30c566e4 10927 {
5f5c8ee5 10928 struct frame *f = XFRAME (frame);
2311178e 10929
886bd6f2 10930 if (FRAME_WINDOW_P (f) || f == sf)
9769686d 10931 {
dd429b03
KH
10932 if (! EQ (frame, selected_frame))
10933 /* Select the frame, for the sake of frame-local
10934 variables. */
10935 select_frame_for_redisplay (frame);
10936
5f5c8ee5
GM
10937 /* Mark all the scroll bars to be removed; we'll redeem
10938 the ones we want when we redisplay their windows. */
9769686d 10939 if (condemn_scroll_bars_hook)
504454e8 10940 condemn_scroll_bars_hook (f);
30c566e4 10941
f21ef775 10942 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
5f5c8ee5 10943 redisplay_windows (FRAME_ROOT_WINDOW (f));
30c566e4 10944
5f5c8ee5
GM
10945 /* Any scroll bars which redisplay_windows should have
10946 nuked should now go away. */
9769686d 10947 if (judge_scroll_bars_hook)
504454e8 10948 judge_scroll_bars_hook (f);
bd9d0f3f
GM
10949
10950 /* If fonts changed, display again. */
b60c9653
RS
10951 /* ??? rms: I suspect it is a mistake to jump all the way
10952 back to retry here. It should just retry this frame. */
bd9d0f3f
GM
10953 if (fonts_changed_p)
10954 goto retry;
2311178e 10955
bd9d0f3f
GM
10956 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10957 {
10958 /* See if we have to hscroll. */
a61fed91
KS
10959 if (!f->already_hscrolled_p)
10960 {
10961 f->already_hscrolled_p = 1;
10962 if (hscroll_windows (f->root_window))
10963 goto retry;
10964 }
bd9d0f3f
GM
10965
10966 /* Prevent various kinds of signals during display
10967 update. stdio is not robust about handling
10968 signals, which can cause an apparent I/O
10969 error. */
10970 if (interrupt_input)
10971 unrequest_sigio ();
aac2d8b2 10972 STOP_POLLING;
bd9d0f3f
GM
10973
10974 /* Update the display. */
10975 set_window_update_flags (XWINDOW (f->root_window), 1);
10976 pause |= update_frame (f, 0, 0);
ccbb9ed2 10977#if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
bd9d0f3f
GM
10978 if (pause)
10979 break;
ccbb9ed2 10980#endif
bd9d0f3f 10981
2bb212bd 10982 f->updated_p = 1;
bd9d0f3f 10983 }
9769686d 10984 }
30c566e4 10985 }
0528abe1 10986
6f68b035
GM
10987 if (!pause)
10988 {
10989 /* Do the mark_window_display_accurate after all windows have
10990 been redisplayed because this call resets flags in buffers
10991 which are needed for proper redisplay. */
2bb212bd 10992 FOR_EACH_FRAME (tail, frame)
6f68b035 10993 {
2bb212bd
KS
10994 struct frame *f = XFRAME (frame);
10995 if (f->updated_p)
10996 {
10997 mark_window_display_accurate (f->root_window, 1);
10998 if (frame_up_to_date_hook)
10999 frame_up_to_date_hook (f);
11000 }
6f68b035 11001 }
0528abe1 11002 }
a2889657 11003 }
bd9d0f3f
GM
11004 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11005 {
11006 Lisp_Object mini_window;
11007 struct frame *mini_frame;
5f5c8ee5 11008
82a7ab23 11009 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
7033d6df
RS
11010 /* Use list_of_error, not Qerror, so that
11011 we catch only errors and don't run the debugger. */
11012 internal_condition_case_1 (redisplay_window_1, selected_window,
11013 list_of_error,
82a7ab23 11014 redisplay_window_error);
2311178e 11015
bd9d0f3f 11016 /* Compare desired and current matrices, perform output. */
7d0393cf 11017
26683087 11018 update:
bd9d0f3f
GM
11019 /* If fonts changed, display again. */
11020 if (fonts_changed_p)
92a90e89 11021 goto retry;
a2889657 11022
bd9d0f3f
GM
11023 /* Prevent various kinds of signals during display update.
11024 stdio is not robust about handling signals,
11025 which can cause an apparent I/O error. */
11026 if (interrupt_input)
11027 unrequest_sigio ();
aac2d8b2 11028 STOP_POLLING;
1af9f229 11029
bd9d0f3f 11030 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
5f5c8ee5 11031 {
92a90e89
GM
11032 if (hscroll_windows (selected_window))
11033 goto retry;
2311178e 11034
5f5c8ee5 11035 XWINDOW (selected_window)->must_be_updated_p = 1;
886bd6f2 11036 pause = update_frame (sf, 0, 0);
5f5c8ee5 11037 }
d724d989 11038
8de2d90b 11039 /* We may have called echo_area_display at the top of this
44fa5b1e
JB
11040 function. If the echo area is on another frame, that may
11041 have put text on a frame other than the selected one, so the
11042 above call to update_frame would not have caught it. Catch
8de2d90b 11043 it here. */
bd9d0f3f
GM
11044 mini_window = FRAME_MINIBUF_WINDOW (sf);
11045 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
2311178e 11046
bd9d0f3f
GM
11047 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11048 {
11049 XWINDOW (mini_window)->must_be_updated_p = 1;
11050 pause |= update_frame (mini_frame, 0, 0);
11051 if (!pause && hscroll_windows (mini_window))
11052 goto retry;
11053 }
6e8290aa 11054 }
a2889657 11055
5f5c8ee5
GM
11056 /* If display was paused because of pending input, make sure we do a
11057 thorough update the next time. */
a2889657
JB
11058 if (pause)
11059 {
5f5c8ee5
GM
11060 /* Prevent the optimization at the beginning of
11061 redisplay_internal that tries a single-line update of the
11062 line containing the cursor in the selected window. */
11063 CHARPOS (this_line_start_pos) = 0;
11064
11065 /* Let the overlay arrow be updated the next time. */
351b5434 11066 update_overlay_arrows (0);
2311178e 11067
5f5c8ee5
GM
11068 /* If we pause after scrolling, some rows in the current
11069 matrices of some windows are not valid. */
11070 if (!WINDOW_FULL_WIDTH_P (w)
11071 && !FRAME_WINDOW_P (XFRAME (w->frame)))
a2889657
JB
11072 update_mode_lines = 1;
11073 }
43c09969 11074 else
a2889657 11075 {
43c09969 11076 if (!consider_all_windows_p)
a2889657 11077 {
43c09969
GM
11078 /* This has already been done above if
11079 consider_all_windows_p is set. */
11080 mark_window_display_accurate_1 (w, 1);
2311178e 11081
351b5434
KS
11082 /* Say overlay arrows are up to date. */
11083 update_overlay_arrows (1);
2311178e 11084
efc63ef0 11085 if (frame_up_to_date_hook != 0)
43c09969 11086 frame_up_to_date_hook (sf);
a2889657 11087 }
15e26c76 11088
a2889657
JB
11089 update_mode_lines = 0;
11090 windows_or_buffers_changed = 0;
5fb96e96 11091 cursor_type_changed = 0;
a2889657
JB
11092 }
11093
5f5c8ee5
GM
11094 /* Start SIGIO interrupts coming again. Having them off during the
11095 code above makes it less likely one will discard output, but not
11096 impossible, since there might be stuff in the system buffer here.
a2889657 11097 But it is much hairier to try to do anything about that. */
a2889657
JB
11098 if (interrupt_input)
11099 request_sigio ();
aac2d8b2 11100 RESUME_POLLING;
a2889657 11101
5f5c8ee5
GM
11102 /* If a frame has become visible which was not before, redisplay
11103 again, so that we display it. Expose events for such a frame
11104 (which it gets when becoming visible) don't call the parts of
11105 redisplay constructing glyphs, so simply exposing a frame won't
11106 display anything in this case. So, we have to display these
11107 frames here explicitly. */
11c52c4f
RS
11108 if (!pause)
11109 {
11110 Lisp_Object tail, frame;
11111 int new_count = 0;
11112
11113 FOR_EACH_FRAME (tail, frame)
11114 {
11115 int this_is_visible = 0;
8e83f802
RS
11116
11117 if (XFRAME (frame)->visible)
11118 this_is_visible = 1;
11119 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11120 if (XFRAME (frame)->visible)
11121 this_is_visible = 1;
11c52c4f
RS
11122
11123 if (this_is_visible)
11124 new_count++;
11125 }
11126
89819bdd 11127 if (new_count != number_of_visible_frames)
11c52c4f
RS
11128 windows_or_buffers_changed++;
11129 }
11130
44fa5b1e 11131 /* Change frame size now if a change is pending. */
c6e89d6c 11132 do_pending_window_change (1);
d8e242fd 11133
8b32d885
RS
11134 /* If we just did a pending size change, or have additional
11135 visible frames, redisplay again. */
3c8c72e0 11136 if (windows_or_buffers_changed && !pause)
8b32d885 11137 goto retry;
5f5c8ee5 11138
418b5052
KS
11139 /* Clear the face cache eventually. */
11140 if (consider_all_windows_p)
11141 {
11142 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11143 {
11144 clear_face_cache (0);
11145 clear_face_cache_count = 0;
11146 }
11147#ifdef HAVE_WINDOW_SYSTEM
11148 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11149 {
11150 Lisp_Object tail, frame;
11151 FOR_EACH_FRAME (tail, frame)
11152 {
11153 struct frame *f = XFRAME (frame);
11154 if (FRAME_WINDOW_P (f))
11155 clear_image_cache (f, 0);
11156 }
11157 clear_image_cache_count = 0;
11158 }
11159#endif /* HAVE_WINDOW_SYSTEM */
11160 }
11161
1987b083 11162 end_of_redisplay:
28514cd9 11163 unbind_to (count, Qnil);
aac2d8b2 11164 RESUME_POLLING;
a2889657
JB
11165}
11166
5f5c8ee5
GM
11167
11168/* Redisplay, but leave alone any recent echo area message unless
11169 another message has been requested in its place.
a2889657
JB
11170
11171 This is useful in situations where you need to redisplay but no
11172 user action has occurred, making it inappropriate for the message
11173 area to be cleared. See tracking_off and
d64b707c 11174 wait_reading_process_output for examples of these situations.
9bf76936
GM
11175
11176 FROM_WHERE is an integer saying from where this function was
11177 called. This is useful for debugging. */
a2889657 11178
8991bb31 11179void
9bf76936
GM
11180redisplay_preserve_echo_area (from_where)
11181 int from_where;
a2889657 11182{
9bf76936
GM
11183 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11184
c6e89d6c 11185 if (!NILP (echo_area_buffer[1]))
a2889657 11186 {
c6e89d6c
GM
11187 /* We have a previously displayed message, but no current
11188 message. Redisplay the previous message. */
11189 display_last_displayed_message_p = 1;
e9874cee 11190 redisplay_internal (1);
c6e89d6c 11191 display_last_displayed_message_p = 0;
a2889657
JB
11192 }
11193 else
e9874cee 11194 redisplay_internal (1);
16fe48b4 11195
777f4146 11196 if (rif != NULL && rif->flush_display_optional)
16fe48b4 11197 rif->flush_display_optional (NULL);
a2889657
JB
11198}
11199
5f5c8ee5 11200
28514cd9 11201/* Function registered with record_unwind_protect in
acfca545
RS
11202 redisplay_internal. Reset redisplaying_p to the value it had
11203 before redisplay_internal was called, and clear
dd429b03
KH
11204 prevent_freeing_realized_faces_p. It also selects the previously
11205 selected frame. */
28514cd9
GM
11206
11207static Lisp_Object
dd429b03
KH
11208unwind_redisplay (val)
11209 Lisp_Object val;
28514cd9 11210{
dd429b03
KH
11211 Lisp_Object old_redisplaying_p, old_frame;
11212
11213 old_redisplaying_p = XCAR (val);
28514cd9 11214 redisplaying_p = XFASTINT (old_redisplaying_p);
dd429b03
KH
11215 old_frame = XCDR (val);
11216 if (! EQ (old_frame, selected_frame))
11217 select_frame_for_redisplay (old_frame);
c6e89d6c 11218 return Qnil;
28514cd9
GM
11219}
11220
11221
43c09969
GM
11222/* Mark the display of window W as accurate or inaccurate. If
11223 ACCURATE_P is non-zero mark display of W as accurate. If
11224 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11225 redisplay_internal is called. */
5f5c8ee5 11226
43c09969
GM
11227static void
11228mark_window_display_accurate_1 (w, accurate_p)
11229 struct window *w;
5f5c8ee5 11230 int accurate_p;
a2889657 11231{
43c09969 11232 if (BUFFERP (w->buffer))
a2889657 11233 {
43c09969 11234 struct buffer *b = XBUFFER (w->buffer);
2311178e 11235
43c09969
GM
11236 w->last_modified
11237 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11238 w->last_overlay_modified
11239 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11240 w->last_had_star
11241 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
a2889657 11242
43c09969 11243 if (accurate_p)
bd66d1ba 11244 {
43c09969
GM
11245 b->clip_changed = 0;
11246 b->prevent_redisplay_optimizations_p = 0;
11247
11248 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11249 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11250 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11251 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
2311178e 11252
43c09969
GM
11253 w->current_matrix->buffer = b;
11254 w->current_matrix->begv = BUF_BEGV (b);
11255 w->current_matrix->zv = BUF_ZV (b);
2311178e 11256
43c09969
GM
11257 w->last_cursor = w->cursor;
11258 w->last_cursor_off_p = w->cursor_off_p;
2311178e 11259
43c09969
GM
11260 if (w == XWINDOW (selected_window))
11261 w->last_point = make_number (BUF_PT (b));
11262 else
11263 w->last_point = make_number (XMARKER (w->pointm)->charpos);
bd66d1ba 11264 }
43c09969 11265 }
bd66d1ba 11266
43c09969
GM
11267 if (accurate_p)
11268 {
d2f84654 11269 w->window_end_valid = w->buffer;
99332eb6 11270#if 0 /* This is incorrect with variable-height lines. */
2913a9c0 11271 xassert (XINT (w->window_end_vpos)
da8b7f4f 11272 < (WINDOW_TOTAL_LINES (w)
2913a9c0 11273 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
99332eb6 11274#endif
a2889657 11275 w->update_mode_line = Qnil;
43c09969
GM
11276 }
11277}
11278
11279
11280/* Mark the display of windows in the window tree rooted at WINDOW as
11281 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11282 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11283 be redisplayed the next time redisplay_internal is called. */
11284
11285void
11286mark_window_display_accurate (window, accurate_p)
11287 Lisp_Object window;
11288 int accurate_p;
11289{
11290 struct window *w;
2311178e 11291
43c09969
GM
11292 for (; !NILP (window); window = w->next)
11293 {
11294 w = XWINDOW (window);
11295 mark_window_display_accurate_1 (w, accurate_p);
a2889657 11296
265a9e55 11297 if (!NILP (w->vchild))
5f5c8ee5 11298 mark_window_display_accurate (w->vchild, accurate_p);
265a9e55 11299 if (!NILP (w->hchild))
5f5c8ee5 11300 mark_window_display_accurate (w->hchild, accurate_p);
a2889657
JB
11301 }
11302
5f5c8ee5 11303 if (accurate_p)
a2889657 11304 {
351b5434 11305 update_overlay_arrows (1);
a2889657
JB
11306 }
11307 else
11308 {
5f5c8ee5
GM
11309 /* Force a thorough redisplay the next time by setting
11310 last_arrow_position and last_arrow_string to t, which is
4b41cebb 11311 unequal to any useful value of Voverlay_arrow_... */
351b5434 11312 update_overlay_arrows (-1);
a2889657
JB
11313 }
11314}
5f5c8ee5
GM
11315
11316
11317/* Return value in display table DP (Lisp_Char_Table *) for character
11318 C. Since a display table doesn't have any parent, we don't have to
11319 follow parent. Do not call this function directly but use the
11320 macro DISP_CHAR_VECTOR. */
11321
11322Lisp_Object
11323disp_char_vector (dp, c)
11324 struct Lisp_Char_Table *dp;
11325 int c;
11326{
11327 int code[4], i;
11328 Lisp_Object val;
11329
11330 if (SINGLE_BYTE_CHAR_P (c))
11331 return (dp->contents[c]);
2311178e 11332
c5924f47 11333 SPLIT_CHAR (c, code[0], code[1], code[2]);
260a86a0
KH
11334 if (code[1] < 32)
11335 code[1] = -1;
11336 else if (code[2] < 32)
11337 code[2] = -1;
2311178e 11338
5f5c8ee5
GM
11339 /* Here, the possible range of code[0] (== charset ID) is
11340 128..max_charset. Since the top level char table contains data
11341 for multibyte characters after 256th element, we must increment
11342 code[0] by 128 to get a correct index. */
11343 code[0] += 128;
11344 code[3] = -1; /* anchor */
11345
11346 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11347 {
11348 val = dp->contents[code[i]];
11349 if (!SUB_CHAR_TABLE_P (val))
11350 return (NILP (val) ? dp->defalt : val);
11351 }
2311178e 11352
5f5c8ee5
GM
11353 /* Here, val is a sub char table. We return the default value of
11354 it. */
11355 return (dp->defalt);
11356}
11357
11358
a2889657 11359\f
5f5c8ee5
GM
11360/***********************************************************************
11361 Window Redisplay
11362 ***********************************************************************/
a2725ab2 11363
5f5c8ee5 11364/* Redisplay all leaf windows in the window tree rooted at WINDOW. */
90adcf20
RS
11365
11366static void
5f5c8ee5
GM
11367redisplay_windows (window)
11368 Lisp_Object window;
90adcf20 11369{
5f5c8ee5
GM
11370 while (!NILP (window))
11371 {
11372 struct window *w = XWINDOW (window);
2311178e 11373
5f5c8ee5
GM
11374 if (!NILP (w->hchild))
11375 redisplay_windows (w->hchild);
11376 else if (!NILP (w->vchild))
11377 redisplay_windows (w->vchild);
11378 else
82a7ab23
RS
11379 {
11380 displayed_buffer = XBUFFER (w->buffer);
7033d6df
RS
11381 /* Use list_of_error, not Qerror, so that
11382 we catch only errors and don't run the debugger. */
2311178e 11383 internal_condition_case_1 (redisplay_window_0, window,
7033d6df 11384 list_of_error,
82a7ab23
RS
11385 redisplay_window_error);
11386 }
a2725ab2 11387
5f5c8ee5
GM
11388 window = w->next;
11389 }
11390}
11391
82a7ab23
RS
11392static Lisp_Object
11393redisplay_window_error ()
11394{
11395 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11396 return Qnil;
11397}
11398
11399static Lisp_Object
11400redisplay_window_0 (window)
11401 Lisp_Object window;
11402{
11403 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11404 redisplay_window (window, 0);
11405 return Qnil;
11406}
5f5c8ee5 11407
82a7ab23
RS
11408static Lisp_Object
11409redisplay_window_1 (window)
11410 Lisp_Object window;
11411{
11412 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11413 redisplay_window (window, 1);
11414 return Qnil;
11415}
11416\f
bc6371a6
KH
11417
11418/* Increment GLYPH until it reaches END or CONDITION fails while
11419 adding (GLYPH)->pixel_width to X. */
11420
11421#define SKIP_GLYPHS(glyph, end, x, condition) \
11422 do \
11423 { \
11424 (x) += (glyph)->pixel_width; \
11425 ++(glyph); \
11426 } \
11427 while ((glyph) < (end) && (condition))
11428
11429
5f5c8ee5
GM
11430/* Set cursor position of W. PT is assumed to be displayed in ROW.
11431 DELTA is the number of bytes by which positions recorded in ROW
11432 differ from current buffer positions. */
11433
11434void
11435set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11436 struct window *w;
11437 struct glyph_row *row;
11438 struct glyph_matrix *matrix;
11439 int delta, delta_bytes, dy, dvpos;
11440{
11441 struct glyph *glyph = row->glyphs[TEXT_AREA];
11442 struct glyph *end = glyph + row->used[TEXT_AREA];
cc2734ee 11443 struct glyph *cursor = NULL;
ae73dc1c
KH
11444 /* The first glyph that starts a sequence of glyphs from string. */
11445 struct glyph *string_start;
11446 /* The X coordinate of string_start. */
11447 int string_start_x;
11448 /* The last known character position. */
11449 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11450 /* The last known character position before string_start. */
11451 int string_before_pos;
5f5c8ee5 11452 int x = row->x;
cc2734ee 11453 int cursor_x = x;
a01c5155 11454 int cursor_from_overlay_pos = 0;
5f5c8ee5
GM
11455 int pt_old = PT - delta;
11456
11457 /* Skip over glyphs not having an object at the start of the row.
11458 These are special glyphs like truncation marks on terminal
11459 frames. */
11460 if (row->displays_text_p)
11461 while (glyph < end
6fc556fd 11462 && INTEGERP (glyph->object)
5f5c8ee5
GM
11463 && glyph->charpos < 0)
11464 {
11465 x += glyph->pixel_width;
11466 ++glyph;
11467 }
11468
ae73dc1c 11469 string_start = NULL;
5f5c8ee5 11470 while (glyph < end
6fc556fd 11471 && !INTEGERP (glyph->object)
5f5c8ee5 11472 && (!BUFFERP (glyph->object)
ae73dc1c 11473 || (last_pos = glyph->charpos) < pt_old))
5f5c8ee5 11474 {
ae73dc1c
KH
11475 if (! STRINGP (glyph->object))
11476 {
11477 string_start = NULL;
11478 x += glyph->pixel_width;
11479 ++glyph;
a01c5155
KS
11480 if (cursor_from_overlay_pos
11481 && last_pos > cursor_from_overlay_pos)
11482 {
11483 cursor_from_overlay_pos = 0;
11484 cursor = 0;
11485 }
ae73dc1c
KH
11486 }
11487 else
11488 {
11489 string_before_pos = last_pos;
11490 string_start = glyph;
11491 string_start_x = x;
11492 /* Skip all glyphs from string. */
cc2734ee
KS
11493 do
11494 {
a01c5155 11495 int pos;
cc2734ee
KS
11496 if ((cursor == NULL || glyph > cursor)
11497 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
a01c5155
KS
11498 Qcursor, (glyph)->object))
11499 && (pos = string_buffer_position (w, glyph->object,
11500 string_before_pos),
11501 (pos == 0 /* From overlay */
11502 || pos == pt_old)))
cc2734ee 11503 {
bc9447bc
KS
11504 /* Estimate overlay buffer position from the buffer
11505 positions of the glyphs before and after the overlay.
11506 Add 1 to last_pos so that if point corresponds to the
11507 glyph right after the overlay, we still use a 'cursor'
11508 property found in that overlay. */
11509 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
cc2734ee
KS
11510 cursor = glyph;
11511 cursor_x = x;
11512 }
11513 x += glyph->pixel_width;
11514 ++glyph;
11515 }
11516 while (glyph < end && STRINGP (glyph->object));
ae73dc1c
KH
11517 }
11518 }
11519
cc2734ee
KS
11520 if (cursor != NULL)
11521 {
11522 glyph = cursor;
11523 x = cursor_x;
11524 }
f323e507
RS
11525 else if (row->ends_in_ellipsis_p && glyph == end)
11526 {
11527 /* Scan back over the ellipsis glyphs, decrementing positions. */
11528 while (glyph > row->glyphs[TEXT_AREA]
11529 && (glyph - 1)->charpos == last_pos)
11530 glyph--, x -= glyph->pixel_width;
11531 /* That loop always goes one position too far,
11532 including the glyph before the ellipsis.
11533 So scan forward over that one. */
11534 x += glyph->pixel_width;
11535 glyph++;
11536 }
cc2734ee
KS
11537 else if (string_start
11538 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
ae73dc1c
KH
11539 {
11540 /* We may have skipped over point because the previous glyphs
11541 are from string. As there's no easy way to know the
11542 character position of the current glyph, find the correct
11543 glyph on point by scanning from string_start again. */
bc6371a6
KH
11544 Lisp_Object limit;
11545 Lisp_Object string;
11546 int pos;
ae73dc1c 11547
bc6371a6
KH
11548 limit = make_number (pt_old + 1);
11549 end = glyph;
ae73dc1c
KH
11550 glyph = string_start;
11551 x = string_start_x;
bc6371a6
KH
11552 string = glyph->object;
11553 pos = string_buffer_position (w, string, string_before_pos);
11554 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11555 because we always put cursor after overlay strings. */
11556 while (pos == 0 && glyph < end)
ae73dc1c 11557 {
bc6371a6
KH
11558 string = glyph->object;
11559 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11560 if (glyph < end)
11561 pos = string_buffer_position (w, glyph->object, string_before_pos);
11562 }
11563
11564 while (glyph < end)
11565 {
11566 pos = XINT (Fnext_single_char_property_change
11567 (make_number (pos), Qdisplay, Qnil, limit));
11568 if (pos > pt_old)
11569 break;
ae73dc1c 11570 /* Skip glyphs from the same string. */
bc6371a6
KH
11571 string = glyph->object;
11572 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11573 /* Skip glyphs from an overlay. */
11574 while (glyph < end
11575 && ! string_buffer_position (w, glyph->object, pos))
ae73dc1c 11576 {
bc6371a6
KH
11577 string = glyph->object;
11578 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
ae73dc1c 11579 }
ae73dc1c 11580 }
5f5c8ee5
GM
11581 }
11582
11583 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11584 w->cursor.x = x;
11585 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11586 w->cursor.y = row->y + dy;
11587
11588 if (w == XWINDOW (selected_window))
11589 {
11590 if (!row->continued_p
11591 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11592 && row->x == 0)
11593 {
11594 this_line_buffer = XBUFFER (w->buffer);
2311178e 11595
5f5c8ee5
GM
11596 CHARPOS (this_line_start_pos)
11597 = MATRIX_ROW_START_CHARPOS (row) + delta;
11598 BYTEPOS (this_line_start_pos)
11599 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
2311178e 11600
5f5c8ee5
GM
11601 CHARPOS (this_line_end_pos)
11602 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11603 BYTEPOS (this_line_end_pos)
11604 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
2311178e 11605
5f5c8ee5
GM
11606 this_line_y = w->cursor.y;
11607 this_line_pixel_height = row->height;
11608 this_line_vpos = w->cursor.vpos;
11609 this_line_start_x = row->x;
11610 }
11611 else
11612 CHARPOS (this_line_start_pos) = 0;
11613 }
11614}
11615
11616
11617/* Run window scroll functions, if any, for WINDOW with new window
756a3cb6
RS
11618 start STARTP. Sets the window start of WINDOW to that position.
11619
11620 We assume that the window's buffer is really current. */
5f5c8ee5
GM
11621
11622static INLINE struct text_pos
11623run_window_scroll_functions (window, startp)
11624 Lisp_Object window;
11625 struct text_pos startp;
11626{
11627 struct window *w = XWINDOW (window);
11628 SET_MARKER_FROM_TEXT_POS (w->start, startp);
756a3cb6
RS
11629
11630 if (current_buffer != XBUFFER (w->buffer))
11631 abort ();
11632
5f5c8ee5
GM
11633 if (!NILP (Vwindow_scroll_functions))
11634 {
2311178e 11635 run_hook_with_args_2 (Qwindow_scroll_functions, window,
5f5c8ee5
GM
11636 make_number (CHARPOS (startp)));
11637 SET_TEXT_POS_FROM_MARKER (startp, w->start);
756a3cb6
RS
11638 /* In case the hook functions switch buffers. */
11639 if (current_buffer != XBUFFER (w->buffer))
11640 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5 11641 }
90adcf20 11642
5f5c8ee5
GM
11643 return startp;
11644}
11645
11646
ddf6b9a3
RS
11647/* Make sure the line containing the cursor is fully visible.
11648 A value of 1 means there is nothing to be done.
11649 (Either the line is fully visible, or it cannot be made so,
11650 or we cannot tell.)
cb9d4a9f
KS
11651
11652 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11653 is higher than window.
11654
ddf6b9a3
RS
11655 A value of 0 means the caller should do scrolling
11656 as if point had gone off the screen. */
5f5c8ee5 11657
cb617e7c 11658static int
e10ee30c 11659cursor_row_fully_visible_p (w, force_p, current_matrix_p)
5f5c8ee5 11660 struct window *w;
cb9d4a9f 11661 int force_p;
43f15d4a 11662 int current_matrix_p;
5f5c8ee5
GM
11663{
11664 struct glyph_matrix *matrix;
11665 struct glyph_row *row;
478d746b 11666 int window_height;
2311178e 11667
8232c913
KS
11668 if (!make_cursor_line_fully_visible_p)
11669 return 1;
11670
5f5c8ee5
GM
11671 /* It's not always possible to find the cursor, e.g, when a window
11672 is full of overlay strings. Don't do anything in that case. */
11673 if (w->cursor.vpos < 0)
cb617e7c 11674 return 1;
2311178e 11675
e10ee30c 11676 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
5f5c8ee5
GM
11677 row = MATRIX_ROW (matrix, w->cursor.vpos);
11678
acda20e1 11679 /* If the cursor row is not partially visible, there's nothing to do. */
546f98b8 11680 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
cb617e7c 11681 return 1;
b28cb6ed
GM
11682
11683 /* If the row the cursor is in is taller than the window's height,
11684 it's not clear what to do, so do nothing. */
11685 window_height = window_box_height (w);
11686 if (row->height >= window_height)
e893970b 11687 {
e3ea3a51 11688 if (!force_p || MINI_WINDOW_P (w) || w->vscroll)
e893970b
KS
11689 return 1;
11690 }
ddf6b9a3
RS
11691 return 0;
11692
11693#if 0
11694 /* This code used to try to scroll the window just enough to make
11695 the line visible. It returned 0 to say that the caller should
11696 allocate larger glyph matrices. */
11697
b28cb6ed 11698 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
5f5c8ee5
GM
11699 {
11700 int dy = row->height - row->visible_height;
11701 w->vscroll = 0;
11702 w->cursor.y += dy;
11703 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11704 }
b28cb6ed 11705 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
5f5c8ee5
GM
11706 {
11707 int dy = - (row->height - row->visible_height);
11708 w->vscroll = dy;
11709 w->cursor.y += dy;
11710 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11711 }
2311178e 11712
5f5c8ee5
GM
11713 /* When we change the cursor y-position of the selected window,
11714 change this_line_y as well so that the display optimization for
11715 the cursor line of the selected window in redisplay_internal uses
11716 the correct y-position. */
11717 if (w == XWINDOW (selected_window))
11718 this_line_y = w->cursor.y;
cb617e7c
GM
11719
11720 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11721 redisplay with larger matrices. */
11722 if (matrix->nrows < required_matrix_height (w))
11723 {
11724 fonts_changed_p = 1;
11725 return 0;
11726 }
11727
11728 return 1;
ddf6b9a3 11729#endif /* 0 */
5f5c8ee5
GM
11730}
11731
11732
11733/* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11734 non-zero means only WINDOW is redisplayed in redisplay_internal.
11735 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11736 in redisplay_window to bring a partially visible line into view in
11737 the case that only the cursor has moved.
11738
03b0a4b4
RS
11739 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11740 last screen line's vertical height extends past the end of the screen.
11741
5f5c8ee5
GM
11742 Value is
11743
11744 1 if scrolling succeeded
2311178e 11745
5f5c8ee5 11746 0 if scrolling didn't find point.
2311178e 11747
5f5c8ee5
GM
11748 -1 if new fonts have been loaded so that we must interrupt
11749 redisplay, adjust glyph matrices, and try again. */
11750
cb617e7c
GM
11751enum
11752{
11753 SCROLLING_SUCCESS,
11754 SCROLLING_FAILED,
11755 SCROLLING_NEED_LARGER_MATRICES
11756};
11757
5f5c8ee5
GM
11758static int
11759try_scrolling (window, just_this_one_p, scroll_conservatively,
03b0a4b4 11760 scroll_step, temp_scroll_step, last_line_misfit)
5f5c8ee5
GM
11761 Lisp_Object window;
11762 int just_this_one_p;
31ade731 11763 EMACS_INT scroll_conservatively, scroll_step;
5f5c8ee5 11764 int temp_scroll_step;
03b0a4b4 11765 int last_line_misfit;
5f5c8ee5
GM
11766{
11767 struct window *w = XWINDOW (window);
11768 struct frame *f = XFRAME (w->frame);
11769 struct text_pos scroll_margin_pos;
11770 struct text_pos pos;
11771 struct text_pos startp;
11772 struct it it;
11773 Lisp_Object window_end;
11774 int this_scroll_margin;
11775 int dy = 0;
11776 int scroll_max;
b8a63ccb 11777 int rc;
5f5c8ee5
GM
11778 int amount_to_scroll = 0;
11779 Lisp_Object aggressive;
11780 int height;
cb9d4a9f 11781 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
5f5c8ee5
GM
11782
11783#if GLYPH_DEBUG
11784 debug_method_add (w, "try_scrolling");
78614721 11785#endif
5f5c8ee5
GM
11786
11787 SET_TEXT_POS_FROM_MARKER (startp, w->start);
2311178e 11788
5f5c8ee5
GM
11789 /* Compute scroll margin height in pixels. We scroll when point is
11790 within this distance from the top or bottom of the window. */
11791 if (scroll_margin > 0)
90adcf20 11792 {
da8b7f4f
KS
11793 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11794 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
5f5c8ee5
GM
11795 }
11796 else
11797 this_scroll_margin = 0;
11798
75c5501b
JB
11799 /* Force scroll_conservatively to have a reasonable value so it doesn't
11800 cause an overflow while computing how much to scroll. */
11801 if (scroll_conservatively)
11802 scroll_conservatively = min (scroll_conservatively,
11803 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11804
5f5c8ee5
GM
11805 /* Compute how much we should try to scroll maximally to bring point
11806 into view. */
0894e696
SM
11807 if (scroll_step || scroll_conservatively || temp_scroll_step)
11808 scroll_max = max (scroll_step,
11809 max (scroll_conservatively, temp_scroll_step));
5f5c8ee5
GM
11810 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11811 || NUMBERP (current_buffer->scroll_up_aggressively))
11812 /* We're trying to scroll because of aggressive scrolling
11813 but no scroll_step is set. Choose an arbitrary one. Maybe
11814 there should be a variable for this. */
11815 scroll_max = 10;
11816 else
11817 scroll_max = 0;
da8b7f4f 11818 scroll_max *= FRAME_LINE_HEIGHT (f);
5f5c8ee5
GM
11819
11820 /* Decide whether we have to scroll down. Start at the window end
11821 and move this_scroll_margin up to find the position of the scroll
11822 margin. */
11823 window_end = Fwindow_end (window, Qt);
03b0a4b4
RS
11824
11825 too_near_end:
11826
5f5c8ee5
GM
11827 CHARPOS (scroll_margin_pos) = XINT (window_end);
11828 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
03b0a4b4 11829
cb9d4a9f 11830 if (this_scroll_margin || extra_scroll_margin_lines)
5f5c8ee5
GM
11831 {
11832 start_display (&it, w, scroll_margin_pos);
cb9d4a9f 11833 if (this_scroll_margin)
546f98b8 11834 move_it_vertically_backward (&it, this_scroll_margin);
cb9d4a9f
KS
11835 if (extra_scroll_margin_lines)
11836 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
5f5c8ee5
GM
11837 scroll_margin_pos = it.current.pos;
11838 }
11839
11840 if (PT >= CHARPOS (scroll_margin_pos))
11841 {
11842 int y0;
2311178e 11843
5f5c8ee5
GM
11844 /* Point is in the scroll margin at the bottom of the window, or
11845 below. Compute a new window start that makes point visible. */
47589c8c 11846
5f5c8ee5
GM
11847 /* Compute the distance from the scroll margin to PT.
11848 Give up if the distance is greater than scroll_max. */
11849 start_display (&it, w, scroll_margin_pos);
11850 y0 = it.current_y;
11851 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11852 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
539e92ad
GM
11853
11854 /* To make point visible, we have to move the window start
11855 down so that the line the cursor is in is visible, which
11856 means we have to add in the height of the cursor line. */
11857 dy = line_bottom_y (&it) - y0;
2311178e 11858
5f5c8ee5 11859 if (dy > scroll_max)
cb617e7c 11860 return SCROLLING_FAILED;
2311178e 11861
5f5c8ee5
GM
11862 /* Move the window start down. If scrolling conservatively,
11863 move it just enough down to make point visible. If
11864 scroll_step is set, move it down by scroll_step. */
11865 start_display (&it, w, startp);
11866
11867 if (scroll_conservatively)
03b0a4b4
RS
11868 /* Set AMOUNT_TO_SCROLL to at least one line,
11869 and at most scroll_conservatively lines. */
e89aaabd 11870 amount_to_scroll
da8b7f4f
KS
11871 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11872 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
5f5c8ee5
GM
11873 else if (scroll_step || temp_scroll_step)
11874 amount_to_scroll = scroll_max;
11875 else
90adcf20 11876 {
46226c1d 11877 aggressive = current_buffer->scroll_up_aggressively;
da8b7f4f 11878 height = WINDOW_BOX_TEXT_HEIGHT (w);
5f5c8ee5 11879 if (NUMBERP (aggressive))
6924d3b7
RS
11880 {
11881 double float_amount = XFLOATINT (aggressive) * height;
11882 amount_to_scroll = float_amount;
11883 if (amount_to_scroll == 0 && float_amount > 0)
11884 amount_to_scroll = 1;
11885 }
5f5c8ee5 11886 }
a2725ab2 11887
5f5c8ee5 11888 if (amount_to_scroll <= 0)
cb617e7c 11889 return SCROLLING_FAILED;
a2725ab2 11890
03b0a4b4
RS
11891 /* If moving by amount_to_scroll leaves STARTP unchanged,
11892 move it down one screen line. */
11893
5f5c8ee5 11894 move_it_vertically (&it, amount_to_scroll);
03b0a4b4
RS
11895 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11896 move_it_by_lines (&it, 1, 1);
5f5c8ee5
GM
11897 startp = it.current.pos;
11898 }
11899 else
11900 {
11901 /* See if point is inside the scroll margin at the top of the
11902 window. */
11903 scroll_margin_pos = startp;
11904 if (this_scroll_margin)
11905 {
11906 start_display (&it, w, startp);
11907 move_it_vertically (&it, this_scroll_margin);
11908 scroll_margin_pos = it.current.pos;
11909 }
11910
11911 if (PT < CHARPOS (scroll_margin_pos))
11912 {
11913 /* Point is in the scroll margin at the top of the window or
11914 above what is displayed in the window. */
11915 int y0;
2311178e 11916
5f5c8ee5
GM
11917 /* Compute the vertical distance from PT to the scroll
11918 margin position. Give up if distance is greater than
11919 scroll_max. */
11920 SET_TEXT_POS (pos, PT, PT_BYTE);
11921 start_display (&it, w, pos);
11922 y0 = it.current_y;
11923 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11924 it.last_visible_y, -1,
11925 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11926 dy = it.current_y - y0;
11927 if (dy > scroll_max)
cb617e7c 11928 return SCROLLING_FAILED;
2311178e 11929
5f5c8ee5
GM
11930 /* Compute new window start. */
11931 start_display (&it, w, startp);
2311178e 11932
5f5c8ee5 11933 if (scroll_conservatively)
70567d1c
RS
11934 amount_to_scroll
11935 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
5f5c8ee5
GM
11936 else if (scroll_step || temp_scroll_step)
11937 amount_to_scroll = scroll_max;
538f13d4 11938 else
5f5c8ee5 11939 {
46226c1d 11940 aggressive = current_buffer->scroll_down_aggressively;
da8b7f4f 11941 height = WINDOW_BOX_TEXT_HEIGHT (w);
5f5c8ee5 11942 if (NUMBERP (aggressive))
6924d3b7
RS
11943 {
11944 double float_amount = XFLOATINT (aggressive) * height;
11945 amount_to_scroll = float_amount;
11946 if (amount_to_scroll == 0 && float_amount > 0)
11947 amount_to_scroll = 1;
11948 }
5f5c8ee5 11949 }
a2725ab2 11950
5f5c8ee5 11951 if (amount_to_scroll <= 0)
cb617e7c 11952 return SCROLLING_FAILED;
2311178e 11953
546f98b8 11954 move_it_vertically_backward (&it, amount_to_scroll);
5f5c8ee5 11955 startp = it.current.pos;
90adcf20
RS
11956 }
11957 }
a2889657 11958
5f5c8ee5
GM
11959 /* Run window scroll functions. */
11960 startp = run_window_scroll_functions (window, startp);
90adcf20 11961
5f5c8ee5
GM
11962 /* Display the window. Give up if new fonts are loaded, or if point
11963 doesn't appear. */
493c1d49 11964 if (!try_window (window, startp, 0))
cb617e7c 11965 rc = SCROLLING_NEED_LARGER_MATRICES;
5f5c8ee5
GM
11966 else if (w->cursor.vpos < 0)
11967 {
11968 clear_glyph_matrix (w->desired_matrix);
cb617e7c 11969 rc = SCROLLING_FAILED;
5f5c8ee5
GM
11970 }
11971 else
11972 {
11973 /* Maybe forget recorded base line for line number display. */
2311178e 11974 if (!just_this_one_p
5f5c8ee5 11975 || current_buffer->clip_changed
9142dd5b 11976 || BEG_UNCHANGED < CHARPOS (startp))
5f5c8ee5 11977 w->base_line_number = Qnil;
2311178e 11978
ddf6b9a3
RS
11979 /* If cursor ends up on a partially visible line,
11980 treat that as being off the bottom of the screen. */
e10ee30c 11981 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
ef3c2c73
RS
11982 {
11983 clear_glyph_matrix (w->desired_matrix);
cb9d4a9f 11984 ++extra_scroll_margin_lines;
ef3c2c73
RS
11985 goto too_near_end;
11986 }
ddf6b9a3 11987 rc = SCROLLING_SUCCESS;
5f5c8ee5
GM
11988 }
11989
11990 return rc;
a2889657
JB
11991}
11992
5f5c8ee5
GM
11993
11994/* Compute a suitable window start for window W if display of W starts
11995 on a continuation line. Value is non-zero if a new window start
11996 was computed.
11997
11998 The new window start will be computed, based on W's width, starting
11999 from the start of the continued line. It is the start of the
12000 screen line with the minimum distance from the old start W->start. */
12001
12002static int
12003compute_window_start_on_continuation_line (w)
12004 struct window *w;
1f1ff51d 12005{
5f5c8ee5
GM
12006 struct text_pos pos, start_pos;
12007 int window_start_changed_p = 0;
1f1ff51d 12008
5f5c8ee5 12009 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
1f1ff51d 12010
5f5c8ee5 12011 /* If window start is on a continuation line... Window start may be
2311178e 12012 < BEGV in case there's invisible text at the start of the
5f5c8ee5
GM
12013 buffer (M-x rmail, for example). */
12014 if (CHARPOS (start_pos) > BEGV
12015 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
1f1ff51d 12016 {
5f5c8ee5
GM
12017 struct it it;
12018 struct glyph_row *row;
f3751a65
GM
12019
12020 /* Handle the case that the window start is out of range. */
12021 if (CHARPOS (start_pos) < BEGV)
12022 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12023 else if (CHARPOS (start_pos) > ZV)
12024 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
2311178e 12025
5f5c8ee5
GM
12026 /* Find the start of the continued line. This should be fast
12027 because scan_buffer is fast (newline cache). */
045dee35 12028 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
5f5c8ee5
GM
12029 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12030 row, DEFAULT_FACE_ID);
12031 reseat_at_previous_visible_line_start (&it);
12032
12033 /* If the line start is "too far" away from the window start,
12034 say it takes too much time to compute a new window start. */
12035 if (CHARPOS (start_pos) - IT_CHARPOS (it)
da8b7f4f 12036 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
5f5c8ee5
GM
12037 {
12038 int min_distance, distance;
2311178e 12039
5f5c8ee5
GM
12040 /* Move forward by display lines to find the new window
12041 start. If window width was enlarged, the new start can
12042 be expected to be > the old start. If window width was
12043 decreased, the new window start will be < the old start.
12044 So, we're looking for the display line start with the
12045 minimum distance from the old window start. */
12046 pos = it.current.pos;
12047 min_distance = INFINITY;
12048 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12049 distance < min_distance)
12050 {
12051 min_distance = distance;
12052 pos = it.current.pos;
12053 move_it_by_lines (&it, 1, 0);
12054 }
2311178e 12055
5f5c8ee5
GM
12056 /* Set the window start there. */
12057 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12058 window_start_changed_p = 1;
12059 }
1f1ff51d 12060 }
2311178e 12061
5f5c8ee5 12062 return window_start_changed_p;
1f1ff51d
KH
12063}
12064
5f5c8ee5 12065
1dd5768b 12066/* Try cursor movement in case text has not changed in window WINDOW,
47589c8c
GM
12067 with window start STARTP. Value is
12068
cb617e7c 12069 CURSOR_MOVEMENT_SUCCESS if successful
2311178e 12070
cb617e7c
GM
12071 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12072
12073 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12074 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12075 we want to scroll as if scroll-step were set to 1. See the code.
12076
12077 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12078 which case we have to abort this redisplay, and adjust matrices
12079 first. */
12080
2311178e 12081enum
cb617e7c
GM
12082{
12083 CURSOR_MOVEMENT_SUCCESS,
12084 CURSOR_MOVEMENT_CANNOT_BE_USED,
12085 CURSOR_MOVEMENT_MUST_SCROLL,
12086 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12087};
47589c8c
GM
12088
12089static int
12090try_cursor_movement (window, startp, scroll_step)
12091 Lisp_Object window;
12092 struct text_pos startp;
12093 int *scroll_step;
12094{
12095 struct window *w = XWINDOW (window);
12096 struct frame *f = XFRAME (w->frame);
cb617e7c 12097 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
2311178e 12098
69d1f7c9 12099#if GLYPH_DEBUG
76cb5e06
GM
12100 if (inhibit_try_cursor_movement)
12101 return rc;
12102#endif
12103
47589c8c
GM
12104 /* Handle case where text has not changed, only point, and it has
12105 not moved off the frame. */
12106 if (/* Point may be in this window. */
12107 PT >= CHARPOS (startp)
47589c8c
GM
12108 /* Selective display hasn't changed. */
12109 && !current_buffer->clip_changed
4db87380
GM
12110 /* Function force-mode-line-update is used to force a thorough
12111 redisplay. It sets either windows_or_buffers_changed or
12112 update_mode_lines. So don't take a shortcut here for these
12113 cases. */
12114 && !update_mode_lines
12115 && !windows_or_buffers_changed
5fb96e96 12116 && !cursor_type_changed
2311178e 12117 /* Can't use this case if highlighting a region. When a
47589c8c
GM
12118 region exists, cursor movement has to do more than just
12119 set the cursor. */
12120 && !(!NILP (Vtransient_mark_mode)
12121 && !NILP (current_buffer->mark_active))
12122 && NILP (w->region_showing)
12123 && NILP (Vshow_trailing_whitespace)
12124 /* Right after splitting windows, last_point may be nil. */
12125 && INTEGERP (w->last_point)
12126 /* This code is not used for mini-buffer for the sake of the case
12127 of redisplaying to replace an echo area message; since in
12128 that case the mini-buffer contents per se are usually
12129 unchanged. This code is of no real use in the mini-buffer
12130 since the handling of this_line_start_pos, etc., in redisplay
12131 handles the same cases. */
12132 && !EQ (window, minibuf_window)
12133 /* When splitting windows or for new windows, it happens that
12134 redisplay is called with a nil window_end_vpos or one being
12135 larger than the window. This should really be fixed in
12136 window.c. I don't have this on my list, now, so we do
12137 approximately the same as the old redisplay code. --gerd. */
12138 && INTEGERP (w->window_end_vpos)
12139 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12140 && (FRAME_WINDOW_P (f)
351b5434 12141 || !overlay_arrow_in_current_buffer_p ()))
47589c8c 12142 {
38793d6a 12143 int this_scroll_margin, top_scroll_margin;
8ee5b6a3 12144 struct glyph_row *row = NULL;
47589c8c
GM
12145
12146#if GLYPH_DEBUG
12147 debug_method_add (w, "cursor movement");
12148#endif
12149
12150 /* Scroll if point within this distance from the top or bottom
12151 of the window. This is a pixel value. */
12152 this_scroll_margin = max (0, scroll_margin);
da8b7f4f
KS
12153 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12154 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
47589c8c 12155
38793d6a
KS
12156 top_scroll_margin = this_scroll_margin;
12157 if (WINDOW_WANTS_HEADER_LINE_P (w))
12158 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12159
47589c8c
GM
12160 /* Start with the row the cursor was displayed during the last
12161 not paused redisplay. Give up if that row is not valid. */
bd9d0f3f
GM
12162 if (w->last_cursor.vpos < 0
12163 || w->last_cursor.vpos >= w->current_matrix->nrows)
cb617e7c 12164 rc = CURSOR_MOVEMENT_MUST_SCROLL;
47589c8c
GM
12165 else
12166 {
12167 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12168 if (row->mode_line_p)
12169 ++row;
12170 if (!row->enabled_p)
cb617e7c 12171 rc = CURSOR_MOVEMENT_MUST_SCROLL;
47589c8c
GM
12172 }
12173
cb617e7c 12174 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
47589c8c
GM
12175 {
12176 int scroll_p = 0;
68c5d1db 12177 int last_y = window_text_bottom_y (w) - this_scroll_margin;
2311178e 12178
47589c8c
GM
12179 if (PT > XFASTINT (w->last_point))
12180 {
12181 /* Point has moved forward. */
47589c8c
GM
12182 while (MATRIX_ROW_END_CHARPOS (row) < PT
12183 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12184 {
12185 xassert (row->enabled_p);
12186 ++row;
12187 }
12188
12189 /* The end position of a row equals the start position
12190 of the next row. If PT is there, we would rather
cafafe0b
GM
12191 display it in the next line. */
12192 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12193 && MATRIX_ROW_END_CHARPOS (row) == PT
12194 && !cursor_row_p (w, row))
12195 ++row;
47589c8c
GM
12196
12197 /* If within the scroll margin, scroll. Note that
12198 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12199 the next line would be drawn, and that
12200 this_scroll_margin can be zero. */
12201 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12202 || PT > MATRIX_ROW_END_CHARPOS (row)
12203 /* Line is completely visible last line in window
12204 and PT is to be set in the next line. */
12205 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12206 && PT == MATRIX_ROW_END_CHARPOS (row)
12207 && !row->ends_at_zv_p
12208 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12209 scroll_p = 1;
12210 }
12211 else if (PT < XFASTINT (w->last_point))
12212 {
12213 /* Cursor has to be moved backward. Note that PT >=
70567d1c 12214 CHARPOS (startp) because of the outer if-statement. */
47589c8c
GM
12215 while (!row->mode_line_p
12216 && (MATRIX_ROW_START_CHARPOS (row) > PT
12217 || (MATRIX_ROW_START_CHARPOS (row) == PT
e0e9a80e
KS
12218 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12219 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12220 row > w->current_matrix->rows
12221 && (row-1)->ends_in_newline_from_string_p))))
38793d6a 12222 && (row->y > top_scroll_margin
47589c8c
GM
12223 || CHARPOS (startp) == BEGV))
12224 {
12225 xassert (row->enabled_p);
12226 --row;
12227 }
12228
12229 /* Consider the following case: Window starts at BEGV,
12230 there is invisible, intangible text at BEGV, so that
12231 display starts at some point START > BEGV. It can
12232 happen that we are called with PT somewhere between
12233 BEGV and START. Try to handle that case. */
12234 if (row < w->current_matrix->rows
12235 || row->mode_line_p)
12236 {
12237 row = w->current_matrix->rows;
12238 if (row->mode_line_p)
12239 ++row;
12240 }
12241
12242 /* Due to newlines in overlay strings, we may have to
12243 skip forward over overlay strings. */
68c5d1db
GM
12244 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12245 && MATRIX_ROW_END_CHARPOS (row) == PT
12246 && !cursor_row_p (w, row))
47589c8c 12247 ++row;
2311178e 12248
47589c8c 12249 /* If within the scroll margin, scroll. */
38793d6a 12250 if (row->y < top_scroll_margin
47589c8c
GM
12251 && CHARPOS (startp) != BEGV)
12252 scroll_p = 1;
12253 }
e10ee30c
KS
12254 else
12255 {
12256 /* Cursor did not move. So don't scroll even if cursor line
12257 is partially visible, as it was so before. */
12258 rc = CURSOR_MOVEMENT_SUCCESS;
12259 }
47589c8c
GM
12260
12261 if (PT < MATRIX_ROW_START_CHARPOS (row)
12262 || PT > MATRIX_ROW_END_CHARPOS (row))
12263 {
12264 /* if PT is not in the glyph row, give up. */
cb617e7c 12265 rc = CURSOR_MOVEMENT_MUST_SCROLL;
47589c8c 12266 }
e10ee30c
KS
12267 else if (rc != CURSOR_MOVEMENT_SUCCESS
12268 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
8232c913 12269 && make_cursor_line_fully_visible_p)
47589c8c 12270 {
8de4aaf8
GM
12271 if (PT == MATRIX_ROW_END_CHARPOS (row)
12272 && !row->ends_at_zv_p
12273 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
cb617e7c 12274 rc = CURSOR_MOVEMENT_MUST_SCROLL;
3f7e3031 12275 else if (row->height > window_box_height (w))
440fc135 12276 {
8de4aaf8
GM
12277 /* If we end up in a partially visible line, let's
12278 make it fully visible, except when it's taller
12279 than the window, in which case we can't do much
12280 about it. */
440fc135 12281 *scroll_step = 1;
cb617e7c 12282 rc = CURSOR_MOVEMENT_MUST_SCROLL;
440fc135
GM
12283 }
12284 else
12285 {
12286 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
e10ee30c 12287 if (!cursor_row_fully_visible_p (w, 0, 1))
ddf6b9a3 12288 rc = CURSOR_MOVEMENT_MUST_SCROLL;
cb617e7c
GM
12289 else
12290 rc = CURSOR_MOVEMENT_SUCCESS;
440fc135 12291 }
47589c8c
GM
12292 }
12293 else if (scroll_p)
cb617e7c 12294 rc = CURSOR_MOVEMENT_MUST_SCROLL;
47589c8c
GM
12295 else
12296 {
12297 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
cb617e7c 12298 rc = CURSOR_MOVEMENT_SUCCESS;
47589c8c
GM
12299 }
12300 }
12301 }
12302
12303 return rc;
12304}
12305
9a08d928
SM
12306void
12307set_vertical_scroll_bar (w)
12308 struct window *w;
12309{
12310 int start, end, whole;
12311
12312 /* Calculate the start and end positions for the current window.
12313 At some point, it would be nice to choose between scrollbars
12314 which reflect the whole buffer size, with special markers
12315 indicating narrowing, and scrollbars which reflect only the
12316 visible region.
be21b925 12317
9a08d928
SM
12318 Note that mini-buffers sometimes aren't displaying any text. */
12319 if (!MINI_WINDOW_P (w)
12320 || (w == XWINDOW (minibuf_window)
12321 && NILP (echo_area_buffer[0])))
12322 {
12323 struct buffer *buf = XBUFFER (w->buffer);
12324 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12325 start = marker_position (w->start) - BUF_BEGV (buf);
12326 /* I don't think this is guaranteed to be right. For the
12327 moment, we'll pretend it is. */
12328 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
be21b925 12329
9a08d928
SM
12330 if (end < start)
12331 end = start;
12332 if (whole < (end - start))
12333 whole = end - start;
12334 }
12335 else
12336 start = end = whole = 0;
12337
12338 /* Indicate what this scroll bar ought to be displaying now. */
12339 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12340}
47589c8c 12341
88e6b646 12342
5f5c8ee5 12343/* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
b60c9653
RS
12344 selected_window is redisplayed.
12345
12346 We can return without actually redisplaying the window if
12347 fonts_changed_p is nonzero. In that case, redisplay_internal will
12348 retry. */
90adcf20 12349
a2889657 12350static void
5f5c8ee5 12351redisplay_window (window, just_this_one_p)
a2889657 12352 Lisp_Object window;
5f5c8ee5 12353 int just_this_one_p;
a2889657 12354{
5f5c8ee5
GM
12355 struct window *w = XWINDOW (window);
12356 struct frame *f = XFRAME (w->frame);
12357 struct buffer *buffer = XBUFFER (w->buffer);
a2889657 12358 struct buffer *old = current_buffer;
5f5c8ee5 12359 struct text_pos lpoint, opoint, startp;
e481f960 12360 int update_mode_line;
5f5c8ee5
GM
12361 int tem;
12362 struct it it;
12363 /* Record it now because it's overwritten. */
12364 int current_matrix_up_to_date_p = 0;
88e6b646 12365 int used_current_matrix_p = 0;
c62c1bb5
RS
12366 /* This is less strict than current_matrix_up_to_date_p.
12367 It indictes that the buffer contents and narrowing are unchanged. */
12368 int buffer_unchanged_p = 0;
5f5c8ee5 12369 int temp_scroll_step = 0;
331379bf 12370 int count = SPECPDL_INDEX ();
47589c8c 12371 int rc;
e10ee30c 12372 int centering_position = -1;
03b0a4b4 12373 int last_line_misfit = 0;
a2889657 12374
5f5c8ee5
GM
12375 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12376 opoint = lpoint;
a2889657 12377
5f5c8ee5
GM
12378 /* W must be a leaf window here. */
12379 xassert (!NILP (w->buffer));
12380#if GLYPH_DEBUG
12381 *w->desired_matrix->method = 0;
12382#endif
2e54982e
RS
12383
12384 specbind (Qinhibit_point_motion_hooks, Qt);
9142dd5b
GM
12385
12386 reconsider_clip_changes (w, buffer);
2311178e
TTN
12387
12388 /* Has the mode line to be updated? */
5f5c8ee5
GM
12389 update_mode_line = (!NILP (w->update_mode_line)
12390 || update_mode_lines
c62c1bb5
RS
12391 || buffer->clip_changed
12392 || buffer->prevent_redisplay_optimizations_p);
8de2d90b
JB
12393
12394 if (MINI_WINDOW_P (w))
12395 {
5f5c8ee5 12396 if (w == XWINDOW (echo_area_window)
c6e89d6c 12397 && !NILP (echo_area_buffer[0]))
5f5c8ee5
GM
12398 {
12399 if (update_mode_line)
12400 /* We may have to update a tty frame's menu bar or a
e037b9ec 12401 tool-bar. Example `M-x C-h C-h C-g'. */
5f5c8ee5
GM
12402 goto finish_menu_bars;
12403 else
12404 /* We've already displayed the echo area glyphs in this window. */
12405 goto finish_scroll_bars;
12406 }
b2b5455d
RS
12407 else if ((w != XWINDOW (minibuf_window)
12408 || minibuf_level == 0)
c0bcce6f
JPW
12409 /* When buffer is nonempty, redisplay window normally. */
12410 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
3354fdcf
RS
12411 /* Quail displays non-mini buffers in minibuffer window.
12412 In that case, redisplay the window normally. */
b2b5455d 12413 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
8de2d90b 12414 {
3354fdcf
RS
12415 /* W is a mini-buffer window, but it's not active, so clear
12416 it. */
5f5c8ee5
GM
12417 int yb = window_text_bottom_y (w);
12418 struct glyph_row *row;
12419 int y;
12420
12421 for (y = 0, row = w->desired_matrix->rows;
12422 y < yb;
12423 y += row->height, ++row)
12424 blank_row (w, row, y);
88f22aff 12425 goto finish_scroll_bars;
8de2d90b 12426 }
c095a1dd
GM
12427
12428 clear_glyph_matrix (w->desired_matrix);
8de2d90b 12429 }
a2889657 12430
5f5c8ee5
GM
12431 /* Otherwise set up data on this window; select its buffer and point
12432 value. */
6a93695f
GM
12433 /* Really select the buffer, for the sake of buffer-local
12434 variables. */
12435 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5
GM
12436 SET_TEXT_POS (opoint, PT, PT_BYTE);
12437
12438 current_matrix_up_to_date_p
12439 = (!NILP (w->window_end_valid)
12440 && !current_buffer->clip_changed
c62c1bb5 12441 && !current_buffer->prevent_redisplay_optimizations_p
5f5c8ee5
GM
12442 && XFASTINT (w->last_modified) >= MODIFF
12443 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
e481f960 12444
c62c1bb5
RS
12445 buffer_unchanged_p
12446 = (!NILP (w->window_end_valid)
12447 && !current_buffer->clip_changed
3f1258d0 12448 && XFASTINT (w->last_modified) >= MODIFF
c62c1bb5
RS
12449 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12450
5f5c8ee5
GM
12451 /* When windows_or_buffers_changed is non-zero, we can't rely on
12452 the window end being valid, so set it to nil there. */
12453 if (windows_or_buffers_changed)
12454 {
12455 /* If window starts on a continuation line, maybe adjust the
12456 window start in case the window's width changed. */
12457 if (XMARKER (w->start)->buffer == current_buffer)
12458 compute_window_start_on_continuation_line (w);
2311178e 12459
5f5c8ee5
GM
12460 w->window_end_valid = Qnil;
12461 }
12adba34 12462
5f5c8ee5
GM
12463 /* Some sanity checks. */
12464 CHECK_WINDOW_END (w);
12465 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12adba34 12466 abort ();
5f5c8ee5 12467 if (BYTEPOS (opoint) < CHARPOS (opoint))
12adba34 12468 abort ();
a2889657 12469
28995e67
RS
12470 /* If %c is in mode line, update it if needed. */
12471 if (!NILP (w->column_number_displayed)
12472 /* This alternative quickly identifies a common case
12473 where no change is needed. */
12474 && !(PT == XFASTINT (w->last_point)
8850a573
RS
12475 && XFASTINT (w->last_modified) >= MODIFF
12476 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
2311178e
TTN
12477 && (XFASTINT (w->column_number_displayed)
12478 != (int) current_column ())) /* iftc */
12479 update_mode_line = 1;
28995e67 12480
5f5c8ee5
GM
12481 /* Count number of windows showing the selected buffer. An indirect
12482 buffer counts as its base buffer. */
12483 if (!just_this_one_p)
42640f83
RS
12484 {
12485 struct buffer *current_base, *window_base;
12486 current_base = current_buffer;
12487 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12488 if (current_base->base_buffer)
12489 current_base = current_base->base_buffer;
12490 if (window_base->base_buffer)
12491 window_base = window_base->base_buffer;
12492 if (current_base == window_base)
12493 buffer_shared++;
12494 }
a2889657 12495
5f5c8ee5
GM
12496 /* Point refers normally to the selected window. For any other
12497 window, set up appropriate value. */
a2889657
JB
12498 if (!EQ (window, selected_window))
12499 {
12adba34
RS
12500 int new_pt = XMARKER (w->pointm)->charpos;
12501 int new_pt_byte = marker_byte_position (w->pointm);
f67a0f51 12502 if (new_pt < BEGV)
a2889657 12503 {
f67a0f51 12504 new_pt = BEGV;
12adba34
RS
12505 new_pt_byte = BEGV_BYTE;
12506 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
a2889657 12507 }
f67a0f51 12508 else if (new_pt > (ZV - 1))
a2889657 12509 {
f67a0f51 12510 new_pt = ZV;
12adba34
RS
12511 new_pt_byte = ZV_BYTE;
12512 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
a2889657 12513 }
2311178e 12514
f67a0f51 12515 /* We don't use SET_PT so that the point-motion hooks don't run. */
12adba34 12516 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
a2889657
JB
12517 }
12518
f4faa47c 12519 /* If any of the character widths specified in the display table
5f5c8ee5
GM
12520 have changed, invalidate the width run cache. It's true that
12521 this may be a bit late to catch such changes, but the rest of
f4faa47c
JB
12522 redisplay goes (non-fatally) haywire when the display table is
12523 changed, so why should we worry about doing any better? */
12524 if (current_buffer->width_run_cache)
12525 {
f908610f 12526 struct Lisp_Char_Table *disptab = buffer_display_table ();
f4faa47c
JB
12527
12528 if (! disptab_matches_widthtab (disptab,
12529 XVECTOR (current_buffer->width_table)))
12530 {
12531 invalidate_region_cache (current_buffer,
12532 current_buffer->width_run_cache,
12533 BEG, Z);
12534 recompute_width_table (current_buffer, disptab);
12535 }
12536 }
12537
a2889657 12538 /* If window-start is screwed up, choose a new one. */
a2889657
JB
12539 if (XMARKER (w->start)->buffer != current_buffer)
12540 goto recenter;
12541
5f5c8ee5 12542 SET_TEXT_POS_FROM_MARKER (startp, w->start);
a2889657 12543
cf0df6ab
RS
12544 /* If someone specified a new starting point but did not insist,
12545 check whether it can be used. */
cfad01b4
GM
12546 if (!NILP (w->optional_new_start)
12547 && CHARPOS (startp) >= BEGV
12548 && CHARPOS (startp) <= ZV)
cf0df6ab
RS
12549 {
12550 w->optional_new_start = Qnil;
5f5c8ee5
GM
12551 start_display (&it, w, startp);
12552 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12553 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12554 if (IT_CHARPOS (it) == PT)
cf0df6ab 12555 w->force_start = Qt;
29b3ea63
KS
12556 /* IT may overshoot PT if text at PT is invisible. */
12557 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12558 w->force_start = Qt;
12559
12560
cf0df6ab
RS
12561 }
12562
8de2d90b 12563 /* Handle case where place to start displaying has been specified,
aa6d10fa 12564 unless the specified location is outside the accessible range. */
9472f927
GM
12565 if (!NILP (w->force_start)
12566 || w->frozen_window_start_p)
a2889657 12567 {
b60c9653
RS
12568 /* We set this later on if we have to adjust point. */
12569 int new_vpos = -1;
493c1d49 12570 int val;
b60c9653 12571
e63574d7 12572 w->force_start = Qnil;
5f5c8ee5 12573 w->vscroll = 0;
b5174a51 12574 w->window_end_valid = Qnil;
5f5c8ee5
GM
12575
12576 /* Forget any recorded base line for line number display. */
c62c1bb5 12577 if (!buffer_unchanged_p)
5f5c8ee5
GM
12578 w->base_line_number = Qnil;
12579
75c43375
RS
12580 /* Redisplay the mode line. Select the buffer properly for that.
12581 Also, run the hook window-scroll-functions
12582 because we have scrolled. */
e63574d7
RS
12583 /* Note, we do this after clearing force_start because
12584 if there's an error, it is better to forget about force_start
12585 than to get into an infinite loop calling the hook functions
12586 and having them get more errors. */
75c43375
RS
12587 if (!update_mode_line
12588 || ! NILP (Vwindow_scroll_functions))
e481f960 12589 {
e481f960
RS
12590 update_mode_line = 1;
12591 w->update_mode_line = Qt;
5f5c8ee5 12592 startp = run_window_scroll_functions (window, startp);
e481f960 12593 }
2311178e 12594
ac90c44f
GM
12595 w->last_modified = make_number (0);
12596 w->last_overlay_modified = make_number (0);
5f5c8ee5
GM
12597 if (CHARPOS (startp) < BEGV)
12598 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12599 else if (CHARPOS (startp) > ZV)
12600 SET_TEXT_POS (startp, ZV, ZV_BYTE);
2311178e
TTN
12601
12602 /* Redisplay, then check if cursor has been set during the
5f5c8ee5 12603 redisplay. Give up if new fonts were loaded. */
493c1d49
RS
12604 val = try_window (window, startp, 1);
12605 if (!val)
5f5c8ee5
GM
12606 {
12607 w->force_start = Qt;
12608 clear_glyph_matrix (w->desired_matrix);
b60c9653 12609 goto need_larger_matrices;
5f5c8ee5 12610 }
493c1d49
RS
12611 /* Point was outside the scroll margins. */
12612 if (val < 0)
12613 new_vpos = window_box_height (w) / 2;
5f5c8ee5 12614
9472f927 12615 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
5f5c8ee5 12616 {
b28cb6ed
GM
12617 /* If point does not appear, try to move point so it does
12618 appear. The desired matrix has been built above, so we
12619 can use it here. */
b60c9653
RS
12620 new_vpos = window_box_height (w) / 2;
12621 }
12622
e10ee30c 12623 if (!cursor_row_fully_visible_p (w, 0, 0))
b60c9653
RS
12624 {
12625 /* Point does appear, but on a line partly visible at end of window.
12626 Move it back to a fully-visible line. */
12627 new_vpos = window_box_height (w);
12628 }
12629
12630 /* If we need to move point for either of the above reasons,
12631 now actually do it. */
12632 if (new_vpos >= 0)
12633 {
b28cb6ed
GM
12634 struct glyph_row *row;
12635
b28cb6ed 12636 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
b60c9653 12637 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
5f5c8ee5
GM
12638 ++row;
12639
12640 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12641 MATRIX_ROW_START_BYTEPOS (row));
12642
90adcf20 12643 if (w != XWINDOW (selected_window))
12adba34 12644 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5f5c8ee5
GM
12645 else if (current_buffer == old)
12646 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12647
12648 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
2311178e 12649
5f5c8ee5
GM
12650 /* If we are highlighting the region, then we just changed
12651 the region, so redisplay to show it. */
df0b5ea1
RS
12652 if (!NILP (Vtransient_mark_mode)
12653 && !NILP (current_buffer->mark_active))
6f27fa9b 12654 {
5f5c8ee5 12655 clear_glyph_matrix (w->desired_matrix);
493c1d49 12656 if (!try_window (window, startp, 0))
cb617e7c 12657 goto need_larger_matrices;
6f27fa9b 12658 }
a2889657 12659 }
5f5c8ee5 12660
5f5c8ee5
GM
12661#if GLYPH_DEBUG
12662 debug_method_add (w, "forced window start");
12663#endif
a2889657
JB
12664 goto done;
12665 }
12666
5f5c8ee5 12667 /* Handle case where text has not changed, only point, and it has
3f029ea0
RS
12668 not moved off the frame, and we are not retrying after hscroll.
12669 (current_matrix_up_to_date_p is nonzero when retrying.) */
12670 if (current_matrix_up_to_date_p
47589c8c 12671 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
cb617e7c 12672 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
a2889657 12673 {
cb617e7c
GM
12674 switch (rc)
12675 {
12676 case CURSOR_MOVEMENT_SUCCESS:
88e6b646 12677 used_current_matrix_p = 1;
cb617e7c 12678 goto done;
2311178e 12679
b60c9653 12680#if 0 /* try_cursor_movement never returns this value. */
cb617e7c
GM
12681 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
12682 goto need_larger_matrices;
b60c9653 12683#endif
2311178e 12684
cb617e7c
GM
12685 case CURSOR_MOVEMENT_MUST_SCROLL:
12686 goto try_to_scroll;
2311178e 12687
cb617e7c
GM
12688 default:
12689 abort ();
12690 }
a2889657
JB
12691 }
12692 /* If current starting point was originally the beginning of a line
12693 but no longer is, find a new starting point. */
265a9e55 12694 else if (!NILP (w->start_at_line_beg)
5f5c8ee5
GM
12695 && !(CHARPOS (startp) <= BEGV
12696 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
a2889657 12697 {
5f5c8ee5
GM
12698#if GLYPH_DEBUG
12699 debug_method_add (w, "recenter 1");
12700#endif
a2889657
JB
12701 goto recenter;
12702 }
2311178e 12703
27d16f05
GM
12704 /* Try scrolling with try_window_id. Value is > 0 if update has
12705 been done, it is -1 if we know that the same window start will
12706 not work. It is 0 if unsuccessful for some other reason. */
12707 else if ((tem = try_window_id (w)) != 0)
a2889657 12708 {
5f5c8ee5 12709#if GLYPH_DEBUG
ef121659 12710 debug_method_add (w, "try_window_id %d", tem);
5f5c8ee5
GM
12711#endif
12712
12713 if (fonts_changed_p)
cb617e7c 12714 goto need_larger_matrices;
a2889657
JB
12715 if (tem > 0)
12716 goto done;
ef121659 12717
5f5c8ee5
GM
12718 /* Otherwise try_window_id has returned -1 which means that we
12719 don't want the alternative below this comment to execute. */
a2889657 12720 }
5f5c8ee5
GM
12721 else if (CHARPOS (startp) >= BEGV
12722 && CHARPOS (startp) <= ZV
12723 && PT >= CHARPOS (startp)
12724 && (CHARPOS (startp) < ZV
e9874cee 12725 /* Avoid starting at end of buffer. */
5f5c8ee5 12726 || CHARPOS (startp) == BEGV
8850a573
RS
12727 || (XFASTINT (w->last_modified) >= MODIFF
12728 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
a2889657 12729 {
5f5c8ee5
GM
12730#if GLYPH_DEBUG
12731 debug_method_add (w, "same window start");
12732#endif
2311178e 12733
5f5c8ee5
GM
12734 /* Try to redisplay starting at same place as before.
12735 If point has not moved off frame, accept the results. */
12736 if (!current_matrix_up_to_date_p
12737 /* Don't use try_window_reusing_current_matrix in this case
15e26c76
GM
12738 because a window scroll function can have changed the
12739 buffer. */
5f5c8ee5
GM
12740 || !NILP (Vwindow_scroll_functions)
12741 || MINI_WINDOW_P (w)
70567d1c
RS
12742 || !(used_current_matrix_p
12743 = try_window_reusing_current_matrix (w)))
5f5c8ee5
GM
12744 {
12745 IF_DEBUG (debug_method_add (w, "1"));
493c1d49
RS
12746 if (try_window (window, startp, 1) < 0)
12747 /* -1 means we need to scroll.
12748 0 means we need new matrices, but fonts_changed_p
12749 is set in that case, so we will detect it below. */
12750 goto try_to_scroll;
5f5c8ee5
GM
12751 }
12752
12753 if (fonts_changed_p)
cb617e7c 12754 goto need_larger_matrices;
2311178e 12755
5f5c8ee5 12756 if (w->cursor.vpos >= 0)
aa6d10fa 12757 {
2311178e 12758 if (!just_this_one_p
5f5c8ee5 12759 || current_buffer->clip_changed
9142dd5b 12760 || BEG_UNCHANGED < CHARPOS (startp))
aa6d10fa
RS
12761 /* Forget any recorded base line for line number display. */
12762 w->base_line_number = Qnil;
2311178e 12763
e10ee30c 12764 if (!cursor_row_fully_visible_p (w, 1, 0))
03b0a4b4
RS
12765 {
12766 clear_glyph_matrix (w->desired_matrix);
12767 last_line_misfit = 1;
12768 }
ddf6b9a3 12769 /* Drop through and scroll. */
ef3c2c73
RS
12770 else
12771 goto done;
aa6d10fa 12772 }
a2889657 12773 else
5f5c8ee5 12774 clear_glyph_matrix (w->desired_matrix);
a2889657
JB
12775 }
12776
5f5c8ee5
GM
12777 try_to_scroll:
12778
ac90c44f
GM
12779 w->last_modified = make_number (0);
12780 w->last_overlay_modified = make_number (0);
5f5c8ee5 12781
e481f960
RS
12782 /* Redisplay the mode line. Select the buffer properly for that. */
12783 if (!update_mode_line)
12784 {
e481f960
RS
12785 update_mode_line = 1;
12786 w->update_mode_line = Qt;
12787 }
a2889657 12788
5f5c8ee5
GM
12789 /* Try to scroll by specified few lines. */
12790 if ((scroll_conservatively
12791 || scroll_step
12792 || temp_scroll_step
12793 || NUMBERP (current_buffer->scroll_up_aggressively)
12794 || NUMBERP (current_buffer->scroll_down_aggressively))
09cacf9c 12795 && !current_buffer->clip_changed
2311178e 12796 && CHARPOS (startp) >= BEGV
5f5c8ee5 12797 && CHARPOS (startp) <= ZV)
0789adb2 12798 {
5f5c8ee5
GM
12799 /* The function returns -1 if new fonts were loaded, 1 if
12800 successful, 0 if not successful. */
12801 int rc = try_scrolling (window, just_this_one_p,
12802 scroll_conservatively,
12803 scroll_step,
03b0a4b4 12804 temp_scroll_step, last_line_misfit);
cb617e7c
GM
12805 switch (rc)
12806 {
12807 case SCROLLING_SUCCESS:
12808 goto done;
2311178e 12809
cb617e7c
GM
12810 case SCROLLING_NEED_LARGER_MATRICES:
12811 goto need_larger_matrices;
2311178e 12812
cb617e7c
GM
12813 case SCROLLING_FAILED:
12814 break;
2311178e 12815
cb617e7c
GM
12816 default:
12817 abort ();
12818 }
5f5c8ee5 12819 }
f9c8af06 12820
5f5c8ee5 12821 /* Finally, just choose place to start which centers point */
5936754e 12822
5f5c8ee5 12823 recenter:
e10ee30c
KS
12824 if (centering_position < 0)
12825 centering_position = window_box_height (w) / 2;
44173109 12826
5f5c8ee5
GM
12827#if GLYPH_DEBUG
12828 debug_method_add (w, "recenter");
12829#endif
0789adb2 12830
5f5c8ee5 12831 /* w->vscroll = 0; */
0789adb2 12832
5f5c8ee5 12833 /* Forget any previously recorded base line for line number display. */
c62c1bb5 12834 if (!buffer_unchanged_p)
5f5c8ee5
GM
12835 w->base_line_number = Qnil;
12836
12837 /* Move backward half the height of the window. */
12838 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12839 it.current_y = it.last_visible_y;
ddf6b9a3 12840 move_it_vertically_backward (&it, centering_position);
5f5c8ee5
GM
12841 xassert (IT_CHARPOS (it) >= BEGV);
12842
12843 /* The function move_it_vertically_backward may move over more
12844 than the specified y-distance. If it->w is small, e.g. a
12845 mini-buffer window, we may end up in front of the window's
12846 display area. Start displaying at the start of the line
12847 containing PT in this case. */
12848 if (it.current_y <= 0)
12849 {
12850 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
546f98b8 12851 move_it_vertically_backward (&it, 0);
527d43c2
KS
12852#if 0
12853 /* I think this assert is bogus if buffer contains
12854 invisible text or images. KFS. */
5f5c8ee5 12855 xassert (IT_CHARPOS (it) <= PT);
527d43c2 12856#endif
5f5c8ee5 12857 it.current_y = 0;
0789adb2
RS
12858 }
12859
5f5c8ee5 12860 it.current_x = it.hpos = 0;
2311178e 12861
5f5c8ee5
GM
12862 /* Set startp here explicitly in case that helps avoid an infinite loop
12863 in case the window-scroll-functions functions get errors. */
12864 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12865
12866 /* Run scroll hooks. */
12867 startp = run_window_scroll_functions (window, it.current.pos);
12868
12869 /* Redisplay the window. */
12870 if (!current_matrix_up_to_date_p
12871 || windows_or_buffers_changed
5fb96e96 12872 || cursor_type_changed
5f5c8ee5
GM
12873 /* Don't use try_window_reusing_current_matrix in this case
12874 because it can have changed the buffer. */
12875 || !NILP (Vwindow_scroll_functions)
12876 || !just_this_one_p
12877 || MINI_WINDOW_P (w)
70567d1c
RS
12878 || !(used_current_matrix_p
12879 = try_window_reusing_current_matrix (w)))
493c1d49 12880 try_window (window, startp, 0);
5f5c8ee5
GM
12881
12882 /* If new fonts have been loaded (due to fontsets), give up. We
12883 have to start a new redisplay since we need to re-adjust glyph
12884 matrices. */
12885 if (fonts_changed_p)
cb617e7c 12886 goto need_larger_matrices;
5f5c8ee5
GM
12887
12888 /* If cursor did not appear assume that the middle of the window is
12889 in the first line of the window. Do it again with the next line.
12890 (Imagine a window of height 100, displaying two lines of height
12891 60. Moving back 50 from it->last_visible_y will end in the first
12892 line.) */
12893 if (w->cursor.vpos < 0)
a2889657 12894 {
5f5c8ee5
GM
12895 if (!NILP (w->window_end_valid)
12896 && PT >= Z - XFASTINT (w->window_end_pos))
a2889657 12897 {
5f5c8ee5
GM
12898 clear_glyph_matrix (w->desired_matrix);
12899 move_it_by_lines (&it, 1, 0);
493c1d49 12900 try_window (window, it.current.pos, 0);
a2889657 12901 }
5f5c8ee5 12902 else if (PT < IT_CHARPOS (it))
a2889657 12903 {
5f5c8ee5
GM
12904 clear_glyph_matrix (w->desired_matrix);
12905 move_it_by_lines (&it, -1, 0);
493c1d49 12906 try_window (window, it.current.pos, 0);
5f5c8ee5
GM
12907 }
12908 else
12909 {
12910 /* Not much we can do about it. */
a2889657 12911 }
a2889657 12912 }
010494d0 12913
5f5c8ee5
GM
12914 /* Consider the following case: Window starts at BEGV, there is
12915 invisible, intangible text at BEGV, so that display starts at
12916 some point START > BEGV. It can happen that we are called with
12917 PT somewhere between BEGV and START. Try to handle that case. */
12918 if (w->cursor.vpos < 0)
835766b6 12919 {
5f5c8ee5
GM
12920 struct glyph_row *row = w->current_matrix->rows;
12921 if (row->mode_line_p)
12922 ++row;
12923 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
835766b6 12924 }
2311178e 12925
e10ee30c 12926 if (!cursor_row_fully_visible_p (w, 0, 0))
ddf6b9a3 12927 {
932357b4
KS
12928 /* If vscroll is enabled, disable it and try again. */
12929 if (w->vscroll)
12930 {
12931 w->vscroll = 0;
12932 clear_glyph_matrix (w->desired_matrix);
12933 goto recenter;
12934 }
2a6d0874 12935
ddf6b9a3
RS
12936 /* If centering point failed to make the whole line visible,
12937 put point at the top instead. That has to make the whole line
12938 visible, if it can be done. */
6f67f013
KS
12939 if (centering_position == 0)
12940 goto done;
e10ee30c 12941
cb9d4a9f 12942 clear_glyph_matrix (w->desired_matrix);
ddf6b9a3 12943 centering_position = 0;
e10ee30c 12944 goto recenter;
ddf6b9a3 12945 }
b5174a51 12946
74d481ac
GM
12947 done:
12948
5f5c8ee5
GM
12949 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12950 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12951 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12952 ? Qt : Qnil);
a2889657 12953
5f5c8ee5 12954 /* Display the mode line, if we must. */
e481f960 12955 if ((update_mode_line
aa6d10fa 12956 /* If window not full width, must redo its mode line
2311178e 12957 if (a) the window to its side is being redone and
5f5c8ee5
GM
12958 (b) we do a frame-based redisplay. This is a consequence
12959 of how inverted lines are drawn in frame-based redisplay. */
2311178e 12960 || (!just_this_one_p
5f5c8ee5
GM
12961 && !FRAME_WINDOW_P (f)
12962 && !WINDOW_FULL_WIDTH_P (w))
12963 /* Line number to display. */
155ef550 12964 || INTEGERP (w->base_line_pos)
5f5c8ee5 12965 /* Column number is displayed and different from the one displayed. */
155ef550 12966 || (!NILP (w->column_number_displayed)
2311178e
TTN
12967 && (XFASTINT (w->column_number_displayed)
12968 != (int) current_column ()))) /* iftc */
5f5c8ee5
GM
12969 /* This means that the window has a mode line. */
12970 && (WINDOW_WANTS_MODELINE_P (w)
045dee35 12971 || WINDOW_WANTS_HEADER_LINE_P (w)))
5ba50c51 12972 {
5f5c8ee5
GM
12973 display_mode_lines (w);
12974
12975 /* If mode line height has changed, arrange for a thorough
12976 immediate redisplay using the correct mode line height. */
12977 if (WINDOW_WANTS_MODELINE_P (w)
12978 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
5ba50c51 12979 {
5f5c8ee5
GM
12980 fonts_changed_p = 1;
12981 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12982 = DESIRED_MODE_LINE_HEIGHT (w);
5ba50c51 12983 }
2311178e 12984
5f5c8ee5
GM
12985 /* If top line height has changed, arrange for a thorough
12986 immediate redisplay using the correct mode line height. */
045dee35
GM
12987 if (WINDOW_WANTS_HEADER_LINE_P (w)
12988 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
5f5c8ee5
GM
12989 {
12990 fonts_changed_p = 1;
045dee35
GM
12991 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12992 = DESIRED_HEADER_LINE_HEIGHT (w);
5f5c8ee5
GM
12993 }
12994
12995 if (fonts_changed_p)
cb617e7c 12996 goto need_larger_matrices;
5ba50c51 12997 }
5f5c8ee5
GM
12998
12999 if (!line_number_displayed
13000 && !BUFFERP (w->base_line_pos))
aa6d10fa
RS
13001 {
13002 w->base_line_pos = Qnil;
13003 w->base_line_number = Qnil;
13004 }
a2889657 13005
5f5c8ee5 13006 finish_menu_bars:
2311178e 13007
7ce2c095 13008 /* When we reach a frame's selected window, redo the frame's menu bar. */
e481f960 13009 if (update_mode_line
5f5c8ee5
GM
13010 && EQ (FRAME_SELECTED_WINDOW (f), window))
13011 {
13012 int redisplay_menu_p = 0;
488dd4c4 13013 int redisplay_tool_bar_p = 0;
5f5c8ee5
GM
13014
13015 if (FRAME_WINDOW_P (f))
13016 {
488dd4c4
JD
13017#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13018 || defined (USE_GTK)
5f5c8ee5 13019 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
76412d64 13020#else
5f5c8ee5 13021 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
76412d64 13022#endif
5f5c8ee5
GM
13023 }
13024 else
13025 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13026
13027 if (redisplay_menu_p)
13028 display_menu_bar (w);
13029
13030#ifdef HAVE_WINDOW_SYSTEM
488dd4c4
JD
13031#ifdef USE_GTK
13032 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13033#else
13034 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13035 && (FRAME_TOOL_BAR_LINES (f) > 0
13036 || auto_resize_tool_bars_p);
177c0ea7 13037
488dd4c4
JD
13038#endif
13039
13040 if (redisplay_tool_bar_p)
13041 redisplay_tool_bar (f);
5f5c8ee5
GM
13042#endif
13043 }
7ce2c095 13044
7af0e8d7 13045#ifdef HAVE_WINDOW_SYSTEM
f459b8c0 13046 if (FRAME_WINDOW_P (f)
61d6a058
KS
13047 && update_window_fringes (w, (just_this_one_p
13048 || (!used_current_matrix_p && !overlay_arrow_seen)
13049 || w->pseudo_window_p)))
88e6b646
KS
13050 {
13051 update_begin (f);
13052 BLOCK_INPUT;
0a1527d6
KS
13053 if (draw_window_fringes (w, 1))
13054 x_draw_vertical_border (w);
88e6b646
KS
13055 UNBLOCK_INPUT;
13056 update_end (f);
13057 }
7af0e8d7 13058#endif /* HAVE_WINDOW_SYSTEM */
88e6b646 13059
b60c9653
RS
13060 /* We go to this label, with fonts_changed_p nonzero,
13061 if it is necessary to try again using larger glyph matrices.
13062 We have to redeem the scroll bar even in this case,
13063 because the loop in redisplay_internal expects that. */
cb617e7c
GM
13064 need_larger_matrices:
13065 ;
88f22aff 13066 finish_scroll_bars:
5f5c8ee5 13067
da8b7f4f 13068 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
30c566e4 13069 {
9a08d928
SM
13070 /* Set the thumb's position and size. */
13071 set_vertical_scroll_bar (w);
30c566e4 13072
5f5c8ee5
GM
13073 /* Note that we actually used the scroll bar attached to this
13074 window, so it shouldn't be deleted at the end of redisplay. */
504454e8 13075 redeem_scroll_bar_hook (w);
30c566e4 13076 }
b1d1124b 13077
5f5c8ee5
GM
13078 /* Restore current_buffer and value of point in it. */
13079 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
6a93695f 13080 set_buffer_internal_1 (old);
5f5c8ee5 13081 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
2e54982e
RS
13082
13083 unbind_to (count, Qnil);
a2889657 13084}
a2889657 13085
5f5c8ee5
GM
13086
13087/* Build the complete desired matrix of WINDOW with a window start
493c1d49
RS
13088 buffer position POS.
13089
13090 Value is 1 if successful. It is zero if fonts were loaded during
13091 redisplay which makes re-adjusting glyph matrices necessary, and -1
13092 if point would appear in the scroll margins.
13093 (We check that only if CHECK_MARGINS is nonzero. */
5f5c8ee5
GM
13094
13095int
493c1d49 13096try_window (window, pos, check_margins)
a2889657 13097 Lisp_Object window;
5f5c8ee5 13098 struct text_pos pos;
493c1d49 13099 int check_margins;
5f5c8ee5
GM
13100{
13101 struct window *w = XWINDOW (window);
13102 struct it it;
13103 struct glyph_row *last_text_row = NULL;
9cbab4ff 13104
5f5c8ee5
GM
13105 /* Make POS the new window start. */
13106 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12adba34 13107
5f5c8ee5
GM
13108 /* Mark cursor position as unknown. No overlay arrow seen. */
13109 w->cursor.vpos = -1;
a2889657 13110 overlay_arrow_seen = 0;
642eefc6 13111
5f5c8ee5
GM
13112 /* Initialize iterator and info to start at POS. */
13113 start_display (&it, w, pos);
a2889657 13114
5f5c8ee5
GM
13115 /* Display all lines of W. */
13116 while (it.current_y < it.last_visible_y)
13117 {
13118 if (display_line (&it))
13119 last_text_row = it.glyph_row - 1;
13120 if (fonts_changed_p)
13121 return 0;
13122 }
a2889657 13123
493c1d49 13124 /* Don't let the cursor end in the scroll margins. */
e3ea3a51
KS
13125 if (check_margins
13126 && !MINI_WINDOW_P (w))
493c1d49 13127 {
edf3d146 13128 int this_scroll_margin;
493c1d49
RS
13129
13130 this_scroll_margin = max (0, scroll_margin);
13131 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13132 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
493c1d49
RS
13133
13134 if ((w->cursor.y < this_scroll_margin
58e5658e
KS
13135 && CHARPOS (pos) > BEGV
13136 && IT_CHARPOS (it) < ZV)
9b869380
RS
13137 /* rms: considering make_cursor_line_fully_visible_p here
13138 seems to give wrong results. We don't want to recenter
13139 when the last line is partly visible, we want to allow
13140 that case to be handled in the usual way. */
13141 || (w->cursor.y + 1) > it.last_visible_y)
493c1d49
RS
13142 {
13143 w->cursor.vpos = -1;
13144 clear_glyph_matrix (w->desired_matrix);
13145 return -1;
13146 }
13147 }
13148
5f5c8ee5
GM
13149 /* If bottom moved off end of frame, change mode line percentage. */
13150 if (XFASTINT (w->window_end_pos) <= 0
13151 && Z != IT_CHARPOS (it))
a2889657
JB
13152 w->update_mode_line = Qt;
13153
5f5c8ee5
GM
13154 /* Set window_end_pos to the offset of the last character displayed
13155 on the window from the end of current_buffer. Set
13156 window_end_vpos to its row number. */
13157 if (last_text_row)
13158 {
13159 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13160 w->window_end_bytepos
13161 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
13162 w->window_end_pos
13163 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13164 w->window_end_vpos
13165 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
5f5c8ee5
GM
13166 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13167 ->displays_text_p);
13168 }
13169 else
13170 {
86bf3faa
RS
13171 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13172 w->window_end_pos = make_number (Z - ZV);
13173 w->window_end_vpos = make_number (0);
5f5c8ee5 13174 }
2311178e 13175
a2889657
JB
13176 /* But that is not valid info until redisplay finishes. */
13177 w->window_end_valid = Qnil;
5f5c8ee5 13178 return 1;
a2889657 13179}
5f5c8ee5
GM
13180
13181
a2889657 13182\f
5f5c8ee5
GM
13183/************************************************************************
13184 Window redisplay reusing current matrix when buffer has not changed
13185 ************************************************************************/
13186
13187/* Try redisplay of window W showing an unchanged buffer with a
13188 different window start than the last time it was displayed by
13189 reusing its current matrix. Value is non-zero if successful.
13190 W->start is the new window start. */
a2889657
JB
13191
13192static int
5f5c8ee5
GM
13193try_window_reusing_current_matrix (w)
13194 struct window *w;
a2889657 13195{
5f5c8ee5
GM
13196 struct frame *f = XFRAME (w->frame);
13197 struct glyph_row *row, *bottom_row;
13198 struct it it;
13199 struct run run;
13200 struct text_pos start, new_start;
13201 int nrows_scrolled, i;
13202 struct glyph_row *last_text_row;
13203 struct glyph_row *last_reused_text_row;
13204 struct glyph_row *start_row;
13205 int start_vpos, min_y, max_y;
75c5350a 13206
69d1f7c9 13207#if GLYPH_DEBUG
76cb5e06
GM
13208 if (inhibit_try_window_reusing)
13209 return 0;
13210#endif
13211
d18354b6
GM
13212 if (/* This function doesn't handle terminal frames. */
13213 !FRAME_WINDOW_P (f)
13214 /* Don't try to reuse the display if windows have been split
13215 or such. */
5fb96e96
RS
13216 || windows_or_buffers_changed
13217 || cursor_type_changed)
5f5c8ee5 13218 return 0;
a2889657 13219
5f5c8ee5
GM
13220 /* Can't do this if region may have changed. */
13221 if ((!NILP (Vtransient_mark_mode)
13222 && !NILP (current_buffer->mark_active))
8f897821
GM
13223 || !NILP (w->region_showing)
13224 || !NILP (Vshow_trailing_whitespace))
5f5c8ee5 13225 return 0;
a2889657 13226
5f5c8ee5 13227 /* If top-line visibility has changed, give up. */
045dee35
GM
13228 if (WINDOW_WANTS_HEADER_LINE_P (w)
13229 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
5f5c8ee5
GM
13230 return 0;
13231
13232 /* Give up if old or new display is scrolled vertically. We could
13233 make this function handle this, but right now it doesn't. */
13234 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
546f98b8 13235 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
5f5c8ee5
GM
13236 return 0;
13237
13238 /* The variable new_start now holds the new window start. The old
13239 start `start' can be determined from the current matrix. */
13240 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13241 start = start_row->start.pos;
13242 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
a2889657 13243
5f5c8ee5
GM
13244 /* Clear the desired matrix for the display below. */
13245 clear_glyph_matrix (w->desired_matrix);
2311178e 13246
5f5c8ee5
GM
13247 if (CHARPOS (new_start) <= CHARPOS (start))
13248 {
13249 int first_row_y;
2311178e 13250
607ec83c
GM
13251 /* Don't use this method if the display starts with an ellipsis
13252 displayed for invisible text. It's not easy to handle that case
13253 below, and it's certainly not worth the effort since this is
13254 not a frequent case. */
13255 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13256 return 0;
13257
5f5c8ee5 13258 IF_DEBUG (debug_method_add (w, "twu1"));
2311178e 13259
5f5c8ee5
GM
13260 /* Display up to a row that can be reused. The variable
13261 last_text_row is set to the last row displayed that displays
b48f74cb
GM
13262 text. Note that it.vpos == 0 if or if not there is a
13263 header-line; it's not the same as the MATRIX_ROW_VPOS! */
5f5c8ee5
GM
13264 start_display (&it, w, new_start);
13265 first_row_y = it.current_y;
13266 w->cursor.vpos = -1;
13267 last_text_row = last_reused_text_row = NULL;
2311178e 13268
5f5c8ee5 13269 while (it.current_y < it.last_visible_y
5f5c8ee5 13270 && !fonts_changed_p)
3df5f8aa
RS
13271 {
13272 /* If we have reached into the characters in the START row,
13273 that means the line boundaries have changed. So we
13274 can't start copying with the row START. Maybe it will
13275 work to start copying with the following row. */
13276 while (IT_CHARPOS (it) > CHARPOS (start))
13277 {
13278 /* Advance to the next row as the "start". */
13279 start_row++;
13280 start = start_row->start.pos;
13281 /* If there are no more rows to try, or just one, give up. */
13282 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
546f98b8 13283 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
3df5f8aa
RS
13284 || CHARPOS (start) == ZV)
13285 {
13286 clear_glyph_matrix (w->desired_matrix);
13287 return 0;
13288 }
13289
13290 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13291 }
13292 /* If we have reached alignment,
13293 we can copy the rest of the rows. */
13294 if (IT_CHARPOS (it) == CHARPOS (start))
13295 break;
13296
13297 if (display_line (&it))
13298 last_text_row = it.glyph_row - 1;
13299 }
5f5c8ee5
GM
13300
13301 /* A value of current_y < last_visible_y means that we stopped
13302 at the previous window start, which in turn means that we
13303 have at least one reusable row. */
13304 if (it.current_y < it.last_visible_y)
a2889657 13305 {
b48f74cb 13306 /* IT.vpos always starts from 0; it counts text lines. */
3df5f8aa 13307 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
2311178e 13308
5f5c8ee5
GM
13309 /* Find PT if not already found in the lines displayed. */
13310 if (w->cursor.vpos < 0)
a2889657 13311 {
3df5f8aa 13312 int dy = it.current_y - start_row->y;
2311178e 13313
5f5c8ee5 13314 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
0df56f4d
GM
13315 row = row_containing_pos (w, PT, row, NULL, dy);
13316 if (row)
13317 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13318 dy, nrows_scrolled);
13319 else
5f5c8ee5
GM
13320 {
13321 clear_glyph_matrix (w->desired_matrix);
13322 return 0;
13323 }
a2889657 13324 }
5f5c8ee5
GM
13325
13326 /* Scroll the display. Do it before the current matrix is
13327 changed. The problem here is that update has not yet
13328 run, i.e. part of the current matrix is not up to date.
13329 scroll_run_hook will clear the cursor, and use the
13330 current matrix to get the height of the row the cursor is
13331 in. */
3df5f8aa 13332 run.current_y = start_row->y;
5f5c8ee5
GM
13333 run.desired_y = it.current_y;
13334 run.height = it.last_visible_y - it.current_y;
b48f74cb
GM
13335
13336 if (run.height > 0 && run.current_y != run.desired_y)
a2889657 13337 {
5f5c8ee5
GM
13338 update_begin (f);
13339 rif->update_window_begin_hook (w);
fa3c6b4d 13340 rif->clear_window_mouse_face (w);
5f5c8ee5 13341 rif->scroll_run_hook (w, &run);
64d1e7d3 13342 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5 13343 update_end (f);
a2889657 13344 }
5f5c8ee5
GM
13345
13346 /* Shift current matrix down by nrows_scrolled lines. */
13347 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13348 rotate_matrix (w->current_matrix,
13349 start_vpos,
13350 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13351 nrows_scrolled);
2311178e 13352
9c8b8382 13353 /* Disable lines that must be updated. */
5f5c8ee5 13354 for (i = 0; i < it.vpos; ++i)
b48f74cb 13355 (start_row + i)->enabled_p = 0;
9c8b8382 13356
5f5c8ee5 13357 /* Re-compute Y positions. */
da8b7f4f 13358 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
5f5c8ee5 13359 max_y = it.last_visible_y;
b48f74cb
GM
13360 for (row = start_row + nrows_scrolled;
13361 row < bottom_row;
13362 ++row)
d2f84654 13363 {
5f5c8ee5 13364 row->y = it.current_y;
75c5350a 13365 row->visible_height = row->height;
5f5c8ee5
GM
13366
13367 if (row->y < min_y)
75c5350a
GM
13368 row->visible_height -= min_y - row->y;
13369 if (row->y + row->height > max_y)
13370 row->visible_height -= row->y + row->height - max_y;
88e6b646 13371 row->redraw_fringe_bitmaps_p = 1;
2311178e 13372
5f5c8ee5 13373 it.current_y += row->height;
5f5c8ee5
GM
13374
13375 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13376 last_reused_text_row = row;
13377 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13378 break;
d2f84654 13379 }
2311178e 13380
9c8b8382
GM
13381 /* Disable lines in the current matrix which are now
13382 below the window. */
bafb434c 13383 for (++row; row < bottom_row; ++row)
49c56036 13384 row->enabled_p = row->mode_line_p = 0;
a2889657 13385 }
5f5c8ee5
GM
13386
13387 /* Update window_end_pos etc.; last_reused_text_row is the last
13388 reused row from the current matrix containing text, if any.
13389 The value of last_text_row is the last displayed line
13390 containing text. */
13391 if (last_reused_text_row)
a2889657 13392 {
5f5c8ee5
GM
13393 w->window_end_bytepos
13394 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
ac90c44f
GM
13395 w->window_end_pos
13396 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13397 w->window_end_vpos
13398 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13399 w->current_matrix));
a2889657 13400 }
5f5c8ee5
GM
13401 else if (last_text_row)
13402 {
13403 w->window_end_bytepos
13404 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
13405 w->window_end_pos
13406 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13407 w->window_end_vpos
13408 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
5f5c8ee5
GM
13409 }
13410 else
13411 {
13412 /* This window must be completely empty. */
86bf3faa
RS
13413 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13414 w->window_end_pos = make_number (Z - ZV);
13415 w->window_end_vpos = make_number (0);
5f5c8ee5
GM
13416 }
13417 w->window_end_valid = Qnil;
a2889657 13418
5f5c8ee5
GM
13419 /* Update hint: don't try scrolling again in update_window. */
13420 w->desired_matrix->no_scrolling_p = 1;
2311178e 13421
5f5c8ee5
GM
13422#if GLYPH_DEBUG
13423 debug_method_add (w, "try_window_reusing_current_matrix 1");
13424#endif
13425 return 1;
a2889657 13426 }
5f5c8ee5
GM
13427 else if (CHARPOS (new_start) > CHARPOS (start))
13428 {
13429 struct glyph_row *pt_row, *row;
13430 struct glyph_row *first_reusable_row;
13431 struct glyph_row *first_row_to_display;
13432 int dy;
13433 int yb = window_text_bottom_y (w);
13434
5f5c8ee5
GM
13435 /* Find the row starting at new_start, if there is one. Don't
13436 reuse a partially visible line at the end. */
b48f74cb 13437 first_reusable_row = start_row;
5f5c8ee5
GM
13438 while (first_reusable_row->enabled_p
13439 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13440 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13441 < CHARPOS (new_start)))
13442 ++first_reusable_row;
13443
13444 /* Give up if there is no row to reuse. */
13445 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
28514cd9
GM
13446 || !first_reusable_row->enabled_p
13447 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13448 != CHARPOS (new_start)))
5f5c8ee5
GM
13449 return 0;
13450
5f5c8ee5
GM
13451 /* We can reuse fully visible rows beginning with
13452 first_reusable_row to the end of the window. Set
13453 first_row_to_display to the first row that cannot be reused.
13454 Set pt_row to the row containing point, if there is any. */
5f5c8ee5 13455 pt_row = NULL;
ac90c44f
GM
13456 for (first_row_to_display = first_reusable_row;
13457 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13458 ++first_row_to_display)
5f5c8ee5 13459 {
4bde0ebb
GM
13460 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13461 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
5f5c8ee5 13462 pt_row = first_row_to_display;
5f5c8ee5 13463 }
a2889657 13464
5f5c8ee5
GM
13465 /* Start displaying at the start of first_row_to_display. */
13466 xassert (first_row_to_display->y < yb);
13467 init_to_row_start (&it, w, first_row_to_display);
607ec83c 13468
b48f74cb
GM
13469 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13470 - start_vpos);
5f5c8ee5
GM
13471 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13472 - nrows_scrolled);
b48f74cb 13473 it.current_y = (first_row_to_display->y - first_reusable_row->y
da8b7f4f 13474 + WINDOW_HEADER_LINE_HEIGHT (w));
5f5c8ee5
GM
13475
13476 /* Display lines beginning with first_row_to_display in the
13477 desired matrix. Set last_text_row to the last row displayed
13478 that displays text. */
13479 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13480 if (pt_row == NULL)
13481 w->cursor.vpos = -1;
13482 last_text_row = NULL;
13483 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13484 if (display_line (&it))
13485 last_text_row = it.glyph_row - 1;
13486
13487 /* Give up If point isn't in a row displayed or reused. */
13488 if (w->cursor.vpos < 0)
13489 {
13490 clear_glyph_matrix (w->desired_matrix);
13491 return 0;
13492 }
12adba34 13493
5f5c8ee5
GM
13494 /* If point is in a reused row, adjust y and vpos of the cursor
13495 position. */
13496 if (pt_row)
13497 {
38793d6a
KS
13498 w->cursor.vpos -= nrows_scrolled;
13499 w->cursor.y -= first_reusable_row->y - start_row->y;
a2889657
JB
13500 }
13501
5f5c8ee5
GM
13502 /* Scroll the display. */
13503 run.current_y = first_reusable_row->y;
da8b7f4f 13504 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
5f5c8ee5 13505 run.height = it.last_visible_y - run.current_y;
4da61803 13506 dy = run.current_y - run.desired_y;
2311178e 13507
5f5c8ee5
GM
13508 if (run.height)
13509 {
5f5c8ee5
GM
13510 update_begin (f);
13511 rif->update_window_begin_hook (w);
fa3c6b4d 13512 rif->clear_window_mouse_face (w);
5f5c8ee5 13513 rif->scroll_run_hook (w, &run);
64d1e7d3 13514 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5
GM
13515 update_end (f);
13516 }
a2889657 13517
5f5c8ee5
GM
13518 /* Adjust Y positions of reused rows. */
13519 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
da8b7f4f 13520 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
5f5c8ee5 13521 max_y = it.last_visible_y;
b48f74cb 13522 for (row = first_reusable_row; row < first_row_to_display; ++row)
5f5c8ee5
GM
13523 {
13524 row->y -= dy;
75c5350a 13525 row->visible_height = row->height;
5f5c8ee5 13526 if (row->y < min_y)
75c5350a
GM
13527 row->visible_height -= min_y - row->y;
13528 if (row->y + row->height > max_y)
13529 row->visible_height -= row->y + row->height - max_y;
88e6b646 13530 row->redraw_fringe_bitmaps_p = 1;
5f5c8ee5 13531 }
a2889657 13532
5f5c8ee5
GM
13533 /* Scroll the current matrix. */
13534 xassert (nrows_scrolled > 0);
13535 rotate_matrix (w->current_matrix,
13536 start_vpos,
13537 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13538 -nrows_scrolled);
13539
ac90c44f
GM
13540 /* Disable rows not reused. */
13541 for (row -= nrows_scrolled; row < bottom_row; ++row)
13542 row->enabled_p = 0;
13543
38793d6a
KS
13544 /* Point may have moved to a different line, so we cannot assume that
13545 the previous cursor position is valid; locate the correct row. */
13546 if (pt_row)
13547 {
13548 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13549 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
13550 row++)
13551 {
13552 w->cursor.vpos++;
13553 w->cursor.y = row->y;
13554 }
13555 if (row < bottom_row)
13556 {
13557 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
13558 while (glyph->charpos < PT)
13559 {
13560 w->cursor.hpos++;
13561 w->cursor.x += glyph->pixel_width;
13562 glyph++;
13563 }
13564 }
13565 }
13566
5f5c8ee5
GM
13567 /* Adjust window end. A null value of last_text_row means that
13568 the window end is in reused rows which in turn means that
13569 only its vpos can have changed. */
13570 if (last_text_row)
13571 {
13572 w->window_end_bytepos
13573 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
13574 w->window_end_pos
13575 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13576 w->window_end_vpos
13577 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
5f5c8ee5
GM
13578 }
13579 else
a2889657 13580 {
ac90c44f
GM
13581 w->window_end_vpos
13582 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
a2889657 13583 }
2311178e 13584
5f5c8ee5
GM
13585 w->window_end_valid = Qnil;
13586 w->desired_matrix->no_scrolling_p = 1;
13587
13588#if GLYPH_DEBUG
13589 debug_method_add (w, "try_window_reusing_current_matrix 2");
13590#endif
13591 return 1;
a2889657 13592 }
2311178e 13593
5f5c8ee5
GM
13594 return 0;
13595}
a2889657 13596
a2889657 13597
5f5c8ee5
GM
13598\f
13599/************************************************************************
13600 Window redisplay reusing current matrix when buffer has changed
13601 ************************************************************************/
13602
1ec185cb
GM
13603static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
13604static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
5f5c8ee5
GM
13605 int *, int *));
13606static struct glyph_row *
13607find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
13608 struct glyph_row *));
13609
13610
13611/* Return the last row in MATRIX displaying text. If row START is
13612 non-null, start searching with that row. IT gives the dimensions
13613 of the display. Value is null if matrix is empty; otherwise it is
13614 a pointer to the row found. */
13615
13616static struct glyph_row *
13617find_last_row_displaying_text (matrix, it, start)
13618 struct glyph_matrix *matrix;
13619 struct it *it;
13620 struct glyph_row *start;
13621{
13622 struct glyph_row *row, *row_found;
13623
13624 /* Set row_found to the last row in IT->w's current matrix
13625 displaying text. The loop looks funny but think of partially
13626 visible lines. */
13627 row_found = NULL;
13628 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
13629 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13630 {
13631 xassert (row->enabled_p);
13632 row_found = row;
13633 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
13634 break;
13635 ++row;
a2889657 13636 }
2311178e 13637
5f5c8ee5
GM
13638 return row_found;
13639}
13640
a2889657 13641
5f5c8ee5 13642/* Return the last row in the current matrix of W that is not affected
d43fbe9d
GM
13643 by changes at the start of current_buffer that occurred since W's
13644 current matrix was built. Value is null if no such row exists.
a2889657 13645
d43fbe9d
GM
13646 BEG_UNCHANGED us the number of characters unchanged at the start of
13647 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
13648 first changed character in current_buffer. Characters at positions <
13649 BEG + BEG_UNCHANGED are at the same buffer positions as they were
13650 when the current matrix was built. */
5f5c8ee5
GM
13651
13652static struct glyph_row *
1ec185cb 13653find_last_unchanged_at_beg_row (w)
5f5c8ee5
GM
13654 struct window *w;
13655{
9142dd5b 13656 int first_changed_pos = BEG + BEG_UNCHANGED;
5f5c8ee5
GM
13657 struct glyph_row *row;
13658 struct glyph_row *row_found = NULL;
13659 int yb = window_text_bottom_y (w);
13660
13661 /* Find the last row displaying unchanged text. */
13662 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13663 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13664 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
a2889657 13665 {
5f5c8ee5
GM
13666 if (/* If row ends before first_changed_pos, it is unchanged,
13667 except in some case. */
13668 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
13669 /* When row ends in ZV and we write at ZV it is not
13670 unchanged. */
13671 && !row->ends_at_zv_p
13672 /* When first_changed_pos is the end of a continued line,
13673 row is not unchanged because it may be no longer
13674 continued. */
13675 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
88e6b646
KS
13676 && (row->continued_p
13677 || row->exact_window_width_line_p)))
5f5c8ee5
GM
13678 row_found = row;
13679
13680 /* Stop if last visible row. */
13681 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
13682 break;
2311178e 13683
5f5c8ee5 13684 ++row;
a2889657
JB
13685 }
13686
5f5c8ee5 13687 return row_found;
a2889657 13688}
5f5c8ee5
GM
13689
13690
13691/* Find the first glyph row in the current matrix of W that is not
2311178e 13692 affected by changes at the end of current_buffer since the
d43fbe9d
GM
13693 time W's current matrix was built.
13694
13695 Return in *DELTA the number of chars by which buffer positions in
13696 unchanged text at the end of current_buffer must be adjusted.
2311178e 13697
d43fbe9d
GM
13698 Return in *DELTA_BYTES the corresponding number of bytes.
13699
13700 Value is null if no such row exists, i.e. all rows are affected by
13701 changes. */
2311178e 13702
5f5c8ee5 13703static struct glyph_row *
1ec185cb 13704find_first_unchanged_at_end_row (w, delta, delta_bytes)
5f5c8ee5
GM
13705 struct window *w;
13706 int *delta, *delta_bytes;
a2889657 13707{
5f5c8ee5
GM
13708 struct glyph_row *row;
13709 struct glyph_row *row_found = NULL;
c581d710 13710
5f5c8ee5 13711 *delta = *delta_bytes = 0;
b2a76982 13712
1ec185cb
GM
13713 /* Display must not have been paused, otherwise the current matrix
13714 is not up to date. */
13715 if (NILP (w->window_end_valid))
13716 abort ();
2311178e 13717
1ec185cb 13718 /* A value of window_end_pos >= END_UNCHANGED means that the window
5f5c8ee5
GM
13719 end is in the range of changed text. If so, there is no
13720 unchanged row at the end of W's current matrix. */
9142dd5b 13721 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
5f5c8ee5
GM
13722 return NULL;
13723
13724 /* Set row to the last row in W's current matrix displaying text. */
13725 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
2311178e
TTN
13726
13727 /* If matrix is entirely empty, no unchanged row exists. */
5f5c8ee5
GM
13728 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13729 {
13730 /* The value of row is the last glyph row in the matrix having a
13731 meaningful buffer position in it. The end position of row
13732 corresponds to window_end_pos. This allows us to translate
13733 buffer positions in the current matrix to current buffer
13734 positions for characters not in changed text. */
13735 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13736 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13737 int last_unchanged_pos, last_unchanged_pos_old;
13738 struct glyph_row *first_text_row
13739 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13740
13741 *delta = Z - Z_old;
13742 *delta_bytes = Z_BYTE - Z_BYTE_old;
13743
13744 /* Set last_unchanged_pos to the buffer position of the last
13745 character in the buffer that has not been changed. Z is the
d43fbe9d
GM
13746 index + 1 of the last character in current_buffer, i.e. by
13747 subtracting END_UNCHANGED we get the index of the last
5f5c8ee5
GM
13748 unchanged character, and we have to add BEG to get its buffer
13749 position. */
9142dd5b 13750 last_unchanged_pos = Z - END_UNCHANGED + BEG;
5f5c8ee5 13751 last_unchanged_pos_old = last_unchanged_pos - *delta;
2311178e 13752
5f5c8ee5
GM
13753 /* Search backward from ROW for a row displaying a line that
13754 starts at a minimum position >= last_unchanged_pos_old. */
1ec185cb 13755 for (; row > first_text_row; --row)
5f5c8ee5 13756 {
e10ee30c
KS
13757 /* This used to abort, but it can happen.
13758 It is ok to just stop the search instead here. KFS. */
1ec185cb 13759 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
e10ee30c 13760 break;
2311178e 13761
5f5c8ee5
GM
13762 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
13763 row_found = row;
5f5c8ee5
GM
13764 }
13765 }
13766
1ec185cb
GM
13767 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
13768 abort ();
2311178e 13769
5f5c8ee5 13770 return row_found;
c581d710
RS
13771}
13772
c581d710 13773
5f5c8ee5
GM
13774/* Make sure that glyph rows in the current matrix of window W
13775 reference the same glyph memory as corresponding rows in the
13776 frame's frame matrix. This function is called after scrolling W's
13777 current matrix on a terminal frame in try_window_id and
13778 try_window_reusing_current_matrix. */
13779
13780static void
13781sync_frame_with_window_matrix_rows (w)
13782 struct window *w;
c581d710 13783{
5f5c8ee5
GM
13784 struct frame *f = XFRAME (w->frame);
13785 struct glyph_row *window_row, *window_row_end, *frame_row;
13786
13787 /* Preconditions: W must be a leaf window and full-width. Its frame
13788 must have a frame matrix. */
13789 xassert (NILP (w->hchild) && NILP (w->vchild));
13790 xassert (WINDOW_FULL_WIDTH_P (w));
13791 xassert (!FRAME_WINDOW_P (f));
13792
13793 /* If W is a full-width window, glyph pointers in W's current matrix
13794 have, by definition, to be the same as glyph pointers in the
7d4cc828
GM
13795 corresponding frame matrix. Note that frame matrices have no
13796 marginal areas (see build_frame_matrix). */
5f5c8ee5
GM
13797 window_row = w->current_matrix->rows;
13798 window_row_end = window_row + w->current_matrix->nrows;
da8b7f4f 13799 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
5f5c8ee5 13800 while (window_row < window_row_end)
659a218f 13801 {
7d4cc828
GM
13802 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
13803 struct glyph *end = window_row->glyphs[LAST_AREA];
13804
13805 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
13806 frame_row->glyphs[TEXT_AREA] = start;
13807 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
13808 frame_row->glyphs[LAST_AREA] = end;
f002db93
GM
13809
13810 /* Disable frame rows whose corresponding window rows have
13811 been disabled in try_window_id. */
13812 if (!window_row->enabled_p)
13813 frame_row->enabled_p = 0;
2311178e 13814
5f5c8ee5 13815 ++window_row, ++frame_row;
659a218f 13816 }
a2889657 13817}
5f5c8ee5
GM
13818
13819
e037b9ec
GM
13820/* Find the glyph row in window W containing CHARPOS. Consider all
13821 rows between START and END (not inclusive). END null means search
13822 all rows to the end of the display area of W. Value is the row
13823 containing CHARPOS or null. */
13824
0bef35bc 13825struct glyph_row *
0df56f4d 13826row_containing_pos (w, charpos, start, end, dy)
e037b9ec
GM
13827 struct window *w;
13828 int charpos;
13829 struct glyph_row *start, *end;
0df56f4d 13830 int dy;
e037b9ec
GM
13831{
13832 struct glyph_row *row = start;
13833 int last_y;
13834
13835 /* If we happen to start on a header-line, skip that. */
13836 if (row->mode_line_p)
13837 ++row;
2311178e 13838
e037b9ec
GM
13839 if ((end && row >= end) || !row->enabled_p)
13840 return NULL;
2311178e 13841
0df56f4d 13842 last_y = window_text_bottom_y (w) - dy;
2311178e 13843
5cce13dd
RS
13844 while (1)
13845 {
13846 /* Give up if we have gone too far. */
13847 if (end && row >= end)
13848 return NULL;
40a301b3
RS
13849 /* This formerly returned if they were equal.
13850 I think that both quantities are of a "last plus one" type;
13851 if so, when they are equal, the row is within the screen. -- rms. */
13852 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
5cce13dd
RS
13853 return NULL;
13854
13855 /* If it is in this row, return this row. */
13856 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
e037b9ec 13857 || (MATRIX_ROW_END_CHARPOS (row) == charpos
0df56f4d
GM
13858 /* The end position of a row equals the start
13859 position of the next row. If CHARPOS is there, we
13860 would rather display it in the next line, except
13861 when this line ends in ZV. */
13862 && !row->ends_at_zv_p
5cce13dd
RS
13863 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13864 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13865 return row;
13866 ++row;
13867 }
e037b9ec
GM
13868}
13869
13870
5f5c8ee5
GM
13871/* Try to redisplay window W by reusing its existing display. W's
13872 current matrix must be up to date when this function is called,
13873 i.e. window_end_valid must not be nil.
13874
13875 Value is
13876
13877 1 if display has been updated
13878 0 if otherwise unsuccessful
13879 -1 if redisplay with same window start is known not to succeed
13880
13881 The following steps are performed:
13882
13883 1. Find the last row in the current matrix of W that is not
13884 affected by changes at the start of current_buffer. If no such row
13885 is found, give up.
13886
13887 2. Find the first row in W's current matrix that is not affected by
13888 changes at the end of current_buffer. Maybe there is no such row.
13889
13890 3. Display lines beginning with the row + 1 found in step 1 to the
13891 row found in step 2 or, if step 2 didn't find a row, to the end of
13892 the window.
13893
13894 4. If cursor is not known to appear on the window, give up.
13895
13896 5. If display stopped at the row found in step 2, scroll the
13897 display and current matrix as needed.
13898
13899 6. Maybe display some lines at the end of W, if we must. This can
13900 happen under various circumstances, like a partially visible line
13901 becoming fully visible, or because newly displayed lines are displayed
13902 in smaller font sizes.
13903
13904 7. Update W's window end information. */
13905
12adba34 13906static int
5f5c8ee5 13907try_window_id (w)
12adba34 13908 struct window *w;
12adba34 13909{
5f5c8ee5
GM
13910 struct frame *f = XFRAME (w->frame);
13911 struct glyph_matrix *current_matrix = w->current_matrix;
13912 struct glyph_matrix *desired_matrix = w->desired_matrix;
13913 struct glyph_row *last_unchanged_at_beg_row;
13914 struct glyph_row *first_unchanged_at_end_row;
13915 struct glyph_row *row;
13916 struct glyph_row *bottom_row;
13917 int bottom_vpos;
13918 struct it it;
13919 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13920 struct text_pos start_pos;
13921 struct run run;
13922 int first_unchanged_at_end_vpos = 0;
13923 struct glyph_row *last_text_row, *last_text_row_at_end;
13924 struct text_pos start;
27d16f05 13925 int first_changed_charpos, last_changed_charpos;
5f5c8ee5 13926
69d1f7c9 13927#if GLYPH_DEBUG
76cb5e06
GM
13928 if (inhibit_try_window_id)
13929 return 0;
13930#endif
13931
27d16f05
GM
13932 /* This is handy for debugging. */
13933#if 0
13934#define GIVE_UP(X) \
13935 do { \
13936 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13937 return 0; \
13938 } while (0)
13939#else
62397849 13940#define GIVE_UP(X) return 0
27d16f05 13941#endif
2311178e 13942
5f5c8ee5
GM
13943 SET_TEXT_POS_FROM_MARKER (start, w->start);
13944
27d16f05
GM
13945 /* Don't use this for mini-windows because these can show
13946 messages and mini-buffers, and we don't handle that here. */
13947 if (MINI_WINDOW_P (w))
13948 GIVE_UP (1);
2311178e 13949
27d16f05 13950 /* This flag is used to prevent redisplay optimizations. */
5fb96e96 13951 if (windows_or_buffers_changed || cursor_type_changed)
27d16f05 13952 GIVE_UP (2);
2311178e 13953
c62c1bb5
RS
13954 /* Verify that narrowing has not changed.
13955 Also verify that we were not told to prevent redisplay optimizations.
13956 It would be nice to further
27d16f05 13957 reduce the number of cases where this prevents try_window_id. */
c62c1bb5
RS
13958 if (current_buffer->clip_changed
13959 || current_buffer->prevent_redisplay_optimizations_p)
27d16f05
GM
13960 GIVE_UP (3);
13961
13962 /* Window must either use window-based redisplay or be full width. */
13963 if (!FRAME_WINDOW_P (f)
13964 && (!line_ins_del_ok
13965 || !WINDOW_FULL_WIDTH_P (w)))
13966 GIVE_UP (4);
5f5c8ee5 13967
f5376658 13968 /* Give up if point is not known NOT to appear in W. */
27d16f05
GM
13969 if (PT < CHARPOS (start))
13970 GIVE_UP (5);
13971
13972 /* Another way to prevent redisplay optimizations. */
13973 if (XFASTINT (w->last_modified) == 0)
13974 GIVE_UP (6);
2311178e 13975
f5376658 13976 /* Verify that window is not hscrolled. */
27d16f05
GM
13977 if (XFASTINT (w->hscroll) != 0)
13978 GIVE_UP (7);
2311178e 13979
f5376658 13980 /* Verify that display wasn't paused. */
27d16f05
GM
13981 if (NILP (w->window_end_valid))
13982 GIVE_UP (8);
2311178e 13983
27d16f05
GM
13984 /* Can't use this if highlighting a region because a cursor movement
13985 will do more than just set the cursor. */
13986 if (!NILP (Vtransient_mark_mode)
13987 && !NILP (current_buffer->mark_active))
13988 GIVE_UP (9);
13989
13990 /* Likewise if highlighting trailing whitespace. */
13991 if (!NILP (Vshow_trailing_whitespace))
13992 GIVE_UP (11);
2311178e 13993
27d16f05
GM
13994 /* Likewise if showing a region. */
13995 if (!NILP (w->region_showing))
13996 GIVE_UP (10);
2311178e 13997
27d16f05 13998 /* Can use this if overlay arrow position and or string have changed. */
351b5434 13999 if (overlay_arrows_changed_p ())
27d16f05
GM
14000 GIVE_UP (12);
14001
2311178e 14002
5f5c8ee5
GM
14003 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14004 only if buffer has really changed. The reason is that the gap is
14005 initially at Z for freshly visited files. The code below would
14006 set end_unchanged to 0 in that case. */
28ee91c0
GM
14007 if (MODIFF > SAVE_MODIFF
14008 /* This seems to happen sometimes after saving a buffer. */
14009 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
5f5c8ee5 14010 {
9142dd5b
GM
14011 if (GPT - BEG < BEG_UNCHANGED)
14012 BEG_UNCHANGED = GPT - BEG;
14013 if (Z - GPT < END_UNCHANGED)
14014 END_UNCHANGED = Z - GPT;
5f5c8ee5 14015 }
bf9249e3 14016
27d16f05
GM
14017 /* The position of the first and last character that has been changed. */
14018 first_changed_charpos = BEG + BEG_UNCHANGED;
14019 last_changed_charpos = Z - END_UNCHANGED;
14020
5f5c8ee5
GM
14021 /* If window starts after a line end, and the last change is in
14022 front of that newline, then changes don't affect the display.
f2d86d7a
GM
14023 This case happens with stealth-fontification. Note that although
14024 the display is unchanged, glyph positions in the matrix have to
14025 be adjusted, of course. */
5f5c8ee5 14026 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
27d16f05 14027 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
e60f4527 14028 && ((last_changed_charpos < CHARPOS (start)
27d16f05 14029 && CHARPOS (start) == BEGV)
e60f4527 14030 || (last_changed_charpos < CHARPOS (start) - 1
27d16f05
GM
14031 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14032 {
14033 int Z_old, delta, Z_BYTE_old, delta_bytes;
14034 struct glyph_row *r0;
14035
14036 /* Compute how many chars/bytes have been added to or removed
14037 from the buffer. */
14038 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14039 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14040 delta = Z - Z_old;
14041 delta_bytes = Z_BYTE - Z_BYTE_old;
2311178e 14042
27d16f05
GM
14043 /* Give up if PT is not in the window. Note that it already has
14044 been checked at the start of try_window_id that PT is not in
14045 front of the window start. */
14046 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14047 GIVE_UP (13);
14048
14049 /* If window start is unchanged, we can reuse the whole matrix
14050 as is, after adjusting glyph positions. No need to compute
14051 the window end again, since its offset from Z hasn't changed. */
14052 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14053 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
2d031b89
AS
14054 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14055 /* PT must not be in a partially visible line. */
14056 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14057 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
27d16f05
GM
14058 {
14059 /* Adjust positions in the glyph matrix. */
14060 if (delta || delta_bytes)
14061 {
14062 struct glyph_row *r1
14063 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14064 increment_matrix_positions (w->current_matrix,
14065 MATRIX_ROW_VPOS (r0, current_matrix),
14066 MATRIX_ROW_VPOS (r1, current_matrix),
14067 delta, delta_bytes);
14068 }
2311178e 14069
27d16f05 14070 /* Set the cursor. */
0df56f4d 14071 row = row_containing_pos (w, PT, r0, NULL, 0);
59adf8e8
KS
14072 if (row)
14073 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
40a301b3
RS
14074 else
14075 abort ();
27d16f05
GM
14076 return 1;
14077 }
5f5c8ee5
GM
14078 }
14079
27d16f05 14080 /* Handle the case that changes are all below what is displayed in
33ea6c4d 14081 the window, and that PT is in the window. This shortcut cannot
2b9c25e0
GM
14082 be taken if ZV is visible in the window, and text has been added
14083 there that is visible in the window. */
14084 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
33ea6c4d
GM
14085 /* ZV is not visible in the window, or there are no
14086 changes at ZV, actually. */
14087 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14088 || first_changed_charpos == last_changed_charpos))
5f5c8ee5 14089 {
27d16f05 14090 struct glyph_row *r0;
ef121659 14091
27d16f05
GM
14092 /* Give up if PT is not in the window. Note that it already has
14093 been checked at the start of try_window_id that PT is not in
14094 front of the window start. */
14095 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14096 GIVE_UP (14);
14097
14098 /* If window start is unchanged, we can reuse the whole matrix
14099 as is, without changing glyph positions since no text has
14100 been added/removed in front of the window end. */
14101 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
2d031b89
AS
14102 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14103 /* PT must not be in a partially visible line. */
14104 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14105 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
27d16f05
GM
14106 {
14107 /* We have to compute the window end anew since text
14108 can have been added/removed after it. */
14109 w->window_end_pos
14110 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14111 w->window_end_bytepos
14112 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14113
14114 /* Set the cursor. */
0df56f4d 14115 row = row_containing_pos (w, PT, r0, NULL, 0);
59adf8e8
KS
14116 if (row)
14117 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
40a301b3
RS
14118 else
14119 abort ();
27d16f05
GM
14120 return 2;
14121 }
5f5c8ee5
GM
14122 }
14123
2b9c25e0 14124 /* Give up if window start is in the changed area.
2311178e 14125
2b9c25e0
GM
14126 The condition used to read
14127
14128 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14129
14130 but why that was tested escapes me at the moment. */
14131 if (CHARPOS (start) >= first_changed_charpos
27d16f05
GM
14132 && CHARPOS (start) <= last_changed_charpos)
14133 GIVE_UP (15);
2311178e 14134
f5376658
RS
14135 /* Check that window start agrees with the start of the first glyph
14136 row in its current matrix. Check this after we know the window
14137 start is not in changed text, otherwise positions would not be
14138 comparable. */
27d16f05 14139 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
5f5c8ee5 14140 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
27d16f05 14141 GIVE_UP (16);
5f5c8ee5 14142
23e8bd86
GM
14143 /* Give up if the window ends in strings. Overlay strings
14144 at the end are difficult to handle, so don't try. */
14145 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14146 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14147 GIVE_UP (20);
14148
5f5c8ee5
GM
14149 /* Compute the position at which we have to start displaying new
14150 lines. Some of the lines at the top of the window might be
14151 reusable because they are not displaying changed text. Find the
14152 last row in W's current matrix not affected by changes at the
14153 start of current_buffer. Value is null if changes start in the
14154 first line of window. */
1ec185cb 14155 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
5f5c8ee5
GM
14156 if (last_unchanged_at_beg_row)
14157 {
67f1cf4c
GM
14158 /* Avoid starting to display in the moddle of a character, a TAB
14159 for instance. This is easier than to set up the iterator
14160 exactly, and it's not a frequent case, so the additional
14161 effort wouldn't really pay off. */
e74fb0f7
GM
14162 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14163 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
67f1cf4c
GM
14164 && last_unchanged_at_beg_row > w->current_matrix->rows)
14165 --last_unchanged_at_beg_row;
14166
14167 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
27d16f05 14168 GIVE_UP (17);
47d57b22
GM
14169
14170 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14171 GIVE_UP (18);
5f5c8ee5 14172 start_pos = it.current.pos;
2311178e 14173
5f5c8ee5
GM
14174 /* Start displaying new lines in the desired matrix at the same
14175 vpos we would use in the current matrix, i.e. below
14176 last_unchanged_at_beg_row. */
14177 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14178 current_matrix);
14179 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14180 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14181
14182 xassert (it.hpos == 0 && it.current_x == 0);
14183 }
14184 else
14185 {
14186 /* There are no reusable lines at the start of the window.
3ccb8bcf 14187 Start displaying in the first text line. */
5f5c8ee5 14188 start_display (&it, w, start);
3ccb8bcf 14189 it.vpos = it.first_vpos;
5f5c8ee5
GM
14190 start_pos = it.current.pos;
14191 }
14192
5f5c8ee5
GM
14193 /* Find the first row that is not affected by changes at the end of
14194 the buffer. Value will be null if there is no unchanged row, in
14195 which case we must redisplay to the end of the window. delta
14196 will be set to the value by which buffer positions beginning with
14197 first_unchanged_at_end_row have to be adjusted due to text
14198 changes. */
14199 first_unchanged_at_end_row
1ec185cb 14200 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
5f5c8ee5
GM
14201 IF_DEBUG (debug_delta = delta);
14202 IF_DEBUG (debug_delta_bytes = delta_bytes);
2311178e 14203
5f5c8ee5
GM
14204 /* Set stop_pos to the buffer position up to which we will have to
14205 display new lines. If first_unchanged_at_end_row != NULL, this
14206 is the buffer position of the start of the line displayed in that
14207 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14208 that we don't stop at a buffer position. */
14209 stop_pos = 0;
14210 if (first_unchanged_at_end_row)
14211 {
14212 xassert (last_unchanged_at_beg_row == NULL
14213 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
2311178e 14214
5f5c8ee5
GM
14215 /* If this is a continuation line, move forward to the next one
14216 that isn't. Changes in lines above affect this line.
14217 Caution: this may move first_unchanged_at_end_row to a row
14218 not displaying text. */
14219 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14220 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14221 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14222 < it.last_visible_y))
14223 ++first_unchanged_at_end_row;
14224
14225 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14226 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14227 >= it.last_visible_y))
14228 first_unchanged_at_end_row = NULL;
14229 else
14230 {
14231 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14232 + delta);
14233 first_unchanged_at_end_vpos
14234 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
9142dd5b 14235 xassert (stop_pos >= Z - END_UNCHANGED);
5f5c8ee5
GM
14236 }
14237 }
14238 else if (last_unchanged_at_beg_row == NULL)
23e8bd86 14239 GIVE_UP (19);
5f5c8ee5
GM
14240
14241
14242#if GLYPH_DEBUG
2311178e 14243
5f5c8ee5
GM
14244 /* Either there is no unchanged row at the end, or the one we have
14245 now displays text. This is a necessary condition for the window
14246 end pos calculation at the end of this function. */
14247 xassert (first_unchanged_at_end_row == NULL
14248 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
2311178e 14249
5f5c8ee5
GM
14250 debug_last_unchanged_at_beg_vpos
14251 = (last_unchanged_at_beg_row
14252 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14253 : -1);
14254 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
2311178e 14255
5f5c8ee5
GM
14256#endif /* GLYPH_DEBUG != 0 */
14257
2311178e 14258
5f5c8ee5
GM
14259 /* Display new lines. Set last_text_row to the last new line
14260 displayed which has text on it, i.e. might end up as being the
14261 line where the window_end_vpos is. */
14262 w->cursor.vpos = -1;
14263 last_text_row = NULL;
14264 overlay_arrow_seen = 0;
14265 while (it.current_y < it.last_visible_y
14266 && !fonts_changed_p
14267 && (first_unchanged_at_end_row == NULL
14268 || IT_CHARPOS (it) < stop_pos))
14269 {
14270 if (display_line (&it))
14271 last_text_row = it.glyph_row - 1;
14272 }
14273
14274 if (fonts_changed_p)
14275 return -1;
14276
14277
14278 /* Compute differences in buffer positions, y-positions etc. for
14279 lines reused at the bottom of the window. Compute what we can
14280 scroll. */
ca42b2e8
GM
14281 if (first_unchanged_at_end_row
14282 /* No lines reused because we displayed everything up to the
14283 bottom of the window. */
14284 && it.current_y < it.last_visible_y)
5f5c8ee5
GM
14285 {
14286 dvpos = (it.vpos
14287 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14288 current_matrix));
14289 dy = it.current_y - first_unchanged_at_end_row->y;
14290 run.current_y = first_unchanged_at_end_row->y;
14291 run.desired_y = run.current_y + dy;
14292 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14293 }
14294 else
ca42b2e8
GM
14295 {
14296 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14297 first_unchanged_at_end_row = NULL;
14298 }
5f5c8ee5
GM
14299 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14300
8f8ba186 14301
5f5c8ee5
GM
14302 /* Find the cursor if not already found. We have to decide whether
14303 PT will appear on this window (it sometimes doesn't, but this is
14304 not a very frequent case.) This decision has to be made before
14305 the current matrix is altered. A value of cursor.vpos < 0 means
14306 that PT is either in one of the lines beginning at
14307 first_unchanged_at_end_row or below the window. Don't care for
14308 lines that might be displayed later at the window end; as
14309 mentioned, this is not a frequent case. */
14310 if (w->cursor.vpos < 0)
14311 {
5f5c8ee5
GM
14312 /* Cursor in unchanged rows at the top? */
14313 if (PT < CHARPOS (start_pos)
14314 && last_unchanged_at_beg_row)
14315 {
e037b9ec
GM
14316 row = row_containing_pos (w, PT,
14317 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
0df56f4d 14318 last_unchanged_at_beg_row + 1, 0);
bfe0ee88
GM
14319 if (row)
14320 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
5f5c8ee5
GM
14321 }
14322
14323 /* Start from first_unchanged_at_end_row looking for PT. */
14324 else if (first_unchanged_at_end_row)
14325 {
e037b9ec 14326 row = row_containing_pos (w, PT - delta,
0df56f4d 14327 first_unchanged_at_end_row, NULL, 0);
e037b9ec 14328 if (row)
468155d7
GM
14329 set_cursor_from_row (w, row, w->current_matrix, delta,
14330 delta_bytes, dy, dvpos);
5f5c8ee5
GM
14331 }
14332
14333 /* Give up if cursor was not found. */
14334 if (w->cursor.vpos < 0)
14335 {
14336 clear_glyph_matrix (w->desired_matrix);
14337 return -1;
14338 }
14339 }
2311178e 14340
5f5c8ee5
GM
14341 /* Don't let the cursor end in the scroll margins. */
14342 {
14343 int this_scroll_margin, cursor_height;
2311178e 14344
5f5c8ee5 14345 this_scroll_margin = max (0, scroll_margin);
da8b7f4f
KS
14346 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14347 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
5f5c8ee5 14348 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
2311178e 14349
5f5c8ee5
GM
14350 if ((w->cursor.y < this_scroll_margin
14351 && CHARPOS (start) > BEGV)
38793d6a
KS
14352 /* Old redisplay didn't take scroll margin into account at the bottom,
14353 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
8232c913
KS
14354 || (w->cursor.y + (make_cursor_line_fully_visible_p
14355 ? cursor_height + this_scroll_margin
14356 : 1)) > it.last_visible_y)
5f5c8ee5
GM
14357 {
14358 w->cursor.vpos = -1;
14359 clear_glyph_matrix (w->desired_matrix);
14360 return -1;
14361 }
14362 }
14363
14364 /* Scroll the display. Do it before changing the current matrix so
14365 that xterm.c doesn't get confused about where the cursor glyph is
14366 found. */
fa77249f 14367 if (dy && run.height)
5f5c8ee5
GM
14368 {
14369 update_begin (f);
2311178e 14370
5f5c8ee5
GM
14371 if (FRAME_WINDOW_P (f))
14372 {
14373 rif->update_window_begin_hook (w);
fa3c6b4d 14374 rif->clear_window_mouse_face (w);
5f5c8ee5 14375 rif->scroll_run_hook (w, &run);
64d1e7d3 14376 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5
GM
14377 }
14378 else
14379 {
14380 /* Terminal frame. In this case, dvpos gives the number of
14381 lines to scroll by; dvpos < 0 means scroll up. */
14382 int first_unchanged_at_end_vpos
14383 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
da8b7f4f
KS
14384 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14385 int end = (WINDOW_TOP_EDGE_LINE (w)
522ed7fb
GM
14386 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14387 + window_internal_height (w));
2311178e 14388
5f5c8ee5
GM
14389 /* Perform the operation on the screen. */
14390 if (dvpos > 0)
14391 {
14392 /* Scroll last_unchanged_at_beg_row to the end of the
14393 window down dvpos lines. */
14394 set_terminal_window (end);
14395
14396 /* On dumb terminals delete dvpos lines at the end
14397 before inserting dvpos empty lines. */
14398 if (!scroll_region_ok)
14399 ins_del_lines (end - dvpos, -dvpos);
14400
14401 /* Insert dvpos empty lines in front of
14402 last_unchanged_at_beg_row. */
14403 ins_del_lines (from, dvpos);
14404 }
14405 else if (dvpos < 0)
14406 {
14407 /* Scroll up last_unchanged_at_beg_vpos to the end of
14408 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14409 set_terminal_window (end);
14410
14411 /* Delete dvpos lines in front of
14412 last_unchanged_at_beg_vpos. ins_del_lines will set
14413 the cursor to the given vpos and emit |dvpos| delete
14414 line sequences. */
14415 ins_del_lines (from + dvpos, dvpos);
14416
14417 /* On a dumb terminal insert dvpos empty lines at the
14418 end. */
14419 if (!scroll_region_ok)
14420 ins_del_lines (end + dvpos, -dvpos);
14421 }
2311178e 14422
5f5c8ee5
GM
14423 set_terminal_window (0);
14424 }
2311178e 14425
5f5c8ee5
GM
14426 update_end (f);
14427 }
14428
ca42b2e8
GM
14429 /* Shift reused rows of the current matrix to the right position.
14430 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14431 text. */
14432 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14433 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
5f5c8ee5
GM
14434 if (dvpos < 0)
14435 {
14436 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14437 bottom_vpos, dvpos);
14438 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14439 bottom_vpos, 0);
14440 }
14441 else if (dvpos > 0)
14442 {
14443 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14444 bottom_vpos, dvpos);
14445 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14446 first_unchanged_at_end_vpos + dvpos, 0);
14447 }
14448
14449 /* For frame-based redisplay, make sure that current frame and window
14450 matrix are in sync with respect to glyph memory. */
14451 if (!FRAME_WINDOW_P (f))
14452 sync_frame_with_window_matrix_rows (w);
14453
14454 /* Adjust buffer positions in reused rows. */
14455 if (delta)
f2d86d7a
GM
14456 increment_matrix_positions (current_matrix,
14457 first_unchanged_at_end_vpos + dvpos,
14458 bottom_vpos, delta, delta_bytes);
5f5c8ee5
GM
14459
14460 /* Adjust Y positions. */
14461 if (dy)
14462 shift_glyph_matrix (w, current_matrix,
14463 first_unchanged_at_end_vpos + dvpos,
14464 bottom_vpos, dy);
14465
14466 if (first_unchanged_at_end_row)
f1deb763
KS
14467 {
14468 first_unchanged_at_end_row += dvpos;
14469 if (first_unchanged_at_end_row->y >= it.last_visible_y
14470 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14471 first_unchanged_at_end_row = NULL;
14472 }
5f5c8ee5
GM
14473
14474 /* If scrolling up, there may be some lines to display at the end of
14475 the window. */
14476 last_text_row_at_end = NULL;
14477 if (dy < 0)
14478 {
9c6ba6d1
GM
14479 /* Scrolling up can leave for example a partially visible line
14480 at the end of the window to be redisplayed. */
5f5c8ee5
GM
14481 /* Set last_row to the glyph row in the current matrix where the
14482 window end line is found. It has been moved up or down in
14483 the matrix by dvpos. */
14484 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14485 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14486
14487 /* If last_row is the window end line, it should display text. */
14488 xassert (last_row->displays_text_p);
14489
14490 /* If window end line was partially visible before, begin
14491 displaying at that line. Otherwise begin displaying with the
14492 line following it. */
14493 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14494 {
14495 init_to_row_start (&it, w, last_row);
14496 it.vpos = last_vpos;
14497 it.current_y = last_row->y;
14498 }
14499 else
14500 {
14501 init_to_row_end (&it, w, last_row);
14502 it.vpos = 1 + last_vpos;
14503 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14504 ++last_row;
14505 }
12adba34 14506
23e8bd86
GM
14507 /* We may start in a continuation line. If so, we have to
14508 get the right continuation_lines_width and current_x. */
14509 it.continuation_lines_width = last_row->continuation_lines_width;
14510 it.hpos = it.current_x = 0;
2311178e 14511
23e8bd86
GM
14512 /* Display the rest of the lines at the window end. */
14513 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14514 while (it.current_y < it.last_visible_y
14515 && !fonts_changed_p)
5f5c8ee5 14516 {
23e8bd86
GM
14517 /* Is it always sure that the display agrees with lines in
14518 the current matrix? I don't think so, so we mark rows
14519 displayed invalid in the current matrix by setting their
14520 enabled_p flag to zero. */
14521 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14522 if (display_line (&it))
14523 last_text_row_at_end = it.glyph_row - 1;
5f5c8ee5
GM
14524 }
14525 }
12adba34 14526
5f5c8ee5
GM
14527 /* Update window_end_pos and window_end_vpos. */
14528 if (first_unchanged_at_end_row
5f5c8ee5
GM
14529 && !last_text_row_at_end)
14530 {
14531 /* Window end line if one of the preserved rows from the current
14532 matrix. Set row to the last row displaying text in current
14533 matrix starting at first_unchanged_at_end_row, after
14534 scrolling. */
14535 xassert (first_unchanged_at_end_row->displays_text_p);
14536 row = find_last_row_displaying_text (w->current_matrix, &it,
14537 first_unchanged_at_end_row);
14538 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
2311178e 14539
ac90c44f 14540 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
5f5c8ee5 14541 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
ac90c44f
GM
14542 w->window_end_vpos
14543 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
23fca891 14544 xassert (w->window_end_bytepos >= 0);
0416532f 14545 IF_DEBUG (debug_method_add (w, "A"));
5f5c8ee5
GM
14546 }
14547 else if (last_text_row_at_end)
14548 {
ac90c44f
GM
14549 w->window_end_pos
14550 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
5f5c8ee5
GM
14551 w->window_end_bytepos
14552 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
ac90c44f
GM
14553 w->window_end_vpos
14554 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
23fca891 14555 xassert (w->window_end_bytepos >= 0);
0416532f 14556 IF_DEBUG (debug_method_add (w, "B"));
5f5c8ee5
GM
14557 }
14558 else if (last_text_row)
14559 {
14560 /* We have displayed either to the end of the window or at the
14561 end of the window, i.e. the last row with text is to be found
14562 in the desired matrix. */
ac90c44f
GM
14563 w->window_end_pos
14564 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
5f5c8ee5
GM
14565 w->window_end_bytepos
14566 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
14567 w->window_end_vpos
14568 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
23fca891 14569 xassert (w->window_end_bytepos >= 0);
5f5c8ee5
GM
14570 }
14571 else if (first_unchanged_at_end_row == NULL
14572 && last_text_row == NULL
14573 && last_text_row_at_end == NULL)
14574 {
14575 /* Displayed to end of window, but no line containing text was
14576 displayed. Lines were deleted at the end of the window. */
0416532f
GM
14577 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14578 int vpos = XFASTINT (w->window_end_vpos);
14579 struct glyph_row *current_row = current_matrix->rows + vpos;
14580 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14581
14582 for (row = NULL;
14583 row == NULL && vpos >= first_vpos;
14584 --vpos, --current_row, --desired_row)
14585 {
14586 if (desired_row->enabled_p)
14587 {
14588 if (desired_row->displays_text_p)
14589 row = desired_row;
14590 }
14591 else if (current_row->displays_text_p)
14592 row = current_row;
14593 }
12adba34 14594
0416532f 14595 xassert (row != NULL);
d88a79d4 14596 w->window_end_vpos = make_number (vpos + 1);
8c56a983
GM
14597 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14598 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
23fca891 14599 xassert (w->window_end_bytepos >= 0);
0416532f 14600 IF_DEBUG (debug_method_add (w, "C"));
5f5c8ee5
GM
14601 }
14602 else
14603 abort ();
ac90c44f 14604
8cdb267e
GM
14605#if 0 /* This leads to problems, for instance when the cursor is
14606 at ZV, and the cursor line displays no text. */
ac90c44f
GM
14607 /* Disable rows below what's displayed in the window. This makes
14608 debugging easier. */
14609 enable_glyph_matrix_rows (current_matrix,
14610 XFASTINT (w->window_end_vpos) + 1,
14611 bottom_vpos, 0);
8cdb267e 14612#endif
23e8bd86 14613
5f5c8ee5
GM
14614 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
14615 debug_end_vpos = XFASTINT (w->window_end_vpos));
12adba34 14616
5f5c8ee5
GM
14617 /* Record that display has not been completed. */
14618 w->window_end_valid = Qnil;
14619 w->desired_matrix->no_scrolling_p = 1;
ef121659 14620 return 3;
27d16f05
GM
14621
14622#undef GIVE_UP
12adba34 14623}
0f9c0ff0 14624
a2889657 14625
5f5c8ee5
GM
14626\f
14627/***********************************************************************
14628 More debugging support
14629 ***********************************************************************/
a2889657 14630
5f5c8ee5 14631#if GLYPH_DEBUG
a2889657 14632
eaaa65b0 14633void dump_glyph_row P_ ((struct glyph_row *, int, int));
9ab436b9 14634void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
e187cf71 14635void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
1c9241f5 14636
31b24551 14637
9ab436b9 14638/* Dump the contents of glyph matrix MATRIX on stderr.
ca26e1c8 14639
9ab436b9
GM
14640 GLYPHS 0 means don't show glyph contents.
14641 GLYPHS 1 means show glyphs in short form
14642 GLYPHS > 1 means show glyphs in long form. */
14643
14644void
14645dump_glyph_matrix (matrix, glyphs)
5f5c8ee5 14646 struct glyph_matrix *matrix;
9ab436b9 14647 int glyphs;
5f5c8ee5 14648{
efc63ef0 14649 int i;
5f5c8ee5 14650 for (i = 0; i < matrix->nrows; ++i)
eaaa65b0 14651 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
5f5c8ee5 14652}
31b24551 14653
68a37fa8 14654
e187cf71
GM
14655/* Dump contents of glyph GLYPH to stderr. ROW and AREA are
14656 the glyph row and area where the glyph comes from. */
14657
14658void
14659dump_glyph (row, glyph, area)
14660 struct glyph_row *row;
14661 struct glyph *glyph;
14662 int area;
14663{
14664 if (glyph->type == CHAR_GLYPH)
14665 {
14666 fprintf (stderr,
14667 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14668 glyph - row->glyphs[TEXT_AREA],
14669 'C',
14670 glyph->charpos,
14671 (BUFFERP (glyph->object)
14672 ? 'B'
14673 : (STRINGP (glyph->object)
14674 ? 'S'
14675 : '-')),
14676 glyph->pixel_width,
14677 glyph->u.ch,
14678 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
14679 ? glyph->u.ch
14680 : '.'),
14681 glyph->face_id,
14682 glyph->left_box_line_p,
14683 glyph->right_box_line_p);
14684 }
14685 else if (glyph->type == STRETCH_GLYPH)
14686 {
14687 fprintf (stderr,
14688 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14689 glyph - row->glyphs[TEXT_AREA],
14690 'S',
14691 glyph->charpos,
14692 (BUFFERP (glyph->object)
14693 ? 'B'
14694 : (STRINGP (glyph->object)
14695 ? 'S'
14696 : '-')),
14697 glyph->pixel_width,
14698 0,
14699 '.',
14700 glyph->face_id,
14701 glyph->left_box_line_p,
14702 glyph->right_box_line_p);
14703 }
14704 else if (glyph->type == IMAGE_GLYPH)
14705 {
14706 fprintf (stderr,
14707 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14708 glyph - row->glyphs[TEXT_AREA],
14709 'I',
14710 glyph->charpos,
14711 (BUFFERP (glyph->object)
14712 ? 'B'
14713 : (STRINGP (glyph->object)
14714 ? 'S'
14715 : '-')),
14716 glyph->pixel_width,
14717 glyph->u.img_id,
14718 '.',
14719 glyph->face_id,
14720 glyph->left_box_line_p,
14721 glyph->right_box_line_p);
14722 }
14723}
14724
14725
5f5c8ee5 14726/* Dump the contents of glyph row at VPOS in MATRIX to stderr.
9ab436b9
GM
14727 GLYPHS 0 means don't show glyph contents.
14728 GLYPHS 1 means show glyphs in short form
14729 GLYPHS > 1 means show glyphs in long form. */
a2889657 14730
5f5c8ee5 14731void
eaaa65b0
GM
14732dump_glyph_row (row, vpos, glyphs)
14733 struct glyph_row *row;
9ab436b9 14734 int vpos, glyphs;
5f5c8ee5 14735{
9ab436b9
GM
14736 if (glyphs != 1)
14737 {
e235145c
MB
14738 fprintf (stderr, "Row Start End Used oEI><\\CTZFesm X Y W H V A P\n");
14739 fprintf (stderr, "======================================================================\n");
2311178e 14740
e235145c 14741 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
33ea6c4d 14742%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 14743 vpos,
9ab436b9
GM
14744 MATRIX_ROW_START_CHARPOS (row),
14745 MATRIX_ROW_END_CHARPOS (row),
14746 row->used[TEXT_AREA],
14747 row->contains_overlapping_glyphs_p,
14748 row->enabled_p,
9ab436b9
GM
14749 row->truncated_on_left_p,
14750 row->truncated_on_right_p,
9ab436b9
GM
14751 row->continued_p,
14752 MATRIX_ROW_CONTINUATION_LINE_P (row),
14753 row->displays_text_p,
14754 row->ends_at_zv_p,
14755 row->fill_line_p,
14756 row->ends_in_middle_of_char_p,
14757 row->starts_in_middle_of_char_p,
b94c0d9c 14758 row->mouse_face_p,
9ab436b9
GM
14759 row->x,
14760 row->y,
14761 row->pixel_width,
14762 row->height,
14763 row->visible_height,
14764 row->ascent,
14765 row->phys_ascent);
33ea6c4d
GM
14766 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
14767 row->end.overlay_string_index,
14768 row->continuation_lines_width);
9ab436b9
GM
14769 fprintf (stderr, "%9d %5d\n",
14770 CHARPOS (row->start.string_pos),
14771 CHARPOS (row->end.string_pos));
14772 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
14773 row->end.dpvec_index);
14774 }
2311178e 14775
9ab436b9 14776 if (glyphs > 1)
bd66d1ba 14777 {
e187cf71
GM
14778 int area;
14779
14780 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14781 {
091f8878
GM
14782 struct glyph *glyph = row->glyphs[area];
14783 struct glyph *glyph_end = glyph + row->used[area];
2311178e 14784
e187cf71 14785 /* Glyph for a line end in text. */
091f8878 14786 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
e187cf71 14787 ++glyph_end;
2311178e 14788
e187cf71
GM
14789 if (glyph < glyph_end)
14790 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
2311178e 14791
e187cf71
GM
14792 for (; glyph < glyph_end; ++glyph)
14793 dump_glyph (row, glyph, area);
f7b4b63a 14794 }
f4faa47c 14795 }
9ab436b9
GM
14796 else if (glyphs == 1)
14797 {
e187cf71 14798 int area;
9ab436b9 14799
e187cf71 14800 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
9ab436b9 14801 {
e187cf71
GM
14802 char *s = (char *) alloca (row->used[area] + 1);
14803 int i;
14804
14805 for (i = 0; i < row->used[area]; ++i)
14806 {
14807 struct glyph *glyph = row->glyphs[area] + i;
14808 if (glyph->type == CHAR_GLYPH
14809 && glyph->u.ch < 0x80
14810 && glyph->u.ch >= ' ')
14811 s[i] = glyph->u.ch;
14812 else
14813 s[i] = '.';
14814 }
2311178e 14815
e187cf71
GM
14816 s[i] = '\0';
14817 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
14818 }
9ab436b9 14819 }
5f5c8ee5 14820}
f4faa47c 14821
a2889657 14822
5f5c8ee5
GM
14823DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
14824 Sdump_glyph_matrix, 0, 1, "p",
7ee72033 14825 doc: /* Dump the current matrix of the selected window to stderr.
228299fa
GM
14826Shows contents of glyph row structures. With non-nil
14827parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
7ee72033
MB
14828glyphs in short form, otherwise show glyphs in long form. */)
14829 (glyphs)
9ab436b9 14830 Lisp_Object glyphs;
5f5c8ee5
GM
14831{
14832 struct window *w = XWINDOW (selected_window);
14833 struct buffer *buffer = XBUFFER (w->buffer);
14834
14835 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14836 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14837 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14838 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14839 fprintf (stderr, "=============================================\n");
9ab436b9
GM
14840 dump_glyph_matrix (w->current_matrix,
14841 NILP (glyphs) ? 0 : XINT (glyphs));
5f5c8ee5
GM
14842 return Qnil;
14843}
1c2250c2 14844
1fca3fae 14845
7d4cc828
GM
14846DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14847 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14848 ()
14849{
14850 struct frame *f = XFRAME (selected_frame);
14851 dump_glyph_matrix (f->current_matrix, 1);
14852 return Qnil;
14853}
14854
14855
e9abc296 14856DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
7ee72033 14857 doc: /* Dump glyph row ROW to stderr.
228299fa
GM
14858GLYPH 0 means don't dump glyphs.
14859GLYPH 1 means dump glyphs in short form.
7ee72033
MB
14860GLYPH > 1 or omitted means dump glyphs in long form. */)
14861 (row, glyphs)
e9abc296 14862 Lisp_Object row, glyphs;
5f5c8ee5 14863{
eaaa65b0
GM
14864 struct glyph_matrix *matrix;
14865 int vpos;
2311178e 14866
b7826503 14867 CHECK_NUMBER (row);
eaaa65b0
GM
14868 matrix = XWINDOW (selected_window)->current_matrix;
14869 vpos = XINT (row);
14870 if (vpos >= 0 && vpos < matrix->nrows)
14871 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14872 vpos,
14873 INTEGERP (glyphs) ? XINT (glyphs) : 2);
5f5c8ee5
GM
14874 return Qnil;
14875}
1fca3fae 14876
67481ae5 14877
b94c0d9c 14878DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
c2552f79 14879 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
228299fa
GM
14880GLYPH 0 means don't dump glyphs.
14881GLYPH 1 means dump glyphs in short form.
7ee72033
MB
14882GLYPH > 1 or omitted means dump glyphs in long form. */)
14883 (row, glyphs)
b94c0d9c 14884 Lisp_Object row, glyphs;
5f5c8ee5 14885{
886bd6f2 14886 struct frame *sf = SELECTED_FRAME ();
eaaa65b0
GM
14887 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14888 int vpos;
2311178e 14889
b7826503 14890 CHECK_NUMBER (row);
eaaa65b0
GM
14891 vpos = XINT (row);
14892 if (vpos >= 0 && vpos < m->nrows)
14893 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14894 INTEGERP (glyphs) ? XINT (glyphs) : 2);
5f5c8ee5
GM
14895 return Qnil;
14896}
ca26e1c8 14897
0f9c0ff0 14898
62397849 14899DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
7ee72033
MB
14900 doc: /* Toggle tracing of redisplay.
14901With ARG, turn tracing on if and only if ARG is positive. */)
14902 (arg)
62397849 14903 Lisp_Object arg;
5f5c8ee5 14904{
62397849
GM
14905 if (NILP (arg))
14906 trace_redisplay_p = !trace_redisplay_p;
14907 else
14908 {
14909 arg = Fprefix_numeric_value (arg);
14910 trace_redisplay_p = XINT (arg) > 0;
14911 }
2311178e 14912
5f5c8ee5
GM
14913 return Qnil;
14914}
bf9249e3
GM
14915
14916
f4a374a1 14917DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
e9757d41
SM
14918 doc: /* Like `format', but print result to stderr.
14919usage: (trace-to-stderr STRING &rest OBJECTS) */)
7ee72033 14920 (nargs, args)
f4a374a1
GM
14921 int nargs;
14922 Lisp_Object *args;
bf9249e3 14923{
f4a374a1 14924 Lisp_Object s = Fformat (nargs, args);
2051c264 14925 fprintf (stderr, "%s", SDATA (s));
bf9249e3
GM
14926 return Qnil;
14927}
2311178e 14928
5f5c8ee5 14929#endif /* GLYPH_DEBUG */
ca26e1c8 14930
ca26e1c8 14931
5f5c8ee5
GM
14932\f
14933/***********************************************************************
14934 Building Desired Matrix Rows
14935 ***********************************************************************/
a2889657 14936
c514efbb
KS
14937/* Return a temporary glyph row holding the glyphs of an overlay arrow.
14938 Used for non-window-redisplay windows, and for windows w/o left fringe. */
ca26e1c8 14939
5f5c8ee5 14940static struct glyph_row *
351b5434 14941get_overlay_arrow_glyph_row (w, overlay_arrow_string)
5f5c8ee5 14942 struct window *w;
351b5434 14943 Lisp_Object overlay_arrow_string;
5f5c8ee5
GM
14944{
14945 struct frame *f = XFRAME (WINDOW_FRAME (w));
14946 struct buffer *buffer = XBUFFER (w->buffer);
14947 struct buffer *old = current_buffer;
351b5434
KS
14948 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14949 int arrow_len = SCHARS (overlay_arrow_string);
50f80c2f
KR
14950 const unsigned char *arrow_end = arrow_string + arrow_len;
14951 const unsigned char *p;
5f5c8ee5
GM
14952 struct it it;
14953 int multibyte_p;
14954 int n_glyphs_before;
14955
14956 set_buffer_temp (buffer);
14957 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14958 it.glyph_row->used[TEXT_AREA] = 0;
14959 SET_TEXT_POS (it.position, 0, 0);
14960
14961 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14962 p = arrow_string;
14963 while (p < arrow_end)
14964 {
14965 Lisp_Object face, ilisp;
2311178e 14966
5f5c8ee5
GM
14967 /* Get the next character. */
14968 if (multibyte_p)
4fdb80f2 14969 it.c = string_char_and_length (p, arrow_len, &it.len);
5f5c8ee5
GM
14970 else
14971 it.c = *p, it.len = 1;
14972 p += it.len;
2311178e 14973
5f5c8ee5 14974 /* Get its face. */
ac90c44f 14975 ilisp = make_number (p - arrow_string);
351b5434 14976 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
5f5c8ee5
GM
14977 it.face_id = compute_char_face (f, it.c, face);
14978
14979 /* Compute its width, get its glyphs. */
14980 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
337042a9 14981 SET_TEXT_POS (it.position, -1, -1);
5f5c8ee5
GM
14982 PRODUCE_GLYPHS (&it);
14983
14984 /* If this character doesn't fit any more in the line, we have
14985 to remove some glyphs. */
14986 if (it.current_x > it.last_visible_x)
14987 {
14988 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14989 break;
14990 }
14991 }
2311178e 14992
5f5c8ee5
GM
14993 set_buffer_temp (old);
14994 return it.glyph_row;
14995}
ca26e1c8 14996
b0a0fbda 14997
5f5c8ee5
GM
14998/* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14999 glyphs are only inserted for terminal frames since we can't really
15000 win with truncation glyphs when partially visible glyphs are
15001 involved. Which glyphs to insert is determined by
15002 produce_special_glyphs. */
67481ae5 15003
5f5c8ee5
GM
15004static void
15005insert_left_trunc_glyphs (it)
15006 struct it *it;
15007{
15008 struct it truncate_it;
15009 struct glyph *from, *end, *to, *toend;
15010
15011 xassert (!FRAME_WINDOW_P (it->f));
15012
15013 /* Get the truncation glyphs. */
15014 truncate_it = *it;
5f5c8ee5
GM
15015 truncate_it.current_x = 0;
15016 truncate_it.face_id = DEFAULT_FACE_ID;
15017 truncate_it.glyph_row = &scratch_glyph_row;
15018 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15019 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
6fc556fd 15020 truncate_it.object = make_number (0);
5f5c8ee5 15021 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
2311178e 15022
5f5c8ee5
GM
15023 /* Overwrite glyphs from IT with truncation glyphs. */
15024 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15025 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15026 to = it->glyph_row->glyphs[TEXT_AREA];
15027 toend = to + it->glyph_row->used[TEXT_AREA];
15028
15029 while (from < end)
15030 *to++ = *from++;
15031
37be86f2
KH
15032 /* There may be padding glyphs left over. Overwrite them too. */
15033 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15034 {
15035 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15036 while (from < end)
15037 *to++ = *from++;
15038 }
5f5c8ee5 15039
37be86f2
KH
15040 if (to > toend)
15041 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
5f5c8ee5 15042}
e0bfbde6 15043
e0bfbde6 15044
5f5c8ee5 15045/* Compute the pixel height and width of IT->glyph_row.
9c49d3d7 15046
5f5c8ee5
GM
15047 Most of the time, ascent and height of a display line will be equal
15048 to the max_ascent and max_height values of the display iterator
15049 structure. This is not the case if
67481ae5 15050
5f5c8ee5
GM
15051 1. We hit ZV without displaying anything. In this case, max_ascent
15052 and max_height will be zero.
1c9241f5 15053
5f5c8ee5
GM
15054 2. We have some glyphs that don't contribute to the line height.
15055 (The glyph row flag contributes_to_line_height_p is for future
15056 pixmap extensions).
f6fd109b 15057
5f5c8ee5
GM
15058 The first case is easily covered by using default values because in
15059 these cases, the line height does not really matter, except that it
15060 must not be zero. */
67481ae5 15061
5f5c8ee5
GM
15062static void
15063compute_line_metrics (it)
15064 struct it *it;
15065{
15066 struct glyph_row *row = it->glyph_row;
15067 int area, i;
1c2250c2 15068
5f5c8ee5
GM
15069 if (FRAME_WINDOW_P (it->f))
15070 {
75c5350a 15071 int i, min_y, max_y;
1c2250c2 15072
5f5c8ee5
GM
15073 /* The line may consist of one space only, that was added to
15074 place the cursor on it. If so, the row's height hasn't been
15075 computed yet. */
15076 if (row->height == 0)
15077 {
15078 if (it->max_ascent + it->max_descent == 0)
da8b7f4f 15079 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
5f5c8ee5
GM
15080 row->ascent = it->max_ascent;
15081 row->height = it->max_ascent + it->max_descent;
312246d1
GM
15082 row->phys_ascent = it->max_phys_ascent;
15083 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
546f98b8 15084 row->extra_line_spacing = it->max_extra_line_spacing;
5f5c8ee5 15085 }
2311178e 15086
5f5c8ee5
GM
15087 /* Compute the width of this line. */
15088 row->pixel_width = row->x;
15089 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15090 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15091
15092 xassert (row->pixel_width >= 0);
15093 xassert (row->ascent >= 0 && row->height > 0);
15094
312246d1
GM
15095 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15096 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15097
15098 /* If first line's physical ascent is larger than its logical
15099 ascent, use the physical ascent, and make the row taller.
15100 This makes accented characters fully visible. */
b28cb6ed 15101 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
312246d1
GM
15102 && row->phys_ascent > row->ascent)
15103 {
15104 row->height += row->phys_ascent - row->ascent;
15105 row->ascent = row->phys_ascent;
15106 }
15107
5f5c8ee5
GM
15108 /* Compute how much of the line is visible. */
15109 row->visible_height = row->height;
2311178e 15110
da8b7f4f
KS
15111 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15112 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
75c5350a
GM
15113
15114 if (row->y < min_y)
15115 row->visible_height -= min_y - row->y;
15116 if (row->y + row->height > max_y)
15117 row->visible_height -= row->y + row->height - max_y;
5f5c8ee5
GM
15118 }
15119 else
15120 {
15121 row->pixel_width = row->used[TEXT_AREA];
33ea6c4d
GM
15122 if (row->continued_p)
15123 row->pixel_width -= it->continuation_pixel_width;
15124 else if (row->truncated_on_right_p)
15125 row->pixel_width -= it->truncation_pixel_width;
312246d1
GM
15126 row->ascent = row->phys_ascent = 0;
15127 row->height = row->phys_height = row->visible_height = 1;
546f98b8 15128 row->extra_line_spacing = 0;
5f5c8ee5 15129 }
67481ae5 15130
5f5c8ee5
GM
15131 /* Compute a hash code for this row. */
15132 row->hash = 0;
15133 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15134 for (i = 0; i < row->used[area]; ++i)
15135 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15136 + row->glyphs[area][i].u.val
43d120d8
KH
15137 + row->glyphs[area][i].face_id
15138 + row->glyphs[area][i].padding_p
5f5c8ee5 15139 + (row->glyphs[area][i].type << 2));
a2889657 15140
5f5c8ee5 15141 it->max_ascent = it->max_descent = 0;
312246d1 15142 it->max_phys_ascent = it->max_phys_descent = 0;
5f5c8ee5 15143}
12adba34 15144
ca26e1c8 15145
5f5c8ee5 15146/* Append one space to the glyph row of iterator IT if doing a
03e35edc 15147 window-based redisplay. The space has the same face as
80c6cb1f 15148 IT->face_id. Value is non-zero if a space was added.
c6e89d6c
GM
15149
15150 This function is called to make sure that there is always one glyph
15151 at the end of a glyph row that the cursor can be set on under
15152 window-systems. (If there weren't such a glyph we would not know
15153 how wide and tall a box cursor should be displayed).
15154
15155 At the same time this space let's a nicely handle clearing to the
15156 end of the line if the row ends in italic text. */
ca26e1c8 15157
80c6cb1f 15158static int
4933c603 15159append_space_for_newline (it, default_face_p)
5f5c8ee5 15160 struct it *it;
4933c603 15161 int default_face_p;
5f5c8ee5
GM
15162{
15163 if (FRAME_WINDOW_P (it->f))
15164 {
15165 int n = it->glyph_row->used[TEXT_AREA];
ca26e1c8 15166
5f5c8ee5
GM
15167 if (it->glyph_row->glyphs[TEXT_AREA] + n
15168 < it->glyph_row->glyphs[1 + TEXT_AREA])
a2889657 15169 {
cafafe0b
GM
15170 /* Save some values that must not be changed.
15171 Must save IT->c and IT->len because otherwise
15172 ITERATOR_AT_END_P wouldn't work anymore after
03e35edc 15173 append_space_for_newline has been called. */
478d746b 15174 enum display_element_type saved_what = it->what;
cafafe0b
GM
15175 int saved_c = it->c, saved_len = it->len;
15176 int saved_x = it->current_x;
5f5c8ee5 15177 int saved_face_id = it->face_id;
cafafe0b 15178 struct text_pos saved_pos;
5f5c8ee5 15179 Lisp_Object saved_object;
980806b6 15180 struct face *face;
5f5c8ee5
GM
15181
15182 saved_object = it->object;
15183 saved_pos = it->position;
2311178e 15184
5f5c8ee5
GM
15185 it->what = IT_CHARACTER;
15186 bzero (&it->position, sizeof it->position);
6fc556fd 15187 it->object = make_number (0);
5f5c8ee5
GM
15188 it->c = ' ';
15189 it->len = 1;
5f5c8ee5 15190
4933c603
KS
15191 if (default_face_p)
15192 it->face_id = DEFAULT_FACE_ID;
15193 else if (it->face_before_selective_p)
4aad61f8 15194 it->face_id = it->saved_face_id;
980806b6
KH
15195 face = FACE_FROM_ID (it->f, it->face_id);
15196 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
1842fc1a 15197
5f5c8ee5 15198 PRODUCE_GLYPHS (it);
2311178e 15199
a9e5c932 15200 it->override_ascent = -1;
e893970b 15201 it->constrain_row_ascent_descent_p = 0;
5f5c8ee5
GM
15202 it->current_x = saved_x;
15203 it->object = saved_object;
15204 it->position = saved_pos;
15205 it->what = saved_what;
15206 it->face_id = saved_face_id;
cafafe0b
GM
15207 it->len = saved_len;
15208 it->c = saved_c;
80c6cb1f 15209 return 1;
5f5c8ee5
GM
15210 }
15211 }
80c6cb1f
GM
15212
15213 return 0;
5f5c8ee5 15214}
12adba34 15215
1842fc1a 15216
5f5c8ee5
GM
15217/* Extend the face of the last glyph in the text area of IT->glyph_row
15218 to the end of the display line. Called from display_line.
15219 If the glyph row is empty, add a space glyph to it so that we
15220 know the face to draw. Set the glyph row flag fill_line_p. */
2311178e 15221
5f5c8ee5
GM
15222static void
15223extend_face_to_end_of_line (it)
15224 struct it *it;
15225{
15226 struct face *face;
15227 struct frame *f = it->f;
1842fc1a 15228
5f5c8ee5
GM
15229 /* If line is already filled, do nothing. */
15230 if (it->current_x >= it->last_visible_x)
15231 return;
2311178e 15232
5f5c8ee5
GM
15233 /* Face extension extends the background and box of IT->face_id
15234 to the end of the line. If the background equals the background
4aad61f8
GM
15235 of the frame, we don't have to do anything. */
15236 if (it->face_before_selective_p)
15237 face = FACE_FROM_ID (it->f, it->saved_face_id);
15238 else
15239 face = FACE_FROM_ID (f, it->face_id);
2311178e 15240
5f5c8ee5
GM
15241 if (FRAME_WINDOW_P (f)
15242 && face->box == FACE_NO_BOX
15243 && face->background == FRAME_BACKGROUND_PIXEL (f)
15244 && !face->stipple)
15245 return;
1842fc1a 15246
5f5c8ee5
GM
15247 /* Set the glyph row flag indicating that the face of the last glyph
15248 in the text area has to be drawn to the end of the text area. */
15249 it->glyph_row->fill_line_p = 1;
545e04f6 15250
980806b6
KH
15251 /* If current character of IT is not ASCII, make sure we have the
15252 ASCII face. This will be automatically undone the next time
15253 get_next_display_element returns a multibyte character. Note
15254 that the character will always be single byte in unibyte text. */
15255 if (!SINGLE_BYTE_CHAR_P (it->c))
5f5c8ee5 15256 {
980806b6 15257 it->face_id = FACE_FOR_CHAR (f, face, 0);
5f5c8ee5 15258 }
545e04f6 15259
5f5c8ee5
GM
15260 if (FRAME_WINDOW_P (f))
15261 {
15262 /* If the row is empty, add a space with the current face of IT,
15263 so that we know which face to draw. */
15264 if (it->glyph_row->used[TEXT_AREA] == 0)
a2889657 15265 {
5f5c8ee5 15266 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
43d120d8 15267 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
5f5c8ee5 15268 it->glyph_row->used[TEXT_AREA] = 1;
a2889657 15269 }
5f5c8ee5
GM
15270 }
15271 else
15272 {
15273 /* Save some values that must not be changed. */
15274 int saved_x = it->current_x;
15275 struct text_pos saved_pos;
15276 Lisp_Object saved_object;
478d746b 15277 enum display_element_type saved_what = it->what;
4aad61f8 15278 int saved_face_id = it->face_id;
5f5c8ee5
GM
15279
15280 saved_object = it->object;
15281 saved_pos = it->position;
2311178e 15282
5f5c8ee5
GM
15283 it->what = IT_CHARACTER;
15284 bzero (&it->position, sizeof it->position);
6fc556fd 15285 it->object = make_number (0);
5f5c8ee5
GM
15286 it->c = ' ';
15287 it->len = 1;
4aad61f8 15288 it->face_id = face->id;
2311178e 15289
5f5c8ee5 15290 PRODUCE_GLYPHS (it);
2311178e 15291
5f5c8ee5
GM
15292 while (it->current_x <= it->last_visible_x)
15293 PRODUCE_GLYPHS (it);
2311178e 15294
5f5c8ee5
GM
15295 /* Don't count these blanks really. It would let us insert a left
15296 truncation glyph below and make us set the cursor on them, maybe. */
15297 it->current_x = saved_x;
15298 it->object = saved_object;
15299 it->position = saved_pos;
15300 it->what = saved_what;
4aad61f8 15301 it->face_id = saved_face_id;
5f5c8ee5
GM
15302 }
15303}
12adba34 15304
545e04f6 15305
5f5c8ee5
GM
15306/* Value is non-zero if text starting at CHARPOS in current_buffer is
15307 trailing whitespace. */
1c9241f5 15308
5f5c8ee5
GM
15309static int
15310trailing_whitespace_p (charpos)
15311 int charpos;
15312{
15313 int bytepos = CHAR_TO_BYTE (charpos);
15314 int c = 0;
7bbe686f 15315
5f5c8ee5
GM
15316 while (bytepos < ZV_BYTE
15317 && (c = FETCH_CHAR (bytepos),
15318 c == ' ' || c == '\t'))
15319 ++bytepos;
0d09d1e6 15320
8f897821
GM
15321 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15322 {
15323 if (bytepos != PT_BYTE)
15324 return 1;
15325 }
15326 return 0;
5f5c8ee5 15327}
31b24551 15328
545e04f6 15329
5f5c8ee5 15330/* Highlight trailing whitespace, if any, in ROW. */
545e04f6 15331
5f5c8ee5
GM
15332void
15333highlight_trailing_whitespace (f, row)
15334 struct frame *f;
15335 struct glyph_row *row;
15336{
15337 int used = row->used[TEXT_AREA];
2311178e 15338
5f5c8ee5
GM
15339 if (used)
15340 {
15341 struct glyph *start = row->glyphs[TEXT_AREA];
15342 struct glyph *glyph = start + used - 1;
15343
ade0bee1
GM
15344 /* Skip over glyphs inserted to display the cursor at the
15345 end of a line, for extending the face of the last glyph
15346 to the end of the line on terminals, and for truncation
15347 and continuation glyphs. */
65008712
GM
15348 while (glyph >= start
15349 && glyph->type == CHAR_GLYPH
65008712 15350 && INTEGERP (glyph->object))
5f5c8ee5
GM
15351 --glyph;
15352
15353 /* If last glyph is a space or stretch, and it's trailing
15354 whitespace, set the face of all trailing whitespace glyphs in
15355 IT->glyph_row to `trailing-whitespace'. */
15356 if (glyph >= start
15357 && BUFFERP (glyph->object)
15358 && (glyph->type == STRETCH_GLYPH
15359 || (glyph->type == CHAR_GLYPH
43d120d8 15360 && glyph->u.ch == ' '))
5f5c8ee5 15361 && trailing_whitespace_p (glyph->charpos))
545e04f6 15362 {
ff7d52cd
KS
15363 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15364 if (face_id < 0)
15365 return;
2311178e 15366
5f5c8ee5
GM
15367 while (glyph >= start
15368 && BUFFERP (glyph->object)
15369 && (glyph->type == STRETCH_GLYPH
15370 || (glyph->type == CHAR_GLYPH
43d120d8
KH
15371 && glyph->u.ch == ' ')))
15372 (glyph--)->face_id = face_id;
545e04f6 15373 }
a2889657 15374 }
5f5c8ee5 15375}
a2889657 15376
5fcbb24d 15377
cafafe0b 15378/* Value is non-zero if glyph row ROW in window W should be
0fd37545 15379 used to hold the cursor. */
cafafe0b
GM
15380
15381static int
15382cursor_row_p (w, row)
15383 struct window *w;
15384 struct glyph_row *row;
15385{
9a038881 15386 int cursor_row_p = 1;
2311178e 15387
cafafe0b
GM
15388 if (PT == MATRIX_ROW_END_CHARPOS (row))
15389 {
9a038881 15390 /* If the row ends with a newline from a string, we don't want
e0e9a80e
KS
15391 the cursor there, but we still want it at the start of the
15392 string if the string starts in this row.
15393 If the row is continued it doesn't end in a newline. */
f323e507 15394 if (CHARPOS (row->end.string_pos) >= 0)
e0e9a80e
KS
15395 cursor_row_p = (row->continued_p
15396 || PT >= MATRIX_ROW_START_CHARPOS (row));
f323e507
RS
15397 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15398 {
15399 /* If the row ends in middle of a real character,
15400 and the line is continued, we want the cursor here.
15401 That's because MATRIX_ROW_END_CHARPOS would equal
15402 PT if PT is before the character. */
15403 if (!row->ends_in_ellipsis_p)
15404 cursor_row_p = row->continued_p;
15405 else
15406 /* If the row ends in an ellipsis, then
15407 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15408 We want that position to be displayed after the ellipsis. */
15409 cursor_row_p = 0;
15410 }
9a038881
GM
15411 /* If the row ends at ZV, display the cursor at the end of that
15412 row instead of at the start of the row below. */
15413 else if (row->ends_at_zv_p)
15414 cursor_row_p = 1;
15415 else
15416 cursor_row_p = 0;
cafafe0b
GM
15417 }
15418
9a038881 15419 return cursor_row_p;
cafafe0b
GM
15420}
15421
15422
5f5c8ee5
GM
15423/* Construct the glyph row IT->glyph_row in the desired matrix of
15424 IT->w from text at the current position of IT. See dispextern.h
15425 for an overview of struct it. Value is non-zero if
15426 IT->glyph_row displays text, as opposed to a line displaying ZV
15427 only. */
2311178e 15428
5f5c8ee5
GM
15429static int
15430display_line (it)
15431 struct it *it;
15432{
15433 struct glyph_row *row = it->glyph_row;
351b5434 15434 Lisp_Object overlay_arrow_string;
5f5c8ee5
GM
15435
15436 /* We always start displaying at hpos zero even if hscrolled. */
15437 xassert (it->hpos == 0 && it->current_x == 0);
a2889657 15438
cf4a901e
KS
15439 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15440 >= it->w->desired_matrix->nrows)
15441 {
15442 it->w->nrows_scale_factor++;
15443 fonts_changed_p = 1;
15444 return 0;
15445 }
12adba34 15446
5f5c8ee5
GM
15447 /* Is IT->w showing the region? */
15448 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12adba34 15449
5f5c8ee5
GM
15450 /* Clear the result glyph row and enable it. */
15451 prepare_desired_row (row);
12adba34 15452
5f5c8ee5 15453 row->y = it->current_y;
29b3ea63 15454 row->start = it->start;
5f5c8ee5
GM
15455 row->continuation_lines_width = it->continuation_lines_width;
15456 row->displays_text_p = 1;
91004049
GM
15457 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15458 it->starts_in_middle_of_char_p = 0;
5f5c8ee5
GM
15459
15460 /* Arrange the overlays nicely for our purposes. Usually, we call
15461 display_line on only one line at a time, in which case this
15462 can't really hurt too much, or we call it on lines which appear
15463 one after another in the buffer, in which case all calls to
15464 recenter_overlay_lists but the first will be pretty cheap. */
15465 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15466
5f5c8ee5
GM
15467 /* Move over display elements that are not visible because we are
15468 hscrolled. This may stop at an x-position < IT->first_visible_x
15469 if the first glyph is partially visible or if we hit a line end. */
15470 if (it->current_x < it->first_visible_x)
bd0e59f8
KS
15471 {
15472 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15473 MOVE_TO_POS | MOVE_TO_X);
bd0e59f8 15474 }
5f5c8ee5
GM
15475
15476 /* Get the initial row height. This is either the height of the
15477 text hscrolled, if there is any, or zero. */
15478 row->ascent = it->max_ascent;
15479 row->height = it->max_ascent + it->max_descent;
312246d1
GM
15480 row->phys_ascent = it->max_phys_ascent;
15481 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
546f98b8 15482 row->extra_line_spacing = it->max_extra_line_spacing;
5f5c8ee5
GM
15483
15484 /* Loop generating characters. The loop is left with IT on the next
15485 character to display. */
15486 while (1)
15487 {
15488 int n_glyphs_before, hpos_before, x_before;
15489 int x, i, nglyphs;
ae26e27d 15490 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
b28cb6ed 15491
5f5c8ee5
GM
15492 /* Retrieve the next thing to display. Value is zero if end of
15493 buffer reached. */
15494 if (!get_next_display_element (it))
15495 {
15496 /* Maybe add a space at the end of this line that is used to
1b335865
GM
15497 display the cursor there under X. Set the charpos of the
15498 first glyph of blank lines not corresponding to any text
15499 to -1. */
6d925726
KS
15500#ifdef HAVE_WINDOW_SYSTEM
15501 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15502 row->exact_window_width_line_p = 1;
15503 else
15504#endif /* HAVE_WINDOW_SYSTEM */
4933c603 15505 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
1b335865 15506 || row->used[TEXT_AREA] == 0)
a2889657 15507 {
5f5c8ee5
GM
15508 row->glyphs[TEXT_AREA]->charpos = -1;
15509 row->displays_text_p = 0;
15510
2ad551af 15511 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
e6b70fd8
GM
15512 && (!MINI_WINDOW_P (it->w)
15513 || (minibuf_level && EQ (it->window, minibuf_window))))
5f5c8ee5 15514 row->indicate_empty_line_p = 1;
a2889657 15515 }
2311178e 15516
5f5c8ee5
GM
15517 it->continuation_lines_width = 0;
15518 row->ends_at_zv_p = 1;
15519 break;
a2889657 15520 }
a2889657 15521
5f5c8ee5
GM
15522 /* Now, get the metrics of what we want to display. This also
15523 generates glyphs in `row' (which is IT->glyph_row). */
15524 n_glyphs_before = row->used[TEXT_AREA];
15525 x = it->current_x;
e6819faf
GM
15526
15527 /* Remember the line height so far in case the next element doesn't
15528 fit on the line. */
15529 if (!it->truncate_lines_p)
15530 {
15531 ascent = it->max_ascent;
15532 descent = it->max_descent;
15533 phys_ascent = it->max_phys_ascent;
15534 phys_descent = it->max_phys_descent;
15535 }
2311178e 15536
5f5c8ee5 15537 PRODUCE_GLYPHS (it);
a2889657 15538
5f5c8ee5
GM
15539 /* If this display element was in marginal areas, continue with
15540 the next one. */
15541 if (it->area != TEXT_AREA)
a2889657 15542 {
5f5c8ee5
GM
15543 row->ascent = max (row->ascent, it->max_ascent);
15544 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
15545 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15546 row->phys_height = max (row->phys_height,
15547 it->max_phys_ascent + it->max_phys_descent);
546f98b8
KS
15548 row->extra_line_spacing = max (row->extra_line_spacing,
15549 it->max_extra_line_spacing);
cafafe0b 15550 set_iterator_to_next (it, 1);
5f5c8ee5
GM
15551 continue;
15552 }
5936754e 15553
5f5c8ee5
GM
15554 /* Does the display element fit on the line? If we truncate
15555 lines, we should draw past the right edge of the window. If
15556 we don't truncate, we want to stop so that we can display the
15557 continuation glyph before the right margin. If lines are
15558 continued, there are two possible strategies for characters
15559 resulting in more than 1 glyph (e.g. tabs): Display as many
15560 glyphs as possible in this line and leave the rest for the
15561 continuation line, or display the whole element in the next
15562 line. Original redisplay did the former, so we do it also. */
15563 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
15564 hpos_before = it->hpos;
15565 x_before = x;
4dcd74e6 15566
4b41cebb 15567 if (/* Not a newline. */
4dcd74e6 15568 nglyphs > 0
202c1b5b 15569 /* Glyphs produced fit entirely in the line. */
4dcd74e6
GM
15570 && it->current_x < it->last_visible_x)
15571 {
37be86f2 15572 it->hpos += nglyphs;
5f5c8ee5
GM
15573 row->ascent = max (row->ascent, it->max_ascent);
15574 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
15575 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15576 row->phys_height = max (row->phys_height,
15577 it->max_phys_ascent + it->max_phys_descent);
546f98b8
KS
15578 row->extra_line_spacing = max (row->extra_line_spacing,
15579 it->max_extra_line_spacing);
5f5c8ee5
GM
15580 if (it->current_x - it->pixel_width < it->first_visible_x)
15581 row->x = x - it->first_visible_x;
15582 }
15583 else
15584 {
15585 int new_x;
15586 struct glyph *glyph;
2311178e 15587
5f5c8ee5 15588 for (i = 0; i < nglyphs; ++i, x = new_x)
b5bbc9a5 15589 {
5f5c8ee5
GM
15590 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15591 new_x = x + glyph->pixel_width;
15592
15593 if (/* Lines are continued. */
15594 !it->truncate_lines_p
15595 && (/* Glyph doesn't fit on the line. */
15596 new_x > it->last_visible_x
15597 /* Or it fits exactly on a window system frame. */
15598 || (new_x == it->last_visible_x
15599 && FRAME_WINDOW_P (it->f))))
a2889657 15600 {
5f5c8ee5 15601 /* End of a continued line. */
2311178e 15602
5f5c8ee5
GM
15603 if (it->hpos == 0
15604 || (new_x == it->last_visible_x
15605 && FRAME_WINDOW_P (it->f)))
15606 {
e6819faf
GM
15607 /* Current glyph is the only one on the line or
15608 fits exactly on the line. We must continue
15609 the line because we can't draw the cursor
15610 after the glyph. */
5f5c8ee5
GM
15611 row->continued_p = 1;
15612 it->current_x = new_x;
15613 it->continuation_lines_width += new_x;
15614 ++it->hpos;
15615 if (i == nglyphs - 1)
88e6b646
KS
15616 {
15617 set_iterator_to_next (it, 1);
7af0e8d7 15618#ifdef HAVE_WINDOW_SYSTEM
88e6b646
KS
15619 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15620 {
6d925726
KS
15621 if (!get_next_display_element (it))
15622 {
15623 row->exact_window_width_line_p = 1;
15624 it->continuation_lines_width = 0;
15625 row->continued_p = 0;
15626 row->ends_at_zv_p = 1;
15627 }
15628 else if (ITERATOR_AT_END_OF_LINE_P (it))
88e6b646
KS
15629 {
15630 row->continued_p = 0;
15631 row->exact_window_width_line_p = 1;
15632 }
15633 }
7af0e8d7 15634#endif /* HAVE_WINDOW_SYSTEM */
88e6b646 15635 }
5f5c8ee5 15636 }
3b3c4bf0
GM
15637 else if (CHAR_GLYPH_PADDING_P (*glyph)
15638 && !FRAME_WINDOW_P (it->f))
15639 {
15640 /* A padding glyph that doesn't fit on this line.
15641 This means the whole character doesn't fit
15642 on the line. */
15643 row->used[TEXT_AREA] = n_glyphs_before;
2311178e 15644
3b3c4bf0
GM
15645 /* Fill the rest of the row with continuation
15646 glyphs like in 20.x. */
15647 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
15648 < row->glyphs[1 + TEXT_AREA])
15649 produce_special_glyphs (it, IT_CONTINUATION);
2311178e 15650
3b3c4bf0
GM
15651 row->continued_p = 1;
15652 it->current_x = x_before;
15653 it->continuation_lines_width += x_before;
2311178e 15654
3b3c4bf0
GM
15655 /* Restore the height to what it was before the
15656 element not fitting on the line. */
15657 it->max_ascent = ascent;
15658 it->max_descent = descent;
15659 it->max_phys_ascent = phys_ascent;
15660 it->max_phys_descent = phys_descent;
15661 }
0df56f4d
GM
15662 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
15663 {
15664 /* A TAB that extends past the right edge of the
15665 window. This produces a single glyph on
15666 window system frames. We leave the glyph in
15667 this row and let it fill the row, but don't
15668 consume the TAB. */
15669 it->continuation_lines_width += it->last_visible_x;
15670 row->ends_in_middle_of_char_p = 1;
15671 row->continued_p = 1;
15672 glyph->pixel_width = it->last_visible_x - x;
15673 it->starts_in_middle_of_char_p = 1;
15674 }
5f5c8ee5 15675 else
5936754e 15676 {
0df56f4d
GM
15677 /* Something other than a TAB that draws past
15678 the right edge of the window. Restore
15679 positions to values before the element. */
5f5c8ee5 15680 row->used[TEXT_AREA] = n_glyphs_before + i;
2311178e 15681
5f5c8ee5
GM
15682 /* Display continuation glyphs. */
15683 if (!FRAME_WINDOW_P (it->f))
15684 produce_special_glyphs (it, IT_CONTINUATION);
15685 row->continued_p = 1;
653c329b 15686
8a955545 15687 it->current_x = x_before;
0df56f4d 15688 it->continuation_lines_width += x;
8a955545 15689 extend_face_to_end_of_line (it);
2311178e 15690
91004049
GM
15691 if (nglyphs > 1 && i > 0)
15692 {
15693 row->ends_in_middle_of_char_p = 1;
15694 it->starts_in_middle_of_char_p = 1;
15695 }
2311178e 15696
e6819faf
GM
15697 /* Restore the height to what it was before the
15698 element not fitting on the line. */
15699 it->max_ascent = ascent;
15700 it->max_descent = descent;
15701 it->max_phys_ascent = phys_ascent;
15702 it->max_phys_descent = phys_descent;
5936754e 15703 }
e6819faf 15704
5f5c8ee5
GM
15705 break;
15706 }
15707 else if (new_x > it->first_visible_x)
15708 {
15709 /* Increment number of glyphs actually displayed. */
15710 ++it->hpos;
2311178e 15711
5f5c8ee5
GM
15712 if (x < it->first_visible_x)
15713 /* Glyph is partially visible, i.e. row starts at
15714 negative X position. */
15715 row->x = x - it->first_visible_x;
15716 }
15717 else
15718 {
15719 /* Glyph is completely off the left margin of the
15720 window. This should not happen because of the
72b8c434
RS
15721 move_it_in_display_line at the start of this
15722 function, unless the text display area of the
15723 window is empty. */
15724 xassert (it->first_visible_x <= it->last_visible_x);
a2889657 15725 }
a2889657 15726 }
2311178e 15727
5f5c8ee5
GM
15728 row->ascent = max (row->ascent, it->max_ascent);
15729 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
15730 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15731 row->phys_height = max (row->phys_height,
15732 it->max_phys_ascent + it->max_phys_descent);
546f98b8
KS
15733 row->extra_line_spacing = max (row->extra_line_spacing,
15734 it->max_extra_line_spacing);
2311178e 15735
5f5c8ee5 15736 /* End of this display line if row is continued. */
6d925726 15737 if (row->continued_p || row->ends_at_zv_p)
5f5c8ee5 15738 break;
a2889657 15739 }
a2889657 15740
88e6b646 15741 at_end_of_line:
5f5c8ee5
GM
15742 /* Is this a line end? If yes, we're also done, after making
15743 sure that a non-default face is extended up to the right
15744 margin of the window. */
15745 if (ITERATOR_AT_END_OF_LINE_P (it))
1c9241f5 15746 {
5f5c8ee5
GM
15747 int used_before = row->used[TEXT_AREA];
15748
e74fb0f7
GM
15749 row->ends_in_newline_from_string_p = STRINGP (it->object);
15750
7af0e8d7 15751#ifdef HAVE_WINDOW_SYSTEM
5f5c8ee5
GM
15752 /* Add a space at the end of the line that is used to
15753 display the cursor there. */
88e6b646 15754 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
4933c603 15755 append_space_for_newline (it, 0);
7af0e8d7 15756#endif /* HAVE_WINDOW_SYSTEM */
2311178e 15757
5f5c8ee5
GM
15758 /* Extend the face to the end of the line. */
15759 extend_face_to_end_of_line (it);
15760
15761 /* Make sure we have the position. */
15762 if (used_before == 0)
15763 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
2311178e 15764
5f5c8ee5 15765 /* Consume the line end. This skips over invisible lines. */
cafafe0b 15766 set_iterator_to_next (it, 1);
5f5c8ee5
GM
15767 it->continuation_lines_width = 0;
15768 break;
1c9241f5 15769 }
a2889657 15770
2311178e 15771 /* Proceed with next display element. Note that this skips
5f5c8ee5 15772 over lines invisible because of selective display. */
cafafe0b 15773 set_iterator_to_next (it, 1);
b1d1124b 15774
5f5c8ee5
GM
15775 /* If we truncate lines, we are done when the last displayed
15776 glyphs reach past the right margin of the window. */
15777 if (it->truncate_lines_p
15778 && (FRAME_WINDOW_P (it->f)
15779 ? (it->current_x >= it->last_visible_x)
15780 : (it->current_x > it->last_visible_x)))
75d13c64 15781 {
5f5c8ee5
GM
15782 /* Maybe add truncation glyphs. */
15783 if (!FRAME_WINDOW_P (it->f))
15784 {
a98b5ed9 15785 int i, n;
2311178e 15786
a98b5ed9
GM
15787 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15788 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15789 break;
15790
15791 for (n = row->used[TEXT_AREA]; i < n; ++i)
15792 {
15793 row->used[TEXT_AREA] = i;
15794 produce_special_glyphs (it, IT_TRUNCATION);
15795 }
5f5c8ee5 15796 }
7af0e8d7 15797#ifdef HAVE_WINDOW_SYSTEM
88e6b646
KS
15798 else
15799 {
15800 /* Don't truncate if we can overflow newline into fringe. */
15801 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15802 {
6d925726
KS
15803 if (!get_next_display_element (it))
15804 {
6d925726
KS
15805 it->continuation_lines_width = 0;
15806 row->ends_at_zv_p = 1;
15807 row->exact_window_width_line_p = 1;
15808 break;
6d925726 15809 }
88e6b646
KS
15810 if (ITERATOR_AT_END_OF_LINE_P (it))
15811 {
15812 row->exact_window_width_line_p = 1;
15813 goto at_end_of_line;
15814 }
15815 }
15816 }
7af0e8d7 15817#endif /* HAVE_WINDOW_SYSTEM */
2311178e 15818
5f5c8ee5
GM
15819 row->truncated_on_right_p = 1;
15820 it->continuation_lines_width = 0;
312246d1 15821 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
15822 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
15823 it->hpos = hpos_before;
15824 it->current_x = x_before;
15825 break;
75d13c64 15826 }
a2889657 15827 }
a2889657 15828
5f5c8ee5
GM
15829 /* If line is not empty and hscrolled, maybe insert truncation glyphs
15830 at the left window margin. */
15831 if (it->first_visible_x
15832 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
15833 {
15834 if (!FRAME_WINDOW_P (it->f))
15835 insert_left_trunc_glyphs (it);
15836 row->truncated_on_left_p = 1;
15837 }
a2889657 15838
5f5c8ee5
GM
15839 /* If the start of this line is the overlay arrow-position, then
15840 mark this glyph row as the one containing the overlay arrow.
15841 This is clearly a mess with variable size fonts. It would be
15842 better to let it be displayed like cursors under X. */
25bcb353
KS
15843 if ((row->displays_text_p || !overlay_arrow_seen)
15844 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
15845 !NILP (overlay_arrow_string)))
a2889657 15846 {
b46952ae 15847 /* Overlay arrow in window redisplay is a fringe bitmap. */
c514efbb 15848 if (STRINGP (overlay_arrow_string))
c4628384 15849 {
351b5434 15850 struct glyph_row *arrow_row
7d90b587 15851 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
5f5c8ee5
GM
15852 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15853 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15854 struct glyph *p = row->glyphs[TEXT_AREA];
15855 struct glyph *p2, *end;
15856
15857 /* Copy the arrow glyphs. */
15858 while (glyph < arrow_end)
15859 *p++ = *glyph++;
15860
15861 /* Throw away padding glyphs. */
15862 p2 = p;
15863 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15864 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15865 ++p2;
15866 if (p2 > p)
212e4f87 15867 {
5f5c8ee5
GM
15868 while (p2 < end)
15869 *p++ = *p2++;
15870 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
c4628384 15871 }
c4628384 15872 }
c514efbb
KS
15873 else
15874 {
25bcb353
KS
15875 xassert (INTEGERP (overlay_arrow_string));
15876 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
c514efbb 15877 }
a2889657
JB
15878 overlay_arrow_seen = 1;
15879 }
15880
5f5c8ee5
GM
15881 /* Compute pixel dimensions of this line. */
15882 compute_line_metrics (it);
15883
15884 /* Remember the position at which this line ends. */
15885 row->end = it->current;
15886
f323e507
RS
15887 /* Record whether this row ends inside an ellipsis. */
15888 row->ends_in_ellipsis_p
a6f82cc4 15889 = (it->method == GET_FROM_DISPLAY_VECTOR
f323e507
RS
15890 && it->ellipsis_p);
15891
6d925726
KS
15892 /* Save fringe bitmaps in this row. */
15893 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
15894 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
15895 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
15896 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
15897
15898 it->left_user_fringe_bitmap = 0;
15899 it->left_user_fringe_face_id = 0;
15900 it->right_user_fringe_bitmap = 0;
15901 it->right_user_fringe_face_id = 0;
15902
173cbca8 15903 /* Maybe set the cursor. */
5f5c8ee5
GM
15904 if (it->w->cursor.vpos < 0
15905 && PT >= MATRIX_ROW_START_CHARPOS (row)
cafafe0b
GM
15906 && PT <= MATRIX_ROW_END_CHARPOS (row)
15907 && cursor_row_p (it->w, row))
15908 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
5f5c8ee5
GM
15909
15910 /* Highlight trailing whitespace. */
8f897821 15911 if (!NILP (Vshow_trailing_whitespace))
5f5c8ee5
GM
15912 highlight_trailing_whitespace (it->f, it->glyph_row);
15913
15914 /* Prepare for the next line. This line starts horizontally at (X
15915 HPOS) = (0 0). Vertical positions are incremented. As a
15916 convenience for the caller, IT->glyph_row is set to the next
15917 row to be used. */
15918 it->current_x = it->hpos = 0;
15919 it->current_y += row->height;
15920 ++it->vpos;
15921 ++it->glyph_row;
29b3ea63 15922 it->start = it->current;
5f5c8ee5 15923 return row->displays_text_p;
a2889657 15924}
5f5c8ee5
GM
15925
15926
a2889657 15927\f
5f5c8ee5
GM
15928/***********************************************************************
15929 Menu Bar
15930 ***********************************************************************/
15931
15932/* Redisplay the menu bar in the frame for window W.
15933
15934 The menu bar of X frames that don't have X toolkit support is
15935 displayed in a special window W->frame->menu_bar_window.
2311178e 15936
5f5c8ee5
GM
15937 The menu bar of terminal frames is treated specially as far as
15938 glyph matrices are concerned. Menu bar lines are not part of
15939 windows, so the update is done directly on the frame matrix rows
15940 for the menu bar. */
7ce2c095
RS
15941
15942static void
15943display_menu_bar (w)
15944 struct window *w;
15945{
5f5c8ee5
GM
15946 struct frame *f = XFRAME (WINDOW_FRAME (w));
15947 struct it it;
15948 Lisp_Object items;
8351baf2 15949 int i;
7ce2c095 15950
5f5c8ee5 15951 /* Don't do all this for graphical frames. */
dc937613 15952#ifdef HAVE_NTGUI
d129c4c2
KH
15953 if (!NILP (Vwindow_system))
15954 return;
dc937613 15955#endif
488dd4c4 15956#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
d3413a53 15957 if (FRAME_X_P (f))
7ce2c095 15958 return;
5f5c8ee5 15959#endif
e0f712ba 15960#ifdef MAC_OS
1a578e9b
AC
15961 if (FRAME_MAC_P (f))
15962 return;
15963#endif
5f5c8ee5
GM
15964
15965#ifdef USE_X_TOOLKIT
15966 xassert (!FRAME_WINDOW_P (f));
52377a47 15967 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
5f5c8ee5 15968 it.first_visible_x = 0;
da8b7f4f 15969 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
5f5c8ee5
GM
15970#else /* not USE_X_TOOLKIT */
15971 if (FRAME_WINDOW_P (f))
15972 {
15973 /* Menu bar lines are displayed in the desired matrix of the
15974 dummy window menu_bar_window. */
15975 struct window *menu_w;
15976 xassert (WINDOWP (f->menu_bar_window));
15977 menu_w = XWINDOW (f->menu_bar_window);
15978 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
52377a47 15979 MENU_FACE_ID);
5f5c8ee5 15980 it.first_visible_x = 0;
da8b7f4f 15981 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
5f5c8ee5
GM
15982 }
15983 else
15984 {
15985 /* This is a TTY frame, i.e. character hpos/vpos are used as
15986 pixel x/y. */
15987 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
52377a47 15988 MENU_FACE_ID);
5f5c8ee5 15989 it.first_visible_x = 0;
da8b7f4f 15990 it.last_visible_x = FRAME_COLS (f);
5f5c8ee5
GM
15991 }
15992#endif /* not USE_X_TOOLKIT */
15993
1862a24e
MB
15994 if (! mode_line_inverse_video)
15995 /* Force the menu-bar to be displayed in the default face. */
15996 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15997
5f5c8ee5
GM
15998 /* Clear all rows of the menu bar. */
15999 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16000 {
16001 struct glyph_row *row = it.glyph_row + i;
16002 clear_glyph_row (row);
16003 row->enabled_p = 1;
16004 row->full_width_p = 1;
16005 }
7ce2c095 16006
5f5c8ee5
GM
16007 /* Display all items of the menu bar. */
16008 items = FRAME_MENU_BAR_ITEMS (it.f);
469937ac 16009 for (i = 0; i < XVECTOR (items)->size; i += 4)
7ce2c095 16010 {
5f5c8ee5
GM
16011 Lisp_Object string;
16012
16013 /* Stop at nil string. */
a61b7058 16014 string = AREF (items, i + 1);
8351baf2
RS
16015 if (NILP (string))
16016 break;
2d66ad19 16017
5f5c8ee5 16018 /* Remember where item was displayed. */
a61b7058 16019 AREF (items, i + 3) = make_number (it.hpos);
7ce2c095 16020
5f5c8ee5
GM
16021 /* Display the item, pad with one space. */
16022 if (it.current_x < it.last_visible_x)
16023 display_string (NULL, string, Qnil, 0, 0, &it,
2051c264 16024 SCHARS (string) + 1, 0, 0, -1);
7ce2c095
RS
16025 }
16026
2d66ad19 16027 /* Fill out the line with spaces. */
5f5c8ee5
GM
16028 if (it.current_x < it.last_visible_x)
16029 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
db6f348c 16030
5f5c8ee5
GM
16031 /* Compute the total height of the lines. */
16032 compute_line_metrics (&it);
7ce2c095 16033}
5f5c8ee5
GM
16034
16035
7ce2c095 16036\f
5f5c8ee5
GM
16037/***********************************************************************
16038 Mode Line
16039 ***********************************************************************/
16040
715e84c9
GM
16041/* Redisplay mode lines in the window tree whose root is WINDOW. If
16042 FORCE is non-zero, redisplay mode lines unconditionally.
16043 Otherwise, redisplay only mode lines that are garbaged. Value is
16044 the number of windows whose mode lines were redisplayed. */
a2889657 16045
715e84c9
GM
16046static int
16047redisplay_mode_lines (window, force)
16048 Lisp_Object window;
16049 int force;
16050{
16051 int nwindows = 0;
2311178e 16052
715e84c9
GM
16053 while (!NILP (window))
16054 {
16055 struct window *w = XWINDOW (window);
2311178e 16056
715e84c9
GM
16057 if (WINDOWP (w->hchild))
16058 nwindows += redisplay_mode_lines (w->hchild, force);
16059 else if (WINDOWP (w->vchild))
16060 nwindows += redisplay_mode_lines (w->vchild, force);
16061 else if (force
16062 || FRAME_GARBAGED_P (XFRAME (w->frame))
16063 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16064 {
715e84c9
GM
16065 struct text_pos lpoint;
16066 struct buffer *old = current_buffer;
16067
16068 /* Set the window's buffer for the mode line display. */
16069 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16070 set_buffer_internal_1 (XBUFFER (w->buffer));
2311178e 16071
715e84c9
GM
16072 /* Point refers normally to the selected window. For any
16073 other window, set up appropriate value. */
16074 if (!EQ (window, selected_window))
16075 {
16076 struct text_pos pt;
2311178e 16077
715e84c9
GM
16078 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16079 if (CHARPOS (pt) < BEGV)
16080 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16081 else if (CHARPOS (pt) > (ZV - 1))
16082 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16083 else
16084 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16085 }
16086
715e84c9
GM
16087 /* Display mode lines. */
16088 clear_glyph_matrix (w->desired_matrix);
16089 if (display_mode_lines (w))
16090 {
16091 ++nwindows;
16092 w->must_be_updated_p = 1;
16093 }
16094
16095 /* Restore old settings. */
715e84c9
GM
16096 set_buffer_internal_1 (old);
16097 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16098 }
16099
16100 window = w->next;
16101 }
16102
16103 return nwindows;
16104}
16105
16106
16107/* Display the mode and/or top line of window W. Value is the number
16108 of mode lines displayed. */
16109
16110static int
5f5c8ee5 16111display_mode_lines (w)
a2889657
JB
16112 struct window *w;
16113{
edd1e654 16114 Lisp_Object old_selected_window, old_selected_frame;
715e84c9 16115 int n = 0;
edd1e654
GM
16116
16117 old_selected_frame = selected_frame;
16118 selected_frame = w->frame;
16119 old_selected_window = selected_window;
16120 XSETWINDOW (selected_window, w);
2311178e 16121
5f5c8ee5 16122 /* These will be set while the mode line specs are processed. */
aa6d10fa 16123 line_number_displayed = 0;
155ef550 16124 w->column_number_displayed = Qnil;
aa6d10fa 16125
5f5c8ee5 16126 if (WINDOW_WANTS_MODELINE_P (w))
715e84c9 16127 {
c96e83bf 16128 struct window *sel_w = XWINDOW (old_selected_window);
f87c0a98 16129
96d2320f 16130 /* Select mode line face based on the real selected window. */
c96e83bf 16131 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
715e84c9
GM
16132 current_buffer->mode_line_format);
16133 ++n;
16134 }
2311178e 16135
045dee35 16136 if (WINDOW_WANTS_HEADER_LINE_P (w))
715e84c9
GM
16137 {
16138 display_mode_line (w, HEADER_LINE_FACE_ID,
16139 current_buffer->header_line_format);
16140 ++n;
16141 }
16142
edd1e654
GM
16143 selected_frame = old_selected_frame;
16144 selected_window = old_selected_window;
715e84c9 16145 return n;
5f5c8ee5 16146}
03b294dc 16147
03b294dc 16148
5f5c8ee5 16149/* Display mode or top line of window W. FACE_ID specifies which line
045dee35 16150 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
04612a64
GM
16151 FORMAT is the mode line format to display. Value is the pixel
16152 height of the mode line displayed. */
03b294dc 16153
04612a64 16154static int
5f5c8ee5
GM
16155display_mode_line (w, face_id, format)
16156 struct window *w;
16157 enum face_id face_id;
16158 Lisp_Object format;
16159{
16160 struct it it;
16161 struct face *face;
53088ce8 16162 int count = SPECPDL_INDEX ();
03b294dc 16163
5f5c8ee5
GM
16164 init_iterator (&it, w, -1, -1, NULL, face_id);
16165 prepare_desired_row (it.glyph_row);
16166
da06e4d7
KS
16167 it.glyph_row->mode_line_p = 1;
16168
1862a24e
MB
16169 if (! mode_line_inverse_video)
16170 /* Force the mode-line to be displayed in the default face. */
16171 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16172
53088ce8 16173 record_unwind_protect (unwind_format_mode_line,
f5b8d5f2 16174 format_mode_line_unwind_data (NULL, 0));
53088ce8
KS
16175
16176 mode_line_target = MODE_LINE_DISPLAY;
16177
5f5c8ee5
GM
16178 /* Temporarily make frame's keyboard the current kboard so that
16179 kboard-local variables in the mode_line_format will get the right
16180 values. */
16181 push_frame_kboard (it.f);
c53a1624 16182 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
5f5c8ee5 16183 pop_frame_kboard ();
a2889657 16184
53088ce8
KS
16185 unbind_to (count, Qnil);
16186
5f5c8ee5
GM
16187 /* Fill up with spaces. */
16188 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
2311178e 16189
5f5c8ee5
GM
16190 compute_line_metrics (&it);
16191 it.glyph_row->full_width_p = 1;
5f5c8ee5
GM
16192 it.glyph_row->continued_p = 0;
16193 it.glyph_row->truncated_on_left_p = 0;
16194 it.glyph_row->truncated_on_right_p = 0;
16195
16196 /* Make a 3D mode-line have a shadow at its right end. */
16197 face = FACE_FROM_ID (it.f, face_id);
16198 extend_face_to_end_of_line (&it);
16199 if (face->box != FACE_NO_BOX)
d7eb09a0 16200 {
5f5c8ee5
GM
16201 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16202 + it.glyph_row->used[TEXT_AREA] - 1);
16203 last->right_box_line_p = 1;
d7eb09a0 16204 }
04612a64
GM
16205
16206 return it.glyph_row->height;
a2889657
JB
16207}
16208
f5b8d5f2
RS
16209/* Move element ELT in LIST to the front of LIST.
16210 Return the updated list. */
16211
16212static Lisp_Object
16213move_elt_to_front (elt, list)
16214 Lisp_Object elt, list;
16215{
16216 register Lisp_Object tail, prev;
16217 register Lisp_Object tem;
16218
16219 tail = list;
16220 prev = Qnil;
16221 while (CONSP (tail))
16222 {
16223 tem = XCAR (tail);
8a955545 16224
f5b8d5f2
RS
16225 if (EQ (elt, tem))
16226 {
16227 /* Splice out the link TAIL. */
16228 if (NILP (prev))
16229 list = XCDR (tail);
16230 else
16231 Fsetcdr (prev, XCDR (tail));
16232
16233 /* Now make it the first. */
16234 Fsetcdr (tail, list);
16235 return tail;
16236 }
16237 else
16238 prev = tail;
16239 tail = XCDR (tail);
16240 QUIT;
16241 }
16242
16243 /* Not found--return unchanged LIST. */
16244 return list;
16245}
16246
5f5c8ee5
GM
16247/* Contribute ELT to the mode line for window IT->w. How it
16248 translates into text depends on its data type.
a2889657 16249
5f5c8ee5 16250 IT describes the display environment in which we display, as usual.
a2889657
JB
16251
16252 DEPTH is the depth in recursion. It is used to prevent
16253 infinite recursion here.
16254
5f5c8ee5
GM
16255 FIELD_WIDTH is the number of characters the display of ELT should
16256 occupy in the mode line, and PRECISION is the maximum number of
16257 characters to display from ELT's representation. See
b8523839 16258 display_string for details.
a2889657 16259
c53a1624
RS
16260 Returns the hpos of the end of the text generated by ELT.
16261
16262 PROPS is a property list to add to any string we encounter.
16263
7d0393cf 16264 If RISKY is nonzero, remove (disregard) any properties in any string
93da8435
SM
16265 we encounter, and ignore :eval and :propertize.
16266
4c0e4b80
KS
16267 The global variable `mode_line_target' determines whether the
16268 output is passed to `store_mode_line_noprop',
16269 `store_mode_line_string', or `display_string'. */
a2889657
JB
16270
16271static int
c53a1624 16272display_mode_element (it, depth, field_width, precision, elt, props, risky)
5f5c8ee5 16273 struct it *it;
a2889657 16274 int depth;
5f5c8ee5 16275 int field_width, precision;
0fcf414f 16276 Lisp_Object elt, props;
c53a1624 16277 int risky;
a2889657 16278{
5f5c8ee5 16279 int n = 0, field, prec;
0fcf414f 16280 int literal = 0;
5f5c8ee5 16281
a2889657 16282 tail_recurse:
1a89be1e
SM
16283 if (depth > 100)
16284 elt = build_string ("*too-deep*");
a2889657
JB
16285
16286 depth++;
16287
0220c518 16288 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
a2889657
JB
16289 {
16290 case Lisp_String:
16291 {
16292 /* A string: output it and check for %-constructs within it. */
5f5c8ee5 16293 unsigned char c;
025568d9 16294 int offset = 0;
5f5c8ee5 16295
f5b8d5f2
RS
16296 if (SCHARS (elt) > 0
16297 && (!NILP (props) || risky))
0fcf414f
RS
16298 {
16299 Lisp_Object oprops, aelt;
16300 oprops = Ftext_properties_at (make_number (0), elt);
c53a1624 16301
3df5f8aa
RS
16302 /* If the starting string's properties are not what
16303 we want, translate the string. Also, if the string
16304 is risky, do that anyway. */
16305
c53a1624 16306 if (NILP (Fequal (props, oprops)) || risky)
0fcf414f 16307 {
ae02e06a
RS
16308 /* If the starting string has properties,
16309 merge the specified ones onto the existing ones. */
c53a1624 16310 if (! NILP (oprops) && !risky)
ae02e06a
RS
16311 {
16312 Lisp_Object tem;
16313
16314 oprops = Fcopy_sequence (oprops);
16315 tem = props;
16316 while (CONSP (tem))
16317 {
16318 oprops = Fplist_put (oprops, XCAR (tem),
16319 XCAR (XCDR (tem)));
16320 tem = XCDR (XCDR (tem));
16321 }
16322 props = oprops;
16323 }
16324
0fcf414f
RS
16325 aelt = Fassoc (elt, mode_line_proptrans_alist);
16326 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
adb63af1 16327 {
f5b8d5f2
RS
16328 /* AELT is what we want. Move it to the front
16329 without consing. */
adb63af1 16330 elt = XCAR (aelt);
f5b8d5f2
RS
16331 mode_line_proptrans_alist
16332 = move_elt_to_front (aelt, mode_line_proptrans_alist);
adb63af1 16333 }
0fcf414f
RS
16334 else
16335 {
adb63af1
RS
16336 Lisp_Object tem;
16337
f5b8d5f2
RS
16338 /* If AELT has the wrong props, it is useless.
16339 so get rid of it. */
16340 if (! NILP (aelt))
16341 mode_line_proptrans_alist
16342 = Fdelq (aelt, mode_line_proptrans_alist);
16343
0fcf414f 16344 elt = Fcopy_sequence (elt);
dc3b2c8b
SM
16345 Fset_text_properties (make_number (0), Flength (elt),
16346 props, elt);
adb63af1 16347 /* Add this item to mode_line_proptrans_alist. */
0fcf414f
RS
16348 mode_line_proptrans_alist
16349 = Fcons (Fcons (elt, props),
16350 mode_line_proptrans_alist);
adb63af1
RS
16351 /* Truncate mode_line_proptrans_alist
16352 to at most 50 elements. */
16353 tem = Fnthcdr (make_number (50),
16354 mode_line_proptrans_alist);
16355 if (! NILP (tem))
16356 XSETCDR (tem, Qnil);
0fcf414f
RS
16357 }
16358 }
16359 }
16360
025568d9 16361 offset = 0;
ae02e06a 16362
0fcf414f
RS
16363 if (literal)
16364 {
16365 prec = precision - n;
4c0e4b80
KS
16366 switch (mode_line_target)
16367 {
16368 case MODE_LINE_NOPROP:
16369 case MODE_LINE_TITLE:
16370 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16371 break;
16372 case MODE_LINE_STRING:
16373 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16374 break;
16375 case MODE_LINE_DISPLAY:
16376 n += display_string (NULL, elt, Qnil, 0, 0, it,
16377 0, prec, 0, STRING_MULTIBYTE (elt));
16378 break;
16379 }
0fcf414f
RS
16380
16381 break;
16382 }
16383
025568d9
RS
16384 /* Handle the non-literal case. */
16385
5f5c8ee5 16386 while ((precision <= 0 || n < precision)
025568d9 16387 && SREF (elt, offset) != 0
4c0e4b80 16388 && (mode_line_target != MODE_LINE_DISPLAY
5f5c8ee5 16389 || it->current_x < it->last_visible_x))
a2889657 16390 {
025568d9 16391 int last_offset = offset;
5f5c8ee5
GM
16392
16393 /* Advance to end of string or next format specifier. */
025568d9 16394 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
a2889657 16395 ;
2311178e 16396
025568d9 16397 if (offset - 1 != last_offset)
a2889657 16398 {
7a6b6cce
KS
16399 int nchars, nbytes;
16400
5f5c8ee5
GM
16401 /* Output to end of string or up to '%'. Field width
16402 is length of string. Don't output more than
16403 PRECISION allows us. */
025568d9 16404 offset--;
eaaa65b0 16405
025568d9
RS
16406 prec = c_string_width (SDATA (elt) + last_offset,
16407 offset - last_offset, precision - n,
7a6b6cce 16408 &nchars, &nbytes);
2311178e 16409
4c0e4b80 16410 switch (mode_line_target)
eaaa65b0 16411 {
4c0e4b80
KS
16412 case MODE_LINE_NOPROP:
16413 case MODE_LINE_TITLE:
025568d9 16414 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
4c0e4b80
KS
16415 break;
16416 case MODE_LINE_STRING:
16417 {
025568d9 16418 int bytepos = last_offset;
4c0e4b80
KS
16419 int charpos = string_byte_to_char (elt, bytepos);
16420 int endpos = (precision <= 0
025568d9 16421 ? string_byte_to_char (elt, offset)
4c0e4b80
KS
16422 : charpos + nchars);
16423
16424 n += store_mode_line_string (NULL,
16425 Fsubstring (elt, make_number (charpos),
16426 make_number (endpos)),
16427 0, 0, 0, Qnil);
16428 }
16429 break;
16430 case MODE_LINE_DISPLAY:
16431 {
025568d9 16432 int bytepos = last_offset;
4c0e4b80
KS
16433 int charpos = string_byte_to_char (elt, bytepos);
16434 n += display_string (NULL, elt, Qnil, 0, charpos,
16435 it, 0, prec, 0,
16436 STRING_MULTIBYTE (elt));
16437 }
16438 break;
eaaa65b0 16439 }
a2889657
JB
16440 }
16441 else /* c == '%' */
16442 {
025568d9 16443 int percent_position = offset;
2311178e 16444
5f5c8ee5
GM
16445 /* Get the specified minimum width. Zero means
16446 don't pad. */
16447 field = 0;
025568d9 16448 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
5f5c8ee5 16449 field = field * 10 + c - '0';
a2889657 16450
5f5c8ee5
GM
16451 /* Don't pad beyond the total padding allowed. */
16452 if (field_width - n > 0 && field > field_width - n)
16453 field = field_width - n;
a2889657 16454
5f5c8ee5
GM
16455 /* Note that either PRECISION <= 0 or N < PRECISION. */
16456 prec = precision - n;
2311178e 16457
a2889657 16458 if (c == 'M')
5f5c8ee5 16459 n += display_mode_element (it, depth, field, prec,
c53a1624
RS
16460 Vglobal_mode_string, props,
16461 risky);
a2889657 16462 else if (c != 0)
d39b6696 16463 {
72f62cb5 16464 int multibyte;
ae02e06a
RS
16465 int bytepos, charpos;
16466 unsigned char *spec;
2311178e 16467
025568d9 16468 bytepos = percent_position;
ae02e06a
RS
16469 charpos = (STRING_MULTIBYTE (elt)
16470 ? string_byte_to_char (elt, bytepos)
16471 : bytepos);
16472
16473 spec
72f62cb5
GM
16474 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16475
4c0e4b80 16476 switch (mode_line_target)
5f5c8ee5 16477 {
4c0e4b80
KS
16478 case MODE_LINE_NOPROP:
16479 case MODE_LINE_TITLE:
16480 n += store_mode_line_noprop (spec, field, prec);
16481 break;
16482 case MODE_LINE_STRING:
16483 {
16484 int len = strlen (spec);
16485 Lisp_Object tem = make_string (spec, len);
16486 props = Ftext_properties_at (make_number (charpos), elt);
16487 /* Should only keep face property in props */
16488 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
16489 }
16490 break;
16491 case MODE_LINE_DISPLAY:
16492 {
16493 int nglyphs_before, nwritten;
16494
16495 nglyphs_before = it->glyph_row->used[TEXT_AREA];
16496 nwritten = display_string (spec, Qnil, elt,
16497 charpos, 0, it,
16498 field, prec, 0,
16499 multibyte);
16500
16501 /* Assign to the glyphs written above the
16502 string where the `%x' came from, position
16503 of the `%'. */
16504 if (nwritten > 0)
16505 {
16506 struct glyph *glyph
16507 = (it->glyph_row->glyphs[TEXT_AREA]
16508 + nglyphs_before);
16509 int i;
16510
16511 for (i = 0; i < nwritten; ++i)
16512 {
16513 glyph[i].object = elt;
16514 glyph[i].charpos = charpos;
16515 }
16516
16517 n += nwritten;
16518 }
16519 }
16520 break;
5f5c8ee5 16521 }
d39b6696 16522 }
b8523839
AS
16523 else /* c == 0 */
16524 break;
a2889657
JB
16525 }
16526 }
16527 }
16528 break;
16529
16530 case Lisp_Symbol:
16531 /* A symbol: process the value of the symbol recursively
16532 as if it appeared here directly. Avoid error if symbol void.
16533 Special case: if value of symbol is a string, output the string
16534 literally. */
16535 {
16536 register Lisp_Object tem;
c53a1624
RS
16537
16538 /* If the variable is not marked as risky to set
16539 then its contents are risky to use. */
16540 if (NILP (Fget (elt, Qrisky_local_variable)))
16541 risky = 1;
16542
a2889657 16543 tem = Fboundp (elt);
265a9e55 16544 if (!NILP (tem))
a2889657
JB
16545 {
16546 tem = Fsymbol_value (elt);
16547 /* If value is a string, output that string literally:
16548 don't check for % within it. */
e24c997d 16549 if (STRINGP (tem))
0fcf414f
RS
16550 literal = 1;
16551
16552 if (!EQ (tem, elt))
5f5c8ee5
GM
16553 {
16554 /* Give up right away for nil or t. */
16555 elt = tem;
16556 goto tail_recurse;
16557 }
a2889657
JB
16558 }
16559 }
16560 break;
16561
16562 case Lisp_Cons:
16563 {
16564 register Lisp_Object car, tem;
16565
0fcf414f
RS
16566 /* A cons cell: five distinct cases.
16567 If first element is :eval or :propertize, do something special.
a2889657
JB
16568 If first element is a string or a cons, process all the elements
16569 and effectively concatenate them.
16570 If first element is a negative number, truncate displaying cdr to
16571 at most that many characters. If positive, pad (with spaces)
16572 to at least that many characters.
16573 If first element is a symbol, process the cadr or caddr recursively
16574 according to whether the symbol's value is non-nil or nil. */
9472f927 16575 car = XCAR (elt);
0fcf414f 16576 if (EQ (car, QCeval))
5f5c8ee5
GM
16577 {
16578 /* An element of the form (:eval FORM) means evaluate FORM
16579 and use the result as mode line elements. */
0fcf414f 16580
c53a1624
RS
16581 if (risky)
16582 break;
16583
0fcf414f
RS
16584 if (CONSP (XCDR (elt)))
16585 {
16586 Lisp_Object spec;
16587 spec = safe_eval (XCAR (XCDR (elt)));
16588 n += display_mode_element (it, depth, field_width - n,
c53a1624
RS
16589 precision - n, spec, props,
16590 risky);
0fcf414f
RS
16591 }
16592 }
16593 else if (EQ (car, QCpropertize))
16594 {
c53a1624
RS
16595 /* An element of the form (:propertize ELT PROPS...)
16596 means display ELT but applying properties PROPS. */
16597
16598 if (risky)
16599 break;
16600
0fcf414f 16601 if (CONSP (XCDR (elt)))
c53a1624
RS
16602 n += display_mode_element (it, depth, field_width - n,
16603 precision - n, XCAR (XCDR (elt)),
16604 XCDR (XCDR (elt)), risky);
5f5c8ee5
GM
16605 }
16606 else if (SYMBOLP (car))
a2889657
JB
16607 {
16608 tem = Fboundp (car);
9472f927 16609 elt = XCDR (elt);
e24c997d 16610 if (!CONSP (elt))
a2889657
JB
16611 goto invalid;
16612 /* elt is now the cdr, and we know it is a cons cell.
16613 Use its car if CAR has a non-nil value. */
265a9e55 16614 if (!NILP (tem))
a2889657
JB
16615 {
16616 tem = Fsymbol_value (car);
265a9e55 16617 if (!NILP (tem))
9472f927
GM
16618 {
16619 elt = XCAR (elt);
16620 goto tail_recurse;
16621 }
a2889657
JB
16622 }
16623 /* Symbol's value is nil (or symbol is unbound)
16624 Get the cddr of the original list
16625 and if possible find the caddr and use that. */
9472f927 16626 elt = XCDR (elt);
265a9e55 16627 if (NILP (elt))
a2889657 16628 break;
e24c997d 16629 else if (!CONSP (elt))
a2889657 16630 goto invalid;
9472f927 16631 elt = XCAR (elt);
a2889657
JB
16632 goto tail_recurse;
16633 }
e24c997d 16634 else if (INTEGERP (car))
a2889657
JB
16635 {
16636 register int lim = XINT (car);
9472f927 16637 elt = XCDR (elt);
a2889657 16638 if (lim < 0)
5f5c8ee5
GM
16639 {
16640 /* Negative int means reduce maximum width. */
16641 if (precision <= 0)
16642 precision = -lim;
16643 else
16644 precision = min (precision, -lim);
16645 }
a2889657
JB
16646 else if (lim > 0)
16647 {
16648 /* Padding specified. Don't let it be more than
16649 current maximum. */
5f5c8ee5
GM
16650 if (precision > 0)
16651 lim = min (precision, lim);
16652
a2889657
JB
16653 /* If that's more padding than already wanted, queue it.
16654 But don't reduce padding already specified even if
16655 that is beyond the current truncation point. */
5f5c8ee5 16656 field_width = max (lim, field_width);
a2889657
JB
16657 }
16658 goto tail_recurse;
16659 }
e24c997d 16660 else if (STRINGP (car) || CONSP (car))
a2889657
JB
16661 {
16662 register int limit = 50;
5f5c8ee5
GM
16663 /* Limit is to protect against circular lists. */
16664 while (CONSP (elt)
16665 && --limit > 0
16666 && (precision <= 0 || n < precision))
a2889657 16667 {
5c213fcc
RS
16668 n += display_mode_element (it, depth,
16669 /* Do padding only after the last
16670 element in the list. */
16671 (! CONSP (XCDR (elt))
16672 ? field_width - n
16673 : 0),
c53a1624
RS
16674 precision - n, XCAR (elt),
16675 props, risky);
9472f927 16676 elt = XCDR (elt);
a2889657
JB
16677 }
16678 }
16679 }
16680 break;
16681
16682 default:
16683 invalid:
1a89be1e
SM
16684 elt = build_string ("*invalid*");
16685 goto tail_recurse;
a2889657
JB
16686 }
16687
5f5c8ee5
GM
16688 /* Pad to FIELD_WIDTH. */
16689 if (field_width > 0 && n < field_width)
16690 {
4c0e4b80
KS
16691 switch (mode_line_target)
16692 {
16693 case MODE_LINE_NOPROP:
16694 case MODE_LINE_TITLE:
16695 n += store_mode_line_noprop ("", field_width - n, 0);
16696 break;
16697 case MODE_LINE_STRING:
16698 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
16699 break;
16700 case MODE_LINE_DISPLAY:
16701 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
16702 0, 0, 0);
16703 break;
16704 }
5f5c8ee5 16705 }
2311178e 16706
5f5c8ee5 16707 return n;
a2889657 16708}
5f5c8ee5 16709
fec8f23e
KS
16710/* Store a mode-line string element in mode_line_string_list.
16711
16712 If STRING is non-null, display that C string. Otherwise, the Lisp
16713 string LISP_STRING is displayed.
16714
16715 FIELD_WIDTH is the minimum number of output glyphs to produce.
16716 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16717 with spaces. FIELD_WIDTH <= 0 means don't pad.
16718
16719 PRECISION is the maximum number of characters to output from
16720 STRING. PRECISION <= 0 means don't truncate the string.
16721
16722 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
16723 properties to the string.
16724
7d0393cf 16725 PROPS are the properties to add to the string.
fec8f23e
KS
16726 The mode_line_string_face face property is always added to the string.
16727 */
16728
70567d1c
RS
16729static int
16730store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
fec8f23e
KS
16731 char *string;
16732 Lisp_Object lisp_string;
16733 int copy_string;
16734 int field_width;
16735 int precision;
16736 Lisp_Object props;
16737{
16738 int len;
16739 int n = 0;
16740
16741 if (string != NULL)
16742 {
16743 len = strlen (string);
16744 if (precision > 0 && len > precision)
16745 len = precision;
16746 lisp_string = make_string (string, len);
16747 if (NILP (props))
16748 props = mode_line_string_face_prop;
16749 else if (!NILP (mode_line_string_face))
16750 {
bccf59df 16751 Lisp_Object face = Fplist_get (props, Qface);
fec8f23e
KS
16752 props = Fcopy_sequence (props);
16753 if (NILP (face))
16754 face = mode_line_string_face;
16755 else
16756 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16757 props = Fplist_put (props, Qface, face);
16758 }
16759 Fadd_text_properties (make_number (0), make_number (len),
16760 props, lisp_string);
16761 }
7d0393cf 16762 else
fec8f23e 16763 {
c8224325 16764 len = XFASTINT (Flength (lisp_string));
fec8f23e
KS
16765 if (precision > 0 && len > precision)
16766 {
16767 len = precision;
16768 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
16769 precision = -1;
16770 }
16771 if (!NILP (mode_line_string_face))
16772 {
16773 Lisp_Object face;
16774 if (NILP (props))
16775 props = Ftext_properties_at (make_number (0), lisp_string);
bccf59df 16776 face = Fplist_get (props, Qface);
fec8f23e
KS
16777 if (NILP (face))
16778 face = mode_line_string_face;
16779 else
16780 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16781 props = Fcons (Qface, Fcons (face, Qnil));
16782 if (copy_string)
16783 lisp_string = Fcopy_sequence (lisp_string);
16784 }
16785 if (!NILP (props))
16786 Fadd_text_properties (make_number (0), make_number (len),
16787 props, lisp_string);
16788 }
16789
16790 if (len > 0)
16791 {
16792 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
7d0393cf 16793 n += len;
fec8f23e
KS
16794 }
16795
16796 if (field_width > len)
16797 {
16798 field_width -= len;
16799 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
16800 if (!NILP (props))
16801 Fadd_text_properties (make_number (0), make_number (field_width),
16802 props, lisp_string);
16803 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
7d0393cf 16804 n += field_width;
fec8f23e
KS
16805 }
16806
16807 return n;
16808}
16809
5f5c8ee5 16810
8143e6ab 16811DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
8d3ef989
KS
16812 1, 4, 0,
16813 doc: /* Format a string out of a mode line format specification.
16814First arg FORMAT specifies the mode line format (see `mode-line-format'
16815for details) to use.
43facb76 16816
8d3ef989 16817Optional second arg FACE specifies the face property to put
43facb76
RS
16818on all characters for which no face is specified.
16819t means whatever face the window's mode line currently uses
16820\(either `mode-line' or `mode-line-inactive', depending).
8d3ef989
KS
16821nil means the default is no face property.
16822If FACE is an integer, the value string has no text properties.
16823
16824Optional third and fourth args WINDOW and BUFFER specify the window
16825and buffer to use as the context for the formatting (defaults
16826are the selected window and the window's buffer). */)
16827 (format, face, window, buffer)
16828 Lisp_Object format, face, window, buffer;
8143e6ab
KS
16829{
16830 struct it it;
8143e6ab
KS
16831 int len;
16832 struct window *w;
16833 struct buffer *old_buffer = NULL;
8d3ef989
KS
16834 int face_id = -1;
16835 int no_props = INTEGERP (face);
4c0e4b80
KS
16836 int count = SPECPDL_INDEX ();
16837 Lisp_Object str;
d06e3847 16838 int string_start = 0;
8143e6ab
KS
16839
16840 if (NILP (window))
16841 window = selected_window;
16842 CHECK_WINDOW (window);
16843 w = XWINDOW (window);
8143e6ab 16844
8d3ef989
KS
16845 if (NILP (buffer))
16846 buffer = w->buffer;
16847 CHECK_BUFFER (buffer);
16848
16849 if (NILP (format))
16850 return build_string ("");
16851
16852 if (no_props)
16853 face = Qnil;
43facb76
RS
16854
16855 if (!NILP (face))
16856 {
8d3ef989
KS
16857 if (EQ (face, Qt))
16858 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
16859 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
43facb76
RS
16860 }
16861
8d3ef989
KS
16862 if (face_id < 0)
16863 face_id = DEFAULT_FACE_ID;
5670531e
RS
16864
16865 if (XBUFFER (buffer) != current_buffer)
4c0e4b80
KS
16866 old_buffer = current_buffer;
16867
27f8fd99
RS
16868 /* Save things including mode_line_proptrans_alist,
16869 and set that to nil so that we don't alter the outer value. */
4c0e4b80 16870 record_unwind_protect (unwind_format_mode_line,
f5b8d5f2 16871 format_mode_line_unwind_data (old_buffer, 1));
27f8fd99 16872 mode_line_proptrans_alist = Qnil;
4c0e4b80
KS
16873
16874 if (old_buffer)
16875 set_buffer_internal_1 (XBUFFER (buffer));
8143e6ab 16876
fec8f23e 16877 init_iterator (&it, w, -1, -1, NULL, face_id);
8143e6ab 16878
4c0e4b80 16879 if (no_props)
fec8f23e 16880 {
4c0e4b80
KS
16881 mode_line_target = MODE_LINE_NOPROP;
16882 mode_line_string_face_prop = Qnil;
16883 mode_line_string_list = Qnil;
d06e3847 16884 string_start = MODE_LINE_NOPROP_LEN (0);
fec8f23e
KS
16885 }
16886 else
16887 {
4c0e4b80 16888 mode_line_target = MODE_LINE_STRING;
fec8f23e 16889 mode_line_string_list = Qnil;
4c0e4b80
KS
16890 mode_line_string_face = face;
16891 mode_line_string_face_prop
16892 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
fec8f23e 16893 }
8143e6ab
KS
16894
16895 push_frame_kboard (it.f);
16896 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16897 pop_frame_kboard ();
16898
4c0e4b80 16899 if (no_props)
fec8f23e 16900 {
d06e3847
KS
16901 len = MODE_LINE_NOPROP_LEN (string_start);
16902 str = make_string (mode_line_noprop_buf + string_start, len);
fec8f23e 16903 }
4c0e4b80 16904 else
8143e6ab 16905 {
4c0e4b80
KS
16906 mode_line_string_list = Fnreverse (mode_line_string_list);
16907 str = Fmapconcat (intern ("identity"), mode_line_string_list,
16908 make_string ("", 0));
8143e6ab
KS
16909 }
16910
4c0e4b80
KS
16911 unbind_to (count, Qnil);
16912 return str;
8143e6ab
KS
16913}
16914
766525bc
RS
16915/* Write a null-terminated, right justified decimal representation of
16916 the positive integer D to BUF using a minimal field width WIDTH. */
16917
16918static void
16919pint2str (buf, width, d)
16920 register char *buf;
16921 register int width;
16922 register int d;
16923{
16924 register char *p = buf;
2311178e 16925
766525bc 16926 if (d <= 0)
5f5c8ee5 16927 *p++ = '0';
766525bc 16928 else
5f5c8ee5 16929 {
766525bc 16930 while (d > 0)
5f5c8ee5 16931 {
766525bc
RS
16932 *p++ = d % 10 + '0';
16933 d /= 10;
5f5c8ee5
GM
16934 }
16935 }
2311178e 16936
5f5c8ee5
GM
16937 for (width -= (int) (p - buf); width > 0; --width)
16938 *p++ = ' ';
766525bc
RS
16939 *p-- = '\0';
16940 while (p > buf)
5f5c8ee5 16941 {
766525bc
RS
16942 d = *buf;
16943 *buf++ = *p;
16944 *p-- = d;
5f5c8ee5 16945 }
766525bc
RS
16946}
16947
525b8b09
LK
16948/* Write a null-terminated, right justified decimal and "human
16949 readable" representation of the nonnegative integer D to BUF using
16950 a minimal field width WIDTH. D should be smaller than 999.5e24. */
16951
16952static const char power_letter[] =
16953 {
16954 0, /* not used */
16955 'k', /* kilo */
16956 'M', /* mega */
16957 'G', /* giga */
16958 'T', /* tera */
16959 'P', /* peta */
16960 'E', /* exa */
16961 'Z', /* zetta */
16962 'Y' /* yotta */
16963 };
16964
16965static void
16966pint2hrstr (buf, width, d)
16967 char *buf;
16968 int width;
16969 int d;
16970{
16971 /* We aim to represent the nonnegative integer D as
16972 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
16973 int quotient = d;
16974 int remainder = 0;
16975 /* -1 means: do not use TENTHS. */
16976 int tenths = -1;
16977 int exponent = 0;
16978
16979 /* Length of QUOTIENT.TENTHS as a string. */
16980 int length;
16981
16982 char * psuffix;
16983 char * p;
16984
16985 if (1000 <= quotient)
16986 {
16987 /* Scale to the appropriate EXPONENT. */
16988 do
16989 {
16990 remainder = quotient % 1000;
16991 quotient /= 1000;
16992 exponent++;
16993 }
16994 while (1000 <= quotient);
16995
16996 /* Round to nearest and decide whether to use TENTHS or not. */
16997 if (quotient <= 9)
16998 {
16999 tenths = remainder / 100;
17000 if (50 <= remainder % 100)
0162fa4e
KS
17001 {
17002 if (tenths < 9)
17003 tenths++;
17004 else
17005 {
17006 quotient++;
17007 if (quotient == 10)
17008 tenths = -1;
17009 else
17010 tenths = 0;
17011 }
17012 }
525b8b09
LK
17013 }
17014 else
17015 if (500 <= remainder)
0162fa4e
KS
17016 {
17017 if (quotient < 999)
17018 quotient++;
17019 else
17020 {
17021 quotient = 1;
17022 exponent++;
17023 tenths = 0;
17024 }
17025 }
525b8b09
LK
17026 }
17027
17028 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17029 if (tenths == -1 && quotient <= 99)
17030 if (quotient <= 9)
17031 length = 1;
17032 else
17033 length = 2;
17034 else
17035 length = 3;
17036 p = psuffix = buf + max (width, length);
17037
17038 /* Print EXPONENT. */
17039 if (exponent)
17040 *psuffix++ = power_letter[exponent];
17041 *psuffix = '\0';
17042
17043 /* Print TENTHS. */
17044 if (tenths >= 0)
17045 {
17046 *--p = '0' + tenths;
17047 *--p = '.';
17048 }
17049
17050 /* Print QUOTIENT. */
17051 do
17052 {
17053 int digit = quotient % 10;
17054 *--p = '0' + digit;
17055 }
17056 while ((quotient /= 10) != 0);
17057
17058 /* Print leading spaces. */
17059 while (buf < p)
17060 *--p = ' ';
17061}
17062
5f5c8ee5 17063/* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
1c9241f5
KH
17064 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17065 type of CODING_SYSTEM. Return updated pointer into BUF. */
17066
6693a99a 17067static unsigned char invalid_eol_type[] = "(*invalid*)";
d24715e8 17068
1c9241f5
KH
17069static char *
17070decode_mode_spec_coding (coding_system, buf, eol_flag)
17071 Lisp_Object coding_system;
17072 register char *buf;
17073 int eol_flag;
17074{
1e1078d6 17075 Lisp_Object val;
916848d8 17076 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
50f80c2f 17077 const unsigned char *eol_str;
302f2b38
EZ
17078 int eol_str_len;
17079 /* The EOL conversion we are using. */
17080 Lisp_Object eoltype;
1e1078d6 17081
4a09dee0 17082 val = Fget (coding_system, Qcoding_system);
085536c2 17083 eoltype = Qnil;
1c9241f5 17084
4a09dee0 17085 if (!VECTORP (val)) /* Not yet decided. */
1c9241f5 17086 {
916848d8
RS
17087 if (multibyte)
17088 *buf++ = '-';
21e989e3 17089 if (eol_flag)
302f2b38 17090 eoltype = eol_mnemonic_undecided;
1e1078d6 17091 /* Don't mention EOL conversion if it isn't decided. */
1c9241f5
KH
17092 }
17093 else
17094 {
1e1078d6
RS
17095 Lisp_Object eolvalue;
17096
17097 eolvalue = Fget (coding_system, Qeol_type);
17098
916848d8 17099 if (multibyte)
a61b7058 17100 *buf++ = XFASTINT (AREF (val, 1));
916848d8 17101
1c9241f5
KH
17102 if (eol_flag)
17103 {
1e1078d6
RS
17104 /* The EOL conversion that is normal on this system. */
17105
17106 if (NILP (eolvalue)) /* Not yet decided. */
17107 eoltype = eol_mnemonic_undecided;
17108 else if (VECTORP (eolvalue)) /* Not yet decided. */
17109 eoltype = eol_mnemonic_undecided;
17110 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
17111 eoltype = (XFASTINT (eolvalue) == 0
17112 ? eol_mnemonic_unix
17113 : (XFASTINT (eolvalue) == 1
17114 ? eol_mnemonic_dos : eol_mnemonic_mac));
302f2b38
EZ
17115 }
17116 }
2311178e 17117
302f2b38
EZ
17118 if (eol_flag)
17119 {
17120 /* Mention the EOL conversion if it is not the usual one. */
17121 if (STRINGP (eoltype))
17122 {
2051c264
GM
17123 eol_str = SDATA (eoltype);
17124 eol_str_len = SBYTES (eoltype);
302f2b38 17125 }
f30b3499
KH
17126 else if (INTEGERP (eoltype)
17127 && CHAR_VALID_P (XINT (eoltype), 0))
17128 {
50f80c2f
KR
17129 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17130 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17131 eol_str = tmp;
f30b3499 17132 }
302f2b38
EZ
17133 else
17134 {
17135 eol_str = invalid_eol_type;
17136 eol_str_len = sizeof (invalid_eol_type) - 1;
1c9241f5 17137 }
f30b3499 17138 bcopy (eol_str, buf, eol_str_len);
302f2b38 17139 buf += eol_str_len;
1c9241f5 17140 }
302f2b38 17141
1c9241f5
KH
17142 return buf;
17143}
17144
a2889657 17145/* Return a string for the output of a mode line %-spec for window W,
5f5c8ee5
GM
17146 generated by character C. PRECISION >= 0 means don't return a
17147 string longer than that value. FIELD_WIDTH > 0 means pad the
72f62cb5 17148 string returned with spaces to that value. Return 1 in *MULTIBYTE
64b7122d
RS
17149 if the result is multibyte text.
17150
17151 Note we operate on the current buffer for most purposes,
17152 the exception being w->base_line_pos. */
a2889657 17153
11e82b76
JB
17154static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17155
a2889657 17156static char *
72f62cb5 17157decode_mode_spec (w, c, field_width, precision, multibyte)
a2889657 17158 struct window *w;
68c45bf0 17159 register int c;
5f5c8ee5 17160 int field_width, precision;
72f62cb5 17161 int *multibyte;
a2889657 17162{
0b67772d 17163 Lisp_Object obj;
5f5c8ee5
GM
17164 struct frame *f = XFRAME (WINDOW_FRAME (w));
17165 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
64b7122d 17166 struct buffer *b = current_buffer;
a2889657 17167
0b67772d 17168 obj = Qnil;
72f62cb5 17169 *multibyte = 0;
a2889657
JB
17170
17171 switch (c)
17172 {
1af9f229
RS
17173 case '*':
17174 if (!NILP (b->read_only))
17175 return "%";
17176 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17177 return "*";
17178 return "-";
17179
17180 case '+':
17181 /* This differs from %* only for a modified read-only buffer. */
17182 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17183 return "*";
17184 if (!NILP (b->read_only))
17185 return "%";
17186 return "-";
17187
17188 case '&':
17189 /* This differs from %* in ignoring read-only-ness. */
17190 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17191 return "*";
17192 return "-";
17193
17194 case '%':
17195 return "%";
17196
2311178e 17197 case '[':
1af9f229
RS
17198 {
17199 int i;
17200 char *p;
17201
17202 if (command_loop_level > 5)
17203 return "[[[... ";
17204 p = decode_mode_spec_buf;
17205 for (i = 0; i < command_loop_level; i++)
17206 *p++ = '[';
17207 *p = 0;
17208 return decode_mode_spec_buf;
17209 }
17210
2311178e 17211 case ']':
1af9f229
RS
17212 {
17213 int i;
17214 char *p;
17215
17216 if (command_loop_level > 5)
17217 return " ...]]]";
17218 p = decode_mode_spec_buf;
17219 for (i = 0; i < command_loop_level; i++)
17220 *p++ = ']';
17221 *p = 0;
17222 return decode_mode_spec_buf;
17223 }
17224
17225 case '-':
17226 {
1af9f229 17227 register int i;
5f5c8ee5
GM
17228
17229 /* Let lots_of_dashes be a string of infinite length. */
4c0e4b80
KS
17230 if (mode_line_target == MODE_LINE_NOPROP ||
17231 mode_line_target == MODE_LINE_STRING)
fec8f23e 17232 return "--";
5f5c8ee5
GM
17233 if (field_width <= 0
17234 || field_width > sizeof (lots_of_dashes))
1af9f229 17235 {
5f5c8ee5
GM
17236 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17237 decode_mode_spec_buf[i] = '-';
17238 decode_mode_spec_buf[i] = '\0';
17239 return decode_mode_spec_buf;
1af9f229 17240 }
5f5c8ee5
GM
17241 else
17242 return lots_of_dashes;
1af9f229
RS
17243 }
17244
2311178e 17245 case 'b':
d39b6696 17246 obj = b->name;
a2889657
JB
17247 break;
17248
1af9f229
RS
17249 case 'c':
17250 {
2311178e 17251 int col = (int) current_column (); /* iftc */
ac90c44f 17252 w->column_number_displayed = make_number (col);
5f5c8ee5 17253 pint2str (decode_mode_spec_buf, field_width, col);
1af9f229
RS
17254 return decode_mode_spec_buf;
17255 }
17256
fbfde322
RS
17257 case 'e':
17258#ifndef SYSTEM_MALLOC
17259 {
f5b8d5f2 17260 if (NILP (Vmemory_full))
fbfde322
RS
17261 return "";
17262 else
17263 return "!MEM FULL! ";
17264 }
17265#else
17266 return "";
17267#endif
17268
1af9f229
RS
17269 case 'F':
17270 /* %F displays the frame name. */
5f5c8ee5 17271 if (!NILP (f->title))
2051c264 17272 return (char *) SDATA (f->title);
fd8ff63d 17273 if (f->explicit_name || ! FRAME_WINDOW_P (f))
2051c264 17274 return (char *) SDATA (f->name);
9c6da96f 17275 return "Emacs";
1af9f229 17276
2311178e 17277 case 'f':
d39b6696 17278 obj = b->filename;
a2889657
JB
17279 break;
17280
525b8b09
LK
17281 case 'i':
17282 {
17283 int size = ZV - BEGV;
17284 pint2str (decode_mode_spec_buf, field_width, size);
17285 return decode_mode_spec_buf;
17286 }
17287
17288 case 'I':
17289 {
17290 int size = ZV - BEGV;
17291 pint2hrstr (decode_mode_spec_buf, field_width, size);
17292 return decode_mode_spec_buf;
17293 }
17294
aa6d10fa
RS
17295 case 'l':
17296 {
12adba34
RS
17297 int startpos = XMARKER (w->start)->charpos;
17298 int startpos_byte = marker_byte_position (w->start);
17299 int line, linepos, linepos_byte, topline;
aa6d10fa 17300 int nlines, junk;
da8b7f4f 17301 int height = WINDOW_TOTAL_LINES (w);
aa6d10fa 17302
2311178e 17303 /* If we decided that this buffer isn't suitable for line numbers,
aa6d10fa
RS
17304 don't forget that too fast. */
17305 if (EQ (w->base_line_pos, w->buffer))
766525bc 17306 goto no_value;
5300fd39
RS
17307 /* But do forget it, if the window shows a different buffer now. */
17308 else if (BUFFERP (w->base_line_pos))
17309 w->base_line_pos = Qnil;
aa6d10fa
RS
17310
17311 /* If the buffer is very big, don't waste time. */
37d034d3 17312 if (INTEGERP (Vline_number_display_limit)
090703f4 17313 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
aa6d10fa
RS
17314 {
17315 w->base_line_pos = Qnil;
17316 w->base_line_number = Qnil;
766525bc 17317 goto no_value;
aa6d10fa
RS
17318 }
17319
17320 if (!NILP (w->base_line_number)
17321 && !NILP (w->base_line_pos)
12adba34 17322 && XFASTINT (w->base_line_pos) <= startpos)
aa6d10fa
RS
17323 {
17324 line = XFASTINT (w->base_line_number);
17325 linepos = XFASTINT (w->base_line_pos);
12adba34 17326 linepos_byte = buf_charpos_to_bytepos (b, linepos);
aa6d10fa
RS
17327 }
17328 else
17329 {
17330 line = 1;
d39b6696 17331 linepos = BUF_BEGV (b);
12adba34 17332 linepos_byte = BUF_BEGV_BYTE (b);
aa6d10fa
RS
17333 }
17334
17335 /* Count lines from base line to window start position. */
12adba34
RS
17336 nlines = display_count_lines (linepos, linepos_byte,
17337 startpos_byte,
17338 startpos, &junk);
aa6d10fa
RS
17339
17340 topline = nlines + line;
17341
17342 /* Determine a new base line, if the old one is too close
17343 or too far away, or if we did not have one.
17344 "Too close" means it's plausible a scroll-down would
17345 go back past it. */
d39b6696 17346 if (startpos == BUF_BEGV (b))
aa6d10fa 17347 {
ac90c44f
GM
17348 w->base_line_number = make_number (topline);
17349 w->base_line_pos = make_number (BUF_BEGV (b));
aa6d10fa
RS
17350 }
17351 else if (nlines < height + 25 || nlines > height * 3 + 50
d39b6696 17352 || linepos == BUF_BEGV (b))
aa6d10fa 17353 {
d39b6696 17354 int limit = BUF_BEGV (b);
12adba34 17355 int limit_byte = BUF_BEGV_BYTE (b);
aa6d10fa 17356 int position;
5d121aec 17357 int distance = (height * 2 + 30) * line_number_display_limit_width;
aa6d10fa
RS
17358
17359 if (startpos - distance > limit)
12adba34
RS
17360 {
17361 limit = startpos - distance;
17362 limit_byte = CHAR_TO_BYTE (limit);
17363 }
aa6d10fa 17364
12adba34
RS
17365 nlines = display_count_lines (startpos, startpos_byte,
17366 limit_byte,
17367 - (height * 2 + 30),
aa6d10fa 17368 &position);
2311178e 17369 /* If we couldn't find the lines we wanted within
5d121aec 17370 line_number_display_limit_width chars per line,
aa6d10fa 17371 give up on line numbers for this window. */
12adba34 17372 if (position == limit_byte && limit == startpos - distance)
aa6d10fa
RS
17373 {
17374 w->base_line_pos = w->buffer;
17375 w->base_line_number = Qnil;
766525bc 17376 goto no_value;
aa6d10fa
RS
17377 }
17378
ac90c44f
GM
17379 w->base_line_number = make_number (topline - nlines);
17380 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
aa6d10fa
RS
17381 }
17382
17383 /* Now count lines from the start pos to point. */
12adba34
RS
17384 nlines = display_count_lines (startpos, startpos_byte,
17385 PT_BYTE, PT, &junk);
aa6d10fa
RS
17386
17387 /* Record that we did display the line number. */
17388 line_number_displayed = 1;
17389
17390 /* Make the string to show. */
5f5c8ee5 17391 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
aa6d10fa 17392 return decode_mode_spec_buf;
766525bc
RS
17393 no_value:
17394 {
17395 char* p = decode_mode_spec_buf;
5f5c8ee5
GM
17396 int pad = field_width - 2;
17397 while (pad-- > 0)
17398 *p++ = ' ';
17399 *p++ = '?';
b357b9d4
KR
17400 *p++ = '?';
17401 *p = '\0';
766525bc
RS
17402 return decode_mode_spec_buf;
17403 }
aa6d10fa
RS
17404 }
17405 break;
17406
2311178e 17407 case 'm':
d39b6696 17408 obj = b->mode_name;
a2889657
JB
17409 break;
17410
17411 case 'n':
d39b6696 17412 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
a2889657
JB
17413 return " Narrow";
17414 break;
17415
a2889657
JB
17416 case 'p':
17417 {
17418 int pos = marker_position (w->start);
d39b6696 17419 int total = BUF_ZV (b) - BUF_BEGV (b);
a2889657 17420
d39b6696 17421 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
a2889657 17422 {
d39b6696 17423 if (pos <= BUF_BEGV (b))
a2889657
JB
17424 return "All";
17425 else
17426 return "Bottom";
17427 }
d39b6696 17428 else if (pos <= BUF_BEGV (b))
a2889657
JB
17429 return "Top";
17430 else
17431 {
3c7d31b9
RS
17432 if (total > 1000000)
17433 /* Do it differently for a large value, to avoid overflow. */
17434 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17435 else
17436 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
a2889657
JB
17437 /* We can't normally display a 3-digit number,
17438 so get us a 2-digit number that is close. */
17439 if (total == 100)
17440 total = 99;
17441 sprintf (decode_mode_spec_buf, "%2d%%", total);
17442 return decode_mode_spec_buf;
17443 }
17444 }
17445
8ffcb79f
RS
17446 /* Display percentage of size above the bottom of the screen. */
17447 case 'P':
17448 {
17449 int toppos = marker_position (w->start);
d39b6696
KH
17450 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
17451 int total = BUF_ZV (b) - BUF_BEGV (b);
8ffcb79f 17452
d39b6696 17453 if (botpos >= BUF_ZV (b))
8ffcb79f 17454 {
d39b6696 17455 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
17456 return "All";
17457 else
17458 return "Bottom";
17459 }
17460 else
17461 {
3c7d31b9
RS
17462 if (total > 1000000)
17463 /* Do it differently for a large value, to avoid overflow. */
17464 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17465 else
17466 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
8ffcb79f
RS
17467 /* We can't normally display a 3-digit number,
17468 so get us a 2-digit number that is close. */
17469 if (total == 100)
17470 total = 99;
d39b6696 17471 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
17472 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
17473 else
17474 sprintf (decode_mode_spec_buf, "%2d%%", total);
17475 return decode_mode_spec_buf;
17476 }
17477 }
17478
1af9f229
RS
17479 case 's':
17480 /* status of process */
64b7122d 17481 obj = Fget_buffer_process (Fcurrent_buffer ());
1af9f229
RS
17482 if (NILP (obj))
17483 return "no process";
17484#ifdef subprocesses
17485 obj = Fsymbol_name (Fprocess_status (obj));
17486#endif
17487 break;
d39b6696 17488
1af9f229
RS
17489 case 't': /* indicate TEXT or BINARY */
17490#ifdef MODE_LINE_BINARY_TEXT
17491 return MODE_LINE_BINARY_TEXT (b);
17492#else
17493 return "T";
17494#endif
1c9241f5
KH
17495
17496 case 'z':
17497 /* coding-system (not including end-of-line format) */
17498 case 'Z':
17499 /* coding-system (including end-of-line type) */
17500 {
17501 int eol_flag = (c == 'Z');
539b4d41 17502 char *p = decode_mode_spec_buf;
1c9241f5 17503
d30e754b 17504 if (! FRAME_WINDOW_P (f))
1c9241f5 17505 {
11c52c4f
RS
17506 /* No need to mention EOL here--the terminal never needs
17507 to do EOL conversion. */
17508 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
17509 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
1c9241f5 17510 }
f13c925f 17511 p = decode_mode_spec_coding (b->buffer_file_coding_system,
539b4d41 17512 p, eol_flag);
f13c925f 17513
11c52c4f 17514#if 0 /* This proves to be annoying; I think we can do without. -- rms. */
1c9241f5
KH
17515#ifdef subprocesses
17516 obj = Fget_buffer_process (Fcurrent_buffer ());
17517 if (PROCESSP (obj))
17518 {
17519 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
17520 p, eol_flag);
17521 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
17522 p, eol_flag);
17523 }
17524#endif /* subprocesses */
11c52c4f 17525#endif /* 0 */
1c9241f5
KH
17526 *p = 0;
17527 return decode_mode_spec_buf;
17528 }
a2889657 17529 }
d39b6696 17530
e24c997d 17531 if (STRINGP (obj))
72f62cb5
GM
17532 {
17533 *multibyte = STRING_MULTIBYTE (obj);
2051c264 17534 return (char *) SDATA (obj);
72f62cb5 17535 }
a2889657
JB
17536 else
17537 return "";
17538}
5f5c8ee5
GM
17539
17540
12adba34
RS
17541/* Count up to COUNT lines starting from START / START_BYTE.
17542 But don't go beyond LIMIT_BYTE.
17543 Return the number of lines thus found (always nonnegative).
59b49f63 17544
12adba34 17545 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
59b49f63
RS
17546
17547static int
12adba34
RS
17548display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
17549 int start, start_byte, limit_byte, count;
17550 int *byte_pos_ptr;
59b49f63 17551{
59b49f63
RS
17552 register unsigned char *cursor;
17553 unsigned char *base;
17554
17555 register int ceiling;
17556 register unsigned char *ceiling_addr;
12adba34 17557 int orig_count = count;
59b49f63
RS
17558
17559 /* If we are not in selective display mode,
17560 check only for newlines. */
12adba34
RS
17561 int selective_display = (!NILP (current_buffer->selective_display)
17562 && !INTEGERP (current_buffer->selective_display));
59b49f63
RS
17563
17564 if (count > 0)
12adba34
RS
17565 {
17566 while (start_byte < limit_byte)
17567 {
17568 ceiling = BUFFER_CEILING_OF (start_byte);
17569 ceiling = min (limit_byte - 1, ceiling);
17570 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
17571 base = (cursor = BYTE_POS_ADDR (start_byte));
17572 while (1)
17573 {
17574 if (selective_display)
17575 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
17576 ;
17577 else
17578 while (*cursor != '\n' && ++cursor != ceiling_addr)
17579 ;
17580
17581 if (cursor != ceiling_addr)
17582 {
17583 if (--count == 0)
17584 {
17585 start_byte += cursor - base + 1;
17586 *byte_pos_ptr = start_byte;
17587 return orig_count;
17588 }
17589 else
17590 if (++cursor == ceiling_addr)
17591 break;
17592 }
17593 else
17594 break;
17595 }
17596 start_byte += cursor - base;
17597 }
17598 }
59b49f63
RS
17599 else
17600 {
12adba34
RS
17601 while (start_byte > limit_byte)
17602 {
17603 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
17604 ceiling = max (limit_byte, ceiling);
17605 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
17606 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
59b49f63
RS
17607 while (1)
17608 {
12adba34
RS
17609 if (selective_display)
17610 while (--cursor != ceiling_addr
17611 && *cursor != '\n' && *cursor != 015)
17612 ;
17613 else
17614 while (--cursor != ceiling_addr && *cursor != '\n')
17615 ;
17616
59b49f63
RS
17617 if (cursor != ceiling_addr)
17618 {
17619 if (++count == 0)
17620 {
12adba34
RS
17621 start_byte += cursor - base + 1;
17622 *byte_pos_ptr = start_byte;
17623 /* When scanning backwards, we should
17624 not count the newline posterior to which we stop. */
17625 return - orig_count - 1;
59b49f63
RS
17626 }
17627 }
17628 else
17629 break;
17630 }
12adba34
RS
17631 /* Here we add 1 to compensate for the last decrement
17632 of CURSOR, which took it past the valid range. */
17633 start_byte += cursor - base + 1;
59b49f63
RS
17634 }
17635 }
17636
12adba34 17637 *byte_pos_ptr = limit_byte;
aa6d10fa 17638
12adba34
RS
17639 if (count < 0)
17640 return - orig_count + count;
17641 return orig_count - count;
aa6d10fa 17642
12adba34 17643}
a2889657 17644
a2889657 17645
5f5c8ee5
GM
17646\f
17647/***********************************************************************
17648 Displaying strings
17649 ***********************************************************************/
278feba9 17650
5f5c8ee5 17651/* Display a NUL-terminated string, starting with index START.
a3788d53 17652
5f5c8ee5
GM
17653 If STRING is non-null, display that C string. Otherwise, the Lisp
17654 string LISP_STRING is displayed.
a2889657 17655
5f5c8ee5
GM
17656 If FACE_STRING is not nil, FACE_STRING_POS is a position in
17657 FACE_STRING. Display STRING or LISP_STRING with the face at
17658 FACE_STRING_POS in FACE_STRING:
a2889657 17659
5f5c8ee5
GM
17660 Display the string in the environment given by IT, but use the
17661 standard display table, temporarily.
a3788d53 17662
5f5c8ee5
GM
17663 FIELD_WIDTH is the minimum number of output glyphs to produce.
17664 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17665 with spaces. If STRING has more characters, more than FIELD_WIDTH
17666 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
2311178e 17667
5f5c8ee5
GM
17668 PRECISION is the maximum number of characters to output from
17669 STRING. PRECISION < 0 means don't truncate the string.
a2889657 17670
5f5c8ee5 17671 This is roughly equivalent to printf format specifiers:
a2889657 17672
5f5c8ee5
GM
17673 FIELD_WIDTH PRECISION PRINTF
17674 ----------------------------------------
17675 -1 -1 %s
17676 -1 10 %.10s
17677 10 -1 %10s
17678 20 10 %20.10s
a2889657 17679
5f5c8ee5
GM
17680 MULTIBYTE zero means do not display multibyte chars, > 0 means do
17681 display them, and < 0 means obey the current buffer's value of
17682 enable_multibyte_characters.
278feba9 17683
5f5c8ee5 17684 Value is the number of glyphs produced. */
b1d1124b 17685
5f5c8ee5
GM
17686static int
17687display_string (string, lisp_string, face_string, face_string_pos,
17688 start, it, field_width, precision, max_x, multibyte)
17689 unsigned char *string;
17690 Lisp_Object lisp_string;
68c45bf0
PE
17691 Lisp_Object face_string;
17692 int face_string_pos;
5f5c8ee5
GM
17693 int start;
17694 struct it *it;
17695 int field_width, precision, max_x;
17696 int multibyte;
17697{
17698 int hpos_at_start = it->hpos;
17699 int saved_face_id = it->face_id;
17700 struct glyph_row *row = it->glyph_row;
17701
17702 /* Initialize the iterator IT for iteration over STRING beginning
e719f5ae 17703 with index START. */
5f5c8ee5
GM
17704 reseat_to_string (it, string, lisp_string, start,
17705 precision, field_width, multibyte);
17706
17707 /* If displaying STRING, set up the face of the iterator
17708 from LISP_STRING, if that's given. */
17709 if (STRINGP (face_string))
17710 {
17711 int endptr;
17712 struct face *face;
2311178e 17713
5f5c8ee5
GM
17714 it->face_id
17715 = face_at_string_position (it->w, face_string, face_string_pos,
17716 0, it->region_beg_charpos,
17717 it->region_end_charpos,
5de7c6f2 17718 &endptr, it->base_face_id, 0);
5f5c8ee5
GM
17719 face = FACE_FROM_ID (it->f, it->face_id);
17720 it->face_box_p = face->box != FACE_NO_BOX;
b1d1124b 17721 }
a2889657 17722
5f5c8ee5
GM
17723 /* Set max_x to the maximum allowed X position. Don't let it go
17724 beyond the right edge of the window. */
17725 if (max_x <= 0)
17726 max_x = it->last_visible_x;
17727 else
17728 max_x = min (max_x, it->last_visible_x);
efc63ef0 17729
5f5c8ee5
GM
17730 /* Skip over display elements that are not visible. because IT->w is
17731 hscrolled. */
17732 if (it->current_x < it->first_visible_x)
17733 move_it_in_display_line_to (it, 100000, it->first_visible_x,
17734 MOVE_TO_POS | MOVE_TO_X);
a2889657 17735
5f5c8ee5
GM
17736 row->ascent = it->max_ascent;
17737 row->height = it->max_ascent + it->max_descent;
312246d1
GM
17738 row->phys_ascent = it->max_phys_ascent;
17739 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
546f98b8 17740 row->extra_line_spacing = it->max_extra_line_spacing;
1c9241f5 17741
5f5c8ee5
GM
17742 /* This condition is for the case that we are called with current_x
17743 past last_visible_x. */
17744 while (it->current_x < max_x)
a2889657 17745 {
5f5c8ee5 17746 int x_before, x, n_glyphs_before, i, nglyphs;
1c9241f5 17747
5f5c8ee5
GM
17748 /* Get the next display element. */
17749 if (!get_next_display_element (it))
90adcf20 17750 break;
1c9241f5 17751
5f5c8ee5
GM
17752 /* Produce glyphs. */
17753 x_before = it->current_x;
17754 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
17755 PRODUCE_GLYPHS (it);
90adcf20 17756
5f5c8ee5
GM
17757 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
17758 i = 0;
17759 x = x_before;
17760 while (i < nglyphs)
a2889657 17761 {
5f5c8ee5 17762 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
2311178e 17763
5f5c8ee5
GM
17764 if (!it->truncate_lines_p
17765 && x + glyph->pixel_width > max_x)
17766 {
17767 /* End of continued line or max_x reached. */
37be86f2
KH
17768 if (CHAR_GLYPH_PADDING_P (*glyph))
17769 {
17770 /* A wide character is unbreakable. */
17771 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
17772 it->current_x = x_before;
17773 }
17774 else
17775 {
17776 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
17777 it->current_x = x;
17778 }
5f5c8ee5
GM
17779 break;
17780 }
17781 else if (x + glyph->pixel_width > it->first_visible_x)
17782 {
17783 /* Glyph is at least partially visible. */
17784 ++it->hpos;
17785 if (x < it->first_visible_x)
17786 it->glyph_row->x = x - it->first_visible_x;
17787 }
17788 else
a2889657 17789 {
5f5c8ee5
GM
17790 /* Glyph is off the left margin of the display area.
17791 Should not happen. */
17792 abort ();
a2889657 17793 }
5f5c8ee5
GM
17794
17795 row->ascent = max (row->ascent, it->max_ascent);
17796 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
17797 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17798 row->phys_height = max (row->phys_height,
17799 it->max_phys_ascent + it->max_phys_descent);
546f98b8
KS
17800 row->extra_line_spacing = max (row->extra_line_spacing,
17801 it->max_extra_line_spacing);
5f5c8ee5
GM
17802 x += glyph->pixel_width;
17803 ++i;
a2889657 17804 }
5f5c8ee5
GM
17805
17806 /* Stop if max_x reached. */
17807 if (i < nglyphs)
17808 break;
17809
17810 /* Stop at line ends. */
17811 if (ITERATOR_AT_END_OF_LINE_P (it))
a2889657 17812 {
5f5c8ee5
GM
17813 it->continuation_lines_width = 0;
17814 break;
a2889657 17815 }
1c9241f5 17816
cafafe0b 17817 set_iterator_to_next (it, 1);
a688bb24 17818
5f5c8ee5
GM
17819 /* Stop if truncating at the right edge. */
17820 if (it->truncate_lines_p
17821 && it->current_x >= it->last_visible_x)
17822 {
17823 /* Add truncation mark, but don't do it if the line is
17824 truncated at a padding space. */
17825 if (IT_CHARPOS (*it) < it->string_nchars)
1c9241f5 17826 {
5f5c8ee5 17827 if (!FRAME_WINDOW_P (it->f))
37be86f2
KH
17828 {
17829 int i, n;
17830
9299cb15 17831 if (it->current_x > it->last_visible_x)
37be86f2 17832 {
9299cb15
KH
17833 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17834 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17835 break;
17836 for (n = row->used[TEXT_AREA]; i < n; ++i)
17837 {
17838 row->used[TEXT_AREA] = i;
17839 produce_special_glyphs (it, IT_TRUNCATION);
17840 }
37be86f2 17841 }
9299cb15 17842 produce_special_glyphs (it, IT_TRUNCATION);
37be86f2 17843 }
5f5c8ee5 17844 it->glyph_row->truncated_on_right_p = 1;
1c9241f5 17845 }
5f5c8ee5 17846 break;
1c9241f5 17847 }
a2889657
JB
17848 }
17849
5f5c8ee5
GM
17850 /* Maybe insert a truncation at the left. */
17851 if (it->first_visible_x
17852 && IT_CHARPOS (*it) > 0)
a2889657 17853 {
5f5c8ee5
GM
17854 if (!FRAME_WINDOW_P (it->f))
17855 insert_left_trunc_glyphs (it);
17856 it->glyph_row->truncated_on_left_p = 1;
a2889657
JB
17857 }
17858
5f5c8ee5 17859 it->face_id = saved_face_id;
2311178e 17860
5f5c8ee5
GM
17861 /* Value is number of columns displayed. */
17862 return it->hpos - hpos_at_start;
17863}
a2889657 17864
a2889657 17865
a2889657 17866\f
3b6b6db7 17867/* This is like a combination of memq and assq. Return 1/2 if PROPVAL
5f5c8ee5
GM
17868 appears as an element of LIST or as the car of an element of LIST.
17869 If PROPVAL is a list, compare each element against LIST in that
3b6b6db7
SM
17870 way, and return 1/2 if any element of PROPVAL is found in LIST.
17871 Otherwise return 0. This function cannot quit.
17872 The return value is 2 if the text is invisible but with an ellipsis
17873 and 1 if it's invisible and without an ellipsis. */
642eefc6
RS
17874
17875int
17876invisible_p (propval, list)
17877 register Lisp_Object propval;
17878 Lisp_Object list;
17879{
3b6b6db7 17880 register Lisp_Object tail, proptail;
2311178e 17881
3b6b6db7
SM
17882 for (tail = list; CONSP (tail); tail = XCDR (tail))
17883 {
17884 register Lisp_Object tem;
17885 tem = XCAR (tail);
17886 if (EQ (propval, tem))
17887 return 1;
17888 if (CONSP (tem) && EQ (propval, XCAR (tem)))
17889 return NILP (XCDR (tem)) ? 1 : 2;
17890 }
2311178e 17891
3b6b6db7
SM
17892 if (CONSP (propval))
17893 {
17894 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
17895 {
17896 Lisp_Object propelt;
17897 propelt = XCAR (proptail);
17898 for (tail = list; CONSP (tail); tail = XCDR (tail))
17899 {
17900 register Lisp_Object tem;
17901 tem = XCAR (tail);
17902 if (EQ (propelt, tem))
17903 return 1;
17904 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
17905 return NILP (XCDR (tem)) ? 1 : 2;
17906 }
17907 }
17908 }
2311178e 17909
3b6b6db7 17910 return 0;
642eefc6 17911}
5f5c8ee5 17912
da06e4d7
KS
17913/* Calculate a width or height in pixels from a specification using
17914 the following elements:
17915
be21b925 17916 SPEC ::=
da06e4d7
KS
17917 NUM - a (fractional) multiple of the default font width/height
17918 (NUM) - specifies exactly NUM pixels
17919 UNIT - a fixed number of pixels, see below.
17920 ELEMENT - size of a display element in pixels, see below.
17921 (NUM . SPEC) - equals NUM * SPEC
17922 (+ SPEC SPEC ...) - add pixel values
17923 (- SPEC SPEC ...) - subtract pixel values
17924 (- SPEC) - negate pixel value
17925
be21b925 17926 NUM ::=
da06e4d7
KS
17927 INT or FLOAT - a number constant
17928 SYMBOL - use symbol's (buffer local) variable binding.
17929
17930 UNIT ::=
17931 in - pixels per inch *)
17932 mm - pixels per 1/1000 meter *)
17933 cm - pixels per 1/100 meter *)
17934 width - width of current font in pixels.
17935 height - height of current font in pixels.
17936
17937 *) using the ratio(s) defined in display-pixels-per-inch.
17938
17939 ELEMENT ::=
17940
17941 left-fringe - left fringe width in pixels
17942 right-fringe - right fringe width in pixels
17943
17944 left-margin - left margin width in pixels
17945 right-margin - right margin width in pixels
17946
17947 scroll-bar - scroll-bar area width in pixels
17948
17949 Examples:
17950
17951 Pixels corresponding to 5 inches:
be21b925
KS
17952 (5 . in)
17953
da06e4d7
KS
17954 Total width of non-text areas on left side of window (if scroll-bar is on left):
17955 '(space :width (+ left-fringe left-margin scroll-bar))
17956
17957 Align to first text column (in header line):
17958 '(space :align-to 0)
17959
be21b925 17960 Align to middle of text area minus half the width of variable `my-image'
da06e4d7
KS
17961 containing a loaded image:
17962 '(space :align-to (0.5 . (- text my-image)))
17963
17964 Width of left margin minus width of 1 character in the default font:
17965 '(space :width (- left-margin 1))
17966
17967 Width of left margin minus width of 2 characters in the current font:
17968 '(space :width (- left-margin (2 . width)))
17969
17970 Center 1 character over left-margin (in header line):
17971 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
17972
17973 Different ways to express width of left fringe plus left margin minus one pixel:
17974 '(space :width (- (+ left-fringe left-margin) (1)))
17975 '(space :width (+ left-fringe left-margin (- (1))))
17976 '(space :width (+ left-fringe left-margin (-1)))
17977
17978*/
17979
17980#define NUMVAL(X) \
17981 ((INTEGERP (X) || FLOATP (X)) \
17982 ? XFLOATINT (X) \
17983 : - 1)
17984
17985int
17986calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
17987 double *res;
17988 struct it *it;
17989 Lisp_Object prop;
17990 void *font;
17991 int width_p, *align_to;
17992{
17993 double pixels;
17994
17995#define OK_PIXELS(val) ((*res = (double)(val)), 1)
17996#define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
17997
17998 if (NILP (prop))
17999 return OK_PIXELS (0);
18000
18001 if (SYMBOLP (prop))
18002 {
18003 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18004 {
18005 char *unit = SDATA (SYMBOL_NAME (prop));
18006
18007 if (unit[0] == 'i' && unit[1] == 'n')
18008 pixels = 1.0;
18009 else if (unit[0] == 'm' && unit[1] == 'm')
18010 pixels = 25.4;
18011 else if (unit[0] == 'c' && unit[1] == 'm')
18012 pixels = 2.54;
18013 else
18014 pixels = 0;
18015 if (pixels > 0)
18016 {
18017 double ppi;
c0d7caba
KS
18018#ifdef HAVE_WINDOW_SYSTEM
18019 if (FRAME_WINDOW_P (it->f)
18020 && (ppi = (width_p
18021 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18022 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18023 ppi > 0))
18024 return OK_PIXELS (ppi / pixels);
18025#endif
18026
da06e4d7
KS
18027 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18028 || (CONSP (Vdisplay_pixels_per_inch)
18029 && (ppi = (width_p
18030 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18031 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18032 ppi > 0)))
18033 return OK_PIXELS (ppi / pixels);
18034
18035 return 0;
18036 }
18037 }
18038
18039#ifdef HAVE_WINDOW_SYSTEM
18040 if (EQ (prop, Qheight))
18041 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18042 if (EQ (prop, Qwidth))
18043 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18044#else
18045 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18046 return OK_PIXELS (1);
18047#endif
18048
18049 if (EQ (prop, Qtext))
18050 return OK_PIXELS (width_p
18051 ? window_box_width (it->w, TEXT_AREA)
18052 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18053
18054 if (align_to && *align_to < 0)
18055 {
18056 *res = 0;
18057 if (EQ (prop, Qleft))
18058 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18059 if (EQ (prop, Qright))
18060 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18061 if (EQ (prop, Qcenter))
18062 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18063 + window_box_width (it->w, TEXT_AREA) / 2);
18064 if (EQ (prop, Qleft_fringe))
18065 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18066 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18067 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18068 if (EQ (prop, Qright_fringe))
18069 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18070 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18071 : window_box_right_offset (it->w, TEXT_AREA));
18072 if (EQ (prop, Qleft_margin))
18073 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18074 if (EQ (prop, Qright_margin))
18075 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18076 if (EQ (prop, Qscroll_bar))
18077 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18078 ? 0
18079 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18080 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18081 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18082 : 0)));
18083 }
18084 else
18085 {
18086 if (EQ (prop, Qleft_fringe))
18087 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18088 if (EQ (prop, Qright_fringe))
18089 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18090 if (EQ (prop, Qleft_margin))
18091 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18092 if (EQ (prop, Qright_margin))
18093 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18094 if (EQ (prop, Qscroll_bar))
18095 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18096 }
18097
18098 prop = Fbuffer_local_value (prop, it->w->buffer);
18099 }
18100
18101 if (INTEGERP (prop) || FLOATP (prop))
18102 {
18103 int base_unit = (width_p
18104 ? FRAME_COLUMN_WIDTH (it->f)
18105 : FRAME_LINE_HEIGHT (it->f));
18106 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18107 }
18108
18109 if (CONSP (prop))
18110 {
18111 Lisp_Object car = XCAR (prop);
18112 Lisp_Object cdr = XCDR (prop);
18113
18114 if (SYMBOLP (car))
18115 {
992126de 18116#ifdef HAVE_WINDOW_SYSTEM
da06e4d7
KS
18117 if (valid_image_p (prop))
18118 {
18119 int id = lookup_image (it->f, prop);
18120 struct image *img = IMAGE_FROM_ID (it->f, id);
18121
18122 return OK_PIXELS (width_p ? img->width : img->height);
18123 }
992126de 18124#endif
da06e4d7
KS
18125 if (EQ (car, Qplus) || EQ (car, Qminus))
18126 {
18127 int first = 1;
18128 double px;
18129
18130 pixels = 0;
18131 while (CONSP (cdr))
18132 {
18133 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18134 font, width_p, align_to))
18135 return 0;
18136 if (first)
18137 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18138 else
18139 pixels += px;
18140 cdr = XCDR (cdr);
18141 }
18142 if (EQ (car, Qminus))
18143 pixels = -pixels;
18144 return OK_PIXELS (pixels);
18145 }
18146
18147 car = Fbuffer_local_value (car, it->w->buffer);
18148 }
18149
18150 if (INTEGERP (car) || FLOATP (car))
18151 {
18152 double fact;
18153 pixels = XFLOATINT (car);
18154 if (NILP (cdr))
18155 return OK_PIXELS (pixels);
18156 if (calc_pixel_width_or_height (&fact, it, cdr,
18157 font, width_p, align_to))
18158 return OK_PIXELS (pixels * fact);
18159 return 0;
18160 }
18161
18162 return 0;
18163 }
18164
18165 return 0;
18166}
18167
642eefc6 18168\f
133c764e
KS
18169/***********************************************************************
18170 Glyph Display
18171 ***********************************************************************/
18172
79fa9e0f
KS
18173#ifdef HAVE_WINDOW_SYSTEM
18174
133c764e
KS
18175#if GLYPH_DEBUG
18176
18177void
18178dump_glyph_string (s)
18179 struct glyph_string *s;
18180{
18181 fprintf (stderr, "glyph string\n");
18182 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18183 s->x, s->y, s->width, s->height);
18184 fprintf (stderr, " ybase = %d\n", s->ybase);
18185 fprintf (stderr, " hl = %d\n", s->hl);
18186 fprintf (stderr, " left overhang = %d, right = %d\n",
18187 s->left_overhang, s->right_overhang);
18188 fprintf (stderr, " nchars = %d\n", s->nchars);
18189 fprintf (stderr, " extends to end of line = %d\n",
18190 s->extends_to_end_of_line_p);
18191 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18192 fprintf (stderr, " bg width = %d\n", s->background_width);
18193}
18194
18195#endif /* GLYPH_DEBUG */
18196
18197/* Initialize glyph string S. CHAR2B is a suitably allocated vector
18198 of XChar2b structures for S; it can't be allocated in
18199 init_glyph_string because it must be allocated via `alloca'. W
18200 is the window on which S is drawn. ROW and AREA are the glyph row
18201 and area within the row from which S is constructed. START is the
18202 index of the first glyph structure covered by S. HL is a
18203 face-override for drawing S. */
18204
18205#ifdef HAVE_NTGUI
18206#define OPTIONAL_HDC(hdc) hdc,
18207#define DECLARE_HDC(hdc) HDC hdc;
18208#define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18209#define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18210#endif
18211
18212#ifndef OPTIONAL_HDC
18213#define OPTIONAL_HDC(hdc)
18214#define DECLARE_HDC(hdc)
18215#define ALLOCATE_HDC(hdc, f)
18216#define RELEASE_HDC(hdc, f)
18217#endif
18218
18219static void
18220init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18221 struct glyph_string *s;
18222 DECLARE_HDC (hdc)
18223 XChar2b *char2b;
18224 struct window *w;
18225 struct glyph_row *row;
18226 enum glyph_row_area area;
18227 int start;
18228 enum draw_glyphs_face hl;
18229{
18230 bzero (s, sizeof *s);
18231 s->w = w;
18232 s->f = XFRAME (w->frame);
18233#ifdef HAVE_NTGUI
18234 s->hdc = hdc;
18235#endif
18236 s->display = FRAME_X_DISPLAY (s->f);
18237 s->window = FRAME_X_WINDOW (s->f);
18238 s->char2b = char2b;
18239 s->hl = hl;
18240 s->row = row;
18241 s->area = area;
18242 s->first_glyph = row->glyphs[area] + start;
18243 s->height = row->height;
18244 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18245
18246 /* Display the internal border below the tool-bar window. */
94c4db3c
SM
18247 if (WINDOWP (s->f->tool_bar_window)
18248 && s->w == XWINDOW (s->f->tool_bar_window))
133c764e
KS
18249 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18250
18251 s->ybase = s->y + row->ascent;
18252}
18253
18254
18255/* Append the list of glyph strings with head H and tail T to the list
18256 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18257
18258static INLINE void
18259append_glyph_string_lists (head, tail, h, t)
18260 struct glyph_string **head, **tail;
18261 struct glyph_string *h, *t;
18262{
18263 if (h)
18264 {
18265 if (*head)
18266 (*tail)->next = h;
18267 else
18268 *head = h;
18269 h->prev = *tail;
18270 *tail = t;
18271 }
18272}
18273
18274
18275/* Prepend the list of glyph strings with head H and tail T to the
18276 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18277 result. */
18278
18279static INLINE void
18280prepend_glyph_string_lists (head, tail, h, t)
18281 struct glyph_string **head, **tail;
18282 struct glyph_string *h, *t;
18283{
18284 if (h)
18285 {
18286 if (*head)
18287 (*head)->prev = t;
18288 else
18289 *tail = t;
18290 t->next = *head;
18291 *head = h;
18292 }
18293}
18294
18295
18296/* Append glyph string S to the list with head *HEAD and tail *TAIL.
18297 Set *HEAD and *TAIL to the resulting list. */
18298
18299static INLINE void
18300append_glyph_string (head, tail, s)
18301 struct glyph_string **head, **tail;
18302 struct glyph_string *s;
18303{
18304 s->next = s->prev = NULL;
18305 append_glyph_string_lists (head, tail, s, s);
18306}
18307
18308
18309/* Get face and two-byte form of character glyph GLYPH on frame F.
18310 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
18311 a pointer to a realized face that is ready for display. */
18312
18313static INLINE struct face *
18314get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
18315 struct frame *f;
18316 struct glyph *glyph;
18317 XChar2b *char2b;
18318 int *two_byte_p;
18319{
18320 struct face *face;
18321
18322 xassert (glyph->type == CHAR_GLYPH);
18323 face = FACE_FROM_ID (f, glyph->face_id);
18324
18325 if (two_byte_p)
18326 *two_byte_p = 0;
18327
18328 if (!glyph->multibyte_p)
18329 {
18330 /* Unibyte case. We don't have to encode, but we have to make
18331 sure to use a face suitable for unibyte. */
18332 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18333 }
18334 else if (glyph->u.ch < 128
18335 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
18336 {
18337 /* Case of ASCII in a face known to fit ASCII. */
18338 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18339 }
18340 else
18341 {
18342 int c1, c2, charset;
18343
18344 /* Split characters into bytes. If c2 is -1 afterwards, C is
18345 really a one-byte character so that byte1 is zero. */
18346 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
18347 if (c2 > 0)
18348 STORE_XCHAR2B (char2b, c1, c2);
18349 else
18350 STORE_XCHAR2B (char2b, 0, c1);
18351
18352 /* Maybe encode the character in *CHAR2B. */
18353 if (charset != CHARSET_ASCII)
18354 {
18355 struct font_info *font_info
18356 = FONT_INFO_FROM_ID (f, face->font_info_id);
18357 if (font_info)
18358 glyph->font_type
18359 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
18360 }
18361 }
18362
18363 /* Make sure X resources of the face are allocated. */
18364 xassert (face != NULL);
18365 PREPARE_FACE_FOR_DISPLAY (f, face);
18366 return face;
18367}
18368
18369
18370/* Fill glyph string S with composition components specified by S->cmp.
18371
18372 FACES is an array of faces for all components of this composition.
18373 S->gidx is the index of the first component for S.
a66678fd
YM
18374
18375 OVERLAPS non-zero means S should draw the foreground only, and use
18376 its physical height for clipping. See also draw_glyphs.
133c764e
KS
18377
18378 Value is the index of a component not in S. */
18379
18380static int
a66678fd 18381fill_composite_glyph_string (s, faces, overlaps)
133c764e
KS
18382 struct glyph_string *s;
18383 struct face **faces;
a66678fd 18384 int overlaps;
133c764e
KS
18385{
18386 int i;
18387
18388 xassert (s);
18389
a66678fd 18390 s->for_overlaps = overlaps;
133c764e
KS
18391
18392 s->face = faces[s->gidx];
18393 s->font = s->face->font;
18394 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18395
18396 /* For all glyphs of this composition, starting at the offset
18397 S->gidx, until we reach the end of the definition or encounter a
18398 glyph that requires the different face, add it to S. */
18399 ++s->nchars;
18400 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
18401 ++s->nchars;
18402
18403 /* All glyph strings for the same composition has the same width,
18404 i.e. the width set for the first component of the composition. */
18405
18406 s->width = s->first_glyph->pixel_width;
18407
18408 /* If the specified font could not be loaded, use the frame's
18409 default font, but record the fact that we couldn't load it in
18410 the glyph string so that we can draw rectangles for the
18411 characters of the glyph string. */
18412 if (s->font == NULL)
18413 {
18414 s->font_not_found_p = 1;
18415 s->font = FRAME_FONT (s->f);
18416 }
18417
18418 /* Adjust base line for subscript/superscript text. */
18419 s->ybase += s->first_glyph->voffset;
18420
18421 xassert (s->face && s->face->gc);
18422
18423 /* This glyph string must always be drawn with 16-bit functions. */
18424 s->two_byte_p = 1;
18425
18426 return s->gidx + s->nchars;
18427}
18428
18429
18430/* Fill glyph string S from a sequence of character glyphs.
18431
18432 FACE_ID is the face id of the string. START is the index of the
18433 first glyph to consider, END is the index of the last + 1.
a66678fd
YM
18434 OVERLAPS non-zero means S should draw the foreground only, and use
18435 its physical height for clipping. See also draw_glyphs.
133c764e
KS
18436
18437 Value is the index of the first glyph not in S. */
18438
18439static int
a66678fd 18440fill_glyph_string (s, face_id, start, end, overlaps)
133c764e
KS
18441 struct glyph_string *s;
18442 int face_id;
a66678fd 18443 int start, end, overlaps;
133c764e
KS
18444{
18445 struct glyph *glyph, *last;
18446 int voffset;
18447 int glyph_not_available_p;
18448
18449 xassert (s->f == XFRAME (s->w->frame));
18450 xassert (s->nchars == 0);
18451 xassert (start >= 0 && end > start);
18452
a66678fd 18453 s->for_overlaps = overlaps,
133c764e
KS
18454 glyph = s->row->glyphs[s->area] + start;
18455 last = s->row->glyphs[s->area] + end;
18456 voffset = glyph->voffset;
18457
18458 glyph_not_available_p = glyph->glyph_not_available_p;
18459
18460 while (glyph < last
18461 && glyph->type == CHAR_GLYPH
18462 && glyph->voffset == voffset
18463 /* Same face id implies same font, nowadays. */
18464 && glyph->face_id == face_id
18465 && glyph->glyph_not_available_p == glyph_not_available_p)
18466 {
18467 int two_byte_p;
18468
18469 s->face = get_glyph_face_and_encoding (s->f, glyph,
18470 s->char2b + s->nchars,
18471 &two_byte_p);
18472 s->two_byte_p = two_byte_p;
18473 ++s->nchars;
18474 xassert (s->nchars <= end - start);
18475 s->width += glyph->pixel_width;
18476 ++glyph;
18477 }
18478
18479 s->font = s->face->font;
18480 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18481
18482 /* If the specified font could not be loaded, use the frame's font,
18483 but record the fact that we couldn't load it in
18484 S->font_not_found_p so that we can draw rectangles for the
18485 characters of the glyph string. */
18486 if (s->font == NULL || glyph_not_available_p)
18487 {
18488 s->font_not_found_p = 1;
18489 s->font = FRAME_FONT (s->f);
18490 }
18491
18492 /* Adjust base line for subscript/superscript text. */
18493 s->ybase += voffset;
18494
18495 xassert (s->face && s->face->gc);
18496 return glyph - s->row->glyphs[s->area];
18497}
18498
18499
18500/* Fill glyph string S from image glyph S->first_glyph. */
18501
18502static void
18503fill_image_glyph_string (s)
18504 struct glyph_string *s;
18505{
18506 xassert (s->first_glyph->type == IMAGE_GLYPH);
18507 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
18508 xassert (s->img);
e893970b 18509 s->slice = s->first_glyph->slice;
133c764e
KS
18510 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
18511 s->font = s->face->font;
18512 s->width = s->first_glyph->pixel_width;
18513
18514 /* Adjust base line for subscript/superscript text. */
18515 s->ybase += s->first_glyph->voffset;
18516}
18517
18518
18519/* Fill glyph string S from a sequence of stretch glyphs.
18520
18521 ROW is the glyph row in which the glyphs are found, AREA is the
18522 area within the row. START is the index of the first glyph to
18523 consider, END is the index of the last + 1.
18524
18525 Value is the index of the first glyph not in S. */
18526
18527static int
18528fill_stretch_glyph_string (s, row, area, start, end)
18529 struct glyph_string *s;
18530 struct glyph_row *row;
18531 enum glyph_row_area area;
18532 int start, end;
18533{
18534 struct glyph *glyph, *last;
18535 int voffset, face_id;
18536
18537 xassert (s->first_glyph->type == STRETCH_GLYPH);
18538
18539 glyph = s->row->glyphs[s->area] + start;
18540 last = s->row->glyphs[s->area] + end;
18541 face_id = glyph->face_id;
18542 s->face = FACE_FROM_ID (s->f, face_id);
18543 s->font = s->face->font;
18544 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18545 s->width = glyph->pixel_width;
18546 voffset = glyph->voffset;
18547
18548 for (++glyph;
18549 (glyph < last
18550 && glyph->type == STRETCH_GLYPH
18551 && glyph->voffset == voffset
18552 && glyph->face_id == face_id);
18553 ++glyph)
18554 s->width += glyph->pixel_width;
18555
18556 /* Adjust base line for subscript/superscript text. */
18557 s->ybase += voffset;
18558
18559 /* The case that face->gc == 0 is handled when drawing the glyph
18560 string by calling PREPARE_FACE_FOR_DISPLAY. */
18561 xassert (s->face);
18562 return glyph - s->row->glyphs[s->area];
18563}
18564
18565
18566/* EXPORT for RIF:
18567 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
18568 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
18569 assumed to be zero. */
18570
18571void
18572x_get_glyph_overhangs (glyph, f, left, right)
18573 struct glyph *glyph;
18574 struct frame *f;
18575 int *left, *right;
18576{
18577 *left = *right = 0;
18578
18579 if (glyph->type == CHAR_GLYPH)
18580 {
18581 XFontStruct *font;
18582 struct face *face;
18583 struct font_info *font_info;
18584 XChar2b char2b;
18585 XCharStruct *pcm;
18586
18587 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
18588 font = face->font;
18589 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
18590 if (font /* ++KFS: Should this be font_info ? */
18591 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
18592 {
18593 if (pcm->rbearing > pcm->width)
18594 *right = pcm->rbearing - pcm->width;
18595 if (pcm->lbearing < 0)
18596 *left = -pcm->lbearing;
18597 }
18598 }
18599}
18600
18601
18602/* Return the index of the first glyph preceding glyph string S that
18603 is overwritten by S because of S's left overhang. Value is -1
18604 if no glyphs are overwritten. */
18605
18606static int
18607left_overwritten (s)
18608 struct glyph_string *s;
18609{
18610 int k;
18611
18612 if (s->left_overhang)
18613 {
18614 int x = 0, i;
18615 struct glyph *glyphs = s->row->glyphs[s->area];
18616 int first = s->first_glyph - glyphs;
18617
18618 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
18619 x -= glyphs[i].pixel_width;
18620
18621 k = i + 1;
18622 }
18623 else
18624 k = -1;
18625
18626 return k;
18627}
18628
18629
18630/* Return the index of the first glyph preceding glyph string S that
18631 is overwriting S because of its right overhang. Value is -1 if no
18632 glyph in front of S overwrites S. */
18633
18634static int
18635left_overwriting (s)
18636 struct glyph_string *s;
18637{
18638 int i, k, x;
18639 struct glyph *glyphs = s->row->glyphs[s->area];
18640 int first = s->first_glyph - glyphs;
18641
18642 k = -1;
18643 x = 0;
18644 for (i = first - 1; i >= 0; --i)
18645 {
18646 int left, right;
18647 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18648 if (x + right > 0)
18649 k = i;
18650 x -= glyphs[i].pixel_width;
18651 }
18652
18653 return k;
18654}
18655
18656
18657/* Return the index of the last glyph following glyph string S that is
18658 not overwritten by S because of S's right overhang. Value is -1 if
18659 no such glyph is found. */
18660
18661static int
18662right_overwritten (s)
18663 struct glyph_string *s;
18664{
18665 int k = -1;
18666
18667 if (s->right_overhang)
18668 {
18669 int x = 0, i;
18670 struct glyph *glyphs = s->row->glyphs[s->area];
18671 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18672 int end = s->row->used[s->area];
18673
18674 for (i = first; i < end && s->right_overhang > x; ++i)
18675 x += glyphs[i].pixel_width;
18676
18677 k = i;
18678 }
18679
18680 return k;
18681}
18682
18683
18684/* Return the index of the last glyph following glyph string S that
18685 overwrites S because of its left overhang. Value is negative
18686 if no such glyph is found. */
18687
18688static int
18689right_overwriting (s)
18690 struct glyph_string *s;
18691{
18692 int i, k, x;
18693 int end = s->row->used[s->area];
18694 struct glyph *glyphs = s->row->glyphs[s->area];
18695 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18696
18697 k = -1;
18698 x = 0;
18699 for (i = first; i < end; ++i)
18700 {
18701 int left, right;
18702 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18703 if (x - left < 0)
18704 k = i;
18705 x += glyphs[i].pixel_width;
18706 }
18707
18708 return k;
18709}
18710
18711
18712/* Get face and two-byte form of character C in face FACE_ID on frame
18713 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
18714 means we want to display multibyte text. DISPLAY_P non-zero means
18715 make sure that X resources for the face returned are allocated.
18716 Value is a pointer to a realized face that is ready for display if
18717 DISPLAY_P is non-zero. */
18718
18719static INLINE struct face *
18720get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
18721 struct frame *f;
18722 int c, face_id;
18723 XChar2b *char2b;
18724 int multibyte_p, display_p;
18725{
18726 struct face *face = FACE_FROM_ID (f, face_id);
18727
18728 if (!multibyte_p)
18729 {
18730 /* Unibyte case. We don't have to encode, but we have to make
18731 sure to use a face suitable for unibyte. */
18732 STORE_XCHAR2B (char2b, 0, c);
18733 face_id = FACE_FOR_CHAR (f, face, c);
18734 face = FACE_FROM_ID (f, face_id);
18735 }
18736 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
18737 {
18738 /* Case of ASCII in a face known to fit ASCII. */
18739 STORE_XCHAR2B (char2b, 0, c);
18740 }
18741 else
18742 {
18743 int c1, c2, charset;
18744
18745 /* Split characters into bytes. If c2 is -1 afterwards, C is
18746 really a one-byte character so that byte1 is zero. */
18747 SPLIT_CHAR (c, charset, c1, c2);
18748 if (c2 > 0)
18749 STORE_XCHAR2B (char2b, c1, c2);
18750 else
18751 STORE_XCHAR2B (char2b, 0, c1);
18752
18753 /* Maybe encode the character in *CHAR2B. */
18754 if (face->font != NULL)
18755 {
18756 struct font_info *font_info
18757 = FONT_INFO_FROM_ID (f, face->font_info_id);
18758 if (font_info)
18759 rif->encode_char (c, char2b, font_info, 0);
18760 }
18761 }
18762
18763 /* Make sure X resources of the face are allocated. */
18764#ifdef HAVE_X_WINDOWS
18765 if (display_p)
18766#endif
18767 {
18768 xassert (face != NULL);
18769 PREPARE_FACE_FOR_DISPLAY (f, face);
18770 }
18771
18772 return face;
18773}
18774
18775
18776/* Set background width of glyph string S. START is the index of the
18777 first glyph following S. LAST_X is the right-most x-position + 1
18778 in the drawing area. */
18779
18780static INLINE void
18781set_glyph_string_background_width (s, start, last_x)
18782 struct glyph_string *s;
18783 int start;
18784 int last_x;
18785{
18786 /* If the face of this glyph string has to be drawn to the end of
18787 the drawing area, set S->extends_to_end_of_line_p. */
133c764e
KS
18788
18789 if (start == s->row->used[s->area]
18790 && s->area == TEXT_AREA
8a955545
KS
18791 && ((s->row->fill_line_p
18792 && (s->hl == DRAW_NORMAL_TEXT
18793 || s->hl == DRAW_IMAGE_RAISED
18794 || s->hl == DRAW_IMAGE_SUNKEN))
18795 || s->hl == DRAW_MOUSE_FACE))
18796 s->extends_to_end_of_line_p = 1;
133c764e
KS
18797
18798 /* If S extends its face to the end of the line, set its
18799 background_width to the distance to the right edge of the drawing
18800 area. */
18801 if (s->extends_to_end_of_line_p)
18802 s->background_width = last_x - s->x + 1;
18803 else
18804 s->background_width = s->width;
18805}
18806
18807
18808/* Compute overhangs and x-positions for glyph string S and its
18809 predecessors, or successors. X is the starting x-position for S.
18810 BACKWARD_P non-zero means process predecessors. */
18811
18812static void
18813compute_overhangs_and_x (s, x, backward_p)
18814 struct glyph_string *s;
18815 int x;
18816 int backward_p;
18817{
18818 if (backward_p)
18819 {
18820 while (s)
18821 {
18822 if (rif->compute_glyph_string_overhangs)
18823 rif->compute_glyph_string_overhangs (s);
18824 x -= s->width;
18825 s->x = x;
18826 s = s->prev;
18827 }
18828 }
18829 else
18830 {
18831 while (s)
18832 {
18833 if (rif->compute_glyph_string_overhangs)
18834 rif->compute_glyph_string_overhangs (s);
18835 s->x = x;
18836 x += s->width;
18837 s = s->next;
18838 }
18839 }
18840}
18841
18842
18843
fa3c6b4d 18844/* The following macros are only called from draw_glyphs below.
133c764e
KS
18845 They reference the following parameters of that function directly:
18846 `w', `row', `area', and `overlap_p'
18847 as well as the following local variables:
18848 `s', `f', and `hdc' (in W32) */
18849
18850#ifdef HAVE_NTGUI
18851/* On W32, silently add local `hdc' variable to argument list of
18852 init_glyph_string. */
18853#define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18854 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
18855#else
18856#define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18857 init_glyph_string (s, char2b, w, row, area, start, hl)
18858#endif
18859
18860/* Add a glyph string for a stretch glyph to the list of strings
18861 between HEAD and TAIL. START is the index of the stretch glyph in
18862 row area AREA of glyph row ROW. END is the index of the last glyph
18863 in that glyph row area. X is the current output position assigned
18864 to the new glyph string constructed. HL overrides that face of the
18865 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18866 is the right-most x-position of the drawing area. */
18867
18868/* SunOS 4 bundled cc, barfed on continuations in the arg lists here
18869 and below -- keep them on one line. */
18870#define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18871 do \
18872 { \
18873 s = (struct glyph_string *) alloca (sizeof *s); \
18874 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18875 START = fill_stretch_glyph_string (s, row, area, START, END); \
18876 append_glyph_string (&HEAD, &TAIL, s); \
18877 s->x = (X); \
18878 } \
18879 while (0)
18880
18881
18882/* Add a glyph string for an image glyph to the list of strings
18883 between HEAD and TAIL. START is the index of the image glyph in
18884 row area AREA of glyph row ROW. END is the index of the last glyph
18885 in that glyph row area. X is the current output position assigned
18886 to the new glyph string constructed. HL overrides that face of the
18887 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18888 is the right-most x-position of the drawing area. */
18889
18890#define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18891 do \
18892 { \
18893 s = (struct glyph_string *) alloca (sizeof *s); \
18894 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18895 fill_image_glyph_string (s); \
18896 append_glyph_string (&HEAD, &TAIL, s); \
18897 ++START; \
18898 s->x = (X); \
18899 } \
18900 while (0)
18901
18902
18903/* Add a glyph string for a sequence of character glyphs to the list
18904 of strings between HEAD and TAIL. START is the index of the first
18905 glyph in row area AREA of glyph row ROW that is part of the new
18906 glyph string. END is the index of the last glyph in that glyph row
18907 area. X is the current output position assigned to the new glyph
18908 string constructed. HL overrides that face of the glyph; e.g. it
18909 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
18910 right-most x-position of the drawing area. */
18911
18912#define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18913 do \
18914 { \
18915 int c, face_id; \
18916 XChar2b *char2b; \
18917 \
18918 c = (row)->glyphs[area][START].u.ch; \
18919 face_id = (row)->glyphs[area][START].face_id; \
18920 \
18921 s = (struct glyph_string *) alloca (sizeof *s); \
18922 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
18923 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
18924 append_glyph_string (&HEAD, &TAIL, s); \
18925 s->x = (X); \
a66678fd 18926 START = fill_glyph_string (s, face_id, START, END, overlaps); \
133c764e
KS
18927 } \
18928 while (0)
18929
18930
18931/* Add a glyph string for a composite sequence to the list of strings
18932 between HEAD and TAIL. START is the index of the first glyph in
18933 row area AREA of glyph row ROW that is part of the new glyph
18934 string. END is the index of the last glyph in that glyph row area.
18935 X is the current output position assigned to the new glyph string
18936 constructed. HL overrides that face of the glyph; e.g. it is
18937 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
18938 x-position of the drawing area. */
18939
18940#define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18941 do { \
18942 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
18943 int face_id = (row)->glyphs[area][START].face_id; \
18944 struct face *base_face = FACE_FROM_ID (f, face_id); \
18945 struct composition *cmp = composition_table[cmp_id]; \
18946 int glyph_len = cmp->glyph_len; \
18947 XChar2b *char2b; \
18948 struct face **faces; \
18949 struct glyph_string *first_s = NULL; \
18950 int n; \
18951 \
18952 base_face = base_face->ascii_face; \
18953 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
18954 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
18955 /* At first, fill in `char2b' and `faces'. */ \
18956 for (n = 0; n < glyph_len; n++) \
18957 { \
18958 int c = COMPOSITION_GLYPH (cmp, n); \
18959 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
18960 faces[n] = FACE_FROM_ID (f, this_face_id); \
18961 get_char_face_and_encoding (f, c, this_face_id, \
18962 char2b + n, 1, 1); \
18963 } \
18964 \
18965 /* Make glyph_strings for each glyph sequence that is drawable by \
18966 the same face, and append them to HEAD/TAIL. */ \
18967 for (n = 0; n < cmp->glyph_len;) \
18968 { \
18969 s = (struct glyph_string *) alloca (sizeof *s); \
18970 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
18971 append_glyph_string (&(HEAD), &(TAIL), s); \
18972 s->cmp = cmp; \
18973 s->gidx = n; \
18974 s->x = (X); \
18975 \
18976 if (n == 0) \
18977 first_s = s; \
18978 \
a66678fd 18979 n = fill_composite_glyph_string (s, faces, overlaps); \
133c764e
KS
18980 } \
18981 \
18982 ++START; \
18983 s = first_s; \
18984 } while (0)
18985
18986
18987/* Build a list of glyph strings between HEAD and TAIL for the glyphs
18988 of AREA of glyph row ROW on window W between indices START and END.
18989 HL overrides the face for drawing glyph strings, e.g. it is
18990 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
18991 x-positions of the drawing area.
18992
18993 This is an ugly monster macro construct because we must use alloca
fa3c6b4d 18994 to allocate glyph strings (because draw_glyphs can be called
133c764e
KS
18995 asynchronously). */
18996
18997#define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18998 do \
18999 { \
19000 HEAD = TAIL = NULL; \
19001 while (START < END) \
19002 { \
19003 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19004 switch (first_glyph->type) \
19005 { \
19006 case CHAR_GLYPH: \
19007 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19008 HL, X, LAST_X); \
19009 break; \
19010 \
19011 case COMPOSITE_GLYPH: \
19012 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19013 HL, X, LAST_X); \
19014 break; \
19015 \
19016 case STRETCH_GLYPH: \
19017 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19018 HL, X, LAST_X); \
19019 break; \
19020 \
19021 case IMAGE_GLYPH: \
19022 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19023 HL, X, LAST_X); \
19024 break; \
19025 \
19026 default: \
19027 abort (); \
19028 } \
19029 \
19030 set_glyph_string_background_width (s, START, LAST_X); \
19031 (X) += s->width; \
19032 } \
19033 } \
19034 while (0)
19035
19036
19037/* Draw glyphs between START and END in AREA of ROW on window W,
19038 starting at x-position X. X is relative to AREA in W. HL is a
19039 face-override with the following meaning:
19040
19041 DRAW_NORMAL_TEXT draw normally
19042 DRAW_CURSOR draw in cursor face
19043 DRAW_MOUSE_FACE draw in mouse face.
19044 DRAW_INVERSE_VIDEO draw in mode line face
19045 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19046 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19047
a66678fd
YM
19048 If OVERLAPS is non-zero, draw only the foreground of characters and
19049 clip to the physical height of ROW. Non-zero value also defines
19050 the overlapping part to be drawn:
19051
19052 OVERLAPS_PRED overlap with preceding rows
19053 OVERLAPS_SUCC overlap with succeeding rows
19054 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19055 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
133c764e
KS
19056
19057 Value is the x-position reached, relative to AREA of W. */
19058
fa3c6b4d 19059static int
a66678fd 19060draw_glyphs (w, x, row, area, start, end, hl, overlaps)
133c764e
KS
19061 struct window *w;
19062 int x;
19063 struct glyph_row *row;
19064 enum glyph_row_area area;
19065 int start, end;
19066 enum draw_glyphs_face hl;
a66678fd 19067 int overlaps;
133c764e
KS
19068{
19069 struct glyph_string *head, *tail;
19070 struct glyph_string *s;
8c2da0fa 19071 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
133c764e
KS
19072 int last_x, area_width;
19073 int x_reached;
19074 int i, j;
19075 struct frame *f = XFRAME (WINDOW_FRAME (w));
19076 DECLARE_HDC (hdc);
19077
19078 ALLOCATE_HDC (hdc, f);
19079
19080 /* Let's rather be paranoid than getting a SEGV. */
19081 end = min (end, row->used[area]);
19082 start = max (0, start);
19083 start = min (end, start);
19084
19085 /* Translate X to frame coordinates. Set last_x to the right
19086 end of the drawing area. */
19087 if (row->full_width_p)
19088 {
19089 /* X is relative to the left edge of W, without scroll bars
19090 or fringes. */
da8b7f4f
KS
19091 x += WINDOW_LEFT_EDGE_X (w);
19092 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
133c764e
KS
19093 }
19094 else
19095 {
da8b7f4f
KS
19096 int area_left = window_box_left (w, area);
19097 x += area_left;
133c764e 19098 area_width = window_box_width (w, area);
da8b7f4f 19099 last_x = area_left + area_width;
133c764e
KS
19100 }
19101
19102 /* Build a doubly-linked list of glyph_string structures between
19103 head and tail from what we have to draw. Note that the macro
19104 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19105 the reason we use a separate variable `i'. */
19106 i = start;
19107 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19108 if (tail)
19109 x_reached = tail->x + tail->background_width;
19110 else
19111 x_reached = x;
19112
19113 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19114 the row, redraw some glyphs in front or following the glyph
19115 strings built above. */
a66678fd 19116 if (head && !overlaps && row->contains_overlapping_glyphs_p)
133c764e
KS
19117 {
19118 int dummy_x = 0;
19119 struct glyph_string *h, *t;
19120
19121 /* Compute overhangs for all glyph strings. */
19122 if (rif->compute_glyph_string_overhangs)
19123 for (s = head; s; s = s->next)
19124 rif->compute_glyph_string_overhangs (s);
19125
19126 /* Prepend glyph strings for glyphs in front of the first glyph
19127 string that are overwritten because of the first glyph
19128 string's left overhang. The background of all strings
19129 prepended must be drawn because the first glyph string
19130 draws over it. */
19131 i = left_overwritten (head);
19132 if (i >= 0)
19133 {
19134 j = i;
19135 BUILD_GLYPH_STRINGS (j, start, h, t,
19136 DRAW_NORMAL_TEXT, dummy_x, last_x);
19137 start = i;
19138 compute_overhangs_and_x (t, head->x, 1);
19139 prepend_glyph_string_lists (&head, &tail, h, t);
8c2da0fa 19140 clip_head = head;
133c764e
KS
19141 }
19142
19143 /* Prepend glyph strings for glyphs in front of the first glyph
19144 string that overwrite that glyph string because of their
19145 right overhang. For these strings, only the foreground must
19146 be drawn, because it draws over the glyph string at `head'.
19147 The background must not be drawn because this would overwrite
19148 right overhangs of preceding glyphs for which no glyph
19149 strings exist. */
19150 i = left_overwriting (head);
19151 if (i >= 0)
19152 {
8c2da0fa 19153 clip_head = head;
133c764e
KS
19154 BUILD_GLYPH_STRINGS (i, start, h, t,
19155 DRAW_NORMAL_TEXT, dummy_x, last_x);
19156 for (s = h; s; s = s->next)
19157 s->background_filled_p = 1;
19158 compute_overhangs_and_x (t, head->x, 1);
19159 prepend_glyph_string_lists (&head, &tail, h, t);
19160 }
19161
19162 /* Append glyphs strings for glyphs following the last glyph
19163 string tail that are overwritten by tail. The background of
19164 these strings has to be drawn because tail's foreground draws
19165 over it. */
19166 i = right_overwritten (tail);
19167 if (i >= 0)
19168 {
19169 BUILD_GLYPH_STRINGS (end, i, h, t,
19170 DRAW_NORMAL_TEXT, x, last_x);
19171 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19172 append_glyph_string_lists (&head, &tail, h, t);
8c2da0fa 19173 clip_tail = tail;
133c764e
KS
19174 }
19175
19176 /* Append glyph strings for glyphs following the last glyph
19177 string tail that overwrite tail. The foreground of such
19178 glyphs has to be drawn because it writes into the background
19179 of tail. The background must not be drawn because it could
19180 paint over the foreground of following glyphs. */
19181 i = right_overwriting (tail);
19182 if (i >= 0)
19183 {
8c2da0fa 19184 clip_tail = tail;
133c764e
KS
19185 BUILD_GLYPH_STRINGS (end, i, h, t,
19186 DRAW_NORMAL_TEXT, x, last_x);
19187 for (s = h; s; s = s->next)
19188 s->background_filled_p = 1;
19189 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19190 append_glyph_string_lists (&head, &tail, h, t);
19191 }
8c2da0fa
ST
19192 if (clip_head || clip_tail)
19193 for (s = head; s; s = s->next)
19194 {
19195 s->clip_head = clip_head;
19196 s->clip_tail = clip_tail;
19197 }
133c764e
KS
19198 }
19199
19200 /* Draw all strings. */
19201 for (s = head; s; s = s->next)
19202 rif->draw_glyph_string (s);
19203
19204 if (area == TEXT_AREA
19205 && !row->full_width_p
19206 /* When drawing overlapping rows, only the glyph strings'
19207 foreground is drawn, which doesn't erase a cursor
19208 completely. */
a66678fd 19209 && !overlaps)
133c764e 19210 {
8c2da0fa
ST
19211 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19212 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19213 : (tail ? tail->x + tail->background_width : x));
133c764e 19214
da8b7f4f
KS
19215 int text_left = window_box_left (w, TEXT_AREA);
19216 x0 -= text_left;
19217 x1 -= text_left;
133c764e 19218
da8b7f4f 19219 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
133c764e
KS
19220 row->y, MATRIX_ROW_BOTTOM_Y (row));
19221 }
19222
19223 /* Value is the x-position up to which drawn, relative to AREA of W.
19224 This doesn't include parts drawn because of overhangs. */
da8b7f4f
KS
19225 if (row->full_width_p)
19226 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19227 else
19228 x_reached -= window_box_left (w, area);
133c764e
KS
19229
19230 RELEASE_HDC (hdc, f);
19231
19232 return x_reached;
19233}
19234
425daf4a
KS
19235/* Expand row matrix if too narrow. Don't expand if area
19236 is not present. */
19237
19238#define IT_EXPAND_MATRIX_WIDTH(it, area) \
19239 { \
19240 if (!fonts_changed_p \
19241 && (it->glyph_row->glyphs[area] \
19242 < it->glyph_row->glyphs[area + 1])) \
19243 { \
19244 it->w->ncols_scale_factor++; \
19245 fonts_changed_p = 1; \
19246 } \
19247 }
133c764e
KS
19248
19249/* Store one glyph for IT->char_to_display in IT->glyph_row.
19250 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19251
19252static INLINE void
19253append_glyph (it)
19254 struct it *it;
19255{
19256 struct glyph *glyph;
19257 enum glyph_row_area area = it->area;
19258
19259 xassert (it->glyph_row);
19260 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
19261
19262 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19263 if (glyph < it->glyph_row->glyphs[area + 1])
19264 {
19265 glyph->charpos = CHARPOS (it->position);
19266 glyph->object = it->object;
19267 glyph->pixel_width = it->pixel_width;
493fdc3c
KS
19268 glyph->ascent = it->ascent;
19269 glyph->descent = it->descent;
133c764e
KS
19270 glyph->voffset = it->voffset;
19271 glyph->type = CHAR_GLYPH;
19272 glyph->multibyte_p = it->multibyte_p;
19273 glyph->left_box_line_p = it->start_of_box_run_p;
19274 glyph->right_box_line_p = it->end_of_box_run_p;
19275 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19276 || it->phys_descent > it->descent);
19277 glyph->padding_p = 0;
19278 glyph->glyph_not_available_p = it->glyph_not_available_p;
19279 glyph->face_id = it->face_id;
19280 glyph->u.ch = it->char_to_display;
2883e85a 19281 glyph->slice = null_glyph_slice;
133c764e
KS
19282 glyph->font_type = FONT_TYPE_UNKNOWN;
19283 ++it->glyph_row->used[area];
19284 }
425daf4a
KS
19285 else
19286 IT_EXPAND_MATRIX_WIDTH (it, area);
133c764e
KS
19287}
19288
19289/* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
19290 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19291
19292static INLINE void
19293append_composite_glyph (it)
19294 struct it *it;
19295{
19296 struct glyph *glyph;
19297 enum glyph_row_area area = it->area;
19298
19299 xassert (it->glyph_row);
19300
19301 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19302 if (glyph < it->glyph_row->glyphs[area + 1])
19303 {
19304 glyph->charpos = CHARPOS (it->position);
19305 glyph->object = it->object;
19306 glyph->pixel_width = it->pixel_width;
493fdc3c
KS
19307 glyph->ascent = it->ascent;
19308 glyph->descent = it->descent;
133c764e
KS
19309 glyph->voffset = it->voffset;
19310 glyph->type = COMPOSITE_GLYPH;
19311 glyph->multibyte_p = it->multibyte_p;
19312 glyph->left_box_line_p = it->start_of_box_run_p;
19313 glyph->right_box_line_p = it->end_of_box_run_p;
19314 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19315 || it->phys_descent > it->descent);
19316 glyph->padding_p = 0;
19317 glyph->glyph_not_available_p = 0;
19318 glyph->face_id = it->face_id;
19319 glyph->u.cmp_id = it->cmp_id;
2883e85a 19320 glyph->slice = null_glyph_slice;
133c764e
KS
19321 glyph->font_type = FONT_TYPE_UNKNOWN;
19322 ++it->glyph_row->used[area];
19323 }
425daf4a
KS
19324 else
19325 IT_EXPAND_MATRIX_WIDTH (it, area);
133c764e
KS
19326}
19327
19328
19329/* Change IT->ascent and IT->height according to the setting of
19330 IT->voffset. */
19331
19332static INLINE void
19333take_vertical_position_into_account (it)
19334 struct it *it;
19335{
19336 if (it->voffset)
19337 {
19338 if (it->voffset < 0)
19339 /* Increase the ascent so that we can display the text higher
19340 in the line. */
e893970b 19341 it->ascent -= it->voffset;
133c764e
KS
19342 else
19343 /* Increase the descent so that we can display the text lower
19344 in the line. */
19345 it->descent += it->voffset;
19346 }
19347}
19348
19349
19350/* Produce glyphs/get display metrics for the image IT is loaded with.
19351 See the description of struct display_iterator in dispextern.h for
19352 an overview of struct display_iterator. */
19353
19354static void
19355produce_image_glyph (it)
19356 struct it *it;
19357{
19358 struct image *img;
19359 struct face *face;
0162fa4e 19360 int glyph_ascent;
e893970b 19361 struct glyph_slice slice;
133c764e
KS
19362
19363 xassert (it->what == IT_IMAGE);
19364
19365 face = FACE_FROM_ID (it->f, it->face_id);
dd341dd9
KS
19366 xassert (face);
19367 /* Make sure X resources of the face is loaded. */
19368 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19369
19370 if (it->image_id < 0)
19371 {
19372 /* Fringe bitmap. */
351b5434
KS
19373 it->ascent = it->phys_ascent = 0;
19374 it->descent = it->phys_descent = 0;
19375 it->pixel_width = 0;
dd341dd9
KS
19376 it->nglyphs = 0;
19377 return;
19378 }
19379
133c764e
KS
19380 img = IMAGE_FROM_ID (it->f, it->image_id);
19381 xassert (img);
dd341dd9 19382 /* Make sure X resources of the image is loaded. */
133c764e
KS
19383 prepare_image_for_display (it->f, img);
19384
e893970b
KS
19385 slice.x = slice.y = 0;
19386 slice.width = img->width;
19387 slice.height = img->height;
19388
19389 if (INTEGERP (it->slice.x))
19390 slice.x = XINT (it->slice.x);
19391 else if (FLOATP (it->slice.x))
19392 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19393
19394 if (INTEGERP (it->slice.y))
19395 slice.y = XINT (it->slice.y);
19396 else if (FLOATP (it->slice.y))
19397 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19398
19399 if (INTEGERP (it->slice.width))
19400 slice.width = XINT (it->slice.width);
19401 else if (FLOATP (it->slice.width))
19402 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19403
19404 if (INTEGERP (it->slice.height))
19405 slice.height = XINT (it->slice.height);
19406 else if (FLOATP (it->slice.height))
19407 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
19408
19409 if (slice.x >= img->width)
19410 slice.x = img->width;
19411 if (slice.y >= img->height)
19412 slice.y = img->height;
19413 if (slice.x + slice.width >= img->width)
19414 slice.width = img->width - slice.x;
19415 if (slice.y + slice.height > img->height)
19416 slice.height = img->height - slice.y;
19417
19418 if (slice.width == 0 || slice.height == 0)
19419 return;
19420
19421 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
19422
19423 it->descent = slice.height - glyph_ascent;
19424 if (slice.y == 0)
19425 it->descent += img->vmargin;
19426 if (slice.y + slice.height == img->height)
19427 it->descent += img->vmargin;
19428 it->phys_descent = it->descent;
19429
19430 it->pixel_width = slice.width;
19431 if (slice.x == 0)
19432 it->pixel_width += img->hmargin;
19433 if (slice.x + slice.width == img->width)
19434 it->pixel_width += img->hmargin;
133c764e 19435
7d8a0b55
MB
19436 /* It's quite possible for images to have an ascent greater than
19437 their height, so don't get confused in that case. */
19438 if (it->descent < 0)
19439 it->descent = 0;
19440
e893970b 19441#if 0 /* this breaks image tiling */
493fdc3c 19442 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
0162fa4e 19443 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
493fdc3c
KS
19444 if (face_ascent > it->ascent)
19445 it->ascent = it->phys_ascent = face_ascent;
e893970b 19446#endif
493fdc3c 19447
133c764e
KS
19448 it->nglyphs = 1;
19449
19450 if (face->box != FACE_NO_BOX)
19451 {
19452 if (face->box_line_width > 0)
19453 {
e893970b
KS
19454 if (slice.y == 0)
19455 it->ascent += face->box_line_width;
19456 if (slice.y + slice.height == img->height)
19457 it->descent += face->box_line_width;
133c764e
KS
19458 }
19459
e893970b 19460 if (it->start_of_box_run_p && slice.x == 0)
133c764e 19461 it->pixel_width += abs (face->box_line_width);
e893970b 19462 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
133c764e
KS
19463 it->pixel_width += abs (face->box_line_width);
19464 }
19465
19466 take_vertical_position_into_account (it);
19467
19468 if (it->glyph_row)
19469 {
19470 struct glyph *glyph;
19471 enum glyph_row_area area = it->area;
19472
19473 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19474 if (glyph < it->glyph_row->glyphs[area + 1])
19475 {
19476 glyph->charpos = CHARPOS (it->position);
19477 glyph->object = it->object;
19478 glyph->pixel_width = it->pixel_width;
493fdc3c
KS
19479 glyph->ascent = glyph_ascent;
19480 glyph->descent = it->descent;
133c764e
KS
19481 glyph->voffset = it->voffset;
19482 glyph->type = IMAGE_GLYPH;
19483 glyph->multibyte_p = it->multibyte_p;
19484 glyph->left_box_line_p = it->start_of_box_run_p;
19485 glyph->right_box_line_p = it->end_of_box_run_p;
19486 glyph->overlaps_vertically_p = 0;
19487 glyph->padding_p = 0;
19488 glyph->glyph_not_available_p = 0;
19489 glyph->face_id = it->face_id;
19490 glyph->u.img_id = img->id;
e893970b 19491 glyph->slice = slice;
133c764e
KS
19492 glyph->font_type = FONT_TYPE_UNKNOWN;
19493 ++it->glyph_row->used[area];
19494 }
425daf4a
KS
19495 else
19496 IT_EXPAND_MATRIX_WIDTH (it, area);
133c764e
KS
19497 }
19498}
19499
19500
19501/* Append a stretch glyph to IT->glyph_row. OBJECT is the source
19502 of the glyph, WIDTH and HEIGHT are the width and height of the
f65536fa 19503 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
133c764e
KS
19504
19505static void
19506append_stretch_glyph (it, object, width, height, ascent)
19507 struct it *it;
19508 Lisp_Object object;
19509 int width, height;
f65536fa 19510 int ascent;
133c764e
KS
19511{
19512 struct glyph *glyph;
19513 enum glyph_row_area area = it->area;
19514
f65536fa 19515 xassert (ascent >= 0 && ascent <= height);
133c764e
KS
19516
19517 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19518 if (glyph < it->glyph_row->glyphs[area + 1])
19519 {
19520 glyph->charpos = CHARPOS (it->position);
19521 glyph->object = object;
19522 glyph->pixel_width = width;
493fdc3c
KS
19523 glyph->ascent = ascent;
19524 glyph->descent = height - ascent;
133c764e
KS
19525 glyph->voffset = it->voffset;
19526 glyph->type = STRETCH_GLYPH;
19527 glyph->multibyte_p = it->multibyte_p;
19528 glyph->left_box_line_p = it->start_of_box_run_p;
19529 glyph->right_box_line_p = it->end_of_box_run_p;
19530 glyph->overlaps_vertically_p = 0;
19531 glyph->padding_p = 0;
19532 glyph->glyph_not_available_p = 0;
19533 glyph->face_id = it->face_id;
f65536fa 19534 glyph->u.stretch.ascent = ascent;
133c764e 19535 glyph->u.stretch.height = height;
2883e85a 19536 glyph->slice = null_glyph_slice;
133c764e
KS
19537 glyph->font_type = FONT_TYPE_UNKNOWN;
19538 ++it->glyph_row->used[area];
19539 }
425daf4a
KS
19540 else
19541 IT_EXPAND_MATRIX_WIDTH (it, area);
133c764e
KS
19542}
19543
19544
19545/* Produce a stretch glyph for iterator IT. IT->object is the value
19546 of the glyph property displayed. The value must be a list
19547 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
19548 being recognized:
19549
19550 1. `:width WIDTH' specifies that the space should be WIDTH *
19551 canonical char width wide. WIDTH may be an integer or floating
19552 point number.
19553
19554 2. `:relative-width FACTOR' specifies that the width of the stretch
19555 should be computed from the width of the first character having the
19556 `glyph' property, and should be FACTOR times that width.
19557
19558 3. `:align-to HPOS' specifies that the space should be wide enough
19559 to reach HPOS, a value in canonical character units.
19560
19561 Exactly one of the above pairs must be present.
19562
19563 4. `:height HEIGHT' specifies that the height of the stretch produced
19564 should be HEIGHT, measured in canonical character units.
19565
19566 5. `:relative-height FACTOR' specifies that the height of the
19567 stretch should be FACTOR times the height of the characters having
19568 the glyph property.
19569
19570 Either none or exactly one of 4 or 5 must be present.
19571
19572 6. `:ascent ASCENT' specifies that ASCENT percent of the height
19573 of the stretch should be used for the ascent of the stretch.
19574 ASCENT must be in the range 0 <= ASCENT <= 100. */
19575
133c764e
KS
19576static void
19577produce_stretch_glyph (it)
19578 struct it *it;
19579{
f65536fa 19580 /* (space :width WIDTH :height HEIGHT ...) */
133c764e 19581 Lisp_Object prop, plist;
da06e4d7 19582 int width = 0, height = 0, align_to = -1;
f65536fa
KS
19583 int zero_width_ok_p = 0, zero_height_ok_p = 0;
19584 int ascent = 0;
19585 double tem;
133c764e
KS
19586 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19587 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
19588
19589 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19590
19591 /* List should start with `space'. */
19592 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
19593 plist = XCDR (it->object);
19594
19595 /* Compute the width of the stretch. */
bccf59df 19596 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
da06e4d7 19597 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
f65536fa
KS
19598 {
19599 /* Absolute width `:width WIDTH' specified and valid. */
19600 zero_width_ok_p = 1;
19601 width = (int)tem;
19602 }
bccf59df 19603 else if (prop = Fplist_get (plist, QCrelative_width),
133c764e
KS
19604 NUMVAL (prop) > 0)
19605 {
19606 /* Relative width `:relative-width FACTOR' specified and valid.
19607 Compute the width of the characters having the `glyph'
19608 property. */
19609 struct it it2;
19610 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
19611
19612 it2 = *it;
19613 if (it->multibyte_p)
19614 {
19615 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
19616 - IT_BYTEPOS (*it));
19617 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
19618 }
19619 else
19620 it2.c = *p, it2.len = 1;
19621
19622 it2.glyph_row = NULL;
19623 it2.what = IT_CHARACTER;
19624 x_produce_glyphs (&it2);
19625 width = NUMVAL (prop) * it2.pixel_width;
19626 }
bccf59df 19627 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
da06e4d7
KS
19628 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
19629 {
19630 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
be21b925 19631 align_to = (align_to < 0
da06e4d7
KS
19632 ? 0
19633 : align_to - window_box_left_offset (it->w, TEXT_AREA));
19634 else if (align_to < 0)
19635 align_to = window_box_left_offset (it->w, TEXT_AREA);
19636 width = max (0, (int)tem + align_to - it->current_x);
f65536fa
KS
19637 zero_width_ok_p = 1;
19638 }
133c764e
KS
19639 else
19640 /* Nothing specified -> width defaults to canonical char width. */
da8b7f4f 19641 width = FRAME_COLUMN_WIDTH (it->f);
133c764e 19642
f65536fa
KS
19643 if (width <= 0 && (width < 0 || !zero_width_ok_p))
19644 width = 1;
19645
133c764e 19646 /* Compute height. */
bccf59df 19647 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
da06e4d7 19648 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
f65536fa
KS
19649 {
19650 height = (int)tem;
19651 zero_height_ok_p = 1;
19652 }
bccf59df 19653 else if (prop = Fplist_get (plist, QCrelative_height),
133c764e
KS
19654 NUMVAL (prop) > 0)
19655 height = FONT_HEIGHT (font) * NUMVAL (prop);
19656 else
19657 height = FONT_HEIGHT (font);
19658
f65536fa
KS
19659 if (height <= 0 && (height < 0 || !zero_height_ok_p))
19660 height = 1;
19661
133c764e
KS
19662 /* Compute percentage of height used for ascent. If
19663 `:ascent ASCENT' is present and valid, use that. Otherwise,
19664 derive the ascent from the font in use. */
bccf59df 19665 if (prop = Fplist_get (plist, QCascent),
133c764e 19666 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
f65536fa
KS
19667 ascent = height * NUMVAL (prop) / 100.0;
19668 else if (!NILP (prop)
da06e4d7 19669 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
f65536fa 19670 ascent = min (max (0, (int)tem), height);
133c764e 19671 else
f65536fa 19672 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
133c764e 19673
f65536fa 19674 if (width > 0 && height > 0 && it->glyph_row)
133c764e
KS
19675 {
19676 Lisp_Object object = it->stack[it->sp - 1].string;
19677 if (!STRINGP (object))
19678 object = it->w->buffer;
19679 append_stretch_glyph (it, object, width, height, ascent);
19680 }
19681
19682 it->pixel_width = width;
f65536fa 19683 it->ascent = it->phys_ascent = ascent;
133c764e 19684 it->descent = it->phys_descent = height - it->ascent;
f65536fa 19685 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
133c764e 19686
f65536fa 19687 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
133c764e
KS
19688 {
19689 if (face->box_line_width > 0)
19690 {
19691 it->ascent += face->box_line_width;
19692 it->descent += face->box_line_width;
19693 }
19694
19695 if (it->start_of_box_run_p)
19696 it->pixel_width += abs (face->box_line_width);
19697 if (it->end_of_box_run_p)
19698 it->pixel_width += abs (face->box_line_width);
19699 }
19700
19701 take_vertical_position_into_account (it);
19702}
19703
99c71e51
KS
19704/* Get line-height and line-spacing property at point.
19705 If line-height has format (HEIGHT TOTAL), return TOTAL
19706 in TOTAL_HEIGHT. */
a9e5c932
KS
19707
19708static Lisp_Object
99c71e51 19709get_line_height_property (it, prop)
a9e5c932
KS
19710 struct it *it;
19711 Lisp_Object prop;
a9e5c932 19712{
b78ab259 19713 Lisp_Object position;
a9e5c932 19714
7e1704d0
KS
19715 if (STRINGP (it->object))
19716 position = make_number (IT_STRING_CHARPOS (*it));
16fe48b4 19717 else if (BUFFERP (it->object))
7e1704d0 19718 position = make_number (IT_CHARPOS (*it));
16fe48b4
KS
19719 else
19720 return Qnil;
7e1704d0 19721
99c71e51
KS
19722 return Fget_char_property (position, prop, it->object);
19723}
a9e5c932 19724
99c71e51
KS
19725/* Calculate line-height and line-spacing properties.
19726 An integer value specifies explicit pixel value.
19727 A float value specifies relative value to current face height.
19728 A cons (float . face-name) specifies relative value to
19729 height of specified face font.
a9e5c932 19730
99c71e51 19731 Returns height in pixels, or nil. */
17b01ffc 19732
99c71e51
KS
19733
19734static Lisp_Object
19735calc_line_height_property (it, val, font, boff, override)
19736 struct it *it;
19737 Lisp_Object val;
19738 XFontStruct *font;
19739 int boff, override;
19740{
19741 Lisp_Object face_name = Qnil;
19742 int ascent, descent, height;
19743
19744 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
a9e5c932
KS
19745 return val;
19746
19747 if (CONSP (val))
19748 {
99c71e51
KS
19749 face_name = XCAR (val);
19750 val = XCDR (val);
19751 if (!NUMBERP (val))
19752 val = make_number (1);
19753 if (NILP (face_name))
19754 {
19755 height = it->ascent + it->descent;
19756 goto scale;
19757 }
a9e5c932
KS
19758 }
19759
a9e5c932
KS
19760 if (NILP (face_name))
19761 {
19762 font = FRAME_FONT (it->f);
19763 boff = FRAME_BASELINE_OFFSET (it->f);
19764 }
19765 else if (EQ (face_name, Qt))
19766 {
19767 override = 0;
19768 }
19769 else
19770 {
19771 int face_id;
19772 struct face *face;
19773 struct font_info *font_info;
19774
ff7d52cd 19775 face_id = lookup_named_face (it->f, face_name, ' ', 0);
a9e5c932 19776 if (face_id < 0)
d9bf7950 19777 return make_number (-1);
a9e5c932
KS
19778
19779 face = FACE_FROM_ID (it->f, face_id);
19780 font = face->font;
19781 if (font == NULL)
d9bf7950 19782 return make_number (-1);
a9e5c932
KS
19783
19784 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19785 boff = font_info->baseline_offset;
19786 if (font_info->vertical_centering)
19787 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19788 }
19789
19790 ascent = FONT_BASE (font) + boff;
19791 descent = FONT_DESCENT (font) - boff;
19792
19793 if (override)
19794 {
19795 it->override_ascent = ascent;
19796 it->override_descent = descent;
19797 it->override_boff = boff;
19798 }
19799
19800 height = ascent + descent;
99c71e51
KS
19801
19802 scale:
a9e5c932
KS
19803 if (FLOATP (val))
19804 height = (int)(XFLOAT_DATA (val) * height);
19805 else if (INTEGERP (val))
19806 height *= XINT (val);
19807
19808 return make_number (height);
19809}
19810
19811
133c764e
KS
19812/* RIF:
19813 Produce glyphs/get display metrics for the display element IT is
19814 loaded with. See the description of struct display_iterator in
19815 dispextern.h for an overview of struct display_iterator. */
19816
19817void
19818x_produce_glyphs (it)
19819 struct it *it;
19820{
03e35edc
KS
19821 int extra_line_spacing = it->extra_line_spacing;
19822
133c764e
KS
19823 it->glyph_not_available_p = 0;
19824
19825 if (it->what == IT_CHARACTER)
19826 {
19827 XChar2b char2b;
19828 XFontStruct *font;
19829 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19830 XCharStruct *pcm;
19831 int font_not_found_p;
19832 struct font_info *font_info;
19833 int boff; /* baseline offset */
19834 /* We may change it->multibyte_p upon unibyte<->multibyte
19835 conversion. So, save the current value now and restore it
19836 later.
19837
19838 Note: It seems that we don't have to record multibyte_p in
19839 struct glyph because the character code itself tells if or
19840 not the character is multibyte. Thus, in the future, we must
19841 consider eliminating the field `multibyte_p' in the struct
19842 glyph. */
19843 int saved_multibyte_p = it->multibyte_p;
19844
19845 /* Maybe translate single-byte characters to multibyte, or the
19846 other way. */
19847 it->char_to_display = it->c;
19848 if (!ASCII_BYTE_P (it->c))
19849 {
19850 if (unibyte_display_via_language_environment
19851 && SINGLE_BYTE_CHAR_P (it->c)
19852 && (it->c >= 0240
19853 || !NILP (Vnonascii_translation_table)))
19854 {
19855 it->char_to_display = unibyte_char_to_multibyte (it->c);
19856 it->multibyte_p = 1;
19857 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19858 face = FACE_FROM_ID (it->f, it->face_id);
19859 }
19860 else if (!SINGLE_BYTE_CHAR_P (it->c)
19861 && !it->multibyte_p)
19862 {
19863 it->multibyte_p = 1;
19864 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19865 face = FACE_FROM_ID (it->f, it->face_id);
19866 }
19867 }
19868
19869 /* Get font to use. Encode IT->char_to_display. */
19870 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19871 &char2b, it->multibyte_p, 0);
19872 font = face->font;
19873
19874 /* When no suitable font found, use the default font. */
19875 font_not_found_p = font == NULL;
19876 if (font_not_found_p)
19877 {
19878 font = FRAME_FONT (it->f);
19879 boff = FRAME_BASELINE_OFFSET (it->f);
19880 font_info = NULL;
19881 }
19882 else
19883 {
19884 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19885 boff = font_info->baseline_offset;
19886 if (font_info->vertical_centering)
19887 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19888 }
19889
19890 if (it->char_to_display >= ' '
19891 && (!it->multibyte_p || it->char_to_display < 128))
19892 {
19893 /* Either unibyte or ASCII. */
19894 int stretched_p;
19895
19896 it->nglyphs = 1;
19897
19898 pcm = rif->per_char_metric (font, &char2b,
19899 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
e893970b 19900
a9e5c932
KS
19901 if (it->override_ascent >= 0)
19902 {
19903 it->ascent = it->override_ascent;
19904 it->descent = it->override_descent;
19905 boff = it->override_boff;
19906 }
19907 else
19908 {
19909 it->ascent = FONT_BASE (font) + boff;
19910 it->descent = FONT_DESCENT (font) - boff;
19911 }
133c764e
KS
19912
19913 if (pcm)
19914 {
19915 it->phys_ascent = pcm->ascent + boff;
19916 it->phys_descent = pcm->descent - boff;
19917 it->pixel_width = pcm->width;
19918 }
19919 else
19920 {
19921 it->glyph_not_available_p = 1;
e893970b
KS
19922 it->phys_ascent = it->ascent;
19923 it->phys_descent = it->descent;
133c764e
KS
19924 it->pixel_width = FONT_WIDTH (font);
19925 }
19926
e893970b
KS
19927 if (it->constrain_row_ascent_descent_p)
19928 {
19929 if (it->descent > it->max_descent)
4933c603
KS
19930 {
19931 it->ascent += it->descent - it->max_descent;
19932 it->descent = it->max_descent;
19933 }
19934 if (it->ascent > it->max_ascent)
19935 {
19936 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19937 it->ascent = it->max_ascent;
19938 }
19939 it->phys_ascent = min (it->phys_ascent, it->ascent);
19940 it->phys_descent = min (it->phys_descent, it->descent);
19941 extra_line_spacing = 0;
19942 }
e893970b 19943
133c764e
KS
19944 /* If this is a space inside a region of text with
19945 `space-width' property, change its width. */
19946 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
19947 if (stretched_p)
19948 it->pixel_width *= XFLOATINT (it->space_width);
19949
19950 /* If face has a box, add the box thickness to the character
19951 height. If character has a box line to the left and/or
19952 right, add the box line width to the character's width. */
19953 if (face->box != FACE_NO_BOX)
19954 {
19955 int thick = face->box_line_width;
19956
19957 if (thick > 0)
19958 {
19959 it->ascent += thick;
19960 it->descent += thick;
19961 }
19962 else
19963 thick = -thick;
19964
19965 if (it->start_of_box_run_p)
19966 it->pixel_width += thick;
19967 if (it->end_of_box_run_p)
19968 it->pixel_width += thick;
19969 }
19970
19971 /* If face has an overline, add the height of the overline
19972 (1 pixel) and a 1 pixel margin to the character height. */
19973 if (face->overline_p)
19974 it->ascent += 2;
19975
e893970b
KS
19976 if (it->constrain_row_ascent_descent_p)
19977 {
19978 if (it->ascent > it->max_ascent)
19979 it->ascent = it->max_ascent;
19980 if (it->descent > it->max_descent)
19981 it->descent = it->max_descent;
19982 }
19983
133c764e
KS
19984 take_vertical_position_into_account (it);
19985
19986 /* If we have to actually produce glyphs, do it. */
19987 if (it->glyph_row)
19988 {
19989 if (stretched_p)
19990 {
19991 /* Translate a space with a `space-width' property
19992 into a stretch glyph. */
f65536fa
KS
19993 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
19994 / FONT_HEIGHT (font));
133c764e
KS
19995 append_stretch_glyph (it, it->object, it->pixel_width,
19996 it->ascent + it->descent, ascent);
19997 }
19998 else
19999 append_glyph (it);
20000
20001 /* If characters with lbearing or rbearing are displayed
20002 in this line, record that fact in a flag of the
20003 glyph row. This is used to optimize X output code. */
20004 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20005 it->glyph_row->contains_overlapping_glyphs_p = 1;
20006 }
20007 }
20008 else if (it->char_to_display == '\n')
20009 {
e893970b
KS
20010 /* A newline has no width but we need the height of the line.
20011 But if previous part of the line set a height, don't
20012 increase that height */
20013
17b01ffc 20014 Lisp_Object height;
99c71e51 20015 Lisp_Object total_height = Qnil;
03e35edc 20016
a9e5c932 20017 it->override_ascent = -1;
133c764e
KS
20018 it->pixel_width = 0;
20019 it->nglyphs = 0;
133c764e 20020
99c71e51
KS
20021 height = get_line_height_property(it, Qline_height);
20022 /* Split (line-height total-height) list */
29aa4ce7
KS
20023 if (CONSP (height)
20024 && CONSP (XCDR (height))
20025 && NILP (XCDR (XCDR (height))))
99c71e51
KS
20026 {
20027 total_height = XCAR (XCDR (height));
20028 height = XCAR (height);
20029 }
20030 height = calc_line_height_property(it, height, font, boff, 1);
4933c603 20031
a9e5c932 20032 if (it->override_ascent >= 0)
4933c603 20033 {
a9e5c932
KS
20034 it->ascent = it->override_ascent;
20035 it->descent = it->override_descent;
20036 boff = it->override_boff;
20037 }
20038 else
20039 {
20040 it->ascent = FONT_BASE (font) + boff;
20041 it->descent = FONT_DESCENT (font) - boff;
4933c603 20042 }
03e35edc 20043
99c71e51 20044 if (EQ (height, Qt))
e893970b 20045 {
03e35edc
KS
20046 if (it->descent > it->max_descent)
20047 {
20048 it->ascent += it->descent - it->max_descent;
20049 it->descent = it->max_descent;
20050 }
4933c603 20051 if (it->ascent > it->max_ascent)
03e35edc
KS
20052 {
20053 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20054 it->ascent = it->max_ascent;
20055 }
20056 it->phys_ascent = min (it->phys_ascent, it->ascent);
20057 it->phys_descent = min (it->phys_descent, it->descent);
4933c603 20058 it->constrain_row_ascent_descent_p = 1;
03e35edc 20059 extra_line_spacing = 0;
e893970b
KS
20060 }
20061 else
20062 {
17b01ffc 20063 Lisp_Object spacing;
17b01ffc 20064
03e35edc
KS
20065 it->phys_ascent = it->ascent;
20066 it->phys_descent = it->descent;
e893970b 20067
03e35edc
KS
20068 if ((it->max_ascent > 0 || it->max_descent > 0)
20069 && face->box != FACE_NO_BOX
20070 && face->box_line_width > 0)
20071 {
20072 it->ascent += face->box_line_width;
20073 it->descent += face->box_line_width;
20074 }
a9e5c932
KS
20075 if (!NILP (height)
20076 && XINT (height) > it->ascent + it->descent)
20077 it->ascent = XINT (height) - it->descent;
4933c603 20078
99c71e51
KS
20079 if (!NILP (total_height))
20080 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20081 else
20082 {
20083 spacing = get_line_height_property(it, Qline_spacing);
20084 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20085 }
17b01ffc
KS
20086 if (INTEGERP (spacing))
20087 {
20088 extra_line_spacing = XINT (spacing);
99c71e51 20089 if (!NILP (total_height))
17b01ffc
KS
20090 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20091 }
a9e5c932 20092 }
133c764e
KS
20093 }
20094 else if (it->char_to_display == '\t')
20095 {
7335e61a 20096 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
133c764e
KS
20097 int x = it->current_x + it->continuation_lines_width;
20098 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20099
20100 /* If the distance from the current position to the next tab
7335e61a 20101 stop is less than a space character width, use the
133c764e 20102 tab stop after that. */
7335e61a 20103 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
133c764e
KS
20104 next_tab_x += tab_width;
20105
20106 it->pixel_width = next_tab_x - x;
20107 it->nglyphs = 1;
20108 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20109 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20110
20111 if (it->glyph_row)
20112 {
133c764e 20113 append_stretch_glyph (it, it->object, it->pixel_width,
f65536fa 20114 it->ascent + it->descent, it->ascent);
133c764e
KS
20115 }
20116 }
20117 else
20118 {
20119 /* A multi-byte character. Assume that the display width of the
20120 character is the width of the character multiplied by the
20121 width of the font. */
20122
20123 /* If we found a font, this font should give us the right
20124 metrics. If we didn't find a font, use the frame's
20125 default font and calculate the width of the character
20126 from the charset width; this is what old redisplay code
20127 did. */
20128
20129 pcm = rif->per_char_metric (font, &char2b,
20130 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20131
20132 if (font_not_found_p || !pcm)
20133 {
20134 int charset = CHAR_CHARSET (it->char_to_display);
20135
20136 it->glyph_not_available_p = 1;
da8b7f4f 20137 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
133c764e
KS
20138 * CHARSET_WIDTH (charset));
20139 it->phys_ascent = FONT_BASE (font) + boff;
20140 it->phys_descent = FONT_DESCENT (font) - boff;
20141 }
20142 else
20143 {
20144 it->pixel_width = pcm->width;
20145 it->phys_ascent = pcm->ascent + boff;
20146 it->phys_descent = pcm->descent - boff;
20147 if (it->glyph_row
20148 && (pcm->lbearing < 0
20149 || pcm->rbearing > pcm->width))
20150 it->glyph_row->contains_overlapping_glyphs_p = 1;
20151 }
20152 it->nglyphs = 1;
20153 it->ascent = FONT_BASE (font) + boff;
20154 it->descent = FONT_DESCENT (font) - boff;
20155 if (face->box != FACE_NO_BOX)
20156 {
20157 int thick = face->box_line_width;
20158
20159 if (thick > 0)
20160 {
20161 it->ascent += thick;
20162 it->descent += thick;
20163 }
20164 else
20165 thick = - thick;
20166
20167 if (it->start_of_box_run_p)
20168 it->pixel_width += thick;
20169 if (it->end_of_box_run_p)
20170 it->pixel_width += thick;
20171 }
20172
20173 /* If face has an overline, add the height of the overline
20174 (1 pixel) and a 1 pixel margin to the character height. */
20175 if (face->overline_p)
20176 it->ascent += 2;
20177
20178 take_vertical_position_into_account (it);
20179
20180 if (it->glyph_row)
20181 append_glyph (it);
20182 }
20183 it->multibyte_p = saved_multibyte_p;
20184 }
20185 else if (it->what == IT_COMPOSITION)
20186 {
20187 /* Note: A composition is represented as one glyph in the
20188 glyph matrix. There are no padding glyphs. */
20189 XChar2b char2b;
20190 XFontStruct *font;
20191 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20192 XCharStruct *pcm;
20193 int font_not_found_p;
20194 struct font_info *font_info;
20195 int boff; /* baseline offset */
20196 struct composition *cmp = composition_table[it->cmp_id];
20197
20198 /* Maybe translate single-byte characters to multibyte. */
20199 it->char_to_display = it->c;
20200 if (unibyte_display_via_language_environment
20201 && SINGLE_BYTE_CHAR_P (it->c)
20202 && (it->c >= 0240
20203 || (it->c >= 0200
20204 && !NILP (Vnonascii_translation_table))))
20205 {
20206 it->char_to_display = unibyte_char_to_multibyte (it->c);
20207 }
20208
20209 /* Get face and font to use. Encode IT->char_to_display. */
20210 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20211 face = FACE_FROM_ID (it->f, it->face_id);
20212 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20213 &char2b, it->multibyte_p, 0);
20214 font = face->font;
20215
20216 /* When no suitable font found, use the default font. */
20217 font_not_found_p = font == NULL;
20218 if (font_not_found_p)
20219 {
20220 font = FRAME_FONT (it->f);
20221 boff = FRAME_BASELINE_OFFSET (it->f);
20222 font_info = NULL;
20223 }
20224 else
20225 {
20226 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20227 boff = font_info->baseline_offset;
20228 if (font_info->vertical_centering)
20229 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20230 }
20231
20232 /* There are no padding glyphs, so there is only one glyph to
20233 produce for the composition. Important is that pixel_width,
20234 ascent and descent are the values of what is drawn by
20235 draw_glyphs (i.e. the values of the overall glyphs composed). */
20236 it->nglyphs = 1;
20237
20238 /* If we have not yet calculated pixel size data of glyphs of
20239 the composition for the current face font, calculate them
20240 now. Theoretically, we have to check all fonts for the
20241 glyphs, but that requires much time and memory space. So,
20242 here we check only the font of the first glyph. This leads
20243 to incorrect display very rarely, and C-l (recenter) can
20244 correct the display anyway. */
20245 if (cmp->font != (void *) font)
20246 {
20247 /* Ascent and descent of the font of the first character of
20248 this composition (adjusted by baseline offset). Ascent
20249 and descent of overall glyphs should not be less than
20250 them respectively. */
20251 int font_ascent = FONT_BASE (font) + boff;
20252 int font_descent = FONT_DESCENT (font) - boff;
20253 /* Bounding box of the overall glyphs. */
20254 int leftmost, rightmost, lowest, highest;
20255 int i, width, ascent, descent;
20256
20257 cmp->font = (void *) font;
20258
20259 /* Initialize the bounding box. */
20260 if (font_info
20261 && (pcm = rif->per_char_metric (font, &char2b,
20262 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
20263 {
20264 width = pcm->width;
20265 ascent = pcm->ascent;
20266 descent = pcm->descent;
20267 }
20268 else
20269 {
20270 width = FONT_WIDTH (font);
20271 ascent = FONT_BASE (font);
20272 descent = FONT_DESCENT (font);
20273 }
20274
20275 rightmost = width;
20276 lowest = - descent + boff;
20277 highest = ascent + boff;
20278 leftmost = 0;
20279
20280 if (font_info
20281 && font_info->default_ascent
20282 && CHAR_TABLE_P (Vuse_default_ascent)
20283 && !NILP (Faref (Vuse_default_ascent,
20284 make_number (it->char_to_display))))
20285 highest = font_info->default_ascent + boff;
20286
20287 /* Draw the first glyph at the normal position. It may be
20288 shifted to right later if some other glyphs are drawn at
20289 the left. */
20290 cmp->offsets[0] = 0;
20291 cmp->offsets[1] = boff;
20292
20293 /* Set cmp->offsets for the remaining glyphs. */
20294 for (i = 1; i < cmp->glyph_len; i++)
20295 {
20296 int left, right, btm, top;
20297 int ch = COMPOSITION_GLYPH (cmp, i);
20298 int face_id = FACE_FOR_CHAR (it->f, face, ch);
20299
20300 face = FACE_FROM_ID (it->f, face_id);
20301 get_char_face_and_encoding (it->f, ch, face->id,
20302 &char2b, it->multibyte_p, 0);
20303 font = face->font;
20304 if (font == NULL)
20305 {
20306 font = FRAME_FONT (it->f);
2a6d0874 20307 boff = FRAME_BASELINE_OFFSET (it->f);
133c764e
KS
20308 font_info = NULL;
20309 }
20310 else
20311 {
20312 font_info
20313 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20314 boff = font_info->baseline_offset;
20315 if (font_info->vertical_centering)
20316 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20317 }
20318
20319 if (font_info
2a6d0874 20320 && (pcm = rif->per_char_metric (font, &char2b,
133c764e
KS
20321 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
20322 {
20323 width = pcm->width;
20324 ascent = pcm->ascent;
20325 descent = pcm->descent;
20326 }
20327 else
20328 {
20329 width = FONT_WIDTH (font);
20330 ascent = 1;
20331 descent = 0;
20332 }
20333
20334 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
20335 {
20336 /* Relative composition with or without
20337 alternate chars. */
20338 left = (leftmost + rightmost - width) / 2;
20339 btm = - descent + boff;
20340 if (font_info && font_info->relative_compose
20341 && (! CHAR_TABLE_P (Vignore_relative_composition)
20342 || NILP (Faref (Vignore_relative_composition,
20343 make_number (ch)))))
20344 {
20345
20346 if (- descent >= font_info->relative_compose)
20347 /* One extra pixel between two glyphs. */
20348 btm = highest + 1;
20349 else if (ascent <= 0)
20350 /* One extra pixel between two glyphs. */
20351 btm = lowest - 1 - ascent - descent;
20352 }
20353 }
20354 else
20355 {
20356 /* A composition rule is specified by an integer
20357 value that encodes global and new reference
20358 points (GREF and NREF). GREF and NREF are
20359 specified by numbers as below:
20360
20361 0---1---2 -- ascent
20362 | |
20363 | |
20364 | |
20365 9--10--11 -- center
20366 | |
20367 ---3---4---5--- baseline
20368 | |
20369 6---7---8 -- descent
20370 */
20371 int rule = COMPOSITION_RULE (cmp, i);
20372 int gref, nref, grefx, grefy, nrefx, nrefy;
20373
20374 COMPOSITION_DECODE_RULE (rule, gref, nref);
20375 grefx = gref % 3, nrefx = nref % 3;
20376 grefy = gref / 3, nrefy = nref / 3;
20377
20378 left = (leftmost
20379 + grefx * (rightmost - leftmost) / 2
20380 - nrefx * width / 2);
20381 btm = ((grefy == 0 ? highest
20382 : grefy == 1 ? 0
20383 : grefy == 2 ? lowest
20384 : (highest + lowest) / 2)
20385 - (nrefy == 0 ? ascent + descent
20386 : nrefy == 1 ? descent - boff
20387 : nrefy == 2 ? 0
20388 : (ascent + descent) / 2));
20389 }
20390
20391 cmp->offsets[i * 2] = left;
20392 cmp->offsets[i * 2 + 1] = btm + descent;
20393
20394 /* Update the bounding box of the overall glyphs. */
20395 right = left + width;
20396 top = btm + descent + ascent;
20397 if (left < leftmost)
20398 leftmost = left;
20399 if (right > rightmost)
20400 rightmost = right;
20401 if (top > highest)
20402 highest = top;
20403 if (btm < lowest)
20404 lowest = btm;
20405 }
20406
20407 /* If there are glyphs whose x-offsets are negative,
20408 shift all glyphs to the right and make all x-offsets
20409 non-negative. */
20410 if (leftmost < 0)
20411 {
20412 for (i = 0; i < cmp->glyph_len; i++)
20413 cmp->offsets[i * 2] -= leftmost;
20414 rightmost -= leftmost;
20415 }
20416
20417 cmp->pixel_width = rightmost;
20418 cmp->ascent = highest;
20419 cmp->descent = - lowest;
20420 if (cmp->ascent < font_ascent)
20421 cmp->ascent = font_ascent;
20422 if (cmp->descent < font_descent)
20423 cmp->descent = font_descent;
20424 }
20425
20426 it->pixel_width = cmp->pixel_width;
20427 it->ascent = it->phys_ascent = cmp->ascent;
20428 it->descent = it->phys_descent = cmp->descent;
20429
20430 if (face->box != FACE_NO_BOX)
20431 {
20432 int thick = face->box_line_width;
20433
20434 if (thick > 0)
20435 {
20436 it->ascent += thick;
20437 it->descent += thick;
20438 }
20439 else
20440 thick = - thick;
20441
20442 if (it->start_of_box_run_p)
20443 it->pixel_width += thick;
20444 if (it->end_of_box_run_p)
20445 it->pixel_width += thick;
20446 }
20447
20448 /* If face has an overline, add the height of the overline
20449 (1 pixel) and a 1 pixel margin to the character height. */
20450 if (face->overline_p)
20451 it->ascent += 2;
20452
20453 take_vertical_position_into_account (it);
20454
20455 if (it->glyph_row)
20456 append_composite_glyph (it);
20457 }
20458 else if (it->what == IT_IMAGE)
20459 produce_image_glyph (it);
20460 else if (it->what == IT_STRETCH)
20461 produce_stretch_glyph (it);
20462
20463 /* Accumulate dimensions. Note: can't assume that it->descent > 0
20464 because this isn't true for images with `:ascent 100'. */
20465 xassert (it->ascent >= 0 && it->descent >= 0);
20466 if (it->area == TEXT_AREA)
20467 it->current_x += it->pixel_width;
20468
a9e5c932 20469 if (extra_line_spacing > 0)
546f98b8
KS
20470 {
20471 it->descent += extra_line_spacing;
20472 if (extra_line_spacing > it->max_extra_line_spacing)
20473 it->max_extra_line_spacing = extra_line_spacing;
20474 }
133c764e
KS
20475
20476 it->max_ascent = max (it->max_ascent, it->ascent);
20477 it->max_descent = max (it->max_descent, it->descent);
20478 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
20479 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
20480}
20481
fa3c6b4d
KS
20482/* EXPORT for RIF:
20483 Output LEN glyphs starting at START at the nominal cursor position.
20484 Advance the nominal cursor over the text. The global variable
20485 updated_window contains the window being updated, updated_row is
20486 the glyph row being updated, and updated_area is the area of that
20487 row being updated. */
133c764e 20488
fa3c6b4d
KS
20489void
20490x_write_glyphs (start, len)
20491 struct glyph *start;
20492 int len;
20493{
20494 int x, hpos;
133c764e 20495
fa3c6b4d
KS
20496 xassert (updated_window && updated_row);
20497 BLOCK_INPUT;
133c764e 20498
fa3c6b4d 20499 /* Write glyphs. */
cfe03a41 20500
fa3c6b4d
KS
20501 hpos = start - updated_row->glyphs[updated_area];
20502 x = draw_glyphs (updated_window, output_cursor.x,
20503 updated_row, updated_area,
20504 hpos, hpos + len,
20505 DRAW_NORMAL_TEXT, 0);
cfe03a41 20506
fa3c6b4d
KS
20507 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
20508 if (updated_area == TEXT_AREA
20509 && updated_window->phys_cursor_on_p
20510 && updated_window->phys_cursor.vpos == output_cursor.vpos
20511 && updated_window->phys_cursor.hpos >= hpos
20512 && updated_window->phys_cursor.hpos < hpos + len)
20513 updated_window->phys_cursor_on_p = 0;
cfe03a41 20514
fa3c6b4d 20515 UNBLOCK_INPUT;
cfe03a41 20516
fa3c6b4d
KS
20517 /* Advance the output cursor. */
20518 output_cursor.hpos += len;
20519 output_cursor.x = x;
20520}
cfe03a41 20521
cfe03a41 20522
fa3c6b4d
KS
20523/* EXPORT for RIF:
20524 Insert LEN glyphs from START at the nominal cursor position. */
20525
20526void
20527x_insert_glyphs (start, len)
20528 struct glyph *start;
20529 int len;
20530{
20531 struct frame *f;
20532 struct window *w;
20533 int line_height, shift_by_width, shifted_region_width;
20534 struct glyph_row *row;
20535 struct glyph *glyph;
20536 int frame_x, frame_y, hpos;
20537
20538 xassert (updated_window && updated_row);
20539 BLOCK_INPUT;
20540 w = updated_window;
20541 f = XFRAME (WINDOW_FRAME (w));
20542
20543 /* Get the height of the line we are in. */
20544 row = updated_row;
20545 line_height = row->height;
20546
20547 /* Get the width of the glyphs to insert. */
20548 shift_by_width = 0;
20549 for (glyph = start; glyph < start + len; ++glyph)
20550 shift_by_width += glyph->pixel_width;
20551
20552 /* Get the width of the region to shift right. */
20553 shifted_region_width = (window_box_width (w, updated_area)
20554 - output_cursor.x
20555 - shift_by_width);
20556
20557 /* Shift right. */
20558 frame_x = window_box_left (w, updated_area) + output_cursor.x;
20559 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
20560
7b7b454e
AS
20561 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
20562 line_height, shift_by_width);
fa3c6b4d
KS
20563
20564 /* Write the glyphs. */
20565 hpos = start - row->glyphs[updated_area];
20566 draw_glyphs (w, output_cursor.x, row, updated_area,
20567 hpos, hpos + len,
20568 DRAW_NORMAL_TEXT, 0);
20569
20570 /* Advance the output cursor. */
20571 output_cursor.hpos += len;
20572 output_cursor.x += shift_by_width;
20573 UNBLOCK_INPUT;
20574}
20575
20576
20577/* EXPORT for RIF:
20578 Erase the current text line from the nominal cursor position
20579 (inclusive) to pixel column TO_X (exclusive). The idea is that
20580 everything from TO_X onward is already erased.
20581
20582 TO_X is a pixel position relative to updated_area of
20583 updated_window. TO_X == -1 means clear to the end of this area. */
20584
20585void
20586x_clear_end_of_line (to_x)
20587 int to_x;
20588{
20589 struct frame *f;
20590 struct window *w = updated_window;
20591 int max_x, min_y, max_y;
20592 int from_x, from_y, to_y;
20593
20594 xassert (updated_window && updated_row);
20595 f = XFRAME (w->frame);
20596
20597 if (updated_row->full_width_p)
da8b7f4f 20598 max_x = WINDOW_TOTAL_WIDTH (w);
fa3c6b4d
KS
20599 else
20600 max_x = window_box_width (w, updated_area);
20601 max_y = window_text_bottom_y (w);
20602
20603 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
20604 of window. For TO_X > 0, truncate to end of drawing area. */
20605 if (to_x == 0)
20606 return;
20607 else if (to_x < 0)
20608 to_x = max_x;
20609 else
20610 to_x = min (to_x, max_x);
20611
20612 to_y = min (max_y, output_cursor.y + updated_row->height);
20613
20614 /* Notice if the cursor will be cleared by this operation. */
20615 if (!updated_row->full_width_p)
20616 notice_overwritten_cursor (w, updated_area,
20617 output_cursor.x, -1,
20618 updated_row->y,
20619 MATRIX_ROW_BOTTOM_Y (updated_row));
20620
20621 from_x = output_cursor.x;
20622
20623 /* Translate to frame coordinates. */
20624 if (updated_row->full_width_p)
20625 {
20626 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
20627 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
20628 }
20629 else
20630 {
da8b7f4f
KS
20631 int area_left = window_box_left (w, updated_area);
20632 from_x += area_left;
20633 to_x += area_left;
fa3c6b4d
KS
20634 }
20635
da8b7f4f 20636 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
fa3c6b4d
KS
20637 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
20638 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
20639
20640 /* Prevent inadvertently clearing to end of the X window. */
20641 if (to_x > from_x && to_y > from_y)
20642 {
20643 BLOCK_INPUT;
20644 rif->clear_frame_area (f, from_x, from_y,
20645 to_x - from_x, to_y - from_y);
20646 UNBLOCK_INPUT;
20647 }
20648}
20649
20650#endif /* HAVE_WINDOW_SYSTEM */
20651
20652
20653\f
20654/***********************************************************************
20655 Cursor types
20656 ***********************************************************************/
20657
20658/* Value is the internal representation of the specified cursor type
20659 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
20660 of the bar cursor. */
20661
f65536fa 20662static enum text_cursor_kinds
fa3c6b4d
KS
20663get_specified_cursor_type (arg, width)
20664 Lisp_Object arg;
20665 int *width;
20666{
20667 enum text_cursor_kinds type;
20668
20669 if (NILP (arg))
20670 return NO_CURSOR;
20671
20672 if (EQ (arg, Qbox))
20673 return FILLED_BOX_CURSOR;
20674
20675 if (EQ (arg, Qhollow))
20676 return HOLLOW_BOX_CURSOR;
20677
20678 if (EQ (arg, Qbar))
20679 {
20680 *width = 2;
20681 return BAR_CURSOR;
20682 }
20683
20684 if (CONSP (arg)
20685 && EQ (XCAR (arg), Qbar)
20686 && INTEGERP (XCDR (arg))
20687 && XINT (XCDR (arg)) >= 0)
20688 {
20689 *width = XINT (XCDR (arg));
20690 return BAR_CURSOR;
20691 }
20692
20693 if (EQ (arg, Qhbar))
20694 {
20695 *width = 2;
20696 return HBAR_CURSOR;
20697 }
20698
20699 if (CONSP (arg)
20700 && EQ (XCAR (arg), Qhbar)
20701 && INTEGERP (XCDR (arg))
20702 && XINT (XCDR (arg)) >= 0)
20703 {
20704 *width = XINT (XCDR (arg));
20705 return HBAR_CURSOR;
20706 }
20707
20708 /* Treat anything unknown as "hollow box cursor".
20709 It was bad to signal an error; people have trouble fixing
20710 .Xdefaults with Emacs, when it has something bad in it. */
20711 type = HOLLOW_BOX_CURSOR;
20712
20713 return type;
20714}
20715
20716/* Set the default cursor types for specified frame. */
20717void
20718set_frame_cursor_types (f, arg)
20719 struct frame *f;
20720 Lisp_Object arg;
20721{
20722 int width;
20723 Lisp_Object tem;
20724
20725 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
20726 FRAME_CURSOR_WIDTH (f) = width;
20727
20728 /* By default, set up the blink-off state depending on the on-state. */
20729
20730 tem = Fassoc (arg, Vblink_cursor_alist);
20731 if (!NILP (tem))
20732 {
20733 FRAME_BLINK_OFF_CURSOR (f)
20734 = get_specified_cursor_type (XCDR (tem), &width);
20735 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
20736 }
20737 else
20738 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
20739}
20740
20741
20742/* Return the cursor we want to be displayed in window W. Return
20743 width of bar/hbar cursor through WIDTH arg. Return with
20744 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
20745 (i.e. if the `system caret' should track this cursor).
20746
20747 In a mini-buffer window, we want the cursor only to appear if we
20748 are reading input from this window. For the selected window, we
20749 want the cursor type given by the frame parameter or buffer local
20750 setting of cursor-type. If explicitly marked off, draw no cursor.
20751 In all other cases, we want a hollow box cursor. */
20752
f65536fa
KS
20753static enum text_cursor_kinds
20754get_window_cursor_type (w, glyph, width, active_cursor)
fa3c6b4d 20755 struct window *w;
f65536fa 20756 struct glyph *glyph;
fa3c6b4d
KS
20757 int *width;
20758 int *active_cursor;
20759{
20760 struct frame *f = XFRAME (w->frame);
20761 struct buffer *b = XBUFFER (w->buffer);
20762 int cursor_type = DEFAULT_CURSOR;
20763 Lisp_Object alt_cursor;
20764 int non_selected = 0;
20765
20766 *active_cursor = 1;
20767
20768 /* Echo area */
20769 if (cursor_in_echo_area
20770 && FRAME_HAS_MINIBUF_P (f)
20771 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
20772 {
20773 if (w == XWINDOW (echo_area_window))
20774 {
20775 *width = FRAME_CURSOR_WIDTH (f);
20776 return FRAME_DESIRED_CURSOR (f);
20777 }
20778
20779 *active_cursor = 0;
20780 non_selected = 1;
20781 }
20782
20783 /* Nonselected window or nonselected frame. */
20784 else if (w != XWINDOW (f->selected_window)
20785#ifdef HAVE_WINDOW_SYSTEM
20786 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
20787#endif
20788 )
20789 {
20790 *active_cursor = 0;
20791
20792 if (MINI_WINDOW_P (w) && minibuf_level == 0)
20793 return NO_CURSOR;
20794
20795 non_selected = 1;
20796 }
20797
20798 /* Never display a cursor in a window in which cursor-type is nil. */
20799 if (NILP (b->cursor_type))
20800 return NO_CURSOR;
20801
20802 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
20803 if (non_selected)
20804 {
a6cbd53f 20805 alt_cursor = b->cursor_in_non_selected_windows;
fa3c6b4d
KS
20806 return get_specified_cursor_type (alt_cursor, width);
20807 }
20808
20809 /* Get the normal cursor type for this window. */
20810 if (EQ (b->cursor_type, Qt))
20811 {
20812 cursor_type = FRAME_DESIRED_CURSOR (f);
20813 *width = FRAME_CURSOR_WIDTH (f);
20814 }
20815 else
20816 cursor_type = get_specified_cursor_type (b->cursor_type, width);
20817
20818 /* Use normal cursor if not blinked off. */
1d712183 20819 if (!w->cursor_off_p)
f65536fa 20820 {
1d712183 20821 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
f65536fa
KS
20822 if (cursor_type == FILLED_BOX_CURSOR)
20823 cursor_type = HOLLOW_BOX_CURSOR;
20824 }
20825 return cursor_type;
20826 }
fa3c6b4d
KS
20827
20828 /* Cursor is blinked off, so determine how to "toggle" it. */
20829
20830 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
20831 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
20832 return get_specified_cursor_type (XCDR (alt_cursor), width);
20833
20834 /* Then see if frame has specified a specific blink off cursor type. */
20835 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
20836 {
20837 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
20838 return FRAME_BLINK_OFF_CURSOR (f);
20839 }
20840
a57dd8b1
KS
20841#if 0
20842 /* Some people liked having a permanently visible blinking cursor,
20843 while others had very strong opinions against it. So it was
20844 decided to remove it. KFS 2003-09-03 */
20845
fa3c6b4d
KS
20846 /* Finally perform built-in cursor blinking:
20847 filled box <-> hollow box
20848 wide [h]bar <-> narrow [h]bar
20849 narrow [h]bar <-> no cursor
20850 other type <-> no cursor */
20851
20852 if (cursor_type == FILLED_BOX_CURSOR)
20853 return HOLLOW_BOX_CURSOR;
20854
20855 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
20856 {
20857 *width = 1;
20858 return cursor_type;
20859 }
a57dd8b1 20860#endif
fa3c6b4d
KS
20861
20862 return NO_CURSOR;
20863}
20864
20865
20866#ifdef HAVE_WINDOW_SYSTEM
20867
20868/* Notice when the text cursor of window W has been completely
20869 overwritten by a drawing operation that outputs glyphs in AREA
20870 starting at X0 and ending at X1 in the line starting at Y0 and
20871 ending at Y1. X coordinates are area-relative. X1 < 0 means all
20872 the rest of the line after X0 has been written. Y coordinates
20873 are window-relative. */
20874
20875static void
20876notice_overwritten_cursor (w, area, x0, x1, y0, y1)
20877 struct window *w;
20878 enum glyph_row_area area;
20879 int x0, y0, x1, y1;
20880{
88e6b646
KS
20881 int cx0, cx1, cy0, cy1;
20882 struct glyph_row *row;
fa3c6b4d 20883
88e6b646
KS
20884 if (!w->phys_cursor_on_p)
20885 return;
20886 if (area != TEXT_AREA)
20887 return;
fa3c6b4d 20888
2e87e8f2
KS
20889 if (w->phys_cursor.vpos < 0
20890 || w->phys_cursor.vpos >= w->current_matrix->nrows
20891 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
20892 !(row->enabled_p && row->displays_text_p)))
83daa9dc 20893 return;
fa3c6b4d 20894
88e6b646
KS
20895 if (row->cursor_in_fringe_p)
20896 {
20897 row->cursor_in_fringe_p = 0;
20898 draw_fringe_bitmap (w, row, 0);
20899 w->phys_cursor_on_p = 0;
20900 return;
fa3c6b4d 20901 }
88e6b646
KS
20902
20903 cx0 = w->phys_cursor.x;
20904 cx1 = cx0 + w->phys_cursor_width;
20905 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
20906 return;
20907
20908 /* The cursor image will be completely removed from the
20909 screen if the output area intersects the cursor area in
20910 y-direction. When we draw in [y0 y1[, and some part of
20911 the cursor is at y < y0, that part must have been drawn
20912 before. When scrolling, the cursor is erased before
20913 actually scrolling, so we don't come here. When not
20914 scrolling, the rows above the old cursor row must have
20915 changed, and in this case these rows must have written
20916 over the cursor image.
20917
20918 Likewise if part of the cursor is below y1, with the
20919 exception of the cursor being in the first blank row at
20920 the buffer and window end because update_text_area
20921 doesn't draw that row. (Except when it does, but
20922 that's handled in update_text_area.) */
20923
20924 cy0 = w->phys_cursor.y;
20925 cy1 = cy0 + w->phys_cursor_height;
20926 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
20927 return;
20928
20929 w->phys_cursor_on_p = 0;
fa3c6b4d
KS
20930}
20931
20932#endif /* HAVE_WINDOW_SYSTEM */
20933
20934\f
20935/************************************************************************
20936 Mouse Face
20937 ************************************************************************/
20938
20939#ifdef HAVE_WINDOW_SYSTEM
20940
20941/* EXPORT for RIF:
a66678fd
YM
20942 Fix the display of area AREA of overlapping row ROW in window W
20943 with respect to the overlapping part OVERLAPS. */
fa3c6b4d
KS
20944
20945void
a66678fd 20946x_fix_overlapping_area (w, row, area, overlaps)
fa3c6b4d
KS
20947 struct window *w;
20948 struct glyph_row *row;
20949 enum glyph_row_area area;
a66678fd 20950 int overlaps;
fa3c6b4d
KS
20951{
20952 int i, x;
20953
20954 BLOCK_INPUT;
20955
57dd22f4 20956 x = 0;
fa3c6b4d
KS
20957 for (i = 0; i < row->used[area];)
20958 {
20959 if (row->glyphs[area][i].overlaps_vertically_p)
20960 {
20961 int start = i, start_x = x;
20962
20963 do
20964 {
20965 x += row->glyphs[area][i].pixel_width;
20966 ++i;
20967 }
20968 while (i < row->used[area]
20969 && row->glyphs[area][i].overlaps_vertically_p);
20970
20971 draw_glyphs (w, start_x, row, area,
20972 start, i,
a66678fd 20973 DRAW_NORMAL_TEXT, overlaps);
fa3c6b4d
KS
20974 }
20975 else
20976 {
20977 x += row->glyphs[area][i].pixel_width;
20978 ++i;
20979 }
20980 }
20981
20982 UNBLOCK_INPUT;
20983}
20984
20985
20986/* EXPORT:
20987 Draw the cursor glyph of window W in glyph row ROW. See the
20988 comment of draw_glyphs for the meaning of HL. */
20989
20990void
20991draw_phys_cursor_glyph (w, row, hl)
20992 struct window *w;
20993 struct glyph_row *row;
20994 enum draw_glyphs_face hl;
20995{
20996 /* If cursor hpos is out of bounds, don't draw garbage. This can
20997 happen in mini-buffer windows when switching between echo area
20998 glyphs and mini-buffer. */
20999 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21000 {
21001 int on_p = w->phys_cursor_on_p;
21002 int x1;
21003 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21004 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21005 hl, 0);
21006 w->phys_cursor_on_p = on_p;
21007
fa3c6b4d
KS
21008 if (hl == DRAW_CURSOR)
21009 w->phys_cursor_width = x1 - w->phys_cursor.x;
fa3c6b4d
KS
21010 /* When we erase the cursor, and ROW is overlapped by other
21011 rows, make sure that these overlapping parts of other rows
21012 are redrawn. */
365fa1b3 21013 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
fa3c6b4d 21014 {
a66678fd
YM
21015 w->phys_cursor_width = x1 - w->phys_cursor.x;
21016
fa3c6b4d
KS
21017 if (row > w->current_matrix->rows
21018 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
a66678fd
YM
21019 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21020 OVERLAPS_ERASED_CURSOR);
fa3c6b4d
KS
21021
21022 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21023 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
a66678fd
YM
21024 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21025 OVERLAPS_ERASED_CURSOR);
fa3c6b4d
KS
21026 }
21027 }
21028}
21029
21030
21031/* EXPORT:
21032 Erase the image of a cursor of window W from the screen. */
21033
21034void
21035erase_phys_cursor (w)
21036 struct window *w;
21037{
21038 struct frame *f = XFRAME (w->frame);
21039 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21040 int hpos = w->phys_cursor.hpos;
21041 int vpos = w->phys_cursor.vpos;
21042 int mouse_face_here_p = 0;
21043 struct glyph_matrix *active_glyphs = w->current_matrix;
21044 struct glyph_row *cursor_row;
21045 struct glyph *cursor_glyph;
21046 enum draw_glyphs_face hl;
21047
21048 /* No cursor displayed or row invalidated => nothing to do on the
21049 screen. */
21050 if (w->phys_cursor_type == NO_CURSOR)
21051 goto mark_cursor_off;
21052
21053 /* VPOS >= active_glyphs->nrows means that window has been resized.
21054 Don't bother to erase the cursor. */
21055 if (vpos >= active_glyphs->nrows)
21056 goto mark_cursor_off;
21057
21058 /* If row containing cursor is marked invalid, there is nothing we
21059 can do. */
21060 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21061 if (!cursor_row->enabled_p)
21062 goto mark_cursor_off;
21063
78e7b91f
KS
21064 /* If line spacing is > 0, old cursor may only be partially visible in
21065 window after split-window. So adjust visible height. */
21066 cursor_row->visible_height = min (cursor_row->visible_height,
21067 window_text_bottom_y (w) - cursor_row->y);
21068
fa3c6b4d
KS
21069 /* If row is completely invisible, don't attempt to delete a cursor which
21070 isn't there. This can happen if cursor is at top of a window, and
21071 we switch to a buffer with a header line in that window. */
21072 if (cursor_row->visible_height <= 0)
21073 goto mark_cursor_off;
21074
88e6b646
KS
21075 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21076 if (cursor_row->cursor_in_fringe_p)
21077 {
21078 cursor_row->cursor_in_fringe_p = 0;
21079 draw_fringe_bitmap (w, cursor_row, 0);
21080 goto mark_cursor_off;
21081 }
21082
fa3c6b4d
KS
21083 /* This can happen when the new row is shorter than the old one.
21084 In this case, either draw_glyphs or clear_end_of_line
21085 should have cleared the cursor. Note that we wouldn't be
21086 able to erase the cursor in this case because we don't have a
21087 cursor glyph at hand. */
21088 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21089 goto mark_cursor_off;
21090
21091 /* If the cursor is in the mouse face area, redisplay that when
21092 we clear the cursor. */
21093 if (! NILP (dpyinfo->mouse_face_window)
21094 && w == XWINDOW (dpyinfo->mouse_face_window)
21095 && (vpos > dpyinfo->mouse_face_beg_row
21096 || (vpos == dpyinfo->mouse_face_beg_row
21097 && hpos >= dpyinfo->mouse_face_beg_col))
21098 && (vpos < dpyinfo->mouse_face_end_row
21099 || (vpos == dpyinfo->mouse_face_end_row
21100 && hpos < dpyinfo->mouse_face_end_col))
21101 /* Don't redraw the cursor's spot in mouse face if it is at the
21102 end of a line (on a newline). The cursor appears there, but
21103 mouse highlighting does not. */
21104 && cursor_row->used[TEXT_AREA] > hpos)
21105 mouse_face_here_p = 1;
21106
21107 /* Maybe clear the display under the cursor. */
21108 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21109 {
21110 int x, y;
da8b7f4f 21111 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
e1dca367 21112 int width;
fa3c6b4d
KS
21113
21114 cursor_glyph = get_phys_cursor_glyph (w);
21115 if (cursor_glyph == NULL)
21116 goto mark_cursor_off;
21117
21118 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
21119 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
e1dca367
KS
21120 width = min (cursor_glyph->pixel_width,
21121 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
fa3c6b4d 21122
e1dca367 21123 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
fa3c6b4d
KS
21124 }
21125
21126 /* Erase the cursor by redrawing the character underneath it. */
21127 if (mouse_face_here_p)
21128 hl = DRAW_MOUSE_FACE;
21129 else
21130 hl = DRAW_NORMAL_TEXT;
21131 draw_phys_cursor_glyph (w, cursor_row, hl);
21132
21133 mark_cursor_off:
21134 w->phys_cursor_on_p = 0;
21135 w->phys_cursor_type = NO_CURSOR;
21136}
21137
21138
21139/* EXPORT:
21140 Display or clear cursor of window W. If ON is zero, clear the
21141 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21142 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21143
21144void
21145display_and_set_cursor (w, on, hpos, vpos, x, y)
21146 struct window *w;
21147 int on, hpos, vpos, x, y;
21148{
21149 struct frame *f = XFRAME (w->frame);
21150 int new_cursor_type;
21151 int new_cursor_width;
21152 int active_cursor;
fa3c6b4d
KS
21153 struct glyph_row *glyph_row;
21154 struct glyph *glyph;
21155
21156 /* This is pointless on invisible frames, and dangerous on garbaged
21157 windows and frames; in the latter case, the frame or window may
21158 be in the midst of changing its size, and x and y may be off the
21159 window. */
21160 if (! FRAME_VISIBLE_P (f)
21161 || FRAME_GARBAGED_P (f)
21162 || vpos >= w->current_matrix->nrows
21163 || hpos >= w->current_matrix->matrix_w)
21164 return;
21165
21166 /* If cursor is off and we want it off, return quickly. */
21167 if (!on && !w->phys_cursor_on_p)
21168 return;
21169
1d712183 21170 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
fa3c6b4d
KS
21171 /* If cursor row is not enabled, we don't really know where to
21172 display the cursor. */
21173 if (!glyph_row->enabled_p)
21174 {
21175 w->phys_cursor_on_p = 0;
21176 return;
21177 }
21178
1d712183
KS
21179 glyph = NULL;
21180 if (!glyph_row->exact_window_width_line_p
21181 || hpos < glyph_row->used[TEXT_AREA])
21182 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
21183
fa3c6b4d
KS
21184 xassert (interrupt_input_blocked);
21185
21186 /* Set new_cursor_type to the cursor we want to be displayed. */
f65536fa
KS
21187 new_cursor_type = get_window_cursor_type (w, glyph,
21188 &new_cursor_width, &active_cursor);
fa3c6b4d
KS
21189
21190 /* If cursor is currently being shown and we don't want it to be or
21191 it is in the wrong place, or the cursor type is not what we want,
21192 erase it. */
21193 if (w->phys_cursor_on_p
21194 && (!on
21195 || w->phys_cursor.x != x
21196 || w->phys_cursor.y != y
21197 || new_cursor_type != w->phys_cursor_type
21198 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
21199 && new_cursor_width != w->phys_cursor_width)))
21200 erase_phys_cursor (w);
21201
21202 /* Don't check phys_cursor_on_p here because that flag is only set
21203 to zero in some cases where we know that the cursor has been
21204 completely erased, to avoid the extra work of erasing the cursor
21205 twice. In other words, phys_cursor_on_p can be 1 and the cursor
21206 still not be visible, or it has only been partly erased. */
21207 if (on)
21208 {
21209 w->phys_cursor_ascent = glyph_row->ascent;
21210 w->phys_cursor_height = glyph_row->height;
21211
21212 /* Set phys_cursor_.* before x_draw_.* is called because some
21213 of them may need the information. */
21214 w->phys_cursor.x = x;
21215 w->phys_cursor.y = glyph_row->y;
21216 w->phys_cursor.hpos = hpos;
21217 w->phys_cursor.vpos = vpos;
21218 }
21219
b4ebbb12
KS
21220 rif->draw_window_cursor (w, glyph_row, x, y,
21221 new_cursor_type, new_cursor_width,
21222 on, active_cursor);
fa3c6b4d
KS
21223}
21224
21225
21226/* Switch the display of W's cursor on or off, according to the value
21227 of ON. */
21228
21229static void
21230update_window_cursor (w, on)
21231 struct window *w;
21232 int on;
21233{
21234 /* Don't update cursor in windows whose frame is in the process
21235 of being deleted. */
21236 if (w->current_matrix)
21237 {
21238 BLOCK_INPUT;
21239 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
21240 w->phys_cursor.x, w->phys_cursor.y);
21241 UNBLOCK_INPUT;
21242 }
21243}
21244
21245
21246/* Call update_window_cursor with parameter ON_P on all leaf windows
21247 in the window tree rooted at W. */
21248
21249static void
21250update_cursor_in_window_tree (w, on_p)
21251 struct window *w;
21252 int on_p;
21253{
21254 while (w)
21255 {
21256 if (!NILP (w->hchild))
21257 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
21258 else if (!NILP (w->vchild))
21259 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
21260 else
21261 update_window_cursor (w, on_p);
21262
21263 w = NILP (w->next) ? 0 : XWINDOW (w->next);
21264 }
21265}
21266
21267
21268/* EXPORT:
21269 Display the cursor on window W, or clear it, according to ON_P.
21270 Don't change the cursor's position. */
21271
21272void
21273x_update_cursor (f, on_p)
21274 struct frame *f;
21275 int on_p;
21276{
21277 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
21278}
21279
21280
21281/* EXPORT:
21282 Clear the cursor of window W to background color, and mark the
21283 cursor as not shown. This is used when the text where the cursor
21284 is is about to be rewritten. */
21285
21286void
21287x_clear_cursor (w)
21288 struct window *w;
21289{
21290 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
21291 update_window_cursor (w, 0);
21292}
21293
21294
21295/* EXPORT:
21296 Display the active region described by mouse_face_* according to DRAW. */
21297
21298void
21299show_mouse_face (dpyinfo, draw)
21300 Display_Info *dpyinfo;
21301 enum draw_glyphs_face draw;
21302{
21303 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
21304 struct frame *f = XFRAME (WINDOW_FRAME (w));
21305
21306 if (/* If window is in the process of being destroyed, don't bother
21307 to do anything. */
21308 w->current_matrix != NULL
21309 /* Don't update mouse highlight if hidden */
21310 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
21311 /* Recognize when we are called to operate on rows that don't exist
21312 anymore. This can happen when a window is split. */
21313 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
21314 {
21315 int phys_cursor_on_p = w->phys_cursor_on_p;
21316 struct glyph_row *row, *first, *last;
21317
21318 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21319 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21320
21321 for (row = first; row <= last && row->enabled_p; ++row)
21322 {
21323 int start_hpos, end_hpos, start_x;
21324
21325 /* For all but the first row, the highlight starts at column 0. */
21326 if (row == first)
21327 {
21328 start_hpos = dpyinfo->mouse_face_beg_col;
21329 start_x = dpyinfo->mouse_face_beg_x;
21330 }
21331 else
21332 {
21333 start_hpos = 0;
21334 start_x = 0;
21335 }
21336
21337 if (row == last)
21338 end_hpos = dpyinfo->mouse_face_end_col;
21339 else
8a658d94
KS
21340 {
21341 end_hpos = row->used[TEXT_AREA];
21342 if (draw == DRAW_NORMAL_TEXT)
21343 row->fill_line_p = 1; /* Clear to end of line */
21344 }
fa3c6b4d
KS
21345
21346 if (end_hpos > start_hpos)
21347 {
21348 draw_glyphs (w, start_x, row, TEXT_AREA,
21349 start_hpos, end_hpos,
21350 draw, 0);
21351
21352 row->mouse_face_p
21353 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
21354 }
21355 }
21356
21357 /* When we've written over the cursor, arrange for it to
21358 be displayed again. */
21359 if (phys_cursor_on_p && !w->phys_cursor_on_p)
21360 {
21361 BLOCK_INPUT;
21362 display_and_set_cursor (w, 1,
21363 w->phys_cursor.hpos, w->phys_cursor.vpos,
21364 w->phys_cursor.x, w->phys_cursor.y);
21365 UNBLOCK_INPUT;
21366 }
21367 }
21368
21369 /* Change the mouse cursor. */
21370 if (draw == DRAW_NORMAL_TEXT)
21371 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
21372 else if (draw == DRAW_MOUSE_FACE)
21373 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
21374 else
21375 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
21376}
21377
21378/* EXPORT:
21379 Clear out the mouse-highlighted active region.
21380 Redraw it un-highlighted first. Value is non-zero if mouse
21381 face was actually drawn unhighlighted. */
21382
21383int
21384clear_mouse_face (dpyinfo)
21385 Display_Info *dpyinfo;
21386{
21387 int cleared = 0;
21388
867756d8 21389 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
fa3c6b4d
KS
21390 {
21391 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
21392 cleared = 1;
21393 }
21394
21395 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21396 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21397 dpyinfo->mouse_face_window = Qnil;
21398 dpyinfo->mouse_face_overlay = Qnil;
21399 return cleared;
21400}
21401
21402
21403/* EXPORT:
21404 Non-zero if physical cursor of window W is within mouse face. */
21405
21406int
21407cursor_in_mouse_face_p (w)
21408 struct window *w;
21409{
21410 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21411 int in_mouse_face = 0;
21412
21413 if (WINDOWP (dpyinfo->mouse_face_window)
21414 && XWINDOW (dpyinfo->mouse_face_window) == w)
21415 {
21416 int hpos = w->phys_cursor.hpos;
21417 int vpos = w->phys_cursor.vpos;
21418
21419 if (vpos >= dpyinfo->mouse_face_beg_row
21420 && vpos <= dpyinfo->mouse_face_end_row
21421 && (vpos > dpyinfo->mouse_face_beg_row
21422 || hpos >= dpyinfo->mouse_face_beg_col)
21423 && (vpos < dpyinfo->mouse_face_end_row
21424 || hpos < dpyinfo->mouse_face_end_col
21425 || dpyinfo->mouse_face_past_end))
21426 in_mouse_face = 1;
21427 }
21428
21429 return in_mouse_face;
21430}
21431
21432
21433
21434\f
21435/* Find the glyph matrix position of buffer position CHARPOS in window
21436 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
21437 current glyphs must be up to date. If CHARPOS is above window
21438 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
21439 of last line in W. In the row containing CHARPOS, stop before glyphs
21440 having STOP as object. */
21441
9c3521e0 21442#if 1 /* This is a version of fast_find_position that's more correct
fa3c6b4d
KS
21443 in the presence of hscrolling, for example. I didn't install
21444 it right away because the problem fixed is minor, it failed
21445 in 20.x as well, and I think it's too risky to install
21446 so near the release of 21.1. 2001-09-25 gerd. */
21447
21448static int
21449fast_find_position (w, charpos, hpos, vpos, x, y, stop)
21450 struct window *w;
21451 int charpos;
21452 int *hpos, *vpos, *x, *y;
21453 Lisp_Object stop;
21454{
21455 struct glyph_row *row, *first;
21456 struct glyph *glyph, *end;
b19a5b64 21457 int past_end = 0;
fa3c6b4d
KS
21458
21459 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
a78bc3c0
KS
21460 if (charpos < MATRIX_ROW_START_CHARPOS (first))
21461 {
21462 *x = first->x;
21463 *y = first->y;
21464 *hpos = 0;
21465 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
21466 return 1;
21467 }
21468
fa3c6b4d
KS
21469 row = row_containing_pos (w, charpos, first, NULL, 0);
21470 if (row == NULL)
21471 {
a78bc3c0
KS
21472 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
21473 past_end = 1;
fa3c6b4d
KS
21474 }
21475
c6576d9d
KS
21476 /* If whole rows or last part of a row came from a display overlay,
21477 row_containing_pos will skip over such rows because their end pos
e6d09683
RS
21478 equals the start pos of the overlay or interval.
21479
21480 Move back if we have a STOP object and previous row's
21481 end glyph came from STOP. */
c6576d9d
KS
21482 if (!NILP (stop))
21483 {
e3198b77 21484 struct glyph_row *prev;
c6576d9d
KS
21485 while ((prev = row - 1, prev >= first)
21486 && MATRIX_ROW_END_CHARPOS (prev) == charpos
21487 && prev->used[TEXT_AREA] > 0)
21488 {
e6d09683
RS
21489 struct glyph *beg = prev->glyphs[TEXT_AREA];
21490 glyph = beg + prev->used[TEXT_AREA];
21491 while (--glyph >= beg
c6576d9d 21492 && INTEGERP (glyph->object));
e6d09683 21493 if (glyph < beg
e3198b77 21494 || !EQ (stop, glyph->object))
c6576d9d
KS
21495 break;
21496 row = prev;
21497 }
21498 }
21499
fa3c6b4d
KS
21500 *x = row->x;
21501 *y = row->y;
21502 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21503
21504 glyph = row->glyphs[TEXT_AREA];
21505 end = glyph + row->used[TEXT_AREA];
21506
21507 /* Skip over glyphs not having an object at the start of the row.
21508 These are special glyphs like truncation marks on terminal
21509 frames. */
21510 if (row->displays_text_p)
21511 while (glyph < end
21512 && INTEGERP (glyph->object)
21513 && !EQ (stop, glyph->object)
21514 && glyph->charpos < 0)
21515 {
21516 *x += glyph->pixel_width;
21517 ++glyph;
21518 }
21519
21520 while (glyph < end
21521 && !INTEGERP (glyph->object)
21522 && !EQ (stop, glyph->object)
21523 && (!BUFFERP (glyph->object)
21524 || glyph->charpos < charpos))
21525 {
21526 *x += glyph->pixel_width;
21527 ++glyph;
21528 }
21529
21530 *hpos = glyph - row->glyphs[TEXT_AREA];
7a272039 21531 return !past_end;
fa3c6b4d
KS
21532}
21533
9c3521e0 21534#else /* not 1 */
fa3c6b4d
KS
21535
21536static int
21537fast_find_position (w, pos, hpos, vpos, x, y, stop)
21538 struct window *w;
21539 int pos;
21540 int *hpos, *vpos, *x, *y;
21541 Lisp_Object stop;
21542{
21543 int i;
21544 int lastcol;
21545 int maybe_next_line_p = 0;
21546 int line_start_position;
21547 int yb = window_text_bottom_y (w);
21548 struct glyph_row *row, *best_row;
21549 int row_vpos, best_row_vpos;
21550 int current_x;
21551
21552 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21553 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21554
21555 while (row->y < yb)
21556 {
21557 if (row->used[TEXT_AREA])
21558 line_start_position = row->glyphs[TEXT_AREA]->charpos;
21559 else
21560 line_start_position = 0;
21561
21562 if (line_start_position > pos)
21563 break;
21564 /* If the position sought is the end of the buffer,
21565 don't include the blank lines at the bottom of the window. */
21566 else if (line_start_position == pos
21567 && pos == BUF_ZV (XBUFFER (w->buffer)))
21568 {
21569 maybe_next_line_p = 1;
21570 break;
21571 }
21572 else if (line_start_position > 0)
21573 {
21574 best_row = row;
21575 best_row_vpos = row_vpos;
21576 }
21577
21578 if (row->y + row->height >= yb)
21579 break;
21580
21581 ++row;
21582 ++row_vpos;
21583 }
21584
21585 /* Find the right column within BEST_ROW. */
21586 lastcol = 0;
21587 current_x = best_row->x;
21588 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
21589 {
21590 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
21591 int charpos = glyph->charpos;
21592
21593 if (BUFFERP (glyph->object))
21594 {
21595 if (charpos == pos)
21596 {
21597 *hpos = i;
21598 *vpos = best_row_vpos;
21599 *x = current_x;
21600 *y = best_row->y;
21601 return 1;
21602 }
21603 else if (charpos > pos)
21604 break;
21605 }
21606 else if (EQ (glyph->object, stop))
21607 break;
21608
21609 if (charpos > 0)
21610 lastcol = i;
21611 current_x += glyph->pixel_width;
21612 }
21613
21614 /* If we're looking for the end of the buffer,
21615 and we didn't find it in the line we scanned,
21616 use the start of the following line. */
21617 if (maybe_next_line_p)
21618 {
21619 ++best_row;
21620 ++best_row_vpos;
21621 lastcol = 0;
21622 current_x = best_row->x;
21623 }
21624
21625 *vpos = best_row_vpos;
21626 *hpos = lastcol + 1;
21627 *x = current_x;
21628 *y = best_row->y;
21629 return 0;
21630}
21631
9c3521e0 21632#endif /* not 1 */
fa3c6b4d
KS
21633
21634
21635/* Find the position of the glyph for position POS in OBJECT in
21636 window W's current matrix, and return in *X, *Y the pixel
21637 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
21638
21639 RIGHT_P non-zero means return the position of the right edge of the
21640 glyph, RIGHT_P zero means return the left edge position.
21641
21642 If no glyph for POS exists in the matrix, return the position of
21643 the glyph with the next smaller position that is in the matrix, if
21644 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
21645 exists in the matrix, return the position of the glyph with the
21646 next larger position in OBJECT.
21647
21648 Value is non-zero if a glyph was found. */
21649
21650static int
21651fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
21652 struct window *w;
21653 int pos;
21654 Lisp_Object object;
21655 int *hpos, *vpos, *x, *y;
21656 int right_p;
21657{
21658 int yb = window_text_bottom_y (w);
21659 struct glyph_row *r;
21660 struct glyph *best_glyph = NULL;
21661 struct glyph_row *best_row = NULL;
21662 int best_x = 0;
21663
21664 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21665 r->enabled_p && r->y < yb;
21666 ++r)
21667 {
21668 struct glyph *g = r->glyphs[TEXT_AREA];
21669 struct glyph *e = g + r->used[TEXT_AREA];
21670 int gx;
21671
21672 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
21673 if (EQ (g->object, object))
21674 {
21675 if (g->charpos == pos)
21676 {
21677 best_glyph = g;
21678 best_x = gx;
21679 best_row = r;
21680 goto found;
21681 }
21682 else if (best_glyph == NULL
21683 || ((abs (g->charpos - pos)
21684 < abs (best_glyph->charpos - pos))
21685 && (right_p
21686 ? g->charpos < pos
21687 : g->charpos > pos)))
21688 {
21689 best_glyph = g;
21690 best_x = gx;
21691 best_row = r;
21692 }
21693 }
21694 }
21695
21696 found:
21697
21698 if (best_glyph)
21699 {
21700 *x = best_x;
21701 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
21702
21703 if (right_p)
21704 {
21705 *x += best_glyph->pixel_width;
21706 ++*hpos;
21707 }
21708
21709 *y = best_row->y;
21710 *vpos = best_row - w->current_matrix->rows;
21711 }
21712
21713 return best_glyph != NULL;
21714}
21715
21716
493fdc3c
KS
21717/* See if position X, Y is within a hot-spot of an image. */
21718
21719static int
21720on_hot_spot_p (hot_spot, x, y)
21721 Lisp_Object hot_spot;
21722 int x, y;
21723{
21724 if (!CONSP (hot_spot))
21725 return 0;
21726
21727 if (EQ (XCAR (hot_spot), Qrect))
21728 {
21729 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
21730 Lisp_Object rect = XCDR (hot_spot);
21731 Lisp_Object tem;
21732 if (!CONSP (rect))
21733 return 0;
21734 if (!CONSP (XCAR (rect)))
21735 return 0;
21736 if (!CONSP (XCDR (rect)))
21737 return 0;
21738 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
21739 return 0;
21740 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
21741 return 0;
21742 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
21743 return 0;
21744 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
21745 return 0;
21746 return 1;
21747 }
21748 else if (EQ (XCAR (hot_spot), Qcircle))
21749 {
21750 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
21751 Lisp_Object circ = XCDR (hot_spot);
21752 Lisp_Object lr, lx0, ly0;
21753 if (CONSP (circ)
21754 && CONSP (XCAR (circ))
21755 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
21756 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
21757 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
21758 {
21759 double r = XFLOATINT (lr);
21760 double dx = XINT (lx0) - x;
21761 double dy = XINT (ly0) - y;
21762 return (dx * dx + dy * dy <= r * r);
21763 }
21764 }
21765 else if (EQ (XCAR (hot_spot), Qpoly))
21766 {
21767 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
21768 if (VECTORP (XCDR (hot_spot)))
21769 {
21770 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
21771 Lisp_Object *poly = v->contents;
21772 int n = v->size;
21773 int i;
21774 int inside = 0;
21775 Lisp_Object lx, ly;
21776 int x0, y0;
21777
21778 /* Need an even number of coordinates, and at least 3 edges. */
be21b925 21779 if (n < 6 || n & 1)
493fdc3c
KS
21780 return 0;
21781
21782 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
21783 If count is odd, we are inside polygon. Pixels on edges
21784 may or may not be included depending on actual geometry of the
21785 polygon. */
21786 if ((lx = poly[n-2], !INTEGERP (lx))
21787 || (ly = poly[n-1], !INTEGERP (lx)))
21788 return 0;
21789 x0 = XINT (lx), y0 = XINT (ly);
21790 for (i = 0; i < n; i += 2)
21791 {
21792 int x1 = x0, y1 = y0;
21793 if ((lx = poly[i], !INTEGERP (lx))
21794 || (ly = poly[i+1], !INTEGERP (ly)))
21795 return 0;
21796 x0 = XINT (lx), y0 = XINT (ly);
21797
21798 /* Does this segment cross the X line? */
21799 if (x0 >= x)
21800 {
21801 if (x1 >= x)
21802 continue;
21803 }
21804 else if (x1 < x)
21805 continue;
21806 if (y > y0 && y > y1)
21807 continue;
21808 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
21809 inside = !inside;
21810 }
21811 return inside;
21812 }
21813 }
b1c2132c
SM
21814 /* If we don't understand the format, pretend we're not in the hot-spot. */
21815 return 0;
493fdc3c
KS
21816}
21817
21818Lisp_Object
21819find_hot_spot (map, x, y)
21820 Lisp_Object map;
21821 int x, y;
21822{
21823 while (CONSP (map))
21824 {
21825 if (CONSP (XCAR (map))
21826 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
21827 return XCAR (map);
21828 map = XCDR (map);
21829 }
be21b925 21830
493fdc3c
KS
21831 return Qnil;
21832}
21833
21834DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
21835 3, 3, 0,
be21b925 21836 doc: /* Lookup in image map MAP coordinates X and Y.
493fdc3c
KS
21837An image map is an alist where each element has the format (AREA ID PLIST).
21838An AREA is specified as either a rectangle, a circle, or a polygon:
21839A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
21840pixel coordinates of the upper left and bottom right corners.
21841A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
21842and the radius of the circle; r may be a float or integer.
21843A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
21844vector describes one corner in the polygon.
21845Returns the alist element for the first matching AREA in MAP. */)
21846 (map, x, y)
21847 Lisp_Object map;
21848 Lisp_Object x, y;
21849{
493fdc3c
KS
21850 if (NILP (map))
21851 return Qnil;
21852
6d925726
KS
21853 CHECK_NUMBER (x);
21854 CHECK_NUMBER (y);
493fdc3c
KS
21855
21856 return find_hot_spot (map, XINT (x), XINT (y));
21857}
21858
21859
21860/* Display frame CURSOR, optionally using shape defined by POINTER. */
21861static void
21862define_frame_cursor1 (f, cursor, pointer)
21863 struct frame *f;
21864 Cursor cursor;
21865 Lisp_Object pointer;
21866{
562020fe
KS
21867 /* Do not change cursor shape while dragging mouse. */
21868 if (!NILP (do_mouse_tracking))
21869 return;
21870
493fdc3c
KS
21871 if (!NILP (pointer))
21872 {
21873 if (EQ (pointer, Qarrow))
21874 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21875 else if (EQ (pointer, Qhand))
21876 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
21877 else if (EQ (pointer, Qtext))
21878 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21879 else if (EQ (pointer, intern ("hdrag")))
21880 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21881#ifdef HAVE_X_WINDOWS
21882 else if (EQ (pointer, intern ("vdrag")))
21883 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
21884#endif
21885 else if (EQ (pointer, intern ("hourglass")))
21886 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
21887 else if (EQ (pointer, Qmodeline))
21888 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
21889 else
21890 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21891 }
21892
493fdc3c 21893 if (cursor != No_Cursor)
493fdc3c
KS
21894 rif->define_frame_cursor (f, cursor);
21895}
21896
fa3c6b4d
KS
21897/* Take proper action when mouse has moved to the mode or header line
21898 or marginal area AREA of window W, x-position X and y-position Y.
21899 X is relative to the start of the text display area of W, so the
21900 width of bitmap areas and scroll bars must be subtracted to get a
21901 position relative to the start of the mode line. */
21902
21903static void
359e4563
MY
21904note_mode_line_or_margin_highlight (window, x, y, area)
21905 Lisp_Object window;
fa3c6b4d
KS
21906 int x, y;
21907 enum window_part area;
21908{
359e4563 21909 struct window *w = XWINDOW (window);
fa3c6b4d
KS
21910 struct frame *f = XFRAME (w->frame);
21911 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
f65536fa 21912 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
493fdc3c 21913 Lisp_Object pointer = Qnil;
349c653e
KS
21914 int charpos, dx, dy, width, height;
21915 Lisp_Object string, object = Qnil;
b1c2132c 21916 Lisp_Object pos, help;
fa3c6b4d 21917
359e4563
MY
21918 Lisp_Object mouse_face;
21919 int original_x_pixel = x;
21920 struct glyph * glyph = NULL;
55ce5058
MY
21921 struct glyph_row *row;
21922
fa3c6b4d 21923 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
359e4563
MY
21924 {
21925 int x0;
21926 struct glyph *end;
21927
21928 string = mode_line_string (w, area, &x, &y, &charpos,
21929 &object, &dx, &dy, &width, &height);
21930
6d0e6660
RS
21931 row = (area == ON_MODE_LINE
21932 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
21933 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
55ce5058 21934
359e4563
MY
21935 /* Find glyph */
21936 if (row->mode_line_p && row->enabled_p)
21937 {
21938 glyph = row->glyphs[TEXT_AREA];
21939 end = glyph + row->used[TEXT_AREA];
55ce5058
MY
21940
21941 for (x0 = original_x_pixel;
21942 glyph < end && x0 >= glyph->pixel_width;
359e4563
MY
21943 ++glyph)
21944 x0 -= glyph->pixel_width;
55ce5058 21945
359e4563
MY
21946 if (glyph >= end)
21947 glyph = NULL;
21948 }
21949 }
fa3c6b4d 21950 else
493fdc3c
KS
21951 {
21952 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
349c653e
KS
21953 string = marginal_area_string (w, area, &x, &y, &charpos,
21954 &object, &dx, &dy, &width, &height);
493fdc3c
KS
21955 }
21956
21957 help = Qnil;
fa3c6b4d 21958
349c653e 21959 if (IMAGEP (object))
493fdc3c
KS
21960 {
21961 Lisp_Object image_map, hotspot;
bccf59df 21962 if ((image_map = Fplist_get (XCDR (object), QCmap),
493fdc3c
KS
21963 !NILP (image_map))
21964 && (hotspot = find_hot_spot (image_map, dx, dy),
21965 CONSP (hotspot))
21966 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21967 {
21968 Lisp_Object area_id, plist;
fa3c6b4d 21969
493fdc3c
KS
21970 area_id = XCAR (hotspot);
21971 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21972 If so, we could look for mouse-enter, mouse-leave
21973 properties in PLIST (and do something...). */
109b593f
KS
21974 hotspot = XCDR (hotspot);
21975 if (CONSP (hotspot)
21976 && (plist = XCAR (hotspot), CONSP (plist)))
493fdc3c 21977 {
bccf59df 21978 pointer = Fplist_get (plist, Qpointer);
493fdc3c
KS
21979 if (NILP (pointer))
21980 pointer = Qhand;
bccf59df 21981 help = Fplist_get (plist, Qhelp_echo);
493fdc3c
KS
21982 if (!NILP (help))
21983 {
21984 help_echo_string = help;
21985 /* Is this correct? ++kfs */
21986 XSETWINDOW (help_echo_window, w);
21987 help_echo_object = w->buffer;
21988 help_echo_pos = charpos;
21989 }
21990 }
349c653e 21991 }
9c25f170 21992 if (NILP (pointer))
bccf59df 21993 pointer = Fplist_get (XCDR (object), QCpointer);
349c653e
KS
21994 }
21995
21996 if (STRINGP (string))
21997 {
21998 pos = make_number (charpos);
21999 /* If we're on a string with `help-echo' text property, arrange
22000 for the help to be displayed. This is done by setting the
22001 global variable help_echo_string to the help string. */
425daf4a 22002 if (NILP (help))
349c653e 22003 {
425daf4a
KS
22004 help = Fget_text_property (pos, Qhelp_echo, string);
22005 if (!NILP (help))
22006 {
22007 help_echo_string = help;
22008 XSETWINDOW (help_echo_window, w);
22009 help_echo_object = string;
22010 help_echo_pos = charpos;
22011 }
349c653e
KS
22012 }
22013
22014 if (NILP (pointer))
22015 pointer = Fget_text_property (pos, Qpointer, string);
22016
22017 /* Change the mouse pointer according to what is under X/Y. */
73bc43da 22018 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
349c653e
KS
22019 {
22020 Lisp_Object map;
22021 map = Fget_text_property (pos, Qlocal_map, string);
22022 if (!KEYMAPP (map))
22023 map = Fget_text_property (pos, Qkeymap, string);
22024 if (!KEYMAPP (map))
22025 cursor = dpyinfo->vertical_scroll_bar_cursor;
493fdc3c 22026 }
55ce5058 22027
359e4563
MY
22028 /* Change the mouse face according to what is under X/Y. */
22029 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22030 if (!NILP (mouse_face)
22031 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22032 && glyph)
22033 {
22034 Lisp_Object b, e;
22035
22036 struct glyph * tmp_glyph;
22037
22038 int gpos;
22039 int gseq_length;
22040 int total_pixel_width;
22041 int ignore;
22042
602907ae 22043 int vpos, hpos;
121c59a3 22044
6d0e6660
RS
22045 b = Fprevious_single_property_change (make_number (charpos + 1),
22046 Qmouse_face, string, Qnil);
55ce5058
MY
22047 if (NILP (b))
22048 b = make_number (0);
22049
22050 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22051 if (NILP (e))
22052 e = make_number (SCHARS (string));
22053
22054 /* Calculate the position(glyph position: GPOS) of GLYPH in
22055 displayed string. GPOS is different from CHARPOS.
22056
359e4563
MY
22057 CHARPOS is the position of glyph in internal string
22058 object. A mode line string format has structures which
22059 is converted to a flatten by emacs lisp interpreter.
22060 The internal string is an element of the structures.
22061 The displayed string is the flatten string. */
55ce5058 22062 for (tmp_glyph = glyph - 1, gpos = 0;
6d0e6660 22063 tmp_glyph->charpos >= XINT (b);
359e4563
MY
22064 tmp_glyph--, gpos++)
22065 {
3c1b433a 22066 if (!EQ (tmp_glyph->object, glyph->object))
359e4563
MY
22067 break;
22068 }
22069
22070 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
55ce5058 22071 displayed string holding GLYPH.
359e4563
MY
22072
22073 GSEQ_LENGTH is different from SCHARS (STRING).
22074 SCHARS (STRING) returns the length of the internal string. */
22075 for (tmp_glyph = glyph, gseq_length = gpos;
6d0e6660 22076 tmp_glyph->charpos < XINT (e);
359e4563
MY
22077 tmp_glyph++, gseq_length++)
22078 {
3c1b433a 22079 if (!EQ (tmp_glyph->object, glyph->object))
359e4563
MY
22080 break;
22081 }
359e4563
MY
22082
22083 total_pixel_width = 0;
55ce5058 22084 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
359e4563 22085 total_pixel_width += tmp_glyph->pixel_width;
55ce5058 22086
602907ae
MY
22087 /* Pre calculation of re-rendering position */
22088 vpos = (x - gpos);
22089 hpos = (area == ON_MODE_LINE
22090 ? (w->current_matrix)->nrows - 1
22091 : 0);
121c59a3 22092
602907ae
MY
22093 /* If the re-rendering position is included in the last
22094 re-rendering area, we should do nothing. */
b78ab259 22095 if ( EQ (window, dpyinfo->mouse_face_window)
602907ae
MY
22096 && dpyinfo->mouse_face_beg_col <= vpos
22097 && vpos < dpyinfo->mouse_face_end_col
22098 && dpyinfo->mouse_face_beg_row == hpos )
22099 return;
121c59a3 22100
602907ae
MY
22101 if (clear_mouse_face (dpyinfo))
22102 cursor = No_Cursor;
121c59a3 22103
602907ae
MY
22104 dpyinfo->mouse_face_beg_col = vpos;
22105 dpyinfo->mouse_face_beg_row = hpos;
359e4563
MY
22106
22107 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22108 dpyinfo->mouse_face_beg_y = 0;
55ce5058 22109
602907ae 22110 dpyinfo->mouse_face_end_col = vpos + gseq_length;
359e4563
MY
22111 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22112
22113 dpyinfo->mouse_face_end_x = 0;
22114 dpyinfo->mouse_face_end_y = 0;
22115
22116 dpyinfo->mouse_face_past_end = 0;
22117 dpyinfo->mouse_face_window = window;
22118
6d0e6660
RS
22119 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22120 charpos,
22121 0, 0, 0, &ignore,
22122 glyph->face_id, 1);
359e4563 22123 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
edfaeb4e
MY
22124
22125 if (NILP (pointer))
22126 pointer = Qhand;
359e4563 22127 }
121c59a3
MY
22128 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22129 clear_mouse_face (dpyinfo);
493fdc3c 22130 }
493fdc3c 22131 define_frame_cursor1 (f, cursor, pointer);
fa3c6b4d
KS
22132}
22133
fa3c6b4d
KS
22134
22135/* EXPORT:
22136 Take proper action when the mouse has moved to position X, Y on
22137 frame F as regards highlighting characters that have mouse-face
22138 properties. Also de-highlighting chars where the mouse was before.
22139 X and Y can be negative or out of range. */
22140
22141void
22142note_mouse_highlight (f, x, y)
22143 struct frame *f;
22144 int x, y;
22145{
22146 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22147 enum window_part part;
22148 Lisp_Object window;
22149 struct window *w;
22150 Cursor cursor = No_Cursor;
493fdc3c 22151 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
fa3c6b4d
KS
22152 struct buffer *b;
22153
22154 /* When a menu is active, don't highlight because this looks odd. */
22155#if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
22156 if (popup_activated ())
22157 return;
22158#endif
22159
22160 if (NILP (Vmouse_highlight)
22161 || !f->glyphs_initialized_p)
22162 return;
22163
22164 dpyinfo->mouse_face_mouse_x = x;
22165 dpyinfo->mouse_face_mouse_y = y;
22166 dpyinfo->mouse_face_mouse_frame = f;
22167
22168 if (dpyinfo->mouse_face_defer)
22169 return;
22170
22171 if (gc_in_progress)
22172 {
22173 dpyinfo->mouse_face_deferred_gc = 1;
22174 return;
22175 }
22176
22177 /* Which window is that in? */
da8b7f4f 22178 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
fa3c6b4d 22179
4674c583
KS
22180 /* If we were displaying active text in another window, clear that.
22181 Also clear if we move out of text area in same window. */
22182 if (! EQ (window, dpyinfo->mouse_face_window)
0afd0871 22183 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
602907ae 22184 && !NILP (dpyinfo->mouse_face_window)))
fa3c6b4d
KS
22185 clear_mouse_face (dpyinfo);
22186
22187 /* Not on a window -> return. */
22188 if (!WINDOWP (window))
22189 return;
22190
22191 /* Reset help_echo_string. It will get recomputed below. */
fa3c6b4d
KS
22192 help_echo_string = Qnil;
22193
22194 /* Convert to window-relative pixel coordinates. */
22195 w = XWINDOW (window);
22196 frame_to_window_pixel_xy (w, &x, &y);
22197
22198 /* Handle tool-bar window differently since it doesn't display a
22199 buffer. */
22200 if (EQ (window, f->tool_bar_window))
22201 {
22202 note_tool_bar_highlight (f, x, y);
22203 return;
22204 }
22205
fa3c6b4d
KS
22206 /* Mouse is on the mode, header line or margin? */
22207 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
22208 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
22209 {
359e4563 22210 note_mode_line_or_margin_highlight (window, x, y, part);
fa3c6b4d
KS
22211 return;
22212 }
fa3c6b4d
KS
22213
22214 if (part == ON_VERTICAL_BORDER)
22215 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
b15de414
ST
22216 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
22217 || part == ON_SCROLL_BAR)
f65536fa 22218 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
fa3c6b4d
KS
22219 else
22220 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22221
22222 /* Are we in a window whose display is up to date?
22223 And verify the buffer's text has not changed. */
22224 b = XBUFFER (w->buffer);
22225 if (part == ON_TEXT
22226 && EQ (w->window_end_valid, w->buffer)
22227 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
22228 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
22229 {
493fdc3c 22230 int hpos, vpos, pos, i, dx, dy, area;
fa3c6b4d
KS
22231 struct glyph *glyph;
22232 Lisp_Object object;
22233 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
22234 Lisp_Object *overlay_vec = NULL;
7994a991 22235 int noverlays;
fa3c6b4d
KS
22236 struct buffer *obuf;
22237 int obegv, ozv, same_region;
22238
22239 /* Find the glyph under X/Y. */
493fdc3c
KS
22240 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
22241
22242 /* Look for :pointer property on image. */
22243 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22244 {
22245 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22246 if (img != NULL && IMAGEP (img->spec))
22247 {
22248 Lisp_Object image_map, hotspot;
bccf59df 22249 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
493fdc3c 22250 !NILP (image_map))
e893970b
KS
22251 && (hotspot = find_hot_spot (image_map,
22252 glyph->slice.x + dx,
22253 glyph->slice.y + dy),
493fdc3c
KS
22254 CONSP (hotspot))
22255 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22256 {
22257 Lisp_Object area_id, plist;
22258
22259 area_id = XCAR (hotspot);
22260 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22261 If so, we could look for mouse-enter, mouse-leave
22262 properties in PLIST (and do something...). */
109b593f
KS
22263 hotspot = XCDR (hotspot);
22264 if (CONSP (hotspot)
22265 && (plist = XCAR (hotspot), CONSP (plist)))
493fdc3c 22266 {
bccf59df 22267 pointer = Fplist_get (plist, Qpointer);
493fdc3c
KS
22268 if (NILP (pointer))
22269 pointer = Qhand;
bccf59df 22270 help_echo_string = Fplist_get (plist, Qhelp_echo);
493fdc3c
KS
22271 if (!NILP (help_echo_string))
22272 {
22273 help_echo_window = window;
22274 help_echo_object = glyph->object;
22275 help_echo_pos = glyph->charpos;
22276 }
22277 }
22278 }
22279 if (NILP (pointer))
bccf59df 22280 pointer = Fplist_get (XCDR (img->spec), QCpointer);
493fdc3c
KS
22281 }
22282 }
fa3c6b4d
KS
22283
22284 /* Clear mouse face if X/Y not over text. */
22285 if (glyph == NULL
22286 || area != TEXT_AREA
22287 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
22288 {
fa3c6b4d
KS
22289 if (clear_mouse_face (dpyinfo))
22290 cursor = No_Cursor;
493fdc3c
KS
22291 if (NILP (pointer))
22292 {
22293 if (area != TEXT_AREA)
22294 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22295 else
22296 pointer = Vvoid_text_area_pointer;
22297 }
fa3c6b4d
KS
22298 goto set_cursor;
22299 }
22300
22301 pos = glyph->charpos;
22302 object = glyph->object;
22303 if (!STRINGP (object) && !BUFFERP (object))
22304 goto set_cursor;
22305
22306 /* If we get an out-of-range value, return now; avoid an error. */
22307 if (BUFFERP (object) && pos > BUF_Z (b))
22308 goto set_cursor;
22309
22310 /* Make the window's buffer temporarily current for
22311 overlays_at and compute_char_face. */
22312 obuf = current_buffer;
22313 current_buffer = b;
22314 obegv = BEGV;
22315 ozv = ZV;
22316 BEGV = BEG;
22317 ZV = Z;
22318
22319 /* Is this char mouse-active or does it have help-echo? */
22320 position = make_number (pos);
22321
22322 if (BUFFERP (object))
22323 {
7994a991
KS
22324 /* Put all the overlays we want in a vector in overlay_vec. */
22325 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
fa3c6b4d
KS
22326 /* Sort overlays into increasing priority order. */
22327 noverlays = sort_overlays (overlay_vec, noverlays, w);
22328 }
22329 else
22330 noverlays = 0;
22331
22332 same_region = (EQ (window, dpyinfo->mouse_face_window)
22333 && vpos >= dpyinfo->mouse_face_beg_row
22334 && vpos <= dpyinfo->mouse_face_end_row
22335 && (vpos > dpyinfo->mouse_face_beg_row
22336 || hpos >= dpyinfo->mouse_face_beg_col)
22337 && (vpos < dpyinfo->mouse_face_end_row
22338 || hpos < dpyinfo->mouse_face_end_col
22339 || dpyinfo->mouse_face_past_end));
22340
22341 if (same_region)
22342 cursor = No_Cursor;
22343
22344 /* Check mouse-face highlighting. */
22345 if (! same_region
22346 /* If there exists an overlay with mouse-face overlapping
22347 the one we are currently highlighting, we have to
22348 check if we enter the overlapping overlay, and then
22349 highlight only that. */
22350 || (OVERLAYP (dpyinfo->mouse_face_overlay)
22351 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
22352 {
22353 /* Find the highest priority overlay that has a mouse-face
22354 property. */
22355 overlay = Qnil;
22356 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
22357 {
22358 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
22359 if (!NILP (mouse_face))
22360 overlay = overlay_vec[i];
22361 }
22362
22363 /* If we're actually highlighting the same overlay as
22364 before, there's no need to do that again. */
22365 if (!NILP (overlay)
22366 && EQ (overlay, dpyinfo->mouse_face_overlay))
22367 goto check_help_echo;
22368
22369 dpyinfo->mouse_face_overlay = overlay;
22370
22371 /* Clear the display of the old active region, if any. */
22372 if (clear_mouse_face (dpyinfo))
22373 cursor = No_Cursor;
22374
22375 /* If no overlay applies, get a text property. */
22376 if (NILP (overlay))
22377 mouse_face = Fget_text_property (position, Qmouse_face, object);
22378
22379 /* Handle the overlay case. */
22380 if (!NILP (overlay))
22381 {
22382 /* Find the range of text around this char that
22383 should be active. */
22384 Lisp_Object before, after;
22385 int ignore;
22386
22387 before = Foverlay_start (overlay);
22388 after = Foverlay_end (overlay);
22389 /* Record this as the current active region. */
22390 fast_find_position (w, XFASTINT (before),
22391 &dpyinfo->mouse_face_beg_col,
22392 &dpyinfo->mouse_face_beg_row,
22393 &dpyinfo->mouse_face_beg_x,
22394 &dpyinfo->mouse_face_beg_y, Qnil);
22395
22396 dpyinfo->mouse_face_past_end
22397 = !fast_find_position (w, XFASTINT (after),
22398 &dpyinfo->mouse_face_end_col,
22399 &dpyinfo->mouse_face_end_row,
22400 &dpyinfo->mouse_face_end_x,
22401 &dpyinfo->mouse_face_end_y, Qnil);
22402 dpyinfo->mouse_face_window = window;
22403
22404 dpyinfo->mouse_face_face_id
22405 = face_at_buffer_position (w, pos, 0, 0,
22406 &ignore, pos + 1,
22407 !dpyinfo->mouse_face_hidden);
22408
22409 /* Display it as active. */
22410 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22411 cursor = No_Cursor;
22412 }
22413 /* Handle the text property case. */
22414 else if (!NILP (mouse_face) && BUFFERP (object))
22415 {
22416 /* Find the range of text around this char that
22417 should be active. */
22418 Lisp_Object before, after, beginning, end;
22419 int ignore;
22420
22421 beginning = Fmarker_position (w->start);
22422 end = make_number (BUF_Z (XBUFFER (object))
22423 - XFASTINT (w->window_end_pos));
22424 before
22425 = Fprevious_single_property_change (make_number (pos + 1),
22426 Qmouse_face,
22427 object, beginning);
22428 after
22429 = Fnext_single_property_change (position, Qmouse_face,
22430 object, end);
22431
22432 /* Record this as the current active region. */
22433 fast_find_position (w, XFASTINT (before),
22434 &dpyinfo->mouse_face_beg_col,
22435 &dpyinfo->mouse_face_beg_row,
22436 &dpyinfo->mouse_face_beg_x,
22437 &dpyinfo->mouse_face_beg_y, Qnil);
22438 dpyinfo->mouse_face_past_end
22439 = !fast_find_position (w, XFASTINT (after),
22440 &dpyinfo->mouse_face_end_col,
22441 &dpyinfo->mouse_face_end_row,
22442 &dpyinfo->mouse_face_end_x,
22443 &dpyinfo->mouse_face_end_y, Qnil);
22444 dpyinfo->mouse_face_window = window;
22445
22446 if (BUFFERP (object))
22447 dpyinfo->mouse_face_face_id
22448 = face_at_buffer_position (w, pos, 0, 0,
22449 &ignore, pos + 1,
22450 !dpyinfo->mouse_face_hidden);
22451
22452 /* Display it as active. */
22453 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22454 cursor = No_Cursor;
22455 }
22456 else if (!NILP (mouse_face) && STRINGP (object))
22457 {
22458 Lisp_Object b, e;
22459 int ignore;
22460
22461 b = Fprevious_single_property_change (make_number (pos + 1),
22462 Qmouse_face,
22463 object, Qnil);
22464 e = Fnext_single_property_change (position, Qmouse_face,
22465 object, Qnil);
22466 if (NILP (b))
22467 b = make_number (0);
22468 if (NILP (e))
22469 e = make_number (SCHARS (object) - 1);
55ce5058 22470
fa3c6b4d
KS
22471 fast_find_string_pos (w, XINT (b), object,
22472 &dpyinfo->mouse_face_beg_col,
22473 &dpyinfo->mouse_face_beg_row,
22474 &dpyinfo->mouse_face_beg_x,
22475 &dpyinfo->mouse_face_beg_y, 0);
22476 fast_find_string_pos (w, XINT (e), object,
22477 &dpyinfo->mouse_face_end_col,
22478 &dpyinfo->mouse_face_end_row,
22479 &dpyinfo->mouse_face_end_x,
22480 &dpyinfo->mouse_face_end_y, 1);
22481 dpyinfo->mouse_face_past_end = 0;
22482 dpyinfo->mouse_face_window = window;
22483 dpyinfo->mouse_face_face_id
22484 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
22485 glyph->face_id, 1);
22486 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22487 cursor = No_Cursor;
22488 }
22489 else if (STRINGP (object) && NILP (mouse_face))
22490 {
22491 /* A string which doesn't have mouse-face, but
22492 the text ``under'' it might have. */
22493 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
22494 int start = MATRIX_ROW_START_CHARPOS (r);
22495
22496 pos = string_buffer_position (w, object, start);
22497 if (pos > 0)
22498 mouse_face = get_char_property_and_overlay (make_number (pos),
22499 Qmouse_face,
22500 w->buffer,
22501 &overlay);
22502 if (!NILP (mouse_face) && !NILP (overlay))
22503 {
22504 Lisp_Object before = Foverlay_start (overlay);
22505 Lisp_Object after = Foverlay_end (overlay);
22506 int ignore;
22507
22508 /* Note that we might not be able to find position
22509 BEFORE in the glyph matrix if the overlay is
22510 entirely covered by a `display' property. In
22511 this case, we overshoot. So let's stop in
22512 the glyph matrix before glyphs for OBJECT. */
22513 fast_find_position (w, XFASTINT (before),
22514 &dpyinfo->mouse_face_beg_col,
22515 &dpyinfo->mouse_face_beg_row,
22516 &dpyinfo->mouse_face_beg_x,
22517 &dpyinfo->mouse_face_beg_y,
22518 object);
22519
22520 dpyinfo->mouse_face_past_end
22521 = !fast_find_position (w, XFASTINT (after),
22522 &dpyinfo->mouse_face_end_col,
22523 &dpyinfo->mouse_face_end_row,
22524 &dpyinfo->mouse_face_end_x,
22525 &dpyinfo->mouse_face_end_y,
22526 Qnil);
22527 dpyinfo->mouse_face_window = window;
22528 dpyinfo->mouse_face_face_id
22529 = face_at_buffer_position (w, pos, 0, 0,
22530 &ignore, pos + 1,
22531 !dpyinfo->mouse_face_hidden);
22532
22533 /* Display it as active. */
22534 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22535 cursor = No_Cursor;
22536 }
22537 }
22538 }
22539
22540 check_help_echo:
22541
22542 /* Look for a `help-echo' property. */
493fdc3c 22543 if (NILP (help_echo_string)) {
fa3c6b4d
KS
22544 Lisp_Object help, overlay;
22545
22546 /* Check overlays first. */
22547 help = overlay = Qnil;
22548 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
22549 {
22550 overlay = overlay_vec[i];
22551 help = Foverlay_get (overlay, Qhelp_echo);
22552 }
22553
22554 if (!NILP (help))
22555 {
22556 help_echo_string = help;
22557 help_echo_window = window;
22558 help_echo_object = overlay;
22559 help_echo_pos = pos;
22560 }
22561 else
22562 {
22563 Lisp_Object object = glyph->object;
22564 int charpos = glyph->charpos;
22565
22566 /* Try text properties. */
22567 if (STRINGP (object)
22568 && charpos >= 0
22569 && charpos < SCHARS (object))
22570 {
22571 help = Fget_text_property (make_number (charpos),
22572 Qhelp_echo, object);
22573 if (NILP (help))
22574 {
22575 /* If the string itself doesn't specify a help-echo,
22576 see if the buffer text ``under'' it does. */
22577 struct glyph_row *r
22578 = MATRIX_ROW (w->current_matrix, vpos);
22579 int start = MATRIX_ROW_START_CHARPOS (r);
22580 int pos = string_buffer_position (w, object, start);
22581 if (pos > 0)
22582 {
22583 help = Fget_char_property (make_number (pos),
22584 Qhelp_echo, w->buffer);
22585 if (!NILP (help))
22586 {
22587 charpos = pos;
22588 object = w->buffer;
22589 }
22590 }
22591 }
22592 }
22593 else if (BUFFERP (object)
22594 && charpos >= BEGV
22595 && charpos < ZV)
22596 help = Fget_text_property (make_number (charpos), Qhelp_echo,
22597 object);
22598
22599 if (!NILP (help))
22600 {
22601 help_echo_string = help;
22602 help_echo_window = window;
22603 help_echo_object = object;
22604 help_echo_pos = charpos;
22605 }
22606 }
22607 }
22608
493fdc3c
KS
22609 /* Look for a `pointer' property. */
22610 if (NILP (pointer))
22611 {
22612 /* Check overlays first. */
22613 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
22614 pointer = Foverlay_get (overlay_vec[i], Qpointer);
22615
22616 if (NILP (pointer))
22617 {
22618 Lisp_Object object = glyph->object;
22619 int charpos = glyph->charpos;
22620
22621 /* Try text properties. */
22622 if (STRINGP (object)
22623 && charpos >= 0
22624 && charpos < SCHARS (object))
22625 {
22626 pointer = Fget_text_property (make_number (charpos),
22627 Qpointer, object);
22628 if (NILP (pointer))
22629 {
22630 /* If the string itself doesn't specify a pointer,
22631 see if the buffer text ``under'' it does. */
22632 struct glyph_row *r
22633 = MATRIX_ROW (w->current_matrix, vpos);
22634 int start = MATRIX_ROW_START_CHARPOS (r);
22635 int pos = string_buffer_position (w, object, start);
22636 if (pos > 0)
22637 pointer = Fget_char_property (make_number (pos),
22638 Qpointer, w->buffer);
22639 }
22640 }
22641 else if (BUFFERP (object)
22642 && charpos >= BEGV
22643 && charpos < ZV)
22644 pointer = Fget_text_property (make_number (charpos),
22645 Qpointer, object);
22646 }
22647 }
22648
fa3c6b4d
KS
22649 BEGV = obegv;
22650 ZV = ozv;
22651 current_buffer = obuf;
22652 }
22653
22654 set_cursor:
22655
493fdc3c 22656 define_frame_cursor1 (f, cursor, pointer);
fa3c6b4d
KS
22657}
22658
22659
22660/* EXPORT for RIF:
22661 Clear any mouse-face on window W. This function is part of the
22662 redisplay interface, and is called from try_window_id and similar
22663 functions to ensure the mouse-highlight is off. */
22664
22665void
22666x_clear_window_mouse_face (w)
22667 struct window *w;
22668{
22669 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22670 Lisp_Object window;
22671
22672 BLOCK_INPUT;
22673 XSETWINDOW (window, w);
22674 if (EQ (window, dpyinfo->mouse_face_window))
22675 clear_mouse_face (dpyinfo);
22676 UNBLOCK_INPUT;
22677}
22678
22679
22680/* EXPORT:
22681 Just discard the mouse face information for frame F, if any.
22682 This is used when the size of F is changed. */
22683
22684void
22685cancel_mouse_face (f)
22686 struct frame *f;
22687{
22688 Lisp_Object window;
22689 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22690
22691 window = dpyinfo->mouse_face_window;
22692 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
22693 {
22694 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22695 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22696 dpyinfo->mouse_face_window = Qnil;
22697 }
22698}
22699
22700
22701#endif /* HAVE_WINDOW_SYSTEM */
22702
22703\f
22704/***********************************************************************
22705 Exposure Events
22706 ***********************************************************************/
22707
22708#ifdef HAVE_WINDOW_SYSTEM
22709
22710/* Redraw the part of glyph row area AREA of glyph row ROW on window W
22711 which intersects rectangle R. R is in window-relative coordinates. */
22712
22713static void
22714expose_area (w, row, r, area)
22715 struct window *w;
22716 struct glyph_row *row;
22717 XRectangle *r;
22718 enum glyph_row_area area;
22719{
22720 struct glyph *first = row->glyphs[area];
22721 struct glyph *end = row->glyphs[area] + row->used[area];
22722 struct glyph *last;
22723 int first_x, start_x, x;
22724
22725 if (area == TEXT_AREA && row->fill_line_p)
22726 /* If row extends face to end of line write the whole line. */
22727 draw_glyphs (w, 0, row, area,
22728 0, row->used[area],
22729 DRAW_NORMAL_TEXT, 0);
22730 else
22731 {
22732 /* Set START_X to the window-relative start position for drawing glyphs of
22733 AREA. The first glyph of the text area can be partially visible.
22734 The first glyphs of other areas cannot. */
da8b7f4f 22735 start_x = window_box_left_offset (w, area);
fa3c6b4d 22736 x = start_x;
29b3ea63
KS
22737 if (area == TEXT_AREA)
22738 x += row->x;
fa3c6b4d
KS
22739
22740 /* Find the first glyph that must be redrawn. */
22741 while (first < end
22742 && x + first->pixel_width < r->x)
22743 {
22744 x += first->pixel_width;
22745 ++first;
22746 }
22747
22748 /* Find the last one. */
22749 last = first;
22750 first_x = x;
22751 while (last < end
22752 && x < r->x + r->width)
22753 {
22754 x += last->pixel_width;
22755 ++last;
22756 }
22757
22758 /* Repaint. */
22759 if (last > first)
22760 draw_glyphs (w, first_x - start_x, row, area,
22761 first - row->glyphs[area], last - row->glyphs[area],
22762 DRAW_NORMAL_TEXT, 0);
22763 }
22764}
22765
22766
22767/* Redraw the parts of the glyph row ROW on window W intersecting
22768 rectangle R. R is in window-relative coordinates. Value is
22769 non-zero if mouse-face was overwritten. */
22770
22771static int
22772expose_line (w, row, r)
22773 struct window *w;
22774 struct glyph_row *row;
22775 XRectangle *r;
22776{
22777 xassert (row->enabled_p);
22778
22779 if (row->mode_line_p || w->pseudo_window_p)
22780 draw_glyphs (w, 0, row, TEXT_AREA,
22781 0, row->used[TEXT_AREA],
22782 DRAW_NORMAL_TEXT, 0);
22783 else
22784 {
22785 if (row->used[LEFT_MARGIN_AREA])
22786 expose_area (w, row, r, LEFT_MARGIN_AREA);
22787 if (row->used[TEXT_AREA])
22788 expose_area (w, row, r, TEXT_AREA);
22789 if (row->used[RIGHT_MARGIN_AREA])
22790 expose_area (w, row, r, RIGHT_MARGIN_AREA);
22791 draw_row_fringe_bitmaps (w, row);
cfe03a41
KS
22792 }
22793
fa3c6b4d
KS
22794 return row->mouse_face_p;
22795}
22796
22797
22798/* Redraw those parts of glyphs rows during expose event handling that
22799 overlap other rows. Redrawing of an exposed line writes over parts
22800 of lines overlapping that exposed line; this function fixes that.
22801
22802 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
22803 row in W's current matrix that is exposed and overlaps other rows.
22804 LAST_OVERLAPPING_ROW is the last such row. */
22805
22806static void
22807expose_overlaps (w, first_overlapping_row, last_overlapping_row)
22808 struct window *w;
22809 struct glyph_row *first_overlapping_row;
22810 struct glyph_row *last_overlapping_row;
22811{
22812 struct glyph_row *row;
22813
22814 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
22815 if (row->overlapping_p)
22816 {
22817 xassert (row->enabled_p && !row->mode_line_p);
22818
22819 if (row->used[LEFT_MARGIN_AREA])
a66678fd 22820 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
cfe03a41 22821
fa3c6b4d 22822 if (row->used[TEXT_AREA])
a66678fd 22823 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
fa3c6b4d
KS
22824
22825 if (row->used[RIGHT_MARGIN_AREA])
a66678fd 22826 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
fa3c6b4d
KS
22827 }
22828}
22829
22830
22831/* Return non-zero if W's cursor intersects rectangle R. */
22832
22833static int
22834phys_cursor_in_rect_p (w, r)
22835 struct window *w;
22836 XRectangle *r;
22837{
22838 XRectangle cr, result;
22839 struct glyph *cursor_glyph;
22840
22841 cursor_glyph = get_phys_cursor_glyph (w);
22842 if (cursor_glyph)
cfe03a41 22843 {
be21b925 22844 /* r is relative to W's box, but w->phys_cursor.x is relative
aecf50d2
KS
22845 to left edge of W's TEXT area. Adjust it. */
22846 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
fa3c6b4d
KS
22847 cr.y = w->phys_cursor.y;
22848 cr.width = cursor_glyph->pixel_width;
22849 cr.height = w->phys_cursor_height;
be21b925 22850 /* ++KFS: W32 version used W32-specific IntersectRect here, but
fa3c6b4d
KS
22851 I assume the effect is the same -- and this is portable. */
22852 return x_intersect_rectangles (&cr, r, &result);
cfe03a41 22853 }
fa3c6b4d
KS
22854 else
22855 return 0;
22856}
cfe03a41 22857
cfe03a41 22858
fa3c6b4d
KS
22859/* EXPORT:
22860 Draw a vertical window border to the right of window W if W doesn't
22861 have vertical scroll bars. */
cfe03a41 22862
cfe03a41 22863void
fa3c6b4d
KS
22864x_draw_vertical_border (w)
22865 struct window *w;
cfe03a41 22866{
da8b7f4f 22867 /* We could do better, if we knew what type of scroll-bar the adjacent
be21b925 22868 windows (on either side) have... But we don't :-(
da8b7f4f
KS
22869 However, I think this works ok. ++KFS 2003-04-25 */
22870
fa3c6b4d
KS
22871 /* Redraw borders between horizontally adjacent windows. Don't
22872 do it for frames with vertical scroll bars because either the
22873 right scroll bar of a window, or the left scroll bar of its
22874 neighbor will suffice as a border. */
b55d59ba
KS
22875 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
22876 return;
22877
fa3c6b4d 22878 if (!WINDOW_RIGHTMOST_P (w)
da8b7f4f 22879 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
fa3c6b4d
KS
22880 {
22881 int x0, x1, y0, y1;
cfe03a41 22882
fa3c6b4d 22883 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
fa3c6b4d 22884 y1 -= 1;
cfe03a41 22885
74fb229b
JL
22886 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
22887 x1 -= 1;
22888
fa3c6b4d 22889 rif->draw_vertical_window_border (w, x1, y0, y1);
cfe03a41 22890 }
da8b7f4f
KS
22891 else if (!WINDOW_LEFTMOST_P (w)
22892 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
22893 {
22894 int x0, x1, y0, y1;
22895
22896 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22897 y1 -= 1;
22898
74fb229b
JL
22899 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
22900 x0 -= 1;
22901
da8b7f4f
KS
22902 rif->draw_vertical_window_border (w, x0, y0, y1);
22903 }
cfe03a41
KS
22904}
22905
22906
fa3c6b4d
KS
22907/* Redraw the part of window W intersection rectangle FR. Pixel
22908 coordinates in FR are frame-relative. Call this function with
22909 input blocked. Value is non-zero if the exposure overwrites
22910 mouse-face. */
cfe03a41 22911
fa3c6b4d
KS
22912static int
22913expose_window (w, fr)
cfe03a41 22914 struct window *w;
fa3c6b4d 22915 XRectangle *fr;
cfe03a41
KS
22916{
22917 struct frame *f = XFRAME (w->frame);
fa3c6b4d
KS
22918 XRectangle wr, r;
22919 int mouse_face_overwritten_p = 0;
22920
22921 /* If window is not yet fully initialized, do nothing. This can
22922 happen when toolkit scroll bars are used and a window is split.
22923 Reconfiguring the scroll bar will generate an expose for a newly
22924 created window. */
22925 if (w->current_matrix == NULL)
22926 return 0;
cfe03a41 22927
fa3c6b4d
KS
22928 /* When we're currently updating the window, display and current
22929 matrix usually don't agree. Arrange for a thorough display
22930 later. */
22931 if (w == updated_window)
22932 {
22933 SET_FRAME_GARBAGED (f);
22934 return 0;
22935 }
e9c99027 22936
fa3c6b4d 22937 /* Frame-relative pixel rectangle of W. */
da8b7f4f
KS
22938 wr.x = WINDOW_LEFT_EDGE_X (w);
22939 wr.y = WINDOW_TOP_EDGE_Y (w);
22940 wr.width = WINDOW_TOTAL_WIDTH (w);
22941 wr.height = WINDOW_TOTAL_HEIGHT (w);
fa3c6b4d
KS
22942
22943 if (x_intersect_rectangles (fr, &wr, &r))
cfe03a41 22944 {
fa3c6b4d
KS
22945 int yb = window_text_bottom_y (w);
22946 struct glyph_row *row;
22947 int cursor_cleared_p;
22948 struct glyph_row *first_overlapping_row, *last_overlapping_row;
22949
22950 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
22951 r.x, r.y, r.width, r.height));
22952
22953 /* Convert to window coordinates. */
cc8549df
KS
22954 r.x -= WINDOW_LEFT_EDGE_X (w);
22955 r.y -= WINDOW_TOP_EDGE_Y (w);
fa3c6b4d
KS
22956
22957 /* Turn off the cursor. */
22958 if (!w->pseudo_window_p
22959 && phys_cursor_in_rect_p (w, &r))
cfe03a41 22960 {
fa3c6b4d
KS
22961 x_clear_cursor (w);
22962 cursor_cleared_p = 1;
cfe03a41 22963 }
fa3c6b4d
KS
22964 else
22965 cursor_cleared_p = 0;
cfe03a41 22966
fa3c6b4d
KS
22967 /* Update lines intersecting rectangle R. */
22968 first_overlapping_row = last_overlapping_row = NULL;
22969 for (row = w->current_matrix->rows;
22970 row->enabled_p;
22971 ++row)
22972 {
22973 int y0 = row->y;
22974 int y1 = MATRIX_ROW_BOTTOM_Y (row);
cfe03a41 22975
fa3c6b4d
KS
22976 if ((y0 >= r.y && y0 < r.y + r.height)
22977 || (y1 > r.y && y1 < r.y + r.height)
22978 || (r.y >= y0 && r.y < y1)
22979 || (r.y + r.height > y0 && r.y + r.height < y1))
22980 {
83e2a07a
KS
22981 /* A header line may be overlapping, but there is no need
22982 to fix overlapping areas for them. KFS 2005-02-12 */
22983 if (row->overlapping_p && !row->mode_line_p)
fa3c6b4d
KS
22984 {
22985 if (first_overlapping_row == NULL)
22986 first_overlapping_row = row;
22987 last_overlapping_row = row;
22988 }
e9c99027 22989
fa3c6b4d
KS
22990 if (expose_line (w, row, &r))
22991 mouse_face_overwritten_p = 1;
22992 }
cfe03a41 22993
fa3c6b4d
KS
22994 if (y1 >= yb)
22995 break;
22996 }
cfe03a41 22997
fa3c6b4d
KS
22998 /* Display the mode line if there is one. */
22999 if (WINDOW_WANTS_MODELINE_P (w)
23000 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23001 row->enabled_p)
23002 && row->y < r.y + r.height)
23003 {
23004 if (expose_line (w, row, &r))
23005 mouse_face_overwritten_p = 1;
23006 }
cfe03a41 23007
fa3c6b4d
KS
23008 if (!w->pseudo_window_p)
23009 {
23010 /* Fix the display of overlapping rows. */
23011 if (first_overlapping_row)
23012 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23013
23014 /* Draw border between windows. */
23015 x_draw_vertical_border (w);
23016
23017 /* Turn the cursor on again. */
23018 if (cursor_cleared_p)
23019 update_window_cursor (w, 1);
23020 }
23021 }
23022
fa3c6b4d
KS
23023 return mouse_face_overwritten_p;
23024}
23025
23026
23027
23028/* Redraw (parts) of all windows in the window tree rooted at W that
23029 intersect R. R contains frame pixel coordinates. Value is
23030 non-zero if the exposure overwrites mouse-face. */
23031
23032static int
23033expose_window_tree (w, r)
23034 struct window *w;
23035 XRectangle *r;
23036{
23037 struct frame *f = XFRAME (w->frame);
23038 int mouse_face_overwritten_p = 0;
23039
23040 while (w && !FRAME_GARBAGED_P (f))
cfe03a41 23041 {
fa3c6b4d
KS
23042 if (!NILP (w->hchild))
23043 mouse_face_overwritten_p
23044 |= expose_window_tree (XWINDOW (w->hchild), r);
23045 else if (!NILP (w->vchild))
23046 mouse_face_overwritten_p
23047 |= expose_window_tree (XWINDOW (w->vchild), r);
23048 else
23049 mouse_face_overwritten_p |= expose_window (w, r);
23050
23051 w = NILP (w->next) ? NULL : XWINDOW (w->next);
cfe03a41 23052 }
cfe03a41 23053
fa3c6b4d
KS
23054 return mouse_face_overwritten_p;
23055}
cfe03a41 23056
cfe03a41 23057
fa3c6b4d
KS
23058/* EXPORT:
23059 Redisplay an exposed area of frame F. X and Y are the upper-left
23060 corner of the exposed rectangle. W and H are width and height of
23061 the exposed area. All are pixel values. W or H zero means redraw
23062 the entire frame. */
cfe03a41 23063
fa3c6b4d
KS
23064void
23065expose_frame (f, x, y, w, h)
23066 struct frame *f;
23067 int x, y, w, h;
23068{
23069 XRectangle r;
23070 int mouse_face_overwritten_p = 0;
23071
23072 TRACE ((stderr, "expose_frame "));
23073
23074 /* No need to redraw if frame will be redrawn soon. */
23075 if (FRAME_GARBAGED_P (f))
cfe03a41 23076 {
fa3c6b4d
KS
23077 TRACE ((stderr, " garbaged\n"));
23078 return;
cfe03a41 23079 }
7d0393cf 23080
fa3c6b4d
KS
23081 /* If basic faces haven't been realized yet, there is no point in
23082 trying to redraw anything. This can happen when we get an expose
23083 event while Emacs is starting, e.g. by moving another window. */
23084 if (FRAME_FACE_CACHE (f) == NULL
23085 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23086 {
23087 TRACE ((stderr, " no faces\n"));
23088 return;
23089 }
cfe03a41 23090
fa3c6b4d 23091 if (w == 0 || h == 0)
cfe03a41 23092 {
fa3c6b4d 23093 r.x = r.y = 0;
da8b7f4f
KS
23094 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23095 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
fa3c6b4d
KS
23096 }
23097 else
23098 {
23099 r.x = x;
23100 r.y = y;
23101 r.width = w;
23102 r.height = h;
cfe03a41
KS
23103 }
23104
fa3c6b4d
KS
23105 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23106 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23107
23108 if (WINDOWP (f->tool_bar_window))
23109 mouse_face_overwritten_p
23110 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23111
23112#ifdef HAVE_X_WINDOWS
23113#ifndef MSDOS
23114#ifndef USE_X_TOOLKIT
23115 if (WINDOWP (f->menu_bar_window))
23116 mouse_face_overwritten_p
23117 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23118#endif /* not USE_X_TOOLKIT */
23119#endif
23120#endif
23121
23122 /* Some window managers support a focus-follows-mouse style with
23123 delayed raising of frames. Imagine a partially obscured frame,
23124 and moving the mouse into partially obscured mouse-face on that
23125 frame. The visible part of the mouse-face will be highlighted,
23126 then the WM raises the obscured frame. With at least one WM, KDE
23127 2.1, Emacs is not getting any event for the raising of the frame
23128 (even tried with SubstructureRedirectMask), only Expose events.
23129 These expose events will draw text normally, i.e. not
23130 highlighted. Which means we must redo the highlight here.
23131 Subsume it under ``we love X''. --gerd 2001-08-15 */
23132 /* Included in Windows version because Windows most likely does not
23133 do the right thing if any third party tool offers
23134 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23135 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23136 {
23137 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23138 if (f == dpyinfo->mouse_face_mouse_frame)
23139 {
23140 int x = dpyinfo->mouse_face_mouse_x;
23141 int y = dpyinfo->mouse_face_mouse_y;
23142 clear_mouse_face (dpyinfo);
23143 note_mouse_highlight (f, x, y);
23144 }
23145 }
cfe03a41
KS
23146}
23147
133c764e 23148
fa3c6b4d
KS
23149/* EXPORT:
23150 Determine the intersection of two rectangles R1 and R2. Return
23151 the intersection in *RESULT. Value is non-zero if RESULT is not
23152 empty. */
133c764e 23153
fa3c6b4d
KS
23154int
23155x_intersect_rectangles (r1, r2, result)
23156 XRectangle *r1, *r2, *result;
133c764e 23157{
fa3c6b4d
KS
23158 XRectangle *left, *right;
23159 XRectangle *upper, *lower;
23160 int intersection_p = 0;
23161
23162 /* Rearrange so that R1 is the left-most rectangle. */
23163 if (r1->x < r2->x)
23164 left = r1, right = r2;
23165 else
23166 left = r2, right = r1;
23167
23168 /* X0 of the intersection is right.x0, if this is inside R1,
23169 otherwise there is no intersection. */
23170 if (right->x <= left->x + left->width)
133c764e 23171 {
fa3c6b4d 23172 result->x = right->x;
133c764e 23173
fa3c6b4d
KS
23174 /* The right end of the intersection is the minimum of the
23175 the right ends of left and right. */
23176 result->width = (min (left->x + left->width, right->x + right->width)
23177 - result->x);
133c764e 23178
fa3c6b4d
KS
23179 /* Same game for Y. */
23180 if (r1->y < r2->y)
23181 upper = r1, lower = r2;
23182 else
23183 upper = r2, lower = r1;
133c764e 23184
fa3c6b4d
KS
23185 /* The upper end of the intersection is lower.y0, if this is inside
23186 of upper. Otherwise, there is no intersection. */
23187 if (lower->y <= upper->y + upper->height)
23188 {
23189 result->y = lower->y;
23190
23191 /* The lower end of the intersection is the minimum of the lower
23192 ends of upper and lower. */
23193 result->height = (min (lower->y + lower->height,
23194 upper->y + upper->height)
23195 - result->y);
23196 intersection_p = 1;
133c764e
KS
23197 }
23198 }
fa3c6b4d
KS
23199
23200 return intersection_p;
133c764e
KS
23201}
23202
fa3c6b4d
KS
23203#endif /* HAVE_WINDOW_SYSTEM */
23204
cfe03a41 23205\f
5f5c8ee5
GM
23206/***********************************************************************
23207 Initialization
23208 ***********************************************************************/
23209
a2889657
JB
23210void
23211syms_of_xdisp ()
23212{
c6e89d6c
GM
23213 Vwith_echo_area_save_vector = Qnil;
23214 staticpro (&Vwith_echo_area_save_vector);
5f5c8ee5 23215
c6e89d6c
GM
23216 Vmessage_stack = Qnil;
23217 staticpro (&Vmessage_stack);
2311178e 23218
735c094c 23219 Qinhibit_redisplay = intern ("inhibit-redisplay");
c6e89d6c 23220 staticpro (&Qinhibit_redisplay);
735c094c 23221
b14bc55e
RS
23222 message_dolog_marker1 = Fmake_marker ();
23223 staticpro (&message_dolog_marker1);
23224 message_dolog_marker2 = Fmake_marker ();
23225 staticpro (&message_dolog_marker2);
23226 message_dolog_marker3 = Fmake_marker ();
23227 staticpro (&message_dolog_marker3);
23228
5f5c8ee5 23229#if GLYPH_DEBUG
7d4cc828 23230 defsubr (&Sdump_frame_glyph_matrix);
5f5c8ee5
GM
23231 defsubr (&Sdump_glyph_matrix);
23232 defsubr (&Sdump_glyph_row);
e037b9ec 23233 defsubr (&Sdump_tool_bar_row);
62397849 23234 defsubr (&Strace_redisplay);
bf9249e3 23235 defsubr (&Strace_to_stderr);
5f5c8ee5 23236#endif
99a5de87 23237#ifdef HAVE_WINDOW_SYSTEM
57c28064 23238 defsubr (&Stool_bar_lines_needed);
493fdc3c 23239 defsubr (&Slookup_image_map);
99a5de87 23240#endif
8143e6ab 23241 defsubr (&Sformat_mode_line);
5f5c8ee5 23242
cf074754
RS
23243 staticpro (&Qmenu_bar_update_hook);
23244 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
23245
d46fb96a 23246 staticpro (&Qoverriding_terminal_local_map);
7079aefa 23247 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
d46fb96a 23248
399164b4
KH
23249 staticpro (&Qoverriding_local_map);
23250 Qoverriding_local_map = intern ("overriding-local-map");
23251
75c43375
RS
23252 staticpro (&Qwindow_scroll_functions);
23253 Qwindow_scroll_functions = intern ("window-scroll-functions");
23254
e0bfbde6
RS
23255 staticpro (&Qredisplay_end_trigger_functions);
23256 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
2311178e 23257
2e54982e
RS
23258 staticpro (&Qinhibit_point_motion_hooks);
23259 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
23260
9499d71b
GM
23261 QCdata = intern (":data");
23262 staticpro (&QCdata);
5f5c8ee5 23263 Qdisplay = intern ("display");
f3751a65 23264 staticpro (&Qdisplay);
5f5c8ee5
GM
23265 Qspace_width = intern ("space-width");
23266 staticpro (&Qspace_width);
5f5c8ee5
GM
23267 Qraise = intern ("raise");
23268 staticpro (&Qraise);
e893970b
KS
23269 Qslice = intern ("slice");
23270 staticpro (&Qslice);
5f5c8ee5
GM
23271 Qspace = intern ("space");
23272 staticpro (&Qspace);
f3751a65
GM
23273 Qmargin = intern ("margin");
23274 staticpro (&Qmargin);
493fdc3c
KS
23275 Qpointer = intern ("pointer");
23276 staticpro (&Qpointer);
5f5c8ee5 23277 Qleft_margin = intern ("left-margin");
f3751a65 23278 staticpro (&Qleft_margin);
5f5c8ee5 23279 Qright_margin = intern ("right-margin");
f3751a65 23280 staticpro (&Qright_margin);
da06e4d7
KS
23281 Qcenter = intern ("center");
23282 staticpro (&Qcenter);
4933c603
KS
23283 Qline_height = intern ("line-height");
23284 staticpro (&Qline_height);
5f5c8ee5
GM
23285 QCalign_to = intern (":align-to");
23286 staticpro (&QCalign_to);
5f5c8ee5
GM
23287 QCrelative_width = intern (":relative-width");
23288 staticpro (&QCrelative_width);
23289 QCrelative_height = intern (":relative-height");
23290 staticpro (&QCrelative_height);
23291 QCeval = intern (":eval");
23292 staticpro (&QCeval);
0fcf414f
RS
23293 QCpropertize = intern (":propertize");
23294 staticpro (&QCpropertize);
886bd6f2
GM
23295 QCfile = intern (":file");
23296 staticpro (&QCfile);
5f5c8ee5
GM
23297 Qfontified = intern ("fontified");
23298 staticpro (&Qfontified);
23299 Qfontification_functions = intern ("fontification-functions");
23300 staticpro (&Qfontification_functions);
5f5c8ee5
GM
23301 Qtrailing_whitespace = intern ("trailing-whitespace");
23302 staticpro (&Qtrailing_whitespace);
30374745
RS
23303 Qescape_glyph = intern ("escape-glyph");
23304 staticpro (&Qescape_glyph);
9c69e6e1
RS
23305 Qnobreak_space = intern ("nobreak-space");
23306 staticpro (&Qnobreak_space);
5f5c8ee5
GM
23307 Qimage = intern ("image");
23308 staticpro (&Qimage);
493fdc3c
KS
23309 QCmap = intern (":map");
23310 staticpro (&QCmap);
23311 QCpointer = intern (":pointer");
23312 staticpro (&QCpointer);
23313 Qrect = intern ("rect");
23314 staticpro (&Qrect);
23315 Qcircle = intern ("circle");
23316 staticpro (&Qcircle);
23317 Qpoly = intern ("poly");
23318 staticpro (&Qpoly);
ad4f174e
GM
23319 Qmessage_truncate_lines = intern ("message-truncate-lines");
23320 staticpro (&Qmessage_truncate_lines);
6422c1d7
GM
23321 Qgrow_only = intern ("grow-only");
23322 staticpro (&Qgrow_only);
e1477f43
GM
23323 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
23324 staticpro (&Qinhibit_menubar_update);
30a3f61c
GM
23325 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
23326 staticpro (&Qinhibit_eval_during_redisplay);
b384d6f8
GM
23327 Qposition = intern ("position");
23328 staticpro (&Qposition);
23329 Qbuffer_position = intern ("buffer-position");
23330 staticpro (&Qbuffer_position);
23331 Qobject = intern ("object");
23332 staticpro (&Qobject);
cfe03a41
KS
23333 Qbar = intern ("bar");
23334 staticpro (&Qbar);
23335 Qhbar = intern ("hbar");
23336 staticpro (&Qhbar);
23337 Qbox = intern ("box");
23338 staticpro (&Qbox);
23339 Qhollow = intern ("hollow");
23340 staticpro (&Qhollow);
493fdc3c
KS
23341 Qhand = intern ("hand");
23342 staticpro (&Qhand);
23343 Qarrow = intern ("arrow");
23344 staticpro (&Qarrow);
23345 Qtext = intern ("text");
23346 staticpro (&Qtext);
c53a1624
RS
23347 Qrisky_local_variable = intern ("risky-local-variable");
23348 staticpro (&Qrisky_local_variable);
26683087
RS
23349 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
23350 staticpro (&Qinhibit_free_realized_faces);
5f5c8ee5 23351
be21b925
KS
23352 list_of_error = Fcons (Fcons (intern ("error"),
23353 Fcons (intern ("void-variable"), Qnil)),
23354 Qnil);
7033d6df
RS
23355 staticpro (&list_of_error);
23356
351b5434
KS
23357 Qlast_arrow_position = intern ("last-arrow-position");
23358 staticpro (&Qlast_arrow_position);
23359 Qlast_arrow_string = intern ("last-arrow-string");
23360 staticpro (&Qlast_arrow_string);
23361
23362 Qoverlay_arrow_string = intern ("overlay-arrow-string");
23363 staticpro (&Qoverlay_arrow_string);
23364 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
23365 staticpro (&Qoverlay_arrow_bitmap);
2311178e 23366
c6e89d6c
GM
23367 echo_buffer[0] = echo_buffer[1] = Qnil;
23368 staticpro (&echo_buffer[0]);
23369 staticpro (&echo_buffer[1]);
23370
23371 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
23372 staticpro (&echo_area_buffer[0]);
23373 staticpro (&echo_area_buffer[1]);
a2889657 23374
6a94510a
GM
23375 Vmessages_buffer_name = build_string ("*Messages*");
23376 staticpro (&Vmessages_buffer_name);
0fcf414f
RS
23377
23378 mode_line_proptrans_alist = Qnil;
23379 staticpro (&mode_line_proptrans_alist);
fec8f23e
KS
23380 mode_line_string_list = Qnil;
23381 staticpro (&mode_line_string_list);
4c0e4b80
KS
23382 mode_line_string_face = Qnil;
23383 staticpro (&mode_line_string_face);
23384 mode_line_string_face_prop = Qnil;
23385 staticpro (&mode_line_string_face_prop);
23386 Vmode_line_unwind_vector = Qnil;
23387 staticpro (&Vmode_line_unwind_vector);
fec8f23e 23388
fa3c6b4d
KS
23389 help_echo_string = Qnil;
23390 staticpro (&help_echo_string);
23391 help_echo_object = Qnil;
23392 staticpro (&help_echo_object);
23393 help_echo_window = Qnil;
23394 staticpro (&help_echo_window);
23395 previous_help_echo_string = Qnil;
23396 staticpro (&previous_help_echo_string);
23397 help_echo_pos = -1;
23398
23399#ifdef HAVE_WINDOW_SYSTEM
23400 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
23401 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
23402For example, if a block cursor is over a tab, it will be drawn as
23403wide as that tab on the display. */);
23404 x_stretch_cursor_p = 0;
23405#endif
23406
7ee72033 23407 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
88e6b646 23408 doc: /* *Non-nil means highlight trailing whitespace.
228299fa 23409The face used for trailing whitespace is `trailing-whitespace'. */);
8f897821
GM
23410 Vshow_trailing_whitespace = Qnil;
23411
9c69e6e1
RS
23412 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
23413 doc: /* *Control highlighting of nobreak space and soft hyphen.
0afd0871
JB
23414A value of t means highlight the character itself (for nobreak space,
23415use face `nobreak-space').
23416A value of nil means no highlighting.
23417Other values mean display the escape glyph followed by an ordinary
9c69e6e1
RS
23418space or ordinary hyphen. */);
23419 Vnobreak_char_display = Qt;
fe1c2006 23420
493fdc3c 23421 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
88e6b646 23422 doc: /* *The pointer shape to show in void text areas.
0afd0871
JB
23423A value of nil means to show the text pointer. Other options are `arrow',
23424`text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
493fdc3c 23425 Vvoid_text_area_pointer = Qarrow;
f65536fa 23426
7ee72033
MB
23427 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
23428 doc: /* Non-nil means don't actually do any redisplay.
228299fa 23429This is used for internal purposes. */);
735c094c
KH
23430 Vinhibit_redisplay = Qnil;
23431
7ee72033
MB
23432 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
23433 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
a2889657
JB
23434 Vglobal_mode_string = Qnil;
23435
7ee72033
MB
23436 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
23437 doc: /* Marker for where to display an arrow on top of the buffer text.
228299fa
GM
23438This must be the beginning of a line in order to work.
23439See also `overlay-arrow-string'. */);
a2889657
JB
23440 Voverlay_arrow_position = Qnil;
23441
7ee72033 23442 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
351b5434
KS
23443 doc: /* String to display as an arrow in non-window frames.
23444See also `overlay-arrow-position'. */);
5b487988 23445 Voverlay_arrow_string = build_string ("=>");
a2889657 23446
351b5434
KS
23447 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
23448 doc: /* List of variables (symbols) which hold markers for overlay arrows.
23449The symbols on this list are examined during redisplay to determine
23450where to display overlay arrows. */);
23451 Voverlay_arrow_variable_list
23452 = Fcons (intern ("overlay-arrow-position"), Qnil);
23453
7ee72033
MB
23454 DEFVAR_INT ("scroll-step", &scroll_step,
23455 doc: /* *The number of lines to try scrolling a window by when point moves out.
228299fa
GM
23456If that fails to bring point back on frame, point is centered instead.
23457If this is zero, point is always centered after it moves off frame.
23458If you want scrolling to always be a line at a time, you should set
23459`scroll-conservatively' to a large value rather than set this to 1. */);
23460
7ee72033
MB
23461 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
23462 doc: /* *Scroll up to this many lines, to bring point back on screen.
228299fa
GM
23463A value of zero means to scroll the text to center point vertically
23464in the window. */);
0789adb2
RS
23465 scroll_conservatively = 0;
23466
7ee72033
MB
23467 DEFVAR_INT ("scroll-margin", &scroll_margin,
23468 doc: /* *Number of lines of margin at the top and bottom of a window.
228299fa
GM
23469Recenter the window whenever point gets within this many lines
23470of the top or bottom of the window. */);
9afd2168
RS
23471 scroll_margin = 0;
23472
f65536fa 23473 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
c0d7caba 23474 doc: /* Pixels per inch value for non-window system displays.
f65536fa
KS
23475Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
23476 Vdisplay_pixels_per_inch = make_float (72.0);
23477
5f5c8ee5 23478#if GLYPH_DEBUG
7ee72033 23479 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
5f5c8ee5 23480#endif
a2889657
JB
23481
23482 DEFVAR_BOOL ("truncate-partial-width-windows",
7ee72033
MB
23483 &truncate_partial_width_windows,
23484 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
a2889657
JB
23485 truncate_partial_width_windows = 1;
23486
7ee72033
MB
23487 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
23488 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
228299fa 23489Any other value means to use the appropriate face, `mode-line',
ccfe8f57 23490`header-line', or `menu' respectively. */);
1862a24e 23491 mode_line_inverse_video = 1;
aa6d10fa 23492
7ee72033
MB
23493 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
23494 doc: /* *Maximum buffer size for which line number should be displayed.
228299fa
GM
23495If the buffer is bigger than this, the line number does not appear
23496in the mode line. A value of nil means no limit. */);
090703f4 23497 Vline_number_display_limit = Qnil;
fba9ce76 23498
090703f4 23499 DEFVAR_INT ("line-number-display-limit-width",
7ee72033
MB
23500 &line_number_display_limit_width,
23501 doc: /* *Maximum line width (in characters) for line number display.
228299fa
GM
23502If the average length of the lines near point is bigger than this, then the
23503line number may be omitted from the mode line. */);
5d121aec
KH
23504 line_number_display_limit_width = 200;
23505
7ee72033
MB
23506 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
23507 doc: /* *Non-nil means highlight region even in nonselected windows. */);
293a54ce 23508 highlight_nonselected_windows = 0;
d39b6696 23509
7ee72033
MB
23510 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
23511 doc: /* Non-nil if more than one frame is visible on this display.
228299fa
GM
23512Minibuffer-only frames don't count, but iconified frames do.
23513This variable is not guaranteed to be accurate except while processing
23514`frame-title-format' and `icon-title-format'. */);
23515
7ee72033
MB
23516 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
23517 doc: /* Template for displaying the title bar of visible frames.
228299fa
GM
23518\(Assuming the window manager supports this feature.)
23519This variable has the same structure as `mode-line-format' (which see),
23520and is used only on frames for which no explicit name has been set
23521\(see `modify-frame-parameters'). */);
f65536fa 23522
7ee72033
MB
23523 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
23524 doc: /* Template for displaying the title bar of an iconified frame.
228299fa
GM
23525\(Assuming the window manager supports this feature.)
23526This variable has the same structure as `mode-line-format' (which see),
23527and is used only on frames for which no explicit name has been set
23528\(see `modify-frame-parameters'). */);
d39b6696
KH
23529 Vicon_title_format
23530 = Vframe_title_format
23531 = Fcons (intern ("multiple-frames"),
23532 Fcons (build_string ("%b"),
3ebf0ea9 23533 Fcons (Fcons (empty_string,
d39b6696
KH
23534 Fcons (intern ("invocation-name"),
23535 Fcons (build_string ("@"),
23536 Fcons (intern ("system-name"),
23537 Qnil)))),
23538 Qnil)));
5992c4f7 23539
7ee72033
MB
23540 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
23541 doc: /* Maximum number of lines to keep in the message log buffer.
228299fa
GM
23542If nil, disable message logging. If t, log messages but don't truncate
23543the buffer when it becomes large. */);
ac90c44f 23544 Vmessage_log_max = make_number (50);
08b610e4 23545
7ee72033
MB
23546 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
23547 doc: /* Functions called before redisplay, if window sizes have changed.
228299fa
GM
23548The value should be a list of functions that take one argument.
23549Just before redisplay, for each frame, if any of its windows have changed
23550size since the last redisplay, or have been split or deleted,
23551all the functions in the list are called, with the frame as argument. */);
08b610e4 23552 Vwindow_size_change_functions = Qnil;
75c43375 23553
7ee72033 23554 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
4b9da0fb 23555 doc: /* List of functions to call before redisplaying a window with scrolling.
228299fa
GM
23556Each function is called with two arguments, the window
23557and its new display-start position. Note that the value of `window-end'
23558is not valid when these functions are called. */);
75c43375 23559 Vwindow_scroll_functions = Qnil;
2311178e 23560
e2b64074
JB
23561 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
23562 doc: /* Functions called when redisplay of a window reaches the end trigger.
23563Each function is called with two arguments, the window and the end trigger value.
23564See `set-window-redisplay-end-trigger'. */);
23565 Vredisplay_end_trigger_functions = Qnil;
23566
fa3c6b4d
KS
23567 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
23568 doc: /* *Non-nil means autoselect window with mouse pointer. */);
23569 mouse_autoselect_window = 0;
23570
7ee72033
MB
23571 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
23572 doc: /* *Non-nil means automatically resize tool-bars.
228299fa
GM
23573This increases a tool-bar's height if not all tool-bar items are visible.
23574It decreases a tool-bar's height when it would display blank lines
23575otherwise. */);
e037b9ec 23576 auto_resize_tool_bars_p = 1;
2311178e 23577
7ee72033
MB
23578 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
23579 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
e037b9ec 23580 auto_raise_tool_bar_buttons_p = 1;
5f5c8ee5 23581
8232c913
KS
23582 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
23583 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
23584 make_cursor_line_fully_visible_p = 1;
23585
7ee72033
MB
23586 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
23587 doc: /* *Margin around tool-bar buttons in pixels.
228299fa 23588If an integer, use that for both horizontal and vertical margins.
f6c89f27 23589Otherwise, value should be a pair of integers `(HORZ . VERT)' with
228299fa
GM
23590HORZ specifying the horizontal margin, and VERT specifying the
23591vertical margin. */);
c3d76173 23592 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
5f5c8ee5 23593
7ee72033 23594 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
6da3c85b 23595 doc: /* *Relief thickness of tool-bar buttons. */);
c3d76173 23596 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
5f5c8ee5 23597
7ee72033
MB
23598 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
23599 doc: /* List of functions to call to fontify regions of text.
228299fa
GM
23600Each function is called with one argument POS. Functions must
23601fontify a region starting at POS in the current buffer, and give
23602fontified regions the property `fontified'. */);
5f5c8ee5 23603 Vfontification_functions = Qnil;
6b9f0906 23604 Fmake_variable_buffer_local (Qfontification_functions);
7bbe686f
AI
23605
23606 DEFVAR_BOOL ("unibyte-display-via-language-environment",
7ee72033
MB
23607 &unibyte_display_via_language_environment,
23608 doc: /* *Non-nil means display unibyte text according to language environment.
228299fa
GM
23609Specifically this means that unibyte non-ASCII characters
23610are displayed by converting them to the equivalent multibyte characters
23611according to the current language environment. As a result, they are
23612displayed according to the current fontset. */);
7bbe686f 23613 unibyte_display_via_language_environment = 0;
c6e89d6c 23614
7ee72033
MB
23615 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
23616 doc: /* *Maximum height for resizing mini-windows.
228299fa
GM
23617If a float, it specifies a fraction of the mini-window frame's height.
23618If an integer, it specifies a number of lines. */);
c6e89d6c 23619 Vmax_mini_window_height = make_float (0.25);
6422c1d7 23620
7ee72033
MB
23621 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
23622 doc: /* *How to resize mini-windows.
228299fa
GM
23623A value of nil means don't automatically resize mini-windows.
23624A value of t means resize them to fit the text displayed in them.
23625A value of `grow-only', the default, means let mini-windows grow
23626only, until their display becomes empty, at which point the windows
23627go back to their normal size. */);
6422c1d7
GM
23628 Vresize_mini_windows = Qgrow_only;
23629
cfe03a41
KS
23630 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
23631 doc: /* Alist specifying how to blink the cursor off.
23632Each element has the form (ON-STATE . OFF-STATE). Whenever the
23633`cursor-type' frame-parameter or variable equals ON-STATE,
23634comparing using `equal', Emacs uses OFF-STATE to specify
2cb33a98
RS
23635how to blink it off. ON-STATE and OFF-STATE are values for
23636the `cursor-type' frame parameter.
23637
23638If a frame's ON-STATE has no entry in this list,
23639the frame's other specifications determine how to blink the cursor off. */);
cfe03a41 23640 Vblink_cursor_alist = Qnil;
2311178e 23641
e76d28d5 23642 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
7ee72033 23643 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
d475bcb8 23644 automatic_hscrolling_p = 1;
1df7e8f0 23645
e76d28d5 23646 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
1df7e8f0
EZ
23647 doc: /* *How many columns away from the window edge point is allowed to get
23648before automatic hscrolling will horizontally scroll the window. */);
e76d28d5 23649 hscroll_margin = 5;
1df7e8f0 23650
e76d28d5 23651 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
1df7e8f0
EZ
23652 doc: /* *How many columns to scroll the window when point gets too close to the edge.
23653When point is less than `automatic-hscroll-margin' columns from the window
23654edge, automatic hscrolling will scroll the window by the amount of columns
23655determined by this variable. If its value is a positive integer, scroll that
23656many columns. If it's a positive floating-point number, it specifies the
23657fraction of the window's width to scroll. If it's nil or zero, point will be
23658centered horizontally after the scroll. Any other value, including negative
23659numbers, are treated as if the value were zero.
23660
23661Automatic hscrolling always moves point outside the scroll margin, so if
23662point was more than scroll step columns inside the margin, the window will
23663scroll more than the value given by the scroll step.
23664
23665Note that the lower bound for automatic hscrolling specified by `scroll-left'
23666and `scroll-right' overrides this variable's effect. */);
e76d28d5 23667 Vhscroll_step = make_number (0);
2311178e 23668
7ee72033
MB
23669 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
23670 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
228299fa 23671Bind this around calls to `message' to let it take effect. */);
ad4f174e 23672 message_truncate_lines = 0;
0bca8940 23673
7ee72033 23674 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
e6d09683
RS
23675 doc: /* Normal hook run to update the menu bar definitions.
23676Redisplay runs this hook before it redisplays the menu bar.
23677This is used to update submenus such as Buffers,
23678whose contents depend on various data. */);
6422c1d7 23679 Vmenu_bar_update_hook = Qnil;
2311178e 23680
7ee72033
MB
23681 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
23682 doc: /* Non-nil means don't update menu bars. Internal use only. */);
e1477f43 23683 inhibit_menubar_update = 0;
30a3f61c 23684
7ee72033
MB
23685 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
23686 doc: /* Non-nil means don't eval Lisp during redisplay. */);
30a3f61c 23687 inhibit_eval_during_redisplay = 0;
76cb5e06 23688
26683087
RS
23689 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
23690 doc: /* Non-nil means don't free realized faces. Internal use only. */);
23691 inhibit_free_realized_faces = 0;
23692
69d1f7c9 23693#if GLYPH_DEBUG
76cb5e06
GM
23694 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
23695 doc: /* Inhibit try_window_id display optimization. */);
23696 inhibit_try_window_id = 0;
23697
23698 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
23699 doc: /* Inhibit try_window_reusing display optimization. */);
23700 inhibit_try_window_reusing = 0;
23701
23702 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
23703 doc: /* Inhibit try_cursor_movement display optimization. */);
23704 inhibit_try_cursor_movement = 0;
23705#endif /* GLYPH_DEBUG */
a2889657
JB
23706}
23707
5f5c8ee5
GM
23708
23709/* Initialize this module when Emacs starts. */
23710
dfcf069d 23711void
a2889657
JB
23712init_xdisp ()
23713{
23714 Lisp_Object root_window;
5f5c8ee5 23715 struct window *mini_w;
a2889657 23716
04612a64
GM
23717 current_header_line_height = current_mode_line_height = -1;
23718
5f5c8ee5 23719 CHARPOS (this_line_start_pos) = 0;
a2889657
JB
23720
23721 mini_w = XWINDOW (minibuf_window);
11e82b76 23722 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
a2889657 23723
a2889657
JB
23724 if (!noninteractive)
23725 {
5f5c8ee5
GM
23726 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
23727 int i;
23728
da8b7f4f 23729 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
12c226c5 23730 set_window_height (root_window,
da8b7f4f 23731 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
12c226c5 23732 0);
da8b7f4f 23733 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
a2889657
JB
23734 set_window_height (minibuf_window, 1, 0);
23735
da8b7f4f
KS
23736 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
23737 mini_w->total_cols = make_number (FRAME_COLS (f));
5f5c8ee5
GM
23738
23739 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
23740 scratch_glyph_row.glyphs[TEXT_AREA + 1]
23741 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
23742
2311178e 23743 /* The default ellipsis glyphs `...'. */
5f5c8ee5 23744 for (i = 0; i < 3; ++i)
ac90c44f 23745 default_invis_vector[i] = make_number ('.');
a2889657 23746 }
5f5c8ee5 23747
5f5c8ee5 23748 {
93da8435
SM
23749 /* Allocate the buffer for frame titles.
23750 Also used for `format-mode-line'. */
5f5c8ee5 23751 int size = 100;
4c0e4b80
KS
23752 mode_line_noprop_buf = (char *) xmalloc (size);
23753 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
23754 mode_line_noprop_ptr = mode_line_noprop_buf;
23755 mode_line_target = MODE_LINE_DISPLAY;
5f5c8ee5 23756 }
2311178e 23757
21fdfb65 23758 help_echo_showing_p = 0;
a2889657 23759}
5f5c8ee5
GM
23760
23761
ab5796a9
MB
23762/* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
23763 (do not change this comment) */