(get_glyph_string_clip_rect): Fix reduction of cursor
[bpt/emacs.git] / src / xdisp.c
CommitLineData
a2889657 1/* Display generation from window structure and buffer text.
7af0e8d7 2 Copyright (C) 1985,86,87,88,93,94,95,97,98,99,2000,01,02,03,04
5f5c8ee5 3 Free Software Foundation, Inc.
a2889657
JB
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
b1d1124b 9the Free Software Foundation; either version 2, or (at your option)
a2889657
JB
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
a2889657 21
5f5c8ee5
GM
22/* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
48400103 36 let's say almost---see the description of direct update
4b41cebb 37 operations, below.)
5f5c8ee5
GM
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
2311178e 50 direct_output_for_insert,
5f5c8ee5
GM
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
82a7ab23 55 +--------------+ redisplay +----------------+
5f5c8ee5
GM
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
959bc044 67 What does redisplay do? Obviously, it has to figure out somehow what
5f5c8ee5
GM
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
48400103 91 You will find a lot of redisplay optimizations when you start
5f5c8ee5
GM
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
2311178e 109
5f5c8ee5
GM
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
959bc044 123 interface functions taking an iterator structure (struct it)
5f5c8ee5
GM
124 argument.
125
48400103 126 Iteration over things to be displayed is then simple. It is
1178ddde
GM
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
5f5c8ee5
GM
130 set_iterator_to_next move the iterator to the next thing.
131
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
138
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
146
147
148 Frame matrices.
149
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
156
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
a2889657 168
18160b98 169#include <config.h>
a2889657 170#include <stdio.h>
7ee72033 171
a2889657 172#include "lisp.h"
546a4f00 173#include "keyboard.h"
44fa5b1e 174#include "frame.h"
a2889657
JB
175#include "window.h"
176#include "termchar.h"
177#include "dispextern.h"
178#include "buffer.h"
1c9241f5 179#include "charset.h"
a2889657
JB
180#include "indent.h"
181#include "commands.h"
fa3c6b4d 182#include "keymap.h"
a2889657
JB
183#include "macros.h"
184#include "disptab.h"
30c566e4 185#include "termhooks.h"
b0a0fbda 186#include "intervals.h"
1c9241f5
KH
187#include "coding.h"
188#include "process.h"
dfcf069d 189#include "region-cache.h"
0ef75e87 190#include "fontset.h"
f9b3d256 191#include "blockinput.h"
dfcf069d 192
6d55d620 193#ifdef HAVE_X_WINDOWS
dfcf069d
AS
194#include "xterm.h"
195#endif
a75dfea0
AI
196#ifdef WINDOWSNT
197#include "w32term.h"
198#endif
e0f712ba 199#ifdef MAC_OS
1a578e9b 200#include "macterm.h"
365fa1b3
AC
201
202Cursor No_Cursor;
1a578e9b 203#endif
a2889657 204
1dabd0cf
KS
205#ifndef FRAME_X_OUTPUT
206#define FRAME_X_OUTPUT(f) ((f)->output_data.x)
207#endif
208
5f5c8ee5
GM
209#define INFINITY 10000000
210
488dd4c4
JD
211#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
212 || defined (USE_GTK)
2e621225 213extern void set_frame_menubar P_ ((struct frame *f, int, int));
cd6dfed6 214extern int pending_menu_activation;
76412d64
RS
215#endif
216
a2889657
JB
217extern int interrupt_input;
218extern int command_loop_level;
219
b6436d4e 220extern int minibuffer_auto_raise;
b2b5455d 221extern Lisp_Object Vminibuffer_list;
b6436d4e 222
c4628384 223extern Lisp_Object Qface;
fec8f23e 224extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
c4628384 225
399164b4
KH
226extern Lisp_Object Voverriding_local_map;
227extern Lisp_Object Voverriding_local_map_menu_flag;
5f5c8ee5 228extern Lisp_Object Qmenu_item;
0b063c27 229extern Lisp_Object Qwhen;
fa3c6b4d 230extern Lisp_Object Qhelp_echo;
399164b4 231
d46fb96a 232Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
75c43375 233Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
e0bfbde6 234Lisp_Object Qredisplay_end_trigger_functions;
2e54982e 235Lisp_Object Qinhibit_point_motion_hooks;
0b063c27 236Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
5f5c8ee5 237Lisp_Object Qfontified;
6422c1d7 238Lisp_Object Qgrow_only;
869fb12c 239Lisp_Object Qinhibit_eval_during_redisplay;
b384d6f8 240Lisp_Object Qbuffer_position, Qposition, Qobject;
5f5c8ee5 241
cfe03a41
KS
242/* Cursor shapes */
243Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
244
493fdc3c
KS
245/* Pointer shapes */
246Lisp_Object Qarrow, Qhand, Qtext;
247
c53a1624
RS
248Lisp_Object Qrisky_local_variable;
249
7033d6df
RS
250/* Holds the list (error). */
251Lisp_Object list_of_error;
252
5f5c8ee5
GM
253/* Functions called to fontify regions of text. */
254
255Lisp_Object Vfontification_functions;
256Lisp_Object Qfontification_functions;
257
fa3c6b4d
KS
258/* Non-zero means automatically select any window when the mouse
259 cursor moves into it. */
260int mouse_autoselect_window;
261
e037b9ec 262/* Non-zero means draw tool bar buttons raised when the mouse moves
5f5c8ee5
GM
263 over them. */
264
e037b9ec 265int auto_raise_tool_bar_buttons_p;
5f5c8ee5 266
e037b9ec 267/* Margin around tool bar buttons in pixels. */
5f5c8ee5 268
35a41507 269Lisp_Object Vtool_bar_button_margin;
5f5c8ee5 270
e037b9ec 271/* Thickness of shadow to draw around tool bar buttons. */
5f5c8ee5 272
31ade731 273EMACS_INT tool_bar_button_relief;
5f5c8ee5 274
e037b9ec 275/* Non-zero means automatically resize tool-bars so that all tool-bar
5f5c8ee5
GM
276 items are visible, and no blank lines remain. */
277
e037b9ec 278int auto_resize_tool_bars_p;
399164b4 279
fa3c6b4d
KS
280/* Non-zero means draw block and hollow cursor as wide as the glyph
281 under it. For example, if a block cursor is over a tab, it will be
282 drawn as wide as that tab on the display. */
283
284int x_stretch_cursor_p;
285
735c094c
KH
286/* Non-nil means don't actually do any redisplay. */
287
288Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
289
30a3f61c
GM
290/* Non-zero means Lisp evaluation during redisplay is inhibited. */
291
869fb12c 292int inhibit_eval_during_redisplay;
30a3f61c 293
5f5c8ee5
GM
294/* Names of text properties relevant for redisplay. */
295
493fdc3c 296Lisp_Object Qdisplay;
4b41cebb 297extern Lisp_Object Qface, Qinvisible, Qwidth;
5f5c8ee5
GM
298
299/* Symbols used in text property values. */
300
f65536fa 301Lisp_Object Vdisplay_pixels_per_inch;
5f5c8ee5 302Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
a7e27ef7 303Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
da06e4d7 304Lisp_Object Qcenter;
493fdc3c 305Lisp_Object Qmargin, Qpointer;
a7e27ef7 306extern Lisp_Object Qheight;
133c764e 307extern Lisp_Object QCwidth, QCheight, QCascent;
f65536fa 308extern Lisp_Object Qscroll_bar;
5f5c8ee5 309
8f897821 310/* Non-nil means highlight trailing whitespace. */
5f5c8ee5 311
8f897821 312Lisp_Object Vshow_trailing_whitespace;
5f5c8ee5 313
7af0e8d7 314#ifdef HAVE_WINDOW_SYSTEM
6d925726 315extern Lisp_Object Voverflow_newline_into_fringe;
88e6b646
KS
316
317/* Test if overflow newline into fringe. Called with iterator IT
be21b925 318 at or past right window margin, and with IT->current_x set. */
88e6b646
KS
319
320#define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
321 (!NILP (Voverflow_newline_into_fringe) \
322 && FRAME_WINDOW_P (it->f) \
323 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
324 && it->current_x == it->last_visible_x)
325
6d925726
KS
326#endif /* HAVE_WINDOW_SYSTEM */
327
f65536fa
KS
328/* Non-nil means show the text cursor in void text areas
329 i.e. in blank areas after eol and eob. This used to be
330 the default in 21.3. */
331
493fdc3c 332Lisp_Object Vvoid_text_area_pointer;
f65536fa 333
5f5c8ee5
GM
334/* Name of the face used to highlight trailing whitespace. */
335
336Lisp_Object Qtrailing_whitespace;
337
338/* The symbol `image' which is the car of the lists used to represent
339 images in Lisp. */
340
341Lisp_Object Qimage;
342
493fdc3c
KS
343/* The image map types. */
344Lisp_Object QCmap, QCpointer;
345Lisp_Object Qrect, Qcircle, Qpoly;
346
5f5c8ee5
GM
347/* Non-zero means print newline to stdout before next mini-buffer
348 message. */
a2889657
JB
349
350int noninteractive_need_newline;
351
5f5c8ee5 352/* Non-zero means print newline to message log before next message. */
f88eb0b6 353
3c6595e0 354static int message_log_need_newline;
f88eb0b6 355
b14bc55e
RS
356/* Three markers that message_dolog uses.
357 It could allocate them itself, but that causes trouble
358 in handling memory-full errors. */
359static Lisp_Object message_dolog_marker1;
360static Lisp_Object message_dolog_marker2;
361static Lisp_Object message_dolog_marker3;
5f5c8ee5
GM
362\f
363/* The buffer position of the first character appearing entirely or
364 partially on the line of the selected window which contains the
365 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
366 redisplay optimization in redisplay_internal. */
a2889657 367
5f5c8ee5 368static struct text_pos this_line_start_pos;
a2889657 369
5f5c8ee5
GM
370/* Number of characters past the end of the line above, including the
371 terminating newline. */
372
373static struct text_pos this_line_end_pos;
374
375/* The vertical positions and the height of this line. */
a2889657 376
a2889657 377static int this_line_vpos;
5f5c8ee5
GM
378static int this_line_y;
379static int this_line_pixel_height;
380
381/* X position at which this display line starts. Usually zero;
382 negative if first character is partially visible. */
383
384static int this_line_start_x;
a2889657 385
5f5c8ee5 386/* Buffer that this_line_.* variables are referring to. */
a2889657 387
a2889657
JB
388static struct buffer *this_line_buffer;
389
5f5c8ee5
GM
390/* Nonzero means truncate lines in all windows less wide than the
391 frame. */
a2889657 392
a2889657
JB
393int truncate_partial_width_windows;
394
7bbe686f 395/* A flag to control how to display unibyte 8-bit character. */
5f5c8ee5 396
7bbe686f 397int unibyte_display_via_language_environment;
2311178e 398
5f5c8ee5
GM
399/* Nonzero means we have more than one non-mini-buffer-only frame.
400 Not guaranteed to be accurate except while parsing
401 frame-title-format. */
7bbe686f 402
d39b6696
KH
403int multiple_frames;
404
a2889657
JB
405Lisp_Object Vglobal_mode_string;
406
351b5434
KS
407
408/* List of variables (symbols) which hold markers for overlay arrows.
409 The symbols on this list are examined during redisplay to determine
be21b925 410 where to display overlay arrows. */
351b5434
KS
411
412Lisp_Object Voverlay_arrow_variable_list;
413
a2889657 414/* Marker for where to display an arrow on top of the buffer text. */
5f5c8ee5 415
a2889657
JB
416Lisp_Object Voverlay_arrow_position;
417
5f5c8ee5
GM
418/* String to display for the arrow. Only used on terminal frames. */
419
a2889657
JB
420Lisp_Object Voverlay_arrow_string;
421
351b5434
KS
422/* Values of those variables at last redisplay are stored as
423 properties on `overlay-arrow-position' symbol. However, if
424 Voverlay_arrow_position is a marker, last-arrow-position is its
5f5c8ee5
GM
425 numerical position. */
426
351b5434
KS
427Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
428
429/* Alternative overlay-arrow-string and overlay-arrow-bitmap
430 properties on a symbol in overlay-arrow-variable-list. */
431
432Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
d45de95b 433
5f5c8ee5
GM
434/* Like mode-line-format, but for the title bar on a visible frame. */
435
d39b6696
KH
436Lisp_Object Vframe_title_format;
437
5f5c8ee5
GM
438/* Like mode-line-format, but for the title bar on an iconified frame. */
439
d39b6696
KH
440Lisp_Object Vicon_title_format;
441
08b610e4
RS
442/* List of functions to call when a window's size changes. These
443 functions get one arg, a frame on which one or more windows' sizes
444 have changed. */
5f5c8ee5 445
08b610e4
RS
446static Lisp_Object Vwindow_size_change_functions;
447
0bca8940 448Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
cf074754 449
a2889657 450/* Nonzero if overlay arrow has been displayed once in this window. */
a2889657 451
5f5c8ee5 452static int overlay_arrow_seen;
ca26e1c8 453
fba9ce76 454/* Nonzero means highlight the region even in nonselected windows. */
fba9ce76 455
5f5c8ee5
GM
456int highlight_nonselected_windows;
457
458/* If cursor motion alone moves point off frame, try scrolling this
459 many lines up or down if that will bring it back. */
460
31ade731 461static EMACS_INT scroll_step;
a2889657 462
4b41cebb 463/* Nonzero means scroll just far enough to bring point back on the
5f5c8ee5
GM
464 screen, when appropriate. */
465
31ade731 466static EMACS_INT scroll_conservatively;
0789adb2 467
5f5c8ee5
GM
468/* Recenter the window whenever point gets within this many lines of
469 the top or bottom of the window. This value is translated into a
da8b7f4f
KS
470 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
471 that there is really a fixed pixel height scroll margin. */
5f5c8ee5 472
31ade731 473EMACS_INT scroll_margin;
9afd2168 474
5f5c8ee5
GM
475/* Number of windows showing the buffer of the selected window (or
476 another buffer with the same base buffer). keyboard.c refers to
477 this. */
a2889657 478
a2889657
JB
479int buffer_shared;
480
5f5c8ee5 481/* Vector containing glyphs for an ellipsis `...'. */
a2889657 482
5f5c8ee5 483static Lisp_Object default_invis_vector[3];
a2889657 484
1862a24e
MB
485/* Zero means display the mode-line/header-line/menu-bar in the default face
486 (this slightly odd definition is for compatibility with previous versions
487 of emacs), non-zero means display them using their respective faces.
488
489 This variable is deprecated. */
a2889657 490
a2889657
JB
491int mode_line_inverse_video;
492
5f5c8ee5
GM
493/* Prompt to display in front of the mini-buffer contents. */
494
8c5b6a0a 495Lisp_Object minibuf_prompt;
a2889657 496
5f5c8ee5
GM
497/* Width of current mini-buffer prompt. Only set after display_line
498 of the line that contains the prompt. */
499
a2889657 500int minibuf_prompt_width;
5f5c8ee5 501
5f5c8ee5
GM
502/* This is the window where the echo area message was displayed. It
503 is always a mini-buffer window, but it may not be the same window
504 currently active as a mini-buffer. */
505
73af359d
RS
506Lisp_Object echo_area_window;
507
c6e89d6c
GM
508/* List of pairs (MESSAGE . MULTIBYTE). The function save_message
509 pushes the current message and the value of
510 message_enable_multibyte on the stack, the function restore_message
511 pops the stack and displays MESSAGE again. */
512
513Lisp_Object Vmessage_stack;
514
a3788d53
RS
515/* Nonzero means multibyte characters were enabled when the echo area
516 message was specified. */
5f5c8ee5 517
a3788d53
RS
518int message_enable_multibyte;
519
4b41cebb 520/* Nonzero if we should redraw the mode lines on the next redisplay. */
5f5c8ee5 521
a2889657
JB
522int update_mode_lines;
523
5f5c8ee5 524/* Nonzero if window sizes or contents have changed since last
4b41cebb 525 redisplay that finished. */
5f5c8ee5 526
a2889657
JB
527int windows_or_buffers_changed;
528
5fb96e96
RS
529/* Nonzero means a frame's cursor type has been changed. */
530
531int cursor_type_changed;
532
5f5c8ee5
GM
533/* Nonzero after display_mode_line if %l was used and it displayed a
534 line number. */
535
aa6d10fa
RS
536int line_number_displayed;
537
538/* Maximum buffer size for which to display line numbers. */
5f5c8ee5 539
090703f4 540Lisp_Object Vline_number_display_limit;
5992c4f7 541
4b41cebb 542/* Line width to consider when repositioning for line number display. */
5d121aec 543
31ade731 544static EMACS_INT line_number_display_limit_width;
5d121aec 545
5f5c8ee5
GM
546/* Number of lines to keep in the message log buffer. t means
547 infinite. nil means don't log at all. */
548
5992c4f7 549Lisp_Object Vmessage_log_max;
d45de95b 550
6a94510a
GM
551/* The name of the *Messages* buffer, a string. */
552
553static Lisp_Object Vmessages_buffer_name;
554
c6e89d6c
GM
555/* Current, index 0, and last displayed echo area message. Either
556 buffers from echo_buffers, or nil to indicate no message. */
557
558Lisp_Object echo_area_buffer[2];
559
560/* The buffers referenced from echo_area_buffer. */
561
562static Lisp_Object echo_buffer[2];
563
564/* A vector saved used in with_area_buffer to reduce consing. */
565
566static Lisp_Object Vwith_echo_area_save_vector;
567
568/* Non-zero means display_echo_area should display the last echo area
569 message again. Set by redisplay_preserve_echo_area. */
570
571static int display_last_displayed_message_p;
572
573/* Nonzero if echo area is being used by print; zero if being used by
574 message. */
575
576int message_buf_print;
577
e1477f43
GM
578/* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
579
580Lisp_Object Qinhibit_menubar_update;
581int inhibit_menubar_update;
582
9142dd5b
GM
583/* Maximum height for resizing mini-windows. Either a float
584 specifying a fraction of the available height, or an integer
585 specifying a number of lines. */
c6e89d6c 586
ad4f174e
GM
587Lisp_Object Vmax_mini_window_height;
588
589/* Non-zero means messages should be displayed with truncated
590 lines instead of being continued. */
591
592int message_truncate_lines;
593Lisp_Object Qmessage_truncate_lines;
c6e89d6c 594
6e019995
GM
595/* Set to 1 in clear_message to make redisplay_internal aware
596 of an emptied echo area. */
597
598static int message_cleared_p;
599
d6d26ed3
GM
600/* Non-zero means we want a hollow cursor in windows that are not
601 selected. Zero means there's no cursor in such windows. */
602
cfe03a41 603Lisp_Object Vcursor_in_non_selected_windows;
af79bccb 604Lisp_Object Qcursor_in_non_selected_windows;
d6d26ed3 605
cfe03a41
KS
606/* How to blink the default frame cursor off. */
607Lisp_Object Vblink_cursor_alist;
608
5f5c8ee5
GM
609/* A scratch glyph row with contents used for generating truncation
610 glyphs. Also used in direct_output_for_insert. */
12adba34 611
5f5c8ee5
GM
612#define MAX_SCRATCH_GLYPHS 100
613struct glyph_row scratch_glyph_row;
614static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
1adc55de 615
5f5c8ee5
GM
616/* Ascent and height of the last line processed by move_it_to. */
617
618static int last_max_ascent, last_height;
619
21fdfb65
GM
620/* Non-zero if there's a help-echo in the echo area. */
621
622int help_echo_showing_p;
623
04612a64
GM
624/* If >= 0, computed, exact values of mode-line and header-line height
625 to use in the macros CURRENT_MODE_LINE_HEIGHT and
626 CURRENT_HEADER_LINE_HEIGHT. */
627
628int current_mode_line_height, current_header_line_height;
629
5f5c8ee5 630/* The maximum distance to look ahead for text properties. Values
2311178e 631 that are too small let us call compute_char_face and similar
5f5c8ee5
GM
632 functions too often which is expensive. Values that are too large
633 let us call compute_char_face and alike too often because we
634 might not be interested in text properties that far away. */
635
636#define TEXT_PROP_DISTANCE_LIMIT 100
637
47589c8c
GM
638#if GLYPH_DEBUG
639
76cb5e06
GM
640/* Variables to turn off display optimizations from Lisp. */
641
642int inhibit_try_window_id, inhibit_try_window_reusing;
643int inhibit_try_cursor_movement;
644
5f5c8ee5
GM
645/* Non-zero means print traces of redisplay if compiled with
646 GLYPH_DEBUG != 0. */
647
5f5c8ee5 648int trace_redisplay_p;
47589c8c 649
546a4f00 650#endif /* GLYPH_DEBUG */
47589c8c 651
546a4f00
AI
652#ifdef DEBUG_TRACE_MOVE
653/* Non-zero means trace with TRACE_MOVE to stderr. */
47589c8c
GM
654int trace_move;
655
47589c8c
GM
656#define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
657#else
91c3f500 658#define TRACE_MOVE(x) (void) 0
5f5c8ee5 659#endif
2311178e 660
d475bcb8
GM
661/* Non-zero means automatically scroll windows horizontally to make
662 point visible. */
663
664int automatic_hscrolling_p;
665
1df7e8f0
EZ
666/* How close to the margin can point get before the window is scrolled
667 horizontally. */
5d335845 668EMACS_INT hscroll_margin;
1df7e8f0
EZ
669
670/* How much to scroll horizontally when point is inside the above margin. */
e76d28d5 671Lisp_Object Vhscroll_step;
1df7e8f0 672
e00daaa0
GM
673/* A list of symbols, one for each supported image type. */
674
675Lisp_Object Vimage_types;
676
6422c1d7 677/* The variable `resize-mini-windows'. If nil, don't resize
67526daf 678 mini-windows. If t, always resize them to fit the text they
6422c1d7
GM
679 display. If `grow-only', let mini-windows grow only until they
680 become empty. */
681
682Lisp_Object Vresize_mini_windows;
683
82a7ab23
RS
684/* Buffer being redisplayed -- for redisplay_window_error. */
685
686struct buffer *displayed_buffer;
687
5f5c8ee5
GM
688/* Value returned from text property handlers (see below). */
689
690enum prop_handled
3c6595e0 691{
5f5c8ee5
GM
692 HANDLED_NORMALLY,
693 HANDLED_RECOMPUTE_PROPS,
694 HANDLED_OVERLAY_STRING_CONSUMED,
695 HANDLED_RETURN
696};
3c6595e0 697
5f5c8ee5
GM
698/* A description of text properties that redisplay is interested
699 in. */
3c6595e0 700
5f5c8ee5
GM
701struct props
702{
703 /* The name of the property. */
704 Lisp_Object *name;
90adcf20 705
5f5c8ee5
GM
706 /* A unique index for the property. */
707 enum prop_idx idx;
708
709 /* A handler function called to set up iterator IT from the property
710 at IT's current position. Value is used to steer handle_stop. */
711 enum prop_handled (*handler) P_ ((struct it *it));
712};
713
714static enum prop_handled handle_face_prop P_ ((struct it *));
715static enum prop_handled handle_invisible_prop P_ ((struct it *));
716static enum prop_handled handle_display_prop P_ ((struct it *));
260a86a0 717static enum prop_handled handle_composition_prop P_ ((struct it *));
5f5c8ee5
GM
718static enum prop_handled handle_overlay_change P_ ((struct it *));
719static enum prop_handled handle_fontified_prop P_ ((struct it *));
720
721/* Properties handled by iterators. */
722
723static struct props it_props[] =
5992c4f7 724{
5f5c8ee5
GM
725 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
726 /* Handle `face' before `display' because some sub-properties of
727 `display' need to know the face. */
728 {&Qface, FACE_PROP_IDX, handle_face_prop},
729 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
730 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
260a86a0 731 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
5f5c8ee5
GM
732 {NULL, 0, NULL}
733};
5992c4f7 734
5f5c8ee5
GM
735/* Value is the position described by X. If X is a marker, value is
736 the marker_position of X. Otherwise, value is X. */
12adba34 737
5f5c8ee5 738#define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
12adba34 739
5f5c8ee5 740/* Enumeration returned by some move_it_.* functions internally. */
12adba34 741
5f5c8ee5
GM
742enum move_it_result
743{
744 /* Not used. Undefined value. */
745 MOVE_UNDEFINED,
bab29e15 746
5f5c8ee5
GM
747 /* Move ended at the requested buffer position or ZV. */
748 MOVE_POS_MATCH_OR_ZV,
bab29e15 749
5f5c8ee5
GM
750 /* Move ended at the requested X pixel position. */
751 MOVE_X_REACHED,
12adba34 752
5f5c8ee5
GM
753 /* Move within a line ended at the end of a line that must be
754 continued. */
755 MOVE_LINE_CONTINUED,
2311178e 756
5f5c8ee5
GM
757 /* Move within a line ended at the end of a line that would
758 be displayed truncated. */
759 MOVE_LINE_TRUNCATED,
ff6c30e5 760
5f5c8ee5
GM
761 /* Move within a line ended at a line end. */
762 MOVE_NEWLINE_OR_CR
763};
12adba34 764
1987b083
RS
765/* This counter is used to clear the face cache every once in a while
766 in redisplay_internal. It is incremented for each redisplay.
767 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
768 cleared. */
769
770#define CLEAR_FACE_CACHE_COUNT 500
771static int clear_face_cache_count;
772
773/* Record the previous terminal frame we displayed. */
774
775static struct frame *previous_terminal_frame;
776
777/* Non-zero while redisplay_internal is in progress. */
778
779int redisplaying_p;
780
26683087
RS
781/* Non-zero means don't free realized faces. Bound while freeing
782 realized faces is dangerous because glyph matrices might still
783 reference them. */
1987b083 784
26683087
RS
785int inhibit_free_realized_faces;
786Lisp_Object Qinhibit_free_realized_faces;
ff6c30e5 787
fa3c6b4d
KS
788/* If a string, XTread_socket generates an event to display that string.
789 (The display is done in read_char.) */
790
791Lisp_Object help_echo_string;
792Lisp_Object help_echo_window;
793Lisp_Object help_echo_object;
794int help_echo_pos;
795
796/* Temporary variable for XTread_socket. */
797
798Lisp_Object previous_help_echo_string;
799
800
5f5c8ee5
GM
801\f
802/* Function prototypes. */
803
5a08cbaf 804static void setup_for_ellipsis P_ ((struct it *));
43c09969 805static void mark_window_display_accurate_1 P_ ((struct window *, int));
74bd6d65
GM
806static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
807static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
cafafe0b 808static int cursor_row_p P_ ((struct window *, struct glyph_row *));
715e84c9 809static int redisplay_mode_lines P_ ((Lisp_Object, int));
2e621225 810static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
c0a53abb
PJ
811
812#if 0
2e621225 813static int invisible_text_between_p P_ ((struct it *, int, int));
c0a53abb
PJ
814#endif
815
2e621225
GM
816static int next_element_from_ellipsis P_ ((struct it *));
817static void pint2str P_ ((char *, int, int));
525b8b09 818static void pint2hrstr P_ ((char *, int, int));
2e621225
GM
819static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
820 struct text_pos));
821static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
822static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
823static void store_frame_title_char P_ ((char));
50f80c2f 824static int store_frame_title P_ ((const unsigned char *, int, int));
2e621225
GM
825static void x_consider_frame_title P_ ((Lisp_Object));
826static void handle_stop P_ ((struct it *));
827static int tool_bar_lines_needed P_ ((struct frame *));
06568bbf 828static int single_display_prop_intangible_p P_ ((Lisp_Object));
5bcfeb49 829static void ensure_echo_area_buffers P_ ((void));
c6e89d6c
GM
830static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
831static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
23a96c77 832static int with_echo_area_buffer P_ ((struct window *, int,
23dd2d97
KR
833 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
834 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
c6e89d6c 835static void clear_garbaged_frames P_ ((void));
23dd2d97
KR
836static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
837static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
838static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
c6e89d6c 839static int display_echo_area P_ ((struct window *));
23dd2d97
KR
840static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
841static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
28514cd9 842static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
50f80c2f 843static int string_char_and_length P_ ((const unsigned char *, int, int *));
5f5c8ee5
GM
844static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
845 struct text_pos));
846static int compute_window_start_on_continuation_line P_ ((struct window *));
116d6f5c 847static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
5f5c8ee5 848static void insert_left_trunc_glyphs P_ ((struct it *));
351b5434
KS
849static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
850 Lisp_Object));
5f5c8ee5 851static void extend_face_to_end_of_line P_ ((struct it *));
80c6cb1f 852static int append_space P_ ((struct it *, int));
cb9d4a9f 853static int make_cursor_line_fully_visible P_ ((struct window *, int));
03b0a4b4 854static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
47589c8c 855static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
5f5c8ee5
GM
856static int trailing_whitespace_p P_ ((int));
857static int message_log_check_duplicate P_ ((int, int, int, int));
5f5c8ee5
GM
858static void push_it P_ ((struct it *));
859static void pop_it P_ ((struct it *));
860static void sync_frame_with_window_matrix_rows P_ ((struct window *));
dd429b03 861static void select_frame_for_redisplay P_ ((Lisp_Object));
5f5c8ee5 862static void redisplay_internal P_ ((int));
c6e89d6c 863static int echo_area_display P_ ((int));
5f5c8ee5
GM
864static void redisplay_windows P_ ((Lisp_Object));
865static void redisplay_window P_ ((Lisp_Object, int));
82a7ab23
RS
866static Lisp_Object redisplay_window_error ();
867static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
868static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
5f5c8ee5
GM
869static void update_menu_bar P_ ((struct frame *, int));
870static int try_window_reusing_current_matrix P_ ((struct window *));
871static int try_window_id P_ ((struct window *));
872static int display_line P_ ((struct it *));
715e84c9 873static int display_mode_lines P_ ((struct window *));
04612a64 874static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
c53a1624 875static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
fec8f23e 876static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
72f62cb5 877static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
5f5c8ee5
GM
878static void display_menu_bar P_ ((struct window *));
879static int display_count_lines P_ ((int, int, int, int, int *));
880static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
881 int, int, struct it *, int, int, int, int));
882static void compute_line_metrics P_ ((struct it *));
883static void run_redisplay_end_trigger_hook P_ ((struct it *));
5a08cbaf 884static int get_overlay_strings P_ ((struct it *, int));
5f5c8ee5 885static void next_overlay_string P_ ((struct it *));
5f5c8ee5
GM
886static void reseat P_ ((struct it *, struct text_pos, int));
887static void reseat_1 P_ ((struct it *, struct text_pos, int));
888static void back_to_previous_visible_line_start P_ ((struct it *));
889static void reseat_at_previous_visible_line_start P_ ((struct it *));
312246d1 890static void reseat_at_next_visible_line_start P_ ((struct it *, int));
5f5c8ee5
GM
891static int next_element_from_display_vector P_ ((struct it *));
892static int next_element_from_string P_ ((struct it *));
893static int next_element_from_c_string P_ ((struct it *));
894static int next_element_from_buffer P_ ((struct it *));
260a86a0 895static int next_element_from_composition P_ ((struct it *));
5f5c8ee5
GM
896static int next_element_from_image P_ ((struct it *));
897static int next_element_from_stretch P_ ((struct it *));
5a08cbaf 898static void load_overlay_strings P_ ((struct it *, int));
47d57b22
GM
899static int init_from_display_pos P_ ((struct it *, struct window *,
900 struct display_pos *));
5f5c8ee5
GM
901static void reseat_to_string P_ ((struct it *, unsigned char *,
902 Lisp_Object, int, int, int, int));
5f5c8ee5
GM
903static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
904 int, int, int));
905void move_it_vertically_backward P_ ((struct it *, int));
906static void init_to_row_start P_ ((struct it *, struct window *,
907 struct glyph_row *));
47d57b22
GM
908static int init_to_row_end P_ ((struct it *, struct window *,
909 struct glyph_row *));
5f5c8ee5 910static void back_to_previous_line_start P_ ((struct it *));
cafafe0b 911static int forward_to_next_line_start P_ ((struct it *, int *));
5f5c8ee5
GM
912static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
913 Lisp_Object, int));
914static struct text_pos string_pos P_ ((int, Lisp_Object));
915static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
916static int number_of_chars P_ ((unsigned char *, int));
917static void compute_stop_pos P_ ((struct it *));
918static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
919 Lisp_Object));
920static int face_before_or_after_it_pos P_ ((struct it *, int));
921static int next_overlay_change P_ ((int));
922static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
a61b7058
GM
923 Lisp_Object, struct text_pos *,
924 int));
06a12811 925static int underlying_face_id P_ ((struct it *));
4bde0ebb
GM
926static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
927 struct window *));
5f5c8ee5
GM
928
929#define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
930#define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
ff6c30e5 931
5f5c8ee5 932#ifdef HAVE_WINDOW_SYSTEM
12adba34 933
e037b9ec
GM
934static void update_tool_bar P_ ((struct frame *, int));
935static void build_desired_tool_bar_string P_ ((struct frame *f));
936static int redisplay_tool_bar P_ ((struct frame *));
937static void display_tool_bar_line P_ ((struct it *));
fa3c6b4d
KS
938static void notice_overwritten_cursor P_ ((struct window *,
939 enum glyph_row_area,
940 int, int, int, int));
941
942
12adba34 943
5f5c8ee5 944#endif /* HAVE_WINDOW_SYSTEM */
12adba34 945
5f5c8ee5
GM
946\f
947/***********************************************************************
948 Window display dimensions
949 ***********************************************************************/
12adba34 950
40a301b3
RS
951/* Return the bottom boundary y-position for text lines in window W.
952 This is the first y position at which a line cannot start.
953 It is relative to the top of the window.
954
955 This is the height of W minus the height of a mode line, if any. */
5f5c8ee5
GM
956
957INLINE int
958window_text_bottom_y (w)
959 struct window *w;
960{
da8b7f4f 961 int height = WINDOW_TOTAL_HEIGHT (w);
1a578e9b 962
5f5c8ee5
GM
963 if (WINDOW_WANTS_MODELINE_P (w))
964 height -= CURRENT_MODE_LINE_HEIGHT (w);
965 return height;
f88eb0b6
KH
966}
967
5f5c8ee5 968/* Return the pixel width of display area AREA of window W. AREA < 0
b46952ae 969 means return the total width of W, not including fringes to
5f5c8ee5 970 the left and right of the window. */
ff6c30e5 971
5f5c8ee5
GM
972INLINE int
973window_box_width (w, area)
974 struct window *w;
975 int area;
976{
da8b7f4f
KS
977 int cols = XFASTINT (w->total_cols);
978 int pixels = 0;
2311178e 979
5f5c8ee5 980 if (!w->pseudo_window_p)
ff6c30e5 981 {
da8b7f4f 982 cols -= WINDOW_SCROLL_BAR_COLS (w);
2311178e 983
5f5c8ee5
GM
984 if (area == TEXT_AREA)
985 {
da8b7f4f
KS
986 if (INTEGERP (w->left_margin_cols))
987 cols -= XFASTINT (w->left_margin_cols);
988 if (INTEGERP (w->right_margin_cols))
989 cols -= XFASTINT (w->right_margin_cols);
990 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
5f5c8ee5
GM
991 }
992 else if (area == LEFT_MARGIN_AREA)
da8b7f4f
KS
993 {
994 cols = (INTEGERP (w->left_margin_cols)
995 ? XFASTINT (w->left_margin_cols) : 0);
996 pixels = 0;
997 }
5f5c8ee5 998 else if (area == RIGHT_MARGIN_AREA)
da8b7f4f
KS
999 {
1000 cols = (INTEGERP (w->right_margin_cols)
1001 ? XFASTINT (w->right_margin_cols) : 0);
1002 pixels = 0;
1003 }
ff6c30e5 1004 }
5f5c8ee5 1005
da8b7f4f 1006 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
ff6c30e5 1007}
1adc55de 1008
1adc55de 1009
5f5c8ee5 1010/* Return the pixel height of the display area of window W, not
4b41cebb 1011 including mode lines of W, if any. */
f88eb0b6 1012
5f5c8ee5
GM
1013INLINE int
1014window_box_height (w)
1015 struct window *w;
f88eb0b6 1016{
5f5c8ee5 1017 struct frame *f = XFRAME (w->frame);
da8b7f4f 1018 int height = WINDOW_TOTAL_HEIGHT (w);
7ecd4937
GM
1019
1020 xassert (height >= 0);
2311178e 1021
d9c9e99c
MB
1022 /* Note: the code below that determines the mode-line/header-line
1023 height is essentially the same as that contained in the macro
1024 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1025 the appropriate glyph row has its `mode_line_p' flag set,
1026 and if it doesn't, uses estimate_mode_line_height instead. */
1027
5f5c8ee5 1028 if (WINDOW_WANTS_MODELINE_P (w))
97dff879
MB
1029 {
1030 struct glyph_row *ml_row
1031 = (w->current_matrix && w->current_matrix->rows
1032 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1033 : 0);
1034 if (ml_row && ml_row->mode_line_p)
1035 height -= ml_row->height;
1036 else
96d2320f 1037 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
97dff879 1038 }
5f5c8ee5 1039
045dee35 1040 if (WINDOW_WANTS_HEADER_LINE_P (w))
97dff879
MB
1041 {
1042 struct glyph_row *hl_row
1043 = (w->current_matrix && w->current_matrix->rows
1044 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1045 : 0);
1046 if (hl_row && hl_row->mode_line_p)
1047 height -= hl_row->height;
1048 else
1049 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1050 }
5f5c8ee5 1051
7c75be36
PJ
1052 /* With a very small font and a mode-line that's taller than
1053 default, we might end up with a negative height. */
1054 return max (0, height);
5992c4f7
KH
1055}
1056
da8b7f4f
KS
1057/* Return the window-relative coordinate of the left edge of display
1058 area AREA of window W. AREA < 0 means return the left edge of the
1059 whole window, to the right of the left fringe of W. */
1060
1061INLINE int
1062window_box_left_offset (w, area)
1063 struct window *w;
1064 int area;
1065{
1066 int x;
1067
1068 if (w->pseudo_window_p)
1069 return 0;
1070
1071 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1072
1073 if (area == TEXT_AREA)
1074 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1075 + window_box_width (w, LEFT_MARGIN_AREA));
1076 else if (area == RIGHT_MARGIN_AREA)
1077 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1078 + window_box_width (w, LEFT_MARGIN_AREA)
1079 + window_box_width (w, TEXT_AREA)
1080 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1081 ? 0
1082 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1083 else if (area == LEFT_MARGIN_AREA
1084 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1085 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1086
1087 return x;
1088}
1089
1090
1091/* Return the window-relative coordinate of the right edge of display
1092 area AREA of window W. AREA < 0 means return the left edge of the
1093 whole window, to the left of the right fringe of W. */
1094
1095INLINE int
1096window_box_right_offset (w, area)
1097 struct window *w;
1098 int area;
1099{
1100 return window_box_left_offset (w, area) + window_box_width (w, area);
1101}
5992c4f7 1102
5f5c8ee5
GM
1103/* Return the frame-relative coordinate of the left edge of display
1104 area AREA of window W. AREA < 0 means return the left edge of the
b46952ae 1105 whole window, to the right of the left fringe of W. */
5992c4f7 1106
5f5c8ee5
GM
1107INLINE int
1108window_box_left (w, area)
1109 struct window *w;
1110 int area;
90adcf20 1111{
5f5c8ee5 1112 struct frame *f = XFRAME (w->frame);
da8b7f4f 1113 int x;
a3788d53 1114
da8b7f4f
KS
1115 if (w->pseudo_window_p)
1116 return FRAME_INTERNAL_BORDER_WIDTH (f);
2311178e 1117
da8b7f4f
KS
1118 x = (WINDOW_LEFT_EDGE_X (w)
1119 + window_box_left_offset (w, area));
73af359d 1120
5f5c8ee5 1121 return x;
2311178e 1122}
90adcf20 1123
b6436d4e 1124
5f5c8ee5
GM
1125/* Return the frame-relative coordinate of the right edge of display
1126 area AREA of window W. AREA < 0 means return the left edge of the
b46952ae 1127 whole window, to the left of the right fringe of W. */
ded34426 1128
5f5c8ee5
GM
1129INLINE int
1130window_box_right (w, area)
1131 struct window *w;
1132 int area;
1133{
1134 return window_box_left (w, area) + window_box_width (w, area);
2311178e
TTN
1135}
1136
5f5c8ee5
GM
1137/* Get the bounding box of the display area AREA of window W, without
1138 mode lines, in frame-relative coordinates. AREA < 0 means the
b46952ae 1139 whole window, not including the left and right fringes of
5f5c8ee5
GM
1140 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1141 coordinates of the upper-left corner of the box. Return in
1142 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1143
1144INLINE void
1145window_box (w, area, box_x, box_y, box_width, box_height)
1146 struct window *w;
1147 int area;
1148 int *box_x, *box_y, *box_width, *box_height;
1149{
da8b7f4f
KS
1150 if (box_width)
1151 *box_width = window_box_width (w, area);
1152 if (box_height)
1153 *box_height = window_box_height (w);
1154 if (box_x)
1155 *box_x = window_box_left (w, area);
1156 if (box_y)
1157 {
1158 *box_y = WINDOW_TOP_EDGE_Y (w);
1159 if (WINDOW_WANTS_HEADER_LINE_P (w))
1160 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1161 }
ded34426 1162}
1adc55de 1163
1adc55de 1164
5f5c8ee5 1165/* Get the bounding box of the display area AREA of window W, without
b46952ae
KS
1166 mode lines. AREA < 0 means the whole window, not including the
1167 left and right fringe of the window. Return in *TOP_LEFT_X
5f5c8ee5
GM
1168 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1169 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1170 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1171 box. */
ded34426 1172
5f5c8ee5
GM
1173INLINE void
1174window_box_edges (w, area, top_left_x, top_left_y,
1175 bottom_right_x, bottom_right_y)
1176 struct window *w;
1177 int area;
1178 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
48ae5f0a 1179{
5f5c8ee5
GM
1180 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1181 bottom_right_y);
1182 *bottom_right_x += *top_left_x;
1183 *bottom_right_y += *top_left_y;
48ae5f0a
KH
1184}
1185
5f5c8ee5
GM
1186
1187\f
1188/***********************************************************************
1189 Utilities
1190 ***********************************************************************/
1191
8b6ea97f
GM
1192/* Return the bottom y-position of the line the iterator IT is in.
1193 This can modify IT's settings. */
1194
1195int
1196line_bottom_y (it)
1197 struct it *it;
1198{
1199 int line_height = it->max_ascent + it->max_descent;
1200 int line_top_y = it->current_y;
2311178e 1201
8b6ea97f
GM
1202 if (line_height == 0)
1203 {
1204 if (last_height)
1205 line_height = last_height;
1206 else if (IT_CHARPOS (*it) < ZV)
1207 {
1208 move_it_by_lines (it, 1, 1);
1209 line_height = (it->max_ascent || it->max_descent
1210 ? it->max_ascent + it->max_descent
1211 : last_height);
1212 }
1213 else
1214 {
1215 struct glyph_row *row = it->glyph_row;
2311178e 1216
8b6ea97f
GM
1217 /* Use the default character height. */
1218 it->glyph_row = NULL;
1219 it->what = IT_CHARACTER;
1220 it->c = ' ';
1221 it->len = 1;
1222 PRODUCE_GLYPHS (it);
1223 line_height = it->ascent + it->descent;
1224 it->glyph_row = row;
1225 }
1226 }
1227
1228 return line_top_y + line_height;
1229}
1230
1231
0db95684
GM
1232/* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1233 1 if POS is visible and the line containing POS is fully visible.
1234 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1235 and header-lines heights. */
3a641a69
GM
1236
1237int
04612a64 1238pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
3a641a69 1239 struct window *w;
04612a64 1240 int charpos, *fully, exact_mode_line_heights_p;
3a641a69
GM
1241{
1242 struct it it;
1243 struct text_pos top;
fcab1954
GM
1244 int visible_p;
1245 struct buffer *old_buffer = NULL;
1246
1247 if (XBUFFER (w->buffer) != current_buffer)
1248 {
1249 old_buffer = current_buffer;
1250 set_buffer_internal_1 (XBUFFER (w->buffer));
1251 }
3a641a69
GM
1252
1253 *fully = visible_p = 0;
1254 SET_TEXT_POS_FROM_MARKER (top, w->start);
2311178e 1255
04612a64
GM
1256 /* Compute exact mode line heights, if requested. */
1257 if (exact_mode_line_heights_p)
1258 {
1259 if (WINDOW_WANTS_MODELINE_P (w))
1260 current_mode_line_height
96d2320f 1261 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
04612a64 1262 current_buffer->mode_line_format);
2311178e 1263
04612a64
GM
1264 if (WINDOW_WANTS_HEADER_LINE_P (w))
1265 current_header_line_height
1266 = display_mode_line (w, HEADER_LINE_FACE_ID,
1267 current_buffer->header_line_format);
1268 }
3a641a69 1269
04612a64 1270 start_display (&it, w, top);
3a641a69
GM
1271 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1272 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
a13be207
GM
1273
1274 /* Note that we may overshoot because of invisible text. */
1275 if (IT_CHARPOS (it) >= charpos)
3a641a69 1276 {
8b6ea97f
GM
1277 int top_y = it.current_y;
1278 int bottom_y = line_bottom_y (&it);
da8b7f4f 1279 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
2311178e 1280
8b6ea97f
GM
1281 if (top_y < window_top_y)
1282 visible_p = bottom_y > window_top_y;
1283 else if (top_y < it.last_visible_y)
fcab1954
GM
1284 {
1285 visible_p = 1;
8b6ea97f 1286 *fully = bottom_y <= it.last_visible_y;
fcab1954 1287 }
3a641a69
GM
1288 }
1289 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1290 {
1291 move_it_by_lines (&it, 1, 0);
1292 if (charpos < IT_CHARPOS (it))
1293 {
1294 visible_p = 1;
1295 *fully = 0;
1296 }
1297 }
fcab1954
GM
1298
1299 if (old_buffer)
1300 set_buffer_internal_1 (old_buffer);
04612a64
GM
1301
1302 current_header_line_height = current_mode_line_height = -1;
3a641a69
GM
1303 return visible_p;
1304}
1305
1306
4fdb80f2
GM
1307/* Return the next character from STR which is MAXLEN bytes long.
1308 Return in *LEN the length of the character. This is like
1309 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
9ab8560d 1310 we find one, we return a `?', but with the length of the invalid
4fdb80f2
GM
1311 character. */
1312
1313static INLINE int
7a5b8a93 1314string_char_and_length (str, maxlen, len)
50f80c2f 1315 const unsigned char *str;
7a5b8a93 1316 int maxlen, *len;
4fdb80f2
GM
1317{
1318 int c;
1319
1320 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1321 if (!CHAR_VALID_P (c, 1))
1322 /* We may not change the length here because other places in Emacs
9ab8560d 1323 don't use this function, i.e. they silently accept invalid
4fdb80f2
GM
1324 characters. */
1325 c = '?';
1326
1327 return c;
1328}
1329
1330
1331
5f5c8ee5
GM
1332/* Given a position POS containing a valid character and byte position
1333 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1334
1335static struct text_pos
1336string_pos_nchars_ahead (pos, string, nchars)
1337 struct text_pos pos;
1338 Lisp_Object string;
1339 int nchars;
0b1005ef 1340{
5f5c8ee5
GM
1341 xassert (STRINGP (string) && nchars >= 0);
1342
1343 if (STRING_MULTIBYTE (string))
1344 {
2051c264 1345 int rest = SBYTES (string) - BYTEPOS (pos);
50f80c2f 1346 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
5f5c8ee5
GM
1347 int len;
1348
1349 while (nchars--)
1350 {
4fdb80f2 1351 string_char_and_length (p, rest, &len);
5f5c8ee5
GM
1352 p += len, rest -= len;
1353 xassert (rest >= 0);
1354 CHARPOS (pos) += 1;
1355 BYTEPOS (pos) += len;
1356 }
1357 }
1358 else
1359 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1360
1361 return pos;
0a9dc68b
RS
1362}
1363
0a9dc68b 1364
5f5c8ee5
GM
1365/* Value is the text position, i.e. character and byte position,
1366 for character position CHARPOS in STRING. */
1367
1368static INLINE struct text_pos
1369string_pos (charpos, string)
1370 int charpos;
0a9dc68b 1371 Lisp_Object string;
0a9dc68b 1372{
5f5c8ee5
GM
1373 struct text_pos pos;
1374 xassert (STRINGP (string));
1375 xassert (charpos >= 0);
1376 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1377 return pos;
1378}
1379
1380
1381/* Value is a text position, i.e. character and byte position, for
1382 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1383 means recognize multibyte characters. */
1384
1385static struct text_pos
1386c_string_pos (charpos, s, multibyte_p)
1387 int charpos;
1388 unsigned char *s;
1389 int multibyte_p;
1390{
1391 struct text_pos pos;
1392
1393 xassert (s != NULL);
1394 xassert (charpos >= 0);
1395
1396 if (multibyte_p)
0a9dc68b 1397 {
5f5c8ee5
GM
1398 int rest = strlen (s), len;
1399
1400 SET_TEXT_POS (pos, 0, 0);
1401 while (charpos--)
0a9dc68b 1402 {
4fdb80f2 1403 string_char_and_length (s, rest, &len);
5f5c8ee5
GM
1404 s += len, rest -= len;
1405 xassert (rest >= 0);
1406 CHARPOS (pos) += 1;
1407 BYTEPOS (pos) += len;
0a9dc68b
RS
1408 }
1409 }
5f5c8ee5
GM
1410 else
1411 SET_TEXT_POS (pos, charpos, charpos);
0a9dc68b 1412
5f5c8ee5
GM
1413 return pos;
1414}
0a9dc68b 1415
0a9dc68b 1416
5f5c8ee5
GM
1417/* Value is the number of characters in C string S. MULTIBYTE_P
1418 non-zero means recognize multibyte characters. */
0a9dc68b 1419
5f5c8ee5
GM
1420static int
1421number_of_chars (s, multibyte_p)
1422 unsigned char *s;
1423 int multibyte_p;
1424{
1425 int nchars;
2311178e 1426
5f5c8ee5
GM
1427 if (multibyte_p)
1428 {
1429 int rest = strlen (s), len;
1430 unsigned char *p = (unsigned char *) s;
0a9dc68b 1431
5f5c8ee5
GM
1432 for (nchars = 0; rest > 0; ++nchars)
1433 {
4fdb80f2 1434 string_char_and_length (p, rest, &len);
5f5c8ee5 1435 rest -= len, p += len;
0a9dc68b
RS
1436 }
1437 }
5f5c8ee5
GM
1438 else
1439 nchars = strlen (s);
1440
1441 return nchars;
0b1005ef
KH
1442}
1443
2311178e 1444
5f5c8ee5
GM
1445/* Compute byte position NEWPOS->bytepos corresponding to
1446 NEWPOS->charpos. POS is a known position in string STRING.
1447 NEWPOS->charpos must be >= POS.charpos. */
76412d64 1448
5f5c8ee5
GM
1449static void
1450compute_string_pos (newpos, pos, string)
1451 struct text_pos *newpos, pos;
1452 Lisp_Object string;
76412d64 1453{
5f5c8ee5
GM
1454 xassert (STRINGP (string));
1455 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
2311178e 1456
5f5c8ee5 1457 if (STRING_MULTIBYTE (string))
6fc556fd
KR
1458 *newpos = string_pos_nchars_ahead (pos, string,
1459 CHARPOS (*newpos) - CHARPOS (pos));
5f5c8ee5
GM
1460 else
1461 BYTEPOS (*newpos) = CHARPOS (*newpos);
76412d64
RS
1462}
1463
fa3c6b4d
KS
1464/* EXPORT:
1465 Return an estimation of the pixel height of mode or top lines on
1466 frame F. FACE_ID specifies what line's height to estimate. */
1467
1468int
1469estimate_mode_line_height (f, face_id)
1470 struct frame *f;
1471 enum face_id face_id;
1472{
1473#ifdef HAVE_WINDOW_SYSTEM
1474 if (FRAME_WINDOW_P (f))
1475 {
1476 int height = FONT_HEIGHT (FRAME_FONT (f));
1477
1478 /* This function is called so early when Emacs starts that the face
1479 cache and mode line face are not yet initialized. */
1480 if (FRAME_FACE_CACHE (f))
1481 {
1482 struct face *face = FACE_FROM_ID (f, face_id);
1483 if (face)
1484 {
1485 if (face->font)
1486 height = FONT_HEIGHT (face->font);
1487 if (face->box_line_width > 0)
1488 height += 2 * face->box_line_width;
1489 }
1490 }
1491
1492 return height;
1493 }
1494#endif
1495
1496 return 1;
1497}
1498
e080d3eb
KS
1499/* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1500 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1501 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1502 not force the value into range. */
1503
1504void
1505pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1506 FRAME_PTR f;
1507 register int pix_x, pix_y;
1508 int *x, *y;
1509 NativeRectangle *bounds;
1510 int noclip;
1511{
1512
1513#ifdef HAVE_WINDOW_SYSTEM
1514 if (FRAME_WINDOW_P (f))
1515 {
da8b7f4f 1516 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
e080d3eb
KS
1517 even for negative values. */
1518 if (pix_x < 0)
da8b7f4f 1519 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
e080d3eb 1520 if (pix_y < 0)
da8b7f4f 1521 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
e080d3eb 1522
da8b7f4f
KS
1523 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1524 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
e080d3eb
KS
1525
1526 if (bounds)
1527 STORE_NATIVE_RECT (*bounds,
da8b7f4f
KS
1528 FRAME_COL_TO_PIXEL_X (f, pix_x),
1529 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1530 FRAME_COLUMN_WIDTH (f) - 1,
1531 FRAME_LINE_HEIGHT (f) - 1);
e080d3eb
KS
1532
1533 if (!noclip)
1534 {
1535 if (pix_x < 0)
1536 pix_x = 0;
da8b7f4f
KS
1537 else if (pix_x > FRAME_TOTAL_COLS (f))
1538 pix_x = FRAME_TOTAL_COLS (f);
e080d3eb
KS
1539
1540 if (pix_y < 0)
1541 pix_y = 0;
da8b7f4f
KS
1542 else if (pix_y > FRAME_LINES (f))
1543 pix_y = FRAME_LINES (f);
e080d3eb
KS
1544 }
1545 }
1546#endif
1547
1548 *x = pix_x;
1549 *y = pix_y;
1550}
1551
1552
1553/* Given HPOS/VPOS in the current matrix of W, return corresponding
1554 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1555 can't tell the positions because W's display is not up to date,
1556 return 0. */
1557
1558int
1559glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1560 struct window *w;
1561 int hpos, vpos;
1562 int *frame_x, *frame_y;
1563{
1564#ifdef HAVE_WINDOW_SYSTEM
1565 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1566 {
1567 int success_p;
1568
1569 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1570 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1571
1572 if (display_completed)
1573 {
1574 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1575 struct glyph *glyph = row->glyphs[TEXT_AREA];
1576 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1577
1578 hpos = row->x;
1579 vpos = row->y;
1580 while (glyph < end)
1581 {
1582 hpos += glyph->pixel_width;
1583 ++glyph;
1584 }
1585
f65536fa
KS
1586 /* If first glyph is partially visible, its first visible position is still 0. */
1587 if (hpos < 0)
1588 hpos = 0;
1589
e080d3eb
KS
1590 success_p = 1;
1591 }
1592 else
1593 {
1594 hpos = vpos = 0;
1595 success_p = 0;
1596 }
1597
1598 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1599 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1600 return success_p;
1601 }
1602#endif
1603
1604 *frame_x = hpos;
1605 *frame_y = vpos;
1606 return 1;
1607}
1608
1609
fa3c6b4d
KS
1610#ifdef HAVE_WINDOW_SYSTEM
1611
1612/* Find the glyph under window-relative coordinates X/Y in window W.
1613 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1614 strings. Return in *HPOS and *VPOS the row and column number of
1615 the glyph found. Return in *AREA the glyph area containing X.
1616 Value is a pointer to the glyph found or null if X/Y is not on
1617 text, or we can't tell because W's current matrix is not up to
1618 date. */
1619
1620static struct glyph *
493fdc3c 1621x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
fa3c6b4d
KS
1622 struct window *w;
1623 int x, y;
493fdc3c 1624 int *hpos, *vpos, *dx, *dy, *area;
fa3c6b4d
KS
1625{
1626 struct glyph *glyph, *end;
1627 struct glyph_row *row = NULL;
da8b7f4f 1628 int x0, i;
fa3c6b4d
KS
1629
1630 /* Find row containing Y. Give up if some row is not enabled. */
1631 for (i = 0; i < w->current_matrix->nrows; ++i)
1632 {
1633 row = MATRIX_ROW (w->current_matrix, i);
1634 if (!row->enabled_p)
1635 return NULL;
1636 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1637 break;
1638 }
1639
1640 *vpos = i;
1641 *hpos = 0;
1642
1643 /* Give up if Y is not in the window. */
1644 if (i == w->current_matrix->nrows)
1645 return NULL;
1646
1647 /* Get the glyph area containing X. */
1648 if (w->pseudo_window_p)
1649 {
1650 *area = TEXT_AREA;
1651 x0 = 0;
1652 }
1653 else
1654 {
da8b7f4f 1655 if (x < window_box_left_offset (w, TEXT_AREA))
fa3c6b4d
KS
1656 {
1657 *area = LEFT_MARGIN_AREA;
da8b7f4f 1658 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
fa3c6b4d 1659 }
da8b7f4f 1660 else if (x < window_box_right_offset (w, TEXT_AREA))
fa3c6b4d
KS
1661 {
1662 *area = TEXT_AREA;
f65536fa 1663 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
fa3c6b4d
KS
1664 }
1665 else
1666 {
1667 *area = RIGHT_MARGIN_AREA;
da8b7f4f 1668 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
fa3c6b4d
KS
1669 }
1670 }
1671
1672 /* Find glyph containing X. */
1673 glyph = row->glyphs[*area];
1674 end = glyph + row->used[*area];
493fdc3c
KS
1675 x -= x0;
1676 while (glyph < end && x >= glyph->pixel_width)
fa3c6b4d 1677 {
493fdc3c 1678 x -= glyph->pixel_width;
fa3c6b4d
KS
1679 ++glyph;
1680 }
1681
1682 if (glyph == end)
1683 return NULL;
1684
493fdc3c
KS
1685 if (dx)
1686 {
1687 *dx = x;
1688 *dy = y - (row->y + row->ascent - glyph->ascent);
1689 }
1690
fa3c6b4d
KS
1691 *hpos = glyph - row->glyphs[*area];
1692 return glyph;
1693}
1694
1695
1696/* EXPORT:
1697 Convert frame-relative x/y to coordinates relative to window W.
1698 Takes pseudo-windows into account. */
1699
1700void
1701frame_to_window_pixel_xy (w, x, y)
1702 struct window *w;
1703 int *x, *y;
1704{
1705 if (w->pseudo_window_p)
1706 {
1707 /* A pseudo-window is always full-width, and starts at the
1708 left edge of the frame, plus a frame border. */
1709 struct frame *f = XFRAME (w->frame);
da8b7f4f 1710 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
fa3c6b4d
KS
1711 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1712 }
1713 else
1714 {
da8b7f4f 1715 *x -= WINDOW_LEFT_EDGE_X (w);
fa3c6b4d
KS
1716 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1717 }
1718}
1719
1720/* EXPORT:
1721 Return in *R the clipping rectangle for glyph string S. */
1722
1723void
1724get_glyph_string_clip_rect (s, nr)
1725 struct glyph_string *s;
1726 NativeRectangle *nr;
1727{
1728 XRectangle r;
1729
1730 if (s->row->full_width_p)
1731 {
da8b7f4f
KS
1732 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1733 r.x = WINDOW_LEFT_EDGE_X (s->w);
1734 r.width = WINDOW_TOTAL_WIDTH (s->w);
fa3c6b4d
KS
1735
1736 /* Unless displaying a mode or menu bar line, which are always
1737 fully visible, clip to the visible part of the row. */
1738 if (s->w->pseudo_window_p)
1739 r.height = s->row->visible_height;
1740 else
1741 r.height = s->height;
1742 }
1743 else
1744 {
1745 /* This is a text line that may be partially visible. */
da8b7f4f 1746 r.x = window_box_left (s->w, s->area);
fa3c6b4d
KS
1747 r.width = window_box_width (s->w, s->area);
1748 r.height = s->row->visible_height;
1749 }
1750
1751 /* If S draws overlapping rows, it's sufficient to use the top and
1752 bottom of the window for clipping because this glyph string
1753 intentionally draws over other lines. */
1754 if (s->for_overlaps_p)
1755 {
da8b7f4f 1756 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
fa3c6b4d
KS
1757 r.height = window_text_bottom_y (s->w) - r.y;
1758 }
1759 else
1760 {
1761 /* Don't use S->y for clipping because it doesn't take partially
1762 visible lines into account. For example, it can be negative for
1763 partially visible lines at the top of a window. */
1764 if (!s->row->full_width_p
1765 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
da8b7f4f 1766 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
fa3c6b4d
KS
1767 else
1768 r.y = max (0, s->row->y);
1769
1770 /* If drawing a tool-bar window, draw it over the internal border
1771 at the top of the window. */
1772 if (s->w == XWINDOW (s->f->tool_bar_window))
b4ebbb12 1773 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
fa3c6b4d
KS
1774 }
1775
1776 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1777
fa3c6b4d
KS
1778 /* If drawing the cursor, don't let glyph draw outside its
1779 advertised boundaries. Cleartype does this under some circumstances. */
1780 if (s->hl == DRAW_CURSOR)
1781 {
493fdc3c
KS
1782 struct glyph *glyph = s->first_glyph;
1783 int height;
1784
fa3c6b4d
KS
1785 if (s->x > r.x)
1786 {
1787 r.width -= s->x - r.x;
1788 r.x = s->x;
1789 }
493fdc3c
KS
1790 r.width = min (r.width, glyph->pixel_width);
1791
1792 /* Don't draw cursor glyph taller than our actual glyph. */
1793 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1794 if (height < r.height)
1795 {
cb9d4a9f
KS
1796 int max_y = r.y + r.height;
1797 r.y = min (max_y, s->ybase + glyph->descent - height);
1798 r.height = min (max_y - r.y, height);
493fdc3c 1799 }
fa3c6b4d 1800 }
fa3c6b4d
KS
1801
1802#ifdef CONVERT_FROM_XRECT
1803 CONVERT_FROM_XRECT (r, *nr);
1804#else
1805 *nr = r;
fd4c9408 1806#endif
fa3c6b4d
KS
1807}
1808
1809#endif /* HAVE_WINDOW_SYSTEM */
9c74a0dd 1810
5f5c8ee5
GM
1811\f
1812/***********************************************************************
1813 Lisp form evaluation
1814 ***********************************************************************/
1815
116d6f5c 1816/* Error handler for safe_eval and safe_call. */
5f5c8ee5
GM
1817
1818static Lisp_Object
116d6f5c 1819safe_eval_handler (arg)
5f5c8ee5
GM
1820 Lisp_Object arg;
1821{
0dbf9fd2 1822 add_to_log ("Error during redisplay: %s", arg, Qnil);
5f5c8ee5
GM
1823 return Qnil;
1824}
1825
1826
1827/* Evaluate SEXPR and return the result, or nil if something went
2913a9c0 1828 wrong. Prevent redisplay during the evaluation. */
5f5c8ee5 1829
71e5b1b8 1830Lisp_Object
116d6f5c 1831safe_eval (sexpr)
5f5c8ee5
GM
1832 Lisp_Object sexpr;
1833{
5f5c8ee5 1834 Lisp_Object val;
2311178e 1835
30a3f61c
GM
1836 if (inhibit_eval_during_redisplay)
1837 val = Qnil;
1838 else
1839 {
331379bf 1840 int count = SPECPDL_INDEX ();
30a3f61c 1841 struct gcpro gcpro1;
0d8b31c0 1842
30a3f61c
GM
1843 GCPRO1 (sexpr);
1844 specbind (Qinhibit_redisplay, Qt);
7033d6df
RS
1845 /* Use Qt to ensure debugger does not run,
1846 so there is no possibility of wanting to redisplay. */
1847 val = internal_condition_case_1 (Feval, sexpr, Qt,
30a3f61c
GM
1848 safe_eval_handler);
1849 UNGCPRO;
1850 val = unbind_to (count, val);
1851 }
2311178e 1852
30a3f61c 1853 return val;
0d8b31c0
GM
1854}
1855
1856
1857/* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2913a9c0
GM
1858 Return the result, or nil if something went wrong. Prevent
1859 redisplay during the evaluation. */
0d8b31c0
GM
1860
1861Lisp_Object
116d6f5c 1862safe_call (nargs, args)
0d8b31c0
GM
1863 int nargs;
1864 Lisp_Object *args;
1865{
0d8b31c0 1866 Lisp_Object val;
2311178e 1867
30a3f61c
GM
1868 if (inhibit_eval_during_redisplay)
1869 val = Qnil;
1870 else
1871 {
331379bf 1872 int count = SPECPDL_INDEX ();
30a3f61c 1873 struct gcpro gcpro1;
0d8b31c0 1874
30a3f61c
GM
1875 GCPRO1 (args[0]);
1876 gcpro1.nvars = nargs;
1877 specbind (Qinhibit_redisplay, Qt);
7033d6df
RS
1878 /* Use Qt to ensure debugger does not run,
1879 so there is no possibility of wanting to redisplay. */
1880 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
30a3f61c
GM
1881 safe_eval_handler);
1882 UNGCPRO;
1883 val = unbind_to (count, val);
1884 }
1885
1886 return val;
5f5c8ee5
GM
1887}
1888
1889
116d6f5c
GM
1890/* Call function FN with one argument ARG.
1891 Return the result, or nil if something went wrong. */
1892
1893Lisp_Object
1894safe_call1 (fn, arg)
1895 Lisp_Object fn, arg;
1896{
1897 Lisp_Object args[2];
1898 args[0] = fn;
1899 args[1] = arg;
1900 return safe_call (2, args);
1901}
1902
1903
5f5c8ee5
GM
1904\f
1905/***********************************************************************
1906 Debugging
1907 ***********************************************************************/
1908
1909#if 0
1910
1911/* Define CHECK_IT to perform sanity checks on iterators.
1912 This is for debugging. It is too slow to do unconditionally. */
1913
1914static void
1915check_it (it)
1916 struct it *it;
1917{
1918 if (it->method == next_element_from_string)
a2889657 1919 {
5f5c8ee5
GM
1920 xassert (STRINGP (it->string));
1921 xassert (IT_STRING_CHARPOS (*it) >= 0);
1922 }
7d8a0b55 1923 else
5f5c8ee5 1924 {
7d8a0b55
MB
1925 xassert (IT_STRING_CHARPOS (*it) < 0);
1926 if (it->method == next_element_from_buffer)
1927 {
1928 /* Check that character and byte positions agree. */
1929 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1930 }
5f5c8ee5 1931 }
73af359d 1932
5f5c8ee5
GM
1933 if (it->dpvec)
1934 xassert (it->current.dpvec_index >= 0);
1935 else
1936 xassert (it->current.dpvec_index < 0);
1937}
1f40cad2 1938
5f5c8ee5
GM
1939#define CHECK_IT(IT) check_it ((IT))
1940
1941#else /* not 0 */
1942
1943#define CHECK_IT(IT) (void) 0
1944
1945#endif /* not 0 */
1946
1947
1948#if GLYPH_DEBUG
1949
1950/* Check that the window end of window W is what we expect it
1951 to be---the last row in the current matrix displaying text. */
1952
1953static void
1954check_window_end (w)
1955 struct window *w;
1956{
1957 if (!MINI_WINDOW_P (w)
1958 && !NILP (w->window_end_valid))
1959 {
1960 struct glyph_row *row;
1961 xassert ((row = MATRIX_ROW (w->current_matrix,
1962 XFASTINT (w->window_end_vpos)),
1963 !row->enabled_p
1964 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1965 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1966 }
1967}
1968
1969#define CHECK_WINDOW_END(W) check_window_end ((W))
1970
1971#else /* not GLYPH_DEBUG */
1972
1973#define CHECK_WINDOW_END(W) (void) 0
1974
1975#endif /* not GLYPH_DEBUG */
1976
1977
1978\f
1979/***********************************************************************
1980 Iterator initialization
1981 ***********************************************************************/
1982
1983/* Initialize IT for displaying current_buffer in window W, starting
1984 at character position CHARPOS. CHARPOS < 0 means that no buffer
1985 position is specified which is useful when the iterator is assigned
1986 a position later. BYTEPOS is the byte position corresponding to
3ebf0ea9 1987 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
5f5c8ee5
GM
1988
1989 If ROW is not null, calls to produce_glyphs with IT as parameter
1990 will produce glyphs in that row.
1991
1992 BASE_FACE_ID is the id of a base face to use. It must be one of
96d2320f
KS
1993 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1994 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1995 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2311178e 1996
96d2320f
KS
1997 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1998 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1999 will be initialized to use the corresponding mode line glyph row of
2000 the desired matrix of W. */
5f5c8ee5
GM
2001
2002void
2003init_iterator (it, w, charpos, bytepos, row, base_face_id)
2004 struct it *it;
2005 struct window *w;
2006 int charpos, bytepos;
2007 struct glyph_row *row;
2008 enum face_id base_face_id;
2009{
2010 int highlight_region_p;
5f5c8ee5
GM
2011
2012 /* Some precondition checks. */
2013 xassert (w != NULL && it != NULL);
3b6b6db7
SM
2014 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2015 && charpos <= ZV));
5f5c8ee5
GM
2016
2017 /* If face attributes have been changed since the last redisplay,
2018 free realized faces now because they depend on face definitions
7d0393cf 2019 that might have changed. Don't free faces while there might be
1987b083 2020 desired matrices pending which reference these faces. */
26683087 2021 if (face_change_count && !inhibit_free_realized_faces)
5f5c8ee5
GM
2022 {
2023 face_change_count = 0;
2024 free_all_realized_faces (Qnil);
2025 }
2026
2027 /* Use one of the mode line rows of W's desired matrix if
2028 appropriate. */
2029 if (row == NULL)
2030 {
96d2320f
KS
2031 if (base_face_id == MODE_LINE_FACE_ID
2032 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
5f5c8ee5 2033 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
045dee35
GM
2034 else if (base_face_id == HEADER_LINE_FACE_ID)
2035 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
5f5c8ee5 2036 }
2311178e 2037
5f5c8ee5
GM
2038 /* Clear IT. */
2039 bzero (it, sizeof *it);
2040 it->current.overlay_string_index = -1;
2041 it->current.dpvec_index = -1;
5f5c8ee5 2042 it->base_face_id = base_face_id;
7d8a0b55
MB
2043 it->string = Qnil;
2044 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5f5c8ee5
GM
2045
2046 /* The window in which we iterate over current_buffer: */
2047 XSETWINDOW (it->window, w);
2048 it->w = w;
2049 it->f = XFRAME (w->frame);
2050
d475bcb8
GM
2051 /* Extra space between lines (on window systems only). */
2052 if (base_face_id == DEFAULT_FACE_ID
2053 && FRAME_WINDOW_P (it->f))
2054 {
2055 if (NATNUMP (current_buffer->extra_line_spacing))
2056 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2057 else if (it->f->extra_line_spacing > 0)
2058 it->extra_line_spacing = it->f->extra_line_spacing;
2059 }
2060
5f5c8ee5 2061 /* If realized faces have been removed, e.g. because of face
62be9979
GM
2062 attribute changes of named faces, recompute them. When running
2063 in batch mode, the face cache of Vterminal_frame is null. If
2064 we happen to get called, make a dummy face cache. */
a24d4cb2 2065 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
62be9979 2066 init_frame_faces (it->f);
5f5c8ee5
GM
2067 if (FRAME_FACE_CACHE (it->f)->used == 0)
2068 recompute_basic_faces (it->f);
2069
5f5c8ee5
GM
2070 /* Current value of the `space-width', and 'height' properties. */
2071 it->space_width = Qnil;
2072 it->font_height = Qnil;
2311178e 2073
5f5c8ee5
GM
2074 /* Are control characters displayed as `^C'? */
2075 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2076
2077 /* -1 means everything between a CR and the following line end
2078 is invisible. >0 means lines indented more than this value are
2079 invisible. */
2080 it->selective = (INTEGERP (current_buffer->selective_display)
2081 ? XFASTINT (current_buffer->selective_display)
2311178e 2082 : (!NILP (current_buffer->selective_display)
5f5c8ee5
GM
2083 ? -1 : 0));
2084 it->selective_display_ellipsis_p
2085 = !NILP (current_buffer->selective_display_ellipses);
2086
2087 /* Display table to use. */
2088 it->dp = window_display_table (w);
2089
2090 /* Are multibyte characters enabled in current_buffer? */
2091 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2092
2093 /* Non-zero if we should highlight the region. */
2094 highlight_region_p
3ebf0ea9 2095 = (!NILP (Vtransient_mark_mode)
5f5c8ee5
GM
2096 && !NILP (current_buffer->mark_active)
2097 && XMARKER (current_buffer->mark)->buffer != 0);
2098
2099 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2100 start and end of a visible region in window IT->w. Set both to
2101 -1 to indicate no region. */
2102 if (highlight_region_p
2103 /* Maybe highlight only in selected window. */
2311178e 2104 && (/* Either show region everywhere. */
5f5c8ee5
GM
2105 highlight_nonselected_windows
2106 /* Or show region in the selected window. */
2107 || w == XWINDOW (selected_window)
2108 /* Or show the region if we are in the mini-buffer and W is
2109 the window the mini-buffer refers to. */
2110 || (MINI_WINDOW_P (XWINDOW (selected_window))
5705966b
KS
2111 && WINDOWP (minibuf_selected_window)
2112 && w == XWINDOW (minibuf_selected_window))))
5f5c8ee5
GM
2113 {
2114 int charpos = marker_position (current_buffer->mark);
2115 it->region_beg_charpos = min (PT, charpos);
2116 it->region_end_charpos = max (PT, charpos);
2117 }
2118 else
2119 it->region_beg_charpos = it->region_end_charpos = -1;
2120
2121 /* Get the position at which the redisplay_end_trigger hook should
2122 be run, if it is to be run at all. */
2123 if (MARKERP (w->redisplay_end_trigger)
2124 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2125 it->redisplay_end_trigger_charpos
2126 = marker_position (w->redisplay_end_trigger);
2127 else if (INTEGERP (w->redisplay_end_trigger))
2128 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2129
2130 /* Correct bogus values of tab_width. */
2131 it->tab_width = XINT (current_buffer->tab_width);
2132 if (it->tab_width <= 0 || it->tab_width > 1000)
2133 it->tab_width = 8;
2134
2135 /* Are lines in the display truncated? */
2136 it->truncate_lines_p
2137 = (base_face_id != DEFAULT_FACE_ID
2138 || XINT (it->w->hscroll)
2139 || (truncate_partial_width_windows
2140 && !WINDOW_FULL_WIDTH_P (it->w))
2141 || !NILP (current_buffer->truncate_lines));
2142
2143 /* Get dimensions of truncation and continuation glyphs. These are
b46952ae 2144 displayed as fringe bitmaps under X, so we don't need them for such
5f5c8ee5
GM
2145 frames. */
2146 if (!FRAME_WINDOW_P (it->f))
2147 {
2148 if (it->truncate_lines_p)
2149 {
2150 /* We will need the truncation glyph. */
2151 xassert (it->glyph_row == NULL);
2152 produce_special_glyphs (it, IT_TRUNCATION);
2153 it->truncation_pixel_width = it->pixel_width;
2154 }
2155 else
2156 {
2157 /* We will need the continuation glyph. */
2158 xassert (it->glyph_row == NULL);
2159 produce_special_glyphs (it, IT_CONTINUATION);
2160 it->continuation_pixel_width = it->pixel_width;
2161 }
2162
153c2160 2163 /* Reset these values to zero because the produce_special_glyphs
5f5c8ee5
GM
2164 above has changed them. */
2165 it->pixel_width = it->ascent = it->descent = 0;
312246d1 2166 it->phys_ascent = it->phys_descent = 0;
5f5c8ee5
GM
2167 }
2168
2169 /* Set this after getting the dimensions of truncation and
2170 continuation glyphs, so that we don't produce glyphs when calling
2171 produce_special_glyphs, above. */
2172 it->glyph_row = row;
2173 it->area = TEXT_AREA;
2174
2175 /* Get the dimensions of the display area. The display area
2176 consists of the visible window area plus a horizontally scrolled
2177 part to the left of the window. All x-values are relative to the
2178 start of this total display area. */
2179 if (base_face_id != DEFAULT_FACE_ID)
2180 {
2181 /* Mode lines, menu bar in terminal frames. */
2182 it->first_visible_x = 0;
da8b7f4f 2183 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
5f5c8ee5
GM
2184 }
2185 else
2186 {
2187 it->first_visible_x
da8b7f4f 2188 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
5f5c8ee5
GM
2189 it->last_visible_x = (it->first_visible_x
2190 + window_box_width (w, TEXT_AREA));
2191
2192 /* If we truncate lines, leave room for the truncator glyph(s) at
2193 the right margin. Otherwise, leave room for the continuation
2194 glyph(s). Truncation and continuation glyphs are not inserted
2195 for window-based redisplay. */
2196 if (!FRAME_WINDOW_P (it->f))
2197 {
2198 if (it->truncate_lines_p)
2199 it->last_visible_x -= it->truncation_pixel_width;
2200 else
2201 it->last_visible_x -= it->continuation_pixel_width;
2202 }
2203
045dee35 2204 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
da8b7f4f 2205 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
5f5c8ee5
GM
2206 }
2207
2208 /* Leave room for a border glyph. */
2209 if (!FRAME_WINDOW_P (it->f)
2210 && !WINDOW_RIGHTMOST_P (it->w))
2211 it->last_visible_x -= 1;
2212
2213 it->last_visible_y = window_text_bottom_y (w);
2214
2215 /* For mode lines and alike, arrange for the first glyph having a
2216 left box line if the face specifies a box. */
2217 if (base_face_id != DEFAULT_FACE_ID)
2218 {
2219 struct face *face;
2311178e 2220
5f5c8ee5
GM
2221 it->face_id = base_face_id;
2222
2223 /* If we have a boxed mode line, make the first character appear
2224 with a left box line. */
2225 face = FACE_FROM_ID (it->f, base_face_id);
2226 if (face->box != FACE_NO_BOX)
2227 it->start_of_box_run_p = 1;
2228 }
2229
2230 /* If a buffer position was specified, set the iterator there,
2231 getting overlays and face properties from that position. */
3ebf0ea9 2232 if (charpos >= BUF_BEG (current_buffer))
5f5c8ee5
GM
2233 {
2234 it->end_charpos = ZV;
2235 it->face_id = -1;
2236 IT_CHARPOS (*it) = charpos;
2311178e 2237
5f5c8ee5 2238 /* Compute byte position if not specified. */
3ebf0ea9 2239 if (bytepos < charpos)
5f5c8ee5
GM
2240 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2241 else
2242 IT_BYTEPOS (*it) = bytepos;
2243
29b3ea63
KS
2244 it->start = it->current;
2245
5f5c8ee5
GM
2246 /* Compute faces etc. */
2247 reseat (it, it->current.pos, 1);
2248 }
2311178e 2249
5f5c8ee5
GM
2250 CHECK_IT (it);
2251}
2252
2253
2254/* Initialize IT for the display of window W with window start POS. */
2255
2256void
2257start_display (it, w, pos)
2258 struct it *it;
2259 struct window *w;
2260 struct text_pos pos;
2261{
5f5c8ee5 2262 struct glyph_row *row;
045dee35 2263 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
5f5c8ee5
GM
2264
2265 row = w->desired_matrix->rows + first_vpos;
2266 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3ccb8bcf 2267 it->first_vpos = first_vpos;
17fdcfc8
GM
2268
2269 if (!it->truncate_lines_p)
5f5c8ee5 2270 {
17fdcfc8
GM
2271 int start_at_line_beg_p;
2272 int first_y = it->current_y;
2311178e 2273
17fdcfc8
GM
2274 /* If window start is not at a line start, skip forward to POS to
2275 get the correct continuation lines width. */
2276 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2277 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2278 if (!start_at_line_beg_p)
5f5c8ee5 2279 {
0e47bbf7
RS
2280 int new_x;
2281
17fdcfc8
GM
2282 reseat_at_previous_visible_line_start (it);
2283 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2284
0e47bbf7
RS
2285 new_x = it->current_x + it->pixel_width;
2286
17fdcfc8
GM
2287 /* If lines are continued, this line may end in the middle
2288 of a multi-glyph character (e.g. a control character
2289 displayed as \003, or in the middle of an overlay
2290 string). In this case move_it_to above will not have
2291 taken us to the start of the continuation line but to the
2292 end of the continued line. */
0e47bbf7
RS
2293 if (it->current_x > 0
2294 && !it->truncate_lines_p /* Lines are continued. */
2295 && (/* And glyph doesn't fit on the line. */
2296 new_x > it->last_visible_x
2297 /* Or it fits exactly and we're on a window
2298 system frame. */
2299 || (new_x == it->last_visible_x
2300 && FRAME_WINDOW_P (it->f))))
5f5c8ee5 2301 {
b28cb6ed
GM
2302 if (it->current.dpvec_index >= 0
2303 || it->current.overlay_string_index >= 0)
2304 {
cafafe0b 2305 set_iterator_to_next (it, 1);
b28cb6ed
GM
2306 move_it_in_display_line_to (it, -1, -1, 0);
2307 }
2311178e 2308
b28cb6ed 2309 it->continuation_lines_width += it->current_x;
5f5c8ee5 2310 }
b28cb6ed
GM
2311
2312 /* We're starting a new display line, not affected by the
2313 height of the continued line, so clear the appropriate
2314 fields in the iterator structure. */
2315 it->max_ascent = it->max_descent = 0;
2316 it->max_phys_ascent = it->max_phys_descent = 0;
2311178e 2317
17fdcfc8
GM
2318 it->current_y = first_y;
2319 it->vpos = 0;
2320 it->current_x = it->hpos = 0;
2321 }
5f5c8ee5
GM
2322 }
2323
2324#if 0 /* Don't assert the following because start_display is sometimes
2325 called intentionally with a window start that is not at a
2326 line start. Please leave this code in as a comment. */
2311178e 2327
5f5c8ee5
GM
2328 /* Window start should be on a line start, now. */
2329 xassert (it->continuation_lines_width
2330 || IT_CHARPOS (it) == BEGV
2331 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2332#endif /* 0 */
2333}
2334
2335
4bde0ebb
GM
2336/* Return 1 if POS is a position in ellipses displayed for invisible
2337 text. W is the window we display, for text property lookup. */
5f5c8ee5 2338
4bde0ebb
GM
2339static int
2340in_ellipses_for_invisible_text_p (pos, w)
5f5c8ee5 2341 struct display_pos *pos;
4bde0ebb 2342 struct window *w;
5f5c8ee5 2343{
ac90c44f 2344 Lisp_Object prop, window;
4bde0ebb
GM
2345 int ellipses_p = 0;
2346 int charpos = CHARPOS (pos->pos);
2311178e 2347
ac90c44f
GM
2348 /* If POS specifies a position in a display vector, this might
2349 be for an ellipsis displayed for invisible text. We won't
2350 get the iterator set up for delivering that ellipsis unless
2351 we make sure that it gets aware of the invisible text. */
2352 if (pos->dpvec_index >= 0
2353 && pos->overlay_string_index < 0
2354 && CHARPOS (pos->string_pos) < 0
2355 && charpos > BEGV
2356 && (XSETWINDOW (window, w),
2357 prop = Fget_char_property (make_number (charpos),
2358 Qinvisible, window),
20fbd925 2359 !TEXT_PROP_MEANS_INVISIBLE (prop)))
ac90c44f
GM
2360 {
2361 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2362 window);
8580a4e3 2363 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
4bde0ebb
GM
2364 }
2365
2366 return ellipses_p;
2367}
2368
2369
2370/* Initialize IT for stepping through current_buffer in window W,
2371 starting at position POS that includes overlay string and display
47d57b22
GM
2372 vector/ control character translation position information. Value
2373 is zero if there are overlay strings with newlines at POS. */
4bde0ebb 2374
47d57b22 2375static int
4bde0ebb
GM
2376init_from_display_pos (it, w, pos)
2377 struct it *it;
2378 struct window *w;
2379 struct display_pos *pos;
2380{
2381 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
47d57b22 2382 int i, overlay_strings_with_newlines = 0;
2311178e 2383
4bde0ebb
GM
2384 /* If POS specifies a position in a display vector, this might
2385 be for an ellipsis displayed for invisible text. We won't
2386 get the iterator set up for delivering that ellipsis unless
2387 we make sure that it gets aware of the invisible text. */
2388 if (in_ellipses_for_invisible_text_p (pos, w))
2389 {
2390 --charpos;
2391 bytepos = 0;
ac90c44f 2392 }
2311178e 2393
5f5c8ee5
GM
2394 /* Keep in mind: the call to reseat in init_iterator skips invisible
2395 text, so we might end up at a position different from POS. This
2396 is only a problem when POS is a row start after a newline and an
2397 overlay starts there with an after-string, and the overlay has an
2398 invisible property. Since we don't skip invisible text in
2399 display_line and elsewhere immediately after consuming the
2400 newline before the row start, such a POS will not be in a string,
2401 but the call to init_iterator below will move us to the
2402 after-string. */
ac90c44f 2403 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
5f5c8ee5 2404
47d57b22 2405 for (i = 0; i < it->n_overlay_strings; ++i)
1e1e5daf 2406 {
50f80c2f
KR
2407 const char *s = SDATA (it->overlay_strings[i]);
2408 const char *e = s + SBYTES (it->overlay_strings[i]);
2311178e 2409
1e1e5daf
GM
2410 while (s < e && *s != '\n')
2411 ++s;
2412
2413 if (s < e)
2414 {
2415 overlay_strings_with_newlines = 1;
2416 break;
2417 }
2418 }
47d57b22 2419
75c5350a
GM
2420 /* If position is within an overlay string, set up IT to the right
2421 overlay string. */
5f5c8ee5
GM
2422 if (pos->overlay_string_index >= 0)
2423 {
2424 int relative_index;
75c5350a
GM
2425
2426 /* If the first overlay string happens to have a `display'
2427 property for an image, the iterator will be set up for that
2428 image, and we have to undo that setup first before we can
2429 correct the overlay string index. */
2430 if (it->method == next_element_from_image)
2431 pop_it (it);
2311178e 2432
5f5c8ee5
GM
2433 /* We already have the first chunk of overlay strings in
2434 IT->overlay_strings. Load more until the one for
2435 pos->overlay_string_index is in IT->overlay_strings. */
2436 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2437 {
2438 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2439 it->current.overlay_string_index = 0;
2440 while (n--)
2441 {
5a08cbaf 2442 load_overlay_strings (it, 0);
5f5c8ee5
GM
2443 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2444 }
2445 }
2311178e 2446
5f5c8ee5
GM
2447 it->current.overlay_string_index = pos->overlay_string_index;
2448 relative_index = (it->current.overlay_string_index
2449 % OVERLAY_STRING_CHUNK_SIZE);
2450 it->string = it->overlay_strings[relative_index];
cafafe0b 2451 xassert (STRINGP (it->string));
5f5c8ee5
GM
2452 it->current.string_pos = pos->string_pos;
2453 it->method = next_element_from_string;
2454 }
2311178e 2455
1e1e5daf
GM
2456#if 0 /* This is bogus because POS not having an overlay string
2457 position does not mean it's after the string. Example: A
2458 line starting with a before-string and initialization of IT
2459 to the previous row's end position. */
2be8f184
GM
2460 else if (it->current.overlay_string_index >= 0)
2461 {
2462 /* If POS says we're already after an overlay string ending at
2463 POS, make sure to pop the iterator because it will be in
2464 front of that overlay string. When POS is ZV, we've thereby
2465 also ``processed'' overlay strings at ZV. */
aeb2b8fc
GM
2466 while (it->sp)
2467 pop_it (it);
2be8f184
GM
2468 it->current.overlay_string_index = -1;
2469 it->method = next_element_from_buffer;
2470 if (CHARPOS (pos->pos) == ZV)
2471 it->overlay_strings_at_end_processed_p = 1;
2472 }
1e1e5daf 2473#endif /* 0 */
2311178e 2474
2be8f184 2475 if (CHARPOS (pos->string_pos) >= 0)
5f5c8ee5
GM
2476 {
2477 /* Recorded position is not in an overlay string, but in another
2478 string. This can only be a string from a `display' property.
2479 IT should already be filled with that string. */
2480 it->current.string_pos = pos->string_pos;
2481 xassert (STRINGP (it->string));
2482 }
2483
ac90c44f
GM
2484 /* Restore position in display vector translations, control
2485 character translations or ellipses. */
5f5c8ee5
GM
2486 if (pos->dpvec_index >= 0)
2487 {
ac90c44f
GM
2488 if (it->dpvec == NULL)
2489 get_next_display_element (it);
5f5c8ee5
GM
2490 xassert (it->dpvec && it->current.dpvec_index == 0);
2491 it->current.dpvec_index = pos->dpvec_index;
2492 }
2311178e 2493
5f5c8ee5 2494 CHECK_IT (it);
47d57b22 2495 return !overlay_strings_with_newlines;
5f5c8ee5
GM
2496}
2497
2498
2499/* Initialize IT for stepping through current_buffer in window W
2500 starting at ROW->start. */
2501
2502static void
2503init_to_row_start (it, w, row)
2504 struct it *it;
2505 struct window *w;
2506 struct glyph_row *row;
2507{
2508 init_from_display_pos (it, w, &row->start);
29b3ea63 2509 it->start = row->start;
5f5c8ee5
GM
2510 it->continuation_lines_width = row->continuation_lines_width;
2511 CHECK_IT (it);
2512}
2513
2311178e 2514
5f5c8ee5 2515/* Initialize IT for stepping through current_buffer in window W
47d57b22
GM
2516 starting in the line following ROW, i.e. starting at ROW->end.
2517 Value is zero if there are overlay strings with newlines at ROW's
2518 end position. */
5f5c8ee5 2519
47d57b22 2520static int
5f5c8ee5
GM
2521init_to_row_end (it, w, row)
2522 struct it *it;
2523 struct window *w;
2524 struct glyph_row *row;
2525{
47d57b22 2526 int success = 0;
2311178e 2527
47d57b22
GM
2528 if (init_from_display_pos (it, w, &row->end))
2529 {
2530 if (row->continued_p)
2531 it->continuation_lines_width
2532 = row->continuation_lines_width + row->pixel_width;
2533 CHECK_IT (it);
2534 success = 1;
2535 }
2311178e 2536
47d57b22 2537 return success;
5f5c8ee5
GM
2538}
2539
2540
2541
2542\f
2543/***********************************************************************
2544 Text properties
2545 ***********************************************************************/
2546
2547/* Called when IT reaches IT->stop_charpos. Handle text property and
2548 overlay changes. Set IT->stop_charpos to the next position where
2549 to stop. */
2550
2551static void
2552handle_stop (it)
2553 struct it *it;
2554{
2555 enum prop_handled handled;
2556 int handle_overlay_change_p = 1;
2557 struct props *p;
2558
2559 it->dpvec = NULL;
2560 it->current.dpvec_index = -1;
2561
2562 do
2563 {
2564 handled = HANDLED_NORMALLY;
2311178e 2565
5f5c8ee5
GM
2566 /* Call text property handlers. */
2567 for (p = it_props; p->handler; ++p)
2568 {
2569 handled = p->handler (it);
2970b9be 2570
5f5c8ee5
GM
2571 if (handled == HANDLED_RECOMPUTE_PROPS)
2572 break;
2573 else if (handled == HANDLED_RETURN)
2574 return;
2575 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2576 handle_overlay_change_p = 0;
2577 }
2578
2579 if (handled != HANDLED_RECOMPUTE_PROPS)
2580 {
2581 /* Don't check for overlay strings below when set to deliver
2582 characters from a display vector. */
2583 if (it->method == next_element_from_display_vector)
2584 handle_overlay_change_p = 0;
2585
2586 /* Handle overlay changes. */
2587 if (handle_overlay_change_p)
2588 handled = handle_overlay_change (it);
2311178e 2589
5f5c8ee5
GM
2590 /* Determine where to stop next. */
2591 if (handled == HANDLED_NORMALLY)
2592 compute_stop_pos (it);
2593 }
2594 }
2595 while (handled == HANDLED_RECOMPUTE_PROPS);
2596}
2597
2598
2599/* Compute IT->stop_charpos from text property and overlay change
2600 information for IT's current position. */
2601
2602static void
2603compute_stop_pos (it)
2604 struct it *it;
2605{
2606 register INTERVAL iv, next_iv;
2607 Lisp_Object object, limit, position;
2608
2609 /* If nowhere else, stop at the end. */
2610 it->stop_charpos = it->end_charpos;
2311178e 2611
5f5c8ee5
GM
2612 if (STRINGP (it->string))
2613 {
2614 /* Strings are usually short, so don't limit the search for
2615 properties. */
2616 object = it->string;
2617 limit = Qnil;
ac90c44f 2618 position = make_number (IT_STRING_CHARPOS (*it));
5f5c8ee5
GM
2619 }
2620 else
2621 {
2622 int charpos;
2623
2624 /* If next overlay change is in front of the current stop pos
2625 (which is IT->end_charpos), stop there. Note: value of
2626 next_overlay_change is point-max if no overlay change
2627 follows. */
2628 charpos = next_overlay_change (IT_CHARPOS (*it));
2629 if (charpos < it->stop_charpos)
2630 it->stop_charpos = charpos;
2631
2632 /* If showing the region, we have to stop at the region
2633 start or end because the face might change there. */
2634 if (it->region_beg_charpos > 0)
2635 {
2636 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2637 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2638 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2639 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2640 }
2311178e 2641
5f5c8ee5
GM
2642 /* Set up variables for computing the stop position from text
2643 property changes. */
2644 XSETBUFFER (object, current_buffer);
ac90c44f
GM
2645 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2646 position = make_number (IT_CHARPOS (*it));
5f5c8ee5
GM
2647
2648 }
2649
2650 /* Get the interval containing IT's position. Value is a null
2651 interval if there isn't such an interval. */
2652 iv = validate_interval_range (object, &position, &position, 0);
2653 if (!NULL_INTERVAL_P (iv))
2654 {
2655 Lisp_Object values_here[LAST_PROP_IDX];
2656 struct props *p;
2657
2658 /* Get properties here. */
2659 for (p = it_props; p->handler; ++p)
2660 values_here[p->idx] = textget (iv->plist, *p->name);
2661
2662 /* Look for an interval following iv that has different
2663 properties. */
2664 for (next_iv = next_interval (iv);
2665 (!NULL_INTERVAL_P (next_iv)
2666 && (NILP (limit)
2667 || XFASTINT (limit) > next_iv->position));
2668 next_iv = next_interval (next_iv))
2669 {
2670 for (p = it_props; p->handler; ++p)
2671 {
2672 Lisp_Object new_value;
2673
2674 new_value = textget (next_iv->plist, *p->name);
2675 if (!EQ (values_here[p->idx], new_value))
2676 break;
2677 }
2311178e 2678
5f5c8ee5
GM
2679 if (p->handler)
2680 break;
2681 }
2682
2683 if (!NULL_INTERVAL_P (next_iv))
2684 {
2685 if (INTEGERP (limit)
2686 && next_iv->position >= XFASTINT (limit))
2687 /* No text property change up to limit. */
2688 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2689 else
2690 /* Text properties change in next_iv. */
2691 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2692 }
2693 }
2694
2695 xassert (STRINGP (it->string)
2696 || (it->stop_charpos >= BEGV
2697 && it->stop_charpos >= IT_CHARPOS (*it)));
2698}
2699
2700
2701/* Return the position of the next overlay change after POS in
2702 current_buffer. Value is point-max if no overlay change
2703 follows. This is like `next-overlay-change' but doesn't use
2704 xmalloc. */
2705
2706static int
2707next_overlay_change (pos)
2708 int pos;
2709{
2710 int noverlays;
2711 int endpos;
2712 Lisp_Object *overlays;
2713 int len;
2714 int i;
2715
2716 /* Get all overlays at the given position. */
2717 len = 10;
2718 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
a0315a63 2719 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
5f5c8ee5
GM
2720 if (noverlays > len)
2721 {
2722 len = noverlays;
2723 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
a0315a63 2724 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
5f5c8ee5
GM
2725 }
2726
2727 /* If any of these overlays ends before endpos,
2728 use its ending point instead. */
2729 for (i = 0; i < noverlays; ++i)
2730 {
2731 Lisp_Object oend;
2732 int oendpos;
2733
2734 oend = OVERLAY_END (overlays[i]);
2735 oendpos = OVERLAY_POSITION (oend);
2736 endpos = min (endpos, oendpos);
2737 }
2738
2739 return endpos;
2740}
2741
2742
2743\f
2744/***********************************************************************
2745 Fontification
2746 ***********************************************************************/
2747
2748/* Handle changes in the `fontified' property of the current buffer by
2749 calling hook functions from Qfontification_functions to fontify
2750 regions of text. */
2751
2752static enum prop_handled
2753handle_fontified_prop (it)
2754 struct it *it;
2755{
2756 Lisp_Object prop, pos;
2757 enum prop_handled handled = HANDLED_NORMALLY;
2758
2759 /* Get the value of the `fontified' property at IT's current buffer
2760 position. (The `fontified' property doesn't have a special
2761 meaning in strings.) If the value is nil, call functions from
2762 Qfontification_functions. */
2763 if (!STRINGP (it->string)
2764 && it->s == NULL
2765 && !NILP (Vfontification_functions)
085536c2 2766 && !NILP (Vrun_hooks)
5f5c8ee5
GM
2767 && (pos = make_number (IT_CHARPOS (*it)),
2768 prop = Fget_char_property (pos, Qfontified, Qnil),
2769 NILP (prop)))
2770 {
331379bf 2771 int count = SPECPDL_INDEX ();
085536c2
GM
2772 Lisp_Object val;
2773
2774 val = Vfontification_functions;
2775 specbind (Qfontification_functions, Qnil);
2311178e 2776
085536c2 2777 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
116d6f5c 2778 safe_call1 (val, pos);
085536c2
GM
2779 else
2780 {
2781 Lisp_Object globals, fn;
2782 struct gcpro gcpro1, gcpro2;
5f5c8ee5 2783
085536c2
GM
2784 globals = Qnil;
2785 GCPRO2 (val, globals);
2311178e 2786
085536c2
GM
2787 for (; CONSP (val); val = XCDR (val))
2788 {
2789 fn = XCAR (val);
2311178e 2790
085536c2
GM
2791 if (EQ (fn, Qt))
2792 {
2793 /* A value of t indicates this hook has a local
2794 binding; it means to run the global binding too.
2795 In a global value, t should not occur. If it
2796 does, we must ignore it to avoid an endless
2797 loop. */
2798 for (globals = Fdefault_value (Qfontification_functions);
2799 CONSP (globals);
2800 globals = XCDR (globals))
2801 {
2802 fn = XCAR (globals);
2803 if (!EQ (fn, Qt))
116d6f5c 2804 safe_call1 (fn, pos);
085536c2
GM
2805 }
2806 }
2807 else
116d6f5c 2808 safe_call1 (fn, pos);
085536c2
GM
2809 }
2810
2811 UNGCPRO;
2812 }
2813
2814 unbind_to (count, Qnil);
5f5c8ee5
GM
2815
2816 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2817 something. This avoids an endless loop if they failed to
2818 fontify the text for which reason ever. */
2819 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2820 handled = HANDLED_RECOMPUTE_PROPS;
2821 }
2822
2823 return handled;
2824}
2825
2826
2827\f
2828/***********************************************************************
2829 Faces
2830 ***********************************************************************/
2831
2832/* Set up iterator IT from face properties at its current position.
2833 Called from handle_stop. */
2834
2835static enum prop_handled
2836handle_face_prop (it)
2837 struct it *it;
2838{
2839 int new_face_id, next_stop;
2311178e 2840
5f5c8ee5
GM
2841 if (!STRINGP (it->string))
2842 {
2843 new_face_id
2844 = face_at_buffer_position (it->w,
2845 IT_CHARPOS (*it),
2846 it->region_beg_charpos,
2847 it->region_end_charpos,
2848 &next_stop,
2849 (IT_CHARPOS (*it)
2850 + TEXT_PROP_DISTANCE_LIMIT),
2851 0);
2311178e 2852
5f5c8ee5
GM
2853 /* Is this a start of a run of characters with box face?
2854 Caveat: this can be called for a freshly initialized
4b41cebb 2855 iterator; face_id is -1 in this case. We know that the new
5f5c8ee5
GM
2856 face will not change until limit, i.e. if the new face has a
2857 box, all characters up to limit will have one. But, as
2858 usual, we don't know whether limit is really the end. */
2859 if (new_face_id != it->face_id)
2860 {
2861 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2311178e 2862
5f5c8ee5
GM
2863 /* If new face has a box but old face has not, this is
2864 the start of a run of characters with box, i.e. it has
2865 a shadow on the left side. The value of face_id of the
2866 iterator will be -1 if this is the initial call that gets
2867 the face. In this case, we have to look in front of IT's
2868 position and see whether there is a face != new_face_id. */
2869 it->start_of_box_run_p
2870 = (new_face->box != FACE_NO_BOX
2871 && (it->face_id >= 0
2872 || IT_CHARPOS (*it) == BEG
2873 || new_face_id != face_before_it_pos (it)));
2874 it->face_box_p = new_face->box != FACE_NO_BOX;
2875 }
2876 }
2877 else
2878 {
06a12811
GM
2879 int base_face_id, bufpos;
2880
2881 if (it->current.overlay_string_index >= 0)
2882 bufpos = IT_CHARPOS (*it);
2883 else
2884 bufpos = 0;
2311178e 2885
06a12811
GM
2886 /* For strings from a buffer, i.e. overlay strings or strings
2887 from a `display' property, use the face at IT's current
2888 buffer position as the base face to merge with, so that
2889 overlay strings appear in the same face as surrounding
2890 text, unless they specify their own faces. */
2891 base_face_id = underlying_face_id (it);
2311178e 2892
06a12811
GM
2893 new_face_id = face_at_string_position (it->w,
2894 it->string,
2895 IT_STRING_CHARPOS (*it),
2896 bufpos,
2897 it->region_beg_charpos,
2898 it->region_end_charpos,
2899 &next_stop,
5de7c6f2 2900 base_face_id, 0);
2311178e 2901
5f5c8ee5
GM
2902#if 0 /* This shouldn't be neccessary. Let's check it. */
2903 /* If IT is used to display a mode line we would really like to
2904 use the mode line face instead of the frame's default face. */
2905 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2906 && new_face_id == DEFAULT_FACE_ID)
96d2320f 2907 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
5f5c8ee5 2908#endif
2311178e 2909
5f5c8ee5
GM
2910 /* Is this a start of a run of characters with box? Caveat:
2911 this can be called for a freshly allocated iterator; face_id
2912 is -1 is this case. We know that the new face will not
2913 change until the next check pos, i.e. if the new face has a
2914 box, all characters up to that position will have a
2915 box. But, as usual, we don't know whether that position
2916 is really the end. */
2917 if (new_face_id != it->face_id)
2918 {
2919 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2920 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2311178e 2921
5f5c8ee5
GM
2922 /* If new face has a box but old face hasn't, this is the
2923 start of a run of characters with box, i.e. it has a
2924 shadow on the left side. */
2925 it->start_of_box_run_p
2926 = new_face->box && (old_face == NULL || !old_face->box);
2927 it->face_box_p = new_face->box != FACE_NO_BOX;
2928 }
2929 }
2311178e 2930
5f5c8ee5 2931 it->face_id = new_face_id;
5f5c8ee5
GM
2932 return HANDLED_NORMALLY;
2933}
2934
2935
06a12811
GM
2936/* Return the ID of the face ``underlying'' IT's current position,
2937 which is in a string. If the iterator is associated with a
2938 buffer, return the face at IT's current buffer position.
2939 Otherwise, use the iterator's base_face_id. */
2940
2941static int
2942underlying_face_id (it)
2943 struct it *it;
2944{
2945 int face_id = it->base_face_id, i;
2946
2947 xassert (STRINGP (it->string));
2948
2949 for (i = it->sp - 1; i >= 0; --i)
2950 if (NILP (it->stack[i].string))
2951 face_id = it->stack[i].face_id;
2952
2953 return face_id;
2954}
2955
2956
5f5c8ee5
GM
2957/* Compute the face one character before or after the current position
2958 of IT. BEFORE_P non-zero means get the face in front of IT's
2959 position. Value is the id of the face. */
2960
2961static int
2962face_before_or_after_it_pos (it, before_p)
2963 struct it *it;
2964 int before_p;
2965{
2966 int face_id, limit;
2967 int next_check_charpos;
2968 struct text_pos pos;
2969
2970 xassert (it->s == NULL);
2311178e 2971
5f5c8ee5
GM
2972 if (STRINGP (it->string))
2973 {
06a12811 2974 int bufpos, base_face_id;
2311178e 2975
5f5c8ee5
GM
2976 /* No face change past the end of the string (for the case
2977 we are padding with spaces). No face change before the
2978 string start. */
2051c264 2979 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
5f5c8ee5
GM
2980 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2981 return it->face_id;
2982
2983 /* Set pos to the position before or after IT's current position. */
2984 if (before_p)
2985 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2986 else
260a86a0
KH
2987 /* For composition, we must check the character after the
2988 composition. */
2989 pos = (it->what == IT_COMPOSITION
2990 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2991 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
5f5c8ee5 2992
06a12811
GM
2993 if (it->current.overlay_string_index >= 0)
2994 bufpos = IT_CHARPOS (*it);
2995 else
2996 bufpos = 0;
2997
2998 base_face_id = underlying_face_id (it);
2999
5f5c8ee5 3000 /* Get the face for ASCII, or unibyte. */
06a12811
GM
3001 face_id = face_at_string_position (it->w,
3002 it->string,
3003 CHARPOS (pos),
3004 bufpos,
3005 it->region_beg_charpos,
3006 it->region_end_charpos,
3007 &next_check_charpos,
5de7c6f2 3008 base_face_id, 0);
5f5c8ee5
GM
3009
3010 /* Correct the face for charsets different from ASCII. Do it
3011 for the multibyte case only. The face returned above is
3012 suitable for unibyte text if IT->string is unibyte. */
3013 if (STRING_MULTIBYTE (it->string))
3014 {
50f80c2f 3015 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
2051c264 3016 int rest = SBYTES (it->string) - BYTEPOS (pos);
980806b6
KH
3017 int c, len;
3018 struct face *face = FACE_FROM_ID (it->f, face_id);
2311178e 3019
4fdb80f2 3020 c = string_char_and_length (p, rest, &len);
980806b6 3021 face_id = FACE_FOR_CHAR (it->f, face, c);
5f5c8ee5
GM
3022 }
3023 }
3024 else
3025 {
70851746
GM
3026 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3027 || (IT_CHARPOS (*it) <= BEGV && before_p))
3028 return it->face_id;
2311178e 3029
5f5c8ee5
GM
3030 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3031 pos = it->current.pos;
2311178e 3032
5f5c8ee5 3033 if (before_p)
c1005d06 3034 DEC_TEXT_POS (pos, it->multibyte_p);
5f5c8ee5 3035 else
260a86a0
KH
3036 {
3037 if (it->what == IT_COMPOSITION)
3038 /* For composition, we must check the position after the
3039 composition. */
3040 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3041 else
c1005d06 3042 INC_TEXT_POS (pos, it->multibyte_p);
260a86a0 3043 }
2311178e 3044
5f5c8ee5
GM
3045 /* Determine face for CHARSET_ASCII, or unibyte. */
3046 face_id = face_at_buffer_position (it->w,
3047 CHARPOS (pos),
3048 it->region_beg_charpos,
3049 it->region_end_charpos,
3050 &next_check_charpos,
3051 limit, 0);
3052
3053 /* Correct the face for charsets different from ASCII. Do it
3054 for the multibyte case only. The face returned above is
3055 suitable for unibyte text if current_buffer is unibyte. */
3056 if (it->multibyte_p)
3057 {
1d1b6e6a 3058 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
980806b6
KH
3059 struct face *face = FACE_FROM_ID (it->f, face_id);
3060 face_id = FACE_FOR_CHAR (it->f, face, c);
5f5c8ee5
GM
3061 }
3062 }
2311178e 3063
5f5c8ee5
GM
3064 return face_id;
3065}
3066
3067
3068\f
3069/***********************************************************************
3070 Invisible text
3071 ***********************************************************************/
3072
3073/* Set up iterator IT from invisible properties at its current
3074 position. Called from handle_stop. */
3075
3076static enum prop_handled
3077handle_invisible_prop (it)
3078 struct it *it;
3079{
3080 enum prop_handled handled = HANDLED_NORMALLY;
3081
3082 if (STRINGP (it->string))
3083 {
3084 extern Lisp_Object Qinvisible;
3085 Lisp_Object prop, end_charpos, limit, charpos;
3086
3087 /* Get the value of the invisible text property at the
3088 current position. Value will be nil if there is no such
3089 property. */
ac90c44f 3090 charpos = make_number (IT_STRING_CHARPOS (*it));
5f5c8ee5
GM
3091 prop = Fget_text_property (charpos, Qinvisible, it->string);
3092
eadc0bf8
GM
3093 if (!NILP (prop)
3094 && IT_STRING_CHARPOS (*it) < it->end_charpos)
5f5c8ee5
GM
3095 {
3096 handled = HANDLED_RECOMPUTE_PROPS;
2311178e 3097
5f5c8ee5
GM
3098 /* Get the position at which the next change of the
3099 invisible text property can be found in IT->string.
3100 Value will be nil if the property value is the same for
3101 all the rest of IT->string. */
2051c264 3102 XSETINT (limit, SCHARS (it->string));
5f5c8ee5 3103 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2051c264 3104 it->string, limit);
2311178e 3105
5f5c8ee5
GM
3106 /* Text at current position is invisible. The next
3107 change in the property is at position end_charpos.
3108 Move IT's current position to that position. */
3109 if (INTEGERP (end_charpos)
3110 && XFASTINT (end_charpos) < XFASTINT (limit))
3111 {
3112 struct text_pos old;
3113 old = it->current.string_pos;
3114 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3115 compute_string_pos (&it->current.string_pos, old, it->string);
3116 }
3117 else
3118 {
3119 /* The rest of the string is invisible. If this is an
3120 overlay string, proceed with the next overlay string
3121 or whatever comes and return a character from there. */
3122 if (it->current.overlay_string_index >= 0)
3123 {
3124 next_overlay_string (it);
3125 /* Don't check for overlay strings when we just
3126 finished processing them. */
3127 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3128 }
3129 else
3130 {
2051c264
GM
3131 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3132 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
5f5c8ee5
GM
3133 }
3134 }
3135 }
3136 }
3137 else
3138 {
8580a4e3 3139 int invis_p, newpos, next_stop, start_charpos;
5a08cbaf 3140 Lisp_Object pos, prop, overlay;
5f5c8ee5
GM
3141
3142 /* First of all, is there invisible text at this position? */
5a08cbaf 3143 start_charpos = IT_CHARPOS (*it);
ac90c44f 3144 pos = make_number (IT_CHARPOS (*it));
5a08cbaf
GM
3145 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3146 &overlay);
8580a4e3
SM
3147 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3148
5f5c8ee5 3149 /* If we are on invisible text, skip over it. */
8580a4e3 3150 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
5f5c8ee5
GM
3151 {
3152 /* Record whether we have to display an ellipsis for the
3153 invisible text. */
8580a4e3 3154 int display_ellipsis_p = invis_p == 2;
5f5c8ee5
GM
3155
3156 handled = HANDLED_RECOMPUTE_PROPS;
2311178e 3157
5f5c8ee5
GM
3158 /* Loop skipping over invisible text. The loop is left at
3159 ZV or with IT on the first char being visible again. */
3160 do
3161 {
3162 /* Try to skip some invisible text. Return value is the
3163 position reached which can be equal to IT's position
3164 if there is nothing invisible here. This skips both
3165 over invisible text properties and overlays with
3166 invisible property. */
3167 newpos = skip_invisible (IT_CHARPOS (*it),
3168 &next_stop, ZV, it->window);
3169
3170 /* If we skipped nothing at all we weren't at invisible
3171 text in the first place. If everything to the end of
3172 the buffer was skipped, end the loop. */
3173 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
8580a4e3 3174 invis_p = 0;
5f5c8ee5
GM
3175 else
3176 {
3177 /* We skipped some characters but not necessarily
3178 all there are. Check if we ended up on visible
3179 text. Fget_char_property returns the property of
3180 the char before the given position, i.e. if we
8580a4e3 3181 get invis_p = 0, this means that the char at
5f5c8ee5 3182 newpos is visible. */
ac90c44f 3183 pos = make_number (newpos);
5f5c8ee5 3184 prop = Fget_char_property (pos, Qinvisible, it->window);
8580a4e3 3185 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
5f5c8ee5 3186 }
2311178e 3187
5f5c8ee5
GM
3188 /* If we ended up on invisible text, proceed to
3189 skip starting with next_stop. */
8580a4e3 3190 if (invis_p)
5f5c8ee5
GM
3191 IT_CHARPOS (*it) = next_stop;
3192 }
8580a4e3 3193 while (invis_p);
5a08cbaf 3194
5f5c8ee5
GM
3195 /* The position newpos is now either ZV or on visible text. */
3196 IT_CHARPOS (*it) = newpos;
3197 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2311178e 3198
5a08cbaf
GM
3199 /* If there are before-strings at the start of invisible
3200 text, and the text is invisible because of a text
3201 property, arrange to show before-strings because 20.x did
3202 it that way. (If the text is invisible because of an
3203 overlay property instead of a text property, this is
3204 already handled in the overlay code.) */
3205 if (NILP (overlay)
3206 && get_overlay_strings (it, start_charpos))
5f5c8ee5 3207 {
5a08cbaf
GM
3208 handled = HANDLED_RECOMPUTE_PROPS;
3209 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
5f5c8ee5 3210 }
5a08cbaf
GM
3211 else if (display_ellipsis_p)
3212 setup_for_ellipsis (it);
5f5c8ee5
GM
3213 }
3214 }
3215
3216 return handled;
3217}
3218
3219
5a08cbaf
GM
3220/* Make iterator IT return `...' next. */
3221
3222static void
3223setup_for_ellipsis (it)
3224 struct it *it;
3225{
2311178e 3226 if (it->dp
5a08cbaf
GM
3227 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3228 {
3229 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3230 it->dpvec = v->contents;
3231 it->dpend = v->contents + v->size;
3232 }
2311178e 3233 else
5a08cbaf
GM
3234 {
3235 /* Default `...'. */
3236 it->dpvec = default_invis_vector;
3237 it->dpend = default_invis_vector + 3;
3238 }
2311178e 3239
5a08cbaf
GM
3240 /* The ellipsis display does not replace the display of the
3241 character at the new position. Indicate this by setting
3242 IT->dpvec_char_len to zero. */
3243 it->dpvec_char_len = 0;
2311178e 3244
5a08cbaf
GM
3245 it->current.dpvec_index = 0;
3246 it->method = next_element_from_display_vector;
3247}
3248
3249
5f5c8ee5
GM
3250\f
3251/***********************************************************************
3252 'display' property
3253 ***********************************************************************/
3254
3255/* Set up iterator IT from `display' property at its current position.
3256 Called from handle_stop. */
3257
3258static enum prop_handled
3259handle_display_prop (it)
3260 struct it *it;
3261{
3262 Lisp_Object prop, object;
3263 struct text_pos *position;
a61b7058 3264 int display_replaced_p = 0;
5f5c8ee5
GM
3265
3266 if (STRINGP (it->string))
3267 {
3268 object = it->string;
3269 position = &it->current.string_pos;
3270 }
3271 else
3272 {
221dd3e7 3273 object = it->w->buffer;
5f5c8ee5
GM
3274 position = &it->current.pos;
3275 }
3276
3277 /* Reset those iterator values set from display property values. */
3278 it->font_height = Qnil;
3279 it->space_width = Qnil;
3280 it->voffset = 0;
3281
3282 /* We don't support recursive `display' properties, i.e. string
3283 values that have a string `display' property, that have a string
3284 `display' property etc. */
3285 if (!it->string_from_display_prop_p)
3286 it->area = TEXT_AREA;
3287
3288 prop = Fget_char_property (make_number (position->charpos),
3289 Qdisplay, object);
3290 if (NILP (prop))
3291 return HANDLED_NORMALLY;
3292
f3751a65 3293 if (CONSP (prop)
12700f40
GM
3294 /* Simple properties. */
3295 && !EQ (XCAR (prop), Qimage)
3296 && !EQ (XCAR (prop), Qspace)
3297 && !EQ (XCAR (prop), Qwhen)
3298 && !EQ (XCAR (prop), Qspace_width)
3299 && !EQ (XCAR (prop), Qheight)
3300 && !EQ (XCAR (prop), Qraise)
3301 /* Marginal area specifications. */
3302 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
6d925726
KS
3303 && !EQ (XCAR (prop), Qleft_fringe)
3304 && !EQ (XCAR (prop), Qright_fringe)
12700f40 3305 && !NILP (XCAR (prop)))
5f5c8ee5 3306 {
a61b7058 3307 for (; CONSP (prop); prop = XCDR (prop))
5f5c8ee5 3308 {
a61b7058
GM
3309 if (handle_single_display_prop (it, XCAR (prop), object,
3310 position, display_replaced_p))
3311 display_replaced_p = 1;
5f5c8ee5
GM
3312 }
3313 }
3314 else if (VECTORP (prop))
3315 {
3316 int i;
a61b7058
GM
3317 for (i = 0; i < ASIZE (prop); ++i)
3318 if (handle_single_display_prop (it, AREF (prop, i), object,
3319 position, display_replaced_p))
3320 display_replaced_p = 1;
5f5c8ee5
GM
3321 }
3322 else
3323 {
a61b7058
GM
3324 if (handle_single_display_prop (it, prop, object, position, 0))
3325 display_replaced_p = 1;
5f5c8ee5
GM
3326 }
3327
a61b7058 3328 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
5f5c8ee5
GM
3329}
3330
3331
6c577098 3332/* Value is the position of the end of the `display' property starting
5f5c8ee5
GM
3333 at START_POS in OBJECT. */
3334
3335static struct text_pos
3336display_prop_end (it, object, start_pos)
3337 struct it *it;
3338 Lisp_Object object;
3339 struct text_pos start_pos;
3340{
3341 Lisp_Object end;
3342 struct text_pos end_pos;
5f5c8ee5 3343
016b5642
MB
3344 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3345 Qdisplay, object, Qnil);
6c577098
GM
3346 CHARPOS (end_pos) = XFASTINT (end);
3347 if (STRINGP (object))
5f5c8ee5
GM
3348 compute_string_pos (&end_pos, start_pos, it->string);
3349 else
6c577098 3350 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
5f5c8ee5
GM
3351
3352 return end_pos;
3353}
3354
3355
3356/* Set up IT from a single `display' sub-property value PROP. OBJECT
3357 is the object in which the `display' property was found. *POSITION
a61b7058
GM
3358 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3359 means that we previously saw a display sub-property which already
3360 replaced text display with something else, for example an image;
3361 ignore such properties after the first one has been processed.
5f5c8ee5
GM
3362
3363 If PROP is a `space' or `image' sub-property, set *POSITION to the
3364 end position of the `display' property.
3365
4b41cebb 3366 Value is non-zero if something was found which replaces the display
a61b7058 3367 of buffer or string text. */
5f5c8ee5
GM
3368
3369static int
a61b7058
GM
3370handle_single_display_prop (it, prop, object, position,
3371 display_replaced_before_p)
5f5c8ee5
GM
3372 struct it *it;
3373 Lisp_Object prop;
3374 Lisp_Object object;
3375 struct text_pos *position;
a61b7058 3376 int display_replaced_before_p;
5f5c8ee5
GM
3377{
3378 Lisp_Object value;
a61b7058 3379 int replaces_text_display_p = 0;
5f5c8ee5
GM
3380 Lisp_Object form;
3381
d3acf96b 3382 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
4b41cebb 3383 evaluated. If the result is nil, VALUE is ignored. */
5f5c8ee5 3384 form = Qt;
d3acf96b 3385 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5f5c8ee5
GM
3386 {
3387 prop = XCDR (prop);
3388 if (!CONSP (prop))
3389 return 0;
3390 form = XCAR (prop);
3391 prop = XCDR (prop);
5f5c8ee5
GM
3392 }
3393
3394 if (!NILP (form) && !EQ (form, Qt))
3395 {
331379bf 3396 int count = SPECPDL_INDEX ();
5f5c8ee5 3397 struct gcpro gcpro1;
5f5c8ee5 3398
b384d6f8
GM
3399 /* Bind `object' to the object having the `display' property, a
3400 buffer or string. Bind `position' to the position in the
3401 object where the property was found, and `buffer-position'
3402 to the current position in the buffer. */
3403 specbind (Qobject, object);
31ac723b
SM
3404 specbind (Qposition, make_number (CHARPOS (*position)));
3405 specbind (Qbuffer_position,
3406 make_number (STRINGP (object)
3407 ? IT_CHARPOS (*it) : CHARPOS (*position)));
b384d6f8 3408 GCPRO1 (form);
116d6f5c 3409 form = safe_eval (form);
b384d6f8
GM
3410 UNGCPRO;
3411 unbind_to (count, Qnil);
5f5c8ee5 3412 }
2311178e 3413
5f5c8ee5
GM
3414 if (NILP (form))
3415 return 0;
3416
3417 if (CONSP (prop)
3418 && EQ (XCAR (prop), Qheight)
3419 && CONSP (XCDR (prop)))
3420 {
e4093f38 3421 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
5f5c8ee5 3422 return 0;
2311178e 3423
5f5c8ee5
GM
3424 /* `(height HEIGHT)'. */
3425 it->font_height = XCAR (XCDR (prop));
3426 if (!NILP (it->font_height))
3427 {
3428 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3429 int new_height = -1;
3430
3431 if (CONSP (it->font_height)
3432 && (EQ (XCAR (it->font_height), Qplus)
3433 || EQ (XCAR (it->font_height), Qminus))
3434 && CONSP (XCDR (it->font_height))
3435 && INTEGERP (XCAR (XCDR (it->font_height))))
3436 {
3437 /* `(+ N)' or `(- N)' where N is an integer. */
3438 int steps = XINT (XCAR (XCDR (it->font_height)));
3439 if (EQ (XCAR (it->font_height), Qplus))
3440 steps = - steps;
3441 it->face_id = smaller_face (it->f, it->face_id, steps);
3442 }
0d8b31c0 3443 else if (FUNCTIONP (it->font_height))
5f5c8ee5
GM
3444 {
3445 /* Call function with current height as argument.
3446 Value is the new height. */
116d6f5c
GM
3447 Lisp_Object height;
3448 height = safe_call1 (it->font_height,
3449 face->lface[LFACE_HEIGHT_INDEX]);
5f5c8ee5
GM
3450 if (NUMBERP (height))
3451 new_height = XFLOATINT (height);
5f5c8ee5
GM
3452 }
3453 else if (NUMBERP (it->font_height))
3454 {
3455 /* Value is a multiple of the canonical char height. */
3456 struct face *face;
2311178e 3457
5f5c8ee5
GM
3458 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3459 new_height = (XFLOATINT (it->font_height)
3460 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3461 }
3462 else
3463 {
3464 /* Evaluate IT->font_height with `height' bound to the
3465 current specified height to get the new height. */
3466 Lisp_Object value;
331379bf 3467 int count = SPECPDL_INDEX ();
2311178e 3468
5f5c8ee5 3469 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
116d6f5c 3470 value = safe_eval (it->font_height);
5f5c8ee5 3471 unbind_to (count, Qnil);
2311178e 3472
5f5c8ee5
GM
3473 if (NUMBERP (value))
3474 new_height = XFLOATINT (value);
3475 }
2311178e 3476
5f5c8ee5
GM
3477 if (new_height > 0)
3478 it->face_id = face_with_height (it->f, it->face_id, new_height);
3479 }
3480 }
3481 else if (CONSP (prop)
3482 && EQ (XCAR (prop), Qspace_width)
3483 && CONSP (XCDR (prop)))
3484 {
3485 /* `(space_width WIDTH)'. */
e4093f38 3486 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
bafb434c 3487 return 0;
2311178e 3488
5f5c8ee5
GM
3489 value = XCAR (XCDR (prop));
3490 if (NUMBERP (value) && XFLOATINT (value) > 0)
3491 it->space_width = value;
3492 }
3493 else if (CONSP (prop)
3494 && EQ (XCAR (prop), Qraise)
3495 && CONSP (XCDR (prop)))
3496 {
5f5c8ee5 3497 /* `(raise FACTOR)'. */
e4093f38 3498 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
bafb434c 3499 return 0;
2311178e 3500
fb3842a8 3501#ifdef HAVE_WINDOW_SYSTEM
5f5c8ee5
GM
3502 value = XCAR (XCDR (prop));
3503 if (NUMBERP (value))
3504 {
3505 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3506 it->voffset = - (XFLOATINT (value)
1ab3e082 3507 * (FONT_HEIGHT (face->font)));
5f5c8ee5
GM
3508 }
3509#endif /* HAVE_WINDOW_SYSTEM */
3510 }
3511 else if (!it->string_from_display_prop_p)
3512 {
f3751a65 3513 /* `((margin left-margin) VALUE)' or `((margin right-margin)
4b41cebb 3514 VALUE) or `((margin nil) VALUE)' or VALUE. */
5f5c8ee5
GM
3515 Lisp_Object location, value;
3516 struct text_pos start_pos;
3517 int valid_p;
3518
3519 /* Characters having this form of property are not displayed, so
3520 we have to find the end of the property. */
5f5c8ee5
GM
3521 start_pos = *position;
3522 *position = display_prop_end (it, object, start_pos);
15e26c76 3523 value = Qnil;
5f5c8ee5
GM
3524
3525 /* Let's stop at the new position and assume that all
3526 text properties change there. */
3527 it->stop_charpos = position->charpos;
3528
dd341dd9
KS
3529 if (CONSP (prop)
3530 && (EQ (XCAR (prop), Qleft_fringe)
3531 || EQ (XCAR (prop), Qright_fringe))
3532 && CONSP (XCDR (prop)))
3533 {
3534 unsigned face_id = DEFAULT_FACE_ID;
3535
3536 /* Save current settings of IT so that we can restore them
3537 when we are finished with the glyph property value. */
3538
3539 /* `(left-fringe BITMAP FACE)'. */
3540 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3541 return 0;
3542
3543#ifdef HAVE_WINDOW_SYSTEM
3544 value = XCAR (XCDR (prop));
3545 if (!NUMBERP (value)
3546 || !valid_fringe_bitmap_id_p (XINT (value)))
3547 return 0;
3548
3549 if (CONSP (XCDR (XCDR (prop))))
3550 {
3551 Lisp_Object face_name = XCAR (XCDR (XCDR (prop)));
3552
3553 face_id = lookup_named_face (it->f, face_name, 'A');
3554 if (face_id < 0)
3555 return 0;
3556 }
3557
3558 push_it (it);
3559
3560 it->area = TEXT_AREA;
3561 it->what = IT_IMAGE;
3562 it->image_id = -1; /* no image */
3563 it->position = start_pos;
3564 it->object = NILP (object) ? it->w->buffer : object;
3565 it->method = next_element_from_image;
3566 it->face_id = face_id;
3567
3568 /* Say that we haven't consumed the characters with
3569 `display' property yet. The call to pop_it in
3570 set_iterator_to_next will clean this up. */
3571 *position = start_pos;
3572
3573 if (EQ (XCAR (prop), Qleft_fringe))
3574 {
3575 it->left_user_fringe_bitmap = XINT (value);
3576 it->left_user_fringe_face_id = face_id;
3577 }
3578 else
3579 {
3580 it->right_user_fringe_bitmap = XINT (value);
3581 it->right_user_fringe_face_id = face_id;
3582 }
3583#endif /* HAVE_WINDOW_SYSTEM */
3584 return 1;
3585 }
3586
f3751a65
GM
3587 location = Qunbound;
3588 if (CONSP (prop) && CONSP (XCAR (prop)))
5f5c8ee5 3589 {
f3751a65 3590 Lisp_Object tem;
2311178e 3591
5f5c8ee5 3592 value = XCDR (prop);
f3751a65
GM
3593 if (CONSP (value))
3594 value = XCAR (value);
3595
3596 tem = XCAR (prop);
3597 if (EQ (XCAR (tem), Qmargin)
3598 && (tem = XCDR (tem),
3599 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3600 (NILP (tem)
3601 || EQ (tem, Qleft_margin)
3602 || EQ (tem, Qright_margin))))
3603 location = tem;
5f5c8ee5 3604 }
f3751a65
GM
3605
3606 if (EQ (location, Qunbound))
5f5c8ee5
GM
3607 {
3608 location = Qnil;
3609 value = prop;
3610 }
3611
da06e4d7 3612 valid_p = (STRINGP (value)
5f5c8ee5 3613#ifdef HAVE_WINDOW_SYSTEM
da06e4d7 3614 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
5f5c8ee5 3615#endif /* not HAVE_WINDOW_SYSTEM */
da06e4d7 3616 || (CONSP (value) && EQ (XCAR (value), Qspace)));
2311178e 3617
5f5c8ee5
GM
3618 if ((EQ (location, Qleft_margin)
3619 || EQ (location, Qright_margin)
3620 || NILP (location))
a61b7058
GM
3621 && valid_p
3622 && !display_replaced_before_p)
5f5c8ee5 3623 {
a61b7058 3624 replaces_text_display_p = 1;
2311178e 3625
5f5c8ee5
GM
3626 /* Save current settings of IT so that we can restore them
3627 when we are finished with the glyph property value. */
3628 push_it (it);
2311178e 3629
5f5c8ee5
GM
3630 if (NILP (location))
3631 it->area = TEXT_AREA;
3632 else if (EQ (location, Qleft_margin))
3633 it->area = LEFT_MARGIN_AREA;
3634 else
3635 it->area = RIGHT_MARGIN_AREA;
2311178e 3636
5f5c8ee5
GM
3637 if (STRINGP (value))
3638 {
3639 it->string = value;
3640 it->multibyte_p = STRING_MULTIBYTE (it->string);
3641 it->current.overlay_string_index = -1;
3642 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2051c264 3643 it->end_charpos = it->string_nchars = SCHARS (it->string);
5f5c8ee5
GM
3644 it->method = next_element_from_string;
3645 it->stop_charpos = 0;
3646 it->string_from_display_prop_p = 1;
e187cf71
GM
3647 /* Say that we haven't consumed the characters with
3648 `display' property yet. The call to pop_it in
3649 set_iterator_to_next will clean this up. */
3650 *position = start_pos;
5f5c8ee5
GM
3651 }
3652 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3653 {
3654 it->method = next_element_from_stretch;
3655 it->object = value;
3656 it->current.pos = it->position = start_pos;
3657 }
3658#ifdef HAVE_WINDOW_SYSTEM
3659 else
3660 {
3661 it->what = IT_IMAGE;
3662 it->image_id = lookup_image (it->f, value);
3663 it->position = start_pos;
3664 it->object = NILP (object) ? it->w->buffer : object;
3665 it->method = next_element_from_image;
2311178e 3666
02513cdd 3667 /* Say that we haven't consumed the characters with
5f5c8ee5
GM
3668 `display' property yet. The call to pop_it in
3669 set_iterator_to_next will clean this up. */
3670 *position = start_pos;
3671 }
3672#endif /* HAVE_WINDOW_SYSTEM */
3673 }
a21a3943
GM
3674 else
3675 /* Invalid property or property not supported. Restore
3676 the position to what it was before. */
3677 *position = start_pos;
5f5c8ee5
GM
3678 }
3679
a61b7058 3680 return replaces_text_display_p;
5f5c8ee5
GM
3681}
3682
3683
06568bbf
GM
3684/* Check if PROP is a display sub-property value whose text should be
3685 treated as intangible. */
3686
3687static int
3688single_display_prop_intangible_p (prop)
3689 Lisp_Object prop;
3690{
3691 /* Skip over `when FORM'. */
3692 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3693 {
3694 prop = XCDR (prop);
3695 if (!CONSP (prop))
3696 return 0;
3697 prop = XCDR (prop);
3698 }
3699
959bc044
SM
3700 if (STRINGP (prop))
3701 return 1;
3702
06568bbf
GM
3703 if (!CONSP (prop))
3704 return 0;
3705
3706 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3707 we don't need to treat text as intangible. */
3708 if (EQ (XCAR (prop), Qmargin))
3709 {
3710 prop = XCDR (prop);
3711 if (!CONSP (prop))
3712 return 0;
3713
3714 prop = XCDR (prop);
3715 if (!CONSP (prop)
3716 || EQ (XCAR (prop), Qleft_margin)
3717 || EQ (XCAR (prop), Qright_margin))
3718 return 0;
3719 }
2311178e 3720
6cd83a9a
SM
3721 return (CONSP (prop)
3722 && (EQ (XCAR (prop), Qimage)
3723 || EQ (XCAR (prop), Qspace)));
06568bbf
GM
3724}
3725
3726
3727/* Check if PROP is a display property value whose text should be
3728 treated as intangible. */
3729
3730int
3731display_prop_intangible_p (prop)
3732 Lisp_Object prop;
3733{
3734 if (CONSP (prop)
3735 && CONSP (XCAR (prop))
3736 && !EQ (Qmargin, XCAR (XCAR (prop))))
3737 {
3738 /* A list of sub-properties. */
3739 while (CONSP (prop))
3740 {
3741 if (single_display_prop_intangible_p (XCAR (prop)))
3742 return 1;
3743 prop = XCDR (prop);
3744 }
3745 }
3746 else if (VECTORP (prop))
3747 {
3748 /* A vector of sub-properties. */
3749 int i;
a61b7058
GM
3750 for (i = 0; i < ASIZE (prop); ++i)
3751 if (single_display_prop_intangible_p (AREF (prop, i)))
06568bbf
GM
3752 return 1;
3753 }
3754 else
3755 return single_display_prop_intangible_p (prop);
3756
3757 return 0;
3758}
3759
74bd6d65
GM
3760
3761/* Return 1 if PROP is a display sub-property value containing STRING. */
3762
3763static int
3764single_display_prop_string_p (prop, string)
3765 Lisp_Object prop, string;
3766{
74bd6d65
GM
3767 if (EQ (string, prop))
3768 return 1;
2311178e 3769
74bd6d65
GM
3770 /* Skip over `when FORM'. */
3771 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3772 {
3773 prop = XCDR (prop);
3774 if (!CONSP (prop))
3775 return 0;
3776 prop = XCDR (prop);
3777 }
3778
3779 if (CONSP (prop))
3780 /* Skip over `margin LOCATION'. */
3781 if (EQ (XCAR (prop), Qmargin))
3782 {
3783 prop = XCDR (prop);
3784 if (!CONSP (prop))
3785 return 0;
3786
3787 prop = XCDR (prop);
3788 if (!CONSP (prop))
3789 return 0;
3790 }
2311178e 3791
74bd6d65
GM
3792 return CONSP (prop) && EQ (XCAR (prop), string);
3793}
3794
3795
3796/* Return 1 if STRING appears in the `display' property PROP. */
3797
3798static int
3799display_prop_string_p (prop, string)
3800 Lisp_Object prop, string;
3801{
74bd6d65
GM
3802 if (CONSP (prop)
3803 && CONSP (XCAR (prop))
3804 && !EQ (Qmargin, XCAR (XCAR (prop))))
3805 {
3806 /* A list of sub-properties. */
3807 while (CONSP (prop))
3808 {
3809 if (single_display_prop_string_p (XCAR (prop), string))
3810 return 1;
3811 prop = XCDR (prop);
3812 }
3813 }
3814 else if (VECTORP (prop))
3815 {
3816 /* A vector of sub-properties. */
3817 int i;
3818 for (i = 0; i < ASIZE (prop); ++i)
3819 if (single_display_prop_string_p (AREF (prop, i), string))
3820 return 1;
3821 }
3822 else
3823 return single_display_prop_string_p (prop, string);
3824
3825 return 0;
3826}
3827
3828
3829/* Determine from which buffer position in W's buffer STRING comes
3830 from. AROUND_CHARPOS is an approximate position where it could
3831 be from. Value is the buffer position or 0 if it couldn't be
3832 determined.
3833
3834 W's buffer must be current.
3835
3836 This function is necessary because we don't record buffer positions
3837 in glyphs generated from strings (to keep struct glyph small).
3838 This function may only use code that doesn't eval because it is
3839 called asynchronously from note_mouse_highlight. */
3840
3841int
3842string_buffer_position (w, string, around_charpos)
3843 struct window *w;
3844 Lisp_Object string;
3845 int around_charpos;
3846{
74bd6d65
GM
3847 Lisp_Object limit, prop, pos;
3848 const int MAX_DISTANCE = 1000;
3849 int found = 0;
3850
d8731202 3851 pos = make_number (around_charpos);
74bd6d65
GM
3852 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3853 while (!found && !EQ (pos, limit))
3854 {
3855 prop = Fget_char_property (pos, Qdisplay, Qnil);
3856 if (!NILP (prop) && display_prop_string_p (prop, string))
3857 found = 1;
3858 else
134d9283 3859 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
74bd6d65
GM
3860 }
3861
3862 if (!found)
3863 {
d8731202 3864 pos = make_number (around_charpos);
74bd6d65
GM
3865 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3866 while (!found && !EQ (pos, limit))
3867 {
3868 prop = Fget_char_property (pos, Qdisplay, Qnil);
3869 if (!NILP (prop) && display_prop_string_p (prop, string))
3870 found = 1;
3871 else
134d9283
GM
3872 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3873 limit);
74bd6d65
GM
3874 }
3875 }
3876
3877 return found ? XINT (pos) : 0;
3878}
3879
3880
5f5c8ee5 3881\f
260a86a0
KH
3882/***********************************************************************
3883 `composition' property
3884 ***********************************************************************/
3885
3886/* Set up iterator IT from `composition' property at its current
3887 position. Called from handle_stop. */
3888
3889static enum prop_handled
3890handle_composition_prop (it)
3891 struct it *it;
3892{
3893 Lisp_Object prop, string;
3894 int pos, pos_byte, end;
3895 enum prop_handled handled = HANDLED_NORMALLY;
3896
3897 if (STRINGP (it->string))
3898 {
3899 pos = IT_STRING_CHARPOS (*it);
3900 pos_byte = IT_STRING_BYTEPOS (*it);
3901 string = it->string;
3902 }
3903 else
3904 {
3905 pos = IT_CHARPOS (*it);
3906 pos_byte = IT_BYTEPOS (*it);
3907 string = Qnil;
3908 }
3909
3910 /* If there's a valid composition and point is not inside of the
3911 composition (in the case that the composition is from the current
3912 buffer), draw a glyph composed from the composition components. */
3913 if (find_composition (pos, -1, &pos, &end, &prop, string)
3914 && COMPOSITION_VALID_P (pos, end, prop)
3915 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3916 {
3917 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3918
3919 if (id >= 0)
3920 {
3921 it->method = next_element_from_composition;
3922 it->cmp_id = id;
3923 it->cmp_len = COMPOSITION_LENGTH (prop);
3924 /* For a terminal, draw only the first character of the
3925 components. */
3926 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3927 it->len = (STRINGP (it->string)
3928 ? string_char_to_byte (it->string, end)
3929 : CHAR_TO_BYTE (end)) - pos_byte;
3930 it->stop_charpos = end;
3931 handled = HANDLED_RETURN;
3932 }
3933 }
3934
3935 return handled;
3936}
3937
3938
3939\f
5f5c8ee5
GM
3940/***********************************************************************
3941 Overlay strings
3942 ***********************************************************************/
3943
3944/* The following structure is used to record overlay strings for
3945 later sorting in load_overlay_strings. */
3946
3947struct overlay_entry
3948{
2970b9be 3949 Lisp_Object overlay;
5f5c8ee5
GM
3950 Lisp_Object string;
3951 int priority;
3952 int after_string_p;
3953};
3954
3955
3956/* Set up iterator IT from overlay strings at its current position.
3957 Called from handle_stop. */
3958
3959static enum prop_handled
3960handle_overlay_change (it)
3961 struct it *it;
3962{
5a08cbaf 3963 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
2970b9be 3964 return HANDLED_RECOMPUTE_PROPS;
5f5c8ee5 3965 else
2970b9be 3966 return HANDLED_NORMALLY;
5f5c8ee5
GM
3967}
3968
3969
3970/* Set up the next overlay string for delivery by IT, if there is an
3971 overlay string to deliver. Called by set_iterator_to_next when the
3972 end of the current overlay string is reached. If there are more
3973 overlay strings to display, IT->string and
3974 IT->current.overlay_string_index are set appropriately here.
3975 Otherwise IT->string is set to nil. */
2311178e 3976
5f5c8ee5
GM
3977static void
3978next_overlay_string (it)
3979 struct it *it;
3980{
3981 ++it->current.overlay_string_index;
3982 if (it->current.overlay_string_index == it->n_overlay_strings)
3983 {
3984 /* No more overlay strings. Restore IT's settings to what
3985 they were before overlay strings were processed, and
3986 continue to deliver from current_buffer. */
5a08cbaf 3987 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
2311178e 3988
5f5c8ee5
GM
3989 pop_it (it);
3990 xassert (it->stop_charpos >= BEGV
3991 && it->stop_charpos <= it->end_charpos);
3992 it->string = Qnil;
3993 it->current.overlay_string_index = -1;
3994 SET_TEXT_POS (it->current.string_pos, -1, -1);
3995 it->n_overlay_strings = 0;
3996 it->method = next_element_from_buffer;
2970b9be
GM
3997
3998 /* If we're at the end of the buffer, record that we have
3999 processed the overlay strings there already, so that
4000 next_element_from_buffer doesn't try it again. */
4001 if (IT_CHARPOS (*it) >= it->end_charpos)
4002 it->overlay_strings_at_end_processed_p = 1;
5a08cbaf
GM
4003
4004 /* If we have to display `...' for invisible text, set
4005 the iterator up for that. */
4006 if (display_ellipsis_p)
4007 setup_for_ellipsis (it);
5f5c8ee5
GM
4008 }
4009 else
4010 {
4011 /* There are more overlay strings to process. If
4012 IT->current.overlay_string_index has advanced to a position
4013 where we must load IT->overlay_strings with more strings, do
4014 it. */
4015 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
2311178e 4016
5f5c8ee5 4017 if (it->current.overlay_string_index && i == 0)
5a08cbaf 4018 load_overlay_strings (it, 0);
5f5c8ee5
GM
4019
4020 /* Initialize IT to deliver display elements from the overlay
4021 string. */
4022 it->string = it->overlay_strings[i];
4023 it->multibyte_p = STRING_MULTIBYTE (it->string);
4024 SET_TEXT_POS (it->current.string_pos, 0, 0);
4025 it->method = next_element_from_string;
4026 it->stop_charpos = 0;
4027 }
2311178e 4028
5f5c8ee5
GM
4029 CHECK_IT (it);
4030}
4031
4032
4033/* Compare two overlay_entry structures E1 and E2. Used as a
4034 comparison function for qsort in load_overlay_strings. Overlay
4035 strings for the same position are sorted so that
4036
2970b9be
GM
4037 1. All after-strings come in front of before-strings, except
4038 when they come from the same overlay.
2311178e 4039
5f5c8ee5
GM
4040 2. Within after-strings, strings are sorted so that overlay strings
4041 from overlays with higher priorities come first.
4042
4043 2. Within before-strings, strings are sorted so that overlay
4044 strings from overlays with higher priorities come last.
4045
4046 Value is analogous to strcmp. */
4047
2311178e 4048
5f5c8ee5
GM
4049static int
4050compare_overlay_entries (e1, e2)
4051 void *e1, *e2;
4052{
4053 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4054 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4055 int result;
4056
4057 if (entry1->after_string_p != entry2->after_string_p)
2970b9be
GM
4058 {
4059 /* Let after-strings appear in front of before-strings if
4060 they come from different overlays. */
4061 if (EQ (entry1->overlay, entry2->overlay))
4062 result = entry1->after_string_p ? 1 : -1;
4063 else
4064 result = entry1->after_string_p ? -1 : 1;
4065 }
5f5c8ee5
GM
4066 else if (entry1->after_string_p)
4067 /* After-strings sorted in order of decreasing priority. */
4068 result = entry2->priority - entry1->priority;
4069 else
4070 /* Before-strings sorted in order of increasing priority. */
4071 result = entry1->priority - entry2->priority;
4072
4073 return result;
4074}
4075
4076
4077/* Load the vector IT->overlay_strings with overlay strings from IT's
5a08cbaf
GM
4078 current buffer position, or from CHARPOS if that is > 0. Set
4079 IT->n_overlays to the total number of overlay strings found.
5f5c8ee5
GM
4080
4081 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4082 a time. On entry into load_overlay_strings,
4083 IT->current.overlay_string_index gives the number of overlay
4084 strings that have already been loaded by previous calls to this
4085 function.
4086
2970b9be
GM
4087 IT->add_overlay_start contains an additional overlay start
4088 position to consider for taking overlay strings from, if non-zero.
4089 This position comes into play when the overlay has an `invisible'
4090 property, and both before and after-strings. When we've skipped to
4091 the end of the overlay, because of its `invisible' property, we
4092 nevertheless want its before-string to appear.
4093 IT->add_overlay_start will contain the overlay start position
4094 in this case.
4095
5f5c8ee5
GM
4096 Overlay strings are sorted so that after-string strings come in
4097 front of before-string strings. Within before and after-strings,
4098 strings are sorted by overlay priority. See also function
4099 compare_overlay_entries. */
2311178e 4100
5f5c8ee5 4101static void
5a08cbaf 4102load_overlay_strings (it, charpos)
5f5c8ee5 4103 struct it *it;
5a08cbaf 4104 int charpos;
5f5c8ee5
GM
4105{
4106 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
b7253a3e
SM
4107 Lisp_Object overlay, window, str, invisible;
4108 struct Lisp_Overlay *ov;
5f5c8ee5
GM
4109 int start, end;
4110 int size = 20;
cafafe0b 4111 int n = 0, i, j, invis_p;
5f5c8ee5
GM
4112 struct overlay_entry *entries
4113 = (struct overlay_entry *) alloca (size * sizeof *entries);
5a08cbaf
GM
4114
4115 if (charpos <= 0)
4116 charpos = IT_CHARPOS (*it);
5f5c8ee5
GM
4117
4118 /* Append the overlay string STRING of overlay OVERLAY to vector
4119 `entries' which has size `size' and currently contains `n'
4120 elements. AFTER_P non-zero means STRING is an after-string of
4121 OVERLAY. */
4122#define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4123 do \
4124 { \
4125 Lisp_Object priority; \
4126 \
4127 if (n == size) \
4128 { \
4129 int new_size = 2 * size; \
4130 struct overlay_entry *old = entries; \
4131 entries = \
4132 (struct overlay_entry *) alloca (new_size \
4133 * sizeof *entries); \
4134 bcopy (old, entries, size * sizeof *entries); \
4135 size = new_size; \
4136 } \
4137 \
4138 entries[n].string = (STRING); \
2970b9be 4139 entries[n].overlay = (OVERLAY); \
5f5c8ee5 4140 priority = Foverlay_get ((OVERLAY), Qpriority); \
2970b9be 4141 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5f5c8ee5
GM
4142 entries[n].after_string_p = (AFTER_P); \
4143 ++n; \
4144 } \
4145 while (0)
4146
4147 /* Process overlay before the overlay center. */
b7253a3e 4148 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5f5c8ee5 4149 {
b7253a3e 4150 XSETMISC (overlay, ov);
5f5c8ee5
GM
4151 xassert (OVERLAYP (overlay));
4152 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4153 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2311178e 4154
cafafe0b 4155 if (end < charpos)
5f5c8ee5
GM
4156 break;
4157
4158 /* Skip this overlay if it doesn't start or end at IT's current
4159 position. */
cafafe0b 4160 if (end != charpos && start != charpos)
5f5c8ee5 4161 continue;
2311178e 4162
5f5c8ee5
GM
4163 /* Skip this overlay if it doesn't apply to IT->w. */
4164 window = Foverlay_get (overlay, Qwindow);
4165 if (WINDOWP (window) && XWINDOW (window) != it->w)
4166 continue;
4167
cafafe0b
GM
4168 /* If the text ``under'' the overlay is invisible, both before-
4169 and after-strings from this overlay are visible; start and
4170 end position are indistinguishable. */
4171 invisible = Foverlay_get (overlay, Qinvisible);
4172 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4173
5f5c8ee5 4174 /* If overlay has a non-empty before-string, record it. */
cafafe0b 4175 if ((start == charpos || (end == charpos && invis_p))
5f5c8ee5 4176 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2051c264 4177 && SCHARS (str))
5f5c8ee5 4178 RECORD_OVERLAY_STRING (overlay, str, 0);
2311178e 4179
5f5c8ee5 4180 /* If overlay has a non-empty after-string, record it. */
cafafe0b 4181 if ((end == charpos || (start == charpos && invis_p))
5f5c8ee5 4182 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2051c264 4183 && SCHARS (str))
5f5c8ee5
GM
4184 RECORD_OVERLAY_STRING (overlay, str, 1);
4185 }
2311178e 4186
5f5c8ee5 4187 /* Process overlays after the overlay center. */
b7253a3e 4188 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5f5c8ee5 4189 {
b7253a3e 4190 XSETMISC (overlay, ov);
5f5c8ee5
GM
4191 xassert (OVERLAYP (overlay));
4192 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4193 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4194
cafafe0b 4195 if (start > charpos)
5f5c8ee5 4196 break;
2311178e 4197
5f5c8ee5
GM
4198 /* Skip this overlay if it doesn't start or end at IT's current
4199 position. */
cafafe0b 4200 if (end != charpos && start != charpos)
5f5c8ee5
GM
4201 continue;
4202
4203 /* Skip this overlay if it doesn't apply to IT->w. */
4204 window = Foverlay_get (overlay, Qwindow);
4205 if (WINDOWP (window) && XWINDOW (window) != it->w)
4206 continue;
2311178e 4207
cafafe0b
GM
4208 /* If the text ``under'' the overlay is invisible, it has a zero
4209 dimension, and both before- and after-strings apply. */
4210 invisible = Foverlay_get (overlay, Qinvisible);
4211 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4212
5f5c8ee5 4213 /* If overlay has a non-empty before-string, record it. */
cafafe0b 4214 if ((start == charpos || (end == charpos && invis_p))
5f5c8ee5 4215 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2051c264 4216 && SCHARS (str))
5f5c8ee5 4217 RECORD_OVERLAY_STRING (overlay, str, 0);
2311178e 4218
5f5c8ee5 4219 /* If overlay has a non-empty after-string, record it. */
cafafe0b 4220 if ((end == charpos || (start == charpos && invis_p))
5f5c8ee5 4221 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2051c264 4222 && SCHARS (str))
5f5c8ee5
GM
4223 RECORD_OVERLAY_STRING (overlay, str, 1);
4224 }
4225
4226#undef RECORD_OVERLAY_STRING
2311178e 4227
5f5c8ee5 4228 /* Sort entries. */
cafafe0b 4229 if (n > 1)
2970b9be 4230 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5f5c8ee5
GM
4231
4232 /* Record the total number of strings to process. */
4233 it->n_overlay_strings = n;
4234
4235 /* IT->current.overlay_string_index is the number of overlay strings
4236 that have already been consumed by IT. Copy some of the
4237 remaining overlay strings to IT->overlay_strings. */
4238 i = 0;
4239 j = it->current.overlay_string_index;
4240 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4241 it->overlay_strings[i++] = entries[j++].string;
2970b9be 4242
5f5c8ee5
GM
4243 CHECK_IT (it);
4244}
4245
4246
4247/* Get the first chunk of overlay strings at IT's current buffer
5a08cbaf
GM
4248 position, or at CHARPOS if that is > 0. Value is non-zero if at
4249 least one overlay string was found. */
5f5c8ee5
GM
4250
4251static int
5a08cbaf 4252get_overlay_strings (it, charpos)
5f5c8ee5 4253 struct it *it;
5a08cbaf 4254 int charpos;
5f5c8ee5
GM
4255{
4256 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4257 process. This fills IT->overlay_strings with strings, and sets
4258 IT->n_overlay_strings to the total number of strings to process.
4259 IT->pos.overlay_string_index has to be set temporarily to zero
4260 because load_overlay_strings needs this; it must be set to -1
4261 when no overlay strings are found because a zero value would
4262 indicate a position in the first overlay string. */
4263 it->current.overlay_string_index = 0;
5a08cbaf 4264 load_overlay_strings (it, charpos);
5f5c8ee5
GM
4265
4266 /* If we found overlay strings, set up IT to deliver display
4267 elements from the first one. Otherwise set up IT to deliver
4268 from current_buffer. */
4269 if (it->n_overlay_strings)
4270 {
4271 /* Make sure we know settings in current_buffer, so that we can
4272 restore meaningful values when we're done with the overlay
4273 strings. */
4274 compute_stop_pos (it);
4275 xassert (it->face_id >= 0);
2311178e 4276
5f5c8ee5
GM
4277 /* Save IT's settings. They are restored after all overlay
4278 strings have been processed. */
4279 xassert (it->sp == 0);
4280 push_it (it);
4281
4282 /* Set up IT to deliver display elements from the first overlay
4283 string. */
4284 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5f5c8ee5 4285 it->string = it->overlay_strings[0];
b2046df8 4286 it->stop_charpos = 0;
5f5c8ee5 4287 xassert (STRINGP (it->string));
2051c264 4288 it->end_charpos = SCHARS (it->string);
d5db4077 4289 it->multibyte_p = STRING_MULTIBYTE (it->string);
5f5c8ee5
GM
4290 it->method = next_element_from_string;
4291 }
4292 else
4293 {
4294 it->string = Qnil;
4295 it->current.overlay_string_index = -1;
4296 it->method = next_element_from_buffer;
4297 }
4298
4299 CHECK_IT (it);
4300
4301 /* Value is non-zero if we found at least one overlay string. */
4302 return STRINGP (it->string);
4303}
4304
4305
4306\f
4307/***********************************************************************
4308 Saving and restoring state
4309 ***********************************************************************/
4310
4311/* Save current settings of IT on IT->stack. Called, for example,
4312 before setting up IT for an overlay string, to be able to restore
4313 IT's settings to what they were after the overlay string has been
4314 processed. */
4315
4316static void
4317push_it (it)
4318 struct it *it;
4319{
4320 struct iterator_stack_entry *p;
2311178e 4321
5f5c8ee5
GM
4322 xassert (it->sp < 2);
4323 p = it->stack + it->sp;
4324
4325 p->stop_charpos = it->stop_charpos;
4326 xassert (it->face_id >= 0);
4327 p->face_id = it->face_id;
4328 p->string = it->string;
4329 p->pos = it->current;
4330 p->end_charpos = it->end_charpos;
4331 p->string_nchars = it->string_nchars;
4332 p->area = it->area;
4333 p->multibyte_p = it->multibyte_p;
4334 p->space_width = it->space_width;
4335 p->font_height = it->font_height;
4336 p->voffset = it->voffset;
4337 p->string_from_display_prop_p = it->string_from_display_prop_p;
5a08cbaf 4338 p->display_ellipsis_p = 0;
5f5c8ee5
GM
4339 ++it->sp;
4340}
4341
4342
4343/* Restore IT's settings from IT->stack. Called, for example, when no
4344 more overlay strings must be processed, and we return to delivering
4345 display elements from a buffer, or when the end of a string from a
4346 `display' property is reached and we return to delivering display
4347 elements from an overlay string, or from a buffer. */
4348
4349static void
4350pop_it (it)
4351 struct it *it;
4352{
4353 struct iterator_stack_entry *p;
2311178e 4354
5f5c8ee5
GM
4355 xassert (it->sp > 0);
4356 --it->sp;
4357 p = it->stack + it->sp;
4358 it->stop_charpos = p->stop_charpos;
4359 it->face_id = p->face_id;
4360 it->string = p->string;
4361 it->current = p->pos;
4362 it->end_charpos = p->end_charpos;
4363 it->string_nchars = p->string_nchars;
4364 it->area = p->area;
4365 it->multibyte_p = p->multibyte_p;
4366 it->space_width = p->space_width;
4367 it->font_height = p->font_height;
4368 it->voffset = p->voffset;
4369 it->string_from_display_prop_p = p->string_from_display_prop_p;
4370}
4371
4372
4373\f
4374/***********************************************************************
4375 Moving over lines
4376 ***********************************************************************/
4377
4378/* Set IT's current position to the previous line start. */
4379
4380static void
4381back_to_previous_line_start (it)
4382 struct it *it;
4383{
4384 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4385 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4386}
4387
4388
cafafe0b 4389/* Move IT to the next line start.
2311178e 4390
cafafe0b
GM
4391 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4392 we skipped over part of the text (as opposed to moving the iterator
4393 continuously over the text). Otherwise, don't change the value
4394 of *SKIPPED_P.
2311178e 4395
cafafe0b
GM
4396 Newlines may come from buffer text, overlay strings, or strings
4397 displayed via the `display' property. That's the reason we can't
0fd37545
GM
4398 simply use find_next_newline_no_quit.
4399
4400 Note that this function may not skip over invisible text that is so
4401 because of text properties and immediately follows a newline. If
4402 it would, function reseat_at_next_visible_line_start, when called
4403 from set_iterator_to_next, would effectively make invisible
4404 characters following a newline part of the wrong glyph row, which
4405 leads to wrong cursor motion. */
5f5c8ee5 4406
cafafe0b
GM
4407static int
4408forward_to_next_line_start (it, skipped_p)
5f5c8ee5 4409 struct it *it;
cafafe0b 4410 int *skipped_p;
5f5c8ee5 4411{
54918e2b 4412 int old_selective, newline_found_p, n;
cafafe0b
GM
4413 const int MAX_NEWLINE_DISTANCE = 500;
4414
0fd37545
GM
4415 /* If already on a newline, just consume it to avoid unintended
4416 skipping over invisible text below. */
51695746
GM
4417 if (it->what == IT_CHARACTER
4418 && it->c == '\n'
4419 && CHARPOS (it->position) == IT_CHARPOS (*it))
0fd37545
GM
4420 {
4421 set_iterator_to_next (it, 0);
a77dc1ec 4422 it->c = 0;
0fd37545
GM
4423 return 1;
4424 }
4425
54918e2b 4426 /* Don't handle selective display in the following. It's (a)
0fd37545
GM
4427 unnecessary because it's done by the caller, and (b) leads to an
4428 infinite recursion because next_element_from_ellipsis indirectly
4429 calls this function. */
54918e2b
GM
4430 old_selective = it->selective;
4431 it->selective = 0;
4432
cafafe0b
GM
4433 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4434 from buffer text. */
3aec8722
GM
4435 for (n = newline_found_p = 0;
4436 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4437 n += STRINGP (it->string) ? 0 : 1)
cafafe0b 4438 {
8692ca92 4439 if (!get_next_display_element (it))
d43be70c 4440 return 0;
d02f1cb8 4441 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
cafafe0b 4442 set_iterator_to_next (it, 0);
cafafe0b
GM
4443 }
4444
4445 /* If we didn't find a newline near enough, see if we can use a
4446 short-cut. */
db0bb807 4447 if (!newline_found_p)
cafafe0b
GM
4448 {
4449 int start = IT_CHARPOS (*it);
4450 int limit = find_next_newline_no_quit (start, 1);
4451 Lisp_Object pos;
4452
4453 xassert (!STRINGP (it->string));
4454
4455 /* If there isn't any `display' property in sight, and no
4456 overlays, we can just use the position of the newline in
4457 buffer text. */
4458 if (it->stop_charpos >= limit
4459 || ((pos = Fnext_single_property_change (make_number (start),
4460 Qdisplay,
4461 Qnil, make_number (limit)),
4462 NILP (pos))
4463 && next_overlay_change (start) == ZV))
4464 {
4465 IT_CHARPOS (*it) = limit;
4466 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4467 *skipped_p = newline_found_p = 1;
4468 }
4469 else
4470 {
4471 while (get_next_display_element (it)
4472 && !newline_found_p)
4473 {
4474 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4475 set_iterator_to_next (it, 0);
4476 }
4477 }
4478 }
4479
54918e2b 4480 it->selective = old_selective;
cafafe0b 4481 return newline_found_p;
5f5c8ee5
GM
4482}
4483
4484
4485/* Set IT's current position to the previous visible line start. Skip
4486 invisible text that is so either due to text properties or due to
4487 selective display. Caution: this does not change IT->current_x and
4488 IT->hpos. */
4489
4490static void
4491back_to_previous_visible_line_start (it)
4492 struct it *it;
4493{
4494 int visible_p = 0;
4495
4496 /* Go back one newline if not on BEGV already. */
4497 if (IT_CHARPOS (*it) > BEGV)
4498 back_to_previous_line_start (it);
4499
4500 /* Move over lines that are invisible because of selective display
4501 or text properties. */
4502 while (IT_CHARPOS (*it) > BEGV
4503 && !visible_p)
4504 {
4505 visible_p = 1;
4506
4507 /* If selective > 0, then lines indented more than that values
4508 are invisible. */
4509 if (it->selective > 0
4510 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
a67e162b 4511 (double) it->selective)) /* iftc */
5f5c8ee5 4512 visible_p = 0;
2311178e 4513 else
5f5c8ee5
GM
4514 {
4515 Lisp_Object prop;
4516
6fc556fd
KR
4517 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
4518 Qinvisible, it->window);
5f5c8ee5
GM
4519 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4520 visible_p = 0;
4521 }
5f5c8ee5
GM
4522
4523 /* Back one more newline if the current one is invisible. */
4524 if (!visible_p)
4525 back_to_previous_line_start (it);
4526 }
4527
4528 xassert (IT_CHARPOS (*it) >= BEGV);
4529 xassert (IT_CHARPOS (*it) == BEGV
4530 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4531 CHECK_IT (it);
4532}
4533
4534
4535/* Reseat iterator IT at the previous visible line start. Skip
4536 invisible text that is so either due to text properties or due to
4537 selective display. At the end, update IT's overlay information,
4538 face information etc. */
4539
4540static void
4541reseat_at_previous_visible_line_start (it)
4542 struct it *it;
4543{
4544 back_to_previous_visible_line_start (it);
4545 reseat (it, it->current.pos, 1);
4546 CHECK_IT (it);
4547}
4548
4549
4550/* Reseat iterator IT on the next visible line start in the current
312246d1
GM
4551 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4552 preceding the line start. Skip over invisible text that is so
4553 because of selective display. Compute faces, overlays etc at the
4554 new position. Note that this function does not skip over text that
4555 is invisible because of text properties. */
5f5c8ee5
GM
4556
4557static void
312246d1 4558reseat_at_next_visible_line_start (it, on_newline_p)
5f5c8ee5 4559 struct it *it;
312246d1 4560 int on_newline_p;
5f5c8ee5 4561{
cafafe0b
GM
4562 int newline_found_p, skipped_p = 0;
4563
4564 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4565
4566 /* Skip over lines that are invisible because they are indented
4567 more than the value of IT->selective. */
4568 if (it->selective > 0)
4569 while (IT_CHARPOS (*it) < ZV
a77dc1ec 4570 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
a67e162b 4571 (double) it->selective)) /* iftc */
a77dc1ec
GM
4572 {
4573 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4574 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4575 }
cafafe0b
GM
4576
4577 /* Position on the newline if that's what's requested. */
4578 if (on_newline_p && newline_found_p)
5f5c8ee5 4579 {
cafafe0b 4580 if (STRINGP (it->string))
5f5c8ee5 4581 {
cafafe0b
GM
4582 if (IT_STRING_CHARPOS (*it) > 0)
4583 {
4584 --IT_STRING_CHARPOS (*it);
4585 --IT_STRING_BYTEPOS (*it);
4586 }
5f5c8ee5 4587 }
cafafe0b 4588 else if (IT_CHARPOS (*it) > BEGV)
312246d1
GM
4589 {
4590 --IT_CHARPOS (*it);
cafafe0b
GM
4591 --IT_BYTEPOS (*it);
4592 reseat (it, it->current.pos, 0);
312246d1 4593 }
5f5c8ee5 4594 }
cafafe0b
GM
4595 else if (skipped_p)
4596 reseat (it, it->current.pos, 0);
2311178e 4597
5f5c8ee5
GM
4598 CHECK_IT (it);
4599}
4600
4601
4602\f
4603/***********************************************************************
4604 Changing an iterator's position
4605***********************************************************************/
4606
4607/* Change IT's current position to POS in current_buffer. If FORCE_P
4608 is non-zero, always check for text properties at the new position.
4609 Otherwise, text properties are only looked up if POS >=
4610 IT->check_charpos of a property. */
4611
4612static void
4613reseat (it, pos, force_p)
4614 struct it *it;
4615 struct text_pos pos;
4616 int force_p;
4617{
4618 int original_pos = IT_CHARPOS (*it);
4619
4620 reseat_1 (it, pos, 0);
4621
4622 /* Determine where to check text properties. Avoid doing it
4623 where possible because text property lookup is very expensive. */
4624 if (force_p
4625 || CHARPOS (pos) > it->stop_charpos
4626 || CHARPOS (pos) < original_pos)
4627 handle_stop (it);
4628
4629 CHECK_IT (it);
4630}
4631
4632
4633/* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4634 IT->stop_pos to POS, also. */
4635
4636static void
4637reseat_1 (it, pos, set_stop_p)
4638 struct it *it;
4639 struct text_pos pos;
4640 int set_stop_p;
4641{
4642 /* Don't call this function when scanning a C string. */
4643 xassert (it->s == NULL);
4644
4645 /* POS must be a reasonable value. */
4646 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4647
4648 it->current.pos = it->position = pos;
4649 XSETBUFFER (it->object, current_buffer);
78c663d8 4650 it->end_charpos = ZV;
5f5c8ee5
GM
4651 it->dpvec = NULL;
4652 it->current.dpvec_index = -1;
4653 it->current.overlay_string_index = -1;
4654 IT_STRING_CHARPOS (*it) = -1;
4655 IT_STRING_BYTEPOS (*it) = -1;
4656 it->string = Qnil;
4657 it->method = next_element_from_buffer;
5905025c
RS
4658 /* RMS: I added this to fix a bug in move_it_vertically_backward
4659 where it->area continued to relate to the starting point
4660 for the backward motion. Bug report from
4661 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4662 However, I am not sure whether reseat still does the right thing
4663 in general after this change. */
4664 it->area = TEXT_AREA;
06fd3792 4665 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5f5c8ee5 4666 it->sp = 0;
4aad61f8 4667 it->face_before_selective_p = 0;
5f5c8ee5
GM
4668
4669 if (set_stop_p)
4670 it->stop_charpos = CHARPOS (pos);
4671}
4672
4673
4674/* Set up IT for displaying a string, starting at CHARPOS in window W.
4675 If S is non-null, it is a C string to iterate over. Otherwise,
4676 STRING gives a Lisp string to iterate over.
2311178e 4677
5f5c8ee5
GM
4678 If PRECISION > 0, don't return more then PRECISION number of
4679 characters from the string.
4680
4681 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4682 characters have been returned. FIELD_WIDTH < 0 means an infinite
4683 field width.
4684
4685 MULTIBYTE = 0 means disable processing of multibyte characters,
4686 MULTIBYTE > 0 means enable it,
4687 MULTIBYTE < 0 means use IT->multibyte_p.
4688
4689 IT must be initialized via a prior call to init_iterator before
4690 calling this function. */
4691
4692static void
4693reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4694 struct it *it;
4695 unsigned char *s;
4696 Lisp_Object string;
4697 int charpos;
4698 int precision, field_width, multibyte;
4699{
4700 /* No region in strings. */
4701 it->region_beg_charpos = it->region_end_charpos = -1;
4702
4703 /* No text property checks performed by default, but see below. */
4704 it->stop_charpos = -1;
4705
4706 /* Set iterator position and end position. */
4707 bzero (&it->current, sizeof it->current);
4708 it->current.overlay_string_index = -1;
4709 it->current.dpvec_index = -1;
5f5c8ee5 4710 xassert (charpos >= 0);
2311178e 4711
e719f5ae
GM
4712 /* If STRING is specified, use its multibyteness, otherwise use the
4713 setting of MULTIBYTE, if specified. */
cabf45da 4714 if (multibyte >= 0)
5f5c8ee5 4715 it->multibyte_p = multibyte > 0;
2311178e 4716
5f5c8ee5
GM
4717 if (s == NULL)
4718 {
4719 xassert (STRINGP (string));
4720 it->string = string;
4721 it->s = NULL;
2051c264 4722 it->end_charpos = it->string_nchars = SCHARS (string);
5f5c8ee5
GM
4723 it->method = next_element_from_string;
4724 it->current.string_pos = string_pos (charpos, string);
4725 }
4726 else
4727 {
4728 it->s = s;
4729 it->string = Qnil;
4730
4731 /* Note that we use IT->current.pos, not it->current.string_pos,
4732 for displaying C strings. */
4733 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4734 if (it->multibyte_p)
4735 {
4736 it->current.pos = c_string_pos (charpos, s, 1);
4737 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4738 }
4739 else
4740 {
4741 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4742 it->end_charpos = it->string_nchars = strlen (s);
4743 }
2311178e 4744
5f5c8ee5
GM
4745 it->method = next_element_from_c_string;
4746 }
4747
4748 /* PRECISION > 0 means don't return more than PRECISION characters
4749 from the string. */
4750 if (precision > 0 && it->end_charpos - charpos > precision)
4751 it->end_charpos = it->string_nchars = charpos + precision;
4752
4753 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4754 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4755 FIELD_WIDTH < 0 means infinite field width. This is useful for
4756 padding with `-' at the end of a mode line. */
4757 if (field_width < 0)
4758 field_width = INFINITY;
4759 if (field_width > it->end_charpos - charpos)
4760 it->end_charpos = charpos + field_width;
4761
4762 /* Use the standard display table for displaying strings. */
4763 if (DISP_TABLE_P (Vstandard_display_table))
4764 it->dp = XCHAR_TABLE (Vstandard_display_table);
4765
4766 it->stop_charpos = charpos;
4767 CHECK_IT (it);
4768}
4769
4770
4771\f
4772/***********************************************************************
4773 Iteration
4774 ***********************************************************************/
4775
4776/* Load IT's display element fields with information about the next
4777 display element from the current position of IT. Value is zero if
4778 end of buffer (or C string) is reached. */
4779
4780int
4781get_next_display_element (it)
4782 struct it *it;
4783{
7d0393cf 4784 /* Non-zero means that we found a display element. Zero means that
5f5c8ee5
GM
4785 we hit the end of what we iterate over. Performance note: the
4786 function pointer `method' used here turns out to be faster than
4787 using a sequence of if-statements. */
4788 int success_p = (*it->method) (it);
5f5c8ee5
GM
4789
4790 if (it->what == IT_CHARACTER)
4791 {
4792 /* Map via display table or translate control characters.
4793 IT->c, IT->len etc. have been set to the next character by
4794 the function call above. If we have a display table, and it
4795 contains an entry for IT->c, translate it. Don't do this if
4796 IT->c itself comes from a display table, otherwise we could
4797 end up in an infinite recursion. (An alternative could be to
4798 count the recursion depth of this function and signal an
4799 error when a certain maximum depth is reached.) Is it worth
4800 it? */
4801 if (success_p && it->dpvec == NULL)
4802 {
4803 Lisp_Object dv;
4804
4805 if (it->dp
4806 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4807 VECTORP (dv)))
4808 {
4809 struct Lisp_Vector *v = XVECTOR (dv);
4810
4811 /* Return the first character from the display table
4812 entry, if not empty. If empty, don't display the
4813 current character. */
4814 if (v->size)
4815 {
4816 it->dpvec_char_len = it->len;
4817 it->dpvec = v->contents;
4818 it->dpend = v->contents + v->size;
4819 it->current.dpvec_index = 0;
4820 it->method = next_element_from_display_vector;
7eee47cc
GM
4821 success_p = get_next_display_element (it);
4822 }
4823 else
4824 {
4825 set_iterator_to_next (it, 0);
4826 success_p = get_next_display_element (it);
5f5c8ee5 4827 }
5f5c8ee5
GM
4828 }
4829
4830 /* Translate control characters into `\003' or `^C' form.
4831 Control characters coming from a display table entry are
4832 currently not translated because we use IT->dpvec to hold
4833 the translation. This could easily be changed but I
197516c2
KH
4834 don't believe that it is worth doing.
4835
fa831cf8
KH
4836 If it->multibyte_p is nonzero, eight-bit characters and
4837 non-printable multibyte characters are also translated to
4838 octal form.
4839
4840 If it->multibyte_p is zero, eight-bit characters that
4841 don't have corresponding multibyte char code are also
4842 translated to octal form. */
ba197fe6 4843 else if ((it->c < ' '
5f5c8ee5 4844 && (it->area != TEXT_AREA
c6e89d6c 4845 || (it->c != '\n' && it->c != '\t')))
fa831cf8
KH
4846 || (it->multibyte_p
4847 ? ((it->c >= 127
4848 && it->len == 1)
4849 || !CHAR_PRINTABLE_P (it->c))
ba197fe6 4850 : (it->c >= 127
fa831cf8 4851 && it->c == unibyte_char_to_multibyte (it->c))))
5f5c8ee5
GM
4852 {
4853 /* IT->c is a control character which must be displayed
4854 either as '\003' or as `^C' where the '\\' and '^'
4855 can be defined in the display table. Fill
4856 IT->ctl_chars with glyphs for what we have to
4857 display. Then, set IT->dpvec to these glyphs. */
4858 GLYPH g;
4859
54c85a23 4860 if (it->c < 128 && it->ctl_arrow_p)
5f5c8ee5
GM
4861 {
4862 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4863 if (it->dp
4864 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4865 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4866 g = XINT (DISP_CTRL_GLYPH (it->dp));
4867 else
4868 g = FAST_MAKE_GLYPH ('^', 0);
4869 XSETINT (it->ctl_chars[0], g);
4870
4871 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4872 XSETINT (it->ctl_chars[1], g);
4873
4874 /* Set up IT->dpvec and return first character from it. */
4875 it->dpvec_char_len = it->len;
4876 it->dpvec = it->ctl_chars;
4877 it->dpend = it->dpvec + 2;
4878 it->current.dpvec_index = 0;
4879 it->method = next_element_from_display_vector;
4880 get_next_display_element (it);
4881 }
4882 else
4883 {
260a86a0 4884 unsigned char str[MAX_MULTIBYTE_LENGTH];
c5924f47 4885 int len;
197516c2
KH
4886 int i;
4887 GLYPH escape_glyph;
4888
5f5c8ee5
GM
4889 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4890 if (it->dp
4891 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4892 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
197516c2 4893 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5f5c8ee5 4894 else
197516c2
KH
4895 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4896
c5924f47
KH
4897 if (SINGLE_BYTE_CHAR_P (it->c))
4898 str[0] = it->c, len = 1;
4899 else
ea9dd091
GM
4900 {
4901 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4902 if (len < 0)
4903 {
4904 /* It's an invalid character, which
4905 shouldn't happen actually, but due to
4906 bugs it may happen. Let's print the char
4907 as is, there's not much meaningful we can
4908 do with it. */
4909 str[0] = it->c;
4910 str[1] = it->c >> 8;
4911 str[2] = it->c >> 16;
4912 str[3] = it->c >> 24;
4913 len = 4;
4914 }
4915 }
c5924f47 4916
197516c2
KH
4917 for (i = 0; i < len; i++)
4918 {
4919 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4920 /* Insert three more glyphs into IT->ctl_chars for
4921 the octal display of the character. */
2311178e 4922 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
197516c2 4923 XSETINT (it->ctl_chars[i * 4 + 1], g);
2311178e 4924 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
197516c2 4925 XSETINT (it->ctl_chars[i * 4 + 2], g);
2311178e 4926 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
197516c2
KH
4927 XSETINT (it->ctl_chars[i * 4 + 3], g);
4928 }
5f5c8ee5
GM
4929
4930 /* Set up IT->dpvec and return the first character
4931 from it. */
4932 it->dpvec_char_len = it->len;
4933 it->dpvec = it->ctl_chars;
197516c2 4934 it->dpend = it->dpvec + len * 4;
5f5c8ee5
GM
4935 it->current.dpvec_index = 0;
4936 it->method = next_element_from_display_vector;
4937 get_next_display_element (it);
4938 }
4939 }
4940 }
4941
980806b6
KH
4942 /* Adjust face id for a multibyte character. There are no
4943 multibyte character in unibyte text. */
5f5c8ee5
GM
4944 if (it->multibyte_p
4945 && success_p
980806b6 4946 && FRAME_WINDOW_P (it->f))
5f5c8ee5 4947 {
980806b6
KH
4948 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4949 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5f5c8ee5
GM
4950 }
4951 }
4952
4953 /* Is this character the last one of a run of characters with
4954 box? If yes, set IT->end_of_box_run_p to 1. */
4955 if (it->face_box_p
4956 && it->s == NULL)
4957 {
4958 int face_id;
4959 struct face *face;
4960
4961 it->end_of_box_run_p
4962 = ((face_id = face_after_it_pos (it),
4963 face_id != it->face_id)
4964 && (face = FACE_FROM_ID (it->f, face_id),
4965 face->box == FACE_NO_BOX));
4966 }
4967
4968 /* Value is 0 if end of buffer or string reached. */
4969 return success_p;
4970}
4971
4972
4973/* Move IT to the next display element.
4974
cafafe0b
GM
4975 RESEAT_P non-zero means if called on a newline in buffer text,
4976 skip to the next visible line start.
4977
5f5c8ee5
GM
4978 Functions get_next_display_element and set_iterator_to_next are
4979 separate because I find this arrangement easier to handle than a
4980 get_next_display_element function that also increments IT's
4981 position. The way it is we can first look at an iterator's current
4982 display element, decide whether it fits on a line, and if it does,
4983 increment the iterator position. The other way around we probably
4984 would either need a flag indicating whether the iterator has to be
4985 incremented the next time, or we would have to implement a
4986 decrement position function which would not be easy to write. */
4987
4988void
cafafe0b 4989set_iterator_to_next (it, reseat_p)
5f5c8ee5 4990 struct it *it;
cafafe0b 4991 int reseat_p;
5f5c8ee5 4992{
483de32b
GM
4993 /* Reset flags indicating start and end of a sequence of characters
4994 with box. Reset them at the start of this function because
4995 moving the iterator to a new position might set them. */
4996 it->start_of_box_run_p = it->end_of_box_run_p = 0;
2311178e 4997
5f5c8ee5
GM
4998 if (it->method == next_element_from_buffer)
4999 {
5000 /* The current display element of IT is a character from
5001 current_buffer. Advance in the buffer, and maybe skip over
5002 invisible lines that are so because of selective display. */
cafafe0b 5003 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
312246d1 5004 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
5005 else
5006 {
5007 xassert (it->len != 0);
5008 IT_BYTEPOS (*it) += it->len;
5009 IT_CHARPOS (*it) += 1;
5010 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5011 }
5012 }
260a86a0
KH
5013 else if (it->method == next_element_from_composition)
5014 {
5015 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
5016 if (STRINGP (it->string))
5017 {
5018 IT_STRING_BYTEPOS (*it) += it->len;
5019 IT_STRING_CHARPOS (*it) += it->cmp_len;
5020 it->method = next_element_from_string;
5021 goto consider_string_end;
5022 }
5023 else
5024 {
5025 IT_BYTEPOS (*it) += it->len;
5026 IT_CHARPOS (*it) += it->cmp_len;
5027 it->method = next_element_from_buffer;
5028 }
5029 }
5f5c8ee5
GM
5030 else if (it->method == next_element_from_c_string)
5031 {
5032 /* Current display element of IT is from a C string. */
5033 IT_BYTEPOS (*it) += it->len;
5034 IT_CHARPOS (*it) += 1;
5035 }
5036 else if (it->method == next_element_from_display_vector)
5037 {
5038 /* Current display element of IT is from a display table entry.
5039 Advance in the display table definition. Reset it to null if
5040 end reached, and continue with characters from buffers/
5041 strings. */
5042 ++it->current.dpvec_index;
286bcbc9 5043
980806b6
KH
5044 /* Restore face of the iterator to what they were before the
5045 display vector entry (these entries may contain faces). */
5f5c8ee5 5046 it->face_id = it->saved_face_id;
2311178e 5047
5f5c8ee5
GM
5048 if (it->dpvec + it->current.dpvec_index == it->dpend)
5049 {
5050 if (it->s)
5051 it->method = next_element_from_c_string;
5052 else if (STRINGP (it->string))
5053 it->method = next_element_from_string;
5054 else
5055 it->method = next_element_from_buffer;
5056
5057 it->dpvec = NULL;
5058 it->current.dpvec_index = -1;
5059
312246d1
GM
5060 /* Skip over characters which were displayed via IT->dpvec. */
5061 if (it->dpvec_char_len < 0)
5062 reseat_at_next_visible_line_start (it, 1);
5063 else if (it->dpvec_char_len > 0)
5f5c8ee5
GM
5064 {
5065 it->len = it->dpvec_char_len;
cafafe0b 5066 set_iterator_to_next (it, reseat_p);
5f5c8ee5
GM
5067 }
5068 }
5069 }
5070 else if (it->method == next_element_from_string)
5071 {
5072 /* Current display element is a character from a Lisp string. */
5073 xassert (it->s == NULL && STRINGP (it->string));
5074 IT_STRING_BYTEPOS (*it) += it->len;
5075 IT_STRING_CHARPOS (*it) += 1;
2311178e 5076
5f5c8ee5
GM
5077 consider_string_end:
5078
5079 if (it->current.overlay_string_index >= 0)
5080 {
5081 /* IT->string is an overlay string. Advance to the
5082 next, if there is one. */
2051c264 5083 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5f5c8ee5
GM
5084 next_overlay_string (it);
5085 }
5086 else
5087 {
5088 /* IT->string is not an overlay string. If we reached
5089 its end, and there is something on IT->stack, proceed
5090 with what is on the stack. This can be either another
5091 string, this time an overlay string, or a buffer. */
2051c264 5092 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5f5c8ee5
GM
5093 && it->sp > 0)
5094 {
5095 pop_it (it);
5096 if (!STRINGP (it->string))
5097 it->method = next_element_from_buffer;
b2046df8
GM
5098 else
5099 goto consider_string_end;
5f5c8ee5
GM
5100 }
5101 }
5102 }
5103 else if (it->method == next_element_from_image
5104 || it->method == next_element_from_stretch)
5105 {
5106 /* The position etc with which we have to proceed are on
5107 the stack. The position may be at the end of a string,
5108 if the `display' property takes up the whole string. */
5109 pop_it (it);
5110 it->image_id = 0;
5111 if (STRINGP (it->string))
5112 {
5113 it->method = next_element_from_string;
5114 goto consider_string_end;
5115 }
5116 else
5117 it->method = next_element_from_buffer;
5118 }
5119 else
5120 /* There are no other methods defined, so this should be a bug. */
5121 abort ();
5122
5f5c8ee5
GM
5123 xassert (it->method != next_element_from_string
5124 || (STRINGP (it->string)
5125 && IT_STRING_CHARPOS (*it) >= 0));
5126}
5127
5128
5129/* Load IT's display element fields with information about the next
5130 display element which comes from a display table entry or from the
5131 result of translating a control character to one of the forms `^C'
5132 or `\003'. IT->dpvec holds the glyphs to return as characters. */
5133
5134static int
5135next_element_from_display_vector (it)
5136 struct it *it;
5137{
5138 /* Precondition. */
5139 xassert (it->dpvec && it->current.dpvec_index >= 0);
5140
5141 /* Remember the current face id in case glyphs specify faces.
5142 IT's face is restored in set_iterator_to_next. */
5143 it->saved_face_id = it->face_id;
2311178e 5144
5f5c8ee5
GM
5145 if (INTEGERP (*it->dpvec)
5146 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5147 {
5148 int lface_id;
5149 GLYPH g;
5150
5151 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5152 it->c = FAST_GLYPH_CHAR (g);
c5924f47 5153 it->len = CHAR_BYTES (it->c);
5f5c8ee5
GM
5154
5155 /* The entry may contain a face id to use. Such a face id is
5156 the id of a Lisp face, not a realized face. A face id of
969065c3 5157 zero means no face is specified. */
5f5c8ee5
GM
5158 lface_id = FAST_GLYPH_FACE (g);
5159 if (lface_id)
5160 {
969065c3 5161 /* The function returns -1 if lface_id is invalid. */
5f5c8ee5
GM
5162 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
5163 if (face_id >= 0)
969065c3 5164 it->face_id = face_id;
5f5c8ee5
GM
5165 }
5166 }
5167 else
5168 /* Display table entry is invalid. Return a space. */
5169 it->c = ' ', it->len = 1;
5170
5171 /* Don't change position and object of the iterator here. They are
5172 still the values of the character that had this display table
5173 entry or was translated, and that's what we want. */
5174 it->what = IT_CHARACTER;
5175 return 1;
5176}
5177
5178
5179/* Load IT with the next display element from Lisp string IT->string.
5180 IT->current.string_pos is the current position within the string.
5181 If IT->current.overlay_string_index >= 0, the Lisp string is an
5182 overlay string. */
5183
5184static int
5185next_element_from_string (it)
5186 struct it *it;
5187{
5188 struct text_pos position;
5189
5190 xassert (STRINGP (it->string));
5191 xassert (IT_STRING_CHARPOS (*it) >= 0);
5192 position = it->current.string_pos;
5193
5194 /* Time to check for invisible text? */
5195 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5196 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5197 {
5198 handle_stop (it);
5199
5200 /* Since a handler may have changed IT->method, we must
5201 recurse here. */
5202 return get_next_display_element (it);
5203 }
5204
5205 if (it->current.overlay_string_index >= 0)
5206 {
5207 /* Get the next character from an overlay string. In overlay
5208 strings, There is no field width or padding with spaces to
5209 do. */
2051c264 5210 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5f5c8ee5
GM
5211 {
5212 it->what = IT_EOB;
5213 return 0;
5214 }
5215 else if (STRING_MULTIBYTE (it->string))
5216 {
2051c264 5217 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
50f80c2f
KR
5218 const unsigned char *s = (SDATA (it->string)
5219 + IT_STRING_BYTEPOS (*it));
4fdb80f2 5220 it->c = string_char_and_length (s, remaining, &it->len);
5f5c8ee5
GM
5221 }
5222 else
5223 {
2051c264 5224 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5f5c8ee5
GM
5225 it->len = 1;
5226 }
5227 }
5228 else
5229 {
5230 /* Get the next character from a Lisp string that is not an
5231 overlay string. Such strings come from the mode line, for
5232 example. We may have to pad with spaces, or truncate the
5233 string. See also next_element_from_c_string. */
5234 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5235 {
5236 it->what = IT_EOB;
5237 return 0;
5238 }
5239 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5240 {
5241 /* Pad with spaces. */
5242 it->c = ' ', it->len = 1;
5243 CHARPOS (position) = BYTEPOS (position) = -1;
5244 }
5245 else if (STRING_MULTIBYTE (it->string))
5246 {
2051c264 5247 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
50f80c2f
KR
5248 const unsigned char *s = (SDATA (it->string)
5249 + IT_STRING_BYTEPOS (*it));
4fdb80f2 5250 it->c = string_char_and_length (s, maxlen, &it->len);
5f5c8ee5
GM
5251 }
5252 else
5253 {
2051c264 5254 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5f5c8ee5
GM
5255 it->len = 1;
5256 }
5257 }
5258
5259 /* Record what we have and where it came from. Note that we store a
5260 buffer position in IT->position although it could arguably be a
5261 string position. */
5262 it->what = IT_CHARACTER;
5263 it->object = it->string;
5264 it->position = position;
5265 return 1;
5266}
5267
5268
5269/* Load IT with next display element from C string IT->s.
5270 IT->string_nchars is the maximum number of characters to return
5271 from the string. IT->end_charpos may be greater than
5272 IT->string_nchars when this function is called, in which case we
5273 may have to return padding spaces. Value is zero if end of string
5274 reached, including padding spaces. */
5275
5276static int
5277next_element_from_c_string (it)
5278 struct it *it;
5279{
5280 int success_p = 1;
2311178e 5281
5f5c8ee5
GM
5282 xassert (it->s);
5283 it->what = IT_CHARACTER;
5284 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5285 it->object = Qnil;
2311178e 5286
5f5c8ee5
GM
5287 /* IT's position can be greater IT->string_nchars in case a field
5288 width or precision has been specified when the iterator was
5289 initialized. */
5290 if (IT_CHARPOS (*it) >= it->end_charpos)
5291 {
5292 /* End of the game. */
5293 it->what = IT_EOB;
5294 success_p = 0;
5295 }
5296 else if (IT_CHARPOS (*it) >= it->string_nchars)
5297 {
5298 /* Pad with spaces. */
5299 it->c = ' ', it->len = 1;
5300 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5301 }
5302 else if (it->multibyte_p)
5303 {
5304 /* Implementation note: The calls to strlen apparently aren't a
5305 performance problem because there is no noticeable performance
5306 difference between Emacs running in unibyte or multibyte mode. */
5307 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4fdb80f2
GM
5308 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5309 maxlen, &it->len);
5f5c8ee5
GM
5310 }
5311 else
5312 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
2311178e 5313
5f5c8ee5
GM
5314 return success_p;
5315}
5316
5317
5318/* Set up IT to return characters from an ellipsis, if appropriate.
5319 The definition of the ellipsis glyphs may come from a display table
5320 entry. This function Fills IT with the first glyph from the
5321 ellipsis if an ellipsis is to be displayed. */
5322
13f19968 5323static int
5f5c8ee5
GM
5324next_element_from_ellipsis (it)
5325 struct it *it;
5326{
13f19968 5327 if (it->selective_display_ellipsis_p)
5f5c8ee5 5328 {
13f19968
GM
5329 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
5330 {
5331 /* Use the display table definition for `...'. Invalid glyphs
5332 will be handled by the method returning elements from dpvec. */
5333 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
5334 it->dpvec_char_len = it->len;
5335 it->dpvec = v->contents;
5336 it->dpend = v->contents + v->size;
5337 it->current.dpvec_index = 0;
5338 it->method = next_element_from_display_vector;
5339 }
5340 else
5341 {
5342 /* Use default `...' which is stored in default_invis_vector. */
5343 it->dpvec_char_len = it->len;
5344 it->dpvec = default_invis_vector;
5345 it->dpend = default_invis_vector + 3;
5346 it->current.dpvec_index = 0;
5347 it->method = next_element_from_display_vector;
5348 }
5f5c8ee5 5349 }
13f19968 5350 else
54918e2b 5351 {
4aad61f8
GM
5352 /* The face at the current position may be different from the
5353 face we find after the invisible text. Remember what it
5354 was in IT->saved_face_id, and signal that it's there by
5355 setting face_before_selective_p. */
5356 it->saved_face_id = it->face_id;
54918e2b
GM
5357 it->method = next_element_from_buffer;
5358 reseat_at_next_visible_line_start (it, 1);
4aad61f8 5359 it->face_before_selective_p = 1;
54918e2b 5360 }
2311178e 5361
13f19968 5362 return get_next_display_element (it);
5f5c8ee5
GM
5363}
5364
5365
5366/* Deliver an image display element. The iterator IT is already
5367 filled with image information (done in handle_display_prop). Value
5368 is always 1. */
2311178e 5369
5f5c8ee5
GM
5370
5371static int
5372next_element_from_image (it)
5373 struct it *it;
5374{
5375 it->what = IT_IMAGE;
5376 return 1;
5377}
5378
5379
5380/* Fill iterator IT with next display element from a stretch glyph
5381 property. IT->object is the value of the text property. Value is
5382 always 1. */
5383
5384static int
5385next_element_from_stretch (it)
5386 struct it *it;
5387{
5388 it->what = IT_STRETCH;
5389 return 1;
5390}
5391
5392
5393/* Load IT with the next display element from current_buffer. Value
5394 is zero if end of buffer reached. IT->stop_charpos is the next
5395 position at which to stop and check for text properties or buffer
5396 end. */
5397
5398static int
5399next_element_from_buffer (it)
5400 struct it *it;
5401{
5402 int success_p = 1;
5403
5404 /* Check this assumption, otherwise, we would never enter the
5405 if-statement, below. */
5406 xassert (IT_CHARPOS (*it) >= BEGV
5407 && IT_CHARPOS (*it) <= it->stop_charpos);
5408
5409 if (IT_CHARPOS (*it) >= it->stop_charpos)
5410 {
5411 if (IT_CHARPOS (*it) >= it->end_charpos)
5412 {
5413 int overlay_strings_follow_p;
2311178e 5414
5f5c8ee5
GM
5415 /* End of the game, except when overlay strings follow that
5416 haven't been returned yet. */
5417 if (it->overlay_strings_at_end_processed_p)
5418 overlay_strings_follow_p = 0;
5419 else
5420 {
5421 it->overlay_strings_at_end_processed_p = 1;
5a08cbaf 5422 overlay_strings_follow_p = get_overlay_strings (it, 0);
5f5c8ee5
GM
5423 }
5424
5425 if (overlay_strings_follow_p)
5426 success_p = get_next_display_element (it);
5427 else
5428 {
5429 it->what = IT_EOB;
5430 it->position = it->current.pos;
5431 success_p = 0;
5432 }
5433 }
5434 else
5435 {
5436 handle_stop (it);
5437 return get_next_display_element (it);
5438 }
5439 }
5440 else
5441 {
5442 /* No face changes, overlays etc. in sight, so just return a
5443 character from current_buffer. */
5444 unsigned char *p;
5445
5446 /* Maybe run the redisplay end trigger hook. Performance note:
5447 This doesn't seem to cost measurable time. */
5448 if (it->redisplay_end_trigger_charpos
5449 && it->glyph_row
5450 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5451 run_redisplay_end_trigger_hook (it);
5452
5453 /* Get the next character, maybe multibyte. */
5454 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
260a86a0 5455 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5f5c8ee5
GM
5456 {
5457 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5458 - IT_BYTEPOS (*it));
4fdb80f2 5459 it->c = string_char_and_length (p, maxlen, &it->len);
5f5c8ee5
GM
5460 }
5461 else
5462 it->c = *p, it->len = 1;
5463
5464 /* Record what we have and where it came from. */
5465 it->what = IT_CHARACTER;;
5466 it->object = it->w->buffer;
5467 it->position = it->current.pos;
5468
5469 /* Normally we return the character found above, except when we
5470 really want to return an ellipsis for selective display. */
5471 if (it->selective)
5472 {
5473 if (it->c == '\n')
5474 {
5475 /* A value of selective > 0 means hide lines indented more
5476 than that number of columns. */
5477 if (it->selective > 0
5478 && IT_CHARPOS (*it) + 1 < ZV
5479 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5480 IT_BYTEPOS (*it) + 1,
a67e162b 5481 (double) it->selective)) /* iftc */
312246d1 5482 {
13f19968 5483 success_p = next_element_from_ellipsis (it);
312246d1
GM
5484 it->dpvec_char_len = -1;
5485 }
5f5c8ee5
GM
5486 }
5487 else if (it->c == '\r' && it->selective == -1)
5488 {
5489 /* A value of selective == -1 means that everything from the
5490 CR to the end of the line is invisible, with maybe an
5491 ellipsis displayed for it. */
13f19968 5492 success_p = next_element_from_ellipsis (it);
312246d1 5493 it->dpvec_char_len = -1;
5f5c8ee5
GM
5494 }
5495 }
5496 }
5497
5498 /* Value is zero if end of buffer reached. */
c880678e 5499 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5f5c8ee5
GM
5500 return success_p;
5501}
5502
2311178e 5503
5f5c8ee5
GM
5504/* Run the redisplay end trigger hook for IT. */
5505
5506static void
5507run_redisplay_end_trigger_hook (it)
5508 struct it *it;
5509{
5510 Lisp_Object args[3];
5511
5512 /* IT->glyph_row should be non-null, i.e. we should be actually
5513 displaying something, or otherwise we should not run the hook. */
5514 xassert (it->glyph_row);
5515
5516 /* Set up hook arguments. */
5517 args[0] = Qredisplay_end_trigger_functions;
5518 args[1] = it->window;
5519 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5520 it->redisplay_end_trigger_charpos = 0;
5521
5522 /* Since we are *trying* to run these functions, don't try to run
5523 them again, even if they get an error. */
5524 it->w->redisplay_end_trigger = Qnil;
5525 Frun_hook_with_args (3, args);
2311178e 5526
5f5c8ee5
GM
5527 /* Notice if it changed the face of the character we are on. */
5528 handle_face_prop (it);
5529}
5530
5531
260a86a0
KH
5532/* Deliver a composition display element. The iterator IT is already
5533 filled with composition information (done in
5534 handle_composition_prop). Value is always 1. */
5535
5536static int
5537next_element_from_composition (it)
5538 struct it *it;
5539{
5540 it->what = IT_COMPOSITION;
5541 it->position = (STRINGP (it->string)
5542 ? it->current.string_pos
5543 : it->current.pos);
5544 return 1;
5545}
5546
5547
5f5c8ee5
GM
5548\f
5549/***********************************************************************
5550 Moving an iterator without producing glyphs
5551 ***********************************************************************/
5552
5553/* Move iterator IT to a specified buffer or X position within one
5554 line on the display without producing glyphs.
5555
c53a1624
RS
5556 OP should be a bit mask including some or all of these bits:
5557 MOVE_TO_X: Stop on reaching x-position TO_X.
5558 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5559 Regardless of OP's value, stop in reaching the end of the display line.
5f5c8ee5 5560
c53a1624
RS
5561 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5562 This means, in particular, that TO_X includes window's horizontal
5563 scroll amount.
5f5c8ee5 5564
c53a1624
RS
5565 The return value has several possible values that
5566 say what condition caused the scan to stop:
5f5c8ee5
GM
5567
5568 MOVE_POS_MATCH_OR_ZV
5569 - when TO_POS or ZV was reached.
2311178e 5570
5f5c8ee5
GM
5571 MOVE_X_REACHED
5572 -when TO_X was reached before TO_POS or ZV were reached.
2311178e 5573
5f5c8ee5
GM
5574 MOVE_LINE_CONTINUED
5575 - when we reached the end of the display area and the line must
5576 be continued.
2311178e 5577
5f5c8ee5
GM
5578 MOVE_LINE_TRUNCATED
5579 - when we reached the end of the display area and the line is
5580 truncated.
5581
5582 MOVE_NEWLINE_OR_CR
5583 - when we stopped at a line end, i.e. a newline or a CR and selective
5584 display is on. */
5585
701552dd 5586static enum move_it_result
5f5c8ee5
GM
5587move_it_in_display_line_to (it, to_charpos, to_x, op)
5588 struct it *it;
5589 int to_charpos, to_x, op;
5590{
5591 enum move_it_result result = MOVE_UNDEFINED;
5592 struct glyph_row *saved_glyph_row;
5593
5594 /* Don't produce glyphs in produce_glyphs. */
5595 saved_glyph_row = it->glyph_row;
5596 it->glyph_row = NULL;
5597
4540d74c
KS
5598#define BUFFER_POS_REACHED_P() \
5599 ((op & MOVE_TO_POS) != 0 \
5600 && BUFFERP (it->object) \
5601 && IT_CHARPOS (*it) >= to_charpos)
5602
5f5c8ee5
GM
5603 while (1)
5604 {
ae26e27d 5605 int x, i, ascent = 0, descent = 0;
2311178e 5606
5f5c8ee5
GM
5607 /* Stop when ZV or TO_CHARPOS reached. */
5608 if (!get_next_display_element (it)
4540d74c 5609 || BUFFER_POS_REACHED_P ())
5f5c8ee5
GM
5610 {
5611 result = MOVE_POS_MATCH_OR_ZV;
5612 break;
5613 }
2311178e 5614
5f5c8ee5
GM
5615 /* The call to produce_glyphs will get the metrics of the
5616 display element IT is loaded with. We record in x the
5617 x-position before this display element in case it does not
5618 fit on the line. */
5619 x = it->current_x;
2311178e 5620
47589c8c
GM
5621 /* Remember the line height so far in case the next element doesn't
5622 fit on the line. */
5623 if (!it->truncate_lines_p)
5624 {
5625 ascent = it->max_ascent;
5626 descent = it->max_descent;
5627 }
2311178e 5628
5f5c8ee5
GM
5629 PRODUCE_GLYPHS (it);
5630
5631 if (it->area != TEXT_AREA)
5632 {
cafafe0b 5633 set_iterator_to_next (it, 1);
5f5c8ee5
GM
5634 continue;
5635 }
5636
5637 /* The number of glyphs we get back in IT->nglyphs will normally
5638 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5639 character on a terminal frame, or (iii) a line end. For the
5640 second case, IT->nglyphs - 1 padding glyphs will be present
5641 (on X frames, there is only one glyph produced for a
5642 composite character.
5643
5644 The behavior implemented below means, for continuation lines,
5645 that as many spaces of a TAB as fit on the current line are
5646 displayed there. For terminal frames, as many glyphs of a
5647 multi-glyph character are displayed in the current line, too.
5648 This is what the old redisplay code did, and we keep it that
5649 way. Under X, the whole shape of a complex character must
5650 fit on the line or it will be completely displayed in the
5651 next line.
5652
5653 Note that both for tabs and padding glyphs, all glyphs have
5654 the same width. */
5655 if (it->nglyphs)
5656 {
5657 /* More than one glyph or glyph doesn't fit on line. All
5658 glyphs have the same width. */
5659 int single_glyph_width = it->pixel_width / it->nglyphs;
5660 int new_x;
2311178e 5661
5f5c8ee5
GM
5662 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5663 {
5664 new_x = x + single_glyph_width;
5665
5666 /* We want to leave anything reaching TO_X to the caller. */
5667 if ((op & MOVE_TO_X) && new_x > to_x)
5668 {
5669 it->current_x = x;
5670 result = MOVE_X_REACHED;
5671 break;
5672 }
5673 else if (/* Lines are continued. */
5674 !it->truncate_lines_p
5675 && (/* And glyph doesn't fit on the line. */
5676 new_x > it->last_visible_x
5677 /* Or it fits exactly and we're on a window
5678 system frame. */
5679 || (new_x == it->last_visible_x
5680 && FRAME_WINDOW_P (it->f))))
5681 {
5682 if (/* IT->hpos == 0 means the very first glyph
5683 doesn't fit on the line, e.g. a wide image. */
5684 it->hpos == 0
5685 || (new_x == it->last_visible_x
5686 && FRAME_WINDOW_P (it->f)))
5687 {
5688 ++it->hpos;
5689 it->current_x = new_x;
5690 if (i == it->nglyphs - 1)
88e6b646
KS
5691 {
5692 set_iterator_to_next (it, 1);
7af0e8d7 5693#ifdef HAVE_WINDOW_SYSTEM
88e6b646
KS
5694 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5695 {
4540d74c
KS
5696 if (!get_next_display_element (it)
5697 || BUFFER_POS_REACHED_P ())
6d925726
KS
5698 {
5699 result = MOVE_POS_MATCH_OR_ZV;
5700 break;
5701 }
88e6b646
KS
5702 if (ITERATOR_AT_END_OF_LINE_P (it))
5703 {
5704 result = MOVE_NEWLINE_OR_CR;
5705 break;
5706 }
5707 }
7af0e8d7 5708#endif /* HAVE_WINDOW_SYSTEM */
88e6b646 5709 }
5f5c8ee5
GM
5710 }
5711 else
47589c8c
GM
5712 {
5713 it->current_x = x;
5714 it->max_ascent = ascent;
5715 it->max_descent = descent;
5716 }
2311178e 5717
47589c8c
GM
5718 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5719 IT_CHARPOS (*it)));
5f5c8ee5
GM
5720 result = MOVE_LINE_CONTINUED;
5721 break;
5722 }
5723 else if (new_x > it->first_visible_x)
5724 {
5725 /* Glyph is visible. Increment number of glyphs that
5726 would be displayed. */
5727 ++it->hpos;
5728 }
5729 else
5730 {
2311178e 5731 /* Glyph is completely off the left margin of the display
5f5c8ee5
GM
5732 area. Nothing to do. */
5733 }
5734 }
5735
5736 if (result != MOVE_UNDEFINED)
5737 break;
5738 }
5739 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5740 {
5741 /* Stop when TO_X specified and reached. This check is
5742 necessary here because of lines consisting of a line end,
5743 only. The line end will not produce any glyphs and we
5744 would never get MOVE_X_REACHED. */
5745 xassert (it->nglyphs == 0);
5746 result = MOVE_X_REACHED;
5747 break;
5748 }
2311178e 5749
5f5c8ee5
GM
5750 /* Is this a line end? If yes, we're done. */
5751 if (ITERATOR_AT_END_OF_LINE_P (it))
5752 {
5753 result = MOVE_NEWLINE_OR_CR;
5754 break;
5755 }
2311178e 5756
5f5c8ee5
GM
5757 /* The current display element has been consumed. Advance
5758 to the next. */
cafafe0b 5759 set_iterator_to_next (it, 1);
2311178e 5760
5f5c8ee5
GM
5761 /* Stop if lines are truncated and IT's current x-position is
5762 past the right edge of the window now. */
5763 if (it->truncate_lines_p
5764 && it->current_x >= it->last_visible_x)
5765 {
7af0e8d7 5766#ifdef HAVE_WINDOW_SYSTEM
88e6b646
KS
5767 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5768 {
4540d74c
KS
5769 if (!get_next_display_element (it)
5770 || BUFFER_POS_REACHED_P ())
6d925726
KS
5771 {
5772 result = MOVE_POS_MATCH_OR_ZV;
5773 break;
5774 }
88e6b646
KS
5775 if (ITERATOR_AT_END_OF_LINE_P (it))
5776 {
5777 result = MOVE_NEWLINE_OR_CR;
5778 break;
5779 }
5780 }
7af0e8d7 5781#endif /* HAVE_WINDOW_SYSTEM */
5f5c8ee5
GM
5782 result = MOVE_LINE_TRUNCATED;
5783 break;
5784 }
5785 }
5786
4540d74c
KS
5787#undef BUFFER_POS_REACHED_P
5788
5f5c8ee5
GM
5789 /* Restore the iterator settings altered at the beginning of this
5790 function. */
5791 it->glyph_row = saved_glyph_row;
5792 return result;
5793}
5794
5795
9b2bba76
RS
5796/* Move IT forward until it satisfies one or more of the criteria in
5797 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5798
5799 OP is a bit-mask that specifies where to stop, and in particular,
5800 which of those four position arguments makes a difference. See the
5801 description of enum move_operation_enum.
2311178e 5802
5f5c8ee5
GM
5803 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5804 screen line, this function will set IT to the next position >
5805 TO_CHARPOS. */
5806
5807void
5808move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5809 struct it *it;
5810 int to_charpos, to_x, to_y, to_vpos;
5811 int op;
5812{
5813 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5814 int line_height;
47589c8c 5815 int reached = 0;
5f5c8ee5 5816
47589c8c 5817 for (;;)
5f5c8ee5
GM
5818 {
5819 if (op & MOVE_TO_VPOS)
5820 {
5821 /* If no TO_CHARPOS and no TO_X specified, stop at the
5822 start of the line TO_VPOS. */
5823 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5824 {
5825 if (it->vpos == to_vpos)
47589c8c
GM
5826 {
5827 reached = 1;
5828 break;
5829 }
5830 else
5831 skip = move_it_in_display_line_to (it, -1, -1, 0);
5f5c8ee5
GM
5832 }
5833 else
5834 {
5835 /* TO_VPOS >= 0 means stop at TO_X in the line at
5836 TO_VPOS, or at TO_POS, whichever comes first. */
47589c8c
GM
5837 if (it->vpos == to_vpos)
5838 {
5839 reached = 2;
5840 break;
5841 }
2311178e 5842
5f5c8ee5
GM
5843 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5844
5845 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
47589c8c
GM
5846 {
5847 reached = 3;
5848 break;
5849 }
5f5c8ee5
GM
5850 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5851 {
5852 /* We have reached TO_X but not in the line we want. */
5853 skip = move_it_in_display_line_to (it, to_charpos,
5854 -1, MOVE_TO_POS);
5855 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c
GM
5856 {
5857 reached = 4;
5858 break;
5859 }
5f5c8ee5
GM
5860 }
5861 }
5862 }
5863 else if (op & MOVE_TO_Y)
5864 {
5865 struct it it_backup;
2311178e 5866
5f5c8ee5
GM
5867 /* TO_Y specified means stop at TO_X in the line containing
5868 TO_Y---or at TO_CHARPOS if this is reached first. The
5869 problem is that we can't really tell whether the line
5870 contains TO_Y before we have completely scanned it, and
5871 this may skip past TO_X. What we do is to first scan to
5872 TO_X.
5873
5874 If TO_X is not specified, use a TO_X of zero. The reason
5875 is to make the outcome of this function more predictable.
5876 If we didn't use TO_X == 0, we would stop at the end of
5877 the line which is probably not what a caller would expect
5878 to happen. */
5879 skip = move_it_in_display_line_to (it, to_charpos,
5880 ((op & MOVE_TO_X)
5881 ? to_x : 0),
5882 (MOVE_TO_X
5883 | (op & MOVE_TO_POS)));
5884
5885 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5886 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c
GM
5887 {
5888 reached = 5;
5889 break;
5890 }
2311178e 5891
5f5c8ee5
GM
5892 /* If TO_X was reached, we would like to know whether TO_Y
5893 is in the line. This can only be said if we know the
5894 total line height which requires us to scan the rest of
5895 the line. */
5f5c8ee5
GM
5896 if (skip == MOVE_X_REACHED)
5897 {
5898 it_backup = *it;
47589c8c 5899 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
5900 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5901 op & MOVE_TO_POS);
47589c8c 5902 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
5903 }
5904
5905 /* Now, decide whether TO_Y is in this line. */
5906 line_height = it->max_ascent + it->max_descent;
47589c8c 5907 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
2311178e 5908
5f5c8ee5
GM
5909 if (to_y >= it->current_y
5910 && to_y < it->current_y + line_height)
5911 {
5912 if (skip == MOVE_X_REACHED)
5913 /* If TO_Y is in this line and TO_X was reached above,
5914 we scanned too far. We have to restore IT's settings
5915 to the ones before skipping. */
5916 *it = it_backup;
47589c8c 5917 reached = 6;
5f5c8ee5
GM
5918 }
5919 else if (skip == MOVE_X_REACHED)
5920 {
5921 skip = skip2;
5922 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c 5923 reached = 7;
5f5c8ee5
GM
5924 }
5925
47589c8c 5926 if (reached)
5f5c8ee5
GM
5927 break;
5928 }
5929 else
5930 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5931
5932 switch (skip)
5933 {
5934 case MOVE_POS_MATCH_OR_ZV:
47589c8c
GM
5935 reached = 8;
5936 goto out;
5f5c8ee5
GM
5937
5938 case MOVE_NEWLINE_OR_CR:
cafafe0b 5939 set_iterator_to_next (it, 1);
5f5c8ee5
GM
5940 it->continuation_lines_width = 0;
5941 break;
5942
5943 case MOVE_LINE_TRUNCATED:
5944 it->continuation_lines_width = 0;
312246d1 5945 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
5946 if ((op & MOVE_TO_POS) != 0
5947 && IT_CHARPOS (*it) > to_charpos)
47589c8c
GM
5948 {
5949 reached = 9;
5950 goto out;
5951 }
5f5c8ee5
GM
5952 break;
5953
5954 case MOVE_LINE_CONTINUED:
5955 it->continuation_lines_width += it->current_x;
5956 break;
5957
5958 default:
5959 abort ();
5960 }
5961
5962 /* Reset/increment for the next run. */
5963 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5964 it->current_x = it->hpos = 0;
5965 it->current_y += it->max_ascent + it->max_descent;
5966 ++it->vpos;
5967 last_height = it->max_ascent + it->max_descent;
5968 last_max_ascent = it->max_ascent;
5969 it->max_ascent = it->max_descent = 0;
5970 }
2311178e 5971
47589c8c
GM
5972 out:
5973
5974 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5f5c8ee5
GM
5975}
5976
5977
5978/* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5979
5980 If DY > 0, move IT backward at least that many pixels. DY = 0
5981 means move IT backward to the preceding line start or BEGV. This
5982 function may move over more than DY pixels if IT->current_y - DY
5983 ends up in the middle of a line; in this case IT->current_y will be
5984 set to the top of the line moved to. */
5985
5986void
5987move_it_vertically_backward (it, dy)
5988 struct it *it;
5989 int dy;
5990{
79ddf6f7
GM
5991 int nlines, h;
5992 struct it it2, it3;
5f5c8ee5 5993 int start_pos = IT_CHARPOS (*it);
2311178e 5994
5f5c8ee5
GM
5995 xassert (dy >= 0);
5996
5997 /* Estimate how many newlines we must move back. */
da8b7f4f 5998 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
5f5c8ee5
GM
5999
6000 /* Set the iterator's position that many lines back. */
6001 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6002 back_to_previous_visible_line_start (it);
6003
6004 /* Reseat the iterator here. When moving backward, we don't want
6005 reseat to skip forward over invisible text, set up the iterator
6006 to deliver from overlay strings at the new position etc. So,
6007 use reseat_1 here. */
6008 reseat_1 (it, it->current.pos, 1);
6009
6010 /* We are now surely at a line start. */
6011 it->current_x = it->hpos = 0;
0e47bbf7 6012 it->continuation_lines_width = 0;
5f5c8ee5
GM
6013
6014 /* Move forward and see what y-distance we moved. First move to the
6015 start of the next line so that we get its height. We need this
6016 height to be able to tell whether we reached the specified
6017 y-distance. */
6018 it2 = *it;
6019 it2.max_ascent = it2.max_descent = 0;
6020 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6021 MOVE_TO_POS | MOVE_TO_VPOS);
6022 xassert (IT_CHARPOS (*it) >= BEGV);
79ddf6f7 6023 it3 = it2;
2311178e 6024
5f5c8ee5
GM
6025 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6026 xassert (IT_CHARPOS (*it) >= BEGV);
ccbb9ed2
RS
6027 /* H is the actual vertical distance from the position in *IT
6028 and the starting position. */
5f5c8ee5 6029 h = it2.current_y - it->current_y;
ccbb9ed2 6030 /* NLINES is the distance in number of lines. */
5f5c8ee5
GM
6031 nlines = it2.vpos - it->vpos;
6032
ccbb9ed2
RS
6033 /* Correct IT's y and vpos position
6034 so that they are relative to the starting point. */
5f5c8ee5
GM
6035 it->vpos -= nlines;
6036 it->current_y -= h;
2311178e 6037
5f5c8ee5
GM
6038 if (dy == 0)
6039 {
6040 /* DY == 0 means move to the start of the screen line. The
6041 value of nlines is > 0 if continuation lines were involved. */
6042 if (nlines > 0)
6043 move_it_by_lines (it, nlines, 1);
6044 xassert (IT_CHARPOS (*it) <= start_pos);
6045 }
ccbb9ed2 6046 else
5f5c8ee5 6047 {
ccbb9ed2
RS
6048 /* The y-position we try to reach, relative to *IT.
6049 Note that H has been subtracted in front of the if-statement. */
5f5c8ee5 6050 int target_y = it->current_y + h - dy;
79ddf6f7
GM
6051 int y0 = it3.current_y;
6052 int y1 = line_bottom_y (&it3);
6053 int line_height = y1 - y0;
f7ccfc8c 6054
5f5c8ee5
GM
6055 /* If we did not reach target_y, try to move further backward if
6056 we can. If we moved too far backward, try to move forward. */
6057 if (target_y < it->current_y
79ddf6f7
GM
6058 /* This is heuristic. In a window that's 3 lines high, with
6059 a line height of 13 pixels each, recentering with point
6060 on the bottom line will try to move -39/2 = 19 pixels
6061 backward. Try to avoid moving into the first line. */
798dbe1f 6062 && it->current_y - target_y > line_height / 3 * 2
5f5c8ee5
GM
6063 && IT_CHARPOS (*it) > BEGV)
6064 {
f7ccfc8c
GM
6065 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6066 target_y - it->current_y));
5f5c8ee5
GM
6067 move_it_vertically (it, target_y - it->current_y);
6068 xassert (IT_CHARPOS (*it) >= BEGV);
6069 }
6070 else if (target_y >= it->current_y + line_height
6071 && IT_CHARPOS (*it) < ZV)
6072 {
55591976 6073 /* Should move forward by at least one line, maybe more.
2311178e 6074
55591976
GM
6075 Note: Calling move_it_by_lines can be expensive on
6076 terminal frames, where compute_motion is used (via
6077 vmotion) to do the job, when there are very long lines
6078 and truncate-lines is nil. That's the reason for
6079 treating terminal frames specially here. */
2311178e 6080
55591976
GM
6081 if (!FRAME_WINDOW_P (it->f))
6082 move_it_vertically (it, target_y - (it->current_y + line_height));
6083 else
f7ccfc8c 6084 {
55591976
GM
6085 do
6086 {
6087 move_it_by_lines (it, 1, 1);
6088 }
6089 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
f7ccfc8c 6090 }
f7ccfc8c 6091
5f5c8ee5
GM
6092 xassert (IT_CHARPOS (*it) >= BEGV);
6093 }
6094 }
6095}
6096
6097
6098/* Move IT by a specified amount of pixel lines DY. DY negative means
6099 move backwards. DY = 0 means move to start of screen line. At the
6100 end, IT will be on the start of a screen line. */
6101
2311178e 6102void
5f5c8ee5
GM
6103move_it_vertically (it, dy)
6104 struct it *it;
6105 int dy;
6106{
6107 if (dy <= 0)
6108 move_it_vertically_backward (it, -dy);
6109 else if (dy > 0)
6110 {
47589c8c 6111 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5f5c8ee5
GM
6112 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6113 MOVE_TO_POS | MOVE_TO_Y);
47589c8c 6114 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
6115
6116 /* If buffer ends in ZV without a newline, move to the start of
6117 the line to satisfy the post-condition. */
6118 if (IT_CHARPOS (*it) == ZV
6119 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6120 move_it_by_lines (it, 0, 0);
6121 }
6122}
6123
6124
47fc2c10
GM
6125/* Move iterator IT past the end of the text line it is in. */
6126
6127void
6128move_it_past_eol (it)
6129 struct it *it;
6130{
6131 enum move_it_result rc;
2311178e 6132
47fc2c10
GM
6133 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6134 if (rc == MOVE_NEWLINE_OR_CR)
6135 set_iterator_to_next (it, 0);
6136}
6137
6138
2c79b732
GM
6139#if 0 /* Currently not used. */
6140
5f5c8ee5
GM
6141/* Return non-zero if some text between buffer positions START_CHARPOS
6142 and END_CHARPOS is invisible. IT->window is the window for text
6143 property lookup. */
6144
6145static int
6146invisible_text_between_p (it, start_charpos, end_charpos)
6147 struct it *it;
6148 int start_charpos, end_charpos;
6149{
5f5c8ee5
GM
6150 Lisp_Object prop, limit;
6151 int invisible_found_p;
2311178e 6152
5f5c8ee5
GM
6153 xassert (it != NULL && start_charpos <= end_charpos);
6154
6155 /* Is text at START invisible? */
6156 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6157 it->window);
6158 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6159 invisible_found_p = 1;
6160 else
6161 {
016b5642
MB
6162 limit = Fnext_single_char_property_change (make_number (start_charpos),
6163 Qinvisible, Qnil,
6164 make_number (end_charpos));
5f5c8ee5
GM
6165 invisible_found_p = XFASTINT (limit) < end_charpos;
6166 }
6167
6168 return invisible_found_p;
5f5c8ee5
GM
6169}
6170
2c79b732
GM
6171#endif /* 0 */
6172
5f5c8ee5
GM
6173
6174/* Move IT by a specified number DVPOS of screen lines down. DVPOS
6175 negative means move up. DVPOS == 0 means move to the start of the
6176 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6177 NEED_Y_P is zero, IT->current_y will be left unchanged.
6178
6179 Further optimization ideas: If we would know that IT->f doesn't use
6180 a face with proportional font, we could be faster for
6181 truncate-lines nil. */
6182
6183void
6184move_it_by_lines (it, dvpos, need_y_p)
6185 struct it *it;
6186 int dvpos, need_y_p;
6187{
6188 struct position pos;
2311178e 6189
5f5c8ee5
GM
6190 if (!FRAME_WINDOW_P (it->f))
6191 {
6192 struct text_pos textpos;
2311178e 6193
5f5c8ee5
GM
6194 /* We can use vmotion on frames without proportional fonts. */
6195 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6196 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6197 reseat (it, textpos, 1);
6198 it->vpos += pos.vpos;
6199 it->current_y += pos.vpos;
6200 }
6201 else if (dvpos == 0)
6202 {
6203 /* DVPOS == 0 means move to the start of the screen line. */
6204 move_it_vertically_backward (it, 0);
6205 xassert (it->current_x == 0 && it->hpos == 0);
6206 }
6207 else if (dvpos > 0)
2c79b732 6208 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5f5c8ee5
GM
6209 else
6210 {
6211 struct it it2;
6212 int start_charpos, i;
2311178e 6213
e8660d73
GM
6214 /* Start at the beginning of the screen line containing IT's
6215 position. */
6216 move_it_vertically_backward (it, 0);
2311178e 6217
5f5c8ee5
GM
6218 /* Go back -DVPOS visible lines and reseat the iterator there. */
6219 start_charpos = IT_CHARPOS (*it);
6220 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6221 back_to_previous_visible_line_start (it);
6222 reseat (it, it->current.pos, 1);
6223 it->current_x = it->hpos = 0;
6224
6225 /* Above call may have moved too far if continuation lines
6226 are involved. Scan forward and see if it did. */
6227 it2 = *it;
6228 it2.vpos = it2.current_y = 0;
6229 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6230 it->vpos -= it2.vpos;
6231 it->current_y -= it2.current_y;
6232 it->current_x = it->hpos = 0;
6233
6234 /* If we moved too far, move IT some lines forward. */
6235 if (it2.vpos > -dvpos)
6236 {
6237 int delta = it2.vpos + dvpos;
6238 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6239 }
6240 }
6241}
6242
3824b1a6
AS
6243/* Return 1 if IT points into the middle of a display vector. */
6244
6245int
6246in_display_vector_p (it)
6247 struct it *it;
6248{
6249 return (it->method == next_element_from_display_vector
6250 && it->current.dpvec_index > 0
6251 && it->dpvec + it->current.dpvec_index != it->dpend);
6252}
5f5c8ee5
GM
6253
6254\f
6255/***********************************************************************
6256 Messages
6257 ***********************************************************************/
6258
6259
937248bc
GM
6260/* Add a message with format string FORMAT and arguments ARG1 and ARG2
6261 to *Messages*. */
6262
6263void
6264add_to_log (format, arg1, arg2)
6265 char *format;
6266 Lisp_Object arg1, arg2;
6267{
6268 Lisp_Object args[3];
6269 Lisp_Object msg, fmt;
6270 char *buffer;
6271 int len;
6272 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6273
ae794295
GM
6274 /* Do nothing if called asynchronously. Inserting text into
6275 a buffer may call after-change-functions and alike and
6276 that would means running Lisp asynchronously. */
6277 if (handling_signal)
6278 return;
6279
937248bc
GM
6280 fmt = msg = Qnil;
6281 GCPRO4 (fmt, msg, arg1, arg2);
2311178e 6282
937248bc
GM
6283 args[0] = fmt = build_string (format);
6284 args[1] = arg1;
6285 args[2] = arg2;
6fc556fd 6286 msg = Fformat (3, args);
937248bc 6287
2051c264 6288 len = SBYTES (msg) + 1;
937248bc 6289 buffer = (char *) alloca (len);
2051c264 6290 bcopy (SDATA (msg), buffer, len);
2311178e 6291
796184bc 6292 message_dolog (buffer, len - 1, 1, 0);
937248bc
GM
6293 UNGCPRO;
6294}
6295
6296
5f5c8ee5
GM
6297/* Output a newline in the *Messages* buffer if "needs" one. */
6298
6299void
6300message_log_maybe_newline ()
6301{
6302 if (message_log_need_newline)
6303 message_dolog ("", 0, 1, 0);
6304}
6305
6306
1e313f28 6307/* Add a string M of length NBYTES to the message log, optionally
5f5c8ee5
GM
6308 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6309 nonzero, means interpret the contents of M as multibyte. This
6310 function calls low-level routines in order to bypass text property
6311 hooks, etc. which might not be safe to run. */
6312
6313void
1e313f28 6314message_dolog (m, nbytes, nlflag, multibyte)
50f80c2f 6315 const char *m;
1e313f28 6316 int nbytes, nlflag, multibyte;
5f5c8ee5 6317{
a67e162b
RS
6318 if (!NILP (Vmemory_full))
6319 return;
6320
5f5c8ee5
GM
6321 if (!NILP (Vmessage_log_max))
6322 {
6323 struct buffer *oldbuf;
6324 Lisp_Object oldpoint, oldbegv, oldzv;
6325 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6326 int point_at_end = 0;
6327 int zv_at_end = 0;
6328 Lisp_Object old_deactivate_mark, tem;
6052529b 6329 struct gcpro gcpro1;
5f5c8ee5
GM
6330
6331 old_deactivate_mark = Vdeactivate_mark;
6332 oldbuf = current_buffer;
6a94510a 6333 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5f5c8ee5
GM
6334 current_buffer->undo_list = Qt;
6335
b14bc55e
RS
6336 oldpoint = message_dolog_marker1;
6337 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6338 oldbegv = message_dolog_marker2;
6339 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6340 oldzv = message_dolog_marker3;
6341 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6342 GCPRO1 (old_deactivate_mark);
5f5c8ee5
GM
6343
6344 if (PT == Z)
6345 point_at_end = 1;
6346 if (ZV == Z)
6347 zv_at_end = 1;
6348
6349 BEGV = BEG;
6350 BEGV_BYTE = BEG_BYTE;
6351 ZV = Z;
6352 ZV_BYTE = Z_BYTE;
6353 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6354
6355 /* Insert the string--maybe converting multibyte to single byte
6356 or vice versa, so that all the text fits the buffer. */
6357 if (multibyte
6358 && NILP (current_buffer->enable_multibyte_characters))
6359 {
1e313f28 6360 int i, c, char_bytes;
5f5c8ee5 6361 unsigned char work[1];
2311178e 6362
5f5c8ee5
GM
6363 /* Convert a multibyte string to single-byte
6364 for the *Message* buffer. */
6e57ec5e 6365 for (i = 0; i < nbytes; i += char_bytes)
5f5c8ee5 6366 {
1e313f28 6367 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5f5c8ee5
GM
6368 work[0] = (SINGLE_BYTE_CHAR_P (c)
6369 ? c
6370 : multibyte_char_to_unibyte (c, Qnil));
6371 insert_1_both (work, 1, 1, 1, 0, 0);
6372 }
6373 }
6374 else if (! multibyte
6375 && ! NILP (current_buffer->enable_multibyte_characters))
6376 {
1e313f28 6377 int i, c, char_bytes;
5f5c8ee5 6378 unsigned char *msg = (unsigned char *) m;
260a86a0 6379 unsigned char str[MAX_MULTIBYTE_LENGTH];
5f5c8ee5
GM
6380 /* Convert a single-byte string to multibyte
6381 for the *Message* buffer. */
1e313f28 6382 for (i = 0; i < nbytes; i++)
5f5c8ee5
GM
6383 {
6384 c = unibyte_char_to_multibyte (msg[i]);
1e313f28
GM
6385 char_bytes = CHAR_STRING (c, str);
6386 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5f5c8ee5
GM
6387 }
6388 }
1e313f28
GM
6389 else if (nbytes)
6390 insert_1 (m, nbytes, 1, 0, 0);
5f5c8ee5
GM
6391
6392 if (nlflag)
6393 {
6394 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6395 insert_1 ("\n", 1, 1, 0, 0);
6396
6397 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6398 this_bol = PT;
6399 this_bol_byte = PT_BYTE;
6400
b14bc55e
RS
6401 /* See if this line duplicates the previous one.
6402 If so, combine duplicates. */
5f5c8ee5
GM
6403 if (this_bol > BEG)
6404 {
6405 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6406 prev_bol = PT;
6407 prev_bol_byte = PT_BYTE;
6408
6409 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6410 this_bol, this_bol_byte);
6411 if (dup)
6412 {
6413 del_range_both (prev_bol, prev_bol_byte,
6414 this_bol, this_bol_byte, 0);
6415 if (dup > 1)
6416 {
6417 char dupstr[40];
6418 int duplen;
6419
6420 /* If you change this format, don't forget to also
6421 change message_log_check_duplicate. */
6422 sprintf (dupstr, " [%d times]", dup);
6423 duplen = strlen (dupstr);
6424 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6425 insert_1 (dupstr, duplen, 1, 0, 1);
6426 }
6427 }
6428 }
6429
b14bc55e
RS
6430 /* If we have more than the desired maximum number of lines
6431 in the *Messages* buffer now, delete the oldest ones.
6432 This is safe because we don't have undo in this buffer. */
6433
5f5c8ee5
GM
6434 if (NATNUMP (Vmessage_log_max))
6435 {
6436 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6437 -XFASTINT (Vmessage_log_max) - 1, 0);
6438 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6439 }
6440 }
6441 BEGV = XMARKER (oldbegv)->charpos;
6442 BEGV_BYTE = marker_byte_position (oldbegv);
6443
6444 if (zv_at_end)
6445 {
6446 ZV = Z;
6447 ZV_BYTE = Z_BYTE;
6448 }
6449 else
6450 {
6451 ZV = XMARKER (oldzv)->charpos;
6452 ZV_BYTE = marker_byte_position (oldzv);
6453 }
6454
6455 if (point_at_end)
6456 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6457 else
6458 /* We can't do Fgoto_char (oldpoint) because it will run some
6459 Lisp code. */
6460 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6461 XMARKER (oldpoint)->bytepos);
6462
6463 UNGCPRO;
cfea0546
SM
6464 unchain_marker (XMARKER (oldpoint));
6465 unchain_marker (XMARKER (oldbegv));
6466 unchain_marker (XMARKER (oldzv));
5f5c8ee5
GM
6467
6468 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6469 set_buffer_internal (oldbuf);
6470 if (NILP (tem))
6471 windows_or_buffers_changed = old_windows_or_buffers_changed;
6472 message_log_need_newline = !nlflag;
6473 Vdeactivate_mark = old_deactivate_mark;
6474 }
6475}
6476
6477
6478/* We are at the end of the buffer after just having inserted a newline.
6479 (Note: We depend on the fact we won't be crossing the gap.)
6480 Check to see if the most recent message looks a lot like the previous one.
6481 Return 0 if different, 1 if the new one should just replace it, or a
6482 value N > 1 if we should also append " [N times]". */
6483
6484static int
6485message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6486 int prev_bol, this_bol;
6487 int prev_bol_byte, this_bol_byte;
6488{
6489 int i;
6490 int len = Z_BYTE - 1 - this_bol_byte;
6491 int seen_dots = 0;
6492 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6493 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6494
6495 for (i = 0; i < len; i++)
6496 {
509633e3 6497 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5f5c8ee5
GM
6498 seen_dots = 1;
6499 if (p1[i] != p2[i])
6500 return seen_dots;
6501 }
6502 p1 += len;
6503 if (*p1 == '\n')
6504 return 2;
6505 if (*p1++ == ' ' && *p1++ == '[')
6506 {
6507 int n = 0;
6508 while (*p1 >= '0' && *p1 <= '9')
6509 n = n * 10 + *p1++ - '0';
6510 if (strncmp (p1, " times]\n", 8) == 0)
6511 return n+1;
6512 }
6513 return 0;
6514}
6515
6516
1e313f28
GM
6517/* Display an echo area message M with a specified length of NBYTES
6518 bytes. The string may include null characters. If M is 0, clear
6519 out any existing message, and let the mini-buffer text show
6520 through.
5f5c8ee5
GM
6521
6522 The buffer M must continue to exist until after the echo area gets
6523 cleared or some other message gets displayed there. This means do
6524 not pass text that is stored in a Lisp string; do not pass text in
6525 a buffer that was alloca'd. */
6526
6527void
1e313f28 6528message2 (m, nbytes, multibyte)
50f80c2f 6529 const char *m;
1e313f28 6530 int nbytes;
5f5c8ee5
GM
6531 int multibyte;
6532{
6533 /* First flush out any partial line written with print. */
6534 message_log_maybe_newline ();
6535 if (m)
1e313f28
GM
6536 message_dolog (m, nbytes, 1, multibyte);
6537 message2_nolog (m, nbytes, multibyte);
5f5c8ee5
GM
6538}
6539
6540
6541/* The non-logging counterpart of message2. */
6542
6543void
1e313f28 6544message2_nolog (m, nbytes, multibyte)
50f80c2f 6545 const char *m;
e3383b6f 6546 int nbytes, multibyte;
5f5c8ee5 6547{
886bd6f2 6548 struct frame *sf = SELECTED_FRAME ();
5f5c8ee5
GM
6549 message_enable_multibyte = multibyte;
6550
6551 if (noninteractive)
6552 {
6553 if (noninteractive_need_newline)
6554 putc ('\n', stderr);
6555 noninteractive_need_newline = 0;
6556 if (m)
1e313f28 6557 fwrite (m, nbytes, 1, stderr);
5f5c8ee5
GM
6558 if (cursor_in_echo_area == 0)
6559 fprintf (stderr, "\n");
6560 fflush (stderr);
6561 }
6562 /* A null message buffer means that the frame hasn't really been
6563 initialized yet. Error messages get reported properly by
6564 cmd_error, so this must be just an informative message; toss it. */
2311178e 6565 else if (INTERACTIVE
886bd6f2
GM
6566 && sf->glyphs_initialized_p
6567 && FRAME_MESSAGE_BUF (sf))
5f5c8ee5
GM
6568 {
6569 Lisp_Object mini_window;
6570 struct frame *f;
6571
6572 /* Get the frame containing the mini-buffer
6573 that the selected frame is using. */
886bd6f2 6574 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
6575 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6576
6577 FRAME_SAMPLE_VISIBILITY (f);
886bd6f2 6578 if (FRAME_VISIBLE_P (sf)
5f5c8ee5
GM
6579 && ! FRAME_VISIBLE_P (f))
6580 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6581
6582 if (m)
6583 {
1e313f28 6584 set_message (m, Qnil, nbytes, multibyte);
5f5c8ee5
GM
6585 if (minibuffer_auto_raise)
6586 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6587 }
6588 else
c6e89d6c 6589 clear_message (1, 1);
5f5c8ee5 6590
c6e89d6c 6591 do_pending_window_change (0);
5f5c8ee5 6592 echo_area_display (1);
c6e89d6c 6593 do_pending_window_change (0);
5f5c8ee5
GM
6594 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6595 (*frame_up_to_date_hook) (f);
6596 }
6597}
6598
6599
c6e89d6c
GM
6600/* Display an echo area message M with a specified length of NBYTES
6601 bytes. The string may include null characters. If M is not a
5f5c8ee5
GM
6602 string, clear out any existing message, and let the mini-buffer
6603 text show through. */
6604
6605void
c6e89d6c 6606message3 (m, nbytes, multibyte)
5f5c8ee5 6607 Lisp_Object m;
c6e89d6c 6608 int nbytes;
5f5c8ee5
GM
6609 int multibyte;
6610{
6611 struct gcpro gcpro1;
6612
6613 GCPRO1 (m);
2311178e 6614
5f5c8ee5
GM
6615 /* First flush out any partial line written with print. */
6616 message_log_maybe_newline ();
6617 if (STRINGP (m))
2051c264 6618 message_dolog (SDATA (m), nbytes, 1, multibyte);
c6e89d6c 6619 message3_nolog (m, nbytes, multibyte);
5f5c8ee5
GM
6620
6621 UNGCPRO;
6622}
6623
6624
6625/* The non-logging version of message3. */
6626
6627void
c6e89d6c 6628message3_nolog (m, nbytes, multibyte)
5f5c8ee5 6629 Lisp_Object m;
c6e89d6c 6630 int nbytes, multibyte;
5f5c8ee5 6631{
886bd6f2 6632 struct frame *sf = SELECTED_FRAME ();
5f5c8ee5
GM
6633 message_enable_multibyte = multibyte;
6634
6635 if (noninteractive)
6636 {
6637 if (noninteractive_need_newline)
6638 putc ('\n', stderr);
6639 noninteractive_need_newline = 0;
6640 if (STRINGP (m))
2051c264 6641 fwrite (SDATA (m), nbytes, 1, stderr);
5f5c8ee5
GM
6642 if (cursor_in_echo_area == 0)
6643 fprintf (stderr, "\n");
6644 fflush (stderr);
6645 }
6646 /* A null message buffer means that the frame hasn't really been
6647 initialized yet. Error messages get reported properly by
6648 cmd_error, so this must be just an informative message; toss it. */
2311178e 6649 else if (INTERACTIVE
886bd6f2
GM
6650 && sf->glyphs_initialized_p
6651 && FRAME_MESSAGE_BUF (sf))
5f5c8ee5
GM
6652 {
6653 Lisp_Object mini_window;
c6e89d6c 6654 Lisp_Object frame;
5f5c8ee5
GM
6655 struct frame *f;
6656
6657 /* Get the frame containing the mini-buffer
6658 that the selected frame is using. */
886bd6f2 6659 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
6660 frame = XWINDOW (mini_window)->frame;
6661 f = XFRAME (frame);
5f5c8ee5
GM
6662
6663 FRAME_SAMPLE_VISIBILITY (f);
886bd6f2 6664 if (FRAME_VISIBLE_P (sf)
c6e89d6c
GM
6665 && !FRAME_VISIBLE_P (f))
6666 Fmake_frame_visible (frame);
5f5c8ee5 6667
2051c264 6668 if (STRINGP (m) && SCHARS (m) > 0)
5f5c8ee5 6669 {
c6e89d6c 6670 set_message (NULL, m, nbytes, multibyte);
468155d7
GM
6671 if (minibuffer_auto_raise)
6672 Fraise_frame (frame);
5f5c8ee5
GM
6673 }
6674 else
c6e89d6c 6675 clear_message (1, 1);
5f5c8ee5 6676
c6e89d6c 6677 do_pending_window_change (0);
5f5c8ee5 6678 echo_area_display (1);
c6e89d6c 6679 do_pending_window_change (0);
5f5c8ee5
GM
6680 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6681 (*frame_up_to_date_hook) (f);
6682 }
6683}
6684
6685
6686/* Display a null-terminated echo area message M. If M is 0, clear
6687 out any existing message, and let the mini-buffer text show through.
6688
6689 The buffer M must continue to exist until after the echo area gets
6690 cleared or some other message gets displayed there. Do not pass
6691 text that is stored in a Lisp string. Do not pass text in a buffer
6692 that was alloca'd. */
6693
6694void
6695message1 (m)
6696 char *m;
6697{
6698 message2 (m, (m ? strlen (m) : 0), 0);
6699}
6700
6701
6702/* The non-logging counterpart of message1. */
6703
6704void
6705message1_nolog (m)
6706 char *m;
6707{
6708 message2_nolog (m, (m ? strlen (m) : 0), 0);
6709}
6710
6711/* Display a message M which contains a single %s
6712 which gets replaced with STRING. */
6713
6714void
6715message_with_string (m, string, log)
6716 char *m;
6717 Lisp_Object string;
6718 int log;
6719{
5b6d51b6
RS
6720 CHECK_STRING (string);
6721
5f5c8ee5
GM
6722 if (noninteractive)
6723 {
6724 if (m)
6725 {
6726 if (noninteractive_need_newline)
6727 putc ('\n', stderr);
6728 noninteractive_need_newline = 0;
2051c264 6729 fprintf (stderr, m, SDATA (string));
5f5c8ee5
GM
6730 if (cursor_in_echo_area == 0)
6731 fprintf (stderr, "\n");
6732 fflush (stderr);
6733 }
6734 }
6735 else if (INTERACTIVE)
6736 {
6737 /* The frame whose minibuffer we're going to display the message on.
6738 It may be larger than the selected frame, so we need
6739 to use its buffer, not the selected frame's buffer. */
6740 Lisp_Object mini_window;
886bd6f2 6741 struct frame *f, *sf = SELECTED_FRAME ();
5f5c8ee5
GM
6742
6743 /* Get the frame containing the minibuffer
6744 that the selected frame is using. */
886bd6f2 6745 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
6746 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6747
6748 /* A null message buffer means that the frame hasn't really been
6749 initialized yet. Error messages get reported properly by
6750 cmd_error, so this must be just an informative message; toss it. */
6751 if (FRAME_MESSAGE_BUF (f))
6752 {
eb484132
GM
6753 Lisp_Object args[2], message;
6754 struct gcpro gcpro1, gcpro2;
5f5c8ee5 6755
eb484132
GM
6756 args[0] = build_string (m);
6757 args[1] = message = string;
78e17433 6758 GCPRO2 (args[0], message);
eb484132 6759 gcpro1.nvars = 2;
2311178e 6760
eb484132 6761 message = Fformat (2, args);
5f5c8ee5
GM
6762
6763 if (log)
d5db4077 6764 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
5f5c8ee5 6765 else
d5db4077 6766 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
eb484132
GM
6767
6768 UNGCPRO;
5f5c8ee5
GM
6769
6770 /* Print should start at the beginning of the message
6771 buffer next time. */
6772 message_buf_print = 0;
6773 }
6774 }
6775}
6776
6777
5f5c8ee5
GM
6778/* Dump an informative message to the minibuf. If M is 0, clear out
6779 any existing message, and let the mini-buffer text show through. */
6780
6781/* VARARGS 1 */
6782void
6783message (m, a1, a2, a3)
6784 char *m;
6785 EMACS_INT a1, a2, a3;
6786{
6787 if (noninteractive)
6788 {
6789 if (m)
6790 {
6791 if (noninteractive_need_newline)
6792 putc ('\n', stderr);
6793 noninteractive_need_newline = 0;
6794 fprintf (stderr, m, a1, a2, a3);
6795 if (cursor_in_echo_area == 0)
6796 fprintf (stderr, "\n");
6797 fflush (stderr);
6798 }
6799 }
6800 else if (INTERACTIVE)
6801 {
6802 /* The frame whose mini-buffer we're going to display the message
6803 on. It may be larger than the selected frame, so we need to
6804 use its buffer, not the selected frame's buffer. */
6805 Lisp_Object mini_window;
886bd6f2 6806 struct frame *f, *sf = SELECTED_FRAME ();
5f5c8ee5
GM
6807
6808 /* Get the frame containing the mini-buffer
6809 that the selected frame is using. */
886bd6f2 6810 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
6811 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6812
6813 /* A null message buffer means that the frame hasn't really been
6814 initialized yet. Error messages get reported properly by
6815 cmd_error, so this must be just an informative message; toss
6816 it. */
6817 if (FRAME_MESSAGE_BUF (f))
6818 {
6819 if (m)
6820 {
6821 int len;
6822#ifdef NO_ARG_ARRAY
6823 char *a[3];
6824 a[0] = (char *) a1;
6825 a[1] = (char *) a2;
6826 a[2] = (char *) a3;
6827
6828 len = doprnt (FRAME_MESSAGE_BUF (f),
6829 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6830#else
6831 len = doprnt (FRAME_MESSAGE_BUF (f),
6832 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6833 (char **) &a1);
6834#endif /* NO_ARG_ARRAY */
6835
6836 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6837 }
6838 else
6839 message1 (0);
6840
6841 /* Print should start at the beginning of the message
6842 buffer next time. */
6843 message_buf_print = 0;
6844 }
6845 }
6846}
6847
6848
6849/* The non-logging version of message. */
6850
6851void
6852message_nolog (m, a1, a2, a3)
6853 char *m;
6854 EMACS_INT a1, a2, a3;
6855{
6856 Lisp_Object old_log_max;
6857 old_log_max = Vmessage_log_max;
6858 Vmessage_log_max = Qnil;
6859 message (m, a1, a2, a3);
6860 Vmessage_log_max = old_log_max;
6861}
6862
6863
c6e89d6c
GM
6864/* Display the current message in the current mini-buffer. This is
6865 only called from error handlers in process.c, and is not time
6866 critical. */
5f5c8ee5
GM
6867
6868void
6869update_echo_area ()
6870{
c6e89d6c
GM
6871 if (!NILP (echo_area_buffer[0]))
6872 {
6873 Lisp_Object string;
6874 string = Fcurrent_message ();
2311178e 6875 message3 (string, SBYTES (string),
c6e89d6c
GM
6876 !NILP (current_buffer->enable_multibyte_characters));
6877 }
6878}
6879
6880
a67e162b
RS
6881/* Make sure echo area buffers in `echo_buffers' are live.
6882 If they aren't, make new ones. */
5bcfeb49
GM
6883
6884static void
6885ensure_echo_area_buffers ()
6886{
6887 int i;
6888
6889 for (i = 0; i < 2; ++i)
6890 if (!BUFFERP (echo_buffer[i])
6891 || NILP (XBUFFER (echo_buffer[i])->name))
6892 {
6893 char name[30];
ff3d9573
GM
6894 Lisp_Object old_buffer;
6895 int j;
6896
6897 old_buffer = echo_buffer[i];
5bcfeb49
GM
6898 sprintf (name, " *Echo Area %d*", i);
6899 echo_buffer[i] = Fget_buffer_create (build_string (name));
ad4f174e 6900 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
ff3d9573
GM
6901
6902 for (j = 0; j < 2; ++j)
6903 if (EQ (old_buffer, echo_area_buffer[j]))
6904 echo_area_buffer[j] = echo_buffer[i];
5bcfeb49
GM
6905 }
6906}
6907
6908
23a96c77 6909/* Call FN with args A1..A4 with either the current or last displayed
c6e89d6c
GM
6910 echo_area_buffer as current buffer.
6911
6912 WHICH zero means use the current message buffer
6913 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6914 from echo_buffer[] and clear it.
6915
6916 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6917 suitable buffer from echo_buffer[] and clear it.
6918
6919 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6920 that the current message becomes the last displayed one, make
6921 choose a suitable buffer for echo_area_buffer[0], and clear it.
6922
4b41cebb 6923 Value is what FN returns. */
c6e89d6c
GM
6924
6925static int
23a96c77 6926with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
c6e89d6c
GM
6927 struct window *w;
6928 int which;
23dd2d97
KR
6929 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6930 EMACS_INT a1;
6931 Lisp_Object a2;
6932 EMACS_INT a3, a4;
c6e89d6c
GM
6933{
6934 Lisp_Object buffer;
15e26c76 6935 int this_one, the_other, clear_buffer_p, rc;
331379bf 6936 int count = SPECPDL_INDEX ();
c6e89d6c 6937
9583b2bb 6938 /* If buffers aren't live, make new ones. */
5bcfeb49 6939 ensure_echo_area_buffers ();
c6e89d6c
GM
6940
6941 clear_buffer_p = 0;
2311178e 6942
c6e89d6c
GM
6943 if (which == 0)
6944 this_one = 0, the_other = 1;
6945 else if (which > 0)
6946 this_one = 1, the_other = 0;
5f5c8ee5 6947 else
c6e89d6c
GM
6948 {
6949 this_one = 0, the_other = 1;
6950 clear_buffer_p = 1;
2311178e 6951
c6e89d6c
GM
6952 /* We need a fresh one in case the current echo buffer equals
6953 the one containing the last displayed echo area message. */
6954 if (!NILP (echo_area_buffer[this_one])
6955 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6956 echo_area_buffer[this_one] = Qnil;
c6e89d6c
GM
6957 }
6958
6959 /* Choose a suitable buffer from echo_buffer[] is we don't
6960 have one. */
6961 if (NILP (echo_area_buffer[this_one]))
6962 {
6963 echo_area_buffer[this_one]
6964 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6965 ? echo_buffer[the_other]
6966 : echo_buffer[this_one]);
6967 clear_buffer_p = 1;
6968 }
6969
6970 buffer = echo_area_buffer[this_one];
6971
1013f4e3
GM
6972 /* Don't get confused by reusing the buffer used for echoing
6973 for a different purpose. */
032906b1 6974 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
1013f4e3
GM
6975 cancel_echoing ();
6976
c6e89d6c
GM
6977 record_unwind_protect (unwind_with_echo_area_buffer,
6978 with_echo_area_buffer_unwind_data (w));
6979
6980 /* Make the echo area buffer current. Note that for display
6981 purposes, it is not necessary that the displayed window's buffer
6982 == current_buffer, except for text property lookup. So, let's
6983 only set that buffer temporarily here without doing a full
6984 Fset_window_buffer. We must also change w->pointm, though,
6985 because otherwise an assertions in unshow_buffer fails, and Emacs
6986 aborts. */
9142dd5b 6987 set_buffer_internal_1 (XBUFFER (buffer));
c6e89d6c
GM
6988 if (w)
6989 {
6990 w->buffer = buffer;
6991 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6992 }
ad4f174e 6993
c6e89d6c
GM
6994 current_buffer->undo_list = Qt;
6995 current_buffer->read_only = Qnil;
bbbf6d06 6996 specbind (Qinhibit_read_only, Qt);
0b04fa5f 6997 specbind (Qinhibit_modification_hooks, Qt);
c6e89d6c
GM
6998
6999 if (clear_buffer_p && Z > BEG)
7000 del_range (BEG, Z);
7001
7002 xassert (BEGV >= BEG);
7003 xassert (ZV <= Z && ZV >= BEGV);
7004
23a96c77 7005 rc = fn (a1, a2, a3, a4);
c6e89d6c
GM
7006
7007 xassert (BEGV >= BEG);
7008 xassert (ZV <= Z && ZV >= BEGV);
7009
7010 unbind_to (count, Qnil);
7011 return rc;
5f5c8ee5
GM
7012}
7013
7014
c6e89d6c
GM
7015/* Save state that should be preserved around the call to the function
7016 FN called in with_echo_area_buffer. */
5f5c8ee5 7017
c6e89d6c
GM
7018static Lisp_Object
7019with_echo_area_buffer_unwind_data (w)
7020 struct window *w;
5f5c8ee5 7021{
c6e89d6c
GM
7022 int i = 0;
7023 Lisp_Object vector;
5f5c8ee5 7024
c6e89d6c
GM
7025 /* Reduce consing by keeping one vector in
7026 Vwith_echo_area_save_vector. */
7027 vector = Vwith_echo_area_save_vector;
7028 Vwith_echo_area_save_vector = Qnil;
2311178e 7029
c6e89d6c 7030 if (NILP (vector))
9142dd5b 7031 vector = Fmake_vector (make_number (7), Qnil);
2311178e 7032
a61b7058
GM
7033 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7034 AREF (vector, i) = Vdeactivate_mark, ++i;
7035 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
2311178e 7036
c6e89d6c
GM
7037 if (w)
7038 {
a61b7058
GM
7039 XSETWINDOW (AREF (vector, i), w); ++i;
7040 AREF (vector, i) = w->buffer; ++i;
7041 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7042 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
c6e89d6c
GM
7043 }
7044 else
7045 {
7046 int end = i + 4;
a61b7058
GM
7047 for (; i < end; ++i)
7048 AREF (vector, i) = Qnil;
c6e89d6c 7049 }
5f5c8ee5 7050
a61b7058 7051 xassert (i == ASIZE (vector));
c6e89d6c
GM
7052 return vector;
7053}
5f5c8ee5 7054
5f5c8ee5 7055
c6e89d6c
GM
7056/* Restore global state from VECTOR which was created by
7057 with_echo_area_buffer_unwind_data. */
7058
7059static Lisp_Object
7060unwind_with_echo_area_buffer (vector)
7061 Lisp_Object vector;
7062{
bbbf6d06
GM
7063 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7064 Vdeactivate_mark = AREF (vector, 1);
7065 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
c6e89d6c 7066
bbbf6d06 7067 if (WINDOWP (AREF (vector, 3)))
c6e89d6c
GM
7068 {
7069 struct window *w;
7070 Lisp_Object buffer, charpos, bytepos;
2311178e 7071
bbbf6d06
GM
7072 w = XWINDOW (AREF (vector, 3));
7073 buffer = AREF (vector, 4);
7074 charpos = AREF (vector, 5);
7075 bytepos = AREF (vector, 6);
2311178e 7076
c6e89d6c
GM
7077 w->buffer = buffer;
7078 set_marker_both (w->pointm, buffer,
7079 XFASTINT (charpos), XFASTINT (bytepos));
7080 }
7081
7082 Vwith_echo_area_save_vector = vector;
7083 return Qnil;
7084}
7085
7086
7087/* Set up the echo area for use by print functions. MULTIBYTE_P
7088 non-zero means we will print multibyte. */
7089
7090void
7091setup_echo_area_for_printing (multibyte_p)
7092 int multibyte_p;
7093{
03b0a4b4
RS
7094 /* If we can't find an echo area any more, exit. */
7095 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7096 Fkill_emacs (Qnil);
7097
5bcfeb49
GM
7098 ensure_echo_area_buffers ();
7099
c6e89d6c
GM
7100 if (!message_buf_print)
7101 {
7102 /* A message has been output since the last time we printed.
7103 Choose a fresh echo area buffer. */
7104 if (EQ (echo_area_buffer[1], echo_buffer[0]))
2311178e 7105 echo_area_buffer[0] = echo_buffer[1];
c6e89d6c
GM
7106 else
7107 echo_area_buffer[0] = echo_buffer[0];
7108
7109 /* Switch to that buffer and clear it. */
7110 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
ab2c5f0a 7111 current_buffer->truncate_lines = Qnil;
2311178e 7112
c6e89d6c 7113 if (Z > BEG)
bbbf6d06 7114 {
331379bf 7115 int count = SPECPDL_INDEX ();
bbbf6d06 7116 specbind (Qinhibit_read_only, Qt);
a67e162b 7117 /* Note that undo recording is always disabled. */
bbbf6d06
GM
7118 del_range (BEG, Z);
7119 unbind_to (count, Qnil);
7120 }
c6e89d6c
GM
7121 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7122
7123 /* Set up the buffer for the multibyteness we need. */
7124 if (multibyte_p
7125 != !NILP (current_buffer->enable_multibyte_characters))
7126 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7127
7128 /* Raise the frame containing the echo area. */
7129 if (minibuffer_auto_raise)
7130 {
886bd6f2 7131 struct frame *sf = SELECTED_FRAME ();
c6e89d6c 7132 Lisp_Object mini_window;
886bd6f2 7133 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
7134 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7135 }
7136
8a4e3c0c 7137 message_log_maybe_newline ();
c6e89d6c
GM
7138 message_buf_print = 1;
7139 }
fa77249f
GM
7140 else
7141 {
7142 if (NILP (echo_area_buffer[0]))
7143 {
7144 if (EQ (echo_area_buffer[1], echo_buffer[0]))
2311178e 7145 echo_area_buffer[0] = echo_buffer[1];
fa77249f
GM
7146 else
7147 echo_area_buffer[0] = echo_buffer[0];
7148 }
2311178e 7149
fa77249f 7150 if (current_buffer != XBUFFER (echo_area_buffer[0]))
ab2c5f0a
GM
7151 {
7152 /* Someone switched buffers between print requests. */
7153 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7154 current_buffer->truncate_lines = Qnil;
7155 }
fa77249f 7156 }
c6e89d6c
GM
7157}
7158
7159
dd2eb166
GM
7160/* Display an echo area message in window W. Value is non-zero if W's
7161 height is changed. If display_last_displayed_message_p is
7162 non-zero, display the message that was last displayed, otherwise
7163 display the current message. */
c6e89d6c
GM
7164
7165static int
7166display_echo_area (w)
7167 struct window *w;
7168{
25edb08f
GM
7169 int i, no_message_p, window_height_changed_p, count;
7170
7171 /* Temporarily disable garbage collections while displaying the echo
7172 area. This is done because a GC can print a message itself.
7173 That message would modify the echo area buffer's contents while a
7174 redisplay of the buffer is going on, and seriously confuse
7175 redisplay. */
7176 count = inhibit_garbage_collection ();
dd2eb166
GM
7177
7178 /* If there is no message, we must call display_echo_area_1
7179 nevertheless because it resizes the window. But we will have to
7180 reset the echo_area_buffer in question to nil at the end because
7181 with_echo_area_buffer will sets it to an empty buffer. */
7182 i = display_last_displayed_message_p ? 1 : 0;
7183 no_message_p = NILP (echo_area_buffer[i]);
2311178e 7184
dd2eb166
GM
7185 window_height_changed_p
7186 = with_echo_area_buffer (w, display_last_displayed_message_p,
23a96c77 7187 display_echo_area_1,
23dd2d97 7188 (EMACS_INT) w, Qnil, 0, 0);
dd2eb166
GM
7189
7190 if (no_message_p)
7191 echo_area_buffer[i] = Qnil;
25edb08f
GM
7192
7193 unbind_to (count, Qnil);
dd2eb166 7194 return window_height_changed_p;
c6e89d6c
GM
7195}
7196
7197
7198/* Helper for display_echo_area. Display the current buffer which
23a96c77
GM
7199 contains the current echo area message in window W, a mini-window,
7200 a pointer to which is passed in A1. A2..A4 are currently not used.
c6e89d6c
GM
7201 Change the height of W so that all of the message is displayed.
7202 Value is non-zero if height of W was changed. */
7203
7204static int
23a96c77 7205display_echo_area_1 (a1, a2, a3, a4)
23dd2d97
KR
7206 EMACS_INT a1;
7207 Lisp_Object a2;
7208 EMACS_INT a3, a4;
c6e89d6c 7209{
23a96c77 7210 struct window *w = (struct window *) a1;
c6e89d6c 7211 Lisp_Object window;
c6e89d6c
GM
7212 struct text_pos start;
7213 int window_height_changed_p = 0;
7214
7215 /* Do this before displaying, so that we have a large enough glyph
7216 matrix for the display. */
92a90e89 7217 window_height_changed_p = resize_mini_window (w, 0);
c6e89d6c
GM
7218
7219 /* Display. */
7220 clear_glyph_matrix (w->desired_matrix);
7221 XSETWINDOW (window, w);
7222 SET_TEXT_POS (start, BEG, BEG_BYTE);
7223 try_window (window, start);
7224
c6e89d6c
GM
7225 return window_height_changed_p;
7226}
7227
7228
92a90e89 7229/* Resize the echo area window to exactly the size needed for the
6d004fea
GM
7230 currently displayed message, if there is one. If a mini-buffer
7231 is active, don't shrink it. */
92a90e89
GM
7232
7233void
308a74d8 7234resize_echo_area_exactly ()
92a90e89
GM
7235{
7236 if (BUFFERP (echo_area_buffer[0])
7237 && WINDOWP (echo_area_window))
7238 {
7239 struct window *w = XWINDOW (echo_area_window);
7240 int resized_p;
6d004fea
GM
7241 Lisp_Object resize_exactly;
7242
7243 if (minibuf_level == 0)
7244 resize_exactly = Qt;
7245 else
7246 resize_exactly = Qnil;
2311178e 7247
23a96c77 7248 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6d004fea 7249 (EMACS_INT) w, resize_exactly, 0, 0);
92a90e89
GM
7250 if (resized_p)
7251 {
7252 ++windows_or_buffers_changed;
7253 ++update_mode_lines;
7254 redisplay_internal (0);
7255 }
7256 }
7257}
7258
7259
23a96c77 7260/* Callback function for with_echo_area_buffer, when used from
308a74d8 7261 resize_echo_area_exactly. A1 contains a pointer to the window to
6d004fea
GM
7262 resize, EXACTLY non-nil means resize the mini-window exactly to the
7263 size of the text displayed. A3 and A4 are not used. Value is what
7264 resize_mini_window returns. */
23a96c77
GM
7265
7266static int
6d004fea 7267resize_mini_window_1 (a1, exactly, a3, a4)
23dd2d97 7268 EMACS_INT a1;
6d004fea 7269 Lisp_Object exactly;
23dd2d97 7270 EMACS_INT a3, a4;
23a96c77 7271{
6d004fea 7272 return resize_mini_window ((struct window *) a1, !NILP (exactly));
23a96c77
GM
7273}
7274
7275
92a90e89
GM
7276/* Resize mini-window W to fit the size of its contents. EXACT:P
7277 means size the window exactly to the size needed. Otherwise, it's
7278 only enlarged until W's buffer is empty. Value is non-zero if
4b41cebb 7279 the window height has been changed. */
c6e89d6c 7280
9472f927 7281int
92a90e89 7282resize_mini_window (w, exact_p)
c6e89d6c 7283 struct window *w;
92a90e89 7284 int exact_p;
c6e89d6c
GM
7285{
7286 struct frame *f = XFRAME (w->frame);
7287 int window_height_changed_p = 0;
7288
7289 xassert (MINI_WINDOW_P (w));
97cafc0f 7290
2913a9c0
GM
7291 /* Don't resize windows while redisplaying a window; it would
7292 confuse redisplay functions when the size of the window they are
7293 displaying changes from under them. Such a resizing can happen,
7294 for instance, when which-func prints a long message while
7295 we are running fontification-functions. We're running these
7296 functions with safe_call which binds inhibit-redisplay to t. */
7297 if (!NILP (Vinhibit_redisplay))
64c5be50 7298 return 0;
2311178e 7299
97cafc0f 7300 /* Nil means don't try to resize. */
6422c1d7 7301 if (NILP (Vresize_mini_windows)
fa3c6b4d 7302 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
97cafc0f 7303 return 0;
2311178e 7304
c6e89d6c
GM
7305 if (!FRAME_MINIBUF_ONLY_P (f))
7306 {
7307 struct it it;
dd2eb166 7308 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
da8b7f4f 7309 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
dd2eb166 7310 int height, max_height;
da8b7f4f 7311 int unit = FRAME_LINE_HEIGHT (f);
dd2eb166 7312 struct text_pos start;
1bfdbe43
GM
7313 struct buffer *old_current_buffer = NULL;
7314
7315 if (current_buffer != XBUFFER (w->buffer))
7316 {
7317 old_current_buffer = current_buffer;
7318 set_buffer_internal (XBUFFER (w->buffer));
7319 }
9142dd5b 7320
c6e89d6c 7321 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
c6e89d6c 7322
dd2eb166
GM
7323 /* Compute the max. number of lines specified by the user. */
7324 if (FLOATP (Vmax_mini_window_height))
da8b7f4f 7325 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
dd2eb166
GM
7326 else if (INTEGERP (Vmax_mini_window_height))
7327 max_height = XINT (Vmax_mini_window_height);
97cafc0f
GM
7328 else
7329 max_height = total_height / 4;
2311178e 7330
4b41cebb 7331 /* Correct that max. height if it's bogus. */
dd2eb166
GM
7332 max_height = max (1, max_height);
7333 max_height = min (total_height, max_height);
2311178e 7334
dd2eb166 7335 /* Find out the height of the text in the window. */
ad4f174e
GM
7336 if (it.truncate_lines_p)
7337 height = 1;
55b064bd 7338 else
ad4f174e
GM
7339 {
7340 last_height = 0;
7341 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7342 if (it.max_ascent == 0 && it.max_descent == 0)
7343 height = it.current_y + last_height;
7344 else
7345 height = it.current_y + it.max_ascent + it.max_descent;
3c4b7685 7346 height -= it.extra_line_spacing;
ad4f174e
GM
7347 height = (height + unit - 1) / unit;
7348 }
2311178e 7349
dd2eb166
GM
7350 /* Compute a suitable window start. */
7351 if (height > max_height)
7352 {
7353 height = max_height;
7354 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7355 move_it_vertically_backward (&it, (height - 1) * unit);
7356 start = it.current.pos;
7357 }
7358 else
7359 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7360 SET_MARKER_FROM_TEXT_POS (w->start, start);
c59c668a 7361
6422c1d7 7362 if (EQ (Vresize_mini_windows, Qgrow_only))
dd2eb166 7363 {
6422c1d7
GM
7364 /* Let it grow only, until we display an empty message, in which
7365 case the window shrinks again. */
da8b7f4f 7366 if (height > WINDOW_TOTAL_LINES (w))
6422c1d7 7367 {
da8b7f4f 7368 int old_height = WINDOW_TOTAL_LINES (w);
6422c1d7 7369 freeze_window_starts (f, 1);
da8b7f4f
KS
7370 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7371 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
6422c1d7 7372 }
da8b7f4f 7373 else if (height < WINDOW_TOTAL_LINES (w)
6422c1d7
GM
7374 && (exact_p || BEGV == ZV))
7375 {
da8b7f4f 7376 int old_height = WINDOW_TOTAL_LINES (w);
6422c1d7
GM
7377 freeze_window_starts (f, 0);
7378 shrink_mini_window (w);
da8b7f4f 7379 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
6422c1d7 7380 }
da448723 7381 }
2311178e 7382 else
da448723 7383 {
6422c1d7 7384 /* Always resize to exact size needed. */
da8b7f4f 7385 if (height > WINDOW_TOTAL_LINES (w))
6422c1d7 7386 {
da8b7f4f 7387 int old_height = WINDOW_TOTAL_LINES (w);
6422c1d7 7388 freeze_window_starts (f, 1);
da8b7f4f
KS
7389 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7390 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
6422c1d7 7391 }
da8b7f4f 7392 else if (height < WINDOW_TOTAL_LINES (w))
6422c1d7 7393 {
da8b7f4f 7394 int old_height = WINDOW_TOTAL_LINES (w);
6422c1d7
GM
7395 freeze_window_starts (f, 0);
7396 shrink_mini_window (w);
7397
7398 if (height)
7399 {
7400 freeze_window_starts (f, 1);
da8b7f4f 7401 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
6422c1d7 7402 }
2311178e 7403
da8b7f4f 7404 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
6422c1d7 7405 }
9142dd5b 7406 }
1bfdbe43
GM
7407
7408 if (old_current_buffer)
7409 set_buffer_internal (old_current_buffer);
c6e89d6c
GM
7410 }
7411
7412 return window_height_changed_p;
7413}
7414
7415
7416/* Value is the current message, a string, or nil if there is no
7417 current message. */
7418
7419Lisp_Object
7420current_message ()
7421{
7422 Lisp_Object msg;
7423
7424 if (NILP (echo_area_buffer[0]))
7425 msg = Qnil;
7426 else
7427 {
23a96c77 7428 with_echo_area_buffer (0, 0, current_message_1,
23dd2d97 7429 (EMACS_INT) &msg, Qnil, 0, 0);
c6e89d6c
GM
7430 if (NILP (msg))
7431 echo_area_buffer[0] = Qnil;
7432 }
2311178e 7433
c6e89d6c
GM
7434 return msg;
7435}
7436
7437
7438static int
23a96c77 7439current_message_1 (a1, a2, a3, a4)
23dd2d97
KR
7440 EMACS_INT a1;
7441 Lisp_Object a2;
7442 EMACS_INT a3, a4;
c6e89d6c 7443{
23a96c77 7444 Lisp_Object *msg = (Lisp_Object *) a1;
2311178e 7445
c6e89d6c
GM
7446 if (Z > BEG)
7447 *msg = make_buffer_string (BEG, Z, 1);
7448 else
7449 *msg = Qnil;
7450 return 0;
7451}
7452
7453
7454/* Push the current message on Vmessage_stack for later restauration
7455 by restore_message. Value is non-zero if the current message isn't
7456 empty. This is a relatively infrequent operation, so it's not
7457 worth optimizing. */
7458
7459int
7460push_message ()
7461{
7462 Lisp_Object msg;
7463 msg = current_message ();
7464 Vmessage_stack = Fcons (msg, Vmessage_stack);
7465 return STRINGP (msg);
7466}
7467
7468
7469/* Restore message display from the top of Vmessage_stack. */
7470
7471void
7472restore_message ()
7473{
7474 Lisp_Object msg;
2311178e 7475
c6e89d6c
GM
7476 xassert (CONSP (Vmessage_stack));
7477 msg = XCAR (Vmessage_stack);
7478 if (STRINGP (msg))
d5db4077 7479 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
c6e89d6c
GM
7480 else
7481 message3_nolog (msg, 0, 0);
7482}
7483
7484
37d66095
RS
7485/* Handler for record_unwind_protect calling pop_message. */
7486
7487Lisp_Object
7488pop_message_unwind (dummy)
7489 Lisp_Object dummy;
7490{
7491 pop_message ();
7492 return Qnil;
7493}
7494
c6e89d6c
GM
7495/* Pop the top-most entry off Vmessage_stack. */
7496
7497void
7498pop_message ()
7499{
7500 xassert (CONSP (Vmessage_stack));
7501 Vmessage_stack = XCDR (Vmessage_stack);
7502}
7503
7504
7505/* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7506 exits. If the stack is not empty, we have a missing pop_message
7507 somewhere. */
7508
7509void
7510check_message_stack ()
7511{
7512 if (!NILP (Vmessage_stack))
7513 abort ();
7514}
7515
7516
7517/* Truncate to NCHARS what will be displayed in the echo area the next
7518 time we display it---but don't redisplay it now. */
7519
7520void
7521truncate_echo_area (nchars)
7522 int nchars;
7523{
7524 if (nchars == 0)
7525 echo_area_buffer[0] = Qnil;
7526 /* A null message buffer means that the frame hasn't really been
7527 initialized yet. Error messages get reported properly by
7528 cmd_error, so this must be just an informative message; toss it. */
7529 else if (!noninteractive
7530 && INTERACTIVE
c6e89d6c 7531 && !NILP (echo_area_buffer[0]))
886bd6f2
GM
7532 {
7533 struct frame *sf = SELECTED_FRAME ();
7534 if (FRAME_MESSAGE_BUF (sf))
23dd2d97 7535 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
886bd6f2 7536 }
c6e89d6c
GM
7537}
7538
7539
7540/* Helper function for truncate_echo_area. Truncate the current
7541 message to at most NCHARS characters. */
7542
7543static int
23a96c77 7544truncate_message_1 (nchars, a2, a3, a4)
23dd2d97
KR
7545 EMACS_INT nchars;
7546 Lisp_Object a2;
7547 EMACS_INT a3, a4;
c6e89d6c
GM
7548{
7549 if (BEG + nchars < Z)
7550 del_range (BEG + nchars, Z);
7551 if (Z == BEG)
7552 echo_area_buffer[0] = Qnil;
7553 return 0;
7554}
7555
7556
7557/* Set the current message to a substring of S or STRING.
7558
7559 If STRING is a Lisp string, set the message to the first NBYTES
7560 bytes from STRING. NBYTES zero means use the whole string. If
7561 STRING is multibyte, the message will be displayed multibyte.
7562
7563 If S is not null, set the message to the first LEN bytes of S. LEN
7564 zero means use the whole string. MULTIBYTE_P non-zero means S is
7565 multibyte. Display the message multibyte in that case. */
7566
7567void
7568set_message (s, string, nbytes, multibyte_p)
50f80c2f 7569 const char *s;
c6e89d6c 7570 Lisp_Object string;
e3383b6f 7571 int nbytes, multibyte_p;
c6e89d6c
GM
7572{
7573 message_enable_multibyte
7574 = ((s && multibyte_p)
7575 || (STRINGP (string) && STRING_MULTIBYTE (string)));
2311178e 7576
23a96c77
GM
7577 with_echo_area_buffer (0, -1, set_message_1,
7578 (EMACS_INT) s, string, nbytes, multibyte_p);
c6e89d6c 7579 message_buf_print = 0;
21fdfb65 7580 help_echo_showing_p = 0;
c6e89d6c
GM
7581}
7582
7583
7584/* Helper function for set_message. Arguments have the same meaning
23a96c77
GM
7585 as there, with A1 corresponding to S and A2 corresponding to STRING
7586 This function is called with the echo area buffer being
c6e89d6c
GM
7587 current. */
7588
7589static int
23a96c77 7590set_message_1 (a1, a2, nbytes, multibyte_p)
23dd2d97
KR
7591 EMACS_INT a1;
7592 Lisp_Object a2;
7593 EMACS_INT nbytes, multibyte_p;
c6e89d6c 7594{
50f80c2f 7595 const char *s = (const char *) a1;
23dd2d97 7596 Lisp_Object string = a2;
2311178e 7597
c6e89d6c 7598 xassert (BEG == Z);
2311178e 7599
c6e89d6c
GM
7600 /* Change multibyteness of the echo buffer appropriately. */
7601 if (message_enable_multibyte
7602 != !NILP (current_buffer->enable_multibyte_characters))
7603 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7604
ad4f174e 7605 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
2311178e 7606
c6e89d6c
GM
7607 /* Insert new message at BEG. */
7608 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7609
7610 if (STRINGP (string))
7611 {
7612 int nchars;
2311178e 7613
c6e89d6c 7614 if (nbytes == 0)
2051c264 7615 nbytes = SBYTES (string);
c6e89d6c 7616 nchars = string_byte_to_char (string, nbytes);
2311178e 7617
c6e89d6c
GM
7618 /* This function takes care of single/multibyte conversion. We
7619 just have to ensure that the echo area buffer has the right
7620 setting of enable_multibyte_characters. */
7621 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7622 }
7623 else if (s)
7624 {
7625 if (nbytes == 0)
7626 nbytes = strlen (s);
2311178e 7627
c6e89d6c
GM
7628 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7629 {
7630 /* Convert from multi-byte to single-byte. */
7631 int i, c, n;
7632 unsigned char work[1];
2311178e 7633
c6e89d6c
GM
7634 /* Convert a multibyte string to single-byte. */
7635 for (i = 0; i < nbytes; i += n)
7636 {
7637 c = string_char_and_length (s + i, nbytes - i, &n);
7638 work[0] = (SINGLE_BYTE_CHAR_P (c)
7639 ? c
7640 : multibyte_char_to_unibyte (c, Qnil));
7641 insert_1_both (work, 1, 1, 1, 0, 0);
7642 }
7643 }
7644 else if (!multibyte_p
7645 && !NILP (current_buffer->enable_multibyte_characters))
7646 {
7647 /* Convert from single-byte to multi-byte. */
7648 int i, c, n;
50f80c2f 7649 const unsigned char *msg = (const unsigned char *) s;
260a86a0 7650 unsigned char str[MAX_MULTIBYTE_LENGTH];
2311178e 7651
c6e89d6c
GM
7652 /* Convert a single-byte string to multibyte. */
7653 for (i = 0; i < nbytes; i++)
7654 {
7655 c = unibyte_char_to_multibyte (msg[i]);
260a86a0
KH
7656 n = CHAR_STRING (c, str);
7657 insert_1_both (str, 1, n, 1, 0, 0);
c6e89d6c
GM
7658 }
7659 }
7660 else
7661 insert_1 (s, nbytes, 1, 0, 0);
7662 }
7663
7664 return 0;
7665}
7666
7667
7668/* Clear messages. CURRENT_P non-zero means clear the current
7669 message. LAST_DISPLAYED_P non-zero means clear the message
7670 last displayed. */
7671
7672void
7673clear_message (current_p, last_displayed_p)
7674 int current_p, last_displayed_p;
7675{
7676 if (current_p)
6e019995
GM
7677 {
7678 echo_area_buffer[0] = Qnil;
7679 message_cleared_p = 1;
7680 }
2311178e 7681
c6e89d6c
GM
7682 if (last_displayed_p)
7683 echo_area_buffer[1] = Qnil;
2311178e 7684
c6e89d6c
GM
7685 message_buf_print = 0;
7686}
7687
7688/* Clear garbaged frames.
7689
7690 This function is used where the old redisplay called
7691 redraw_garbaged_frames which in turn called redraw_frame which in
7692 turn called clear_frame. The call to clear_frame was a source of
7693 flickering. I believe a clear_frame is not necessary. It should
7694 suffice in the new redisplay to invalidate all current matrices,
7695 and ensure a complete redisplay of all windows. */
7696
7697static void
7698clear_garbaged_frames ()
7699{
5f5c8ee5
GM
7700 if (frame_garbaged)
7701 {
5f5c8ee5 7702 Lisp_Object tail, frame;
5fb96e96 7703 int changed_count = 0;
2311178e 7704
5f5c8ee5
GM
7705 FOR_EACH_FRAME (tail, frame)
7706 {
7707 struct frame *f = XFRAME (frame);
2311178e 7708
5f5c8ee5
GM
7709 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7710 {
6bb95882 7711 if (f->resized_p)
6d925726
KS
7712 {
7713 Fredraw_frame (frame);
7714 f->force_flush_display_p = 1;
7715 }
5f5c8ee5 7716 clear_current_matrices (f);
5fb96e96 7717 changed_count++;
5f5c8ee5 7718 f->garbaged = 0;
6bb95882 7719 f->resized_p = 0;
5f5c8ee5
GM
7720 }
7721 }
7722
7723 frame_garbaged = 0;
5fb96e96
RS
7724 if (changed_count)
7725 ++windows_or_buffers_changed;
5f5c8ee5 7726 }
c6e89d6c 7727}
5f5c8ee5 7728
5f5c8ee5 7729
886bd6f2
GM
7730/* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7731 is non-zero update selected_frame. Value is non-zero if the
c6e89d6c 7732 mini-windows height has been changed. */
5f5c8ee5 7733
c6e89d6c
GM
7734static int
7735echo_area_display (update_frame_p)
7736 int update_frame_p;
7737{
7738 Lisp_Object mini_window;
7739 struct window *w;
7740 struct frame *f;
7741 int window_height_changed_p = 0;
886bd6f2 7742 struct frame *sf = SELECTED_FRAME ();
c6e89d6c 7743
886bd6f2 7744 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
7745 w = XWINDOW (mini_window);
7746 f = XFRAME (WINDOW_FRAME (w));
7747
7748 /* Don't display if frame is invisible or not yet initialized. */
7749 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7750 return 0;
5f5c8ee5 7751
1a578e9b 7752/* The terminal frame is used as the first Emacs frame on the Mac OS. */
e0f712ba 7753#ifndef MAC_OS8
1ab3e082 7754#ifdef HAVE_WINDOW_SYSTEM
c6e89d6c
GM
7755 /* When Emacs starts, selected_frame may be a visible terminal
7756 frame, even if we run under a window system. If we let this
7757 through, a message would be displayed on the terminal. */
2311178e 7758 if (EQ (selected_frame, Vterminal_frame)
622e3754 7759 && !NILP (Vwindow_system))
c6e89d6c 7760 return 0;
1ab3e082 7761#endif /* HAVE_WINDOW_SYSTEM */
1a578e9b 7762#endif
c6e89d6c
GM
7763
7764 /* Redraw garbaged frames. */
7765 if (frame_garbaged)
7766 clear_garbaged_frames ();
7767
7768 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7769 {
7770 echo_area_window = mini_window;
7771 window_height_changed_p = display_echo_area (w);
5f5c8ee5 7772 w->must_be_updated_p = 1;
c59c668a 7773
d4358b37
GM
7774 /* Update the display, unless called from redisplay_internal.
7775 Also don't update the screen during redisplay itself. The
7776 update will happen at the end of redisplay, and an update
7777 here could cause confusion. */
7778 if (update_frame_p && !redisplaying_p)
5f5c8ee5 7779 {
715e84c9 7780 int n = 0;
edc68111 7781
715e84c9
GM
7782 /* If the display update has been interrupted by pending
7783 input, update mode lines in the frame. Due to the
7784 pending input, it might have been that redisplay hasn't
7785 been called, so that mode lines above the echo area are
7786 garbaged. This looks odd, so we prevent it here. */
7787 if (!display_completed)
7788 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
2311178e 7789
8af1308e
GM
7790 if (window_height_changed_p
7791 /* Don't do this if Emacs is shutting down. Redisplay
7792 needs to run hooks. */
7793 && !NILP (Vrun_hooks))
c59c668a 7794 {
c06017fb
GM
7795 /* Must update other windows. Likewise as in other
7796 cases, don't let this update be interrupted by
7797 pending input. */
331379bf 7798 int count = SPECPDL_INDEX ();
c06017fb 7799 specbind (Qredisplay_dont_pause, Qt);
edc68111 7800 windows_or_buffers_changed = 1;
c59c668a 7801 redisplay_internal (0);
c06017fb 7802 unbind_to (count, Qnil);
c59c668a 7803 }
715e84c9 7804 else if (FRAME_WINDOW_P (f) && n == 0)
5f5c8ee5 7805 {
edc68111 7806 /* Window configuration is the same as before.
715e84c9
GM
7807 Can do with a display update of the echo area,
7808 unless we displayed some mode lines. */
5f5c8ee5
GM
7809 update_single_window (w, 1);
7810 rif->flush_display (f);
7811 }
7812 else
7813 update_frame (f, 1, 1);
31b6671b
GM
7814
7815 /* If cursor is in the echo area, make sure that the next
7816 redisplay displays the minibuffer, so that the cursor will
7817 be replaced with what the minibuffer wants. */
7818 if (cursor_in_echo_area)
7819 ++windows_or_buffers_changed;
5f5c8ee5
GM
7820 }
7821 }
7822 else if (!EQ (mini_window, selected_window))
7823 windows_or_buffers_changed++;
c59c668a
GM
7824
7825 /* Last displayed message is now the current message. */
dd2eb166 7826 echo_area_buffer[1] = echo_area_buffer[0];
2311178e 7827
5f5c8ee5
GM
7828 /* Prevent redisplay optimization in redisplay_internal by resetting
7829 this_line_start_pos. This is done because the mini-buffer now
7830 displays the message instead of its buffer text. */
7831 if (EQ (mini_window, selected_window))
7832 CHARPOS (this_line_start_pos) = 0;
c6e89d6c
GM
7833
7834 return window_height_changed_p;
5f5c8ee5
GM
7835}
7836
7837
7838\f
7839/***********************************************************************
7840 Frame Titles
7841 ***********************************************************************/
7842
7843
8143e6ab
KS
7844/* The frame title buffering code is also used by Fformat_mode_line.
7845 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
5f5c8ee5
GM
7846
7847/* A buffer for constructing frame titles in it; allocated from the
7848 heap in init_xdisp and resized as needed in store_frame_title_char. */
7849
7850static char *frame_title_buf;
7851
7852/* The buffer's end, and a current output position in it. */
7853
7854static char *frame_title_buf_end;
7855static char *frame_title_ptr;
7856
7857
7858/* Store a single character C for the frame title in frame_title_buf.
7859 Re-allocate frame_title_buf if necessary. */
7860
7861static void
5d15cc8f
DL
7862#ifdef PROTOTYPES
7863store_frame_title_char (char c)
7864#else
5f5c8ee5
GM
7865store_frame_title_char (c)
7866 char c;
5d15cc8f 7867#endif
5f5c8ee5
GM
7868{
7869 /* If output position has reached the end of the allocated buffer,
7870 double the buffer's size. */
7871 if (frame_title_ptr == frame_title_buf_end)
7872 {
7873 int len = frame_title_ptr - frame_title_buf;
7874 int new_size = 2 * len * sizeof *frame_title_buf;
7875 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7876 frame_title_buf_end = frame_title_buf + new_size;
7877 frame_title_ptr = frame_title_buf + len;
7878 }
7879
7880 *frame_title_ptr++ = c;
7881}
7882
7883
7884/* Store part of a frame title in frame_title_buf, beginning at
d26b89b8
KH
7885 frame_title_ptr. STR is the string to store. Do not copy
7886 characters that yield more columns than PRECISION; PRECISION <= 0
7887 means copy the whole string. Pad with spaces until FIELD_WIDTH
7888 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7889 pad. Called from display_mode_element when it is used to build a
7890 frame title. */
5f5c8ee5
GM
7891
7892static int
7893store_frame_title (str, field_width, precision)
50f80c2f 7894 const unsigned char *str;
5f5c8ee5
GM
7895 int field_width, precision;
7896{
7897 int n = 0;
3b552d56 7898 int dummy, nbytes;
5f5c8ee5
GM
7899
7900 /* Copy at most PRECISION chars from STR. */
d26b89b8
KH
7901 nbytes = strlen (str);
7902 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7903 while (nbytes--)
7904 store_frame_title_char (*str++);
5f5c8ee5
GM
7905
7906 /* Fill up with spaces until FIELD_WIDTH reached. */
7907 while (field_width > 0
7908 && n < field_width)
7909 {
7910 store_frame_title_char (' ');
7911 ++n;
7912 }
7913
7914 return n;
7915}
7916
8143e6ab 7917#ifdef HAVE_WINDOW_SYSTEM
5f5c8ee5
GM
7918
7919/* Set the title of FRAME, if it has changed. The title format is
7920 Vicon_title_format if FRAME is iconified, otherwise it is
7921 frame_title_format. */
7922
7923static void
7924x_consider_frame_title (frame)
7925 Lisp_Object frame;
7926{
7927 struct frame *f = XFRAME (frame);
7928
7929 if (FRAME_WINDOW_P (f)
7930 || FRAME_MINIBUF_ONLY_P (f)
7931 || f->explicit_name)
7932 {
7933 /* Do we have more than one visible frame on this X display? */
7934 Lisp_Object tail;
7935 Lisp_Object fmt;
7936 struct buffer *obuf;
7937 int len;
7938 struct it it;
7939
9472f927 7940 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
5f5c8ee5 7941 {
74779f52
JR
7942 Lisp_Object other_frame = XCAR (tail);
7943 struct frame *tf = XFRAME (other_frame);
5f5c8ee5 7944
2311178e 7945 if (tf != f
5f5c8ee5
GM
7946 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7947 && !FRAME_MINIBUF_ONLY_P (tf)
74779f52 7948 && !EQ (other_frame, tip_frame)
5f5c8ee5
GM
7949 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7950 break;
7951 }
7952
7953 /* Set global variable indicating that multiple frames exist. */
7954 multiple_frames = CONSP (tail);
7955
7956 /* Switch to the buffer of selected window of the frame. Set up
7957 frame_title_ptr so that display_mode_element will output into it;
7958 then display the title. */
7959 obuf = current_buffer;
bb336f8d 7960 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
5f5c8ee5
GM
7961 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7962 frame_title_ptr = frame_title_buf;
7963 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7964 NULL, DEFAULT_FACE_ID);
c53a1624 7965 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
d26b89b8 7966 len = frame_title_ptr - frame_title_buf;
5f5c8ee5 7967 frame_title_ptr = NULL;
bb336f8d 7968 set_buffer_internal_1 (obuf);
5f5c8ee5
GM
7969
7970 /* Set the title only if it's changed. This avoids consing in
7971 the common case where it hasn't. (If it turns out that we've
7972 already wasted too much time by walking through the list with
7973 display_mode_element, then we might need to optimize at a
7974 higher level than this.) */
2311178e 7975 if (! STRINGP (f->name)
2051c264
GM
7976 || SBYTES (f->name) != len
7977 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
5f5c8ee5
GM
7978 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7979 }
7980}
7981
5f5c8ee5
GM
7982#endif /* not HAVE_WINDOW_SYSTEM */
7983
7984
7985
7986\f
7987/***********************************************************************
7988 Menu Bars
7989 ***********************************************************************/
7990
7991
7992/* Prepare for redisplay by updating menu-bar item lists when
7993 appropriate. This can call eval. */
7994
7995void
7996prepare_menu_bars ()
7997{
7998 int all_windows;
7999 struct gcpro gcpro1, gcpro2;
8000 struct frame *f;
6b5c4794 8001 Lisp_Object tooltip_frame;
5f5c8ee5 8002
86ffe5cd 8003#ifdef HAVE_WINDOW_SYSTEM
5f5c8ee5
GM
8004 tooltip_frame = tip_frame;
8005#else
6b5c4794 8006 tooltip_frame = Qnil;
5f5c8ee5
GM
8007#endif
8008
8009 /* Update all frame titles based on their buffer names, etc. We do
8010 this before the menu bars so that the buffer-menu will show the
8011 up-to-date frame titles. */
8012#ifdef HAVE_WINDOW_SYSTEM
8013 if (windows_or_buffers_changed || update_mode_lines)
8014 {
8015 Lisp_Object tail, frame;
8016
8017 FOR_EACH_FRAME (tail, frame)
8018 {
8019 f = XFRAME (frame);
6b5c4794 8020 if (!EQ (frame, tooltip_frame)
5f5c8ee5
GM
8021 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8022 x_consider_frame_title (frame);
8023 }
8024 }
8025#endif /* HAVE_WINDOW_SYSTEM */
8026
8027 /* Update the menu bar item lists, if appropriate. This has to be
8028 done before any actual redisplay or generation of display lines. */
2311178e 8029 all_windows = (update_mode_lines
5f5c8ee5
GM
8030 || buffer_shared > 1
8031 || windows_or_buffers_changed);
8032 if (all_windows)
8033 {
8034 Lisp_Object tail, frame;
331379bf 8035 int count = SPECPDL_INDEX ();
5f5c8ee5
GM
8036
8037 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8038
8039 FOR_EACH_FRAME (tail, frame)
8040 {
8041 f = XFRAME (frame);
8042
8043 /* Ignore tooltip frame. */
6b5c4794 8044 if (EQ (frame, tooltip_frame))
5f5c8ee5 8045 continue;
2311178e 8046
5f5c8ee5
GM
8047 /* If a window on this frame changed size, report that to
8048 the user and clear the size-change flag. */
8049 if (FRAME_WINDOW_SIZES_CHANGED (f))
8050 {
8051 Lisp_Object functions;
2311178e 8052
5f5c8ee5
GM
8053 /* Clear flag first in case we get an error below. */
8054 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8055 functions = Vwindow_size_change_functions;
8056 GCPRO2 (tail, functions);
2311178e 8057
5f5c8ee5
GM
8058 while (CONSP (functions))
8059 {
8060 call1 (XCAR (functions), frame);
8061 functions = XCDR (functions);
8062 }
8063 UNGCPRO;
8064 }
2311178e 8065
5f5c8ee5
GM
8066 GCPRO1 (tail);
8067 update_menu_bar (f, 0);
8068#ifdef HAVE_WINDOW_SYSTEM
e037b9ec 8069 update_tool_bar (f, 0);
5f5c8ee5
GM
8070#endif
8071 UNGCPRO;
8072 }
8073
8074 unbind_to (count, Qnil);
8075 }
8076 else
8077 {
886bd6f2
GM
8078 struct frame *sf = SELECTED_FRAME ();
8079 update_menu_bar (sf, 1);
5f5c8ee5 8080#ifdef HAVE_WINDOW_SYSTEM
886bd6f2 8081 update_tool_bar (sf, 1);
5f5c8ee5
GM
8082#endif
8083 }
8084
8085 /* Motif needs this. See comment in xmenu.c. Turn it off when
8086 pending_menu_activation is not defined. */
8087#ifdef USE_X_TOOLKIT
8088 pending_menu_activation = 0;
8089#endif
8090}
8091
8092
8093/* Update the menu bar item list for frame F. This has to be done
8094 before we start to fill in any display lines, because it can call
8095 eval.
8096
8097 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8098
8099static void
8100update_menu_bar (f, save_match_data)
8101 struct frame *f;
8102 int save_match_data;
8103{
8104 Lisp_Object window;
8105 register struct window *w;
8106
e1477f43
GM
8107 /* If called recursively during a menu update, do nothing. This can
8108 happen when, for instance, an activate-menubar-hook causes a
8109 redisplay. */
8110 if (inhibit_menubar_update)
8111 return;
8112
5f5c8ee5
GM
8113 window = FRAME_SELECTED_WINDOW (f);
8114 w = XWINDOW (window);
2311178e 8115
84f2e615 8116#if 0 /* The if statement below this if statement used to include the
a5f08374
RS
8117 condition !NILP (w->update_mode_line), rather than using
8118 update_mode_lines directly, and this if statement may have
8119 been added to make that condition work. Now the if
2311178e 8120 statement below matches its comment, this isn't needed. */
5f5c8ee5
GM
8121 if (update_mode_lines)
8122 w->update_mode_line = Qt;
a5f08374 8123#endif
5f5c8ee5
GM
8124
8125 if (FRAME_WINDOW_P (f)
8126 ?
488dd4c4
JD
8127#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8128 || defined (USE_GTK)
2311178e 8129 FRAME_EXTERNAL_MENU_BAR (f)
5f5c8ee5
GM
8130#else
8131 FRAME_MENU_BAR_LINES (f) > 0
8132#endif
8133 : FRAME_MENU_BAR_LINES (f) > 0)
8134 {
8135 /* If the user has switched buffers or windows, we need to
8136 recompute to reflect the new bindings. But we'll
8137 recompute when update_mode_lines is set too; that means
8138 that people can use force-mode-line-update to request
8139 that the menu bar be recomputed. The adverse effect on
8140 the rest of the redisplay algorithm is about the same as
8141 windows_or_buffers_changed anyway. */
8142 if (windows_or_buffers_changed
a5f08374
RS
8143 /* This used to test w->update_mode_line, but we believe
8144 there is no need to recompute the menu in that case. */
8145 || update_mode_lines
5f5c8ee5
GM
8146 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8147 < BUF_MODIFF (XBUFFER (w->buffer)))
8148 != !NILP (w->last_had_star))
8149 || ((!NILP (Vtransient_mark_mode)
8150 && !NILP (XBUFFER (w->buffer)->mark_active))
8151 != !NILP (w->region_showing)))
8152 {
8153 struct buffer *prev = current_buffer;
331379bf 8154 int count = SPECPDL_INDEX ();
5f5c8ee5 8155
e1477f43
GM
8156 specbind (Qinhibit_menubar_update, Qt);
8157
5f5c8ee5
GM
8158 set_buffer_internal_1 (XBUFFER (w->buffer));
8159 if (save_match_data)
8160 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8161 if (NILP (Voverriding_local_map_menu_flag))
8162 {
8163 specbind (Qoverriding_terminal_local_map, Qnil);
8164 specbind (Qoverriding_local_map, Qnil);
8165 }
8166
8167 /* Run the Lucid hook. */
65048e97 8168 safe_run_hooks (Qactivate_menubar_hook);
2311178e 8169
5f5c8ee5
GM
8170 /* If it has changed current-menubar from previous value,
8171 really recompute the menu-bar from the value. */
8172 if (! NILP (Vlucid_menu_bar_dirty_flag))
8173 call0 (Qrecompute_lucid_menubar);
2311178e 8174
5f5c8ee5
GM
8175 safe_run_hooks (Qmenu_bar_update_hook);
8176 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
2311178e 8177
5f5c8ee5 8178 /* Redisplay the menu bar in case we changed it. */
488dd4c4
JD
8179#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8180 || defined (USE_GTK)
1a578e9b 8181 if (FRAME_WINDOW_P (f)
e0f712ba 8182#if defined (MAC_OS)
1a578e9b
AC
8183 /* All frames on Mac OS share the same menubar. So only the
8184 selected frame should be allowed to set it. */
8185 && f == SELECTED_FRAME ()
8186#endif
8187 )
5f5c8ee5
GM
8188 set_frame_menubar (f, 0, 0);
8189 else
8190 /* On a terminal screen, the menu bar is an ordinary screen
8191 line, and this makes it get updated. */
8192 w->update_mode_line = Qt;
488dd4c4 8193#else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
5f5c8ee5
GM
8194 /* In the non-toolkit version, the menu bar is an ordinary screen
8195 line, and this makes it get updated. */
8196 w->update_mode_line = Qt;
488dd4c4 8197#endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
5f5c8ee5
GM
8198
8199 unbind_to (count, Qnil);
8200 set_buffer_internal_1 (prev);
8201 }
8202 }
8203}
8204
8205
8206\f
fa3c6b4d
KS
8207/***********************************************************************
8208 Output Cursor
8209 ***********************************************************************/
8210
1dabd0cf
KS
8211#ifdef HAVE_WINDOW_SYSTEM
8212
fa3c6b4d
KS
8213/* EXPORT:
8214 Nominal cursor position -- where to draw output.
8215 HPOS and VPOS are window relative glyph matrix coordinates.
8216 X and Y are window relative pixel coordinates. */
8217
8218struct cursor_pos output_cursor;
8219
8220
8221/* EXPORT:
8222 Set the global variable output_cursor to CURSOR. All cursor
8223 positions are relative to updated_window. */
8224
8225void
8226set_output_cursor (cursor)
8227 struct cursor_pos *cursor;
8228{
8229 output_cursor.hpos = cursor->hpos;
8230 output_cursor.vpos = cursor->vpos;
8231 output_cursor.x = cursor->x;
8232 output_cursor.y = cursor->y;
8233}
8234
8235
8236/* EXPORT for RIF:
8237 Set a nominal cursor position.
8238
8239 HPOS and VPOS are column/row positions in a window glyph matrix. X
8240 and Y are window text area relative pixel positions.
8241
8242 If this is done during an update, updated_window will contain the
8243 window that is being updated and the position is the future output
8244 cursor position for that window. If updated_window is null, use
8245 selected_window and display the cursor at the given position. */
8246
8247void
8248x_cursor_to (vpos, hpos, y, x)
8249 int vpos, hpos, y, x;
8250{
8251 struct window *w;
8252
8253 /* If updated_window is not set, work on selected_window. */
8254 if (updated_window)
8255 w = updated_window;
8256 else
8257 w = XWINDOW (selected_window);
8258
8259 /* Set the output cursor. */
8260 output_cursor.hpos = hpos;
8261 output_cursor.vpos = vpos;
8262 output_cursor.x = x;
8263 output_cursor.y = y;
8264
8265 /* If not called as part of an update, really display the cursor.
8266 This will also set the cursor position of W. */
8267 if (updated_window == NULL)
8268 {
8269 BLOCK_INPUT;
8270 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8271 if (rif->flush_display_optional)
8272 rif->flush_display_optional (SELECTED_FRAME ());
8273 UNBLOCK_INPUT;
8274 }
8275}
8276
1dabd0cf 8277#endif /* HAVE_WINDOW_SYSTEM */
fa3c6b4d
KS
8278
8279\f
5f5c8ee5 8280/***********************************************************************
e037b9ec 8281 Tool-bars
5f5c8ee5
GM
8282 ***********************************************************************/
8283
8284#ifdef HAVE_WINDOW_SYSTEM
8285
fa3c6b4d
KS
8286/* Where the mouse was last time we reported a mouse event. */
8287
8288FRAME_PTR last_mouse_frame;
8289
8290/* Tool-bar item index of the item on which a mouse button was pressed
8291 or -1. */
8292
8293int last_tool_bar_item;
8294
8295
e037b9ec 8296/* Update the tool-bar item list for frame F. This has to be done
5f5c8ee5
GM
8297 before we start to fill in any display lines. Called from
8298 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8299 and restore it here. */
8300
8301static void
e037b9ec 8302update_tool_bar (f, save_match_data)
5f5c8ee5
GM
8303 struct frame *f;
8304 int save_match_data;
8305{
488dd4c4 8306#ifdef USE_GTK
810f2256 8307 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
488dd4c4
JD
8308#else
8309 int do_update = WINDOWP (f->tool_bar_window)
da8b7f4f 8310 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
488dd4c4
JD
8311#endif
8312
8313 if (do_update)
5f5c8ee5
GM
8314 {
8315 Lisp_Object window;
8316 struct window *w;
8317
8318 window = FRAME_SELECTED_WINDOW (f);
8319 w = XWINDOW (window);
2311178e 8320
5f5c8ee5
GM
8321 /* If the user has switched buffers or windows, we need to
8322 recompute to reflect the new bindings. But we'll
8323 recompute when update_mode_lines is set too; that means
8324 that people can use force-mode-line-update to request
8325 that the menu bar be recomputed. The adverse effect on
8326 the rest of the redisplay algorithm is about the same as
8327 windows_or_buffers_changed anyway. */
8328 if (windows_or_buffers_changed
8329 || !NILP (w->update_mode_line)
84f2e615 8330 || update_mode_lines
5f5c8ee5
GM
8331 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8332 < BUF_MODIFF (XBUFFER (w->buffer)))
8333 != !NILP (w->last_had_star))
8334 || ((!NILP (Vtransient_mark_mode)
8335 && !NILP (XBUFFER (w->buffer)->mark_active))
8336 != !NILP (w->region_showing)))
8337 {
8338 struct buffer *prev = current_buffer;
331379bf 8339 int count = SPECPDL_INDEX ();
84f2e615
RS
8340 Lisp_Object old_tool_bar;
8341 struct gcpro gcpro1;
a2889657 8342
5f5c8ee5
GM
8343 /* Set current_buffer to the buffer of the selected
8344 window of the frame, so that we get the right local
8345 keymaps. */
8346 set_buffer_internal_1 (XBUFFER (w->buffer));
1f40cad2 8347
5f5c8ee5
GM
8348 /* Save match data, if we must. */
8349 if (save_match_data)
8350 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8351
8352 /* Make sure that we don't accidentally use bogus keymaps. */
8353 if (NILP (Voverriding_local_map_menu_flag))
8354 {
8355 specbind (Qoverriding_terminal_local_map, Qnil);
8356 specbind (Qoverriding_local_map, Qnil);
1f40cad2 8357 }
1f40cad2 8358
84f2e615
RS
8359 old_tool_bar = f->tool_bar_items;
8360 GCPRO1 (old_tool_bar);
8361
e037b9ec 8362 /* Build desired tool-bar items from keymaps. */
e3b2c21f 8363 BLOCK_INPUT;
7464726e
GM
8364 f->tool_bar_items
8365 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
e3b2c21f 8366 UNBLOCK_INPUT;
2311178e 8367
84f2e615
RS
8368 /* Redisplay the tool-bar if we changed it. */
8369 if (! NILP (Fequal (old_tool_bar, f->tool_bar_items)))
8370 w->update_mode_line = Qt;
be21b925 8371
1e802fb5 8372 UNGCPRO;
5f5c8ee5
GM
8373
8374 unbind_to (count, Qnil);
8375 set_buffer_internal_1 (prev);
81d478f3 8376 }
a2889657
JB
8377 }
8378}
8379
6c4429a5 8380
e037b9ec 8381/* Set F->desired_tool_bar_string to a Lisp string representing frame
7464726e 8382 F's desired tool-bar contents. F->tool_bar_items must have
5f5c8ee5
GM
8383 been set up previously by calling prepare_menu_bars. */
8384
a2889657 8385static void
e037b9ec 8386build_desired_tool_bar_string (f)
5f5c8ee5 8387 struct frame *f;
a2889657 8388{
a23887b9 8389 int i, size, size_needed;
5f5c8ee5
GM
8390 struct gcpro gcpro1, gcpro2, gcpro3;
8391 Lisp_Object image, plist, props;
a2889657 8392
5f5c8ee5
GM
8393 image = plist = props = Qnil;
8394 GCPRO3 (image, plist, props);
a2889657 8395
e037b9ec 8396 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
5f5c8ee5 8397 Otherwise, make a new string. */
2311178e 8398
5f5c8ee5 8399 /* The size of the string we might be able to reuse. */
e037b9ec 8400 size = (STRINGP (f->desired_tool_bar_string)
2051c264 8401 ? SCHARS (f->desired_tool_bar_string)
5f5c8ee5
GM
8402 : 0);
8403
b94c0d9c 8404 /* We need one space in the string for each image. */
a23887b9 8405 size_needed = f->n_tool_bar_items;
2311178e 8406
b94c0d9c 8407 /* Reuse f->desired_tool_bar_string, if possible. */
cb2ddc53 8408 if (size < size_needed || NILP (f->desired_tool_bar_string))
6fc556fd
KR
8409 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8410 make_number (' '));
5f5c8ee5
GM
8411 else
8412 {
8413 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8414 Fremove_text_properties (make_number (0), make_number (size),
e037b9ec 8415 props, f->desired_tool_bar_string);
5f5c8ee5 8416 }
a2889657 8417
5f5c8ee5 8418 /* Put a `display' property on the string for the images to display,
e037b9ec
GM
8419 put a `menu_item' property on tool-bar items with a value that
8420 is the index of the item in F's tool-bar item vector. */
a23887b9 8421 for (i = 0; i < f->n_tool_bar_items; ++i)
a2889657 8422 {
7464726e 8423#define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
5f5c8ee5 8424
e037b9ec
GM
8425 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8426 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
35a41507 8427 int hmargin, vmargin, relief, idx, end;
493fdc3c 8428 extern Lisp_Object QCrelief, QCmargin, QCconversion;
5f5c8ee5
GM
8429
8430 /* If image is a vector, choose the image according to the
8431 button state. */
e037b9ec 8432 image = PROP (TOOL_BAR_ITEM_IMAGES);
5f5c8ee5
GM
8433 if (VECTORP (image))
8434 {
5f5c8ee5
GM
8435 if (enabled_p)
8436 idx = (selected_p
e037b9ec
GM
8437 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8438 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
5f5c8ee5
GM
8439 else
8440 idx = (selected_p
e037b9ec
GM
8441 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8442 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
2311178e 8443
37e4e482
GM
8444 xassert (ASIZE (image) >= idx);
8445 image = AREF (image, idx);
5f5c8ee5 8446 }
37e4e482
GM
8447 else
8448 idx = -1;
5f5c8ee5
GM
8449
8450 /* Ignore invalid image specifications. */
8451 if (!valid_image_p (image))
8452 continue;
8453
e037b9ec 8454 /* Display the tool-bar button pressed, or depressed. */
5f5c8ee5
GM
8455 plist = Fcopy_sequence (XCDR (image));
8456
8457 /* Compute margin and relief to draw. */
9f864bf9 8458 relief = (tool_bar_button_relief >= 0
c3d76173
GM
8459 ? tool_bar_button_relief
8460 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
35a41507
GM
8461 hmargin = vmargin = relief;
8462
8463 if (INTEGERP (Vtool_bar_button_margin)
8464 && XINT (Vtool_bar_button_margin) > 0)
8465 {
8466 hmargin += XFASTINT (Vtool_bar_button_margin);
8467 vmargin += XFASTINT (Vtool_bar_button_margin);
8468 }
8469 else if (CONSP (Vtool_bar_button_margin))
8470 {
8471 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8472 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8473 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
2311178e 8474
35a41507
GM
8475 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8476 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8477 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8478 }
2311178e 8479
e037b9ec 8480 if (auto_raise_tool_bar_buttons_p)
5f5c8ee5
GM
8481 {
8482 /* Add a `:relief' property to the image spec if the item is
8483 selected. */
8484 if (selected_p)
8485 {
8486 plist = Fplist_put (plist, QCrelief, make_number (-relief));
35a41507
GM
8487 hmargin -= relief;
8488 vmargin -= relief;
5f5c8ee5
GM
8489 }
8490 }
8491 else
8492 {
8493 /* If image is selected, display it pressed, i.e. with a
8494 negative relief. If it's not selected, display it with a
8495 raised relief. */
8496 plist = Fplist_put (plist, QCrelief,
8497 (selected_p
8498 ? make_number (-relief)
8499 : make_number (relief)));
35a41507
GM
8500 hmargin -= relief;
8501 vmargin -= relief;
5f5c8ee5
GM
8502 }
8503
8504 /* Put a margin around the image. */
35a41507
GM
8505 if (hmargin || vmargin)
8506 {
8507 if (hmargin == vmargin)
8508 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8509 else
8510 plist = Fplist_put (plist, QCmargin,
8511 Fcons (make_number (hmargin),
8512 make_number (vmargin)));
8513 }
2311178e 8514
37e4e482
GM
8515 /* If button is not enabled, and we don't have special images
8516 for the disabled state, make the image appear disabled by
5f5c8ee5 8517 applying an appropriate algorithm to it. */
37e4e482 8518 if (!enabled_p && idx < 0)
ecd01a0e 8519 plist = Fplist_put (plist, QCconversion, Qdisabled);
2311178e 8520
5f5c8ee5
GM
8521 /* Put a `display' text property on the string for the image to
8522 display. Put a `menu-item' property on the string that gives
e037b9ec 8523 the start of this item's properties in the tool-bar items
5f5c8ee5
GM
8524 vector. */
8525 image = Fcons (Qimage, plist);
8526 props = list4 (Qdisplay, image,
a23887b9
GM
8527 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8528
8529 /* Let the last image hide all remaining spaces in the tool bar
8530 string. The string can be longer than needed when we reuse a
8531 previous string. */
8532 if (i + 1 == f->n_tool_bar_items)
2051c264 8533 end = SCHARS (f->desired_tool_bar_string);
a23887b9
GM
8534 else
8535 end = i + 1;
8536 Fadd_text_properties (make_number (i), make_number (end),
e037b9ec 8537 props, f->desired_tool_bar_string);
5f5c8ee5 8538#undef PROP
a2889657
JB
8539 }
8540
5f5c8ee5
GM
8541 UNGCPRO;
8542}
8543
8544
e037b9ec 8545/* Display one line of the tool-bar of frame IT->f. */
5f5c8ee5
GM
8546
8547static void
e037b9ec 8548display_tool_bar_line (it)
5f5c8ee5
GM
8549 struct it *it;
8550{
8551 struct glyph_row *row = it->glyph_row;
8552 int max_x = it->last_visible_x;
8553 struct glyph *last;
2311178e 8554
5f5c8ee5
GM
8555 prepare_desired_row (row);
8556 row->y = it->current_y;
90aa2856
GM
8557
8558 /* Note that this isn't made use of if the face hasn't a box,
8559 so there's no need to check the face here. */
8560 it->start_of_box_run_p = 1;
2311178e 8561
5f5c8ee5 8562 while (it->current_x < max_x)
a2889657 8563 {
5f5c8ee5 8564 int x_before, x, n_glyphs_before, i, nglyphs;
a2f016e3 8565
5f5c8ee5
GM
8566 /* Get the next display element. */
8567 if (!get_next_display_element (it))
8568 break;
73af359d 8569
5f5c8ee5
GM
8570 /* Produce glyphs. */
8571 x_before = it->current_x;
8572 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8573 PRODUCE_GLYPHS (it);
daa37602 8574
5f5c8ee5
GM
8575 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8576 i = 0;
8577 x = x_before;
8578 while (i < nglyphs)
8579 {
8580 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
2311178e 8581
5f5c8ee5
GM
8582 if (x + glyph->pixel_width > max_x)
8583 {
8584 /* Glyph doesn't fit on line. */
8585 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8586 it->current_x = x;
8587 goto out;
8588 }
daa37602 8589
5f5c8ee5
GM
8590 ++it->hpos;
8591 x += glyph->pixel_width;
8592 ++i;
8593 }
8594
8595 /* Stop at line ends. */
8596 if (ITERATOR_AT_END_OF_LINE_P (it))
8597 break;
8598
cafafe0b 8599 set_iterator_to_next (it, 1);
a2889657 8600 }
a2889657 8601
5f5c8ee5 8602 out:;
a2889657 8603
5f5c8ee5
GM
8604 row->displays_text_p = row->used[TEXT_AREA] != 0;
8605 extend_face_to_end_of_line (it);
8606 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8607 last->right_box_line_p = 1;
90aa2856
GM
8608 if (last == row->glyphs[TEXT_AREA])
8609 last->left_box_line_p = 1;
5f5c8ee5 8610 compute_line_metrics (it);
2311178e 8611
e037b9ec 8612 /* If line is empty, make it occupy the rest of the tool-bar. */
5f5c8ee5
GM
8613 if (!row->displays_text_p)
8614 {
312246d1
GM
8615 row->height = row->phys_height = it->last_visible_y - row->y;
8616 row->ascent = row->phys_ascent = 0;
5f5c8ee5 8617 }
2311178e 8618
5f5c8ee5
GM
8619 row->full_width_p = 1;
8620 row->continued_p = 0;
8621 row->truncated_on_left_p = 0;
8622 row->truncated_on_right_p = 0;
8623
8624 it->current_x = it->hpos = 0;
8625 it->current_y += row->height;
8626 ++it->vpos;
8627 ++it->glyph_row;
a2889657 8628}
96a410bc 8629
5f5c8ee5 8630
e037b9ec 8631/* Value is the number of screen lines needed to make all tool-bar
5f5c8ee5 8632 items of frame F visible. */
96a410bc 8633
d39b6696 8634static int
e037b9ec 8635tool_bar_lines_needed (f)
5f5c8ee5 8636 struct frame *f;
d39b6696 8637{
e037b9ec 8638 struct window *w = XWINDOW (f->tool_bar_window);
5f5c8ee5 8639 struct it it;
2311178e 8640
e037b9ec
GM
8641 /* Initialize an iterator for iteration over
8642 F->desired_tool_bar_string in the tool-bar window of frame F. */
8643 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
5f5c8ee5 8644 it.first_visible_x = 0;
da8b7f4f 8645 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
e037b9ec 8646 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
5f5c8ee5
GM
8647
8648 while (!ITERATOR_AT_END_P (&it))
8649 {
8650 it.glyph_row = w->desired_matrix->rows;
8651 clear_glyph_row (it.glyph_row);
e037b9ec 8652 display_tool_bar_line (&it);
5f5c8ee5
GM
8653 }
8654
da8b7f4f 8655 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
d39b6696 8656}
96a410bc 8657
5f5c8ee5 8658
57c28064
GM
8659DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8660 0, 1, 0,
7ee72033
MB
8661 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8662 (frame)
57c28064
GM
8663 Lisp_Object frame;
8664{
8665 struct frame *f;
8666 struct window *w;
8667 int nlines = 0;
8668
8669 if (NILP (frame))
8670 frame = selected_frame;
8671 else
b7826503 8672 CHECK_FRAME (frame);
57c28064 8673 f = XFRAME (frame);
2311178e 8674
57c28064
GM
8675 if (WINDOWP (f->tool_bar_window)
8676 || (w = XWINDOW (f->tool_bar_window),
da8b7f4f 8677 WINDOW_TOTAL_LINES (w) > 0))
57c28064
GM
8678 {
8679 update_tool_bar (f, 1);
8680 if (f->n_tool_bar_items)
8681 {
8682 build_desired_tool_bar_string (f);
8683 nlines = tool_bar_lines_needed (f);
8684 }
8685 }
8686
8687 return make_number (nlines);
8688}
8689
8690
e037b9ec 8691/* Display the tool-bar of frame F. Value is non-zero if tool-bar's
5f5c8ee5
GM
8692 height should be changed. */
8693
8694static int
e037b9ec 8695redisplay_tool_bar (f)
5f5c8ee5 8696 struct frame *f;
96a410bc 8697{
5f5c8ee5
GM
8698 struct window *w;
8699 struct it it;
8700 struct glyph_row *row;
8701 int change_height_p = 0;
177c0ea7 8702
488dd4c4 8703#ifdef USE_GTK
810f2256 8704 if (FRAME_EXTERNAL_TOOL_BAR (f))
488dd4c4
JD
8705 update_frame_tool_bar (f);
8706 return 0;
8707#endif
2311178e 8708
e037b9ec
GM
8709 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8710 do anything. This means you must start with tool-bar-lines
5f5c8ee5 8711 non-zero to get the auto-sizing effect. Or in other words, you
e037b9ec
GM
8712 can turn off tool-bars by specifying tool-bar-lines zero. */
8713 if (!WINDOWP (f->tool_bar_window)
8714 || (w = XWINDOW (f->tool_bar_window),
da8b7f4f 8715 WINDOW_TOTAL_LINES (w) == 0))
5f5c8ee5 8716 return 0;
96a410bc 8717
e037b9ec
GM
8718 /* Set up an iterator for the tool-bar window. */
8719 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
5f5c8ee5 8720 it.first_visible_x = 0;
da8b7f4f 8721 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
5f5c8ee5 8722 row = it.glyph_row;
3450d04c 8723
e037b9ec
GM
8724 /* Build a string that represents the contents of the tool-bar. */
8725 build_desired_tool_bar_string (f);
8726 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
3450d04c 8727
e037b9ec 8728 /* Display as many lines as needed to display all tool-bar items. */
5f5c8ee5 8729 while (it.current_y < it.last_visible_y)
e037b9ec 8730 display_tool_bar_line (&it);
3450d04c 8731
e037b9ec 8732 /* It doesn't make much sense to try scrolling in the tool-bar
5f5c8ee5
GM
8733 window, so don't do it. */
8734 w->desired_matrix->no_scrolling_p = 1;
8735 w->must_be_updated_p = 1;
3450d04c 8736
e037b9ec 8737 if (auto_resize_tool_bars_p)
5f5c8ee5
GM
8738 {
8739 int nlines;
6e33e6f0
GM
8740
8741 /* If we couldn't display everything, change the tool-bar's
8742 height. */
8743 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8744 change_height_p = 1;
2311178e 8745
5f5c8ee5
GM
8746 /* If there are blank lines at the end, except for a partially
8747 visible blank line at the end that is smaller than
da8b7f4f 8748 FRAME_LINE_HEIGHT, change the tool-bar's height. */
5f5c8ee5
GM
8749 row = it.glyph_row - 1;
8750 if (!row->displays_text_p
da8b7f4f 8751 && row->height >= FRAME_LINE_HEIGHT (f))
5f5c8ee5
GM
8752 change_height_p = 1;
8753
e037b9ec
GM
8754 /* If row displays tool-bar items, but is partially visible,
8755 change the tool-bar's height. */
5f5c8ee5
GM
8756 if (row->displays_text_p
8757 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8758 change_height_p = 1;
8759
e037b9ec 8760 /* Resize windows as needed by changing the `tool-bar-lines'
5f5c8ee5
GM
8761 frame parameter. */
8762 if (change_height_p
e037b9ec 8763 && (nlines = tool_bar_lines_needed (f),
da8b7f4f 8764 nlines != WINDOW_TOTAL_LINES (w)))
5f5c8ee5 8765 {
e037b9ec 8766 extern Lisp_Object Qtool_bar_lines;
5f5c8ee5 8767 Lisp_Object frame;
da8b7f4f 8768 int old_height = WINDOW_TOTAL_LINES (w);
2311178e 8769
5f5c8ee5
GM
8770 XSETFRAME (frame, f);
8771 clear_glyph_matrix (w->desired_matrix);
8772 Fmodify_frame_parameters (frame,
e037b9ec 8773 Fcons (Fcons (Qtool_bar_lines,
5f5c8ee5
GM
8774 make_number (nlines)),
8775 Qnil));
da8b7f4f 8776 if (WINDOW_TOTAL_LINES (w) != old_height)
e85ee976 8777 fonts_changed_p = 1;
5f5c8ee5
GM
8778 }
8779 }
3450d04c 8780
5f5c8ee5 8781 return change_height_p;
96a410bc 8782}
90adcf20 8783
5f5c8ee5 8784
e037b9ec
GM
8785/* Get information about the tool-bar item which is displayed in GLYPH
8786 on frame F. Return in *PROP_IDX the index where tool-bar item
7464726e 8787 properties start in F->tool_bar_items. Value is zero if
e037b9ec 8788 GLYPH doesn't display a tool-bar item. */
5f5c8ee5 8789
fa3c6b4d 8790static int
e037b9ec 8791tool_bar_item_info (f, glyph, prop_idx)
5f5c8ee5
GM
8792 struct frame *f;
8793 struct glyph *glyph;
8794 int *prop_idx;
90adcf20 8795{
5f5c8ee5
GM
8796 Lisp_Object prop;
8797 int success_p;
be676094
GM
8798 int charpos;
8799
8800 /* This function can be called asynchronously, which means we must
8801 exclude any possibility that Fget_text_property signals an
8802 error. */
2051c264 8803 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
be676094 8804 charpos = max (0, charpos);
2311178e 8805
5f5c8ee5
GM
8806 /* Get the text property `menu-item' at pos. The value of that
8807 property is the start index of this item's properties in
7464726e 8808 F->tool_bar_items. */
be676094 8809 prop = Fget_text_property (make_number (charpos),
e037b9ec 8810 Qmenu_item, f->current_tool_bar_string);
5f5c8ee5
GM
8811 if (INTEGERP (prop))
8812 {
8813 *prop_idx = XINT (prop);
8814 success_p = 1;
8815 }
8816 else
8817 success_p = 0;
90adcf20 8818
5f5c8ee5
GM
8819 return success_p;
8820}
2311178e 8821
fa3c6b4d
KS
8822\f
8823/* Get information about the tool-bar item at position X/Y on frame F.
8824 Return in *GLYPH a pointer to the glyph of the tool-bar item in
8825 the current matrix of the tool-bar window of F, or NULL if not
8826 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8827 item in F->tool_bar_items. Value is
8828
8829 -1 if X/Y is not on a tool-bar item
8830 0 if X/Y is on the same item that was highlighted before.
8831 1 otherwise. */
8832
8833static int
8834get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
8835 struct frame *f;
8836 int x, y;
8837 struct glyph **glyph;
8838 int *hpos, *vpos, *prop_idx;
8839{
8840 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8841 struct window *w = XWINDOW (f->tool_bar_window);
8842 int area;
8843
8844 /* Find the glyph under X/Y. */
493fdc3c 8845 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
fa3c6b4d
KS
8846 if (*glyph == NULL)
8847 return -1;
8848
8849 /* Get the start of this tool-bar item's properties in
8850 f->tool_bar_items. */
8851 if (!tool_bar_item_info (f, *glyph, prop_idx))
8852 return -1;
8853
8854 /* Is mouse on the highlighted item? */
8855 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
8856 && *vpos >= dpyinfo->mouse_face_beg_row
8857 && *vpos <= dpyinfo->mouse_face_end_row
8858 && (*vpos > dpyinfo->mouse_face_beg_row
8859 || *hpos >= dpyinfo->mouse_face_beg_col)
8860 && (*vpos < dpyinfo->mouse_face_end_row
8861 || *hpos < dpyinfo->mouse_face_end_col
8862 || dpyinfo->mouse_face_past_end))
8863 return 0;
8864
8865 return 1;
8866}
8867
8868
8869/* EXPORT:
8870 Handle mouse button event on the tool-bar of frame F, at
8871 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
8872 0 for button release. MODIFIERS is event modifiers for button
8873 release. */
8874
8875void
8876handle_tool_bar_click (f, x, y, down_p, modifiers)
8877 struct frame *f;
8878 int x, y, down_p;
8879 unsigned int modifiers;
8880{
8881 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8882 struct window *w = XWINDOW (f->tool_bar_window);
8883 int hpos, vpos, prop_idx;
8884 struct glyph *glyph;
8885 Lisp_Object enabled_p;
8886
8887 /* If not on the highlighted tool-bar item, return. */
8888 frame_to_window_pixel_xy (w, &x, &y);
8889 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
8890 return;
8891
8892 /* If item is disabled, do nothing. */
8893 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8894 if (NILP (enabled_p))
8895 return;
8896
8897 if (down_p)
8898 {
8899 /* Show item in pressed state. */
8900 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
8901 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
8902 last_tool_bar_item = prop_idx;
8903 }
8904 else
8905 {
8906 Lisp_Object key, frame;
8907 struct input_event event;
d86705ec 8908 EVENT_INIT (event);
fa3c6b4d
KS
8909
8910 /* Show item in released state. */
8911 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
8912 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
8913
8914 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
8915
8916 XSETFRAME (frame, f);
8917 event.kind = TOOL_BAR_EVENT;
8918 event.frame_or_window = frame;
8919 event.arg = frame;
8920 kbd_buffer_store_event (&event);
8921
8922 event.kind = TOOL_BAR_EVENT;
8923 event.frame_or_window = frame;
8924 event.arg = key;
8925 event.modifiers = modifiers;
8926 kbd_buffer_store_event (&event);
8927 last_tool_bar_item = -1;
8928 }
8929}
8930
8931
8932/* Possibly highlight a tool-bar item on frame F when mouse moves to
8933 tool-bar window-relative coordinates X/Y. Called from
8934 note_mouse_highlight. */
8935
8936static void
8937note_tool_bar_highlight (f, x, y)
8938 struct frame *f;
8939 int x, y;
8940{
8941 Lisp_Object window = f->tool_bar_window;
8942 struct window *w = XWINDOW (window);
8943 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8944 int hpos, vpos;
8945 struct glyph *glyph;
8946 struct glyph_row *row;
8947 int i;
8948 Lisp_Object enabled_p;
8949 int prop_idx;
8950 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
8951 int mouse_down_p, rc;
8952
8953 /* Function note_mouse_highlight is called with negative x(y
8954 values when mouse moves outside of the frame. */
8955 if (x <= 0 || y <= 0)
8956 {
8957 clear_mouse_face (dpyinfo);
8958 return;
8959 }
8960
8961 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
8962 if (rc < 0)
8963 {
8964 /* Not on tool-bar item. */
8965 clear_mouse_face (dpyinfo);
8966 return;
8967 }
8968 else if (rc == 0)
8969 /* On same tool-bar item as before. */
8970 goto set_help_echo;
8971
8972 clear_mouse_face (dpyinfo);
8973
8974 /* Mouse is down, but on different tool-bar item? */
8975 mouse_down_p = (dpyinfo->grabbed
8976 && f == last_mouse_frame
8977 && FRAME_LIVE_P (f));
8978 if (mouse_down_p
8979 && last_tool_bar_item != prop_idx)
8980 return;
8981
8982 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
8983 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
8984
8985 /* If tool-bar item is not enabled, don't highlight it. */
8986 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8987 if (!NILP (enabled_p))
8988 {
8989 /* Compute the x-position of the glyph. In front and past the
da8b7f4f 8990 image is a space. We include this in the highlighted area. */
fa3c6b4d
KS
8991 row = MATRIX_ROW (w->current_matrix, vpos);
8992 for (i = x = 0; i < hpos; ++i)
8993 x += row->glyphs[TEXT_AREA][i].pixel_width;
8994
8995 /* Record this as the current active region. */
8996 dpyinfo->mouse_face_beg_col = hpos;
8997 dpyinfo->mouse_face_beg_row = vpos;
8998 dpyinfo->mouse_face_beg_x = x;
8999 dpyinfo->mouse_face_beg_y = row->y;
9000 dpyinfo->mouse_face_past_end = 0;
9001
9002 dpyinfo->mouse_face_end_col = hpos + 1;
9003 dpyinfo->mouse_face_end_row = vpos;
9004 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9005 dpyinfo->mouse_face_end_y = row->y;
9006 dpyinfo->mouse_face_window = window;
9007 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9008
9009 /* Display it as active. */
9010 show_mouse_face (dpyinfo, draw);
9011 dpyinfo->mouse_face_image_state = draw;
9012 }
9013
9014 set_help_echo:
9015
9016 /* Set help_echo_string to a help string to display for this tool-bar item.
9017 XTread_socket does the rest. */
9018 help_echo_object = help_echo_window = Qnil;
9019 help_echo_pos = -1;
9020 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9021 if (NILP (help_echo_string))
9022 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9023}
9024
5f5c8ee5 9025#endif /* HAVE_WINDOW_SYSTEM */
ffbbc941
KS
9026
9027
9028\f
5f5c8ee5
GM
9029/************************************************************************
9030 Horizontal scrolling
9031 ************************************************************************/
feb0c42f 9032
5f5c8ee5
GM
9033static int hscroll_window_tree P_ ((Lisp_Object));
9034static int hscroll_windows P_ ((Lisp_Object));
feb0c42f 9035
5f5c8ee5
GM
9036/* For all leaf windows in the window tree rooted at WINDOW, set their
9037 hscroll value so that PT is (i) visible in the window, and (ii) so
9038 that it is not within a certain margin at the window's left and
9039 right border. Value is non-zero if any window's hscroll has been
9040 changed. */
9041
9042static int
9043hscroll_window_tree (window)
9044 Lisp_Object window;
9045{
9046 int hscrolled_p = 0;
e76d28d5 9047 int hscroll_relative_p = FLOATP (Vhscroll_step);
1df7e8f0
EZ
9048 int hscroll_step_abs = 0;
9049 double hscroll_step_rel = 0;
9050
9051 if (hscroll_relative_p)
9052 {
e76d28d5 9053 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
1df7e8f0
EZ
9054 if (hscroll_step_rel < 0)
9055 {
9056 hscroll_relative_p = 0;
9057 hscroll_step_abs = 0;
9058 }
9059 }
e76d28d5 9060 else if (INTEGERP (Vhscroll_step))
1df7e8f0 9061 {
e76d28d5 9062 hscroll_step_abs = XINT (Vhscroll_step);
1df7e8f0
EZ
9063 if (hscroll_step_abs < 0)
9064 hscroll_step_abs = 0;
9065 }
9066 else
9067 hscroll_step_abs = 0;
9068
5f5c8ee5 9069 while (WINDOWP (window))
90adcf20 9070 {
5f5c8ee5 9071 struct window *w = XWINDOW (window);
1df7e8f0 9072
5f5c8ee5
GM
9073 if (WINDOWP (w->hchild))
9074 hscrolled_p |= hscroll_window_tree (w->hchild);
9075 else if (WINDOWP (w->vchild))
9076 hscrolled_p |= hscroll_window_tree (w->vchild);
9077 else if (w->cursor.vpos >= 0)
9078 {
da8b7f4f
KS
9079 int h_margin;
9080 int text_area_width;
92a90e89
GM
9081 struct glyph_row *current_cursor_row
9082 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9083 struct glyph_row *desired_cursor_row
9084 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9085 struct glyph_row *cursor_row
9086 = (desired_cursor_row->enabled_p
9087 ? desired_cursor_row
9088 : current_cursor_row);
a2725ab2 9089
da8b7f4f 9090 text_area_width = window_box_width (w, TEXT_AREA);
90adcf20 9091
5f5c8ee5 9092 /* Scroll when cursor is inside this scroll margin. */
da8b7f4f 9093 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
1df7e8f0 9094
5f5c8ee5 9095 if ((XFASTINT (w->hscroll)
e76d28d5 9096 && w->cursor.x <= h_margin)
92a90e89
GM
9097 || (cursor_row->enabled_p
9098 && cursor_row->truncated_on_right_p
e76d28d5 9099 && (w->cursor.x >= text_area_width - h_margin)))
08b610e4 9100 {
5f5c8ee5
GM
9101 struct it it;
9102 int hscroll;
9103 struct buffer *saved_current_buffer;
9104 int pt;
1df7e8f0 9105 int wanted_x;
5f5c8ee5
GM
9106
9107 /* Find point in a display of infinite width. */
9108 saved_current_buffer = current_buffer;
9109 current_buffer = XBUFFER (w->buffer);
1df7e8f0 9110
5f5c8ee5
GM
9111 if (w == XWINDOW (selected_window))
9112 pt = BUF_PT (current_buffer);
9113 else
08b610e4 9114 {
5f5c8ee5
GM
9115 pt = marker_position (w->pointm);
9116 pt = max (BEGV, pt);
9117 pt = min (ZV, pt);
9118 }
9119
9120 /* Move iterator to pt starting at cursor_row->start in
9121 a line with infinite width. */
9122 init_to_row_start (&it, w, cursor_row);
9123 it.last_visible_x = INFINITY;
9124 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9125 current_buffer = saved_current_buffer;
9126
1df7e8f0
EZ
9127 /* Position cursor in window. */
9128 if (!hscroll_relative_p && hscroll_step_abs == 0)
ec110e9e
KS
9129 hscroll = max (0, (it.current_x
9130 - (ITERATOR_AT_END_OF_LINE_P (&it)
9131 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9132 : (text_area_width / 2))))
da8b7f4f 9133 / FRAME_COLUMN_WIDTH (it.f);
e76d28d5 9134 else if (w->cursor.x >= text_area_width - h_margin)
1df7e8f0
EZ
9135 {
9136 if (hscroll_relative_p)
9137 wanted_x = text_area_width * (1 - hscroll_step_rel)
e76d28d5 9138 - h_margin;
1df7e8f0
EZ
9139 else
9140 wanted_x = text_area_width
da8b7f4f 9141 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
e76d28d5 9142 - h_margin;
1df7e8f0 9143 hscroll
da8b7f4f 9144 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
1df7e8f0
EZ
9145 }
9146 else
9147 {
9148 if (hscroll_relative_p)
9149 wanted_x = text_area_width * hscroll_step_rel
e76d28d5 9150 + h_margin;
1df7e8f0 9151 else
da8b7f4f 9152 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
e76d28d5 9153 + h_margin;
1df7e8f0 9154 hscroll
da8b7f4f 9155 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
1df7e8f0 9156 }
3172f70b 9157 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
5f5c8ee5
GM
9158
9159 /* Don't call Fset_window_hscroll if value hasn't
9160 changed because it will prevent redisplay
9161 optimizations. */
9162 if (XFASTINT (w->hscroll) != hscroll)
9163 {
3172f70b
GM
9164 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9165 w->hscroll = make_number (hscroll);
5f5c8ee5 9166 hscrolled_p = 1;
08b610e4 9167 }
08b610e4 9168 }
08b610e4 9169 }
a2725ab2 9170
5f5c8ee5 9171 window = w->next;
90adcf20 9172 }
cd6dfed6 9173
5f5c8ee5
GM
9174 /* Value is non-zero if hscroll of any leaf window has been changed. */
9175 return hscrolled_p;
9176}
9177
9178
9179/* Set hscroll so that cursor is visible and not inside horizontal
9180 scroll margins for all windows in the tree rooted at WINDOW. See
9181 also hscroll_window_tree above. Value is non-zero if any window's
9182 hscroll has been changed. If it has, desired matrices on the frame
9183 of WINDOW are cleared. */
9184
9185static int
9186hscroll_windows (window)
9187 Lisp_Object window;
9188{
d475bcb8 9189 int hscrolled_p;
2311178e 9190
d475bcb8
GM
9191 if (automatic_hscrolling_p)
9192 {
9193 hscrolled_p = hscroll_window_tree (window);
9194 if (hscrolled_p)
9195 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9196 }
9197 else
9198 hscrolled_p = 0;
5f5c8ee5 9199 return hscrolled_p;
90adcf20 9200}
5f5c8ee5
GM
9201
9202
90adcf20 9203\f
5f5c8ee5
GM
9204/************************************************************************
9205 Redisplay
9206 ************************************************************************/
9207
9208/* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9209 to a non-zero value. This is sometimes handy to have in a debugger
9210 session. */
9211
9212#if GLYPH_DEBUG
a2889657 9213
5f5c8ee5
GM
9214/* First and last unchanged row for try_window_id. */
9215
9216int debug_first_unchanged_at_end_vpos;
9217int debug_last_unchanged_at_beg_vpos;
9218
9219/* Delta vpos and y. */
9220
9221int debug_dvpos, debug_dy;
9222
9223/* Delta in characters and bytes for try_window_id. */
9224
9225int debug_delta, debug_delta_bytes;
9226
9227/* Values of window_end_pos and window_end_vpos at the end of
9228 try_window_id. */
9229
31ade731 9230EMACS_INT debug_end_pos, debug_end_vpos;
5f5c8ee5
GM
9231
9232/* Append a string to W->desired_matrix->method. FMT is a printf
9233 format string. A1...A9 are a supplement for a variable-length
9234 argument list. If trace_redisplay_p is non-zero also printf the
9235 resulting string to stderr. */
9236
9237static void
9238debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9239 struct window *w;
9240 char *fmt;
9241 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9242{
9243 char buffer[512];
9244 char *method = w->desired_matrix->method;
9245 int len = strlen (method);
9246 int size = sizeof w->desired_matrix->method;
9247 int remaining = size - len - 1;
9248
9249 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9250 if (len && remaining)
9251 {
9252 method[len] = '|';
9253 --remaining, ++len;
9254 }
2311178e 9255
5f5c8ee5
GM
9256 strncpy (method + len, buffer, remaining);
9257
9258 if (trace_redisplay_p)
9259 fprintf (stderr, "%p (%s): %s\n",
9260 w,
9261 ((BUFFERP (w->buffer)
9262 && STRINGP (XBUFFER (w->buffer)->name))
2051c264 9263 ? (char *) SDATA (XBUFFER (w->buffer)->name)
5f5c8ee5
GM
9264 : "no buffer"),
9265 buffer);
9266}
a2889657 9267
5f5c8ee5 9268#endif /* GLYPH_DEBUG */
90adcf20 9269
a2889657 9270
5f5c8ee5
GM
9271/* Value is non-zero if all changes in window W, which displays
9272 current_buffer, are in the text between START and END. START is a
9273 buffer position, END is given as a distance from Z. Used in
9274 redisplay_internal for display optimization. */
9275
9276static INLINE int
9277text_outside_line_unchanged_p (w, start, end)
9278 struct window *w;
9279 int start, end;
9280{
9281 int unchanged_p = 1;
2311178e 9282
5f5c8ee5
GM
9283 /* If text or overlays have changed, see where. */
9284 if (XFASTINT (w->last_modified) < MODIFF
9285 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9286 {
9287 /* Gap in the line? */
9288 if (GPT < start || Z - GPT < end)
9289 unchanged_p = 0;
9290
9291 /* Changes start in front of the line, or end after it? */
9292 if (unchanged_p
9142dd5b
GM
9293 && (BEG_UNCHANGED < start - 1
9294 || END_UNCHANGED < end))
5f5c8ee5 9295 unchanged_p = 0;
2311178e 9296
5f5c8ee5
GM
9297 /* If selective display, can't optimize if changes start at the
9298 beginning of the line. */
9299 if (unchanged_p
9300 && INTEGERP (current_buffer->selective_display)
9301 && XINT (current_buffer->selective_display) > 0
9142dd5b 9302 && (BEG_UNCHANGED < start || GPT <= start))
5f5c8ee5 9303 unchanged_p = 0;
47d57b22
GM
9304
9305 /* If there are overlays at the start or end of the line, these
9306 may have overlay strings with newlines in them. A change at
9307 START, for instance, may actually concern the display of such
9308 overlay strings as well, and they are displayed on different
9309 lines. So, quickly rule out this case. (For the future, it
9310 might be desirable to implement something more telling than
9311 just BEG/END_UNCHANGED.) */
9312 if (unchanged_p)
9313 {
9314 if (BEG + BEG_UNCHANGED == start
9315 && overlay_touches_p (start))
9316 unchanged_p = 0;
9317 if (END_UNCHANGED == end
9318 && overlay_touches_p (Z - end))
9319 unchanged_p = 0;
9320 }
5f5c8ee5
GM
9321 }
9322
9323 return unchanged_p;
9324}
9325
9326
9327/* Do a frame update, taking possible shortcuts into account. This is
9328 the main external entry point for redisplay.
9329
9330 If the last redisplay displayed an echo area message and that message
9331 is no longer requested, we clear the echo area or bring back the
9332 mini-buffer if that is in use. */
20de20dc 9333
a2889657
JB
9334void
9335redisplay ()
e9874cee
RS
9336{
9337 redisplay_internal (0);
9338}
9339
47d57b22 9340
351b5434
KS
9341static Lisp_Object
9342overlay_arrow_string_or_property (var, pbitmap)
9343 Lisp_Object var;
9344 int *pbitmap;
9345{
9346 Lisp_Object pstr = Fget (var, Qoverlay_arrow_string);
9347 Lisp_Object bitmap;
9348
9349 if (pbitmap)
9350 {
9351 *pbitmap = 0;
9352 if (bitmap = Fget (var, Qoverlay_arrow_bitmap), INTEGERP (bitmap))
9353 *pbitmap = XINT (bitmap);
9354 }
9355
9356 if (!NILP (pstr))
9357 return pstr;
9358 return Voverlay_arrow_string;
9359}
9360
9361/* Return 1 if there are any overlay-arrows in current_buffer. */
9362static int
9363overlay_arrow_in_current_buffer_p ()
9364{
9365 Lisp_Object vlist;
9366
9367 for (vlist = Voverlay_arrow_variable_list;
9368 CONSP (vlist);
9369 vlist = XCDR (vlist))
9370 {
9371 Lisp_Object var = XCAR (vlist);
9372 Lisp_Object val;
9373
9374 if (!SYMBOLP (var))
9375 continue;
9376 val = find_symbol_value (var);
9377 if (MARKERP (val)
9378 && current_buffer == XMARKER (val)->buffer)
9379 return 1;
9380 }
9381 return 0;
9382}
9383
9384
9385/* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9386 has changed. */
9387
9388static int
9389overlay_arrows_changed_p ()
9390{
9391 Lisp_Object vlist;
9392
9393 for (vlist = Voverlay_arrow_variable_list;
9394 CONSP (vlist);
9395 vlist = XCDR (vlist))
9396 {
9397 Lisp_Object var = XCAR (vlist);
9398 Lisp_Object val, pstr;
9399
9400 if (!SYMBOLP (var))
9401 continue;
9402 val = find_symbol_value (var);
9403 if (!MARKERP (val))
9404 continue;
9405 if (! EQ (COERCE_MARKER (val),
9406 Fget (var, Qlast_arrow_position))
9407 || ! (pstr = overlay_arrow_string_or_property (var, 0),
9408 EQ (pstr, Fget (var, Qlast_arrow_string))))
9409 return 1;
9410 }
9411 return 0;
9412}
9413
9414/* Mark overlay arrows to be updated on next redisplay. */
9415
9416static void
9417update_overlay_arrows (up_to_date)
9418 int up_to_date;
9419{
9420 Lisp_Object vlist;
9421
9422 for (vlist = Voverlay_arrow_variable_list;
9423 CONSP (vlist);
9424 vlist = XCDR (vlist))
9425 {
9426 Lisp_Object var = XCAR (vlist);
351b5434
KS
9427
9428 if (!SYMBOLP (var))
9429 continue;
9430
9431 if (up_to_date)
9432 {
7d90b587 9433 Lisp_Object val = find_symbol_value (var);
351b5434 9434 Fput (var, Qlast_arrow_position,
7d90b587 9435 COERCE_MARKER (val));
351b5434
KS
9436 Fput (var, Qlast_arrow_string,
9437 overlay_arrow_string_or_property (var, 0));
9438 }
9439 else if (up_to_date < 0
9440 || !NILP (Fget (var, Qlast_arrow_position)))
9441 {
9442 Fput (var, Qlast_arrow_position, Qt);
9443 Fput (var, Qlast_arrow_string, Qt);
9444 }
9445 }
9446}
9447
9448
9449/* Return overlay arrow string at row, or nil. */
9450
9451static Lisp_Object
9452overlay_arrow_at_row (f, row, pbitmap)
9453 struct frame *f;
9454 struct glyph_row *row;
9455 int *pbitmap;
9456{
9457 Lisp_Object vlist;
9458
9459 for (vlist = Voverlay_arrow_variable_list;
9460 CONSP (vlist);
9461 vlist = XCDR (vlist))
9462 {
9463 Lisp_Object var = XCAR (vlist);
9464 Lisp_Object val;
9465
9466 if (!SYMBOLP (var))
9467 continue;
9468
9469 val = find_symbol_value (var);
be21b925 9470
351b5434
KS
9471 if (MARKERP (val)
9472 && current_buffer == XMARKER (val)->buffer
9473 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
9474 {
9475 val = overlay_arrow_string_or_property (var, pbitmap);
9476 if (FRAME_WINDOW_P (f))
9477 return Qt;
9478 else if (STRINGP (val))
9479 return val;
9480 break;
9481 }
9482 }
9483
9484 *pbitmap = 0;
9485 return Qnil;
9486}
9487
260a86a0
KH
9488/* Return 1 if point moved out of or into a composition. Otherwise
9489 return 0. PREV_BUF and PREV_PT are the last point buffer and
9490 position. BUF and PT are the current point buffer and position. */
9491
9492int
9493check_point_in_composition (prev_buf, prev_pt, buf, pt)
9494 struct buffer *prev_buf, *buf;
9495 int prev_pt, pt;
9496{
9497 int start, end;
9498 Lisp_Object prop;
9499 Lisp_Object buffer;
9500
9501 XSETBUFFER (buffer, buf);
9502 /* Check a composition at the last point if point moved within the
9503 same buffer. */
9504 if (prev_buf == buf)
9505 {
9506 if (prev_pt == pt)
9507 /* Point didn't move. */
9508 return 0;
2311178e 9509
260a86a0
KH
9510 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9511 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9512 && COMPOSITION_VALID_P (start, end, prop)
9513 && start < prev_pt && end > prev_pt)
9514 /* The last point was within the composition. Return 1 iff
9515 point moved out of the composition. */
9516 return (pt <= start || pt >= end);
9517 }
9518
9519 /* Check a composition at the current point. */
9520 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9521 && find_composition (pt, -1, &start, &end, &prop, buffer)
9522 && COMPOSITION_VALID_P (start, end, prop)
9523 && start < pt && end > pt);
9524}
5f5c8ee5 9525
47d57b22 9526
9142dd5b
GM
9527/* Reconsider the setting of B->clip_changed which is displayed
9528 in window W. */
9529
9530static INLINE void
9531reconsider_clip_changes (w, b)
9532 struct window *w;
9533 struct buffer *b;
9534{
c62c1bb5 9535 if (b->clip_changed
9142dd5b
GM
9536 && !NILP (w->window_end_valid)
9537 && w->current_matrix->buffer == b
9538 && w->current_matrix->zv == BUF_ZV (b)
9539 && w->current_matrix->begv == BUF_BEGV (b))
9540 b->clip_changed = 0;
260a86a0
KH
9541
9542 /* If display wasn't paused, and W is not a tool bar window, see if
9543 point has been moved into or out of a composition. In that case,
9544 we set b->clip_changed to 1 to force updating the screen. If
9545 b->clip_changed has already been set to 1, we can skip this
9546 check. */
9547 if (!b->clip_changed
9548 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9549 {
9550 int pt;
9551
9552 if (w == XWINDOW (selected_window))
9553 pt = BUF_PT (current_buffer);
9554 else
9555 pt = marker_position (w->pointm);
9556
9557 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
6fc556fd 9558 || pt != XINT (w->last_point))
260a86a0 9559 && check_point_in_composition (w->current_matrix->buffer,
6fc556fd 9560 XINT (w->last_point),
260a86a0
KH
9561 XBUFFER (w->buffer), pt))
9562 b->clip_changed = 1;
9563 }
9142dd5b 9564}
aac2d8b2 9565\f
dd429b03
KH
9566
9567/* Select FRAME to forward the values of frame-local variables into C
9568 variables so that the redisplay routines can access those values
9569 directly. */
9570
9571static void
9572select_frame_for_redisplay (frame)
9573 Lisp_Object frame;
9574{
9575 Lisp_Object tail, sym, val;
9576 Lisp_Object old = selected_frame;
be21b925 9577
dd429b03
KH
9578 selected_frame = frame;
9579
9580 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
9581 if (CONSP (XCAR (tail))
9582 && (sym = XCAR (XCAR (tail)),
9583 SYMBOLP (sym))
9584 && (sym = indirect_variable (sym),
9585 val = SYMBOL_VALUE (sym),
9586 (BUFFER_LOCAL_VALUEP (val)
9587 || SOME_BUFFER_LOCAL_VALUEP (val)))
9588 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9589 Fsymbol_value (sym);
9590
9591 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
9592 if (CONSP (XCAR (tail))
9593 && (sym = XCAR (XCAR (tail)),
9594 SYMBOLP (sym))
9595 && (sym = indirect_variable (sym),
9596 val = SYMBOL_VALUE (sym),
9597 (BUFFER_LOCAL_VALUEP (val)
9598 || SOME_BUFFER_LOCAL_VALUEP (val)))
9599 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9600 Fsymbol_value (sym);
9601}
9602
9603
aac2d8b2
RS
9604#define STOP_POLLING \
9605do { if (! polling_stopped_here) stop_polling (); \
9606 polling_stopped_here = 1; } while (0)
9607
9608#define RESUME_POLLING \
9609do { if (polling_stopped_here) start_polling (); \
9610 polling_stopped_here = 0; } while (0)
9142dd5b
GM
9611
9612
5f5c8ee5
GM
9613/* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9614 response to any user action; therefore, we should preserve the echo
9615 area. (Actually, our caller does that job.) Perhaps in the future
9616 avoid recentering windows if it is not necessary; currently that
9617 causes some problems. */
e9874cee
RS
9618
9619static void
9620redisplay_internal (preserve_echo_area)
9621 int preserve_echo_area;
a2889657 9622{
5f5c8ee5
GM
9623 struct window *w = XWINDOW (selected_window);
9624 struct frame *f = XFRAME (w->frame);
9625 int pause;
a2889657 9626 int must_finish = 0;
5f5c8ee5 9627 struct text_pos tlbufpos, tlendpos;
89819bdd 9628 int number_of_visible_frames;
28514cd9 9629 int count;
886bd6f2 9630 struct frame *sf = SELECTED_FRAME ();
aac2d8b2 9631 int polling_stopped_here = 0;
a2889657 9632
5f5c8ee5
GM
9633 /* Non-zero means redisplay has to consider all windows on all
9634 frames. Zero means, only selected_window is considered. */
9635 int consider_all_windows_p;
2311178e 9636
5f5c8ee5
GM
9637 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9638
9639 /* No redisplay if running in batch mode or frame is not yet fully
9640 initialized, or redisplay is explicitly turned off by setting
9641 Vinhibit_redisplay. */
9642 if (noninteractive
9643 || !NILP (Vinhibit_redisplay)
9644 || !f->glyphs_initialized_p)
a2889657
JB
9645 return;
9646
5f5c8ee5
GM
9647 /* The flag redisplay_performed_directly_p is set by
9648 direct_output_for_insert when it already did the whole screen
9649 update necessary. */
9650 if (redisplay_performed_directly_p)
9651 {
9652 redisplay_performed_directly_p = 0;
9653 if (!hscroll_windows (selected_window))
9654 return;
9655 }
9656
488dd4c4 9657#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15f0cf78
RS
9658 if (popup_activated ())
9659 return;
9660#endif
9661
28514cd9 9662 /* I don't think this happens but let's be paranoid. */
5f5c8ee5 9663 if (redisplaying_p)
735c094c
KH
9664 return;
9665
28514cd9
GM
9666 /* Record a function that resets redisplaying_p to its old value
9667 when we leave this function. */
331379bf 9668 count = SPECPDL_INDEX ();
dd429b03
KH
9669 record_unwind_protect (unwind_redisplay,
9670 Fcons (make_number (redisplaying_p), selected_frame));
28514cd9 9671 ++redisplaying_p;
26683087 9672 specbind (Qinhibit_free_realized_faces, Qnil);
2311178e 9673
8b32d885 9674 retry:
bd9d0f3f 9675 pause = 0;
9142dd5b
GM
9676 reconsider_clip_changes (w, current_buffer);
9677
5f5c8ee5
GM
9678 /* If new fonts have been loaded that make a glyph matrix adjustment
9679 necessary, do it. */
9680 if (fonts_changed_p)
9681 {
9682 adjust_glyphs (NULL);
9683 ++windows_or_buffers_changed;
9684 fonts_changed_p = 0;
9685 }
9686
6961e0c1
GM
9687 /* If face_change_count is non-zero, init_iterator will free all
9688 realized faces, which includes the faces referenced from current
9689 matrices. So, we can't reuse current matrices in this case. */
9690 if (face_change_count)
9691 ++windows_or_buffers_changed;
9692
886bd6f2
GM
9693 if (! FRAME_WINDOW_P (sf)
9694 && previous_terminal_frame != sf)
20de20dc 9695 {
5f5c8ee5
GM
9696 /* Since frames on an ASCII terminal share the same display
9697 area, displaying a different frame means redisplay the whole
9698 thing. */
20de20dc 9699 windows_or_buffers_changed++;
886bd6f2
GM
9700 SET_FRAME_GARBAGED (sf);
9701 XSETFRAME (Vterminal_frame, sf);
20de20dc 9702 }
886bd6f2 9703 previous_terminal_frame = sf;
20de20dc 9704
5f5c8ee5
GM
9705 /* Set the visible flags for all frames. Do this before checking
9706 for resized or garbaged frames; they want to know if their frames
9707 are visible. See the comment in frame.h for
9708 FRAME_SAMPLE_VISIBILITY. */
d724d989 9709 {
35f56f96 9710 Lisp_Object tail, frame;
d724d989 9711
89819bdd
RS
9712 number_of_visible_frames = 0;
9713
35f56f96 9714 FOR_EACH_FRAME (tail, frame)
f82aff7c 9715 {
5f5c8ee5 9716 struct frame *f = XFRAME (frame);
2311178e 9717
5f5c8ee5
GM
9718 FRAME_SAMPLE_VISIBILITY (f);
9719 if (FRAME_VISIBLE_P (f))
9720 ++number_of_visible_frames;
9721 clear_desired_matrices (f);
f82aff7c 9722 }
d724d989
JB
9723 }
9724
44fa5b1e 9725 /* Notice any pending interrupt request to change frame size. */
c6e89d6c 9726 do_pending_window_change (1);
a2889657 9727
5f5c8ee5 9728 /* Clear frames marked as garbaged. */
44fa5b1e 9729 if (frame_garbaged)
c6e89d6c 9730 clear_garbaged_frames ();
a2889657 9731
e037b9ec 9732 /* Build menubar and tool-bar items. */
f82aff7c
RS
9733 prepare_menu_bars ();
9734
28995e67 9735 if (windows_or_buffers_changed)
a2889657
JB
9736 update_mode_lines++;
9737
538f13d4
RS
9738 /* Detect case that we need to write or remove a star in the mode line. */
9739 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
a2889657
JB
9740 {
9741 w->update_mode_line = Qt;
9742 if (buffer_shared > 1)
9743 update_mode_lines++;
9744 }
9745
5f5c8ee5 9746 /* If %c is in the mode line, update it if needed. */
28995e67
RS
9747 if (!NILP (w->column_number_displayed)
9748 /* This alternative quickly identifies a common case
9749 where no change is needed. */
9750 && !(PT == XFASTINT (w->last_point)
8850a573
RS
9751 && XFASTINT (w->last_modified) >= MODIFF
9752 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
2311178e
TTN
9753 && (XFASTINT (w->column_number_displayed)
9754 != (int) current_column ())) /* iftc */
9755 w->update_mode_line = Qt;
28995e67 9756
44fa5b1e 9757 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
a2889657 9758
5f5c8ee5
GM
9759 /* The variable buffer_shared is set in redisplay_window and
9760 indicates that we redisplay a buffer in different windows. See
9761 there. */
5fb96e96
RS
9762 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
9763 || cursor_type_changed);
a2889657
JB
9764
9765 /* If specs for an arrow have changed, do thorough redisplay
9766 to ensure we remove any arrow that should no longer exist. */
351b5434 9767 if (overlay_arrows_changed_p ())
5f5c8ee5 9768 consider_all_windows_p = windows_or_buffers_changed = 1;
a2889657 9769
90adcf20
RS
9770 /* Normally the message* functions will have already displayed and
9771 updated the echo area, but the frame may have been trashed, or
9772 the update may have been preempted, so display the echo area
6e019995 9773 again here. Checking message_cleared_p captures the case that
c6e89d6c 9774 the echo area should be cleared. */
96d5e9a0
GM
9775 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
9776 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
3fa69ee7
GM
9777 || (message_cleared_p
9778 && minibuf_level == 0
9779 /* If the mini-window is currently selected, this means the
9780 echo-area doesn't show through. */
9781 && !MINI_WINDOW_P (XWINDOW (selected_window))))
90adcf20 9782 {
c6e89d6c 9783 int window_height_changed_p = echo_area_display (0);
90adcf20 9784 must_finish = 1;
6e019995
GM
9785
9786 /* If we don't display the current message, don't clear the
9787 message_cleared_p flag, because, if we did, we wouldn't clear
9788 the echo area in the next redisplay which doesn't preserve
9789 the echo area. */
9790 if (!display_last_displayed_message_p)
9791 message_cleared_p = 0;
2311178e 9792
c6e89d6c
GM
9793 if (fonts_changed_p)
9794 goto retry;
9795 else if (window_height_changed_p)
9796 {
9797 consider_all_windows_p = 1;
9798 ++update_mode_lines;
9799 ++windows_or_buffers_changed;
2311178e 9800
9142dd5b
GM
9801 /* If window configuration was changed, frames may have been
9802 marked garbaged. Clear them or we will experience
9803 surprises wrt scrolling. */
9804 if (frame_garbaged)
9805 clear_garbaged_frames ();
c6e89d6c 9806 }
90adcf20 9807 }
97a36635 9808 else if (EQ (selected_window, minibuf_window)
dd2eb166
GM
9809 && (current_buffer->clip_changed
9810 || XFASTINT (w->last_modified) < MODIFF
9811 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
92a90e89 9812 && resize_mini_window (w, 0))
c6e89d6c
GM
9813 {
9814 /* Resized active mini-window to fit the size of what it is
dd2eb166
GM
9815 showing if its contents might have changed. */
9816 must_finish = 1;
9817 consider_all_windows_p = 1;
c6e89d6c 9818 ++windows_or_buffers_changed;
dd2eb166 9819 ++update_mode_lines;
2311178e 9820
9142dd5b
GM
9821 /* If window configuration was changed, frames may have been
9822 marked garbaged. Clear them or we will experience
9823 surprises wrt scrolling. */
9824 if (frame_garbaged)
9825 clear_garbaged_frames ();
c6e89d6c 9826 }
2311178e 9827
90adcf20 9828
5f5c8ee5
GM
9829 /* If showing the region, and mark has changed, we must redisplay
9830 the whole window. The assignment to this_line_start_pos prevents
9831 the optimization directly below this if-statement. */
bd66d1ba
RS
9832 if (((!NILP (Vtransient_mark_mode)
9833 && !NILP (XBUFFER (w->buffer)->mark_active))
9834 != !NILP (w->region_showing))
82d04750
JB
9835 || (!NILP (w->region_showing)
9836 && !EQ (w->region_showing,
9837 Fmarker_position (XBUFFER (w->buffer)->mark))))
5f5c8ee5
GM
9838 CHARPOS (this_line_start_pos) = 0;
9839
9840 /* Optimize the case that only the line containing the cursor in the
9841 selected window has changed. Variables starting with this_ are
9842 set in display_line and record information about the line
9843 containing the cursor. */
9844 tlbufpos = this_line_start_pos;
9845 tlendpos = this_line_end_pos;
9846 if (!consider_all_windows_p
9847 && CHARPOS (tlbufpos) > 0
9848 && NILP (w->update_mode_line)
73af359d 9849 && !current_buffer->clip_changed
c62c1bb5 9850 && !current_buffer->prevent_redisplay_optimizations_p
44fa5b1e 9851 && FRAME_VISIBLE_P (XFRAME (w->frame))
f21ef775 9852 && !FRAME_OBSCURED_P (XFRAME (w->frame))
5f5c8ee5 9853 /* Make sure recorded data applies to current buffer, etc. */
a2889657
JB
9854 && this_line_buffer == current_buffer
9855 && current_buffer == XBUFFER (w->buffer)
265a9e55 9856 && NILP (w->force_start)
acda20e1 9857 && NILP (w->optional_new_start)
5f5c8ee5
GM
9858 /* Point must be on the line that we have info recorded about. */
9859 && PT >= CHARPOS (tlbufpos)
9860 && PT <= Z - CHARPOS (tlendpos)
a2889657
JB
9861 /* All text outside that line, including its final newline,
9862 must be unchanged */
5f5c8ee5
GM
9863 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
9864 CHARPOS (tlendpos)))
9865 {
9866 if (CHARPOS (tlbufpos) > BEGV
9867 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
9868 && (CHARPOS (tlbufpos) == ZV
9869 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
a2889657
JB
9870 /* Former continuation line has disappeared by becoming empty */
9871 goto cancel;
9872 else if (XFASTINT (w->last_modified) < MODIFF
8850a573 9873 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
a2889657
JB
9874 || MINI_WINDOW_P (w))
9875 {
1c9241f5
KH
9876 /* We have to handle the case of continuation around a
9877 wide-column character (See the comment in indent.c around
9878 line 885).
9879
9880 For instance, in the following case:
9881
9882 -------- Insert --------
9883 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
9884 J_I_ ==> J_I_ `^^' are cursors.
9885 ^^ ^^
9886 -------- --------
9887
9888 As we have to redraw the line above, we should goto cancel. */
9889
5f5c8ee5
GM
9890 struct it it;
9891 int line_height_before = this_line_pixel_height;
9892
9893 /* Note that start_display will handle the case that the
9894 line starting at tlbufpos is a continuation lines. */
9895 start_display (&it, w, tlbufpos);
9896
9897 /* Implementation note: It this still necessary? */
9898 if (it.current_x != this_line_start_x)
1c9241f5
KH
9899 goto cancel;
9900
5f5c8ee5
GM
9901 TRACE ((stderr, "trying display optimization 1\n"));
9902 w->cursor.vpos = -1;
a2889657 9903 overlay_arrow_seen = 0;
5f5c8ee5
GM
9904 it.vpos = this_line_vpos;
9905 it.current_y = this_line_y;
9906 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
9907 display_line (&it);
9908
a2889657 9909 /* If line contains point, is not continued,
5f5c8ee5 9910 and ends at same distance from eob as before, we win */
2311178e 9911 if (w->cursor.vpos >= 0
5f5c8ee5
GM
9912 /* Line is not continued, otherwise this_line_start_pos
9913 would have been set to 0 in display_line. */
9914 && CHARPOS (this_line_start_pos)
9915 /* Line ends as before. */
9916 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
9917 /* Line has same height as before. Otherwise other lines
9918 would have to be shifted up or down. */
9919 && this_line_pixel_height == line_height_before)
a2889657 9920 {
5f5c8ee5
GM
9921 /* If this is not the window's last line, we must adjust
9922 the charstarts of the lines below. */
9923 if (it.current_y < it.last_visible_y)
9924 {
9925 struct glyph_row *row
9926 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
9927 int delta, delta_bytes;
2311178e 9928
5f5c8ee5
GM
9929 if (Z - CHARPOS (tlendpos) == ZV)
9930 {
9931 /* This line ends at end of (accessible part of)
9932 buffer. There is no newline to count. */
9933 delta = (Z
9934 - CHARPOS (tlendpos)
9935 - MATRIX_ROW_START_CHARPOS (row));
9936 delta_bytes = (Z_BYTE
9937 - BYTEPOS (tlendpos)
9938 - MATRIX_ROW_START_BYTEPOS (row));
9939 }
9940 else
9941 {
9942 /* This line ends in a newline. Must take
9943 account of the newline and the rest of the
9944 text that follows. */
9945 delta = (Z
9946 - CHARPOS (tlendpos)
9947 - MATRIX_ROW_START_CHARPOS (row));
9948 delta_bytes = (Z_BYTE
9949 - BYTEPOS (tlendpos)
9950 - MATRIX_ROW_START_BYTEPOS (row));
9951 }
2311178e 9952
f2d86d7a
GM
9953 increment_matrix_positions (w->current_matrix,
9954 this_line_vpos + 1,
9955 w->current_matrix->nrows,
9956 delta, delta_bytes);
85bcef6c 9957 }
46db8486 9958
5f5c8ee5
GM
9959 /* If this row displays text now but previously didn't,
9960 or vice versa, w->window_end_vpos may have to be
9961 adjusted. */
9962 if ((it.glyph_row - 1)->displays_text_p)
9963 {
9964 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
9965 XSETINT (w->window_end_vpos, this_line_vpos);
9966 }
9967 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
9968 && this_line_vpos > 0)
9969 XSETINT (w->window_end_vpos, this_line_vpos - 1);
9970 w->window_end_valid = Qnil;
2311178e 9971
5f5c8ee5
GM
9972 /* Update hint: No need to try to scroll in update_window. */
9973 w->desired_matrix->no_scrolling_p = 1;
9974
9975#if GLYPH_DEBUG
9976 *w->desired_matrix->method = 0;
9977 debug_method_add (w, "optimization 1");
9978#endif
7af0e8d7 9979#ifdef HAVE_WINDOW_SYSTEM
88e6b646 9980 update_window_fringes (w, 0);
7af0e8d7 9981#endif
a2889657
JB
9982 goto update;
9983 }
9984 else
9985 goto cancel;
9986 }
5f5c8ee5
GM
9987 else if (/* Cursor position hasn't changed. */
9988 PT == XFASTINT (w->last_point)
b6f0fe04
RS
9989 /* Make sure the cursor was last displayed
9990 in this window. Otherwise we have to reposition it. */
5f5c8ee5 9991 && 0 <= w->cursor.vpos
da8b7f4f 9992 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
a2889657
JB
9993 {
9994 if (!must_finish)
9995 {
c6e89d6c 9996 do_pending_window_change (1);
5f5c8ee5 9997
2311178e 9998 /* We used to always goto end_of_redisplay here, but this
5f5c8ee5
GM
9999 isn't enough if we have a blinking cursor. */
10000 if (w->cursor_off_p == w->last_cursor_off_p)
10001 goto end_of_redisplay;
a2889657
JB
10002 }
10003 goto update;
10004 }
8b51f1e3
KH
10005 /* If highlighting the region, or if the cursor is in the echo area,
10006 then we can't just move the cursor. */
bd66d1ba
RS
10007 else if (! (!NILP (Vtransient_mark_mode)
10008 && !NILP (current_buffer->mark_active))
97a36635 10009 && (EQ (selected_window, current_buffer->last_selected_window)
293a54ce 10010 || highlight_nonselected_windows)
8b51f1e3 10011 && NILP (w->region_showing)
8f897821 10012 && NILP (Vshow_trailing_whitespace)
8b51f1e3 10013 && !cursor_in_echo_area)
a2889657 10014 {
5f5c8ee5
GM
10015 struct it it;
10016 struct glyph_row *row;
10017
10018 /* Skip from tlbufpos to PT and see where it is. Note that
10019 PT may be in invisible text. If so, we will end at the
10020 next visible position. */
10021 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10022 NULL, DEFAULT_FACE_ID);
10023 it.current_x = this_line_start_x;
10024 it.current_y = this_line_y;
10025 it.vpos = this_line_vpos;
2311178e 10026
5f5c8ee5
GM
10027 /* The call to move_it_to stops in front of PT, but
10028 moves over before-strings. */
10029 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10030
10031 if (it.vpos == this_line_vpos
10032 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10033 row->enabled_p))
a2889657 10034 {
5f5c8ee5
GM
10035 xassert (this_line_vpos == it.vpos);
10036 xassert (this_line_y == it.current_y);
10037 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
ade0bee1
GM
10038#if GLYPH_DEBUG
10039 *w->desired_matrix->method = 0;
10040 debug_method_add (w, "optimization 3");
10041#endif
a2889657
JB
10042 goto update;
10043 }
10044 else
10045 goto cancel;
10046 }
5f5c8ee5 10047
a2889657 10048 cancel:
5f5c8ee5
GM
10049 /* Text changed drastically or point moved off of line. */
10050 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
a2889657
JB
10051 }
10052
5f5c8ee5
GM
10053 CHARPOS (this_line_start_pos) = 0;
10054 consider_all_windows_p |= buffer_shared > 1;
10055 ++clear_face_cache_count;
a2889657 10056
2311178e 10057
bd9d0f3f
GM
10058 /* Build desired matrices, and update the display. If
10059 consider_all_windows_p is non-zero, do it for all windows on all
10060 frames. Otherwise do it for selected_window, only. */
463f6b91 10061
5f5c8ee5 10062 if (consider_all_windows_p)
a2889657 10063 {
35f56f96 10064 Lisp_Object tail, frame;
0528abe1
GM
10065 int i, n = 0, size = 50;
10066 struct frame **updated
10067 = (struct frame **) alloca (size * sizeof *updated);
a2889657 10068
5f5c8ee5
GM
10069 /* Clear the face cache eventually. */
10070 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
463f6b91 10071 {
5f5c8ee5 10072 clear_face_cache (0);
463f6b91
RS
10073 clear_face_cache_count = 0;
10074 }
31b24551 10075
5f5c8ee5
GM
10076 /* Recompute # windows showing selected buffer. This will be
10077 incremented each time such a window is displayed. */
a2889657
JB
10078 buffer_shared = 0;
10079
35f56f96 10080 FOR_EACH_FRAME (tail, frame)
30c566e4 10081 {
5f5c8ee5 10082 struct frame *f = XFRAME (frame);
2311178e 10083
886bd6f2 10084 if (FRAME_WINDOW_P (f) || f == sf)
9769686d 10085 {
dd429b03
KH
10086 if (! EQ (frame, selected_frame))
10087 /* Select the frame, for the sake of frame-local
10088 variables. */
10089 select_frame_for_redisplay (frame);
10090
ae02e06a 10091#ifdef HAVE_WINDOW_SYSTEM
bb336f8d
RS
10092 if (clear_face_cache_count % 50 == 0
10093 && FRAME_WINDOW_P (f))
10094 clear_image_cache (f, 0);
ae02e06a 10095#endif /* HAVE_WINDOW_SYSTEM */
bb336f8d 10096
5f5c8ee5
GM
10097 /* Mark all the scroll bars to be removed; we'll redeem
10098 the ones we want when we redisplay their windows. */
9769686d 10099 if (condemn_scroll_bars_hook)
504454e8 10100 condemn_scroll_bars_hook (f);
30c566e4 10101
f21ef775 10102 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
5f5c8ee5 10103 redisplay_windows (FRAME_ROOT_WINDOW (f));
30c566e4 10104
5f5c8ee5
GM
10105 /* Any scroll bars which redisplay_windows should have
10106 nuked should now go away. */
9769686d 10107 if (judge_scroll_bars_hook)
504454e8 10108 judge_scroll_bars_hook (f);
bd9d0f3f
GM
10109
10110 /* If fonts changed, display again. */
b60c9653
RS
10111 /* ??? rms: I suspect it is a mistake to jump all the way
10112 back to retry here. It should just retry this frame. */
bd9d0f3f
GM
10113 if (fonts_changed_p)
10114 goto retry;
2311178e 10115
bd9d0f3f
GM
10116 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10117 {
10118 /* See if we have to hscroll. */
10119 if (hscroll_windows (f->root_window))
10120 goto retry;
10121
10122 /* Prevent various kinds of signals during display
10123 update. stdio is not robust about handling
10124 signals, which can cause an apparent I/O
10125 error. */
10126 if (interrupt_input)
10127 unrequest_sigio ();
aac2d8b2 10128 STOP_POLLING;
bd9d0f3f
GM
10129
10130 /* Update the display. */
10131 set_window_update_flags (XWINDOW (f->root_window), 1);
10132 pause |= update_frame (f, 0, 0);
ccbb9ed2 10133#if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
bd9d0f3f
GM
10134 if (pause)
10135 break;
ccbb9ed2 10136#endif
bd9d0f3f 10137
0528abe1
GM
10138 if (n == size)
10139 {
10140 int nbytes = size * sizeof *updated;
10141 struct frame **p = (struct frame **) alloca (2 * nbytes);
10142 bcopy (updated, p, nbytes);
10143 size *= 2;
10144 }
2311178e 10145
0528abe1 10146 updated[n++] = f;
bd9d0f3f 10147 }
9769686d 10148 }
30c566e4 10149 }
0528abe1 10150
6f68b035
GM
10151 if (!pause)
10152 {
10153 /* Do the mark_window_display_accurate after all windows have
10154 been redisplayed because this call resets flags in buffers
10155 which are needed for proper redisplay. */
10156 for (i = 0; i < n; ++i)
10157 {
10158 struct frame *f = updated[i];
10159 mark_window_display_accurate (f->root_window, 1);
10160 if (frame_up_to_date_hook)
10161 frame_up_to_date_hook (f);
10162 }
0528abe1 10163 }
a2889657 10164 }
bd9d0f3f
GM
10165 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10166 {
10167 Lisp_Object mini_window;
10168 struct frame *mini_frame;
5f5c8ee5 10169
82a7ab23 10170 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
7033d6df
RS
10171 /* Use list_of_error, not Qerror, so that
10172 we catch only errors and don't run the debugger. */
10173 internal_condition_case_1 (redisplay_window_1, selected_window,
10174 list_of_error,
82a7ab23 10175 redisplay_window_error);
2311178e 10176
bd9d0f3f 10177 /* Compare desired and current matrices, perform output. */
7d0393cf 10178
26683087 10179 update:
bd9d0f3f
GM
10180 /* If fonts changed, display again. */
10181 if (fonts_changed_p)
92a90e89 10182 goto retry;
a2889657 10183
bd9d0f3f
GM
10184 /* Prevent various kinds of signals during display update.
10185 stdio is not robust about handling signals,
10186 which can cause an apparent I/O error. */
10187 if (interrupt_input)
10188 unrequest_sigio ();
aac2d8b2 10189 STOP_POLLING;
1af9f229 10190
bd9d0f3f 10191 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
5f5c8ee5 10192 {
92a90e89
GM
10193 if (hscroll_windows (selected_window))
10194 goto retry;
2311178e 10195
5f5c8ee5 10196 XWINDOW (selected_window)->must_be_updated_p = 1;
886bd6f2 10197 pause = update_frame (sf, 0, 0);
5f5c8ee5 10198 }
d724d989 10199
8de2d90b 10200 /* We may have called echo_area_display at the top of this
44fa5b1e
JB
10201 function. If the echo area is on another frame, that may
10202 have put text on a frame other than the selected one, so the
10203 above call to update_frame would not have caught it. Catch
8de2d90b 10204 it here. */
bd9d0f3f
GM
10205 mini_window = FRAME_MINIBUF_WINDOW (sf);
10206 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
2311178e 10207
bd9d0f3f
GM
10208 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10209 {
10210 XWINDOW (mini_window)->must_be_updated_p = 1;
10211 pause |= update_frame (mini_frame, 0, 0);
10212 if (!pause && hscroll_windows (mini_window))
10213 goto retry;
10214 }
6e8290aa 10215 }
a2889657 10216
5f5c8ee5
GM
10217 /* If display was paused because of pending input, make sure we do a
10218 thorough update the next time. */
a2889657
JB
10219 if (pause)
10220 {
5f5c8ee5
GM
10221 /* Prevent the optimization at the beginning of
10222 redisplay_internal that tries a single-line update of the
10223 line containing the cursor in the selected window. */
10224 CHARPOS (this_line_start_pos) = 0;
10225
10226 /* Let the overlay arrow be updated the next time. */
351b5434 10227 update_overlay_arrows (0);
2311178e 10228
5f5c8ee5
GM
10229 /* If we pause after scrolling, some rows in the current
10230 matrices of some windows are not valid. */
10231 if (!WINDOW_FULL_WIDTH_P (w)
10232 && !FRAME_WINDOW_P (XFRAME (w->frame)))
a2889657
JB
10233 update_mode_lines = 1;
10234 }
43c09969 10235 else
a2889657 10236 {
43c09969 10237 if (!consider_all_windows_p)
a2889657 10238 {
43c09969
GM
10239 /* This has already been done above if
10240 consider_all_windows_p is set. */
10241 mark_window_display_accurate_1 (w, 1);
2311178e 10242
351b5434
KS
10243 /* Say overlay arrows are up to date. */
10244 update_overlay_arrows (1);
2311178e 10245
efc63ef0 10246 if (frame_up_to_date_hook != 0)
43c09969 10247 frame_up_to_date_hook (sf);
a2889657 10248 }
15e26c76 10249
a2889657
JB
10250 update_mode_lines = 0;
10251 windows_or_buffers_changed = 0;
5fb96e96 10252 cursor_type_changed = 0;
a2889657
JB
10253 }
10254
5f5c8ee5
GM
10255 /* Start SIGIO interrupts coming again. Having them off during the
10256 code above makes it less likely one will discard output, but not
10257 impossible, since there might be stuff in the system buffer here.
a2889657 10258 But it is much hairier to try to do anything about that. */
a2889657
JB
10259 if (interrupt_input)
10260 request_sigio ();
aac2d8b2 10261 RESUME_POLLING;
a2889657 10262
5f5c8ee5
GM
10263 /* If a frame has become visible which was not before, redisplay
10264 again, so that we display it. Expose events for such a frame
10265 (which it gets when becoming visible) don't call the parts of
10266 redisplay constructing glyphs, so simply exposing a frame won't
10267 display anything in this case. So, we have to display these
10268 frames here explicitly. */
11c52c4f
RS
10269 if (!pause)
10270 {
10271 Lisp_Object tail, frame;
10272 int new_count = 0;
10273
10274 FOR_EACH_FRAME (tail, frame)
10275 {
10276 int this_is_visible = 0;
8e83f802
RS
10277
10278 if (XFRAME (frame)->visible)
10279 this_is_visible = 1;
10280 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10281 if (XFRAME (frame)->visible)
10282 this_is_visible = 1;
11c52c4f
RS
10283
10284 if (this_is_visible)
10285 new_count++;
10286 }
10287
89819bdd 10288 if (new_count != number_of_visible_frames)
11c52c4f
RS
10289 windows_or_buffers_changed++;
10290 }
10291
44fa5b1e 10292 /* Change frame size now if a change is pending. */
c6e89d6c 10293 do_pending_window_change (1);
d8e242fd 10294
8b32d885
RS
10295 /* If we just did a pending size change, or have additional
10296 visible frames, redisplay again. */
3c8c72e0 10297 if (windows_or_buffers_changed && !pause)
8b32d885 10298 goto retry;
5f5c8ee5 10299
1987b083 10300 end_of_redisplay:
28514cd9 10301 unbind_to (count, Qnil);
aac2d8b2 10302 RESUME_POLLING;
a2889657
JB
10303}
10304
5f5c8ee5
GM
10305
10306/* Redisplay, but leave alone any recent echo area message unless
10307 another message has been requested in its place.
a2889657
JB
10308
10309 This is useful in situations where you need to redisplay but no
10310 user action has occurred, making it inappropriate for the message
10311 area to be cleared. See tracking_off and
9bf76936
GM
10312 wait_reading_process_input for examples of these situations.
10313
10314 FROM_WHERE is an integer saying from where this function was
10315 called. This is useful for debugging. */
a2889657 10316
8991bb31 10317void
9bf76936
GM
10318redisplay_preserve_echo_area (from_where)
10319 int from_where;
a2889657 10320{
9bf76936
GM
10321 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10322
c6e89d6c 10323 if (!NILP (echo_area_buffer[1]))
a2889657 10324 {
c6e89d6c
GM
10325 /* We have a previously displayed message, but no current
10326 message. Redisplay the previous message. */
10327 display_last_displayed_message_p = 1;
e9874cee 10328 redisplay_internal (1);
c6e89d6c 10329 display_last_displayed_message_p = 0;
a2889657
JB
10330 }
10331 else
e9874cee 10332 redisplay_internal (1);
a2889657
JB
10333}
10334
5f5c8ee5 10335
28514cd9 10336/* Function registered with record_unwind_protect in
acfca545
RS
10337 redisplay_internal. Reset redisplaying_p to the value it had
10338 before redisplay_internal was called, and clear
dd429b03
KH
10339 prevent_freeing_realized_faces_p. It also selects the previously
10340 selected frame. */
28514cd9
GM
10341
10342static Lisp_Object
dd429b03
KH
10343unwind_redisplay (val)
10344 Lisp_Object val;
28514cd9 10345{
dd429b03
KH
10346 Lisp_Object old_redisplaying_p, old_frame;
10347
10348 old_redisplaying_p = XCAR (val);
28514cd9 10349 redisplaying_p = XFASTINT (old_redisplaying_p);
dd429b03
KH
10350 old_frame = XCDR (val);
10351 if (! EQ (old_frame, selected_frame))
10352 select_frame_for_redisplay (old_frame);
c6e89d6c 10353 return Qnil;
28514cd9
GM
10354}
10355
10356
43c09969
GM
10357/* Mark the display of window W as accurate or inaccurate. If
10358 ACCURATE_P is non-zero mark display of W as accurate. If
10359 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10360 redisplay_internal is called. */
5f5c8ee5 10361
43c09969
GM
10362static void
10363mark_window_display_accurate_1 (w, accurate_p)
10364 struct window *w;
5f5c8ee5 10365 int accurate_p;
a2889657 10366{
43c09969 10367 if (BUFFERP (w->buffer))
a2889657 10368 {
43c09969 10369 struct buffer *b = XBUFFER (w->buffer);
2311178e 10370
43c09969
GM
10371 w->last_modified
10372 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10373 w->last_overlay_modified
10374 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10375 w->last_had_star
10376 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
a2889657 10377
43c09969 10378 if (accurate_p)
bd66d1ba 10379 {
43c09969
GM
10380 b->clip_changed = 0;
10381 b->prevent_redisplay_optimizations_p = 0;
10382
10383 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10384 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10385 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10386 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
2311178e 10387
43c09969
GM
10388 w->current_matrix->buffer = b;
10389 w->current_matrix->begv = BUF_BEGV (b);
10390 w->current_matrix->zv = BUF_ZV (b);
2311178e 10391
43c09969
GM
10392 w->last_cursor = w->cursor;
10393 w->last_cursor_off_p = w->cursor_off_p;
2311178e 10394
43c09969
GM
10395 if (w == XWINDOW (selected_window))
10396 w->last_point = make_number (BUF_PT (b));
10397 else
10398 w->last_point = make_number (XMARKER (w->pointm)->charpos);
bd66d1ba 10399 }
43c09969 10400 }
bd66d1ba 10401
43c09969
GM
10402 if (accurate_p)
10403 {
d2f84654 10404 w->window_end_valid = w->buffer;
99332eb6 10405#if 0 /* This is incorrect with variable-height lines. */
2913a9c0 10406 xassert (XINT (w->window_end_vpos)
da8b7f4f 10407 < (WINDOW_TOTAL_LINES (w)
2913a9c0 10408 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
99332eb6 10409#endif
a2889657 10410 w->update_mode_line = Qnil;
43c09969
GM
10411 }
10412}
10413
10414
10415/* Mark the display of windows in the window tree rooted at WINDOW as
10416 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10417 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10418 be redisplayed the next time redisplay_internal is called. */
10419
10420void
10421mark_window_display_accurate (window, accurate_p)
10422 Lisp_Object window;
10423 int accurate_p;
10424{
10425 struct window *w;
2311178e 10426
43c09969
GM
10427 for (; !NILP (window); window = w->next)
10428 {
10429 w = XWINDOW (window);
10430 mark_window_display_accurate_1 (w, accurate_p);
a2889657 10431
265a9e55 10432 if (!NILP (w->vchild))
5f5c8ee5 10433 mark_window_display_accurate (w->vchild, accurate_p);
265a9e55 10434 if (!NILP (w->hchild))
5f5c8ee5 10435 mark_window_display_accurate (w->hchild, accurate_p);
a2889657
JB
10436 }
10437
5f5c8ee5 10438 if (accurate_p)
a2889657 10439 {
351b5434 10440 update_overlay_arrows (1);
a2889657
JB
10441 }
10442 else
10443 {
5f5c8ee5
GM
10444 /* Force a thorough redisplay the next time by setting
10445 last_arrow_position and last_arrow_string to t, which is
4b41cebb 10446 unequal to any useful value of Voverlay_arrow_... */
351b5434 10447 update_overlay_arrows (-1);
a2889657
JB
10448 }
10449}
5f5c8ee5
GM
10450
10451
10452/* Return value in display table DP (Lisp_Char_Table *) for character
10453 C. Since a display table doesn't have any parent, we don't have to
10454 follow parent. Do not call this function directly but use the
10455 macro DISP_CHAR_VECTOR. */
10456
10457Lisp_Object
10458disp_char_vector (dp, c)
10459 struct Lisp_Char_Table *dp;
10460 int c;
10461{
10462 int code[4], i;
10463 Lisp_Object val;
10464
10465 if (SINGLE_BYTE_CHAR_P (c))
10466 return (dp->contents[c]);
2311178e 10467
c5924f47 10468 SPLIT_CHAR (c, code[0], code[1], code[2]);
260a86a0
KH
10469 if (code[1] < 32)
10470 code[1] = -1;
10471 else if (code[2] < 32)
10472 code[2] = -1;
2311178e 10473
5f5c8ee5
GM
10474 /* Here, the possible range of code[0] (== charset ID) is
10475 128..max_charset. Since the top level char table contains data
10476 for multibyte characters after 256th element, we must increment
10477 code[0] by 128 to get a correct index. */
10478 code[0] += 128;
10479 code[3] = -1; /* anchor */
10480
10481 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10482 {
10483 val = dp->contents[code[i]];
10484 if (!SUB_CHAR_TABLE_P (val))
10485 return (NILP (val) ? dp->defalt : val);
10486 }
2311178e 10487
5f5c8ee5
GM
10488 /* Here, val is a sub char table. We return the default value of
10489 it. */
10490 return (dp->defalt);
10491}
10492
10493
a2889657 10494\f
5f5c8ee5
GM
10495/***********************************************************************
10496 Window Redisplay
10497 ***********************************************************************/
a2725ab2 10498
5f5c8ee5 10499/* Redisplay all leaf windows in the window tree rooted at WINDOW. */
90adcf20
RS
10500
10501static void
5f5c8ee5
GM
10502redisplay_windows (window)
10503 Lisp_Object window;
90adcf20 10504{
5f5c8ee5
GM
10505 while (!NILP (window))
10506 {
10507 struct window *w = XWINDOW (window);
2311178e 10508
5f5c8ee5
GM
10509 if (!NILP (w->hchild))
10510 redisplay_windows (w->hchild);
10511 else if (!NILP (w->vchild))
10512 redisplay_windows (w->vchild);
10513 else
82a7ab23
RS
10514 {
10515 displayed_buffer = XBUFFER (w->buffer);
7033d6df
RS
10516 /* Use list_of_error, not Qerror, so that
10517 we catch only errors and don't run the debugger. */
2311178e 10518 internal_condition_case_1 (redisplay_window_0, window,
7033d6df 10519 list_of_error,
82a7ab23
RS
10520 redisplay_window_error);
10521 }
a2725ab2 10522
5f5c8ee5
GM
10523 window = w->next;
10524 }
10525}
10526
82a7ab23
RS
10527static Lisp_Object
10528redisplay_window_error ()
10529{
10530 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10531 return Qnil;
10532}
10533
10534static Lisp_Object
10535redisplay_window_0 (window)
10536 Lisp_Object window;
10537{
10538 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10539 redisplay_window (window, 0);
10540 return Qnil;
10541}
5f5c8ee5 10542
82a7ab23
RS
10543static Lisp_Object
10544redisplay_window_1 (window)
10545 Lisp_Object window;
10546{
10547 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10548 redisplay_window (window, 1);
10549 return Qnil;
10550}
10551\f
bc6371a6
KH
10552
10553/* Increment GLYPH until it reaches END or CONDITION fails while
10554 adding (GLYPH)->pixel_width to X. */
10555
10556#define SKIP_GLYPHS(glyph, end, x, condition) \
10557 do \
10558 { \
10559 (x) += (glyph)->pixel_width; \
10560 ++(glyph); \
10561 } \
10562 while ((glyph) < (end) && (condition))
10563
10564
5f5c8ee5
GM
10565/* Set cursor position of W. PT is assumed to be displayed in ROW.
10566 DELTA is the number of bytes by which positions recorded in ROW
10567 differ from current buffer positions. */
10568
10569void
10570set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10571 struct window *w;
10572 struct glyph_row *row;
10573 struct glyph_matrix *matrix;
10574 int delta, delta_bytes, dy, dvpos;
10575{
10576 struct glyph *glyph = row->glyphs[TEXT_AREA];
10577 struct glyph *end = glyph + row->used[TEXT_AREA];
ae73dc1c
KH
10578 /* The first glyph that starts a sequence of glyphs from string. */
10579 struct glyph *string_start;
10580 /* The X coordinate of string_start. */
10581 int string_start_x;
10582 /* The last known character position. */
10583 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10584 /* The last known character position before string_start. */
10585 int string_before_pos;
5f5c8ee5
GM
10586 int x = row->x;
10587 int pt_old = PT - delta;
10588
10589 /* Skip over glyphs not having an object at the start of the row.
10590 These are special glyphs like truncation marks on terminal
10591 frames. */
10592 if (row->displays_text_p)
10593 while (glyph < end
6fc556fd 10594 && INTEGERP (glyph->object)
5f5c8ee5
GM
10595 && glyph->charpos < 0)
10596 {
10597 x += glyph->pixel_width;
10598 ++glyph;
10599 }
10600
ae73dc1c 10601 string_start = NULL;
5f5c8ee5 10602 while (glyph < end
6fc556fd 10603 && !INTEGERP (glyph->object)
5f5c8ee5 10604 && (!BUFFERP (glyph->object)
ae73dc1c 10605 || (last_pos = glyph->charpos) < pt_old))
5f5c8ee5 10606 {
ae73dc1c
KH
10607 if (! STRINGP (glyph->object))
10608 {
10609 string_start = NULL;
10610 x += glyph->pixel_width;
10611 ++glyph;
10612 }
10613 else
10614 {
10615 string_before_pos = last_pos;
10616 string_start = glyph;
10617 string_start_x = x;
10618 /* Skip all glyphs from string. */
bc6371a6 10619 SKIP_GLYPHS (glyph, end, x, STRINGP (glyph->object));
ae73dc1c
KH
10620 }
10621 }
10622
10623 if (string_start
10624 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
10625 {
10626 /* We may have skipped over point because the previous glyphs
10627 are from string. As there's no easy way to know the
10628 character position of the current glyph, find the correct
10629 glyph on point by scanning from string_start again. */
bc6371a6
KH
10630 Lisp_Object limit;
10631 Lisp_Object string;
10632 int pos;
ae73dc1c 10633
bc6371a6
KH
10634 limit = make_number (pt_old + 1);
10635 end = glyph;
ae73dc1c
KH
10636 glyph = string_start;
10637 x = string_start_x;
bc6371a6
KH
10638 string = glyph->object;
10639 pos = string_buffer_position (w, string, string_before_pos);
10640 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
10641 because we always put cursor after overlay strings. */
10642 while (pos == 0 && glyph < end)
ae73dc1c 10643 {
bc6371a6
KH
10644 string = glyph->object;
10645 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10646 if (glyph < end)
10647 pos = string_buffer_position (w, glyph->object, string_before_pos);
10648 }
10649
10650 while (glyph < end)
10651 {
10652 pos = XINT (Fnext_single_char_property_change
10653 (make_number (pos), Qdisplay, Qnil, limit));
10654 if (pos > pt_old)
10655 break;
ae73dc1c 10656 /* Skip glyphs from the same string. */
bc6371a6
KH
10657 string = glyph->object;
10658 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10659 /* Skip glyphs from an overlay. */
10660 while (glyph < end
10661 && ! string_buffer_position (w, glyph->object, pos))
ae73dc1c 10662 {
bc6371a6
KH
10663 string = glyph->object;
10664 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
ae73dc1c 10665 }
ae73dc1c 10666 }
5f5c8ee5
GM
10667 }
10668
10669 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
10670 w->cursor.x = x;
10671 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
10672 w->cursor.y = row->y + dy;
10673
10674 if (w == XWINDOW (selected_window))
10675 {
10676 if (!row->continued_p
10677 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
10678 && row->x == 0)
10679 {
10680 this_line_buffer = XBUFFER (w->buffer);
2311178e 10681
5f5c8ee5
GM
10682 CHARPOS (this_line_start_pos)
10683 = MATRIX_ROW_START_CHARPOS (row) + delta;
10684 BYTEPOS (this_line_start_pos)
10685 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
2311178e 10686
5f5c8ee5
GM
10687 CHARPOS (this_line_end_pos)
10688 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
10689 BYTEPOS (this_line_end_pos)
10690 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
2311178e 10691
5f5c8ee5
GM
10692 this_line_y = w->cursor.y;
10693 this_line_pixel_height = row->height;
10694 this_line_vpos = w->cursor.vpos;
10695 this_line_start_x = row->x;
10696 }
10697 else
10698 CHARPOS (this_line_start_pos) = 0;
10699 }
10700}
10701
10702
10703/* Run window scroll functions, if any, for WINDOW with new window
756a3cb6
RS
10704 start STARTP. Sets the window start of WINDOW to that position.
10705
10706 We assume that the window's buffer is really current. */
5f5c8ee5
GM
10707
10708static INLINE struct text_pos
10709run_window_scroll_functions (window, startp)
10710 Lisp_Object window;
10711 struct text_pos startp;
10712{
10713 struct window *w = XWINDOW (window);
10714 SET_MARKER_FROM_TEXT_POS (w->start, startp);
756a3cb6
RS
10715
10716 if (current_buffer != XBUFFER (w->buffer))
10717 abort ();
10718
5f5c8ee5
GM
10719 if (!NILP (Vwindow_scroll_functions))
10720 {
2311178e 10721 run_hook_with_args_2 (Qwindow_scroll_functions, window,
5f5c8ee5
GM
10722 make_number (CHARPOS (startp)));
10723 SET_TEXT_POS_FROM_MARKER (startp, w->start);
756a3cb6
RS
10724 /* In case the hook functions switch buffers. */
10725 if (current_buffer != XBUFFER (w->buffer))
10726 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5 10727 }
90adcf20 10728
5f5c8ee5
GM
10729 return startp;
10730}
10731
10732
ddf6b9a3
RS
10733/* Make sure the line containing the cursor is fully visible.
10734 A value of 1 means there is nothing to be done.
10735 (Either the line is fully visible, or it cannot be made so,
10736 or we cannot tell.)
cb9d4a9f
KS
10737
10738 If FORCE_P is non-zero, return 0 even if partial visible cursor row
10739 is higher than window.
10740
ddf6b9a3
RS
10741 A value of 0 means the caller should do scrolling
10742 as if point had gone off the screen. */
5f5c8ee5 10743
cb617e7c 10744static int
cb9d4a9f 10745make_cursor_line_fully_visible (w, force_p)
5f5c8ee5 10746 struct window *w;
cb9d4a9f 10747 int force_p;
5f5c8ee5
GM
10748{
10749 struct glyph_matrix *matrix;
10750 struct glyph_row *row;
478d746b 10751 int window_height;
2311178e 10752
5f5c8ee5
GM
10753 /* It's not always possible to find the cursor, e.g, when a window
10754 is full of overlay strings. Don't do anything in that case. */
10755 if (w->cursor.vpos < 0)
cb617e7c 10756 return 1;
2311178e 10757
5f5c8ee5
GM
10758 matrix = w->desired_matrix;
10759 row = MATRIX_ROW (matrix, w->cursor.vpos);
10760
acda20e1 10761 /* If the cursor row is not partially visible, there's nothing to do. */
b28cb6ed 10762 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
cb617e7c 10763 return 1;
b28cb6ed 10764
cb9d4a9f
KS
10765 if (force_p)
10766 return 0;
10767
b28cb6ed
GM
10768 /* If the row the cursor is in is taller than the window's height,
10769 it's not clear what to do, so do nothing. */
10770 window_height = window_box_height (w);
10771 if (row->height >= window_height)
cb617e7c 10772 return 1;
b28cb6ed 10773
ddf6b9a3
RS
10774 return 0;
10775
10776#if 0
10777 /* This code used to try to scroll the window just enough to make
10778 the line visible. It returned 0 to say that the caller should
10779 allocate larger glyph matrices. */
10780
b28cb6ed 10781 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
5f5c8ee5
GM
10782 {
10783 int dy = row->height - row->visible_height;
10784 w->vscroll = 0;
10785 w->cursor.y += dy;
10786 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10787 }
b28cb6ed 10788 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
5f5c8ee5
GM
10789 {
10790 int dy = - (row->height - row->visible_height);
10791 w->vscroll = dy;
10792 w->cursor.y += dy;
10793 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10794 }
2311178e 10795
5f5c8ee5
GM
10796 /* When we change the cursor y-position of the selected window,
10797 change this_line_y as well so that the display optimization for
10798 the cursor line of the selected window in redisplay_internal uses
10799 the correct y-position. */
10800 if (w == XWINDOW (selected_window))
10801 this_line_y = w->cursor.y;
cb617e7c
GM
10802
10803 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
10804 redisplay with larger matrices. */
10805 if (matrix->nrows < required_matrix_height (w))
10806 {
10807 fonts_changed_p = 1;
10808 return 0;
10809 }
10810
10811 return 1;
ddf6b9a3 10812#endif /* 0 */
5f5c8ee5
GM
10813}
10814
10815
10816/* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
10817 non-zero means only WINDOW is redisplayed in redisplay_internal.
10818 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
10819 in redisplay_window to bring a partially visible line into view in
10820 the case that only the cursor has moved.
10821
03b0a4b4
RS
10822 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
10823 last screen line's vertical height extends past the end of the screen.
10824
5f5c8ee5
GM
10825 Value is
10826
10827 1 if scrolling succeeded
2311178e 10828
5f5c8ee5 10829 0 if scrolling didn't find point.
2311178e 10830
5f5c8ee5
GM
10831 -1 if new fonts have been loaded so that we must interrupt
10832 redisplay, adjust glyph matrices, and try again. */
10833
cb617e7c
GM
10834enum
10835{
10836 SCROLLING_SUCCESS,
10837 SCROLLING_FAILED,
10838 SCROLLING_NEED_LARGER_MATRICES
10839};
10840
5f5c8ee5
GM
10841static int
10842try_scrolling (window, just_this_one_p, scroll_conservatively,
03b0a4b4 10843 scroll_step, temp_scroll_step, last_line_misfit)
5f5c8ee5
GM
10844 Lisp_Object window;
10845 int just_this_one_p;
31ade731 10846 EMACS_INT scroll_conservatively, scroll_step;
5f5c8ee5 10847 int temp_scroll_step;
03b0a4b4 10848 int last_line_misfit;
5f5c8ee5
GM
10849{
10850 struct window *w = XWINDOW (window);
10851 struct frame *f = XFRAME (w->frame);
10852 struct text_pos scroll_margin_pos;
10853 struct text_pos pos;
10854 struct text_pos startp;
10855 struct it it;
10856 Lisp_Object window_end;
10857 int this_scroll_margin;
10858 int dy = 0;
10859 int scroll_max;
b8a63ccb 10860 int rc;
5f5c8ee5
GM
10861 int amount_to_scroll = 0;
10862 Lisp_Object aggressive;
10863 int height;
cb9d4a9f 10864 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
5f5c8ee5
GM
10865
10866#if GLYPH_DEBUG
10867 debug_method_add (w, "try_scrolling");
78614721 10868#endif
5f5c8ee5
GM
10869
10870 SET_TEXT_POS_FROM_MARKER (startp, w->start);
2311178e 10871
5f5c8ee5
GM
10872 /* Compute scroll margin height in pixels. We scroll when point is
10873 within this distance from the top or bottom of the window. */
10874 if (scroll_margin > 0)
90adcf20 10875 {
da8b7f4f
KS
10876 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
10877 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
5f5c8ee5
GM
10878 }
10879 else
10880 this_scroll_margin = 0;
10881
10882 /* Compute how much we should try to scroll maximally to bring point
10883 into view. */
0894e696
SM
10884 if (scroll_step || scroll_conservatively || temp_scroll_step)
10885 scroll_max = max (scroll_step,
10886 max (scroll_conservatively, temp_scroll_step));
5f5c8ee5
GM
10887 else if (NUMBERP (current_buffer->scroll_down_aggressively)
10888 || NUMBERP (current_buffer->scroll_up_aggressively))
10889 /* We're trying to scroll because of aggressive scrolling
10890 but no scroll_step is set. Choose an arbitrary one. Maybe
10891 there should be a variable for this. */
10892 scroll_max = 10;
10893 else
10894 scroll_max = 0;
da8b7f4f 10895 scroll_max *= FRAME_LINE_HEIGHT (f);
5f5c8ee5
GM
10896
10897 /* Decide whether we have to scroll down. Start at the window end
10898 and move this_scroll_margin up to find the position of the scroll
10899 margin. */
10900 window_end = Fwindow_end (window, Qt);
03b0a4b4
RS
10901
10902 too_near_end:
10903
5f5c8ee5
GM
10904 CHARPOS (scroll_margin_pos) = XINT (window_end);
10905 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
03b0a4b4 10906
cb9d4a9f 10907 if (this_scroll_margin || extra_scroll_margin_lines)
5f5c8ee5
GM
10908 {
10909 start_display (&it, w, scroll_margin_pos);
cb9d4a9f
KS
10910 if (this_scroll_margin)
10911 move_it_vertically (&it, - this_scroll_margin);
10912 if (extra_scroll_margin_lines)
10913 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
5f5c8ee5
GM
10914 scroll_margin_pos = it.current.pos;
10915 }
10916
10917 if (PT >= CHARPOS (scroll_margin_pos))
10918 {
10919 int y0;
2311178e 10920
5f5c8ee5
GM
10921 /* Point is in the scroll margin at the bottom of the window, or
10922 below. Compute a new window start that makes point visible. */
47589c8c 10923
5f5c8ee5
GM
10924 /* Compute the distance from the scroll margin to PT.
10925 Give up if the distance is greater than scroll_max. */
10926 start_display (&it, w, scroll_margin_pos);
10927 y0 = it.current_y;
10928 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10929 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
539e92ad
GM
10930
10931 /* To make point visible, we have to move the window start
10932 down so that the line the cursor is in is visible, which
10933 means we have to add in the height of the cursor line. */
10934 dy = line_bottom_y (&it) - y0;
2311178e 10935
5f5c8ee5 10936 if (dy > scroll_max)
cb617e7c 10937 return SCROLLING_FAILED;
2311178e 10938
5f5c8ee5
GM
10939 /* Move the window start down. If scrolling conservatively,
10940 move it just enough down to make point visible. If
10941 scroll_step is set, move it down by scroll_step. */
10942 start_display (&it, w, startp);
10943
10944 if (scroll_conservatively)
03b0a4b4
RS
10945 /* Set AMOUNT_TO_SCROLL to at least one line,
10946 and at most scroll_conservatively lines. */
e89aaabd 10947 amount_to_scroll
da8b7f4f
KS
10948 = min (max (dy, FRAME_LINE_HEIGHT (f)),
10949 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
5f5c8ee5
GM
10950 else if (scroll_step || temp_scroll_step)
10951 amount_to_scroll = scroll_max;
10952 else
90adcf20 10953 {
46226c1d 10954 aggressive = current_buffer->scroll_up_aggressively;
da8b7f4f 10955 height = WINDOW_BOX_TEXT_HEIGHT (w);
5f5c8ee5
GM
10956 if (NUMBERP (aggressive))
10957 amount_to_scroll = XFLOATINT (aggressive) * height;
10958 }
a2725ab2 10959
5f5c8ee5 10960 if (amount_to_scroll <= 0)
cb617e7c 10961 return SCROLLING_FAILED;
a2725ab2 10962
03b0a4b4
RS
10963 /* If moving by amount_to_scroll leaves STARTP unchanged,
10964 move it down one screen line. */
10965
5f5c8ee5 10966 move_it_vertically (&it, amount_to_scroll);
03b0a4b4
RS
10967 if (CHARPOS (it.current.pos) == CHARPOS (startp))
10968 move_it_by_lines (&it, 1, 1);
5f5c8ee5
GM
10969 startp = it.current.pos;
10970 }
10971 else
10972 {
10973 /* See if point is inside the scroll margin at the top of the
10974 window. */
10975 scroll_margin_pos = startp;
10976 if (this_scroll_margin)
10977 {
10978 start_display (&it, w, startp);
10979 move_it_vertically (&it, this_scroll_margin);
10980 scroll_margin_pos = it.current.pos;
10981 }
10982
10983 if (PT < CHARPOS (scroll_margin_pos))
10984 {
10985 /* Point is in the scroll margin at the top of the window or
10986 above what is displayed in the window. */
10987 int y0;
2311178e 10988
5f5c8ee5
GM
10989 /* Compute the vertical distance from PT to the scroll
10990 margin position. Give up if distance is greater than
10991 scroll_max. */
10992 SET_TEXT_POS (pos, PT, PT_BYTE);
10993 start_display (&it, w, pos);
10994 y0 = it.current_y;
10995 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
10996 it.last_visible_y, -1,
10997 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10998 dy = it.current_y - y0;
10999 if (dy > scroll_max)
cb617e7c 11000 return SCROLLING_FAILED;
2311178e 11001
5f5c8ee5
GM
11002 /* Compute new window start. */
11003 start_display (&it, w, startp);
2311178e 11004
5f5c8ee5 11005 if (scroll_conservatively)
0894e696 11006 amount_to_scroll =
da8b7f4f 11007 max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
5f5c8ee5
GM
11008 else if (scroll_step || temp_scroll_step)
11009 amount_to_scroll = scroll_max;
538f13d4 11010 else
5f5c8ee5 11011 {
46226c1d 11012 aggressive = current_buffer->scroll_down_aggressively;
da8b7f4f 11013 height = WINDOW_BOX_TEXT_HEIGHT (w);
5f5c8ee5
GM
11014 if (NUMBERP (aggressive))
11015 amount_to_scroll = XFLOATINT (aggressive) * height;
11016 }
a2725ab2 11017
5f5c8ee5 11018 if (amount_to_scroll <= 0)
cb617e7c 11019 return SCROLLING_FAILED;
2311178e 11020
5f5c8ee5
GM
11021 move_it_vertically (&it, - amount_to_scroll);
11022 startp = it.current.pos;
90adcf20
RS
11023 }
11024 }
a2889657 11025
5f5c8ee5
GM
11026 /* Run window scroll functions. */
11027 startp = run_window_scroll_functions (window, startp);
90adcf20 11028
5f5c8ee5
GM
11029 /* Display the window. Give up if new fonts are loaded, or if point
11030 doesn't appear. */
11031 if (!try_window (window, startp))
cb617e7c 11032 rc = SCROLLING_NEED_LARGER_MATRICES;
5f5c8ee5
GM
11033 else if (w->cursor.vpos < 0)
11034 {
11035 clear_glyph_matrix (w->desired_matrix);
cb617e7c 11036 rc = SCROLLING_FAILED;
5f5c8ee5
GM
11037 }
11038 else
11039 {
11040 /* Maybe forget recorded base line for line number display. */
2311178e 11041 if (!just_this_one_p
5f5c8ee5 11042 || current_buffer->clip_changed
9142dd5b 11043 || BEG_UNCHANGED < CHARPOS (startp))
5f5c8ee5 11044 w->base_line_number = Qnil;
2311178e 11045
ddf6b9a3
RS
11046 /* If cursor ends up on a partially visible line,
11047 treat that as being off the bottom of the screen. */
cb9d4a9f 11048 if (! make_cursor_line_fully_visible (w, extra_scroll_margin_lines <= 1))
ef3c2c73
RS
11049 {
11050 clear_glyph_matrix (w->desired_matrix);
cb9d4a9f 11051 ++extra_scroll_margin_lines;
ef3c2c73
RS
11052 goto too_near_end;
11053 }
ddf6b9a3 11054 rc = SCROLLING_SUCCESS;
5f5c8ee5
GM
11055 }
11056
11057 return rc;
a2889657
JB
11058}
11059
5f5c8ee5
GM
11060
11061/* Compute a suitable window start for window W if display of W starts
11062 on a continuation line. Value is non-zero if a new window start
11063 was computed.
11064
11065 The new window start will be computed, based on W's width, starting
11066 from the start of the continued line. It is the start of the
11067 screen line with the minimum distance from the old start W->start. */
11068
11069static int
11070compute_window_start_on_continuation_line (w)
11071 struct window *w;
1f1ff51d 11072{
5f5c8ee5
GM
11073 struct text_pos pos, start_pos;
11074 int window_start_changed_p = 0;
1f1ff51d 11075
5f5c8ee5 11076 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
1f1ff51d 11077
5f5c8ee5 11078 /* If window start is on a continuation line... Window start may be
2311178e 11079 < BEGV in case there's invisible text at the start of the
5f5c8ee5
GM
11080 buffer (M-x rmail, for example). */
11081 if (CHARPOS (start_pos) > BEGV
11082 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
1f1ff51d 11083 {
5f5c8ee5
GM
11084 struct it it;
11085 struct glyph_row *row;
f3751a65
GM
11086
11087 /* Handle the case that the window start is out of range. */
11088 if (CHARPOS (start_pos) < BEGV)
11089 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11090 else if (CHARPOS (start_pos) > ZV)
11091 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
2311178e 11092
5f5c8ee5
GM
11093 /* Find the start of the continued line. This should be fast
11094 because scan_buffer is fast (newline cache). */
045dee35 11095 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
5f5c8ee5
GM
11096 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11097 row, DEFAULT_FACE_ID);
11098 reseat_at_previous_visible_line_start (&it);
11099
11100 /* If the line start is "too far" away from the window start,
11101 say it takes too much time to compute a new window start. */
11102 if (CHARPOS (start_pos) - IT_CHARPOS (it)
da8b7f4f 11103 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
5f5c8ee5
GM
11104 {
11105 int min_distance, distance;
2311178e 11106
5f5c8ee5
GM
11107 /* Move forward by display lines to find the new window
11108 start. If window width was enlarged, the new start can
11109 be expected to be > the old start. If window width was
11110 decreased, the new window start will be < the old start.
11111 So, we're looking for the display line start with the
11112 minimum distance from the old window start. */
11113 pos = it.current.pos;
11114 min_distance = INFINITY;
11115 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11116 distance < min_distance)
11117 {
11118 min_distance = distance;
11119 pos = it.current.pos;
11120 move_it_by_lines (&it, 1, 0);
11121 }
2311178e 11122
5f5c8ee5
GM
11123 /* Set the window start there. */
11124 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11125 window_start_changed_p = 1;
11126 }
1f1ff51d 11127 }
2311178e 11128
5f5c8ee5 11129 return window_start_changed_p;
1f1ff51d
KH
11130}
11131
5f5c8ee5 11132
1dd5768b 11133/* Try cursor movement in case text has not changed in window WINDOW,
47589c8c
GM
11134 with window start STARTP. Value is
11135
cb617e7c 11136 CURSOR_MOVEMENT_SUCCESS if successful
2311178e 11137
cb617e7c
GM
11138 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11139
11140 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11141 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11142 we want to scroll as if scroll-step were set to 1. See the code.
11143
11144 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11145 which case we have to abort this redisplay, and adjust matrices
11146 first. */
11147
2311178e 11148enum
cb617e7c
GM
11149{
11150 CURSOR_MOVEMENT_SUCCESS,
11151 CURSOR_MOVEMENT_CANNOT_BE_USED,
11152 CURSOR_MOVEMENT_MUST_SCROLL,
11153 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11154};
47589c8c
GM
11155
11156static int
11157try_cursor_movement (window, startp, scroll_step)
11158 Lisp_Object window;
11159 struct text_pos startp;
11160 int *scroll_step;
11161{
11162 struct window *w = XWINDOW (window);
11163 struct frame *f = XFRAME (w->frame);
cb617e7c 11164 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
2311178e 11165
69d1f7c9 11166#if GLYPH_DEBUG
76cb5e06
GM
11167 if (inhibit_try_cursor_movement)
11168 return rc;
11169#endif
11170
47589c8c
GM
11171 /* Handle case where text has not changed, only point, and it has
11172 not moved off the frame. */
11173 if (/* Point may be in this window. */
11174 PT >= CHARPOS (startp)
47589c8c
GM
11175 /* Selective display hasn't changed. */
11176 && !current_buffer->clip_changed
4db87380
GM
11177 /* Function force-mode-line-update is used to force a thorough
11178 redisplay. It sets either windows_or_buffers_changed or
11179 update_mode_lines. So don't take a shortcut here for these
11180 cases. */
11181 && !update_mode_lines
11182 && !windows_or_buffers_changed
5fb96e96 11183 && !cursor_type_changed
2311178e 11184 /* Can't use this case if highlighting a region. When a
47589c8c
GM
11185 region exists, cursor movement has to do more than just
11186 set the cursor. */
11187 && !(!NILP (Vtransient_mark_mode)
11188 && !NILP (current_buffer->mark_active))
11189 && NILP (w->region_showing)
11190 && NILP (Vshow_trailing_whitespace)
11191 /* Right after splitting windows, last_point may be nil. */
11192 && INTEGERP (w->last_point)
11193 /* This code is not used for mini-buffer for the sake of the case
11194 of redisplaying to replace an echo area message; since in
11195 that case the mini-buffer contents per se are usually
11196 unchanged. This code is of no real use in the mini-buffer
11197 since the handling of this_line_start_pos, etc., in redisplay
11198 handles the same cases. */
11199 && !EQ (window, minibuf_window)
11200 /* When splitting windows or for new windows, it happens that
11201 redisplay is called with a nil window_end_vpos or one being
11202 larger than the window. This should really be fixed in
11203 window.c. I don't have this on my list, now, so we do
11204 approximately the same as the old redisplay code. --gerd. */
11205 && INTEGERP (w->window_end_vpos)
11206 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11207 && (FRAME_WINDOW_P (f)
351b5434 11208 || !overlay_arrow_in_current_buffer_p ()))
47589c8c
GM
11209 {
11210 int this_scroll_margin;
8ee5b6a3 11211 struct glyph_row *row = NULL;
47589c8c
GM
11212
11213#if GLYPH_DEBUG
11214 debug_method_add (w, "cursor movement");
11215#endif
11216
11217 /* Scroll if point within this distance from the top or bottom
11218 of the window. This is a pixel value. */
11219 this_scroll_margin = max (0, scroll_margin);
da8b7f4f
KS
11220 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11221 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
47589c8c
GM
11222
11223 /* Start with the row the cursor was displayed during the last
11224 not paused redisplay. Give up if that row is not valid. */
bd9d0f3f
GM
11225 if (w->last_cursor.vpos < 0
11226 || w->last_cursor.vpos >= w->current_matrix->nrows)
cb617e7c 11227 rc = CURSOR_MOVEMENT_MUST_SCROLL;
47589c8c
GM
11228 else
11229 {
11230 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11231 if (row->mode_line_p)
11232 ++row;
11233 if (!row->enabled_p)
cb617e7c 11234 rc = CURSOR_MOVEMENT_MUST_SCROLL;
47589c8c
GM
11235 }
11236
cb617e7c 11237 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
47589c8c
GM
11238 {
11239 int scroll_p = 0;
68c5d1db 11240 int last_y = window_text_bottom_y (w) - this_scroll_margin;
2311178e 11241
47589c8c
GM
11242 if (PT > XFASTINT (w->last_point))
11243 {
11244 /* Point has moved forward. */
47589c8c
GM
11245 while (MATRIX_ROW_END_CHARPOS (row) < PT
11246 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11247 {
11248 xassert (row->enabled_p);
11249 ++row;
11250 }
11251
11252 /* The end position of a row equals the start position
11253 of the next row. If PT is there, we would rather
cafafe0b
GM
11254 display it in the next line. */
11255 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11256 && MATRIX_ROW_END_CHARPOS (row) == PT
11257 && !cursor_row_p (w, row))
11258 ++row;
47589c8c
GM
11259
11260 /* If within the scroll margin, scroll. Note that
11261 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11262 the next line would be drawn, and that
11263 this_scroll_margin can be zero. */
11264 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11265 || PT > MATRIX_ROW_END_CHARPOS (row)
11266 /* Line is completely visible last line in window
11267 and PT is to be set in the next line. */
11268 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11269 && PT == MATRIX_ROW_END_CHARPOS (row)
11270 && !row->ends_at_zv_p
11271 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11272 scroll_p = 1;
11273 }
11274 else if (PT < XFASTINT (w->last_point))
11275 {
11276 /* Cursor has to be moved backward. Note that PT >=
11277 CHARPOS (startp) because of the outer
11278 if-statement. */
11279 while (!row->mode_line_p
11280 && (MATRIX_ROW_START_CHARPOS (row) > PT
11281 || (MATRIX_ROW_START_CHARPOS (row) == PT
11282 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11283 && (row->y > this_scroll_margin
11284 || CHARPOS (startp) == BEGV))
11285 {
11286 xassert (row->enabled_p);
11287 --row;
11288 }
11289
11290 /* Consider the following case: Window starts at BEGV,
11291 there is invisible, intangible text at BEGV, so that
11292 display starts at some point START > BEGV. It can
11293 happen that we are called with PT somewhere between
11294 BEGV and START. Try to handle that case. */
11295 if (row < w->current_matrix->rows
11296 || row->mode_line_p)
11297 {
11298 row = w->current_matrix->rows;
11299 if (row->mode_line_p)
11300 ++row;
11301 }
11302
11303 /* Due to newlines in overlay strings, we may have to
11304 skip forward over overlay strings. */
68c5d1db
GM
11305 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11306 && MATRIX_ROW_END_CHARPOS (row) == PT
11307 && !cursor_row_p (w, row))
47589c8c 11308 ++row;
2311178e 11309
47589c8c
GM
11310 /* If within the scroll margin, scroll. */
11311 if (row->y < this_scroll_margin
11312 && CHARPOS (startp) != BEGV)
11313 scroll_p = 1;
11314 }
11315
11316 if (PT < MATRIX_ROW_START_CHARPOS (row)
11317 || PT > MATRIX_ROW_END_CHARPOS (row))
11318 {
11319 /* if PT is not in the glyph row, give up. */
cb617e7c 11320 rc = CURSOR_MOVEMENT_MUST_SCROLL;
47589c8c 11321 }
440fc135 11322 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
47589c8c 11323 {
8de4aaf8
GM
11324 if (PT == MATRIX_ROW_END_CHARPOS (row)
11325 && !row->ends_at_zv_p
11326 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
cb617e7c 11327 rc = CURSOR_MOVEMENT_MUST_SCROLL;
3f7e3031 11328 else if (row->height > window_box_height (w))
440fc135 11329 {
8de4aaf8
GM
11330 /* If we end up in a partially visible line, let's
11331 make it fully visible, except when it's taller
11332 than the window, in which case we can't do much
11333 about it. */
440fc135 11334 *scroll_step = 1;
cb617e7c 11335 rc = CURSOR_MOVEMENT_MUST_SCROLL;
440fc135
GM
11336 }
11337 else
11338 {
11339 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
cb9d4a9f 11340 if (!make_cursor_line_fully_visible (w, 0))
ddf6b9a3 11341 rc = CURSOR_MOVEMENT_MUST_SCROLL;
cb617e7c
GM
11342 else
11343 rc = CURSOR_MOVEMENT_SUCCESS;
440fc135 11344 }
47589c8c
GM
11345 }
11346 else if (scroll_p)
cb617e7c 11347 rc = CURSOR_MOVEMENT_MUST_SCROLL;
47589c8c
GM
11348 else
11349 {
11350 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
cb617e7c 11351 rc = CURSOR_MOVEMENT_SUCCESS;
47589c8c
GM
11352 }
11353 }
11354 }
11355
11356 return rc;
11357}
11358
9a08d928
SM
11359void
11360set_vertical_scroll_bar (w)
11361 struct window *w;
11362{
11363 int start, end, whole;
11364
11365 /* Calculate the start and end positions for the current window.
11366 At some point, it would be nice to choose between scrollbars
11367 which reflect the whole buffer size, with special markers
11368 indicating narrowing, and scrollbars which reflect only the
11369 visible region.
be21b925 11370
9a08d928
SM
11371 Note that mini-buffers sometimes aren't displaying any text. */
11372 if (!MINI_WINDOW_P (w)
11373 || (w == XWINDOW (minibuf_window)
11374 && NILP (echo_area_buffer[0])))
11375 {
11376 struct buffer *buf = XBUFFER (w->buffer);
11377 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11378 start = marker_position (w->start) - BUF_BEGV (buf);
11379 /* I don't think this is guaranteed to be right. For the
11380 moment, we'll pretend it is. */
11381 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
be21b925 11382
9a08d928
SM
11383 if (end < start)
11384 end = start;
11385 if (whole < (end - start))
11386 whole = end - start;
11387 }
11388 else
11389 start = end = whole = 0;
11390
11391 /* Indicate what this scroll bar ought to be displaying now. */
11392 set_vertical_scroll_bar_hook (w, end - start, whole, start);
11393}
47589c8c 11394
88e6b646 11395
5f5c8ee5 11396/* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
b60c9653
RS
11397 selected_window is redisplayed.
11398
11399 We can return without actually redisplaying the window if
11400 fonts_changed_p is nonzero. In that case, redisplay_internal will
11401 retry. */
90adcf20 11402
a2889657 11403static void
5f5c8ee5 11404redisplay_window (window, just_this_one_p)
a2889657 11405 Lisp_Object window;
5f5c8ee5 11406 int just_this_one_p;
a2889657 11407{
5f5c8ee5
GM
11408 struct window *w = XWINDOW (window);
11409 struct frame *f = XFRAME (w->frame);
11410 struct buffer *buffer = XBUFFER (w->buffer);
a2889657 11411 struct buffer *old = current_buffer;
5f5c8ee5 11412 struct text_pos lpoint, opoint, startp;
e481f960 11413 int update_mode_line;
5f5c8ee5
GM
11414 int tem;
11415 struct it it;
11416 /* Record it now because it's overwritten. */
11417 int current_matrix_up_to_date_p = 0;
88e6b646 11418 int used_current_matrix_p = 0;
c62c1bb5
RS
11419 /* This is less strict than current_matrix_up_to_date_p.
11420 It indictes that the buffer contents and narrowing are unchanged. */
11421 int buffer_unchanged_p = 0;
5f5c8ee5 11422 int temp_scroll_step = 0;
331379bf 11423 int count = SPECPDL_INDEX ();
47589c8c 11424 int rc;
ddf6b9a3 11425 int centering_position;
03b0a4b4 11426 int last_line_misfit = 0;
a2889657 11427
5f5c8ee5
GM
11428 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11429 opoint = lpoint;
a2889657 11430
5f5c8ee5
GM
11431 /* W must be a leaf window here. */
11432 xassert (!NILP (w->buffer));
11433#if GLYPH_DEBUG
11434 *w->desired_matrix->method = 0;
11435#endif
2e54982e
RS
11436
11437 specbind (Qinhibit_point_motion_hooks, Qt);
9142dd5b
GM
11438
11439 reconsider_clip_changes (w, buffer);
2311178e
TTN
11440
11441 /* Has the mode line to be updated? */
5f5c8ee5
GM
11442 update_mode_line = (!NILP (w->update_mode_line)
11443 || update_mode_lines
c62c1bb5
RS
11444 || buffer->clip_changed
11445 || buffer->prevent_redisplay_optimizations_p);
8de2d90b
JB
11446
11447 if (MINI_WINDOW_P (w))
11448 {
5f5c8ee5 11449 if (w == XWINDOW (echo_area_window)
c6e89d6c 11450 && !NILP (echo_area_buffer[0]))
5f5c8ee5
GM
11451 {
11452 if (update_mode_line)
11453 /* We may have to update a tty frame's menu bar or a
e037b9ec 11454 tool-bar. Example `M-x C-h C-h C-g'. */
5f5c8ee5
GM
11455 goto finish_menu_bars;
11456 else
11457 /* We've already displayed the echo area glyphs in this window. */
11458 goto finish_scroll_bars;
11459 }
b2b5455d
RS
11460 else if ((w != XWINDOW (minibuf_window)
11461 || minibuf_level == 0)
c0bcce6f
JPW
11462 /* When buffer is nonempty, redisplay window normally. */
11463 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
3354fdcf
RS
11464 /* Quail displays non-mini buffers in minibuffer window.
11465 In that case, redisplay the window normally. */
b2b5455d 11466 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
8de2d90b 11467 {
3354fdcf
RS
11468 /* W is a mini-buffer window, but it's not active, so clear
11469 it. */
5f5c8ee5
GM
11470 int yb = window_text_bottom_y (w);
11471 struct glyph_row *row;
11472 int y;
11473
11474 for (y = 0, row = w->desired_matrix->rows;
11475 y < yb;
11476 y += row->height, ++row)
11477 blank_row (w, row, y);
88f22aff 11478 goto finish_scroll_bars;
8de2d90b 11479 }
c095a1dd
GM
11480
11481 clear_glyph_matrix (w->desired_matrix);
8de2d90b 11482 }
a2889657 11483
5f5c8ee5
GM
11484 /* Otherwise set up data on this window; select its buffer and point
11485 value. */
6a93695f
GM
11486 /* Really select the buffer, for the sake of buffer-local
11487 variables. */
11488 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5
GM
11489 SET_TEXT_POS (opoint, PT, PT_BYTE);
11490
11491 current_matrix_up_to_date_p
11492 = (!NILP (w->window_end_valid)
11493 && !current_buffer->clip_changed
c62c1bb5 11494 && !current_buffer->prevent_redisplay_optimizations_p
5f5c8ee5
GM
11495 && XFASTINT (w->last_modified) >= MODIFF
11496 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
e481f960 11497
c62c1bb5
RS
11498 buffer_unchanged_p
11499 = (!NILP (w->window_end_valid)
11500 && !current_buffer->clip_changed
3f1258d0 11501 && XFASTINT (w->last_modified) >= MODIFF
c62c1bb5
RS
11502 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11503
5f5c8ee5
GM
11504 /* When windows_or_buffers_changed is non-zero, we can't rely on
11505 the window end being valid, so set it to nil there. */
11506 if (windows_or_buffers_changed)
11507 {
11508 /* If window starts on a continuation line, maybe adjust the
11509 window start in case the window's width changed. */
11510 if (XMARKER (w->start)->buffer == current_buffer)
11511 compute_window_start_on_continuation_line (w);
2311178e 11512
5f5c8ee5
GM
11513 w->window_end_valid = Qnil;
11514 }
12adba34 11515
5f5c8ee5
GM
11516 /* Some sanity checks. */
11517 CHECK_WINDOW_END (w);
11518 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12adba34 11519 abort ();
5f5c8ee5 11520 if (BYTEPOS (opoint) < CHARPOS (opoint))
12adba34 11521 abort ();
a2889657 11522
28995e67
RS
11523 /* If %c is in mode line, update it if needed. */
11524 if (!NILP (w->column_number_displayed)
11525 /* This alternative quickly identifies a common case
11526 where no change is needed. */
11527 && !(PT == XFASTINT (w->last_point)
8850a573
RS
11528 && XFASTINT (w->last_modified) >= MODIFF
11529 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
2311178e
TTN
11530 && (XFASTINT (w->column_number_displayed)
11531 != (int) current_column ())) /* iftc */
11532 update_mode_line = 1;
28995e67 11533
5f5c8ee5
GM
11534 /* Count number of windows showing the selected buffer. An indirect
11535 buffer counts as its base buffer. */
11536 if (!just_this_one_p)
42640f83
RS
11537 {
11538 struct buffer *current_base, *window_base;
11539 current_base = current_buffer;
11540 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11541 if (current_base->base_buffer)
11542 current_base = current_base->base_buffer;
11543 if (window_base->base_buffer)
11544 window_base = window_base->base_buffer;
11545 if (current_base == window_base)
11546 buffer_shared++;
11547 }
a2889657 11548
5f5c8ee5
GM
11549 /* Point refers normally to the selected window. For any other
11550 window, set up appropriate value. */
a2889657
JB
11551 if (!EQ (window, selected_window))
11552 {
12adba34
RS
11553 int new_pt = XMARKER (w->pointm)->charpos;
11554 int new_pt_byte = marker_byte_position (w->pointm);
f67a0f51 11555 if (new_pt < BEGV)
a2889657 11556 {
f67a0f51 11557 new_pt = BEGV;
12adba34
RS
11558 new_pt_byte = BEGV_BYTE;
11559 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
a2889657 11560 }
f67a0f51 11561 else if (new_pt > (ZV - 1))
a2889657 11562 {
f67a0f51 11563 new_pt = ZV;
12adba34
RS
11564 new_pt_byte = ZV_BYTE;
11565 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
a2889657 11566 }
2311178e 11567
f67a0f51 11568 /* We don't use SET_PT so that the point-motion hooks don't run. */
12adba34 11569 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
a2889657
JB
11570 }
11571
f4faa47c 11572 /* If any of the character widths specified in the display table
5f5c8ee5
GM
11573 have changed, invalidate the width run cache. It's true that
11574 this may be a bit late to catch such changes, but the rest of
f4faa47c
JB
11575 redisplay goes (non-fatally) haywire when the display table is
11576 changed, so why should we worry about doing any better? */
11577 if (current_buffer->width_run_cache)
11578 {
f908610f 11579 struct Lisp_Char_Table *disptab = buffer_display_table ();
f4faa47c
JB
11580
11581 if (! disptab_matches_widthtab (disptab,
11582 XVECTOR (current_buffer->width_table)))
11583 {
11584 invalidate_region_cache (current_buffer,
11585 current_buffer->width_run_cache,
11586 BEG, Z);
11587 recompute_width_table (current_buffer, disptab);
11588 }
11589 }
11590
a2889657 11591 /* If window-start is screwed up, choose a new one. */
a2889657
JB
11592 if (XMARKER (w->start)->buffer != current_buffer)
11593 goto recenter;
11594
5f5c8ee5 11595 SET_TEXT_POS_FROM_MARKER (startp, w->start);
a2889657 11596
cf0df6ab
RS
11597 /* If someone specified a new starting point but did not insist,
11598 check whether it can be used. */
cfad01b4
GM
11599 if (!NILP (w->optional_new_start)
11600 && CHARPOS (startp) >= BEGV
11601 && CHARPOS (startp) <= ZV)
cf0df6ab
RS
11602 {
11603 w->optional_new_start = Qnil;
5f5c8ee5
GM
11604 start_display (&it, w, startp);
11605 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11606 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11607 if (IT_CHARPOS (it) == PT)
cf0df6ab 11608 w->force_start = Qt;
29b3ea63
KS
11609 /* IT may overshoot PT if text at PT is invisible. */
11610 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
11611 w->force_start = Qt;
11612
11613
cf0df6ab
RS
11614 }
11615
8de2d90b 11616 /* Handle case where place to start displaying has been specified,
aa6d10fa 11617 unless the specified location is outside the accessible range. */
9472f927
GM
11618 if (!NILP (w->force_start)
11619 || w->frozen_window_start_p)
a2889657 11620 {
b60c9653
RS
11621 /* We set this later on if we have to adjust point. */
11622 int new_vpos = -1;
11623
e63574d7 11624 w->force_start = Qnil;
5f5c8ee5 11625 w->vscroll = 0;
b5174a51 11626 w->window_end_valid = Qnil;
5f5c8ee5
GM
11627
11628 /* Forget any recorded base line for line number display. */
c62c1bb5 11629 if (!buffer_unchanged_p)
5f5c8ee5
GM
11630 w->base_line_number = Qnil;
11631
75c43375
RS
11632 /* Redisplay the mode line. Select the buffer properly for that.
11633 Also, run the hook window-scroll-functions
11634 because we have scrolled. */
e63574d7
RS
11635 /* Note, we do this after clearing force_start because
11636 if there's an error, it is better to forget about force_start
11637 than to get into an infinite loop calling the hook functions
11638 and having them get more errors. */
75c43375
RS
11639 if (!update_mode_line
11640 || ! NILP (Vwindow_scroll_functions))
e481f960 11641 {
e481f960
RS
11642 update_mode_line = 1;
11643 w->update_mode_line = Qt;
5f5c8ee5 11644 startp = run_window_scroll_functions (window, startp);
e481f960 11645 }
2311178e 11646
ac90c44f
GM
11647 w->last_modified = make_number (0);
11648 w->last_overlay_modified = make_number (0);
5f5c8ee5
GM
11649 if (CHARPOS (startp) < BEGV)
11650 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
11651 else if (CHARPOS (startp) > ZV)
11652 SET_TEXT_POS (startp, ZV, ZV_BYTE);
2311178e
TTN
11653
11654 /* Redisplay, then check if cursor has been set during the
5f5c8ee5
GM
11655 redisplay. Give up if new fonts were loaded. */
11656 if (!try_window (window, startp))
11657 {
11658 w->force_start = Qt;
11659 clear_glyph_matrix (w->desired_matrix);
b60c9653 11660 goto need_larger_matrices;
5f5c8ee5
GM
11661 }
11662
9472f927 11663 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
5f5c8ee5 11664 {
b28cb6ed
GM
11665 /* If point does not appear, try to move point so it does
11666 appear. The desired matrix has been built above, so we
11667 can use it here. */
b60c9653
RS
11668 new_vpos = window_box_height (w) / 2;
11669 }
11670
cb9d4a9f 11671 if (!make_cursor_line_fully_visible (w, 0))
b60c9653
RS
11672 {
11673 /* Point does appear, but on a line partly visible at end of window.
11674 Move it back to a fully-visible line. */
11675 new_vpos = window_box_height (w);
11676 }
11677
11678 /* If we need to move point for either of the above reasons,
11679 now actually do it. */
11680 if (new_vpos >= 0)
11681 {
b28cb6ed
GM
11682 struct glyph_row *row;
11683
b28cb6ed 11684 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
b60c9653 11685 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
5f5c8ee5
GM
11686 ++row;
11687
11688 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
11689 MATRIX_ROW_START_BYTEPOS (row));
11690
90adcf20 11691 if (w != XWINDOW (selected_window))
12adba34 11692 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5f5c8ee5
GM
11693 else if (current_buffer == old)
11694 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11695
11696 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
2311178e 11697
5f5c8ee5
GM
11698 /* If we are highlighting the region, then we just changed
11699 the region, so redisplay to show it. */
df0b5ea1
RS
11700 if (!NILP (Vtransient_mark_mode)
11701 && !NILP (current_buffer->mark_active))
6f27fa9b 11702 {
5f5c8ee5
GM
11703 clear_glyph_matrix (w->desired_matrix);
11704 if (!try_window (window, startp))
cb617e7c 11705 goto need_larger_matrices;
6f27fa9b 11706 }
a2889657 11707 }
5f5c8ee5 11708
5f5c8ee5
GM
11709#if GLYPH_DEBUG
11710 debug_method_add (w, "forced window start");
11711#endif
a2889657
JB
11712 goto done;
11713 }
11714
5f5c8ee5 11715 /* Handle case where text has not changed, only point, and it has
3f029ea0
RS
11716 not moved off the frame, and we are not retrying after hscroll.
11717 (current_matrix_up_to_date_p is nonzero when retrying.) */
11718 if (current_matrix_up_to_date_p
47589c8c 11719 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
cb617e7c 11720 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
a2889657 11721 {
cb617e7c
GM
11722 switch (rc)
11723 {
11724 case CURSOR_MOVEMENT_SUCCESS:
88e6b646 11725 used_current_matrix_p = 1;
cb617e7c 11726 goto done;
2311178e 11727
b60c9653 11728#if 0 /* try_cursor_movement never returns this value. */
cb617e7c
GM
11729 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
11730 goto need_larger_matrices;
b60c9653 11731#endif
2311178e 11732
cb617e7c
GM
11733 case CURSOR_MOVEMENT_MUST_SCROLL:
11734 goto try_to_scroll;
2311178e 11735
cb617e7c
GM
11736 default:
11737 abort ();
11738 }
a2889657
JB
11739 }
11740 /* If current starting point was originally the beginning of a line
11741 but no longer is, find a new starting point. */
265a9e55 11742 else if (!NILP (w->start_at_line_beg)
5f5c8ee5
GM
11743 && !(CHARPOS (startp) <= BEGV
11744 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
a2889657 11745 {
5f5c8ee5
GM
11746#if GLYPH_DEBUG
11747 debug_method_add (w, "recenter 1");
11748#endif
a2889657
JB
11749 goto recenter;
11750 }
2311178e 11751
27d16f05
GM
11752 /* Try scrolling with try_window_id. Value is > 0 if update has
11753 been done, it is -1 if we know that the same window start will
11754 not work. It is 0 if unsuccessful for some other reason. */
11755 else if ((tem = try_window_id (w)) != 0)
a2889657 11756 {
5f5c8ee5 11757#if GLYPH_DEBUG
ef121659 11758 debug_method_add (w, "try_window_id %d", tem);
5f5c8ee5
GM
11759#endif
11760
11761 if (fonts_changed_p)
cb617e7c 11762 goto need_larger_matrices;
a2889657
JB
11763 if (tem > 0)
11764 goto done;
ef121659 11765
5f5c8ee5
GM
11766 /* Otherwise try_window_id has returned -1 which means that we
11767 don't want the alternative below this comment to execute. */
a2889657 11768 }
5f5c8ee5
GM
11769 else if (CHARPOS (startp) >= BEGV
11770 && CHARPOS (startp) <= ZV
11771 && PT >= CHARPOS (startp)
11772 && (CHARPOS (startp) < ZV
e9874cee 11773 /* Avoid starting at end of buffer. */
5f5c8ee5 11774 || CHARPOS (startp) == BEGV
8850a573
RS
11775 || (XFASTINT (w->last_modified) >= MODIFF
11776 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
a2889657 11777 {
5f5c8ee5
GM
11778#if GLYPH_DEBUG
11779 debug_method_add (w, "same window start");
11780#endif
2311178e 11781
5f5c8ee5
GM
11782 /* Try to redisplay starting at same place as before.
11783 If point has not moved off frame, accept the results. */
11784 if (!current_matrix_up_to_date_p
11785 /* Don't use try_window_reusing_current_matrix in this case
15e26c76
GM
11786 because a window scroll function can have changed the
11787 buffer. */
5f5c8ee5
GM
11788 || !NILP (Vwindow_scroll_functions)
11789 || MINI_WINDOW_P (w)
88e6b646
KS
11790 || !(used_current_matrix_p =
11791 try_window_reusing_current_matrix (w)))
5f5c8ee5
GM
11792 {
11793 IF_DEBUG (debug_method_add (w, "1"));
11794 try_window (window, startp);
11795 }
11796
11797 if (fonts_changed_p)
cb617e7c 11798 goto need_larger_matrices;
2311178e 11799
5f5c8ee5 11800 if (w->cursor.vpos >= 0)
aa6d10fa 11801 {
2311178e 11802 if (!just_this_one_p
5f5c8ee5 11803 || current_buffer->clip_changed
9142dd5b 11804 || BEG_UNCHANGED < CHARPOS (startp))
aa6d10fa
RS
11805 /* Forget any recorded base line for line number display. */
11806 w->base_line_number = Qnil;
2311178e 11807
cb9d4a9f 11808 if (!make_cursor_line_fully_visible (w, 1))
03b0a4b4
RS
11809 {
11810 clear_glyph_matrix (w->desired_matrix);
11811 last_line_misfit = 1;
11812 }
ddf6b9a3 11813 /* Drop through and scroll. */
ef3c2c73
RS
11814 else
11815 goto done;
aa6d10fa 11816 }
a2889657 11817 else
5f5c8ee5 11818 clear_glyph_matrix (w->desired_matrix);
a2889657
JB
11819 }
11820
5f5c8ee5
GM
11821 try_to_scroll:
11822
ac90c44f
GM
11823 w->last_modified = make_number (0);
11824 w->last_overlay_modified = make_number (0);
5f5c8ee5 11825
e481f960
RS
11826 /* Redisplay the mode line. Select the buffer properly for that. */
11827 if (!update_mode_line)
11828 {
e481f960
RS
11829 update_mode_line = 1;
11830 w->update_mode_line = Qt;
11831 }
a2889657 11832
5f5c8ee5
GM
11833 /* Try to scroll by specified few lines. */
11834 if ((scroll_conservatively
11835 || scroll_step
11836 || temp_scroll_step
11837 || NUMBERP (current_buffer->scroll_up_aggressively)
11838 || NUMBERP (current_buffer->scroll_down_aggressively))
09cacf9c 11839 && !current_buffer->clip_changed
2311178e 11840 && CHARPOS (startp) >= BEGV
5f5c8ee5 11841 && CHARPOS (startp) <= ZV)
0789adb2 11842 {
5f5c8ee5
GM
11843 /* The function returns -1 if new fonts were loaded, 1 if
11844 successful, 0 if not successful. */
11845 int rc = try_scrolling (window, just_this_one_p,
11846 scroll_conservatively,
11847 scroll_step,
03b0a4b4 11848 temp_scroll_step, last_line_misfit);
cb617e7c
GM
11849 switch (rc)
11850 {
11851 case SCROLLING_SUCCESS:
11852 goto done;
2311178e 11853
cb617e7c
GM
11854 case SCROLLING_NEED_LARGER_MATRICES:
11855 goto need_larger_matrices;
2311178e 11856
cb617e7c
GM
11857 case SCROLLING_FAILED:
11858 break;
2311178e 11859
cb617e7c
GM
11860 default:
11861 abort ();
11862 }
5f5c8ee5 11863 }
f9c8af06 11864
5f5c8ee5 11865 /* Finally, just choose place to start which centers point */
5936754e 11866
5f5c8ee5 11867 recenter:
ddf6b9a3
RS
11868 centering_position = window_box_height (w) / 2;
11869
11870 point_at_top:
11871 /* Jump here with centering_position already set to 0. */
44173109 11872
5f5c8ee5
GM
11873#if GLYPH_DEBUG
11874 debug_method_add (w, "recenter");
11875#endif
0789adb2 11876
5f5c8ee5 11877 /* w->vscroll = 0; */
0789adb2 11878
5f5c8ee5 11879 /* Forget any previously recorded base line for line number display. */
c62c1bb5 11880 if (!buffer_unchanged_p)
5f5c8ee5
GM
11881 w->base_line_number = Qnil;
11882
11883 /* Move backward half the height of the window. */
11884 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11885 it.current_y = it.last_visible_y;
ddf6b9a3 11886 move_it_vertically_backward (&it, centering_position);
5f5c8ee5
GM
11887 xassert (IT_CHARPOS (it) >= BEGV);
11888
11889 /* The function move_it_vertically_backward may move over more
11890 than the specified y-distance. If it->w is small, e.g. a
11891 mini-buffer window, we may end up in front of the window's
11892 display area. Start displaying at the start of the line
11893 containing PT in this case. */
11894 if (it.current_y <= 0)
11895 {
11896 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11897 move_it_vertically (&it, 0);
11898 xassert (IT_CHARPOS (it) <= PT);
11899 it.current_y = 0;
0789adb2
RS
11900 }
11901
5f5c8ee5 11902 it.current_x = it.hpos = 0;
2311178e 11903
5f5c8ee5
GM
11904 /* Set startp here explicitly in case that helps avoid an infinite loop
11905 in case the window-scroll-functions functions get errors. */
11906 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
11907
11908 /* Run scroll hooks. */
11909 startp = run_window_scroll_functions (window, it.current.pos);
11910
11911 /* Redisplay the window. */
11912 if (!current_matrix_up_to_date_p
11913 || windows_or_buffers_changed
5fb96e96 11914 || cursor_type_changed
5f5c8ee5
GM
11915 /* Don't use try_window_reusing_current_matrix in this case
11916 because it can have changed the buffer. */
11917 || !NILP (Vwindow_scroll_functions)
11918 || !just_this_one_p
11919 || MINI_WINDOW_P (w)
88e6b646
KS
11920 || !(used_current_matrix_p =
11921 try_window_reusing_current_matrix (w)))
5f5c8ee5
GM
11922 try_window (window, startp);
11923
11924 /* If new fonts have been loaded (due to fontsets), give up. We
11925 have to start a new redisplay since we need to re-adjust glyph
11926 matrices. */
11927 if (fonts_changed_p)
cb617e7c 11928 goto need_larger_matrices;
5f5c8ee5
GM
11929
11930 /* If cursor did not appear assume that the middle of the window is
11931 in the first line of the window. Do it again with the next line.
11932 (Imagine a window of height 100, displaying two lines of height
11933 60. Moving back 50 from it->last_visible_y will end in the first
11934 line.) */
11935 if (w->cursor.vpos < 0)
a2889657 11936 {
5f5c8ee5
GM
11937 if (!NILP (w->window_end_valid)
11938 && PT >= Z - XFASTINT (w->window_end_pos))
a2889657 11939 {
5f5c8ee5
GM
11940 clear_glyph_matrix (w->desired_matrix);
11941 move_it_by_lines (&it, 1, 0);
11942 try_window (window, it.current.pos);
a2889657 11943 }
5f5c8ee5 11944 else if (PT < IT_CHARPOS (it))
a2889657 11945 {
5f5c8ee5
GM
11946 clear_glyph_matrix (w->desired_matrix);
11947 move_it_by_lines (&it, -1, 0);
11948 try_window (window, it.current.pos);
11949 }
11950 else
11951 {
11952 /* Not much we can do about it. */
a2889657 11953 }
a2889657 11954 }
010494d0 11955
5f5c8ee5
GM
11956 /* Consider the following case: Window starts at BEGV, there is
11957 invisible, intangible text at BEGV, so that display starts at
11958 some point START > BEGV. It can happen that we are called with
11959 PT somewhere between BEGV and START. Try to handle that case. */
11960 if (w->cursor.vpos < 0)
835766b6 11961 {
5f5c8ee5
GM
11962 struct glyph_row *row = w->current_matrix->rows;
11963 if (row->mode_line_p)
11964 ++row;
11965 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
835766b6 11966 }
2311178e 11967
cb9d4a9f 11968 if (!make_cursor_line_fully_visible (w, centering_position > 0))
ddf6b9a3 11969 {
932357b4
KS
11970 /* If vscroll is enabled, disable it and try again. */
11971 if (w->vscroll)
11972 {
11973 w->vscroll = 0;
11974 clear_glyph_matrix (w->desired_matrix);
11975 goto recenter;
11976 }
2a6d0874 11977
ddf6b9a3
RS
11978 /* If centering point failed to make the whole line visible,
11979 put point at the top instead. That has to make the whole line
11980 visible, if it can be done. */
cb9d4a9f 11981 clear_glyph_matrix (w->desired_matrix);
ddf6b9a3
RS
11982 centering_position = 0;
11983 goto point_at_top;
11984 }
b5174a51 11985
74d481ac
GM
11986 done:
11987
5f5c8ee5
GM
11988 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11989 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
11990 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
11991 ? Qt : Qnil);
a2889657 11992
5f5c8ee5 11993 /* Display the mode line, if we must. */
e481f960 11994 if ((update_mode_line
aa6d10fa 11995 /* If window not full width, must redo its mode line
2311178e 11996 if (a) the window to its side is being redone and
5f5c8ee5
GM
11997 (b) we do a frame-based redisplay. This is a consequence
11998 of how inverted lines are drawn in frame-based redisplay. */
2311178e 11999 || (!just_this_one_p
5f5c8ee5
GM
12000 && !FRAME_WINDOW_P (f)
12001 && !WINDOW_FULL_WIDTH_P (w))
12002 /* Line number to display. */
155ef550 12003 || INTEGERP (w->base_line_pos)
5f5c8ee5 12004 /* Column number is displayed and different from the one displayed. */
155ef550 12005 || (!NILP (w->column_number_displayed)
2311178e
TTN
12006 && (XFASTINT (w->column_number_displayed)
12007 != (int) current_column ()))) /* iftc */
5f5c8ee5
GM
12008 /* This means that the window has a mode line. */
12009 && (WINDOW_WANTS_MODELINE_P (w)
045dee35 12010 || WINDOW_WANTS_HEADER_LINE_P (w)))
5ba50c51 12011 {
5f5c8ee5
GM
12012 display_mode_lines (w);
12013
12014 /* If mode line height has changed, arrange for a thorough
12015 immediate redisplay using the correct mode line height. */
12016 if (WINDOW_WANTS_MODELINE_P (w)
12017 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
5ba50c51 12018 {
5f5c8ee5
GM
12019 fonts_changed_p = 1;
12020 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12021 = DESIRED_MODE_LINE_HEIGHT (w);
5ba50c51 12022 }
2311178e 12023
5f5c8ee5
GM
12024 /* If top line height has changed, arrange for a thorough
12025 immediate redisplay using the correct mode line height. */
045dee35
GM
12026 if (WINDOW_WANTS_HEADER_LINE_P (w)
12027 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
5f5c8ee5
GM
12028 {
12029 fonts_changed_p = 1;
045dee35
GM
12030 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12031 = DESIRED_HEADER_LINE_HEIGHT (w);
5f5c8ee5
GM
12032 }
12033
12034 if (fonts_changed_p)
cb617e7c 12035 goto need_larger_matrices;
5ba50c51 12036 }
5f5c8ee5
GM
12037
12038 if (!line_number_displayed
12039 && !BUFFERP (w->base_line_pos))
aa6d10fa
RS
12040 {
12041 w->base_line_pos = Qnil;
12042 w->base_line_number = Qnil;
12043 }
a2889657 12044
5f5c8ee5 12045 finish_menu_bars:
2311178e 12046
7ce2c095 12047 /* When we reach a frame's selected window, redo the frame's menu bar. */
e481f960 12048 if (update_mode_line
5f5c8ee5
GM
12049 && EQ (FRAME_SELECTED_WINDOW (f), window))
12050 {
12051 int redisplay_menu_p = 0;
488dd4c4 12052 int redisplay_tool_bar_p = 0;
5f5c8ee5
GM
12053
12054 if (FRAME_WINDOW_P (f))
12055 {
488dd4c4
JD
12056#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12057 || defined (USE_GTK)
5f5c8ee5 12058 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
76412d64 12059#else
5f5c8ee5 12060 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
76412d64 12061#endif
5f5c8ee5
GM
12062 }
12063 else
12064 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12065
12066 if (redisplay_menu_p)
12067 display_menu_bar (w);
12068
12069#ifdef HAVE_WINDOW_SYSTEM
488dd4c4
JD
12070#ifdef USE_GTK
12071 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12072#else
12073 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12074 && (FRAME_TOOL_BAR_LINES (f) > 0
12075 || auto_resize_tool_bars_p);
177c0ea7 12076
488dd4c4
JD
12077#endif
12078
12079 if (redisplay_tool_bar_p)
12080 redisplay_tool_bar (f);
5f5c8ee5
GM
12081#endif
12082 }
7ce2c095 12083
7af0e8d7 12084#ifdef HAVE_WINDOW_SYSTEM
88e6b646 12085 if (update_window_fringes (w, 0)
6d925726 12086 && !just_this_one_p
88e6b646
KS
12087 && (used_current_matrix_p || overlay_arrow_seen)
12088 && !w->pseudo_window_p)
12089 {
12090 update_begin (f);
12091 BLOCK_INPUT;
12092 draw_window_fringes (w);
12093 UNBLOCK_INPUT;
12094 update_end (f);
12095 }
7af0e8d7 12096#endif /* HAVE_WINDOW_SYSTEM */
88e6b646 12097
b60c9653
RS
12098 /* We go to this label, with fonts_changed_p nonzero,
12099 if it is necessary to try again using larger glyph matrices.
12100 We have to redeem the scroll bar even in this case,
12101 because the loop in redisplay_internal expects that. */
cb617e7c
GM
12102 need_larger_matrices:
12103 ;
88f22aff 12104 finish_scroll_bars:
5f5c8ee5 12105
da8b7f4f 12106 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
30c566e4 12107 {
9a08d928
SM
12108 /* Set the thumb's position and size. */
12109 set_vertical_scroll_bar (w);
30c566e4 12110
5f5c8ee5
GM
12111 /* Note that we actually used the scroll bar attached to this
12112 window, so it shouldn't be deleted at the end of redisplay. */
504454e8 12113 redeem_scroll_bar_hook (w);
30c566e4 12114 }
b1d1124b 12115
5f5c8ee5
GM
12116 /* Restore current_buffer and value of point in it. */
12117 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
6a93695f 12118 set_buffer_internal_1 (old);
5f5c8ee5 12119 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
2e54982e
RS
12120
12121 unbind_to (count, Qnil);
a2889657 12122}
a2889657 12123
5f5c8ee5
GM
12124
12125/* Build the complete desired matrix of WINDOW with a window start
12126 buffer position POS. Value is non-zero if successful. It is zero
12127 if fonts were loaded during redisplay which makes re-adjusting
12128 glyph matrices necessary. */
12129
12130int
a2889657
JB
12131try_window (window, pos)
12132 Lisp_Object window;
5f5c8ee5
GM
12133 struct text_pos pos;
12134{
12135 struct window *w = XWINDOW (window);
12136 struct it it;
12137 struct glyph_row *last_text_row = NULL;
9cbab4ff 12138
5f5c8ee5
GM
12139 /* Make POS the new window start. */
12140 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12adba34 12141
5f5c8ee5
GM
12142 /* Mark cursor position as unknown. No overlay arrow seen. */
12143 w->cursor.vpos = -1;
a2889657 12144 overlay_arrow_seen = 0;
642eefc6 12145
5f5c8ee5
GM
12146 /* Initialize iterator and info to start at POS. */
12147 start_display (&it, w, pos);
a2889657 12148
5f5c8ee5
GM
12149 /* Display all lines of W. */
12150 while (it.current_y < it.last_visible_y)
12151 {
12152 if (display_line (&it))
12153 last_text_row = it.glyph_row - 1;
12154 if (fonts_changed_p)
12155 return 0;
12156 }
a2889657 12157
5f5c8ee5
GM
12158 /* If bottom moved off end of frame, change mode line percentage. */
12159 if (XFASTINT (w->window_end_pos) <= 0
12160 && Z != IT_CHARPOS (it))
a2889657
JB
12161 w->update_mode_line = Qt;
12162
5f5c8ee5
GM
12163 /* Set window_end_pos to the offset of the last character displayed
12164 on the window from the end of current_buffer. Set
12165 window_end_vpos to its row number. */
12166 if (last_text_row)
12167 {
12168 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12169 w->window_end_bytepos
12170 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
12171 w->window_end_pos
12172 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12173 w->window_end_vpos
12174 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
5f5c8ee5
GM
12175 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12176 ->displays_text_p);
12177 }
12178 else
12179 {
86bf3faa
RS
12180 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12181 w->window_end_pos = make_number (Z - ZV);
12182 w->window_end_vpos = make_number (0);
5f5c8ee5 12183 }
2311178e 12184
a2889657
JB
12185 /* But that is not valid info until redisplay finishes. */
12186 w->window_end_valid = Qnil;
5f5c8ee5 12187 return 1;
a2889657 12188}
5f5c8ee5
GM
12189
12190
a2889657 12191\f
5f5c8ee5
GM
12192/************************************************************************
12193 Window redisplay reusing current matrix when buffer has not changed
12194 ************************************************************************/
12195
12196/* Try redisplay of window W showing an unchanged buffer with a
12197 different window start than the last time it was displayed by
12198 reusing its current matrix. Value is non-zero if successful.
12199 W->start is the new window start. */
a2889657
JB
12200
12201static int
5f5c8ee5
GM
12202try_window_reusing_current_matrix (w)
12203 struct window *w;
a2889657 12204{
5f5c8ee5
GM
12205 struct frame *f = XFRAME (w->frame);
12206 struct glyph_row *row, *bottom_row;
12207 struct it it;
12208 struct run run;
12209 struct text_pos start, new_start;
12210 int nrows_scrolled, i;
12211 struct glyph_row *last_text_row;
12212 struct glyph_row *last_reused_text_row;
12213 struct glyph_row *start_row;
12214 int start_vpos, min_y, max_y;
75c5350a 12215
69d1f7c9 12216#if GLYPH_DEBUG
76cb5e06
GM
12217 if (inhibit_try_window_reusing)
12218 return 0;
12219#endif
12220
d18354b6
GM
12221 if (/* This function doesn't handle terminal frames. */
12222 !FRAME_WINDOW_P (f)
12223 /* Don't try to reuse the display if windows have been split
12224 or such. */
5fb96e96
RS
12225 || windows_or_buffers_changed
12226 || cursor_type_changed)
5f5c8ee5 12227 return 0;
a2889657 12228
5f5c8ee5
GM
12229 /* Can't do this if region may have changed. */
12230 if ((!NILP (Vtransient_mark_mode)
12231 && !NILP (current_buffer->mark_active))
8f897821
GM
12232 || !NILP (w->region_showing)
12233 || !NILP (Vshow_trailing_whitespace))
5f5c8ee5 12234 return 0;
a2889657 12235
5f5c8ee5 12236 /* If top-line visibility has changed, give up. */
045dee35
GM
12237 if (WINDOW_WANTS_HEADER_LINE_P (w)
12238 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
5f5c8ee5
GM
12239 return 0;
12240
12241 /* Give up if old or new display is scrolled vertically. We could
12242 make this function handle this, but right now it doesn't. */
12243 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12244 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
12245 return 0;
12246
12247 /* The variable new_start now holds the new window start. The old
12248 start `start' can be determined from the current matrix. */
12249 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12250 start = start_row->start.pos;
12251 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
a2889657 12252
5f5c8ee5
GM
12253 /* Clear the desired matrix for the display below. */
12254 clear_glyph_matrix (w->desired_matrix);
2311178e 12255
5f5c8ee5
GM
12256 if (CHARPOS (new_start) <= CHARPOS (start))
12257 {
12258 int first_row_y;
2311178e 12259
607ec83c
GM
12260 /* Don't use this method if the display starts with an ellipsis
12261 displayed for invisible text. It's not easy to handle that case
12262 below, and it's certainly not worth the effort since this is
12263 not a frequent case. */
12264 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12265 return 0;
12266
5f5c8ee5 12267 IF_DEBUG (debug_method_add (w, "twu1"));
2311178e 12268
5f5c8ee5
GM
12269 /* Display up to a row that can be reused. The variable
12270 last_text_row is set to the last row displayed that displays
b48f74cb
GM
12271 text. Note that it.vpos == 0 if or if not there is a
12272 header-line; it's not the same as the MATRIX_ROW_VPOS! */
5f5c8ee5
GM
12273 start_display (&it, w, new_start);
12274 first_row_y = it.current_y;
12275 w->cursor.vpos = -1;
12276 last_text_row = last_reused_text_row = NULL;
2311178e 12277
5f5c8ee5
GM
12278 while (it.current_y < it.last_visible_y
12279 && IT_CHARPOS (it) < CHARPOS (start)
12280 && !fonts_changed_p)
12281 if (display_line (&it))
12282 last_text_row = it.glyph_row - 1;
12283
12284 /* A value of current_y < last_visible_y means that we stopped
12285 at the previous window start, which in turn means that we
12286 have at least one reusable row. */
12287 if (it.current_y < it.last_visible_y)
a2889657 12288 {
b48f74cb 12289 /* IT.vpos always starts from 0; it counts text lines. */
5f5c8ee5 12290 nrows_scrolled = it.vpos;
2311178e 12291
5f5c8ee5
GM
12292 /* Find PT if not already found in the lines displayed. */
12293 if (w->cursor.vpos < 0)
a2889657 12294 {
5f5c8ee5 12295 int dy = it.current_y - first_row_y;
2311178e 12296
5f5c8ee5 12297 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
0df56f4d
GM
12298 row = row_containing_pos (w, PT, row, NULL, dy);
12299 if (row)
12300 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12301 dy, nrows_scrolled);
12302 else
5f5c8ee5
GM
12303 {
12304 clear_glyph_matrix (w->desired_matrix);
12305 return 0;
12306 }
a2889657 12307 }
5f5c8ee5
GM
12308
12309 /* Scroll the display. Do it before the current matrix is
12310 changed. The problem here is that update has not yet
12311 run, i.e. part of the current matrix is not up to date.
12312 scroll_run_hook will clear the cursor, and use the
12313 current matrix to get the height of the row the cursor is
12314 in. */
12315 run.current_y = first_row_y;
12316 run.desired_y = it.current_y;
12317 run.height = it.last_visible_y - it.current_y;
b48f74cb
GM
12318
12319 if (run.height > 0 && run.current_y != run.desired_y)
a2889657 12320 {
5f5c8ee5
GM
12321 update_begin (f);
12322 rif->update_window_begin_hook (w);
fa3c6b4d 12323 rif->clear_window_mouse_face (w);
5f5c8ee5 12324 rif->scroll_run_hook (w, &run);
64d1e7d3 12325 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5 12326 update_end (f);
a2889657 12327 }
5f5c8ee5
GM
12328
12329 /* Shift current matrix down by nrows_scrolled lines. */
12330 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12331 rotate_matrix (w->current_matrix,
12332 start_vpos,
12333 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12334 nrows_scrolled);
2311178e 12335
9c8b8382 12336 /* Disable lines that must be updated. */
5f5c8ee5 12337 for (i = 0; i < it.vpos; ++i)
b48f74cb 12338 (start_row + i)->enabled_p = 0;
9c8b8382 12339
5f5c8ee5 12340 /* Re-compute Y positions. */
da8b7f4f 12341 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
5f5c8ee5 12342 max_y = it.last_visible_y;
b48f74cb
GM
12343 for (row = start_row + nrows_scrolled;
12344 row < bottom_row;
12345 ++row)
d2f84654 12346 {
5f5c8ee5 12347 row->y = it.current_y;
75c5350a 12348 row->visible_height = row->height;
5f5c8ee5
GM
12349
12350 if (row->y < min_y)
75c5350a
GM
12351 row->visible_height -= min_y - row->y;
12352 if (row->y + row->height > max_y)
12353 row->visible_height -= row->y + row->height - max_y;
88e6b646 12354 row->redraw_fringe_bitmaps_p = 1;
2311178e 12355
5f5c8ee5 12356 it.current_y += row->height;
5f5c8ee5
GM
12357
12358 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12359 last_reused_text_row = row;
12360 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12361 break;
d2f84654 12362 }
2311178e 12363
9c8b8382
GM
12364 /* Disable lines in the current matrix which are now
12365 below the window. */
bafb434c 12366 for (++row; row < bottom_row; ++row)
9c8b8382 12367 row->enabled_p = 0;
a2889657 12368 }
5f5c8ee5
GM
12369
12370 /* Update window_end_pos etc.; last_reused_text_row is the last
12371 reused row from the current matrix containing text, if any.
12372 The value of last_text_row is the last displayed line
12373 containing text. */
12374 if (last_reused_text_row)
a2889657 12375 {
5f5c8ee5
GM
12376 w->window_end_bytepos
12377 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
ac90c44f
GM
12378 w->window_end_pos
12379 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12380 w->window_end_vpos
12381 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12382 w->current_matrix));
a2889657 12383 }
5f5c8ee5
GM
12384 else if (last_text_row)
12385 {
12386 w->window_end_bytepos
12387 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
12388 w->window_end_pos
12389 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12390 w->window_end_vpos
12391 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
5f5c8ee5
GM
12392 }
12393 else
12394 {
12395 /* This window must be completely empty. */
86bf3faa
RS
12396 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12397 w->window_end_pos = make_number (Z - ZV);
12398 w->window_end_vpos = make_number (0);
5f5c8ee5
GM
12399 }
12400 w->window_end_valid = Qnil;
a2889657 12401
5f5c8ee5
GM
12402 /* Update hint: don't try scrolling again in update_window. */
12403 w->desired_matrix->no_scrolling_p = 1;
2311178e 12404
5f5c8ee5
GM
12405#if GLYPH_DEBUG
12406 debug_method_add (w, "try_window_reusing_current_matrix 1");
12407#endif
12408 return 1;
a2889657 12409 }
5f5c8ee5
GM
12410 else if (CHARPOS (new_start) > CHARPOS (start))
12411 {
12412 struct glyph_row *pt_row, *row;
12413 struct glyph_row *first_reusable_row;
12414 struct glyph_row *first_row_to_display;
12415 int dy;
12416 int yb = window_text_bottom_y (w);
12417
5f5c8ee5
GM
12418 /* Find the row starting at new_start, if there is one. Don't
12419 reuse a partially visible line at the end. */
b48f74cb 12420 first_reusable_row = start_row;
5f5c8ee5
GM
12421 while (first_reusable_row->enabled_p
12422 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12423 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12424 < CHARPOS (new_start)))
12425 ++first_reusable_row;
12426
12427 /* Give up if there is no row to reuse. */
12428 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
28514cd9
GM
12429 || !first_reusable_row->enabled_p
12430 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12431 != CHARPOS (new_start)))
5f5c8ee5
GM
12432 return 0;
12433
5f5c8ee5
GM
12434 /* We can reuse fully visible rows beginning with
12435 first_reusable_row to the end of the window. Set
12436 first_row_to_display to the first row that cannot be reused.
12437 Set pt_row to the row containing point, if there is any. */
5f5c8ee5 12438 pt_row = NULL;
ac90c44f
GM
12439 for (first_row_to_display = first_reusable_row;
12440 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12441 ++first_row_to_display)
5f5c8ee5 12442 {
4bde0ebb
GM
12443 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12444 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
5f5c8ee5 12445 pt_row = first_row_to_display;
5f5c8ee5 12446 }
a2889657 12447
5f5c8ee5
GM
12448 /* Start displaying at the start of first_row_to_display. */
12449 xassert (first_row_to_display->y < yb);
12450 init_to_row_start (&it, w, first_row_to_display);
607ec83c 12451
b48f74cb
GM
12452 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12453 - start_vpos);
5f5c8ee5
GM
12454 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12455 - nrows_scrolled);
b48f74cb 12456 it.current_y = (first_row_to_display->y - first_reusable_row->y
da8b7f4f 12457 + WINDOW_HEADER_LINE_HEIGHT (w));
5f5c8ee5
GM
12458
12459 /* Display lines beginning with first_row_to_display in the
12460 desired matrix. Set last_text_row to the last row displayed
12461 that displays text. */
12462 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12463 if (pt_row == NULL)
12464 w->cursor.vpos = -1;
12465 last_text_row = NULL;
12466 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12467 if (display_line (&it))
12468 last_text_row = it.glyph_row - 1;
12469
12470 /* Give up If point isn't in a row displayed or reused. */
12471 if (w->cursor.vpos < 0)
12472 {
12473 clear_glyph_matrix (w->desired_matrix);
12474 return 0;
12475 }
12adba34 12476
5f5c8ee5
GM
12477 /* If point is in a reused row, adjust y and vpos of the cursor
12478 position. */
12479 if (pt_row)
12480 {
12481 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
12482 w->current_matrix);
12483 w->cursor.y -= first_reusable_row->y;
a2889657
JB
12484 }
12485
5f5c8ee5
GM
12486 /* Scroll the display. */
12487 run.current_y = first_reusable_row->y;
da8b7f4f 12488 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
5f5c8ee5 12489 run.height = it.last_visible_y - run.current_y;
4da61803 12490 dy = run.current_y - run.desired_y;
2311178e 12491
5f5c8ee5
GM
12492 if (run.height)
12493 {
5f5c8ee5
GM
12494 update_begin (f);
12495 rif->update_window_begin_hook (w);
fa3c6b4d 12496 rif->clear_window_mouse_face (w);
5f5c8ee5 12497 rif->scroll_run_hook (w, &run);
64d1e7d3 12498 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5
GM
12499 update_end (f);
12500 }
a2889657 12501
5f5c8ee5
GM
12502 /* Adjust Y positions of reused rows. */
12503 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
da8b7f4f 12504 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
5f5c8ee5 12505 max_y = it.last_visible_y;
b48f74cb 12506 for (row = first_reusable_row; row < first_row_to_display; ++row)
5f5c8ee5
GM
12507 {
12508 row->y -= dy;
75c5350a 12509 row->visible_height = row->height;
5f5c8ee5 12510 if (row->y < min_y)
75c5350a
GM
12511 row->visible_height -= min_y - row->y;
12512 if (row->y + row->height > max_y)
12513 row->visible_height -= row->y + row->height - max_y;
88e6b646 12514 row->redraw_fringe_bitmaps_p = 1;
5f5c8ee5 12515 }
a2889657 12516
5f5c8ee5
GM
12517 /* Scroll the current matrix. */
12518 xassert (nrows_scrolled > 0);
12519 rotate_matrix (w->current_matrix,
12520 start_vpos,
12521 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12522 -nrows_scrolled);
12523
ac90c44f
GM
12524 /* Disable rows not reused. */
12525 for (row -= nrows_scrolled; row < bottom_row; ++row)
12526 row->enabled_p = 0;
12527
5f5c8ee5
GM
12528 /* Adjust window end. A null value of last_text_row means that
12529 the window end is in reused rows which in turn means that
12530 only its vpos can have changed. */
12531 if (last_text_row)
12532 {
12533 w->window_end_bytepos
12534 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
12535 w->window_end_pos
12536 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12537 w->window_end_vpos
12538 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
5f5c8ee5
GM
12539 }
12540 else
a2889657 12541 {
ac90c44f
GM
12542 w->window_end_vpos
12543 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
a2889657 12544 }
2311178e 12545
5f5c8ee5
GM
12546 w->window_end_valid = Qnil;
12547 w->desired_matrix->no_scrolling_p = 1;
12548
12549#if GLYPH_DEBUG
12550 debug_method_add (w, "try_window_reusing_current_matrix 2");
12551#endif
12552 return 1;
a2889657 12553 }
2311178e 12554
5f5c8ee5
GM
12555 return 0;
12556}
a2889657 12557
a2889657 12558
5f5c8ee5
GM
12559\f
12560/************************************************************************
12561 Window redisplay reusing current matrix when buffer has changed
12562 ************************************************************************/
12563
1ec185cb
GM
12564static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
12565static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
5f5c8ee5
GM
12566 int *, int *));
12567static struct glyph_row *
12568find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
12569 struct glyph_row *));
12570
12571
12572/* Return the last row in MATRIX displaying text. If row START is
12573 non-null, start searching with that row. IT gives the dimensions
12574 of the display. Value is null if matrix is empty; otherwise it is
12575 a pointer to the row found. */
12576
12577static struct glyph_row *
12578find_last_row_displaying_text (matrix, it, start)
12579 struct glyph_matrix *matrix;
12580 struct it *it;
12581 struct glyph_row *start;
12582{
12583 struct glyph_row *row, *row_found;
12584
12585 /* Set row_found to the last row in IT->w's current matrix
12586 displaying text. The loop looks funny but think of partially
12587 visible lines. */
12588 row_found = NULL;
12589 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
12590 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12591 {
12592 xassert (row->enabled_p);
12593 row_found = row;
12594 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
12595 break;
12596 ++row;
a2889657 12597 }
2311178e 12598
5f5c8ee5
GM
12599 return row_found;
12600}
12601
a2889657 12602
5f5c8ee5 12603/* Return the last row in the current matrix of W that is not affected
d43fbe9d
GM
12604 by changes at the start of current_buffer that occurred since W's
12605 current matrix was built. Value is null if no such row exists.
a2889657 12606
d43fbe9d
GM
12607 BEG_UNCHANGED us the number of characters unchanged at the start of
12608 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
12609 first changed character in current_buffer. Characters at positions <
12610 BEG + BEG_UNCHANGED are at the same buffer positions as they were
12611 when the current matrix was built. */
5f5c8ee5
GM
12612
12613static struct glyph_row *
1ec185cb 12614find_last_unchanged_at_beg_row (w)
5f5c8ee5
GM
12615 struct window *w;
12616{
9142dd5b 12617 int first_changed_pos = BEG + BEG_UNCHANGED;
5f5c8ee5
GM
12618 struct glyph_row *row;
12619 struct glyph_row *row_found = NULL;
12620 int yb = window_text_bottom_y (w);
12621
12622 /* Find the last row displaying unchanged text. */
12623 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12624 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12625 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
a2889657 12626 {
5f5c8ee5
GM
12627 if (/* If row ends before first_changed_pos, it is unchanged,
12628 except in some case. */
12629 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
12630 /* When row ends in ZV and we write at ZV it is not
12631 unchanged. */
12632 && !row->ends_at_zv_p
12633 /* When first_changed_pos is the end of a continued line,
12634 row is not unchanged because it may be no longer
12635 continued. */
12636 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
88e6b646
KS
12637 && (row->continued_p
12638 || row->exact_window_width_line_p)))
5f5c8ee5
GM
12639 row_found = row;
12640
12641 /* Stop if last visible row. */
12642 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
12643 break;
2311178e 12644
5f5c8ee5 12645 ++row;
a2889657
JB
12646 }
12647
5f5c8ee5 12648 return row_found;
a2889657 12649}
5f5c8ee5
GM
12650
12651
12652/* Find the first glyph row in the current matrix of W that is not
2311178e 12653 affected by changes at the end of current_buffer since the
d43fbe9d
GM
12654 time W's current matrix was built.
12655
12656 Return in *DELTA the number of chars by which buffer positions in
12657 unchanged text at the end of current_buffer must be adjusted.
2311178e 12658
d43fbe9d
GM
12659 Return in *DELTA_BYTES the corresponding number of bytes.
12660
12661 Value is null if no such row exists, i.e. all rows are affected by
12662 changes. */
2311178e 12663
5f5c8ee5 12664static struct glyph_row *
1ec185cb 12665find_first_unchanged_at_end_row (w, delta, delta_bytes)
5f5c8ee5
GM
12666 struct window *w;
12667 int *delta, *delta_bytes;
a2889657 12668{
5f5c8ee5
GM
12669 struct glyph_row *row;
12670 struct glyph_row *row_found = NULL;
c581d710 12671
5f5c8ee5 12672 *delta = *delta_bytes = 0;
b2a76982 12673
1ec185cb
GM
12674 /* Display must not have been paused, otherwise the current matrix
12675 is not up to date. */
12676 if (NILP (w->window_end_valid))
12677 abort ();
2311178e 12678
1ec185cb 12679 /* A value of window_end_pos >= END_UNCHANGED means that the window
5f5c8ee5
GM
12680 end is in the range of changed text. If so, there is no
12681 unchanged row at the end of W's current matrix. */
9142dd5b 12682 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
5f5c8ee5
GM
12683 return NULL;
12684
12685 /* Set row to the last row in W's current matrix displaying text. */
12686 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
2311178e
TTN
12687
12688 /* If matrix is entirely empty, no unchanged row exists. */
5f5c8ee5
GM
12689 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12690 {
12691 /* The value of row is the last glyph row in the matrix having a
12692 meaningful buffer position in it. The end position of row
12693 corresponds to window_end_pos. This allows us to translate
12694 buffer positions in the current matrix to current buffer
12695 positions for characters not in changed text. */
12696 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12697 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12698 int last_unchanged_pos, last_unchanged_pos_old;
12699 struct glyph_row *first_text_row
12700 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12701
12702 *delta = Z - Z_old;
12703 *delta_bytes = Z_BYTE - Z_BYTE_old;
12704
12705 /* Set last_unchanged_pos to the buffer position of the last
12706 character in the buffer that has not been changed. Z is the
d43fbe9d
GM
12707 index + 1 of the last character in current_buffer, i.e. by
12708 subtracting END_UNCHANGED we get the index of the last
5f5c8ee5
GM
12709 unchanged character, and we have to add BEG to get its buffer
12710 position. */
9142dd5b 12711 last_unchanged_pos = Z - END_UNCHANGED + BEG;
5f5c8ee5 12712 last_unchanged_pos_old = last_unchanged_pos - *delta;
2311178e 12713
5f5c8ee5
GM
12714 /* Search backward from ROW for a row displaying a line that
12715 starts at a minimum position >= last_unchanged_pos_old. */
1ec185cb 12716 for (; row > first_text_row; --row)
5f5c8ee5 12717 {
1ec185cb
GM
12718 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
12719 abort ();
2311178e 12720
5f5c8ee5
GM
12721 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
12722 row_found = row;
5f5c8ee5
GM
12723 }
12724 }
12725
1ec185cb
GM
12726 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
12727 abort ();
2311178e 12728
5f5c8ee5 12729 return row_found;
c581d710
RS
12730}
12731
c581d710 12732
5f5c8ee5
GM
12733/* Make sure that glyph rows in the current matrix of window W
12734 reference the same glyph memory as corresponding rows in the
12735 frame's frame matrix. This function is called after scrolling W's
12736 current matrix on a terminal frame in try_window_id and
12737 try_window_reusing_current_matrix. */
12738
12739static void
12740sync_frame_with_window_matrix_rows (w)
12741 struct window *w;
c581d710 12742{
5f5c8ee5
GM
12743 struct frame *f = XFRAME (w->frame);
12744 struct glyph_row *window_row, *window_row_end, *frame_row;
12745
12746 /* Preconditions: W must be a leaf window and full-width. Its frame
12747 must have a frame matrix. */
12748 xassert (NILP (w->hchild) && NILP (w->vchild));
12749 xassert (WINDOW_FULL_WIDTH_P (w));
12750 xassert (!FRAME_WINDOW_P (f));
12751
12752 /* If W is a full-width window, glyph pointers in W's current matrix
12753 have, by definition, to be the same as glyph pointers in the
7d4cc828
GM
12754 corresponding frame matrix. Note that frame matrices have no
12755 marginal areas (see build_frame_matrix). */
5f5c8ee5
GM
12756 window_row = w->current_matrix->rows;
12757 window_row_end = window_row + w->current_matrix->nrows;
da8b7f4f 12758 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
5f5c8ee5 12759 while (window_row < window_row_end)
659a218f 12760 {
7d4cc828
GM
12761 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
12762 struct glyph *end = window_row->glyphs[LAST_AREA];
12763
12764 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
12765 frame_row->glyphs[TEXT_AREA] = start;
12766 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
12767 frame_row->glyphs[LAST_AREA] = end;
f002db93
GM
12768
12769 /* Disable frame rows whose corresponding window rows have
12770 been disabled in try_window_id. */
12771 if (!window_row->enabled_p)
12772 frame_row->enabled_p = 0;
2311178e 12773
5f5c8ee5 12774 ++window_row, ++frame_row;
659a218f 12775 }
a2889657 12776}
5f5c8ee5
GM
12777
12778
e037b9ec
GM
12779/* Find the glyph row in window W containing CHARPOS. Consider all
12780 rows between START and END (not inclusive). END null means search
12781 all rows to the end of the display area of W. Value is the row
12782 containing CHARPOS or null. */
12783
0bef35bc 12784struct glyph_row *
0df56f4d 12785row_containing_pos (w, charpos, start, end, dy)
e037b9ec
GM
12786 struct window *w;
12787 int charpos;
12788 struct glyph_row *start, *end;
0df56f4d 12789 int dy;
e037b9ec
GM
12790{
12791 struct glyph_row *row = start;
12792 int last_y;
12793
12794 /* If we happen to start on a header-line, skip that. */
12795 if (row->mode_line_p)
12796 ++row;
2311178e 12797
e037b9ec
GM
12798 if ((end && row >= end) || !row->enabled_p)
12799 return NULL;
2311178e 12800
0df56f4d 12801 last_y = window_text_bottom_y (w) - dy;
2311178e 12802
5cce13dd
RS
12803 while (1)
12804 {
12805 /* Give up if we have gone too far. */
12806 if (end && row >= end)
12807 return NULL;
40a301b3
RS
12808 /* This formerly returned if they were equal.
12809 I think that both quantities are of a "last plus one" type;
12810 if so, when they are equal, the row is within the screen. -- rms. */
12811 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
5cce13dd
RS
12812 return NULL;
12813
12814 /* If it is in this row, return this row. */
12815 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
e037b9ec 12816 || (MATRIX_ROW_END_CHARPOS (row) == charpos
0df56f4d
GM
12817 /* The end position of a row equals the start
12818 position of the next row. If CHARPOS is there, we
12819 would rather display it in the next line, except
12820 when this line ends in ZV. */
12821 && !row->ends_at_zv_p
5cce13dd
RS
12822 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12823 && charpos >= MATRIX_ROW_START_CHARPOS (row))
12824 return row;
12825 ++row;
12826 }
e037b9ec
GM
12827}
12828
12829
5f5c8ee5
GM
12830/* Try to redisplay window W by reusing its existing display. W's
12831 current matrix must be up to date when this function is called,
12832 i.e. window_end_valid must not be nil.
12833
12834 Value is
12835
12836 1 if display has been updated
12837 0 if otherwise unsuccessful
12838 -1 if redisplay with same window start is known not to succeed
12839
12840 The following steps are performed:
12841
12842 1. Find the last row in the current matrix of W that is not
12843 affected by changes at the start of current_buffer. If no such row
12844 is found, give up.
12845
12846 2. Find the first row in W's current matrix that is not affected by
12847 changes at the end of current_buffer. Maybe there is no such row.
12848
12849 3. Display lines beginning with the row + 1 found in step 1 to the
12850 row found in step 2 or, if step 2 didn't find a row, to the end of
12851 the window.
12852
12853 4. If cursor is not known to appear on the window, give up.
12854
12855 5. If display stopped at the row found in step 2, scroll the
12856 display and current matrix as needed.
12857
12858 6. Maybe display some lines at the end of W, if we must. This can
12859 happen under various circumstances, like a partially visible line
12860 becoming fully visible, or because newly displayed lines are displayed
12861 in smaller font sizes.
12862
12863 7. Update W's window end information. */
12864
12adba34 12865static int
5f5c8ee5 12866try_window_id (w)
12adba34 12867 struct window *w;
12adba34 12868{
5f5c8ee5
GM
12869 struct frame *f = XFRAME (w->frame);
12870 struct glyph_matrix *current_matrix = w->current_matrix;
12871 struct glyph_matrix *desired_matrix = w->desired_matrix;
12872 struct glyph_row *last_unchanged_at_beg_row;
12873 struct glyph_row *first_unchanged_at_end_row;
12874 struct glyph_row *row;
12875 struct glyph_row *bottom_row;
12876 int bottom_vpos;
12877 struct it it;
12878 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
12879 struct text_pos start_pos;
12880 struct run run;
12881 int first_unchanged_at_end_vpos = 0;
12882 struct glyph_row *last_text_row, *last_text_row_at_end;
12883 struct text_pos start;
27d16f05 12884 int first_changed_charpos, last_changed_charpos;
5f5c8ee5 12885
69d1f7c9 12886#if GLYPH_DEBUG
76cb5e06
GM
12887 if (inhibit_try_window_id)
12888 return 0;
12889#endif
12890
27d16f05
GM
12891 /* This is handy for debugging. */
12892#if 0
12893#define GIVE_UP(X) \
12894 do { \
12895 fprintf (stderr, "try_window_id give up %d\n", (X)); \
12896 return 0; \
12897 } while (0)
12898#else
62397849 12899#define GIVE_UP(X) return 0
27d16f05 12900#endif
2311178e 12901
5f5c8ee5
GM
12902 SET_TEXT_POS_FROM_MARKER (start, w->start);
12903
27d16f05
GM
12904 /* Don't use this for mini-windows because these can show
12905 messages and mini-buffers, and we don't handle that here. */
12906 if (MINI_WINDOW_P (w))
12907 GIVE_UP (1);
2311178e 12908
27d16f05 12909 /* This flag is used to prevent redisplay optimizations. */
5fb96e96 12910 if (windows_or_buffers_changed || cursor_type_changed)
27d16f05 12911 GIVE_UP (2);
2311178e 12912
c62c1bb5
RS
12913 /* Verify that narrowing has not changed.
12914 Also verify that we were not told to prevent redisplay optimizations.
12915 It would be nice to further
27d16f05 12916 reduce the number of cases where this prevents try_window_id. */
c62c1bb5
RS
12917 if (current_buffer->clip_changed
12918 || current_buffer->prevent_redisplay_optimizations_p)
27d16f05
GM
12919 GIVE_UP (3);
12920
12921 /* Window must either use window-based redisplay or be full width. */
12922 if (!FRAME_WINDOW_P (f)
12923 && (!line_ins_del_ok
12924 || !WINDOW_FULL_WIDTH_P (w)))
12925 GIVE_UP (4);
5f5c8ee5 12926
f5376658 12927 /* Give up if point is not known NOT to appear in W. */
27d16f05
GM
12928 if (PT < CHARPOS (start))
12929 GIVE_UP (5);
12930
12931 /* Another way to prevent redisplay optimizations. */
12932 if (XFASTINT (w->last_modified) == 0)
12933 GIVE_UP (6);
2311178e 12934
f5376658 12935 /* Verify that window is not hscrolled. */
27d16f05
GM
12936 if (XFASTINT (w->hscroll) != 0)
12937 GIVE_UP (7);
2311178e 12938
f5376658 12939 /* Verify that display wasn't paused. */
27d16f05
GM
12940 if (NILP (w->window_end_valid))
12941 GIVE_UP (8);
2311178e 12942
27d16f05
GM
12943 /* Can't use this if highlighting a region because a cursor movement
12944 will do more than just set the cursor. */
12945 if (!NILP (Vtransient_mark_mode)
12946 && !NILP (current_buffer->mark_active))
12947 GIVE_UP (9);
12948
12949 /* Likewise if highlighting trailing whitespace. */
12950 if (!NILP (Vshow_trailing_whitespace))
12951 GIVE_UP (11);
2311178e 12952
27d16f05
GM
12953 /* Likewise if showing a region. */
12954 if (!NILP (w->region_showing))
12955 GIVE_UP (10);
2311178e 12956
27d16f05 12957 /* Can use this if overlay arrow position and or string have changed. */
351b5434 12958 if (overlay_arrows_changed_p ())
27d16f05
GM
12959 GIVE_UP (12);
12960
2311178e 12961
5f5c8ee5
GM
12962 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
12963 only if buffer has really changed. The reason is that the gap is
12964 initially at Z for freshly visited files. The code below would
12965 set end_unchanged to 0 in that case. */
28ee91c0
GM
12966 if (MODIFF > SAVE_MODIFF
12967 /* This seems to happen sometimes after saving a buffer. */
12968 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
5f5c8ee5 12969 {
9142dd5b
GM
12970 if (GPT - BEG < BEG_UNCHANGED)
12971 BEG_UNCHANGED = GPT - BEG;
12972 if (Z - GPT < END_UNCHANGED)
12973 END_UNCHANGED = Z - GPT;
5f5c8ee5 12974 }
bf9249e3 12975
27d16f05
GM
12976 /* The position of the first and last character that has been changed. */
12977 first_changed_charpos = BEG + BEG_UNCHANGED;
12978 last_changed_charpos = Z - END_UNCHANGED;
12979
5f5c8ee5
GM
12980 /* If window starts after a line end, and the last change is in
12981 front of that newline, then changes don't affect the display.
f2d86d7a
GM
12982 This case happens with stealth-fontification. Note that although
12983 the display is unchanged, glyph positions in the matrix have to
12984 be adjusted, of course. */
5f5c8ee5 12985 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
27d16f05 12986 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
e60f4527 12987 && ((last_changed_charpos < CHARPOS (start)
27d16f05 12988 && CHARPOS (start) == BEGV)
e60f4527 12989 || (last_changed_charpos < CHARPOS (start) - 1
27d16f05
GM
12990 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
12991 {
12992 int Z_old, delta, Z_BYTE_old, delta_bytes;
12993 struct glyph_row *r0;
12994
12995 /* Compute how many chars/bytes have been added to or removed
12996 from the buffer. */
12997 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12998 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12999 delta = Z - Z_old;
13000 delta_bytes = Z_BYTE - Z_BYTE_old;
2311178e 13001
27d16f05
GM
13002 /* Give up if PT is not in the window. Note that it already has
13003 been checked at the start of try_window_id that PT is not in
13004 front of the window start. */
13005 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13006 GIVE_UP (13);
13007
13008 /* If window start is unchanged, we can reuse the whole matrix
13009 as is, after adjusting glyph positions. No need to compute
13010 the window end again, since its offset from Z hasn't changed. */
13011 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13012 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
2d031b89
AS
13013 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13014 /* PT must not be in a partially visible line. */
13015 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13016 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
27d16f05
GM
13017 {
13018 /* Adjust positions in the glyph matrix. */
13019 if (delta || delta_bytes)
13020 {
13021 struct glyph_row *r1
13022 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13023 increment_matrix_positions (w->current_matrix,
13024 MATRIX_ROW_VPOS (r0, current_matrix),
13025 MATRIX_ROW_VPOS (r1, current_matrix),
13026 delta, delta_bytes);
13027 }
2311178e 13028
27d16f05 13029 /* Set the cursor. */
0df56f4d 13030 row = row_containing_pos (w, PT, r0, NULL, 0);
59adf8e8
KS
13031 if (row)
13032 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
40a301b3
RS
13033 else
13034 abort ();
27d16f05
GM
13035 return 1;
13036 }
5f5c8ee5
GM
13037 }
13038
27d16f05 13039 /* Handle the case that changes are all below what is displayed in
33ea6c4d 13040 the window, and that PT is in the window. This shortcut cannot
2b9c25e0
GM
13041 be taken if ZV is visible in the window, and text has been added
13042 there that is visible in the window. */
13043 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
33ea6c4d
GM
13044 /* ZV is not visible in the window, or there are no
13045 changes at ZV, actually. */
13046 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13047 || first_changed_charpos == last_changed_charpos))
5f5c8ee5 13048 {
27d16f05 13049 struct glyph_row *r0;
ef121659 13050
27d16f05
GM
13051 /* Give up if PT is not in the window. Note that it already has
13052 been checked at the start of try_window_id that PT is not in
13053 front of the window start. */
13054 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13055 GIVE_UP (14);
13056
13057 /* If window start is unchanged, we can reuse the whole matrix
13058 as is, without changing glyph positions since no text has
13059 been added/removed in front of the window end. */
13060 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
2d031b89
AS
13061 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13062 /* PT must not be in a partially visible line. */
13063 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13064 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
27d16f05
GM
13065 {
13066 /* We have to compute the window end anew since text
13067 can have been added/removed after it. */
13068 w->window_end_pos
13069 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13070 w->window_end_bytepos
13071 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13072
13073 /* Set the cursor. */
0df56f4d 13074 row = row_containing_pos (w, PT, r0, NULL, 0);
59adf8e8
KS
13075 if (row)
13076 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
40a301b3
RS
13077 else
13078 abort ();
27d16f05
GM
13079 return 2;
13080 }
5f5c8ee5
GM
13081 }
13082
2b9c25e0 13083 /* Give up if window start is in the changed area.
2311178e 13084
2b9c25e0
GM
13085 The condition used to read
13086
13087 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13088
13089 but why that was tested escapes me at the moment. */
13090 if (CHARPOS (start) >= first_changed_charpos
27d16f05
GM
13091 && CHARPOS (start) <= last_changed_charpos)
13092 GIVE_UP (15);
2311178e 13093
f5376658
RS
13094 /* Check that window start agrees with the start of the first glyph
13095 row in its current matrix. Check this after we know the window
13096 start is not in changed text, otherwise positions would not be
13097 comparable. */
27d16f05 13098 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
5f5c8ee5 13099 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
27d16f05 13100 GIVE_UP (16);
5f5c8ee5 13101
23e8bd86
GM
13102 /* Give up if the window ends in strings. Overlay strings
13103 at the end are difficult to handle, so don't try. */
13104 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13105 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13106 GIVE_UP (20);
13107
5f5c8ee5
GM
13108 /* Compute the position at which we have to start displaying new
13109 lines. Some of the lines at the top of the window might be
13110 reusable because they are not displaying changed text. Find the
13111 last row in W's current matrix not affected by changes at the
13112 start of current_buffer. Value is null if changes start in the
13113 first line of window. */
1ec185cb 13114 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
5f5c8ee5
GM
13115 if (last_unchanged_at_beg_row)
13116 {
67f1cf4c
GM
13117 /* Avoid starting to display in the moddle of a character, a TAB
13118 for instance. This is easier than to set up the iterator
13119 exactly, and it's not a frequent case, so the additional
13120 effort wouldn't really pay off. */
e74fb0f7
GM
13121 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13122 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
67f1cf4c
GM
13123 && last_unchanged_at_beg_row > w->current_matrix->rows)
13124 --last_unchanged_at_beg_row;
13125
13126 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
27d16f05 13127 GIVE_UP (17);
47d57b22
GM
13128
13129 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13130 GIVE_UP (18);
5f5c8ee5 13131 start_pos = it.current.pos;
2311178e 13132
5f5c8ee5
GM
13133 /* Start displaying new lines in the desired matrix at the same
13134 vpos we would use in the current matrix, i.e. below
13135 last_unchanged_at_beg_row. */
13136 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13137 current_matrix);
13138 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13139 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13140
13141 xassert (it.hpos == 0 && it.current_x == 0);
13142 }
13143 else
13144 {
13145 /* There are no reusable lines at the start of the window.
3ccb8bcf 13146 Start displaying in the first text line. */
5f5c8ee5 13147 start_display (&it, w, start);
3ccb8bcf 13148 it.vpos = it.first_vpos;
5f5c8ee5
GM
13149 start_pos = it.current.pos;
13150 }
13151
5f5c8ee5
GM
13152 /* Find the first row that is not affected by changes at the end of
13153 the buffer. Value will be null if there is no unchanged row, in
13154 which case we must redisplay to the end of the window. delta
13155 will be set to the value by which buffer positions beginning with
13156 first_unchanged_at_end_row have to be adjusted due to text
13157 changes. */
13158 first_unchanged_at_end_row
1ec185cb 13159 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
5f5c8ee5
GM
13160 IF_DEBUG (debug_delta = delta);
13161 IF_DEBUG (debug_delta_bytes = delta_bytes);
2311178e 13162
5f5c8ee5
GM
13163 /* Set stop_pos to the buffer position up to which we will have to
13164 display new lines. If first_unchanged_at_end_row != NULL, this
13165 is the buffer position of the start of the line displayed in that
13166 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13167 that we don't stop at a buffer position. */
13168 stop_pos = 0;
13169 if (first_unchanged_at_end_row)
13170 {
13171 xassert (last_unchanged_at_beg_row == NULL
13172 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
2311178e 13173
5f5c8ee5
GM
13174 /* If this is a continuation line, move forward to the next one
13175 that isn't. Changes in lines above affect this line.
13176 Caution: this may move first_unchanged_at_end_row to a row
13177 not displaying text. */
13178 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13179 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13180 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13181 < it.last_visible_y))
13182 ++first_unchanged_at_end_row;
13183
13184 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13185 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13186 >= it.last_visible_y))
13187 first_unchanged_at_end_row = NULL;
13188 else
13189 {
13190 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13191 + delta);
13192 first_unchanged_at_end_vpos
13193 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
9142dd5b 13194 xassert (stop_pos >= Z - END_UNCHANGED);
5f5c8ee5
GM
13195 }
13196 }
13197 else if (last_unchanged_at_beg_row == NULL)
23e8bd86 13198 GIVE_UP (19);
5f5c8ee5
GM
13199
13200
13201#if GLYPH_DEBUG
2311178e 13202
5f5c8ee5
GM
13203 /* Either there is no unchanged row at the end, or the one we have
13204 now displays text. This is a necessary condition for the window
13205 end pos calculation at the end of this function. */
13206 xassert (first_unchanged_at_end_row == NULL
13207 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
2311178e 13208
5f5c8ee5
GM
13209 debug_last_unchanged_at_beg_vpos
13210 = (last_unchanged_at_beg_row
13211 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13212 : -1);
13213 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
2311178e 13214
5f5c8ee5
GM
13215#endif /* GLYPH_DEBUG != 0 */
13216
2311178e 13217
5f5c8ee5
GM
13218 /* Display new lines. Set last_text_row to the last new line
13219 displayed which has text on it, i.e. might end up as being the
13220 line where the window_end_vpos is. */
13221 w->cursor.vpos = -1;
13222 last_text_row = NULL;
13223 overlay_arrow_seen = 0;
13224 while (it.current_y < it.last_visible_y
13225 && !fonts_changed_p
13226 && (first_unchanged_at_end_row == NULL
13227 || IT_CHARPOS (it) < stop_pos))
13228 {
13229 if (display_line (&it))
13230 last_text_row = it.glyph_row - 1;
13231 }
13232
13233 if (fonts_changed_p)
13234 return -1;
13235
13236
13237 /* Compute differences in buffer positions, y-positions etc. for
13238 lines reused at the bottom of the window. Compute what we can
13239 scroll. */
ca42b2e8
GM
13240 if (first_unchanged_at_end_row
13241 /* No lines reused because we displayed everything up to the
13242 bottom of the window. */
13243 && it.current_y < it.last_visible_y)
5f5c8ee5
GM
13244 {
13245 dvpos = (it.vpos
13246 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13247 current_matrix));
13248 dy = it.current_y - first_unchanged_at_end_row->y;
13249 run.current_y = first_unchanged_at_end_row->y;
13250 run.desired_y = run.current_y + dy;
13251 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13252 }
13253 else
ca42b2e8
GM
13254 {
13255 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13256 first_unchanged_at_end_row = NULL;
13257 }
5f5c8ee5
GM
13258 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13259
8f8ba186 13260
5f5c8ee5
GM
13261 /* Find the cursor if not already found. We have to decide whether
13262 PT will appear on this window (it sometimes doesn't, but this is
13263 not a very frequent case.) This decision has to be made before
13264 the current matrix is altered. A value of cursor.vpos < 0 means
13265 that PT is either in one of the lines beginning at
13266 first_unchanged_at_end_row or below the window. Don't care for
13267 lines that might be displayed later at the window end; as
13268 mentioned, this is not a frequent case. */
13269 if (w->cursor.vpos < 0)
13270 {
5f5c8ee5
GM
13271 /* Cursor in unchanged rows at the top? */
13272 if (PT < CHARPOS (start_pos)
13273 && last_unchanged_at_beg_row)
13274 {
e037b9ec
GM
13275 row = row_containing_pos (w, PT,
13276 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
0df56f4d 13277 last_unchanged_at_beg_row + 1, 0);
bfe0ee88
GM
13278 if (row)
13279 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
5f5c8ee5
GM
13280 }
13281
13282 /* Start from first_unchanged_at_end_row looking for PT. */
13283 else if (first_unchanged_at_end_row)
13284 {
e037b9ec 13285 row = row_containing_pos (w, PT - delta,
0df56f4d 13286 first_unchanged_at_end_row, NULL, 0);
e037b9ec 13287 if (row)
468155d7
GM
13288 set_cursor_from_row (w, row, w->current_matrix, delta,
13289 delta_bytes, dy, dvpos);
5f5c8ee5
GM
13290 }
13291
13292 /* Give up if cursor was not found. */
13293 if (w->cursor.vpos < 0)
13294 {
13295 clear_glyph_matrix (w->desired_matrix);
13296 return -1;
13297 }
13298 }
2311178e 13299
5f5c8ee5
GM
13300 /* Don't let the cursor end in the scroll margins. */
13301 {
13302 int this_scroll_margin, cursor_height;
2311178e 13303
5f5c8ee5 13304 this_scroll_margin = max (0, scroll_margin);
da8b7f4f
KS
13305 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13306 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
5f5c8ee5 13307 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
2311178e 13308
5f5c8ee5
GM
13309 if ((w->cursor.y < this_scroll_margin
13310 && CHARPOS (start) > BEGV)
13311 /* Don't take scroll margin into account at the bottom because
13312 old redisplay didn't do it either. */
13313 || w->cursor.y + cursor_height > it.last_visible_y)
13314 {
13315 w->cursor.vpos = -1;
13316 clear_glyph_matrix (w->desired_matrix);
13317 return -1;
13318 }
13319 }
13320
13321 /* Scroll the display. Do it before changing the current matrix so
13322 that xterm.c doesn't get confused about where the cursor glyph is
13323 found. */
fa77249f 13324 if (dy && run.height)
5f5c8ee5
GM
13325 {
13326 update_begin (f);
2311178e 13327
5f5c8ee5
GM
13328 if (FRAME_WINDOW_P (f))
13329 {
13330 rif->update_window_begin_hook (w);
fa3c6b4d 13331 rif->clear_window_mouse_face (w);
5f5c8ee5 13332 rif->scroll_run_hook (w, &run);
64d1e7d3 13333 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5
GM
13334 }
13335 else
13336 {
13337 /* Terminal frame. In this case, dvpos gives the number of
13338 lines to scroll by; dvpos < 0 means scroll up. */
13339 int first_unchanged_at_end_vpos
13340 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
da8b7f4f
KS
13341 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13342 int end = (WINDOW_TOP_EDGE_LINE (w)
522ed7fb
GM
13343 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13344 + window_internal_height (w));
2311178e 13345
5f5c8ee5
GM
13346 /* Perform the operation on the screen. */
13347 if (dvpos > 0)
13348 {
13349 /* Scroll last_unchanged_at_beg_row to the end of the
13350 window down dvpos lines. */
13351 set_terminal_window (end);
13352
13353 /* On dumb terminals delete dvpos lines at the end
13354 before inserting dvpos empty lines. */
13355 if (!scroll_region_ok)
13356 ins_del_lines (end - dvpos, -dvpos);
13357
13358 /* Insert dvpos empty lines in front of
13359 last_unchanged_at_beg_row. */
13360 ins_del_lines (from, dvpos);
13361 }
13362 else if (dvpos < 0)
13363 {
13364 /* Scroll up last_unchanged_at_beg_vpos to the end of
13365 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13366 set_terminal_window (end);
13367
13368 /* Delete dvpos lines in front of
13369 last_unchanged_at_beg_vpos. ins_del_lines will set
13370 the cursor to the given vpos and emit |dvpos| delete
13371 line sequences. */
13372 ins_del_lines (from + dvpos, dvpos);
13373
13374 /* On a dumb terminal insert dvpos empty lines at the
13375 end. */
13376 if (!scroll_region_ok)
13377 ins_del_lines (end + dvpos, -dvpos);
13378 }
2311178e 13379
5f5c8ee5
GM
13380 set_terminal_window (0);
13381 }
2311178e 13382
5f5c8ee5
GM
13383 update_end (f);
13384 }
13385
ca42b2e8
GM
13386 /* Shift reused rows of the current matrix to the right position.
13387 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13388 text. */
13389 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13390 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
5f5c8ee5
GM
13391 if (dvpos < 0)
13392 {
13393 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13394 bottom_vpos, dvpos);
13395 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13396 bottom_vpos, 0);
13397 }
13398 else if (dvpos > 0)
13399 {
13400 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13401 bottom_vpos, dvpos);
13402 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13403 first_unchanged_at_end_vpos + dvpos, 0);
13404 }
13405
13406 /* For frame-based redisplay, make sure that current frame and window
13407 matrix are in sync with respect to glyph memory. */
13408 if (!FRAME_WINDOW_P (f))
13409 sync_frame_with_window_matrix_rows (w);
13410
13411 /* Adjust buffer positions in reused rows. */
13412 if (delta)
f2d86d7a
GM
13413 increment_matrix_positions (current_matrix,
13414 first_unchanged_at_end_vpos + dvpos,
13415 bottom_vpos, delta, delta_bytes);
5f5c8ee5
GM
13416
13417 /* Adjust Y positions. */
13418 if (dy)
13419 shift_glyph_matrix (w, current_matrix,
13420 first_unchanged_at_end_vpos + dvpos,
13421 bottom_vpos, dy);
13422
13423 if (first_unchanged_at_end_row)
13424 first_unchanged_at_end_row += dvpos;
13425
13426 /* If scrolling up, there may be some lines to display at the end of
13427 the window. */
13428 last_text_row_at_end = NULL;
13429 if (dy < 0)
13430 {
9c6ba6d1
GM
13431 /* Scrolling up can leave for example a partially visible line
13432 at the end of the window to be redisplayed. */
5f5c8ee5
GM
13433 /* Set last_row to the glyph row in the current matrix where the
13434 window end line is found. It has been moved up or down in
13435 the matrix by dvpos. */
13436 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13437 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13438
13439 /* If last_row is the window end line, it should display text. */
13440 xassert (last_row->displays_text_p);
13441
13442 /* If window end line was partially visible before, begin
13443 displaying at that line. Otherwise begin displaying with the
13444 line following it. */
13445 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13446 {
13447 init_to_row_start (&it, w, last_row);
13448 it.vpos = last_vpos;
13449 it.current_y = last_row->y;
13450 }
13451 else
13452 {
13453 init_to_row_end (&it, w, last_row);
13454 it.vpos = 1 + last_vpos;
13455 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13456 ++last_row;
13457 }
12adba34 13458
23e8bd86
GM
13459 /* We may start in a continuation line. If so, we have to
13460 get the right continuation_lines_width and current_x. */
13461 it.continuation_lines_width = last_row->continuation_lines_width;
13462 it.hpos = it.current_x = 0;
2311178e 13463
23e8bd86
GM
13464 /* Display the rest of the lines at the window end. */
13465 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13466 while (it.current_y < it.last_visible_y
13467 && !fonts_changed_p)
5f5c8ee5 13468 {
23e8bd86
GM
13469 /* Is it always sure that the display agrees with lines in
13470 the current matrix? I don't think so, so we mark rows
13471 displayed invalid in the current matrix by setting their
13472 enabled_p flag to zero. */
13473 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13474 if (display_line (&it))
13475 last_text_row_at_end = it.glyph_row - 1;
5f5c8ee5
GM
13476 }
13477 }
12adba34 13478
5f5c8ee5
GM
13479 /* Update window_end_pos and window_end_vpos. */
13480 if (first_unchanged_at_end_row
13481 && first_unchanged_at_end_row->y < it.last_visible_y
13482 && !last_text_row_at_end)
13483 {
13484 /* Window end line if one of the preserved rows from the current
13485 matrix. Set row to the last row displaying text in current
13486 matrix starting at first_unchanged_at_end_row, after
13487 scrolling. */
13488 xassert (first_unchanged_at_end_row->displays_text_p);
13489 row = find_last_row_displaying_text (w->current_matrix, &it,
13490 first_unchanged_at_end_row);
13491 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
2311178e 13492
ac90c44f 13493 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
5f5c8ee5 13494 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
ac90c44f
GM
13495 w->window_end_vpos
13496 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
23fca891 13497 xassert (w->window_end_bytepos >= 0);
0416532f 13498 IF_DEBUG (debug_method_add (w, "A"));
5f5c8ee5
GM
13499 }
13500 else if (last_text_row_at_end)
13501 {
ac90c44f
GM
13502 w->window_end_pos
13503 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
5f5c8ee5
GM
13504 w->window_end_bytepos
13505 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
ac90c44f
GM
13506 w->window_end_vpos
13507 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
23fca891 13508 xassert (w->window_end_bytepos >= 0);
0416532f 13509 IF_DEBUG (debug_method_add (w, "B"));
5f5c8ee5
GM
13510 }
13511 else if (last_text_row)
13512 {
13513 /* We have displayed either to the end of the window or at the
13514 end of the window, i.e. the last row with text is to be found
13515 in the desired matrix. */
ac90c44f
GM
13516 w->window_end_pos
13517 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
5f5c8ee5
GM
13518 w->window_end_bytepos
13519 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
13520 w->window_end_vpos
13521 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
23fca891 13522 xassert (w->window_end_bytepos >= 0);
5f5c8ee5
GM
13523 }
13524 else if (first_unchanged_at_end_row == NULL
13525 && last_text_row == NULL
13526 && last_text_row_at_end == NULL)
13527 {
13528 /* Displayed to end of window, but no line containing text was
13529 displayed. Lines were deleted at the end of the window. */
0416532f
GM
13530 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
13531 int vpos = XFASTINT (w->window_end_vpos);
13532 struct glyph_row *current_row = current_matrix->rows + vpos;
13533 struct glyph_row *desired_row = desired_matrix->rows + vpos;
13534
13535 for (row = NULL;
13536 row == NULL && vpos >= first_vpos;
13537 --vpos, --current_row, --desired_row)
13538 {
13539 if (desired_row->enabled_p)
13540 {
13541 if (desired_row->displays_text_p)
13542 row = desired_row;
13543 }
13544 else if (current_row->displays_text_p)
13545 row = current_row;
13546 }
12adba34 13547
0416532f 13548 xassert (row != NULL);
d88a79d4 13549 w->window_end_vpos = make_number (vpos + 1);
8c56a983
GM
13550 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13551 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
23fca891 13552 xassert (w->window_end_bytepos >= 0);
0416532f 13553 IF_DEBUG (debug_method_add (w, "C"));
5f5c8ee5
GM
13554 }
13555 else
13556 abort ();
ac90c44f 13557
8cdb267e
GM
13558#if 0 /* This leads to problems, for instance when the cursor is
13559 at ZV, and the cursor line displays no text. */
ac90c44f
GM
13560 /* Disable rows below what's displayed in the window. This makes
13561 debugging easier. */
13562 enable_glyph_matrix_rows (current_matrix,
13563 XFASTINT (w->window_end_vpos) + 1,
13564 bottom_vpos, 0);
8cdb267e 13565#endif
23e8bd86 13566
5f5c8ee5
GM
13567 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
13568 debug_end_vpos = XFASTINT (w->window_end_vpos));
12adba34 13569
5f5c8ee5
GM
13570 /* Record that display has not been completed. */
13571 w->window_end_valid = Qnil;
13572 w->desired_matrix->no_scrolling_p = 1;
ef121659 13573 return 3;
27d16f05
GM
13574
13575#undef GIVE_UP
12adba34 13576}
0f9c0ff0 13577
a2889657 13578
5f5c8ee5
GM
13579\f
13580/***********************************************************************
13581 More debugging support
13582 ***********************************************************************/
a2889657 13583
5f5c8ee5 13584#if GLYPH_DEBUG
a2889657 13585
eaaa65b0 13586void dump_glyph_row P_ ((struct glyph_row *, int, int));
9ab436b9 13587void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
e187cf71 13588void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
1c9241f5 13589
31b24551 13590
9ab436b9 13591/* Dump the contents of glyph matrix MATRIX on stderr.
ca26e1c8 13592
9ab436b9
GM
13593 GLYPHS 0 means don't show glyph contents.
13594 GLYPHS 1 means show glyphs in short form
13595 GLYPHS > 1 means show glyphs in long form. */
13596
13597void
13598dump_glyph_matrix (matrix, glyphs)
5f5c8ee5 13599 struct glyph_matrix *matrix;
9ab436b9 13600 int glyphs;
5f5c8ee5 13601{
efc63ef0 13602 int i;
5f5c8ee5 13603 for (i = 0; i < matrix->nrows; ++i)
eaaa65b0 13604 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
5f5c8ee5 13605}
31b24551 13606
68a37fa8 13607
e187cf71
GM
13608/* Dump contents of glyph GLYPH to stderr. ROW and AREA are
13609 the glyph row and area where the glyph comes from. */
13610
13611void
13612dump_glyph (row, glyph, area)
13613 struct glyph_row *row;
13614 struct glyph *glyph;
13615 int area;
13616{
13617 if (glyph->type == CHAR_GLYPH)
13618 {
13619 fprintf (stderr,
13620 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13621 glyph - row->glyphs[TEXT_AREA],
13622 'C',
13623 glyph->charpos,
13624 (BUFFERP (glyph->object)
13625 ? 'B'
13626 : (STRINGP (glyph->object)
13627 ? 'S'
13628 : '-')),
13629 glyph->pixel_width,
13630 glyph->u.ch,
13631 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
13632 ? glyph->u.ch
13633 : '.'),
13634 glyph->face_id,
13635 glyph->left_box_line_p,
13636 glyph->right_box_line_p);
13637 }
13638 else if (glyph->type == STRETCH_GLYPH)
13639 {
13640 fprintf (stderr,
13641 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13642 glyph - row->glyphs[TEXT_AREA],
13643 'S',
13644 glyph->charpos,
13645 (BUFFERP (glyph->object)
13646 ? 'B'
13647 : (STRINGP (glyph->object)
13648 ? 'S'
13649 : '-')),
13650 glyph->pixel_width,
13651 0,
13652 '.',
13653 glyph->face_id,
13654 glyph->left_box_line_p,
13655 glyph->right_box_line_p);
13656 }
13657 else if (glyph->type == IMAGE_GLYPH)
13658 {
13659 fprintf (stderr,
13660 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13661 glyph - row->glyphs[TEXT_AREA],
13662 'I',
13663 glyph->charpos,
13664 (BUFFERP (glyph->object)
13665 ? 'B'
13666 : (STRINGP (glyph->object)
13667 ? 'S'
13668 : '-')),
13669 glyph->pixel_width,
13670 glyph->u.img_id,
13671 '.',
13672 glyph->face_id,
13673 glyph->left_box_line_p,
13674 glyph->right_box_line_p);
13675 }
13676}
13677
13678
5f5c8ee5 13679/* Dump the contents of glyph row at VPOS in MATRIX to stderr.
9ab436b9
GM
13680 GLYPHS 0 means don't show glyph contents.
13681 GLYPHS 1 means show glyphs in short form
13682 GLYPHS > 1 means show glyphs in long form. */
a2889657 13683
5f5c8ee5 13684void
eaaa65b0
GM
13685dump_glyph_row (row, vpos, glyphs)
13686 struct glyph_row *row;
9ab436b9 13687 int vpos, glyphs;
5f5c8ee5 13688{
9ab436b9
GM
13689 if (glyphs != 1)
13690 {
b94c0d9c 13691 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
9ab436b9 13692 fprintf (stderr, "=======================================================================\n");
2311178e 13693
0ad38729 13694 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
33ea6c4d 13695%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 13696 vpos,
9ab436b9
GM
13697 MATRIX_ROW_START_CHARPOS (row),
13698 MATRIX_ROW_END_CHARPOS (row),
13699 row->used[TEXT_AREA],
13700 row->contains_overlapping_glyphs_p,
13701 row->enabled_p,
9ab436b9
GM
13702 row->truncated_on_left_p,
13703 row->truncated_on_right_p,
13704 row->overlay_arrow_p,
13705 row->continued_p,
13706 MATRIX_ROW_CONTINUATION_LINE_P (row),
13707 row->displays_text_p,
13708 row->ends_at_zv_p,
13709 row->fill_line_p,
13710 row->ends_in_middle_of_char_p,
13711 row->starts_in_middle_of_char_p,
b94c0d9c 13712 row->mouse_face_p,
9ab436b9
GM
13713 row->x,
13714 row->y,
13715 row->pixel_width,
13716 row->height,
13717 row->visible_height,
13718 row->ascent,
13719 row->phys_ascent);
33ea6c4d
GM
13720 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
13721 row->end.overlay_string_index,
13722 row->continuation_lines_width);
9ab436b9
GM
13723 fprintf (stderr, "%9d %5d\n",
13724 CHARPOS (row->start.string_pos),
13725 CHARPOS (row->end.string_pos));
13726 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
13727 row->end.dpvec_index);
13728 }
2311178e 13729
9ab436b9 13730 if (glyphs > 1)
bd66d1ba 13731 {
e187cf71
GM
13732 int area;
13733
13734 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13735 {
091f8878
GM
13736 struct glyph *glyph = row->glyphs[area];
13737 struct glyph *glyph_end = glyph + row->used[area];
2311178e 13738
e187cf71 13739 /* Glyph for a line end in text. */
091f8878 13740 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
e187cf71 13741 ++glyph_end;
2311178e 13742
e187cf71
GM
13743 if (glyph < glyph_end)
13744 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
2311178e 13745
e187cf71
GM
13746 for (; glyph < glyph_end; ++glyph)
13747 dump_glyph (row, glyph, area);
f7b4b63a 13748 }
f4faa47c 13749 }
9ab436b9
GM
13750 else if (glyphs == 1)
13751 {
e187cf71 13752 int area;
9ab436b9 13753
e187cf71 13754 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
9ab436b9 13755 {
e187cf71
GM
13756 char *s = (char *) alloca (row->used[area] + 1);
13757 int i;
13758
13759 for (i = 0; i < row->used[area]; ++i)
13760 {
13761 struct glyph *glyph = row->glyphs[area] + i;
13762 if (glyph->type == CHAR_GLYPH
13763 && glyph->u.ch < 0x80
13764 && glyph->u.ch >= ' ')
13765 s[i] = glyph->u.ch;
13766 else
13767 s[i] = '.';
13768 }
2311178e 13769
e187cf71
GM
13770 s[i] = '\0';
13771 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
13772 }
9ab436b9 13773 }
5f5c8ee5 13774}
f4faa47c 13775
a2889657 13776
5f5c8ee5
GM
13777DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
13778 Sdump_glyph_matrix, 0, 1, "p",
7ee72033 13779 doc: /* Dump the current matrix of the selected window to stderr.
228299fa
GM
13780Shows contents of glyph row structures. With non-nil
13781parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
7ee72033
MB
13782glyphs in short form, otherwise show glyphs in long form. */)
13783 (glyphs)
9ab436b9 13784 Lisp_Object glyphs;
5f5c8ee5
GM
13785{
13786 struct window *w = XWINDOW (selected_window);
13787 struct buffer *buffer = XBUFFER (w->buffer);
13788
13789 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
13790 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
13791 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
13792 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
13793 fprintf (stderr, "=============================================\n");
9ab436b9
GM
13794 dump_glyph_matrix (w->current_matrix,
13795 NILP (glyphs) ? 0 : XINT (glyphs));
5f5c8ee5
GM
13796 return Qnil;
13797}
1c2250c2 13798
1fca3fae 13799
7d4cc828
GM
13800DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
13801 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
13802 ()
13803{
13804 struct frame *f = XFRAME (selected_frame);
13805 dump_glyph_matrix (f->current_matrix, 1);
13806 return Qnil;
13807}
13808
13809
e9abc296 13810DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
7ee72033 13811 doc: /* Dump glyph row ROW to stderr.
228299fa
GM
13812GLYPH 0 means don't dump glyphs.
13813GLYPH 1 means dump glyphs in short form.
7ee72033
MB
13814GLYPH > 1 or omitted means dump glyphs in long form. */)
13815 (row, glyphs)
e9abc296 13816 Lisp_Object row, glyphs;
5f5c8ee5 13817{
eaaa65b0
GM
13818 struct glyph_matrix *matrix;
13819 int vpos;
2311178e 13820
b7826503 13821 CHECK_NUMBER (row);
eaaa65b0
GM
13822 matrix = XWINDOW (selected_window)->current_matrix;
13823 vpos = XINT (row);
13824 if (vpos >= 0 && vpos < matrix->nrows)
13825 dump_glyph_row (MATRIX_ROW (matrix, vpos),
13826 vpos,
13827 INTEGERP (glyphs) ? XINT (glyphs) : 2);
5f5c8ee5
GM
13828 return Qnil;
13829}
1fca3fae 13830
67481ae5 13831
b94c0d9c 13832DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
c2552f79 13833 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
228299fa
GM
13834GLYPH 0 means don't dump glyphs.
13835GLYPH 1 means dump glyphs in short form.
7ee72033
MB
13836GLYPH > 1 or omitted means dump glyphs in long form. */)
13837 (row, glyphs)
b94c0d9c 13838 Lisp_Object row, glyphs;
5f5c8ee5 13839{
886bd6f2 13840 struct frame *sf = SELECTED_FRAME ();
eaaa65b0
GM
13841 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
13842 int vpos;
2311178e 13843
b7826503 13844 CHECK_NUMBER (row);
eaaa65b0
GM
13845 vpos = XINT (row);
13846 if (vpos >= 0 && vpos < m->nrows)
13847 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
13848 INTEGERP (glyphs) ? XINT (glyphs) : 2);
5f5c8ee5
GM
13849 return Qnil;
13850}
ca26e1c8 13851
0f9c0ff0 13852
62397849 13853DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
7ee72033
MB
13854 doc: /* Toggle tracing of redisplay.
13855With ARG, turn tracing on if and only if ARG is positive. */)
13856 (arg)
62397849 13857 Lisp_Object arg;
5f5c8ee5 13858{
62397849
GM
13859 if (NILP (arg))
13860 trace_redisplay_p = !trace_redisplay_p;
13861 else
13862 {
13863 arg = Fprefix_numeric_value (arg);
13864 trace_redisplay_p = XINT (arg) > 0;
13865 }
2311178e 13866
5f5c8ee5
GM
13867 return Qnil;
13868}
bf9249e3
GM
13869
13870
f4a374a1 13871DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
e9757d41
SM
13872 doc: /* Like `format', but print result to stderr.
13873usage: (trace-to-stderr STRING &rest OBJECTS) */)
7ee72033 13874 (nargs, args)
f4a374a1
GM
13875 int nargs;
13876 Lisp_Object *args;
bf9249e3 13877{
f4a374a1 13878 Lisp_Object s = Fformat (nargs, args);
2051c264 13879 fprintf (stderr, "%s", SDATA (s));
bf9249e3
GM
13880 return Qnil;
13881}
2311178e 13882
5f5c8ee5 13883#endif /* GLYPH_DEBUG */
ca26e1c8 13884
ca26e1c8 13885
5f5c8ee5
GM
13886\f
13887/***********************************************************************
13888 Building Desired Matrix Rows
13889 ***********************************************************************/
a2889657 13890
5f5c8ee5
GM
13891/* Return a temporary glyph row holding the glyphs of an overlay
13892 arrow. Only used for non-window-redisplay windows. */
ca26e1c8 13893
5f5c8ee5 13894static struct glyph_row *
351b5434 13895get_overlay_arrow_glyph_row (w, overlay_arrow_string)
5f5c8ee5 13896 struct window *w;
351b5434 13897 Lisp_Object overlay_arrow_string;
5f5c8ee5
GM
13898{
13899 struct frame *f = XFRAME (WINDOW_FRAME (w));
13900 struct buffer *buffer = XBUFFER (w->buffer);
13901 struct buffer *old = current_buffer;
351b5434
KS
13902 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
13903 int arrow_len = SCHARS (overlay_arrow_string);
50f80c2f
KR
13904 const unsigned char *arrow_end = arrow_string + arrow_len;
13905 const unsigned char *p;
5f5c8ee5
GM
13906 struct it it;
13907 int multibyte_p;
13908 int n_glyphs_before;
13909
13910 set_buffer_temp (buffer);
13911 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
13912 it.glyph_row->used[TEXT_AREA] = 0;
13913 SET_TEXT_POS (it.position, 0, 0);
13914
13915 multibyte_p = !NILP (buffer->enable_multibyte_characters);
13916 p = arrow_string;
13917 while (p < arrow_end)
13918 {
13919 Lisp_Object face, ilisp;
2311178e 13920
5f5c8ee5
GM
13921 /* Get the next character. */
13922 if (multibyte_p)
4fdb80f2 13923 it.c = string_char_and_length (p, arrow_len, &it.len);
5f5c8ee5
GM
13924 else
13925 it.c = *p, it.len = 1;
13926 p += it.len;
2311178e 13927
5f5c8ee5 13928 /* Get its face. */
ac90c44f 13929 ilisp = make_number (p - arrow_string);
351b5434 13930 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
5f5c8ee5
GM
13931 it.face_id = compute_char_face (f, it.c, face);
13932
13933 /* Compute its width, get its glyphs. */
13934 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
337042a9 13935 SET_TEXT_POS (it.position, -1, -1);
5f5c8ee5
GM
13936 PRODUCE_GLYPHS (&it);
13937
13938 /* If this character doesn't fit any more in the line, we have
13939 to remove some glyphs. */
13940 if (it.current_x > it.last_visible_x)
13941 {
13942 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
13943 break;
13944 }
13945 }
2311178e 13946
5f5c8ee5
GM
13947 set_buffer_temp (old);
13948 return it.glyph_row;
13949}
ca26e1c8 13950
b0a0fbda 13951
5f5c8ee5
GM
13952/* Insert truncation glyphs at the start of IT->glyph_row. Truncation
13953 glyphs are only inserted for terminal frames since we can't really
13954 win with truncation glyphs when partially visible glyphs are
13955 involved. Which glyphs to insert is determined by
13956 produce_special_glyphs. */
67481ae5 13957
5f5c8ee5
GM
13958static void
13959insert_left_trunc_glyphs (it)
13960 struct it *it;
13961{
13962 struct it truncate_it;
13963 struct glyph *from, *end, *to, *toend;
13964
13965 xassert (!FRAME_WINDOW_P (it->f));
13966
13967 /* Get the truncation glyphs. */
13968 truncate_it = *it;
5f5c8ee5
GM
13969 truncate_it.current_x = 0;
13970 truncate_it.face_id = DEFAULT_FACE_ID;
13971 truncate_it.glyph_row = &scratch_glyph_row;
13972 truncate_it.glyph_row->used[TEXT_AREA] = 0;
13973 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
6fc556fd 13974 truncate_it.object = make_number (0);
5f5c8ee5 13975 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
2311178e 13976
5f5c8ee5
GM
13977 /* Overwrite glyphs from IT with truncation glyphs. */
13978 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
13979 end = from + truncate_it.glyph_row->used[TEXT_AREA];
13980 to = it->glyph_row->glyphs[TEXT_AREA];
13981 toend = to + it->glyph_row->used[TEXT_AREA];
13982
13983 while (from < end)
13984 *to++ = *from++;
13985
37be86f2
KH
13986 /* There may be padding glyphs left over. Overwrite them too. */
13987 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
13988 {
13989 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
13990 while (from < end)
13991 *to++ = *from++;
13992 }
5f5c8ee5 13993
37be86f2
KH
13994 if (to > toend)
13995 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
5f5c8ee5 13996}
e0bfbde6 13997
e0bfbde6 13998
5f5c8ee5 13999/* Compute the pixel height and width of IT->glyph_row.
9c49d3d7 14000
5f5c8ee5
GM
14001 Most of the time, ascent and height of a display line will be equal
14002 to the max_ascent and max_height values of the display iterator
14003 structure. This is not the case if
67481ae5 14004
5f5c8ee5
GM
14005 1. We hit ZV without displaying anything. In this case, max_ascent
14006 and max_height will be zero.
1c9241f5 14007
5f5c8ee5
GM
14008 2. We have some glyphs that don't contribute to the line height.
14009 (The glyph row flag contributes_to_line_height_p is for future
14010 pixmap extensions).
f6fd109b 14011
5f5c8ee5
GM
14012 The first case is easily covered by using default values because in
14013 these cases, the line height does not really matter, except that it
14014 must not be zero. */
67481ae5 14015
5f5c8ee5
GM
14016static void
14017compute_line_metrics (it)
14018 struct it *it;
14019{
14020 struct glyph_row *row = it->glyph_row;
14021 int area, i;
1c2250c2 14022
5f5c8ee5
GM
14023 if (FRAME_WINDOW_P (it->f))
14024 {
75c5350a 14025 int i, min_y, max_y;
1c2250c2 14026
5f5c8ee5
GM
14027 /* The line may consist of one space only, that was added to
14028 place the cursor on it. If so, the row's height hasn't been
14029 computed yet. */
14030 if (row->height == 0)
14031 {
14032 if (it->max_ascent + it->max_descent == 0)
da8b7f4f 14033 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
5f5c8ee5
GM
14034 row->ascent = it->max_ascent;
14035 row->height = it->max_ascent + it->max_descent;
312246d1
GM
14036 row->phys_ascent = it->max_phys_ascent;
14037 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
5f5c8ee5 14038 }
2311178e 14039
5f5c8ee5
GM
14040 /* Compute the width of this line. */
14041 row->pixel_width = row->x;
14042 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14043 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14044
14045 xassert (row->pixel_width >= 0);
14046 xassert (row->ascent >= 0 && row->height > 0);
14047
312246d1
GM
14048 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14049 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14050
14051 /* If first line's physical ascent is larger than its logical
14052 ascent, use the physical ascent, and make the row taller.
14053 This makes accented characters fully visible. */
b28cb6ed 14054 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
312246d1
GM
14055 && row->phys_ascent > row->ascent)
14056 {
14057 row->height += row->phys_ascent - row->ascent;
14058 row->ascent = row->phys_ascent;
14059 }
14060
5f5c8ee5
GM
14061 /* Compute how much of the line is visible. */
14062 row->visible_height = row->height;
2311178e 14063
da8b7f4f
KS
14064 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14065 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
75c5350a
GM
14066
14067 if (row->y < min_y)
14068 row->visible_height -= min_y - row->y;
14069 if (row->y + row->height > max_y)
14070 row->visible_height -= row->y + row->height - max_y;
5f5c8ee5
GM
14071 }
14072 else
14073 {
14074 row->pixel_width = row->used[TEXT_AREA];
33ea6c4d
GM
14075 if (row->continued_p)
14076 row->pixel_width -= it->continuation_pixel_width;
14077 else if (row->truncated_on_right_p)
14078 row->pixel_width -= it->truncation_pixel_width;
312246d1
GM
14079 row->ascent = row->phys_ascent = 0;
14080 row->height = row->phys_height = row->visible_height = 1;
5f5c8ee5 14081 }
67481ae5 14082
5f5c8ee5
GM
14083 /* Compute a hash code for this row. */
14084 row->hash = 0;
14085 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14086 for (i = 0; i < row->used[area]; ++i)
14087 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14088 + row->glyphs[area][i].u.val
43d120d8
KH
14089 + row->glyphs[area][i].face_id
14090 + row->glyphs[area][i].padding_p
5f5c8ee5 14091 + (row->glyphs[area][i].type << 2));
a2889657 14092
5f5c8ee5 14093 it->max_ascent = it->max_descent = 0;
312246d1 14094 it->max_phys_ascent = it->max_phys_descent = 0;
5f5c8ee5 14095}
12adba34 14096
ca26e1c8 14097
5f5c8ee5
GM
14098/* Append one space to the glyph row of iterator IT if doing a
14099 window-based redisplay. DEFAULT_FACE_P non-zero means let the
14100 space have the default face, otherwise let it have the same face as
80c6cb1f 14101 IT->face_id. Value is non-zero if a space was added.
c6e89d6c
GM
14102
14103 This function is called to make sure that there is always one glyph
14104 at the end of a glyph row that the cursor can be set on under
14105 window-systems. (If there weren't such a glyph we would not know
14106 how wide and tall a box cursor should be displayed).
14107
14108 At the same time this space let's a nicely handle clearing to the
14109 end of the line if the row ends in italic text. */
ca26e1c8 14110
80c6cb1f 14111static int
5f5c8ee5
GM
14112append_space (it, default_face_p)
14113 struct it *it;
14114 int default_face_p;
14115{
14116 if (FRAME_WINDOW_P (it->f))
14117 {
14118 int n = it->glyph_row->used[TEXT_AREA];
ca26e1c8 14119
5f5c8ee5
GM
14120 if (it->glyph_row->glyphs[TEXT_AREA] + n
14121 < it->glyph_row->glyphs[1 + TEXT_AREA])
a2889657 14122 {
cafafe0b
GM
14123 /* Save some values that must not be changed.
14124 Must save IT->c and IT->len because otherwise
14125 ITERATOR_AT_END_P wouldn't work anymore after
14126 append_space has been called. */
478d746b 14127 enum display_element_type saved_what = it->what;
cafafe0b
GM
14128 int saved_c = it->c, saved_len = it->len;
14129 int saved_x = it->current_x;
5f5c8ee5 14130 int saved_face_id = it->face_id;
cafafe0b 14131 struct text_pos saved_pos;
5f5c8ee5 14132 Lisp_Object saved_object;
980806b6 14133 struct face *face;
5f5c8ee5
GM
14134
14135 saved_object = it->object;
14136 saved_pos = it->position;
2311178e 14137
5f5c8ee5
GM
14138 it->what = IT_CHARACTER;
14139 bzero (&it->position, sizeof it->position);
6fc556fd 14140 it->object = make_number (0);
5f5c8ee5
GM
14141 it->c = ' ';
14142 it->len = 1;
5f5c8ee5
GM
14143
14144 if (default_face_p)
14145 it->face_id = DEFAULT_FACE_ID;
4aad61f8
GM
14146 else if (it->face_before_selective_p)
14147 it->face_id = it->saved_face_id;
980806b6
KH
14148 face = FACE_FROM_ID (it->f, it->face_id);
14149 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
1842fc1a 14150
5f5c8ee5 14151 PRODUCE_GLYPHS (it);
2311178e 14152
5f5c8ee5
GM
14153 it->current_x = saved_x;
14154 it->object = saved_object;
14155 it->position = saved_pos;
14156 it->what = saved_what;
14157 it->face_id = saved_face_id;
cafafe0b
GM
14158 it->len = saved_len;
14159 it->c = saved_c;
80c6cb1f 14160 return 1;
5f5c8ee5
GM
14161 }
14162 }
80c6cb1f
GM
14163
14164 return 0;
5f5c8ee5 14165}
12adba34 14166
1842fc1a 14167
5f5c8ee5
GM
14168/* Extend the face of the last glyph in the text area of IT->glyph_row
14169 to the end of the display line. Called from display_line.
14170 If the glyph row is empty, add a space glyph to it so that we
14171 know the face to draw. Set the glyph row flag fill_line_p. */
2311178e 14172
5f5c8ee5
GM
14173static void
14174extend_face_to_end_of_line (it)
14175 struct it *it;
14176{
14177 struct face *face;
14178 struct frame *f = it->f;
1842fc1a 14179
5f5c8ee5
GM
14180 /* If line is already filled, do nothing. */
14181 if (it->current_x >= it->last_visible_x)
14182 return;
2311178e 14183
5f5c8ee5
GM
14184 /* Face extension extends the background and box of IT->face_id
14185 to the end of the line. If the background equals the background
4aad61f8
GM
14186 of the frame, we don't have to do anything. */
14187 if (it->face_before_selective_p)
14188 face = FACE_FROM_ID (it->f, it->saved_face_id);
14189 else
14190 face = FACE_FROM_ID (f, it->face_id);
2311178e 14191
5f5c8ee5
GM
14192 if (FRAME_WINDOW_P (f)
14193 && face->box == FACE_NO_BOX
14194 && face->background == FRAME_BACKGROUND_PIXEL (f)
14195 && !face->stipple)
14196 return;
1842fc1a 14197
5f5c8ee5
GM
14198 /* Set the glyph row flag indicating that the face of the last glyph
14199 in the text area has to be drawn to the end of the text area. */
14200 it->glyph_row->fill_line_p = 1;
545e04f6 14201
980806b6
KH
14202 /* If current character of IT is not ASCII, make sure we have the
14203 ASCII face. This will be automatically undone the next time
14204 get_next_display_element returns a multibyte character. Note
14205 that the character will always be single byte in unibyte text. */
14206 if (!SINGLE_BYTE_CHAR_P (it->c))
5f5c8ee5 14207 {
980806b6 14208 it->face_id = FACE_FOR_CHAR (f, face, 0);
5f5c8ee5 14209 }
545e04f6 14210
5f5c8ee5
GM
14211 if (FRAME_WINDOW_P (f))
14212 {
14213 /* If the row is empty, add a space with the current face of IT,
14214 so that we know which face to draw. */
14215 if (it->glyph_row->used[TEXT_AREA] == 0)
a2889657 14216 {
5f5c8ee5 14217 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
43d120d8 14218 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
5f5c8ee5 14219 it->glyph_row->used[TEXT_AREA] = 1;
a2889657 14220 }
5f5c8ee5
GM
14221 }
14222 else
14223 {
14224 /* Save some values that must not be changed. */
14225 int saved_x = it->current_x;
14226 struct text_pos saved_pos;
14227 Lisp_Object saved_object;
478d746b 14228 enum display_element_type saved_what = it->what;
4aad61f8 14229 int saved_face_id = it->face_id;
5f5c8ee5
GM
14230
14231 saved_object = it->object;
14232 saved_pos = it->position;
2311178e 14233
5f5c8ee5
GM
14234 it->what = IT_CHARACTER;
14235 bzero (&it->position, sizeof it->position);
6fc556fd 14236 it->object = make_number (0);
5f5c8ee5
GM
14237 it->c = ' ';
14238 it->len = 1;
4aad61f8 14239 it->face_id = face->id;
2311178e 14240
5f5c8ee5 14241 PRODUCE_GLYPHS (it);
2311178e 14242
5f5c8ee5
GM
14243 while (it->current_x <= it->last_visible_x)
14244 PRODUCE_GLYPHS (it);
2311178e 14245
5f5c8ee5
GM
14246 /* Don't count these blanks really. It would let us insert a left
14247 truncation glyph below and make us set the cursor on them, maybe. */
14248 it->current_x = saved_x;
14249 it->object = saved_object;
14250 it->position = saved_pos;
14251 it->what = saved_what;
4aad61f8 14252 it->face_id = saved_face_id;
5f5c8ee5
GM
14253 }
14254}
12adba34 14255
545e04f6 14256
5f5c8ee5
GM
14257/* Value is non-zero if text starting at CHARPOS in current_buffer is
14258 trailing whitespace. */
1c9241f5 14259
5f5c8ee5
GM
14260static int
14261trailing_whitespace_p (charpos)
14262 int charpos;
14263{
14264 int bytepos = CHAR_TO_BYTE (charpos);
14265 int c = 0;
7bbe686f 14266
5f5c8ee5
GM
14267 while (bytepos < ZV_BYTE
14268 && (c = FETCH_CHAR (bytepos),
14269 c == ' ' || c == '\t'))
14270 ++bytepos;
0d09d1e6 14271
8f897821
GM
14272 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14273 {
14274 if (bytepos != PT_BYTE)
14275 return 1;
14276 }
14277 return 0;
5f5c8ee5 14278}
31b24551 14279
545e04f6 14280
5f5c8ee5 14281/* Highlight trailing whitespace, if any, in ROW. */
545e04f6 14282
5f5c8ee5
GM
14283void
14284highlight_trailing_whitespace (f, row)
14285 struct frame *f;
14286 struct glyph_row *row;
14287{
14288 int used = row->used[TEXT_AREA];
2311178e 14289
5f5c8ee5
GM
14290 if (used)
14291 {
14292 struct glyph *start = row->glyphs[TEXT_AREA];
14293 struct glyph *glyph = start + used - 1;
14294
ade0bee1
GM
14295 /* Skip over glyphs inserted to display the cursor at the
14296 end of a line, for extending the face of the last glyph
14297 to the end of the line on terminals, and for truncation
14298 and continuation glyphs. */
65008712
GM
14299 while (glyph >= start
14300 && glyph->type == CHAR_GLYPH
65008712 14301 && INTEGERP (glyph->object))
5f5c8ee5
GM
14302 --glyph;
14303
14304 /* If last glyph is a space or stretch, and it's trailing
14305 whitespace, set the face of all trailing whitespace glyphs in
14306 IT->glyph_row to `trailing-whitespace'. */
14307 if (glyph >= start
14308 && BUFFERP (glyph->object)
14309 && (glyph->type == STRETCH_GLYPH
14310 || (glyph->type == CHAR_GLYPH
43d120d8 14311 && glyph->u.ch == ' '))
5f5c8ee5 14312 && trailing_whitespace_p (glyph->charpos))
545e04f6 14313 {
980806b6 14314 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
2311178e 14315
5f5c8ee5
GM
14316 while (glyph >= start
14317 && BUFFERP (glyph->object)
14318 && (glyph->type == STRETCH_GLYPH
14319 || (glyph->type == CHAR_GLYPH
43d120d8
KH
14320 && glyph->u.ch == ' ')))
14321 (glyph--)->face_id = face_id;
545e04f6 14322 }
a2889657 14323 }
5f5c8ee5 14324}
a2889657 14325
5fcbb24d 14326
cafafe0b 14327/* Value is non-zero if glyph row ROW in window W should be
0fd37545 14328 used to hold the cursor. */
cafafe0b
GM
14329
14330static int
14331cursor_row_p (w, row)
14332 struct window *w;
14333 struct glyph_row *row;
14334{
9a038881 14335 int cursor_row_p = 1;
2311178e 14336
cafafe0b
GM
14337 if (PT == MATRIX_ROW_END_CHARPOS (row))
14338 {
9a038881
GM
14339 /* If the row ends with a newline from a string, we don't want
14340 the cursor there (if the row is continued it doesn't end in a
14341 newline). */
14342 if (CHARPOS (row->end.string_pos) >= 0
14343 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14344 cursor_row_p = row->continued_p;
14345
14346 /* If the row ends at ZV, display the cursor at the end of that
14347 row instead of at the start of the row below. */
14348 else if (row->ends_at_zv_p)
14349 cursor_row_p = 1;
14350 else
14351 cursor_row_p = 0;
cafafe0b
GM
14352 }
14353
9a038881 14354 return cursor_row_p;
cafafe0b
GM
14355}
14356
14357
5f5c8ee5
GM
14358/* Construct the glyph row IT->glyph_row in the desired matrix of
14359 IT->w from text at the current position of IT. See dispextern.h
14360 for an overview of struct it. Value is non-zero if
14361 IT->glyph_row displays text, as opposed to a line displaying ZV
14362 only. */
2311178e 14363
5f5c8ee5
GM
14364static int
14365display_line (it)
14366 struct it *it;
14367{
14368 struct glyph_row *row = it->glyph_row;
351b5434
KS
14369 int overlay_arrow_bitmap;
14370 Lisp_Object overlay_arrow_string;
5f5c8ee5
GM
14371
14372 /* We always start displaying at hpos zero even if hscrolled. */
14373 xassert (it->hpos == 0 && it->current_x == 0);
a2889657 14374
5f5c8ee5
GM
14375 /* We must not display in a row that's not a text row. */
14376 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14377 < it->w->desired_matrix->nrows);
12adba34 14378
5f5c8ee5
GM
14379 /* Is IT->w showing the region? */
14380 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12adba34 14381
5f5c8ee5
GM
14382 /* Clear the result glyph row and enable it. */
14383 prepare_desired_row (row);
12adba34 14384
5f5c8ee5 14385 row->y = it->current_y;
29b3ea63 14386 row->start = it->start;
5f5c8ee5
GM
14387 row->continuation_lines_width = it->continuation_lines_width;
14388 row->displays_text_p = 1;
91004049
GM
14389 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14390 it->starts_in_middle_of_char_p = 0;
5f5c8ee5
GM
14391
14392 /* Arrange the overlays nicely for our purposes. Usually, we call
14393 display_line on only one line at a time, in which case this
14394 can't really hurt too much, or we call it on lines which appear
14395 one after another in the buffer, in which case all calls to
14396 recenter_overlay_lists but the first will be pretty cheap. */
14397 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14398
5f5c8ee5
GM
14399 /* Move over display elements that are not visible because we are
14400 hscrolled. This may stop at an x-position < IT->first_visible_x
14401 if the first glyph is partially visible or if we hit a line end. */
14402 if (it->current_x < it->first_visible_x)
14403 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14404 MOVE_TO_POS | MOVE_TO_X);
14405
14406 /* Get the initial row height. This is either the height of the
14407 text hscrolled, if there is any, or zero. */
14408 row->ascent = it->max_ascent;
14409 row->height = it->max_ascent + it->max_descent;
312246d1
GM
14410 row->phys_ascent = it->max_phys_ascent;
14411 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
5f5c8ee5
GM
14412
14413 /* Loop generating characters. The loop is left with IT on the next
14414 character to display. */
14415 while (1)
14416 {
14417 int n_glyphs_before, hpos_before, x_before;
14418 int x, i, nglyphs;
ae26e27d 14419 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
b28cb6ed 14420
5f5c8ee5
GM
14421 /* Retrieve the next thing to display. Value is zero if end of
14422 buffer reached. */
14423 if (!get_next_display_element (it))
14424 {
14425 /* Maybe add a space at the end of this line that is used to
1b335865
GM
14426 display the cursor there under X. Set the charpos of the
14427 first glyph of blank lines not corresponding to any text
14428 to -1. */
6d925726
KS
14429#ifdef HAVE_WINDOW_SYSTEM
14430 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14431 row->exact_window_width_line_p = 1;
14432 else
14433#endif /* HAVE_WINDOW_SYSTEM */
1b335865
GM
14434 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
14435 || row->used[TEXT_AREA] == 0)
a2889657 14436 {
5f5c8ee5
GM
14437 row->glyphs[TEXT_AREA]->charpos = -1;
14438 row->displays_text_p = 0;
14439
2ad551af 14440 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
e6b70fd8
GM
14441 && (!MINI_WINDOW_P (it->w)
14442 || (minibuf_level && EQ (it->window, minibuf_window))))
5f5c8ee5 14443 row->indicate_empty_line_p = 1;
a2889657 14444 }
2311178e 14445
5f5c8ee5
GM
14446 it->continuation_lines_width = 0;
14447 row->ends_at_zv_p = 1;
14448 break;
a2889657 14449 }
a2889657 14450
5f5c8ee5
GM
14451 /* Now, get the metrics of what we want to display. This also
14452 generates glyphs in `row' (which is IT->glyph_row). */
14453 n_glyphs_before = row->used[TEXT_AREA];
14454 x = it->current_x;
e6819faf
GM
14455
14456 /* Remember the line height so far in case the next element doesn't
14457 fit on the line. */
14458 if (!it->truncate_lines_p)
14459 {
14460 ascent = it->max_ascent;
14461 descent = it->max_descent;
14462 phys_ascent = it->max_phys_ascent;
14463 phys_descent = it->max_phys_descent;
14464 }
2311178e 14465
5f5c8ee5 14466 PRODUCE_GLYPHS (it);
a2889657 14467
5f5c8ee5
GM
14468 /* If this display element was in marginal areas, continue with
14469 the next one. */
14470 if (it->area != TEXT_AREA)
a2889657 14471 {
5f5c8ee5
GM
14472 row->ascent = max (row->ascent, it->max_ascent);
14473 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
14474 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14475 row->phys_height = max (row->phys_height,
14476 it->max_phys_ascent + it->max_phys_descent);
cafafe0b 14477 set_iterator_to_next (it, 1);
5f5c8ee5
GM
14478 continue;
14479 }
5936754e 14480
5f5c8ee5
GM
14481 /* Does the display element fit on the line? If we truncate
14482 lines, we should draw past the right edge of the window. If
14483 we don't truncate, we want to stop so that we can display the
14484 continuation glyph before the right margin. If lines are
14485 continued, there are two possible strategies for characters
14486 resulting in more than 1 glyph (e.g. tabs): Display as many
14487 glyphs as possible in this line and leave the rest for the
14488 continuation line, or display the whole element in the next
14489 line. Original redisplay did the former, so we do it also. */
14490 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14491 hpos_before = it->hpos;
14492 x_before = x;
4dcd74e6 14493
4b41cebb 14494 if (/* Not a newline. */
4dcd74e6 14495 nglyphs > 0
202c1b5b 14496 /* Glyphs produced fit entirely in the line. */
4dcd74e6
GM
14497 && it->current_x < it->last_visible_x)
14498 {
37be86f2 14499 it->hpos += nglyphs;
5f5c8ee5
GM
14500 row->ascent = max (row->ascent, it->max_ascent);
14501 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
14502 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14503 row->phys_height = max (row->phys_height,
14504 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
14505 if (it->current_x - it->pixel_width < it->first_visible_x)
14506 row->x = x - it->first_visible_x;
14507 }
14508 else
14509 {
14510 int new_x;
14511 struct glyph *glyph;
2311178e 14512
5f5c8ee5 14513 for (i = 0; i < nglyphs; ++i, x = new_x)
b5bbc9a5 14514 {
5f5c8ee5
GM
14515 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14516 new_x = x + glyph->pixel_width;
14517
14518 if (/* Lines are continued. */
14519 !it->truncate_lines_p
14520 && (/* Glyph doesn't fit on the line. */
14521 new_x > it->last_visible_x
14522 /* Or it fits exactly on a window system frame. */
14523 || (new_x == it->last_visible_x
14524 && FRAME_WINDOW_P (it->f))))
a2889657 14525 {
5f5c8ee5 14526 /* End of a continued line. */
2311178e 14527
5f5c8ee5
GM
14528 if (it->hpos == 0
14529 || (new_x == it->last_visible_x
14530 && FRAME_WINDOW_P (it->f)))
14531 {
e6819faf
GM
14532 /* Current glyph is the only one on the line or
14533 fits exactly on the line. We must continue
14534 the line because we can't draw the cursor
14535 after the glyph. */
5f5c8ee5
GM
14536 row->continued_p = 1;
14537 it->current_x = new_x;
14538 it->continuation_lines_width += new_x;
14539 ++it->hpos;
14540 if (i == nglyphs - 1)
88e6b646
KS
14541 {
14542 set_iterator_to_next (it, 1);
7af0e8d7 14543#ifdef HAVE_WINDOW_SYSTEM
88e6b646
KS
14544 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14545 {
6d925726
KS
14546 if (!get_next_display_element (it))
14547 {
14548 row->exact_window_width_line_p = 1;
14549 it->continuation_lines_width = 0;
14550 row->continued_p = 0;
14551 row->ends_at_zv_p = 1;
14552 }
14553 else if (ITERATOR_AT_END_OF_LINE_P (it))
88e6b646
KS
14554 {
14555 row->continued_p = 0;
14556 row->exact_window_width_line_p = 1;
14557 }
14558 }
7af0e8d7 14559#endif /* HAVE_WINDOW_SYSTEM */
88e6b646 14560 }
5f5c8ee5 14561 }
3b3c4bf0
GM
14562 else if (CHAR_GLYPH_PADDING_P (*glyph)
14563 && !FRAME_WINDOW_P (it->f))
14564 {
14565 /* A padding glyph that doesn't fit on this line.
14566 This means the whole character doesn't fit
14567 on the line. */
14568 row->used[TEXT_AREA] = n_glyphs_before;
2311178e 14569
3b3c4bf0
GM
14570 /* Fill the rest of the row with continuation
14571 glyphs like in 20.x. */
14572 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
14573 < row->glyphs[1 + TEXT_AREA])
14574 produce_special_glyphs (it, IT_CONTINUATION);
2311178e 14575
3b3c4bf0
GM
14576 row->continued_p = 1;
14577 it->current_x = x_before;
14578 it->continuation_lines_width += x_before;
2311178e 14579
3b3c4bf0
GM
14580 /* Restore the height to what it was before the
14581 element not fitting on the line. */
14582 it->max_ascent = ascent;
14583 it->max_descent = descent;
14584 it->max_phys_ascent = phys_ascent;
14585 it->max_phys_descent = phys_descent;
14586 }
0df56f4d
GM
14587 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
14588 {
14589 /* A TAB that extends past the right edge of the
14590 window. This produces a single glyph on
14591 window system frames. We leave the glyph in
14592 this row and let it fill the row, but don't
14593 consume the TAB. */
14594 it->continuation_lines_width += it->last_visible_x;
14595 row->ends_in_middle_of_char_p = 1;
14596 row->continued_p = 1;
14597 glyph->pixel_width = it->last_visible_x - x;
14598 it->starts_in_middle_of_char_p = 1;
14599 }
5f5c8ee5 14600 else
5936754e 14601 {
0df56f4d
GM
14602 /* Something other than a TAB that draws past
14603 the right edge of the window. Restore
14604 positions to values before the element. */
5f5c8ee5 14605 row->used[TEXT_AREA] = n_glyphs_before + i;
2311178e 14606
5f5c8ee5
GM
14607 /* Display continuation glyphs. */
14608 if (!FRAME_WINDOW_P (it->f))
14609 produce_special_glyphs (it, IT_CONTINUATION);
14610 row->continued_p = 1;
653c329b 14611
0df56f4d 14612 it->continuation_lines_width += x;
2311178e 14613
91004049
GM
14614 if (nglyphs > 1 && i > 0)
14615 {
14616 row->ends_in_middle_of_char_p = 1;
14617 it->starts_in_middle_of_char_p = 1;
14618 }
2311178e 14619
e6819faf
GM
14620 /* Restore the height to what it was before the
14621 element not fitting on the line. */
14622 it->max_ascent = ascent;
14623 it->max_descent = descent;
14624 it->max_phys_ascent = phys_ascent;
14625 it->max_phys_descent = phys_descent;
5936754e 14626 }
e6819faf 14627
5f5c8ee5
GM
14628 break;
14629 }
14630 else if (new_x > it->first_visible_x)
14631 {
14632 /* Increment number of glyphs actually displayed. */
14633 ++it->hpos;
2311178e 14634
5f5c8ee5
GM
14635 if (x < it->first_visible_x)
14636 /* Glyph is partially visible, i.e. row starts at
14637 negative X position. */
14638 row->x = x - it->first_visible_x;
14639 }
14640 else
14641 {
14642 /* Glyph is completely off the left margin of the
14643 window. This should not happen because of the
72b8c434
RS
14644 move_it_in_display_line at the start of this
14645 function, unless the text display area of the
14646 window is empty. */
14647 xassert (it->first_visible_x <= it->last_visible_x);
a2889657 14648 }
a2889657 14649 }
2311178e 14650
5f5c8ee5
GM
14651 row->ascent = max (row->ascent, it->max_ascent);
14652 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
14653 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14654 row->phys_height = max (row->phys_height,
14655 it->max_phys_ascent + it->max_phys_descent);
2311178e 14656
5f5c8ee5 14657 /* End of this display line if row is continued. */
6d925726 14658 if (row->continued_p || row->ends_at_zv_p)
5f5c8ee5 14659 break;
a2889657 14660 }
a2889657 14661
88e6b646 14662 at_end_of_line:
5f5c8ee5
GM
14663 /* Is this a line end? If yes, we're also done, after making
14664 sure that a non-default face is extended up to the right
14665 margin of the window. */
14666 if (ITERATOR_AT_END_OF_LINE_P (it))
1c9241f5 14667 {
5f5c8ee5
GM
14668 int used_before = row->used[TEXT_AREA];
14669
e74fb0f7
GM
14670 row->ends_in_newline_from_string_p = STRINGP (it->object);
14671
7af0e8d7 14672#ifdef HAVE_WINDOW_SYSTEM
5f5c8ee5
GM
14673 /* Add a space at the end of the line that is used to
14674 display the cursor there. */
88e6b646
KS
14675 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14676 append_space (it, 0);
7af0e8d7 14677#endif /* HAVE_WINDOW_SYSTEM */
2311178e 14678
5f5c8ee5
GM
14679 /* Extend the face to the end of the line. */
14680 extend_face_to_end_of_line (it);
14681
14682 /* Make sure we have the position. */
14683 if (used_before == 0)
14684 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
2311178e 14685
5f5c8ee5 14686 /* Consume the line end. This skips over invisible lines. */
cafafe0b 14687 set_iterator_to_next (it, 1);
5f5c8ee5
GM
14688 it->continuation_lines_width = 0;
14689 break;
1c9241f5 14690 }
a2889657 14691
2311178e 14692 /* Proceed with next display element. Note that this skips
5f5c8ee5 14693 over lines invisible because of selective display. */
cafafe0b 14694 set_iterator_to_next (it, 1);
b1d1124b 14695
5f5c8ee5
GM
14696 /* If we truncate lines, we are done when the last displayed
14697 glyphs reach past the right margin of the window. */
14698 if (it->truncate_lines_p
14699 && (FRAME_WINDOW_P (it->f)
14700 ? (it->current_x >= it->last_visible_x)
14701 : (it->current_x > it->last_visible_x)))
75d13c64 14702 {
5f5c8ee5
GM
14703 /* Maybe add truncation glyphs. */
14704 if (!FRAME_WINDOW_P (it->f))
14705 {
a98b5ed9 14706 int i, n;
2311178e 14707
a98b5ed9
GM
14708 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14709 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14710 break;
14711
14712 for (n = row->used[TEXT_AREA]; i < n; ++i)
14713 {
14714 row->used[TEXT_AREA] = i;
14715 produce_special_glyphs (it, IT_TRUNCATION);
14716 }
5f5c8ee5 14717 }
7af0e8d7 14718#ifdef HAVE_WINDOW_SYSTEM
88e6b646
KS
14719 else
14720 {
14721 /* Don't truncate if we can overflow newline into fringe. */
14722 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14723 {
6d925726
KS
14724 if (!get_next_display_element (it))
14725 {
14726#ifdef HAVE_WINDOW_SYSTEM
14727 it->continuation_lines_width = 0;
14728 row->ends_at_zv_p = 1;
14729 row->exact_window_width_line_p = 1;
14730 break;
14731#endif /* HAVE_WINDOW_SYSTEM */
14732 }
88e6b646
KS
14733 if (ITERATOR_AT_END_OF_LINE_P (it))
14734 {
14735 row->exact_window_width_line_p = 1;
14736 goto at_end_of_line;
14737 }
14738 }
14739 }
7af0e8d7 14740#endif /* HAVE_WINDOW_SYSTEM */
2311178e 14741
5f5c8ee5
GM
14742 row->truncated_on_right_p = 1;
14743 it->continuation_lines_width = 0;
312246d1 14744 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
14745 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
14746 it->hpos = hpos_before;
14747 it->current_x = x_before;
14748 break;
75d13c64 14749 }
a2889657 14750 }
a2889657 14751
5f5c8ee5
GM
14752 /* If line is not empty and hscrolled, maybe insert truncation glyphs
14753 at the left window margin. */
14754 if (it->first_visible_x
14755 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
14756 {
14757 if (!FRAME_WINDOW_P (it->f))
14758 insert_left_trunc_glyphs (it);
14759 row->truncated_on_left_p = 1;
14760 }
a2889657 14761
5f5c8ee5
GM
14762 /* If the start of this line is the overlay arrow-position, then
14763 mark this glyph row as the one containing the overlay arrow.
14764 This is clearly a mess with variable size fonts. It would be
14765 better to let it be displayed like cursors under X. */
351b5434 14766 if (! overlay_arrow_seen
7d90b587
KS
14767 && (overlay_arrow_string
14768 = overlay_arrow_at_row (it->f, row, &overlay_arrow_bitmap),
351b5434 14769 !NILP (overlay_arrow_string)))
a2889657 14770 {
b46952ae 14771 /* Overlay arrow in window redisplay is a fringe bitmap. */
5f5c8ee5 14772 if (!FRAME_WINDOW_P (it->f))
c4628384 14773 {
351b5434 14774 struct glyph_row *arrow_row
7d90b587 14775 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
5f5c8ee5
GM
14776 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
14777 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
14778 struct glyph *p = row->glyphs[TEXT_AREA];
14779 struct glyph *p2, *end;
14780
14781 /* Copy the arrow glyphs. */
14782 while (glyph < arrow_end)
14783 *p++ = *glyph++;
14784
14785 /* Throw away padding glyphs. */
14786 p2 = p;
14787 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
14788 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
14789 ++p2;
14790 if (p2 > p)
212e4f87 14791 {
5f5c8ee5
GM
14792 while (p2 < end)
14793 *p++ = *p2++;
14794 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
c4628384 14795 }
c4628384 14796 }
2311178e 14797
a2889657 14798 overlay_arrow_seen = 1;
351b5434 14799 it->w->overlay_arrow_bitmap = overlay_arrow_bitmap;
5f5c8ee5 14800 row->overlay_arrow_p = 1;
a2889657
JB
14801 }
14802
5f5c8ee5
GM
14803 /* Compute pixel dimensions of this line. */
14804 compute_line_metrics (it);
14805
14806 /* Remember the position at which this line ends. */
14807 row->end = it->current;
14808
6d925726
KS
14809 /* Save fringe bitmaps in this row. */
14810 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
14811 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
14812 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
14813 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
14814
14815 it->left_user_fringe_bitmap = 0;
14816 it->left_user_fringe_face_id = 0;
14817 it->right_user_fringe_bitmap = 0;
14818 it->right_user_fringe_face_id = 0;
14819
173cbca8 14820 /* Maybe set the cursor. */
5f5c8ee5
GM
14821 if (it->w->cursor.vpos < 0
14822 && PT >= MATRIX_ROW_START_CHARPOS (row)
cafafe0b
GM
14823 && PT <= MATRIX_ROW_END_CHARPOS (row)
14824 && cursor_row_p (it->w, row))
14825 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
5f5c8ee5
GM
14826
14827 /* Highlight trailing whitespace. */
8f897821 14828 if (!NILP (Vshow_trailing_whitespace))
5f5c8ee5
GM
14829 highlight_trailing_whitespace (it->f, it->glyph_row);
14830
14831 /* Prepare for the next line. This line starts horizontally at (X
14832 HPOS) = (0 0). Vertical positions are incremented. As a
14833 convenience for the caller, IT->glyph_row is set to the next
14834 row to be used. */
14835 it->current_x = it->hpos = 0;
14836 it->current_y += row->height;
14837 ++it->vpos;
14838 ++it->glyph_row;
29b3ea63 14839 it->start = it->current;
5f5c8ee5 14840 return row->displays_text_p;
a2889657 14841}
5f5c8ee5
GM
14842
14843
a2889657 14844\f
5f5c8ee5
GM
14845/***********************************************************************
14846 Menu Bar
14847 ***********************************************************************/
14848
14849/* Redisplay the menu bar in the frame for window W.
14850
14851 The menu bar of X frames that don't have X toolkit support is
14852 displayed in a special window W->frame->menu_bar_window.
2311178e 14853
5f5c8ee5
GM
14854 The menu bar of terminal frames is treated specially as far as
14855 glyph matrices are concerned. Menu bar lines are not part of
14856 windows, so the update is done directly on the frame matrix rows
14857 for the menu bar. */
7ce2c095
RS
14858
14859static void
14860display_menu_bar (w)
14861 struct window *w;
14862{
5f5c8ee5
GM
14863 struct frame *f = XFRAME (WINDOW_FRAME (w));
14864 struct it it;
14865 Lisp_Object items;
8351baf2 14866 int i;
7ce2c095 14867
5f5c8ee5 14868 /* Don't do all this for graphical frames. */
dc937613 14869#ifdef HAVE_NTGUI
d129c4c2
KH
14870 if (!NILP (Vwindow_system))
14871 return;
dc937613 14872#endif
488dd4c4 14873#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
d3413a53 14874 if (FRAME_X_P (f))
7ce2c095 14875 return;
5f5c8ee5 14876#endif
e0f712ba 14877#ifdef MAC_OS
1a578e9b
AC
14878 if (FRAME_MAC_P (f))
14879 return;
14880#endif
5f5c8ee5
GM
14881
14882#ifdef USE_X_TOOLKIT
14883 xassert (!FRAME_WINDOW_P (f));
52377a47 14884 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
5f5c8ee5 14885 it.first_visible_x = 0;
da8b7f4f 14886 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
5f5c8ee5
GM
14887#else /* not USE_X_TOOLKIT */
14888 if (FRAME_WINDOW_P (f))
14889 {
14890 /* Menu bar lines are displayed in the desired matrix of the
14891 dummy window menu_bar_window. */
14892 struct window *menu_w;
14893 xassert (WINDOWP (f->menu_bar_window));
14894 menu_w = XWINDOW (f->menu_bar_window);
14895 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
52377a47 14896 MENU_FACE_ID);
5f5c8ee5 14897 it.first_visible_x = 0;
da8b7f4f 14898 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
5f5c8ee5
GM
14899 }
14900 else
14901 {
14902 /* This is a TTY frame, i.e. character hpos/vpos are used as
14903 pixel x/y. */
14904 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
52377a47 14905 MENU_FACE_ID);
5f5c8ee5 14906 it.first_visible_x = 0;
da8b7f4f 14907 it.last_visible_x = FRAME_COLS (f);
5f5c8ee5
GM
14908 }
14909#endif /* not USE_X_TOOLKIT */
14910
1862a24e
MB
14911 if (! mode_line_inverse_video)
14912 /* Force the menu-bar to be displayed in the default face. */
14913 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
14914
5f5c8ee5
GM
14915 /* Clear all rows of the menu bar. */
14916 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
14917 {
14918 struct glyph_row *row = it.glyph_row + i;
14919 clear_glyph_row (row);
14920 row->enabled_p = 1;
14921 row->full_width_p = 1;
14922 }
7ce2c095 14923
5f5c8ee5
GM
14924 /* Display all items of the menu bar. */
14925 items = FRAME_MENU_BAR_ITEMS (it.f);
469937ac 14926 for (i = 0; i < XVECTOR (items)->size; i += 4)
7ce2c095 14927 {
5f5c8ee5
GM
14928 Lisp_Object string;
14929
14930 /* Stop at nil string. */
a61b7058 14931 string = AREF (items, i + 1);
8351baf2
RS
14932 if (NILP (string))
14933 break;
2d66ad19 14934
5f5c8ee5 14935 /* Remember where item was displayed. */
a61b7058 14936 AREF (items, i + 3) = make_number (it.hpos);
7ce2c095 14937
5f5c8ee5
GM
14938 /* Display the item, pad with one space. */
14939 if (it.current_x < it.last_visible_x)
14940 display_string (NULL, string, Qnil, 0, 0, &it,
2051c264 14941 SCHARS (string) + 1, 0, 0, -1);
7ce2c095
RS
14942 }
14943
2d66ad19 14944 /* Fill out the line with spaces. */
5f5c8ee5
GM
14945 if (it.current_x < it.last_visible_x)
14946 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
db6f348c 14947
5f5c8ee5
GM
14948 /* Compute the total height of the lines. */
14949 compute_line_metrics (&it);
7ce2c095 14950}
5f5c8ee5
GM
14951
14952
7ce2c095 14953\f
5f5c8ee5
GM
14954/***********************************************************************
14955 Mode Line
14956 ***********************************************************************/
14957
715e84c9
GM
14958/* Redisplay mode lines in the window tree whose root is WINDOW. If
14959 FORCE is non-zero, redisplay mode lines unconditionally.
14960 Otherwise, redisplay only mode lines that are garbaged. Value is
14961 the number of windows whose mode lines were redisplayed. */
a2889657 14962
715e84c9
GM
14963static int
14964redisplay_mode_lines (window, force)
14965 Lisp_Object window;
14966 int force;
14967{
14968 int nwindows = 0;
2311178e 14969
715e84c9
GM
14970 while (!NILP (window))
14971 {
14972 struct window *w = XWINDOW (window);
2311178e 14973
715e84c9
GM
14974 if (WINDOWP (w->hchild))
14975 nwindows += redisplay_mode_lines (w->hchild, force);
14976 else if (WINDOWP (w->vchild))
14977 nwindows += redisplay_mode_lines (w->vchild, force);
14978 else if (force
14979 || FRAME_GARBAGED_P (XFRAME (w->frame))
14980 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
14981 {
715e84c9
GM
14982 struct text_pos lpoint;
14983 struct buffer *old = current_buffer;
14984
14985 /* Set the window's buffer for the mode line display. */
14986 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14987 set_buffer_internal_1 (XBUFFER (w->buffer));
2311178e 14988
715e84c9
GM
14989 /* Point refers normally to the selected window. For any
14990 other window, set up appropriate value. */
14991 if (!EQ (window, selected_window))
14992 {
14993 struct text_pos pt;
2311178e 14994
715e84c9
GM
14995 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
14996 if (CHARPOS (pt) < BEGV)
14997 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
14998 else if (CHARPOS (pt) > (ZV - 1))
14999 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15000 else
15001 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15002 }
15003
715e84c9
GM
15004 /* Display mode lines. */
15005 clear_glyph_matrix (w->desired_matrix);
15006 if (display_mode_lines (w))
15007 {
15008 ++nwindows;
15009 w->must_be_updated_p = 1;
15010 }
15011
15012 /* Restore old settings. */
715e84c9
GM
15013 set_buffer_internal_1 (old);
15014 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15015 }
15016
15017 window = w->next;
15018 }
15019
15020 return nwindows;
15021}
15022
15023
15024/* Display the mode and/or top line of window W. Value is the number
15025 of mode lines displayed. */
15026
15027static int
5f5c8ee5 15028display_mode_lines (w)
a2889657
JB
15029 struct window *w;
15030{
edd1e654 15031 Lisp_Object old_selected_window, old_selected_frame;
715e84c9 15032 int n = 0;
edd1e654
GM
15033
15034 old_selected_frame = selected_frame;
15035 selected_frame = w->frame;
15036 old_selected_window = selected_window;
15037 XSETWINDOW (selected_window, w);
2311178e 15038
5f5c8ee5 15039 /* These will be set while the mode line specs are processed. */
aa6d10fa 15040 line_number_displayed = 0;
155ef550 15041 w->column_number_displayed = Qnil;
aa6d10fa 15042
5f5c8ee5 15043 if (WINDOW_WANTS_MODELINE_P (w))
715e84c9 15044 {
c96e83bf 15045 struct window *sel_w = XWINDOW (old_selected_window);
f87c0a98 15046
96d2320f 15047 /* Select mode line face based on the real selected window. */
c96e83bf 15048 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
715e84c9
GM
15049 current_buffer->mode_line_format);
15050 ++n;
15051 }
2311178e 15052
045dee35 15053 if (WINDOW_WANTS_HEADER_LINE_P (w))
715e84c9
GM
15054 {
15055 display_mode_line (w, HEADER_LINE_FACE_ID,
15056 current_buffer->header_line_format);
15057 ++n;
15058 }
15059
edd1e654
GM
15060 selected_frame = old_selected_frame;
15061 selected_window = old_selected_window;
715e84c9 15062 return n;
5f5c8ee5 15063}
03b294dc 15064
03b294dc 15065
5f5c8ee5 15066/* Display mode or top line of window W. FACE_ID specifies which line
045dee35 15067 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
04612a64
GM
15068 FORMAT is the mode line format to display. Value is the pixel
15069 height of the mode line displayed. */
03b294dc 15070
04612a64 15071static int
5f5c8ee5
GM
15072display_mode_line (w, face_id, format)
15073 struct window *w;
15074 enum face_id face_id;
15075 Lisp_Object format;
15076{
15077 struct it it;
15078 struct face *face;
03b294dc 15079
5f5c8ee5
GM
15080 init_iterator (&it, w, -1, -1, NULL, face_id);
15081 prepare_desired_row (it.glyph_row);
15082
da06e4d7
KS
15083 it.glyph_row->mode_line_p = 1;
15084
1862a24e
MB
15085 if (! mode_line_inverse_video)
15086 /* Force the mode-line to be displayed in the default face. */
15087 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15088
5f5c8ee5
GM
15089 /* Temporarily make frame's keyboard the current kboard so that
15090 kboard-local variables in the mode_line_format will get the right
15091 values. */
15092 push_frame_kboard (it.f);
c53a1624 15093 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
5f5c8ee5 15094 pop_frame_kboard ();
a2889657 15095
5f5c8ee5
GM
15096 /* Fill up with spaces. */
15097 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
2311178e 15098
5f5c8ee5
GM
15099 compute_line_metrics (&it);
15100 it.glyph_row->full_width_p = 1;
5f5c8ee5
GM
15101 it.glyph_row->continued_p = 0;
15102 it.glyph_row->truncated_on_left_p = 0;
15103 it.glyph_row->truncated_on_right_p = 0;
15104
15105 /* Make a 3D mode-line have a shadow at its right end. */
15106 face = FACE_FROM_ID (it.f, face_id);
15107 extend_face_to_end_of_line (&it);
15108 if (face->box != FACE_NO_BOX)
d7eb09a0 15109 {
5f5c8ee5
GM
15110 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15111 + it.glyph_row->used[TEXT_AREA] - 1);
15112 last->right_box_line_p = 1;
d7eb09a0 15113 }
04612a64
GM
15114
15115 return it.glyph_row->height;
a2889657
JB
15116}
15117
0fcf414f
RS
15118/* Alist that caches the results of :propertize.
15119 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
15120Lisp_Object mode_line_proptrans_alist;
a2889657 15121
fec8f23e
KS
15122/* List of strings making up the mode-line. */
15123Lisp_Object mode_line_string_list;
15124
15125/* Base face property when building propertized mode line string. */
15126static Lisp_Object mode_line_string_face;
15127static Lisp_Object mode_line_string_face_prop;
15128
15129
5f5c8ee5
GM
15130/* Contribute ELT to the mode line for window IT->w. How it
15131 translates into text depends on its data type.
a2889657 15132
5f5c8ee5 15133 IT describes the display environment in which we display, as usual.
a2889657
JB
15134
15135 DEPTH is the depth in recursion. It is used to prevent
15136 infinite recursion here.
15137
5f5c8ee5
GM
15138 FIELD_WIDTH is the number of characters the display of ELT should
15139 occupy in the mode line, and PRECISION is the maximum number of
15140 characters to display from ELT's representation. See
b8523839 15141 display_string for details.
a2889657 15142
c53a1624
RS
15143 Returns the hpos of the end of the text generated by ELT.
15144
15145 PROPS is a property list to add to any string we encounter.
15146
7d0393cf 15147 If RISKY is nonzero, remove (disregard) any properties in any string
93da8435
SM
15148 we encounter, and ignore :eval and :propertize.
15149
15150 If the global variable `frame_title_ptr' is non-NULL, then the output
15151 is passed to `store_frame_title' instead of `display_string'. */
a2889657
JB
15152
15153static int
c53a1624 15154display_mode_element (it, depth, field_width, precision, elt, props, risky)
5f5c8ee5 15155 struct it *it;
a2889657 15156 int depth;
5f5c8ee5 15157 int field_width, precision;
0fcf414f 15158 Lisp_Object elt, props;
c53a1624 15159 int risky;
a2889657 15160{
5f5c8ee5 15161 int n = 0, field, prec;
0fcf414f 15162 int literal = 0;
5f5c8ee5 15163
a2889657 15164 tail_recurse:
1a89be1e
SM
15165 if (depth > 100)
15166 elt = build_string ("*too-deep*");
a2889657
JB
15167
15168 depth++;
15169
0220c518 15170 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
a2889657
JB
15171 {
15172 case Lisp_String:
15173 {
15174 /* A string: output it and check for %-constructs within it. */
5f5c8ee5 15175 unsigned char c;
50f80c2f 15176 const unsigned char *this, *lisp_string;
5f5c8ee5 15177
c53a1624 15178 if (!NILP (props) || risky)
0fcf414f
RS
15179 {
15180 Lisp_Object oprops, aelt;
15181 oprops = Ftext_properties_at (make_number (0), elt);
c53a1624
RS
15182
15183 if (NILP (Fequal (props, oprops)) || risky)
0fcf414f 15184 {
ae02e06a
RS
15185 /* If the starting string has properties,
15186 merge the specified ones onto the existing ones. */
c53a1624 15187 if (! NILP (oprops) && !risky)
ae02e06a
RS
15188 {
15189 Lisp_Object tem;
15190
15191 oprops = Fcopy_sequence (oprops);
15192 tem = props;
15193 while (CONSP (tem))
15194 {
15195 oprops = Fplist_put (oprops, XCAR (tem),
15196 XCAR (XCDR (tem)));
15197 tem = XCDR (XCDR (tem));
15198 }
15199 props = oprops;
15200 }
15201
0fcf414f
RS
15202 aelt = Fassoc (elt, mode_line_proptrans_alist);
15203 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
adb63af1
RS
15204 {
15205 mode_line_proptrans_alist
15206 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15207 elt = XCAR (aelt);
15208 }
0fcf414f
RS
15209 else
15210 {
adb63af1
RS
15211 Lisp_Object tem;
15212
0fcf414f 15213 elt = Fcopy_sequence (elt);
dc3b2c8b
SM
15214 Fset_text_properties (make_number (0), Flength (elt),
15215 props, elt);
adb63af1 15216 /* Add this item to mode_line_proptrans_alist. */
0fcf414f
RS
15217 mode_line_proptrans_alist
15218 = Fcons (Fcons (elt, props),
15219 mode_line_proptrans_alist);
adb63af1
RS
15220 /* Truncate mode_line_proptrans_alist
15221 to at most 50 elements. */
15222 tem = Fnthcdr (make_number (50),
15223 mode_line_proptrans_alist);
15224 if (! NILP (tem))
15225 XSETCDR (tem, Qnil);
0fcf414f
RS
15226 }
15227 }
15228 }
15229
2051c264 15230 this = SDATA (elt);
ae02e06a
RS
15231 lisp_string = this;
15232
0fcf414f
RS
15233 if (literal)
15234 {
15235 prec = precision - n;
15236 if (frame_title_ptr)
2051c264 15237 n += store_frame_title (SDATA (elt), -1, prec);
fec8f23e
KS
15238 else if (!NILP (mode_line_string_list))
15239 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
0fcf414f
RS
15240 else
15241 n += display_string (NULL, elt, Qnil, 0, 0, it,
15242 0, prec, 0, STRING_MULTIBYTE (elt));
15243
15244 break;
15245 }
15246
5f5c8ee5
GM
15247 while ((precision <= 0 || n < precision)
15248 && *this
15249 && (frame_title_ptr
fec8f23e 15250 || !NILP (mode_line_string_list)
5f5c8ee5 15251 || it->current_x < it->last_visible_x))
a2889657 15252 {
50f80c2f 15253 const unsigned char *last = this;
5f5c8ee5
GM
15254
15255 /* Advance to end of string or next format specifier. */
a2889657
JB
15256 while ((c = *this++) != '\0' && c != '%')
15257 ;
2311178e 15258
a2889657
JB
15259 if (this - 1 != last)
15260 {
5f5c8ee5
GM
15261 /* Output to end of string or up to '%'. Field width
15262 is length of string. Don't output more than
15263 PRECISION allows us. */
d26b89b8 15264 --this;
eaaa65b0
GM
15265
15266 prec = chars_in_text (last, this - last);
5f5c8ee5
GM
15267 if (precision > 0 && prec > precision - n)
15268 prec = precision - n;
2311178e 15269
d39b6696 15270 if (frame_title_ptr)
d26b89b8 15271 n += store_frame_title (last, 0, prec);
fec8f23e
KS
15272 else if (!NILP (mode_line_string_list))
15273 {
15274 int bytepos = last - lisp_string;
15275 int charpos = string_byte_to_char (elt, bytepos);
15276 n += store_mode_line_string (NULL,
15277 Fsubstring (elt, make_number (charpos),
15278 make_number (charpos + prec)),
15279 0, 0, 0, Qnil);
15280 }
d39b6696 15281 else
eaaa65b0
GM
15282 {
15283 int bytepos = last - lisp_string;
15284 int charpos = string_byte_to_char (elt, bytepos);
15285 n += display_string (NULL, elt, Qnil, 0, charpos,
ca77144d
GM
15286 it, 0, prec, 0,
15287 STRING_MULTIBYTE (elt));
eaaa65b0 15288 }
a2889657
JB
15289 }
15290 else /* c == '%' */
15291 {
50f80c2f 15292 const unsigned char *percent_position = this;
2311178e 15293
5f5c8ee5
GM
15294 /* Get the specified minimum width. Zero means
15295 don't pad. */
15296 field = 0;
a2889657 15297 while ((c = *this++) >= '0' && c <= '9')
5f5c8ee5 15298 field = field * 10 + c - '0';
a2889657 15299
5f5c8ee5
GM
15300 /* Don't pad beyond the total padding allowed. */
15301 if (field_width - n > 0 && field > field_width - n)
15302 field = field_width - n;
a2889657 15303
5f5c8ee5
GM
15304 /* Note that either PRECISION <= 0 or N < PRECISION. */
15305 prec = precision - n;
2311178e 15306
a2889657 15307 if (c == 'M')
5f5c8ee5 15308 n += display_mode_element (it, depth, field, prec,
c53a1624
RS
15309 Vglobal_mode_string, props,
15310 risky);
a2889657 15311 else if (c != 0)
d39b6696 15312 {
72f62cb5 15313 int multibyte;
ae02e06a
RS
15314 int bytepos, charpos;
15315 unsigned char *spec;
2311178e 15316
ae02e06a
RS
15317 bytepos = percent_position - lisp_string;
15318 charpos = (STRING_MULTIBYTE (elt)
15319 ? string_byte_to_char (elt, bytepos)
15320 : bytepos);
15321
15322 spec
72f62cb5
GM
15323 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15324
d39b6696 15325 if (frame_title_ptr)
5f5c8ee5 15326 n += store_frame_title (spec, field, prec);
fec8f23e
KS
15327 else if (!NILP (mode_line_string_list))
15328 {
15329 int len = strlen (spec);
15330 Lisp_Object tem = make_string (spec, len);
15331 props = Ftext_properties_at (make_number (charpos), elt);
15332 /* Should only keep face property in props */
15333 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15334 }
d39b6696 15335 else
5f5c8ee5 15336 {
ae02e06a 15337 int nglyphs_before, nwritten;
2311178e 15338
72f62cb5 15339 nglyphs_before = it->glyph_row->used[TEXT_AREA];
72f62cb5
GM
15340 nwritten = display_string (spec, Qnil, elt,
15341 charpos, 0, it,
15342 field, prec, 0,
15343 multibyte);
5f5c8ee5
GM
15344
15345 /* Assign to the glyphs written above the
15346 string where the `%x' came from, position
15347 of the `%'. */
15348 if (nwritten > 0)
15349 {
15350 struct glyph *glyph
15351 = (it->glyph_row->glyphs[TEXT_AREA]
15352 + nglyphs_before);
15353 int i;
15354
15355 for (i = 0; i < nwritten; ++i)
15356 {
15357 glyph[i].object = elt;
15358 glyph[i].charpos = charpos;
15359 }
2311178e 15360
5f5c8ee5
GM
15361 n += nwritten;
15362 }
15363 }
d39b6696 15364 }
b8523839
AS
15365 else /* c == 0 */
15366 break;
a2889657
JB
15367 }
15368 }
15369 }
15370 break;
15371
15372 case Lisp_Symbol:
15373 /* A symbol: process the value of the symbol recursively
15374 as if it appeared here directly. Avoid error if symbol void.
15375 Special case: if value of symbol is a string, output the string
15376 literally. */
15377 {
15378 register Lisp_Object tem;
c53a1624
RS
15379
15380 /* If the variable is not marked as risky to set
15381 then its contents are risky to use. */
15382 if (NILP (Fget (elt, Qrisky_local_variable)))
15383 risky = 1;
15384
a2889657 15385 tem = Fboundp (elt);
265a9e55 15386 if (!NILP (tem))
a2889657
JB
15387 {
15388 tem = Fsymbol_value (elt);
15389 /* If value is a string, output that string literally:
15390 don't check for % within it. */
e24c997d 15391 if (STRINGP (tem))
0fcf414f
RS
15392 literal = 1;
15393
15394 if (!EQ (tem, elt))
5f5c8ee5
GM
15395 {
15396 /* Give up right away for nil or t. */
15397 elt = tem;
15398 goto tail_recurse;
15399 }
a2889657
JB
15400 }
15401 }
15402 break;
15403
15404 case Lisp_Cons:
15405 {
15406 register Lisp_Object car, tem;
15407
0fcf414f
RS
15408 /* A cons cell: five distinct cases.
15409 If first element is :eval or :propertize, do something special.
a2889657
JB
15410 If first element is a string or a cons, process all the elements
15411 and effectively concatenate them.
15412 If first element is a negative number, truncate displaying cdr to
15413 at most that many characters. If positive, pad (with spaces)
15414 to at least that many characters.
15415 If first element is a symbol, process the cadr or caddr recursively
15416 according to whether the symbol's value is non-nil or nil. */
9472f927 15417 car = XCAR (elt);
0fcf414f 15418 if (EQ (car, QCeval))
5f5c8ee5
GM
15419 {
15420 /* An element of the form (:eval FORM) means evaluate FORM
15421 and use the result as mode line elements. */
0fcf414f 15422
c53a1624
RS
15423 if (risky)
15424 break;
15425
0fcf414f
RS
15426 if (CONSP (XCDR (elt)))
15427 {
15428 Lisp_Object spec;
15429 spec = safe_eval (XCAR (XCDR (elt)));
15430 n += display_mode_element (it, depth, field_width - n,
c53a1624
RS
15431 precision - n, spec, props,
15432 risky);
0fcf414f
RS
15433 }
15434 }
15435 else if (EQ (car, QCpropertize))
15436 {
c53a1624
RS
15437 /* An element of the form (:propertize ELT PROPS...)
15438 means display ELT but applying properties PROPS. */
15439
15440 if (risky)
15441 break;
15442
0fcf414f 15443 if (CONSP (XCDR (elt)))
c53a1624
RS
15444 n += display_mode_element (it, depth, field_width - n,
15445 precision - n, XCAR (XCDR (elt)),
15446 XCDR (XCDR (elt)), risky);
5f5c8ee5
GM
15447 }
15448 else if (SYMBOLP (car))
a2889657
JB
15449 {
15450 tem = Fboundp (car);
9472f927 15451 elt = XCDR (elt);
e24c997d 15452 if (!CONSP (elt))
a2889657
JB
15453 goto invalid;
15454 /* elt is now the cdr, and we know it is a cons cell.
15455 Use its car if CAR has a non-nil value. */
265a9e55 15456 if (!NILP (tem))
a2889657
JB
15457 {
15458 tem = Fsymbol_value (car);
265a9e55 15459 if (!NILP (tem))
9472f927
GM
15460 {
15461 elt = XCAR (elt);
15462 goto tail_recurse;
15463 }
a2889657
JB
15464 }
15465 /* Symbol's value is nil (or symbol is unbound)
15466 Get the cddr of the original list
15467 and if possible find the caddr and use that. */
9472f927 15468 elt = XCDR (elt);
265a9e55 15469 if (NILP (elt))
a2889657 15470 break;
e24c997d 15471 else if (!CONSP (elt))
a2889657 15472 goto invalid;
9472f927 15473 elt = XCAR (elt);
a2889657
JB
15474 goto tail_recurse;
15475 }
e24c997d 15476 else if (INTEGERP (car))
a2889657
JB
15477 {
15478 register int lim = XINT (car);
9472f927 15479 elt = XCDR (elt);
a2889657 15480 if (lim < 0)
5f5c8ee5
GM
15481 {
15482 /* Negative int means reduce maximum width. */
15483 if (precision <= 0)
15484 precision = -lim;
15485 else
15486 precision = min (precision, -lim);
15487 }
a2889657
JB
15488 else if (lim > 0)
15489 {
15490 /* Padding specified. Don't let it be more than
15491 current maximum. */
5f5c8ee5
GM
15492 if (precision > 0)
15493 lim = min (precision, lim);
15494
a2889657
JB
15495 /* If that's more padding than already wanted, queue it.
15496 But don't reduce padding already specified even if
15497 that is beyond the current truncation point. */
5f5c8ee5 15498 field_width = max (lim, field_width);
a2889657
JB
15499 }
15500 goto tail_recurse;
15501 }
e24c997d 15502 else if (STRINGP (car) || CONSP (car))
a2889657
JB
15503 {
15504 register int limit = 50;
5f5c8ee5
GM
15505 /* Limit is to protect against circular lists. */
15506 while (CONSP (elt)
15507 && --limit > 0
15508 && (precision <= 0 || n < precision))
a2889657 15509 {
5f5c8ee5 15510 n += display_mode_element (it, depth, field_width - n,
c53a1624
RS
15511 precision - n, XCAR (elt),
15512 props, risky);
9472f927 15513 elt = XCDR (elt);
a2889657
JB
15514 }
15515 }
15516 }
15517 break;
15518
15519 default:
15520 invalid:
1a89be1e
SM
15521 elt = build_string ("*invalid*");
15522 goto tail_recurse;
a2889657
JB
15523 }
15524
5f5c8ee5
GM
15525 /* Pad to FIELD_WIDTH. */
15526 if (field_width > 0 && n < field_width)
15527 {
15528 if (frame_title_ptr)
15529 n += store_frame_title ("", field_width - n, 0);
fec8f23e
KS
15530 else if (!NILP (mode_line_string_list))
15531 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
5f5c8ee5
GM
15532 else
15533 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
15534 0, 0, 0);
15535 }
2311178e 15536
5f5c8ee5 15537 return n;
a2889657 15538}
5f5c8ee5 15539
fec8f23e
KS
15540/* Store a mode-line string element in mode_line_string_list.
15541
15542 If STRING is non-null, display that C string. Otherwise, the Lisp
15543 string LISP_STRING is displayed.
15544
15545 FIELD_WIDTH is the minimum number of output glyphs to produce.
15546 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15547 with spaces. FIELD_WIDTH <= 0 means don't pad.
15548
15549 PRECISION is the maximum number of characters to output from
15550 STRING. PRECISION <= 0 means don't truncate the string.
15551
15552 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
15553 properties to the string.
15554
7d0393cf 15555 PROPS are the properties to add to the string.
fec8f23e
KS
15556 The mode_line_string_face face property is always added to the string.
15557 */
15558
15559static int store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
15560 char *string;
15561 Lisp_Object lisp_string;
15562 int copy_string;
15563 int field_width;
15564 int precision;
15565 Lisp_Object props;
15566{
15567 int len;
15568 int n = 0;
15569
15570 if (string != NULL)
15571 {
15572 len = strlen (string);
15573 if (precision > 0 && len > precision)
15574 len = precision;
15575 lisp_string = make_string (string, len);
15576 if (NILP (props))
15577 props = mode_line_string_face_prop;
15578 else if (!NILP (mode_line_string_face))
15579 {
15580 Lisp_Object face = Fplist_get (props, Qface);
15581 props = Fcopy_sequence (props);
15582 if (NILP (face))
15583 face = mode_line_string_face;
15584 else
15585 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15586 props = Fplist_put (props, Qface, face);
15587 }
15588 Fadd_text_properties (make_number (0), make_number (len),
15589 props, lisp_string);
15590 }
7d0393cf 15591 else
fec8f23e 15592 {
c8224325 15593 len = XFASTINT (Flength (lisp_string));
fec8f23e
KS
15594 if (precision > 0 && len > precision)
15595 {
15596 len = precision;
15597 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
15598 precision = -1;
15599 }
15600 if (!NILP (mode_line_string_face))
15601 {
15602 Lisp_Object face;
15603 if (NILP (props))
15604 props = Ftext_properties_at (make_number (0), lisp_string);
15605 face = Fplist_get (props, Qface);
15606 if (NILP (face))
15607 face = mode_line_string_face;
15608 else
15609 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15610 props = Fcons (Qface, Fcons (face, Qnil));
15611 if (copy_string)
15612 lisp_string = Fcopy_sequence (lisp_string);
15613 }
15614 if (!NILP (props))
15615 Fadd_text_properties (make_number (0), make_number (len),
15616 props, lisp_string);
15617 }
15618
15619 if (len > 0)
15620 {
15621 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
7d0393cf 15622 n += len;
fec8f23e
KS
15623 }
15624
15625 if (field_width > len)
15626 {
15627 field_width -= len;
15628 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
15629 if (!NILP (props))
15630 Fadd_text_properties (make_number (0), make_number (field_width),
15631 props, lisp_string);
15632 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
7d0393cf 15633 n += field_width;
fec8f23e
KS
15634 }
15635
15636 return n;
15637}
15638
5f5c8ee5 15639
8143e6ab 15640DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
fec8f23e 15641 0, 3, 0,
8143e6ab
KS
15642 doc: /* Return the mode-line of selected window as a string.
15643First optional arg FORMAT specifies a different format string (see
8b22c65a 15644`mode-line-format' for details) to use. If FORMAT is t, return
8143e6ab 15645the buffer's header-line. Second optional arg WINDOW specifies a
fec8f23e
KS
15646different window to use as the context for the formatting.
15647If third optional arg NO-PROPS is non-nil, string is not propertized. */)
15648 (format, window, no_props)
15649 Lisp_Object format, window, no_props;
8143e6ab
KS
15650{
15651 struct it it;
8143e6ab
KS
15652 int len;
15653 struct window *w;
15654 struct buffer *old_buffer = NULL;
fec8f23e 15655 enum face_id face_id = DEFAULT_FACE_ID;
8143e6ab
KS
15656
15657 if (NILP (window))
15658 window = selected_window;
15659 CHECK_WINDOW (window);
15660 w = XWINDOW (window);
15661 CHECK_BUFFER (w->buffer);
15662
15663 if (XBUFFER (w->buffer) != current_buffer)
15664 {
15665 old_buffer = current_buffer;
15666 set_buffer_internal_1 (XBUFFER (w->buffer));
15667 }
15668
15669 if (NILP (format) || EQ (format, Qt))
fec8f23e
KS
15670 {
15671 face_id = NILP (format)
15672 ? CURRENT_MODE_LINE_FACE_ID (w) :
15673 HEADER_LINE_FACE_ID;
15674 format = NILP (format)
15675 ? current_buffer->mode_line_format
15676 : current_buffer->header_line_format;
15677 }
15678
15679 init_iterator (&it, w, -1, -1, NULL, face_id);
8143e6ab 15680
fec8f23e
KS
15681 if (NILP (no_props))
15682 {
15683 mode_line_string_face =
15684 (face_id == MODE_LINE_FACE_ID ? Qmode_line :
15685 face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive :
15686 face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
8143e6ab 15687
fec8f23e
KS
15688 mode_line_string_face_prop =
15689 NILP (mode_line_string_face) ? Qnil :
15690 Fcons (Qface, Fcons (mode_line_string_face, Qnil));
15691
15692 /* We need a dummy last element in mode_line_string_list to
15693 indicate we are building the propertized mode-line string.
15694 Using mode_line_string_face_prop here GC protects it. */
7d0393cf 15695 mode_line_string_list =
fec8f23e
KS
15696 Fcons (mode_line_string_face_prop, Qnil);
15697 frame_title_ptr = NULL;
15698 }
15699 else
15700 {
15701 mode_line_string_face_prop = Qnil;
15702 mode_line_string_list = Qnil;
15703 frame_title_ptr = frame_title_buf;
15704 }
8143e6ab
KS
15705
15706 push_frame_kboard (it.f);
15707 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15708 pop_frame_kboard ();
15709
15710 if (old_buffer)
15711 set_buffer_internal_1 (old_buffer);
15712
fec8f23e
KS
15713 if (NILP (no_props))
15714 {
15715 Lisp_Object str;
15716 mode_line_string_list = Fnreverse (mode_line_string_list);
15717 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
15718 make_string ("", 0));
15719 mode_line_string_face_prop = Qnil;
15720 mode_line_string_list = Qnil;
15721 return str;
15722 }
15723
8143e6ab
KS
15724 len = frame_title_ptr - frame_title_buf;
15725 if (len > 0 && frame_title_ptr[-1] == '-')
15726 {
15727 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
15728 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
15729 ;
15730 frame_title_ptr += 3; /* restore last non-dash + two dashes */
15731 if (len > frame_title_ptr - frame_title_buf)
15732 len = frame_title_ptr - frame_title_buf;
15733 }
15734
15735 frame_title_ptr = NULL;
15736 return make_string (frame_title_buf, len);
15737}
15738
766525bc
RS
15739/* Write a null-terminated, right justified decimal representation of
15740 the positive integer D to BUF using a minimal field width WIDTH. */
15741
15742static void
15743pint2str (buf, width, d)
15744 register char *buf;
15745 register int width;
15746 register int d;
15747{
15748 register char *p = buf;
2311178e 15749
766525bc 15750 if (d <= 0)
5f5c8ee5 15751 *p++ = '0';
766525bc 15752 else
5f5c8ee5 15753 {
766525bc 15754 while (d > 0)
5f5c8ee5 15755 {
766525bc
RS
15756 *p++ = d % 10 + '0';
15757 d /= 10;
5f5c8ee5
GM
15758 }
15759 }
2311178e 15760
5f5c8ee5
GM
15761 for (width -= (int) (p - buf); width > 0; --width)
15762 *p++ = ' ';
766525bc
RS
15763 *p-- = '\0';
15764 while (p > buf)
5f5c8ee5 15765 {
766525bc
RS
15766 d = *buf;
15767 *buf++ = *p;
15768 *p-- = d;
5f5c8ee5 15769 }
766525bc
RS
15770}
15771
525b8b09
LK
15772/* Write a null-terminated, right justified decimal and "human
15773 readable" representation of the nonnegative integer D to BUF using
15774 a minimal field width WIDTH. D should be smaller than 999.5e24. */
15775
15776static const char power_letter[] =
15777 {
15778 0, /* not used */
15779 'k', /* kilo */
15780 'M', /* mega */
15781 'G', /* giga */
15782 'T', /* tera */
15783 'P', /* peta */
15784 'E', /* exa */
15785 'Z', /* zetta */
15786 'Y' /* yotta */
15787 };
15788
15789static void
15790pint2hrstr (buf, width, d)
15791 char *buf;
15792 int width;
15793 int d;
15794{
15795 /* We aim to represent the nonnegative integer D as
15796 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
15797 int quotient = d;
15798 int remainder = 0;
15799 /* -1 means: do not use TENTHS. */
15800 int tenths = -1;
15801 int exponent = 0;
15802
15803 /* Length of QUOTIENT.TENTHS as a string. */
15804 int length;
15805
15806 char * psuffix;
15807 char * p;
15808
15809 if (1000 <= quotient)
15810 {
15811 /* Scale to the appropriate EXPONENT. */
15812 do
15813 {
15814 remainder = quotient % 1000;
15815 quotient /= 1000;
15816 exponent++;
15817 }
15818 while (1000 <= quotient);
15819
15820 /* Round to nearest and decide whether to use TENTHS or not. */
15821 if (quotient <= 9)
15822 {
15823 tenths = remainder / 100;
15824 if (50 <= remainder % 100)
15825 if (tenths < 9)
15826 tenths++;
15827 else
15828 {
15829 quotient++;
15830 if (quotient == 10)
15831 tenths = -1;
15832 else
15833 tenths = 0;
15834 }
15835 }
15836 else
15837 if (500 <= remainder)
15838 if (quotient < 999)
15839 quotient++;
15840 else
15841 {
15842 quotient = 1;
15843 exponent++;
15844 tenths = 0;
15845 }
15846 }
15847
15848 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
15849 if (tenths == -1 && quotient <= 99)
15850 if (quotient <= 9)
15851 length = 1;
15852 else
15853 length = 2;
15854 else
15855 length = 3;
15856 p = psuffix = buf + max (width, length);
15857
15858 /* Print EXPONENT. */
15859 if (exponent)
15860 *psuffix++ = power_letter[exponent];
15861 *psuffix = '\0';
15862
15863 /* Print TENTHS. */
15864 if (tenths >= 0)
15865 {
15866 *--p = '0' + tenths;
15867 *--p = '.';
15868 }
15869
15870 /* Print QUOTIENT. */
15871 do
15872 {
15873 int digit = quotient % 10;
15874 *--p = '0' + digit;
15875 }
15876 while ((quotient /= 10) != 0);
15877
15878 /* Print leading spaces. */
15879 while (buf < p)
15880 *--p = ' ';
15881}
15882
5f5c8ee5 15883/* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
1c9241f5
KH
15884 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
15885 type of CODING_SYSTEM. Return updated pointer into BUF. */
15886
6693a99a 15887static unsigned char invalid_eol_type[] = "(*invalid*)";
d24715e8 15888
1c9241f5
KH
15889static char *
15890decode_mode_spec_coding (coding_system, buf, eol_flag)
15891 Lisp_Object coding_system;
15892 register char *buf;
15893 int eol_flag;
15894{
1e1078d6 15895 Lisp_Object val;
916848d8 15896 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
50f80c2f 15897 const unsigned char *eol_str;
302f2b38
EZ
15898 int eol_str_len;
15899 /* The EOL conversion we are using. */
15900 Lisp_Object eoltype;
1e1078d6 15901
4a09dee0 15902 val = Fget (coding_system, Qcoding_system);
085536c2 15903 eoltype = Qnil;
1c9241f5 15904
4a09dee0 15905 if (!VECTORP (val)) /* Not yet decided. */
1c9241f5 15906 {
916848d8
RS
15907 if (multibyte)
15908 *buf++ = '-';
21e989e3 15909 if (eol_flag)
302f2b38 15910 eoltype = eol_mnemonic_undecided;
1e1078d6 15911 /* Don't mention EOL conversion if it isn't decided. */
1c9241f5
KH
15912 }
15913 else
15914 {
1e1078d6
RS
15915 Lisp_Object eolvalue;
15916
15917 eolvalue = Fget (coding_system, Qeol_type);
15918
916848d8 15919 if (multibyte)
a61b7058 15920 *buf++ = XFASTINT (AREF (val, 1));
916848d8 15921
1c9241f5
KH
15922 if (eol_flag)
15923 {
1e1078d6
RS
15924 /* The EOL conversion that is normal on this system. */
15925
15926 if (NILP (eolvalue)) /* Not yet decided. */
15927 eoltype = eol_mnemonic_undecided;
15928 else if (VECTORP (eolvalue)) /* Not yet decided. */
15929 eoltype = eol_mnemonic_undecided;
15930 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
15931 eoltype = (XFASTINT (eolvalue) == 0
15932 ? eol_mnemonic_unix
15933 : (XFASTINT (eolvalue) == 1
15934 ? eol_mnemonic_dos : eol_mnemonic_mac));
302f2b38
EZ
15935 }
15936 }
2311178e 15937
302f2b38
EZ
15938 if (eol_flag)
15939 {
15940 /* Mention the EOL conversion if it is not the usual one. */
15941 if (STRINGP (eoltype))
15942 {
2051c264
GM
15943 eol_str = SDATA (eoltype);
15944 eol_str_len = SBYTES (eoltype);
302f2b38 15945 }
f30b3499
KH
15946 else if (INTEGERP (eoltype)
15947 && CHAR_VALID_P (XINT (eoltype), 0))
15948 {
50f80c2f
KR
15949 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
15950 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
15951 eol_str = tmp;
f30b3499 15952 }
302f2b38
EZ
15953 else
15954 {
15955 eol_str = invalid_eol_type;
15956 eol_str_len = sizeof (invalid_eol_type) - 1;
1c9241f5 15957 }
f30b3499 15958 bcopy (eol_str, buf, eol_str_len);
302f2b38 15959 buf += eol_str_len;
1c9241f5 15960 }
302f2b38 15961
1c9241f5
KH
15962 return buf;
15963}
15964
a2889657 15965/* Return a string for the output of a mode line %-spec for window W,
5f5c8ee5
GM
15966 generated by character C. PRECISION >= 0 means don't return a
15967 string longer than that value. FIELD_WIDTH > 0 means pad the
72f62cb5
GM
15968 string returned with spaces to that value. Return 1 in *MULTIBYTE
15969 if the result is multibyte text. */
a2889657 15970
11e82b76
JB
15971static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
15972
a2889657 15973static char *
72f62cb5 15974decode_mode_spec (w, c, field_width, precision, multibyte)
a2889657 15975 struct window *w;
68c45bf0 15976 register int c;
5f5c8ee5 15977 int field_width, precision;
72f62cb5 15978 int *multibyte;
a2889657 15979{
0b67772d 15980 Lisp_Object obj;
5f5c8ee5
GM
15981 struct frame *f = XFRAME (WINDOW_FRAME (w));
15982 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
d39b6696 15983 struct buffer *b = XBUFFER (w->buffer);
a2889657 15984
0b67772d 15985 obj = Qnil;
72f62cb5 15986 *multibyte = 0;
a2889657
JB
15987
15988 switch (c)
15989 {
1af9f229
RS
15990 case '*':
15991 if (!NILP (b->read_only))
15992 return "%";
15993 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
15994 return "*";
15995 return "-";
15996
15997 case '+':
15998 /* This differs from %* only for a modified read-only buffer. */
15999 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16000 return "*";
16001 if (!NILP (b->read_only))
16002 return "%";
16003 return "-";
16004
16005 case '&':
16006 /* This differs from %* in ignoring read-only-ness. */
16007 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16008 return "*";
16009 return "-";
16010
16011 case '%':
16012 return "%";
16013
2311178e 16014 case '[':
1af9f229
RS
16015 {
16016 int i;
16017 char *p;
16018
16019 if (command_loop_level > 5)
16020 return "[[[... ";
16021 p = decode_mode_spec_buf;
16022 for (i = 0; i < command_loop_level; i++)
16023 *p++ = '[';
16024 *p = 0;
16025 return decode_mode_spec_buf;
16026 }
16027
2311178e 16028 case ']':
1af9f229
RS
16029 {
16030 int i;
16031 char *p;
16032
16033 if (command_loop_level > 5)
16034 return " ...]]]";
16035 p = decode_mode_spec_buf;
16036 for (i = 0; i < command_loop_level; i++)
16037 *p++ = ']';
16038 *p = 0;
16039 return decode_mode_spec_buf;
16040 }
16041
16042 case '-':
16043 {
1af9f229 16044 register int i;
5f5c8ee5
GM
16045
16046 /* Let lots_of_dashes be a string of infinite length. */
fec8f23e
KS
16047 if (!NILP (mode_line_string_list))
16048 return "--";
5f5c8ee5
GM
16049 if (field_width <= 0
16050 || field_width > sizeof (lots_of_dashes))
1af9f229 16051 {
5f5c8ee5
GM
16052 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16053 decode_mode_spec_buf[i] = '-';
16054 decode_mode_spec_buf[i] = '\0';
16055 return decode_mode_spec_buf;
1af9f229 16056 }
5f5c8ee5
GM
16057 else
16058 return lots_of_dashes;
1af9f229
RS
16059 }
16060
2311178e 16061 case 'b':
d39b6696 16062 obj = b->name;
a2889657
JB
16063 break;
16064
1af9f229
RS
16065 case 'c':
16066 {
2311178e 16067 int col = (int) current_column (); /* iftc */
ac90c44f 16068 w->column_number_displayed = make_number (col);
5f5c8ee5 16069 pint2str (decode_mode_spec_buf, field_width, col);
1af9f229
RS
16070 return decode_mode_spec_buf;
16071 }
16072
16073 case 'F':
16074 /* %F displays the frame name. */
5f5c8ee5 16075 if (!NILP (f->title))
2051c264 16076 return (char *) SDATA (f->title);
fd8ff63d 16077 if (f->explicit_name || ! FRAME_WINDOW_P (f))
2051c264 16078 return (char *) SDATA (f->name);
9c6da96f 16079 return "Emacs";
1af9f229 16080
2311178e 16081 case 'f':
d39b6696 16082 obj = b->filename;
a2889657
JB
16083 break;
16084
525b8b09
LK
16085 case 'i':
16086 {
16087 int size = ZV - BEGV;
16088 pint2str (decode_mode_spec_buf, field_width, size);
16089 return decode_mode_spec_buf;
16090 }
16091
16092 case 'I':
16093 {
16094 int size = ZV - BEGV;
16095 pint2hrstr (decode_mode_spec_buf, field_width, size);
16096 return decode_mode_spec_buf;
16097 }
16098
aa6d10fa
RS
16099 case 'l':
16100 {
12adba34
RS
16101 int startpos = XMARKER (w->start)->charpos;
16102 int startpos_byte = marker_byte_position (w->start);
16103 int line, linepos, linepos_byte, topline;
aa6d10fa 16104 int nlines, junk;
da8b7f4f 16105 int height = WINDOW_TOTAL_LINES (w);
aa6d10fa 16106
2311178e 16107 /* If we decided that this buffer isn't suitable for line numbers,
aa6d10fa
RS
16108 don't forget that too fast. */
16109 if (EQ (w->base_line_pos, w->buffer))
766525bc 16110 goto no_value;
5300fd39
RS
16111 /* But do forget it, if the window shows a different buffer now. */
16112 else if (BUFFERP (w->base_line_pos))
16113 w->base_line_pos = Qnil;
aa6d10fa
RS
16114
16115 /* If the buffer is very big, don't waste time. */
37d034d3 16116 if (INTEGERP (Vline_number_display_limit)
090703f4 16117 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
aa6d10fa
RS
16118 {
16119 w->base_line_pos = Qnil;
16120 w->base_line_number = Qnil;
766525bc 16121 goto no_value;
aa6d10fa
RS
16122 }
16123
16124 if (!NILP (w->base_line_number)
16125 && !NILP (w->base_line_pos)
12adba34 16126 && XFASTINT (w->base_line_pos) <= startpos)
aa6d10fa
RS
16127 {
16128 line = XFASTINT (w->base_line_number);
16129 linepos = XFASTINT (w->base_line_pos);
12adba34 16130 linepos_byte = buf_charpos_to_bytepos (b, linepos);
aa6d10fa
RS
16131 }
16132 else
16133 {
16134 line = 1;
d39b6696 16135 linepos = BUF_BEGV (b);
12adba34 16136 linepos_byte = BUF_BEGV_BYTE (b);
aa6d10fa
RS
16137 }
16138
16139 /* Count lines from base line to window start position. */
12adba34
RS
16140 nlines = display_count_lines (linepos, linepos_byte,
16141 startpos_byte,
16142 startpos, &junk);
aa6d10fa
RS
16143
16144 topline = nlines + line;
16145
16146 /* Determine a new base line, if the old one is too close
16147 or too far away, or if we did not have one.
16148 "Too close" means it's plausible a scroll-down would
16149 go back past it. */
d39b6696 16150 if (startpos == BUF_BEGV (b))
aa6d10fa 16151 {
ac90c44f
GM
16152 w->base_line_number = make_number (topline);
16153 w->base_line_pos = make_number (BUF_BEGV (b));
aa6d10fa
RS
16154 }
16155 else if (nlines < height + 25 || nlines > height * 3 + 50
d39b6696 16156 || linepos == BUF_BEGV (b))
aa6d10fa 16157 {
d39b6696 16158 int limit = BUF_BEGV (b);
12adba34 16159 int limit_byte = BUF_BEGV_BYTE (b);
aa6d10fa 16160 int position;
5d121aec 16161 int distance = (height * 2 + 30) * line_number_display_limit_width;
aa6d10fa
RS
16162
16163 if (startpos - distance > limit)
12adba34
RS
16164 {
16165 limit = startpos - distance;
16166 limit_byte = CHAR_TO_BYTE (limit);
16167 }
aa6d10fa 16168
12adba34
RS
16169 nlines = display_count_lines (startpos, startpos_byte,
16170 limit_byte,
16171 - (height * 2 + 30),
aa6d10fa 16172 &position);
2311178e 16173 /* If we couldn't find the lines we wanted within
5d121aec 16174 line_number_display_limit_width chars per line,
aa6d10fa 16175 give up on line numbers for this window. */
12adba34 16176 if (position == limit_byte && limit == startpos - distance)
aa6d10fa
RS
16177 {
16178 w->base_line_pos = w->buffer;
16179 w->base_line_number = Qnil;
766525bc 16180 goto no_value;
aa6d10fa
RS
16181 }
16182
ac90c44f
GM
16183 w->base_line_number = make_number (topline - nlines);
16184 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
aa6d10fa
RS
16185 }
16186
16187 /* Now count lines from the start pos to point. */
12adba34
RS
16188 nlines = display_count_lines (startpos, startpos_byte,
16189 PT_BYTE, PT, &junk);
aa6d10fa
RS
16190
16191 /* Record that we did display the line number. */
16192 line_number_displayed = 1;
16193
16194 /* Make the string to show. */
5f5c8ee5 16195 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
aa6d10fa 16196 return decode_mode_spec_buf;
766525bc
RS
16197 no_value:
16198 {
16199 char* p = decode_mode_spec_buf;
5f5c8ee5
GM
16200 int pad = field_width - 2;
16201 while (pad-- > 0)
16202 *p++ = ' ';
16203 *p++ = '?';
b357b9d4
KR
16204 *p++ = '?';
16205 *p = '\0';
766525bc
RS
16206 return decode_mode_spec_buf;
16207 }
aa6d10fa
RS
16208 }
16209 break;
16210
2311178e 16211 case 'm':
d39b6696 16212 obj = b->mode_name;
a2889657
JB
16213 break;
16214
16215 case 'n':
d39b6696 16216 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
a2889657
JB
16217 return " Narrow";
16218 break;
16219
a2889657
JB
16220 case 'p':
16221 {
16222 int pos = marker_position (w->start);
d39b6696 16223 int total = BUF_ZV (b) - BUF_BEGV (b);
a2889657 16224
d39b6696 16225 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
a2889657 16226 {
d39b6696 16227 if (pos <= BUF_BEGV (b))
a2889657
JB
16228 return "All";
16229 else
16230 return "Bottom";
16231 }
d39b6696 16232 else if (pos <= BUF_BEGV (b))
a2889657
JB
16233 return "Top";
16234 else
16235 {
3c7d31b9
RS
16236 if (total > 1000000)
16237 /* Do it differently for a large value, to avoid overflow. */
16238 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16239 else
16240 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
a2889657
JB
16241 /* We can't normally display a 3-digit number,
16242 so get us a 2-digit number that is close. */
16243 if (total == 100)
16244 total = 99;
16245 sprintf (decode_mode_spec_buf, "%2d%%", total);
16246 return decode_mode_spec_buf;
16247 }
16248 }
16249
8ffcb79f
RS
16250 /* Display percentage of size above the bottom of the screen. */
16251 case 'P':
16252 {
16253 int toppos = marker_position (w->start);
d39b6696
KH
16254 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16255 int total = BUF_ZV (b) - BUF_BEGV (b);
8ffcb79f 16256
d39b6696 16257 if (botpos >= BUF_ZV (b))
8ffcb79f 16258 {
d39b6696 16259 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
16260 return "All";
16261 else
16262 return "Bottom";
16263 }
16264 else
16265 {
3c7d31b9
RS
16266 if (total > 1000000)
16267 /* Do it differently for a large value, to avoid overflow. */
16268 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16269 else
16270 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
8ffcb79f
RS
16271 /* We can't normally display a 3-digit number,
16272 so get us a 2-digit number that is close. */
16273 if (total == 100)
16274 total = 99;
d39b6696 16275 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
16276 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
16277 else
16278 sprintf (decode_mode_spec_buf, "%2d%%", total);
16279 return decode_mode_spec_buf;
16280 }
16281 }
16282
1af9f229
RS
16283 case 's':
16284 /* status of process */
16285 obj = Fget_buffer_process (w->buffer);
16286 if (NILP (obj))
16287 return "no process";
16288#ifdef subprocesses
16289 obj = Fsymbol_name (Fprocess_status (obj));
16290#endif
16291 break;
d39b6696 16292
1af9f229
RS
16293 case 't': /* indicate TEXT or BINARY */
16294#ifdef MODE_LINE_BINARY_TEXT
16295 return MODE_LINE_BINARY_TEXT (b);
16296#else
16297 return "T";
16298#endif
1c9241f5
KH
16299
16300 case 'z':
16301 /* coding-system (not including end-of-line format) */
16302 case 'Z':
16303 /* coding-system (including end-of-line type) */
16304 {
16305 int eol_flag = (c == 'Z');
539b4d41 16306 char *p = decode_mode_spec_buf;
1c9241f5 16307
d30e754b 16308 if (! FRAME_WINDOW_P (f))
1c9241f5 16309 {
11c52c4f
RS
16310 /* No need to mention EOL here--the terminal never needs
16311 to do EOL conversion. */
16312 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
16313 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
1c9241f5 16314 }
f13c925f 16315 p = decode_mode_spec_coding (b->buffer_file_coding_system,
539b4d41 16316 p, eol_flag);
f13c925f 16317
11c52c4f 16318#if 0 /* This proves to be annoying; I think we can do without. -- rms. */
1c9241f5
KH
16319#ifdef subprocesses
16320 obj = Fget_buffer_process (Fcurrent_buffer ());
16321 if (PROCESSP (obj))
16322 {
16323 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16324 p, eol_flag);
16325 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16326 p, eol_flag);
16327 }
16328#endif /* subprocesses */
11c52c4f 16329#endif /* 0 */
1c9241f5
KH
16330 *p = 0;
16331 return decode_mode_spec_buf;
16332 }
a2889657 16333 }
d39b6696 16334
e24c997d 16335 if (STRINGP (obj))
72f62cb5
GM
16336 {
16337 *multibyte = STRING_MULTIBYTE (obj);
2051c264 16338 return (char *) SDATA (obj);
72f62cb5 16339 }
a2889657
JB
16340 else
16341 return "";
16342}
5f5c8ee5
GM
16343
16344
12adba34
RS
16345/* Count up to COUNT lines starting from START / START_BYTE.
16346 But don't go beyond LIMIT_BYTE.
16347 Return the number of lines thus found (always nonnegative).
59b49f63 16348
12adba34 16349 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
59b49f63
RS
16350
16351static int
12adba34
RS
16352display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16353 int start, start_byte, limit_byte, count;
16354 int *byte_pos_ptr;
59b49f63 16355{
59b49f63
RS
16356 register unsigned char *cursor;
16357 unsigned char *base;
16358
16359 register int ceiling;
16360 register unsigned char *ceiling_addr;
12adba34 16361 int orig_count = count;
59b49f63
RS
16362
16363 /* If we are not in selective display mode,
16364 check only for newlines. */
12adba34
RS
16365 int selective_display = (!NILP (current_buffer->selective_display)
16366 && !INTEGERP (current_buffer->selective_display));
59b49f63
RS
16367
16368 if (count > 0)
12adba34
RS
16369 {
16370 while (start_byte < limit_byte)
16371 {
16372 ceiling = BUFFER_CEILING_OF (start_byte);
16373 ceiling = min (limit_byte - 1, ceiling);
16374 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16375 base = (cursor = BYTE_POS_ADDR (start_byte));
16376 while (1)
16377 {
16378 if (selective_display)
16379 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16380 ;
16381 else
16382 while (*cursor != '\n' && ++cursor != ceiling_addr)
16383 ;
16384
16385 if (cursor != ceiling_addr)
16386 {
16387 if (--count == 0)
16388 {
16389 start_byte += cursor - base + 1;
16390 *byte_pos_ptr = start_byte;
16391 return orig_count;
16392 }
16393 else
16394 if (++cursor == ceiling_addr)
16395 break;
16396 }
16397 else
16398 break;
16399 }
16400 start_byte += cursor - base;
16401 }
16402 }
59b49f63
RS
16403 else
16404 {
12adba34
RS
16405 while (start_byte > limit_byte)
16406 {
16407 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16408 ceiling = max (limit_byte, ceiling);
16409 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16410 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
59b49f63
RS
16411 while (1)
16412 {
12adba34
RS
16413 if (selective_display)
16414 while (--cursor != ceiling_addr
16415 && *cursor != '\n' && *cursor != 015)
16416 ;
16417 else
16418 while (--cursor != ceiling_addr && *cursor != '\n')
16419 ;
16420
59b49f63
RS
16421 if (cursor != ceiling_addr)
16422 {
16423 if (++count == 0)
16424 {
12adba34
RS
16425 start_byte += cursor - base + 1;
16426 *byte_pos_ptr = start_byte;
16427 /* When scanning backwards, we should
16428 not count the newline posterior to which we stop. */
16429 return - orig_count - 1;
59b49f63
RS
16430 }
16431 }
16432 else
16433 break;
16434 }
12adba34
RS
16435 /* Here we add 1 to compensate for the last decrement
16436 of CURSOR, which took it past the valid range. */
16437 start_byte += cursor - base + 1;
59b49f63
RS
16438 }
16439 }
16440
12adba34 16441 *byte_pos_ptr = limit_byte;
aa6d10fa 16442
12adba34
RS
16443 if (count < 0)
16444 return - orig_count + count;
16445 return orig_count - count;
aa6d10fa 16446
12adba34 16447}
a2889657 16448
a2889657 16449
5f5c8ee5
GM
16450\f
16451/***********************************************************************
16452 Displaying strings
16453 ***********************************************************************/
278feba9 16454
5f5c8ee5 16455/* Display a NUL-terminated string, starting with index START.
a3788d53 16456
5f5c8ee5
GM
16457 If STRING is non-null, display that C string. Otherwise, the Lisp
16458 string LISP_STRING is displayed.
a2889657 16459
5f5c8ee5
GM
16460 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16461 FACE_STRING. Display STRING or LISP_STRING with the face at
16462 FACE_STRING_POS in FACE_STRING:
a2889657 16463
5f5c8ee5
GM
16464 Display the string in the environment given by IT, but use the
16465 standard display table, temporarily.
a3788d53 16466
5f5c8ee5
GM
16467 FIELD_WIDTH is the minimum number of output glyphs to produce.
16468 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16469 with spaces. If STRING has more characters, more than FIELD_WIDTH
16470 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
2311178e 16471
5f5c8ee5
GM
16472 PRECISION is the maximum number of characters to output from
16473 STRING. PRECISION < 0 means don't truncate the string.
a2889657 16474
5f5c8ee5 16475 This is roughly equivalent to printf format specifiers:
a2889657 16476
5f5c8ee5
GM
16477 FIELD_WIDTH PRECISION PRINTF
16478 ----------------------------------------
16479 -1 -1 %s
16480 -1 10 %.10s
16481 10 -1 %10s
16482 20 10 %20.10s
a2889657 16483
5f5c8ee5
GM
16484 MULTIBYTE zero means do not display multibyte chars, > 0 means do
16485 display them, and < 0 means obey the current buffer's value of
16486 enable_multibyte_characters.
278feba9 16487
5f5c8ee5 16488 Value is the number of glyphs produced. */
b1d1124b 16489
5f5c8ee5
GM
16490static int
16491display_string (string, lisp_string, face_string, face_string_pos,
16492 start, it, field_width, precision, max_x, multibyte)
16493 unsigned char *string;
16494 Lisp_Object lisp_string;
68c45bf0
PE
16495 Lisp_Object face_string;
16496 int face_string_pos;
5f5c8ee5
GM
16497 int start;
16498 struct it *it;
16499 int field_width, precision, max_x;
16500 int multibyte;
16501{
16502 int hpos_at_start = it->hpos;
16503 int saved_face_id = it->face_id;
16504 struct glyph_row *row = it->glyph_row;
16505
16506 /* Initialize the iterator IT for iteration over STRING beginning
e719f5ae 16507 with index START. */
5f5c8ee5
GM
16508 reseat_to_string (it, string, lisp_string, start,
16509 precision, field_width, multibyte);
16510
16511 /* If displaying STRING, set up the face of the iterator
16512 from LISP_STRING, if that's given. */
16513 if (STRINGP (face_string))
16514 {
16515 int endptr;
16516 struct face *face;
2311178e 16517
5f5c8ee5
GM
16518 it->face_id
16519 = face_at_string_position (it->w, face_string, face_string_pos,
16520 0, it->region_beg_charpos,
16521 it->region_end_charpos,
5de7c6f2 16522 &endptr, it->base_face_id, 0);
5f5c8ee5
GM
16523 face = FACE_FROM_ID (it->f, it->face_id);
16524 it->face_box_p = face->box != FACE_NO_BOX;
b1d1124b 16525 }
a2889657 16526
5f5c8ee5
GM
16527 /* Set max_x to the maximum allowed X position. Don't let it go
16528 beyond the right edge of the window. */
16529 if (max_x <= 0)
16530 max_x = it->last_visible_x;
16531 else
16532 max_x = min (max_x, it->last_visible_x);
efc63ef0 16533
5f5c8ee5
GM
16534 /* Skip over display elements that are not visible. because IT->w is
16535 hscrolled. */
16536 if (it->current_x < it->first_visible_x)
16537 move_it_in_display_line_to (it, 100000, it->first_visible_x,
16538 MOVE_TO_POS | MOVE_TO_X);
a2889657 16539
5f5c8ee5
GM
16540 row->ascent = it->max_ascent;
16541 row->height = it->max_ascent + it->max_descent;
312246d1
GM
16542 row->phys_ascent = it->max_phys_ascent;
16543 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
1c9241f5 16544
5f5c8ee5
GM
16545 /* This condition is for the case that we are called with current_x
16546 past last_visible_x. */
16547 while (it->current_x < max_x)
a2889657 16548 {
5f5c8ee5 16549 int x_before, x, n_glyphs_before, i, nglyphs;
1c9241f5 16550
5f5c8ee5
GM
16551 /* Get the next display element. */
16552 if (!get_next_display_element (it))
90adcf20 16553 break;
1c9241f5 16554
5f5c8ee5
GM
16555 /* Produce glyphs. */
16556 x_before = it->current_x;
16557 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
16558 PRODUCE_GLYPHS (it);
90adcf20 16559
5f5c8ee5
GM
16560 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
16561 i = 0;
16562 x = x_before;
16563 while (i < nglyphs)
a2889657 16564 {
5f5c8ee5 16565 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
2311178e 16566
5f5c8ee5
GM
16567 if (!it->truncate_lines_p
16568 && x + glyph->pixel_width > max_x)
16569 {
16570 /* End of continued line or max_x reached. */
37be86f2
KH
16571 if (CHAR_GLYPH_PADDING_P (*glyph))
16572 {
16573 /* A wide character is unbreakable. */
16574 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
16575 it->current_x = x_before;
16576 }
16577 else
16578 {
16579 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
16580 it->current_x = x;
16581 }
5f5c8ee5
GM
16582 break;
16583 }
16584 else if (x + glyph->pixel_width > it->first_visible_x)
16585 {
16586 /* Glyph is at least partially visible. */
16587 ++it->hpos;
16588 if (x < it->first_visible_x)
16589 it->glyph_row->x = x - it->first_visible_x;
16590 }
16591 else
a2889657 16592 {
5f5c8ee5
GM
16593 /* Glyph is off the left margin of the display area.
16594 Should not happen. */
16595 abort ();
a2889657 16596 }
5f5c8ee5
GM
16597
16598 row->ascent = max (row->ascent, it->max_ascent);
16599 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
16600 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16601 row->phys_height = max (row->phys_height,
16602 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
16603 x += glyph->pixel_width;
16604 ++i;
a2889657 16605 }
5f5c8ee5
GM
16606
16607 /* Stop if max_x reached. */
16608 if (i < nglyphs)
16609 break;
16610
16611 /* Stop at line ends. */
16612 if (ITERATOR_AT_END_OF_LINE_P (it))
a2889657 16613 {
5f5c8ee5
GM
16614 it->continuation_lines_width = 0;
16615 break;
a2889657 16616 }
1c9241f5 16617
cafafe0b 16618 set_iterator_to_next (it, 1);
a688bb24 16619
5f5c8ee5
GM
16620 /* Stop if truncating at the right edge. */
16621 if (it->truncate_lines_p
16622 && it->current_x >= it->last_visible_x)
16623 {
16624 /* Add truncation mark, but don't do it if the line is
16625 truncated at a padding space. */
16626 if (IT_CHARPOS (*it) < it->string_nchars)
1c9241f5 16627 {
5f5c8ee5 16628 if (!FRAME_WINDOW_P (it->f))
37be86f2
KH
16629 {
16630 int i, n;
16631
9299cb15 16632 if (it->current_x > it->last_visible_x)
37be86f2 16633 {
9299cb15
KH
16634 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16635 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16636 break;
16637 for (n = row->used[TEXT_AREA]; i < n; ++i)
16638 {
16639 row->used[TEXT_AREA] = i;
16640 produce_special_glyphs (it, IT_TRUNCATION);
16641 }
37be86f2 16642 }
9299cb15 16643 produce_special_glyphs (it, IT_TRUNCATION);
37be86f2 16644 }
5f5c8ee5 16645 it->glyph_row->truncated_on_right_p = 1;
1c9241f5 16646 }
5f5c8ee5 16647 break;
1c9241f5 16648 }
a2889657
JB
16649 }
16650
5f5c8ee5
GM
16651 /* Maybe insert a truncation at the left. */
16652 if (it->first_visible_x
16653 && IT_CHARPOS (*it) > 0)
a2889657 16654 {
5f5c8ee5
GM
16655 if (!FRAME_WINDOW_P (it->f))
16656 insert_left_trunc_glyphs (it);
16657 it->glyph_row->truncated_on_left_p = 1;
a2889657
JB
16658 }
16659
5f5c8ee5 16660 it->face_id = saved_face_id;
2311178e 16661
5f5c8ee5
GM
16662 /* Value is number of columns displayed. */
16663 return it->hpos - hpos_at_start;
16664}
a2889657 16665
a2889657 16666
a2889657 16667\f
3b6b6db7 16668/* This is like a combination of memq and assq. Return 1/2 if PROPVAL
5f5c8ee5
GM
16669 appears as an element of LIST or as the car of an element of LIST.
16670 If PROPVAL is a list, compare each element against LIST in that
3b6b6db7
SM
16671 way, and return 1/2 if any element of PROPVAL is found in LIST.
16672 Otherwise return 0. This function cannot quit.
16673 The return value is 2 if the text is invisible but with an ellipsis
16674 and 1 if it's invisible and without an ellipsis. */
642eefc6
RS
16675
16676int
16677invisible_p (propval, list)
16678 register Lisp_Object propval;
16679 Lisp_Object list;
16680{
3b6b6db7 16681 register Lisp_Object tail, proptail;
2311178e 16682
3b6b6db7
SM
16683 for (tail = list; CONSP (tail); tail = XCDR (tail))
16684 {
16685 register Lisp_Object tem;
16686 tem = XCAR (tail);
16687 if (EQ (propval, tem))
16688 return 1;
16689 if (CONSP (tem) && EQ (propval, XCAR (tem)))
16690 return NILP (XCDR (tem)) ? 1 : 2;
16691 }
2311178e 16692
3b6b6db7
SM
16693 if (CONSP (propval))
16694 {
16695 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
16696 {
16697 Lisp_Object propelt;
16698 propelt = XCAR (proptail);
16699 for (tail = list; CONSP (tail); tail = XCDR (tail))
16700 {
16701 register Lisp_Object tem;
16702 tem = XCAR (tail);
16703 if (EQ (propelt, tem))
16704 return 1;
16705 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
16706 return NILP (XCDR (tem)) ? 1 : 2;
16707 }
16708 }
16709 }
2311178e 16710
3b6b6db7 16711 return 0;
642eefc6 16712}
5f5c8ee5 16713
da06e4d7
KS
16714/* Calculate a width or height in pixels from a specification using
16715 the following elements:
16716
be21b925 16717 SPEC ::=
da06e4d7
KS
16718 NUM - a (fractional) multiple of the default font width/height
16719 (NUM) - specifies exactly NUM pixels
16720 UNIT - a fixed number of pixels, see below.
16721 ELEMENT - size of a display element in pixels, see below.
16722 (NUM . SPEC) - equals NUM * SPEC
16723 (+ SPEC SPEC ...) - add pixel values
16724 (- SPEC SPEC ...) - subtract pixel values
16725 (- SPEC) - negate pixel value
16726
be21b925 16727 NUM ::=
da06e4d7
KS
16728 INT or FLOAT - a number constant
16729 SYMBOL - use symbol's (buffer local) variable binding.
16730
16731 UNIT ::=
16732 in - pixels per inch *)
16733 mm - pixels per 1/1000 meter *)
16734 cm - pixels per 1/100 meter *)
16735 width - width of current font in pixels.
16736 height - height of current font in pixels.
16737
16738 *) using the ratio(s) defined in display-pixels-per-inch.
16739
16740 ELEMENT ::=
16741
16742 left-fringe - left fringe width in pixels
16743 right-fringe - right fringe width in pixels
16744
16745 left-margin - left margin width in pixels
16746 right-margin - right margin width in pixels
16747
16748 scroll-bar - scroll-bar area width in pixels
16749
16750 Examples:
16751
16752 Pixels corresponding to 5 inches:
be21b925
KS
16753 (5 . in)
16754
da06e4d7
KS
16755 Total width of non-text areas on left side of window (if scroll-bar is on left):
16756 '(space :width (+ left-fringe left-margin scroll-bar))
16757
16758 Align to first text column (in header line):
16759 '(space :align-to 0)
16760
be21b925 16761 Align to middle of text area minus half the width of variable `my-image'
da06e4d7
KS
16762 containing a loaded image:
16763 '(space :align-to (0.5 . (- text my-image)))
16764
16765 Width of left margin minus width of 1 character in the default font:
16766 '(space :width (- left-margin 1))
16767
16768 Width of left margin minus width of 2 characters in the current font:
16769 '(space :width (- left-margin (2 . width)))
16770
16771 Center 1 character over left-margin (in header line):
16772 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
16773
16774 Different ways to express width of left fringe plus left margin minus one pixel:
16775 '(space :width (- (+ left-fringe left-margin) (1)))
16776 '(space :width (+ left-fringe left-margin (- (1))))
16777 '(space :width (+ left-fringe left-margin (-1)))
16778
16779*/
16780
16781#define NUMVAL(X) \
16782 ((INTEGERP (X) || FLOATP (X)) \
16783 ? XFLOATINT (X) \
16784 : - 1)
16785
16786int
16787calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
16788 double *res;
16789 struct it *it;
16790 Lisp_Object prop;
16791 void *font;
16792 int width_p, *align_to;
16793{
16794 double pixels;
16795
16796#define OK_PIXELS(val) ((*res = (double)(val)), 1)
16797#define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
16798
16799 if (NILP (prop))
16800 return OK_PIXELS (0);
16801
16802 if (SYMBOLP (prop))
16803 {
16804 if (SCHARS (SYMBOL_NAME (prop)) == 2)
16805 {
16806 char *unit = SDATA (SYMBOL_NAME (prop));
16807
16808 if (unit[0] == 'i' && unit[1] == 'n')
16809 pixels = 1.0;
16810 else if (unit[0] == 'm' && unit[1] == 'm')
16811 pixels = 25.4;
16812 else if (unit[0] == 'c' && unit[1] == 'm')
16813 pixels = 2.54;
16814 else
16815 pixels = 0;
16816 if (pixels > 0)
16817 {
16818 double ppi;
16819 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
16820 || (CONSP (Vdisplay_pixels_per_inch)
16821 && (ppi = (width_p
16822 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
16823 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
16824 ppi > 0)))
16825 return OK_PIXELS (ppi / pixels);
16826
16827 return 0;
16828 }
16829 }
16830
16831#ifdef HAVE_WINDOW_SYSTEM
16832 if (EQ (prop, Qheight))
16833 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
16834 if (EQ (prop, Qwidth))
16835 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
16836#else
16837 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
16838 return OK_PIXELS (1);
16839#endif
16840
16841 if (EQ (prop, Qtext))
16842 return OK_PIXELS (width_p
16843 ? window_box_width (it->w, TEXT_AREA)
16844 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
16845
16846 if (align_to && *align_to < 0)
16847 {
16848 *res = 0;
16849 if (EQ (prop, Qleft))
16850 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
16851 if (EQ (prop, Qright))
16852 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
16853 if (EQ (prop, Qcenter))
16854 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
16855 + window_box_width (it->w, TEXT_AREA) / 2);
16856 if (EQ (prop, Qleft_fringe))
16857 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
16858 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
16859 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
16860 if (EQ (prop, Qright_fringe))
16861 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
16862 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
16863 : window_box_right_offset (it->w, TEXT_AREA));
16864 if (EQ (prop, Qleft_margin))
16865 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
16866 if (EQ (prop, Qright_margin))
16867 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
16868 if (EQ (prop, Qscroll_bar))
16869 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
16870 ? 0
16871 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
16872 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
16873 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
16874 : 0)));
16875 }
16876 else
16877 {
16878 if (EQ (prop, Qleft_fringe))
16879 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
16880 if (EQ (prop, Qright_fringe))
16881 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
16882 if (EQ (prop, Qleft_margin))
16883 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
16884 if (EQ (prop, Qright_margin))
16885 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
16886 if (EQ (prop, Qscroll_bar))
16887 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
16888 }
16889
16890 prop = Fbuffer_local_value (prop, it->w->buffer);
16891 }
16892
16893 if (INTEGERP (prop) || FLOATP (prop))
16894 {
16895 int base_unit = (width_p
16896 ? FRAME_COLUMN_WIDTH (it->f)
16897 : FRAME_LINE_HEIGHT (it->f));
16898 return OK_PIXELS (XFLOATINT (prop) * base_unit);
16899 }
16900
16901 if (CONSP (prop))
16902 {
16903 Lisp_Object car = XCAR (prop);
16904 Lisp_Object cdr = XCDR (prop);
16905
16906 if (SYMBOLP (car))
16907 {
992126de 16908#ifdef HAVE_WINDOW_SYSTEM
da06e4d7
KS
16909 if (valid_image_p (prop))
16910 {
16911 int id = lookup_image (it->f, prop);
16912 struct image *img = IMAGE_FROM_ID (it->f, id);
16913
16914 return OK_PIXELS (width_p ? img->width : img->height);
16915 }
992126de 16916#endif
da06e4d7
KS
16917 if (EQ (car, Qplus) || EQ (car, Qminus))
16918 {
16919 int first = 1;
16920 double px;
16921
16922 pixels = 0;
16923 while (CONSP (cdr))
16924 {
16925 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
16926 font, width_p, align_to))
16927 return 0;
16928 if (first)
16929 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
16930 else
16931 pixels += px;
16932 cdr = XCDR (cdr);
16933 }
16934 if (EQ (car, Qminus))
16935 pixels = -pixels;
16936 return OK_PIXELS (pixels);
16937 }
16938
16939 car = Fbuffer_local_value (car, it->w->buffer);
16940 }
16941
16942 if (INTEGERP (car) || FLOATP (car))
16943 {
16944 double fact;
16945 pixels = XFLOATINT (car);
16946 if (NILP (cdr))
16947 return OK_PIXELS (pixels);
16948 if (calc_pixel_width_or_height (&fact, it, cdr,
16949 font, width_p, align_to))
16950 return OK_PIXELS (pixels * fact);
16951 return 0;
16952 }
16953
16954 return 0;
16955 }
16956
16957 return 0;
16958}
16959
642eefc6 16960\f
133c764e
KS
16961/***********************************************************************
16962 Glyph Display
16963 ***********************************************************************/
16964
79fa9e0f
KS
16965#ifdef HAVE_WINDOW_SYSTEM
16966
133c764e
KS
16967#if GLYPH_DEBUG
16968
16969void
16970dump_glyph_string (s)
16971 struct glyph_string *s;
16972{
16973 fprintf (stderr, "glyph string\n");
16974 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
16975 s->x, s->y, s->width, s->height);
16976 fprintf (stderr, " ybase = %d\n", s->ybase);
16977 fprintf (stderr, " hl = %d\n", s->hl);
16978 fprintf (stderr, " left overhang = %d, right = %d\n",
16979 s->left_overhang, s->right_overhang);
16980 fprintf (stderr, " nchars = %d\n", s->nchars);
16981 fprintf (stderr, " extends to end of line = %d\n",
16982 s->extends_to_end_of_line_p);
16983 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
16984 fprintf (stderr, " bg width = %d\n", s->background_width);
16985}
16986
16987#endif /* GLYPH_DEBUG */
16988
16989/* Initialize glyph string S. CHAR2B is a suitably allocated vector
16990 of XChar2b structures for S; it can't be allocated in
16991 init_glyph_string because it must be allocated via `alloca'. W
16992 is the window on which S is drawn. ROW and AREA are the glyph row
16993 and area within the row from which S is constructed. START is the
16994 index of the first glyph structure covered by S. HL is a
16995 face-override for drawing S. */
16996
16997#ifdef HAVE_NTGUI
16998#define OPTIONAL_HDC(hdc) hdc,
16999#define DECLARE_HDC(hdc) HDC hdc;
17000#define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17001#define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17002#endif
17003
17004#ifndef OPTIONAL_HDC
17005#define OPTIONAL_HDC(hdc)
17006#define DECLARE_HDC(hdc)
17007#define ALLOCATE_HDC(hdc, f)
17008#define RELEASE_HDC(hdc, f)
17009#endif
17010
17011static void
17012init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17013 struct glyph_string *s;
17014 DECLARE_HDC (hdc)
17015 XChar2b *char2b;
17016 struct window *w;
17017 struct glyph_row *row;
17018 enum glyph_row_area area;
17019 int start;
17020 enum draw_glyphs_face hl;
17021{
17022 bzero (s, sizeof *s);
17023 s->w = w;
17024 s->f = XFRAME (w->frame);
17025#ifdef HAVE_NTGUI
17026 s->hdc = hdc;
17027#endif
17028 s->display = FRAME_X_DISPLAY (s->f);
17029 s->window = FRAME_X_WINDOW (s->f);
17030 s->char2b = char2b;
17031 s->hl = hl;
17032 s->row = row;
17033 s->area = area;
17034 s->first_glyph = row->glyphs[area] + start;
17035 s->height = row->height;
17036 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17037
17038 /* Display the internal border below the tool-bar window. */
17039 if (s->w == XWINDOW (s->f->tool_bar_window))
17040 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17041
17042 s->ybase = s->y + row->ascent;
17043}
17044
17045
17046/* Append the list of glyph strings with head H and tail T to the list
17047 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17048
17049static INLINE void
17050append_glyph_string_lists (head, tail, h, t)
17051 struct glyph_string **head, **tail;
17052 struct glyph_string *h, *t;
17053{
17054 if (h)
17055 {
17056 if (*head)
17057 (*tail)->next = h;
17058 else
17059 *head = h;
17060 h->prev = *tail;
17061 *tail = t;
17062 }
17063}
17064
17065
17066/* Prepend the list of glyph strings with head H and tail T to the
17067 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17068 result. */
17069
17070static INLINE void
17071prepend_glyph_string_lists (head, tail, h, t)
17072 struct glyph_string **head, **tail;
17073 struct glyph_string *h, *t;
17074{
17075 if (h)
17076 {
17077 if (*head)
17078 (*head)->prev = t;
17079 else
17080 *tail = t;
17081 t->next = *head;
17082 *head = h;
17083 }
17084}
17085
17086
17087/* Append glyph string S to the list with head *HEAD and tail *TAIL.
17088 Set *HEAD and *TAIL to the resulting list. */
17089
17090static INLINE void
17091append_glyph_string (head, tail, s)
17092 struct glyph_string **head, **tail;
17093 struct glyph_string *s;
17094{
17095 s->next = s->prev = NULL;
17096 append_glyph_string_lists (head, tail, s, s);
17097}
17098
17099
17100/* Get face and two-byte form of character glyph GLYPH on frame F.
17101 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17102 a pointer to a realized face that is ready for display. */
17103
17104static INLINE struct face *
17105get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17106 struct frame *f;
17107 struct glyph *glyph;
17108 XChar2b *char2b;
17109 int *two_byte_p;
17110{
17111 struct face *face;
17112
17113 xassert (glyph->type == CHAR_GLYPH);
17114 face = FACE_FROM_ID (f, glyph->face_id);
17115
17116 if (two_byte_p)
17117 *two_byte_p = 0;
17118
17119 if (!glyph->multibyte_p)
17120 {
17121 /* Unibyte case. We don't have to encode, but we have to make
17122 sure to use a face suitable for unibyte. */
17123 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17124 }
17125 else if (glyph->u.ch < 128
17126 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17127 {
17128 /* Case of ASCII in a face known to fit ASCII. */
17129 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17130 }
17131 else
17132 {
17133 int c1, c2, charset;
17134
17135 /* Split characters into bytes. If c2 is -1 afterwards, C is
17136 really a one-byte character so that byte1 is zero. */
17137 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17138 if (c2 > 0)
17139 STORE_XCHAR2B (char2b, c1, c2);
17140 else
17141 STORE_XCHAR2B (char2b, 0, c1);
17142
17143 /* Maybe encode the character in *CHAR2B. */
17144 if (charset != CHARSET_ASCII)
17145 {
17146 struct font_info *font_info
17147 = FONT_INFO_FROM_ID (f, face->font_info_id);
17148 if (font_info)
17149 glyph->font_type
17150 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
17151 }
17152 }
17153
17154 /* Make sure X resources of the face are allocated. */
17155 xassert (face != NULL);
17156 PREPARE_FACE_FOR_DISPLAY (f, face);
17157 return face;
17158}
17159
17160
17161/* Fill glyph string S with composition components specified by S->cmp.
17162
17163 FACES is an array of faces for all components of this composition.
17164 S->gidx is the index of the first component for S.
17165 OVERLAPS_P non-zero means S should draw the foreground only, and
17166 use its physical height for clipping.
17167
17168 Value is the index of a component not in S. */
17169
17170static int
17171fill_composite_glyph_string (s, faces, overlaps_p)
17172 struct glyph_string *s;
17173 struct face **faces;
17174 int overlaps_p;
17175{
17176 int i;
17177
17178 xassert (s);
17179
17180 s->for_overlaps_p = overlaps_p;
17181
17182 s->face = faces[s->gidx];
17183 s->font = s->face->font;
17184 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17185
17186 /* For all glyphs of this composition, starting at the offset
17187 S->gidx, until we reach the end of the definition or encounter a
17188 glyph that requires the different face, add it to S. */
17189 ++s->nchars;
17190 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
17191 ++s->nchars;
17192
17193 /* All glyph strings for the same composition has the same width,
17194 i.e. the width set for the first component of the composition. */
17195
17196 s->width = s->first_glyph->pixel_width;
17197
17198 /* If the specified font could not be loaded, use the frame's
17199 default font, but record the fact that we couldn't load it in
17200 the glyph string so that we can draw rectangles for the
17201 characters of the glyph string. */
17202 if (s->font == NULL)
17203 {
17204 s->font_not_found_p = 1;
17205 s->font = FRAME_FONT (s->f);
17206 }
17207
17208 /* Adjust base line for subscript/superscript text. */
17209 s->ybase += s->first_glyph->voffset;
17210
17211 xassert (s->face && s->face->gc);
17212
17213 /* This glyph string must always be drawn with 16-bit functions. */
17214 s->two_byte_p = 1;
17215
17216 return s->gidx + s->nchars;
17217}
17218
17219
17220/* Fill glyph string S from a sequence of character glyphs.
17221
17222 FACE_ID is the face id of the string. START is the index of the
17223 first glyph to consider, END is the index of the last + 1.
17224 OVERLAPS_P non-zero means S should draw the foreground only, and
17225 use its physical height for clipping.
17226
17227 Value is the index of the first glyph not in S. */
17228
17229static int
17230fill_glyph_string (s, face_id, start, end, overlaps_p)
17231 struct glyph_string *s;
17232 int face_id;
17233 int start, end, overlaps_p;
17234{
17235 struct glyph *glyph, *last;
17236 int voffset;
17237 int glyph_not_available_p;
17238
17239 xassert (s->f == XFRAME (s->w->frame));
17240 xassert (s->nchars == 0);
17241 xassert (start >= 0 && end > start);
17242
17243 s->for_overlaps_p = overlaps_p,
17244 glyph = s->row->glyphs[s->area] + start;
17245 last = s->row->glyphs[s->area] + end;
17246 voffset = glyph->voffset;
17247
17248 glyph_not_available_p = glyph->glyph_not_available_p;
17249
17250 while (glyph < last
17251 && glyph->type == CHAR_GLYPH
17252 && glyph->voffset == voffset
17253 /* Same face id implies same font, nowadays. */
17254 && glyph->face_id == face_id
17255 && glyph->glyph_not_available_p == glyph_not_available_p)
17256 {
17257 int two_byte_p;
17258
17259 s->face = get_glyph_face_and_encoding (s->f, glyph,
17260 s->char2b + s->nchars,
17261 &two_byte_p);
17262 s->two_byte_p = two_byte_p;
17263 ++s->nchars;
17264 xassert (s->nchars <= end - start);
17265 s->width += glyph->pixel_width;
17266 ++glyph;
17267 }
17268
17269 s->font = s->face->font;
17270 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17271
17272 /* If the specified font could not be loaded, use the frame's font,
17273 but record the fact that we couldn't load it in
17274 S->font_not_found_p so that we can draw rectangles for the
17275 characters of the glyph string. */
17276 if (s->font == NULL || glyph_not_available_p)
17277 {
17278 s->font_not_found_p = 1;
17279 s->font = FRAME_FONT (s->f);
17280 }
17281
17282 /* Adjust base line for subscript/superscript text. */
17283 s->ybase += voffset;
17284
17285 xassert (s->face && s->face->gc);
17286 return glyph - s->row->glyphs[s->area];
17287}
17288
17289
17290/* Fill glyph string S from image glyph S->first_glyph. */
17291
17292static void
17293fill_image_glyph_string (s)
17294 struct glyph_string *s;
17295{
17296 xassert (s->first_glyph->type == IMAGE_GLYPH);
17297 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
17298 xassert (s->img);
17299 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
17300 s->font = s->face->font;
17301 s->width = s->first_glyph->pixel_width;
17302
17303 /* Adjust base line for subscript/superscript text. */
17304 s->ybase += s->first_glyph->voffset;
17305}
17306
17307
17308/* Fill glyph string S from a sequence of stretch glyphs.
17309
17310 ROW is the glyph row in which the glyphs are found, AREA is the
17311 area within the row. START is the index of the first glyph to
17312 consider, END is the index of the last + 1.
17313
17314 Value is the index of the first glyph not in S. */
17315
17316static int
17317fill_stretch_glyph_string (s, row, area, start, end)
17318 struct glyph_string *s;
17319 struct glyph_row *row;
17320 enum glyph_row_area area;
17321 int start, end;
17322{
17323 struct glyph *glyph, *last;
17324 int voffset, face_id;
17325
17326 xassert (s->first_glyph->type == STRETCH_GLYPH);
17327
17328 glyph = s->row->glyphs[s->area] + start;
17329 last = s->row->glyphs[s->area] + end;
17330 face_id = glyph->face_id;
17331 s->face = FACE_FROM_ID (s->f, face_id);
17332 s->font = s->face->font;
17333 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17334 s->width = glyph->pixel_width;
17335 voffset = glyph->voffset;
17336
17337 for (++glyph;
17338 (glyph < last
17339 && glyph->type == STRETCH_GLYPH
17340 && glyph->voffset == voffset
17341 && glyph->face_id == face_id);
17342 ++glyph)
17343 s->width += glyph->pixel_width;
17344
17345 /* Adjust base line for subscript/superscript text. */
17346 s->ybase += voffset;
17347
17348 /* The case that face->gc == 0 is handled when drawing the glyph
17349 string by calling PREPARE_FACE_FOR_DISPLAY. */
17350 xassert (s->face);
17351 return glyph - s->row->glyphs[s->area];
17352}
17353
17354
17355/* EXPORT for RIF:
17356 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
17357 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
17358 assumed to be zero. */
17359
17360void
17361x_get_glyph_overhangs (glyph, f, left, right)
17362 struct glyph *glyph;
17363 struct frame *f;
17364 int *left, *right;
17365{
17366 *left = *right = 0;
17367
17368 if (glyph->type == CHAR_GLYPH)
17369 {
17370 XFontStruct *font;
17371 struct face *face;
17372 struct font_info *font_info;
17373 XChar2b char2b;
17374 XCharStruct *pcm;
17375
17376 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
17377 font = face->font;
17378 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
17379 if (font /* ++KFS: Should this be font_info ? */
17380 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
17381 {
17382 if (pcm->rbearing > pcm->width)
17383 *right = pcm->rbearing - pcm->width;
17384 if (pcm->lbearing < 0)
17385 *left = -pcm->lbearing;
17386 }
17387 }
17388}
17389
17390
17391/* Return the index of the first glyph preceding glyph string S that
17392 is overwritten by S because of S's left overhang. Value is -1
17393 if no glyphs are overwritten. */
17394
17395static int
17396left_overwritten (s)
17397 struct glyph_string *s;
17398{
17399 int k;
17400
17401 if (s->left_overhang)
17402 {
17403 int x = 0, i;
17404 struct glyph *glyphs = s->row->glyphs[s->area];
17405 int first = s->first_glyph - glyphs;
17406
17407 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
17408 x -= glyphs[i].pixel_width;
17409
17410 k = i + 1;
17411 }
17412 else
17413 k = -1;
17414
17415 return k;
17416}
17417
17418
17419/* Return the index of the first glyph preceding glyph string S that
17420 is overwriting S because of its right overhang. Value is -1 if no
17421 glyph in front of S overwrites S. */
17422
17423static int
17424left_overwriting (s)
17425 struct glyph_string *s;
17426{
17427 int i, k, x;
17428 struct glyph *glyphs = s->row->glyphs[s->area];
17429 int first = s->first_glyph - glyphs;
17430
17431 k = -1;
17432 x = 0;
17433 for (i = first - 1; i >= 0; --i)
17434 {
17435 int left, right;
17436 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17437 if (x + right > 0)
17438 k = i;
17439 x -= glyphs[i].pixel_width;
17440 }
17441
17442 return k;
17443}
17444
17445
17446/* Return the index of the last glyph following glyph string S that is
17447 not overwritten by S because of S's right overhang. Value is -1 if
17448 no such glyph is found. */
17449
17450static int
17451right_overwritten (s)
17452 struct glyph_string *s;
17453{
17454 int k = -1;
17455
17456 if (s->right_overhang)
17457 {
17458 int x = 0, i;
17459 struct glyph *glyphs = s->row->glyphs[s->area];
17460 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17461 int end = s->row->used[s->area];
17462
17463 for (i = first; i < end && s->right_overhang > x; ++i)
17464 x += glyphs[i].pixel_width;
17465
17466 k = i;
17467 }
17468
17469 return k;
17470}
17471
17472
17473/* Return the index of the last glyph following glyph string S that
17474 overwrites S because of its left overhang. Value is negative
17475 if no such glyph is found. */
17476
17477static int
17478right_overwriting (s)
17479 struct glyph_string *s;
17480{
17481 int i, k, x;
17482 int end = s->row->used[s->area];
17483 struct glyph *glyphs = s->row->glyphs[s->area];
17484 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17485
17486 k = -1;
17487 x = 0;
17488 for (i = first; i < end; ++i)
17489 {
17490 int left, right;
17491 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17492 if (x - left < 0)
17493 k = i;
17494 x += glyphs[i].pixel_width;
17495 }
17496
17497 return k;
17498}
17499
17500
17501/* Get face and two-byte form of character C in face FACE_ID on frame
17502 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
17503 means we want to display multibyte text. DISPLAY_P non-zero means
17504 make sure that X resources for the face returned are allocated.
17505 Value is a pointer to a realized face that is ready for display if
17506 DISPLAY_P is non-zero. */
17507
17508static INLINE struct face *
17509get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
17510 struct frame *f;
17511 int c, face_id;
17512 XChar2b *char2b;
17513 int multibyte_p, display_p;
17514{
17515 struct face *face = FACE_FROM_ID (f, face_id);
17516
17517 if (!multibyte_p)
17518 {
17519 /* Unibyte case. We don't have to encode, but we have to make
17520 sure to use a face suitable for unibyte. */
17521 STORE_XCHAR2B (char2b, 0, c);
17522 face_id = FACE_FOR_CHAR (f, face, c);
17523 face = FACE_FROM_ID (f, face_id);
17524 }
17525 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
17526 {
17527 /* Case of ASCII in a face known to fit ASCII. */
17528 STORE_XCHAR2B (char2b, 0, c);
17529 }
17530 else
17531 {
17532 int c1, c2, charset;
17533
17534 /* Split characters into bytes. If c2 is -1 afterwards, C is
17535 really a one-byte character so that byte1 is zero. */
17536 SPLIT_CHAR (c, charset, c1, c2);
17537 if (c2 > 0)
17538 STORE_XCHAR2B (char2b, c1, c2);
17539 else
17540 STORE_XCHAR2B (char2b, 0, c1);
17541
17542 /* Maybe encode the character in *CHAR2B. */
17543 if (face->font != NULL)
17544 {
17545 struct font_info *font_info
17546 = FONT_INFO_FROM_ID (f, face->font_info_id);
17547 if (font_info)
17548 rif->encode_char (c, char2b, font_info, 0);
17549 }
17550 }
17551
17552 /* Make sure X resources of the face are allocated. */
17553#ifdef HAVE_X_WINDOWS
17554 if (display_p)
17555#endif
17556 {
17557 xassert (face != NULL);
17558 PREPARE_FACE_FOR_DISPLAY (f, face);
17559 }
17560
17561 return face;
17562}
17563
17564
17565/* Set background width of glyph string S. START is the index of the
17566 first glyph following S. LAST_X is the right-most x-position + 1
17567 in the drawing area. */
17568
17569static INLINE void
17570set_glyph_string_background_width (s, start, last_x)
17571 struct glyph_string *s;
17572 int start;
17573 int last_x;
17574{
17575 /* If the face of this glyph string has to be drawn to the end of
17576 the drawing area, set S->extends_to_end_of_line_p. */
17577 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
17578
17579 if (start == s->row->used[s->area]
17580 && s->area == TEXT_AREA
17581 && ((s->hl == DRAW_NORMAL_TEXT
17582 && (s->row->fill_line_p
17583 || s->face->background != default_face->background
17584 || s->face->stipple != default_face->stipple
17585 || s->row->mouse_face_p))
17586 || s->hl == DRAW_MOUSE_FACE
17587 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
17588 && s->row->fill_line_p)))
17589 s->extends_to_end_of_line_p = 1;
17590
17591 /* If S extends its face to the end of the line, set its
17592 background_width to the distance to the right edge of the drawing
17593 area. */
17594 if (s->extends_to_end_of_line_p)
17595 s->background_width = last_x - s->x + 1;
17596 else
17597 s->background_width = s->width;
17598}
17599
17600
17601/* Compute overhangs and x-positions for glyph string S and its
17602 predecessors, or successors. X is the starting x-position for S.
17603 BACKWARD_P non-zero means process predecessors. */
17604
17605static void
17606compute_overhangs_and_x (s, x, backward_p)
17607 struct glyph_string *s;
17608 int x;
17609 int backward_p;
17610{
17611 if (backward_p)
17612 {
17613 while (s)
17614 {
17615 if (rif->compute_glyph_string_overhangs)
17616 rif->compute_glyph_string_overhangs (s);
17617 x -= s->width;
17618 s->x = x;
17619 s = s->prev;
17620 }
17621 }
17622 else
17623 {
17624 while (s)
17625 {
17626 if (rif->compute_glyph_string_overhangs)
17627 rif->compute_glyph_string_overhangs (s);
17628 s->x = x;
17629 x += s->width;
17630 s = s->next;
17631 }
17632 }
17633}
17634
17635
17636
fa3c6b4d 17637/* The following macros are only called from draw_glyphs below.
133c764e
KS
17638 They reference the following parameters of that function directly:
17639 `w', `row', `area', and `overlap_p'
17640 as well as the following local variables:
17641 `s', `f', and `hdc' (in W32) */
17642
17643#ifdef HAVE_NTGUI
17644/* On W32, silently add local `hdc' variable to argument list of
17645 init_glyph_string. */
17646#define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17647 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
17648#else
17649#define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17650 init_glyph_string (s, char2b, w, row, area, start, hl)
17651#endif
17652
17653/* Add a glyph string for a stretch glyph to the list of strings
17654 between HEAD and TAIL. START is the index of the stretch glyph in
17655 row area AREA of glyph row ROW. END is the index of the last glyph
17656 in that glyph row area. X is the current output position assigned
17657 to the new glyph string constructed. HL overrides that face of the
17658 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17659 is the right-most x-position of the drawing area. */
17660
17661/* SunOS 4 bundled cc, barfed on continuations in the arg lists here
17662 and below -- keep them on one line. */
17663#define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17664 do \
17665 { \
17666 s = (struct glyph_string *) alloca (sizeof *s); \
17667 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17668 START = fill_stretch_glyph_string (s, row, area, START, END); \
17669 append_glyph_string (&HEAD, &TAIL, s); \
17670 s->x = (X); \
17671 } \
17672 while (0)
17673
17674
17675/* Add a glyph string for an image glyph to the list of strings
17676 between HEAD and TAIL. START is the index of the image glyph in
17677 row area AREA of glyph row ROW. END is the index of the last glyph
17678 in that glyph row area. X is the current output position assigned
17679 to the new glyph string constructed. HL overrides that face of the
17680 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17681 is the right-most x-position of the drawing area. */
17682
17683#define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17684 do \
17685 { \
17686 s = (struct glyph_string *) alloca (sizeof *s); \
17687 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17688 fill_image_glyph_string (s); \
17689 append_glyph_string (&HEAD, &TAIL, s); \
17690 ++START; \
17691 s->x = (X); \
17692 } \
17693 while (0)
17694
17695
17696/* Add a glyph string for a sequence of character glyphs to the list
17697 of strings between HEAD and TAIL. START is the index of the first
17698 glyph in row area AREA of glyph row ROW that is part of the new
17699 glyph string. END is the index of the last glyph in that glyph row
17700 area. X is the current output position assigned to the new glyph
17701 string constructed. HL overrides that face of the glyph; e.g. it
17702 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
17703 right-most x-position of the drawing area. */
17704
17705#define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17706 do \
17707 { \
17708 int c, face_id; \
17709 XChar2b *char2b; \
17710 \
17711 c = (row)->glyphs[area][START].u.ch; \
17712 face_id = (row)->glyphs[area][START].face_id; \
17713 \
17714 s = (struct glyph_string *) alloca (sizeof *s); \
17715 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
17716 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
17717 append_glyph_string (&HEAD, &TAIL, s); \
17718 s->x = (X); \
17719 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
17720 } \
17721 while (0)
17722
17723
17724/* Add a glyph string for a composite sequence to the list of strings
17725 between HEAD and TAIL. START is the index of the first glyph in
17726 row area AREA of glyph row ROW that is part of the new glyph
17727 string. END is the index of the last glyph in that glyph row area.
17728 X is the current output position assigned to the new glyph string
17729 constructed. HL overrides that face of the glyph; e.g. it is
17730 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
17731 x-position of the drawing area. */
17732
17733#define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17734 do { \
17735 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
17736 int face_id = (row)->glyphs[area][START].face_id; \
17737 struct face *base_face = FACE_FROM_ID (f, face_id); \
17738 struct composition *cmp = composition_table[cmp_id]; \
17739 int glyph_len = cmp->glyph_len; \
17740 XChar2b *char2b; \
17741 struct face **faces; \
17742 struct glyph_string *first_s = NULL; \
17743 int n; \
17744 \
17745 base_face = base_face->ascii_face; \
17746 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
17747 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
17748 /* At first, fill in `char2b' and `faces'. */ \
17749 for (n = 0; n < glyph_len; n++) \
17750 { \
17751 int c = COMPOSITION_GLYPH (cmp, n); \
17752 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
17753 faces[n] = FACE_FROM_ID (f, this_face_id); \
17754 get_char_face_and_encoding (f, c, this_face_id, \
17755 char2b + n, 1, 1); \
17756 } \
17757 \
17758 /* Make glyph_strings for each glyph sequence that is drawable by \
17759 the same face, and append them to HEAD/TAIL. */ \
17760 for (n = 0; n < cmp->glyph_len;) \
17761 { \
17762 s = (struct glyph_string *) alloca (sizeof *s); \
17763 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
17764 append_glyph_string (&(HEAD), &(TAIL), s); \
17765 s->cmp = cmp; \
17766 s->gidx = n; \
17767 s->x = (X); \
17768 \
17769 if (n == 0) \
17770 first_s = s; \
17771 \
17772 n = fill_composite_glyph_string (s, faces, overlaps_p); \
17773 } \
17774 \
17775 ++START; \
17776 s = first_s; \
17777 } while (0)
17778
17779
17780/* Build a list of glyph strings between HEAD and TAIL for the glyphs
17781 of AREA of glyph row ROW on window W between indices START and END.
17782 HL overrides the face for drawing glyph strings, e.g. it is
17783 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
17784 x-positions of the drawing area.
17785
17786 This is an ugly monster macro construct because we must use alloca
fa3c6b4d 17787 to allocate glyph strings (because draw_glyphs can be called
133c764e
KS
17788 asynchronously). */
17789
17790#define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17791 do \
17792 { \
17793 HEAD = TAIL = NULL; \
17794 while (START < END) \
17795 { \
17796 struct glyph *first_glyph = (row)->glyphs[area] + START; \
17797 switch (first_glyph->type) \
17798 { \
17799 case CHAR_GLYPH: \
17800 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
17801 HL, X, LAST_X); \
17802 break; \
17803 \
17804 case COMPOSITE_GLYPH: \
17805 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
17806 HL, X, LAST_X); \
17807 break; \
17808 \
17809 case STRETCH_GLYPH: \
17810 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
17811 HL, X, LAST_X); \
17812 break; \
17813 \
17814 case IMAGE_GLYPH: \
17815 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
17816 HL, X, LAST_X); \
17817 break; \
17818 \
17819 default: \
17820 abort (); \
17821 } \
17822 \
17823 set_glyph_string_background_width (s, START, LAST_X); \
17824 (X) += s->width; \
17825 } \
17826 } \
17827 while (0)
17828
17829
17830/* Draw glyphs between START and END in AREA of ROW on window W,
17831 starting at x-position X. X is relative to AREA in W. HL is a
17832 face-override with the following meaning:
17833
17834 DRAW_NORMAL_TEXT draw normally
17835 DRAW_CURSOR draw in cursor face
17836 DRAW_MOUSE_FACE draw in mouse face.
17837 DRAW_INVERSE_VIDEO draw in mode line face
17838 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
17839 DRAW_IMAGE_RAISED draw an image with a raised relief around it
17840
17841 If OVERLAPS_P is non-zero, draw only the foreground of characters
17842 and clip to the physical height of ROW.
17843
17844 Value is the x-position reached, relative to AREA of W. */
17845
fa3c6b4d
KS
17846static int
17847draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
133c764e
KS
17848 struct window *w;
17849 int x;
17850 struct glyph_row *row;
17851 enum glyph_row_area area;
17852 int start, end;
17853 enum draw_glyphs_face hl;
17854 int overlaps_p;
17855{
17856 struct glyph_string *head, *tail;
17857 struct glyph_string *s;
17858 int last_x, area_width;
17859 int x_reached;
17860 int i, j;
17861 struct frame *f = XFRAME (WINDOW_FRAME (w));
17862 DECLARE_HDC (hdc);
17863
17864 ALLOCATE_HDC (hdc, f);
17865
17866 /* Let's rather be paranoid than getting a SEGV. */
17867 end = min (end, row->used[area]);
17868 start = max (0, start);
17869 start = min (end, start);
17870
17871 /* Translate X to frame coordinates. Set last_x to the right
17872 end of the drawing area. */
17873 if (row->full_width_p)
17874 {
17875 /* X is relative to the left edge of W, without scroll bars
17876 or fringes. */
da8b7f4f
KS
17877 x += WINDOW_LEFT_EDGE_X (w);
17878 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
133c764e
KS
17879 }
17880 else
17881 {
da8b7f4f
KS
17882 int area_left = window_box_left (w, area);
17883 x += area_left;
133c764e 17884 area_width = window_box_width (w, area);
da8b7f4f 17885 last_x = area_left + area_width;
133c764e
KS
17886 }
17887
17888 /* Build a doubly-linked list of glyph_string structures between
17889 head and tail from what we have to draw. Note that the macro
17890 BUILD_GLYPH_STRINGS will modify its start parameter. That's
17891 the reason we use a separate variable `i'. */
17892 i = start;
17893 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
17894 if (tail)
17895 x_reached = tail->x + tail->background_width;
17896 else
17897 x_reached = x;
17898
17899 /* If there are any glyphs with lbearing < 0 or rbearing > width in
17900 the row, redraw some glyphs in front or following the glyph
17901 strings built above. */
17902 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
17903 {
17904 int dummy_x = 0;
17905 struct glyph_string *h, *t;
17906
17907 /* Compute overhangs for all glyph strings. */
17908 if (rif->compute_glyph_string_overhangs)
17909 for (s = head; s; s = s->next)
17910 rif->compute_glyph_string_overhangs (s);
17911
17912 /* Prepend glyph strings for glyphs in front of the first glyph
17913 string that are overwritten because of the first glyph
17914 string's left overhang. The background of all strings
17915 prepended must be drawn because the first glyph string
17916 draws over it. */
17917 i = left_overwritten (head);
17918 if (i >= 0)
17919 {
17920 j = i;
17921 BUILD_GLYPH_STRINGS (j, start, h, t,
17922 DRAW_NORMAL_TEXT, dummy_x, last_x);
17923 start = i;
17924 compute_overhangs_and_x (t, head->x, 1);
17925 prepend_glyph_string_lists (&head, &tail, h, t);
17926 }
17927
17928 /* Prepend glyph strings for glyphs in front of the first glyph
17929 string that overwrite that glyph string because of their
17930 right overhang. For these strings, only the foreground must
17931 be drawn, because it draws over the glyph string at `head'.
17932 The background must not be drawn because this would overwrite
17933 right overhangs of preceding glyphs for which no glyph
17934 strings exist. */
17935 i = left_overwriting (head);
17936 if (i >= 0)
17937 {
17938 BUILD_GLYPH_STRINGS (i, start, h, t,
17939 DRAW_NORMAL_TEXT, dummy_x, last_x);
17940 for (s = h; s; s = s->next)
17941 s->background_filled_p = 1;
17942 compute_overhangs_and_x (t, head->x, 1);
17943 prepend_glyph_string_lists (&head, &tail, h, t);
17944 }
17945
17946 /* Append glyphs strings for glyphs following the last glyph
17947 string tail that are overwritten by tail. The background of
17948 these strings has to be drawn because tail's foreground draws
17949 over it. */
17950 i = right_overwritten (tail);
17951 if (i >= 0)
17952 {
17953 BUILD_GLYPH_STRINGS (end, i, h, t,
17954 DRAW_NORMAL_TEXT, x, last_x);
17955 compute_overhangs_and_x (h, tail->x + tail->width, 0);
17956 append_glyph_string_lists (&head, &tail, h, t);
17957 }
17958
17959 /* Append glyph strings for glyphs following the last glyph
17960 string tail that overwrite tail. The foreground of such
17961 glyphs has to be drawn because it writes into the background
17962 of tail. The background must not be drawn because it could
17963 paint over the foreground of following glyphs. */
17964 i = right_overwriting (tail);
17965 if (i >= 0)
17966 {
17967 BUILD_GLYPH_STRINGS (end, i, h, t,
17968 DRAW_NORMAL_TEXT, x, last_x);
17969 for (s = h; s; s = s->next)
17970 s->background_filled_p = 1;
17971 compute_overhangs_and_x (h, tail->x + tail->width, 0);
17972 append_glyph_string_lists (&head, &tail, h, t);
17973 }
17974 }
17975
17976 /* Draw all strings. */
17977 for (s = head; s; s = s->next)
17978 rif->draw_glyph_string (s);
17979
17980 if (area == TEXT_AREA
17981 && !row->full_width_p
17982 /* When drawing overlapping rows, only the glyph strings'
17983 foreground is drawn, which doesn't erase a cursor
17984 completely. */
17985 && !overlaps_p)
17986 {
17987 int x0 = head ? head->x : x;
17988 int x1 = tail ? tail->x + tail->background_width : x;
17989
da8b7f4f
KS
17990 int text_left = window_box_left (w, TEXT_AREA);
17991 x0 -= text_left;
17992 x1 -= text_left;
133c764e 17993
da8b7f4f 17994 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
133c764e
KS
17995 row->y, MATRIX_ROW_BOTTOM_Y (row));
17996 }
17997
17998 /* Value is the x-position up to which drawn, relative to AREA of W.
17999 This doesn't include parts drawn because of overhangs. */
da8b7f4f
KS
18000 if (row->full_width_p)
18001 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18002 else
18003 x_reached -= window_box_left (w, area);
133c764e
KS
18004
18005 RELEASE_HDC (hdc, f);
18006
18007 return x_reached;
18008}
18009
18010
18011/* Store one glyph for IT->char_to_display in IT->glyph_row.
18012 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18013
18014static INLINE void
18015append_glyph (it)
18016 struct it *it;
18017{
18018 struct glyph *glyph;
18019 enum glyph_row_area area = it->area;
18020
18021 xassert (it->glyph_row);
18022 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18023
18024 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18025 if (glyph < it->glyph_row->glyphs[area + 1])
18026 {
18027 glyph->charpos = CHARPOS (it->position);
18028 glyph->object = it->object;
18029 glyph->pixel_width = it->pixel_width;
493fdc3c
KS
18030 glyph->ascent = it->ascent;
18031 glyph->descent = it->descent;
133c764e
KS
18032 glyph->voffset = it->voffset;
18033 glyph->type = CHAR_GLYPH;
18034 glyph->multibyte_p = it->multibyte_p;
18035 glyph->left_box_line_p = it->start_of_box_run_p;
18036 glyph->right_box_line_p = it->end_of_box_run_p;
18037 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18038 || it->phys_descent > it->descent);
18039 glyph->padding_p = 0;
18040 glyph->glyph_not_available_p = it->glyph_not_available_p;
18041 glyph->face_id = it->face_id;
18042 glyph->u.ch = it->char_to_display;
18043 glyph->font_type = FONT_TYPE_UNKNOWN;
18044 ++it->glyph_row->used[area];
18045 }
18046}
18047
18048/* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18049 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18050
18051static INLINE void
18052append_composite_glyph (it)
18053 struct it *it;
18054{
18055 struct glyph *glyph;
18056 enum glyph_row_area area = it->area;
18057
18058 xassert (it->glyph_row);
18059
18060 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18061 if (glyph < it->glyph_row->glyphs[area + 1])
18062 {
18063 glyph->charpos = CHARPOS (it->position);
18064 glyph->object = it->object;
18065 glyph->pixel_width = it->pixel_width;
493fdc3c
KS
18066 glyph->ascent = it->ascent;
18067 glyph->descent = it->descent;
133c764e
KS
18068 glyph->voffset = it->voffset;
18069 glyph->type = COMPOSITE_GLYPH;
18070 glyph->multibyte_p = it->multibyte_p;
18071 glyph->left_box_line_p = it->start_of_box_run_p;
18072 glyph->right_box_line_p = it->end_of_box_run_p;
18073 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18074 || it->phys_descent > it->descent);
18075 glyph->padding_p = 0;
18076 glyph->glyph_not_available_p = 0;
18077 glyph->face_id = it->face_id;
18078 glyph->u.cmp_id = it->cmp_id;
18079 glyph->font_type = FONT_TYPE_UNKNOWN;
18080 ++it->glyph_row->used[area];
18081 }
18082}
18083
18084
18085/* Change IT->ascent and IT->height according to the setting of
18086 IT->voffset. */
18087
18088static INLINE void
18089take_vertical_position_into_account (it)
18090 struct it *it;
18091{
18092 if (it->voffset)
18093 {
18094 if (it->voffset < 0)
18095 /* Increase the ascent so that we can display the text higher
18096 in the line. */
18097 it->ascent += abs (it->voffset);
18098 else
18099 /* Increase the descent so that we can display the text lower
18100 in the line. */
18101 it->descent += it->voffset;
18102 }
18103}
18104
18105
18106/* Produce glyphs/get display metrics for the image IT is loaded with.
18107 See the description of struct display_iterator in dispextern.h for
18108 an overview of struct display_iterator. */
18109
18110static void
18111produce_image_glyph (it)
18112 struct it *it;
18113{
18114 struct image *img;
18115 struct face *face;
493fdc3c 18116 int face_ascent, glyph_ascent;
133c764e
KS
18117
18118 xassert (it->what == IT_IMAGE);
18119
18120 face = FACE_FROM_ID (it->f, it->face_id);
dd341dd9
KS
18121 xassert (face);
18122 /* Make sure X resources of the face is loaded. */
18123 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18124
18125 if (it->image_id < 0)
18126 {
18127 /* Fringe bitmap. */
351b5434
KS
18128 it->ascent = it->phys_ascent = 0;
18129 it->descent = it->phys_descent = 0;
18130 it->pixel_width = 0;
dd341dd9
KS
18131 it->nglyphs = 0;
18132 return;
18133 }
18134
133c764e
KS
18135 img = IMAGE_FROM_ID (it->f, it->image_id);
18136 xassert (img);
dd341dd9 18137 /* Make sure X resources of the image is loaded. */
133c764e
KS
18138 prepare_image_for_display (it->f, img);
18139
493fdc3c 18140 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face);
133c764e
KS
18141 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
18142 it->pixel_width = img->width + 2 * img->hmargin;
18143
7d8a0b55
MB
18144 /* It's quite possible for images to have an ascent greater than
18145 their height, so don't get confused in that case. */
18146 if (it->descent < 0)
18147 it->descent = 0;
18148
493fdc3c
KS
18149 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
18150 face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
18151 if (face_ascent > it->ascent)
18152 it->ascent = it->phys_ascent = face_ascent;
18153
133c764e
KS
18154 it->nglyphs = 1;
18155
18156 if (face->box != FACE_NO_BOX)
18157 {
18158 if (face->box_line_width > 0)
18159 {
18160 it->ascent += face->box_line_width;
18161 it->descent += face->box_line_width;
18162 }
18163
18164 if (it->start_of_box_run_p)
18165 it->pixel_width += abs (face->box_line_width);
18166 if (it->end_of_box_run_p)
18167 it->pixel_width += abs (face->box_line_width);
18168 }
18169
18170 take_vertical_position_into_account (it);
18171
18172 if (it->glyph_row)
18173 {
18174 struct glyph *glyph;
18175 enum glyph_row_area area = it->area;
18176
18177 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18178 if (glyph < it->glyph_row->glyphs[area + 1])
18179 {
18180 glyph->charpos = CHARPOS (it->position);
18181 glyph->object = it->object;
18182 glyph->pixel_width = it->pixel_width;
493fdc3c
KS
18183 glyph->ascent = glyph_ascent;
18184 glyph->descent = it->descent;
133c764e
KS
18185 glyph->voffset = it->voffset;
18186 glyph->type = IMAGE_GLYPH;
18187 glyph->multibyte_p = it->multibyte_p;
18188 glyph->left_box_line_p = it->start_of_box_run_p;
18189 glyph->right_box_line_p = it->end_of_box_run_p;
18190 glyph->overlaps_vertically_p = 0;
18191 glyph->padding_p = 0;
18192 glyph->glyph_not_available_p = 0;
18193 glyph->face_id = it->face_id;
18194 glyph->u.img_id = img->id;
18195 glyph->font_type = FONT_TYPE_UNKNOWN;
18196 ++it->glyph_row->used[area];
18197 }
18198 }
18199}
18200
18201
18202/* Append a stretch glyph to IT->glyph_row. OBJECT is the source
18203 of the glyph, WIDTH and HEIGHT are the width and height of the
f65536fa 18204 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
133c764e
KS
18205
18206static void
18207append_stretch_glyph (it, object, width, height, ascent)
18208 struct it *it;
18209 Lisp_Object object;
18210 int width, height;
f65536fa 18211 int ascent;
133c764e
KS
18212{
18213 struct glyph *glyph;
18214 enum glyph_row_area area = it->area;
18215
f65536fa 18216 xassert (ascent >= 0 && ascent <= height);
133c764e
KS
18217
18218 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18219 if (glyph < it->glyph_row->glyphs[area + 1])
18220 {
18221 glyph->charpos = CHARPOS (it->position);
18222 glyph->object = object;
18223 glyph->pixel_width = width;
493fdc3c
KS
18224 glyph->ascent = ascent;
18225 glyph->descent = height - ascent;
133c764e
KS
18226 glyph->voffset = it->voffset;
18227 glyph->type = STRETCH_GLYPH;
18228 glyph->multibyte_p = it->multibyte_p;
18229 glyph->left_box_line_p = it->start_of_box_run_p;
18230 glyph->right_box_line_p = it->end_of_box_run_p;
18231 glyph->overlaps_vertically_p = 0;
18232 glyph->padding_p = 0;
18233 glyph->glyph_not_available_p = 0;
18234 glyph->face_id = it->face_id;
f65536fa 18235 glyph->u.stretch.ascent = ascent;
133c764e
KS
18236 glyph->u.stretch.height = height;
18237 glyph->font_type = FONT_TYPE_UNKNOWN;
18238 ++it->glyph_row->used[area];
18239 }
18240}
18241
18242
18243/* Produce a stretch glyph for iterator IT. IT->object is the value
18244 of the glyph property displayed. The value must be a list
18245 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
18246 being recognized:
18247
18248 1. `:width WIDTH' specifies that the space should be WIDTH *
18249 canonical char width wide. WIDTH may be an integer or floating
18250 point number.
18251
18252 2. `:relative-width FACTOR' specifies that the width of the stretch
18253 should be computed from the width of the first character having the
18254 `glyph' property, and should be FACTOR times that width.
18255
18256 3. `:align-to HPOS' specifies that the space should be wide enough
18257 to reach HPOS, a value in canonical character units.
18258
18259 Exactly one of the above pairs must be present.
18260
18261 4. `:height HEIGHT' specifies that the height of the stretch produced
18262 should be HEIGHT, measured in canonical character units.
18263
18264 5. `:relative-height FACTOR' specifies that the height of the
18265 stretch should be FACTOR times the height of the characters having
18266 the glyph property.
18267
18268 Either none or exactly one of 4 or 5 must be present.
18269
18270 6. `:ascent ASCENT' specifies that ASCENT percent of the height
18271 of the stretch should be used for the ascent of the stretch.
18272 ASCENT must be in the range 0 <= ASCENT <= 100. */
18273
133c764e
KS
18274static void
18275produce_stretch_glyph (it)
18276 struct it *it;
18277{
f65536fa 18278 /* (space :width WIDTH :height HEIGHT ...) */
133c764e 18279 Lisp_Object prop, plist;
da06e4d7 18280 int width = 0, height = 0, align_to = -1;
f65536fa
KS
18281 int zero_width_ok_p = 0, zero_height_ok_p = 0;
18282 int ascent = 0;
18283 double tem;
133c764e
KS
18284 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18285 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
18286
18287 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18288
18289 /* List should start with `space'. */
18290 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
18291 plist = XCDR (it->object);
18292
18293 /* Compute the width of the stretch. */
f65536fa 18294 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
da06e4d7 18295 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
f65536fa
KS
18296 {
18297 /* Absolute width `:width WIDTH' specified and valid. */
18298 zero_width_ok_p = 1;
18299 width = (int)tem;
18300 }
133c764e
KS
18301 else if (prop = Fplist_get (plist, QCrelative_width),
18302 NUMVAL (prop) > 0)
18303 {
18304 /* Relative width `:relative-width FACTOR' specified and valid.
18305 Compute the width of the characters having the `glyph'
18306 property. */
18307 struct it it2;
18308 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
18309
18310 it2 = *it;
18311 if (it->multibyte_p)
18312 {
18313 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
18314 - IT_BYTEPOS (*it));
18315 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
18316 }
18317 else
18318 it2.c = *p, it2.len = 1;
18319
18320 it2.glyph_row = NULL;
18321 it2.what = IT_CHARACTER;
18322 x_produce_glyphs (&it2);
18323 width = NUMVAL (prop) * it2.pixel_width;
18324 }
f65536fa 18325 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
da06e4d7
KS
18326 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
18327 {
18328 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
be21b925 18329 align_to = (align_to < 0
da06e4d7
KS
18330 ? 0
18331 : align_to - window_box_left_offset (it->w, TEXT_AREA));
18332 else if (align_to < 0)
18333 align_to = window_box_left_offset (it->w, TEXT_AREA);
18334 width = max (0, (int)tem + align_to - it->current_x);
f65536fa
KS
18335 zero_width_ok_p = 1;
18336 }
133c764e
KS
18337 else
18338 /* Nothing specified -> width defaults to canonical char width. */
da8b7f4f 18339 width = FRAME_COLUMN_WIDTH (it->f);
133c764e 18340
f65536fa
KS
18341 if (width <= 0 && (width < 0 || !zero_width_ok_p))
18342 width = 1;
18343
133c764e 18344 /* Compute height. */
f65536fa 18345 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
da06e4d7 18346 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
f65536fa
KS
18347 {
18348 height = (int)tem;
18349 zero_height_ok_p = 1;
18350 }
133c764e
KS
18351 else if (prop = Fplist_get (plist, QCrelative_height),
18352 NUMVAL (prop) > 0)
18353 height = FONT_HEIGHT (font) * NUMVAL (prop);
18354 else
18355 height = FONT_HEIGHT (font);
18356
f65536fa
KS
18357 if (height <= 0 && (height < 0 || !zero_height_ok_p))
18358 height = 1;
18359
133c764e
KS
18360 /* Compute percentage of height used for ascent. If
18361 `:ascent ASCENT' is present and valid, use that. Otherwise,
18362 derive the ascent from the font in use. */
18363 if (prop = Fplist_get (plist, QCascent),
18364 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
f65536fa
KS
18365 ascent = height * NUMVAL (prop) / 100.0;
18366 else if (!NILP (prop)
da06e4d7 18367 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
f65536fa 18368 ascent = min (max (0, (int)tem), height);
133c764e 18369 else
f65536fa 18370 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
133c764e 18371
f65536fa 18372 if (width > 0 && height > 0 && it->glyph_row)
133c764e
KS
18373 {
18374 Lisp_Object object = it->stack[it->sp - 1].string;
18375 if (!STRINGP (object))
18376 object = it->w->buffer;
18377 append_stretch_glyph (it, object, width, height, ascent);
18378 }
18379
18380 it->pixel_width = width;
f65536fa 18381 it->ascent = it->phys_ascent = ascent;
133c764e 18382 it->descent = it->phys_descent = height - it->ascent;
f65536fa 18383 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
133c764e 18384
f65536fa 18385 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
133c764e
KS
18386 {
18387 if (face->box_line_width > 0)
18388 {
18389 it->ascent += face->box_line_width;
18390 it->descent += face->box_line_width;
18391 }
18392
18393 if (it->start_of_box_run_p)
18394 it->pixel_width += abs (face->box_line_width);
18395 if (it->end_of_box_run_p)
18396 it->pixel_width += abs (face->box_line_width);
18397 }
18398
18399 take_vertical_position_into_account (it);
18400}
18401
18402/* RIF:
18403 Produce glyphs/get display metrics for the display element IT is
18404 loaded with. See the description of struct display_iterator in
18405 dispextern.h for an overview of struct display_iterator. */
18406
18407void
18408x_produce_glyphs (it)
18409 struct it *it;
18410{
18411 it->glyph_not_available_p = 0;
18412
18413 if (it->what == IT_CHARACTER)
18414 {
18415 XChar2b char2b;
18416 XFontStruct *font;
18417 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18418 XCharStruct *pcm;
18419 int font_not_found_p;
18420 struct font_info *font_info;
18421 int boff; /* baseline offset */
18422 /* We may change it->multibyte_p upon unibyte<->multibyte
18423 conversion. So, save the current value now and restore it
18424 later.
18425
18426 Note: It seems that we don't have to record multibyte_p in
18427 struct glyph because the character code itself tells if or
18428 not the character is multibyte. Thus, in the future, we must
18429 consider eliminating the field `multibyte_p' in the struct
18430 glyph. */
18431 int saved_multibyte_p = it->multibyte_p;
18432
18433 /* Maybe translate single-byte characters to multibyte, or the
18434 other way. */
18435 it->char_to_display = it->c;
18436 if (!ASCII_BYTE_P (it->c))
18437 {
18438 if (unibyte_display_via_language_environment
18439 && SINGLE_BYTE_CHAR_P (it->c)
18440 && (it->c >= 0240
18441 || !NILP (Vnonascii_translation_table)))
18442 {
18443 it->char_to_display = unibyte_char_to_multibyte (it->c);
18444 it->multibyte_p = 1;
18445 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18446 face = FACE_FROM_ID (it->f, it->face_id);
18447 }
18448 else if (!SINGLE_BYTE_CHAR_P (it->c)
18449 && !it->multibyte_p)
18450 {
18451 it->multibyte_p = 1;
18452 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18453 face = FACE_FROM_ID (it->f, it->face_id);
18454 }
18455 }
18456
18457 /* Get font to use. Encode IT->char_to_display. */
18458 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18459 &char2b, it->multibyte_p, 0);
18460 font = face->font;
18461
18462 /* When no suitable font found, use the default font. */
18463 font_not_found_p = font == NULL;
18464 if (font_not_found_p)
18465 {
18466 font = FRAME_FONT (it->f);
18467 boff = FRAME_BASELINE_OFFSET (it->f);
18468 font_info = NULL;
18469 }
18470 else
18471 {
18472 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18473 boff = font_info->baseline_offset;
18474 if (font_info->vertical_centering)
18475 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18476 }
18477
18478 if (it->char_to_display >= ' '
18479 && (!it->multibyte_p || it->char_to_display < 128))
18480 {
18481 /* Either unibyte or ASCII. */
18482 int stretched_p;
18483
18484 it->nglyphs = 1;
18485
18486 pcm = rif->per_char_metric (font, &char2b,
18487 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
18488 it->ascent = FONT_BASE (font) + boff;
18489 it->descent = FONT_DESCENT (font) - boff;
18490
18491 if (pcm)
18492 {
18493 it->phys_ascent = pcm->ascent + boff;
18494 it->phys_descent = pcm->descent - boff;
18495 it->pixel_width = pcm->width;
18496 }
18497 else
18498 {
18499 it->glyph_not_available_p = 1;
18500 it->phys_ascent = FONT_BASE (font) + boff;
18501 it->phys_descent = FONT_DESCENT (font) - boff;
18502 it->pixel_width = FONT_WIDTH (font);
18503 }
18504
18505 /* If this is a space inside a region of text with
18506 `space-width' property, change its width. */
18507 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
18508 if (stretched_p)
18509 it->pixel_width *= XFLOATINT (it->space_width);
18510
18511 /* If face has a box, add the box thickness to the character
18512 height. If character has a box line to the left and/or
18513 right, add the box line width to the character's width. */
18514 if (face->box != FACE_NO_BOX)
18515 {
18516 int thick = face->box_line_width;
18517
18518 if (thick > 0)
18519 {
18520 it->ascent += thick;
18521 it->descent += thick;
18522 }
18523 else
18524 thick = -thick;
18525
18526 if (it->start_of_box_run_p)
18527 it->pixel_width += thick;
18528 if (it->end_of_box_run_p)
18529 it->pixel_width += thick;
18530 }
18531
18532 /* If face has an overline, add the height of the overline
18533 (1 pixel) and a 1 pixel margin to the character height. */
18534 if (face->overline_p)
18535 it->ascent += 2;
18536
18537 take_vertical_position_into_account (it);
18538
18539 /* If we have to actually produce glyphs, do it. */
18540 if (it->glyph_row)
18541 {
18542 if (stretched_p)
18543 {
18544 /* Translate a space with a `space-width' property
18545 into a stretch glyph. */
f65536fa
KS
18546 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
18547 / FONT_HEIGHT (font));
133c764e
KS
18548 append_stretch_glyph (it, it->object, it->pixel_width,
18549 it->ascent + it->descent, ascent);
18550 }
18551 else
18552 append_glyph (it);
18553
18554 /* If characters with lbearing or rbearing are displayed
18555 in this line, record that fact in a flag of the
18556 glyph row. This is used to optimize X output code. */
18557 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
18558 it->glyph_row->contains_overlapping_glyphs_p = 1;
18559 }
18560 }
18561 else if (it->char_to_display == '\n')
18562 {
18563 /* A newline has no width but we need the height of the line. */
18564 it->pixel_width = 0;
18565 it->nglyphs = 0;
18566 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
18567 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
18568
18569 if (face->box != FACE_NO_BOX
18570 && face->box_line_width > 0)
18571 {
18572 it->ascent += face->box_line_width;
18573 it->descent += face->box_line_width;
18574 }
18575 }
18576 else if (it->char_to_display == '\t')
18577 {
da8b7f4f 18578 int tab_width = it->tab_width * FRAME_COLUMN_WIDTH (it->f);
133c764e
KS
18579 int x = it->current_x + it->continuation_lines_width;
18580 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
18581
18582 /* If the distance from the current position to the next tab
18583 stop is less than a canonical character width, use the
18584 tab stop after that. */
da8b7f4f 18585 if (next_tab_x - x < FRAME_COLUMN_WIDTH (it->f))
133c764e
KS
18586 next_tab_x += tab_width;
18587
18588 it->pixel_width = next_tab_x - x;
18589 it->nglyphs = 1;
18590 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
18591 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
18592
18593 if (it->glyph_row)
18594 {
133c764e 18595 append_stretch_glyph (it, it->object, it->pixel_width,
f65536fa 18596 it->ascent + it->descent, it->ascent);
133c764e
KS
18597 }
18598 }
18599 else
18600 {
18601 /* A multi-byte character. Assume that the display width of the
18602 character is the width of the character multiplied by the
18603 width of the font. */
18604
18605 /* If we found a font, this font should give us the right
18606 metrics. If we didn't find a font, use the frame's
18607 default font and calculate the width of the character
18608 from the charset width; this is what old redisplay code
18609 did. */
18610
18611 pcm = rif->per_char_metric (font, &char2b,
18612 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
18613
18614 if (font_not_found_p || !pcm)
18615 {
18616 int charset = CHAR_CHARSET (it->char_to_display);
18617
18618 it->glyph_not_available_p = 1;
da8b7f4f 18619 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
133c764e
KS
18620 * CHARSET_WIDTH (charset));
18621 it->phys_ascent = FONT_BASE (font) + boff;
18622 it->phys_descent = FONT_DESCENT (font) - boff;
18623 }
18624 else
18625 {
18626 it->pixel_width = pcm->width;
18627 it->phys_ascent = pcm->ascent + boff;
18628 it->phys_descent = pcm->descent - boff;
18629 if (it->glyph_row
18630 && (pcm->lbearing < 0
18631 || pcm->rbearing > pcm->width))
18632 it->glyph_row->contains_overlapping_glyphs_p = 1;
18633 }
18634 it->nglyphs = 1;
18635 it->ascent = FONT_BASE (font) + boff;
18636 it->descent = FONT_DESCENT (font) - boff;
18637 if (face->box != FACE_NO_BOX)
18638 {
18639 int thick = face->box_line_width;
18640
18641 if (thick > 0)
18642 {
18643 it->ascent += thick;
18644 it->descent += thick;
18645 }
18646 else
18647 thick = - thick;
18648
18649 if (it->start_of_box_run_p)
18650 it->pixel_width += thick;
18651 if (it->end_of_box_run_p)
18652 it->pixel_width += thick;
18653 }
18654
18655 /* If face has an overline, add the height of the overline
18656 (1 pixel) and a 1 pixel margin to the character height. */
18657 if (face->overline_p)
18658 it->ascent += 2;
18659
18660 take_vertical_position_into_account (it);
18661
18662 if (it->glyph_row)
18663 append_glyph (it);
18664 }
18665 it->multibyte_p = saved_multibyte_p;
18666 }
18667 else if (it->what == IT_COMPOSITION)
18668 {
18669 /* Note: A composition is represented as one glyph in the
18670 glyph matrix. There are no padding glyphs. */
18671 XChar2b char2b;
18672 XFontStruct *font;
18673 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18674 XCharStruct *pcm;
18675 int font_not_found_p;
18676 struct font_info *font_info;
18677 int boff; /* baseline offset */
18678 struct composition *cmp = composition_table[it->cmp_id];
18679
18680 /* Maybe translate single-byte characters to multibyte. */
18681 it->char_to_display = it->c;
18682 if (unibyte_display_via_language_environment
18683 && SINGLE_BYTE_CHAR_P (it->c)
18684 && (it->c >= 0240
18685 || (it->c >= 0200
18686 && !NILP (Vnonascii_translation_table))))
18687 {
18688 it->char_to_display = unibyte_char_to_multibyte (it->c);
18689 }
18690
18691 /* Get face and font to use. Encode IT->char_to_display. */
18692 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18693 face = FACE_FROM_ID (it->f, it->face_id);
18694 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18695 &char2b, it->multibyte_p, 0);
18696 font = face->font;
18697
18698 /* When no suitable font found, use the default font. */
18699 font_not_found_p = font == NULL;
18700 if (font_not_found_p)
18701 {
18702 font = FRAME_FONT (it->f);
18703 boff = FRAME_BASELINE_OFFSET (it->f);
18704 font_info = NULL;
18705 }
18706 else
18707 {
18708 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18709 boff = font_info->baseline_offset;
18710 if (font_info->vertical_centering)
18711 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18712 }
18713
18714 /* There are no padding glyphs, so there is only one glyph to
18715 produce for the composition. Important is that pixel_width,
18716 ascent and descent are the values of what is drawn by
18717 draw_glyphs (i.e. the values of the overall glyphs composed). */
18718 it->nglyphs = 1;
18719
18720 /* If we have not yet calculated pixel size data of glyphs of
18721 the composition for the current face font, calculate them
18722 now. Theoretically, we have to check all fonts for the
18723 glyphs, but that requires much time and memory space. So,
18724 here we check only the font of the first glyph. This leads
18725 to incorrect display very rarely, and C-l (recenter) can
18726 correct the display anyway. */
18727 if (cmp->font != (void *) font)
18728 {
18729 /* Ascent and descent of the font of the first character of
18730 this composition (adjusted by baseline offset). Ascent
18731 and descent of overall glyphs should not be less than
18732 them respectively. */
18733 int font_ascent = FONT_BASE (font) + boff;
18734 int font_descent = FONT_DESCENT (font) - boff;
18735 /* Bounding box of the overall glyphs. */
18736 int leftmost, rightmost, lowest, highest;
18737 int i, width, ascent, descent;
18738
18739 cmp->font = (void *) font;
18740
18741 /* Initialize the bounding box. */
18742 if (font_info
18743 && (pcm = rif->per_char_metric (font, &char2b,
18744 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
18745 {
18746 width = pcm->width;
18747 ascent = pcm->ascent;
18748 descent = pcm->descent;
18749 }
18750 else
18751 {
18752 width = FONT_WIDTH (font);
18753 ascent = FONT_BASE (font);
18754 descent = FONT_DESCENT (font);
18755 }
18756
18757 rightmost = width;
18758 lowest = - descent + boff;
18759 highest = ascent + boff;
18760 leftmost = 0;
18761
18762 if (font_info
18763 && font_info->default_ascent
18764 && CHAR_TABLE_P (Vuse_default_ascent)
18765 && !NILP (Faref (Vuse_default_ascent,
18766 make_number (it->char_to_display))))
18767 highest = font_info->default_ascent + boff;
18768
18769 /* Draw the first glyph at the normal position. It may be
18770 shifted to right later if some other glyphs are drawn at
18771 the left. */
18772 cmp->offsets[0] = 0;
18773 cmp->offsets[1] = boff;
18774
18775 /* Set cmp->offsets for the remaining glyphs. */
18776 for (i = 1; i < cmp->glyph_len; i++)
18777 {
18778 int left, right, btm, top;
18779 int ch = COMPOSITION_GLYPH (cmp, i);
18780 int face_id = FACE_FOR_CHAR (it->f, face, ch);
18781
18782 face = FACE_FROM_ID (it->f, face_id);
18783 get_char_face_and_encoding (it->f, ch, face->id,
18784 &char2b, it->multibyte_p, 0);
18785 font = face->font;
18786 if (font == NULL)
18787 {
18788 font = FRAME_FONT (it->f);
2a6d0874 18789 boff = FRAME_BASELINE_OFFSET (it->f);
133c764e
KS
18790 font_info = NULL;
18791 }
18792 else
18793 {
18794 font_info
18795 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18796 boff = font_info->baseline_offset;
18797 if (font_info->vertical_centering)
18798 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18799 }
18800
18801 if (font_info
2a6d0874 18802 && (pcm = rif->per_char_metric (font, &char2b,
133c764e
KS
18803 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
18804 {
18805 width = pcm->width;
18806 ascent = pcm->ascent;
18807 descent = pcm->descent;
18808 }
18809 else
18810 {
18811 width = FONT_WIDTH (font);
18812 ascent = 1;
18813 descent = 0;
18814 }
18815
18816 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
18817 {
18818 /* Relative composition with or without
18819 alternate chars. */
18820 left = (leftmost + rightmost - width) / 2;
18821 btm = - descent + boff;
18822 if (font_info && font_info->relative_compose
18823 && (! CHAR_TABLE_P (Vignore_relative_composition)
18824 || NILP (Faref (Vignore_relative_composition,
18825 make_number (ch)))))
18826 {
18827
18828 if (- descent >= font_info->relative_compose)
18829 /* One extra pixel between two glyphs. */
18830 btm = highest + 1;
18831 else if (ascent <= 0)
18832 /* One extra pixel between two glyphs. */
18833 btm = lowest - 1 - ascent - descent;
18834 }
18835 }
18836 else
18837 {
18838 /* A composition rule is specified by an integer
18839 value that encodes global and new reference
18840 points (GREF and NREF). GREF and NREF are
18841 specified by numbers as below:
18842
18843 0---1---2 -- ascent
18844 | |
18845 | |
18846 | |
18847 9--10--11 -- center
18848 | |
18849 ---3---4---5--- baseline
18850 | |
18851 6---7---8 -- descent
18852 */
18853 int rule = COMPOSITION_RULE (cmp, i);
18854 int gref, nref, grefx, grefy, nrefx, nrefy;
18855
18856 COMPOSITION_DECODE_RULE (rule, gref, nref);
18857 grefx = gref % 3, nrefx = nref % 3;
18858 grefy = gref / 3, nrefy = nref / 3;
18859
18860 left = (leftmost
18861 + grefx * (rightmost - leftmost) / 2
18862 - nrefx * width / 2);
18863 btm = ((grefy == 0 ? highest
18864 : grefy == 1 ? 0
18865 : grefy == 2 ? lowest
18866 : (highest + lowest) / 2)
18867 - (nrefy == 0 ? ascent + descent
18868 : nrefy == 1 ? descent - boff
18869 : nrefy == 2 ? 0
18870 : (ascent + descent) / 2));
18871 }
18872
18873 cmp->offsets[i * 2] = left;
18874 cmp->offsets[i * 2 + 1] = btm + descent;
18875
18876 /* Update the bounding box of the overall glyphs. */
18877 right = left + width;
18878 top = btm + descent + ascent;
18879 if (left < leftmost)
18880 leftmost = left;
18881 if (right > rightmost)
18882 rightmost = right;
18883 if (top > highest)
18884 highest = top;
18885 if (btm < lowest)
18886 lowest = btm;
18887 }
18888
18889 /* If there are glyphs whose x-offsets are negative,
18890 shift all glyphs to the right and make all x-offsets
18891 non-negative. */
18892 if (leftmost < 0)
18893 {
18894 for (i = 0; i < cmp->glyph_len; i++)
18895 cmp->offsets[i * 2] -= leftmost;
18896 rightmost -= leftmost;
18897 }
18898
18899 cmp->pixel_width = rightmost;
18900 cmp->ascent = highest;
18901 cmp->descent = - lowest;
18902 if (cmp->ascent < font_ascent)
18903 cmp->ascent = font_ascent;
18904 if (cmp->descent < font_descent)
18905 cmp->descent = font_descent;
18906 }
18907
18908 it->pixel_width = cmp->pixel_width;
18909 it->ascent = it->phys_ascent = cmp->ascent;
18910 it->descent = it->phys_descent = cmp->descent;
18911
18912 if (face->box != FACE_NO_BOX)
18913 {
18914 int thick = face->box_line_width;
18915
18916 if (thick > 0)
18917 {
18918 it->ascent += thick;
18919 it->descent += thick;
18920 }
18921 else
18922 thick = - thick;
18923
18924 if (it->start_of_box_run_p)
18925 it->pixel_width += thick;
18926 if (it->end_of_box_run_p)
18927 it->pixel_width += thick;
18928 }
18929
18930 /* If face has an overline, add the height of the overline
18931 (1 pixel) and a 1 pixel margin to the character height. */
18932 if (face->overline_p)
18933 it->ascent += 2;
18934
18935 take_vertical_position_into_account (it);
18936
18937 if (it->glyph_row)
18938 append_composite_glyph (it);
18939 }
18940 else if (it->what == IT_IMAGE)
18941 produce_image_glyph (it);
18942 else if (it->what == IT_STRETCH)
18943 produce_stretch_glyph (it);
18944
18945 /* Accumulate dimensions. Note: can't assume that it->descent > 0
18946 because this isn't true for images with `:ascent 100'. */
18947 xassert (it->ascent >= 0 && it->descent >= 0);
18948 if (it->area == TEXT_AREA)
18949 it->current_x += it->pixel_width;
18950
18951 it->descent += it->extra_line_spacing;
18952
18953 it->max_ascent = max (it->max_ascent, it->ascent);
18954 it->max_descent = max (it->max_descent, it->descent);
18955 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
18956 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
18957}
18958
fa3c6b4d
KS
18959/* EXPORT for RIF:
18960 Output LEN glyphs starting at START at the nominal cursor position.
18961 Advance the nominal cursor over the text. The global variable
18962 updated_window contains the window being updated, updated_row is
18963 the glyph row being updated, and updated_area is the area of that
18964 row being updated. */
133c764e 18965
fa3c6b4d
KS
18966void
18967x_write_glyphs (start, len)
18968 struct glyph *start;
18969 int len;
18970{
18971 int x, hpos;
133c764e 18972
fa3c6b4d
KS
18973 xassert (updated_window && updated_row);
18974 BLOCK_INPUT;
133c764e 18975
fa3c6b4d 18976 /* Write glyphs. */
cfe03a41 18977
fa3c6b4d
KS
18978 hpos = start - updated_row->glyphs[updated_area];
18979 x = draw_glyphs (updated_window, output_cursor.x,
18980 updated_row, updated_area,
18981 hpos, hpos + len,
18982 DRAW_NORMAL_TEXT, 0);
cfe03a41 18983
fa3c6b4d
KS
18984 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
18985 if (updated_area == TEXT_AREA
18986 && updated_window->phys_cursor_on_p
18987 && updated_window->phys_cursor.vpos == output_cursor.vpos
18988 && updated_window->phys_cursor.hpos >= hpos
18989 && updated_window->phys_cursor.hpos < hpos + len)
18990 updated_window->phys_cursor_on_p = 0;
cfe03a41 18991
fa3c6b4d 18992 UNBLOCK_INPUT;
cfe03a41 18993
fa3c6b4d
KS
18994 /* Advance the output cursor. */
18995 output_cursor.hpos += len;
18996 output_cursor.x = x;
18997}
cfe03a41 18998
cfe03a41 18999
fa3c6b4d
KS
19000/* EXPORT for RIF:
19001 Insert LEN glyphs from START at the nominal cursor position. */
19002
19003void
19004x_insert_glyphs (start, len)
19005 struct glyph *start;
19006 int len;
19007{
19008 struct frame *f;
19009 struct window *w;
19010 int line_height, shift_by_width, shifted_region_width;
19011 struct glyph_row *row;
19012 struct glyph *glyph;
19013 int frame_x, frame_y, hpos;
19014
19015 xassert (updated_window && updated_row);
19016 BLOCK_INPUT;
19017 w = updated_window;
19018 f = XFRAME (WINDOW_FRAME (w));
19019
19020 /* Get the height of the line we are in. */
19021 row = updated_row;
19022 line_height = row->height;
19023
19024 /* Get the width of the glyphs to insert. */
19025 shift_by_width = 0;
19026 for (glyph = start; glyph < start + len; ++glyph)
19027 shift_by_width += glyph->pixel_width;
19028
19029 /* Get the width of the region to shift right. */
19030 shifted_region_width = (window_box_width (w, updated_area)
19031 - output_cursor.x
19032 - shift_by_width);
19033
19034 /* Shift right. */
19035 frame_x = window_box_left (w, updated_area) + output_cursor.x;
19036 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
19037
7b7b454e
AS
19038 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
19039 line_height, shift_by_width);
fa3c6b4d
KS
19040
19041 /* Write the glyphs. */
19042 hpos = start - row->glyphs[updated_area];
19043 draw_glyphs (w, output_cursor.x, row, updated_area,
19044 hpos, hpos + len,
19045 DRAW_NORMAL_TEXT, 0);
19046
19047 /* Advance the output cursor. */
19048 output_cursor.hpos += len;
19049 output_cursor.x += shift_by_width;
19050 UNBLOCK_INPUT;
19051}
19052
19053
19054/* EXPORT for RIF:
19055 Erase the current text line from the nominal cursor position
19056 (inclusive) to pixel column TO_X (exclusive). The idea is that
19057 everything from TO_X onward is already erased.
19058
19059 TO_X is a pixel position relative to updated_area of
19060 updated_window. TO_X == -1 means clear to the end of this area. */
19061
19062void
19063x_clear_end_of_line (to_x)
19064 int to_x;
19065{
19066 struct frame *f;
19067 struct window *w = updated_window;
19068 int max_x, min_y, max_y;
19069 int from_x, from_y, to_y;
19070
19071 xassert (updated_window && updated_row);
19072 f = XFRAME (w->frame);
19073
19074 if (updated_row->full_width_p)
da8b7f4f 19075 max_x = WINDOW_TOTAL_WIDTH (w);
fa3c6b4d
KS
19076 else
19077 max_x = window_box_width (w, updated_area);
19078 max_y = window_text_bottom_y (w);
19079
19080 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
19081 of window. For TO_X > 0, truncate to end of drawing area. */
19082 if (to_x == 0)
19083 return;
19084 else if (to_x < 0)
19085 to_x = max_x;
19086 else
19087 to_x = min (to_x, max_x);
19088
19089 to_y = min (max_y, output_cursor.y + updated_row->height);
19090
19091 /* Notice if the cursor will be cleared by this operation. */
19092 if (!updated_row->full_width_p)
19093 notice_overwritten_cursor (w, updated_area,
19094 output_cursor.x, -1,
19095 updated_row->y,
19096 MATRIX_ROW_BOTTOM_Y (updated_row));
19097
19098 from_x = output_cursor.x;
19099
19100 /* Translate to frame coordinates. */
19101 if (updated_row->full_width_p)
19102 {
19103 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
19104 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
19105 }
19106 else
19107 {
da8b7f4f
KS
19108 int area_left = window_box_left (w, updated_area);
19109 from_x += area_left;
19110 to_x += area_left;
fa3c6b4d
KS
19111 }
19112
da8b7f4f 19113 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
fa3c6b4d
KS
19114 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
19115 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
19116
19117 /* Prevent inadvertently clearing to end of the X window. */
19118 if (to_x > from_x && to_y > from_y)
19119 {
19120 BLOCK_INPUT;
19121 rif->clear_frame_area (f, from_x, from_y,
19122 to_x - from_x, to_y - from_y);
19123 UNBLOCK_INPUT;
19124 }
19125}
19126
19127#endif /* HAVE_WINDOW_SYSTEM */
19128
19129
19130\f
19131/***********************************************************************
19132 Cursor types
19133 ***********************************************************************/
19134
19135/* Value is the internal representation of the specified cursor type
19136 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
19137 of the bar cursor. */
19138
f65536fa 19139static enum text_cursor_kinds
fa3c6b4d
KS
19140get_specified_cursor_type (arg, width)
19141 Lisp_Object arg;
19142 int *width;
19143{
19144 enum text_cursor_kinds type;
19145
19146 if (NILP (arg))
19147 return NO_CURSOR;
19148
19149 if (EQ (arg, Qbox))
19150 return FILLED_BOX_CURSOR;
19151
19152 if (EQ (arg, Qhollow))
19153 return HOLLOW_BOX_CURSOR;
19154
19155 if (EQ (arg, Qbar))
19156 {
19157 *width = 2;
19158 return BAR_CURSOR;
19159 }
19160
19161 if (CONSP (arg)
19162 && EQ (XCAR (arg), Qbar)
19163 && INTEGERP (XCDR (arg))
19164 && XINT (XCDR (arg)) >= 0)
19165 {
19166 *width = XINT (XCDR (arg));
19167 return BAR_CURSOR;
19168 }
19169
19170 if (EQ (arg, Qhbar))
19171 {
19172 *width = 2;
19173 return HBAR_CURSOR;
19174 }
19175
19176 if (CONSP (arg)
19177 && EQ (XCAR (arg), Qhbar)
19178 && INTEGERP (XCDR (arg))
19179 && XINT (XCDR (arg)) >= 0)
19180 {
19181 *width = XINT (XCDR (arg));
19182 return HBAR_CURSOR;
19183 }
19184
19185 /* Treat anything unknown as "hollow box cursor".
19186 It was bad to signal an error; people have trouble fixing
19187 .Xdefaults with Emacs, when it has something bad in it. */
19188 type = HOLLOW_BOX_CURSOR;
19189
19190 return type;
19191}
19192
19193/* Set the default cursor types for specified frame. */
19194void
19195set_frame_cursor_types (f, arg)
19196 struct frame *f;
19197 Lisp_Object arg;
19198{
19199 int width;
19200 Lisp_Object tem;
19201
19202 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
19203 FRAME_CURSOR_WIDTH (f) = width;
19204
19205 /* By default, set up the blink-off state depending on the on-state. */
19206
19207 tem = Fassoc (arg, Vblink_cursor_alist);
19208 if (!NILP (tem))
19209 {
19210 FRAME_BLINK_OFF_CURSOR (f)
19211 = get_specified_cursor_type (XCDR (tem), &width);
19212 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
19213 }
19214 else
19215 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
19216}
19217
19218
19219/* Return the cursor we want to be displayed in window W. Return
19220 width of bar/hbar cursor through WIDTH arg. Return with
19221 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
19222 (i.e. if the `system caret' should track this cursor).
19223
19224 In a mini-buffer window, we want the cursor only to appear if we
19225 are reading input from this window. For the selected window, we
19226 want the cursor type given by the frame parameter or buffer local
19227 setting of cursor-type. If explicitly marked off, draw no cursor.
19228 In all other cases, we want a hollow box cursor. */
19229
f65536fa
KS
19230static enum text_cursor_kinds
19231get_window_cursor_type (w, glyph, width, active_cursor)
fa3c6b4d 19232 struct window *w;
f65536fa 19233 struct glyph *glyph;
fa3c6b4d
KS
19234 int *width;
19235 int *active_cursor;
19236{
19237 struct frame *f = XFRAME (w->frame);
19238 struct buffer *b = XBUFFER (w->buffer);
19239 int cursor_type = DEFAULT_CURSOR;
19240 Lisp_Object alt_cursor;
19241 int non_selected = 0;
19242
19243 *active_cursor = 1;
19244
19245 /* Echo area */
19246 if (cursor_in_echo_area
19247 && FRAME_HAS_MINIBUF_P (f)
19248 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
19249 {
19250 if (w == XWINDOW (echo_area_window))
19251 {
19252 *width = FRAME_CURSOR_WIDTH (f);
19253 return FRAME_DESIRED_CURSOR (f);
19254 }
19255
19256 *active_cursor = 0;
19257 non_selected = 1;
19258 }
19259
19260 /* Nonselected window or nonselected frame. */
19261 else if (w != XWINDOW (f->selected_window)
19262#ifdef HAVE_WINDOW_SYSTEM
19263 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
19264#endif
19265 )
19266 {
19267 *active_cursor = 0;
19268
19269 if (MINI_WINDOW_P (w) && minibuf_level == 0)
19270 return NO_CURSOR;
19271
19272 non_selected = 1;
19273 }
19274
19275 /* Never display a cursor in a window in which cursor-type is nil. */
19276 if (NILP (b->cursor_type))
19277 return NO_CURSOR;
19278
19279 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
19280 if (non_selected)
19281 {
19282 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
19283 return get_specified_cursor_type (alt_cursor, width);
19284 }
19285
19286 /* Get the normal cursor type for this window. */
19287 if (EQ (b->cursor_type, Qt))
19288 {
19289 cursor_type = FRAME_DESIRED_CURSOR (f);
19290 *width = FRAME_CURSOR_WIDTH (f);
19291 }
19292 else
19293 cursor_type = get_specified_cursor_type (b->cursor_type, width);
19294
19295 /* Use normal cursor if not blinked off. */
1d712183 19296 if (!w->cursor_off_p)
f65536fa 19297 {
1d712183 19298 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
f65536fa
KS
19299 if (cursor_type == FILLED_BOX_CURSOR)
19300 cursor_type = HOLLOW_BOX_CURSOR;
19301 }
19302 return cursor_type;
19303 }
fa3c6b4d
KS
19304
19305 /* Cursor is blinked off, so determine how to "toggle" it. */
19306
19307 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
19308 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
19309 return get_specified_cursor_type (XCDR (alt_cursor), width);
19310
19311 /* Then see if frame has specified a specific blink off cursor type. */
19312 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
19313 {
19314 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
19315 return FRAME_BLINK_OFF_CURSOR (f);
19316 }
19317
a57dd8b1
KS
19318#if 0
19319 /* Some people liked having a permanently visible blinking cursor,
19320 while others had very strong opinions against it. So it was
19321 decided to remove it. KFS 2003-09-03 */
19322
fa3c6b4d
KS
19323 /* Finally perform built-in cursor blinking:
19324 filled box <-> hollow box
19325 wide [h]bar <-> narrow [h]bar
19326 narrow [h]bar <-> no cursor
19327 other type <-> no cursor */
19328
19329 if (cursor_type == FILLED_BOX_CURSOR)
19330 return HOLLOW_BOX_CURSOR;
19331
19332 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
19333 {
19334 *width = 1;
19335 return cursor_type;
19336 }
a57dd8b1 19337#endif
fa3c6b4d
KS
19338
19339 return NO_CURSOR;
19340}
19341
19342
19343#ifdef HAVE_WINDOW_SYSTEM
19344
19345/* Notice when the text cursor of window W has been completely
19346 overwritten by a drawing operation that outputs glyphs in AREA
19347 starting at X0 and ending at X1 in the line starting at Y0 and
19348 ending at Y1. X coordinates are area-relative. X1 < 0 means all
19349 the rest of the line after X0 has been written. Y coordinates
19350 are window-relative. */
19351
19352static void
19353notice_overwritten_cursor (w, area, x0, x1, y0, y1)
19354 struct window *w;
19355 enum glyph_row_area area;
19356 int x0, y0, x1, y1;
19357{
88e6b646
KS
19358 int cx0, cx1, cy0, cy1;
19359 struct glyph_row *row;
fa3c6b4d 19360
88e6b646
KS
19361 if (!w->phys_cursor_on_p)
19362 return;
19363 if (area != TEXT_AREA)
19364 return;
fa3c6b4d 19365
88e6b646
KS
19366 row = w->current_matrix->rows + w->phys_cursor.vpos;
19367 if (!row->displays_text_p)
19368 return;
fa3c6b4d 19369
88e6b646
KS
19370 if (row->cursor_in_fringe_p)
19371 {
19372 row->cursor_in_fringe_p = 0;
19373 draw_fringe_bitmap (w, row, 0);
19374 w->phys_cursor_on_p = 0;
19375 return;
fa3c6b4d 19376 }
88e6b646
KS
19377
19378 cx0 = w->phys_cursor.x;
19379 cx1 = cx0 + w->phys_cursor_width;
19380 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
19381 return;
19382
19383 /* The cursor image will be completely removed from the
19384 screen if the output area intersects the cursor area in
19385 y-direction. When we draw in [y0 y1[, and some part of
19386 the cursor is at y < y0, that part must have been drawn
19387 before. When scrolling, the cursor is erased before
19388 actually scrolling, so we don't come here. When not
19389 scrolling, the rows above the old cursor row must have
19390 changed, and in this case these rows must have written
19391 over the cursor image.
19392
19393 Likewise if part of the cursor is below y1, with the
19394 exception of the cursor being in the first blank row at
19395 the buffer and window end because update_text_area
19396 doesn't draw that row. (Except when it does, but
19397 that's handled in update_text_area.) */
19398
19399 cy0 = w->phys_cursor.y;
19400 cy1 = cy0 + w->phys_cursor_height;
19401 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
19402 return;
19403
19404 w->phys_cursor_on_p = 0;
fa3c6b4d
KS
19405}
19406
19407#endif /* HAVE_WINDOW_SYSTEM */
19408
19409\f
19410/************************************************************************
19411 Mouse Face
19412 ************************************************************************/
19413
19414#ifdef HAVE_WINDOW_SYSTEM
19415
19416/* EXPORT for RIF:
19417 Fix the display of area AREA of overlapping row ROW in window W. */
19418
19419void
19420x_fix_overlapping_area (w, row, area)
19421 struct window *w;
19422 struct glyph_row *row;
19423 enum glyph_row_area area;
19424{
19425 int i, x;
19426
19427 BLOCK_INPUT;
19428
57dd22f4 19429 x = 0;
fa3c6b4d
KS
19430 for (i = 0; i < row->used[area];)
19431 {
19432 if (row->glyphs[area][i].overlaps_vertically_p)
19433 {
19434 int start = i, start_x = x;
19435
19436 do
19437 {
19438 x += row->glyphs[area][i].pixel_width;
19439 ++i;
19440 }
19441 while (i < row->used[area]
19442 && row->glyphs[area][i].overlaps_vertically_p);
19443
19444 draw_glyphs (w, start_x, row, area,
19445 start, i,
19446 DRAW_NORMAL_TEXT, 1);
19447 }
19448 else
19449 {
19450 x += row->glyphs[area][i].pixel_width;
19451 ++i;
19452 }
19453 }
19454
19455 UNBLOCK_INPUT;
19456}
19457
19458
19459/* EXPORT:
19460 Draw the cursor glyph of window W in glyph row ROW. See the
19461 comment of draw_glyphs for the meaning of HL. */
19462
19463void
19464draw_phys_cursor_glyph (w, row, hl)
19465 struct window *w;
19466 struct glyph_row *row;
19467 enum draw_glyphs_face hl;
19468{
19469 /* If cursor hpos is out of bounds, don't draw garbage. This can
19470 happen in mini-buffer windows when switching between echo area
19471 glyphs and mini-buffer. */
19472 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
19473 {
19474 int on_p = w->phys_cursor_on_p;
19475 int x1;
19476 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
19477 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
19478 hl, 0);
19479 w->phys_cursor_on_p = on_p;
19480
fa3c6b4d
KS
19481 if (hl == DRAW_CURSOR)
19482 w->phys_cursor_width = x1 - w->phys_cursor.x;
fa3c6b4d
KS
19483 /* When we erase the cursor, and ROW is overlapped by other
19484 rows, make sure that these overlapping parts of other rows
19485 are redrawn. */
365fa1b3 19486 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
fa3c6b4d
KS
19487 {
19488 if (row > w->current_matrix->rows
19489 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
19490 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
19491
19492 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
19493 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
19494 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
19495 }
19496 }
19497}
19498
19499
19500/* EXPORT:
19501 Erase the image of a cursor of window W from the screen. */
19502
19503void
19504erase_phys_cursor (w)
19505 struct window *w;
19506{
19507 struct frame *f = XFRAME (w->frame);
19508 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
19509 int hpos = w->phys_cursor.hpos;
19510 int vpos = w->phys_cursor.vpos;
19511 int mouse_face_here_p = 0;
19512 struct glyph_matrix *active_glyphs = w->current_matrix;
19513 struct glyph_row *cursor_row;
19514 struct glyph *cursor_glyph;
19515 enum draw_glyphs_face hl;
19516
19517 /* No cursor displayed or row invalidated => nothing to do on the
19518 screen. */
19519 if (w->phys_cursor_type == NO_CURSOR)
19520 goto mark_cursor_off;
19521
19522 /* VPOS >= active_glyphs->nrows means that window has been resized.
19523 Don't bother to erase the cursor. */
19524 if (vpos >= active_glyphs->nrows)
19525 goto mark_cursor_off;
19526
19527 /* If row containing cursor is marked invalid, there is nothing we
19528 can do. */
19529 cursor_row = MATRIX_ROW (active_glyphs, vpos);
19530 if (!cursor_row->enabled_p)
19531 goto mark_cursor_off;
19532
19533 /* If row is completely invisible, don't attempt to delete a cursor which
19534 isn't there. This can happen if cursor is at top of a window, and
19535 we switch to a buffer with a header line in that window. */
19536 if (cursor_row->visible_height <= 0)
19537 goto mark_cursor_off;
19538
88e6b646
KS
19539 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
19540 if (cursor_row->cursor_in_fringe_p)
19541 {
19542 cursor_row->cursor_in_fringe_p = 0;
19543 draw_fringe_bitmap (w, cursor_row, 0);
19544 goto mark_cursor_off;
19545 }
19546
fa3c6b4d
KS
19547 /* This can happen when the new row is shorter than the old one.
19548 In this case, either draw_glyphs or clear_end_of_line
19549 should have cleared the cursor. Note that we wouldn't be
19550 able to erase the cursor in this case because we don't have a
19551 cursor glyph at hand. */
19552 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
19553 goto mark_cursor_off;
19554
19555 /* If the cursor is in the mouse face area, redisplay that when
19556 we clear the cursor. */
19557 if (! NILP (dpyinfo->mouse_face_window)
19558 && w == XWINDOW (dpyinfo->mouse_face_window)
19559 && (vpos > dpyinfo->mouse_face_beg_row
19560 || (vpos == dpyinfo->mouse_face_beg_row
19561 && hpos >= dpyinfo->mouse_face_beg_col))
19562 && (vpos < dpyinfo->mouse_face_end_row
19563 || (vpos == dpyinfo->mouse_face_end_row
19564 && hpos < dpyinfo->mouse_face_end_col))
19565 /* Don't redraw the cursor's spot in mouse face if it is at the
19566 end of a line (on a newline). The cursor appears there, but
19567 mouse highlighting does not. */
19568 && cursor_row->used[TEXT_AREA] > hpos)
19569 mouse_face_here_p = 1;
19570
19571 /* Maybe clear the display under the cursor. */
19572 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
19573 {
19574 int x, y;
da8b7f4f 19575 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
fa3c6b4d
KS
19576
19577 cursor_glyph = get_phys_cursor_glyph (w);
19578 if (cursor_glyph == NULL)
19579 goto mark_cursor_off;
19580
19581 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
19582 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
19583
19584 rif->clear_frame_area (f, x, y,
19585 cursor_glyph->pixel_width, cursor_row->visible_height);
19586 }
19587
19588 /* Erase the cursor by redrawing the character underneath it. */
19589 if (mouse_face_here_p)
19590 hl = DRAW_MOUSE_FACE;
19591 else
19592 hl = DRAW_NORMAL_TEXT;
19593 draw_phys_cursor_glyph (w, cursor_row, hl);
19594
19595 mark_cursor_off:
19596 w->phys_cursor_on_p = 0;
19597 w->phys_cursor_type = NO_CURSOR;
19598}
19599
19600
19601/* EXPORT:
19602 Display or clear cursor of window W. If ON is zero, clear the
19603 cursor. If it is non-zero, display the cursor. If ON is nonzero,
19604 where to put the cursor is specified by HPOS, VPOS, X and Y. */
19605
19606void
19607display_and_set_cursor (w, on, hpos, vpos, x, y)
19608 struct window *w;
19609 int on, hpos, vpos, x, y;
19610{
19611 struct frame *f = XFRAME (w->frame);
19612 int new_cursor_type;
19613 int new_cursor_width;
19614 int active_cursor;
fa3c6b4d
KS
19615 struct glyph_row *glyph_row;
19616 struct glyph *glyph;
19617
19618 /* This is pointless on invisible frames, and dangerous on garbaged
19619 windows and frames; in the latter case, the frame or window may
19620 be in the midst of changing its size, and x and y may be off the
19621 window. */
19622 if (! FRAME_VISIBLE_P (f)
19623 || FRAME_GARBAGED_P (f)
19624 || vpos >= w->current_matrix->nrows
19625 || hpos >= w->current_matrix->matrix_w)
19626 return;
19627
19628 /* If cursor is off and we want it off, return quickly. */
19629 if (!on && !w->phys_cursor_on_p)
19630 return;
19631
1d712183 19632 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
fa3c6b4d
KS
19633 /* If cursor row is not enabled, we don't really know where to
19634 display the cursor. */
19635 if (!glyph_row->enabled_p)
19636 {
19637 w->phys_cursor_on_p = 0;
19638 return;
19639 }
19640
1d712183
KS
19641 glyph = NULL;
19642 if (!glyph_row->exact_window_width_line_p
19643 || hpos < glyph_row->used[TEXT_AREA])
19644 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
19645
fa3c6b4d
KS
19646 xassert (interrupt_input_blocked);
19647
19648 /* Set new_cursor_type to the cursor we want to be displayed. */
f65536fa
KS
19649 new_cursor_type = get_window_cursor_type (w, glyph,
19650 &new_cursor_width, &active_cursor);
fa3c6b4d
KS
19651
19652 /* If cursor is currently being shown and we don't want it to be or
19653 it is in the wrong place, or the cursor type is not what we want,
19654 erase it. */
19655 if (w->phys_cursor_on_p
19656 && (!on
19657 || w->phys_cursor.x != x
19658 || w->phys_cursor.y != y
19659 || new_cursor_type != w->phys_cursor_type
19660 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
19661 && new_cursor_width != w->phys_cursor_width)))
19662 erase_phys_cursor (w);
19663
19664 /* Don't check phys_cursor_on_p here because that flag is only set
19665 to zero in some cases where we know that the cursor has been
19666 completely erased, to avoid the extra work of erasing the cursor
19667 twice. In other words, phys_cursor_on_p can be 1 and the cursor
19668 still not be visible, or it has only been partly erased. */
19669 if (on)
19670 {
19671 w->phys_cursor_ascent = glyph_row->ascent;
19672 w->phys_cursor_height = glyph_row->height;
19673
19674 /* Set phys_cursor_.* before x_draw_.* is called because some
19675 of them may need the information. */
19676 w->phys_cursor.x = x;
19677 w->phys_cursor.y = glyph_row->y;
19678 w->phys_cursor.hpos = hpos;
19679 w->phys_cursor.vpos = vpos;
19680 }
19681
b4ebbb12
KS
19682 rif->draw_window_cursor (w, glyph_row, x, y,
19683 new_cursor_type, new_cursor_width,
19684 on, active_cursor);
fa3c6b4d
KS
19685}
19686
19687
19688/* Switch the display of W's cursor on or off, according to the value
19689 of ON. */
19690
19691static void
19692update_window_cursor (w, on)
19693 struct window *w;
19694 int on;
19695{
19696 /* Don't update cursor in windows whose frame is in the process
19697 of being deleted. */
19698 if (w->current_matrix)
19699 {
19700 BLOCK_INPUT;
19701 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
19702 w->phys_cursor.x, w->phys_cursor.y);
19703 UNBLOCK_INPUT;
19704 }
19705}
19706
19707
19708/* Call update_window_cursor with parameter ON_P on all leaf windows
19709 in the window tree rooted at W. */
19710
19711static void
19712update_cursor_in_window_tree (w, on_p)
19713 struct window *w;
19714 int on_p;
19715{
19716 while (w)
19717 {
19718 if (!NILP (w->hchild))
19719 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
19720 else if (!NILP (w->vchild))
19721 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
19722 else
19723 update_window_cursor (w, on_p);
19724
19725 w = NILP (w->next) ? 0 : XWINDOW (w->next);
19726 }
19727}
19728
19729
19730/* EXPORT:
19731 Display the cursor on window W, or clear it, according to ON_P.
19732 Don't change the cursor's position. */
19733
19734void
19735x_update_cursor (f, on_p)
19736 struct frame *f;
19737 int on_p;
19738{
19739 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
19740}
19741
19742
19743/* EXPORT:
19744 Clear the cursor of window W to background color, and mark the
19745 cursor as not shown. This is used when the text where the cursor
19746 is is about to be rewritten. */
19747
19748void
19749x_clear_cursor (w)
19750 struct window *w;
19751{
19752 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
19753 update_window_cursor (w, 0);
19754}
19755
19756
19757/* EXPORT:
19758 Display the active region described by mouse_face_* according to DRAW. */
19759
19760void
19761show_mouse_face (dpyinfo, draw)
19762 Display_Info *dpyinfo;
19763 enum draw_glyphs_face draw;
19764{
19765 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
19766 struct frame *f = XFRAME (WINDOW_FRAME (w));
19767
19768 if (/* If window is in the process of being destroyed, don't bother
19769 to do anything. */
19770 w->current_matrix != NULL
19771 /* Don't update mouse highlight if hidden */
19772 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
19773 /* Recognize when we are called to operate on rows that don't exist
19774 anymore. This can happen when a window is split. */
19775 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
19776 {
19777 int phys_cursor_on_p = w->phys_cursor_on_p;
19778 struct glyph_row *row, *first, *last;
19779
19780 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
19781 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
19782
19783 for (row = first; row <= last && row->enabled_p; ++row)
19784 {
19785 int start_hpos, end_hpos, start_x;
19786
19787 /* For all but the first row, the highlight starts at column 0. */
19788 if (row == first)
19789 {
19790 start_hpos = dpyinfo->mouse_face_beg_col;
19791 start_x = dpyinfo->mouse_face_beg_x;
19792 }
19793 else
19794 {
19795 start_hpos = 0;
19796 start_x = 0;
19797 }
19798
19799 if (row == last)
19800 end_hpos = dpyinfo->mouse_face_end_col;
19801 else
19802 end_hpos = row->used[TEXT_AREA];
19803
19804 if (end_hpos > start_hpos)
19805 {
19806 draw_glyphs (w, start_x, row, TEXT_AREA,
19807 start_hpos, end_hpos,
19808 draw, 0);
19809
19810 row->mouse_face_p
19811 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
19812 }
19813 }
19814
19815 /* When we've written over the cursor, arrange for it to
19816 be displayed again. */
19817 if (phys_cursor_on_p && !w->phys_cursor_on_p)
19818 {
19819 BLOCK_INPUT;
19820 display_and_set_cursor (w, 1,
19821 w->phys_cursor.hpos, w->phys_cursor.vpos,
19822 w->phys_cursor.x, w->phys_cursor.y);
19823 UNBLOCK_INPUT;
19824 }
19825 }
19826
19827 /* Change the mouse cursor. */
19828 if (draw == DRAW_NORMAL_TEXT)
19829 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
19830 else if (draw == DRAW_MOUSE_FACE)
19831 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
19832 else
19833 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
19834}
19835
19836/* EXPORT:
19837 Clear out the mouse-highlighted active region.
19838 Redraw it un-highlighted first. Value is non-zero if mouse
19839 face was actually drawn unhighlighted. */
19840
19841int
19842clear_mouse_face (dpyinfo)
19843 Display_Info *dpyinfo;
19844{
19845 int cleared = 0;
19846
867756d8 19847 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
fa3c6b4d
KS
19848 {
19849 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
19850 cleared = 1;
19851 }
19852
19853 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
19854 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
19855 dpyinfo->mouse_face_window = Qnil;
19856 dpyinfo->mouse_face_overlay = Qnil;
19857 return cleared;
19858}
19859
19860
19861/* EXPORT:
19862 Non-zero if physical cursor of window W is within mouse face. */
19863
19864int
19865cursor_in_mouse_face_p (w)
19866 struct window *w;
19867{
19868 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
19869 int in_mouse_face = 0;
19870
19871 if (WINDOWP (dpyinfo->mouse_face_window)
19872 && XWINDOW (dpyinfo->mouse_face_window) == w)
19873 {
19874 int hpos = w->phys_cursor.hpos;
19875 int vpos = w->phys_cursor.vpos;
19876
19877 if (vpos >= dpyinfo->mouse_face_beg_row
19878 && vpos <= dpyinfo->mouse_face_end_row
19879 && (vpos > dpyinfo->mouse_face_beg_row
19880 || hpos >= dpyinfo->mouse_face_beg_col)
19881 && (vpos < dpyinfo->mouse_face_end_row
19882 || hpos < dpyinfo->mouse_face_end_col
19883 || dpyinfo->mouse_face_past_end))
19884 in_mouse_face = 1;
19885 }
19886
19887 return in_mouse_face;
19888}
19889
19890
19891
19892\f
19893/* Find the glyph matrix position of buffer position CHARPOS in window
19894 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
19895 current glyphs must be up to date. If CHARPOS is above window
19896 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
19897 of last line in W. In the row containing CHARPOS, stop before glyphs
19898 having STOP as object. */
19899
9c3521e0 19900#if 1 /* This is a version of fast_find_position that's more correct
fa3c6b4d
KS
19901 in the presence of hscrolling, for example. I didn't install
19902 it right away because the problem fixed is minor, it failed
19903 in 20.x as well, and I think it's too risky to install
19904 so near the release of 21.1. 2001-09-25 gerd. */
19905
19906static int
19907fast_find_position (w, charpos, hpos, vpos, x, y, stop)
19908 struct window *w;
19909 int charpos;
19910 int *hpos, *vpos, *x, *y;
19911 Lisp_Object stop;
19912{
19913 struct glyph_row *row, *first;
19914 struct glyph *glyph, *end;
b19a5b64 19915 int past_end = 0;
fa3c6b4d
KS
19916
19917 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19918 row = row_containing_pos (w, charpos, first, NULL, 0);
19919 if (row == NULL)
19920 {
19921 if (charpos < MATRIX_ROW_START_CHARPOS (first))
19922 {
19923 *x = *y = *hpos = *vpos = 0;
7a272039 19924 return 1;
fa3c6b4d
KS
19925 }
19926 else
19927 {
19928 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
19929 past_end = 1;
19930 }
19931 }
19932
19933 *x = row->x;
19934 *y = row->y;
19935 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
19936
19937 glyph = row->glyphs[TEXT_AREA];
19938 end = glyph + row->used[TEXT_AREA];
19939
19940 /* Skip over glyphs not having an object at the start of the row.
19941 These are special glyphs like truncation marks on terminal
19942 frames. */
19943 if (row->displays_text_p)
19944 while (glyph < end
19945 && INTEGERP (glyph->object)
19946 && !EQ (stop, glyph->object)
19947 && glyph->charpos < 0)
19948 {
19949 *x += glyph->pixel_width;
19950 ++glyph;
19951 }
19952
19953 while (glyph < end
19954 && !INTEGERP (glyph->object)
19955 && !EQ (stop, glyph->object)
19956 && (!BUFFERP (glyph->object)
19957 || glyph->charpos < charpos))
19958 {
19959 *x += glyph->pixel_width;
19960 ++glyph;
19961 }
19962
19963 *hpos = glyph - row->glyphs[TEXT_AREA];
7a272039 19964 return !past_end;
fa3c6b4d
KS
19965}
19966
9c3521e0 19967#else /* not 1 */
fa3c6b4d
KS
19968
19969static int
19970fast_find_position (w, pos, hpos, vpos, x, y, stop)
19971 struct window *w;
19972 int pos;
19973 int *hpos, *vpos, *x, *y;
19974 Lisp_Object stop;
19975{
19976 int i;
19977 int lastcol;
19978 int maybe_next_line_p = 0;
19979 int line_start_position;
19980 int yb = window_text_bottom_y (w);
19981 struct glyph_row *row, *best_row;
19982 int row_vpos, best_row_vpos;
19983 int current_x;
19984
19985 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19986 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
19987
19988 while (row->y < yb)
19989 {
19990 if (row->used[TEXT_AREA])
19991 line_start_position = row->glyphs[TEXT_AREA]->charpos;
19992 else
19993 line_start_position = 0;
19994
19995 if (line_start_position > pos)
19996 break;
19997 /* If the position sought is the end of the buffer,
19998 don't include the blank lines at the bottom of the window. */
19999 else if (line_start_position == pos
20000 && pos == BUF_ZV (XBUFFER (w->buffer)))
20001 {
20002 maybe_next_line_p = 1;
20003 break;
20004 }
20005 else if (line_start_position > 0)
20006 {
20007 best_row = row;
20008 best_row_vpos = row_vpos;
20009 }
20010
20011 if (row->y + row->height >= yb)
20012 break;
20013
20014 ++row;
20015 ++row_vpos;
20016 }
20017
20018 /* Find the right column within BEST_ROW. */
20019 lastcol = 0;
20020 current_x = best_row->x;
20021 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
20022 {
20023 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
20024 int charpos = glyph->charpos;
20025
20026 if (BUFFERP (glyph->object))
20027 {
20028 if (charpos == pos)
20029 {
20030 *hpos = i;
20031 *vpos = best_row_vpos;
20032 *x = current_x;
20033 *y = best_row->y;
20034 return 1;
20035 }
20036 else if (charpos > pos)
20037 break;
20038 }
20039 else if (EQ (glyph->object, stop))
20040 break;
20041
20042 if (charpos > 0)
20043 lastcol = i;
20044 current_x += glyph->pixel_width;
20045 }
20046
20047 /* If we're looking for the end of the buffer,
20048 and we didn't find it in the line we scanned,
20049 use the start of the following line. */
20050 if (maybe_next_line_p)
20051 {
20052 ++best_row;
20053 ++best_row_vpos;
20054 lastcol = 0;
20055 current_x = best_row->x;
20056 }
20057
20058 *vpos = best_row_vpos;
20059 *hpos = lastcol + 1;
20060 *x = current_x;
20061 *y = best_row->y;
20062 return 0;
20063}
20064
9c3521e0 20065#endif /* not 1 */
fa3c6b4d
KS
20066
20067
20068/* Find the position of the glyph for position POS in OBJECT in
20069 window W's current matrix, and return in *X, *Y the pixel
20070 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
20071
20072 RIGHT_P non-zero means return the position of the right edge of the
20073 glyph, RIGHT_P zero means return the left edge position.
20074
20075 If no glyph for POS exists in the matrix, return the position of
20076 the glyph with the next smaller position that is in the matrix, if
20077 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
20078 exists in the matrix, return the position of the glyph with the
20079 next larger position in OBJECT.
20080
20081 Value is non-zero if a glyph was found. */
20082
20083static int
20084fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
20085 struct window *w;
20086 int pos;
20087 Lisp_Object object;
20088 int *hpos, *vpos, *x, *y;
20089 int right_p;
20090{
20091 int yb = window_text_bottom_y (w);
20092 struct glyph_row *r;
20093 struct glyph *best_glyph = NULL;
20094 struct glyph_row *best_row = NULL;
20095 int best_x = 0;
20096
20097 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20098 r->enabled_p && r->y < yb;
20099 ++r)
20100 {
20101 struct glyph *g = r->glyphs[TEXT_AREA];
20102 struct glyph *e = g + r->used[TEXT_AREA];
20103 int gx;
20104
20105 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
20106 if (EQ (g->object, object))
20107 {
20108 if (g->charpos == pos)
20109 {
20110 best_glyph = g;
20111 best_x = gx;
20112 best_row = r;
20113 goto found;
20114 }
20115 else if (best_glyph == NULL
20116 || ((abs (g->charpos - pos)
20117 < abs (best_glyph->charpos - pos))
20118 && (right_p
20119 ? g->charpos < pos
20120 : g->charpos > pos)))
20121 {
20122 best_glyph = g;
20123 best_x = gx;
20124 best_row = r;
20125 }
20126 }
20127 }
20128
20129 found:
20130
20131 if (best_glyph)
20132 {
20133 *x = best_x;
20134 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
20135
20136 if (right_p)
20137 {
20138 *x += best_glyph->pixel_width;
20139 ++*hpos;
20140 }
20141
20142 *y = best_row->y;
20143 *vpos = best_row - w->current_matrix->rows;
20144 }
20145
20146 return best_glyph != NULL;
20147}
20148
20149
493fdc3c
KS
20150/* See if position X, Y is within a hot-spot of an image. */
20151
20152static int
20153on_hot_spot_p (hot_spot, x, y)
20154 Lisp_Object hot_spot;
20155 int x, y;
20156{
20157 if (!CONSP (hot_spot))
20158 return 0;
20159
20160 if (EQ (XCAR (hot_spot), Qrect))
20161 {
20162 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
20163 Lisp_Object rect = XCDR (hot_spot);
20164 Lisp_Object tem;
20165 if (!CONSP (rect))
20166 return 0;
20167 if (!CONSP (XCAR (rect)))
20168 return 0;
20169 if (!CONSP (XCDR (rect)))
20170 return 0;
20171 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
20172 return 0;
20173 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
20174 return 0;
20175 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
20176 return 0;
20177 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
20178 return 0;
20179 return 1;
20180 }
20181 else if (EQ (XCAR (hot_spot), Qcircle))
20182 {
20183 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
20184 Lisp_Object circ = XCDR (hot_spot);
20185 Lisp_Object lr, lx0, ly0;
20186 if (CONSP (circ)
20187 && CONSP (XCAR (circ))
20188 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
20189 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
20190 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
20191 {
20192 double r = XFLOATINT (lr);
20193 double dx = XINT (lx0) - x;
20194 double dy = XINT (ly0) - y;
20195 return (dx * dx + dy * dy <= r * r);
20196 }
20197 }
20198 else if (EQ (XCAR (hot_spot), Qpoly))
20199 {
20200 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
20201 if (VECTORP (XCDR (hot_spot)))
20202 {
20203 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
20204 Lisp_Object *poly = v->contents;
20205 int n = v->size;
20206 int i;
20207 int inside = 0;
20208 Lisp_Object lx, ly;
20209 int x0, y0;
20210
20211 /* Need an even number of coordinates, and at least 3 edges. */
be21b925 20212 if (n < 6 || n & 1)
493fdc3c
KS
20213 return 0;
20214
20215 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
20216 If count is odd, we are inside polygon. Pixels on edges
20217 may or may not be included depending on actual geometry of the
20218 polygon. */
20219 if ((lx = poly[n-2], !INTEGERP (lx))
20220 || (ly = poly[n-1], !INTEGERP (lx)))
20221 return 0;
20222 x0 = XINT (lx), y0 = XINT (ly);
20223 for (i = 0; i < n; i += 2)
20224 {
20225 int x1 = x0, y1 = y0;
20226 if ((lx = poly[i], !INTEGERP (lx))
20227 || (ly = poly[i+1], !INTEGERP (ly)))
20228 return 0;
20229 x0 = XINT (lx), y0 = XINT (ly);
20230
20231 /* Does this segment cross the X line? */
20232 if (x0 >= x)
20233 {
20234 if (x1 >= x)
20235 continue;
20236 }
20237 else if (x1 < x)
20238 continue;
20239 if (y > y0 && y > y1)
20240 continue;
20241 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
20242 inside = !inside;
20243 }
20244 return inside;
20245 }
20246 }
20247 else
20248 return 0;
20249}
20250
20251Lisp_Object
20252find_hot_spot (map, x, y)
20253 Lisp_Object map;
20254 int x, y;
20255{
20256 while (CONSP (map))
20257 {
20258 if (CONSP (XCAR (map))
20259 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
20260 return XCAR (map);
20261 map = XCDR (map);
20262 }
be21b925 20263
493fdc3c
KS
20264 return Qnil;
20265}
20266
20267DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
20268 3, 3, 0,
be21b925 20269 doc: /* Lookup in image map MAP coordinates X and Y.
493fdc3c
KS
20270An image map is an alist where each element has the format (AREA ID PLIST).
20271An AREA is specified as either a rectangle, a circle, or a polygon:
20272A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
20273pixel coordinates of the upper left and bottom right corners.
20274A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
20275and the radius of the circle; r may be a float or integer.
20276A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
20277vector describes one corner in the polygon.
20278Returns the alist element for the first matching AREA in MAP. */)
20279 (map, x, y)
20280 Lisp_Object map;
20281 Lisp_Object x, y;
20282{
20283 int ix, iy;
20284 if (NILP (map))
20285 return Qnil;
20286
6d925726
KS
20287 CHECK_NUMBER (x);
20288 CHECK_NUMBER (y);
493fdc3c
KS
20289
20290 return find_hot_spot (map, XINT (x), XINT (y));
20291}
20292
20293
20294/* Display frame CURSOR, optionally using shape defined by POINTER. */
20295static void
20296define_frame_cursor1 (f, cursor, pointer)
20297 struct frame *f;
20298 Cursor cursor;
20299 Lisp_Object pointer;
20300{
20301 if (!NILP (pointer))
20302 {
20303 if (EQ (pointer, Qarrow))
20304 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20305 else if (EQ (pointer, Qhand))
20306 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
20307 else if (EQ (pointer, Qtext))
20308 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20309 else if (EQ (pointer, intern ("hdrag")))
20310 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
20311#ifdef HAVE_X_WINDOWS
20312 else if (EQ (pointer, intern ("vdrag")))
20313 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
20314#endif
20315 else if (EQ (pointer, intern ("hourglass")))
20316 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
20317 else if (EQ (pointer, Qmodeline))
20318 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
20319 else
20320 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20321 }
20322
20323#ifndef HAVE_CARBON
20324 if (cursor != No_Cursor)
20325#else
20326 if (bcmp (&cursor, &No_Cursor, sizeof (Cursor)))
20327#endif
20328 rif->define_frame_cursor (f, cursor);
20329}
20330
fa3c6b4d
KS
20331/* Take proper action when mouse has moved to the mode or header line
20332 or marginal area AREA of window W, x-position X and y-position Y.
20333 X is relative to the start of the text display area of W, so the
20334 width of bitmap areas and scroll bars must be subtracted to get a
20335 position relative to the start of the mode line. */
20336
20337static void
20338note_mode_line_or_margin_highlight (w, x, y, area)
20339 struct window *w;
20340 int x, y;
20341 enum window_part area;
20342{
20343 struct frame *f = XFRAME (w->frame);
20344 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
f65536fa 20345 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
493fdc3c 20346 Lisp_Object pointer = Qnil;
349c653e
KS
20347 int charpos, dx, dy, width, height;
20348 Lisp_Object string, object = Qnil;
493fdc3c 20349 Lisp_Object pos, help, image;
fa3c6b4d
KS
20350
20351 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
349c653e
KS
20352 string = mode_line_string (w, area, &x, &y, &charpos,
20353 &object, &dx, &dy, &width, &height);
fa3c6b4d 20354 else
493fdc3c
KS
20355 {
20356 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
349c653e
KS
20357 string = marginal_area_string (w, area, &x, &y, &charpos,
20358 &object, &dx, &dy, &width, &height);
493fdc3c
KS
20359 }
20360
20361 help = Qnil;
fa3c6b4d 20362
349c653e 20363 if (IMAGEP (object))
493fdc3c
KS
20364 {
20365 Lisp_Object image_map, hotspot;
349c653e 20366 if ((image_map = Fplist_get (XCDR (object), QCmap),
493fdc3c
KS
20367 !NILP (image_map))
20368 && (hotspot = find_hot_spot (image_map, dx, dy),
20369 CONSP (hotspot))
20370 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
20371 {
20372 Lisp_Object area_id, plist;
fa3c6b4d 20373
493fdc3c
KS
20374 area_id = XCAR (hotspot);
20375 /* Could check AREA_ID to see if we enter/leave this hot-spot.
20376 If so, we could look for mouse-enter, mouse-leave
20377 properties in PLIST (and do something...). */
20378 if ((plist = XCDR (hotspot), CONSP (plist)))
20379 {
20380 pointer = Fplist_get (plist, Qpointer);
20381 if (NILP (pointer))
20382 pointer = Qhand;
20383 help = Fplist_get (plist, Qhelp_echo);
20384 if (!NILP (help))
20385 {
20386 help_echo_string = help;
20387 /* Is this correct? ++kfs */
20388 XSETWINDOW (help_echo_window, w);
20389 help_echo_object = w->buffer;
20390 help_echo_pos = charpos;
20391 }
20392 }
20393 if (NILP (pointer))
349c653e
KS
20394 pointer = Fplist_get (XCDR (object), QCpointer);
20395 }
20396 }
20397
20398 if (STRINGP (string))
20399 {
20400 pos = make_number (charpos);
20401 /* If we're on a string with `help-echo' text property, arrange
20402 for the help to be displayed. This is done by setting the
20403 global variable help_echo_string to the help string. */
20404 help = Fget_text_property (pos, Qhelp_echo, string);
20405 if (!NILP (help))
20406 {
20407 help_echo_string = help;
20408 XSETWINDOW (help_echo_window, w);
20409 help_echo_object = string;
20410 help_echo_pos = charpos;
20411 }
20412
20413 if (NILP (pointer))
20414 pointer = Fget_text_property (pos, Qpointer, string);
20415
20416 /* Change the mouse pointer according to what is under X/Y. */
73bc43da 20417 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
349c653e
KS
20418 {
20419 Lisp_Object map;
20420 map = Fget_text_property (pos, Qlocal_map, string);
20421 if (!KEYMAPP (map))
20422 map = Fget_text_property (pos, Qkeymap, string);
20423 if (!KEYMAPP (map))
20424 cursor = dpyinfo->vertical_scroll_bar_cursor;
493fdc3c
KS
20425 }
20426 }
20427
20428 define_frame_cursor1 (f, cursor, pointer);
fa3c6b4d
KS
20429}
20430
fa3c6b4d
KS
20431
20432/* EXPORT:
20433 Take proper action when the mouse has moved to position X, Y on
20434 frame F as regards highlighting characters that have mouse-face
20435 properties. Also de-highlighting chars where the mouse was before.
20436 X and Y can be negative or out of range. */
20437
20438void
20439note_mouse_highlight (f, x, y)
20440 struct frame *f;
20441 int x, y;
20442{
20443 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20444 enum window_part part;
20445 Lisp_Object window;
20446 struct window *w;
20447 Cursor cursor = No_Cursor;
493fdc3c 20448 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
fa3c6b4d
KS
20449 struct buffer *b;
20450
20451 /* When a menu is active, don't highlight because this looks odd. */
20452#if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
20453 if (popup_activated ())
20454 return;
20455#endif
20456
20457 if (NILP (Vmouse_highlight)
20458 || !f->glyphs_initialized_p)
20459 return;
20460
20461 dpyinfo->mouse_face_mouse_x = x;
20462 dpyinfo->mouse_face_mouse_y = y;
20463 dpyinfo->mouse_face_mouse_frame = f;
20464
20465 if (dpyinfo->mouse_face_defer)
20466 return;
20467
20468 if (gc_in_progress)
20469 {
20470 dpyinfo->mouse_face_deferred_gc = 1;
20471 return;
20472 }
20473
20474 /* Which window is that in? */
da8b7f4f 20475 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
fa3c6b4d
KS
20476
20477 /* If we were displaying active text in another window, clear that. */
20478 if (! EQ (window, dpyinfo->mouse_face_window))
20479 clear_mouse_face (dpyinfo);
20480
20481 /* Not on a window -> return. */
20482 if (!WINDOWP (window))
20483 return;
20484
20485 /* Reset help_echo_string. It will get recomputed below. */
fa3c6b4d
KS
20486 help_echo_string = Qnil;
20487
20488 /* Convert to window-relative pixel coordinates. */
20489 w = XWINDOW (window);
20490 frame_to_window_pixel_xy (w, &x, &y);
20491
20492 /* Handle tool-bar window differently since it doesn't display a
20493 buffer. */
20494 if (EQ (window, f->tool_bar_window))
20495 {
20496 note_tool_bar_highlight (f, x, y);
20497 return;
20498 }
20499
fa3c6b4d
KS
20500 /* Mouse is on the mode, header line or margin? */
20501 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
20502 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
20503 {
20504 note_mode_line_or_margin_highlight (w, x, y, part);
20505 return;
20506 }
fa3c6b4d
KS
20507
20508 if (part == ON_VERTICAL_BORDER)
20509 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
f65536fa
KS
20510 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE)
20511 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
fa3c6b4d
KS
20512 else
20513 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20514
20515 /* Are we in a window whose display is up to date?
20516 And verify the buffer's text has not changed. */
20517 b = XBUFFER (w->buffer);
20518 if (part == ON_TEXT
20519 && EQ (w->window_end_valid, w->buffer)
20520 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
20521 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
20522 {
493fdc3c 20523 int hpos, vpos, pos, i, dx, dy, area;
fa3c6b4d
KS
20524 struct glyph *glyph;
20525 Lisp_Object object;
20526 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
20527 Lisp_Object *overlay_vec = NULL;
20528 int len, noverlays;
20529 struct buffer *obuf;
20530 int obegv, ozv, same_region;
20531
20532 /* Find the glyph under X/Y. */
493fdc3c
KS
20533 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
20534
20535 /* Look for :pointer property on image. */
20536 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
20537 {
20538 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
20539 if (img != NULL && IMAGEP (img->spec))
20540 {
20541 Lisp_Object image_map, hotspot;
20542 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
20543 !NILP (image_map))
20544 && (hotspot = find_hot_spot (image_map, dx, dy),
20545 CONSP (hotspot))
20546 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
20547 {
20548 Lisp_Object area_id, plist;
20549
20550 area_id = XCAR (hotspot);
20551 /* Could check AREA_ID to see if we enter/leave this hot-spot.
20552 If so, we could look for mouse-enter, mouse-leave
20553 properties in PLIST (and do something...). */
20554 if ((plist = XCDR (hotspot), CONSP (plist)))
20555 {
20556 pointer = Fplist_get (plist, Qpointer);
20557 if (NILP (pointer))
20558 pointer = Qhand;
20559 help_echo_string = Fplist_get (plist, Qhelp_echo);
20560 if (!NILP (help_echo_string))
20561 {
20562 help_echo_window = window;
20563 help_echo_object = glyph->object;
20564 help_echo_pos = glyph->charpos;
20565 }
20566 }
20567 }
20568 if (NILP (pointer))
20569 pointer = Fplist_get (XCDR (img->spec), QCpointer);
20570 }
20571 }
fa3c6b4d
KS
20572
20573 /* Clear mouse face if X/Y not over text. */
20574 if (glyph == NULL
20575 || area != TEXT_AREA
20576 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
20577 {
fa3c6b4d
KS
20578 if (clear_mouse_face (dpyinfo))
20579 cursor = No_Cursor;
493fdc3c
KS
20580 if (NILP (pointer))
20581 {
20582 if (area != TEXT_AREA)
20583 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20584 else
20585 pointer = Vvoid_text_area_pointer;
20586 }
fa3c6b4d
KS
20587 goto set_cursor;
20588 }
20589
20590 pos = glyph->charpos;
20591 object = glyph->object;
20592 if (!STRINGP (object) && !BUFFERP (object))
20593 goto set_cursor;
20594
20595 /* If we get an out-of-range value, return now; avoid an error. */
20596 if (BUFFERP (object) && pos > BUF_Z (b))
20597 goto set_cursor;
20598
20599 /* Make the window's buffer temporarily current for
20600 overlays_at and compute_char_face. */
20601 obuf = current_buffer;
20602 current_buffer = b;
20603 obegv = BEGV;
20604 ozv = ZV;
20605 BEGV = BEG;
20606 ZV = Z;
20607
20608 /* Is this char mouse-active or does it have help-echo? */
20609 position = make_number (pos);
20610
20611 if (BUFFERP (object))
20612 {
20613 /* Put all the overlays we want in a vector in overlay_vec.
20614 Store the length in len. If there are more than 10, make
20615 enough space for all, and try again. */
20616 len = 10;
20617 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
20618 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
20619 if (noverlays > len)
20620 {
20621 len = noverlays;
20622 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
20623 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
20624 }
20625
20626 /* Sort overlays into increasing priority order. */
20627 noverlays = sort_overlays (overlay_vec, noverlays, w);
20628 }
20629 else
20630 noverlays = 0;
20631
20632 same_region = (EQ (window, dpyinfo->mouse_face_window)
20633 && vpos >= dpyinfo->mouse_face_beg_row
20634 && vpos <= dpyinfo->mouse_face_end_row
20635 && (vpos > dpyinfo->mouse_face_beg_row
20636 || hpos >= dpyinfo->mouse_face_beg_col)
20637 && (vpos < dpyinfo->mouse_face_end_row
20638 || hpos < dpyinfo->mouse_face_end_col
20639 || dpyinfo->mouse_face_past_end));
20640
20641 if (same_region)
20642 cursor = No_Cursor;
20643
20644 /* Check mouse-face highlighting. */
20645 if (! same_region
20646 /* If there exists an overlay with mouse-face overlapping
20647 the one we are currently highlighting, we have to
20648 check if we enter the overlapping overlay, and then
20649 highlight only that. */
20650 || (OVERLAYP (dpyinfo->mouse_face_overlay)
20651 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
20652 {
20653 /* Find the highest priority overlay that has a mouse-face
20654 property. */
20655 overlay = Qnil;
20656 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
20657 {
20658 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
20659 if (!NILP (mouse_face))
20660 overlay = overlay_vec[i];
20661 }
20662
20663 /* If we're actually highlighting the same overlay as
20664 before, there's no need to do that again. */
20665 if (!NILP (overlay)
20666 && EQ (overlay, dpyinfo->mouse_face_overlay))
20667 goto check_help_echo;
20668
20669 dpyinfo->mouse_face_overlay = overlay;
20670
20671 /* Clear the display of the old active region, if any. */
20672 if (clear_mouse_face (dpyinfo))
20673 cursor = No_Cursor;
20674
20675 /* If no overlay applies, get a text property. */
20676 if (NILP (overlay))
20677 mouse_face = Fget_text_property (position, Qmouse_face, object);
20678
20679 /* Handle the overlay case. */
20680 if (!NILP (overlay))
20681 {
20682 /* Find the range of text around this char that
20683 should be active. */
20684 Lisp_Object before, after;
20685 int ignore;
20686
20687 before = Foverlay_start (overlay);
20688 after = Foverlay_end (overlay);
20689 /* Record this as the current active region. */
20690 fast_find_position (w, XFASTINT (before),
20691 &dpyinfo->mouse_face_beg_col,
20692 &dpyinfo->mouse_face_beg_row,
20693 &dpyinfo->mouse_face_beg_x,
20694 &dpyinfo->mouse_face_beg_y, Qnil);
20695
20696 dpyinfo->mouse_face_past_end
20697 = !fast_find_position (w, XFASTINT (after),
20698 &dpyinfo->mouse_face_end_col,
20699 &dpyinfo->mouse_face_end_row,
20700 &dpyinfo->mouse_face_end_x,
20701 &dpyinfo->mouse_face_end_y, Qnil);
20702 dpyinfo->mouse_face_window = window;
20703
20704 dpyinfo->mouse_face_face_id
20705 = face_at_buffer_position (w, pos, 0, 0,
20706 &ignore, pos + 1,
20707 !dpyinfo->mouse_face_hidden);
20708
20709 /* Display it as active. */
20710 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
20711 cursor = No_Cursor;
20712 }
20713 /* Handle the text property case. */
20714 else if (!NILP (mouse_face) && BUFFERP (object))
20715 {
20716 /* Find the range of text around this char that
20717 should be active. */
20718 Lisp_Object before, after, beginning, end;
20719 int ignore;
20720
20721 beginning = Fmarker_position (w->start);
20722 end = make_number (BUF_Z (XBUFFER (object))
20723 - XFASTINT (w->window_end_pos));
20724 before
20725 = Fprevious_single_property_change (make_number (pos + 1),
20726 Qmouse_face,
20727 object, beginning);
20728 after
20729 = Fnext_single_property_change (position, Qmouse_face,
20730 object, end);
20731
20732 /* Record this as the current active region. */
20733 fast_find_position (w, XFASTINT (before),
20734 &dpyinfo->mouse_face_beg_col,
20735 &dpyinfo->mouse_face_beg_row,
20736 &dpyinfo->mouse_face_beg_x,
20737 &dpyinfo->mouse_face_beg_y, Qnil);
20738 dpyinfo->mouse_face_past_end
20739 = !fast_find_position (w, XFASTINT (after),
20740 &dpyinfo->mouse_face_end_col,
20741 &dpyinfo->mouse_face_end_row,
20742 &dpyinfo->mouse_face_end_x,
20743 &dpyinfo->mouse_face_end_y, Qnil);
20744 dpyinfo->mouse_face_window = window;
20745
20746 if (BUFFERP (object))
20747 dpyinfo->mouse_face_face_id
20748 = face_at_buffer_position (w, pos, 0, 0,
20749 &ignore, pos + 1,
20750 !dpyinfo->mouse_face_hidden);
20751
20752 /* Display it as active. */
20753 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
20754 cursor = No_Cursor;
20755 }
20756 else if (!NILP (mouse_face) && STRINGP (object))
20757 {
20758 Lisp_Object b, e;
20759 int ignore;
20760
20761 b = Fprevious_single_property_change (make_number (pos + 1),
20762 Qmouse_face,
20763 object, Qnil);
20764 e = Fnext_single_property_change (position, Qmouse_face,
20765 object, Qnil);
20766 if (NILP (b))
20767 b = make_number (0);
20768 if (NILP (e))
20769 e = make_number (SCHARS (object) - 1);
20770 fast_find_string_pos (w, XINT (b), object,
20771 &dpyinfo->mouse_face_beg_col,
20772 &dpyinfo->mouse_face_beg_row,
20773 &dpyinfo->mouse_face_beg_x,
20774 &dpyinfo->mouse_face_beg_y, 0);
20775 fast_find_string_pos (w, XINT (e), object,
20776 &dpyinfo->mouse_face_end_col,
20777 &dpyinfo->mouse_face_end_row,
20778 &dpyinfo->mouse_face_end_x,
20779 &dpyinfo->mouse_face_end_y, 1);
20780 dpyinfo->mouse_face_past_end = 0;
20781 dpyinfo->mouse_face_window = window;
20782 dpyinfo->mouse_face_face_id
20783 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
20784 glyph->face_id, 1);
20785 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
20786 cursor = No_Cursor;
20787 }
20788 else if (STRINGP (object) && NILP (mouse_face))
20789 {
20790 /* A string which doesn't have mouse-face, but
20791 the text ``under'' it might have. */
20792 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
20793 int start = MATRIX_ROW_START_CHARPOS (r);
20794
20795 pos = string_buffer_position (w, object, start);
20796 if (pos > 0)
20797 mouse_face = get_char_property_and_overlay (make_number (pos),
20798 Qmouse_face,
20799 w->buffer,
20800 &overlay);
20801 if (!NILP (mouse_face) && !NILP (overlay))
20802 {
20803 Lisp_Object before = Foverlay_start (overlay);
20804 Lisp_Object after = Foverlay_end (overlay);
20805 int ignore;
20806
20807 /* Note that we might not be able to find position
20808 BEFORE in the glyph matrix if the overlay is
20809 entirely covered by a `display' property. In
20810 this case, we overshoot. So let's stop in
20811 the glyph matrix before glyphs for OBJECT. */
20812 fast_find_position (w, XFASTINT (before),
20813 &dpyinfo->mouse_face_beg_col,
20814 &dpyinfo->mouse_face_beg_row,
20815 &dpyinfo->mouse_face_beg_x,
20816 &dpyinfo->mouse_face_beg_y,
20817 object);
20818
20819 dpyinfo->mouse_face_past_end
20820 = !fast_find_position (w, XFASTINT (after),
20821 &dpyinfo->mouse_face_end_col,
20822 &dpyinfo->mouse_face_end_row,
20823 &dpyinfo->mouse_face_end_x,
20824 &dpyinfo->mouse_face_end_y,
20825 Qnil);
20826 dpyinfo->mouse_face_window = window;
20827 dpyinfo->mouse_face_face_id
20828 = face_at_buffer_position (w, pos, 0, 0,
20829 &ignore, pos + 1,
20830 !dpyinfo->mouse_face_hidden);
20831
20832 /* Display it as active. */
20833 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
20834 cursor = No_Cursor;
20835 }
20836 }
20837 }
20838
20839 check_help_echo:
20840
20841 /* Look for a `help-echo' property. */
493fdc3c 20842 if (NILP (help_echo_string)) {
fa3c6b4d
KS
20843 Lisp_Object help, overlay;
20844
20845 /* Check overlays first. */
20846 help = overlay = Qnil;
20847 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
20848 {
20849 overlay = overlay_vec[i];
20850 help = Foverlay_get (overlay, Qhelp_echo);
20851 }
20852
20853 if (!NILP (help))
20854 {
20855 help_echo_string = help;
20856 help_echo_window = window;
20857 help_echo_object = overlay;
20858 help_echo_pos = pos;
20859 }
20860 else
20861 {
20862 Lisp_Object object = glyph->object;
20863 int charpos = glyph->charpos;
20864
20865 /* Try text properties. */
20866 if (STRINGP (object)
20867 && charpos >= 0
20868 && charpos < SCHARS (object))
20869 {
20870 help = Fget_text_property (make_number (charpos),
20871 Qhelp_echo, object);
20872 if (NILP (help))
20873 {
20874 /* If the string itself doesn't specify a help-echo,
20875 see if the buffer text ``under'' it does. */
20876 struct glyph_row *r
20877 = MATRIX_ROW (w->current_matrix, vpos);
20878 int start = MATRIX_ROW_START_CHARPOS (r);
20879 int pos = string_buffer_position (w, object, start);
20880 if (pos > 0)
20881 {
20882 help = Fget_char_property (make_number (pos),
20883 Qhelp_echo, w->buffer);
20884 if (!NILP (help))
20885 {
20886 charpos = pos;
20887 object = w->buffer;
20888 }
20889 }
20890 }
20891 }
20892 else if (BUFFERP (object)
20893 && charpos >= BEGV
20894 && charpos < ZV)
20895 help = Fget_text_property (make_number (charpos), Qhelp_echo,
20896 object);
20897
20898 if (!NILP (help))
20899 {
20900 help_echo_string = help;
20901 help_echo_window = window;
20902 help_echo_object = object;
20903 help_echo_pos = charpos;
20904 }
20905 }
20906 }
20907
493fdc3c
KS
20908 /* Look for a `pointer' property. */
20909 if (NILP (pointer))
20910 {
20911 /* Check overlays first. */
20912 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
20913 pointer = Foverlay_get (overlay_vec[i], Qpointer);
20914
20915 if (NILP (pointer))
20916 {
20917 Lisp_Object object = glyph->object;
20918 int charpos = glyph->charpos;
20919
20920 /* Try text properties. */
20921 if (STRINGP (object)
20922 && charpos >= 0
20923 && charpos < SCHARS (object))
20924 {
20925 pointer = Fget_text_property (make_number (charpos),
20926 Qpointer, object);
20927 if (NILP (pointer))
20928 {
20929 /* If the string itself doesn't specify a pointer,
20930 see if the buffer text ``under'' it does. */
20931 struct glyph_row *r
20932 = MATRIX_ROW (w->current_matrix, vpos);
20933 int start = MATRIX_ROW_START_CHARPOS (r);
20934 int pos = string_buffer_position (w, object, start);
20935 if (pos > 0)
20936 pointer = Fget_char_property (make_number (pos),
20937 Qpointer, w->buffer);
20938 }
20939 }
20940 else if (BUFFERP (object)
20941 && charpos >= BEGV
20942 && charpos < ZV)
20943 pointer = Fget_text_property (make_number (charpos),
20944 Qpointer, object);
20945 }
20946 }
20947
fa3c6b4d
KS
20948 BEGV = obegv;
20949 ZV = ozv;
20950 current_buffer = obuf;
20951 }
20952
20953 set_cursor:
20954
493fdc3c 20955 define_frame_cursor1 (f, cursor, pointer);
fa3c6b4d
KS
20956}
20957
20958
20959/* EXPORT for RIF:
20960 Clear any mouse-face on window W. This function is part of the
20961 redisplay interface, and is called from try_window_id and similar
20962 functions to ensure the mouse-highlight is off. */
20963
20964void
20965x_clear_window_mouse_face (w)
20966 struct window *w;
20967{
20968 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20969 Lisp_Object window;
20970
20971 BLOCK_INPUT;
20972 XSETWINDOW (window, w);
20973 if (EQ (window, dpyinfo->mouse_face_window))
20974 clear_mouse_face (dpyinfo);
20975 UNBLOCK_INPUT;
20976}
20977
20978
20979/* EXPORT:
20980 Just discard the mouse face information for frame F, if any.
20981 This is used when the size of F is changed. */
20982
20983void
20984cancel_mouse_face (f)
20985 struct frame *f;
20986{
20987 Lisp_Object window;
20988 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20989
20990 window = dpyinfo->mouse_face_window;
20991 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
20992 {
20993 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20994 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20995 dpyinfo->mouse_face_window = Qnil;
20996 }
20997}
20998
20999
21000#endif /* HAVE_WINDOW_SYSTEM */
21001
21002\f
21003/***********************************************************************
21004 Exposure Events
21005 ***********************************************************************/
21006
21007#ifdef HAVE_WINDOW_SYSTEM
21008
21009/* Redraw the part of glyph row area AREA of glyph row ROW on window W
21010 which intersects rectangle R. R is in window-relative coordinates. */
21011
21012static void
21013expose_area (w, row, r, area)
21014 struct window *w;
21015 struct glyph_row *row;
21016 XRectangle *r;
21017 enum glyph_row_area area;
21018{
21019 struct glyph *first = row->glyphs[area];
21020 struct glyph *end = row->glyphs[area] + row->used[area];
21021 struct glyph *last;
21022 int first_x, start_x, x;
21023
21024 if (area == TEXT_AREA && row->fill_line_p)
21025 /* If row extends face to end of line write the whole line. */
21026 draw_glyphs (w, 0, row, area,
21027 0, row->used[area],
21028 DRAW_NORMAL_TEXT, 0);
21029 else
21030 {
21031 /* Set START_X to the window-relative start position for drawing glyphs of
21032 AREA. The first glyph of the text area can be partially visible.
21033 The first glyphs of other areas cannot. */
da8b7f4f 21034 start_x = window_box_left_offset (w, area);
fa3c6b4d 21035 x = start_x;
29b3ea63
KS
21036 if (area == TEXT_AREA)
21037 x += row->x;
fa3c6b4d
KS
21038
21039 /* Find the first glyph that must be redrawn. */
21040 while (first < end
21041 && x + first->pixel_width < r->x)
21042 {
21043 x += first->pixel_width;
21044 ++first;
21045 }
21046
21047 /* Find the last one. */
21048 last = first;
21049 first_x = x;
21050 while (last < end
21051 && x < r->x + r->width)
21052 {
21053 x += last->pixel_width;
21054 ++last;
21055 }
21056
21057 /* Repaint. */
21058 if (last > first)
21059 draw_glyphs (w, first_x - start_x, row, area,
21060 first - row->glyphs[area], last - row->glyphs[area],
21061 DRAW_NORMAL_TEXT, 0);
21062 }
21063}
21064
21065
21066/* Redraw the parts of the glyph row ROW on window W intersecting
21067 rectangle R. R is in window-relative coordinates. Value is
21068 non-zero if mouse-face was overwritten. */
21069
21070static int
21071expose_line (w, row, r)
21072 struct window *w;
21073 struct glyph_row *row;
21074 XRectangle *r;
21075{
21076 xassert (row->enabled_p);
21077
21078 if (row->mode_line_p || w->pseudo_window_p)
21079 draw_glyphs (w, 0, row, TEXT_AREA,
21080 0, row->used[TEXT_AREA],
21081 DRAW_NORMAL_TEXT, 0);
21082 else
21083 {
21084 if (row->used[LEFT_MARGIN_AREA])
21085 expose_area (w, row, r, LEFT_MARGIN_AREA);
21086 if (row->used[TEXT_AREA])
21087 expose_area (w, row, r, TEXT_AREA);
21088 if (row->used[RIGHT_MARGIN_AREA])
21089 expose_area (w, row, r, RIGHT_MARGIN_AREA);
21090 draw_row_fringe_bitmaps (w, row);
cfe03a41
KS
21091 }
21092
fa3c6b4d
KS
21093 return row->mouse_face_p;
21094}
21095
21096
21097/* Redraw those parts of glyphs rows during expose event handling that
21098 overlap other rows. Redrawing of an exposed line writes over parts
21099 of lines overlapping that exposed line; this function fixes that.
21100
21101 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
21102 row in W's current matrix that is exposed and overlaps other rows.
21103 LAST_OVERLAPPING_ROW is the last such row. */
21104
21105static void
21106expose_overlaps (w, first_overlapping_row, last_overlapping_row)
21107 struct window *w;
21108 struct glyph_row *first_overlapping_row;
21109 struct glyph_row *last_overlapping_row;
21110{
21111 struct glyph_row *row;
21112
21113 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
21114 if (row->overlapping_p)
21115 {
21116 xassert (row->enabled_p && !row->mode_line_p);
21117
21118 if (row->used[LEFT_MARGIN_AREA])
21119 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
cfe03a41 21120
fa3c6b4d
KS
21121 if (row->used[TEXT_AREA])
21122 x_fix_overlapping_area (w, row, TEXT_AREA);
21123
21124 if (row->used[RIGHT_MARGIN_AREA])
21125 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
21126 }
21127}
21128
21129
21130/* Return non-zero if W's cursor intersects rectangle R. */
21131
21132static int
21133phys_cursor_in_rect_p (w, r)
21134 struct window *w;
21135 XRectangle *r;
21136{
21137 XRectangle cr, result;
21138 struct glyph *cursor_glyph;
21139
21140 cursor_glyph = get_phys_cursor_glyph (w);
21141 if (cursor_glyph)
cfe03a41 21142 {
be21b925 21143 /* r is relative to W's box, but w->phys_cursor.x is relative
aecf50d2
KS
21144 to left edge of W's TEXT area. Adjust it. */
21145 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
fa3c6b4d
KS
21146 cr.y = w->phys_cursor.y;
21147 cr.width = cursor_glyph->pixel_width;
21148 cr.height = w->phys_cursor_height;
be21b925 21149 /* ++KFS: W32 version used W32-specific IntersectRect here, but
fa3c6b4d
KS
21150 I assume the effect is the same -- and this is portable. */
21151 return x_intersect_rectangles (&cr, r, &result);
cfe03a41 21152 }
fa3c6b4d
KS
21153 else
21154 return 0;
21155}
cfe03a41 21156
cfe03a41 21157
fa3c6b4d
KS
21158/* EXPORT:
21159 Draw a vertical window border to the right of window W if W doesn't
21160 have vertical scroll bars. */
cfe03a41 21161
cfe03a41 21162void
fa3c6b4d
KS
21163x_draw_vertical_border (w)
21164 struct window *w;
cfe03a41 21165{
da8b7f4f 21166 /* We could do better, if we knew what type of scroll-bar the adjacent
be21b925 21167 windows (on either side) have... But we don't :-(
da8b7f4f
KS
21168 However, I think this works ok. ++KFS 2003-04-25 */
21169
fa3c6b4d
KS
21170 /* Redraw borders between horizontally adjacent windows. Don't
21171 do it for frames with vertical scroll bars because either the
21172 right scroll bar of a window, or the left scroll bar of its
21173 neighbor will suffice as a border. */
21174 if (!WINDOW_RIGHTMOST_P (w)
da8b7f4f 21175 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
fa3c6b4d
KS
21176 {
21177 int x0, x1, y0, y1;
cfe03a41 21178
fa3c6b4d 21179 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
fa3c6b4d 21180 y1 -= 1;
cfe03a41 21181
fa3c6b4d 21182 rif->draw_vertical_window_border (w, x1, y0, y1);
cfe03a41 21183 }
da8b7f4f
KS
21184 else if (!WINDOW_LEFTMOST_P (w)
21185 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
21186 {
21187 int x0, x1, y0, y1;
21188
21189 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21190 y1 -= 1;
21191
21192 rif->draw_vertical_window_border (w, x0, y0, y1);
21193 }
cfe03a41
KS
21194}
21195
21196
fa3c6b4d
KS
21197/* Redraw the part of window W intersection rectangle FR. Pixel
21198 coordinates in FR are frame-relative. Call this function with
21199 input blocked. Value is non-zero if the exposure overwrites
21200 mouse-face. */
cfe03a41 21201
fa3c6b4d
KS
21202static int
21203expose_window (w, fr)
cfe03a41 21204 struct window *w;
fa3c6b4d 21205 XRectangle *fr;
cfe03a41
KS
21206{
21207 struct frame *f = XFRAME (w->frame);
fa3c6b4d
KS
21208 XRectangle wr, r;
21209 int mouse_face_overwritten_p = 0;
21210
21211 /* If window is not yet fully initialized, do nothing. This can
21212 happen when toolkit scroll bars are used and a window is split.
21213 Reconfiguring the scroll bar will generate an expose for a newly
21214 created window. */
21215 if (w->current_matrix == NULL)
21216 return 0;
cfe03a41 21217
fa3c6b4d
KS
21218 /* When we're currently updating the window, display and current
21219 matrix usually don't agree. Arrange for a thorough display
21220 later. */
21221 if (w == updated_window)
21222 {
21223 SET_FRAME_GARBAGED (f);
21224 return 0;
21225 }
e9c99027 21226
fa3c6b4d 21227 /* Frame-relative pixel rectangle of W. */
da8b7f4f
KS
21228 wr.x = WINDOW_LEFT_EDGE_X (w);
21229 wr.y = WINDOW_TOP_EDGE_Y (w);
21230 wr.width = WINDOW_TOTAL_WIDTH (w);
21231 wr.height = WINDOW_TOTAL_HEIGHT (w);
fa3c6b4d
KS
21232
21233 if (x_intersect_rectangles (fr, &wr, &r))
cfe03a41 21234 {
fa3c6b4d
KS
21235 int yb = window_text_bottom_y (w);
21236 struct glyph_row *row;
21237 int cursor_cleared_p;
21238 struct glyph_row *first_overlapping_row, *last_overlapping_row;
21239
21240 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
21241 r.x, r.y, r.width, r.height));
21242
21243 /* Convert to window coordinates. */
cc8549df
KS
21244 r.x -= WINDOW_LEFT_EDGE_X (w);
21245 r.y -= WINDOW_TOP_EDGE_Y (w);
fa3c6b4d
KS
21246
21247 /* Turn off the cursor. */
21248 if (!w->pseudo_window_p
21249 && phys_cursor_in_rect_p (w, &r))
cfe03a41 21250 {
fa3c6b4d
KS
21251 x_clear_cursor (w);
21252 cursor_cleared_p = 1;
cfe03a41 21253 }
fa3c6b4d
KS
21254 else
21255 cursor_cleared_p = 0;
cfe03a41 21256
fa3c6b4d
KS
21257 /* Update lines intersecting rectangle R. */
21258 first_overlapping_row = last_overlapping_row = NULL;
21259 for (row = w->current_matrix->rows;
21260 row->enabled_p;
21261 ++row)
21262 {
21263 int y0 = row->y;
21264 int y1 = MATRIX_ROW_BOTTOM_Y (row);
cfe03a41 21265
fa3c6b4d
KS
21266 if ((y0 >= r.y && y0 < r.y + r.height)
21267 || (y1 > r.y && y1 < r.y + r.height)
21268 || (r.y >= y0 && r.y < y1)
21269 || (r.y + r.height > y0 && r.y + r.height < y1))
21270 {
21271 if (row->overlapping_p)
21272 {
21273 if (first_overlapping_row == NULL)
21274 first_overlapping_row = row;
21275 last_overlapping_row = row;
21276 }
e9c99027 21277
fa3c6b4d
KS
21278 if (expose_line (w, row, &r))
21279 mouse_face_overwritten_p = 1;
21280 }
cfe03a41 21281
fa3c6b4d
KS
21282 if (y1 >= yb)
21283 break;
21284 }
cfe03a41 21285
fa3c6b4d
KS
21286 /* Display the mode line if there is one. */
21287 if (WINDOW_WANTS_MODELINE_P (w)
21288 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
21289 row->enabled_p)
21290 && row->y < r.y + r.height)
21291 {
21292 if (expose_line (w, row, &r))
21293 mouse_face_overwritten_p = 1;
21294 }
cfe03a41 21295
fa3c6b4d
KS
21296 if (!w->pseudo_window_p)
21297 {
21298 /* Fix the display of overlapping rows. */
21299 if (first_overlapping_row)
21300 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
21301
21302 /* Draw border between windows. */
21303 x_draw_vertical_border (w);
21304
21305 /* Turn the cursor on again. */
21306 if (cursor_cleared_p)
21307 update_window_cursor (w, 1);
21308 }
21309 }
21310
21311#ifdef HAVE_CARBON
21312 /* Display scroll bar for this window. */
21313 if (!NILP (w->vertical_scroll_bar))
cfe03a41 21314 {
fa3c6b4d
KS
21315 /* ++KFS:
21316 If this doesn't work here (maybe some header files are missing),
21317 make a function in macterm.c and call it to do the job! */
21318 ControlHandle ch
21319 = SCROLL_BAR_CONTROL_HANDLE (XSCROLL_BAR (w->vertical_scroll_bar));
21320
21321 Draw1Control (ch);
cfe03a41 21322 }
fa3c6b4d 21323#endif
cfe03a41 21324
fa3c6b4d
KS
21325 return mouse_face_overwritten_p;
21326}
21327
21328
21329
21330/* Redraw (parts) of all windows in the window tree rooted at W that
21331 intersect R. R contains frame pixel coordinates. Value is
21332 non-zero if the exposure overwrites mouse-face. */
21333
21334static int
21335expose_window_tree (w, r)
21336 struct window *w;
21337 XRectangle *r;
21338{
21339 struct frame *f = XFRAME (w->frame);
21340 int mouse_face_overwritten_p = 0;
21341
21342 while (w && !FRAME_GARBAGED_P (f))
cfe03a41 21343 {
fa3c6b4d
KS
21344 if (!NILP (w->hchild))
21345 mouse_face_overwritten_p
21346 |= expose_window_tree (XWINDOW (w->hchild), r);
21347 else if (!NILP (w->vchild))
21348 mouse_face_overwritten_p
21349 |= expose_window_tree (XWINDOW (w->vchild), r);
21350 else
21351 mouse_face_overwritten_p |= expose_window (w, r);
21352
21353 w = NILP (w->next) ? NULL : XWINDOW (w->next);
cfe03a41 21354 }
cfe03a41 21355
fa3c6b4d
KS
21356 return mouse_face_overwritten_p;
21357}
cfe03a41 21358
cfe03a41 21359
fa3c6b4d
KS
21360/* EXPORT:
21361 Redisplay an exposed area of frame F. X and Y are the upper-left
21362 corner of the exposed rectangle. W and H are width and height of
21363 the exposed area. All are pixel values. W or H zero means redraw
21364 the entire frame. */
cfe03a41 21365
fa3c6b4d
KS
21366void
21367expose_frame (f, x, y, w, h)
21368 struct frame *f;
21369 int x, y, w, h;
21370{
21371 XRectangle r;
21372 int mouse_face_overwritten_p = 0;
21373
21374 TRACE ((stderr, "expose_frame "));
21375
21376 /* No need to redraw if frame will be redrawn soon. */
21377 if (FRAME_GARBAGED_P (f))
cfe03a41 21378 {
fa3c6b4d
KS
21379 TRACE ((stderr, " garbaged\n"));
21380 return;
cfe03a41 21381 }
7d0393cf 21382
fa3c6b4d
KS
21383#ifdef HAVE_CARBON
21384 /* MAC_TODO: this is a kludge, but if scroll bars are not activated
21385 or deactivated here, for unknown reasons, activated scroll bars
21386 are shown in deactivated frames in some instances. */
21387 if (f == FRAME_MAC_DISPLAY_INFO (f)->x_focus_frame)
21388 activate_scroll_bars (f);
21389 else
21390 deactivate_scroll_bars (f);
21391#endif
cfe03a41 21392
fa3c6b4d
KS
21393 /* If basic faces haven't been realized yet, there is no point in
21394 trying to redraw anything. This can happen when we get an expose
21395 event while Emacs is starting, e.g. by moving another window. */
21396 if (FRAME_FACE_CACHE (f) == NULL
21397 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
21398 {
21399 TRACE ((stderr, " no faces\n"));
21400 return;
21401 }
cfe03a41 21402
fa3c6b4d 21403 if (w == 0 || h == 0)
cfe03a41 21404 {
fa3c6b4d 21405 r.x = r.y = 0;
da8b7f4f
KS
21406 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
21407 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
fa3c6b4d
KS
21408 }
21409 else
21410 {
21411 r.x = x;
21412 r.y = y;
21413 r.width = w;
21414 r.height = h;
cfe03a41
KS
21415 }
21416
fa3c6b4d
KS
21417 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
21418 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
21419
21420 if (WINDOWP (f->tool_bar_window))
21421 mouse_face_overwritten_p
21422 |= expose_window (XWINDOW (f->tool_bar_window), &r);
21423
21424#ifdef HAVE_X_WINDOWS
21425#ifndef MSDOS
21426#ifndef USE_X_TOOLKIT
21427 if (WINDOWP (f->menu_bar_window))
21428 mouse_face_overwritten_p
21429 |= expose_window (XWINDOW (f->menu_bar_window), &r);
21430#endif /* not USE_X_TOOLKIT */
21431#endif
21432#endif
21433
21434 /* Some window managers support a focus-follows-mouse style with
21435 delayed raising of frames. Imagine a partially obscured frame,
21436 and moving the mouse into partially obscured mouse-face on that
21437 frame. The visible part of the mouse-face will be highlighted,
21438 then the WM raises the obscured frame. With at least one WM, KDE
21439 2.1, Emacs is not getting any event for the raising of the frame
21440 (even tried with SubstructureRedirectMask), only Expose events.
21441 These expose events will draw text normally, i.e. not
21442 highlighted. Which means we must redo the highlight here.
21443 Subsume it under ``we love X''. --gerd 2001-08-15 */
21444 /* Included in Windows version because Windows most likely does not
21445 do the right thing if any third party tool offers
21446 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
21447 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
21448 {
21449 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21450 if (f == dpyinfo->mouse_face_mouse_frame)
21451 {
21452 int x = dpyinfo->mouse_face_mouse_x;
21453 int y = dpyinfo->mouse_face_mouse_y;
21454 clear_mouse_face (dpyinfo);
21455 note_mouse_highlight (f, x, y);
21456 }
21457 }
cfe03a41
KS
21458}
21459
133c764e 21460
fa3c6b4d
KS
21461/* EXPORT:
21462 Determine the intersection of two rectangles R1 and R2. Return
21463 the intersection in *RESULT. Value is non-zero if RESULT is not
21464 empty. */
133c764e 21465
fa3c6b4d
KS
21466int
21467x_intersect_rectangles (r1, r2, result)
21468 XRectangle *r1, *r2, *result;
133c764e 21469{
fa3c6b4d
KS
21470 XRectangle *left, *right;
21471 XRectangle *upper, *lower;
21472 int intersection_p = 0;
21473
21474 /* Rearrange so that R1 is the left-most rectangle. */
21475 if (r1->x < r2->x)
21476 left = r1, right = r2;
21477 else
21478 left = r2, right = r1;
21479
21480 /* X0 of the intersection is right.x0, if this is inside R1,
21481 otherwise there is no intersection. */
21482 if (right->x <= left->x + left->width)
133c764e 21483 {
fa3c6b4d 21484 result->x = right->x;
133c764e 21485
fa3c6b4d
KS
21486 /* The right end of the intersection is the minimum of the
21487 the right ends of left and right. */
21488 result->width = (min (left->x + left->width, right->x + right->width)
21489 - result->x);
133c764e 21490
fa3c6b4d
KS
21491 /* Same game for Y. */
21492 if (r1->y < r2->y)
21493 upper = r1, lower = r2;
21494 else
21495 upper = r2, lower = r1;
133c764e 21496
fa3c6b4d
KS
21497 /* The upper end of the intersection is lower.y0, if this is inside
21498 of upper. Otherwise, there is no intersection. */
21499 if (lower->y <= upper->y + upper->height)
21500 {
21501 result->y = lower->y;
21502
21503 /* The lower end of the intersection is the minimum of the lower
21504 ends of upper and lower. */
21505 result->height = (min (lower->y + lower->height,
21506 upper->y + upper->height)
21507 - result->y);
21508 intersection_p = 1;
133c764e
KS
21509 }
21510 }
fa3c6b4d
KS
21511
21512 return intersection_p;
133c764e
KS
21513}
21514
fa3c6b4d
KS
21515#endif /* HAVE_WINDOW_SYSTEM */
21516
cfe03a41 21517\f
5f5c8ee5
GM
21518/***********************************************************************
21519 Initialization
21520 ***********************************************************************/
21521
a2889657
JB
21522void
21523syms_of_xdisp ()
21524{
c6e89d6c
GM
21525 Vwith_echo_area_save_vector = Qnil;
21526 staticpro (&Vwith_echo_area_save_vector);
5f5c8ee5 21527
c6e89d6c
GM
21528 Vmessage_stack = Qnil;
21529 staticpro (&Vmessage_stack);
2311178e 21530
735c094c 21531 Qinhibit_redisplay = intern ("inhibit-redisplay");
c6e89d6c 21532 staticpro (&Qinhibit_redisplay);
735c094c 21533
b14bc55e
RS
21534 message_dolog_marker1 = Fmake_marker ();
21535 staticpro (&message_dolog_marker1);
21536 message_dolog_marker2 = Fmake_marker ();
21537 staticpro (&message_dolog_marker2);
21538 message_dolog_marker3 = Fmake_marker ();
21539 staticpro (&message_dolog_marker3);
21540
5f5c8ee5 21541#if GLYPH_DEBUG
7d4cc828 21542 defsubr (&Sdump_frame_glyph_matrix);
5f5c8ee5
GM
21543 defsubr (&Sdump_glyph_matrix);
21544 defsubr (&Sdump_glyph_row);
e037b9ec 21545 defsubr (&Sdump_tool_bar_row);
62397849 21546 defsubr (&Strace_redisplay);
bf9249e3 21547 defsubr (&Strace_to_stderr);
5f5c8ee5 21548#endif
99a5de87 21549#ifdef HAVE_WINDOW_SYSTEM
57c28064 21550 defsubr (&Stool_bar_lines_needed);
493fdc3c 21551 defsubr (&Slookup_image_map);
99a5de87 21552#endif
8143e6ab 21553 defsubr (&Sformat_mode_line);
5f5c8ee5 21554
cf074754
RS
21555 staticpro (&Qmenu_bar_update_hook);
21556 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
21557
d46fb96a 21558 staticpro (&Qoverriding_terminal_local_map);
7079aefa 21559 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
d46fb96a 21560
399164b4
KH
21561 staticpro (&Qoverriding_local_map);
21562 Qoverriding_local_map = intern ("overriding-local-map");
21563
75c43375
RS
21564 staticpro (&Qwindow_scroll_functions);
21565 Qwindow_scroll_functions = intern ("window-scroll-functions");
21566
e0bfbde6
RS
21567 staticpro (&Qredisplay_end_trigger_functions);
21568 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
2311178e 21569
2e54982e
RS
21570 staticpro (&Qinhibit_point_motion_hooks);
21571 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
21572
9499d71b
GM
21573 QCdata = intern (":data");
21574 staticpro (&QCdata);
5f5c8ee5 21575 Qdisplay = intern ("display");
f3751a65 21576 staticpro (&Qdisplay);
5f5c8ee5
GM
21577 Qspace_width = intern ("space-width");
21578 staticpro (&Qspace_width);
5f5c8ee5
GM
21579 Qraise = intern ("raise");
21580 staticpro (&Qraise);
21581 Qspace = intern ("space");
21582 staticpro (&Qspace);
f3751a65
GM
21583 Qmargin = intern ("margin");
21584 staticpro (&Qmargin);
493fdc3c
KS
21585 Qpointer = intern ("pointer");
21586 staticpro (&Qpointer);
5f5c8ee5 21587 Qleft_margin = intern ("left-margin");
f3751a65 21588 staticpro (&Qleft_margin);
5f5c8ee5 21589 Qright_margin = intern ("right-margin");
f3751a65 21590 staticpro (&Qright_margin);
da06e4d7
KS
21591 Qcenter = intern ("center");
21592 staticpro (&Qcenter);
5f5c8ee5
GM
21593 QCalign_to = intern (":align-to");
21594 staticpro (&QCalign_to);
5f5c8ee5
GM
21595 QCrelative_width = intern (":relative-width");
21596 staticpro (&QCrelative_width);
21597 QCrelative_height = intern (":relative-height");
21598 staticpro (&QCrelative_height);
21599 QCeval = intern (":eval");
21600 staticpro (&QCeval);
0fcf414f
RS
21601 QCpropertize = intern (":propertize");
21602 staticpro (&QCpropertize);
886bd6f2
GM
21603 QCfile = intern (":file");
21604 staticpro (&QCfile);
5f5c8ee5
GM
21605 Qfontified = intern ("fontified");
21606 staticpro (&Qfontified);
21607 Qfontification_functions = intern ("fontification-functions");
21608 staticpro (&Qfontification_functions);
5f5c8ee5
GM
21609 Qtrailing_whitespace = intern ("trailing-whitespace");
21610 staticpro (&Qtrailing_whitespace);
21611 Qimage = intern ("image");
21612 staticpro (&Qimage);
493fdc3c
KS
21613 QCmap = intern (":map");
21614 staticpro (&QCmap);
21615 QCpointer = intern (":pointer");
21616 staticpro (&QCpointer);
21617 Qrect = intern ("rect");
21618 staticpro (&Qrect);
21619 Qcircle = intern ("circle");
21620 staticpro (&Qcircle);
21621 Qpoly = intern ("poly");
21622 staticpro (&Qpoly);
ad4f174e
GM
21623 Qmessage_truncate_lines = intern ("message-truncate-lines");
21624 staticpro (&Qmessage_truncate_lines);
af79bccb
RS
21625 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
21626 staticpro (&Qcursor_in_non_selected_windows);
6422c1d7
GM
21627 Qgrow_only = intern ("grow-only");
21628 staticpro (&Qgrow_only);
e1477f43
GM
21629 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
21630 staticpro (&Qinhibit_menubar_update);
30a3f61c
GM
21631 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
21632 staticpro (&Qinhibit_eval_during_redisplay);
b384d6f8
GM
21633 Qposition = intern ("position");
21634 staticpro (&Qposition);
21635 Qbuffer_position = intern ("buffer-position");
21636 staticpro (&Qbuffer_position);
21637 Qobject = intern ("object");
21638 staticpro (&Qobject);
cfe03a41
KS
21639 Qbar = intern ("bar");
21640 staticpro (&Qbar);
21641 Qhbar = intern ("hbar");
21642 staticpro (&Qhbar);
21643 Qbox = intern ("box");
21644 staticpro (&Qbox);
21645 Qhollow = intern ("hollow");
21646 staticpro (&Qhollow);
493fdc3c
KS
21647 Qhand = intern ("hand");
21648 staticpro (&Qhand);
21649 Qarrow = intern ("arrow");
21650 staticpro (&Qarrow);
21651 Qtext = intern ("text");
21652 staticpro (&Qtext);
c53a1624
RS
21653 Qrisky_local_variable = intern ("risky-local-variable");
21654 staticpro (&Qrisky_local_variable);
26683087
RS
21655 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
21656 staticpro (&Qinhibit_free_realized_faces);
5f5c8ee5 21657
be21b925
KS
21658 list_of_error = Fcons (Fcons (intern ("error"),
21659 Fcons (intern ("void-variable"), Qnil)),
21660 Qnil);
7033d6df
RS
21661 staticpro (&list_of_error);
21662
351b5434
KS
21663 Qlast_arrow_position = intern ("last-arrow-position");
21664 staticpro (&Qlast_arrow_position);
21665 Qlast_arrow_string = intern ("last-arrow-string");
21666 staticpro (&Qlast_arrow_string);
21667
21668 Qoverlay_arrow_string = intern ("overlay-arrow-string");
21669 staticpro (&Qoverlay_arrow_string);
21670 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
21671 staticpro (&Qoverlay_arrow_bitmap);
2311178e 21672
c6e89d6c
GM
21673 echo_buffer[0] = echo_buffer[1] = Qnil;
21674 staticpro (&echo_buffer[0]);
21675 staticpro (&echo_buffer[1]);
21676
21677 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
21678 staticpro (&echo_area_buffer[0]);
21679 staticpro (&echo_area_buffer[1]);
a2889657 21680
6a94510a
GM
21681 Vmessages_buffer_name = build_string ("*Messages*");
21682 staticpro (&Vmessages_buffer_name);
0fcf414f
RS
21683
21684 mode_line_proptrans_alist = Qnil;
21685 staticpro (&mode_line_proptrans_alist);
2311178e 21686
fec8f23e
KS
21687 mode_line_string_list = Qnil;
21688 staticpro (&mode_line_string_list);
21689
fa3c6b4d
KS
21690 help_echo_string = Qnil;
21691 staticpro (&help_echo_string);
21692 help_echo_object = Qnil;
21693 staticpro (&help_echo_object);
21694 help_echo_window = Qnil;
21695 staticpro (&help_echo_window);
21696 previous_help_echo_string = Qnil;
21697 staticpro (&previous_help_echo_string);
21698 help_echo_pos = -1;
21699
21700#ifdef HAVE_WINDOW_SYSTEM
21701 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
21702 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
21703For example, if a block cursor is over a tab, it will be drawn as
21704wide as that tab on the display. */);
21705 x_stretch_cursor_p = 0;
21706#endif
21707
7ee72033 21708 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
88e6b646 21709 doc: /* *Non-nil means highlight trailing whitespace.
228299fa 21710The face used for trailing whitespace is `trailing-whitespace'. */);
8f897821
GM
21711 Vshow_trailing_whitespace = Qnil;
21712
493fdc3c 21713 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
88e6b646 21714 doc: /* *The pointer shape to show in void text areas.
493fdc3c 21715Nil means to show the text pointer. Other options are `arrow', `text',
be21b925 21716`hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
493fdc3c 21717 Vvoid_text_area_pointer = Qarrow;
f65536fa 21718
7ee72033
MB
21719 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
21720 doc: /* Non-nil means don't actually do any redisplay.
228299fa 21721This is used for internal purposes. */);
735c094c
KH
21722 Vinhibit_redisplay = Qnil;
21723
7ee72033
MB
21724 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
21725 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
a2889657
JB
21726 Vglobal_mode_string = Qnil;
21727
7ee72033
MB
21728 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
21729 doc: /* Marker for where to display an arrow on top of the buffer text.
228299fa
GM
21730This must be the beginning of a line in order to work.
21731See also `overlay-arrow-string'. */);
a2889657
JB
21732 Voverlay_arrow_position = Qnil;
21733
7ee72033 21734 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
351b5434
KS
21735 doc: /* String to display as an arrow in non-window frames.
21736See also `overlay-arrow-position'. */);
a2889657
JB
21737 Voverlay_arrow_string = Qnil;
21738
351b5434
KS
21739 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
21740 doc: /* List of variables (symbols) which hold markers for overlay arrows.
21741The symbols on this list are examined during redisplay to determine
21742where to display overlay arrows. */);
21743 Voverlay_arrow_variable_list
21744 = Fcons (intern ("overlay-arrow-position"), Qnil);
21745
7ee72033
MB
21746 DEFVAR_INT ("scroll-step", &scroll_step,
21747 doc: /* *The number of lines to try scrolling a window by when point moves out.
228299fa
GM
21748If that fails to bring point back on frame, point is centered instead.
21749If this is zero, point is always centered after it moves off frame.
21750If you want scrolling to always be a line at a time, you should set
21751`scroll-conservatively' to a large value rather than set this to 1. */);
21752
7ee72033
MB
21753 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
21754 doc: /* *Scroll up to this many lines, to bring point back on screen.
228299fa
GM
21755A value of zero means to scroll the text to center point vertically
21756in the window. */);
0789adb2
RS
21757 scroll_conservatively = 0;
21758
7ee72033
MB
21759 DEFVAR_INT ("scroll-margin", &scroll_margin,
21760 doc: /* *Number of lines of margin at the top and bottom of a window.
228299fa
GM
21761Recenter the window whenever point gets within this many lines
21762of the top or bottom of the window. */);
9afd2168
RS
21763 scroll_margin = 0;
21764
f65536fa
KS
21765 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
21766 doc: /* Pixels per inch on current display.
21767Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
21768 Vdisplay_pixels_per_inch = make_float (72.0);
21769
5f5c8ee5 21770#if GLYPH_DEBUG
7ee72033 21771 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
5f5c8ee5 21772#endif
a2889657
JB
21773
21774 DEFVAR_BOOL ("truncate-partial-width-windows",
7ee72033
MB
21775 &truncate_partial_width_windows,
21776 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
a2889657
JB
21777 truncate_partial_width_windows = 1;
21778
7ee72033
MB
21779 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
21780 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
228299fa 21781Any other value means to use the appropriate face, `mode-line',
ccfe8f57 21782`header-line', or `menu' respectively. */);
1862a24e 21783 mode_line_inverse_video = 1;
aa6d10fa 21784
7ee72033
MB
21785 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
21786 doc: /* *Maximum buffer size for which line number should be displayed.
228299fa
GM
21787If the buffer is bigger than this, the line number does not appear
21788in the mode line. A value of nil means no limit. */);
090703f4 21789 Vline_number_display_limit = Qnil;
fba9ce76 21790
090703f4 21791 DEFVAR_INT ("line-number-display-limit-width",
7ee72033
MB
21792 &line_number_display_limit_width,
21793 doc: /* *Maximum line width (in characters) for line number display.
228299fa
GM
21794If the average length of the lines near point is bigger than this, then the
21795line number may be omitted from the mode line. */);
5d121aec
KH
21796 line_number_display_limit_width = 200;
21797
7ee72033
MB
21798 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
21799 doc: /* *Non-nil means highlight region even in nonselected windows. */);
293a54ce 21800 highlight_nonselected_windows = 0;
d39b6696 21801
7ee72033
MB
21802 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
21803 doc: /* Non-nil if more than one frame is visible on this display.
228299fa
GM
21804Minibuffer-only frames don't count, but iconified frames do.
21805This variable is not guaranteed to be accurate except while processing
21806`frame-title-format' and `icon-title-format'. */);
21807
7ee72033
MB
21808 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
21809 doc: /* Template for displaying the title bar of visible frames.
228299fa
GM
21810\(Assuming the window manager supports this feature.)
21811This variable has the same structure as `mode-line-format' (which see),
21812and is used only on frames for which no explicit name has been set
21813\(see `modify-frame-parameters'). */);
f65536fa 21814
7ee72033
MB
21815 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
21816 doc: /* Template for displaying the title bar of an iconified frame.
228299fa
GM
21817\(Assuming the window manager supports this feature.)
21818This variable has the same structure as `mode-line-format' (which see),
21819and is used only on frames for which no explicit name has been set
21820\(see `modify-frame-parameters'). */);
d39b6696
KH
21821 Vicon_title_format
21822 = Vframe_title_format
21823 = Fcons (intern ("multiple-frames"),
21824 Fcons (build_string ("%b"),
3ebf0ea9 21825 Fcons (Fcons (empty_string,
d39b6696
KH
21826 Fcons (intern ("invocation-name"),
21827 Fcons (build_string ("@"),
21828 Fcons (intern ("system-name"),
21829 Qnil)))),
21830 Qnil)));
5992c4f7 21831
7ee72033
MB
21832 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
21833 doc: /* Maximum number of lines to keep in the message log buffer.
228299fa
GM
21834If nil, disable message logging. If t, log messages but don't truncate
21835the buffer when it becomes large. */);
ac90c44f 21836 Vmessage_log_max = make_number (50);
08b610e4 21837
7ee72033
MB
21838 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
21839 doc: /* Functions called before redisplay, if window sizes have changed.
228299fa
GM
21840The value should be a list of functions that take one argument.
21841Just before redisplay, for each frame, if any of its windows have changed
21842size since the last redisplay, or have been split or deleted,
21843all the functions in the list are called, with the frame as argument. */);
08b610e4 21844 Vwindow_size_change_functions = Qnil;
75c43375 21845
7ee72033
MB
21846 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
21847 doc: /* List of Functions to call before redisplaying a window with scrolling.
228299fa
GM
21848Each function is called with two arguments, the window
21849and its new display-start position. Note that the value of `window-end'
21850is not valid when these functions are called. */);
75c43375 21851 Vwindow_scroll_functions = Qnil;
2311178e 21852
fa3c6b4d
KS
21853 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
21854 doc: /* *Non-nil means autoselect window with mouse pointer. */);
21855 mouse_autoselect_window = 0;
21856
7ee72033
MB
21857 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
21858 doc: /* *Non-nil means automatically resize tool-bars.
228299fa
GM
21859This increases a tool-bar's height if not all tool-bar items are visible.
21860It decreases a tool-bar's height when it would display blank lines
21861otherwise. */);
e037b9ec 21862 auto_resize_tool_bars_p = 1;
2311178e 21863
7ee72033
MB
21864 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
21865 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
e037b9ec 21866 auto_raise_tool_bar_buttons_p = 1;
5f5c8ee5 21867
7ee72033
MB
21868 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
21869 doc: /* *Margin around tool-bar buttons in pixels.
228299fa 21870If an integer, use that for both horizontal and vertical margins.
f6c89f27 21871Otherwise, value should be a pair of integers `(HORZ . VERT)' with
228299fa
GM
21872HORZ specifying the horizontal margin, and VERT specifying the
21873vertical margin. */);
c3d76173 21874 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
5f5c8ee5 21875
7ee72033 21876 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
6da3c85b 21877 doc: /* *Relief thickness of tool-bar buttons. */);
c3d76173 21878 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
5f5c8ee5 21879
7ee72033
MB
21880 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
21881 doc: /* List of functions to call to fontify regions of text.
228299fa
GM
21882Each function is called with one argument POS. Functions must
21883fontify a region starting at POS in the current buffer, and give
21884fontified regions the property `fontified'. */);
5f5c8ee5 21885 Vfontification_functions = Qnil;
6b9f0906 21886 Fmake_variable_buffer_local (Qfontification_functions);
7bbe686f
AI
21887
21888 DEFVAR_BOOL ("unibyte-display-via-language-environment",
7ee72033
MB
21889 &unibyte_display_via_language_environment,
21890 doc: /* *Non-nil means display unibyte text according to language environment.
228299fa
GM
21891Specifically this means that unibyte non-ASCII characters
21892are displayed by converting them to the equivalent multibyte characters
21893according to the current language environment. As a result, they are
21894displayed according to the current fontset. */);
7bbe686f 21895 unibyte_display_via_language_environment = 0;
c6e89d6c 21896
7ee72033
MB
21897 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
21898 doc: /* *Maximum height for resizing mini-windows.
228299fa
GM
21899If a float, it specifies a fraction of the mini-window frame's height.
21900If an integer, it specifies a number of lines. */);
c6e89d6c 21901 Vmax_mini_window_height = make_float (0.25);
6422c1d7 21902
7ee72033
MB
21903 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
21904 doc: /* *How to resize mini-windows.
228299fa
GM
21905A value of nil means don't automatically resize mini-windows.
21906A value of t means resize them to fit the text displayed in them.
21907A value of `grow-only', the default, means let mini-windows grow
21908only, until their display becomes empty, at which point the windows
21909go back to their normal size. */);
6422c1d7
GM
21910 Vresize_mini_windows = Qgrow_only;
21911
cfe03a41
KS
21912 DEFVAR_LISP ("cursor-in-non-selected-windows",
21913 &Vcursor_in_non_selected_windows,
21914 doc: /* *Cursor type to display in non-selected windows.
21915t means to use hollow box cursor. See `cursor-type' for other values. */);
21916 Vcursor_in_non_selected_windows = Qt;
21917
cfe03a41
KS
21918 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
21919 doc: /* Alist specifying how to blink the cursor off.
21920Each element has the form (ON-STATE . OFF-STATE). Whenever the
21921`cursor-type' frame-parameter or variable equals ON-STATE,
21922comparing using `equal', Emacs uses OFF-STATE to specify
21923how to blink it off. */);
21924 Vblink_cursor_alist = Qnil;
2311178e 21925
e76d28d5 21926 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
7ee72033 21927 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
d475bcb8 21928 automatic_hscrolling_p = 1;
1df7e8f0 21929
e76d28d5 21930 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
1df7e8f0
EZ
21931 doc: /* *How many columns away from the window edge point is allowed to get
21932before automatic hscrolling will horizontally scroll the window. */);
e76d28d5 21933 hscroll_margin = 5;
1df7e8f0 21934
e76d28d5 21935 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
1df7e8f0
EZ
21936 doc: /* *How many columns to scroll the window when point gets too close to the edge.
21937When point is less than `automatic-hscroll-margin' columns from the window
21938edge, automatic hscrolling will scroll the window by the amount of columns
21939determined by this variable. If its value is a positive integer, scroll that
21940many columns. If it's a positive floating-point number, it specifies the
21941fraction of the window's width to scroll. If it's nil or zero, point will be
21942centered horizontally after the scroll. Any other value, including negative
21943numbers, are treated as if the value were zero.
21944
21945Automatic hscrolling always moves point outside the scroll margin, so if
21946point was more than scroll step columns inside the margin, the window will
21947scroll more than the value given by the scroll step.
21948
21949Note that the lower bound for automatic hscrolling specified by `scroll-left'
21950and `scroll-right' overrides this variable's effect. */);
e76d28d5 21951 Vhscroll_step = make_number (0);
2311178e 21952
7ee72033
MB
21953 DEFVAR_LISP ("image-types", &Vimage_types,
21954 doc: /* List of supported image types.
228299fa 21955Each element of the list is a symbol for a supported image type. */);
e00daaa0 21956 Vimage_types = Qnil;
2311178e 21957
7ee72033
MB
21958 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
21959 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
228299fa 21960Bind this around calls to `message' to let it take effect. */);
ad4f174e 21961 message_truncate_lines = 0;
0bca8940 21962
7ee72033
MB
21963 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
21964 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
228299fa 21965Can be used to update submenus whose contents should vary. */);
6422c1d7 21966 Vmenu_bar_update_hook = Qnil;
2311178e 21967
7ee72033
MB
21968 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
21969 doc: /* Non-nil means don't update menu bars. Internal use only. */);
e1477f43 21970 inhibit_menubar_update = 0;
30a3f61c 21971
7ee72033
MB
21972 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
21973 doc: /* Non-nil means don't eval Lisp during redisplay. */);
30a3f61c 21974 inhibit_eval_during_redisplay = 0;
76cb5e06 21975
26683087
RS
21976 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
21977 doc: /* Non-nil means don't free realized faces. Internal use only. */);
21978 inhibit_free_realized_faces = 0;
21979
69d1f7c9 21980#if GLYPH_DEBUG
76cb5e06
GM
21981 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
21982 doc: /* Inhibit try_window_id display optimization. */);
21983 inhibit_try_window_id = 0;
21984
21985 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
21986 doc: /* Inhibit try_window_reusing display optimization. */);
21987 inhibit_try_window_reusing = 0;
21988
21989 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
21990 doc: /* Inhibit try_cursor_movement display optimization. */);
21991 inhibit_try_cursor_movement = 0;
21992#endif /* GLYPH_DEBUG */
a2889657
JB
21993}
21994
5f5c8ee5
GM
21995
21996/* Initialize this module when Emacs starts. */
21997
dfcf069d 21998void
a2889657
JB
21999init_xdisp ()
22000{
22001 Lisp_Object root_window;
5f5c8ee5 22002 struct window *mini_w;
a2889657 22003
04612a64
GM
22004 current_header_line_height = current_mode_line_height = -1;
22005
5f5c8ee5 22006 CHARPOS (this_line_start_pos) = 0;
a2889657
JB
22007
22008 mini_w = XWINDOW (minibuf_window);
11e82b76 22009 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
a2889657 22010
a2889657
JB
22011 if (!noninteractive)
22012 {
5f5c8ee5
GM
22013 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
22014 int i;
22015
da8b7f4f 22016 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
12c226c5 22017 set_window_height (root_window,
da8b7f4f 22018 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
12c226c5 22019 0);
da8b7f4f 22020 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
a2889657
JB
22021 set_window_height (minibuf_window, 1, 0);
22022
da8b7f4f
KS
22023 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
22024 mini_w->total_cols = make_number (FRAME_COLS (f));
5f5c8ee5
GM
22025
22026 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
22027 scratch_glyph_row.glyphs[TEXT_AREA + 1]
22028 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
22029
2311178e 22030 /* The default ellipsis glyphs `...'. */
5f5c8ee5 22031 for (i = 0; i < 3; ++i)
ac90c44f 22032 default_invis_vector[i] = make_number ('.');
a2889657 22033 }
5f5c8ee5 22034
5f5c8ee5 22035 {
93da8435
SM
22036 /* Allocate the buffer for frame titles.
22037 Also used for `format-mode-line'. */
5f5c8ee5
GM
22038 int size = 100;
22039 frame_title_buf = (char *) xmalloc (size);
22040 frame_title_buf_end = frame_title_buf + size;
22041 frame_title_ptr = NULL;
22042 }
2311178e 22043
21fdfb65 22044 help_echo_showing_p = 0;
a2889657 22045}
5f5c8ee5
GM
22046
22047
ab5796a9
MB
22048/* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
22049 (do not change this comment) */