(Fselect_frame, Fset_frame_selected_window)
[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
AC
200#include "macterm.h"
201#endif
a2889657 202
1dabd0cf
KS
203#ifndef FRAME_X_OUTPUT
204#define FRAME_X_OUTPUT(f) ((f)->output_data.x)
205#endif
206
5f5c8ee5
GM
207#define INFINITY 10000000
208
488dd4c4
JD
209#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
210 || defined (USE_GTK)
2e621225 211extern void set_frame_menubar P_ ((struct frame *f, int, int));
cd6dfed6 212extern int pending_menu_activation;
76412d64
RS
213#endif
214
a2889657
JB
215extern int interrupt_input;
216extern int command_loop_level;
217
b6436d4e 218extern int minibuffer_auto_raise;
b2b5455d 219extern Lisp_Object Vminibuffer_list;
b6436d4e 220
c4628384 221extern Lisp_Object Qface;
fec8f23e 222extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
c4628384 223
399164b4
KH
224extern Lisp_Object Voverriding_local_map;
225extern Lisp_Object Voverriding_local_map_menu_flag;
5f5c8ee5 226extern Lisp_Object Qmenu_item;
0b063c27 227extern Lisp_Object Qwhen;
fa3c6b4d 228extern Lisp_Object Qhelp_echo;
399164b4 229
d46fb96a 230Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
75c43375 231Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
e0bfbde6 232Lisp_Object Qredisplay_end_trigger_functions;
2e54982e 233Lisp_Object Qinhibit_point_motion_hooks;
0b063c27 234Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
5f5c8ee5 235Lisp_Object Qfontified;
6422c1d7 236Lisp_Object Qgrow_only;
869fb12c 237Lisp_Object Qinhibit_eval_during_redisplay;
b384d6f8 238Lisp_Object Qbuffer_position, Qposition, Qobject;
5f5c8ee5 239
cfe03a41
KS
240/* Cursor shapes */
241Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
242
493fdc3c
KS
243/* Pointer shapes */
244Lisp_Object Qarrow, Qhand, Qtext;
245
c53a1624
RS
246Lisp_Object Qrisky_local_variable;
247
7033d6df
RS
248/* Holds the list (error). */
249Lisp_Object list_of_error;
250
5f5c8ee5
GM
251/* Functions called to fontify regions of text. */
252
253Lisp_Object Vfontification_functions;
254Lisp_Object Qfontification_functions;
255
fa3c6b4d
KS
256/* Non-zero means automatically select any window when the mouse
257 cursor moves into it. */
258int mouse_autoselect_window;
259
e037b9ec 260/* Non-zero means draw tool bar buttons raised when the mouse moves
5f5c8ee5
GM
261 over them. */
262
e037b9ec 263int auto_raise_tool_bar_buttons_p;
5f5c8ee5 264
e037b9ec 265/* Margin around tool bar buttons in pixels. */
5f5c8ee5 266
35a41507 267Lisp_Object Vtool_bar_button_margin;
5f5c8ee5 268
e037b9ec 269/* Thickness of shadow to draw around tool bar buttons. */
5f5c8ee5 270
31ade731 271EMACS_INT tool_bar_button_relief;
5f5c8ee5 272
e037b9ec 273/* Non-zero means automatically resize tool-bars so that all tool-bar
5f5c8ee5
GM
274 items are visible, and no blank lines remain. */
275
e037b9ec 276int auto_resize_tool_bars_p;
399164b4 277
fa3c6b4d
KS
278/* Non-zero means draw block and hollow cursor as wide as the glyph
279 under it. For example, if a block cursor is over a tab, it will be
280 drawn as wide as that tab on the display. */
281
282int x_stretch_cursor_p;
283
735c094c
KH
284/* Non-nil means don't actually do any redisplay. */
285
286Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
287
30a3f61c
GM
288/* Non-zero means Lisp evaluation during redisplay is inhibited. */
289
869fb12c 290int inhibit_eval_during_redisplay;
30a3f61c 291
5f5c8ee5
GM
292/* Names of text properties relevant for redisplay. */
293
493fdc3c 294Lisp_Object Qdisplay;
4b41cebb 295extern Lisp_Object Qface, Qinvisible, Qwidth;
5f5c8ee5
GM
296
297/* Symbols used in text property values. */
298
f65536fa 299Lisp_Object Vdisplay_pixels_per_inch;
5f5c8ee5 300Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
a7e27ef7 301Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
e893970b 302Lisp_Object Qslice;
da06e4d7 303Lisp_Object Qcenter;
493fdc3c 304Lisp_Object Qmargin, Qpointer;
17b01ffc 305Lisp_Object Qline_height, Qtotal;
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
6422c1d7 673/* The variable `resize-mini-windows'. If nil, don't resize
67526daf 674 mini-windows. If t, always resize them to fit the text they
6422c1d7
GM
675 display. If `grow-only', let mini-windows grow only until they
676 become empty. */
677
678Lisp_Object Vresize_mini_windows;
679
82a7ab23
RS
680/* Buffer being redisplayed -- for redisplay_window_error. */
681
682struct buffer *displayed_buffer;
683
5f5c8ee5
GM
684/* Value returned from text property handlers (see below). */
685
686enum prop_handled
3c6595e0 687{
5f5c8ee5
GM
688 HANDLED_NORMALLY,
689 HANDLED_RECOMPUTE_PROPS,
690 HANDLED_OVERLAY_STRING_CONSUMED,
691 HANDLED_RETURN
692};
3c6595e0 693
5f5c8ee5
GM
694/* A description of text properties that redisplay is interested
695 in. */
3c6595e0 696
5f5c8ee5
GM
697struct props
698{
699 /* The name of the property. */
700 Lisp_Object *name;
90adcf20 701
5f5c8ee5
GM
702 /* A unique index for the property. */
703 enum prop_idx idx;
704
705 /* A handler function called to set up iterator IT from the property
706 at IT's current position. Value is used to steer handle_stop. */
707 enum prop_handled (*handler) P_ ((struct it *it));
708};
709
710static enum prop_handled handle_face_prop P_ ((struct it *));
711static enum prop_handled handle_invisible_prop P_ ((struct it *));
712static enum prop_handled handle_display_prop P_ ((struct it *));
260a86a0 713static enum prop_handled handle_composition_prop P_ ((struct it *));
5f5c8ee5
GM
714static enum prop_handled handle_overlay_change P_ ((struct it *));
715static enum prop_handled handle_fontified_prop P_ ((struct it *));
716
717/* Properties handled by iterators. */
718
719static struct props it_props[] =
5992c4f7 720{
5f5c8ee5
GM
721 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
722 /* Handle `face' before `display' because some sub-properties of
723 `display' need to know the face. */
724 {&Qface, FACE_PROP_IDX, handle_face_prop},
725 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
726 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
260a86a0 727 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
5f5c8ee5
GM
728 {NULL, 0, NULL}
729};
5992c4f7 730
5f5c8ee5
GM
731/* Value is the position described by X. If X is a marker, value is
732 the marker_position of X. Otherwise, value is X. */
12adba34 733
5f5c8ee5 734#define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
12adba34 735
5f5c8ee5 736/* Enumeration returned by some move_it_.* functions internally. */
12adba34 737
5f5c8ee5
GM
738enum move_it_result
739{
740 /* Not used. Undefined value. */
741 MOVE_UNDEFINED,
bab29e15 742
5f5c8ee5
GM
743 /* Move ended at the requested buffer position or ZV. */
744 MOVE_POS_MATCH_OR_ZV,
bab29e15 745
5f5c8ee5
GM
746 /* Move ended at the requested X pixel position. */
747 MOVE_X_REACHED,
12adba34 748
5f5c8ee5
GM
749 /* Move within a line ended at the end of a line that must be
750 continued. */
751 MOVE_LINE_CONTINUED,
2311178e 752
5f5c8ee5
GM
753 /* Move within a line ended at the end of a line that would
754 be displayed truncated. */
755 MOVE_LINE_TRUNCATED,
ff6c30e5 756
5f5c8ee5
GM
757 /* Move within a line ended at a line end. */
758 MOVE_NEWLINE_OR_CR
759};
12adba34 760
1987b083
RS
761/* This counter is used to clear the face cache every once in a while
762 in redisplay_internal. It is incremented for each redisplay.
763 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
764 cleared. */
765
766#define CLEAR_FACE_CACHE_COUNT 500
767static int clear_face_cache_count;
768
769/* Record the previous terminal frame we displayed. */
770
771static struct frame *previous_terminal_frame;
772
773/* Non-zero while redisplay_internal is in progress. */
774
775int redisplaying_p;
776
26683087
RS
777/* Non-zero means don't free realized faces. Bound while freeing
778 realized faces is dangerous because glyph matrices might still
779 reference them. */
1987b083 780
26683087
RS
781int inhibit_free_realized_faces;
782Lisp_Object Qinhibit_free_realized_faces;
ff6c30e5 783
fa3c6b4d
KS
784/* If a string, XTread_socket generates an event to display that string.
785 (The display is done in read_char.) */
786
787Lisp_Object help_echo_string;
788Lisp_Object help_echo_window;
789Lisp_Object help_echo_object;
790int help_echo_pos;
791
792/* Temporary variable for XTread_socket. */
793
794Lisp_Object previous_help_echo_string;
795
2883e85a
KS
796/* Null glyph slice */
797
798static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
fa3c6b4d 799
5f5c8ee5
GM
800\f
801/* Function prototypes. */
802
5a08cbaf 803static void setup_for_ellipsis P_ ((struct it *));
43c09969 804static void mark_window_display_accurate_1 P_ ((struct window *, int));
74bd6d65
GM
805static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
806static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
cafafe0b 807static int cursor_row_p P_ ((struct window *, struct glyph_row *));
715e84c9 808static int redisplay_mode_lines P_ ((Lisp_Object, int));
2e621225 809static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
c0a53abb
PJ
810
811#if 0
2e621225 812static int invisible_text_between_p P_ ((struct it *, int, int));
c0a53abb
PJ
813#endif
814
2e621225
GM
815static int next_element_from_ellipsis P_ ((struct it *));
816static void pint2str P_ ((char *, int, int));
525b8b09 817static void pint2hrstr P_ ((char *, int, int));
2e621225
GM
818static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
819 struct text_pos));
820static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
821static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
822static void store_frame_title_char P_ ((char));
50f80c2f 823static int store_frame_title P_ ((const unsigned char *, int, int));
2e621225
GM
824static void x_consider_frame_title P_ ((Lisp_Object));
825static void handle_stop P_ ((struct it *));
826static int tool_bar_lines_needed P_ ((struct frame *));
06568bbf 827static int single_display_prop_intangible_p P_ ((Lisp_Object));
5bcfeb49 828static void ensure_echo_area_buffers P_ ((void));
c6e89d6c
GM
829static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
830static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
23a96c77 831static int with_echo_area_buffer P_ ((struct window *, int,
23dd2d97
KR
832 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
833 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
c6e89d6c 834static void clear_garbaged_frames P_ ((void));
23dd2d97
KR
835static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
836static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
837static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
c6e89d6c 838static int display_echo_area P_ ((struct window *));
23dd2d97
KR
839static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
840static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
28514cd9 841static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
50f80c2f 842static int string_char_and_length P_ ((const unsigned char *, int, int *));
5f5c8ee5
GM
843static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
844 struct text_pos));
845static int compute_window_start_on_continuation_line P_ ((struct window *));
116d6f5c 846static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
5f5c8ee5 847static void insert_left_trunc_glyphs P_ ((struct it *));
351b5434
KS
848static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
849 Lisp_Object));
5f5c8ee5 850static void extend_face_to_end_of_line P_ ((struct it *));
4933c603 851static int append_space_for_newline P_ ((struct it *, int));
cb9d4a9f 852static int make_cursor_line_fully_visible P_ ((struct window *, int));
03b0a4b4 853static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
47589c8c 854static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
5f5c8ee5
GM
855static int trailing_whitespace_p P_ ((int));
856static int message_log_check_duplicate P_ ((int, int, int, int));
5f5c8ee5
GM
857static void push_it P_ ((struct it *));
858static void pop_it P_ ((struct it *));
859static void sync_frame_with_window_matrix_rows P_ ((struct window *));
dd429b03 860static void select_frame_for_redisplay P_ ((Lisp_Object));
5f5c8ee5 861static void redisplay_internal P_ ((int));
c6e89d6c 862static int echo_area_display P_ ((int));
5f5c8ee5
GM
863static void redisplay_windows P_ ((Lisp_Object));
864static void redisplay_window P_ ((Lisp_Object, int));
82a7ab23
RS
865static Lisp_Object redisplay_window_error ();
866static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
867static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
5f5c8ee5
GM
868static void update_menu_bar P_ ((struct frame *, int));
869static int try_window_reusing_current_matrix P_ ((struct window *));
870static int try_window_id P_ ((struct window *));
871static int display_line P_ ((struct it *));
715e84c9 872static int display_mode_lines P_ ((struct window *));
04612a64 873static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
c53a1624 874static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
fec8f23e 875static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
72f62cb5 876static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
5f5c8ee5
GM
877static void display_menu_bar P_ ((struct window *));
878static int display_count_lines P_ ((int, int, int, int, int *));
879static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
880 int, int, struct it *, int, int, int, int));
881static void compute_line_metrics P_ ((struct it *));
882static void run_redisplay_end_trigger_hook P_ ((struct it *));
5a08cbaf 883static int get_overlay_strings P_ ((struct it *, int));
5f5c8ee5 884static void next_overlay_string P_ ((struct it *));
5f5c8ee5
GM
885static void reseat P_ ((struct it *, struct text_pos, int));
886static void reseat_1 P_ ((struct it *, struct text_pos, int));
887static void back_to_previous_visible_line_start P_ ((struct it *));
888static void reseat_at_previous_visible_line_start P_ ((struct it *));
312246d1 889static void reseat_at_next_visible_line_start P_ ((struct it *, int));
5f5c8ee5
GM
890static int next_element_from_display_vector P_ ((struct it *));
891static int next_element_from_string P_ ((struct it *));
892static int next_element_from_c_string P_ ((struct it *));
893static int next_element_from_buffer P_ ((struct it *));
260a86a0 894static int next_element_from_composition P_ ((struct it *));
5f5c8ee5
GM
895static int next_element_from_image P_ ((struct it *));
896static int next_element_from_stretch P_ ((struct it *));
5a08cbaf 897static void load_overlay_strings P_ ((struct it *, int));
47d57b22
GM
898static int init_from_display_pos P_ ((struct it *, struct window *,
899 struct display_pos *));
5f5c8ee5
GM
900static void reseat_to_string P_ ((struct it *, unsigned char *,
901 Lisp_Object, int, int, int, int));
5f5c8ee5
GM
902static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
903 int, int, int));
904void move_it_vertically_backward P_ ((struct it *, int));
905static void init_to_row_start P_ ((struct it *, struct window *,
906 struct glyph_row *));
47d57b22
GM
907static int init_to_row_end P_ ((struct it *, struct window *,
908 struct glyph_row *));
5f5c8ee5 909static void back_to_previous_line_start P_ ((struct it *));
cafafe0b 910static int forward_to_next_line_start P_ ((struct it *, int *));
5f5c8ee5
GM
911static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
912 Lisp_Object, int));
913static struct text_pos string_pos P_ ((int, Lisp_Object));
914static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
915static int number_of_chars P_ ((unsigned char *, int));
916static void compute_stop_pos P_ ((struct it *));
917static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
918 Lisp_Object));
919static int face_before_or_after_it_pos P_ ((struct it *, int));
920static int next_overlay_change P_ ((int));
921static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
a61b7058
GM
922 Lisp_Object, struct text_pos *,
923 int));
06a12811 924static int underlying_face_id P_ ((struct it *));
4bde0ebb
GM
925static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
926 struct window *));
5f5c8ee5
GM
927
928#define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
929#define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
ff6c30e5 930
5f5c8ee5 931#ifdef HAVE_WINDOW_SYSTEM
12adba34 932
e037b9ec
GM
933static void update_tool_bar P_ ((struct frame *, int));
934static void build_desired_tool_bar_string P_ ((struct frame *f));
935static int redisplay_tool_bar P_ ((struct frame *));
936static void display_tool_bar_line P_ ((struct it *));
fa3c6b4d
KS
937static void notice_overwritten_cursor P_ ((struct window *,
938 enum glyph_row_area,
939 int, int, int, int));
940
941
12adba34 942
5f5c8ee5 943#endif /* HAVE_WINDOW_SYSTEM */
12adba34 944
5f5c8ee5
GM
945\f
946/***********************************************************************
947 Window display dimensions
948 ***********************************************************************/
12adba34 949
40a301b3
RS
950/* Return the bottom boundary y-position for text lines in window W.
951 This is the first y position at which a line cannot start.
952 It is relative to the top of the window.
953
954 This is the height of W minus the height of a mode line, if any. */
5f5c8ee5
GM
955
956INLINE int
957window_text_bottom_y (w)
958 struct window *w;
959{
da8b7f4f 960 int height = WINDOW_TOTAL_HEIGHT (w);
1a578e9b 961
5f5c8ee5
GM
962 if (WINDOW_WANTS_MODELINE_P (w))
963 height -= CURRENT_MODE_LINE_HEIGHT (w);
964 return height;
f88eb0b6
KH
965}
966
5f5c8ee5 967/* Return the pixel width of display area AREA of window W. AREA < 0
b46952ae 968 means return the total width of W, not including fringes to
5f5c8ee5 969 the left and right of the window. */
ff6c30e5 970
5f5c8ee5
GM
971INLINE int
972window_box_width (w, area)
973 struct window *w;
974 int area;
975{
da8b7f4f
KS
976 int cols = XFASTINT (w->total_cols);
977 int pixels = 0;
2311178e 978
5f5c8ee5 979 if (!w->pseudo_window_p)
ff6c30e5 980 {
da8b7f4f 981 cols -= WINDOW_SCROLL_BAR_COLS (w);
2311178e 982
5f5c8ee5
GM
983 if (area == TEXT_AREA)
984 {
da8b7f4f
KS
985 if (INTEGERP (w->left_margin_cols))
986 cols -= XFASTINT (w->left_margin_cols);
987 if (INTEGERP (w->right_margin_cols))
988 cols -= XFASTINT (w->right_margin_cols);
989 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
5f5c8ee5
GM
990 }
991 else if (area == LEFT_MARGIN_AREA)
da8b7f4f
KS
992 {
993 cols = (INTEGERP (w->left_margin_cols)
994 ? XFASTINT (w->left_margin_cols) : 0);
995 pixels = 0;
996 }
5f5c8ee5 997 else if (area == RIGHT_MARGIN_AREA)
da8b7f4f
KS
998 {
999 cols = (INTEGERP (w->right_margin_cols)
1000 ? XFASTINT (w->right_margin_cols) : 0);
1001 pixels = 0;
1002 }
ff6c30e5 1003 }
5f5c8ee5 1004
da8b7f4f 1005 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
ff6c30e5 1006}
1adc55de 1007
1adc55de 1008
5f5c8ee5 1009/* Return the pixel height of the display area of window W, not
4b41cebb 1010 including mode lines of W, if any. */
f88eb0b6 1011
5f5c8ee5
GM
1012INLINE int
1013window_box_height (w)
1014 struct window *w;
f88eb0b6 1015{
5f5c8ee5 1016 struct frame *f = XFRAME (w->frame);
da8b7f4f 1017 int height = WINDOW_TOTAL_HEIGHT (w);
7ecd4937
GM
1018
1019 xassert (height >= 0);
2311178e 1020
d9c9e99c
MB
1021 /* Note: the code below that determines the mode-line/header-line
1022 height is essentially the same as that contained in the macro
1023 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1024 the appropriate glyph row has its `mode_line_p' flag set,
1025 and if it doesn't, uses estimate_mode_line_height instead. */
1026
5f5c8ee5 1027 if (WINDOW_WANTS_MODELINE_P (w))
97dff879
MB
1028 {
1029 struct glyph_row *ml_row
1030 = (w->current_matrix && w->current_matrix->rows
1031 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1032 : 0);
1033 if (ml_row && ml_row->mode_line_p)
1034 height -= ml_row->height;
1035 else
96d2320f 1036 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
97dff879 1037 }
5f5c8ee5 1038
045dee35 1039 if (WINDOW_WANTS_HEADER_LINE_P (w))
97dff879
MB
1040 {
1041 struct glyph_row *hl_row
1042 = (w->current_matrix && w->current_matrix->rows
1043 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1044 : 0);
1045 if (hl_row && hl_row->mode_line_p)
1046 height -= hl_row->height;
1047 else
1048 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1049 }
5f5c8ee5 1050
7c75be36
PJ
1051 /* With a very small font and a mode-line that's taller than
1052 default, we might end up with a negative height. */
1053 return max (0, height);
5992c4f7
KH
1054}
1055
da8b7f4f
KS
1056/* Return the window-relative coordinate of the left edge of display
1057 area AREA of window W. AREA < 0 means return the left edge of the
1058 whole window, to the right of the left fringe of W. */
1059
1060INLINE int
1061window_box_left_offset (w, area)
1062 struct window *w;
1063 int area;
1064{
1065 int x;
1066
1067 if (w->pseudo_window_p)
1068 return 0;
1069
1070 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1071
1072 if (area == TEXT_AREA)
1073 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1074 + window_box_width (w, LEFT_MARGIN_AREA));
1075 else if (area == RIGHT_MARGIN_AREA)
1076 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1077 + window_box_width (w, LEFT_MARGIN_AREA)
1078 + window_box_width (w, TEXT_AREA)
1079 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1080 ? 0
1081 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1082 else if (area == LEFT_MARGIN_AREA
1083 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1084 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1085
1086 return x;
1087}
1088
1089
1090/* Return the window-relative coordinate of the right edge of display
1091 area AREA of window W. AREA < 0 means return the left edge of the
1092 whole window, to the left of the right fringe of W. */
1093
1094INLINE int
1095window_box_right_offset (w, area)
1096 struct window *w;
1097 int area;
1098{
1099 return window_box_left_offset (w, area) + window_box_width (w, area);
1100}
5992c4f7 1101
5f5c8ee5
GM
1102/* Return the frame-relative coordinate of the left edge of display
1103 area AREA of window W. AREA < 0 means return the left edge of the
b46952ae 1104 whole window, to the right of the left fringe of W. */
5992c4f7 1105
5f5c8ee5
GM
1106INLINE int
1107window_box_left (w, area)
1108 struct window *w;
1109 int area;
90adcf20 1110{
5f5c8ee5 1111 struct frame *f = XFRAME (w->frame);
da8b7f4f 1112 int x;
a3788d53 1113
da8b7f4f
KS
1114 if (w->pseudo_window_p)
1115 return FRAME_INTERNAL_BORDER_WIDTH (f);
2311178e 1116
da8b7f4f
KS
1117 x = (WINDOW_LEFT_EDGE_X (w)
1118 + window_box_left_offset (w, area));
73af359d 1119
5f5c8ee5 1120 return x;
2311178e 1121}
90adcf20 1122
b6436d4e 1123
5f5c8ee5
GM
1124/* Return the frame-relative coordinate of the right edge of display
1125 area AREA of window W. AREA < 0 means return the left edge of the
b46952ae 1126 whole window, to the left of the right fringe of W. */
ded34426 1127
5f5c8ee5
GM
1128INLINE int
1129window_box_right (w, area)
1130 struct window *w;
1131 int area;
1132{
1133 return window_box_left (w, area) + window_box_width (w, area);
2311178e
TTN
1134}
1135
5f5c8ee5
GM
1136/* Get the bounding box of the display area AREA of window W, without
1137 mode lines, in frame-relative coordinates. AREA < 0 means the
b46952ae 1138 whole window, not including the left and right fringes of
5f5c8ee5
GM
1139 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1140 coordinates of the upper-left corner of the box. Return in
1141 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1142
1143INLINE void
1144window_box (w, area, box_x, box_y, box_width, box_height)
1145 struct window *w;
1146 int area;
1147 int *box_x, *box_y, *box_width, *box_height;
1148{
da8b7f4f
KS
1149 if (box_width)
1150 *box_width = window_box_width (w, area);
1151 if (box_height)
1152 *box_height = window_box_height (w);
1153 if (box_x)
1154 *box_x = window_box_left (w, area);
1155 if (box_y)
1156 {
1157 *box_y = WINDOW_TOP_EDGE_Y (w);
1158 if (WINDOW_WANTS_HEADER_LINE_P (w))
1159 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1160 }
ded34426 1161}
1adc55de 1162
1adc55de 1163
5f5c8ee5 1164/* Get the bounding box of the display area AREA of window W, without
b46952ae
KS
1165 mode lines. AREA < 0 means the whole window, not including the
1166 left and right fringe of the window. Return in *TOP_LEFT_X
5f5c8ee5
GM
1167 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1168 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1169 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1170 box. */
ded34426 1171
5f5c8ee5
GM
1172INLINE void
1173window_box_edges (w, area, top_left_x, top_left_y,
1174 bottom_right_x, bottom_right_y)
1175 struct window *w;
1176 int area;
1177 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
48ae5f0a 1178{
5f5c8ee5
GM
1179 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1180 bottom_right_y);
1181 *bottom_right_x += *top_left_x;
1182 *bottom_right_y += *top_left_y;
48ae5f0a
KH
1183}
1184
5f5c8ee5
GM
1185
1186\f
1187/***********************************************************************
1188 Utilities
1189 ***********************************************************************/
1190
8b6ea97f
GM
1191/* Return the bottom y-position of the line the iterator IT is in.
1192 This can modify IT's settings. */
1193
1194int
1195line_bottom_y (it)
1196 struct it *it;
1197{
1198 int line_height = it->max_ascent + it->max_descent;
1199 int line_top_y = it->current_y;
2311178e 1200
8b6ea97f
GM
1201 if (line_height == 0)
1202 {
1203 if (last_height)
1204 line_height = last_height;
1205 else if (IT_CHARPOS (*it) < ZV)
1206 {
1207 move_it_by_lines (it, 1, 1);
1208 line_height = (it->max_ascent || it->max_descent
1209 ? it->max_ascent + it->max_descent
1210 : last_height);
1211 }
1212 else
1213 {
1214 struct glyph_row *row = it->glyph_row;
2311178e 1215
8b6ea97f
GM
1216 /* Use the default character height. */
1217 it->glyph_row = NULL;
1218 it->what = IT_CHARACTER;
1219 it->c = ' ';
1220 it->len = 1;
1221 PRODUCE_GLYPHS (it);
1222 line_height = it->ascent + it->descent;
1223 it->glyph_row = row;
1224 }
1225 }
1226
1227 return line_top_y + line_height;
1228}
1229
1230
0db95684
GM
1231/* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1232 1 if POS is visible and the line containing POS is fully visible.
1233 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1234 and header-lines heights. */
3a641a69
GM
1235
1236int
e893970b 1237pos_visible_p (w, charpos, fully, x, y, exact_mode_line_heights_p)
3a641a69 1238 struct window *w;
e893970b 1239 int charpos, *fully, *x, *y, exact_mode_line_heights_p;
3a641a69
GM
1240{
1241 struct it it;
1242 struct text_pos top;
fcab1954
GM
1243 int visible_p;
1244 struct buffer *old_buffer = NULL;
1245
1246 if (XBUFFER (w->buffer) != current_buffer)
1247 {
1248 old_buffer = current_buffer;
1249 set_buffer_internal_1 (XBUFFER (w->buffer));
1250 }
3a641a69
GM
1251
1252 *fully = visible_p = 0;
1253 SET_TEXT_POS_FROM_MARKER (top, w->start);
2311178e 1254
04612a64
GM
1255 /* Compute exact mode line heights, if requested. */
1256 if (exact_mode_line_heights_p)
1257 {
1258 if (WINDOW_WANTS_MODELINE_P (w))
1259 current_mode_line_height
96d2320f 1260 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
04612a64 1261 current_buffer->mode_line_format);
2311178e 1262
04612a64
GM
1263 if (WINDOW_WANTS_HEADER_LINE_P (w))
1264 current_header_line_height
1265 = display_mode_line (w, HEADER_LINE_FACE_ID,
1266 current_buffer->header_line_format);
1267 }
3a641a69 1268
04612a64 1269 start_display (&it, w, top);
3a641a69
GM
1270 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1271 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
a13be207
GM
1272
1273 /* Note that we may overshoot because of invisible text. */
1274 if (IT_CHARPOS (it) >= charpos)
3a641a69 1275 {
8b6ea97f
GM
1276 int top_y = it.current_y;
1277 int bottom_y = line_bottom_y (&it);
da8b7f4f 1278 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
2311178e 1279
8b6ea97f
GM
1280 if (top_y < window_top_y)
1281 visible_p = bottom_y > window_top_y;
1282 else if (top_y < it.last_visible_y)
fcab1954
GM
1283 {
1284 visible_p = 1;
8b6ea97f 1285 *fully = bottom_y <= it.last_visible_y;
fcab1954 1286 }
e893970b
KS
1287 if (visible_p && x)
1288 {
1289 *x = it.current_x;
1290 *y = max (top_y + it.max_ascent - it.ascent, window_top_y);
1291 }
3a641a69
GM
1292 }
1293 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1294 {
e893970b
KS
1295 struct it it2;
1296
1297 it2 = it;
3a641a69
GM
1298 move_it_by_lines (&it, 1, 0);
1299 if (charpos < IT_CHARPOS (it))
1300 {
1301 visible_p = 1;
e893970b
KS
1302 if (x)
1303 {
1304 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1305 *x = it2.current_x;
1306 *y = it2.current_y + it2.max_ascent - it2.ascent;
1307 }
3a641a69
GM
1308 }
1309 }
fcab1954
GM
1310
1311 if (old_buffer)
1312 set_buffer_internal_1 (old_buffer);
04612a64
GM
1313
1314 current_header_line_height = current_mode_line_height = -1;
e893970b 1315
3a641a69
GM
1316 return visible_p;
1317}
1318
1319
4fdb80f2
GM
1320/* Return the next character from STR which is MAXLEN bytes long.
1321 Return in *LEN the length of the character. This is like
1322 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
9ab8560d 1323 we find one, we return a `?', but with the length of the invalid
4fdb80f2
GM
1324 character. */
1325
1326static INLINE int
7a5b8a93 1327string_char_and_length (str, maxlen, len)
50f80c2f 1328 const unsigned char *str;
7a5b8a93 1329 int maxlen, *len;
4fdb80f2
GM
1330{
1331 int c;
1332
1333 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1334 if (!CHAR_VALID_P (c, 1))
1335 /* We may not change the length here because other places in Emacs
9ab8560d 1336 don't use this function, i.e. they silently accept invalid
4fdb80f2
GM
1337 characters. */
1338 c = '?';
1339
1340 return c;
1341}
1342
1343
1344
5f5c8ee5
GM
1345/* Given a position POS containing a valid character and byte position
1346 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1347
1348static struct text_pos
1349string_pos_nchars_ahead (pos, string, nchars)
1350 struct text_pos pos;
1351 Lisp_Object string;
1352 int nchars;
0b1005ef 1353{
5f5c8ee5
GM
1354 xassert (STRINGP (string) && nchars >= 0);
1355
1356 if (STRING_MULTIBYTE (string))
1357 {
2051c264 1358 int rest = SBYTES (string) - BYTEPOS (pos);
50f80c2f 1359 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
5f5c8ee5
GM
1360 int len;
1361
1362 while (nchars--)
1363 {
4fdb80f2 1364 string_char_and_length (p, rest, &len);
5f5c8ee5
GM
1365 p += len, rest -= len;
1366 xassert (rest >= 0);
1367 CHARPOS (pos) += 1;
1368 BYTEPOS (pos) += len;
1369 }
1370 }
1371 else
1372 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1373
1374 return pos;
0a9dc68b
RS
1375}
1376
0a9dc68b 1377
5f5c8ee5
GM
1378/* Value is the text position, i.e. character and byte position,
1379 for character position CHARPOS in STRING. */
1380
1381static INLINE struct text_pos
1382string_pos (charpos, string)
1383 int charpos;
0a9dc68b 1384 Lisp_Object string;
0a9dc68b 1385{
5f5c8ee5
GM
1386 struct text_pos pos;
1387 xassert (STRINGP (string));
1388 xassert (charpos >= 0);
1389 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1390 return pos;
1391}
1392
1393
1394/* Value is a text position, i.e. character and byte position, for
1395 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1396 means recognize multibyte characters. */
1397
1398static struct text_pos
1399c_string_pos (charpos, s, multibyte_p)
1400 int charpos;
1401 unsigned char *s;
1402 int multibyte_p;
1403{
1404 struct text_pos pos;
1405
1406 xassert (s != NULL);
1407 xassert (charpos >= 0);
1408
1409 if (multibyte_p)
0a9dc68b 1410 {
5f5c8ee5
GM
1411 int rest = strlen (s), len;
1412
1413 SET_TEXT_POS (pos, 0, 0);
1414 while (charpos--)
0a9dc68b 1415 {
4fdb80f2 1416 string_char_and_length (s, rest, &len);
5f5c8ee5
GM
1417 s += len, rest -= len;
1418 xassert (rest >= 0);
1419 CHARPOS (pos) += 1;
1420 BYTEPOS (pos) += len;
0a9dc68b
RS
1421 }
1422 }
5f5c8ee5
GM
1423 else
1424 SET_TEXT_POS (pos, charpos, charpos);
0a9dc68b 1425
5f5c8ee5
GM
1426 return pos;
1427}
0a9dc68b 1428
0a9dc68b 1429
5f5c8ee5
GM
1430/* Value is the number of characters in C string S. MULTIBYTE_P
1431 non-zero means recognize multibyte characters. */
0a9dc68b 1432
5f5c8ee5
GM
1433static int
1434number_of_chars (s, multibyte_p)
1435 unsigned char *s;
1436 int multibyte_p;
1437{
1438 int nchars;
2311178e 1439
5f5c8ee5
GM
1440 if (multibyte_p)
1441 {
1442 int rest = strlen (s), len;
1443 unsigned char *p = (unsigned char *) s;
0a9dc68b 1444
5f5c8ee5
GM
1445 for (nchars = 0; rest > 0; ++nchars)
1446 {
4fdb80f2 1447 string_char_and_length (p, rest, &len);
5f5c8ee5 1448 rest -= len, p += len;
0a9dc68b
RS
1449 }
1450 }
5f5c8ee5
GM
1451 else
1452 nchars = strlen (s);
1453
1454 return nchars;
0b1005ef
KH
1455}
1456
2311178e 1457
5f5c8ee5
GM
1458/* Compute byte position NEWPOS->bytepos corresponding to
1459 NEWPOS->charpos. POS is a known position in string STRING.
1460 NEWPOS->charpos must be >= POS.charpos. */
76412d64 1461
5f5c8ee5
GM
1462static void
1463compute_string_pos (newpos, pos, string)
1464 struct text_pos *newpos, pos;
1465 Lisp_Object string;
76412d64 1466{
5f5c8ee5
GM
1467 xassert (STRINGP (string));
1468 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
2311178e 1469
5f5c8ee5 1470 if (STRING_MULTIBYTE (string))
6fc556fd
KR
1471 *newpos = string_pos_nchars_ahead (pos, string,
1472 CHARPOS (*newpos) - CHARPOS (pos));
5f5c8ee5
GM
1473 else
1474 BYTEPOS (*newpos) = CHARPOS (*newpos);
76412d64
RS
1475}
1476
fa3c6b4d
KS
1477/* EXPORT:
1478 Return an estimation of the pixel height of mode or top lines on
1479 frame F. FACE_ID specifies what line's height to estimate. */
1480
1481int
1482estimate_mode_line_height (f, face_id)
1483 struct frame *f;
1484 enum face_id face_id;
1485{
1486#ifdef HAVE_WINDOW_SYSTEM
1487 if (FRAME_WINDOW_P (f))
1488 {
1489 int height = FONT_HEIGHT (FRAME_FONT (f));
1490
1491 /* This function is called so early when Emacs starts that the face
1492 cache and mode line face are not yet initialized. */
1493 if (FRAME_FACE_CACHE (f))
1494 {
1495 struct face *face = FACE_FROM_ID (f, face_id);
1496 if (face)
1497 {
1498 if (face->font)
1499 height = FONT_HEIGHT (face->font);
1500 if (face->box_line_width > 0)
1501 height += 2 * face->box_line_width;
1502 }
1503 }
1504
1505 return height;
1506 }
1507#endif
1508
1509 return 1;
1510}
1511
e080d3eb
KS
1512/* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1513 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1514 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1515 not force the value into range. */
1516
1517void
1518pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1519 FRAME_PTR f;
1520 register int pix_x, pix_y;
1521 int *x, *y;
1522 NativeRectangle *bounds;
1523 int noclip;
1524{
1525
1526#ifdef HAVE_WINDOW_SYSTEM
1527 if (FRAME_WINDOW_P (f))
1528 {
da8b7f4f 1529 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
e080d3eb
KS
1530 even for negative values. */
1531 if (pix_x < 0)
da8b7f4f 1532 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
e080d3eb 1533 if (pix_y < 0)
da8b7f4f 1534 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
e080d3eb 1535
da8b7f4f
KS
1536 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1537 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
e080d3eb
KS
1538
1539 if (bounds)
1540 STORE_NATIVE_RECT (*bounds,
da8b7f4f
KS
1541 FRAME_COL_TO_PIXEL_X (f, pix_x),
1542 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1543 FRAME_COLUMN_WIDTH (f) - 1,
1544 FRAME_LINE_HEIGHT (f) - 1);
e080d3eb
KS
1545
1546 if (!noclip)
1547 {
1548 if (pix_x < 0)
1549 pix_x = 0;
da8b7f4f
KS
1550 else if (pix_x > FRAME_TOTAL_COLS (f))
1551 pix_x = FRAME_TOTAL_COLS (f);
e080d3eb
KS
1552
1553 if (pix_y < 0)
1554 pix_y = 0;
da8b7f4f
KS
1555 else if (pix_y > FRAME_LINES (f))
1556 pix_y = FRAME_LINES (f);
e080d3eb
KS
1557 }
1558 }
1559#endif
1560
1561 *x = pix_x;
1562 *y = pix_y;
1563}
1564
1565
1566/* Given HPOS/VPOS in the current matrix of W, return corresponding
1567 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1568 can't tell the positions because W's display is not up to date,
1569 return 0. */
1570
1571int
1572glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1573 struct window *w;
1574 int hpos, vpos;
1575 int *frame_x, *frame_y;
1576{
1577#ifdef HAVE_WINDOW_SYSTEM
1578 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1579 {
1580 int success_p;
1581
1582 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1583 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1584
1585 if (display_completed)
1586 {
1587 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1588 struct glyph *glyph = row->glyphs[TEXT_AREA];
1589 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1590
1591 hpos = row->x;
1592 vpos = row->y;
1593 while (glyph < end)
1594 {
1595 hpos += glyph->pixel_width;
1596 ++glyph;
1597 }
1598
f65536fa
KS
1599 /* If first glyph is partially visible, its first visible position is still 0. */
1600 if (hpos < 0)
1601 hpos = 0;
1602
e080d3eb
KS
1603 success_p = 1;
1604 }
1605 else
1606 {
1607 hpos = vpos = 0;
1608 success_p = 0;
1609 }
1610
1611 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1612 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1613 return success_p;
1614 }
1615#endif
1616
1617 *frame_x = hpos;
1618 *frame_y = vpos;
1619 return 1;
1620}
1621
1622
fa3c6b4d
KS
1623#ifdef HAVE_WINDOW_SYSTEM
1624
1625/* Find the glyph under window-relative coordinates X/Y in window W.
1626 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1627 strings. Return in *HPOS and *VPOS the row and column number of
1628 the glyph found. Return in *AREA the glyph area containing X.
1629 Value is a pointer to the glyph found or null if X/Y is not on
1630 text, or we can't tell because W's current matrix is not up to
1631 date. */
1632
1633static struct glyph *
493fdc3c 1634x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
fa3c6b4d
KS
1635 struct window *w;
1636 int x, y;
493fdc3c 1637 int *hpos, *vpos, *dx, *dy, *area;
fa3c6b4d
KS
1638{
1639 struct glyph *glyph, *end;
1640 struct glyph_row *row = NULL;
da8b7f4f 1641 int x0, i;
fa3c6b4d
KS
1642
1643 /* Find row containing Y. Give up if some row is not enabled. */
1644 for (i = 0; i < w->current_matrix->nrows; ++i)
1645 {
1646 row = MATRIX_ROW (w->current_matrix, i);
1647 if (!row->enabled_p)
1648 return NULL;
1649 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1650 break;
1651 }
1652
1653 *vpos = i;
1654 *hpos = 0;
1655
1656 /* Give up if Y is not in the window. */
1657 if (i == w->current_matrix->nrows)
1658 return NULL;
1659
1660 /* Get the glyph area containing X. */
1661 if (w->pseudo_window_p)
1662 {
1663 *area = TEXT_AREA;
1664 x0 = 0;
1665 }
1666 else
1667 {
da8b7f4f 1668 if (x < window_box_left_offset (w, TEXT_AREA))
fa3c6b4d
KS
1669 {
1670 *area = LEFT_MARGIN_AREA;
da8b7f4f 1671 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
fa3c6b4d 1672 }
da8b7f4f 1673 else if (x < window_box_right_offset (w, TEXT_AREA))
fa3c6b4d
KS
1674 {
1675 *area = TEXT_AREA;
f65536fa 1676 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
fa3c6b4d
KS
1677 }
1678 else
1679 {
1680 *area = RIGHT_MARGIN_AREA;
da8b7f4f 1681 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
fa3c6b4d
KS
1682 }
1683 }
1684
1685 /* Find glyph containing X. */
1686 glyph = row->glyphs[*area];
1687 end = glyph + row->used[*area];
493fdc3c
KS
1688 x -= x0;
1689 while (glyph < end && x >= glyph->pixel_width)
fa3c6b4d 1690 {
493fdc3c 1691 x -= glyph->pixel_width;
fa3c6b4d
KS
1692 ++glyph;
1693 }
1694
1695 if (glyph == end)
1696 return NULL;
1697
493fdc3c
KS
1698 if (dx)
1699 {
1700 *dx = x;
1701 *dy = y - (row->y + row->ascent - glyph->ascent);
1702 }
1703
fa3c6b4d
KS
1704 *hpos = glyph - row->glyphs[*area];
1705 return glyph;
1706}
1707
1708
1709/* EXPORT:
1710 Convert frame-relative x/y to coordinates relative to window W.
1711 Takes pseudo-windows into account. */
1712
1713void
1714frame_to_window_pixel_xy (w, x, y)
1715 struct window *w;
1716 int *x, *y;
1717{
1718 if (w->pseudo_window_p)
1719 {
1720 /* A pseudo-window is always full-width, and starts at the
1721 left edge of the frame, plus a frame border. */
1722 struct frame *f = XFRAME (w->frame);
da8b7f4f 1723 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
fa3c6b4d
KS
1724 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1725 }
1726 else
1727 {
da8b7f4f 1728 *x -= WINDOW_LEFT_EDGE_X (w);
fa3c6b4d
KS
1729 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1730 }
1731}
1732
1733/* EXPORT:
1734 Return in *R the clipping rectangle for glyph string S. */
1735
1736void
1737get_glyph_string_clip_rect (s, nr)
1738 struct glyph_string *s;
1739 NativeRectangle *nr;
1740{
1741 XRectangle r;
1742
1743 if (s->row->full_width_p)
1744 {
da8b7f4f
KS
1745 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1746 r.x = WINDOW_LEFT_EDGE_X (s->w);
1747 r.width = WINDOW_TOTAL_WIDTH (s->w);
fa3c6b4d
KS
1748
1749 /* Unless displaying a mode or menu bar line, which are always
1750 fully visible, clip to the visible part of the row. */
1751 if (s->w->pseudo_window_p)
1752 r.height = s->row->visible_height;
1753 else
1754 r.height = s->height;
1755 }
1756 else
1757 {
1758 /* This is a text line that may be partially visible. */
da8b7f4f 1759 r.x = window_box_left (s->w, s->area);
fa3c6b4d
KS
1760 r.width = window_box_width (s->w, s->area);
1761 r.height = s->row->visible_height;
1762 }
1763
1764 /* If S draws overlapping rows, it's sufficient to use the top and
1765 bottom of the window for clipping because this glyph string
1766 intentionally draws over other lines. */
1767 if (s->for_overlaps_p)
1768 {
da8b7f4f 1769 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
fa3c6b4d
KS
1770 r.height = window_text_bottom_y (s->w) - r.y;
1771 }
1772 else
1773 {
1774 /* Don't use S->y for clipping because it doesn't take partially
1775 visible lines into account. For example, it can be negative for
1776 partially visible lines at the top of a window. */
1777 if (!s->row->full_width_p
1778 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
da8b7f4f 1779 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
fa3c6b4d
KS
1780 else
1781 r.y = max (0, s->row->y);
1782
1783 /* If drawing a tool-bar window, draw it over the internal border
1784 at the top of the window. */
1785 if (s->w == XWINDOW (s->f->tool_bar_window))
b4ebbb12 1786 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
fa3c6b4d
KS
1787 }
1788
1789 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1790
fa3c6b4d
KS
1791 /* If drawing the cursor, don't let glyph draw outside its
1792 advertised boundaries. Cleartype does this under some circumstances. */
1793 if (s->hl == DRAW_CURSOR)
1794 {
493fdc3c
KS
1795 struct glyph *glyph = s->first_glyph;
1796 int height;
1797
fa3c6b4d
KS
1798 if (s->x > r.x)
1799 {
1800 r.width -= s->x - r.x;
1801 r.x = s->x;
1802 }
493fdc3c
KS
1803 r.width = min (r.width, glyph->pixel_width);
1804
1805 /* Don't draw cursor glyph taller than our actual glyph. */
1806 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1807 if (height < r.height)
1808 {
cb9d4a9f
KS
1809 int max_y = r.y + r.height;
1810 r.y = min (max_y, s->ybase + glyph->descent - height);
1811 r.height = min (max_y - r.y, height);
493fdc3c 1812 }
fa3c6b4d 1813 }
fa3c6b4d
KS
1814
1815#ifdef CONVERT_FROM_XRECT
1816 CONVERT_FROM_XRECT (r, *nr);
1817#else
1818 *nr = r;
fd4c9408 1819#endif
fa3c6b4d
KS
1820}
1821
1822#endif /* HAVE_WINDOW_SYSTEM */
9c74a0dd 1823
5f5c8ee5
GM
1824\f
1825/***********************************************************************
1826 Lisp form evaluation
1827 ***********************************************************************/
1828
116d6f5c 1829/* Error handler for safe_eval and safe_call. */
5f5c8ee5
GM
1830
1831static Lisp_Object
116d6f5c 1832safe_eval_handler (arg)
5f5c8ee5
GM
1833 Lisp_Object arg;
1834{
0dbf9fd2 1835 add_to_log ("Error during redisplay: %s", arg, Qnil);
5f5c8ee5
GM
1836 return Qnil;
1837}
1838
1839
1840/* Evaluate SEXPR and return the result, or nil if something went
2913a9c0 1841 wrong. Prevent redisplay during the evaluation. */
5f5c8ee5 1842
71e5b1b8 1843Lisp_Object
116d6f5c 1844safe_eval (sexpr)
5f5c8ee5
GM
1845 Lisp_Object sexpr;
1846{
5f5c8ee5 1847 Lisp_Object val;
2311178e 1848
30a3f61c
GM
1849 if (inhibit_eval_during_redisplay)
1850 val = Qnil;
1851 else
1852 {
331379bf 1853 int count = SPECPDL_INDEX ();
30a3f61c 1854 struct gcpro gcpro1;
0d8b31c0 1855
30a3f61c
GM
1856 GCPRO1 (sexpr);
1857 specbind (Qinhibit_redisplay, Qt);
7033d6df
RS
1858 /* Use Qt to ensure debugger does not run,
1859 so there is no possibility of wanting to redisplay. */
1860 val = internal_condition_case_1 (Feval, sexpr, Qt,
30a3f61c
GM
1861 safe_eval_handler);
1862 UNGCPRO;
1863 val = unbind_to (count, val);
1864 }
2311178e 1865
30a3f61c 1866 return val;
0d8b31c0
GM
1867}
1868
1869
1870/* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2913a9c0
GM
1871 Return the result, or nil if something went wrong. Prevent
1872 redisplay during the evaluation. */
0d8b31c0
GM
1873
1874Lisp_Object
116d6f5c 1875safe_call (nargs, args)
0d8b31c0
GM
1876 int nargs;
1877 Lisp_Object *args;
1878{
0d8b31c0 1879 Lisp_Object val;
2311178e 1880
30a3f61c
GM
1881 if (inhibit_eval_during_redisplay)
1882 val = Qnil;
1883 else
1884 {
331379bf 1885 int count = SPECPDL_INDEX ();
30a3f61c 1886 struct gcpro gcpro1;
0d8b31c0 1887
30a3f61c
GM
1888 GCPRO1 (args[0]);
1889 gcpro1.nvars = nargs;
1890 specbind (Qinhibit_redisplay, Qt);
7033d6df
RS
1891 /* Use Qt to ensure debugger does not run,
1892 so there is no possibility of wanting to redisplay. */
1893 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
30a3f61c
GM
1894 safe_eval_handler);
1895 UNGCPRO;
1896 val = unbind_to (count, val);
1897 }
1898
1899 return val;
5f5c8ee5
GM
1900}
1901
1902
116d6f5c
GM
1903/* Call function FN with one argument ARG.
1904 Return the result, or nil if something went wrong. */
1905
1906Lisp_Object
1907safe_call1 (fn, arg)
1908 Lisp_Object fn, arg;
1909{
1910 Lisp_Object args[2];
1911 args[0] = fn;
1912 args[1] = arg;
1913 return safe_call (2, args);
1914}
1915
1916
5f5c8ee5
GM
1917\f
1918/***********************************************************************
1919 Debugging
1920 ***********************************************************************/
1921
1922#if 0
1923
1924/* Define CHECK_IT to perform sanity checks on iterators.
1925 This is for debugging. It is too slow to do unconditionally. */
1926
1927static void
1928check_it (it)
1929 struct it *it;
1930{
1931 if (it->method == next_element_from_string)
a2889657 1932 {
5f5c8ee5
GM
1933 xassert (STRINGP (it->string));
1934 xassert (IT_STRING_CHARPOS (*it) >= 0);
1935 }
7d8a0b55 1936 else
5f5c8ee5 1937 {
7d8a0b55
MB
1938 xassert (IT_STRING_CHARPOS (*it) < 0);
1939 if (it->method == next_element_from_buffer)
1940 {
1941 /* Check that character and byte positions agree. */
1942 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1943 }
5f5c8ee5 1944 }
73af359d 1945
5f5c8ee5
GM
1946 if (it->dpvec)
1947 xassert (it->current.dpvec_index >= 0);
1948 else
1949 xassert (it->current.dpvec_index < 0);
1950}
1f40cad2 1951
5f5c8ee5
GM
1952#define CHECK_IT(IT) check_it ((IT))
1953
1954#else /* not 0 */
1955
1956#define CHECK_IT(IT) (void) 0
1957
1958#endif /* not 0 */
1959
1960
1961#if GLYPH_DEBUG
1962
1963/* Check that the window end of window W is what we expect it
1964 to be---the last row in the current matrix displaying text. */
1965
1966static void
1967check_window_end (w)
1968 struct window *w;
1969{
1970 if (!MINI_WINDOW_P (w)
1971 && !NILP (w->window_end_valid))
1972 {
1973 struct glyph_row *row;
1974 xassert ((row = MATRIX_ROW (w->current_matrix,
1975 XFASTINT (w->window_end_vpos)),
1976 !row->enabled_p
1977 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1978 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1979 }
1980}
1981
1982#define CHECK_WINDOW_END(W) check_window_end ((W))
1983
1984#else /* not GLYPH_DEBUG */
1985
1986#define CHECK_WINDOW_END(W) (void) 0
1987
1988#endif /* not GLYPH_DEBUG */
1989
1990
1991\f
1992/***********************************************************************
1993 Iterator initialization
1994 ***********************************************************************/
1995
1996/* Initialize IT for displaying current_buffer in window W, starting
1997 at character position CHARPOS. CHARPOS < 0 means that no buffer
1998 position is specified which is useful when the iterator is assigned
1999 a position later. BYTEPOS is the byte position corresponding to
3ebf0ea9 2000 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
5f5c8ee5
GM
2001
2002 If ROW is not null, calls to produce_glyphs with IT as parameter
2003 will produce glyphs in that row.
2004
2005 BASE_FACE_ID is the id of a base face to use. It must be one of
96d2320f
KS
2006 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2007 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2008 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2311178e 2009
96d2320f
KS
2010 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2011 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2012 will be initialized to use the corresponding mode line glyph row of
2013 the desired matrix of W. */
5f5c8ee5
GM
2014
2015void
2016init_iterator (it, w, charpos, bytepos, row, base_face_id)
2017 struct it *it;
2018 struct window *w;
2019 int charpos, bytepos;
2020 struct glyph_row *row;
2021 enum face_id base_face_id;
2022{
2023 int highlight_region_p;
5f5c8ee5
GM
2024
2025 /* Some precondition checks. */
2026 xassert (w != NULL && it != NULL);
3b6b6db7
SM
2027 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2028 && charpos <= ZV));
5f5c8ee5
GM
2029
2030 /* If face attributes have been changed since the last redisplay,
2031 free realized faces now because they depend on face definitions
7d0393cf 2032 that might have changed. Don't free faces while there might be
1987b083 2033 desired matrices pending which reference these faces. */
26683087 2034 if (face_change_count && !inhibit_free_realized_faces)
5f5c8ee5
GM
2035 {
2036 face_change_count = 0;
2037 free_all_realized_faces (Qnil);
2038 }
2039
2040 /* Use one of the mode line rows of W's desired matrix if
2041 appropriate. */
2042 if (row == NULL)
2043 {
96d2320f
KS
2044 if (base_face_id == MODE_LINE_FACE_ID
2045 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
5f5c8ee5 2046 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
045dee35
GM
2047 else if (base_face_id == HEADER_LINE_FACE_ID)
2048 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
5f5c8ee5 2049 }
2311178e 2050
5f5c8ee5
GM
2051 /* Clear IT. */
2052 bzero (it, sizeof *it);
2053 it->current.overlay_string_index = -1;
2054 it->current.dpvec_index = -1;
5f5c8ee5 2055 it->base_face_id = base_face_id;
7d8a0b55
MB
2056 it->string = Qnil;
2057 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5f5c8ee5
GM
2058
2059 /* The window in which we iterate over current_buffer: */
2060 XSETWINDOW (it->window, w);
2061 it->w = w;
2062 it->f = XFRAME (w->frame);
2063
d475bcb8
GM
2064 /* Extra space between lines (on window systems only). */
2065 if (base_face_id == DEFAULT_FACE_ID
2066 && FRAME_WINDOW_P (it->f))
2067 {
2068 if (NATNUMP (current_buffer->extra_line_spacing))
2069 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
a9e5c932
KS
2070 else if (FLOATP (current_buffer->extra_line_spacing))
2071 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2072 * FRAME_LINE_HEIGHT (it->f));
d475bcb8
GM
2073 else if (it->f->extra_line_spacing > 0)
2074 it->extra_line_spacing = it->f->extra_line_spacing;
2075 }
2076
5f5c8ee5 2077 /* If realized faces have been removed, e.g. because of face
62be9979
GM
2078 attribute changes of named faces, recompute them. When running
2079 in batch mode, the face cache of Vterminal_frame is null. If
2080 we happen to get called, make a dummy face cache. */
a24d4cb2 2081 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
62be9979 2082 init_frame_faces (it->f);
5f5c8ee5
GM
2083 if (FRAME_FACE_CACHE (it->f)->used == 0)
2084 recompute_basic_faces (it->f);
2085
e893970b
KS
2086 /* Current value of the `slice', `space-width', and 'height' properties. */
2087 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
5f5c8ee5
GM
2088 it->space_width = Qnil;
2089 it->font_height = Qnil;
a9e5c932 2090 it->override_ascent = -1;
2311178e 2091
5f5c8ee5
GM
2092 /* Are control characters displayed as `^C'? */
2093 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2094
2095 /* -1 means everything between a CR and the following line end
2096 is invisible. >0 means lines indented more than this value are
2097 invisible. */
2098 it->selective = (INTEGERP (current_buffer->selective_display)
2099 ? XFASTINT (current_buffer->selective_display)
2311178e 2100 : (!NILP (current_buffer->selective_display)
5f5c8ee5
GM
2101 ? -1 : 0));
2102 it->selective_display_ellipsis_p
2103 = !NILP (current_buffer->selective_display_ellipses);
2104
2105 /* Display table to use. */
2106 it->dp = window_display_table (w);
2107
2108 /* Are multibyte characters enabled in current_buffer? */
2109 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2110
2111 /* Non-zero if we should highlight the region. */
2112 highlight_region_p
3ebf0ea9 2113 = (!NILP (Vtransient_mark_mode)
5f5c8ee5
GM
2114 && !NILP (current_buffer->mark_active)
2115 && XMARKER (current_buffer->mark)->buffer != 0);
2116
2117 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2118 start and end of a visible region in window IT->w. Set both to
2119 -1 to indicate no region. */
2120 if (highlight_region_p
2121 /* Maybe highlight only in selected window. */
2311178e 2122 && (/* Either show region everywhere. */
5f5c8ee5
GM
2123 highlight_nonselected_windows
2124 /* Or show region in the selected window. */
2125 || w == XWINDOW (selected_window)
2126 /* Or show the region if we are in the mini-buffer and W is
2127 the window the mini-buffer refers to. */
2128 || (MINI_WINDOW_P (XWINDOW (selected_window))
5705966b
KS
2129 && WINDOWP (minibuf_selected_window)
2130 && w == XWINDOW (minibuf_selected_window))))
5f5c8ee5
GM
2131 {
2132 int charpos = marker_position (current_buffer->mark);
2133 it->region_beg_charpos = min (PT, charpos);
2134 it->region_end_charpos = max (PT, charpos);
2135 }
2136 else
2137 it->region_beg_charpos = it->region_end_charpos = -1;
2138
2139 /* Get the position at which the redisplay_end_trigger hook should
2140 be run, if it is to be run at all. */
2141 if (MARKERP (w->redisplay_end_trigger)
2142 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2143 it->redisplay_end_trigger_charpos
2144 = marker_position (w->redisplay_end_trigger);
2145 else if (INTEGERP (w->redisplay_end_trigger))
2146 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2147
2148 /* Correct bogus values of tab_width. */
2149 it->tab_width = XINT (current_buffer->tab_width);
2150 if (it->tab_width <= 0 || it->tab_width > 1000)
2151 it->tab_width = 8;
2152
2153 /* Are lines in the display truncated? */
2154 it->truncate_lines_p
2155 = (base_face_id != DEFAULT_FACE_ID
2156 || XINT (it->w->hscroll)
2157 || (truncate_partial_width_windows
2158 && !WINDOW_FULL_WIDTH_P (it->w))
2159 || !NILP (current_buffer->truncate_lines));
2160
2161 /* Get dimensions of truncation and continuation glyphs. These are
b46952ae 2162 displayed as fringe bitmaps under X, so we don't need them for such
5f5c8ee5
GM
2163 frames. */
2164 if (!FRAME_WINDOW_P (it->f))
2165 {
2166 if (it->truncate_lines_p)
2167 {
2168 /* We will need the truncation glyph. */
2169 xassert (it->glyph_row == NULL);
2170 produce_special_glyphs (it, IT_TRUNCATION);
2171 it->truncation_pixel_width = it->pixel_width;
2172 }
2173 else
2174 {
2175 /* We will need the continuation glyph. */
2176 xassert (it->glyph_row == NULL);
2177 produce_special_glyphs (it, IT_CONTINUATION);
2178 it->continuation_pixel_width = it->pixel_width;
2179 }
2180
153c2160 2181 /* Reset these values to zero because the produce_special_glyphs
5f5c8ee5
GM
2182 above has changed them. */
2183 it->pixel_width = it->ascent = it->descent = 0;
312246d1 2184 it->phys_ascent = it->phys_descent = 0;
5f5c8ee5
GM
2185 }
2186
2187 /* Set this after getting the dimensions of truncation and
2188 continuation glyphs, so that we don't produce glyphs when calling
2189 produce_special_glyphs, above. */
2190 it->glyph_row = row;
2191 it->area = TEXT_AREA;
2192
2193 /* Get the dimensions of the display area. The display area
2194 consists of the visible window area plus a horizontally scrolled
2195 part to the left of the window. All x-values are relative to the
2196 start of this total display area. */
2197 if (base_face_id != DEFAULT_FACE_ID)
2198 {
2199 /* Mode lines, menu bar in terminal frames. */
2200 it->first_visible_x = 0;
da8b7f4f 2201 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
5f5c8ee5
GM
2202 }
2203 else
2204 {
2205 it->first_visible_x
da8b7f4f 2206 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
5f5c8ee5
GM
2207 it->last_visible_x = (it->first_visible_x
2208 + window_box_width (w, TEXT_AREA));
2209
2210 /* If we truncate lines, leave room for the truncator glyph(s) at
2211 the right margin. Otherwise, leave room for the continuation
2212 glyph(s). Truncation and continuation glyphs are not inserted
2213 for window-based redisplay. */
2214 if (!FRAME_WINDOW_P (it->f))
2215 {
2216 if (it->truncate_lines_p)
2217 it->last_visible_x -= it->truncation_pixel_width;
2218 else
2219 it->last_visible_x -= it->continuation_pixel_width;
2220 }
2221
045dee35 2222 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
da8b7f4f 2223 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
5f5c8ee5
GM
2224 }
2225
2226 /* Leave room for a border glyph. */
2227 if (!FRAME_WINDOW_P (it->f)
2228 && !WINDOW_RIGHTMOST_P (it->w))
2229 it->last_visible_x -= 1;
2230
2231 it->last_visible_y = window_text_bottom_y (w);
2232
2233 /* For mode lines and alike, arrange for the first glyph having a
2234 left box line if the face specifies a box. */
2235 if (base_face_id != DEFAULT_FACE_ID)
2236 {
2237 struct face *face;
2311178e 2238
5f5c8ee5
GM
2239 it->face_id = base_face_id;
2240
2241 /* If we have a boxed mode line, make the first character appear
2242 with a left box line. */
2243 face = FACE_FROM_ID (it->f, base_face_id);
2244 if (face->box != FACE_NO_BOX)
2245 it->start_of_box_run_p = 1;
2246 }
2247
2248 /* If a buffer position was specified, set the iterator there,
2249 getting overlays and face properties from that position. */
3ebf0ea9 2250 if (charpos >= BUF_BEG (current_buffer))
5f5c8ee5
GM
2251 {
2252 it->end_charpos = ZV;
2253 it->face_id = -1;
2254 IT_CHARPOS (*it) = charpos;
2311178e 2255
5f5c8ee5 2256 /* Compute byte position if not specified. */
3ebf0ea9 2257 if (bytepos < charpos)
5f5c8ee5
GM
2258 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2259 else
2260 IT_BYTEPOS (*it) = bytepos;
2261
29b3ea63
KS
2262 it->start = it->current;
2263
5f5c8ee5
GM
2264 /* Compute faces etc. */
2265 reseat (it, it->current.pos, 1);
2266 }
2311178e 2267
5f5c8ee5
GM
2268 CHECK_IT (it);
2269}
2270
2271
2272/* Initialize IT for the display of window W with window start POS. */
2273
2274void
2275start_display (it, w, pos)
2276 struct it *it;
2277 struct window *w;
2278 struct text_pos pos;
2279{
5f5c8ee5 2280 struct glyph_row *row;
045dee35 2281 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
5f5c8ee5
GM
2282
2283 row = w->desired_matrix->rows + first_vpos;
2284 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3ccb8bcf 2285 it->first_vpos = first_vpos;
17fdcfc8
GM
2286
2287 if (!it->truncate_lines_p)
5f5c8ee5 2288 {
17fdcfc8
GM
2289 int start_at_line_beg_p;
2290 int first_y = it->current_y;
2311178e 2291
17fdcfc8
GM
2292 /* If window start is not at a line start, skip forward to POS to
2293 get the correct continuation lines width. */
2294 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2295 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2296 if (!start_at_line_beg_p)
5f5c8ee5 2297 {
0e47bbf7
RS
2298 int new_x;
2299
17fdcfc8
GM
2300 reseat_at_previous_visible_line_start (it);
2301 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2302
0e47bbf7
RS
2303 new_x = it->current_x + it->pixel_width;
2304
17fdcfc8
GM
2305 /* If lines are continued, this line may end in the middle
2306 of a multi-glyph character (e.g. a control character
2307 displayed as \003, or in the middle of an overlay
2308 string). In this case move_it_to above will not have
2309 taken us to the start of the continuation line but to the
2310 end of the continued line. */
0e47bbf7
RS
2311 if (it->current_x > 0
2312 && !it->truncate_lines_p /* Lines are continued. */
2313 && (/* And glyph doesn't fit on the line. */
2314 new_x > it->last_visible_x
2315 /* Or it fits exactly and we're on a window
2316 system frame. */
2317 || (new_x == it->last_visible_x
2318 && FRAME_WINDOW_P (it->f))))
5f5c8ee5 2319 {
b28cb6ed
GM
2320 if (it->current.dpvec_index >= 0
2321 || it->current.overlay_string_index >= 0)
2322 {
cafafe0b 2323 set_iterator_to_next (it, 1);
b28cb6ed
GM
2324 move_it_in_display_line_to (it, -1, -1, 0);
2325 }
2311178e 2326
b28cb6ed 2327 it->continuation_lines_width += it->current_x;
5f5c8ee5 2328 }
b28cb6ed
GM
2329
2330 /* We're starting a new display line, not affected by the
2331 height of the continued line, so clear the appropriate
2332 fields in the iterator structure. */
2333 it->max_ascent = it->max_descent = 0;
2334 it->max_phys_ascent = it->max_phys_descent = 0;
2311178e 2335
17fdcfc8
GM
2336 it->current_y = first_y;
2337 it->vpos = 0;
2338 it->current_x = it->hpos = 0;
2339 }
5f5c8ee5
GM
2340 }
2341
2342#if 0 /* Don't assert the following because start_display is sometimes
2343 called intentionally with a window start that is not at a
2344 line start. Please leave this code in as a comment. */
2311178e 2345
5f5c8ee5
GM
2346 /* Window start should be on a line start, now. */
2347 xassert (it->continuation_lines_width
2348 || IT_CHARPOS (it) == BEGV
2349 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2350#endif /* 0 */
2351}
2352
2353
4bde0ebb
GM
2354/* Return 1 if POS is a position in ellipses displayed for invisible
2355 text. W is the window we display, for text property lookup. */
5f5c8ee5 2356
4bde0ebb
GM
2357static int
2358in_ellipses_for_invisible_text_p (pos, w)
5f5c8ee5 2359 struct display_pos *pos;
4bde0ebb 2360 struct window *w;
5f5c8ee5 2361{
ac90c44f 2362 Lisp_Object prop, window;
4bde0ebb
GM
2363 int ellipses_p = 0;
2364 int charpos = CHARPOS (pos->pos);
2311178e 2365
ac90c44f
GM
2366 /* If POS specifies a position in a display vector, this might
2367 be for an ellipsis displayed for invisible text. We won't
2368 get the iterator set up for delivering that ellipsis unless
2369 we make sure that it gets aware of the invisible text. */
2370 if (pos->dpvec_index >= 0
2371 && pos->overlay_string_index < 0
2372 && CHARPOS (pos->string_pos) < 0
2373 && charpos > BEGV
2374 && (XSETWINDOW (window, w),
2375 prop = Fget_char_property (make_number (charpos),
2376 Qinvisible, window),
20fbd925 2377 !TEXT_PROP_MEANS_INVISIBLE (prop)))
ac90c44f
GM
2378 {
2379 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2380 window);
8580a4e3 2381 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
4bde0ebb
GM
2382 }
2383
2384 return ellipses_p;
2385}
2386
2387
2388/* Initialize IT for stepping through current_buffer in window W,
2389 starting at position POS that includes overlay string and display
47d57b22
GM
2390 vector/ control character translation position information. Value
2391 is zero if there are overlay strings with newlines at POS. */
4bde0ebb 2392
47d57b22 2393static int
4bde0ebb
GM
2394init_from_display_pos (it, w, pos)
2395 struct it *it;
2396 struct window *w;
2397 struct display_pos *pos;
2398{
2399 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
47d57b22 2400 int i, overlay_strings_with_newlines = 0;
2311178e 2401
4bde0ebb
GM
2402 /* If POS specifies a position in a display vector, this might
2403 be for an ellipsis displayed for invisible text. We won't
2404 get the iterator set up for delivering that ellipsis unless
2405 we make sure that it gets aware of the invisible text. */
2406 if (in_ellipses_for_invisible_text_p (pos, w))
2407 {
2408 --charpos;
2409 bytepos = 0;
ac90c44f 2410 }
2311178e 2411
5f5c8ee5
GM
2412 /* Keep in mind: the call to reseat in init_iterator skips invisible
2413 text, so we might end up at a position different from POS. This
2414 is only a problem when POS is a row start after a newline and an
2415 overlay starts there with an after-string, and the overlay has an
2416 invisible property. Since we don't skip invisible text in
2417 display_line and elsewhere immediately after consuming the
2418 newline before the row start, such a POS will not be in a string,
2419 but the call to init_iterator below will move us to the
2420 after-string. */
ac90c44f 2421 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
5f5c8ee5 2422
47d57b22 2423 for (i = 0; i < it->n_overlay_strings; ++i)
1e1e5daf 2424 {
50f80c2f
KR
2425 const char *s = SDATA (it->overlay_strings[i]);
2426 const char *e = s + SBYTES (it->overlay_strings[i]);
2311178e 2427
1e1e5daf
GM
2428 while (s < e && *s != '\n')
2429 ++s;
2430
2431 if (s < e)
2432 {
2433 overlay_strings_with_newlines = 1;
2434 break;
2435 }
2436 }
47d57b22 2437
75c5350a
GM
2438 /* If position is within an overlay string, set up IT to the right
2439 overlay string. */
5f5c8ee5
GM
2440 if (pos->overlay_string_index >= 0)
2441 {
2442 int relative_index;
75c5350a
GM
2443
2444 /* If the first overlay string happens to have a `display'
2445 property for an image, the iterator will be set up for that
2446 image, and we have to undo that setup first before we can
2447 correct the overlay string index. */
2448 if (it->method == next_element_from_image)
2449 pop_it (it);
2311178e 2450
5f5c8ee5
GM
2451 /* We already have the first chunk of overlay strings in
2452 IT->overlay_strings. Load more until the one for
2453 pos->overlay_string_index is in IT->overlay_strings. */
2454 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2455 {
2456 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2457 it->current.overlay_string_index = 0;
2458 while (n--)
2459 {
5a08cbaf 2460 load_overlay_strings (it, 0);
5f5c8ee5
GM
2461 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2462 }
2463 }
2311178e 2464
5f5c8ee5
GM
2465 it->current.overlay_string_index = pos->overlay_string_index;
2466 relative_index = (it->current.overlay_string_index
2467 % OVERLAY_STRING_CHUNK_SIZE);
2468 it->string = it->overlay_strings[relative_index];
cafafe0b 2469 xassert (STRINGP (it->string));
5f5c8ee5
GM
2470 it->current.string_pos = pos->string_pos;
2471 it->method = next_element_from_string;
2472 }
2311178e 2473
1e1e5daf
GM
2474#if 0 /* This is bogus because POS not having an overlay string
2475 position does not mean it's after the string. Example: A
2476 line starting with a before-string and initialization of IT
2477 to the previous row's end position. */
2be8f184
GM
2478 else if (it->current.overlay_string_index >= 0)
2479 {
2480 /* If POS says we're already after an overlay string ending at
2481 POS, make sure to pop the iterator because it will be in
2482 front of that overlay string. When POS is ZV, we've thereby
2483 also ``processed'' overlay strings at ZV. */
aeb2b8fc
GM
2484 while (it->sp)
2485 pop_it (it);
2be8f184
GM
2486 it->current.overlay_string_index = -1;
2487 it->method = next_element_from_buffer;
2488 if (CHARPOS (pos->pos) == ZV)
2489 it->overlay_strings_at_end_processed_p = 1;
2490 }
1e1e5daf 2491#endif /* 0 */
2311178e 2492
2be8f184 2493 if (CHARPOS (pos->string_pos) >= 0)
5f5c8ee5
GM
2494 {
2495 /* Recorded position is not in an overlay string, but in another
2496 string. This can only be a string from a `display' property.
2497 IT should already be filled with that string. */
2498 it->current.string_pos = pos->string_pos;
2499 xassert (STRINGP (it->string));
2500 }
2501
ac90c44f
GM
2502 /* Restore position in display vector translations, control
2503 character translations or ellipses. */
5f5c8ee5
GM
2504 if (pos->dpvec_index >= 0)
2505 {
ac90c44f
GM
2506 if (it->dpvec == NULL)
2507 get_next_display_element (it);
5f5c8ee5
GM
2508 xassert (it->dpvec && it->current.dpvec_index == 0);
2509 it->current.dpvec_index = pos->dpvec_index;
2510 }
2311178e 2511
5f5c8ee5 2512 CHECK_IT (it);
47d57b22 2513 return !overlay_strings_with_newlines;
5f5c8ee5
GM
2514}
2515
2516
2517/* Initialize IT for stepping through current_buffer in window W
2518 starting at ROW->start. */
2519
2520static void
2521init_to_row_start (it, w, row)
2522 struct it *it;
2523 struct window *w;
2524 struct glyph_row *row;
2525{
2526 init_from_display_pos (it, w, &row->start);
29b3ea63 2527 it->start = row->start;
5f5c8ee5
GM
2528 it->continuation_lines_width = row->continuation_lines_width;
2529 CHECK_IT (it);
2530}
2531
2311178e 2532
5f5c8ee5 2533/* Initialize IT for stepping through current_buffer in window W
47d57b22
GM
2534 starting in the line following ROW, i.e. starting at ROW->end.
2535 Value is zero if there are overlay strings with newlines at ROW's
2536 end position. */
5f5c8ee5 2537
47d57b22 2538static int
5f5c8ee5
GM
2539init_to_row_end (it, w, row)
2540 struct it *it;
2541 struct window *w;
2542 struct glyph_row *row;
2543{
47d57b22 2544 int success = 0;
2311178e 2545
47d57b22
GM
2546 if (init_from_display_pos (it, w, &row->end))
2547 {
2548 if (row->continued_p)
2549 it->continuation_lines_width
2550 = row->continuation_lines_width + row->pixel_width;
2551 CHECK_IT (it);
2552 success = 1;
2553 }
2311178e 2554
47d57b22 2555 return success;
5f5c8ee5
GM
2556}
2557
2558
2559
2560\f
2561/***********************************************************************
2562 Text properties
2563 ***********************************************************************/
2564
2565/* Called when IT reaches IT->stop_charpos. Handle text property and
2566 overlay changes. Set IT->stop_charpos to the next position where
2567 to stop. */
2568
2569static void
2570handle_stop (it)
2571 struct it *it;
2572{
2573 enum prop_handled handled;
2574 int handle_overlay_change_p = 1;
2575 struct props *p;
2576
2577 it->dpvec = NULL;
2578 it->current.dpvec_index = -1;
2579
2580 do
2581 {
2582 handled = HANDLED_NORMALLY;
2311178e 2583
5f5c8ee5
GM
2584 /* Call text property handlers. */
2585 for (p = it_props; p->handler; ++p)
2586 {
2587 handled = p->handler (it);
2970b9be 2588
5f5c8ee5
GM
2589 if (handled == HANDLED_RECOMPUTE_PROPS)
2590 break;
2591 else if (handled == HANDLED_RETURN)
2592 return;
2593 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2594 handle_overlay_change_p = 0;
2595 }
2596
2597 if (handled != HANDLED_RECOMPUTE_PROPS)
2598 {
2599 /* Don't check for overlay strings below when set to deliver
2600 characters from a display vector. */
2601 if (it->method == next_element_from_display_vector)
2602 handle_overlay_change_p = 0;
2603
2604 /* Handle overlay changes. */
2605 if (handle_overlay_change_p)
2606 handled = handle_overlay_change (it);
2311178e 2607
5f5c8ee5
GM
2608 /* Determine where to stop next. */
2609 if (handled == HANDLED_NORMALLY)
2610 compute_stop_pos (it);
2611 }
2612 }
2613 while (handled == HANDLED_RECOMPUTE_PROPS);
2614}
2615
2616
2617/* Compute IT->stop_charpos from text property and overlay change
2618 information for IT's current position. */
2619
2620static void
2621compute_stop_pos (it)
2622 struct it *it;
2623{
2624 register INTERVAL iv, next_iv;
2625 Lisp_Object object, limit, position;
2626
2627 /* If nowhere else, stop at the end. */
2628 it->stop_charpos = it->end_charpos;
2311178e 2629
5f5c8ee5
GM
2630 if (STRINGP (it->string))
2631 {
2632 /* Strings are usually short, so don't limit the search for
2633 properties. */
2634 object = it->string;
2635 limit = Qnil;
ac90c44f 2636 position = make_number (IT_STRING_CHARPOS (*it));
5f5c8ee5
GM
2637 }
2638 else
2639 {
2640 int charpos;
2641
2642 /* If next overlay change is in front of the current stop pos
2643 (which is IT->end_charpos), stop there. Note: value of
2644 next_overlay_change is point-max if no overlay change
2645 follows. */
2646 charpos = next_overlay_change (IT_CHARPOS (*it));
2647 if (charpos < it->stop_charpos)
2648 it->stop_charpos = charpos;
2649
2650 /* If showing the region, we have to stop at the region
2651 start or end because the face might change there. */
2652 if (it->region_beg_charpos > 0)
2653 {
2654 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2655 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2656 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2657 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2658 }
2311178e 2659
5f5c8ee5
GM
2660 /* Set up variables for computing the stop position from text
2661 property changes. */
2662 XSETBUFFER (object, current_buffer);
ac90c44f
GM
2663 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2664 position = make_number (IT_CHARPOS (*it));
5f5c8ee5
GM
2665
2666 }
2667
2668 /* Get the interval containing IT's position. Value is a null
2669 interval if there isn't such an interval. */
2670 iv = validate_interval_range (object, &position, &position, 0);
2671 if (!NULL_INTERVAL_P (iv))
2672 {
2673 Lisp_Object values_here[LAST_PROP_IDX];
2674 struct props *p;
2675
2676 /* Get properties here. */
2677 for (p = it_props; p->handler; ++p)
2678 values_here[p->idx] = textget (iv->plist, *p->name);
2679
2680 /* Look for an interval following iv that has different
2681 properties. */
2682 for (next_iv = next_interval (iv);
2683 (!NULL_INTERVAL_P (next_iv)
2684 && (NILP (limit)
2685 || XFASTINT (limit) > next_iv->position));
2686 next_iv = next_interval (next_iv))
2687 {
2688 for (p = it_props; p->handler; ++p)
2689 {
2690 Lisp_Object new_value;
2691
2692 new_value = textget (next_iv->plist, *p->name);
2693 if (!EQ (values_here[p->idx], new_value))
2694 break;
2695 }
2311178e 2696
5f5c8ee5
GM
2697 if (p->handler)
2698 break;
2699 }
2700
2701 if (!NULL_INTERVAL_P (next_iv))
2702 {
2703 if (INTEGERP (limit)
2704 && next_iv->position >= XFASTINT (limit))
2705 /* No text property change up to limit. */
2706 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2707 else
2708 /* Text properties change in next_iv. */
2709 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2710 }
2711 }
2712
2713 xassert (STRINGP (it->string)
2714 || (it->stop_charpos >= BEGV
2715 && it->stop_charpos >= IT_CHARPOS (*it)));
2716}
2717
2718
2719/* Return the position of the next overlay change after POS in
2720 current_buffer. Value is point-max if no overlay change
2721 follows. This is like `next-overlay-change' but doesn't use
2722 xmalloc. */
2723
2724static int
2725next_overlay_change (pos)
2726 int pos;
2727{
2728 int noverlays;
2729 int endpos;
2730 Lisp_Object *overlays;
5f5c8ee5
GM
2731 int i;
2732
2733 /* Get all overlays at the given position. */
7994a991 2734 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
5f5c8ee5
GM
2735
2736 /* If any of these overlays ends before endpos,
2737 use its ending point instead. */
2738 for (i = 0; i < noverlays; ++i)
2739 {
2740 Lisp_Object oend;
2741 int oendpos;
2742
2743 oend = OVERLAY_END (overlays[i]);
2744 oendpos = OVERLAY_POSITION (oend);
2745 endpos = min (endpos, oendpos);
2746 }
2747
2748 return endpos;
2749}
2750
2751
2752\f
2753/***********************************************************************
2754 Fontification
2755 ***********************************************************************/
2756
2757/* Handle changes in the `fontified' property of the current buffer by
2758 calling hook functions from Qfontification_functions to fontify
2759 regions of text. */
2760
2761static enum prop_handled
2762handle_fontified_prop (it)
2763 struct it *it;
2764{
2765 Lisp_Object prop, pos;
2766 enum prop_handled handled = HANDLED_NORMALLY;
2767
2768 /* Get the value of the `fontified' property at IT's current buffer
2769 position. (The `fontified' property doesn't have a special
2770 meaning in strings.) If the value is nil, call functions from
2771 Qfontification_functions. */
2772 if (!STRINGP (it->string)
2773 && it->s == NULL
2774 && !NILP (Vfontification_functions)
085536c2 2775 && !NILP (Vrun_hooks)
5f5c8ee5
GM
2776 && (pos = make_number (IT_CHARPOS (*it)),
2777 prop = Fget_char_property (pos, Qfontified, Qnil),
2778 NILP (prop)))
2779 {
331379bf 2780 int count = SPECPDL_INDEX ();
085536c2
GM
2781 Lisp_Object val;
2782
2783 val = Vfontification_functions;
2784 specbind (Qfontification_functions, Qnil);
2311178e 2785
085536c2 2786 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
116d6f5c 2787 safe_call1 (val, pos);
085536c2
GM
2788 else
2789 {
2790 Lisp_Object globals, fn;
2791 struct gcpro gcpro1, gcpro2;
5f5c8ee5 2792
085536c2
GM
2793 globals = Qnil;
2794 GCPRO2 (val, globals);
2311178e 2795
085536c2
GM
2796 for (; CONSP (val); val = XCDR (val))
2797 {
2798 fn = XCAR (val);
2311178e 2799
085536c2
GM
2800 if (EQ (fn, Qt))
2801 {
2802 /* A value of t indicates this hook has a local
2803 binding; it means to run the global binding too.
2804 In a global value, t should not occur. If it
2805 does, we must ignore it to avoid an endless
2806 loop. */
2807 for (globals = Fdefault_value (Qfontification_functions);
2808 CONSP (globals);
2809 globals = XCDR (globals))
2810 {
2811 fn = XCAR (globals);
2812 if (!EQ (fn, Qt))
116d6f5c 2813 safe_call1 (fn, pos);
085536c2
GM
2814 }
2815 }
2816 else
116d6f5c 2817 safe_call1 (fn, pos);
085536c2
GM
2818 }
2819
2820 UNGCPRO;
2821 }
2822
2823 unbind_to (count, Qnil);
5f5c8ee5
GM
2824
2825 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2826 something. This avoids an endless loop if they failed to
2827 fontify the text for which reason ever. */
2828 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2829 handled = HANDLED_RECOMPUTE_PROPS;
2830 }
2831
2832 return handled;
2833}
2834
2835
2836\f
2837/***********************************************************************
2838 Faces
2839 ***********************************************************************/
2840
2841/* Set up iterator IT from face properties at its current position.
2842 Called from handle_stop. */
2843
2844static enum prop_handled
2845handle_face_prop (it)
2846 struct it *it;
2847{
2848 int new_face_id, next_stop;
2311178e 2849
5f5c8ee5
GM
2850 if (!STRINGP (it->string))
2851 {
2852 new_face_id
2853 = face_at_buffer_position (it->w,
2854 IT_CHARPOS (*it),
2855 it->region_beg_charpos,
2856 it->region_end_charpos,
2857 &next_stop,
2858 (IT_CHARPOS (*it)
2859 + TEXT_PROP_DISTANCE_LIMIT),
2860 0);
2311178e 2861
5f5c8ee5
GM
2862 /* Is this a start of a run of characters with box face?
2863 Caveat: this can be called for a freshly initialized
4b41cebb 2864 iterator; face_id is -1 in this case. We know that the new
5f5c8ee5
GM
2865 face will not change until limit, i.e. if the new face has a
2866 box, all characters up to limit will have one. But, as
2867 usual, we don't know whether limit is really the end. */
2868 if (new_face_id != it->face_id)
2869 {
2870 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2311178e 2871
5f5c8ee5
GM
2872 /* If new face has a box but old face has not, this is
2873 the start of a run of characters with box, i.e. it has
2874 a shadow on the left side. The value of face_id of the
2875 iterator will be -1 if this is the initial call that gets
2876 the face. In this case, we have to look in front of IT's
2877 position and see whether there is a face != new_face_id. */
2878 it->start_of_box_run_p
2879 = (new_face->box != FACE_NO_BOX
2880 && (it->face_id >= 0
2881 || IT_CHARPOS (*it) == BEG
2882 || new_face_id != face_before_it_pos (it)));
2883 it->face_box_p = new_face->box != FACE_NO_BOX;
2884 }
2885 }
2886 else
2887 {
06a12811
GM
2888 int base_face_id, bufpos;
2889
2890 if (it->current.overlay_string_index >= 0)
2891 bufpos = IT_CHARPOS (*it);
2892 else
2893 bufpos = 0;
2311178e 2894
06a12811
GM
2895 /* For strings from a buffer, i.e. overlay strings or strings
2896 from a `display' property, use the face at IT's current
2897 buffer position as the base face to merge with, so that
2898 overlay strings appear in the same face as surrounding
2899 text, unless they specify their own faces. */
2900 base_face_id = underlying_face_id (it);
2311178e 2901
06a12811
GM
2902 new_face_id = face_at_string_position (it->w,
2903 it->string,
2904 IT_STRING_CHARPOS (*it),
2905 bufpos,
2906 it->region_beg_charpos,
2907 it->region_end_charpos,
2908 &next_stop,
5de7c6f2 2909 base_face_id, 0);
2311178e 2910
5f5c8ee5
GM
2911#if 0 /* This shouldn't be neccessary. Let's check it. */
2912 /* If IT is used to display a mode line we would really like to
2913 use the mode line face instead of the frame's default face. */
2914 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2915 && new_face_id == DEFAULT_FACE_ID)
96d2320f 2916 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
5f5c8ee5 2917#endif
2311178e 2918
5f5c8ee5
GM
2919 /* Is this a start of a run of characters with box? Caveat:
2920 this can be called for a freshly allocated iterator; face_id
2921 is -1 is this case. We know that the new face will not
2922 change until the next check pos, i.e. if the new face has a
2923 box, all characters up to that position will have a
2924 box. But, as usual, we don't know whether that position
2925 is really the end. */
2926 if (new_face_id != it->face_id)
2927 {
2928 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2929 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2311178e 2930
5f5c8ee5
GM
2931 /* If new face has a box but old face hasn't, this is the
2932 start of a run of characters with box, i.e. it has a
2933 shadow on the left side. */
2934 it->start_of_box_run_p
2935 = new_face->box && (old_face == NULL || !old_face->box);
2936 it->face_box_p = new_face->box != FACE_NO_BOX;
2937 }
2938 }
2311178e 2939
5f5c8ee5 2940 it->face_id = new_face_id;
5f5c8ee5
GM
2941 return HANDLED_NORMALLY;
2942}
2943
2944
06a12811
GM
2945/* Return the ID of the face ``underlying'' IT's current position,
2946 which is in a string. If the iterator is associated with a
2947 buffer, return the face at IT's current buffer position.
2948 Otherwise, use the iterator's base_face_id. */
2949
2950static int
2951underlying_face_id (it)
2952 struct it *it;
2953{
2954 int face_id = it->base_face_id, i;
2955
2956 xassert (STRINGP (it->string));
2957
2958 for (i = it->sp - 1; i >= 0; --i)
2959 if (NILP (it->stack[i].string))
2960 face_id = it->stack[i].face_id;
2961
2962 return face_id;
2963}
2964
2965
5f5c8ee5
GM
2966/* Compute the face one character before or after the current position
2967 of IT. BEFORE_P non-zero means get the face in front of IT's
2968 position. Value is the id of the face. */
2969
2970static int
2971face_before_or_after_it_pos (it, before_p)
2972 struct it *it;
2973 int before_p;
2974{
2975 int face_id, limit;
2976 int next_check_charpos;
2977 struct text_pos pos;
2978
2979 xassert (it->s == NULL);
2311178e 2980
5f5c8ee5
GM
2981 if (STRINGP (it->string))
2982 {
06a12811 2983 int bufpos, base_face_id;
2311178e 2984
5f5c8ee5
GM
2985 /* No face change past the end of the string (for the case
2986 we are padding with spaces). No face change before the
2987 string start. */
2051c264 2988 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
5f5c8ee5
GM
2989 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2990 return it->face_id;
2991
2992 /* Set pos to the position before or after IT's current position. */
2993 if (before_p)
2994 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2995 else
260a86a0
KH
2996 /* For composition, we must check the character after the
2997 composition. */
2998 pos = (it->what == IT_COMPOSITION
2999 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3000 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
5f5c8ee5 3001
06a12811
GM
3002 if (it->current.overlay_string_index >= 0)
3003 bufpos = IT_CHARPOS (*it);
3004 else
3005 bufpos = 0;
3006
3007 base_face_id = underlying_face_id (it);
3008
5f5c8ee5 3009 /* Get the face for ASCII, or unibyte. */
06a12811
GM
3010 face_id = face_at_string_position (it->w,
3011 it->string,
3012 CHARPOS (pos),
3013 bufpos,
3014 it->region_beg_charpos,
3015 it->region_end_charpos,
3016 &next_check_charpos,
5de7c6f2 3017 base_face_id, 0);
5f5c8ee5
GM
3018
3019 /* Correct the face for charsets different from ASCII. Do it
3020 for the multibyte case only. The face returned above is
3021 suitable for unibyte text if IT->string is unibyte. */
3022 if (STRING_MULTIBYTE (it->string))
3023 {
50f80c2f 3024 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
2051c264 3025 int rest = SBYTES (it->string) - BYTEPOS (pos);
980806b6
KH
3026 int c, len;
3027 struct face *face = FACE_FROM_ID (it->f, face_id);
2311178e 3028
4fdb80f2 3029 c = string_char_and_length (p, rest, &len);
980806b6 3030 face_id = FACE_FOR_CHAR (it->f, face, c);
5f5c8ee5
GM
3031 }
3032 }
3033 else
3034 {
70851746
GM
3035 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3036 || (IT_CHARPOS (*it) <= BEGV && before_p))
3037 return it->face_id;
2311178e 3038
5f5c8ee5
GM
3039 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3040 pos = it->current.pos;
2311178e 3041
5f5c8ee5 3042 if (before_p)
c1005d06 3043 DEC_TEXT_POS (pos, it->multibyte_p);
5f5c8ee5 3044 else
260a86a0
KH
3045 {
3046 if (it->what == IT_COMPOSITION)
3047 /* For composition, we must check the position after the
3048 composition. */
3049 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3050 else
c1005d06 3051 INC_TEXT_POS (pos, it->multibyte_p);
260a86a0 3052 }
2311178e 3053
5f5c8ee5
GM
3054 /* Determine face for CHARSET_ASCII, or unibyte. */
3055 face_id = face_at_buffer_position (it->w,
3056 CHARPOS (pos),
3057 it->region_beg_charpos,
3058 it->region_end_charpos,
3059 &next_check_charpos,
3060 limit, 0);
3061
3062 /* Correct the face for charsets different from ASCII. Do it
3063 for the multibyte case only. The face returned above is
3064 suitable for unibyte text if current_buffer is unibyte. */
3065 if (it->multibyte_p)
3066 {
1d1b6e6a 3067 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
980806b6
KH
3068 struct face *face = FACE_FROM_ID (it->f, face_id);
3069 face_id = FACE_FOR_CHAR (it->f, face, c);
5f5c8ee5
GM
3070 }
3071 }
2311178e 3072
5f5c8ee5
GM
3073 return face_id;
3074}
3075
3076
3077\f
3078/***********************************************************************
3079 Invisible text
3080 ***********************************************************************/
3081
3082/* Set up iterator IT from invisible properties at its current
3083 position. Called from handle_stop. */
3084
3085static enum prop_handled
3086handle_invisible_prop (it)
3087 struct it *it;
3088{
3089 enum prop_handled handled = HANDLED_NORMALLY;
3090
3091 if (STRINGP (it->string))
3092 {
3093 extern Lisp_Object Qinvisible;
3094 Lisp_Object prop, end_charpos, limit, charpos;
3095
3096 /* Get the value of the invisible text property at the
3097 current position. Value will be nil if there is no such
3098 property. */
ac90c44f 3099 charpos = make_number (IT_STRING_CHARPOS (*it));
5f5c8ee5
GM
3100 prop = Fget_text_property (charpos, Qinvisible, it->string);
3101
eadc0bf8
GM
3102 if (!NILP (prop)
3103 && IT_STRING_CHARPOS (*it) < it->end_charpos)
5f5c8ee5
GM
3104 {
3105 handled = HANDLED_RECOMPUTE_PROPS;
2311178e 3106
5f5c8ee5
GM
3107 /* Get the position at which the next change of the
3108 invisible text property can be found in IT->string.
3109 Value will be nil if the property value is the same for
3110 all the rest of IT->string. */
2051c264 3111 XSETINT (limit, SCHARS (it->string));
5f5c8ee5 3112 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2051c264 3113 it->string, limit);
2311178e 3114
5f5c8ee5
GM
3115 /* Text at current position is invisible. The next
3116 change in the property is at position end_charpos.
3117 Move IT's current position to that position. */
3118 if (INTEGERP (end_charpos)
3119 && XFASTINT (end_charpos) < XFASTINT (limit))
3120 {
3121 struct text_pos old;
3122 old = it->current.string_pos;
3123 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3124 compute_string_pos (&it->current.string_pos, old, it->string);
3125 }
3126 else
3127 {
3128 /* The rest of the string is invisible. If this is an
3129 overlay string, proceed with the next overlay string
3130 or whatever comes and return a character from there. */
3131 if (it->current.overlay_string_index >= 0)
3132 {
3133 next_overlay_string (it);
3134 /* Don't check for overlay strings when we just
3135 finished processing them. */
3136 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3137 }
3138 else
3139 {
2051c264
GM
3140 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3141 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
5f5c8ee5
GM
3142 }
3143 }
3144 }
3145 }
3146 else
3147 {
8580a4e3 3148 int invis_p, newpos, next_stop, start_charpos;
5a08cbaf 3149 Lisp_Object pos, prop, overlay;
5f5c8ee5
GM
3150
3151 /* First of all, is there invisible text at this position? */
5a08cbaf 3152 start_charpos = IT_CHARPOS (*it);
ac90c44f 3153 pos = make_number (IT_CHARPOS (*it));
5a08cbaf
GM
3154 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3155 &overlay);
8580a4e3
SM
3156 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3157
5f5c8ee5 3158 /* If we are on invisible text, skip over it. */
8580a4e3 3159 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
5f5c8ee5
GM
3160 {
3161 /* Record whether we have to display an ellipsis for the
3162 invisible text. */
8580a4e3 3163 int display_ellipsis_p = invis_p == 2;
5f5c8ee5
GM
3164
3165 handled = HANDLED_RECOMPUTE_PROPS;
2311178e 3166
5f5c8ee5
GM
3167 /* Loop skipping over invisible text. The loop is left at
3168 ZV or with IT on the first char being visible again. */
3169 do
3170 {
3171 /* Try to skip some invisible text. Return value is the
3172 position reached which can be equal to IT's position
3173 if there is nothing invisible here. This skips both
3174 over invisible text properties and overlays with
3175 invisible property. */
3176 newpos = skip_invisible (IT_CHARPOS (*it),
3177 &next_stop, ZV, it->window);
3178
3179 /* If we skipped nothing at all we weren't at invisible
3180 text in the first place. If everything to the end of
3181 the buffer was skipped, end the loop. */
3182 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
8580a4e3 3183 invis_p = 0;
5f5c8ee5
GM
3184 else
3185 {
3186 /* We skipped some characters but not necessarily
3187 all there are. Check if we ended up on visible
3188 text. Fget_char_property returns the property of
3189 the char before the given position, i.e. if we
8580a4e3 3190 get invis_p = 0, this means that the char at
5f5c8ee5 3191 newpos is visible. */
ac90c44f 3192 pos = make_number (newpos);
5f5c8ee5 3193 prop = Fget_char_property (pos, Qinvisible, it->window);
8580a4e3 3194 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
5f5c8ee5 3195 }
2311178e 3196
5f5c8ee5
GM
3197 /* If we ended up on invisible text, proceed to
3198 skip starting with next_stop. */
8580a4e3 3199 if (invis_p)
5f5c8ee5
GM
3200 IT_CHARPOS (*it) = next_stop;
3201 }
8580a4e3 3202 while (invis_p);
5a08cbaf 3203
5f5c8ee5
GM
3204 /* The position newpos is now either ZV or on visible text. */
3205 IT_CHARPOS (*it) = newpos;
3206 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2311178e 3207
5a08cbaf
GM
3208 /* If there are before-strings at the start of invisible
3209 text, and the text is invisible because of a text
3210 property, arrange to show before-strings because 20.x did
3211 it that way. (If the text is invisible because of an
3212 overlay property instead of a text property, this is
3213 already handled in the overlay code.) */
3214 if (NILP (overlay)
3215 && get_overlay_strings (it, start_charpos))
5f5c8ee5 3216 {
5a08cbaf
GM
3217 handled = HANDLED_RECOMPUTE_PROPS;
3218 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
5f5c8ee5 3219 }
5a08cbaf
GM
3220 else if (display_ellipsis_p)
3221 setup_for_ellipsis (it);
5f5c8ee5
GM
3222 }
3223 }
3224
3225 return handled;
3226}
3227
3228
5a08cbaf
GM
3229/* Make iterator IT return `...' next. */
3230
3231static void
3232setup_for_ellipsis (it)
3233 struct it *it;
3234{
2311178e 3235 if (it->dp
5a08cbaf
GM
3236 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3237 {
3238 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3239 it->dpvec = v->contents;
3240 it->dpend = v->contents + v->size;
3241 }
2311178e 3242 else
5a08cbaf
GM
3243 {
3244 /* Default `...'. */
3245 it->dpvec = default_invis_vector;
3246 it->dpend = default_invis_vector + 3;
3247 }
2311178e 3248
5a08cbaf
GM
3249 /* The ellipsis display does not replace the display of the
3250 character at the new position. Indicate this by setting
3251 IT->dpvec_char_len to zero. */
3252 it->dpvec_char_len = 0;
2311178e 3253
5a08cbaf
GM
3254 it->current.dpvec_index = 0;
3255 it->method = next_element_from_display_vector;
3256}
3257
3258
5f5c8ee5
GM
3259\f
3260/***********************************************************************
3261 'display' property
3262 ***********************************************************************/
3263
3264/* Set up iterator IT from `display' property at its current position.
3265 Called from handle_stop. */
3266
3267static enum prop_handled
3268handle_display_prop (it)
3269 struct it *it;
3270{
3271 Lisp_Object prop, object;
3272 struct text_pos *position;
a61b7058 3273 int display_replaced_p = 0;
5f5c8ee5
GM
3274
3275 if (STRINGP (it->string))
3276 {
3277 object = it->string;
3278 position = &it->current.string_pos;
3279 }
3280 else
3281 {
221dd3e7 3282 object = it->w->buffer;
5f5c8ee5
GM
3283 position = &it->current.pos;
3284 }
3285
3286 /* Reset those iterator values set from display property values. */
e893970b 3287 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
5f5c8ee5 3288 it->space_width = Qnil;
e893970b 3289 it->font_height = Qnil;
5f5c8ee5
GM
3290 it->voffset = 0;
3291
3292 /* We don't support recursive `display' properties, i.e. string
3293 values that have a string `display' property, that have a string
3294 `display' property etc. */
3295 if (!it->string_from_display_prop_p)
3296 it->area = TEXT_AREA;
3297
3298 prop = Fget_char_property (make_number (position->charpos),
3299 Qdisplay, object);
3300 if (NILP (prop))
3301 return HANDLED_NORMALLY;
3302
f3751a65 3303 if (CONSP (prop)
12700f40
GM
3304 /* Simple properties. */
3305 && !EQ (XCAR (prop), Qimage)
3306 && !EQ (XCAR (prop), Qspace)
3307 && !EQ (XCAR (prop), Qwhen)
e893970b 3308 && !EQ (XCAR (prop), Qslice)
12700f40
GM
3309 && !EQ (XCAR (prop), Qspace_width)
3310 && !EQ (XCAR (prop), Qheight)
3311 && !EQ (XCAR (prop), Qraise)
3312 /* Marginal area specifications. */
3313 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
6d925726
KS
3314 && !EQ (XCAR (prop), Qleft_fringe)
3315 && !EQ (XCAR (prop), Qright_fringe)
12700f40 3316 && !NILP (XCAR (prop)))
5f5c8ee5 3317 {
a61b7058 3318 for (; CONSP (prop); prop = XCDR (prop))
5f5c8ee5 3319 {
a61b7058
GM
3320 if (handle_single_display_prop (it, XCAR (prop), object,
3321 position, display_replaced_p))
3322 display_replaced_p = 1;
5f5c8ee5
GM
3323 }
3324 }
3325 else if (VECTORP (prop))
3326 {
3327 int i;
a61b7058
GM
3328 for (i = 0; i < ASIZE (prop); ++i)
3329 if (handle_single_display_prop (it, AREF (prop, i), object,
3330 position, display_replaced_p))
3331 display_replaced_p = 1;
5f5c8ee5
GM
3332 }
3333 else
3334 {
a61b7058
GM
3335 if (handle_single_display_prop (it, prop, object, position, 0))
3336 display_replaced_p = 1;
5f5c8ee5
GM
3337 }
3338
a61b7058 3339 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
5f5c8ee5
GM
3340}
3341
3342
6c577098 3343/* Value is the position of the end of the `display' property starting
5f5c8ee5
GM
3344 at START_POS in OBJECT. */
3345
3346static struct text_pos
3347display_prop_end (it, object, start_pos)
3348 struct it *it;
3349 Lisp_Object object;
3350 struct text_pos start_pos;
3351{
3352 Lisp_Object end;
3353 struct text_pos end_pos;
5f5c8ee5 3354
016b5642
MB
3355 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3356 Qdisplay, object, Qnil);
6c577098
GM
3357 CHARPOS (end_pos) = XFASTINT (end);
3358 if (STRINGP (object))
5f5c8ee5
GM
3359 compute_string_pos (&end_pos, start_pos, it->string);
3360 else
6c577098 3361 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
5f5c8ee5
GM
3362
3363 return end_pos;
3364}
3365
3366
3367/* Set up IT from a single `display' sub-property value PROP. OBJECT
3368 is the object in which the `display' property was found. *POSITION
a61b7058
GM
3369 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3370 means that we previously saw a display sub-property which already
3371 replaced text display with something else, for example an image;
3372 ignore such properties after the first one has been processed.
5f5c8ee5
GM
3373
3374 If PROP is a `space' or `image' sub-property, set *POSITION to the
3375 end position of the `display' property.
3376
4b41cebb 3377 Value is non-zero if something was found which replaces the display
a61b7058 3378 of buffer or string text. */
5f5c8ee5
GM
3379
3380static int
a61b7058
GM
3381handle_single_display_prop (it, prop, object, position,
3382 display_replaced_before_p)
5f5c8ee5
GM
3383 struct it *it;
3384 Lisp_Object prop;
3385 Lisp_Object object;
3386 struct text_pos *position;
a61b7058 3387 int display_replaced_before_p;
5f5c8ee5
GM
3388{
3389 Lisp_Object value;
a61b7058 3390 int replaces_text_display_p = 0;
5f5c8ee5
GM
3391 Lisp_Object form;
3392
d3acf96b 3393 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
4b41cebb 3394 evaluated. If the result is nil, VALUE is ignored. */
5f5c8ee5 3395 form = Qt;
d3acf96b 3396 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5f5c8ee5
GM
3397 {
3398 prop = XCDR (prop);
3399 if (!CONSP (prop))
3400 return 0;
3401 form = XCAR (prop);
3402 prop = XCDR (prop);
5f5c8ee5
GM
3403 }
3404
3405 if (!NILP (form) && !EQ (form, Qt))
3406 {
331379bf 3407 int count = SPECPDL_INDEX ();
5f5c8ee5 3408 struct gcpro gcpro1;
5f5c8ee5 3409
b384d6f8
GM
3410 /* Bind `object' to the object having the `display' property, a
3411 buffer or string. Bind `position' to the position in the
3412 object where the property was found, and `buffer-position'
3413 to the current position in the buffer. */
3414 specbind (Qobject, object);
31ac723b
SM
3415 specbind (Qposition, make_number (CHARPOS (*position)));
3416 specbind (Qbuffer_position,
3417 make_number (STRINGP (object)
3418 ? IT_CHARPOS (*it) : CHARPOS (*position)));
b384d6f8 3419 GCPRO1 (form);
116d6f5c 3420 form = safe_eval (form);
b384d6f8
GM
3421 UNGCPRO;
3422 unbind_to (count, Qnil);
5f5c8ee5 3423 }
2311178e 3424
5f5c8ee5
GM
3425 if (NILP (form))
3426 return 0;
3427
3428 if (CONSP (prop)
3429 && EQ (XCAR (prop), Qheight)
3430 && CONSP (XCDR (prop)))
3431 {
e4093f38 3432 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
5f5c8ee5 3433 return 0;
2311178e 3434
5f5c8ee5
GM
3435 /* `(height HEIGHT)'. */
3436 it->font_height = XCAR (XCDR (prop));
3437 if (!NILP (it->font_height))
3438 {
3439 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3440 int new_height = -1;
3441
3442 if (CONSP (it->font_height)
3443 && (EQ (XCAR (it->font_height), Qplus)
3444 || EQ (XCAR (it->font_height), Qminus))
3445 && CONSP (XCDR (it->font_height))
3446 && INTEGERP (XCAR (XCDR (it->font_height))))
3447 {
3448 /* `(+ N)' or `(- N)' where N is an integer. */
3449 int steps = XINT (XCAR (XCDR (it->font_height)));
3450 if (EQ (XCAR (it->font_height), Qplus))
3451 steps = - steps;
3452 it->face_id = smaller_face (it->f, it->face_id, steps);
3453 }
0d8b31c0 3454 else if (FUNCTIONP (it->font_height))
5f5c8ee5
GM
3455 {
3456 /* Call function with current height as argument.
3457 Value is the new height. */
116d6f5c
GM
3458 Lisp_Object height;
3459 height = safe_call1 (it->font_height,
3460 face->lface[LFACE_HEIGHT_INDEX]);
5f5c8ee5
GM
3461 if (NUMBERP (height))
3462 new_height = XFLOATINT (height);
5f5c8ee5
GM
3463 }
3464 else if (NUMBERP (it->font_height))
3465 {
3466 /* Value is a multiple of the canonical char height. */
3467 struct face *face;
2311178e 3468
5f5c8ee5
GM
3469 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3470 new_height = (XFLOATINT (it->font_height)
3471 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3472 }
3473 else
3474 {
3475 /* Evaluate IT->font_height with `height' bound to the
3476 current specified height to get the new height. */
3477 Lisp_Object value;
331379bf 3478 int count = SPECPDL_INDEX ();
2311178e 3479
5f5c8ee5 3480 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
116d6f5c 3481 value = safe_eval (it->font_height);
5f5c8ee5 3482 unbind_to (count, Qnil);
2311178e 3483
5f5c8ee5
GM
3484 if (NUMBERP (value))
3485 new_height = XFLOATINT (value);
3486 }
2311178e 3487
5f5c8ee5
GM
3488 if (new_height > 0)
3489 it->face_id = face_with_height (it->f, it->face_id, new_height);
3490 }
3491 }
3492 else if (CONSP (prop)
3493 && EQ (XCAR (prop), Qspace_width)
3494 && CONSP (XCDR (prop)))
3495 {
3496 /* `(space_width WIDTH)'. */
e4093f38 3497 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
bafb434c 3498 return 0;
2311178e 3499
5f5c8ee5
GM
3500 value = XCAR (XCDR (prop));
3501 if (NUMBERP (value) && XFLOATINT (value) > 0)
3502 it->space_width = value;
3503 }
e893970b
KS
3504 else if (CONSP (prop)
3505 && EQ (XCAR (prop), Qslice))
3506 {
3507 /* `(slice X Y WIDTH HEIGHT)'. */
3508 Lisp_Object tem;
3509
3510 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3511 return 0;
3512
3513 if (tem = XCDR (prop), CONSP (tem))
3514 {
3515 it->slice.x = XCAR (tem);
3516 if (tem = XCDR (tem), CONSP (tem))
3517 {
3518 it->slice.y = XCAR (tem);
3519 if (tem = XCDR (tem), CONSP (tem))
3520 {
3521 it->slice.width = XCAR (tem);
3522 if (tem = XCDR (tem), CONSP (tem))
3523 it->slice.height = XCAR (tem);
3524 }
3525 }
3526 }
3527 }
5f5c8ee5
GM
3528 else if (CONSP (prop)
3529 && EQ (XCAR (prop), Qraise)
3530 && CONSP (XCDR (prop)))
3531 {
5f5c8ee5 3532 /* `(raise FACTOR)'. */
e4093f38 3533 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
bafb434c 3534 return 0;
2311178e 3535
fb3842a8 3536#ifdef HAVE_WINDOW_SYSTEM
5f5c8ee5
GM
3537 value = XCAR (XCDR (prop));
3538 if (NUMBERP (value))
3539 {
3540 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3541 it->voffset = - (XFLOATINT (value)
1ab3e082 3542 * (FONT_HEIGHT (face->font)));
5f5c8ee5
GM
3543 }
3544#endif /* HAVE_WINDOW_SYSTEM */
3545 }
3546 else if (!it->string_from_display_prop_p)
3547 {
f3751a65 3548 /* `((margin left-margin) VALUE)' or `((margin right-margin)
4b41cebb 3549 VALUE) or `((margin nil) VALUE)' or VALUE. */
5f5c8ee5
GM
3550 Lisp_Object location, value;
3551 struct text_pos start_pos;
3552 int valid_p;
3553
3554 /* Characters having this form of property are not displayed, so
3555 we have to find the end of the property. */
5f5c8ee5
GM
3556 start_pos = *position;
3557 *position = display_prop_end (it, object, start_pos);
15e26c76 3558 value = Qnil;
5f5c8ee5
GM
3559
3560 /* Let's stop at the new position and assume that all
3561 text properties change there. */
3562 it->stop_charpos = position->charpos;
3563
dd341dd9
KS
3564 if (CONSP (prop)
3565 && (EQ (XCAR (prop), Qleft_fringe)
3566 || EQ (XCAR (prop), Qright_fringe))
3567 && CONSP (XCDR (prop)))
3568 {
3569 unsigned face_id = DEFAULT_FACE_ID;
3570
3571 /* Save current settings of IT so that we can restore them
3572 when we are finished with the glyph property value. */
3573
3574 /* `(left-fringe BITMAP FACE)'. */
3575 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3576 return 0;
3577
3578#ifdef HAVE_WINDOW_SYSTEM
3579 value = XCAR (XCDR (prop));
3580 if (!NUMBERP (value)
3581 || !valid_fringe_bitmap_id_p (XINT (value)))
3582 return 0;
3583
3584 if (CONSP (XCDR (XCDR (prop))))
3585 {
3586 Lisp_Object face_name = XCAR (XCDR (XCDR (prop)));
3587
3588 face_id = lookup_named_face (it->f, face_name, 'A');
3589 if (face_id < 0)
3590 return 0;
3591 }
3592
3593 push_it (it);
3594
3595 it->area = TEXT_AREA;
3596 it->what = IT_IMAGE;
3597 it->image_id = -1; /* no image */
3598 it->position = start_pos;
3599 it->object = NILP (object) ? it->w->buffer : object;
3600 it->method = next_element_from_image;
3601 it->face_id = face_id;
3602
3603 /* Say that we haven't consumed the characters with
3604 `display' property yet. The call to pop_it in
3605 set_iterator_to_next will clean this up. */
3606 *position = start_pos;
3607
3608 if (EQ (XCAR (prop), Qleft_fringe))
3609 {
3610 it->left_user_fringe_bitmap = XINT (value);
3611 it->left_user_fringe_face_id = face_id;
3612 }
3613 else
3614 {
3615 it->right_user_fringe_bitmap = XINT (value);
3616 it->right_user_fringe_face_id = face_id;
3617 }
3618#endif /* HAVE_WINDOW_SYSTEM */
3619 return 1;
3620 }
3621
f3751a65
GM
3622 location = Qunbound;
3623 if (CONSP (prop) && CONSP (XCAR (prop)))
5f5c8ee5 3624 {
f3751a65 3625 Lisp_Object tem;
2311178e 3626
5f5c8ee5 3627 value = XCDR (prop);
f3751a65
GM
3628 if (CONSP (value))
3629 value = XCAR (value);
3630
3631 tem = XCAR (prop);
3632 if (EQ (XCAR (tem), Qmargin)
3633 && (tem = XCDR (tem),
3634 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3635 (NILP (tem)
3636 || EQ (tem, Qleft_margin)
3637 || EQ (tem, Qright_margin))))
3638 location = tem;
5f5c8ee5 3639 }
f3751a65
GM
3640
3641 if (EQ (location, Qunbound))
5f5c8ee5
GM
3642 {
3643 location = Qnil;
3644 value = prop;
3645 }
3646
da06e4d7 3647 valid_p = (STRINGP (value)
5f5c8ee5 3648#ifdef HAVE_WINDOW_SYSTEM
da06e4d7 3649 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
5f5c8ee5 3650#endif /* not HAVE_WINDOW_SYSTEM */
da06e4d7 3651 || (CONSP (value) && EQ (XCAR (value), Qspace)));
2311178e 3652
5f5c8ee5
GM
3653 if ((EQ (location, Qleft_margin)
3654 || EQ (location, Qright_margin)
3655 || NILP (location))
a61b7058
GM
3656 && valid_p
3657 && !display_replaced_before_p)
5f5c8ee5 3658 {
a61b7058 3659 replaces_text_display_p = 1;
2311178e 3660
5f5c8ee5
GM
3661 /* Save current settings of IT so that we can restore them
3662 when we are finished with the glyph property value. */
3663 push_it (it);
2311178e 3664
5f5c8ee5
GM
3665 if (NILP (location))
3666 it->area = TEXT_AREA;
3667 else if (EQ (location, Qleft_margin))
3668 it->area = LEFT_MARGIN_AREA;
3669 else
3670 it->area = RIGHT_MARGIN_AREA;
2311178e 3671
5f5c8ee5
GM
3672 if (STRINGP (value))
3673 {
3674 it->string = value;
3675 it->multibyte_p = STRING_MULTIBYTE (it->string);
3676 it->current.overlay_string_index = -1;
3677 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2051c264 3678 it->end_charpos = it->string_nchars = SCHARS (it->string);
5f5c8ee5
GM
3679 it->method = next_element_from_string;
3680 it->stop_charpos = 0;
3681 it->string_from_display_prop_p = 1;
e187cf71
GM
3682 /* Say that we haven't consumed the characters with
3683 `display' property yet. The call to pop_it in
3684 set_iterator_to_next will clean this up. */
3685 *position = start_pos;
5f5c8ee5
GM
3686 }
3687 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3688 {
3689 it->method = next_element_from_stretch;
3690 it->object = value;
3691 it->current.pos = it->position = start_pos;
3692 }
3693#ifdef HAVE_WINDOW_SYSTEM
3694 else
3695 {
3696 it->what = IT_IMAGE;
3697 it->image_id = lookup_image (it->f, value);
3698 it->position = start_pos;
3699 it->object = NILP (object) ? it->w->buffer : object;
3700 it->method = next_element_from_image;
2311178e 3701
02513cdd 3702 /* Say that we haven't consumed the characters with
5f5c8ee5
GM
3703 `display' property yet. The call to pop_it in
3704 set_iterator_to_next will clean this up. */
3705 *position = start_pos;
3706 }
3707#endif /* HAVE_WINDOW_SYSTEM */
3708 }
a21a3943
GM
3709 else
3710 /* Invalid property or property not supported. Restore
3711 the position to what it was before. */
3712 *position = start_pos;
5f5c8ee5
GM
3713 }
3714
a61b7058 3715 return replaces_text_display_p;
5f5c8ee5
GM
3716}
3717
3718
06568bbf
GM
3719/* Check if PROP is a display sub-property value whose text should be
3720 treated as intangible. */
3721
3722static int
3723single_display_prop_intangible_p (prop)
3724 Lisp_Object prop;
3725{
3726 /* Skip over `when FORM'. */
3727 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3728 {
3729 prop = XCDR (prop);
3730 if (!CONSP (prop))
3731 return 0;
3732 prop = XCDR (prop);
3733 }
3734
959bc044
SM
3735 if (STRINGP (prop))
3736 return 1;
3737
06568bbf
GM
3738 if (!CONSP (prop))
3739 return 0;
3740
3741 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3742 we don't need to treat text as intangible. */
3743 if (EQ (XCAR (prop), Qmargin))
3744 {
3745 prop = XCDR (prop);
3746 if (!CONSP (prop))
3747 return 0;
3748
3749 prop = XCDR (prop);
3750 if (!CONSP (prop)
3751 || EQ (XCAR (prop), Qleft_margin)
3752 || EQ (XCAR (prop), Qright_margin))
3753 return 0;
3754 }
2311178e 3755
6cd83a9a
SM
3756 return (CONSP (prop)
3757 && (EQ (XCAR (prop), Qimage)
3758 || EQ (XCAR (prop), Qspace)));
06568bbf
GM
3759}
3760
3761
3762/* Check if PROP is a display property value whose text should be
3763 treated as intangible. */
3764
3765int
3766display_prop_intangible_p (prop)
3767 Lisp_Object prop;
3768{
3769 if (CONSP (prop)
3770 && CONSP (XCAR (prop))
3771 && !EQ (Qmargin, XCAR (XCAR (prop))))
3772 {
3773 /* A list of sub-properties. */
3774 while (CONSP (prop))
3775 {
3776 if (single_display_prop_intangible_p (XCAR (prop)))
3777 return 1;
3778 prop = XCDR (prop);
3779 }
3780 }
3781 else if (VECTORP (prop))
3782 {
3783 /* A vector of sub-properties. */
3784 int i;
a61b7058
GM
3785 for (i = 0; i < ASIZE (prop); ++i)
3786 if (single_display_prop_intangible_p (AREF (prop, i)))
06568bbf
GM
3787 return 1;
3788 }
3789 else
3790 return single_display_prop_intangible_p (prop);
3791
3792 return 0;
3793}
3794
74bd6d65
GM
3795
3796/* Return 1 if PROP is a display sub-property value containing STRING. */
3797
3798static int
3799single_display_prop_string_p (prop, string)
3800 Lisp_Object prop, string;
3801{
74bd6d65
GM
3802 if (EQ (string, prop))
3803 return 1;
2311178e 3804
74bd6d65
GM
3805 /* Skip over `when FORM'. */
3806 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3807 {
3808 prop = XCDR (prop);
3809 if (!CONSP (prop))
3810 return 0;
3811 prop = XCDR (prop);
3812 }
3813
3814 if (CONSP (prop))
3815 /* Skip over `margin LOCATION'. */
3816 if (EQ (XCAR (prop), Qmargin))
3817 {
3818 prop = XCDR (prop);
3819 if (!CONSP (prop))
3820 return 0;
3821
3822 prop = XCDR (prop);
3823 if (!CONSP (prop))
3824 return 0;
3825 }
2311178e 3826
74bd6d65
GM
3827 return CONSP (prop) && EQ (XCAR (prop), string);
3828}
3829
3830
3831/* Return 1 if STRING appears in the `display' property PROP. */
3832
3833static int
3834display_prop_string_p (prop, string)
3835 Lisp_Object prop, string;
3836{
74bd6d65
GM
3837 if (CONSP (prop)
3838 && CONSP (XCAR (prop))
3839 && !EQ (Qmargin, XCAR (XCAR (prop))))
3840 {
3841 /* A list of sub-properties. */
3842 while (CONSP (prop))
3843 {
3844 if (single_display_prop_string_p (XCAR (prop), string))
3845 return 1;
3846 prop = XCDR (prop);
3847 }
3848 }
3849 else if (VECTORP (prop))
3850 {
3851 /* A vector of sub-properties. */
3852 int i;
3853 for (i = 0; i < ASIZE (prop); ++i)
3854 if (single_display_prop_string_p (AREF (prop, i), string))
3855 return 1;
3856 }
3857 else
3858 return single_display_prop_string_p (prop, string);
3859
3860 return 0;
3861}
3862
3863
3864/* Determine from which buffer position in W's buffer STRING comes
3865 from. AROUND_CHARPOS is an approximate position where it could
3866 be from. Value is the buffer position or 0 if it couldn't be
3867 determined.
3868
3869 W's buffer must be current.
3870
3871 This function is necessary because we don't record buffer positions
3872 in glyphs generated from strings (to keep struct glyph small).
3873 This function may only use code that doesn't eval because it is
3874 called asynchronously from note_mouse_highlight. */
3875
3876int
3877string_buffer_position (w, string, around_charpos)
3878 struct window *w;
3879 Lisp_Object string;
3880 int around_charpos;
3881{
74bd6d65
GM
3882 Lisp_Object limit, prop, pos;
3883 const int MAX_DISTANCE = 1000;
3884 int found = 0;
3885
d8731202 3886 pos = make_number (around_charpos);
74bd6d65
GM
3887 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3888 while (!found && !EQ (pos, limit))
3889 {
3890 prop = Fget_char_property (pos, Qdisplay, Qnil);
3891 if (!NILP (prop) && display_prop_string_p (prop, string))
3892 found = 1;
3893 else
134d9283 3894 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
74bd6d65
GM
3895 }
3896
3897 if (!found)
3898 {
d8731202 3899 pos = make_number (around_charpos);
74bd6d65
GM
3900 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3901 while (!found && !EQ (pos, limit))
3902 {
3903 prop = Fget_char_property (pos, Qdisplay, Qnil);
3904 if (!NILP (prop) && display_prop_string_p (prop, string))
3905 found = 1;
3906 else
134d9283
GM
3907 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3908 limit);
74bd6d65
GM
3909 }
3910 }
3911
3912 return found ? XINT (pos) : 0;
3913}
3914
3915
5f5c8ee5 3916\f
260a86a0
KH
3917/***********************************************************************
3918 `composition' property
3919 ***********************************************************************/
3920
3921/* Set up iterator IT from `composition' property at its current
3922 position. Called from handle_stop. */
3923
3924static enum prop_handled
3925handle_composition_prop (it)
3926 struct it *it;
3927{
3928 Lisp_Object prop, string;
3929 int pos, pos_byte, end;
3930 enum prop_handled handled = HANDLED_NORMALLY;
3931
3932 if (STRINGP (it->string))
3933 {
3934 pos = IT_STRING_CHARPOS (*it);
3935 pos_byte = IT_STRING_BYTEPOS (*it);
3936 string = it->string;
3937 }
3938 else
3939 {
3940 pos = IT_CHARPOS (*it);
3941 pos_byte = IT_BYTEPOS (*it);
3942 string = Qnil;
3943 }
3944
3945 /* If there's a valid composition and point is not inside of the
3946 composition (in the case that the composition is from the current
3947 buffer), draw a glyph composed from the composition components. */
3948 if (find_composition (pos, -1, &pos, &end, &prop, string)
3949 && COMPOSITION_VALID_P (pos, end, prop)
3950 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3951 {
3952 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3953
3954 if (id >= 0)
3955 {
3956 it->method = next_element_from_composition;
3957 it->cmp_id = id;
3958 it->cmp_len = COMPOSITION_LENGTH (prop);
3959 /* For a terminal, draw only the first character of the
3960 components. */
3961 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3962 it->len = (STRINGP (it->string)
3963 ? string_char_to_byte (it->string, end)
3964 : CHAR_TO_BYTE (end)) - pos_byte;
3965 it->stop_charpos = end;
3966 handled = HANDLED_RETURN;
3967 }
3968 }
3969
3970 return handled;
3971}
3972
3973
3974\f
5f5c8ee5
GM
3975/***********************************************************************
3976 Overlay strings
3977 ***********************************************************************/
3978
3979/* The following structure is used to record overlay strings for
3980 later sorting in load_overlay_strings. */
3981
3982struct overlay_entry
3983{
2970b9be 3984 Lisp_Object overlay;
5f5c8ee5
GM
3985 Lisp_Object string;
3986 int priority;
3987 int after_string_p;
3988};
3989
3990
3991/* Set up iterator IT from overlay strings at its current position.
3992 Called from handle_stop. */
3993
3994static enum prop_handled
3995handle_overlay_change (it)
3996 struct it *it;
3997{
5a08cbaf 3998 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
2970b9be 3999 return HANDLED_RECOMPUTE_PROPS;
5f5c8ee5 4000 else
2970b9be 4001 return HANDLED_NORMALLY;
5f5c8ee5
GM
4002}
4003
4004
4005/* Set up the next overlay string for delivery by IT, if there is an
4006 overlay string to deliver. Called by set_iterator_to_next when the
4007 end of the current overlay string is reached. If there are more
4008 overlay strings to display, IT->string and
4009 IT->current.overlay_string_index are set appropriately here.
4010 Otherwise IT->string is set to nil. */
2311178e 4011
5f5c8ee5
GM
4012static void
4013next_overlay_string (it)
4014 struct it *it;
4015{
4016 ++it->current.overlay_string_index;
4017 if (it->current.overlay_string_index == it->n_overlay_strings)
4018 {
4019 /* No more overlay strings. Restore IT's settings to what
4020 they were before overlay strings were processed, and
4021 continue to deliver from current_buffer. */
5a08cbaf 4022 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
2311178e 4023
5f5c8ee5
GM
4024 pop_it (it);
4025 xassert (it->stop_charpos >= BEGV
4026 && it->stop_charpos <= it->end_charpos);
4027 it->string = Qnil;
4028 it->current.overlay_string_index = -1;
4029 SET_TEXT_POS (it->current.string_pos, -1, -1);
4030 it->n_overlay_strings = 0;
4031 it->method = next_element_from_buffer;
2970b9be
GM
4032
4033 /* If we're at the end of the buffer, record that we have
4034 processed the overlay strings there already, so that
4035 next_element_from_buffer doesn't try it again. */
4036 if (IT_CHARPOS (*it) >= it->end_charpos)
4037 it->overlay_strings_at_end_processed_p = 1;
5a08cbaf
GM
4038
4039 /* If we have to display `...' for invisible text, set
4040 the iterator up for that. */
4041 if (display_ellipsis_p)
4042 setup_for_ellipsis (it);
5f5c8ee5
GM
4043 }
4044 else
4045 {
4046 /* There are more overlay strings to process. If
4047 IT->current.overlay_string_index has advanced to a position
4048 where we must load IT->overlay_strings with more strings, do
4049 it. */
4050 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
2311178e 4051
5f5c8ee5 4052 if (it->current.overlay_string_index && i == 0)
5a08cbaf 4053 load_overlay_strings (it, 0);
5f5c8ee5
GM
4054
4055 /* Initialize IT to deliver display elements from the overlay
4056 string. */
4057 it->string = it->overlay_strings[i];
4058 it->multibyte_p = STRING_MULTIBYTE (it->string);
4059 SET_TEXT_POS (it->current.string_pos, 0, 0);
4060 it->method = next_element_from_string;
4061 it->stop_charpos = 0;
4062 }
2311178e 4063
5f5c8ee5
GM
4064 CHECK_IT (it);
4065}
4066
4067
4068/* Compare two overlay_entry structures E1 and E2. Used as a
4069 comparison function for qsort in load_overlay_strings. Overlay
4070 strings for the same position are sorted so that
4071
2970b9be
GM
4072 1. All after-strings come in front of before-strings, except
4073 when they come from the same overlay.
2311178e 4074
5f5c8ee5
GM
4075 2. Within after-strings, strings are sorted so that overlay strings
4076 from overlays with higher priorities come first.
4077
4078 2. Within before-strings, strings are sorted so that overlay
4079 strings from overlays with higher priorities come last.
4080
4081 Value is analogous to strcmp. */
4082
2311178e 4083
5f5c8ee5
GM
4084static int
4085compare_overlay_entries (e1, e2)
4086 void *e1, *e2;
4087{
4088 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4089 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4090 int result;
4091
4092 if (entry1->after_string_p != entry2->after_string_p)
2970b9be
GM
4093 {
4094 /* Let after-strings appear in front of before-strings if
4095 they come from different overlays. */
4096 if (EQ (entry1->overlay, entry2->overlay))
4097 result = entry1->after_string_p ? 1 : -1;
4098 else
4099 result = entry1->after_string_p ? -1 : 1;
4100 }
5f5c8ee5
GM
4101 else if (entry1->after_string_p)
4102 /* After-strings sorted in order of decreasing priority. */
4103 result = entry2->priority - entry1->priority;
4104 else
4105 /* Before-strings sorted in order of increasing priority. */
4106 result = entry1->priority - entry2->priority;
4107
4108 return result;
4109}
4110
4111
4112/* Load the vector IT->overlay_strings with overlay strings from IT's
5a08cbaf
GM
4113 current buffer position, or from CHARPOS if that is > 0. Set
4114 IT->n_overlays to the total number of overlay strings found.
5f5c8ee5
GM
4115
4116 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4117 a time. On entry into load_overlay_strings,
4118 IT->current.overlay_string_index gives the number of overlay
4119 strings that have already been loaded by previous calls to this
4120 function.
4121
2970b9be
GM
4122 IT->add_overlay_start contains an additional overlay start
4123 position to consider for taking overlay strings from, if non-zero.
4124 This position comes into play when the overlay has an `invisible'
4125 property, and both before and after-strings. When we've skipped to
4126 the end of the overlay, because of its `invisible' property, we
4127 nevertheless want its before-string to appear.
4128 IT->add_overlay_start will contain the overlay start position
4129 in this case.
4130
5f5c8ee5
GM
4131 Overlay strings are sorted so that after-string strings come in
4132 front of before-string strings. Within before and after-strings,
4133 strings are sorted by overlay priority. See also function
4134 compare_overlay_entries. */
2311178e 4135
5f5c8ee5 4136static void
5a08cbaf 4137load_overlay_strings (it, charpos)
5f5c8ee5 4138 struct it *it;
5a08cbaf 4139 int charpos;
5f5c8ee5
GM
4140{
4141 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
b7253a3e
SM
4142 Lisp_Object overlay, window, str, invisible;
4143 struct Lisp_Overlay *ov;
5f5c8ee5
GM
4144 int start, end;
4145 int size = 20;
cafafe0b 4146 int n = 0, i, j, invis_p;
5f5c8ee5
GM
4147 struct overlay_entry *entries
4148 = (struct overlay_entry *) alloca (size * sizeof *entries);
5a08cbaf
GM
4149
4150 if (charpos <= 0)
4151 charpos = IT_CHARPOS (*it);
5f5c8ee5
GM
4152
4153 /* Append the overlay string STRING of overlay OVERLAY to vector
4154 `entries' which has size `size' and currently contains `n'
4155 elements. AFTER_P non-zero means STRING is an after-string of
4156 OVERLAY. */
4157#define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4158 do \
4159 { \
4160 Lisp_Object priority; \
4161 \
4162 if (n == size) \
4163 { \
4164 int new_size = 2 * size; \
4165 struct overlay_entry *old = entries; \
4166 entries = \
4167 (struct overlay_entry *) alloca (new_size \
4168 * sizeof *entries); \
4169 bcopy (old, entries, size * sizeof *entries); \
4170 size = new_size; \
4171 } \
4172 \
4173 entries[n].string = (STRING); \
2970b9be 4174 entries[n].overlay = (OVERLAY); \
5f5c8ee5 4175 priority = Foverlay_get ((OVERLAY), Qpriority); \
2970b9be 4176 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5f5c8ee5
GM
4177 entries[n].after_string_p = (AFTER_P); \
4178 ++n; \
4179 } \
4180 while (0)
4181
4182 /* Process overlay before the overlay center. */
b7253a3e 4183 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5f5c8ee5 4184 {
b7253a3e 4185 XSETMISC (overlay, ov);
5f5c8ee5
GM
4186 xassert (OVERLAYP (overlay));
4187 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4188 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2311178e 4189
cafafe0b 4190 if (end < charpos)
5f5c8ee5
GM
4191 break;
4192
4193 /* Skip this overlay if it doesn't start or end at IT's current
4194 position. */
cafafe0b 4195 if (end != charpos && start != charpos)
5f5c8ee5 4196 continue;
2311178e 4197
5f5c8ee5
GM
4198 /* Skip this overlay if it doesn't apply to IT->w. */
4199 window = Foverlay_get (overlay, Qwindow);
4200 if (WINDOWP (window) && XWINDOW (window) != it->w)
4201 continue;
4202
cafafe0b
GM
4203 /* If the text ``under'' the overlay is invisible, both before-
4204 and after-strings from this overlay are visible; start and
4205 end position are indistinguishable. */
4206 invisible = Foverlay_get (overlay, Qinvisible);
4207 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4208
5f5c8ee5 4209 /* If overlay has a non-empty before-string, record it. */
cafafe0b 4210 if ((start == charpos || (end == charpos && invis_p))
5f5c8ee5 4211 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2051c264 4212 && SCHARS (str))
5f5c8ee5 4213 RECORD_OVERLAY_STRING (overlay, str, 0);
2311178e 4214
5f5c8ee5 4215 /* If overlay has a non-empty after-string, record it. */
cafafe0b 4216 if ((end == charpos || (start == charpos && invis_p))
5f5c8ee5 4217 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2051c264 4218 && SCHARS (str))
5f5c8ee5
GM
4219 RECORD_OVERLAY_STRING (overlay, str, 1);
4220 }
2311178e 4221
5f5c8ee5 4222 /* Process overlays after the overlay center. */
b7253a3e 4223 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5f5c8ee5 4224 {
b7253a3e 4225 XSETMISC (overlay, ov);
5f5c8ee5
GM
4226 xassert (OVERLAYP (overlay));
4227 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4228 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4229
cafafe0b 4230 if (start > charpos)
5f5c8ee5 4231 break;
2311178e 4232
5f5c8ee5
GM
4233 /* Skip this overlay if it doesn't start or end at IT's current
4234 position. */
cafafe0b 4235 if (end != charpos && start != charpos)
5f5c8ee5
GM
4236 continue;
4237
4238 /* Skip this overlay if it doesn't apply to IT->w. */
4239 window = Foverlay_get (overlay, Qwindow);
4240 if (WINDOWP (window) && XWINDOW (window) != it->w)
4241 continue;
2311178e 4242
cafafe0b
GM
4243 /* If the text ``under'' the overlay is invisible, it has a zero
4244 dimension, and both before- and after-strings apply. */
4245 invisible = Foverlay_get (overlay, Qinvisible);
4246 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4247
5f5c8ee5 4248 /* If overlay has a non-empty before-string, record it. */
cafafe0b 4249 if ((start == charpos || (end == charpos && invis_p))
5f5c8ee5 4250 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2051c264 4251 && SCHARS (str))
5f5c8ee5 4252 RECORD_OVERLAY_STRING (overlay, str, 0);
2311178e 4253
5f5c8ee5 4254 /* If overlay has a non-empty after-string, record it. */
cafafe0b 4255 if ((end == charpos || (start == charpos && invis_p))
5f5c8ee5 4256 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2051c264 4257 && SCHARS (str))
5f5c8ee5
GM
4258 RECORD_OVERLAY_STRING (overlay, str, 1);
4259 }
4260
4261#undef RECORD_OVERLAY_STRING
2311178e 4262
5f5c8ee5 4263 /* Sort entries. */
cafafe0b 4264 if (n > 1)
2970b9be 4265 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5f5c8ee5
GM
4266
4267 /* Record the total number of strings to process. */
4268 it->n_overlay_strings = n;
4269
4270 /* IT->current.overlay_string_index is the number of overlay strings
4271 that have already been consumed by IT. Copy some of the
4272 remaining overlay strings to IT->overlay_strings. */
4273 i = 0;
4274 j = it->current.overlay_string_index;
4275 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4276 it->overlay_strings[i++] = entries[j++].string;
2970b9be 4277
5f5c8ee5
GM
4278 CHECK_IT (it);
4279}
4280
4281
4282/* Get the first chunk of overlay strings at IT's current buffer
5a08cbaf
GM
4283 position, or at CHARPOS if that is > 0. Value is non-zero if at
4284 least one overlay string was found. */
5f5c8ee5
GM
4285
4286static int
5a08cbaf 4287get_overlay_strings (it, charpos)
5f5c8ee5 4288 struct it *it;
5a08cbaf 4289 int charpos;
5f5c8ee5
GM
4290{
4291 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4292 process. This fills IT->overlay_strings with strings, and sets
4293 IT->n_overlay_strings to the total number of strings to process.
4294 IT->pos.overlay_string_index has to be set temporarily to zero
4295 because load_overlay_strings needs this; it must be set to -1
4296 when no overlay strings are found because a zero value would
4297 indicate a position in the first overlay string. */
4298 it->current.overlay_string_index = 0;
5a08cbaf 4299 load_overlay_strings (it, charpos);
5f5c8ee5
GM
4300
4301 /* If we found overlay strings, set up IT to deliver display
4302 elements from the first one. Otherwise set up IT to deliver
4303 from current_buffer. */
4304 if (it->n_overlay_strings)
4305 {
4306 /* Make sure we know settings in current_buffer, so that we can
4307 restore meaningful values when we're done with the overlay
4308 strings. */
4309 compute_stop_pos (it);
4310 xassert (it->face_id >= 0);
2311178e 4311
5f5c8ee5
GM
4312 /* Save IT's settings. They are restored after all overlay
4313 strings have been processed. */
4314 xassert (it->sp == 0);
4315 push_it (it);
4316
4317 /* Set up IT to deliver display elements from the first overlay
4318 string. */
4319 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5f5c8ee5 4320 it->string = it->overlay_strings[0];
b2046df8 4321 it->stop_charpos = 0;
5f5c8ee5 4322 xassert (STRINGP (it->string));
2051c264 4323 it->end_charpos = SCHARS (it->string);
d5db4077 4324 it->multibyte_p = STRING_MULTIBYTE (it->string);
5f5c8ee5
GM
4325 it->method = next_element_from_string;
4326 }
4327 else
4328 {
4329 it->string = Qnil;
4330 it->current.overlay_string_index = -1;
4331 it->method = next_element_from_buffer;
4332 }
4333
4334 CHECK_IT (it);
4335
4336 /* Value is non-zero if we found at least one overlay string. */
4337 return STRINGP (it->string);
4338}
4339
4340
4341\f
4342/***********************************************************************
4343 Saving and restoring state
4344 ***********************************************************************/
4345
4346/* Save current settings of IT on IT->stack. Called, for example,
4347 before setting up IT for an overlay string, to be able to restore
4348 IT's settings to what they were after the overlay string has been
4349 processed. */
4350
4351static void
4352push_it (it)
4353 struct it *it;
4354{
4355 struct iterator_stack_entry *p;
2311178e 4356
5f5c8ee5
GM
4357 xassert (it->sp < 2);
4358 p = it->stack + it->sp;
4359
4360 p->stop_charpos = it->stop_charpos;
4361 xassert (it->face_id >= 0);
4362 p->face_id = it->face_id;
4363 p->string = it->string;
4364 p->pos = it->current;
4365 p->end_charpos = it->end_charpos;
4366 p->string_nchars = it->string_nchars;
4367 p->area = it->area;
4368 p->multibyte_p = it->multibyte_p;
e893970b 4369 p->slice = it->slice;
5f5c8ee5
GM
4370 p->space_width = it->space_width;
4371 p->font_height = it->font_height;
4372 p->voffset = it->voffset;
4373 p->string_from_display_prop_p = it->string_from_display_prop_p;
5a08cbaf 4374 p->display_ellipsis_p = 0;
5f5c8ee5
GM
4375 ++it->sp;
4376}
4377
4378
4379/* Restore IT's settings from IT->stack. Called, for example, when no
4380 more overlay strings must be processed, and we return to delivering
4381 display elements from a buffer, or when the end of a string from a
4382 `display' property is reached and we return to delivering display
4383 elements from an overlay string, or from a buffer. */
4384
4385static void
4386pop_it (it)
4387 struct it *it;
4388{
4389 struct iterator_stack_entry *p;
2311178e 4390
5f5c8ee5
GM
4391 xassert (it->sp > 0);
4392 --it->sp;
4393 p = it->stack + it->sp;
4394 it->stop_charpos = p->stop_charpos;
4395 it->face_id = p->face_id;
4396 it->string = p->string;
4397 it->current = p->pos;
4398 it->end_charpos = p->end_charpos;
4399 it->string_nchars = p->string_nchars;
4400 it->area = p->area;
4401 it->multibyte_p = p->multibyte_p;
e893970b 4402 it->slice = p->slice;
5f5c8ee5
GM
4403 it->space_width = p->space_width;
4404 it->font_height = p->font_height;
4405 it->voffset = p->voffset;
4406 it->string_from_display_prop_p = p->string_from_display_prop_p;
4407}
4408
4409
4410\f
4411/***********************************************************************
4412 Moving over lines
4413 ***********************************************************************/
4414
4415/* Set IT's current position to the previous line start. */
4416
4417static void
4418back_to_previous_line_start (it)
4419 struct it *it;
4420{
4421 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4422 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4423}
4424
4425
cafafe0b 4426/* Move IT to the next line start.
2311178e 4427
cafafe0b
GM
4428 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4429 we skipped over part of the text (as opposed to moving the iterator
4430 continuously over the text). Otherwise, don't change the value
4431 of *SKIPPED_P.
2311178e 4432
cafafe0b
GM
4433 Newlines may come from buffer text, overlay strings, or strings
4434 displayed via the `display' property. That's the reason we can't
0fd37545
GM
4435 simply use find_next_newline_no_quit.
4436
4437 Note that this function may not skip over invisible text that is so
4438 because of text properties and immediately follows a newline. If
4439 it would, function reseat_at_next_visible_line_start, when called
4440 from set_iterator_to_next, would effectively make invisible
4441 characters following a newline part of the wrong glyph row, which
4442 leads to wrong cursor motion. */
5f5c8ee5 4443
cafafe0b
GM
4444static int
4445forward_to_next_line_start (it, skipped_p)
5f5c8ee5 4446 struct it *it;
cafafe0b 4447 int *skipped_p;
5f5c8ee5 4448{
54918e2b 4449 int old_selective, newline_found_p, n;
cafafe0b
GM
4450 const int MAX_NEWLINE_DISTANCE = 500;
4451
0fd37545
GM
4452 /* If already on a newline, just consume it to avoid unintended
4453 skipping over invisible text below. */
51695746
GM
4454 if (it->what == IT_CHARACTER
4455 && it->c == '\n'
4456 && CHARPOS (it->position) == IT_CHARPOS (*it))
0fd37545
GM
4457 {
4458 set_iterator_to_next (it, 0);
a77dc1ec 4459 it->c = 0;
0fd37545
GM
4460 return 1;
4461 }
4462
54918e2b 4463 /* Don't handle selective display in the following. It's (a)
0fd37545
GM
4464 unnecessary because it's done by the caller, and (b) leads to an
4465 infinite recursion because next_element_from_ellipsis indirectly
4466 calls this function. */
54918e2b
GM
4467 old_selective = it->selective;
4468 it->selective = 0;
4469
cafafe0b
GM
4470 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4471 from buffer text. */
3aec8722
GM
4472 for (n = newline_found_p = 0;
4473 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4474 n += STRINGP (it->string) ? 0 : 1)
cafafe0b 4475 {
8692ca92 4476 if (!get_next_display_element (it))
d43be70c 4477 return 0;
d02f1cb8 4478 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
cafafe0b 4479 set_iterator_to_next (it, 0);
cafafe0b
GM
4480 }
4481
4482 /* If we didn't find a newline near enough, see if we can use a
4483 short-cut. */
db0bb807 4484 if (!newline_found_p)
cafafe0b
GM
4485 {
4486 int start = IT_CHARPOS (*it);
4487 int limit = find_next_newline_no_quit (start, 1);
4488 Lisp_Object pos;
4489
4490 xassert (!STRINGP (it->string));
4491
4492 /* If there isn't any `display' property in sight, and no
4493 overlays, we can just use the position of the newline in
4494 buffer text. */
4495 if (it->stop_charpos >= limit
4496 || ((pos = Fnext_single_property_change (make_number (start),
4497 Qdisplay,
4498 Qnil, make_number (limit)),
4499 NILP (pos))
4500 && next_overlay_change (start) == ZV))
4501 {
4502 IT_CHARPOS (*it) = limit;
4503 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4504 *skipped_p = newline_found_p = 1;
4505 }
4506 else
4507 {
4508 while (get_next_display_element (it)
4509 && !newline_found_p)
4510 {
4511 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4512 set_iterator_to_next (it, 0);
4513 }
4514 }
4515 }
4516
54918e2b 4517 it->selective = old_selective;
cafafe0b 4518 return newline_found_p;
5f5c8ee5
GM
4519}
4520
4521
4522/* Set IT's current position to the previous visible line start. Skip
4523 invisible text that is so either due to text properties or due to
4524 selective display. Caution: this does not change IT->current_x and
4525 IT->hpos. */
4526
4527static void
4528back_to_previous_visible_line_start (it)
4529 struct it *it;
4530{
4531 int visible_p = 0;
4532
4533 /* Go back one newline if not on BEGV already. */
4534 if (IT_CHARPOS (*it) > BEGV)
4535 back_to_previous_line_start (it);
4536
4537 /* Move over lines that are invisible because of selective display
4538 or text properties. */
4539 while (IT_CHARPOS (*it) > BEGV
4540 && !visible_p)
4541 {
4542 visible_p = 1;
4543
4544 /* If selective > 0, then lines indented more than that values
4545 are invisible. */
4546 if (it->selective > 0
4547 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
a67e162b 4548 (double) it->selective)) /* iftc */
5f5c8ee5 4549 visible_p = 0;
2311178e 4550 else
5f5c8ee5
GM
4551 {
4552 Lisp_Object prop;
4553
6fc556fd
KR
4554 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
4555 Qinvisible, it->window);
5f5c8ee5
GM
4556 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4557 visible_p = 0;
4558 }
5f5c8ee5 4559
6c0afe12
KS
4560 if (visible_p)
4561 {
4562 struct it it2 = *it;
4563
4564 if (handle_display_prop (&it2) == HANDLED_RETURN)
4565 visible_p = 0;
4566 }
4567
5f5c8ee5
GM
4568 /* Back one more newline if the current one is invisible. */
4569 if (!visible_p)
4570 back_to_previous_line_start (it);
4571 }
4572
4573 xassert (IT_CHARPOS (*it) >= BEGV);
4574 xassert (IT_CHARPOS (*it) == BEGV
4575 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4576 CHECK_IT (it);
4577}
4578
4579
4580/* Reseat iterator IT at the previous visible line start. Skip
4581 invisible text that is so either due to text properties or due to
4582 selective display. At the end, update IT's overlay information,
4583 face information etc. */
4584
4585static void
4586reseat_at_previous_visible_line_start (it)
4587 struct it *it;
4588{
4589 back_to_previous_visible_line_start (it);
4590 reseat (it, it->current.pos, 1);
4591 CHECK_IT (it);
4592}
4593
4594
4595/* Reseat iterator IT on the next visible line start in the current
312246d1
GM
4596 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4597 preceding the line start. Skip over invisible text that is so
4598 because of selective display. Compute faces, overlays etc at the
4599 new position. Note that this function does not skip over text that
4600 is invisible because of text properties. */
5f5c8ee5
GM
4601
4602static void
312246d1 4603reseat_at_next_visible_line_start (it, on_newline_p)
5f5c8ee5 4604 struct it *it;
312246d1 4605 int on_newline_p;
5f5c8ee5 4606{
cafafe0b
GM
4607 int newline_found_p, skipped_p = 0;
4608
4609 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4610
4611 /* Skip over lines that are invisible because they are indented
4612 more than the value of IT->selective. */
4613 if (it->selective > 0)
4614 while (IT_CHARPOS (*it) < ZV
a77dc1ec 4615 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
a67e162b 4616 (double) it->selective)) /* iftc */
a77dc1ec
GM
4617 {
4618 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4619 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4620 }
cafafe0b
GM
4621
4622 /* Position on the newline if that's what's requested. */
4623 if (on_newline_p && newline_found_p)
5f5c8ee5 4624 {
cafafe0b 4625 if (STRINGP (it->string))
5f5c8ee5 4626 {
cafafe0b
GM
4627 if (IT_STRING_CHARPOS (*it) > 0)
4628 {
4629 --IT_STRING_CHARPOS (*it);
4630 --IT_STRING_BYTEPOS (*it);
4631 }
5f5c8ee5 4632 }
cafafe0b 4633 else if (IT_CHARPOS (*it) > BEGV)
312246d1
GM
4634 {
4635 --IT_CHARPOS (*it);
cafafe0b
GM
4636 --IT_BYTEPOS (*it);
4637 reseat (it, it->current.pos, 0);
312246d1 4638 }
5f5c8ee5 4639 }
cafafe0b
GM
4640 else if (skipped_p)
4641 reseat (it, it->current.pos, 0);
2311178e 4642
5f5c8ee5
GM
4643 CHECK_IT (it);
4644}
4645
4646
4647\f
4648/***********************************************************************
4649 Changing an iterator's position
4650***********************************************************************/
4651
4652/* Change IT's current position to POS in current_buffer. If FORCE_P
4653 is non-zero, always check for text properties at the new position.
4654 Otherwise, text properties are only looked up if POS >=
4655 IT->check_charpos of a property. */
4656
4657static void
4658reseat (it, pos, force_p)
4659 struct it *it;
4660 struct text_pos pos;
4661 int force_p;
4662{
4663 int original_pos = IT_CHARPOS (*it);
4664
4665 reseat_1 (it, pos, 0);
4666
4667 /* Determine where to check text properties. Avoid doing it
4668 where possible because text property lookup is very expensive. */
4669 if (force_p
4670 || CHARPOS (pos) > it->stop_charpos
4671 || CHARPOS (pos) < original_pos)
4672 handle_stop (it);
4673
4674 CHECK_IT (it);
4675}
4676
4677
4678/* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4679 IT->stop_pos to POS, also. */
4680
4681static void
4682reseat_1 (it, pos, set_stop_p)
4683 struct it *it;
4684 struct text_pos pos;
4685 int set_stop_p;
4686{
4687 /* Don't call this function when scanning a C string. */
4688 xassert (it->s == NULL);
4689
4690 /* POS must be a reasonable value. */
4691 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4692
4693 it->current.pos = it->position = pos;
4694 XSETBUFFER (it->object, current_buffer);
78c663d8 4695 it->end_charpos = ZV;
5f5c8ee5
GM
4696 it->dpvec = NULL;
4697 it->current.dpvec_index = -1;
4698 it->current.overlay_string_index = -1;
4699 IT_STRING_CHARPOS (*it) = -1;
4700 IT_STRING_BYTEPOS (*it) = -1;
4701 it->string = Qnil;
4702 it->method = next_element_from_buffer;
5905025c
RS
4703 /* RMS: I added this to fix a bug in move_it_vertically_backward
4704 where it->area continued to relate to the starting point
4705 for the backward motion. Bug report from
4706 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4707 However, I am not sure whether reseat still does the right thing
4708 in general after this change. */
4709 it->area = TEXT_AREA;
06fd3792 4710 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5f5c8ee5 4711 it->sp = 0;
4aad61f8 4712 it->face_before_selective_p = 0;
5f5c8ee5
GM
4713
4714 if (set_stop_p)
4715 it->stop_charpos = CHARPOS (pos);
4716}
4717
4718
4719/* Set up IT for displaying a string, starting at CHARPOS in window W.
4720 If S is non-null, it is a C string to iterate over. Otherwise,
4721 STRING gives a Lisp string to iterate over.
2311178e 4722
5f5c8ee5
GM
4723 If PRECISION > 0, don't return more then PRECISION number of
4724 characters from the string.
4725
4726 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4727 characters have been returned. FIELD_WIDTH < 0 means an infinite
4728 field width.
4729
4730 MULTIBYTE = 0 means disable processing of multibyte characters,
4731 MULTIBYTE > 0 means enable it,
4732 MULTIBYTE < 0 means use IT->multibyte_p.
4733
4734 IT must be initialized via a prior call to init_iterator before
4735 calling this function. */
4736
4737static void
4738reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4739 struct it *it;
4740 unsigned char *s;
4741 Lisp_Object string;
4742 int charpos;
4743 int precision, field_width, multibyte;
4744{
4745 /* No region in strings. */
4746 it->region_beg_charpos = it->region_end_charpos = -1;
4747
4748 /* No text property checks performed by default, but see below. */
4749 it->stop_charpos = -1;
4750
4751 /* Set iterator position and end position. */
4752 bzero (&it->current, sizeof it->current);
4753 it->current.overlay_string_index = -1;
4754 it->current.dpvec_index = -1;
5f5c8ee5 4755 xassert (charpos >= 0);
2311178e 4756
e719f5ae
GM
4757 /* If STRING is specified, use its multibyteness, otherwise use the
4758 setting of MULTIBYTE, if specified. */
cabf45da 4759 if (multibyte >= 0)
5f5c8ee5 4760 it->multibyte_p = multibyte > 0;
2311178e 4761
5f5c8ee5
GM
4762 if (s == NULL)
4763 {
4764 xassert (STRINGP (string));
4765 it->string = string;
4766 it->s = NULL;
2051c264 4767 it->end_charpos = it->string_nchars = SCHARS (string);
5f5c8ee5
GM
4768 it->method = next_element_from_string;
4769 it->current.string_pos = string_pos (charpos, string);
4770 }
4771 else
4772 {
4773 it->s = s;
4774 it->string = Qnil;
4775
4776 /* Note that we use IT->current.pos, not it->current.string_pos,
4777 for displaying C strings. */
4778 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4779 if (it->multibyte_p)
4780 {
4781 it->current.pos = c_string_pos (charpos, s, 1);
4782 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4783 }
4784 else
4785 {
4786 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4787 it->end_charpos = it->string_nchars = strlen (s);
4788 }
2311178e 4789
5f5c8ee5
GM
4790 it->method = next_element_from_c_string;
4791 }
4792
4793 /* PRECISION > 0 means don't return more than PRECISION characters
4794 from the string. */
4795 if (precision > 0 && it->end_charpos - charpos > precision)
4796 it->end_charpos = it->string_nchars = charpos + precision;
4797
4798 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4799 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4800 FIELD_WIDTH < 0 means infinite field width. This is useful for
4801 padding with `-' at the end of a mode line. */
4802 if (field_width < 0)
4803 field_width = INFINITY;
4804 if (field_width > it->end_charpos - charpos)
4805 it->end_charpos = charpos + field_width;
4806
4807 /* Use the standard display table for displaying strings. */
4808 if (DISP_TABLE_P (Vstandard_display_table))
4809 it->dp = XCHAR_TABLE (Vstandard_display_table);
4810
4811 it->stop_charpos = charpos;
4812 CHECK_IT (it);
4813}
4814
4815
4816\f
4817/***********************************************************************
4818 Iteration
4819 ***********************************************************************/
4820
4821/* Load IT's display element fields with information about the next
4822 display element from the current position of IT. Value is zero if
4823 end of buffer (or C string) is reached. */
4824
4825int
4826get_next_display_element (it)
4827 struct it *it;
4828{
7d0393cf 4829 /* Non-zero means that we found a display element. Zero means that
5f5c8ee5
GM
4830 we hit the end of what we iterate over. Performance note: the
4831 function pointer `method' used here turns out to be faster than
4832 using a sequence of if-statements. */
4833 int success_p = (*it->method) (it);
5f5c8ee5
GM
4834
4835 if (it->what == IT_CHARACTER)
4836 {
4837 /* Map via display table or translate control characters.
4838 IT->c, IT->len etc. have been set to the next character by
4839 the function call above. If we have a display table, and it
4840 contains an entry for IT->c, translate it. Don't do this if
4841 IT->c itself comes from a display table, otherwise we could
4842 end up in an infinite recursion. (An alternative could be to
4843 count the recursion depth of this function and signal an
4844 error when a certain maximum depth is reached.) Is it worth
4845 it? */
4846 if (success_p && it->dpvec == NULL)
4847 {
4848 Lisp_Object dv;
4849
4850 if (it->dp
4851 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4852 VECTORP (dv)))
4853 {
4854 struct Lisp_Vector *v = XVECTOR (dv);
4855
4856 /* Return the first character from the display table
4857 entry, if not empty. If empty, don't display the
4858 current character. */
4859 if (v->size)
4860 {
4861 it->dpvec_char_len = it->len;
4862 it->dpvec = v->contents;
4863 it->dpend = v->contents + v->size;
4864 it->current.dpvec_index = 0;
4865 it->method = next_element_from_display_vector;
7eee47cc
GM
4866 success_p = get_next_display_element (it);
4867 }
4868 else
4869 {
4870 set_iterator_to_next (it, 0);
4871 success_p = get_next_display_element (it);
5f5c8ee5 4872 }
5f5c8ee5
GM
4873 }
4874
4875 /* Translate control characters into `\003' or `^C' form.
4876 Control characters coming from a display table entry are
4877 currently not translated because we use IT->dpvec to hold
4878 the translation. This could easily be changed but I
197516c2
KH
4879 don't believe that it is worth doing.
4880
fa831cf8
KH
4881 If it->multibyte_p is nonzero, eight-bit characters and
4882 non-printable multibyte characters are also translated to
4883 octal form.
4884
4885 If it->multibyte_p is zero, eight-bit characters that
4886 don't have corresponding multibyte char code are also
4887 translated to octal form. */
ba197fe6 4888 else if ((it->c < ' '
5f5c8ee5 4889 && (it->area != TEXT_AREA
c6e89d6c 4890 || (it->c != '\n' && it->c != '\t')))
fa831cf8
KH
4891 || (it->multibyte_p
4892 ? ((it->c >= 127
4893 && it->len == 1)
4894 || !CHAR_PRINTABLE_P (it->c))
ba197fe6 4895 : (it->c >= 127
fa831cf8 4896 && it->c == unibyte_char_to_multibyte (it->c))))
5f5c8ee5
GM
4897 {
4898 /* IT->c is a control character which must be displayed
4899 either as '\003' or as `^C' where the '\\' and '^'
4900 can be defined in the display table. Fill
4901 IT->ctl_chars with glyphs for what we have to
4902 display. Then, set IT->dpvec to these glyphs. */
4903 GLYPH g;
4904
54c85a23 4905 if (it->c < 128 && it->ctl_arrow_p)
5f5c8ee5
GM
4906 {
4907 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4908 if (it->dp
4909 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4910 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4911 g = XINT (DISP_CTRL_GLYPH (it->dp));
4912 else
4913 g = FAST_MAKE_GLYPH ('^', 0);
4914 XSETINT (it->ctl_chars[0], g);
4915
4916 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4917 XSETINT (it->ctl_chars[1], g);
4918
4919 /* Set up IT->dpvec and return first character from it. */
4920 it->dpvec_char_len = it->len;
4921 it->dpvec = it->ctl_chars;
4922 it->dpend = it->dpvec + 2;
4923 it->current.dpvec_index = 0;
4924 it->method = next_element_from_display_vector;
4925 get_next_display_element (it);
4926 }
4927 else
4928 {
260a86a0 4929 unsigned char str[MAX_MULTIBYTE_LENGTH];
c5924f47 4930 int len;
197516c2
KH
4931 int i;
4932 GLYPH escape_glyph;
4933
5f5c8ee5
GM
4934 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4935 if (it->dp
4936 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4937 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
197516c2 4938 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5f5c8ee5 4939 else
197516c2
KH
4940 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4941
c5924f47
KH
4942 if (SINGLE_BYTE_CHAR_P (it->c))
4943 str[0] = it->c, len = 1;
4944 else
ea9dd091
GM
4945 {
4946 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4947 if (len < 0)
4948 {
4949 /* It's an invalid character, which
4950 shouldn't happen actually, but due to
4951 bugs it may happen. Let's print the char
4952 as is, there's not much meaningful we can
4953 do with it. */
4954 str[0] = it->c;
4955 str[1] = it->c >> 8;
4956 str[2] = it->c >> 16;
4957 str[3] = it->c >> 24;
4958 len = 4;
4959 }
4960 }
c5924f47 4961
197516c2
KH
4962 for (i = 0; i < len; i++)
4963 {
4964 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4965 /* Insert three more glyphs into IT->ctl_chars for
4966 the octal display of the character. */
2311178e 4967 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
197516c2 4968 XSETINT (it->ctl_chars[i * 4 + 1], g);
2311178e 4969 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
197516c2 4970 XSETINT (it->ctl_chars[i * 4 + 2], g);
2311178e 4971 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
197516c2
KH
4972 XSETINT (it->ctl_chars[i * 4 + 3], g);
4973 }
5f5c8ee5
GM
4974
4975 /* Set up IT->dpvec and return the first character
4976 from it. */
4977 it->dpvec_char_len = it->len;
4978 it->dpvec = it->ctl_chars;
197516c2 4979 it->dpend = it->dpvec + len * 4;
5f5c8ee5
GM
4980 it->current.dpvec_index = 0;
4981 it->method = next_element_from_display_vector;
4982 get_next_display_element (it);
4983 }
4984 }
4985 }
4986
980806b6
KH
4987 /* Adjust face id for a multibyte character. There are no
4988 multibyte character in unibyte text. */
5f5c8ee5
GM
4989 if (it->multibyte_p
4990 && success_p
980806b6 4991 && FRAME_WINDOW_P (it->f))
5f5c8ee5 4992 {
980806b6
KH
4993 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4994 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5f5c8ee5
GM
4995 }
4996 }
4997
4998 /* Is this character the last one of a run of characters with
4999 box? If yes, set IT->end_of_box_run_p to 1. */
5000 if (it->face_box_p
5001 && it->s == NULL)
5002 {
5003 int face_id;
5004 struct face *face;
5005
5006 it->end_of_box_run_p
5007 = ((face_id = face_after_it_pos (it),
5008 face_id != it->face_id)
5009 && (face = FACE_FROM_ID (it->f, face_id),
5010 face->box == FACE_NO_BOX));
5011 }
5012
5013 /* Value is 0 if end of buffer or string reached. */
5014 return success_p;
5015}
5016
5017
5018/* Move IT to the next display element.
5019
cafafe0b
GM
5020 RESEAT_P non-zero means if called on a newline in buffer text,
5021 skip to the next visible line start.
5022
5f5c8ee5
GM
5023 Functions get_next_display_element and set_iterator_to_next are
5024 separate because I find this arrangement easier to handle than a
5025 get_next_display_element function that also increments IT's
5026 position. The way it is we can first look at an iterator's current
5027 display element, decide whether it fits on a line, and if it does,
5028 increment the iterator position. The other way around we probably
5029 would either need a flag indicating whether the iterator has to be
5030 incremented the next time, or we would have to implement a
5031 decrement position function which would not be easy to write. */
5032
5033void
cafafe0b 5034set_iterator_to_next (it, reseat_p)
5f5c8ee5 5035 struct it *it;
cafafe0b 5036 int reseat_p;
5f5c8ee5 5037{
483de32b
GM
5038 /* Reset flags indicating start and end of a sequence of characters
5039 with box. Reset them at the start of this function because
5040 moving the iterator to a new position might set them. */
5041 it->start_of_box_run_p = it->end_of_box_run_p = 0;
2311178e 5042
5f5c8ee5
GM
5043 if (it->method == next_element_from_buffer)
5044 {
5045 /* The current display element of IT is a character from
5046 current_buffer. Advance in the buffer, and maybe skip over
5047 invisible lines that are so because of selective display. */
cafafe0b 5048 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
312246d1 5049 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
5050 else
5051 {
5052 xassert (it->len != 0);
5053 IT_BYTEPOS (*it) += it->len;
5054 IT_CHARPOS (*it) += 1;
5055 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5056 }
5057 }
260a86a0
KH
5058 else if (it->method == next_element_from_composition)
5059 {
5060 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
5061 if (STRINGP (it->string))
5062 {
5063 IT_STRING_BYTEPOS (*it) += it->len;
5064 IT_STRING_CHARPOS (*it) += it->cmp_len;
5065 it->method = next_element_from_string;
5066 goto consider_string_end;
5067 }
5068 else
5069 {
5070 IT_BYTEPOS (*it) += it->len;
5071 IT_CHARPOS (*it) += it->cmp_len;
5072 it->method = next_element_from_buffer;
5073 }
5074 }
5f5c8ee5
GM
5075 else if (it->method == next_element_from_c_string)
5076 {
5077 /* Current display element of IT is from a C string. */
5078 IT_BYTEPOS (*it) += it->len;
5079 IT_CHARPOS (*it) += 1;
5080 }
5081 else if (it->method == next_element_from_display_vector)
5082 {
5083 /* Current display element of IT is from a display table entry.
5084 Advance in the display table definition. Reset it to null if
5085 end reached, and continue with characters from buffers/
5086 strings. */
5087 ++it->current.dpvec_index;
286bcbc9 5088
980806b6
KH
5089 /* Restore face of the iterator to what they were before the
5090 display vector entry (these entries may contain faces). */
5f5c8ee5 5091 it->face_id = it->saved_face_id;
2311178e 5092
5f5c8ee5
GM
5093 if (it->dpvec + it->current.dpvec_index == it->dpend)
5094 {
5095 if (it->s)
5096 it->method = next_element_from_c_string;
5097 else if (STRINGP (it->string))
5098 it->method = next_element_from_string;
5099 else
5100 it->method = next_element_from_buffer;
5101
5102 it->dpvec = NULL;
5103 it->current.dpvec_index = -1;
5104
312246d1
GM
5105 /* Skip over characters which were displayed via IT->dpvec. */
5106 if (it->dpvec_char_len < 0)
5107 reseat_at_next_visible_line_start (it, 1);
5108 else if (it->dpvec_char_len > 0)
5f5c8ee5
GM
5109 {
5110 it->len = it->dpvec_char_len;
cafafe0b 5111 set_iterator_to_next (it, reseat_p);
5f5c8ee5
GM
5112 }
5113 }
5114 }
5115 else if (it->method == next_element_from_string)
5116 {
5117 /* Current display element is a character from a Lisp string. */
5118 xassert (it->s == NULL && STRINGP (it->string));
5119 IT_STRING_BYTEPOS (*it) += it->len;
5120 IT_STRING_CHARPOS (*it) += 1;
2311178e 5121
5f5c8ee5
GM
5122 consider_string_end:
5123
5124 if (it->current.overlay_string_index >= 0)
5125 {
5126 /* IT->string is an overlay string. Advance to the
5127 next, if there is one. */
2051c264 5128 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5f5c8ee5
GM
5129 next_overlay_string (it);
5130 }
5131 else
5132 {
5133 /* IT->string is not an overlay string. If we reached
5134 its end, and there is something on IT->stack, proceed
5135 with what is on the stack. This can be either another
5136 string, this time an overlay string, or a buffer. */
2051c264 5137 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5f5c8ee5
GM
5138 && it->sp > 0)
5139 {
5140 pop_it (it);
5141 if (!STRINGP (it->string))
5142 it->method = next_element_from_buffer;
b2046df8
GM
5143 else
5144 goto consider_string_end;
5f5c8ee5
GM
5145 }
5146 }
5147 }
5148 else if (it->method == next_element_from_image
5149 || it->method == next_element_from_stretch)
5150 {
5151 /* The position etc with which we have to proceed are on
5152 the stack. The position may be at the end of a string,
5153 if the `display' property takes up the whole string. */
5154 pop_it (it);
5155 it->image_id = 0;
5156 if (STRINGP (it->string))
5157 {
5158 it->method = next_element_from_string;
5159 goto consider_string_end;
5160 }
5161 else
5162 it->method = next_element_from_buffer;
5163 }
5164 else
5165 /* There are no other methods defined, so this should be a bug. */
5166 abort ();
5167
5f5c8ee5
GM
5168 xassert (it->method != next_element_from_string
5169 || (STRINGP (it->string)
5170 && IT_STRING_CHARPOS (*it) >= 0));
5171}
5172
5173
5174/* Load IT's display element fields with information about the next
5175 display element which comes from a display table entry or from the
5176 result of translating a control character to one of the forms `^C'
5177 or `\003'. IT->dpvec holds the glyphs to return as characters. */
5178
5179static int
5180next_element_from_display_vector (it)
5181 struct it *it;
5182{
5183 /* Precondition. */
5184 xassert (it->dpvec && it->current.dpvec_index >= 0);
5185
5186 /* Remember the current face id in case glyphs specify faces.
5187 IT's face is restored in set_iterator_to_next. */
5188 it->saved_face_id = it->face_id;
2311178e 5189
5f5c8ee5
GM
5190 if (INTEGERP (*it->dpvec)
5191 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5192 {
5193 int lface_id;
5194 GLYPH g;
5195
5196 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5197 it->c = FAST_GLYPH_CHAR (g);
c5924f47 5198 it->len = CHAR_BYTES (it->c);
5f5c8ee5
GM
5199
5200 /* The entry may contain a face id to use. Such a face id is
5201 the id of a Lisp face, not a realized face. A face id of
969065c3 5202 zero means no face is specified. */
5f5c8ee5
GM
5203 lface_id = FAST_GLYPH_FACE (g);
5204 if (lface_id)
5205 {
969065c3 5206 /* The function returns -1 if lface_id is invalid. */
5f5c8ee5
GM
5207 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
5208 if (face_id >= 0)
969065c3 5209 it->face_id = face_id;
5f5c8ee5
GM
5210 }
5211 }
5212 else
5213 /* Display table entry is invalid. Return a space. */
5214 it->c = ' ', it->len = 1;
5215
5216 /* Don't change position and object of the iterator here. They are
5217 still the values of the character that had this display table
5218 entry or was translated, and that's what we want. */
5219 it->what = IT_CHARACTER;
5220 return 1;
5221}
5222
5223
5224/* Load IT with the next display element from Lisp string IT->string.
5225 IT->current.string_pos is the current position within the string.
5226 If IT->current.overlay_string_index >= 0, the Lisp string is an
5227 overlay string. */
5228
5229static int
5230next_element_from_string (it)
5231 struct it *it;
5232{
5233 struct text_pos position;
5234
5235 xassert (STRINGP (it->string));
5236 xassert (IT_STRING_CHARPOS (*it) >= 0);
5237 position = it->current.string_pos;
5238
5239 /* Time to check for invisible text? */
5240 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5241 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5242 {
5243 handle_stop (it);
5244
5245 /* Since a handler may have changed IT->method, we must
5246 recurse here. */
5247 return get_next_display_element (it);
5248 }
5249
5250 if (it->current.overlay_string_index >= 0)
5251 {
5252 /* Get the next character from an overlay string. In overlay
5253 strings, There is no field width or padding with spaces to
5254 do. */
2051c264 5255 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5f5c8ee5
GM
5256 {
5257 it->what = IT_EOB;
5258 return 0;
5259 }
5260 else if (STRING_MULTIBYTE (it->string))
5261 {
2051c264 5262 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
50f80c2f
KR
5263 const unsigned char *s = (SDATA (it->string)
5264 + IT_STRING_BYTEPOS (*it));
4fdb80f2 5265 it->c = string_char_and_length (s, remaining, &it->len);
5f5c8ee5
GM
5266 }
5267 else
5268 {
2051c264 5269 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5f5c8ee5
GM
5270 it->len = 1;
5271 }
5272 }
5273 else
5274 {
5275 /* Get the next character from a Lisp string that is not an
5276 overlay string. Such strings come from the mode line, for
5277 example. We may have to pad with spaces, or truncate the
5278 string. See also next_element_from_c_string. */
5279 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5280 {
5281 it->what = IT_EOB;
5282 return 0;
5283 }
5284 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5285 {
5286 /* Pad with spaces. */
5287 it->c = ' ', it->len = 1;
5288 CHARPOS (position) = BYTEPOS (position) = -1;
5289 }
5290 else if (STRING_MULTIBYTE (it->string))
5291 {
2051c264 5292 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
50f80c2f
KR
5293 const unsigned char *s = (SDATA (it->string)
5294 + IT_STRING_BYTEPOS (*it));
4fdb80f2 5295 it->c = string_char_and_length (s, maxlen, &it->len);
5f5c8ee5
GM
5296 }
5297 else
5298 {
2051c264 5299 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5f5c8ee5
GM
5300 it->len = 1;
5301 }
5302 }
5303
5304 /* Record what we have and where it came from. Note that we store a
5305 buffer position in IT->position although it could arguably be a
5306 string position. */
5307 it->what = IT_CHARACTER;
5308 it->object = it->string;
5309 it->position = position;
5310 return 1;
5311}
5312
5313
5314/* Load IT with next display element from C string IT->s.
5315 IT->string_nchars is the maximum number of characters to return
5316 from the string. IT->end_charpos may be greater than
5317 IT->string_nchars when this function is called, in which case we
5318 may have to return padding spaces. Value is zero if end of string
5319 reached, including padding spaces. */
5320
5321static int
5322next_element_from_c_string (it)
5323 struct it *it;
5324{
5325 int success_p = 1;
2311178e 5326
5f5c8ee5
GM
5327 xassert (it->s);
5328 it->what = IT_CHARACTER;
5329 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5330 it->object = Qnil;
2311178e 5331
5f5c8ee5
GM
5332 /* IT's position can be greater IT->string_nchars in case a field
5333 width or precision has been specified when the iterator was
5334 initialized. */
5335 if (IT_CHARPOS (*it) >= it->end_charpos)
5336 {
5337 /* End of the game. */
5338 it->what = IT_EOB;
5339 success_p = 0;
5340 }
5341 else if (IT_CHARPOS (*it) >= it->string_nchars)
5342 {
5343 /* Pad with spaces. */
5344 it->c = ' ', it->len = 1;
5345 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5346 }
5347 else if (it->multibyte_p)
5348 {
5349 /* Implementation note: The calls to strlen apparently aren't a
5350 performance problem because there is no noticeable performance
5351 difference between Emacs running in unibyte or multibyte mode. */
5352 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4fdb80f2
GM
5353 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5354 maxlen, &it->len);
5f5c8ee5
GM
5355 }
5356 else
5357 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
2311178e 5358
5f5c8ee5
GM
5359 return success_p;
5360}
5361
5362
5363/* Set up IT to return characters from an ellipsis, if appropriate.
5364 The definition of the ellipsis glyphs may come from a display table
5365 entry. This function Fills IT with the first glyph from the
5366 ellipsis if an ellipsis is to be displayed. */
5367
13f19968 5368static int
5f5c8ee5
GM
5369next_element_from_ellipsis (it)
5370 struct it *it;
5371{
13f19968 5372 if (it->selective_display_ellipsis_p)
5f5c8ee5 5373 {
13f19968
GM
5374 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
5375 {
5376 /* Use the display table definition for `...'. Invalid glyphs
5377 will be handled by the method returning elements from dpvec. */
5378 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
5379 it->dpvec_char_len = it->len;
5380 it->dpvec = v->contents;
5381 it->dpend = v->contents + v->size;
5382 it->current.dpvec_index = 0;
5383 it->method = next_element_from_display_vector;
5384 }
5385 else
5386 {
5387 /* Use default `...' which is stored in default_invis_vector. */
5388 it->dpvec_char_len = it->len;
5389 it->dpvec = default_invis_vector;
5390 it->dpend = default_invis_vector + 3;
5391 it->current.dpvec_index = 0;
5392 it->method = next_element_from_display_vector;
5393 }
5f5c8ee5 5394 }
13f19968 5395 else
54918e2b 5396 {
4aad61f8
GM
5397 /* The face at the current position may be different from the
5398 face we find after the invisible text. Remember what it
5399 was in IT->saved_face_id, and signal that it's there by
5400 setting face_before_selective_p. */
5401 it->saved_face_id = it->face_id;
54918e2b
GM
5402 it->method = next_element_from_buffer;
5403 reseat_at_next_visible_line_start (it, 1);
4aad61f8 5404 it->face_before_selective_p = 1;
54918e2b 5405 }
2311178e 5406
13f19968 5407 return get_next_display_element (it);
5f5c8ee5
GM
5408}
5409
5410
5411/* Deliver an image display element. The iterator IT is already
5412 filled with image information (done in handle_display_prop). Value
5413 is always 1. */
2311178e 5414
5f5c8ee5
GM
5415
5416static int
5417next_element_from_image (it)
5418 struct it *it;
5419{
5420 it->what = IT_IMAGE;
5421 return 1;
5422}
5423
5424
5425/* Fill iterator IT with next display element from a stretch glyph
5426 property. IT->object is the value of the text property. Value is
5427 always 1. */
5428
5429static int
5430next_element_from_stretch (it)
5431 struct it *it;
5432{
5433 it->what = IT_STRETCH;
5434 return 1;
5435}
5436
5437
5438/* Load IT with the next display element from current_buffer. Value
5439 is zero if end of buffer reached. IT->stop_charpos is the next
5440 position at which to stop and check for text properties or buffer
5441 end. */
5442
5443static int
5444next_element_from_buffer (it)
5445 struct it *it;
5446{
5447 int success_p = 1;
5448
5449 /* Check this assumption, otherwise, we would never enter the
5450 if-statement, below. */
5451 xassert (IT_CHARPOS (*it) >= BEGV
5452 && IT_CHARPOS (*it) <= it->stop_charpos);
5453
5454 if (IT_CHARPOS (*it) >= it->stop_charpos)
5455 {
5456 if (IT_CHARPOS (*it) >= it->end_charpos)
5457 {
5458 int overlay_strings_follow_p;
2311178e 5459
5f5c8ee5
GM
5460 /* End of the game, except when overlay strings follow that
5461 haven't been returned yet. */
5462 if (it->overlay_strings_at_end_processed_p)
5463 overlay_strings_follow_p = 0;
5464 else
5465 {
5466 it->overlay_strings_at_end_processed_p = 1;
5a08cbaf 5467 overlay_strings_follow_p = get_overlay_strings (it, 0);
5f5c8ee5
GM
5468 }
5469
5470 if (overlay_strings_follow_p)
5471 success_p = get_next_display_element (it);
5472 else
5473 {
5474 it->what = IT_EOB;
5475 it->position = it->current.pos;
5476 success_p = 0;
5477 }
5478 }
5479 else
5480 {
5481 handle_stop (it);
5482 return get_next_display_element (it);
5483 }
5484 }
5485 else
5486 {
5487 /* No face changes, overlays etc. in sight, so just return a
5488 character from current_buffer. */
5489 unsigned char *p;
5490
5491 /* Maybe run the redisplay end trigger hook. Performance note:
5492 This doesn't seem to cost measurable time. */
5493 if (it->redisplay_end_trigger_charpos
5494 && it->glyph_row
5495 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5496 run_redisplay_end_trigger_hook (it);
5497
5498 /* Get the next character, maybe multibyte. */
5499 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
260a86a0 5500 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5f5c8ee5
GM
5501 {
5502 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5503 - IT_BYTEPOS (*it));
4fdb80f2 5504 it->c = string_char_and_length (p, maxlen, &it->len);
5f5c8ee5
GM
5505 }
5506 else
5507 it->c = *p, it->len = 1;
5508
5509 /* Record what we have and where it came from. */
5510 it->what = IT_CHARACTER;;
5511 it->object = it->w->buffer;
5512 it->position = it->current.pos;
5513
5514 /* Normally we return the character found above, except when we
5515 really want to return an ellipsis for selective display. */
5516 if (it->selective)
5517 {
5518 if (it->c == '\n')
5519 {
5520 /* A value of selective > 0 means hide lines indented more
5521 than that number of columns. */
5522 if (it->selective > 0
5523 && IT_CHARPOS (*it) + 1 < ZV
5524 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5525 IT_BYTEPOS (*it) + 1,
a67e162b 5526 (double) it->selective)) /* iftc */
312246d1 5527 {
13f19968 5528 success_p = next_element_from_ellipsis (it);
312246d1
GM
5529 it->dpvec_char_len = -1;
5530 }
5f5c8ee5
GM
5531 }
5532 else if (it->c == '\r' && it->selective == -1)
5533 {
5534 /* A value of selective == -1 means that everything from the
5535 CR to the end of the line is invisible, with maybe an
5536 ellipsis displayed for it. */
13f19968 5537 success_p = next_element_from_ellipsis (it);
312246d1 5538 it->dpvec_char_len = -1;
5f5c8ee5
GM
5539 }
5540 }
5541 }
5542
5543 /* Value is zero if end of buffer reached. */
c880678e 5544 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5f5c8ee5
GM
5545 return success_p;
5546}
5547
2311178e 5548
5f5c8ee5
GM
5549/* Run the redisplay end trigger hook for IT. */
5550
5551static void
5552run_redisplay_end_trigger_hook (it)
5553 struct it *it;
5554{
5555 Lisp_Object args[3];
5556
5557 /* IT->glyph_row should be non-null, i.e. we should be actually
5558 displaying something, or otherwise we should not run the hook. */
5559 xassert (it->glyph_row);
5560
5561 /* Set up hook arguments. */
5562 args[0] = Qredisplay_end_trigger_functions;
5563 args[1] = it->window;
5564 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5565 it->redisplay_end_trigger_charpos = 0;
5566
5567 /* Since we are *trying* to run these functions, don't try to run
5568 them again, even if they get an error. */
5569 it->w->redisplay_end_trigger = Qnil;
5570 Frun_hook_with_args (3, args);
2311178e 5571
5f5c8ee5
GM
5572 /* Notice if it changed the face of the character we are on. */
5573 handle_face_prop (it);
5574}
5575
5576
260a86a0
KH
5577/* Deliver a composition display element. The iterator IT is already
5578 filled with composition information (done in
5579 handle_composition_prop). Value is always 1. */
5580
5581static int
5582next_element_from_composition (it)
5583 struct it *it;
5584{
5585 it->what = IT_COMPOSITION;
5586 it->position = (STRINGP (it->string)
5587 ? it->current.string_pos
5588 : it->current.pos);
5589 return 1;
5590}
5591
5592
5f5c8ee5
GM
5593\f
5594/***********************************************************************
5595 Moving an iterator without producing glyphs
5596 ***********************************************************************/
5597
5598/* Move iterator IT to a specified buffer or X position within one
5599 line on the display without producing glyphs.
5600
c53a1624
RS
5601 OP should be a bit mask including some or all of these bits:
5602 MOVE_TO_X: Stop on reaching x-position TO_X.
5603 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5604 Regardless of OP's value, stop in reaching the end of the display line.
5f5c8ee5 5605
c53a1624
RS
5606 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5607 This means, in particular, that TO_X includes window's horizontal
5608 scroll amount.
5f5c8ee5 5609
c53a1624
RS
5610 The return value has several possible values that
5611 say what condition caused the scan to stop:
5f5c8ee5
GM
5612
5613 MOVE_POS_MATCH_OR_ZV
5614 - when TO_POS or ZV was reached.
2311178e 5615
5f5c8ee5
GM
5616 MOVE_X_REACHED
5617 -when TO_X was reached before TO_POS or ZV were reached.
2311178e 5618
5f5c8ee5
GM
5619 MOVE_LINE_CONTINUED
5620 - when we reached the end of the display area and the line must
5621 be continued.
2311178e 5622
5f5c8ee5
GM
5623 MOVE_LINE_TRUNCATED
5624 - when we reached the end of the display area and the line is
5625 truncated.
5626
5627 MOVE_NEWLINE_OR_CR
5628 - when we stopped at a line end, i.e. a newline or a CR and selective
5629 display is on. */
5630
701552dd 5631static enum move_it_result
5f5c8ee5
GM
5632move_it_in_display_line_to (it, to_charpos, to_x, op)
5633 struct it *it;
5634 int to_charpos, to_x, op;
5635{
5636 enum move_it_result result = MOVE_UNDEFINED;
5637 struct glyph_row *saved_glyph_row;
5638
5639 /* Don't produce glyphs in produce_glyphs. */
5640 saved_glyph_row = it->glyph_row;
5641 it->glyph_row = NULL;
5642
4540d74c
KS
5643#define BUFFER_POS_REACHED_P() \
5644 ((op & MOVE_TO_POS) != 0 \
5645 && BUFFERP (it->object) \
5646 && IT_CHARPOS (*it) >= to_charpos)
5647
5f5c8ee5
GM
5648 while (1)
5649 {
ae26e27d 5650 int x, i, ascent = 0, descent = 0;
2311178e 5651
5f5c8ee5
GM
5652 /* Stop when ZV or TO_CHARPOS reached. */
5653 if (!get_next_display_element (it)
4540d74c 5654 || BUFFER_POS_REACHED_P ())
5f5c8ee5
GM
5655 {
5656 result = MOVE_POS_MATCH_OR_ZV;
5657 break;
5658 }
2311178e 5659
5f5c8ee5
GM
5660 /* The call to produce_glyphs will get the metrics of the
5661 display element IT is loaded with. We record in x the
5662 x-position before this display element in case it does not
5663 fit on the line. */
5664 x = it->current_x;
2311178e 5665
47589c8c
GM
5666 /* Remember the line height so far in case the next element doesn't
5667 fit on the line. */
5668 if (!it->truncate_lines_p)
5669 {
5670 ascent = it->max_ascent;
5671 descent = it->max_descent;
5672 }
2311178e 5673
5f5c8ee5
GM
5674 PRODUCE_GLYPHS (it);
5675
5676 if (it->area != TEXT_AREA)
5677 {
cafafe0b 5678 set_iterator_to_next (it, 1);
5f5c8ee5
GM
5679 continue;
5680 }
5681
5682 /* The number of glyphs we get back in IT->nglyphs will normally
5683 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5684 character on a terminal frame, or (iii) a line end. For the
5685 second case, IT->nglyphs - 1 padding glyphs will be present
5686 (on X frames, there is only one glyph produced for a
5687 composite character.
5688
5689 The behavior implemented below means, for continuation lines,
5690 that as many spaces of a TAB as fit on the current line are
5691 displayed there. For terminal frames, as many glyphs of a
5692 multi-glyph character are displayed in the current line, too.
5693 This is what the old redisplay code did, and we keep it that
5694 way. Under X, the whole shape of a complex character must
5695 fit on the line or it will be completely displayed in the
5696 next line.
5697
5698 Note that both for tabs and padding glyphs, all glyphs have
5699 the same width. */
5700 if (it->nglyphs)
5701 {
5702 /* More than one glyph or glyph doesn't fit on line. All
5703 glyphs have the same width. */
5704 int single_glyph_width = it->pixel_width / it->nglyphs;
5705 int new_x;
2311178e 5706
5f5c8ee5
GM
5707 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5708 {
5709 new_x = x + single_glyph_width;
5710
5711 /* We want to leave anything reaching TO_X to the caller. */
5712 if ((op & MOVE_TO_X) && new_x > to_x)
5713 {
5714 it->current_x = x;
5715 result = MOVE_X_REACHED;
5716 break;
5717 }
5718 else if (/* Lines are continued. */
5719 !it->truncate_lines_p
5720 && (/* And glyph doesn't fit on the line. */
5721 new_x > it->last_visible_x
5722 /* Or it fits exactly and we're on a window
5723 system frame. */
5724 || (new_x == it->last_visible_x
5725 && FRAME_WINDOW_P (it->f))))
5726 {
5727 if (/* IT->hpos == 0 means the very first glyph
5728 doesn't fit on the line, e.g. a wide image. */
5729 it->hpos == 0
5730 || (new_x == it->last_visible_x
5731 && FRAME_WINDOW_P (it->f)))
5732 {
5733 ++it->hpos;
5734 it->current_x = new_x;
5735 if (i == it->nglyphs - 1)
88e6b646
KS
5736 {
5737 set_iterator_to_next (it, 1);
7af0e8d7 5738#ifdef HAVE_WINDOW_SYSTEM
88e6b646
KS
5739 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5740 {
4540d74c
KS
5741 if (!get_next_display_element (it)
5742 || BUFFER_POS_REACHED_P ())
6d925726
KS
5743 {
5744 result = MOVE_POS_MATCH_OR_ZV;
5745 break;
5746 }
88e6b646
KS
5747 if (ITERATOR_AT_END_OF_LINE_P (it))
5748 {
5749 result = MOVE_NEWLINE_OR_CR;
5750 break;
5751 }
5752 }
7af0e8d7 5753#endif /* HAVE_WINDOW_SYSTEM */
88e6b646 5754 }
5f5c8ee5
GM
5755 }
5756 else
47589c8c
GM
5757 {
5758 it->current_x = x;
5759 it->max_ascent = ascent;
5760 it->max_descent = descent;
5761 }
2311178e 5762
47589c8c
GM
5763 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5764 IT_CHARPOS (*it)));
5f5c8ee5
GM
5765 result = MOVE_LINE_CONTINUED;
5766 break;
5767 }
5768 else if (new_x > it->first_visible_x)
5769 {
5770 /* Glyph is visible. Increment number of glyphs that
5771 would be displayed. */
5772 ++it->hpos;
5773 }
5774 else
5775 {
2311178e 5776 /* Glyph is completely off the left margin of the display
5f5c8ee5
GM
5777 area. Nothing to do. */
5778 }
5779 }
5780
5781 if (result != MOVE_UNDEFINED)
5782 break;
5783 }
5784 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5785 {
5786 /* Stop when TO_X specified and reached. This check is
5787 necessary here because of lines consisting of a line end,
5788 only. The line end will not produce any glyphs and we
5789 would never get MOVE_X_REACHED. */
5790 xassert (it->nglyphs == 0);
5791 result = MOVE_X_REACHED;
5792 break;
5793 }
2311178e 5794
5f5c8ee5
GM
5795 /* Is this a line end? If yes, we're done. */
5796 if (ITERATOR_AT_END_OF_LINE_P (it))
5797 {
5798 result = MOVE_NEWLINE_OR_CR;
5799 break;
5800 }
2311178e 5801
5f5c8ee5
GM
5802 /* The current display element has been consumed. Advance
5803 to the next. */
cafafe0b 5804 set_iterator_to_next (it, 1);
2311178e 5805
5f5c8ee5
GM
5806 /* Stop if lines are truncated and IT's current x-position is
5807 past the right edge of the window now. */
5808 if (it->truncate_lines_p
5809 && it->current_x >= it->last_visible_x)
5810 {
7af0e8d7 5811#ifdef HAVE_WINDOW_SYSTEM
88e6b646
KS
5812 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5813 {
4540d74c
KS
5814 if (!get_next_display_element (it)
5815 || BUFFER_POS_REACHED_P ())
6d925726
KS
5816 {
5817 result = MOVE_POS_MATCH_OR_ZV;
5818 break;
5819 }
88e6b646
KS
5820 if (ITERATOR_AT_END_OF_LINE_P (it))
5821 {
5822 result = MOVE_NEWLINE_OR_CR;
5823 break;
5824 }
5825 }
7af0e8d7 5826#endif /* HAVE_WINDOW_SYSTEM */
5f5c8ee5
GM
5827 result = MOVE_LINE_TRUNCATED;
5828 break;
5829 }
5830 }
5831
4540d74c
KS
5832#undef BUFFER_POS_REACHED_P
5833
5f5c8ee5
GM
5834 /* Restore the iterator settings altered at the beginning of this
5835 function. */
5836 it->glyph_row = saved_glyph_row;
5837 return result;
5838}
5839
5840
9b2bba76
RS
5841/* Move IT forward until it satisfies one or more of the criteria in
5842 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5843
5844 OP is a bit-mask that specifies where to stop, and in particular,
5845 which of those four position arguments makes a difference. See the
5846 description of enum move_operation_enum.
2311178e 5847
5f5c8ee5
GM
5848 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5849 screen line, this function will set IT to the next position >
5850 TO_CHARPOS. */
5851
5852void
5853move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5854 struct it *it;
5855 int to_charpos, to_x, to_y, to_vpos;
5856 int op;
5857{
5858 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5859 int line_height;
47589c8c 5860 int reached = 0;
5f5c8ee5 5861
47589c8c 5862 for (;;)
5f5c8ee5
GM
5863 {
5864 if (op & MOVE_TO_VPOS)
5865 {
5866 /* If no TO_CHARPOS and no TO_X specified, stop at the
5867 start of the line TO_VPOS. */
5868 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5869 {
5870 if (it->vpos == to_vpos)
47589c8c
GM
5871 {
5872 reached = 1;
5873 break;
5874 }
5875 else
5876 skip = move_it_in_display_line_to (it, -1, -1, 0);
5f5c8ee5
GM
5877 }
5878 else
5879 {
5880 /* TO_VPOS >= 0 means stop at TO_X in the line at
5881 TO_VPOS, or at TO_POS, whichever comes first. */
47589c8c
GM
5882 if (it->vpos == to_vpos)
5883 {
5884 reached = 2;
5885 break;
5886 }
2311178e 5887
5f5c8ee5
GM
5888 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5889
5890 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
47589c8c
GM
5891 {
5892 reached = 3;
5893 break;
5894 }
5f5c8ee5
GM
5895 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5896 {
5897 /* We have reached TO_X but not in the line we want. */
5898 skip = move_it_in_display_line_to (it, to_charpos,
5899 -1, MOVE_TO_POS);
5900 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c
GM
5901 {
5902 reached = 4;
5903 break;
5904 }
5f5c8ee5
GM
5905 }
5906 }
5907 }
5908 else if (op & MOVE_TO_Y)
5909 {
5910 struct it it_backup;
2311178e 5911
5f5c8ee5
GM
5912 /* TO_Y specified means stop at TO_X in the line containing
5913 TO_Y---or at TO_CHARPOS if this is reached first. The
5914 problem is that we can't really tell whether the line
5915 contains TO_Y before we have completely scanned it, and
5916 this may skip past TO_X. What we do is to first scan to
5917 TO_X.
5918
5919 If TO_X is not specified, use a TO_X of zero. The reason
5920 is to make the outcome of this function more predictable.
5921 If we didn't use TO_X == 0, we would stop at the end of
5922 the line which is probably not what a caller would expect
5923 to happen. */
5924 skip = move_it_in_display_line_to (it, to_charpos,
5925 ((op & MOVE_TO_X)
5926 ? to_x : 0),
5927 (MOVE_TO_X
5928 | (op & MOVE_TO_POS)));
5929
5930 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5931 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c
GM
5932 {
5933 reached = 5;
5934 break;
5935 }
2311178e 5936
5f5c8ee5
GM
5937 /* If TO_X was reached, we would like to know whether TO_Y
5938 is in the line. This can only be said if we know the
5939 total line height which requires us to scan the rest of
5940 the line. */
5f5c8ee5
GM
5941 if (skip == MOVE_X_REACHED)
5942 {
5943 it_backup = *it;
47589c8c 5944 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
5945 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5946 op & MOVE_TO_POS);
47589c8c 5947 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
5948 }
5949
5950 /* Now, decide whether TO_Y is in this line. */
5951 line_height = it->max_ascent + it->max_descent;
47589c8c 5952 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
2311178e 5953
5f5c8ee5
GM
5954 if (to_y >= it->current_y
5955 && to_y < it->current_y + line_height)
5956 {
5957 if (skip == MOVE_X_REACHED)
5958 /* If TO_Y is in this line and TO_X was reached above,
5959 we scanned too far. We have to restore IT's settings
5960 to the ones before skipping. */
5961 *it = it_backup;
47589c8c 5962 reached = 6;
5f5c8ee5
GM
5963 }
5964 else if (skip == MOVE_X_REACHED)
5965 {
5966 skip = skip2;
5967 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c 5968 reached = 7;
5f5c8ee5
GM
5969 }
5970
47589c8c 5971 if (reached)
5f5c8ee5
GM
5972 break;
5973 }
5974 else
5975 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5976
5977 switch (skip)
5978 {
5979 case MOVE_POS_MATCH_OR_ZV:
47589c8c
GM
5980 reached = 8;
5981 goto out;
5f5c8ee5
GM
5982
5983 case MOVE_NEWLINE_OR_CR:
cafafe0b 5984 set_iterator_to_next (it, 1);
5f5c8ee5
GM
5985 it->continuation_lines_width = 0;
5986 break;
5987
5988 case MOVE_LINE_TRUNCATED:
5989 it->continuation_lines_width = 0;
312246d1 5990 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
5991 if ((op & MOVE_TO_POS) != 0
5992 && IT_CHARPOS (*it) > to_charpos)
47589c8c
GM
5993 {
5994 reached = 9;
5995 goto out;
5996 }
5f5c8ee5
GM
5997 break;
5998
5999 case MOVE_LINE_CONTINUED:
6000 it->continuation_lines_width += it->current_x;
6001 break;
6002
6003 default:
6004 abort ();
6005 }
6006
6007 /* Reset/increment for the next run. */
6008 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6009 it->current_x = it->hpos = 0;
6010 it->current_y += it->max_ascent + it->max_descent;
6011 ++it->vpos;
6012 last_height = it->max_ascent + it->max_descent;
6013 last_max_ascent = it->max_ascent;
6014 it->max_ascent = it->max_descent = 0;
6015 }
2311178e 6016
47589c8c
GM
6017 out:
6018
6019 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5f5c8ee5
GM
6020}
6021
6022
6023/* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6024
6025 If DY > 0, move IT backward at least that many pixels. DY = 0
6026 means move IT backward to the preceding line start or BEGV. This
6027 function may move over more than DY pixels if IT->current_y - DY
6028 ends up in the middle of a line; in this case IT->current_y will be
6029 set to the top of the line moved to. */
6030
6031void
6032move_it_vertically_backward (it, dy)
6033 struct it *it;
6034 int dy;
6035{
79ddf6f7
GM
6036 int nlines, h;
6037 struct it it2, it3;
5f5c8ee5 6038 int start_pos = IT_CHARPOS (*it);
2311178e 6039
5f5c8ee5
GM
6040 xassert (dy >= 0);
6041
6042 /* Estimate how many newlines we must move back. */
da8b7f4f 6043 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
5f5c8ee5
GM
6044
6045 /* Set the iterator's position that many lines back. */
6046 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6047 back_to_previous_visible_line_start (it);
6048
6049 /* Reseat the iterator here. When moving backward, we don't want
6050 reseat to skip forward over invisible text, set up the iterator
6051 to deliver from overlay strings at the new position etc. So,
6052 use reseat_1 here. */
6053 reseat_1 (it, it->current.pos, 1);
6054
6055 /* We are now surely at a line start. */
6056 it->current_x = it->hpos = 0;
0e47bbf7 6057 it->continuation_lines_width = 0;
5f5c8ee5
GM
6058
6059 /* Move forward and see what y-distance we moved. First move to the
6060 start of the next line so that we get its height. We need this
6061 height to be able to tell whether we reached the specified
6062 y-distance. */
6063 it2 = *it;
6064 it2.max_ascent = it2.max_descent = 0;
6065 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6066 MOVE_TO_POS | MOVE_TO_VPOS);
6067 xassert (IT_CHARPOS (*it) >= BEGV);
79ddf6f7 6068 it3 = it2;
2311178e 6069
5f5c8ee5
GM
6070 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6071 xassert (IT_CHARPOS (*it) >= BEGV);
ccbb9ed2
RS
6072 /* H is the actual vertical distance from the position in *IT
6073 and the starting position. */
5f5c8ee5 6074 h = it2.current_y - it->current_y;
ccbb9ed2 6075 /* NLINES is the distance in number of lines. */
5f5c8ee5
GM
6076 nlines = it2.vpos - it->vpos;
6077
ccbb9ed2
RS
6078 /* Correct IT's y and vpos position
6079 so that they are relative to the starting point. */
5f5c8ee5
GM
6080 it->vpos -= nlines;
6081 it->current_y -= h;
2311178e 6082
5f5c8ee5
GM
6083 if (dy == 0)
6084 {
6085 /* DY == 0 means move to the start of the screen line. The
6086 value of nlines is > 0 if continuation lines were involved. */
6087 if (nlines > 0)
6088 move_it_by_lines (it, nlines, 1);
6089 xassert (IT_CHARPOS (*it) <= start_pos);
6090 }
ccbb9ed2 6091 else
5f5c8ee5 6092 {
ccbb9ed2
RS
6093 /* The y-position we try to reach, relative to *IT.
6094 Note that H has been subtracted in front of the if-statement. */
5f5c8ee5 6095 int target_y = it->current_y + h - dy;
79ddf6f7
GM
6096 int y0 = it3.current_y;
6097 int y1 = line_bottom_y (&it3);
6098 int line_height = y1 - y0;
f7ccfc8c 6099
5f5c8ee5
GM
6100 /* If we did not reach target_y, try to move further backward if
6101 we can. If we moved too far backward, try to move forward. */
6102 if (target_y < it->current_y
79ddf6f7
GM
6103 /* This is heuristic. In a window that's 3 lines high, with
6104 a line height of 13 pixels each, recentering with point
6105 on the bottom line will try to move -39/2 = 19 pixels
6106 backward. Try to avoid moving into the first line. */
798dbe1f 6107 && it->current_y - target_y > line_height / 3 * 2
5f5c8ee5
GM
6108 && IT_CHARPOS (*it) > BEGV)
6109 {
f7ccfc8c
GM
6110 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6111 target_y - it->current_y));
5f5c8ee5
GM
6112 move_it_vertically (it, target_y - it->current_y);
6113 xassert (IT_CHARPOS (*it) >= BEGV);
6114 }
6115 else if (target_y >= it->current_y + line_height
6116 && IT_CHARPOS (*it) < ZV)
6117 {
55591976 6118 /* Should move forward by at least one line, maybe more.
2311178e 6119
55591976
GM
6120 Note: Calling move_it_by_lines can be expensive on
6121 terminal frames, where compute_motion is used (via
6122 vmotion) to do the job, when there are very long lines
6123 and truncate-lines is nil. That's the reason for
6124 treating terminal frames specially here. */
2311178e 6125
55591976
GM
6126 if (!FRAME_WINDOW_P (it->f))
6127 move_it_vertically (it, target_y - (it->current_y + line_height));
6128 else
f7ccfc8c 6129 {
55591976
GM
6130 do
6131 {
6132 move_it_by_lines (it, 1, 1);
6133 }
6134 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
f7ccfc8c 6135 }
f7ccfc8c 6136
5f5c8ee5
GM
6137 xassert (IT_CHARPOS (*it) >= BEGV);
6138 }
6139 }
6140}
6141
6142
6143/* Move IT by a specified amount of pixel lines DY. DY negative means
6144 move backwards. DY = 0 means move to start of screen line. At the
6145 end, IT will be on the start of a screen line. */
6146
2311178e 6147void
5f5c8ee5
GM
6148move_it_vertically (it, dy)
6149 struct it *it;
6150 int dy;
6151{
6152 if (dy <= 0)
6153 move_it_vertically_backward (it, -dy);
6154 else if (dy > 0)
6155 {
47589c8c 6156 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5f5c8ee5
GM
6157 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6158 MOVE_TO_POS | MOVE_TO_Y);
47589c8c 6159 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
6160
6161 /* If buffer ends in ZV without a newline, move to the start of
6162 the line to satisfy the post-condition. */
6163 if (IT_CHARPOS (*it) == ZV
6164 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6165 move_it_by_lines (it, 0, 0);
6166 }
6167}
6168
6169
47fc2c10
GM
6170/* Move iterator IT past the end of the text line it is in. */
6171
6172void
6173move_it_past_eol (it)
6174 struct it *it;
6175{
6176 enum move_it_result rc;
2311178e 6177
47fc2c10
GM
6178 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6179 if (rc == MOVE_NEWLINE_OR_CR)
6180 set_iterator_to_next (it, 0);
6181}
6182
6183
2c79b732
GM
6184#if 0 /* Currently not used. */
6185
5f5c8ee5
GM
6186/* Return non-zero if some text between buffer positions START_CHARPOS
6187 and END_CHARPOS is invisible. IT->window is the window for text
6188 property lookup. */
6189
6190static int
6191invisible_text_between_p (it, start_charpos, end_charpos)
6192 struct it *it;
6193 int start_charpos, end_charpos;
6194{
5f5c8ee5
GM
6195 Lisp_Object prop, limit;
6196 int invisible_found_p;
2311178e 6197
5f5c8ee5
GM
6198 xassert (it != NULL && start_charpos <= end_charpos);
6199
6200 /* Is text at START invisible? */
6201 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6202 it->window);
6203 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6204 invisible_found_p = 1;
6205 else
6206 {
016b5642
MB
6207 limit = Fnext_single_char_property_change (make_number (start_charpos),
6208 Qinvisible, Qnil,
6209 make_number (end_charpos));
5f5c8ee5
GM
6210 invisible_found_p = XFASTINT (limit) < end_charpos;
6211 }
6212
6213 return invisible_found_p;
5f5c8ee5
GM
6214}
6215
2c79b732
GM
6216#endif /* 0 */
6217
5f5c8ee5
GM
6218
6219/* Move IT by a specified number DVPOS of screen lines down. DVPOS
6220 negative means move up. DVPOS == 0 means move to the start of the
6221 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6222 NEED_Y_P is zero, IT->current_y will be left unchanged.
6223
6224 Further optimization ideas: If we would know that IT->f doesn't use
6225 a face with proportional font, we could be faster for
6226 truncate-lines nil. */
6227
6228void
6229move_it_by_lines (it, dvpos, need_y_p)
6230 struct it *it;
6231 int dvpos, need_y_p;
6232{
6233 struct position pos;
2311178e 6234
5f5c8ee5
GM
6235 if (!FRAME_WINDOW_P (it->f))
6236 {
6237 struct text_pos textpos;
2311178e 6238
5f5c8ee5
GM
6239 /* We can use vmotion on frames without proportional fonts. */
6240 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6241 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6242 reseat (it, textpos, 1);
6243 it->vpos += pos.vpos;
6244 it->current_y += pos.vpos;
6245 }
6246 else if (dvpos == 0)
6247 {
6248 /* DVPOS == 0 means move to the start of the screen line. */
6249 move_it_vertically_backward (it, 0);
6250 xassert (it->current_x == 0 && it->hpos == 0);
6251 }
6252 else if (dvpos > 0)
2c79b732 6253 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5f5c8ee5
GM
6254 else
6255 {
6256 struct it it2;
6257 int start_charpos, i;
2311178e 6258
e8660d73
GM
6259 /* Start at the beginning of the screen line containing IT's
6260 position. */
6261 move_it_vertically_backward (it, 0);
2311178e 6262
5f5c8ee5
GM
6263 /* Go back -DVPOS visible lines and reseat the iterator there. */
6264 start_charpos = IT_CHARPOS (*it);
6265 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6266 back_to_previous_visible_line_start (it);
6267 reseat (it, it->current.pos, 1);
6268 it->current_x = it->hpos = 0;
6269
6270 /* Above call may have moved too far if continuation lines
6271 are involved. Scan forward and see if it did. */
6272 it2 = *it;
6273 it2.vpos = it2.current_y = 0;
6274 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6275 it->vpos -= it2.vpos;
6276 it->current_y -= it2.current_y;
6277 it->current_x = it->hpos = 0;
6278
6279 /* If we moved too far, move IT some lines forward. */
6280 if (it2.vpos > -dvpos)
6281 {
6282 int delta = it2.vpos + dvpos;
6283 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6284 }
6285 }
6286}
6287
3824b1a6
AS
6288/* Return 1 if IT points into the middle of a display vector. */
6289
6290int
6291in_display_vector_p (it)
6292 struct it *it;
6293{
6294 return (it->method == next_element_from_display_vector
6295 && it->current.dpvec_index > 0
6296 && it->dpvec + it->current.dpvec_index != it->dpend);
6297}
5f5c8ee5
GM
6298
6299\f
6300/***********************************************************************
6301 Messages
6302 ***********************************************************************/
6303
6304
937248bc
GM
6305/* Add a message with format string FORMAT and arguments ARG1 and ARG2
6306 to *Messages*. */
6307
6308void
6309add_to_log (format, arg1, arg2)
6310 char *format;
6311 Lisp_Object arg1, arg2;
6312{
6313 Lisp_Object args[3];
6314 Lisp_Object msg, fmt;
6315 char *buffer;
6316 int len;
6317 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
dbadb64b 6318 USE_SAFE_ALLOCA;
937248bc 6319
ae794295
GM
6320 /* Do nothing if called asynchronously. Inserting text into
6321 a buffer may call after-change-functions and alike and
6322 that would means running Lisp asynchronously. */
6323 if (handling_signal)
6324 return;
6325
937248bc
GM
6326 fmt = msg = Qnil;
6327 GCPRO4 (fmt, msg, arg1, arg2);
2311178e 6328
937248bc
GM
6329 args[0] = fmt = build_string (format);
6330 args[1] = arg1;
6331 args[2] = arg2;
6fc556fd 6332 msg = Fformat (3, args);
937248bc 6333
2051c264 6334 len = SBYTES (msg) + 1;
dbadb64b 6335 SAFE_ALLOCA (buffer, char *, len);
2051c264 6336 bcopy (SDATA (msg), buffer, len);
2311178e 6337
796184bc 6338 message_dolog (buffer, len - 1, 1, 0);
dbadb64b
KS
6339 SAFE_FREE (len);
6340
937248bc
GM
6341 UNGCPRO;
6342}
6343
6344
5f5c8ee5
GM
6345/* Output a newline in the *Messages* buffer if "needs" one. */
6346
6347void
6348message_log_maybe_newline ()
6349{
6350 if (message_log_need_newline)
6351 message_dolog ("", 0, 1, 0);
6352}
6353
6354
1e313f28 6355/* Add a string M of length NBYTES to the message log, optionally
5f5c8ee5
GM
6356 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6357 nonzero, means interpret the contents of M as multibyte. This
6358 function calls low-level routines in order to bypass text property
6359 hooks, etc. which might not be safe to run. */
6360
6361void
1e313f28 6362message_dolog (m, nbytes, nlflag, multibyte)
50f80c2f 6363 const char *m;
1e313f28 6364 int nbytes, nlflag, multibyte;
5f5c8ee5 6365{
a67e162b
RS
6366 if (!NILP (Vmemory_full))
6367 return;
6368
5f5c8ee5
GM
6369 if (!NILP (Vmessage_log_max))
6370 {
6371 struct buffer *oldbuf;
6372 Lisp_Object oldpoint, oldbegv, oldzv;
6373 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6374 int point_at_end = 0;
6375 int zv_at_end = 0;
6376 Lisp_Object old_deactivate_mark, tem;
6052529b 6377 struct gcpro gcpro1;
5f5c8ee5
GM
6378
6379 old_deactivate_mark = Vdeactivate_mark;
6380 oldbuf = current_buffer;
6a94510a 6381 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5f5c8ee5
GM
6382 current_buffer->undo_list = Qt;
6383
b14bc55e
RS
6384 oldpoint = message_dolog_marker1;
6385 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6386 oldbegv = message_dolog_marker2;
6387 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6388 oldzv = message_dolog_marker3;
6389 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6390 GCPRO1 (old_deactivate_mark);
5f5c8ee5
GM
6391
6392 if (PT == Z)
6393 point_at_end = 1;
6394 if (ZV == Z)
6395 zv_at_end = 1;
6396
6397 BEGV = BEG;
6398 BEGV_BYTE = BEG_BYTE;
6399 ZV = Z;
6400 ZV_BYTE = Z_BYTE;
6401 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6402
6403 /* Insert the string--maybe converting multibyte to single byte
6404 or vice versa, so that all the text fits the buffer. */
6405 if (multibyte
6406 && NILP (current_buffer->enable_multibyte_characters))
6407 {
1e313f28 6408 int i, c, char_bytes;
5f5c8ee5 6409 unsigned char work[1];
2311178e 6410
5f5c8ee5
GM
6411 /* Convert a multibyte string to single-byte
6412 for the *Message* buffer. */
6e57ec5e 6413 for (i = 0; i < nbytes; i += char_bytes)
5f5c8ee5 6414 {
1e313f28 6415 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5f5c8ee5
GM
6416 work[0] = (SINGLE_BYTE_CHAR_P (c)
6417 ? c
6418 : multibyte_char_to_unibyte (c, Qnil));
6419 insert_1_both (work, 1, 1, 1, 0, 0);
6420 }
6421 }
6422 else if (! multibyte
6423 && ! NILP (current_buffer->enable_multibyte_characters))
6424 {
1e313f28 6425 int i, c, char_bytes;
5f5c8ee5 6426 unsigned char *msg = (unsigned char *) m;
260a86a0 6427 unsigned char str[MAX_MULTIBYTE_LENGTH];
5f5c8ee5
GM
6428 /* Convert a single-byte string to multibyte
6429 for the *Message* buffer. */
1e313f28 6430 for (i = 0; i < nbytes; i++)
5f5c8ee5
GM
6431 {
6432 c = unibyte_char_to_multibyte (msg[i]);
1e313f28
GM
6433 char_bytes = CHAR_STRING (c, str);
6434 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5f5c8ee5
GM
6435 }
6436 }
1e313f28
GM
6437 else if (nbytes)
6438 insert_1 (m, nbytes, 1, 0, 0);
5f5c8ee5
GM
6439
6440 if (nlflag)
6441 {
6442 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6443 insert_1 ("\n", 1, 1, 0, 0);
6444
6445 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6446 this_bol = PT;
6447 this_bol_byte = PT_BYTE;
6448
b14bc55e
RS
6449 /* See if this line duplicates the previous one.
6450 If so, combine duplicates. */
5f5c8ee5
GM
6451 if (this_bol > BEG)
6452 {
6453 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6454 prev_bol = PT;
6455 prev_bol_byte = PT_BYTE;
6456
6457 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6458 this_bol, this_bol_byte);
6459 if (dup)
6460 {
6461 del_range_both (prev_bol, prev_bol_byte,
6462 this_bol, this_bol_byte, 0);
6463 if (dup > 1)
6464 {
6465 char dupstr[40];
6466 int duplen;
6467
6468 /* If you change this format, don't forget to also
6469 change message_log_check_duplicate. */
6470 sprintf (dupstr, " [%d times]", dup);
6471 duplen = strlen (dupstr);
6472 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6473 insert_1 (dupstr, duplen, 1, 0, 1);
6474 }
6475 }
6476 }
6477
b14bc55e
RS
6478 /* If we have more than the desired maximum number of lines
6479 in the *Messages* buffer now, delete the oldest ones.
6480 This is safe because we don't have undo in this buffer. */
6481
5f5c8ee5
GM
6482 if (NATNUMP (Vmessage_log_max))
6483 {
6484 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6485 -XFASTINT (Vmessage_log_max) - 1, 0);
6486 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6487 }
6488 }
6489 BEGV = XMARKER (oldbegv)->charpos;
6490 BEGV_BYTE = marker_byte_position (oldbegv);
6491
6492 if (zv_at_end)
6493 {
6494 ZV = Z;
6495 ZV_BYTE = Z_BYTE;
6496 }
6497 else
6498 {
6499 ZV = XMARKER (oldzv)->charpos;
6500 ZV_BYTE = marker_byte_position (oldzv);
6501 }
6502
6503 if (point_at_end)
6504 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6505 else
6506 /* We can't do Fgoto_char (oldpoint) because it will run some
6507 Lisp code. */
6508 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6509 XMARKER (oldpoint)->bytepos);
6510
6511 UNGCPRO;
cfea0546
SM
6512 unchain_marker (XMARKER (oldpoint));
6513 unchain_marker (XMARKER (oldbegv));
6514 unchain_marker (XMARKER (oldzv));
5f5c8ee5
GM
6515
6516 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6517 set_buffer_internal (oldbuf);
6518 if (NILP (tem))
6519 windows_or_buffers_changed = old_windows_or_buffers_changed;
6520 message_log_need_newline = !nlflag;
6521 Vdeactivate_mark = old_deactivate_mark;
6522 }
6523}
6524
6525
6526/* We are at the end of the buffer after just having inserted a newline.
6527 (Note: We depend on the fact we won't be crossing the gap.)
6528 Check to see if the most recent message looks a lot like the previous one.
6529 Return 0 if different, 1 if the new one should just replace it, or a
6530 value N > 1 if we should also append " [N times]". */
6531
6532static int
6533message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6534 int prev_bol, this_bol;
6535 int prev_bol_byte, this_bol_byte;
6536{
6537 int i;
6538 int len = Z_BYTE - 1 - this_bol_byte;
6539 int seen_dots = 0;
6540 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6541 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6542
6543 for (i = 0; i < len; i++)
6544 {
509633e3 6545 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5f5c8ee5
GM
6546 seen_dots = 1;
6547 if (p1[i] != p2[i])
6548 return seen_dots;
6549 }
6550 p1 += len;
6551 if (*p1 == '\n')
6552 return 2;
6553 if (*p1++ == ' ' && *p1++ == '[')
6554 {
6555 int n = 0;
6556 while (*p1 >= '0' && *p1 <= '9')
6557 n = n * 10 + *p1++ - '0';
6558 if (strncmp (p1, " times]\n", 8) == 0)
6559 return n+1;
6560 }
6561 return 0;
6562}
6563
6564
1e313f28
GM
6565/* Display an echo area message M with a specified length of NBYTES
6566 bytes. The string may include null characters. If M is 0, clear
6567 out any existing message, and let the mini-buffer text show
6568 through.
5f5c8ee5
GM
6569
6570 The buffer M must continue to exist until after the echo area gets
6571 cleared or some other message gets displayed there. This means do
6572 not pass text that is stored in a Lisp string; do not pass text in
6573 a buffer that was alloca'd. */
6574
6575void
1e313f28 6576message2 (m, nbytes, multibyte)
50f80c2f 6577 const char *m;
1e313f28 6578 int nbytes;
5f5c8ee5
GM
6579 int multibyte;
6580{
6581 /* First flush out any partial line written with print. */
6582 message_log_maybe_newline ();
6583 if (m)
1e313f28
GM
6584 message_dolog (m, nbytes, 1, multibyte);
6585 message2_nolog (m, nbytes, multibyte);
5f5c8ee5
GM
6586}
6587
6588
6589/* The non-logging counterpart of message2. */
6590
6591void
1e313f28 6592message2_nolog (m, nbytes, multibyte)
50f80c2f 6593 const char *m;
e3383b6f 6594 int nbytes, multibyte;
5f5c8ee5 6595{
886bd6f2 6596 struct frame *sf = SELECTED_FRAME ();
5f5c8ee5
GM
6597 message_enable_multibyte = multibyte;
6598
6599 if (noninteractive)
6600 {
6601 if (noninteractive_need_newline)
6602 putc ('\n', stderr);
6603 noninteractive_need_newline = 0;
6604 if (m)
1e313f28 6605 fwrite (m, nbytes, 1, stderr);
5f5c8ee5
GM
6606 if (cursor_in_echo_area == 0)
6607 fprintf (stderr, "\n");
6608 fflush (stderr);
6609 }
6610 /* A null message buffer means that the frame hasn't really been
6611 initialized yet. Error messages get reported properly by
6612 cmd_error, so this must be just an informative message; toss it. */
2311178e 6613 else if (INTERACTIVE
886bd6f2
GM
6614 && sf->glyphs_initialized_p
6615 && FRAME_MESSAGE_BUF (sf))
5f5c8ee5
GM
6616 {
6617 Lisp_Object mini_window;
6618 struct frame *f;
6619
6620 /* Get the frame containing the mini-buffer
6621 that the selected frame is using. */
886bd6f2 6622 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
6623 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6624
6625 FRAME_SAMPLE_VISIBILITY (f);
886bd6f2 6626 if (FRAME_VISIBLE_P (sf)
5f5c8ee5
GM
6627 && ! FRAME_VISIBLE_P (f))
6628 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6629
6630 if (m)
6631 {
1e313f28 6632 set_message (m, Qnil, nbytes, multibyte);
5f5c8ee5
GM
6633 if (minibuffer_auto_raise)
6634 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6635 }
6636 else
c6e89d6c 6637 clear_message (1, 1);
5f5c8ee5 6638
c6e89d6c 6639 do_pending_window_change (0);
5f5c8ee5 6640 echo_area_display (1);
c6e89d6c 6641 do_pending_window_change (0);
5f5c8ee5
GM
6642 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6643 (*frame_up_to_date_hook) (f);
6644 }
6645}
6646
6647
c6e89d6c
GM
6648/* Display an echo area message M with a specified length of NBYTES
6649 bytes. The string may include null characters. If M is not a
5f5c8ee5
GM
6650 string, clear out any existing message, and let the mini-buffer
6651 text show through. */
6652
6653void
c6e89d6c 6654message3 (m, nbytes, multibyte)
5f5c8ee5 6655 Lisp_Object m;
c6e89d6c 6656 int nbytes;
5f5c8ee5
GM
6657 int multibyte;
6658{
6659 struct gcpro gcpro1;
6660
6661 GCPRO1 (m);
2311178e 6662
5f5c8ee5
GM
6663 /* First flush out any partial line written with print. */
6664 message_log_maybe_newline ();
6665 if (STRINGP (m))
2051c264 6666 message_dolog (SDATA (m), nbytes, 1, multibyte);
c6e89d6c 6667 message3_nolog (m, nbytes, multibyte);
5f5c8ee5
GM
6668
6669 UNGCPRO;
6670}
6671
6672
6673/* The non-logging version of message3. */
6674
6675void
c6e89d6c 6676message3_nolog (m, nbytes, multibyte)
5f5c8ee5 6677 Lisp_Object m;
c6e89d6c 6678 int nbytes, multibyte;
5f5c8ee5 6679{
886bd6f2 6680 struct frame *sf = SELECTED_FRAME ();
5f5c8ee5
GM
6681 message_enable_multibyte = multibyte;
6682
6683 if (noninteractive)
6684 {
6685 if (noninteractive_need_newline)
6686 putc ('\n', stderr);
6687 noninteractive_need_newline = 0;
6688 if (STRINGP (m))
2051c264 6689 fwrite (SDATA (m), nbytes, 1, stderr);
5f5c8ee5
GM
6690 if (cursor_in_echo_area == 0)
6691 fprintf (stderr, "\n");
6692 fflush (stderr);
6693 }
6694 /* A null message buffer means that the frame hasn't really been
6695 initialized yet. Error messages get reported properly by
6696 cmd_error, so this must be just an informative message; toss it. */
2311178e 6697 else if (INTERACTIVE
886bd6f2
GM
6698 && sf->glyphs_initialized_p
6699 && FRAME_MESSAGE_BUF (sf))
5f5c8ee5
GM
6700 {
6701 Lisp_Object mini_window;
c6e89d6c 6702 Lisp_Object frame;
5f5c8ee5
GM
6703 struct frame *f;
6704
6705 /* Get the frame containing the mini-buffer
6706 that the selected frame is using. */
886bd6f2 6707 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
6708 frame = XWINDOW (mini_window)->frame;
6709 f = XFRAME (frame);
5f5c8ee5
GM
6710
6711 FRAME_SAMPLE_VISIBILITY (f);
886bd6f2 6712 if (FRAME_VISIBLE_P (sf)
c6e89d6c
GM
6713 && !FRAME_VISIBLE_P (f))
6714 Fmake_frame_visible (frame);
5f5c8ee5 6715
2051c264 6716 if (STRINGP (m) && SCHARS (m) > 0)
5f5c8ee5 6717 {
c6e89d6c 6718 set_message (NULL, m, nbytes, multibyte);
468155d7
GM
6719 if (minibuffer_auto_raise)
6720 Fraise_frame (frame);
5f5c8ee5
GM
6721 }
6722 else
c6e89d6c 6723 clear_message (1, 1);
5f5c8ee5 6724
c6e89d6c 6725 do_pending_window_change (0);
5f5c8ee5 6726 echo_area_display (1);
c6e89d6c 6727 do_pending_window_change (0);
5f5c8ee5
GM
6728 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6729 (*frame_up_to_date_hook) (f);
6730 }
6731}
6732
6733
6734/* Display a null-terminated echo area message M. If M is 0, clear
6735 out any existing message, and let the mini-buffer text show through.
6736
6737 The buffer M must continue to exist until after the echo area gets
6738 cleared or some other message gets displayed there. Do not pass
6739 text that is stored in a Lisp string. Do not pass text in a buffer
6740 that was alloca'd. */
6741
6742void
6743message1 (m)
6744 char *m;
6745{
6746 message2 (m, (m ? strlen (m) : 0), 0);
6747}
6748
6749
6750/* The non-logging counterpart of message1. */
6751
6752void
6753message1_nolog (m)
6754 char *m;
6755{
6756 message2_nolog (m, (m ? strlen (m) : 0), 0);
6757}
6758
6759/* Display a message M which contains a single %s
6760 which gets replaced with STRING. */
6761
6762void
6763message_with_string (m, string, log)
6764 char *m;
6765 Lisp_Object string;
6766 int log;
6767{
5b6d51b6
RS
6768 CHECK_STRING (string);
6769
5f5c8ee5
GM
6770 if (noninteractive)
6771 {
6772 if (m)
6773 {
6774 if (noninteractive_need_newline)
6775 putc ('\n', stderr);
6776 noninteractive_need_newline = 0;
2051c264 6777 fprintf (stderr, m, SDATA (string));
5f5c8ee5
GM
6778 if (cursor_in_echo_area == 0)
6779 fprintf (stderr, "\n");
6780 fflush (stderr);
6781 }
6782 }
6783 else if (INTERACTIVE)
6784 {
6785 /* The frame whose minibuffer we're going to display the message on.
6786 It may be larger than the selected frame, so we need
6787 to use its buffer, not the selected frame's buffer. */
6788 Lisp_Object mini_window;
886bd6f2 6789 struct frame *f, *sf = SELECTED_FRAME ();
5f5c8ee5
GM
6790
6791 /* Get the frame containing the minibuffer
6792 that the selected frame is using. */
886bd6f2 6793 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
6794 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6795
6796 /* A null message buffer means that the frame hasn't really been
6797 initialized yet. Error messages get reported properly by
6798 cmd_error, so this must be just an informative message; toss it. */
6799 if (FRAME_MESSAGE_BUF (f))
6800 {
eb484132
GM
6801 Lisp_Object args[2], message;
6802 struct gcpro gcpro1, gcpro2;
5f5c8ee5 6803
eb484132
GM
6804 args[0] = build_string (m);
6805 args[1] = message = string;
78e17433 6806 GCPRO2 (args[0], message);
eb484132 6807 gcpro1.nvars = 2;
2311178e 6808
eb484132 6809 message = Fformat (2, args);
5f5c8ee5
GM
6810
6811 if (log)
d5db4077 6812 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
5f5c8ee5 6813 else
d5db4077 6814 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
eb484132
GM
6815
6816 UNGCPRO;
5f5c8ee5
GM
6817
6818 /* Print should start at the beginning of the message
6819 buffer next time. */
6820 message_buf_print = 0;
6821 }
6822 }
6823}
6824
6825
5f5c8ee5
GM
6826/* Dump an informative message to the minibuf. If M is 0, clear out
6827 any existing message, and let the mini-buffer text show through. */
6828
6829/* VARARGS 1 */
6830void
6831message (m, a1, a2, a3)
6832 char *m;
6833 EMACS_INT a1, a2, a3;
6834{
6835 if (noninteractive)
6836 {
6837 if (m)
6838 {
6839 if (noninteractive_need_newline)
6840 putc ('\n', stderr);
6841 noninteractive_need_newline = 0;
6842 fprintf (stderr, m, a1, a2, a3);
6843 if (cursor_in_echo_area == 0)
6844 fprintf (stderr, "\n");
6845 fflush (stderr);
6846 }
6847 }
6848 else if (INTERACTIVE)
6849 {
6850 /* The frame whose mini-buffer we're going to display the message
6851 on. It may be larger than the selected frame, so we need to
6852 use its buffer, not the selected frame's buffer. */
6853 Lisp_Object mini_window;
886bd6f2 6854 struct frame *f, *sf = SELECTED_FRAME ();
5f5c8ee5
GM
6855
6856 /* Get the frame containing the mini-buffer
6857 that the selected frame is using. */
886bd6f2 6858 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
6859 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6860
6861 /* A null message buffer means that the frame hasn't really been
6862 initialized yet. Error messages get reported properly by
6863 cmd_error, so this must be just an informative message; toss
6864 it. */
6865 if (FRAME_MESSAGE_BUF (f))
6866 {
6867 if (m)
6868 {
6869 int len;
6870#ifdef NO_ARG_ARRAY
6871 char *a[3];
6872 a[0] = (char *) a1;
6873 a[1] = (char *) a2;
6874 a[2] = (char *) a3;
6875
6876 len = doprnt (FRAME_MESSAGE_BUF (f),
6877 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6878#else
6879 len = doprnt (FRAME_MESSAGE_BUF (f),
6880 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6881 (char **) &a1);
6882#endif /* NO_ARG_ARRAY */
6883
6884 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6885 }
6886 else
6887 message1 (0);
6888
6889 /* Print should start at the beginning of the message
6890 buffer next time. */
6891 message_buf_print = 0;
6892 }
6893 }
6894}
6895
6896
6897/* The non-logging version of message. */
6898
6899void
6900message_nolog (m, a1, a2, a3)
6901 char *m;
6902 EMACS_INT a1, a2, a3;
6903{
6904 Lisp_Object old_log_max;
6905 old_log_max = Vmessage_log_max;
6906 Vmessage_log_max = Qnil;
6907 message (m, a1, a2, a3);
6908 Vmessage_log_max = old_log_max;
6909}
6910
6911
c6e89d6c
GM
6912/* Display the current message in the current mini-buffer. This is
6913 only called from error handlers in process.c, and is not time
6914 critical. */
5f5c8ee5
GM
6915
6916void
6917update_echo_area ()
6918{
c6e89d6c
GM
6919 if (!NILP (echo_area_buffer[0]))
6920 {
6921 Lisp_Object string;
6922 string = Fcurrent_message ();
2311178e 6923 message3 (string, SBYTES (string),
c6e89d6c
GM
6924 !NILP (current_buffer->enable_multibyte_characters));
6925 }
6926}
6927
6928
a67e162b
RS
6929/* Make sure echo area buffers in `echo_buffers' are live.
6930 If they aren't, make new ones. */
5bcfeb49
GM
6931
6932static void
6933ensure_echo_area_buffers ()
6934{
6935 int i;
6936
6937 for (i = 0; i < 2; ++i)
6938 if (!BUFFERP (echo_buffer[i])
6939 || NILP (XBUFFER (echo_buffer[i])->name))
6940 {
6941 char name[30];
ff3d9573
GM
6942 Lisp_Object old_buffer;
6943 int j;
6944
6945 old_buffer = echo_buffer[i];
5bcfeb49
GM
6946 sprintf (name, " *Echo Area %d*", i);
6947 echo_buffer[i] = Fget_buffer_create (build_string (name));
ad4f174e 6948 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
ff3d9573
GM
6949
6950 for (j = 0; j < 2; ++j)
6951 if (EQ (old_buffer, echo_area_buffer[j]))
6952 echo_area_buffer[j] = echo_buffer[i];
5bcfeb49
GM
6953 }
6954}
6955
6956
23a96c77 6957/* Call FN with args A1..A4 with either the current or last displayed
c6e89d6c
GM
6958 echo_area_buffer as current buffer.
6959
6960 WHICH zero means use the current message buffer
6961 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6962 from echo_buffer[] and clear it.
6963
6964 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6965 suitable buffer from echo_buffer[] and clear it.
6966
6967 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6968 that the current message becomes the last displayed one, make
6969 choose a suitable buffer for echo_area_buffer[0], and clear it.
6970
4b41cebb 6971 Value is what FN returns. */
c6e89d6c
GM
6972
6973static int
23a96c77 6974with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
c6e89d6c
GM
6975 struct window *w;
6976 int which;
23dd2d97
KR
6977 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6978 EMACS_INT a1;
6979 Lisp_Object a2;
6980 EMACS_INT a3, a4;
c6e89d6c
GM
6981{
6982 Lisp_Object buffer;
15e26c76 6983 int this_one, the_other, clear_buffer_p, rc;
331379bf 6984 int count = SPECPDL_INDEX ();
c6e89d6c 6985
9583b2bb 6986 /* If buffers aren't live, make new ones. */
5bcfeb49 6987 ensure_echo_area_buffers ();
c6e89d6c
GM
6988
6989 clear_buffer_p = 0;
2311178e 6990
c6e89d6c
GM
6991 if (which == 0)
6992 this_one = 0, the_other = 1;
6993 else if (which > 0)
6994 this_one = 1, the_other = 0;
5f5c8ee5 6995 else
c6e89d6c
GM
6996 {
6997 this_one = 0, the_other = 1;
6998 clear_buffer_p = 1;
2311178e 6999
c6e89d6c
GM
7000 /* We need a fresh one in case the current echo buffer equals
7001 the one containing the last displayed echo area message. */
7002 if (!NILP (echo_area_buffer[this_one])
7003 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
7004 echo_area_buffer[this_one] = Qnil;
c6e89d6c
GM
7005 }
7006
7007 /* Choose a suitable buffer from echo_buffer[] is we don't
7008 have one. */
7009 if (NILP (echo_area_buffer[this_one]))
7010 {
7011 echo_area_buffer[this_one]
7012 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7013 ? echo_buffer[the_other]
7014 : echo_buffer[this_one]);
7015 clear_buffer_p = 1;
7016 }
7017
7018 buffer = echo_area_buffer[this_one];
7019
1013f4e3
GM
7020 /* Don't get confused by reusing the buffer used for echoing
7021 for a different purpose. */
032906b1 7022 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
1013f4e3
GM
7023 cancel_echoing ();
7024
c6e89d6c
GM
7025 record_unwind_protect (unwind_with_echo_area_buffer,
7026 with_echo_area_buffer_unwind_data (w));
7027
7028 /* Make the echo area buffer current. Note that for display
7029 purposes, it is not necessary that the displayed window's buffer
7030 == current_buffer, except for text property lookup. So, let's
7031 only set that buffer temporarily here without doing a full
7032 Fset_window_buffer. We must also change w->pointm, though,
7033 because otherwise an assertions in unshow_buffer fails, and Emacs
7034 aborts. */
9142dd5b 7035 set_buffer_internal_1 (XBUFFER (buffer));
c6e89d6c
GM
7036 if (w)
7037 {
7038 w->buffer = buffer;
7039 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7040 }
ad4f174e 7041
c6e89d6c
GM
7042 current_buffer->undo_list = Qt;
7043 current_buffer->read_only = Qnil;
bbbf6d06 7044 specbind (Qinhibit_read_only, Qt);
0b04fa5f 7045 specbind (Qinhibit_modification_hooks, Qt);
c6e89d6c
GM
7046
7047 if (clear_buffer_p && Z > BEG)
7048 del_range (BEG, Z);
7049
7050 xassert (BEGV >= BEG);
7051 xassert (ZV <= Z && ZV >= BEGV);
7052
23a96c77 7053 rc = fn (a1, a2, a3, a4);
c6e89d6c
GM
7054
7055 xassert (BEGV >= BEG);
7056 xassert (ZV <= Z && ZV >= BEGV);
7057
7058 unbind_to (count, Qnil);
7059 return rc;
5f5c8ee5
GM
7060}
7061
7062
c6e89d6c
GM
7063/* Save state that should be preserved around the call to the function
7064 FN called in with_echo_area_buffer. */
5f5c8ee5 7065
c6e89d6c
GM
7066static Lisp_Object
7067with_echo_area_buffer_unwind_data (w)
7068 struct window *w;
5f5c8ee5 7069{
c6e89d6c
GM
7070 int i = 0;
7071 Lisp_Object vector;
5f5c8ee5 7072
c6e89d6c
GM
7073 /* Reduce consing by keeping one vector in
7074 Vwith_echo_area_save_vector. */
7075 vector = Vwith_echo_area_save_vector;
7076 Vwith_echo_area_save_vector = Qnil;
2311178e 7077
c6e89d6c 7078 if (NILP (vector))
9142dd5b 7079 vector = Fmake_vector (make_number (7), Qnil);
2311178e 7080
a61b7058
GM
7081 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7082 AREF (vector, i) = Vdeactivate_mark, ++i;
7083 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
2311178e 7084
c6e89d6c
GM
7085 if (w)
7086 {
a61b7058
GM
7087 XSETWINDOW (AREF (vector, i), w); ++i;
7088 AREF (vector, i) = w->buffer; ++i;
7089 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7090 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
c6e89d6c
GM
7091 }
7092 else
7093 {
7094 int end = i + 4;
a61b7058
GM
7095 for (; i < end; ++i)
7096 AREF (vector, i) = Qnil;
c6e89d6c 7097 }
5f5c8ee5 7098
a61b7058 7099 xassert (i == ASIZE (vector));
c6e89d6c
GM
7100 return vector;
7101}
5f5c8ee5 7102
5f5c8ee5 7103
c6e89d6c
GM
7104/* Restore global state from VECTOR which was created by
7105 with_echo_area_buffer_unwind_data. */
7106
7107static Lisp_Object
7108unwind_with_echo_area_buffer (vector)
7109 Lisp_Object vector;
7110{
bbbf6d06
GM
7111 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7112 Vdeactivate_mark = AREF (vector, 1);
7113 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
c6e89d6c 7114
bbbf6d06 7115 if (WINDOWP (AREF (vector, 3)))
c6e89d6c
GM
7116 {
7117 struct window *w;
7118 Lisp_Object buffer, charpos, bytepos;
2311178e 7119
bbbf6d06
GM
7120 w = XWINDOW (AREF (vector, 3));
7121 buffer = AREF (vector, 4);
7122 charpos = AREF (vector, 5);
7123 bytepos = AREF (vector, 6);
2311178e 7124
c6e89d6c
GM
7125 w->buffer = buffer;
7126 set_marker_both (w->pointm, buffer,
7127 XFASTINT (charpos), XFASTINT (bytepos));
7128 }
7129
7130 Vwith_echo_area_save_vector = vector;
7131 return Qnil;
7132}
7133
7134
7135/* Set up the echo area for use by print functions. MULTIBYTE_P
7136 non-zero means we will print multibyte. */
7137
7138void
7139setup_echo_area_for_printing (multibyte_p)
7140 int multibyte_p;
7141{
03b0a4b4
RS
7142 /* If we can't find an echo area any more, exit. */
7143 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7144 Fkill_emacs (Qnil);
7145
5bcfeb49
GM
7146 ensure_echo_area_buffers ();
7147
c6e89d6c
GM
7148 if (!message_buf_print)
7149 {
7150 /* A message has been output since the last time we printed.
7151 Choose a fresh echo area buffer. */
7152 if (EQ (echo_area_buffer[1], echo_buffer[0]))
2311178e 7153 echo_area_buffer[0] = echo_buffer[1];
c6e89d6c
GM
7154 else
7155 echo_area_buffer[0] = echo_buffer[0];
7156
7157 /* Switch to that buffer and clear it. */
7158 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
ab2c5f0a 7159 current_buffer->truncate_lines = Qnil;
2311178e 7160
c6e89d6c 7161 if (Z > BEG)
bbbf6d06 7162 {
331379bf 7163 int count = SPECPDL_INDEX ();
bbbf6d06 7164 specbind (Qinhibit_read_only, Qt);
a67e162b 7165 /* Note that undo recording is always disabled. */
bbbf6d06
GM
7166 del_range (BEG, Z);
7167 unbind_to (count, Qnil);
7168 }
c6e89d6c
GM
7169 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7170
7171 /* Set up the buffer for the multibyteness we need. */
7172 if (multibyte_p
7173 != !NILP (current_buffer->enable_multibyte_characters))
7174 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7175
7176 /* Raise the frame containing the echo area. */
7177 if (minibuffer_auto_raise)
7178 {
886bd6f2 7179 struct frame *sf = SELECTED_FRAME ();
c6e89d6c 7180 Lisp_Object mini_window;
886bd6f2 7181 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
7182 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7183 }
7184
8a4e3c0c 7185 message_log_maybe_newline ();
c6e89d6c
GM
7186 message_buf_print = 1;
7187 }
fa77249f
GM
7188 else
7189 {
7190 if (NILP (echo_area_buffer[0]))
7191 {
7192 if (EQ (echo_area_buffer[1], echo_buffer[0]))
2311178e 7193 echo_area_buffer[0] = echo_buffer[1];
fa77249f
GM
7194 else
7195 echo_area_buffer[0] = echo_buffer[0];
7196 }
2311178e 7197
fa77249f 7198 if (current_buffer != XBUFFER (echo_area_buffer[0]))
ab2c5f0a
GM
7199 {
7200 /* Someone switched buffers between print requests. */
7201 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7202 current_buffer->truncate_lines = Qnil;
7203 }
fa77249f 7204 }
c6e89d6c
GM
7205}
7206
7207
dd2eb166
GM
7208/* Display an echo area message in window W. Value is non-zero if W's
7209 height is changed. If display_last_displayed_message_p is
7210 non-zero, display the message that was last displayed, otherwise
7211 display the current message. */
c6e89d6c
GM
7212
7213static int
7214display_echo_area (w)
7215 struct window *w;
7216{
25edb08f
GM
7217 int i, no_message_p, window_height_changed_p, count;
7218
7219 /* Temporarily disable garbage collections while displaying the echo
7220 area. This is done because a GC can print a message itself.
7221 That message would modify the echo area buffer's contents while a
7222 redisplay of the buffer is going on, and seriously confuse
7223 redisplay. */
7224 count = inhibit_garbage_collection ();
dd2eb166
GM
7225
7226 /* If there is no message, we must call display_echo_area_1
7227 nevertheless because it resizes the window. But we will have to
7228 reset the echo_area_buffer in question to nil at the end because
7229 with_echo_area_buffer will sets it to an empty buffer. */
7230 i = display_last_displayed_message_p ? 1 : 0;
7231 no_message_p = NILP (echo_area_buffer[i]);
2311178e 7232
dd2eb166
GM
7233 window_height_changed_p
7234 = with_echo_area_buffer (w, display_last_displayed_message_p,
23a96c77 7235 display_echo_area_1,
23dd2d97 7236 (EMACS_INT) w, Qnil, 0, 0);
dd2eb166
GM
7237
7238 if (no_message_p)
7239 echo_area_buffer[i] = Qnil;
25edb08f
GM
7240
7241 unbind_to (count, Qnil);
dd2eb166 7242 return window_height_changed_p;
c6e89d6c
GM
7243}
7244
7245
7246/* Helper for display_echo_area. Display the current buffer which
23a96c77
GM
7247 contains the current echo area message in window W, a mini-window,
7248 a pointer to which is passed in A1. A2..A4 are currently not used.
c6e89d6c
GM
7249 Change the height of W so that all of the message is displayed.
7250 Value is non-zero if height of W was changed. */
7251
7252static int
23a96c77 7253display_echo_area_1 (a1, a2, a3, a4)
23dd2d97
KR
7254 EMACS_INT a1;
7255 Lisp_Object a2;
7256 EMACS_INT a3, a4;
c6e89d6c 7257{
23a96c77 7258 struct window *w = (struct window *) a1;
c6e89d6c 7259 Lisp_Object window;
c6e89d6c
GM
7260 struct text_pos start;
7261 int window_height_changed_p = 0;
7262
7263 /* Do this before displaying, so that we have a large enough glyph
7264 matrix for the display. */
92a90e89 7265 window_height_changed_p = resize_mini_window (w, 0);
c6e89d6c
GM
7266
7267 /* Display. */
7268 clear_glyph_matrix (w->desired_matrix);
7269 XSETWINDOW (window, w);
7270 SET_TEXT_POS (start, BEG, BEG_BYTE);
7271 try_window (window, start);
7272
c6e89d6c
GM
7273 return window_height_changed_p;
7274}
7275
7276
92a90e89 7277/* Resize the echo area window to exactly the size needed for the
6d004fea
GM
7278 currently displayed message, if there is one. If a mini-buffer
7279 is active, don't shrink it. */
92a90e89
GM
7280
7281void
308a74d8 7282resize_echo_area_exactly ()
92a90e89
GM
7283{
7284 if (BUFFERP (echo_area_buffer[0])
7285 && WINDOWP (echo_area_window))
7286 {
7287 struct window *w = XWINDOW (echo_area_window);
7288 int resized_p;
6d004fea
GM
7289 Lisp_Object resize_exactly;
7290
7291 if (minibuf_level == 0)
7292 resize_exactly = Qt;
7293 else
7294 resize_exactly = Qnil;
2311178e 7295
23a96c77 7296 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6d004fea 7297 (EMACS_INT) w, resize_exactly, 0, 0);
92a90e89
GM
7298 if (resized_p)
7299 {
7300 ++windows_or_buffers_changed;
7301 ++update_mode_lines;
7302 redisplay_internal (0);
7303 }
7304 }
7305}
7306
7307
23a96c77 7308/* Callback function for with_echo_area_buffer, when used from
308a74d8 7309 resize_echo_area_exactly. A1 contains a pointer to the window to
6d004fea
GM
7310 resize, EXACTLY non-nil means resize the mini-window exactly to the
7311 size of the text displayed. A3 and A4 are not used. Value is what
7312 resize_mini_window returns. */
23a96c77
GM
7313
7314static int
6d004fea 7315resize_mini_window_1 (a1, exactly, a3, a4)
23dd2d97 7316 EMACS_INT a1;
6d004fea 7317 Lisp_Object exactly;
23dd2d97 7318 EMACS_INT a3, a4;
23a96c77 7319{
6d004fea 7320 return resize_mini_window ((struct window *) a1, !NILP (exactly));
23a96c77
GM
7321}
7322
7323
92a90e89
GM
7324/* Resize mini-window W to fit the size of its contents. EXACT:P
7325 means size the window exactly to the size needed. Otherwise, it's
7326 only enlarged until W's buffer is empty. Value is non-zero if
4b41cebb 7327 the window height has been changed. */
c6e89d6c 7328
9472f927 7329int
92a90e89 7330resize_mini_window (w, exact_p)
c6e89d6c 7331 struct window *w;
92a90e89 7332 int exact_p;
c6e89d6c
GM
7333{
7334 struct frame *f = XFRAME (w->frame);
7335 int window_height_changed_p = 0;
7336
7337 xassert (MINI_WINDOW_P (w));
97cafc0f 7338
2913a9c0
GM
7339 /* Don't resize windows while redisplaying a window; it would
7340 confuse redisplay functions when the size of the window they are
7341 displaying changes from under them. Such a resizing can happen,
7342 for instance, when which-func prints a long message while
7343 we are running fontification-functions. We're running these
7344 functions with safe_call which binds inhibit-redisplay to t. */
7345 if (!NILP (Vinhibit_redisplay))
64c5be50 7346 return 0;
2311178e 7347
97cafc0f 7348 /* Nil means don't try to resize. */
6422c1d7 7349 if (NILP (Vresize_mini_windows)
fa3c6b4d 7350 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
97cafc0f 7351 return 0;
2311178e 7352
c6e89d6c
GM
7353 if (!FRAME_MINIBUF_ONLY_P (f))
7354 {
7355 struct it it;
dd2eb166 7356 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
da8b7f4f 7357 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
dd2eb166 7358 int height, max_height;
da8b7f4f 7359 int unit = FRAME_LINE_HEIGHT (f);
dd2eb166 7360 struct text_pos start;
1bfdbe43
GM
7361 struct buffer *old_current_buffer = NULL;
7362
7363 if (current_buffer != XBUFFER (w->buffer))
7364 {
7365 old_current_buffer = current_buffer;
7366 set_buffer_internal (XBUFFER (w->buffer));
7367 }
9142dd5b 7368
c6e89d6c 7369 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
c6e89d6c 7370
dd2eb166
GM
7371 /* Compute the max. number of lines specified by the user. */
7372 if (FLOATP (Vmax_mini_window_height))
da8b7f4f 7373 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
dd2eb166
GM
7374 else if (INTEGERP (Vmax_mini_window_height))
7375 max_height = XINT (Vmax_mini_window_height);
97cafc0f
GM
7376 else
7377 max_height = total_height / 4;
2311178e 7378
4b41cebb 7379 /* Correct that max. height if it's bogus. */
dd2eb166
GM
7380 max_height = max (1, max_height);
7381 max_height = min (total_height, max_height);
2311178e 7382
dd2eb166 7383 /* Find out the height of the text in the window. */
ad4f174e
GM
7384 if (it.truncate_lines_p)
7385 height = 1;
55b064bd 7386 else
ad4f174e
GM
7387 {
7388 last_height = 0;
7389 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7390 if (it.max_ascent == 0 && it.max_descent == 0)
7391 height = it.current_y + last_height;
7392 else
7393 height = it.current_y + it.max_ascent + it.max_descent;
3c4b7685 7394 height -= it.extra_line_spacing;
ad4f174e
GM
7395 height = (height + unit - 1) / unit;
7396 }
2311178e 7397
dd2eb166
GM
7398 /* Compute a suitable window start. */
7399 if (height > max_height)
7400 {
7401 height = max_height;
7402 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7403 move_it_vertically_backward (&it, (height - 1) * unit);
7404 start = it.current.pos;
7405 }
7406 else
7407 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7408 SET_MARKER_FROM_TEXT_POS (w->start, start);
c59c668a 7409
6422c1d7 7410 if (EQ (Vresize_mini_windows, Qgrow_only))
dd2eb166 7411 {
6422c1d7
GM
7412 /* Let it grow only, until we display an empty message, in which
7413 case the window shrinks again. */
da8b7f4f 7414 if (height > WINDOW_TOTAL_LINES (w))
6422c1d7 7415 {
da8b7f4f 7416 int old_height = WINDOW_TOTAL_LINES (w);
6422c1d7 7417 freeze_window_starts (f, 1);
da8b7f4f
KS
7418 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7419 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
6422c1d7 7420 }
da8b7f4f 7421 else if (height < WINDOW_TOTAL_LINES (w)
6422c1d7
GM
7422 && (exact_p || BEGV == ZV))
7423 {
da8b7f4f 7424 int old_height = WINDOW_TOTAL_LINES (w);
6422c1d7
GM
7425 freeze_window_starts (f, 0);
7426 shrink_mini_window (w);
da8b7f4f 7427 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
6422c1d7 7428 }
da448723 7429 }
2311178e 7430 else
da448723 7431 {
6422c1d7 7432 /* Always resize to exact size needed. */
da8b7f4f 7433 if (height > WINDOW_TOTAL_LINES (w))
6422c1d7 7434 {
da8b7f4f 7435 int old_height = WINDOW_TOTAL_LINES (w);
6422c1d7 7436 freeze_window_starts (f, 1);
da8b7f4f
KS
7437 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7438 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
6422c1d7 7439 }
da8b7f4f 7440 else if (height < WINDOW_TOTAL_LINES (w))
6422c1d7 7441 {
da8b7f4f 7442 int old_height = WINDOW_TOTAL_LINES (w);
6422c1d7
GM
7443 freeze_window_starts (f, 0);
7444 shrink_mini_window (w);
7445
7446 if (height)
7447 {
7448 freeze_window_starts (f, 1);
da8b7f4f 7449 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
6422c1d7 7450 }
2311178e 7451
da8b7f4f 7452 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
6422c1d7 7453 }
9142dd5b 7454 }
1bfdbe43
GM
7455
7456 if (old_current_buffer)
7457 set_buffer_internal (old_current_buffer);
c6e89d6c
GM
7458 }
7459
7460 return window_height_changed_p;
7461}
7462
7463
7464/* Value is the current message, a string, or nil if there is no
7465 current message. */
7466
7467Lisp_Object
7468current_message ()
7469{
7470 Lisp_Object msg;
7471
7472 if (NILP (echo_area_buffer[0]))
7473 msg = Qnil;
7474 else
7475 {
23a96c77 7476 with_echo_area_buffer (0, 0, current_message_1,
23dd2d97 7477 (EMACS_INT) &msg, Qnil, 0, 0);
c6e89d6c
GM
7478 if (NILP (msg))
7479 echo_area_buffer[0] = Qnil;
7480 }
2311178e 7481
c6e89d6c
GM
7482 return msg;
7483}
7484
7485
7486static int
23a96c77 7487current_message_1 (a1, a2, a3, a4)
23dd2d97
KR
7488 EMACS_INT a1;
7489 Lisp_Object a2;
7490 EMACS_INT a3, a4;
c6e89d6c 7491{
23a96c77 7492 Lisp_Object *msg = (Lisp_Object *) a1;
2311178e 7493
c6e89d6c
GM
7494 if (Z > BEG)
7495 *msg = make_buffer_string (BEG, Z, 1);
7496 else
7497 *msg = Qnil;
7498 return 0;
7499}
7500
7501
7502/* Push the current message on Vmessage_stack for later restauration
7503 by restore_message. Value is non-zero if the current message isn't
7504 empty. This is a relatively infrequent operation, so it's not
7505 worth optimizing. */
7506
7507int
7508push_message ()
7509{
7510 Lisp_Object msg;
7511 msg = current_message ();
7512 Vmessage_stack = Fcons (msg, Vmessage_stack);
7513 return STRINGP (msg);
7514}
7515
7516
7517/* Restore message display from the top of Vmessage_stack. */
7518
7519void
7520restore_message ()
7521{
7522 Lisp_Object msg;
2311178e 7523
c6e89d6c
GM
7524 xassert (CONSP (Vmessage_stack));
7525 msg = XCAR (Vmessage_stack);
7526 if (STRINGP (msg))
d5db4077 7527 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
c6e89d6c
GM
7528 else
7529 message3_nolog (msg, 0, 0);
7530}
7531
7532
37d66095
RS
7533/* Handler for record_unwind_protect calling pop_message. */
7534
7535Lisp_Object
7536pop_message_unwind (dummy)
7537 Lisp_Object dummy;
7538{
7539 pop_message ();
7540 return Qnil;
7541}
7542
c6e89d6c
GM
7543/* Pop the top-most entry off Vmessage_stack. */
7544
7545void
7546pop_message ()
7547{
7548 xassert (CONSP (Vmessage_stack));
7549 Vmessage_stack = XCDR (Vmessage_stack);
7550}
7551
7552
7553/* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7554 exits. If the stack is not empty, we have a missing pop_message
7555 somewhere. */
7556
7557void
7558check_message_stack ()
7559{
7560 if (!NILP (Vmessage_stack))
7561 abort ();
7562}
7563
7564
7565/* Truncate to NCHARS what will be displayed in the echo area the next
7566 time we display it---but don't redisplay it now. */
7567
7568void
7569truncate_echo_area (nchars)
7570 int nchars;
7571{
7572 if (nchars == 0)
7573 echo_area_buffer[0] = Qnil;
7574 /* A null message buffer means that the frame hasn't really been
7575 initialized yet. Error messages get reported properly by
7576 cmd_error, so this must be just an informative message; toss it. */
7577 else if (!noninteractive
7578 && INTERACTIVE
c6e89d6c 7579 && !NILP (echo_area_buffer[0]))
886bd6f2
GM
7580 {
7581 struct frame *sf = SELECTED_FRAME ();
7582 if (FRAME_MESSAGE_BUF (sf))
23dd2d97 7583 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
886bd6f2 7584 }
c6e89d6c
GM
7585}
7586
7587
7588/* Helper function for truncate_echo_area. Truncate the current
7589 message to at most NCHARS characters. */
7590
7591static int
23a96c77 7592truncate_message_1 (nchars, a2, a3, a4)
23dd2d97
KR
7593 EMACS_INT nchars;
7594 Lisp_Object a2;
7595 EMACS_INT a3, a4;
c6e89d6c
GM
7596{
7597 if (BEG + nchars < Z)
7598 del_range (BEG + nchars, Z);
7599 if (Z == BEG)
7600 echo_area_buffer[0] = Qnil;
7601 return 0;
7602}
7603
7604
7605/* Set the current message to a substring of S or STRING.
7606
7607 If STRING is a Lisp string, set the message to the first NBYTES
7608 bytes from STRING. NBYTES zero means use the whole string. If
7609 STRING is multibyte, the message will be displayed multibyte.
7610
7611 If S is not null, set the message to the first LEN bytes of S. LEN
7612 zero means use the whole string. MULTIBYTE_P non-zero means S is
7613 multibyte. Display the message multibyte in that case. */
7614
7615void
7616set_message (s, string, nbytes, multibyte_p)
50f80c2f 7617 const char *s;
c6e89d6c 7618 Lisp_Object string;
e3383b6f 7619 int nbytes, multibyte_p;
c6e89d6c
GM
7620{
7621 message_enable_multibyte
7622 = ((s && multibyte_p)
7623 || (STRINGP (string) && STRING_MULTIBYTE (string)));
2311178e 7624
23a96c77
GM
7625 with_echo_area_buffer (0, -1, set_message_1,
7626 (EMACS_INT) s, string, nbytes, multibyte_p);
c6e89d6c 7627 message_buf_print = 0;
21fdfb65 7628 help_echo_showing_p = 0;
c6e89d6c
GM
7629}
7630
7631
7632/* Helper function for set_message. Arguments have the same meaning
23a96c77
GM
7633 as there, with A1 corresponding to S and A2 corresponding to STRING
7634 This function is called with the echo area buffer being
c6e89d6c
GM
7635 current. */
7636
7637static int
23a96c77 7638set_message_1 (a1, a2, nbytes, multibyte_p)
23dd2d97
KR
7639 EMACS_INT a1;
7640 Lisp_Object a2;
7641 EMACS_INT nbytes, multibyte_p;
c6e89d6c 7642{
50f80c2f 7643 const char *s = (const char *) a1;
23dd2d97 7644 Lisp_Object string = a2;
2311178e 7645
c6e89d6c 7646 xassert (BEG == Z);
2311178e 7647
c6e89d6c
GM
7648 /* Change multibyteness of the echo buffer appropriately. */
7649 if (message_enable_multibyte
7650 != !NILP (current_buffer->enable_multibyte_characters))
7651 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7652
ad4f174e 7653 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
2311178e 7654
c6e89d6c
GM
7655 /* Insert new message at BEG. */
7656 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7657
7658 if (STRINGP (string))
7659 {
7660 int nchars;
2311178e 7661
c6e89d6c 7662 if (nbytes == 0)
2051c264 7663 nbytes = SBYTES (string);
c6e89d6c 7664 nchars = string_byte_to_char (string, nbytes);
2311178e 7665
c6e89d6c
GM
7666 /* This function takes care of single/multibyte conversion. We
7667 just have to ensure that the echo area buffer has the right
7668 setting of enable_multibyte_characters. */
7669 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7670 }
7671 else if (s)
7672 {
7673 if (nbytes == 0)
7674 nbytes = strlen (s);
2311178e 7675
c6e89d6c
GM
7676 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7677 {
7678 /* Convert from multi-byte to single-byte. */
7679 int i, c, n;
7680 unsigned char work[1];
2311178e 7681
c6e89d6c
GM
7682 /* Convert a multibyte string to single-byte. */
7683 for (i = 0; i < nbytes; i += n)
7684 {
7685 c = string_char_and_length (s + i, nbytes - i, &n);
7686 work[0] = (SINGLE_BYTE_CHAR_P (c)
7687 ? c
7688 : multibyte_char_to_unibyte (c, Qnil));
7689 insert_1_both (work, 1, 1, 1, 0, 0);
7690 }
7691 }
7692 else if (!multibyte_p
7693 && !NILP (current_buffer->enable_multibyte_characters))
7694 {
7695 /* Convert from single-byte to multi-byte. */
7696 int i, c, n;
50f80c2f 7697 const unsigned char *msg = (const unsigned char *) s;
260a86a0 7698 unsigned char str[MAX_MULTIBYTE_LENGTH];
2311178e 7699
c6e89d6c
GM
7700 /* Convert a single-byte string to multibyte. */
7701 for (i = 0; i < nbytes; i++)
7702 {
7703 c = unibyte_char_to_multibyte (msg[i]);
260a86a0
KH
7704 n = CHAR_STRING (c, str);
7705 insert_1_both (str, 1, n, 1, 0, 0);
c6e89d6c
GM
7706 }
7707 }
7708 else
7709 insert_1 (s, nbytes, 1, 0, 0);
7710 }
7711
7712 return 0;
7713}
7714
7715
7716/* Clear messages. CURRENT_P non-zero means clear the current
7717 message. LAST_DISPLAYED_P non-zero means clear the message
7718 last displayed. */
7719
7720void
7721clear_message (current_p, last_displayed_p)
7722 int current_p, last_displayed_p;
7723{
7724 if (current_p)
6e019995
GM
7725 {
7726 echo_area_buffer[0] = Qnil;
7727 message_cleared_p = 1;
7728 }
2311178e 7729
c6e89d6c
GM
7730 if (last_displayed_p)
7731 echo_area_buffer[1] = Qnil;
2311178e 7732
c6e89d6c
GM
7733 message_buf_print = 0;
7734}
7735
7736/* Clear garbaged frames.
7737
7738 This function is used where the old redisplay called
7739 redraw_garbaged_frames which in turn called redraw_frame which in
7740 turn called clear_frame. The call to clear_frame was a source of
7741 flickering. I believe a clear_frame is not necessary. It should
7742 suffice in the new redisplay to invalidate all current matrices,
7743 and ensure a complete redisplay of all windows. */
7744
7745static void
7746clear_garbaged_frames ()
7747{
5f5c8ee5
GM
7748 if (frame_garbaged)
7749 {
5f5c8ee5 7750 Lisp_Object tail, frame;
5fb96e96 7751 int changed_count = 0;
2311178e 7752
5f5c8ee5
GM
7753 FOR_EACH_FRAME (tail, frame)
7754 {
7755 struct frame *f = XFRAME (frame);
2311178e 7756
5f5c8ee5
GM
7757 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7758 {
6bb95882 7759 if (f->resized_p)
6d925726
KS
7760 {
7761 Fredraw_frame (frame);
7762 f->force_flush_display_p = 1;
7763 }
5f5c8ee5 7764 clear_current_matrices (f);
5fb96e96 7765 changed_count++;
5f5c8ee5 7766 f->garbaged = 0;
6bb95882 7767 f->resized_p = 0;
5f5c8ee5
GM
7768 }
7769 }
7770
7771 frame_garbaged = 0;
5fb96e96
RS
7772 if (changed_count)
7773 ++windows_or_buffers_changed;
5f5c8ee5 7774 }
c6e89d6c 7775}
5f5c8ee5 7776
5f5c8ee5 7777
886bd6f2
GM
7778/* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7779 is non-zero update selected_frame. Value is non-zero if the
c6e89d6c 7780 mini-windows height has been changed. */
5f5c8ee5 7781
c6e89d6c
GM
7782static int
7783echo_area_display (update_frame_p)
7784 int update_frame_p;
7785{
7786 Lisp_Object mini_window;
7787 struct window *w;
7788 struct frame *f;
7789 int window_height_changed_p = 0;
886bd6f2 7790 struct frame *sf = SELECTED_FRAME ();
c6e89d6c 7791
886bd6f2 7792 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
7793 w = XWINDOW (mini_window);
7794 f = XFRAME (WINDOW_FRAME (w));
7795
7796 /* Don't display if frame is invisible or not yet initialized. */
7797 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7798 return 0;
5f5c8ee5 7799
1a578e9b 7800/* The terminal frame is used as the first Emacs frame on the Mac OS. */
e0f712ba 7801#ifndef MAC_OS8
1ab3e082 7802#ifdef HAVE_WINDOW_SYSTEM
c6e89d6c
GM
7803 /* When Emacs starts, selected_frame may be a visible terminal
7804 frame, even if we run under a window system. If we let this
7805 through, a message would be displayed on the terminal. */
2311178e 7806 if (EQ (selected_frame, Vterminal_frame)
622e3754 7807 && !NILP (Vwindow_system))
c6e89d6c 7808 return 0;
1ab3e082 7809#endif /* HAVE_WINDOW_SYSTEM */
1a578e9b 7810#endif
c6e89d6c
GM
7811
7812 /* Redraw garbaged frames. */
7813 if (frame_garbaged)
7814 clear_garbaged_frames ();
7815
7816 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7817 {
7818 echo_area_window = mini_window;
7819 window_height_changed_p = display_echo_area (w);
5f5c8ee5 7820 w->must_be_updated_p = 1;
c59c668a 7821
d4358b37
GM
7822 /* Update the display, unless called from redisplay_internal.
7823 Also don't update the screen during redisplay itself. The
7824 update will happen at the end of redisplay, and an update
7825 here could cause confusion. */
7826 if (update_frame_p && !redisplaying_p)
5f5c8ee5 7827 {
715e84c9 7828 int n = 0;
edc68111 7829
715e84c9
GM
7830 /* If the display update has been interrupted by pending
7831 input, update mode lines in the frame. Due to the
7832 pending input, it might have been that redisplay hasn't
7833 been called, so that mode lines above the echo area are
7834 garbaged. This looks odd, so we prevent it here. */
7835 if (!display_completed)
7836 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
2311178e 7837
8af1308e
GM
7838 if (window_height_changed_p
7839 /* Don't do this if Emacs is shutting down. Redisplay
7840 needs to run hooks. */
7841 && !NILP (Vrun_hooks))
c59c668a 7842 {
c06017fb
GM
7843 /* Must update other windows. Likewise as in other
7844 cases, don't let this update be interrupted by
7845 pending input. */
331379bf 7846 int count = SPECPDL_INDEX ();
c06017fb 7847 specbind (Qredisplay_dont_pause, Qt);
edc68111 7848 windows_or_buffers_changed = 1;
c59c668a 7849 redisplay_internal (0);
c06017fb 7850 unbind_to (count, Qnil);
c59c668a 7851 }
715e84c9 7852 else if (FRAME_WINDOW_P (f) && n == 0)
5f5c8ee5 7853 {
edc68111 7854 /* Window configuration is the same as before.
715e84c9
GM
7855 Can do with a display update of the echo area,
7856 unless we displayed some mode lines. */
5f5c8ee5
GM
7857 update_single_window (w, 1);
7858 rif->flush_display (f);
7859 }
7860 else
7861 update_frame (f, 1, 1);
31b6671b
GM
7862
7863 /* If cursor is in the echo area, make sure that the next
7864 redisplay displays the minibuffer, so that the cursor will
7865 be replaced with what the minibuffer wants. */
7866 if (cursor_in_echo_area)
7867 ++windows_or_buffers_changed;
5f5c8ee5
GM
7868 }
7869 }
7870 else if (!EQ (mini_window, selected_window))
7871 windows_or_buffers_changed++;
c59c668a
GM
7872
7873 /* Last displayed message is now the current message. */
dd2eb166 7874 echo_area_buffer[1] = echo_area_buffer[0];
2311178e 7875
5f5c8ee5
GM
7876 /* Prevent redisplay optimization in redisplay_internal by resetting
7877 this_line_start_pos. This is done because the mini-buffer now
7878 displays the message instead of its buffer text. */
7879 if (EQ (mini_window, selected_window))
7880 CHARPOS (this_line_start_pos) = 0;
c6e89d6c
GM
7881
7882 return window_height_changed_p;
5f5c8ee5
GM
7883}
7884
7885
7886\f
7887/***********************************************************************
7888 Frame Titles
7889 ***********************************************************************/
7890
7891
8143e6ab
KS
7892/* The frame title buffering code is also used by Fformat_mode_line.
7893 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
5f5c8ee5
GM
7894
7895/* A buffer for constructing frame titles in it; allocated from the
7896 heap in init_xdisp and resized as needed in store_frame_title_char. */
7897
7898static char *frame_title_buf;
7899
7900/* The buffer's end, and a current output position in it. */
7901
7902static char *frame_title_buf_end;
7903static char *frame_title_ptr;
7904
7905
7906/* Store a single character C for the frame title in frame_title_buf.
7907 Re-allocate frame_title_buf if necessary. */
7908
7909static void
5d15cc8f
DL
7910#ifdef PROTOTYPES
7911store_frame_title_char (char c)
7912#else
5f5c8ee5
GM
7913store_frame_title_char (c)
7914 char c;
5d15cc8f 7915#endif
5f5c8ee5
GM
7916{
7917 /* If output position has reached the end of the allocated buffer,
7918 double the buffer's size. */
7919 if (frame_title_ptr == frame_title_buf_end)
7920 {
7921 int len = frame_title_ptr - frame_title_buf;
7922 int new_size = 2 * len * sizeof *frame_title_buf;
7923 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7924 frame_title_buf_end = frame_title_buf + new_size;
7925 frame_title_ptr = frame_title_buf + len;
7926 }
7927
7928 *frame_title_ptr++ = c;
7929}
7930
7931
7932/* Store part of a frame title in frame_title_buf, beginning at
d26b89b8
KH
7933 frame_title_ptr. STR is the string to store. Do not copy
7934 characters that yield more columns than PRECISION; PRECISION <= 0
7935 means copy the whole string. Pad with spaces until FIELD_WIDTH
7936 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7937 pad. Called from display_mode_element when it is used to build a
7938 frame title. */
5f5c8ee5
GM
7939
7940static int
7941store_frame_title (str, field_width, precision)
50f80c2f 7942 const unsigned char *str;
5f5c8ee5
GM
7943 int field_width, precision;
7944{
7945 int n = 0;
3b552d56 7946 int dummy, nbytes;
5f5c8ee5
GM
7947
7948 /* Copy at most PRECISION chars from STR. */
d26b89b8
KH
7949 nbytes = strlen (str);
7950 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7951 while (nbytes--)
7952 store_frame_title_char (*str++);
5f5c8ee5
GM
7953
7954 /* Fill up with spaces until FIELD_WIDTH reached. */
7955 while (field_width > 0
7956 && n < field_width)
7957 {
7958 store_frame_title_char (' ');
7959 ++n;
7960 }
7961
7962 return n;
7963}
7964
8143e6ab 7965#ifdef HAVE_WINDOW_SYSTEM
5f5c8ee5
GM
7966
7967/* Set the title of FRAME, if it has changed. The title format is
7968 Vicon_title_format if FRAME is iconified, otherwise it is
7969 frame_title_format. */
7970
7971static void
7972x_consider_frame_title (frame)
7973 Lisp_Object frame;
7974{
7975 struct frame *f = XFRAME (frame);
7976
7977 if (FRAME_WINDOW_P (f)
7978 || FRAME_MINIBUF_ONLY_P (f)
7979 || f->explicit_name)
7980 {
7981 /* Do we have more than one visible frame on this X display? */
7982 Lisp_Object tail;
7983 Lisp_Object fmt;
7984 struct buffer *obuf;
7985 int len;
7986 struct it it;
7987
9472f927 7988 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
5f5c8ee5 7989 {
74779f52
JR
7990 Lisp_Object other_frame = XCAR (tail);
7991 struct frame *tf = XFRAME (other_frame);
5f5c8ee5 7992
2311178e 7993 if (tf != f
5f5c8ee5
GM
7994 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7995 && !FRAME_MINIBUF_ONLY_P (tf)
74779f52 7996 && !EQ (other_frame, tip_frame)
5f5c8ee5
GM
7997 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7998 break;
7999 }
8000
8001 /* Set global variable indicating that multiple frames exist. */
8002 multiple_frames = CONSP (tail);
8003
8004 /* Switch to the buffer of selected window of the frame. Set up
8005 frame_title_ptr so that display_mode_element will output into it;
8006 then display the title. */
8007 obuf = current_buffer;
bb336f8d 8008 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
5f5c8ee5
GM
8009 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8010 frame_title_ptr = frame_title_buf;
8011 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8012 NULL, DEFAULT_FACE_ID);
c53a1624 8013 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
d26b89b8 8014 len = frame_title_ptr - frame_title_buf;
5f5c8ee5 8015 frame_title_ptr = NULL;
bb336f8d 8016 set_buffer_internal_1 (obuf);
5f5c8ee5
GM
8017
8018 /* Set the title only if it's changed. This avoids consing in
8019 the common case where it hasn't. (If it turns out that we've
8020 already wasted too much time by walking through the list with
8021 display_mode_element, then we might need to optimize at a
8022 higher level than this.) */
2311178e 8023 if (! STRINGP (f->name)
2051c264
GM
8024 || SBYTES (f->name) != len
8025 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
5f5c8ee5
GM
8026 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
8027 }
8028}
8029
5f5c8ee5
GM
8030#endif /* not HAVE_WINDOW_SYSTEM */
8031
8032
8033
8034\f
8035/***********************************************************************
8036 Menu Bars
8037 ***********************************************************************/
8038
8039
8040/* Prepare for redisplay by updating menu-bar item lists when
8041 appropriate. This can call eval. */
8042
8043void
8044prepare_menu_bars ()
8045{
8046 int all_windows;
8047 struct gcpro gcpro1, gcpro2;
8048 struct frame *f;
6b5c4794 8049 Lisp_Object tooltip_frame;
5f5c8ee5 8050
86ffe5cd 8051#ifdef HAVE_WINDOW_SYSTEM
5f5c8ee5
GM
8052 tooltip_frame = tip_frame;
8053#else
6b5c4794 8054 tooltip_frame = Qnil;
5f5c8ee5
GM
8055#endif
8056
8057 /* Update all frame titles based on their buffer names, etc. We do
8058 this before the menu bars so that the buffer-menu will show the
8059 up-to-date frame titles. */
8060#ifdef HAVE_WINDOW_SYSTEM
8061 if (windows_or_buffers_changed || update_mode_lines)
8062 {
8063 Lisp_Object tail, frame;
8064
8065 FOR_EACH_FRAME (tail, frame)
8066 {
8067 f = XFRAME (frame);
6b5c4794 8068 if (!EQ (frame, tooltip_frame)
5f5c8ee5
GM
8069 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8070 x_consider_frame_title (frame);
8071 }
8072 }
8073#endif /* HAVE_WINDOW_SYSTEM */
8074
8075 /* Update the menu bar item lists, if appropriate. This has to be
8076 done before any actual redisplay or generation of display lines. */
2311178e 8077 all_windows = (update_mode_lines
5f5c8ee5
GM
8078 || buffer_shared > 1
8079 || windows_or_buffers_changed);
8080 if (all_windows)
8081 {
8082 Lisp_Object tail, frame;
331379bf 8083 int count = SPECPDL_INDEX ();
5f5c8ee5
GM
8084
8085 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8086
8087 FOR_EACH_FRAME (tail, frame)
8088 {
8089 f = XFRAME (frame);
8090
8091 /* Ignore tooltip frame. */
6b5c4794 8092 if (EQ (frame, tooltip_frame))
5f5c8ee5 8093 continue;
2311178e 8094
5f5c8ee5
GM
8095 /* If a window on this frame changed size, report that to
8096 the user and clear the size-change flag. */
8097 if (FRAME_WINDOW_SIZES_CHANGED (f))
8098 {
8099 Lisp_Object functions;
2311178e 8100
5f5c8ee5
GM
8101 /* Clear flag first in case we get an error below. */
8102 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8103 functions = Vwindow_size_change_functions;
8104 GCPRO2 (tail, functions);
2311178e 8105
5f5c8ee5
GM
8106 while (CONSP (functions))
8107 {
8108 call1 (XCAR (functions), frame);
8109 functions = XCDR (functions);
8110 }
8111 UNGCPRO;
8112 }
2311178e 8113
5f5c8ee5
GM
8114 GCPRO1 (tail);
8115 update_menu_bar (f, 0);
8116#ifdef HAVE_WINDOW_SYSTEM
e037b9ec 8117 update_tool_bar (f, 0);
5f5c8ee5
GM
8118#endif
8119 UNGCPRO;
8120 }
8121
8122 unbind_to (count, Qnil);
8123 }
8124 else
8125 {
886bd6f2
GM
8126 struct frame *sf = SELECTED_FRAME ();
8127 update_menu_bar (sf, 1);
5f5c8ee5 8128#ifdef HAVE_WINDOW_SYSTEM
886bd6f2 8129 update_tool_bar (sf, 1);
5f5c8ee5
GM
8130#endif
8131 }
8132
8133 /* Motif needs this. See comment in xmenu.c. Turn it off when
8134 pending_menu_activation is not defined. */
8135#ifdef USE_X_TOOLKIT
8136 pending_menu_activation = 0;
8137#endif
8138}
8139
8140
8141/* Update the menu bar item list for frame F. This has to be done
8142 before we start to fill in any display lines, because it can call
8143 eval.
8144
8145 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8146
8147static void
8148update_menu_bar (f, save_match_data)
8149 struct frame *f;
8150 int save_match_data;
8151{
8152 Lisp_Object window;
8153 register struct window *w;
8154
e1477f43
GM
8155 /* If called recursively during a menu update, do nothing. This can
8156 happen when, for instance, an activate-menubar-hook causes a
8157 redisplay. */
8158 if (inhibit_menubar_update)
8159 return;
8160
5f5c8ee5
GM
8161 window = FRAME_SELECTED_WINDOW (f);
8162 w = XWINDOW (window);
2311178e 8163
84f2e615 8164#if 0 /* The if statement below this if statement used to include the
a5f08374
RS
8165 condition !NILP (w->update_mode_line), rather than using
8166 update_mode_lines directly, and this if statement may have
8167 been added to make that condition work. Now the if
2311178e 8168 statement below matches its comment, this isn't needed. */
5f5c8ee5
GM
8169 if (update_mode_lines)
8170 w->update_mode_line = Qt;
a5f08374 8171#endif
5f5c8ee5
GM
8172
8173 if (FRAME_WINDOW_P (f)
8174 ?
488dd4c4
JD
8175#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8176 || defined (USE_GTK)
2311178e 8177 FRAME_EXTERNAL_MENU_BAR (f)
5f5c8ee5
GM
8178#else
8179 FRAME_MENU_BAR_LINES (f) > 0
8180#endif
8181 : FRAME_MENU_BAR_LINES (f) > 0)
8182 {
8183 /* If the user has switched buffers or windows, we need to
8184 recompute to reflect the new bindings. But we'll
8185 recompute when update_mode_lines is set too; that means
8186 that people can use force-mode-line-update to request
8187 that the menu bar be recomputed. The adverse effect on
8188 the rest of the redisplay algorithm is about the same as
8189 windows_or_buffers_changed anyway. */
8190 if (windows_or_buffers_changed
a5f08374
RS
8191 /* This used to test w->update_mode_line, but we believe
8192 there is no need to recompute the menu in that case. */
8193 || update_mode_lines
5f5c8ee5
GM
8194 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8195 < BUF_MODIFF (XBUFFER (w->buffer)))
8196 != !NILP (w->last_had_star))
8197 || ((!NILP (Vtransient_mark_mode)
8198 && !NILP (XBUFFER (w->buffer)->mark_active))
8199 != !NILP (w->region_showing)))
8200 {
8201 struct buffer *prev = current_buffer;
331379bf 8202 int count = SPECPDL_INDEX ();
5f5c8ee5 8203
e1477f43
GM
8204 specbind (Qinhibit_menubar_update, Qt);
8205
5f5c8ee5
GM
8206 set_buffer_internal_1 (XBUFFER (w->buffer));
8207 if (save_match_data)
8208 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8209 if (NILP (Voverriding_local_map_menu_flag))
8210 {
8211 specbind (Qoverriding_terminal_local_map, Qnil);
8212 specbind (Qoverriding_local_map, Qnil);
8213 }
8214
8215 /* Run the Lucid hook. */
65048e97 8216 safe_run_hooks (Qactivate_menubar_hook);
2311178e 8217
5f5c8ee5
GM
8218 /* If it has changed current-menubar from previous value,
8219 really recompute the menu-bar from the value. */
8220 if (! NILP (Vlucid_menu_bar_dirty_flag))
8221 call0 (Qrecompute_lucid_menubar);
2311178e 8222
5f5c8ee5
GM
8223 safe_run_hooks (Qmenu_bar_update_hook);
8224 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
2311178e 8225
5f5c8ee5 8226 /* Redisplay the menu bar in case we changed it. */
488dd4c4
JD
8227#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8228 || defined (USE_GTK)
1a578e9b 8229 if (FRAME_WINDOW_P (f)
e0f712ba 8230#if defined (MAC_OS)
1a578e9b
AC
8231 /* All frames on Mac OS share the same menubar. So only the
8232 selected frame should be allowed to set it. */
8233 && f == SELECTED_FRAME ()
8234#endif
8235 )
5f5c8ee5
GM
8236 set_frame_menubar (f, 0, 0);
8237 else
8238 /* On a terminal screen, the menu bar is an ordinary screen
8239 line, and this makes it get updated. */
8240 w->update_mode_line = Qt;
488dd4c4 8241#else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
5f5c8ee5
GM
8242 /* In the non-toolkit version, the menu bar is an ordinary screen
8243 line, and this makes it get updated. */
8244 w->update_mode_line = Qt;
488dd4c4 8245#endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
5f5c8ee5
GM
8246
8247 unbind_to (count, Qnil);
8248 set_buffer_internal_1 (prev);
8249 }
8250 }
8251}
8252
8253
8254\f
fa3c6b4d
KS
8255/***********************************************************************
8256 Output Cursor
8257 ***********************************************************************/
8258
1dabd0cf
KS
8259#ifdef HAVE_WINDOW_SYSTEM
8260
fa3c6b4d
KS
8261/* EXPORT:
8262 Nominal cursor position -- where to draw output.
8263 HPOS and VPOS are window relative glyph matrix coordinates.
8264 X and Y are window relative pixel coordinates. */
8265
8266struct cursor_pos output_cursor;
8267
8268
8269/* EXPORT:
8270 Set the global variable output_cursor to CURSOR. All cursor
8271 positions are relative to updated_window. */
8272
8273void
8274set_output_cursor (cursor)
8275 struct cursor_pos *cursor;
8276{
8277 output_cursor.hpos = cursor->hpos;
8278 output_cursor.vpos = cursor->vpos;
8279 output_cursor.x = cursor->x;
8280 output_cursor.y = cursor->y;
8281}
8282
8283
8284/* EXPORT for RIF:
8285 Set a nominal cursor position.
8286
8287 HPOS and VPOS are column/row positions in a window glyph matrix. X
8288 and Y are window text area relative pixel positions.
8289
8290 If this is done during an update, updated_window will contain the
8291 window that is being updated and the position is the future output
8292 cursor position for that window. If updated_window is null, use
8293 selected_window and display the cursor at the given position. */
8294
8295void
8296x_cursor_to (vpos, hpos, y, x)
8297 int vpos, hpos, y, x;
8298{
8299 struct window *w;
8300
8301 /* If updated_window is not set, work on selected_window. */
8302 if (updated_window)
8303 w = updated_window;
8304 else
8305 w = XWINDOW (selected_window);
8306
8307 /* Set the output cursor. */
8308 output_cursor.hpos = hpos;
8309 output_cursor.vpos = vpos;
8310 output_cursor.x = x;
8311 output_cursor.y = y;
8312
8313 /* If not called as part of an update, really display the cursor.
8314 This will also set the cursor position of W. */
8315 if (updated_window == NULL)
8316 {
8317 BLOCK_INPUT;
8318 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8319 if (rif->flush_display_optional)
8320 rif->flush_display_optional (SELECTED_FRAME ());
8321 UNBLOCK_INPUT;
8322 }
8323}
8324
1dabd0cf 8325#endif /* HAVE_WINDOW_SYSTEM */
fa3c6b4d
KS
8326
8327\f
5f5c8ee5 8328/***********************************************************************
e037b9ec 8329 Tool-bars
5f5c8ee5
GM
8330 ***********************************************************************/
8331
8332#ifdef HAVE_WINDOW_SYSTEM
8333
fa3c6b4d
KS
8334/* Where the mouse was last time we reported a mouse event. */
8335
8336FRAME_PTR last_mouse_frame;
8337
8338/* Tool-bar item index of the item on which a mouse button was pressed
8339 or -1. */
8340
8341int last_tool_bar_item;
8342
8343
e037b9ec 8344/* Update the tool-bar item list for frame F. This has to be done
5f5c8ee5
GM
8345 before we start to fill in any display lines. Called from
8346 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8347 and restore it here. */
8348
8349static void
e037b9ec 8350update_tool_bar (f, save_match_data)
5f5c8ee5
GM
8351 struct frame *f;
8352 int save_match_data;
8353{
488dd4c4 8354#ifdef USE_GTK
810f2256 8355 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
488dd4c4
JD
8356#else
8357 int do_update = WINDOWP (f->tool_bar_window)
da8b7f4f 8358 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
488dd4c4
JD
8359#endif
8360
8361 if (do_update)
5f5c8ee5
GM
8362 {
8363 Lisp_Object window;
8364 struct window *w;
8365
8366 window = FRAME_SELECTED_WINDOW (f);
8367 w = XWINDOW (window);
2311178e 8368
5f5c8ee5
GM
8369 /* If the user has switched buffers or windows, we need to
8370 recompute to reflect the new bindings. But we'll
8371 recompute when update_mode_lines is set too; that means
8372 that people can use force-mode-line-update to request
8373 that the menu bar be recomputed. The adverse effect on
8374 the rest of the redisplay algorithm is about the same as
8375 windows_or_buffers_changed anyway. */
8376 if (windows_or_buffers_changed
8377 || !NILP (w->update_mode_line)
84f2e615 8378 || update_mode_lines
5f5c8ee5
GM
8379 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8380 < BUF_MODIFF (XBUFFER (w->buffer)))
8381 != !NILP (w->last_had_star))
8382 || ((!NILP (Vtransient_mark_mode)
8383 && !NILP (XBUFFER (w->buffer)->mark_active))
8384 != !NILP (w->region_showing)))
8385 {
8386 struct buffer *prev = current_buffer;
331379bf 8387 int count = SPECPDL_INDEX ();
84f2e615
RS
8388 Lisp_Object old_tool_bar;
8389 struct gcpro gcpro1;
a2889657 8390
5f5c8ee5
GM
8391 /* Set current_buffer to the buffer of the selected
8392 window of the frame, so that we get the right local
8393 keymaps. */
8394 set_buffer_internal_1 (XBUFFER (w->buffer));
1f40cad2 8395
5f5c8ee5
GM
8396 /* Save match data, if we must. */
8397 if (save_match_data)
8398 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8399
8400 /* Make sure that we don't accidentally use bogus keymaps. */
8401 if (NILP (Voverriding_local_map_menu_flag))
8402 {
8403 specbind (Qoverriding_terminal_local_map, Qnil);
8404 specbind (Qoverriding_local_map, Qnil);
1f40cad2 8405 }
1f40cad2 8406
84f2e615
RS
8407 old_tool_bar = f->tool_bar_items;
8408 GCPRO1 (old_tool_bar);
8409
e037b9ec 8410 /* Build desired tool-bar items from keymaps. */
e3b2c21f 8411 BLOCK_INPUT;
7464726e
GM
8412 f->tool_bar_items
8413 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
e3b2c21f 8414 UNBLOCK_INPUT;
2311178e 8415
84f2e615
RS
8416 /* Redisplay the tool-bar if we changed it. */
8417 if (! NILP (Fequal (old_tool_bar, f->tool_bar_items)))
8418 w->update_mode_line = Qt;
be21b925 8419
1e802fb5 8420 UNGCPRO;
5f5c8ee5
GM
8421
8422 unbind_to (count, Qnil);
8423 set_buffer_internal_1 (prev);
81d478f3 8424 }
a2889657
JB
8425 }
8426}
8427
6c4429a5 8428
e037b9ec 8429/* Set F->desired_tool_bar_string to a Lisp string representing frame
7464726e 8430 F's desired tool-bar contents. F->tool_bar_items must have
5f5c8ee5
GM
8431 been set up previously by calling prepare_menu_bars. */
8432
a2889657 8433static void
e037b9ec 8434build_desired_tool_bar_string (f)
5f5c8ee5 8435 struct frame *f;
a2889657 8436{
a23887b9 8437 int i, size, size_needed;
5f5c8ee5
GM
8438 struct gcpro gcpro1, gcpro2, gcpro3;
8439 Lisp_Object image, plist, props;
a2889657 8440
5f5c8ee5
GM
8441 image = plist = props = Qnil;
8442 GCPRO3 (image, plist, props);
a2889657 8443
e037b9ec 8444 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
5f5c8ee5 8445 Otherwise, make a new string. */
2311178e 8446
5f5c8ee5 8447 /* The size of the string we might be able to reuse. */
e037b9ec 8448 size = (STRINGP (f->desired_tool_bar_string)
2051c264 8449 ? SCHARS (f->desired_tool_bar_string)
5f5c8ee5
GM
8450 : 0);
8451
b94c0d9c 8452 /* We need one space in the string for each image. */
a23887b9 8453 size_needed = f->n_tool_bar_items;
2311178e 8454
b94c0d9c 8455 /* Reuse f->desired_tool_bar_string, if possible. */
cb2ddc53 8456 if (size < size_needed || NILP (f->desired_tool_bar_string))
6fc556fd
KR
8457 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8458 make_number (' '));
5f5c8ee5
GM
8459 else
8460 {
8461 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8462 Fremove_text_properties (make_number (0), make_number (size),
e037b9ec 8463 props, f->desired_tool_bar_string);
5f5c8ee5 8464 }
a2889657 8465
5f5c8ee5 8466 /* Put a `display' property on the string for the images to display,
e037b9ec
GM
8467 put a `menu_item' property on tool-bar items with a value that
8468 is the index of the item in F's tool-bar item vector. */
a23887b9 8469 for (i = 0; i < f->n_tool_bar_items; ++i)
a2889657 8470 {
7464726e 8471#define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
5f5c8ee5 8472
e037b9ec
GM
8473 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8474 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
35a41507 8475 int hmargin, vmargin, relief, idx, end;
493fdc3c 8476 extern Lisp_Object QCrelief, QCmargin, QCconversion;
5f5c8ee5
GM
8477
8478 /* If image is a vector, choose the image according to the
8479 button state. */
e037b9ec 8480 image = PROP (TOOL_BAR_ITEM_IMAGES);
5f5c8ee5
GM
8481 if (VECTORP (image))
8482 {
5f5c8ee5
GM
8483 if (enabled_p)
8484 idx = (selected_p
e037b9ec
GM
8485 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8486 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
5f5c8ee5
GM
8487 else
8488 idx = (selected_p
e037b9ec
GM
8489 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8490 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
2311178e 8491
37e4e482
GM
8492 xassert (ASIZE (image) >= idx);
8493 image = AREF (image, idx);
5f5c8ee5 8494 }
37e4e482
GM
8495 else
8496 idx = -1;
5f5c8ee5
GM
8497
8498 /* Ignore invalid image specifications. */
8499 if (!valid_image_p (image))
8500 continue;
8501
e037b9ec 8502 /* Display the tool-bar button pressed, or depressed. */
5f5c8ee5
GM
8503 plist = Fcopy_sequence (XCDR (image));
8504
8505 /* Compute margin and relief to draw. */
9f864bf9 8506 relief = (tool_bar_button_relief >= 0
c3d76173
GM
8507 ? tool_bar_button_relief
8508 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
35a41507
GM
8509 hmargin = vmargin = relief;
8510
8511 if (INTEGERP (Vtool_bar_button_margin)
8512 && XINT (Vtool_bar_button_margin) > 0)
8513 {
8514 hmargin += XFASTINT (Vtool_bar_button_margin);
8515 vmargin += XFASTINT (Vtool_bar_button_margin);
8516 }
8517 else if (CONSP (Vtool_bar_button_margin))
8518 {
8519 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8520 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8521 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
2311178e 8522
35a41507
GM
8523 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8524 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8525 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8526 }
2311178e 8527
e037b9ec 8528 if (auto_raise_tool_bar_buttons_p)
5f5c8ee5
GM
8529 {
8530 /* Add a `:relief' property to the image spec if the item is
8531 selected. */
8532 if (selected_p)
8533 {
8534 plist = Fplist_put (plist, QCrelief, make_number (-relief));
35a41507
GM
8535 hmargin -= relief;
8536 vmargin -= relief;
5f5c8ee5
GM
8537 }
8538 }
8539 else
8540 {
8541 /* If image is selected, display it pressed, i.e. with a
8542 negative relief. If it's not selected, display it with a
8543 raised relief. */
8544 plist = Fplist_put (plist, QCrelief,
8545 (selected_p
8546 ? make_number (-relief)
8547 : make_number (relief)));
35a41507
GM
8548 hmargin -= relief;
8549 vmargin -= relief;
5f5c8ee5
GM
8550 }
8551
8552 /* Put a margin around the image. */
35a41507
GM
8553 if (hmargin || vmargin)
8554 {
8555 if (hmargin == vmargin)
8556 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8557 else
8558 plist = Fplist_put (plist, QCmargin,
8559 Fcons (make_number (hmargin),
8560 make_number (vmargin)));
8561 }
2311178e 8562
37e4e482
GM
8563 /* If button is not enabled, and we don't have special images
8564 for the disabled state, make the image appear disabled by
5f5c8ee5 8565 applying an appropriate algorithm to it. */
37e4e482 8566 if (!enabled_p && idx < 0)
ecd01a0e 8567 plist = Fplist_put (plist, QCconversion, Qdisabled);
2311178e 8568
5f5c8ee5
GM
8569 /* Put a `display' text property on the string for the image to
8570 display. Put a `menu-item' property on the string that gives
e037b9ec 8571 the start of this item's properties in the tool-bar items
5f5c8ee5
GM
8572 vector. */
8573 image = Fcons (Qimage, plist);
8574 props = list4 (Qdisplay, image,
a23887b9
GM
8575 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8576
8577 /* Let the last image hide all remaining spaces in the tool bar
8578 string. The string can be longer than needed when we reuse a
8579 previous string. */
8580 if (i + 1 == f->n_tool_bar_items)
2051c264 8581 end = SCHARS (f->desired_tool_bar_string);
a23887b9
GM
8582 else
8583 end = i + 1;
8584 Fadd_text_properties (make_number (i), make_number (end),
e037b9ec 8585 props, f->desired_tool_bar_string);
5f5c8ee5 8586#undef PROP
a2889657
JB
8587 }
8588
5f5c8ee5
GM
8589 UNGCPRO;
8590}
8591
8592
e037b9ec 8593/* Display one line of the tool-bar of frame IT->f. */
5f5c8ee5
GM
8594
8595static void
e037b9ec 8596display_tool_bar_line (it)
5f5c8ee5
GM
8597 struct it *it;
8598{
8599 struct glyph_row *row = it->glyph_row;
8600 int max_x = it->last_visible_x;
8601 struct glyph *last;
2311178e 8602
5f5c8ee5
GM
8603 prepare_desired_row (row);
8604 row->y = it->current_y;
90aa2856
GM
8605
8606 /* Note that this isn't made use of if the face hasn't a box,
8607 so there's no need to check the face here. */
8608 it->start_of_box_run_p = 1;
2311178e 8609
5f5c8ee5 8610 while (it->current_x < max_x)
a2889657 8611 {
5f5c8ee5 8612 int x_before, x, n_glyphs_before, i, nglyphs;
a2f016e3 8613
5f5c8ee5
GM
8614 /* Get the next display element. */
8615 if (!get_next_display_element (it))
8616 break;
73af359d 8617
5f5c8ee5
GM
8618 /* Produce glyphs. */
8619 x_before = it->current_x;
8620 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8621 PRODUCE_GLYPHS (it);
daa37602 8622
5f5c8ee5
GM
8623 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8624 i = 0;
8625 x = x_before;
8626 while (i < nglyphs)
8627 {
8628 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
2311178e 8629
5f5c8ee5
GM
8630 if (x + glyph->pixel_width > max_x)
8631 {
8632 /* Glyph doesn't fit on line. */
8633 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8634 it->current_x = x;
8635 goto out;
8636 }
daa37602 8637
5f5c8ee5
GM
8638 ++it->hpos;
8639 x += glyph->pixel_width;
8640 ++i;
8641 }
8642
8643 /* Stop at line ends. */
8644 if (ITERATOR_AT_END_OF_LINE_P (it))
8645 break;
8646
cafafe0b 8647 set_iterator_to_next (it, 1);
a2889657 8648 }
a2889657 8649
5f5c8ee5 8650 out:;
a2889657 8651
5f5c8ee5
GM
8652 row->displays_text_p = row->used[TEXT_AREA] != 0;
8653 extend_face_to_end_of_line (it);
8654 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8655 last->right_box_line_p = 1;
90aa2856
GM
8656 if (last == row->glyphs[TEXT_AREA])
8657 last->left_box_line_p = 1;
5f5c8ee5 8658 compute_line_metrics (it);
2311178e 8659
e037b9ec 8660 /* If line is empty, make it occupy the rest of the tool-bar. */
5f5c8ee5
GM
8661 if (!row->displays_text_p)
8662 {
312246d1
GM
8663 row->height = row->phys_height = it->last_visible_y - row->y;
8664 row->ascent = row->phys_ascent = 0;
5f5c8ee5 8665 }
2311178e 8666
5f5c8ee5
GM
8667 row->full_width_p = 1;
8668 row->continued_p = 0;
8669 row->truncated_on_left_p = 0;
8670 row->truncated_on_right_p = 0;
8671
8672 it->current_x = it->hpos = 0;
8673 it->current_y += row->height;
8674 ++it->vpos;
8675 ++it->glyph_row;
a2889657 8676}
96a410bc 8677
5f5c8ee5 8678
e037b9ec 8679/* Value is the number of screen lines needed to make all tool-bar
5f5c8ee5 8680 items of frame F visible. */
96a410bc 8681
d39b6696 8682static int
e037b9ec 8683tool_bar_lines_needed (f)
5f5c8ee5 8684 struct frame *f;
d39b6696 8685{
e037b9ec 8686 struct window *w = XWINDOW (f->tool_bar_window);
5f5c8ee5 8687 struct it it;
2311178e 8688
e037b9ec
GM
8689 /* Initialize an iterator for iteration over
8690 F->desired_tool_bar_string in the tool-bar window of frame F. */
8691 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
5f5c8ee5 8692 it.first_visible_x = 0;
da8b7f4f 8693 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
e037b9ec 8694 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
5f5c8ee5
GM
8695
8696 while (!ITERATOR_AT_END_P (&it))
8697 {
8698 it.glyph_row = w->desired_matrix->rows;
8699 clear_glyph_row (it.glyph_row);
e037b9ec 8700 display_tool_bar_line (&it);
5f5c8ee5
GM
8701 }
8702
da8b7f4f 8703 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
d39b6696 8704}
96a410bc 8705
5f5c8ee5 8706
57c28064
GM
8707DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8708 0, 1, 0,
7ee72033
MB
8709 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8710 (frame)
57c28064
GM
8711 Lisp_Object frame;
8712{
8713 struct frame *f;
8714 struct window *w;
8715 int nlines = 0;
8716
8717 if (NILP (frame))
8718 frame = selected_frame;
8719 else
b7826503 8720 CHECK_FRAME (frame);
57c28064 8721 f = XFRAME (frame);
2311178e 8722
57c28064
GM
8723 if (WINDOWP (f->tool_bar_window)
8724 || (w = XWINDOW (f->tool_bar_window),
da8b7f4f 8725 WINDOW_TOTAL_LINES (w) > 0))
57c28064
GM
8726 {
8727 update_tool_bar (f, 1);
8728 if (f->n_tool_bar_items)
8729 {
8730 build_desired_tool_bar_string (f);
8731 nlines = tool_bar_lines_needed (f);
8732 }
8733 }
8734
8735 return make_number (nlines);
8736}
8737
8738
e037b9ec 8739/* Display the tool-bar of frame F. Value is non-zero if tool-bar's
5f5c8ee5
GM
8740 height should be changed. */
8741
8742static int
e037b9ec 8743redisplay_tool_bar (f)
5f5c8ee5 8744 struct frame *f;
96a410bc 8745{
5f5c8ee5
GM
8746 struct window *w;
8747 struct it it;
8748 struct glyph_row *row;
8749 int change_height_p = 0;
177c0ea7 8750
488dd4c4 8751#ifdef USE_GTK
810f2256 8752 if (FRAME_EXTERNAL_TOOL_BAR (f))
488dd4c4
JD
8753 update_frame_tool_bar (f);
8754 return 0;
8755#endif
2311178e 8756
e037b9ec
GM
8757 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8758 do anything. This means you must start with tool-bar-lines
5f5c8ee5 8759 non-zero to get the auto-sizing effect. Or in other words, you
e037b9ec
GM
8760 can turn off tool-bars by specifying tool-bar-lines zero. */
8761 if (!WINDOWP (f->tool_bar_window)
8762 || (w = XWINDOW (f->tool_bar_window),
da8b7f4f 8763 WINDOW_TOTAL_LINES (w) == 0))
5f5c8ee5 8764 return 0;
96a410bc 8765
e037b9ec
GM
8766 /* Set up an iterator for the tool-bar window. */
8767 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
5f5c8ee5 8768 it.first_visible_x = 0;
da8b7f4f 8769 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
5f5c8ee5 8770 row = it.glyph_row;
3450d04c 8771
e037b9ec
GM
8772 /* Build a string that represents the contents of the tool-bar. */
8773 build_desired_tool_bar_string (f);
8774 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
3450d04c 8775
e037b9ec 8776 /* Display as many lines as needed to display all tool-bar items. */
5f5c8ee5 8777 while (it.current_y < it.last_visible_y)
e037b9ec 8778 display_tool_bar_line (&it);
3450d04c 8779
e037b9ec 8780 /* It doesn't make much sense to try scrolling in the tool-bar
5f5c8ee5
GM
8781 window, so don't do it. */
8782 w->desired_matrix->no_scrolling_p = 1;
8783 w->must_be_updated_p = 1;
3450d04c 8784
e037b9ec 8785 if (auto_resize_tool_bars_p)
5f5c8ee5
GM
8786 {
8787 int nlines;
6e33e6f0
GM
8788
8789 /* If we couldn't display everything, change the tool-bar's
8790 height. */
8791 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8792 change_height_p = 1;
2311178e 8793
5f5c8ee5
GM
8794 /* If there are blank lines at the end, except for a partially
8795 visible blank line at the end that is smaller than
da8b7f4f 8796 FRAME_LINE_HEIGHT, change the tool-bar's height. */
5f5c8ee5
GM
8797 row = it.glyph_row - 1;
8798 if (!row->displays_text_p
da8b7f4f 8799 && row->height >= FRAME_LINE_HEIGHT (f))
5f5c8ee5
GM
8800 change_height_p = 1;
8801
e037b9ec
GM
8802 /* If row displays tool-bar items, but is partially visible,
8803 change the tool-bar's height. */
5f5c8ee5
GM
8804 if (row->displays_text_p
8805 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8806 change_height_p = 1;
8807
e037b9ec 8808 /* Resize windows as needed by changing the `tool-bar-lines'
5f5c8ee5
GM
8809 frame parameter. */
8810 if (change_height_p
e037b9ec 8811 && (nlines = tool_bar_lines_needed (f),
da8b7f4f 8812 nlines != WINDOW_TOTAL_LINES (w)))
5f5c8ee5 8813 {
e037b9ec 8814 extern Lisp_Object Qtool_bar_lines;
5f5c8ee5 8815 Lisp_Object frame;
da8b7f4f 8816 int old_height = WINDOW_TOTAL_LINES (w);
2311178e 8817
5f5c8ee5
GM
8818 XSETFRAME (frame, f);
8819 clear_glyph_matrix (w->desired_matrix);
8820 Fmodify_frame_parameters (frame,
e037b9ec 8821 Fcons (Fcons (Qtool_bar_lines,
5f5c8ee5
GM
8822 make_number (nlines)),
8823 Qnil));
da8b7f4f 8824 if (WINDOW_TOTAL_LINES (w) != old_height)
e85ee976 8825 fonts_changed_p = 1;
5f5c8ee5
GM
8826 }
8827 }
3450d04c 8828
5f5c8ee5 8829 return change_height_p;
96a410bc 8830}
90adcf20 8831
5f5c8ee5 8832
e037b9ec
GM
8833/* Get information about the tool-bar item which is displayed in GLYPH
8834 on frame F. Return in *PROP_IDX the index where tool-bar item
7464726e 8835 properties start in F->tool_bar_items. Value is zero if
e037b9ec 8836 GLYPH doesn't display a tool-bar item. */
5f5c8ee5 8837
fa3c6b4d 8838static int
e037b9ec 8839tool_bar_item_info (f, glyph, prop_idx)
5f5c8ee5
GM
8840 struct frame *f;
8841 struct glyph *glyph;
8842 int *prop_idx;
90adcf20 8843{
5f5c8ee5
GM
8844 Lisp_Object prop;
8845 int success_p;
be676094
GM
8846 int charpos;
8847
8848 /* This function can be called asynchronously, which means we must
8849 exclude any possibility that Fget_text_property signals an
8850 error. */
2051c264 8851 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
be676094 8852 charpos = max (0, charpos);
2311178e 8853
5f5c8ee5
GM
8854 /* Get the text property `menu-item' at pos. The value of that
8855 property is the start index of this item's properties in
7464726e 8856 F->tool_bar_items. */
be676094 8857 prop = Fget_text_property (make_number (charpos),
e037b9ec 8858 Qmenu_item, f->current_tool_bar_string);
5f5c8ee5
GM
8859 if (INTEGERP (prop))
8860 {
8861 *prop_idx = XINT (prop);
8862 success_p = 1;
8863 }
8864 else
8865 success_p = 0;
90adcf20 8866
5f5c8ee5
GM
8867 return success_p;
8868}
2311178e 8869
fa3c6b4d
KS
8870\f
8871/* Get information about the tool-bar item at position X/Y on frame F.
8872 Return in *GLYPH a pointer to the glyph of the tool-bar item in
8873 the current matrix of the tool-bar window of F, or NULL if not
8874 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8875 item in F->tool_bar_items. Value is
8876
8877 -1 if X/Y is not on a tool-bar item
8878 0 if X/Y is on the same item that was highlighted before.
8879 1 otherwise. */
8880
8881static int
8882get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
8883 struct frame *f;
8884 int x, y;
8885 struct glyph **glyph;
8886 int *hpos, *vpos, *prop_idx;
8887{
8888 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8889 struct window *w = XWINDOW (f->tool_bar_window);
8890 int area;
8891
8892 /* Find the glyph under X/Y. */
493fdc3c 8893 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
fa3c6b4d
KS
8894 if (*glyph == NULL)
8895 return -1;
8896
8897 /* Get the start of this tool-bar item's properties in
8898 f->tool_bar_items. */
8899 if (!tool_bar_item_info (f, *glyph, prop_idx))
8900 return -1;
8901
8902 /* Is mouse on the highlighted item? */
8903 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
8904 && *vpos >= dpyinfo->mouse_face_beg_row
8905 && *vpos <= dpyinfo->mouse_face_end_row
8906 && (*vpos > dpyinfo->mouse_face_beg_row
8907 || *hpos >= dpyinfo->mouse_face_beg_col)
8908 && (*vpos < dpyinfo->mouse_face_end_row
8909 || *hpos < dpyinfo->mouse_face_end_col
8910 || dpyinfo->mouse_face_past_end))
8911 return 0;
8912
8913 return 1;
8914}
8915
8916
8917/* EXPORT:
8918 Handle mouse button event on the tool-bar of frame F, at
8919 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
8920 0 for button release. MODIFIERS is event modifiers for button
8921 release. */
8922
8923void
8924handle_tool_bar_click (f, x, y, down_p, modifiers)
8925 struct frame *f;
8926 int x, y, down_p;
8927 unsigned int modifiers;
8928{
8929 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8930 struct window *w = XWINDOW (f->tool_bar_window);
8931 int hpos, vpos, prop_idx;
8932 struct glyph *glyph;
8933 Lisp_Object enabled_p;
8934
8935 /* If not on the highlighted tool-bar item, return. */
8936 frame_to_window_pixel_xy (w, &x, &y);
8937 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
8938 return;
8939
8940 /* If item is disabled, do nothing. */
8941 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8942 if (NILP (enabled_p))
8943 return;
8944
8945 if (down_p)
8946 {
8947 /* Show item in pressed state. */
8948 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
8949 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
8950 last_tool_bar_item = prop_idx;
8951 }
8952 else
8953 {
8954 Lisp_Object key, frame;
8955 struct input_event event;
d86705ec 8956 EVENT_INIT (event);
fa3c6b4d
KS
8957
8958 /* Show item in released state. */
8959 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
8960 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
8961
8962 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
8963
8964 XSETFRAME (frame, f);
8965 event.kind = TOOL_BAR_EVENT;
8966 event.frame_or_window = frame;
8967 event.arg = frame;
8968 kbd_buffer_store_event (&event);
8969
8970 event.kind = TOOL_BAR_EVENT;
8971 event.frame_or_window = frame;
8972 event.arg = key;
8973 event.modifiers = modifiers;
8974 kbd_buffer_store_event (&event);
8975 last_tool_bar_item = -1;
8976 }
8977}
8978
8979
8980/* Possibly highlight a tool-bar item on frame F when mouse moves to
8981 tool-bar window-relative coordinates X/Y. Called from
8982 note_mouse_highlight. */
8983
8984static void
8985note_tool_bar_highlight (f, x, y)
8986 struct frame *f;
8987 int x, y;
8988{
8989 Lisp_Object window = f->tool_bar_window;
8990 struct window *w = XWINDOW (window);
8991 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8992 int hpos, vpos;
8993 struct glyph *glyph;
8994 struct glyph_row *row;
8995 int i;
8996 Lisp_Object enabled_p;
8997 int prop_idx;
8998 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
8999 int mouse_down_p, rc;
9000
9001 /* Function note_mouse_highlight is called with negative x(y
9002 values when mouse moves outside of the frame. */
9003 if (x <= 0 || y <= 0)
9004 {
9005 clear_mouse_face (dpyinfo);
9006 return;
9007 }
9008
9009 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9010 if (rc < 0)
9011 {
9012 /* Not on tool-bar item. */
9013 clear_mouse_face (dpyinfo);
9014 return;
9015 }
9016 else if (rc == 0)
9017 /* On same tool-bar item as before. */
9018 goto set_help_echo;
9019
9020 clear_mouse_face (dpyinfo);
9021
9022 /* Mouse is down, but on different tool-bar item? */
9023 mouse_down_p = (dpyinfo->grabbed
9024 && f == last_mouse_frame
9025 && FRAME_LIVE_P (f));
9026 if (mouse_down_p
9027 && last_tool_bar_item != prop_idx)
9028 return;
9029
9030 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9031 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9032
9033 /* If tool-bar item is not enabled, don't highlight it. */
9034 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9035 if (!NILP (enabled_p))
9036 {
9037 /* Compute the x-position of the glyph. In front and past the
da8b7f4f 9038 image is a space. We include this in the highlighted area. */
fa3c6b4d
KS
9039 row = MATRIX_ROW (w->current_matrix, vpos);
9040 for (i = x = 0; i < hpos; ++i)
9041 x += row->glyphs[TEXT_AREA][i].pixel_width;
9042
9043 /* Record this as the current active region. */
9044 dpyinfo->mouse_face_beg_col = hpos;
9045 dpyinfo->mouse_face_beg_row = vpos;
9046 dpyinfo->mouse_face_beg_x = x;
9047 dpyinfo->mouse_face_beg_y = row->y;
9048 dpyinfo->mouse_face_past_end = 0;
9049
9050 dpyinfo->mouse_face_end_col = hpos + 1;
9051 dpyinfo->mouse_face_end_row = vpos;
9052 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9053 dpyinfo->mouse_face_end_y = row->y;
9054 dpyinfo->mouse_face_window = window;
9055 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9056
9057 /* Display it as active. */
9058 show_mouse_face (dpyinfo, draw);
9059 dpyinfo->mouse_face_image_state = draw;
9060 }
9061
9062 set_help_echo:
9063
9064 /* Set help_echo_string to a help string to display for this tool-bar item.
9065 XTread_socket does the rest. */
9066 help_echo_object = help_echo_window = Qnil;
9067 help_echo_pos = -1;
9068 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9069 if (NILP (help_echo_string))
9070 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9071}
9072
5f5c8ee5 9073#endif /* HAVE_WINDOW_SYSTEM */
ffbbc941
KS
9074
9075
9076\f
5f5c8ee5
GM
9077/************************************************************************
9078 Horizontal scrolling
9079 ************************************************************************/
feb0c42f 9080
5f5c8ee5
GM
9081static int hscroll_window_tree P_ ((Lisp_Object));
9082static int hscroll_windows P_ ((Lisp_Object));
feb0c42f 9083
5f5c8ee5
GM
9084/* For all leaf windows in the window tree rooted at WINDOW, set their
9085 hscroll value so that PT is (i) visible in the window, and (ii) so
9086 that it is not within a certain margin at the window's left and
9087 right border. Value is non-zero if any window's hscroll has been
9088 changed. */
9089
9090static int
9091hscroll_window_tree (window)
9092 Lisp_Object window;
9093{
9094 int hscrolled_p = 0;
e76d28d5 9095 int hscroll_relative_p = FLOATP (Vhscroll_step);
1df7e8f0
EZ
9096 int hscroll_step_abs = 0;
9097 double hscroll_step_rel = 0;
9098
9099 if (hscroll_relative_p)
9100 {
e76d28d5 9101 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
1df7e8f0
EZ
9102 if (hscroll_step_rel < 0)
9103 {
9104 hscroll_relative_p = 0;
9105 hscroll_step_abs = 0;
9106 }
9107 }
e76d28d5 9108 else if (INTEGERP (Vhscroll_step))
1df7e8f0 9109 {
e76d28d5 9110 hscroll_step_abs = XINT (Vhscroll_step);
1df7e8f0
EZ
9111 if (hscroll_step_abs < 0)
9112 hscroll_step_abs = 0;
9113 }
9114 else
9115 hscroll_step_abs = 0;
9116
5f5c8ee5 9117 while (WINDOWP (window))
90adcf20 9118 {
5f5c8ee5 9119 struct window *w = XWINDOW (window);
1df7e8f0 9120
5f5c8ee5
GM
9121 if (WINDOWP (w->hchild))
9122 hscrolled_p |= hscroll_window_tree (w->hchild);
9123 else if (WINDOWP (w->vchild))
9124 hscrolled_p |= hscroll_window_tree (w->vchild);
9125 else if (w->cursor.vpos >= 0)
9126 {
da8b7f4f
KS
9127 int h_margin;
9128 int text_area_width;
92a90e89
GM
9129 struct glyph_row *current_cursor_row
9130 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9131 struct glyph_row *desired_cursor_row
9132 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9133 struct glyph_row *cursor_row
9134 = (desired_cursor_row->enabled_p
9135 ? desired_cursor_row
9136 : current_cursor_row);
a2725ab2 9137
da8b7f4f 9138 text_area_width = window_box_width (w, TEXT_AREA);
90adcf20 9139
5f5c8ee5 9140 /* Scroll when cursor is inside this scroll margin. */
da8b7f4f 9141 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
1df7e8f0 9142
5f5c8ee5 9143 if ((XFASTINT (w->hscroll)
e76d28d5 9144 && w->cursor.x <= h_margin)
92a90e89
GM
9145 || (cursor_row->enabled_p
9146 && cursor_row->truncated_on_right_p
e76d28d5 9147 && (w->cursor.x >= text_area_width - h_margin)))
08b610e4 9148 {
5f5c8ee5
GM
9149 struct it it;
9150 int hscroll;
9151 struct buffer *saved_current_buffer;
9152 int pt;
1df7e8f0 9153 int wanted_x;
5f5c8ee5
GM
9154
9155 /* Find point in a display of infinite width. */
9156 saved_current_buffer = current_buffer;
9157 current_buffer = XBUFFER (w->buffer);
1df7e8f0 9158
5f5c8ee5
GM
9159 if (w == XWINDOW (selected_window))
9160 pt = BUF_PT (current_buffer);
9161 else
08b610e4 9162 {
5f5c8ee5
GM
9163 pt = marker_position (w->pointm);
9164 pt = max (BEGV, pt);
9165 pt = min (ZV, pt);
9166 }
9167
9168 /* Move iterator to pt starting at cursor_row->start in
9169 a line with infinite width. */
9170 init_to_row_start (&it, w, cursor_row);
9171 it.last_visible_x = INFINITY;
9172 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9173 current_buffer = saved_current_buffer;
9174
1df7e8f0
EZ
9175 /* Position cursor in window. */
9176 if (!hscroll_relative_p && hscroll_step_abs == 0)
ec110e9e
KS
9177 hscroll = max (0, (it.current_x
9178 - (ITERATOR_AT_END_OF_LINE_P (&it)
9179 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9180 : (text_area_width / 2))))
da8b7f4f 9181 / FRAME_COLUMN_WIDTH (it.f);
e76d28d5 9182 else if (w->cursor.x >= text_area_width - h_margin)
1df7e8f0
EZ
9183 {
9184 if (hscroll_relative_p)
9185 wanted_x = text_area_width * (1 - hscroll_step_rel)
e76d28d5 9186 - h_margin;
1df7e8f0
EZ
9187 else
9188 wanted_x = text_area_width
da8b7f4f 9189 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
e76d28d5 9190 - h_margin;
1df7e8f0 9191 hscroll
da8b7f4f 9192 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
1df7e8f0
EZ
9193 }
9194 else
9195 {
9196 if (hscroll_relative_p)
9197 wanted_x = text_area_width * hscroll_step_rel
e76d28d5 9198 + h_margin;
1df7e8f0 9199 else
da8b7f4f 9200 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
e76d28d5 9201 + h_margin;
1df7e8f0 9202 hscroll
da8b7f4f 9203 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
1df7e8f0 9204 }
3172f70b 9205 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
5f5c8ee5
GM
9206
9207 /* Don't call Fset_window_hscroll if value hasn't
9208 changed because it will prevent redisplay
9209 optimizations. */
9210 if (XFASTINT (w->hscroll) != hscroll)
9211 {
3172f70b
GM
9212 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9213 w->hscroll = make_number (hscroll);
5f5c8ee5 9214 hscrolled_p = 1;
08b610e4 9215 }
08b610e4 9216 }
08b610e4 9217 }
a2725ab2 9218
5f5c8ee5 9219 window = w->next;
90adcf20 9220 }
cd6dfed6 9221
5f5c8ee5
GM
9222 /* Value is non-zero if hscroll of any leaf window has been changed. */
9223 return hscrolled_p;
9224}
9225
9226
9227/* Set hscroll so that cursor is visible and not inside horizontal
9228 scroll margins for all windows in the tree rooted at WINDOW. See
9229 also hscroll_window_tree above. Value is non-zero if any window's
9230 hscroll has been changed. If it has, desired matrices on the frame
9231 of WINDOW are cleared. */
9232
9233static int
9234hscroll_windows (window)
9235 Lisp_Object window;
9236{
d475bcb8 9237 int hscrolled_p;
2311178e 9238
d475bcb8
GM
9239 if (automatic_hscrolling_p)
9240 {
9241 hscrolled_p = hscroll_window_tree (window);
9242 if (hscrolled_p)
9243 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9244 }
9245 else
9246 hscrolled_p = 0;
5f5c8ee5 9247 return hscrolled_p;
90adcf20 9248}
5f5c8ee5
GM
9249
9250
90adcf20 9251\f
5f5c8ee5
GM
9252/************************************************************************
9253 Redisplay
9254 ************************************************************************/
9255
9256/* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9257 to a non-zero value. This is sometimes handy to have in a debugger
9258 session. */
9259
9260#if GLYPH_DEBUG
a2889657 9261
5f5c8ee5
GM
9262/* First and last unchanged row for try_window_id. */
9263
9264int debug_first_unchanged_at_end_vpos;
9265int debug_last_unchanged_at_beg_vpos;
9266
9267/* Delta vpos and y. */
9268
9269int debug_dvpos, debug_dy;
9270
9271/* Delta in characters and bytes for try_window_id. */
9272
9273int debug_delta, debug_delta_bytes;
9274
9275/* Values of window_end_pos and window_end_vpos at the end of
9276 try_window_id. */
9277
31ade731 9278EMACS_INT debug_end_pos, debug_end_vpos;
5f5c8ee5
GM
9279
9280/* Append a string to W->desired_matrix->method. FMT is a printf
9281 format string. A1...A9 are a supplement for a variable-length
9282 argument list. If trace_redisplay_p is non-zero also printf the
9283 resulting string to stderr. */
9284
9285static void
9286debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9287 struct window *w;
9288 char *fmt;
9289 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9290{
9291 char buffer[512];
9292 char *method = w->desired_matrix->method;
9293 int len = strlen (method);
9294 int size = sizeof w->desired_matrix->method;
9295 int remaining = size - len - 1;
9296
9297 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9298 if (len && remaining)
9299 {
9300 method[len] = '|';
9301 --remaining, ++len;
9302 }
2311178e 9303
5f5c8ee5
GM
9304 strncpy (method + len, buffer, remaining);
9305
9306 if (trace_redisplay_p)
9307 fprintf (stderr, "%p (%s): %s\n",
9308 w,
9309 ((BUFFERP (w->buffer)
9310 && STRINGP (XBUFFER (w->buffer)->name))
2051c264 9311 ? (char *) SDATA (XBUFFER (w->buffer)->name)
5f5c8ee5
GM
9312 : "no buffer"),
9313 buffer);
9314}
a2889657 9315
5f5c8ee5 9316#endif /* GLYPH_DEBUG */
90adcf20 9317
a2889657 9318
5f5c8ee5
GM
9319/* Value is non-zero if all changes in window W, which displays
9320 current_buffer, are in the text between START and END. START is a
9321 buffer position, END is given as a distance from Z. Used in
9322 redisplay_internal for display optimization. */
9323
9324static INLINE int
9325text_outside_line_unchanged_p (w, start, end)
9326 struct window *w;
9327 int start, end;
9328{
9329 int unchanged_p = 1;
2311178e 9330
5f5c8ee5
GM
9331 /* If text or overlays have changed, see where. */
9332 if (XFASTINT (w->last_modified) < MODIFF
9333 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9334 {
9335 /* Gap in the line? */
9336 if (GPT < start || Z - GPT < end)
9337 unchanged_p = 0;
9338
9339 /* Changes start in front of the line, or end after it? */
9340 if (unchanged_p
9142dd5b
GM
9341 && (BEG_UNCHANGED < start - 1
9342 || END_UNCHANGED < end))
5f5c8ee5 9343 unchanged_p = 0;
2311178e 9344
5f5c8ee5
GM
9345 /* If selective display, can't optimize if changes start at the
9346 beginning of the line. */
9347 if (unchanged_p
9348 && INTEGERP (current_buffer->selective_display)
9349 && XINT (current_buffer->selective_display) > 0
9142dd5b 9350 && (BEG_UNCHANGED < start || GPT <= start))
5f5c8ee5 9351 unchanged_p = 0;
47d57b22
GM
9352
9353 /* If there are overlays at the start or end of the line, these
9354 may have overlay strings with newlines in them. A change at
9355 START, for instance, may actually concern the display of such
9356 overlay strings as well, and they are displayed on different
9357 lines. So, quickly rule out this case. (For the future, it
9358 might be desirable to implement something more telling than
9359 just BEG/END_UNCHANGED.) */
9360 if (unchanged_p)
9361 {
9362 if (BEG + BEG_UNCHANGED == start
9363 && overlay_touches_p (start))
9364 unchanged_p = 0;
9365 if (END_UNCHANGED == end
9366 && overlay_touches_p (Z - end))
9367 unchanged_p = 0;
9368 }
5f5c8ee5
GM
9369 }
9370
9371 return unchanged_p;
9372}
9373
9374
9375/* Do a frame update, taking possible shortcuts into account. This is
9376 the main external entry point for redisplay.
9377
9378 If the last redisplay displayed an echo area message and that message
9379 is no longer requested, we clear the echo area or bring back the
9380 mini-buffer if that is in use. */
20de20dc 9381
a2889657
JB
9382void
9383redisplay ()
e9874cee
RS
9384{
9385 redisplay_internal (0);
9386}
9387
47d57b22 9388
351b5434
KS
9389static Lisp_Object
9390overlay_arrow_string_or_property (var, pbitmap)
9391 Lisp_Object var;
9392 int *pbitmap;
9393{
9394 Lisp_Object pstr = Fget (var, Qoverlay_arrow_string);
9395 Lisp_Object bitmap;
9396
9397 if (pbitmap)
9398 {
9399 *pbitmap = 0;
9400 if (bitmap = Fget (var, Qoverlay_arrow_bitmap), INTEGERP (bitmap))
9401 *pbitmap = XINT (bitmap);
9402 }
9403
9404 if (!NILP (pstr))
9405 return pstr;
9406 return Voverlay_arrow_string;
9407}
9408
9409/* Return 1 if there are any overlay-arrows in current_buffer. */
9410static int
9411overlay_arrow_in_current_buffer_p ()
9412{
9413 Lisp_Object vlist;
9414
9415 for (vlist = Voverlay_arrow_variable_list;
9416 CONSP (vlist);
9417 vlist = XCDR (vlist))
9418 {
9419 Lisp_Object var = XCAR (vlist);
9420 Lisp_Object val;
9421
9422 if (!SYMBOLP (var))
9423 continue;
9424 val = find_symbol_value (var);
9425 if (MARKERP (val)
9426 && current_buffer == XMARKER (val)->buffer)
9427 return 1;
9428 }
9429 return 0;
9430}
9431
9432
9433/* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9434 has changed. */
9435
9436static int
9437overlay_arrows_changed_p ()
9438{
9439 Lisp_Object vlist;
9440
9441 for (vlist = Voverlay_arrow_variable_list;
9442 CONSP (vlist);
9443 vlist = XCDR (vlist))
9444 {
9445 Lisp_Object var = XCAR (vlist);
9446 Lisp_Object val, pstr;
9447
9448 if (!SYMBOLP (var))
9449 continue;
9450 val = find_symbol_value (var);
9451 if (!MARKERP (val))
9452 continue;
9453 if (! EQ (COERCE_MARKER (val),
9454 Fget (var, Qlast_arrow_position))
9455 || ! (pstr = overlay_arrow_string_or_property (var, 0),
9456 EQ (pstr, Fget (var, Qlast_arrow_string))))
9457 return 1;
9458 }
9459 return 0;
9460}
9461
9462/* Mark overlay arrows to be updated on next redisplay. */
9463
9464static void
9465update_overlay_arrows (up_to_date)
9466 int up_to_date;
9467{
9468 Lisp_Object vlist;
9469
9470 for (vlist = Voverlay_arrow_variable_list;
9471 CONSP (vlist);
9472 vlist = XCDR (vlist))
9473 {
9474 Lisp_Object var = XCAR (vlist);
351b5434
KS
9475
9476 if (!SYMBOLP (var))
9477 continue;
9478
409368b9 9479 if (up_to_date > 0)
351b5434 9480 {
7d90b587 9481 Lisp_Object val = find_symbol_value (var);
351b5434 9482 Fput (var, Qlast_arrow_position,
7d90b587 9483 COERCE_MARKER (val));
351b5434
KS
9484 Fput (var, Qlast_arrow_string,
9485 overlay_arrow_string_or_property (var, 0));
9486 }
9487 else if (up_to_date < 0
9488 || !NILP (Fget (var, Qlast_arrow_position)))
9489 {
9490 Fput (var, Qlast_arrow_position, Qt);
9491 Fput (var, Qlast_arrow_string, Qt);
9492 }
9493 }
9494}
9495
9496
9497/* Return overlay arrow string at row, or nil. */
9498
9499static Lisp_Object
9500overlay_arrow_at_row (f, row, pbitmap)
9501 struct frame *f;
9502 struct glyph_row *row;
9503 int *pbitmap;
9504{
9505 Lisp_Object vlist;
9506
9507 for (vlist = Voverlay_arrow_variable_list;
9508 CONSP (vlist);
9509 vlist = XCDR (vlist))
9510 {
9511 Lisp_Object var = XCAR (vlist);
9512 Lisp_Object val;
9513
9514 if (!SYMBOLP (var))
9515 continue;
9516
9517 val = find_symbol_value (var);
be21b925 9518
351b5434
KS
9519 if (MARKERP (val)
9520 && current_buffer == XMARKER (val)->buffer
9521 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
9522 {
9523 val = overlay_arrow_string_or_property (var, pbitmap);
9524 if (FRAME_WINDOW_P (f))
9525 return Qt;
9526 else if (STRINGP (val))
9527 return val;
9528 break;
9529 }
9530 }
9531
9532 *pbitmap = 0;
9533 return Qnil;
9534}
9535
260a86a0
KH
9536/* Return 1 if point moved out of or into a composition. Otherwise
9537 return 0. PREV_BUF and PREV_PT are the last point buffer and
9538 position. BUF and PT are the current point buffer and position. */
9539
9540int
9541check_point_in_composition (prev_buf, prev_pt, buf, pt)
9542 struct buffer *prev_buf, *buf;
9543 int prev_pt, pt;
9544{
9545 int start, end;
9546 Lisp_Object prop;
9547 Lisp_Object buffer;
9548
9549 XSETBUFFER (buffer, buf);
9550 /* Check a composition at the last point if point moved within the
9551 same buffer. */
9552 if (prev_buf == buf)
9553 {
9554 if (prev_pt == pt)
9555 /* Point didn't move. */
9556 return 0;
2311178e 9557
260a86a0
KH
9558 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9559 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9560 && COMPOSITION_VALID_P (start, end, prop)
9561 && start < prev_pt && end > prev_pt)
9562 /* The last point was within the composition. Return 1 iff
9563 point moved out of the composition. */
9564 return (pt <= start || pt >= end);
9565 }
9566
9567 /* Check a composition at the current point. */
9568 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9569 && find_composition (pt, -1, &start, &end, &prop, buffer)
9570 && COMPOSITION_VALID_P (start, end, prop)
9571 && start < pt && end > pt);
9572}
5f5c8ee5 9573
47d57b22 9574
9142dd5b
GM
9575/* Reconsider the setting of B->clip_changed which is displayed
9576 in window W. */
9577
9578static INLINE void
9579reconsider_clip_changes (w, b)
9580 struct window *w;
9581 struct buffer *b;
9582{
c62c1bb5 9583 if (b->clip_changed
9142dd5b
GM
9584 && !NILP (w->window_end_valid)
9585 && w->current_matrix->buffer == b
9586 && w->current_matrix->zv == BUF_ZV (b)
9587 && w->current_matrix->begv == BUF_BEGV (b))
9588 b->clip_changed = 0;
260a86a0
KH
9589
9590 /* If display wasn't paused, and W is not a tool bar window, see if
9591 point has been moved into or out of a composition. In that case,
9592 we set b->clip_changed to 1 to force updating the screen. If
9593 b->clip_changed has already been set to 1, we can skip this
9594 check. */
9595 if (!b->clip_changed
9596 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9597 {
9598 int pt;
9599
9600 if (w == XWINDOW (selected_window))
9601 pt = BUF_PT (current_buffer);
9602 else
9603 pt = marker_position (w->pointm);
9604
9605 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
6fc556fd 9606 || pt != XINT (w->last_point))
260a86a0 9607 && check_point_in_composition (w->current_matrix->buffer,
6fc556fd 9608 XINT (w->last_point),
260a86a0
KH
9609 XBUFFER (w->buffer), pt))
9610 b->clip_changed = 1;
9611 }
9142dd5b 9612}
aac2d8b2 9613\f
dd429b03
KH
9614
9615/* Select FRAME to forward the values of frame-local variables into C
9616 variables so that the redisplay routines can access those values
9617 directly. */
9618
9619static void
9620select_frame_for_redisplay (frame)
9621 Lisp_Object frame;
9622{
9623 Lisp_Object tail, sym, val;
9624 Lisp_Object old = selected_frame;
be21b925 9625
dd429b03
KH
9626 selected_frame = frame;
9627
9628 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
9629 if (CONSP (XCAR (tail))
9630 && (sym = XCAR (XCAR (tail)),
9631 SYMBOLP (sym))
9632 && (sym = indirect_variable (sym),
9633 val = SYMBOL_VALUE (sym),
9634 (BUFFER_LOCAL_VALUEP (val)
9635 || SOME_BUFFER_LOCAL_VALUEP (val)))
9636 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9637 Fsymbol_value (sym);
9638
9639 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
9640 if (CONSP (XCAR (tail))
9641 && (sym = XCAR (XCAR (tail)),
9642 SYMBOLP (sym))
9643 && (sym = indirect_variable (sym),
9644 val = SYMBOL_VALUE (sym),
9645 (BUFFER_LOCAL_VALUEP (val)
9646 || SOME_BUFFER_LOCAL_VALUEP (val)))
9647 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9648 Fsymbol_value (sym);
9649}
9650
9651
aac2d8b2
RS
9652#define STOP_POLLING \
9653do { if (! polling_stopped_here) stop_polling (); \
9654 polling_stopped_here = 1; } while (0)
9655
9656#define RESUME_POLLING \
9657do { if (polling_stopped_here) start_polling (); \
9658 polling_stopped_here = 0; } while (0)
9142dd5b
GM
9659
9660
5f5c8ee5
GM
9661/* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9662 response to any user action; therefore, we should preserve the echo
9663 area. (Actually, our caller does that job.) Perhaps in the future
9664 avoid recentering windows if it is not necessary; currently that
9665 causes some problems. */
e9874cee
RS
9666
9667static void
9668redisplay_internal (preserve_echo_area)
9669 int preserve_echo_area;
a2889657 9670{
5f5c8ee5
GM
9671 struct window *w = XWINDOW (selected_window);
9672 struct frame *f = XFRAME (w->frame);
9673 int pause;
a2889657 9674 int must_finish = 0;
5f5c8ee5 9675 struct text_pos tlbufpos, tlendpos;
89819bdd 9676 int number_of_visible_frames;
28514cd9 9677 int count;
886bd6f2 9678 struct frame *sf = SELECTED_FRAME ();
aac2d8b2 9679 int polling_stopped_here = 0;
a2889657 9680
5f5c8ee5
GM
9681 /* Non-zero means redisplay has to consider all windows on all
9682 frames. Zero means, only selected_window is considered. */
9683 int consider_all_windows_p;
2311178e 9684
5f5c8ee5
GM
9685 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9686
9687 /* No redisplay if running in batch mode or frame is not yet fully
9688 initialized, or redisplay is explicitly turned off by setting
9689 Vinhibit_redisplay. */
9690 if (noninteractive
9691 || !NILP (Vinhibit_redisplay)
9692 || !f->glyphs_initialized_p)
a2889657
JB
9693 return;
9694
5f5c8ee5
GM
9695 /* The flag redisplay_performed_directly_p is set by
9696 direct_output_for_insert when it already did the whole screen
9697 update necessary. */
9698 if (redisplay_performed_directly_p)
9699 {
9700 redisplay_performed_directly_p = 0;
9701 if (!hscroll_windows (selected_window))
9702 return;
9703 }
9704
488dd4c4 9705#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15f0cf78
RS
9706 if (popup_activated ())
9707 return;
9708#endif
9709
28514cd9 9710 /* I don't think this happens but let's be paranoid. */
5f5c8ee5 9711 if (redisplaying_p)
735c094c
KH
9712 return;
9713
28514cd9
GM
9714 /* Record a function that resets redisplaying_p to its old value
9715 when we leave this function. */
331379bf 9716 count = SPECPDL_INDEX ();
dd429b03
KH
9717 record_unwind_protect (unwind_redisplay,
9718 Fcons (make_number (redisplaying_p), selected_frame));
28514cd9 9719 ++redisplaying_p;
26683087 9720 specbind (Qinhibit_free_realized_faces, Qnil);
2311178e 9721
8b32d885 9722 retry:
bd9d0f3f 9723 pause = 0;
9142dd5b
GM
9724 reconsider_clip_changes (w, current_buffer);
9725
5f5c8ee5
GM
9726 /* If new fonts have been loaded that make a glyph matrix adjustment
9727 necessary, do it. */
9728 if (fonts_changed_p)
9729 {
9730 adjust_glyphs (NULL);
9731 ++windows_or_buffers_changed;
9732 fonts_changed_p = 0;
9733 }
9734
6961e0c1
GM
9735 /* If face_change_count is non-zero, init_iterator will free all
9736 realized faces, which includes the faces referenced from current
9737 matrices. So, we can't reuse current matrices in this case. */
9738 if (face_change_count)
9739 ++windows_or_buffers_changed;
9740
886bd6f2
GM
9741 if (! FRAME_WINDOW_P (sf)
9742 && previous_terminal_frame != sf)
20de20dc 9743 {
5f5c8ee5
GM
9744 /* Since frames on an ASCII terminal share the same display
9745 area, displaying a different frame means redisplay the whole
9746 thing. */
20de20dc 9747 windows_or_buffers_changed++;
886bd6f2
GM
9748 SET_FRAME_GARBAGED (sf);
9749 XSETFRAME (Vterminal_frame, sf);
20de20dc 9750 }
886bd6f2 9751 previous_terminal_frame = sf;
20de20dc 9752
5f5c8ee5
GM
9753 /* Set the visible flags for all frames. Do this before checking
9754 for resized or garbaged frames; they want to know if their frames
9755 are visible. See the comment in frame.h for
9756 FRAME_SAMPLE_VISIBILITY. */
d724d989 9757 {
35f56f96 9758 Lisp_Object tail, frame;
d724d989 9759
89819bdd
RS
9760 number_of_visible_frames = 0;
9761
35f56f96 9762 FOR_EACH_FRAME (tail, frame)
f82aff7c 9763 {
5f5c8ee5 9764 struct frame *f = XFRAME (frame);
2311178e 9765
5f5c8ee5
GM
9766 FRAME_SAMPLE_VISIBILITY (f);
9767 if (FRAME_VISIBLE_P (f))
9768 ++number_of_visible_frames;
9769 clear_desired_matrices (f);
f82aff7c 9770 }
d724d989
JB
9771 }
9772
44fa5b1e 9773 /* Notice any pending interrupt request to change frame size. */
c6e89d6c 9774 do_pending_window_change (1);
a2889657 9775
5f5c8ee5 9776 /* Clear frames marked as garbaged. */
44fa5b1e 9777 if (frame_garbaged)
c6e89d6c 9778 clear_garbaged_frames ();
a2889657 9779
e037b9ec 9780 /* Build menubar and tool-bar items. */
f82aff7c
RS
9781 prepare_menu_bars ();
9782
28995e67 9783 if (windows_or_buffers_changed)
a2889657
JB
9784 update_mode_lines++;
9785
538f13d4
RS
9786 /* Detect case that we need to write or remove a star in the mode line. */
9787 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
a2889657
JB
9788 {
9789 w->update_mode_line = Qt;
9790 if (buffer_shared > 1)
9791 update_mode_lines++;
9792 }
9793
5f5c8ee5 9794 /* If %c is in the mode line, update it if needed. */
28995e67
RS
9795 if (!NILP (w->column_number_displayed)
9796 /* This alternative quickly identifies a common case
9797 where no change is needed. */
9798 && !(PT == XFASTINT (w->last_point)
8850a573
RS
9799 && XFASTINT (w->last_modified) >= MODIFF
9800 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
2311178e
TTN
9801 && (XFASTINT (w->column_number_displayed)
9802 != (int) current_column ())) /* iftc */
9803 w->update_mode_line = Qt;
28995e67 9804
44fa5b1e 9805 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
a2889657 9806
5f5c8ee5
GM
9807 /* The variable buffer_shared is set in redisplay_window and
9808 indicates that we redisplay a buffer in different windows. See
9809 there. */
5fb96e96
RS
9810 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
9811 || cursor_type_changed);
a2889657
JB
9812
9813 /* If specs for an arrow have changed, do thorough redisplay
9814 to ensure we remove any arrow that should no longer exist. */
351b5434 9815 if (overlay_arrows_changed_p ())
5f5c8ee5 9816 consider_all_windows_p = windows_or_buffers_changed = 1;
a2889657 9817
90adcf20
RS
9818 /* Normally the message* functions will have already displayed and
9819 updated the echo area, but the frame may have been trashed, or
9820 the update may have been preempted, so display the echo area
6e019995 9821 again here. Checking message_cleared_p captures the case that
c6e89d6c 9822 the echo area should be cleared. */
96d5e9a0
GM
9823 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
9824 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
3fa69ee7
GM
9825 || (message_cleared_p
9826 && minibuf_level == 0
9827 /* If the mini-window is currently selected, this means the
9828 echo-area doesn't show through. */
9829 && !MINI_WINDOW_P (XWINDOW (selected_window))))
90adcf20 9830 {
c6e89d6c 9831 int window_height_changed_p = echo_area_display (0);
90adcf20 9832 must_finish = 1;
6e019995
GM
9833
9834 /* If we don't display the current message, don't clear the
9835 message_cleared_p flag, because, if we did, we wouldn't clear
9836 the echo area in the next redisplay which doesn't preserve
9837 the echo area. */
9838 if (!display_last_displayed_message_p)
9839 message_cleared_p = 0;
2311178e 9840
c6e89d6c
GM
9841 if (fonts_changed_p)
9842 goto retry;
9843 else if (window_height_changed_p)
9844 {
9845 consider_all_windows_p = 1;
9846 ++update_mode_lines;
9847 ++windows_or_buffers_changed;
2311178e 9848
9142dd5b
GM
9849 /* If window configuration was changed, frames may have been
9850 marked garbaged. Clear them or we will experience
9851 surprises wrt scrolling. */
9852 if (frame_garbaged)
9853 clear_garbaged_frames ();
c6e89d6c 9854 }
90adcf20 9855 }
97a36635 9856 else if (EQ (selected_window, minibuf_window)
dd2eb166
GM
9857 && (current_buffer->clip_changed
9858 || XFASTINT (w->last_modified) < MODIFF
9859 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
92a90e89 9860 && resize_mini_window (w, 0))
c6e89d6c
GM
9861 {
9862 /* Resized active mini-window to fit the size of what it is
dd2eb166
GM
9863 showing if its contents might have changed. */
9864 must_finish = 1;
9865 consider_all_windows_p = 1;
c6e89d6c 9866 ++windows_or_buffers_changed;
dd2eb166 9867 ++update_mode_lines;
2311178e 9868
9142dd5b
GM
9869 /* If window configuration was changed, frames may have been
9870 marked garbaged. Clear them or we will experience
9871 surprises wrt scrolling. */
9872 if (frame_garbaged)
9873 clear_garbaged_frames ();
c6e89d6c 9874 }
2311178e 9875
90adcf20 9876
5f5c8ee5
GM
9877 /* If showing the region, and mark has changed, we must redisplay
9878 the whole window. The assignment to this_line_start_pos prevents
9879 the optimization directly below this if-statement. */
bd66d1ba
RS
9880 if (((!NILP (Vtransient_mark_mode)
9881 && !NILP (XBUFFER (w->buffer)->mark_active))
9882 != !NILP (w->region_showing))
82d04750
JB
9883 || (!NILP (w->region_showing)
9884 && !EQ (w->region_showing,
9885 Fmarker_position (XBUFFER (w->buffer)->mark))))
5f5c8ee5
GM
9886 CHARPOS (this_line_start_pos) = 0;
9887
9888 /* Optimize the case that only the line containing the cursor in the
9889 selected window has changed. Variables starting with this_ are
9890 set in display_line and record information about the line
9891 containing the cursor. */
9892 tlbufpos = this_line_start_pos;
9893 tlendpos = this_line_end_pos;
9894 if (!consider_all_windows_p
9895 && CHARPOS (tlbufpos) > 0
9896 && NILP (w->update_mode_line)
73af359d 9897 && !current_buffer->clip_changed
c62c1bb5 9898 && !current_buffer->prevent_redisplay_optimizations_p
44fa5b1e 9899 && FRAME_VISIBLE_P (XFRAME (w->frame))
f21ef775 9900 && !FRAME_OBSCURED_P (XFRAME (w->frame))
5f5c8ee5 9901 /* Make sure recorded data applies to current buffer, etc. */
a2889657
JB
9902 && this_line_buffer == current_buffer
9903 && current_buffer == XBUFFER (w->buffer)
265a9e55 9904 && NILP (w->force_start)
acda20e1 9905 && NILP (w->optional_new_start)
5f5c8ee5
GM
9906 /* Point must be on the line that we have info recorded about. */
9907 && PT >= CHARPOS (tlbufpos)
9908 && PT <= Z - CHARPOS (tlendpos)
a2889657
JB
9909 /* All text outside that line, including its final newline,
9910 must be unchanged */
5f5c8ee5
GM
9911 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
9912 CHARPOS (tlendpos)))
9913 {
9914 if (CHARPOS (tlbufpos) > BEGV
9915 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
9916 && (CHARPOS (tlbufpos) == ZV
9917 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
a2889657
JB
9918 /* Former continuation line has disappeared by becoming empty */
9919 goto cancel;
9920 else if (XFASTINT (w->last_modified) < MODIFF
8850a573 9921 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
a2889657
JB
9922 || MINI_WINDOW_P (w))
9923 {
1c9241f5
KH
9924 /* We have to handle the case of continuation around a
9925 wide-column character (See the comment in indent.c around
9926 line 885).
9927
9928 For instance, in the following case:
9929
9930 -------- Insert --------
9931 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
9932 J_I_ ==> J_I_ `^^' are cursors.
9933 ^^ ^^
9934 -------- --------
9935
9936 As we have to redraw the line above, we should goto cancel. */
9937
5f5c8ee5
GM
9938 struct it it;
9939 int line_height_before = this_line_pixel_height;
9940
9941 /* Note that start_display will handle the case that the
9942 line starting at tlbufpos is a continuation lines. */
9943 start_display (&it, w, tlbufpos);
9944
9945 /* Implementation note: It this still necessary? */
9946 if (it.current_x != this_line_start_x)
1c9241f5
KH
9947 goto cancel;
9948
5f5c8ee5
GM
9949 TRACE ((stderr, "trying display optimization 1\n"));
9950 w->cursor.vpos = -1;
a2889657 9951 overlay_arrow_seen = 0;
5f5c8ee5
GM
9952 it.vpos = this_line_vpos;
9953 it.current_y = this_line_y;
9954 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
9955 display_line (&it);
9956
a2889657 9957 /* If line contains point, is not continued,
5f5c8ee5 9958 and ends at same distance from eob as before, we win */
2311178e 9959 if (w->cursor.vpos >= 0
5f5c8ee5
GM
9960 /* Line is not continued, otherwise this_line_start_pos
9961 would have been set to 0 in display_line. */
9962 && CHARPOS (this_line_start_pos)
9963 /* Line ends as before. */
9964 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
9965 /* Line has same height as before. Otherwise other lines
9966 would have to be shifted up or down. */
9967 && this_line_pixel_height == line_height_before)
a2889657 9968 {
5f5c8ee5
GM
9969 /* If this is not the window's last line, we must adjust
9970 the charstarts of the lines below. */
9971 if (it.current_y < it.last_visible_y)
9972 {
9973 struct glyph_row *row
9974 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
9975 int delta, delta_bytes;
2311178e 9976
5f5c8ee5
GM
9977 if (Z - CHARPOS (tlendpos) == ZV)
9978 {
9979 /* This line ends at end of (accessible part of)
9980 buffer. There is no newline to count. */
9981 delta = (Z
9982 - CHARPOS (tlendpos)
9983 - MATRIX_ROW_START_CHARPOS (row));
9984 delta_bytes = (Z_BYTE
9985 - BYTEPOS (tlendpos)
9986 - MATRIX_ROW_START_BYTEPOS (row));
9987 }
9988 else
9989 {
9990 /* This line ends in a newline. Must take
9991 account of the newline and the rest of the
9992 text that follows. */
9993 delta = (Z
9994 - CHARPOS (tlendpos)
9995 - MATRIX_ROW_START_CHARPOS (row));
9996 delta_bytes = (Z_BYTE
9997 - BYTEPOS (tlendpos)
9998 - MATRIX_ROW_START_BYTEPOS (row));
9999 }
2311178e 10000
f2d86d7a
GM
10001 increment_matrix_positions (w->current_matrix,
10002 this_line_vpos + 1,
10003 w->current_matrix->nrows,
10004 delta, delta_bytes);
85bcef6c 10005 }
46db8486 10006
5f5c8ee5
GM
10007 /* If this row displays text now but previously didn't,
10008 or vice versa, w->window_end_vpos may have to be
10009 adjusted. */
10010 if ((it.glyph_row - 1)->displays_text_p)
10011 {
10012 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10013 XSETINT (w->window_end_vpos, this_line_vpos);
10014 }
10015 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10016 && this_line_vpos > 0)
10017 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10018 w->window_end_valid = Qnil;
2311178e 10019
5f5c8ee5
GM
10020 /* Update hint: No need to try to scroll in update_window. */
10021 w->desired_matrix->no_scrolling_p = 1;
10022
10023#if GLYPH_DEBUG
10024 *w->desired_matrix->method = 0;
10025 debug_method_add (w, "optimization 1");
10026#endif
7af0e8d7 10027#ifdef HAVE_WINDOW_SYSTEM
88e6b646 10028 update_window_fringes (w, 0);
7af0e8d7 10029#endif
a2889657
JB
10030 goto update;
10031 }
10032 else
10033 goto cancel;
10034 }
5f5c8ee5
GM
10035 else if (/* Cursor position hasn't changed. */
10036 PT == XFASTINT (w->last_point)
b6f0fe04
RS
10037 /* Make sure the cursor was last displayed
10038 in this window. Otherwise we have to reposition it. */
5f5c8ee5 10039 && 0 <= w->cursor.vpos
da8b7f4f 10040 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
a2889657
JB
10041 {
10042 if (!must_finish)
10043 {
c6e89d6c 10044 do_pending_window_change (1);
5f5c8ee5 10045
2311178e 10046 /* We used to always goto end_of_redisplay here, but this
5f5c8ee5
GM
10047 isn't enough if we have a blinking cursor. */
10048 if (w->cursor_off_p == w->last_cursor_off_p)
10049 goto end_of_redisplay;
a2889657
JB
10050 }
10051 goto update;
10052 }
8b51f1e3
KH
10053 /* If highlighting the region, or if the cursor is in the echo area,
10054 then we can't just move the cursor. */
bd66d1ba
RS
10055 else if (! (!NILP (Vtransient_mark_mode)
10056 && !NILP (current_buffer->mark_active))
97a36635 10057 && (EQ (selected_window, current_buffer->last_selected_window)
293a54ce 10058 || highlight_nonselected_windows)
8b51f1e3 10059 && NILP (w->region_showing)
8f897821 10060 && NILP (Vshow_trailing_whitespace)
8b51f1e3 10061 && !cursor_in_echo_area)
a2889657 10062 {
5f5c8ee5
GM
10063 struct it it;
10064 struct glyph_row *row;
10065
10066 /* Skip from tlbufpos to PT and see where it is. Note that
10067 PT may be in invisible text. If so, we will end at the
10068 next visible position. */
10069 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10070 NULL, DEFAULT_FACE_ID);
10071 it.current_x = this_line_start_x;
10072 it.current_y = this_line_y;
10073 it.vpos = this_line_vpos;
2311178e 10074
5f5c8ee5
GM
10075 /* The call to move_it_to stops in front of PT, but
10076 moves over before-strings. */
10077 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10078
10079 if (it.vpos == this_line_vpos
10080 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10081 row->enabled_p))
a2889657 10082 {
5f5c8ee5
GM
10083 xassert (this_line_vpos == it.vpos);
10084 xassert (this_line_y == it.current_y);
10085 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
ade0bee1
GM
10086#if GLYPH_DEBUG
10087 *w->desired_matrix->method = 0;
10088 debug_method_add (w, "optimization 3");
10089#endif
a2889657
JB
10090 goto update;
10091 }
10092 else
10093 goto cancel;
10094 }
5f5c8ee5 10095
a2889657 10096 cancel:
5f5c8ee5
GM
10097 /* Text changed drastically or point moved off of line. */
10098 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
a2889657
JB
10099 }
10100
5f5c8ee5
GM
10101 CHARPOS (this_line_start_pos) = 0;
10102 consider_all_windows_p |= buffer_shared > 1;
10103 ++clear_face_cache_count;
a2889657 10104
2311178e 10105
bd9d0f3f
GM
10106 /* Build desired matrices, and update the display. If
10107 consider_all_windows_p is non-zero, do it for all windows on all
10108 frames. Otherwise do it for selected_window, only. */
463f6b91 10109
5f5c8ee5 10110 if (consider_all_windows_p)
a2889657 10111 {
35f56f96 10112 Lisp_Object tail, frame;
0528abe1
GM
10113 int i, n = 0, size = 50;
10114 struct frame **updated
10115 = (struct frame **) alloca (size * sizeof *updated);
a2889657 10116
5f5c8ee5
GM
10117 /* Clear the face cache eventually. */
10118 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
463f6b91 10119 {
5f5c8ee5 10120 clear_face_cache (0);
463f6b91
RS
10121 clear_face_cache_count = 0;
10122 }
31b24551 10123
5f5c8ee5
GM
10124 /* Recompute # windows showing selected buffer. This will be
10125 incremented each time such a window is displayed. */
a2889657
JB
10126 buffer_shared = 0;
10127
35f56f96 10128 FOR_EACH_FRAME (tail, frame)
30c566e4 10129 {
5f5c8ee5 10130 struct frame *f = XFRAME (frame);
2311178e 10131
886bd6f2 10132 if (FRAME_WINDOW_P (f) || f == sf)
9769686d 10133 {
dd429b03
KH
10134 if (! EQ (frame, selected_frame))
10135 /* Select the frame, for the sake of frame-local
10136 variables. */
10137 select_frame_for_redisplay (frame);
10138
ae02e06a 10139#ifdef HAVE_WINDOW_SYSTEM
bb336f8d
RS
10140 if (clear_face_cache_count % 50 == 0
10141 && FRAME_WINDOW_P (f))
10142 clear_image_cache (f, 0);
ae02e06a 10143#endif /* HAVE_WINDOW_SYSTEM */
bb336f8d 10144
5f5c8ee5
GM
10145 /* Mark all the scroll bars to be removed; we'll redeem
10146 the ones we want when we redisplay their windows. */
9769686d 10147 if (condemn_scroll_bars_hook)
504454e8 10148 condemn_scroll_bars_hook (f);
30c566e4 10149
f21ef775 10150 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
5f5c8ee5 10151 redisplay_windows (FRAME_ROOT_WINDOW (f));
30c566e4 10152
5f5c8ee5
GM
10153 /* Any scroll bars which redisplay_windows should have
10154 nuked should now go away. */
9769686d 10155 if (judge_scroll_bars_hook)
504454e8 10156 judge_scroll_bars_hook (f);
bd9d0f3f
GM
10157
10158 /* If fonts changed, display again. */
b60c9653
RS
10159 /* ??? rms: I suspect it is a mistake to jump all the way
10160 back to retry here. It should just retry this frame. */
bd9d0f3f
GM
10161 if (fonts_changed_p)
10162 goto retry;
2311178e 10163
bd9d0f3f
GM
10164 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10165 {
10166 /* See if we have to hscroll. */
10167 if (hscroll_windows (f->root_window))
10168 goto retry;
10169
10170 /* Prevent various kinds of signals during display
10171 update. stdio is not robust about handling
10172 signals, which can cause an apparent I/O
10173 error. */
10174 if (interrupt_input)
10175 unrequest_sigio ();
aac2d8b2 10176 STOP_POLLING;
bd9d0f3f
GM
10177
10178 /* Update the display. */
10179 set_window_update_flags (XWINDOW (f->root_window), 1);
10180 pause |= update_frame (f, 0, 0);
ccbb9ed2 10181#if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
bd9d0f3f
GM
10182 if (pause)
10183 break;
ccbb9ed2 10184#endif
bd9d0f3f 10185
0528abe1
GM
10186 if (n == size)
10187 {
10188 int nbytes = size * sizeof *updated;
10189 struct frame **p = (struct frame **) alloca (2 * nbytes);
10190 bcopy (updated, p, nbytes);
10191 size *= 2;
10192 }
2311178e 10193
0528abe1 10194 updated[n++] = f;
bd9d0f3f 10195 }
9769686d 10196 }
30c566e4 10197 }
0528abe1 10198
6f68b035
GM
10199 if (!pause)
10200 {
10201 /* Do the mark_window_display_accurate after all windows have
10202 been redisplayed because this call resets flags in buffers
10203 which are needed for proper redisplay. */
10204 for (i = 0; i < n; ++i)
10205 {
10206 struct frame *f = updated[i];
10207 mark_window_display_accurate (f->root_window, 1);
10208 if (frame_up_to_date_hook)
10209 frame_up_to_date_hook (f);
10210 }
0528abe1 10211 }
a2889657 10212 }
bd9d0f3f
GM
10213 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10214 {
10215 Lisp_Object mini_window;
10216 struct frame *mini_frame;
5f5c8ee5 10217
82a7ab23 10218 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
7033d6df
RS
10219 /* Use list_of_error, not Qerror, so that
10220 we catch only errors and don't run the debugger. */
10221 internal_condition_case_1 (redisplay_window_1, selected_window,
10222 list_of_error,
82a7ab23 10223 redisplay_window_error);
2311178e 10224
bd9d0f3f 10225 /* Compare desired and current matrices, perform output. */
7d0393cf 10226
26683087 10227 update:
bd9d0f3f
GM
10228 /* If fonts changed, display again. */
10229 if (fonts_changed_p)
92a90e89 10230 goto retry;
a2889657 10231
bd9d0f3f
GM
10232 /* Prevent various kinds of signals during display update.
10233 stdio is not robust about handling signals,
10234 which can cause an apparent I/O error. */
10235 if (interrupt_input)
10236 unrequest_sigio ();
aac2d8b2 10237 STOP_POLLING;
1af9f229 10238
bd9d0f3f 10239 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
5f5c8ee5 10240 {
92a90e89
GM
10241 if (hscroll_windows (selected_window))
10242 goto retry;
2311178e 10243
5f5c8ee5 10244 XWINDOW (selected_window)->must_be_updated_p = 1;
886bd6f2 10245 pause = update_frame (sf, 0, 0);
5f5c8ee5 10246 }
d724d989 10247
8de2d90b 10248 /* We may have called echo_area_display at the top of this
44fa5b1e
JB
10249 function. If the echo area is on another frame, that may
10250 have put text on a frame other than the selected one, so the
10251 above call to update_frame would not have caught it. Catch
8de2d90b 10252 it here. */
bd9d0f3f
GM
10253 mini_window = FRAME_MINIBUF_WINDOW (sf);
10254 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
2311178e 10255
bd9d0f3f
GM
10256 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10257 {
10258 XWINDOW (mini_window)->must_be_updated_p = 1;
10259 pause |= update_frame (mini_frame, 0, 0);
10260 if (!pause && hscroll_windows (mini_window))
10261 goto retry;
10262 }
6e8290aa 10263 }
a2889657 10264
5f5c8ee5
GM
10265 /* If display was paused because of pending input, make sure we do a
10266 thorough update the next time. */
a2889657
JB
10267 if (pause)
10268 {
5f5c8ee5
GM
10269 /* Prevent the optimization at the beginning of
10270 redisplay_internal that tries a single-line update of the
10271 line containing the cursor in the selected window. */
10272 CHARPOS (this_line_start_pos) = 0;
10273
10274 /* Let the overlay arrow be updated the next time. */
351b5434 10275 update_overlay_arrows (0);
2311178e 10276
5f5c8ee5
GM
10277 /* If we pause after scrolling, some rows in the current
10278 matrices of some windows are not valid. */
10279 if (!WINDOW_FULL_WIDTH_P (w)
10280 && !FRAME_WINDOW_P (XFRAME (w->frame)))
a2889657
JB
10281 update_mode_lines = 1;
10282 }
43c09969 10283 else
a2889657 10284 {
43c09969 10285 if (!consider_all_windows_p)
a2889657 10286 {
43c09969
GM
10287 /* This has already been done above if
10288 consider_all_windows_p is set. */
10289 mark_window_display_accurate_1 (w, 1);
2311178e 10290
351b5434
KS
10291 /* Say overlay arrows are up to date. */
10292 update_overlay_arrows (1);
2311178e 10293
efc63ef0 10294 if (frame_up_to_date_hook != 0)
43c09969 10295 frame_up_to_date_hook (sf);
a2889657 10296 }
15e26c76 10297
a2889657
JB
10298 update_mode_lines = 0;
10299 windows_or_buffers_changed = 0;
5fb96e96 10300 cursor_type_changed = 0;
a2889657
JB
10301 }
10302
5f5c8ee5
GM
10303 /* Start SIGIO interrupts coming again. Having them off during the
10304 code above makes it less likely one will discard output, but not
10305 impossible, since there might be stuff in the system buffer here.
a2889657 10306 But it is much hairier to try to do anything about that. */
a2889657
JB
10307 if (interrupt_input)
10308 request_sigio ();
aac2d8b2 10309 RESUME_POLLING;
a2889657 10310
5f5c8ee5
GM
10311 /* If a frame has become visible which was not before, redisplay
10312 again, so that we display it. Expose events for such a frame
10313 (which it gets when becoming visible) don't call the parts of
10314 redisplay constructing glyphs, so simply exposing a frame won't
10315 display anything in this case. So, we have to display these
10316 frames here explicitly. */
11c52c4f
RS
10317 if (!pause)
10318 {
10319 Lisp_Object tail, frame;
10320 int new_count = 0;
10321
10322 FOR_EACH_FRAME (tail, frame)
10323 {
10324 int this_is_visible = 0;
8e83f802
RS
10325
10326 if (XFRAME (frame)->visible)
10327 this_is_visible = 1;
10328 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10329 if (XFRAME (frame)->visible)
10330 this_is_visible = 1;
11c52c4f
RS
10331
10332 if (this_is_visible)
10333 new_count++;
10334 }
10335
89819bdd 10336 if (new_count != number_of_visible_frames)
11c52c4f
RS
10337 windows_or_buffers_changed++;
10338 }
10339
44fa5b1e 10340 /* Change frame size now if a change is pending. */
c6e89d6c 10341 do_pending_window_change (1);
d8e242fd 10342
8b32d885
RS
10343 /* If we just did a pending size change, or have additional
10344 visible frames, redisplay again. */
3c8c72e0 10345 if (windows_or_buffers_changed && !pause)
8b32d885 10346 goto retry;
5f5c8ee5 10347
1987b083 10348 end_of_redisplay:
28514cd9 10349 unbind_to (count, Qnil);
aac2d8b2 10350 RESUME_POLLING;
a2889657
JB
10351}
10352
5f5c8ee5
GM
10353
10354/* Redisplay, but leave alone any recent echo area message unless
10355 another message has been requested in its place.
a2889657
JB
10356
10357 This is useful in situations where you need to redisplay but no
10358 user action has occurred, making it inappropriate for the message
10359 area to be cleared. See tracking_off and
9bf76936
GM
10360 wait_reading_process_input for examples of these situations.
10361
10362 FROM_WHERE is an integer saying from where this function was
10363 called. This is useful for debugging. */
a2889657 10364
8991bb31 10365void
9bf76936
GM
10366redisplay_preserve_echo_area (from_where)
10367 int from_where;
a2889657 10368{
9bf76936
GM
10369 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10370
c6e89d6c 10371 if (!NILP (echo_area_buffer[1]))
a2889657 10372 {
c6e89d6c
GM
10373 /* We have a previously displayed message, but no current
10374 message. Redisplay the previous message. */
10375 display_last_displayed_message_p = 1;
e9874cee 10376 redisplay_internal (1);
c6e89d6c 10377 display_last_displayed_message_p = 0;
a2889657
JB
10378 }
10379 else
e9874cee 10380 redisplay_internal (1);
a2889657
JB
10381}
10382
5f5c8ee5 10383
28514cd9 10384/* Function registered with record_unwind_protect in
acfca545
RS
10385 redisplay_internal. Reset redisplaying_p to the value it had
10386 before redisplay_internal was called, and clear
dd429b03
KH
10387 prevent_freeing_realized_faces_p. It also selects the previously
10388 selected frame. */
28514cd9
GM
10389
10390static Lisp_Object
dd429b03
KH
10391unwind_redisplay (val)
10392 Lisp_Object val;
28514cd9 10393{
dd429b03
KH
10394 Lisp_Object old_redisplaying_p, old_frame;
10395
10396 old_redisplaying_p = XCAR (val);
28514cd9 10397 redisplaying_p = XFASTINT (old_redisplaying_p);
dd429b03
KH
10398 old_frame = XCDR (val);
10399 if (! EQ (old_frame, selected_frame))
10400 select_frame_for_redisplay (old_frame);
c6e89d6c 10401 return Qnil;
28514cd9
GM
10402}
10403
10404
43c09969
GM
10405/* Mark the display of window W as accurate or inaccurate. If
10406 ACCURATE_P is non-zero mark display of W as accurate. If
10407 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10408 redisplay_internal is called. */
5f5c8ee5 10409
43c09969
GM
10410static void
10411mark_window_display_accurate_1 (w, accurate_p)
10412 struct window *w;
5f5c8ee5 10413 int accurate_p;
a2889657 10414{
43c09969 10415 if (BUFFERP (w->buffer))
a2889657 10416 {
43c09969 10417 struct buffer *b = XBUFFER (w->buffer);
2311178e 10418
43c09969
GM
10419 w->last_modified
10420 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10421 w->last_overlay_modified
10422 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10423 w->last_had_star
10424 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
a2889657 10425
43c09969 10426 if (accurate_p)
bd66d1ba 10427 {
43c09969
GM
10428 b->clip_changed = 0;
10429 b->prevent_redisplay_optimizations_p = 0;
10430
10431 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10432 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10433 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10434 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
2311178e 10435
43c09969
GM
10436 w->current_matrix->buffer = b;
10437 w->current_matrix->begv = BUF_BEGV (b);
10438 w->current_matrix->zv = BUF_ZV (b);
2311178e 10439
43c09969
GM
10440 w->last_cursor = w->cursor;
10441 w->last_cursor_off_p = w->cursor_off_p;
2311178e 10442
43c09969
GM
10443 if (w == XWINDOW (selected_window))
10444 w->last_point = make_number (BUF_PT (b));
10445 else
10446 w->last_point = make_number (XMARKER (w->pointm)->charpos);
bd66d1ba 10447 }
43c09969 10448 }
bd66d1ba 10449
43c09969
GM
10450 if (accurate_p)
10451 {
d2f84654 10452 w->window_end_valid = w->buffer;
99332eb6 10453#if 0 /* This is incorrect with variable-height lines. */
2913a9c0 10454 xassert (XINT (w->window_end_vpos)
da8b7f4f 10455 < (WINDOW_TOTAL_LINES (w)
2913a9c0 10456 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
99332eb6 10457#endif
a2889657 10458 w->update_mode_line = Qnil;
43c09969
GM
10459 }
10460}
10461
10462
10463/* Mark the display of windows in the window tree rooted at WINDOW as
10464 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10465 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10466 be redisplayed the next time redisplay_internal is called. */
10467
10468void
10469mark_window_display_accurate (window, accurate_p)
10470 Lisp_Object window;
10471 int accurate_p;
10472{
10473 struct window *w;
2311178e 10474
43c09969
GM
10475 for (; !NILP (window); window = w->next)
10476 {
10477 w = XWINDOW (window);
10478 mark_window_display_accurate_1 (w, accurate_p);
a2889657 10479
265a9e55 10480 if (!NILP (w->vchild))
5f5c8ee5 10481 mark_window_display_accurate (w->vchild, accurate_p);
265a9e55 10482 if (!NILP (w->hchild))
5f5c8ee5 10483 mark_window_display_accurate (w->hchild, accurate_p);
a2889657
JB
10484 }
10485
5f5c8ee5 10486 if (accurate_p)
a2889657 10487 {
351b5434 10488 update_overlay_arrows (1);
a2889657
JB
10489 }
10490 else
10491 {
5f5c8ee5
GM
10492 /* Force a thorough redisplay the next time by setting
10493 last_arrow_position and last_arrow_string to t, which is
4b41cebb 10494 unequal to any useful value of Voverlay_arrow_... */
351b5434 10495 update_overlay_arrows (-1);
a2889657
JB
10496 }
10497}
5f5c8ee5
GM
10498
10499
10500/* Return value in display table DP (Lisp_Char_Table *) for character
10501 C. Since a display table doesn't have any parent, we don't have to
10502 follow parent. Do not call this function directly but use the
10503 macro DISP_CHAR_VECTOR. */
10504
10505Lisp_Object
10506disp_char_vector (dp, c)
10507 struct Lisp_Char_Table *dp;
10508 int c;
10509{
10510 int code[4], i;
10511 Lisp_Object val;
10512
10513 if (SINGLE_BYTE_CHAR_P (c))
10514 return (dp->contents[c]);
2311178e 10515
c5924f47 10516 SPLIT_CHAR (c, code[0], code[1], code[2]);
260a86a0
KH
10517 if (code[1] < 32)
10518 code[1] = -1;
10519 else if (code[2] < 32)
10520 code[2] = -1;
2311178e 10521
5f5c8ee5
GM
10522 /* Here, the possible range of code[0] (== charset ID) is
10523 128..max_charset. Since the top level char table contains data
10524 for multibyte characters after 256th element, we must increment
10525 code[0] by 128 to get a correct index. */
10526 code[0] += 128;
10527 code[3] = -1; /* anchor */
10528
10529 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10530 {
10531 val = dp->contents[code[i]];
10532 if (!SUB_CHAR_TABLE_P (val))
10533 return (NILP (val) ? dp->defalt : val);
10534 }
2311178e 10535
5f5c8ee5
GM
10536 /* Here, val is a sub char table. We return the default value of
10537 it. */
10538 return (dp->defalt);
10539}
10540
10541
a2889657 10542\f
5f5c8ee5
GM
10543/***********************************************************************
10544 Window Redisplay
10545 ***********************************************************************/
a2725ab2 10546
5f5c8ee5 10547/* Redisplay all leaf windows in the window tree rooted at WINDOW. */
90adcf20
RS
10548
10549static void
5f5c8ee5
GM
10550redisplay_windows (window)
10551 Lisp_Object window;
90adcf20 10552{
5f5c8ee5
GM
10553 while (!NILP (window))
10554 {
10555 struct window *w = XWINDOW (window);
2311178e 10556
5f5c8ee5
GM
10557 if (!NILP (w->hchild))
10558 redisplay_windows (w->hchild);
10559 else if (!NILP (w->vchild))
10560 redisplay_windows (w->vchild);
10561 else
82a7ab23
RS
10562 {
10563 displayed_buffer = XBUFFER (w->buffer);
7033d6df
RS
10564 /* Use list_of_error, not Qerror, so that
10565 we catch only errors and don't run the debugger. */
2311178e 10566 internal_condition_case_1 (redisplay_window_0, window,
7033d6df 10567 list_of_error,
82a7ab23
RS
10568 redisplay_window_error);
10569 }
a2725ab2 10570
5f5c8ee5
GM
10571 window = w->next;
10572 }
10573}
10574
82a7ab23
RS
10575static Lisp_Object
10576redisplay_window_error ()
10577{
10578 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10579 return Qnil;
10580}
10581
10582static Lisp_Object
10583redisplay_window_0 (window)
10584 Lisp_Object window;
10585{
10586 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10587 redisplay_window (window, 0);
10588 return Qnil;
10589}
5f5c8ee5 10590
82a7ab23
RS
10591static Lisp_Object
10592redisplay_window_1 (window)
10593 Lisp_Object window;
10594{
10595 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10596 redisplay_window (window, 1);
10597 return Qnil;
10598}
10599\f
bc6371a6
KH
10600
10601/* Increment GLYPH until it reaches END or CONDITION fails while
10602 adding (GLYPH)->pixel_width to X. */
10603
10604#define SKIP_GLYPHS(glyph, end, x, condition) \
10605 do \
10606 { \
10607 (x) += (glyph)->pixel_width; \
10608 ++(glyph); \
10609 } \
10610 while ((glyph) < (end) && (condition))
10611
10612
5f5c8ee5
GM
10613/* Set cursor position of W. PT is assumed to be displayed in ROW.
10614 DELTA is the number of bytes by which positions recorded in ROW
10615 differ from current buffer positions. */
10616
10617void
10618set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10619 struct window *w;
10620 struct glyph_row *row;
10621 struct glyph_matrix *matrix;
10622 int delta, delta_bytes, dy, dvpos;
10623{
10624 struct glyph *glyph = row->glyphs[TEXT_AREA];
10625 struct glyph *end = glyph + row->used[TEXT_AREA];
ae73dc1c
KH
10626 /* The first glyph that starts a sequence of glyphs from string. */
10627 struct glyph *string_start;
10628 /* The X coordinate of string_start. */
10629 int string_start_x;
10630 /* The last known character position. */
10631 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10632 /* The last known character position before string_start. */
10633 int string_before_pos;
5f5c8ee5
GM
10634 int x = row->x;
10635 int pt_old = PT - delta;
10636
10637 /* Skip over glyphs not having an object at the start of the row.
10638 These are special glyphs like truncation marks on terminal
10639 frames. */
10640 if (row->displays_text_p)
10641 while (glyph < end
6fc556fd 10642 && INTEGERP (glyph->object)
5f5c8ee5
GM
10643 && glyph->charpos < 0)
10644 {
10645 x += glyph->pixel_width;
10646 ++glyph;
10647 }
10648
ae73dc1c 10649 string_start = NULL;
5f5c8ee5 10650 while (glyph < end
6fc556fd 10651 && !INTEGERP (glyph->object)
5f5c8ee5 10652 && (!BUFFERP (glyph->object)
ae73dc1c 10653 || (last_pos = glyph->charpos) < pt_old))
5f5c8ee5 10654 {
ae73dc1c
KH
10655 if (! STRINGP (glyph->object))
10656 {
10657 string_start = NULL;
10658 x += glyph->pixel_width;
10659 ++glyph;
10660 }
10661 else
10662 {
10663 string_before_pos = last_pos;
10664 string_start = glyph;
10665 string_start_x = x;
10666 /* Skip all glyphs from string. */
bc6371a6 10667 SKIP_GLYPHS (glyph, end, x, STRINGP (glyph->object));
ae73dc1c
KH
10668 }
10669 }
10670
10671 if (string_start
10672 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
10673 {
10674 /* We may have skipped over point because the previous glyphs
10675 are from string. As there's no easy way to know the
10676 character position of the current glyph, find the correct
10677 glyph on point by scanning from string_start again. */
bc6371a6
KH
10678 Lisp_Object limit;
10679 Lisp_Object string;
10680 int pos;
ae73dc1c 10681
bc6371a6
KH
10682 limit = make_number (pt_old + 1);
10683 end = glyph;
ae73dc1c
KH
10684 glyph = string_start;
10685 x = string_start_x;
bc6371a6
KH
10686 string = glyph->object;
10687 pos = string_buffer_position (w, string, string_before_pos);
10688 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
10689 because we always put cursor after overlay strings. */
10690 while (pos == 0 && glyph < end)
ae73dc1c 10691 {
bc6371a6
KH
10692 string = glyph->object;
10693 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10694 if (glyph < end)
10695 pos = string_buffer_position (w, glyph->object, string_before_pos);
10696 }
10697
10698 while (glyph < end)
10699 {
10700 pos = XINT (Fnext_single_char_property_change
10701 (make_number (pos), Qdisplay, Qnil, limit));
10702 if (pos > pt_old)
10703 break;
ae73dc1c 10704 /* Skip glyphs from the same string. */
bc6371a6
KH
10705 string = glyph->object;
10706 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10707 /* Skip glyphs from an overlay. */
10708 while (glyph < end
10709 && ! string_buffer_position (w, glyph->object, pos))
ae73dc1c 10710 {
bc6371a6
KH
10711 string = glyph->object;
10712 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
ae73dc1c 10713 }
ae73dc1c 10714 }
5f5c8ee5
GM
10715 }
10716
10717 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
10718 w->cursor.x = x;
10719 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
10720 w->cursor.y = row->y + dy;
10721
10722 if (w == XWINDOW (selected_window))
10723 {
10724 if (!row->continued_p
10725 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
10726 && row->x == 0)
10727 {
10728 this_line_buffer = XBUFFER (w->buffer);
2311178e 10729
5f5c8ee5
GM
10730 CHARPOS (this_line_start_pos)
10731 = MATRIX_ROW_START_CHARPOS (row) + delta;
10732 BYTEPOS (this_line_start_pos)
10733 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
2311178e 10734
5f5c8ee5
GM
10735 CHARPOS (this_line_end_pos)
10736 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
10737 BYTEPOS (this_line_end_pos)
10738 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
2311178e 10739
5f5c8ee5
GM
10740 this_line_y = w->cursor.y;
10741 this_line_pixel_height = row->height;
10742 this_line_vpos = w->cursor.vpos;
10743 this_line_start_x = row->x;
10744 }
10745 else
10746 CHARPOS (this_line_start_pos) = 0;
10747 }
10748}
10749
10750
10751/* Run window scroll functions, if any, for WINDOW with new window
756a3cb6
RS
10752 start STARTP. Sets the window start of WINDOW to that position.
10753
10754 We assume that the window's buffer is really current. */
5f5c8ee5
GM
10755
10756static INLINE struct text_pos
10757run_window_scroll_functions (window, startp)
10758 Lisp_Object window;
10759 struct text_pos startp;
10760{
10761 struct window *w = XWINDOW (window);
10762 SET_MARKER_FROM_TEXT_POS (w->start, startp);
756a3cb6
RS
10763
10764 if (current_buffer != XBUFFER (w->buffer))
10765 abort ();
10766
5f5c8ee5
GM
10767 if (!NILP (Vwindow_scroll_functions))
10768 {
2311178e 10769 run_hook_with_args_2 (Qwindow_scroll_functions, window,
5f5c8ee5
GM
10770 make_number (CHARPOS (startp)));
10771 SET_TEXT_POS_FROM_MARKER (startp, w->start);
756a3cb6
RS
10772 /* In case the hook functions switch buffers. */
10773 if (current_buffer != XBUFFER (w->buffer))
10774 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5 10775 }
90adcf20 10776
5f5c8ee5
GM
10777 return startp;
10778}
10779
10780
ddf6b9a3
RS
10781/* Make sure the line containing the cursor is fully visible.
10782 A value of 1 means there is nothing to be done.
10783 (Either the line is fully visible, or it cannot be made so,
10784 or we cannot tell.)
cb9d4a9f
KS
10785
10786 If FORCE_P is non-zero, return 0 even if partial visible cursor row
10787 is higher than window.
10788
ddf6b9a3
RS
10789 A value of 0 means the caller should do scrolling
10790 as if point had gone off the screen. */
5f5c8ee5 10791
cb617e7c 10792static int
cb9d4a9f 10793make_cursor_line_fully_visible (w, force_p)
5f5c8ee5 10794 struct window *w;
cb9d4a9f 10795 int force_p;
5f5c8ee5
GM
10796{
10797 struct glyph_matrix *matrix;
10798 struct glyph_row *row;
478d746b 10799 int window_height;
2311178e 10800
5f5c8ee5
GM
10801 /* It's not always possible to find the cursor, e.g, when a window
10802 is full of overlay strings. Don't do anything in that case. */
10803 if (w->cursor.vpos < 0)
cb617e7c 10804 return 1;
2311178e 10805
5f5c8ee5
GM
10806 matrix = w->desired_matrix;
10807 row = MATRIX_ROW (matrix, w->cursor.vpos);
10808
acda20e1 10809 /* If the cursor row is not partially visible, there's nothing to do. */
b28cb6ed 10810 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
cb617e7c 10811 return 1;
b28cb6ed
GM
10812
10813 /* If the row the cursor is in is taller than the window's height,
10814 it's not clear what to do, so do nothing. */
10815 window_height = window_box_height (w);
10816 if (row->height >= window_height)
e893970b
KS
10817 {
10818 if (!force_p || w->vscroll)
10819 return 1;
10820 }
ddf6b9a3
RS
10821 return 0;
10822
10823#if 0
10824 /* This code used to try to scroll the window just enough to make
10825 the line visible. It returned 0 to say that the caller should
10826 allocate larger glyph matrices. */
10827
b28cb6ed 10828 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
5f5c8ee5
GM
10829 {
10830 int dy = row->height - row->visible_height;
10831 w->vscroll = 0;
10832 w->cursor.y += dy;
10833 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10834 }
b28cb6ed 10835 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
5f5c8ee5
GM
10836 {
10837 int dy = - (row->height - row->visible_height);
10838 w->vscroll = dy;
10839 w->cursor.y += dy;
10840 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10841 }
2311178e 10842
5f5c8ee5
GM
10843 /* When we change the cursor y-position of the selected window,
10844 change this_line_y as well so that the display optimization for
10845 the cursor line of the selected window in redisplay_internal uses
10846 the correct y-position. */
10847 if (w == XWINDOW (selected_window))
10848 this_line_y = w->cursor.y;
cb617e7c
GM
10849
10850 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
10851 redisplay with larger matrices. */
10852 if (matrix->nrows < required_matrix_height (w))
10853 {
10854 fonts_changed_p = 1;
10855 return 0;
10856 }
10857
10858 return 1;
ddf6b9a3 10859#endif /* 0 */
5f5c8ee5
GM
10860}
10861
10862
10863/* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
10864 non-zero means only WINDOW is redisplayed in redisplay_internal.
10865 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
10866 in redisplay_window to bring a partially visible line into view in
10867 the case that only the cursor has moved.
10868
03b0a4b4
RS
10869 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
10870 last screen line's vertical height extends past the end of the screen.
10871
5f5c8ee5
GM
10872 Value is
10873
10874 1 if scrolling succeeded
2311178e 10875
5f5c8ee5 10876 0 if scrolling didn't find point.
2311178e 10877
5f5c8ee5
GM
10878 -1 if new fonts have been loaded so that we must interrupt
10879 redisplay, adjust glyph matrices, and try again. */
10880
cb617e7c
GM
10881enum
10882{
10883 SCROLLING_SUCCESS,
10884 SCROLLING_FAILED,
10885 SCROLLING_NEED_LARGER_MATRICES
10886};
10887
5f5c8ee5
GM
10888static int
10889try_scrolling (window, just_this_one_p, scroll_conservatively,
03b0a4b4 10890 scroll_step, temp_scroll_step, last_line_misfit)
5f5c8ee5
GM
10891 Lisp_Object window;
10892 int just_this_one_p;
31ade731 10893 EMACS_INT scroll_conservatively, scroll_step;
5f5c8ee5 10894 int temp_scroll_step;
03b0a4b4 10895 int last_line_misfit;
5f5c8ee5
GM
10896{
10897 struct window *w = XWINDOW (window);
10898 struct frame *f = XFRAME (w->frame);
10899 struct text_pos scroll_margin_pos;
10900 struct text_pos pos;
10901 struct text_pos startp;
10902 struct it it;
10903 Lisp_Object window_end;
10904 int this_scroll_margin;
10905 int dy = 0;
10906 int scroll_max;
b8a63ccb 10907 int rc;
5f5c8ee5
GM
10908 int amount_to_scroll = 0;
10909 Lisp_Object aggressive;
10910 int height;
cb9d4a9f 10911 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
5f5c8ee5
GM
10912
10913#if GLYPH_DEBUG
10914 debug_method_add (w, "try_scrolling");
78614721 10915#endif
5f5c8ee5
GM
10916
10917 SET_TEXT_POS_FROM_MARKER (startp, w->start);
2311178e 10918
5f5c8ee5
GM
10919 /* Compute scroll margin height in pixels. We scroll when point is
10920 within this distance from the top or bottom of the window. */
10921 if (scroll_margin > 0)
90adcf20 10922 {
da8b7f4f
KS
10923 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
10924 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
5f5c8ee5
GM
10925 }
10926 else
10927 this_scroll_margin = 0;
10928
75c5501b
JB
10929 /* Force scroll_conservatively to have a reasonable value so it doesn't
10930 cause an overflow while computing how much to scroll. */
10931 if (scroll_conservatively)
10932 scroll_conservatively = min (scroll_conservatively,
10933 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
10934
5f5c8ee5
GM
10935 /* Compute how much we should try to scroll maximally to bring point
10936 into view. */
0894e696
SM
10937 if (scroll_step || scroll_conservatively || temp_scroll_step)
10938 scroll_max = max (scroll_step,
10939 max (scroll_conservatively, temp_scroll_step));
5f5c8ee5
GM
10940 else if (NUMBERP (current_buffer->scroll_down_aggressively)
10941 || NUMBERP (current_buffer->scroll_up_aggressively))
10942 /* We're trying to scroll because of aggressive scrolling
10943 but no scroll_step is set. Choose an arbitrary one. Maybe
10944 there should be a variable for this. */
10945 scroll_max = 10;
10946 else
10947 scroll_max = 0;
da8b7f4f 10948 scroll_max *= FRAME_LINE_HEIGHT (f);
5f5c8ee5
GM
10949
10950 /* Decide whether we have to scroll down. Start at the window end
10951 and move this_scroll_margin up to find the position of the scroll
10952 margin. */
10953 window_end = Fwindow_end (window, Qt);
03b0a4b4
RS
10954
10955 too_near_end:
10956
5f5c8ee5
GM
10957 CHARPOS (scroll_margin_pos) = XINT (window_end);
10958 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
03b0a4b4 10959
cb9d4a9f 10960 if (this_scroll_margin || extra_scroll_margin_lines)
5f5c8ee5
GM
10961 {
10962 start_display (&it, w, scroll_margin_pos);
cb9d4a9f
KS
10963 if (this_scroll_margin)
10964 move_it_vertically (&it, - this_scroll_margin);
10965 if (extra_scroll_margin_lines)
10966 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
5f5c8ee5
GM
10967 scroll_margin_pos = it.current.pos;
10968 }
10969
10970 if (PT >= CHARPOS (scroll_margin_pos))
10971 {
10972 int y0;
2311178e 10973
5f5c8ee5
GM
10974 /* Point is in the scroll margin at the bottom of the window, or
10975 below. Compute a new window start that makes point visible. */
47589c8c 10976
5f5c8ee5
GM
10977 /* Compute the distance from the scroll margin to PT.
10978 Give up if the distance is greater than scroll_max. */
10979 start_display (&it, w, scroll_margin_pos);
10980 y0 = it.current_y;
10981 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10982 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
539e92ad
GM
10983
10984 /* To make point visible, we have to move the window start
10985 down so that the line the cursor is in is visible, which
10986 means we have to add in the height of the cursor line. */
10987 dy = line_bottom_y (&it) - y0;
2311178e 10988
5f5c8ee5 10989 if (dy > scroll_max)
cb617e7c 10990 return SCROLLING_FAILED;
2311178e 10991
5f5c8ee5
GM
10992 /* Move the window start down. If scrolling conservatively,
10993 move it just enough down to make point visible. If
10994 scroll_step is set, move it down by scroll_step. */
10995 start_display (&it, w, startp);
10996
10997 if (scroll_conservatively)
03b0a4b4
RS
10998 /* Set AMOUNT_TO_SCROLL to at least one line,
10999 and at most scroll_conservatively lines. */
e89aaabd 11000 amount_to_scroll
da8b7f4f
KS
11001 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11002 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
5f5c8ee5
GM
11003 else if (scroll_step || temp_scroll_step)
11004 amount_to_scroll = scroll_max;
11005 else
90adcf20 11006 {
46226c1d 11007 aggressive = current_buffer->scroll_up_aggressively;
da8b7f4f 11008 height = WINDOW_BOX_TEXT_HEIGHT (w);
5f5c8ee5 11009 if (NUMBERP (aggressive))
6924d3b7
RS
11010 {
11011 double float_amount = XFLOATINT (aggressive) * height;
11012 amount_to_scroll = float_amount;
11013 if (amount_to_scroll == 0 && float_amount > 0)
11014 amount_to_scroll = 1;
11015 }
5f5c8ee5 11016 }
a2725ab2 11017
5f5c8ee5 11018 if (amount_to_scroll <= 0)
cb617e7c 11019 return SCROLLING_FAILED;
a2725ab2 11020
03b0a4b4
RS
11021 /* If moving by amount_to_scroll leaves STARTP unchanged,
11022 move it down one screen line. */
11023
5f5c8ee5 11024 move_it_vertically (&it, amount_to_scroll);
03b0a4b4
RS
11025 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11026 move_it_by_lines (&it, 1, 1);
5f5c8ee5
GM
11027 startp = it.current.pos;
11028 }
11029 else
11030 {
11031 /* See if point is inside the scroll margin at the top of the
11032 window. */
11033 scroll_margin_pos = startp;
11034 if (this_scroll_margin)
11035 {
11036 start_display (&it, w, startp);
11037 move_it_vertically (&it, this_scroll_margin);
11038 scroll_margin_pos = it.current.pos;
11039 }
11040
11041 if (PT < CHARPOS (scroll_margin_pos))
11042 {
11043 /* Point is in the scroll margin at the top of the window or
11044 above what is displayed in the window. */
11045 int y0;
2311178e 11046
5f5c8ee5
GM
11047 /* Compute the vertical distance from PT to the scroll
11048 margin position. Give up if distance is greater than
11049 scroll_max. */
11050 SET_TEXT_POS (pos, PT, PT_BYTE);
11051 start_display (&it, w, pos);
11052 y0 = it.current_y;
11053 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11054 it.last_visible_y, -1,
11055 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11056 dy = it.current_y - y0;
11057 if (dy > scroll_max)
cb617e7c 11058 return SCROLLING_FAILED;
2311178e 11059
5f5c8ee5
GM
11060 /* Compute new window start. */
11061 start_display (&it, w, startp);
2311178e 11062
5f5c8ee5 11063 if (scroll_conservatively)
0894e696 11064 amount_to_scroll =
da8b7f4f 11065 max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
5f5c8ee5
GM
11066 else if (scroll_step || temp_scroll_step)
11067 amount_to_scroll = scroll_max;
538f13d4 11068 else
5f5c8ee5 11069 {
46226c1d 11070 aggressive = current_buffer->scroll_down_aggressively;
da8b7f4f 11071 height = WINDOW_BOX_TEXT_HEIGHT (w);
5f5c8ee5 11072 if (NUMBERP (aggressive))
6924d3b7
RS
11073 {
11074 double float_amount = XFLOATINT (aggressive) * height;
11075 amount_to_scroll = float_amount;
11076 if (amount_to_scroll == 0 && float_amount > 0)
11077 amount_to_scroll = 1;
11078 }
5f5c8ee5 11079 }
a2725ab2 11080
5f5c8ee5 11081 if (amount_to_scroll <= 0)
cb617e7c 11082 return SCROLLING_FAILED;
2311178e 11083
5f5c8ee5
GM
11084 move_it_vertically (&it, - amount_to_scroll);
11085 startp = it.current.pos;
90adcf20
RS
11086 }
11087 }
a2889657 11088
5f5c8ee5
GM
11089 /* Run window scroll functions. */
11090 startp = run_window_scroll_functions (window, startp);
90adcf20 11091
5f5c8ee5
GM
11092 /* Display the window. Give up if new fonts are loaded, or if point
11093 doesn't appear. */
11094 if (!try_window (window, startp))
cb617e7c 11095 rc = SCROLLING_NEED_LARGER_MATRICES;
5f5c8ee5
GM
11096 else if (w->cursor.vpos < 0)
11097 {
11098 clear_glyph_matrix (w->desired_matrix);
cb617e7c 11099 rc = SCROLLING_FAILED;
5f5c8ee5
GM
11100 }
11101 else
11102 {
11103 /* Maybe forget recorded base line for line number display. */
2311178e 11104 if (!just_this_one_p
5f5c8ee5 11105 || current_buffer->clip_changed
9142dd5b 11106 || BEG_UNCHANGED < CHARPOS (startp))
5f5c8ee5 11107 w->base_line_number = Qnil;
2311178e 11108
ddf6b9a3
RS
11109 /* If cursor ends up on a partially visible line,
11110 treat that as being off the bottom of the screen. */
cb9d4a9f 11111 if (! make_cursor_line_fully_visible (w, extra_scroll_margin_lines <= 1))
ef3c2c73
RS
11112 {
11113 clear_glyph_matrix (w->desired_matrix);
cb9d4a9f 11114 ++extra_scroll_margin_lines;
ef3c2c73
RS
11115 goto too_near_end;
11116 }
ddf6b9a3 11117 rc = SCROLLING_SUCCESS;
5f5c8ee5
GM
11118 }
11119
11120 return rc;
a2889657
JB
11121}
11122
5f5c8ee5
GM
11123
11124/* Compute a suitable window start for window W if display of W starts
11125 on a continuation line. Value is non-zero if a new window start
11126 was computed.
11127
11128 The new window start will be computed, based on W's width, starting
11129 from the start of the continued line. It is the start of the
11130 screen line with the minimum distance from the old start W->start. */
11131
11132static int
11133compute_window_start_on_continuation_line (w)
11134 struct window *w;
1f1ff51d 11135{
5f5c8ee5
GM
11136 struct text_pos pos, start_pos;
11137 int window_start_changed_p = 0;
1f1ff51d 11138
5f5c8ee5 11139 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
1f1ff51d 11140
5f5c8ee5 11141 /* If window start is on a continuation line... Window start may be
2311178e 11142 < BEGV in case there's invisible text at the start of the
5f5c8ee5
GM
11143 buffer (M-x rmail, for example). */
11144 if (CHARPOS (start_pos) > BEGV
11145 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
1f1ff51d 11146 {
5f5c8ee5
GM
11147 struct it it;
11148 struct glyph_row *row;
f3751a65
GM
11149
11150 /* Handle the case that the window start is out of range. */
11151 if (CHARPOS (start_pos) < BEGV)
11152 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11153 else if (CHARPOS (start_pos) > ZV)
11154 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
2311178e 11155
5f5c8ee5
GM
11156 /* Find the start of the continued line. This should be fast
11157 because scan_buffer is fast (newline cache). */
045dee35 11158 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
5f5c8ee5
GM
11159 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11160 row, DEFAULT_FACE_ID);
11161 reseat_at_previous_visible_line_start (&it);
11162
11163 /* If the line start is "too far" away from the window start,
11164 say it takes too much time to compute a new window start. */
11165 if (CHARPOS (start_pos) - IT_CHARPOS (it)
da8b7f4f 11166 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
5f5c8ee5
GM
11167 {
11168 int min_distance, distance;
2311178e 11169
5f5c8ee5
GM
11170 /* Move forward by display lines to find the new window
11171 start. If window width was enlarged, the new start can
11172 be expected to be > the old start. If window width was
11173 decreased, the new window start will be < the old start.
11174 So, we're looking for the display line start with the
11175 minimum distance from the old window start. */
11176 pos = it.current.pos;
11177 min_distance = INFINITY;
11178 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11179 distance < min_distance)
11180 {
11181 min_distance = distance;
11182 pos = it.current.pos;
11183 move_it_by_lines (&it, 1, 0);
11184 }
2311178e 11185
5f5c8ee5
GM
11186 /* Set the window start there. */
11187 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11188 window_start_changed_p = 1;
11189 }
1f1ff51d 11190 }
2311178e 11191
5f5c8ee5 11192 return window_start_changed_p;
1f1ff51d
KH
11193}
11194
5f5c8ee5 11195
1dd5768b 11196/* Try cursor movement in case text has not changed in window WINDOW,
47589c8c
GM
11197 with window start STARTP. Value is
11198
cb617e7c 11199 CURSOR_MOVEMENT_SUCCESS if successful
2311178e 11200
cb617e7c
GM
11201 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11202
11203 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11204 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11205 we want to scroll as if scroll-step were set to 1. See the code.
11206
11207 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11208 which case we have to abort this redisplay, and adjust matrices
11209 first. */
11210
2311178e 11211enum
cb617e7c
GM
11212{
11213 CURSOR_MOVEMENT_SUCCESS,
11214 CURSOR_MOVEMENT_CANNOT_BE_USED,
11215 CURSOR_MOVEMENT_MUST_SCROLL,
11216 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11217};
47589c8c
GM
11218
11219static int
11220try_cursor_movement (window, startp, scroll_step)
11221 Lisp_Object window;
11222 struct text_pos startp;
11223 int *scroll_step;
11224{
11225 struct window *w = XWINDOW (window);
11226 struct frame *f = XFRAME (w->frame);
cb617e7c 11227 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
2311178e 11228
69d1f7c9 11229#if GLYPH_DEBUG
76cb5e06
GM
11230 if (inhibit_try_cursor_movement)
11231 return rc;
11232#endif
11233
47589c8c
GM
11234 /* Handle case where text has not changed, only point, and it has
11235 not moved off the frame. */
11236 if (/* Point may be in this window. */
11237 PT >= CHARPOS (startp)
47589c8c
GM
11238 /* Selective display hasn't changed. */
11239 && !current_buffer->clip_changed
4db87380
GM
11240 /* Function force-mode-line-update is used to force a thorough
11241 redisplay. It sets either windows_or_buffers_changed or
11242 update_mode_lines. So don't take a shortcut here for these
11243 cases. */
11244 && !update_mode_lines
11245 && !windows_or_buffers_changed
5fb96e96 11246 && !cursor_type_changed
2311178e 11247 /* Can't use this case if highlighting a region. When a
47589c8c
GM
11248 region exists, cursor movement has to do more than just
11249 set the cursor. */
11250 && !(!NILP (Vtransient_mark_mode)
11251 && !NILP (current_buffer->mark_active))
11252 && NILP (w->region_showing)
11253 && NILP (Vshow_trailing_whitespace)
11254 /* Right after splitting windows, last_point may be nil. */
11255 && INTEGERP (w->last_point)
11256 /* This code is not used for mini-buffer for the sake of the case
11257 of redisplaying to replace an echo area message; since in
11258 that case the mini-buffer contents per se are usually
11259 unchanged. This code is of no real use in the mini-buffer
11260 since the handling of this_line_start_pos, etc., in redisplay
11261 handles the same cases. */
11262 && !EQ (window, minibuf_window)
11263 /* When splitting windows or for new windows, it happens that
11264 redisplay is called with a nil window_end_vpos or one being
11265 larger than the window. This should really be fixed in
11266 window.c. I don't have this on my list, now, so we do
11267 approximately the same as the old redisplay code. --gerd. */
11268 && INTEGERP (w->window_end_vpos)
11269 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11270 && (FRAME_WINDOW_P (f)
351b5434 11271 || !overlay_arrow_in_current_buffer_p ()))
47589c8c 11272 {
38793d6a 11273 int this_scroll_margin, top_scroll_margin;
8ee5b6a3 11274 struct glyph_row *row = NULL;
47589c8c
GM
11275
11276#if GLYPH_DEBUG
11277 debug_method_add (w, "cursor movement");
11278#endif
11279
11280 /* Scroll if point within this distance from the top or bottom
11281 of the window. This is a pixel value. */
11282 this_scroll_margin = max (0, scroll_margin);
da8b7f4f
KS
11283 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11284 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
47589c8c 11285
38793d6a
KS
11286 top_scroll_margin = this_scroll_margin;
11287 if (WINDOW_WANTS_HEADER_LINE_P (w))
11288 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
11289
47589c8c
GM
11290 /* Start with the row the cursor was displayed during the last
11291 not paused redisplay. Give up if that row is not valid. */
bd9d0f3f
GM
11292 if (w->last_cursor.vpos < 0
11293 || w->last_cursor.vpos >= w->current_matrix->nrows)
cb617e7c 11294 rc = CURSOR_MOVEMENT_MUST_SCROLL;
47589c8c
GM
11295 else
11296 {
11297 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11298 if (row->mode_line_p)
11299 ++row;
11300 if (!row->enabled_p)
cb617e7c 11301 rc = CURSOR_MOVEMENT_MUST_SCROLL;
47589c8c
GM
11302 }
11303
cb617e7c 11304 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
47589c8c
GM
11305 {
11306 int scroll_p = 0;
68c5d1db 11307 int last_y = window_text_bottom_y (w) - this_scroll_margin;
2311178e 11308
47589c8c
GM
11309 if (PT > XFASTINT (w->last_point))
11310 {
11311 /* Point has moved forward. */
47589c8c
GM
11312 while (MATRIX_ROW_END_CHARPOS (row) < PT
11313 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11314 {
11315 xassert (row->enabled_p);
11316 ++row;
11317 }
11318
11319 /* The end position of a row equals the start position
11320 of the next row. If PT is there, we would rather
cafafe0b
GM
11321 display it in the next line. */
11322 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11323 && MATRIX_ROW_END_CHARPOS (row) == PT
11324 && !cursor_row_p (w, row))
11325 ++row;
47589c8c
GM
11326
11327 /* If within the scroll margin, scroll. Note that
11328 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11329 the next line would be drawn, and that
11330 this_scroll_margin can be zero. */
11331 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11332 || PT > MATRIX_ROW_END_CHARPOS (row)
11333 /* Line is completely visible last line in window
11334 and PT is to be set in the next line. */
11335 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11336 && PT == MATRIX_ROW_END_CHARPOS (row)
11337 && !row->ends_at_zv_p
11338 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11339 scroll_p = 1;
11340 }
11341 else if (PT < XFASTINT (w->last_point))
11342 {
11343 /* Cursor has to be moved backward. Note that PT >=
11344 CHARPOS (startp) because of the outer
11345 if-statement. */
11346 while (!row->mode_line_p
11347 && (MATRIX_ROW_START_CHARPOS (row) > PT
11348 || (MATRIX_ROW_START_CHARPOS (row) == PT
11349 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
38793d6a 11350 && (row->y > top_scroll_margin
47589c8c
GM
11351 || CHARPOS (startp) == BEGV))
11352 {
11353 xassert (row->enabled_p);
11354 --row;
11355 }
11356
11357 /* Consider the following case: Window starts at BEGV,
11358 there is invisible, intangible text at BEGV, so that
11359 display starts at some point START > BEGV. It can
11360 happen that we are called with PT somewhere between
11361 BEGV and START. Try to handle that case. */
11362 if (row < w->current_matrix->rows
11363 || row->mode_line_p)
11364 {
11365 row = w->current_matrix->rows;
11366 if (row->mode_line_p)
11367 ++row;
11368 }
11369
11370 /* Due to newlines in overlay strings, we may have to
11371 skip forward over overlay strings. */
68c5d1db
GM
11372 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11373 && MATRIX_ROW_END_CHARPOS (row) == PT
11374 && !cursor_row_p (w, row))
47589c8c 11375 ++row;
2311178e 11376
47589c8c 11377 /* If within the scroll margin, scroll. */
38793d6a 11378 if (row->y < top_scroll_margin
47589c8c
GM
11379 && CHARPOS (startp) != BEGV)
11380 scroll_p = 1;
11381 }
11382
11383 if (PT < MATRIX_ROW_START_CHARPOS (row)
11384 || PT > MATRIX_ROW_END_CHARPOS (row))
11385 {
11386 /* if PT is not in the glyph row, give up. */
cb617e7c 11387 rc = CURSOR_MOVEMENT_MUST_SCROLL;
47589c8c 11388 }
440fc135 11389 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
47589c8c 11390 {
8de4aaf8
GM
11391 if (PT == MATRIX_ROW_END_CHARPOS (row)
11392 && !row->ends_at_zv_p
11393 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
cb617e7c 11394 rc = CURSOR_MOVEMENT_MUST_SCROLL;
3f7e3031 11395 else if (row->height > window_box_height (w))
440fc135 11396 {
8de4aaf8
GM
11397 /* If we end up in a partially visible line, let's
11398 make it fully visible, except when it's taller
11399 than the window, in which case we can't do much
11400 about it. */
440fc135 11401 *scroll_step = 1;
cb617e7c 11402 rc = CURSOR_MOVEMENT_MUST_SCROLL;
440fc135
GM
11403 }
11404 else
11405 {
11406 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
cb9d4a9f 11407 if (!make_cursor_line_fully_visible (w, 0))
ddf6b9a3 11408 rc = CURSOR_MOVEMENT_MUST_SCROLL;
cb617e7c
GM
11409 else
11410 rc = CURSOR_MOVEMENT_SUCCESS;
440fc135 11411 }
47589c8c
GM
11412 }
11413 else if (scroll_p)
cb617e7c 11414 rc = CURSOR_MOVEMENT_MUST_SCROLL;
47589c8c
GM
11415 else
11416 {
11417 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
cb617e7c 11418 rc = CURSOR_MOVEMENT_SUCCESS;
47589c8c
GM
11419 }
11420 }
11421 }
11422
11423 return rc;
11424}
11425
9a08d928
SM
11426void
11427set_vertical_scroll_bar (w)
11428 struct window *w;
11429{
11430 int start, end, whole;
11431
11432 /* Calculate the start and end positions for the current window.
11433 At some point, it would be nice to choose between scrollbars
11434 which reflect the whole buffer size, with special markers
11435 indicating narrowing, and scrollbars which reflect only the
11436 visible region.
be21b925 11437
9a08d928
SM
11438 Note that mini-buffers sometimes aren't displaying any text. */
11439 if (!MINI_WINDOW_P (w)
11440 || (w == XWINDOW (minibuf_window)
11441 && NILP (echo_area_buffer[0])))
11442 {
11443 struct buffer *buf = XBUFFER (w->buffer);
11444 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11445 start = marker_position (w->start) - BUF_BEGV (buf);
11446 /* I don't think this is guaranteed to be right. For the
11447 moment, we'll pretend it is. */
11448 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
be21b925 11449
9a08d928
SM
11450 if (end < start)
11451 end = start;
11452 if (whole < (end - start))
11453 whole = end - start;
11454 }
11455 else
11456 start = end = whole = 0;
11457
11458 /* Indicate what this scroll bar ought to be displaying now. */
11459 set_vertical_scroll_bar_hook (w, end - start, whole, start);
11460}
47589c8c 11461
88e6b646 11462
5f5c8ee5 11463/* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
b60c9653
RS
11464 selected_window is redisplayed.
11465
11466 We can return without actually redisplaying the window if
11467 fonts_changed_p is nonzero. In that case, redisplay_internal will
11468 retry. */
90adcf20 11469
a2889657 11470static void
5f5c8ee5 11471redisplay_window (window, just_this_one_p)
a2889657 11472 Lisp_Object window;
5f5c8ee5 11473 int just_this_one_p;
a2889657 11474{
5f5c8ee5
GM
11475 struct window *w = XWINDOW (window);
11476 struct frame *f = XFRAME (w->frame);
11477 struct buffer *buffer = XBUFFER (w->buffer);
a2889657 11478 struct buffer *old = current_buffer;
5f5c8ee5 11479 struct text_pos lpoint, opoint, startp;
e481f960 11480 int update_mode_line;
5f5c8ee5
GM
11481 int tem;
11482 struct it it;
11483 /* Record it now because it's overwritten. */
11484 int current_matrix_up_to_date_p = 0;
88e6b646 11485 int used_current_matrix_p = 0;
c62c1bb5
RS
11486 /* This is less strict than current_matrix_up_to_date_p.
11487 It indictes that the buffer contents and narrowing are unchanged. */
11488 int buffer_unchanged_p = 0;
5f5c8ee5 11489 int temp_scroll_step = 0;
331379bf 11490 int count = SPECPDL_INDEX ();
47589c8c 11491 int rc;
ddf6b9a3 11492 int centering_position;
03b0a4b4 11493 int last_line_misfit = 0;
a2889657 11494
5f5c8ee5
GM
11495 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11496 opoint = lpoint;
a2889657 11497
5f5c8ee5
GM
11498 /* W must be a leaf window here. */
11499 xassert (!NILP (w->buffer));
11500#if GLYPH_DEBUG
11501 *w->desired_matrix->method = 0;
11502#endif
2e54982e
RS
11503
11504 specbind (Qinhibit_point_motion_hooks, Qt);
9142dd5b
GM
11505
11506 reconsider_clip_changes (w, buffer);
2311178e
TTN
11507
11508 /* Has the mode line to be updated? */
5f5c8ee5
GM
11509 update_mode_line = (!NILP (w->update_mode_line)
11510 || update_mode_lines
c62c1bb5
RS
11511 || buffer->clip_changed
11512 || buffer->prevent_redisplay_optimizations_p);
8de2d90b
JB
11513
11514 if (MINI_WINDOW_P (w))
11515 {
5f5c8ee5 11516 if (w == XWINDOW (echo_area_window)
c6e89d6c 11517 && !NILP (echo_area_buffer[0]))
5f5c8ee5
GM
11518 {
11519 if (update_mode_line)
11520 /* We may have to update a tty frame's menu bar or a
e037b9ec 11521 tool-bar. Example `M-x C-h C-h C-g'. */
5f5c8ee5
GM
11522 goto finish_menu_bars;
11523 else
11524 /* We've already displayed the echo area glyphs in this window. */
11525 goto finish_scroll_bars;
11526 }
b2b5455d
RS
11527 else if ((w != XWINDOW (minibuf_window)
11528 || minibuf_level == 0)
c0bcce6f
JPW
11529 /* When buffer is nonempty, redisplay window normally. */
11530 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
3354fdcf
RS
11531 /* Quail displays non-mini buffers in minibuffer window.
11532 In that case, redisplay the window normally. */
b2b5455d 11533 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
8de2d90b 11534 {
3354fdcf
RS
11535 /* W is a mini-buffer window, but it's not active, so clear
11536 it. */
5f5c8ee5
GM
11537 int yb = window_text_bottom_y (w);
11538 struct glyph_row *row;
11539 int y;
11540
11541 for (y = 0, row = w->desired_matrix->rows;
11542 y < yb;
11543 y += row->height, ++row)
11544 blank_row (w, row, y);
88f22aff 11545 goto finish_scroll_bars;
8de2d90b 11546 }
c095a1dd
GM
11547
11548 clear_glyph_matrix (w->desired_matrix);
8de2d90b 11549 }
a2889657 11550
5f5c8ee5
GM
11551 /* Otherwise set up data on this window; select its buffer and point
11552 value. */
6a93695f
GM
11553 /* Really select the buffer, for the sake of buffer-local
11554 variables. */
11555 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5
GM
11556 SET_TEXT_POS (opoint, PT, PT_BYTE);
11557
11558 current_matrix_up_to_date_p
11559 = (!NILP (w->window_end_valid)
11560 && !current_buffer->clip_changed
c62c1bb5 11561 && !current_buffer->prevent_redisplay_optimizations_p
5f5c8ee5
GM
11562 && XFASTINT (w->last_modified) >= MODIFF
11563 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
e481f960 11564
c62c1bb5
RS
11565 buffer_unchanged_p
11566 = (!NILP (w->window_end_valid)
11567 && !current_buffer->clip_changed
3f1258d0 11568 && XFASTINT (w->last_modified) >= MODIFF
c62c1bb5
RS
11569 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11570
5f5c8ee5
GM
11571 /* When windows_or_buffers_changed is non-zero, we can't rely on
11572 the window end being valid, so set it to nil there. */
11573 if (windows_or_buffers_changed)
11574 {
11575 /* If window starts on a continuation line, maybe adjust the
11576 window start in case the window's width changed. */
11577 if (XMARKER (w->start)->buffer == current_buffer)
11578 compute_window_start_on_continuation_line (w);
2311178e 11579
5f5c8ee5
GM
11580 w->window_end_valid = Qnil;
11581 }
12adba34 11582
5f5c8ee5
GM
11583 /* Some sanity checks. */
11584 CHECK_WINDOW_END (w);
11585 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12adba34 11586 abort ();
5f5c8ee5 11587 if (BYTEPOS (opoint) < CHARPOS (opoint))
12adba34 11588 abort ();
a2889657 11589
28995e67
RS
11590 /* If %c is in mode line, update it if needed. */
11591 if (!NILP (w->column_number_displayed)
11592 /* This alternative quickly identifies a common case
11593 where no change is needed. */
11594 && !(PT == XFASTINT (w->last_point)
8850a573
RS
11595 && XFASTINT (w->last_modified) >= MODIFF
11596 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
2311178e
TTN
11597 && (XFASTINT (w->column_number_displayed)
11598 != (int) current_column ())) /* iftc */
11599 update_mode_line = 1;
28995e67 11600
5f5c8ee5
GM
11601 /* Count number of windows showing the selected buffer. An indirect
11602 buffer counts as its base buffer. */
11603 if (!just_this_one_p)
42640f83
RS
11604 {
11605 struct buffer *current_base, *window_base;
11606 current_base = current_buffer;
11607 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11608 if (current_base->base_buffer)
11609 current_base = current_base->base_buffer;
11610 if (window_base->base_buffer)
11611 window_base = window_base->base_buffer;
11612 if (current_base == window_base)
11613 buffer_shared++;
11614 }
a2889657 11615
5f5c8ee5
GM
11616 /* Point refers normally to the selected window. For any other
11617 window, set up appropriate value. */
a2889657
JB
11618 if (!EQ (window, selected_window))
11619 {
12adba34
RS
11620 int new_pt = XMARKER (w->pointm)->charpos;
11621 int new_pt_byte = marker_byte_position (w->pointm);
f67a0f51 11622 if (new_pt < BEGV)
a2889657 11623 {
f67a0f51 11624 new_pt = BEGV;
12adba34
RS
11625 new_pt_byte = BEGV_BYTE;
11626 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
a2889657 11627 }
f67a0f51 11628 else if (new_pt > (ZV - 1))
a2889657 11629 {
f67a0f51 11630 new_pt = ZV;
12adba34
RS
11631 new_pt_byte = ZV_BYTE;
11632 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
a2889657 11633 }
2311178e 11634
f67a0f51 11635 /* We don't use SET_PT so that the point-motion hooks don't run. */
12adba34 11636 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
a2889657
JB
11637 }
11638
f4faa47c 11639 /* If any of the character widths specified in the display table
5f5c8ee5
GM
11640 have changed, invalidate the width run cache. It's true that
11641 this may be a bit late to catch such changes, but the rest of
f4faa47c
JB
11642 redisplay goes (non-fatally) haywire when the display table is
11643 changed, so why should we worry about doing any better? */
11644 if (current_buffer->width_run_cache)
11645 {
f908610f 11646 struct Lisp_Char_Table *disptab = buffer_display_table ();
f4faa47c
JB
11647
11648 if (! disptab_matches_widthtab (disptab,
11649 XVECTOR (current_buffer->width_table)))
11650 {
11651 invalidate_region_cache (current_buffer,
11652 current_buffer->width_run_cache,
11653 BEG, Z);
11654 recompute_width_table (current_buffer, disptab);
11655 }
11656 }
11657
a2889657 11658 /* If window-start is screwed up, choose a new one. */
a2889657
JB
11659 if (XMARKER (w->start)->buffer != current_buffer)
11660 goto recenter;
11661
5f5c8ee5 11662 SET_TEXT_POS_FROM_MARKER (startp, w->start);
a2889657 11663
cf0df6ab
RS
11664 /* If someone specified a new starting point but did not insist,
11665 check whether it can be used. */
cfad01b4
GM
11666 if (!NILP (w->optional_new_start)
11667 && CHARPOS (startp) >= BEGV
11668 && CHARPOS (startp) <= ZV)
cf0df6ab
RS
11669 {
11670 w->optional_new_start = Qnil;
5f5c8ee5
GM
11671 start_display (&it, w, startp);
11672 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11673 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11674 if (IT_CHARPOS (it) == PT)
cf0df6ab 11675 w->force_start = Qt;
29b3ea63
KS
11676 /* IT may overshoot PT if text at PT is invisible. */
11677 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
11678 w->force_start = Qt;
11679
11680
cf0df6ab
RS
11681 }
11682
8de2d90b 11683 /* Handle case where place to start displaying has been specified,
aa6d10fa 11684 unless the specified location is outside the accessible range. */
9472f927
GM
11685 if (!NILP (w->force_start)
11686 || w->frozen_window_start_p)
a2889657 11687 {
b60c9653
RS
11688 /* We set this later on if we have to adjust point. */
11689 int new_vpos = -1;
11690
e63574d7 11691 w->force_start = Qnil;
5f5c8ee5 11692 w->vscroll = 0;
b5174a51 11693 w->window_end_valid = Qnil;
5f5c8ee5
GM
11694
11695 /* Forget any recorded base line for line number display. */
c62c1bb5 11696 if (!buffer_unchanged_p)
5f5c8ee5
GM
11697 w->base_line_number = Qnil;
11698
75c43375
RS
11699 /* Redisplay the mode line. Select the buffer properly for that.
11700 Also, run the hook window-scroll-functions
11701 because we have scrolled. */
e63574d7
RS
11702 /* Note, we do this after clearing force_start because
11703 if there's an error, it is better to forget about force_start
11704 than to get into an infinite loop calling the hook functions
11705 and having them get more errors. */
75c43375
RS
11706 if (!update_mode_line
11707 || ! NILP (Vwindow_scroll_functions))
e481f960 11708 {
e481f960
RS
11709 update_mode_line = 1;
11710 w->update_mode_line = Qt;
5f5c8ee5 11711 startp = run_window_scroll_functions (window, startp);
e481f960 11712 }
2311178e 11713
ac90c44f
GM
11714 w->last_modified = make_number (0);
11715 w->last_overlay_modified = make_number (0);
5f5c8ee5
GM
11716 if (CHARPOS (startp) < BEGV)
11717 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
11718 else if (CHARPOS (startp) > ZV)
11719 SET_TEXT_POS (startp, ZV, ZV_BYTE);
2311178e
TTN
11720
11721 /* Redisplay, then check if cursor has been set during the
5f5c8ee5
GM
11722 redisplay. Give up if new fonts were loaded. */
11723 if (!try_window (window, startp))
11724 {
11725 w->force_start = Qt;
11726 clear_glyph_matrix (w->desired_matrix);
b60c9653 11727 goto need_larger_matrices;
5f5c8ee5
GM
11728 }
11729
9472f927 11730 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
5f5c8ee5 11731 {
b28cb6ed
GM
11732 /* If point does not appear, try to move point so it does
11733 appear. The desired matrix has been built above, so we
11734 can use it here. */
b60c9653
RS
11735 new_vpos = window_box_height (w) / 2;
11736 }
11737
cb9d4a9f 11738 if (!make_cursor_line_fully_visible (w, 0))
b60c9653
RS
11739 {
11740 /* Point does appear, but on a line partly visible at end of window.
11741 Move it back to a fully-visible line. */
11742 new_vpos = window_box_height (w);
11743 }
11744
11745 /* If we need to move point for either of the above reasons,
11746 now actually do it. */
11747 if (new_vpos >= 0)
11748 {
b28cb6ed
GM
11749 struct glyph_row *row;
11750
b28cb6ed 11751 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
b60c9653 11752 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
5f5c8ee5
GM
11753 ++row;
11754
11755 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
11756 MATRIX_ROW_START_BYTEPOS (row));
11757
90adcf20 11758 if (w != XWINDOW (selected_window))
12adba34 11759 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5f5c8ee5
GM
11760 else if (current_buffer == old)
11761 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11762
11763 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
2311178e 11764
5f5c8ee5
GM
11765 /* If we are highlighting the region, then we just changed
11766 the region, so redisplay to show it. */
df0b5ea1
RS
11767 if (!NILP (Vtransient_mark_mode)
11768 && !NILP (current_buffer->mark_active))
6f27fa9b 11769 {
5f5c8ee5
GM
11770 clear_glyph_matrix (w->desired_matrix);
11771 if (!try_window (window, startp))
cb617e7c 11772 goto need_larger_matrices;
6f27fa9b 11773 }
a2889657 11774 }
5f5c8ee5 11775
5f5c8ee5
GM
11776#if GLYPH_DEBUG
11777 debug_method_add (w, "forced window start");
11778#endif
a2889657
JB
11779 goto done;
11780 }
11781
5f5c8ee5 11782 /* Handle case where text has not changed, only point, and it has
3f029ea0
RS
11783 not moved off the frame, and we are not retrying after hscroll.
11784 (current_matrix_up_to_date_p is nonzero when retrying.) */
11785 if (current_matrix_up_to_date_p
47589c8c 11786 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
cb617e7c 11787 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
a2889657 11788 {
cb617e7c
GM
11789 switch (rc)
11790 {
11791 case CURSOR_MOVEMENT_SUCCESS:
88e6b646 11792 used_current_matrix_p = 1;
cb617e7c 11793 goto done;
2311178e 11794
b60c9653 11795#if 0 /* try_cursor_movement never returns this value. */
cb617e7c
GM
11796 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
11797 goto need_larger_matrices;
b60c9653 11798#endif
2311178e 11799
cb617e7c
GM
11800 case CURSOR_MOVEMENT_MUST_SCROLL:
11801 goto try_to_scroll;
2311178e 11802
cb617e7c
GM
11803 default:
11804 abort ();
11805 }
a2889657
JB
11806 }
11807 /* If current starting point was originally the beginning of a line
11808 but no longer is, find a new starting point. */
265a9e55 11809 else if (!NILP (w->start_at_line_beg)
5f5c8ee5
GM
11810 && !(CHARPOS (startp) <= BEGV
11811 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
a2889657 11812 {
5f5c8ee5
GM
11813#if GLYPH_DEBUG
11814 debug_method_add (w, "recenter 1");
11815#endif
a2889657
JB
11816 goto recenter;
11817 }
2311178e 11818
27d16f05
GM
11819 /* Try scrolling with try_window_id. Value is > 0 if update has
11820 been done, it is -1 if we know that the same window start will
11821 not work. It is 0 if unsuccessful for some other reason. */
11822 else if ((tem = try_window_id (w)) != 0)
a2889657 11823 {
5f5c8ee5 11824#if GLYPH_DEBUG
ef121659 11825 debug_method_add (w, "try_window_id %d", tem);
5f5c8ee5
GM
11826#endif
11827
11828 if (fonts_changed_p)
cb617e7c 11829 goto need_larger_matrices;
a2889657
JB
11830 if (tem > 0)
11831 goto done;
ef121659 11832
5f5c8ee5
GM
11833 /* Otherwise try_window_id has returned -1 which means that we
11834 don't want the alternative below this comment to execute. */
a2889657 11835 }
5f5c8ee5
GM
11836 else if (CHARPOS (startp) >= BEGV
11837 && CHARPOS (startp) <= ZV
11838 && PT >= CHARPOS (startp)
11839 && (CHARPOS (startp) < ZV
e9874cee 11840 /* Avoid starting at end of buffer. */
5f5c8ee5 11841 || CHARPOS (startp) == BEGV
8850a573
RS
11842 || (XFASTINT (w->last_modified) >= MODIFF
11843 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
a2889657 11844 {
5f5c8ee5
GM
11845#if GLYPH_DEBUG
11846 debug_method_add (w, "same window start");
11847#endif
2311178e 11848
5f5c8ee5
GM
11849 /* Try to redisplay starting at same place as before.
11850 If point has not moved off frame, accept the results. */
11851 if (!current_matrix_up_to_date_p
11852 /* Don't use try_window_reusing_current_matrix in this case
15e26c76
GM
11853 because a window scroll function can have changed the
11854 buffer. */
5f5c8ee5
GM
11855 || !NILP (Vwindow_scroll_functions)
11856 || MINI_WINDOW_P (w)
88e6b646
KS
11857 || !(used_current_matrix_p =
11858 try_window_reusing_current_matrix (w)))
5f5c8ee5
GM
11859 {
11860 IF_DEBUG (debug_method_add (w, "1"));
11861 try_window (window, startp);
11862 }
11863
11864 if (fonts_changed_p)
cb617e7c 11865 goto need_larger_matrices;
2311178e 11866
5f5c8ee5 11867 if (w->cursor.vpos >= 0)
aa6d10fa 11868 {
2311178e 11869 if (!just_this_one_p
5f5c8ee5 11870 || current_buffer->clip_changed
9142dd5b 11871 || BEG_UNCHANGED < CHARPOS (startp))
aa6d10fa
RS
11872 /* Forget any recorded base line for line number display. */
11873 w->base_line_number = Qnil;
2311178e 11874
cb9d4a9f 11875 if (!make_cursor_line_fully_visible (w, 1))
03b0a4b4
RS
11876 {
11877 clear_glyph_matrix (w->desired_matrix);
11878 last_line_misfit = 1;
11879 }
ddf6b9a3 11880 /* Drop through and scroll. */
ef3c2c73
RS
11881 else
11882 goto done;
aa6d10fa 11883 }
a2889657 11884 else
5f5c8ee5 11885 clear_glyph_matrix (w->desired_matrix);
a2889657
JB
11886 }
11887
5f5c8ee5
GM
11888 try_to_scroll:
11889
ac90c44f
GM
11890 w->last_modified = make_number (0);
11891 w->last_overlay_modified = make_number (0);
5f5c8ee5 11892
e481f960
RS
11893 /* Redisplay the mode line. Select the buffer properly for that. */
11894 if (!update_mode_line)
11895 {
e481f960
RS
11896 update_mode_line = 1;
11897 w->update_mode_line = Qt;
11898 }
a2889657 11899
5f5c8ee5
GM
11900 /* Try to scroll by specified few lines. */
11901 if ((scroll_conservatively
11902 || scroll_step
11903 || temp_scroll_step
11904 || NUMBERP (current_buffer->scroll_up_aggressively)
11905 || NUMBERP (current_buffer->scroll_down_aggressively))
09cacf9c 11906 && !current_buffer->clip_changed
2311178e 11907 && CHARPOS (startp) >= BEGV
5f5c8ee5 11908 && CHARPOS (startp) <= ZV)
0789adb2 11909 {
5f5c8ee5
GM
11910 /* The function returns -1 if new fonts were loaded, 1 if
11911 successful, 0 if not successful. */
11912 int rc = try_scrolling (window, just_this_one_p,
11913 scroll_conservatively,
11914 scroll_step,
03b0a4b4 11915 temp_scroll_step, last_line_misfit);
cb617e7c
GM
11916 switch (rc)
11917 {
11918 case SCROLLING_SUCCESS:
11919 goto done;
2311178e 11920
cb617e7c
GM
11921 case SCROLLING_NEED_LARGER_MATRICES:
11922 goto need_larger_matrices;
2311178e 11923
cb617e7c
GM
11924 case SCROLLING_FAILED:
11925 break;
2311178e 11926
cb617e7c
GM
11927 default:
11928 abort ();
11929 }
5f5c8ee5 11930 }
f9c8af06 11931
5f5c8ee5 11932 /* Finally, just choose place to start which centers point */
5936754e 11933
5f5c8ee5 11934 recenter:
ddf6b9a3
RS
11935 centering_position = window_box_height (w) / 2;
11936
11937 point_at_top:
11938 /* Jump here with centering_position already set to 0. */
44173109 11939
5f5c8ee5
GM
11940#if GLYPH_DEBUG
11941 debug_method_add (w, "recenter");
11942#endif
0789adb2 11943
5f5c8ee5 11944 /* w->vscroll = 0; */
0789adb2 11945
5f5c8ee5 11946 /* Forget any previously recorded base line for line number display. */
c62c1bb5 11947 if (!buffer_unchanged_p)
5f5c8ee5
GM
11948 w->base_line_number = Qnil;
11949
11950 /* Move backward half the height of the window. */
11951 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11952 it.current_y = it.last_visible_y;
ddf6b9a3 11953 move_it_vertically_backward (&it, centering_position);
5f5c8ee5
GM
11954 xassert (IT_CHARPOS (it) >= BEGV);
11955
11956 /* The function move_it_vertically_backward may move over more
11957 than the specified y-distance. If it->w is small, e.g. a
11958 mini-buffer window, we may end up in front of the window's
11959 display area. Start displaying at the start of the line
11960 containing PT in this case. */
11961 if (it.current_y <= 0)
11962 {
11963 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11964 move_it_vertically (&it, 0);
11965 xassert (IT_CHARPOS (it) <= PT);
11966 it.current_y = 0;
0789adb2
RS
11967 }
11968
5f5c8ee5 11969 it.current_x = it.hpos = 0;
2311178e 11970
5f5c8ee5
GM
11971 /* Set startp here explicitly in case that helps avoid an infinite loop
11972 in case the window-scroll-functions functions get errors. */
11973 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
11974
11975 /* Run scroll hooks. */
11976 startp = run_window_scroll_functions (window, it.current.pos);
11977
11978 /* Redisplay the window. */
11979 if (!current_matrix_up_to_date_p
11980 || windows_or_buffers_changed
5fb96e96 11981 || cursor_type_changed
5f5c8ee5
GM
11982 /* Don't use try_window_reusing_current_matrix in this case
11983 because it can have changed the buffer. */
11984 || !NILP (Vwindow_scroll_functions)
11985 || !just_this_one_p
11986 || MINI_WINDOW_P (w)
88e6b646
KS
11987 || !(used_current_matrix_p =
11988 try_window_reusing_current_matrix (w)))
5f5c8ee5
GM
11989 try_window (window, startp);
11990
11991 /* If new fonts have been loaded (due to fontsets), give up. We
11992 have to start a new redisplay since we need to re-adjust glyph
11993 matrices. */
11994 if (fonts_changed_p)
cb617e7c 11995 goto need_larger_matrices;
5f5c8ee5
GM
11996
11997 /* If cursor did not appear assume that the middle of the window is
11998 in the first line of the window. Do it again with the next line.
11999 (Imagine a window of height 100, displaying two lines of height
12000 60. Moving back 50 from it->last_visible_y will end in the first
12001 line.) */
12002 if (w->cursor.vpos < 0)
a2889657 12003 {
5f5c8ee5
GM
12004 if (!NILP (w->window_end_valid)
12005 && PT >= Z - XFASTINT (w->window_end_pos))
a2889657 12006 {
5f5c8ee5
GM
12007 clear_glyph_matrix (w->desired_matrix);
12008 move_it_by_lines (&it, 1, 0);
12009 try_window (window, it.current.pos);
a2889657 12010 }
5f5c8ee5 12011 else if (PT < IT_CHARPOS (it))
a2889657 12012 {
5f5c8ee5
GM
12013 clear_glyph_matrix (w->desired_matrix);
12014 move_it_by_lines (&it, -1, 0);
12015 try_window (window, it.current.pos);
12016 }
12017 else
12018 {
12019 /* Not much we can do about it. */
a2889657 12020 }
a2889657 12021 }
010494d0 12022
5f5c8ee5
GM
12023 /* Consider the following case: Window starts at BEGV, there is
12024 invisible, intangible text at BEGV, so that display starts at
12025 some point START > BEGV. It can happen that we are called with
12026 PT somewhere between BEGV and START. Try to handle that case. */
12027 if (w->cursor.vpos < 0)
835766b6 12028 {
5f5c8ee5
GM
12029 struct glyph_row *row = w->current_matrix->rows;
12030 if (row->mode_line_p)
12031 ++row;
12032 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
835766b6 12033 }
2311178e 12034
cb9d4a9f 12035 if (!make_cursor_line_fully_visible (w, centering_position > 0))
ddf6b9a3 12036 {
932357b4
KS
12037 /* If vscroll is enabled, disable it and try again. */
12038 if (w->vscroll)
12039 {
12040 w->vscroll = 0;
12041 clear_glyph_matrix (w->desired_matrix);
12042 goto recenter;
12043 }
2a6d0874 12044
ddf6b9a3
RS
12045 /* If centering point failed to make the whole line visible,
12046 put point at the top instead. That has to make the whole line
12047 visible, if it can be done. */
cb9d4a9f 12048 clear_glyph_matrix (w->desired_matrix);
ddf6b9a3
RS
12049 centering_position = 0;
12050 goto point_at_top;
12051 }
b5174a51 12052
74d481ac
GM
12053 done:
12054
5f5c8ee5
GM
12055 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12056 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12057 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12058 ? Qt : Qnil);
a2889657 12059
5f5c8ee5 12060 /* Display the mode line, if we must. */
e481f960 12061 if ((update_mode_line
aa6d10fa 12062 /* If window not full width, must redo its mode line
2311178e 12063 if (a) the window to its side is being redone and
5f5c8ee5
GM
12064 (b) we do a frame-based redisplay. This is a consequence
12065 of how inverted lines are drawn in frame-based redisplay. */
2311178e 12066 || (!just_this_one_p
5f5c8ee5
GM
12067 && !FRAME_WINDOW_P (f)
12068 && !WINDOW_FULL_WIDTH_P (w))
12069 /* Line number to display. */
155ef550 12070 || INTEGERP (w->base_line_pos)
5f5c8ee5 12071 /* Column number is displayed and different from the one displayed. */
155ef550 12072 || (!NILP (w->column_number_displayed)
2311178e
TTN
12073 && (XFASTINT (w->column_number_displayed)
12074 != (int) current_column ()))) /* iftc */
5f5c8ee5
GM
12075 /* This means that the window has a mode line. */
12076 && (WINDOW_WANTS_MODELINE_P (w)
045dee35 12077 || WINDOW_WANTS_HEADER_LINE_P (w)))
5ba50c51 12078 {
5f5c8ee5
GM
12079 display_mode_lines (w);
12080
12081 /* If mode line height has changed, arrange for a thorough
12082 immediate redisplay using the correct mode line height. */
12083 if (WINDOW_WANTS_MODELINE_P (w)
12084 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
5ba50c51 12085 {
5f5c8ee5
GM
12086 fonts_changed_p = 1;
12087 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12088 = DESIRED_MODE_LINE_HEIGHT (w);
5ba50c51 12089 }
2311178e 12090
5f5c8ee5
GM
12091 /* If top line height has changed, arrange for a thorough
12092 immediate redisplay using the correct mode line height. */
045dee35
GM
12093 if (WINDOW_WANTS_HEADER_LINE_P (w)
12094 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
5f5c8ee5
GM
12095 {
12096 fonts_changed_p = 1;
045dee35
GM
12097 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12098 = DESIRED_HEADER_LINE_HEIGHT (w);
5f5c8ee5
GM
12099 }
12100
12101 if (fonts_changed_p)
cb617e7c 12102 goto need_larger_matrices;
5ba50c51 12103 }
5f5c8ee5
GM
12104
12105 if (!line_number_displayed
12106 && !BUFFERP (w->base_line_pos))
aa6d10fa
RS
12107 {
12108 w->base_line_pos = Qnil;
12109 w->base_line_number = Qnil;
12110 }
a2889657 12111
5f5c8ee5 12112 finish_menu_bars:
2311178e 12113
7ce2c095 12114 /* When we reach a frame's selected window, redo the frame's menu bar. */
e481f960 12115 if (update_mode_line
5f5c8ee5
GM
12116 && EQ (FRAME_SELECTED_WINDOW (f), window))
12117 {
12118 int redisplay_menu_p = 0;
488dd4c4 12119 int redisplay_tool_bar_p = 0;
5f5c8ee5
GM
12120
12121 if (FRAME_WINDOW_P (f))
12122 {
488dd4c4
JD
12123#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12124 || defined (USE_GTK)
5f5c8ee5 12125 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
76412d64 12126#else
5f5c8ee5 12127 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
76412d64 12128#endif
5f5c8ee5
GM
12129 }
12130 else
12131 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12132
12133 if (redisplay_menu_p)
12134 display_menu_bar (w);
12135
12136#ifdef HAVE_WINDOW_SYSTEM
488dd4c4
JD
12137#ifdef USE_GTK
12138 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12139#else
12140 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12141 && (FRAME_TOOL_BAR_LINES (f) > 0
12142 || auto_resize_tool_bars_p);
177c0ea7 12143
488dd4c4
JD
12144#endif
12145
12146 if (redisplay_tool_bar_p)
12147 redisplay_tool_bar (f);
5f5c8ee5
GM
12148#endif
12149 }
7ce2c095 12150
7af0e8d7 12151#ifdef HAVE_WINDOW_SYSTEM
88e6b646 12152 if (update_window_fringes (w, 0)
6d925726 12153 && !just_this_one_p
88e6b646
KS
12154 && (used_current_matrix_p || overlay_arrow_seen)
12155 && !w->pseudo_window_p)
12156 {
12157 update_begin (f);
12158 BLOCK_INPUT;
12159 draw_window_fringes (w);
12160 UNBLOCK_INPUT;
12161 update_end (f);
12162 }
7af0e8d7 12163#endif /* HAVE_WINDOW_SYSTEM */
88e6b646 12164
b60c9653
RS
12165 /* We go to this label, with fonts_changed_p nonzero,
12166 if it is necessary to try again using larger glyph matrices.
12167 We have to redeem the scroll bar even in this case,
12168 because the loop in redisplay_internal expects that. */
cb617e7c
GM
12169 need_larger_matrices:
12170 ;
88f22aff 12171 finish_scroll_bars:
5f5c8ee5 12172
da8b7f4f 12173 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
30c566e4 12174 {
9a08d928
SM
12175 /* Set the thumb's position and size. */
12176 set_vertical_scroll_bar (w);
30c566e4 12177
5f5c8ee5
GM
12178 /* Note that we actually used the scroll bar attached to this
12179 window, so it shouldn't be deleted at the end of redisplay. */
504454e8 12180 redeem_scroll_bar_hook (w);
30c566e4 12181 }
b1d1124b 12182
5f5c8ee5
GM
12183 /* Restore current_buffer and value of point in it. */
12184 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
6a93695f 12185 set_buffer_internal_1 (old);
5f5c8ee5 12186 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
2e54982e
RS
12187
12188 unbind_to (count, Qnil);
a2889657 12189}
a2889657 12190
5f5c8ee5
GM
12191
12192/* Build the complete desired matrix of WINDOW with a window start
12193 buffer position POS. Value is non-zero if successful. It is zero
12194 if fonts were loaded during redisplay which makes re-adjusting
12195 glyph matrices necessary. */
12196
12197int
a2889657
JB
12198try_window (window, pos)
12199 Lisp_Object window;
5f5c8ee5
GM
12200 struct text_pos pos;
12201{
12202 struct window *w = XWINDOW (window);
12203 struct it it;
12204 struct glyph_row *last_text_row = NULL;
9cbab4ff 12205
5f5c8ee5
GM
12206 /* Make POS the new window start. */
12207 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12adba34 12208
5f5c8ee5
GM
12209 /* Mark cursor position as unknown. No overlay arrow seen. */
12210 w->cursor.vpos = -1;
a2889657 12211 overlay_arrow_seen = 0;
642eefc6 12212
5f5c8ee5
GM
12213 /* Initialize iterator and info to start at POS. */
12214 start_display (&it, w, pos);
a2889657 12215
5f5c8ee5
GM
12216 /* Display all lines of W. */
12217 while (it.current_y < it.last_visible_y)
12218 {
12219 if (display_line (&it))
12220 last_text_row = it.glyph_row - 1;
12221 if (fonts_changed_p)
12222 return 0;
12223 }
a2889657 12224
5f5c8ee5
GM
12225 /* If bottom moved off end of frame, change mode line percentage. */
12226 if (XFASTINT (w->window_end_pos) <= 0
12227 && Z != IT_CHARPOS (it))
a2889657
JB
12228 w->update_mode_line = Qt;
12229
5f5c8ee5
GM
12230 /* Set window_end_pos to the offset of the last character displayed
12231 on the window from the end of current_buffer. Set
12232 window_end_vpos to its row number. */
12233 if (last_text_row)
12234 {
12235 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12236 w->window_end_bytepos
12237 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
12238 w->window_end_pos
12239 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12240 w->window_end_vpos
12241 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
5f5c8ee5
GM
12242 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12243 ->displays_text_p);
12244 }
12245 else
12246 {
86bf3faa
RS
12247 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12248 w->window_end_pos = make_number (Z - ZV);
12249 w->window_end_vpos = make_number (0);
5f5c8ee5 12250 }
2311178e 12251
a2889657
JB
12252 /* But that is not valid info until redisplay finishes. */
12253 w->window_end_valid = Qnil;
5f5c8ee5 12254 return 1;
a2889657 12255}
5f5c8ee5
GM
12256
12257
a2889657 12258\f
5f5c8ee5
GM
12259/************************************************************************
12260 Window redisplay reusing current matrix when buffer has not changed
12261 ************************************************************************/
12262
12263/* Try redisplay of window W showing an unchanged buffer with a
12264 different window start than the last time it was displayed by
12265 reusing its current matrix. Value is non-zero if successful.
12266 W->start is the new window start. */
a2889657
JB
12267
12268static int
5f5c8ee5
GM
12269try_window_reusing_current_matrix (w)
12270 struct window *w;
a2889657 12271{
5f5c8ee5
GM
12272 struct frame *f = XFRAME (w->frame);
12273 struct glyph_row *row, *bottom_row;
12274 struct it it;
12275 struct run run;
12276 struct text_pos start, new_start;
12277 int nrows_scrolled, i;
12278 struct glyph_row *last_text_row;
12279 struct glyph_row *last_reused_text_row;
12280 struct glyph_row *start_row;
12281 int start_vpos, min_y, max_y;
75c5350a 12282
69d1f7c9 12283#if GLYPH_DEBUG
76cb5e06
GM
12284 if (inhibit_try_window_reusing)
12285 return 0;
12286#endif
12287
d18354b6
GM
12288 if (/* This function doesn't handle terminal frames. */
12289 !FRAME_WINDOW_P (f)
12290 /* Don't try to reuse the display if windows have been split
12291 or such. */
5fb96e96
RS
12292 || windows_or_buffers_changed
12293 || cursor_type_changed)
5f5c8ee5 12294 return 0;
a2889657 12295
5f5c8ee5
GM
12296 /* Can't do this if region may have changed. */
12297 if ((!NILP (Vtransient_mark_mode)
12298 && !NILP (current_buffer->mark_active))
8f897821
GM
12299 || !NILP (w->region_showing)
12300 || !NILP (Vshow_trailing_whitespace))
5f5c8ee5 12301 return 0;
a2889657 12302
5f5c8ee5 12303 /* If top-line visibility has changed, give up. */
045dee35
GM
12304 if (WINDOW_WANTS_HEADER_LINE_P (w)
12305 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
5f5c8ee5
GM
12306 return 0;
12307
12308 /* Give up if old or new display is scrolled vertically. We could
12309 make this function handle this, but right now it doesn't. */
12310 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12311 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
12312 return 0;
12313
12314 /* The variable new_start now holds the new window start. The old
12315 start `start' can be determined from the current matrix. */
12316 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12317 start = start_row->start.pos;
12318 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
a2889657 12319
5f5c8ee5
GM
12320 /* Clear the desired matrix for the display below. */
12321 clear_glyph_matrix (w->desired_matrix);
2311178e 12322
5f5c8ee5
GM
12323 if (CHARPOS (new_start) <= CHARPOS (start))
12324 {
12325 int first_row_y;
2311178e 12326
607ec83c
GM
12327 /* Don't use this method if the display starts with an ellipsis
12328 displayed for invisible text. It's not easy to handle that case
12329 below, and it's certainly not worth the effort since this is
12330 not a frequent case. */
12331 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12332 return 0;
12333
5f5c8ee5 12334 IF_DEBUG (debug_method_add (w, "twu1"));
2311178e 12335
5f5c8ee5
GM
12336 /* Display up to a row that can be reused. The variable
12337 last_text_row is set to the last row displayed that displays
b48f74cb
GM
12338 text. Note that it.vpos == 0 if or if not there is a
12339 header-line; it's not the same as the MATRIX_ROW_VPOS! */
5f5c8ee5
GM
12340 start_display (&it, w, new_start);
12341 first_row_y = it.current_y;
12342 w->cursor.vpos = -1;
12343 last_text_row = last_reused_text_row = NULL;
2311178e 12344
5f5c8ee5
GM
12345 while (it.current_y < it.last_visible_y
12346 && IT_CHARPOS (it) < CHARPOS (start)
12347 && !fonts_changed_p)
12348 if (display_line (&it))
12349 last_text_row = it.glyph_row - 1;
12350
12351 /* A value of current_y < last_visible_y means that we stopped
12352 at the previous window start, which in turn means that we
12353 have at least one reusable row. */
12354 if (it.current_y < it.last_visible_y)
a2889657 12355 {
b48f74cb 12356 /* IT.vpos always starts from 0; it counts text lines. */
5f5c8ee5 12357 nrows_scrolled = it.vpos;
2311178e 12358
5f5c8ee5
GM
12359 /* Find PT if not already found in the lines displayed. */
12360 if (w->cursor.vpos < 0)
a2889657 12361 {
5f5c8ee5 12362 int dy = it.current_y - first_row_y;
2311178e 12363
5f5c8ee5 12364 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
0df56f4d
GM
12365 row = row_containing_pos (w, PT, row, NULL, dy);
12366 if (row)
12367 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12368 dy, nrows_scrolled);
12369 else
5f5c8ee5
GM
12370 {
12371 clear_glyph_matrix (w->desired_matrix);
12372 return 0;
12373 }
a2889657 12374 }
5f5c8ee5
GM
12375
12376 /* Scroll the display. Do it before the current matrix is
12377 changed. The problem here is that update has not yet
12378 run, i.e. part of the current matrix is not up to date.
12379 scroll_run_hook will clear the cursor, and use the
12380 current matrix to get the height of the row the cursor is
12381 in. */
12382 run.current_y = first_row_y;
12383 run.desired_y = it.current_y;
12384 run.height = it.last_visible_y - it.current_y;
b48f74cb
GM
12385
12386 if (run.height > 0 && run.current_y != run.desired_y)
a2889657 12387 {
5f5c8ee5
GM
12388 update_begin (f);
12389 rif->update_window_begin_hook (w);
fa3c6b4d 12390 rif->clear_window_mouse_face (w);
5f5c8ee5 12391 rif->scroll_run_hook (w, &run);
64d1e7d3 12392 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5 12393 update_end (f);
a2889657 12394 }
5f5c8ee5
GM
12395
12396 /* Shift current matrix down by nrows_scrolled lines. */
12397 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12398 rotate_matrix (w->current_matrix,
12399 start_vpos,
12400 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12401 nrows_scrolled);
2311178e 12402
9c8b8382 12403 /* Disable lines that must be updated. */
5f5c8ee5 12404 for (i = 0; i < it.vpos; ++i)
b48f74cb 12405 (start_row + i)->enabled_p = 0;
9c8b8382 12406
5f5c8ee5 12407 /* Re-compute Y positions. */
da8b7f4f 12408 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
5f5c8ee5 12409 max_y = it.last_visible_y;
b48f74cb
GM
12410 for (row = start_row + nrows_scrolled;
12411 row < bottom_row;
12412 ++row)
d2f84654 12413 {
5f5c8ee5 12414 row->y = it.current_y;
75c5350a 12415 row->visible_height = row->height;
5f5c8ee5
GM
12416
12417 if (row->y < min_y)
75c5350a
GM
12418 row->visible_height -= min_y - row->y;
12419 if (row->y + row->height > max_y)
12420 row->visible_height -= row->y + row->height - max_y;
88e6b646 12421 row->redraw_fringe_bitmaps_p = 1;
2311178e 12422
5f5c8ee5 12423 it.current_y += row->height;
5f5c8ee5
GM
12424
12425 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12426 last_reused_text_row = row;
12427 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12428 break;
d2f84654 12429 }
2311178e 12430
9c8b8382
GM
12431 /* Disable lines in the current matrix which are now
12432 below the window. */
bafb434c 12433 for (++row; row < bottom_row; ++row)
9c8b8382 12434 row->enabled_p = 0;
a2889657 12435 }
5f5c8ee5
GM
12436
12437 /* Update window_end_pos etc.; last_reused_text_row is the last
12438 reused row from the current matrix containing text, if any.
12439 The value of last_text_row is the last displayed line
12440 containing text. */
12441 if (last_reused_text_row)
a2889657 12442 {
5f5c8ee5
GM
12443 w->window_end_bytepos
12444 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
ac90c44f
GM
12445 w->window_end_pos
12446 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12447 w->window_end_vpos
12448 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12449 w->current_matrix));
a2889657 12450 }
5f5c8ee5
GM
12451 else if (last_text_row)
12452 {
12453 w->window_end_bytepos
12454 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
12455 w->window_end_pos
12456 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12457 w->window_end_vpos
12458 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
5f5c8ee5
GM
12459 }
12460 else
12461 {
12462 /* This window must be completely empty. */
86bf3faa
RS
12463 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12464 w->window_end_pos = make_number (Z - ZV);
12465 w->window_end_vpos = make_number (0);
5f5c8ee5
GM
12466 }
12467 w->window_end_valid = Qnil;
a2889657 12468
5f5c8ee5
GM
12469 /* Update hint: don't try scrolling again in update_window. */
12470 w->desired_matrix->no_scrolling_p = 1;
2311178e 12471
5f5c8ee5
GM
12472#if GLYPH_DEBUG
12473 debug_method_add (w, "try_window_reusing_current_matrix 1");
12474#endif
12475 return 1;
a2889657 12476 }
5f5c8ee5
GM
12477 else if (CHARPOS (new_start) > CHARPOS (start))
12478 {
12479 struct glyph_row *pt_row, *row;
12480 struct glyph_row *first_reusable_row;
12481 struct glyph_row *first_row_to_display;
12482 int dy;
12483 int yb = window_text_bottom_y (w);
12484
5f5c8ee5
GM
12485 /* Find the row starting at new_start, if there is one. Don't
12486 reuse a partially visible line at the end. */
b48f74cb 12487 first_reusable_row = start_row;
5f5c8ee5
GM
12488 while (first_reusable_row->enabled_p
12489 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12490 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12491 < CHARPOS (new_start)))
12492 ++first_reusable_row;
12493
12494 /* Give up if there is no row to reuse. */
12495 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
28514cd9
GM
12496 || !first_reusable_row->enabled_p
12497 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12498 != CHARPOS (new_start)))
5f5c8ee5
GM
12499 return 0;
12500
5f5c8ee5
GM
12501 /* We can reuse fully visible rows beginning with
12502 first_reusable_row to the end of the window. Set
12503 first_row_to_display to the first row that cannot be reused.
12504 Set pt_row to the row containing point, if there is any. */
5f5c8ee5 12505 pt_row = NULL;
ac90c44f
GM
12506 for (first_row_to_display = first_reusable_row;
12507 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12508 ++first_row_to_display)
5f5c8ee5 12509 {
4bde0ebb
GM
12510 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12511 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
5f5c8ee5 12512 pt_row = first_row_to_display;
5f5c8ee5 12513 }
a2889657 12514
5f5c8ee5
GM
12515 /* Start displaying at the start of first_row_to_display. */
12516 xassert (first_row_to_display->y < yb);
12517 init_to_row_start (&it, w, first_row_to_display);
607ec83c 12518
b48f74cb
GM
12519 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12520 - start_vpos);
5f5c8ee5
GM
12521 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12522 - nrows_scrolled);
b48f74cb 12523 it.current_y = (first_row_to_display->y - first_reusable_row->y
da8b7f4f 12524 + WINDOW_HEADER_LINE_HEIGHT (w));
5f5c8ee5
GM
12525
12526 /* Display lines beginning with first_row_to_display in the
12527 desired matrix. Set last_text_row to the last row displayed
12528 that displays text. */
12529 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12530 if (pt_row == NULL)
12531 w->cursor.vpos = -1;
12532 last_text_row = NULL;
12533 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12534 if (display_line (&it))
12535 last_text_row = it.glyph_row - 1;
12536
12537 /* Give up If point isn't in a row displayed or reused. */
12538 if (w->cursor.vpos < 0)
12539 {
12540 clear_glyph_matrix (w->desired_matrix);
12541 return 0;
12542 }
12adba34 12543
5f5c8ee5
GM
12544 /* If point is in a reused row, adjust y and vpos of the cursor
12545 position. */
12546 if (pt_row)
12547 {
38793d6a
KS
12548 w->cursor.vpos -= nrows_scrolled;
12549 w->cursor.y -= first_reusable_row->y - start_row->y;
a2889657
JB
12550 }
12551
5f5c8ee5
GM
12552 /* Scroll the display. */
12553 run.current_y = first_reusable_row->y;
da8b7f4f 12554 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
5f5c8ee5 12555 run.height = it.last_visible_y - run.current_y;
4da61803 12556 dy = run.current_y - run.desired_y;
2311178e 12557
5f5c8ee5
GM
12558 if (run.height)
12559 {
5f5c8ee5
GM
12560 update_begin (f);
12561 rif->update_window_begin_hook (w);
fa3c6b4d 12562 rif->clear_window_mouse_face (w);
5f5c8ee5 12563 rif->scroll_run_hook (w, &run);
64d1e7d3 12564 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5
GM
12565 update_end (f);
12566 }
a2889657 12567
5f5c8ee5
GM
12568 /* Adjust Y positions of reused rows. */
12569 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
da8b7f4f 12570 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
5f5c8ee5 12571 max_y = it.last_visible_y;
b48f74cb 12572 for (row = first_reusable_row; row < first_row_to_display; ++row)
5f5c8ee5
GM
12573 {
12574 row->y -= dy;
75c5350a 12575 row->visible_height = row->height;
5f5c8ee5 12576 if (row->y < min_y)
75c5350a
GM
12577 row->visible_height -= min_y - row->y;
12578 if (row->y + row->height > max_y)
12579 row->visible_height -= row->y + row->height - max_y;
88e6b646 12580 row->redraw_fringe_bitmaps_p = 1;
5f5c8ee5 12581 }
a2889657 12582
5f5c8ee5
GM
12583 /* Scroll the current matrix. */
12584 xassert (nrows_scrolled > 0);
12585 rotate_matrix (w->current_matrix,
12586 start_vpos,
12587 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12588 -nrows_scrolled);
12589
ac90c44f
GM
12590 /* Disable rows not reused. */
12591 for (row -= nrows_scrolled; row < bottom_row; ++row)
12592 row->enabled_p = 0;
12593
38793d6a
KS
12594 /* Point may have moved to a different line, so we cannot assume that
12595 the previous cursor position is valid; locate the correct row. */
12596 if (pt_row)
12597 {
12598 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12599 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
12600 row++)
12601 {
12602 w->cursor.vpos++;
12603 w->cursor.y = row->y;
12604 }
12605 if (row < bottom_row)
12606 {
12607 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
12608 while (glyph->charpos < PT)
12609 {
12610 w->cursor.hpos++;
12611 w->cursor.x += glyph->pixel_width;
12612 glyph++;
12613 }
12614 }
12615 }
12616
5f5c8ee5
GM
12617 /* Adjust window end. A null value of last_text_row means that
12618 the window end is in reused rows which in turn means that
12619 only its vpos can have changed. */
12620 if (last_text_row)
12621 {
12622 w->window_end_bytepos
12623 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
12624 w->window_end_pos
12625 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12626 w->window_end_vpos
12627 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
5f5c8ee5
GM
12628 }
12629 else
a2889657 12630 {
ac90c44f
GM
12631 w->window_end_vpos
12632 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
a2889657 12633 }
2311178e 12634
5f5c8ee5
GM
12635 w->window_end_valid = Qnil;
12636 w->desired_matrix->no_scrolling_p = 1;
12637
12638#if GLYPH_DEBUG
12639 debug_method_add (w, "try_window_reusing_current_matrix 2");
12640#endif
12641 return 1;
a2889657 12642 }
2311178e 12643
5f5c8ee5
GM
12644 return 0;
12645}
a2889657 12646
a2889657 12647
5f5c8ee5
GM
12648\f
12649/************************************************************************
12650 Window redisplay reusing current matrix when buffer has changed
12651 ************************************************************************/
12652
1ec185cb
GM
12653static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
12654static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
5f5c8ee5
GM
12655 int *, int *));
12656static struct glyph_row *
12657find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
12658 struct glyph_row *));
12659
12660
12661/* Return the last row in MATRIX displaying text. If row START is
12662 non-null, start searching with that row. IT gives the dimensions
12663 of the display. Value is null if matrix is empty; otherwise it is
12664 a pointer to the row found. */
12665
12666static struct glyph_row *
12667find_last_row_displaying_text (matrix, it, start)
12668 struct glyph_matrix *matrix;
12669 struct it *it;
12670 struct glyph_row *start;
12671{
12672 struct glyph_row *row, *row_found;
12673
12674 /* Set row_found to the last row in IT->w's current matrix
12675 displaying text. The loop looks funny but think of partially
12676 visible lines. */
12677 row_found = NULL;
12678 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
12679 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12680 {
12681 xassert (row->enabled_p);
12682 row_found = row;
12683 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
12684 break;
12685 ++row;
a2889657 12686 }
2311178e 12687
5f5c8ee5
GM
12688 return row_found;
12689}
12690
a2889657 12691
5f5c8ee5 12692/* Return the last row in the current matrix of W that is not affected
d43fbe9d
GM
12693 by changes at the start of current_buffer that occurred since W's
12694 current matrix was built. Value is null if no such row exists.
a2889657 12695
d43fbe9d
GM
12696 BEG_UNCHANGED us the number of characters unchanged at the start of
12697 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
12698 first changed character in current_buffer. Characters at positions <
12699 BEG + BEG_UNCHANGED are at the same buffer positions as they were
12700 when the current matrix was built. */
5f5c8ee5
GM
12701
12702static struct glyph_row *
1ec185cb 12703find_last_unchanged_at_beg_row (w)
5f5c8ee5
GM
12704 struct window *w;
12705{
9142dd5b 12706 int first_changed_pos = BEG + BEG_UNCHANGED;
5f5c8ee5
GM
12707 struct glyph_row *row;
12708 struct glyph_row *row_found = NULL;
12709 int yb = window_text_bottom_y (w);
12710
12711 /* Find the last row displaying unchanged text. */
12712 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12713 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12714 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
a2889657 12715 {
5f5c8ee5
GM
12716 if (/* If row ends before first_changed_pos, it is unchanged,
12717 except in some case. */
12718 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
12719 /* When row ends in ZV and we write at ZV it is not
12720 unchanged. */
12721 && !row->ends_at_zv_p
12722 /* When first_changed_pos is the end of a continued line,
12723 row is not unchanged because it may be no longer
12724 continued. */
12725 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
88e6b646
KS
12726 && (row->continued_p
12727 || row->exact_window_width_line_p)))
5f5c8ee5
GM
12728 row_found = row;
12729
12730 /* Stop if last visible row. */
12731 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
12732 break;
2311178e 12733
5f5c8ee5 12734 ++row;
a2889657
JB
12735 }
12736
5f5c8ee5 12737 return row_found;
a2889657 12738}
5f5c8ee5
GM
12739
12740
12741/* Find the first glyph row in the current matrix of W that is not
2311178e 12742 affected by changes at the end of current_buffer since the
d43fbe9d
GM
12743 time W's current matrix was built.
12744
12745 Return in *DELTA the number of chars by which buffer positions in
12746 unchanged text at the end of current_buffer must be adjusted.
2311178e 12747
d43fbe9d
GM
12748 Return in *DELTA_BYTES the corresponding number of bytes.
12749
12750 Value is null if no such row exists, i.e. all rows are affected by
12751 changes. */
2311178e 12752
5f5c8ee5 12753static struct glyph_row *
1ec185cb 12754find_first_unchanged_at_end_row (w, delta, delta_bytes)
5f5c8ee5
GM
12755 struct window *w;
12756 int *delta, *delta_bytes;
a2889657 12757{
5f5c8ee5
GM
12758 struct glyph_row *row;
12759 struct glyph_row *row_found = NULL;
c581d710 12760
5f5c8ee5 12761 *delta = *delta_bytes = 0;
b2a76982 12762
1ec185cb
GM
12763 /* Display must not have been paused, otherwise the current matrix
12764 is not up to date. */
12765 if (NILP (w->window_end_valid))
12766 abort ();
2311178e 12767
1ec185cb 12768 /* A value of window_end_pos >= END_UNCHANGED means that the window
5f5c8ee5
GM
12769 end is in the range of changed text. If so, there is no
12770 unchanged row at the end of W's current matrix. */
9142dd5b 12771 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
5f5c8ee5
GM
12772 return NULL;
12773
12774 /* Set row to the last row in W's current matrix displaying text. */
12775 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
2311178e
TTN
12776
12777 /* If matrix is entirely empty, no unchanged row exists. */
5f5c8ee5
GM
12778 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12779 {
12780 /* The value of row is the last glyph row in the matrix having a
12781 meaningful buffer position in it. The end position of row
12782 corresponds to window_end_pos. This allows us to translate
12783 buffer positions in the current matrix to current buffer
12784 positions for characters not in changed text. */
12785 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12786 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12787 int last_unchanged_pos, last_unchanged_pos_old;
12788 struct glyph_row *first_text_row
12789 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12790
12791 *delta = Z - Z_old;
12792 *delta_bytes = Z_BYTE - Z_BYTE_old;
12793
12794 /* Set last_unchanged_pos to the buffer position of the last
12795 character in the buffer that has not been changed. Z is the
d43fbe9d
GM
12796 index + 1 of the last character in current_buffer, i.e. by
12797 subtracting END_UNCHANGED we get the index of the last
5f5c8ee5
GM
12798 unchanged character, and we have to add BEG to get its buffer
12799 position. */
9142dd5b 12800 last_unchanged_pos = Z - END_UNCHANGED + BEG;
5f5c8ee5 12801 last_unchanged_pos_old = last_unchanged_pos - *delta;
2311178e 12802
5f5c8ee5
GM
12803 /* Search backward from ROW for a row displaying a line that
12804 starts at a minimum position >= last_unchanged_pos_old. */
1ec185cb 12805 for (; row > first_text_row; --row)
5f5c8ee5 12806 {
1ec185cb
GM
12807 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
12808 abort ();
2311178e 12809
5f5c8ee5
GM
12810 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
12811 row_found = row;
5f5c8ee5
GM
12812 }
12813 }
12814
1ec185cb
GM
12815 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
12816 abort ();
2311178e 12817
5f5c8ee5 12818 return row_found;
c581d710
RS
12819}
12820
c581d710 12821
5f5c8ee5
GM
12822/* Make sure that glyph rows in the current matrix of window W
12823 reference the same glyph memory as corresponding rows in the
12824 frame's frame matrix. This function is called after scrolling W's
12825 current matrix on a terminal frame in try_window_id and
12826 try_window_reusing_current_matrix. */
12827
12828static void
12829sync_frame_with_window_matrix_rows (w)
12830 struct window *w;
c581d710 12831{
5f5c8ee5
GM
12832 struct frame *f = XFRAME (w->frame);
12833 struct glyph_row *window_row, *window_row_end, *frame_row;
12834
12835 /* Preconditions: W must be a leaf window and full-width. Its frame
12836 must have a frame matrix. */
12837 xassert (NILP (w->hchild) && NILP (w->vchild));
12838 xassert (WINDOW_FULL_WIDTH_P (w));
12839 xassert (!FRAME_WINDOW_P (f));
12840
12841 /* If W is a full-width window, glyph pointers in W's current matrix
12842 have, by definition, to be the same as glyph pointers in the
7d4cc828
GM
12843 corresponding frame matrix. Note that frame matrices have no
12844 marginal areas (see build_frame_matrix). */
5f5c8ee5
GM
12845 window_row = w->current_matrix->rows;
12846 window_row_end = window_row + w->current_matrix->nrows;
da8b7f4f 12847 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
5f5c8ee5 12848 while (window_row < window_row_end)
659a218f 12849 {
7d4cc828
GM
12850 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
12851 struct glyph *end = window_row->glyphs[LAST_AREA];
12852
12853 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
12854 frame_row->glyphs[TEXT_AREA] = start;
12855 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
12856 frame_row->glyphs[LAST_AREA] = end;
f002db93
GM
12857
12858 /* Disable frame rows whose corresponding window rows have
12859 been disabled in try_window_id. */
12860 if (!window_row->enabled_p)
12861 frame_row->enabled_p = 0;
2311178e 12862
5f5c8ee5 12863 ++window_row, ++frame_row;
659a218f 12864 }
a2889657 12865}
5f5c8ee5
GM
12866
12867
e037b9ec
GM
12868/* Find the glyph row in window W containing CHARPOS. Consider all
12869 rows between START and END (not inclusive). END null means search
12870 all rows to the end of the display area of W. Value is the row
12871 containing CHARPOS or null. */
12872
0bef35bc 12873struct glyph_row *
0df56f4d 12874row_containing_pos (w, charpos, start, end, dy)
e037b9ec
GM
12875 struct window *w;
12876 int charpos;
12877 struct glyph_row *start, *end;
0df56f4d 12878 int dy;
e037b9ec
GM
12879{
12880 struct glyph_row *row = start;
12881 int last_y;
12882
12883 /* If we happen to start on a header-line, skip that. */
12884 if (row->mode_line_p)
12885 ++row;
2311178e 12886
e037b9ec
GM
12887 if ((end && row >= end) || !row->enabled_p)
12888 return NULL;
2311178e 12889
0df56f4d 12890 last_y = window_text_bottom_y (w) - dy;
2311178e 12891
5cce13dd
RS
12892 while (1)
12893 {
12894 /* Give up if we have gone too far. */
12895 if (end && row >= end)
12896 return NULL;
40a301b3
RS
12897 /* This formerly returned if they were equal.
12898 I think that both quantities are of a "last plus one" type;
12899 if so, when they are equal, the row is within the screen. -- rms. */
12900 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
5cce13dd
RS
12901 return NULL;
12902
12903 /* If it is in this row, return this row. */
12904 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
e037b9ec 12905 || (MATRIX_ROW_END_CHARPOS (row) == charpos
0df56f4d
GM
12906 /* The end position of a row equals the start
12907 position of the next row. If CHARPOS is there, we
12908 would rather display it in the next line, except
12909 when this line ends in ZV. */
12910 && !row->ends_at_zv_p
5cce13dd
RS
12911 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12912 && charpos >= MATRIX_ROW_START_CHARPOS (row))
12913 return row;
12914 ++row;
12915 }
e037b9ec
GM
12916}
12917
12918
5f5c8ee5
GM
12919/* Try to redisplay window W by reusing its existing display. W's
12920 current matrix must be up to date when this function is called,
12921 i.e. window_end_valid must not be nil.
12922
12923 Value is
12924
12925 1 if display has been updated
12926 0 if otherwise unsuccessful
12927 -1 if redisplay with same window start is known not to succeed
12928
12929 The following steps are performed:
12930
12931 1. Find the last row in the current matrix of W that is not
12932 affected by changes at the start of current_buffer. If no such row
12933 is found, give up.
12934
12935 2. Find the first row in W's current matrix that is not affected by
12936 changes at the end of current_buffer. Maybe there is no such row.
12937
12938 3. Display lines beginning with the row + 1 found in step 1 to the
12939 row found in step 2 or, if step 2 didn't find a row, to the end of
12940 the window.
12941
12942 4. If cursor is not known to appear on the window, give up.
12943
12944 5. If display stopped at the row found in step 2, scroll the
12945 display and current matrix as needed.
12946
12947 6. Maybe display some lines at the end of W, if we must. This can
12948 happen under various circumstances, like a partially visible line
12949 becoming fully visible, or because newly displayed lines are displayed
12950 in smaller font sizes.
12951
12952 7. Update W's window end information. */
12953
12adba34 12954static int
5f5c8ee5 12955try_window_id (w)
12adba34 12956 struct window *w;
12adba34 12957{
5f5c8ee5
GM
12958 struct frame *f = XFRAME (w->frame);
12959 struct glyph_matrix *current_matrix = w->current_matrix;
12960 struct glyph_matrix *desired_matrix = w->desired_matrix;
12961 struct glyph_row *last_unchanged_at_beg_row;
12962 struct glyph_row *first_unchanged_at_end_row;
12963 struct glyph_row *row;
12964 struct glyph_row *bottom_row;
12965 int bottom_vpos;
12966 struct it it;
12967 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
12968 struct text_pos start_pos;
12969 struct run run;
12970 int first_unchanged_at_end_vpos = 0;
12971 struct glyph_row *last_text_row, *last_text_row_at_end;
12972 struct text_pos start;
27d16f05 12973 int first_changed_charpos, last_changed_charpos;
5f5c8ee5 12974
69d1f7c9 12975#if GLYPH_DEBUG
76cb5e06
GM
12976 if (inhibit_try_window_id)
12977 return 0;
12978#endif
12979
27d16f05
GM
12980 /* This is handy for debugging. */
12981#if 0
12982#define GIVE_UP(X) \
12983 do { \
12984 fprintf (stderr, "try_window_id give up %d\n", (X)); \
12985 return 0; \
12986 } while (0)
12987#else
62397849 12988#define GIVE_UP(X) return 0
27d16f05 12989#endif
2311178e 12990
5f5c8ee5
GM
12991 SET_TEXT_POS_FROM_MARKER (start, w->start);
12992
27d16f05
GM
12993 /* Don't use this for mini-windows because these can show
12994 messages and mini-buffers, and we don't handle that here. */
12995 if (MINI_WINDOW_P (w))
12996 GIVE_UP (1);
2311178e 12997
27d16f05 12998 /* This flag is used to prevent redisplay optimizations. */
5fb96e96 12999 if (windows_or_buffers_changed || cursor_type_changed)
27d16f05 13000 GIVE_UP (2);
2311178e 13001
c62c1bb5
RS
13002 /* Verify that narrowing has not changed.
13003 Also verify that we were not told to prevent redisplay optimizations.
13004 It would be nice to further
27d16f05 13005 reduce the number of cases where this prevents try_window_id. */
c62c1bb5
RS
13006 if (current_buffer->clip_changed
13007 || current_buffer->prevent_redisplay_optimizations_p)
27d16f05
GM
13008 GIVE_UP (3);
13009
13010 /* Window must either use window-based redisplay or be full width. */
13011 if (!FRAME_WINDOW_P (f)
13012 && (!line_ins_del_ok
13013 || !WINDOW_FULL_WIDTH_P (w)))
13014 GIVE_UP (4);
5f5c8ee5 13015
f5376658 13016 /* Give up if point is not known NOT to appear in W. */
27d16f05
GM
13017 if (PT < CHARPOS (start))
13018 GIVE_UP (5);
13019
13020 /* Another way to prevent redisplay optimizations. */
13021 if (XFASTINT (w->last_modified) == 0)
13022 GIVE_UP (6);
2311178e 13023
f5376658 13024 /* Verify that window is not hscrolled. */
27d16f05
GM
13025 if (XFASTINT (w->hscroll) != 0)
13026 GIVE_UP (7);
2311178e 13027
f5376658 13028 /* Verify that display wasn't paused. */
27d16f05
GM
13029 if (NILP (w->window_end_valid))
13030 GIVE_UP (8);
2311178e 13031
27d16f05
GM
13032 /* Can't use this if highlighting a region because a cursor movement
13033 will do more than just set the cursor. */
13034 if (!NILP (Vtransient_mark_mode)
13035 && !NILP (current_buffer->mark_active))
13036 GIVE_UP (9);
13037
13038 /* Likewise if highlighting trailing whitespace. */
13039 if (!NILP (Vshow_trailing_whitespace))
13040 GIVE_UP (11);
2311178e 13041
27d16f05
GM
13042 /* Likewise if showing a region. */
13043 if (!NILP (w->region_showing))
13044 GIVE_UP (10);
2311178e 13045
27d16f05 13046 /* Can use this if overlay arrow position and or string have changed. */
351b5434 13047 if (overlay_arrows_changed_p ())
27d16f05
GM
13048 GIVE_UP (12);
13049
2311178e 13050
5f5c8ee5
GM
13051 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13052 only if buffer has really changed. The reason is that the gap is
13053 initially at Z for freshly visited files. The code below would
13054 set end_unchanged to 0 in that case. */
28ee91c0
GM
13055 if (MODIFF > SAVE_MODIFF
13056 /* This seems to happen sometimes after saving a buffer. */
13057 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
5f5c8ee5 13058 {
9142dd5b
GM
13059 if (GPT - BEG < BEG_UNCHANGED)
13060 BEG_UNCHANGED = GPT - BEG;
13061 if (Z - GPT < END_UNCHANGED)
13062 END_UNCHANGED = Z - GPT;
5f5c8ee5 13063 }
bf9249e3 13064
27d16f05
GM
13065 /* The position of the first and last character that has been changed. */
13066 first_changed_charpos = BEG + BEG_UNCHANGED;
13067 last_changed_charpos = Z - END_UNCHANGED;
13068
5f5c8ee5
GM
13069 /* If window starts after a line end, and the last change is in
13070 front of that newline, then changes don't affect the display.
f2d86d7a
GM
13071 This case happens with stealth-fontification. Note that although
13072 the display is unchanged, glyph positions in the matrix have to
13073 be adjusted, of course. */
5f5c8ee5 13074 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
27d16f05 13075 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
e60f4527 13076 && ((last_changed_charpos < CHARPOS (start)
27d16f05 13077 && CHARPOS (start) == BEGV)
e60f4527 13078 || (last_changed_charpos < CHARPOS (start) - 1
27d16f05
GM
13079 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13080 {
13081 int Z_old, delta, Z_BYTE_old, delta_bytes;
13082 struct glyph_row *r0;
13083
13084 /* Compute how many chars/bytes have been added to or removed
13085 from the buffer. */
13086 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13087 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13088 delta = Z - Z_old;
13089 delta_bytes = Z_BYTE - Z_BYTE_old;
2311178e 13090
27d16f05
GM
13091 /* Give up if PT is not in the window. Note that it already has
13092 been checked at the start of try_window_id that PT is not in
13093 front of the window start. */
13094 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13095 GIVE_UP (13);
13096
13097 /* If window start is unchanged, we can reuse the whole matrix
13098 as is, after adjusting glyph positions. No need to compute
13099 the window end again, since its offset from Z hasn't changed. */
13100 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13101 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
2d031b89
AS
13102 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13103 /* PT must not be in a partially visible line. */
13104 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13105 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
27d16f05
GM
13106 {
13107 /* Adjust positions in the glyph matrix. */
13108 if (delta || delta_bytes)
13109 {
13110 struct glyph_row *r1
13111 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13112 increment_matrix_positions (w->current_matrix,
13113 MATRIX_ROW_VPOS (r0, current_matrix),
13114 MATRIX_ROW_VPOS (r1, current_matrix),
13115 delta, delta_bytes);
13116 }
2311178e 13117
27d16f05 13118 /* Set the cursor. */
0df56f4d 13119 row = row_containing_pos (w, PT, r0, NULL, 0);
59adf8e8
KS
13120 if (row)
13121 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
40a301b3
RS
13122 else
13123 abort ();
27d16f05
GM
13124 return 1;
13125 }
5f5c8ee5
GM
13126 }
13127
27d16f05 13128 /* Handle the case that changes are all below what is displayed in
33ea6c4d 13129 the window, and that PT is in the window. This shortcut cannot
2b9c25e0
GM
13130 be taken if ZV is visible in the window, and text has been added
13131 there that is visible in the window. */
13132 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
33ea6c4d
GM
13133 /* ZV is not visible in the window, or there are no
13134 changes at ZV, actually. */
13135 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13136 || first_changed_charpos == last_changed_charpos))
5f5c8ee5 13137 {
27d16f05 13138 struct glyph_row *r0;
ef121659 13139
27d16f05
GM
13140 /* Give up if PT is not in the window. Note that it already has
13141 been checked at the start of try_window_id that PT is not in
13142 front of the window start. */
13143 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13144 GIVE_UP (14);
13145
13146 /* If window start is unchanged, we can reuse the whole matrix
13147 as is, without changing glyph positions since no text has
13148 been added/removed in front of the window end. */
13149 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
2d031b89
AS
13150 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13151 /* PT must not be in a partially visible line. */
13152 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13153 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
27d16f05
GM
13154 {
13155 /* We have to compute the window end anew since text
13156 can have been added/removed after it. */
13157 w->window_end_pos
13158 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13159 w->window_end_bytepos
13160 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13161
13162 /* Set the cursor. */
0df56f4d 13163 row = row_containing_pos (w, PT, r0, NULL, 0);
59adf8e8
KS
13164 if (row)
13165 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
40a301b3
RS
13166 else
13167 abort ();
27d16f05
GM
13168 return 2;
13169 }
5f5c8ee5
GM
13170 }
13171
2b9c25e0 13172 /* Give up if window start is in the changed area.
2311178e 13173
2b9c25e0
GM
13174 The condition used to read
13175
13176 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13177
13178 but why that was tested escapes me at the moment. */
13179 if (CHARPOS (start) >= first_changed_charpos
27d16f05
GM
13180 && CHARPOS (start) <= last_changed_charpos)
13181 GIVE_UP (15);
2311178e 13182
f5376658
RS
13183 /* Check that window start agrees with the start of the first glyph
13184 row in its current matrix. Check this after we know the window
13185 start is not in changed text, otherwise positions would not be
13186 comparable. */
27d16f05 13187 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
5f5c8ee5 13188 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
27d16f05 13189 GIVE_UP (16);
5f5c8ee5 13190
23e8bd86
GM
13191 /* Give up if the window ends in strings. Overlay strings
13192 at the end are difficult to handle, so don't try. */
13193 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13194 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13195 GIVE_UP (20);
13196
5f5c8ee5
GM
13197 /* Compute the position at which we have to start displaying new
13198 lines. Some of the lines at the top of the window might be
13199 reusable because they are not displaying changed text. Find the
13200 last row in W's current matrix not affected by changes at the
13201 start of current_buffer. Value is null if changes start in the
13202 first line of window. */
1ec185cb 13203 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
5f5c8ee5
GM
13204 if (last_unchanged_at_beg_row)
13205 {
67f1cf4c
GM
13206 /* Avoid starting to display in the moddle of a character, a TAB
13207 for instance. This is easier than to set up the iterator
13208 exactly, and it's not a frequent case, so the additional
13209 effort wouldn't really pay off. */
e74fb0f7
GM
13210 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13211 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
67f1cf4c
GM
13212 && last_unchanged_at_beg_row > w->current_matrix->rows)
13213 --last_unchanged_at_beg_row;
13214
13215 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
27d16f05 13216 GIVE_UP (17);
47d57b22
GM
13217
13218 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13219 GIVE_UP (18);
5f5c8ee5 13220 start_pos = it.current.pos;
2311178e 13221
5f5c8ee5
GM
13222 /* Start displaying new lines in the desired matrix at the same
13223 vpos we would use in the current matrix, i.e. below
13224 last_unchanged_at_beg_row. */
13225 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13226 current_matrix);
13227 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13228 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13229
13230 xassert (it.hpos == 0 && it.current_x == 0);
13231 }
13232 else
13233 {
13234 /* There are no reusable lines at the start of the window.
3ccb8bcf 13235 Start displaying in the first text line. */
5f5c8ee5 13236 start_display (&it, w, start);
3ccb8bcf 13237 it.vpos = it.first_vpos;
5f5c8ee5
GM
13238 start_pos = it.current.pos;
13239 }
13240
5f5c8ee5
GM
13241 /* Find the first row that is not affected by changes at the end of
13242 the buffer. Value will be null if there is no unchanged row, in
13243 which case we must redisplay to the end of the window. delta
13244 will be set to the value by which buffer positions beginning with
13245 first_unchanged_at_end_row have to be adjusted due to text
13246 changes. */
13247 first_unchanged_at_end_row
1ec185cb 13248 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
5f5c8ee5
GM
13249 IF_DEBUG (debug_delta = delta);
13250 IF_DEBUG (debug_delta_bytes = delta_bytes);
2311178e 13251
5f5c8ee5
GM
13252 /* Set stop_pos to the buffer position up to which we will have to
13253 display new lines. If first_unchanged_at_end_row != NULL, this
13254 is the buffer position of the start of the line displayed in that
13255 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13256 that we don't stop at a buffer position. */
13257 stop_pos = 0;
13258 if (first_unchanged_at_end_row)
13259 {
13260 xassert (last_unchanged_at_beg_row == NULL
13261 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
2311178e 13262
5f5c8ee5
GM
13263 /* If this is a continuation line, move forward to the next one
13264 that isn't. Changes in lines above affect this line.
13265 Caution: this may move first_unchanged_at_end_row to a row
13266 not displaying text. */
13267 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13268 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13269 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13270 < it.last_visible_y))
13271 ++first_unchanged_at_end_row;
13272
13273 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13274 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13275 >= it.last_visible_y))
13276 first_unchanged_at_end_row = NULL;
13277 else
13278 {
13279 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13280 + delta);
13281 first_unchanged_at_end_vpos
13282 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
9142dd5b 13283 xassert (stop_pos >= Z - END_UNCHANGED);
5f5c8ee5
GM
13284 }
13285 }
13286 else if (last_unchanged_at_beg_row == NULL)
23e8bd86 13287 GIVE_UP (19);
5f5c8ee5
GM
13288
13289
13290#if GLYPH_DEBUG
2311178e 13291
5f5c8ee5
GM
13292 /* Either there is no unchanged row at the end, or the one we have
13293 now displays text. This is a necessary condition for the window
13294 end pos calculation at the end of this function. */
13295 xassert (first_unchanged_at_end_row == NULL
13296 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
2311178e 13297
5f5c8ee5
GM
13298 debug_last_unchanged_at_beg_vpos
13299 = (last_unchanged_at_beg_row
13300 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13301 : -1);
13302 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
2311178e 13303
5f5c8ee5
GM
13304#endif /* GLYPH_DEBUG != 0 */
13305
2311178e 13306
5f5c8ee5
GM
13307 /* Display new lines. Set last_text_row to the last new line
13308 displayed which has text on it, i.e. might end up as being the
13309 line where the window_end_vpos is. */
13310 w->cursor.vpos = -1;
13311 last_text_row = NULL;
13312 overlay_arrow_seen = 0;
13313 while (it.current_y < it.last_visible_y
13314 && !fonts_changed_p
13315 && (first_unchanged_at_end_row == NULL
13316 || IT_CHARPOS (it) < stop_pos))
13317 {
13318 if (display_line (&it))
13319 last_text_row = it.glyph_row - 1;
13320 }
13321
13322 if (fonts_changed_p)
13323 return -1;
13324
13325
13326 /* Compute differences in buffer positions, y-positions etc. for
13327 lines reused at the bottom of the window. Compute what we can
13328 scroll. */
ca42b2e8
GM
13329 if (first_unchanged_at_end_row
13330 /* No lines reused because we displayed everything up to the
13331 bottom of the window. */
13332 && it.current_y < it.last_visible_y)
5f5c8ee5
GM
13333 {
13334 dvpos = (it.vpos
13335 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13336 current_matrix));
13337 dy = it.current_y - first_unchanged_at_end_row->y;
13338 run.current_y = first_unchanged_at_end_row->y;
13339 run.desired_y = run.current_y + dy;
13340 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13341 }
13342 else
ca42b2e8
GM
13343 {
13344 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13345 first_unchanged_at_end_row = NULL;
13346 }
5f5c8ee5
GM
13347 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13348
8f8ba186 13349
5f5c8ee5
GM
13350 /* Find the cursor if not already found. We have to decide whether
13351 PT will appear on this window (it sometimes doesn't, but this is
13352 not a very frequent case.) This decision has to be made before
13353 the current matrix is altered. A value of cursor.vpos < 0 means
13354 that PT is either in one of the lines beginning at
13355 first_unchanged_at_end_row or below the window. Don't care for
13356 lines that might be displayed later at the window end; as
13357 mentioned, this is not a frequent case. */
13358 if (w->cursor.vpos < 0)
13359 {
5f5c8ee5
GM
13360 /* Cursor in unchanged rows at the top? */
13361 if (PT < CHARPOS (start_pos)
13362 && last_unchanged_at_beg_row)
13363 {
e037b9ec
GM
13364 row = row_containing_pos (w, PT,
13365 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
0df56f4d 13366 last_unchanged_at_beg_row + 1, 0);
bfe0ee88
GM
13367 if (row)
13368 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
5f5c8ee5
GM
13369 }
13370
13371 /* Start from first_unchanged_at_end_row looking for PT. */
13372 else if (first_unchanged_at_end_row)
13373 {
e037b9ec 13374 row = row_containing_pos (w, PT - delta,
0df56f4d 13375 first_unchanged_at_end_row, NULL, 0);
e037b9ec 13376 if (row)
468155d7
GM
13377 set_cursor_from_row (w, row, w->current_matrix, delta,
13378 delta_bytes, dy, dvpos);
5f5c8ee5
GM
13379 }
13380
13381 /* Give up if cursor was not found. */
13382 if (w->cursor.vpos < 0)
13383 {
13384 clear_glyph_matrix (w->desired_matrix);
13385 return -1;
13386 }
13387 }
2311178e 13388
5f5c8ee5
GM
13389 /* Don't let the cursor end in the scroll margins. */
13390 {
13391 int this_scroll_margin, cursor_height;
2311178e 13392
5f5c8ee5 13393 this_scroll_margin = max (0, scroll_margin);
da8b7f4f
KS
13394 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13395 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
5f5c8ee5 13396 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
2311178e 13397
5f5c8ee5
GM
13398 if ((w->cursor.y < this_scroll_margin
13399 && CHARPOS (start) > BEGV)
38793d6a
KS
13400 /* Old redisplay didn't take scroll margin into account at the bottom,
13401 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
13402 || w->cursor.y + cursor_height + this_scroll_margin > it.last_visible_y)
5f5c8ee5
GM
13403 {
13404 w->cursor.vpos = -1;
13405 clear_glyph_matrix (w->desired_matrix);
13406 return -1;
13407 }
13408 }
13409
13410 /* Scroll the display. Do it before changing the current matrix so
13411 that xterm.c doesn't get confused about where the cursor glyph is
13412 found. */
fa77249f 13413 if (dy && run.height)
5f5c8ee5
GM
13414 {
13415 update_begin (f);
2311178e 13416
5f5c8ee5
GM
13417 if (FRAME_WINDOW_P (f))
13418 {
13419 rif->update_window_begin_hook (w);
fa3c6b4d 13420 rif->clear_window_mouse_face (w);
5f5c8ee5 13421 rif->scroll_run_hook (w, &run);
64d1e7d3 13422 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5
GM
13423 }
13424 else
13425 {
13426 /* Terminal frame. In this case, dvpos gives the number of
13427 lines to scroll by; dvpos < 0 means scroll up. */
13428 int first_unchanged_at_end_vpos
13429 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
da8b7f4f
KS
13430 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13431 int end = (WINDOW_TOP_EDGE_LINE (w)
522ed7fb
GM
13432 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13433 + window_internal_height (w));
2311178e 13434
5f5c8ee5
GM
13435 /* Perform the operation on the screen. */
13436 if (dvpos > 0)
13437 {
13438 /* Scroll last_unchanged_at_beg_row to the end of the
13439 window down dvpos lines. */
13440 set_terminal_window (end);
13441
13442 /* On dumb terminals delete dvpos lines at the end
13443 before inserting dvpos empty lines. */
13444 if (!scroll_region_ok)
13445 ins_del_lines (end - dvpos, -dvpos);
13446
13447 /* Insert dvpos empty lines in front of
13448 last_unchanged_at_beg_row. */
13449 ins_del_lines (from, dvpos);
13450 }
13451 else if (dvpos < 0)
13452 {
13453 /* Scroll up last_unchanged_at_beg_vpos to the end of
13454 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13455 set_terminal_window (end);
13456
13457 /* Delete dvpos lines in front of
13458 last_unchanged_at_beg_vpos. ins_del_lines will set
13459 the cursor to the given vpos and emit |dvpos| delete
13460 line sequences. */
13461 ins_del_lines (from + dvpos, dvpos);
13462
13463 /* On a dumb terminal insert dvpos empty lines at the
13464 end. */
13465 if (!scroll_region_ok)
13466 ins_del_lines (end + dvpos, -dvpos);
13467 }
2311178e 13468
5f5c8ee5
GM
13469 set_terminal_window (0);
13470 }
2311178e 13471
5f5c8ee5
GM
13472 update_end (f);
13473 }
13474
ca42b2e8
GM
13475 /* Shift reused rows of the current matrix to the right position.
13476 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13477 text. */
13478 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13479 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
5f5c8ee5
GM
13480 if (dvpos < 0)
13481 {
13482 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13483 bottom_vpos, dvpos);
13484 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13485 bottom_vpos, 0);
13486 }
13487 else if (dvpos > 0)
13488 {
13489 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13490 bottom_vpos, dvpos);
13491 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13492 first_unchanged_at_end_vpos + dvpos, 0);
13493 }
13494
13495 /* For frame-based redisplay, make sure that current frame and window
13496 matrix are in sync with respect to glyph memory. */
13497 if (!FRAME_WINDOW_P (f))
13498 sync_frame_with_window_matrix_rows (w);
13499
13500 /* Adjust buffer positions in reused rows. */
13501 if (delta)
f2d86d7a
GM
13502 increment_matrix_positions (current_matrix,
13503 first_unchanged_at_end_vpos + dvpos,
13504 bottom_vpos, delta, delta_bytes);
5f5c8ee5
GM
13505
13506 /* Adjust Y positions. */
13507 if (dy)
13508 shift_glyph_matrix (w, current_matrix,
13509 first_unchanged_at_end_vpos + dvpos,
13510 bottom_vpos, dy);
13511
13512 if (first_unchanged_at_end_row)
13513 first_unchanged_at_end_row += dvpos;
13514
13515 /* If scrolling up, there may be some lines to display at the end of
13516 the window. */
13517 last_text_row_at_end = NULL;
13518 if (dy < 0)
13519 {
9c6ba6d1
GM
13520 /* Scrolling up can leave for example a partially visible line
13521 at the end of the window to be redisplayed. */
5f5c8ee5
GM
13522 /* Set last_row to the glyph row in the current matrix where the
13523 window end line is found. It has been moved up or down in
13524 the matrix by dvpos. */
13525 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13526 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13527
13528 /* If last_row is the window end line, it should display text. */
13529 xassert (last_row->displays_text_p);
13530
13531 /* If window end line was partially visible before, begin
13532 displaying at that line. Otherwise begin displaying with the
13533 line following it. */
13534 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13535 {
13536 init_to_row_start (&it, w, last_row);
13537 it.vpos = last_vpos;
13538 it.current_y = last_row->y;
13539 }
13540 else
13541 {
13542 init_to_row_end (&it, w, last_row);
13543 it.vpos = 1 + last_vpos;
13544 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13545 ++last_row;
13546 }
12adba34 13547
23e8bd86
GM
13548 /* We may start in a continuation line. If so, we have to
13549 get the right continuation_lines_width and current_x. */
13550 it.continuation_lines_width = last_row->continuation_lines_width;
13551 it.hpos = it.current_x = 0;
2311178e 13552
23e8bd86
GM
13553 /* Display the rest of the lines at the window end. */
13554 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13555 while (it.current_y < it.last_visible_y
13556 && !fonts_changed_p)
5f5c8ee5 13557 {
23e8bd86
GM
13558 /* Is it always sure that the display agrees with lines in
13559 the current matrix? I don't think so, so we mark rows
13560 displayed invalid in the current matrix by setting their
13561 enabled_p flag to zero. */
13562 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13563 if (display_line (&it))
13564 last_text_row_at_end = it.glyph_row - 1;
5f5c8ee5
GM
13565 }
13566 }
12adba34 13567
5f5c8ee5
GM
13568 /* Update window_end_pos and window_end_vpos. */
13569 if (first_unchanged_at_end_row
13570 && first_unchanged_at_end_row->y < it.last_visible_y
13571 && !last_text_row_at_end)
13572 {
13573 /* Window end line if one of the preserved rows from the current
13574 matrix. Set row to the last row displaying text in current
13575 matrix starting at first_unchanged_at_end_row, after
13576 scrolling. */
13577 xassert (first_unchanged_at_end_row->displays_text_p);
13578 row = find_last_row_displaying_text (w->current_matrix, &it,
13579 first_unchanged_at_end_row);
13580 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
2311178e 13581
ac90c44f 13582 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
5f5c8ee5 13583 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
ac90c44f
GM
13584 w->window_end_vpos
13585 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
23fca891 13586 xassert (w->window_end_bytepos >= 0);
0416532f 13587 IF_DEBUG (debug_method_add (w, "A"));
5f5c8ee5
GM
13588 }
13589 else if (last_text_row_at_end)
13590 {
ac90c44f
GM
13591 w->window_end_pos
13592 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
5f5c8ee5
GM
13593 w->window_end_bytepos
13594 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
ac90c44f
GM
13595 w->window_end_vpos
13596 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
23fca891 13597 xassert (w->window_end_bytepos >= 0);
0416532f 13598 IF_DEBUG (debug_method_add (w, "B"));
5f5c8ee5
GM
13599 }
13600 else if (last_text_row)
13601 {
13602 /* We have displayed either to the end of the window or at the
13603 end of the window, i.e. the last row with text is to be found
13604 in the desired matrix. */
ac90c44f
GM
13605 w->window_end_pos
13606 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
5f5c8ee5
GM
13607 w->window_end_bytepos
13608 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
13609 w->window_end_vpos
13610 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
23fca891 13611 xassert (w->window_end_bytepos >= 0);
5f5c8ee5
GM
13612 }
13613 else if (first_unchanged_at_end_row == NULL
13614 && last_text_row == NULL
13615 && last_text_row_at_end == NULL)
13616 {
13617 /* Displayed to end of window, but no line containing text was
13618 displayed. Lines were deleted at the end of the window. */
0416532f
GM
13619 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
13620 int vpos = XFASTINT (w->window_end_vpos);
13621 struct glyph_row *current_row = current_matrix->rows + vpos;
13622 struct glyph_row *desired_row = desired_matrix->rows + vpos;
13623
13624 for (row = NULL;
13625 row == NULL && vpos >= first_vpos;
13626 --vpos, --current_row, --desired_row)
13627 {
13628 if (desired_row->enabled_p)
13629 {
13630 if (desired_row->displays_text_p)
13631 row = desired_row;
13632 }
13633 else if (current_row->displays_text_p)
13634 row = current_row;
13635 }
12adba34 13636
0416532f 13637 xassert (row != NULL);
d88a79d4 13638 w->window_end_vpos = make_number (vpos + 1);
8c56a983
GM
13639 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13640 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
23fca891 13641 xassert (w->window_end_bytepos >= 0);
0416532f 13642 IF_DEBUG (debug_method_add (w, "C"));
5f5c8ee5
GM
13643 }
13644 else
13645 abort ();
ac90c44f 13646
8cdb267e
GM
13647#if 0 /* This leads to problems, for instance when the cursor is
13648 at ZV, and the cursor line displays no text. */
ac90c44f
GM
13649 /* Disable rows below what's displayed in the window. This makes
13650 debugging easier. */
13651 enable_glyph_matrix_rows (current_matrix,
13652 XFASTINT (w->window_end_vpos) + 1,
13653 bottom_vpos, 0);
8cdb267e 13654#endif
23e8bd86 13655
5f5c8ee5
GM
13656 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
13657 debug_end_vpos = XFASTINT (w->window_end_vpos));
12adba34 13658
5f5c8ee5
GM
13659 /* Record that display has not been completed. */
13660 w->window_end_valid = Qnil;
13661 w->desired_matrix->no_scrolling_p = 1;
ef121659 13662 return 3;
27d16f05
GM
13663
13664#undef GIVE_UP
12adba34 13665}
0f9c0ff0 13666
a2889657 13667
5f5c8ee5
GM
13668\f
13669/***********************************************************************
13670 More debugging support
13671 ***********************************************************************/
a2889657 13672
5f5c8ee5 13673#if GLYPH_DEBUG
a2889657 13674
eaaa65b0 13675void dump_glyph_row P_ ((struct glyph_row *, int, int));
9ab436b9 13676void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
e187cf71 13677void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
1c9241f5 13678
31b24551 13679
9ab436b9 13680/* Dump the contents of glyph matrix MATRIX on stderr.
ca26e1c8 13681
9ab436b9
GM
13682 GLYPHS 0 means don't show glyph contents.
13683 GLYPHS 1 means show glyphs in short form
13684 GLYPHS > 1 means show glyphs in long form. */
13685
13686void
13687dump_glyph_matrix (matrix, glyphs)
5f5c8ee5 13688 struct glyph_matrix *matrix;
9ab436b9 13689 int glyphs;
5f5c8ee5 13690{
efc63ef0 13691 int i;
5f5c8ee5 13692 for (i = 0; i < matrix->nrows; ++i)
eaaa65b0 13693 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
5f5c8ee5 13694}
31b24551 13695
68a37fa8 13696
e187cf71
GM
13697/* Dump contents of glyph GLYPH to stderr. ROW and AREA are
13698 the glyph row and area where the glyph comes from. */
13699
13700void
13701dump_glyph (row, glyph, area)
13702 struct glyph_row *row;
13703 struct glyph *glyph;
13704 int area;
13705{
13706 if (glyph->type == CHAR_GLYPH)
13707 {
13708 fprintf (stderr,
13709 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13710 glyph - row->glyphs[TEXT_AREA],
13711 'C',
13712 glyph->charpos,
13713 (BUFFERP (glyph->object)
13714 ? 'B'
13715 : (STRINGP (glyph->object)
13716 ? 'S'
13717 : '-')),
13718 glyph->pixel_width,
13719 glyph->u.ch,
13720 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
13721 ? glyph->u.ch
13722 : '.'),
13723 glyph->face_id,
13724 glyph->left_box_line_p,
13725 glyph->right_box_line_p);
13726 }
13727 else if (glyph->type == STRETCH_GLYPH)
13728 {
13729 fprintf (stderr,
13730 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13731 glyph - row->glyphs[TEXT_AREA],
13732 'S',
13733 glyph->charpos,
13734 (BUFFERP (glyph->object)
13735 ? 'B'
13736 : (STRINGP (glyph->object)
13737 ? 'S'
13738 : '-')),
13739 glyph->pixel_width,
13740 0,
13741 '.',
13742 glyph->face_id,
13743 glyph->left_box_line_p,
13744 glyph->right_box_line_p);
13745 }
13746 else if (glyph->type == IMAGE_GLYPH)
13747 {
13748 fprintf (stderr,
13749 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13750 glyph - row->glyphs[TEXT_AREA],
13751 'I',
13752 glyph->charpos,
13753 (BUFFERP (glyph->object)
13754 ? 'B'
13755 : (STRINGP (glyph->object)
13756 ? 'S'
13757 : '-')),
13758 glyph->pixel_width,
13759 glyph->u.img_id,
13760 '.',
13761 glyph->face_id,
13762 glyph->left_box_line_p,
13763 glyph->right_box_line_p);
13764 }
13765}
13766
13767
5f5c8ee5 13768/* Dump the contents of glyph row at VPOS in MATRIX to stderr.
9ab436b9
GM
13769 GLYPHS 0 means don't show glyph contents.
13770 GLYPHS 1 means show glyphs in short form
13771 GLYPHS > 1 means show glyphs in long form. */
a2889657 13772
5f5c8ee5 13773void
eaaa65b0
GM
13774dump_glyph_row (row, vpos, glyphs)
13775 struct glyph_row *row;
9ab436b9 13776 int vpos, glyphs;
5f5c8ee5 13777{
9ab436b9
GM
13778 if (glyphs != 1)
13779 {
b94c0d9c 13780 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
9ab436b9 13781 fprintf (stderr, "=======================================================================\n");
2311178e 13782
0ad38729 13783 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
33ea6c4d 13784%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 13785 vpos,
9ab436b9
GM
13786 MATRIX_ROW_START_CHARPOS (row),
13787 MATRIX_ROW_END_CHARPOS (row),
13788 row->used[TEXT_AREA],
13789 row->contains_overlapping_glyphs_p,
13790 row->enabled_p,
9ab436b9
GM
13791 row->truncated_on_left_p,
13792 row->truncated_on_right_p,
13793 row->overlay_arrow_p,
13794 row->continued_p,
13795 MATRIX_ROW_CONTINUATION_LINE_P (row),
13796 row->displays_text_p,
13797 row->ends_at_zv_p,
13798 row->fill_line_p,
13799 row->ends_in_middle_of_char_p,
13800 row->starts_in_middle_of_char_p,
b94c0d9c 13801 row->mouse_face_p,
9ab436b9
GM
13802 row->x,
13803 row->y,
13804 row->pixel_width,
13805 row->height,
13806 row->visible_height,
13807 row->ascent,
13808 row->phys_ascent);
33ea6c4d
GM
13809 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
13810 row->end.overlay_string_index,
13811 row->continuation_lines_width);
9ab436b9
GM
13812 fprintf (stderr, "%9d %5d\n",
13813 CHARPOS (row->start.string_pos),
13814 CHARPOS (row->end.string_pos));
13815 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
13816 row->end.dpvec_index);
13817 }
2311178e 13818
9ab436b9 13819 if (glyphs > 1)
bd66d1ba 13820 {
e187cf71
GM
13821 int area;
13822
13823 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13824 {
091f8878
GM
13825 struct glyph *glyph = row->glyphs[area];
13826 struct glyph *glyph_end = glyph + row->used[area];
2311178e 13827
e187cf71 13828 /* Glyph for a line end in text. */
091f8878 13829 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
e187cf71 13830 ++glyph_end;
2311178e 13831
e187cf71
GM
13832 if (glyph < glyph_end)
13833 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
2311178e 13834
e187cf71
GM
13835 for (; glyph < glyph_end; ++glyph)
13836 dump_glyph (row, glyph, area);
f7b4b63a 13837 }
f4faa47c 13838 }
9ab436b9
GM
13839 else if (glyphs == 1)
13840 {
e187cf71 13841 int area;
9ab436b9 13842
e187cf71 13843 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
9ab436b9 13844 {
e187cf71
GM
13845 char *s = (char *) alloca (row->used[area] + 1);
13846 int i;
13847
13848 for (i = 0; i < row->used[area]; ++i)
13849 {
13850 struct glyph *glyph = row->glyphs[area] + i;
13851 if (glyph->type == CHAR_GLYPH
13852 && glyph->u.ch < 0x80
13853 && glyph->u.ch >= ' ')
13854 s[i] = glyph->u.ch;
13855 else
13856 s[i] = '.';
13857 }
2311178e 13858
e187cf71
GM
13859 s[i] = '\0';
13860 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
13861 }
9ab436b9 13862 }
5f5c8ee5 13863}
f4faa47c 13864
a2889657 13865
5f5c8ee5
GM
13866DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
13867 Sdump_glyph_matrix, 0, 1, "p",
7ee72033 13868 doc: /* Dump the current matrix of the selected window to stderr.
228299fa
GM
13869Shows contents of glyph row structures. With non-nil
13870parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
7ee72033
MB
13871glyphs in short form, otherwise show glyphs in long form. */)
13872 (glyphs)
9ab436b9 13873 Lisp_Object glyphs;
5f5c8ee5
GM
13874{
13875 struct window *w = XWINDOW (selected_window);
13876 struct buffer *buffer = XBUFFER (w->buffer);
13877
13878 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
13879 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
13880 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
13881 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
13882 fprintf (stderr, "=============================================\n");
9ab436b9
GM
13883 dump_glyph_matrix (w->current_matrix,
13884 NILP (glyphs) ? 0 : XINT (glyphs));
5f5c8ee5
GM
13885 return Qnil;
13886}
1c2250c2 13887
1fca3fae 13888
7d4cc828
GM
13889DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
13890 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
13891 ()
13892{
13893 struct frame *f = XFRAME (selected_frame);
13894 dump_glyph_matrix (f->current_matrix, 1);
13895 return Qnil;
13896}
13897
13898
e9abc296 13899DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
7ee72033 13900 doc: /* Dump glyph row ROW to stderr.
228299fa
GM
13901GLYPH 0 means don't dump glyphs.
13902GLYPH 1 means dump glyphs in short form.
7ee72033
MB
13903GLYPH > 1 or omitted means dump glyphs in long form. */)
13904 (row, glyphs)
e9abc296 13905 Lisp_Object row, glyphs;
5f5c8ee5 13906{
eaaa65b0
GM
13907 struct glyph_matrix *matrix;
13908 int vpos;
2311178e 13909
b7826503 13910 CHECK_NUMBER (row);
eaaa65b0
GM
13911 matrix = XWINDOW (selected_window)->current_matrix;
13912 vpos = XINT (row);
13913 if (vpos >= 0 && vpos < matrix->nrows)
13914 dump_glyph_row (MATRIX_ROW (matrix, vpos),
13915 vpos,
13916 INTEGERP (glyphs) ? XINT (glyphs) : 2);
5f5c8ee5
GM
13917 return Qnil;
13918}
1fca3fae 13919
67481ae5 13920
b94c0d9c 13921DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
c2552f79 13922 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
228299fa
GM
13923GLYPH 0 means don't dump glyphs.
13924GLYPH 1 means dump glyphs in short form.
7ee72033
MB
13925GLYPH > 1 or omitted means dump glyphs in long form. */)
13926 (row, glyphs)
b94c0d9c 13927 Lisp_Object row, glyphs;
5f5c8ee5 13928{
886bd6f2 13929 struct frame *sf = SELECTED_FRAME ();
eaaa65b0
GM
13930 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
13931 int vpos;
2311178e 13932
b7826503 13933 CHECK_NUMBER (row);
eaaa65b0
GM
13934 vpos = XINT (row);
13935 if (vpos >= 0 && vpos < m->nrows)
13936 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
13937 INTEGERP (glyphs) ? XINT (glyphs) : 2);
5f5c8ee5
GM
13938 return Qnil;
13939}
ca26e1c8 13940
0f9c0ff0 13941
62397849 13942DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
7ee72033
MB
13943 doc: /* Toggle tracing of redisplay.
13944With ARG, turn tracing on if and only if ARG is positive. */)
13945 (arg)
62397849 13946 Lisp_Object arg;
5f5c8ee5 13947{
62397849
GM
13948 if (NILP (arg))
13949 trace_redisplay_p = !trace_redisplay_p;
13950 else
13951 {
13952 arg = Fprefix_numeric_value (arg);
13953 trace_redisplay_p = XINT (arg) > 0;
13954 }
2311178e 13955
5f5c8ee5
GM
13956 return Qnil;
13957}
bf9249e3
GM
13958
13959
f4a374a1 13960DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
e9757d41
SM
13961 doc: /* Like `format', but print result to stderr.
13962usage: (trace-to-stderr STRING &rest OBJECTS) */)
7ee72033 13963 (nargs, args)
f4a374a1
GM
13964 int nargs;
13965 Lisp_Object *args;
bf9249e3 13966{
f4a374a1 13967 Lisp_Object s = Fformat (nargs, args);
2051c264 13968 fprintf (stderr, "%s", SDATA (s));
bf9249e3
GM
13969 return Qnil;
13970}
2311178e 13971
5f5c8ee5 13972#endif /* GLYPH_DEBUG */
ca26e1c8 13973
ca26e1c8 13974
5f5c8ee5
GM
13975\f
13976/***********************************************************************
13977 Building Desired Matrix Rows
13978 ***********************************************************************/
a2889657 13979
5f5c8ee5
GM
13980/* Return a temporary glyph row holding the glyphs of an overlay
13981 arrow. Only used for non-window-redisplay windows. */
ca26e1c8 13982
5f5c8ee5 13983static struct glyph_row *
351b5434 13984get_overlay_arrow_glyph_row (w, overlay_arrow_string)
5f5c8ee5 13985 struct window *w;
351b5434 13986 Lisp_Object overlay_arrow_string;
5f5c8ee5
GM
13987{
13988 struct frame *f = XFRAME (WINDOW_FRAME (w));
13989 struct buffer *buffer = XBUFFER (w->buffer);
13990 struct buffer *old = current_buffer;
351b5434
KS
13991 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
13992 int arrow_len = SCHARS (overlay_arrow_string);
50f80c2f
KR
13993 const unsigned char *arrow_end = arrow_string + arrow_len;
13994 const unsigned char *p;
5f5c8ee5
GM
13995 struct it it;
13996 int multibyte_p;
13997 int n_glyphs_before;
13998
13999 set_buffer_temp (buffer);
14000 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14001 it.glyph_row->used[TEXT_AREA] = 0;
14002 SET_TEXT_POS (it.position, 0, 0);
14003
14004 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14005 p = arrow_string;
14006 while (p < arrow_end)
14007 {
14008 Lisp_Object face, ilisp;
2311178e 14009
5f5c8ee5
GM
14010 /* Get the next character. */
14011 if (multibyte_p)
4fdb80f2 14012 it.c = string_char_and_length (p, arrow_len, &it.len);
5f5c8ee5
GM
14013 else
14014 it.c = *p, it.len = 1;
14015 p += it.len;
2311178e 14016
5f5c8ee5 14017 /* Get its face. */
ac90c44f 14018 ilisp = make_number (p - arrow_string);
351b5434 14019 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
5f5c8ee5
GM
14020 it.face_id = compute_char_face (f, it.c, face);
14021
14022 /* Compute its width, get its glyphs. */
14023 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
337042a9 14024 SET_TEXT_POS (it.position, -1, -1);
5f5c8ee5
GM
14025 PRODUCE_GLYPHS (&it);
14026
14027 /* If this character doesn't fit any more in the line, we have
14028 to remove some glyphs. */
14029 if (it.current_x > it.last_visible_x)
14030 {
14031 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14032 break;
14033 }
14034 }
2311178e 14035
5f5c8ee5
GM
14036 set_buffer_temp (old);
14037 return it.glyph_row;
14038}
ca26e1c8 14039
b0a0fbda 14040
5f5c8ee5
GM
14041/* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14042 glyphs are only inserted for terminal frames since we can't really
14043 win with truncation glyphs when partially visible glyphs are
14044 involved. Which glyphs to insert is determined by
14045 produce_special_glyphs. */
67481ae5 14046
5f5c8ee5
GM
14047static void
14048insert_left_trunc_glyphs (it)
14049 struct it *it;
14050{
14051 struct it truncate_it;
14052 struct glyph *from, *end, *to, *toend;
14053
14054 xassert (!FRAME_WINDOW_P (it->f));
14055
14056 /* Get the truncation glyphs. */
14057 truncate_it = *it;
5f5c8ee5
GM
14058 truncate_it.current_x = 0;
14059 truncate_it.face_id = DEFAULT_FACE_ID;
14060 truncate_it.glyph_row = &scratch_glyph_row;
14061 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14062 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
6fc556fd 14063 truncate_it.object = make_number (0);
5f5c8ee5 14064 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
2311178e 14065
5f5c8ee5
GM
14066 /* Overwrite glyphs from IT with truncation glyphs. */
14067 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14068 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14069 to = it->glyph_row->glyphs[TEXT_AREA];
14070 toend = to + it->glyph_row->used[TEXT_AREA];
14071
14072 while (from < end)
14073 *to++ = *from++;
14074
37be86f2
KH
14075 /* There may be padding glyphs left over. Overwrite them too. */
14076 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14077 {
14078 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14079 while (from < end)
14080 *to++ = *from++;
14081 }
5f5c8ee5 14082
37be86f2
KH
14083 if (to > toend)
14084 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
5f5c8ee5 14085}
e0bfbde6 14086
e0bfbde6 14087
5f5c8ee5 14088/* Compute the pixel height and width of IT->glyph_row.
9c49d3d7 14089
5f5c8ee5
GM
14090 Most of the time, ascent and height of a display line will be equal
14091 to the max_ascent and max_height values of the display iterator
14092 structure. This is not the case if
67481ae5 14093
5f5c8ee5
GM
14094 1. We hit ZV without displaying anything. In this case, max_ascent
14095 and max_height will be zero.
1c9241f5 14096
5f5c8ee5
GM
14097 2. We have some glyphs that don't contribute to the line height.
14098 (The glyph row flag contributes_to_line_height_p is for future
14099 pixmap extensions).
f6fd109b 14100
5f5c8ee5
GM
14101 The first case is easily covered by using default values because in
14102 these cases, the line height does not really matter, except that it
14103 must not be zero. */
67481ae5 14104
5f5c8ee5
GM
14105static void
14106compute_line_metrics (it)
14107 struct it *it;
14108{
14109 struct glyph_row *row = it->glyph_row;
14110 int area, i;
1c2250c2 14111
5f5c8ee5
GM
14112 if (FRAME_WINDOW_P (it->f))
14113 {
75c5350a 14114 int i, min_y, max_y;
1c2250c2 14115
5f5c8ee5
GM
14116 /* The line may consist of one space only, that was added to
14117 place the cursor on it. If so, the row's height hasn't been
14118 computed yet. */
14119 if (row->height == 0)
14120 {
14121 if (it->max_ascent + it->max_descent == 0)
da8b7f4f 14122 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
5f5c8ee5
GM
14123 row->ascent = it->max_ascent;
14124 row->height = it->max_ascent + it->max_descent;
312246d1
GM
14125 row->phys_ascent = it->max_phys_ascent;
14126 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
5f5c8ee5 14127 }
2311178e 14128
5f5c8ee5
GM
14129 /* Compute the width of this line. */
14130 row->pixel_width = row->x;
14131 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14132 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14133
14134 xassert (row->pixel_width >= 0);
14135 xassert (row->ascent >= 0 && row->height > 0);
14136
312246d1
GM
14137 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14138 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14139
14140 /* If first line's physical ascent is larger than its logical
14141 ascent, use the physical ascent, and make the row taller.
14142 This makes accented characters fully visible. */
b28cb6ed 14143 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
312246d1
GM
14144 && row->phys_ascent > row->ascent)
14145 {
14146 row->height += row->phys_ascent - row->ascent;
14147 row->ascent = row->phys_ascent;
14148 }
14149
5f5c8ee5
GM
14150 /* Compute how much of the line is visible. */
14151 row->visible_height = row->height;
2311178e 14152
da8b7f4f
KS
14153 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14154 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
75c5350a
GM
14155
14156 if (row->y < min_y)
14157 row->visible_height -= min_y - row->y;
14158 if (row->y + row->height > max_y)
14159 row->visible_height -= row->y + row->height - max_y;
5f5c8ee5
GM
14160 }
14161 else
14162 {
14163 row->pixel_width = row->used[TEXT_AREA];
33ea6c4d
GM
14164 if (row->continued_p)
14165 row->pixel_width -= it->continuation_pixel_width;
14166 else if (row->truncated_on_right_p)
14167 row->pixel_width -= it->truncation_pixel_width;
312246d1
GM
14168 row->ascent = row->phys_ascent = 0;
14169 row->height = row->phys_height = row->visible_height = 1;
5f5c8ee5 14170 }
67481ae5 14171
5f5c8ee5
GM
14172 /* Compute a hash code for this row. */
14173 row->hash = 0;
14174 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14175 for (i = 0; i < row->used[area]; ++i)
14176 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14177 + row->glyphs[area][i].u.val
43d120d8
KH
14178 + row->glyphs[area][i].face_id
14179 + row->glyphs[area][i].padding_p
5f5c8ee5 14180 + (row->glyphs[area][i].type << 2));
a2889657 14181
5f5c8ee5 14182 it->max_ascent = it->max_descent = 0;
312246d1 14183 it->max_phys_ascent = it->max_phys_descent = 0;
5f5c8ee5 14184}
12adba34 14185
ca26e1c8 14186
5f5c8ee5 14187/* Append one space to the glyph row of iterator IT if doing a
03e35edc 14188 window-based redisplay. The space has the same face as
80c6cb1f 14189 IT->face_id. Value is non-zero if a space was added.
c6e89d6c
GM
14190
14191 This function is called to make sure that there is always one glyph
14192 at the end of a glyph row that the cursor can be set on under
14193 window-systems. (If there weren't such a glyph we would not know
14194 how wide and tall a box cursor should be displayed).
14195
14196 At the same time this space let's a nicely handle clearing to the
14197 end of the line if the row ends in italic text. */
ca26e1c8 14198
80c6cb1f 14199static int
4933c603 14200append_space_for_newline (it, default_face_p)
5f5c8ee5 14201 struct it *it;
4933c603 14202 int default_face_p;
5f5c8ee5
GM
14203{
14204 if (FRAME_WINDOW_P (it->f))
14205 {
14206 int n = it->glyph_row->used[TEXT_AREA];
ca26e1c8 14207
5f5c8ee5
GM
14208 if (it->glyph_row->glyphs[TEXT_AREA] + n
14209 < it->glyph_row->glyphs[1 + TEXT_AREA])
a2889657 14210 {
cafafe0b
GM
14211 /* Save some values that must not be changed.
14212 Must save IT->c and IT->len because otherwise
14213 ITERATOR_AT_END_P wouldn't work anymore after
03e35edc 14214 append_space_for_newline has been called. */
478d746b 14215 enum display_element_type saved_what = it->what;
cafafe0b
GM
14216 int saved_c = it->c, saved_len = it->len;
14217 int saved_x = it->current_x;
5f5c8ee5 14218 int saved_face_id = it->face_id;
cafafe0b 14219 struct text_pos saved_pos;
5f5c8ee5 14220 Lisp_Object saved_object;
980806b6 14221 struct face *face;
5f5c8ee5
GM
14222
14223 saved_object = it->object;
14224 saved_pos = it->position;
2311178e 14225
5f5c8ee5
GM
14226 it->what = IT_CHARACTER;
14227 bzero (&it->position, sizeof it->position);
6fc556fd 14228 it->object = make_number (0);
5f5c8ee5
GM
14229 it->c = ' ';
14230 it->len = 1;
5f5c8ee5 14231
4933c603
KS
14232 if (default_face_p)
14233 it->face_id = DEFAULT_FACE_ID;
14234 else if (it->face_before_selective_p)
4aad61f8 14235 it->face_id = it->saved_face_id;
980806b6
KH
14236 face = FACE_FROM_ID (it->f, it->face_id);
14237 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
1842fc1a 14238
5f5c8ee5 14239 PRODUCE_GLYPHS (it);
2311178e 14240
a9e5c932 14241 it->override_ascent = -1;
e893970b 14242 it->constrain_row_ascent_descent_p = 0;
5f5c8ee5
GM
14243 it->current_x = saved_x;
14244 it->object = saved_object;
14245 it->position = saved_pos;
14246 it->what = saved_what;
14247 it->face_id = saved_face_id;
cafafe0b
GM
14248 it->len = saved_len;
14249 it->c = saved_c;
80c6cb1f 14250 return 1;
5f5c8ee5
GM
14251 }
14252 }
80c6cb1f
GM
14253
14254 return 0;
5f5c8ee5 14255}
12adba34 14256
1842fc1a 14257
5f5c8ee5
GM
14258/* Extend the face of the last glyph in the text area of IT->glyph_row
14259 to the end of the display line. Called from display_line.
14260 If the glyph row is empty, add a space glyph to it so that we
14261 know the face to draw. Set the glyph row flag fill_line_p. */
2311178e 14262
5f5c8ee5
GM
14263static void
14264extend_face_to_end_of_line (it)
14265 struct it *it;
14266{
14267 struct face *face;
14268 struct frame *f = it->f;
1842fc1a 14269
5f5c8ee5
GM
14270 /* If line is already filled, do nothing. */
14271 if (it->current_x >= it->last_visible_x)
14272 return;
2311178e 14273
5f5c8ee5
GM
14274 /* Face extension extends the background and box of IT->face_id
14275 to the end of the line. If the background equals the background
4aad61f8
GM
14276 of the frame, we don't have to do anything. */
14277 if (it->face_before_selective_p)
14278 face = FACE_FROM_ID (it->f, it->saved_face_id);
14279 else
14280 face = FACE_FROM_ID (f, it->face_id);
2311178e 14281
5f5c8ee5
GM
14282 if (FRAME_WINDOW_P (f)
14283 && face->box == FACE_NO_BOX
14284 && face->background == FRAME_BACKGROUND_PIXEL (f)
14285 && !face->stipple)
14286 return;
1842fc1a 14287
5f5c8ee5
GM
14288 /* Set the glyph row flag indicating that the face of the last glyph
14289 in the text area has to be drawn to the end of the text area. */
14290 it->glyph_row->fill_line_p = 1;
545e04f6 14291
980806b6
KH
14292 /* If current character of IT is not ASCII, make sure we have the
14293 ASCII face. This will be automatically undone the next time
14294 get_next_display_element returns a multibyte character. Note
14295 that the character will always be single byte in unibyte text. */
14296 if (!SINGLE_BYTE_CHAR_P (it->c))
5f5c8ee5 14297 {
980806b6 14298 it->face_id = FACE_FOR_CHAR (f, face, 0);
5f5c8ee5 14299 }
545e04f6 14300
5f5c8ee5
GM
14301 if (FRAME_WINDOW_P (f))
14302 {
14303 /* If the row is empty, add a space with the current face of IT,
14304 so that we know which face to draw. */
14305 if (it->glyph_row->used[TEXT_AREA] == 0)
a2889657 14306 {
5f5c8ee5 14307 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
43d120d8 14308 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
5f5c8ee5 14309 it->glyph_row->used[TEXT_AREA] = 1;
a2889657 14310 }
5f5c8ee5
GM
14311 }
14312 else
14313 {
14314 /* Save some values that must not be changed. */
14315 int saved_x = it->current_x;
14316 struct text_pos saved_pos;
14317 Lisp_Object saved_object;
478d746b 14318 enum display_element_type saved_what = it->what;
4aad61f8 14319 int saved_face_id = it->face_id;
5f5c8ee5
GM
14320
14321 saved_object = it->object;
14322 saved_pos = it->position;
2311178e 14323
5f5c8ee5
GM
14324 it->what = IT_CHARACTER;
14325 bzero (&it->position, sizeof it->position);
6fc556fd 14326 it->object = make_number (0);
5f5c8ee5
GM
14327 it->c = ' ';
14328 it->len = 1;
4aad61f8 14329 it->face_id = face->id;
2311178e 14330
5f5c8ee5 14331 PRODUCE_GLYPHS (it);
2311178e 14332
5f5c8ee5
GM
14333 while (it->current_x <= it->last_visible_x)
14334 PRODUCE_GLYPHS (it);
2311178e 14335
5f5c8ee5
GM
14336 /* Don't count these blanks really. It would let us insert a left
14337 truncation glyph below and make us set the cursor on them, maybe. */
14338 it->current_x = saved_x;
14339 it->object = saved_object;
14340 it->position = saved_pos;
14341 it->what = saved_what;
4aad61f8 14342 it->face_id = saved_face_id;
5f5c8ee5
GM
14343 }
14344}
12adba34 14345
545e04f6 14346
5f5c8ee5
GM
14347/* Value is non-zero if text starting at CHARPOS in current_buffer is
14348 trailing whitespace. */
1c9241f5 14349
5f5c8ee5
GM
14350static int
14351trailing_whitespace_p (charpos)
14352 int charpos;
14353{
14354 int bytepos = CHAR_TO_BYTE (charpos);
14355 int c = 0;
7bbe686f 14356
5f5c8ee5
GM
14357 while (bytepos < ZV_BYTE
14358 && (c = FETCH_CHAR (bytepos),
14359 c == ' ' || c == '\t'))
14360 ++bytepos;
0d09d1e6 14361
8f897821
GM
14362 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14363 {
14364 if (bytepos != PT_BYTE)
14365 return 1;
14366 }
14367 return 0;
5f5c8ee5 14368}
31b24551 14369
545e04f6 14370
5f5c8ee5 14371/* Highlight trailing whitespace, if any, in ROW. */
545e04f6 14372
5f5c8ee5
GM
14373void
14374highlight_trailing_whitespace (f, row)
14375 struct frame *f;
14376 struct glyph_row *row;
14377{
14378 int used = row->used[TEXT_AREA];
2311178e 14379
5f5c8ee5
GM
14380 if (used)
14381 {
14382 struct glyph *start = row->glyphs[TEXT_AREA];
14383 struct glyph *glyph = start + used - 1;
14384
ade0bee1
GM
14385 /* Skip over glyphs inserted to display the cursor at the
14386 end of a line, for extending the face of the last glyph
14387 to the end of the line on terminals, and for truncation
14388 and continuation glyphs. */
65008712
GM
14389 while (glyph >= start
14390 && glyph->type == CHAR_GLYPH
65008712 14391 && INTEGERP (glyph->object))
5f5c8ee5
GM
14392 --glyph;
14393
14394 /* If last glyph is a space or stretch, and it's trailing
14395 whitespace, set the face of all trailing whitespace glyphs in
14396 IT->glyph_row to `trailing-whitespace'. */
14397 if (glyph >= start
14398 && BUFFERP (glyph->object)
14399 && (glyph->type == STRETCH_GLYPH
14400 || (glyph->type == CHAR_GLYPH
43d120d8 14401 && glyph->u.ch == ' '))
5f5c8ee5 14402 && trailing_whitespace_p (glyph->charpos))
545e04f6 14403 {
980806b6 14404 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
2311178e 14405
5f5c8ee5
GM
14406 while (glyph >= start
14407 && BUFFERP (glyph->object)
14408 && (glyph->type == STRETCH_GLYPH
14409 || (glyph->type == CHAR_GLYPH
43d120d8
KH
14410 && glyph->u.ch == ' ')))
14411 (glyph--)->face_id = face_id;
545e04f6 14412 }
a2889657 14413 }
5f5c8ee5 14414}
a2889657 14415
5fcbb24d 14416
cafafe0b 14417/* Value is non-zero if glyph row ROW in window W should be
0fd37545 14418 used to hold the cursor. */
cafafe0b
GM
14419
14420static int
14421cursor_row_p (w, row)
14422 struct window *w;
14423 struct glyph_row *row;
14424{
9a038881 14425 int cursor_row_p = 1;
2311178e 14426
cafafe0b
GM
14427 if (PT == MATRIX_ROW_END_CHARPOS (row))
14428 {
9a038881
GM
14429 /* If the row ends with a newline from a string, we don't want
14430 the cursor there (if the row is continued it doesn't end in a
14431 newline). */
14432 if (CHARPOS (row->end.string_pos) >= 0
14433 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14434 cursor_row_p = row->continued_p;
14435
14436 /* If the row ends at ZV, display the cursor at the end of that
14437 row instead of at the start of the row below. */
14438 else if (row->ends_at_zv_p)
14439 cursor_row_p = 1;
14440 else
14441 cursor_row_p = 0;
cafafe0b
GM
14442 }
14443
9a038881 14444 return cursor_row_p;
cafafe0b
GM
14445}
14446
14447
5f5c8ee5
GM
14448/* Construct the glyph row IT->glyph_row in the desired matrix of
14449 IT->w from text at the current position of IT. See dispextern.h
14450 for an overview of struct it. Value is non-zero if
14451 IT->glyph_row displays text, as opposed to a line displaying ZV
14452 only. */
2311178e 14453
5f5c8ee5
GM
14454static int
14455display_line (it)
14456 struct it *it;
14457{
14458 struct glyph_row *row = it->glyph_row;
351b5434
KS
14459 int overlay_arrow_bitmap;
14460 Lisp_Object overlay_arrow_string;
5f5c8ee5
GM
14461
14462 /* We always start displaying at hpos zero even if hscrolled. */
14463 xassert (it->hpos == 0 && it->current_x == 0);
a2889657 14464
cf4a901e
KS
14465 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14466 >= it->w->desired_matrix->nrows)
14467 {
14468 it->w->nrows_scale_factor++;
14469 fonts_changed_p = 1;
14470 return 0;
14471 }
12adba34 14472
5f5c8ee5
GM
14473 /* Is IT->w showing the region? */
14474 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12adba34 14475
5f5c8ee5
GM
14476 /* Clear the result glyph row and enable it. */
14477 prepare_desired_row (row);
12adba34 14478
5f5c8ee5 14479 row->y = it->current_y;
29b3ea63 14480 row->start = it->start;
5f5c8ee5
GM
14481 row->continuation_lines_width = it->continuation_lines_width;
14482 row->displays_text_p = 1;
91004049
GM
14483 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14484 it->starts_in_middle_of_char_p = 0;
5f5c8ee5
GM
14485
14486 /* Arrange the overlays nicely for our purposes. Usually, we call
14487 display_line on only one line at a time, in which case this
14488 can't really hurt too much, or we call it on lines which appear
14489 one after another in the buffer, in which case all calls to
14490 recenter_overlay_lists but the first will be pretty cheap. */
14491 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14492
5f5c8ee5
GM
14493 /* Move over display elements that are not visible because we are
14494 hscrolled. This may stop at an x-position < IT->first_visible_x
14495 if the first glyph is partially visible or if we hit a line end. */
14496 if (it->current_x < it->first_visible_x)
14497 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14498 MOVE_TO_POS | MOVE_TO_X);
14499
14500 /* Get the initial row height. This is either the height of the
14501 text hscrolled, if there is any, or zero. */
14502 row->ascent = it->max_ascent;
14503 row->height = it->max_ascent + it->max_descent;
312246d1
GM
14504 row->phys_ascent = it->max_phys_ascent;
14505 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
5f5c8ee5
GM
14506
14507 /* Loop generating characters. The loop is left with IT on the next
14508 character to display. */
14509 while (1)
14510 {
14511 int n_glyphs_before, hpos_before, x_before;
14512 int x, i, nglyphs;
ae26e27d 14513 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
b28cb6ed 14514
5f5c8ee5
GM
14515 /* Retrieve the next thing to display. Value is zero if end of
14516 buffer reached. */
14517 if (!get_next_display_element (it))
14518 {
14519 /* Maybe add a space at the end of this line that is used to
1b335865
GM
14520 display the cursor there under X. Set the charpos of the
14521 first glyph of blank lines not corresponding to any text
14522 to -1. */
6d925726
KS
14523#ifdef HAVE_WINDOW_SYSTEM
14524 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14525 row->exact_window_width_line_p = 1;
14526 else
14527#endif /* HAVE_WINDOW_SYSTEM */
4933c603 14528 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
1b335865 14529 || row->used[TEXT_AREA] == 0)
a2889657 14530 {
5f5c8ee5
GM
14531 row->glyphs[TEXT_AREA]->charpos = -1;
14532 row->displays_text_p = 0;
14533
2ad551af 14534 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
e6b70fd8
GM
14535 && (!MINI_WINDOW_P (it->w)
14536 || (minibuf_level && EQ (it->window, minibuf_window))))
5f5c8ee5 14537 row->indicate_empty_line_p = 1;
a2889657 14538 }
2311178e 14539
5f5c8ee5
GM
14540 it->continuation_lines_width = 0;
14541 row->ends_at_zv_p = 1;
14542 break;
a2889657 14543 }
a2889657 14544
5f5c8ee5
GM
14545 /* Now, get the metrics of what we want to display. This also
14546 generates glyphs in `row' (which is IT->glyph_row). */
14547 n_glyphs_before = row->used[TEXT_AREA];
14548 x = it->current_x;
e6819faf
GM
14549
14550 /* Remember the line height so far in case the next element doesn't
14551 fit on the line. */
14552 if (!it->truncate_lines_p)
14553 {
14554 ascent = it->max_ascent;
14555 descent = it->max_descent;
14556 phys_ascent = it->max_phys_ascent;
14557 phys_descent = it->max_phys_descent;
14558 }
2311178e 14559
5f5c8ee5 14560 PRODUCE_GLYPHS (it);
a2889657 14561
5f5c8ee5
GM
14562 /* If this display element was in marginal areas, continue with
14563 the next one. */
14564 if (it->area != TEXT_AREA)
a2889657 14565 {
5f5c8ee5
GM
14566 row->ascent = max (row->ascent, it->max_ascent);
14567 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
14568 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14569 row->phys_height = max (row->phys_height,
14570 it->max_phys_ascent + it->max_phys_descent);
cafafe0b 14571 set_iterator_to_next (it, 1);
5f5c8ee5
GM
14572 continue;
14573 }
5936754e 14574
5f5c8ee5
GM
14575 /* Does the display element fit on the line? If we truncate
14576 lines, we should draw past the right edge of the window. If
14577 we don't truncate, we want to stop so that we can display the
14578 continuation glyph before the right margin. If lines are
14579 continued, there are two possible strategies for characters
14580 resulting in more than 1 glyph (e.g. tabs): Display as many
14581 glyphs as possible in this line and leave the rest for the
14582 continuation line, or display the whole element in the next
14583 line. Original redisplay did the former, so we do it also. */
14584 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14585 hpos_before = it->hpos;
14586 x_before = x;
4dcd74e6 14587
4b41cebb 14588 if (/* Not a newline. */
4dcd74e6 14589 nglyphs > 0
202c1b5b 14590 /* Glyphs produced fit entirely in the line. */
4dcd74e6
GM
14591 && it->current_x < it->last_visible_x)
14592 {
37be86f2 14593 it->hpos += nglyphs;
5f5c8ee5
GM
14594 row->ascent = max (row->ascent, it->max_ascent);
14595 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
14596 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14597 row->phys_height = max (row->phys_height,
14598 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
14599 if (it->current_x - it->pixel_width < it->first_visible_x)
14600 row->x = x - it->first_visible_x;
14601 }
14602 else
14603 {
14604 int new_x;
14605 struct glyph *glyph;
2311178e 14606
5f5c8ee5 14607 for (i = 0; i < nglyphs; ++i, x = new_x)
b5bbc9a5 14608 {
5f5c8ee5
GM
14609 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14610 new_x = x + glyph->pixel_width;
14611
14612 if (/* Lines are continued. */
14613 !it->truncate_lines_p
14614 && (/* Glyph doesn't fit on the line. */
14615 new_x > it->last_visible_x
14616 /* Or it fits exactly on a window system frame. */
14617 || (new_x == it->last_visible_x
14618 && FRAME_WINDOW_P (it->f))))
a2889657 14619 {
5f5c8ee5 14620 /* End of a continued line. */
2311178e 14621
5f5c8ee5
GM
14622 if (it->hpos == 0
14623 || (new_x == it->last_visible_x
14624 && FRAME_WINDOW_P (it->f)))
14625 {
e6819faf
GM
14626 /* Current glyph is the only one on the line or
14627 fits exactly on the line. We must continue
14628 the line because we can't draw the cursor
14629 after the glyph. */
5f5c8ee5
GM
14630 row->continued_p = 1;
14631 it->current_x = new_x;
14632 it->continuation_lines_width += new_x;
14633 ++it->hpos;
14634 if (i == nglyphs - 1)
88e6b646
KS
14635 {
14636 set_iterator_to_next (it, 1);
7af0e8d7 14637#ifdef HAVE_WINDOW_SYSTEM
88e6b646
KS
14638 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14639 {
6d925726
KS
14640 if (!get_next_display_element (it))
14641 {
14642 row->exact_window_width_line_p = 1;
14643 it->continuation_lines_width = 0;
14644 row->continued_p = 0;
14645 row->ends_at_zv_p = 1;
14646 }
14647 else if (ITERATOR_AT_END_OF_LINE_P (it))
88e6b646
KS
14648 {
14649 row->continued_p = 0;
14650 row->exact_window_width_line_p = 1;
14651 }
14652 }
7af0e8d7 14653#endif /* HAVE_WINDOW_SYSTEM */
88e6b646 14654 }
5f5c8ee5 14655 }
3b3c4bf0
GM
14656 else if (CHAR_GLYPH_PADDING_P (*glyph)
14657 && !FRAME_WINDOW_P (it->f))
14658 {
14659 /* A padding glyph that doesn't fit on this line.
14660 This means the whole character doesn't fit
14661 on the line. */
14662 row->used[TEXT_AREA] = n_glyphs_before;
2311178e 14663
3b3c4bf0
GM
14664 /* Fill the rest of the row with continuation
14665 glyphs like in 20.x. */
14666 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
14667 < row->glyphs[1 + TEXT_AREA])
14668 produce_special_glyphs (it, IT_CONTINUATION);
2311178e 14669
3b3c4bf0
GM
14670 row->continued_p = 1;
14671 it->current_x = x_before;
14672 it->continuation_lines_width += x_before;
2311178e 14673
3b3c4bf0
GM
14674 /* Restore the height to what it was before the
14675 element not fitting on the line. */
14676 it->max_ascent = ascent;
14677 it->max_descent = descent;
14678 it->max_phys_ascent = phys_ascent;
14679 it->max_phys_descent = phys_descent;
14680 }
0df56f4d
GM
14681 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
14682 {
14683 /* A TAB that extends past the right edge of the
14684 window. This produces a single glyph on
14685 window system frames. We leave the glyph in
14686 this row and let it fill the row, but don't
14687 consume the TAB. */
14688 it->continuation_lines_width += it->last_visible_x;
14689 row->ends_in_middle_of_char_p = 1;
14690 row->continued_p = 1;
14691 glyph->pixel_width = it->last_visible_x - x;
14692 it->starts_in_middle_of_char_p = 1;
14693 }
5f5c8ee5 14694 else
5936754e 14695 {
0df56f4d
GM
14696 /* Something other than a TAB that draws past
14697 the right edge of the window. Restore
14698 positions to values before the element. */
5f5c8ee5 14699 row->used[TEXT_AREA] = n_glyphs_before + i;
2311178e 14700
5f5c8ee5
GM
14701 /* Display continuation glyphs. */
14702 if (!FRAME_WINDOW_P (it->f))
14703 produce_special_glyphs (it, IT_CONTINUATION);
14704 row->continued_p = 1;
653c329b 14705
0df56f4d 14706 it->continuation_lines_width += x;
2311178e 14707
91004049
GM
14708 if (nglyphs > 1 && i > 0)
14709 {
14710 row->ends_in_middle_of_char_p = 1;
14711 it->starts_in_middle_of_char_p = 1;
14712 }
2311178e 14713
e6819faf
GM
14714 /* Restore the height to what it was before the
14715 element not fitting on the line. */
14716 it->max_ascent = ascent;
14717 it->max_descent = descent;
14718 it->max_phys_ascent = phys_ascent;
14719 it->max_phys_descent = phys_descent;
5936754e 14720 }
e6819faf 14721
5f5c8ee5
GM
14722 break;
14723 }
14724 else if (new_x > it->first_visible_x)
14725 {
14726 /* Increment number of glyphs actually displayed. */
14727 ++it->hpos;
2311178e 14728
5f5c8ee5
GM
14729 if (x < it->first_visible_x)
14730 /* Glyph is partially visible, i.e. row starts at
14731 negative X position. */
14732 row->x = x - it->first_visible_x;
14733 }
14734 else
14735 {
14736 /* Glyph is completely off the left margin of the
14737 window. This should not happen because of the
72b8c434
RS
14738 move_it_in_display_line at the start of this
14739 function, unless the text display area of the
14740 window is empty. */
14741 xassert (it->first_visible_x <= it->last_visible_x);
a2889657 14742 }
a2889657 14743 }
2311178e 14744
5f5c8ee5
GM
14745 row->ascent = max (row->ascent, it->max_ascent);
14746 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
14747 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14748 row->phys_height = max (row->phys_height,
14749 it->max_phys_ascent + it->max_phys_descent);
2311178e 14750
5f5c8ee5 14751 /* End of this display line if row is continued. */
6d925726 14752 if (row->continued_p || row->ends_at_zv_p)
5f5c8ee5 14753 break;
a2889657 14754 }
a2889657 14755
88e6b646 14756 at_end_of_line:
5f5c8ee5
GM
14757 /* Is this a line end? If yes, we're also done, after making
14758 sure that a non-default face is extended up to the right
14759 margin of the window. */
14760 if (ITERATOR_AT_END_OF_LINE_P (it))
1c9241f5 14761 {
5f5c8ee5
GM
14762 int used_before = row->used[TEXT_AREA];
14763
e74fb0f7
GM
14764 row->ends_in_newline_from_string_p = STRINGP (it->object);
14765
7af0e8d7 14766#ifdef HAVE_WINDOW_SYSTEM
5f5c8ee5
GM
14767 /* Add a space at the end of the line that is used to
14768 display the cursor there. */
88e6b646 14769 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
4933c603 14770 append_space_for_newline (it, 0);
7af0e8d7 14771#endif /* HAVE_WINDOW_SYSTEM */
2311178e 14772
5f5c8ee5
GM
14773 /* Extend the face to the end of the line. */
14774 extend_face_to_end_of_line (it);
14775
14776 /* Make sure we have the position. */
14777 if (used_before == 0)
14778 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
2311178e 14779
5f5c8ee5 14780 /* Consume the line end. This skips over invisible lines. */
cafafe0b 14781 set_iterator_to_next (it, 1);
5f5c8ee5
GM
14782 it->continuation_lines_width = 0;
14783 break;
1c9241f5 14784 }
a2889657 14785
2311178e 14786 /* Proceed with next display element. Note that this skips
5f5c8ee5 14787 over lines invisible because of selective display. */
cafafe0b 14788 set_iterator_to_next (it, 1);
b1d1124b 14789
5f5c8ee5
GM
14790 /* If we truncate lines, we are done when the last displayed
14791 glyphs reach past the right margin of the window. */
14792 if (it->truncate_lines_p
14793 && (FRAME_WINDOW_P (it->f)
14794 ? (it->current_x >= it->last_visible_x)
14795 : (it->current_x > it->last_visible_x)))
75d13c64 14796 {
5f5c8ee5
GM
14797 /* Maybe add truncation glyphs. */
14798 if (!FRAME_WINDOW_P (it->f))
14799 {
a98b5ed9 14800 int i, n;
2311178e 14801
a98b5ed9
GM
14802 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14803 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14804 break;
14805
14806 for (n = row->used[TEXT_AREA]; i < n; ++i)
14807 {
14808 row->used[TEXT_AREA] = i;
14809 produce_special_glyphs (it, IT_TRUNCATION);
14810 }
5f5c8ee5 14811 }
7af0e8d7 14812#ifdef HAVE_WINDOW_SYSTEM
88e6b646
KS
14813 else
14814 {
14815 /* Don't truncate if we can overflow newline into fringe. */
14816 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14817 {
6d925726
KS
14818 if (!get_next_display_element (it))
14819 {
14820#ifdef HAVE_WINDOW_SYSTEM
14821 it->continuation_lines_width = 0;
14822 row->ends_at_zv_p = 1;
14823 row->exact_window_width_line_p = 1;
14824 break;
14825#endif /* HAVE_WINDOW_SYSTEM */
14826 }
88e6b646
KS
14827 if (ITERATOR_AT_END_OF_LINE_P (it))
14828 {
14829 row->exact_window_width_line_p = 1;
14830 goto at_end_of_line;
14831 }
14832 }
14833 }
7af0e8d7 14834#endif /* HAVE_WINDOW_SYSTEM */
2311178e 14835
5f5c8ee5
GM
14836 row->truncated_on_right_p = 1;
14837 it->continuation_lines_width = 0;
312246d1 14838 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
14839 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
14840 it->hpos = hpos_before;
14841 it->current_x = x_before;
14842 break;
75d13c64 14843 }
a2889657 14844 }
a2889657 14845
5f5c8ee5
GM
14846 /* If line is not empty and hscrolled, maybe insert truncation glyphs
14847 at the left window margin. */
14848 if (it->first_visible_x
14849 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
14850 {
14851 if (!FRAME_WINDOW_P (it->f))
14852 insert_left_trunc_glyphs (it);
14853 row->truncated_on_left_p = 1;
14854 }
a2889657 14855
5f5c8ee5
GM
14856 /* If the start of this line is the overlay arrow-position, then
14857 mark this glyph row as the one containing the overlay arrow.
14858 This is clearly a mess with variable size fonts. It would be
14859 better to let it be displayed like cursors under X. */
351b5434 14860 if (! overlay_arrow_seen
7d90b587
KS
14861 && (overlay_arrow_string
14862 = overlay_arrow_at_row (it->f, row, &overlay_arrow_bitmap),
351b5434 14863 !NILP (overlay_arrow_string)))
a2889657 14864 {
b46952ae 14865 /* Overlay arrow in window redisplay is a fringe bitmap. */
5f5c8ee5 14866 if (!FRAME_WINDOW_P (it->f))
c4628384 14867 {
351b5434 14868 struct glyph_row *arrow_row
7d90b587 14869 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
5f5c8ee5
GM
14870 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
14871 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
14872 struct glyph *p = row->glyphs[TEXT_AREA];
14873 struct glyph *p2, *end;
14874
14875 /* Copy the arrow glyphs. */
14876 while (glyph < arrow_end)
14877 *p++ = *glyph++;
14878
14879 /* Throw away padding glyphs. */
14880 p2 = p;
14881 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
14882 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
14883 ++p2;
14884 if (p2 > p)
212e4f87 14885 {
5f5c8ee5
GM
14886 while (p2 < end)
14887 *p++ = *p2++;
14888 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
c4628384 14889 }
c4628384 14890 }
2311178e 14891
a2889657 14892 overlay_arrow_seen = 1;
351b5434 14893 it->w->overlay_arrow_bitmap = overlay_arrow_bitmap;
5f5c8ee5 14894 row->overlay_arrow_p = 1;
a2889657
JB
14895 }
14896
5f5c8ee5
GM
14897 /* Compute pixel dimensions of this line. */
14898 compute_line_metrics (it);
14899
14900 /* Remember the position at which this line ends. */
14901 row->end = it->current;
14902
6d925726
KS
14903 /* Save fringe bitmaps in this row. */
14904 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
14905 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
14906 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
14907 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
14908
14909 it->left_user_fringe_bitmap = 0;
14910 it->left_user_fringe_face_id = 0;
14911 it->right_user_fringe_bitmap = 0;
14912 it->right_user_fringe_face_id = 0;
14913
173cbca8 14914 /* Maybe set the cursor. */
5f5c8ee5
GM
14915 if (it->w->cursor.vpos < 0
14916 && PT >= MATRIX_ROW_START_CHARPOS (row)
cafafe0b
GM
14917 && PT <= MATRIX_ROW_END_CHARPOS (row)
14918 && cursor_row_p (it->w, row))
14919 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
5f5c8ee5
GM
14920
14921 /* Highlight trailing whitespace. */
8f897821 14922 if (!NILP (Vshow_trailing_whitespace))
5f5c8ee5
GM
14923 highlight_trailing_whitespace (it->f, it->glyph_row);
14924
14925 /* Prepare for the next line. This line starts horizontally at (X
14926 HPOS) = (0 0). Vertical positions are incremented. As a
14927 convenience for the caller, IT->glyph_row is set to the next
14928 row to be used. */
14929 it->current_x = it->hpos = 0;
14930 it->current_y += row->height;
14931 ++it->vpos;
14932 ++it->glyph_row;
29b3ea63 14933 it->start = it->current;
5f5c8ee5 14934 return row->displays_text_p;
a2889657 14935}
5f5c8ee5
GM
14936
14937
a2889657 14938\f
5f5c8ee5
GM
14939/***********************************************************************
14940 Menu Bar
14941 ***********************************************************************/
14942
14943/* Redisplay the menu bar in the frame for window W.
14944
14945 The menu bar of X frames that don't have X toolkit support is
14946 displayed in a special window W->frame->menu_bar_window.
2311178e 14947
5f5c8ee5
GM
14948 The menu bar of terminal frames is treated specially as far as
14949 glyph matrices are concerned. Menu bar lines are not part of
14950 windows, so the update is done directly on the frame matrix rows
14951 for the menu bar. */
7ce2c095
RS
14952
14953static void
14954display_menu_bar (w)
14955 struct window *w;
14956{
5f5c8ee5
GM
14957 struct frame *f = XFRAME (WINDOW_FRAME (w));
14958 struct it it;
14959 Lisp_Object items;
8351baf2 14960 int i;
7ce2c095 14961
5f5c8ee5 14962 /* Don't do all this for graphical frames. */
dc937613 14963#ifdef HAVE_NTGUI
d129c4c2
KH
14964 if (!NILP (Vwindow_system))
14965 return;
dc937613 14966#endif
488dd4c4 14967#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
d3413a53 14968 if (FRAME_X_P (f))
7ce2c095 14969 return;
5f5c8ee5 14970#endif
e0f712ba 14971#ifdef MAC_OS
1a578e9b
AC
14972 if (FRAME_MAC_P (f))
14973 return;
14974#endif
5f5c8ee5
GM
14975
14976#ifdef USE_X_TOOLKIT
14977 xassert (!FRAME_WINDOW_P (f));
52377a47 14978 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
5f5c8ee5 14979 it.first_visible_x = 0;
da8b7f4f 14980 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
5f5c8ee5
GM
14981#else /* not USE_X_TOOLKIT */
14982 if (FRAME_WINDOW_P (f))
14983 {
14984 /* Menu bar lines are displayed in the desired matrix of the
14985 dummy window menu_bar_window. */
14986 struct window *menu_w;
14987 xassert (WINDOWP (f->menu_bar_window));
14988 menu_w = XWINDOW (f->menu_bar_window);
14989 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
52377a47 14990 MENU_FACE_ID);
5f5c8ee5 14991 it.first_visible_x = 0;
da8b7f4f 14992 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
5f5c8ee5
GM
14993 }
14994 else
14995 {
14996 /* This is a TTY frame, i.e. character hpos/vpos are used as
14997 pixel x/y. */
14998 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
52377a47 14999 MENU_FACE_ID);
5f5c8ee5 15000 it.first_visible_x = 0;
da8b7f4f 15001 it.last_visible_x = FRAME_COLS (f);
5f5c8ee5
GM
15002 }
15003#endif /* not USE_X_TOOLKIT */
15004
1862a24e
MB
15005 if (! mode_line_inverse_video)
15006 /* Force the menu-bar to be displayed in the default face. */
15007 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15008
5f5c8ee5
GM
15009 /* Clear all rows of the menu bar. */
15010 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
15011 {
15012 struct glyph_row *row = it.glyph_row + i;
15013 clear_glyph_row (row);
15014 row->enabled_p = 1;
15015 row->full_width_p = 1;
15016 }
7ce2c095 15017
5f5c8ee5
GM
15018 /* Display all items of the menu bar. */
15019 items = FRAME_MENU_BAR_ITEMS (it.f);
469937ac 15020 for (i = 0; i < XVECTOR (items)->size; i += 4)
7ce2c095 15021 {
5f5c8ee5
GM
15022 Lisp_Object string;
15023
15024 /* Stop at nil string. */
a61b7058 15025 string = AREF (items, i + 1);
8351baf2
RS
15026 if (NILP (string))
15027 break;
2d66ad19 15028
5f5c8ee5 15029 /* Remember where item was displayed. */
a61b7058 15030 AREF (items, i + 3) = make_number (it.hpos);
7ce2c095 15031
5f5c8ee5
GM
15032 /* Display the item, pad with one space. */
15033 if (it.current_x < it.last_visible_x)
15034 display_string (NULL, string, Qnil, 0, 0, &it,
2051c264 15035 SCHARS (string) + 1, 0, 0, -1);
7ce2c095
RS
15036 }
15037
2d66ad19 15038 /* Fill out the line with spaces. */
5f5c8ee5
GM
15039 if (it.current_x < it.last_visible_x)
15040 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
db6f348c 15041
5f5c8ee5
GM
15042 /* Compute the total height of the lines. */
15043 compute_line_metrics (&it);
7ce2c095 15044}
5f5c8ee5
GM
15045
15046
7ce2c095 15047\f
5f5c8ee5
GM
15048/***********************************************************************
15049 Mode Line
15050 ***********************************************************************/
15051
715e84c9
GM
15052/* Redisplay mode lines in the window tree whose root is WINDOW. If
15053 FORCE is non-zero, redisplay mode lines unconditionally.
15054 Otherwise, redisplay only mode lines that are garbaged. Value is
15055 the number of windows whose mode lines were redisplayed. */
a2889657 15056
715e84c9
GM
15057static int
15058redisplay_mode_lines (window, force)
15059 Lisp_Object window;
15060 int force;
15061{
15062 int nwindows = 0;
2311178e 15063
715e84c9
GM
15064 while (!NILP (window))
15065 {
15066 struct window *w = XWINDOW (window);
2311178e 15067
715e84c9
GM
15068 if (WINDOWP (w->hchild))
15069 nwindows += redisplay_mode_lines (w->hchild, force);
15070 else if (WINDOWP (w->vchild))
15071 nwindows += redisplay_mode_lines (w->vchild, force);
15072 else if (force
15073 || FRAME_GARBAGED_P (XFRAME (w->frame))
15074 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15075 {
715e84c9
GM
15076 struct text_pos lpoint;
15077 struct buffer *old = current_buffer;
15078
15079 /* Set the window's buffer for the mode line display. */
15080 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15081 set_buffer_internal_1 (XBUFFER (w->buffer));
2311178e 15082
715e84c9
GM
15083 /* Point refers normally to the selected window. For any
15084 other window, set up appropriate value. */
15085 if (!EQ (window, selected_window))
15086 {
15087 struct text_pos pt;
2311178e 15088
715e84c9
GM
15089 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15090 if (CHARPOS (pt) < BEGV)
15091 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15092 else if (CHARPOS (pt) > (ZV - 1))
15093 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15094 else
15095 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15096 }
15097
715e84c9
GM
15098 /* Display mode lines. */
15099 clear_glyph_matrix (w->desired_matrix);
15100 if (display_mode_lines (w))
15101 {
15102 ++nwindows;
15103 w->must_be_updated_p = 1;
15104 }
15105
15106 /* Restore old settings. */
715e84c9
GM
15107 set_buffer_internal_1 (old);
15108 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15109 }
15110
15111 window = w->next;
15112 }
15113
15114 return nwindows;
15115}
15116
15117
15118/* Display the mode and/or top line of window W. Value is the number
15119 of mode lines displayed. */
15120
15121static int
5f5c8ee5 15122display_mode_lines (w)
a2889657
JB
15123 struct window *w;
15124{
edd1e654 15125 Lisp_Object old_selected_window, old_selected_frame;
715e84c9 15126 int n = 0;
edd1e654
GM
15127
15128 old_selected_frame = selected_frame;
15129 selected_frame = w->frame;
15130 old_selected_window = selected_window;
15131 XSETWINDOW (selected_window, w);
2311178e 15132
5f5c8ee5 15133 /* These will be set while the mode line specs are processed. */
aa6d10fa 15134 line_number_displayed = 0;
155ef550 15135 w->column_number_displayed = Qnil;
aa6d10fa 15136
5f5c8ee5 15137 if (WINDOW_WANTS_MODELINE_P (w))
715e84c9 15138 {
c96e83bf 15139 struct window *sel_w = XWINDOW (old_selected_window);
f87c0a98 15140
96d2320f 15141 /* Select mode line face based on the real selected window. */
c96e83bf 15142 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
715e84c9
GM
15143 current_buffer->mode_line_format);
15144 ++n;
15145 }
2311178e 15146
045dee35 15147 if (WINDOW_WANTS_HEADER_LINE_P (w))
715e84c9
GM
15148 {
15149 display_mode_line (w, HEADER_LINE_FACE_ID,
15150 current_buffer->header_line_format);
15151 ++n;
15152 }
15153
edd1e654
GM
15154 selected_frame = old_selected_frame;
15155 selected_window = old_selected_window;
715e84c9 15156 return n;
5f5c8ee5 15157}
03b294dc 15158
03b294dc 15159
5f5c8ee5 15160/* Display mode or top line of window W. FACE_ID specifies which line
045dee35 15161 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
04612a64
GM
15162 FORMAT is the mode line format to display. Value is the pixel
15163 height of the mode line displayed. */
03b294dc 15164
04612a64 15165static int
5f5c8ee5
GM
15166display_mode_line (w, face_id, format)
15167 struct window *w;
15168 enum face_id face_id;
15169 Lisp_Object format;
15170{
15171 struct it it;
15172 struct face *face;
03b294dc 15173
5f5c8ee5
GM
15174 init_iterator (&it, w, -1, -1, NULL, face_id);
15175 prepare_desired_row (it.glyph_row);
15176
da06e4d7
KS
15177 it.glyph_row->mode_line_p = 1;
15178
1862a24e
MB
15179 if (! mode_line_inverse_video)
15180 /* Force the mode-line to be displayed in the default face. */
15181 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15182
5f5c8ee5
GM
15183 /* Temporarily make frame's keyboard the current kboard so that
15184 kboard-local variables in the mode_line_format will get the right
15185 values. */
15186 push_frame_kboard (it.f);
c53a1624 15187 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
5f5c8ee5 15188 pop_frame_kboard ();
a2889657 15189
5f5c8ee5
GM
15190 /* Fill up with spaces. */
15191 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
2311178e 15192
5f5c8ee5
GM
15193 compute_line_metrics (&it);
15194 it.glyph_row->full_width_p = 1;
5f5c8ee5
GM
15195 it.glyph_row->continued_p = 0;
15196 it.glyph_row->truncated_on_left_p = 0;
15197 it.glyph_row->truncated_on_right_p = 0;
15198
15199 /* Make a 3D mode-line have a shadow at its right end. */
15200 face = FACE_FROM_ID (it.f, face_id);
15201 extend_face_to_end_of_line (&it);
15202 if (face->box != FACE_NO_BOX)
d7eb09a0 15203 {
5f5c8ee5
GM
15204 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15205 + it.glyph_row->used[TEXT_AREA] - 1);
15206 last->right_box_line_p = 1;
d7eb09a0 15207 }
04612a64
GM
15208
15209 return it.glyph_row->height;
a2889657
JB
15210}
15211
0fcf414f
RS
15212/* Alist that caches the results of :propertize.
15213 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
15214Lisp_Object mode_line_proptrans_alist;
a2889657 15215
fec8f23e
KS
15216/* List of strings making up the mode-line. */
15217Lisp_Object mode_line_string_list;
15218
15219/* Base face property when building propertized mode line string. */
15220static Lisp_Object mode_line_string_face;
15221static Lisp_Object mode_line_string_face_prop;
15222
15223
5f5c8ee5
GM
15224/* Contribute ELT to the mode line for window IT->w. How it
15225 translates into text depends on its data type.
a2889657 15226
5f5c8ee5 15227 IT describes the display environment in which we display, as usual.
a2889657
JB
15228
15229 DEPTH is the depth in recursion. It is used to prevent
15230 infinite recursion here.
15231
5f5c8ee5
GM
15232 FIELD_WIDTH is the number of characters the display of ELT should
15233 occupy in the mode line, and PRECISION is the maximum number of
15234 characters to display from ELT's representation. See
b8523839 15235 display_string for details.
a2889657 15236
c53a1624
RS
15237 Returns the hpos of the end of the text generated by ELT.
15238
15239 PROPS is a property list to add to any string we encounter.
15240
7d0393cf 15241 If RISKY is nonzero, remove (disregard) any properties in any string
93da8435
SM
15242 we encounter, and ignore :eval and :propertize.
15243
15244 If the global variable `frame_title_ptr' is non-NULL, then the output
15245 is passed to `store_frame_title' instead of `display_string'. */
a2889657
JB
15246
15247static int
c53a1624 15248display_mode_element (it, depth, field_width, precision, elt, props, risky)
5f5c8ee5 15249 struct it *it;
a2889657 15250 int depth;
5f5c8ee5 15251 int field_width, precision;
0fcf414f 15252 Lisp_Object elt, props;
c53a1624 15253 int risky;
a2889657 15254{
5f5c8ee5 15255 int n = 0, field, prec;
0fcf414f 15256 int literal = 0;
5f5c8ee5 15257
a2889657 15258 tail_recurse:
1a89be1e
SM
15259 if (depth > 100)
15260 elt = build_string ("*too-deep*");
a2889657
JB
15261
15262 depth++;
15263
0220c518 15264 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
a2889657
JB
15265 {
15266 case Lisp_String:
15267 {
15268 /* A string: output it and check for %-constructs within it. */
5f5c8ee5 15269 unsigned char c;
50f80c2f 15270 const unsigned char *this, *lisp_string;
5f5c8ee5 15271
c53a1624 15272 if (!NILP (props) || risky)
0fcf414f
RS
15273 {
15274 Lisp_Object oprops, aelt;
15275 oprops = Ftext_properties_at (make_number (0), elt);
c53a1624
RS
15276
15277 if (NILP (Fequal (props, oprops)) || risky)
0fcf414f 15278 {
ae02e06a
RS
15279 /* If the starting string has properties,
15280 merge the specified ones onto the existing ones. */
c53a1624 15281 if (! NILP (oprops) && !risky)
ae02e06a
RS
15282 {
15283 Lisp_Object tem;
15284
15285 oprops = Fcopy_sequence (oprops);
15286 tem = props;
15287 while (CONSP (tem))
15288 {
15289 oprops = Fplist_put (oprops, XCAR (tem),
15290 XCAR (XCDR (tem)));
15291 tem = XCDR (XCDR (tem));
15292 }
15293 props = oprops;
15294 }
15295
0fcf414f
RS
15296 aelt = Fassoc (elt, mode_line_proptrans_alist);
15297 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
adb63af1
RS
15298 {
15299 mode_line_proptrans_alist
15300 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15301 elt = XCAR (aelt);
15302 }
0fcf414f
RS
15303 else
15304 {
adb63af1
RS
15305 Lisp_Object tem;
15306
0fcf414f 15307 elt = Fcopy_sequence (elt);
dc3b2c8b
SM
15308 Fset_text_properties (make_number (0), Flength (elt),
15309 props, elt);
adb63af1 15310 /* Add this item to mode_line_proptrans_alist. */
0fcf414f
RS
15311 mode_line_proptrans_alist
15312 = Fcons (Fcons (elt, props),
15313 mode_line_proptrans_alist);
adb63af1
RS
15314 /* Truncate mode_line_proptrans_alist
15315 to at most 50 elements. */
15316 tem = Fnthcdr (make_number (50),
15317 mode_line_proptrans_alist);
15318 if (! NILP (tem))
15319 XSETCDR (tem, Qnil);
0fcf414f
RS
15320 }
15321 }
15322 }
15323
2051c264 15324 this = SDATA (elt);
ae02e06a
RS
15325 lisp_string = this;
15326
0fcf414f
RS
15327 if (literal)
15328 {
15329 prec = precision - n;
15330 if (frame_title_ptr)
2051c264 15331 n += store_frame_title (SDATA (elt), -1, prec);
fec8f23e
KS
15332 else if (!NILP (mode_line_string_list))
15333 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
0fcf414f
RS
15334 else
15335 n += display_string (NULL, elt, Qnil, 0, 0, it,
15336 0, prec, 0, STRING_MULTIBYTE (elt));
15337
15338 break;
15339 }
15340
5f5c8ee5
GM
15341 while ((precision <= 0 || n < precision)
15342 && *this
15343 && (frame_title_ptr
fec8f23e 15344 || !NILP (mode_line_string_list)
5f5c8ee5 15345 || it->current_x < it->last_visible_x))
a2889657 15346 {
50f80c2f 15347 const unsigned char *last = this;
5f5c8ee5
GM
15348
15349 /* Advance to end of string or next format specifier. */
a2889657
JB
15350 while ((c = *this++) != '\0' && c != '%')
15351 ;
2311178e 15352
a2889657
JB
15353 if (this - 1 != last)
15354 {
5f5c8ee5
GM
15355 /* Output to end of string or up to '%'. Field width
15356 is length of string. Don't output more than
15357 PRECISION allows us. */
d26b89b8 15358 --this;
eaaa65b0
GM
15359
15360 prec = chars_in_text (last, this - last);
5f5c8ee5
GM
15361 if (precision > 0 && prec > precision - n)
15362 prec = precision - n;
2311178e 15363
d39b6696 15364 if (frame_title_ptr)
d26b89b8 15365 n += store_frame_title (last, 0, prec);
fec8f23e
KS
15366 else if (!NILP (mode_line_string_list))
15367 {
15368 int bytepos = last - lisp_string;
15369 int charpos = string_byte_to_char (elt, bytepos);
15370 n += store_mode_line_string (NULL,
15371 Fsubstring (elt, make_number (charpos),
15372 make_number (charpos + prec)),
15373 0, 0, 0, Qnil);
15374 }
d39b6696 15375 else
eaaa65b0
GM
15376 {
15377 int bytepos = last - lisp_string;
15378 int charpos = string_byte_to_char (elt, bytepos);
15379 n += display_string (NULL, elt, Qnil, 0, charpos,
ca77144d
GM
15380 it, 0, prec, 0,
15381 STRING_MULTIBYTE (elt));
eaaa65b0 15382 }
a2889657
JB
15383 }
15384 else /* c == '%' */
15385 {
50f80c2f 15386 const unsigned char *percent_position = this;
2311178e 15387
5f5c8ee5
GM
15388 /* Get the specified minimum width. Zero means
15389 don't pad. */
15390 field = 0;
a2889657 15391 while ((c = *this++) >= '0' && c <= '9')
5f5c8ee5 15392 field = field * 10 + c - '0';
a2889657 15393
5f5c8ee5
GM
15394 /* Don't pad beyond the total padding allowed. */
15395 if (field_width - n > 0 && field > field_width - n)
15396 field = field_width - n;
a2889657 15397
5f5c8ee5
GM
15398 /* Note that either PRECISION <= 0 or N < PRECISION. */
15399 prec = precision - n;
2311178e 15400
a2889657 15401 if (c == 'M')
5f5c8ee5 15402 n += display_mode_element (it, depth, field, prec,
c53a1624
RS
15403 Vglobal_mode_string, props,
15404 risky);
a2889657 15405 else if (c != 0)
d39b6696 15406 {
72f62cb5 15407 int multibyte;
ae02e06a
RS
15408 int bytepos, charpos;
15409 unsigned char *spec;
2311178e 15410
ae02e06a
RS
15411 bytepos = percent_position - lisp_string;
15412 charpos = (STRING_MULTIBYTE (elt)
15413 ? string_byte_to_char (elt, bytepos)
15414 : bytepos);
15415
15416 spec
72f62cb5
GM
15417 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15418
d39b6696 15419 if (frame_title_ptr)
5f5c8ee5 15420 n += store_frame_title (spec, field, prec);
fec8f23e
KS
15421 else if (!NILP (mode_line_string_list))
15422 {
15423 int len = strlen (spec);
15424 Lisp_Object tem = make_string (spec, len);
15425 props = Ftext_properties_at (make_number (charpos), elt);
15426 /* Should only keep face property in props */
15427 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15428 }
d39b6696 15429 else
5f5c8ee5 15430 {
ae02e06a 15431 int nglyphs_before, nwritten;
2311178e 15432
72f62cb5 15433 nglyphs_before = it->glyph_row->used[TEXT_AREA];
72f62cb5
GM
15434 nwritten = display_string (spec, Qnil, elt,
15435 charpos, 0, it,
15436 field, prec, 0,
15437 multibyte);
5f5c8ee5
GM
15438
15439 /* Assign to the glyphs written above the
15440 string where the `%x' came from, position
15441 of the `%'. */
15442 if (nwritten > 0)
15443 {
15444 struct glyph *glyph
15445 = (it->glyph_row->glyphs[TEXT_AREA]
15446 + nglyphs_before);
15447 int i;
15448
15449 for (i = 0; i < nwritten; ++i)
15450 {
15451 glyph[i].object = elt;
15452 glyph[i].charpos = charpos;
15453 }
2311178e 15454
5f5c8ee5
GM
15455 n += nwritten;
15456 }
15457 }
d39b6696 15458 }
b8523839
AS
15459 else /* c == 0 */
15460 break;
a2889657
JB
15461 }
15462 }
15463 }
15464 break;
15465
15466 case Lisp_Symbol:
15467 /* A symbol: process the value of the symbol recursively
15468 as if it appeared here directly. Avoid error if symbol void.
15469 Special case: if value of symbol is a string, output the string
15470 literally. */
15471 {
15472 register Lisp_Object tem;
c53a1624
RS
15473
15474 /* If the variable is not marked as risky to set
15475 then its contents are risky to use. */
15476 if (NILP (Fget (elt, Qrisky_local_variable)))
15477 risky = 1;
15478
a2889657 15479 tem = Fboundp (elt);
265a9e55 15480 if (!NILP (tem))
a2889657
JB
15481 {
15482 tem = Fsymbol_value (elt);
15483 /* If value is a string, output that string literally:
15484 don't check for % within it. */
e24c997d 15485 if (STRINGP (tem))
0fcf414f
RS
15486 literal = 1;
15487
15488 if (!EQ (tem, elt))
5f5c8ee5
GM
15489 {
15490 /* Give up right away for nil or t. */
15491 elt = tem;
15492 goto tail_recurse;
15493 }
a2889657
JB
15494 }
15495 }
15496 break;
15497
15498 case Lisp_Cons:
15499 {
15500 register Lisp_Object car, tem;
15501
0fcf414f
RS
15502 /* A cons cell: five distinct cases.
15503 If first element is :eval or :propertize, do something special.
a2889657
JB
15504 If first element is a string or a cons, process all the elements
15505 and effectively concatenate them.
15506 If first element is a negative number, truncate displaying cdr to
15507 at most that many characters. If positive, pad (with spaces)
15508 to at least that many characters.
15509 If first element is a symbol, process the cadr or caddr recursively
15510 according to whether the symbol's value is non-nil or nil. */
9472f927 15511 car = XCAR (elt);
0fcf414f 15512 if (EQ (car, QCeval))
5f5c8ee5
GM
15513 {
15514 /* An element of the form (:eval FORM) means evaluate FORM
15515 and use the result as mode line elements. */
0fcf414f 15516
c53a1624
RS
15517 if (risky)
15518 break;
15519
0fcf414f
RS
15520 if (CONSP (XCDR (elt)))
15521 {
15522 Lisp_Object spec;
15523 spec = safe_eval (XCAR (XCDR (elt)));
15524 n += display_mode_element (it, depth, field_width - n,
c53a1624
RS
15525 precision - n, spec, props,
15526 risky);
0fcf414f
RS
15527 }
15528 }
15529 else if (EQ (car, QCpropertize))
15530 {
c53a1624
RS
15531 /* An element of the form (:propertize ELT PROPS...)
15532 means display ELT but applying properties PROPS. */
15533
15534 if (risky)
15535 break;
15536
0fcf414f 15537 if (CONSP (XCDR (elt)))
c53a1624
RS
15538 n += display_mode_element (it, depth, field_width - n,
15539 precision - n, XCAR (XCDR (elt)),
15540 XCDR (XCDR (elt)), risky);
5f5c8ee5
GM
15541 }
15542 else if (SYMBOLP (car))
a2889657
JB
15543 {
15544 tem = Fboundp (car);
9472f927 15545 elt = XCDR (elt);
e24c997d 15546 if (!CONSP (elt))
a2889657
JB
15547 goto invalid;
15548 /* elt is now the cdr, and we know it is a cons cell.
15549 Use its car if CAR has a non-nil value. */
265a9e55 15550 if (!NILP (tem))
a2889657
JB
15551 {
15552 tem = Fsymbol_value (car);
265a9e55 15553 if (!NILP (tem))
9472f927
GM
15554 {
15555 elt = XCAR (elt);
15556 goto tail_recurse;
15557 }
a2889657
JB
15558 }
15559 /* Symbol's value is nil (or symbol is unbound)
15560 Get the cddr of the original list
15561 and if possible find the caddr and use that. */
9472f927 15562 elt = XCDR (elt);
265a9e55 15563 if (NILP (elt))
a2889657 15564 break;
e24c997d 15565 else if (!CONSP (elt))
a2889657 15566 goto invalid;
9472f927 15567 elt = XCAR (elt);
a2889657
JB
15568 goto tail_recurse;
15569 }
e24c997d 15570 else if (INTEGERP (car))
a2889657
JB
15571 {
15572 register int lim = XINT (car);
9472f927 15573 elt = XCDR (elt);
a2889657 15574 if (lim < 0)
5f5c8ee5
GM
15575 {
15576 /* Negative int means reduce maximum width. */
15577 if (precision <= 0)
15578 precision = -lim;
15579 else
15580 precision = min (precision, -lim);
15581 }
a2889657
JB
15582 else if (lim > 0)
15583 {
15584 /* Padding specified. Don't let it be more than
15585 current maximum. */
5f5c8ee5
GM
15586 if (precision > 0)
15587 lim = min (precision, lim);
15588
a2889657
JB
15589 /* If that's more padding than already wanted, queue it.
15590 But don't reduce padding already specified even if
15591 that is beyond the current truncation point. */
5f5c8ee5 15592 field_width = max (lim, field_width);
a2889657
JB
15593 }
15594 goto tail_recurse;
15595 }
e24c997d 15596 else if (STRINGP (car) || CONSP (car))
a2889657
JB
15597 {
15598 register int limit = 50;
5f5c8ee5
GM
15599 /* Limit is to protect against circular lists. */
15600 while (CONSP (elt)
15601 && --limit > 0
15602 && (precision <= 0 || n < precision))
a2889657 15603 {
5f5c8ee5 15604 n += display_mode_element (it, depth, field_width - n,
c53a1624
RS
15605 precision - n, XCAR (elt),
15606 props, risky);
9472f927 15607 elt = XCDR (elt);
a2889657
JB
15608 }
15609 }
15610 }
15611 break;
15612
15613 default:
15614 invalid:
1a89be1e
SM
15615 elt = build_string ("*invalid*");
15616 goto tail_recurse;
a2889657
JB
15617 }
15618
5f5c8ee5
GM
15619 /* Pad to FIELD_WIDTH. */
15620 if (field_width > 0 && n < field_width)
15621 {
15622 if (frame_title_ptr)
15623 n += store_frame_title ("", field_width - n, 0);
fec8f23e
KS
15624 else if (!NILP (mode_line_string_list))
15625 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
5f5c8ee5
GM
15626 else
15627 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
15628 0, 0, 0);
15629 }
2311178e 15630
5f5c8ee5 15631 return n;
a2889657 15632}
5f5c8ee5 15633
fec8f23e
KS
15634/* Store a mode-line string element in mode_line_string_list.
15635
15636 If STRING is non-null, display that C string. Otherwise, the Lisp
15637 string LISP_STRING is displayed.
15638
15639 FIELD_WIDTH is the minimum number of output glyphs to produce.
15640 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15641 with spaces. FIELD_WIDTH <= 0 means don't pad.
15642
15643 PRECISION is the maximum number of characters to output from
15644 STRING. PRECISION <= 0 means don't truncate the string.
15645
15646 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
15647 properties to the string.
15648
7d0393cf 15649 PROPS are the properties to add to the string.
fec8f23e
KS
15650 The mode_line_string_face face property is always added to the string.
15651 */
15652
15653static int store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
15654 char *string;
15655 Lisp_Object lisp_string;
15656 int copy_string;
15657 int field_width;
15658 int precision;
15659 Lisp_Object props;
15660{
15661 int len;
15662 int n = 0;
15663
15664 if (string != NULL)
15665 {
15666 len = strlen (string);
15667 if (precision > 0 && len > precision)
15668 len = precision;
15669 lisp_string = make_string (string, len);
15670 if (NILP (props))
15671 props = mode_line_string_face_prop;
15672 else if (!NILP (mode_line_string_face))
15673 {
15674 Lisp_Object face = Fplist_get (props, Qface);
15675 props = Fcopy_sequence (props);
15676 if (NILP (face))
15677 face = mode_line_string_face;
15678 else
15679 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15680 props = Fplist_put (props, Qface, face);
15681 }
15682 Fadd_text_properties (make_number (0), make_number (len),
15683 props, lisp_string);
15684 }
7d0393cf 15685 else
fec8f23e 15686 {
c8224325 15687 len = XFASTINT (Flength (lisp_string));
fec8f23e
KS
15688 if (precision > 0 && len > precision)
15689 {
15690 len = precision;
15691 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
15692 precision = -1;
15693 }
15694 if (!NILP (mode_line_string_face))
15695 {
15696 Lisp_Object face;
15697 if (NILP (props))
15698 props = Ftext_properties_at (make_number (0), lisp_string);
15699 face = Fplist_get (props, Qface);
15700 if (NILP (face))
15701 face = mode_line_string_face;
15702 else
15703 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15704 props = Fcons (Qface, Fcons (face, Qnil));
15705 if (copy_string)
15706 lisp_string = Fcopy_sequence (lisp_string);
15707 }
15708 if (!NILP (props))
15709 Fadd_text_properties (make_number (0), make_number (len),
15710 props, lisp_string);
15711 }
15712
15713 if (len > 0)
15714 {
15715 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
7d0393cf 15716 n += len;
fec8f23e
KS
15717 }
15718
15719 if (field_width > len)
15720 {
15721 field_width -= len;
15722 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
15723 if (!NILP (props))
15724 Fadd_text_properties (make_number (0), make_number (field_width),
15725 props, lisp_string);
15726 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
7d0393cf 15727 n += field_width;
fec8f23e
KS
15728 }
15729
15730 return n;
15731}
15732
5f5c8ee5 15733
8143e6ab 15734DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
fec8f23e 15735 0, 3, 0,
8143e6ab
KS
15736 doc: /* Return the mode-line of selected window as a string.
15737First optional arg FORMAT specifies a different format string (see
8b22c65a 15738`mode-line-format' for details) to use. If FORMAT is t, return
8143e6ab 15739the buffer's header-line. Second optional arg WINDOW specifies a
fec8f23e
KS
15740different window to use as the context for the formatting.
15741If third optional arg NO-PROPS is non-nil, string is not propertized. */)
15742 (format, window, no_props)
15743 Lisp_Object format, window, no_props;
8143e6ab
KS
15744{
15745 struct it it;
8143e6ab
KS
15746 int len;
15747 struct window *w;
15748 struct buffer *old_buffer = NULL;
fec8f23e 15749 enum face_id face_id = DEFAULT_FACE_ID;
8143e6ab
KS
15750
15751 if (NILP (window))
15752 window = selected_window;
15753 CHECK_WINDOW (window);
15754 w = XWINDOW (window);
15755 CHECK_BUFFER (w->buffer);
15756
15757 if (XBUFFER (w->buffer) != current_buffer)
15758 {
15759 old_buffer = current_buffer;
15760 set_buffer_internal_1 (XBUFFER (w->buffer));
15761 }
15762
15763 if (NILP (format) || EQ (format, Qt))
fec8f23e
KS
15764 {
15765 face_id = NILP (format)
15766 ? CURRENT_MODE_LINE_FACE_ID (w) :
15767 HEADER_LINE_FACE_ID;
15768 format = NILP (format)
15769 ? current_buffer->mode_line_format
15770 : current_buffer->header_line_format;
15771 }
15772
15773 init_iterator (&it, w, -1, -1, NULL, face_id);
8143e6ab 15774
fec8f23e
KS
15775 if (NILP (no_props))
15776 {
15777 mode_line_string_face =
15778 (face_id == MODE_LINE_FACE_ID ? Qmode_line :
15779 face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive :
15780 face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
8143e6ab 15781
fec8f23e
KS
15782 mode_line_string_face_prop =
15783 NILP (mode_line_string_face) ? Qnil :
15784 Fcons (Qface, Fcons (mode_line_string_face, Qnil));
15785
15786 /* We need a dummy last element in mode_line_string_list to
15787 indicate we are building the propertized mode-line string.
15788 Using mode_line_string_face_prop here GC protects it. */
7d0393cf 15789 mode_line_string_list =
fec8f23e
KS
15790 Fcons (mode_line_string_face_prop, Qnil);
15791 frame_title_ptr = NULL;
15792 }
15793 else
15794 {
15795 mode_line_string_face_prop = Qnil;
15796 mode_line_string_list = Qnil;
15797 frame_title_ptr = frame_title_buf;
15798 }
8143e6ab
KS
15799
15800 push_frame_kboard (it.f);
15801 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15802 pop_frame_kboard ();
15803
15804 if (old_buffer)
15805 set_buffer_internal_1 (old_buffer);
15806
fec8f23e
KS
15807 if (NILP (no_props))
15808 {
15809 Lisp_Object str;
15810 mode_line_string_list = Fnreverse (mode_line_string_list);
15811 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
15812 make_string ("", 0));
15813 mode_line_string_face_prop = Qnil;
15814 mode_line_string_list = Qnil;
15815 return str;
15816 }
15817
8143e6ab
KS
15818 len = frame_title_ptr - frame_title_buf;
15819 if (len > 0 && frame_title_ptr[-1] == '-')
15820 {
15821 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
15822 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
15823 ;
15824 frame_title_ptr += 3; /* restore last non-dash + two dashes */
15825 if (len > frame_title_ptr - frame_title_buf)
15826 len = frame_title_ptr - frame_title_buf;
15827 }
15828
15829 frame_title_ptr = NULL;
15830 return make_string (frame_title_buf, len);
15831}
15832
766525bc
RS
15833/* Write a null-terminated, right justified decimal representation of
15834 the positive integer D to BUF using a minimal field width WIDTH. */
15835
15836static void
15837pint2str (buf, width, d)
15838 register char *buf;
15839 register int width;
15840 register int d;
15841{
15842 register char *p = buf;
2311178e 15843
766525bc 15844 if (d <= 0)
5f5c8ee5 15845 *p++ = '0';
766525bc 15846 else
5f5c8ee5 15847 {
766525bc 15848 while (d > 0)
5f5c8ee5 15849 {
766525bc
RS
15850 *p++ = d % 10 + '0';
15851 d /= 10;
5f5c8ee5
GM
15852 }
15853 }
2311178e 15854
5f5c8ee5
GM
15855 for (width -= (int) (p - buf); width > 0; --width)
15856 *p++ = ' ';
766525bc
RS
15857 *p-- = '\0';
15858 while (p > buf)
5f5c8ee5 15859 {
766525bc
RS
15860 d = *buf;
15861 *buf++ = *p;
15862 *p-- = d;
5f5c8ee5 15863 }
766525bc
RS
15864}
15865
525b8b09
LK
15866/* Write a null-terminated, right justified decimal and "human
15867 readable" representation of the nonnegative integer D to BUF using
15868 a minimal field width WIDTH. D should be smaller than 999.5e24. */
15869
15870static const char power_letter[] =
15871 {
15872 0, /* not used */
15873 'k', /* kilo */
15874 'M', /* mega */
15875 'G', /* giga */
15876 'T', /* tera */
15877 'P', /* peta */
15878 'E', /* exa */
15879 'Z', /* zetta */
15880 'Y' /* yotta */
15881 };
15882
15883static void
15884pint2hrstr (buf, width, d)
15885 char *buf;
15886 int width;
15887 int d;
15888{
15889 /* We aim to represent the nonnegative integer D as
15890 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
15891 int quotient = d;
15892 int remainder = 0;
15893 /* -1 means: do not use TENTHS. */
15894 int tenths = -1;
15895 int exponent = 0;
15896
15897 /* Length of QUOTIENT.TENTHS as a string. */
15898 int length;
15899
15900 char * psuffix;
15901 char * p;
15902
15903 if (1000 <= quotient)
15904 {
15905 /* Scale to the appropriate EXPONENT. */
15906 do
15907 {
15908 remainder = quotient % 1000;
15909 quotient /= 1000;
15910 exponent++;
15911 }
15912 while (1000 <= quotient);
15913
15914 /* Round to nearest and decide whether to use TENTHS or not. */
15915 if (quotient <= 9)
15916 {
15917 tenths = remainder / 100;
15918 if (50 <= remainder % 100)
15919 if (tenths < 9)
15920 tenths++;
15921 else
15922 {
15923 quotient++;
15924 if (quotient == 10)
15925 tenths = -1;
15926 else
15927 tenths = 0;
15928 }
15929 }
15930 else
15931 if (500 <= remainder)
15932 if (quotient < 999)
15933 quotient++;
15934 else
15935 {
15936 quotient = 1;
15937 exponent++;
15938 tenths = 0;
15939 }
15940 }
15941
15942 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
15943 if (tenths == -1 && quotient <= 99)
15944 if (quotient <= 9)
15945 length = 1;
15946 else
15947 length = 2;
15948 else
15949 length = 3;
15950 p = psuffix = buf + max (width, length);
15951
15952 /* Print EXPONENT. */
15953 if (exponent)
15954 *psuffix++ = power_letter[exponent];
15955 *psuffix = '\0';
15956
15957 /* Print TENTHS. */
15958 if (tenths >= 0)
15959 {
15960 *--p = '0' + tenths;
15961 *--p = '.';
15962 }
15963
15964 /* Print QUOTIENT. */
15965 do
15966 {
15967 int digit = quotient % 10;
15968 *--p = '0' + digit;
15969 }
15970 while ((quotient /= 10) != 0);
15971
15972 /* Print leading spaces. */
15973 while (buf < p)
15974 *--p = ' ';
15975}
15976
5f5c8ee5 15977/* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
1c9241f5
KH
15978 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
15979 type of CODING_SYSTEM. Return updated pointer into BUF. */
15980
6693a99a 15981static unsigned char invalid_eol_type[] = "(*invalid*)";
d24715e8 15982
1c9241f5
KH
15983static char *
15984decode_mode_spec_coding (coding_system, buf, eol_flag)
15985 Lisp_Object coding_system;
15986 register char *buf;
15987 int eol_flag;
15988{
1e1078d6 15989 Lisp_Object val;
916848d8 15990 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
50f80c2f 15991 const unsigned char *eol_str;
302f2b38
EZ
15992 int eol_str_len;
15993 /* The EOL conversion we are using. */
15994 Lisp_Object eoltype;
1e1078d6 15995
4a09dee0 15996 val = Fget (coding_system, Qcoding_system);
085536c2 15997 eoltype = Qnil;
1c9241f5 15998
4a09dee0 15999 if (!VECTORP (val)) /* Not yet decided. */
1c9241f5 16000 {
916848d8
RS
16001 if (multibyte)
16002 *buf++ = '-';
21e989e3 16003 if (eol_flag)
302f2b38 16004 eoltype = eol_mnemonic_undecided;
1e1078d6 16005 /* Don't mention EOL conversion if it isn't decided. */
1c9241f5
KH
16006 }
16007 else
16008 {
1e1078d6
RS
16009 Lisp_Object eolvalue;
16010
16011 eolvalue = Fget (coding_system, Qeol_type);
16012
916848d8 16013 if (multibyte)
a61b7058 16014 *buf++ = XFASTINT (AREF (val, 1));
916848d8 16015
1c9241f5
KH
16016 if (eol_flag)
16017 {
1e1078d6
RS
16018 /* The EOL conversion that is normal on this system. */
16019
16020 if (NILP (eolvalue)) /* Not yet decided. */
16021 eoltype = eol_mnemonic_undecided;
16022 else if (VECTORP (eolvalue)) /* Not yet decided. */
16023 eoltype = eol_mnemonic_undecided;
16024 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
16025 eoltype = (XFASTINT (eolvalue) == 0
16026 ? eol_mnemonic_unix
16027 : (XFASTINT (eolvalue) == 1
16028 ? eol_mnemonic_dos : eol_mnemonic_mac));
302f2b38
EZ
16029 }
16030 }
2311178e 16031
302f2b38
EZ
16032 if (eol_flag)
16033 {
16034 /* Mention the EOL conversion if it is not the usual one. */
16035 if (STRINGP (eoltype))
16036 {
2051c264
GM
16037 eol_str = SDATA (eoltype);
16038 eol_str_len = SBYTES (eoltype);
302f2b38 16039 }
f30b3499
KH
16040 else if (INTEGERP (eoltype)
16041 && CHAR_VALID_P (XINT (eoltype), 0))
16042 {
50f80c2f
KR
16043 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16044 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16045 eol_str = tmp;
f30b3499 16046 }
302f2b38
EZ
16047 else
16048 {
16049 eol_str = invalid_eol_type;
16050 eol_str_len = sizeof (invalid_eol_type) - 1;
1c9241f5 16051 }
f30b3499 16052 bcopy (eol_str, buf, eol_str_len);
302f2b38 16053 buf += eol_str_len;
1c9241f5 16054 }
302f2b38 16055
1c9241f5
KH
16056 return buf;
16057}
16058
a2889657 16059/* Return a string for the output of a mode line %-spec for window W,
5f5c8ee5
GM
16060 generated by character C. PRECISION >= 0 means don't return a
16061 string longer than that value. FIELD_WIDTH > 0 means pad the
72f62cb5
GM
16062 string returned with spaces to that value. Return 1 in *MULTIBYTE
16063 if the result is multibyte text. */
a2889657 16064
11e82b76
JB
16065static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16066
a2889657 16067static char *
72f62cb5 16068decode_mode_spec (w, c, field_width, precision, multibyte)
a2889657 16069 struct window *w;
68c45bf0 16070 register int c;
5f5c8ee5 16071 int field_width, precision;
72f62cb5 16072 int *multibyte;
a2889657 16073{
0b67772d 16074 Lisp_Object obj;
5f5c8ee5
GM
16075 struct frame *f = XFRAME (WINDOW_FRAME (w));
16076 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
d39b6696 16077 struct buffer *b = XBUFFER (w->buffer);
a2889657 16078
0b67772d 16079 obj = Qnil;
72f62cb5 16080 *multibyte = 0;
a2889657
JB
16081
16082 switch (c)
16083 {
1af9f229
RS
16084 case '*':
16085 if (!NILP (b->read_only))
16086 return "%";
16087 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16088 return "*";
16089 return "-";
16090
16091 case '+':
16092 /* This differs from %* only for a modified read-only buffer. */
16093 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16094 return "*";
16095 if (!NILP (b->read_only))
16096 return "%";
16097 return "-";
16098
16099 case '&':
16100 /* This differs from %* in ignoring read-only-ness. */
16101 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16102 return "*";
16103 return "-";
16104
16105 case '%':
16106 return "%";
16107
2311178e 16108 case '[':
1af9f229
RS
16109 {
16110 int i;
16111 char *p;
16112
16113 if (command_loop_level > 5)
16114 return "[[[... ";
16115 p = decode_mode_spec_buf;
16116 for (i = 0; i < command_loop_level; i++)
16117 *p++ = '[';
16118 *p = 0;
16119 return decode_mode_spec_buf;
16120 }
16121
2311178e 16122 case ']':
1af9f229
RS
16123 {
16124 int i;
16125 char *p;
16126
16127 if (command_loop_level > 5)
16128 return " ...]]]";
16129 p = decode_mode_spec_buf;
16130 for (i = 0; i < command_loop_level; i++)
16131 *p++ = ']';
16132 *p = 0;
16133 return decode_mode_spec_buf;
16134 }
16135
16136 case '-':
16137 {
1af9f229 16138 register int i;
5f5c8ee5
GM
16139
16140 /* Let lots_of_dashes be a string of infinite length. */
fec8f23e
KS
16141 if (!NILP (mode_line_string_list))
16142 return "--";
5f5c8ee5
GM
16143 if (field_width <= 0
16144 || field_width > sizeof (lots_of_dashes))
1af9f229 16145 {
5f5c8ee5
GM
16146 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16147 decode_mode_spec_buf[i] = '-';
16148 decode_mode_spec_buf[i] = '\0';
16149 return decode_mode_spec_buf;
1af9f229 16150 }
5f5c8ee5
GM
16151 else
16152 return lots_of_dashes;
1af9f229
RS
16153 }
16154
2311178e 16155 case 'b':
d39b6696 16156 obj = b->name;
a2889657
JB
16157 break;
16158
1af9f229
RS
16159 case 'c':
16160 {
2311178e 16161 int col = (int) current_column (); /* iftc */
ac90c44f 16162 w->column_number_displayed = make_number (col);
5f5c8ee5 16163 pint2str (decode_mode_spec_buf, field_width, col);
1af9f229
RS
16164 return decode_mode_spec_buf;
16165 }
16166
16167 case 'F':
16168 /* %F displays the frame name. */
5f5c8ee5 16169 if (!NILP (f->title))
2051c264 16170 return (char *) SDATA (f->title);
fd8ff63d 16171 if (f->explicit_name || ! FRAME_WINDOW_P (f))
2051c264 16172 return (char *) SDATA (f->name);
9c6da96f 16173 return "Emacs";
1af9f229 16174
2311178e 16175 case 'f':
d39b6696 16176 obj = b->filename;
a2889657
JB
16177 break;
16178
525b8b09
LK
16179 case 'i':
16180 {
16181 int size = ZV - BEGV;
16182 pint2str (decode_mode_spec_buf, field_width, size);
16183 return decode_mode_spec_buf;
16184 }
16185
16186 case 'I':
16187 {
16188 int size = ZV - BEGV;
16189 pint2hrstr (decode_mode_spec_buf, field_width, size);
16190 return decode_mode_spec_buf;
16191 }
16192
aa6d10fa
RS
16193 case 'l':
16194 {
12adba34
RS
16195 int startpos = XMARKER (w->start)->charpos;
16196 int startpos_byte = marker_byte_position (w->start);
16197 int line, linepos, linepos_byte, topline;
aa6d10fa 16198 int nlines, junk;
da8b7f4f 16199 int height = WINDOW_TOTAL_LINES (w);
aa6d10fa 16200
2311178e 16201 /* If we decided that this buffer isn't suitable for line numbers,
aa6d10fa
RS
16202 don't forget that too fast. */
16203 if (EQ (w->base_line_pos, w->buffer))
766525bc 16204 goto no_value;
5300fd39
RS
16205 /* But do forget it, if the window shows a different buffer now. */
16206 else if (BUFFERP (w->base_line_pos))
16207 w->base_line_pos = Qnil;
aa6d10fa
RS
16208
16209 /* If the buffer is very big, don't waste time. */
37d034d3 16210 if (INTEGERP (Vline_number_display_limit)
090703f4 16211 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
aa6d10fa
RS
16212 {
16213 w->base_line_pos = Qnil;
16214 w->base_line_number = Qnil;
766525bc 16215 goto no_value;
aa6d10fa
RS
16216 }
16217
16218 if (!NILP (w->base_line_number)
16219 && !NILP (w->base_line_pos)
12adba34 16220 && XFASTINT (w->base_line_pos) <= startpos)
aa6d10fa
RS
16221 {
16222 line = XFASTINT (w->base_line_number);
16223 linepos = XFASTINT (w->base_line_pos);
12adba34 16224 linepos_byte = buf_charpos_to_bytepos (b, linepos);
aa6d10fa
RS
16225 }
16226 else
16227 {
16228 line = 1;
d39b6696 16229 linepos = BUF_BEGV (b);
12adba34 16230 linepos_byte = BUF_BEGV_BYTE (b);
aa6d10fa
RS
16231 }
16232
16233 /* Count lines from base line to window start position. */
12adba34
RS
16234 nlines = display_count_lines (linepos, linepos_byte,
16235 startpos_byte,
16236 startpos, &junk);
aa6d10fa
RS
16237
16238 topline = nlines + line;
16239
16240 /* Determine a new base line, if the old one is too close
16241 or too far away, or if we did not have one.
16242 "Too close" means it's plausible a scroll-down would
16243 go back past it. */
d39b6696 16244 if (startpos == BUF_BEGV (b))
aa6d10fa 16245 {
ac90c44f
GM
16246 w->base_line_number = make_number (topline);
16247 w->base_line_pos = make_number (BUF_BEGV (b));
aa6d10fa
RS
16248 }
16249 else if (nlines < height + 25 || nlines > height * 3 + 50
d39b6696 16250 || linepos == BUF_BEGV (b))
aa6d10fa 16251 {
d39b6696 16252 int limit = BUF_BEGV (b);
12adba34 16253 int limit_byte = BUF_BEGV_BYTE (b);
aa6d10fa 16254 int position;
5d121aec 16255 int distance = (height * 2 + 30) * line_number_display_limit_width;
aa6d10fa
RS
16256
16257 if (startpos - distance > limit)
12adba34
RS
16258 {
16259 limit = startpos - distance;
16260 limit_byte = CHAR_TO_BYTE (limit);
16261 }
aa6d10fa 16262
12adba34
RS
16263 nlines = display_count_lines (startpos, startpos_byte,
16264 limit_byte,
16265 - (height * 2 + 30),
aa6d10fa 16266 &position);
2311178e 16267 /* If we couldn't find the lines we wanted within
5d121aec 16268 line_number_display_limit_width chars per line,
aa6d10fa 16269 give up on line numbers for this window. */
12adba34 16270 if (position == limit_byte && limit == startpos - distance)
aa6d10fa
RS
16271 {
16272 w->base_line_pos = w->buffer;
16273 w->base_line_number = Qnil;
766525bc 16274 goto no_value;
aa6d10fa
RS
16275 }
16276
ac90c44f
GM
16277 w->base_line_number = make_number (topline - nlines);
16278 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
aa6d10fa
RS
16279 }
16280
16281 /* Now count lines from the start pos to point. */
12adba34
RS
16282 nlines = display_count_lines (startpos, startpos_byte,
16283 PT_BYTE, PT, &junk);
aa6d10fa
RS
16284
16285 /* Record that we did display the line number. */
16286 line_number_displayed = 1;
16287
16288 /* Make the string to show. */
5f5c8ee5 16289 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
aa6d10fa 16290 return decode_mode_spec_buf;
766525bc
RS
16291 no_value:
16292 {
16293 char* p = decode_mode_spec_buf;
5f5c8ee5
GM
16294 int pad = field_width - 2;
16295 while (pad-- > 0)
16296 *p++ = ' ';
16297 *p++ = '?';
b357b9d4
KR
16298 *p++ = '?';
16299 *p = '\0';
766525bc
RS
16300 return decode_mode_spec_buf;
16301 }
aa6d10fa
RS
16302 }
16303 break;
16304
2311178e 16305 case 'm':
d39b6696 16306 obj = b->mode_name;
a2889657
JB
16307 break;
16308
16309 case 'n':
d39b6696 16310 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
a2889657
JB
16311 return " Narrow";
16312 break;
16313
a2889657
JB
16314 case 'p':
16315 {
16316 int pos = marker_position (w->start);
d39b6696 16317 int total = BUF_ZV (b) - BUF_BEGV (b);
a2889657 16318
d39b6696 16319 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
a2889657 16320 {
d39b6696 16321 if (pos <= BUF_BEGV (b))
a2889657
JB
16322 return "All";
16323 else
16324 return "Bottom";
16325 }
d39b6696 16326 else if (pos <= BUF_BEGV (b))
a2889657
JB
16327 return "Top";
16328 else
16329 {
3c7d31b9
RS
16330 if (total > 1000000)
16331 /* Do it differently for a large value, to avoid overflow. */
16332 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16333 else
16334 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
a2889657
JB
16335 /* We can't normally display a 3-digit number,
16336 so get us a 2-digit number that is close. */
16337 if (total == 100)
16338 total = 99;
16339 sprintf (decode_mode_spec_buf, "%2d%%", total);
16340 return decode_mode_spec_buf;
16341 }
16342 }
16343
8ffcb79f
RS
16344 /* Display percentage of size above the bottom of the screen. */
16345 case 'P':
16346 {
16347 int toppos = marker_position (w->start);
d39b6696
KH
16348 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16349 int total = BUF_ZV (b) - BUF_BEGV (b);
8ffcb79f 16350
d39b6696 16351 if (botpos >= BUF_ZV (b))
8ffcb79f 16352 {
d39b6696 16353 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
16354 return "All";
16355 else
16356 return "Bottom";
16357 }
16358 else
16359 {
3c7d31b9
RS
16360 if (total > 1000000)
16361 /* Do it differently for a large value, to avoid overflow. */
16362 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16363 else
16364 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
8ffcb79f
RS
16365 /* We can't normally display a 3-digit number,
16366 so get us a 2-digit number that is close. */
16367 if (total == 100)
16368 total = 99;
d39b6696 16369 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
16370 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
16371 else
16372 sprintf (decode_mode_spec_buf, "%2d%%", total);
16373 return decode_mode_spec_buf;
16374 }
16375 }
16376
1af9f229
RS
16377 case 's':
16378 /* status of process */
16379 obj = Fget_buffer_process (w->buffer);
16380 if (NILP (obj))
16381 return "no process";
16382#ifdef subprocesses
16383 obj = Fsymbol_name (Fprocess_status (obj));
16384#endif
16385 break;
d39b6696 16386
1af9f229
RS
16387 case 't': /* indicate TEXT or BINARY */
16388#ifdef MODE_LINE_BINARY_TEXT
16389 return MODE_LINE_BINARY_TEXT (b);
16390#else
16391 return "T";
16392#endif
1c9241f5
KH
16393
16394 case 'z':
16395 /* coding-system (not including end-of-line format) */
16396 case 'Z':
16397 /* coding-system (including end-of-line type) */
16398 {
16399 int eol_flag = (c == 'Z');
539b4d41 16400 char *p = decode_mode_spec_buf;
1c9241f5 16401
d30e754b 16402 if (! FRAME_WINDOW_P (f))
1c9241f5 16403 {
11c52c4f
RS
16404 /* No need to mention EOL here--the terminal never needs
16405 to do EOL conversion. */
16406 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
16407 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
1c9241f5 16408 }
f13c925f 16409 p = decode_mode_spec_coding (b->buffer_file_coding_system,
539b4d41 16410 p, eol_flag);
f13c925f 16411
11c52c4f 16412#if 0 /* This proves to be annoying; I think we can do without. -- rms. */
1c9241f5
KH
16413#ifdef subprocesses
16414 obj = Fget_buffer_process (Fcurrent_buffer ());
16415 if (PROCESSP (obj))
16416 {
16417 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16418 p, eol_flag);
16419 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16420 p, eol_flag);
16421 }
16422#endif /* subprocesses */
11c52c4f 16423#endif /* 0 */
1c9241f5
KH
16424 *p = 0;
16425 return decode_mode_spec_buf;
16426 }
a2889657 16427 }
d39b6696 16428
e24c997d 16429 if (STRINGP (obj))
72f62cb5
GM
16430 {
16431 *multibyte = STRING_MULTIBYTE (obj);
2051c264 16432 return (char *) SDATA (obj);
72f62cb5 16433 }
a2889657
JB
16434 else
16435 return "";
16436}
5f5c8ee5
GM
16437
16438
12adba34
RS
16439/* Count up to COUNT lines starting from START / START_BYTE.
16440 But don't go beyond LIMIT_BYTE.
16441 Return the number of lines thus found (always nonnegative).
59b49f63 16442
12adba34 16443 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
59b49f63
RS
16444
16445static int
12adba34
RS
16446display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16447 int start, start_byte, limit_byte, count;
16448 int *byte_pos_ptr;
59b49f63 16449{
59b49f63
RS
16450 register unsigned char *cursor;
16451 unsigned char *base;
16452
16453 register int ceiling;
16454 register unsigned char *ceiling_addr;
12adba34 16455 int orig_count = count;
59b49f63
RS
16456
16457 /* If we are not in selective display mode,
16458 check only for newlines. */
12adba34
RS
16459 int selective_display = (!NILP (current_buffer->selective_display)
16460 && !INTEGERP (current_buffer->selective_display));
59b49f63
RS
16461
16462 if (count > 0)
12adba34
RS
16463 {
16464 while (start_byte < limit_byte)
16465 {
16466 ceiling = BUFFER_CEILING_OF (start_byte);
16467 ceiling = min (limit_byte - 1, ceiling);
16468 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16469 base = (cursor = BYTE_POS_ADDR (start_byte));
16470 while (1)
16471 {
16472 if (selective_display)
16473 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16474 ;
16475 else
16476 while (*cursor != '\n' && ++cursor != ceiling_addr)
16477 ;
16478
16479 if (cursor != ceiling_addr)
16480 {
16481 if (--count == 0)
16482 {
16483 start_byte += cursor - base + 1;
16484 *byte_pos_ptr = start_byte;
16485 return orig_count;
16486 }
16487 else
16488 if (++cursor == ceiling_addr)
16489 break;
16490 }
16491 else
16492 break;
16493 }
16494 start_byte += cursor - base;
16495 }
16496 }
59b49f63
RS
16497 else
16498 {
12adba34
RS
16499 while (start_byte > limit_byte)
16500 {
16501 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16502 ceiling = max (limit_byte, ceiling);
16503 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16504 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
59b49f63
RS
16505 while (1)
16506 {
12adba34
RS
16507 if (selective_display)
16508 while (--cursor != ceiling_addr
16509 && *cursor != '\n' && *cursor != 015)
16510 ;
16511 else
16512 while (--cursor != ceiling_addr && *cursor != '\n')
16513 ;
16514
59b49f63
RS
16515 if (cursor != ceiling_addr)
16516 {
16517 if (++count == 0)
16518 {
12adba34
RS
16519 start_byte += cursor - base + 1;
16520 *byte_pos_ptr = start_byte;
16521 /* When scanning backwards, we should
16522 not count the newline posterior to which we stop. */
16523 return - orig_count - 1;
59b49f63
RS
16524 }
16525 }
16526 else
16527 break;
16528 }
12adba34
RS
16529 /* Here we add 1 to compensate for the last decrement
16530 of CURSOR, which took it past the valid range. */
16531 start_byte += cursor - base + 1;
59b49f63
RS
16532 }
16533 }
16534
12adba34 16535 *byte_pos_ptr = limit_byte;
aa6d10fa 16536
12adba34
RS
16537 if (count < 0)
16538 return - orig_count + count;
16539 return orig_count - count;
aa6d10fa 16540
12adba34 16541}
a2889657 16542
a2889657 16543
5f5c8ee5
GM
16544\f
16545/***********************************************************************
16546 Displaying strings
16547 ***********************************************************************/
278feba9 16548
5f5c8ee5 16549/* Display a NUL-terminated string, starting with index START.
a3788d53 16550
5f5c8ee5
GM
16551 If STRING is non-null, display that C string. Otherwise, the Lisp
16552 string LISP_STRING is displayed.
a2889657 16553
5f5c8ee5
GM
16554 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16555 FACE_STRING. Display STRING or LISP_STRING with the face at
16556 FACE_STRING_POS in FACE_STRING:
a2889657 16557
5f5c8ee5
GM
16558 Display the string in the environment given by IT, but use the
16559 standard display table, temporarily.
a3788d53 16560
5f5c8ee5
GM
16561 FIELD_WIDTH is the minimum number of output glyphs to produce.
16562 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16563 with spaces. If STRING has more characters, more than FIELD_WIDTH
16564 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
2311178e 16565
5f5c8ee5
GM
16566 PRECISION is the maximum number of characters to output from
16567 STRING. PRECISION < 0 means don't truncate the string.
a2889657 16568
5f5c8ee5 16569 This is roughly equivalent to printf format specifiers:
a2889657 16570
5f5c8ee5
GM
16571 FIELD_WIDTH PRECISION PRINTF
16572 ----------------------------------------
16573 -1 -1 %s
16574 -1 10 %.10s
16575 10 -1 %10s
16576 20 10 %20.10s
a2889657 16577
5f5c8ee5
GM
16578 MULTIBYTE zero means do not display multibyte chars, > 0 means do
16579 display them, and < 0 means obey the current buffer's value of
16580 enable_multibyte_characters.
278feba9 16581
5f5c8ee5 16582 Value is the number of glyphs produced. */
b1d1124b 16583
5f5c8ee5
GM
16584static int
16585display_string (string, lisp_string, face_string, face_string_pos,
16586 start, it, field_width, precision, max_x, multibyte)
16587 unsigned char *string;
16588 Lisp_Object lisp_string;
68c45bf0
PE
16589 Lisp_Object face_string;
16590 int face_string_pos;
5f5c8ee5
GM
16591 int start;
16592 struct it *it;
16593 int field_width, precision, max_x;
16594 int multibyte;
16595{
16596 int hpos_at_start = it->hpos;
16597 int saved_face_id = it->face_id;
16598 struct glyph_row *row = it->glyph_row;
16599
16600 /* Initialize the iterator IT for iteration over STRING beginning
e719f5ae 16601 with index START. */
5f5c8ee5
GM
16602 reseat_to_string (it, string, lisp_string, start,
16603 precision, field_width, multibyte);
16604
16605 /* If displaying STRING, set up the face of the iterator
16606 from LISP_STRING, if that's given. */
16607 if (STRINGP (face_string))
16608 {
16609 int endptr;
16610 struct face *face;
2311178e 16611
5f5c8ee5
GM
16612 it->face_id
16613 = face_at_string_position (it->w, face_string, face_string_pos,
16614 0, it->region_beg_charpos,
16615 it->region_end_charpos,
5de7c6f2 16616 &endptr, it->base_face_id, 0);
5f5c8ee5
GM
16617 face = FACE_FROM_ID (it->f, it->face_id);
16618 it->face_box_p = face->box != FACE_NO_BOX;
b1d1124b 16619 }
a2889657 16620
5f5c8ee5
GM
16621 /* Set max_x to the maximum allowed X position. Don't let it go
16622 beyond the right edge of the window. */
16623 if (max_x <= 0)
16624 max_x = it->last_visible_x;
16625 else
16626 max_x = min (max_x, it->last_visible_x);
efc63ef0 16627
5f5c8ee5
GM
16628 /* Skip over display elements that are not visible. because IT->w is
16629 hscrolled. */
16630 if (it->current_x < it->first_visible_x)
16631 move_it_in_display_line_to (it, 100000, it->first_visible_x,
16632 MOVE_TO_POS | MOVE_TO_X);
a2889657 16633
5f5c8ee5
GM
16634 row->ascent = it->max_ascent;
16635 row->height = it->max_ascent + it->max_descent;
312246d1
GM
16636 row->phys_ascent = it->max_phys_ascent;
16637 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
1c9241f5 16638
5f5c8ee5
GM
16639 /* This condition is for the case that we are called with current_x
16640 past last_visible_x. */
16641 while (it->current_x < max_x)
a2889657 16642 {
5f5c8ee5 16643 int x_before, x, n_glyphs_before, i, nglyphs;
1c9241f5 16644
5f5c8ee5
GM
16645 /* Get the next display element. */
16646 if (!get_next_display_element (it))
90adcf20 16647 break;
1c9241f5 16648
5f5c8ee5
GM
16649 /* Produce glyphs. */
16650 x_before = it->current_x;
16651 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
16652 PRODUCE_GLYPHS (it);
90adcf20 16653
5f5c8ee5
GM
16654 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
16655 i = 0;
16656 x = x_before;
16657 while (i < nglyphs)
a2889657 16658 {
5f5c8ee5 16659 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
2311178e 16660
5f5c8ee5
GM
16661 if (!it->truncate_lines_p
16662 && x + glyph->pixel_width > max_x)
16663 {
16664 /* End of continued line or max_x reached. */
37be86f2
KH
16665 if (CHAR_GLYPH_PADDING_P (*glyph))
16666 {
16667 /* A wide character is unbreakable. */
16668 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
16669 it->current_x = x_before;
16670 }
16671 else
16672 {
16673 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
16674 it->current_x = x;
16675 }
5f5c8ee5
GM
16676 break;
16677 }
16678 else if (x + glyph->pixel_width > it->first_visible_x)
16679 {
16680 /* Glyph is at least partially visible. */
16681 ++it->hpos;
16682 if (x < it->first_visible_x)
16683 it->glyph_row->x = x - it->first_visible_x;
16684 }
16685 else
a2889657 16686 {
5f5c8ee5
GM
16687 /* Glyph is off the left margin of the display area.
16688 Should not happen. */
16689 abort ();
a2889657 16690 }
5f5c8ee5
GM
16691
16692 row->ascent = max (row->ascent, it->max_ascent);
16693 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
16694 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16695 row->phys_height = max (row->phys_height,
16696 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
16697 x += glyph->pixel_width;
16698 ++i;
a2889657 16699 }
5f5c8ee5
GM
16700
16701 /* Stop if max_x reached. */
16702 if (i < nglyphs)
16703 break;
16704
16705 /* Stop at line ends. */
16706 if (ITERATOR_AT_END_OF_LINE_P (it))
a2889657 16707 {
5f5c8ee5
GM
16708 it->continuation_lines_width = 0;
16709 break;
a2889657 16710 }
1c9241f5 16711
cafafe0b 16712 set_iterator_to_next (it, 1);
a688bb24 16713
5f5c8ee5
GM
16714 /* Stop if truncating at the right edge. */
16715 if (it->truncate_lines_p
16716 && it->current_x >= it->last_visible_x)
16717 {
16718 /* Add truncation mark, but don't do it if the line is
16719 truncated at a padding space. */
16720 if (IT_CHARPOS (*it) < it->string_nchars)
1c9241f5 16721 {
5f5c8ee5 16722 if (!FRAME_WINDOW_P (it->f))
37be86f2
KH
16723 {
16724 int i, n;
16725
9299cb15 16726 if (it->current_x > it->last_visible_x)
37be86f2 16727 {
9299cb15
KH
16728 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16729 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16730 break;
16731 for (n = row->used[TEXT_AREA]; i < n; ++i)
16732 {
16733 row->used[TEXT_AREA] = i;
16734 produce_special_glyphs (it, IT_TRUNCATION);
16735 }
37be86f2 16736 }
9299cb15 16737 produce_special_glyphs (it, IT_TRUNCATION);
37be86f2 16738 }
5f5c8ee5 16739 it->glyph_row->truncated_on_right_p = 1;
1c9241f5 16740 }
5f5c8ee5 16741 break;
1c9241f5 16742 }
a2889657
JB
16743 }
16744
5f5c8ee5
GM
16745 /* Maybe insert a truncation at the left. */
16746 if (it->first_visible_x
16747 && IT_CHARPOS (*it) > 0)
a2889657 16748 {
5f5c8ee5
GM
16749 if (!FRAME_WINDOW_P (it->f))
16750 insert_left_trunc_glyphs (it);
16751 it->glyph_row->truncated_on_left_p = 1;
a2889657
JB
16752 }
16753
5f5c8ee5 16754 it->face_id = saved_face_id;
2311178e 16755
5f5c8ee5
GM
16756 /* Value is number of columns displayed. */
16757 return it->hpos - hpos_at_start;
16758}
a2889657 16759
a2889657 16760
a2889657 16761\f
3b6b6db7 16762/* This is like a combination of memq and assq. Return 1/2 if PROPVAL
5f5c8ee5
GM
16763 appears as an element of LIST or as the car of an element of LIST.
16764 If PROPVAL is a list, compare each element against LIST in that
3b6b6db7
SM
16765 way, and return 1/2 if any element of PROPVAL is found in LIST.
16766 Otherwise return 0. This function cannot quit.
16767 The return value is 2 if the text is invisible but with an ellipsis
16768 and 1 if it's invisible and without an ellipsis. */
642eefc6
RS
16769
16770int
16771invisible_p (propval, list)
16772 register Lisp_Object propval;
16773 Lisp_Object list;
16774{
3b6b6db7 16775 register Lisp_Object tail, proptail;
2311178e 16776
3b6b6db7
SM
16777 for (tail = list; CONSP (tail); tail = XCDR (tail))
16778 {
16779 register Lisp_Object tem;
16780 tem = XCAR (tail);
16781 if (EQ (propval, tem))
16782 return 1;
16783 if (CONSP (tem) && EQ (propval, XCAR (tem)))
16784 return NILP (XCDR (tem)) ? 1 : 2;
16785 }
2311178e 16786
3b6b6db7
SM
16787 if (CONSP (propval))
16788 {
16789 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
16790 {
16791 Lisp_Object propelt;
16792 propelt = XCAR (proptail);
16793 for (tail = list; CONSP (tail); tail = XCDR (tail))
16794 {
16795 register Lisp_Object tem;
16796 tem = XCAR (tail);
16797 if (EQ (propelt, tem))
16798 return 1;
16799 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
16800 return NILP (XCDR (tem)) ? 1 : 2;
16801 }
16802 }
16803 }
2311178e 16804
3b6b6db7 16805 return 0;
642eefc6 16806}
5f5c8ee5 16807
da06e4d7
KS
16808/* Calculate a width or height in pixels from a specification using
16809 the following elements:
16810
be21b925 16811 SPEC ::=
da06e4d7
KS
16812 NUM - a (fractional) multiple of the default font width/height
16813 (NUM) - specifies exactly NUM pixels
16814 UNIT - a fixed number of pixels, see below.
16815 ELEMENT - size of a display element in pixels, see below.
16816 (NUM . SPEC) - equals NUM * SPEC
16817 (+ SPEC SPEC ...) - add pixel values
16818 (- SPEC SPEC ...) - subtract pixel values
16819 (- SPEC) - negate pixel value
16820
be21b925 16821 NUM ::=
da06e4d7
KS
16822 INT or FLOAT - a number constant
16823 SYMBOL - use symbol's (buffer local) variable binding.
16824
16825 UNIT ::=
16826 in - pixels per inch *)
16827 mm - pixels per 1/1000 meter *)
16828 cm - pixels per 1/100 meter *)
16829 width - width of current font in pixels.
16830 height - height of current font in pixels.
16831
16832 *) using the ratio(s) defined in display-pixels-per-inch.
16833
16834 ELEMENT ::=
16835
16836 left-fringe - left fringe width in pixels
16837 right-fringe - right fringe width in pixels
16838
16839 left-margin - left margin width in pixels
16840 right-margin - right margin width in pixels
16841
16842 scroll-bar - scroll-bar area width in pixels
16843
16844 Examples:
16845
16846 Pixels corresponding to 5 inches:
be21b925
KS
16847 (5 . in)
16848
da06e4d7
KS
16849 Total width of non-text areas on left side of window (if scroll-bar is on left):
16850 '(space :width (+ left-fringe left-margin scroll-bar))
16851
16852 Align to first text column (in header line):
16853 '(space :align-to 0)
16854
be21b925 16855 Align to middle of text area minus half the width of variable `my-image'
da06e4d7
KS
16856 containing a loaded image:
16857 '(space :align-to (0.5 . (- text my-image)))
16858
16859 Width of left margin minus width of 1 character in the default font:
16860 '(space :width (- left-margin 1))
16861
16862 Width of left margin minus width of 2 characters in the current font:
16863 '(space :width (- left-margin (2 . width)))
16864
16865 Center 1 character over left-margin (in header line):
16866 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
16867
16868 Different ways to express width of left fringe plus left margin minus one pixel:
16869 '(space :width (- (+ left-fringe left-margin) (1)))
16870 '(space :width (+ left-fringe left-margin (- (1))))
16871 '(space :width (+ left-fringe left-margin (-1)))
16872
16873*/
16874
16875#define NUMVAL(X) \
16876 ((INTEGERP (X) || FLOATP (X)) \
16877 ? XFLOATINT (X) \
16878 : - 1)
16879
16880int
16881calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
16882 double *res;
16883 struct it *it;
16884 Lisp_Object prop;
16885 void *font;
16886 int width_p, *align_to;
16887{
16888 double pixels;
16889
16890#define OK_PIXELS(val) ((*res = (double)(val)), 1)
16891#define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
16892
16893 if (NILP (prop))
16894 return OK_PIXELS (0);
16895
16896 if (SYMBOLP (prop))
16897 {
16898 if (SCHARS (SYMBOL_NAME (prop)) == 2)
16899 {
16900 char *unit = SDATA (SYMBOL_NAME (prop));
16901
16902 if (unit[0] == 'i' && unit[1] == 'n')
16903 pixels = 1.0;
16904 else if (unit[0] == 'm' && unit[1] == 'm')
16905 pixels = 25.4;
16906 else if (unit[0] == 'c' && unit[1] == 'm')
16907 pixels = 2.54;
16908 else
16909 pixels = 0;
16910 if (pixels > 0)
16911 {
16912 double ppi;
16913 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
16914 || (CONSP (Vdisplay_pixels_per_inch)
16915 && (ppi = (width_p
16916 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
16917 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
16918 ppi > 0)))
16919 return OK_PIXELS (ppi / pixels);
16920
16921 return 0;
16922 }
16923 }
16924
16925#ifdef HAVE_WINDOW_SYSTEM
16926 if (EQ (prop, Qheight))
16927 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
16928 if (EQ (prop, Qwidth))
16929 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
16930#else
16931 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
16932 return OK_PIXELS (1);
16933#endif
16934
16935 if (EQ (prop, Qtext))
16936 return OK_PIXELS (width_p
16937 ? window_box_width (it->w, TEXT_AREA)
16938 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
16939
16940 if (align_to && *align_to < 0)
16941 {
16942 *res = 0;
16943 if (EQ (prop, Qleft))
16944 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
16945 if (EQ (prop, Qright))
16946 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
16947 if (EQ (prop, Qcenter))
16948 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
16949 + window_box_width (it->w, TEXT_AREA) / 2);
16950 if (EQ (prop, Qleft_fringe))
16951 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
16952 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
16953 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
16954 if (EQ (prop, Qright_fringe))
16955 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
16956 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
16957 : window_box_right_offset (it->w, TEXT_AREA));
16958 if (EQ (prop, Qleft_margin))
16959 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
16960 if (EQ (prop, Qright_margin))
16961 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
16962 if (EQ (prop, Qscroll_bar))
16963 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
16964 ? 0
16965 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
16966 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
16967 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
16968 : 0)));
16969 }
16970 else
16971 {
16972 if (EQ (prop, Qleft_fringe))
16973 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
16974 if (EQ (prop, Qright_fringe))
16975 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
16976 if (EQ (prop, Qleft_margin))
16977 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
16978 if (EQ (prop, Qright_margin))
16979 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
16980 if (EQ (prop, Qscroll_bar))
16981 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
16982 }
16983
16984 prop = Fbuffer_local_value (prop, it->w->buffer);
16985 }
16986
16987 if (INTEGERP (prop) || FLOATP (prop))
16988 {
16989 int base_unit = (width_p
16990 ? FRAME_COLUMN_WIDTH (it->f)
16991 : FRAME_LINE_HEIGHT (it->f));
16992 return OK_PIXELS (XFLOATINT (prop) * base_unit);
16993 }
16994
16995 if (CONSP (prop))
16996 {
16997 Lisp_Object car = XCAR (prop);
16998 Lisp_Object cdr = XCDR (prop);
16999
17000 if (SYMBOLP (car))
17001 {
992126de 17002#ifdef HAVE_WINDOW_SYSTEM
da06e4d7
KS
17003 if (valid_image_p (prop))
17004 {
17005 int id = lookup_image (it->f, prop);
17006 struct image *img = IMAGE_FROM_ID (it->f, id);
17007
17008 return OK_PIXELS (width_p ? img->width : img->height);
17009 }
992126de 17010#endif
da06e4d7
KS
17011 if (EQ (car, Qplus) || EQ (car, Qminus))
17012 {
17013 int first = 1;
17014 double px;
17015
17016 pixels = 0;
17017 while (CONSP (cdr))
17018 {
17019 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
17020 font, width_p, align_to))
17021 return 0;
17022 if (first)
17023 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
17024 else
17025 pixels += px;
17026 cdr = XCDR (cdr);
17027 }
17028 if (EQ (car, Qminus))
17029 pixels = -pixels;
17030 return OK_PIXELS (pixels);
17031 }
17032
17033 car = Fbuffer_local_value (car, it->w->buffer);
17034 }
17035
17036 if (INTEGERP (car) || FLOATP (car))
17037 {
17038 double fact;
17039 pixels = XFLOATINT (car);
17040 if (NILP (cdr))
17041 return OK_PIXELS (pixels);
17042 if (calc_pixel_width_or_height (&fact, it, cdr,
17043 font, width_p, align_to))
17044 return OK_PIXELS (pixels * fact);
17045 return 0;
17046 }
17047
17048 return 0;
17049 }
17050
17051 return 0;
17052}
17053
642eefc6 17054\f
133c764e
KS
17055/***********************************************************************
17056 Glyph Display
17057 ***********************************************************************/
17058
79fa9e0f
KS
17059#ifdef HAVE_WINDOW_SYSTEM
17060
133c764e
KS
17061#if GLYPH_DEBUG
17062
17063void
17064dump_glyph_string (s)
17065 struct glyph_string *s;
17066{
17067 fprintf (stderr, "glyph string\n");
17068 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17069 s->x, s->y, s->width, s->height);
17070 fprintf (stderr, " ybase = %d\n", s->ybase);
17071 fprintf (stderr, " hl = %d\n", s->hl);
17072 fprintf (stderr, " left overhang = %d, right = %d\n",
17073 s->left_overhang, s->right_overhang);
17074 fprintf (stderr, " nchars = %d\n", s->nchars);
17075 fprintf (stderr, " extends to end of line = %d\n",
17076 s->extends_to_end_of_line_p);
17077 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17078 fprintf (stderr, " bg width = %d\n", s->background_width);
17079}
17080
17081#endif /* GLYPH_DEBUG */
17082
17083/* Initialize glyph string S. CHAR2B is a suitably allocated vector
17084 of XChar2b structures for S; it can't be allocated in
17085 init_glyph_string because it must be allocated via `alloca'. W
17086 is the window on which S is drawn. ROW and AREA are the glyph row
17087 and area within the row from which S is constructed. START is the
17088 index of the first glyph structure covered by S. HL is a
17089 face-override for drawing S. */
17090
17091#ifdef HAVE_NTGUI
17092#define OPTIONAL_HDC(hdc) hdc,
17093#define DECLARE_HDC(hdc) HDC hdc;
17094#define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17095#define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17096#endif
17097
17098#ifndef OPTIONAL_HDC
17099#define OPTIONAL_HDC(hdc)
17100#define DECLARE_HDC(hdc)
17101#define ALLOCATE_HDC(hdc, f)
17102#define RELEASE_HDC(hdc, f)
17103#endif
17104
17105static void
17106init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17107 struct glyph_string *s;
17108 DECLARE_HDC (hdc)
17109 XChar2b *char2b;
17110 struct window *w;
17111 struct glyph_row *row;
17112 enum glyph_row_area area;
17113 int start;
17114 enum draw_glyphs_face hl;
17115{
17116 bzero (s, sizeof *s);
17117 s->w = w;
17118 s->f = XFRAME (w->frame);
17119#ifdef HAVE_NTGUI
17120 s->hdc = hdc;
17121#endif
17122 s->display = FRAME_X_DISPLAY (s->f);
17123 s->window = FRAME_X_WINDOW (s->f);
17124 s->char2b = char2b;
17125 s->hl = hl;
17126 s->row = row;
17127 s->area = area;
17128 s->first_glyph = row->glyphs[area] + start;
17129 s->height = row->height;
17130 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17131
17132 /* Display the internal border below the tool-bar window. */
17133 if (s->w == XWINDOW (s->f->tool_bar_window))
17134 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17135
17136 s->ybase = s->y + row->ascent;
17137}
17138
17139
17140/* Append the list of glyph strings with head H and tail T to the list
17141 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17142
17143static INLINE void
17144append_glyph_string_lists (head, tail, h, t)
17145 struct glyph_string **head, **tail;
17146 struct glyph_string *h, *t;
17147{
17148 if (h)
17149 {
17150 if (*head)
17151 (*tail)->next = h;
17152 else
17153 *head = h;
17154 h->prev = *tail;
17155 *tail = t;
17156 }
17157}
17158
17159
17160/* Prepend the list of glyph strings with head H and tail T to the
17161 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17162 result. */
17163
17164static INLINE void
17165prepend_glyph_string_lists (head, tail, h, t)
17166 struct glyph_string **head, **tail;
17167 struct glyph_string *h, *t;
17168{
17169 if (h)
17170 {
17171 if (*head)
17172 (*head)->prev = t;
17173 else
17174 *tail = t;
17175 t->next = *head;
17176 *head = h;
17177 }
17178}
17179
17180
17181/* Append glyph string S to the list with head *HEAD and tail *TAIL.
17182 Set *HEAD and *TAIL to the resulting list. */
17183
17184static INLINE void
17185append_glyph_string (head, tail, s)
17186 struct glyph_string **head, **tail;
17187 struct glyph_string *s;
17188{
17189 s->next = s->prev = NULL;
17190 append_glyph_string_lists (head, tail, s, s);
17191}
17192
17193
17194/* Get face and two-byte form of character glyph GLYPH on frame F.
17195 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17196 a pointer to a realized face that is ready for display. */
17197
17198static INLINE struct face *
17199get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17200 struct frame *f;
17201 struct glyph *glyph;
17202 XChar2b *char2b;
17203 int *two_byte_p;
17204{
17205 struct face *face;
17206
17207 xassert (glyph->type == CHAR_GLYPH);
17208 face = FACE_FROM_ID (f, glyph->face_id);
17209
17210 if (two_byte_p)
17211 *two_byte_p = 0;
17212
17213 if (!glyph->multibyte_p)
17214 {
17215 /* Unibyte case. We don't have to encode, but we have to make
17216 sure to use a face suitable for unibyte. */
17217 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17218 }
17219 else if (glyph->u.ch < 128
17220 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17221 {
17222 /* Case of ASCII in a face known to fit ASCII. */
17223 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17224 }
17225 else
17226 {
17227 int c1, c2, charset;
17228
17229 /* Split characters into bytes. If c2 is -1 afterwards, C is
17230 really a one-byte character so that byte1 is zero. */
17231 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17232 if (c2 > 0)
17233 STORE_XCHAR2B (char2b, c1, c2);
17234 else
17235 STORE_XCHAR2B (char2b, 0, c1);
17236
17237 /* Maybe encode the character in *CHAR2B. */
17238 if (charset != CHARSET_ASCII)
17239 {
17240 struct font_info *font_info
17241 = FONT_INFO_FROM_ID (f, face->font_info_id);
17242 if (font_info)
17243 glyph->font_type
17244 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
17245 }
17246 }
17247
17248 /* Make sure X resources of the face are allocated. */
17249 xassert (face != NULL);
17250 PREPARE_FACE_FOR_DISPLAY (f, face);
17251 return face;
17252}
17253
17254
17255/* Fill glyph string S with composition components specified by S->cmp.
17256
17257 FACES is an array of faces for all components of this composition.
17258 S->gidx is the index of the first component for S.
17259 OVERLAPS_P non-zero means S should draw the foreground only, and
17260 use its physical height for clipping.
17261
17262 Value is the index of a component not in S. */
17263
17264static int
17265fill_composite_glyph_string (s, faces, overlaps_p)
17266 struct glyph_string *s;
17267 struct face **faces;
17268 int overlaps_p;
17269{
17270 int i;
17271
17272 xassert (s);
17273
17274 s->for_overlaps_p = overlaps_p;
17275
17276 s->face = faces[s->gidx];
17277 s->font = s->face->font;
17278 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17279
17280 /* For all glyphs of this composition, starting at the offset
17281 S->gidx, until we reach the end of the definition or encounter a
17282 glyph that requires the different face, add it to S. */
17283 ++s->nchars;
17284 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
17285 ++s->nchars;
17286
17287 /* All glyph strings for the same composition has the same width,
17288 i.e. the width set for the first component of the composition. */
17289
17290 s->width = s->first_glyph->pixel_width;
17291
17292 /* If the specified font could not be loaded, use the frame's
17293 default font, but record the fact that we couldn't load it in
17294 the glyph string so that we can draw rectangles for the
17295 characters of the glyph string. */
17296 if (s->font == NULL)
17297 {
17298 s->font_not_found_p = 1;
17299 s->font = FRAME_FONT (s->f);
17300 }
17301
17302 /* Adjust base line for subscript/superscript text. */
17303 s->ybase += s->first_glyph->voffset;
17304
17305 xassert (s->face && s->face->gc);
17306
17307 /* This glyph string must always be drawn with 16-bit functions. */
17308 s->two_byte_p = 1;
17309
17310 return s->gidx + s->nchars;
17311}
17312
17313
17314/* Fill glyph string S from a sequence of character glyphs.
17315
17316 FACE_ID is the face id of the string. START is the index of the
17317 first glyph to consider, END is the index of the last + 1.
17318 OVERLAPS_P non-zero means S should draw the foreground only, and
17319 use its physical height for clipping.
17320
17321 Value is the index of the first glyph not in S. */
17322
17323static int
17324fill_glyph_string (s, face_id, start, end, overlaps_p)
17325 struct glyph_string *s;
17326 int face_id;
17327 int start, end, overlaps_p;
17328{
17329 struct glyph *glyph, *last;
17330 int voffset;
17331 int glyph_not_available_p;
17332
17333 xassert (s->f == XFRAME (s->w->frame));
17334 xassert (s->nchars == 0);
17335 xassert (start >= 0 && end > start);
17336
17337 s->for_overlaps_p = overlaps_p,
17338 glyph = s->row->glyphs[s->area] + start;
17339 last = s->row->glyphs[s->area] + end;
17340 voffset = glyph->voffset;
17341
17342 glyph_not_available_p = glyph->glyph_not_available_p;
17343
17344 while (glyph < last
17345 && glyph->type == CHAR_GLYPH
17346 && glyph->voffset == voffset
17347 /* Same face id implies same font, nowadays. */
17348 && glyph->face_id == face_id
17349 && glyph->glyph_not_available_p == glyph_not_available_p)
17350 {
17351 int two_byte_p;
17352
17353 s->face = get_glyph_face_and_encoding (s->f, glyph,
17354 s->char2b + s->nchars,
17355 &two_byte_p);
17356 s->two_byte_p = two_byte_p;
17357 ++s->nchars;
17358 xassert (s->nchars <= end - start);
17359 s->width += glyph->pixel_width;
17360 ++glyph;
17361 }
17362
17363 s->font = s->face->font;
17364 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17365
17366 /* If the specified font could not be loaded, use the frame's font,
17367 but record the fact that we couldn't load it in
17368 S->font_not_found_p so that we can draw rectangles for the
17369 characters of the glyph string. */
17370 if (s->font == NULL || glyph_not_available_p)
17371 {
17372 s->font_not_found_p = 1;
17373 s->font = FRAME_FONT (s->f);
17374 }
17375
17376 /* Adjust base line for subscript/superscript text. */
17377 s->ybase += voffset;
17378
17379 xassert (s->face && s->face->gc);
17380 return glyph - s->row->glyphs[s->area];
17381}
17382
17383
17384/* Fill glyph string S from image glyph S->first_glyph. */
17385
17386static void
17387fill_image_glyph_string (s)
17388 struct glyph_string *s;
17389{
17390 xassert (s->first_glyph->type == IMAGE_GLYPH);
17391 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
17392 xassert (s->img);
e893970b 17393 s->slice = s->first_glyph->slice;
133c764e
KS
17394 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
17395 s->font = s->face->font;
17396 s->width = s->first_glyph->pixel_width;
17397
17398 /* Adjust base line for subscript/superscript text. */
17399 s->ybase += s->first_glyph->voffset;
17400}
17401
17402
17403/* Fill glyph string S from a sequence of stretch glyphs.
17404
17405 ROW is the glyph row in which the glyphs are found, AREA is the
17406 area within the row. START is the index of the first glyph to
17407 consider, END is the index of the last + 1.
17408
17409 Value is the index of the first glyph not in S. */
17410
17411static int
17412fill_stretch_glyph_string (s, row, area, start, end)
17413 struct glyph_string *s;
17414 struct glyph_row *row;
17415 enum glyph_row_area area;
17416 int start, end;
17417{
17418 struct glyph *glyph, *last;
17419 int voffset, face_id;
17420
17421 xassert (s->first_glyph->type == STRETCH_GLYPH);
17422
17423 glyph = s->row->glyphs[s->area] + start;
17424 last = s->row->glyphs[s->area] + end;
17425 face_id = glyph->face_id;
17426 s->face = FACE_FROM_ID (s->f, face_id);
17427 s->font = s->face->font;
17428 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17429 s->width = glyph->pixel_width;
17430 voffset = glyph->voffset;
17431
17432 for (++glyph;
17433 (glyph < last
17434 && glyph->type == STRETCH_GLYPH
17435 && glyph->voffset == voffset
17436 && glyph->face_id == face_id);
17437 ++glyph)
17438 s->width += glyph->pixel_width;
17439
17440 /* Adjust base line for subscript/superscript text. */
17441 s->ybase += voffset;
17442
17443 /* The case that face->gc == 0 is handled when drawing the glyph
17444 string by calling PREPARE_FACE_FOR_DISPLAY. */
17445 xassert (s->face);
17446 return glyph - s->row->glyphs[s->area];
17447}
17448
17449
17450/* EXPORT for RIF:
17451 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
17452 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
17453 assumed to be zero. */
17454
17455void
17456x_get_glyph_overhangs (glyph, f, left, right)
17457 struct glyph *glyph;
17458 struct frame *f;
17459 int *left, *right;
17460{
17461 *left = *right = 0;
17462
17463 if (glyph->type == CHAR_GLYPH)
17464 {
17465 XFontStruct *font;
17466 struct face *face;
17467 struct font_info *font_info;
17468 XChar2b char2b;
17469 XCharStruct *pcm;
17470
17471 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
17472 font = face->font;
17473 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
17474 if (font /* ++KFS: Should this be font_info ? */
17475 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
17476 {
17477 if (pcm->rbearing > pcm->width)
17478 *right = pcm->rbearing - pcm->width;
17479 if (pcm->lbearing < 0)
17480 *left = -pcm->lbearing;
17481 }
17482 }
17483}
17484
17485
17486/* Return the index of the first glyph preceding glyph string S that
17487 is overwritten by S because of S's left overhang. Value is -1
17488 if no glyphs are overwritten. */
17489
17490static int
17491left_overwritten (s)
17492 struct glyph_string *s;
17493{
17494 int k;
17495
17496 if (s->left_overhang)
17497 {
17498 int x = 0, i;
17499 struct glyph *glyphs = s->row->glyphs[s->area];
17500 int first = s->first_glyph - glyphs;
17501
17502 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
17503 x -= glyphs[i].pixel_width;
17504
17505 k = i + 1;
17506 }
17507 else
17508 k = -1;
17509
17510 return k;
17511}
17512
17513
17514/* Return the index of the first glyph preceding glyph string S that
17515 is overwriting S because of its right overhang. Value is -1 if no
17516 glyph in front of S overwrites S. */
17517
17518static int
17519left_overwriting (s)
17520 struct glyph_string *s;
17521{
17522 int i, k, x;
17523 struct glyph *glyphs = s->row->glyphs[s->area];
17524 int first = s->first_glyph - glyphs;
17525
17526 k = -1;
17527 x = 0;
17528 for (i = first - 1; i >= 0; --i)
17529 {
17530 int left, right;
17531 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17532 if (x + right > 0)
17533 k = i;
17534 x -= glyphs[i].pixel_width;
17535 }
17536
17537 return k;
17538}
17539
17540
17541/* Return the index of the last glyph following glyph string S that is
17542 not overwritten by S because of S's right overhang. Value is -1 if
17543 no such glyph is found. */
17544
17545static int
17546right_overwritten (s)
17547 struct glyph_string *s;
17548{
17549 int k = -1;
17550
17551 if (s->right_overhang)
17552 {
17553 int x = 0, i;
17554 struct glyph *glyphs = s->row->glyphs[s->area];
17555 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17556 int end = s->row->used[s->area];
17557
17558 for (i = first; i < end && s->right_overhang > x; ++i)
17559 x += glyphs[i].pixel_width;
17560
17561 k = i;
17562 }
17563
17564 return k;
17565}
17566
17567
17568/* Return the index of the last glyph following glyph string S that
17569 overwrites S because of its left overhang. Value is negative
17570 if no such glyph is found. */
17571
17572static int
17573right_overwriting (s)
17574 struct glyph_string *s;
17575{
17576 int i, k, x;
17577 int end = s->row->used[s->area];
17578 struct glyph *glyphs = s->row->glyphs[s->area];
17579 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17580
17581 k = -1;
17582 x = 0;
17583 for (i = first; i < end; ++i)
17584 {
17585 int left, right;
17586 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17587 if (x - left < 0)
17588 k = i;
17589 x += glyphs[i].pixel_width;
17590 }
17591
17592 return k;
17593}
17594
17595
17596/* Get face and two-byte form of character C in face FACE_ID on frame
17597 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
17598 means we want to display multibyte text. DISPLAY_P non-zero means
17599 make sure that X resources for the face returned are allocated.
17600 Value is a pointer to a realized face that is ready for display if
17601 DISPLAY_P is non-zero. */
17602
17603static INLINE struct face *
17604get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
17605 struct frame *f;
17606 int c, face_id;
17607 XChar2b *char2b;
17608 int multibyte_p, display_p;
17609{
17610 struct face *face = FACE_FROM_ID (f, face_id);
17611
17612 if (!multibyte_p)
17613 {
17614 /* Unibyte case. We don't have to encode, but we have to make
17615 sure to use a face suitable for unibyte. */
17616 STORE_XCHAR2B (char2b, 0, c);
17617 face_id = FACE_FOR_CHAR (f, face, c);
17618 face = FACE_FROM_ID (f, face_id);
17619 }
17620 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
17621 {
17622 /* Case of ASCII in a face known to fit ASCII. */
17623 STORE_XCHAR2B (char2b, 0, c);
17624 }
17625 else
17626 {
17627 int c1, c2, charset;
17628
17629 /* Split characters into bytes. If c2 is -1 afterwards, C is
17630 really a one-byte character so that byte1 is zero. */
17631 SPLIT_CHAR (c, charset, c1, c2);
17632 if (c2 > 0)
17633 STORE_XCHAR2B (char2b, c1, c2);
17634 else
17635 STORE_XCHAR2B (char2b, 0, c1);
17636
17637 /* Maybe encode the character in *CHAR2B. */
17638 if (face->font != NULL)
17639 {
17640 struct font_info *font_info
17641 = FONT_INFO_FROM_ID (f, face->font_info_id);
17642 if (font_info)
17643 rif->encode_char (c, char2b, font_info, 0);
17644 }
17645 }
17646
17647 /* Make sure X resources of the face are allocated. */
17648#ifdef HAVE_X_WINDOWS
17649 if (display_p)
17650#endif
17651 {
17652 xassert (face != NULL);
17653 PREPARE_FACE_FOR_DISPLAY (f, face);
17654 }
17655
17656 return face;
17657}
17658
17659
17660/* Set background width of glyph string S. START is the index of the
17661 first glyph following S. LAST_X is the right-most x-position + 1
17662 in the drawing area. */
17663
17664static INLINE void
17665set_glyph_string_background_width (s, start, last_x)
17666 struct glyph_string *s;
17667 int start;
17668 int last_x;
17669{
17670 /* If the face of this glyph string has to be drawn to the end of
17671 the drawing area, set S->extends_to_end_of_line_p. */
17672 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
17673
17674 if (start == s->row->used[s->area]
17675 && s->area == TEXT_AREA
17676 && ((s->hl == DRAW_NORMAL_TEXT
17677 && (s->row->fill_line_p
17678 || s->face->background != default_face->background
17679 || s->face->stipple != default_face->stipple
17680 || s->row->mouse_face_p))
17681 || s->hl == DRAW_MOUSE_FACE
17682 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
17683 && s->row->fill_line_p)))
17684 s->extends_to_end_of_line_p = 1;
17685
17686 /* If S extends its face to the end of the line, set its
17687 background_width to the distance to the right edge of the drawing
17688 area. */
17689 if (s->extends_to_end_of_line_p)
17690 s->background_width = last_x - s->x + 1;
17691 else
17692 s->background_width = s->width;
17693}
17694
17695
17696/* Compute overhangs and x-positions for glyph string S and its
17697 predecessors, or successors. X is the starting x-position for S.
17698 BACKWARD_P non-zero means process predecessors. */
17699
17700static void
17701compute_overhangs_and_x (s, x, backward_p)
17702 struct glyph_string *s;
17703 int x;
17704 int backward_p;
17705{
17706 if (backward_p)
17707 {
17708 while (s)
17709 {
17710 if (rif->compute_glyph_string_overhangs)
17711 rif->compute_glyph_string_overhangs (s);
17712 x -= s->width;
17713 s->x = x;
17714 s = s->prev;
17715 }
17716 }
17717 else
17718 {
17719 while (s)
17720 {
17721 if (rif->compute_glyph_string_overhangs)
17722 rif->compute_glyph_string_overhangs (s);
17723 s->x = x;
17724 x += s->width;
17725 s = s->next;
17726 }
17727 }
17728}
17729
17730
17731
fa3c6b4d 17732/* The following macros are only called from draw_glyphs below.
133c764e
KS
17733 They reference the following parameters of that function directly:
17734 `w', `row', `area', and `overlap_p'
17735 as well as the following local variables:
17736 `s', `f', and `hdc' (in W32) */
17737
17738#ifdef HAVE_NTGUI
17739/* On W32, silently add local `hdc' variable to argument list of
17740 init_glyph_string. */
17741#define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17742 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
17743#else
17744#define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17745 init_glyph_string (s, char2b, w, row, area, start, hl)
17746#endif
17747
17748/* Add a glyph string for a stretch glyph to the list of strings
17749 between HEAD and TAIL. START is the index of the stretch glyph in
17750 row area AREA of glyph row ROW. END is the index of the last glyph
17751 in that glyph row area. X is the current output position assigned
17752 to the new glyph string constructed. HL overrides that face of the
17753 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17754 is the right-most x-position of the drawing area. */
17755
17756/* SunOS 4 bundled cc, barfed on continuations in the arg lists here
17757 and below -- keep them on one line. */
17758#define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17759 do \
17760 { \
17761 s = (struct glyph_string *) alloca (sizeof *s); \
17762 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17763 START = fill_stretch_glyph_string (s, row, area, START, END); \
17764 append_glyph_string (&HEAD, &TAIL, s); \
17765 s->x = (X); \
17766 } \
17767 while (0)
17768
17769
17770/* Add a glyph string for an image glyph to the list of strings
17771 between HEAD and TAIL. START is the index of the image glyph in
17772 row area AREA of glyph row ROW. END is the index of the last glyph
17773 in that glyph row area. X is the current output position assigned
17774 to the new glyph string constructed. HL overrides that face of the
17775 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17776 is the right-most x-position of the drawing area. */
17777
17778#define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17779 do \
17780 { \
17781 s = (struct glyph_string *) alloca (sizeof *s); \
17782 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17783 fill_image_glyph_string (s); \
17784 append_glyph_string (&HEAD, &TAIL, s); \
17785 ++START; \
17786 s->x = (X); \
17787 } \
17788 while (0)
17789
17790
17791/* Add a glyph string for a sequence of character glyphs to the list
17792 of strings between HEAD and TAIL. START is the index of the first
17793 glyph in row area AREA of glyph row ROW that is part of the new
17794 glyph string. END is the index of the last glyph in that glyph row
17795 area. X is the current output position assigned to the new glyph
17796 string constructed. HL overrides that face of the glyph; e.g. it
17797 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
17798 right-most x-position of the drawing area. */
17799
17800#define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17801 do \
17802 { \
17803 int c, face_id; \
17804 XChar2b *char2b; \
17805 \
17806 c = (row)->glyphs[area][START].u.ch; \
17807 face_id = (row)->glyphs[area][START].face_id; \
17808 \
17809 s = (struct glyph_string *) alloca (sizeof *s); \
17810 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
17811 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
17812 append_glyph_string (&HEAD, &TAIL, s); \
17813 s->x = (X); \
17814 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
17815 } \
17816 while (0)
17817
17818
17819/* Add a glyph string for a composite sequence to the list of strings
17820 between HEAD and TAIL. START is the index of the first glyph in
17821 row area AREA of glyph row ROW that is part of the new glyph
17822 string. END is the index of the last glyph in that glyph row area.
17823 X is the current output position assigned to the new glyph string
17824 constructed. HL overrides that face of the glyph; e.g. it is
17825 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
17826 x-position of the drawing area. */
17827
17828#define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17829 do { \
17830 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
17831 int face_id = (row)->glyphs[area][START].face_id; \
17832 struct face *base_face = FACE_FROM_ID (f, face_id); \
17833 struct composition *cmp = composition_table[cmp_id]; \
17834 int glyph_len = cmp->glyph_len; \
17835 XChar2b *char2b; \
17836 struct face **faces; \
17837 struct glyph_string *first_s = NULL; \
17838 int n; \
17839 \
17840 base_face = base_face->ascii_face; \
17841 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
17842 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
17843 /* At first, fill in `char2b' and `faces'. */ \
17844 for (n = 0; n < glyph_len; n++) \
17845 { \
17846 int c = COMPOSITION_GLYPH (cmp, n); \
17847 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
17848 faces[n] = FACE_FROM_ID (f, this_face_id); \
17849 get_char_face_and_encoding (f, c, this_face_id, \
17850 char2b + n, 1, 1); \
17851 } \
17852 \
17853 /* Make glyph_strings for each glyph sequence that is drawable by \
17854 the same face, and append them to HEAD/TAIL. */ \
17855 for (n = 0; n < cmp->glyph_len;) \
17856 { \
17857 s = (struct glyph_string *) alloca (sizeof *s); \
17858 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
17859 append_glyph_string (&(HEAD), &(TAIL), s); \
17860 s->cmp = cmp; \
17861 s->gidx = n; \
17862 s->x = (X); \
17863 \
17864 if (n == 0) \
17865 first_s = s; \
17866 \
17867 n = fill_composite_glyph_string (s, faces, overlaps_p); \
17868 } \
17869 \
17870 ++START; \
17871 s = first_s; \
17872 } while (0)
17873
17874
17875/* Build a list of glyph strings between HEAD and TAIL for the glyphs
17876 of AREA of glyph row ROW on window W between indices START and END.
17877 HL overrides the face for drawing glyph strings, e.g. it is
17878 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
17879 x-positions of the drawing area.
17880
17881 This is an ugly monster macro construct because we must use alloca
fa3c6b4d 17882 to allocate glyph strings (because draw_glyphs can be called
133c764e
KS
17883 asynchronously). */
17884
17885#define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17886 do \
17887 { \
17888 HEAD = TAIL = NULL; \
17889 while (START < END) \
17890 { \
17891 struct glyph *first_glyph = (row)->glyphs[area] + START; \
17892 switch (first_glyph->type) \
17893 { \
17894 case CHAR_GLYPH: \
17895 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
17896 HL, X, LAST_X); \
17897 break; \
17898 \
17899 case COMPOSITE_GLYPH: \
17900 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
17901 HL, X, LAST_X); \
17902 break; \
17903 \
17904 case STRETCH_GLYPH: \
17905 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
17906 HL, X, LAST_X); \
17907 break; \
17908 \
17909 case IMAGE_GLYPH: \
17910 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
17911 HL, X, LAST_X); \
17912 break; \
17913 \
17914 default: \
17915 abort (); \
17916 } \
17917 \
17918 set_glyph_string_background_width (s, START, LAST_X); \
17919 (X) += s->width; \
17920 } \
17921 } \
17922 while (0)
17923
17924
17925/* Draw glyphs between START and END in AREA of ROW on window W,
17926 starting at x-position X. X is relative to AREA in W. HL is a
17927 face-override with the following meaning:
17928
17929 DRAW_NORMAL_TEXT draw normally
17930 DRAW_CURSOR draw in cursor face
17931 DRAW_MOUSE_FACE draw in mouse face.
17932 DRAW_INVERSE_VIDEO draw in mode line face
17933 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
17934 DRAW_IMAGE_RAISED draw an image with a raised relief around it
17935
17936 If OVERLAPS_P is non-zero, draw only the foreground of characters
17937 and clip to the physical height of ROW.
17938
17939 Value is the x-position reached, relative to AREA of W. */
17940
fa3c6b4d
KS
17941static int
17942draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
133c764e
KS
17943 struct window *w;
17944 int x;
17945 struct glyph_row *row;
17946 enum glyph_row_area area;
17947 int start, end;
17948 enum draw_glyphs_face hl;
17949 int overlaps_p;
17950{
17951 struct glyph_string *head, *tail;
17952 struct glyph_string *s;
17953 int last_x, area_width;
17954 int x_reached;
17955 int i, j;
17956 struct frame *f = XFRAME (WINDOW_FRAME (w));
17957 DECLARE_HDC (hdc);
17958
17959 ALLOCATE_HDC (hdc, f);
17960
17961 /* Let's rather be paranoid than getting a SEGV. */
17962 end = min (end, row->used[area]);
17963 start = max (0, start);
17964 start = min (end, start);
17965
17966 /* Translate X to frame coordinates. Set last_x to the right
17967 end of the drawing area. */
17968 if (row->full_width_p)
17969 {
17970 /* X is relative to the left edge of W, without scroll bars
17971 or fringes. */
da8b7f4f
KS
17972 x += WINDOW_LEFT_EDGE_X (w);
17973 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
133c764e
KS
17974 }
17975 else
17976 {
da8b7f4f
KS
17977 int area_left = window_box_left (w, area);
17978 x += area_left;
133c764e 17979 area_width = window_box_width (w, area);
da8b7f4f 17980 last_x = area_left + area_width;
133c764e
KS
17981 }
17982
17983 /* Build a doubly-linked list of glyph_string structures between
17984 head and tail from what we have to draw. Note that the macro
17985 BUILD_GLYPH_STRINGS will modify its start parameter. That's
17986 the reason we use a separate variable `i'. */
17987 i = start;
17988 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
17989 if (tail)
17990 x_reached = tail->x + tail->background_width;
17991 else
17992 x_reached = x;
17993
17994 /* If there are any glyphs with lbearing < 0 or rbearing > width in
17995 the row, redraw some glyphs in front or following the glyph
17996 strings built above. */
17997 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
17998 {
17999 int dummy_x = 0;
18000 struct glyph_string *h, *t;
18001
18002 /* Compute overhangs for all glyph strings. */
18003 if (rif->compute_glyph_string_overhangs)
18004 for (s = head; s; s = s->next)
18005 rif->compute_glyph_string_overhangs (s);
18006
18007 /* Prepend glyph strings for glyphs in front of the first glyph
18008 string that are overwritten because of the first glyph
18009 string's left overhang. The background of all strings
18010 prepended must be drawn because the first glyph string
18011 draws over it. */
18012 i = left_overwritten (head);
18013 if (i >= 0)
18014 {
18015 j = i;
18016 BUILD_GLYPH_STRINGS (j, start, h, t,
18017 DRAW_NORMAL_TEXT, dummy_x, last_x);
18018 start = i;
18019 compute_overhangs_and_x (t, head->x, 1);
18020 prepend_glyph_string_lists (&head, &tail, h, t);
18021 }
18022
18023 /* Prepend glyph strings for glyphs in front of the first glyph
18024 string that overwrite that glyph string because of their
18025 right overhang. For these strings, only the foreground must
18026 be drawn, because it draws over the glyph string at `head'.
18027 The background must not be drawn because this would overwrite
18028 right overhangs of preceding glyphs for which no glyph
18029 strings exist. */
18030 i = left_overwriting (head);
18031 if (i >= 0)
18032 {
18033 BUILD_GLYPH_STRINGS (i, start, h, t,
18034 DRAW_NORMAL_TEXT, dummy_x, last_x);
18035 for (s = h; s; s = s->next)
18036 s->background_filled_p = 1;
18037 compute_overhangs_and_x (t, head->x, 1);
18038 prepend_glyph_string_lists (&head, &tail, h, t);
18039 }
18040
18041 /* Append glyphs strings for glyphs following the last glyph
18042 string tail that are overwritten by tail. The background of
18043 these strings has to be drawn because tail's foreground draws
18044 over it. */
18045 i = right_overwritten (tail);
18046 if (i >= 0)
18047 {
18048 BUILD_GLYPH_STRINGS (end, i, h, t,
18049 DRAW_NORMAL_TEXT, x, last_x);
18050 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18051 append_glyph_string_lists (&head, &tail, h, t);
18052 }
18053
18054 /* Append glyph strings for glyphs following the last glyph
18055 string tail that overwrite tail. The foreground of such
18056 glyphs has to be drawn because it writes into the background
18057 of tail. The background must not be drawn because it could
18058 paint over the foreground of following glyphs. */
18059 i = right_overwriting (tail);
18060 if (i >= 0)
18061 {
18062 BUILD_GLYPH_STRINGS (end, i, h, t,
18063 DRAW_NORMAL_TEXT, x, last_x);
18064 for (s = h; s; s = s->next)
18065 s->background_filled_p = 1;
18066 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18067 append_glyph_string_lists (&head, &tail, h, t);
18068 }
18069 }
18070
18071 /* Draw all strings. */
18072 for (s = head; s; s = s->next)
18073 rif->draw_glyph_string (s);
18074
18075 if (area == TEXT_AREA
18076 && !row->full_width_p
18077 /* When drawing overlapping rows, only the glyph strings'
18078 foreground is drawn, which doesn't erase a cursor
18079 completely. */
18080 && !overlaps_p)
18081 {
18082 int x0 = head ? head->x : x;
18083 int x1 = tail ? tail->x + tail->background_width : x;
18084
da8b7f4f
KS
18085 int text_left = window_box_left (w, TEXT_AREA);
18086 x0 -= text_left;
18087 x1 -= text_left;
133c764e 18088
da8b7f4f 18089 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
133c764e
KS
18090 row->y, MATRIX_ROW_BOTTOM_Y (row));
18091 }
18092
18093 /* Value is the x-position up to which drawn, relative to AREA of W.
18094 This doesn't include parts drawn because of overhangs. */
da8b7f4f
KS
18095 if (row->full_width_p)
18096 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18097 else
18098 x_reached -= window_box_left (w, area);
133c764e
KS
18099
18100 RELEASE_HDC (hdc, f);
18101
18102 return x_reached;
18103}
18104
18105
18106/* Store one glyph for IT->char_to_display in IT->glyph_row.
18107 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18108
18109static INLINE void
18110append_glyph (it)
18111 struct it *it;
18112{
18113 struct glyph *glyph;
18114 enum glyph_row_area area = it->area;
18115
18116 xassert (it->glyph_row);
18117 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18118
18119 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18120 if (glyph < it->glyph_row->glyphs[area + 1])
18121 {
18122 glyph->charpos = CHARPOS (it->position);
18123 glyph->object = it->object;
18124 glyph->pixel_width = it->pixel_width;
493fdc3c
KS
18125 glyph->ascent = it->ascent;
18126 glyph->descent = it->descent;
133c764e
KS
18127 glyph->voffset = it->voffset;
18128 glyph->type = CHAR_GLYPH;
18129 glyph->multibyte_p = it->multibyte_p;
18130 glyph->left_box_line_p = it->start_of_box_run_p;
18131 glyph->right_box_line_p = it->end_of_box_run_p;
18132 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18133 || it->phys_descent > it->descent);
18134 glyph->padding_p = 0;
18135 glyph->glyph_not_available_p = it->glyph_not_available_p;
18136 glyph->face_id = it->face_id;
18137 glyph->u.ch = it->char_to_display;
2883e85a 18138 glyph->slice = null_glyph_slice;
133c764e
KS
18139 glyph->font_type = FONT_TYPE_UNKNOWN;
18140 ++it->glyph_row->used[area];
18141 }
cf4a901e
KS
18142 else if (!fonts_changed_p)
18143 {
18144 it->w->ncols_scale_factor++;
18145 fonts_changed_p = 1;
18146 }
133c764e
KS
18147}
18148
18149/* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18150 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18151
18152static INLINE void
18153append_composite_glyph (it)
18154 struct it *it;
18155{
18156 struct glyph *glyph;
18157 enum glyph_row_area area = it->area;
18158
18159 xassert (it->glyph_row);
18160
18161 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18162 if (glyph < it->glyph_row->glyphs[area + 1])
18163 {
18164 glyph->charpos = CHARPOS (it->position);
18165 glyph->object = it->object;
18166 glyph->pixel_width = it->pixel_width;
493fdc3c
KS
18167 glyph->ascent = it->ascent;
18168 glyph->descent = it->descent;
133c764e
KS
18169 glyph->voffset = it->voffset;
18170 glyph->type = COMPOSITE_GLYPH;
18171 glyph->multibyte_p = it->multibyte_p;
18172 glyph->left_box_line_p = it->start_of_box_run_p;
18173 glyph->right_box_line_p = it->end_of_box_run_p;
18174 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18175 || it->phys_descent > it->descent);
18176 glyph->padding_p = 0;
18177 glyph->glyph_not_available_p = 0;
18178 glyph->face_id = it->face_id;
18179 glyph->u.cmp_id = it->cmp_id;
2883e85a 18180 glyph->slice = null_glyph_slice;
133c764e
KS
18181 glyph->font_type = FONT_TYPE_UNKNOWN;
18182 ++it->glyph_row->used[area];
18183 }
cf4a901e
KS
18184 else if (!fonts_changed_p)
18185 {
18186 it->w->ncols_scale_factor++;
18187 fonts_changed_p = 1;
18188 }
133c764e
KS
18189}
18190
18191
18192/* Change IT->ascent and IT->height according to the setting of
18193 IT->voffset. */
18194
18195static INLINE void
18196take_vertical_position_into_account (it)
18197 struct it *it;
18198{
18199 if (it->voffset)
18200 {
18201 if (it->voffset < 0)
18202 /* Increase the ascent so that we can display the text higher
18203 in the line. */
e893970b 18204 it->ascent -= it->voffset;
133c764e
KS
18205 else
18206 /* Increase the descent so that we can display the text lower
18207 in the line. */
18208 it->descent += it->voffset;
18209 }
18210}
18211
18212
18213/* Produce glyphs/get display metrics for the image IT is loaded with.
18214 See the description of struct display_iterator in dispextern.h for
18215 an overview of struct display_iterator. */
18216
18217static void
18218produce_image_glyph (it)
18219 struct it *it;
18220{
18221 struct image *img;
18222 struct face *face;
493fdc3c 18223 int face_ascent, glyph_ascent;
e893970b 18224 struct glyph_slice slice;
133c764e
KS
18225
18226 xassert (it->what == IT_IMAGE);
18227
18228 face = FACE_FROM_ID (it->f, it->face_id);
dd341dd9
KS
18229 xassert (face);
18230 /* Make sure X resources of the face is loaded. */
18231 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18232
18233 if (it->image_id < 0)
18234 {
18235 /* Fringe bitmap. */
351b5434
KS
18236 it->ascent = it->phys_ascent = 0;
18237 it->descent = it->phys_descent = 0;
18238 it->pixel_width = 0;
dd341dd9
KS
18239 it->nglyphs = 0;
18240 return;
18241 }
18242
133c764e
KS
18243 img = IMAGE_FROM_ID (it->f, it->image_id);
18244 xassert (img);
dd341dd9 18245 /* Make sure X resources of the image is loaded. */
133c764e
KS
18246 prepare_image_for_display (it->f, img);
18247
e893970b
KS
18248 slice.x = slice.y = 0;
18249 slice.width = img->width;
18250 slice.height = img->height;
18251
18252 if (INTEGERP (it->slice.x))
18253 slice.x = XINT (it->slice.x);
18254 else if (FLOATP (it->slice.x))
18255 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
18256
18257 if (INTEGERP (it->slice.y))
18258 slice.y = XINT (it->slice.y);
18259 else if (FLOATP (it->slice.y))
18260 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
18261
18262 if (INTEGERP (it->slice.width))
18263 slice.width = XINT (it->slice.width);
18264 else if (FLOATP (it->slice.width))
18265 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
18266
18267 if (INTEGERP (it->slice.height))
18268 slice.height = XINT (it->slice.height);
18269 else if (FLOATP (it->slice.height))
18270 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
18271
18272 if (slice.x >= img->width)
18273 slice.x = img->width;
18274 if (slice.y >= img->height)
18275 slice.y = img->height;
18276 if (slice.x + slice.width >= img->width)
18277 slice.width = img->width - slice.x;
18278 if (slice.y + slice.height > img->height)
18279 slice.height = img->height - slice.y;
18280
18281 if (slice.width == 0 || slice.height == 0)
18282 return;
18283
18284 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
18285
18286 it->descent = slice.height - glyph_ascent;
18287 if (slice.y == 0)
18288 it->descent += img->vmargin;
18289 if (slice.y + slice.height == img->height)
18290 it->descent += img->vmargin;
18291 it->phys_descent = it->descent;
18292
18293 it->pixel_width = slice.width;
18294 if (slice.x == 0)
18295 it->pixel_width += img->hmargin;
18296 if (slice.x + slice.width == img->width)
18297 it->pixel_width += img->hmargin;
133c764e 18298
7d8a0b55
MB
18299 /* It's quite possible for images to have an ascent greater than
18300 their height, so don't get confused in that case. */
18301 if (it->descent < 0)
18302 it->descent = 0;
18303
e893970b 18304#if 0 /* this breaks image tiling */
493fdc3c
KS
18305 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
18306 face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
18307 if (face_ascent > it->ascent)
18308 it->ascent = it->phys_ascent = face_ascent;
e893970b 18309#endif
493fdc3c 18310
133c764e
KS
18311 it->nglyphs = 1;
18312
18313 if (face->box != FACE_NO_BOX)
18314 {
18315 if (face->box_line_width > 0)
18316 {
e893970b
KS
18317 if (slice.y == 0)
18318 it->ascent += face->box_line_width;
18319 if (slice.y + slice.height == img->height)
18320 it->descent += face->box_line_width;
133c764e
KS
18321 }
18322
e893970b 18323 if (it->start_of_box_run_p && slice.x == 0)
133c764e 18324 it->pixel_width += abs (face->box_line_width);
e893970b 18325 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
133c764e
KS
18326 it->pixel_width += abs (face->box_line_width);
18327 }
18328
18329 take_vertical_position_into_account (it);
18330
18331 if (it->glyph_row)
18332 {
18333 struct glyph *glyph;
18334 enum glyph_row_area area = it->area;
18335
18336 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18337 if (glyph < it->glyph_row->glyphs[area + 1])
18338 {
18339 glyph->charpos = CHARPOS (it->position);
18340 glyph->object = it->object;
18341 glyph->pixel_width = it->pixel_width;
493fdc3c
KS
18342 glyph->ascent = glyph_ascent;
18343 glyph->descent = it->descent;
133c764e
KS
18344 glyph->voffset = it->voffset;
18345 glyph->type = IMAGE_GLYPH;
18346 glyph->multibyte_p = it->multibyte_p;
18347 glyph->left_box_line_p = it->start_of_box_run_p;
18348 glyph->right_box_line_p = it->end_of_box_run_p;
18349 glyph->overlaps_vertically_p = 0;
18350 glyph->padding_p = 0;
18351 glyph->glyph_not_available_p = 0;
18352 glyph->face_id = it->face_id;
18353 glyph->u.img_id = img->id;
e893970b 18354 glyph->slice = slice;
133c764e
KS
18355 glyph->font_type = FONT_TYPE_UNKNOWN;
18356 ++it->glyph_row->used[area];
18357 }
cf4a901e
KS
18358 else if (!fonts_changed_p)
18359 {
18360 it->w->ncols_scale_factor++;
18361 fonts_changed_p = 1;
18362 }
133c764e
KS
18363 }
18364}
18365
18366
18367/* Append a stretch glyph to IT->glyph_row. OBJECT is the source
18368 of the glyph, WIDTH and HEIGHT are the width and height of the
f65536fa 18369 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
133c764e
KS
18370
18371static void
18372append_stretch_glyph (it, object, width, height, ascent)
18373 struct it *it;
18374 Lisp_Object object;
18375 int width, height;
f65536fa 18376 int ascent;
133c764e
KS
18377{
18378 struct glyph *glyph;
18379 enum glyph_row_area area = it->area;
18380
f65536fa 18381 xassert (ascent >= 0 && ascent <= height);
133c764e
KS
18382
18383 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18384 if (glyph < it->glyph_row->glyphs[area + 1])
18385 {
18386 glyph->charpos = CHARPOS (it->position);
18387 glyph->object = object;
18388 glyph->pixel_width = width;
493fdc3c
KS
18389 glyph->ascent = ascent;
18390 glyph->descent = height - ascent;
133c764e
KS
18391 glyph->voffset = it->voffset;
18392 glyph->type = STRETCH_GLYPH;
18393 glyph->multibyte_p = it->multibyte_p;
18394 glyph->left_box_line_p = it->start_of_box_run_p;
18395 glyph->right_box_line_p = it->end_of_box_run_p;
18396 glyph->overlaps_vertically_p = 0;
18397 glyph->padding_p = 0;
18398 glyph->glyph_not_available_p = 0;
18399 glyph->face_id = it->face_id;
f65536fa 18400 glyph->u.stretch.ascent = ascent;
133c764e 18401 glyph->u.stretch.height = height;
2883e85a 18402 glyph->slice = null_glyph_slice;
133c764e
KS
18403 glyph->font_type = FONT_TYPE_UNKNOWN;
18404 ++it->glyph_row->used[area];
18405 }
cf4a901e
KS
18406 else if (!fonts_changed_p)
18407 {
18408 it->w->ncols_scale_factor++;
18409 fonts_changed_p = 1;
18410 }
133c764e
KS
18411}
18412
18413
18414/* Produce a stretch glyph for iterator IT. IT->object is the value
18415 of the glyph property displayed. The value must be a list
18416 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
18417 being recognized:
18418
18419 1. `:width WIDTH' specifies that the space should be WIDTH *
18420 canonical char width wide. WIDTH may be an integer or floating
18421 point number.
18422
18423 2. `:relative-width FACTOR' specifies that the width of the stretch
18424 should be computed from the width of the first character having the
18425 `glyph' property, and should be FACTOR times that width.
18426
18427 3. `:align-to HPOS' specifies that the space should be wide enough
18428 to reach HPOS, a value in canonical character units.
18429
18430 Exactly one of the above pairs must be present.
18431
18432 4. `:height HEIGHT' specifies that the height of the stretch produced
18433 should be HEIGHT, measured in canonical character units.
18434
18435 5. `:relative-height FACTOR' specifies that the height of the
18436 stretch should be FACTOR times the height of the characters having
18437 the glyph property.
18438
18439 Either none or exactly one of 4 or 5 must be present.
18440
18441 6. `:ascent ASCENT' specifies that ASCENT percent of the height
18442 of the stretch should be used for the ascent of the stretch.
18443 ASCENT must be in the range 0 <= ASCENT <= 100. */
18444
133c764e
KS
18445static void
18446produce_stretch_glyph (it)
18447 struct it *it;
18448{
f65536fa 18449 /* (space :width WIDTH :height HEIGHT ...) */
133c764e 18450 Lisp_Object prop, plist;
da06e4d7 18451 int width = 0, height = 0, align_to = -1;
f65536fa
KS
18452 int zero_width_ok_p = 0, zero_height_ok_p = 0;
18453 int ascent = 0;
18454 double tem;
133c764e
KS
18455 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18456 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
18457
18458 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18459
18460 /* List should start with `space'. */
18461 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
18462 plist = XCDR (it->object);
18463
18464 /* Compute the width of the stretch. */
f65536fa 18465 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
da06e4d7 18466 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
f65536fa
KS
18467 {
18468 /* Absolute width `:width WIDTH' specified and valid. */
18469 zero_width_ok_p = 1;
18470 width = (int)tem;
18471 }
133c764e
KS
18472 else if (prop = Fplist_get (plist, QCrelative_width),
18473 NUMVAL (prop) > 0)
18474 {
18475 /* Relative width `:relative-width FACTOR' specified and valid.
18476 Compute the width of the characters having the `glyph'
18477 property. */
18478 struct it it2;
18479 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
18480
18481 it2 = *it;
18482 if (it->multibyte_p)
18483 {
18484 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
18485 - IT_BYTEPOS (*it));
18486 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
18487 }
18488 else
18489 it2.c = *p, it2.len = 1;
18490
18491 it2.glyph_row = NULL;
18492 it2.what = IT_CHARACTER;
18493 x_produce_glyphs (&it2);
18494 width = NUMVAL (prop) * it2.pixel_width;
18495 }
f65536fa 18496 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
da06e4d7
KS
18497 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
18498 {
18499 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
be21b925 18500 align_to = (align_to < 0
da06e4d7
KS
18501 ? 0
18502 : align_to - window_box_left_offset (it->w, TEXT_AREA));
18503 else if (align_to < 0)
18504 align_to = window_box_left_offset (it->w, TEXT_AREA);
18505 width = max (0, (int)tem + align_to - it->current_x);
f65536fa
KS
18506 zero_width_ok_p = 1;
18507 }
133c764e
KS
18508 else
18509 /* Nothing specified -> width defaults to canonical char width. */
da8b7f4f 18510 width = FRAME_COLUMN_WIDTH (it->f);
133c764e 18511
f65536fa
KS
18512 if (width <= 0 && (width < 0 || !zero_width_ok_p))
18513 width = 1;
18514
133c764e 18515 /* Compute height. */
f65536fa 18516 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
da06e4d7 18517 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
f65536fa
KS
18518 {
18519 height = (int)tem;
18520 zero_height_ok_p = 1;
18521 }
133c764e
KS
18522 else if (prop = Fplist_get (plist, QCrelative_height),
18523 NUMVAL (prop) > 0)
18524 height = FONT_HEIGHT (font) * NUMVAL (prop);
18525 else
18526 height = FONT_HEIGHT (font);
18527
f65536fa
KS
18528 if (height <= 0 && (height < 0 || !zero_height_ok_p))
18529 height = 1;
18530
133c764e
KS
18531 /* Compute percentage of height used for ascent. If
18532 `:ascent ASCENT' is present and valid, use that. Otherwise,
18533 derive the ascent from the font in use. */
18534 if (prop = Fplist_get (plist, QCascent),
18535 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
f65536fa
KS
18536 ascent = height * NUMVAL (prop) / 100.0;
18537 else if (!NILP (prop)
da06e4d7 18538 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
f65536fa 18539 ascent = min (max (0, (int)tem), height);
133c764e 18540 else
f65536fa 18541 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
133c764e 18542
f65536fa 18543 if (width > 0 && height > 0 && it->glyph_row)
133c764e
KS
18544 {
18545 Lisp_Object object = it->stack[it->sp - 1].string;
18546 if (!STRINGP (object))
18547 object = it->w->buffer;
18548 append_stretch_glyph (it, object, width, height, ascent);
18549 }
18550
18551 it->pixel_width = width;
f65536fa 18552 it->ascent = it->phys_ascent = ascent;
133c764e 18553 it->descent = it->phys_descent = height - it->ascent;
f65536fa 18554 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
133c764e 18555
f65536fa 18556 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
133c764e
KS
18557 {
18558 if (face->box_line_width > 0)
18559 {
18560 it->ascent += face->box_line_width;
18561 it->descent += face->box_line_width;
18562 }
18563
18564 if (it->start_of_box_run_p)
18565 it->pixel_width += abs (face->box_line_width);
18566 if (it->end_of_box_run_p)
18567 it->pixel_width += abs (face->box_line_width);
18568 }
18569
18570 take_vertical_position_into_account (it);
18571}
18572
a9e5c932
KS
18573/* Calculate line-height and line-spacing properties.
18574 An integer value specifies explicit pixel value.
18575 A float value specifies relative value to current face height.
18576 A cons (float . face-name) specifies relative value to
18577 height of specified face font.
18578
18579 Returns height in pixels, or nil. */
18580
18581static Lisp_Object
17b01ffc 18582calc_line_height_property (it, prop, font, boff, total)
a9e5c932
KS
18583 struct it *it;
18584 Lisp_Object prop;
18585 XFontStruct *font;
17b01ffc 18586 int boff, *total;
a9e5c932 18587{
7e1704d0 18588 Lisp_Object position, val;
a9e5c932
KS
18589 Lisp_Object face_name = Qnil;
18590 int ascent, descent, height, override;
18591
7e1704d0
KS
18592 if (STRINGP (it->object))
18593 position = make_number (IT_STRING_CHARPOS (*it));
18594 else
18595 position = make_number (IT_CHARPOS (*it));
18596
18597 val = Fget_char_property (position, prop, it->object);
a9e5c932
KS
18598
18599 if (NILP (val))
18600 return val;
18601
17b01ffc
KS
18602 if (total && CONSP (val) && EQ (XCAR (val), Qtotal))
18603 {
18604 *total = 1;
18605 val = XCDR (val);
18606 }
18607
a9e5c932
KS
18608 if (INTEGERP (val))
18609 return val;
18610
18611 if (CONSP (val))
18612 {
18613 face_name = XCDR (val);
18614 val = XCAR (val);
18615 }
18616 else if (SYMBOLP (val))
18617 {
18618 face_name = val;
18619 val = Qnil;
18620 }
18621
18622 override = EQ (prop, Qline_height);
18623
18624 if (NILP (face_name))
18625 {
18626 font = FRAME_FONT (it->f);
18627 boff = FRAME_BASELINE_OFFSET (it->f);
18628 }
18629 else if (EQ (face_name, Qt))
18630 {
18631 override = 0;
18632 }
18633 else
18634 {
18635 int face_id;
18636 struct face *face;
18637 struct font_info *font_info;
18638
18639 face_id = lookup_named_face (it->f, face_name, ' ');
18640 if (face_id < 0)
d9bf7950 18641 return make_number (-1);
a9e5c932
KS
18642
18643 face = FACE_FROM_ID (it->f, face_id);
18644 font = face->font;
18645 if (font == NULL)
d9bf7950 18646 return make_number (-1);
a9e5c932
KS
18647
18648 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18649 boff = font_info->baseline_offset;
18650 if (font_info->vertical_centering)
18651 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18652 }
18653
18654 ascent = FONT_BASE (font) + boff;
18655 descent = FONT_DESCENT (font) - boff;
18656
18657 if (override)
18658 {
18659 it->override_ascent = ascent;
18660 it->override_descent = descent;
18661 it->override_boff = boff;
18662 }
18663
18664 height = ascent + descent;
18665 if (FLOATP (val))
18666 height = (int)(XFLOAT_DATA (val) * height);
18667 else if (INTEGERP (val))
18668 height *= XINT (val);
18669
18670 return make_number (height);
18671}
18672
18673
133c764e
KS
18674/* RIF:
18675 Produce glyphs/get display metrics for the display element IT is
18676 loaded with. See the description of struct display_iterator in
18677 dispextern.h for an overview of struct display_iterator. */
18678
18679void
18680x_produce_glyphs (it)
18681 struct it *it;
18682{
03e35edc
KS
18683 int extra_line_spacing = it->extra_line_spacing;
18684
133c764e
KS
18685 it->glyph_not_available_p = 0;
18686
18687 if (it->what == IT_CHARACTER)
18688 {
18689 XChar2b char2b;
18690 XFontStruct *font;
18691 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18692 XCharStruct *pcm;
18693 int font_not_found_p;
18694 struct font_info *font_info;
18695 int boff; /* baseline offset */
18696 /* We may change it->multibyte_p upon unibyte<->multibyte
18697 conversion. So, save the current value now and restore it
18698 later.
18699
18700 Note: It seems that we don't have to record multibyte_p in
18701 struct glyph because the character code itself tells if or
18702 not the character is multibyte. Thus, in the future, we must
18703 consider eliminating the field `multibyte_p' in the struct
18704 glyph. */
18705 int saved_multibyte_p = it->multibyte_p;
18706
18707 /* Maybe translate single-byte characters to multibyte, or the
18708 other way. */
18709 it->char_to_display = it->c;
18710 if (!ASCII_BYTE_P (it->c))
18711 {
18712 if (unibyte_display_via_language_environment
18713 && SINGLE_BYTE_CHAR_P (it->c)
18714 && (it->c >= 0240
18715 || !NILP (Vnonascii_translation_table)))
18716 {
18717 it->char_to_display = unibyte_char_to_multibyte (it->c);
18718 it->multibyte_p = 1;
18719 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18720 face = FACE_FROM_ID (it->f, it->face_id);
18721 }
18722 else if (!SINGLE_BYTE_CHAR_P (it->c)
18723 && !it->multibyte_p)
18724 {
18725 it->multibyte_p = 1;
18726 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18727 face = FACE_FROM_ID (it->f, it->face_id);
18728 }
18729 }
18730
18731 /* Get font to use. Encode IT->char_to_display. */
18732 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18733 &char2b, it->multibyte_p, 0);
18734 font = face->font;
18735
18736 /* When no suitable font found, use the default font. */
18737 font_not_found_p = font == NULL;
18738 if (font_not_found_p)
18739 {
18740 font = FRAME_FONT (it->f);
18741 boff = FRAME_BASELINE_OFFSET (it->f);
18742 font_info = NULL;
18743 }
18744 else
18745 {
18746 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18747 boff = font_info->baseline_offset;
18748 if (font_info->vertical_centering)
18749 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18750 }
18751
18752 if (it->char_to_display >= ' '
18753 && (!it->multibyte_p || it->char_to_display < 128))
18754 {
18755 /* Either unibyte or ASCII. */
18756 int stretched_p;
18757
18758 it->nglyphs = 1;
18759
18760 pcm = rif->per_char_metric (font, &char2b,
18761 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
e893970b 18762
a9e5c932
KS
18763 if (it->override_ascent >= 0)
18764 {
18765 it->ascent = it->override_ascent;
18766 it->descent = it->override_descent;
18767 boff = it->override_boff;
18768 }
18769 else
18770 {
18771 it->ascent = FONT_BASE (font) + boff;
18772 it->descent = FONT_DESCENT (font) - boff;
18773 }
133c764e
KS
18774
18775 if (pcm)
18776 {
18777 it->phys_ascent = pcm->ascent + boff;
18778 it->phys_descent = pcm->descent - boff;
18779 it->pixel_width = pcm->width;
18780 }
18781 else
18782 {
18783 it->glyph_not_available_p = 1;
e893970b
KS
18784 it->phys_ascent = it->ascent;
18785 it->phys_descent = it->descent;
133c764e
KS
18786 it->pixel_width = FONT_WIDTH (font);
18787 }
18788
e893970b
KS
18789 if (it->constrain_row_ascent_descent_p)
18790 {
18791 if (it->descent > it->max_descent)
4933c603
KS
18792 {
18793 it->ascent += it->descent - it->max_descent;
18794 it->descent = it->max_descent;
18795 }
18796 if (it->ascent > it->max_ascent)
18797 {
18798 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
18799 it->ascent = it->max_ascent;
18800 }
18801 it->phys_ascent = min (it->phys_ascent, it->ascent);
18802 it->phys_descent = min (it->phys_descent, it->descent);
18803 extra_line_spacing = 0;
18804 }
e893970b 18805
133c764e
KS
18806 /* If this is a space inside a region of text with
18807 `space-width' property, change its width. */
18808 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
18809 if (stretched_p)
18810 it->pixel_width *= XFLOATINT (it->space_width);
18811
18812 /* If face has a box, add the box thickness to the character
18813 height. If character has a box line to the left and/or
18814 right, add the box line width to the character's width. */
18815 if (face->box != FACE_NO_BOX)
18816 {
18817 int thick = face->box_line_width;
18818
18819 if (thick > 0)
18820 {
18821 it->ascent += thick;
18822 it->descent += thick;
18823 }
18824 else
18825 thick = -thick;
18826
18827 if (it->start_of_box_run_p)
18828 it->pixel_width += thick;
18829 if (it->end_of_box_run_p)
18830 it->pixel_width += thick;
18831 }
18832
18833 /* If face has an overline, add the height of the overline
18834 (1 pixel) and a 1 pixel margin to the character height. */
18835 if (face->overline_p)
18836 it->ascent += 2;
18837
e893970b
KS
18838 if (it->constrain_row_ascent_descent_p)
18839 {
18840 if (it->ascent > it->max_ascent)
18841 it->ascent = it->max_ascent;
18842 if (it->descent > it->max_descent)
18843 it->descent = it->max_descent;
18844 }
18845
133c764e
KS
18846 take_vertical_position_into_account (it);
18847
18848 /* If we have to actually produce glyphs, do it. */
18849 if (it->glyph_row)
18850 {
18851 if (stretched_p)
18852 {
18853 /* Translate a space with a `space-width' property
18854 into a stretch glyph. */
f65536fa
KS
18855 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
18856 / FONT_HEIGHT (font));
133c764e
KS
18857 append_stretch_glyph (it, it->object, it->pixel_width,
18858 it->ascent + it->descent, ascent);
18859 }
18860 else
18861 append_glyph (it);
18862
18863 /* If characters with lbearing or rbearing are displayed
18864 in this line, record that fact in a flag of the
18865 glyph row. This is used to optimize X output code. */
18866 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
18867 it->glyph_row->contains_overlapping_glyphs_p = 1;
18868 }
18869 }
18870 else if (it->char_to_display == '\n')
18871 {
e893970b
KS
18872 /* A newline has no width but we need the height of the line.
18873 But if previous part of the line set a height, don't
18874 increase that height */
18875
17b01ffc 18876 Lisp_Object height;
03e35edc 18877
a9e5c932 18878 it->override_ascent = -1;
133c764e
KS
18879 it->pixel_width = 0;
18880 it->nglyphs = 0;
133c764e 18881
17b01ffc 18882 height = calc_line_height_property(it, Qline_height, font, boff, 0);
4933c603 18883
a9e5c932 18884 if (it->override_ascent >= 0)
4933c603 18885 {
a9e5c932
KS
18886 it->ascent = it->override_ascent;
18887 it->descent = it->override_descent;
18888 boff = it->override_boff;
18889 }
18890 else
18891 {
18892 it->ascent = FONT_BASE (font) + boff;
18893 it->descent = FONT_DESCENT (font) - boff;
4933c603 18894 }
03e35edc 18895
a9e5c932 18896 if (EQ (height, make_number(0)))
e893970b 18897 {
03e35edc
KS
18898 if (it->descent > it->max_descent)
18899 {
18900 it->ascent += it->descent - it->max_descent;
18901 it->descent = it->max_descent;
18902 }
4933c603 18903 if (it->ascent > it->max_ascent)
03e35edc
KS
18904 {
18905 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
18906 it->ascent = it->max_ascent;
18907 }
18908 it->phys_ascent = min (it->phys_ascent, it->ascent);
18909 it->phys_descent = min (it->phys_descent, it->descent);
4933c603 18910 it->constrain_row_ascent_descent_p = 1;
03e35edc 18911 extra_line_spacing = 0;
e893970b
KS
18912 }
18913 else
18914 {
17b01ffc
KS
18915 Lisp_Object spacing;
18916 int total = 0;
18917
03e35edc
KS
18918 it->phys_ascent = it->ascent;
18919 it->phys_descent = it->descent;
e893970b 18920
03e35edc
KS
18921 if ((it->max_ascent > 0 || it->max_descent > 0)
18922 && face->box != FACE_NO_BOX
18923 && face->box_line_width > 0)
18924 {
18925 it->ascent += face->box_line_width;
18926 it->descent += face->box_line_width;
18927 }
a9e5c932
KS
18928 if (!NILP (height)
18929 && XINT (height) > it->ascent + it->descent)
18930 it->ascent = XINT (height) - it->descent;
4933c603 18931
17b01ffc
KS
18932 spacing = calc_line_height_property(it, Qline_spacing, font, boff, &total);
18933 if (INTEGERP (spacing))
18934 {
18935 extra_line_spacing = XINT (spacing);
18936 if (total)
18937 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
18938 }
a9e5c932 18939 }
133c764e
KS
18940 }
18941 else if (it->char_to_display == '\t')
18942 {
da8b7f4f 18943 int tab_width = it->tab_width * FRAME_COLUMN_WIDTH (it->f);
133c764e
KS
18944 int x = it->current_x + it->continuation_lines_width;
18945 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
18946
18947 /* If the distance from the current position to the next tab
18948 stop is less than a canonical character width, use the
18949 tab stop after that. */
da8b7f4f 18950 if (next_tab_x - x < FRAME_COLUMN_WIDTH (it->f))
133c764e
KS
18951 next_tab_x += tab_width;
18952
18953 it->pixel_width = next_tab_x - x;
18954 it->nglyphs = 1;
18955 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
18956 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
18957
18958 if (it->glyph_row)
18959 {
133c764e 18960 append_stretch_glyph (it, it->object, it->pixel_width,
f65536fa 18961 it->ascent + it->descent, it->ascent);
133c764e
KS
18962 }
18963 }
18964 else
18965 {
18966 /* A multi-byte character. Assume that the display width of the
18967 character is the width of the character multiplied by the
18968 width of the font. */
18969
18970 /* If we found a font, this font should give us the right
18971 metrics. If we didn't find a font, use the frame's
18972 default font and calculate the width of the character
18973 from the charset width; this is what old redisplay code
18974 did. */
18975
18976 pcm = rif->per_char_metric (font, &char2b,
18977 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
18978
18979 if (font_not_found_p || !pcm)
18980 {
18981 int charset = CHAR_CHARSET (it->char_to_display);
18982
18983 it->glyph_not_available_p = 1;
da8b7f4f 18984 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
133c764e
KS
18985 * CHARSET_WIDTH (charset));
18986 it->phys_ascent = FONT_BASE (font) + boff;
18987 it->phys_descent = FONT_DESCENT (font) - boff;
18988 }
18989 else
18990 {
18991 it->pixel_width = pcm->width;
18992 it->phys_ascent = pcm->ascent + boff;
18993 it->phys_descent = pcm->descent - boff;
18994 if (it->glyph_row
18995 && (pcm->lbearing < 0
18996 || pcm->rbearing > pcm->width))
18997 it->glyph_row->contains_overlapping_glyphs_p = 1;
18998 }
18999 it->nglyphs = 1;
19000 it->ascent = FONT_BASE (font) + boff;
19001 it->descent = FONT_DESCENT (font) - boff;
19002 if (face->box != FACE_NO_BOX)
19003 {
19004 int thick = face->box_line_width;
19005
19006 if (thick > 0)
19007 {
19008 it->ascent += thick;
19009 it->descent += thick;
19010 }
19011 else
19012 thick = - thick;
19013
19014 if (it->start_of_box_run_p)
19015 it->pixel_width += thick;
19016 if (it->end_of_box_run_p)
19017 it->pixel_width += thick;
19018 }
19019
19020 /* If face has an overline, add the height of the overline
19021 (1 pixel) and a 1 pixel margin to the character height. */
19022 if (face->overline_p)
19023 it->ascent += 2;
19024
19025 take_vertical_position_into_account (it);
19026
19027 if (it->glyph_row)
19028 append_glyph (it);
19029 }
19030 it->multibyte_p = saved_multibyte_p;
19031 }
19032 else if (it->what == IT_COMPOSITION)
19033 {
19034 /* Note: A composition is represented as one glyph in the
19035 glyph matrix. There are no padding glyphs. */
19036 XChar2b char2b;
19037 XFontStruct *font;
19038 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19039 XCharStruct *pcm;
19040 int font_not_found_p;
19041 struct font_info *font_info;
19042 int boff; /* baseline offset */
19043 struct composition *cmp = composition_table[it->cmp_id];
19044
19045 /* Maybe translate single-byte characters to multibyte. */
19046 it->char_to_display = it->c;
19047 if (unibyte_display_via_language_environment
19048 && SINGLE_BYTE_CHAR_P (it->c)
19049 && (it->c >= 0240
19050 || (it->c >= 0200
19051 && !NILP (Vnonascii_translation_table))))
19052 {
19053 it->char_to_display = unibyte_char_to_multibyte (it->c);
19054 }
19055
19056 /* Get face and font to use. Encode IT->char_to_display. */
19057 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19058 face = FACE_FROM_ID (it->f, it->face_id);
19059 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19060 &char2b, it->multibyte_p, 0);
19061 font = face->font;
19062
19063 /* When no suitable font found, use the default font. */
19064 font_not_found_p = font == NULL;
19065 if (font_not_found_p)
19066 {
19067 font = FRAME_FONT (it->f);
19068 boff = FRAME_BASELINE_OFFSET (it->f);
19069 font_info = NULL;
19070 }
19071 else
19072 {
19073 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19074 boff = font_info->baseline_offset;
19075 if (font_info->vertical_centering)
19076 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19077 }
19078
19079 /* There are no padding glyphs, so there is only one glyph to
19080 produce for the composition. Important is that pixel_width,
19081 ascent and descent are the values of what is drawn by
19082 draw_glyphs (i.e. the values of the overall glyphs composed). */
19083 it->nglyphs = 1;
19084
19085 /* If we have not yet calculated pixel size data of glyphs of
19086 the composition for the current face font, calculate them
19087 now. Theoretically, we have to check all fonts for the
19088 glyphs, but that requires much time and memory space. So,
19089 here we check only the font of the first glyph. This leads
19090 to incorrect display very rarely, and C-l (recenter) can
19091 correct the display anyway. */
19092 if (cmp->font != (void *) font)
19093 {
19094 /* Ascent and descent of the font of the first character of
19095 this composition (adjusted by baseline offset). Ascent
19096 and descent of overall glyphs should not be less than
19097 them respectively. */
19098 int font_ascent = FONT_BASE (font) + boff;
19099 int font_descent = FONT_DESCENT (font) - boff;
19100 /* Bounding box of the overall glyphs. */
19101 int leftmost, rightmost, lowest, highest;
19102 int i, width, ascent, descent;
19103
19104 cmp->font = (void *) font;
19105
19106 /* Initialize the bounding box. */
19107 if (font_info
19108 && (pcm = rif->per_char_metric (font, &char2b,
19109 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19110 {
19111 width = pcm->width;
19112 ascent = pcm->ascent;
19113 descent = pcm->descent;
19114 }
19115 else
19116 {
19117 width = FONT_WIDTH (font);
19118 ascent = FONT_BASE (font);
19119 descent = FONT_DESCENT (font);
19120 }
19121
19122 rightmost = width;
19123 lowest = - descent + boff;
19124 highest = ascent + boff;
19125 leftmost = 0;
19126
19127 if (font_info
19128 && font_info->default_ascent
19129 && CHAR_TABLE_P (Vuse_default_ascent)
19130 && !NILP (Faref (Vuse_default_ascent,
19131 make_number (it->char_to_display))))
19132 highest = font_info->default_ascent + boff;
19133
19134 /* Draw the first glyph at the normal position. It may be
19135 shifted to right later if some other glyphs are drawn at
19136 the left. */
19137 cmp->offsets[0] = 0;
19138 cmp->offsets[1] = boff;
19139
19140 /* Set cmp->offsets for the remaining glyphs. */
19141 for (i = 1; i < cmp->glyph_len; i++)
19142 {
19143 int left, right, btm, top;
19144 int ch = COMPOSITION_GLYPH (cmp, i);
19145 int face_id = FACE_FOR_CHAR (it->f, face, ch);
19146
19147 face = FACE_FROM_ID (it->f, face_id);
19148 get_char_face_and_encoding (it->f, ch, face->id,
19149 &char2b, it->multibyte_p, 0);
19150 font = face->font;
19151 if (font == NULL)
19152 {
19153 font = FRAME_FONT (it->f);
2a6d0874 19154 boff = FRAME_BASELINE_OFFSET (it->f);
133c764e
KS
19155 font_info = NULL;
19156 }
19157 else
19158 {
19159 font_info
19160 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19161 boff = font_info->baseline_offset;
19162 if (font_info->vertical_centering)
19163 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19164 }
19165
19166 if (font_info
2a6d0874 19167 && (pcm = rif->per_char_metric (font, &char2b,
133c764e
KS
19168 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19169 {
19170 width = pcm->width;
19171 ascent = pcm->ascent;
19172 descent = pcm->descent;
19173 }
19174 else
19175 {
19176 width = FONT_WIDTH (font);
19177 ascent = 1;
19178 descent = 0;
19179 }
19180
19181 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19182 {
19183 /* Relative composition with or without
19184 alternate chars. */
19185 left = (leftmost + rightmost - width) / 2;
19186 btm = - descent + boff;
19187 if (font_info && font_info->relative_compose
19188 && (! CHAR_TABLE_P (Vignore_relative_composition)
19189 || NILP (Faref (Vignore_relative_composition,
19190 make_number (ch)))))
19191 {
19192
19193 if (- descent >= font_info->relative_compose)
19194 /* One extra pixel between two glyphs. */
19195 btm = highest + 1;
19196 else if (ascent <= 0)
19197 /* One extra pixel between two glyphs. */
19198 btm = lowest - 1 - ascent - descent;
19199 }
19200 }
19201 else
19202 {
19203 /* A composition rule is specified by an integer
19204 value that encodes global and new reference
19205 points (GREF and NREF). GREF and NREF are
19206 specified by numbers as below:
19207
19208 0---1---2 -- ascent
19209 | |
19210 | |
19211 | |
19212 9--10--11 -- center
19213 | |
19214 ---3---4---5--- baseline
19215 | |
19216 6---7---8 -- descent
19217 */
19218 int rule = COMPOSITION_RULE (cmp, i);
19219 int gref, nref, grefx, grefy, nrefx, nrefy;
19220
19221 COMPOSITION_DECODE_RULE (rule, gref, nref);
19222 grefx = gref % 3, nrefx = nref % 3;
19223 grefy = gref / 3, nrefy = nref / 3;
19224
19225 left = (leftmost
19226 + grefx * (rightmost - leftmost) / 2
19227 - nrefx * width / 2);
19228 btm = ((grefy == 0 ? highest
19229 : grefy == 1 ? 0
19230 : grefy == 2 ? lowest
19231 : (highest + lowest) / 2)
19232 - (nrefy == 0 ? ascent + descent
19233 : nrefy == 1 ? descent - boff
19234 : nrefy == 2 ? 0
19235 : (ascent + descent) / 2));
19236 }
19237
19238 cmp->offsets[i * 2] = left;
19239 cmp->offsets[i * 2 + 1] = btm + descent;
19240
19241 /* Update the bounding box of the overall glyphs. */
19242 right = left + width;
19243 top = btm + descent + ascent;
19244 if (left < leftmost)
19245 leftmost = left;
19246 if (right > rightmost)
19247 rightmost = right;
19248 if (top > highest)
19249 highest = top;
19250 if (btm < lowest)
19251 lowest = btm;
19252 }
19253
19254 /* If there are glyphs whose x-offsets are negative,
19255 shift all glyphs to the right and make all x-offsets
19256 non-negative. */
19257 if (leftmost < 0)
19258 {
19259 for (i = 0; i < cmp->glyph_len; i++)
19260 cmp->offsets[i * 2] -= leftmost;
19261 rightmost -= leftmost;
19262 }
19263
19264 cmp->pixel_width = rightmost;
19265 cmp->ascent = highest;
19266 cmp->descent = - lowest;
19267 if (cmp->ascent < font_ascent)
19268 cmp->ascent = font_ascent;
19269 if (cmp->descent < font_descent)
19270 cmp->descent = font_descent;
19271 }
19272
19273 it->pixel_width = cmp->pixel_width;
19274 it->ascent = it->phys_ascent = cmp->ascent;
19275 it->descent = it->phys_descent = cmp->descent;
19276
19277 if (face->box != FACE_NO_BOX)
19278 {
19279 int thick = face->box_line_width;
19280
19281 if (thick > 0)
19282 {
19283 it->ascent += thick;
19284 it->descent += thick;
19285 }
19286 else
19287 thick = - thick;
19288
19289 if (it->start_of_box_run_p)
19290 it->pixel_width += thick;
19291 if (it->end_of_box_run_p)
19292 it->pixel_width += thick;
19293 }
19294
19295 /* If face has an overline, add the height of the overline
19296 (1 pixel) and a 1 pixel margin to the character height. */
19297 if (face->overline_p)
19298 it->ascent += 2;
19299
19300 take_vertical_position_into_account (it);
19301
19302 if (it->glyph_row)
19303 append_composite_glyph (it);
19304 }
19305 else if (it->what == IT_IMAGE)
19306 produce_image_glyph (it);
19307 else if (it->what == IT_STRETCH)
19308 produce_stretch_glyph (it);
19309
19310 /* Accumulate dimensions. Note: can't assume that it->descent > 0
19311 because this isn't true for images with `:ascent 100'. */
19312 xassert (it->ascent >= 0 && it->descent >= 0);
19313 if (it->area == TEXT_AREA)
19314 it->current_x += it->pixel_width;
19315
a9e5c932
KS
19316 if (extra_line_spacing > 0)
19317 it->descent += extra_line_spacing;
133c764e
KS
19318
19319 it->max_ascent = max (it->max_ascent, it->ascent);
19320 it->max_descent = max (it->max_descent, it->descent);
19321 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
19322 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
19323}
19324
fa3c6b4d
KS
19325/* EXPORT for RIF:
19326 Output LEN glyphs starting at START at the nominal cursor position.
19327 Advance the nominal cursor over the text. The global variable
19328 updated_window contains the window being updated, updated_row is
19329 the glyph row being updated, and updated_area is the area of that
19330 row being updated. */
133c764e 19331
fa3c6b4d
KS
19332void
19333x_write_glyphs (start, len)
19334 struct glyph *start;
19335 int len;
19336{
19337 int x, hpos;
133c764e 19338
fa3c6b4d
KS
19339 xassert (updated_window && updated_row);
19340 BLOCK_INPUT;
133c764e 19341
fa3c6b4d 19342 /* Write glyphs. */
cfe03a41 19343
fa3c6b4d
KS
19344 hpos = start - updated_row->glyphs[updated_area];
19345 x = draw_glyphs (updated_window, output_cursor.x,
19346 updated_row, updated_area,
19347 hpos, hpos + len,
19348 DRAW_NORMAL_TEXT, 0);
cfe03a41 19349
fa3c6b4d
KS
19350 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
19351 if (updated_area == TEXT_AREA
19352 && updated_window->phys_cursor_on_p
19353 && updated_window->phys_cursor.vpos == output_cursor.vpos
19354 && updated_window->phys_cursor.hpos >= hpos
19355 && updated_window->phys_cursor.hpos < hpos + len)
19356 updated_window->phys_cursor_on_p = 0;
cfe03a41 19357
fa3c6b4d 19358 UNBLOCK_INPUT;
cfe03a41 19359
fa3c6b4d
KS
19360 /* Advance the output cursor. */
19361 output_cursor.hpos += len;
19362 output_cursor.x = x;
19363}
cfe03a41 19364
cfe03a41 19365
fa3c6b4d
KS
19366/* EXPORT for RIF:
19367 Insert LEN glyphs from START at the nominal cursor position. */
19368
19369void
19370x_insert_glyphs (start, len)
19371 struct glyph *start;
19372 int len;
19373{
19374 struct frame *f;
19375 struct window *w;
19376 int line_height, shift_by_width, shifted_region_width;
19377 struct glyph_row *row;
19378 struct glyph *glyph;
19379 int frame_x, frame_y, hpos;
19380
19381 xassert (updated_window && updated_row);
19382 BLOCK_INPUT;
19383 w = updated_window;
19384 f = XFRAME (WINDOW_FRAME (w));
19385
19386 /* Get the height of the line we are in. */
19387 row = updated_row;
19388 line_height = row->height;
19389
19390 /* Get the width of the glyphs to insert. */
19391 shift_by_width = 0;
19392 for (glyph = start; glyph < start + len; ++glyph)
19393 shift_by_width += glyph->pixel_width;
19394
19395 /* Get the width of the region to shift right. */
19396 shifted_region_width = (window_box_width (w, updated_area)
19397 - output_cursor.x
19398 - shift_by_width);
19399
19400 /* Shift right. */
19401 frame_x = window_box_left (w, updated_area) + output_cursor.x;
19402 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
19403
7b7b454e
AS
19404 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
19405 line_height, shift_by_width);
fa3c6b4d
KS
19406
19407 /* Write the glyphs. */
19408 hpos = start - row->glyphs[updated_area];
19409 draw_glyphs (w, output_cursor.x, row, updated_area,
19410 hpos, hpos + len,
19411 DRAW_NORMAL_TEXT, 0);
19412
19413 /* Advance the output cursor. */
19414 output_cursor.hpos += len;
19415 output_cursor.x += shift_by_width;
19416 UNBLOCK_INPUT;
19417}
19418
19419
19420/* EXPORT for RIF:
19421 Erase the current text line from the nominal cursor position
19422 (inclusive) to pixel column TO_X (exclusive). The idea is that
19423 everything from TO_X onward is already erased.
19424
19425 TO_X is a pixel position relative to updated_area of
19426 updated_window. TO_X == -1 means clear to the end of this area. */
19427
19428void
19429x_clear_end_of_line (to_x)
19430 int to_x;
19431{
19432 struct frame *f;
19433 struct window *w = updated_window;
19434 int max_x, min_y, max_y;
19435 int from_x, from_y, to_y;
19436
19437 xassert (updated_window && updated_row);
19438 f = XFRAME (w->frame);
19439
19440 if (updated_row->full_width_p)
da8b7f4f 19441 max_x = WINDOW_TOTAL_WIDTH (w);
fa3c6b4d
KS
19442 else
19443 max_x = window_box_width (w, updated_area);
19444 max_y = window_text_bottom_y (w);
19445
19446 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
19447 of window. For TO_X > 0, truncate to end of drawing area. */
19448 if (to_x == 0)
19449 return;
19450 else if (to_x < 0)
19451 to_x = max_x;
19452 else
19453 to_x = min (to_x, max_x);
19454
19455 to_y = min (max_y, output_cursor.y + updated_row->height);
19456
19457 /* Notice if the cursor will be cleared by this operation. */
19458 if (!updated_row->full_width_p)
19459 notice_overwritten_cursor (w, updated_area,
19460 output_cursor.x, -1,
19461 updated_row->y,
19462 MATRIX_ROW_BOTTOM_Y (updated_row));
19463
19464 from_x = output_cursor.x;
19465
19466 /* Translate to frame coordinates. */
19467 if (updated_row->full_width_p)
19468 {
19469 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
19470 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
19471 }
19472 else
19473 {
da8b7f4f
KS
19474 int area_left = window_box_left (w, updated_area);
19475 from_x += area_left;
19476 to_x += area_left;
fa3c6b4d
KS
19477 }
19478
da8b7f4f 19479 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
fa3c6b4d
KS
19480 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
19481 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
19482
19483 /* Prevent inadvertently clearing to end of the X window. */
19484 if (to_x > from_x && to_y > from_y)
19485 {
19486 BLOCK_INPUT;
19487 rif->clear_frame_area (f, from_x, from_y,
19488 to_x - from_x, to_y - from_y);
19489 UNBLOCK_INPUT;
19490 }
19491}
19492
19493#endif /* HAVE_WINDOW_SYSTEM */
19494
19495
19496\f
19497/***********************************************************************
19498 Cursor types
19499 ***********************************************************************/
19500
19501/* Value is the internal representation of the specified cursor type
19502 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
19503 of the bar cursor. */
19504
f65536fa 19505static enum text_cursor_kinds
fa3c6b4d
KS
19506get_specified_cursor_type (arg, width)
19507 Lisp_Object arg;
19508 int *width;
19509{
19510 enum text_cursor_kinds type;
19511
19512 if (NILP (arg))
19513 return NO_CURSOR;
19514
19515 if (EQ (arg, Qbox))
19516 return FILLED_BOX_CURSOR;
19517
19518 if (EQ (arg, Qhollow))
19519 return HOLLOW_BOX_CURSOR;
19520
19521 if (EQ (arg, Qbar))
19522 {
19523 *width = 2;
19524 return BAR_CURSOR;
19525 }
19526
19527 if (CONSP (arg)
19528 && EQ (XCAR (arg), Qbar)
19529 && INTEGERP (XCDR (arg))
19530 && XINT (XCDR (arg)) >= 0)
19531 {
19532 *width = XINT (XCDR (arg));
19533 return BAR_CURSOR;
19534 }
19535
19536 if (EQ (arg, Qhbar))
19537 {
19538 *width = 2;
19539 return HBAR_CURSOR;
19540 }
19541
19542 if (CONSP (arg)
19543 && EQ (XCAR (arg), Qhbar)
19544 && INTEGERP (XCDR (arg))
19545 && XINT (XCDR (arg)) >= 0)
19546 {
19547 *width = XINT (XCDR (arg));
19548 return HBAR_CURSOR;
19549 }
19550
19551 /* Treat anything unknown as "hollow box cursor".
19552 It was bad to signal an error; people have trouble fixing
19553 .Xdefaults with Emacs, when it has something bad in it. */
19554 type = HOLLOW_BOX_CURSOR;
19555
19556 return type;
19557}
19558
19559/* Set the default cursor types for specified frame. */
19560void
19561set_frame_cursor_types (f, arg)
19562 struct frame *f;
19563 Lisp_Object arg;
19564{
19565 int width;
19566 Lisp_Object tem;
19567
19568 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
19569 FRAME_CURSOR_WIDTH (f) = width;
19570
19571 /* By default, set up the blink-off state depending on the on-state. */
19572
19573 tem = Fassoc (arg, Vblink_cursor_alist);
19574 if (!NILP (tem))
19575 {
19576 FRAME_BLINK_OFF_CURSOR (f)
19577 = get_specified_cursor_type (XCDR (tem), &width);
19578 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
19579 }
19580 else
19581 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
19582}
19583
19584
19585/* Return the cursor we want to be displayed in window W. Return
19586 width of bar/hbar cursor through WIDTH arg. Return with
19587 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
19588 (i.e. if the `system caret' should track this cursor).
19589
19590 In a mini-buffer window, we want the cursor only to appear if we
19591 are reading input from this window. For the selected window, we
19592 want the cursor type given by the frame parameter or buffer local
19593 setting of cursor-type. If explicitly marked off, draw no cursor.
19594 In all other cases, we want a hollow box cursor. */
19595
f65536fa
KS
19596static enum text_cursor_kinds
19597get_window_cursor_type (w, glyph, width, active_cursor)
fa3c6b4d 19598 struct window *w;
f65536fa 19599 struct glyph *glyph;
fa3c6b4d
KS
19600 int *width;
19601 int *active_cursor;
19602{
19603 struct frame *f = XFRAME (w->frame);
19604 struct buffer *b = XBUFFER (w->buffer);
19605 int cursor_type = DEFAULT_CURSOR;
19606 Lisp_Object alt_cursor;
19607 int non_selected = 0;
19608
19609 *active_cursor = 1;
19610
19611 /* Echo area */
19612 if (cursor_in_echo_area
19613 && FRAME_HAS_MINIBUF_P (f)
19614 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
19615 {
19616 if (w == XWINDOW (echo_area_window))
19617 {
19618 *width = FRAME_CURSOR_WIDTH (f);
19619 return FRAME_DESIRED_CURSOR (f);
19620 }
19621
19622 *active_cursor = 0;
19623 non_selected = 1;
19624 }
19625
19626 /* Nonselected window or nonselected frame. */
19627 else if (w != XWINDOW (f->selected_window)
19628#ifdef HAVE_WINDOW_SYSTEM
19629 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
19630#endif
19631 )
19632 {
19633 *active_cursor = 0;
19634
19635 if (MINI_WINDOW_P (w) && minibuf_level == 0)
19636 return NO_CURSOR;
19637
19638 non_selected = 1;
19639 }
19640
19641 /* Never display a cursor in a window in which cursor-type is nil. */
19642 if (NILP (b->cursor_type))
19643 return NO_CURSOR;
19644
19645 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
19646 if (non_selected)
19647 {
19648 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
19649 return get_specified_cursor_type (alt_cursor, width);
19650 }
19651
19652 /* Get the normal cursor type for this window. */
19653 if (EQ (b->cursor_type, Qt))
19654 {
19655 cursor_type = FRAME_DESIRED_CURSOR (f);
19656 *width = FRAME_CURSOR_WIDTH (f);
19657 }
19658 else
19659 cursor_type = get_specified_cursor_type (b->cursor_type, width);
19660
19661 /* Use normal cursor if not blinked off. */
1d712183 19662 if (!w->cursor_off_p)
f65536fa 19663 {
1d712183 19664 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
f65536fa
KS
19665 if (cursor_type == FILLED_BOX_CURSOR)
19666 cursor_type = HOLLOW_BOX_CURSOR;
19667 }
19668 return cursor_type;
19669 }
fa3c6b4d
KS
19670
19671 /* Cursor is blinked off, so determine how to "toggle" it. */
19672
19673 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
19674 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
19675 return get_specified_cursor_type (XCDR (alt_cursor), width);
19676
19677 /* Then see if frame has specified a specific blink off cursor type. */
19678 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
19679 {
19680 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
19681 return FRAME_BLINK_OFF_CURSOR (f);
19682 }
19683
a57dd8b1
KS
19684#if 0
19685 /* Some people liked having a permanently visible blinking cursor,
19686 while others had very strong opinions against it. So it was
19687 decided to remove it. KFS 2003-09-03 */
19688
fa3c6b4d
KS
19689 /* Finally perform built-in cursor blinking:
19690 filled box <-> hollow box
19691 wide [h]bar <-> narrow [h]bar
19692 narrow [h]bar <-> no cursor
19693 other type <-> no cursor */
19694
19695 if (cursor_type == FILLED_BOX_CURSOR)
19696 return HOLLOW_BOX_CURSOR;
19697
19698 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
19699 {
19700 *width = 1;
19701 return cursor_type;
19702 }
a57dd8b1 19703#endif
fa3c6b4d
KS
19704
19705 return NO_CURSOR;
19706}
19707
19708
19709#ifdef HAVE_WINDOW_SYSTEM
19710
19711/* Notice when the text cursor of window W has been completely
19712 overwritten by a drawing operation that outputs glyphs in AREA
19713 starting at X0 and ending at X1 in the line starting at Y0 and
19714 ending at Y1. X coordinates are area-relative. X1 < 0 means all
19715 the rest of the line after X0 has been written. Y coordinates
19716 are window-relative. */
19717
19718static void
19719notice_overwritten_cursor (w, area, x0, x1, y0, y1)
19720 struct window *w;
19721 enum glyph_row_area area;
19722 int x0, y0, x1, y1;
19723{
88e6b646
KS
19724 int cx0, cx1, cy0, cy1;
19725 struct glyph_row *row;
fa3c6b4d 19726
88e6b646
KS
19727 if (!w->phys_cursor_on_p)
19728 return;
19729 if (area != TEXT_AREA)
19730 return;
fa3c6b4d 19731
88e6b646
KS
19732 row = w->current_matrix->rows + w->phys_cursor.vpos;
19733 if (!row->displays_text_p)
19734 return;
fa3c6b4d 19735
88e6b646
KS
19736 if (row->cursor_in_fringe_p)
19737 {
19738 row->cursor_in_fringe_p = 0;
19739 draw_fringe_bitmap (w, row, 0);
19740 w->phys_cursor_on_p = 0;
19741 return;
fa3c6b4d 19742 }
88e6b646
KS
19743
19744 cx0 = w->phys_cursor.x;
19745 cx1 = cx0 + w->phys_cursor_width;
19746 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
19747 return;
19748
19749 /* The cursor image will be completely removed from the
19750 screen if the output area intersects the cursor area in
19751 y-direction. When we draw in [y0 y1[, and some part of
19752 the cursor is at y < y0, that part must have been drawn
19753 before. When scrolling, the cursor is erased before
19754 actually scrolling, so we don't come here. When not
19755 scrolling, the rows above the old cursor row must have
19756 changed, and in this case these rows must have written
19757 over the cursor image.
19758
19759 Likewise if part of the cursor is below y1, with the
19760 exception of the cursor being in the first blank row at
19761 the buffer and window end because update_text_area
19762 doesn't draw that row. (Except when it does, but
19763 that's handled in update_text_area.) */
19764
19765 cy0 = w->phys_cursor.y;
19766 cy1 = cy0 + w->phys_cursor_height;
19767 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
19768 return;
19769
19770 w->phys_cursor_on_p = 0;
fa3c6b4d
KS
19771}
19772
19773#endif /* HAVE_WINDOW_SYSTEM */
19774
19775\f
19776/************************************************************************
19777 Mouse Face
19778 ************************************************************************/
19779
19780#ifdef HAVE_WINDOW_SYSTEM
19781
19782/* EXPORT for RIF:
19783 Fix the display of area AREA of overlapping row ROW in window W. */
19784
19785void
19786x_fix_overlapping_area (w, row, area)
19787 struct window *w;
19788 struct glyph_row *row;
19789 enum glyph_row_area area;
19790{
19791 int i, x;
19792
19793 BLOCK_INPUT;
19794
57dd22f4 19795 x = 0;
fa3c6b4d
KS
19796 for (i = 0; i < row->used[area];)
19797 {
19798 if (row->glyphs[area][i].overlaps_vertically_p)
19799 {
19800 int start = i, start_x = x;
19801
19802 do
19803 {
19804 x += row->glyphs[area][i].pixel_width;
19805 ++i;
19806 }
19807 while (i < row->used[area]
19808 && row->glyphs[area][i].overlaps_vertically_p);
19809
19810 draw_glyphs (w, start_x, row, area,
19811 start, i,
19812 DRAW_NORMAL_TEXT, 1);
19813 }
19814 else
19815 {
19816 x += row->glyphs[area][i].pixel_width;
19817 ++i;
19818 }
19819 }
19820
19821 UNBLOCK_INPUT;
19822}
19823
19824
19825/* EXPORT:
19826 Draw the cursor glyph of window W in glyph row ROW. See the
19827 comment of draw_glyphs for the meaning of HL. */
19828
19829void
19830draw_phys_cursor_glyph (w, row, hl)
19831 struct window *w;
19832 struct glyph_row *row;
19833 enum draw_glyphs_face hl;
19834{
19835 /* If cursor hpos is out of bounds, don't draw garbage. This can
19836 happen in mini-buffer windows when switching between echo area
19837 glyphs and mini-buffer. */
19838 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
19839 {
19840 int on_p = w->phys_cursor_on_p;
19841 int x1;
19842 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
19843 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
19844 hl, 0);
19845 w->phys_cursor_on_p = on_p;
19846
fa3c6b4d
KS
19847 if (hl == DRAW_CURSOR)
19848 w->phys_cursor_width = x1 - w->phys_cursor.x;
fa3c6b4d
KS
19849 /* When we erase the cursor, and ROW is overlapped by other
19850 rows, make sure that these overlapping parts of other rows
19851 are redrawn. */
365fa1b3 19852 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
fa3c6b4d
KS
19853 {
19854 if (row > w->current_matrix->rows
19855 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
19856 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
19857
19858 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
19859 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
19860 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
19861 }
19862 }
19863}
19864
19865
19866/* EXPORT:
19867 Erase the image of a cursor of window W from the screen. */
19868
19869void
19870erase_phys_cursor (w)
19871 struct window *w;
19872{
19873 struct frame *f = XFRAME (w->frame);
19874 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
19875 int hpos = w->phys_cursor.hpos;
19876 int vpos = w->phys_cursor.vpos;
19877 int mouse_face_here_p = 0;
19878 struct glyph_matrix *active_glyphs = w->current_matrix;
19879 struct glyph_row *cursor_row;
19880 struct glyph *cursor_glyph;
19881 enum draw_glyphs_face hl;
19882
19883 /* No cursor displayed or row invalidated => nothing to do on the
19884 screen. */
19885 if (w->phys_cursor_type == NO_CURSOR)
19886 goto mark_cursor_off;
19887
19888 /* VPOS >= active_glyphs->nrows means that window has been resized.
19889 Don't bother to erase the cursor. */
19890 if (vpos >= active_glyphs->nrows)
19891 goto mark_cursor_off;
19892
19893 /* If row containing cursor is marked invalid, there is nothing we
19894 can do. */
19895 cursor_row = MATRIX_ROW (active_glyphs, vpos);
19896 if (!cursor_row->enabled_p)
19897 goto mark_cursor_off;
19898
19899 /* If row is completely invisible, don't attempt to delete a cursor which
19900 isn't there. This can happen if cursor is at top of a window, and
19901 we switch to a buffer with a header line in that window. */
19902 if (cursor_row->visible_height <= 0)
19903 goto mark_cursor_off;
19904
88e6b646
KS
19905 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
19906 if (cursor_row->cursor_in_fringe_p)
19907 {
19908 cursor_row->cursor_in_fringe_p = 0;
19909 draw_fringe_bitmap (w, cursor_row, 0);
19910 goto mark_cursor_off;
19911 }
19912
fa3c6b4d
KS
19913 /* This can happen when the new row is shorter than the old one.
19914 In this case, either draw_glyphs or clear_end_of_line
19915 should have cleared the cursor. Note that we wouldn't be
19916 able to erase the cursor in this case because we don't have a
19917 cursor glyph at hand. */
19918 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
19919 goto mark_cursor_off;
19920
19921 /* If the cursor is in the mouse face area, redisplay that when
19922 we clear the cursor. */
19923 if (! NILP (dpyinfo->mouse_face_window)
19924 && w == XWINDOW (dpyinfo->mouse_face_window)
19925 && (vpos > dpyinfo->mouse_face_beg_row
19926 || (vpos == dpyinfo->mouse_face_beg_row
19927 && hpos >= dpyinfo->mouse_face_beg_col))
19928 && (vpos < dpyinfo->mouse_face_end_row
19929 || (vpos == dpyinfo->mouse_face_end_row
19930 && hpos < dpyinfo->mouse_face_end_col))
19931 /* Don't redraw the cursor's spot in mouse face if it is at the
19932 end of a line (on a newline). The cursor appears there, but
19933 mouse highlighting does not. */
19934 && cursor_row->used[TEXT_AREA] > hpos)
19935 mouse_face_here_p = 1;
19936
19937 /* Maybe clear the display under the cursor. */
19938 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
19939 {
19940 int x, y;
da8b7f4f 19941 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
fa3c6b4d
KS
19942
19943 cursor_glyph = get_phys_cursor_glyph (w);
19944 if (cursor_glyph == NULL)
19945 goto mark_cursor_off;
19946
19947 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
19948 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
19949
19950 rif->clear_frame_area (f, x, y,
19951 cursor_glyph->pixel_width, cursor_row->visible_height);
19952 }
19953
19954 /* Erase the cursor by redrawing the character underneath it. */
19955 if (mouse_face_here_p)
19956 hl = DRAW_MOUSE_FACE;
19957 else
19958 hl = DRAW_NORMAL_TEXT;
19959 draw_phys_cursor_glyph (w, cursor_row, hl);
19960
19961 mark_cursor_off:
19962 w->phys_cursor_on_p = 0;
19963 w->phys_cursor_type = NO_CURSOR;
19964}
19965
19966
19967/* EXPORT:
19968 Display or clear cursor of window W. If ON is zero, clear the
19969 cursor. If it is non-zero, display the cursor. If ON is nonzero,
19970 where to put the cursor is specified by HPOS, VPOS, X and Y. */
19971
19972void
19973display_and_set_cursor (w, on, hpos, vpos, x, y)
19974 struct window *w;
19975 int on, hpos, vpos, x, y;
19976{
19977 struct frame *f = XFRAME (w->frame);
19978 int new_cursor_type;
19979 int new_cursor_width;
19980 int active_cursor;
fa3c6b4d
KS
19981 struct glyph_row *glyph_row;
19982 struct glyph *glyph;
19983
19984 /* This is pointless on invisible frames, and dangerous on garbaged
19985 windows and frames; in the latter case, the frame or window may
19986 be in the midst of changing its size, and x and y may be off the
19987 window. */
19988 if (! FRAME_VISIBLE_P (f)
19989 || FRAME_GARBAGED_P (f)
19990 || vpos >= w->current_matrix->nrows
19991 || hpos >= w->current_matrix->matrix_w)
19992 return;
19993
19994 /* If cursor is off and we want it off, return quickly. */
19995 if (!on && !w->phys_cursor_on_p)
19996 return;
19997
1d712183 19998 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
fa3c6b4d
KS
19999 /* If cursor row is not enabled, we don't really know where to
20000 display the cursor. */
20001 if (!glyph_row->enabled_p)
20002 {
20003 w->phys_cursor_on_p = 0;
20004 return;
20005 }
20006
1d712183
KS
20007 glyph = NULL;
20008 if (!glyph_row->exact_window_width_line_p
20009 || hpos < glyph_row->used[TEXT_AREA])
20010 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
20011
fa3c6b4d
KS
20012 xassert (interrupt_input_blocked);
20013
20014 /* Set new_cursor_type to the cursor we want to be displayed. */
f65536fa
KS
20015 new_cursor_type = get_window_cursor_type (w, glyph,
20016 &new_cursor_width, &active_cursor);
fa3c6b4d
KS
20017
20018 /* If cursor is currently being shown and we don't want it to be or
20019 it is in the wrong place, or the cursor type is not what we want,
20020 erase it. */
20021 if (w->phys_cursor_on_p
20022 && (!on
20023 || w->phys_cursor.x != x
20024 || w->phys_cursor.y != y
20025 || new_cursor_type != w->phys_cursor_type
20026 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
20027 && new_cursor_width != w->phys_cursor_width)))
20028 erase_phys_cursor (w);
20029
20030 /* Don't check phys_cursor_on_p here because that flag is only set
20031 to zero in some cases where we know that the cursor has been
20032 completely erased, to avoid the extra work of erasing the cursor
20033 twice. In other words, phys_cursor_on_p can be 1 and the cursor
20034 still not be visible, or it has only been partly erased. */
20035 if (on)
20036 {
20037 w->phys_cursor_ascent = glyph_row->ascent;
20038 w->phys_cursor_height = glyph_row->height;
20039
20040 /* Set phys_cursor_.* before x_draw_.* is called because some
20041 of them may need the information. */
20042 w->phys_cursor.x = x;
20043 w->phys_cursor.y = glyph_row->y;
20044 w->phys_cursor.hpos = hpos;
20045 w->phys_cursor.vpos = vpos;
20046 }
20047
b4ebbb12
KS
20048 rif->draw_window_cursor (w, glyph_row, x, y,
20049 new_cursor_type, new_cursor_width,
20050 on, active_cursor);
fa3c6b4d
KS
20051}
20052
20053
20054/* Switch the display of W's cursor on or off, according to the value
20055 of ON. */
20056
20057static void
20058update_window_cursor (w, on)
20059 struct window *w;
20060 int on;
20061{
20062 /* Don't update cursor in windows whose frame is in the process
20063 of being deleted. */
20064 if (w->current_matrix)
20065 {
20066 BLOCK_INPUT;
20067 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20068 w->phys_cursor.x, w->phys_cursor.y);
20069 UNBLOCK_INPUT;
20070 }
20071}
20072
20073
20074/* Call update_window_cursor with parameter ON_P on all leaf windows
20075 in the window tree rooted at W. */
20076
20077static void
20078update_cursor_in_window_tree (w, on_p)
20079 struct window *w;
20080 int on_p;
20081{
20082 while (w)
20083 {
20084 if (!NILP (w->hchild))
20085 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20086 else if (!NILP (w->vchild))
20087 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20088 else
20089 update_window_cursor (w, on_p);
20090
20091 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20092 }
20093}
20094
20095
20096/* EXPORT:
20097 Display the cursor on window W, or clear it, according to ON_P.
20098 Don't change the cursor's position. */
20099
20100void
20101x_update_cursor (f, on_p)
20102 struct frame *f;
20103 int on_p;
20104{
20105 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20106}
20107
20108
20109/* EXPORT:
20110 Clear the cursor of window W to background color, and mark the
20111 cursor as not shown. This is used when the text where the cursor
20112 is is about to be rewritten. */
20113
20114void
20115x_clear_cursor (w)
20116 struct window *w;
20117{
20118 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20119 update_window_cursor (w, 0);
20120}
20121
20122
20123/* EXPORT:
20124 Display the active region described by mouse_face_* according to DRAW. */
20125
20126void
20127show_mouse_face (dpyinfo, draw)
20128 Display_Info *dpyinfo;
20129 enum draw_glyphs_face draw;
20130{
20131 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20132 struct frame *f = XFRAME (WINDOW_FRAME (w));
20133
20134 if (/* If window is in the process of being destroyed, don't bother
20135 to do anything. */
20136 w->current_matrix != NULL
20137 /* Don't update mouse highlight if hidden */
20138 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20139 /* Recognize when we are called to operate on rows that don't exist
20140 anymore. This can happen when a window is split. */
20141 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20142 {
20143 int phys_cursor_on_p = w->phys_cursor_on_p;
20144 struct glyph_row *row, *first, *last;
20145
20146 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20147 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20148
20149 for (row = first; row <= last && row->enabled_p; ++row)
20150 {
20151 int start_hpos, end_hpos, start_x;
20152
20153 /* For all but the first row, the highlight starts at column 0. */
20154 if (row == first)
20155 {
20156 start_hpos = dpyinfo->mouse_face_beg_col;
20157 start_x = dpyinfo->mouse_face_beg_x;
20158 }
20159 else
20160 {
20161 start_hpos = 0;
20162 start_x = 0;
20163 }
20164
20165 if (row == last)
20166 end_hpos = dpyinfo->mouse_face_end_col;
20167 else
20168 end_hpos = row->used[TEXT_AREA];
20169
20170 if (end_hpos > start_hpos)
20171 {
20172 draw_glyphs (w, start_x, row, TEXT_AREA,
20173 start_hpos, end_hpos,
20174 draw, 0);
20175
20176 row->mouse_face_p
20177 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20178 }
20179 }
20180
20181 /* When we've written over the cursor, arrange for it to
20182 be displayed again. */
20183 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20184 {
20185 BLOCK_INPUT;
20186 display_and_set_cursor (w, 1,
20187 w->phys_cursor.hpos, w->phys_cursor.vpos,
20188 w->phys_cursor.x, w->phys_cursor.y);
20189 UNBLOCK_INPUT;
20190 }
20191 }
20192
20193 /* Change the mouse cursor. */
20194 if (draw == DRAW_NORMAL_TEXT)
20195 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
20196 else if (draw == DRAW_MOUSE_FACE)
20197 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
20198 else
20199 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
20200}
20201
20202/* EXPORT:
20203 Clear out the mouse-highlighted active region.
20204 Redraw it un-highlighted first. Value is non-zero if mouse
20205 face was actually drawn unhighlighted. */
20206
20207int
20208clear_mouse_face (dpyinfo)
20209 Display_Info *dpyinfo;
20210{
20211 int cleared = 0;
20212
867756d8 20213 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
fa3c6b4d
KS
20214 {
20215 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
20216 cleared = 1;
20217 }
20218
20219 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20220 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20221 dpyinfo->mouse_face_window = Qnil;
20222 dpyinfo->mouse_face_overlay = Qnil;
20223 return cleared;
20224}
20225
20226
20227/* EXPORT:
20228 Non-zero if physical cursor of window W is within mouse face. */
20229
20230int
20231cursor_in_mouse_face_p (w)
20232 struct window *w;
20233{
20234 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20235 int in_mouse_face = 0;
20236
20237 if (WINDOWP (dpyinfo->mouse_face_window)
20238 && XWINDOW (dpyinfo->mouse_face_window) == w)
20239 {
20240 int hpos = w->phys_cursor.hpos;
20241 int vpos = w->phys_cursor.vpos;
20242
20243 if (vpos >= dpyinfo->mouse_face_beg_row
20244 && vpos <= dpyinfo->mouse_face_end_row
20245 && (vpos > dpyinfo->mouse_face_beg_row
20246 || hpos >= dpyinfo->mouse_face_beg_col)
20247 && (vpos < dpyinfo->mouse_face_end_row
20248 || hpos < dpyinfo->mouse_face_end_col
20249 || dpyinfo->mouse_face_past_end))
20250 in_mouse_face = 1;
20251 }
20252
20253 return in_mouse_face;
20254}
20255
20256
20257
20258\f
20259/* Find the glyph matrix position of buffer position CHARPOS in window
20260 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
20261 current glyphs must be up to date. If CHARPOS is above window
20262 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
20263 of last line in W. In the row containing CHARPOS, stop before glyphs
20264 having STOP as object. */
20265
9c3521e0 20266#if 1 /* This is a version of fast_find_position that's more correct
fa3c6b4d
KS
20267 in the presence of hscrolling, for example. I didn't install
20268 it right away because the problem fixed is minor, it failed
20269 in 20.x as well, and I think it's too risky to install
20270 so near the release of 21.1. 2001-09-25 gerd. */
20271
20272static int
20273fast_find_position (w, charpos, hpos, vpos, x, y, stop)
20274 struct window *w;
20275 int charpos;
20276 int *hpos, *vpos, *x, *y;
20277 Lisp_Object stop;
20278{
20279 struct glyph_row *row, *first;
20280 struct glyph *glyph, *end;
b19a5b64 20281 int past_end = 0;
fa3c6b4d
KS
20282
20283 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20284 row = row_containing_pos (w, charpos, first, NULL, 0);
20285 if (row == NULL)
20286 {
20287 if (charpos < MATRIX_ROW_START_CHARPOS (first))
20288 {
20289 *x = *y = *hpos = *vpos = 0;
7a272039 20290 return 1;
fa3c6b4d
KS
20291 }
20292 else
20293 {
20294 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
20295 past_end = 1;
20296 }
20297 }
20298
20299 *x = row->x;
20300 *y = row->y;
20301 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20302
20303 glyph = row->glyphs[TEXT_AREA];
20304 end = glyph + row->used[TEXT_AREA];
20305
20306 /* Skip over glyphs not having an object at the start of the row.
20307 These are special glyphs like truncation marks on terminal
20308 frames. */
20309 if (row->displays_text_p)
20310 while (glyph < end
20311 && INTEGERP (glyph->object)
20312 && !EQ (stop, glyph->object)
20313 && glyph->charpos < 0)
20314 {
20315 *x += glyph->pixel_width;
20316 ++glyph;
20317 }
20318
20319 while (glyph < end
20320 && !INTEGERP (glyph->object)
20321 && !EQ (stop, glyph->object)
20322 && (!BUFFERP (glyph->object)
20323 || glyph->charpos < charpos))
20324 {
20325 *x += glyph->pixel_width;
20326 ++glyph;
20327 }
20328
20329 *hpos = glyph - row->glyphs[TEXT_AREA];
7a272039 20330 return !past_end;
fa3c6b4d
KS
20331}
20332
9c3521e0 20333#else /* not 1 */
fa3c6b4d
KS
20334
20335static int
20336fast_find_position (w, pos, hpos, vpos, x, y, stop)
20337 struct window *w;
20338 int pos;
20339 int *hpos, *vpos, *x, *y;
20340 Lisp_Object stop;
20341{
20342 int i;
20343 int lastcol;
20344 int maybe_next_line_p = 0;
20345 int line_start_position;
20346 int yb = window_text_bottom_y (w);
20347 struct glyph_row *row, *best_row;
20348 int row_vpos, best_row_vpos;
20349 int current_x;
20350
20351 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20352 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20353
20354 while (row->y < yb)
20355 {
20356 if (row->used[TEXT_AREA])
20357 line_start_position = row->glyphs[TEXT_AREA]->charpos;
20358 else
20359 line_start_position = 0;
20360
20361 if (line_start_position > pos)
20362 break;
20363 /* If the position sought is the end of the buffer,
20364 don't include the blank lines at the bottom of the window. */
20365 else if (line_start_position == pos
20366 && pos == BUF_ZV (XBUFFER (w->buffer)))
20367 {
20368 maybe_next_line_p = 1;
20369 break;
20370 }
20371 else if (line_start_position > 0)
20372 {
20373 best_row = row;
20374 best_row_vpos = row_vpos;
20375 }
20376
20377 if (row->y + row->height >= yb)
20378 break;
20379
20380 ++row;
20381 ++row_vpos;
20382 }
20383
20384 /* Find the right column within BEST_ROW. */
20385 lastcol = 0;
20386 current_x = best_row->x;
20387 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
20388 {
20389 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
20390 int charpos = glyph->charpos;
20391
20392 if (BUFFERP (glyph->object))
20393 {
20394 if (charpos == pos)
20395 {
20396 *hpos = i;
20397 *vpos = best_row_vpos;
20398 *x = current_x;
20399 *y = best_row->y;
20400 return 1;
20401 }
20402 else if (charpos > pos)
20403 break;
20404 }
20405 else if (EQ (glyph->object, stop))
20406 break;
20407
20408 if (charpos > 0)
20409 lastcol = i;
20410 current_x += glyph->pixel_width;
20411 }
20412
20413 /* If we're looking for the end of the buffer,
20414 and we didn't find it in the line we scanned,
20415 use the start of the following line. */
20416 if (maybe_next_line_p)
20417 {
20418 ++best_row;
20419 ++best_row_vpos;
20420 lastcol = 0;
20421 current_x = best_row->x;
20422 }
20423
20424 *vpos = best_row_vpos;
20425 *hpos = lastcol + 1;
20426 *x = current_x;
20427 *y = best_row->y;
20428 return 0;
20429}
20430
9c3521e0 20431#endif /* not 1 */
fa3c6b4d
KS
20432
20433
20434/* Find the position of the glyph for position POS in OBJECT in
20435 window W's current matrix, and return in *X, *Y the pixel
20436 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
20437
20438 RIGHT_P non-zero means return the position of the right edge of the
20439 glyph, RIGHT_P zero means return the left edge position.
20440
20441 If no glyph for POS exists in the matrix, return the position of
20442 the glyph with the next smaller position that is in the matrix, if
20443 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
20444 exists in the matrix, return the position of the glyph with the
20445 next larger position in OBJECT.
20446
20447 Value is non-zero if a glyph was found. */
20448
20449static int
20450fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
20451 struct window *w;
20452 int pos;
20453 Lisp_Object object;
20454 int *hpos, *vpos, *x, *y;
20455 int right_p;
20456{
20457 int yb = window_text_bottom_y (w);
20458 struct glyph_row *r;
20459 struct glyph *best_glyph = NULL;
20460 struct glyph_row *best_row = NULL;
20461 int best_x = 0;
20462
20463 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20464 r->enabled_p && r->y < yb;
20465 ++r)
20466 {
20467 struct glyph *g = r->glyphs[TEXT_AREA];
20468 struct glyph *e = g + r->used[TEXT_AREA];
20469 int gx;
20470
20471 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
20472 if (EQ (g->object, object))
20473 {
20474 if (g->charpos == pos)
20475 {
20476 best_glyph = g;
20477 best_x = gx;
20478 best_row = r;
20479 goto found;
20480 }
20481 else if (best_glyph == NULL
20482 || ((abs (g->charpos - pos)
20483 < abs (best_glyph->charpos - pos))
20484 && (right_p
20485 ? g->charpos < pos
20486 : g->charpos > pos)))
20487 {
20488 best_glyph = g;
20489 best_x = gx;
20490 best_row = r;
20491 }
20492 }
20493 }
20494
20495 found:
20496
20497 if (best_glyph)
20498 {
20499 *x = best_x;
20500 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
20501
20502 if (right_p)
20503 {
20504 *x += best_glyph->pixel_width;
20505 ++*hpos;
20506 }
20507
20508 *y = best_row->y;
20509 *vpos = best_row - w->current_matrix->rows;
20510 }
20511
20512 return best_glyph != NULL;
20513}
20514
20515
493fdc3c
KS
20516/* See if position X, Y is within a hot-spot of an image. */
20517
20518static int
20519on_hot_spot_p (hot_spot, x, y)
20520 Lisp_Object hot_spot;
20521 int x, y;
20522{
20523 if (!CONSP (hot_spot))
20524 return 0;
20525
20526 if (EQ (XCAR (hot_spot), Qrect))
20527 {
20528 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
20529 Lisp_Object rect = XCDR (hot_spot);
20530 Lisp_Object tem;
20531 if (!CONSP (rect))
20532 return 0;
20533 if (!CONSP (XCAR (rect)))
20534 return 0;
20535 if (!CONSP (XCDR (rect)))
20536 return 0;
20537 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
20538 return 0;
20539 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
20540 return 0;
20541 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
20542 return 0;
20543 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
20544 return 0;
20545 return 1;
20546 }
20547 else if (EQ (XCAR (hot_spot), Qcircle))
20548 {
20549 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
20550 Lisp_Object circ = XCDR (hot_spot);
20551 Lisp_Object lr, lx0, ly0;
20552 if (CONSP (circ)
20553 && CONSP (XCAR (circ))
20554 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
20555 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
20556 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
20557 {
20558 double r = XFLOATINT (lr);
20559 double dx = XINT (lx0) - x;
20560 double dy = XINT (ly0) - y;
20561 return (dx * dx + dy * dy <= r * r);
20562 }
20563 }
20564 else if (EQ (XCAR (hot_spot), Qpoly))
20565 {
20566 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
20567 if (VECTORP (XCDR (hot_spot)))
20568 {
20569 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
20570 Lisp_Object *poly = v->contents;
20571 int n = v->size;
20572 int i;
20573 int inside = 0;
20574 Lisp_Object lx, ly;
20575 int x0, y0;
20576
20577 /* Need an even number of coordinates, and at least 3 edges. */
be21b925 20578 if (n < 6 || n & 1)
493fdc3c
KS
20579 return 0;
20580
20581 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
20582 If count is odd, we are inside polygon. Pixels on edges
20583 may or may not be included depending on actual geometry of the
20584 polygon. */
20585 if ((lx = poly[n-2], !INTEGERP (lx))
20586 || (ly = poly[n-1], !INTEGERP (lx)))
20587 return 0;
20588 x0 = XINT (lx), y0 = XINT (ly);
20589 for (i = 0; i < n; i += 2)
20590 {
20591 int x1 = x0, y1 = y0;
20592 if ((lx = poly[i], !INTEGERP (lx))
20593 || (ly = poly[i+1], !INTEGERP (ly)))
20594 return 0;
20595 x0 = XINT (lx), y0 = XINT (ly);
20596
20597 /* Does this segment cross the X line? */
20598 if (x0 >= x)
20599 {
20600 if (x1 >= x)
20601 continue;
20602 }
20603 else if (x1 < x)
20604 continue;
20605 if (y > y0 && y > y1)
20606 continue;
20607 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
20608 inside = !inside;
20609 }
20610 return inside;
20611 }
20612 }
b1c2132c
SM
20613 /* If we don't understand the format, pretend we're not in the hot-spot. */
20614 return 0;
493fdc3c
KS
20615}
20616
20617Lisp_Object
20618find_hot_spot (map, x, y)
20619 Lisp_Object map;
20620 int x, y;
20621{
20622 while (CONSP (map))
20623 {
20624 if (CONSP (XCAR (map))
20625 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
20626 return XCAR (map);
20627 map = XCDR (map);
20628 }
be21b925 20629
493fdc3c
KS
20630 return Qnil;
20631}
20632
20633DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
20634 3, 3, 0,
be21b925 20635 doc: /* Lookup in image map MAP coordinates X and Y.
493fdc3c
KS
20636An image map is an alist where each element has the format (AREA ID PLIST).
20637An AREA is specified as either a rectangle, a circle, or a polygon:
20638A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
20639pixel coordinates of the upper left and bottom right corners.
20640A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
20641and the radius of the circle; r may be a float or integer.
20642A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
20643vector describes one corner in the polygon.
20644Returns the alist element for the first matching AREA in MAP. */)
20645 (map, x, y)
20646 Lisp_Object map;
20647 Lisp_Object x, y;
20648{
493fdc3c
KS
20649 if (NILP (map))
20650 return Qnil;
20651
6d925726
KS
20652 CHECK_NUMBER (x);
20653 CHECK_NUMBER (y);
493fdc3c
KS
20654
20655 return find_hot_spot (map, XINT (x), XINT (y));
20656}
20657
20658
20659/* Display frame CURSOR, optionally using shape defined by POINTER. */
20660static void
20661define_frame_cursor1 (f, cursor, pointer)
20662 struct frame *f;
20663 Cursor cursor;
20664 Lisp_Object pointer;
20665{
20666 if (!NILP (pointer))
20667 {
20668 if (EQ (pointer, Qarrow))
20669 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20670 else if (EQ (pointer, Qhand))
20671 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
20672 else if (EQ (pointer, Qtext))
20673 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20674 else if (EQ (pointer, intern ("hdrag")))
20675 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
20676#ifdef HAVE_X_WINDOWS
20677 else if (EQ (pointer, intern ("vdrag")))
20678 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
20679#endif
20680 else if (EQ (pointer, intern ("hourglass")))
20681 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
20682 else if (EQ (pointer, Qmodeline))
20683 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
20684 else
20685 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20686 }
20687
493fdc3c 20688 if (cursor != No_Cursor)
493fdc3c
KS
20689 rif->define_frame_cursor (f, cursor);
20690}
20691
fa3c6b4d
KS
20692/* Take proper action when mouse has moved to the mode or header line
20693 or marginal area AREA of window W, x-position X and y-position Y.
20694 X is relative to the start of the text display area of W, so the
20695 width of bitmap areas and scroll bars must be subtracted to get a
20696 position relative to the start of the mode line. */
20697
20698static void
20699note_mode_line_or_margin_highlight (w, x, y, area)
20700 struct window *w;
20701 int x, y;
20702 enum window_part area;
20703{
20704 struct frame *f = XFRAME (w->frame);
20705 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
f65536fa 20706 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
493fdc3c 20707 Lisp_Object pointer = Qnil;
349c653e
KS
20708 int charpos, dx, dy, width, height;
20709 Lisp_Object string, object = Qnil;
b1c2132c 20710 Lisp_Object pos, help;
fa3c6b4d
KS
20711
20712 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
349c653e
KS
20713 string = mode_line_string (w, area, &x, &y, &charpos,
20714 &object, &dx, &dy, &width, &height);
fa3c6b4d 20715 else
493fdc3c
KS
20716 {
20717 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
349c653e
KS
20718 string = marginal_area_string (w, area, &x, &y, &charpos,
20719 &object, &dx, &dy, &width, &height);
493fdc3c
KS
20720 }
20721
20722 help = Qnil;
fa3c6b4d 20723
349c653e 20724 if (IMAGEP (object))
493fdc3c
KS
20725 {
20726 Lisp_Object image_map, hotspot;
349c653e 20727 if ((image_map = Fplist_get (XCDR (object), QCmap),
493fdc3c
KS
20728 !NILP (image_map))
20729 && (hotspot = find_hot_spot (image_map, dx, dy),
20730 CONSP (hotspot))
20731 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
20732 {
20733 Lisp_Object area_id, plist;
fa3c6b4d 20734
493fdc3c
KS
20735 area_id = XCAR (hotspot);
20736 /* Could check AREA_ID to see if we enter/leave this hot-spot.
20737 If so, we could look for mouse-enter, mouse-leave
20738 properties in PLIST (and do something...). */
20739 if ((plist = XCDR (hotspot), CONSP (plist)))
20740 {
20741 pointer = Fplist_get (plist, Qpointer);
20742 if (NILP (pointer))
20743 pointer = Qhand;
20744 help = Fplist_get (plist, Qhelp_echo);
20745 if (!NILP (help))
20746 {
20747 help_echo_string = help;
20748 /* Is this correct? ++kfs */
20749 XSETWINDOW (help_echo_window, w);
20750 help_echo_object = w->buffer;
20751 help_echo_pos = charpos;
20752 }
20753 }
20754 if (NILP (pointer))
349c653e
KS
20755 pointer = Fplist_get (XCDR (object), QCpointer);
20756 }
20757 }
20758
20759 if (STRINGP (string))
20760 {
20761 pos = make_number (charpos);
20762 /* If we're on a string with `help-echo' text property, arrange
20763 for the help to be displayed. This is done by setting the
20764 global variable help_echo_string to the help string. */
20765 help = Fget_text_property (pos, Qhelp_echo, string);
20766 if (!NILP (help))
20767 {
20768 help_echo_string = help;
20769 XSETWINDOW (help_echo_window, w);
20770 help_echo_object = string;
20771 help_echo_pos = charpos;
20772 }
20773
20774 if (NILP (pointer))
20775 pointer = Fget_text_property (pos, Qpointer, string);
20776
20777 /* Change the mouse pointer according to what is under X/Y. */
73bc43da 20778 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
349c653e
KS
20779 {
20780 Lisp_Object map;
20781 map = Fget_text_property (pos, Qlocal_map, string);
20782 if (!KEYMAPP (map))
20783 map = Fget_text_property (pos, Qkeymap, string);
20784 if (!KEYMAPP (map))
20785 cursor = dpyinfo->vertical_scroll_bar_cursor;
493fdc3c
KS
20786 }
20787 }
20788
20789 define_frame_cursor1 (f, cursor, pointer);
fa3c6b4d
KS
20790}
20791
fa3c6b4d
KS
20792
20793/* EXPORT:
20794 Take proper action when the mouse has moved to position X, Y on
20795 frame F as regards highlighting characters that have mouse-face
20796 properties. Also de-highlighting chars where the mouse was before.
20797 X and Y can be negative or out of range. */
20798
20799void
20800note_mouse_highlight (f, x, y)
20801 struct frame *f;
20802 int x, y;
20803{
20804 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20805 enum window_part part;
20806 Lisp_Object window;
20807 struct window *w;
20808 Cursor cursor = No_Cursor;
493fdc3c 20809 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
fa3c6b4d
KS
20810 struct buffer *b;
20811
20812 /* When a menu is active, don't highlight because this looks odd. */
20813#if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
20814 if (popup_activated ())
20815 return;
20816#endif
20817
20818 if (NILP (Vmouse_highlight)
20819 || !f->glyphs_initialized_p)
20820 return;
20821
20822 dpyinfo->mouse_face_mouse_x = x;
20823 dpyinfo->mouse_face_mouse_y = y;
20824 dpyinfo->mouse_face_mouse_frame = f;
20825
20826 if (dpyinfo->mouse_face_defer)
20827 return;
20828
20829 if (gc_in_progress)
20830 {
20831 dpyinfo->mouse_face_deferred_gc = 1;
20832 return;
20833 }
20834
20835 /* Which window is that in? */
da8b7f4f 20836 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
fa3c6b4d
KS
20837
20838 /* If we were displaying active text in another window, clear that. */
20839 if (! EQ (window, dpyinfo->mouse_face_window))
20840 clear_mouse_face (dpyinfo);
20841
20842 /* Not on a window -> return. */
20843 if (!WINDOWP (window))
20844 return;
20845
20846 /* Reset help_echo_string. It will get recomputed below. */
fa3c6b4d
KS
20847 help_echo_string = Qnil;
20848
20849 /* Convert to window-relative pixel coordinates. */
20850 w = XWINDOW (window);
20851 frame_to_window_pixel_xy (w, &x, &y);
20852
20853 /* Handle tool-bar window differently since it doesn't display a
20854 buffer. */
20855 if (EQ (window, f->tool_bar_window))
20856 {
20857 note_tool_bar_highlight (f, x, y);
20858 return;
20859 }
20860
fa3c6b4d
KS
20861 /* Mouse is on the mode, header line or margin? */
20862 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
20863 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
20864 {
20865 note_mode_line_or_margin_highlight (w, x, y, part);
20866 return;
20867 }
fa3c6b4d
KS
20868
20869 if (part == ON_VERTICAL_BORDER)
20870 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
f65536fa
KS
20871 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE)
20872 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
fa3c6b4d
KS
20873 else
20874 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20875
20876 /* Are we in a window whose display is up to date?
20877 And verify the buffer's text has not changed. */
20878 b = XBUFFER (w->buffer);
20879 if (part == ON_TEXT
20880 && EQ (w->window_end_valid, w->buffer)
20881 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
20882 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
20883 {
493fdc3c 20884 int hpos, vpos, pos, i, dx, dy, area;
fa3c6b4d
KS
20885 struct glyph *glyph;
20886 Lisp_Object object;
20887 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
20888 Lisp_Object *overlay_vec = NULL;
7994a991 20889 int noverlays;
fa3c6b4d
KS
20890 struct buffer *obuf;
20891 int obegv, ozv, same_region;
20892
20893 /* Find the glyph under X/Y. */
493fdc3c
KS
20894 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
20895
20896 /* Look for :pointer property on image. */
20897 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
20898 {
20899 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
20900 if (img != NULL && IMAGEP (img->spec))
20901 {
20902 Lisp_Object image_map, hotspot;
20903 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
20904 !NILP (image_map))
e893970b
KS
20905 && (hotspot = find_hot_spot (image_map,
20906 glyph->slice.x + dx,
20907 glyph->slice.y + dy),
493fdc3c
KS
20908 CONSP (hotspot))
20909 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
20910 {
20911 Lisp_Object area_id, plist;
20912
20913 area_id = XCAR (hotspot);
20914 /* Could check AREA_ID to see if we enter/leave this hot-spot.
20915 If so, we could look for mouse-enter, mouse-leave
20916 properties in PLIST (and do something...). */
20917 if ((plist = XCDR (hotspot), CONSP (plist)))
20918 {
20919 pointer = Fplist_get (plist, Qpointer);
20920 if (NILP (pointer))
20921 pointer = Qhand;
20922 help_echo_string = Fplist_get (plist, Qhelp_echo);
20923 if (!NILP (help_echo_string))
20924 {
20925 help_echo_window = window;
20926 help_echo_object = glyph->object;
20927 help_echo_pos = glyph->charpos;
20928 }
20929 }
20930 }
20931 if (NILP (pointer))
20932 pointer = Fplist_get (XCDR (img->spec), QCpointer);
20933 }
20934 }
fa3c6b4d
KS
20935
20936 /* Clear mouse face if X/Y not over text. */
20937 if (glyph == NULL
20938 || area != TEXT_AREA
20939 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
20940 {
fa3c6b4d
KS
20941 if (clear_mouse_face (dpyinfo))
20942 cursor = No_Cursor;
493fdc3c
KS
20943 if (NILP (pointer))
20944 {
20945 if (area != TEXT_AREA)
20946 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20947 else
20948 pointer = Vvoid_text_area_pointer;
20949 }
fa3c6b4d
KS
20950 goto set_cursor;
20951 }
20952
20953 pos = glyph->charpos;
20954 object = glyph->object;
20955 if (!STRINGP (object) && !BUFFERP (object))
20956 goto set_cursor;
20957
20958 /* If we get an out-of-range value, return now; avoid an error. */
20959 if (BUFFERP (object) && pos > BUF_Z (b))
20960 goto set_cursor;
20961
20962 /* Make the window's buffer temporarily current for
20963 overlays_at and compute_char_face. */
20964 obuf = current_buffer;
20965 current_buffer = b;
20966 obegv = BEGV;
20967 ozv = ZV;
20968 BEGV = BEG;
20969 ZV = Z;
20970
20971 /* Is this char mouse-active or does it have help-echo? */
20972 position = make_number (pos);
20973
20974 if (BUFFERP (object))
20975 {
7994a991
KS
20976 /* Put all the overlays we want in a vector in overlay_vec. */
20977 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
fa3c6b4d
KS
20978 /* Sort overlays into increasing priority order. */
20979 noverlays = sort_overlays (overlay_vec, noverlays, w);
20980 }
20981 else
20982 noverlays = 0;
20983
20984 same_region = (EQ (window, dpyinfo->mouse_face_window)
20985 && vpos >= dpyinfo->mouse_face_beg_row
20986 && vpos <= dpyinfo->mouse_face_end_row
20987 && (vpos > dpyinfo->mouse_face_beg_row
20988 || hpos >= dpyinfo->mouse_face_beg_col)
20989 && (vpos < dpyinfo->mouse_face_end_row
20990 || hpos < dpyinfo->mouse_face_end_col
20991 || dpyinfo->mouse_face_past_end));
20992
20993 if (same_region)
20994 cursor = No_Cursor;
20995
20996 /* Check mouse-face highlighting. */
20997 if (! same_region
20998 /* If there exists an overlay with mouse-face overlapping
20999 the one we are currently highlighting, we have to
21000 check if we enter the overlapping overlay, and then
21001 highlight only that. */
21002 || (OVERLAYP (dpyinfo->mouse_face_overlay)
21003 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
21004 {
21005 /* Find the highest priority overlay that has a mouse-face
21006 property. */
21007 overlay = Qnil;
21008 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
21009 {
21010 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
21011 if (!NILP (mouse_face))
21012 overlay = overlay_vec[i];
21013 }
21014
21015 /* If we're actually highlighting the same overlay as
21016 before, there's no need to do that again. */
21017 if (!NILP (overlay)
21018 && EQ (overlay, dpyinfo->mouse_face_overlay))
21019 goto check_help_echo;
21020
21021 dpyinfo->mouse_face_overlay = overlay;
21022
21023 /* Clear the display of the old active region, if any. */
21024 if (clear_mouse_face (dpyinfo))
21025 cursor = No_Cursor;
21026
21027 /* If no overlay applies, get a text property. */
21028 if (NILP (overlay))
21029 mouse_face = Fget_text_property (position, Qmouse_face, object);
21030
21031 /* Handle the overlay case. */
21032 if (!NILP (overlay))
21033 {
21034 /* Find the range of text around this char that
21035 should be active. */
21036 Lisp_Object before, after;
21037 int ignore;
21038
21039 before = Foverlay_start (overlay);
21040 after = Foverlay_end (overlay);
21041 /* Record this as the current active region. */
21042 fast_find_position (w, XFASTINT (before),
21043 &dpyinfo->mouse_face_beg_col,
21044 &dpyinfo->mouse_face_beg_row,
21045 &dpyinfo->mouse_face_beg_x,
21046 &dpyinfo->mouse_face_beg_y, Qnil);
21047
21048 dpyinfo->mouse_face_past_end
21049 = !fast_find_position (w, XFASTINT (after),
21050 &dpyinfo->mouse_face_end_col,
21051 &dpyinfo->mouse_face_end_row,
21052 &dpyinfo->mouse_face_end_x,
21053 &dpyinfo->mouse_face_end_y, Qnil);
21054 dpyinfo->mouse_face_window = window;
21055
21056 dpyinfo->mouse_face_face_id
21057 = face_at_buffer_position (w, pos, 0, 0,
21058 &ignore, pos + 1,
21059 !dpyinfo->mouse_face_hidden);
21060
21061 /* Display it as active. */
21062 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21063 cursor = No_Cursor;
21064 }
21065 /* Handle the text property case. */
21066 else if (!NILP (mouse_face) && BUFFERP (object))
21067 {
21068 /* Find the range of text around this char that
21069 should be active. */
21070 Lisp_Object before, after, beginning, end;
21071 int ignore;
21072
21073 beginning = Fmarker_position (w->start);
21074 end = make_number (BUF_Z (XBUFFER (object))
21075 - XFASTINT (w->window_end_pos));
21076 before
21077 = Fprevious_single_property_change (make_number (pos + 1),
21078 Qmouse_face,
21079 object, beginning);
21080 after
21081 = Fnext_single_property_change (position, Qmouse_face,
21082 object, end);
21083
21084 /* Record this as the current active region. */
21085 fast_find_position (w, XFASTINT (before),
21086 &dpyinfo->mouse_face_beg_col,
21087 &dpyinfo->mouse_face_beg_row,
21088 &dpyinfo->mouse_face_beg_x,
21089 &dpyinfo->mouse_face_beg_y, Qnil);
21090 dpyinfo->mouse_face_past_end
21091 = !fast_find_position (w, XFASTINT (after),
21092 &dpyinfo->mouse_face_end_col,
21093 &dpyinfo->mouse_face_end_row,
21094 &dpyinfo->mouse_face_end_x,
21095 &dpyinfo->mouse_face_end_y, Qnil);
21096 dpyinfo->mouse_face_window = window;
21097
21098 if (BUFFERP (object))
21099 dpyinfo->mouse_face_face_id
21100 = face_at_buffer_position (w, pos, 0, 0,
21101 &ignore, pos + 1,
21102 !dpyinfo->mouse_face_hidden);
21103
21104 /* Display it as active. */
21105 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21106 cursor = No_Cursor;
21107 }
21108 else if (!NILP (mouse_face) && STRINGP (object))
21109 {
21110 Lisp_Object b, e;
21111 int ignore;
21112
21113 b = Fprevious_single_property_change (make_number (pos + 1),
21114 Qmouse_face,
21115 object, Qnil);
21116 e = Fnext_single_property_change (position, Qmouse_face,
21117 object, Qnil);
21118 if (NILP (b))
21119 b = make_number (0);
21120 if (NILP (e))
21121 e = make_number (SCHARS (object) - 1);
21122 fast_find_string_pos (w, XINT (b), object,
21123 &dpyinfo->mouse_face_beg_col,
21124 &dpyinfo->mouse_face_beg_row,
21125 &dpyinfo->mouse_face_beg_x,
21126 &dpyinfo->mouse_face_beg_y, 0);
21127 fast_find_string_pos (w, XINT (e), object,
21128 &dpyinfo->mouse_face_end_col,
21129 &dpyinfo->mouse_face_end_row,
21130 &dpyinfo->mouse_face_end_x,
21131 &dpyinfo->mouse_face_end_y, 1);
21132 dpyinfo->mouse_face_past_end = 0;
21133 dpyinfo->mouse_face_window = window;
21134 dpyinfo->mouse_face_face_id
21135 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
21136 glyph->face_id, 1);
21137 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21138 cursor = No_Cursor;
21139 }
21140 else if (STRINGP (object) && NILP (mouse_face))
21141 {
21142 /* A string which doesn't have mouse-face, but
21143 the text ``under'' it might have. */
21144 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
21145 int start = MATRIX_ROW_START_CHARPOS (r);
21146
21147 pos = string_buffer_position (w, object, start);
21148 if (pos > 0)
21149 mouse_face = get_char_property_and_overlay (make_number (pos),
21150 Qmouse_face,
21151 w->buffer,
21152 &overlay);
21153 if (!NILP (mouse_face) && !NILP (overlay))
21154 {
21155 Lisp_Object before = Foverlay_start (overlay);
21156 Lisp_Object after = Foverlay_end (overlay);
21157 int ignore;
21158
21159 /* Note that we might not be able to find position
21160 BEFORE in the glyph matrix if the overlay is
21161 entirely covered by a `display' property. In
21162 this case, we overshoot. So let's stop in
21163 the glyph matrix before glyphs for OBJECT. */
21164 fast_find_position (w, XFASTINT (before),
21165 &dpyinfo->mouse_face_beg_col,
21166 &dpyinfo->mouse_face_beg_row,
21167 &dpyinfo->mouse_face_beg_x,
21168 &dpyinfo->mouse_face_beg_y,
21169 object);
21170
21171 dpyinfo->mouse_face_past_end
21172 = !fast_find_position (w, XFASTINT (after),
21173 &dpyinfo->mouse_face_end_col,
21174 &dpyinfo->mouse_face_end_row,
21175 &dpyinfo->mouse_face_end_x,
21176 &dpyinfo->mouse_face_end_y,
21177 Qnil);
21178 dpyinfo->mouse_face_window = window;
21179 dpyinfo->mouse_face_face_id
21180 = face_at_buffer_position (w, pos, 0, 0,
21181 &ignore, pos + 1,
21182 !dpyinfo->mouse_face_hidden);
21183
21184 /* Display it as active. */
21185 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21186 cursor = No_Cursor;
21187 }
21188 }
21189 }
21190
21191 check_help_echo:
21192
21193 /* Look for a `help-echo' property. */
493fdc3c 21194 if (NILP (help_echo_string)) {
fa3c6b4d
KS
21195 Lisp_Object help, overlay;
21196
21197 /* Check overlays first. */
21198 help = overlay = Qnil;
21199 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
21200 {
21201 overlay = overlay_vec[i];
21202 help = Foverlay_get (overlay, Qhelp_echo);
21203 }
21204
21205 if (!NILP (help))
21206 {
21207 help_echo_string = help;
21208 help_echo_window = window;
21209 help_echo_object = overlay;
21210 help_echo_pos = pos;
21211 }
21212 else
21213 {
21214 Lisp_Object object = glyph->object;
21215 int charpos = glyph->charpos;
21216
21217 /* Try text properties. */
21218 if (STRINGP (object)
21219 && charpos >= 0
21220 && charpos < SCHARS (object))
21221 {
21222 help = Fget_text_property (make_number (charpos),
21223 Qhelp_echo, object);
21224 if (NILP (help))
21225 {
21226 /* If the string itself doesn't specify a help-echo,
21227 see if the buffer text ``under'' it does. */
21228 struct glyph_row *r
21229 = MATRIX_ROW (w->current_matrix, vpos);
21230 int start = MATRIX_ROW_START_CHARPOS (r);
21231 int pos = string_buffer_position (w, object, start);
21232 if (pos > 0)
21233 {
21234 help = Fget_char_property (make_number (pos),
21235 Qhelp_echo, w->buffer);
21236 if (!NILP (help))
21237 {
21238 charpos = pos;
21239 object = w->buffer;
21240 }
21241 }
21242 }
21243 }
21244 else if (BUFFERP (object)
21245 && charpos >= BEGV
21246 && charpos < ZV)
21247 help = Fget_text_property (make_number (charpos), Qhelp_echo,
21248 object);
21249
21250 if (!NILP (help))
21251 {
21252 help_echo_string = help;
21253 help_echo_window = window;
21254 help_echo_object = object;
21255 help_echo_pos = charpos;
21256 }
21257 }
21258 }
21259
493fdc3c
KS
21260 /* Look for a `pointer' property. */
21261 if (NILP (pointer))
21262 {
21263 /* Check overlays first. */
21264 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
21265 pointer = Foverlay_get (overlay_vec[i], Qpointer);
21266
21267 if (NILP (pointer))
21268 {
21269 Lisp_Object object = glyph->object;
21270 int charpos = glyph->charpos;
21271
21272 /* Try text properties. */
21273 if (STRINGP (object)
21274 && charpos >= 0
21275 && charpos < SCHARS (object))
21276 {
21277 pointer = Fget_text_property (make_number (charpos),
21278 Qpointer, object);
21279 if (NILP (pointer))
21280 {
21281 /* If the string itself doesn't specify a pointer,
21282 see if the buffer text ``under'' it does. */
21283 struct glyph_row *r
21284 = MATRIX_ROW (w->current_matrix, vpos);
21285 int start = MATRIX_ROW_START_CHARPOS (r);
21286 int pos = string_buffer_position (w, object, start);
21287 if (pos > 0)
21288 pointer = Fget_char_property (make_number (pos),
21289 Qpointer, w->buffer);
21290 }
21291 }
21292 else if (BUFFERP (object)
21293 && charpos >= BEGV
21294 && charpos < ZV)
21295 pointer = Fget_text_property (make_number (charpos),
21296 Qpointer, object);
21297 }
21298 }
21299
fa3c6b4d
KS
21300 BEGV = obegv;
21301 ZV = ozv;
21302 current_buffer = obuf;
21303 }
21304
21305 set_cursor:
21306
493fdc3c 21307 define_frame_cursor1 (f, cursor, pointer);
fa3c6b4d
KS
21308}
21309
21310
21311/* EXPORT for RIF:
21312 Clear any mouse-face on window W. This function is part of the
21313 redisplay interface, and is called from try_window_id and similar
21314 functions to ensure the mouse-highlight is off. */
21315
21316void
21317x_clear_window_mouse_face (w)
21318 struct window *w;
21319{
21320 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21321 Lisp_Object window;
21322
21323 BLOCK_INPUT;
21324 XSETWINDOW (window, w);
21325 if (EQ (window, dpyinfo->mouse_face_window))
21326 clear_mouse_face (dpyinfo);
21327 UNBLOCK_INPUT;
21328}
21329
21330
21331/* EXPORT:
21332 Just discard the mouse face information for frame F, if any.
21333 This is used when the size of F is changed. */
21334
21335void
21336cancel_mouse_face (f)
21337 struct frame *f;
21338{
21339 Lisp_Object window;
21340 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21341
21342 window = dpyinfo->mouse_face_window;
21343 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
21344 {
21345 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21346 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21347 dpyinfo->mouse_face_window = Qnil;
21348 }
21349}
21350
21351
21352#endif /* HAVE_WINDOW_SYSTEM */
21353
21354\f
21355/***********************************************************************
21356 Exposure Events
21357 ***********************************************************************/
21358
21359#ifdef HAVE_WINDOW_SYSTEM
21360
21361/* Redraw the part of glyph row area AREA of glyph row ROW on window W
21362 which intersects rectangle R. R is in window-relative coordinates. */
21363
21364static void
21365expose_area (w, row, r, area)
21366 struct window *w;
21367 struct glyph_row *row;
21368 XRectangle *r;
21369 enum glyph_row_area area;
21370{
21371 struct glyph *first = row->glyphs[area];
21372 struct glyph *end = row->glyphs[area] + row->used[area];
21373 struct glyph *last;
21374 int first_x, start_x, x;
21375
21376 if (area == TEXT_AREA && row->fill_line_p)
21377 /* If row extends face to end of line write the whole line. */
21378 draw_glyphs (w, 0, row, area,
21379 0, row->used[area],
21380 DRAW_NORMAL_TEXT, 0);
21381 else
21382 {
21383 /* Set START_X to the window-relative start position for drawing glyphs of
21384 AREA. The first glyph of the text area can be partially visible.
21385 The first glyphs of other areas cannot. */
da8b7f4f 21386 start_x = window_box_left_offset (w, area);
fa3c6b4d 21387 x = start_x;
29b3ea63
KS
21388 if (area == TEXT_AREA)
21389 x += row->x;
fa3c6b4d
KS
21390
21391 /* Find the first glyph that must be redrawn. */
21392 while (first < end
21393 && x + first->pixel_width < r->x)
21394 {
21395 x += first->pixel_width;
21396 ++first;
21397 }
21398
21399 /* Find the last one. */
21400 last = first;
21401 first_x = x;
21402 while (last < end
21403 && x < r->x + r->width)
21404 {
21405 x += last->pixel_width;
21406 ++last;
21407 }
21408
21409 /* Repaint. */
21410 if (last > first)
21411 draw_glyphs (w, first_x - start_x, row, area,
21412 first - row->glyphs[area], last - row->glyphs[area],
21413 DRAW_NORMAL_TEXT, 0);
21414 }
21415}
21416
21417
21418/* Redraw the parts of the glyph row ROW on window W intersecting
21419 rectangle R. R is in window-relative coordinates. Value is
21420 non-zero if mouse-face was overwritten. */
21421
21422static int
21423expose_line (w, row, r)
21424 struct window *w;
21425 struct glyph_row *row;
21426 XRectangle *r;
21427{
21428 xassert (row->enabled_p);
21429
21430 if (row->mode_line_p || w->pseudo_window_p)
21431 draw_glyphs (w, 0, row, TEXT_AREA,
21432 0, row->used[TEXT_AREA],
21433 DRAW_NORMAL_TEXT, 0);
21434 else
21435 {
21436 if (row->used[LEFT_MARGIN_AREA])
21437 expose_area (w, row, r, LEFT_MARGIN_AREA);
21438 if (row->used[TEXT_AREA])
21439 expose_area (w, row, r, TEXT_AREA);
21440 if (row->used[RIGHT_MARGIN_AREA])
21441 expose_area (w, row, r, RIGHT_MARGIN_AREA);
21442 draw_row_fringe_bitmaps (w, row);
cfe03a41
KS
21443 }
21444
fa3c6b4d
KS
21445 return row->mouse_face_p;
21446}
21447
21448
21449/* Redraw those parts of glyphs rows during expose event handling that
21450 overlap other rows. Redrawing of an exposed line writes over parts
21451 of lines overlapping that exposed line; this function fixes that.
21452
21453 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
21454 row in W's current matrix that is exposed and overlaps other rows.
21455 LAST_OVERLAPPING_ROW is the last such row. */
21456
21457static void
21458expose_overlaps (w, first_overlapping_row, last_overlapping_row)
21459 struct window *w;
21460 struct glyph_row *first_overlapping_row;
21461 struct glyph_row *last_overlapping_row;
21462{
21463 struct glyph_row *row;
21464
21465 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
21466 if (row->overlapping_p)
21467 {
21468 xassert (row->enabled_p && !row->mode_line_p);
21469
21470 if (row->used[LEFT_MARGIN_AREA])
21471 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
cfe03a41 21472
fa3c6b4d
KS
21473 if (row->used[TEXT_AREA])
21474 x_fix_overlapping_area (w, row, TEXT_AREA);
21475
21476 if (row->used[RIGHT_MARGIN_AREA])
21477 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
21478 }
21479}
21480
21481
21482/* Return non-zero if W's cursor intersects rectangle R. */
21483
21484static int
21485phys_cursor_in_rect_p (w, r)
21486 struct window *w;
21487 XRectangle *r;
21488{
21489 XRectangle cr, result;
21490 struct glyph *cursor_glyph;
21491
21492 cursor_glyph = get_phys_cursor_glyph (w);
21493 if (cursor_glyph)
cfe03a41 21494 {
be21b925 21495 /* r is relative to W's box, but w->phys_cursor.x is relative
aecf50d2
KS
21496 to left edge of W's TEXT area. Adjust it. */
21497 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
fa3c6b4d
KS
21498 cr.y = w->phys_cursor.y;
21499 cr.width = cursor_glyph->pixel_width;
21500 cr.height = w->phys_cursor_height;
be21b925 21501 /* ++KFS: W32 version used W32-specific IntersectRect here, but
fa3c6b4d
KS
21502 I assume the effect is the same -- and this is portable. */
21503 return x_intersect_rectangles (&cr, r, &result);
cfe03a41 21504 }
fa3c6b4d
KS
21505 else
21506 return 0;
21507}
cfe03a41 21508
cfe03a41 21509
fa3c6b4d
KS
21510/* EXPORT:
21511 Draw a vertical window border to the right of window W if W doesn't
21512 have vertical scroll bars. */
cfe03a41 21513
cfe03a41 21514void
fa3c6b4d
KS
21515x_draw_vertical_border (w)
21516 struct window *w;
cfe03a41 21517{
da8b7f4f 21518 /* We could do better, if we knew what type of scroll-bar the adjacent
be21b925 21519 windows (on either side) have... But we don't :-(
da8b7f4f
KS
21520 However, I think this works ok. ++KFS 2003-04-25 */
21521
fa3c6b4d
KS
21522 /* Redraw borders between horizontally adjacent windows. Don't
21523 do it for frames with vertical scroll bars because either the
21524 right scroll bar of a window, or the left scroll bar of its
21525 neighbor will suffice as a border. */
21526 if (!WINDOW_RIGHTMOST_P (w)
da8b7f4f 21527 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
fa3c6b4d
KS
21528 {
21529 int x0, x1, y0, y1;
cfe03a41 21530
fa3c6b4d 21531 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
fa3c6b4d 21532 y1 -= 1;
cfe03a41 21533
fa3c6b4d 21534 rif->draw_vertical_window_border (w, x1, y0, y1);
cfe03a41 21535 }
da8b7f4f
KS
21536 else if (!WINDOW_LEFTMOST_P (w)
21537 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
21538 {
21539 int x0, x1, y0, y1;
21540
21541 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21542 y1 -= 1;
21543
21544 rif->draw_vertical_window_border (w, x0, y0, y1);
21545 }
cfe03a41
KS
21546}
21547
21548
fa3c6b4d
KS
21549/* Redraw the part of window W intersection rectangle FR. Pixel
21550 coordinates in FR are frame-relative. Call this function with
21551 input blocked. Value is non-zero if the exposure overwrites
21552 mouse-face. */
cfe03a41 21553
fa3c6b4d
KS
21554static int
21555expose_window (w, fr)
cfe03a41 21556 struct window *w;
fa3c6b4d 21557 XRectangle *fr;
cfe03a41
KS
21558{
21559 struct frame *f = XFRAME (w->frame);
fa3c6b4d
KS
21560 XRectangle wr, r;
21561 int mouse_face_overwritten_p = 0;
21562
21563 /* If window is not yet fully initialized, do nothing. This can
21564 happen when toolkit scroll bars are used and a window is split.
21565 Reconfiguring the scroll bar will generate an expose for a newly
21566 created window. */
21567 if (w->current_matrix == NULL)
21568 return 0;
cfe03a41 21569
fa3c6b4d
KS
21570 /* When we're currently updating the window, display and current
21571 matrix usually don't agree. Arrange for a thorough display
21572 later. */
21573 if (w == updated_window)
21574 {
21575 SET_FRAME_GARBAGED (f);
21576 return 0;
21577 }
e9c99027 21578
fa3c6b4d 21579 /* Frame-relative pixel rectangle of W. */
da8b7f4f
KS
21580 wr.x = WINDOW_LEFT_EDGE_X (w);
21581 wr.y = WINDOW_TOP_EDGE_Y (w);
21582 wr.width = WINDOW_TOTAL_WIDTH (w);
21583 wr.height = WINDOW_TOTAL_HEIGHT (w);
fa3c6b4d
KS
21584
21585 if (x_intersect_rectangles (fr, &wr, &r))
cfe03a41 21586 {
fa3c6b4d
KS
21587 int yb = window_text_bottom_y (w);
21588 struct glyph_row *row;
21589 int cursor_cleared_p;
21590 struct glyph_row *first_overlapping_row, *last_overlapping_row;
21591
21592 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
21593 r.x, r.y, r.width, r.height));
21594
21595 /* Convert to window coordinates. */
cc8549df
KS
21596 r.x -= WINDOW_LEFT_EDGE_X (w);
21597 r.y -= WINDOW_TOP_EDGE_Y (w);
fa3c6b4d
KS
21598
21599 /* Turn off the cursor. */
21600 if (!w->pseudo_window_p
21601 && phys_cursor_in_rect_p (w, &r))
cfe03a41 21602 {
fa3c6b4d
KS
21603 x_clear_cursor (w);
21604 cursor_cleared_p = 1;
cfe03a41 21605 }
fa3c6b4d
KS
21606 else
21607 cursor_cleared_p = 0;
cfe03a41 21608
fa3c6b4d
KS
21609 /* Update lines intersecting rectangle R. */
21610 first_overlapping_row = last_overlapping_row = NULL;
21611 for (row = w->current_matrix->rows;
21612 row->enabled_p;
21613 ++row)
21614 {
21615 int y0 = row->y;
21616 int y1 = MATRIX_ROW_BOTTOM_Y (row);
cfe03a41 21617
fa3c6b4d
KS
21618 if ((y0 >= r.y && y0 < r.y + r.height)
21619 || (y1 > r.y && y1 < r.y + r.height)
21620 || (r.y >= y0 && r.y < y1)
21621 || (r.y + r.height > y0 && r.y + r.height < y1))
21622 {
21623 if (row->overlapping_p)
21624 {
21625 if (first_overlapping_row == NULL)
21626 first_overlapping_row = row;
21627 last_overlapping_row = row;
21628 }
e9c99027 21629
fa3c6b4d
KS
21630 if (expose_line (w, row, &r))
21631 mouse_face_overwritten_p = 1;
21632 }
cfe03a41 21633
fa3c6b4d
KS
21634 if (y1 >= yb)
21635 break;
21636 }
cfe03a41 21637
fa3c6b4d
KS
21638 /* Display the mode line if there is one. */
21639 if (WINDOW_WANTS_MODELINE_P (w)
21640 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
21641 row->enabled_p)
21642 && row->y < r.y + r.height)
21643 {
21644 if (expose_line (w, row, &r))
21645 mouse_face_overwritten_p = 1;
21646 }
cfe03a41 21647
fa3c6b4d
KS
21648 if (!w->pseudo_window_p)
21649 {
21650 /* Fix the display of overlapping rows. */
21651 if (first_overlapping_row)
21652 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
21653
21654 /* Draw border between windows. */
21655 x_draw_vertical_border (w);
21656
21657 /* Turn the cursor on again. */
21658 if (cursor_cleared_p)
21659 update_window_cursor (w, 1);
21660 }
21661 }
21662
21663#ifdef HAVE_CARBON
21664 /* Display scroll bar for this window. */
21665 if (!NILP (w->vertical_scroll_bar))
cfe03a41 21666 {
fa3c6b4d
KS
21667 /* ++KFS:
21668 If this doesn't work here (maybe some header files are missing),
21669 make a function in macterm.c and call it to do the job! */
21670 ControlHandle ch
21671 = SCROLL_BAR_CONTROL_HANDLE (XSCROLL_BAR (w->vertical_scroll_bar));
21672
21673 Draw1Control (ch);
cfe03a41 21674 }
fa3c6b4d 21675#endif
cfe03a41 21676
fa3c6b4d
KS
21677 return mouse_face_overwritten_p;
21678}
21679
21680
21681
21682/* Redraw (parts) of all windows in the window tree rooted at W that
21683 intersect R. R contains frame pixel coordinates. Value is
21684 non-zero if the exposure overwrites mouse-face. */
21685
21686static int
21687expose_window_tree (w, r)
21688 struct window *w;
21689 XRectangle *r;
21690{
21691 struct frame *f = XFRAME (w->frame);
21692 int mouse_face_overwritten_p = 0;
21693
21694 while (w && !FRAME_GARBAGED_P (f))
cfe03a41 21695 {
fa3c6b4d
KS
21696 if (!NILP (w->hchild))
21697 mouse_face_overwritten_p
21698 |= expose_window_tree (XWINDOW (w->hchild), r);
21699 else if (!NILP (w->vchild))
21700 mouse_face_overwritten_p
21701 |= expose_window_tree (XWINDOW (w->vchild), r);
21702 else
21703 mouse_face_overwritten_p |= expose_window (w, r);
21704
21705 w = NILP (w->next) ? NULL : XWINDOW (w->next);
cfe03a41 21706 }
cfe03a41 21707
fa3c6b4d
KS
21708 return mouse_face_overwritten_p;
21709}
cfe03a41 21710
cfe03a41 21711
fa3c6b4d
KS
21712/* EXPORT:
21713 Redisplay an exposed area of frame F. X and Y are the upper-left
21714 corner of the exposed rectangle. W and H are width and height of
21715 the exposed area. All are pixel values. W or H zero means redraw
21716 the entire frame. */
cfe03a41 21717
fa3c6b4d
KS
21718void
21719expose_frame (f, x, y, w, h)
21720 struct frame *f;
21721 int x, y, w, h;
21722{
21723 XRectangle r;
21724 int mouse_face_overwritten_p = 0;
21725
21726 TRACE ((stderr, "expose_frame "));
21727
21728 /* No need to redraw if frame will be redrawn soon. */
21729 if (FRAME_GARBAGED_P (f))
cfe03a41 21730 {
fa3c6b4d
KS
21731 TRACE ((stderr, " garbaged\n"));
21732 return;
cfe03a41 21733 }
7d0393cf 21734
fa3c6b4d
KS
21735#ifdef HAVE_CARBON
21736 /* MAC_TODO: this is a kludge, but if scroll bars are not activated
21737 or deactivated here, for unknown reasons, activated scroll bars
21738 are shown in deactivated frames in some instances. */
21739 if (f == FRAME_MAC_DISPLAY_INFO (f)->x_focus_frame)
21740 activate_scroll_bars (f);
21741 else
21742 deactivate_scroll_bars (f);
21743#endif
cfe03a41 21744
fa3c6b4d
KS
21745 /* If basic faces haven't been realized yet, there is no point in
21746 trying to redraw anything. This can happen when we get an expose
21747 event while Emacs is starting, e.g. by moving another window. */
21748 if (FRAME_FACE_CACHE (f) == NULL
21749 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
21750 {
21751 TRACE ((stderr, " no faces\n"));
21752 return;
21753 }
cfe03a41 21754
fa3c6b4d 21755 if (w == 0 || h == 0)
cfe03a41 21756 {
fa3c6b4d 21757 r.x = r.y = 0;
da8b7f4f
KS
21758 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
21759 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
fa3c6b4d
KS
21760 }
21761 else
21762 {
21763 r.x = x;
21764 r.y = y;
21765 r.width = w;
21766 r.height = h;
cfe03a41
KS
21767 }
21768
fa3c6b4d
KS
21769 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
21770 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
21771
21772 if (WINDOWP (f->tool_bar_window))
21773 mouse_face_overwritten_p
21774 |= expose_window (XWINDOW (f->tool_bar_window), &r);
21775
21776#ifdef HAVE_X_WINDOWS
21777#ifndef MSDOS
21778#ifndef USE_X_TOOLKIT
21779 if (WINDOWP (f->menu_bar_window))
21780 mouse_face_overwritten_p
21781 |= expose_window (XWINDOW (f->menu_bar_window), &r);
21782#endif /* not USE_X_TOOLKIT */
21783#endif
21784#endif
21785
21786 /* Some window managers support a focus-follows-mouse style with
21787 delayed raising of frames. Imagine a partially obscured frame,
21788 and moving the mouse into partially obscured mouse-face on that
21789 frame. The visible part of the mouse-face will be highlighted,
21790 then the WM raises the obscured frame. With at least one WM, KDE
21791 2.1, Emacs is not getting any event for the raising of the frame
21792 (even tried with SubstructureRedirectMask), only Expose events.
21793 These expose events will draw text normally, i.e. not
21794 highlighted. Which means we must redo the highlight here.
21795 Subsume it under ``we love X''. --gerd 2001-08-15 */
21796 /* Included in Windows version because Windows most likely does not
21797 do the right thing if any third party tool offers
21798 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
21799 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
21800 {
21801 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21802 if (f == dpyinfo->mouse_face_mouse_frame)
21803 {
21804 int x = dpyinfo->mouse_face_mouse_x;
21805 int y = dpyinfo->mouse_face_mouse_y;
21806 clear_mouse_face (dpyinfo);
21807 note_mouse_highlight (f, x, y);
21808 }
21809 }
cfe03a41
KS
21810}
21811
133c764e 21812
fa3c6b4d
KS
21813/* EXPORT:
21814 Determine the intersection of two rectangles R1 and R2. Return
21815 the intersection in *RESULT. Value is non-zero if RESULT is not
21816 empty. */
133c764e 21817
fa3c6b4d
KS
21818int
21819x_intersect_rectangles (r1, r2, result)
21820 XRectangle *r1, *r2, *result;
133c764e 21821{
fa3c6b4d
KS
21822 XRectangle *left, *right;
21823 XRectangle *upper, *lower;
21824 int intersection_p = 0;
21825
21826 /* Rearrange so that R1 is the left-most rectangle. */
21827 if (r1->x < r2->x)
21828 left = r1, right = r2;
21829 else
21830 left = r2, right = r1;
21831
21832 /* X0 of the intersection is right.x0, if this is inside R1,
21833 otherwise there is no intersection. */
21834 if (right->x <= left->x + left->width)
133c764e 21835 {
fa3c6b4d 21836 result->x = right->x;
133c764e 21837
fa3c6b4d
KS
21838 /* The right end of the intersection is the minimum of the
21839 the right ends of left and right. */
21840 result->width = (min (left->x + left->width, right->x + right->width)
21841 - result->x);
133c764e 21842
fa3c6b4d
KS
21843 /* Same game for Y. */
21844 if (r1->y < r2->y)
21845 upper = r1, lower = r2;
21846 else
21847 upper = r2, lower = r1;
133c764e 21848
fa3c6b4d
KS
21849 /* The upper end of the intersection is lower.y0, if this is inside
21850 of upper. Otherwise, there is no intersection. */
21851 if (lower->y <= upper->y + upper->height)
21852 {
21853 result->y = lower->y;
21854
21855 /* The lower end of the intersection is the minimum of the lower
21856 ends of upper and lower. */
21857 result->height = (min (lower->y + lower->height,
21858 upper->y + upper->height)
21859 - result->y);
21860 intersection_p = 1;
133c764e
KS
21861 }
21862 }
fa3c6b4d
KS
21863
21864 return intersection_p;
133c764e
KS
21865}
21866
fa3c6b4d
KS
21867#endif /* HAVE_WINDOW_SYSTEM */
21868
cfe03a41 21869\f
5f5c8ee5
GM
21870/***********************************************************************
21871 Initialization
21872 ***********************************************************************/
21873
a2889657
JB
21874void
21875syms_of_xdisp ()
21876{
c6e89d6c
GM
21877 Vwith_echo_area_save_vector = Qnil;
21878 staticpro (&Vwith_echo_area_save_vector);
5f5c8ee5 21879
c6e89d6c
GM
21880 Vmessage_stack = Qnil;
21881 staticpro (&Vmessage_stack);
2311178e 21882
735c094c 21883 Qinhibit_redisplay = intern ("inhibit-redisplay");
c6e89d6c 21884 staticpro (&Qinhibit_redisplay);
735c094c 21885
b14bc55e
RS
21886 message_dolog_marker1 = Fmake_marker ();
21887 staticpro (&message_dolog_marker1);
21888 message_dolog_marker2 = Fmake_marker ();
21889 staticpro (&message_dolog_marker2);
21890 message_dolog_marker3 = Fmake_marker ();
21891 staticpro (&message_dolog_marker3);
21892
5f5c8ee5 21893#if GLYPH_DEBUG
7d4cc828 21894 defsubr (&Sdump_frame_glyph_matrix);
5f5c8ee5
GM
21895 defsubr (&Sdump_glyph_matrix);
21896 defsubr (&Sdump_glyph_row);
e037b9ec 21897 defsubr (&Sdump_tool_bar_row);
62397849 21898 defsubr (&Strace_redisplay);
bf9249e3 21899 defsubr (&Strace_to_stderr);
5f5c8ee5 21900#endif
99a5de87 21901#ifdef HAVE_WINDOW_SYSTEM
57c28064 21902 defsubr (&Stool_bar_lines_needed);
493fdc3c 21903 defsubr (&Slookup_image_map);
99a5de87 21904#endif
8143e6ab 21905 defsubr (&Sformat_mode_line);
5f5c8ee5 21906
cf074754
RS
21907 staticpro (&Qmenu_bar_update_hook);
21908 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
21909
d46fb96a 21910 staticpro (&Qoverriding_terminal_local_map);
7079aefa 21911 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
d46fb96a 21912
399164b4
KH
21913 staticpro (&Qoverriding_local_map);
21914 Qoverriding_local_map = intern ("overriding-local-map");
21915
75c43375
RS
21916 staticpro (&Qwindow_scroll_functions);
21917 Qwindow_scroll_functions = intern ("window-scroll-functions");
21918
e0bfbde6
RS
21919 staticpro (&Qredisplay_end_trigger_functions);
21920 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
2311178e 21921
2e54982e
RS
21922 staticpro (&Qinhibit_point_motion_hooks);
21923 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
21924
9499d71b
GM
21925 QCdata = intern (":data");
21926 staticpro (&QCdata);
5f5c8ee5 21927 Qdisplay = intern ("display");
f3751a65 21928 staticpro (&Qdisplay);
5f5c8ee5
GM
21929 Qspace_width = intern ("space-width");
21930 staticpro (&Qspace_width);
5f5c8ee5
GM
21931 Qraise = intern ("raise");
21932 staticpro (&Qraise);
e893970b
KS
21933 Qslice = intern ("slice");
21934 staticpro (&Qslice);
5f5c8ee5
GM
21935 Qspace = intern ("space");
21936 staticpro (&Qspace);
f3751a65
GM
21937 Qmargin = intern ("margin");
21938 staticpro (&Qmargin);
493fdc3c
KS
21939 Qpointer = intern ("pointer");
21940 staticpro (&Qpointer);
5f5c8ee5 21941 Qleft_margin = intern ("left-margin");
f3751a65 21942 staticpro (&Qleft_margin);
5f5c8ee5 21943 Qright_margin = intern ("right-margin");
f3751a65 21944 staticpro (&Qright_margin);
da06e4d7
KS
21945 Qcenter = intern ("center");
21946 staticpro (&Qcenter);
4933c603
KS
21947 Qline_height = intern ("line-height");
21948 staticpro (&Qline_height);
17b01ffc
KS
21949 Qtotal = intern ("total");
21950 staticpro (&Qtotal);
5f5c8ee5
GM
21951 QCalign_to = intern (":align-to");
21952 staticpro (&QCalign_to);
5f5c8ee5
GM
21953 QCrelative_width = intern (":relative-width");
21954 staticpro (&QCrelative_width);
21955 QCrelative_height = intern (":relative-height");
21956 staticpro (&QCrelative_height);
21957 QCeval = intern (":eval");
21958 staticpro (&QCeval);
0fcf414f
RS
21959 QCpropertize = intern (":propertize");
21960 staticpro (&QCpropertize);
886bd6f2
GM
21961 QCfile = intern (":file");
21962 staticpro (&QCfile);
5f5c8ee5
GM
21963 Qfontified = intern ("fontified");
21964 staticpro (&Qfontified);
21965 Qfontification_functions = intern ("fontification-functions");
21966 staticpro (&Qfontification_functions);
5f5c8ee5
GM
21967 Qtrailing_whitespace = intern ("trailing-whitespace");
21968 staticpro (&Qtrailing_whitespace);
21969 Qimage = intern ("image");
21970 staticpro (&Qimage);
493fdc3c
KS
21971 QCmap = intern (":map");
21972 staticpro (&QCmap);
21973 QCpointer = intern (":pointer");
21974 staticpro (&QCpointer);
21975 Qrect = intern ("rect");
21976 staticpro (&Qrect);
21977 Qcircle = intern ("circle");
21978 staticpro (&Qcircle);
21979 Qpoly = intern ("poly");
21980 staticpro (&Qpoly);
ad4f174e
GM
21981 Qmessage_truncate_lines = intern ("message-truncate-lines");
21982 staticpro (&Qmessage_truncate_lines);
af79bccb
RS
21983 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
21984 staticpro (&Qcursor_in_non_selected_windows);
6422c1d7
GM
21985 Qgrow_only = intern ("grow-only");
21986 staticpro (&Qgrow_only);
e1477f43
GM
21987 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
21988 staticpro (&Qinhibit_menubar_update);
30a3f61c
GM
21989 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
21990 staticpro (&Qinhibit_eval_during_redisplay);
b384d6f8
GM
21991 Qposition = intern ("position");
21992 staticpro (&Qposition);
21993 Qbuffer_position = intern ("buffer-position");
21994 staticpro (&Qbuffer_position);
21995 Qobject = intern ("object");
21996 staticpro (&Qobject);
cfe03a41
KS
21997 Qbar = intern ("bar");
21998 staticpro (&Qbar);
21999 Qhbar = intern ("hbar");
22000 staticpro (&Qhbar);
22001 Qbox = intern ("box");
22002 staticpro (&Qbox);
22003 Qhollow = intern ("hollow");
22004 staticpro (&Qhollow);
493fdc3c
KS
22005 Qhand = intern ("hand");
22006 staticpro (&Qhand);
22007 Qarrow = intern ("arrow");
22008 staticpro (&Qarrow);
22009 Qtext = intern ("text");
22010 staticpro (&Qtext);
c53a1624
RS
22011 Qrisky_local_variable = intern ("risky-local-variable");
22012 staticpro (&Qrisky_local_variable);
26683087
RS
22013 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
22014 staticpro (&Qinhibit_free_realized_faces);
5f5c8ee5 22015
be21b925
KS
22016 list_of_error = Fcons (Fcons (intern ("error"),
22017 Fcons (intern ("void-variable"), Qnil)),
22018 Qnil);
7033d6df
RS
22019 staticpro (&list_of_error);
22020
351b5434
KS
22021 Qlast_arrow_position = intern ("last-arrow-position");
22022 staticpro (&Qlast_arrow_position);
22023 Qlast_arrow_string = intern ("last-arrow-string");
22024 staticpro (&Qlast_arrow_string);
22025
22026 Qoverlay_arrow_string = intern ("overlay-arrow-string");
22027 staticpro (&Qoverlay_arrow_string);
22028 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
22029 staticpro (&Qoverlay_arrow_bitmap);
2311178e 22030
c6e89d6c
GM
22031 echo_buffer[0] = echo_buffer[1] = Qnil;
22032 staticpro (&echo_buffer[0]);
22033 staticpro (&echo_buffer[1]);
22034
22035 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
22036 staticpro (&echo_area_buffer[0]);
22037 staticpro (&echo_area_buffer[1]);
a2889657 22038
6a94510a
GM
22039 Vmessages_buffer_name = build_string ("*Messages*");
22040 staticpro (&Vmessages_buffer_name);
0fcf414f
RS
22041
22042 mode_line_proptrans_alist = Qnil;
22043 staticpro (&mode_line_proptrans_alist);
2311178e 22044
fec8f23e
KS
22045 mode_line_string_list = Qnil;
22046 staticpro (&mode_line_string_list);
22047
fa3c6b4d
KS
22048 help_echo_string = Qnil;
22049 staticpro (&help_echo_string);
22050 help_echo_object = Qnil;
22051 staticpro (&help_echo_object);
22052 help_echo_window = Qnil;
22053 staticpro (&help_echo_window);
22054 previous_help_echo_string = Qnil;
22055 staticpro (&previous_help_echo_string);
22056 help_echo_pos = -1;
22057
22058#ifdef HAVE_WINDOW_SYSTEM
22059 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
22060 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
22061For example, if a block cursor is over a tab, it will be drawn as
22062wide as that tab on the display. */);
22063 x_stretch_cursor_p = 0;
22064#endif
22065
7ee72033 22066 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
88e6b646 22067 doc: /* *Non-nil means highlight trailing whitespace.
228299fa 22068The face used for trailing whitespace is `trailing-whitespace'. */);
8f897821
GM
22069 Vshow_trailing_whitespace = Qnil;
22070
493fdc3c 22071 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
88e6b646 22072 doc: /* *The pointer shape to show in void text areas.
493fdc3c 22073Nil means to show the text pointer. Other options are `arrow', `text',
be21b925 22074`hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
493fdc3c 22075 Vvoid_text_area_pointer = Qarrow;
f65536fa 22076
7ee72033
MB
22077 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
22078 doc: /* Non-nil means don't actually do any redisplay.
228299fa 22079This is used for internal purposes. */);
735c094c
KH
22080 Vinhibit_redisplay = Qnil;
22081
7ee72033
MB
22082 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
22083 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
a2889657
JB
22084 Vglobal_mode_string = Qnil;
22085
7ee72033
MB
22086 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
22087 doc: /* Marker for where to display an arrow on top of the buffer text.
228299fa
GM
22088This must be the beginning of a line in order to work.
22089See also `overlay-arrow-string'. */);
a2889657
JB
22090 Voverlay_arrow_position = Qnil;
22091
7ee72033 22092 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
351b5434
KS
22093 doc: /* String to display as an arrow in non-window frames.
22094See also `overlay-arrow-position'. */);
a2889657
JB
22095 Voverlay_arrow_string = Qnil;
22096
351b5434
KS
22097 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
22098 doc: /* List of variables (symbols) which hold markers for overlay arrows.
22099The symbols on this list are examined during redisplay to determine
22100where to display overlay arrows. */);
22101 Voverlay_arrow_variable_list
22102 = Fcons (intern ("overlay-arrow-position"), Qnil);
22103
7ee72033
MB
22104 DEFVAR_INT ("scroll-step", &scroll_step,
22105 doc: /* *The number of lines to try scrolling a window by when point moves out.
228299fa
GM
22106If that fails to bring point back on frame, point is centered instead.
22107If this is zero, point is always centered after it moves off frame.
22108If you want scrolling to always be a line at a time, you should set
22109`scroll-conservatively' to a large value rather than set this to 1. */);
22110
7ee72033
MB
22111 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
22112 doc: /* *Scroll up to this many lines, to bring point back on screen.
228299fa
GM
22113A value of zero means to scroll the text to center point vertically
22114in the window. */);
0789adb2
RS
22115 scroll_conservatively = 0;
22116
7ee72033
MB
22117 DEFVAR_INT ("scroll-margin", &scroll_margin,
22118 doc: /* *Number of lines of margin at the top and bottom of a window.
228299fa
GM
22119Recenter the window whenever point gets within this many lines
22120of the top or bottom of the window. */);
9afd2168
RS
22121 scroll_margin = 0;
22122
f65536fa
KS
22123 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
22124 doc: /* Pixels per inch on current display.
22125Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
22126 Vdisplay_pixels_per_inch = make_float (72.0);
22127
5f5c8ee5 22128#if GLYPH_DEBUG
7ee72033 22129 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
5f5c8ee5 22130#endif
a2889657
JB
22131
22132 DEFVAR_BOOL ("truncate-partial-width-windows",
7ee72033
MB
22133 &truncate_partial_width_windows,
22134 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
a2889657
JB
22135 truncate_partial_width_windows = 1;
22136
7ee72033
MB
22137 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
22138 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
228299fa 22139Any other value means to use the appropriate face, `mode-line',
ccfe8f57 22140`header-line', or `menu' respectively. */);
1862a24e 22141 mode_line_inverse_video = 1;
aa6d10fa 22142
7ee72033
MB
22143 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
22144 doc: /* *Maximum buffer size for which line number should be displayed.
228299fa
GM
22145If the buffer is bigger than this, the line number does not appear
22146in the mode line. A value of nil means no limit. */);
090703f4 22147 Vline_number_display_limit = Qnil;
fba9ce76 22148
090703f4 22149 DEFVAR_INT ("line-number-display-limit-width",
7ee72033
MB
22150 &line_number_display_limit_width,
22151 doc: /* *Maximum line width (in characters) for line number display.
228299fa
GM
22152If the average length of the lines near point is bigger than this, then the
22153line number may be omitted from the mode line. */);
5d121aec
KH
22154 line_number_display_limit_width = 200;
22155
7ee72033
MB
22156 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
22157 doc: /* *Non-nil means highlight region even in nonselected windows. */);
293a54ce 22158 highlight_nonselected_windows = 0;
d39b6696 22159
7ee72033
MB
22160 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
22161 doc: /* Non-nil if more than one frame is visible on this display.
228299fa
GM
22162Minibuffer-only frames don't count, but iconified frames do.
22163This variable is not guaranteed to be accurate except while processing
22164`frame-title-format' and `icon-title-format'. */);
22165
7ee72033
MB
22166 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
22167 doc: /* Template for displaying the title bar of visible frames.
228299fa
GM
22168\(Assuming the window manager supports this feature.)
22169This variable has the same structure as `mode-line-format' (which see),
22170and is used only on frames for which no explicit name has been set
22171\(see `modify-frame-parameters'). */);
f65536fa 22172
7ee72033
MB
22173 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
22174 doc: /* Template for displaying the title bar of an iconified frame.
228299fa
GM
22175\(Assuming the window manager supports this feature.)
22176This variable has the same structure as `mode-line-format' (which see),
22177and is used only on frames for which no explicit name has been set
22178\(see `modify-frame-parameters'). */);
d39b6696
KH
22179 Vicon_title_format
22180 = Vframe_title_format
22181 = Fcons (intern ("multiple-frames"),
22182 Fcons (build_string ("%b"),
3ebf0ea9 22183 Fcons (Fcons (empty_string,
d39b6696
KH
22184 Fcons (intern ("invocation-name"),
22185 Fcons (build_string ("@"),
22186 Fcons (intern ("system-name"),
22187 Qnil)))),
22188 Qnil)));
5992c4f7 22189
7ee72033
MB
22190 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
22191 doc: /* Maximum number of lines to keep in the message log buffer.
228299fa
GM
22192If nil, disable message logging. If t, log messages but don't truncate
22193the buffer when it becomes large. */);
ac90c44f 22194 Vmessage_log_max = make_number (50);
08b610e4 22195
7ee72033
MB
22196 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
22197 doc: /* Functions called before redisplay, if window sizes have changed.
228299fa
GM
22198The value should be a list of functions that take one argument.
22199Just before redisplay, for each frame, if any of its windows have changed
22200size since the last redisplay, or have been split or deleted,
22201all the functions in the list are called, with the frame as argument. */);
08b610e4 22202 Vwindow_size_change_functions = Qnil;
75c43375 22203
7ee72033 22204 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
4b9da0fb 22205 doc: /* List of functions to call before redisplaying a window with scrolling.
228299fa
GM
22206Each function is called with two arguments, the window
22207and its new display-start position. Note that the value of `window-end'
22208is not valid when these functions are called. */);
75c43375 22209 Vwindow_scroll_functions = Qnil;
2311178e 22210
fa3c6b4d
KS
22211 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
22212 doc: /* *Non-nil means autoselect window with mouse pointer. */);
22213 mouse_autoselect_window = 0;
22214
7ee72033
MB
22215 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
22216 doc: /* *Non-nil means automatically resize tool-bars.
228299fa
GM
22217This increases a tool-bar's height if not all tool-bar items are visible.
22218It decreases a tool-bar's height when it would display blank lines
22219otherwise. */);
e037b9ec 22220 auto_resize_tool_bars_p = 1;
2311178e 22221
7ee72033
MB
22222 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
22223 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
e037b9ec 22224 auto_raise_tool_bar_buttons_p = 1;
5f5c8ee5 22225
7ee72033
MB
22226 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
22227 doc: /* *Margin around tool-bar buttons in pixels.
228299fa 22228If an integer, use that for both horizontal and vertical margins.
f6c89f27 22229Otherwise, value should be a pair of integers `(HORZ . VERT)' with
228299fa
GM
22230HORZ specifying the horizontal margin, and VERT specifying the
22231vertical margin. */);
c3d76173 22232 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
5f5c8ee5 22233
7ee72033 22234 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
6da3c85b 22235 doc: /* *Relief thickness of tool-bar buttons. */);
c3d76173 22236 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
5f5c8ee5 22237
7ee72033
MB
22238 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
22239 doc: /* List of functions to call to fontify regions of text.
228299fa
GM
22240Each function is called with one argument POS. Functions must
22241fontify a region starting at POS in the current buffer, and give
22242fontified regions the property `fontified'. */);
5f5c8ee5 22243 Vfontification_functions = Qnil;
6b9f0906 22244 Fmake_variable_buffer_local (Qfontification_functions);
7bbe686f
AI
22245
22246 DEFVAR_BOOL ("unibyte-display-via-language-environment",
7ee72033
MB
22247 &unibyte_display_via_language_environment,
22248 doc: /* *Non-nil means display unibyte text according to language environment.
228299fa
GM
22249Specifically this means that unibyte non-ASCII characters
22250are displayed by converting them to the equivalent multibyte characters
22251according to the current language environment. As a result, they are
22252displayed according to the current fontset. */);
7bbe686f 22253 unibyte_display_via_language_environment = 0;
c6e89d6c 22254
7ee72033
MB
22255 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
22256 doc: /* *Maximum height for resizing mini-windows.
228299fa
GM
22257If a float, it specifies a fraction of the mini-window frame's height.
22258If an integer, it specifies a number of lines. */);
c6e89d6c 22259 Vmax_mini_window_height = make_float (0.25);
6422c1d7 22260
7ee72033
MB
22261 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
22262 doc: /* *How to resize mini-windows.
228299fa
GM
22263A value of nil means don't automatically resize mini-windows.
22264A value of t means resize them to fit the text displayed in them.
22265A value of `grow-only', the default, means let mini-windows grow
22266only, until their display becomes empty, at which point the windows
22267go back to their normal size. */);
6422c1d7
GM
22268 Vresize_mini_windows = Qgrow_only;
22269
cfe03a41
KS
22270 DEFVAR_LISP ("cursor-in-non-selected-windows",
22271 &Vcursor_in_non_selected_windows,
22272 doc: /* *Cursor type to display in non-selected windows.
22273t means to use hollow box cursor. See `cursor-type' for other values. */);
22274 Vcursor_in_non_selected_windows = Qt;
22275
cfe03a41
KS
22276 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
22277 doc: /* Alist specifying how to blink the cursor off.
22278Each element has the form (ON-STATE . OFF-STATE). Whenever the
22279`cursor-type' frame-parameter or variable equals ON-STATE,
22280comparing using `equal', Emacs uses OFF-STATE to specify
22281how to blink it off. */);
22282 Vblink_cursor_alist = Qnil;
2311178e 22283
e76d28d5 22284 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
7ee72033 22285 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
d475bcb8 22286 automatic_hscrolling_p = 1;
1df7e8f0 22287
e76d28d5 22288 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
1df7e8f0
EZ
22289 doc: /* *How many columns away from the window edge point is allowed to get
22290before automatic hscrolling will horizontally scroll the window. */);
e76d28d5 22291 hscroll_margin = 5;
1df7e8f0 22292
e76d28d5 22293 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
1df7e8f0
EZ
22294 doc: /* *How many columns to scroll the window when point gets too close to the edge.
22295When point is less than `automatic-hscroll-margin' columns from the window
22296edge, automatic hscrolling will scroll the window by the amount of columns
22297determined by this variable. If its value is a positive integer, scroll that
22298many columns. If it's a positive floating-point number, it specifies the
22299fraction of the window's width to scroll. If it's nil or zero, point will be
22300centered horizontally after the scroll. Any other value, including negative
22301numbers, are treated as if the value were zero.
22302
22303Automatic hscrolling always moves point outside the scroll margin, so if
22304point was more than scroll step columns inside the margin, the window will
22305scroll more than the value given by the scroll step.
22306
22307Note that the lower bound for automatic hscrolling specified by `scroll-left'
22308and `scroll-right' overrides this variable's effect. */);
e76d28d5 22309 Vhscroll_step = make_number (0);
2311178e 22310
7ee72033
MB
22311 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
22312 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
228299fa 22313Bind this around calls to `message' to let it take effect. */);
ad4f174e 22314 message_truncate_lines = 0;
0bca8940 22315
7ee72033
MB
22316 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
22317 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
228299fa 22318Can be used to update submenus whose contents should vary. */);
6422c1d7 22319 Vmenu_bar_update_hook = Qnil;
2311178e 22320
7ee72033
MB
22321 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
22322 doc: /* Non-nil means don't update menu bars. Internal use only. */);
e1477f43 22323 inhibit_menubar_update = 0;
30a3f61c 22324
7ee72033
MB
22325 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
22326 doc: /* Non-nil means don't eval Lisp during redisplay. */);
30a3f61c 22327 inhibit_eval_during_redisplay = 0;
76cb5e06 22328
26683087
RS
22329 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
22330 doc: /* Non-nil means don't free realized faces. Internal use only. */);
22331 inhibit_free_realized_faces = 0;
22332
69d1f7c9 22333#if GLYPH_DEBUG
76cb5e06
GM
22334 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
22335 doc: /* Inhibit try_window_id display optimization. */);
22336 inhibit_try_window_id = 0;
22337
22338 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
22339 doc: /* Inhibit try_window_reusing display optimization. */);
22340 inhibit_try_window_reusing = 0;
22341
22342 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
22343 doc: /* Inhibit try_cursor_movement display optimization. */);
22344 inhibit_try_cursor_movement = 0;
22345#endif /* GLYPH_DEBUG */
a2889657
JB
22346}
22347
5f5c8ee5
GM
22348
22349/* Initialize this module when Emacs starts. */
22350
dfcf069d 22351void
a2889657
JB
22352init_xdisp ()
22353{
22354 Lisp_Object root_window;
5f5c8ee5 22355 struct window *mini_w;
a2889657 22356
04612a64
GM
22357 current_header_line_height = current_mode_line_height = -1;
22358
5f5c8ee5 22359 CHARPOS (this_line_start_pos) = 0;
a2889657
JB
22360
22361 mini_w = XWINDOW (minibuf_window);
11e82b76 22362 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
a2889657 22363
a2889657
JB
22364 if (!noninteractive)
22365 {
5f5c8ee5
GM
22366 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
22367 int i;
22368
da8b7f4f 22369 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
12c226c5 22370 set_window_height (root_window,
da8b7f4f 22371 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
12c226c5 22372 0);
da8b7f4f 22373 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
a2889657
JB
22374 set_window_height (minibuf_window, 1, 0);
22375
da8b7f4f
KS
22376 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
22377 mini_w->total_cols = make_number (FRAME_COLS (f));
5f5c8ee5
GM
22378
22379 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
22380 scratch_glyph_row.glyphs[TEXT_AREA + 1]
22381 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
22382
2311178e 22383 /* The default ellipsis glyphs `...'. */
5f5c8ee5 22384 for (i = 0; i < 3; ++i)
ac90c44f 22385 default_invis_vector[i] = make_number ('.');
a2889657 22386 }
5f5c8ee5 22387
5f5c8ee5 22388 {
93da8435
SM
22389 /* Allocate the buffer for frame titles.
22390 Also used for `format-mode-line'. */
5f5c8ee5
GM
22391 int size = 100;
22392 frame_title_buf = (char *) xmalloc (size);
22393 frame_title_buf_end = frame_title_buf + size;
22394 frame_title_ptr = NULL;
22395 }
2311178e 22396
21fdfb65 22397 help_echo_showing_p = 0;
a2889657 22398}
5f5c8ee5
GM
22399
22400
ab5796a9
MB
22401/* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
22402 (do not change this comment) */