]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(antlr-mode): Use mode-require-final-newline.
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985,86,87,88,93,94,95,97,98,99,2000,01,02,03,04
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
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,
36 let's say almost---see the description of direct update
37 operations, below.)
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)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay +----------------+
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
67 What does redisplay do? Obviously, it has to figure out somehow what
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
91 You will find a lot of redisplay optimizations when you start
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
109
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
123 interface functions taking an iterator structure (struct it)
124 argument.
125
126 Iteration over things to be displayed is then simple. It is
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
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. */
168
169 #include <config.h>
170 #include <stdio.h>
171
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "keymap.h"
183 #include "macros.h"
184 #include "disptab.h"
185 #include "termhooks.h"
186 #include "intervals.h"
187 #include "coding.h"
188 #include "process.h"
189 #include "region-cache.h"
190 #include "fontset.h"
191 #include "blockinput.h"
192
193 #ifdef HAVE_X_WINDOWS
194 #include "xterm.h"
195 #endif
196 #ifdef WINDOWSNT
197 #include "w32term.h"
198 #endif
199 #ifdef MAC_OS
200 #include "macterm.h"
201 #endif
202
203 #ifndef FRAME_X_OUTPUT
204 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
205 #endif
206
207 #define INFINITY 10000000
208
209 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
210 || defined (USE_GTK)
211 extern void set_frame_menubar P_ ((struct frame *f, int, int));
212 extern int pending_menu_activation;
213 #endif
214
215 extern int interrupt_input;
216 extern int command_loop_level;
217
218 extern Lisp_Object do_mouse_tracking;
219
220 extern int minibuffer_auto_raise;
221 extern Lisp_Object Vminibuffer_list;
222
223 extern Lisp_Object Qface;
224 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
225
226 extern Lisp_Object Voverriding_local_map;
227 extern Lisp_Object Voverriding_local_map_menu_flag;
228 extern Lisp_Object Qmenu_item;
229 extern Lisp_Object Qwhen;
230 extern Lisp_Object Qhelp_echo;
231
232 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
233 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
234 Lisp_Object Qredisplay_end_trigger_functions;
235 Lisp_Object Qinhibit_point_motion_hooks;
236 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
237 Lisp_Object Qfontified;
238 Lisp_Object Qgrow_only;
239 Lisp_Object Qinhibit_eval_during_redisplay;
240 Lisp_Object Qbuffer_position, Qposition, Qobject;
241
242 /* Cursor shapes */
243 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
244
245 /* Pointer shapes */
246 Lisp_Object Qarrow, Qhand, Qtext;
247
248 Lisp_Object Qrisky_local_variable;
249
250 /* Holds the list (error). */
251 Lisp_Object list_of_error;
252
253 /* Functions called to fontify regions of text. */
254
255 Lisp_Object Vfontification_functions;
256 Lisp_Object Qfontification_functions;
257
258 /* Non-zero means automatically select any window when the mouse
259 cursor moves into it. */
260 int mouse_autoselect_window;
261
262 /* Non-zero means draw tool bar buttons raised when the mouse moves
263 over them. */
264
265 int auto_raise_tool_bar_buttons_p;
266
267 /* Non-zero means to reposition window if cursor line is only partially visible. */
268
269 int make_cursor_line_fully_visible_p;
270
271 /* Margin around tool bar buttons in pixels. */
272
273 Lisp_Object Vtool_bar_button_margin;
274
275 /* Thickness of shadow to draw around tool bar buttons. */
276
277 EMACS_INT tool_bar_button_relief;
278
279 /* Non-zero means automatically resize tool-bars so that all tool-bar
280 items are visible, and no blank lines remain. */
281
282 int auto_resize_tool_bars_p;
283
284 /* Non-zero means draw block and hollow cursor as wide as the glyph
285 under it. For example, if a block cursor is over a tab, it will be
286 drawn as wide as that tab on the display. */
287
288 int x_stretch_cursor_p;
289
290 /* Non-nil means don't actually do any redisplay. */
291
292 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
293
294 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
295
296 int inhibit_eval_during_redisplay;
297
298 /* Names of text properties relevant for redisplay. */
299
300 Lisp_Object Qdisplay;
301 extern Lisp_Object Qface, Qinvisible, Qwidth;
302
303 /* Symbols used in text property values. */
304
305 Lisp_Object Vdisplay_pixels_per_inch;
306 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
307 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
308 Lisp_Object Qslice;
309 Lisp_Object Qcenter;
310 Lisp_Object Qmargin, Qpointer;
311 Lisp_Object Qline_height;
312 extern Lisp_Object Qheight;
313 extern Lisp_Object QCwidth, QCheight, QCascent;
314 extern Lisp_Object Qscroll_bar;
315 extern Lisp_Object Qcursor;
316
317 /* Non-nil means highlight trailing whitespace. */
318
319 Lisp_Object Vshow_trailing_whitespace;
320
321 #ifdef HAVE_WINDOW_SYSTEM
322 extern Lisp_Object Voverflow_newline_into_fringe;
323
324 /* Test if overflow newline into fringe. Called with iterator IT
325 at or past right window margin, and with IT->current_x set. */
326
327 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
328 (!NILP (Voverflow_newline_into_fringe) \
329 && FRAME_WINDOW_P (it->f) \
330 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
331 && it->current_x == it->last_visible_x)
332
333 #endif /* HAVE_WINDOW_SYSTEM */
334
335 /* Non-nil means show the text cursor in void text areas
336 i.e. in blank areas after eol and eob. This used to be
337 the default in 21.3. */
338
339 Lisp_Object Vvoid_text_area_pointer;
340
341 /* Name of the face used to highlight trailing whitespace. */
342
343 Lisp_Object Qtrailing_whitespace;
344
345 /* Name and number of the face used to highlight escape glyphs. */
346
347 Lisp_Object Qescape_glyph;
348 int escape_glyph_face;
349
350 /* The symbol `image' which is the car of the lists used to represent
351 images in Lisp. */
352
353 Lisp_Object Qimage;
354
355 /* The image map types. */
356 Lisp_Object QCmap, QCpointer;
357 Lisp_Object Qrect, Qcircle, Qpoly;
358
359 /* Non-zero means print newline to stdout before next mini-buffer
360 message. */
361
362 int noninteractive_need_newline;
363
364 /* Non-zero means print newline to message log before next message. */
365
366 static int message_log_need_newline;
367
368 /* Three markers that message_dolog uses.
369 It could allocate them itself, but that causes trouble
370 in handling memory-full errors. */
371 static Lisp_Object message_dolog_marker1;
372 static Lisp_Object message_dolog_marker2;
373 static Lisp_Object message_dolog_marker3;
374 \f
375 /* The buffer position of the first character appearing entirely or
376 partially on the line of the selected window which contains the
377 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
378 redisplay optimization in redisplay_internal. */
379
380 static struct text_pos this_line_start_pos;
381
382 /* Number of characters past the end of the line above, including the
383 terminating newline. */
384
385 static struct text_pos this_line_end_pos;
386
387 /* The vertical positions and the height of this line. */
388
389 static int this_line_vpos;
390 static int this_line_y;
391 static int this_line_pixel_height;
392
393 /* X position at which this display line starts. Usually zero;
394 negative if first character is partially visible. */
395
396 static int this_line_start_x;
397
398 /* Buffer that this_line_.* variables are referring to. */
399
400 static struct buffer *this_line_buffer;
401
402 /* Nonzero means truncate lines in all windows less wide than the
403 frame. */
404
405 int truncate_partial_width_windows;
406
407 /* A flag to control how to display unibyte 8-bit character. */
408
409 int unibyte_display_via_language_environment;
410
411 /* Nonzero means we have more than one non-mini-buffer-only frame.
412 Not guaranteed to be accurate except while parsing
413 frame-title-format. */
414
415 int multiple_frames;
416
417 Lisp_Object Vglobal_mode_string;
418
419
420 /* List of variables (symbols) which hold markers for overlay arrows.
421 The symbols on this list are examined during redisplay to determine
422 where to display overlay arrows. */
423
424 Lisp_Object Voverlay_arrow_variable_list;
425
426 /* Marker for where to display an arrow on top of the buffer text. */
427
428 Lisp_Object Voverlay_arrow_position;
429
430 /* String to display for the arrow. Only used on terminal frames. */
431
432 Lisp_Object Voverlay_arrow_string;
433
434 /* Values of those variables at last redisplay are stored as
435 properties on `overlay-arrow-position' symbol. However, if
436 Voverlay_arrow_position is a marker, last-arrow-position is its
437 numerical position. */
438
439 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
440
441 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
442 properties on a symbol in overlay-arrow-variable-list. */
443
444 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
445
446 /* Like mode-line-format, but for the title bar on a visible frame. */
447
448 Lisp_Object Vframe_title_format;
449
450 /* Like mode-line-format, but for the title bar on an iconified frame. */
451
452 Lisp_Object Vicon_title_format;
453
454 /* List of functions to call when a window's size changes. These
455 functions get one arg, a frame on which one or more windows' sizes
456 have changed. */
457
458 static Lisp_Object Vwindow_size_change_functions;
459
460 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
461
462 /* Nonzero if overlay arrow has been displayed once in this window. */
463
464 static int overlay_arrow_seen;
465
466 /* Nonzero means highlight the region even in nonselected windows. */
467
468 int highlight_nonselected_windows;
469
470 /* If cursor motion alone moves point off frame, try scrolling this
471 many lines up or down if that will bring it back. */
472
473 static EMACS_INT scroll_step;
474
475 /* Nonzero means scroll just far enough to bring point back on the
476 screen, when appropriate. */
477
478 static EMACS_INT scroll_conservatively;
479
480 /* Recenter the window whenever point gets within this many lines of
481 the top or bottom of the window. This value is translated into a
482 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
483 that there is really a fixed pixel height scroll margin. */
484
485 EMACS_INT scroll_margin;
486
487 /* Number of windows showing the buffer of the selected window (or
488 another buffer with the same base buffer). keyboard.c refers to
489 this. */
490
491 int buffer_shared;
492
493 /* Vector containing glyphs for an ellipsis `...'. */
494
495 static Lisp_Object default_invis_vector[3];
496
497 /* Zero means display the mode-line/header-line/menu-bar in the default face
498 (this slightly odd definition is for compatibility with previous versions
499 of emacs), non-zero means display them using their respective faces.
500
501 This variable is deprecated. */
502
503 int mode_line_inverse_video;
504
505 /* Prompt to display in front of the mini-buffer contents. */
506
507 Lisp_Object minibuf_prompt;
508
509 /* Width of current mini-buffer prompt. Only set after display_line
510 of the line that contains the prompt. */
511
512 int minibuf_prompt_width;
513
514 /* This is the window where the echo area message was displayed. It
515 is always a mini-buffer window, but it may not be the same window
516 currently active as a mini-buffer. */
517
518 Lisp_Object echo_area_window;
519
520 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
521 pushes the current message and the value of
522 message_enable_multibyte on the stack, the function restore_message
523 pops the stack and displays MESSAGE again. */
524
525 Lisp_Object Vmessage_stack;
526
527 /* Nonzero means multibyte characters were enabled when the echo area
528 message was specified. */
529
530 int message_enable_multibyte;
531
532 /* Nonzero if we should redraw the mode lines on the next redisplay. */
533
534 int update_mode_lines;
535
536 /* Nonzero if window sizes or contents have changed since last
537 redisplay that finished. */
538
539 int windows_or_buffers_changed;
540
541 /* Nonzero means a frame's cursor type has been changed. */
542
543 int cursor_type_changed;
544
545 /* Nonzero after display_mode_line if %l was used and it displayed a
546 line number. */
547
548 int line_number_displayed;
549
550 /* Maximum buffer size for which to display line numbers. */
551
552 Lisp_Object Vline_number_display_limit;
553
554 /* Line width to consider when repositioning for line number display. */
555
556 static EMACS_INT line_number_display_limit_width;
557
558 /* Number of lines to keep in the message log buffer. t means
559 infinite. nil means don't log at all. */
560
561 Lisp_Object Vmessage_log_max;
562
563 /* The name of the *Messages* buffer, a string. */
564
565 static Lisp_Object Vmessages_buffer_name;
566
567 /* Current, index 0, and last displayed echo area message. Either
568 buffers from echo_buffers, or nil to indicate no message. */
569
570 Lisp_Object echo_area_buffer[2];
571
572 /* The buffers referenced from echo_area_buffer. */
573
574 static Lisp_Object echo_buffer[2];
575
576 /* A vector saved used in with_area_buffer to reduce consing. */
577
578 static Lisp_Object Vwith_echo_area_save_vector;
579
580 /* Non-zero means display_echo_area should display the last echo area
581 message again. Set by redisplay_preserve_echo_area. */
582
583 static int display_last_displayed_message_p;
584
585 /* Nonzero if echo area is being used by print; zero if being used by
586 message. */
587
588 int message_buf_print;
589
590 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
591
592 Lisp_Object Qinhibit_menubar_update;
593 int inhibit_menubar_update;
594
595 /* Maximum height for resizing mini-windows. Either a float
596 specifying a fraction of the available height, or an integer
597 specifying a number of lines. */
598
599 Lisp_Object Vmax_mini_window_height;
600
601 /* Non-zero means messages should be displayed with truncated
602 lines instead of being continued. */
603
604 int message_truncate_lines;
605 Lisp_Object Qmessage_truncate_lines;
606
607 /* Set to 1 in clear_message to make redisplay_internal aware
608 of an emptied echo area. */
609
610 static int message_cleared_p;
611
612 /* Non-zero means we want a hollow cursor in windows that are not
613 selected. Zero means there's no cursor in such windows. */
614
615 Lisp_Object Vcursor_in_non_selected_windows;
616 Lisp_Object Qcursor_in_non_selected_windows;
617
618 /* How to blink the default frame cursor off. */
619 Lisp_Object Vblink_cursor_alist;
620
621 /* A scratch glyph row with contents used for generating truncation
622 glyphs. Also used in direct_output_for_insert. */
623
624 #define MAX_SCRATCH_GLYPHS 100
625 struct glyph_row scratch_glyph_row;
626 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
627
628 /* Ascent and height of the last line processed by move_it_to. */
629
630 static int last_max_ascent, last_height;
631
632 /* Non-zero if there's a help-echo in the echo area. */
633
634 int help_echo_showing_p;
635
636 /* If >= 0, computed, exact values of mode-line and header-line height
637 to use in the macros CURRENT_MODE_LINE_HEIGHT and
638 CURRENT_HEADER_LINE_HEIGHT. */
639
640 int current_mode_line_height, current_header_line_height;
641
642 /* The maximum distance to look ahead for text properties. Values
643 that are too small let us call compute_char_face and similar
644 functions too often which is expensive. Values that are too large
645 let us call compute_char_face and alike too often because we
646 might not be interested in text properties that far away. */
647
648 #define TEXT_PROP_DISTANCE_LIMIT 100
649
650 #if GLYPH_DEBUG
651
652 /* Variables to turn off display optimizations from Lisp. */
653
654 int inhibit_try_window_id, inhibit_try_window_reusing;
655 int inhibit_try_cursor_movement;
656
657 /* Non-zero means print traces of redisplay if compiled with
658 GLYPH_DEBUG != 0. */
659
660 int trace_redisplay_p;
661
662 #endif /* GLYPH_DEBUG */
663
664 #ifdef DEBUG_TRACE_MOVE
665 /* Non-zero means trace with TRACE_MOVE to stderr. */
666 int trace_move;
667
668 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
669 #else
670 #define TRACE_MOVE(x) (void) 0
671 #endif
672
673 /* Non-zero means automatically scroll windows horizontally to make
674 point visible. */
675
676 int automatic_hscrolling_p;
677
678 /* How close to the margin can point get before the window is scrolled
679 horizontally. */
680 EMACS_INT hscroll_margin;
681
682 /* How much to scroll horizontally when point is inside the above margin. */
683 Lisp_Object Vhscroll_step;
684
685 /* The variable `resize-mini-windows'. If nil, don't resize
686 mini-windows. If t, always resize them to fit the text they
687 display. If `grow-only', let mini-windows grow only until they
688 become empty. */
689
690 Lisp_Object Vresize_mini_windows;
691
692 /* Buffer being redisplayed -- for redisplay_window_error. */
693
694 struct buffer *displayed_buffer;
695
696 /* Value returned from text property handlers (see below). */
697
698 enum prop_handled
699 {
700 HANDLED_NORMALLY,
701 HANDLED_RECOMPUTE_PROPS,
702 HANDLED_OVERLAY_STRING_CONSUMED,
703 HANDLED_RETURN
704 };
705
706 /* A description of text properties that redisplay is interested
707 in. */
708
709 struct props
710 {
711 /* The name of the property. */
712 Lisp_Object *name;
713
714 /* A unique index for the property. */
715 enum prop_idx idx;
716
717 /* A handler function called to set up iterator IT from the property
718 at IT's current position. Value is used to steer handle_stop. */
719 enum prop_handled (*handler) P_ ((struct it *it));
720 };
721
722 static enum prop_handled handle_face_prop P_ ((struct it *));
723 static enum prop_handled handle_invisible_prop P_ ((struct it *));
724 static enum prop_handled handle_display_prop P_ ((struct it *));
725 static enum prop_handled handle_composition_prop P_ ((struct it *));
726 static enum prop_handled handle_overlay_change P_ ((struct it *));
727 static enum prop_handled handle_fontified_prop P_ ((struct it *));
728
729 /* Properties handled by iterators. */
730
731 static struct props it_props[] =
732 {
733 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
734 /* Handle `face' before `display' because some sub-properties of
735 `display' need to know the face. */
736 {&Qface, FACE_PROP_IDX, handle_face_prop},
737 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
738 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
739 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
740 {NULL, 0, NULL}
741 };
742
743 /* Value is the position described by X. If X is a marker, value is
744 the marker_position of X. Otherwise, value is X. */
745
746 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
747
748 /* Enumeration returned by some move_it_.* functions internally. */
749
750 enum move_it_result
751 {
752 /* Not used. Undefined value. */
753 MOVE_UNDEFINED,
754
755 /* Move ended at the requested buffer position or ZV. */
756 MOVE_POS_MATCH_OR_ZV,
757
758 /* Move ended at the requested X pixel position. */
759 MOVE_X_REACHED,
760
761 /* Move within a line ended at the end of a line that must be
762 continued. */
763 MOVE_LINE_CONTINUED,
764
765 /* Move within a line ended at the end of a line that would
766 be displayed truncated. */
767 MOVE_LINE_TRUNCATED,
768
769 /* Move within a line ended at a line end. */
770 MOVE_NEWLINE_OR_CR
771 };
772
773 /* This counter is used to clear the face cache every once in a while
774 in redisplay_internal. It is incremented for each redisplay.
775 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
776 cleared. */
777
778 #define CLEAR_FACE_CACHE_COUNT 500
779 static int clear_face_cache_count;
780
781 /* Record the previous terminal frame we displayed. */
782
783 static struct frame *previous_terminal_frame;
784
785 /* Non-zero while redisplay_internal is in progress. */
786
787 int redisplaying_p;
788
789 /* Non-zero means don't free realized faces. Bound while freeing
790 realized faces is dangerous because glyph matrices might still
791 reference them. */
792
793 int inhibit_free_realized_faces;
794 Lisp_Object Qinhibit_free_realized_faces;
795
796 /* If a string, XTread_socket generates an event to display that string.
797 (The display is done in read_char.) */
798
799 Lisp_Object help_echo_string;
800 Lisp_Object help_echo_window;
801 Lisp_Object help_echo_object;
802 int help_echo_pos;
803
804 /* Temporary variable for XTread_socket. */
805
806 Lisp_Object previous_help_echo_string;
807
808 /* Null glyph slice */
809
810 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
811
812 \f
813 /* Function prototypes. */
814
815 static void setup_for_ellipsis P_ ((struct it *, int));
816 static void mark_window_display_accurate_1 P_ ((struct window *, int));
817 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
818 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
819 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
820 static int redisplay_mode_lines P_ ((Lisp_Object, int));
821 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
822
823 #if 0
824 static int invisible_text_between_p P_ ((struct it *, int, int));
825 #endif
826
827 static int next_element_from_ellipsis P_ ((struct it *));
828 static void pint2str P_ ((char *, int, int));
829 static void pint2hrstr P_ ((char *, int, int));
830 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
831 struct text_pos));
832 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
833 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
834 static void store_frame_title_char P_ ((char));
835 static int store_frame_title P_ ((const unsigned char *, int, int));
836 static void x_consider_frame_title P_ ((Lisp_Object));
837 static void handle_stop P_ ((struct it *));
838 static int tool_bar_lines_needed P_ ((struct frame *));
839 static int single_display_spec_intangible_p P_ ((Lisp_Object));
840 static void ensure_echo_area_buffers P_ ((void));
841 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
842 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
843 static int with_echo_area_buffer P_ ((struct window *, int,
844 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
845 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
846 static void clear_garbaged_frames P_ ((void));
847 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
848 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
849 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
850 static int display_echo_area P_ ((struct window *));
851 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
852 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
853 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
854 static int string_char_and_length P_ ((const unsigned char *, int, int *));
855 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
856 struct text_pos));
857 static int compute_window_start_on_continuation_line P_ ((struct window *));
858 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
859 static void insert_left_trunc_glyphs P_ ((struct it *));
860 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
861 Lisp_Object));
862 static void extend_face_to_end_of_line P_ ((struct it *));
863 static int append_space_for_newline P_ ((struct it *, int));
864 static int make_cursor_line_fully_visible P_ ((struct window *, int));
865 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
866 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
867 static int trailing_whitespace_p P_ ((int));
868 static int message_log_check_duplicate P_ ((int, int, int, int));
869 static void push_it P_ ((struct it *));
870 static void pop_it P_ ((struct it *));
871 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
872 static void select_frame_for_redisplay P_ ((Lisp_Object));
873 static void redisplay_internal P_ ((int));
874 static int echo_area_display P_ ((int));
875 static void redisplay_windows P_ ((Lisp_Object));
876 static void redisplay_window P_ ((Lisp_Object, int));
877 static Lisp_Object redisplay_window_error ();
878 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
879 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
880 static void update_menu_bar P_ ((struct frame *, int));
881 static int try_window_reusing_current_matrix P_ ((struct window *));
882 static int try_window_id P_ ((struct window *));
883 static int display_line P_ ((struct it *));
884 static int display_mode_lines P_ ((struct window *));
885 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
886 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
887 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
888 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
889 static void display_menu_bar P_ ((struct window *));
890 static int display_count_lines P_ ((int, int, int, int, int *));
891 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
892 int, int, struct it *, int, int, int, int));
893 static void compute_line_metrics P_ ((struct it *));
894 static void run_redisplay_end_trigger_hook P_ ((struct it *));
895 static int get_overlay_strings P_ ((struct it *, int));
896 static void next_overlay_string P_ ((struct it *));
897 static void reseat P_ ((struct it *, struct text_pos, int));
898 static void reseat_1 P_ ((struct it *, struct text_pos, int));
899 static void back_to_previous_visible_line_start P_ ((struct it *));
900 void reseat_at_previous_visible_line_start P_ ((struct it *));
901 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
902 static int next_element_from_display_vector P_ ((struct it *));
903 static int next_element_from_string P_ ((struct it *));
904 static int next_element_from_c_string P_ ((struct it *));
905 static int next_element_from_buffer P_ ((struct it *));
906 static int next_element_from_composition P_ ((struct it *));
907 static int next_element_from_image P_ ((struct it *));
908 static int next_element_from_stretch P_ ((struct it *));
909 static void load_overlay_strings P_ ((struct it *, int));
910 static int init_from_display_pos P_ ((struct it *, struct window *,
911 struct display_pos *));
912 static void reseat_to_string P_ ((struct it *, unsigned char *,
913 Lisp_Object, int, int, int, int));
914 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
915 int, int, int));
916 void move_it_vertically_backward P_ ((struct it *, int));
917 static void init_to_row_start P_ ((struct it *, struct window *,
918 struct glyph_row *));
919 static int init_to_row_end P_ ((struct it *, struct window *,
920 struct glyph_row *));
921 static void back_to_previous_line_start P_ ((struct it *));
922 static int forward_to_next_line_start P_ ((struct it *, int *));
923 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
924 Lisp_Object, int));
925 static struct text_pos string_pos P_ ((int, Lisp_Object));
926 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
927 static int number_of_chars P_ ((unsigned char *, int));
928 static void compute_stop_pos P_ ((struct it *));
929 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
930 Lisp_Object));
931 static int face_before_or_after_it_pos P_ ((struct it *, int));
932 static int next_overlay_change P_ ((int));
933 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
934 Lisp_Object, struct text_pos *,
935 int));
936 static int underlying_face_id P_ ((struct it *));
937 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
938 struct window *));
939
940 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
941 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
942
943 #ifdef HAVE_WINDOW_SYSTEM
944
945 static void update_tool_bar P_ ((struct frame *, int));
946 static void build_desired_tool_bar_string P_ ((struct frame *f));
947 static int redisplay_tool_bar P_ ((struct frame *));
948 static void display_tool_bar_line P_ ((struct it *));
949 static void notice_overwritten_cursor P_ ((struct window *,
950 enum glyph_row_area,
951 int, int, int, int));
952
953
954
955 #endif /* HAVE_WINDOW_SYSTEM */
956
957 \f
958 /***********************************************************************
959 Window display dimensions
960 ***********************************************************************/
961
962 /* Return the bottom boundary y-position for text lines in window W.
963 This is the first y position at which a line cannot start.
964 It is relative to the top of the window.
965
966 This is the height of W minus the height of a mode line, if any. */
967
968 INLINE int
969 window_text_bottom_y (w)
970 struct window *w;
971 {
972 int height = WINDOW_TOTAL_HEIGHT (w);
973
974 if (WINDOW_WANTS_MODELINE_P (w))
975 height -= CURRENT_MODE_LINE_HEIGHT (w);
976 return height;
977 }
978
979 /* Return the pixel width of display area AREA of window W. AREA < 0
980 means return the total width of W, not including fringes to
981 the left and right of the window. */
982
983 INLINE int
984 window_box_width (w, area)
985 struct window *w;
986 int area;
987 {
988 int cols = XFASTINT (w->total_cols);
989 int pixels = 0;
990
991 if (!w->pseudo_window_p)
992 {
993 cols -= WINDOW_SCROLL_BAR_COLS (w);
994
995 if (area == TEXT_AREA)
996 {
997 if (INTEGERP (w->left_margin_cols))
998 cols -= XFASTINT (w->left_margin_cols);
999 if (INTEGERP (w->right_margin_cols))
1000 cols -= XFASTINT (w->right_margin_cols);
1001 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1002 }
1003 else if (area == LEFT_MARGIN_AREA)
1004 {
1005 cols = (INTEGERP (w->left_margin_cols)
1006 ? XFASTINT (w->left_margin_cols) : 0);
1007 pixels = 0;
1008 }
1009 else if (area == RIGHT_MARGIN_AREA)
1010 {
1011 cols = (INTEGERP (w->right_margin_cols)
1012 ? XFASTINT (w->right_margin_cols) : 0);
1013 pixels = 0;
1014 }
1015 }
1016
1017 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1018 }
1019
1020
1021 /* Return the pixel height of the display area of window W, not
1022 including mode lines of W, if any. */
1023
1024 INLINE int
1025 window_box_height (w)
1026 struct window *w;
1027 {
1028 struct frame *f = XFRAME (w->frame);
1029 int height = WINDOW_TOTAL_HEIGHT (w);
1030
1031 xassert (height >= 0);
1032
1033 /* Note: the code below that determines the mode-line/header-line
1034 height is essentially the same as that contained in the macro
1035 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1036 the appropriate glyph row has its `mode_line_p' flag set,
1037 and if it doesn't, uses estimate_mode_line_height instead. */
1038
1039 if (WINDOW_WANTS_MODELINE_P (w))
1040 {
1041 struct glyph_row *ml_row
1042 = (w->current_matrix && w->current_matrix->rows
1043 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1044 : 0);
1045 if (ml_row && ml_row->mode_line_p)
1046 height -= ml_row->height;
1047 else
1048 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1049 }
1050
1051 if (WINDOW_WANTS_HEADER_LINE_P (w))
1052 {
1053 struct glyph_row *hl_row
1054 = (w->current_matrix && w->current_matrix->rows
1055 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1056 : 0);
1057 if (hl_row && hl_row->mode_line_p)
1058 height -= hl_row->height;
1059 else
1060 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1061 }
1062
1063 /* With a very small font and a mode-line that's taller than
1064 default, we might end up with a negative height. */
1065 return max (0, height);
1066 }
1067
1068 /* Return the window-relative coordinate of the left edge of display
1069 area AREA of window W. AREA < 0 means return the left edge of the
1070 whole window, to the right of the left fringe of W. */
1071
1072 INLINE int
1073 window_box_left_offset (w, area)
1074 struct window *w;
1075 int area;
1076 {
1077 int x;
1078
1079 if (w->pseudo_window_p)
1080 return 0;
1081
1082 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1083
1084 if (area == TEXT_AREA)
1085 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1086 + window_box_width (w, LEFT_MARGIN_AREA));
1087 else if (area == RIGHT_MARGIN_AREA)
1088 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1089 + window_box_width (w, LEFT_MARGIN_AREA)
1090 + window_box_width (w, TEXT_AREA)
1091 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1092 ? 0
1093 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1094 else if (area == LEFT_MARGIN_AREA
1095 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1096 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1097
1098 return x;
1099 }
1100
1101
1102 /* Return the window-relative coordinate of the right edge of display
1103 area AREA of window W. AREA < 0 means return the left edge of the
1104 whole window, to the left of the right fringe of W. */
1105
1106 INLINE int
1107 window_box_right_offset (w, area)
1108 struct window *w;
1109 int area;
1110 {
1111 return window_box_left_offset (w, area) + window_box_width (w, area);
1112 }
1113
1114 /* Return the frame-relative coordinate of the left edge of display
1115 area AREA of window W. AREA < 0 means return the left edge of the
1116 whole window, to the right of the left fringe of W. */
1117
1118 INLINE int
1119 window_box_left (w, area)
1120 struct window *w;
1121 int area;
1122 {
1123 struct frame *f = XFRAME (w->frame);
1124 int x;
1125
1126 if (w->pseudo_window_p)
1127 return FRAME_INTERNAL_BORDER_WIDTH (f);
1128
1129 x = (WINDOW_LEFT_EDGE_X (w)
1130 + window_box_left_offset (w, area));
1131
1132 return x;
1133 }
1134
1135
1136 /* Return the frame-relative coordinate of the right edge of display
1137 area AREA of window W. AREA < 0 means return the left edge of the
1138 whole window, to the left of the right fringe of W. */
1139
1140 INLINE int
1141 window_box_right (w, area)
1142 struct window *w;
1143 int area;
1144 {
1145 return window_box_left (w, area) + window_box_width (w, area);
1146 }
1147
1148 /* Get the bounding box of the display area AREA of window W, without
1149 mode lines, in frame-relative coordinates. AREA < 0 means the
1150 whole window, not including the left and right fringes of
1151 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1152 coordinates of the upper-left corner of the box. Return in
1153 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1154
1155 INLINE void
1156 window_box (w, area, box_x, box_y, box_width, box_height)
1157 struct window *w;
1158 int area;
1159 int *box_x, *box_y, *box_width, *box_height;
1160 {
1161 if (box_width)
1162 *box_width = window_box_width (w, area);
1163 if (box_height)
1164 *box_height = window_box_height (w);
1165 if (box_x)
1166 *box_x = window_box_left (w, area);
1167 if (box_y)
1168 {
1169 *box_y = WINDOW_TOP_EDGE_Y (w);
1170 if (WINDOW_WANTS_HEADER_LINE_P (w))
1171 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1172 }
1173 }
1174
1175
1176 /* Get the bounding box of the display area AREA of window W, without
1177 mode lines. AREA < 0 means the whole window, not including the
1178 left and right fringe of the window. Return in *TOP_LEFT_X
1179 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1180 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1181 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1182 box. */
1183
1184 INLINE void
1185 window_box_edges (w, area, top_left_x, top_left_y,
1186 bottom_right_x, bottom_right_y)
1187 struct window *w;
1188 int area;
1189 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1190 {
1191 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1192 bottom_right_y);
1193 *bottom_right_x += *top_left_x;
1194 *bottom_right_y += *top_left_y;
1195 }
1196
1197
1198 \f
1199 /***********************************************************************
1200 Utilities
1201 ***********************************************************************/
1202
1203 /* Return the bottom y-position of the line the iterator IT is in.
1204 This can modify IT's settings. */
1205
1206 int
1207 line_bottom_y (it)
1208 struct it *it;
1209 {
1210 int line_height = it->max_ascent + it->max_descent;
1211 int line_top_y = it->current_y;
1212
1213 if (line_height == 0)
1214 {
1215 if (last_height)
1216 line_height = last_height;
1217 else if (IT_CHARPOS (*it) < ZV)
1218 {
1219 move_it_by_lines (it, 1, 1);
1220 line_height = (it->max_ascent || it->max_descent
1221 ? it->max_ascent + it->max_descent
1222 : last_height);
1223 }
1224 else
1225 {
1226 struct glyph_row *row = it->glyph_row;
1227
1228 /* Use the default character height. */
1229 it->glyph_row = NULL;
1230 it->what = IT_CHARACTER;
1231 it->c = ' ';
1232 it->len = 1;
1233 PRODUCE_GLYPHS (it);
1234 line_height = it->ascent + it->descent;
1235 it->glyph_row = row;
1236 }
1237 }
1238
1239 return line_top_y + line_height;
1240 }
1241
1242
1243 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1244 1 if POS is visible and the line containing POS is fully visible.
1245 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1246 and header-lines heights. */
1247
1248 int
1249 pos_visible_p (w, charpos, fully, x, y, exact_mode_line_heights_p)
1250 struct window *w;
1251 int charpos, *fully, *x, *y, exact_mode_line_heights_p;
1252 {
1253 struct it it;
1254 struct text_pos top;
1255 int visible_p;
1256 struct buffer *old_buffer = NULL;
1257
1258 if (XBUFFER (w->buffer) != current_buffer)
1259 {
1260 old_buffer = current_buffer;
1261 set_buffer_internal_1 (XBUFFER (w->buffer));
1262 }
1263
1264 *fully = visible_p = 0;
1265 SET_TEXT_POS_FROM_MARKER (top, w->start);
1266
1267 /* Compute exact mode line heights, if requested. */
1268 if (exact_mode_line_heights_p)
1269 {
1270 if (WINDOW_WANTS_MODELINE_P (w))
1271 current_mode_line_height
1272 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1273 current_buffer->mode_line_format);
1274
1275 if (WINDOW_WANTS_HEADER_LINE_P (w))
1276 current_header_line_height
1277 = display_mode_line (w, HEADER_LINE_FACE_ID,
1278 current_buffer->header_line_format);
1279 }
1280
1281 start_display (&it, w, top);
1282 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1283 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1284
1285 /* Note that we may overshoot because of invisible text. */
1286 if (IT_CHARPOS (it) >= charpos)
1287 {
1288 int top_y = it.current_y;
1289 int bottom_y = line_bottom_y (&it);
1290 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1291
1292 if (top_y < window_top_y)
1293 visible_p = bottom_y > window_top_y;
1294 else if (top_y < it.last_visible_y)
1295 {
1296 visible_p = 1;
1297 *fully = bottom_y <= it.last_visible_y;
1298 }
1299 if (visible_p && x)
1300 {
1301 *x = it.current_x;
1302 *y = max (top_y + it.max_ascent - it.ascent, window_top_y);
1303 }
1304 }
1305 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1306 {
1307 struct it it2;
1308
1309 it2 = it;
1310 move_it_by_lines (&it, 1, 0);
1311 if (charpos < IT_CHARPOS (it))
1312 {
1313 visible_p = 1;
1314 if (x)
1315 {
1316 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1317 *x = it2.current_x;
1318 *y = it2.current_y + it2.max_ascent - it2.ascent;
1319 }
1320 }
1321 }
1322
1323 if (old_buffer)
1324 set_buffer_internal_1 (old_buffer);
1325
1326 current_header_line_height = current_mode_line_height = -1;
1327
1328 return visible_p;
1329 }
1330
1331
1332 /* Return the next character from STR which is MAXLEN bytes long.
1333 Return in *LEN the length of the character. This is like
1334 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1335 we find one, we return a `?', but with the length of the invalid
1336 character. */
1337
1338 static INLINE int
1339 string_char_and_length (str, maxlen, len)
1340 const unsigned char *str;
1341 int maxlen, *len;
1342 {
1343 int c;
1344
1345 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1346 if (!CHAR_VALID_P (c, 1))
1347 /* We may not change the length here because other places in Emacs
1348 don't use this function, i.e. they silently accept invalid
1349 characters. */
1350 c = '?';
1351
1352 return c;
1353 }
1354
1355
1356
1357 /* Given a position POS containing a valid character and byte position
1358 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1359
1360 static struct text_pos
1361 string_pos_nchars_ahead (pos, string, nchars)
1362 struct text_pos pos;
1363 Lisp_Object string;
1364 int nchars;
1365 {
1366 xassert (STRINGP (string) && nchars >= 0);
1367
1368 if (STRING_MULTIBYTE (string))
1369 {
1370 int rest = SBYTES (string) - BYTEPOS (pos);
1371 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1372 int len;
1373
1374 while (nchars--)
1375 {
1376 string_char_and_length (p, rest, &len);
1377 p += len, rest -= len;
1378 xassert (rest >= 0);
1379 CHARPOS (pos) += 1;
1380 BYTEPOS (pos) += len;
1381 }
1382 }
1383 else
1384 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1385
1386 return pos;
1387 }
1388
1389
1390 /* Value is the text position, i.e. character and byte position,
1391 for character position CHARPOS in STRING. */
1392
1393 static INLINE struct text_pos
1394 string_pos (charpos, string)
1395 int charpos;
1396 Lisp_Object string;
1397 {
1398 struct text_pos pos;
1399 xassert (STRINGP (string));
1400 xassert (charpos >= 0);
1401 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1402 return pos;
1403 }
1404
1405
1406 /* Value is a text position, i.e. character and byte position, for
1407 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1408 means recognize multibyte characters. */
1409
1410 static struct text_pos
1411 c_string_pos (charpos, s, multibyte_p)
1412 int charpos;
1413 unsigned char *s;
1414 int multibyte_p;
1415 {
1416 struct text_pos pos;
1417
1418 xassert (s != NULL);
1419 xassert (charpos >= 0);
1420
1421 if (multibyte_p)
1422 {
1423 int rest = strlen (s), len;
1424
1425 SET_TEXT_POS (pos, 0, 0);
1426 while (charpos--)
1427 {
1428 string_char_and_length (s, rest, &len);
1429 s += len, rest -= len;
1430 xassert (rest >= 0);
1431 CHARPOS (pos) += 1;
1432 BYTEPOS (pos) += len;
1433 }
1434 }
1435 else
1436 SET_TEXT_POS (pos, charpos, charpos);
1437
1438 return pos;
1439 }
1440
1441
1442 /* Value is the number of characters in C string S. MULTIBYTE_P
1443 non-zero means recognize multibyte characters. */
1444
1445 static int
1446 number_of_chars (s, multibyte_p)
1447 unsigned char *s;
1448 int multibyte_p;
1449 {
1450 int nchars;
1451
1452 if (multibyte_p)
1453 {
1454 int rest = strlen (s), len;
1455 unsigned char *p = (unsigned char *) s;
1456
1457 for (nchars = 0; rest > 0; ++nchars)
1458 {
1459 string_char_and_length (p, rest, &len);
1460 rest -= len, p += len;
1461 }
1462 }
1463 else
1464 nchars = strlen (s);
1465
1466 return nchars;
1467 }
1468
1469
1470 /* Compute byte position NEWPOS->bytepos corresponding to
1471 NEWPOS->charpos. POS is a known position in string STRING.
1472 NEWPOS->charpos must be >= POS.charpos. */
1473
1474 static void
1475 compute_string_pos (newpos, pos, string)
1476 struct text_pos *newpos, pos;
1477 Lisp_Object string;
1478 {
1479 xassert (STRINGP (string));
1480 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1481
1482 if (STRING_MULTIBYTE (string))
1483 *newpos = string_pos_nchars_ahead (pos, string,
1484 CHARPOS (*newpos) - CHARPOS (pos));
1485 else
1486 BYTEPOS (*newpos) = CHARPOS (*newpos);
1487 }
1488
1489 /* EXPORT:
1490 Return an estimation of the pixel height of mode or top lines on
1491 frame F. FACE_ID specifies what line's height to estimate. */
1492
1493 int
1494 estimate_mode_line_height (f, face_id)
1495 struct frame *f;
1496 enum face_id face_id;
1497 {
1498 #ifdef HAVE_WINDOW_SYSTEM
1499 if (FRAME_WINDOW_P (f))
1500 {
1501 int height = FONT_HEIGHT (FRAME_FONT (f));
1502
1503 /* This function is called so early when Emacs starts that the face
1504 cache and mode line face are not yet initialized. */
1505 if (FRAME_FACE_CACHE (f))
1506 {
1507 struct face *face = FACE_FROM_ID (f, face_id);
1508 if (face)
1509 {
1510 if (face->font)
1511 height = FONT_HEIGHT (face->font);
1512 if (face->box_line_width > 0)
1513 height += 2 * face->box_line_width;
1514 }
1515 }
1516
1517 return height;
1518 }
1519 #endif
1520
1521 return 1;
1522 }
1523
1524 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1525 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1526 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1527 not force the value into range. */
1528
1529 void
1530 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1531 FRAME_PTR f;
1532 register int pix_x, pix_y;
1533 int *x, *y;
1534 NativeRectangle *bounds;
1535 int noclip;
1536 {
1537
1538 #ifdef HAVE_WINDOW_SYSTEM
1539 if (FRAME_WINDOW_P (f))
1540 {
1541 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1542 even for negative values. */
1543 if (pix_x < 0)
1544 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1545 if (pix_y < 0)
1546 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1547
1548 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1549 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1550
1551 if (bounds)
1552 STORE_NATIVE_RECT (*bounds,
1553 FRAME_COL_TO_PIXEL_X (f, pix_x),
1554 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1555 FRAME_COLUMN_WIDTH (f) - 1,
1556 FRAME_LINE_HEIGHT (f) - 1);
1557
1558 if (!noclip)
1559 {
1560 if (pix_x < 0)
1561 pix_x = 0;
1562 else if (pix_x > FRAME_TOTAL_COLS (f))
1563 pix_x = FRAME_TOTAL_COLS (f);
1564
1565 if (pix_y < 0)
1566 pix_y = 0;
1567 else if (pix_y > FRAME_LINES (f))
1568 pix_y = FRAME_LINES (f);
1569 }
1570 }
1571 #endif
1572
1573 *x = pix_x;
1574 *y = pix_y;
1575 }
1576
1577
1578 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1579 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1580 can't tell the positions because W's display is not up to date,
1581 return 0. */
1582
1583 int
1584 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1585 struct window *w;
1586 int hpos, vpos;
1587 int *frame_x, *frame_y;
1588 {
1589 #ifdef HAVE_WINDOW_SYSTEM
1590 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1591 {
1592 int success_p;
1593
1594 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1595 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1596
1597 if (display_completed)
1598 {
1599 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1600 struct glyph *glyph = row->glyphs[TEXT_AREA];
1601 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1602
1603 hpos = row->x;
1604 vpos = row->y;
1605 while (glyph < end)
1606 {
1607 hpos += glyph->pixel_width;
1608 ++glyph;
1609 }
1610
1611 /* If first glyph is partially visible, its first visible position is still 0. */
1612 if (hpos < 0)
1613 hpos = 0;
1614
1615 success_p = 1;
1616 }
1617 else
1618 {
1619 hpos = vpos = 0;
1620 success_p = 0;
1621 }
1622
1623 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1624 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1625 return success_p;
1626 }
1627 #endif
1628
1629 *frame_x = hpos;
1630 *frame_y = vpos;
1631 return 1;
1632 }
1633
1634
1635 #ifdef HAVE_WINDOW_SYSTEM
1636
1637 /* Find the glyph under window-relative coordinates X/Y in window W.
1638 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1639 strings. Return in *HPOS and *VPOS the row and column number of
1640 the glyph found. Return in *AREA the glyph area containing X.
1641 Value is a pointer to the glyph found or null if X/Y is not on
1642 text, or we can't tell because W's current matrix is not up to
1643 date. */
1644
1645 static struct glyph *
1646 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1647 struct window *w;
1648 int x, y;
1649 int *hpos, *vpos, *dx, *dy, *area;
1650 {
1651 struct glyph *glyph, *end;
1652 struct glyph_row *row = NULL;
1653 int x0, i;
1654
1655 /* Find row containing Y. Give up if some row is not enabled. */
1656 for (i = 0; i < w->current_matrix->nrows; ++i)
1657 {
1658 row = MATRIX_ROW (w->current_matrix, i);
1659 if (!row->enabled_p)
1660 return NULL;
1661 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1662 break;
1663 }
1664
1665 *vpos = i;
1666 *hpos = 0;
1667
1668 /* Give up if Y is not in the window. */
1669 if (i == w->current_matrix->nrows)
1670 return NULL;
1671
1672 /* Get the glyph area containing X. */
1673 if (w->pseudo_window_p)
1674 {
1675 *area = TEXT_AREA;
1676 x0 = 0;
1677 }
1678 else
1679 {
1680 if (x < window_box_left_offset (w, TEXT_AREA))
1681 {
1682 *area = LEFT_MARGIN_AREA;
1683 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1684 }
1685 else if (x < window_box_right_offset (w, TEXT_AREA))
1686 {
1687 *area = TEXT_AREA;
1688 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1689 }
1690 else
1691 {
1692 *area = RIGHT_MARGIN_AREA;
1693 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1694 }
1695 }
1696
1697 /* Find glyph containing X. */
1698 glyph = row->glyphs[*area];
1699 end = glyph + row->used[*area];
1700 x -= x0;
1701 while (glyph < end && x >= glyph->pixel_width)
1702 {
1703 x -= glyph->pixel_width;
1704 ++glyph;
1705 }
1706
1707 if (glyph == end)
1708 return NULL;
1709
1710 if (dx)
1711 {
1712 *dx = x;
1713 *dy = y - (row->y + row->ascent - glyph->ascent);
1714 }
1715
1716 *hpos = glyph - row->glyphs[*area];
1717 return glyph;
1718 }
1719
1720
1721 /* EXPORT:
1722 Convert frame-relative x/y to coordinates relative to window W.
1723 Takes pseudo-windows into account. */
1724
1725 void
1726 frame_to_window_pixel_xy (w, x, y)
1727 struct window *w;
1728 int *x, *y;
1729 {
1730 if (w->pseudo_window_p)
1731 {
1732 /* A pseudo-window is always full-width, and starts at the
1733 left edge of the frame, plus a frame border. */
1734 struct frame *f = XFRAME (w->frame);
1735 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1736 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1737 }
1738 else
1739 {
1740 *x -= WINDOW_LEFT_EDGE_X (w);
1741 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1742 }
1743 }
1744
1745 /* EXPORT:
1746 Return in *R the clipping rectangle for glyph string S. */
1747
1748 void
1749 get_glyph_string_clip_rect (s, nr)
1750 struct glyph_string *s;
1751 NativeRectangle *nr;
1752 {
1753 XRectangle r;
1754
1755 if (s->row->full_width_p)
1756 {
1757 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1758 r.x = WINDOW_LEFT_EDGE_X (s->w);
1759 r.width = WINDOW_TOTAL_WIDTH (s->w);
1760
1761 /* Unless displaying a mode or menu bar line, which are always
1762 fully visible, clip to the visible part of the row. */
1763 if (s->w->pseudo_window_p)
1764 r.height = s->row->visible_height;
1765 else
1766 r.height = s->height;
1767 }
1768 else
1769 {
1770 /* This is a text line that may be partially visible. */
1771 r.x = window_box_left (s->w, s->area);
1772 r.width = window_box_width (s->w, s->area);
1773 r.height = s->row->visible_height;
1774 }
1775
1776 /* If S draws overlapping rows, it's sufficient to use the top and
1777 bottom of the window for clipping because this glyph string
1778 intentionally draws over other lines. */
1779 if (s->for_overlaps_p)
1780 {
1781 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1782 r.height = window_text_bottom_y (s->w) - r.y;
1783 }
1784 else
1785 {
1786 /* Don't use S->y for clipping because it doesn't take partially
1787 visible lines into account. For example, it can be negative for
1788 partially visible lines at the top of a window. */
1789 if (!s->row->full_width_p
1790 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1791 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1792 else
1793 r.y = max (0, s->row->y);
1794
1795 /* If drawing a tool-bar window, draw it over the internal border
1796 at the top of the window. */
1797 if (WINDOWP (s->f->tool_bar_window)
1798 && s->w == XWINDOW (s->f->tool_bar_window))
1799 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1800 }
1801
1802 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1803
1804 /* If drawing the cursor, don't let glyph draw outside its
1805 advertised boundaries. Cleartype does this under some circumstances. */
1806 if (s->hl == DRAW_CURSOR)
1807 {
1808 struct glyph *glyph = s->first_glyph;
1809 int height;
1810
1811 if (s->x > r.x)
1812 {
1813 r.width -= s->x - r.x;
1814 r.x = s->x;
1815 }
1816 r.width = min (r.width, glyph->pixel_width);
1817
1818 /* Don't draw cursor glyph taller than our actual glyph. */
1819 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1820 if (height < r.height)
1821 {
1822 int max_y = r.y + r.height;
1823 r.y = min (max_y, s->ybase + glyph->descent - height);
1824 r.height = min (max_y - r.y, height);
1825 }
1826 }
1827
1828 #ifdef CONVERT_FROM_XRECT
1829 CONVERT_FROM_XRECT (r, *nr);
1830 #else
1831 *nr = r;
1832 #endif
1833 }
1834
1835 #endif /* HAVE_WINDOW_SYSTEM */
1836
1837 \f
1838 /***********************************************************************
1839 Lisp form evaluation
1840 ***********************************************************************/
1841
1842 /* Error handler for safe_eval and safe_call. */
1843
1844 static Lisp_Object
1845 safe_eval_handler (arg)
1846 Lisp_Object arg;
1847 {
1848 add_to_log ("Error during redisplay: %s", arg, Qnil);
1849 return Qnil;
1850 }
1851
1852
1853 /* Evaluate SEXPR and return the result, or nil if something went
1854 wrong. Prevent redisplay during the evaluation. */
1855
1856 Lisp_Object
1857 safe_eval (sexpr)
1858 Lisp_Object sexpr;
1859 {
1860 Lisp_Object val;
1861
1862 if (inhibit_eval_during_redisplay)
1863 val = Qnil;
1864 else
1865 {
1866 int count = SPECPDL_INDEX ();
1867 struct gcpro gcpro1;
1868
1869 GCPRO1 (sexpr);
1870 specbind (Qinhibit_redisplay, Qt);
1871 /* Use Qt to ensure debugger does not run,
1872 so there is no possibility of wanting to redisplay. */
1873 val = internal_condition_case_1 (Feval, sexpr, Qt,
1874 safe_eval_handler);
1875 UNGCPRO;
1876 val = unbind_to (count, val);
1877 }
1878
1879 return val;
1880 }
1881
1882
1883 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1884 Return the result, or nil if something went wrong. Prevent
1885 redisplay during the evaluation. */
1886
1887 Lisp_Object
1888 safe_call (nargs, args)
1889 int nargs;
1890 Lisp_Object *args;
1891 {
1892 Lisp_Object val;
1893
1894 if (inhibit_eval_during_redisplay)
1895 val = Qnil;
1896 else
1897 {
1898 int count = SPECPDL_INDEX ();
1899 struct gcpro gcpro1;
1900
1901 GCPRO1 (args[0]);
1902 gcpro1.nvars = nargs;
1903 specbind (Qinhibit_redisplay, Qt);
1904 /* Use Qt to ensure debugger does not run,
1905 so there is no possibility of wanting to redisplay. */
1906 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1907 safe_eval_handler);
1908 UNGCPRO;
1909 val = unbind_to (count, val);
1910 }
1911
1912 return val;
1913 }
1914
1915
1916 /* Call function FN with one argument ARG.
1917 Return the result, or nil if something went wrong. */
1918
1919 Lisp_Object
1920 safe_call1 (fn, arg)
1921 Lisp_Object fn, arg;
1922 {
1923 Lisp_Object args[2];
1924 args[0] = fn;
1925 args[1] = arg;
1926 return safe_call (2, args);
1927 }
1928
1929
1930 \f
1931 /***********************************************************************
1932 Debugging
1933 ***********************************************************************/
1934
1935 #if 0
1936
1937 /* Define CHECK_IT to perform sanity checks on iterators.
1938 This is for debugging. It is too slow to do unconditionally. */
1939
1940 static void
1941 check_it (it)
1942 struct it *it;
1943 {
1944 if (it->method == next_element_from_string)
1945 {
1946 xassert (STRINGP (it->string));
1947 xassert (IT_STRING_CHARPOS (*it) >= 0);
1948 }
1949 else
1950 {
1951 xassert (IT_STRING_CHARPOS (*it) < 0);
1952 if (it->method == next_element_from_buffer)
1953 {
1954 /* Check that character and byte positions agree. */
1955 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1956 }
1957 }
1958
1959 if (it->dpvec)
1960 xassert (it->current.dpvec_index >= 0);
1961 else
1962 xassert (it->current.dpvec_index < 0);
1963 }
1964
1965 #define CHECK_IT(IT) check_it ((IT))
1966
1967 #else /* not 0 */
1968
1969 #define CHECK_IT(IT) (void) 0
1970
1971 #endif /* not 0 */
1972
1973
1974 #if GLYPH_DEBUG
1975
1976 /* Check that the window end of window W is what we expect it
1977 to be---the last row in the current matrix displaying text. */
1978
1979 static void
1980 check_window_end (w)
1981 struct window *w;
1982 {
1983 if (!MINI_WINDOW_P (w)
1984 && !NILP (w->window_end_valid))
1985 {
1986 struct glyph_row *row;
1987 xassert ((row = MATRIX_ROW (w->current_matrix,
1988 XFASTINT (w->window_end_vpos)),
1989 !row->enabled_p
1990 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1991 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1992 }
1993 }
1994
1995 #define CHECK_WINDOW_END(W) check_window_end ((W))
1996
1997 #else /* not GLYPH_DEBUG */
1998
1999 #define CHECK_WINDOW_END(W) (void) 0
2000
2001 #endif /* not GLYPH_DEBUG */
2002
2003
2004 \f
2005 /***********************************************************************
2006 Iterator initialization
2007 ***********************************************************************/
2008
2009 /* Initialize IT for displaying current_buffer in window W, starting
2010 at character position CHARPOS. CHARPOS < 0 means that no buffer
2011 position is specified which is useful when the iterator is assigned
2012 a position later. BYTEPOS is the byte position corresponding to
2013 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2014
2015 If ROW is not null, calls to produce_glyphs with IT as parameter
2016 will produce glyphs in that row.
2017
2018 BASE_FACE_ID is the id of a base face to use. It must be one of
2019 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2020 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2021 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2022
2023 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2024 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2025 will be initialized to use the corresponding mode line glyph row of
2026 the desired matrix of W. */
2027
2028 void
2029 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2030 struct it *it;
2031 struct window *w;
2032 int charpos, bytepos;
2033 struct glyph_row *row;
2034 enum face_id base_face_id;
2035 {
2036 int highlight_region_p;
2037
2038 /* Some precondition checks. */
2039 xassert (w != NULL && it != NULL);
2040 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2041 && charpos <= ZV));
2042
2043 /* If face attributes have been changed since the last redisplay,
2044 free realized faces now because they depend on face definitions
2045 that might have changed. Don't free faces while there might be
2046 desired matrices pending which reference these faces. */
2047 if (face_change_count && !inhibit_free_realized_faces)
2048 {
2049 face_change_count = 0;
2050 free_all_realized_faces (Qnil);
2051 }
2052
2053 /* Use one of the mode line rows of W's desired matrix if
2054 appropriate. */
2055 if (row == NULL)
2056 {
2057 if (base_face_id == MODE_LINE_FACE_ID
2058 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2059 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2060 else if (base_face_id == HEADER_LINE_FACE_ID)
2061 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2062 }
2063
2064 /* Clear IT. */
2065 bzero (it, sizeof *it);
2066 it->current.overlay_string_index = -1;
2067 it->current.dpvec_index = -1;
2068 it->base_face_id = base_face_id;
2069 it->string = Qnil;
2070 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2071
2072 /* The window in which we iterate over current_buffer: */
2073 XSETWINDOW (it->window, w);
2074 it->w = w;
2075 it->f = XFRAME (w->frame);
2076
2077 /* Extra space between lines (on window systems only). */
2078 if (base_face_id == DEFAULT_FACE_ID
2079 && FRAME_WINDOW_P (it->f))
2080 {
2081 if (NATNUMP (current_buffer->extra_line_spacing))
2082 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2083 else if (FLOATP (current_buffer->extra_line_spacing))
2084 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2085 * FRAME_LINE_HEIGHT (it->f));
2086 else if (it->f->extra_line_spacing > 0)
2087 it->extra_line_spacing = it->f->extra_line_spacing;
2088 it->max_extra_line_spacing = 0;
2089 }
2090
2091 /* If realized faces have been removed, e.g. because of face
2092 attribute changes of named faces, recompute them. When running
2093 in batch mode, the face cache of Vterminal_frame is null. If
2094 we happen to get called, make a dummy face cache. */
2095 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2096 init_frame_faces (it->f);
2097 if (FRAME_FACE_CACHE (it->f)->used == 0)
2098 recompute_basic_faces (it->f);
2099
2100 /* Current value of the `slice', `space-width', and 'height' properties. */
2101 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2102 it->space_width = Qnil;
2103 it->font_height = Qnil;
2104 it->override_ascent = -1;
2105
2106 /* Are control characters displayed as `^C'? */
2107 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2108
2109 /* -1 means everything between a CR and the following line end
2110 is invisible. >0 means lines indented more than this value are
2111 invisible. */
2112 it->selective = (INTEGERP (current_buffer->selective_display)
2113 ? XFASTINT (current_buffer->selective_display)
2114 : (!NILP (current_buffer->selective_display)
2115 ? -1 : 0));
2116 it->selective_display_ellipsis_p
2117 = !NILP (current_buffer->selective_display_ellipses);
2118
2119 /* Display table to use. */
2120 it->dp = window_display_table (w);
2121
2122 /* Are multibyte characters enabled in current_buffer? */
2123 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2124
2125 /* Non-zero if we should highlight the region. */
2126 highlight_region_p
2127 = (!NILP (Vtransient_mark_mode)
2128 && !NILP (current_buffer->mark_active)
2129 && XMARKER (current_buffer->mark)->buffer != 0);
2130
2131 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2132 start and end of a visible region in window IT->w. Set both to
2133 -1 to indicate no region. */
2134 if (highlight_region_p
2135 /* Maybe highlight only in selected window. */
2136 && (/* Either show region everywhere. */
2137 highlight_nonselected_windows
2138 /* Or show region in the selected window. */
2139 || w == XWINDOW (selected_window)
2140 /* Or show the region if we are in the mini-buffer and W is
2141 the window the mini-buffer refers to. */
2142 || (MINI_WINDOW_P (XWINDOW (selected_window))
2143 && WINDOWP (minibuf_selected_window)
2144 && w == XWINDOW (minibuf_selected_window))))
2145 {
2146 int charpos = marker_position (current_buffer->mark);
2147 it->region_beg_charpos = min (PT, charpos);
2148 it->region_end_charpos = max (PT, charpos);
2149 }
2150 else
2151 it->region_beg_charpos = it->region_end_charpos = -1;
2152
2153 /* Get the position at which the redisplay_end_trigger hook should
2154 be run, if it is to be run at all. */
2155 if (MARKERP (w->redisplay_end_trigger)
2156 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2157 it->redisplay_end_trigger_charpos
2158 = marker_position (w->redisplay_end_trigger);
2159 else if (INTEGERP (w->redisplay_end_trigger))
2160 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2161
2162 /* Correct bogus values of tab_width. */
2163 it->tab_width = XINT (current_buffer->tab_width);
2164 if (it->tab_width <= 0 || it->tab_width > 1000)
2165 it->tab_width = 8;
2166
2167 /* Are lines in the display truncated? */
2168 it->truncate_lines_p
2169 = (base_face_id != DEFAULT_FACE_ID
2170 || XINT (it->w->hscroll)
2171 || (truncate_partial_width_windows
2172 && !WINDOW_FULL_WIDTH_P (it->w))
2173 || !NILP (current_buffer->truncate_lines));
2174
2175 /* Get dimensions of truncation and continuation glyphs. These are
2176 displayed as fringe bitmaps under X, so we don't need them for such
2177 frames. */
2178 if (!FRAME_WINDOW_P (it->f))
2179 {
2180 if (it->truncate_lines_p)
2181 {
2182 /* We will need the truncation glyph. */
2183 xassert (it->glyph_row == NULL);
2184 produce_special_glyphs (it, IT_TRUNCATION);
2185 it->truncation_pixel_width = it->pixel_width;
2186 }
2187 else
2188 {
2189 /* We will need the continuation glyph. */
2190 xassert (it->glyph_row == NULL);
2191 produce_special_glyphs (it, IT_CONTINUATION);
2192 it->continuation_pixel_width = it->pixel_width;
2193 }
2194
2195 /* Reset these values to zero because the produce_special_glyphs
2196 above has changed them. */
2197 it->pixel_width = it->ascent = it->descent = 0;
2198 it->phys_ascent = it->phys_descent = 0;
2199 }
2200
2201 /* Set this after getting the dimensions of truncation and
2202 continuation glyphs, so that we don't produce glyphs when calling
2203 produce_special_glyphs, above. */
2204 it->glyph_row = row;
2205 it->area = TEXT_AREA;
2206
2207 /* Get the dimensions of the display area. The display area
2208 consists of the visible window area plus a horizontally scrolled
2209 part to the left of the window. All x-values are relative to the
2210 start of this total display area. */
2211 if (base_face_id != DEFAULT_FACE_ID)
2212 {
2213 /* Mode lines, menu bar in terminal frames. */
2214 it->first_visible_x = 0;
2215 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2216 }
2217 else
2218 {
2219 it->first_visible_x
2220 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2221 it->last_visible_x = (it->first_visible_x
2222 + window_box_width (w, TEXT_AREA));
2223
2224 /* If we truncate lines, leave room for the truncator glyph(s) at
2225 the right margin. Otherwise, leave room for the continuation
2226 glyph(s). Truncation and continuation glyphs are not inserted
2227 for window-based redisplay. */
2228 if (!FRAME_WINDOW_P (it->f))
2229 {
2230 if (it->truncate_lines_p)
2231 it->last_visible_x -= it->truncation_pixel_width;
2232 else
2233 it->last_visible_x -= it->continuation_pixel_width;
2234 }
2235
2236 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2237 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2238 }
2239
2240 /* Leave room for a border glyph. */
2241 if (!FRAME_WINDOW_P (it->f)
2242 && !WINDOW_RIGHTMOST_P (it->w))
2243 it->last_visible_x -= 1;
2244
2245 it->last_visible_y = window_text_bottom_y (w);
2246
2247 /* For mode lines and alike, arrange for the first glyph having a
2248 left box line if the face specifies a box. */
2249 if (base_face_id != DEFAULT_FACE_ID)
2250 {
2251 struct face *face;
2252
2253 it->face_id = base_face_id;
2254
2255 /* If we have a boxed mode line, make the first character appear
2256 with a left box line. */
2257 face = FACE_FROM_ID (it->f, base_face_id);
2258 if (face->box != FACE_NO_BOX)
2259 it->start_of_box_run_p = 1;
2260 }
2261
2262 /* If a buffer position was specified, set the iterator there,
2263 getting overlays and face properties from that position. */
2264 if (charpos >= BUF_BEG (current_buffer))
2265 {
2266 it->end_charpos = ZV;
2267 it->face_id = -1;
2268 IT_CHARPOS (*it) = charpos;
2269
2270 /* Compute byte position if not specified. */
2271 if (bytepos < charpos)
2272 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2273 else
2274 IT_BYTEPOS (*it) = bytepos;
2275
2276 it->start = it->current;
2277
2278 /* Compute faces etc. */
2279 reseat (it, it->current.pos, 1);
2280 }
2281
2282 CHECK_IT (it);
2283 }
2284
2285
2286 /* Initialize IT for the display of window W with window start POS. */
2287
2288 void
2289 start_display (it, w, pos)
2290 struct it *it;
2291 struct window *w;
2292 struct text_pos pos;
2293 {
2294 struct glyph_row *row;
2295 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2296
2297 row = w->desired_matrix->rows + first_vpos;
2298 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2299 it->first_vpos = first_vpos;
2300
2301 if (!it->truncate_lines_p)
2302 {
2303 int start_at_line_beg_p;
2304 int first_y = it->current_y;
2305
2306 /* If window start is not at a line start, skip forward to POS to
2307 get the correct continuation lines width. */
2308 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2309 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2310 if (!start_at_line_beg_p)
2311 {
2312 int new_x;
2313
2314 reseat_at_previous_visible_line_start (it);
2315 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2316
2317 new_x = it->current_x + it->pixel_width;
2318
2319 /* If lines are continued, this line may end in the middle
2320 of a multi-glyph character (e.g. a control character
2321 displayed as \003, or in the middle of an overlay
2322 string). In this case move_it_to above will not have
2323 taken us to the start of the continuation line but to the
2324 end of the continued line. */
2325 if (it->current_x > 0
2326 && !it->truncate_lines_p /* Lines are continued. */
2327 && (/* And glyph doesn't fit on the line. */
2328 new_x > it->last_visible_x
2329 /* Or it fits exactly and we're on a window
2330 system frame. */
2331 || (new_x == it->last_visible_x
2332 && FRAME_WINDOW_P (it->f))))
2333 {
2334 if (it->current.dpvec_index >= 0
2335 || it->current.overlay_string_index >= 0)
2336 {
2337 set_iterator_to_next (it, 1);
2338 move_it_in_display_line_to (it, -1, -1, 0);
2339 }
2340
2341 it->continuation_lines_width += it->current_x;
2342 }
2343
2344 /* We're starting a new display line, not affected by the
2345 height of the continued line, so clear the appropriate
2346 fields in the iterator structure. */
2347 it->max_ascent = it->max_descent = 0;
2348 it->max_phys_ascent = it->max_phys_descent = 0;
2349
2350 it->current_y = first_y;
2351 it->vpos = 0;
2352 it->current_x = it->hpos = 0;
2353 }
2354 }
2355
2356 #if 0 /* Don't assert the following because start_display is sometimes
2357 called intentionally with a window start that is not at a
2358 line start. Please leave this code in as a comment. */
2359
2360 /* Window start should be on a line start, now. */
2361 xassert (it->continuation_lines_width
2362 || IT_CHARPOS (it) == BEGV
2363 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2364 #endif /* 0 */
2365 }
2366
2367
2368 /* Return 1 if POS is a position in ellipses displayed for invisible
2369 text. W is the window we display, for text property lookup. */
2370
2371 static int
2372 in_ellipses_for_invisible_text_p (pos, w)
2373 struct display_pos *pos;
2374 struct window *w;
2375 {
2376 Lisp_Object prop, window;
2377 int ellipses_p = 0;
2378 int charpos = CHARPOS (pos->pos);
2379
2380 /* If POS specifies a position in a display vector, this might
2381 be for an ellipsis displayed for invisible text. We won't
2382 get the iterator set up for delivering that ellipsis unless
2383 we make sure that it gets aware of the invisible text. */
2384 if (pos->dpvec_index >= 0
2385 && pos->overlay_string_index < 0
2386 && CHARPOS (pos->string_pos) < 0
2387 && charpos > BEGV
2388 && (XSETWINDOW (window, w),
2389 prop = Fget_char_property (make_number (charpos),
2390 Qinvisible, window),
2391 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2392 {
2393 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2394 window);
2395 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2396 }
2397
2398 return ellipses_p;
2399 }
2400
2401
2402 /* Initialize IT for stepping through current_buffer in window W,
2403 starting at position POS that includes overlay string and display
2404 vector/ control character translation position information. Value
2405 is zero if there are overlay strings with newlines at POS. */
2406
2407 static int
2408 init_from_display_pos (it, w, pos)
2409 struct it *it;
2410 struct window *w;
2411 struct display_pos *pos;
2412 {
2413 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2414 int i, overlay_strings_with_newlines = 0;
2415
2416 /* If POS specifies a position in a display vector, this might
2417 be for an ellipsis displayed for invisible text. We won't
2418 get the iterator set up for delivering that ellipsis unless
2419 we make sure that it gets aware of the invisible text. */
2420 if (in_ellipses_for_invisible_text_p (pos, w))
2421 {
2422 --charpos;
2423 bytepos = 0;
2424 }
2425
2426 /* Keep in mind: the call to reseat in init_iterator skips invisible
2427 text, so we might end up at a position different from POS. This
2428 is only a problem when POS is a row start after a newline and an
2429 overlay starts there with an after-string, and the overlay has an
2430 invisible property. Since we don't skip invisible text in
2431 display_line and elsewhere immediately after consuming the
2432 newline before the row start, such a POS will not be in a string,
2433 but the call to init_iterator below will move us to the
2434 after-string. */
2435 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2436
2437 for (i = 0; i < it->n_overlay_strings; ++i)
2438 {
2439 const char *s = SDATA (it->overlay_strings[i]);
2440 const char *e = s + SBYTES (it->overlay_strings[i]);
2441
2442 while (s < e && *s != '\n')
2443 ++s;
2444
2445 if (s < e)
2446 {
2447 overlay_strings_with_newlines = 1;
2448 break;
2449 }
2450 }
2451
2452 /* If position is within an overlay string, set up IT to the right
2453 overlay string. */
2454 if (pos->overlay_string_index >= 0)
2455 {
2456 int relative_index;
2457
2458 /* If the first overlay string happens to have a `display'
2459 property for an image, the iterator will be set up for that
2460 image, and we have to undo that setup first before we can
2461 correct the overlay string index. */
2462 if (it->method == next_element_from_image)
2463 pop_it (it);
2464
2465 /* We already have the first chunk of overlay strings in
2466 IT->overlay_strings. Load more until the one for
2467 pos->overlay_string_index is in IT->overlay_strings. */
2468 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2469 {
2470 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2471 it->current.overlay_string_index = 0;
2472 while (n--)
2473 {
2474 load_overlay_strings (it, 0);
2475 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2476 }
2477 }
2478
2479 it->current.overlay_string_index = pos->overlay_string_index;
2480 relative_index = (it->current.overlay_string_index
2481 % OVERLAY_STRING_CHUNK_SIZE);
2482 it->string = it->overlay_strings[relative_index];
2483 xassert (STRINGP (it->string));
2484 it->current.string_pos = pos->string_pos;
2485 it->method = next_element_from_string;
2486 }
2487
2488 #if 0 /* This is bogus because POS not having an overlay string
2489 position does not mean it's after the string. Example: A
2490 line starting with a before-string and initialization of IT
2491 to the previous row's end position. */
2492 else if (it->current.overlay_string_index >= 0)
2493 {
2494 /* If POS says we're already after an overlay string ending at
2495 POS, make sure to pop the iterator because it will be in
2496 front of that overlay string. When POS is ZV, we've thereby
2497 also ``processed'' overlay strings at ZV. */
2498 while (it->sp)
2499 pop_it (it);
2500 it->current.overlay_string_index = -1;
2501 it->method = next_element_from_buffer;
2502 if (CHARPOS (pos->pos) == ZV)
2503 it->overlay_strings_at_end_processed_p = 1;
2504 }
2505 #endif /* 0 */
2506
2507 if (CHARPOS (pos->string_pos) >= 0)
2508 {
2509 /* Recorded position is not in an overlay string, but in another
2510 string. This can only be a string from a `display' property.
2511 IT should already be filled with that string. */
2512 it->current.string_pos = pos->string_pos;
2513 xassert (STRINGP (it->string));
2514 }
2515
2516 /* Restore position in display vector translations, control
2517 character translations or ellipses. */
2518 if (pos->dpvec_index >= 0)
2519 {
2520 if (it->dpvec == NULL)
2521 get_next_display_element (it);
2522 xassert (it->dpvec && it->current.dpvec_index == 0);
2523 it->current.dpvec_index = pos->dpvec_index;
2524 }
2525
2526 CHECK_IT (it);
2527 return !overlay_strings_with_newlines;
2528 }
2529
2530
2531 /* Initialize IT for stepping through current_buffer in window W
2532 starting at ROW->start. */
2533
2534 static void
2535 init_to_row_start (it, w, row)
2536 struct it *it;
2537 struct window *w;
2538 struct glyph_row *row;
2539 {
2540 init_from_display_pos (it, w, &row->start);
2541 it->start = row->start;
2542 it->continuation_lines_width = row->continuation_lines_width;
2543 CHECK_IT (it);
2544 }
2545
2546
2547 /* Initialize IT for stepping through current_buffer in window W
2548 starting in the line following ROW, i.e. starting at ROW->end.
2549 Value is zero if there are overlay strings with newlines at ROW's
2550 end position. */
2551
2552 static int
2553 init_to_row_end (it, w, row)
2554 struct it *it;
2555 struct window *w;
2556 struct glyph_row *row;
2557 {
2558 int success = 0;
2559
2560 if (init_from_display_pos (it, w, &row->end))
2561 {
2562 if (row->continued_p)
2563 it->continuation_lines_width
2564 = row->continuation_lines_width + row->pixel_width;
2565 CHECK_IT (it);
2566 success = 1;
2567 }
2568
2569 return success;
2570 }
2571
2572
2573
2574 \f
2575 /***********************************************************************
2576 Text properties
2577 ***********************************************************************/
2578
2579 /* Called when IT reaches IT->stop_charpos. Handle text property and
2580 overlay changes. Set IT->stop_charpos to the next position where
2581 to stop. */
2582
2583 static void
2584 handle_stop (it)
2585 struct it *it;
2586 {
2587 enum prop_handled handled;
2588 int handle_overlay_change_p = 1;
2589 struct props *p;
2590
2591 it->dpvec = NULL;
2592 it->current.dpvec_index = -1;
2593
2594 do
2595 {
2596 handled = HANDLED_NORMALLY;
2597
2598 /* Call text property handlers. */
2599 for (p = it_props; p->handler; ++p)
2600 {
2601 handled = p->handler (it);
2602
2603 if (handled == HANDLED_RECOMPUTE_PROPS)
2604 break;
2605 else if (handled == HANDLED_RETURN)
2606 return;
2607 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2608 handle_overlay_change_p = 0;
2609 }
2610
2611 if (handled != HANDLED_RECOMPUTE_PROPS)
2612 {
2613 /* Don't check for overlay strings below when set to deliver
2614 characters from a display vector. */
2615 if (it->method == next_element_from_display_vector)
2616 handle_overlay_change_p = 0;
2617
2618 /* Handle overlay changes. */
2619 if (handle_overlay_change_p)
2620 handled = handle_overlay_change (it);
2621
2622 /* Determine where to stop next. */
2623 if (handled == HANDLED_NORMALLY)
2624 compute_stop_pos (it);
2625 }
2626 }
2627 while (handled == HANDLED_RECOMPUTE_PROPS);
2628 }
2629
2630
2631 /* Compute IT->stop_charpos from text property and overlay change
2632 information for IT's current position. */
2633
2634 static void
2635 compute_stop_pos (it)
2636 struct it *it;
2637 {
2638 register INTERVAL iv, next_iv;
2639 Lisp_Object object, limit, position;
2640
2641 /* If nowhere else, stop at the end. */
2642 it->stop_charpos = it->end_charpos;
2643
2644 if (STRINGP (it->string))
2645 {
2646 /* Strings are usually short, so don't limit the search for
2647 properties. */
2648 object = it->string;
2649 limit = Qnil;
2650 position = make_number (IT_STRING_CHARPOS (*it));
2651 }
2652 else
2653 {
2654 int charpos;
2655
2656 /* If next overlay change is in front of the current stop pos
2657 (which is IT->end_charpos), stop there. Note: value of
2658 next_overlay_change is point-max if no overlay change
2659 follows. */
2660 charpos = next_overlay_change (IT_CHARPOS (*it));
2661 if (charpos < it->stop_charpos)
2662 it->stop_charpos = charpos;
2663
2664 /* If showing the region, we have to stop at the region
2665 start or end because the face might change there. */
2666 if (it->region_beg_charpos > 0)
2667 {
2668 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2669 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2670 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2671 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2672 }
2673
2674 /* Set up variables for computing the stop position from text
2675 property changes. */
2676 XSETBUFFER (object, current_buffer);
2677 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2678 position = make_number (IT_CHARPOS (*it));
2679
2680 }
2681
2682 /* Get the interval containing IT's position. Value is a null
2683 interval if there isn't such an interval. */
2684 iv = validate_interval_range (object, &position, &position, 0);
2685 if (!NULL_INTERVAL_P (iv))
2686 {
2687 Lisp_Object values_here[LAST_PROP_IDX];
2688 struct props *p;
2689
2690 /* Get properties here. */
2691 for (p = it_props; p->handler; ++p)
2692 values_here[p->idx] = textget (iv->plist, *p->name);
2693
2694 /* Look for an interval following iv that has different
2695 properties. */
2696 for (next_iv = next_interval (iv);
2697 (!NULL_INTERVAL_P (next_iv)
2698 && (NILP (limit)
2699 || XFASTINT (limit) > next_iv->position));
2700 next_iv = next_interval (next_iv))
2701 {
2702 for (p = it_props; p->handler; ++p)
2703 {
2704 Lisp_Object new_value;
2705
2706 new_value = textget (next_iv->plist, *p->name);
2707 if (!EQ (values_here[p->idx], new_value))
2708 break;
2709 }
2710
2711 if (p->handler)
2712 break;
2713 }
2714
2715 if (!NULL_INTERVAL_P (next_iv))
2716 {
2717 if (INTEGERP (limit)
2718 && next_iv->position >= XFASTINT (limit))
2719 /* No text property change up to limit. */
2720 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2721 else
2722 /* Text properties change in next_iv. */
2723 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2724 }
2725 }
2726
2727 xassert (STRINGP (it->string)
2728 || (it->stop_charpos >= BEGV
2729 && it->stop_charpos >= IT_CHARPOS (*it)));
2730 }
2731
2732
2733 /* Return the position of the next overlay change after POS in
2734 current_buffer. Value is point-max if no overlay change
2735 follows. This is like `next-overlay-change' but doesn't use
2736 xmalloc. */
2737
2738 static int
2739 next_overlay_change (pos)
2740 int pos;
2741 {
2742 int noverlays;
2743 int endpos;
2744 Lisp_Object *overlays;
2745 int i;
2746
2747 /* Get all overlays at the given position. */
2748 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
2749
2750 /* If any of these overlays ends before endpos,
2751 use its ending point instead. */
2752 for (i = 0; i < noverlays; ++i)
2753 {
2754 Lisp_Object oend;
2755 int oendpos;
2756
2757 oend = OVERLAY_END (overlays[i]);
2758 oendpos = OVERLAY_POSITION (oend);
2759 endpos = min (endpos, oendpos);
2760 }
2761
2762 return endpos;
2763 }
2764
2765
2766 \f
2767 /***********************************************************************
2768 Fontification
2769 ***********************************************************************/
2770
2771 /* Handle changes in the `fontified' property of the current buffer by
2772 calling hook functions from Qfontification_functions to fontify
2773 regions of text. */
2774
2775 static enum prop_handled
2776 handle_fontified_prop (it)
2777 struct it *it;
2778 {
2779 Lisp_Object prop, pos;
2780 enum prop_handled handled = HANDLED_NORMALLY;
2781
2782 /* Get the value of the `fontified' property at IT's current buffer
2783 position. (The `fontified' property doesn't have a special
2784 meaning in strings.) If the value is nil, call functions from
2785 Qfontification_functions. */
2786 if (!STRINGP (it->string)
2787 && it->s == NULL
2788 && !NILP (Vfontification_functions)
2789 && !NILP (Vrun_hooks)
2790 && (pos = make_number (IT_CHARPOS (*it)),
2791 prop = Fget_char_property (pos, Qfontified, Qnil),
2792 NILP (prop)))
2793 {
2794 int count = SPECPDL_INDEX ();
2795 Lisp_Object val;
2796
2797 val = Vfontification_functions;
2798 specbind (Qfontification_functions, Qnil);
2799
2800 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2801 safe_call1 (val, pos);
2802 else
2803 {
2804 Lisp_Object globals, fn;
2805 struct gcpro gcpro1, gcpro2;
2806
2807 globals = Qnil;
2808 GCPRO2 (val, globals);
2809
2810 for (; CONSP (val); val = XCDR (val))
2811 {
2812 fn = XCAR (val);
2813
2814 if (EQ (fn, Qt))
2815 {
2816 /* A value of t indicates this hook has a local
2817 binding; it means to run the global binding too.
2818 In a global value, t should not occur. If it
2819 does, we must ignore it to avoid an endless
2820 loop. */
2821 for (globals = Fdefault_value (Qfontification_functions);
2822 CONSP (globals);
2823 globals = XCDR (globals))
2824 {
2825 fn = XCAR (globals);
2826 if (!EQ (fn, Qt))
2827 safe_call1 (fn, pos);
2828 }
2829 }
2830 else
2831 safe_call1 (fn, pos);
2832 }
2833
2834 UNGCPRO;
2835 }
2836
2837 unbind_to (count, Qnil);
2838
2839 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2840 something. This avoids an endless loop if they failed to
2841 fontify the text for which reason ever. */
2842 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2843 handled = HANDLED_RECOMPUTE_PROPS;
2844 }
2845
2846 return handled;
2847 }
2848
2849
2850 \f
2851 /***********************************************************************
2852 Faces
2853 ***********************************************************************/
2854
2855 /* Set up iterator IT from face properties at its current position.
2856 Called from handle_stop. */
2857
2858 static enum prop_handled
2859 handle_face_prop (it)
2860 struct it *it;
2861 {
2862 int new_face_id, next_stop;
2863
2864 if (!STRINGP (it->string))
2865 {
2866 new_face_id
2867 = face_at_buffer_position (it->w,
2868 IT_CHARPOS (*it),
2869 it->region_beg_charpos,
2870 it->region_end_charpos,
2871 &next_stop,
2872 (IT_CHARPOS (*it)
2873 + TEXT_PROP_DISTANCE_LIMIT),
2874 0);
2875
2876 /* Is this a start of a run of characters with box face?
2877 Caveat: this can be called for a freshly initialized
2878 iterator; face_id is -1 in this case. We know that the new
2879 face will not change until limit, i.e. if the new face has a
2880 box, all characters up to limit will have one. But, as
2881 usual, we don't know whether limit is really the end. */
2882 if (new_face_id != it->face_id)
2883 {
2884 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2885
2886 /* If new face has a box but old face has not, this is
2887 the start of a run of characters with box, i.e. it has
2888 a shadow on the left side. The value of face_id of the
2889 iterator will be -1 if this is the initial call that gets
2890 the face. In this case, we have to look in front of IT's
2891 position and see whether there is a face != new_face_id. */
2892 it->start_of_box_run_p
2893 = (new_face->box != FACE_NO_BOX
2894 && (it->face_id >= 0
2895 || IT_CHARPOS (*it) == BEG
2896 || new_face_id != face_before_it_pos (it)));
2897 it->face_box_p = new_face->box != FACE_NO_BOX;
2898 }
2899 }
2900 else
2901 {
2902 int base_face_id, bufpos;
2903
2904 if (it->current.overlay_string_index >= 0)
2905 bufpos = IT_CHARPOS (*it);
2906 else
2907 bufpos = 0;
2908
2909 /* For strings from a buffer, i.e. overlay strings or strings
2910 from a `display' property, use the face at IT's current
2911 buffer position as the base face to merge with, so that
2912 overlay strings appear in the same face as surrounding
2913 text, unless they specify their own faces. */
2914 base_face_id = underlying_face_id (it);
2915
2916 new_face_id = face_at_string_position (it->w,
2917 it->string,
2918 IT_STRING_CHARPOS (*it),
2919 bufpos,
2920 it->region_beg_charpos,
2921 it->region_end_charpos,
2922 &next_stop,
2923 base_face_id, 0);
2924
2925 #if 0 /* This shouldn't be neccessary. Let's check it. */
2926 /* If IT is used to display a mode line we would really like to
2927 use the mode line face instead of the frame's default face. */
2928 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2929 && new_face_id == DEFAULT_FACE_ID)
2930 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2931 #endif
2932
2933 /* Is this a start of a run of characters with box? Caveat:
2934 this can be called for a freshly allocated iterator; face_id
2935 is -1 is this case. We know that the new face will not
2936 change until the next check pos, i.e. if the new face has a
2937 box, all characters up to that position will have a
2938 box. But, as usual, we don't know whether that position
2939 is really the end. */
2940 if (new_face_id != it->face_id)
2941 {
2942 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2943 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2944
2945 /* If new face has a box but old face hasn't, this is the
2946 start of a run of characters with box, i.e. it has a
2947 shadow on the left side. */
2948 it->start_of_box_run_p
2949 = new_face->box && (old_face == NULL || !old_face->box);
2950 it->face_box_p = new_face->box != FACE_NO_BOX;
2951 }
2952 }
2953
2954 it->face_id = new_face_id;
2955 return HANDLED_NORMALLY;
2956 }
2957
2958
2959 /* Return the ID of the face ``underlying'' IT's current position,
2960 which is in a string. If the iterator is associated with a
2961 buffer, return the face at IT's current buffer position.
2962 Otherwise, use the iterator's base_face_id. */
2963
2964 static int
2965 underlying_face_id (it)
2966 struct it *it;
2967 {
2968 int face_id = it->base_face_id, i;
2969
2970 xassert (STRINGP (it->string));
2971
2972 for (i = it->sp - 1; i >= 0; --i)
2973 if (NILP (it->stack[i].string))
2974 face_id = it->stack[i].face_id;
2975
2976 return face_id;
2977 }
2978
2979
2980 /* Compute the face one character before or after the current position
2981 of IT. BEFORE_P non-zero means get the face in front of IT's
2982 position. Value is the id of the face. */
2983
2984 static int
2985 face_before_or_after_it_pos (it, before_p)
2986 struct it *it;
2987 int before_p;
2988 {
2989 int face_id, limit;
2990 int next_check_charpos;
2991 struct text_pos pos;
2992
2993 xassert (it->s == NULL);
2994
2995 if (STRINGP (it->string))
2996 {
2997 int bufpos, base_face_id;
2998
2999 /* No face change past the end of the string (for the case
3000 we are padding with spaces). No face change before the
3001 string start. */
3002 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3003 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3004 return it->face_id;
3005
3006 /* Set pos to the position before or after IT's current position. */
3007 if (before_p)
3008 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3009 else
3010 /* For composition, we must check the character after the
3011 composition. */
3012 pos = (it->what == IT_COMPOSITION
3013 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3014 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3015
3016 if (it->current.overlay_string_index >= 0)
3017 bufpos = IT_CHARPOS (*it);
3018 else
3019 bufpos = 0;
3020
3021 base_face_id = underlying_face_id (it);
3022
3023 /* Get the face for ASCII, or unibyte. */
3024 face_id = face_at_string_position (it->w,
3025 it->string,
3026 CHARPOS (pos),
3027 bufpos,
3028 it->region_beg_charpos,
3029 it->region_end_charpos,
3030 &next_check_charpos,
3031 base_face_id, 0);
3032
3033 /* Correct the face for charsets different from ASCII. Do it
3034 for the multibyte case only. The face returned above is
3035 suitable for unibyte text if IT->string is unibyte. */
3036 if (STRING_MULTIBYTE (it->string))
3037 {
3038 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3039 int rest = SBYTES (it->string) - BYTEPOS (pos);
3040 int c, len;
3041 struct face *face = FACE_FROM_ID (it->f, face_id);
3042
3043 c = string_char_and_length (p, rest, &len);
3044 face_id = FACE_FOR_CHAR (it->f, face, c);
3045 }
3046 }
3047 else
3048 {
3049 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3050 || (IT_CHARPOS (*it) <= BEGV && before_p))
3051 return it->face_id;
3052
3053 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3054 pos = it->current.pos;
3055
3056 if (before_p)
3057 DEC_TEXT_POS (pos, it->multibyte_p);
3058 else
3059 {
3060 if (it->what == IT_COMPOSITION)
3061 /* For composition, we must check the position after the
3062 composition. */
3063 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3064 else
3065 INC_TEXT_POS (pos, it->multibyte_p);
3066 }
3067
3068 /* Determine face for CHARSET_ASCII, or unibyte. */
3069 face_id = face_at_buffer_position (it->w,
3070 CHARPOS (pos),
3071 it->region_beg_charpos,
3072 it->region_end_charpos,
3073 &next_check_charpos,
3074 limit, 0);
3075
3076 /* Correct the face for charsets different from ASCII. Do it
3077 for the multibyte case only. The face returned above is
3078 suitable for unibyte text if current_buffer is unibyte. */
3079 if (it->multibyte_p)
3080 {
3081 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3082 struct face *face = FACE_FROM_ID (it->f, face_id);
3083 face_id = FACE_FOR_CHAR (it->f, face, c);
3084 }
3085 }
3086
3087 return face_id;
3088 }
3089
3090
3091 \f
3092 /***********************************************************************
3093 Invisible text
3094 ***********************************************************************/
3095
3096 /* Set up iterator IT from invisible properties at its current
3097 position. Called from handle_stop. */
3098
3099 static enum prop_handled
3100 handle_invisible_prop (it)
3101 struct it *it;
3102 {
3103 enum prop_handled handled = HANDLED_NORMALLY;
3104
3105 if (STRINGP (it->string))
3106 {
3107 extern Lisp_Object Qinvisible;
3108 Lisp_Object prop, end_charpos, limit, charpos;
3109
3110 /* Get the value of the invisible text property at the
3111 current position. Value will be nil if there is no such
3112 property. */
3113 charpos = make_number (IT_STRING_CHARPOS (*it));
3114 prop = Fget_text_property (charpos, Qinvisible, it->string);
3115
3116 if (!NILP (prop)
3117 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3118 {
3119 handled = HANDLED_RECOMPUTE_PROPS;
3120
3121 /* Get the position at which the next change of the
3122 invisible text property can be found in IT->string.
3123 Value will be nil if the property value is the same for
3124 all the rest of IT->string. */
3125 XSETINT (limit, SCHARS (it->string));
3126 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3127 it->string, limit);
3128
3129 /* Text at current position is invisible. The next
3130 change in the property is at position end_charpos.
3131 Move IT's current position to that position. */
3132 if (INTEGERP (end_charpos)
3133 && XFASTINT (end_charpos) < XFASTINT (limit))
3134 {
3135 struct text_pos old;
3136 old = it->current.string_pos;
3137 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3138 compute_string_pos (&it->current.string_pos, old, it->string);
3139 }
3140 else
3141 {
3142 /* The rest of the string is invisible. If this is an
3143 overlay string, proceed with the next overlay string
3144 or whatever comes and return a character from there. */
3145 if (it->current.overlay_string_index >= 0)
3146 {
3147 next_overlay_string (it);
3148 /* Don't check for overlay strings when we just
3149 finished processing them. */
3150 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3151 }
3152 else
3153 {
3154 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3155 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3156 }
3157 }
3158 }
3159 }
3160 else
3161 {
3162 int invis_p, newpos, next_stop, start_charpos;
3163 Lisp_Object pos, prop, overlay;
3164
3165 /* First of all, is there invisible text at this position? */
3166 start_charpos = IT_CHARPOS (*it);
3167 pos = make_number (IT_CHARPOS (*it));
3168 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3169 &overlay);
3170 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3171
3172 /* If we are on invisible text, skip over it. */
3173 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3174 {
3175 /* Record whether we have to display an ellipsis for the
3176 invisible text. */
3177 int display_ellipsis_p = invis_p == 2;
3178
3179 handled = HANDLED_RECOMPUTE_PROPS;
3180
3181 /* Loop skipping over invisible text. The loop is left at
3182 ZV or with IT on the first char being visible again. */
3183 do
3184 {
3185 /* Try to skip some invisible text. Return value is the
3186 position reached which can be equal to IT's position
3187 if there is nothing invisible here. This skips both
3188 over invisible text properties and overlays with
3189 invisible property. */
3190 newpos = skip_invisible (IT_CHARPOS (*it),
3191 &next_stop, ZV, it->window);
3192
3193 /* If we skipped nothing at all we weren't at invisible
3194 text in the first place. If everything to the end of
3195 the buffer was skipped, end the loop. */
3196 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3197 invis_p = 0;
3198 else
3199 {
3200 /* We skipped some characters but not necessarily
3201 all there are. Check if we ended up on visible
3202 text. Fget_char_property returns the property of
3203 the char before the given position, i.e. if we
3204 get invis_p = 0, this means that the char at
3205 newpos is visible. */
3206 pos = make_number (newpos);
3207 prop = Fget_char_property (pos, Qinvisible, it->window);
3208 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3209 }
3210
3211 /* If we ended up on invisible text, proceed to
3212 skip starting with next_stop. */
3213 if (invis_p)
3214 IT_CHARPOS (*it) = next_stop;
3215 }
3216 while (invis_p);
3217
3218 /* The position newpos is now either ZV or on visible text. */
3219 IT_CHARPOS (*it) = newpos;
3220 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3221
3222 /* If there are before-strings at the start of invisible
3223 text, and the text is invisible because of a text
3224 property, arrange to show before-strings because 20.x did
3225 it that way. (If the text is invisible because of an
3226 overlay property instead of a text property, this is
3227 already handled in the overlay code.) */
3228 if (NILP (overlay)
3229 && get_overlay_strings (it, start_charpos))
3230 {
3231 handled = HANDLED_RECOMPUTE_PROPS;
3232 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3233 }
3234 else if (display_ellipsis_p)
3235 setup_for_ellipsis (it, 0);
3236 }
3237 }
3238
3239 return handled;
3240 }
3241
3242
3243 /* Make iterator IT return `...' next.
3244 Replaces LEN characters from buffer. */
3245
3246 static void
3247 setup_for_ellipsis (it, len)
3248 struct it *it;
3249 int len;
3250 {
3251 /* Use the display table definition for `...'. Invalid glyphs
3252 will be handled by the method returning elements from dpvec. */
3253 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3254 {
3255 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3256 it->dpvec = v->contents;
3257 it->dpend = v->contents + v->size;
3258 }
3259 else
3260 {
3261 /* Default `...'. */
3262 it->dpvec = default_invis_vector;
3263 it->dpend = default_invis_vector + 3;
3264 }
3265
3266 it->dpvec_char_len = len;
3267 it->current.dpvec_index = 0;
3268
3269 /* Remember the current face id in case glyphs specify faces.
3270 IT's face is restored in set_iterator_to_next. */
3271 it->saved_face_id = it->face_id;
3272 it->method = next_element_from_display_vector;
3273 }
3274
3275
3276 \f
3277 /***********************************************************************
3278 'display' property
3279 ***********************************************************************/
3280
3281 /* Set up iterator IT from `display' property at its current position.
3282 Called from handle_stop.
3283 We return HANDLED_RETURN if some part of the display property
3284 overrides the display of the buffer text itself.
3285 Otherwise we return HANDLED_NORMALLY. */
3286
3287 static enum prop_handled
3288 handle_display_prop (it)
3289 struct it *it;
3290 {
3291 Lisp_Object prop, object;
3292 struct text_pos *position;
3293 /* Nonzero if some property replaces the display of the text itself. */
3294 int display_replaced_p = 0;
3295
3296 if (STRINGP (it->string))
3297 {
3298 object = it->string;
3299 position = &it->current.string_pos;
3300 }
3301 else
3302 {
3303 object = it->w->buffer;
3304 position = &it->current.pos;
3305 }
3306
3307 /* Reset those iterator values set from display property values. */
3308 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3309 it->space_width = Qnil;
3310 it->font_height = Qnil;
3311 it->voffset = 0;
3312
3313 /* We don't support recursive `display' properties, i.e. string
3314 values that have a string `display' property, that have a string
3315 `display' property etc. */
3316 if (!it->string_from_display_prop_p)
3317 it->area = TEXT_AREA;
3318
3319 prop = Fget_char_property (make_number (position->charpos),
3320 Qdisplay, object);
3321 if (NILP (prop))
3322 return HANDLED_NORMALLY;
3323
3324 if (CONSP (prop)
3325 /* Simple properties. */
3326 && !EQ (XCAR (prop), Qimage)
3327 && !EQ (XCAR (prop), Qspace)
3328 && !EQ (XCAR (prop), Qwhen)
3329 && !EQ (XCAR (prop), Qslice)
3330 && !EQ (XCAR (prop), Qspace_width)
3331 && !EQ (XCAR (prop), Qheight)
3332 && !EQ (XCAR (prop), Qraise)
3333 /* Marginal area specifications. */
3334 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3335 && !EQ (XCAR (prop), Qleft_fringe)
3336 && !EQ (XCAR (prop), Qright_fringe)
3337 && !NILP (XCAR (prop)))
3338 {
3339 for (; CONSP (prop); prop = XCDR (prop))
3340 {
3341 if (handle_single_display_spec (it, XCAR (prop), object,
3342 position, display_replaced_p))
3343 display_replaced_p = 1;
3344 }
3345 }
3346 else if (VECTORP (prop))
3347 {
3348 int i;
3349 for (i = 0; i < ASIZE (prop); ++i)
3350 if (handle_single_display_spec (it, AREF (prop, i), object,
3351 position, display_replaced_p))
3352 display_replaced_p = 1;
3353 }
3354 else
3355 {
3356 if (handle_single_display_spec (it, prop, object, position, 0))
3357 display_replaced_p = 1;
3358 }
3359
3360 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3361 }
3362
3363
3364 /* Value is the position of the end of the `display' property starting
3365 at START_POS in OBJECT. */
3366
3367 static struct text_pos
3368 display_prop_end (it, object, start_pos)
3369 struct it *it;
3370 Lisp_Object object;
3371 struct text_pos start_pos;
3372 {
3373 Lisp_Object end;
3374 struct text_pos end_pos;
3375
3376 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3377 Qdisplay, object, Qnil);
3378 CHARPOS (end_pos) = XFASTINT (end);
3379 if (STRINGP (object))
3380 compute_string_pos (&end_pos, start_pos, it->string);
3381 else
3382 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3383
3384 return end_pos;
3385 }
3386
3387
3388 /* Set up IT from a single `display' specification PROP. OBJECT
3389 is the object in which the `display' property was found. *POSITION
3390 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3391 means that we previously saw a display specification which already
3392 replaced text display with something else, for example an image;
3393 we ignore such properties after the first one has been processed.
3394
3395 If PROP is a `space' or `image' specification, and in some other
3396 cases too, set *POSITION to the position where the `display'
3397 property ends.
3398
3399 Value is non-zero if something was found which replaces the display
3400 of buffer or string text. */
3401
3402 static int
3403 handle_single_display_spec (it, spec, object, position,
3404 display_replaced_before_p)
3405 struct it *it;
3406 Lisp_Object spec;
3407 Lisp_Object object;
3408 struct text_pos *position;
3409 int display_replaced_before_p;
3410 {
3411 Lisp_Object form;
3412 Lisp_Object location, value;
3413 struct text_pos start_pos;
3414 int valid_p;
3415
3416 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3417 If the result is non-nil, use VALUE instead of SPEC. */
3418 form = Qt;
3419 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3420 {
3421 spec = XCDR (spec);
3422 if (!CONSP (spec))
3423 return 0;
3424 form = XCAR (spec);
3425 spec = XCDR (spec);
3426 }
3427
3428 if (!NILP (form) && !EQ (form, Qt))
3429 {
3430 int count = SPECPDL_INDEX ();
3431 struct gcpro gcpro1;
3432
3433 /* Bind `object' to the object having the `display' property, a
3434 buffer or string. Bind `position' to the position in the
3435 object where the property was found, and `buffer-position'
3436 to the current position in the buffer. */
3437 specbind (Qobject, object);
3438 specbind (Qposition, make_number (CHARPOS (*position)));
3439 specbind (Qbuffer_position,
3440 make_number (STRINGP (object)
3441 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3442 GCPRO1 (form);
3443 form = safe_eval (form);
3444 UNGCPRO;
3445 unbind_to (count, Qnil);
3446 }
3447
3448 if (NILP (form))
3449 return 0;
3450
3451 /* Handle `(height HEIGHT)' specifications. */
3452 if (CONSP (spec)
3453 && EQ (XCAR (spec), Qheight)
3454 && CONSP (XCDR (spec)))
3455 {
3456 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3457 return 0;
3458
3459 it->font_height = XCAR (XCDR (spec));
3460 if (!NILP (it->font_height))
3461 {
3462 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3463 int new_height = -1;
3464
3465 if (CONSP (it->font_height)
3466 && (EQ (XCAR (it->font_height), Qplus)
3467 || EQ (XCAR (it->font_height), Qminus))
3468 && CONSP (XCDR (it->font_height))
3469 && INTEGERP (XCAR (XCDR (it->font_height))))
3470 {
3471 /* `(+ N)' or `(- N)' where N is an integer. */
3472 int steps = XINT (XCAR (XCDR (it->font_height)));
3473 if (EQ (XCAR (it->font_height), Qplus))
3474 steps = - steps;
3475 it->face_id = smaller_face (it->f, it->face_id, steps);
3476 }
3477 else if (FUNCTIONP (it->font_height))
3478 {
3479 /* Call function with current height as argument.
3480 Value is the new height. */
3481 Lisp_Object height;
3482 height = safe_call1 (it->font_height,
3483 face->lface[LFACE_HEIGHT_INDEX]);
3484 if (NUMBERP (height))
3485 new_height = XFLOATINT (height);
3486 }
3487 else if (NUMBERP (it->font_height))
3488 {
3489 /* Value is a multiple of the canonical char height. */
3490 struct face *face;
3491
3492 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3493 new_height = (XFLOATINT (it->font_height)
3494 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3495 }
3496 else
3497 {
3498 /* Evaluate IT->font_height with `height' bound to the
3499 current specified height to get the new height. */
3500 int count = SPECPDL_INDEX ();
3501
3502 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3503 value = safe_eval (it->font_height);
3504 unbind_to (count, Qnil);
3505
3506 if (NUMBERP (value))
3507 new_height = XFLOATINT (value);
3508 }
3509
3510 if (new_height > 0)
3511 it->face_id = face_with_height (it->f, it->face_id, new_height);
3512 }
3513
3514 return 0;
3515 }
3516
3517 /* Handle `(space_width WIDTH)'. */
3518 if (CONSP (spec)
3519 && EQ (XCAR (spec), Qspace_width)
3520 && CONSP (XCDR (spec)))
3521 {
3522 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3523 return 0;
3524
3525 value = XCAR (XCDR (spec));
3526 if (NUMBERP (value) && XFLOATINT (value) > 0)
3527 it->space_width = value;
3528
3529 return 0;
3530 }
3531
3532 /* Handle `(slice X Y WIDTH HEIGHT)'. */
3533 if (CONSP (spec)
3534 && EQ (XCAR (spec), Qslice))
3535 {
3536 Lisp_Object tem;
3537
3538 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3539 return 0;
3540
3541 if (tem = XCDR (spec), CONSP (tem))
3542 {
3543 it->slice.x = XCAR (tem);
3544 if (tem = XCDR (tem), CONSP (tem))
3545 {
3546 it->slice.y = XCAR (tem);
3547 if (tem = XCDR (tem), CONSP (tem))
3548 {
3549 it->slice.width = XCAR (tem);
3550 if (tem = XCDR (tem), CONSP (tem))
3551 it->slice.height = XCAR (tem);
3552 }
3553 }
3554 }
3555
3556 return 0;
3557 }
3558
3559 /* Handle `(raise FACTOR)'. */
3560 if (CONSP (spec)
3561 && EQ (XCAR (spec), Qraise)
3562 && CONSP (XCDR (spec)))
3563 {
3564 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3565 return 0;
3566
3567 #ifdef HAVE_WINDOW_SYSTEM
3568 value = XCAR (XCDR (spec));
3569 if (NUMBERP (value))
3570 {
3571 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3572 it->voffset = - (XFLOATINT (value)
3573 * (FONT_HEIGHT (face->font)));
3574 }
3575 #endif /* HAVE_WINDOW_SYSTEM */
3576
3577 return 0;
3578 }
3579
3580 /* Don't handle the other kinds of display specifications
3581 inside a string that we got from a `display' property. */
3582 if (it->string_from_display_prop_p)
3583 return 0;
3584
3585 /* Characters having this form of property are not displayed, so
3586 we have to find the end of the property. */
3587 start_pos = *position;
3588 *position = display_prop_end (it, object, start_pos);
3589 value = Qnil;
3590
3591 /* Stop the scan at that end position--we assume that all
3592 text properties change there. */
3593 it->stop_charpos = position->charpos;
3594
3595 /* Handle `(left-fringe BITMAP [FACE])'
3596 and `(right-fringe BITMAP [FACE])'. */
3597 if (CONSP (spec)
3598 && (EQ (XCAR (spec), Qleft_fringe)
3599 || EQ (XCAR (spec), Qright_fringe))
3600 && CONSP (XCDR (spec)))
3601 {
3602 int face_id = DEFAULT_FACE_ID;
3603 int fringe_bitmap;
3604
3605 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3606 /* If we return here, POSITION has been advanced
3607 across the text with this property. */
3608 return 0;
3609
3610 #ifdef HAVE_WINDOW_SYSTEM
3611 value = XCAR (XCDR (spec));
3612 if (!SYMBOLP (value)
3613 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
3614 /* If we return here, POSITION has been advanced
3615 across the text with this property. */
3616 return 0;
3617
3618 if (CONSP (XCDR (XCDR (spec))))
3619 {
3620 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
3621 int face_id2 = lookup_named_face (it->f, face_name, 'A', 0);
3622 if (face_id2 >= 0)
3623 face_id = face_id2;
3624 }
3625
3626 /* Save current settings of IT so that we can restore them
3627 when we are finished with the glyph property value. */
3628
3629 push_it (it);
3630
3631 it->area = TEXT_AREA;
3632 it->what = IT_IMAGE;
3633 it->image_id = -1; /* no image */
3634 it->position = start_pos;
3635 it->object = NILP (object) ? it->w->buffer : object;
3636 it->method = next_element_from_image;
3637 it->face_id = face_id;
3638
3639 /* Say that we haven't consumed the characters with
3640 `display' property yet. The call to pop_it in
3641 set_iterator_to_next will clean this up. */
3642 *position = start_pos;
3643
3644 if (EQ (XCAR (spec), Qleft_fringe))
3645 {
3646 it->left_user_fringe_bitmap = fringe_bitmap;
3647 it->left_user_fringe_face_id = face_id;
3648 }
3649 else
3650 {
3651 it->right_user_fringe_bitmap = fringe_bitmap;
3652 it->right_user_fringe_face_id = face_id;
3653 }
3654 #endif /* HAVE_WINDOW_SYSTEM */
3655 return 1;
3656 }
3657
3658 /* Prepare to handle `((margin left-margin) ...)',
3659 `((margin right-margin) ...)' and `((margin nil) ...)'
3660 prefixes for display specifications. */
3661 location = Qunbound;
3662 if (CONSP (spec) && CONSP (XCAR (spec)))
3663 {
3664 Lisp_Object tem;
3665
3666 value = XCDR (spec);
3667 if (CONSP (value))
3668 value = XCAR (value);
3669
3670 tem = XCAR (spec);
3671 if (EQ (XCAR (tem), Qmargin)
3672 && (tem = XCDR (tem),
3673 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3674 (NILP (tem)
3675 || EQ (tem, Qleft_margin)
3676 || EQ (tem, Qright_margin))))
3677 location = tem;
3678 }
3679
3680 if (EQ (location, Qunbound))
3681 {
3682 location = Qnil;
3683 value = spec;
3684 }
3685
3686 /* After this point, VALUE is the property after any
3687 margin prefix has been stripped. It must be a string,
3688 an image specification, or `(space ...)'.
3689
3690 LOCATION specifies where to display: `left-margin',
3691 `right-margin' or nil. */
3692
3693 valid_p = (STRINGP (value)
3694 #ifdef HAVE_WINDOW_SYSTEM
3695 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
3696 #endif /* not HAVE_WINDOW_SYSTEM */
3697 || (CONSP (value) && EQ (XCAR (value), Qspace)));
3698
3699 if (valid_p && !display_replaced_before_p)
3700 {
3701 /* Save current settings of IT so that we can restore them
3702 when we are finished with the glyph property value. */
3703 push_it (it);
3704
3705 if (NILP (location))
3706 it->area = TEXT_AREA;
3707 else if (EQ (location, Qleft_margin))
3708 it->area = LEFT_MARGIN_AREA;
3709 else
3710 it->area = RIGHT_MARGIN_AREA;
3711
3712 if (STRINGP (value))
3713 {
3714 it->string = value;
3715 it->multibyte_p = STRING_MULTIBYTE (it->string);
3716 it->current.overlay_string_index = -1;
3717 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3718 it->end_charpos = it->string_nchars = SCHARS (it->string);
3719 it->method = next_element_from_string;
3720 it->stop_charpos = 0;
3721 it->string_from_display_prop_p = 1;
3722 /* Say that we haven't consumed the characters with
3723 `display' property yet. The call to pop_it in
3724 set_iterator_to_next will clean this up. */
3725 *position = start_pos;
3726 }
3727 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3728 {
3729 it->method = next_element_from_stretch;
3730 it->object = value;
3731 it->current.pos = it->position = start_pos;
3732 }
3733 #ifdef HAVE_WINDOW_SYSTEM
3734 else
3735 {
3736 it->what = IT_IMAGE;
3737 it->image_id = lookup_image (it->f, value);
3738 it->position = start_pos;
3739 it->object = NILP (object) ? it->w->buffer : object;
3740 it->method = next_element_from_image;
3741
3742 /* Say that we haven't consumed the characters with
3743 `display' property yet. The call to pop_it in
3744 set_iterator_to_next will clean this up. */
3745 *position = start_pos;
3746 }
3747 #endif /* HAVE_WINDOW_SYSTEM */
3748
3749 return 1;
3750 }
3751
3752 /* Invalid property or property not supported. Restore
3753 POSITION to what it was before. */
3754 *position = start_pos;
3755 return 0;
3756 }
3757
3758
3759 /* Check if SPEC is a display specification value whose text should be
3760 treated as intangible. */
3761
3762 static int
3763 single_display_spec_intangible_p (prop)
3764 Lisp_Object prop;
3765 {
3766 /* Skip over `when FORM'. */
3767 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3768 {
3769 prop = XCDR (prop);
3770 if (!CONSP (prop))
3771 return 0;
3772 prop = XCDR (prop);
3773 }
3774
3775 if (STRINGP (prop))
3776 return 1;
3777
3778 if (!CONSP (prop))
3779 return 0;
3780
3781 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3782 we don't need to treat text as intangible. */
3783 if (EQ (XCAR (prop), Qmargin))
3784 {
3785 prop = XCDR (prop);
3786 if (!CONSP (prop))
3787 return 0;
3788
3789 prop = XCDR (prop);
3790 if (!CONSP (prop)
3791 || EQ (XCAR (prop), Qleft_margin)
3792 || EQ (XCAR (prop), Qright_margin))
3793 return 0;
3794 }
3795
3796 return (CONSP (prop)
3797 && (EQ (XCAR (prop), Qimage)
3798 || EQ (XCAR (prop), Qspace)));
3799 }
3800
3801
3802 /* Check if PROP is a display property value whose text should be
3803 treated as intangible. */
3804
3805 int
3806 display_prop_intangible_p (prop)
3807 Lisp_Object prop;
3808 {
3809 if (CONSP (prop)
3810 && CONSP (XCAR (prop))
3811 && !EQ (Qmargin, XCAR (XCAR (prop))))
3812 {
3813 /* A list of sub-properties. */
3814 while (CONSP (prop))
3815 {
3816 if (single_display_spec_intangible_p (XCAR (prop)))
3817 return 1;
3818 prop = XCDR (prop);
3819 }
3820 }
3821 else if (VECTORP (prop))
3822 {
3823 /* A vector of sub-properties. */
3824 int i;
3825 for (i = 0; i < ASIZE (prop); ++i)
3826 if (single_display_spec_intangible_p (AREF (prop, i)))
3827 return 1;
3828 }
3829 else
3830 return single_display_spec_intangible_p (prop);
3831
3832 return 0;
3833 }
3834
3835
3836 /* Return 1 if PROP is a display sub-property value containing STRING. */
3837
3838 static int
3839 single_display_spec_string_p (prop, string)
3840 Lisp_Object prop, string;
3841 {
3842 if (EQ (string, prop))
3843 return 1;
3844
3845 /* Skip over `when FORM'. */
3846 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3847 {
3848 prop = XCDR (prop);
3849 if (!CONSP (prop))
3850 return 0;
3851 prop = XCDR (prop);
3852 }
3853
3854 if (CONSP (prop))
3855 /* Skip over `margin LOCATION'. */
3856 if (EQ (XCAR (prop), Qmargin))
3857 {
3858 prop = XCDR (prop);
3859 if (!CONSP (prop))
3860 return 0;
3861
3862 prop = XCDR (prop);
3863 if (!CONSP (prop))
3864 return 0;
3865 }
3866
3867 return CONSP (prop) && EQ (XCAR (prop), string);
3868 }
3869
3870
3871 /* Return 1 if STRING appears in the `display' property PROP. */
3872
3873 static int
3874 display_prop_string_p (prop, string)
3875 Lisp_Object prop, string;
3876 {
3877 if (CONSP (prop)
3878 && CONSP (XCAR (prop))
3879 && !EQ (Qmargin, XCAR (XCAR (prop))))
3880 {
3881 /* A list of sub-properties. */
3882 while (CONSP (prop))
3883 {
3884 if (single_display_spec_string_p (XCAR (prop), string))
3885 return 1;
3886 prop = XCDR (prop);
3887 }
3888 }
3889 else if (VECTORP (prop))
3890 {
3891 /* A vector of sub-properties. */
3892 int i;
3893 for (i = 0; i < ASIZE (prop); ++i)
3894 if (single_display_spec_string_p (AREF (prop, i), string))
3895 return 1;
3896 }
3897 else
3898 return single_display_spec_string_p (prop, string);
3899
3900 return 0;
3901 }
3902
3903
3904 /* Determine from which buffer position in W's buffer STRING comes
3905 from. AROUND_CHARPOS is an approximate position where it could
3906 be from. Value is the buffer position or 0 if it couldn't be
3907 determined.
3908
3909 W's buffer must be current.
3910
3911 This function is necessary because we don't record buffer positions
3912 in glyphs generated from strings (to keep struct glyph small).
3913 This function may only use code that doesn't eval because it is
3914 called asynchronously from note_mouse_highlight. */
3915
3916 int
3917 string_buffer_position (w, string, around_charpos)
3918 struct window *w;
3919 Lisp_Object string;
3920 int around_charpos;
3921 {
3922 Lisp_Object limit, prop, pos;
3923 const int MAX_DISTANCE = 1000;
3924 int found = 0;
3925
3926 pos = make_number (around_charpos);
3927 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3928 while (!found && !EQ (pos, limit))
3929 {
3930 prop = Fget_char_property (pos, Qdisplay, Qnil);
3931 if (!NILP (prop) && display_prop_string_p (prop, string))
3932 found = 1;
3933 else
3934 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3935 }
3936
3937 if (!found)
3938 {
3939 pos = make_number (around_charpos);
3940 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3941 while (!found && !EQ (pos, limit))
3942 {
3943 prop = Fget_char_property (pos, Qdisplay, Qnil);
3944 if (!NILP (prop) && display_prop_string_p (prop, string))
3945 found = 1;
3946 else
3947 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3948 limit);
3949 }
3950 }
3951
3952 return found ? XINT (pos) : 0;
3953 }
3954
3955
3956 \f
3957 /***********************************************************************
3958 `composition' property
3959 ***********************************************************************/
3960
3961 /* Set up iterator IT from `composition' property at its current
3962 position. Called from handle_stop. */
3963
3964 static enum prop_handled
3965 handle_composition_prop (it)
3966 struct it *it;
3967 {
3968 Lisp_Object prop, string;
3969 int pos, pos_byte, end;
3970 enum prop_handled handled = HANDLED_NORMALLY;
3971
3972 if (STRINGP (it->string))
3973 {
3974 pos = IT_STRING_CHARPOS (*it);
3975 pos_byte = IT_STRING_BYTEPOS (*it);
3976 string = it->string;
3977 }
3978 else
3979 {
3980 pos = IT_CHARPOS (*it);
3981 pos_byte = IT_BYTEPOS (*it);
3982 string = Qnil;
3983 }
3984
3985 /* If there's a valid composition and point is not inside of the
3986 composition (in the case that the composition is from the current
3987 buffer), draw a glyph composed from the composition components. */
3988 if (find_composition (pos, -1, &pos, &end, &prop, string)
3989 && COMPOSITION_VALID_P (pos, end, prop)
3990 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3991 {
3992 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3993
3994 if (id >= 0)
3995 {
3996 it->method = next_element_from_composition;
3997 it->cmp_id = id;
3998 it->cmp_len = COMPOSITION_LENGTH (prop);
3999 /* For a terminal, draw only the first character of the
4000 components. */
4001 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4002 it->len = (STRINGP (it->string)
4003 ? string_char_to_byte (it->string, end)
4004 : CHAR_TO_BYTE (end)) - pos_byte;
4005 it->stop_charpos = end;
4006 handled = HANDLED_RETURN;
4007 }
4008 }
4009
4010 return handled;
4011 }
4012
4013
4014 \f
4015 /***********************************************************************
4016 Overlay strings
4017 ***********************************************************************/
4018
4019 /* The following structure is used to record overlay strings for
4020 later sorting in load_overlay_strings. */
4021
4022 struct overlay_entry
4023 {
4024 Lisp_Object overlay;
4025 Lisp_Object string;
4026 int priority;
4027 int after_string_p;
4028 };
4029
4030
4031 /* Set up iterator IT from overlay strings at its current position.
4032 Called from handle_stop. */
4033
4034 static enum prop_handled
4035 handle_overlay_change (it)
4036 struct it *it;
4037 {
4038 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4039 return HANDLED_RECOMPUTE_PROPS;
4040 else
4041 return HANDLED_NORMALLY;
4042 }
4043
4044
4045 /* Set up the next overlay string for delivery by IT, if there is an
4046 overlay string to deliver. Called by set_iterator_to_next when the
4047 end of the current overlay string is reached. If there are more
4048 overlay strings to display, IT->string and
4049 IT->current.overlay_string_index are set appropriately here.
4050 Otherwise IT->string is set to nil. */
4051
4052 static void
4053 next_overlay_string (it)
4054 struct it *it;
4055 {
4056 ++it->current.overlay_string_index;
4057 if (it->current.overlay_string_index == it->n_overlay_strings)
4058 {
4059 /* No more overlay strings. Restore IT's settings to what
4060 they were before overlay strings were processed, and
4061 continue to deliver from current_buffer. */
4062 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4063
4064 pop_it (it);
4065 xassert (it->stop_charpos >= BEGV
4066 && it->stop_charpos <= it->end_charpos);
4067 it->string = Qnil;
4068 it->current.overlay_string_index = -1;
4069 SET_TEXT_POS (it->current.string_pos, -1, -1);
4070 it->n_overlay_strings = 0;
4071 it->method = next_element_from_buffer;
4072
4073 /* If we're at the end of the buffer, record that we have
4074 processed the overlay strings there already, so that
4075 next_element_from_buffer doesn't try it again. */
4076 if (IT_CHARPOS (*it) >= it->end_charpos)
4077 it->overlay_strings_at_end_processed_p = 1;
4078
4079 /* If we have to display `...' for invisible text, set
4080 the iterator up for that. */
4081 if (display_ellipsis_p)
4082 setup_for_ellipsis (it, 0);
4083 }
4084 else
4085 {
4086 /* There are more overlay strings to process. If
4087 IT->current.overlay_string_index has advanced to a position
4088 where we must load IT->overlay_strings with more strings, do
4089 it. */
4090 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4091
4092 if (it->current.overlay_string_index && i == 0)
4093 load_overlay_strings (it, 0);
4094
4095 /* Initialize IT to deliver display elements from the overlay
4096 string. */
4097 it->string = it->overlay_strings[i];
4098 it->multibyte_p = STRING_MULTIBYTE (it->string);
4099 SET_TEXT_POS (it->current.string_pos, 0, 0);
4100 it->method = next_element_from_string;
4101 it->stop_charpos = 0;
4102 }
4103
4104 CHECK_IT (it);
4105 }
4106
4107
4108 /* Compare two overlay_entry structures E1 and E2. Used as a
4109 comparison function for qsort in load_overlay_strings. Overlay
4110 strings for the same position are sorted so that
4111
4112 1. All after-strings come in front of before-strings, except
4113 when they come from the same overlay.
4114
4115 2. Within after-strings, strings are sorted so that overlay strings
4116 from overlays with higher priorities come first.
4117
4118 2. Within before-strings, strings are sorted so that overlay
4119 strings from overlays with higher priorities come last.
4120
4121 Value is analogous to strcmp. */
4122
4123
4124 static int
4125 compare_overlay_entries (e1, e2)
4126 void *e1, *e2;
4127 {
4128 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4129 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4130 int result;
4131
4132 if (entry1->after_string_p != entry2->after_string_p)
4133 {
4134 /* Let after-strings appear in front of before-strings if
4135 they come from different overlays. */
4136 if (EQ (entry1->overlay, entry2->overlay))
4137 result = entry1->after_string_p ? 1 : -1;
4138 else
4139 result = entry1->after_string_p ? -1 : 1;
4140 }
4141 else if (entry1->after_string_p)
4142 /* After-strings sorted in order of decreasing priority. */
4143 result = entry2->priority - entry1->priority;
4144 else
4145 /* Before-strings sorted in order of increasing priority. */
4146 result = entry1->priority - entry2->priority;
4147
4148 return result;
4149 }
4150
4151
4152 /* Load the vector IT->overlay_strings with overlay strings from IT's
4153 current buffer position, or from CHARPOS if that is > 0. Set
4154 IT->n_overlays to the total number of overlay strings found.
4155
4156 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4157 a time. On entry into load_overlay_strings,
4158 IT->current.overlay_string_index gives the number of overlay
4159 strings that have already been loaded by previous calls to this
4160 function.
4161
4162 IT->add_overlay_start contains an additional overlay start
4163 position to consider for taking overlay strings from, if non-zero.
4164 This position comes into play when the overlay has an `invisible'
4165 property, and both before and after-strings. When we've skipped to
4166 the end of the overlay, because of its `invisible' property, we
4167 nevertheless want its before-string to appear.
4168 IT->add_overlay_start will contain the overlay start position
4169 in this case.
4170
4171 Overlay strings are sorted so that after-string strings come in
4172 front of before-string strings. Within before and after-strings,
4173 strings are sorted by overlay priority. See also function
4174 compare_overlay_entries. */
4175
4176 static void
4177 load_overlay_strings (it, charpos)
4178 struct it *it;
4179 int charpos;
4180 {
4181 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4182 Lisp_Object overlay, window, str, invisible;
4183 struct Lisp_Overlay *ov;
4184 int start, end;
4185 int size = 20;
4186 int n = 0, i, j, invis_p;
4187 struct overlay_entry *entries
4188 = (struct overlay_entry *) alloca (size * sizeof *entries);
4189
4190 if (charpos <= 0)
4191 charpos = IT_CHARPOS (*it);
4192
4193 /* Append the overlay string STRING of overlay OVERLAY to vector
4194 `entries' which has size `size' and currently contains `n'
4195 elements. AFTER_P non-zero means STRING is an after-string of
4196 OVERLAY. */
4197 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4198 do \
4199 { \
4200 Lisp_Object priority; \
4201 \
4202 if (n == size) \
4203 { \
4204 int new_size = 2 * size; \
4205 struct overlay_entry *old = entries; \
4206 entries = \
4207 (struct overlay_entry *) alloca (new_size \
4208 * sizeof *entries); \
4209 bcopy (old, entries, size * sizeof *entries); \
4210 size = new_size; \
4211 } \
4212 \
4213 entries[n].string = (STRING); \
4214 entries[n].overlay = (OVERLAY); \
4215 priority = Foverlay_get ((OVERLAY), Qpriority); \
4216 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4217 entries[n].after_string_p = (AFTER_P); \
4218 ++n; \
4219 } \
4220 while (0)
4221
4222 /* Process overlay before the overlay center. */
4223 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4224 {
4225 XSETMISC (overlay, ov);
4226 xassert (OVERLAYP (overlay));
4227 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4228 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4229
4230 if (end < charpos)
4231 break;
4232
4233 /* Skip this overlay if it doesn't start or end at IT's current
4234 position. */
4235 if (end != charpos && start != charpos)
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;
4242
4243 /* If the text ``under'' the overlay is invisible, both before-
4244 and after-strings from this overlay are visible; start and
4245 end position are indistinguishable. */
4246 invisible = Foverlay_get (overlay, Qinvisible);
4247 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4248
4249 /* If overlay has a non-empty before-string, record it. */
4250 if ((start == charpos || (end == charpos && invis_p))
4251 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4252 && SCHARS (str))
4253 RECORD_OVERLAY_STRING (overlay, str, 0);
4254
4255 /* If overlay has a non-empty after-string, record it. */
4256 if ((end == charpos || (start == charpos && invis_p))
4257 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4258 && SCHARS (str))
4259 RECORD_OVERLAY_STRING (overlay, str, 1);
4260 }
4261
4262 /* Process overlays after the overlay center. */
4263 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4264 {
4265 XSETMISC (overlay, ov);
4266 xassert (OVERLAYP (overlay));
4267 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4268 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4269
4270 if (start > charpos)
4271 break;
4272
4273 /* Skip this overlay if it doesn't start or end at IT's current
4274 position. */
4275 if (end != charpos && start != charpos)
4276 continue;
4277
4278 /* Skip this overlay if it doesn't apply to IT->w. */
4279 window = Foverlay_get (overlay, Qwindow);
4280 if (WINDOWP (window) && XWINDOW (window) != it->w)
4281 continue;
4282
4283 /* If the text ``under'' the overlay is invisible, it has a zero
4284 dimension, and both before- and after-strings apply. */
4285 invisible = Foverlay_get (overlay, Qinvisible);
4286 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4287
4288 /* If overlay has a non-empty before-string, record it. */
4289 if ((start == charpos || (end == charpos && invis_p))
4290 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4291 && SCHARS (str))
4292 RECORD_OVERLAY_STRING (overlay, str, 0);
4293
4294 /* If overlay has a non-empty after-string, record it. */
4295 if ((end == charpos || (start == charpos && invis_p))
4296 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4297 && SCHARS (str))
4298 RECORD_OVERLAY_STRING (overlay, str, 1);
4299 }
4300
4301 #undef RECORD_OVERLAY_STRING
4302
4303 /* Sort entries. */
4304 if (n > 1)
4305 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4306
4307 /* Record the total number of strings to process. */
4308 it->n_overlay_strings = n;
4309
4310 /* IT->current.overlay_string_index is the number of overlay strings
4311 that have already been consumed by IT. Copy some of the
4312 remaining overlay strings to IT->overlay_strings. */
4313 i = 0;
4314 j = it->current.overlay_string_index;
4315 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4316 it->overlay_strings[i++] = entries[j++].string;
4317
4318 CHECK_IT (it);
4319 }
4320
4321
4322 /* Get the first chunk of overlay strings at IT's current buffer
4323 position, or at CHARPOS if that is > 0. Value is non-zero if at
4324 least one overlay string was found. */
4325
4326 static int
4327 get_overlay_strings (it, charpos)
4328 struct it *it;
4329 int charpos;
4330 {
4331 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4332 process. This fills IT->overlay_strings with strings, and sets
4333 IT->n_overlay_strings to the total number of strings to process.
4334 IT->pos.overlay_string_index has to be set temporarily to zero
4335 because load_overlay_strings needs this; it must be set to -1
4336 when no overlay strings are found because a zero value would
4337 indicate a position in the first overlay string. */
4338 it->current.overlay_string_index = 0;
4339 load_overlay_strings (it, charpos);
4340
4341 /* If we found overlay strings, set up IT to deliver display
4342 elements from the first one. Otherwise set up IT to deliver
4343 from current_buffer. */
4344 if (it->n_overlay_strings)
4345 {
4346 /* Make sure we know settings in current_buffer, so that we can
4347 restore meaningful values when we're done with the overlay
4348 strings. */
4349 compute_stop_pos (it);
4350 xassert (it->face_id >= 0);
4351
4352 /* Save IT's settings. They are restored after all overlay
4353 strings have been processed. */
4354 xassert (it->sp == 0);
4355 push_it (it);
4356
4357 /* Set up IT to deliver display elements from the first overlay
4358 string. */
4359 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4360 it->string = it->overlay_strings[0];
4361 it->stop_charpos = 0;
4362 xassert (STRINGP (it->string));
4363 it->end_charpos = SCHARS (it->string);
4364 it->multibyte_p = STRING_MULTIBYTE (it->string);
4365 it->method = next_element_from_string;
4366 }
4367 else
4368 {
4369 it->string = Qnil;
4370 it->current.overlay_string_index = -1;
4371 it->method = next_element_from_buffer;
4372 }
4373
4374 CHECK_IT (it);
4375
4376 /* Value is non-zero if we found at least one overlay string. */
4377 return STRINGP (it->string);
4378 }
4379
4380
4381 \f
4382 /***********************************************************************
4383 Saving and restoring state
4384 ***********************************************************************/
4385
4386 /* Save current settings of IT on IT->stack. Called, for example,
4387 before setting up IT for an overlay string, to be able to restore
4388 IT's settings to what they were after the overlay string has been
4389 processed. */
4390
4391 static void
4392 push_it (it)
4393 struct it *it;
4394 {
4395 struct iterator_stack_entry *p;
4396
4397 xassert (it->sp < 2);
4398 p = it->stack + it->sp;
4399
4400 p->stop_charpos = it->stop_charpos;
4401 xassert (it->face_id >= 0);
4402 p->face_id = it->face_id;
4403 p->string = it->string;
4404 p->pos = it->current;
4405 p->end_charpos = it->end_charpos;
4406 p->string_nchars = it->string_nchars;
4407 p->area = it->area;
4408 p->multibyte_p = it->multibyte_p;
4409 p->slice = it->slice;
4410 p->space_width = it->space_width;
4411 p->font_height = it->font_height;
4412 p->voffset = it->voffset;
4413 p->string_from_display_prop_p = it->string_from_display_prop_p;
4414 p->display_ellipsis_p = 0;
4415 ++it->sp;
4416 }
4417
4418
4419 /* Restore IT's settings from IT->stack. Called, for example, when no
4420 more overlay strings must be processed, and we return to delivering
4421 display elements from a buffer, or when the end of a string from a
4422 `display' property is reached and we return to delivering display
4423 elements from an overlay string, or from a buffer. */
4424
4425 static void
4426 pop_it (it)
4427 struct it *it;
4428 {
4429 struct iterator_stack_entry *p;
4430
4431 xassert (it->sp > 0);
4432 --it->sp;
4433 p = it->stack + it->sp;
4434 it->stop_charpos = p->stop_charpos;
4435 it->face_id = p->face_id;
4436 it->string = p->string;
4437 it->current = p->pos;
4438 it->end_charpos = p->end_charpos;
4439 it->string_nchars = p->string_nchars;
4440 it->area = p->area;
4441 it->multibyte_p = p->multibyte_p;
4442 it->slice = p->slice;
4443 it->space_width = p->space_width;
4444 it->font_height = p->font_height;
4445 it->voffset = p->voffset;
4446 it->string_from_display_prop_p = p->string_from_display_prop_p;
4447 }
4448
4449
4450 \f
4451 /***********************************************************************
4452 Moving over lines
4453 ***********************************************************************/
4454
4455 /* Set IT's current position to the previous line start. */
4456
4457 static void
4458 back_to_previous_line_start (it)
4459 struct it *it;
4460 {
4461 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4462 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4463 }
4464
4465
4466 /* Move IT to the next line start.
4467
4468 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4469 we skipped over part of the text (as opposed to moving the iterator
4470 continuously over the text). Otherwise, don't change the value
4471 of *SKIPPED_P.
4472
4473 Newlines may come from buffer text, overlay strings, or strings
4474 displayed via the `display' property. That's the reason we can't
4475 simply use find_next_newline_no_quit.
4476
4477 Note that this function may not skip over invisible text that is so
4478 because of text properties and immediately follows a newline. If
4479 it would, function reseat_at_next_visible_line_start, when called
4480 from set_iterator_to_next, would effectively make invisible
4481 characters following a newline part of the wrong glyph row, which
4482 leads to wrong cursor motion. */
4483
4484 static int
4485 forward_to_next_line_start (it, skipped_p)
4486 struct it *it;
4487 int *skipped_p;
4488 {
4489 int old_selective, newline_found_p, n;
4490 const int MAX_NEWLINE_DISTANCE = 500;
4491
4492 /* If already on a newline, just consume it to avoid unintended
4493 skipping over invisible text below. */
4494 if (it->what == IT_CHARACTER
4495 && it->c == '\n'
4496 && CHARPOS (it->position) == IT_CHARPOS (*it))
4497 {
4498 set_iterator_to_next (it, 0);
4499 it->c = 0;
4500 return 1;
4501 }
4502
4503 /* Don't handle selective display in the following. It's (a)
4504 unnecessary because it's done by the caller, and (b) leads to an
4505 infinite recursion because next_element_from_ellipsis indirectly
4506 calls this function. */
4507 old_selective = it->selective;
4508 it->selective = 0;
4509
4510 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4511 from buffer text. */
4512 for (n = newline_found_p = 0;
4513 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4514 n += STRINGP (it->string) ? 0 : 1)
4515 {
4516 if (!get_next_display_element (it))
4517 return 0;
4518 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4519 set_iterator_to_next (it, 0);
4520 }
4521
4522 /* If we didn't find a newline near enough, see if we can use a
4523 short-cut. */
4524 if (!newline_found_p)
4525 {
4526 int start = IT_CHARPOS (*it);
4527 int limit = find_next_newline_no_quit (start, 1);
4528 Lisp_Object pos;
4529
4530 xassert (!STRINGP (it->string));
4531
4532 /* If there isn't any `display' property in sight, and no
4533 overlays, we can just use the position of the newline in
4534 buffer text. */
4535 if (it->stop_charpos >= limit
4536 || ((pos = Fnext_single_property_change (make_number (start),
4537 Qdisplay,
4538 Qnil, make_number (limit)),
4539 NILP (pos))
4540 && next_overlay_change (start) == ZV))
4541 {
4542 IT_CHARPOS (*it) = limit;
4543 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4544 *skipped_p = newline_found_p = 1;
4545 }
4546 else
4547 {
4548 while (get_next_display_element (it)
4549 && !newline_found_p)
4550 {
4551 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4552 set_iterator_to_next (it, 0);
4553 }
4554 }
4555 }
4556
4557 it->selective = old_selective;
4558 return newline_found_p;
4559 }
4560
4561
4562 /* Set IT's current position to the previous visible line start. Skip
4563 invisible text that is so either due to text properties or due to
4564 selective display. Caution: this does not change IT->current_x and
4565 IT->hpos. */
4566
4567 static void
4568 back_to_previous_visible_line_start (it)
4569 struct it *it;
4570 {
4571 int visible_p = 0;
4572
4573 /* Go back one newline if not on BEGV already. */
4574 if (IT_CHARPOS (*it) > BEGV)
4575 back_to_previous_line_start (it);
4576
4577 /* Move over lines that are invisible because of selective display
4578 or text properties. */
4579 while (IT_CHARPOS (*it) > BEGV
4580 && !visible_p)
4581 {
4582 visible_p = 1;
4583
4584 /* If selective > 0, then lines indented more than that values
4585 are invisible. */
4586 if (it->selective > 0
4587 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4588 (double) it->selective)) /* iftc */
4589 visible_p = 0;
4590 else
4591 {
4592 Lisp_Object prop;
4593
4594 /* Check the newline before point for invisibility. */
4595 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
4596 Qinvisible, it->window);
4597 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4598 visible_p = 0;
4599 }
4600
4601 #if 0
4602 /* Commenting this out fixes the bug described in
4603 http://www.math.ku.dk/~larsh/emacs/emacs-loops-on-large-images/test-case.txt. */
4604 if (visible_p)
4605 {
4606 struct it it2 = *it;
4607
4608 if (handle_display_prop (&it2) == HANDLED_RETURN)
4609 visible_p = 0;
4610 }
4611 #endif
4612
4613 /* Back one more newline if the current one is invisible. */
4614 if (!visible_p)
4615 back_to_previous_line_start (it);
4616 }
4617
4618 xassert (IT_CHARPOS (*it) >= BEGV);
4619 xassert (IT_CHARPOS (*it) == BEGV
4620 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4621 CHECK_IT (it);
4622 }
4623
4624
4625 /* Reseat iterator IT at the previous visible line start. Skip
4626 invisible text that is so either due to text properties or due to
4627 selective display. At the end, update IT's overlay information,
4628 face information etc. */
4629
4630 void
4631 reseat_at_previous_visible_line_start (it)
4632 struct it *it;
4633 {
4634 back_to_previous_visible_line_start (it);
4635 reseat (it, it->current.pos, 1);
4636 CHECK_IT (it);
4637 }
4638
4639
4640 /* Reseat iterator IT on the next visible line start in the current
4641 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4642 preceding the line start. Skip over invisible text that is so
4643 because of selective display. Compute faces, overlays etc at the
4644 new position. Note that this function does not skip over text that
4645 is invisible because of text properties. */
4646
4647 static void
4648 reseat_at_next_visible_line_start (it, on_newline_p)
4649 struct it *it;
4650 int on_newline_p;
4651 {
4652 int newline_found_p, skipped_p = 0;
4653
4654 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4655
4656 /* Skip over lines that are invisible because they are indented
4657 more than the value of IT->selective. */
4658 if (it->selective > 0)
4659 while (IT_CHARPOS (*it) < ZV
4660 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4661 (double) it->selective)) /* iftc */
4662 {
4663 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4664 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4665 }
4666
4667 /* Position on the newline if that's what's requested. */
4668 if (on_newline_p && newline_found_p)
4669 {
4670 if (STRINGP (it->string))
4671 {
4672 if (IT_STRING_CHARPOS (*it) > 0)
4673 {
4674 --IT_STRING_CHARPOS (*it);
4675 --IT_STRING_BYTEPOS (*it);
4676 }
4677 }
4678 else if (IT_CHARPOS (*it) > BEGV)
4679 {
4680 --IT_CHARPOS (*it);
4681 --IT_BYTEPOS (*it);
4682 reseat (it, it->current.pos, 0);
4683 }
4684 }
4685 else if (skipped_p)
4686 reseat (it, it->current.pos, 0);
4687
4688 CHECK_IT (it);
4689 }
4690
4691
4692 \f
4693 /***********************************************************************
4694 Changing an iterator's position
4695 ***********************************************************************/
4696
4697 /* Change IT's current position to POS in current_buffer. If FORCE_P
4698 is non-zero, always check for text properties at the new position.
4699 Otherwise, text properties are only looked up if POS >=
4700 IT->check_charpos of a property. */
4701
4702 static void
4703 reseat (it, pos, force_p)
4704 struct it *it;
4705 struct text_pos pos;
4706 int force_p;
4707 {
4708 int original_pos = IT_CHARPOS (*it);
4709
4710 reseat_1 (it, pos, 0);
4711
4712 /* Determine where to check text properties. Avoid doing it
4713 where possible because text property lookup is very expensive. */
4714 if (force_p
4715 || CHARPOS (pos) > it->stop_charpos
4716 || CHARPOS (pos) < original_pos)
4717 handle_stop (it);
4718
4719 CHECK_IT (it);
4720 }
4721
4722
4723 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4724 IT->stop_pos to POS, also. */
4725
4726 static void
4727 reseat_1 (it, pos, set_stop_p)
4728 struct it *it;
4729 struct text_pos pos;
4730 int set_stop_p;
4731 {
4732 /* Don't call this function when scanning a C string. */
4733 xassert (it->s == NULL);
4734
4735 /* POS must be a reasonable value. */
4736 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4737
4738 it->current.pos = it->position = pos;
4739 XSETBUFFER (it->object, current_buffer);
4740 it->end_charpos = ZV;
4741 it->dpvec = NULL;
4742 it->current.dpvec_index = -1;
4743 it->current.overlay_string_index = -1;
4744 IT_STRING_CHARPOS (*it) = -1;
4745 IT_STRING_BYTEPOS (*it) = -1;
4746 it->string = Qnil;
4747 it->method = next_element_from_buffer;
4748 /* RMS: I added this to fix a bug in move_it_vertically_backward
4749 where it->area continued to relate to the starting point
4750 for the backward motion. Bug report from
4751 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4752 However, I am not sure whether reseat still does the right thing
4753 in general after this change. */
4754 it->area = TEXT_AREA;
4755 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4756 it->sp = 0;
4757 it->face_before_selective_p = 0;
4758
4759 if (set_stop_p)
4760 it->stop_charpos = CHARPOS (pos);
4761 }
4762
4763
4764 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4765 If S is non-null, it is a C string to iterate over. Otherwise,
4766 STRING gives a Lisp string to iterate over.
4767
4768 If PRECISION > 0, don't return more then PRECISION number of
4769 characters from the string.
4770
4771 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4772 characters have been returned. FIELD_WIDTH < 0 means an infinite
4773 field width.
4774
4775 MULTIBYTE = 0 means disable processing of multibyte characters,
4776 MULTIBYTE > 0 means enable it,
4777 MULTIBYTE < 0 means use IT->multibyte_p.
4778
4779 IT must be initialized via a prior call to init_iterator before
4780 calling this function. */
4781
4782 static void
4783 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4784 struct it *it;
4785 unsigned char *s;
4786 Lisp_Object string;
4787 int charpos;
4788 int precision, field_width, multibyte;
4789 {
4790 /* No region in strings. */
4791 it->region_beg_charpos = it->region_end_charpos = -1;
4792
4793 /* No text property checks performed by default, but see below. */
4794 it->stop_charpos = -1;
4795
4796 /* Set iterator position and end position. */
4797 bzero (&it->current, sizeof it->current);
4798 it->current.overlay_string_index = -1;
4799 it->current.dpvec_index = -1;
4800 xassert (charpos >= 0);
4801
4802 /* If STRING is specified, use its multibyteness, otherwise use the
4803 setting of MULTIBYTE, if specified. */
4804 if (multibyte >= 0)
4805 it->multibyte_p = multibyte > 0;
4806
4807 if (s == NULL)
4808 {
4809 xassert (STRINGP (string));
4810 it->string = string;
4811 it->s = NULL;
4812 it->end_charpos = it->string_nchars = SCHARS (string);
4813 it->method = next_element_from_string;
4814 it->current.string_pos = string_pos (charpos, string);
4815 }
4816 else
4817 {
4818 it->s = s;
4819 it->string = Qnil;
4820
4821 /* Note that we use IT->current.pos, not it->current.string_pos,
4822 for displaying C strings. */
4823 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4824 if (it->multibyte_p)
4825 {
4826 it->current.pos = c_string_pos (charpos, s, 1);
4827 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4828 }
4829 else
4830 {
4831 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4832 it->end_charpos = it->string_nchars = strlen (s);
4833 }
4834
4835 it->method = next_element_from_c_string;
4836 }
4837
4838 /* PRECISION > 0 means don't return more than PRECISION characters
4839 from the string. */
4840 if (precision > 0 && it->end_charpos - charpos > precision)
4841 it->end_charpos = it->string_nchars = charpos + precision;
4842
4843 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4844 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4845 FIELD_WIDTH < 0 means infinite field width. This is useful for
4846 padding with `-' at the end of a mode line. */
4847 if (field_width < 0)
4848 field_width = INFINITY;
4849 if (field_width > it->end_charpos - charpos)
4850 it->end_charpos = charpos + field_width;
4851
4852 /* Use the standard display table for displaying strings. */
4853 if (DISP_TABLE_P (Vstandard_display_table))
4854 it->dp = XCHAR_TABLE (Vstandard_display_table);
4855
4856 it->stop_charpos = charpos;
4857 CHECK_IT (it);
4858 }
4859
4860
4861 \f
4862 /***********************************************************************
4863 Iteration
4864 ***********************************************************************/
4865
4866 /* Load IT's display element fields with information about the next
4867 display element from the current position of IT. Value is zero if
4868 end of buffer (or C string) is reached. */
4869
4870 int
4871 get_next_display_element (it)
4872 struct it *it;
4873 {
4874 /* Non-zero means that we found a display element. Zero means that
4875 we hit the end of what we iterate over. Performance note: the
4876 function pointer `method' used here turns out to be faster than
4877 using a sequence of if-statements. */
4878 int success_p;
4879
4880 get_next:
4881 success_p = (*it->method) (it);
4882
4883 if (it->what == IT_CHARACTER)
4884 {
4885 /* Map via display table or translate control characters.
4886 IT->c, IT->len etc. have been set to the next character by
4887 the function call above. If we have a display table, and it
4888 contains an entry for IT->c, translate it. Don't do this if
4889 IT->c itself comes from a display table, otherwise we could
4890 end up in an infinite recursion. (An alternative could be to
4891 count the recursion depth of this function and signal an
4892 error when a certain maximum depth is reached.) Is it worth
4893 it? */
4894 if (success_p && it->dpvec == NULL)
4895 {
4896 Lisp_Object dv;
4897
4898 if (it->dp
4899 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4900 VECTORP (dv)))
4901 {
4902 struct Lisp_Vector *v = XVECTOR (dv);
4903
4904 /* Return the first character from the display table
4905 entry, if not empty. If empty, don't display the
4906 current character. */
4907 if (v->size)
4908 {
4909 it->dpvec_char_len = it->len;
4910 it->dpvec = v->contents;
4911 it->dpend = v->contents + v->size;
4912 it->current.dpvec_index = 0;
4913 it->saved_face_id = it->face_id;
4914 it->method = next_element_from_display_vector;
4915 }
4916 else
4917 {
4918 set_iterator_to_next (it, 0);
4919 }
4920 goto get_next;
4921 }
4922
4923 /* Translate control characters into `\003' or `^C' form.
4924 Control characters coming from a display table entry are
4925 currently not translated because we use IT->dpvec to hold
4926 the translation. This could easily be changed but I
4927 don't believe that it is worth doing.
4928
4929 If it->multibyte_p is nonzero, eight-bit characters and
4930 non-printable multibyte characters are also translated to
4931 octal form.
4932
4933 If it->multibyte_p is zero, eight-bit characters that
4934 don't have corresponding multibyte char code are also
4935 translated to octal form. */
4936 else if ((it->c < ' '
4937 && (it->area != TEXT_AREA
4938 /* In mode line, treat \n like other crl chars. */
4939 || (it->c != '\n'
4940 && it->glyph_row && it->glyph_row->mode_line_p)
4941 || (it->c != '\n' && it->c != '\t')))
4942 || (it->multibyte_p
4943 ? ((it->c >= 127
4944 && it->len == 1)
4945 || !CHAR_PRINTABLE_P (it->c)
4946 || it->c == 0x8ad
4947 || it->c == 0x8a0)
4948 : (it->c >= 127
4949 && (!unibyte_display_via_language_environment
4950 || it->c == unibyte_char_to_multibyte (it->c)))))
4951 {
4952 /* IT->c is a control character which must be displayed
4953 either as '\003' or as `^C' where the '\\' and '^'
4954 can be defined in the display table. Fill
4955 IT->ctl_chars with glyphs for what we have to
4956 display. Then, set IT->dpvec to these glyphs. */
4957 GLYPH g;
4958 int ctl_len;
4959 int face_id = escape_glyph_face;
4960
4961 /* Find the face id if `escape-glyph' unless we recently did. */
4962 if (face_id < 0)
4963 {
4964 Lisp_Object tem = Fget (Qescape_glyph, Qface);
4965 if (INTEGERP (tem))
4966 face_id = XINT (tem);
4967 else
4968 face_id = 0;
4969 /* If there's overflow, use 0 instead. */
4970 if (FAST_GLYPH_FACE (FAST_MAKE_GLYPH (0, face_id)) != face_id)
4971 face_id = 0;
4972 escape_glyph_face = face_id;
4973 }
4974
4975 if (it->c < 128 && it->ctl_arrow_p)
4976 {
4977 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4978 if (it->dp
4979 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4980 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4981 g = XINT (DISP_CTRL_GLYPH (it->dp));
4982 else
4983 g = FAST_MAKE_GLYPH ('^', face_id);
4984 XSETINT (it->ctl_chars[0], g);
4985
4986 g = FAST_MAKE_GLYPH (it->c ^ 0100, face_id);
4987 XSETINT (it->ctl_chars[1], g);
4988 ctl_len = 2;
4989 }
4990 else if (it->c == 0x8a0 || it->c == 0x8ad)
4991 {
4992 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4993 if (it->dp
4994 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4995 && GLYPH_CHAR_VALID_P (XINT (DISP_ESCAPE_GLYPH (it->dp))))
4996 g = XINT (DISP_ESCAPE_GLYPH (it->dp));
4997 else
4998 g = FAST_MAKE_GLYPH ('\\', face_id);
4999 XSETINT (it->ctl_chars[0], g);
5000
5001 g = FAST_MAKE_GLYPH (it->c == 0x8ad ? '-' : ' ', face_id);
5002 XSETINT (it->ctl_chars[1], g);
5003 ctl_len = 2;
5004 }
5005 else
5006 {
5007 unsigned char str[MAX_MULTIBYTE_LENGTH];
5008 int len;
5009 int i;
5010 GLYPH escape_glyph;
5011
5012 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5013 if (it->dp
5014 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5015 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5016 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5017 else
5018 escape_glyph = FAST_MAKE_GLYPH ('\\', face_id);
5019
5020 if (SINGLE_BYTE_CHAR_P (it->c))
5021 str[0] = it->c, len = 1;
5022 else
5023 {
5024 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5025 if (len < 0)
5026 {
5027 /* It's an invalid character, which
5028 shouldn't happen actually, but due to
5029 bugs it may happen. Let's print the char
5030 as is, there's not much meaningful we can
5031 do with it. */
5032 str[0] = it->c;
5033 str[1] = it->c >> 8;
5034 str[2] = it->c >> 16;
5035 str[3] = it->c >> 24;
5036 len = 4;
5037 }
5038 }
5039
5040 for (i = 0; i < len; i++)
5041 {
5042 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5043 /* Insert three more glyphs into IT->ctl_chars for
5044 the octal display of the character. */
5045 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0',
5046 face_id);
5047 XSETINT (it->ctl_chars[i * 4 + 1], g);
5048 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0',
5049 face_id);
5050 XSETINT (it->ctl_chars[i * 4 + 2], g);
5051 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0',
5052 face_id);
5053 XSETINT (it->ctl_chars[i * 4 + 3], g);
5054 }
5055 ctl_len = len * 4;
5056 }
5057
5058 /* Set up IT->dpvec and return first character from it. */
5059 it->dpvec_char_len = it->len;
5060 it->dpvec = it->ctl_chars;
5061 it->dpend = it->dpvec + ctl_len;
5062 it->current.dpvec_index = 0;
5063 it->saved_face_id = it->face_id;
5064 it->method = next_element_from_display_vector;
5065 goto get_next;
5066 }
5067 }
5068
5069 /* Adjust face id for a multibyte character. There are no
5070 multibyte character in unibyte text. */
5071 if (it->multibyte_p
5072 && success_p
5073 && FRAME_WINDOW_P (it->f))
5074 {
5075 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5076 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5077 }
5078 }
5079
5080 /* Is this character the last one of a run of characters with
5081 box? If yes, set IT->end_of_box_run_p to 1. */
5082 if (it->face_box_p
5083 && it->s == NULL)
5084 {
5085 int face_id;
5086 struct face *face;
5087
5088 it->end_of_box_run_p
5089 = ((face_id = face_after_it_pos (it),
5090 face_id != it->face_id)
5091 && (face = FACE_FROM_ID (it->f, face_id),
5092 face->box == FACE_NO_BOX));
5093 }
5094
5095 /* Value is 0 if end of buffer or string reached. */
5096 return success_p;
5097 }
5098
5099
5100 /* Move IT to the next display element.
5101
5102 RESEAT_P non-zero means if called on a newline in buffer text,
5103 skip to the next visible line start.
5104
5105 Functions get_next_display_element and set_iterator_to_next are
5106 separate because I find this arrangement easier to handle than a
5107 get_next_display_element function that also increments IT's
5108 position. The way it is we can first look at an iterator's current
5109 display element, decide whether it fits on a line, and if it does,
5110 increment the iterator position. The other way around we probably
5111 would either need a flag indicating whether the iterator has to be
5112 incremented the next time, or we would have to implement a
5113 decrement position function which would not be easy to write. */
5114
5115 void
5116 set_iterator_to_next (it, reseat_p)
5117 struct it *it;
5118 int reseat_p;
5119 {
5120 /* Reset flags indicating start and end of a sequence of characters
5121 with box. Reset them at the start of this function because
5122 moving the iterator to a new position might set them. */
5123 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5124
5125 if (it->method == next_element_from_buffer)
5126 {
5127 /* The current display element of IT is a character from
5128 current_buffer. Advance in the buffer, and maybe skip over
5129 invisible lines that are so because of selective display. */
5130 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5131 reseat_at_next_visible_line_start (it, 0);
5132 else
5133 {
5134 xassert (it->len != 0);
5135 IT_BYTEPOS (*it) += it->len;
5136 IT_CHARPOS (*it) += 1;
5137 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5138 }
5139 }
5140 else if (it->method == next_element_from_composition)
5141 {
5142 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
5143 if (STRINGP (it->string))
5144 {
5145 IT_STRING_BYTEPOS (*it) += it->len;
5146 IT_STRING_CHARPOS (*it) += it->cmp_len;
5147 it->method = next_element_from_string;
5148 goto consider_string_end;
5149 }
5150 else
5151 {
5152 IT_BYTEPOS (*it) += it->len;
5153 IT_CHARPOS (*it) += it->cmp_len;
5154 it->method = next_element_from_buffer;
5155 }
5156 }
5157 else if (it->method == next_element_from_c_string)
5158 {
5159 /* Current display element of IT is from a C string. */
5160 IT_BYTEPOS (*it) += it->len;
5161 IT_CHARPOS (*it) += 1;
5162 }
5163 else if (it->method == next_element_from_display_vector)
5164 {
5165 /* Current display element of IT is from a display table entry.
5166 Advance in the display table definition. Reset it to null if
5167 end reached, and continue with characters from buffers/
5168 strings. */
5169 ++it->current.dpvec_index;
5170
5171 /* Restore face of the iterator to what they were before the
5172 display vector entry (these entries may contain faces). */
5173 it->face_id = it->saved_face_id;
5174
5175 if (it->dpvec + it->current.dpvec_index == it->dpend)
5176 {
5177 if (it->s)
5178 it->method = next_element_from_c_string;
5179 else if (STRINGP (it->string))
5180 it->method = next_element_from_string;
5181 else
5182 it->method = next_element_from_buffer;
5183
5184 it->dpvec = NULL;
5185 it->current.dpvec_index = -1;
5186
5187 /* Recheck faces after display vector */
5188 it->stop_charpos = 0;
5189
5190 /* Skip over characters which were displayed via IT->dpvec. */
5191 if (it->dpvec_char_len < 0)
5192 reseat_at_next_visible_line_start (it, 1);
5193 else if (it->dpvec_char_len > 0)
5194 {
5195 it->len = it->dpvec_char_len;
5196 set_iterator_to_next (it, reseat_p);
5197 }
5198 }
5199 }
5200 else if (it->method == next_element_from_string)
5201 {
5202 /* Current display element is a character from a Lisp string. */
5203 xassert (it->s == NULL && STRINGP (it->string));
5204 IT_STRING_BYTEPOS (*it) += it->len;
5205 IT_STRING_CHARPOS (*it) += 1;
5206
5207 consider_string_end:
5208
5209 if (it->current.overlay_string_index >= 0)
5210 {
5211 /* IT->string is an overlay string. Advance to the
5212 next, if there is one. */
5213 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5214 next_overlay_string (it);
5215 }
5216 else
5217 {
5218 /* IT->string is not an overlay string. If we reached
5219 its end, and there is something on IT->stack, proceed
5220 with what is on the stack. This can be either another
5221 string, this time an overlay string, or a buffer. */
5222 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5223 && it->sp > 0)
5224 {
5225 pop_it (it);
5226 if (!STRINGP (it->string))
5227 it->method = next_element_from_buffer;
5228 else
5229 goto consider_string_end;
5230 }
5231 }
5232 }
5233 else if (it->method == next_element_from_image
5234 || it->method == next_element_from_stretch)
5235 {
5236 /* The position etc with which we have to proceed are on
5237 the stack. The position may be at the end of a string,
5238 if the `display' property takes up the whole string. */
5239 pop_it (it);
5240 it->image_id = 0;
5241 if (STRINGP (it->string))
5242 {
5243 it->method = next_element_from_string;
5244 goto consider_string_end;
5245 }
5246 else
5247 it->method = next_element_from_buffer;
5248 }
5249 else
5250 /* There are no other methods defined, so this should be a bug. */
5251 abort ();
5252
5253 xassert (it->method != next_element_from_string
5254 || (STRINGP (it->string)
5255 && IT_STRING_CHARPOS (*it) >= 0));
5256 }
5257
5258 /* Load IT's display element fields with information about the next
5259 display element which comes from a display table entry or from the
5260 result of translating a control character to one of the forms `^C'
5261 or `\003'.
5262
5263 IT->dpvec holds the glyphs to return as characters.
5264 IT->saved_face_id holds the face id before the display vector--
5265 it is restored into IT->face_idin set_iterator_to_next. */
5266
5267 static int
5268 next_element_from_display_vector (it)
5269 struct it *it;
5270 {
5271 /* Precondition. */
5272 xassert (it->dpvec && it->current.dpvec_index >= 0);
5273
5274 if (INTEGERP (*it->dpvec)
5275 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5276 {
5277 int lface_id;
5278 GLYPH g;
5279
5280 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5281 it->c = FAST_GLYPH_CHAR (g);
5282 it->len = CHAR_BYTES (it->c);
5283
5284 /* The entry may contain a face id to use. Such a face id is
5285 the id of a Lisp face, not a realized face. A face id of
5286 zero means no face is specified. */
5287 lface_id = FAST_GLYPH_FACE (g);
5288 if (lface_id)
5289 {
5290 /* The function returns -1 if lface_id is invalid. */
5291 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
5292 if (face_id >= 0)
5293 it->face_id = face_id;
5294 }
5295 }
5296 else
5297 /* Display table entry is invalid. Return a space. */
5298 it->c = ' ', it->len = 1;
5299
5300 /* Don't change position and object of the iterator here. They are
5301 still the values of the character that had this display table
5302 entry or was translated, and that's what we want. */
5303 it->what = IT_CHARACTER;
5304 return 1;
5305 }
5306
5307
5308 /* Load IT with the next display element from Lisp string IT->string.
5309 IT->current.string_pos is the current position within the string.
5310 If IT->current.overlay_string_index >= 0, the Lisp string is an
5311 overlay string. */
5312
5313 static int
5314 next_element_from_string (it)
5315 struct it *it;
5316 {
5317 struct text_pos position;
5318
5319 xassert (STRINGP (it->string));
5320 xassert (IT_STRING_CHARPOS (*it) >= 0);
5321 position = it->current.string_pos;
5322
5323 /* Time to check for invisible text? */
5324 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5325 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5326 {
5327 handle_stop (it);
5328
5329 /* Since a handler may have changed IT->method, we must
5330 recurse here. */
5331 return get_next_display_element (it);
5332 }
5333
5334 if (it->current.overlay_string_index >= 0)
5335 {
5336 /* Get the next character from an overlay string. In overlay
5337 strings, There is no field width or padding with spaces to
5338 do. */
5339 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5340 {
5341 it->what = IT_EOB;
5342 return 0;
5343 }
5344 else if (STRING_MULTIBYTE (it->string))
5345 {
5346 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5347 const unsigned char *s = (SDATA (it->string)
5348 + IT_STRING_BYTEPOS (*it));
5349 it->c = string_char_and_length (s, remaining, &it->len);
5350 }
5351 else
5352 {
5353 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5354 it->len = 1;
5355 }
5356 }
5357 else
5358 {
5359 /* Get the next character from a Lisp string that is not an
5360 overlay string. Such strings come from the mode line, for
5361 example. We may have to pad with spaces, or truncate the
5362 string. See also next_element_from_c_string. */
5363 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5364 {
5365 it->what = IT_EOB;
5366 return 0;
5367 }
5368 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5369 {
5370 /* Pad with spaces. */
5371 it->c = ' ', it->len = 1;
5372 CHARPOS (position) = BYTEPOS (position) = -1;
5373 }
5374 else if (STRING_MULTIBYTE (it->string))
5375 {
5376 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5377 const unsigned char *s = (SDATA (it->string)
5378 + IT_STRING_BYTEPOS (*it));
5379 it->c = string_char_and_length (s, maxlen, &it->len);
5380 }
5381 else
5382 {
5383 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5384 it->len = 1;
5385 }
5386 }
5387
5388 /* Record what we have and where it came from. Note that we store a
5389 buffer position in IT->position although it could arguably be a
5390 string position. */
5391 it->what = IT_CHARACTER;
5392 it->object = it->string;
5393 it->position = position;
5394 return 1;
5395 }
5396
5397
5398 /* Load IT with next display element from C string IT->s.
5399 IT->string_nchars is the maximum number of characters to return
5400 from the string. IT->end_charpos may be greater than
5401 IT->string_nchars when this function is called, in which case we
5402 may have to return padding spaces. Value is zero if end of string
5403 reached, including padding spaces. */
5404
5405 static int
5406 next_element_from_c_string (it)
5407 struct it *it;
5408 {
5409 int success_p = 1;
5410
5411 xassert (it->s);
5412 it->what = IT_CHARACTER;
5413 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5414 it->object = Qnil;
5415
5416 /* IT's position can be greater IT->string_nchars in case a field
5417 width or precision has been specified when the iterator was
5418 initialized. */
5419 if (IT_CHARPOS (*it) >= it->end_charpos)
5420 {
5421 /* End of the game. */
5422 it->what = IT_EOB;
5423 success_p = 0;
5424 }
5425 else if (IT_CHARPOS (*it) >= it->string_nchars)
5426 {
5427 /* Pad with spaces. */
5428 it->c = ' ', it->len = 1;
5429 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5430 }
5431 else if (it->multibyte_p)
5432 {
5433 /* Implementation note: The calls to strlen apparently aren't a
5434 performance problem because there is no noticeable performance
5435 difference between Emacs running in unibyte or multibyte mode. */
5436 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5437 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5438 maxlen, &it->len);
5439 }
5440 else
5441 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5442
5443 return success_p;
5444 }
5445
5446
5447 /* Set up IT to return characters from an ellipsis, if appropriate.
5448 The definition of the ellipsis glyphs may come from a display table
5449 entry. This function Fills IT with the first glyph from the
5450 ellipsis if an ellipsis is to be displayed. */
5451
5452 static int
5453 next_element_from_ellipsis (it)
5454 struct it *it;
5455 {
5456 if (it->selective_display_ellipsis_p)
5457 setup_for_ellipsis (it, it->len);
5458 else
5459 {
5460 /* The face at the current position may be different from the
5461 face we find after the invisible text. Remember what it
5462 was in IT->saved_face_id, and signal that it's there by
5463 setting face_before_selective_p. */
5464 it->saved_face_id = it->face_id;
5465 it->method = next_element_from_buffer;
5466 reseat_at_next_visible_line_start (it, 1);
5467 it->face_before_selective_p = 1;
5468 }
5469
5470 return get_next_display_element (it);
5471 }
5472
5473
5474 /* Deliver an image display element. The iterator IT is already
5475 filled with image information (done in handle_display_prop). Value
5476 is always 1. */
5477
5478
5479 static int
5480 next_element_from_image (it)
5481 struct it *it;
5482 {
5483 it->what = IT_IMAGE;
5484 return 1;
5485 }
5486
5487
5488 /* Fill iterator IT with next display element from a stretch glyph
5489 property. IT->object is the value of the text property. Value is
5490 always 1. */
5491
5492 static int
5493 next_element_from_stretch (it)
5494 struct it *it;
5495 {
5496 it->what = IT_STRETCH;
5497 return 1;
5498 }
5499
5500
5501 /* Load IT with the next display element from current_buffer. Value
5502 is zero if end of buffer reached. IT->stop_charpos is the next
5503 position at which to stop and check for text properties or buffer
5504 end. */
5505
5506 static int
5507 next_element_from_buffer (it)
5508 struct it *it;
5509 {
5510 int success_p = 1;
5511
5512 /* Check this assumption, otherwise, we would never enter the
5513 if-statement, below. */
5514 xassert (IT_CHARPOS (*it) >= BEGV
5515 && IT_CHARPOS (*it) <= it->stop_charpos);
5516
5517 if (IT_CHARPOS (*it) >= it->stop_charpos)
5518 {
5519 if (IT_CHARPOS (*it) >= it->end_charpos)
5520 {
5521 int overlay_strings_follow_p;
5522
5523 /* End of the game, except when overlay strings follow that
5524 haven't been returned yet. */
5525 if (it->overlay_strings_at_end_processed_p)
5526 overlay_strings_follow_p = 0;
5527 else
5528 {
5529 it->overlay_strings_at_end_processed_p = 1;
5530 overlay_strings_follow_p = get_overlay_strings (it, 0);
5531 }
5532
5533 if (overlay_strings_follow_p)
5534 success_p = get_next_display_element (it);
5535 else
5536 {
5537 it->what = IT_EOB;
5538 it->position = it->current.pos;
5539 success_p = 0;
5540 }
5541 }
5542 else
5543 {
5544 handle_stop (it);
5545 return get_next_display_element (it);
5546 }
5547 }
5548 else
5549 {
5550 /* No face changes, overlays etc. in sight, so just return a
5551 character from current_buffer. */
5552 unsigned char *p;
5553
5554 /* Maybe run the redisplay end trigger hook. Performance note:
5555 This doesn't seem to cost measurable time. */
5556 if (it->redisplay_end_trigger_charpos
5557 && it->glyph_row
5558 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5559 run_redisplay_end_trigger_hook (it);
5560
5561 /* Get the next character, maybe multibyte. */
5562 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5563 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5564 {
5565 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5566 - IT_BYTEPOS (*it));
5567 it->c = string_char_and_length (p, maxlen, &it->len);
5568 }
5569 else
5570 it->c = *p, it->len = 1;
5571
5572 /* Record what we have and where it came from. */
5573 it->what = IT_CHARACTER;;
5574 it->object = it->w->buffer;
5575 it->position = it->current.pos;
5576
5577 /* Normally we return the character found above, except when we
5578 really want to return an ellipsis for selective display. */
5579 if (it->selective)
5580 {
5581 if (it->c == '\n')
5582 {
5583 /* A value of selective > 0 means hide lines indented more
5584 than that number of columns. */
5585 if (it->selective > 0
5586 && IT_CHARPOS (*it) + 1 < ZV
5587 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5588 IT_BYTEPOS (*it) + 1,
5589 (double) it->selective)) /* iftc */
5590 {
5591 success_p = next_element_from_ellipsis (it);
5592 it->dpvec_char_len = -1;
5593 }
5594 }
5595 else if (it->c == '\r' && it->selective == -1)
5596 {
5597 /* A value of selective == -1 means that everything from the
5598 CR to the end of the line is invisible, with maybe an
5599 ellipsis displayed for it. */
5600 success_p = next_element_from_ellipsis (it);
5601 it->dpvec_char_len = -1;
5602 }
5603 }
5604 }
5605
5606 /* Value is zero if end of buffer reached. */
5607 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5608 return success_p;
5609 }
5610
5611
5612 /* Run the redisplay end trigger hook for IT. */
5613
5614 static void
5615 run_redisplay_end_trigger_hook (it)
5616 struct it *it;
5617 {
5618 Lisp_Object args[3];
5619
5620 /* IT->glyph_row should be non-null, i.e. we should be actually
5621 displaying something, or otherwise we should not run the hook. */
5622 xassert (it->glyph_row);
5623
5624 /* Set up hook arguments. */
5625 args[0] = Qredisplay_end_trigger_functions;
5626 args[1] = it->window;
5627 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5628 it->redisplay_end_trigger_charpos = 0;
5629
5630 /* Since we are *trying* to run these functions, don't try to run
5631 them again, even if they get an error. */
5632 it->w->redisplay_end_trigger = Qnil;
5633 Frun_hook_with_args (3, args);
5634
5635 /* Notice if it changed the face of the character we are on. */
5636 handle_face_prop (it);
5637 }
5638
5639
5640 /* Deliver a composition display element. The iterator IT is already
5641 filled with composition information (done in
5642 handle_composition_prop). Value is always 1. */
5643
5644 static int
5645 next_element_from_composition (it)
5646 struct it *it;
5647 {
5648 it->what = IT_COMPOSITION;
5649 it->position = (STRINGP (it->string)
5650 ? it->current.string_pos
5651 : it->current.pos);
5652 return 1;
5653 }
5654
5655
5656 \f
5657 /***********************************************************************
5658 Moving an iterator without producing glyphs
5659 ***********************************************************************/
5660
5661 /* Move iterator IT to a specified buffer or X position within one
5662 line on the display without producing glyphs.
5663
5664 OP should be a bit mask including some or all of these bits:
5665 MOVE_TO_X: Stop on reaching x-position TO_X.
5666 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5667 Regardless of OP's value, stop in reaching the end of the display line.
5668
5669 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5670 This means, in particular, that TO_X includes window's horizontal
5671 scroll amount.
5672
5673 The return value has several possible values that
5674 say what condition caused the scan to stop:
5675
5676 MOVE_POS_MATCH_OR_ZV
5677 - when TO_POS or ZV was reached.
5678
5679 MOVE_X_REACHED
5680 -when TO_X was reached before TO_POS or ZV were reached.
5681
5682 MOVE_LINE_CONTINUED
5683 - when we reached the end of the display area and the line must
5684 be continued.
5685
5686 MOVE_LINE_TRUNCATED
5687 - when we reached the end of the display area and the line is
5688 truncated.
5689
5690 MOVE_NEWLINE_OR_CR
5691 - when we stopped at a line end, i.e. a newline or a CR and selective
5692 display is on. */
5693
5694 static enum move_it_result
5695 move_it_in_display_line_to (it, to_charpos, to_x, op)
5696 struct it *it;
5697 int to_charpos, to_x, op;
5698 {
5699 enum move_it_result result = MOVE_UNDEFINED;
5700 struct glyph_row *saved_glyph_row;
5701
5702 /* Don't produce glyphs in produce_glyphs. */
5703 saved_glyph_row = it->glyph_row;
5704 it->glyph_row = NULL;
5705
5706 #define BUFFER_POS_REACHED_P() \
5707 ((op & MOVE_TO_POS) != 0 \
5708 && BUFFERP (it->object) \
5709 && IT_CHARPOS (*it) >= to_charpos)
5710
5711 while (1)
5712 {
5713 int x, i, ascent = 0, descent = 0;
5714
5715 /* Stop when ZV reached.
5716 We used to stop here when TO_CHARPOS reached as well, but that is
5717 too soon if this glyph does not fit on this line. So we handle it
5718 explicitly below. */
5719 if (!get_next_display_element (it)
5720 || (it->truncate_lines_p
5721 && BUFFER_POS_REACHED_P ()))
5722 {
5723 result = MOVE_POS_MATCH_OR_ZV;
5724 break;
5725 }
5726
5727 /* The call to produce_glyphs will get the metrics of the
5728 display element IT is loaded with. We record in x the
5729 x-position before this display element in case it does not
5730 fit on the line. */
5731 x = it->current_x;
5732
5733 /* Remember the line height so far in case the next element doesn't
5734 fit on the line. */
5735 if (!it->truncate_lines_p)
5736 {
5737 ascent = it->max_ascent;
5738 descent = it->max_descent;
5739 }
5740
5741 PRODUCE_GLYPHS (it);
5742
5743 if (it->area != TEXT_AREA)
5744 {
5745 set_iterator_to_next (it, 1);
5746 continue;
5747 }
5748
5749 /* The number of glyphs we get back in IT->nglyphs will normally
5750 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5751 character on a terminal frame, or (iii) a line end. For the
5752 second case, IT->nglyphs - 1 padding glyphs will be present
5753 (on X frames, there is only one glyph produced for a
5754 composite character.
5755
5756 The behavior implemented below means, for continuation lines,
5757 that as many spaces of a TAB as fit on the current line are
5758 displayed there. For terminal frames, as many glyphs of a
5759 multi-glyph character are displayed in the current line, too.
5760 This is what the old redisplay code did, and we keep it that
5761 way. Under X, the whole shape of a complex character must
5762 fit on the line or it will be completely displayed in the
5763 next line.
5764
5765 Note that both for tabs and padding glyphs, all glyphs have
5766 the same width. */
5767 if (it->nglyphs)
5768 {
5769 /* More than one glyph or glyph doesn't fit on line. All
5770 glyphs have the same width. */
5771 int single_glyph_width = it->pixel_width / it->nglyphs;
5772 int new_x;
5773
5774 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5775 {
5776 new_x = x + single_glyph_width;
5777
5778 /* We want to leave anything reaching TO_X to the caller. */
5779 if ((op & MOVE_TO_X) && new_x > to_x)
5780 {
5781 if (BUFFER_POS_REACHED_P ())
5782 goto buffer_pos_reached;
5783 it->current_x = x;
5784 result = MOVE_X_REACHED;
5785 break;
5786 }
5787 else if (/* Lines are continued. */
5788 !it->truncate_lines_p
5789 && (/* And glyph doesn't fit on the line. */
5790 new_x > it->last_visible_x
5791 /* Or it fits exactly and we're on a window
5792 system frame. */
5793 || (new_x == it->last_visible_x
5794 && FRAME_WINDOW_P (it->f))))
5795 {
5796 if (/* IT->hpos == 0 means the very first glyph
5797 doesn't fit on the line, e.g. a wide image. */
5798 it->hpos == 0
5799 || (new_x == it->last_visible_x
5800 && FRAME_WINDOW_P (it->f)))
5801 {
5802 ++it->hpos;
5803 it->current_x = new_x;
5804 if (i == it->nglyphs - 1)
5805 {
5806 set_iterator_to_next (it, 1);
5807 #ifdef HAVE_WINDOW_SYSTEM
5808 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5809 {
5810 if (!get_next_display_element (it))
5811 {
5812 result = MOVE_POS_MATCH_OR_ZV;
5813 break;
5814 }
5815 if (BUFFER_POS_REACHED_P ())
5816 {
5817 if (ITERATOR_AT_END_OF_LINE_P (it))
5818 result = MOVE_POS_MATCH_OR_ZV;
5819 else
5820 result = MOVE_LINE_CONTINUED;
5821 break;
5822 }
5823 if (ITERATOR_AT_END_OF_LINE_P (it))
5824 {
5825 result = MOVE_NEWLINE_OR_CR;
5826 break;
5827 }
5828 }
5829 #endif /* HAVE_WINDOW_SYSTEM */
5830 }
5831 }
5832 else
5833 {
5834 it->current_x = x;
5835 it->max_ascent = ascent;
5836 it->max_descent = descent;
5837 }
5838
5839 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5840 IT_CHARPOS (*it)));
5841 result = MOVE_LINE_CONTINUED;
5842 break;
5843 }
5844 else if (BUFFER_POS_REACHED_P ())
5845 goto buffer_pos_reached;
5846 else if (new_x > it->first_visible_x)
5847 {
5848 /* Glyph is visible. Increment number of glyphs that
5849 would be displayed. */
5850 ++it->hpos;
5851 }
5852 else
5853 {
5854 /* Glyph is completely off the left margin of the display
5855 area. Nothing to do. */
5856 }
5857 }
5858
5859 if (result != MOVE_UNDEFINED)
5860 break;
5861 }
5862 else if (BUFFER_POS_REACHED_P ())
5863 {
5864 buffer_pos_reached:
5865 it->current_x = x;
5866 it->max_ascent = ascent;
5867 it->max_descent = descent;
5868 result = MOVE_POS_MATCH_OR_ZV;
5869 break;
5870 }
5871 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5872 {
5873 /* Stop when TO_X specified and reached. This check is
5874 necessary here because of lines consisting of a line end,
5875 only. The line end will not produce any glyphs and we
5876 would never get MOVE_X_REACHED. */
5877 xassert (it->nglyphs == 0);
5878 result = MOVE_X_REACHED;
5879 break;
5880 }
5881
5882 /* Is this a line end? If yes, we're done. */
5883 if (ITERATOR_AT_END_OF_LINE_P (it))
5884 {
5885 result = MOVE_NEWLINE_OR_CR;
5886 break;
5887 }
5888
5889 /* The current display element has been consumed. Advance
5890 to the next. */
5891 set_iterator_to_next (it, 1);
5892
5893 /* Stop if lines are truncated and IT's current x-position is
5894 past the right edge of the window now. */
5895 if (it->truncate_lines_p
5896 && it->current_x >= it->last_visible_x)
5897 {
5898 #ifdef HAVE_WINDOW_SYSTEM
5899 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5900 {
5901 if (!get_next_display_element (it)
5902 || BUFFER_POS_REACHED_P ())
5903 {
5904 result = MOVE_POS_MATCH_OR_ZV;
5905 break;
5906 }
5907 if (ITERATOR_AT_END_OF_LINE_P (it))
5908 {
5909 result = MOVE_NEWLINE_OR_CR;
5910 break;
5911 }
5912 }
5913 #endif /* HAVE_WINDOW_SYSTEM */
5914 result = MOVE_LINE_TRUNCATED;
5915 break;
5916 }
5917 }
5918
5919 #undef BUFFER_POS_REACHED_P
5920
5921 /* Restore the iterator settings altered at the beginning of this
5922 function. */
5923 it->glyph_row = saved_glyph_row;
5924 return result;
5925 }
5926
5927
5928 /* Move IT forward until it satisfies one or more of the criteria in
5929 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5930
5931 OP is a bit-mask that specifies where to stop, and in particular,
5932 which of those four position arguments makes a difference. See the
5933 description of enum move_operation_enum.
5934
5935 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5936 screen line, this function will set IT to the next position >
5937 TO_CHARPOS. */
5938
5939 void
5940 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5941 struct it *it;
5942 int to_charpos, to_x, to_y, to_vpos;
5943 int op;
5944 {
5945 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5946 int line_height;
5947 int reached = 0;
5948
5949 for (;;)
5950 {
5951 if (op & MOVE_TO_VPOS)
5952 {
5953 /* If no TO_CHARPOS and no TO_X specified, stop at the
5954 start of the line TO_VPOS. */
5955 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5956 {
5957 if (it->vpos == to_vpos)
5958 {
5959 reached = 1;
5960 break;
5961 }
5962 else
5963 skip = move_it_in_display_line_to (it, -1, -1, 0);
5964 }
5965 else
5966 {
5967 /* TO_VPOS >= 0 means stop at TO_X in the line at
5968 TO_VPOS, or at TO_POS, whichever comes first. */
5969 if (it->vpos == to_vpos)
5970 {
5971 reached = 2;
5972 break;
5973 }
5974
5975 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5976
5977 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5978 {
5979 reached = 3;
5980 break;
5981 }
5982 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5983 {
5984 /* We have reached TO_X but not in the line we want. */
5985 skip = move_it_in_display_line_to (it, to_charpos,
5986 -1, MOVE_TO_POS);
5987 if (skip == MOVE_POS_MATCH_OR_ZV)
5988 {
5989 reached = 4;
5990 break;
5991 }
5992 }
5993 }
5994 }
5995 else if (op & MOVE_TO_Y)
5996 {
5997 struct it it_backup;
5998
5999 /* TO_Y specified means stop at TO_X in the line containing
6000 TO_Y---or at TO_CHARPOS if this is reached first. The
6001 problem is that we can't really tell whether the line
6002 contains TO_Y before we have completely scanned it, and
6003 this may skip past TO_X. What we do is to first scan to
6004 TO_X.
6005
6006 If TO_X is not specified, use a TO_X of zero. The reason
6007 is to make the outcome of this function more predictable.
6008 If we didn't use TO_X == 0, we would stop at the end of
6009 the line which is probably not what a caller would expect
6010 to happen. */
6011 skip = move_it_in_display_line_to (it, to_charpos,
6012 ((op & MOVE_TO_X)
6013 ? to_x : 0),
6014 (MOVE_TO_X
6015 | (op & MOVE_TO_POS)));
6016
6017 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6018 if (skip == MOVE_POS_MATCH_OR_ZV)
6019 {
6020 reached = 5;
6021 break;
6022 }
6023
6024 /* If TO_X was reached, we would like to know whether TO_Y
6025 is in the line. This can only be said if we know the
6026 total line height which requires us to scan the rest of
6027 the line. */
6028 if (skip == MOVE_X_REACHED)
6029 {
6030 it_backup = *it;
6031 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6032 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6033 op & MOVE_TO_POS);
6034 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6035 }
6036
6037 /* Now, decide whether TO_Y is in this line. */
6038 line_height = it->max_ascent + it->max_descent;
6039 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6040
6041 if (to_y >= it->current_y
6042 && to_y < it->current_y + line_height)
6043 {
6044 if (skip == MOVE_X_REACHED)
6045 /* If TO_Y is in this line and TO_X was reached above,
6046 we scanned too far. We have to restore IT's settings
6047 to the ones before skipping. */
6048 *it = it_backup;
6049 reached = 6;
6050 }
6051 else if (skip == MOVE_X_REACHED)
6052 {
6053 skip = skip2;
6054 if (skip == MOVE_POS_MATCH_OR_ZV)
6055 reached = 7;
6056 }
6057
6058 if (reached)
6059 break;
6060 }
6061 else
6062 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6063
6064 switch (skip)
6065 {
6066 case MOVE_POS_MATCH_OR_ZV:
6067 reached = 8;
6068 goto out;
6069
6070 case MOVE_NEWLINE_OR_CR:
6071 set_iterator_to_next (it, 1);
6072 it->continuation_lines_width = 0;
6073 break;
6074
6075 case MOVE_LINE_TRUNCATED:
6076 it->continuation_lines_width = 0;
6077 reseat_at_next_visible_line_start (it, 0);
6078 if ((op & MOVE_TO_POS) != 0
6079 && IT_CHARPOS (*it) > to_charpos)
6080 {
6081 reached = 9;
6082 goto out;
6083 }
6084 break;
6085
6086 case MOVE_LINE_CONTINUED:
6087 it->continuation_lines_width += it->current_x;
6088 break;
6089
6090 default:
6091 abort ();
6092 }
6093
6094 /* Reset/increment for the next run. */
6095 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6096 it->current_x = it->hpos = 0;
6097 it->current_y += it->max_ascent + it->max_descent;
6098 ++it->vpos;
6099 last_height = it->max_ascent + it->max_descent;
6100 last_max_ascent = it->max_ascent;
6101 it->max_ascent = it->max_descent = 0;
6102 }
6103
6104 out:
6105
6106 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6107 }
6108
6109
6110 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6111
6112 If DY > 0, move IT backward at least that many pixels. DY = 0
6113 means move IT backward to the preceding line start or BEGV. This
6114 function may move over more than DY pixels if IT->current_y - DY
6115 ends up in the middle of a line; in this case IT->current_y will be
6116 set to the top of the line moved to. */
6117
6118 void
6119 move_it_vertically_backward (it, dy)
6120 struct it *it;
6121 int dy;
6122 {
6123 int nlines, h;
6124 struct it it2, it3;
6125 int start_pos;
6126
6127 move_further_back:
6128 xassert (dy >= 0);
6129
6130 start_pos = IT_CHARPOS (*it);
6131
6132 /* Estimate how many newlines we must move back. */
6133 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6134
6135 /* Set the iterator's position that many lines back. */
6136 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6137 back_to_previous_visible_line_start (it);
6138
6139 /* Reseat the iterator here. When moving backward, we don't want
6140 reseat to skip forward over invisible text, set up the iterator
6141 to deliver from overlay strings at the new position etc. So,
6142 use reseat_1 here. */
6143 reseat_1 (it, it->current.pos, 1);
6144
6145 /* We are now surely at a line start. */
6146 it->current_x = it->hpos = 0;
6147 it->continuation_lines_width = 0;
6148
6149 /* Move forward and see what y-distance we moved. First move to the
6150 start of the next line so that we get its height. We need this
6151 height to be able to tell whether we reached the specified
6152 y-distance. */
6153 it2 = *it;
6154 it2.max_ascent = it2.max_descent = 0;
6155 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6156 MOVE_TO_POS | MOVE_TO_VPOS);
6157 xassert (IT_CHARPOS (*it) >= BEGV);
6158 it3 = it2;
6159
6160 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6161 xassert (IT_CHARPOS (*it) >= BEGV);
6162 /* H is the actual vertical distance from the position in *IT
6163 and the starting position. */
6164 h = it2.current_y - it->current_y;
6165 /* NLINES is the distance in number of lines. */
6166 nlines = it2.vpos - it->vpos;
6167
6168 /* Correct IT's y and vpos position
6169 so that they are relative to the starting point. */
6170 it->vpos -= nlines;
6171 it->current_y -= h;
6172
6173 if (dy == 0)
6174 {
6175 /* DY == 0 means move to the start of the screen line. The
6176 value of nlines is > 0 if continuation lines were involved. */
6177 if (nlines > 0)
6178 move_it_by_lines (it, nlines, 1);
6179 xassert (IT_CHARPOS (*it) <= start_pos);
6180 }
6181 else
6182 {
6183 /* The y-position we try to reach, relative to *IT.
6184 Note that H has been subtracted in front of the if-statement. */
6185 int target_y = it->current_y + h - dy;
6186 int y0 = it3.current_y;
6187 int y1 = line_bottom_y (&it3);
6188 int line_height = y1 - y0;
6189
6190 /* If we did not reach target_y, try to move further backward if
6191 we can. If we moved too far backward, try to move forward. */
6192 if (target_y < it->current_y
6193 /* This is heuristic. In a window that's 3 lines high, with
6194 a line height of 13 pixels each, recentering with point
6195 on the bottom line will try to move -39/2 = 19 pixels
6196 backward. Try to avoid moving into the first line. */
6197 && it->current_y - target_y > line_height * 2 / 3
6198 && IT_CHARPOS (*it) > BEGV)
6199 {
6200 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6201 target_y - it->current_y));
6202 dy = it->current_y - target_y;
6203 goto move_further_back;
6204 }
6205 else if (target_y >= it->current_y + line_height
6206 && IT_CHARPOS (*it) < ZV)
6207 {
6208 /* Should move forward by at least one line, maybe more.
6209
6210 Note: Calling move_it_by_lines can be expensive on
6211 terminal frames, where compute_motion is used (via
6212 vmotion) to do the job, when there are very long lines
6213 and truncate-lines is nil. That's the reason for
6214 treating terminal frames specially here. */
6215
6216 if (!FRAME_WINDOW_P (it->f))
6217 move_it_vertically (it, target_y - (it->current_y + line_height));
6218 else
6219 {
6220 do
6221 {
6222 move_it_by_lines (it, 1, 1);
6223 }
6224 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6225 }
6226
6227 xassert (IT_CHARPOS (*it) >= BEGV);
6228 }
6229 }
6230 }
6231
6232
6233 /* Move IT by a specified amount of pixel lines DY. DY negative means
6234 move backwards. DY = 0 means move to start of screen line. At the
6235 end, IT will be on the start of a screen line. */
6236
6237 void
6238 move_it_vertically (it, dy)
6239 struct it *it;
6240 int dy;
6241 {
6242 if (dy <= 0)
6243 move_it_vertically_backward (it, -dy);
6244 else
6245 {
6246 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6247 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6248 MOVE_TO_POS | MOVE_TO_Y);
6249 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6250
6251 /* If buffer ends in ZV without a newline, move to the start of
6252 the line to satisfy the post-condition. */
6253 if (IT_CHARPOS (*it) == ZV
6254 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6255 move_it_by_lines (it, 0, 0);
6256 }
6257 }
6258
6259
6260 /* Move iterator IT past the end of the text line it is in. */
6261
6262 void
6263 move_it_past_eol (it)
6264 struct it *it;
6265 {
6266 enum move_it_result rc;
6267
6268 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6269 if (rc == MOVE_NEWLINE_OR_CR)
6270 set_iterator_to_next (it, 0);
6271 }
6272
6273
6274 #if 0 /* Currently not used. */
6275
6276 /* Return non-zero if some text between buffer positions START_CHARPOS
6277 and END_CHARPOS is invisible. IT->window is the window for text
6278 property lookup. */
6279
6280 static int
6281 invisible_text_between_p (it, start_charpos, end_charpos)
6282 struct it *it;
6283 int start_charpos, end_charpos;
6284 {
6285 Lisp_Object prop, limit;
6286 int invisible_found_p;
6287
6288 xassert (it != NULL && start_charpos <= end_charpos);
6289
6290 /* Is text at START invisible? */
6291 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6292 it->window);
6293 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6294 invisible_found_p = 1;
6295 else
6296 {
6297 limit = Fnext_single_char_property_change (make_number (start_charpos),
6298 Qinvisible, Qnil,
6299 make_number (end_charpos));
6300 invisible_found_p = XFASTINT (limit) < end_charpos;
6301 }
6302
6303 return invisible_found_p;
6304 }
6305
6306 #endif /* 0 */
6307
6308
6309 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6310 negative means move up. DVPOS == 0 means move to the start of the
6311 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6312 NEED_Y_P is zero, IT->current_y will be left unchanged.
6313
6314 Further optimization ideas: If we would know that IT->f doesn't use
6315 a face with proportional font, we could be faster for
6316 truncate-lines nil. */
6317
6318 void
6319 move_it_by_lines (it, dvpos, need_y_p)
6320 struct it *it;
6321 int dvpos, need_y_p;
6322 {
6323 struct position pos;
6324
6325 if (!FRAME_WINDOW_P (it->f))
6326 {
6327 struct text_pos textpos;
6328
6329 /* We can use vmotion on frames without proportional fonts. */
6330 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6331 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6332 reseat (it, textpos, 1);
6333 it->vpos += pos.vpos;
6334 it->current_y += pos.vpos;
6335 }
6336 else if (dvpos == 0)
6337 {
6338 /* DVPOS == 0 means move to the start of the screen line. */
6339 move_it_vertically_backward (it, 0);
6340 xassert (it->current_x == 0 && it->hpos == 0);
6341 /* Let next call to line_bottom_y calculate real line height */
6342 last_height = 0;
6343 }
6344 else if (dvpos > 0)
6345 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6346 else
6347 {
6348 struct it it2;
6349 int start_charpos, i;
6350
6351 /* Start at the beginning of the screen line containing IT's
6352 position. */
6353 move_it_vertically_backward (it, 0);
6354
6355 /* Go back -DVPOS visible lines and reseat the iterator there. */
6356 start_charpos = IT_CHARPOS (*it);
6357 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6358 back_to_previous_visible_line_start (it);
6359 reseat (it, it->current.pos, 1);
6360 it->current_x = it->hpos = 0;
6361
6362 /* Above call may have moved too far if continuation lines
6363 are involved. Scan forward and see if it did. */
6364 it2 = *it;
6365 it2.vpos = it2.current_y = 0;
6366 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6367 it->vpos -= it2.vpos;
6368 it->current_y -= it2.current_y;
6369 it->current_x = it->hpos = 0;
6370
6371 /* If we moved too far, move IT some lines forward. */
6372 if (it2.vpos > -dvpos)
6373 {
6374 int delta = it2.vpos + dvpos;
6375 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6376 }
6377 }
6378 }
6379
6380 /* Return 1 if IT points into the middle of a display vector. */
6381
6382 int
6383 in_display_vector_p (it)
6384 struct it *it;
6385 {
6386 return (it->method == next_element_from_display_vector
6387 && it->current.dpvec_index > 0
6388 && it->dpvec + it->current.dpvec_index != it->dpend);
6389 }
6390
6391 \f
6392 /***********************************************************************
6393 Messages
6394 ***********************************************************************/
6395
6396
6397 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6398 to *Messages*. */
6399
6400 void
6401 add_to_log (format, arg1, arg2)
6402 char *format;
6403 Lisp_Object arg1, arg2;
6404 {
6405 Lisp_Object args[3];
6406 Lisp_Object msg, fmt;
6407 char *buffer;
6408 int len;
6409 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6410 USE_SAFE_ALLOCA;
6411
6412 /* Do nothing if called asynchronously. Inserting text into
6413 a buffer may call after-change-functions and alike and
6414 that would means running Lisp asynchronously. */
6415 if (handling_signal)
6416 return;
6417
6418 fmt = msg = Qnil;
6419 GCPRO4 (fmt, msg, arg1, arg2);
6420
6421 args[0] = fmt = build_string (format);
6422 args[1] = arg1;
6423 args[2] = arg2;
6424 msg = Fformat (3, args);
6425
6426 len = SBYTES (msg) + 1;
6427 SAFE_ALLOCA (buffer, char *, len);
6428 bcopy (SDATA (msg), buffer, len);
6429
6430 message_dolog (buffer, len - 1, 1, 0);
6431 SAFE_FREE ();
6432
6433 UNGCPRO;
6434 }
6435
6436
6437 /* Output a newline in the *Messages* buffer if "needs" one. */
6438
6439 void
6440 message_log_maybe_newline ()
6441 {
6442 if (message_log_need_newline)
6443 message_dolog ("", 0, 1, 0);
6444 }
6445
6446
6447 /* Add a string M of length NBYTES to the message log, optionally
6448 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6449 nonzero, means interpret the contents of M as multibyte. This
6450 function calls low-level routines in order to bypass text property
6451 hooks, etc. which might not be safe to run. */
6452
6453 void
6454 message_dolog (m, nbytes, nlflag, multibyte)
6455 const char *m;
6456 int nbytes, nlflag, multibyte;
6457 {
6458 if (!NILP (Vmemory_full))
6459 return;
6460
6461 if (!NILP (Vmessage_log_max))
6462 {
6463 struct buffer *oldbuf;
6464 Lisp_Object oldpoint, oldbegv, oldzv;
6465 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6466 int point_at_end = 0;
6467 int zv_at_end = 0;
6468 Lisp_Object old_deactivate_mark, tem;
6469 struct gcpro gcpro1;
6470
6471 old_deactivate_mark = Vdeactivate_mark;
6472 oldbuf = current_buffer;
6473 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6474 current_buffer->undo_list = Qt;
6475
6476 oldpoint = message_dolog_marker1;
6477 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6478 oldbegv = message_dolog_marker2;
6479 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6480 oldzv = message_dolog_marker3;
6481 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6482 GCPRO1 (old_deactivate_mark);
6483
6484 if (PT == Z)
6485 point_at_end = 1;
6486 if (ZV == Z)
6487 zv_at_end = 1;
6488
6489 BEGV = BEG;
6490 BEGV_BYTE = BEG_BYTE;
6491 ZV = Z;
6492 ZV_BYTE = Z_BYTE;
6493 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6494
6495 /* Insert the string--maybe converting multibyte to single byte
6496 or vice versa, so that all the text fits the buffer. */
6497 if (multibyte
6498 && NILP (current_buffer->enable_multibyte_characters))
6499 {
6500 int i, c, char_bytes;
6501 unsigned char work[1];
6502
6503 /* Convert a multibyte string to single-byte
6504 for the *Message* buffer. */
6505 for (i = 0; i < nbytes; i += char_bytes)
6506 {
6507 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6508 work[0] = (SINGLE_BYTE_CHAR_P (c)
6509 ? c
6510 : multibyte_char_to_unibyte (c, Qnil));
6511 insert_1_both (work, 1, 1, 1, 0, 0);
6512 }
6513 }
6514 else if (! multibyte
6515 && ! NILP (current_buffer->enable_multibyte_characters))
6516 {
6517 int i, c, char_bytes;
6518 unsigned char *msg = (unsigned char *) m;
6519 unsigned char str[MAX_MULTIBYTE_LENGTH];
6520 /* Convert a single-byte string to multibyte
6521 for the *Message* buffer. */
6522 for (i = 0; i < nbytes; i++)
6523 {
6524 c = unibyte_char_to_multibyte (msg[i]);
6525 char_bytes = CHAR_STRING (c, str);
6526 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6527 }
6528 }
6529 else if (nbytes)
6530 insert_1 (m, nbytes, 1, 0, 0);
6531
6532 if (nlflag)
6533 {
6534 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6535 insert_1 ("\n", 1, 1, 0, 0);
6536
6537 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6538 this_bol = PT;
6539 this_bol_byte = PT_BYTE;
6540
6541 /* See if this line duplicates the previous one.
6542 If so, combine duplicates. */
6543 if (this_bol > BEG)
6544 {
6545 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6546 prev_bol = PT;
6547 prev_bol_byte = PT_BYTE;
6548
6549 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6550 this_bol, this_bol_byte);
6551 if (dup)
6552 {
6553 del_range_both (prev_bol, prev_bol_byte,
6554 this_bol, this_bol_byte, 0);
6555 if (dup > 1)
6556 {
6557 char dupstr[40];
6558 int duplen;
6559
6560 /* If you change this format, don't forget to also
6561 change message_log_check_duplicate. */
6562 sprintf (dupstr, " [%d times]", dup);
6563 duplen = strlen (dupstr);
6564 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6565 insert_1 (dupstr, duplen, 1, 0, 1);
6566 }
6567 }
6568 }
6569
6570 /* If we have more than the desired maximum number of lines
6571 in the *Messages* buffer now, delete the oldest ones.
6572 This is safe because we don't have undo in this buffer. */
6573
6574 if (NATNUMP (Vmessage_log_max))
6575 {
6576 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6577 -XFASTINT (Vmessage_log_max) - 1, 0);
6578 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6579 }
6580 }
6581 BEGV = XMARKER (oldbegv)->charpos;
6582 BEGV_BYTE = marker_byte_position (oldbegv);
6583
6584 if (zv_at_end)
6585 {
6586 ZV = Z;
6587 ZV_BYTE = Z_BYTE;
6588 }
6589 else
6590 {
6591 ZV = XMARKER (oldzv)->charpos;
6592 ZV_BYTE = marker_byte_position (oldzv);
6593 }
6594
6595 if (point_at_end)
6596 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6597 else
6598 /* We can't do Fgoto_char (oldpoint) because it will run some
6599 Lisp code. */
6600 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6601 XMARKER (oldpoint)->bytepos);
6602
6603 UNGCPRO;
6604 unchain_marker (XMARKER (oldpoint));
6605 unchain_marker (XMARKER (oldbegv));
6606 unchain_marker (XMARKER (oldzv));
6607
6608 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6609 set_buffer_internal (oldbuf);
6610 if (NILP (tem))
6611 windows_or_buffers_changed = old_windows_or_buffers_changed;
6612 message_log_need_newline = !nlflag;
6613 Vdeactivate_mark = old_deactivate_mark;
6614 }
6615 }
6616
6617
6618 /* We are at the end of the buffer after just having inserted a newline.
6619 (Note: We depend on the fact we won't be crossing the gap.)
6620 Check to see if the most recent message looks a lot like the previous one.
6621 Return 0 if different, 1 if the new one should just replace it, or a
6622 value N > 1 if we should also append " [N times]". */
6623
6624 static int
6625 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6626 int prev_bol, this_bol;
6627 int prev_bol_byte, this_bol_byte;
6628 {
6629 int i;
6630 int len = Z_BYTE - 1 - this_bol_byte;
6631 int seen_dots = 0;
6632 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6633 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6634
6635 for (i = 0; i < len; i++)
6636 {
6637 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6638 seen_dots = 1;
6639 if (p1[i] != p2[i])
6640 return seen_dots;
6641 }
6642 p1 += len;
6643 if (*p1 == '\n')
6644 return 2;
6645 if (*p1++ == ' ' && *p1++ == '[')
6646 {
6647 int n = 0;
6648 while (*p1 >= '0' && *p1 <= '9')
6649 n = n * 10 + *p1++ - '0';
6650 if (strncmp (p1, " times]\n", 8) == 0)
6651 return n+1;
6652 }
6653 return 0;
6654 }
6655 \f
6656
6657 /* Display an echo area message M with a specified length of NBYTES
6658 bytes. The string may include null characters. If M is 0, clear
6659 out any existing message, and let the mini-buffer text show
6660 through.
6661
6662 The buffer M must continue to exist until after the echo area gets
6663 cleared or some other message gets displayed there. This means do
6664 not pass text that is stored in a Lisp string; do not pass text in
6665 a buffer that was alloca'd. */
6666
6667 void
6668 message2 (m, nbytes, multibyte)
6669 const char *m;
6670 int nbytes;
6671 int multibyte;
6672 {
6673 /* First flush out any partial line written with print. */
6674 message_log_maybe_newline ();
6675 if (m)
6676 message_dolog (m, nbytes, 1, multibyte);
6677 message2_nolog (m, nbytes, multibyte);
6678 }
6679
6680
6681 /* The non-logging counterpart of message2. */
6682
6683 void
6684 message2_nolog (m, nbytes, multibyte)
6685 const char *m;
6686 int nbytes, multibyte;
6687 {
6688 struct frame *sf = SELECTED_FRAME ();
6689 message_enable_multibyte = multibyte;
6690
6691 if (noninteractive)
6692 {
6693 if (noninteractive_need_newline)
6694 putc ('\n', stderr);
6695 noninteractive_need_newline = 0;
6696 if (m)
6697 fwrite (m, nbytes, 1, stderr);
6698 if (cursor_in_echo_area == 0)
6699 fprintf (stderr, "\n");
6700 fflush (stderr);
6701 }
6702 /* A null message buffer means that the frame hasn't really been
6703 initialized yet. Error messages get reported properly by
6704 cmd_error, so this must be just an informative message; toss it. */
6705 else if (INTERACTIVE
6706 && sf->glyphs_initialized_p
6707 && FRAME_MESSAGE_BUF (sf))
6708 {
6709 Lisp_Object mini_window;
6710 struct frame *f;
6711
6712 /* Get the frame containing the mini-buffer
6713 that the selected frame is using. */
6714 mini_window = FRAME_MINIBUF_WINDOW (sf);
6715 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6716
6717 FRAME_SAMPLE_VISIBILITY (f);
6718 if (FRAME_VISIBLE_P (sf)
6719 && ! FRAME_VISIBLE_P (f))
6720 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6721
6722 if (m)
6723 {
6724 set_message (m, Qnil, nbytes, multibyte);
6725 if (minibuffer_auto_raise)
6726 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6727 }
6728 else
6729 clear_message (1, 1);
6730
6731 do_pending_window_change (0);
6732 echo_area_display (1);
6733 do_pending_window_change (0);
6734 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6735 (*frame_up_to_date_hook) (f);
6736 }
6737 }
6738
6739
6740 /* Display an echo area message M with a specified length of NBYTES
6741 bytes. The string may include null characters. If M is not a
6742 string, clear out any existing message, and let the mini-buffer
6743 text show through. */
6744
6745 void
6746 message3 (m, nbytes, multibyte)
6747 Lisp_Object m;
6748 int nbytes;
6749 int multibyte;
6750 {
6751 struct gcpro gcpro1;
6752
6753 GCPRO1 (m);
6754 clear_message (1,1);
6755
6756 /* First flush out any partial line written with print. */
6757 message_log_maybe_newline ();
6758 if (STRINGP (m))
6759 message_dolog (SDATA (m), nbytes, 1, multibyte);
6760 message3_nolog (m, nbytes, multibyte);
6761
6762 UNGCPRO;
6763 }
6764
6765
6766 /* The non-logging version of message3. */
6767
6768 void
6769 message3_nolog (m, nbytes, multibyte)
6770 Lisp_Object m;
6771 int nbytes, multibyte;
6772 {
6773 struct frame *sf = SELECTED_FRAME ();
6774 message_enable_multibyte = multibyte;
6775
6776 if (noninteractive)
6777 {
6778 if (noninteractive_need_newline)
6779 putc ('\n', stderr);
6780 noninteractive_need_newline = 0;
6781 if (STRINGP (m))
6782 fwrite (SDATA (m), nbytes, 1, stderr);
6783 if (cursor_in_echo_area == 0)
6784 fprintf (stderr, "\n");
6785 fflush (stderr);
6786 }
6787 /* A null message buffer means that the frame hasn't really been
6788 initialized yet. Error messages get reported properly by
6789 cmd_error, so this must be just an informative message; toss it. */
6790 else if (INTERACTIVE
6791 && sf->glyphs_initialized_p
6792 && FRAME_MESSAGE_BUF (sf))
6793 {
6794 Lisp_Object mini_window;
6795 Lisp_Object frame;
6796 struct frame *f;
6797
6798 /* Get the frame containing the mini-buffer
6799 that the selected frame is using. */
6800 mini_window = FRAME_MINIBUF_WINDOW (sf);
6801 frame = XWINDOW (mini_window)->frame;
6802 f = XFRAME (frame);
6803
6804 FRAME_SAMPLE_VISIBILITY (f);
6805 if (FRAME_VISIBLE_P (sf)
6806 && !FRAME_VISIBLE_P (f))
6807 Fmake_frame_visible (frame);
6808
6809 if (STRINGP (m) && SCHARS (m) > 0)
6810 {
6811 set_message (NULL, m, nbytes, multibyte);
6812 if (minibuffer_auto_raise)
6813 Fraise_frame (frame);
6814 }
6815 else
6816 clear_message (1, 1);
6817
6818 do_pending_window_change (0);
6819 echo_area_display (1);
6820 do_pending_window_change (0);
6821 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6822 (*frame_up_to_date_hook) (f);
6823 }
6824 }
6825
6826
6827 /* Display a null-terminated echo area message M. If M is 0, clear
6828 out any existing message, and let the mini-buffer text show through.
6829
6830 The buffer M must continue to exist until after the echo area gets
6831 cleared or some other message gets displayed there. Do not pass
6832 text that is stored in a Lisp string. Do not pass text in a buffer
6833 that was alloca'd. */
6834
6835 void
6836 message1 (m)
6837 char *m;
6838 {
6839 message2 (m, (m ? strlen (m) : 0), 0);
6840 }
6841
6842
6843 /* The non-logging counterpart of message1. */
6844
6845 void
6846 message1_nolog (m)
6847 char *m;
6848 {
6849 message2_nolog (m, (m ? strlen (m) : 0), 0);
6850 }
6851
6852 /* Display a message M which contains a single %s
6853 which gets replaced with STRING. */
6854
6855 void
6856 message_with_string (m, string, log)
6857 char *m;
6858 Lisp_Object string;
6859 int log;
6860 {
6861 CHECK_STRING (string);
6862
6863 if (noninteractive)
6864 {
6865 if (m)
6866 {
6867 if (noninteractive_need_newline)
6868 putc ('\n', stderr);
6869 noninteractive_need_newline = 0;
6870 fprintf (stderr, m, SDATA (string));
6871 if (cursor_in_echo_area == 0)
6872 fprintf (stderr, "\n");
6873 fflush (stderr);
6874 }
6875 }
6876 else if (INTERACTIVE)
6877 {
6878 /* The frame whose minibuffer we're going to display the message on.
6879 It may be larger than the selected frame, so we need
6880 to use its buffer, not the selected frame's buffer. */
6881 Lisp_Object mini_window;
6882 struct frame *f, *sf = SELECTED_FRAME ();
6883
6884 /* Get the frame containing the minibuffer
6885 that the selected frame is using. */
6886 mini_window = FRAME_MINIBUF_WINDOW (sf);
6887 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6888
6889 /* A null message buffer means that the frame hasn't really been
6890 initialized yet. Error messages get reported properly by
6891 cmd_error, so this must be just an informative message; toss it. */
6892 if (FRAME_MESSAGE_BUF (f))
6893 {
6894 Lisp_Object args[2], message;
6895 struct gcpro gcpro1, gcpro2;
6896
6897 args[0] = build_string (m);
6898 args[1] = message = string;
6899 GCPRO2 (args[0], message);
6900 gcpro1.nvars = 2;
6901
6902 message = Fformat (2, args);
6903
6904 if (log)
6905 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6906 else
6907 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6908
6909 UNGCPRO;
6910
6911 /* Print should start at the beginning of the message
6912 buffer next time. */
6913 message_buf_print = 0;
6914 }
6915 }
6916 }
6917
6918
6919 /* Dump an informative message to the minibuf. If M is 0, clear out
6920 any existing message, and let the mini-buffer text show through. */
6921
6922 /* VARARGS 1 */
6923 void
6924 message (m, a1, a2, a3)
6925 char *m;
6926 EMACS_INT a1, a2, a3;
6927 {
6928 if (noninteractive)
6929 {
6930 if (m)
6931 {
6932 if (noninteractive_need_newline)
6933 putc ('\n', stderr);
6934 noninteractive_need_newline = 0;
6935 fprintf (stderr, m, a1, a2, a3);
6936 if (cursor_in_echo_area == 0)
6937 fprintf (stderr, "\n");
6938 fflush (stderr);
6939 }
6940 }
6941 else if (INTERACTIVE)
6942 {
6943 /* The frame whose mini-buffer we're going to display the message
6944 on. It may be larger than the selected frame, so we need to
6945 use its buffer, not the selected frame's buffer. */
6946 Lisp_Object mini_window;
6947 struct frame *f, *sf = SELECTED_FRAME ();
6948
6949 /* Get the frame containing the mini-buffer
6950 that the selected frame is using. */
6951 mini_window = FRAME_MINIBUF_WINDOW (sf);
6952 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6953
6954 /* A null message buffer means that the frame hasn't really been
6955 initialized yet. Error messages get reported properly by
6956 cmd_error, so this must be just an informative message; toss
6957 it. */
6958 if (FRAME_MESSAGE_BUF (f))
6959 {
6960 if (m)
6961 {
6962 int len;
6963 #ifdef NO_ARG_ARRAY
6964 char *a[3];
6965 a[0] = (char *) a1;
6966 a[1] = (char *) a2;
6967 a[2] = (char *) a3;
6968
6969 len = doprnt (FRAME_MESSAGE_BUF (f),
6970 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6971 #else
6972 len = doprnt (FRAME_MESSAGE_BUF (f),
6973 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6974 (char **) &a1);
6975 #endif /* NO_ARG_ARRAY */
6976
6977 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6978 }
6979 else
6980 message1 (0);
6981
6982 /* Print should start at the beginning of the message
6983 buffer next time. */
6984 message_buf_print = 0;
6985 }
6986 }
6987 }
6988
6989
6990 /* The non-logging version of message. */
6991
6992 void
6993 message_nolog (m, a1, a2, a3)
6994 char *m;
6995 EMACS_INT a1, a2, a3;
6996 {
6997 Lisp_Object old_log_max;
6998 old_log_max = Vmessage_log_max;
6999 Vmessage_log_max = Qnil;
7000 message (m, a1, a2, a3);
7001 Vmessage_log_max = old_log_max;
7002 }
7003
7004
7005 /* Display the current message in the current mini-buffer. This is
7006 only called from error handlers in process.c, and is not time
7007 critical. */
7008
7009 void
7010 update_echo_area ()
7011 {
7012 if (!NILP (echo_area_buffer[0]))
7013 {
7014 Lisp_Object string;
7015 string = Fcurrent_message ();
7016 message3 (string, SBYTES (string),
7017 !NILP (current_buffer->enable_multibyte_characters));
7018 }
7019 }
7020
7021
7022 /* Make sure echo area buffers in `echo_buffers' are live.
7023 If they aren't, make new ones. */
7024
7025 static void
7026 ensure_echo_area_buffers ()
7027 {
7028 int i;
7029
7030 for (i = 0; i < 2; ++i)
7031 if (!BUFFERP (echo_buffer[i])
7032 || NILP (XBUFFER (echo_buffer[i])->name))
7033 {
7034 char name[30];
7035 Lisp_Object old_buffer;
7036 int j;
7037
7038 old_buffer = echo_buffer[i];
7039 sprintf (name, " *Echo Area %d*", i);
7040 echo_buffer[i] = Fget_buffer_create (build_string (name));
7041 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7042
7043 for (j = 0; j < 2; ++j)
7044 if (EQ (old_buffer, echo_area_buffer[j]))
7045 echo_area_buffer[j] = echo_buffer[i];
7046 }
7047 }
7048
7049
7050 /* Call FN with args A1..A4 with either the current or last displayed
7051 echo_area_buffer as current buffer.
7052
7053 WHICH zero means use the current message buffer
7054 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7055 from echo_buffer[] and clear it.
7056
7057 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7058 suitable buffer from echo_buffer[] and clear it.
7059
7060 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
7061 that the current message becomes the last displayed one, make
7062 choose a suitable buffer for echo_area_buffer[0], and clear it.
7063
7064 Value is what FN returns. */
7065
7066 static int
7067 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7068 struct window *w;
7069 int which;
7070 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7071 EMACS_INT a1;
7072 Lisp_Object a2;
7073 EMACS_INT a3, a4;
7074 {
7075 Lisp_Object buffer;
7076 int this_one, the_other, clear_buffer_p, rc;
7077 int count = SPECPDL_INDEX ();
7078
7079 /* If buffers aren't live, make new ones. */
7080 ensure_echo_area_buffers ();
7081
7082 clear_buffer_p = 0;
7083
7084 if (which == 0)
7085 this_one = 0, the_other = 1;
7086 else if (which > 0)
7087 this_one = 1, the_other = 0;
7088 else
7089 {
7090 this_one = 0, the_other = 1;
7091 clear_buffer_p = 1;
7092
7093 /* We need a fresh one in case the current echo buffer equals
7094 the one containing the last displayed echo area message. */
7095 if (!NILP (echo_area_buffer[this_one])
7096 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
7097 echo_area_buffer[this_one] = Qnil;
7098 }
7099
7100 /* Choose a suitable buffer from echo_buffer[] is we don't
7101 have one. */
7102 if (NILP (echo_area_buffer[this_one]))
7103 {
7104 echo_area_buffer[this_one]
7105 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7106 ? echo_buffer[the_other]
7107 : echo_buffer[this_one]);
7108 clear_buffer_p = 1;
7109 }
7110
7111 buffer = echo_area_buffer[this_one];
7112
7113 /* Don't get confused by reusing the buffer used for echoing
7114 for a different purpose. */
7115 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7116 cancel_echoing ();
7117
7118 record_unwind_protect (unwind_with_echo_area_buffer,
7119 with_echo_area_buffer_unwind_data (w));
7120
7121 /* Make the echo area buffer current. Note that for display
7122 purposes, it is not necessary that the displayed window's buffer
7123 == current_buffer, except for text property lookup. So, let's
7124 only set that buffer temporarily here without doing a full
7125 Fset_window_buffer. We must also change w->pointm, though,
7126 because otherwise an assertions in unshow_buffer fails, and Emacs
7127 aborts. */
7128 set_buffer_internal_1 (XBUFFER (buffer));
7129 if (w)
7130 {
7131 w->buffer = buffer;
7132 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7133 }
7134
7135 current_buffer->undo_list = Qt;
7136 current_buffer->read_only = Qnil;
7137 specbind (Qinhibit_read_only, Qt);
7138 specbind (Qinhibit_modification_hooks, Qt);
7139
7140 if (clear_buffer_p && Z > BEG)
7141 del_range (BEG, Z);
7142
7143 xassert (BEGV >= BEG);
7144 xassert (ZV <= Z && ZV >= BEGV);
7145
7146 rc = fn (a1, a2, a3, a4);
7147
7148 xassert (BEGV >= BEG);
7149 xassert (ZV <= Z && ZV >= BEGV);
7150
7151 unbind_to (count, Qnil);
7152 return rc;
7153 }
7154
7155
7156 /* Save state that should be preserved around the call to the function
7157 FN called in with_echo_area_buffer. */
7158
7159 static Lisp_Object
7160 with_echo_area_buffer_unwind_data (w)
7161 struct window *w;
7162 {
7163 int i = 0;
7164 Lisp_Object vector;
7165
7166 /* Reduce consing by keeping one vector in
7167 Vwith_echo_area_save_vector. */
7168 vector = Vwith_echo_area_save_vector;
7169 Vwith_echo_area_save_vector = Qnil;
7170
7171 if (NILP (vector))
7172 vector = Fmake_vector (make_number (7), Qnil);
7173
7174 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7175 AREF (vector, i) = Vdeactivate_mark, ++i;
7176 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7177
7178 if (w)
7179 {
7180 XSETWINDOW (AREF (vector, i), w); ++i;
7181 AREF (vector, i) = w->buffer; ++i;
7182 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7183 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7184 }
7185 else
7186 {
7187 int end = i + 4;
7188 for (; i < end; ++i)
7189 AREF (vector, i) = Qnil;
7190 }
7191
7192 xassert (i == ASIZE (vector));
7193 return vector;
7194 }
7195
7196
7197 /* Restore global state from VECTOR which was created by
7198 with_echo_area_buffer_unwind_data. */
7199
7200 static Lisp_Object
7201 unwind_with_echo_area_buffer (vector)
7202 Lisp_Object vector;
7203 {
7204 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7205 Vdeactivate_mark = AREF (vector, 1);
7206 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7207
7208 if (WINDOWP (AREF (vector, 3)))
7209 {
7210 struct window *w;
7211 Lisp_Object buffer, charpos, bytepos;
7212
7213 w = XWINDOW (AREF (vector, 3));
7214 buffer = AREF (vector, 4);
7215 charpos = AREF (vector, 5);
7216 bytepos = AREF (vector, 6);
7217
7218 w->buffer = buffer;
7219 set_marker_both (w->pointm, buffer,
7220 XFASTINT (charpos), XFASTINT (bytepos));
7221 }
7222
7223 Vwith_echo_area_save_vector = vector;
7224 return Qnil;
7225 }
7226
7227
7228 /* Set up the echo area for use by print functions. MULTIBYTE_P
7229 non-zero means we will print multibyte. */
7230
7231 void
7232 setup_echo_area_for_printing (multibyte_p)
7233 int multibyte_p;
7234 {
7235 /* If we can't find an echo area any more, exit. */
7236 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7237 Fkill_emacs (Qnil);
7238
7239 ensure_echo_area_buffers ();
7240
7241 if (!message_buf_print)
7242 {
7243 /* A message has been output since the last time we printed.
7244 Choose a fresh echo area buffer. */
7245 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7246 echo_area_buffer[0] = echo_buffer[1];
7247 else
7248 echo_area_buffer[0] = echo_buffer[0];
7249
7250 /* Switch to that buffer and clear it. */
7251 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7252 current_buffer->truncate_lines = Qnil;
7253
7254 if (Z > BEG)
7255 {
7256 int count = SPECPDL_INDEX ();
7257 specbind (Qinhibit_read_only, Qt);
7258 /* Note that undo recording is always disabled. */
7259 del_range (BEG, Z);
7260 unbind_to (count, Qnil);
7261 }
7262 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7263
7264 /* Set up the buffer for the multibyteness we need. */
7265 if (multibyte_p
7266 != !NILP (current_buffer->enable_multibyte_characters))
7267 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7268
7269 /* Raise the frame containing the echo area. */
7270 if (minibuffer_auto_raise)
7271 {
7272 struct frame *sf = SELECTED_FRAME ();
7273 Lisp_Object mini_window;
7274 mini_window = FRAME_MINIBUF_WINDOW (sf);
7275 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7276 }
7277
7278 message_log_maybe_newline ();
7279 message_buf_print = 1;
7280 }
7281 else
7282 {
7283 if (NILP (echo_area_buffer[0]))
7284 {
7285 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7286 echo_area_buffer[0] = echo_buffer[1];
7287 else
7288 echo_area_buffer[0] = echo_buffer[0];
7289 }
7290
7291 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7292 {
7293 /* Someone switched buffers between print requests. */
7294 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7295 current_buffer->truncate_lines = Qnil;
7296 }
7297 }
7298 }
7299
7300
7301 /* Display an echo area message in window W. Value is non-zero if W's
7302 height is changed. If display_last_displayed_message_p is
7303 non-zero, display the message that was last displayed, otherwise
7304 display the current message. */
7305
7306 static int
7307 display_echo_area (w)
7308 struct window *w;
7309 {
7310 int i, no_message_p, window_height_changed_p, count;
7311
7312 /* Temporarily disable garbage collections while displaying the echo
7313 area. This is done because a GC can print a message itself.
7314 That message would modify the echo area buffer's contents while a
7315 redisplay of the buffer is going on, and seriously confuse
7316 redisplay. */
7317 count = inhibit_garbage_collection ();
7318
7319 /* If there is no message, we must call display_echo_area_1
7320 nevertheless because it resizes the window. But we will have to
7321 reset the echo_area_buffer in question to nil at the end because
7322 with_echo_area_buffer will sets it to an empty buffer. */
7323 i = display_last_displayed_message_p ? 1 : 0;
7324 no_message_p = NILP (echo_area_buffer[i]);
7325
7326 window_height_changed_p
7327 = with_echo_area_buffer (w, display_last_displayed_message_p,
7328 display_echo_area_1,
7329 (EMACS_INT) w, Qnil, 0, 0);
7330
7331 if (no_message_p)
7332 echo_area_buffer[i] = Qnil;
7333
7334 unbind_to (count, Qnil);
7335 return window_height_changed_p;
7336 }
7337
7338
7339 /* Helper for display_echo_area. Display the current buffer which
7340 contains the current echo area message in window W, a mini-window,
7341 a pointer to which is passed in A1. A2..A4 are currently not used.
7342 Change the height of W so that all of the message is displayed.
7343 Value is non-zero if height of W was changed. */
7344
7345 static int
7346 display_echo_area_1 (a1, a2, a3, a4)
7347 EMACS_INT a1;
7348 Lisp_Object a2;
7349 EMACS_INT a3, a4;
7350 {
7351 struct window *w = (struct window *) a1;
7352 Lisp_Object window;
7353 struct text_pos start;
7354 int window_height_changed_p = 0;
7355
7356 /* Do this before displaying, so that we have a large enough glyph
7357 matrix for the display. */
7358 window_height_changed_p = resize_mini_window (w, 0);
7359
7360 /* Display. */
7361 clear_glyph_matrix (w->desired_matrix);
7362 XSETWINDOW (window, w);
7363 SET_TEXT_POS (start, BEG, BEG_BYTE);
7364 try_window (window, start);
7365
7366 return window_height_changed_p;
7367 }
7368
7369
7370 /* Resize the echo area window to exactly the size needed for the
7371 currently displayed message, if there is one. If a mini-buffer
7372 is active, don't shrink it. */
7373
7374 void
7375 resize_echo_area_exactly ()
7376 {
7377 if (BUFFERP (echo_area_buffer[0])
7378 && WINDOWP (echo_area_window))
7379 {
7380 struct window *w = XWINDOW (echo_area_window);
7381 int resized_p;
7382 Lisp_Object resize_exactly;
7383
7384 if (minibuf_level == 0)
7385 resize_exactly = Qt;
7386 else
7387 resize_exactly = Qnil;
7388
7389 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7390 (EMACS_INT) w, resize_exactly, 0, 0);
7391 if (resized_p)
7392 {
7393 ++windows_or_buffers_changed;
7394 ++update_mode_lines;
7395 redisplay_internal (0);
7396 }
7397 }
7398 }
7399
7400
7401 /* Callback function for with_echo_area_buffer, when used from
7402 resize_echo_area_exactly. A1 contains a pointer to the window to
7403 resize, EXACTLY non-nil means resize the mini-window exactly to the
7404 size of the text displayed. A3 and A4 are not used. Value is what
7405 resize_mini_window returns. */
7406
7407 static int
7408 resize_mini_window_1 (a1, exactly, a3, a4)
7409 EMACS_INT a1;
7410 Lisp_Object exactly;
7411 EMACS_INT a3, a4;
7412 {
7413 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7414 }
7415
7416
7417 /* Resize mini-window W to fit the size of its contents. EXACT:P
7418 means size the window exactly to the size needed. Otherwise, it's
7419 only enlarged until W's buffer is empty. Value is non-zero if
7420 the window height has been changed. */
7421
7422 int
7423 resize_mini_window (w, exact_p)
7424 struct window *w;
7425 int exact_p;
7426 {
7427 struct frame *f = XFRAME (w->frame);
7428 int window_height_changed_p = 0;
7429
7430 xassert (MINI_WINDOW_P (w));
7431
7432 /* Don't resize windows while redisplaying a window; it would
7433 confuse redisplay functions when the size of the window they are
7434 displaying changes from under them. Such a resizing can happen,
7435 for instance, when which-func prints a long message while
7436 we are running fontification-functions. We're running these
7437 functions with safe_call which binds inhibit-redisplay to t. */
7438 if (!NILP (Vinhibit_redisplay))
7439 return 0;
7440
7441 /* Nil means don't try to resize. */
7442 if (NILP (Vresize_mini_windows)
7443 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7444 return 0;
7445
7446 if (!FRAME_MINIBUF_ONLY_P (f))
7447 {
7448 struct it it;
7449 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7450 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7451 int height, max_height;
7452 int unit = FRAME_LINE_HEIGHT (f);
7453 struct text_pos start;
7454 struct buffer *old_current_buffer = NULL;
7455
7456 if (current_buffer != XBUFFER (w->buffer))
7457 {
7458 old_current_buffer = current_buffer;
7459 set_buffer_internal (XBUFFER (w->buffer));
7460 }
7461
7462 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7463
7464 /* Compute the max. number of lines specified by the user. */
7465 if (FLOATP (Vmax_mini_window_height))
7466 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7467 else if (INTEGERP (Vmax_mini_window_height))
7468 max_height = XINT (Vmax_mini_window_height);
7469 else
7470 max_height = total_height / 4;
7471
7472 /* Correct that max. height if it's bogus. */
7473 max_height = max (1, max_height);
7474 max_height = min (total_height, max_height);
7475
7476 /* Find out the height of the text in the window. */
7477 if (it.truncate_lines_p)
7478 height = 1;
7479 else
7480 {
7481 last_height = 0;
7482 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7483 if (it.max_ascent == 0 && it.max_descent == 0)
7484 height = it.current_y + last_height;
7485 else
7486 height = it.current_y + it.max_ascent + it.max_descent;
7487 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
7488 height = (height + unit - 1) / unit;
7489 }
7490
7491 /* Compute a suitable window start. */
7492 if (height > max_height)
7493 {
7494 height = max_height;
7495 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7496 move_it_vertically_backward (&it, (height - 1) * unit);
7497 start = it.current.pos;
7498 }
7499 else
7500 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7501 SET_MARKER_FROM_TEXT_POS (w->start, start);
7502
7503 if (EQ (Vresize_mini_windows, Qgrow_only))
7504 {
7505 /* Let it grow only, until we display an empty message, in which
7506 case the window shrinks again. */
7507 if (height > WINDOW_TOTAL_LINES (w))
7508 {
7509 int old_height = WINDOW_TOTAL_LINES (w);
7510 freeze_window_starts (f, 1);
7511 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7512 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7513 }
7514 else if (height < WINDOW_TOTAL_LINES (w)
7515 && (exact_p || BEGV == ZV))
7516 {
7517 int old_height = WINDOW_TOTAL_LINES (w);
7518 freeze_window_starts (f, 0);
7519 shrink_mini_window (w);
7520 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7521 }
7522 }
7523 else
7524 {
7525 /* Always resize to exact size needed. */
7526 if (height > WINDOW_TOTAL_LINES (w))
7527 {
7528 int old_height = WINDOW_TOTAL_LINES (w);
7529 freeze_window_starts (f, 1);
7530 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7531 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7532 }
7533 else if (height < WINDOW_TOTAL_LINES (w))
7534 {
7535 int old_height = WINDOW_TOTAL_LINES (w);
7536 freeze_window_starts (f, 0);
7537 shrink_mini_window (w);
7538
7539 if (height)
7540 {
7541 freeze_window_starts (f, 1);
7542 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7543 }
7544
7545 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7546 }
7547 }
7548
7549 if (old_current_buffer)
7550 set_buffer_internal (old_current_buffer);
7551 }
7552
7553 return window_height_changed_p;
7554 }
7555
7556
7557 /* Value is the current message, a string, or nil if there is no
7558 current message. */
7559
7560 Lisp_Object
7561 current_message ()
7562 {
7563 Lisp_Object msg;
7564
7565 if (NILP (echo_area_buffer[0]))
7566 msg = Qnil;
7567 else
7568 {
7569 with_echo_area_buffer (0, 0, current_message_1,
7570 (EMACS_INT) &msg, Qnil, 0, 0);
7571 if (NILP (msg))
7572 echo_area_buffer[0] = Qnil;
7573 }
7574
7575 return msg;
7576 }
7577
7578
7579 static int
7580 current_message_1 (a1, a2, a3, a4)
7581 EMACS_INT a1;
7582 Lisp_Object a2;
7583 EMACS_INT a3, a4;
7584 {
7585 Lisp_Object *msg = (Lisp_Object *) a1;
7586
7587 if (Z > BEG)
7588 *msg = make_buffer_string (BEG, Z, 1);
7589 else
7590 *msg = Qnil;
7591 return 0;
7592 }
7593
7594
7595 /* Push the current message on Vmessage_stack for later restauration
7596 by restore_message. Value is non-zero if the current message isn't
7597 empty. This is a relatively infrequent operation, so it's not
7598 worth optimizing. */
7599
7600 int
7601 push_message ()
7602 {
7603 Lisp_Object msg;
7604 msg = current_message ();
7605 Vmessage_stack = Fcons (msg, Vmessage_stack);
7606 return STRINGP (msg);
7607 }
7608
7609
7610 /* Restore message display from the top of Vmessage_stack. */
7611
7612 void
7613 restore_message ()
7614 {
7615 Lisp_Object msg;
7616
7617 xassert (CONSP (Vmessage_stack));
7618 msg = XCAR (Vmessage_stack);
7619 if (STRINGP (msg))
7620 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7621 else
7622 message3_nolog (msg, 0, 0);
7623 }
7624
7625
7626 /* Handler for record_unwind_protect calling pop_message. */
7627
7628 Lisp_Object
7629 pop_message_unwind (dummy)
7630 Lisp_Object dummy;
7631 {
7632 pop_message ();
7633 return Qnil;
7634 }
7635
7636 /* Pop the top-most entry off Vmessage_stack. */
7637
7638 void
7639 pop_message ()
7640 {
7641 xassert (CONSP (Vmessage_stack));
7642 Vmessage_stack = XCDR (Vmessage_stack);
7643 }
7644
7645
7646 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7647 exits. If the stack is not empty, we have a missing pop_message
7648 somewhere. */
7649
7650 void
7651 check_message_stack ()
7652 {
7653 if (!NILP (Vmessage_stack))
7654 abort ();
7655 }
7656
7657
7658 /* Truncate to NCHARS what will be displayed in the echo area the next
7659 time we display it---but don't redisplay it now. */
7660
7661 void
7662 truncate_echo_area (nchars)
7663 int nchars;
7664 {
7665 if (nchars == 0)
7666 echo_area_buffer[0] = Qnil;
7667 /* A null message buffer means that the frame hasn't really been
7668 initialized yet. Error messages get reported properly by
7669 cmd_error, so this must be just an informative message; toss it. */
7670 else if (!noninteractive
7671 && INTERACTIVE
7672 && !NILP (echo_area_buffer[0]))
7673 {
7674 struct frame *sf = SELECTED_FRAME ();
7675 if (FRAME_MESSAGE_BUF (sf))
7676 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7677 }
7678 }
7679
7680
7681 /* Helper function for truncate_echo_area. Truncate the current
7682 message to at most NCHARS characters. */
7683
7684 static int
7685 truncate_message_1 (nchars, a2, a3, a4)
7686 EMACS_INT nchars;
7687 Lisp_Object a2;
7688 EMACS_INT a3, a4;
7689 {
7690 if (BEG + nchars < Z)
7691 del_range (BEG + nchars, Z);
7692 if (Z == BEG)
7693 echo_area_buffer[0] = Qnil;
7694 return 0;
7695 }
7696
7697
7698 /* Set the current message to a substring of S or STRING.
7699
7700 If STRING is a Lisp string, set the message to the first NBYTES
7701 bytes from STRING. NBYTES zero means use the whole string. If
7702 STRING is multibyte, the message will be displayed multibyte.
7703
7704 If S is not null, set the message to the first LEN bytes of S. LEN
7705 zero means use the whole string. MULTIBYTE_P non-zero means S is
7706 multibyte. Display the message multibyte in that case. */
7707
7708 void
7709 set_message (s, string, nbytes, multibyte_p)
7710 const char *s;
7711 Lisp_Object string;
7712 int nbytes, multibyte_p;
7713 {
7714 message_enable_multibyte
7715 = ((s && multibyte_p)
7716 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7717
7718 with_echo_area_buffer (0, -1, set_message_1,
7719 (EMACS_INT) s, string, nbytes, multibyte_p);
7720 message_buf_print = 0;
7721 help_echo_showing_p = 0;
7722 }
7723
7724
7725 /* Helper function for set_message. Arguments have the same meaning
7726 as there, with A1 corresponding to S and A2 corresponding to STRING
7727 This function is called with the echo area buffer being
7728 current. */
7729
7730 static int
7731 set_message_1 (a1, a2, nbytes, multibyte_p)
7732 EMACS_INT a1;
7733 Lisp_Object a2;
7734 EMACS_INT nbytes, multibyte_p;
7735 {
7736 const char *s = (const char *) a1;
7737 Lisp_Object string = a2;
7738
7739 xassert (BEG == Z);
7740
7741 /* Change multibyteness of the echo buffer appropriately. */
7742 if (message_enable_multibyte
7743 != !NILP (current_buffer->enable_multibyte_characters))
7744 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7745
7746 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7747
7748 /* Insert new message at BEG. */
7749 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7750
7751 if (STRINGP (string))
7752 {
7753 int nchars;
7754
7755 if (nbytes == 0)
7756 nbytes = SBYTES (string);
7757 nchars = string_byte_to_char (string, nbytes);
7758
7759 /* This function takes care of single/multibyte conversion. We
7760 just have to ensure that the echo area buffer has the right
7761 setting of enable_multibyte_characters. */
7762 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7763 }
7764 else if (s)
7765 {
7766 if (nbytes == 0)
7767 nbytes = strlen (s);
7768
7769 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7770 {
7771 /* Convert from multi-byte to single-byte. */
7772 int i, c, n;
7773 unsigned char work[1];
7774
7775 /* Convert a multibyte string to single-byte. */
7776 for (i = 0; i < nbytes; i += n)
7777 {
7778 c = string_char_and_length (s + i, nbytes - i, &n);
7779 work[0] = (SINGLE_BYTE_CHAR_P (c)
7780 ? c
7781 : multibyte_char_to_unibyte (c, Qnil));
7782 insert_1_both (work, 1, 1, 1, 0, 0);
7783 }
7784 }
7785 else if (!multibyte_p
7786 && !NILP (current_buffer->enable_multibyte_characters))
7787 {
7788 /* Convert from single-byte to multi-byte. */
7789 int i, c, n;
7790 const unsigned char *msg = (const unsigned char *) s;
7791 unsigned char str[MAX_MULTIBYTE_LENGTH];
7792
7793 /* Convert a single-byte string to multibyte. */
7794 for (i = 0; i < nbytes; i++)
7795 {
7796 c = unibyte_char_to_multibyte (msg[i]);
7797 n = CHAR_STRING (c, str);
7798 insert_1_both (str, 1, n, 1, 0, 0);
7799 }
7800 }
7801 else
7802 insert_1 (s, nbytes, 1, 0, 0);
7803 }
7804
7805 return 0;
7806 }
7807
7808
7809 /* Clear messages. CURRENT_P non-zero means clear the current
7810 message. LAST_DISPLAYED_P non-zero means clear the message
7811 last displayed. */
7812
7813 void
7814 clear_message (current_p, last_displayed_p)
7815 int current_p, last_displayed_p;
7816 {
7817 if (current_p)
7818 {
7819 echo_area_buffer[0] = Qnil;
7820 message_cleared_p = 1;
7821 }
7822
7823 if (last_displayed_p)
7824 echo_area_buffer[1] = Qnil;
7825
7826 message_buf_print = 0;
7827 }
7828
7829 /* Clear garbaged frames.
7830
7831 This function is used where the old redisplay called
7832 redraw_garbaged_frames which in turn called redraw_frame which in
7833 turn called clear_frame. The call to clear_frame was a source of
7834 flickering. I believe a clear_frame is not necessary. It should
7835 suffice in the new redisplay to invalidate all current matrices,
7836 and ensure a complete redisplay of all windows. */
7837
7838 static void
7839 clear_garbaged_frames ()
7840 {
7841 if (frame_garbaged)
7842 {
7843 Lisp_Object tail, frame;
7844 int changed_count = 0;
7845
7846 FOR_EACH_FRAME (tail, frame)
7847 {
7848 struct frame *f = XFRAME (frame);
7849
7850 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7851 {
7852 if (f->resized_p)
7853 {
7854 Fredraw_frame (frame);
7855 f->force_flush_display_p = 1;
7856 }
7857 clear_current_matrices (f);
7858 changed_count++;
7859 f->garbaged = 0;
7860 f->resized_p = 0;
7861 }
7862 }
7863
7864 frame_garbaged = 0;
7865 if (changed_count)
7866 ++windows_or_buffers_changed;
7867 }
7868 }
7869
7870
7871 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7872 is non-zero update selected_frame. Value is non-zero if the
7873 mini-windows height has been changed. */
7874
7875 static int
7876 echo_area_display (update_frame_p)
7877 int update_frame_p;
7878 {
7879 Lisp_Object mini_window;
7880 struct window *w;
7881 struct frame *f;
7882 int window_height_changed_p = 0;
7883 struct frame *sf = SELECTED_FRAME ();
7884
7885 mini_window = FRAME_MINIBUF_WINDOW (sf);
7886 w = XWINDOW (mini_window);
7887 f = XFRAME (WINDOW_FRAME (w));
7888
7889 /* Don't display if frame is invisible or not yet initialized. */
7890 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7891 return 0;
7892
7893 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7894 #ifndef MAC_OS8
7895 #ifdef HAVE_WINDOW_SYSTEM
7896 /* When Emacs starts, selected_frame may be a visible terminal
7897 frame, even if we run under a window system. If we let this
7898 through, a message would be displayed on the terminal. */
7899 if (EQ (selected_frame, Vterminal_frame)
7900 && !NILP (Vwindow_system))
7901 return 0;
7902 #endif /* HAVE_WINDOW_SYSTEM */
7903 #endif
7904
7905 /* Redraw garbaged frames. */
7906 if (frame_garbaged)
7907 clear_garbaged_frames ();
7908
7909 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7910 {
7911 echo_area_window = mini_window;
7912 window_height_changed_p = display_echo_area (w);
7913 w->must_be_updated_p = 1;
7914
7915 /* Update the display, unless called from redisplay_internal.
7916 Also don't update the screen during redisplay itself. The
7917 update will happen at the end of redisplay, and an update
7918 here could cause confusion. */
7919 if (update_frame_p && !redisplaying_p)
7920 {
7921 int n = 0;
7922
7923 /* If the display update has been interrupted by pending
7924 input, update mode lines in the frame. Due to the
7925 pending input, it might have been that redisplay hasn't
7926 been called, so that mode lines above the echo area are
7927 garbaged. This looks odd, so we prevent it here. */
7928 if (!display_completed)
7929 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7930
7931 if (window_height_changed_p
7932 /* Don't do this if Emacs is shutting down. Redisplay
7933 needs to run hooks. */
7934 && !NILP (Vrun_hooks))
7935 {
7936 /* Must update other windows. Likewise as in other
7937 cases, don't let this update be interrupted by
7938 pending input. */
7939 int count = SPECPDL_INDEX ();
7940 specbind (Qredisplay_dont_pause, Qt);
7941 windows_or_buffers_changed = 1;
7942 redisplay_internal (0);
7943 unbind_to (count, Qnil);
7944 }
7945 else if (FRAME_WINDOW_P (f) && n == 0)
7946 {
7947 /* Window configuration is the same as before.
7948 Can do with a display update of the echo area,
7949 unless we displayed some mode lines. */
7950 update_single_window (w, 1);
7951 rif->flush_display (f);
7952 }
7953 else
7954 update_frame (f, 1, 1);
7955
7956 /* If cursor is in the echo area, make sure that the next
7957 redisplay displays the minibuffer, so that the cursor will
7958 be replaced with what the minibuffer wants. */
7959 if (cursor_in_echo_area)
7960 ++windows_or_buffers_changed;
7961 }
7962 }
7963 else if (!EQ (mini_window, selected_window))
7964 windows_or_buffers_changed++;
7965
7966 /* Last displayed message is now the current message. */
7967 echo_area_buffer[1] = echo_area_buffer[0];
7968
7969 /* Prevent redisplay optimization in redisplay_internal by resetting
7970 this_line_start_pos. This is done because the mini-buffer now
7971 displays the message instead of its buffer text. */
7972 if (EQ (mini_window, selected_window))
7973 CHARPOS (this_line_start_pos) = 0;
7974
7975 return window_height_changed_p;
7976 }
7977
7978
7979 \f
7980 /***********************************************************************
7981 Frame Titles
7982 ***********************************************************************/
7983
7984
7985 /* The frame title buffering code is also used by Fformat_mode_line.
7986 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
7987
7988 /* A buffer for constructing frame titles in it; allocated from the
7989 heap in init_xdisp and resized as needed in store_frame_title_char. */
7990
7991 static char *frame_title_buf;
7992
7993 /* The buffer's end, and a current output position in it. */
7994
7995 static char *frame_title_buf_end;
7996 static char *frame_title_ptr;
7997
7998
7999 /* Store a single character C for the frame title in frame_title_buf.
8000 Re-allocate frame_title_buf if necessary. */
8001
8002 static void
8003 #ifdef PROTOTYPES
8004 store_frame_title_char (char c)
8005 #else
8006 store_frame_title_char (c)
8007 char c;
8008 #endif
8009 {
8010 /* If output position has reached the end of the allocated buffer,
8011 double the buffer's size. */
8012 if (frame_title_ptr == frame_title_buf_end)
8013 {
8014 int len = frame_title_ptr - frame_title_buf;
8015 int new_size = 2 * len * sizeof *frame_title_buf;
8016 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
8017 frame_title_buf_end = frame_title_buf + new_size;
8018 frame_title_ptr = frame_title_buf + len;
8019 }
8020
8021 *frame_title_ptr++ = c;
8022 }
8023
8024
8025 /* Store part of a frame title in frame_title_buf, beginning at
8026 frame_title_ptr. STR is the string to store. Do not copy
8027 characters that yield more columns than PRECISION; PRECISION <= 0
8028 means copy the whole string. Pad with spaces until FIELD_WIDTH
8029 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8030 pad. Called from display_mode_element when it is used to build a
8031 frame title. */
8032
8033 static int
8034 store_frame_title (str, field_width, precision)
8035 const unsigned char *str;
8036 int field_width, precision;
8037 {
8038 int n = 0;
8039 int dummy, nbytes;
8040
8041 /* Copy at most PRECISION chars from STR. */
8042 nbytes = strlen (str);
8043 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8044 while (nbytes--)
8045 store_frame_title_char (*str++);
8046
8047 /* Fill up with spaces until FIELD_WIDTH reached. */
8048 while (field_width > 0
8049 && n < field_width)
8050 {
8051 store_frame_title_char (' ');
8052 ++n;
8053 }
8054
8055 return n;
8056 }
8057
8058 #ifdef HAVE_WINDOW_SYSTEM
8059
8060 /* Set the title of FRAME, if it has changed. The title format is
8061 Vicon_title_format if FRAME is iconified, otherwise it is
8062 frame_title_format. */
8063
8064 static void
8065 x_consider_frame_title (frame)
8066 Lisp_Object frame;
8067 {
8068 struct frame *f = XFRAME (frame);
8069
8070 if (FRAME_WINDOW_P (f)
8071 || FRAME_MINIBUF_ONLY_P (f)
8072 || f->explicit_name)
8073 {
8074 /* Do we have more than one visible frame on this X display? */
8075 Lisp_Object tail;
8076 Lisp_Object fmt;
8077 struct buffer *obuf;
8078 int len;
8079 struct it it;
8080
8081 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8082 {
8083 Lisp_Object other_frame = XCAR (tail);
8084 struct frame *tf = XFRAME (other_frame);
8085
8086 if (tf != f
8087 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8088 && !FRAME_MINIBUF_ONLY_P (tf)
8089 && !EQ (other_frame, tip_frame)
8090 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8091 break;
8092 }
8093
8094 /* Set global variable indicating that multiple frames exist. */
8095 multiple_frames = CONSP (tail);
8096
8097 /* Switch to the buffer of selected window of the frame. Set up
8098 frame_title_ptr so that display_mode_element will output into it;
8099 then display the title. */
8100 obuf = current_buffer;
8101 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8102 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8103 frame_title_ptr = frame_title_buf;
8104 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8105 NULL, DEFAULT_FACE_ID);
8106 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8107 len = frame_title_ptr - frame_title_buf;
8108 frame_title_ptr = NULL;
8109 set_buffer_internal_1 (obuf);
8110
8111 /* Set the title only if it's changed. This avoids consing in
8112 the common case where it hasn't. (If it turns out that we've
8113 already wasted too much time by walking through the list with
8114 display_mode_element, then we might need to optimize at a
8115 higher level than this.) */
8116 if (! STRINGP (f->name)
8117 || SBYTES (f->name) != len
8118 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
8119 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
8120 }
8121 }
8122
8123 #endif /* not HAVE_WINDOW_SYSTEM */
8124
8125
8126
8127 \f
8128 /***********************************************************************
8129 Menu Bars
8130 ***********************************************************************/
8131
8132
8133 /* Prepare for redisplay by updating menu-bar item lists when
8134 appropriate. This can call eval. */
8135
8136 void
8137 prepare_menu_bars ()
8138 {
8139 int all_windows;
8140 struct gcpro gcpro1, gcpro2;
8141 struct frame *f;
8142 Lisp_Object tooltip_frame;
8143
8144 #ifdef HAVE_WINDOW_SYSTEM
8145 tooltip_frame = tip_frame;
8146 #else
8147 tooltip_frame = Qnil;
8148 #endif
8149
8150 /* Update all frame titles based on their buffer names, etc. We do
8151 this before the menu bars so that the buffer-menu will show the
8152 up-to-date frame titles. */
8153 #ifdef HAVE_WINDOW_SYSTEM
8154 if (windows_or_buffers_changed || update_mode_lines)
8155 {
8156 Lisp_Object tail, frame;
8157
8158 FOR_EACH_FRAME (tail, frame)
8159 {
8160 f = XFRAME (frame);
8161 if (!EQ (frame, tooltip_frame)
8162 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8163 x_consider_frame_title (frame);
8164 }
8165 }
8166 #endif /* HAVE_WINDOW_SYSTEM */
8167
8168 /* Update the menu bar item lists, if appropriate. This has to be
8169 done before any actual redisplay or generation of display lines. */
8170 all_windows = (update_mode_lines
8171 || buffer_shared > 1
8172 || windows_or_buffers_changed);
8173 if (all_windows)
8174 {
8175 Lisp_Object tail, frame;
8176 int count = SPECPDL_INDEX ();
8177
8178 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8179
8180 FOR_EACH_FRAME (tail, frame)
8181 {
8182 f = XFRAME (frame);
8183
8184 /* Ignore tooltip frame. */
8185 if (EQ (frame, tooltip_frame))
8186 continue;
8187
8188 /* If a window on this frame changed size, report that to
8189 the user and clear the size-change flag. */
8190 if (FRAME_WINDOW_SIZES_CHANGED (f))
8191 {
8192 Lisp_Object functions;
8193
8194 /* Clear flag first in case we get an error below. */
8195 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8196 functions = Vwindow_size_change_functions;
8197 GCPRO2 (tail, functions);
8198
8199 while (CONSP (functions))
8200 {
8201 call1 (XCAR (functions), frame);
8202 functions = XCDR (functions);
8203 }
8204 UNGCPRO;
8205 }
8206
8207 GCPRO1 (tail);
8208 update_menu_bar (f, 0);
8209 #ifdef HAVE_WINDOW_SYSTEM
8210 update_tool_bar (f, 0);
8211 #endif
8212 UNGCPRO;
8213 }
8214
8215 unbind_to (count, Qnil);
8216 }
8217 else
8218 {
8219 struct frame *sf = SELECTED_FRAME ();
8220 update_menu_bar (sf, 1);
8221 #ifdef HAVE_WINDOW_SYSTEM
8222 update_tool_bar (sf, 1);
8223 #endif
8224 }
8225
8226 /* Motif needs this. See comment in xmenu.c. Turn it off when
8227 pending_menu_activation is not defined. */
8228 #ifdef USE_X_TOOLKIT
8229 pending_menu_activation = 0;
8230 #endif
8231 }
8232
8233
8234 /* Update the menu bar item list for frame F. This has to be done
8235 before we start to fill in any display lines, because it can call
8236 eval.
8237
8238 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8239
8240 static void
8241 update_menu_bar (f, save_match_data)
8242 struct frame *f;
8243 int save_match_data;
8244 {
8245 Lisp_Object window;
8246 register struct window *w;
8247
8248 /* If called recursively during a menu update, do nothing. This can
8249 happen when, for instance, an activate-menubar-hook causes a
8250 redisplay. */
8251 if (inhibit_menubar_update)
8252 return;
8253
8254 window = FRAME_SELECTED_WINDOW (f);
8255 w = XWINDOW (window);
8256
8257 #if 0 /* The if statement below this if statement used to include the
8258 condition !NILP (w->update_mode_line), rather than using
8259 update_mode_lines directly, and this if statement may have
8260 been added to make that condition work. Now the if
8261 statement below matches its comment, this isn't needed. */
8262 if (update_mode_lines)
8263 w->update_mode_line = Qt;
8264 #endif
8265
8266 if (FRAME_WINDOW_P (f)
8267 ?
8268 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8269 || defined (USE_GTK)
8270 FRAME_EXTERNAL_MENU_BAR (f)
8271 #else
8272 FRAME_MENU_BAR_LINES (f) > 0
8273 #endif
8274 : FRAME_MENU_BAR_LINES (f) > 0)
8275 {
8276 /* If the user has switched buffers or windows, we need to
8277 recompute to reflect the new bindings. But we'll
8278 recompute when update_mode_lines is set too; that means
8279 that people can use force-mode-line-update to request
8280 that the menu bar be recomputed. The adverse effect on
8281 the rest of the redisplay algorithm is about the same as
8282 windows_or_buffers_changed anyway. */
8283 if (windows_or_buffers_changed
8284 /* This used to test w->update_mode_line, but we believe
8285 there is no need to recompute the menu in that case. */
8286 || update_mode_lines
8287 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8288 < BUF_MODIFF (XBUFFER (w->buffer)))
8289 != !NILP (w->last_had_star))
8290 || ((!NILP (Vtransient_mark_mode)
8291 && !NILP (XBUFFER (w->buffer)->mark_active))
8292 != !NILP (w->region_showing)))
8293 {
8294 struct buffer *prev = current_buffer;
8295 int count = SPECPDL_INDEX ();
8296
8297 specbind (Qinhibit_menubar_update, Qt);
8298
8299 set_buffer_internal_1 (XBUFFER (w->buffer));
8300 if (save_match_data)
8301 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8302 if (NILP (Voverriding_local_map_menu_flag))
8303 {
8304 specbind (Qoverriding_terminal_local_map, Qnil);
8305 specbind (Qoverriding_local_map, Qnil);
8306 }
8307
8308 /* Run the Lucid hook. */
8309 safe_run_hooks (Qactivate_menubar_hook);
8310
8311 /* If it has changed current-menubar from previous value,
8312 really recompute the menu-bar from the value. */
8313 if (! NILP (Vlucid_menu_bar_dirty_flag))
8314 call0 (Qrecompute_lucid_menubar);
8315
8316 safe_run_hooks (Qmenu_bar_update_hook);
8317 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8318
8319 /* Redisplay the menu bar in case we changed it. */
8320 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8321 || defined (USE_GTK)
8322 if (FRAME_WINDOW_P (f)
8323 #if defined (MAC_OS)
8324 /* All frames on Mac OS share the same menubar. So only the
8325 selected frame should be allowed to set it. */
8326 && f == SELECTED_FRAME ()
8327 #endif
8328 )
8329 set_frame_menubar (f, 0, 0);
8330 else
8331 /* On a terminal screen, the menu bar is an ordinary screen
8332 line, and this makes it get updated. */
8333 w->update_mode_line = Qt;
8334 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8335 /* In the non-toolkit version, the menu bar is an ordinary screen
8336 line, and this makes it get updated. */
8337 w->update_mode_line = Qt;
8338 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8339
8340 unbind_to (count, Qnil);
8341 set_buffer_internal_1 (prev);
8342 }
8343 }
8344 }
8345
8346
8347 \f
8348 /***********************************************************************
8349 Output Cursor
8350 ***********************************************************************/
8351
8352 #ifdef HAVE_WINDOW_SYSTEM
8353
8354 /* EXPORT:
8355 Nominal cursor position -- where to draw output.
8356 HPOS and VPOS are window relative glyph matrix coordinates.
8357 X and Y are window relative pixel coordinates. */
8358
8359 struct cursor_pos output_cursor;
8360
8361
8362 /* EXPORT:
8363 Set the global variable output_cursor to CURSOR. All cursor
8364 positions are relative to updated_window. */
8365
8366 void
8367 set_output_cursor (cursor)
8368 struct cursor_pos *cursor;
8369 {
8370 output_cursor.hpos = cursor->hpos;
8371 output_cursor.vpos = cursor->vpos;
8372 output_cursor.x = cursor->x;
8373 output_cursor.y = cursor->y;
8374 }
8375
8376
8377 /* EXPORT for RIF:
8378 Set a nominal cursor position.
8379
8380 HPOS and VPOS are column/row positions in a window glyph matrix. X
8381 and Y are window text area relative pixel positions.
8382
8383 If this is done during an update, updated_window will contain the
8384 window that is being updated and the position is the future output
8385 cursor position for that window. If updated_window is null, use
8386 selected_window and display the cursor at the given position. */
8387
8388 void
8389 x_cursor_to (vpos, hpos, y, x)
8390 int vpos, hpos, y, x;
8391 {
8392 struct window *w;
8393
8394 /* If updated_window is not set, work on selected_window. */
8395 if (updated_window)
8396 w = updated_window;
8397 else
8398 w = XWINDOW (selected_window);
8399
8400 /* Set the output cursor. */
8401 output_cursor.hpos = hpos;
8402 output_cursor.vpos = vpos;
8403 output_cursor.x = x;
8404 output_cursor.y = y;
8405
8406 /* If not called as part of an update, really display the cursor.
8407 This will also set the cursor position of W. */
8408 if (updated_window == NULL)
8409 {
8410 BLOCK_INPUT;
8411 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8412 if (rif->flush_display_optional)
8413 rif->flush_display_optional (SELECTED_FRAME ());
8414 UNBLOCK_INPUT;
8415 }
8416 }
8417
8418 #endif /* HAVE_WINDOW_SYSTEM */
8419
8420 \f
8421 /***********************************************************************
8422 Tool-bars
8423 ***********************************************************************/
8424
8425 #ifdef HAVE_WINDOW_SYSTEM
8426
8427 /* Where the mouse was last time we reported a mouse event. */
8428
8429 FRAME_PTR last_mouse_frame;
8430
8431 /* Tool-bar item index of the item on which a mouse button was pressed
8432 or -1. */
8433
8434 int last_tool_bar_item;
8435
8436
8437 /* Update the tool-bar item list for frame F. This has to be done
8438 before we start to fill in any display lines. Called from
8439 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8440 and restore it here. */
8441
8442 static void
8443 update_tool_bar (f, save_match_data)
8444 struct frame *f;
8445 int save_match_data;
8446 {
8447 #ifdef USE_GTK
8448 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8449 #else
8450 int do_update = WINDOWP (f->tool_bar_window)
8451 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8452 #endif
8453
8454 if (do_update)
8455 {
8456 Lisp_Object window;
8457 struct window *w;
8458
8459 window = FRAME_SELECTED_WINDOW (f);
8460 w = XWINDOW (window);
8461
8462 /* If the user has switched buffers or windows, we need to
8463 recompute to reflect the new bindings. But we'll
8464 recompute when update_mode_lines is set too; that means
8465 that people can use force-mode-line-update to request
8466 that the menu bar be recomputed. The adverse effect on
8467 the rest of the redisplay algorithm is about the same as
8468 windows_or_buffers_changed anyway. */
8469 if (windows_or_buffers_changed
8470 || !NILP (w->update_mode_line)
8471 || update_mode_lines
8472 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8473 < BUF_MODIFF (XBUFFER (w->buffer)))
8474 != !NILP (w->last_had_star))
8475 || ((!NILP (Vtransient_mark_mode)
8476 && !NILP (XBUFFER (w->buffer)->mark_active))
8477 != !NILP (w->region_showing)))
8478 {
8479 struct buffer *prev = current_buffer;
8480 int count = SPECPDL_INDEX ();
8481 Lisp_Object new_tool_bar;
8482 int new_n_tool_bar;
8483 struct gcpro gcpro1;
8484
8485 /* Set current_buffer to the buffer of the selected
8486 window of the frame, so that we get the right local
8487 keymaps. */
8488 set_buffer_internal_1 (XBUFFER (w->buffer));
8489
8490 /* Save match data, if we must. */
8491 if (save_match_data)
8492 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8493
8494 /* Make sure that we don't accidentally use bogus keymaps. */
8495 if (NILP (Voverriding_local_map_menu_flag))
8496 {
8497 specbind (Qoverriding_terminal_local_map, Qnil);
8498 specbind (Qoverriding_local_map, Qnil);
8499 }
8500
8501 GCPRO1 (new_tool_bar);
8502
8503 /* Build desired tool-bar items from keymaps. */
8504 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
8505 &new_n_tool_bar);
8506
8507 /* Redisplay the tool-bar if we changed it. */
8508 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
8509 {
8510 /* Redisplay that happens asynchronously due to an expose event
8511 may access f->tool_bar_items. Make sure we update both
8512 variables within BLOCK_INPUT so no such event interrupts. */
8513 BLOCK_INPUT;
8514 f->tool_bar_items = new_tool_bar;
8515 f->n_tool_bar_items = new_n_tool_bar;
8516 w->update_mode_line = Qt;
8517 UNBLOCK_INPUT;
8518 }
8519
8520 UNGCPRO;
8521
8522 unbind_to (count, Qnil);
8523 set_buffer_internal_1 (prev);
8524 }
8525 }
8526 }
8527
8528
8529 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8530 F's desired tool-bar contents. F->tool_bar_items must have
8531 been set up previously by calling prepare_menu_bars. */
8532
8533 static void
8534 build_desired_tool_bar_string (f)
8535 struct frame *f;
8536 {
8537 int i, size, size_needed;
8538 struct gcpro gcpro1, gcpro2, gcpro3;
8539 Lisp_Object image, plist, props;
8540
8541 image = plist = props = Qnil;
8542 GCPRO3 (image, plist, props);
8543
8544 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8545 Otherwise, make a new string. */
8546
8547 /* The size of the string we might be able to reuse. */
8548 size = (STRINGP (f->desired_tool_bar_string)
8549 ? SCHARS (f->desired_tool_bar_string)
8550 : 0);
8551
8552 /* We need one space in the string for each image. */
8553 size_needed = f->n_tool_bar_items;
8554
8555 /* Reuse f->desired_tool_bar_string, if possible. */
8556 if (size < size_needed || NILP (f->desired_tool_bar_string))
8557 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8558 make_number (' '));
8559 else
8560 {
8561 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8562 Fremove_text_properties (make_number (0), make_number (size),
8563 props, f->desired_tool_bar_string);
8564 }
8565
8566 /* Put a `display' property on the string for the images to display,
8567 put a `menu_item' property on tool-bar items with a value that
8568 is the index of the item in F's tool-bar item vector. */
8569 for (i = 0; i < f->n_tool_bar_items; ++i)
8570 {
8571 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8572
8573 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8574 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8575 int hmargin, vmargin, relief, idx, end;
8576 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8577
8578 /* If image is a vector, choose the image according to the
8579 button state. */
8580 image = PROP (TOOL_BAR_ITEM_IMAGES);
8581 if (VECTORP (image))
8582 {
8583 if (enabled_p)
8584 idx = (selected_p
8585 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8586 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8587 else
8588 idx = (selected_p
8589 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8590 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8591
8592 xassert (ASIZE (image) >= idx);
8593 image = AREF (image, idx);
8594 }
8595 else
8596 idx = -1;
8597
8598 /* Ignore invalid image specifications. */
8599 if (!valid_image_p (image))
8600 continue;
8601
8602 /* Display the tool-bar button pressed, or depressed. */
8603 plist = Fcopy_sequence (XCDR (image));
8604
8605 /* Compute margin and relief to draw. */
8606 relief = (tool_bar_button_relief >= 0
8607 ? tool_bar_button_relief
8608 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8609 hmargin = vmargin = relief;
8610
8611 if (INTEGERP (Vtool_bar_button_margin)
8612 && XINT (Vtool_bar_button_margin) > 0)
8613 {
8614 hmargin += XFASTINT (Vtool_bar_button_margin);
8615 vmargin += XFASTINT (Vtool_bar_button_margin);
8616 }
8617 else if (CONSP (Vtool_bar_button_margin))
8618 {
8619 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8620 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8621 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8622
8623 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8624 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8625 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8626 }
8627
8628 if (auto_raise_tool_bar_buttons_p)
8629 {
8630 /* Add a `:relief' property to the image spec if the item is
8631 selected. */
8632 if (selected_p)
8633 {
8634 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8635 hmargin -= relief;
8636 vmargin -= relief;
8637 }
8638 }
8639 else
8640 {
8641 /* If image is selected, display it pressed, i.e. with a
8642 negative relief. If it's not selected, display it with a
8643 raised relief. */
8644 plist = Fplist_put (plist, QCrelief,
8645 (selected_p
8646 ? make_number (-relief)
8647 : make_number (relief)));
8648 hmargin -= relief;
8649 vmargin -= relief;
8650 }
8651
8652 /* Put a margin around the image. */
8653 if (hmargin || vmargin)
8654 {
8655 if (hmargin == vmargin)
8656 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8657 else
8658 plist = Fplist_put (plist, QCmargin,
8659 Fcons (make_number (hmargin),
8660 make_number (vmargin)));
8661 }
8662
8663 /* If button is not enabled, and we don't have special images
8664 for the disabled state, make the image appear disabled by
8665 applying an appropriate algorithm to it. */
8666 if (!enabled_p && idx < 0)
8667 plist = Fplist_put (plist, QCconversion, Qdisabled);
8668
8669 /* Put a `display' text property on the string for the image to
8670 display. Put a `menu-item' property on the string that gives
8671 the start of this item's properties in the tool-bar items
8672 vector. */
8673 image = Fcons (Qimage, plist);
8674 props = list4 (Qdisplay, image,
8675 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8676
8677 /* Let the last image hide all remaining spaces in the tool bar
8678 string. The string can be longer than needed when we reuse a
8679 previous string. */
8680 if (i + 1 == f->n_tool_bar_items)
8681 end = SCHARS (f->desired_tool_bar_string);
8682 else
8683 end = i + 1;
8684 Fadd_text_properties (make_number (i), make_number (end),
8685 props, f->desired_tool_bar_string);
8686 #undef PROP
8687 }
8688
8689 UNGCPRO;
8690 }
8691
8692
8693 /* Display one line of the tool-bar of frame IT->f. */
8694
8695 static void
8696 display_tool_bar_line (it)
8697 struct it *it;
8698 {
8699 struct glyph_row *row = it->glyph_row;
8700 int max_x = it->last_visible_x;
8701 struct glyph *last;
8702
8703 prepare_desired_row (row);
8704 row->y = it->current_y;
8705
8706 /* Note that this isn't made use of if the face hasn't a box,
8707 so there's no need to check the face here. */
8708 it->start_of_box_run_p = 1;
8709
8710 while (it->current_x < max_x)
8711 {
8712 int x_before, x, n_glyphs_before, i, nglyphs;
8713
8714 /* Get the next display element. */
8715 if (!get_next_display_element (it))
8716 break;
8717
8718 /* Produce glyphs. */
8719 x_before = it->current_x;
8720 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8721 PRODUCE_GLYPHS (it);
8722
8723 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8724 i = 0;
8725 x = x_before;
8726 while (i < nglyphs)
8727 {
8728 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
8729
8730 if (x + glyph->pixel_width > max_x)
8731 {
8732 /* Glyph doesn't fit on line. */
8733 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8734 it->current_x = x;
8735 goto out;
8736 }
8737
8738 ++it->hpos;
8739 x += glyph->pixel_width;
8740 ++i;
8741 }
8742
8743 /* Stop at line ends. */
8744 if (ITERATOR_AT_END_OF_LINE_P (it))
8745 break;
8746
8747 set_iterator_to_next (it, 1);
8748 }
8749
8750 out:;
8751
8752 row->displays_text_p = row->used[TEXT_AREA] != 0;
8753 extend_face_to_end_of_line (it);
8754 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8755 last->right_box_line_p = 1;
8756 if (last == row->glyphs[TEXT_AREA])
8757 last->left_box_line_p = 1;
8758 compute_line_metrics (it);
8759
8760 /* If line is empty, make it occupy the rest of the tool-bar. */
8761 if (!row->displays_text_p)
8762 {
8763 row->height = row->phys_height = it->last_visible_y - row->y;
8764 row->ascent = row->phys_ascent = 0;
8765 row->extra_line_spacing = 0;
8766 }
8767
8768 row->full_width_p = 1;
8769 row->continued_p = 0;
8770 row->truncated_on_left_p = 0;
8771 row->truncated_on_right_p = 0;
8772
8773 it->current_x = it->hpos = 0;
8774 it->current_y += row->height;
8775 ++it->vpos;
8776 ++it->glyph_row;
8777 }
8778
8779
8780 /* Value is the number of screen lines needed to make all tool-bar
8781 items of frame F visible. */
8782
8783 static int
8784 tool_bar_lines_needed (f)
8785 struct frame *f;
8786 {
8787 struct window *w = XWINDOW (f->tool_bar_window);
8788 struct it it;
8789
8790 /* Initialize an iterator for iteration over
8791 F->desired_tool_bar_string in the tool-bar window of frame F. */
8792 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8793 it.first_visible_x = 0;
8794 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8795 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8796
8797 while (!ITERATOR_AT_END_P (&it))
8798 {
8799 it.glyph_row = w->desired_matrix->rows;
8800 clear_glyph_row (it.glyph_row);
8801 display_tool_bar_line (&it);
8802 }
8803
8804 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
8805 }
8806
8807
8808 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8809 0, 1, 0,
8810 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8811 (frame)
8812 Lisp_Object frame;
8813 {
8814 struct frame *f;
8815 struct window *w;
8816 int nlines = 0;
8817
8818 if (NILP (frame))
8819 frame = selected_frame;
8820 else
8821 CHECK_FRAME (frame);
8822 f = XFRAME (frame);
8823
8824 if (WINDOWP (f->tool_bar_window)
8825 || (w = XWINDOW (f->tool_bar_window),
8826 WINDOW_TOTAL_LINES (w) > 0))
8827 {
8828 update_tool_bar (f, 1);
8829 if (f->n_tool_bar_items)
8830 {
8831 build_desired_tool_bar_string (f);
8832 nlines = tool_bar_lines_needed (f);
8833 }
8834 }
8835
8836 return make_number (nlines);
8837 }
8838
8839
8840 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
8841 height should be changed. */
8842
8843 static int
8844 redisplay_tool_bar (f)
8845 struct frame *f;
8846 {
8847 struct window *w;
8848 struct it it;
8849 struct glyph_row *row;
8850 int change_height_p = 0;
8851
8852 #ifdef USE_GTK
8853 if (FRAME_EXTERNAL_TOOL_BAR (f))
8854 update_frame_tool_bar (f);
8855 return 0;
8856 #endif
8857
8858 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8859 do anything. This means you must start with tool-bar-lines
8860 non-zero to get the auto-sizing effect. Or in other words, you
8861 can turn off tool-bars by specifying tool-bar-lines zero. */
8862 if (!WINDOWP (f->tool_bar_window)
8863 || (w = XWINDOW (f->tool_bar_window),
8864 WINDOW_TOTAL_LINES (w) == 0))
8865 return 0;
8866
8867 /* Set up an iterator for the tool-bar window. */
8868 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8869 it.first_visible_x = 0;
8870 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8871 row = it.glyph_row;
8872
8873 /* Build a string that represents the contents of the tool-bar. */
8874 build_desired_tool_bar_string (f);
8875 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8876
8877 /* Display as many lines as needed to display all tool-bar items. */
8878 while (it.current_y < it.last_visible_y)
8879 display_tool_bar_line (&it);
8880
8881 /* It doesn't make much sense to try scrolling in the tool-bar
8882 window, so don't do it. */
8883 w->desired_matrix->no_scrolling_p = 1;
8884 w->must_be_updated_p = 1;
8885
8886 if (auto_resize_tool_bars_p)
8887 {
8888 int nlines;
8889
8890 /* If we couldn't display everything, change the tool-bar's
8891 height. */
8892 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8893 change_height_p = 1;
8894
8895 /* If there are blank lines at the end, except for a partially
8896 visible blank line at the end that is smaller than
8897 FRAME_LINE_HEIGHT, change the tool-bar's height. */
8898 row = it.glyph_row - 1;
8899 if (!row->displays_text_p
8900 && row->height >= FRAME_LINE_HEIGHT (f))
8901 change_height_p = 1;
8902
8903 /* If row displays tool-bar items, but is partially visible,
8904 change the tool-bar's height. */
8905 if (row->displays_text_p
8906 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8907 change_height_p = 1;
8908
8909 /* Resize windows as needed by changing the `tool-bar-lines'
8910 frame parameter. */
8911 if (change_height_p
8912 && (nlines = tool_bar_lines_needed (f),
8913 nlines != WINDOW_TOTAL_LINES (w)))
8914 {
8915 extern Lisp_Object Qtool_bar_lines;
8916 Lisp_Object frame;
8917 int old_height = WINDOW_TOTAL_LINES (w);
8918
8919 XSETFRAME (frame, f);
8920 clear_glyph_matrix (w->desired_matrix);
8921 Fmodify_frame_parameters (frame,
8922 Fcons (Fcons (Qtool_bar_lines,
8923 make_number (nlines)),
8924 Qnil));
8925 if (WINDOW_TOTAL_LINES (w) != old_height)
8926 fonts_changed_p = 1;
8927 }
8928 }
8929
8930 return change_height_p;
8931 }
8932
8933
8934 /* Get information about the tool-bar item which is displayed in GLYPH
8935 on frame F. Return in *PROP_IDX the index where tool-bar item
8936 properties start in F->tool_bar_items. Value is zero if
8937 GLYPH doesn't display a tool-bar item. */
8938
8939 static int
8940 tool_bar_item_info (f, glyph, prop_idx)
8941 struct frame *f;
8942 struct glyph *glyph;
8943 int *prop_idx;
8944 {
8945 Lisp_Object prop;
8946 int success_p;
8947 int charpos;
8948
8949 /* This function can be called asynchronously, which means we must
8950 exclude any possibility that Fget_text_property signals an
8951 error. */
8952 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8953 charpos = max (0, charpos);
8954
8955 /* Get the text property `menu-item' at pos. The value of that
8956 property is the start index of this item's properties in
8957 F->tool_bar_items. */
8958 prop = Fget_text_property (make_number (charpos),
8959 Qmenu_item, f->current_tool_bar_string);
8960 if (INTEGERP (prop))
8961 {
8962 *prop_idx = XINT (prop);
8963 success_p = 1;
8964 }
8965 else
8966 success_p = 0;
8967
8968 return success_p;
8969 }
8970
8971 \f
8972 /* Get information about the tool-bar item at position X/Y on frame F.
8973 Return in *GLYPH a pointer to the glyph of the tool-bar item in
8974 the current matrix of the tool-bar window of F, or NULL if not
8975 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8976 item in F->tool_bar_items. Value is
8977
8978 -1 if X/Y is not on a tool-bar item
8979 0 if X/Y is on the same item that was highlighted before.
8980 1 otherwise. */
8981
8982 static int
8983 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
8984 struct frame *f;
8985 int x, y;
8986 struct glyph **glyph;
8987 int *hpos, *vpos, *prop_idx;
8988 {
8989 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8990 struct window *w = XWINDOW (f->tool_bar_window);
8991 int area;
8992
8993 /* Find the glyph under X/Y. */
8994 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
8995 if (*glyph == NULL)
8996 return -1;
8997
8998 /* Get the start of this tool-bar item's properties in
8999 f->tool_bar_items. */
9000 if (!tool_bar_item_info (f, *glyph, prop_idx))
9001 return -1;
9002
9003 /* Is mouse on the highlighted item? */
9004 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9005 && *vpos >= dpyinfo->mouse_face_beg_row
9006 && *vpos <= dpyinfo->mouse_face_end_row
9007 && (*vpos > dpyinfo->mouse_face_beg_row
9008 || *hpos >= dpyinfo->mouse_face_beg_col)
9009 && (*vpos < dpyinfo->mouse_face_end_row
9010 || *hpos < dpyinfo->mouse_face_end_col
9011 || dpyinfo->mouse_face_past_end))
9012 return 0;
9013
9014 return 1;
9015 }
9016
9017
9018 /* EXPORT:
9019 Handle mouse button event on the tool-bar of frame F, at
9020 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9021 0 for button release. MODIFIERS is event modifiers for button
9022 release. */
9023
9024 void
9025 handle_tool_bar_click (f, x, y, down_p, modifiers)
9026 struct frame *f;
9027 int x, y, down_p;
9028 unsigned int modifiers;
9029 {
9030 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9031 struct window *w = XWINDOW (f->tool_bar_window);
9032 int hpos, vpos, prop_idx;
9033 struct glyph *glyph;
9034 Lisp_Object enabled_p;
9035
9036 /* If not on the highlighted tool-bar item, return. */
9037 frame_to_window_pixel_xy (w, &x, &y);
9038 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9039 return;
9040
9041 /* If item is disabled, do nothing. */
9042 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9043 if (NILP (enabled_p))
9044 return;
9045
9046 if (down_p)
9047 {
9048 /* Show item in pressed state. */
9049 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9050 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9051 last_tool_bar_item = prop_idx;
9052 }
9053 else
9054 {
9055 Lisp_Object key, frame;
9056 struct input_event event;
9057 EVENT_INIT (event);
9058
9059 /* Show item in released state. */
9060 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9061 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9062
9063 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9064
9065 XSETFRAME (frame, f);
9066 event.kind = TOOL_BAR_EVENT;
9067 event.frame_or_window = frame;
9068 event.arg = frame;
9069 kbd_buffer_store_event (&event);
9070
9071 event.kind = TOOL_BAR_EVENT;
9072 event.frame_or_window = frame;
9073 event.arg = key;
9074 event.modifiers = modifiers;
9075 kbd_buffer_store_event (&event);
9076 last_tool_bar_item = -1;
9077 }
9078 }
9079
9080
9081 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9082 tool-bar window-relative coordinates X/Y. Called from
9083 note_mouse_highlight. */
9084
9085 static void
9086 note_tool_bar_highlight (f, x, y)
9087 struct frame *f;
9088 int x, y;
9089 {
9090 Lisp_Object window = f->tool_bar_window;
9091 struct window *w = XWINDOW (window);
9092 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9093 int hpos, vpos;
9094 struct glyph *glyph;
9095 struct glyph_row *row;
9096 int i;
9097 Lisp_Object enabled_p;
9098 int prop_idx;
9099 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9100 int mouse_down_p, rc;
9101
9102 /* Function note_mouse_highlight is called with negative x(y
9103 values when mouse moves outside of the frame. */
9104 if (x <= 0 || y <= 0)
9105 {
9106 clear_mouse_face (dpyinfo);
9107 return;
9108 }
9109
9110 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9111 if (rc < 0)
9112 {
9113 /* Not on tool-bar item. */
9114 clear_mouse_face (dpyinfo);
9115 return;
9116 }
9117 else if (rc == 0)
9118 /* On same tool-bar item as before. */
9119 goto set_help_echo;
9120
9121 clear_mouse_face (dpyinfo);
9122
9123 /* Mouse is down, but on different tool-bar item? */
9124 mouse_down_p = (dpyinfo->grabbed
9125 && f == last_mouse_frame
9126 && FRAME_LIVE_P (f));
9127 if (mouse_down_p
9128 && last_tool_bar_item != prop_idx)
9129 return;
9130
9131 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9132 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9133
9134 /* If tool-bar item is not enabled, don't highlight it. */
9135 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9136 if (!NILP (enabled_p))
9137 {
9138 /* Compute the x-position of the glyph. In front and past the
9139 image is a space. We include this in the highlighted area. */
9140 row = MATRIX_ROW (w->current_matrix, vpos);
9141 for (i = x = 0; i < hpos; ++i)
9142 x += row->glyphs[TEXT_AREA][i].pixel_width;
9143
9144 /* Record this as the current active region. */
9145 dpyinfo->mouse_face_beg_col = hpos;
9146 dpyinfo->mouse_face_beg_row = vpos;
9147 dpyinfo->mouse_face_beg_x = x;
9148 dpyinfo->mouse_face_beg_y = row->y;
9149 dpyinfo->mouse_face_past_end = 0;
9150
9151 dpyinfo->mouse_face_end_col = hpos + 1;
9152 dpyinfo->mouse_face_end_row = vpos;
9153 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9154 dpyinfo->mouse_face_end_y = row->y;
9155 dpyinfo->mouse_face_window = window;
9156 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9157
9158 /* Display it as active. */
9159 show_mouse_face (dpyinfo, draw);
9160 dpyinfo->mouse_face_image_state = draw;
9161 }
9162
9163 set_help_echo:
9164
9165 /* Set help_echo_string to a help string to display for this tool-bar item.
9166 XTread_socket does the rest. */
9167 help_echo_object = help_echo_window = Qnil;
9168 help_echo_pos = -1;
9169 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9170 if (NILP (help_echo_string))
9171 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9172 }
9173
9174 #endif /* HAVE_WINDOW_SYSTEM */
9175
9176
9177 \f
9178 /************************************************************************
9179 Horizontal scrolling
9180 ************************************************************************/
9181
9182 static int hscroll_window_tree P_ ((Lisp_Object));
9183 static int hscroll_windows P_ ((Lisp_Object));
9184
9185 /* For all leaf windows in the window tree rooted at WINDOW, set their
9186 hscroll value so that PT is (i) visible in the window, and (ii) so
9187 that it is not within a certain margin at the window's left and
9188 right border. Value is non-zero if any window's hscroll has been
9189 changed. */
9190
9191 static int
9192 hscroll_window_tree (window)
9193 Lisp_Object window;
9194 {
9195 int hscrolled_p = 0;
9196 int hscroll_relative_p = FLOATP (Vhscroll_step);
9197 int hscroll_step_abs = 0;
9198 double hscroll_step_rel = 0;
9199
9200 if (hscroll_relative_p)
9201 {
9202 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9203 if (hscroll_step_rel < 0)
9204 {
9205 hscroll_relative_p = 0;
9206 hscroll_step_abs = 0;
9207 }
9208 }
9209 else if (INTEGERP (Vhscroll_step))
9210 {
9211 hscroll_step_abs = XINT (Vhscroll_step);
9212 if (hscroll_step_abs < 0)
9213 hscroll_step_abs = 0;
9214 }
9215 else
9216 hscroll_step_abs = 0;
9217
9218 while (WINDOWP (window))
9219 {
9220 struct window *w = XWINDOW (window);
9221
9222 if (WINDOWP (w->hchild))
9223 hscrolled_p |= hscroll_window_tree (w->hchild);
9224 else if (WINDOWP (w->vchild))
9225 hscrolled_p |= hscroll_window_tree (w->vchild);
9226 else if (w->cursor.vpos >= 0)
9227 {
9228 int h_margin;
9229 int text_area_width;
9230 struct glyph_row *current_cursor_row
9231 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9232 struct glyph_row *desired_cursor_row
9233 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9234 struct glyph_row *cursor_row
9235 = (desired_cursor_row->enabled_p
9236 ? desired_cursor_row
9237 : current_cursor_row);
9238
9239 text_area_width = window_box_width (w, TEXT_AREA);
9240
9241 /* Scroll when cursor is inside this scroll margin. */
9242 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9243
9244 if ((XFASTINT (w->hscroll)
9245 && w->cursor.x <= h_margin)
9246 || (cursor_row->enabled_p
9247 && cursor_row->truncated_on_right_p
9248 && (w->cursor.x >= text_area_width - h_margin)))
9249 {
9250 struct it it;
9251 int hscroll;
9252 struct buffer *saved_current_buffer;
9253 int pt;
9254 int wanted_x;
9255
9256 /* Find point in a display of infinite width. */
9257 saved_current_buffer = current_buffer;
9258 current_buffer = XBUFFER (w->buffer);
9259
9260 if (w == XWINDOW (selected_window))
9261 pt = BUF_PT (current_buffer);
9262 else
9263 {
9264 pt = marker_position (w->pointm);
9265 pt = max (BEGV, pt);
9266 pt = min (ZV, pt);
9267 }
9268
9269 /* Move iterator to pt starting at cursor_row->start in
9270 a line with infinite width. */
9271 init_to_row_start (&it, w, cursor_row);
9272 it.last_visible_x = INFINITY;
9273 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9274 current_buffer = saved_current_buffer;
9275
9276 /* Position cursor in window. */
9277 if (!hscroll_relative_p && hscroll_step_abs == 0)
9278 hscroll = max (0, (it.current_x
9279 - (ITERATOR_AT_END_OF_LINE_P (&it)
9280 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9281 : (text_area_width / 2))))
9282 / FRAME_COLUMN_WIDTH (it.f);
9283 else if (w->cursor.x >= text_area_width - h_margin)
9284 {
9285 if (hscroll_relative_p)
9286 wanted_x = text_area_width * (1 - hscroll_step_rel)
9287 - h_margin;
9288 else
9289 wanted_x = text_area_width
9290 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9291 - h_margin;
9292 hscroll
9293 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9294 }
9295 else
9296 {
9297 if (hscroll_relative_p)
9298 wanted_x = text_area_width * hscroll_step_rel
9299 + h_margin;
9300 else
9301 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9302 + h_margin;
9303 hscroll
9304 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9305 }
9306 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9307
9308 /* Don't call Fset_window_hscroll if value hasn't
9309 changed because it will prevent redisplay
9310 optimizations. */
9311 if (XFASTINT (w->hscroll) != hscroll)
9312 {
9313 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9314 w->hscroll = make_number (hscroll);
9315 hscrolled_p = 1;
9316 }
9317 }
9318 }
9319
9320 window = w->next;
9321 }
9322
9323 /* Value is non-zero if hscroll of any leaf window has been changed. */
9324 return hscrolled_p;
9325 }
9326
9327
9328 /* Set hscroll so that cursor is visible and not inside horizontal
9329 scroll margins for all windows in the tree rooted at WINDOW. See
9330 also hscroll_window_tree above. Value is non-zero if any window's
9331 hscroll has been changed. If it has, desired matrices on the frame
9332 of WINDOW are cleared. */
9333
9334 static int
9335 hscroll_windows (window)
9336 Lisp_Object window;
9337 {
9338 int hscrolled_p;
9339
9340 if (automatic_hscrolling_p)
9341 {
9342 hscrolled_p = hscroll_window_tree (window);
9343 if (hscrolled_p)
9344 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9345 }
9346 else
9347 hscrolled_p = 0;
9348 return hscrolled_p;
9349 }
9350
9351
9352 \f
9353 /************************************************************************
9354 Redisplay
9355 ************************************************************************/
9356
9357 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9358 to a non-zero value. This is sometimes handy to have in a debugger
9359 session. */
9360
9361 #if GLYPH_DEBUG
9362
9363 /* First and last unchanged row for try_window_id. */
9364
9365 int debug_first_unchanged_at_end_vpos;
9366 int debug_last_unchanged_at_beg_vpos;
9367
9368 /* Delta vpos and y. */
9369
9370 int debug_dvpos, debug_dy;
9371
9372 /* Delta in characters and bytes for try_window_id. */
9373
9374 int debug_delta, debug_delta_bytes;
9375
9376 /* Values of window_end_pos and window_end_vpos at the end of
9377 try_window_id. */
9378
9379 EMACS_INT debug_end_pos, debug_end_vpos;
9380
9381 /* Append a string to W->desired_matrix->method. FMT is a printf
9382 format string. A1...A9 are a supplement for a variable-length
9383 argument list. If trace_redisplay_p is non-zero also printf the
9384 resulting string to stderr. */
9385
9386 static void
9387 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9388 struct window *w;
9389 char *fmt;
9390 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9391 {
9392 char buffer[512];
9393 char *method = w->desired_matrix->method;
9394 int len = strlen (method);
9395 int size = sizeof w->desired_matrix->method;
9396 int remaining = size - len - 1;
9397
9398 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9399 if (len && remaining)
9400 {
9401 method[len] = '|';
9402 --remaining, ++len;
9403 }
9404
9405 strncpy (method + len, buffer, remaining);
9406
9407 if (trace_redisplay_p)
9408 fprintf (stderr, "%p (%s): %s\n",
9409 w,
9410 ((BUFFERP (w->buffer)
9411 && STRINGP (XBUFFER (w->buffer)->name))
9412 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9413 : "no buffer"),
9414 buffer);
9415 }
9416
9417 #endif /* GLYPH_DEBUG */
9418
9419
9420 /* Value is non-zero if all changes in window W, which displays
9421 current_buffer, are in the text between START and END. START is a
9422 buffer position, END is given as a distance from Z. Used in
9423 redisplay_internal for display optimization. */
9424
9425 static INLINE int
9426 text_outside_line_unchanged_p (w, start, end)
9427 struct window *w;
9428 int start, end;
9429 {
9430 int unchanged_p = 1;
9431
9432 /* If text or overlays have changed, see where. */
9433 if (XFASTINT (w->last_modified) < MODIFF
9434 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9435 {
9436 /* Gap in the line? */
9437 if (GPT < start || Z - GPT < end)
9438 unchanged_p = 0;
9439
9440 /* Changes start in front of the line, or end after it? */
9441 if (unchanged_p
9442 && (BEG_UNCHANGED < start - 1
9443 || END_UNCHANGED < end))
9444 unchanged_p = 0;
9445
9446 /* If selective display, can't optimize if changes start at the
9447 beginning of the line. */
9448 if (unchanged_p
9449 && INTEGERP (current_buffer->selective_display)
9450 && XINT (current_buffer->selective_display) > 0
9451 && (BEG_UNCHANGED < start || GPT <= start))
9452 unchanged_p = 0;
9453
9454 /* If there are overlays at the start or end of the line, these
9455 may have overlay strings with newlines in them. A change at
9456 START, for instance, may actually concern the display of such
9457 overlay strings as well, and they are displayed on different
9458 lines. So, quickly rule out this case. (For the future, it
9459 might be desirable to implement something more telling than
9460 just BEG/END_UNCHANGED.) */
9461 if (unchanged_p)
9462 {
9463 if (BEG + BEG_UNCHANGED == start
9464 && overlay_touches_p (start))
9465 unchanged_p = 0;
9466 if (END_UNCHANGED == end
9467 && overlay_touches_p (Z - end))
9468 unchanged_p = 0;
9469 }
9470 }
9471
9472 return unchanged_p;
9473 }
9474
9475
9476 /* Do a frame update, taking possible shortcuts into account. This is
9477 the main external entry point for redisplay.
9478
9479 If the last redisplay displayed an echo area message and that message
9480 is no longer requested, we clear the echo area or bring back the
9481 mini-buffer if that is in use. */
9482
9483 void
9484 redisplay ()
9485 {
9486 redisplay_internal (0);
9487 }
9488
9489
9490 static Lisp_Object
9491 overlay_arrow_string_or_property (var, pbitmap)
9492 Lisp_Object var;
9493 int *pbitmap;
9494 {
9495 Lisp_Object pstr = Fget (var, Qoverlay_arrow_string);
9496 Lisp_Object bitmap;
9497
9498 if (pbitmap)
9499 {
9500 *pbitmap = 0;
9501 if (bitmap = Fget (var, Qoverlay_arrow_bitmap), INTEGERP (bitmap))
9502 *pbitmap = XINT (bitmap);
9503 }
9504
9505 if (!NILP (pstr))
9506 return pstr;
9507 return Voverlay_arrow_string;
9508 }
9509
9510 /* Return 1 if there are any overlay-arrows in current_buffer. */
9511 static int
9512 overlay_arrow_in_current_buffer_p ()
9513 {
9514 Lisp_Object vlist;
9515
9516 for (vlist = Voverlay_arrow_variable_list;
9517 CONSP (vlist);
9518 vlist = XCDR (vlist))
9519 {
9520 Lisp_Object var = XCAR (vlist);
9521 Lisp_Object val;
9522
9523 if (!SYMBOLP (var))
9524 continue;
9525 val = find_symbol_value (var);
9526 if (MARKERP (val)
9527 && current_buffer == XMARKER (val)->buffer)
9528 return 1;
9529 }
9530 return 0;
9531 }
9532
9533
9534 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9535 has changed. */
9536
9537 static int
9538 overlay_arrows_changed_p ()
9539 {
9540 Lisp_Object vlist;
9541
9542 for (vlist = Voverlay_arrow_variable_list;
9543 CONSP (vlist);
9544 vlist = XCDR (vlist))
9545 {
9546 Lisp_Object var = XCAR (vlist);
9547 Lisp_Object val, pstr;
9548
9549 if (!SYMBOLP (var))
9550 continue;
9551 val = find_symbol_value (var);
9552 if (!MARKERP (val))
9553 continue;
9554 if (! EQ (COERCE_MARKER (val),
9555 Fget (var, Qlast_arrow_position))
9556 || ! (pstr = overlay_arrow_string_or_property (var, 0),
9557 EQ (pstr, Fget (var, Qlast_arrow_string))))
9558 return 1;
9559 }
9560 return 0;
9561 }
9562
9563 /* Mark overlay arrows to be updated on next redisplay. */
9564
9565 static void
9566 update_overlay_arrows (up_to_date)
9567 int up_to_date;
9568 {
9569 Lisp_Object vlist;
9570
9571 for (vlist = Voverlay_arrow_variable_list;
9572 CONSP (vlist);
9573 vlist = XCDR (vlist))
9574 {
9575 Lisp_Object var = XCAR (vlist);
9576
9577 if (!SYMBOLP (var))
9578 continue;
9579
9580 if (up_to_date > 0)
9581 {
9582 Lisp_Object val = find_symbol_value (var);
9583 Fput (var, Qlast_arrow_position,
9584 COERCE_MARKER (val));
9585 Fput (var, Qlast_arrow_string,
9586 overlay_arrow_string_or_property (var, 0));
9587 }
9588 else if (up_to_date < 0
9589 || !NILP (Fget (var, Qlast_arrow_position)))
9590 {
9591 Fput (var, Qlast_arrow_position, Qt);
9592 Fput (var, Qlast_arrow_string, Qt);
9593 }
9594 }
9595 }
9596
9597
9598 /* Return overlay arrow string to display at row.
9599 Return t if display as bitmap in left fringe.
9600 Return nil if no overlay arrow. */
9601
9602 static Lisp_Object
9603 overlay_arrow_at_row (it, row, pbitmap)
9604 struct it *it;
9605 struct glyph_row *row;
9606 int *pbitmap;
9607 {
9608 Lisp_Object vlist;
9609
9610 for (vlist = Voverlay_arrow_variable_list;
9611 CONSP (vlist);
9612 vlist = XCDR (vlist))
9613 {
9614 Lisp_Object var = XCAR (vlist);
9615 Lisp_Object val;
9616
9617 if (!SYMBOLP (var))
9618 continue;
9619
9620 val = find_symbol_value (var);
9621
9622 if (MARKERP (val)
9623 && current_buffer == XMARKER (val)->buffer
9624 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
9625 {
9626 val = overlay_arrow_string_or_property (var, pbitmap);
9627 if (FRAME_WINDOW_P (it->f)
9628 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
9629 return Qt;
9630 if (STRINGP (val))
9631 return val;
9632 break;
9633 }
9634 }
9635
9636 *pbitmap = 0;
9637 return Qnil;
9638 }
9639
9640 /* Return 1 if point moved out of or into a composition. Otherwise
9641 return 0. PREV_BUF and PREV_PT are the last point buffer and
9642 position. BUF and PT are the current point buffer and position. */
9643
9644 int
9645 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9646 struct buffer *prev_buf, *buf;
9647 int prev_pt, pt;
9648 {
9649 int start, end;
9650 Lisp_Object prop;
9651 Lisp_Object buffer;
9652
9653 XSETBUFFER (buffer, buf);
9654 /* Check a composition at the last point if point moved within the
9655 same buffer. */
9656 if (prev_buf == buf)
9657 {
9658 if (prev_pt == pt)
9659 /* Point didn't move. */
9660 return 0;
9661
9662 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9663 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9664 && COMPOSITION_VALID_P (start, end, prop)
9665 && start < prev_pt && end > prev_pt)
9666 /* The last point was within the composition. Return 1 iff
9667 point moved out of the composition. */
9668 return (pt <= start || pt >= end);
9669 }
9670
9671 /* Check a composition at the current point. */
9672 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9673 && find_composition (pt, -1, &start, &end, &prop, buffer)
9674 && COMPOSITION_VALID_P (start, end, prop)
9675 && start < pt && end > pt);
9676 }
9677
9678
9679 /* Reconsider the setting of B->clip_changed which is displayed
9680 in window W. */
9681
9682 static INLINE void
9683 reconsider_clip_changes (w, b)
9684 struct window *w;
9685 struct buffer *b;
9686 {
9687 if (b->clip_changed
9688 && !NILP (w->window_end_valid)
9689 && w->current_matrix->buffer == b
9690 && w->current_matrix->zv == BUF_ZV (b)
9691 && w->current_matrix->begv == BUF_BEGV (b))
9692 b->clip_changed = 0;
9693
9694 /* If display wasn't paused, and W is not a tool bar window, see if
9695 point has been moved into or out of a composition. In that case,
9696 we set b->clip_changed to 1 to force updating the screen. If
9697 b->clip_changed has already been set to 1, we can skip this
9698 check. */
9699 if (!b->clip_changed
9700 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9701 {
9702 int pt;
9703
9704 if (w == XWINDOW (selected_window))
9705 pt = BUF_PT (current_buffer);
9706 else
9707 pt = marker_position (w->pointm);
9708
9709 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
9710 || pt != XINT (w->last_point))
9711 && check_point_in_composition (w->current_matrix->buffer,
9712 XINT (w->last_point),
9713 XBUFFER (w->buffer), pt))
9714 b->clip_changed = 1;
9715 }
9716 }
9717 \f
9718
9719 /* Select FRAME to forward the values of frame-local variables into C
9720 variables so that the redisplay routines can access those values
9721 directly. */
9722
9723 static void
9724 select_frame_for_redisplay (frame)
9725 Lisp_Object frame;
9726 {
9727 Lisp_Object tail, sym, val;
9728 Lisp_Object old = selected_frame;
9729
9730 selected_frame = frame;
9731
9732 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
9733 if (CONSP (XCAR (tail))
9734 && (sym = XCAR (XCAR (tail)),
9735 SYMBOLP (sym))
9736 && (sym = indirect_variable (sym),
9737 val = SYMBOL_VALUE (sym),
9738 (BUFFER_LOCAL_VALUEP (val)
9739 || SOME_BUFFER_LOCAL_VALUEP (val)))
9740 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9741 Fsymbol_value (sym);
9742
9743 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
9744 if (CONSP (XCAR (tail))
9745 && (sym = XCAR (XCAR (tail)),
9746 SYMBOLP (sym))
9747 && (sym = indirect_variable (sym),
9748 val = SYMBOL_VALUE (sym),
9749 (BUFFER_LOCAL_VALUEP (val)
9750 || SOME_BUFFER_LOCAL_VALUEP (val)))
9751 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9752 Fsymbol_value (sym);
9753 }
9754
9755
9756 #define STOP_POLLING \
9757 do { if (! polling_stopped_here) stop_polling (); \
9758 polling_stopped_here = 1; } while (0)
9759
9760 #define RESUME_POLLING \
9761 do { if (polling_stopped_here) start_polling (); \
9762 polling_stopped_here = 0; } while (0)
9763
9764
9765 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9766 response to any user action; therefore, we should preserve the echo
9767 area. (Actually, our caller does that job.) Perhaps in the future
9768 avoid recentering windows if it is not necessary; currently that
9769 causes some problems. */
9770
9771 static void
9772 redisplay_internal (preserve_echo_area)
9773 int preserve_echo_area;
9774 {
9775 struct window *w = XWINDOW (selected_window);
9776 struct frame *f = XFRAME (w->frame);
9777 int pause;
9778 int must_finish = 0;
9779 struct text_pos tlbufpos, tlendpos;
9780 int number_of_visible_frames;
9781 int count;
9782 struct frame *sf = SELECTED_FRAME ();
9783 int polling_stopped_here = 0;
9784
9785 /* Non-zero means redisplay has to consider all windows on all
9786 frames. Zero means, only selected_window is considered. */
9787 int consider_all_windows_p;
9788
9789 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9790
9791 /* No redisplay if running in batch mode or frame is not yet fully
9792 initialized, or redisplay is explicitly turned off by setting
9793 Vinhibit_redisplay. */
9794 if (noninteractive
9795 || !NILP (Vinhibit_redisplay)
9796 || !f->glyphs_initialized_p)
9797 return;
9798
9799 /* The flag redisplay_performed_directly_p is set by
9800 direct_output_for_insert when it already did the whole screen
9801 update necessary. */
9802 if (redisplay_performed_directly_p)
9803 {
9804 redisplay_performed_directly_p = 0;
9805 if (!hscroll_windows (selected_window))
9806 return;
9807 }
9808
9809 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9810 if (popup_activated ())
9811 return;
9812 #endif
9813
9814 /* I don't think this happens but let's be paranoid. */
9815 if (redisplaying_p)
9816 return;
9817
9818 /* Record a function that resets redisplaying_p to its old value
9819 when we leave this function. */
9820 count = SPECPDL_INDEX ();
9821 record_unwind_protect (unwind_redisplay,
9822 Fcons (make_number (redisplaying_p), selected_frame));
9823 ++redisplaying_p;
9824 specbind (Qinhibit_free_realized_faces, Qnil);
9825
9826 retry:
9827 pause = 0;
9828 reconsider_clip_changes (w, current_buffer);
9829
9830 /* If new fonts have been loaded that make a glyph matrix adjustment
9831 necessary, do it. */
9832 if (fonts_changed_p)
9833 {
9834 adjust_glyphs (NULL);
9835 ++windows_or_buffers_changed;
9836 fonts_changed_p = 0;
9837 }
9838
9839 /* If face_change_count is non-zero, init_iterator will free all
9840 realized faces, which includes the faces referenced from current
9841 matrices. So, we can't reuse current matrices in this case. */
9842 if (face_change_count)
9843 ++windows_or_buffers_changed;
9844
9845 if (! FRAME_WINDOW_P (sf)
9846 && previous_terminal_frame != sf)
9847 {
9848 /* Since frames on an ASCII terminal share the same display
9849 area, displaying a different frame means redisplay the whole
9850 thing. */
9851 windows_or_buffers_changed++;
9852 SET_FRAME_GARBAGED (sf);
9853 XSETFRAME (Vterminal_frame, sf);
9854 }
9855 previous_terminal_frame = sf;
9856
9857 /* Set the visible flags for all frames. Do this before checking
9858 for resized or garbaged frames; they want to know if their frames
9859 are visible. See the comment in frame.h for
9860 FRAME_SAMPLE_VISIBILITY. */
9861 {
9862 Lisp_Object tail, frame;
9863
9864 number_of_visible_frames = 0;
9865
9866 FOR_EACH_FRAME (tail, frame)
9867 {
9868 struct frame *f = XFRAME (frame);
9869
9870 FRAME_SAMPLE_VISIBILITY (f);
9871 if (FRAME_VISIBLE_P (f))
9872 ++number_of_visible_frames;
9873 clear_desired_matrices (f);
9874 }
9875 }
9876
9877 /* Notice any pending interrupt request to change frame size. */
9878 do_pending_window_change (1);
9879
9880 /* Clear frames marked as garbaged. */
9881 if (frame_garbaged)
9882 clear_garbaged_frames ();
9883
9884 /* Build menubar and tool-bar items. */
9885 prepare_menu_bars ();
9886
9887 if (windows_or_buffers_changed)
9888 update_mode_lines++;
9889
9890 /* Detect case that we need to write or remove a star in the mode line. */
9891 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
9892 {
9893 w->update_mode_line = Qt;
9894 if (buffer_shared > 1)
9895 update_mode_lines++;
9896 }
9897
9898 /* If %c is in the mode line, update it if needed. */
9899 if (!NILP (w->column_number_displayed)
9900 /* This alternative quickly identifies a common case
9901 where no change is needed. */
9902 && !(PT == XFASTINT (w->last_point)
9903 && XFASTINT (w->last_modified) >= MODIFF
9904 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9905 && (XFASTINT (w->column_number_displayed)
9906 != (int) current_column ())) /* iftc */
9907 w->update_mode_line = Qt;
9908
9909 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
9910
9911 /* The variable buffer_shared is set in redisplay_window and
9912 indicates that we redisplay a buffer in different windows. See
9913 there. */
9914 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
9915 || cursor_type_changed);
9916
9917 /* If specs for an arrow have changed, do thorough redisplay
9918 to ensure we remove any arrow that should no longer exist. */
9919 if (overlay_arrows_changed_p ())
9920 consider_all_windows_p = windows_or_buffers_changed = 1;
9921
9922 /* Normally the message* functions will have already displayed and
9923 updated the echo area, but the frame may have been trashed, or
9924 the update may have been preempted, so display the echo area
9925 again here. Checking message_cleared_p captures the case that
9926 the echo area should be cleared. */
9927 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
9928 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
9929 || (message_cleared_p
9930 && minibuf_level == 0
9931 /* If the mini-window is currently selected, this means the
9932 echo-area doesn't show through. */
9933 && !MINI_WINDOW_P (XWINDOW (selected_window))))
9934 {
9935 int window_height_changed_p = echo_area_display (0);
9936 must_finish = 1;
9937
9938 /* If we don't display the current message, don't clear the
9939 message_cleared_p flag, because, if we did, we wouldn't clear
9940 the echo area in the next redisplay which doesn't preserve
9941 the echo area. */
9942 if (!display_last_displayed_message_p)
9943 message_cleared_p = 0;
9944
9945 if (fonts_changed_p)
9946 goto retry;
9947 else if (window_height_changed_p)
9948 {
9949 consider_all_windows_p = 1;
9950 ++update_mode_lines;
9951 ++windows_or_buffers_changed;
9952
9953 /* If window configuration was changed, frames may have been
9954 marked garbaged. Clear them or we will experience
9955 surprises wrt scrolling. */
9956 if (frame_garbaged)
9957 clear_garbaged_frames ();
9958 }
9959 }
9960 else if (EQ (selected_window, minibuf_window)
9961 && (current_buffer->clip_changed
9962 || XFASTINT (w->last_modified) < MODIFF
9963 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9964 && resize_mini_window (w, 0))
9965 {
9966 /* Resized active mini-window to fit the size of what it is
9967 showing if its contents might have changed. */
9968 must_finish = 1;
9969 consider_all_windows_p = 1;
9970 ++windows_or_buffers_changed;
9971 ++update_mode_lines;
9972
9973 /* If window configuration was changed, frames may have been
9974 marked garbaged. Clear them or we will experience
9975 surprises wrt scrolling. */
9976 if (frame_garbaged)
9977 clear_garbaged_frames ();
9978 }
9979
9980
9981 /* If showing the region, and mark has changed, we must redisplay
9982 the whole window. The assignment to this_line_start_pos prevents
9983 the optimization directly below this if-statement. */
9984 if (((!NILP (Vtransient_mark_mode)
9985 && !NILP (XBUFFER (w->buffer)->mark_active))
9986 != !NILP (w->region_showing))
9987 || (!NILP (w->region_showing)
9988 && !EQ (w->region_showing,
9989 Fmarker_position (XBUFFER (w->buffer)->mark))))
9990 CHARPOS (this_line_start_pos) = 0;
9991
9992 /* Optimize the case that only the line containing the cursor in the
9993 selected window has changed. Variables starting with this_ are
9994 set in display_line and record information about the line
9995 containing the cursor. */
9996 tlbufpos = this_line_start_pos;
9997 tlendpos = this_line_end_pos;
9998 if (!consider_all_windows_p
9999 && CHARPOS (tlbufpos) > 0
10000 && NILP (w->update_mode_line)
10001 && !current_buffer->clip_changed
10002 && !current_buffer->prevent_redisplay_optimizations_p
10003 && FRAME_VISIBLE_P (XFRAME (w->frame))
10004 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10005 /* Make sure recorded data applies to current buffer, etc. */
10006 && this_line_buffer == current_buffer
10007 && current_buffer == XBUFFER (w->buffer)
10008 && NILP (w->force_start)
10009 && NILP (w->optional_new_start)
10010 /* Point must be on the line that we have info recorded about. */
10011 && PT >= CHARPOS (tlbufpos)
10012 && PT <= Z - CHARPOS (tlendpos)
10013 /* All text outside that line, including its final newline,
10014 must be unchanged */
10015 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10016 CHARPOS (tlendpos)))
10017 {
10018 if (CHARPOS (tlbufpos) > BEGV
10019 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10020 && (CHARPOS (tlbufpos) == ZV
10021 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10022 /* Former continuation line has disappeared by becoming empty */
10023 goto cancel;
10024 else if (XFASTINT (w->last_modified) < MODIFF
10025 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10026 || MINI_WINDOW_P (w))
10027 {
10028 /* We have to handle the case of continuation around a
10029 wide-column character (See the comment in indent.c around
10030 line 885).
10031
10032 For instance, in the following case:
10033
10034 -------- Insert --------
10035 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10036 J_I_ ==> J_I_ `^^' are cursors.
10037 ^^ ^^
10038 -------- --------
10039
10040 As we have to redraw the line above, we should goto cancel. */
10041
10042 struct it it;
10043 int line_height_before = this_line_pixel_height;
10044
10045 /* Note that start_display will handle the case that the
10046 line starting at tlbufpos is a continuation lines. */
10047 start_display (&it, w, tlbufpos);
10048
10049 /* Implementation note: It this still necessary? */
10050 if (it.current_x != this_line_start_x)
10051 goto cancel;
10052
10053 TRACE ((stderr, "trying display optimization 1\n"));
10054 w->cursor.vpos = -1;
10055 overlay_arrow_seen = 0;
10056 it.vpos = this_line_vpos;
10057 it.current_y = this_line_y;
10058 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10059 display_line (&it);
10060
10061 /* If line contains point, is not continued,
10062 and ends at same distance from eob as before, we win */
10063 if (w->cursor.vpos >= 0
10064 /* Line is not continued, otherwise this_line_start_pos
10065 would have been set to 0 in display_line. */
10066 && CHARPOS (this_line_start_pos)
10067 /* Line ends as before. */
10068 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10069 /* Line has same height as before. Otherwise other lines
10070 would have to be shifted up or down. */
10071 && this_line_pixel_height == line_height_before)
10072 {
10073 /* If this is not the window's last line, we must adjust
10074 the charstarts of the lines below. */
10075 if (it.current_y < it.last_visible_y)
10076 {
10077 struct glyph_row *row
10078 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10079 int delta, delta_bytes;
10080
10081 if (Z - CHARPOS (tlendpos) == ZV)
10082 {
10083 /* This line ends at end of (accessible part of)
10084 buffer. There is no newline to count. */
10085 delta = (Z
10086 - CHARPOS (tlendpos)
10087 - MATRIX_ROW_START_CHARPOS (row));
10088 delta_bytes = (Z_BYTE
10089 - BYTEPOS (tlendpos)
10090 - MATRIX_ROW_START_BYTEPOS (row));
10091 }
10092 else
10093 {
10094 /* This line ends in a newline. Must take
10095 account of the newline and the rest of the
10096 text that follows. */
10097 delta = (Z
10098 - CHARPOS (tlendpos)
10099 - MATRIX_ROW_START_CHARPOS (row));
10100 delta_bytes = (Z_BYTE
10101 - BYTEPOS (tlendpos)
10102 - MATRIX_ROW_START_BYTEPOS (row));
10103 }
10104
10105 increment_matrix_positions (w->current_matrix,
10106 this_line_vpos + 1,
10107 w->current_matrix->nrows,
10108 delta, delta_bytes);
10109 }
10110
10111 /* If this row displays text now but previously didn't,
10112 or vice versa, w->window_end_vpos may have to be
10113 adjusted. */
10114 if ((it.glyph_row - 1)->displays_text_p)
10115 {
10116 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10117 XSETINT (w->window_end_vpos, this_line_vpos);
10118 }
10119 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10120 && this_line_vpos > 0)
10121 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10122 w->window_end_valid = Qnil;
10123
10124 /* Update hint: No need to try to scroll in update_window. */
10125 w->desired_matrix->no_scrolling_p = 1;
10126
10127 #if GLYPH_DEBUG
10128 *w->desired_matrix->method = 0;
10129 debug_method_add (w, "optimization 1");
10130 #endif
10131 #ifdef HAVE_WINDOW_SYSTEM
10132 update_window_fringes (w, 0);
10133 #endif
10134 goto update;
10135 }
10136 else
10137 goto cancel;
10138 }
10139 else if (/* Cursor position hasn't changed. */
10140 PT == XFASTINT (w->last_point)
10141 /* Make sure the cursor was last displayed
10142 in this window. Otherwise we have to reposition it. */
10143 && 0 <= w->cursor.vpos
10144 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10145 {
10146 if (!must_finish)
10147 {
10148 do_pending_window_change (1);
10149
10150 /* We used to always goto end_of_redisplay here, but this
10151 isn't enough if we have a blinking cursor. */
10152 if (w->cursor_off_p == w->last_cursor_off_p)
10153 goto end_of_redisplay;
10154 }
10155 goto update;
10156 }
10157 /* If highlighting the region, or if the cursor is in the echo area,
10158 then we can't just move the cursor. */
10159 else if (! (!NILP (Vtransient_mark_mode)
10160 && !NILP (current_buffer->mark_active))
10161 && (EQ (selected_window, current_buffer->last_selected_window)
10162 || highlight_nonselected_windows)
10163 && NILP (w->region_showing)
10164 && NILP (Vshow_trailing_whitespace)
10165 && !cursor_in_echo_area)
10166 {
10167 struct it it;
10168 struct glyph_row *row;
10169
10170 /* Skip from tlbufpos to PT and see where it is. Note that
10171 PT may be in invisible text. If so, we will end at the
10172 next visible position. */
10173 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10174 NULL, DEFAULT_FACE_ID);
10175 it.current_x = this_line_start_x;
10176 it.current_y = this_line_y;
10177 it.vpos = this_line_vpos;
10178
10179 /* The call to move_it_to stops in front of PT, but
10180 moves over before-strings. */
10181 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10182
10183 if (it.vpos == this_line_vpos
10184 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10185 row->enabled_p))
10186 {
10187 xassert (this_line_vpos == it.vpos);
10188 xassert (this_line_y == it.current_y);
10189 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10190 #if GLYPH_DEBUG
10191 *w->desired_matrix->method = 0;
10192 debug_method_add (w, "optimization 3");
10193 #endif
10194 goto update;
10195 }
10196 else
10197 goto cancel;
10198 }
10199
10200 cancel:
10201 /* Text changed drastically or point moved off of line. */
10202 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10203 }
10204
10205 CHARPOS (this_line_start_pos) = 0;
10206 consider_all_windows_p |= buffer_shared > 1;
10207 ++clear_face_cache_count;
10208
10209
10210 /* Build desired matrices, and update the display. If
10211 consider_all_windows_p is non-zero, do it for all windows on all
10212 frames. Otherwise do it for selected_window, only. */
10213
10214 if (consider_all_windows_p)
10215 {
10216 Lisp_Object tail, frame;
10217 int i, n = 0, size = 50;
10218 struct frame **updated
10219 = (struct frame **) alloca (size * sizeof *updated);
10220
10221 /* Clear the face cache eventually. */
10222 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10223 {
10224 clear_face_cache (0);
10225 clear_face_cache_count = 0;
10226 }
10227
10228 /* Recompute # windows showing selected buffer. This will be
10229 incremented each time such a window is displayed. */
10230 buffer_shared = 0;
10231
10232 FOR_EACH_FRAME (tail, frame)
10233 {
10234 struct frame *f = XFRAME (frame);
10235
10236 if (FRAME_WINDOW_P (f) || f == sf)
10237 {
10238 if (! EQ (frame, selected_frame))
10239 /* Select the frame, for the sake of frame-local
10240 variables. */
10241 select_frame_for_redisplay (frame);
10242
10243 #ifdef HAVE_WINDOW_SYSTEM
10244 if (clear_face_cache_count % 50 == 0
10245 && FRAME_WINDOW_P (f))
10246 clear_image_cache (f, 0);
10247 #endif /* HAVE_WINDOW_SYSTEM */
10248
10249 /* Mark all the scroll bars to be removed; we'll redeem
10250 the ones we want when we redisplay their windows. */
10251 if (condemn_scroll_bars_hook)
10252 condemn_scroll_bars_hook (f);
10253
10254 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10255 redisplay_windows (FRAME_ROOT_WINDOW (f));
10256
10257 /* Any scroll bars which redisplay_windows should have
10258 nuked should now go away. */
10259 if (judge_scroll_bars_hook)
10260 judge_scroll_bars_hook (f);
10261
10262 /* If fonts changed, display again. */
10263 /* ??? rms: I suspect it is a mistake to jump all the way
10264 back to retry here. It should just retry this frame. */
10265 if (fonts_changed_p)
10266 goto retry;
10267
10268 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10269 {
10270 /* See if we have to hscroll. */
10271 if (hscroll_windows (f->root_window))
10272 goto retry;
10273
10274 /* Prevent various kinds of signals during display
10275 update. stdio is not robust about handling
10276 signals, which can cause an apparent I/O
10277 error. */
10278 if (interrupt_input)
10279 unrequest_sigio ();
10280 STOP_POLLING;
10281
10282 /* Update the display. */
10283 set_window_update_flags (XWINDOW (f->root_window), 1);
10284 pause |= update_frame (f, 0, 0);
10285 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10286 if (pause)
10287 break;
10288 #endif
10289
10290 if (n == size)
10291 {
10292 int nbytes = size * sizeof *updated;
10293 struct frame **p = (struct frame **) alloca (2 * nbytes);
10294 bcopy (updated, p, nbytes);
10295 size *= 2;
10296 }
10297
10298 updated[n++] = f;
10299 }
10300 }
10301 }
10302
10303 if (!pause)
10304 {
10305 /* Do the mark_window_display_accurate after all windows have
10306 been redisplayed because this call resets flags in buffers
10307 which are needed for proper redisplay. */
10308 for (i = 0; i < n; ++i)
10309 {
10310 struct frame *f = updated[i];
10311 mark_window_display_accurate (f->root_window, 1);
10312 if (frame_up_to_date_hook)
10313 frame_up_to_date_hook (f);
10314 }
10315 }
10316 }
10317 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10318 {
10319 Lisp_Object mini_window;
10320 struct frame *mini_frame;
10321
10322 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10323 /* Use list_of_error, not Qerror, so that
10324 we catch only errors and don't run the debugger. */
10325 internal_condition_case_1 (redisplay_window_1, selected_window,
10326 list_of_error,
10327 redisplay_window_error);
10328
10329 /* Compare desired and current matrices, perform output. */
10330
10331 update:
10332 /* If fonts changed, display again. */
10333 if (fonts_changed_p)
10334 goto retry;
10335
10336 /* Prevent various kinds of signals during display update.
10337 stdio is not robust about handling signals,
10338 which can cause an apparent I/O error. */
10339 if (interrupt_input)
10340 unrequest_sigio ();
10341 STOP_POLLING;
10342
10343 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10344 {
10345 if (hscroll_windows (selected_window))
10346 goto retry;
10347
10348 XWINDOW (selected_window)->must_be_updated_p = 1;
10349 pause = update_frame (sf, 0, 0);
10350 }
10351
10352 /* We may have called echo_area_display at the top of this
10353 function. If the echo area is on another frame, that may
10354 have put text on a frame other than the selected one, so the
10355 above call to update_frame would not have caught it. Catch
10356 it here. */
10357 mini_window = FRAME_MINIBUF_WINDOW (sf);
10358 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10359
10360 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10361 {
10362 XWINDOW (mini_window)->must_be_updated_p = 1;
10363 pause |= update_frame (mini_frame, 0, 0);
10364 if (!pause && hscroll_windows (mini_window))
10365 goto retry;
10366 }
10367 }
10368
10369 /* If display was paused because of pending input, make sure we do a
10370 thorough update the next time. */
10371 if (pause)
10372 {
10373 /* Prevent the optimization at the beginning of
10374 redisplay_internal that tries a single-line update of the
10375 line containing the cursor in the selected window. */
10376 CHARPOS (this_line_start_pos) = 0;
10377
10378 /* Let the overlay arrow be updated the next time. */
10379 update_overlay_arrows (0);
10380
10381 /* If we pause after scrolling, some rows in the current
10382 matrices of some windows are not valid. */
10383 if (!WINDOW_FULL_WIDTH_P (w)
10384 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10385 update_mode_lines = 1;
10386 }
10387 else
10388 {
10389 if (!consider_all_windows_p)
10390 {
10391 /* This has already been done above if
10392 consider_all_windows_p is set. */
10393 mark_window_display_accurate_1 (w, 1);
10394
10395 /* Say overlay arrows are up to date. */
10396 update_overlay_arrows (1);
10397
10398 if (frame_up_to_date_hook != 0)
10399 frame_up_to_date_hook (sf);
10400 }
10401
10402 update_mode_lines = 0;
10403 windows_or_buffers_changed = 0;
10404 cursor_type_changed = 0;
10405 }
10406
10407 /* Start SIGIO interrupts coming again. Having them off during the
10408 code above makes it less likely one will discard output, but not
10409 impossible, since there might be stuff in the system buffer here.
10410 But it is much hairier to try to do anything about that. */
10411 if (interrupt_input)
10412 request_sigio ();
10413 RESUME_POLLING;
10414
10415 /* If a frame has become visible which was not before, redisplay
10416 again, so that we display it. Expose events for such a frame
10417 (which it gets when becoming visible) don't call the parts of
10418 redisplay constructing glyphs, so simply exposing a frame won't
10419 display anything in this case. So, we have to display these
10420 frames here explicitly. */
10421 if (!pause)
10422 {
10423 Lisp_Object tail, frame;
10424 int new_count = 0;
10425
10426 FOR_EACH_FRAME (tail, frame)
10427 {
10428 int this_is_visible = 0;
10429
10430 if (XFRAME (frame)->visible)
10431 this_is_visible = 1;
10432 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10433 if (XFRAME (frame)->visible)
10434 this_is_visible = 1;
10435
10436 if (this_is_visible)
10437 new_count++;
10438 }
10439
10440 if (new_count != number_of_visible_frames)
10441 windows_or_buffers_changed++;
10442 }
10443
10444 /* Change frame size now if a change is pending. */
10445 do_pending_window_change (1);
10446
10447 /* If we just did a pending size change, or have additional
10448 visible frames, redisplay again. */
10449 if (windows_or_buffers_changed && !pause)
10450 goto retry;
10451
10452 end_of_redisplay:
10453 unbind_to (count, Qnil);
10454 RESUME_POLLING;
10455 }
10456
10457
10458 /* Redisplay, but leave alone any recent echo area message unless
10459 another message has been requested in its place.
10460
10461 This is useful in situations where you need to redisplay but no
10462 user action has occurred, making it inappropriate for the message
10463 area to be cleared. See tracking_off and
10464 wait_reading_process_output for examples of these situations.
10465
10466 FROM_WHERE is an integer saying from where this function was
10467 called. This is useful for debugging. */
10468
10469 void
10470 redisplay_preserve_echo_area (from_where)
10471 int from_where;
10472 {
10473 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10474
10475 if (!NILP (echo_area_buffer[1]))
10476 {
10477 /* We have a previously displayed message, but no current
10478 message. Redisplay the previous message. */
10479 display_last_displayed_message_p = 1;
10480 redisplay_internal (1);
10481 display_last_displayed_message_p = 0;
10482 }
10483 else
10484 redisplay_internal (1);
10485
10486 if (rif != NULL && rif->flush_display_optional)
10487 rif->flush_display_optional (NULL);
10488 }
10489
10490
10491 /* Function registered with record_unwind_protect in
10492 redisplay_internal. Reset redisplaying_p to the value it had
10493 before redisplay_internal was called, and clear
10494 prevent_freeing_realized_faces_p. It also selects the previously
10495 selected frame. */
10496
10497 static Lisp_Object
10498 unwind_redisplay (val)
10499 Lisp_Object val;
10500 {
10501 Lisp_Object old_redisplaying_p, old_frame;
10502
10503 old_redisplaying_p = XCAR (val);
10504 redisplaying_p = XFASTINT (old_redisplaying_p);
10505 old_frame = XCDR (val);
10506 if (! EQ (old_frame, selected_frame))
10507 select_frame_for_redisplay (old_frame);
10508 return Qnil;
10509 }
10510
10511
10512 /* Mark the display of window W as accurate or inaccurate. If
10513 ACCURATE_P is non-zero mark display of W as accurate. If
10514 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10515 redisplay_internal is called. */
10516
10517 static void
10518 mark_window_display_accurate_1 (w, accurate_p)
10519 struct window *w;
10520 int accurate_p;
10521 {
10522 if (BUFFERP (w->buffer))
10523 {
10524 struct buffer *b = XBUFFER (w->buffer);
10525
10526 w->last_modified
10527 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10528 w->last_overlay_modified
10529 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10530 w->last_had_star
10531 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10532
10533 if (accurate_p)
10534 {
10535 b->clip_changed = 0;
10536 b->prevent_redisplay_optimizations_p = 0;
10537
10538 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10539 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10540 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10541 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10542
10543 w->current_matrix->buffer = b;
10544 w->current_matrix->begv = BUF_BEGV (b);
10545 w->current_matrix->zv = BUF_ZV (b);
10546
10547 w->last_cursor = w->cursor;
10548 w->last_cursor_off_p = w->cursor_off_p;
10549
10550 if (w == XWINDOW (selected_window))
10551 w->last_point = make_number (BUF_PT (b));
10552 else
10553 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10554 }
10555 }
10556
10557 if (accurate_p)
10558 {
10559 w->window_end_valid = w->buffer;
10560 #if 0 /* This is incorrect with variable-height lines. */
10561 xassert (XINT (w->window_end_vpos)
10562 < (WINDOW_TOTAL_LINES (w)
10563 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10564 #endif
10565 w->update_mode_line = Qnil;
10566 }
10567 }
10568
10569
10570 /* Mark the display of windows in the window tree rooted at WINDOW as
10571 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10572 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10573 be redisplayed the next time redisplay_internal is called. */
10574
10575 void
10576 mark_window_display_accurate (window, accurate_p)
10577 Lisp_Object window;
10578 int accurate_p;
10579 {
10580 struct window *w;
10581
10582 for (; !NILP (window); window = w->next)
10583 {
10584 w = XWINDOW (window);
10585 mark_window_display_accurate_1 (w, accurate_p);
10586
10587 if (!NILP (w->vchild))
10588 mark_window_display_accurate (w->vchild, accurate_p);
10589 if (!NILP (w->hchild))
10590 mark_window_display_accurate (w->hchild, accurate_p);
10591 }
10592
10593 if (accurate_p)
10594 {
10595 update_overlay_arrows (1);
10596 }
10597 else
10598 {
10599 /* Force a thorough redisplay the next time by setting
10600 last_arrow_position and last_arrow_string to t, which is
10601 unequal to any useful value of Voverlay_arrow_... */
10602 update_overlay_arrows (-1);
10603 }
10604 }
10605
10606
10607 /* Return value in display table DP (Lisp_Char_Table *) for character
10608 C. Since a display table doesn't have any parent, we don't have to
10609 follow parent. Do not call this function directly but use the
10610 macro DISP_CHAR_VECTOR. */
10611
10612 Lisp_Object
10613 disp_char_vector (dp, c)
10614 struct Lisp_Char_Table *dp;
10615 int c;
10616 {
10617 int code[4], i;
10618 Lisp_Object val;
10619
10620 if (SINGLE_BYTE_CHAR_P (c))
10621 return (dp->contents[c]);
10622
10623 SPLIT_CHAR (c, code[0], code[1], code[2]);
10624 if (code[1] < 32)
10625 code[1] = -1;
10626 else if (code[2] < 32)
10627 code[2] = -1;
10628
10629 /* Here, the possible range of code[0] (== charset ID) is
10630 128..max_charset. Since the top level char table contains data
10631 for multibyte characters after 256th element, we must increment
10632 code[0] by 128 to get a correct index. */
10633 code[0] += 128;
10634 code[3] = -1; /* anchor */
10635
10636 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10637 {
10638 val = dp->contents[code[i]];
10639 if (!SUB_CHAR_TABLE_P (val))
10640 return (NILP (val) ? dp->defalt : val);
10641 }
10642
10643 /* Here, val is a sub char table. We return the default value of
10644 it. */
10645 return (dp->defalt);
10646 }
10647
10648
10649 \f
10650 /***********************************************************************
10651 Window Redisplay
10652 ***********************************************************************/
10653
10654 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10655
10656 static void
10657 redisplay_windows (window)
10658 Lisp_Object window;
10659 {
10660 while (!NILP (window))
10661 {
10662 struct window *w = XWINDOW (window);
10663
10664 if (!NILP (w->hchild))
10665 redisplay_windows (w->hchild);
10666 else if (!NILP (w->vchild))
10667 redisplay_windows (w->vchild);
10668 else
10669 {
10670 displayed_buffer = XBUFFER (w->buffer);
10671 /* Use list_of_error, not Qerror, so that
10672 we catch only errors and don't run the debugger. */
10673 internal_condition_case_1 (redisplay_window_0, window,
10674 list_of_error,
10675 redisplay_window_error);
10676 }
10677
10678 window = w->next;
10679 }
10680 }
10681
10682 static Lisp_Object
10683 redisplay_window_error ()
10684 {
10685 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10686 return Qnil;
10687 }
10688
10689 static Lisp_Object
10690 redisplay_window_0 (window)
10691 Lisp_Object window;
10692 {
10693 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10694 redisplay_window (window, 0);
10695 return Qnil;
10696 }
10697
10698 static Lisp_Object
10699 redisplay_window_1 (window)
10700 Lisp_Object window;
10701 {
10702 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10703 redisplay_window (window, 1);
10704 return Qnil;
10705 }
10706 \f
10707
10708 /* Increment GLYPH until it reaches END or CONDITION fails while
10709 adding (GLYPH)->pixel_width to X. */
10710
10711 #define SKIP_GLYPHS(glyph, end, x, condition) \
10712 do \
10713 { \
10714 (x) += (glyph)->pixel_width; \
10715 ++(glyph); \
10716 } \
10717 while ((glyph) < (end) && (condition))
10718
10719
10720 /* Set cursor position of W. PT is assumed to be displayed in ROW.
10721 DELTA is the number of bytes by which positions recorded in ROW
10722 differ from current buffer positions. */
10723
10724 void
10725 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10726 struct window *w;
10727 struct glyph_row *row;
10728 struct glyph_matrix *matrix;
10729 int delta, delta_bytes, dy, dvpos;
10730 {
10731 struct glyph *glyph = row->glyphs[TEXT_AREA];
10732 struct glyph *end = glyph + row->used[TEXT_AREA];
10733 struct glyph *cursor = NULL;
10734 /* The first glyph that starts a sequence of glyphs from string. */
10735 struct glyph *string_start;
10736 /* The X coordinate of string_start. */
10737 int string_start_x;
10738 /* The last known character position. */
10739 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10740 /* The last known character position before string_start. */
10741 int string_before_pos;
10742 int x = row->x;
10743 int cursor_x = x;
10744 int cursor_from_overlay_pos = 0;
10745 int pt_old = PT - delta;
10746
10747 /* Skip over glyphs not having an object at the start of the row.
10748 These are special glyphs like truncation marks on terminal
10749 frames. */
10750 if (row->displays_text_p)
10751 while (glyph < end
10752 && INTEGERP (glyph->object)
10753 && glyph->charpos < 0)
10754 {
10755 x += glyph->pixel_width;
10756 ++glyph;
10757 }
10758
10759 string_start = NULL;
10760 while (glyph < end
10761 && !INTEGERP (glyph->object)
10762 && (!BUFFERP (glyph->object)
10763 || (last_pos = glyph->charpos) < pt_old))
10764 {
10765 if (! STRINGP (glyph->object))
10766 {
10767 string_start = NULL;
10768 x += glyph->pixel_width;
10769 ++glyph;
10770 if (cursor_from_overlay_pos
10771 && last_pos > cursor_from_overlay_pos)
10772 {
10773 cursor_from_overlay_pos = 0;
10774 cursor = 0;
10775 }
10776 }
10777 else
10778 {
10779 string_before_pos = last_pos;
10780 string_start = glyph;
10781 string_start_x = x;
10782 /* Skip all glyphs from string. */
10783 do
10784 {
10785 int pos;
10786 if ((cursor == NULL || glyph > cursor)
10787 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
10788 Qcursor, (glyph)->object))
10789 && (pos = string_buffer_position (w, glyph->object,
10790 string_before_pos),
10791 (pos == 0 /* From overlay */
10792 || pos == pt_old)))
10793 {
10794 /* Estimate overlay buffer position from the buffer
10795 positions of the glyphs before and after the overlay.
10796 Add 1 to last_pos so that if point corresponds to the
10797 glyph right after the overlay, we still use a 'cursor'
10798 property found in that overlay. */
10799 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
10800 cursor = glyph;
10801 cursor_x = x;
10802 }
10803 x += glyph->pixel_width;
10804 ++glyph;
10805 }
10806 while (glyph < end && STRINGP (glyph->object));
10807 }
10808 }
10809
10810 if (cursor != NULL)
10811 {
10812 glyph = cursor;
10813 x = cursor_x;
10814 }
10815 else if (string_start
10816 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
10817 {
10818 /* We may have skipped over point because the previous glyphs
10819 are from string. As there's no easy way to know the
10820 character position of the current glyph, find the correct
10821 glyph on point by scanning from string_start again. */
10822 Lisp_Object limit;
10823 Lisp_Object string;
10824 int pos;
10825
10826 limit = make_number (pt_old + 1);
10827 end = glyph;
10828 glyph = string_start;
10829 x = string_start_x;
10830 string = glyph->object;
10831 pos = string_buffer_position (w, string, string_before_pos);
10832 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
10833 because we always put cursor after overlay strings. */
10834 while (pos == 0 && glyph < end)
10835 {
10836 string = glyph->object;
10837 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10838 if (glyph < end)
10839 pos = string_buffer_position (w, glyph->object, string_before_pos);
10840 }
10841
10842 while (glyph < end)
10843 {
10844 pos = XINT (Fnext_single_char_property_change
10845 (make_number (pos), Qdisplay, Qnil, limit));
10846 if (pos > pt_old)
10847 break;
10848 /* Skip glyphs from the same string. */
10849 string = glyph->object;
10850 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10851 /* Skip glyphs from an overlay. */
10852 while (glyph < end
10853 && ! string_buffer_position (w, glyph->object, pos))
10854 {
10855 string = glyph->object;
10856 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10857 }
10858 }
10859 }
10860
10861 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
10862 w->cursor.x = x;
10863 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
10864 w->cursor.y = row->y + dy;
10865
10866 if (w == XWINDOW (selected_window))
10867 {
10868 if (!row->continued_p
10869 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
10870 && row->x == 0)
10871 {
10872 this_line_buffer = XBUFFER (w->buffer);
10873
10874 CHARPOS (this_line_start_pos)
10875 = MATRIX_ROW_START_CHARPOS (row) + delta;
10876 BYTEPOS (this_line_start_pos)
10877 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
10878
10879 CHARPOS (this_line_end_pos)
10880 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
10881 BYTEPOS (this_line_end_pos)
10882 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
10883
10884 this_line_y = w->cursor.y;
10885 this_line_pixel_height = row->height;
10886 this_line_vpos = w->cursor.vpos;
10887 this_line_start_x = row->x;
10888 }
10889 else
10890 CHARPOS (this_line_start_pos) = 0;
10891 }
10892 }
10893
10894
10895 /* Run window scroll functions, if any, for WINDOW with new window
10896 start STARTP. Sets the window start of WINDOW to that position.
10897
10898 We assume that the window's buffer is really current. */
10899
10900 static INLINE struct text_pos
10901 run_window_scroll_functions (window, startp)
10902 Lisp_Object window;
10903 struct text_pos startp;
10904 {
10905 struct window *w = XWINDOW (window);
10906 SET_MARKER_FROM_TEXT_POS (w->start, startp);
10907
10908 if (current_buffer != XBUFFER (w->buffer))
10909 abort ();
10910
10911 if (!NILP (Vwindow_scroll_functions))
10912 {
10913 run_hook_with_args_2 (Qwindow_scroll_functions, window,
10914 make_number (CHARPOS (startp)));
10915 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10916 /* In case the hook functions switch buffers. */
10917 if (current_buffer != XBUFFER (w->buffer))
10918 set_buffer_internal_1 (XBUFFER (w->buffer));
10919 }
10920
10921 return startp;
10922 }
10923
10924
10925 /* Make sure the line containing the cursor is fully visible.
10926 A value of 1 means there is nothing to be done.
10927 (Either the line is fully visible, or it cannot be made so,
10928 or we cannot tell.)
10929
10930 If FORCE_P is non-zero, return 0 even if partial visible cursor row
10931 is higher than window.
10932
10933 A value of 0 means the caller should do scrolling
10934 as if point had gone off the screen. */
10935
10936 static int
10937 make_cursor_line_fully_visible (w, force_p)
10938 struct window *w;
10939 int force_p;
10940 {
10941 struct glyph_matrix *matrix;
10942 struct glyph_row *row;
10943 int window_height;
10944
10945 if (!make_cursor_line_fully_visible_p)
10946 return 1;
10947
10948 /* It's not always possible to find the cursor, e.g, when a window
10949 is full of overlay strings. Don't do anything in that case. */
10950 if (w->cursor.vpos < 0)
10951 return 1;
10952
10953 matrix = w->desired_matrix;
10954 row = MATRIX_ROW (matrix, w->cursor.vpos);
10955
10956 /* If the cursor row is not partially visible, there's nothing to do. */
10957 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
10958 return 1;
10959
10960 /* If the row the cursor is in is taller than the window's height,
10961 it's not clear what to do, so do nothing. */
10962 window_height = window_box_height (w);
10963 if (row->height >= window_height)
10964 {
10965 if (!force_p || w->vscroll)
10966 return 1;
10967 }
10968 return 0;
10969
10970 #if 0
10971 /* This code used to try to scroll the window just enough to make
10972 the line visible. It returned 0 to say that the caller should
10973 allocate larger glyph matrices. */
10974
10975 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
10976 {
10977 int dy = row->height - row->visible_height;
10978 w->vscroll = 0;
10979 w->cursor.y += dy;
10980 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10981 }
10982 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
10983 {
10984 int dy = - (row->height - row->visible_height);
10985 w->vscroll = dy;
10986 w->cursor.y += dy;
10987 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10988 }
10989
10990 /* When we change the cursor y-position of the selected window,
10991 change this_line_y as well so that the display optimization for
10992 the cursor line of the selected window in redisplay_internal uses
10993 the correct y-position. */
10994 if (w == XWINDOW (selected_window))
10995 this_line_y = w->cursor.y;
10996
10997 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
10998 redisplay with larger matrices. */
10999 if (matrix->nrows < required_matrix_height (w))
11000 {
11001 fonts_changed_p = 1;
11002 return 0;
11003 }
11004
11005 return 1;
11006 #endif /* 0 */
11007 }
11008
11009
11010 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11011 non-zero means only WINDOW is redisplayed in redisplay_internal.
11012 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11013 in redisplay_window to bring a partially visible line into view in
11014 the case that only the cursor has moved.
11015
11016 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11017 last screen line's vertical height extends past the end of the screen.
11018
11019 Value is
11020
11021 1 if scrolling succeeded
11022
11023 0 if scrolling didn't find point.
11024
11025 -1 if new fonts have been loaded so that we must interrupt
11026 redisplay, adjust glyph matrices, and try again. */
11027
11028 enum
11029 {
11030 SCROLLING_SUCCESS,
11031 SCROLLING_FAILED,
11032 SCROLLING_NEED_LARGER_MATRICES
11033 };
11034
11035 static int
11036 try_scrolling (window, just_this_one_p, scroll_conservatively,
11037 scroll_step, temp_scroll_step, last_line_misfit)
11038 Lisp_Object window;
11039 int just_this_one_p;
11040 EMACS_INT scroll_conservatively, scroll_step;
11041 int temp_scroll_step;
11042 int last_line_misfit;
11043 {
11044 struct window *w = XWINDOW (window);
11045 struct frame *f = XFRAME (w->frame);
11046 struct text_pos scroll_margin_pos;
11047 struct text_pos pos;
11048 struct text_pos startp;
11049 struct it it;
11050 Lisp_Object window_end;
11051 int this_scroll_margin;
11052 int dy = 0;
11053 int scroll_max;
11054 int rc;
11055 int amount_to_scroll = 0;
11056 Lisp_Object aggressive;
11057 int height;
11058 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11059
11060 #if GLYPH_DEBUG
11061 debug_method_add (w, "try_scrolling");
11062 #endif
11063
11064 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11065
11066 /* Compute scroll margin height in pixels. We scroll when point is
11067 within this distance from the top or bottom of the window. */
11068 if (scroll_margin > 0)
11069 {
11070 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11071 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11072 }
11073 else
11074 this_scroll_margin = 0;
11075
11076 /* Force scroll_conservatively to have a reasonable value so it doesn't
11077 cause an overflow while computing how much to scroll. */
11078 if (scroll_conservatively)
11079 scroll_conservatively = min (scroll_conservatively,
11080 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11081
11082 /* Compute how much we should try to scroll maximally to bring point
11083 into view. */
11084 if (scroll_step || scroll_conservatively || temp_scroll_step)
11085 scroll_max = max (scroll_step,
11086 max (scroll_conservatively, temp_scroll_step));
11087 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11088 || NUMBERP (current_buffer->scroll_up_aggressively))
11089 /* We're trying to scroll because of aggressive scrolling
11090 but no scroll_step is set. Choose an arbitrary one. Maybe
11091 there should be a variable for this. */
11092 scroll_max = 10;
11093 else
11094 scroll_max = 0;
11095 scroll_max *= FRAME_LINE_HEIGHT (f);
11096
11097 /* Decide whether we have to scroll down. Start at the window end
11098 and move this_scroll_margin up to find the position of the scroll
11099 margin. */
11100 window_end = Fwindow_end (window, Qt);
11101
11102 too_near_end:
11103
11104 CHARPOS (scroll_margin_pos) = XINT (window_end);
11105 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11106
11107 if (this_scroll_margin || extra_scroll_margin_lines)
11108 {
11109 start_display (&it, w, scroll_margin_pos);
11110 if (this_scroll_margin)
11111 move_it_vertically_backward (&it, this_scroll_margin);
11112 if (extra_scroll_margin_lines)
11113 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11114 scroll_margin_pos = it.current.pos;
11115 }
11116
11117 if (PT >= CHARPOS (scroll_margin_pos))
11118 {
11119 int y0;
11120
11121 /* Point is in the scroll margin at the bottom of the window, or
11122 below. Compute a new window start that makes point visible. */
11123
11124 /* Compute the distance from the scroll margin to PT.
11125 Give up if the distance is greater than scroll_max. */
11126 start_display (&it, w, scroll_margin_pos);
11127 y0 = it.current_y;
11128 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11129 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11130
11131 /* To make point visible, we have to move the window start
11132 down so that the line the cursor is in is visible, which
11133 means we have to add in the height of the cursor line. */
11134 dy = line_bottom_y (&it) - y0;
11135
11136 if (dy > scroll_max)
11137 return SCROLLING_FAILED;
11138
11139 /* Move the window start down. If scrolling conservatively,
11140 move it just enough down to make point visible. If
11141 scroll_step is set, move it down by scroll_step. */
11142 start_display (&it, w, startp);
11143
11144 if (scroll_conservatively)
11145 /* Set AMOUNT_TO_SCROLL to at least one line,
11146 and at most scroll_conservatively lines. */
11147 amount_to_scroll
11148 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11149 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11150 else if (scroll_step || temp_scroll_step)
11151 amount_to_scroll = scroll_max;
11152 else
11153 {
11154 aggressive = current_buffer->scroll_up_aggressively;
11155 height = WINDOW_BOX_TEXT_HEIGHT (w);
11156 if (NUMBERP (aggressive))
11157 {
11158 double float_amount = XFLOATINT (aggressive) * height;
11159 amount_to_scroll = float_amount;
11160 if (amount_to_scroll == 0 && float_amount > 0)
11161 amount_to_scroll = 1;
11162 }
11163 }
11164
11165 if (amount_to_scroll <= 0)
11166 return SCROLLING_FAILED;
11167
11168 /* If moving by amount_to_scroll leaves STARTP unchanged,
11169 move it down one screen line. */
11170
11171 move_it_vertically (&it, amount_to_scroll);
11172 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11173 move_it_by_lines (&it, 1, 1);
11174 startp = it.current.pos;
11175 }
11176 else
11177 {
11178 /* See if point is inside the scroll margin at the top of the
11179 window. */
11180 scroll_margin_pos = startp;
11181 if (this_scroll_margin)
11182 {
11183 start_display (&it, w, startp);
11184 move_it_vertically (&it, this_scroll_margin);
11185 scroll_margin_pos = it.current.pos;
11186 }
11187
11188 if (PT < CHARPOS (scroll_margin_pos))
11189 {
11190 /* Point is in the scroll margin at the top of the window or
11191 above what is displayed in the window. */
11192 int y0;
11193
11194 /* Compute the vertical distance from PT to the scroll
11195 margin position. Give up if distance is greater than
11196 scroll_max. */
11197 SET_TEXT_POS (pos, PT, PT_BYTE);
11198 start_display (&it, w, pos);
11199 y0 = it.current_y;
11200 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11201 it.last_visible_y, -1,
11202 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11203 dy = it.current_y - y0;
11204 if (dy > scroll_max)
11205 return SCROLLING_FAILED;
11206
11207 /* Compute new window start. */
11208 start_display (&it, w, startp);
11209
11210 if (scroll_conservatively)
11211 amount_to_scroll
11212 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11213 else if (scroll_step || temp_scroll_step)
11214 amount_to_scroll = scroll_max;
11215 else
11216 {
11217 aggressive = current_buffer->scroll_down_aggressively;
11218 height = WINDOW_BOX_TEXT_HEIGHT (w);
11219 if (NUMBERP (aggressive))
11220 {
11221 double float_amount = XFLOATINT (aggressive) * height;
11222 amount_to_scroll = float_amount;
11223 if (amount_to_scroll == 0 && float_amount > 0)
11224 amount_to_scroll = 1;
11225 }
11226 }
11227
11228 if (amount_to_scroll <= 0)
11229 return SCROLLING_FAILED;
11230
11231 move_it_vertically_backward (&it, amount_to_scroll);
11232 startp = it.current.pos;
11233 }
11234 }
11235
11236 /* Run window scroll functions. */
11237 startp = run_window_scroll_functions (window, startp);
11238
11239 /* Display the window. Give up if new fonts are loaded, or if point
11240 doesn't appear. */
11241 if (!try_window (window, startp))
11242 rc = SCROLLING_NEED_LARGER_MATRICES;
11243 else if (w->cursor.vpos < 0)
11244 {
11245 clear_glyph_matrix (w->desired_matrix);
11246 rc = SCROLLING_FAILED;
11247 }
11248 else
11249 {
11250 /* Maybe forget recorded base line for line number display. */
11251 if (!just_this_one_p
11252 || current_buffer->clip_changed
11253 || BEG_UNCHANGED < CHARPOS (startp))
11254 w->base_line_number = Qnil;
11255
11256 /* If cursor ends up on a partially visible line,
11257 treat that as being off the bottom of the screen. */
11258 if (! make_cursor_line_fully_visible (w, extra_scroll_margin_lines <= 1))
11259 {
11260 clear_glyph_matrix (w->desired_matrix);
11261 ++extra_scroll_margin_lines;
11262 goto too_near_end;
11263 }
11264 rc = SCROLLING_SUCCESS;
11265 }
11266
11267 return rc;
11268 }
11269
11270
11271 /* Compute a suitable window start for window W if display of W starts
11272 on a continuation line. Value is non-zero if a new window start
11273 was computed.
11274
11275 The new window start will be computed, based on W's width, starting
11276 from the start of the continued line. It is the start of the
11277 screen line with the minimum distance from the old start W->start. */
11278
11279 static int
11280 compute_window_start_on_continuation_line (w)
11281 struct window *w;
11282 {
11283 struct text_pos pos, start_pos;
11284 int window_start_changed_p = 0;
11285
11286 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11287
11288 /* If window start is on a continuation line... Window start may be
11289 < BEGV in case there's invisible text at the start of the
11290 buffer (M-x rmail, for example). */
11291 if (CHARPOS (start_pos) > BEGV
11292 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11293 {
11294 struct it it;
11295 struct glyph_row *row;
11296
11297 /* Handle the case that the window start is out of range. */
11298 if (CHARPOS (start_pos) < BEGV)
11299 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11300 else if (CHARPOS (start_pos) > ZV)
11301 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11302
11303 /* Find the start of the continued line. This should be fast
11304 because scan_buffer is fast (newline cache). */
11305 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11306 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11307 row, DEFAULT_FACE_ID);
11308 reseat_at_previous_visible_line_start (&it);
11309
11310 /* If the line start is "too far" away from the window start,
11311 say it takes too much time to compute a new window start. */
11312 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11313 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11314 {
11315 int min_distance, distance;
11316
11317 /* Move forward by display lines to find the new window
11318 start. If window width was enlarged, the new start can
11319 be expected to be > the old start. If window width was
11320 decreased, the new window start will be < the old start.
11321 So, we're looking for the display line start with the
11322 minimum distance from the old window start. */
11323 pos = it.current.pos;
11324 min_distance = INFINITY;
11325 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11326 distance < min_distance)
11327 {
11328 min_distance = distance;
11329 pos = it.current.pos;
11330 move_it_by_lines (&it, 1, 0);
11331 }
11332
11333 /* Set the window start there. */
11334 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11335 window_start_changed_p = 1;
11336 }
11337 }
11338
11339 return window_start_changed_p;
11340 }
11341
11342
11343 /* Try cursor movement in case text has not changed in window WINDOW,
11344 with window start STARTP. Value is
11345
11346 CURSOR_MOVEMENT_SUCCESS if successful
11347
11348 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11349
11350 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11351 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11352 we want to scroll as if scroll-step were set to 1. See the code.
11353
11354 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11355 which case we have to abort this redisplay, and adjust matrices
11356 first. */
11357
11358 enum
11359 {
11360 CURSOR_MOVEMENT_SUCCESS,
11361 CURSOR_MOVEMENT_CANNOT_BE_USED,
11362 CURSOR_MOVEMENT_MUST_SCROLL,
11363 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11364 };
11365
11366 static int
11367 try_cursor_movement (window, startp, scroll_step)
11368 Lisp_Object window;
11369 struct text_pos startp;
11370 int *scroll_step;
11371 {
11372 struct window *w = XWINDOW (window);
11373 struct frame *f = XFRAME (w->frame);
11374 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11375
11376 #if GLYPH_DEBUG
11377 if (inhibit_try_cursor_movement)
11378 return rc;
11379 #endif
11380
11381 /* Handle case where text has not changed, only point, and it has
11382 not moved off the frame. */
11383 if (/* Point may be in this window. */
11384 PT >= CHARPOS (startp)
11385 /* Selective display hasn't changed. */
11386 && !current_buffer->clip_changed
11387 /* Function force-mode-line-update is used to force a thorough
11388 redisplay. It sets either windows_or_buffers_changed or
11389 update_mode_lines. So don't take a shortcut here for these
11390 cases. */
11391 && !update_mode_lines
11392 && !windows_or_buffers_changed
11393 && !cursor_type_changed
11394 /* Can't use this case if highlighting a region. When a
11395 region exists, cursor movement has to do more than just
11396 set the cursor. */
11397 && !(!NILP (Vtransient_mark_mode)
11398 && !NILP (current_buffer->mark_active))
11399 && NILP (w->region_showing)
11400 && NILP (Vshow_trailing_whitespace)
11401 /* Right after splitting windows, last_point may be nil. */
11402 && INTEGERP (w->last_point)
11403 /* This code is not used for mini-buffer for the sake of the case
11404 of redisplaying to replace an echo area message; since in
11405 that case the mini-buffer contents per se are usually
11406 unchanged. This code is of no real use in the mini-buffer
11407 since the handling of this_line_start_pos, etc., in redisplay
11408 handles the same cases. */
11409 && !EQ (window, minibuf_window)
11410 /* When splitting windows or for new windows, it happens that
11411 redisplay is called with a nil window_end_vpos or one being
11412 larger than the window. This should really be fixed in
11413 window.c. I don't have this on my list, now, so we do
11414 approximately the same as the old redisplay code. --gerd. */
11415 && INTEGERP (w->window_end_vpos)
11416 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11417 && (FRAME_WINDOW_P (f)
11418 || !overlay_arrow_in_current_buffer_p ()))
11419 {
11420 int this_scroll_margin, top_scroll_margin;
11421 struct glyph_row *row = NULL;
11422
11423 #if GLYPH_DEBUG
11424 debug_method_add (w, "cursor movement");
11425 #endif
11426
11427 /* Scroll if point within this distance from the top or bottom
11428 of the window. This is a pixel value. */
11429 this_scroll_margin = max (0, scroll_margin);
11430 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11431 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11432
11433 top_scroll_margin = this_scroll_margin;
11434 if (WINDOW_WANTS_HEADER_LINE_P (w))
11435 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
11436
11437 /* Start with the row the cursor was displayed during the last
11438 not paused redisplay. Give up if that row is not valid. */
11439 if (w->last_cursor.vpos < 0
11440 || w->last_cursor.vpos >= w->current_matrix->nrows)
11441 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11442 else
11443 {
11444 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11445 if (row->mode_line_p)
11446 ++row;
11447 if (!row->enabled_p)
11448 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11449 }
11450
11451 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11452 {
11453 int scroll_p = 0;
11454 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11455
11456 if (PT > XFASTINT (w->last_point))
11457 {
11458 /* Point has moved forward. */
11459 while (MATRIX_ROW_END_CHARPOS (row) < PT
11460 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11461 {
11462 xassert (row->enabled_p);
11463 ++row;
11464 }
11465
11466 /* The end position of a row equals the start position
11467 of the next row. If PT is there, we would rather
11468 display it in the next line. */
11469 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11470 && MATRIX_ROW_END_CHARPOS (row) == PT
11471 && !cursor_row_p (w, row))
11472 ++row;
11473
11474 /* If within the scroll margin, scroll. Note that
11475 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11476 the next line would be drawn, and that
11477 this_scroll_margin can be zero. */
11478 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11479 || PT > MATRIX_ROW_END_CHARPOS (row)
11480 /* Line is completely visible last line in window
11481 and PT is to be set in the next line. */
11482 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11483 && PT == MATRIX_ROW_END_CHARPOS (row)
11484 && !row->ends_at_zv_p
11485 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11486 scroll_p = 1;
11487 }
11488 else if (PT < XFASTINT (w->last_point))
11489 {
11490 /* Cursor has to be moved backward. Note that PT >=
11491 CHARPOS (startp) because of the outer if-statement. */
11492 while (!row->mode_line_p
11493 && (MATRIX_ROW_START_CHARPOS (row) > PT
11494 || (MATRIX_ROW_START_CHARPOS (row) == PT
11495 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11496 && (row->y > top_scroll_margin
11497 || CHARPOS (startp) == BEGV))
11498 {
11499 xassert (row->enabled_p);
11500 --row;
11501 }
11502
11503 /* Consider the following case: Window starts at BEGV,
11504 there is invisible, intangible text at BEGV, so that
11505 display starts at some point START > BEGV. It can
11506 happen that we are called with PT somewhere between
11507 BEGV and START. Try to handle that case. */
11508 if (row < w->current_matrix->rows
11509 || row->mode_line_p)
11510 {
11511 row = w->current_matrix->rows;
11512 if (row->mode_line_p)
11513 ++row;
11514 }
11515
11516 /* Due to newlines in overlay strings, we may have to
11517 skip forward over overlay strings. */
11518 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11519 && MATRIX_ROW_END_CHARPOS (row) == PT
11520 && !cursor_row_p (w, row))
11521 ++row;
11522
11523 /* If within the scroll margin, scroll. */
11524 if (row->y < top_scroll_margin
11525 && CHARPOS (startp) != BEGV)
11526 scroll_p = 1;
11527 }
11528
11529 if (PT < MATRIX_ROW_START_CHARPOS (row)
11530 || PT > MATRIX_ROW_END_CHARPOS (row))
11531 {
11532 /* if PT is not in the glyph row, give up. */
11533 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11534 }
11535 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
11536 && make_cursor_line_fully_visible_p)
11537 {
11538 if (PT == MATRIX_ROW_END_CHARPOS (row)
11539 && !row->ends_at_zv_p
11540 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11541 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11542 else if (row->height > window_box_height (w))
11543 {
11544 /* If we end up in a partially visible line, let's
11545 make it fully visible, except when it's taller
11546 than the window, in which case we can't do much
11547 about it. */
11548 *scroll_step = 1;
11549 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11550 }
11551 else
11552 {
11553 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11554 if (!make_cursor_line_fully_visible (w, 0))
11555 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11556 else
11557 rc = CURSOR_MOVEMENT_SUCCESS;
11558 }
11559 }
11560 else if (scroll_p)
11561 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11562 else
11563 {
11564 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11565 rc = CURSOR_MOVEMENT_SUCCESS;
11566 }
11567 }
11568 }
11569
11570 return rc;
11571 }
11572
11573 void
11574 set_vertical_scroll_bar (w)
11575 struct window *w;
11576 {
11577 int start, end, whole;
11578
11579 /* Calculate the start and end positions for the current window.
11580 At some point, it would be nice to choose between scrollbars
11581 which reflect the whole buffer size, with special markers
11582 indicating narrowing, and scrollbars which reflect only the
11583 visible region.
11584
11585 Note that mini-buffers sometimes aren't displaying any text. */
11586 if (!MINI_WINDOW_P (w)
11587 || (w == XWINDOW (minibuf_window)
11588 && NILP (echo_area_buffer[0])))
11589 {
11590 struct buffer *buf = XBUFFER (w->buffer);
11591 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11592 start = marker_position (w->start) - BUF_BEGV (buf);
11593 /* I don't think this is guaranteed to be right. For the
11594 moment, we'll pretend it is. */
11595 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11596
11597 if (end < start)
11598 end = start;
11599 if (whole < (end - start))
11600 whole = end - start;
11601 }
11602 else
11603 start = end = whole = 0;
11604
11605 /* Indicate what this scroll bar ought to be displaying now. */
11606 set_vertical_scroll_bar_hook (w, end - start, whole, start);
11607 }
11608
11609
11610 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11611 selected_window is redisplayed.
11612
11613 We can return without actually redisplaying the window if
11614 fonts_changed_p is nonzero. In that case, redisplay_internal will
11615 retry. */
11616
11617 static void
11618 redisplay_window (window, just_this_one_p)
11619 Lisp_Object window;
11620 int just_this_one_p;
11621 {
11622 struct window *w = XWINDOW (window);
11623 struct frame *f = XFRAME (w->frame);
11624 struct buffer *buffer = XBUFFER (w->buffer);
11625 struct buffer *old = current_buffer;
11626 struct text_pos lpoint, opoint, startp;
11627 int update_mode_line;
11628 int tem;
11629 struct it it;
11630 /* Record it now because it's overwritten. */
11631 int current_matrix_up_to_date_p = 0;
11632 int used_current_matrix_p = 0;
11633 /* This is less strict than current_matrix_up_to_date_p.
11634 It indictes that the buffer contents and narrowing are unchanged. */
11635 int buffer_unchanged_p = 0;
11636 int temp_scroll_step = 0;
11637 int count = SPECPDL_INDEX ();
11638 int rc;
11639 int centering_position;
11640 int last_line_misfit = 0;
11641
11642 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11643 opoint = lpoint;
11644
11645 /* W must be a leaf window here. */
11646 xassert (!NILP (w->buffer));
11647 #if GLYPH_DEBUG
11648 *w->desired_matrix->method = 0;
11649 #endif
11650
11651 /* Force this to be looked up again for each redisp of each window. */
11652 escape_glyph_face = -1;
11653
11654 specbind (Qinhibit_point_motion_hooks, Qt);
11655
11656 reconsider_clip_changes (w, buffer);
11657
11658 /* Has the mode line to be updated? */
11659 update_mode_line = (!NILP (w->update_mode_line)
11660 || update_mode_lines
11661 || buffer->clip_changed
11662 || buffer->prevent_redisplay_optimizations_p);
11663
11664 if (MINI_WINDOW_P (w))
11665 {
11666 if (w == XWINDOW (echo_area_window)
11667 && !NILP (echo_area_buffer[0]))
11668 {
11669 if (update_mode_line)
11670 /* We may have to update a tty frame's menu bar or a
11671 tool-bar. Example `M-x C-h C-h C-g'. */
11672 goto finish_menu_bars;
11673 else
11674 /* We've already displayed the echo area glyphs in this window. */
11675 goto finish_scroll_bars;
11676 }
11677 else if ((w != XWINDOW (minibuf_window)
11678 || minibuf_level == 0)
11679 /* When buffer is nonempty, redisplay window normally. */
11680 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
11681 /* Quail displays non-mini buffers in minibuffer window.
11682 In that case, redisplay the window normally. */
11683 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
11684 {
11685 /* W is a mini-buffer window, but it's not active, so clear
11686 it. */
11687 int yb = window_text_bottom_y (w);
11688 struct glyph_row *row;
11689 int y;
11690
11691 for (y = 0, row = w->desired_matrix->rows;
11692 y < yb;
11693 y += row->height, ++row)
11694 blank_row (w, row, y);
11695 goto finish_scroll_bars;
11696 }
11697
11698 clear_glyph_matrix (w->desired_matrix);
11699 }
11700
11701 /* Otherwise set up data on this window; select its buffer and point
11702 value. */
11703 /* Really select the buffer, for the sake of buffer-local
11704 variables. */
11705 set_buffer_internal_1 (XBUFFER (w->buffer));
11706 SET_TEXT_POS (opoint, PT, PT_BYTE);
11707
11708 current_matrix_up_to_date_p
11709 = (!NILP (w->window_end_valid)
11710 && !current_buffer->clip_changed
11711 && !current_buffer->prevent_redisplay_optimizations_p
11712 && XFASTINT (w->last_modified) >= MODIFF
11713 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11714
11715 buffer_unchanged_p
11716 = (!NILP (w->window_end_valid)
11717 && !current_buffer->clip_changed
11718 && XFASTINT (w->last_modified) >= MODIFF
11719 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11720
11721 /* When windows_or_buffers_changed is non-zero, we can't rely on
11722 the window end being valid, so set it to nil there. */
11723 if (windows_or_buffers_changed)
11724 {
11725 /* If window starts on a continuation line, maybe adjust the
11726 window start in case the window's width changed. */
11727 if (XMARKER (w->start)->buffer == current_buffer)
11728 compute_window_start_on_continuation_line (w);
11729
11730 w->window_end_valid = Qnil;
11731 }
11732
11733 /* Some sanity checks. */
11734 CHECK_WINDOW_END (w);
11735 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
11736 abort ();
11737 if (BYTEPOS (opoint) < CHARPOS (opoint))
11738 abort ();
11739
11740 /* If %c is in mode line, update it if needed. */
11741 if (!NILP (w->column_number_displayed)
11742 /* This alternative quickly identifies a common case
11743 where no change is needed. */
11744 && !(PT == XFASTINT (w->last_point)
11745 && XFASTINT (w->last_modified) >= MODIFF
11746 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11747 && (XFASTINT (w->column_number_displayed)
11748 != (int) current_column ())) /* iftc */
11749 update_mode_line = 1;
11750
11751 /* Count number of windows showing the selected buffer. An indirect
11752 buffer counts as its base buffer. */
11753 if (!just_this_one_p)
11754 {
11755 struct buffer *current_base, *window_base;
11756 current_base = current_buffer;
11757 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11758 if (current_base->base_buffer)
11759 current_base = current_base->base_buffer;
11760 if (window_base->base_buffer)
11761 window_base = window_base->base_buffer;
11762 if (current_base == window_base)
11763 buffer_shared++;
11764 }
11765
11766 /* Point refers normally to the selected window. For any other
11767 window, set up appropriate value. */
11768 if (!EQ (window, selected_window))
11769 {
11770 int new_pt = XMARKER (w->pointm)->charpos;
11771 int new_pt_byte = marker_byte_position (w->pointm);
11772 if (new_pt < BEGV)
11773 {
11774 new_pt = BEGV;
11775 new_pt_byte = BEGV_BYTE;
11776 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
11777 }
11778 else if (new_pt > (ZV - 1))
11779 {
11780 new_pt = ZV;
11781 new_pt_byte = ZV_BYTE;
11782 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
11783 }
11784
11785 /* We don't use SET_PT so that the point-motion hooks don't run. */
11786 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
11787 }
11788
11789 /* If any of the character widths specified in the display table
11790 have changed, invalidate the width run cache. It's true that
11791 this may be a bit late to catch such changes, but the rest of
11792 redisplay goes (non-fatally) haywire when the display table is
11793 changed, so why should we worry about doing any better? */
11794 if (current_buffer->width_run_cache)
11795 {
11796 struct Lisp_Char_Table *disptab = buffer_display_table ();
11797
11798 if (! disptab_matches_widthtab (disptab,
11799 XVECTOR (current_buffer->width_table)))
11800 {
11801 invalidate_region_cache (current_buffer,
11802 current_buffer->width_run_cache,
11803 BEG, Z);
11804 recompute_width_table (current_buffer, disptab);
11805 }
11806 }
11807
11808 /* If window-start is screwed up, choose a new one. */
11809 if (XMARKER (w->start)->buffer != current_buffer)
11810 goto recenter;
11811
11812 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11813
11814 /* If someone specified a new starting point but did not insist,
11815 check whether it can be used. */
11816 if (!NILP (w->optional_new_start)
11817 && CHARPOS (startp) >= BEGV
11818 && CHARPOS (startp) <= ZV)
11819 {
11820 w->optional_new_start = Qnil;
11821 start_display (&it, w, startp);
11822 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11823 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11824 if (IT_CHARPOS (it) == PT)
11825 w->force_start = Qt;
11826 /* IT may overshoot PT if text at PT is invisible. */
11827 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
11828 w->force_start = Qt;
11829
11830
11831 }
11832
11833 /* Handle case where place to start displaying has been specified,
11834 unless the specified location is outside the accessible range. */
11835 if (!NILP (w->force_start)
11836 || w->frozen_window_start_p)
11837 {
11838 /* We set this later on if we have to adjust point. */
11839 int new_vpos = -1;
11840
11841 w->force_start = Qnil;
11842 w->vscroll = 0;
11843 w->window_end_valid = Qnil;
11844
11845 /* Forget any recorded base line for line number display. */
11846 if (!buffer_unchanged_p)
11847 w->base_line_number = Qnil;
11848
11849 /* Redisplay the mode line. Select the buffer properly for that.
11850 Also, run the hook window-scroll-functions
11851 because we have scrolled. */
11852 /* Note, we do this after clearing force_start because
11853 if there's an error, it is better to forget about force_start
11854 than to get into an infinite loop calling the hook functions
11855 and having them get more errors. */
11856 if (!update_mode_line
11857 || ! NILP (Vwindow_scroll_functions))
11858 {
11859 update_mode_line = 1;
11860 w->update_mode_line = Qt;
11861 startp = run_window_scroll_functions (window, startp);
11862 }
11863
11864 w->last_modified = make_number (0);
11865 w->last_overlay_modified = make_number (0);
11866 if (CHARPOS (startp) < BEGV)
11867 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
11868 else if (CHARPOS (startp) > ZV)
11869 SET_TEXT_POS (startp, ZV, ZV_BYTE);
11870
11871 /* Redisplay, then check if cursor has been set during the
11872 redisplay. Give up if new fonts were loaded. */
11873 if (!try_window (window, startp))
11874 {
11875 w->force_start = Qt;
11876 clear_glyph_matrix (w->desired_matrix);
11877 goto need_larger_matrices;
11878 }
11879
11880 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
11881 {
11882 /* If point does not appear, try to move point so it does
11883 appear. The desired matrix has been built above, so we
11884 can use it here. */
11885 new_vpos = window_box_height (w) / 2;
11886 }
11887
11888 if (!make_cursor_line_fully_visible (w, 0))
11889 {
11890 /* Point does appear, but on a line partly visible at end of window.
11891 Move it back to a fully-visible line. */
11892 new_vpos = window_box_height (w);
11893 }
11894
11895 /* If we need to move point for either of the above reasons,
11896 now actually do it. */
11897 if (new_vpos >= 0)
11898 {
11899 struct glyph_row *row;
11900
11901 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
11902 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
11903 ++row;
11904
11905 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
11906 MATRIX_ROW_START_BYTEPOS (row));
11907
11908 if (w != XWINDOW (selected_window))
11909 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
11910 else if (current_buffer == old)
11911 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11912
11913 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
11914
11915 /* If we are highlighting the region, then we just changed
11916 the region, so redisplay to show it. */
11917 if (!NILP (Vtransient_mark_mode)
11918 && !NILP (current_buffer->mark_active))
11919 {
11920 clear_glyph_matrix (w->desired_matrix);
11921 if (!try_window (window, startp))
11922 goto need_larger_matrices;
11923 }
11924 }
11925
11926 #if GLYPH_DEBUG
11927 debug_method_add (w, "forced window start");
11928 #endif
11929 goto done;
11930 }
11931
11932 /* Handle case where text has not changed, only point, and it has
11933 not moved off the frame, and we are not retrying after hscroll.
11934 (current_matrix_up_to_date_p is nonzero when retrying.) */
11935 if (current_matrix_up_to_date_p
11936 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
11937 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
11938 {
11939 switch (rc)
11940 {
11941 case CURSOR_MOVEMENT_SUCCESS:
11942 used_current_matrix_p = 1;
11943 goto done;
11944
11945 #if 0 /* try_cursor_movement never returns this value. */
11946 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
11947 goto need_larger_matrices;
11948 #endif
11949
11950 case CURSOR_MOVEMENT_MUST_SCROLL:
11951 goto try_to_scroll;
11952
11953 default:
11954 abort ();
11955 }
11956 }
11957 /* If current starting point was originally the beginning of a line
11958 but no longer is, find a new starting point. */
11959 else if (!NILP (w->start_at_line_beg)
11960 && !(CHARPOS (startp) <= BEGV
11961 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
11962 {
11963 #if GLYPH_DEBUG
11964 debug_method_add (w, "recenter 1");
11965 #endif
11966 goto recenter;
11967 }
11968
11969 /* Try scrolling with try_window_id. Value is > 0 if update has
11970 been done, it is -1 if we know that the same window start will
11971 not work. It is 0 if unsuccessful for some other reason. */
11972 else if ((tem = try_window_id (w)) != 0)
11973 {
11974 #if GLYPH_DEBUG
11975 debug_method_add (w, "try_window_id %d", tem);
11976 #endif
11977
11978 if (fonts_changed_p)
11979 goto need_larger_matrices;
11980 if (tem > 0)
11981 goto done;
11982
11983 /* Otherwise try_window_id has returned -1 which means that we
11984 don't want the alternative below this comment to execute. */
11985 }
11986 else if (CHARPOS (startp) >= BEGV
11987 && CHARPOS (startp) <= ZV
11988 && PT >= CHARPOS (startp)
11989 && (CHARPOS (startp) < ZV
11990 /* Avoid starting at end of buffer. */
11991 || CHARPOS (startp) == BEGV
11992 || (XFASTINT (w->last_modified) >= MODIFF
11993 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
11994 {
11995 #if GLYPH_DEBUG
11996 debug_method_add (w, "same window start");
11997 #endif
11998
11999 /* Try to redisplay starting at same place as before.
12000 If point has not moved off frame, accept the results. */
12001 if (!current_matrix_up_to_date_p
12002 /* Don't use try_window_reusing_current_matrix in this case
12003 because a window scroll function can have changed the
12004 buffer. */
12005 || !NILP (Vwindow_scroll_functions)
12006 || MINI_WINDOW_P (w)
12007 || !(used_current_matrix_p
12008 = try_window_reusing_current_matrix (w)))
12009 {
12010 IF_DEBUG (debug_method_add (w, "1"));
12011 try_window (window, startp);
12012 }
12013
12014 if (fonts_changed_p)
12015 goto need_larger_matrices;
12016
12017 if (w->cursor.vpos >= 0)
12018 {
12019 if (!just_this_one_p
12020 || current_buffer->clip_changed
12021 || BEG_UNCHANGED < CHARPOS (startp))
12022 /* Forget any recorded base line for line number display. */
12023 w->base_line_number = Qnil;
12024
12025 if (!make_cursor_line_fully_visible (w, 1))
12026 {
12027 clear_glyph_matrix (w->desired_matrix);
12028 last_line_misfit = 1;
12029 }
12030 /* Drop through and scroll. */
12031 else
12032 goto done;
12033 }
12034 else
12035 clear_glyph_matrix (w->desired_matrix);
12036 }
12037
12038 try_to_scroll:
12039
12040 w->last_modified = make_number (0);
12041 w->last_overlay_modified = make_number (0);
12042
12043 /* Redisplay the mode line. Select the buffer properly for that. */
12044 if (!update_mode_line)
12045 {
12046 update_mode_line = 1;
12047 w->update_mode_line = Qt;
12048 }
12049
12050 /* Try to scroll by specified few lines. */
12051 if ((scroll_conservatively
12052 || scroll_step
12053 || temp_scroll_step
12054 || NUMBERP (current_buffer->scroll_up_aggressively)
12055 || NUMBERP (current_buffer->scroll_down_aggressively))
12056 && !current_buffer->clip_changed
12057 && CHARPOS (startp) >= BEGV
12058 && CHARPOS (startp) <= ZV)
12059 {
12060 /* The function returns -1 if new fonts were loaded, 1 if
12061 successful, 0 if not successful. */
12062 int rc = try_scrolling (window, just_this_one_p,
12063 scroll_conservatively,
12064 scroll_step,
12065 temp_scroll_step, last_line_misfit);
12066 switch (rc)
12067 {
12068 case SCROLLING_SUCCESS:
12069 goto done;
12070
12071 case SCROLLING_NEED_LARGER_MATRICES:
12072 goto need_larger_matrices;
12073
12074 case SCROLLING_FAILED:
12075 break;
12076
12077 default:
12078 abort ();
12079 }
12080 }
12081
12082 /* Finally, just choose place to start which centers point */
12083
12084 recenter:
12085 centering_position = window_box_height (w) / 2;
12086
12087 point_at_top:
12088 /* Jump here with centering_position already set to 0. */
12089
12090 #if GLYPH_DEBUG
12091 debug_method_add (w, "recenter");
12092 #endif
12093
12094 /* w->vscroll = 0; */
12095
12096 /* Forget any previously recorded base line for line number display. */
12097 if (!buffer_unchanged_p)
12098 w->base_line_number = Qnil;
12099
12100 /* Move backward half the height of the window. */
12101 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12102 it.current_y = it.last_visible_y;
12103 move_it_vertically_backward (&it, centering_position);
12104 xassert (IT_CHARPOS (it) >= BEGV);
12105
12106 /* The function move_it_vertically_backward may move over more
12107 than the specified y-distance. If it->w is small, e.g. a
12108 mini-buffer window, we may end up in front of the window's
12109 display area. Start displaying at the start of the line
12110 containing PT in this case. */
12111 if (it.current_y <= 0)
12112 {
12113 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12114 move_it_vertically_backward (&it, 0);
12115 xassert (IT_CHARPOS (it) <= PT);
12116 it.current_y = 0;
12117 }
12118
12119 it.current_x = it.hpos = 0;
12120
12121 /* Set startp here explicitly in case that helps avoid an infinite loop
12122 in case the window-scroll-functions functions get errors. */
12123 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12124
12125 /* Run scroll hooks. */
12126 startp = run_window_scroll_functions (window, it.current.pos);
12127
12128 /* Redisplay the window. */
12129 if (!current_matrix_up_to_date_p
12130 || windows_or_buffers_changed
12131 || cursor_type_changed
12132 /* Don't use try_window_reusing_current_matrix in this case
12133 because it can have changed the buffer. */
12134 || !NILP (Vwindow_scroll_functions)
12135 || !just_this_one_p
12136 || MINI_WINDOW_P (w)
12137 || !(used_current_matrix_p
12138 = try_window_reusing_current_matrix (w)))
12139 try_window (window, startp);
12140
12141 /* If new fonts have been loaded (due to fontsets), give up. We
12142 have to start a new redisplay since we need to re-adjust glyph
12143 matrices. */
12144 if (fonts_changed_p)
12145 goto need_larger_matrices;
12146
12147 /* If cursor did not appear assume that the middle of the window is
12148 in the first line of the window. Do it again with the next line.
12149 (Imagine a window of height 100, displaying two lines of height
12150 60. Moving back 50 from it->last_visible_y will end in the first
12151 line.) */
12152 if (w->cursor.vpos < 0)
12153 {
12154 if (!NILP (w->window_end_valid)
12155 && PT >= Z - XFASTINT (w->window_end_pos))
12156 {
12157 clear_glyph_matrix (w->desired_matrix);
12158 move_it_by_lines (&it, 1, 0);
12159 try_window (window, it.current.pos);
12160 }
12161 else if (PT < IT_CHARPOS (it))
12162 {
12163 clear_glyph_matrix (w->desired_matrix);
12164 move_it_by_lines (&it, -1, 0);
12165 try_window (window, it.current.pos);
12166 }
12167 else
12168 {
12169 /* Not much we can do about it. */
12170 }
12171 }
12172
12173 /* Consider the following case: Window starts at BEGV, there is
12174 invisible, intangible text at BEGV, so that display starts at
12175 some point START > BEGV. It can happen that we are called with
12176 PT somewhere between BEGV and START. Try to handle that case. */
12177 if (w->cursor.vpos < 0)
12178 {
12179 struct glyph_row *row = w->current_matrix->rows;
12180 if (row->mode_line_p)
12181 ++row;
12182 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12183 }
12184
12185 if (!make_cursor_line_fully_visible (w, centering_position > 0))
12186 {
12187 /* If vscroll is enabled, disable it and try again. */
12188 if (w->vscroll)
12189 {
12190 w->vscroll = 0;
12191 clear_glyph_matrix (w->desired_matrix);
12192 goto recenter;
12193 }
12194
12195 /* If centering point failed to make the whole line visible,
12196 put point at the top instead. That has to make the whole line
12197 visible, if it can be done. */
12198 clear_glyph_matrix (w->desired_matrix);
12199 centering_position = 0;
12200 goto point_at_top;
12201 }
12202
12203 done:
12204
12205 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12206 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12207 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12208 ? Qt : Qnil);
12209
12210 /* Display the mode line, if we must. */
12211 if ((update_mode_line
12212 /* If window not full width, must redo its mode line
12213 if (a) the window to its side is being redone and
12214 (b) we do a frame-based redisplay. This is a consequence
12215 of how inverted lines are drawn in frame-based redisplay. */
12216 || (!just_this_one_p
12217 && !FRAME_WINDOW_P (f)
12218 && !WINDOW_FULL_WIDTH_P (w))
12219 /* Line number to display. */
12220 || INTEGERP (w->base_line_pos)
12221 /* Column number is displayed and different from the one displayed. */
12222 || (!NILP (w->column_number_displayed)
12223 && (XFASTINT (w->column_number_displayed)
12224 != (int) current_column ()))) /* iftc */
12225 /* This means that the window has a mode line. */
12226 && (WINDOW_WANTS_MODELINE_P (w)
12227 || WINDOW_WANTS_HEADER_LINE_P (w)))
12228 {
12229 display_mode_lines (w);
12230
12231 /* If mode line height has changed, arrange for a thorough
12232 immediate redisplay using the correct mode line height. */
12233 if (WINDOW_WANTS_MODELINE_P (w)
12234 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12235 {
12236 fonts_changed_p = 1;
12237 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12238 = DESIRED_MODE_LINE_HEIGHT (w);
12239 }
12240
12241 /* If top line height has changed, arrange for a thorough
12242 immediate redisplay using the correct mode line height. */
12243 if (WINDOW_WANTS_HEADER_LINE_P (w)
12244 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12245 {
12246 fonts_changed_p = 1;
12247 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12248 = DESIRED_HEADER_LINE_HEIGHT (w);
12249 }
12250
12251 if (fonts_changed_p)
12252 goto need_larger_matrices;
12253 }
12254
12255 if (!line_number_displayed
12256 && !BUFFERP (w->base_line_pos))
12257 {
12258 w->base_line_pos = Qnil;
12259 w->base_line_number = Qnil;
12260 }
12261
12262 finish_menu_bars:
12263
12264 /* When we reach a frame's selected window, redo the frame's menu bar. */
12265 if (update_mode_line
12266 && EQ (FRAME_SELECTED_WINDOW (f), window))
12267 {
12268 int redisplay_menu_p = 0;
12269 int redisplay_tool_bar_p = 0;
12270
12271 if (FRAME_WINDOW_P (f))
12272 {
12273 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12274 || defined (USE_GTK)
12275 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12276 #else
12277 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12278 #endif
12279 }
12280 else
12281 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12282
12283 if (redisplay_menu_p)
12284 display_menu_bar (w);
12285
12286 #ifdef HAVE_WINDOW_SYSTEM
12287 #ifdef USE_GTK
12288 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12289 #else
12290 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12291 && (FRAME_TOOL_BAR_LINES (f) > 0
12292 || auto_resize_tool_bars_p);
12293
12294 #endif
12295
12296 if (redisplay_tool_bar_p)
12297 redisplay_tool_bar (f);
12298 #endif
12299 }
12300
12301 #ifdef HAVE_WINDOW_SYSTEM
12302 if (FRAME_WINDOW_P (f)
12303 && update_window_fringes (w, 0)
12304 && !just_this_one_p
12305 && (used_current_matrix_p || overlay_arrow_seen)
12306 && !w->pseudo_window_p)
12307 {
12308 update_begin (f);
12309 BLOCK_INPUT;
12310 if (draw_window_fringes (w, 1))
12311 x_draw_vertical_border (w);
12312 UNBLOCK_INPUT;
12313 update_end (f);
12314 }
12315 #endif /* HAVE_WINDOW_SYSTEM */
12316
12317 /* We go to this label, with fonts_changed_p nonzero,
12318 if it is necessary to try again using larger glyph matrices.
12319 We have to redeem the scroll bar even in this case,
12320 because the loop in redisplay_internal expects that. */
12321 need_larger_matrices:
12322 ;
12323 finish_scroll_bars:
12324
12325 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12326 {
12327 /* Set the thumb's position and size. */
12328 set_vertical_scroll_bar (w);
12329
12330 /* Note that we actually used the scroll bar attached to this
12331 window, so it shouldn't be deleted at the end of redisplay. */
12332 redeem_scroll_bar_hook (w);
12333 }
12334
12335 /* Restore current_buffer and value of point in it. */
12336 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12337 set_buffer_internal_1 (old);
12338 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12339
12340 unbind_to (count, Qnil);
12341 }
12342
12343
12344 /* Build the complete desired matrix of WINDOW with a window start
12345 buffer position POS. Value is non-zero if successful. It is zero
12346 if fonts were loaded during redisplay which makes re-adjusting
12347 glyph matrices necessary. */
12348
12349 int
12350 try_window (window, pos)
12351 Lisp_Object window;
12352 struct text_pos pos;
12353 {
12354 struct window *w = XWINDOW (window);
12355 struct it it;
12356 struct glyph_row *last_text_row = NULL;
12357
12358 /* Make POS the new window start. */
12359 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12360
12361 /* Mark cursor position as unknown. No overlay arrow seen. */
12362 w->cursor.vpos = -1;
12363 overlay_arrow_seen = 0;
12364
12365 /* Initialize iterator and info to start at POS. */
12366 start_display (&it, w, pos);
12367
12368 /* Display all lines of W. */
12369 while (it.current_y < it.last_visible_y)
12370 {
12371 if (display_line (&it))
12372 last_text_row = it.glyph_row - 1;
12373 if (fonts_changed_p)
12374 return 0;
12375 }
12376
12377 /* If bottom moved off end of frame, change mode line percentage. */
12378 if (XFASTINT (w->window_end_pos) <= 0
12379 && Z != IT_CHARPOS (it))
12380 w->update_mode_line = Qt;
12381
12382 /* Set window_end_pos to the offset of the last character displayed
12383 on the window from the end of current_buffer. Set
12384 window_end_vpos to its row number. */
12385 if (last_text_row)
12386 {
12387 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12388 w->window_end_bytepos
12389 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12390 w->window_end_pos
12391 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12392 w->window_end_vpos
12393 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12394 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12395 ->displays_text_p);
12396 }
12397 else
12398 {
12399 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12400 w->window_end_pos = make_number (Z - ZV);
12401 w->window_end_vpos = make_number (0);
12402 }
12403
12404 /* But that is not valid info until redisplay finishes. */
12405 w->window_end_valid = Qnil;
12406 return 1;
12407 }
12408
12409
12410 \f
12411 /************************************************************************
12412 Window redisplay reusing current matrix when buffer has not changed
12413 ************************************************************************/
12414
12415 /* Try redisplay of window W showing an unchanged buffer with a
12416 different window start than the last time it was displayed by
12417 reusing its current matrix. Value is non-zero if successful.
12418 W->start is the new window start. */
12419
12420 static int
12421 try_window_reusing_current_matrix (w)
12422 struct window *w;
12423 {
12424 struct frame *f = XFRAME (w->frame);
12425 struct glyph_row *row, *bottom_row;
12426 struct it it;
12427 struct run run;
12428 struct text_pos start, new_start;
12429 int nrows_scrolled, i;
12430 struct glyph_row *last_text_row;
12431 struct glyph_row *last_reused_text_row;
12432 struct glyph_row *start_row;
12433 int start_vpos, min_y, max_y;
12434
12435 #if GLYPH_DEBUG
12436 if (inhibit_try_window_reusing)
12437 return 0;
12438 #endif
12439
12440 if (/* This function doesn't handle terminal frames. */
12441 !FRAME_WINDOW_P (f)
12442 /* Don't try to reuse the display if windows have been split
12443 or such. */
12444 || windows_or_buffers_changed
12445 || cursor_type_changed)
12446 return 0;
12447
12448 /* Can't do this if region may have changed. */
12449 if ((!NILP (Vtransient_mark_mode)
12450 && !NILP (current_buffer->mark_active))
12451 || !NILP (w->region_showing)
12452 || !NILP (Vshow_trailing_whitespace))
12453 return 0;
12454
12455 /* If top-line visibility has changed, give up. */
12456 if (WINDOW_WANTS_HEADER_LINE_P (w)
12457 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12458 return 0;
12459
12460 /* Give up if old or new display is scrolled vertically. We could
12461 make this function handle this, but right now it doesn't. */
12462 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12463 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
12464 return 0;
12465
12466 /* The variable new_start now holds the new window start. The old
12467 start `start' can be determined from the current matrix. */
12468 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12469 start = start_row->start.pos;
12470 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12471
12472 /* Clear the desired matrix for the display below. */
12473 clear_glyph_matrix (w->desired_matrix);
12474
12475 if (CHARPOS (new_start) <= CHARPOS (start))
12476 {
12477 int first_row_y;
12478
12479 /* Don't use this method if the display starts with an ellipsis
12480 displayed for invisible text. It's not easy to handle that case
12481 below, and it's certainly not worth the effort since this is
12482 not a frequent case. */
12483 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12484 return 0;
12485
12486 IF_DEBUG (debug_method_add (w, "twu1"));
12487
12488 /* Display up to a row that can be reused. The variable
12489 last_text_row is set to the last row displayed that displays
12490 text. Note that it.vpos == 0 if or if not there is a
12491 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12492 start_display (&it, w, new_start);
12493 first_row_y = it.current_y;
12494 w->cursor.vpos = -1;
12495 last_text_row = last_reused_text_row = NULL;
12496
12497 while (it.current_y < it.last_visible_y
12498 && !fonts_changed_p)
12499 {
12500 /* If we have reached into the characters in the START row,
12501 that means the line boundaries have changed. So we
12502 can't start copying with the row START. Maybe it will
12503 work to start copying with the following row. */
12504 while (IT_CHARPOS (it) > CHARPOS (start))
12505 {
12506 /* Advance to the next row as the "start". */
12507 start_row++;
12508 start = start_row->start.pos;
12509 /* If there are no more rows to try, or just one, give up. */
12510 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
12511 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
12512 || CHARPOS (start) == ZV)
12513 {
12514 clear_glyph_matrix (w->desired_matrix);
12515 return 0;
12516 }
12517
12518 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12519 }
12520 /* If we have reached alignment,
12521 we can copy the rest of the rows. */
12522 if (IT_CHARPOS (it) == CHARPOS (start))
12523 break;
12524
12525 if (display_line (&it))
12526 last_text_row = it.glyph_row - 1;
12527 }
12528
12529 /* A value of current_y < last_visible_y means that we stopped
12530 at the previous window start, which in turn means that we
12531 have at least one reusable row. */
12532 if (it.current_y < it.last_visible_y)
12533 {
12534 /* IT.vpos always starts from 0; it counts text lines. */
12535 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
12536
12537 /* Find PT if not already found in the lines displayed. */
12538 if (w->cursor.vpos < 0)
12539 {
12540 int dy = it.current_y - start_row->y;
12541
12542 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12543 row = row_containing_pos (w, PT, row, NULL, dy);
12544 if (row)
12545 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12546 dy, nrows_scrolled);
12547 else
12548 {
12549 clear_glyph_matrix (w->desired_matrix);
12550 return 0;
12551 }
12552 }
12553
12554 /* Scroll the display. Do it before the current matrix is
12555 changed. The problem here is that update has not yet
12556 run, i.e. part of the current matrix is not up to date.
12557 scroll_run_hook will clear the cursor, and use the
12558 current matrix to get the height of the row the cursor is
12559 in. */
12560 run.current_y = start_row->y;
12561 run.desired_y = it.current_y;
12562 run.height = it.last_visible_y - it.current_y;
12563
12564 if (run.height > 0 && run.current_y != run.desired_y)
12565 {
12566 update_begin (f);
12567 rif->update_window_begin_hook (w);
12568 rif->clear_window_mouse_face (w);
12569 rif->scroll_run_hook (w, &run);
12570 rif->update_window_end_hook (w, 0, 0);
12571 update_end (f);
12572 }
12573
12574 /* Shift current matrix down by nrows_scrolled lines. */
12575 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12576 rotate_matrix (w->current_matrix,
12577 start_vpos,
12578 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12579 nrows_scrolled);
12580
12581 /* Disable lines that must be updated. */
12582 for (i = 0; i < it.vpos; ++i)
12583 (start_row + i)->enabled_p = 0;
12584
12585 /* Re-compute Y positions. */
12586 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12587 max_y = it.last_visible_y;
12588 for (row = start_row + nrows_scrolled;
12589 row < bottom_row;
12590 ++row)
12591 {
12592 row->y = it.current_y;
12593 row->visible_height = row->height;
12594
12595 if (row->y < min_y)
12596 row->visible_height -= min_y - row->y;
12597 if (row->y + row->height > max_y)
12598 row->visible_height -= row->y + row->height - max_y;
12599 row->redraw_fringe_bitmaps_p = 1;
12600
12601 it.current_y += row->height;
12602
12603 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12604 last_reused_text_row = row;
12605 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12606 break;
12607 }
12608
12609 /* Disable lines in the current matrix which are now
12610 below the window. */
12611 for (++row; row < bottom_row; ++row)
12612 row->enabled_p = 0;
12613 }
12614
12615 /* Update window_end_pos etc.; last_reused_text_row is the last
12616 reused row from the current matrix containing text, if any.
12617 The value of last_text_row is the last displayed line
12618 containing text. */
12619 if (last_reused_text_row)
12620 {
12621 w->window_end_bytepos
12622 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12623 w->window_end_pos
12624 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12625 w->window_end_vpos
12626 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12627 w->current_matrix));
12628 }
12629 else if (last_text_row)
12630 {
12631 w->window_end_bytepos
12632 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12633 w->window_end_pos
12634 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12635 w->window_end_vpos
12636 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12637 }
12638 else
12639 {
12640 /* This window must be completely empty. */
12641 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12642 w->window_end_pos = make_number (Z - ZV);
12643 w->window_end_vpos = make_number (0);
12644 }
12645 w->window_end_valid = Qnil;
12646
12647 /* Update hint: don't try scrolling again in update_window. */
12648 w->desired_matrix->no_scrolling_p = 1;
12649
12650 #if GLYPH_DEBUG
12651 debug_method_add (w, "try_window_reusing_current_matrix 1");
12652 #endif
12653 return 1;
12654 }
12655 else if (CHARPOS (new_start) > CHARPOS (start))
12656 {
12657 struct glyph_row *pt_row, *row;
12658 struct glyph_row *first_reusable_row;
12659 struct glyph_row *first_row_to_display;
12660 int dy;
12661 int yb = window_text_bottom_y (w);
12662
12663 /* Find the row starting at new_start, if there is one. Don't
12664 reuse a partially visible line at the end. */
12665 first_reusable_row = start_row;
12666 while (first_reusable_row->enabled_p
12667 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12668 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12669 < CHARPOS (new_start)))
12670 ++first_reusable_row;
12671
12672 /* Give up if there is no row to reuse. */
12673 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
12674 || !first_reusable_row->enabled_p
12675 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12676 != CHARPOS (new_start)))
12677 return 0;
12678
12679 /* We can reuse fully visible rows beginning with
12680 first_reusable_row to the end of the window. Set
12681 first_row_to_display to the first row that cannot be reused.
12682 Set pt_row to the row containing point, if there is any. */
12683 pt_row = NULL;
12684 for (first_row_to_display = first_reusable_row;
12685 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12686 ++first_row_to_display)
12687 {
12688 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12689 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
12690 pt_row = first_row_to_display;
12691 }
12692
12693 /* Start displaying at the start of first_row_to_display. */
12694 xassert (first_row_to_display->y < yb);
12695 init_to_row_start (&it, w, first_row_to_display);
12696
12697 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12698 - start_vpos);
12699 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12700 - nrows_scrolled);
12701 it.current_y = (first_row_to_display->y - first_reusable_row->y
12702 + WINDOW_HEADER_LINE_HEIGHT (w));
12703
12704 /* Display lines beginning with first_row_to_display in the
12705 desired matrix. Set last_text_row to the last row displayed
12706 that displays text. */
12707 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12708 if (pt_row == NULL)
12709 w->cursor.vpos = -1;
12710 last_text_row = NULL;
12711 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12712 if (display_line (&it))
12713 last_text_row = it.glyph_row - 1;
12714
12715 /* Give up If point isn't in a row displayed or reused. */
12716 if (w->cursor.vpos < 0)
12717 {
12718 clear_glyph_matrix (w->desired_matrix);
12719 return 0;
12720 }
12721
12722 /* If point is in a reused row, adjust y and vpos of the cursor
12723 position. */
12724 if (pt_row)
12725 {
12726 w->cursor.vpos -= nrows_scrolled;
12727 w->cursor.y -= first_reusable_row->y - start_row->y;
12728 }
12729
12730 /* Scroll the display. */
12731 run.current_y = first_reusable_row->y;
12732 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
12733 run.height = it.last_visible_y - run.current_y;
12734 dy = run.current_y - run.desired_y;
12735
12736 if (run.height)
12737 {
12738 update_begin (f);
12739 rif->update_window_begin_hook (w);
12740 rif->clear_window_mouse_face (w);
12741 rif->scroll_run_hook (w, &run);
12742 rif->update_window_end_hook (w, 0, 0);
12743 update_end (f);
12744 }
12745
12746 /* Adjust Y positions of reused rows. */
12747 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12748 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12749 max_y = it.last_visible_y;
12750 for (row = first_reusable_row; row < first_row_to_display; ++row)
12751 {
12752 row->y -= dy;
12753 row->visible_height = row->height;
12754 if (row->y < min_y)
12755 row->visible_height -= min_y - row->y;
12756 if (row->y + row->height > max_y)
12757 row->visible_height -= row->y + row->height - max_y;
12758 row->redraw_fringe_bitmaps_p = 1;
12759 }
12760
12761 /* Scroll the current matrix. */
12762 xassert (nrows_scrolled > 0);
12763 rotate_matrix (w->current_matrix,
12764 start_vpos,
12765 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12766 -nrows_scrolled);
12767
12768 /* Disable rows not reused. */
12769 for (row -= nrows_scrolled; row < bottom_row; ++row)
12770 row->enabled_p = 0;
12771
12772 /* Point may have moved to a different line, so we cannot assume that
12773 the previous cursor position is valid; locate the correct row. */
12774 if (pt_row)
12775 {
12776 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12777 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
12778 row++)
12779 {
12780 w->cursor.vpos++;
12781 w->cursor.y = row->y;
12782 }
12783 if (row < bottom_row)
12784 {
12785 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
12786 while (glyph->charpos < PT)
12787 {
12788 w->cursor.hpos++;
12789 w->cursor.x += glyph->pixel_width;
12790 glyph++;
12791 }
12792 }
12793 }
12794
12795 /* Adjust window end. A null value of last_text_row means that
12796 the window end is in reused rows which in turn means that
12797 only its vpos can have changed. */
12798 if (last_text_row)
12799 {
12800 w->window_end_bytepos
12801 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12802 w->window_end_pos
12803 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12804 w->window_end_vpos
12805 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12806 }
12807 else
12808 {
12809 w->window_end_vpos
12810 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
12811 }
12812
12813 w->window_end_valid = Qnil;
12814 w->desired_matrix->no_scrolling_p = 1;
12815
12816 #if GLYPH_DEBUG
12817 debug_method_add (w, "try_window_reusing_current_matrix 2");
12818 #endif
12819 return 1;
12820 }
12821
12822 return 0;
12823 }
12824
12825
12826 \f
12827 /************************************************************************
12828 Window redisplay reusing current matrix when buffer has changed
12829 ************************************************************************/
12830
12831 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
12832 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
12833 int *, int *));
12834 static struct glyph_row *
12835 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
12836 struct glyph_row *));
12837
12838
12839 /* Return the last row in MATRIX displaying text. If row START is
12840 non-null, start searching with that row. IT gives the dimensions
12841 of the display. Value is null if matrix is empty; otherwise it is
12842 a pointer to the row found. */
12843
12844 static struct glyph_row *
12845 find_last_row_displaying_text (matrix, it, start)
12846 struct glyph_matrix *matrix;
12847 struct it *it;
12848 struct glyph_row *start;
12849 {
12850 struct glyph_row *row, *row_found;
12851
12852 /* Set row_found to the last row in IT->w's current matrix
12853 displaying text. The loop looks funny but think of partially
12854 visible lines. */
12855 row_found = NULL;
12856 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
12857 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12858 {
12859 xassert (row->enabled_p);
12860 row_found = row;
12861 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
12862 break;
12863 ++row;
12864 }
12865
12866 return row_found;
12867 }
12868
12869
12870 /* Return the last row in the current matrix of W that is not affected
12871 by changes at the start of current_buffer that occurred since W's
12872 current matrix was built. Value is null if no such row exists.
12873
12874 BEG_UNCHANGED us the number of characters unchanged at the start of
12875 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
12876 first changed character in current_buffer. Characters at positions <
12877 BEG + BEG_UNCHANGED are at the same buffer positions as they were
12878 when the current matrix was built. */
12879
12880 static struct glyph_row *
12881 find_last_unchanged_at_beg_row (w)
12882 struct window *w;
12883 {
12884 int first_changed_pos = BEG + BEG_UNCHANGED;
12885 struct glyph_row *row;
12886 struct glyph_row *row_found = NULL;
12887 int yb = window_text_bottom_y (w);
12888
12889 /* Find the last row displaying unchanged text. */
12890 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12891 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12892 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
12893 {
12894 if (/* If row ends before first_changed_pos, it is unchanged,
12895 except in some case. */
12896 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
12897 /* When row ends in ZV and we write at ZV it is not
12898 unchanged. */
12899 && !row->ends_at_zv_p
12900 /* When first_changed_pos is the end of a continued line,
12901 row is not unchanged because it may be no longer
12902 continued. */
12903 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
12904 && (row->continued_p
12905 || row->exact_window_width_line_p)))
12906 row_found = row;
12907
12908 /* Stop if last visible row. */
12909 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
12910 break;
12911
12912 ++row;
12913 }
12914
12915 return row_found;
12916 }
12917
12918
12919 /* Find the first glyph row in the current matrix of W that is not
12920 affected by changes at the end of current_buffer since the
12921 time W's current matrix was built.
12922
12923 Return in *DELTA the number of chars by which buffer positions in
12924 unchanged text at the end of current_buffer must be adjusted.
12925
12926 Return in *DELTA_BYTES the corresponding number of bytes.
12927
12928 Value is null if no such row exists, i.e. all rows are affected by
12929 changes. */
12930
12931 static struct glyph_row *
12932 find_first_unchanged_at_end_row (w, delta, delta_bytes)
12933 struct window *w;
12934 int *delta, *delta_bytes;
12935 {
12936 struct glyph_row *row;
12937 struct glyph_row *row_found = NULL;
12938
12939 *delta = *delta_bytes = 0;
12940
12941 /* Display must not have been paused, otherwise the current matrix
12942 is not up to date. */
12943 if (NILP (w->window_end_valid))
12944 abort ();
12945
12946 /* A value of window_end_pos >= END_UNCHANGED means that the window
12947 end is in the range of changed text. If so, there is no
12948 unchanged row at the end of W's current matrix. */
12949 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
12950 return NULL;
12951
12952 /* Set row to the last row in W's current matrix displaying text. */
12953 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12954
12955 /* If matrix is entirely empty, no unchanged row exists. */
12956 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12957 {
12958 /* The value of row is the last glyph row in the matrix having a
12959 meaningful buffer position in it. The end position of row
12960 corresponds to window_end_pos. This allows us to translate
12961 buffer positions in the current matrix to current buffer
12962 positions for characters not in changed text. */
12963 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12964 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12965 int last_unchanged_pos, last_unchanged_pos_old;
12966 struct glyph_row *first_text_row
12967 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12968
12969 *delta = Z - Z_old;
12970 *delta_bytes = Z_BYTE - Z_BYTE_old;
12971
12972 /* Set last_unchanged_pos to the buffer position of the last
12973 character in the buffer that has not been changed. Z is the
12974 index + 1 of the last character in current_buffer, i.e. by
12975 subtracting END_UNCHANGED we get the index of the last
12976 unchanged character, and we have to add BEG to get its buffer
12977 position. */
12978 last_unchanged_pos = Z - END_UNCHANGED + BEG;
12979 last_unchanged_pos_old = last_unchanged_pos - *delta;
12980
12981 /* Search backward from ROW for a row displaying a line that
12982 starts at a minimum position >= last_unchanged_pos_old. */
12983 for (; row > first_text_row; --row)
12984 {
12985 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
12986 abort ();
12987
12988 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
12989 row_found = row;
12990 }
12991 }
12992
12993 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
12994 abort ();
12995
12996 return row_found;
12997 }
12998
12999
13000 /* Make sure that glyph rows in the current matrix of window W
13001 reference the same glyph memory as corresponding rows in the
13002 frame's frame matrix. This function is called after scrolling W's
13003 current matrix on a terminal frame in try_window_id and
13004 try_window_reusing_current_matrix. */
13005
13006 static void
13007 sync_frame_with_window_matrix_rows (w)
13008 struct window *w;
13009 {
13010 struct frame *f = XFRAME (w->frame);
13011 struct glyph_row *window_row, *window_row_end, *frame_row;
13012
13013 /* Preconditions: W must be a leaf window and full-width. Its frame
13014 must have a frame matrix. */
13015 xassert (NILP (w->hchild) && NILP (w->vchild));
13016 xassert (WINDOW_FULL_WIDTH_P (w));
13017 xassert (!FRAME_WINDOW_P (f));
13018
13019 /* If W is a full-width window, glyph pointers in W's current matrix
13020 have, by definition, to be the same as glyph pointers in the
13021 corresponding frame matrix. Note that frame matrices have no
13022 marginal areas (see build_frame_matrix). */
13023 window_row = w->current_matrix->rows;
13024 window_row_end = window_row + w->current_matrix->nrows;
13025 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
13026 while (window_row < window_row_end)
13027 {
13028 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
13029 struct glyph *end = window_row->glyphs[LAST_AREA];
13030
13031 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
13032 frame_row->glyphs[TEXT_AREA] = start;
13033 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
13034 frame_row->glyphs[LAST_AREA] = end;
13035
13036 /* Disable frame rows whose corresponding window rows have
13037 been disabled in try_window_id. */
13038 if (!window_row->enabled_p)
13039 frame_row->enabled_p = 0;
13040
13041 ++window_row, ++frame_row;
13042 }
13043 }
13044
13045
13046 /* Find the glyph row in window W containing CHARPOS. Consider all
13047 rows between START and END (not inclusive). END null means search
13048 all rows to the end of the display area of W. Value is the row
13049 containing CHARPOS or null. */
13050
13051 struct glyph_row *
13052 row_containing_pos (w, charpos, start, end, dy)
13053 struct window *w;
13054 int charpos;
13055 struct glyph_row *start, *end;
13056 int dy;
13057 {
13058 struct glyph_row *row = start;
13059 int last_y;
13060
13061 /* If we happen to start on a header-line, skip that. */
13062 if (row->mode_line_p)
13063 ++row;
13064
13065 if ((end && row >= end) || !row->enabled_p)
13066 return NULL;
13067
13068 last_y = window_text_bottom_y (w) - dy;
13069
13070 while (1)
13071 {
13072 /* Give up if we have gone too far. */
13073 if (end && row >= end)
13074 return NULL;
13075 /* This formerly returned if they were equal.
13076 I think that both quantities are of a "last plus one" type;
13077 if so, when they are equal, the row is within the screen. -- rms. */
13078 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13079 return NULL;
13080
13081 /* If it is in this row, return this row. */
13082 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13083 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13084 /* The end position of a row equals the start
13085 position of the next row. If CHARPOS is there, we
13086 would rather display it in the next line, except
13087 when this line ends in ZV. */
13088 && !row->ends_at_zv_p
13089 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13090 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13091 return row;
13092 ++row;
13093 }
13094 }
13095
13096
13097 /* Try to redisplay window W by reusing its existing display. W's
13098 current matrix must be up to date when this function is called,
13099 i.e. window_end_valid must not be nil.
13100
13101 Value is
13102
13103 1 if display has been updated
13104 0 if otherwise unsuccessful
13105 -1 if redisplay with same window start is known not to succeed
13106
13107 The following steps are performed:
13108
13109 1. Find the last row in the current matrix of W that is not
13110 affected by changes at the start of current_buffer. If no such row
13111 is found, give up.
13112
13113 2. Find the first row in W's current matrix that is not affected by
13114 changes at the end of current_buffer. Maybe there is no such row.
13115
13116 3. Display lines beginning with the row + 1 found in step 1 to the
13117 row found in step 2 or, if step 2 didn't find a row, to the end of
13118 the window.
13119
13120 4. If cursor is not known to appear on the window, give up.
13121
13122 5. If display stopped at the row found in step 2, scroll the
13123 display and current matrix as needed.
13124
13125 6. Maybe display some lines at the end of W, if we must. This can
13126 happen under various circumstances, like a partially visible line
13127 becoming fully visible, or because newly displayed lines are displayed
13128 in smaller font sizes.
13129
13130 7. Update W's window end information. */
13131
13132 static int
13133 try_window_id (w)
13134 struct window *w;
13135 {
13136 struct frame *f = XFRAME (w->frame);
13137 struct glyph_matrix *current_matrix = w->current_matrix;
13138 struct glyph_matrix *desired_matrix = w->desired_matrix;
13139 struct glyph_row *last_unchanged_at_beg_row;
13140 struct glyph_row *first_unchanged_at_end_row;
13141 struct glyph_row *row;
13142 struct glyph_row *bottom_row;
13143 int bottom_vpos;
13144 struct it it;
13145 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13146 struct text_pos start_pos;
13147 struct run run;
13148 int first_unchanged_at_end_vpos = 0;
13149 struct glyph_row *last_text_row, *last_text_row_at_end;
13150 struct text_pos start;
13151 int first_changed_charpos, last_changed_charpos;
13152
13153 #if GLYPH_DEBUG
13154 if (inhibit_try_window_id)
13155 return 0;
13156 #endif
13157
13158 /* This is handy for debugging. */
13159 #if 0
13160 #define GIVE_UP(X) \
13161 do { \
13162 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13163 return 0; \
13164 } while (0)
13165 #else
13166 #define GIVE_UP(X) return 0
13167 #endif
13168
13169 SET_TEXT_POS_FROM_MARKER (start, w->start);
13170
13171 /* Don't use this for mini-windows because these can show
13172 messages and mini-buffers, and we don't handle that here. */
13173 if (MINI_WINDOW_P (w))
13174 GIVE_UP (1);
13175
13176 /* This flag is used to prevent redisplay optimizations. */
13177 if (windows_or_buffers_changed || cursor_type_changed)
13178 GIVE_UP (2);
13179
13180 /* Verify that narrowing has not changed.
13181 Also verify that we were not told to prevent redisplay optimizations.
13182 It would be nice to further
13183 reduce the number of cases where this prevents try_window_id. */
13184 if (current_buffer->clip_changed
13185 || current_buffer->prevent_redisplay_optimizations_p)
13186 GIVE_UP (3);
13187
13188 /* Window must either use window-based redisplay or be full width. */
13189 if (!FRAME_WINDOW_P (f)
13190 && (!line_ins_del_ok
13191 || !WINDOW_FULL_WIDTH_P (w)))
13192 GIVE_UP (4);
13193
13194 /* Give up if point is not known NOT to appear in W. */
13195 if (PT < CHARPOS (start))
13196 GIVE_UP (5);
13197
13198 /* Another way to prevent redisplay optimizations. */
13199 if (XFASTINT (w->last_modified) == 0)
13200 GIVE_UP (6);
13201
13202 /* Verify that window is not hscrolled. */
13203 if (XFASTINT (w->hscroll) != 0)
13204 GIVE_UP (7);
13205
13206 /* Verify that display wasn't paused. */
13207 if (NILP (w->window_end_valid))
13208 GIVE_UP (8);
13209
13210 /* Can't use this if highlighting a region because a cursor movement
13211 will do more than just set the cursor. */
13212 if (!NILP (Vtransient_mark_mode)
13213 && !NILP (current_buffer->mark_active))
13214 GIVE_UP (9);
13215
13216 /* Likewise if highlighting trailing whitespace. */
13217 if (!NILP (Vshow_trailing_whitespace))
13218 GIVE_UP (11);
13219
13220 /* Likewise if showing a region. */
13221 if (!NILP (w->region_showing))
13222 GIVE_UP (10);
13223
13224 /* Can use this if overlay arrow position and or string have changed. */
13225 if (overlay_arrows_changed_p ())
13226 GIVE_UP (12);
13227
13228
13229 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13230 only if buffer has really changed. The reason is that the gap is
13231 initially at Z for freshly visited files. The code below would
13232 set end_unchanged to 0 in that case. */
13233 if (MODIFF > SAVE_MODIFF
13234 /* This seems to happen sometimes after saving a buffer. */
13235 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13236 {
13237 if (GPT - BEG < BEG_UNCHANGED)
13238 BEG_UNCHANGED = GPT - BEG;
13239 if (Z - GPT < END_UNCHANGED)
13240 END_UNCHANGED = Z - GPT;
13241 }
13242
13243 /* The position of the first and last character that has been changed. */
13244 first_changed_charpos = BEG + BEG_UNCHANGED;
13245 last_changed_charpos = Z - END_UNCHANGED;
13246
13247 /* If window starts after a line end, and the last change is in
13248 front of that newline, then changes don't affect the display.
13249 This case happens with stealth-fontification. Note that although
13250 the display is unchanged, glyph positions in the matrix have to
13251 be adjusted, of course. */
13252 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13253 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13254 && ((last_changed_charpos < CHARPOS (start)
13255 && CHARPOS (start) == BEGV)
13256 || (last_changed_charpos < CHARPOS (start) - 1
13257 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13258 {
13259 int Z_old, delta, Z_BYTE_old, delta_bytes;
13260 struct glyph_row *r0;
13261
13262 /* Compute how many chars/bytes have been added to or removed
13263 from the buffer. */
13264 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13265 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13266 delta = Z - Z_old;
13267 delta_bytes = Z_BYTE - Z_BYTE_old;
13268
13269 /* Give up if PT is not in the window. Note that it already has
13270 been checked at the start of try_window_id that PT is not in
13271 front of the window start. */
13272 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13273 GIVE_UP (13);
13274
13275 /* If window start is unchanged, we can reuse the whole matrix
13276 as is, after adjusting glyph positions. No need to compute
13277 the window end again, since its offset from Z hasn't changed. */
13278 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13279 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13280 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13281 /* PT must not be in a partially visible line. */
13282 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13283 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13284 {
13285 /* Adjust positions in the glyph matrix. */
13286 if (delta || delta_bytes)
13287 {
13288 struct glyph_row *r1
13289 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13290 increment_matrix_positions (w->current_matrix,
13291 MATRIX_ROW_VPOS (r0, current_matrix),
13292 MATRIX_ROW_VPOS (r1, current_matrix),
13293 delta, delta_bytes);
13294 }
13295
13296 /* Set the cursor. */
13297 row = row_containing_pos (w, PT, r0, NULL, 0);
13298 if (row)
13299 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13300 else
13301 abort ();
13302 return 1;
13303 }
13304 }
13305
13306 /* Handle the case that changes are all below what is displayed in
13307 the window, and that PT is in the window. This shortcut cannot
13308 be taken if ZV is visible in the window, and text has been added
13309 there that is visible in the window. */
13310 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13311 /* ZV is not visible in the window, or there are no
13312 changes at ZV, actually. */
13313 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13314 || first_changed_charpos == last_changed_charpos))
13315 {
13316 struct glyph_row *r0;
13317
13318 /* Give up if PT is not in the window. Note that it already has
13319 been checked at the start of try_window_id that PT is not in
13320 front of the window start. */
13321 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13322 GIVE_UP (14);
13323
13324 /* If window start is unchanged, we can reuse the whole matrix
13325 as is, without changing glyph positions since no text has
13326 been added/removed in front of the window end. */
13327 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13328 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13329 /* PT must not be in a partially visible line. */
13330 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13331 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13332 {
13333 /* We have to compute the window end anew since text
13334 can have been added/removed after it. */
13335 w->window_end_pos
13336 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13337 w->window_end_bytepos
13338 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13339
13340 /* Set the cursor. */
13341 row = row_containing_pos (w, PT, r0, NULL, 0);
13342 if (row)
13343 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13344 else
13345 abort ();
13346 return 2;
13347 }
13348 }
13349
13350 /* Give up if window start is in the changed area.
13351
13352 The condition used to read
13353
13354 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13355
13356 but why that was tested escapes me at the moment. */
13357 if (CHARPOS (start) >= first_changed_charpos
13358 && CHARPOS (start) <= last_changed_charpos)
13359 GIVE_UP (15);
13360
13361 /* Check that window start agrees with the start of the first glyph
13362 row in its current matrix. Check this after we know the window
13363 start is not in changed text, otherwise positions would not be
13364 comparable. */
13365 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13366 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13367 GIVE_UP (16);
13368
13369 /* Give up if the window ends in strings. Overlay strings
13370 at the end are difficult to handle, so don't try. */
13371 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13372 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13373 GIVE_UP (20);
13374
13375 /* Compute the position at which we have to start displaying new
13376 lines. Some of the lines at the top of the window might be
13377 reusable because they are not displaying changed text. Find the
13378 last row in W's current matrix not affected by changes at the
13379 start of current_buffer. Value is null if changes start in the
13380 first line of window. */
13381 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13382 if (last_unchanged_at_beg_row)
13383 {
13384 /* Avoid starting to display in the moddle of a character, a TAB
13385 for instance. This is easier than to set up the iterator
13386 exactly, and it's not a frequent case, so the additional
13387 effort wouldn't really pay off. */
13388 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13389 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13390 && last_unchanged_at_beg_row > w->current_matrix->rows)
13391 --last_unchanged_at_beg_row;
13392
13393 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13394 GIVE_UP (17);
13395
13396 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13397 GIVE_UP (18);
13398 start_pos = it.current.pos;
13399
13400 /* Start displaying new lines in the desired matrix at the same
13401 vpos we would use in the current matrix, i.e. below
13402 last_unchanged_at_beg_row. */
13403 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13404 current_matrix);
13405 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13406 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13407
13408 xassert (it.hpos == 0 && it.current_x == 0);
13409 }
13410 else
13411 {
13412 /* There are no reusable lines at the start of the window.
13413 Start displaying in the first text line. */
13414 start_display (&it, w, start);
13415 it.vpos = it.first_vpos;
13416 start_pos = it.current.pos;
13417 }
13418
13419 /* Find the first row that is not affected by changes at the end of
13420 the buffer. Value will be null if there is no unchanged row, in
13421 which case we must redisplay to the end of the window. delta
13422 will be set to the value by which buffer positions beginning with
13423 first_unchanged_at_end_row have to be adjusted due to text
13424 changes. */
13425 first_unchanged_at_end_row
13426 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13427 IF_DEBUG (debug_delta = delta);
13428 IF_DEBUG (debug_delta_bytes = delta_bytes);
13429
13430 /* Set stop_pos to the buffer position up to which we will have to
13431 display new lines. If first_unchanged_at_end_row != NULL, this
13432 is the buffer position of the start of the line displayed in that
13433 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13434 that we don't stop at a buffer position. */
13435 stop_pos = 0;
13436 if (first_unchanged_at_end_row)
13437 {
13438 xassert (last_unchanged_at_beg_row == NULL
13439 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13440
13441 /* If this is a continuation line, move forward to the next one
13442 that isn't. Changes in lines above affect this line.
13443 Caution: this may move first_unchanged_at_end_row to a row
13444 not displaying text. */
13445 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13446 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13447 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13448 < it.last_visible_y))
13449 ++first_unchanged_at_end_row;
13450
13451 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13452 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13453 >= it.last_visible_y))
13454 first_unchanged_at_end_row = NULL;
13455 else
13456 {
13457 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13458 + delta);
13459 first_unchanged_at_end_vpos
13460 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13461 xassert (stop_pos >= Z - END_UNCHANGED);
13462 }
13463 }
13464 else if (last_unchanged_at_beg_row == NULL)
13465 GIVE_UP (19);
13466
13467
13468 #if GLYPH_DEBUG
13469
13470 /* Either there is no unchanged row at the end, or the one we have
13471 now displays text. This is a necessary condition for the window
13472 end pos calculation at the end of this function. */
13473 xassert (first_unchanged_at_end_row == NULL
13474 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13475
13476 debug_last_unchanged_at_beg_vpos
13477 = (last_unchanged_at_beg_row
13478 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13479 : -1);
13480 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13481
13482 #endif /* GLYPH_DEBUG != 0 */
13483
13484
13485 /* Display new lines. Set last_text_row to the last new line
13486 displayed which has text on it, i.e. might end up as being the
13487 line where the window_end_vpos is. */
13488 w->cursor.vpos = -1;
13489 last_text_row = NULL;
13490 overlay_arrow_seen = 0;
13491 while (it.current_y < it.last_visible_y
13492 && !fonts_changed_p
13493 && (first_unchanged_at_end_row == NULL
13494 || IT_CHARPOS (it) < stop_pos))
13495 {
13496 if (display_line (&it))
13497 last_text_row = it.glyph_row - 1;
13498 }
13499
13500 if (fonts_changed_p)
13501 return -1;
13502
13503
13504 /* Compute differences in buffer positions, y-positions etc. for
13505 lines reused at the bottom of the window. Compute what we can
13506 scroll. */
13507 if (first_unchanged_at_end_row
13508 /* No lines reused because we displayed everything up to the
13509 bottom of the window. */
13510 && it.current_y < it.last_visible_y)
13511 {
13512 dvpos = (it.vpos
13513 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13514 current_matrix));
13515 dy = it.current_y - first_unchanged_at_end_row->y;
13516 run.current_y = first_unchanged_at_end_row->y;
13517 run.desired_y = run.current_y + dy;
13518 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13519 }
13520 else
13521 {
13522 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13523 first_unchanged_at_end_row = NULL;
13524 }
13525 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13526
13527
13528 /* Find the cursor if not already found. We have to decide whether
13529 PT will appear on this window (it sometimes doesn't, but this is
13530 not a very frequent case.) This decision has to be made before
13531 the current matrix is altered. A value of cursor.vpos < 0 means
13532 that PT is either in one of the lines beginning at
13533 first_unchanged_at_end_row or below the window. Don't care for
13534 lines that might be displayed later at the window end; as
13535 mentioned, this is not a frequent case. */
13536 if (w->cursor.vpos < 0)
13537 {
13538 /* Cursor in unchanged rows at the top? */
13539 if (PT < CHARPOS (start_pos)
13540 && last_unchanged_at_beg_row)
13541 {
13542 row = row_containing_pos (w, PT,
13543 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13544 last_unchanged_at_beg_row + 1, 0);
13545 if (row)
13546 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13547 }
13548
13549 /* Start from first_unchanged_at_end_row looking for PT. */
13550 else if (first_unchanged_at_end_row)
13551 {
13552 row = row_containing_pos (w, PT - delta,
13553 first_unchanged_at_end_row, NULL, 0);
13554 if (row)
13555 set_cursor_from_row (w, row, w->current_matrix, delta,
13556 delta_bytes, dy, dvpos);
13557 }
13558
13559 /* Give up if cursor was not found. */
13560 if (w->cursor.vpos < 0)
13561 {
13562 clear_glyph_matrix (w->desired_matrix);
13563 return -1;
13564 }
13565 }
13566
13567 /* Don't let the cursor end in the scroll margins. */
13568 {
13569 int this_scroll_margin, cursor_height;
13570
13571 this_scroll_margin = max (0, scroll_margin);
13572 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13573 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13574 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13575
13576 if ((w->cursor.y < this_scroll_margin
13577 && CHARPOS (start) > BEGV)
13578 /* Old redisplay didn't take scroll margin into account at the bottom,
13579 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
13580 || (w->cursor.y + (make_cursor_line_fully_visible_p
13581 ? cursor_height + this_scroll_margin
13582 : 1)) > it.last_visible_y)
13583 {
13584 w->cursor.vpos = -1;
13585 clear_glyph_matrix (w->desired_matrix);
13586 return -1;
13587 }
13588 }
13589
13590 /* Scroll the display. Do it before changing the current matrix so
13591 that xterm.c doesn't get confused about where the cursor glyph is
13592 found. */
13593 if (dy && run.height)
13594 {
13595 update_begin (f);
13596
13597 if (FRAME_WINDOW_P (f))
13598 {
13599 rif->update_window_begin_hook (w);
13600 rif->clear_window_mouse_face (w);
13601 rif->scroll_run_hook (w, &run);
13602 rif->update_window_end_hook (w, 0, 0);
13603 }
13604 else
13605 {
13606 /* Terminal frame. In this case, dvpos gives the number of
13607 lines to scroll by; dvpos < 0 means scroll up. */
13608 int first_unchanged_at_end_vpos
13609 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13610 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13611 int end = (WINDOW_TOP_EDGE_LINE (w)
13612 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13613 + window_internal_height (w));
13614
13615 /* Perform the operation on the screen. */
13616 if (dvpos > 0)
13617 {
13618 /* Scroll last_unchanged_at_beg_row to the end of the
13619 window down dvpos lines. */
13620 set_terminal_window (end);
13621
13622 /* On dumb terminals delete dvpos lines at the end
13623 before inserting dvpos empty lines. */
13624 if (!scroll_region_ok)
13625 ins_del_lines (end - dvpos, -dvpos);
13626
13627 /* Insert dvpos empty lines in front of
13628 last_unchanged_at_beg_row. */
13629 ins_del_lines (from, dvpos);
13630 }
13631 else if (dvpos < 0)
13632 {
13633 /* Scroll up last_unchanged_at_beg_vpos to the end of
13634 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13635 set_terminal_window (end);
13636
13637 /* Delete dvpos lines in front of
13638 last_unchanged_at_beg_vpos. ins_del_lines will set
13639 the cursor to the given vpos and emit |dvpos| delete
13640 line sequences. */
13641 ins_del_lines (from + dvpos, dvpos);
13642
13643 /* On a dumb terminal insert dvpos empty lines at the
13644 end. */
13645 if (!scroll_region_ok)
13646 ins_del_lines (end + dvpos, -dvpos);
13647 }
13648
13649 set_terminal_window (0);
13650 }
13651
13652 update_end (f);
13653 }
13654
13655 /* Shift reused rows of the current matrix to the right position.
13656 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13657 text. */
13658 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13659 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
13660 if (dvpos < 0)
13661 {
13662 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13663 bottom_vpos, dvpos);
13664 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13665 bottom_vpos, 0);
13666 }
13667 else if (dvpos > 0)
13668 {
13669 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13670 bottom_vpos, dvpos);
13671 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13672 first_unchanged_at_end_vpos + dvpos, 0);
13673 }
13674
13675 /* For frame-based redisplay, make sure that current frame and window
13676 matrix are in sync with respect to glyph memory. */
13677 if (!FRAME_WINDOW_P (f))
13678 sync_frame_with_window_matrix_rows (w);
13679
13680 /* Adjust buffer positions in reused rows. */
13681 if (delta)
13682 increment_matrix_positions (current_matrix,
13683 first_unchanged_at_end_vpos + dvpos,
13684 bottom_vpos, delta, delta_bytes);
13685
13686 /* Adjust Y positions. */
13687 if (dy)
13688 shift_glyph_matrix (w, current_matrix,
13689 first_unchanged_at_end_vpos + dvpos,
13690 bottom_vpos, dy);
13691
13692 if (first_unchanged_at_end_row)
13693 first_unchanged_at_end_row += dvpos;
13694
13695 /* If scrolling up, there may be some lines to display at the end of
13696 the window. */
13697 last_text_row_at_end = NULL;
13698 if (dy < 0)
13699 {
13700 /* Scrolling up can leave for example a partially visible line
13701 at the end of the window to be redisplayed. */
13702 /* Set last_row to the glyph row in the current matrix where the
13703 window end line is found. It has been moved up or down in
13704 the matrix by dvpos. */
13705 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13706 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13707
13708 /* If last_row is the window end line, it should display text. */
13709 xassert (last_row->displays_text_p);
13710
13711 /* If window end line was partially visible before, begin
13712 displaying at that line. Otherwise begin displaying with the
13713 line following it. */
13714 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13715 {
13716 init_to_row_start (&it, w, last_row);
13717 it.vpos = last_vpos;
13718 it.current_y = last_row->y;
13719 }
13720 else
13721 {
13722 init_to_row_end (&it, w, last_row);
13723 it.vpos = 1 + last_vpos;
13724 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13725 ++last_row;
13726 }
13727
13728 /* We may start in a continuation line. If so, we have to
13729 get the right continuation_lines_width and current_x. */
13730 it.continuation_lines_width = last_row->continuation_lines_width;
13731 it.hpos = it.current_x = 0;
13732
13733 /* Display the rest of the lines at the window end. */
13734 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13735 while (it.current_y < it.last_visible_y
13736 && !fonts_changed_p)
13737 {
13738 /* Is it always sure that the display agrees with lines in
13739 the current matrix? I don't think so, so we mark rows
13740 displayed invalid in the current matrix by setting their
13741 enabled_p flag to zero. */
13742 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13743 if (display_line (&it))
13744 last_text_row_at_end = it.glyph_row - 1;
13745 }
13746 }
13747
13748 /* Update window_end_pos and window_end_vpos. */
13749 if (first_unchanged_at_end_row
13750 && first_unchanged_at_end_row->y < it.last_visible_y
13751 && !last_text_row_at_end)
13752 {
13753 /* Window end line if one of the preserved rows from the current
13754 matrix. Set row to the last row displaying text in current
13755 matrix starting at first_unchanged_at_end_row, after
13756 scrolling. */
13757 xassert (first_unchanged_at_end_row->displays_text_p);
13758 row = find_last_row_displaying_text (w->current_matrix, &it,
13759 first_unchanged_at_end_row);
13760 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
13761
13762 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13763 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13764 w->window_end_vpos
13765 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
13766 xassert (w->window_end_bytepos >= 0);
13767 IF_DEBUG (debug_method_add (w, "A"));
13768 }
13769 else if (last_text_row_at_end)
13770 {
13771 w->window_end_pos
13772 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
13773 w->window_end_bytepos
13774 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
13775 w->window_end_vpos
13776 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
13777 xassert (w->window_end_bytepos >= 0);
13778 IF_DEBUG (debug_method_add (w, "B"));
13779 }
13780 else if (last_text_row)
13781 {
13782 /* We have displayed either to the end of the window or at the
13783 end of the window, i.e. the last row with text is to be found
13784 in the desired matrix. */
13785 w->window_end_pos
13786 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13787 w->window_end_bytepos
13788 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13789 w->window_end_vpos
13790 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
13791 xassert (w->window_end_bytepos >= 0);
13792 }
13793 else if (first_unchanged_at_end_row == NULL
13794 && last_text_row == NULL
13795 && last_text_row_at_end == NULL)
13796 {
13797 /* Displayed to end of window, but no line containing text was
13798 displayed. Lines were deleted at the end of the window. */
13799 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
13800 int vpos = XFASTINT (w->window_end_vpos);
13801 struct glyph_row *current_row = current_matrix->rows + vpos;
13802 struct glyph_row *desired_row = desired_matrix->rows + vpos;
13803
13804 for (row = NULL;
13805 row == NULL && vpos >= first_vpos;
13806 --vpos, --current_row, --desired_row)
13807 {
13808 if (desired_row->enabled_p)
13809 {
13810 if (desired_row->displays_text_p)
13811 row = desired_row;
13812 }
13813 else if (current_row->displays_text_p)
13814 row = current_row;
13815 }
13816
13817 xassert (row != NULL);
13818 w->window_end_vpos = make_number (vpos + 1);
13819 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13820 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13821 xassert (w->window_end_bytepos >= 0);
13822 IF_DEBUG (debug_method_add (w, "C"));
13823 }
13824 else
13825 abort ();
13826
13827 #if 0 /* This leads to problems, for instance when the cursor is
13828 at ZV, and the cursor line displays no text. */
13829 /* Disable rows below what's displayed in the window. This makes
13830 debugging easier. */
13831 enable_glyph_matrix_rows (current_matrix,
13832 XFASTINT (w->window_end_vpos) + 1,
13833 bottom_vpos, 0);
13834 #endif
13835
13836 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
13837 debug_end_vpos = XFASTINT (w->window_end_vpos));
13838
13839 /* Record that display has not been completed. */
13840 w->window_end_valid = Qnil;
13841 w->desired_matrix->no_scrolling_p = 1;
13842 return 3;
13843
13844 #undef GIVE_UP
13845 }
13846
13847
13848 \f
13849 /***********************************************************************
13850 More debugging support
13851 ***********************************************************************/
13852
13853 #if GLYPH_DEBUG
13854
13855 void dump_glyph_row P_ ((struct glyph_row *, int, int));
13856 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
13857 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
13858
13859
13860 /* Dump the contents of glyph matrix MATRIX on stderr.
13861
13862 GLYPHS 0 means don't show glyph contents.
13863 GLYPHS 1 means show glyphs in short form
13864 GLYPHS > 1 means show glyphs in long form. */
13865
13866 void
13867 dump_glyph_matrix (matrix, glyphs)
13868 struct glyph_matrix *matrix;
13869 int glyphs;
13870 {
13871 int i;
13872 for (i = 0; i < matrix->nrows; ++i)
13873 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
13874 }
13875
13876
13877 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
13878 the glyph row and area where the glyph comes from. */
13879
13880 void
13881 dump_glyph (row, glyph, area)
13882 struct glyph_row *row;
13883 struct glyph *glyph;
13884 int area;
13885 {
13886 if (glyph->type == CHAR_GLYPH)
13887 {
13888 fprintf (stderr,
13889 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13890 glyph - row->glyphs[TEXT_AREA],
13891 'C',
13892 glyph->charpos,
13893 (BUFFERP (glyph->object)
13894 ? 'B'
13895 : (STRINGP (glyph->object)
13896 ? 'S'
13897 : '-')),
13898 glyph->pixel_width,
13899 glyph->u.ch,
13900 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
13901 ? glyph->u.ch
13902 : '.'),
13903 glyph->face_id,
13904 glyph->left_box_line_p,
13905 glyph->right_box_line_p);
13906 }
13907 else if (glyph->type == STRETCH_GLYPH)
13908 {
13909 fprintf (stderr,
13910 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13911 glyph - row->glyphs[TEXT_AREA],
13912 'S',
13913 glyph->charpos,
13914 (BUFFERP (glyph->object)
13915 ? 'B'
13916 : (STRINGP (glyph->object)
13917 ? 'S'
13918 : '-')),
13919 glyph->pixel_width,
13920 0,
13921 '.',
13922 glyph->face_id,
13923 glyph->left_box_line_p,
13924 glyph->right_box_line_p);
13925 }
13926 else if (glyph->type == IMAGE_GLYPH)
13927 {
13928 fprintf (stderr,
13929 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13930 glyph - row->glyphs[TEXT_AREA],
13931 'I',
13932 glyph->charpos,
13933 (BUFFERP (glyph->object)
13934 ? 'B'
13935 : (STRINGP (glyph->object)
13936 ? 'S'
13937 : '-')),
13938 glyph->pixel_width,
13939 glyph->u.img_id,
13940 '.',
13941 glyph->face_id,
13942 glyph->left_box_line_p,
13943 glyph->right_box_line_p);
13944 }
13945 }
13946
13947
13948 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
13949 GLYPHS 0 means don't show glyph contents.
13950 GLYPHS 1 means show glyphs in short form
13951 GLYPHS > 1 means show glyphs in long form. */
13952
13953 void
13954 dump_glyph_row (row, vpos, glyphs)
13955 struct glyph_row *row;
13956 int vpos, glyphs;
13957 {
13958 if (glyphs != 1)
13959 {
13960 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
13961 fprintf (stderr, "=======================================================================\n");
13962
13963 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
13964 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
13965 vpos,
13966 MATRIX_ROW_START_CHARPOS (row),
13967 MATRIX_ROW_END_CHARPOS (row),
13968 row->used[TEXT_AREA],
13969 row->contains_overlapping_glyphs_p,
13970 row->enabled_p,
13971 row->truncated_on_left_p,
13972 row->truncated_on_right_p,
13973 row->overlay_arrow_p,
13974 row->continued_p,
13975 MATRIX_ROW_CONTINUATION_LINE_P (row),
13976 row->displays_text_p,
13977 row->ends_at_zv_p,
13978 row->fill_line_p,
13979 row->ends_in_middle_of_char_p,
13980 row->starts_in_middle_of_char_p,
13981 row->mouse_face_p,
13982 row->x,
13983 row->y,
13984 row->pixel_width,
13985 row->height,
13986 row->visible_height,
13987 row->ascent,
13988 row->phys_ascent);
13989 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
13990 row->end.overlay_string_index,
13991 row->continuation_lines_width);
13992 fprintf (stderr, "%9d %5d\n",
13993 CHARPOS (row->start.string_pos),
13994 CHARPOS (row->end.string_pos));
13995 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
13996 row->end.dpvec_index);
13997 }
13998
13999 if (glyphs > 1)
14000 {
14001 int area;
14002
14003 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14004 {
14005 struct glyph *glyph = row->glyphs[area];
14006 struct glyph *glyph_end = glyph + row->used[area];
14007
14008 /* Glyph for a line end in text. */
14009 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
14010 ++glyph_end;
14011
14012 if (glyph < glyph_end)
14013 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
14014
14015 for (; glyph < glyph_end; ++glyph)
14016 dump_glyph (row, glyph, area);
14017 }
14018 }
14019 else if (glyphs == 1)
14020 {
14021 int area;
14022
14023 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14024 {
14025 char *s = (char *) alloca (row->used[area] + 1);
14026 int i;
14027
14028 for (i = 0; i < row->used[area]; ++i)
14029 {
14030 struct glyph *glyph = row->glyphs[area] + i;
14031 if (glyph->type == CHAR_GLYPH
14032 && glyph->u.ch < 0x80
14033 && glyph->u.ch >= ' ')
14034 s[i] = glyph->u.ch;
14035 else
14036 s[i] = '.';
14037 }
14038
14039 s[i] = '\0';
14040 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
14041 }
14042 }
14043 }
14044
14045
14046 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
14047 Sdump_glyph_matrix, 0, 1, "p",
14048 doc: /* Dump the current matrix of the selected window to stderr.
14049 Shows contents of glyph row structures. With non-nil
14050 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
14051 glyphs in short form, otherwise show glyphs in long form. */)
14052 (glyphs)
14053 Lisp_Object glyphs;
14054 {
14055 struct window *w = XWINDOW (selected_window);
14056 struct buffer *buffer = XBUFFER (w->buffer);
14057
14058 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14059 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14060 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14061 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14062 fprintf (stderr, "=============================================\n");
14063 dump_glyph_matrix (w->current_matrix,
14064 NILP (glyphs) ? 0 : XINT (glyphs));
14065 return Qnil;
14066 }
14067
14068
14069 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14070 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14071 ()
14072 {
14073 struct frame *f = XFRAME (selected_frame);
14074 dump_glyph_matrix (f->current_matrix, 1);
14075 return Qnil;
14076 }
14077
14078
14079 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
14080 doc: /* Dump glyph row ROW to stderr.
14081 GLYPH 0 means don't dump glyphs.
14082 GLYPH 1 means dump glyphs in short form.
14083 GLYPH > 1 or omitted means dump glyphs in long form. */)
14084 (row, glyphs)
14085 Lisp_Object row, glyphs;
14086 {
14087 struct glyph_matrix *matrix;
14088 int vpos;
14089
14090 CHECK_NUMBER (row);
14091 matrix = XWINDOW (selected_window)->current_matrix;
14092 vpos = XINT (row);
14093 if (vpos >= 0 && vpos < matrix->nrows)
14094 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14095 vpos,
14096 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14097 return Qnil;
14098 }
14099
14100
14101 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14102 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14103 GLYPH 0 means don't dump glyphs.
14104 GLYPH 1 means dump glyphs in short form.
14105 GLYPH > 1 or omitted means dump glyphs in long form. */)
14106 (row, glyphs)
14107 Lisp_Object row, glyphs;
14108 {
14109 struct frame *sf = SELECTED_FRAME ();
14110 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14111 int vpos;
14112
14113 CHECK_NUMBER (row);
14114 vpos = XINT (row);
14115 if (vpos >= 0 && vpos < m->nrows)
14116 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14117 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14118 return Qnil;
14119 }
14120
14121
14122 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14123 doc: /* Toggle tracing of redisplay.
14124 With ARG, turn tracing on if and only if ARG is positive. */)
14125 (arg)
14126 Lisp_Object arg;
14127 {
14128 if (NILP (arg))
14129 trace_redisplay_p = !trace_redisplay_p;
14130 else
14131 {
14132 arg = Fprefix_numeric_value (arg);
14133 trace_redisplay_p = XINT (arg) > 0;
14134 }
14135
14136 return Qnil;
14137 }
14138
14139
14140 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14141 doc: /* Like `format', but print result to stderr.
14142 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14143 (nargs, args)
14144 int nargs;
14145 Lisp_Object *args;
14146 {
14147 Lisp_Object s = Fformat (nargs, args);
14148 fprintf (stderr, "%s", SDATA (s));
14149 return Qnil;
14150 }
14151
14152 #endif /* GLYPH_DEBUG */
14153
14154
14155 \f
14156 /***********************************************************************
14157 Building Desired Matrix Rows
14158 ***********************************************************************/
14159
14160 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
14161 Used for non-window-redisplay windows, and for windows w/o left fringe. */
14162
14163 static struct glyph_row *
14164 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14165 struct window *w;
14166 Lisp_Object overlay_arrow_string;
14167 {
14168 struct frame *f = XFRAME (WINDOW_FRAME (w));
14169 struct buffer *buffer = XBUFFER (w->buffer);
14170 struct buffer *old = current_buffer;
14171 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14172 int arrow_len = SCHARS (overlay_arrow_string);
14173 const unsigned char *arrow_end = arrow_string + arrow_len;
14174 const unsigned char *p;
14175 struct it it;
14176 int multibyte_p;
14177 int n_glyphs_before;
14178
14179 set_buffer_temp (buffer);
14180 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14181 it.glyph_row->used[TEXT_AREA] = 0;
14182 SET_TEXT_POS (it.position, 0, 0);
14183
14184 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14185 p = arrow_string;
14186 while (p < arrow_end)
14187 {
14188 Lisp_Object face, ilisp;
14189
14190 /* Get the next character. */
14191 if (multibyte_p)
14192 it.c = string_char_and_length (p, arrow_len, &it.len);
14193 else
14194 it.c = *p, it.len = 1;
14195 p += it.len;
14196
14197 /* Get its face. */
14198 ilisp = make_number (p - arrow_string);
14199 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
14200 it.face_id = compute_char_face (f, it.c, face);
14201
14202 /* Compute its width, get its glyphs. */
14203 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14204 SET_TEXT_POS (it.position, -1, -1);
14205 PRODUCE_GLYPHS (&it);
14206
14207 /* If this character doesn't fit any more in the line, we have
14208 to remove some glyphs. */
14209 if (it.current_x > it.last_visible_x)
14210 {
14211 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14212 break;
14213 }
14214 }
14215
14216 set_buffer_temp (old);
14217 return it.glyph_row;
14218 }
14219
14220
14221 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14222 glyphs are only inserted for terminal frames since we can't really
14223 win with truncation glyphs when partially visible glyphs are
14224 involved. Which glyphs to insert is determined by
14225 produce_special_glyphs. */
14226
14227 static void
14228 insert_left_trunc_glyphs (it)
14229 struct it *it;
14230 {
14231 struct it truncate_it;
14232 struct glyph *from, *end, *to, *toend;
14233
14234 xassert (!FRAME_WINDOW_P (it->f));
14235
14236 /* Get the truncation glyphs. */
14237 truncate_it = *it;
14238 truncate_it.current_x = 0;
14239 truncate_it.face_id = DEFAULT_FACE_ID;
14240 truncate_it.glyph_row = &scratch_glyph_row;
14241 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14242 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14243 truncate_it.object = make_number (0);
14244 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14245
14246 /* Overwrite glyphs from IT with truncation glyphs. */
14247 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14248 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14249 to = it->glyph_row->glyphs[TEXT_AREA];
14250 toend = to + it->glyph_row->used[TEXT_AREA];
14251
14252 while (from < end)
14253 *to++ = *from++;
14254
14255 /* There may be padding glyphs left over. Overwrite them too. */
14256 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14257 {
14258 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14259 while (from < end)
14260 *to++ = *from++;
14261 }
14262
14263 if (to > toend)
14264 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14265 }
14266
14267
14268 /* Compute the pixel height and width of IT->glyph_row.
14269
14270 Most of the time, ascent and height of a display line will be equal
14271 to the max_ascent and max_height values of the display iterator
14272 structure. This is not the case if
14273
14274 1. We hit ZV without displaying anything. In this case, max_ascent
14275 and max_height will be zero.
14276
14277 2. We have some glyphs that don't contribute to the line height.
14278 (The glyph row flag contributes_to_line_height_p is for future
14279 pixmap extensions).
14280
14281 The first case is easily covered by using default values because in
14282 these cases, the line height does not really matter, except that it
14283 must not be zero. */
14284
14285 static void
14286 compute_line_metrics (it)
14287 struct it *it;
14288 {
14289 struct glyph_row *row = it->glyph_row;
14290 int area, i;
14291
14292 if (FRAME_WINDOW_P (it->f))
14293 {
14294 int i, min_y, max_y;
14295
14296 /* The line may consist of one space only, that was added to
14297 place the cursor on it. If so, the row's height hasn't been
14298 computed yet. */
14299 if (row->height == 0)
14300 {
14301 if (it->max_ascent + it->max_descent == 0)
14302 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14303 row->ascent = it->max_ascent;
14304 row->height = it->max_ascent + it->max_descent;
14305 row->phys_ascent = it->max_phys_ascent;
14306 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14307 row->extra_line_spacing = it->max_extra_line_spacing;
14308 }
14309
14310 /* Compute the width of this line. */
14311 row->pixel_width = row->x;
14312 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14313 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14314
14315 xassert (row->pixel_width >= 0);
14316 xassert (row->ascent >= 0 && row->height > 0);
14317
14318 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14319 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14320
14321 /* If first line's physical ascent is larger than its logical
14322 ascent, use the physical ascent, and make the row taller.
14323 This makes accented characters fully visible. */
14324 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14325 && row->phys_ascent > row->ascent)
14326 {
14327 row->height += row->phys_ascent - row->ascent;
14328 row->ascent = row->phys_ascent;
14329 }
14330
14331 /* Compute how much of the line is visible. */
14332 row->visible_height = row->height;
14333
14334 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14335 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14336
14337 if (row->y < min_y)
14338 row->visible_height -= min_y - row->y;
14339 if (row->y + row->height > max_y)
14340 row->visible_height -= row->y + row->height - max_y;
14341 }
14342 else
14343 {
14344 row->pixel_width = row->used[TEXT_AREA];
14345 if (row->continued_p)
14346 row->pixel_width -= it->continuation_pixel_width;
14347 else if (row->truncated_on_right_p)
14348 row->pixel_width -= it->truncation_pixel_width;
14349 row->ascent = row->phys_ascent = 0;
14350 row->height = row->phys_height = row->visible_height = 1;
14351 row->extra_line_spacing = 0;
14352 }
14353
14354 /* Compute a hash code for this row. */
14355 row->hash = 0;
14356 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14357 for (i = 0; i < row->used[area]; ++i)
14358 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14359 + row->glyphs[area][i].u.val
14360 + row->glyphs[area][i].face_id
14361 + row->glyphs[area][i].padding_p
14362 + (row->glyphs[area][i].type << 2));
14363
14364 it->max_ascent = it->max_descent = 0;
14365 it->max_phys_ascent = it->max_phys_descent = 0;
14366 }
14367
14368
14369 /* Append one space to the glyph row of iterator IT if doing a
14370 window-based redisplay. The space has the same face as
14371 IT->face_id. Value is non-zero if a space was added.
14372
14373 This function is called to make sure that there is always one glyph
14374 at the end of a glyph row that the cursor can be set on under
14375 window-systems. (If there weren't such a glyph we would not know
14376 how wide and tall a box cursor should be displayed).
14377
14378 At the same time this space let's a nicely handle clearing to the
14379 end of the line if the row ends in italic text. */
14380
14381 static int
14382 append_space_for_newline (it, default_face_p)
14383 struct it *it;
14384 int default_face_p;
14385 {
14386 if (FRAME_WINDOW_P (it->f))
14387 {
14388 int n = it->glyph_row->used[TEXT_AREA];
14389
14390 if (it->glyph_row->glyphs[TEXT_AREA] + n
14391 < it->glyph_row->glyphs[1 + TEXT_AREA])
14392 {
14393 /* Save some values that must not be changed.
14394 Must save IT->c and IT->len because otherwise
14395 ITERATOR_AT_END_P wouldn't work anymore after
14396 append_space_for_newline has been called. */
14397 enum display_element_type saved_what = it->what;
14398 int saved_c = it->c, saved_len = it->len;
14399 int saved_x = it->current_x;
14400 int saved_face_id = it->face_id;
14401 struct text_pos saved_pos;
14402 Lisp_Object saved_object;
14403 struct face *face;
14404
14405 saved_object = it->object;
14406 saved_pos = it->position;
14407
14408 it->what = IT_CHARACTER;
14409 bzero (&it->position, sizeof it->position);
14410 it->object = make_number (0);
14411 it->c = ' ';
14412 it->len = 1;
14413
14414 if (default_face_p)
14415 it->face_id = DEFAULT_FACE_ID;
14416 else if (it->face_before_selective_p)
14417 it->face_id = it->saved_face_id;
14418 face = FACE_FROM_ID (it->f, it->face_id);
14419 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14420
14421 PRODUCE_GLYPHS (it);
14422
14423 it->override_ascent = -1;
14424 it->constrain_row_ascent_descent_p = 0;
14425 it->current_x = saved_x;
14426 it->object = saved_object;
14427 it->position = saved_pos;
14428 it->what = saved_what;
14429 it->face_id = saved_face_id;
14430 it->len = saved_len;
14431 it->c = saved_c;
14432 return 1;
14433 }
14434 }
14435
14436 return 0;
14437 }
14438
14439
14440 /* Extend the face of the last glyph in the text area of IT->glyph_row
14441 to the end of the display line. Called from display_line.
14442 If the glyph row is empty, add a space glyph to it so that we
14443 know the face to draw. Set the glyph row flag fill_line_p. */
14444
14445 static void
14446 extend_face_to_end_of_line (it)
14447 struct it *it;
14448 {
14449 struct face *face;
14450 struct frame *f = it->f;
14451
14452 /* If line is already filled, do nothing. */
14453 if (it->current_x >= it->last_visible_x)
14454 return;
14455
14456 /* Face extension extends the background and box of IT->face_id
14457 to the end of the line. If the background equals the background
14458 of the frame, we don't have to do anything. */
14459 if (it->face_before_selective_p)
14460 face = FACE_FROM_ID (it->f, it->saved_face_id);
14461 else
14462 face = FACE_FROM_ID (f, it->face_id);
14463
14464 if (FRAME_WINDOW_P (f)
14465 && face->box == FACE_NO_BOX
14466 && face->background == FRAME_BACKGROUND_PIXEL (f)
14467 && !face->stipple)
14468 return;
14469
14470 /* Set the glyph row flag indicating that the face of the last glyph
14471 in the text area has to be drawn to the end of the text area. */
14472 it->glyph_row->fill_line_p = 1;
14473
14474 /* If current character of IT is not ASCII, make sure we have the
14475 ASCII face. This will be automatically undone the next time
14476 get_next_display_element returns a multibyte character. Note
14477 that the character will always be single byte in unibyte text. */
14478 if (!SINGLE_BYTE_CHAR_P (it->c))
14479 {
14480 it->face_id = FACE_FOR_CHAR (f, face, 0);
14481 }
14482
14483 if (FRAME_WINDOW_P (f))
14484 {
14485 /* If the row is empty, add a space with the current face of IT,
14486 so that we know which face to draw. */
14487 if (it->glyph_row->used[TEXT_AREA] == 0)
14488 {
14489 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14490 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14491 it->glyph_row->used[TEXT_AREA] = 1;
14492 }
14493 }
14494 else
14495 {
14496 /* Save some values that must not be changed. */
14497 int saved_x = it->current_x;
14498 struct text_pos saved_pos;
14499 Lisp_Object saved_object;
14500 enum display_element_type saved_what = it->what;
14501 int saved_face_id = it->face_id;
14502
14503 saved_object = it->object;
14504 saved_pos = it->position;
14505
14506 it->what = IT_CHARACTER;
14507 bzero (&it->position, sizeof it->position);
14508 it->object = make_number (0);
14509 it->c = ' ';
14510 it->len = 1;
14511 it->face_id = face->id;
14512
14513 PRODUCE_GLYPHS (it);
14514
14515 while (it->current_x <= it->last_visible_x)
14516 PRODUCE_GLYPHS (it);
14517
14518 /* Don't count these blanks really. It would let us insert a left
14519 truncation glyph below and make us set the cursor on them, maybe. */
14520 it->current_x = saved_x;
14521 it->object = saved_object;
14522 it->position = saved_pos;
14523 it->what = saved_what;
14524 it->face_id = saved_face_id;
14525 }
14526 }
14527
14528
14529 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14530 trailing whitespace. */
14531
14532 static int
14533 trailing_whitespace_p (charpos)
14534 int charpos;
14535 {
14536 int bytepos = CHAR_TO_BYTE (charpos);
14537 int c = 0;
14538
14539 while (bytepos < ZV_BYTE
14540 && (c = FETCH_CHAR (bytepos),
14541 c == ' ' || c == '\t'))
14542 ++bytepos;
14543
14544 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14545 {
14546 if (bytepos != PT_BYTE)
14547 return 1;
14548 }
14549 return 0;
14550 }
14551
14552
14553 /* Highlight trailing whitespace, if any, in ROW. */
14554
14555 void
14556 highlight_trailing_whitespace (f, row)
14557 struct frame *f;
14558 struct glyph_row *row;
14559 {
14560 int used = row->used[TEXT_AREA];
14561
14562 if (used)
14563 {
14564 struct glyph *start = row->glyphs[TEXT_AREA];
14565 struct glyph *glyph = start + used - 1;
14566
14567 /* Skip over glyphs inserted to display the cursor at the
14568 end of a line, for extending the face of the last glyph
14569 to the end of the line on terminals, and for truncation
14570 and continuation glyphs. */
14571 while (glyph >= start
14572 && glyph->type == CHAR_GLYPH
14573 && INTEGERP (glyph->object))
14574 --glyph;
14575
14576 /* If last glyph is a space or stretch, and it's trailing
14577 whitespace, set the face of all trailing whitespace glyphs in
14578 IT->glyph_row to `trailing-whitespace'. */
14579 if (glyph >= start
14580 && BUFFERP (glyph->object)
14581 && (glyph->type == STRETCH_GLYPH
14582 || (glyph->type == CHAR_GLYPH
14583 && glyph->u.ch == ' '))
14584 && trailing_whitespace_p (glyph->charpos))
14585 {
14586 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
14587 if (face_id < 0)
14588 return;
14589
14590 while (glyph >= start
14591 && BUFFERP (glyph->object)
14592 && (glyph->type == STRETCH_GLYPH
14593 || (glyph->type == CHAR_GLYPH
14594 && glyph->u.ch == ' ')))
14595 (glyph--)->face_id = face_id;
14596 }
14597 }
14598 }
14599
14600
14601 /* Value is non-zero if glyph row ROW in window W should be
14602 used to hold the cursor. */
14603
14604 static int
14605 cursor_row_p (w, row)
14606 struct window *w;
14607 struct glyph_row *row;
14608 {
14609 int cursor_row_p = 1;
14610
14611 if (PT == MATRIX_ROW_END_CHARPOS (row))
14612 {
14613 /* If the row ends with a newline from a string, we don't want
14614 the cursor there (if the row is continued it doesn't end in a
14615 newline). */
14616 if (CHARPOS (row->end.string_pos) >= 0
14617 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14618 cursor_row_p = row->continued_p;
14619
14620 /* If the row ends at ZV, display the cursor at the end of that
14621 row instead of at the start of the row below. */
14622 else if (row->ends_at_zv_p)
14623 cursor_row_p = 1;
14624 else
14625 cursor_row_p = 0;
14626 }
14627
14628 return cursor_row_p;
14629 }
14630
14631
14632 /* Construct the glyph row IT->glyph_row in the desired matrix of
14633 IT->w from text at the current position of IT. See dispextern.h
14634 for an overview of struct it. Value is non-zero if
14635 IT->glyph_row displays text, as opposed to a line displaying ZV
14636 only. */
14637
14638 static int
14639 display_line (it)
14640 struct it *it;
14641 {
14642 struct glyph_row *row = it->glyph_row;
14643 int overlay_arrow_bitmap;
14644 Lisp_Object overlay_arrow_string;
14645
14646 /* We always start displaying at hpos zero even if hscrolled. */
14647 xassert (it->hpos == 0 && it->current_x == 0);
14648
14649 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14650 >= it->w->desired_matrix->nrows)
14651 {
14652 it->w->nrows_scale_factor++;
14653 fonts_changed_p = 1;
14654 return 0;
14655 }
14656
14657 /* Is IT->w showing the region? */
14658 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
14659
14660 /* Clear the result glyph row and enable it. */
14661 prepare_desired_row (row);
14662
14663 row->y = it->current_y;
14664 row->start = it->start;
14665 row->continuation_lines_width = it->continuation_lines_width;
14666 row->displays_text_p = 1;
14667 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14668 it->starts_in_middle_of_char_p = 0;
14669
14670 /* Arrange the overlays nicely for our purposes. Usually, we call
14671 display_line on only one line at a time, in which case this
14672 can't really hurt too much, or we call it on lines which appear
14673 one after another in the buffer, in which case all calls to
14674 recenter_overlay_lists but the first will be pretty cheap. */
14675 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14676
14677 /* Move over display elements that are not visible because we are
14678 hscrolled. This may stop at an x-position < IT->first_visible_x
14679 if the first glyph is partially visible or if we hit a line end. */
14680 if (it->current_x < it->first_visible_x)
14681 {
14682 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14683 MOVE_TO_POS | MOVE_TO_X);
14684 }
14685
14686 /* Get the initial row height. This is either the height of the
14687 text hscrolled, if there is any, or zero. */
14688 row->ascent = it->max_ascent;
14689 row->height = it->max_ascent + it->max_descent;
14690 row->phys_ascent = it->max_phys_ascent;
14691 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14692 row->extra_line_spacing = it->max_extra_line_spacing;
14693
14694 /* Loop generating characters. The loop is left with IT on the next
14695 character to display. */
14696 while (1)
14697 {
14698 int n_glyphs_before, hpos_before, x_before;
14699 int x, i, nglyphs;
14700 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
14701
14702 /* Retrieve the next thing to display. Value is zero if end of
14703 buffer reached. */
14704 if (!get_next_display_element (it))
14705 {
14706 /* Maybe add a space at the end of this line that is used to
14707 display the cursor there under X. Set the charpos of the
14708 first glyph of blank lines not corresponding to any text
14709 to -1. */
14710 #ifdef HAVE_WINDOW_SYSTEM
14711 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14712 row->exact_window_width_line_p = 1;
14713 else
14714 #endif /* HAVE_WINDOW_SYSTEM */
14715 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
14716 || row->used[TEXT_AREA] == 0)
14717 {
14718 row->glyphs[TEXT_AREA]->charpos = -1;
14719 row->displays_text_p = 0;
14720
14721 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
14722 && (!MINI_WINDOW_P (it->w)
14723 || (minibuf_level && EQ (it->window, minibuf_window))))
14724 row->indicate_empty_line_p = 1;
14725 }
14726
14727 it->continuation_lines_width = 0;
14728 row->ends_at_zv_p = 1;
14729 break;
14730 }
14731
14732 /* Now, get the metrics of what we want to display. This also
14733 generates glyphs in `row' (which is IT->glyph_row). */
14734 n_glyphs_before = row->used[TEXT_AREA];
14735 x = it->current_x;
14736
14737 /* Remember the line height so far in case the next element doesn't
14738 fit on the line. */
14739 if (!it->truncate_lines_p)
14740 {
14741 ascent = it->max_ascent;
14742 descent = it->max_descent;
14743 phys_ascent = it->max_phys_ascent;
14744 phys_descent = it->max_phys_descent;
14745 }
14746
14747 PRODUCE_GLYPHS (it);
14748
14749 /* If this display element was in marginal areas, continue with
14750 the next one. */
14751 if (it->area != TEXT_AREA)
14752 {
14753 row->ascent = max (row->ascent, it->max_ascent);
14754 row->height = max (row->height, it->max_ascent + it->max_descent);
14755 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14756 row->phys_height = max (row->phys_height,
14757 it->max_phys_ascent + it->max_phys_descent);
14758 row->extra_line_spacing = max (row->extra_line_spacing,
14759 it->max_extra_line_spacing);
14760 set_iterator_to_next (it, 1);
14761 continue;
14762 }
14763
14764 /* Does the display element fit on the line? If we truncate
14765 lines, we should draw past the right edge of the window. If
14766 we don't truncate, we want to stop so that we can display the
14767 continuation glyph before the right margin. If lines are
14768 continued, there are two possible strategies for characters
14769 resulting in more than 1 glyph (e.g. tabs): Display as many
14770 glyphs as possible in this line and leave the rest for the
14771 continuation line, or display the whole element in the next
14772 line. Original redisplay did the former, so we do it also. */
14773 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14774 hpos_before = it->hpos;
14775 x_before = x;
14776
14777 if (/* Not a newline. */
14778 nglyphs > 0
14779 /* Glyphs produced fit entirely in the line. */
14780 && it->current_x < it->last_visible_x)
14781 {
14782 it->hpos += nglyphs;
14783 row->ascent = max (row->ascent, it->max_ascent);
14784 row->height = max (row->height, it->max_ascent + it->max_descent);
14785 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14786 row->phys_height = max (row->phys_height,
14787 it->max_phys_ascent + it->max_phys_descent);
14788 row->extra_line_spacing = max (row->extra_line_spacing,
14789 it->max_extra_line_spacing);
14790 if (it->current_x - it->pixel_width < it->first_visible_x)
14791 row->x = x - it->first_visible_x;
14792 }
14793 else
14794 {
14795 int new_x;
14796 struct glyph *glyph;
14797
14798 for (i = 0; i < nglyphs; ++i, x = new_x)
14799 {
14800 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14801 new_x = x + glyph->pixel_width;
14802
14803 if (/* Lines are continued. */
14804 !it->truncate_lines_p
14805 && (/* Glyph doesn't fit on the line. */
14806 new_x > it->last_visible_x
14807 /* Or it fits exactly on a window system frame. */
14808 || (new_x == it->last_visible_x
14809 && FRAME_WINDOW_P (it->f))))
14810 {
14811 /* End of a continued line. */
14812
14813 if (it->hpos == 0
14814 || (new_x == it->last_visible_x
14815 && FRAME_WINDOW_P (it->f)))
14816 {
14817 /* Current glyph is the only one on the line or
14818 fits exactly on the line. We must continue
14819 the line because we can't draw the cursor
14820 after the glyph. */
14821 row->continued_p = 1;
14822 it->current_x = new_x;
14823 it->continuation_lines_width += new_x;
14824 ++it->hpos;
14825 if (i == nglyphs - 1)
14826 {
14827 set_iterator_to_next (it, 1);
14828 #ifdef HAVE_WINDOW_SYSTEM
14829 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14830 {
14831 if (!get_next_display_element (it))
14832 {
14833 row->exact_window_width_line_p = 1;
14834 it->continuation_lines_width = 0;
14835 row->continued_p = 0;
14836 row->ends_at_zv_p = 1;
14837 }
14838 else if (ITERATOR_AT_END_OF_LINE_P (it))
14839 {
14840 row->continued_p = 0;
14841 row->exact_window_width_line_p = 1;
14842 }
14843 }
14844 #endif /* HAVE_WINDOW_SYSTEM */
14845 }
14846 }
14847 else if (CHAR_GLYPH_PADDING_P (*glyph)
14848 && !FRAME_WINDOW_P (it->f))
14849 {
14850 /* A padding glyph that doesn't fit on this line.
14851 This means the whole character doesn't fit
14852 on the line. */
14853 row->used[TEXT_AREA] = n_glyphs_before;
14854
14855 /* Fill the rest of the row with continuation
14856 glyphs like in 20.x. */
14857 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
14858 < row->glyphs[1 + TEXT_AREA])
14859 produce_special_glyphs (it, IT_CONTINUATION);
14860
14861 row->continued_p = 1;
14862 it->current_x = x_before;
14863 it->continuation_lines_width += x_before;
14864
14865 /* Restore the height to what it was before the
14866 element not fitting on the line. */
14867 it->max_ascent = ascent;
14868 it->max_descent = descent;
14869 it->max_phys_ascent = phys_ascent;
14870 it->max_phys_descent = phys_descent;
14871 }
14872 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
14873 {
14874 /* A TAB that extends past the right edge of the
14875 window. This produces a single glyph on
14876 window system frames. We leave the glyph in
14877 this row and let it fill the row, but don't
14878 consume the TAB. */
14879 it->continuation_lines_width += it->last_visible_x;
14880 row->ends_in_middle_of_char_p = 1;
14881 row->continued_p = 1;
14882 glyph->pixel_width = it->last_visible_x - x;
14883 it->starts_in_middle_of_char_p = 1;
14884 }
14885 else
14886 {
14887 /* Something other than a TAB that draws past
14888 the right edge of the window. Restore
14889 positions to values before the element. */
14890 row->used[TEXT_AREA] = n_glyphs_before + i;
14891
14892 /* Display continuation glyphs. */
14893 if (!FRAME_WINDOW_P (it->f))
14894 produce_special_glyphs (it, IT_CONTINUATION);
14895 row->continued_p = 1;
14896
14897 it->continuation_lines_width += x;
14898
14899 if (nglyphs > 1 && i > 0)
14900 {
14901 row->ends_in_middle_of_char_p = 1;
14902 it->starts_in_middle_of_char_p = 1;
14903 }
14904
14905 /* Restore the height to what it was before the
14906 element not fitting on the line. */
14907 it->max_ascent = ascent;
14908 it->max_descent = descent;
14909 it->max_phys_ascent = phys_ascent;
14910 it->max_phys_descent = phys_descent;
14911 }
14912
14913 break;
14914 }
14915 else if (new_x > it->first_visible_x)
14916 {
14917 /* Increment number of glyphs actually displayed. */
14918 ++it->hpos;
14919
14920 if (x < it->first_visible_x)
14921 /* Glyph is partially visible, i.e. row starts at
14922 negative X position. */
14923 row->x = x - it->first_visible_x;
14924 }
14925 else
14926 {
14927 /* Glyph is completely off the left margin of the
14928 window. This should not happen because of the
14929 move_it_in_display_line at the start of this
14930 function, unless the text display area of the
14931 window is empty. */
14932 xassert (it->first_visible_x <= it->last_visible_x);
14933 }
14934 }
14935
14936 row->ascent = max (row->ascent, it->max_ascent);
14937 row->height = max (row->height, it->max_ascent + it->max_descent);
14938 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14939 row->phys_height = max (row->phys_height,
14940 it->max_phys_ascent + it->max_phys_descent);
14941 row->extra_line_spacing = max (row->extra_line_spacing,
14942 it->max_extra_line_spacing);
14943
14944 /* End of this display line if row is continued. */
14945 if (row->continued_p || row->ends_at_zv_p)
14946 break;
14947 }
14948
14949 at_end_of_line:
14950 /* Is this a line end? If yes, we're also done, after making
14951 sure that a non-default face is extended up to the right
14952 margin of the window. */
14953 if (ITERATOR_AT_END_OF_LINE_P (it))
14954 {
14955 int used_before = row->used[TEXT_AREA];
14956
14957 row->ends_in_newline_from_string_p = STRINGP (it->object);
14958
14959 #ifdef HAVE_WINDOW_SYSTEM
14960 /* Add a space at the end of the line that is used to
14961 display the cursor there. */
14962 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14963 append_space_for_newline (it, 0);
14964 #endif /* HAVE_WINDOW_SYSTEM */
14965
14966 /* Extend the face to the end of the line. */
14967 extend_face_to_end_of_line (it);
14968
14969 /* Make sure we have the position. */
14970 if (used_before == 0)
14971 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
14972
14973 /* Consume the line end. This skips over invisible lines. */
14974 set_iterator_to_next (it, 1);
14975 it->continuation_lines_width = 0;
14976 break;
14977 }
14978
14979 /* Proceed with next display element. Note that this skips
14980 over lines invisible because of selective display. */
14981 set_iterator_to_next (it, 1);
14982
14983 /* If we truncate lines, we are done when the last displayed
14984 glyphs reach past the right margin of the window. */
14985 if (it->truncate_lines_p
14986 && (FRAME_WINDOW_P (it->f)
14987 ? (it->current_x >= it->last_visible_x)
14988 : (it->current_x > it->last_visible_x)))
14989 {
14990 /* Maybe add truncation glyphs. */
14991 if (!FRAME_WINDOW_P (it->f))
14992 {
14993 int i, n;
14994
14995 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14996 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14997 break;
14998
14999 for (n = row->used[TEXT_AREA]; i < n; ++i)
15000 {
15001 row->used[TEXT_AREA] = i;
15002 produce_special_glyphs (it, IT_TRUNCATION);
15003 }
15004 }
15005 #ifdef HAVE_WINDOW_SYSTEM
15006 else
15007 {
15008 /* Don't truncate if we can overflow newline into fringe. */
15009 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15010 {
15011 if (!get_next_display_element (it))
15012 {
15013 it->continuation_lines_width = 0;
15014 row->ends_at_zv_p = 1;
15015 row->exact_window_width_line_p = 1;
15016 break;
15017 }
15018 if (ITERATOR_AT_END_OF_LINE_P (it))
15019 {
15020 row->exact_window_width_line_p = 1;
15021 goto at_end_of_line;
15022 }
15023 }
15024 }
15025 #endif /* HAVE_WINDOW_SYSTEM */
15026
15027 row->truncated_on_right_p = 1;
15028 it->continuation_lines_width = 0;
15029 reseat_at_next_visible_line_start (it, 0);
15030 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
15031 it->hpos = hpos_before;
15032 it->current_x = x_before;
15033 break;
15034 }
15035 }
15036
15037 /* If line is not empty and hscrolled, maybe insert truncation glyphs
15038 at the left window margin. */
15039 if (it->first_visible_x
15040 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
15041 {
15042 if (!FRAME_WINDOW_P (it->f))
15043 insert_left_trunc_glyphs (it);
15044 row->truncated_on_left_p = 1;
15045 }
15046
15047 /* If the start of this line is the overlay arrow-position, then
15048 mark this glyph row as the one containing the overlay arrow.
15049 This is clearly a mess with variable size fonts. It would be
15050 better to let it be displayed like cursors under X. */
15051 if (! overlay_arrow_seen
15052 && (overlay_arrow_string
15053 = overlay_arrow_at_row (it, row, &overlay_arrow_bitmap),
15054 !NILP (overlay_arrow_string)))
15055 {
15056 /* Overlay arrow in window redisplay is a fringe bitmap. */
15057 if (STRINGP (overlay_arrow_string))
15058 {
15059 struct glyph_row *arrow_row
15060 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
15061 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15062 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15063 struct glyph *p = row->glyphs[TEXT_AREA];
15064 struct glyph *p2, *end;
15065
15066 /* Copy the arrow glyphs. */
15067 while (glyph < arrow_end)
15068 *p++ = *glyph++;
15069
15070 /* Throw away padding glyphs. */
15071 p2 = p;
15072 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15073 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15074 ++p2;
15075 if (p2 > p)
15076 {
15077 while (p2 < end)
15078 *p++ = *p2++;
15079 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
15080 }
15081 }
15082 else
15083 {
15084 it->w->overlay_arrow_bitmap = overlay_arrow_bitmap;
15085 row->overlay_arrow_p = 1;
15086 }
15087 overlay_arrow_seen = 1;
15088 }
15089
15090 /* Compute pixel dimensions of this line. */
15091 compute_line_metrics (it);
15092
15093 /* Remember the position at which this line ends. */
15094 row->end = it->current;
15095
15096 /* Save fringe bitmaps in this row. */
15097 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
15098 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
15099 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
15100 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
15101
15102 it->left_user_fringe_bitmap = 0;
15103 it->left_user_fringe_face_id = 0;
15104 it->right_user_fringe_bitmap = 0;
15105 it->right_user_fringe_face_id = 0;
15106
15107 /* Maybe set the cursor. */
15108 if (it->w->cursor.vpos < 0
15109 && PT >= MATRIX_ROW_START_CHARPOS (row)
15110 && PT <= MATRIX_ROW_END_CHARPOS (row)
15111 && cursor_row_p (it->w, row))
15112 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15113
15114 /* Highlight trailing whitespace. */
15115 if (!NILP (Vshow_trailing_whitespace))
15116 highlight_trailing_whitespace (it->f, it->glyph_row);
15117
15118 /* Prepare for the next line. This line starts horizontally at (X
15119 HPOS) = (0 0). Vertical positions are incremented. As a
15120 convenience for the caller, IT->glyph_row is set to the next
15121 row to be used. */
15122 it->current_x = it->hpos = 0;
15123 it->current_y += row->height;
15124 ++it->vpos;
15125 ++it->glyph_row;
15126 it->start = it->current;
15127 return row->displays_text_p;
15128 }
15129
15130
15131 \f
15132 /***********************************************************************
15133 Menu Bar
15134 ***********************************************************************/
15135
15136 /* Redisplay the menu bar in the frame for window W.
15137
15138 The menu bar of X frames that don't have X toolkit support is
15139 displayed in a special window W->frame->menu_bar_window.
15140
15141 The menu bar of terminal frames is treated specially as far as
15142 glyph matrices are concerned. Menu bar lines are not part of
15143 windows, so the update is done directly on the frame matrix rows
15144 for the menu bar. */
15145
15146 static void
15147 display_menu_bar (w)
15148 struct window *w;
15149 {
15150 struct frame *f = XFRAME (WINDOW_FRAME (w));
15151 struct it it;
15152 Lisp_Object items;
15153 int i;
15154
15155 /* Don't do all this for graphical frames. */
15156 #ifdef HAVE_NTGUI
15157 if (!NILP (Vwindow_system))
15158 return;
15159 #endif
15160 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15161 if (FRAME_X_P (f))
15162 return;
15163 #endif
15164 #ifdef MAC_OS
15165 if (FRAME_MAC_P (f))
15166 return;
15167 #endif
15168
15169 #ifdef USE_X_TOOLKIT
15170 xassert (!FRAME_WINDOW_P (f));
15171 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
15172 it.first_visible_x = 0;
15173 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15174 #else /* not USE_X_TOOLKIT */
15175 if (FRAME_WINDOW_P (f))
15176 {
15177 /* Menu bar lines are displayed in the desired matrix of the
15178 dummy window menu_bar_window. */
15179 struct window *menu_w;
15180 xassert (WINDOWP (f->menu_bar_window));
15181 menu_w = XWINDOW (f->menu_bar_window);
15182 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
15183 MENU_FACE_ID);
15184 it.first_visible_x = 0;
15185 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15186 }
15187 else
15188 {
15189 /* This is a TTY frame, i.e. character hpos/vpos are used as
15190 pixel x/y. */
15191 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
15192 MENU_FACE_ID);
15193 it.first_visible_x = 0;
15194 it.last_visible_x = FRAME_COLS (f);
15195 }
15196 #endif /* not USE_X_TOOLKIT */
15197
15198 if (! mode_line_inverse_video)
15199 /* Force the menu-bar to be displayed in the default face. */
15200 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15201
15202 /* Clear all rows of the menu bar. */
15203 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
15204 {
15205 struct glyph_row *row = it.glyph_row + i;
15206 clear_glyph_row (row);
15207 row->enabled_p = 1;
15208 row->full_width_p = 1;
15209 }
15210
15211 /* Display all items of the menu bar. */
15212 items = FRAME_MENU_BAR_ITEMS (it.f);
15213 for (i = 0; i < XVECTOR (items)->size; i += 4)
15214 {
15215 Lisp_Object string;
15216
15217 /* Stop at nil string. */
15218 string = AREF (items, i + 1);
15219 if (NILP (string))
15220 break;
15221
15222 /* Remember where item was displayed. */
15223 AREF (items, i + 3) = make_number (it.hpos);
15224
15225 /* Display the item, pad with one space. */
15226 if (it.current_x < it.last_visible_x)
15227 display_string (NULL, string, Qnil, 0, 0, &it,
15228 SCHARS (string) + 1, 0, 0, -1);
15229 }
15230
15231 /* Fill out the line with spaces. */
15232 if (it.current_x < it.last_visible_x)
15233 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15234
15235 /* Compute the total height of the lines. */
15236 compute_line_metrics (&it);
15237 }
15238
15239
15240 \f
15241 /***********************************************************************
15242 Mode Line
15243 ***********************************************************************/
15244
15245 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15246 FORCE is non-zero, redisplay mode lines unconditionally.
15247 Otherwise, redisplay only mode lines that are garbaged. Value is
15248 the number of windows whose mode lines were redisplayed. */
15249
15250 static int
15251 redisplay_mode_lines (window, force)
15252 Lisp_Object window;
15253 int force;
15254 {
15255 int nwindows = 0;
15256
15257 while (!NILP (window))
15258 {
15259 struct window *w = XWINDOW (window);
15260
15261 if (WINDOWP (w->hchild))
15262 nwindows += redisplay_mode_lines (w->hchild, force);
15263 else if (WINDOWP (w->vchild))
15264 nwindows += redisplay_mode_lines (w->vchild, force);
15265 else if (force
15266 || FRAME_GARBAGED_P (XFRAME (w->frame))
15267 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15268 {
15269 struct text_pos lpoint;
15270 struct buffer *old = current_buffer;
15271
15272 /* Set the window's buffer for the mode line display. */
15273 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15274 set_buffer_internal_1 (XBUFFER (w->buffer));
15275
15276 /* Point refers normally to the selected window. For any
15277 other window, set up appropriate value. */
15278 if (!EQ (window, selected_window))
15279 {
15280 struct text_pos pt;
15281
15282 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15283 if (CHARPOS (pt) < BEGV)
15284 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15285 else if (CHARPOS (pt) > (ZV - 1))
15286 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15287 else
15288 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15289 }
15290
15291 /* Display mode lines. */
15292 clear_glyph_matrix (w->desired_matrix);
15293 if (display_mode_lines (w))
15294 {
15295 ++nwindows;
15296 w->must_be_updated_p = 1;
15297 }
15298
15299 /* Restore old settings. */
15300 set_buffer_internal_1 (old);
15301 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15302 }
15303
15304 window = w->next;
15305 }
15306
15307 return nwindows;
15308 }
15309
15310
15311 /* Display the mode and/or top line of window W. Value is the number
15312 of mode lines displayed. */
15313
15314 static int
15315 display_mode_lines (w)
15316 struct window *w;
15317 {
15318 Lisp_Object old_selected_window, old_selected_frame;
15319 int n = 0;
15320
15321 old_selected_frame = selected_frame;
15322 selected_frame = w->frame;
15323 old_selected_window = selected_window;
15324 XSETWINDOW (selected_window, w);
15325
15326 /* These will be set while the mode line specs are processed. */
15327 line_number_displayed = 0;
15328 w->column_number_displayed = Qnil;
15329
15330 if (WINDOW_WANTS_MODELINE_P (w))
15331 {
15332 struct window *sel_w = XWINDOW (old_selected_window);
15333
15334 /* Select mode line face based on the real selected window. */
15335 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15336 current_buffer->mode_line_format);
15337 ++n;
15338 }
15339
15340 if (WINDOW_WANTS_HEADER_LINE_P (w))
15341 {
15342 display_mode_line (w, HEADER_LINE_FACE_ID,
15343 current_buffer->header_line_format);
15344 ++n;
15345 }
15346
15347 selected_frame = old_selected_frame;
15348 selected_window = old_selected_window;
15349 return n;
15350 }
15351
15352
15353 /* Display mode or top line of window W. FACE_ID specifies which line
15354 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15355 FORMAT is the mode line format to display. Value is the pixel
15356 height of the mode line displayed. */
15357
15358 static int
15359 display_mode_line (w, face_id, format)
15360 struct window *w;
15361 enum face_id face_id;
15362 Lisp_Object format;
15363 {
15364 struct it it;
15365 struct face *face;
15366
15367 init_iterator (&it, w, -1, -1, NULL, face_id);
15368 prepare_desired_row (it.glyph_row);
15369
15370 it.glyph_row->mode_line_p = 1;
15371
15372 if (! mode_line_inverse_video)
15373 /* Force the mode-line to be displayed in the default face. */
15374 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15375
15376 /* Temporarily make frame's keyboard the current kboard so that
15377 kboard-local variables in the mode_line_format will get the right
15378 values. */
15379 push_frame_kboard (it.f);
15380 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15381 pop_frame_kboard ();
15382
15383 /* Fill up with spaces. */
15384 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15385
15386 compute_line_metrics (&it);
15387 it.glyph_row->full_width_p = 1;
15388 it.glyph_row->continued_p = 0;
15389 it.glyph_row->truncated_on_left_p = 0;
15390 it.glyph_row->truncated_on_right_p = 0;
15391
15392 /* Make a 3D mode-line have a shadow at its right end. */
15393 face = FACE_FROM_ID (it.f, face_id);
15394 extend_face_to_end_of_line (&it);
15395 if (face->box != FACE_NO_BOX)
15396 {
15397 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15398 + it.glyph_row->used[TEXT_AREA] - 1);
15399 last->right_box_line_p = 1;
15400 }
15401
15402 return it.glyph_row->height;
15403 }
15404
15405 /* Alist that caches the results of :propertize.
15406 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
15407 Lisp_Object mode_line_proptrans_alist;
15408
15409 /* List of strings making up the mode-line. */
15410 Lisp_Object mode_line_string_list;
15411
15412 /* Base face property when building propertized mode line string. */
15413 static Lisp_Object mode_line_string_face;
15414 static Lisp_Object mode_line_string_face_prop;
15415
15416
15417 /* Contribute ELT to the mode line for window IT->w. How it
15418 translates into text depends on its data type.
15419
15420 IT describes the display environment in which we display, as usual.
15421
15422 DEPTH is the depth in recursion. It is used to prevent
15423 infinite recursion here.
15424
15425 FIELD_WIDTH is the number of characters the display of ELT should
15426 occupy in the mode line, and PRECISION is the maximum number of
15427 characters to display from ELT's representation. See
15428 display_string for details.
15429
15430 Returns the hpos of the end of the text generated by ELT.
15431
15432 PROPS is a property list to add to any string we encounter.
15433
15434 If RISKY is nonzero, remove (disregard) any properties in any string
15435 we encounter, and ignore :eval and :propertize.
15436
15437 If the global variable `frame_title_ptr' is non-NULL, then the output
15438 is passed to `store_frame_title' instead of `display_string'. */
15439
15440 static int
15441 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15442 struct it *it;
15443 int depth;
15444 int field_width, precision;
15445 Lisp_Object elt, props;
15446 int risky;
15447 {
15448 int n = 0, field, prec;
15449 int literal = 0;
15450
15451 tail_recurse:
15452 if (depth > 100)
15453 elt = build_string ("*too-deep*");
15454
15455 depth++;
15456
15457 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15458 {
15459 case Lisp_String:
15460 {
15461 /* A string: output it and check for %-constructs within it. */
15462 unsigned char c;
15463 const unsigned char *this, *lisp_string;
15464
15465 if (!NILP (props) || risky)
15466 {
15467 Lisp_Object oprops, aelt;
15468 oprops = Ftext_properties_at (make_number (0), elt);
15469
15470 /* If the starting string's properties are not what
15471 we want, translate the string. Also, if the string
15472 is risky, do that anyway. */
15473
15474 if (NILP (Fequal (props, oprops)) || risky)
15475 {
15476 /* If the starting string has properties,
15477 merge the specified ones onto the existing ones. */
15478 if (! NILP (oprops) && !risky)
15479 {
15480 Lisp_Object tem;
15481
15482 oprops = Fcopy_sequence (oprops);
15483 tem = props;
15484 while (CONSP (tem))
15485 {
15486 oprops = Fplist_put (oprops, XCAR (tem),
15487 XCAR (XCDR (tem)));
15488 tem = XCDR (XCDR (tem));
15489 }
15490 props = oprops;
15491 }
15492
15493 aelt = Fassoc (elt, mode_line_proptrans_alist);
15494 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15495 {
15496 mode_line_proptrans_alist
15497 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15498 elt = XCAR (aelt);
15499 }
15500 else
15501 {
15502 Lisp_Object tem;
15503
15504 elt = Fcopy_sequence (elt);
15505 Fset_text_properties (make_number (0), Flength (elt),
15506 props, elt);
15507 /* Add this item to mode_line_proptrans_alist. */
15508 mode_line_proptrans_alist
15509 = Fcons (Fcons (elt, props),
15510 mode_line_proptrans_alist);
15511 /* Truncate mode_line_proptrans_alist
15512 to at most 50 elements. */
15513 tem = Fnthcdr (make_number (50),
15514 mode_line_proptrans_alist);
15515 if (! NILP (tem))
15516 XSETCDR (tem, Qnil);
15517 }
15518 }
15519 }
15520
15521 this = SDATA (elt);
15522 lisp_string = this;
15523
15524 if (literal)
15525 {
15526 prec = precision - n;
15527 if (frame_title_ptr)
15528 n += store_frame_title (SDATA (elt), -1, prec);
15529 else if (!NILP (mode_line_string_list))
15530 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15531 else
15532 n += display_string (NULL, elt, Qnil, 0, 0, it,
15533 0, prec, 0, STRING_MULTIBYTE (elt));
15534
15535 break;
15536 }
15537
15538 while ((precision <= 0 || n < precision)
15539 && *this
15540 && (frame_title_ptr
15541 || !NILP (mode_line_string_list)
15542 || it->current_x < it->last_visible_x))
15543 {
15544 const unsigned char *last = this;
15545
15546 /* Advance to end of string or next format specifier. */
15547 while ((c = *this++) != '\0' && c != '%')
15548 ;
15549
15550 if (this - 1 != last)
15551 {
15552 int nchars, nbytes;
15553
15554 /* Output to end of string or up to '%'. Field width
15555 is length of string. Don't output more than
15556 PRECISION allows us. */
15557 --this;
15558
15559 prec = c_string_width (last, this - last, precision - n,
15560 &nchars, &nbytes);
15561
15562 if (frame_title_ptr)
15563 n += store_frame_title (last, 0, prec);
15564 else if (!NILP (mode_line_string_list))
15565 {
15566 int bytepos = last - lisp_string;
15567 int charpos = string_byte_to_char (elt, bytepos);
15568 int endpos = (precision <= 0
15569 ? string_byte_to_char (elt,
15570 this - lisp_string)
15571 : charpos + nchars);
15572
15573 n += store_mode_line_string (NULL,
15574 Fsubstring (elt, make_number (charpos),
15575 make_number (endpos)),
15576 0, 0, 0, Qnil);
15577 }
15578 else
15579 {
15580 int bytepos = last - lisp_string;
15581 int charpos = string_byte_to_char (elt, bytepos);
15582 n += display_string (NULL, elt, Qnil, 0, charpos,
15583 it, 0, prec, 0,
15584 STRING_MULTIBYTE (elt));
15585 }
15586 }
15587 else /* c == '%' */
15588 {
15589 const unsigned char *percent_position = this;
15590
15591 /* Get the specified minimum width. Zero means
15592 don't pad. */
15593 field = 0;
15594 while ((c = *this++) >= '0' && c <= '9')
15595 field = field * 10 + c - '0';
15596
15597 /* Don't pad beyond the total padding allowed. */
15598 if (field_width - n > 0 && field > field_width - n)
15599 field = field_width - n;
15600
15601 /* Note that either PRECISION <= 0 or N < PRECISION. */
15602 prec = precision - n;
15603
15604 if (c == 'M')
15605 n += display_mode_element (it, depth, field, prec,
15606 Vglobal_mode_string, props,
15607 risky);
15608 else if (c != 0)
15609 {
15610 int multibyte;
15611 int bytepos, charpos;
15612 unsigned char *spec;
15613
15614 bytepos = percent_position - lisp_string;
15615 charpos = (STRING_MULTIBYTE (elt)
15616 ? string_byte_to_char (elt, bytepos)
15617 : bytepos);
15618
15619 spec
15620 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15621
15622 if (frame_title_ptr)
15623 n += store_frame_title (spec, field, prec);
15624 else if (!NILP (mode_line_string_list))
15625 {
15626 int len = strlen (spec);
15627 Lisp_Object tem = make_string (spec, len);
15628 props = Ftext_properties_at (make_number (charpos), elt);
15629 /* Should only keep face property in props */
15630 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15631 }
15632 else
15633 {
15634 int nglyphs_before, nwritten;
15635
15636 nglyphs_before = it->glyph_row->used[TEXT_AREA];
15637 nwritten = display_string (spec, Qnil, elt,
15638 charpos, 0, it,
15639 field, prec, 0,
15640 multibyte);
15641
15642 /* Assign to the glyphs written above the
15643 string where the `%x' came from, position
15644 of the `%'. */
15645 if (nwritten > 0)
15646 {
15647 struct glyph *glyph
15648 = (it->glyph_row->glyphs[TEXT_AREA]
15649 + nglyphs_before);
15650 int i;
15651
15652 for (i = 0; i < nwritten; ++i)
15653 {
15654 glyph[i].object = elt;
15655 glyph[i].charpos = charpos;
15656 }
15657
15658 n += nwritten;
15659 }
15660 }
15661 }
15662 else /* c == 0 */
15663 break;
15664 }
15665 }
15666 }
15667 break;
15668
15669 case Lisp_Symbol:
15670 /* A symbol: process the value of the symbol recursively
15671 as if it appeared here directly. Avoid error if symbol void.
15672 Special case: if value of symbol is a string, output the string
15673 literally. */
15674 {
15675 register Lisp_Object tem;
15676
15677 /* If the variable is not marked as risky to set
15678 then its contents are risky to use. */
15679 if (NILP (Fget (elt, Qrisky_local_variable)))
15680 risky = 1;
15681
15682 tem = Fboundp (elt);
15683 if (!NILP (tem))
15684 {
15685 tem = Fsymbol_value (elt);
15686 /* If value is a string, output that string literally:
15687 don't check for % within it. */
15688 if (STRINGP (tem))
15689 literal = 1;
15690
15691 if (!EQ (tem, elt))
15692 {
15693 /* Give up right away for nil or t. */
15694 elt = tem;
15695 goto tail_recurse;
15696 }
15697 }
15698 }
15699 break;
15700
15701 case Lisp_Cons:
15702 {
15703 register Lisp_Object car, tem;
15704
15705 /* A cons cell: five distinct cases.
15706 If first element is :eval or :propertize, do something special.
15707 If first element is a string or a cons, process all the elements
15708 and effectively concatenate them.
15709 If first element is a negative number, truncate displaying cdr to
15710 at most that many characters. If positive, pad (with spaces)
15711 to at least that many characters.
15712 If first element is a symbol, process the cadr or caddr recursively
15713 according to whether the symbol's value is non-nil or nil. */
15714 car = XCAR (elt);
15715 if (EQ (car, QCeval))
15716 {
15717 /* An element of the form (:eval FORM) means evaluate FORM
15718 and use the result as mode line elements. */
15719
15720 if (risky)
15721 break;
15722
15723 if (CONSP (XCDR (elt)))
15724 {
15725 Lisp_Object spec;
15726 spec = safe_eval (XCAR (XCDR (elt)));
15727 n += display_mode_element (it, depth, field_width - n,
15728 precision - n, spec, props,
15729 risky);
15730 }
15731 }
15732 else if (EQ (car, QCpropertize))
15733 {
15734 /* An element of the form (:propertize ELT PROPS...)
15735 means display ELT but applying properties PROPS. */
15736
15737 if (risky)
15738 break;
15739
15740 if (CONSP (XCDR (elt)))
15741 n += display_mode_element (it, depth, field_width - n,
15742 precision - n, XCAR (XCDR (elt)),
15743 XCDR (XCDR (elt)), risky);
15744 }
15745 else if (SYMBOLP (car))
15746 {
15747 tem = Fboundp (car);
15748 elt = XCDR (elt);
15749 if (!CONSP (elt))
15750 goto invalid;
15751 /* elt is now the cdr, and we know it is a cons cell.
15752 Use its car if CAR has a non-nil value. */
15753 if (!NILP (tem))
15754 {
15755 tem = Fsymbol_value (car);
15756 if (!NILP (tem))
15757 {
15758 elt = XCAR (elt);
15759 goto tail_recurse;
15760 }
15761 }
15762 /* Symbol's value is nil (or symbol is unbound)
15763 Get the cddr of the original list
15764 and if possible find the caddr and use that. */
15765 elt = XCDR (elt);
15766 if (NILP (elt))
15767 break;
15768 else if (!CONSP (elt))
15769 goto invalid;
15770 elt = XCAR (elt);
15771 goto tail_recurse;
15772 }
15773 else if (INTEGERP (car))
15774 {
15775 register int lim = XINT (car);
15776 elt = XCDR (elt);
15777 if (lim < 0)
15778 {
15779 /* Negative int means reduce maximum width. */
15780 if (precision <= 0)
15781 precision = -lim;
15782 else
15783 precision = min (precision, -lim);
15784 }
15785 else if (lim > 0)
15786 {
15787 /* Padding specified. Don't let it be more than
15788 current maximum. */
15789 if (precision > 0)
15790 lim = min (precision, lim);
15791
15792 /* If that's more padding than already wanted, queue it.
15793 But don't reduce padding already specified even if
15794 that is beyond the current truncation point. */
15795 field_width = max (lim, field_width);
15796 }
15797 goto tail_recurse;
15798 }
15799 else if (STRINGP (car) || CONSP (car))
15800 {
15801 register int limit = 50;
15802 /* Limit is to protect against circular lists. */
15803 while (CONSP (elt)
15804 && --limit > 0
15805 && (precision <= 0 || n < precision))
15806 {
15807 n += display_mode_element (it, depth, field_width - n,
15808 precision - n, XCAR (elt),
15809 props, risky);
15810 elt = XCDR (elt);
15811 }
15812 }
15813 }
15814 break;
15815
15816 default:
15817 invalid:
15818 elt = build_string ("*invalid*");
15819 goto tail_recurse;
15820 }
15821
15822 /* Pad to FIELD_WIDTH. */
15823 if (field_width > 0 && n < field_width)
15824 {
15825 if (frame_title_ptr)
15826 n += store_frame_title ("", field_width - n, 0);
15827 else if (!NILP (mode_line_string_list))
15828 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
15829 else
15830 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
15831 0, 0, 0);
15832 }
15833
15834 return n;
15835 }
15836
15837 /* Store a mode-line string element in mode_line_string_list.
15838
15839 If STRING is non-null, display that C string. Otherwise, the Lisp
15840 string LISP_STRING is displayed.
15841
15842 FIELD_WIDTH is the minimum number of output glyphs to produce.
15843 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15844 with spaces. FIELD_WIDTH <= 0 means don't pad.
15845
15846 PRECISION is the maximum number of characters to output from
15847 STRING. PRECISION <= 0 means don't truncate the string.
15848
15849 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
15850 properties to the string.
15851
15852 PROPS are the properties to add to the string.
15853 The mode_line_string_face face property is always added to the string.
15854 */
15855
15856 static int
15857 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
15858 char *string;
15859 Lisp_Object lisp_string;
15860 int copy_string;
15861 int field_width;
15862 int precision;
15863 Lisp_Object props;
15864 {
15865 int len;
15866 int n = 0;
15867
15868 if (string != NULL)
15869 {
15870 len = strlen (string);
15871 if (precision > 0 && len > precision)
15872 len = precision;
15873 lisp_string = make_string (string, len);
15874 if (NILP (props))
15875 props = mode_line_string_face_prop;
15876 else if (!NILP (mode_line_string_face))
15877 {
15878 Lisp_Object face = Fsafe_plist_get (props, Qface);
15879 props = Fcopy_sequence (props);
15880 if (NILP (face))
15881 face = mode_line_string_face;
15882 else
15883 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15884 props = Fplist_put (props, Qface, face);
15885 }
15886 Fadd_text_properties (make_number (0), make_number (len),
15887 props, lisp_string);
15888 }
15889 else
15890 {
15891 len = XFASTINT (Flength (lisp_string));
15892 if (precision > 0 && len > precision)
15893 {
15894 len = precision;
15895 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
15896 precision = -1;
15897 }
15898 if (!NILP (mode_line_string_face))
15899 {
15900 Lisp_Object face;
15901 if (NILP (props))
15902 props = Ftext_properties_at (make_number (0), lisp_string);
15903 face = Fsafe_plist_get (props, Qface);
15904 if (NILP (face))
15905 face = mode_line_string_face;
15906 else
15907 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15908 props = Fcons (Qface, Fcons (face, Qnil));
15909 if (copy_string)
15910 lisp_string = Fcopy_sequence (lisp_string);
15911 }
15912 if (!NILP (props))
15913 Fadd_text_properties (make_number (0), make_number (len),
15914 props, lisp_string);
15915 }
15916
15917 if (len > 0)
15918 {
15919 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15920 n += len;
15921 }
15922
15923 if (field_width > len)
15924 {
15925 field_width -= len;
15926 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
15927 if (!NILP (props))
15928 Fadd_text_properties (make_number (0), make_number (field_width),
15929 props, lisp_string);
15930 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15931 n += field_width;
15932 }
15933
15934 return n;
15935 }
15936
15937
15938 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
15939 0, 4, 0,
15940 doc: /* Return the mode-line of selected window as a string.
15941 First optional arg FORMAT specifies a different format string (see
15942 `mode-line-format' for details) to use. If FORMAT is t, return
15943 the buffer's header-line. Second optional arg WINDOW specifies a
15944 different window to use as the context for the formatting.
15945 If third optional arg NO-PROPS is non-nil, string is not propertized.
15946 Fourth optional arg BUFFER specifies which buffer to use. */)
15947 (format, window, no_props, buffer)
15948 Lisp_Object format, window, no_props, buffer;
15949 {
15950 struct it it;
15951 int len;
15952 struct window *w;
15953 struct buffer *old_buffer = NULL;
15954 enum face_id face_id = DEFAULT_FACE_ID;
15955
15956 if (NILP (window))
15957 window = selected_window;
15958 CHECK_WINDOW (window);
15959 w = XWINDOW (window);
15960
15961 if (NILP (buffer))
15962 buffer = w->buffer;
15963
15964 CHECK_BUFFER (buffer);
15965
15966 if (XBUFFER (buffer) != current_buffer)
15967 {
15968 old_buffer = current_buffer;
15969 set_buffer_internal_1 (XBUFFER (buffer));
15970 }
15971
15972 if (NILP (format) || EQ (format, Qt))
15973 {
15974 face_id = (NILP (format)
15975 ? CURRENT_MODE_LINE_FACE_ID (w)
15976 : HEADER_LINE_FACE_ID);
15977 format = (NILP (format)
15978 ? current_buffer->mode_line_format
15979 : current_buffer->header_line_format);
15980 }
15981
15982 init_iterator (&it, w, -1, -1, NULL, face_id);
15983
15984 if (NILP (no_props))
15985 {
15986 mode_line_string_face
15987 = (face_id == MODE_LINE_FACE_ID ? Qmode_line
15988 : face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive
15989 : face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
15990
15991 mode_line_string_face_prop
15992 = (NILP (mode_line_string_face) ? Qnil
15993 : Fcons (Qface, Fcons (mode_line_string_face, Qnil)));
15994
15995 /* We need a dummy last element in mode_line_string_list to
15996 indicate we are building the propertized mode-line string.
15997 Using mode_line_string_face_prop here GC protects it. */
15998 mode_line_string_list
15999 = Fcons (mode_line_string_face_prop, Qnil);
16000 frame_title_ptr = NULL;
16001 }
16002 else
16003 {
16004 mode_line_string_face_prop = Qnil;
16005 mode_line_string_list = Qnil;
16006 frame_title_ptr = frame_title_buf;
16007 }
16008
16009 push_frame_kboard (it.f);
16010 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16011 pop_frame_kboard ();
16012
16013 if (old_buffer)
16014 set_buffer_internal_1 (old_buffer);
16015
16016 if (NILP (no_props))
16017 {
16018 Lisp_Object str;
16019 mode_line_string_list = Fnreverse (mode_line_string_list);
16020 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
16021 make_string ("", 0));
16022 mode_line_string_face_prop = Qnil;
16023 mode_line_string_list = Qnil;
16024 return str;
16025 }
16026
16027 len = frame_title_ptr - frame_title_buf;
16028 if (len > 0 && frame_title_ptr[-1] == '-')
16029 {
16030 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
16031 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
16032 ;
16033 frame_title_ptr += 3; /* restore last non-dash + two dashes */
16034 if (len > frame_title_ptr - frame_title_buf)
16035 len = frame_title_ptr - frame_title_buf;
16036 }
16037
16038 frame_title_ptr = NULL;
16039 return make_string (frame_title_buf, len);
16040 }
16041
16042 /* Write a null-terminated, right justified decimal representation of
16043 the positive integer D to BUF using a minimal field width WIDTH. */
16044
16045 static void
16046 pint2str (buf, width, d)
16047 register char *buf;
16048 register int width;
16049 register int d;
16050 {
16051 register char *p = buf;
16052
16053 if (d <= 0)
16054 *p++ = '0';
16055 else
16056 {
16057 while (d > 0)
16058 {
16059 *p++ = d % 10 + '0';
16060 d /= 10;
16061 }
16062 }
16063
16064 for (width -= (int) (p - buf); width > 0; --width)
16065 *p++ = ' ';
16066 *p-- = '\0';
16067 while (p > buf)
16068 {
16069 d = *buf;
16070 *buf++ = *p;
16071 *p-- = d;
16072 }
16073 }
16074
16075 /* Write a null-terminated, right justified decimal and "human
16076 readable" representation of the nonnegative integer D to BUF using
16077 a minimal field width WIDTH. D should be smaller than 999.5e24. */
16078
16079 static const char power_letter[] =
16080 {
16081 0, /* not used */
16082 'k', /* kilo */
16083 'M', /* mega */
16084 'G', /* giga */
16085 'T', /* tera */
16086 'P', /* peta */
16087 'E', /* exa */
16088 'Z', /* zetta */
16089 'Y' /* yotta */
16090 };
16091
16092 static void
16093 pint2hrstr (buf, width, d)
16094 char *buf;
16095 int width;
16096 int d;
16097 {
16098 /* We aim to represent the nonnegative integer D as
16099 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
16100 int quotient = d;
16101 int remainder = 0;
16102 /* -1 means: do not use TENTHS. */
16103 int tenths = -1;
16104 int exponent = 0;
16105
16106 /* Length of QUOTIENT.TENTHS as a string. */
16107 int length;
16108
16109 char * psuffix;
16110 char * p;
16111
16112 if (1000 <= quotient)
16113 {
16114 /* Scale to the appropriate EXPONENT. */
16115 do
16116 {
16117 remainder = quotient % 1000;
16118 quotient /= 1000;
16119 exponent++;
16120 }
16121 while (1000 <= quotient);
16122
16123 /* Round to nearest and decide whether to use TENTHS or not. */
16124 if (quotient <= 9)
16125 {
16126 tenths = remainder / 100;
16127 if (50 <= remainder % 100)
16128 {
16129 if (tenths < 9)
16130 tenths++;
16131 else
16132 {
16133 quotient++;
16134 if (quotient == 10)
16135 tenths = -1;
16136 else
16137 tenths = 0;
16138 }
16139 }
16140 }
16141 else
16142 if (500 <= remainder)
16143 {
16144 if (quotient < 999)
16145 quotient++;
16146 else
16147 {
16148 quotient = 1;
16149 exponent++;
16150 tenths = 0;
16151 }
16152 }
16153 }
16154
16155 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
16156 if (tenths == -1 && quotient <= 99)
16157 if (quotient <= 9)
16158 length = 1;
16159 else
16160 length = 2;
16161 else
16162 length = 3;
16163 p = psuffix = buf + max (width, length);
16164
16165 /* Print EXPONENT. */
16166 if (exponent)
16167 *psuffix++ = power_letter[exponent];
16168 *psuffix = '\0';
16169
16170 /* Print TENTHS. */
16171 if (tenths >= 0)
16172 {
16173 *--p = '0' + tenths;
16174 *--p = '.';
16175 }
16176
16177 /* Print QUOTIENT. */
16178 do
16179 {
16180 int digit = quotient % 10;
16181 *--p = '0' + digit;
16182 }
16183 while ((quotient /= 10) != 0);
16184
16185 /* Print leading spaces. */
16186 while (buf < p)
16187 *--p = ' ';
16188 }
16189
16190 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
16191 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
16192 type of CODING_SYSTEM. Return updated pointer into BUF. */
16193
16194 static unsigned char invalid_eol_type[] = "(*invalid*)";
16195
16196 static char *
16197 decode_mode_spec_coding (coding_system, buf, eol_flag)
16198 Lisp_Object coding_system;
16199 register char *buf;
16200 int eol_flag;
16201 {
16202 Lisp_Object val;
16203 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
16204 const unsigned char *eol_str;
16205 int eol_str_len;
16206 /* The EOL conversion we are using. */
16207 Lisp_Object eoltype;
16208
16209 val = Fget (coding_system, Qcoding_system);
16210 eoltype = Qnil;
16211
16212 if (!VECTORP (val)) /* Not yet decided. */
16213 {
16214 if (multibyte)
16215 *buf++ = '-';
16216 if (eol_flag)
16217 eoltype = eol_mnemonic_undecided;
16218 /* Don't mention EOL conversion if it isn't decided. */
16219 }
16220 else
16221 {
16222 Lisp_Object eolvalue;
16223
16224 eolvalue = Fget (coding_system, Qeol_type);
16225
16226 if (multibyte)
16227 *buf++ = XFASTINT (AREF (val, 1));
16228
16229 if (eol_flag)
16230 {
16231 /* The EOL conversion that is normal on this system. */
16232
16233 if (NILP (eolvalue)) /* Not yet decided. */
16234 eoltype = eol_mnemonic_undecided;
16235 else if (VECTORP (eolvalue)) /* Not yet decided. */
16236 eoltype = eol_mnemonic_undecided;
16237 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
16238 eoltype = (XFASTINT (eolvalue) == 0
16239 ? eol_mnemonic_unix
16240 : (XFASTINT (eolvalue) == 1
16241 ? eol_mnemonic_dos : eol_mnemonic_mac));
16242 }
16243 }
16244
16245 if (eol_flag)
16246 {
16247 /* Mention the EOL conversion if it is not the usual one. */
16248 if (STRINGP (eoltype))
16249 {
16250 eol_str = SDATA (eoltype);
16251 eol_str_len = SBYTES (eoltype);
16252 }
16253 else if (INTEGERP (eoltype)
16254 && CHAR_VALID_P (XINT (eoltype), 0))
16255 {
16256 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16257 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16258 eol_str = tmp;
16259 }
16260 else
16261 {
16262 eol_str = invalid_eol_type;
16263 eol_str_len = sizeof (invalid_eol_type) - 1;
16264 }
16265 bcopy (eol_str, buf, eol_str_len);
16266 buf += eol_str_len;
16267 }
16268
16269 return buf;
16270 }
16271
16272 /* Return a string for the output of a mode line %-spec for window W,
16273 generated by character C. PRECISION >= 0 means don't return a
16274 string longer than that value. FIELD_WIDTH > 0 means pad the
16275 string returned with spaces to that value. Return 1 in *MULTIBYTE
16276 if the result is multibyte text.
16277
16278 Note we operate on the current buffer for most purposes,
16279 the exception being w->base_line_pos. */
16280
16281 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16282
16283 static char *
16284 decode_mode_spec (w, c, field_width, precision, multibyte)
16285 struct window *w;
16286 register int c;
16287 int field_width, precision;
16288 int *multibyte;
16289 {
16290 Lisp_Object obj;
16291 struct frame *f = XFRAME (WINDOW_FRAME (w));
16292 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16293 struct buffer *b = current_buffer;
16294
16295 obj = Qnil;
16296 *multibyte = 0;
16297
16298 switch (c)
16299 {
16300 case '*':
16301 if (!NILP (b->read_only))
16302 return "%";
16303 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16304 return "*";
16305 return "-";
16306
16307 case '+':
16308 /* This differs from %* only for a modified read-only buffer. */
16309 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16310 return "*";
16311 if (!NILP (b->read_only))
16312 return "%";
16313 return "-";
16314
16315 case '&':
16316 /* This differs from %* in ignoring read-only-ness. */
16317 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16318 return "*";
16319 return "-";
16320
16321 case '%':
16322 return "%";
16323
16324 case '[':
16325 {
16326 int i;
16327 char *p;
16328
16329 if (command_loop_level > 5)
16330 return "[[[... ";
16331 p = decode_mode_spec_buf;
16332 for (i = 0; i < command_loop_level; i++)
16333 *p++ = '[';
16334 *p = 0;
16335 return decode_mode_spec_buf;
16336 }
16337
16338 case ']':
16339 {
16340 int i;
16341 char *p;
16342
16343 if (command_loop_level > 5)
16344 return " ...]]]";
16345 p = decode_mode_spec_buf;
16346 for (i = 0; i < command_loop_level; i++)
16347 *p++ = ']';
16348 *p = 0;
16349 return decode_mode_spec_buf;
16350 }
16351
16352 case '-':
16353 {
16354 register int i;
16355
16356 /* Let lots_of_dashes be a string of infinite length. */
16357 if (!NILP (mode_line_string_list))
16358 return "--";
16359 if (field_width <= 0
16360 || field_width > sizeof (lots_of_dashes))
16361 {
16362 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16363 decode_mode_spec_buf[i] = '-';
16364 decode_mode_spec_buf[i] = '\0';
16365 return decode_mode_spec_buf;
16366 }
16367 else
16368 return lots_of_dashes;
16369 }
16370
16371 case 'b':
16372 obj = b->name;
16373 break;
16374
16375 case 'c':
16376 {
16377 int col = (int) current_column (); /* iftc */
16378 w->column_number_displayed = make_number (col);
16379 pint2str (decode_mode_spec_buf, field_width, col);
16380 return decode_mode_spec_buf;
16381 }
16382
16383 case 'F':
16384 /* %F displays the frame name. */
16385 if (!NILP (f->title))
16386 return (char *) SDATA (f->title);
16387 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16388 return (char *) SDATA (f->name);
16389 return "Emacs";
16390
16391 case 'f':
16392 obj = b->filename;
16393 break;
16394
16395 case 'i':
16396 {
16397 int size = ZV - BEGV;
16398 pint2str (decode_mode_spec_buf, field_width, size);
16399 return decode_mode_spec_buf;
16400 }
16401
16402 case 'I':
16403 {
16404 int size = ZV - BEGV;
16405 pint2hrstr (decode_mode_spec_buf, field_width, size);
16406 return decode_mode_spec_buf;
16407 }
16408
16409 case 'l':
16410 {
16411 int startpos = XMARKER (w->start)->charpos;
16412 int startpos_byte = marker_byte_position (w->start);
16413 int line, linepos, linepos_byte, topline;
16414 int nlines, junk;
16415 int height = WINDOW_TOTAL_LINES (w);
16416
16417 /* If we decided that this buffer isn't suitable for line numbers,
16418 don't forget that too fast. */
16419 if (EQ (w->base_line_pos, w->buffer))
16420 goto no_value;
16421 /* But do forget it, if the window shows a different buffer now. */
16422 else if (BUFFERP (w->base_line_pos))
16423 w->base_line_pos = Qnil;
16424
16425 /* If the buffer is very big, don't waste time. */
16426 if (INTEGERP (Vline_number_display_limit)
16427 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16428 {
16429 w->base_line_pos = Qnil;
16430 w->base_line_number = Qnil;
16431 goto no_value;
16432 }
16433
16434 if (!NILP (w->base_line_number)
16435 && !NILP (w->base_line_pos)
16436 && XFASTINT (w->base_line_pos) <= startpos)
16437 {
16438 line = XFASTINT (w->base_line_number);
16439 linepos = XFASTINT (w->base_line_pos);
16440 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16441 }
16442 else
16443 {
16444 line = 1;
16445 linepos = BUF_BEGV (b);
16446 linepos_byte = BUF_BEGV_BYTE (b);
16447 }
16448
16449 /* Count lines from base line to window start position. */
16450 nlines = display_count_lines (linepos, linepos_byte,
16451 startpos_byte,
16452 startpos, &junk);
16453
16454 topline = nlines + line;
16455
16456 /* Determine a new base line, if the old one is too close
16457 or too far away, or if we did not have one.
16458 "Too close" means it's plausible a scroll-down would
16459 go back past it. */
16460 if (startpos == BUF_BEGV (b))
16461 {
16462 w->base_line_number = make_number (topline);
16463 w->base_line_pos = make_number (BUF_BEGV (b));
16464 }
16465 else if (nlines < height + 25 || nlines > height * 3 + 50
16466 || linepos == BUF_BEGV (b))
16467 {
16468 int limit = BUF_BEGV (b);
16469 int limit_byte = BUF_BEGV_BYTE (b);
16470 int position;
16471 int distance = (height * 2 + 30) * line_number_display_limit_width;
16472
16473 if (startpos - distance > limit)
16474 {
16475 limit = startpos - distance;
16476 limit_byte = CHAR_TO_BYTE (limit);
16477 }
16478
16479 nlines = display_count_lines (startpos, startpos_byte,
16480 limit_byte,
16481 - (height * 2 + 30),
16482 &position);
16483 /* If we couldn't find the lines we wanted within
16484 line_number_display_limit_width chars per line,
16485 give up on line numbers for this window. */
16486 if (position == limit_byte && limit == startpos - distance)
16487 {
16488 w->base_line_pos = w->buffer;
16489 w->base_line_number = Qnil;
16490 goto no_value;
16491 }
16492
16493 w->base_line_number = make_number (topline - nlines);
16494 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
16495 }
16496
16497 /* Now count lines from the start pos to point. */
16498 nlines = display_count_lines (startpos, startpos_byte,
16499 PT_BYTE, PT, &junk);
16500
16501 /* Record that we did display the line number. */
16502 line_number_displayed = 1;
16503
16504 /* Make the string to show. */
16505 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
16506 return decode_mode_spec_buf;
16507 no_value:
16508 {
16509 char* p = decode_mode_spec_buf;
16510 int pad = field_width - 2;
16511 while (pad-- > 0)
16512 *p++ = ' ';
16513 *p++ = '?';
16514 *p++ = '?';
16515 *p = '\0';
16516 return decode_mode_spec_buf;
16517 }
16518 }
16519 break;
16520
16521 case 'm':
16522 obj = b->mode_name;
16523 break;
16524
16525 case 'n':
16526 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
16527 return " Narrow";
16528 break;
16529
16530 case 'p':
16531 {
16532 int pos = marker_position (w->start);
16533 int total = BUF_ZV (b) - BUF_BEGV (b);
16534
16535 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
16536 {
16537 if (pos <= BUF_BEGV (b))
16538 return "All";
16539 else
16540 return "Bottom";
16541 }
16542 else if (pos <= BUF_BEGV (b))
16543 return "Top";
16544 else
16545 {
16546 if (total > 1000000)
16547 /* Do it differently for a large value, to avoid overflow. */
16548 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16549 else
16550 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
16551 /* We can't normally display a 3-digit number,
16552 so get us a 2-digit number that is close. */
16553 if (total == 100)
16554 total = 99;
16555 sprintf (decode_mode_spec_buf, "%2d%%", total);
16556 return decode_mode_spec_buf;
16557 }
16558 }
16559
16560 /* Display percentage of size above the bottom of the screen. */
16561 case 'P':
16562 {
16563 int toppos = marker_position (w->start);
16564 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16565 int total = BUF_ZV (b) - BUF_BEGV (b);
16566
16567 if (botpos >= BUF_ZV (b))
16568 {
16569 if (toppos <= BUF_BEGV (b))
16570 return "All";
16571 else
16572 return "Bottom";
16573 }
16574 else
16575 {
16576 if (total > 1000000)
16577 /* Do it differently for a large value, to avoid overflow. */
16578 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16579 else
16580 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
16581 /* We can't normally display a 3-digit number,
16582 so get us a 2-digit number that is close. */
16583 if (total == 100)
16584 total = 99;
16585 if (toppos <= BUF_BEGV (b))
16586 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
16587 else
16588 sprintf (decode_mode_spec_buf, "%2d%%", total);
16589 return decode_mode_spec_buf;
16590 }
16591 }
16592
16593 case 's':
16594 /* status of process */
16595 obj = Fget_buffer_process (Fcurrent_buffer ());
16596 if (NILP (obj))
16597 return "no process";
16598 #ifdef subprocesses
16599 obj = Fsymbol_name (Fprocess_status (obj));
16600 #endif
16601 break;
16602
16603 case 't': /* indicate TEXT or BINARY */
16604 #ifdef MODE_LINE_BINARY_TEXT
16605 return MODE_LINE_BINARY_TEXT (b);
16606 #else
16607 return "T";
16608 #endif
16609
16610 case 'z':
16611 /* coding-system (not including end-of-line format) */
16612 case 'Z':
16613 /* coding-system (including end-of-line type) */
16614 {
16615 int eol_flag = (c == 'Z');
16616 char *p = decode_mode_spec_buf;
16617
16618 if (! FRAME_WINDOW_P (f))
16619 {
16620 /* No need to mention EOL here--the terminal never needs
16621 to do EOL conversion. */
16622 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
16623 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
16624 }
16625 p = decode_mode_spec_coding (b->buffer_file_coding_system,
16626 p, eol_flag);
16627
16628 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
16629 #ifdef subprocesses
16630 obj = Fget_buffer_process (Fcurrent_buffer ());
16631 if (PROCESSP (obj))
16632 {
16633 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16634 p, eol_flag);
16635 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16636 p, eol_flag);
16637 }
16638 #endif /* subprocesses */
16639 #endif /* 0 */
16640 *p = 0;
16641 return decode_mode_spec_buf;
16642 }
16643 }
16644
16645 if (STRINGP (obj))
16646 {
16647 *multibyte = STRING_MULTIBYTE (obj);
16648 return (char *) SDATA (obj);
16649 }
16650 else
16651 return "";
16652 }
16653
16654
16655 /* Count up to COUNT lines starting from START / START_BYTE.
16656 But don't go beyond LIMIT_BYTE.
16657 Return the number of lines thus found (always nonnegative).
16658
16659 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
16660
16661 static int
16662 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16663 int start, start_byte, limit_byte, count;
16664 int *byte_pos_ptr;
16665 {
16666 register unsigned char *cursor;
16667 unsigned char *base;
16668
16669 register int ceiling;
16670 register unsigned char *ceiling_addr;
16671 int orig_count = count;
16672
16673 /* If we are not in selective display mode,
16674 check only for newlines. */
16675 int selective_display = (!NILP (current_buffer->selective_display)
16676 && !INTEGERP (current_buffer->selective_display));
16677
16678 if (count > 0)
16679 {
16680 while (start_byte < limit_byte)
16681 {
16682 ceiling = BUFFER_CEILING_OF (start_byte);
16683 ceiling = min (limit_byte - 1, ceiling);
16684 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16685 base = (cursor = BYTE_POS_ADDR (start_byte));
16686 while (1)
16687 {
16688 if (selective_display)
16689 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16690 ;
16691 else
16692 while (*cursor != '\n' && ++cursor != ceiling_addr)
16693 ;
16694
16695 if (cursor != ceiling_addr)
16696 {
16697 if (--count == 0)
16698 {
16699 start_byte += cursor - base + 1;
16700 *byte_pos_ptr = start_byte;
16701 return orig_count;
16702 }
16703 else
16704 if (++cursor == ceiling_addr)
16705 break;
16706 }
16707 else
16708 break;
16709 }
16710 start_byte += cursor - base;
16711 }
16712 }
16713 else
16714 {
16715 while (start_byte > limit_byte)
16716 {
16717 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16718 ceiling = max (limit_byte, ceiling);
16719 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16720 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16721 while (1)
16722 {
16723 if (selective_display)
16724 while (--cursor != ceiling_addr
16725 && *cursor != '\n' && *cursor != 015)
16726 ;
16727 else
16728 while (--cursor != ceiling_addr && *cursor != '\n')
16729 ;
16730
16731 if (cursor != ceiling_addr)
16732 {
16733 if (++count == 0)
16734 {
16735 start_byte += cursor - base + 1;
16736 *byte_pos_ptr = start_byte;
16737 /* When scanning backwards, we should
16738 not count the newline posterior to which we stop. */
16739 return - orig_count - 1;
16740 }
16741 }
16742 else
16743 break;
16744 }
16745 /* Here we add 1 to compensate for the last decrement
16746 of CURSOR, which took it past the valid range. */
16747 start_byte += cursor - base + 1;
16748 }
16749 }
16750
16751 *byte_pos_ptr = limit_byte;
16752
16753 if (count < 0)
16754 return - orig_count + count;
16755 return orig_count - count;
16756
16757 }
16758
16759
16760 \f
16761 /***********************************************************************
16762 Displaying strings
16763 ***********************************************************************/
16764
16765 /* Display a NUL-terminated string, starting with index START.
16766
16767 If STRING is non-null, display that C string. Otherwise, the Lisp
16768 string LISP_STRING is displayed.
16769
16770 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16771 FACE_STRING. Display STRING or LISP_STRING with the face at
16772 FACE_STRING_POS in FACE_STRING:
16773
16774 Display the string in the environment given by IT, but use the
16775 standard display table, temporarily.
16776
16777 FIELD_WIDTH is the minimum number of output glyphs to produce.
16778 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16779 with spaces. If STRING has more characters, more than FIELD_WIDTH
16780 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
16781
16782 PRECISION is the maximum number of characters to output from
16783 STRING. PRECISION < 0 means don't truncate the string.
16784
16785 This is roughly equivalent to printf format specifiers:
16786
16787 FIELD_WIDTH PRECISION PRINTF
16788 ----------------------------------------
16789 -1 -1 %s
16790 -1 10 %.10s
16791 10 -1 %10s
16792 20 10 %20.10s
16793
16794 MULTIBYTE zero means do not display multibyte chars, > 0 means do
16795 display them, and < 0 means obey the current buffer's value of
16796 enable_multibyte_characters.
16797
16798 Value is the number of glyphs produced. */
16799
16800 static int
16801 display_string (string, lisp_string, face_string, face_string_pos,
16802 start, it, field_width, precision, max_x, multibyte)
16803 unsigned char *string;
16804 Lisp_Object lisp_string;
16805 Lisp_Object face_string;
16806 int face_string_pos;
16807 int start;
16808 struct it *it;
16809 int field_width, precision, max_x;
16810 int multibyte;
16811 {
16812 int hpos_at_start = it->hpos;
16813 int saved_face_id = it->face_id;
16814 struct glyph_row *row = it->glyph_row;
16815
16816 /* Initialize the iterator IT for iteration over STRING beginning
16817 with index START. */
16818 reseat_to_string (it, string, lisp_string, start,
16819 precision, field_width, multibyte);
16820
16821 /* If displaying STRING, set up the face of the iterator
16822 from LISP_STRING, if that's given. */
16823 if (STRINGP (face_string))
16824 {
16825 int endptr;
16826 struct face *face;
16827
16828 it->face_id
16829 = face_at_string_position (it->w, face_string, face_string_pos,
16830 0, it->region_beg_charpos,
16831 it->region_end_charpos,
16832 &endptr, it->base_face_id, 0);
16833 face = FACE_FROM_ID (it->f, it->face_id);
16834 it->face_box_p = face->box != FACE_NO_BOX;
16835 }
16836
16837 /* Set max_x to the maximum allowed X position. Don't let it go
16838 beyond the right edge of the window. */
16839 if (max_x <= 0)
16840 max_x = it->last_visible_x;
16841 else
16842 max_x = min (max_x, it->last_visible_x);
16843
16844 /* Skip over display elements that are not visible. because IT->w is
16845 hscrolled. */
16846 if (it->current_x < it->first_visible_x)
16847 move_it_in_display_line_to (it, 100000, it->first_visible_x,
16848 MOVE_TO_POS | MOVE_TO_X);
16849
16850 row->ascent = it->max_ascent;
16851 row->height = it->max_ascent + it->max_descent;
16852 row->phys_ascent = it->max_phys_ascent;
16853 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16854 row->extra_line_spacing = it->max_extra_line_spacing;
16855
16856 /* This condition is for the case that we are called with current_x
16857 past last_visible_x. */
16858 while (it->current_x < max_x)
16859 {
16860 int x_before, x, n_glyphs_before, i, nglyphs;
16861
16862 /* Get the next display element. */
16863 if (!get_next_display_element (it))
16864 break;
16865
16866 /* Produce glyphs. */
16867 x_before = it->current_x;
16868 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
16869 PRODUCE_GLYPHS (it);
16870
16871 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
16872 i = 0;
16873 x = x_before;
16874 while (i < nglyphs)
16875 {
16876 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16877
16878 if (!it->truncate_lines_p
16879 && x + glyph->pixel_width > max_x)
16880 {
16881 /* End of continued line or max_x reached. */
16882 if (CHAR_GLYPH_PADDING_P (*glyph))
16883 {
16884 /* A wide character is unbreakable. */
16885 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
16886 it->current_x = x_before;
16887 }
16888 else
16889 {
16890 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
16891 it->current_x = x;
16892 }
16893 break;
16894 }
16895 else if (x + glyph->pixel_width > it->first_visible_x)
16896 {
16897 /* Glyph is at least partially visible. */
16898 ++it->hpos;
16899 if (x < it->first_visible_x)
16900 it->glyph_row->x = x - it->first_visible_x;
16901 }
16902 else
16903 {
16904 /* Glyph is off the left margin of the display area.
16905 Should not happen. */
16906 abort ();
16907 }
16908
16909 row->ascent = max (row->ascent, it->max_ascent);
16910 row->height = max (row->height, it->max_ascent + it->max_descent);
16911 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16912 row->phys_height = max (row->phys_height,
16913 it->max_phys_ascent + it->max_phys_descent);
16914 row->extra_line_spacing = max (row->extra_line_spacing,
16915 it->max_extra_line_spacing);
16916 x += glyph->pixel_width;
16917 ++i;
16918 }
16919
16920 /* Stop if max_x reached. */
16921 if (i < nglyphs)
16922 break;
16923
16924 /* Stop at line ends. */
16925 if (ITERATOR_AT_END_OF_LINE_P (it))
16926 {
16927 it->continuation_lines_width = 0;
16928 break;
16929 }
16930
16931 set_iterator_to_next (it, 1);
16932
16933 /* Stop if truncating at the right edge. */
16934 if (it->truncate_lines_p
16935 && it->current_x >= it->last_visible_x)
16936 {
16937 /* Add truncation mark, but don't do it if the line is
16938 truncated at a padding space. */
16939 if (IT_CHARPOS (*it) < it->string_nchars)
16940 {
16941 if (!FRAME_WINDOW_P (it->f))
16942 {
16943 int i, n;
16944
16945 if (it->current_x > it->last_visible_x)
16946 {
16947 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16948 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16949 break;
16950 for (n = row->used[TEXT_AREA]; i < n; ++i)
16951 {
16952 row->used[TEXT_AREA] = i;
16953 produce_special_glyphs (it, IT_TRUNCATION);
16954 }
16955 }
16956 produce_special_glyphs (it, IT_TRUNCATION);
16957 }
16958 it->glyph_row->truncated_on_right_p = 1;
16959 }
16960 break;
16961 }
16962 }
16963
16964 /* Maybe insert a truncation at the left. */
16965 if (it->first_visible_x
16966 && IT_CHARPOS (*it) > 0)
16967 {
16968 if (!FRAME_WINDOW_P (it->f))
16969 insert_left_trunc_glyphs (it);
16970 it->glyph_row->truncated_on_left_p = 1;
16971 }
16972
16973 it->face_id = saved_face_id;
16974
16975 /* Value is number of columns displayed. */
16976 return it->hpos - hpos_at_start;
16977 }
16978
16979
16980 \f
16981 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
16982 appears as an element of LIST or as the car of an element of LIST.
16983 If PROPVAL is a list, compare each element against LIST in that
16984 way, and return 1/2 if any element of PROPVAL is found in LIST.
16985 Otherwise return 0. This function cannot quit.
16986 The return value is 2 if the text is invisible but with an ellipsis
16987 and 1 if it's invisible and without an ellipsis. */
16988
16989 int
16990 invisible_p (propval, list)
16991 register Lisp_Object propval;
16992 Lisp_Object list;
16993 {
16994 register Lisp_Object tail, proptail;
16995
16996 for (tail = list; CONSP (tail); tail = XCDR (tail))
16997 {
16998 register Lisp_Object tem;
16999 tem = XCAR (tail);
17000 if (EQ (propval, tem))
17001 return 1;
17002 if (CONSP (tem) && EQ (propval, XCAR (tem)))
17003 return NILP (XCDR (tem)) ? 1 : 2;
17004 }
17005
17006 if (CONSP (propval))
17007 {
17008 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
17009 {
17010 Lisp_Object propelt;
17011 propelt = XCAR (proptail);
17012 for (tail = list; CONSP (tail); tail = XCDR (tail))
17013 {
17014 register Lisp_Object tem;
17015 tem = XCAR (tail);
17016 if (EQ (propelt, tem))
17017 return 1;
17018 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
17019 return NILP (XCDR (tem)) ? 1 : 2;
17020 }
17021 }
17022 }
17023
17024 return 0;
17025 }
17026
17027 /* Calculate a width or height in pixels from a specification using
17028 the following elements:
17029
17030 SPEC ::=
17031 NUM - a (fractional) multiple of the default font width/height
17032 (NUM) - specifies exactly NUM pixels
17033 UNIT - a fixed number of pixels, see below.
17034 ELEMENT - size of a display element in pixels, see below.
17035 (NUM . SPEC) - equals NUM * SPEC
17036 (+ SPEC SPEC ...) - add pixel values
17037 (- SPEC SPEC ...) - subtract pixel values
17038 (- SPEC) - negate pixel value
17039
17040 NUM ::=
17041 INT or FLOAT - a number constant
17042 SYMBOL - use symbol's (buffer local) variable binding.
17043
17044 UNIT ::=
17045 in - pixels per inch *)
17046 mm - pixels per 1/1000 meter *)
17047 cm - pixels per 1/100 meter *)
17048 width - width of current font in pixels.
17049 height - height of current font in pixels.
17050
17051 *) using the ratio(s) defined in display-pixels-per-inch.
17052
17053 ELEMENT ::=
17054
17055 left-fringe - left fringe width in pixels
17056 right-fringe - right fringe width in pixels
17057
17058 left-margin - left margin width in pixels
17059 right-margin - right margin width in pixels
17060
17061 scroll-bar - scroll-bar area width in pixels
17062
17063 Examples:
17064
17065 Pixels corresponding to 5 inches:
17066 (5 . in)
17067
17068 Total width of non-text areas on left side of window (if scroll-bar is on left):
17069 '(space :width (+ left-fringe left-margin scroll-bar))
17070
17071 Align to first text column (in header line):
17072 '(space :align-to 0)
17073
17074 Align to middle of text area minus half the width of variable `my-image'
17075 containing a loaded image:
17076 '(space :align-to (0.5 . (- text my-image)))
17077
17078 Width of left margin minus width of 1 character in the default font:
17079 '(space :width (- left-margin 1))
17080
17081 Width of left margin minus width of 2 characters in the current font:
17082 '(space :width (- left-margin (2 . width)))
17083
17084 Center 1 character over left-margin (in header line):
17085 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
17086
17087 Different ways to express width of left fringe plus left margin minus one pixel:
17088 '(space :width (- (+ left-fringe left-margin) (1)))
17089 '(space :width (+ left-fringe left-margin (- (1))))
17090 '(space :width (+ left-fringe left-margin (-1)))
17091
17092 */
17093
17094 #define NUMVAL(X) \
17095 ((INTEGERP (X) || FLOATP (X)) \
17096 ? XFLOATINT (X) \
17097 : - 1)
17098
17099 int
17100 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
17101 double *res;
17102 struct it *it;
17103 Lisp_Object prop;
17104 void *font;
17105 int width_p, *align_to;
17106 {
17107 double pixels;
17108
17109 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
17110 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
17111
17112 if (NILP (prop))
17113 return OK_PIXELS (0);
17114
17115 if (SYMBOLP (prop))
17116 {
17117 if (SCHARS (SYMBOL_NAME (prop)) == 2)
17118 {
17119 char *unit = SDATA (SYMBOL_NAME (prop));
17120
17121 if (unit[0] == 'i' && unit[1] == 'n')
17122 pixels = 1.0;
17123 else if (unit[0] == 'm' && unit[1] == 'm')
17124 pixels = 25.4;
17125 else if (unit[0] == 'c' && unit[1] == 'm')
17126 pixels = 2.54;
17127 else
17128 pixels = 0;
17129 if (pixels > 0)
17130 {
17131 double ppi;
17132 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
17133 || (CONSP (Vdisplay_pixels_per_inch)
17134 && (ppi = (width_p
17135 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
17136 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
17137 ppi > 0)))
17138 return OK_PIXELS (ppi / pixels);
17139
17140 return 0;
17141 }
17142 }
17143
17144 #ifdef HAVE_WINDOW_SYSTEM
17145 if (EQ (prop, Qheight))
17146 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
17147 if (EQ (prop, Qwidth))
17148 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
17149 #else
17150 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
17151 return OK_PIXELS (1);
17152 #endif
17153
17154 if (EQ (prop, Qtext))
17155 return OK_PIXELS (width_p
17156 ? window_box_width (it->w, TEXT_AREA)
17157 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
17158
17159 if (align_to && *align_to < 0)
17160 {
17161 *res = 0;
17162 if (EQ (prop, Qleft))
17163 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
17164 if (EQ (prop, Qright))
17165 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
17166 if (EQ (prop, Qcenter))
17167 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
17168 + window_box_width (it->w, TEXT_AREA) / 2);
17169 if (EQ (prop, Qleft_fringe))
17170 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17171 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
17172 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
17173 if (EQ (prop, Qright_fringe))
17174 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17175 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17176 : window_box_right_offset (it->w, TEXT_AREA));
17177 if (EQ (prop, Qleft_margin))
17178 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
17179 if (EQ (prop, Qright_margin))
17180 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
17181 if (EQ (prop, Qscroll_bar))
17182 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
17183 ? 0
17184 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17185 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17186 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
17187 : 0)));
17188 }
17189 else
17190 {
17191 if (EQ (prop, Qleft_fringe))
17192 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
17193 if (EQ (prop, Qright_fringe))
17194 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
17195 if (EQ (prop, Qleft_margin))
17196 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
17197 if (EQ (prop, Qright_margin))
17198 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
17199 if (EQ (prop, Qscroll_bar))
17200 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
17201 }
17202
17203 prop = Fbuffer_local_value (prop, it->w->buffer);
17204 }
17205
17206 if (INTEGERP (prop) || FLOATP (prop))
17207 {
17208 int base_unit = (width_p
17209 ? FRAME_COLUMN_WIDTH (it->f)
17210 : FRAME_LINE_HEIGHT (it->f));
17211 return OK_PIXELS (XFLOATINT (prop) * base_unit);
17212 }
17213
17214 if (CONSP (prop))
17215 {
17216 Lisp_Object car = XCAR (prop);
17217 Lisp_Object cdr = XCDR (prop);
17218
17219 if (SYMBOLP (car))
17220 {
17221 #ifdef HAVE_WINDOW_SYSTEM
17222 if (valid_image_p (prop))
17223 {
17224 int id = lookup_image (it->f, prop);
17225 struct image *img = IMAGE_FROM_ID (it->f, id);
17226
17227 return OK_PIXELS (width_p ? img->width : img->height);
17228 }
17229 #endif
17230 if (EQ (car, Qplus) || EQ (car, Qminus))
17231 {
17232 int first = 1;
17233 double px;
17234
17235 pixels = 0;
17236 while (CONSP (cdr))
17237 {
17238 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
17239 font, width_p, align_to))
17240 return 0;
17241 if (first)
17242 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
17243 else
17244 pixels += px;
17245 cdr = XCDR (cdr);
17246 }
17247 if (EQ (car, Qminus))
17248 pixels = -pixels;
17249 return OK_PIXELS (pixels);
17250 }
17251
17252 car = Fbuffer_local_value (car, it->w->buffer);
17253 }
17254
17255 if (INTEGERP (car) || FLOATP (car))
17256 {
17257 double fact;
17258 pixels = XFLOATINT (car);
17259 if (NILP (cdr))
17260 return OK_PIXELS (pixels);
17261 if (calc_pixel_width_or_height (&fact, it, cdr,
17262 font, width_p, align_to))
17263 return OK_PIXELS (pixels * fact);
17264 return 0;
17265 }
17266
17267 return 0;
17268 }
17269
17270 return 0;
17271 }
17272
17273 \f
17274 /***********************************************************************
17275 Glyph Display
17276 ***********************************************************************/
17277
17278 #ifdef HAVE_WINDOW_SYSTEM
17279
17280 #if GLYPH_DEBUG
17281
17282 void
17283 dump_glyph_string (s)
17284 struct glyph_string *s;
17285 {
17286 fprintf (stderr, "glyph string\n");
17287 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17288 s->x, s->y, s->width, s->height);
17289 fprintf (stderr, " ybase = %d\n", s->ybase);
17290 fprintf (stderr, " hl = %d\n", s->hl);
17291 fprintf (stderr, " left overhang = %d, right = %d\n",
17292 s->left_overhang, s->right_overhang);
17293 fprintf (stderr, " nchars = %d\n", s->nchars);
17294 fprintf (stderr, " extends to end of line = %d\n",
17295 s->extends_to_end_of_line_p);
17296 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17297 fprintf (stderr, " bg width = %d\n", s->background_width);
17298 }
17299
17300 #endif /* GLYPH_DEBUG */
17301
17302 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17303 of XChar2b structures for S; it can't be allocated in
17304 init_glyph_string because it must be allocated via `alloca'. W
17305 is the window on which S is drawn. ROW and AREA are the glyph row
17306 and area within the row from which S is constructed. START is the
17307 index of the first glyph structure covered by S. HL is a
17308 face-override for drawing S. */
17309
17310 #ifdef HAVE_NTGUI
17311 #define OPTIONAL_HDC(hdc) hdc,
17312 #define DECLARE_HDC(hdc) HDC hdc;
17313 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17314 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17315 #endif
17316
17317 #ifndef OPTIONAL_HDC
17318 #define OPTIONAL_HDC(hdc)
17319 #define DECLARE_HDC(hdc)
17320 #define ALLOCATE_HDC(hdc, f)
17321 #define RELEASE_HDC(hdc, f)
17322 #endif
17323
17324 static void
17325 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17326 struct glyph_string *s;
17327 DECLARE_HDC (hdc)
17328 XChar2b *char2b;
17329 struct window *w;
17330 struct glyph_row *row;
17331 enum glyph_row_area area;
17332 int start;
17333 enum draw_glyphs_face hl;
17334 {
17335 bzero (s, sizeof *s);
17336 s->w = w;
17337 s->f = XFRAME (w->frame);
17338 #ifdef HAVE_NTGUI
17339 s->hdc = hdc;
17340 #endif
17341 s->display = FRAME_X_DISPLAY (s->f);
17342 s->window = FRAME_X_WINDOW (s->f);
17343 s->char2b = char2b;
17344 s->hl = hl;
17345 s->row = row;
17346 s->area = area;
17347 s->first_glyph = row->glyphs[area] + start;
17348 s->height = row->height;
17349 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17350
17351 /* Display the internal border below the tool-bar window. */
17352 if (WINDOWP (s->f->tool_bar_window)
17353 && s->w == XWINDOW (s->f->tool_bar_window))
17354 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17355
17356 s->ybase = s->y + row->ascent;
17357 }
17358
17359
17360 /* Append the list of glyph strings with head H and tail T to the list
17361 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17362
17363 static INLINE void
17364 append_glyph_string_lists (head, tail, h, t)
17365 struct glyph_string **head, **tail;
17366 struct glyph_string *h, *t;
17367 {
17368 if (h)
17369 {
17370 if (*head)
17371 (*tail)->next = h;
17372 else
17373 *head = h;
17374 h->prev = *tail;
17375 *tail = t;
17376 }
17377 }
17378
17379
17380 /* Prepend the list of glyph strings with head H and tail T to the
17381 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17382 result. */
17383
17384 static INLINE void
17385 prepend_glyph_string_lists (head, tail, h, t)
17386 struct glyph_string **head, **tail;
17387 struct glyph_string *h, *t;
17388 {
17389 if (h)
17390 {
17391 if (*head)
17392 (*head)->prev = t;
17393 else
17394 *tail = t;
17395 t->next = *head;
17396 *head = h;
17397 }
17398 }
17399
17400
17401 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17402 Set *HEAD and *TAIL to the resulting list. */
17403
17404 static INLINE void
17405 append_glyph_string (head, tail, s)
17406 struct glyph_string **head, **tail;
17407 struct glyph_string *s;
17408 {
17409 s->next = s->prev = NULL;
17410 append_glyph_string_lists (head, tail, s, s);
17411 }
17412
17413
17414 /* Get face and two-byte form of character glyph GLYPH on frame F.
17415 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17416 a pointer to a realized face that is ready for display. */
17417
17418 static INLINE struct face *
17419 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17420 struct frame *f;
17421 struct glyph *glyph;
17422 XChar2b *char2b;
17423 int *two_byte_p;
17424 {
17425 struct face *face;
17426
17427 xassert (glyph->type == CHAR_GLYPH);
17428 face = FACE_FROM_ID (f, glyph->face_id);
17429
17430 if (two_byte_p)
17431 *two_byte_p = 0;
17432
17433 if (!glyph->multibyte_p)
17434 {
17435 /* Unibyte case. We don't have to encode, but we have to make
17436 sure to use a face suitable for unibyte. */
17437 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17438 }
17439 else if (glyph->u.ch < 128
17440 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17441 {
17442 /* Case of ASCII in a face known to fit ASCII. */
17443 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17444 }
17445 else
17446 {
17447 int c1, c2, charset;
17448
17449 /* Split characters into bytes. If c2 is -1 afterwards, C is
17450 really a one-byte character so that byte1 is zero. */
17451 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17452 if (c2 > 0)
17453 STORE_XCHAR2B (char2b, c1, c2);
17454 else
17455 STORE_XCHAR2B (char2b, 0, c1);
17456
17457 /* Maybe encode the character in *CHAR2B. */
17458 if (charset != CHARSET_ASCII)
17459 {
17460 struct font_info *font_info
17461 = FONT_INFO_FROM_ID (f, face->font_info_id);
17462 if (font_info)
17463 glyph->font_type
17464 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
17465 }
17466 }
17467
17468 /* Make sure X resources of the face are allocated. */
17469 xassert (face != NULL);
17470 PREPARE_FACE_FOR_DISPLAY (f, face);
17471 return face;
17472 }
17473
17474
17475 /* Fill glyph string S with composition components specified by S->cmp.
17476
17477 FACES is an array of faces for all components of this composition.
17478 S->gidx is the index of the first component for S.
17479 OVERLAPS_P non-zero means S should draw the foreground only, and
17480 use its physical height for clipping.
17481
17482 Value is the index of a component not in S. */
17483
17484 static int
17485 fill_composite_glyph_string (s, faces, overlaps_p)
17486 struct glyph_string *s;
17487 struct face **faces;
17488 int overlaps_p;
17489 {
17490 int i;
17491
17492 xassert (s);
17493
17494 s->for_overlaps_p = overlaps_p;
17495
17496 s->face = faces[s->gidx];
17497 s->font = s->face->font;
17498 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17499
17500 /* For all glyphs of this composition, starting at the offset
17501 S->gidx, until we reach the end of the definition or encounter a
17502 glyph that requires the different face, add it to S. */
17503 ++s->nchars;
17504 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
17505 ++s->nchars;
17506
17507 /* All glyph strings for the same composition has the same width,
17508 i.e. the width set for the first component of the composition. */
17509
17510 s->width = s->first_glyph->pixel_width;
17511
17512 /* If the specified font could not be loaded, use the frame's
17513 default font, but record the fact that we couldn't load it in
17514 the glyph string so that we can draw rectangles for the
17515 characters of the glyph string. */
17516 if (s->font == NULL)
17517 {
17518 s->font_not_found_p = 1;
17519 s->font = FRAME_FONT (s->f);
17520 }
17521
17522 /* Adjust base line for subscript/superscript text. */
17523 s->ybase += s->first_glyph->voffset;
17524
17525 xassert (s->face && s->face->gc);
17526
17527 /* This glyph string must always be drawn with 16-bit functions. */
17528 s->two_byte_p = 1;
17529
17530 return s->gidx + s->nchars;
17531 }
17532
17533
17534 /* Fill glyph string S from a sequence of character glyphs.
17535
17536 FACE_ID is the face id of the string. START is the index of the
17537 first glyph to consider, END is the index of the last + 1.
17538 OVERLAPS_P non-zero means S should draw the foreground only, and
17539 use its physical height for clipping.
17540
17541 Value is the index of the first glyph not in S. */
17542
17543 static int
17544 fill_glyph_string (s, face_id, start, end, overlaps_p)
17545 struct glyph_string *s;
17546 int face_id;
17547 int start, end, overlaps_p;
17548 {
17549 struct glyph *glyph, *last;
17550 int voffset;
17551 int glyph_not_available_p;
17552
17553 xassert (s->f == XFRAME (s->w->frame));
17554 xassert (s->nchars == 0);
17555 xassert (start >= 0 && end > start);
17556
17557 s->for_overlaps_p = overlaps_p,
17558 glyph = s->row->glyphs[s->area] + start;
17559 last = s->row->glyphs[s->area] + end;
17560 voffset = glyph->voffset;
17561
17562 glyph_not_available_p = glyph->glyph_not_available_p;
17563
17564 while (glyph < last
17565 && glyph->type == CHAR_GLYPH
17566 && glyph->voffset == voffset
17567 /* Same face id implies same font, nowadays. */
17568 && glyph->face_id == face_id
17569 && glyph->glyph_not_available_p == glyph_not_available_p)
17570 {
17571 int two_byte_p;
17572
17573 s->face = get_glyph_face_and_encoding (s->f, glyph,
17574 s->char2b + s->nchars,
17575 &two_byte_p);
17576 s->two_byte_p = two_byte_p;
17577 ++s->nchars;
17578 xassert (s->nchars <= end - start);
17579 s->width += glyph->pixel_width;
17580 ++glyph;
17581 }
17582
17583 s->font = s->face->font;
17584 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17585
17586 /* If the specified font could not be loaded, use the frame's font,
17587 but record the fact that we couldn't load it in
17588 S->font_not_found_p so that we can draw rectangles for the
17589 characters of the glyph string. */
17590 if (s->font == NULL || glyph_not_available_p)
17591 {
17592 s->font_not_found_p = 1;
17593 s->font = FRAME_FONT (s->f);
17594 }
17595
17596 /* Adjust base line for subscript/superscript text. */
17597 s->ybase += voffset;
17598
17599 xassert (s->face && s->face->gc);
17600 return glyph - s->row->glyphs[s->area];
17601 }
17602
17603
17604 /* Fill glyph string S from image glyph S->first_glyph. */
17605
17606 static void
17607 fill_image_glyph_string (s)
17608 struct glyph_string *s;
17609 {
17610 xassert (s->first_glyph->type == IMAGE_GLYPH);
17611 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
17612 xassert (s->img);
17613 s->slice = s->first_glyph->slice;
17614 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
17615 s->font = s->face->font;
17616 s->width = s->first_glyph->pixel_width;
17617
17618 /* Adjust base line for subscript/superscript text. */
17619 s->ybase += s->first_glyph->voffset;
17620 }
17621
17622
17623 /* Fill glyph string S from a sequence of stretch glyphs.
17624
17625 ROW is the glyph row in which the glyphs are found, AREA is the
17626 area within the row. START is the index of the first glyph to
17627 consider, END is the index of the last + 1.
17628
17629 Value is the index of the first glyph not in S. */
17630
17631 static int
17632 fill_stretch_glyph_string (s, row, area, start, end)
17633 struct glyph_string *s;
17634 struct glyph_row *row;
17635 enum glyph_row_area area;
17636 int start, end;
17637 {
17638 struct glyph *glyph, *last;
17639 int voffset, face_id;
17640
17641 xassert (s->first_glyph->type == STRETCH_GLYPH);
17642
17643 glyph = s->row->glyphs[s->area] + start;
17644 last = s->row->glyphs[s->area] + end;
17645 face_id = glyph->face_id;
17646 s->face = FACE_FROM_ID (s->f, face_id);
17647 s->font = s->face->font;
17648 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17649 s->width = glyph->pixel_width;
17650 voffset = glyph->voffset;
17651
17652 for (++glyph;
17653 (glyph < last
17654 && glyph->type == STRETCH_GLYPH
17655 && glyph->voffset == voffset
17656 && glyph->face_id == face_id);
17657 ++glyph)
17658 s->width += glyph->pixel_width;
17659
17660 /* Adjust base line for subscript/superscript text. */
17661 s->ybase += voffset;
17662
17663 /* The case that face->gc == 0 is handled when drawing the glyph
17664 string by calling PREPARE_FACE_FOR_DISPLAY. */
17665 xassert (s->face);
17666 return glyph - s->row->glyphs[s->area];
17667 }
17668
17669
17670 /* EXPORT for RIF:
17671 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
17672 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
17673 assumed to be zero. */
17674
17675 void
17676 x_get_glyph_overhangs (glyph, f, left, right)
17677 struct glyph *glyph;
17678 struct frame *f;
17679 int *left, *right;
17680 {
17681 *left = *right = 0;
17682
17683 if (glyph->type == CHAR_GLYPH)
17684 {
17685 XFontStruct *font;
17686 struct face *face;
17687 struct font_info *font_info;
17688 XChar2b char2b;
17689 XCharStruct *pcm;
17690
17691 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
17692 font = face->font;
17693 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
17694 if (font /* ++KFS: Should this be font_info ? */
17695 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
17696 {
17697 if (pcm->rbearing > pcm->width)
17698 *right = pcm->rbearing - pcm->width;
17699 if (pcm->lbearing < 0)
17700 *left = -pcm->lbearing;
17701 }
17702 }
17703 }
17704
17705
17706 /* Return the index of the first glyph preceding glyph string S that
17707 is overwritten by S because of S's left overhang. Value is -1
17708 if no glyphs are overwritten. */
17709
17710 static int
17711 left_overwritten (s)
17712 struct glyph_string *s;
17713 {
17714 int k;
17715
17716 if (s->left_overhang)
17717 {
17718 int x = 0, i;
17719 struct glyph *glyphs = s->row->glyphs[s->area];
17720 int first = s->first_glyph - glyphs;
17721
17722 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
17723 x -= glyphs[i].pixel_width;
17724
17725 k = i + 1;
17726 }
17727 else
17728 k = -1;
17729
17730 return k;
17731 }
17732
17733
17734 /* Return the index of the first glyph preceding glyph string S that
17735 is overwriting S because of its right overhang. Value is -1 if no
17736 glyph in front of S overwrites S. */
17737
17738 static int
17739 left_overwriting (s)
17740 struct glyph_string *s;
17741 {
17742 int i, k, x;
17743 struct glyph *glyphs = s->row->glyphs[s->area];
17744 int first = s->first_glyph - glyphs;
17745
17746 k = -1;
17747 x = 0;
17748 for (i = first - 1; i >= 0; --i)
17749 {
17750 int left, right;
17751 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17752 if (x + right > 0)
17753 k = i;
17754 x -= glyphs[i].pixel_width;
17755 }
17756
17757 return k;
17758 }
17759
17760
17761 /* Return the index of the last glyph following glyph string S that is
17762 not overwritten by S because of S's right overhang. Value is -1 if
17763 no such glyph is found. */
17764
17765 static int
17766 right_overwritten (s)
17767 struct glyph_string *s;
17768 {
17769 int k = -1;
17770
17771 if (s->right_overhang)
17772 {
17773 int x = 0, i;
17774 struct glyph *glyphs = s->row->glyphs[s->area];
17775 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17776 int end = s->row->used[s->area];
17777
17778 for (i = first; i < end && s->right_overhang > x; ++i)
17779 x += glyphs[i].pixel_width;
17780
17781 k = i;
17782 }
17783
17784 return k;
17785 }
17786
17787
17788 /* Return the index of the last glyph following glyph string S that
17789 overwrites S because of its left overhang. Value is negative
17790 if no such glyph is found. */
17791
17792 static int
17793 right_overwriting (s)
17794 struct glyph_string *s;
17795 {
17796 int i, k, x;
17797 int end = s->row->used[s->area];
17798 struct glyph *glyphs = s->row->glyphs[s->area];
17799 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17800
17801 k = -1;
17802 x = 0;
17803 for (i = first; i < end; ++i)
17804 {
17805 int left, right;
17806 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17807 if (x - left < 0)
17808 k = i;
17809 x += glyphs[i].pixel_width;
17810 }
17811
17812 return k;
17813 }
17814
17815
17816 /* Get face and two-byte form of character C in face FACE_ID on frame
17817 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
17818 means we want to display multibyte text. DISPLAY_P non-zero means
17819 make sure that X resources for the face returned are allocated.
17820 Value is a pointer to a realized face that is ready for display if
17821 DISPLAY_P is non-zero. */
17822
17823 static INLINE struct face *
17824 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
17825 struct frame *f;
17826 int c, face_id;
17827 XChar2b *char2b;
17828 int multibyte_p, display_p;
17829 {
17830 struct face *face = FACE_FROM_ID (f, face_id);
17831
17832 if (!multibyte_p)
17833 {
17834 /* Unibyte case. We don't have to encode, but we have to make
17835 sure to use a face suitable for unibyte. */
17836 STORE_XCHAR2B (char2b, 0, c);
17837 face_id = FACE_FOR_CHAR (f, face, c);
17838 face = FACE_FROM_ID (f, face_id);
17839 }
17840 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
17841 {
17842 /* Case of ASCII in a face known to fit ASCII. */
17843 STORE_XCHAR2B (char2b, 0, c);
17844 }
17845 else
17846 {
17847 int c1, c2, charset;
17848
17849 /* Split characters into bytes. If c2 is -1 afterwards, C is
17850 really a one-byte character so that byte1 is zero. */
17851 SPLIT_CHAR (c, charset, c1, c2);
17852 if (c2 > 0)
17853 STORE_XCHAR2B (char2b, c1, c2);
17854 else
17855 STORE_XCHAR2B (char2b, 0, c1);
17856
17857 /* Maybe encode the character in *CHAR2B. */
17858 if (face->font != NULL)
17859 {
17860 struct font_info *font_info
17861 = FONT_INFO_FROM_ID (f, face->font_info_id);
17862 if (font_info)
17863 rif->encode_char (c, char2b, font_info, 0);
17864 }
17865 }
17866
17867 /* Make sure X resources of the face are allocated. */
17868 #ifdef HAVE_X_WINDOWS
17869 if (display_p)
17870 #endif
17871 {
17872 xassert (face != NULL);
17873 PREPARE_FACE_FOR_DISPLAY (f, face);
17874 }
17875
17876 return face;
17877 }
17878
17879
17880 /* Set background width of glyph string S. START is the index of the
17881 first glyph following S. LAST_X is the right-most x-position + 1
17882 in the drawing area. */
17883
17884 static INLINE void
17885 set_glyph_string_background_width (s, start, last_x)
17886 struct glyph_string *s;
17887 int start;
17888 int last_x;
17889 {
17890 /* If the face of this glyph string has to be drawn to the end of
17891 the drawing area, set S->extends_to_end_of_line_p. */
17892 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
17893
17894 if (start == s->row->used[s->area]
17895 && s->area == TEXT_AREA
17896 && ((s->hl == DRAW_NORMAL_TEXT
17897 && (s->row->fill_line_p
17898 || s->face->background != default_face->background
17899 || s->face->stipple != default_face->stipple
17900 || s->row->mouse_face_p))
17901 || s->hl == DRAW_MOUSE_FACE
17902 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
17903 && s->row->fill_line_p)))
17904 s->extends_to_end_of_line_p = 1;
17905
17906 /* If S extends its face to the end of the line, set its
17907 background_width to the distance to the right edge of the drawing
17908 area. */
17909 if (s->extends_to_end_of_line_p)
17910 s->background_width = last_x - s->x + 1;
17911 else
17912 s->background_width = s->width;
17913 }
17914
17915
17916 /* Compute overhangs and x-positions for glyph string S and its
17917 predecessors, or successors. X is the starting x-position for S.
17918 BACKWARD_P non-zero means process predecessors. */
17919
17920 static void
17921 compute_overhangs_and_x (s, x, backward_p)
17922 struct glyph_string *s;
17923 int x;
17924 int backward_p;
17925 {
17926 if (backward_p)
17927 {
17928 while (s)
17929 {
17930 if (rif->compute_glyph_string_overhangs)
17931 rif->compute_glyph_string_overhangs (s);
17932 x -= s->width;
17933 s->x = x;
17934 s = s->prev;
17935 }
17936 }
17937 else
17938 {
17939 while (s)
17940 {
17941 if (rif->compute_glyph_string_overhangs)
17942 rif->compute_glyph_string_overhangs (s);
17943 s->x = x;
17944 x += s->width;
17945 s = s->next;
17946 }
17947 }
17948 }
17949
17950
17951
17952 /* The following macros are only called from draw_glyphs below.
17953 They reference the following parameters of that function directly:
17954 `w', `row', `area', and `overlap_p'
17955 as well as the following local variables:
17956 `s', `f', and `hdc' (in W32) */
17957
17958 #ifdef HAVE_NTGUI
17959 /* On W32, silently add local `hdc' variable to argument list of
17960 init_glyph_string. */
17961 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17962 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
17963 #else
17964 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17965 init_glyph_string (s, char2b, w, row, area, start, hl)
17966 #endif
17967
17968 /* Add a glyph string for a stretch glyph to the list of strings
17969 between HEAD and TAIL. START is the index of the stretch glyph in
17970 row area AREA of glyph row ROW. END is the index of the last glyph
17971 in that glyph row area. X is the current output position assigned
17972 to the new glyph string constructed. HL overrides that face of the
17973 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17974 is the right-most x-position of the drawing area. */
17975
17976 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
17977 and below -- keep them on one line. */
17978 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17979 do \
17980 { \
17981 s = (struct glyph_string *) alloca (sizeof *s); \
17982 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17983 START = fill_stretch_glyph_string (s, row, area, START, END); \
17984 append_glyph_string (&HEAD, &TAIL, s); \
17985 s->x = (X); \
17986 } \
17987 while (0)
17988
17989
17990 /* Add a glyph string for an image glyph to the list of strings
17991 between HEAD and TAIL. START is the index of the image glyph in
17992 row area AREA of glyph row ROW. END is the index of the last glyph
17993 in that glyph row area. X is the current output position assigned
17994 to the new glyph string constructed. HL overrides that face of the
17995 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17996 is the right-most x-position of the drawing area. */
17997
17998 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17999 do \
18000 { \
18001 s = (struct glyph_string *) alloca (sizeof *s); \
18002 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18003 fill_image_glyph_string (s); \
18004 append_glyph_string (&HEAD, &TAIL, s); \
18005 ++START; \
18006 s->x = (X); \
18007 } \
18008 while (0)
18009
18010
18011 /* Add a glyph string for a sequence of character glyphs to the list
18012 of strings between HEAD and TAIL. START is the index of the first
18013 glyph in row area AREA of glyph row ROW that is part of the new
18014 glyph string. END is the index of the last glyph in that glyph row
18015 area. X is the current output position assigned to the new glyph
18016 string constructed. HL overrides that face of the glyph; e.g. it
18017 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
18018 right-most x-position of the drawing area. */
18019
18020 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18021 do \
18022 { \
18023 int c, face_id; \
18024 XChar2b *char2b; \
18025 \
18026 c = (row)->glyphs[area][START].u.ch; \
18027 face_id = (row)->glyphs[area][START].face_id; \
18028 \
18029 s = (struct glyph_string *) alloca (sizeof *s); \
18030 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
18031 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
18032 append_glyph_string (&HEAD, &TAIL, s); \
18033 s->x = (X); \
18034 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
18035 } \
18036 while (0)
18037
18038
18039 /* Add a glyph string for a composite sequence to the list of strings
18040 between HEAD and TAIL. START is the index of the first glyph in
18041 row area AREA of glyph row ROW that is part of the new glyph
18042 string. END is the index of the last glyph in that glyph row area.
18043 X is the current output position assigned to the new glyph string
18044 constructed. HL overrides that face of the glyph; e.g. it is
18045 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
18046 x-position of the drawing area. */
18047
18048 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18049 do { \
18050 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
18051 int face_id = (row)->glyphs[area][START].face_id; \
18052 struct face *base_face = FACE_FROM_ID (f, face_id); \
18053 struct composition *cmp = composition_table[cmp_id]; \
18054 int glyph_len = cmp->glyph_len; \
18055 XChar2b *char2b; \
18056 struct face **faces; \
18057 struct glyph_string *first_s = NULL; \
18058 int n; \
18059 \
18060 base_face = base_face->ascii_face; \
18061 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
18062 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
18063 /* At first, fill in `char2b' and `faces'. */ \
18064 for (n = 0; n < glyph_len; n++) \
18065 { \
18066 int c = COMPOSITION_GLYPH (cmp, n); \
18067 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
18068 faces[n] = FACE_FROM_ID (f, this_face_id); \
18069 get_char_face_and_encoding (f, c, this_face_id, \
18070 char2b + n, 1, 1); \
18071 } \
18072 \
18073 /* Make glyph_strings for each glyph sequence that is drawable by \
18074 the same face, and append them to HEAD/TAIL. */ \
18075 for (n = 0; n < cmp->glyph_len;) \
18076 { \
18077 s = (struct glyph_string *) alloca (sizeof *s); \
18078 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
18079 append_glyph_string (&(HEAD), &(TAIL), s); \
18080 s->cmp = cmp; \
18081 s->gidx = n; \
18082 s->x = (X); \
18083 \
18084 if (n == 0) \
18085 first_s = s; \
18086 \
18087 n = fill_composite_glyph_string (s, faces, overlaps_p); \
18088 } \
18089 \
18090 ++START; \
18091 s = first_s; \
18092 } while (0)
18093
18094
18095 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
18096 of AREA of glyph row ROW on window W between indices START and END.
18097 HL overrides the face for drawing glyph strings, e.g. it is
18098 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
18099 x-positions of the drawing area.
18100
18101 This is an ugly monster macro construct because we must use alloca
18102 to allocate glyph strings (because draw_glyphs can be called
18103 asynchronously). */
18104
18105 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18106 do \
18107 { \
18108 HEAD = TAIL = NULL; \
18109 while (START < END) \
18110 { \
18111 struct glyph *first_glyph = (row)->glyphs[area] + START; \
18112 switch (first_glyph->type) \
18113 { \
18114 case CHAR_GLYPH: \
18115 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
18116 HL, X, LAST_X); \
18117 break; \
18118 \
18119 case COMPOSITE_GLYPH: \
18120 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
18121 HL, X, LAST_X); \
18122 break; \
18123 \
18124 case STRETCH_GLYPH: \
18125 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
18126 HL, X, LAST_X); \
18127 break; \
18128 \
18129 case IMAGE_GLYPH: \
18130 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
18131 HL, X, LAST_X); \
18132 break; \
18133 \
18134 default: \
18135 abort (); \
18136 } \
18137 \
18138 set_glyph_string_background_width (s, START, LAST_X); \
18139 (X) += s->width; \
18140 } \
18141 } \
18142 while (0)
18143
18144
18145 /* Draw glyphs between START and END in AREA of ROW on window W,
18146 starting at x-position X. X is relative to AREA in W. HL is a
18147 face-override with the following meaning:
18148
18149 DRAW_NORMAL_TEXT draw normally
18150 DRAW_CURSOR draw in cursor face
18151 DRAW_MOUSE_FACE draw in mouse face.
18152 DRAW_INVERSE_VIDEO draw in mode line face
18153 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
18154 DRAW_IMAGE_RAISED draw an image with a raised relief around it
18155
18156 If OVERLAPS_P is non-zero, draw only the foreground of characters
18157 and clip to the physical height of ROW.
18158
18159 Value is the x-position reached, relative to AREA of W. */
18160
18161 static int
18162 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
18163 struct window *w;
18164 int x;
18165 struct glyph_row *row;
18166 enum glyph_row_area area;
18167 int start, end;
18168 enum draw_glyphs_face hl;
18169 int overlaps_p;
18170 {
18171 struct glyph_string *head, *tail;
18172 struct glyph_string *s;
18173 int last_x, area_width;
18174 int x_reached;
18175 int i, j;
18176 struct frame *f = XFRAME (WINDOW_FRAME (w));
18177 DECLARE_HDC (hdc);
18178
18179 ALLOCATE_HDC (hdc, f);
18180
18181 /* Let's rather be paranoid than getting a SEGV. */
18182 end = min (end, row->used[area]);
18183 start = max (0, start);
18184 start = min (end, start);
18185
18186 /* Translate X to frame coordinates. Set last_x to the right
18187 end of the drawing area. */
18188 if (row->full_width_p)
18189 {
18190 /* X is relative to the left edge of W, without scroll bars
18191 or fringes. */
18192 x += WINDOW_LEFT_EDGE_X (w);
18193 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
18194 }
18195 else
18196 {
18197 int area_left = window_box_left (w, area);
18198 x += area_left;
18199 area_width = window_box_width (w, area);
18200 last_x = area_left + area_width;
18201 }
18202
18203 /* Build a doubly-linked list of glyph_string structures between
18204 head and tail from what we have to draw. Note that the macro
18205 BUILD_GLYPH_STRINGS will modify its start parameter. That's
18206 the reason we use a separate variable `i'. */
18207 i = start;
18208 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
18209 if (tail)
18210 x_reached = tail->x + tail->background_width;
18211 else
18212 x_reached = x;
18213
18214 /* If there are any glyphs with lbearing < 0 or rbearing > width in
18215 the row, redraw some glyphs in front or following the glyph
18216 strings built above. */
18217 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
18218 {
18219 int dummy_x = 0;
18220 struct glyph_string *h, *t;
18221
18222 /* Compute overhangs for all glyph strings. */
18223 if (rif->compute_glyph_string_overhangs)
18224 for (s = head; s; s = s->next)
18225 rif->compute_glyph_string_overhangs (s);
18226
18227 /* Prepend glyph strings for glyphs in front of the first glyph
18228 string that are overwritten because of the first glyph
18229 string's left overhang. The background of all strings
18230 prepended must be drawn because the first glyph string
18231 draws over it. */
18232 i = left_overwritten (head);
18233 if (i >= 0)
18234 {
18235 j = i;
18236 BUILD_GLYPH_STRINGS (j, start, h, t,
18237 DRAW_NORMAL_TEXT, dummy_x, last_x);
18238 start = i;
18239 compute_overhangs_and_x (t, head->x, 1);
18240 prepend_glyph_string_lists (&head, &tail, h, t);
18241 }
18242
18243 /* Prepend glyph strings for glyphs in front of the first glyph
18244 string that overwrite that glyph string because of their
18245 right overhang. For these strings, only the foreground must
18246 be drawn, because it draws over the glyph string at `head'.
18247 The background must not be drawn because this would overwrite
18248 right overhangs of preceding glyphs for which no glyph
18249 strings exist. */
18250 i = left_overwriting (head);
18251 if (i >= 0)
18252 {
18253 BUILD_GLYPH_STRINGS (i, start, h, t,
18254 DRAW_NORMAL_TEXT, dummy_x, last_x);
18255 for (s = h; s; s = s->next)
18256 s->background_filled_p = 1;
18257 compute_overhangs_and_x (t, head->x, 1);
18258 prepend_glyph_string_lists (&head, &tail, h, t);
18259 }
18260
18261 /* Append glyphs strings for glyphs following the last glyph
18262 string tail that are overwritten by tail. The background of
18263 these strings has to be drawn because tail's foreground draws
18264 over it. */
18265 i = right_overwritten (tail);
18266 if (i >= 0)
18267 {
18268 BUILD_GLYPH_STRINGS (end, i, h, t,
18269 DRAW_NORMAL_TEXT, x, last_x);
18270 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18271 append_glyph_string_lists (&head, &tail, h, t);
18272 }
18273
18274 /* Append glyph strings for glyphs following the last glyph
18275 string tail that overwrite tail. The foreground of such
18276 glyphs has to be drawn because it writes into the background
18277 of tail. The background must not be drawn because it could
18278 paint over the foreground of following glyphs. */
18279 i = right_overwriting (tail);
18280 if (i >= 0)
18281 {
18282 BUILD_GLYPH_STRINGS (end, i, h, t,
18283 DRAW_NORMAL_TEXT, x, last_x);
18284 for (s = h; s; s = s->next)
18285 s->background_filled_p = 1;
18286 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18287 append_glyph_string_lists (&head, &tail, h, t);
18288 }
18289 }
18290
18291 /* Draw all strings. */
18292 for (s = head; s; s = s->next)
18293 rif->draw_glyph_string (s);
18294
18295 if (area == TEXT_AREA
18296 && !row->full_width_p
18297 /* When drawing overlapping rows, only the glyph strings'
18298 foreground is drawn, which doesn't erase a cursor
18299 completely. */
18300 && !overlaps_p)
18301 {
18302 int x0 = head ? head->x : x;
18303 int x1 = tail ? tail->x + tail->background_width : x;
18304
18305 int text_left = window_box_left (w, TEXT_AREA);
18306 x0 -= text_left;
18307 x1 -= text_left;
18308
18309 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18310 row->y, MATRIX_ROW_BOTTOM_Y (row));
18311 }
18312
18313 /* Value is the x-position up to which drawn, relative to AREA of W.
18314 This doesn't include parts drawn because of overhangs. */
18315 if (row->full_width_p)
18316 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18317 else
18318 x_reached -= window_box_left (w, area);
18319
18320 RELEASE_HDC (hdc, f);
18321
18322 return x_reached;
18323 }
18324
18325 /* Expand row matrix if too narrow. Don't expand if area
18326 is not present. */
18327
18328 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
18329 { \
18330 if (!fonts_changed_p \
18331 && (it->glyph_row->glyphs[area] \
18332 < it->glyph_row->glyphs[area + 1])) \
18333 { \
18334 it->w->ncols_scale_factor++; \
18335 fonts_changed_p = 1; \
18336 } \
18337 }
18338
18339 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18340 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18341
18342 static INLINE void
18343 append_glyph (it)
18344 struct it *it;
18345 {
18346 struct glyph *glyph;
18347 enum glyph_row_area area = it->area;
18348
18349 xassert (it->glyph_row);
18350 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18351
18352 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18353 if (glyph < it->glyph_row->glyphs[area + 1])
18354 {
18355 glyph->charpos = CHARPOS (it->position);
18356 glyph->object = it->object;
18357 glyph->pixel_width = it->pixel_width;
18358 glyph->ascent = it->ascent;
18359 glyph->descent = it->descent;
18360 glyph->voffset = it->voffset;
18361 glyph->type = CHAR_GLYPH;
18362 glyph->multibyte_p = it->multibyte_p;
18363 glyph->left_box_line_p = it->start_of_box_run_p;
18364 glyph->right_box_line_p = it->end_of_box_run_p;
18365 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18366 || it->phys_descent > it->descent);
18367 glyph->padding_p = 0;
18368 glyph->glyph_not_available_p = it->glyph_not_available_p;
18369 glyph->face_id = it->face_id;
18370 glyph->u.ch = it->char_to_display;
18371 glyph->slice = null_glyph_slice;
18372 glyph->font_type = FONT_TYPE_UNKNOWN;
18373 ++it->glyph_row->used[area];
18374 }
18375 else
18376 IT_EXPAND_MATRIX_WIDTH (it, area);
18377 }
18378
18379 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18380 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18381
18382 static INLINE void
18383 append_composite_glyph (it)
18384 struct it *it;
18385 {
18386 struct glyph *glyph;
18387 enum glyph_row_area area = it->area;
18388
18389 xassert (it->glyph_row);
18390
18391 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18392 if (glyph < it->glyph_row->glyphs[area + 1])
18393 {
18394 glyph->charpos = CHARPOS (it->position);
18395 glyph->object = it->object;
18396 glyph->pixel_width = it->pixel_width;
18397 glyph->ascent = it->ascent;
18398 glyph->descent = it->descent;
18399 glyph->voffset = it->voffset;
18400 glyph->type = COMPOSITE_GLYPH;
18401 glyph->multibyte_p = it->multibyte_p;
18402 glyph->left_box_line_p = it->start_of_box_run_p;
18403 glyph->right_box_line_p = it->end_of_box_run_p;
18404 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18405 || it->phys_descent > it->descent);
18406 glyph->padding_p = 0;
18407 glyph->glyph_not_available_p = 0;
18408 glyph->face_id = it->face_id;
18409 glyph->u.cmp_id = it->cmp_id;
18410 glyph->slice = null_glyph_slice;
18411 glyph->font_type = FONT_TYPE_UNKNOWN;
18412 ++it->glyph_row->used[area];
18413 }
18414 else
18415 IT_EXPAND_MATRIX_WIDTH (it, area);
18416 }
18417
18418
18419 /* Change IT->ascent and IT->height according to the setting of
18420 IT->voffset. */
18421
18422 static INLINE void
18423 take_vertical_position_into_account (it)
18424 struct it *it;
18425 {
18426 if (it->voffset)
18427 {
18428 if (it->voffset < 0)
18429 /* Increase the ascent so that we can display the text higher
18430 in the line. */
18431 it->ascent -= it->voffset;
18432 else
18433 /* Increase the descent so that we can display the text lower
18434 in the line. */
18435 it->descent += it->voffset;
18436 }
18437 }
18438
18439
18440 /* Produce glyphs/get display metrics for the image IT is loaded with.
18441 See the description of struct display_iterator in dispextern.h for
18442 an overview of struct display_iterator. */
18443
18444 static void
18445 produce_image_glyph (it)
18446 struct it *it;
18447 {
18448 struct image *img;
18449 struct face *face;
18450 int glyph_ascent;
18451 struct glyph_slice slice;
18452
18453 xassert (it->what == IT_IMAGE);
18454
18455 face = FACE_FROM_ID (it->f, it->face_id);
18456 xassert (face);
18457 /* Make sure X resources of the face is loaded. */
18458 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18459
18460 if (it->image_id < 0)
18461 {
18462 /* Fringe bitmap. */
18463 it->ascent = it->phys_ascent = 0;
18464 it->descent = it->phys_descent = 0;
18465 it->pixel_width = 0;
18466 it->nglyphs = 0;
18467 return;
18468 }
18469
18470 img = IMAGE_FROM_ID (it->f, it->image_id);
18471 xassert (img);
18472 /* Make sure X resources of the image is loaded. */
18473 prepare_image_for_display (it->f, img);
18474
18475 slice.x = slice.y = 0;
18476 slice.width = img->width;
18477 slice.height = img->height;
18478
18479 if (INTEGERP (it->slice.x))
18480 slice.x = XINT (it->slice.x);
18481 else if (FLOATP (it->slice.x))
18482 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
18483
18484 if (INTEGERP (it->slice.y))
18485 slice.y = XINT (it->slice.y);
18486 else if (FLOATP (it->slice.y))
18487 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
18488
18489 if (INTEGERP (it->slice.width))
18490 slice.width = XINT (it->slice.width);
18491 else if (FLOATP (it->slice.width))
18492 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
18493
18494 if (INTEGERP (it->slice.height))
18495 slice.height = XINT (it->slice.height);
18496 else if (FLOATP (it->slice.height))
18497 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
18498
18499 if (slice.x >= img->width)
18500 slice.x = img->width;
18501 if (slice.y >= img->height)
18502 slice.y = img->height;
18503 if (slice.x + slice.width >= img->width)
18504 slice.width = img->width - slice.x;
18505 if (slice.y + slice.height > img->height)
18506 slice.height = img->height - slice.y;
18507
18508 if (slice.width == 0 || slice.height == 0)
18509 return;
18510
18511 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
18512
18513 it->descent = slice.height - glyph_ascent;
18514 if (slice.y == 0)
18515 it->descent += img->vmargin;
18516 if (slice.y + slice.height == img->height)
18517 it->descent += img->vmargin;
18518 it->phys_descent = it->descent;
18519
18520 it->pixel_width = slice.width;
18521 if (slice.x == 0)
18522 it->pixel_width += img->hmargin;
18523 if (slice.x + slice.width == img->width)
18524 it->pixel_width += img->hmargin;
18525
18526 /* It's quite possible for images to have an ascent greater than
18527 their height, so don't get confused in that case. */
18528 if (it->descent < 0)
18529 it->descent = 0;
18530
18531 #if 0 /* this breaks image tiling */
18532 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
18533 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
18534 if (face_ascent > it->ascent)
18535 it->ascent = it->phys_ascent = face_ascent;
18536 #endif
18537
18538 it->nglyphs = 1;
18539
18540 if (face->box != FACE_NO_BOX)
18541 {
18542 if (face->box_line_width > 0)
18543 {
18544 if (slice.y == 0)
18545 it->ascent += face->box_line_width;
18546 if (slice.y + slice.height == img->height)
18547 it->descent += face->box_line_width;
18548 }
18549
18550 if (it->start_of_box_run_p && slice.x == 0)
18551 it->pixel_width += abs (face->box_line_width);
18552 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
18553 it->pixel_width += abs (face->box_line_width);
18554 }
18555
18556 take_vertical_position_into_account (it);
18557
18558 if (it->glyph_row)
18559 {
18560 struct glyph *glyph;
18561 enum glyph_row_area area = it->area;
18562
18563 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18564 if (glyph < it->glyph_row->glyphs[area + 1])
18565 {
18566 glyph->charpos = CHARPOS (it->position);
18567 glyph->object = it->object;
18568 glyph->pixel_width = it->pixel_width;
18569 glyph->ascent = glyph_ascent;
18570 glyph->descent = it->descent;
18571 glyph->voffset = it->voffset;
18572 glyph->type = IMAGE_GLYPH;
18573 glyph->multibyte_p = it->multibyte_p;
18574 glyph->left_box_line_p = it->start_of_box_run_p;
18575 glyph->right_box_line_p = it->end_of_box_run_p;
18576 glyph->overlaps_vertically_p = 0;
18577 glyph->padding_p = 0;
18578 glyph->glyph_not_available_p = 0;
18579 glyph->face_id = it->face_id;
18580 glyph->u.img_id = img->id;
18581 glyph->slice = slice;
18582 glyph->font_type = FONT_TYPE_UNKNOWN;
18583 ++it->glyph_row->used[area];
18584 }
18585 else
18586 IT_EXPAND_MATRIX_WIDTH (it, area);
18587 }
18588 }
18589
18590
18591 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
18592 of the glyph, WIDTH and HEIGHT are the width and height of the
18593 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
18594
18595 static void
18596 append_stretch_glyph (it, object, width, height, ascent)
18597 struct it *it;
18598 Lisp_Object object;
18599 int width, height;
18600 int ascent;
18601 {
18602 struct glyph *glyph;
18603 enum glyph_row_area area = it->area;
18604
18605 xassert (ascent >= 0 && ascent <= height);
18606
18607 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18608 if (glyph < it->glyph_row->glyphs[area + 1])
18609 {
18610 glyph->charpos = CHARPOS (it->position);
18611 glyph->object = object;
18612 glyph->pixel_width = width;
18613 glyph->ascent = ascent;
18614 glyph->descent = height - ascent;
18615 glyph->voffset = it->voffset;
18616 glyph->type = STRETCH_GLYPH;
18617 glyph->multibyte_p = it->multibyte_p;
18618 glyph->left_box_line_p = it->start_of_box_run_p;
18619 glyph->right_box_line_p = it->end_of_box_run_p;
18620 glyph->overlaps_vertically_p = 0;
18621 glyph->padding_p = 0;
18622 glyph->glyph_not_available_p = 0;
18623 glyph->face_id = it->face_id;
18624 glyph->u.stretch.ascent = ascent;
18625 glyph->u.stretch.height = height;
18626 glyph->slice = null_glyph_slice;
18627 glyph->font_type = FONT_TYPE_UNKNOWN;
18628 ++it->glyph_row->used[area];
18629 }
18630 else
18631 IT_EXPAND_MATRIX_WIDTH (it, area);
18632 }
18633
18634
18635 /* Produce a stretch glyph for iterator IT. IT->object is the value
18636 of the glyph property displayed. The value must be a list
18637 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
18638 being recognized:
18639
18640 1. `:width WIDTH' specifies that the space should be WIDTH *
18641 canonical char width wide. WIDTH may be an integer or floating
18642 point number.
18643
18644 2. `:relative-width FACTOR' specifies that the width of the stretch
18645 should be computed from the width of the first character having the
18646 `glyph' property, and should be FACTOR times that width.
18647
18648 3. `:align-to HPOS' specifies that the space should be wide enough
18649 to reach HPOS, a value in canonical character units.
18650
18651 Exactly one of the above pairs must be present.
18652
18653 4. `:height HEIGHT' specifies that the height of the stretch produced
18654 should be HEIGHT, measured in canonical character units.
18655
18656 5. `:relative-height FACTOR' specifies that the height of the
18657 stretch should be FACTOR times the height of the characters having
18658 the glyph property.
18659
18660 Either none or exactly one of 4 or 5 must be present.
18661
18662 6. `:ascent ASCENT' specifies that ASCENT percent of the height
18663 of the stretch should be used for the ascent of the stretch.
18664 ASCENT must be in the range 0 <= ASCENT <= 100. */
18665
18666 static void
18667 produce_stretch_glyph (it)
18668 struct it *it;
18669 {
18670 /* (space :width WIDTH :height HEIGHT ...) */
18671 Lisp_Object prop, plist;
18672 int width = 0, height = 0, align_to = -1;
18673 int zero_width_ok_p = 0, zero_height_ok_p = 0;
18674 int ascent = 0;
18675 double tem;
18676 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18677 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
18678
18679 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18680
18681 /* List should start with `space'. */
18682 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
18683 plist = XCDR (it->object);
18684
18685 /* Compute the width of the stretch. */
18686 if ((prop = Fsafe_plist_get (plist, QCwidth), !NILP (prop))
18687 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
18688 {
18689 /* Absolute width `:width WIDTH' specified and valid. */
18690 zero_width_ok_p = 1;
18691 width = (int)tem;
18692 }
18693 else if (prop = Fsafe_plist_get (plist, QCrelative_width),
18694 NUMVAL (prop) > 0)
18695 {
18696 /* Relative width `:relative-width FACTOR' specified and valid.
18697 Compute the width of the characters having the `glyph'
18698 property. */
18699 struct it it2;
18700 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
18701
18702 it2 = *it;
18703 if (it->multibyte_p)
18704 {
18705 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
18706 - IT_BYTEPOS (*it));
18707 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
18708 }
18709 else
18710 it2.c = *p, it2.len = 1;
18711
18712 it2.glyph_row = NULL;
18713 it2.what = IT_CHARACTER;
18714 x_produce_glyphs (&it2);
18715 width = NUMVAL (prop) * it2.pixel_width;
18716 }
18717 else if ((prop = Fsafe_plist_get (plist, QCalign_to), !NILP (prop))
18718 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
18719 {
18720 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
18721 align_to = (align_to < 0
18722 ? 0
18723 : align_to - window_box_left_offset (it->w, TEXT_AREA));
18724 else if (align_to < 0)
18725 align_to = window_box_left_offset (it->w, TEXT_AREA);
18726 width = max (0, (int)tem + align_to - it->current_x);
18727 zero_width_ok_p = 1;
18728 }
18729 else
18730 /* Nothing specified -> width defaults to canonical char width. */
18731 width = FRAME_COLUMN_WIDTH (it->f);
18732
18733 if (width <= 0 && (width < 0 || !zero_width_ok_p))
18734 width = 1;
18735
18736 /* Compute height. */
18737 if ((prop = Fsafe_plist_get (plist, QCheight), !NILP (prop))
18738 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18739 {
18740 height = (int)tem;
18741 zero_height_ok_p = 1;
18742 }
18743 else if (prop = Fsafe_plist_get (plist, QCrelative_height),
18744 NUMVAL (prop) > 0)
18745 height = FONT_HEIGHT (font) * NUMVAL (prop);
18746 else
18747 height = FONT_HEIGHT (font);
18748
18749 if (height <= 0 && (height < 0 || !zero_height_ok_p))
18750 height = 1;
18751
18752 /* Compute percentage of height used for ascent. If
18753 `:ascent ASCENT' is present and valid, use that. Otherwise,
18754 derive the ascent from the font in use. */
18755 if (prop = Fsafe_plist_get (plist, QCascent),
18756 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
18757 ascent = height * NUMVAL (prop) / 100.0;
18758 else if (!NILP (prop)
18759 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18760 ascent = min (max (0, (int)tem), height);
18761 else
18762 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
18763
18764 if (width > 0 && height > 0 && it->glyph_row)
18765 {
18766 Lisp_Object object = it->stack[it->sp - 1].string;
18767 if (!STRINGP (object))
18768 object = it->w->buffer;
18769 append_stretch_glyph (it, object, width, height, ascent);
18770 }
18771
18772 it->pixel_width = width;
18773 it->ascent = it->phys_ascent = ascent;
18774 it->descent = it->phys_descent = height - it->ascent;
18775 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
18776
18777 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
18778 {
18779 if (face->box_line_width > 0)
18780 {
18781 it->ascent += face->box_line_width;
18782 it->descent += face->box_line_width;
18783 }
18784
18785 if (it->start_of_box_run_p)
18786 it->pixel_width += abs (face->box_line_width);
18787 if (it->end_of_box_run_p)
18788 it->pixel_width += abs (face->box_line_width);
18789 }
18790
18791 take_vertical_position_into_account (it);
18792 }
18793
18794 /* Get line-height and line-spacing property at point.
18795 If line-height has format (HEIGHT TOTAL), return TOTAL
18796 in TOTAL_HEIGHT. */
18797
18798 static Lisp_Object
18799 get_line_height_property (it, prop)
18800 struct it *it;
18801 Lisp_Object prop;
18802 {
18803 Lisp_Object position, val;
18804
18805 if (STRINGP (it->object))
18806 position = make_number (IT_STRING_CHARPOS (*it));
18807 else if (BUFFERP (it->object))
18808 position = make_number (IT_CHARPOS (*it));
18809 else
18810 return Qnil;
18811
18812 return Fget_char_property (position, prop, it->object);
18813 }
18814
18815 /* Calculate line-height and line-spacing properties.
18816 An integer value specifies explicit pixel value.
18817 A float value specifies relative value to current face height.
18818 A cons (float . face-name) specifies relative value to
18819 height of specified face font.
18820
18821 Returns height in pixels, or nil. */
18822
18823
18824 static Lisp_Object
18825 calc_line_height_property (it, val, font, boff, override)
18826 struct it *it;
18827 Lisp_Object val;
18828 XFontStruct *font;
18829 int boff, override;
18830 {
18831 Lisp_Object face_name = Qnil;
18832 int ascent, descent, height;
18833
18834 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
18835 return val;
18836
18837 if (CONSP (val))
18838 {
18839 face_name = XCAR (val);
18840 val = XCDR (val);
18841 if (!NUMBERP (val))
18842 val = make_number (1);
18843 if (NILP (face_name))
18844 {
18845 height = it->ascent + it->descent;
18846 goto scale;
18847 }
18848 }
18849
18850 if (NILP (face_name))
18851 {
18852 font = FRAME_FONT (it->f);
18853 boff = FRAME_BASELINE_OFFSET (it->f);
18854 }
18855 else if (EQ (face_name, Qt))
18856 {
18857 override = 0;
18858 }
18859 else
18860 {
18861 int face_id;
18862 struct face *face;
18863 struct font_info *font_info;
18864
18865 face_id = lookup_named_face (it->f, face_name, ' ', 0);
18866 if (face_id < 0)
18867 return make_number (-1);
18868
18869 face = FACE_FROM_ID (it->f, face_id);
18870 font = face->font;
18871 if (font == NULL)
18872 return make_number (-1);
18873
18874 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18875 boff = font_info->baseline_offset;
18876 if (font_info->vertical_centering)
18877 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18878 }
18879
18880 ascent = FONT_BASE (font) + boff;
18881 descent = FONT_DESCENT (font) - boff;
18882
18883 if (override)
18884 {
18885 it->override_ascent = ascent;
18886 it->override_descent = descent;
18887 it->override_boff = boff;
18888 }
18889
18890 height = ascent + descent;
18891
18892 scale:
18893 if (FLOATP (val))
18894 height = (int)(XFLOAT_DATA (val) * height);
18895 else if (INTEGERP (val))
18896 height *= XINT (val);
18897
18898 return make_number (height);
18899 }
18900
18901
18902 /* RIF:
18903 Produce glyphs/get display metrics for the display element IT is
18904 loaded with. See the description of struct display_iterator in
18905 dispextern.h for an overview of struct display_iterator. */
18906
18907 void
18908 x_produce_glyphs (it)
18909 struct it *it;
18910 {
18911 int extra_line_spacing = it->extra_line_spacing;
18912
18913 it->glyph_not_available_p = 0;
18914
18915 if (it->what == IT_CHARACTER)
18916 {
18917 XChar2b char2b;
18918 XFontStruct *font;
18919 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18920 XCharStruct *pcm;
18921 int font_not_found_p;
18922 struct font_info *font_info;
18923 int boff; /* baseline offset */
18924 /* We may change it->multibyte_p upon unibyte<->multibyte
18925 conversion. So, save the current value now and restore it
18926 later.
18927
18928 Note: It seems that we don't have to record multibyte_p in
18929 struct glyph because the character code itself tells if or
18930 not the character is multibyte. Thus, in the future, we must
18931 consider eliminating the field `multibyte_p' in the struct
18932 glyph. */
18933 int saved_multibyte_p = it->multibyte_p;
18934
18935 /* Maybe translate single-byte characters to multibyte, or the
18936 other way. */
18937 it->char_to_display = it->c;
18938 if (!ASCII_BYTE_P (it->c))
18939 {
18940 if (unibyte_display_via_language_environment
18941 && SINGLE_BYTE_CHAR_P (it->c)
18942 && (it->c >= 0240
18943 || !NILP (Vnonascii_translation_table)))
18944 {
18945 it->char_to_display = unibyte_char_to_multibyte (it->c);
18946 it->multibyte_p = 1;
18947 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18948 face = FACE_FROM_ID (it->f, it->face_id);
18949 }
18950 else if (!SINGLE_BYTE_CHAR_P (it->c)
18951 && !it->multibyte_p)
18952 {
18953 it->multibyte_p = 1;
18954 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18955 face = FACE_FROM_ID (it->f, it->face_id);
18956 }
18957 }
18958
18959 /* Get font to use. Encode IT->char_to_display. */
18960 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18961 &char2b, it->multibyte_p, 0);
18962 font = face->font;
18963
18964 /* When no suitable font found, use the default font. */
18965 font_not_found_p = font == NULL;
18966 if (font_not_found_p)
18967 {
18968 font = FRAME_FONT (it->f);
18969 boff = FRAME_BASELINE_OFFSET (it->f);
18970 font_info = NULL;
18971 }
18972 else
18973 {
18974 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18975 boff = font_info->baseline_offset;
18976 if (font_info->vertical_centering)
18977 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18978 }
18979
18980 if (it->char_to_display >= ' '
18981 && (!it->multibyte_p || it->char_to_display < 128))
18982 {
18983 /* Either unibyte or ASCII. */
18984 int stretched_p;
18985
18986 it->nglyphs = 1;
18987
18988 pcm = rif->per_char_metric (font, &char2b,
18989 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
18990
18991 if (it->override_ascent >= 0)
18992 {
18993 it->ascent = it->override_ascent;
18994 it->descent = it->override_descent;
18995 boff = it->override_boff;
18996 }
18997 else
18998 {
18999 it->ascent = FONT_BASE (font) + boff;
19000 it->descent = FONT_DESCENT (font) - boff;
19001 }
19002
19003 if (pcm)
19004 {
19005 it->phys_ascent = pcm->ascent + boff;
19006 it->phys_descent = pcm->descent - boff;
19007 it->pixel_width = pcm->width;
19008 }
19009 else
19010 {
19011 it->glyph_not_available_p = 1;
19012 it->phys_ascent = it->ascent;
19013 it->phys_descent = it->descent;
19014 it->pixel_width = FONT_WIDTH (font);
19015 }
19016
19017 if (it->constrain_row_ascent_descent_p)
19018 {
19019 if (it->descent > it->max_descent)
19020 {
19021 it->ascent += it->descent - it->max_descent;
19022 it->descent = it->max_descent;
19023 }
19024 if (it->ascent > it->max_ascent)
19025 {
19026 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19027 it->ascent = it->max_ascent;
19028 }
19029 it->phys_ascent = min (it->phys_ascent, it->ascent);
19030 it->phys_descent = min (it->phys_descent, it->descent);
19031 extra_line_spacing = 0;
19032 }
19033
19034 /* If this is a space inside a region of text with
19035 `space-width' property, change its width. */
19036 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
19037 if (stretched_p)
19038 it->pixel_width *= XFLOATINT (it->space_width);
19039
19040 /* If face has a box, add the box thickness to the character
19041 height. If character has a box line to the left and/or
19042 right, add the box line width to the character's width. */
19043 if (face->box != FACE_NO_BOX)
19044 {
19045 int thick = face->box_line_width;
19046
19047 if (thick > 0)
19048 {
19049 it->ascent += thick;
19050 it->descent += thick;
19051 }
19052 else
19053 thick = -thick;
19054
19055 if (it->start_of_box_run_p)
19056 it->pixel_width += thick;
19057 if (it->end_of_box_run_p)
19058 it->pixel_width += thick;
19059 }
19060
19061 /* If face has an overline, add the height of the overline
19062 (1 pixel) and a 1 pixel margin to the character height. */
19063 if (face->overline_p)
19064 it->ascent += 2;
19065
19066 if (it->constrain_row_ascent_descent_p)
19067 {
19068 if (it->ascent > it->max_ascent)
19069 it->ascent = it->max_ascent;
19070 if (it->descent > it->max_descent)
19071 it->descent = it->max_descent;
19072 }
19073
19074 take_vertical_position_into_account (it);
19075
19076 /* If we have to actually produce glyphs, do it. */
19077 if (it->glyph_row)
19078 {
19079 if (stretched_p)
19080 {
19081 /* Translate a space with a `space-width' property
19082 into a stretch glyph. */
19083 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
19084 / FONT_HEIGHT (font));
19085 append_stretch_glyph (it, it->object, it->pixel_width,
19086 it->ascent + it->descent, ascent);
19087 }
19088 else
19089 append_glyph (it);
19090
19091 /* If characters with lbearing or rbearing are displayed
19092 in this line, record that fact in a flag of the
19093 glyph row. This is used to optimize X output code. */
19094 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
19095 it->glyph_row->contains_overlapping_glyphs_p = 1;
19096 }
19097 }
19098 else if (it->char_to_display == '\n')
19099 {
19100 /* A newline has no width but we need the height of the line.
19101 But if previous part of the line set a height, don't
19102 increase that height */
19103
19104 Lisp_Object height;
19105 Lisp_Object total_height = Qnil;
19106
19107 it->override_ascent = -1;
19108 it->pixel_width = 0;
19109 it->nglyphs = 0;
19110
19111 height = get_line_height_property(it, Qline_height);
19112 /* Split (line-height total-height) list */
19113 if (CONSP (height)
19114 && CONSP (XCDR (height))
19115 && NILP (XCDR (XCDR (height))))
19116 {
19117 total_height = XCAR (XCDR (height));
19118 height = XCAR (height);
19119 }
19120 height = calc_line_height_property(it, height, font, boff, 1);
19121
19122 if (it->override_ascent >= 0)
19123 {
19124 it->ascent = it->override_ascent;
19125 it->descent = it->override_descent;
19126 boff = it->override_boff;
19127 }
19128 else
19129 {
19130 it->ascent = FONT_BASE (font) + boff;
19131 it->descent = FONT_DESCENT (font) - boff;
19132 }
19133
19134 if (EQ (height, Qt))
19135 {
19136 if (it->descent > it->max_descent)
19137 {
19138 it->ascent += it->descent - it->max_descent;
19139 it->descent = it->max_descent;
19140 }
19141 if (it->ascent > it->max_ascent)
19142 {
19143 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19144 it->ascent = it->max_ascent;
19145 }
19146 it->phys_ascent = min (it->phys_ascent, it->ascent);
19147 it->phys_descent = min (it->phys_descent, it->descent);
19148 it->constrain_row_ascent_descent_p = 1;
19149 extra_line_spacing = 0;
19150 }
19151 else
19152 {
19153 Lisp_Object spacing;
19154 int total = 0;
19155
19156 it->phys_ascent = it->ascent;
19157 it->phys_descent = it->descent;
19158
19159 if ((it->max_ascent > 0 || it->max_descent > 0)
19160 && face->box != FACE_NO_BOX
19161 && face->box_line_width > 0)
19162 {
19163 it->ascent += face->box_line_width;
19164 it->descent += face->box_line_width;
19165 }
19166 if (!NILP (height)
19167 && XINT (height) > it->ascent + it->descent)
19168 it->ascent = XINT (height) - it->descent;
19169
19170 if (!NILP (total_height))
19171 spacing = calc_line_height_property(it, total_height, font, boff, 0);
19172 else
19173 {
19174 spacing = get_line_height_property(it, Qline_spacing);
19175 spacing = calc_line_height_property(it, spacing, font, boff, 0);
19176 }
19177 if (INTEGERP (spacing))
19178 {
19179 extra_line_spacing = XINT (spacing);
19180 if (!NILP (total_height))
19181 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19182 }
19183 }
19184 }
19185 else if (it->char_to_display == '\t')
19186 {
19187 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
19188 int x = it->current_x + it->continuation_lines_width;
19189 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
19190
19191 /* If the distance from the current position to the next tab
19192 stop is less than a space character width, use the
19193 tab stop after that. */
19194 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
19195 next_tab_x += tab_width;
19196
19197 it->pixel_width = next_tab_x - x;
19198 it->nglyphs = 1;
19199 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
19200 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
19201
19202 if (it->glyph_row)
19203 {
19204 append_stretch_glyph (it, it->object, it->pixel_width,
19205 it->ascent + it->descent, it->ascent);
19206 }
19207 }
19208 else
19209 {
19210 /* A multi-byte character. Assume that the display width of the
19211 character is the width of the character multiplied by the
19212 width of the font. */
19213
19214 /* If we found a font, this font should give us the right
19215 metrics. If we didn't find a font, use the frame's
19216 default font and calculate the width of the character
19217 from the charset width; this is what old redisplay code
19218 did. */
19219
19220 pcm = rif->per_char_metric (font, &char2b,
19221 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
19222
19223 if (font_not_found_p || !pcm)
19224 {
19225 int charset = CHAR_CHARSET (it->char_to_display);
19226
19227 it->glyph_not_available_p = 1;
19228 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
19229 * CHARSET_WIDTH (charset));
19230 it->phys_ascent = FONT_BASE (font) + boff;
19231 it->phys_descent = FONT_DESCENT (font) - boff;
19232 }
19233 else
19234 {
19235 it->pixel_width = pcm->width;
19236 it->phys_ascent = pcm->ascent + boff;
19237 it->phys_descent = pcm->descent - boff;
19238 if (it->glyph_row
19239 && (pcm->lbearing < 0
19240 || pcm->rbearing > pcm->width))
19241 it->glyph_row->contains_overlapping_glyphs_p = 1;
19242 }
19243 it->nglyphs = 1;
19244 it->ascent = FONT_BASE (font) + boff;
19245 it->descent = FONT_DESCENT (font) - boff;
19246 if (face->box != FACE_NO_BOX)
19247 {
19248 int thick = face->box_line_width;
19249
19250 if (thick > 0)
19251 {
19252 it->ascent += thick;
19253 it->descent += thick;
19254 }
19255 else
19256 thick = - thick;
19257
19258 if (it->start_of_box_run_p)
19259 it->pixel_width += thick;
19260 if (it->end_of_box_run_p)
19261 it->pixel_width += thick;
19262 }
19263
19264 /* If face has an overline, add the height of the overline
19265 (1 pixel) and a 1 pixel margin to the character height. */
19266 if (face->overline_p)
19267 it->ascent += 2;
19268
19269 take_vertical_position_into_account (it);
19270
19271 if (it->glyph_row)
19272 append_glyph (it);
19273 }
19274 it->multibyte_p = saved_multibyte_p;
19275 }
19276 else if (it->what == IT_COMPOSITION)
19277 {
19278 /* Note: A composition is represented as one glyph in the
19279 glyph matrix. There are no padding glyphs. */
19280 XChar2b char2b;
19281 XFontStruct *font;
19282 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19283 XCharStruct *pcm;
19284 int font_not_found_p;
19285 struct font_info *font_info;
19286 int boff; /* baseline offset */
19287 struct composition *cmp = composition_table[it->cmp_id];
19288
19289 /* Maybe translate single-byte characters to multibyte. */
19290 it->char_to_display = it->c;
19291 if (unibyte_display_via_language_environment
19292 && SINGLE_BYTE_CHAR_P (it->c)
19293 && (it->c >= 0240
19294 || (it->c >= 0200
19295 && !NILP (Vnonascii_translation_table))))
19296 {
19297 it->char_to_display = unibyte_char_to_multibyte (it->c);
19298 }
19299
19300 /* Get face and font to use. Encode IT->char_to_display. */
19301 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19302 face = FACE_FROM_ID (it->f, it->face_id);
19303 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19304 &char2b, it->multibyte_p, 0);
19305 font = face->font;
19306
19307 /* When no suitable font found, use the default font. */
19308 font_not_found_p = font == NULL;
19309 if (font_not_found_p)
19310 {
19311 font = FRAME_FONT (it->f);
19312 boff = FRAME_BASELINE_OFFSET (it->f);
19313 font_info = NULL;
19314 }
19315 else
19316 {
19317 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19318 boff = font_info->baseline_offset;
19319 if (font_info->vertical_centering)
19320 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19321 }
19322
19323 /* There are no padding glyphs, so there is only one glyph to
19324 produce for the composition. Important is that pixel_width,
19325 ascent and descent are the values of what is drawn by
19326 draw_glyphs (i.e. the values of the overall glyphs composed). */
19327 it->nglyphs = 1;
19328
19329 /* If we have not yet calculated pixel size data of glyphs of
19330 the composition for the current face font, calculate them
19331 now. Theoretically, we have to check all fonts for the
19332 glyphs, but that requires much time and memory space. So,
19333 here we check only the font of the first glyph. This leads
19334 to incorrect display very rarely, and C-l (recenter) can
19335 correct the display anyway. */
19336 if (cmp->font != (void *) font)
19337 {
19338 /* Ascent and descent of the font of the first character of
19339 this composition (adjusted by baseline offset). Ascent
19340 and descent of overall glyphs should not be less than
19341 them respectively. */
19342 int font_ascent = FONT_BASE (font) + boff;
19343 int font_descent = FONT_DESCENT (font) - boff;
19344 /* Bounding box of the overall glyphs. */
19345 int leftmost, rightmost, lowest, highest;
19346 int i, width, ascent, descent;
19347
19348 cmp->font = (void *) font;
19349
19350 /* Initialize the bounding box. */
19351 if (font_info
19352 && (pcm = rif->per_char_metric (font, &char2b,
19353 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19354 {
19355 width = pcm->width;
19356 ascent = pcm->ascent;
19357 descent = pcm->descent;
19358 }
19359 else
19360 {
19361 width = FONT_WIDTH (font);
19362 ascent = FONT_BASE (font);
19363 descent = FONT_DESCENT (font);
19364 }
19365
19366 rightmost = width;
19367 lowest = - descent + boff;
19368 highest = ascent + boff;
19369 leftmost = 0;
19370
19371 if (font_info
19372 && font_info->default_ascent
19373 && CHAR_TABLE_P (Vuse_default_ascent)
19374 && !NILP (Faref (Vuse_default_ascent,
19375 make_number (it->char_to_display))))
19376 highest = font_info->default_ascent + boff;
19377
19378 /* Draw the first glyph at the normal position. It may be
19379 shifted to right later if some other glyphs are drawn at
19380 the left. */
19381 cmp->offsets[0] = 0;
19382 cmp->offsets[1] = boff;
19383
19384 /* Set cmp->offsets for the remaining glyphs. */
19385 for (i = 1; i < cmp->glyph_len; i++)
19386 {
19387 int left, right, btm, top;
19388 int ch = COMPOSITION_GLYPH (cmp, i);
19389 int face_id = FACE_FOR_CHAR (it->f, face, ch);
19390
19391 face = FACE_FROM_ID (it->f, face_id);
19392 get_char_face_and_encoding (it->f, ch, face->id,
19393 &char2b, it->multibyte_p, 0);
19394 font = face->font;
19395 if (font == NULL)
19396 {
19397 font = FRAME_FONT (it->f);
19398 boff = FRAME_BASELINE_OFFSET (it->f);
19399 font_info = NULL;
19400 }
19401 else
19402 {
19403 font_info
19404 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19405 boff = font_info->baseline_offset;
19406 if (font_info->vertical_centering)
19407 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19408 }
19409
19410 if (font_info
19411 && (pcm = rif->per_char_metric (font, &char2b,
19412 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19413 {
19414 width = pcm->width;
19415 ascent = pcm->ascent;
19416 descent = pcm->descent;
19417 }
19418 else
19419 {
19420 width = FONT_WIDTH (font);
19421 ascent = 1;
19422 descent = 0;
19423 }
19424
19425 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19426 {
19427 /* Relative composition with or without
19428 alternate chars. */
19429 left = (leftmost + rightmost - width) / 2;
19430 btm = - descent + boff;
19431 if (font_info && font_info->relative_compose
19432 && (! CHAR_TABLE_P (Vignore_relative_composition)
19433 || NILP (Faref (Vignore_relative_composition,
19434 make_number (ch)))))
19435 {
19436
19437 if (- descent >= font_info->relative_compose)
19438 /* One extra pixel between two glyphs. */
19439 btm = highest + 1;
19440 else if (ascent <= 0)
19441 /* One extra pixel between two glyphs. */
19442 btm = lowest - 1 - ascent - descent;
19443 }
19444 }
19445 else
19446 {
19447 /* A composition rule is specified by an integer
19448 value that encodes global and new reference
19449 points (GREF and NREF). GREF and NREF are
19450 specified by numbers as below:
19451
19452 0---1---2 -- ascent
19453 | |
19454 | |
19455 | |
19456 9--10--11 -- center
19457 | |
19458 ---3---4---5--- baseline
19459 | |
19460 6---7---8 -- descent
19461 */
19462 int rule = COMPOSITION_RULE (cmp, i);
19463 int gref, nref, grefx, grefy, nrefx, nrefy;
19464
19465 COMPOSITION_DECODE_RULE (rule, gref, nref);
19466 grefx = gref % 3, nrefx = nref % 3;
19467 grefy = gref / 3, nrefy = nref / 3;
19468
19469 left = (leftmost
19470 + grefx * (rightmost - leftmost) / 2
19471 - nrefx * width / 2);
19472 btm = ((grefy == 0 ? highest
19473 : grefy == 1 ? 0
19474 : grefy == 2 ? lowest
19475 : (highest + lowest) / 2)
19476 - (nrefy == 0 ? ascent + descent
19477 : nrefy == 1 ? descent - boff
19478 : nrefy == 2 ? 0
19479 : (ascent + descent) / 2));
19480 }
19481
19482 cmp->offsets[i * 2] = left;
19483 cmp->offsets[i * 2 + 1] = btm + descent;
19484
19485 /* Update the bounding box of the overall glyphs. */
19486 right = left + width;
19487 top = btm + descent + ascent;
19488 if (left < leftmost)
19489 leftmost = left;
19490 if (right > rightmost)
19491 rightmost = right;
19492 if (top > highest)
19493 highest = top;
19494 if (btm < lowest)
19495 lowest = btm;
19496 }
19497
19498 /* If there are glyphs whose x-offsets are negative,
19499 shift all glyphs to the right and make all x-offsets
19500 non-negative. */
19501 if (leftmost < 0)
19502 {
19503 for (i = 0; i < cmp->glyph_len; i++)
19504 cmp->offsets[i * 2] -= leftmost;
19505 rightmost -= leftmost;
19506 }
19507
19508 cmp->pixel_width = rightmost;
19509 cmp->ascent = highest;
19510 cmp->descent = - lowest;
19511 if (cmp->ascent < font_ascent)
19512 cmp->ascent = font_ascent;
19513 if (cmp->descent < font_descent)
19514 cmp->descent = font_descent;
19515 }
19516
19517 it->pixel_width = cmp->pixel_width;
19518 it->ascent = it->phys_ascent = cmp->ascent;
19519 it->descent = it->phys_descent = cmp->descent;
19520
19521 if (face->box != FACE_NO_BOX)
19522 {
19523 int thick = face->box_line_width;
19524
19525 if (thick > 0)
19526 {
19527 it->ascent += thick;
19528 it->descent += thick;
19529 }
19530 else
19531 thick = - thick;
19532
19533 if (it->start_of_box_run_p)
19534 it->pixel_width += thick;
19535 if (it->end_of_box_run_p)
19536 it->pixel_width += thick;
19537 }
19538
19539 /* If face has an overline, add the height of the overline
19540 (1 pixel) and a 1 pixel margin to the character height. */
19541 if (face->overline_p)
19542 it->ascent += 2;
19543
19544 take_vertical_position_into_account (it);
19545
19546 if (it->glyph_row)
19547 append_composite_glyph (it);
19548 }
19549 else if (it->what == IT_IMAGE)
19550 produce_image_glyph (it);
19551 else if (it->what == IT_STRETCH)
19552 produce_stretch_glyph (it);
19553
19554 /* Accumulate dimensions. Note: can't assume that it->descent > 0
19555 because this isn't true for images with `:ascent 100'. */
19556 xassert (it->ascent >= 0 && it->descent >= 0);
19557 if (it->area == TEXT_AREA)
19558 it->current_x += it->pixel_width;
19559
19560 if (extra_line_spacing > 0)
19561 {
19562 it->descent += extra_line_spacing;
19563 if (extra_line_spacing > it->max_extra_line_spacing)
19564 it->max_extra_line_spacing = extra_line_spacing;
19565 }
19566
19567 it->max_ascent = max (it->max_ascent, it->ascent);
19568 it->max_descent = max (it->max_descent, it->descent);
19569 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
19570 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
19571 }
19572
19573 /* EXPORT for RIF:
19574 Output LEN glyphs starting at START at the nominal cursor position.
19575 Advance the nominal cursor over the text. The global variable
19576 updated_window contains the window being updated, updated_row is
19577 the glyph row being updated, and updated_area is the area of that
19578 row being updated. */
19579
19580 void
19581 x_write_glyphs (start, len)
19582 struct glyph *start;
19583 int len;
19584 {
19585 int x, hpos;
19586
19587 xassert (updated_window && updated_row);
19588 BLOCK_INPUT;
19589
19590 /* Write glyphs. */
19591
19592 hpos = start - updated_row->glyphs[updated_area];
19593 x = draw_glyphs (updated_window, output_cursor.x,
19594 updated_row, updated_area,
19595 hpos, hpos + len,
19596 DRAW_NORMAL_TEXT, 0);
19597
19598 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
19599 if (updated_area == TEXT_AREA
19600 && updated_window->phys_cursor_on_p
19601 && updated_window->phys_cursor.vpos == output_cursor.vpos
19602 && updated_window->phys_cursor.hpos >= hpos
19603 && updated_window->phys_cursor.hpos < hpos + len)
19604 updated_window->phys_cursor_on_p = 0;
19605
19606 UNBLOCK_INPUT;
19607
19608 /* Advance the output cursor. */
19609 output_cursor.hpos += len;
19610 output_cursor.x = x;
19611 }
19612
19613
19614 /* EXPORT for RIF:
19615 Insert LEN glyphs from START at the nominal cursor position. */
19616
19617 void
19618 x_insert_glyphs (start, len)
19619 struct glyph *start;
19620 int len;
19621 {
19622 struct frame *f;
19623 struct window *w;
19624 int line_height, shift_by_width, shifted_region_width;
19625 struct glyph_row *row;
19626 struct glyph *glyph;
19627 int frame_x, frame_y, hpos;
19628
19629 xassert (updated_window && updated_row);
19630 BLOCK_INPUT;
19631 w = updated_window;
19632 f = XFRAME (WINDOW_FRAME (w));
19633
19634 /* Get the height of the line we are in. */
19635 row = updated_row;
19636 line_height = row->height;
19637
19638 /* Get the width of the glyphs to insert. */
19639 shift_by_width = 0;
19640 for (glyph = start; glyph < start + len; ++glyph)
19641 shift_by_width += glyph->pixel_width;
19642
19643 /* Get the width of the region to shift right. */
19644 shifted_region_width = (window_box_width (w, updated_area)
19645 - output_cursor.x
19646 - shift_by_width);
19647
19648 /* Shift right. */
19649 frame_x = window_box_left (w, updated_area) + output_cursor.x;
19650 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
19651
19652 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
19653 line_height, shift_by_width);
19654
19655 /* Write the glyphs. */
19656 hpos = start - row->glyphs[updated_area];
19657 draw_glyphs (w, output_cursor.x, row, updated_area,
19658 hpos, hpos + len,
19659 DRAW_NORMAL_TEXT, 0);
19660
19661 /* Advance the output cursor. */
19662 output_cursor.hpos += len;
19663 output_cursor.x += shift_by_width;
19664 UNBLOCK_INPUT;
19665 }
19666
19667
19668 /* EXPORT for RIF:
19669 Erase the current text line from the nominal cursor position
19670 (inclusive) to pixel column TO_X (exclusive). The idea is that
19671 everything from TO_X onward is already erased.
19672
19673 TO_X is a pixel position relative to updated_area of
19674 updated_window. TO_X == -1 means clear to the end of this area. */
19675
19676 void
19677 x_clear_end_of_line (to_x)
19678 int to_x;
19679 {
19680 struct frame *f;
19681 struct window *w = updated_window;
19682 int max_x, min_y, max_y;
19683 int from_x, from_y, to_y;
19684
19685 xassert (updated_window && updated_row);
19686 f = XFRAME (w->frame);
19687
19688 if (updated_row->full_width_p)
19689 max_x = WINDOW_TOTAL_WIDTH (w);
19690 else
19691 max_x = window_box_width (w, updated_area);
19692 max_y = window_text_bottom_y (w);
19693
19694 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
19695 of window. For TO_X > 0, truncate to end of drawing area. */
19696 if (to_x == 0)
19697 return;
19698 else if (to_x < 0)
19699 to_x = max_x;
19700 else
19701 to_x = min (to_x, max_x);
19702
19703 to_y = min (max_y, output_cursor.y + updated_row->height);
19704
19705 /* Notice if the cursor will be cleared by this operation. */
19706 if (!updated_row->full_width_p)
19707 notice_overwritten_cursor (w, updated_area,
19708 output_cursor.x, -1,
19709 updated_row->y,
19710 MATRIX_ROW_BOTTOM_Y (updated_row));
19711
19712 from_x = output_cursor.x;
19713
19714 /* Translate to frame coordinates. */
19715 if (updated_row->full_width_p)
19716 {
19717 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
19718 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
19719 }
19720 else
19721 {
19722 int area_left = window_box_left (w, updated_area);
19723 from_x += area_left;
19724 to_x += area_left;
19725 }
19726
19727 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
19728 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
19729 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
19730
19731 /* Prevent inadvertently clearing to end of the X window. */
19732 if (to_x > from_x && to_y > from_y)
19733 {
19734 BLOCK_INPUT;
19735 rif->clear_frame_area (f, from_x, from_y,
19736 to_x - from_x, to_y - from_y);
19737 UNBLOCK_INPUT;
19738 }
19739 }
19740
19741 #endif /* HAVE_WINDOW_SYSTEM */
19742
19743
19744 \f
19745 /***********************************************************************
19746 Cursor types
19747 ***********************************************************************/
19748
19749 /* Value is the internal representation of the specified cursor type
19750 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
19751 of the bar cursor. */
19752
19753 static enum text_cursor_kinds
19754 get_specified_cursor_type (arg, width)
19755 Lisp_Object arg;
19756 int *width;
19757 {
19758 enum text_cursor_kinds type;
19759
19760 if (NILP (arg))
19761 return NO_CURSOR;
19762
19763 if (EQ (arg, Qbox))
19764 return FILLED_BOX_CURSOR;
19765
19766 if (EQ (arg, Qhollow))
19767 return HOLLOW_BOX_CURSOR;
19768
19769 if (EQ (arg, Qbar))
19770 {
19771 *width = 2;
19772 return BAR_CURSOR;
19773 }
19774
19775 if (CONSP (arg)
19776 && EQ (XCAR (arg), Qbar)
19777 && INTEGERP (XCDR (arg))
19778 && XINT (XCDR (arg)) >= 0)
19779 {
19780 *width = XINT (XCDR (arg));
19781 return BAR_CURSOR;
19782 }
19783
19784 if (EQ (arg, Qhbar))
19785 {
19786 *width = 2;
19787 return HBAR_CURSOR;
19788 }
19789
19790 if (CONSP (arg)
19791 && EQ (XCAR (arg), Qhbar)
19792 && INTEGERP (XCDR (arg))
19793 && XINT (XCDR (arg)) >= 0)
19794 {
19795 *width = XINT (XCDR (arg));
19796 return HBAR_CURSOR;
19797 }
19798
19799 /* Treat anything unknown as "hollow box cursor".
19800 It was bad to signal an error; people have trouble fixing
19801 .Xdefaults with Emacs, when it has something bad in it. */
19802 type = HOLLOW_BOX_CURSOR;
19803
19804 return type;
19805 }
19806
19807 /* Set the default cursor types for specified frame. */
19808 void
19809 set_frame_cursor_types (f, arg)
19810 struct frame *f;
19811 Lisp_Object arg;
19812 {
19813 int width;
19814 Lisp_Object tem;
19815
19816 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
19817 FRAME_CURSOR_WIDTH (f) = width;
19818
19819 /* By default, set up the blink-off state depending on the on-state. */
19820
19821 tem = Fassoc (arg, Vblink_cursor_alist);
19822 if (!NILP (tem))
19823 {
19824 FRAME_BLINK_OFF_CURSOR (f)
19825 = get_specified_cursor_type (XCDR (tem), &width);
19826 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
19827 }
19828 else
19829 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
19830 }
19831
19832
19833 /* Return the cursor we want to be displayed in window W. Return
19834 width of bar/hbar cursor through WIDTH arg. Return with
19835 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
19836 (i.e. if the `system caret' should track this cursor).
19837
19838 In a mini-buffer window, we want the cursor only to appear if we
19839 are reading input from this window. For the selected window, we
19840 want the cursor type given by the frame parameter or buffer local
19841 setting of cursor-type. If explicitly marked off, draw no cursor.
19842 In all other cases, we want a hollow box cursor. */
19843
19844 static enum text_cursor_kinds
19845 get_window_cursor_type (w, glyph, width, active_cursor)
19846 struct window *w;
19847 struct glyph *glyph;
19848 int *width;
19849 int *active_cursor;
19850 {
19851 struct frame *f = XFRAME (w->frame);
19852 struct buffer *b = XBUFFER (w->buffer);
19853 int cursor_type = DEFAULT_CURSOR;
19854 Lisp_Object alt_cursor;
19855 int non_selected = 0;
19856
19857 *active_cursor = 1;
19858
19859 /* Echo area */
19860 if (cursor_in_echo_area
19861 && FRAME_HAS_MINIBUF_P (f)
19862 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
19863 {
19864 if (w == XWINDOW (echo_area_window))
19865 {
19866 *width = FRAME_CURSOR_WIDTH (f);
19867 return FRAME_DESIRED_CURSOR (f);
19868 }
19869
19870 *active_cursor = 0;
19871 non_selected = 1;
19872 }
19873
19874 /* Nonselected window or nonselected frame. */
19875 else if (w != XWINDOW (f->selected_window)
19876 #ifdef HAVE_WINDOW_SYSTEM
19877 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
19878 #endif
19879 )
19880 {
19881 *active_cursor = 0;
19882
19883 if (MINI_WINDOW_P (w) && minibuf_level == 0)
19884 return NO_CURSOR;
19885
19886 non_selected = 1;
19887 }
19888
19889 /* Never display a cursor in a window in which cursor-type is nil. */
19890 if (NILP (b->cursor_type))
19891 return NO_CURSOR;
19892
19893 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
19894 if (non_selected)
19895 {
19896 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
19897 return get_specified_cursor_type (alt_cursor, width);
19898 }
19899
19900 /* Get the normal cursor type for this window. */
19901 if (EQ (b->cursor_type, Qt))
19902 {
19903 cursor_type = FRAME_DESIRED_CURSOR (f);
19904 *width = FRAME_CURSOR_WIDTH (f);
19905 }
19906 else
19907 cursor_type = get_specified_cursor_type (b->cursor_type, width);
19908
19909 /* Use normal cursor if not blinked off. */
19910 if (!w->cursor_off_p)
19911 {
19912 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
19913 if (cursor_type == FILLED_BOX_CURSOR)
19914 cursor_type = HOLLOW_BOX_CURSOR;
19915 }
19916 return cursor_type;
19917 }
19918
19919 /* Cursor is blinked off, so determine how to "toggle" it. */
19920
19921 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
19922 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
19923 return get_specified_cursor_type (XCDR (alt_cursor), width);
19924
19925 /* Then see if frame has specified a specific blink off cursor type. */
19926 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
19927 {
19928 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
19929 return FRAME_BLINK_OFF_CURSOR (f);
19930 }
19931
19932 #if 0
19933 /* Some people liked having a permanently visible blinking cursor,
19934 while others had very strong opinions against it. So it was
19935 decided to remove it. KFS 2003-09-03 */
19936
19937 /* Finally perform built-in cursor blinking:
19938 filled box <-> hollow box
19939 wide [h]bar <-> narrow [h]bar
19940 narrow [h]bar <-> no cursor
19941 other type <-> no cursor */
19942
19943 if (cursor_type == FILLED_BOX_CURSOR)
19944 return HOLLOW_BOX_CURSOR;
19945
19946 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
19947 {
19948 *width = 1;
19949 return cursor_type;
19950 }
19951 #endif
19952
19953 return NO_CURSOR;
19954 }
19955
19956
19957 #ifdef HAVE_WINDOW_SYSTEM
19958
19959 /* Notice when the text cursor of window W has been completely
19960 overwritten by a drawing operation that outputs glyphs in AREA
19961 starting at X0 and ending at X1 in the line starting at Y0 and
19962 ending at Y1. X coordinates are area-relative. X1 < 0 means all
19963 the rest of the line after X0 has been written. Y coordinates
19964 are window-relative. */
19965
19966 static void
19967 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
19968 struct window *w;
19969 enum glyph_row_area area;
19970 int x0, y0, x1, y1;
19971 {
19972 int cx0, cx1, cy0, cy1;
19973 struct glyph_row *row;
19974
19975 if (!w->phys_cursor_on_p)
19976 return;
19977 if (area != TEXT_AREA)
19978 return;
19979
19980 row = w->current_matrix->rows + w->phys_cursor.vpos;
19981 if (!row->displays_text_p)
19982 return;
19983
19984 if (row->cursor_in_fringe_p)
19985 {
19986 row->cursor_in_fringe_p = 0;
19987 draw_fringe_bitmap (w, row, 0);
19988 w->phys_cursor_on_p = 0;
19989 return;
19990 }
19991
19992 cx0 = w->phys_cursor.x;
19993 cx1 = cx0 + w->phys_cursor_width;
19994 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
19995 return;
19996
19997 /* The cursor image will be completely removed from the
19998 screen if the output area intersects the cursor area in
19999 y-direction. When we draw in [y0 y1[, and some part of
20000 the cursor is at y < y0, that part must have been drawn
20001 before. When scrolling, the cursor is erased before
20002 actually scrolling, so we don't come here. When not
20003 scrolling, the rows above the old cursor row must have
20004 changed, and in this case these rows must have written
20005 over the cursor image.
20006
20007 Likewise if part of the cursor is below y1, with the
20008 exception of the cursor being in the first blank row at
20009 the buffer and window end because update_text_area
20010 doesn't draw that row. (Except when it does, but
20011 that's handled in update_text_area.) */
20012
20013 cy0 = w->phys_cursor.y;
20014 cy1 = cy0 + w->phys_cursor_height;
20015 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
20016 return;
20017
20018 w->phys_cursor_on_p = 0;
20019 }
20020
20021 #endif /* HAVE_WINDOW_SYSTEM */
20022
20023 \f
20024 /************************************************************************
20025 Mouse Face
20026 ************************************************************************/
20027
20028 #ifdef HAVE_WINDOW_SYSTEM
20029
20030 /* EXPORT for RIF:
20031 Fix the display of area AREA of overlapping row ROW in window W. */
20032
20033 void
20034 x_fix_overlapping_area (w, row, area)
20035 struct window *w;
20036 struct glyph_row *row;
20037 enum glyph_row_area area;
20038 {
20039 int i, x;
20040
20041 BLOCK_INPUT;
20042
20043 x = 0;
20044 for (i = 0; i < row->used[area];)
20045 {
20046 if (row->glyphs[area][i].overlaps_vertically_p)
20047 {
20048 int start = i, start_x = x;
20049
20050 do
20051 {
20052 x += row->glyphs[area][i].pixel_width;
20053 ++i;
20054 }
20055 while (i < row->used[area]
20056 && row->glyphs[area][i].overlaps_vertically_p);
20057
20058 draw_glyphs (w, start_x, row, area,
20059 start, i,
20060 DRAW_NORMAL_TEXT, 1);
20061 }
20062 else
20063 {
20064 x += row->glyphs[area][i].pixel_width;
20065 ++i;
20066 }
20067 }
20068
20069 UNBLOCK_INPUT;
20070 }
20071
20072
20073 /* EXPORT:
20074 Draw the cursor glyph of window W in glyph row ROW. See the
20075 comment of draw_glyphs for the meaning of HL. */
20076
20077 void
20078 draw_phys_cursor_glyph (w, row, hl)
20079 struct window *w;
20080 struct glyph_row *row;
20081 enum draw_glyphs_face hl;
20082 {
20083 /* If cursor hpos is out of bounds, don't draw garbage. This can
20084 happen in mini-buffer windows when switching between echo area
20085 glyphs and mini-buffer. */
20086 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
20087 {
20088 int on_p = w->phys_cursor_on_p;
20089 int x1;
20090 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
20091 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
20092 hl, 0);
20093 w->phys_cursor_on_p = on_p;
20094
20095 if (hl == DRAW_CURSOR)
20096 w->phys_cursor_width = x1 - w->phys_cursor.x;
20097 /* When we erase the cursor, and ROW is overlapped by other
20098 rows, make sure that these overlapping parts of other rows
20099 are redrawn. */
20100 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
20101 {
20102 if (row > w->current_matrix->rows
20103 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
20104 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
20105
20106 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
20107 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
20108 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
20109 }
20110 }
20111 }
20112
20113
20114 /* EXPORT:
20115 Erase the image of a cursor of window W from the screen. */
20116
20117 void
20118 erase_phys_cursor (w)
20119 struct window *w;
20120 {
20121 struct frame *f = XFRAME (w->frame);
20122 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20123 int hpos = w->phys_cursor.hpos;
20124 int vpos = w->phys_cursor.vpos;
20125 int mouse_face_here_p = 0;
20126 struct glyph_matrix *active_glyphs = w->current_matrix;
20127 struct glyph_row *cursor_row;
20128 struct glyph *cursor_glyph;
20129 enum draw_glyphs_face hl;
20130
20131 /* No cursor displayed or row invalidated => nothing to do on the
20132 screen. */
20133 if (w->phys_cursor_type == NO_CURSOR)
20134 goto mark_cursor_off;
20135
20136 /* VPOS >= active_glyphs->nrows means that window has been resized.
20137 Don't bother to erase the cursor. */
20138 if (vpos >= active_glyphs->nrows)
20139 goto mark_cursor_off;
20140
20141 /* If row containing cursor is marked invalid, there is nothing we
20142 can do. */
20143 cursor_row = MATRIX_ROW (active_glyphs, vpos);
20144 if (!cursor_row->enabled_p)
20145 goto mark_cursor_off;
20146
20147 /* If line spacing is > 0, old cursor may only be partially visible in
20148 window after split-window. So adjust visible height. */
20149 cursor_row->visible_height = min (cursor_row->visible_height,
20150 window_text_bottom_y (w) - cursor_row->y);
20151
20152 /* If row is completely invisible, don't attempt to delete a cursor which
20153 isn't there. This can happen if cursor is at top of a window, and
20154 we switch to a buffer with a header line in that window. */
20155 if (cursor_row->visible_height <= 0)
20156 goto mark_cursor_off;
20157
20158 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
20159 if (cursor_row->cursor_in_fringe_p)
20160 {
20161 cursor_row->cursor_in_fringe_p = 0;
20162 draw_fringe_bitmap (w, cursor_row, 0);
20163 goto mark_cursor_off;
20164 }
20165
20166 /* This can happen when the new row is shorter than the old one.
20167 In this case, either draw_glyphs or clear_end_of_line
20168 should have cleared the cursor. Note that we wouldn't be
20169 able to erase the cursor in this case because we don't have a
20170 cursor glyph at hand. */
20171 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
20172 goto mark_cursor_off;
20173
20174 /* If the cursor is in the mouse face area, redisplay that when
20175 we clear the cursor. */
20176 if (! NILP (dpyinfo->mouse_face_window)
20177 && w == XWINDOW (dpyinfo->mouse_face_window)
20178 && (vpos > dpyinfo->mouse_face_beg_row
20179 || (vpos == dpyinfo->mouse_face_beg_row
20180 && hpos >= dpyinfo->mouse_face_beg_col))
20181 && (vpos < dpyinfo->mouse_face_end_row
20182 || (vpos == dpyinfo->mouse_face_end_row
20183 && hpos < dpyinfo->mouse_face_end_col))
20184 /* Don't redraw the cursor's spot in mouse face if it is at the
20185 end of a line (on a newline). The cursor appears there, but
20186 mouse highlighting does not. */
20187 && cursor_row->used[TEXT_AREA] > hpos)
20188 mouse_face_here_p = 1;
20189
20190 /* Maybe clear the display under the cursor. */
20191 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
20192 {
20193 int x, y;
20194 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
20195 int width;
20196
20197 cursor_glyph = get_phys_cursor_glyph (w);
20198 if (cursor_glyph == NULL)
20199 goto mark_cursor_off;
20200
20201 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
20202 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
20203 width = min (cursor_glyph->pixel_width,
20204 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
20205
20206 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
20207 }
20208
20209 /* Erase the cursor by redrawing the character underneath it. */
20210 if (mouse_face_here_p)
20211 hl = DRAW_MOUSE_FACE;
20212 else
20213 hl = DRAW_NORMAL_TEXT;
20214 draw_phys_cursor_glyph (w, cursor_row, hl);
20215
20216 mark_cursor_off:
20217 w->phys_cursor_on_p = 0;
20218 w->phys_cursor_type = NO_CURSOR;
20219 }
20220
20221
20222 /* EXPORT:
20223 Display or clear cursor of window W. If ON is zero, clear the
20224 cursor. If it is non-zero, display the cursor. If ON is nonzero,
20225 where to put the cursor is specified by HPOS, VPOS, X and Y. */
20226
20227 void
20228 display_and_set_cursor (w, on, hpos, vpos, x, y)
20229 struct window *w;
20230 int on, hpos, vpos, x, y;
20231 {
20232 struct frame *f = XFRAME (w->frame);
20233 int new_cursor_type;
20234 int new_cursor_width;
20235 int active_cursor;
20236 struct glyph_row *glyph_row;
20237 struct glyph *glyph;
20238
20239 /* This is pointless on invisible frames, and dangerous on garbaged
20240 windows and frames; in the latter case, the frame or window may
20241 be in the midst of changing its size, and x and y may be off the
20242 window. */
20243 if (! FRAME_VISIBLE_P (f)
20244 || FRAME_GARBAGED_P (f)
20245 || vpos >= w->current_matrix->nrows
20246 || hpos >= w->current_matrix->matrix_w)
20247 return;
20248
20249 /* If cursor is off and we want it off, return quickly. */
20250 if (!on && !w->phys_cursor_on_p)
20251 return;
20252
20253 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
20254 /* If cursor row is not enabled, we don't really know where to
20255 display the cursor. */
20256 if (!glyph_row->enabled_p)
20257 {
20258 w->phys_cursor_on_p = 0;
20259 return;
20260 }
20261
20262 glyph = NULL;
20263 if (!glyph_row->exact_window_width_line_p
20264 || hpos < glyph_row->used[TEXT_AREA])
20265 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
20266
20267 xassert (interrupt_input_blocked);
20268
20269 /* Set new_cursor_type to the cursor we want to be displayed. */
20270 new_cursor_type = get_window_cursor_type (w, glyph,
20271 &new_cursor_width, &active_cursor);
20272
20273 /* If cursor is currently being shown and we don't want it to be or
20274 it is in the wrong place, or the cursor type is not what we want,
20275 erase it. */
20276 if (w->phys_cursor_on_p
20277 && (!on
20278 || w->phys_cursor.x != x
20279 || w->phys_cursor.y != y
20280 || new_cursor_type != w->phys_cursor_type
20281 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
20282 && new_cursor_width != w->phys_cursor_width)))
20283 erase_phys_cursor (w);
20284
20285 /* Don't check phys_cursor_on_p here because that flag is only set
20286 to zero in some cases where we know that the cursor has been
20287 completely erased, to avoid the extra work of erasing the cursor
20288 twice. In other words, phys_cursor_on_p can be 1 and the cursor
20289 still not be visible, or it has only been partly erased. */
20290 if (on)
20291 {
20292 w->phys_cursor_ascent = glyph_row->ascent;
20293 w->phys_cursor_height = glyph_row->height;
20294
20295 /* Set phys_cursor_.* before x_draw_.* is called because some
20296 of them may need the information. */
20297 w->phys_cursor.x = x;
20298 w->phys_cursor.y = glyph_row->y;
20299 w->phys_cursor.hpos = hpos;
20300 w->phys_cursor.vpos = vpos;
20301 }
20302
20303 rif->draw_window_cursor (w, glyph_row, x, y,
20304 new_cursor_type, new_cursor_width,
20305 on, active_cursor);
20306 }
20307
20308
20309 /* Switch the display of W's cursor on or off, according to the value
20310 of ON. */
20311
20312 static void
20313 update_window_cursor (w, on)
20314 struct window *w;
20315 int on;
20316 {
20317 /* Don't update cursor in windows whose frame is in the process
20318 of being deleted. */
20319 if (w->current_matrix)
20320 {
20321 BLOCK_INPUT;
20322 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20323 w->phys_cursor.x, w->phys_cursor.y);
20324 UNBLOCK_INPUT;
20325 }
20326 }
20327
20328
20329 /* Call update_window_cursor with parameter ON_P on all leaf windows
20330 in the window tree rooted at W. */
20331
20332 static void
20333 update_cursor_in_window_tree (w, on_p)
20334 struct window *w;
20335 int on_p;
20336 {
20337 while (w)
20338 {
20339 if (!NILP (w->hchild))
20340 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20341 else if (!NILP (w->vchild))
20342 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20343 else
20344 update_window_cursor (w, on_p);
20345
20346 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20347 }
20348 }
20349
20350
20351 /* EXPORT:
20352 Display the cursor on window W, or clear it, according to ON_P.
20353 Don't change the cursor's position. */
20354
20355 void
20356 x_update_cursor (f, on_p)
20357 struct frame *f;
20358 int on_p;
20359 {
20360 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20361 }
20362
20363
20364 /* EXPORT:
20365 Clear the cursor of window W to background color, and mark the
20366 cursor as not shown. This is used when the text where the cursor
20367 is is about to be rewritten. */
20368
20369 void
20370 x_clear_cursor (w)
20371 struct window *w;
20372 {
20373 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20374 update_window_cursor (w, 0);
20375 }
20376
20377
20378 /* EXPORT:
20379 Display the active region described by mouse_face_* according to DRAW. */
20380
20381 void
20382 show_mouse_face (dpyinfo, draw)
20383 Display_Info *dpyinfo;
20384 enum draw_glyphs_face draw;
20385 {
20386 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20387 struct frame *f = XFRAME (WINDOW_FRAME (w));
20388
20389 if (/* If window is in the process of being destroyed, don't bother
20390 to do anything. */
20391 w->current_matrix != NULL
20392 /* Don't update mouse highlight if hidden */
20393 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20394 /* Recognize when we are called to operate on rows that don't exist
20395 anymore. This can happen when a window is split. */
20396 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20397 {
20398 int phys_cursor_on_p = w->phys_cursor_on_p;
20399 struct glyph_row *row, *first, *last;
20400
20401 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20402 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20403
20404 for (row = first; row <= last && row->enabled_p; ++row)
20405 {
20406 int start_hpos, end_hpos, start_x;
20407
20408 /* For all but the first row, the highlight starts at column 0. */
20409 if (row == first)
20410 {
20411 start_hpos = dpyinfo->mouse_face_beg_col;
20412 start_x = dpyinfo->mouse_face_beg_x;
20413 }
20414 else
20415 {
20416 start_hpos = 0;
20417 start_x = 0;
20418 }
20419
20420 if (row == last)
20421 end_hpos = dpyinfo->mouse_face_end_col;
20422 else
20423 end_hpos = row->used[TEXT_AREA];
20424
20425 if (end_hpos > start_hpos)
20426 {
20427 draw_glyphs (w, start_x, row, TEXT_AREA,
20428 start_hpos, end_hpos,
20429 draw, 0);
20430
20431 row->mouse_face_p
20432 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20433 }
20434 }
20435
20436 /* When we've written over the cursor, arrange for it to
20437 be displayed again. */
20438 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20439 {
20440 BLOCK_INPUT;
20441 display_and_set_cursor (w, 1,
20442 w->phys_cursor.hpos, w->phys_cursor.vpos,
20443 w->phys_cursor.x, w->phys_cursor.y);
20444 UNBLOCK_INPUT;
20445 }
20446 }
20447
20448 /* Change the mouse cursor. */
20449 if (draw == DRAW_NORMAL_TEXT)
20450 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
20451 else if (draw == DRAW_MOUSE_FACE)
20452 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
20453 else
20454 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
20455 }
20456
20457 /* EXPORT:
20458 Clear out the mouse-highlighted active region.
20459 Redraw it un-highlighted first. Value is non-zero if mouse
20460 face was actually drawn unhighlighted. */
20461
20462 int
20463 clear_mouse_face (dpyinfo)
20464 Display_Info *dpyinfo;
20465 {
20466 int cleared = 0;
20467
20468 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
20469 {
20470 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
20471 cleared = 1;
20472 }
20473
20474 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20475 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20476 dpyinfo->mouse_face_window = Qnil;
20477 dpyinfo->mouse_face_overlay = Qnil;
20478 return cleared;
20479 }
20480
20481
20482 /* EXPORT:
20483 Non-zero if physical cursor of window W is within mouse face. */
20484
20485 int
20486 cursor_in_mouse_face_p (w)
20487 struct window *w;
20488 {
20489 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20490 int in_mouse_face = 0;
20491
20492 if (WINDOWP (dpyinfo->mouse_face_window)
20493 && XWINDOW (dpyinfo->mouse_face_window) == w)
20494 {
20495 int hpos = w->phys_cursor.hpos;
20496 int vpos = w->phys_cursor.vpos;
20497
20498 if (vpos >= dpyinfo->mouse_face_beg_row
20499 && vpos <= dpyinfo->mouse_face_end_row
20500 && (vpos > dpyinfo->mouse_face_beg_row
20501 || hpos >= dpyinfo->mouse_face_beg_col)
20502 && (vpos < dpyinfo->mouse_face_end_row
20503 || hpos < dpyinfo->mouse_face_end_col
20504 || dpyinfo->mouse_face_past_end))
20505 in_mouse_face = 1;
20506 }
20507
20508 return in_mouse_face;
20509 }
20510
20511
20512
20513 \f
20514 /* Find the glyph matrix position of buffer position CHARPOS in window
20515 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
20516 current glyphs must be up to date. If CHARPOS is above window
20517 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
20518 of last line in W. In the row containing CHARPOS, stop before glyphs
20519 having STOP as object. */
20520
20521 #if 1 /* This is a version of fast_find_position that's more correct
20522 in the presence of hscrolling, for example. I didn't install
20523 it right away because the problem fixed is minor, it failed
20524 in 20.x as well, and I think it's too risky to install
20525 so near the release of 21.1. 2001-09-25 gerd. */
20526
20527 static int
20528 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
20529 struct window *w;
20530 int charpos;
20531 int *hpos, *vpos, *x, *y;
20532 Lisp_Object stop;
20533 {
20534 struct glyph_row *row, *first;
20535 struct glyph *glyph, *end;
20536 int past_end = 0;
20537
20538 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20539 if (charpos < MATRIX_ROW_START_CHARPOS (first))
20540 {
20541 *x = first->x;
20542 *y = first->y;
20543 *hpos = 0;
20544 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
20545 return 1;
20546 }
20547
20548 row = row_containing_pos (w, charpos, first, NULL, 0);
20549 if (row == NULL)
20550 {
20551 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
20552 past_end = 1;
20553 }
20554
20555 *x = row->x;
20556 *y = row->y;
20557 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20558
20559 glyph = row->glyphs[TEXT_AREA];
20560 end = glyph + row->used[TEXT_AREA];
20561
20562 /* Skip over glyphs not having an object at the start of the row.
20563 These are special glyphs like truncation marks on terminal
20564 frames. */
20565 if (row->displays_text_p)
20566 while (glyph < end
20567 && INTEGERP (glyph->object)
20568 && !EQ (stop, glyph->object)
20569 && glyph->charpos < 0)
20570 {
20571 *x += glyph->pixel_width;
20572 ++glyph;
20573 }
20574
20575 while (glyph < end
20576 && !INTEGERP (glyph->object)
20577 && !EQ (stop, glyph->object)
20578 && (!BUFFERP (glyph->object)
20579 || glyph->charpos < charpos))
20580 {
20581 *x += glyph->pixel_width;
20582 ++glyph;
20583 }
20584
20585 *hpos = glyph - row->glyphs[TEXT_AREA];
20586 return !past_end;
20587 }
20588
20589 #else /* not 1 */
20590
20591 static int
20592 fast_find_position (w, pos, hpos, vpos, x, y, stop)
20593 struct window *w;
20594 int pos;
20595 int *hpos, *vpos, *x, *y;
20596 Lisp_Object stop;
20597 {
20598 int i;
20599 int lastcol;
20600 int maybe_next_line_p = 0;
20601 int line_start_position;
20602 int yb = window_text_bottom_y (w);
20603 struct glyph_row *row, *best_row;
20604 int row_vpos, best_row_vpos;
20605 int current_x;
20606
20607 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20608 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20609
20610 while (row->y < yb)
20611 {
20612 if (row->used[TEXT_AREA])
20613 line_start_position = row->glyphs[TEXT_AREA]->charpos;
20614 else
20615 line_start_position = 0;
20616
20617 if (line_start_position > pos)
20618 break;
20619 /* If the position sought is the end of the buffer,
20620 don't include the blank lines at the bottom of the window. */
20621 else if (line_start_position == pos
20622 && pos == BUF_ZV (XBUFFER (w->buffer)))
20623 {
20624 maybe_next_line_p = 1;
20625 break;
20626 }
20627 else if (line_start_position > 0)
20628 {
20629 best_row = row;
20630 best_row_vpos = row_vpos;
20631 }
20632
20633 if (row->y + row->height >= yb)
20634 break;
20635
20636 ++row;
20637 ++row_vpos;
20638 }
20639
20640 /* Find the right column within BEST_ROW. */
20641 lastcol = 0;
20642 current_x = best_row->x;
20643 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
20644 {
20645 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
20646 int charpos = glyph->charpos;
20647
20648 if (BUFFERP (glyph->object))
20649 {
20650 if (charpos == pos)
20651 {
20652 *hpos = i;
20653 *vpos = best_row_vpos;
20654 *x = current_x;
20655 *y = best_row->y;
20656 return 1;
20657 }
20658 else if (charpos > pos)
20659 break;
20660 }
20661 else if (EQ (glyph->object, stop))
20662 break;
20663
20664 if (charpos > 0)
20665 lastcol = i;
20666 current_x += glyph->pixel_width;
20667 }
20668
20669 /* If we're looking for the end of the buffer,
20670 and we didn't find it in the line we scanned,
20671 use the start of the following line. */
20672 if (maybe_next_line_p)
20673 {
20674 ++best_row;
20675 ++best_row_vpos;
20676 lastcol = 0;
20677 current_x = best_row->x;
20678 }
20679
20680 *vpos = best_row_vpos;
20681 *hpos = lastcol + 1;
20682 *x = current_x;
20683 *y = best_row->y;
20684 return 0;
20685 }
20686
20687 #endif /* not 1 */
20688
20689
20690 /* Find the position of the glyph for position POS in OBJECT in
20691 window W's current matrix, and return in *X, *Y the pixel
20692 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
20693
20694 RIGHT_P non-zero means return the position of the right edge of the
20695 glyph, RIGHT_P zero means return the left edge position.
20696
20697 If no glyph for POS exists in the matrix, return the position of
20698 the glyph with the next smaller position that is in the matrix, if
20699 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
20700 exists in the matrix, return the position of the glyph with the
20701 next larger position in OBJECT.
20702
20703 Value is non-zero if a glyph was found. */
20704
20705 static int
20706 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
20707 struct window *w;
20708 int pos;
20709 Lisp_Object object;
20710 int *hpos, *vpos, *x, *y;
20711 int right_p;
20712 {
20713 int yb = window_text_bottom_y (w);
20714 struct glyph_row *r;
20715 struct glyph *best_glyph = NULL;
20716 struct glyph_row *best_row = NULL;
20717 int best_x = 0;
20718
20719 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20720 r->enabled_p && r->y < yb;
20721 ++r)
20722 {
20723 struct glyph *g = r->glyphs[TEXT_AREA];
20724 struct glyph *e = g + r->used[TEXT_AREA];
20725 int gx;
20726
20727 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
20728 if (EQ (g->object, object))
20729 {
20730 if (g->charpos == pos)
20731 {
20732 best_glyph = g;
20733 best_x = gx;
20734 best_row = r;
20735 goto found;
20736 }
20737 else if (best_glyph == NULL
20738 || ((abs (g->charpos - pos)
20739 < abs (best_glyph->charpos - pos))
20740 && (right_p
20741 ? g->charpos < pos
20742 : g->charpos > pos)))
20743 {
20744 best_glyph = g;
20745 best_x = gx;
20746 best_row = r;
20747 }
20748 }
20749 }
20750
20751 found:
20752
20753 if (best_glyph)
20754 {
20755 *x = best_x;
20756 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
20757
20758 if (right_p)
20759 {
20760 *x += best_glyph->pixel_width;
20761 ++*hpos;
20762 }
20763
20764 *y = best_row->y;
20765 *vpos = best_row - w->current_matrix->rows;
20766 }
20767
20768 return best_glyph != NULL;
20769 }
20770
20771
20772 /* See if position X, Y is within a hot-spot of an image. */
20773
20774 static int
20775 on_hot_spot_p (hot_spot, x, y)
20776 Lisp_Object hot_spot;
20777 int x, y;
20778 {
20779 if (!CONSP (hot_spot))
20780 return 0;
20781
20782 if (EQ (XCAR (hot_spot), Qrect))
20783 {
20784 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
20785 Lisp_Object rect = XCDR (hot_spot);
20786 Lisp_Object tem;
20787 if (!CONSP (rect))
20788 return 0;
20789 if (!CONSP (XCAR (rect)))
20790 return 0;
20791 if (!CONSP (XCDR (rect)))
20792 return 0;
20793 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
20794 return 0;
20795 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
20796 return 0;
20797 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
20798 return 0;
20799 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
20800 return 0;
20801 return 1;
20802 }
20803 else if (EQ (XCAR (hot_spot), Qcircle))
20804 {
20805 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
20806 Lisp_Object circ = XCDR (hot_spot);
20807 Lisp_Object lr, lx0, ly0;
20808 if (CONSP (circ)
20809 && CONSP (XCAR (circ))
20810 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
20811 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
20812 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
20813 {
20814 double r = XFLOATINT (lr);
20815 double dx = XINT (lx0) - x;
20816 double dy = XINT (ly0) - y;
20817 return (dx * dx + dy * dy <= r * r);
20818 }
20819 }
20820 else if (EQ (XCAR (hot_spot), Qpoly))
20821 {
20822 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
20823 if (VECTORP (XCDR (hot_spot)))
20824 {
20825 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
20826 Lisp_Object *poly = v->contents;
20827 int n = v->size;
20828 int i;
20829 int inside = 0;
20830 Lisp_Object lx, ly;
20831 int x0, y0;
20832
20833 /* Need an even number of coordinates, and at least 3 edges. */
20834 if (n < 6 || n & 1)
20835 return 0;
20836
20837 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
20838 If count is odd, we are inside polygon. Pixels on edges
20839 may or may not be included depending on actual geometry of the
20840 polygon. */
20841 if ((lx = poly[n-2], !INTEGERP (lx))
20842 || (ly = poly[n-1], !INTEGERP (lx)))
20843 return 0;
20844 x0 = XINT (lx), y0 = XINT (ly);
20845 for (i = 0; i < n; i += 2)
20846 {
20847 int x1 = x0, y1 = y0;
20848 if ((lx = poly[i], !INTEGERP (lx))
20849 || (ly = poly[i+1], !INTEGERP (ly)))
20850 return 0;
20851 x0 = XINT (lx), y0 = XINT (ly);
20852
20853 /* Does this segment cross the X line? */
20854 if (x0 >= x)
20855 {
20856 if (x1 >= x)
20857 continue;
20858 }
20859 else if (x1 < x)
20860 continue;
20861 if (y > y0 && y > y1)
20862 continue;
20863 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
20864 inside = !inside;
20865 }
20866 return inside;
20867 }
20868 }
20869 /* If we don't understand the format, pretend we're not in the hot-spot. */
20870 return 0;
20871 }
20872
20873 Lisp_Object
20874 find_hot_spot (map, x, y)
20875 Lisp_Object map;
20876 int x, y;
20877 {
20878 while (CONSP (map))
20879 {
20880 if (CONSP (XCAR (map))
20881 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
20882 return XCAR (map);
20883 map = XCDR (map);
20884 }
20885
20886 return Qnil;
20887 }
20888
20889 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
20890 3, 3, 0,
20891 doc: /* Lookup in image map MAP coordinates X and Y.
20892 An image map is an alist where each element has the format (AREA ID PLIST).
20893 An AREA is specified as either a rectangle, a circle, or a polygon:
20894 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
20895 pixel coordinates of the upper left and bottom right corners.
20896 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
20897 and the radius of the circle; r may be a float or integer.
20898 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
20899 vector describes one corner in the polygon.
20900 Returns the alist element for the first matching AREA in MAP. */)
20901 (map, x, y)
20902 Lisp_Object map;
20903 Lisp_Object x, y;
20904 {
20905 if (NILP (map))
20906 return Qnil;
20907
20908 CHECK_NUMBER (x);
20909 CHECK_NUMBER (y);
20910
20911 return find_hot_spot (map, XINT (x), XINT (y));
20912 }
20913
20914
20915 /* Display frame CURSOR, optionally using shape defined by POINTER. */
20916 static void
20917 define_frame_cursor1 (f, cursor, pointer)
20918 struct frame *f;
20919 Cursor cursor;
20920 Lisp_Object pointer;
20921 {
20922 /* Do not change cursor shape while dragging mouse. */
20923 if (!NILP (do_mouse_tracking))
20924 return;
20925
20926 if (!NILP (pointer))
20927 {
20928 if (EQ (pointer, Qarrow))
20929 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20930 else if (EQ (pointer, Qhand))
20931 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
20932 else if (EQ (pointer, Qtext))
20933 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20934 else if (EQ (pointer, intern ("hdrag")))
20935 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
20936 #ifdef HAVE_X_WINDOWS
20937 else if (EQ (pointer, intern ("vdrag")))
20938 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
20939 #endif
20940 else if (EQ (pointer, intern ("hourglass")))
20941 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
20942 else if (EQ (pointer, Qmodeline))
20943 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
20944 else
20945 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20946 }
20947
20948 if (cursor != No_Cursor)
20949 rif->define_frame_cursor (f, cursor);
20950 }
20951
20952 /* Take proper action when mouse has moved to the mode or header line
20953 or marginal area AREA of window W, x-position X and y-position Y.
20954 X is relative to the start of the text display area of W, so the
20955 width of bitmap areas and scroll bars must be subtracted to get a
20956 position relative to the start of the mode line. */
20957
20958 static void
20959 note_mode_line_or_margin_highlight (w, x, y, area)
20960 struct window *w;
20961 int x, y;
20962 enum window_part area;
20963 {
20964 struct frame *f = XFRAME (w->frame);
20965 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20966 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20967 Lisp_Object pointer = Qnil;
20968 int charpos, dx, dy, width, height;
20969 Lisp_Object string, object = Qnil;
20970 Lisp_Object pos, help;
20971
20972 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
20973 string = mode_line_string (w, area, &x, &y, &charpos,
20974 &object, &dx, &dy, &width, &height);
20975 else
20976 {
20977 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
20978 string = marginal_area_string (w, area, &x, &y, &charpos,
20979 &object, &dx, &dy, &width, &height);
20980 }
20981
20982 help = Qnil;
20983
20984 if (IMAGEP (object))
20985 {
20986 Lisp_Object image_map, hotspot;
20987 if ((image_map = Fsafe_plist_get (XCDR (object), QCmap),
20988 !NILP (image_map))
20989 && (hotspot = find_hot_spot (image_map, dx, dy),
20990 CONSP (hotspot))
20991 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
20992 {
20993 Lisp_Object area_id, plist;
20994
20995 area_id = XCAR (hotspot);
20996 /* Could check AREA_ID to see if we enter/leave this hot-spot.
20997 If so, we could look for mouse-enter, mouse-leave
20998 properties in PLIST (and do something...). */
20999 hotspot = XCDR (hotspot);
21000 if (CONSP (hotspot)
21001 && (plist = XCAR (hotspot), CONSP (plist)))
21002 {
21003 pointer = Fsafe_plist_get (plist, Qpointer);
21004 if (NILP (pointer))
21005 pointer = Qhand;
21006 help = Fsafe_plist_get (plist, Qhelp_echo);
21007 if (!NILP (help))
21008 {
21009 help_echo_string = help;
21010 /* Is this correct? ++kfs */
21011 XSETWINDOW (help_echo_window, w);
21012 help_echo_object = w->buffer;
21013 help_echo_pos = charpos;
21014 }
21015 }
21016 if (NILP (pointer))
21017 pointer = Fsafe_plist_get (XCDR (object), QCpointer);
21018 }
21019 }
21020
21021 if (STRINGP (string))
21022 {
21023 pos = make_number (charpos);
21024 /* If we're on a string with `help-echo' text property, arrange
21025 for the help to be displayed. This is done by setting the
21026 global variable help_echo_string to the help string. */
21027 if (NILP (help))
21028 {
21029 help = Fget_text_property (pos, Qhelp_echo, string);
21030 if (!NILP (help))
21031 {
21032 help_echo_string = help;
21033 XSETWINDOW (help_echo_window, w);
21034 help_echo_object = string;
21035 help_echo_pos = charpos;
21036 }
21037 }
21038
21039 if (NILP (pointer))
21040 pointer = Fget_text_property (pos, Qpointer, string);
21041
21042 /* Change the mouse pointer according to what is under X/Y. */
21043 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
21044 {
21045 Lisp_Object map;
21046 map = Fget_text_property (pos, Qlocal_map, string);
21047 if (!KEYMAPP (map))
21048 map = Fget_text_property (pos, Qkeymap, string);
21049 if (!KEYMAPP (map))
21050 cursor = dpyinfo->vertical_scroll_bar_cursor;
21051 }
21052 }
21053
21054 define_frame_cursor1 (f, cursor, pointer);
21055 }
21056
21057
21058 /* EXPORT:
21059 Take proper action when the mouse has moved to position X, Y on
21060 frame F as regards highlighting characters that have mouse-face
21061 properties. Also de-highlighting chars where the mouse was before.
21062 X and Y can be negative or out of range. */
21063
21064 void
21065 note_mouse_highlight (f, x, y)
21066 struct frame *f;
21067 int x, y;
21068 {
21069 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21070 enum window_part part;
21071 Lisp_Object window;
21072 struct window *w;
21073 Cursor cursor = No_Cursor;
21074 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
21075 struct buffer *b;
21076
21077 /* When a menu is active, don't highlight because this looks odd. */
21078 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
21079 if (popup_activated ())
21080 return;
21081 #endif
21082
21083 if (NILP (Vmouse_highlight)
21084 || !f->glyphs_initialized_p)
21085 return;
21086
21087 dpyinfo->mouse_face_mouse_x = x;
21088 dpyinfo->mouse_face_mouse_y = y;
21089 dpyinfo->mouse_face_mouse_frame = f;
21090
21091 if (dpyinfo->mouse_face_defer)
21092 return;
21093
21094 if (gc_in_progress)
21095 {
21096 dpyinfo->mouse_face_deferred_gc = 1;
21097 return;
21098 }
21099
21100 /* Which window is that in? */
21101 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
21102
21103 /* If we were displaying active text in another window, clear that.
21104 Also clear if we move out of text area in same window. */
21105 if (! EQ (window, dpyinfo->mouse_face_window)
21106 || (part != ON_TEXT && !NILP (dpyinfo->mouse_face_window)))
21107 clear_mouse_face (dpyinfo);
21108
21109 /* Not on a window -> return. */
21110 if (!WINDOWP (window))
21111 return;
21112
21113 /* Reset help_echo_string. It will get recomputed below. */
21114 help_echo_string = Qnil;
21115
21116 /* Convert to window-relative pixel coordinates. */
21117 w = XWINDOW (window);
21118 frame_to_window_pixel_xy (w, &x, &y);
21119
21120 /* Handle tool-bar window differently since it doesn't display a
21121 buffer. */
21122 if (EQ (window, f->tool_bar_window))
21123 {
21124 note_tool_bar_highlight (f, x, y);
21125 return;
21126 }
21127
21128 /* Mouse is on the mode, header line or margin? */
21129 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
21130 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
21131 {
21132 note_mode_line_or_margin_highlight (w, x, y, part);
21133 return;
21134 }
21135
21136 if (part == ON_VERTICAL_BORDER)
21137 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21138 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
21139 || part == ON_SCROLL_BAR)
21140 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21141 else
21142 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21143
21144 /* Are we in a window whose display is up to date?
21145 And verify the buffer's text has not changed. */
21146 b = XBUFFER (w->buffer);
21147 if (part == ON_TEXT
21148 && EQ (w->window_end_valid, w->buffer)
21149 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
21150 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
21151 {
21152 int hpos, vpos, pos, i, dx, dy, area;
21153 struct glyph *glyph;
21154 Lisp_Object object;
21155 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
21156 Lisp_Object *overlay_vec = NULL;
21157 int noverlays;
21158 struct buffer *obuf;
21159 int obegv, ozv, same_region;
21160
21161 /* Find the glyph under X/Y. */
21162 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
21163
21164 /* Look for :pointer property on image. */
21165 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21166 {
21167 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21168 if (img != NULL && IMAGEP (img->spec))
21169 {
21170 Lisp_Object image_map, hotspot;
21171 if ((image_map = Fsafe_plist_get (XCDR (img->spec), QCmap),
21172 !NILP (image_map))
21173 && (hotspot = find_hot_spot (image_map,
21174 glyph->slice.x + dx,
21175 glyph->slice.y + dy),
21176 CONSP (hotspot))
21177 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21178 {
21179 Lisp_Object area_id, plist;
21180
21181 area_id = XCAR (hotspot);
21182 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21183 If so, we could look for mouse-enter, mouse-leave
21184 properties in PLIST (and do something...). */
21185 hotspot = XCDR (hotspot);
21186 if (CONSP (hotspot)
21187 && (plist = XCAR (hotspot), CONSP (plist)))
21188 {
21189 pointer = Fsafe_plist_get (plist, Qpointer);
21190 if (NILP (pointer))
21191 pointer = Qhand;
21192 help_echo_string = Fsafe_plist_get (plist, Qhelp_echo);
21193 if (!NILP (help_echo_string))
21194 {
21195 help_echo_window = window;
21196 help_echo_object = glyph->object;
21197 help_echo_pos = glyph->charpos;
21198 }
21199 }
21200 }
21201 if (NILP (pointer))
21202 pointer = Fsafe_plist_get (XCDR (img->spec), QCpointer);
21203 }
21204 }
21205
21206 /* Clear mouse face if X/Y not over text. */
21207 if (glyph == NULL
21208 || area != TEXT_AREA
21209 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
21210 {
21211 if (clear_mouse_face (dpyinfo))
21212 cursor = No_Cursor;
21213 if (NILP (pointer))
21214 {
21215 if (area != TEXT_AREA)
21216 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21217 else
21218 pointer = Vvoid_text_area_pointer;
21219 }
21220 goto set_cursor;
21221 }
21222
21223 pos = glyph->charpos;
21224 object = glyph->object;
21225 if (!STRINGP (object) && !BUFFERP (object))
21226 goto set_cursor;
21227
21228 /* If we get an out-of-range value, return now; avoid an error. */
21229 if (BUFFERP (object) && pos > BUF_Z (b))
21230 goto set_cursor;
21231
21232 /* Make the window's buffer temporarily current for
21233 overlays_at and compute_char_face. */
21234 obuf = current_buffer;
21235 current_buffer = b;
21236 obegv = BEGV;
21237 ozv = ZV;
21238 BEGV = BEG;
21239 ZV = Z;
21240
21241 /* Is this char mouse-active or does it have help-echo? */
21242 position = make_number (pos);
21243
21244 if (BUFFERP (object))
21245 {
21246 /* Put all the overlays we want in a vector in overlay_vec. */
21247 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
21248 /* Sort overlays into increasing priority order. */
21249 noverlays = sort_overlays (overlay_vec, noverlays, w);
21250 }
21251 else
21252 noverlays = 0;
21253
21254 same_region = (EQ (window, dpyinfo->mouse_face_window)
21255 && vpos >= dpyinfo->mouse_face_beg_row
21256 && vpos <= dpyinfo->mouse_face_end_row
21257 && (vpos > dpyinfo->mouse_face_beg_row
21258 || hpos >= dpyinfo->mouse_face_beg_col)
21259 && (vpos < dpyinfo->mouse_face_end_row
21260 || hpos < dpyinfo->mouse_face_end_col
21261 || dpyinfo->mouse_face_past_end));
21262
21263 if (same_region)
21264 cursor = No_Cursor;
21265
21266 /* Check mouse-face highlighting. */
21267 if (! same_region
21268 /* If there exists an overlay with mouse-face overlapping
21269 the one we are currently highlighting, we have to
21270 check if we enter the overlapping overlay, and then
21271 highlight only that. */
21272 || (OVERLAYP (dpyinfo->mouse_face_overlay)
21273 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
21274 {
21275 /* Find the highest priority overlay that has a mouse-face
21276 property. */
21277 overlay = Qnil;
21278 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
21279 {
21280 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
21281 if (!NILP (mouse_face))
21282 overlay = overlay_vec[i];
21283 }
21284
21285 /* If we're actually highlighting the same overlay as
21286 before, there's no need to do that again. */
21287 if (!NILP (overlay)
21288 && EQ (overlay, dpyinfo->mouse_face_overlay))
21289 goto check_help_echo;
21290
21291 dpyinfo->mouse_face_overlay = overlay;
21292
21293 /* Clear the display of the old active region, if any. */
21294 if (clear_mouse_face (dpyinfo))
21295 cursor = No_Cursor;
21296
21297 /* If no overlay applies, get a text property. */
21298 if (NILP (overlay))
21299 mouse_face = Fget_text_property (position, Qmouse_face, object);
21300
21301 /* Handle the overlay case. */
21302 if (!NILP (overlay))
21303 {
21304 /* Find the range of text around this char that
21305 should be active. */
21306 Lisp_Object before, after;
21307 int ignore;
21308
21309 before = Foverlay_start (overlay);
21310 after = Foverlay_end (overlay);
21311 /* Record this as the current active region. */
21312 fast_find_position (w, XFASTINT (before),
21313 &dpyinfo->mouse_face_beg_col,
21314 &dpyinfo->mouse_face_beg_row,
21315 &dpyinfo->mouse_face_beg_x,
21316 &dpyinfo->mouse_face_beg_y, Qnil);
21317
21318 dpyinfo->mouse_face_past_end
21319 = !fast_find_position (w, XFASTINT (after),
21320 &dpyinfo->mouse_face_end_col,
21321 &dpyinfo->mouse_face_end_row,
21322 &dpyinfo->mouse_face_end_x,
21323 &dpyinfo->mouse_face_end_y, Qnil);
21324 dpyinfo->mouse_face_window = window;
21325
21326 dpyinfo->mouse_face_face_id
21327 = face_at_buffer_position (w, pos, 0, 0,
21328 &ignore, pos + 1,
21329 !dpyinfo->mouse_face_hidden);
21330
21331 /* Display it as active. */
21332 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21333 cursor = No_Cursor;
21334 }
21335 /* Handle the text property case. */
21336 else if (!NILP (mouse_face) && BUFFERP (object))
21337 {
21338 /* Find the range of text around this char that
21339 should be active. */
21340 Lisp_Object before, after, beginning, end;
21341 int ignore;
21342
21343 beginning = Fmarker_position (w->start);
21344 end = make_number (BUF_Z (XBUFFER (object))
21345 - XFASTINT (w->window_end_pos));
21346 before
21347 = Fprevious_single_property_change (make_number (pos + 1),
21348 Qmouse_face,
21349 object, beginning);
21350 after
21351 = Fnext_single_property_change (position, Qmouse_face,
21352 object, end);
21353
21354 /* Record this as the current active region. */
21355 fast_find_position (w, XFASTINT (before),
21356 &dpyinfo->mouse_face_beg_col,
21357 &dpyinfo->mouse_face_beg_row,
21358 &dpyinfo->mouse_face_beg_x,
21359 &dpyinfo->mouse_face_beg_y, Qnil);
21360 dpyinfo->mouse_face_past_end
21361 = !fast_find_position (w, XFASTINT (after),
21362 &dpyinfo->mouse_face_end_col,
21363 &dpyinfo->mouse_face_end_row,
21364 &dpyinfo->mouse_face_end_x,
21365 &dpyinfo->mouse_face_end_y, Qnil);
21366 dpyinfo->mouse_face_window = window;
21367
21368 if (BUFFERP (object))
21369 dpyinfo->mouse_face_face_id
21370 = face_at_buffer_position (w, pos, 0, 0,
21371 &ignore, pos + 1,
21372 !dpyinfo->mouse_face_hidden);
21373
21374 /* Display it as active. */
21375 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21376 cursor = No_Cursor;
21377 }
21378 else if (!NILP (mouse_face) && STRINGP (object))
21379 {
21380 Lisp_Object b, e;
21381 int ignore;
21382
21383 b = Fprevious_single_property_change (make_number (pos + 1),
21384 Qmouse_face,
21385 object, Qnil);
21386 e = Fnext_single_property_change (position, Qmouse_face,
21387 object, Qnil);
21388 if (NILP (b))
21389 b = make_number (0);
21390 if (NILP (e))
21391 e = make_number (SCHARS (object) - 1);
21392 fast_find_string_pos (w, XINT (b), object,
21393 &dpyinfo->mouse_face_beg_col,
21394 &dpyinfo->mouse_face_beg_row,
21395 &dpyinfo->mouse_face_beg_x,
21396 &dpyinfo->mouse_face_beg_y, 0);
21397 fast_find_string_pos (w, XINT (e), object,
21398 &dpyinfo->mouse_face_end_col,
21399 &dpyinfo->mouse_face_end_row,
21400 &dpyinfo->mouse_face_end_x,
21401 &dpyinfo->mouse_face_end_y, 1);
21402 dpyinfo->mouse_face_past_end = 0;
21403 dpyinfo->mouse_face_window = window;
21404 dpyinfo->mouse_face_face_id
21405 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
21406 glyph->face_id, 1);
21407 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21408 cursor = No_Cursor;
21409 }
21410 else if (STRINGP (object) && NILP (mouse_face))
21411 {
21412 /* A string which doesn't have mouse-face, but
21413 the text ``under'' it might have. */
21414 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
21415 int start = MATRIX_ROW_START_CHARPOS (r);
21416
21417 pos = string_buffer_position (w, object, start);
21418 if (pos > 0)
21419 mouse_face = get_char_property_and_overlay (make_number (pos),
21420 Qmouse_face,
21421 w->buffer,
21422 &overlay);
21423 if (!NILP (mouse_face) && !NILP (overlay))
21424 {
21425 Lisp_Object before = Foverlay_start (overlay);
21426 Lisp_Object after = Foverlay_end (overlay);
21427 int ignore;
21428
21429 /* Note that we might not be able to find position
21430 BEFORE in the glyph matrix if the overlay is
21431 entirely covered by a `display' property. In
21432 this case, we overshoot. So let's stop in
21433 the glyph matrix before glyphs for OBJECT. */
21434 fast_find_position (w, XFASTINT (before),
21435 &dpyinfo->mouse_face_beg_col,
21436 &dpyinfo->mouse_face_beg_row,
21437 &dpyinfo->mouse_face_beg_x,
21438 &dpyinfo->mouse_face_beg_y,
21439 object);
21440
21441 dpyinfo->mouse_face_past_end
21442 = !fast_find_position (w, XFASTINT (after),
21443 &dpyinfo->mouse_face_end_col,
21444 &dpyinfo->mouse_face_end_row,
21445 &dpyinfo->mouse_face_end_x,
21446 &dpyinfo->mouse_face_end_y,
21447 Qnil);
21448 dpyinfo->mouse_face_window = window;
21449 dpyinfo->mouse_face_face_id
21450 = face_at_buffer_position (w, pos, 0, 0,
21451 &ignore, pos + 1,
21452 !dpyinfo->mouse_face_hidden);
21453
21454 /* Display it as active. */
21455 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21456 cursor = No_Cursor;
21457 }
21458 }
21459 }
21460
21461 check_help_echo:
21462
21463 /* Look for a `help-echo' property. */
21464 if (NILP (help_echo_string)) {
21465 Lisp_Object help, overlay;
21466
21467 /* Check overlays first. */
21468 help = overlay = Qnil;
21469 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
21470 {
21471 overlay = overlay_vec[i];
21472 help = Foverlay_get (overlay, Qhelp_echo);
21473 }
21474
21475 if (!NILP (help))
21476 {
21477 help_echo_string = help;
21478 help_echo_window = window;
21479 help_echo_object = overlay;
21480 help_echo_pos = pos;
21481 }
21482 else
21483 {
21484 Lisp_Object object = glyph->object;
21485 int charpos = glyph->charpos;
21486
21487 /* Try text properties. */
21488 if (STRINGP (object)
21489 && charpos >= 0
21490 && charpos < SCHARS (object))
21491 {
21492 help = Fget_text_property (make_number (charpos),
21493 Qhelp_echo, object);
21494 if (NILP (help))
21495 {
21496 /* If the string itself doesn't specify a help-echo,
21497 see if the buffer text ``under'' it does. */
21498 struct glyph_row *r
21499 = MATRIX_ROW (w->current_matrix, vpos);
21500 int start = MATRIX_ROW_START_CHARPOS (r);
21501 int pos = string_buffer_position (w, object, start);
21502 if (pos > 0)
21503 {
21504 help = Fget_char_property (make_number (pos),
21505 Qhelp_echo, w->buffer);
21506 if (!NILP (help))
21507 {
21508 charpos = pos;
21509 object = w->buffer;
21510 }
21511 }
21512 }
21513 }
21514 else if (BUFFERP (object)
21515 && charpos >= BEGV
21516 && charpos < ZV)
21517 help = Fget_text_property (make_number (charpos), Qhelp_echo,
21518 object);
21519
21520 if (!NILP (help))
21521 {
21522 help_echo_string = help;
21523 help_echo_window = window;
21524 help_echo_object = object;
21525 help_echo_pos = charpos;
21526 }
21527 }
21528 }
21529
21530 /* Look for a `pointer' property. */
21531 if (NILP (pointer))
21532 {
21533 /* Check overlays first. */
21534 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
21535 pointer = Foverlay_get (overlay_vec[i], Qpointer);
21536
21537 if (NILP (pointer))
21538 {
21539 Lisp_Object object = glyph->object;
21540 int charpos = glyph->charpos;
21541
21542 /* Try text properties. */
21543 if (STRINGP (object)
21544 && charpos >= 0
21545 && charpos < SCHARS (object))
21546 {
21547 pointer = Fget_text_property (make_number (charpos),
21548 Qpointer, object);
21549 if (NILP (pointer))
21550 {
21551 /* If the string itself doesn't specify a pointer,
21552 see if the buffer text ``under'' it does. */
21553 struct glyph_row *r
21554 = MATRIX_ROW (w->current_matrix, vpos);
21555 int start = MATRIX_ROW_START_CHARPOS (r);
21556 int pos = string_buffer_position (w, object, start);
21557 if (pos > 0)
21558 pointer = Fget_char_property (make_number (pos),
21559 Qpointer, w->buffer);
21560 }
21561 }
21562 else if (BUFFERP (object)
21563 && charpos >= BEGV
21564 && charpos < ZV)
21565 pointer = Fget_text_property (make_number (charpos),
21566 Qpointer, object);
21567 }
21568 }
21569
21570 BEGV = obegv;
21571 ZV = ozv;
21572 current_buffer = obuf;
21573 }
21574
21575 set_cursor:
21576
21577 define_frame_cursor1 (f, cursor, pointer);
21578 }
21579
21580
21581 /* EXPORT for RIF:
21582 Clear any mouse-face on window W. This function is part of the
21583 redisplay interface, and is called from try_window_id and similar
21584 functions to ensure the mouse-highlight is off. */
21585
21586 void
21587 x_clear_window_mouse_face (w)
21588 struct window *w;
21589 {
21590 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21591 Lisp_Object window;
21592
21593 BLOCK_INPUT;
21594 XSETWINDOW (window, w);
21595 if (EQ (window, dpyinfo->mouse_face_window))
21596 clear_mouse_face (dpyinfo);
21597 UNBLOCK_INPUT;
21598 }
21599
21600
21601 /* EXPORT:
21602 Just discard the mouse face information for frame F, if any.
21603 This is used when the size of F is changed. */
21604
21605 void
21606 cancel_mouse_face (f)
21607 struct frame *f;
21608 {
21609 Lisp_Object window;
21610 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21611
21612 window = dpyinfo->mouse_face_window;
21613 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
21614 {
21615 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21616 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21617 dpyinfo->mouse_face_window = Qnil;
21618 }
21619 }
21620
21621
21622 #endif /* HAVE_WINDOW_SYSTEM */
21623
21624 \f
21625 /***********************************************************************
21626 Exposure Events
21627 ***********************************************************************/
21628
21629 #ifdef HAVE_WINDOW_SYSTEM
21630
21631 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
21632 which intersects rectangle R. R is in window-relative coordinates. */
21633
21634 static void
21635 expose_area (w, row, r, area)
21636 struct window *w;
21637 struct glyph_row *row;
21638 XRectangle *r;
21639 enum glyph_row_area area;
21640 {
21641 struct glyph *first = row->glyphs[area];
21642 struct glyph *end = row->glyphs[area] + row->used[area];
21643 struct glyph *last;
21644 int first_x, start_x, x;
21645
21646 if (area == TEXT_AREA && row->fill_line_p)
21647 /* If row extends face to end of line write the whole line. */
21648 draw_glyphs (w, 0, row, area,
21649 0, row->used[area],
21650 DRAW_NORMAL_TEXT, 0);
21651 else
21652 {
21653 /* Set START_X to the window-relative start position for drawing glyphs of
21654 AREA. The first glyph of the text area can be partially visible.
21655 The first glyphs of other areas cannot. */
21656 start_x = window_box_left_offset (w, area);
21657 x = start_x;
21658 if (area == TEXT_AREA)
21659 x += row->x;
21660
21661 /* Find the first glyph that must be redrawn. */
21662 while (first < end
21663 && x + first->pixel_width < r->x)
21664 {
21665 x += first->pixel_width;
21666 ++first;
21667 }
21668
21669 /* Find the last one. */
21670 last = first;
21671 first_x = x;
21672 while (last < end
21673 && x < r->x + r->width)
21674 {
21675 x += last->pixel_width;
21676 ++last;
21677 }
21678
21679 /* Repaint. */
21680 if (last > first)
21681 draw_glyphs (w, first_x - start_x, row, area,
21682 first - row->glyphs[area], last - row->glyphs[area],
21683 DRAW_NORMAL_TEXT, 0);
21684 }
21685 }
21686
21687
21688 /* Redraw the parts of the glyph row ROW on window W intersecting
21689 rectangle R. R is in window-relative coordinates. Value is
21690 non-zero if mouse-face was overwritten. */
21691
21692 static int
21693 expose_line (w, row, r)
21694 struct window *w;
21695 struct glyph_row *row;
21696 XRectangle *r;
21697 {
21698 xassert (row->enabled_p);
21699
21700 if (row->mode_line_p || w->pseudo_window_p)
21701 draw_glyphs (w, 0, row, TEXT_AREA,
21702 0, row->used[TEXT_AREA],
21703 DRAW_NORMAL_TEXT, 0);
21704 else
21705 {
21706 if (row->used[LEFT_MARGIN_AREA])
21707 expose_area (w, row, r, LEFT_MARGIN_AREA);
21708 if (row->used[TEXT_AREA])
21709 expose_area (w, row, r, TEXT_AREA);
21710 if (row->used[RIGHT_MARGIN_AREA])
21711 expose_area (w, row, r, RIGHT_MARGIN_AREA);
21712 draw_row_fringe_bitmaps (w, row);
21713 }
21714
21715 return row->mouse_face_p;
21716 }
21717
21718
21719 /* Redraw those parts of glyphs rows during expose event handling that
21720 overlap other rows. Redrawing of an exposed line writes over parts
21721 of lines overlapping that exposed line; this function fixes that.
21722
21723 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
21724 row in W's current matrix that is exposed and overlaps other rows.
21725 LAST_OVERLAPPING_ROW is the last such row. */
21726
21727 static void
21728 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
21729 struct window *w;
21730 struct glyph_row *first_overlapping_row;
21731 struct glyph_row *last_overlapping_row;
21732 {
21733 struct glyph_row *row;
21734
21735 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
21736 if (row->overlapping_p)
21737 {
21738 xassert (row->enabled_p && !row->mode_line_p);
21739
21740 if (row->used[LEFT_MARGIN_AREA])
21741 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
21742
21743 if (row->used[TEXT_AREA])
21744 x_fix_overlapping_area (w, row, TEXT_AREA);
21745
21746 if (row->used[RIGHT_MARGIN_AREA])
21747 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
21748 }
21749 }
21750
21751
21752 /* Return non-zero if W's cursor intersects rectangle R. */
21753
21754 static int
21755 phys_cursor_in_rect_p (w, r)
21756 struct window *w;
21757 XRectangle *r;
21758 {
21759 XRectangle cr, result;
21760 struct glyph *cursor_glyph;
21761
21762 cursor_glyph = get_phys_cursor_glyph (w);
21763 if (cursor_glyph)
21764 {
21765 /* r is relative to W's box, but w->phys_cursor.x is relative
21766 to left edge of W's TEXT area. Adjust it. */
21767 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
21768 cr.y = w->phys_cursor.y;
21769 cr.width = cursor_glyph->pixel_width;
21770 cr.height = w->phys_cursor_height;
21771 /* ++KFS: W32 version used W32-specific IntersectRect here, but
21772 I assume the effect is the same -- and this is portable. */
21773 return x_intersect_rectangles (&cr, r, &result);
21774 }
21775 else
21776 return 0;
21777 }
21778
21779
21780 /* EXPORT:
21781 Draw a vertical window border to the right of window W if W doesn't
21782 have vertical scroll bars. */
21783
21784 void
21785 x_draw_vertical_border (w)
21786 struct window *w;
21787 {
21788 /* We could do better, if we knew what type of scroll-bar the adjacent
21789 windows (on either side) have... But we don't :-(
21790 However, I think this works ok. ++KFS 2003-04-25 */
21791
21792 /* Redraw borders between horizontally adjacent windows. Don't
21793 do it for frames with vertical scroll bars because either the
21794 right scroll bar of a window, or the left scroll bar of its
21795 neighbor will suffice as a border. */
21796 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
21797 return;
21798
21799 if (!WINDOW_RIGHTMOST_P (w)
21800 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
21801 {
21802 int x0, x1, y0, y1;
21803
21804 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21805 y1 -= 1;
21806
21807 rif->draw_vertical_window_border (w, x1, y0, y1);
21808 }
21809 else if (!WINDOW_LEFTMOST_P (w)
21810 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
21811 {
21812 int x0, x1, y0, y1;
21813
21814 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21815 y1 -= 1;
21816
21817 rif->draw_vertical_window_border (w, x0, y0, y1);
21818 }
21819 }
21820
21821
21822 /* Redraw the part of window W intersection rectangle FR. Pixel
21823 coordinates in FR are frame-relative. Call this function with
21824 input blocked. Value is non-zero if the exposure overwrites
21825 mouse-face. */
21826
21827 static int
21828 expose_window (w, fr)
21829 struct window *w;
21830 XRectangle *fr;
21831 {
21832 struct frame *f = XFRAME (w->frame);
21833 XRectangle wr, r;
21834 int mouse_face_overwritten_p = 0;
21835
21836 /* If window is not yet fully initialized, do nothing. This can
21837 happen when toolkit scroll bars are used and a window is split.
21838 Reconfiguring the scroll bar will generate an expose for a newly
21839 created window. */
21840 if (w->current_matrix == NULL)
21841 return 0;
21842
21843 /* When we're currently updating the window, display and current
21844 matrix usually don't agree. Arrange for a thorough display
21845 later. */
21846 if (w == updated_window)
21847 {
21848 SET_FRAME_GARBAGED (f);
21849 return 0;
21850 }
21851
21852 /* Frame-relative pixel rectangle of W. */
21853 wr.x = WINDOW_LEFT_EDGE_X (w);
21854 wr.y = WINDOW_TOP_EDGE_Y (w);
21855 wr.width = WINDOW_TOTAL_WIDTH (w);
21856 wr.height = WINDOW_TOTAL_HEIGHT (w);
21857
21858 if (x_intersect_rectangles (fr, &wr, &r))
21859 {
21860 int yb = window_text_bottom_y (w);
21861 struct glyph_row *row;
21862 int cursor_cleared_p;
21863 struct glyph_row *first_overlapping_row, *last_overlapping_row;
21864
21865 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
21866 r.x, r.y, r.width, r.height));
21867
21868 /* Convert to window coordinates. */
21869 r.x -= WINDOW_LEFT_EDGE_X (w);
21870 r.y -= WINDOW_TOP_EDGE_Y (w);
21871
21872 /* Turn off the cursor. */
21873 if (!w->pseudo_window_p
21874 && phys_cursor_in_rect_p (w, &r))
21875 {
21876 x_clear_cursor (w);
21877 cursor_cleared_p = 1;
21878 }
21879 else
21880 cursor_cleared_p = 0;
21881
21882 /* Update lines intersecting rectangle R. */
21883 first_overlapping_row = last_overlapping_row = NULL;
21884 for (row = w->current_matrix->rows;
21885 row->enabled_p;
21886 ++row)
21887 {
21888 int y0 = row->y;
21889 int y1 = MATRIX_ROW_BOTTOM_Y (row);
21890
21891 if ((y0 >= r.y && y0 < r.y + r.height)
21892 || (y1 > r.y && y1 < r.y + r.height)
21893 || (r.y >= y0 && r.y < y1)
21894 || (r.y + r.height > y0 && r.y + r.height < y1))
21895 {
21896 if (row->overlapping_p)
21897 {
21898 if (first_overlapping_row == NULL)
21899 first_overlapping_row = row;
21900 last_overlapping_row = row;
21901 }
21902
21903 if (expose_line (w, row, &r))
21904 mouse_face_overwritten_p = 1;
21905 }
21906
21907 if (y1 >= yb)
21908 break;
21909 }
21910
21911 /* Display the mode line if there is one. */
21912 if (WINDOW_WANTS_MODELINE_P (w)
21913 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
21914 row->enabled_p)
21915 && row->y < r.y + r.height)
21916 {
21917 if (expose_line (w, row, &r))
21918 mouse_face_overwritten_p = 1;
21919 }
21920
21921 if (!w->pseudo_window_p)
21922 {
21923 /* Fix the display of overlapping rows. */
21924 if (first_overlapping_row)
21925 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
21926
21927 /* Draw border between windows. */
21928 x_draw_vertical_border (w);
21929
21930 /* Turn the cursor on again. */
21931 if (cursor_cleared_p)
21932 update_window_cursor (w, 1);
21933 }
21934 }
21935
21936 return mouse_face_overwritten_p;
21937 }
21938
21939
21940
21941 /* Redraw (parts) of all windows in the window tree rooted at W that
21942 intersect R. R contains frame pixel coordinates. Value is
21943 non-zero if the exposure overwrites mouse-face. */
21944
21945 static int
21946 expose_window_tree (w, r)
21947 struct window *w;
21948 XRectangle *r;
21949 {
21950 struct frame *f = XFRAME (w->frame);
21951 int mouse_face_overwritten_p = 0;
21952
21953 while (w && !FRAME_GARBAGED_P (f))
21954 {
21955 if (!NILP (w->hchild))
21956 mouse_face_overwritten_p
21957 |= expose_window_tree (XWINDOW (w->hchild), r);
21958 else if (!NILP (w->vchild))
21959 mouse_face_overwritten_p
21960 |= expose_window_tree (XWINDOW (w->vchild), r);
21961 else
21962 mouse_face_overwritten_p |= expose_window (w, r);
21963
21964 w = NILP (w->next) ? NULL : XWINDOW (w->next);
21965 }
21966
21967 return mouse_face_overwritten_p;
21968 }
21969
21970
21971 /* EXPORT:
21972 Redisplay an exposed area of frame F. X and Y are the upper-left
21973 corner of the exposed rectangle. W and H are width and height of
21974 the exposed area. All are pixel values. W or H zero means redraw
21975 the entire frame. */
21976
21977 void
21978 expose_frame (f, x, y, w, h)
21979 struct frame *f;
21980 int x, y, w, h;
21981 {
21982 XRectangle r;
21983 int mouse_face_overwritten_p = 0;
21984
21985 TRACE ((stderr, "expose_frame "));
21986
21987 /* No need to redraw if frame will be redrawn soon. */
21988 if (FRAME_GARBAGED_P (f))
21989 {
21990 TRACE ((stderr, " garbaged\n"));
21991 return;
21992 }
21993
21994 /* If basic faces haven't been realized yet, there is no point in
21995 trying to redraw anything. This can happen when we get an expose
21996 event while Emacs is starting, e.g. by moving another window. */
21997 if (FRAME_FACE_CACHE (f) == NULL
21998 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
21999 {
22000 TRACE ((stderr, " no faces\n"));
22001 return;
22002 }
22003
22004 if (w == 0 || h == 0)
22005 {
22006 r.x = r.y = 0;
22007 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
22008 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
22009 }
22010 else
22011 {
22012 r.x = x;
22013 r.y = y;
22014 r.width = w;
22015 r.height = h;
22016 }
22017
22018 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
22019 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
22020
22021 if (WINDOWP (f->tool_bar_window))
22022 mouse_face_overwritten_p
22023 |= expose_window (XWINDOW (f->tool_bar_window), &r);
22024
22025 #ifdef HAVE_X_WINDOWS
22026 #ifndef MSDOS
22027 #ifndef USE_X_TOOLKIT
22028 if (WINDOWP (f->menu_bar_window))
22029 mouse_face_overwritten_p
22030 |= expose_window (XWINDOW (f->menu_bar_window), &r);
22031 #endif /* not USE_X_TOOLKIT */
22032 #endif
22033 #endif
22034
22035 /* Some window managers support a focus-follows-mouse style with
22036 delayed raising of frames. Imagine a partially obscured frame,
22037 and moving the mouse into partially obscured mouse-face on that
22038 frame. The visible part of the mouse-face will be highlighted,
22039 then the WM raises the obscured frame. With at least one WM, KDE
22040 2.1, Emacs is not getting any event for the raising of the frame
22041 (even tried with SubstructureRedirectMask), only Expose events.
22042 These expose events will draw text normally, i.e. not
22043 highlighted. Which means we must redo the highlight here.
22044 Subsume it under ``we love X''. --gerd 2001-08-15 */
22045 /* Included in Windows version because Windows most likely does not
22046 do the right thing if any third party tool offers
22047 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
22048 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
22049 {
22050 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22051 if (f == dpyinfo->mouse_face_mouse_frame)
22052 {
22053 int x = dpyinfo->mouse_face_mouse_x;
22054 int y = dpyinfo->mouse_face_mouse_y;
22055 clear_mouse_face (dpyinfo);
22056 note_mouse_highlight (f, x, y);
22057 }
22058 }
22059 }
22060
22061
22062 /* EXPORT:
22063 Determine the intersection of two rectangles R1 and R2. Return
22064 the intersection in *RESULT. Value is non-zero if RESULT is not
22065 empty. */
22066
22067 int
22068 x_intersect_rectangles (r1, r2, result)
22069 XRectangle *r1, *r2, *result;
22070 {
22071 XRectangle *left, *right;
22072 XRectangle *upper, *lower;
22073 int intersection_p = 0;
22074
22075 /* Rearrange so that R1 is the left-most rectangle. */
22076 if (r1->x < r2->x)
22077 left = r1, right = r2;
22078 else
22079 left = r2, right = r1;
22080
22081 /* X0 of the intersection is right.x0, if this is inside R1,
22082 otherwise there is no intersection. */
22083 if (right->x <= left->x + left->width)
22084 {
22085 result->x = right->x;
22086
22087 /* The right end of the intersection is the minimum of the
22088 the right ends of left and right. */
22089 result->width = (min (left->x + left->width, right->x + right->width)
22090 - result->x);
22091
22092 /* Same game for Y. */
22093 if (r1->y < r2->y)
22094 upper = r1, lower = r2;
22095 else
22096 upper = r2, lower = r1;
22097
22098 /* The upper end of the intersection is lower.y0, if this is inside
22099 of upper. Otherwise, there is no intersection. */
22100 if (lower->y <= upper->y + upper->height)
22101 {
22102 result->y = lower->y;
22103
22104 /* The lower end of the intersection is the minimum of the lower
22105 ends of upper and lower. */
22106 result->height = (min (lower->y + lower->height,
22107 upper->y + upper->height)
22108 - result->y);
22109 intersection_p = 1;
22110 }
22111 }
22112
22113 return intersection_p;
22114 }
22115
22116 #endif /* HAVE_WINDOW_SYSTEM */
22117
22118 \f
22119 /***********************************************************************
22120 Initialization
22121 ***********************************************************************/
22122
22123 void
22124 syms_of_xdisp ()
22125 {
22126 Vwith_echo_area_save_vector = Qnil;
22127 staticpro (&Vwith_echo_area_save_vector);
22128
22129 Vmessage_stack = Qnil;
22130 staticpro (&Vmessage_stack);
22131
22132 Qinhibit_redisplay = intern ("inhibit-redisplay");
22133 staticpro (&Qinhibit_redisplay);
22134
22135 message_dolog_marker1 = Fmake_marker ();
22136 staticpro (&message_dolog_marker1);
22137 message_dolog_marker2 = Fmake_marker ();
22138 staticpro (&message_dolog_marker2);
22139 message_dolog_marker3 = Fmake_marker ();
22140 staticpro (&message_dolog_marker3);
22141
22142 #if GLYPH_DEBUG
22143 defsubr (&Sdump_frame_glyph_matrix);
22144 defsubr (&Sdump_glyph_matrix);
22145 defsubr (&Sdump_glyph_row);
22146 defsubr (&Sdump_tool_bar_row);
22147 defsubr (&Strace_redisplay);
22148 defsubr (&Strace_to_stderr);
22149 #endif
22150 #ifdef HAVE_WINDOW_SYSTEM
22151 defsubr (&Stool_bar_lines_needed);
22152 defsubr (&Slookup_image_map);
22153 #endif
22154 defsubr (&Sformat_mode_line);
22155
22156 staticpro (&Qmenu_bar_update_hook);
22157 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
22158
22159 staticpro (&Qoverriding_terminal_local_map);
22160 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
22161
22162 staticpro (&Qoverriding_local_map);
22163 Qoverriding_local_map = intern ("overriding-local-map");
22164
22165 staticpro (&Qwindow_scroll_functions);
22166 Qwindow_scroll_functions = intern ("window-scroll-functions");
22167
22168 staticpro (&Qredisplay_end_trigger_functions);
22169 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
22170
22171 staticpro (&Qinhibit_point_motion_hooks);
22172 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
22173
22174 QCdata = intern (":data");
22175 staticpro (&QCdata);
22176 Qdisplay = intern ("display");
22177 staticpro (&Qdisplay);
22178 Qspace_width = intern ("space-width");
22179 staticpro (&Qspace_width);
22180 Qraise = intern ("raise");
22181 staticpro (&Qraise);
22182 Qslice = intern ("slice");
22183 staticpro (&Qslice);
22184 Qspace = intern ("space");
22185 staticpro (&Qspace);
22186 Qmargin = intern ("margin");
22187 staticpro (&Qmargin);
22188 Qpointer = intern ("pointer");
22189 staticpro (&Qpointer);
22190 Qleft_margin = intern ("left-margin");
22191 staticpro (&Qleft_margin);
22192 Qright_margin = intern ("right-margin");
22193 staticpro (&Qright_margin);
22194 Qcenter = intern ("center");
22195 staticpro (&Qcenter);
22196 Qline_height = intern ("line-height");
22197 staticpro (&Qline_height);
22198 QCalign_to = intern (":align-to");
22199 staticpro (&QCalign_to);
22200 QCrelative_width = intern (":relative-width");
22201 staticpro (&QCrelative_width);
22202 QCrelative_height = intern (":relative-height");
22203 staticpro (&QCrelative_height);
22204 QCeval = intern (":eval");
22205 staticpro (&QCeval);
22206 QCpropertize = intern (":propertize");
22207 staticpro (&QCpropertize);
22208 QCfile = intern (":file");
22209 staticpro (&QCfile);
22210 Qfontified = intern ("fontified");
22211 staticpro (&Qfontified);
22212 Qfontification_functions = intern ("fontification-functions");
22213 staticpro (&Qfontification_functions);
22214 Qtrailing_whitespace = intern ("trailing-whitespace");
22215 staticpro (&Qtrailing_whitespace);
22216 Qescape_glyph = intern ("escape-glyph");
22217 staticpro (&Qescape_glyph);
22218 Qimage = intern ("image");
22219 staticpro (&Qimage);
22220 QCmap = intern (":map");
22221 staticpro (&QCmap);
22222 QCpointer = intern (":pointer");
22223 staticpro (&QCpointer);
22224 Qrect = intern ("rect");
22225 staticpro (&Qrect);
22226 Qcircle = intern ("circle");
22227 staticpro (&Qcircle);
22228 Qpoly = intern ("poly");
22229 staticpro (&Qpoly);
22230 Qmessage_truncate_lines = intern ("message-truncate-lines");
22231 staticpro (&Qmessage_truncate_lines);
22232 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
22233 staticpro (&Qcursor_in_non_selected_windows);
22234 Qgrow_only = intern ("grow-only");
22235 staticpro (&Qgrow_only);
22236 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
22237 staticpro (&Qinhibit_menubar_update);
22238 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
22239 staticpro (&Qinhibit_eval_during_redisplay);
22240 Qposition = intern ("position");
22241 staticpro (&Qposition);
22242 Qbuffer_position = intern ("buffer-position");
22243 staticpro (&Qbuffer_position);
22244 Qobject = intern ("object");
22245 staticpro (&Qobject);
22246 Qbar = intern ("bar");
22247 staticpro (&Qbar);
22248 Qhbar = intern ("hbar");
22249 staticpro (&Qhbar);
22250 Qbox = intern ("box");
22251 staticpro (&Qbox);
22252 Qhollow = intern ("hollow");
22253 staticpro (&Qhollow);
22254 Qhand = intern ("hand");
22255 staticpro (&Qhand);
22256 Qarrow = intern ("arrow");
22257 staticpro (&Qarrow);
22258 Qtext = intern ("text");
22259 staticpro (&Qtext);
22260 Qrisky_local_variable = intern ("risky-local-variable");
22261 staticpro (&Qrisky_local_variable);
22262 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
22263 staticpro (&Qinhibit_free_realized_faces);
22264
22265 list_of_error = Fcons (Fcons (intern ("error"),
22266 Fcons (intern ("void-variable"), Qnil)),
22267 Qnil);
22268 staticpro (&list_of_error);
22269
22270 Qlast_arrow_position = intern ("last-arrow-position");
22271 staticpro (&Qlast_arrow_position);
22272 Qlast_arrow_string = intern ("last-arrow-string");
22273 staticpro (&Qlast_arrow_string);
22274
22275 Qoverlay_arrow_string = intern ("overlay-arrow-string");
22276 staticpro (&Qoverlay_arrow_string);
22277 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
22278 staticpro (&Qoverlay_arrow_bitmap);
22279
22280 echo_buffer[0] = echo_buffer[1] = Qnil;
22281 staticpro (&echo_buffer[0]);
22282 staticpro (&echo_buffer[1]);
22283
22284 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
22285 staticpro (&echo_area_buffer[0]);
22286 staticpro (&echo_area_buffer[1]);
22287
22288 Vmessages_buffer_name = build_string ("*Messages*");
22289 staticpro (&Vmessages_buffer_name);
22290
22291 mode_line_proptrans_alist = Qnil;
22292 staticpro (&mode_line_proptrans_alist);
22293
22294 mode_line_string_list = Qnil;
22295 staticpro (&mode_line_string_list);
22296
22297 help_echo_string = Qnil;
22298 staticpro (&help_echo_string);
22299 help_echo_object = Qnil;
22300 staticpro (&help_echo_object);
22301 help_echo_window = Qnil;
22302 staticpro (&help_echo_window);
22303 previous_help_echo_string = Qnil;
22304 staticpro (&previous_help_echo_string);
22305 help_echo_pos = -1;
22306
22307 #ifdef HAVE_WINDOW_SYSTEM
22308 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
22309 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
22310 For example, if a block cursor is over a tab, it will be drawn as
22311 wide as that tab on the display. */);
22312 x_stretch_cursor_p = 0;
22313 #endif
22314
22315 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
22316 doc: /* *Non-nil means highlight trailing whitespace.
22317 The face used for trailing whitespace is `trailing-whitespace'. */);
22318 Vshow_trailing_whitespace = Qnil;
22319
22320 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
22321 doc: /* *The pointer shape to show in void text areas.
22322 Nil means to show the text pointer. Other options are `arrow', `text',
22323 `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
22324 Vvoid_text_area_pointer = Qarrow;
22325
22326 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
22327 doc: /* Non-nil means don't actually do any redisplay.
22328 This is used for internal purposes. */);
22329 Vinhibit_redisplay = Qnil;
22330
22331 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
22332 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
22333 Vglobal_mode_string = Qnil;
22334
22335 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
22336 doc: /* Marker for where to display an arrow on top of the buffer text.
22337 This must be the beginning of a line in order to work.
22338 See also `overlay-arrow-string'. */);
22339 Voverlay_arrow_position = Qnil;
22340
22341 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
22342 doc: /* String to display as an arrow in non-window frames.
22343 See also `overlay-arrow-position'. */);
22344 Voverlay_arrow_string = Qnil;
22345
22346 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
22347 doc: /* List of variables (symbols) which hold markers for overlay arrows.
22348 The symbols on this list are examined during redisplay to determine
22349 where to display overlay arrows. */);
22350 Voverlay_arrow_variable_list
22351 = Fcons (intern ("overlay-arrow-position"), Qnil);
22352
22353 DEFVAR_INT ("scroll-step", &scroll_step,
22354 doc: /* *The number of lines to try scrolling a window by when point moves out.
22355 If that fails to bring point back on frame, point is centered instead.
22356 If this is zero, point is always centered after it moves off frame.
22357 If you want scrolling to always be a line at a time, you should set
22358 `scroll-conservatively' to a large value rather than set this to 1. */);
22359
22360 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
22361 doc: /* *Scroll up to this many lines, to bring point back on screen.
22362 A value of zero means to scroll the text to center point vertically
22363 in the window. */);
22364 scroll_conservatively = 0;
22365
22366 DEFVAR_INT ("scroll-margin", &scroll_margin,
22367 doc: /* *Number of lines of margin at the top and bottom of a window.
22368 Recenter the window whenever point gets within this many lines
22369 of the top or bottom of the window. */);
22370 scroll_margin = 0;
22371
22372 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
22373 doc: /* Pixels per inch on current display.
22374 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
22375 Vdisplay_pixels_per_inch = make_float (72.0);
22376
22377 #if GLYPH_DEBUG
22378 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
22379 #endif
22380
22381 DEFVAR_BOOL ("truncate-partial-width-windows",
22382 &truncate_partial_width_windows,
22383 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
22384 truncate_partial_width_windows = 1;
22385
22386 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
22387 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
22388 Any other value means to use the appropriate face, `mode-line',
22389 `header-line', or `menu' respectively. */);
22390 mode_line_inverse_video = 1;
22391
22392 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
22393 doc: /* *Maximum buffer size for which line number should be displayed.
22394 If the buffer is bigger than this, the line number does not appear
22395 in the mode line. A value of nil means no limit. */);
22396 Vline_number_display_limit = Qnil;
22397
22398 DEFVAR_INT ("line-number-display-limit-width",
22399 &line_number_display_limit_width,
22400 doc: /* *Maximum line width (in characters) for line number display.
22401 If the average length of the lines near point is bigger than this, then the
22402 line number may be omitted from the mode line. */);
22403 line_number_display_limit_width = 200;
22404
22405 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
22406 doc: /* *Non-nil means highlight region even in nonselected windows. */);
22407 highlight_nonselected_windows = 0;
22408
22409 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
22410 doc: /* Non-nil if more than one frame is visible on this display.
22411 Minibuffer-only frames don't count, but iconified frames do.
22412 This variable is not guaranteed to be accurate except while processing
22413 `frame-title-format' and `icon-title-format'. */);
22414
22415 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
22416 doc: /* Template for displaying the title bar of visible frames.
22417 \(Assuming the window manager supports this feature.)
22418 This variable has the same structure as `mode-line-format' (which see),
22419 and is used only on frames for which no explicit name has been set
22420 \(see `modify-frame-parameters'). */);
22421
22422 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
22423 doc: /* Template for displaying the title bar of an iconified frame.
22424 \(Assuming the window manager supports this feature.)
22425 This variable has the same structure as `mode-line-format' (which see),
22426 and is used only on frames for which no explicit name has been set
22427 \(see `modify-frame-parameters'). */);
22428 Vicon_title_format
22429 = Vframe_title_format
22430 = Fcons (intern ("multiple-frames"),
22431 Fcons (build_string ("%b"),
22432 Fcons (Fcons (empty_string,
22433 Fcons (intern ("invocation-name"),
22434 Fcons (build_string ("@"),
22435 Fcons (intern ("system-name"),
22436 Qnil)))),
22437 Qnil)));
22438
22439 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
22440 doc: /* Maximum number of lines to keep in the message log buffer.
22441 If nil, disable message logging. If t, log messages but don't truncate
22442 the buffer when it becomes large. */);
22443 Vmessage_log_max = make_number (50);
22444
22445 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
22446 doc: /* Functions called before redisplay, if window sizes have changed.
22447 The value should be a list of functions that take one argument.
22448 Just before redisplay, for each frame, if any of its windows have changed
22449 size since the last redisplay, or have been split or deleted,
22450 all the functions in the list are called, with the frame as argument. */);
22451 Vwindow_size_change_functions = Qnil;
22452
22453 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
22454 doc: /* List of functions to call before redisplaying a window with scrolling.
22455 Each function is called with two arguments, the window
22456 and its new display-start position. Note that the value of `window-end'
22457 is not valid when these functions are called. */);
22458 Vwindow_scroll_functions = Qnil;
22459
22460 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
22461 doc: /* *Non-nil means autoselect window with mouse pointer. */);
22462 mouse_autoselect_window = 0;
22463
22464 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
22465 doc: /* *Non-nil means automatically resize tool-bars.
22466 This increases a tool-bar's height if not all tool-bar items are visible.
22467 It decreases a tool-bar's height when it would display blank lines
22468 otherwise. */);
22469 auto_resize_tool_bars_p = 1;
22470
22471 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
22472 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
22473 auto_raise_tool_bar_buttons_p = 1;
22474
22475 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
22476 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
22477 make_cursor_line_fully_visible_p = 1;
22478
22479 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
22480 doc: /* *Margin around tool-bar buttons in pixels.
22481 If an integer, use that for both horizontal and vertical margins.
22482 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
22483 HORZ specifying the horizontal margin, and VERT specifying the
22484 vertical margin. */);
22485 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
22486
22487 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
22488 doc: /* *Relief thickness of tool-bar buttons. */);
22489 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
22490
22491 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
22492 doc: /* List of functions to call to fontify regions of text.
22493 Each function is called with one argument POS. Functions must
22494 fontify a region starting at POS in the current buffer, and give
22495 fontified regions the property `fontified'. */);
22496 Vfontification_functions = Qnil;
22497 Fmake_variable_buffer_local (Qfontification_functions);
22498
22499 DEFVAR_BOOL ("unibyte-display-via-language-environment",
22500 &unibyte_display_via_language_environment,
22501 doc: /* *Non-nil means display unibyte text according to language environment.
22502 Specifically this means that unibyte non-ASCII characters
22503 are displayed by converting them to the equivalent multibyte characters
22504 according to the current language environment. As a result, they are
22505 displayed according to the current fontset. */);
22506 unibyte_display_via_language_environment = 0;
22507
22508 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
22509 doc: /* *Maximum height for resizing mini-windows.
22510 If a float, it specifies a fraction of the mini-window frame's height.
22511 If an integer, it specifies a number of lines. */);
22512 Vmax_mini_window_height = make_float (0.25);
22513
22514 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
22515 doc: /* *How to resize mini-windows.
22516 A value of nil means don't automatically resize mini-windows.
22517 A value of t means resize them to fit the text displayed in them.
22518 A value of `grow-only', the default, means let mini-windows grow
22519 only, until their display becomes empty, at which point the windows
22520 go back to their normal size. */);
22521 Vresize_mini_windows = Qgrow_only;
22522
22523 DEFVAR_LISP ("cursor-in-non-selected-windows",
22524 &Vcursor_in_non_selected_windows,
22525 doc: /* *Cursor type to display in non-selected windows.
22526 t means to use hollow box cursor. See `cursor-type' for other values. */);
22527 Vcursor_in_non_selected_windows = Qt;
22528
22529 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
22530 doc: /* Alist specifying how to blink the cursor off.
22531 Each element has the form (ON-STATE . OFF-STATE). Whenever the
22532 `cursor-type' frame-parameter or variable equals ON-STATE,
22533 comparing using `equal', Emacs uses OFF-STATE to specify
22534 how to blink it off. */);
22535 Vblink_cursor_alist = Qnil;
22536
22537 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
22538 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
22539 automatic_hscrolling_p = 1;
22540
22541 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
22542 doc: /* *How many columns away from the window edge point is allowed to get
22543 before automatic hscrolling will horizontally scroll the window. */);
22544 hscroll_margin = 5;
22545
22546 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
22547 doc: /* *How many columns to scroll the window when point gets too close to the edge.
22548 When point is less than `automatic-hscroll-margin' columns from the window
22549 edge, automatic hscrolling will scroll the window by the amount of columns
22550 determined by this variable. If its value is a positive integer, scroll that
22551 many columns. If it's a positive floating-point number, it specifies the
22552 fraction of the window's width to scroll. If it's nil or zero, point will be
22553 centered horizontally after the scroll. Any other value, including negative
22554 numbers, are treated as if the value were zero.
22555
22556 Automatic hscrolling always moves point outside the scroll margin, so if
22557 point was more than scroll step columns inside the margin, the window will
22558 scroll more than the value given by the scroll step.
22559
22560 Note that the lower bound for automatic hscrolling specified by `scroll-left'
22561 and `scroll-right' overrides this variable's effect. */);
22562 Vhscroll_step = make_number (0);
22563
22564 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
22565 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
22566 Bind this around calls to `message' to let it take effect. */);
22567 message_truncate_lines = 0;
22568
22569 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
22570 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
22571 Can be used to update submenus whose contents should vary. */);
22572 Vmenu_bar_update_hook = Qnil;
22573
22574 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
22575 doc: /* Non-nil means don't update menu bars. Internal use only. */);
22576 inhibit_menubar_update = 0;
22577
22578 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
22579 doc: /* Non-nil means don't eval Lisp during redisplay. */);
22580 inhibit_eval_during_redisplay = 0;
22581
22582 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
22583 doc: /* Non-nil means don't free realized faces. Internal use only. */);
22584 inhibit_free_realized_faces = 0;
22585
22586 #if GLYPH_DEBUG
22587 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
22588 doc: /* Inhibit try_window_id display optimization. */);
22589 inhibit_try_window_id = 0;
22590
22591 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
22592 doc: /* Inhibit try_window_reusing display optimization. */);
22593 inhibit_try_window_reusing = 0;
22594
22595 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
22596 doc: /* Inhibit try_cursor_movement display optimization. */);
22597 inhibit_try_cursor_movement = 0;
22598 #endif /* GLYPH_DEBUG */
22599 }
22600
22601
22602 /* Initialize this module when Emacs starts. */
22603
22604 void
22605 init_xdisp ()
22606 {
22607 Lisp_Object root_window;
22608 struct window *mini_w;
22609
22610 current_header_line_height = current_mode_line_height = -1;
22611
22612 CHARPOS (this_line_start_pos) = 0;
22613
22614 mini_w = XWINDOW (minibuf_window);
22615 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
22616
22617 if (!noninteractive)
22618 {
22619 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
22620 int i;
22621
22622 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
22623 set_window_height (root_window,
22624 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
22625 0);
22626 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
22627 set_window_height (minibuf_window, 1, 0);
22628
22629 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
22630 mini_w->total_cols = make_number (FRAME_COLS (f));
22631
22632 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
22633 scratch_glyph_row.glyphs[TEXT_AREA + 1]
22634 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
22635
22636 /* The default ellipsis glyphs `...'. */
22637 for (i = 0; i < 3; ++i)
22638 default_invis_vector[i] = make_number ('.');
22639 }
22640
22641 {
22642 /* Allocate the buffer for frame titles.
22643 Also used for `format-mode-line'. */
22644 int size = 100;
22645 frame_title_buf = (char *) xmalloc (size);
22646 frame_title_buf_end = frame_title_buf + size;
22647 frame_title_ptr = NULL;
22648 }
22649
22650 help_echo_showing_p = 0;
22651 }
22652
22653
22654 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
22655 (do not change this comment) */