]> code.delx.au - gnu-emacs/blob - src/xdisp.c
New directory
[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
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
202 Cursor No_Cursor;
203 #endif
204
205 #ifndef FRAME_X_OUTPUT
206 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
207 #endif
208
209 #define INFINITY 10000000
210
211 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
212 || defined (USE_GTK)
213 extern void set_frame_menubar P_ ((struct frame *f, int, int));
214 extern int pending_menu_activation;
215 #endif
216
217 extern int interrupt_input;
218 extern int command_loop_level;
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 Lisp_Object Qrisky_local_variable;
246
247 /* Holds the list (error). */
248 Lisp_Object list_of_error;
249
250 /* Functions called to fontify regions of text. */
251
252 Lisp_Object Vfontification_functions;
253 Lisp_Object Qfontification_functions;
254
255 /* Non-zero means automatically select any window when the mouse
256 cursor moves into it. */
257 int mouse_autoselect_window;
258
259 /* Non-zero means draw tool bar buttons raised when the mouse moves
260 over them. */
261
262 int auto_raise_tool_bar_buttons_p;
263
264 /* Margin around tool bar buttons in pixels. */
265
266 Lisp_Object Vtool_bar_button_margin;
267
268 /* Thickness of shadow to draw around tool bar buttons. */
269
270 EMACS_INT tool_bar_button_relief;
271
272 /* Non-zero means automatically resize tool-bars so that all tool-bar
273 items are visible, and no blank lines remain. */
274
275 int auto_resize_tool_bars_p;
276
277 /* Non-zero means draw block and hollow cursor as wide as the glyph
278 under it. For example, if a block cursor is over a tab, it will be
279 drawn as wide as that tab on the display. */
280
281 int x_stretch_cursor_p;
282
283 /* Non-nil means don't actually do any redisplay. */
284
285 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
286
287 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
288
289 int inhibit_eval_during_redisplay;
290
291 /* Names of text properties relevant for redisplay. */
292
293 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
294 extern Lisp_Object Qface, Qinvisible, Qwidth;
295
296 /* Symbols used in text property values. */
297
298 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
299 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
300 Lisp_Object Qmargin;
301 extern Lisp_Object Qheight;
302 extern Lisp_Object QCwidth, QCheight, QCascent;
303
304 /* Non-nil means highlight trailing whitespace. */
305
306 Lisp_Object Vshow_trailing_whitespace;
307
308 /* Name of the face used to highlight trailing whitespace. */
309
310 Lisp_Object Qtrailing_whitespace;
311
312 /* The symbol `image' which is the car of the lists used to represent
313 images in Lisp. */
314
315 Lisp_Object Qimage;
316
317 /* Non-zero means print newline to stdout before next mini-buffer
318 message. */
319
320 int noninteractive_need_newline;
321
322 /* Non-zero means print newline to message log before next message. */
323
324 static int message_log_need_newline;
325
326 /* Three markers that message_dolog uses.
327 It could allocate them itself, but that causes trouble
328 in handling memory-full errors. */
329 static Lisp_Object message_dolog_marker1;
330 static Lisp_Object message_dolog_marker2;
331 static Lisp_Object message_dolog_marker3;
332 \f
333 /* The buffer position of the first character appearing entirely or
334 partially on the line of the selected window which contains the
335 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
336 redisplay optimization in redisplay_internal. */
337
338 static struct text_pos this_line_start_pos;
339
340 /* Number of characters past the end of the line above, including the
341 terminating newline. */
342
343 static struct text_pos this_line_end_pos;
344
345 /* The vertical positions and the height of this line. */
346
347 static int this_line_vpos;
348 static int this_line_y;
349 static int this_line_pixel_height;
350
351 /* X position at which this display line starts. Usually zero;
352 negative if first character is partially visible. */
353
354 static int this_line_start_x;
355
356 /* Buffer that this_line_.* variables are referring to. */
357
358 static struct buffer *this_line_buffer;
359
360 /* Nonzero means truncate lines in all windows less wide than the
361 frame. */
362
363 int truncate_partial_width_windows;
364
365 /* A flag to control how to display unibyte 8-bit character. */
366
367 int unibyte_display_via_language_environment;
368
369 /* Nonzero means we have more than one non-mini-buffer-only frame.
370 Not guaranteed to be accurate except while parsing
371 frame-title-format. */
372
373 int multiple_frames;
374
375 Lisp_Object Vglobal_mode_string;
376
377 /* Marker for where to display an arrow on top of the buffer text. */
378
379 Lisp_Object Voverlay_arrow_position;
380
381 /* String to display for the arrow. Only used on terminal frames. */
382
383 Lisp_Object Voverlay_arrow_string;
384
385 /* Values of those variables at last redisplay. However, if
386 Voverlay_arrow_position is a marker, last_arrow_position is its
387 numerical position. */
388
389 static Lisp_Object last_arrow_position, last_arrow_string;
390
391 /* Like mode-line-format, but for the title bar on a visible frame. */
392
393 Lisp_Object Vframe_title_format;
394
395 /* Like mode-line-format, but for the title bar on an iconified frame. */
396
397 Lisp_Object Vicon_title_format;
398
399 /* List of functions to call when a window's size changes. These
400 functions get one arg, a frame on which one or more windows' sizes
401 have changed. */
402
403 static Lisp_Object Vwindow_size_change_functions;
404
405 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
406
407 /* Nonzero if overlay arrow has been displayed once in this window. */
408
409 static int overlay_arrow_seen;
410
411 /* Nonzero means highlight the region even in nonselected windows. */
412
413 int highlight_nonselected_windows;
414
415 /* If cursor motion alone moves point off frame, try scrolling this
416 many lines up or down if that will bring it back. */
417
418 static EMACS_INT scroll_step;
419
420 /* Nonzero means scroll just far enough to bring point back on the
421 screen, when appropriate. */
422
423 static EMACS_INT scroll_conservatively;
424
425 /* Recenter the window whenever point gets within this many lines of
426 the top or bottom of the window. This value is translated into a
427 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
428 that there is really a fixed pixel height scroll margin. */
429
430 EMACS_INT scroll_margin;
431
432 /* Number of windows showing the buffer of the selected window (or
433 another buffer with the same base buffer). keyboard.c refers to
434 this. */
435
436 int buffer_shared;
437
438 /* Vector containing glyphs for an ellipsis `...'. */
439
440 static Lisp_Object default_invis_vector[3];
441
442 /* Zero means display the mode-line/header-line/menu-bar in the default face
443 (this slightly odd definition is for compatibility with previous versions
444 of emacs), non-zero means display them using their respective faces.
445
446 This variable is deprecated. */
447
448 int mode_line_inverse_video;
449
450 /* Prompt to display in front of the mini-buffer contents. */
451
452 Lisp_Object minibuf_prompt;
453
454 /* Width of current mini-buffer prompt. Only set after display_line
455 of the line that contains the prompt. */
456
457 int minibuf_prompt_width;
458
459 /* This is the window where the echo area message was displayed. It
460 is always a mini-buffer window, but it may not be the same window
461 currently active as a mini-buffer. */
462
463 Lisp_Object echo_area_window;
464
465 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
466 pushes the current message and the value of
467 message_enable_multibyte on the stack, the function restore_message
468 pops the stack and displays MESSAGE again. */
469
470 Lisp_Object Vmessage_stack;
471
472 /* Nonzero means multibyte characters were enabled when the echo area
473 message was specified. */
474
475 int message_enable_multibyte;
476
477 /* Nonzero if we should redraw the mode lines on the next redisplay. */
478
479 int update_mode_lines;
480
481 /* Nonzero if window sizes or contents have changed since last
482 redisplay that finished. */
483
484 int windows_or_buffers_changed;
485
486 /* Nonzero means a frame's cursor type has been changed. */
487
488 int cursor_type_changed;
489
490 /* Nonzero after display_mode_line if %l was used and it displayed a
491 line number. */
492
493 int line_number_displayed;
494
495 /* Maximum buffer size for which to display line numbers. */
496
497 Lisp_Object Vline_number_display_limit;
498
499 /* Line width to consider when repositioning for line number display. */
500
501 static EMACS_INT line_number_display_limit_width;
502
503 /* Number of lines to keep in the message log buffer. t means
504 infinite. nil means don't log at all. */
505
506 Lisp_Object Vmessage_log_max;
507
508 /* The name of the *Messages* buffer, a string. */
509
510 static Lisp_Object Vmessages_buffer_name;
511
512 /* Current, index 0, and last displayed echo area message. Either
513 buffers from echo_buffers, or nil to indicate no message. */
514
515 Lisp_Object echo_area_buffer[2];
516
517 /* The buffers referenced from echo_area_buffer. */
518
519 static Lisp_Object echo_buffer[2];
520
521 /* A vector saved used in with_area_buffer to reduce consing. */
522
523 static Lisp_Object Vwith_echo_area_save_vector;
524
525 /* Non-zero means display_echo_area should display the last echo area
526 message again. Set by redisplay_preserve_echo_area. */
527
528 static int display_last_displayed_message_p;
529
530 /* Nonzero if echo area is being used by print; zero if being used by
531 message. */
532
533 int message_buf_print;
534
535 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
536
537 Lisp_Object Qinhibit_menubar_update;
538 int inhibit_menubar_update;
539
540 /* Maximum height for resizing mini-windows. Either a float
541 specifying a fraction of the available height, or an integer
542 specifying a number of lines. */
543
544 Lisp_Object Vmax_mini_window_height;
545
546 /* Non-zero means messages should be displayed with truncated
547 lines instead of being continued. */
548
549 int message_truncate_lines;
550 Lisp_Object Qmessage_truncate_lines;
551
552 /* Set to 1 in clear_message to make redisplay_internal aware
553 of an emptied echo area. */
554
555 static int message_cleared_p;
556
557 /* Non-zero means we want a hollow cursor in windows that are not
558 selected. Zero means there's no cursor in such windows. */
559
560 Lisp_Object Vcursor_in_non_selected_windows;
561 Lisp_Object Qcursor_in_non_selected_windows;
562
563 /* How to blink the default frame cursor off. */
564 Lisp_Object Vblink_cursor_alist;
565
566 /* A scratch glyph row with contents used for generating truncation
567 glyphs. Also used in direct_output_for_insert. */
568
569 #define MAX_SCRATCH_GLYPHS 100
570 struct glyph_row scratch_glyph_row;
571 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
572
573 /* Ascent and height of the last line processed by move_it_to. */
574
575 static int last_max_ascent, last_height;
576
577 /* Non-zero if there's a help-echo in the echo area. */
578
579 int help_echo_showing_p;
580
581 /* If >= 0, computed, exact values of mode-line and header-line height
582 to use in the macros CURRENT_MODE_LINE_HEIGHT and
583 CURRENT_HEADER_LINE_HEIGHT. */
584
585 int current_mode_line_height, current_header_line_height;
586
587 /* The maximum distance to look ahead for text properties. Values
588 that are too small let us call compute_char_face and similar
589 functions too often which is expensive. Values that are too large
590 let us call compute_char_face and alike too often because we
591 might not be interested in text properties that far away. */
592
593 #define TEXT_PROP_DISTANCE_LIMIT 100
594
595 #if GLYPH_DEBUG
596
597 /* Variables to turn off display optimizations from Lisp. */
598
599 int inhibit_try_window_id, inhibit_try_window_reusing;
600 int inhibit_try_cursor_movement;
601
602 /* Non-zero means print traces of redisplay if compiled with
603 GLYPH_DEBUG != 0. */
604
605 int trace_redisplay_p;
606
607 #endif /* GLYPH_DEBUG */
608
609 #ifdef DEBUG_TRACE_MOVE
610 /* Non-zero means trace with TRACE_MOVE to stderr. */
611 int trace_move;
612
613 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
614 #else
615 #define TRACE_MOVE(x) (void) 0
616 #endif
617
618 /* Non-zero means automatically scroll windows horizontally to make
619 point visible. */
620
621 int automatic_hscrolling_p;
622
623 /* How close to the margin can point get before the window is scrolled
624 horizontally. */
625 EMACS_INT hscroll_margin;
626
627 /* How much to scroll horizontally when point is inside the above margin. */
628 Lisp_Object Vhscroll_step;
629
630 /* A list of symbols, one for each supported image type. */
631
632 Lisp_Object Vimage_types;
633
634 /* The variable `resize-mini-windows'. If nil, don't resize
635 mini-windows. If t, always resize them to fit the text they
636 display. If `grow-only', let mini-windows grow only until they
637 become empty. */
638
639 Lisp_Object Vresize_mini_windows;
640
641 /* Buffer being redisplayed -- for redisplay_window_error. */
642
643 struct buffer *displayed_buffer;
644
645 /* Value returned from text property handlers (see below). */
646
647 enum prop_handled
648 {
649 HANDLED_NORMALLY,
650 HANDLED_RECOMPUTE_PROPS,
651 HANDLED_OVERLAY_STRING_CONSUMED,
652 HANDLED_RETURN
653 };
654
655 /* A description of text properties that redisplay is interested
656 in. */
657
658 struct props
659 {
660 /* The name of the property. */
661 Lisp_Object *name;
662
663 /* A unique index for the property. */
664 enum prop_idx idx;
665
666 /* A handler function called to set up iterator IT from the property
667 at IT's current position. Value is used to steer handle_stop. */
668 enum prop_handled (*handler) P_ ((struct it *it));
669 };
670
671 static enum prop_handled handle_face_prop P_ ((struct it *));
672 static enum prop_handled handle_invisible_prop P_ ((struct it *));
673 static enum prop_handled handle_display_prop P_ ((struct it *));
674 static enum prop_handled handle_composition_prop P_ ((struct it *));
675 static enum prop_handled handle_overlay_change P_ ((struct it *));
676 static enum prop_handled handle_fontified_prop P_ ((struct it *));
677
678 /* Properties handled by iterators. */
679
680 static struct props it_props[] =
681 {
682 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
683 /* Handle `face' before `display' because some sub-properties of
684 `display' need to know the face. */
685 {&Qface, FACE_PROP_IDX, handle_face_prop},
686 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
687 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
688 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
689 {NULL, 0, NULL}
690 };
691
692 /* Value is the position described by X. If X is a marker, value is
693 the marker_position of X. Otherwise, value is X. */
694
695 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
696
697 /* Enumeration returned by some move_it_.* functions internally. */
698
699 enum move_it_result
700 {
701 /* Not used. Undefined value. */
702 MOVE_UNDEFINED,
703
704 /* Move ended at the requested buffer position or ZV. */
705 MOVE_POS_MATCH_OR_ZV,
706
707 /* Move ended at the requested X pixel position. */
708 MOVE_X_REACHED,
709
710 /* Move within a line ended at the end of a line that must be
711 continued. */
712 MOVE_LINE_CONTINUED,
713
714 /* Move within a line ended at the end of a line that would
715 be displayed truncated. */
716 MOVE_LINE_TRUNCATED,
717
718 /* Move within a line ended at a line end. */
719 MOVE_NEWLINE_OR_CR
720 };
721
722 /* This counter is used to clear the face cache every once in a while
723 in redisplay_internal. It is incremented for each redisplay.
724 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
725 cleared. */
726
727 #define CLEAR_FACE_CACHE_COUNT 500
728 static int clear_face_cache_count;
729
730 /* Record the previous terminal frame we displayed. */
731
732 static struct frame *previous_terminal_frame;
733
734 /* Non-zero while redisplay_internal is in progress. */
735
736 int redisplaying_p;
737
738 /* Non-zero means don't free realized faces. Bound while freeing
739 realized faces is dangerous because glyph matrices might still
740 reference them. */
741
742 int inhibit_free_realized_faces;
743 Lisp_Object Qinhibit_free_realized_faces;
744
745 /* If a string, XTread_socket generates an event to display that string.
746 (The display is done in read_char.) */
747
748 Lisp_Object help_echo_string;
749 Lisp_Object help_echo_window;
750 Lisp_Object help_echo_object;
751 int help_echo_pos;
752
753 /* Temporary variable for XTread_socket. */
754
755 Lisp_Object previous_help_echo_string;
756
757
758 \f
759 /* Function prototypes. */
760
761 static void setup_for_ellipsis P_ ((struct it *));
762 static void mark_window_display_accurate_1 P_ ((struct window *, int));
763 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
764 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
765 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
766 static int redisplay_mode_lines P_ ((Lisp_Object, int));
767 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
768
769 #if 0
770 static int invisible_text_between_p P_ ((struct it *, int, int));
771 #endif
772
773 static int next_element_from_ellipsis P_ ((struct it *));
774 static void pint2str P_ ((char *, int, int));
775 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
776 struct text_pos));
777 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
778 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
779 static void store_frame_title_char P_ ((char));
780 static int store_frame_title P_ ((const unsigned char *, int, int));
781 static void x_consider_frame_title P_ ((Lisp_Object));
782 static void handle_stop P_ ((struct it *));
783 static int tool_bar_lines_needed P_ ((struct frame *));
784 static int single_display_prop_intangible_p P_ ((Lisp_Object));
785 static void ensure_echo_area_buffers P_ ((void));
786 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
787 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
788 static int with_echo_area_buffer P_ ((struct window *, int,
789 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
790 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
791 static void clear_garbaged_frames P_ ((void));
792 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
793 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
794 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
795 static int display_echo_area P_ ((struct window *));
796 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
797 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
798 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
799 static int string_char_and_length P_ ((const unsigned char *, int, int *));
800 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
801 struct text_pos));
802 static int compute_window_start_on_continuation_line P_ ((struct window *));
803 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
804 static void insert_left_trunc_glyphs P_ ((struct it *));
805 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
806 static void extend_face_to_end_of_line P_ ((struct it *));
807 static int append_space P_ ((struct it *, int));
808 static int make_cursor_line_fully_visible P_ ((struct window *));
809 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
810 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
811 static int trailing_whitespace_p P_ ((int));
812 static int message_log_check_duplicate P_ ((int, int, int, int));
813 static void push_it P_ ((struct it *));
814 static void pop_it P_ ((struct it *));
815 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
816 static void redisplay_internal P_ ((int));
817 static int echo_area_display P_ ((int));
818 static void redisplay_windows P_ ((Lisp_Object));
819 static void redisplay_window P_ ((Lisp_Object, int));
820 static Lisp_Object redisplay_window_error ();
821 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
822 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
823 static void update_menu_bar P_ ((struct frame *, int));
824 static int try_window_reusing_current_matrix P_ ((struct window *));
825 static int try_window_id P_ ((struct window *));
826 static int display_line P_ ((struct it *));
827 static int display_mode_lines P_ ((struct window *));
828 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
829 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
830 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
831 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
832 static void display_menu_bar P_ ((struct window *));
833 static int display_count_lines P_ ((int, int, int, int, int *));
834 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
835 int, int, struct it *, int, int, int, int));
836 static void compute_line_metrics P_ ((struct it *));
837 static void run_redisplay_end_trigger_hook P_ ((struct it *));
838 static int get_overlay_strings P_ ((struct it *, int));
839 static void next_overlay_string P_ ((struct it *));
840 static void reseat P_ ((struct it *, struct text_pos, int));
841 static void reseat_1 P_ ((struct it *, struct text_pos, int));
842 static void back_to_previous_visible_line_start P_ ((struct it *));
843 static void reseat_at_previous_visible_line_start P_ ((struct it *));
844 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
845 static int next_element_from_display_vector P_ ((struct it *));
846 static int next_element_from_string P_ ((struct it *));
847 static int next_element_from_c_string P_ ((struct it *));
848 static int next_element_from_buffer P_ ((struct it *));
849 static int next_element_from_composition P_ ((struct it *));
850 static int next_element_from_image P_ ((struct it *));
851 static int next_element_from_stretch P_ ((struct it *));
852 static void load_overlay_strings P_ ((struct it *, int));
853 static int init_from_display_pos P_ ((struct it *, struct window *,
854 struct display_pos *));
855 static void reseat_to_string P_ ((struct it *, unsigned char *,
856 Lisp_Object, int, int, int, int));
857 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
858 int, int, int));
859 void move_it_vertically_backward P_ ((struct it *, int));
860 static void init_to_row_start P_ ((struct it *, struct window *,
861 struct glyph_row *));
862 static int init_to_row_end P_ ((struct it *, struct window *,
863 struct glyph_row *));
864 static void back_to_previous_line_start P_ ((struct it *));
865 static int forward_to_next_line_start P_ ((struct it *, int *));
866 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
867 Lisp_Object, int));
868 static struct text_pos string_pos P_ ((int, Lisp_Object));
869 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
870 static int number_of_chars P_ ((unsigned char *, int));
871 static void compute_stop_pos P_ ((struct it *));
872 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
873 Lisp_Object));
874 static int face_before_or_after_it_pos P_ ((struct it *, int));
875 static int next_overlay_change P_ ((int));
876 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
877 Lisp_Object, struct text_pos *,
878 int));
879 static int underlying_face_id P_ ((struct it *));
880 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
881 struct window *));
882
883 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
884 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
885
886 #ifdef HAVE_WINDOW_SYSTEM
887
888 static void update_tool_bar P_ ((struct frame *, int));
889 static void build_desired_tool_bar_string P_ ((struct frame *f));
890 static int redisplay_tool_bar P_ ((struct frame *));
891 static void display_tool_bar_line P_ ((struct it *));
892 static void notice_overwritten_cursor P_ ((struct window *,
893 enum glyph_row_area,
894 int, int, int, int));
895
896
897
898 #endif /* HAVE_WINDOW_SYSTEM */
899
900 \f
901 /***********************************************************************
902 Window display dimensions
903 ***********************************************************************/
904
905 /* Return the bottom boundary y-position for text lines in window W.
906 This is the first y position at which a line cannot start.
907 It is relative to the top of the window.
908
909 This is the height of W minus the height of a mode line, if any. */
910
911 INLINE int
912 window_text_bottom_y (w)
913 struct window *w;
914 {
915 int height = WINDOW_TOTAL_HEIGHT (w);
916
917 if (WINDOW_WANTS_MODELINE_P (w))
918 height -= CURRENT_MODE_LINE_HEIGHT (w);
919 return height;
920 }
921
922 /* Return the pixel width of display area AREA of window W. AREA < 0
923 means return the total width of W, not including fringes to
924 the left and right of the window. */
925
926 INLINE int
927 window_box_width (w, area)
928 struct window *w;
929 int area;
930 {
931 int cols = XFASTINT (w->total_cols);
932 int pixels = 0;
933
934 if (!w->pseudo_window_p)
935 {
936 cols -= WINDOW_SCROLL_BAR_COLS (w);
937
938 if (area == TEXT_AREA)
939 {
940 if (INTEGERP (w->left_margin_cols))
941 cols -= XFASTINT (w->left_margin_cols);
942 if (INTEGERP (w->right_margin_cols))
943 cols -= XFASTINT (w->right_margin_cols);
944 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
945 }
946 else if (area == LEFT_MARGIN_AREA)
947 {
948 cols = (INTEGERP (w->left_margin_cols)
949 ? XFASTINT (w->left_margin_cols) : 0);
950 pixels = 0;
951 }
952 else if (area == RIGHT_MARGIN_AREA)
953 {
954 cols = (INTEGERP (w->right_margin_cols)
955 ? XFASTINT (w->right_margin_cols) : 0);
956 pixels = 0;
957 }
958 }
959
960 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
961 }
962
963
964 /* Return the pixel height of the display area of window W, not
965 including mode lines of W, if any. */
966
967 INLINE int
968 window_box_height (w)
969 struct window *w;
970 {
971 struct frame *f = XFRAME (w->frame);
972 int height = WINDOW_TOTAL_HEIGHT (w);
973
974 xassert (height >= 0);
975
976 /* Note: the code below that determines the mode-line/header-line
977 height is essentially the same as that contained in the macro
978 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
979 the appropriate glyph row has its `mode_line_p' flag set,
980 and if it doesn't, uses estimate_mode_line_height instead. */
981
982 if (WINDOW_WANTS_MODELINE_P (w))
983 {
984 struct glyph_row *ml_row
985 = (w->current_matrix && w->current_matrix->rows
986 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
987 : 0);
988 if (ml_row && ml_row->mode_line_p)
989 height -= ml_row->height;
990 else
991 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
992 }
993
994 if (WINDOW_WANTS_HEADER_LINE_P (w))
995 {
996 struct glyph_row *hl_row
997 = (w->current_matrix && w->current_matrix->rows
998 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
999 : 0);
1000 if (hl_row && hl_row->mode_line_p)
1001 height -= hl_row->height;
1002 else
1003 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1004 }
1005
1006 /* With a very small font and a mode-line that's taller than
1007 default, we might end up with a negative height. */
1008 return max (0, height);
1009 }
1010
1011 /* Return the window-relative coordinate of the left edge of display
1012 area AREA of window W. AREA < 0 means return the left edge of the
1013 whole window, to the right of the left fringe of W. */
1014
1015 INLINE int
1016 window_box_left_offset (w, area)
1017 struct window *w;
1018 int area;
1019 {
1020 int x;
1021
1022 if (w->pseudo_window_p)
1023 return 0;
1024
1025 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1026
1027 if (area == TEXT_AREA)
1028 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1029 + window_box_width (w, LEFT_MARGIN_AREA));
1030 else if (area == RIGHT_MARGIN_AREA)
1031 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1032 + window_box_width (w, LEFT_MARGIN_AREA)
1033 + window_box_width (w, TEXT_AREA)
1034 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1035 ? 0
1036 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1037 else if (area == LEFT_MARGIN_AREA
1038 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1039 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1040
1041 return x;
1042 }
1043
1044
1045 /* Return the window-relative coordinate of the right edge of display
1046 area AREA of window W. AREA < 0 means return the left edge of the
1047 whole window, to the left of the right fringe of W. */
1048
1049 INLINE int
1050 window_box_right_offset (w, area)
1051 struct window *w;
1052 int area;
1053 {
1054 return window_box_left_offset (w, area) + window_box_width (w, area);
1055 }
1056
1057 /* Return the frame-relative coordinate of the left edge of display
1058 area AREA of window W. AREA < 0 means return the left edge of the
1059 whole window, to the right of the left fringe of W. */
1060
1061 INLINE int
1062 window_box_left (w, area)
1063 struct window *w;
1064 int area;
1065 {
1066 struct frame *f = XFRAME (w->frame);
1067 int x;
1068
1069 if (w->pseudo_window_p)
1070 return FRAME_INTERNAL_BORDER_WIDTH (f);
1071
1072 x = (WINDOW_LEFT_EDGE_X (w)
1073 + window_box_left_offset (w, area));
1074
1075 return x;
1076 }
1077
1078
1079 /* Return the frame-relative coordinate of the right edge of display
1080 area AREA of window W. AREA < 0 means return the left edge of the
1081 whole window, to the left of the right fringe of W. */
1082
1083 INLINE int
1084 window_box_right (w, area)
1085 struct window *w;
1086 int area;
1087 {
1088 return window_box_left (w, area) + window_box_width (w, area);
1089 }
1090
1091 /* Get the bounding box of the display area AREA of window W, without
1092 mode lines, in frame-relative coordinates. AREA < 0 means the
1093 whole window, not including the left and right fringes of
1094 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1095 coordinates of the upper-left corner of the box. Return in
1096 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1097
1098 INLINE void
1099 window_box (w, area, box_x, box_y, box_width, box_height)
1100 struct window *w;
1101 int area;
1102 int *box_x, *box_y, *box_width, *box_height;
1103 {
1104 if (box_width)
1105 *box_width = window_box_width (w, area);
1106 if (box_height)
1107 *box_height = window_box_height (w);
1108 if (box_x)
1109 *box_x = window_box_left (w, area);
1110 if (box_y)
1111 {
1112 *box_y = WINDOW_TOP_EDGE_Y (w);
1113 if (WINDOW_WANTS_HEADER_LINE_P (w))
1114 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1115 }
1116 }
1117
1118
1119 /* Get the bounding box of the display area AREA of window W, without
1120 mode lines. AREA < 0 means the whole window, not including the
1121 left and right fringe of the window. Return in *TOP_LEFT_X
1122 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1123 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1124 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1125 box. */
1126
1127 INLINE void
1128 window_box_edges (w, area, top_left_x, top_left_y,
1129 bottom_right_x, bottom_right_y)
1130 struct window *w;
1131 int area;
1132 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1133 {
1134 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1135 bottom_right_y);
1136 *bottom_right_x += *top_left_x;
1137 *bottom_right_y += *top_left_y;
1138 }
1139
1140
1141 \f
1142 /***********************************************************************
1143 Utilities
1144 ***********************************************************************/
1145
1146 /* Return the bottom y-position of the line the iterator IT is in.
1147 This can modify IT's settings. */
1148
1149 int
1150 line_bottom_y (it)
1151 struct it *it;
1152 {
1153 int line_height = it->max_ascent + it->max_descent;
1154 int line_top_y = it->current_y;
1155
1156 if (line_height == 0)
1157 {
1158 if (last_height)
1159 line_height = last_height;
1160 else if (IT_CHARPOS (*it) < ZV)
1161 {
1162 move_it_by_lines (it, 1, 1);
1163 line_height = (it->max_ascent || it->max_descent
1164 ? it->max_ascent + it->max_descent
1165 : last_height);
1166 }
1167 else
1168 {
1169 struct glyph_row *row = it->glyph_row;
1170
1171 /* Use the default character height. */
1172 it->glyph_row = NULL;
1173 it->what = IT_CHARACTER;
1174 it->c = ' ';
1175 it->len = 1;
1176 PRODUCE_GLYPHS (it);
1177 line_height = it->ascent + it->descent;
1178 it->glyph_row = row;
1179 }
1180 }
1181
1182 return line_top_y + line_height;
1183 }
1184
1185
1186 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1187 1 if POS is visible and the line containing POS is fully visible.
1188 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1189 and header-lines heights. */
1190
1191 int
1192 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1193 struct window *w;
1194 int charpos, *fully, exact_mode_line_heights_p;
1195 {
1196 struct it it;
1197 struct text_pos top;
1198 int visible_p;
1199 struct buffer *old_buffer = NULL;
1200
1201 if (XBUFFER (w->buffer) != current_buffer)
1202 {
1203 old_buffer = current_buffer;
1204 set_buffer_internal_1 (XBUFFER (w->buffer));
1205 }
1206
1207 *fully = visible_p = 0;
1208 SET_TEXT_POS_FROM_MARKER (top, w->start);
1209
1210 /* Compute exact mode line heights, if requested. */
1211 if (exact_mode_line_heights_p)
1212 {
1213 if (WINDOW_WANTS_MODELINE_P (w))
1214 current_mode_line_height
1215 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1216 current_buffer->mode_line_format);
1217
1218 if (WINDOW_WANTS_HEADER_LINE_P (w))
1219 current_header_line_height
1220 = display_mode_line (w, HEADER_LINE_FACE_ID,
1221 current_buffer->header_line_format);
1222 }
1223
1224 start_display (&it, w, top);
1225 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1226 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1227
1228 /* Note that we may overshoot because of invisible text. */
1229 if (IT_CHARPOS (it) >= charpos)
1230 {
1231 int top_y = it.current_y;
1232 int bottom_y = line_bottom_y (&it);
1233 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1234
1235 if (top_y < window_top_y)
1236 visible_p = bottom_y > window_top_y;
1237 else if (top_y < it.last_visible_y)
1238 {
1239 visible_p = 1;
1240 *fully = bottom_y <= it.last_visible_y;
1241 }
1242 }
1243 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1244 {
1245 move_it_by_lines (&it, 1, 0);
1246 if (charpos < IT_CHARPOS (it))
1247 {
1248 visible_p = 1;
1249 *fully = 0;
1250 }
1251 }
1252
1253 if (old_buffer)
1254 set_buffer_internal_1 (old_buffer);
1255
1256 current_header_line_height = current_mode_line_height = -1;
1257 return visible_p;
1258 }
1259
1260
1261 /* Return the next character from STR which is MAXLEN bytes long.
1262 Return in *LEN the length of the character. This is like
1263 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1264 we find one, we return a `?', but with the length of the invalid
1265 character. */
1266
1267 static INLINE int
1268 string_char_and_length (str, maxlen, len)
1269 const unsigned char *str;
1270 int maxlen, *len;
1271 {
1272 int c;
1273
1274 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1275 if (!CHAR_VALID_P (c, 1))
1276 /* We may not change the length here because other places in Emacs
1277 don't use this function, i.e. they silently accept invalid
1278 characters. */
1279 c = '?';
1280
1281 return c;
1282 }
1283
1284
1285
1286 /* Given a position POS containing a valid character and byte position
1287 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1288
1289 static struct text_pos
1290 string_pos_nchars_ahead (pos, string, nchars)
1291 struct text_pos pos;
1292 Lisp_Object string;
1293 int nchars;
1294 {
1295 xassert (STRINGP (string) && nchars >= 0);
1296
1297 if (STRING_MULTIBYTE (string))
1298 {
1299 int rest = SBYTES (string) - BYTEPOS (pos);
1300 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1301 int len;
1302
1303 while (nchars--)
1304 {
1305 string_char_and_length (p, rest, &len);
1306 p += len, rest -= len;
1307 xassert (rest >= 0);
1308 CHARPOS (pos) += 1;
1309 BYTEPOS (pos) += len;
1310 }
1311 }
1312 else
1313 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1314
1315 return pos;
1316 }
1317
1318
1319 /* Value is the text position, i.e. character and byte position,
1320 for character position CHARPOS in STRING. */
1321
1322 static INLINE struct text_pos
1323 string_pos (charpos, string)
1324 int charpos;
1325 Lisp_Object string;
1326 {
1327 struct text_pos pos;
1328 xassert (STRINGP (string));
1329 xassert (charpos >= 0);
1330 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1331 return pos;
1332 }
1333
1334
1335 /* Value is a text position, i.e. character and byte position, for
1336 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1337 means recognize multibyte characters. */
1338
1339 static struct text_pos
1340 c_string_pos (charpos, s, multibyte_p)
1341 int charpos;
1342 unsigned char *s;
1343 int multibyte_p;
1344 {
1345 struct text_pos pos;
1346
1347 xassert (s != NULL);
1348 xassert (charpos >= 0);
1349
1350 if (multibyte_p)
1351 {
1352 int rest = strlen (s), len;
1353
1354 SET_TEXT_POS (pos, 0, 0);
1355 while (charpos--)
1356 {
1357 string_char_and_length (s, rest, &len);
1358 s += len, rest -= len;
1359 xassert (rest >= 0);
1360 CHARPOS (pos) += 1;
1361 BYTEPOS (pos) += len;
1362 }
1363 }
1364 else
1365 SET_TEXT_POS (pos, charpos, charpos);
1366
1367 return pos;
1368 }
1369
1370
1371 /* Value is the number of characters in C string S. MULTIBYTE_P
1372 non-zero means recognize multibyte characters. */
1373
1374 static int
1375 number_of_chars (s, multibyte_p)
1376 unsigned char *s;
1377 int multibyte_p;
1378 {
1379 int nchars;
1380
1381 if (multibyte_p)
1382 {
1383 int rest = strlen (s), len;
1384 unsigned char *p = (unsigned char *) s;
1385
1386 for (nchars = 0; rest > 0; ++nchars)
1387 {
1388 string_char_and_length (p, rest, &len);
1389 rest -= len, p += len;
1390 }
1391 }
1392 else
1393 nchars = strlen (s);
1394
1395 return nchars;
1396 }
1397
1398
1399 /* Compute byte position NEWPOS->bytepos corresponding to
1400 NEWPOS->charpos. POS is a known position in string STRING.
1401 NEWPOS->charpos must be >= POS.charpos. */
1402
1403 static void
1404 compute_string_pos (newpos, pos, string)
1405 struct text_pos *newpos, pos;
1406 Lisp_Object string;
1407 {
1408 xassert (STRINGP (string));
1409 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1410
1411 if (STRING_MULTIBYTE (string))
1412 *newpos = string_pos_nchars_ahead (pos, string,
1413 CHARPOS (*newpos) - CHARPOS (pos));
1414 else
1415 BYTEPOS (*newpos) = CHARPOS (*newpos);
1416 }
1417
1418 /* EXPORT:
1419 Return an estimation of the pixel height of mode or top lines on
1420 frame F. FACE_ID specifies what line's height to estimate. */
1421
1422 int
1423 estimate_mode_line_height (f, face_id)
1424 struct frame *f;
1425 enum face_id face_id;
1426 {
1427 #ifdef HAVE_WINDOW_SYSTEM
1428 if (FRAME_WINDOW_P (f))
1429 {
1430 int height = FONT_HEIGHT (FRAME_FONT (f));
1431
1432 /* This function is called so early when Emacs starts that the face
1433 cache and mode line face are not yet initialized. */
1434 if (FRAME_FACE_CACHE (f))
1435 {
1436 struct face *face = FACE_FROM_ID (f, face_id);
1437 if (face)
1438 {
1439 if (face->font)
1440 height = FONT_HEIGHT (face->font);
1441 if (face->box_line_width > 0)
1442 height += 2 * face->box_line_width;
1443 }
1444 }
1445
1446 return height;
1447 }
1448 #endif
1449
1450 return 1;
1451 }
1452
1453 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1454 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1455 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1456 not force the value into range. */
1457
1458 void
1459 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1460 FRAME_PTR f;
1461 register int pix_x, pix_y;
1462 int *x, *y;
1463 NativeRectangle *bounds;
1464 int noclip;
1465 {
1466
1467 #ifdef HAVE_WINDOW_SYSTEM
1468 if (FRAME_WINDOW_P (f))
1469 {
1470 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1471 even for negative values. */
1472 if (pix_x < 0)
1473 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1474 if (pix_y < 0)
1475 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1476
1477 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1478 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1479
1480 if (bounds)
1481 STORE_NATIVE_RECT (*bounds,
1482 FRAME_COL_TO_PIXEL_X (f, pix_x),
1483 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1484 FRAME_COLUMN_WIDTH (f) - 1,
1485 FRAME_LINE_HEIGHT (f) - 1);
1486
1487 if (!noclip)
1488 {
1489 if (pix_x < 0)
1490 pix_x = 0;
1491 else if (pix_x > FRAME_TOTAL_COLS (f))
1492 pix_x = FRAME_TOTAL_COLS (f);
1493
1494 if (pix_y < 0)
1495 pix_y = 0;
1496 else if (pix_y > FRAME_LINES (f))
1497 pix_y = FRAME_LINES (f);
1498 }
1499 }
1500 #endif
1501
1502 *x = pix_x;
1503 *y = pix_y;
1504 }
1505
1506
1507 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1508 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1509 can't tell the positions because W's display is not up to date,
1510 return 0. */
1511
1512 int
1513 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1514 struct window *w;
1515 int hpos, vpos;
1516 int *frame_x, *frame_y;
1517 {
1518 #ifdef HAVE_WINDOW_SYSTEM
1519 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1520 {
1521 int success_p;
1522
1523 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1524 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1525
1526 if (display_completed)
1527 {
1528 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1529 struct glyph *glyph = row->glyphs[TEXT_AREA];
1530 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1531
1532 hpos = row->x;
1533 vpos = row->y;
1534 while (glyph < end)
1535 {
1536 hpos += glyph->pixel_width;
1537 ++glyph;
1538 }
1539
1540 success_p = 1;
1541 }
1542 else
1543 {
1544 hpos = vpos = 0;
1545 success_p = 0;
1546 }
1547
1548 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1549 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1550 return success_p;
1551 }
1552 #endif
1553
1554 *frame_x = hpos;
1555 *frame_y = vpos;
1556 return 1;
1557 }
1558
1559
1560 #ifdef HAVE_WINDOW_SYSTEM
1561
1562 /* Find the glyph under window-relative coordinates X/Y in window W.
1563 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1564 strings. Return in *HPOS and *VPOS the row and column number of
1565 the glyph found. Return in *AREA the glyph area containing X.
1566 Value is a pointer to the glyph found or null if X/Y is not on
1567 text, or we can't tell because W's current matrix is not up to
1568 date. */
1569
1570 static struct glyph *
1571 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area, buffer_only_p)
1572 struct window *w;
1573 int x, y;
1574 int *hpos, *vpos, *area;
1575 int buffer_only_p;
1576 {
1577 struct glyph *glyph, *end;
1578 struct glyph_row *row = NULL;
1579 int x0, i;
1580
1581 /* Find row containing Y. Give up if some row is not enabled. */
1582 for (i = 0; i < w->current_matrix->nrows; ++i)
1583 {
1584 row = MATRIX_ROW (w->current_matrix, i);
1585 if (!row->enabled_p)
1586 return NULL;
1587 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1588 break;
1589 }
1590
1591 *vpos = i;
1592 *hpos = 0;
1593
1594 /* Give up if Y is not in the window. */
1595 if (i == w->current_matrix->nrows)
1596 return NULL;
1597
1598 /* Get the glyph area containing X. */
1599 if (w->pseudo_window_p)
1600 {
1601 *area = TEXT_AREA;
1602 x0 = 0;
1603 }
1604 else
1605 {
1606 if (x < window_box_left_offset (w, TEXT_AREA))
1607 {
1608 *area = LEFT_MARGIN_AREA;
1609 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1610 }
1611 else if (x < window_box_right_offset (w, TEXT_AREA))
1612 {
1613 *area = TEXT_AREA;
1614 x0 = window_box_left_offset (w, TEXT_AREA);
1615 }
1616 else
1617 {
1618 *area = RIGHT_MARGIN_AREA;
1619 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1620 }
1621 }
1622
1623 /* Find glyph containing X. */
1624 glyph = row->glyphs[*area];
1625 end = glyph + row->used[*area];
1626 while (glyph < end)
1627 {
1628 if (x < x0 + glyph->pixel_width)
1629 {
1630 if (w->pseudo_window_p)
1631 break;
1632 else if (!buffer_only_p || BUFFERP (glyph->object))
1633 break;
1634 }
1635
1636 x0 += glyph->pixel_width;
1637 ++glyph;
1638 }
1639
1640 if (glyph == end)
1641 return NULL;
1642
1643 *hpos = glyph - row->glyphs[*area];
1644 return glyph;
1645 }
1646
1647
1648 /* EXPORT:
1649 Convert frame-relative x/y to coordinates relative to window W.
1650 Takes pseudo-windows into account. */
1651
1652 void
1653 frame_to_window_pixel_xy (w, x, y)
1654 struct window *w;
1655 int *x, *y;
1656 {
1657 if (w->pseudo_window_p)
1658 {
1659 /* A pseudo-window is always full-width, and starts at the
1660 left edge of the frame, plus a frame border. */
1661 struct frame *f = XFRAME (w->frame);
1662 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1663 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1664 }
1665 else
1666 {
1667 *x -= WINDOW_LEFT_EDGE_X (w);
1668 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1669 }
1670 }
1671
1672 /* EXPORT:
1673 Return in *R the clipping rectangle for glyph string S. */
1674
1675 void
1676 get_glyph_string_clip_rect (s, nr)
1677 struct glyph_string *s;
1678 NativeRectangle *nr;
1679 {
1680 XRectangle r;
1681
1682 if (s->row->full_width_p)
1683 {
1684 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1685 r.x = WINDOW_LEFT_EDGE_X (s->w);
1686 r.width = WINDOW_TOTAL_WIDTH (s->w);
1687
1688 /* Unless displaying a mode or menu bar line, which are always
1689 fully visible, clip to the visible part of the row. */
1690 if (s->w->pseudo_window_p)
1691 r.height = s->row->visible_height;
1692 else
1693 r.height = s->height;
1694 }
1695 else
1696 {
1697 /* This is a text line that may be partially visible. */
1698 r.x = window_box_left (s->w, s->area);
1699 r.width = window_box_width (s->w, s->area);
1700 r.height = s->row->visible_height;
1701 }
1702
1703 /* If S draws overlapping rows, it's sufficient to use the top and
1704 bottom of the window for clipping because this glyph string
1705 intentionally draws over other lines. */
1706 if (s->for_overlaps_p)
1707 {
1708 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1709 r.height = window_text_bottom_y (s->w) - r.y;
1710 }
1711 else
1712 {
1713 /* Don't use S->y for clipping because it doesn't take partially
1714 visible lines into account. For example, it can be negative for
1715 partially visible lines at the top of a window. */
1716 if (!s->row->full_width_p
1717 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1718 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1719 else
1720 r.y = max (0, s->row->y);
1721
1722 /* If drawing a tool-bar window, draw it over the internal border
1723 at the top of the window. */
1724 if (s->w == XWINDOW (s->f->tool_bar_window))
1725 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1726 }
1727
1728 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1729
1730 #ifdef HAVE_NTGUI
1731 /* ++KFS: From W32 port, but it looks ok for all platforms to me. */
1732 /* If drawing the cursor, don't let glyph draw outside its
1733 advertised boundaries. Cleartype does this under some circumstances. */
1734 if (s->hl == DRAW_CURSOR)
1735 {
1736 if (s->x > r.x)
1737 {
1738 r.width -= s->x - r.x;
1739 r.x = s->x;
1740 }
1741 r.width = min (r.width, s->first_glyph->pixel_width);
1742 }
1743 #endif
1744
1745 #ifdef CONVERT_FROM_XRECT
1746 CONVERT_FROM_XRECT (r, *nr);
1747 #else
1748 *nr = r;
1749 #endif
1750 }
1751
1752 #endif /* HAVE_WINDOW_SYSTEM */
1753
1754 \f
1755 /***********************************************************************
1756 Lisp form evaluation
1757 ***********************************************************************/
1758
1759 /* Error handler for safe_eval and safe_call. */
1760
1761 static Lisp_Object
1762 safe_eval_handler (arg)
1763 Lisp_Object arg;
1764 {
1765 add_to_log ("Error during redisplay: %s", arg, Qnil);
1766 return Qnil;
1767 }
1768
1769
1770 /* Evaluate SEXPR and return the result, or nil if something went
1771 wrong. Prevent redisplay during the evaluation. */
1772
1773 Lisp_Object
1774 safe_eval (sexpr)
1775 Lisp_Object sexpr;
1776 {
1777 Lisp_Object val;
1778
1779 if (inhibit_eval_during_redisplay)
1780 val = Qnil;
1781 else
1782 {
1783 int count = SPECPDL_INDEX ();
1784 struct gcpro gcpro1;
1785
1786 GCPRO1 (sexpr);
1787 specbind (Qinhibit_redisplay, Qt);
1788 /* Use Qt to ensure debugger does not run,
1789 so there is no possibility of wanting to redisplay. */
1790 val = internal_condition_case_1 (Feval, sexpr, Qt,
1791 safe_eval_handler);
1792 UNGCPRO;
1793 val = unbind_to (count, val);
1794 }
1795
1796 return val;
1797 }
1798
1799
1800 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1801 Return the result, or nil if something went wrong. Prevent
1802 redisplay during the evaluation. */
1803
1804 Lisp_Object
1805 safe_call (nargs, args)
1806 int nargs;
1807 Lisp_Object *args;
1808 {
1809 Lisp_Object val;
1810
1811 if (inhibit_eval_during_redisplay)
1812 val = Qnil;
1813 else
1814 {
1815 int count = SPECPDL_INDEX ();
1816 struct gcpro gcpro1;
1817
1818 GCPRO1 (args[0]);
1819 gcpro1.nvars = nargs;
1820 specbind (Qinhibit_redisplay, Qt);
1821 /* Use Qt to ensure debugger does not run,
1822 so there is no possibility of wanting to redisplay. */
1823 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1824 safe_eval_handler);
1825 UNGCPRO;
1826 val = unbind_to (count, val);
1827 }
1828
1829 return val;
1830 }
1831
1832
1833 /* Call function FN with one argument ARG.
1834 Return the result, or nil if something went wrong. */
1835
1836 Lisp_Object
1837 safe_call1 (fn, arg)
1838 Lisp_Object fn, arg;
1839 {
1840 Lisp_Object args[2];
1841 args[0] = fn;
1842 args[1] = arg;
1843 return safe_call (2, args);
1844 }
1845
1846
1847 \f
1848 /***********************************************************************
1849 Debugging
1850 ***********************************************************************/
1851
1852 #if 0
1853
1854 /* Define CHECK_IT to perform sanity checks on iterators.
1855 This is for debugging. It is too slow to do unconditionally. */
1856
1857 static void
1858 check_it (it)
1859 struct it *it;
1860 {
1861 if (it->method == next_element_from_string)
1862 {
1863 xassert (STRINGP (it->string));
1864 xassert (IT_STRING_CHARPOS (*it) >= 0);
1865 }
1866 else if (it->method == next_element_from_buffer)
1867 {
1868 /* Check that character and byte positions agree. */
1869 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1870 }
1871
1872 if (it->dpvec)
1873 xassert (it->current.dpvec_index >= 0);
1874 else
1875 xassert (it->current.dpvec_index < 0);
1876 }
1877
1878 #define CHECK_IT(IT) check_it ((IT))
1879
1880 #else /* not 0 */
1881
1882 #define CHECK_IT(IT) (void) 0
1883
1884 #endif /* not 0 */
1885
1886
1887 #if GLYPH_DEBUG
1888
1889 /* Check that the window end of window W is what we expect it
1890 to be---the last row in the current matrix displaying text. */
1891
1892 static void
1893 check_window_end (w)
1894 struct window *w;
1895 {
1896 if (!MINI_WINDOW_P (w)
1897 && !NILP (w->window_end_valid))
1898 {
1899 struct glyph_row *row;
1900 xassert ((row = MATRIX_ROW (w->current_matrix,
1901 XFASTINT (w->window_end_vpos)),
1902 !row->enabled_p
1903 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1904 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1905 }
1906 }
1907
1908 #define CHECK_WINDOW_END(W) check_window_end ((W))
1909
1910 #else /* not GLYPH_DEBUG */
1911
1912 #define CHECK_WINDOW_END(W) (void) 0
1913
1914 #endif /* not GLYPH_DEBUG */
1915
1916
1917 \f
1918 /***********************************************************************
1919 Iterator initialization
1920 ***********************************************************************/
1921
1922 /* Initialize IT for displaying current_buffer in window W, starting
1923 at character position CHARPOS. CHARPOS < 0 means that no buffer
1924 position is specified which is useful when the iterator is assigned
1925 a position later. BYTEPOS is the byte position corresponding to
1926 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
1927
1928 If ROW is not null, calls to produce_glyphs with IT as parameter
1929 will produce glyphs in that row.
1930
1931 BASE_FACE_ID is the id of a base face to use. It must be one of
1932 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1933 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1934 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
1935
1936 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1937 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1938 will be initialized to use the corresponding mode line glyph row of
1939 the desired matrix of W. */
1940
1941 void
1942 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1943 struct it *it;
1944 struct window *w;
1945 int charpos, bytepos;
1946 struct glyph_row *row;
1947 enum face_id base_face_id;
1948 {
1949 int highlight_region_p;
1950
1951 /* Some precondition checks. */
1952 xassert (w != NULL && it != NULL);
1953 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1954 && charpos <= ZV));
1955
1956 /* If face attributes have been changed since the last redisplay,
1957 free realized faces now because they depend on face definitions
1958 that might have changed. Don't free faces while there might be
1959 desired matrices pending which reference these faces. */
1960 if (face_change_count && !inhibit_free_realized_faces)
1961 {
1962 face_change_count = 0;
1963 free_all_realized_faces (Qnil);
1964 }
1965
1966 /* Use one of the mode line rows of W's desired matrix if
1967 appropriate. */
1968 if (row == NULL)
1969 {
1970 if (base_face_id == MODE_LINE_FACE_ID
1971 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
1972 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1973 else if (base_face_id == HEADER_LINE_FACE_ID)
1974 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1975 }
1976
1977 /* Clear IT. */
1978 bzero (it, sizeof *it);
1979 it->current.overlay_string_index = -1;
1980 it->current.dpvec_index = -1;
1981 it->base_face_id = base_face_id;
1982
1983 /* The window in which we iterate over current_buffer: */
1984 XSETWINDOW (it->window, w);
1985 it->w = w;
1986 it->f = XFRAME (w->frame);
1987
1988 /* Extra space between lines (on window systems only). */
1989 if (base_face_id == DEFAULT_FACE_ID
1990 && FRAME_WINDOW_P (it->f))
1991 {
1992 if (NATNUMP (current_buffer->extra_line_spacing))
1993 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1994 else if (it->f->extra_line_spacing > 0)
1995 it->extra_line_spacing = it->f->extra_line_spacing;
1996 }
1997
1998 /* If realized faces have been removed, e.g. because of face
1999 attribute changes of named faces, recompute them. When running
2000 in batch mode, the face cache of Vterminal_frame is null. If
2001 we happen to get called, make a dummy face cache. */
2002 if (
2003 #ifndef WINDOWSNT
2004 noninteractive &&
2005 #endif
2006 FRAME_FACE_CACHE (it->f) == NULL)
2007 init_frame_faces (it->f);
2008 if (FRAME_FACE_CACHE (it->f)->used == 0)
2009 recompute_basic_faces (it->f);
2010
2011 /* Current value of the `space-width', and 'height' properties. */
2012 it->space_width = Qnil;
2013 it->font_height = Qnil;
2014
2015 /* Are control characters displayed as `^C'? */
2016 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2017
2018 /* -1 means everything between a CR and the following line end
2019 is invisible. >0 means lines indented more than this value are
2020 invisible. */
2021 it->selective = (INTEGERP (current_buffer->selective_display)
2022 ? XFASTINT (current_buffer->selective_display)
2023 : (!NILP (current_buffer->selective_display)
2024 ? -1 : 0));
2025 it->selective_display_ellipsis_p
2026 = !NILP (current_buffer->selective_display_ellipses);
2027
2028 /* Display table to use. */
2029 it->dp = window_display_table (w);
2030
2031 /* Are multibyte characters enabled in current_buffer? */
2032 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2033
2034 /* Non-zero if we should highlight the region. */
2035 highlight_region_p
2036 = (!NILP (Vtransient_mark_mode)
2037 && !NILP (current_buffer->mark_active)
2038 && XMARKER (current_buffer->mark)->buffer != 0);
2039
2040 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2041 start and end of a visible region in window IT->w. Set both to
2042 -1 to indicate no region. */
2043 if (highlight_region_p
2044 /* Maybe highlight only in selected window. */
2045 && (/* Either show region everywhere. */
2046 highlight_nonselected_windows
2047 /* Or show region in the selected window. */
2048 || w == XWINDOW (selected_window)
2049 /* Or show the region if we are in the mini-buffer and W is
2050 the window the mini-buffer refers to. */
2051 || (MINI_WINDOW_P (XWINDOW (selected_window))
2052 && WINDOWP (minibuf_selected_window)
2053 && w == XWINDOW (minibuf_selected_window))))
2054 {
2055 int charpos = marker_position (current_buffer->mark);
2056 it->region_beg_charpos = min (PT, charpos);
2057 it->region_end_charpos = max (PT, charpos);
2058 }
2059 else
2060 it->region_beg_charpos = it->region_end_charpos = -1;
2061
2062 /* Get the position at which the redisplay_end_trigger hook should
2063 be run, if it is to be run at all. */
2064 if (MARKERP (w->redisplay_end_trigger)
2065 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2066 it->redisplay_end_trigger_charpos
2067 = marker_position (w->redisplay_end_trigger);
2068 else if (INTEGERP (w->redisplay_end_trigger))
2069 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2070
2071 /* Correct bogus values of tab_width. */
2072 it->tab_width = XINT (current_buffer->tab_width);
2073 if (it->tab_width <= 0 || it->tab_width > 1000)
2074 it->tab_width = 8;
2075
2076 /* Are lines in the display truncated? */
2077 it->truncate_lines_p
2078 = (base_face_id != DEFAULT_FACE_ID
2079 || XINT (it->w->hscroll)
2080 || (truncate_partial_width_windows
2081 && !WINDOW_FULL_WIDTH_P (it->w))
2082 || !NILP (current_buffer->truncate_lines));
2083
2084 /* Get dimensions of truncation and continuation glyphs. These are
2085 displayed as fringe bitmaps under X, so we don't need them for such
2086 frames. */
2087 if (!FRAME_WINDOW_P (it->f))
2088 {
2089 if (it->truncate_lines_p)
2090 {
2091 /* We will need the truncation glyph. */
2092 xassert (it->glyph_row == NULL);
2093 produce_special_glyphs (it, IT_TRUNCATION);
2094 it->truncation_pixel_width = it->pixel_width;
2095 }
2096 else
2097 {
2098 /* We will need the continuation glyph. */
2099 xassert (it->glyph_row == NULL);
2100 produce_special_glyphs (it, IT_CONTINUATION);
2101 it->continuation_pixel_width = it->pixel_width;
2102 }
2103
2104 /* Reset these values to zero because the produce_special_glyphs
2105 above has changed them. */
2106 it->pixel_width = it->ascent = it->descent = 0;
2107 it->phys_ascent = it->phys_descent = 0;
2108 }
2109
2110 /* Set this after getting the dimensions of truncation and
2111 continuation glyphs, so that we don't produce glyphs when calling
2112 produce_special_glyphs, above. */
2113 it->glyph_row = row;
2114 it->area = TEXT_AREA;
2115
2116 /* Get the dimensions of the display area. The display area
2117 consists of the visible window area plus a horizontally scrolled
2118 part to the left of the window. All x-values are relative to the
2119 start of this total display area. */
2120 if (base_face_id != DEFAULT_FACE_ID)
2121 {
2122 /* Mode lines, menu bar in terminal frames. */
2123 it->first_visible_x = 0;
2124 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2125 }
2126 else
2127 {
2128 it->first_visible_x
2129 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2130 it->last_visible_x = (it->first_visible_x
2131 + window_box_width (w, TEXT_AREA));
2132
2133 /* If we truncate lines, leave room for the truncator glyph(s) at
2134 the right margin. Otherwise, leave room for the continuation
2135 glyph(s). Truncation and continuation glyphs are not inserted
2136 for window-based redisplay. */
2137 if (!FRAME_WINDOW_P (it->f))
2138 {
2139 if (it->truncate_lines_p)
2140 it->last_visible_x -= it->truncation_pixel_width;
2141 else
2142 it->last_visible_x -= it->continuation_pixel_width;
2143 }
2144
2145 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2146 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2147 }
2148
2149 /* Leave room for a border glyph. */
2150 if (!FRAME_WINDOW_P (it->f)
2151 && !WINDOW_RIGHTMOST_P (it->w))
2152 it->last_visible_x -= 1;
2153
2154 it->last_visible_y = window_text_bottom_y (w);
2155
2156 /* For mode lines and alike, arrange for the first glyph having a
2157 left box line if the face specifies a box. */
2158 if (base_face_id != DEFAULT_FACE_ID)
2159 {
2160 struct face *face;
2161
2162 it->face_id = base_face_id;
2163
2164 /* If we have a boxed mode line, make the first character appear
2165 with a left box line. */
2166 face = FACE_FROM_ID (it->f, base_face_id);
2167 if (face->box != FACE_NO_BOX)
2168 it->start_of_box_run_p = 1;
2169 }
2170
2171 /* If a buffer position was specified, set the iterator there,
2172 getting overlays and face properties from that position. */
2173 if (charpos >= BUF_BEG (current_buffer))
2174 {
2175 it->end_charpos = ZV;
2176 it->face_id = -1;
2177 IT_CHARPOS (*it) = charpos;
2178
2179 /* Compute byte position if not specified. */
2180 if (bytepos < charpos)
2181 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2182 else
2183 IT_BYTEPOS (*it) = bytepos;
2184
2185 /* Compute faces etc. */
2186 reseat (it, it->current.pos, 1);
2187 }
2188
2189 CHECK_IT (it);
2190 }
2191
2192
2193 /* Initialize IT for the display of window W with window start POS. */
2194
2195 void
2196 start_display (it, w, pos)
2197 struct it *it;
2198 struct window *w;
2199 struct text_pos pos;
2200 {
2201 struct glyph_row *row;
2202 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2203
2204 row = w->desired_matrix->rows + first_vpos;
2205 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2206
2207 if (!it->truncate_lines_p)
2208 {
2209 int start_at_line_beg_p;
2210 int first_y = it->current_y;
2211
2212 /* If window start is not at a line start, skip forward to POS to
2213 get the correct continuation lines width. */
2214 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2215 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2216 if (!start_at_line_beg_p)
2217 {
2218 int new_x;
2219
2220 reseat_at_previous_visible_line_start (it);
2221 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2222
2223 new_x = it->current_x + it->pixel_width;
2224
2225 /* If lines are continued, this line may end in the middle
2226 of a multi-glyph character (e.g. a control character
2227 displayed as \003, or in the middle of an overlay
2228 string). In this case move_it_to above will not have
2229 taken us to the start of the continuation line but to the
2230 end of the continued line. */
2231 if (it->current_x > 0
2232 && !it->truncate_lines_p /* Lines are continued. */
2233 && (/* And glyph doesn't fit on the line. */
2234 new_x > it->last_visible_x
2235 /* Or it fits exactly and we're on a window
2236 system frame. */
2237 || (new_x == it->last_visible_x
2238 && FRAME_WINDOW_P (it->f))))
2239 {
2240 if (it->current.dpvec_index >= 0
2241 || it->current.overlay_string_index >= 0)
2242 {
2243 set_iterator_to_next (it, 1);
2244 move_it_in_display_line_to (it, -1, -1, 0);
2245 }
2246
2247 it->continuation_lines_width += it->current_x;
2248 }
2249
2250 /* We're starting a new display line, not affected by the
2251 height of the continued line, so clear the appropriate
2252 fields in the iterator structure. */
2253 it->max_ascent = it->max_descent = 0;
2254 it->max_phys_ascent = it->max_phys_descent = 0;
2255
2256 it->current_y = first_y;
2257 it->vpos = 0;
2258 it->current_x = it->hpos = 0;
2259 }
2260 }
2261
2262 #if 0 /* Don't assert the following because start_display is sometimes
2263 called intentionally with a window start that is not at a
2264 line start. Please leave this code in as a comment. */
2265
2266 /* Window start should be on a line start, now. */
2267 xassert (it->continuation_lines_width
2268 || IT_CHARPOS (it) == BEGV
2269 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2270 #endif /* 0 */
2271 }
2272
2273
2274 /* Return 1 if POS is a position in ellipses displayed for invisible
2275 text. W is the window we display, for text property lookup. */
2276
2277 static int
2278 in_ellipses_for_invisible_text_p (pos, w)
2279 struct display_pos *pos;
2280 struct window *w;
2281 {
2282 Lisp_Object prop, window;
2283 int ellipses_p = 0;
2284 int charpos = CHARPOS (pos->pos);
2285
2286 /* If POS specifies a position in a display vector, this might
2287 be for an ellipsis displayed for invisible text. We won't
2288 get the iterator set up for delivering that ellipsis unless
2289 we make sure that it gets aware of the invisible text. */
2290 if (pos->dpvec_index >= 0
2291 && pos->overlay_string_index < 0
2292 && CHARPOS (pos->string_pos) < 0
2293 && charpos > BEGV
2294 && (XSETWINDOW (window, w),
2295 prop = Fget_char_property (make_number (charpos),
2296 Qinvisible, window),
2297 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2298 {
2299 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2300 window);
2301 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2302 }
2303
2304 return ellipses_p;
2305 }
2306
2307
2308 /* Initialize IT for stepping through current_buffer in window W,
2309 starting at position POS that includes overlay string and display
2310 vector/ control character translation position information. Value
2311 is zero if there are overlay strings with newlines at POS. */
2312
2313 static int
2314 init_from_display_pos (it, w, pos)
2315 struct it *it;
2316 struct window *w;
2317 struct display_pos *pos;
2318 {
2319 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2320 int i, overlay_strings_with_newlines = 0;
2321
2322 /* If POS specifies a position in a display vector, this might
2323 be for an ellipsis displayed for invisible text. We won't
2324 get the iterator set up for delivering that ellipsis unless
2325 we make sure that it gets aware of the invisible text. */
2326 if (in_ellipses_for_invisible_text_p (pos, w))
2327 {
2328 --charpos;
2329 bytepos = 0;
2330 }
2331
2332 /* Keep in mind: the call to reseat in init_iterator skips invisible
2333 text, so we might end up at a position different from POS. This
2334 is only a problem when POS is a row start after a newline and an
2335 overlay starts there with an after-string, and the overlay has an
2336 invisible property. Since we don't skip invisible text in
2337 display_line and elsewhere immediately after consuming the
2338 newline before the row start, such a POS will not be in a string,
2339 but the call to init_iterator below will move us to the
2340 after-string. */
2341 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2342
2343 for (i = 0; i < it->n_overlay_strings; ++i)
2344 {
2345 const char *s = SDATA (it->overlay_strings[i]);
2346 const char *e = s + SBYTES (it->overlay_strings[i]);
2347
2348 while (s < e && *s != '\n')
2349 ++s;
2350
2351 if (s < e)
2352 {
2353 overlay_strings_with_newlines = 1;
2354 break;
2355 }
2356 }
2357
2358 /* If position is within an overlay string, set up IT to the right
2359 overlay string. */
2360 if (pos->overlay_string_index >= 0)
2361 {
2362 int relative_index;
2363
2364 /* If the first overlay string happens to have a `display'
2365 property for an image, the iterator will be set up for that
2366 image, and we have to undo that setup first before we can
2367 correct the overlay string index. */
2368 if (it->method == next_element_from_image)
2369 pop_it (it);
2370
2371 /* We already have the first chunk of overlay strings in
2372 IT->overlay_strings. Load more until the one for
2373 pos->overlay_string_index is in IT->overlay_strings. */
2374 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2375 {
2376 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2377 it->current.overlay_string_index = 0;
2378 while (n--)
2379 {
2380 load_overlay_strings (it, 0);
2381 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2382 }
2383 }
2384
2385 it->current.overlay_string_index = pos->overlay_string_index;
2386 relative_index = (it->current.overlay_string_index
2387 % OVERLAY_STRING_CHUNK_SIZE);
2388 it->string = it->overlay_strings[relative_index];
2389 xassert (STRINGP (it->string));
2390 it->current.string_pos = pos->string_pos;
2391 it->method = next_element_from_string;
2392 }
2393
2394 #if 0 /* This is bogus because POS not having an overlay string
2395 position does not mean it's after the string. Example: A
2396 line starting with a before-string and initialization of IT
2397 to the previous row's end position. */
2398 else if (it->current.overlay_string_index >= 0)
2399 {
2400 /* If POS says we're already after an overlay string ending at
2401 POS, make sure to pop the iterator because it will be in
2402 front of that overlay string. When POS is ZV, we've thereby
2403 also ``processed'' overlay strings at ZV. */
2404 while (it->sp)
2405 pop_it (it);
2406 it->current.overlay_string_index = -1;
2407 it->method = next_element_from_buffer;
2408 if (CHARPOS (pos->pos) == ZV)
2409 it->overlay_strings_at_end_processed_p = 1;
2410 }
2411 #endif /* 0 */
2412
2413 if (CHARPOS (pos->string_pos) >= 0)
2414 {
2415 /* Recorded position is not in an overlay string, but in another
2416 string. This can only be a string from a `display' property.
2417 IT should already be filled with that string. */
2418 it->current.string_pos = pos->string_pos;
2419 xassert (STRINGP (it->string));
2420 }
2421
2422 /* Restore position in display vector translations, control
2423 character translations or ellipses. */
2424 if (pos->dpvec_index >= 0)
2425 {
2426 if (it->dpvec == NULL)
2427 get_next_display_element (it);
2428 xassert (it->dpvec && it->current.dpvec_index == 0);
2429 it->current.dpvec_index = pos->dpvec_index;
2430 }
2431
2432 CHECK_IT (it);
2433 return !overlay_strings_with_newlines;
2434 }
2435
2436
2437 /* Initialize IT for stepping through current_buffer in window W
2438 starting at ROW->start. */
2439
2440 static void
2441 init_to_row_start (it, w, row)
2442 struct it *it;
2443 struct window *w;
2444 struct glyph_row *row;
2445 {
2446 init_from_display_pos (it, w, &row->start);
2447 it->continuation_lines_width = row->continuation_lines_width;
2448 CHECK_IT (it);
2449 }
2450
2451
2452 /* Initialize IT for stepping through current_buffer in window W
2453 starting in the line following ROW, i.e. starting at ROW->end.
2454 Value is zero if there are overlay strings with newlines at ROW's
2455 end position. */
2456
2457 static int
2458 init_to_row_end (it, w, row)
2459 struct it *it;
2460 struct window *w;
2461 struct glyph_row *row;
2462 {
2463 int success = 0;
2464
2465 if (init_from_display_pos (it, w, &row->end))
2466 {
2467 if (row->continued_p)
2468 it->continuation_lines_width
2469 = row->continuation_lines_width + row->pixel_width;
2470 CHECK_IT (it);
2471 success = 1;
2472 }
2473
2474 return success;
2475 }
2476
2477
2478
2479 \f
2480 /***********************************************************************
2481 Text properties
2482 ***********************************************************************/
2483
2484 /* Called when IT reaches IT->stop_charpos. Handle text property and
2485 overlay changes. Set IT->stop_charpos to the next position where
2486 to stop. */
2487
2488 static void
2489 handle_stop (it)
2490 struct it *it;
2491 {
2492 enum prop_handled handled;
2493 int handle_overlay_change_p = 1;
2494 struct props *p;
2495
2496 it->dpvec = NULL;
2497 it->current.dpvec_index = -1;
2498
2499 do
2500 {
2501 handled = HANDLED_NORMALLY;
2502
2503 /* Call text property handlers. */
2504 for (p = it_props; p->handler; ++p)
2505 {
2506 handled = p->handler (it);
2507
2508 if (handled == HANDLED_RECOMPUTE_PROPS)
2509 break;
2510 else if (handled == HANDLED_RETURN)
2511 return;
2512 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2513 handle_overlay_change_p = 0;
2514 }
2515
2516 if (handled != HANDLED_RECOMPUTE_PROPS)
2517 {
2518 /* Don't check for overlay strings below when set to deliver
2519 characters from a display vector. */
2520 if (it->method == next_element_from_display_vector)
2521 handle_overlay_change_p = 0;
2522
2523 /* Handle overlay changes. */
2524 if (handle_overlay_change_p)
2525 handled = handle_overlay_change (it);
2526
2527 /* Determine where to stop next. */
2528 if (handled == HANDLED_NORMALLY)
2529 compute_stop_pos (it);
2530 }
2531 }
2532 while (handled == HANDLED_RECOMPUTE_PROPS);
2533 }
2534
2535
2536 /* Compute IT->stop_charpos from text property and overlay change
2537 information for IT's current position. */
2538
2539 static void
2540 compute_stop_pos (it)
2541 struct it *it;
2542 {
2543 register INTERVAL iv, next_iv;
2544 Lisp_Object object, limit, position;
2545
2546 /* If nowhere else, stop at the end. */
2547 it->stop_charpos = it->end_charpos;
2548
2549 if (STRINGP (it->string))
2550 {
2551 /* Strings are usually short, so don't limit the search for
2552 properties. */
2553 object = it->string;
2554 limit = Qnil;
2555 position = make_number (IT_STRING_CHARPOS (*it));
2556 }
2557 else
2558 {
2559 int charpos;
2560
2561 /* If next overlay change is in front of the current stop pos
2562 (which is IT->end_charpos), stop there. Note: value of
2563 next_overlay_change is point-max if no overlay change
2564 follows. */
2565 charpos = next_overlay_change (IT_CHARPOS (*it));
2566 if (charpos < it->stop_charpos)
2567 it->stop_charpos = charpos;
2568
2569 /* If showing the region, we have to stop at the region
2570 start or end because the face might change there. */
2571 if (it->region_beg_charpos > 0)
2572 {
2573 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2574 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2575 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2576 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2577 }
2578
2579 /* Set up variables for computing the stop position from text
2580 property changes. */
2581 XSETBUFFER (object, current_buffer);
2582 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2583 position = make_number (IT_CHARPOS (*it));
2584
2585 }
2586
2587 /* Get the interval containing IT's position. Value is a null
2588 interval if there isn't such an interval. */
2589 iv = validate_interval_range (object, &position, &position, 0);
2590 if (!NULL_INTERVAL_P (iv))
2591 {
2592 Lisp_Object values_here[LAST_PROP_IDX];
2593 struct props *p;
2594
2595 /* Get properties here. */
2596 for (p = it_props; p->handler; ++p)
2597 values_here[p->idx] = textget (iv->plist, *p->name);
2598
2599 /* Look for an interval following iv that has different
2600 properties. */
2601 for (next_iv = next_interval (iv);
2602 (!NULL_INTERVAL_P (next_iv)
2603 && (NILP (limit)
2604 || XFASTINT (limit) > next_iv->position));
2605 next_iv = next_interval (next_iv))
2606 {
2607 for (p = it_props; p->handler; ++p)
2608 {
2609 Lisp_Object new_value;
2610
2611 new_value = textget (next_iv->plist, *p->name);
2612 if (!EQ (values_here[p->idx], new_value))
2613 break;
2614 }
2615
2616 if (p->handler)
2617 break;
2618 }
2619
2620 if (!NULL_INTERVAL_P (next_iv))
2621 {
2622 if (INTEGERP (limit)
2623 && next_iv->position >= XFASTINT (limit))
2624 /* No text property change up to limit. */
2625 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2626 else
2627 /* Text properties change in next_iv. */
2628 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2629 }
2630 }
2631
2632 xassert (STRINGP (it->string)
2633 || (it->stop_charpos >= BEGV
2634 && it->stop_charpos >= IT_CHARPOS (*it)));
2635 }
2636
2637
2638 /* Return the position of the next overlay change after POS in
2639 current_buffer. Value is point-max if no overlay change
2640 follows. This is like `next-overlay-change' but doesn't use
2641 xmalloc. */
2642
2643 static int
2644 next_overlay_change (pos)
2645 int pos;
2646 {
2647 int noverlays;
2648 int endpos;
2649 Lisp_Object *overlays;
2650 int len;
2651 int i;
2652
2653 /* Get all overlays at the given position. */
2654 len = 10;
2655 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2656 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2657 if (noverlays > len)
2658 {
2659 len = noverlays;
2660 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2661 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2662 }
2663
2664 /* If any of these overlays ends before endpos,
2665 use its ending point instead. */
2666 for (i = 0; i < noverlays; ++i)
2667 {
2668 Lisp_Object oend;
2669 int oendpos;
2670
2671 oend = OVERLAY_END (overlays[i]);
2672 oendpos = OVERLAY_POSITION (oend);
2673 endpos = min (endpos, oendpos);
2674 }
2675
2676 return endpos;
2677 }
2678
2679
2680 \f
2681 /***********************************************************************
2682 Fontification
2683 ***********************************************************************/
2684
2685 /* Handle changes in the `fontified' property of the current buffer by
2686 calling hook functions from Qfontification_functions to fontify
2687 regions of text. */
2688
2689 static enum prop_handled
2690 handle_fontified_prop (it)
2691 struct it *it;
2692 {
2693 Lisp_Object prop, pos;
2694 enum prop_handled handled = HANDLED_NORMALLY;
2695
2696 /* Get the value of the `fontified' property at IT's current buffer
2697 position. (The `fontified' property doesn't have a special
2698 meaning in strings.) If the value is nil, call functions from
2699 Qfontification_functions. */
2700 if (!STRINGP (it->string)
2701 && it->s == NULL
2702 && !NILP (Vfontification_functions)
2703 && !NILP (Vrun_hooks)
2704 && (pos = make_number (IT_CHARPOS (*it)),
2705 prop = Fget_char_property (pos, Qfontified, Qnil),
2706 NILP (prop)))
2707 {
2708 int count = SPECPDL_INDEX ();
2709 Lisp_Object val;
2710
2711 val = Vfontification_functions;
2712 specbind (Qfontification_functions, Qnil);
2713
2714 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2715 safe_call1 (val, pos);
2716 else
2717 {
2718 Lisp_Object globals, fn;
2719 struct gcpro gcpro1, gcpro2;
2720
2721 globals = Qnil;
2722 GCPRO2 (val, globals);
2723
2724 for (; CONSP (val); val = XCDR (val))
2725 {
2726 fn = XCAR (val);
2727
2728 if (EQ (fn, Qt))
2729 {
2730 /* A value of t indicates this hook has a local
2731 binding; it means to run the global binding too.
2732 In a global value, t should not occur. If it
2733 does, we must ignore it to avoid an endless
2734 loop. */
2735 for (globals = Fdefault_value (Qfontification_functions);
2736 CONSP (globals);
2737 globals = XCDR (globals))
2738 {
2739 fn = XCAR (globals);
2740 if (!EQ (fn, Qt))
2741 safe_call1 (fn, pos);
2742 }
2743 }
2744 else
2745 safe_call1 (fn, pos);
2746 }
2747
2748 UNGCPRO;
2749 }
2750
2751 unbind_to (count, Qnil);
2752
2753 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2754 something. This avoids an endless loop if they failed to
2755 fontify the text for which reason ever. */
2756 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2757 handled = HANDLED_RECOMPUTE_PROPS;
2758 }
2759
2760 return handled;
2761 }
2762
2763
2764 \f
2765 /***********************************************************************
2766 Faces
2767 ***********************************************************************/
2768
2769 /* Set up iterator IT from face properties at its current position.
2770 Called from handle_stop. */
2771
2772 static enum prop_handled
2773 handle_face_prop (it)
2774 struct it *it;
2775 {
2776 int new_face_id, next_stop;
2777
2778 if (!STRINGP (it->string))
2779 {
2780 new_face_id
2781 = face_at_buffer_position (it->w,
2782 IT_CHARPOS (*it),
2783 it->region_beg_charpos,
2784 it->region_end_charpos,
2785 &next_stop,
2786 (IT_CHARPOS (*it)
2787 + TEXT_PROP_DISTANCE_LIMIT),
2788 0);
2789
2790 /* Is this a start of a run of characters with box face?
2791 Caveat: this can be called for a freshly initialized
2792 iterator; face_id is -1 in this case. We know that the new
2793 face will not change until limit, i.e. if the new face has a
2794 box, all characters up to limit will have one. But, as
2795 usual, we don't know whether limit is really the end. */
2796 if (new_face_id != it->face_id)
2797 {
2798 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2799
2800 /* If new face has a box but old face has not, this is
2801 the start of a run of characters with box, i.e. it has
2802 a shadow on the left side. The value of face_id of the
2803 iterator will be -1 if this is the initial call that gets
2804 the face. In this case, we have to look in front of IT's
2805 position and see whether there is a face != new_face_id. */
2806 it->start_of_box_run_p
2807 = (new_face->box != FACE_NO_BOX
2808 && (it->face_id >= 0
2809 || IT_CHARPOS (*it) == BEG
2810 || new_face_id != face_before_it_pos (it)));
2811 it->face_box_p = new_face->box != FACE_NO_BOX;
2812 }
2813 }
2814 else
2815 {
2816 int base_face_id, bufpos;
2817
2818 if (it->current.overlay_string_index >= 0)
2819 bufpos = IT_CHARPOS (*it);
2820 else
2821 bufpos = 0;
2822
2823 /* For strings from a buffer, i.e. overlay strings or strings
2824 from a `display' property, use the face at IT's current
2825 buffer position as the base face to merge with, so that
2826 overlay strings appear in the same face as surrounding
2827 text, unless they specify their own faces. */
2828 base_face_id = underlying_face_id (it);
2829
2830 new_face_id = face_at_string_position (it->w,
2831 it->string,
2832 IT_STRING_CHARPOS (*it),
2833 bufpos,
2834 it->region_beg_charpos,
2835 it->region_end_charpos,
2836 &next_stop,
2837 base_face_id, 0);
2838
2839 #if 0 /* This shouldn't be neccessary. Let's check it. */
2840 /* If IT is used to display a mode line we would really like to
2841 use the mode line face instead of the frame's default face. */
2842 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2843 && new_face_id == DEFAULT_FACE_ID)
2844 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2845 #endif
2846
2847 /* Is this a start of a run of characters with box? Caveat:
2848 this can be called for a freshly allocated iterator; face_id
2849 is -1 is this case. We know that the new face will not
2850 change until the next check pos, i.e. if the new face has a
2851 box, all characters up to that position will have a
2852 box. But, as usual, we don't know whether that position
2853 is really the end. */
2854 if (new_face_id != it->face_id)
2855 {
2856 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2857 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2858
2859 /* If new face has a box but old face hasn't, this is the
2860 start of a run of characters with box, i.e. it has a
2861 shadow on the left side. */
2862 it->start_of_box_run_p
2863 = new_face->box && (old_face == NULL || !old_face->box);
2864 it->face_box_p = new_face->box != FACE_NO_BOX;
2865 }
2866 }
2867
2868 it->face_id = new_face_id;
2869 return HANDLED_NORMALLY;
2870 }
2871
2872
2873 /* Return the ID of the face ``underlying'' IT's current position,
2874 which is in a string. If the iterator is associated with a
2875 buffer, return the face at IT's current buffer position.
2876 Otherwise, use the iterator's base_face_id. */
2877
2878 static int
2879 underlying_face_id (it)
2880 struct it *it;
2881 {
2882 int face_id = it->base_face_id, i;
2883
2884 xassert (STRINGP (it->string));
2885
2886 for (i = it->sp - 1; i >= 0; --i)
2887 if (NILP (it->stack[i].string))
2888 face_id = it->stack[i].face_id;
2889
2890 return face_id;
2891 }
2892
2893
2894 /* Compute the face one character before or after the current position
2895 of IT. BEFORE_P non-zero means get the face in front of IT's
2896 position. Value is the id of the face. */
2897
2898 static int
2899 face_before_or_after_it_pos (it, before_p)
2900 struct it *it;
2901 int before_p;
2902 {
2903 int face_id, limit;
2904 int next_check_charpos;
2905 struct text_pos pos;
2906
2907 xassert (it->s == NULL);
2908
2909 if (STRINGP (it->string))
2910 {
2911 int bufpos, base_face_id;
2912
2913 /* No face change past the end of the string (for the case
2914 we are padding with spaces). No face change before the
2915 string start. */
2916 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
2917 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2918 return it->face_id;
2919
2920 /* Set pos to the position before or after IT's current position. */
2921 if (before_p)
2922 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2923 else
2924 /* For composition, we must check the character after the
2925 composition. */
2926 pos = (it->what == IT_COMPOSITION
2927 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2928 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2929
2930 if (it->current.overlay_string_index >= 0)
2931 bufpos = IT_CHARPOS (*it);
2932 else
2933 bufpos = 0;
2934
2935 base_face_id = underlying_face_id (it);
2936
2937 /* Get the face for ASCII, or unibyte. */
2938 face_id = face_at_string_position (it->w,
2939 it->string,
2940 CHARPOS (pos),
2941 bufpos,
2942 it->region_beg_charpos,
2943 it->region_end_charpos,
2944 &next_check_charpos,
2945 base_face_id, 0);
2946
2947 /* Correct the face for charsets different from ASCII. Do it
2948 for the multibyte case only. The face returned above is
2949 suitable for unibyte text if IT->string is unibyte. */
2950 if (STRING_MULTIBYTE (it->string))
2951 {
2952 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
2953 int rest = SBYTES (it->string) - BYTEPOS (pos);
2954 int c, len;
2955 struct face *face = FACE_FROM_ID (it->f, face_id);
2956
2957 c = string_char_and_length (p, rest, &len);
2958 face_id = FACE_FOR_CHAR (it->f, face, c);
2959 }
2960 }
2961 else
2962 {
2963 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2964 || (IT_CHARPOS (*it) <= BEGV && before_p))
2965 return it->face_id;
2966
2967 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2968 pos = it->current.pos;
2969
2970 if (before_p)
2971 DEC_TEXT_POS (pos, it->multibyte_p);
2972 else
2973 {
2974 if (it->what == IT_COMPOSITION)
2975 /* For composition, we must check the position after the
2976 composition. */
2977 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2978 else
2979 INC_TEXT_POS (pos, it->multibyte_p);
2980 }
2981
2982 /* Determine face for CHARSET_ASCII, or unibyte. */
2983 face_id = face_at_buffer_position (it->w,
2984 CHARPOS (pos),
2985 it->region_beg_charpos,
2986 it->region_end_charpos,
2987 &next_check_charpos,
2988 limit, 0);
2989
2990 /* Correct the face for charsets different from ASCII. Do it
2991 for the multibyte case only. The face returned above is
2992 suitable for unibyte text if current_buffer is unibyte. */
2993 if (it->multibyte_p)
2994 {
2995 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
2996 struct face *face = FACE_FROM_ID (it->f, face_id);
2997 face_id = FACE_FOR_CHAR (it->f, face, c);
2998 }
2999 }
3000
3001 return face_id;
3002 }
3003
3004
3005 \f
3006 /***********************************************************************
3007 Invisible text
3008 ***********************************************************************/
3009
3010 /* Set up iterator IT from invisible properties at its current
3011 position. Called from handle_stop. */
3012
3013 static enum prop_handled
3014 handle_invisible_prop (it)
3015 struct it *it;
3016 {
3017 enum prop_handled handled = HANDLED_NORMALLY;
3018
3019 if (STRINGP (it->string))
3020 {
3021 extern Lisp_Object Qinvisible;
3022 Lisp_Object prop, end_charpos, limit, charpos;
3023
3024 /* Get the value of the invisible text property at the
3025 current position. Value will be nil if there is no such
3026 property. */
3027 charpos = make_number (IT_STRING_CHARPOS (*it));
3028 prop = Fget_text_property (charpos, Qinvisible, it->string);
3029
3030 if (!NILP (prop)
3031 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3032 {
3033 handled = HANDLED_RECOMPUTE_PROPS;
3034
3035 /* Get the position at which the next change of the
3036 invisible text property can be found in IT->string.
3037 Value will be nil if the property value is the same for
3038 all the rest of IT->string. */
3039 XSETINT (limit, SCHARS (it->string));
3040 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3041 it->string, limit);
3042
3043 /* Text at current position is invisible. The next
3044 change in the property is at position end_charpos.
3045 Move IT's current position to that position. */
3046 if (INTEGERP (end_charpos)
3047 && XFASTINT (end_charpos) < XFASTINT (limit))
3048 {
3049 struct text_pos old;
3050 old = it->current.string_pos;
3051 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3052 compute_string_pos (&it->current.string_pos, old, it->string);
3053 }
3054 else
3055 {
3056 /* The rest of the string is invisible. If this is an
3057 overlay string, proceed with the next overlay string
3058 or whatever comes and return a character from there. */
3059 if (it->current.overlay_string_index >= 0)
3060 {
3061 next_overlay_string (it);
3062 /* Don't check for overlay strings when we just
3063 finished processing them. */
3064 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3065 }
3066 else
3067 {
3068 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3069 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3070 }
3071 }
3072 }
3073 }
3074 else
3075 {
3076 int invis_p, newpos, next_stop, start_charpos;
3077 Lisp_Object pos, prop, overlay;
3078
3079 /* First of all, is there invisible text at this position? */
3080 start_charpos = IT_CHARPOS (*it);
3081 pos = make_number (IT_CHARPOS (*it));
3082 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3083 &overlay);
3084 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3085
3086 /* If we are on invisible text, skip over it. */
3087 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3088 {
3089 /* Record whether we have to display an ellipsis for the
3090 invisible text. */
3091 int display_ellipsis_p = invis_p == 2;
3092
3093 handled = HANDLED_RECOMPUTE_PROPS;
3094
3095 /* Loop skipping over invisible text. The loop is left at
3096 ZV or with IT on the first char being visible again. */
3097 do
3098 {
3099 /* Try to skip some invisible text. Return value is the
3100 position reached which can be equal to IT's position
3101 if there is nothing invisible here. This skips both
3102 over invisible text properties and overlays with
3103 invisible property. */
3104 newpos = skip_invisible (IT_CHARPOS (*it),
3105 &next_stop, ZV, it->window);
3106
3107 /* If we skipped nothing at all we weren't at invisible
3108 text in the first place. If everything to the end of
3109 the buffer was skipped, end the loop. */
3110 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3111 invis_p = 0;
3112 else
3113 {
3114 /* We skipped some characters but not necessarily
3115 all there are. Check if we ended up on visible
3116 text. Fget_char_property returns the property of
3117 the char before the given position, i.e. if we
3118 get invis_p = 0, this means that the char at
3119 newpos is visible. */
3120 pos = make_number (newpos);
3121 prop = Fget_char_property (pos, Qinvisible, it->window);
3122 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3123 }
3124
3125 /* If we ended up on invisible text, proceed to
3126 skip starting with next_stop. */
3127 if (invis_p)
3128 IT_CHARPOS (*it) = next_stop;
3129 }
3130 while (invis_p);
3131
3132 /* The position newpos is now either ZV or on visible text. */
3133 IT_CHARPOS (*it) = newpos;
3134 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3135
3136 /* If there are before-strings at the start of invisible
3137 text, and the text is invisible because of a text
3138 property, arrange to show before-strings because 20.x did
3139 it that way. (If the text is invisible because of an
3140 overlay property instead of a text property, this is
3141 already handled in the overlay code.) */
3142 if (NILP (overlay)
3143 && get_overlay_strings (it, start_charpos))
3144 {
3145 handled = HANDLED_RECOMPUTE_PROPS;
3146 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3147 }
3148 else if (display_ellipsis_p)
3149 setup_for_ellipsis (it);
3150 }
3151 }
3152
3153 return handled;
3154 }
3155
3156
3157 /* Make iterator IT return `...' next. */
3158
3159 static void
3160 setup_for_ellipsis (it)
3161 struct it *it;
3162 {
3163 if (it->dp
3164 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3165 {
3166 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3167 it->dpvec = v->contents;
3168 it->dpend = v->contents + v->size;
3169 }
3170 else
3171 {
3172 /* Default `...'. */
3173 it->dpvec = default_invis_vector;
3174 it->dpend = default_invis_vector + 3;
3175 }
3176
3177 /* The ellipsis display does not replace the display of the
3178 character at the new position. Indicate this by setting
3179 IT->dpvec_char_len to zero. */
3180 it->dpvec_char_len = 0;
3181
3182 it->current.dpvec_index = 0;
3183 it->method = next_element_from_display_vector;
3184 }
3185
3186
3187 \f
3188 /***********************************************************************
3189 'display' property
3190 ***********************************************************************/
3191
3192 /* Set up iterator IT from `display' property at its current position.
3193 Called from handle_stop. */
3194
3195 static enum prop_handled
3196 handle_display_prop (it)
3197 struct it *it;
3198 {
3199 Lisp_Object prop, object;
3200 struct text_pos *position;
3201 int display_replaced_p = 0;
3202
3203 if (STRINGP (it->string))
3204 {
3205 object = it->string;
3206 position = &it->current.string_pos;
3207 }
3208 else
3209 {
3210 object = it->w->buffer;
3211 position = &it->current.pos;
3212 }
3213
3214 /* Reset those iterator values set from display property values. */
3215 it->font_height = Qnil;
3216 it->space_width = Qnil;
3217 it->voffset = 0;
3218
3219 /* We don't support recursive `display' properties, i.e. string
3220 values that have a string `display' property, that have a string
3221 `display' property etc. */
3222 if (!it->string_from_display_prop_p)
3223 it->area = TEXT_AREA;
3224
3225 prop = Fget_char_property (make_number (position->charpos),
3226 Qdisplay, object);
3227 if (NILP (prop))
3228 return HANDLED_NORMALLY;
3229
3230 if (CONSP (prop)
3231 /* Simple properties. */
3232 && !EQ (XCAR (prop), Qimage)
3233 && !EQ (XCAR (prop), Qspace)
3234 && !EQ (XCAR (prop), Qwhen)
3235 && !EQ (XCAR (prop), Qspace_width)
3236 && !EQ (XCAR (prop), Qheight)
3237 && !EQ (XCAR (prop), Qraise)
3238 /* Marginal area specifications. */
3239 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3240 && !NILP (XCAR (prop)))
3241 {
3242 for (; CONSP (prop); prop = XCDR (prop))
3243 {
3244 if (handle_single_display_prop (it, XCAR (prop), object,
3245 position, display_replaced_p))
3246 display_replaced_p = 1;
3247 }
3248 }
3249 else if (VECTORP (prop))
3250 {
3251 int i;
3252 for (i = 0; i < ASIZE (prop); ++i)
3253 if (handle_single_display_prop (it, AREF (prop, i), object,
3254 position, display_replaced_p))
3255 display_replaced_p = 1;
3256 }
3257 else
3258 {
3259 if (handle_single_display_prop (it, prop, object, position, 0))
3260 display_replaced_p = 1;
3261 }
3262
3263 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3264 }
3265
3266
3267 /* Value is the position of the end of the `display' property starting
3268 at START_POS in OBJECT. */
3269
3270 static struct text_pos
3271 display_prop_end (it, object, start_pos)
3272 struct it *it;
3273 Lisp_Object object;
3274 struct text_pos start_pos;
3275 {
3276 Lisp_Object end;
3277 struct text_pos end_pos;
3278
3279 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3280 Qdisplay, object, Qnil);
3281 CHARPOS (end_pos) = XFASTINT (end);
3282 if (STRINGP (object))
3283 compute_string_pos (&end_pos, start_pos, it->string);
3284 else
3285 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3286
3287 return end_pos;
3288 }
3289
3290
3291 /* Set up IT from a single `display' sub-property value PROP. OBJECT
3292 is the object in which the `display' property was found. *POSITION
3293 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3294 means that we previously saw a display sub-property which already
3295 replaced text display with something else, for example an image;
3296 ignore such properties after the first one has been processed.
3297
3298 If PROP is a `space' or `image' sub-property, set *POSITION to the
3299 end position of the `display' property.
3300
3301 Value is non-zero if something was found which replaces the display
3302 of buffer or string text. */
3303
3304 static int
3305 handle_single_display_prop (it, prop, object, position,
3306 display_replaced_before_p)
3307 struct it *it;
3308 Lisp_Object prop;
3309 Lisp_Object object;
3310 struct text_pos *position;
3311 int display_replaced_before_p;
3312 {
3313 Lisp_Object value;
3314 int replaces_text_display_p = 0;
3315 Lisp_Object form;
3316
3317 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
3318 evaluated. If the result is nil, VALUE is ignored. */
3319 form = Qt;
3320 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3321 {
3322 prop = XCDR (prop);
3323 if (!CONSP (prop))
3324 return 0;
3325 form = XCAR (prop);
3326 prop = XCDR (prop);
3327 }
3328
3329 if (!NILP (form) && !EQ (form, Qt))
3330 {
3331 int count = SPECPDL_INDEX ();
3332 struct gcpro gcpro1;
3333
3334 /* Bind `object' to the object having the `display' property, a
3335 buffer or string. Bind `position' to the position in the
3336 object where the property was found, and `buffer-position'
3337 to the current position in the buffer. */
3338 specbind (Qobject, object);
3339 specbind (Qposition, make_number (CHARPOS (*position)));
3340 specbind (Qbuffer_position,
3341 make_number (STRINGP (object)
3342 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3343 GCPRO1 (form);
3344 form = safe_eval (form);
3345 UNGCPRO;
3346 unbind_to (count, Qnil);
3347 }
3348
3349 if (NILP (form))
3350 return 0;
3351
3352 if (CONSP (prop)
3353 && EQ (XCAR (prop), Qheight)
3354 && CONSP (XCDR (prop)))
3355 {
3356 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3357 return 0;
3358
3359 /* `(height HEIGHT)'. */
3360 it->font_height = XCAR (XCDR (prop));
3361 if (!NILP (it->font_height))
3362 {
3363 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3364 int new_height = -1;
3365
3366 if (CONSP (it->font_height)
3367 && (EQ (XCAR (it->font_height), Qplus)
3368 || EQ (XCAR (it->font_height), Qminus))
3369 && CONSP (XCDR (it->font_height))
3370 && INTEGERP (XCAR (XCDR (it->font_height))))
3371 {
3372 /* `(+ N)' or `(- N)' where N is an integer. */
3373 int steps = XINT (XCAR (XCDR (it->font_height)));
3374 if (EQ (XCAR (it->font_height), Qplus))
3375 steps = - steps;
3376 it->face_id = smaller_face (it->f, it->face_id, steps);
3377 }
3378 else if (FUNCTIONP (it->font_height))
3379 {
3380 /* Call function with current height as argument.
3381 Value is the new height. */
3382 Lisp_Object height;
3383 height = safe_call1 (it->font_height,
3384 face->lface[LFACE_HEIGHT_INDEX]);
3385 if (NUMBERP (height))
3386 new_height = XFLOATINT (height);
3387 }
3388 else if (NUMBERP (it->font_height))
3389 {
3390 /* Value is a multiple of the canonical char height. */
3391 struct face *face;
3392
3393 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3394 new_height = (XFLOATINT (it->font_height)
3395 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3396 }
3397 else
3398 {
3399 /* Evaluate IT->font_height with `height' bound to the
3400 current specified height to get the new height. */
3401 Lisp_Object value;
3402 int count = SPECPDL_INDEX ();
3403
3404 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3405 value = safe_eval (it->font_height);
3406 unbind_to (count, Qnil);
3407
3408 if (NUMBERP (value))
3409 new_height = XFLOATINT (value);
3410 }
3411
3412 if (new_height > 0)
3413 it->face_id = face_with_height (it->f, it->face_id, new_height);
3414 }
3415 }
3416 else if (CONSP (prop)
3417 && EQ (XCAR (prop), Qspace_width)
3418 && CONSP (XCDR (prop)))
3419 {
3420 /* `(space_width WIDTH)'. */
3421 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3422 return 0;
3423
3424 value = XCAR (XCDR (prop));
3425 if (NUMBERP (value) && XFLOATINT (value) > 0)
3426 it->space_width = value;
3427 }
3428 else if (CONSP (prop)
3429 && EQ (XCAR (prop), Qraise)
3430 && CONSP (XCDR (prop)))
3431 {
3432 /* `(raise FACTOR)'. */
3433 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3434 return 0;
3435
3436 #ifdef HAVE_WINDOW_SYSTEM
3437 value = XCAR (XCDR (prop));
3438 if (NUMBERP (value))
3439 {
3440 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3441 it->voffset = - (XFLOATINT (value)
3442 * (FONT_HEIGHT (face->font)));
3443 }
3444 #endif /* HAVE_WINDOW_SYSTEM */
3445 }
3446 else if (!it->string_from_display_prop_p)
3447 {
3448 /* `((margin left-margin) VALUE)' or `((margin right-margin)
3449 VALUE) or `((margin nil) VALUE)' or VALUE. */
3450 Lisp_Object location, value;
3451 struct text_pos start_pos;
3452 int valid_p;
3453
3454 /* Characters having this form of property are not displayed, so
3455 we have to find the end of the property. */
3456 start_pos = *position;
3457 *position = display_prop_end (it, object, start_pos);
3458 value = Qnil;
3459
3460 /* Let's stop at the new position and assume that all
3461 text properties change there. */
3462 it->stop_charpos = position->charpos;
3463
3464 location = Qunbound;
3465 if (CONSP (prop) && CONSP (XCAR (prop)))
3466 {
3467 Lisp_Object tem;
3468
3469 value = XCDR (prop);
3470 if (CONSP (value))
3471 value = XCAR (value);
3472
3473 tem = XCAR (prop);
3474 if (EQ (XCAR (tem), Qmargin)
3475 && (tem = XCDR (tem),
3476 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3477 (NILP (tem)
3478 || EQ (tem, Qleft_margin)
3479 || EQ (tem, Qright_margin))))
3480 location = tem;
3481 }
3482
3483 if (EQ (location, Qunbound))
3484 {
3485 location = Qnil;
3486 value = prop;
3487 }
3488
3489 #ifdef HAVE_WINDOW_SYSTEM
3490 if (FRAME_TERMCAP_P (it->f))
3491 valid_p = STRINGP (value);
3492 else
3493 valid_p = (STRINGP (value)
3494 || (CONSP (value) && EQ (XCAR (value), Qspace))
3495 || valid_image_p (value));
3496 #else /* not HAVE_WINDOW_SYSTEM */
3497 valid_p = STRINGP (value);
3498 #endif /* not HAVE_WINDOW_SYSTEM */
3499
3500 if ((EQ (location, Qleft_margin)
3501 || EQ (location, Qright_margin)
3502 || NILP (location))
3503 && valid_p
3504 && !display_replaced_before_p)
3505 {
3506 replaces_text_display_p = 1;
3507
3508 /* Save current settings of IT so that we can restore them
3509 when we are finished with the glyph property value. */
3510 push_it (it);
3511
3512 if (NILP (location))
3513 it->area = TEXT_AREA;
3514 else if (EQ (location, Qleft_margin))
3515 it->area = LEFT_MARGIN_AREA;
3516 else
3517 it->area = RIGHT_MARGIN_AREA;
3518
3519 if (STRINGP (value))
3520 {
3521 it->string = value;
3522 it->multibyte_p = STRING_MULTIBYTE (it->string);
3523 it->current.overlay_string_index = -1;
3524 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3525 it->end_charpos = it->string_nchars = SCHARS (it->string);
3526 it->method = next_element_from_string;
3527 it->stop_charpos = 0;
3528 it->string_from_display_prop_p = 1;
3529 /* Say that we haven't consumed the characters with
3530 `display' property yet. The call to pop_it in
3531 set_iterator_to_next will clean this up. */
3532 *position = start_pos;
3533 }
3534 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3535 {
3536 it->method = next_element_from_stretch;
3537 it->object = value;
3538 it->current.pos = it->position = start_pos;
3539 }
3540 #ifdef HAVE_WINDOW_SYSTEM
3541 else
3542 {
3543 it->what = IT_IMAGE;
3544 it->image_id = lookup_image (it->f, value);
3545 it->position = start_pos;
3546 it->object = NILP (object) ? it->w->buffer : object;
3547 it->method = next_element_from_image;
3548
3549 /* Say that we haven't consumed the characters with
3550 `display' property yet. The call to pop_it in
3551 set_iterator_to_next will clean this up. */
3552 *position = start_pos;
3553 }
3554 #endif /* HAVE_WINDOW_SYSTEM */
3555 }
3556 else
3557 /* Invalid property or property not supported. Restore
3558 the position to what it was before. */
3559 *position = start_pos;
3560 }
3561
3562 return replaces_text_display_p;
3563 }
3564
3565
3566 /* Check if PROP is a display sub-property value whose text should be
3567 treated as intangible. */
3568
3569 static int
3570 single_display_prop_intangible_p (prop)
3571 Lisp_Object prop;
3572 {
3573 /* Skip over `when FORM'. */
3574 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3575 {
3576 prop = XCDR (prop);
3577 if (!CONSP (prop))
3578 return 0;
3579 prop = XCDR (prop);
3580 }
3581
3582 if (STRINGP (prop))
3583 return 1;
3584
3585 if (!CONSP (prop))
3586 return 0;
3587
3588 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3589 we don't need to treat text as intangible. */
3590 if (EQ (XCAR (prop), Qmargin))
3591 {
3592 prop = XCDR (prop);
3593 if (!CONSP (prop))
3594 return 0;
3595
3596 prop = XCDR (prop);
3597 if (!CONSP (prop)
3598 || EQ (XCAR (prop), Qleft_margin)
3599 || EQ (XCAR (prop), Qright_margin))
3600 return 0;
3601 }
3602
3603 return (CONSP (prop)
3604 && (EQ (XCAR (prop), Qimage)
3605 || EQ (XCAR (prop), Qspace)));
3606 }
3607
3608
3609 /* Check if PROP is a display property value whose text should be
3610 treated as intangible. */
3611
3612 int
3613 display_prop_intangible_p (prop)
3614 Lisp_Object prop;
3615 {
3616 if (CONSP (prop)
3617 && CONSP (XCAR (prop))
3618 && !EQ (Qmargin, XCAR (XCAR (prop))))
3619 {
3620 /* A list of sub-properties. */
3621 while (CONSP (prop))
3622 {
3623 if (single_display_prop_intangible_p (XCAR (prop)))
3624 return 1;
3625 prop = XCDR (prop);
3626 }
3627 }
3628 else if (VECTORP (prop))
3629 {
3630 /* A vector of sub-properties. */
3631 int i;
3632 for (i = 0; i < ASIZE (prop); ++i)
3633 if (single_display_prop_intangible_p (AREF (prop, i)))
3634 return 1;
3635 }
3636 else
3637 return single_display_prop_intangible_p (prop);
3638
3639 return 0;
3640 }
3641
3642
3643 /* Return 1 if PROP is a display sub-property value containing STRING. */
3644
3645 static int
3646 single_display_prop_string_p (prop, string)
3647 Lisp_Object prop, string;
3648 {
3649 if (EQ (string, prop))
3650 return 1;
3651
3652 /* Skip over `when FORM'. */
3653 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3654 {
3655 prop = XCDR (prop);
3656 if (!CONSP (prop))
3657 return 0;
3658 prop = XCDR (prop);
3659 }
3660
3661 if (CONSP (prop))
3662 /* Skip over `margin LOCATION'. */
3663 if (EQ (XCAR (prop), Qmargin))
3664 {
3665 prop = XCDR (prop);
3666 if (!CONSP (prop))
3667 return 0;
3668
3669 prop = XCDR (prop);
3670 if (!CONSP (prop))
3671 return 0;
3672 }
3673
3674 return CONSP (prop) && EQ (XCAR (prop), string);
3675 }
3676
3677
3678 /* Return 1 if STRING appears in the `display' property PROP. */
3679
3680 static int
3681 display_prop_string_p (prop, string)
3682 Lisp_Object prop, string;
3683 {
3684 if (CONSP (prop)
3685 && CONSP (XCAR (prop))
3686 && !EQ (Qmargin, XCAR (XCAR (prop))))
3687 {
3688 /* A list of sub-properties. */
3689 while (CONSP (prop))
3690 {
3691 if (single_display_prop_string_p (XCAR (prop), string))
3692 return 1;
3693 prop = XCDR (prop);
3694 }
3695 }
3696 else if (VECTORP (prop))
3697 {
3698 /* A vector of sub-properties. */
3699 int i;
3700 for (i = 0; i < ASIZE (prop); ++i)
3701 if (single_display_prop_string_p (AREF (prop, i), string))
3702 return 1;
3703 }
3704 else
3705 return single_display_prop_string_p (prop, string);
3706
3707 return 0;
3708 }
3709
3710
3711 /* Determine from which buffer position in W's buffer STRING comes
3712 from. AROUND_CHARPOS is an approximate position where it could
3713 be from. Value is the buffer position or 0 if it couldn't be
3714 determined.
3715
3716 W's buffer must be current.
3717
3718 This function is necessary because we don't record buffer positions
3719 in glyphs generated from strings (to keep struct glyph small).
3720 This function may only use code that doesn't eval because it is
3721 called asynchronously from note_mouse_highlight. */
3722
3723 int
3724 string_buffer_position (w, string, around_charpos)
3725 struct window *w;
3726 Lisp_Object string;
3727 int around_charpos;
3728 {
3729 Lisp_Object limit, prop, pos;
3730 const int MAX_DISTANCE = 1000;
3731 int found = 0;
3732
3733 pos = make_number (around_charpos);
3734 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3735 while (!found && !EQ (pos, limit))
3736 {
3737 prop = Fget_char_property (pos, Qdisplay, Qnil);
3738 if (!NILP (prop) && display_prop_string_p (prop, string))
3739 found = 1;
3740 else
3741 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3742 }
3743
3744 if (!found)
3745 {
3746 pos = make_number (around_charpos);
3747 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3748 while (!found && !EQ (pos, limit))
3749 {
3750 prop = Fget_char_property (pos, Qdisplay, Qnil);
3751 if (!NILP (prop) && display_prop_string_p (prop, string))
3752 found = 1;
3753 else
3754 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3755 limit);
3756 }
3757 }
3758
3759 return found ? XINT (pos) : 0;
3760 }
3761
3762
3763 \f
3764 /***********************************************************************
3765 `composition' property
3766 ***********************************************************************/
3767
3768 /* Set up iterator IT from `composition' property at its current
3769 position. Called from handle_stop. */
3770
3771 static enum prop_handled
3772 handle_composition_prop (it)
3773 struct it *it;
3774 {
3775 Lisp_Object prop, string;
3776 int pos, pos_byte, end;
3777 enum prop_handled handled = HANDLED_NORMALLY;
3778
3779 if (STRINGP (it->string))
3780 {
3781 pos = IT_STRING_CHARPOS (*it);
3782 pos_byte = IT_STRING_BYTEPOS (*it);
3783 string = it->string;
3784 }
3785 else
3786 {
3787 pos = IT_CHARPOS (*it);
3788 pos_byte = IT_BYTEPOS (*it);
3789 string = Qnil;
3790 }
3791
3792 /* If there's a valid composition and point is not inside of the
3793 composition (in the case that the composition is from the current
3794 buffer), draw a glyph composed from the composition components. */
3795 if (find_composition (pos, -1, &pos, &end, &prop, string)
3796 && COMPOSITION_VALID_P (pos, end, prop)
3797 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3798 {
3799 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3800
3801 if (id >= 0)
3802 {
3803 it->method = next_element_from_composition;
3804 it->cmp_id = id;
3805 it->cmp_len = COMPOSITION_LENGTH (prop);
3806 /* For a terminal, draw only the first character of the
3807 components. */
3808 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3809 it->len = (STRINGP (it->string)
3810 ? string_char_to_byte (it->string, end)
3811 : CHAR_TO_BYTE (end)) - pos_byte;
3812 it->stop_charpos = end;
3813 handled = HANDLED_RETURN;
3814 }
3815 }
3816
3817 return handled;
3818 }
3819
3820
3821 \f
3822 /***********************************************************************
3823 Overlay strings
3824 ***********************************************************************/
3825
3826 /* The following structure is used to record overlay strings for
3827 later sorting in load_overlay_strings. */
3828
3829 struct overlay_entry
3830 {
3831 Lisp_Object overlay;
3832 Lisp_Object string;
3833 int priority;
3834 int after_string_p;
3835 };
3836
3837
3838 /* Set up iterator IT from overlay strings at its current position.
3839 Called from handle_stop. */
3840
3841 static enum prop_handled
3842 handle_overlay_change (it)
3843 struct it *it;
3844 {
3845 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3846 return HANDLED_RECOMPUTE_PROPS;
3847 else
3848 return HANDLED_NORMALLY;
3849 }
3850
3851
3852 /* Set up the next overlay string for delivery by IT, if there is an
3853 overlay string to deliver. Called by set_iterator_to_next when the
3854 end of the current overlay string is reached. If there are more
3855 overlay strings to display, IT->string and
3856 IT->current.overlay_string_index are set appropriately here.
3857 Otherwise IT->string is set to nil. */
3858
3859 static void
3860 next_overlay_string (it)
3861 struct it *it;
3862 {
3863 ++it->current.overlay_string_index;
3864 if (it->current.overlay_string_index == it->n_overlay_strings)
3865 {
3866 /* No more overlay strings. Restore IT's settings to what
3867 they were before overlay strings were processed, and
3868 continue to deliver from current_buffer. */
3869 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3870
3871 pop_it (it);
3872 xassert (it->stop_charpos >= BEGV
3873 && it->stop_charpos <= it->end_charpos);
3874 it->string = Qnil;
3875 it->current.overlay_string_index = -1;
3876 SET_TEXT_POS (it->current.string_pos, -1, -1);
3877 it->n_overlay_strings = 0;
3878 it->method = next_element_from_buffer;
3879
3880 /* If we're at the end of the buffer, record that we have
3881 processed the overlay strings there already, so that
3882 next_element_from_buffer doesn't try it again. */
3883 if (IT_CHARPOS (*it) >= it->end_charpos)
3884 it->overlay_strings_at_end_processed_p = 1;
3885
3886 /* If we have to display `...' for invisible text, set
3887 the iterator up for that. */
3888 if (display_ellipsis_p)
3889 setup_for_ellipsis (it);
3890 }
3891 else
3892 {
3893 /* There are more overlay strings to process. If
3894 IT->current.overlay_string_index has advanced to a position
3895 where we must load IT->overlay_strings with more strings, do
3896 it. */
3897 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3898
3899 if (it->current.overlay_string_index && i == 0)
3900 load_overlay_strings (it, 0);
3901
3902 /* Initialize IT to deliver display elements from the overlay
3903 string. */
3904 it->string = it->overlay_strings[i];
3905 it->multibyte_p = STRING_MULTIBYTE (it->string);
3906 SET_TEXT_POS (it->current.string_pos, 0, 0);
3907 it->method = next_element_from_string;
3908 it->stop_charpos = 0;
3909 }
3910
3911 CHECK_IT (it);
3912 }
3913
3914
3915 /* Compare two overlay_entry structures E1 and E2. Used as a
3916 comparison function for qsort in load_overlay_strings. Overlay
3917 strings for the same position are sorted so that
3918
3919 1. All after-strings come in front of before-strings, except
3920 when they come from the same overlay.
3921
3922 2. Within after-strings, strings are sorted so that overlay strings
3923 from overlays with higher priorities come first.
3924
3925 2. Within before-strings, strings are sorted so that overlay
3926 strings from overlays with higher priorities come last.
3927
3928 Value is analogous to strcmp. */
3929
3930
3931 static int
3932 compare_overlay_entries (e1, e2)
3933 void *e1, *e2;
3934 {
3935 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3936 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3937 int result;
3938
3939 if (entry1->after_string_p != entry2->after_string_p)
3940 {
3941 /* Let after-strings appear in front of before-strings if
3942 they come from different overlays. */
3943 if (EQ (entry1->overlay, entry2->overlay))
3944 result = entry1->after_string_p ? 1 : -1;
3945 else
3946 result = entry1->after_string_p ? -1 : 1;
3947 }
3948 else if (entry1->after_string_p)
3949 /* After-strings sorted in order of decreasing priority. */
3950 result = entry2->priority - entry1->priority;
3951 else
3952 /* Before-strings sorted in order of increasing priority. */
3953 result = entry1->priority - entry2->priority;
3954
3955 return result;
3956 }
3957
3958
3959 /* Load the vector IT->overlay_strings with overlay strings from IT's
3960 current buffer position, or from CHARPOS if that is > 0. Set
3961 IT->n_overlays to the total number of overlay strings found.
3962
3963 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3964 a time. On entry into load_overlay_strings,
3965 IT->current.overlay_string_index gives the number of overlay
3966 strings that have already been loaded by previous calls to this
3967 function.
3968
3969 IT->add_overlay_start contains an additional overlay start
3970 position to consider for taking overlay strings from, if non-zero.
3971 This position comes into play when the overlay has an `invisible'
3972 property, and both before and after-strings. When we've skipped to
3973 the end of the overlay, because of its `invisible' property, we
3974 nevertheless want its before-string to appear.
3975 IT->add_overlay_start will contain the overlay start position
3976 in this case.
3977
3978 Overlay strings are sorted so that after-string strings come in
3979 front of before-string strings. Within before and after-strings,
3980 strings are sorted by overlay priority. See also function
3981 compare_overlay_entries. */
3982
3983 static void
3984 load_overlay_strings (it, charpos)
3985 struct it *it;
3986 int charpos;
3987 {
3988 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3989 Lisp_Object overlay, window, str, invisible;
3990 struct Lisp_Overlay *ov;
3991 int start, end;
3992 int size = 20;
3993 int n = 0, i, j, invis_p;
3994 struct overlay_entry *entries
3995 = (struct overlay_entry *) alloca (size * sizeof *entries);
3996
3997 if (charpos <= 0)
3998 charpos = IT_CHARPOS (*it);
3999
4000 /* Append the overlay string STRING of overlay OVERLAY to vector
4001 `entries' which has size `size' and currently contains `n'
4002 elements. AFTER_P non-zero means STRING is an after-string of
4003 OVERLAY. */
4004 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4005 do \
4006 { \
4007 Lisp_Object priority; \
4008 \
4009 if (n == size) \
4010 { \
4011 int new_size = 2 * size; \
4012 struct overlay_entry *old = entries; \
4013 entries = \
4014 (struct overlay_entry *) alloca (new_size \
4015 * sizeof *entries); \
4016 bcopy (old, entries, size * sizeof *entries); \
4017 size = new_size; \
4018 } \
4019 \
4020 entries[n].string = (STRING); \
4021 entries[n].overlay = (OVERLAY); \
4022 priority = Foverlay_get ((OVERLAY), Qpriority); \
4023 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4024 entries[n].after_string_p = (AFTER_P); \
4025 ++n; \
4026 } \
4027 while (0)
4028
4029 /* Process overlay before the overlay center. */
4030 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4031 {
4032 XSETMISC (overlay, ov);
4033 xassert (OVERLAYP (overlay));
4034 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4035 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4036
4037 if (end < charpos)
4038 break;
4039
4040 /* Skip this overlay if it doesn't start or end at IT's current
4041 position. */
4042 if (end != charpos && start != charpos)
4043 continue;
4044
4045 /* Skip this overlay if it doesn't apply to IT->w. */
4046 window = Foverlay_get (overlay, Qwindow);
4047 if (WINDOWP (window) && XWINDOW (window) != it->w)
4048 continue;
4049
4050 /* If the text ``under'' the overlay is invisible, both before-
4051 and after-strings from this overlay are visible; start and
4052 end position are indistinguishable. */
4053 invisible = Foverlay_get (overlay, Qinvisible);
4054 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4055
4056 /* If overlay has a non-empty before-string, record it. */
4057 if ((start == charpos || (end == charpos && invis_p))
4058 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4059 && SCHARS (str))
4060 RECORD_OVERLAY_STRING (overlay, str, 0);
4061
4062 /* If overlay has a non-empty after-string, record it. */
4063 if ((end == charpos || (start == charpos && invis_p))
4064 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4065 && SCHARS (str))
4066 RECORD_OVERLAY_STRING (overlay, str, 1);
4067 }
4068
4069 /* Process overlays after the overlay center. */
4070 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4071 {
4072 XSETMISC (overlay, ov);
4073 xassert (OVERLAYP (overlay));
4074 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4075 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4076
4077 if (start > charpos)
4078 break;
4079
4080 /* Skip this overlay if it doesn't start or end at IT's current
4081 position. */
4082 if (end != charpos && start != charpos)
4083 continue;
4084
4085 /* Skip this overlay if it doesn't apply to IT->w. */
4086 window = Foverlay_get (overlay, Qwindow);
4087 if (WINDOWP (window) && XWINDOW (window) != it->w)
4088 continue;
4089
4090 /* If the text ``under'' the overlay is invisible, it has a zero
4091 dimension, and both before- and after-strings apply. */
4092 invisible = Foverlay_get (overlay, Qinvisible);
4093 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4094
4095 /* If overlay has a non-empty before-string, record it. */
4096 if ((start == charpos || (end == charpos && invis_p))
4097 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4098 && SCHARS (str))
4099 RECORD_OVERLAY_STRING (overlay, str, 0);
4100
4101 /* If overlay has a non-empty after-string, record it. */
4102 if ((end == charpos || (start == charpos && invis_p))
4103 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4104 && SCHARS (str))
4105 RECORD_OVERLAY_STRING (overlay, str, 1);
4106 }
4107
4108 #undef RECORD_OVERLAY_STRING
4109
4110 /* Sort entries. */
4111 if (n > 1)
4112 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4113
4114 /* Record the total number of strings to process. */
4115 it->n_overlay_strings = n;
4116
4117 /* IT->current.overlay_string_index is the number of overlay strings
4118 that have already been consumed by IT. Copy some of the
4119 remaining overlay strings to IT->overlay_strings. */
4120 i = 0;
4121 j = it->current.overlay_string_index;
4122 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4123 it->overlay_strings[i++] = entries[j++].string;
4124
4125 CHECK_IT (it);
4126 }
4127
4128
4129 /* Get the first chunk of overlay strings at IT's current buffer
4130 position, or at CHARPOS if that is > 0. Value is non-zero if at
4131 least one overlay string was found. */
4132
4133 static int
4134 get_overlay_strings (it, charpos)
4135 struct it *it;
4136 int charpos;
4137 {
4138 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4139 process. This fills IT->overlay_strings with strings, and sets
4140 IT->n_overlay_strings to the total number of strings to process.
4141 IT->pos.overlay_string_index has to be set temporarily to zero
4142 because load_overlay_strings needs this; it must be set to -1
4143 when no overlay strings are found because a zero value would
4144 indicate a position in the first overlay string. */
4145 it->current.overlay_string_index = 0;
4146 load_overlay_strings (it, charpos);
4147
4148 /* If we found overlay strings, set up IT to deliver display
4149 elements from the first one. Otherwise set up IT to deliver
4150 from current_buffer. */
4151 if (it->n_overlay_strings)
4152 {
4153 /* Make sure we know settings in current_buffer, so that we can
4154 restore meaningful values when we're done with the overlay
4155 strings. */
4156 compute_stop_pos (it);
4157 xassert (it->face_id >= 0);
4158
4159 /* Save IT's settings. They are restored after all overlay
4160 strings have been processed. */
4161 xassert (it->sp == 0);
4162 push_it (it);
4163
4164 /* Set up IT to deliver display elements from the first overlay
4165 string. */
4166 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4167 it->string = it->overlay_strings[0];
4168 it->stop_charpos = 0;
4169 xassert (STRINGP (it->string));
4170 it->end_charpos = SCHARS (it->string);
4171 it->multibyte_p = STRING_MULTIBYTE (it->string);
4172 it->method = next_element_from_string;
4173 }
4174 else
4175 {
4176 it->string = Qnil;
4177 it->current.overlay_string_index = -1;
4178 it->method = next_element_from_buffer;
4179 }
4180
4181 CHECK_IT (it);
4182
4183 /* Value is non-zero if we found at least one overlay string. */
4184 return STRINGP (it->string);
4185 }
4186
4187
4188 \f
4189 /***********************************************************************
4190 Saving and restoring state
4191 ***********************************************************************/
4192
4193 /* Save current settings of IT on IT->stack. Called, for example,
4194 before setting up IT for an overlay string, to be able to restore
4195 IT's settings to what they were after the overlay string has been
4196 processed. */
4197
4198 static void
4199 push_it (it)
4200 struct it *it;
4201 {
4202 struct iterator_stack_entry *p;
4203
4204 xassert (it->sp < 2);
4205 p = it->stack + it->sp;
4206
4207 p->stop_charpos = it->stop_charpos;
4208 xassert (it->face_id >= 0);
4209 p->face_id = it->face_id;
4210 p->string = it->string;
4211 p->pos = it->current;
4212 p->end_charpos = it->end_charpos;
4213 p->string_nchars = it->string_nchars;
4214 p->area = it->area;
4215 p->multibyte_p = it->multibyte_p;
4216 p->space_width = it->space_width;
4217 p->font_height = it->font_height;
4218 p->voffset = it->voffset;
4219 p->string_from_display_prop_p = it->string_from_display_prop_p;
4220 p->display_ellipsis_p = 0;
4221 ++it->sp;
4222 }
4223
4224
4225 /* Restore IT's settings from IT->stack. Called, for example, when no
4226 more overlay strings must be processed, and we return to delivering
4227 display elements from a buffer, or when the end of a string from a
4228 `display' property is reached and we return to delivering display
4229 elements from an overlay string, or from a buffer. */
4230
4231 static void
4232 pop_it (it)
4233 struct it *it;
4234 {
4235 struct iterator_stack_entry *p;
4236
4237 xassert (it->sp > 0);
4238 --it->sp;
4239 p = it->stack + it->sp;
4240 it->stop_charpos = p->stop_charpos;
4241 it->face_id = p->face_id;
4242 it->string = p->string;
4243 it->current = p->pos;
4244 it->end_charpos = p->end_charpos;
4245 it->string_nchars = p->string_nchars;
4246 it->area = p->area;
4247 it->multibyte_p = p->multibyte_p;
4248 it->space_width = p->space_width;
4249 it->font_height = p->font_height;
4250 it->voffset = p->voffset;
4251 it->string_from_display_prop_p = p->string_from_display_prop_p;
4252 }
4253
4254
4255 \f
4256 /***********************************************************************
4257 Moving over lines
4258 ***********************************************************************/
4259
4260 /* Set IT's current position to the previous line start. */
4261
4262 static void
4263 back_to_previous_line_start (it)
4264 struct it *it;
4265 {
4266 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4267 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4268 }
4269
4270
4271 /* Move IT to the next line start.
4272
4273 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4274 we skipped over part of the text (as opposed to moving the iterator
4275 continuously over the text). Otherwise, don't change the value
4276 of *SKIPPED_P.
4277
4278 Newlines may come from buffer text, overlay strings, or strings
4279 displayed via the `display' property. That's the reason we can't
4280 simply use find_next_newline_no_quit.
4281
4282 Note that this function may not skip over invisible text that is so
4283 because of text properties and immediately follows a newline. If
4284 it would, function reseat_at_next_visible_line_start, when called
4285 from set_iterator_to_next, would effectively make invisible
4286 characters following a newline part of the wrong glyph row, which
4287 leads to wrong cursor motion. */
4288
4289 static int
4290 forward_to_next_line_start (it, skipped_p)
4291 struct it *it;
4292 int *skipped_p;
4293 {
4294 int old_selective, newline_found_p, n;
4295 const int MAX_NEWLINE_DISTANCE = 500;
4296
4297 /* If already on a newline, just consume it to avoid unintended
4298 skipping over invisible text below. */
4299 if (it->what == IT_CHARACTER
4300 && it->c == '\n'
4301 && CHARPOS (it->position) == IT_CHARPOS (*it))
4302 {
4303 set_iterator_to_next (it, 0);
4304 it->c = 0;
4305 return 1;
4306 }
4307
4308 /* Don't handle selective display in the following. It's (a)
4309 unnecessary because it's done by the caller, and (b) leads to an
4310 infinite recursion because next_element_from_ellipsis indirectly
4311 calls this function. */
4312 old_selective = it->selective;
4313 it->selective = 0;
4314
4315 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4316 from buffer text. */
4317 for (n = newline_found_p = 0;
4318 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4319 n += STRINGP (it->string) ? 0 : 1)
4320 {
4321 if (!get_next_display_element (it))
4322 return 0;
4323 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4324 set_iterator_to_next (it, 0);
4325 }
4326
4327 /* If we didn't find a newline near enough, see if we can use a
4328 short-cut. */
4329 if (!newline_found_p)
4330 {
4331 int start = IT_CHARPOS (*it);
4332 int limit = find_next_newline_no_quit (start, 1);
4333 Lisp_Object pos;
4334
4335 xassert (!STRINGP (it->string));
4336
4337 /* If there isn't any `display' property in sight, and no
4338 overlays, we can just use the position of the newline in
4339 buffer text. */
4340 if (it->stop_charpos >= limit
4341 || ((pos = Fnext_single_property_change (make_number (start),
4342 Qdisplay,
4343 Qnil, make_number (limit)),
4344 NILP (pos))
4345 && next_overlay_change (start) == ZV))
4346 {
4347 IT_CHARPOS (*it) = limit;
4348 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4349 *skipped_p = newline_found_p = 1;
4350 }
4351 else
4352 {
4353 while (get_next_display_element (it)
4354 && !newline_found_p)
4355 {
4356 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4357 set_iterator_to_next (it, 0);
4358 }
4359 }
4360 }
4361
4362 it->selective = old_selective;
4363 return newline_found_p;
4364 }
4365
4366
4367 /* Set IT's current position to the previous visible line start. Skip
4368 invisible text that is so either due to text properties or due to
4369 selective display. Caution: this does not change IT->current_x and
4370 IT->hpos. */
4371
4372 static void
4373 back_to_previous_visible_line_start (it)
4374 struct it *it;
4375 {
4376 int visible_p = 0;
4377
4378 /* Go back one newline if not on BEGV already. */
4379 if (IT_CHARPOS (*it) > BEGV)
4380 back_to_previous_line_start (it);
4381
4382 /* Move over lines that are invisible because of selective display
4383 or text properties. */
4384 while (IT_CHARPOS (*it) > BEGV
4385 && !visible_p)
4386 {
4387 visible_p = 1;
4388
4389 /* If selective > 0, then lines indented more than that values
4390 are invisible. */
4391 if (it->selective > 0
4392 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4393 (double) it->selective)) /* iftc */
4394 visible_p = 0;
4395 else
4396 {
4397 Lisp_Object prop;
4398
4399 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
4400 Qinvisible, it->window);
4401 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4402 visible_p = 0;
4403 }
4404
4405 /* Back one more newline if the current one is invisible. */
4406 if (!visible_p)
4407 back_to_previous_line_start (it);
4408 }
4409
4410 xassert (IT_CHARPOS (*it) >= BEGV);
4411 xassert (IT_CHARPOS (*it) == BEGV
4412 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4413 CHECK_IT (it);
4414 }
4415
4416
4417 /* Reseat iterator IT at the previous visible line start. Skip
4418 invisible text that is so either due to text properties or due to
4419 selective display. At the end, update IT's overlay information,
4420 face information etc. */
4421
4422 static void
4423 reseat_at_previous_visible_line_start (it)
4424 struct it *it;
4425 {
4426 back_to_previous_visible_line_start (it);
4427 reseat (it, it->current.pos, 1);
4428 CHECK_IT (it);
4429 }
4430
4431
4432 /* Reseat iterator IT on the next visible line start in the current
4433 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4434 preceding the line start. Skip over invisible text that is so
4435 because of selective display. Compute faces, overlays etc at the
4436 new position. Note that this function does not skip over text that
4437 is invisible because of text properties. */
4438
4439 static void
4440 reseat_at_next_visible_line_start (it, on_newline_p)
4441 struct it *it;
4442 int on_newline_p;
4443 {
4444 int newline_found_p, skipped_p = 0;
4445
4446 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4447
4448 /* Skip over lines that are invisible because they are indented
4449 more than the value of IT->selective. */
4450 if (it->selective > 0)
4451 while (IT_CHARPOS (*it) < ZV
4452 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4453 (double) it->selective)) /* iftc */
4454 {
4455 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4456 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4457 }
4458
4459 /* Position on the newline if that's what's requested. */
4460 if (on_newline_p && newline_found_p)
4461 {
4462 if (STRINGP (it->string))
4463 {
4464 if (IT_STRING_CHARPOS (*it) > 0)
4465 {
4466 --IT_STRING_CHARPOS (*it);
4467 --IT_STRING_BYTEPOS (*it);
4468 }
4469 }
4470 else if (IT_CHARPOS (*it) > BEGV)
4471 {
4472 --IT_CHARPOS (*it);
4473 --IT_BYTEPOS (*it);
4474 reseat (it, it->current.pos, 0);
4475 }
4476 }
4477 else if (skipped_p)
4478 reseat (it, it->current.pos, 0);
4479
4480 CHECK_IT (it);
4481 }
4482
4483
4484 \f
4485 /***********************************************************************
4486 Changing an iterator's position
4487 ***********************************************************************/
4488
4489 /* Change IT's current position to POS in current_buffer. If FORCE_P
4490 is non-zero, always check for text properties at the new position.
4491 Otherwise, text properties are only looked up if POS >=
4492 IT->check_charpos of a property. */
4493
4494 static void
4495 reseat (it, pos, force_p)
4496 struct it *it;
4497 struct text_pos pos;
4498 int force_p;
4499 {
4500 int original_pos = IT_CHARPOS (*it);
4501
4502 reseat_1 (it, pos, 0);
4503
4504 /* Determine where to check text properties. Avoid doing it
4505 where possible because text property lookup is very expensive. */
4506 if (force_p
4507 || CHARPOS (pos) > it->stop_charpos
4508 || CHARPOS (pos) < original_pos)
4509 handle_stop (it);
4510
4511 CHECK_IT (it);
4512 }
4513
4514
4515 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4516 IT->stop_pos to POS, also. */
4517
4518 static void
4519 reseat_1 (it, pos, set_stop_p)
4520 struct it *it;
4521 struct text_pos pos;
4522 int set_stop_p;
4523 {
4524 /* Don't call this function when scanning a C string. */
4525 xassert (it->s == NULL);
4526
4527 /* POS must be a reasonable value. */
4528 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4529
4530 it->current.pos = it->position = pos;
4531 XSETBUFFER (it->object, current_buffer);
4532 it->end_charpos = ZV;
4533 it->dpvec = NULL;
4534 it->current.dpvec_index = -1;
4535 it->current.overlay_string_index = -1;
4536 IT_STRING_CHARPOS (*it) = -1;
4537 IT_STRING_BYTEPOS (*it) = -1;
4538 it->string = Qnil;
4539 it->method = next_element_from_buffer;
4540 /* RMS: I added this to fix a bug in move_it_vertically_backward
4541 where it->area continued to relate to the starting point
4542 for the backward motion. Bug report from
4543 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4544 However, I am not sure whether reseat still does the right thing
4545 in general after this change. */
4546 it->area = TEXT_AREA;
4547 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4548 it->sp = 0;
4549 it->face_before_selective_p = 0;
4550
4551 if (set_stop_p)
4552 it->stop_charpos = CHARPOS (pos);
4553 }
4554
4555
4556 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4557 If S is non-null, it is a C string to iterate over. Otherwise,
4558 STRING gives a Lisp string to iterate over.
4559
4560 If PRECISION > 0, don't return more then PRECISION number of
4561 characters from the string.
4562
4563 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4564 characters have been returned. FIELD_WIDTH < 0 means an infinite
4565 field width.
4566
4567 MULTIBYTE = 0 means disable processing of multibyte characters,
4568 MULTIBYTE > 0 means enable it,
4569 MULTIBYTE < 0 means use IT->multibyte_p.
4570
4571 IT must be initialized via a prior call to init_iterator before
4572 calling this function. */
4573
4574 static void
4575 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4576 struct it *it;
4577 unsigned char *s;
4578 Lisp_Object string;
4579 int charpos;
4580 int precision, field_width, multibyte;
4581 {
4582 /* No region in strings. */
4583 it->region_beg_charpos = it->region_end_charpos = -1;
4584
4585 /* No text property checks performed by default, but see below. */
4586 it->stop_charpos = -1;
4587
4588 /* Set iterator position and end position. */
4589 bzero (&it->current, sizeof it->current);
4590 it->current.overlay_string_index = -1;
4591 it->current.dpvec_index = -1;
4592 xassert (charpos >= 0);
4593
4594 /* If STRING is specified, use its multibyteness, otherwise use the
4595 setting of MULTIBYTE, if specified. */
4596 if (multibyte >= 0)
4597 it->multibyte_p = multibyte > 0;
4598
4599 if (s == NULL)
4600 {
4601 xassert (STRINGP (string));
4602 it->string = string;
4603 it->s = NULL;
4604 it->end_charpos = it->string_nchars = SCHARS (string);
4605 it->method = next_element_from_string;
4606 it->current.string_pos = string_pos (charpos, string);
4607 }
4608 else
4609 {
4610 it->s = s;
4611 it->string = Qnil;
4612
4613 /* Note that we use IT->current.pos, not it->current.string_pos,
4614 for displaying C strings. */
4615 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4616 if (it->multibyte_p)
4617 {
4618 it->current.pos = c_string_pos (charpos, s, 1);
4619 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4620 }
4621 else
4622 {
4623 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4624 it->end_charpos = it->string_nchars = strlen (s);
4625 }
4626
4627 it->method = next_element_from_c_string;
4628 }
4629
4630 /* PRECISION > 0 means don't return more than PRECISION characters
4631 from the string. */
4632 if (precision > 0 && it->end_charpos - charpos > precision)
4633 it->end_charpos = it->string_nchars = charpos + precision;
4634
4635 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4636 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4637 FIELD_WIDTH < 0 means infinite field width. This is useful for
4638 padding with `-' at the end of a mode line. */
4639 if (field_width < 0)
4640 field_width = INFINITY;
4641 if (field_width > it->end_charpos - charpos)
4642 it->end_charpos = charpos + field_width;
4643
4644 /* Use the standard display table for displaying strings. */
4645 if (DISP_TABLE_P (Vstandard_display_table))
4646 it->dp = XCHAR_TABLE (Vstandard_display_table);
4647
4648 it->stop_charpos = charpos;
4649 CHECK_IT (it);
4650 }
4651
4652
4653 \f
4654 /***********************************************************************
4655 Iteration
4656 ***********************************************************************/
4657
4658 /* Load IT's display element fields with information about the next
4659 display element from the current position of IT. Value is zero if
4660 end of buffer (or C string) is reached. */
4661
4662 int
4663 get_next_display_element (it)
4664 struct it *it;
4665 {
4666 /* Non-zero means that we found a display element. Zero means that
4667 we hit the end of what we iterate over. Performance note: the
4668 function pointer `method' used here turns out to be faster than
4669 using a sequence of if-statements. */
4670 int success_p = (*it->method) (it);
4671
4672 if (it->what == IT_CHARACTER)
4673 {
4674 /* Map via display table or translate control characters.
4675 IT->c, IT->len etc. have been set to the next character by
4676 the function call above. If we have a display table, and it
4677 contains an entry for IT->c, translate it. Don't do this if
4678 IT->c itself comes from a display table, otherwise we could
4679 end up in an infinite recursion. (An alternative could be to
4680 count the recursion depth of this function and signal an
4681 error when a certain maximum depth is reached.) Is it worth
4682 it? */
4683 if (success_p && it->dpvec == NULL)
4684 {
4685 Lisp_Object dv;
4686
4687 if (it->dp
4688 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4689 VECTORP (dv)))
4690 {
4691 struct Lisp_Vector *v = XVECTOR (dv);
4692
4693 /* Return the first character from the display table
4694 entry, if not empty. If empty, don't display the
4695 current character. */
4696 if (v->size)
4697 {
4698 it->dpvec_char_len = it->len;
4699 it->dpvec = v->contents;
4700 it->dpend = v->contents + v->size;
4701 it->current.dpvec_index = 0;
4702 it->method = next_element_from_display_vector;
4703 success_p = get_next_display_element (it);
4704 }
4705 else
4706 {
4707 set_iterator_to_next (it, 0);
4708 success_p = get_next_display_element (it);
4709 }
4710 }
4711
4712 /* Translate control characters into `\003' or `^C' form.
4713 Control characters coming from a display table entry are
4714 currently not translated because we use IT->dpvec to hold
4715 the translation. This could easily be changed but I
4716 don't believe that it is worth doing.
4717
4718 If it->multibyte_p is nonzero, eight-bit characters and
4719 non-printable multibyte characters are also translated to
4720 octal form.
4721
4722 If it->multibyte_p is zero, eight-bit characters that
4723 don't have corresponding multibyte char code are also
4724 translated to octal form. */
4725 else if ((it->c < ' '
4726 && (it->area != TEXT_AREA
4727 || (it->c != '\n' && it->c != '\t')))
4728 || (it->multibyte_p
4729 ? ((it->c >= 127
4730 && it->len == 1)
4731 || !CHAR_PRINTABLE_P (it->c))
4732 : (it->c >= 127
4733 && it->c == unibyte_char_to_multibyte (it->c))))
4734 {
4735 /* IT->c is a control character which must be displayed
4736 either as '\003' or as `^C' where the '\\' and '^'
4737 can be defined in the display table. Fill
4738 IT->ctl_chars with glyphs for what we have to
4739 display. Then, set IT->dpvec to these glyphs. */
4740 GLYPH g;
4741
4742 if (it->c < 128 && it->ctl_arrow_p)
4743 {
4744 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4745 if (it->dp
4746 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4747 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4748 g = XINT (DISP_CTRL_GLYPH (it->dp));
4749 else
4750 g = FAST_MAKE_GLYPH ('^', 0);
4751 XSETINT (it->ctl_chars[0], g);
4752
4753 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4754 XSETINT (it->ctl_chars[1], g);
4755
4756 /* Set up IT->dpvec and return first character from it. */
4757 it->dpvec_char_len = it->len;
4758 it->dpvec = it->ctl_chars;
4759 it->dpend = it->dpvec + 2;
4760 it->current.dpvec_index = 0;
4761 it->method = next_element_from_display_vector;
4762 get_next_display_element (it);
4763 }
4764 else
4765 {
4766 unsigned char str[MAX_MULTIBYTE_LENGTH];
4767 int len;
4768 int i;
4769 GLYPH escape_glyph;
4770
4771 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4772 if (it->dp
4773 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4774 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4775 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4776 else
4777 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4778
4779 if (SINGLE_BYTE_CHAR_P (it->c))
4780 str[0] = it->c, len = 1;
4781 else
4782 {
4783 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4784 if (len < 0)
4785 {
4786 /* It's an invalid character, which
4787 shouldn't happen actually, but due to
4788 bugs it may happen. Let's print the char
4789 as is, there's not much meaningful we can
4790 do with it. */
4791 str[0] = it->c;
4792 str[1] = it->c >> 8;
4793 str[2] = it->c >> 16;
4794 str[3] = it->c >> 24;
4795 len = 4;
4796 }
4797 }
4798
4799 for (i = 0; i < len; i++)
4800 {
4801 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4802 /* Insert three more glyphs into IT->ctl_chars for
4803 the octal display of the character. */
4804 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4805 XSETINT (it->ctl_chars[i * 4 + 1], g);
4806 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4807 XSETINT (it->ctl_chars[i * 4 + 2], g);
4808 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4809 XSETINT (it->ctl_chars[i * 4 + 3], g);
4810 }
4811
4812 /* Set up IT->dpvec and return the first character
4813 from it. */
4814 it->dpvec_char_len = it->len;
4815 it->dpvec = it->ctl_chars;
4816 it->dpend = it->dpvec + len * 4;
4817 it->current.dpvec_index = 0;
4818 it->method = next_element_from_display_vector;
4819 get_next_display_element (it);
4820 }
4821 }
4822 }
4823
4824 /* Adjust face id for a multibyte character. There are no
4825 multibyte character in unibyte text. */
4826 if (it->multibyte_p
4827 && success_p
4828 && FRAME_WINDOW_P (it->f))
4829 {
4830 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4831 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4832 }
4833 }
4834
4835 /* Is this character the last one of a run of characters with
4836 box? If yes, set IT->end_of_box_run_p to 1. */
4837 if (it->face_box_p
4838 && it->s == NULL)
4839 {
4840 int face_id;
4841 struct face *face;
4842
4843 it->end_of_box_run_p
4844 = ((face_id = face_after_it_pos (it),
4845 face_id != it->face_id)
4846 && (face = FACE_FROM_ID (it->f, face_id),
4847 face->box == FACE_NO_BOX));
4848 }
4849
4850 /* Value is 0 if end of buffer or string reached. */
4851 return success_p;
4852 }
4853
4854
4855 /* Move IT to the next display element.
4856
4857 RESEAT_P non-zero means if called on a newline in buffer text,
4858 skip to the next visible line start.
4859
4860 Functions get_next_display_element and set_iterator_to_next are
4861 separate because I find this arrangement easier to handle than a
4862 get_next_display_element function that also increments IT's
4863 position. The way it is we can first look at an iterator's current
4864 display element, decide whether it fits on a line, and if it does,
4865 increment the iterator position. The other way around we probably
4866 would either need a flag indicating whether the iterator has to be
4867 incremented the next time, or we would have to implement a
4868 decrement position function which would not be easy to write. */
4869
4870 void
4871 set_iterator_to_next (it, reseat_p)
4872 struct it *it;
4873 int reseat_p;
4874 {
4875 /* Reset flags indicating start and end of a sequence of characters
4876 with box. Reset them at the start of this function because
4877 moving the iterator to a new position might set them. */
4878 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4879
4880 if (it->method == next_element_from_buffer)
4881 {
4882 /* The current display element of IT is a character from
4883 current_buffer. Advance in the buffer, and maybe skip over
4884 invisible lines that are so because of selective display. */
4885 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4886 reseat_at_next_visible_line_start (it, 0);
4887 else
4888 {
4889 xassert (it->len != 0);
4890 IT_BYTEPOS (*it) += it->len;
4891 IT_CHARPOS (*it) += 1;
4892 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4893 }
4894 }
4895 else if (it->method == next_element_from_composition)
4896 {
4897 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4898 if (STRINGP (it->string))
4899 {
4900 IT_STRING_BYTEPOS (*it) += it->len;
4901 IT_STRING_CHARPOS (*it) += it->cmp_len;
4902 it->method = next_element_from_string;
4903 goto consider_string_end;
4904 }
4905 else
4906 {
4907 IT_BYTEPOS (*it) += it->len;
4908 IT_CHARPOS (*it) += it->cmp_len;
4909 it->method = next_element_from_buffer;
4910 }
4911 }
4912 else if (it->method == next_element_from_c_string)
4913 {
4914 /* Current display element of IT is from a C string. */
4915 IT_BYTEPOS (*it) += it->len;
4916 IT_CHARPOS (*it) += 1;
4917 }
4918 else if (it->method == next_element_from_display_vector)
4919 {
4920 /* Current display element of IT is from a display table entry.
4921 Advance in the display table definition. Reset it to null if
4922 end reached, and continue with characters from buffers/
4923 strings. */
4924 ++it->current.dpvec_index;
4925
4926 /* Restore face of the iterator to what they were before the
4927 display vector entry (these entries may contain faces). */
4928 it->face_id = it->saved_face_id;
4929
4930 if (it->dpvec + it->current.dpvec_index == it->dpend)
4931 {
4932 if (it->s)
4933 it->method = next_element_from_c_string;
4934 else if (STRINGP (it->string))
4935 it->method = next_element_from_string;
4936 else
4937 it->method = next_element_from_buffer;
4938
4939 it->dpvec = NULL;
4940 it->current.dpvec_index = -1;
4941
4942 /* Skip over characters which were displayed via IT->dpvec. */
4943 if (it->dpvec_char_len < 0)
4944 reseat_at_next_visible_line_start (it, 1);
4945 else if (it->dpvec_char_len > 0)
4946 {
4947 it->len = it->dpvec_char_len;
4948 set_iterator_to_next (it, reseat_p);
4949 }
4950 }
4951 }
4952 else if (it->method == next_element_from_string)
4953 {
4954 /* Current display element is a character from a Lisp string. */
4955 xassert (it->s == NULL && STRINGP (it->string));
4956 IT_STRING_BYTEPOS (*it) += it->len;
4957 IT_STRING_CHARPOS (*it) += 1;
4958
4959 consider_string_end:
4960
4961 if (it->current.overlay_string_index >= 0)
4962 {
4963 /* IT->string is an overlay string. Advance to the
4964 next, if there is one. */
4965 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
4966 next_overlay_string (it);
4967 }
4968 else
4969 {
4970 /* IT->string is not an overlay string. If we reached
4971 its end, and there is something on IT->stack, proceed
4972 with what is on the stack. This can be either another
4973 string, this time an overlay string, or a buffer. */
4974 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
4975 && it->sp > 0)
4976 {
4977 pop_it (it);
4978 if (!STRINGP (it->string))
4979 it->method = next_element_from_buffer;
4980 else
4981 goto consider_string_end;
4982 }
4983 }
4984 }
4985 else if (it->method == next_element_from_image
4986 || it->method == next_element_from_stretch)
4987 {
4988 /* The position etc with which we have to proceed are on
4989 the stack. The position may be at the end of a string,
4990 if the `display' property takes up the whole string. */
4991 pop_it (it);
4992 it->image_id = 0;
4993 if (STRINGP (it->string))
4994 {
4995 it->method = next_element_from_string;
4996 goto consider_string_end;
4997 }
4998 else
4999 it->method = next_element_from_buffer;
5000 }
5001 else
5002 /* There are no other methods defined, so this should be a bug. */
5003 abort ();
5004
5005 xassert (it->method != next_element_from_string
5006 || (STRINGP (it->string)
5007 && IT_STRING_CHARPOS (*it) >= 0));
5008 }
5009
5010
5011 /* Load IT's display element fields with information about the next
5012 display element which comes from a display table entry or from the
5013 result of translating a control character to one of the forms `^C'
5014 or `\003'. IT->dpvec holds the glyphs to return as characters. */
5015
5016 static int
5017 next_element_from_display_vector (it)
5018 struct it *it;
5019 {
5020 /* Precondition. */
5021 xassert (it->dpvec && it->current.dpvec_index >= 0);
5022
5023 /* Remember the current face id in case glyphs specify faces.
5024 IT's face is restored in set_iterator_to_next. */
5025 it->saved_face_id = it->face_id;
5026
5027 if (INTEGERP (*it->dpvec)
5028 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5029 {
5030 int lface_id;
5031 GLYPH g;
5032
5033 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5034 it->c = FAST_GLYPH_CHAR (g);
5035 it->len = CHAR_BYTES (it->c);
5036
5037 /* The entry may contain a face id to use. Such a face id is
5038 the id of a Lisp face, not a realized face. A face id of
5039 zero means no face is specified. */
5040 lface_id = FAST_GLYPH_FACE (g);
5041 if (lface_id)
5042 {
5043 /* The function returns -1 if lface_id is invalid. */
5044 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
5045 if (face_id >= 0)
5046 it->face_id = face_id;
5047 }
5048 }
5049 else
5050 /* Display table entry is invalid. Return a space. */
5051 it->c = ' ', it->len = 1;
5052
5053 /* Don't change position and object of the iterator here. They are
5054 still the values of the character that had this display table
5055 entry or was translated, and that's what we want. */
5056 it->what = IT_CHARACTER;
5057 return 1;
5058 }
5059
5060
5061 /* Load IT with the next display element from Lisp string IT->string.
5062 IT->current.string_pos is the current position within the string.
5063 If IT->current.overlay_string_index >= 0, the Lisp string is an
5064 overlay string. */
5065
5066 static int
5067 next_element_from_string (it)
5068 struct it *it;
5069 {
5070 struct text_pos position;
5071
5072 xassert (STRINGP (it->string));
5073 xassert (IT_STRING_CHARPOS (*it) >= 0);
5074 position = it->current.string_pos;
5075
5076 /* Time to check for invisible text? */
5077 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5078 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5079 {
5080 handle_stop (it);
5081
5082 /* Since a handler may have changed IT->method, we must
5083 recurse here. */
5084 return get_next_display_element (it);
5085 }
5086
5087 if (it->current.overlay_string_index >= 0)
5088 {
5089 /* Get the next character from an overlay string. In overlay
5090 strings, There is no field width or padding with spaces to
5091 do. */
5092 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5093 {
5094 it->what = IT_EOB;
5095 return 0;
5096 }
5097 else if (STRING_MULTIBYTE (it->string))
5098 {
5099 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5100 const unsigned char *s = (SDATA (it->string)
5101 + IT_STRING_BYTEPOS (*it));
5102 it->c = string_char_and_length (s, remaining, &it->len);
5103 }
5104 else
5105 {
5106 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5107 it->len = 1;
5108 }
5109 }
5110 else
5111 {
5112 /* Get the next character from a Lisp string that is not an
5113 overlay string. Such strings come from the mode line, for
5114 example. We may have to pad with spaces, or truncate the
5115 string. See also next_element_from_c_string. */
5116 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5117 {
5118 it->what = IT_EOB;
5119 return 0;
5120 }
5121 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5122 {
5123 /* Pad with spaces. */
5124 it->c = ' ', it->len = 1;
5125 CHARPOS (position) = BYTEPOS (position) = -1;
5126 }
5127 else if (STRING_MULTIBYTE (it->string))
5128 {
5129 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5130 const unsigned char *s = (SDATA (it->string)
5131 + IT_STRING_BYTEPOS (*it));
5132 it->c = string_char_and_length (s, maxlen, &it->len);
5133 }
5134 else
5135 {
5136 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5137 it->len = 1;
5138 }
5139 }
5140
5141 /* Record what we have and where it came from. Note that we store a
5142 buffer position in IT->position although it could arguably be a
5143 string position. */
5144 it->what = IT_CHARACTER;
5145 it->object = it->string;
5146 it->position = position;
5147 return 1;
5148 }
5149
5150
5151 /* Load IT with next display element from C string IT->s.
5152 IT->string_nchars is the maximum number of characters to return
5153 from the string. IT->end_charpos may be greater than
5154 IT->string_nchars when this function is called, in which case we
5155 may have to return padding spaces. Value is zero if end of string
5156 reached, including padding spaces. */
5157
5158 static int
5159 next_element_from_c_string (it)
5160 struct it *it;
5161 {
5162 int success_p = 1;
5163
5164 xassert (it->s);
5165 it->what = IT_CHARACTER;
5166 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5167 it->object = Qnil;
5168
5169 /* IT's position can be greater IT->string_nchars in case a field
5170 width or precision has been specified when the iterator was
5171 initialized. */
5172 if (IT_CHARPOS (*it) >= it->end_charpos)
5173 {
5174 /* End of the game. */
5175 it->what = IT_EOB;
5176 success_p = 0;
5177 }
5178 else if (IT_CHARPOS (*it) >= it->string_nchars)
5179 {
5180 /* Pad with spaces. */
5181 it->c = ' ', it->len = 1;
5182 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5183 }
5184 else if (it->multibyte_p)
5185 {
5186 /* Implementation note: The calls to strlen apparently aren't a
5187 performance problem because there is no noticeable performance
5188 difference between Emacs running in unibyte or multibyte mode. */
5189 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5190 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5191 maxlen, &it->len);
5192 }
5193 else
5194 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5195
5196 return success_p;
5197 }
5198
5199
5200 /* Set up IT to return characters from an ellipsis, if appropriate.
5201 The definition of the ellipsis glyphs may come from a display table
5202 entry. This function Fills IT with the first glyph from the
5203 ellipsis if an ellipsis is to be displayed. */
5204
5205 static int
5206 next_element_from_ellipsis (it)
5207 struct it *it;
5208 {
5209 if (it->selective_display_ellipsis_p)
5210 {
5211 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
5212 {
5213 /* Use the display table definition for `...'. Invalid glyphs
5214 will be handled by the method returning elements from dpvec. */
5215 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
5216 it->dpvec_char_len = it->len;
5217 it->dpvec = v->contents;
5218 it->dpend = v->contents + v->size;
5219 it->current.dpvec_index = 0;
5220 it->method = next_element_from_display_vector;
5221 }
5222 else
5223 {
5224 /* Use default `...' which is stored in default_invis_vector. */
5225 it->dpvec_char_len = it->len;
5226 it->dpvec = default_invis_vector;
5227 it->dpend = default_invis_vector + 3;
5228 it->current.dpvec_index = 0;
5229 it->method = next_element_from_display_vector;
5230 }
5231 }
5232 else
5233 {
5234 /* The face at the current position may be different from the
5235 face we find after the invisible text. Remember what it
5236 was in IT->saved_face_id, and signal that it's there by
5237 setting face_before_selective_p. */
5238 it->saved_face_id = it->face_id;
5239 it->method = next_element_from_buffer;
5240 reseat_at_next_visible_line_start (it, 1);
5241 it->face_before_selective_p = 1;
5242 }
5243
5244 return get_next_display_element (it);
5245 }
5246
5247
5248 /* Deliver an image display element. The iterator IT is already
5249 filled with image information (done in handle_display_prop). Value
5250 is always 1. */
5251
5252
5253 static int
5254 next_element_from_image (it)
5255 struct it *it;
5256 {
5257 it->what = IT_IMAGE;
5258 return 1;
5259 }
5260
5261
5262 /* Fill iterator IT with next display element from a stretch glyph
5263 property. IT->object is the value of the text property. Value is
5264 always 1. */
5265
5266 static int
5267 next_element_from_stretch (it)
5268 struct it *it;
5269 {
5270 it->what = IT_STRETCH;
5271 return 1;
5272 }
5273
5274
5275 /* Load IT with the next display element from current_buffer. Value
5276 is zero if end of buffer reached. IT->stop_charpos is the next
5277 position at which to stop and check for text properties or buffer
5278 end. */
5279
5280 static int
5281 next_element_from_buffer (it)
5282 struct it *it;
5283 {
5284 int success_p = 1;
5285
5286 /* Check this assumption, otherwise, we would never enter the
5287 if-statement, below. */
5288 xassert (IT_CHARPOS (*it) >= BEGV
5289 && IT_CHARPOS (*it) <= it->stop_charpos);
5290
5291 if (IT_CHARPOS (*it) >= it->stop_charpos)
5292 {
5293 if (IT_CHARPOS (*it) >= it->end_charpos)
5294 {
5295 int overlay_strings_follow_p;
5296
5297 /* End of the game, except when overlay strings follow that
5298 haven't been returned yet. */
5299 if (it->overlay_strings_at_end_processed_p)
5300 overlay_strings_follow_p = 0;
5301 else
5302 {
5303 it->overlay_strings_at_end_processed_p = 1;
5304 overlay_strings_follow_p = get_overlay_strings (it, 0);
5305 }
5306
5307 if (overlay_strings_follow_p)
5308 success_p = get_next_display_element (it);
5309 else
5310 {
5311 it->what = IT_EOB;
5312 it->position = it->current.pos;
5313 success_p = 0;
5314 }
5315 }
5316 else
5317 {
5318 handle_stop (it);
5319 return get_next_display_element (it);
5320 }
5321 }
5322 else
5323 {
5324 /* No face changes, overlays etc. in sight, so just return a
5325 character from current_buffer. */
5326 unsigned char *p;
5327
5328 /* Maybe run the redisplay end trigger hook. Performance note:
5329 This doesn't seem to cost measurable time. */
5330 if (it->redisplay_end_trigger_charpos
5331 && it->glyph_row
5332 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5333 run_redisplay_end_trigger_hook (it);
5334
5335 /* Get the next character, maybe multibyte. */
5336 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5337 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5338 {
5339 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5340 - IT_BYTEPOS (*it));
5341 it->c = string_char_and_length (p, maxlen, &it->len);
5342 }
5343 else
5344 it->c = *p, it->len = 1;
5345
5346 /* Record what we have and where it came from. */
5347 it->what = IT_CHARACTER;;
5348 it->object = it->w->buffer;
5349 it->position = it->current.pos;
5350
5351 /* Normally we return the character found above, except when we
5352 really want to return an ellipsis for selective display. */
5353 if (it->selective)
5354 {
5355 if (it->c == '\n')
5356 {
5357 /* A value of selective > 0 means hide lines indented more
5358 than that number of columns. */
5359 if (it->selective > 0
5360 && IT_CHARPOS (*it) + 1 < ZV
5361 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5362 IT_BYTEPOS (*it) + 1,
5363 (double) it->selective)) /* iftc */
5364 {
5365 success_p = next_element_from_ellipsis (it);
5366 it->dpvec_char_len = -1;
5367 }
5368 }
5369 else if (it->c == '\r' && it->selective == -1)
5370 {
5371 /* A value of selective == -1 means that everything from the
5372 CR to the end of the line is invisible, with maybe an
5373 ellipsis displayed for it. */
5374 success_p = next_element_from_ellipsis (it);
5375 it->dpvec_char_len = -1;
5376 }
5377 }
5378 }
5379
5380 /* Value is zero if end of buffer reached. */
5381 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5382 return success_p;
5383 }
5384
5385
5386 /* Run the redisplay end trigger hook for IT. */
5387
5388 static void
5389 run_redisplay_end_trigger_hook (it)
5390 struct it *it;
5391 {
5392 Lisp_Object args[3];
5393
5394 /* IT->glyph_row should be non-null, i.e. we should be actually
5395 displaying something, or otherwise we should not run the hook. */
5396 xassert (it->glyph_row);
5397
5398 /* Set up hook arguments. */
5399 args[0] = Qredisplay_end_trigger_functions;
5400 args[1] = it->window;
5401 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5402 it->redisplay_end_trigger_charpos = 0;
5403
5404 /* Since we are *trying* to run these functions, don't try to run
5405 them again, even if they get an error. */
5406 it->w->redisplay_end_trigger = Qnil;
5407 Frun_hook_with_args (3, args);
5408
5409 /* Notice if it changed the face of the character we are on. */
5410 handle_face_prop (it);
5411 }
5412
5413
5414 /* Deliver a composition display element. The iterator IT is already
5415 filled with composition information (done in
5416 handle_composition_prop). Value is always 1. */
5417
5418 static int
5419 next_element_from_composition (it)
5420 struct it *it;
5421 {
5422 it->what = IT_COMPOSITION;
5423 it->position = (STRINGP (it->string)
5424 ? it->current.string_pos
5425 : it->current.pos);
5426 return 1;
5427 }
5428
5429
5430 \f
5431 /***********************************************************************
5432 Moving an iterator without producing glyphs
5433 ***********************************************************************/
5434
5435 /* Move iterator IT to a specified buffer or X position within one
5436 line on the display without producing glyphs.
5437
5438 OP should be a bit mask including some or all of these bits:
5439 MOVE_TO_X: Stop on reaching x-position TO_X.
5440 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5441 Regardless of OP's value, stop in reaching the end of the display line.
5442
5443 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5444 This means, in particular, that TO_X includes window's horizontal
5445 scroll amount.
5446
5447 The return value has several possible values that
5448 say what condition caused the scan to stop:
5449
5450 MOVE_POS_MATCH_OR_ZV
5451 - when TO_POS or ZV was reached.
5452
5453 MOVE_X_REACHED
5454 -when TO_X was reached before TO_POS or ZV were reached.
5455
5456 MOVE_LINE_CONTINUED
5457 - when we reached the end of the display area and the line must
5458 be continued.
5459
5460 MOVE_LINE_TRUNCATED
5461 - when we reached the end of the display area and the line is
5462 truncated.
5463
5464 MOVE_NEWLINE_OR_CR
5465 - when we stopped at a line end, i.e. a newline or a CR and selective
5466 display is on. */
5467
5468 static enum move_it_result
5469 move_it_in_display_line_to (it, to_charpos, to_x, op)
5470 struct it *it;
5471 int to_charpos, to_x, op;
5472 {
5473 enum move_it_result result = MOVE_UNDEFINED;
5474 struct glyph_row *saved_glyph_row;
5475
5476 /* Don't produce glyphs in produce_glyphs. */
5477 saved_glyph_row = it->glyph_row;
5478 it->glyph_row = NULL;
5479
5480 while (1)
5481 {
5482 int x, i, ascent = 0, descent = 0;
5483
5484 /* Stop when ZV or TO_CHARPOS reached. */
5485 if (!get_next_display_element (it)
5486 || ((op & MOVE_TO_POS) != 0
5487 && BUFFERP (it->object)
5488 && IT_CHARPOS (*it) >= to_charpos))
5489 {
5490 result = MOVE_POS_MATCH_OR_ZV;
5491 break;
5492 }
5493
5494 /* The call to produce_glyphs will get the metrics of the
5495 display element IT is loaded with. We record in x the
5496 x-position before this display element in case it does not
5497 fit on the line. */
5498 x = it->current_x;
5499
5500 /* Remember the line height so far in case the next element doesn't
5501 fit on the line. */
5502 if (!it->truncate_lines_p)
5503 {
5504 ascent = it->max_ascent;
5505 descent = it->max_descent;
5506 }
5507
5508 PRODUCE_GLYPHS (it);
5509
5510 if (it->area != TEXT_AREA)
5511 {
5512 set_iterator_to_next (it, 1);
5513 continue;
5514 }
5515
5516 /* The number of glyphs we get back in IT->nglyphs will normally
5517 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5518 character on a terminal frame, or (iii) a line end. For the
5519 second case, IT->nglyphs - 1 padding glyphs will be present
5520 (on X frames, there is only one glyph produced for a
5521 composite character.
5522
5523 The behavior implemented below means, for continuation lines,
5524 that as many spaces of a TAB as fit on the current line are
5525 displayed there. For terminal frames, as many glyphs of a
5526 multi-glyph character are displayed in the current line, too.
5527 This is what the old redisplay code did, and we keep it that
5528 way. Under X, the whole shape of a complex character must
5529 fit on the line or it will be completely displayed in the
5530 next line.
5531
5532 Note that both for tabs and padding glyphs, all glyphs have
5533 the same width. */
5534 if (it->nglyphs)
5535 {
5536 /* More than one glyph or glyph doesn't fit on line. All
5537 glyphs have the same width. */
5538 int single_glyph_width = it->pixel_width / it->nglyphs;
5539 int new_x;
5540
5541 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5542 {
5543 new_x = x + single_glyph_width;
5544
5545 /* We want to leave anything reaching TO_X to the caller. */
5546 if ((op & MOVE_TO_X) && new_x > to_x)
5547 {
5548 it->current_x = x;
5549 result = MOVE_X_REACHED;
5550 break;
5551 }
5552 else if (/* Lines are continued. */
5553 !it->truncate_lines_p
5554 && (/* And glyph doesn't fit on the line. */
5555 new_x > it->last_visible_x
5556 /* Or it fits exactly and we're on a window
5557 system frame. */
5558 || (new_x == it->last_visible_x
5559 && FRAME_WINDOW_P (it->f))))
5560 {
5561 if (/* IT->hpos == 0 means the very first glyph
5562 doesn't fit on the line, e.g. a wide image. */
5563 it->hpos == 0
5564 || (new_x == it->last_visible_x
5565 && FRAME_WINDOW_P (it->f)))
5566 {
5567 ++it->hpos;
5568 it->current_x = new_x;
5569 if (i == it->nglyphs - 1)
5570 set_iterator_to_next (it, 1);
5571 }
5572 else
5573 {
5574 it->current_x = x;
5575 it->max_ascent = ascent;
5576 it->max_descent = descent;
5577 }
5578
5579 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5580 IT_CHARPOS (*it)));
5581 result = MOVE_LINE_CONTINUED;
5582 break;
5583 }
5584 else if (new_x > it->first_visible_x)
5585 {
5586 /* Glyph is visible. Increment number of glyphs that
5587 would be displayed. */
5588 ++it->hpos;
5589 }
5590 else
5591 {
5592 /* Glyph is completely off the left margin of the display
5593 area. Nothing to do. */
5594 }
5595 }
5596
5597 if (result != MOVE_UNDEFINED)
5598 break;
5599 }
5600 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5601 {
5602 /* Stop when TO_X specified and reached. This check is
5603 necessary here because of lines consisting of a line end,
5604 only. The line end will not produce any glyphs and we
5605 would never get MOVE_X_REACHED. */
5606 xassert (it->nglyphs == 0);
5607 result = MOVE_X_REACHED;
5608 break;
5609 }
5610
5611 /* Is this a line end? If yes, we're done. */
5612 if (ITERATOR_AT_END_OF_LINE_P (it))
5613 {
5614 result = MOVE_NEWLINE_OR_CR;
5615 break;
5616 }
5617
5618 /* The current display element has been consumed. Advance
5619 to the next. */
5620 set_iterator_to_next (it, 1);
5621
5622 /* Stop if lines are truncated and IT's current x-position is
5623 past the right edge of the window now. */
5624 if (it->truncate_lines_p
5625 && it->current_x >= it->last_visible_x)
5626 {
5627 result = MOVE_LINE_TRUNCATED;
5628 break;
5629 }
5630 }
5631
5632 /* Restore the iterator settings altered at the beginning of this
5633 function. */
5634 it->glyph_row = saved_glyph_row;
5635 return result;
5636 }
5637
5638
5639 /* Move IT forward until it satisfies one or more of the criteria in
5640 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5641
5642 OP is a bit-mask that specifies where to stop, and in particular,
5643 which of those four position arguments makes a difference. See the
5644 description of enum move_operation_enum.
5645
5646 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5647 screen line, this function will set IT to the next position >
5648 TO_CHARPOS. */
5649
5650 void
5651 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5652 struct it *it;
5653 int to_charpos, to_x, to_y, to_vpos;
5654 int op;
5655 {
5656 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5657 int line_height;
5658 int reached = 0;
5659
5660 for (;;)
5661 {
5662 if (op & MOVE_TO_VPOS)
5663 {
5664 /* If no TO_CHARPOS and no TO_X specified, stop at the
5665 start of the line TO_VPOS. */
5666 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5667 {
5668 if (it->vpos == to_vpos)
5669 {
5670 reached = 1;
5671 break;
5672 }
5673 else
5674 skip = move_it_in_display_line_to (it, -1, -1, 0);
5675 }
5676 else
5677 {
5678 /* TO_VPOS >= 0 means stop at TO_X in the line at
5679 TO_VPOS, or at TO_POS, whichever comes first. */
5680 if (it->vpos == to_vpos)
5681 {
5682 reached = 2;
5683 break;
5684 }
5685
5686 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5687
5688 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5689 {
5690 reached = 3;
5691 break;
5692 }
5693 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5694 {
5695 /* We have reached TO_X but not in the line we want. */
5696 skip = move_it_in_display_line_to (it, to_charpos,
5697 -1, MOVE_TO_POS);
5698 if (skip == MOVE_POS_MATCH_OR_ZV)
5699 {
5700 reached = 4;
5701 break;
5702 }
5703 }
5704 }
5705 }
5706 else if (op & MOVE_TO_Y)
5707 {
5708 struct it it_backup;
5709
5710 /* TO_Y specified means stop at TO_X in the line containing
5711 TO_Y---or at TO_CHARPOS if this is reached first. The
5712 problem is that we can't really tell whether the line
5713 contains TO_Y before we have completely scanned it, and
5714 this may skip past TO_X. What we do is to first scan to
5715 TO_X.
5716
5717 If TO_X is not specified, use a TO_X of zero. The reason
5718 is to make the outcome of this function more predictable.
5719 If we didn't use TO_X == 0, we would stop at the end of
5720 the line which is probably not what a caller would expect
5721 to happen. */
5722 skip = move_it_in_display_line_to (it, to_charpos,
5723 ((op & MOVE_TO_X)
5724 ? to_x : 0),
5725 (MOVE_TO_X
5726 | (op & MOVE_TO_POS)));
5727
5728 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5729 if (skip == MOVE_POS_MATCH_OR_ZV)
5730 {
5731 reached = 5;
5732 break;
5733 }
5734
5735 /* If TO_X was reached, we would like to know whether TO_Y
5736 is in the line. This can only be said if we know the
5737 total line height which requires us to scan the rest of
5738 the line. */
5739 if (skip == MOVE_X_REACHED)
5740 {
5741 it_backup = *it;
5742 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5743 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5744 op & MOVE_TO_POS);
5745 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5746 }
5747
5748 /* Now, decide whether TO_Y is in this line. */
5749 line_height = it->max_ascent + it->max_descent;
5750 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5751
5752 if (to_y >= it->current_y
5753 && to_y < it->current_y + line_height)
5754 {
5755 if (skip == MOVE_X_REACHED)
5756 /* If TO_Y is in this line and TO_X was reached above,
5757 we scanned too far. We have to restore IT's settings
5758 to the ones before skipping. */
5759 *it = it_backup;
5760 reached = 6;
5761 }
5762 else if (skip == MOVE_X_REACHED)
5763 {
5764 skip = skip2;
5765 if (skip == MOVE_POS_MATCH_OR_ZV)
5766 reached = 7;
5767 }
5768
5769 if (reached)
5770 break;
5771 }
5772 else
5773 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5774
5775 switch (skip)
5776 {
5777 case MOVE_POS_MATCH_OR_ZV:
5778 reached = 8;
5779 goto out;
5780
5781 case MOVE_NEWLINE_OR_CR:
5782 set_iterator_to_next (it, 1);
5783 it->continuation_lines_width = 0;
5784 break;
5785
5786 case MOVE_LINE_TRUNCATED:
5787 it->continuation_lines_width = 0;
5788 reseat_at_next_visible_line_start (it, 0);
5789 if ((op & MOVE_TO_POS) != 0
5790 && IT_CHARPOS (*it) > to_charpos)
5791 {
5792 reached = 9;
5793 goto out;
5794 }
5795 break;
5796
5797 case MOVE_LINE_CONTINUED:
5798 it->continuation_lines_width += it->current_x;
5799 break;
5800
5801 default:
5802 abort ();
5803 }
5804
5805 /* Reset/increment for the next run. */
5806 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5807 it->current_x = it->hpos = 0;
5808 it->current_y += it->max_ascent + it->max_descent;
5809 ++it->vpos;
5810 last_height = it->max_ascent + it->max_descent;
5811 last_max_ascent = it->max_ascent;
5812 it->max_ascent = it->max_descent = 0;
5813 }
5814
5815 out:
5816
5817 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5818 }
5819
5820
5821 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5822
5823 If DY > 0, move IT backward at least that many pixels. DY = 0
5824 means move IT backward to the preceding line start or BEGV. This
5825 function may move over more than DY pixels if IT->current_y - DY
5826 ends up in the middle of a line; in this case IT->current_y will be
5827 set to the top of the line moved to. */
5828
5829 void
5830 move_it_vertically_backward (it, dy)
5831 struct it *it;
5832 int dy;
5833 {
5834 int nlines, h;
5835 struct it it2, it3;
5836 int start_pos = IT_CHARPOS (*it);
5837
5838 xassert (dy >= 0);
5839
5840 /* Estimate how many newlines we must move back. */
5841 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
5842
5843 /* Set the iterator's position that many lines back. */
5844 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5845 back_to_previous_visible_line_start (it);
5846
5847 /* Reseat the iterator here. When moving backward, we don't want
5848 reseat to skip forward over invisible text, set up the iterator
5849 to deliver from overlay strings at the new position etc. So,
5850 use reseat_1 here. */
5851 reseat_1 (it, it->current.pos, 1);
5852
5853 /* We are now surely at a line start. */
5854 it->current_x = it->hpos = 0;
5855 it->continuation_lines_width = 0;
5856
5857 /* Move forward and see what y-distance we moved. First move to the
5858 start of the next line so that we get its height. We need this
5859 height to be able to tell whether we reached the specified
5860 y-distance. */
5861 it2 = *it;
5862 it2.max_ascent = it2.max_descent = 0;
5863 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5864 MOVE_TO_POS | MOVE_TO_VPOS);
5865 xassert (IT_CHARPOS (*it) >= BEGV);
5866 it3 = it2;
5867
5868 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5869 xassert (IT_CHARPOS (*it) >= BEGV);
5870 /* H is the actual vertical distance from the position in *IT
5871 and the starting position. */
5872 h = it2.current_y - it->current_y;
5873 /* NLINES is the distance in number of lines. */
5874 nlines = it2.vpos - it->vpos;
5875
5876 /* Correct IT's y and vpos position
5877 so that they are relative to the starting point. */
5878 it->vpos -= nlines;
5879 it->current_y -= h;
5880
5881 if (dy == 0)
5882 {
5883 /* DY == 0 means move to the start of the screen line. The
5884 value of nlines is > 0 if continuation lines were involved. */
5885 if (nlines > 0)
5886 move_it_by_lines (it, nlines, 1);
5887 xassert (IT_CHARPOS (*it) <= start_pos);
5888 }
5889 else
5890 {
5891 /* The y-position we try to reach, relative to *IT.
5892 Note that H has been subtracted in front of the if-statement. */
5893 int target_y = it->current_y + h - dy;
5894 int y0 = it3.current_y;
5895 int y1 = line_bottom_y (&it3);
5896 int line_height = y1 - y0;
5897
5898 /* If we did not reach target_y, try to move further backward if
5899 we can. If we moved too far backward, try to move forward. */
5900 if (target_y < it->current_y
5901 /* This is heuristic. In a window that's 3 lines high, with
5902 a line height of 13 pixels each, recentering with point
5903 on the bottom line will try to move -39/2 = 19 pixels
5904 backward. Try to avoid moving into the first line. */
5905 && it->current_y - target_y > line_height / 3 * 2
5906 && IT_CHARPOS (*it) > BEGV)
5907 {
5908 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
5909 target_y - it->current_y));
5910 move_it_vertically (it, target_y - it->current_y);
5911 xassert (IT_CHARPOS (*it) >= BEGV);
5912 }
5913 else if (target_y >= it->current_y + line_height
5914 && IT_CHARPOS (*it) < ZV)
5915 {
5916 /* Should move forward by at least one line, maybe more.
5917
5918 Note: Calling move_it_by_lines can be expensive on
5919 terminal frames, where compute_motion is used (via
5920 vmotion) to do the job, when there are very long lines
5921 and truncate-lines is nil. That's the reason for
5922 treating terminal frames specially here. */
5923
5924 if (!FRAME_WINDOW_P (it->f))
5925 move_it_vertically (it, target_y - (it->current_y + line_height));
5926 else
5927 {
5928 do
5929 {
5930 move_it_by_lines (it, 1, 1);
5931 }
5932 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
5933 }
5934
5935 xassert (IT_CHARPOS (*it) >= BEGV);
5936 }
5937 }
5938 }
5939
5940
5941 /* Move IT by a specified amount of pixel lines DY. DY negative means
5942 move backwards. DY = 0 means move to start of screen line. At the
5943 end, IT will be on the start of a screen line. */
5944
5945 void
5946 move_it_vertically (it, dy)
5947 struct it *it;
5948 int dy;
5949 {
5950 if (dy <= 0)
5951 move_it_vertically_backward (it, -dy);
5952 else if (dy > 0)
5953 {
5954 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5955 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5956 MOVE_TO_POS | MOVE_TO_Y);
5957 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5958
5959 /* If buffer ends in ZV without a newline, move to the start of
5960 the line to satisfy the post-condition. */
5961 if (IT_CHARPOS (*it) == ZV
5962 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5963 move_it_by_lines (it, 0, 0);
5964 }
5965 }
5966
5967
5968 /* Move iterator IT past the end of the text line it is in. */
5969
5970 void
5971 move_it_past_eol (it)
5972 struct it *it;
5973 {
5974 enum move_it_result rc;
5975
5976 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5977 if (rc == MOVE_NEWLINE_OR_CR)
5978 set_iterator_to_next (it, 0);
5979 }
5980
5981
5982 #if 0 /* Currently not used. */
5983
5984 /* Return non-zero if some text between buffer positions START_CHARPOS
5985 and END_CHARPOS is invisible. IT->window is the window for text
5986 property lookup. */
5987
5988 static int
5989 invisible_text_between_p (it, start_charpos, end_charpos)
5990 struct it *it;
5991 int start_charpos, end_charpos;
5992 {
5993 Lisp_Object prop, limit;
5994 int invisible_found_p;
5995
5996 xassert (it != NULL && start_charpos <= end_charpos);
5997
5998 /* Is text at START invisible? */
5999 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6000 it->window);
6001 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6002 invisible_found_p = 1;
6003 else
6004 {
6005 limit = Fnext_single_char_property_change (make_number (start_charpos),
6006 Qinvisible, Qnil,
6007 make_number (end_charpos));
6008 invisible_found_p = XFASTINT (limit) < end_charpos;
6009 }
6010
6011 return invisible_found_p;
6012 }
6013
6014 #endif /* 0 */
6015
6016
6017 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6018 negative means move up. DVPOS == 0 means move to the start of the
6019 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6020 NEED_Y_P is zero, IT->current_y will be left unchanged.
6021
6022 Further optimization ideas: If we would know that IT->f doesn't use
6023 a face with proportional font, we could be faster for
6024 truncate-lines nil. */
6025
6026 void
6027 move_it_by_lines (it, dvpos, need_y_p)
6028 struct it *it;
6029 int dvpos, need_y_p;
6030 {
6031 struct position pos;
6032
6033 if (!FRAME_WINDOW_P (it->f))
6034 {
6035 struct text_pos textpos;
6036
6037 /* We can use vmotion on frames without proportional fonts. */
6038 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6039 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6040 reseat (it, textpos, 1);
6041 it->vpos += pos.vpos;
6042 it->current_y += pos.vpos;
6043 }
6044 else if (dvpos == 0)
6045 {
6046 /* DVPOS == 0 means move to the start of the screen line. */
6047 move_it_vertically_backward (it, 0);
6048 xassert (it->current_x == 0 && it->hpos == 0);
6049 }
6050 else if (dvpos > 0)
6051 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6052 else
6053 {
6054 struct it it2;
6055 int start_charpos, i;
6056
6057 /* Start at the beginning of the screen line containing IT's
6058 position. */
6059 move_it_vertically_backward (it, 0);
6060
6061 /* Go back -DVPOS visible lines and reseat the iterator there. */
6062 start_charpos = IT_CHARPOS (*it);
6063 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6064 back_to_previous_visible_line_start (it);
6065 reseat (it, it->current.pos, 1);
6066 it->current_x = it->hpos = 0;
6067
6068 /* Above call may have moved too far if continuation lines
6069 are involved. Scan forward and see if it did. */
6070 it2 = *it;
6071 it2.vpos = it2.current_y = 0;
6072 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6073 it->vpos -= it2.vpos;
6074 it->current_y -= it2.current_y;
6075 it->current_x = it->hpos = 0;
6076
6077 /* If we moved too far, move IT some lines forward. */
6078 if (it2.vpos > -dvpos)
6079 {
6080 int delta = it2.vpos + dvpos;
6081 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6082 }
6083 }
6084 }
6085
6086 /* Return 1 if IT points into the middle of a display vector. */
6087
6088 int
6089 in_display_vector_p (it)
6090 struct it *it;
6091 {
6092 return (it->method == next_element_from_display_vector
6093 && it->current.dpvec_index > 0
6094 && it->dpvec + it->current.dpvec_index != it->dpend);
6095 }
6096
6097 \f
6098 /***********************************************************************
6099 Messages
6100 ***********************************************************************/
6101
6102
6103 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6104 to *Messages*. */
6105
6106 void
6107 add_to_log (format, arg1, arg2)
6108 char *format;
6109 Lisp_Object arg1, arg2;
6110 {
6111 Lisp_Object args[3];
6112 Lisp_Object msg, fmt;
6113 char *buffer;
6114 int len;
6115 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6116
6117 /* Do nothing if called asynchronously. Inserting text into
6118 a buffer may call after-change-functions and alike and
6119 that would means running Lisp asynchronously. */
6120 if (handling_signal)
6121 return;
6122
6123 fmt = msg = Qnil;
6124 GCPRO4 (fmt, msg, arg1, arg2);
6125
6126 args[0] = fmt = build_string (format);
6127 args[1] = arg1;
6128 args[2] = arg2;
6129 msg = Fformat (3, args);
6130
6131 len = SBYTES (msg) + 1;
6132 buffer = (char *) alloca (len);
6133 bcopy (SDATA (msg), buffer, len);
6134
6135 message_dolog (buffer, len - 1, 1, 0);
6136 UNGCPRO;
6137 }
6138
6139
6140 /* Output a newline in the *Messages* buffer if "needs" one. */
6141
6142 void
6143 message_log_maybe_newline ()
6144 {
6145 if (message_log_need_newline)
6146 message_dolog ("", 0, 1, 0);
6147 }
6148
6149
6150 /* Add a string M of length NBYTES to the message log, optionally
6151 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6152 nonzero, means interpret the contents of M as multibyte. This
6153 function calls low-level routines in order to bypass text property
6154 hooks, etc. which might not be safe to run. */
6155
6156 void
6157 message_dolog (m, nbytes, nlflag, multibyte)
6158 const char *m;
6159 int nbytes, nlflag, multibyte;
6160 {
6161 if (!NILP (Vmemory_full))
6162 return;
6163
6164 if (!NILP (Vmessage_log_max))
6165 {
6166 struct buffer *oldbuf;
6167 Lisp_Object oldpoint, oldbegv, oldzv;
6168 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6169 int point_at_end = 0;
6170 int zv_at_end = 0;
6171 Lisp_Object old_deactivate_mark, tem;
6172 struct gcpro gcpro1;
6173
6174 old_deactivate_mark = Vdeactivate_mark;
6175 oldbuf = current_buffer;
6176 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6177 current_buffer->undo_list = Qt;
6178
6179 oldpoint = message_dolog_marker1;
6180 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6181 oldbegv = message_dolog_marker2;
6182 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6183 oldzv = message_dolog_marker3;
6184 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6185 GCPRO1 (old_deactivate_mark);
6186
6187 if (PT == Z)
6188 point_at_end = 1;
6189 if (ZV == Z)
6190 zv_at_end = 1;
6191
6192 BEGV = BEG;
6193 BEGV_BYTE = BEG_BYTE;
6194 ZV = Z;
6195 ZV_BYTE = Z_BYTE;
6196 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6197
6198 /* Insert the string--maybe converting multibyte to single byte
6199 or vice versa, so that all the text fits the buffer. */
6200 if (multibyte
6201 && NILP (current_buffer->enable_multibyte_characters))
6202 {
6203 int i, c, char_bytes;
6204 unsigned char work[1];
6205
6206 /* Convert a multibyte string to single-byte
6207 for the *Message* buffer. */
6208 for (i = 0; i < nbytes; i += char_bytes)
6209 {
6210 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6211 work[0] = (SINGLE_BYTE_CHAR_P (c)
6212 ? c
6213 : multibyte_char_to_unibyte (c, Qnil));
6214 insert_1_both (work, 1, 1, 1, 0, 0);
6215 }
6216 }
6217 else if (! multibyte
6218 && ! NILP (current_buffer->enable_multibyte_characters))
6219 {
6220 int i, c, char_bytes;
6221 unsigned char *msg = (unsigned char *) m;
6222 unsigned char str[MAX_MULTIBYTE_LENGTH];
6223 /* Convert a single-byte string to multibyte
6224 for the *Message* buffer. */
6225 for (i = 0; i < nbytes; i++)
6226 {
6227 c = unibyte_char_to_multibyte (msg[i]);
6228 char_bytes = CHAR_STRING (c, str);
6229 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6230 }
6231 }
6232 else if (nbytes)
6233 insert_1 (m, nbytes, 1, 0, 0);
6234
6235 if (nlflag)
6236 {
6237 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6238 insert_1 ("\n", 1, 1, 0, 0);
6239
6240 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6241 this_bol = PT;
6242 this_bol_byte = PT_BYTE;
6243
6244 /* See if this line duplicates the previous one.
6245 If so, combine duplicates. */
6246 if (this_bol > BEG)
6247 {
6248 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6249 prev_bol = PT;
6250 prev_bol_byte = PT_BYTE;
6251
6252 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6253 this_bol, this_bol_byte);
6254 if (dup)
6255 {
6256 del_range_both (prev_bol, prev_bol_byte,
6257 this_bol, this_bol_byte, 0);
6258 if (dup > 1)
6259 {
6260 char dupstr[40];
6261 int duplen;
6262
6263 /* If you change this format, don't forget to also
6264 change message_log_check_duplicate. */
6265 sprintf (dupstr, " [%d times]", dup);
6266 duplen = strlen (dupstr);
6267 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6268 insert_1 (dupstr, duplen, 1, 0, 1);
6269 }
6270 }
6271 }
6272
6273 /* If we have more than the desired maximum number of lines
6274 in the *Messages* buffer now, delete the oldest ones.
6275 This is safe because we don't have undo in this buffer. */
6276
6277 if (NATNUMP (Vmessage_log_max))
6278 {
6279 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6280 -XFASTINT (Vmessage_log_max) - 1, 0);
6281 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6282 }
6283 }
6284 BEGV = XMARKER (oldbegv)->charpos;
6285 BEGV_BYTE = marker_byte_position (oldbegv);
6286
6287 if (zv_at_end)
6288 {
6289 ZV = Z;
6290 ZV_BYTE = Z_BYTE;
6291 }
6292 else
6293 {
6294 ZV = XMARKER (oldzv)->charpos;
6295 ZV_BYTE = marker_byte_position (oldzv);
6296 }
6297
6298 if (point_at_end)
6299 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6300 else
6301 /* We can't do Fgoto_char (oldpoint) because it will run some
6302 Lisp code. */
6303 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6304 XMARKER (oldpoint)->bytepos);
6305
6306 UNGCPRO;
6307 unchain_marker (XMARKER (oldpoint));
6308 unchain_marker (XMARKER (oldbegv));
6309 unchain_marker (XMARKER (oldzv));
6310
6311 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6312 set_buffer_internal (oldbuf);
6313 if (NILP (tem))
6314 windows_or_buffers_changed = old_windows_or_buffers_changed;
6315 message_log_need_newline = !nlflag;
6316 Vdeactivate_mark = old_deactivate_mark;
6317 }
6318 }
6319
6320
6321 /* We are at the end of the buffer after just having inserted a newline.
6322 (Note: We depend on the fact we won't be crossing the gap.)
6323 Check to see if the most recent message looks a lot like the previous one.
6324 Return 0 if different, 1 if the new one should just replace it, or a
6325 value N > 1 if we should also append " [N times]". */
6326
6327 static int
6328 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6329 int prev_bol, this_bol;
6330 int prev_bol_byte, this_bol_byte;
6331 {
6332 int i;
6333 int len = Z_BYTE - 1 - this_bol_byte;
6334 int seen_dots = 0;
6335 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6336 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6337
6338 for (i = 0; i < len; i++)
6339 {
6340 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6341 seen_dots = 1;
6342 if (p1[i] != p2[i])
6343 return seen_dots;
6344 }
6345 p1 += len;
6346 if (*p1 == '\n')
6347 return 2;
6348 if (*p1++ == ' ' && *p1++ == '[')
6349 {
6350 int n = 0;
6351 while (*p1 >= '0' && *p1 <= '9')
6352 n = n * 10 + *p1++ - '0';
6353 if (strncmp (p1, " times]\n", 8) == 0)
6354 return n+1;
6355 }
6356 return 0;
6357 }
6358
6359
6360 /* Display an echo area message M with a specified length of NBYTES
6361 bytes. The string may include null characters. If M is 0, clear
6362 out any existing message, and let the mini-buffer text show
6363 through.
6364
6365 The buffer M must continue to exist until after the echo area gets
6366 cleared or some other message gets displayed there. This means do
6367 not pass text that is stored in a Lisp string; do not pass text in
6368 a buffer that was alloca'd. */
6369
6370 void
6371 message2 (m, nbytes, multibyte)
6372 const char *m;
6373 int nbytes;
6374 int multibyte;
6375 {
6376 /* First flush out any partial line written with print. */
6377 message_log_maybe_newline ();
6378 if (m)
6379 message_dolog (m, nbytes, 1, multibyte);
6380 message2_nolog (m, nbytes, multibyte);
6381 }
6382
6383
6384 /* The non-logging counterpart of message2. */
6385
6386 void
6387 message2_nolog (m, nbytes, multibyte)
6388 const char *m;
6389 int nbytes, multibyte;
6390 {
6391 struct frame *sf = SELECTED_FRAME ();
6392 message_enable_multibyte = multibyte;
6393
6394 if (noninteractive)
6395 {
6396 if (noninteractive_need_newline)
6397 putc ('\n', stderr);
6398 noninteractive_need_newline = 0;
6399 if (m)
6400 fwrite (m, nbytes, 1, stderr);
6401 if (cursor_in_echo_area == 0)
6402 fprintf (stderr, "\n");
6403 fflush (stderr);
6404 }
6405 /* A null message buffer means that the frame hasn't really been
6406 initialized yet. Error messages get reported properly by
6407 cmd_error, so this must be just an informative message; toss it. */
6408 else if (INTERACTIVE
6409 && sf->glyphs_initialized_p
6410 && FRAME_MESSAGE_BUF (sf))
6411 {
6412 Lisp_Object mini_window;
6413 struct frame *f;
6414
6415 /* Get the frame containing the mini-buffer
6416 that the selected frame is using. */
6417 mini_window = FRAME_MINIBUF_WINDOW (sf);
6418 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6419
6420 FRAME_SAMPLE_VISIBILITY (f);
6421 if (FRAME_VISIBLE_P (sf)
6422 && ! FRAME_VISIBLE_P (f))
6423 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6424
6425 if (m)
6426 {
6427 set_message (m, Qnil, nbytes, multibyte);
6428 if (minibuffer_auto_raise)
6429 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6430 }
6431 else
6432 clear_message (1, 1);
6433
6434 do_pending_window_change (0);
6435 echo_area_display (1);
6436 do_pending_window_change (0);
6437 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6438 (*frame_up_to_date_hook) (f);
6439 }
6440 }
6441
6442
6443 /* Display an echo area message M with a specified length of NBYTES
6444 bytes. The string may include null characters. If M is not a
6445 string, clear out any existing message, and let the mini-buffer
6446 text show through. */
6447
6448 void
6449 message3 (m, nbytes, multibyte)
6450 Lisp_Object m;
6451 int nbytes;
6452 int multibyte;
6453 {
6454 struct gcpro gcpro1;
6455
6456 GCPRO1 (m);
6457
6458 /* First flush out any partial line written with print. */
6459 message_log_maybe_newline ();
6460 if (STRINGP (m))
6461 message_dolog (SDATA (m), nbytes, 1, multibyte);
6462 message3_nolog (m, nbytes, multibyte);
6463
6464 UNGCPRO;
6465 }
6466
6467
6468 /* The non-logging version of message3. */
6469
6470 void
6471 message3_nolog (m, nbytes, multibyte)
6472 Lisp_Object m;
6473 int nbytes, multibyte;
6474 {
6475 struct frame *sf = SELECTED_FRAME ();
6476 message_enable_multibyte = multibyte;
6477
6478 if (noninteractive)
6479 {
6480 if (noninteractive_need_newline)
6481 putc ('\n', stderr);
6482 noninteractive_need_newline = 0;
6483 if (STRINGP (m))
6484 fwrite (SDATA (m), nbytes, 1, stderr);
6485 if (cursor_in_echo_area == 0)
6486 fprintf (stderr, "\n");
6487 fflush (stderr);
6488 }
6489 /* A null message buffer means that the frame hasn't really been
6490 initialized yet. Error messages get reported properly by
6491 cmd_error, so this must be just an informative message; toss it. */
6492 else if (INTERACTIVE
6493 && sf->glyphs_initialized_p
6494 && FRAME_MESSAGE_BUF (sf))
6495 {
6496 Lisp_Object mini_window;
6497 Lisp_Object frame;
6498 struct frame *f;
6499
6500 /* Get the frame containing the mini-buffer
6501 that the selected frame is using. */
6502 mini_window = FRAME_MINIBUF_WINDOW (sf);
6503 frame = XWINDOW (mini_window)->frame;
6504 f = XFRAME (frame);
6505
6506 FRAME_SAMPLE_VISIBILITY (f);
6507 if (FRAME_VISIBLE_P (sf)
6508 && !FRAME_VISIBLE_P (f))
6509 Fmake_frame_visible (frame);
6510
6511 if (STRINGP (m) && SCHARS (m) > 0)
6512 {
6513 set_message (NULL, m, nbytes, multibyte);
6514 if (minibuffer_auto_raise)
6515 Fraise_frame (frame);
6516 }
6517 else
6518 clear_message (1, 1);
6519
6520 do_pending_window_change (0);
6521 echo_area_display (1);
6522 do_pending_window_change (0);
6523 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6524 (*frame_up_to_date_hook) (f);
6525 }
6526 }
6527
6528
6529 /* Display a null-terminated echo area message M. If M is 0, clear
6530 out any existing message, and let the mini-buffer text show through.
6531
6532 The buffer M must continue to exist until after the echo area gets
6533 cleared or some other message gets displayed there. Do not pass
6534 text that is stored in a Lisp string. Do not pass text in a buffer
6535 that was alloca'd. */
6536
6537 void
6538 message1 (m)
6539 char *m;
6540 {
6541 message2 (m, (m ? strlen (m) : 0), 0);
6542 }
6543
6544
6545 /* The non-logging counterpart of message1. */
6546
6547 void
6548 message1_nolog (m)
6549 char *m;
6550 {
6551 message2_nolog (m, (m ? strlen (m) : 0), 0);
6552 }
6553
6554 /* Display a message M which contains a single %s
6555 which gets replaced with STRING. */
6556
6557 void
6558 message_with_string (m, string, log)
6559 char *m;
6560 Lisp_Object string;
6561 int log;
6562 {
6563 CHECK_STRING (string);
6564
6565 if (noninteractive)
6566 {
6567 if (m)
6568 {
6569 if (noninteractive_need_newline)
6570 putc ('\n', stderr);
6571 noninteractive_need_newline = 0;
6572 fprintf (stderr, m, SDATA (string));
6573 if (cursor_in_echo_area == 0)
6574 fprintf (stderr, "\n");
6575 fflush (stderr);
6576 }
6577 }
6578 else if (INTERACTIVE)
6579 {
6580 /* The frame whose minibuffer we're going to display the message on.
6581 It may be larger than the selected frame, so we need
6582 to use its buffer, not the selected frame's buffer. */
6583 Lisp_Object mini_window;
6584 struct frame *f, *sf = SELECTED_FRAME ();
6585
6586 /* Get the frame containing the minibuffer
6587 that the selected frame is using. */
6588 mini_window = FRAME_MINIBUF_WINDOW (sf);
6589 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6590
6591 /* A null message buffer means that the frame hasn't really been
6592 initialized yet. Error messages get reported properly by
6593 cmd_error, so this must be just an informative message; toss it. */
6594 if (FRAME_MESSAGE_BUF (f))
6595 {
6596 Lisp_Object args[2], message;
6597 struct gcpro gcpro1, gcpro2;
6598
6599 args[0] = build_string (m);
6600 args[1] = message = string;
6601 GCPRO2 (args[0], message);
6602 gcpro1.nvars = 2;
6603
6604 message = Fformat (2, args);
6605
6606 if (log)
6607 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6608 else
6609 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6610
6611 UNGCPRO;
6612
6613 /* Print should start at the beginning of the message
6614 buffer next time. */
6615 message_buf_print = 0;
6616 }
6617 }
6618 }
6619
6620
6621 /* Dump an informative message to the minibuf. If M is 0, clear out
6622 any existing message, and let the mini-buffer text show through. */
6623
6624 /* VARARGS 1 */
6625 void
6626 message (m, a1, a2, a3)
6627 char *m;
6628 EMACS_INT a1, a2, a3;
6629 {
6630 if (noninteractive)
6631 {
6632 if (m)
6633 {
6634 if (noninteractive_need_newline)
6635 putc ('\n', stderr);
6636 noninteractive_need_newline = 0;
6637 fprintf (stderr, m, a1, a2, a3);
6638 if (cursor_in_echo_area == 0)
6639 fprintf (stderr, "\n");
6640 fflush (stderr);
6641 }
6642 }
6643 else if (INTERACTIVE)
6644 {
6645 /* The frame whose mini-buffer we're going to display the message
6646 on. It may be larger than the selected frame, so we need to
6647 use its buffer, not the selected frame's buffer. */
6648 Lisp_Object mini_window;
6649 struct frame *f, *sf = SELECTED_FRAME ();
6650
6651 /* Get the frame containing the mini-buffer
6652 that the selected frame is using. */
6653 mini_window = FRAME_MINIBUF_WINDOW (sf);
6654 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6655
6656 /* A null message buffer means that the frame hasn't really been
6657 initialized yet. Error messages get reported properly by
6658 cmd_error, so this must be just an informative message; toss
6659 it. */
6660 if (FRAME_MESSAGE_BUF (f))
6661 {
6662 if (m)
6663 {
6664 int len;
6665 #ifdef NO_ARG_ARRAY
6666 char *a[3];
6667 a[0] = (char *) a1;
6668 a[1] = (char *) a2;
6669 a[2] = (char *) a3;
6670
6671 len = doprnt (FRAME_MESSAGE_BUF (f),
6672 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6673 #else
6674 len = doprnt (FRAME_MESSAGE_BUF (f),
6675 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6676 (char **) &a1);
6677 #endif /* NO_ARG_ARRAY */
6678
6679 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6680 }
6681 else
6682 message1 (0);
6683
6684 /* Print should start at the beginning of the message
6685 buffer next time. */
6686 message_buf_print = 0;
6687 }
6688 }
6689 }
6690
6691
6692 /* The non-logging version of message. */
6693
6694 void
6695 message_nolog (m, a1, a2, a3)
6696 char *m;
6697 EMACS_INT a1, a2, a3;
6698 {
6699 Lisp_Object old_log_max;
6700 old_log_max = Vmessage_log_max;
6701 Vmessage_log_max = Qnil;
6702 message (m, a1, a2, a3);
6703 Vmessage_log_max = old_log_max;
6704 }
6705
6706
6707 /* Display the current message in the current mini-buffer. This is
6708 only called from error handlers in process.c, and is not time
6709 critical. */
6710
6711 void
6712 update_echo_area ()
6713 {
6714 if (!NILP (echo_area_buffer[0]))
6715 {
6716 Lisp_Object string;
6717 string = Fcurrent_message ();
6718 message3 (string, SBYTES (string),
6719 !NILP (current_buffer->enable_multibyte_characters));
6720 }
6721 }
6722
6723
6724 /* Make sure echo area buffers in `echo_buffers' are live.
6725 If they aren't, make new ones. */
6726
6727 static void
6728 ensure_echo_area_buffers ()
6729 {
6730 int i;
6731
6732 for (i = 0; i < 2; ++i)
6733 if (!BUFFERP (echo_buffer[i])
6734 || NILP (XBUFFER (echo_buffer[i])->name))
6735 {
6736 char name[30];
6737 Lisp_Object old_buffer;
6738 int j;
6739
6740 old_buffer = echo_buffer[i];
6741 sprintf (name, " *Echo Area %d*", i);
6742 echo_buffer[i] = Fget_buffer_create (build_string (name));
6743 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6744
6745 for (j = 0; j < 2; ++j)
6746 if (EQ (old_buffer, echo_area_buffer[j]))
6747 echo_area_buffer[j] = echo_buffer[i];
6748 }
6749 }
6750
6751
6752 /* Call FN with args A1..A4 with either the current or last displayed
6753 echo_area_buffer as current buffer.
6754
6755 WHICH zero means use the current message buffer
6756 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6757 from echo_buffer[] and clear it.
6758
6759 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6760 suitable buffer from echo_buffer[] and clear it.
6761
6762 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6763 that the current message becomes the last displayed one, make
6764 choose a suitable buffer for echo_area_buffer[0], and clear it.
6765
6766 Value is what FN returns. */
6767
6768 static int
6769 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6770 struct window *w;
6771 int which;
6772 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6773 EMACS_INT a1;
6774 Lisp_Object a2;
6775 EMACS_INT a3, a4;
6776 {
6777 Lisp_Object buffer;
6778 int this_one, the_other, clear_buffer_p, rc;
6779 int count = SPECPDL_INDEX ();
6780
6781 /* If buffers aren't live, make new ones. */
6782 ensure_echo_area_buffers ();
6783
6784 clear_buffer_p = 0;
6785
6786 if (which == 0)
6787 this_one = 0, the_other = 1;
6788 else if (which > 0)
6789 this_one = 1, the_other = 0;
6790 else
6791 {
6792 this_one = 0, the_other = 1;
6793 clear_buffer_p = 1;
6794
6795 /* We need a fresh one in case the current echo buffer equals
6796 the one containing the last displayed echo area message. */
6797 if (!NILP (echo_area_buffer[this_one])
6798 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6799 echo_area_buffer[this_one] = Qnil;
6800 }
6801
6802 /* Choose a suitable buffer from echo_buffer[] is we don't
6803 have one. */
6804 if (NILP (echo_area_buffer[this_one]))
6805 {
6806 echo_area_buffer[this_one]
6807 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6808 ? echo_buffer[the_other]
6809 : echo_buffer[this_one]);
6810 clear_buffer_p = 1;
6811 }
6812
6813 buffer = echo_area_buffer[this_one];
6814
6815 /* Don't get confused by reusing the buffer used for echoing
6816 for a different purpose. */
6817 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
6818 cancel_echoing ();
6819
6820 record_unwind_protect (unwind_with_echo_area_buffer,
6821 with_echo_area_buffer_unwind_data (w));
6822
6823 /* Make the echo area buffer current. Note that for display
6824 purposes, it is not necessary that the displayed window's buffer
6825 == current_buffer, except for text property lookup. So, let's
6826 only set that buffer temporarily here without doing a full
6827 Fset_window_buffer. We must also change w->pointm, though,
6828 because otherwise an assertions in unshow_buffer fails, and Emacs
6829 aborts. */
6830 set_buffer_internal_1 (XBUFFER (buffer));
6831 if (w)
6832 {
6833 w->buffer = buffer;
6834 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6835 }
6836
6837 current_buffer->undo_list = Qt;
6838 current_buffer->read_only = Qnil;
6839 specbind (Qinhibit_read_only, Qt);
6840 specbind (Qinhibit_modification_hooks, Qt);
6841
6842 if (clear_buffer_p && Z > BEG)
6843 del_range (BEG, Z);
6844
6845 xassert (BEGV >= BEG);
6846 xassert (ZV <= Z && ZV >= BEGV);
6847
6848 rc = fn (a1, a2, a3, a4);
6849
6850 xassert (BEGV >= BEG);
6851 xassert (ZV <= Z && ZV >= BEGV);
6852
6853 unbind_to (count, Qnil);
6854 return rc;
6855 }
6856
6857
6858 /* Save state that should be preserved around the call to the function
6859 FN called in with_echo_area_buffer. */
6860
6861 static Lisp_Object
6862 with_echo_area_buffer_unwind_data (w)
6863 struct window *w;
6864 {
6865 int i = 0;
6866 Lisp_Object vector;
6867
6868 /* Reduce consing by keeping one vector in
6869 Vwith_echo_area_save_vector. */
6870 vector = Vwith_echo_area_save_vector;
6871 Vwith_echo_area_save_vector = Qnil;
6872
6873 if (NILP (vector))
6874 vector = Fmake_vector (make_number (7), Qnil);
6875
6876 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6877 AREF (vector, i) = Vdeactivate_mark, ++i;
6878 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6879
6880 if (w)
6881 {
6882 XSETWINDOW (AREF (vector, i), w); ++i;
6883 AREF (vector, i) = w->buffer; ++i;
6884 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6885 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6886 }
6887 else
6888 {
6889 int end = i + 4;
6890 for (; i < end; ++i)
6891 AREF (vector, i) = Qnil;
6892 }
6893
6894 xassert (i == ASIZE (vector));
6895 return vector;
6896 }
6897
6898
6899 /* Restore global state from VECTOR which was created by
6900 with_echo_area_buffer_unwind_data. */
6901
6902 static Lisp_Object
6903 unwind_with_echo_area_buffer (vector)
6904 Lisp_Object vector;
6905 {
6906 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6907 Vdeactivate_mark = AREF (vector, 1);
6908 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6909
6910 if (WINDOWP (AREF (vector, 3)))
6911 {
6912 struct window *w;
6913 Lisp_Object buffer, charpos, bytepos;
6914
6915 w = XWINDOW (AREF (vector, 3));
6916 buffer = AREF (vector, 4);
6917 charpos = AREF (vector, 5);
6918 bytepos = AREF (vector, 6);
6919
6920 w->buffer = buffer;
6921 set_marker_both (w->pointm, buffer,
6922 XFASTINT (charpos), XFASTINT (bytepos));
6923 }
6924
6925 Vwith_echo_area_save_vector = vector;
6926 return Qnil;
6927 }
6928
6929
6930 /* Set up the echo area for use by print functions. MULTIBYTE_P
6931 non-zero means we will print multibyte. */
6932
6933 void
6934 setup_echo_area_for_printing (multibyte_p)
6935 int multibyte_p;
6936 {
6937 /* If we can't find an echo area any more, exit. */
6938 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
6939 Fkill_emacs (Qnil);
6940
6941 ensure_echo_area_buffers ();
6942
6943 if (!message_buf_print)
6944 {
6945 /* A message has been output since the last time we printed.
6946 Choose a fresh echo area buffer. */
6947 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6948 echo_area_buffer[0] = echo_buffer[1];
6949 else
6950 echo_area_buffer[0] = echo_buffer[0];
6951
6952 /* Switch to that buffer and clear it. */
6953 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6954 current_buffer->truncate_lines = Qnil;
6955
6956 if (Z > BEG)
6957 {
6958 int count = SPECPDL_INDEX ();
6959 specbind (Qinhibit_read_only, Qt);
6960 /* Note that undo recording is always disabled. */
6961 del_range (BEG, Z);
6962 unbind_to (count, Qnil);
6963 }
6964 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6965
6966 /* Set up the buffer for the multibyteness we need. */
6967 if (multibyte_p
6968 != !NILP (current_buffer->enable_multibyte_characters))
6969 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6970
6971 /* Raise the frame containing the echo area. */
6972 if (minibuffer_auto_raise)
6973 {
6974 struct frame *sf = SELECTED_FRAME ();
6975 Lisp_Object mini_window;
6976 mini_window = FRAME_MINIBUF_WINDOW (sf);
6977 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6978 }
6979
6980 message_log_maybe_newline ();
6981 message_buf_print = 1;
6982 }
6983 else
6984 {
6985 if (NILP (echo_area_buffer[0]))
6986 {
6987 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6988 echo_area_buffer[0] = echo_buffer[1];
6989 else
6990 echo_area_buffer[0] = echo_buffer[0];
6991 }
6992
6993 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6994 {
6995 /* Someone switched buffers between print requests. */
6996 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6997 current_buffer->truncate_lines = Qnil;
6998 }
6999 }
7000 }
7001
7002
7003 /* Display an echo area message in window W. Value is non-zero if W's
7004 height is changed. If display_last_displayed_message_p is
7005 non-zero, display the message that was last displayed, otherwise
7006 display the current message. */
7007
7008 static int
7009 display_echo_area (w)
7010 struct window *w;
7011 {
7012 int i, no_message_p, window_height_changed_p, count;
7013
7014 /* Temporarily disable garbage collections while displaying the echo
7015 area. This is done because a GC can print a message itself.
7016 That message would modify the echo area buffer's contents while a
7017 redisplay of the buffer is going on, and seriously confuse
7018 redisplay. */
7019 count = inhibit_garbage_collection ();
7020
7021 /* If there is no message, we must call display_echo_area_1
7022 nevertheless because it resizes the window. But we will have to
7023 reset the echo_area_buffer in question to nil at the end because
7024 with_echo_area_buffer will sets it to an empty buffer. */
7025 i = display_last_displayed_message_p ? 1 : 0;
7026 no_message_p = NILP (echo_area_buffer[i]);
7027
7028 window_height_changed_p
7029 = with_echo_area_buffer (w, display_last_displayed_message_p,
7030 display_echo_area_1,
7031 (EMACS_INT) w, Qnil, 0, 0);
7032
7033 if (no_message_p)
7034 echo_area_buffer[i] = Qnil;
7035
7036 unbind_to (count, Qnil);
7037 return window_height_changed_p;
7038 }
7039
7040
7041 /* Helper for display_echo_area. Display the current buffer which
7042 contains the current echo area message in window W, a mini-window,
7043 a pointer to which is passed in A1. A2..A4 are currently not used.
7044 Change the height of W so that all of the message is displayed.
7045 Value is non-zero if height of W was changed. */
7046
7047 static int
7048 display_echo_area_1 (a1, a2, a3, a4)
7049 EMACS_INT a1;
7050 Lisp_Object a2;
7051 EMACS_INT a3, a4;
7052 {
7053 struct window *w = (struct window *) a1;
7054 Lisp_Object window;
7055 struct text_pos start;
7056 int window_height_changed_p = 0;
7057
7058 /* Do this before displaying, so that we have a large enough glyph
7059 matrix for the display. */
7060 window_height_changed_p = resize_mini_window (w, 0);
7061
7062 /* Display. */
7063 clear_glyph_matrix (w->desired_matrix);
7064 XSETWINDOW (window, w);
7065 SET_TEXT_POS (start, BEG, BEG_BYTE);
7066 try_window (window, start);
7067
7068 return window_height_changed_p;
7069 }
7070
7071
7072 /* Resize the echo area window to exactly the size needed for the
7073 currently displayed message, if there is one. If a mini-buffer
7074 is active, don't shrink it. */
7075
7076 void
7077 resize_echo_area_exactly ()
7078 {
7079 if (BUFFERP (echo_area_buffer[0])
7080 && WINDOWP (echo_area_window))
7081 {
7082 struct window *w = XWINDOW (echo_area_window);
7083 int resized_p;
7084 Lisp_Object resize_exactly;
7085
7086 if (minibuf_level == 0)
7087 resize_exactly = Qt;
7088 else
7089 resize_exactly = Qnil;
7090
7091 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7092 (EMACS_INT) w, resize_exactly, 0, 0);
7093 if (resized_p)
7094 {
7095 ++windows_or_buffers_changed;
7096 ++update_mode_lines;
7097 redisplay_internal (0);
7098 }
7099 }
7100 }
7101
7102
7103 /* Callback function for with_echo_area_buffer, when used from
7104 resize_echo_area_exactly. A1 contains a pointer to the window to
7105 resize, EXACTLY non-nil means resize the mini-window exactly to the
7106 size of the text displayed. A3 and A4 are not used. Value is what
7107 resize_mini_window returns. */
7108
7109 static int
7110 resize_mini_window_1 (a1, exactly, a3, a4)
7111 EMACS_INT a1;
7112 Lisp_Object exactly;
7113 EMACS_INT a3, a4;
7114 {
7115 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7116 }
7117
7118
7119 /* Resize mini-window W to fit the size of its contents. EXACT:P
7120 means size the window exactly to the size needed. Otherwise, it's
7121 only enlarged until W's buffer is empty. Value is non-zero if
7122 the window height has been changed. */
7123
7124 int
7125 resize_mini_window (w, exact_p)
7126 struct window *w;
7127 int exact_p;
7128 {
7129 struct frame *f = XFRAME (w->frame);
7130 int window_height_changed_p = 0;
7131
7132 xassert (MINI_WINDOW_P (w));
7133
7134 /* Don't resize windows while redisplaying a window; it would
7135 confuse redisplay functions when the size of the window they are
7136 displaying changes from under them. Such a resizing can happen,
7137 for instance, when which-func prints a long message while
7138 we are running fontification-functions. We're running these
7139 functions with safe_call which binds inhibit-redisplay to t. */
7140 if (!NILP (Vinhibit_redisplay))
7141 return 0;
7142
7143 /* Nil means don't try to resize. */
7144 if (NILP (Vresize_mini_windows)
7145 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7146 return 0;
7147
7148 if (!FRAME_MINIBUF_ONLY_P (f))
7149 {
7150 struct it it;
7151 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7152 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7153 int height, max_height;
7154 int unit = FRAME_LINE_HEIGHT (f);
7155 struct text_pos start;
7156 struct buffer *old_current_buffer = NULL;
7157
7158 if (current_buffer != XBUFFER (w->buffer))
7159 {
7160 old_current_buffer = current_buffer;
7161 set_buffer_internal (XBUFFER (w->buffer));
7162 }
7163
7164 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7165
7166 /* Compute the max. number of lines specified by the user. */
7167 if (FLOATP (Vmax_mini_window_height))
7168 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7169 else if (INTEGERP (Vmax_mini_window_height))
7170 max_height = XINT (Vmax_mini_window_height);
7171 else
7172 max_height = total_height / 4;
7173
7174 /* Correct that max. height if it's bogus. */
7175 max_height = max (1, max_height);
7176 max_height = min (total_height, max_height);
7177
7178 /* Find out the height of the text in the window. */
7179 if (it.truncate_lines_p)
7180 height = 1;
7181 else
7182 {
7183 last_height = 0;
7184 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7185 if (it.max_ascent == 0 && it.max_descent == 0)
7186 height = it.current_y + last_height;
7187 else
7188 height = it.current_y + it.max_ascent + it.max_descent;
7189 height -= it.extra_line_spacing;
7190 height = (height + unit - 1) / unit;
7191 }
7192
7193 /* Compute a suitable window start. */
7194 if (height > max_height)
7195 {
7196 height = max_height;
7197 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7198 move_it_vertically_backward (&it, (height - 1) * unit);
7199 start = it.current.pos;
7200 }
7201 else
7202 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7203 SET_MARKER_FROM_TEXT_POS (w->start, start);
7204
7205 if (EQ (Vresize_mini_windows, Qgrow_only))
7206 {
7207 /* Let it grow only, until we display an empty message, in which
7208 case the window shrinks again. */
7209 if (height > WINDOW_TOTAL_LINES (w))
7210 {
7211 int old_height = WINDOW_TOTAL_LINES (w);
7212 freeze_window_starts (f, 1);
7213 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7214 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7215 }
7216 else if (height < WINDOW_TOTAL_LINES (w)
7217 && (exact_p || BEGV == ZV))
7218 {
7219 int old_height = WINDOW_TOTAL_LINES (w);
7220 freeze_window_starts (f, 0);
7221 shrink_mini_window (w);
7222 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7223 }
7224 }
7225 else
7226 {
7227 /* Always resize to exact size needed. */
7228 if (height > WINDOW_TOTAL_LINES (w))
7229 {
7230 int old_height = WINDOW_TOTAL_LINES (w);
7231 freeze_window_starts (f, 1);
7232 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7233 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7234 }
7235 else if (height < WINDOW_TOTAL_LINES (w))
7236 {
7237 int old_height = WINDOW_TOTAL_LINES (w);
7238 freeze_window_starts (f, 0);
7239 shrink_mini_window (w);
7240
7241 if (height)
7242 {
7243 freeze_window_starts (f, 1);
7244 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7245 }
7246
7247 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7248 }
7249 }
7250
7251 if (old_current_buffer)
7252 set_buffer_internal (old_current_buffer);
7253 }
7254
7255 return window_height_changed_p;
7256 }
7257
7258
7259 /* Value is the current message, a string, or nil if there is no
7260 current message. */
7261
7262 Lisp_Object
7263 current_message ()
7264 {
7265 Lisp_Object msg;
7266
7267 if (NILP (echo_area_buffer[0]))
7268 msg = Qnil;
7269 else
7270 {
7271 with_echo_area_buffer (0, 0, current_message_1,
7272 (EMACS_INT) &msg, Qnil, 0, 0);
7273 if (NILP (msg))
7274 echo_area_buffer[0] = Qnil;
7275 }
7276
7277 return msg;
7278 }
7279
7280
7281 static int
7282 current_message_1 (a1, a2, a3, a4)
7283 EMACS_INT a1;
7284 Lisp_Object a2;
7285 EMACS_INT a3, a4;
7286 {
7287 Lisp_Object *msg = (Lisp_Object *) a1;
7288
7289 if (Z > BEG)
7290 *msg = make_buffer_string (BEG, Z, 1);
7291 else
7292 *msg = Qnil;
7293 return 0;
7294 }
7295
7296
7297 /* Push the current message on Vmessage_stack for later restauration
7298 by restore_message. Value is non-zero if the current message isn't
7299 empty. This is a relatively infrequent operation, so it's not
7300 worth optimizing. */
7301
7302 int
7303 push_message ()
7304 {
7305 Lisp_Object msg;
7306 msg = current_message ();
7307 Vmessage_stack = Fcons (msg, Vmessage_stack);
7308 return STRINGP (msg);
7309 }
7310
7311
7312 /* Restore message display from the top of Vmessage_stack. */
7313
7314 void
7315 restore_message ()
7316 {
7317 Lisp_Object msg;
7318
7319 xassert (CONSP (Vmessage_stack));
7320 msg = XCAR (Vmessage_stack);
7321 if (STRINGP (msg))
7322 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7323 else
7324 message3_nolog (msg, 0, 0);
7325 }
7326
7327
7328 /* Handler for record_unwind_protect calling pop_message. */
7329
7330 Lisp_Object
7331 pop_message_unwind (dummy)
7332 Lisp_Object dummy;
7333 {
7334 pop_message ();
7335 return Qnil;
7336 }
7337
7338 /* Pop the top-most entry off Vmessage_stack. */
7339
7340 void
7341 pop_message ()
7342 {
7343 xassert (CONSP (Vmessage_stack));
7344 Vmessage_stack = XCDR (Vmessage_stack);
7345 }
7346
7347
7348 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7349 exits. If the stack is not empty, we have a missing pop_message
7350 somewhere. */
7351
7352 void
7353 check_message_stack ()
7354 {
7355 if (!NILP (Vmessage_stack))
7356 abort ();
7357 }
7358
7359
7360 /* Truncate to NCHARS what will be displayed in the echo area the next
7361 time we display it---but don't redisplay it now. */
7362
7363 void
7364 truncate_echo_area (nchars)
7365 int nchars;
7366 {
7367 if (nchars == 0)
7368 echo_area_buffer[0] = Qnil;
7369 /* A null message buffer means that the frame hasn't really been
7370 initialized yet. Error messages get reported properly by
7371 cmd_error, so this must be just an informative message; toss it. */
7372 else if (!noninteractive
7373 && INTERACTIVE
7374 && !NILP (echo_area_buffer[0]))
7375 {
7376 struct frame *sf = SELECTED_FRAME ();
7377 if (FRAME_MESSAGE_BUF (sf))
7378 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7379 }
7380 }
7381
7382
7383 /* Helper function for truncate_echo_area. Truncate the current
7384 message to at most NCHARS characters. */
7385
7386 static int
7387 truncate_message_1 (nchars, a2, a3, a4)
7388 EMACS_INT nchars;
7389 Lisp_Object a2;
7390 EMACS_INT a3, a4;
7391 {
7392 if (BEG + nchars < Z)
7393 del_range (BEG + nchars, Z);
7394 if (Z == BEG)
7395 echo_area_buffer[0] = Qnil;
7396 return 0;
7397 }
7398
7399
7400 /* Set the current message to a substring of S or STRING.
7401
7402 If STRING is a Lisp string, set the message to the first NBYTES
7403 bytes from STRING. NBYTES zero means use the whole string. If
7404 STRING is multibyte, the message will be displayed multibyte.
7405
7406 If S is not null, set the message to the first LEN bytes of S. LEN
7407 zero means use the whole string. MULTIBYTE_P non-zero means S is
7408 multibyte. Display the message multibyte in that case. */
7409
7410 void
7411 set_message (s, string, nbytes, multibyte_p)
7412 const char *s;
7413 Lisp_Object string;
7414 int nbytes, multibyte_p;
7415 {
7416 message_enable_multibyte
7417 = ((s && multibyte_p)
7418 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7419
7420 with_echo_area_buffer (0, -1, set_message_1,
7421 (EMACS_INT) s, string, nbytes, multibyte_p);
7422 message_buf_print = 0;
7423 help_echo_showing_p = 0;
7424 }
7425
7426
7427 /* Helper function for set_message. Arguments have the same meaning
7428 as there, with A1 corresponding to S and A2 corresponding to STRING
7429 This function is called with the echo area buffer being
7430 current. */
7431
7432 static int
7433 set_message_1 (a1, a2, nbytes, multibyte_p)
7434 EMACS_INT a1;
7435 Lisp_Object a2;
7436 EMACS_INT nbytes, multibyte_p;
7437 {
7438 const char *s = (const char *) a1;
7439 Lisp_Object string = a2;
7440
7441 xassert (BEG == Z);
7442
7443 /* Change multibyteness of the echo buffer appropriately. */
7444 if (message_enable_multibyte
7445 != !NILP (current_buffer->enable_multibyte_characters))
7446 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7447
7448 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7449
7450 /* Insert new message at BEG. */
7451 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7452
7453 if (STRINGP (string))
7454 {
7455 int nchars;
7456
7457 if (nbytes == 0)
7458 nbytes = SBYTES (string);
7459 nchars = string_byte_to_char (string, nbytes);
7460
7461 /* This function takes care of single/multibyte conversion. We
7462 just have to ensure that the echo area buffer has the right
7463 setting of enable_multibyte_characters. */
7464 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7465 }
7466 else if (s)
7467 {
7468 if (nbytes == 0)
7469 nbytes = strlen (s);
7470
7471 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7472 {
7473 /* Convert from multi-byte to single-byte. */
7474 int i, c, n;
7475 unsigned char work[1];
7476
7477 /* Convert a multibyte string to single-byte. */
7478 for (i = 0; i < nbytes; i += n)
7479 {
7480 c = string_char_and_length (s + i, nbytes - i, &n);
7481 work[0] = (SINGLE_BYTE_CHAR_P (c)
7482 ? c
7483 : multibyte_char_to_unibyte (c, Qnil));
7484 insert_1_both (work, 1, 1, 1, 0, 0);
7485 }
7486 }
7487 else if (!multibyte_p
7488 && !NILP (current_buffer->enable_multibyte_characters))
7489 {
7490 /* Convert from single-byte to multi-byte. */
7491 int i, c, n;
7492 const unsigned char *msg = (const unsigned char *) s;
7493 unsigned char str[MAX_MULTIBYTE_LENGTH];
7494
7495 /* Convert a single-byte string to multibyte. */
7496 for (i = 0; i < nbytes; i++)
7497 {
7498 c = unibyte_char_to_multibyte (msg[i]);
7499 n = CHAR_STRING (c, str);
7500 insert_1_both (str, 1, n, 1, 0, 0);
7501 }
7502 }
7503 else
7504 insert_1 (s, nbytes, 1, 0, 0);
7505 }
7506
7507 return 0;
7508 }
7509
7510
7511 /* Clear messages. CURRENT_P non-zero means clear the current
7512 message. LAST_DISPLAYED_P non-zero means clear the message
7513 last displayed. */
7514
7515 void
7516 clear_message (current_p, last_displayed_p)
7517 int current_p, last_displayed_p;
7518 {
7519 if (current_p)
7520 {
7521 echo_area_buffer[0] = Qnil;
7522 message_cleared_p = 1;
7523 }
7524
7525 if (last_displayed_p)
7526 echo_area_buffer[1] = Qnil;
7527
7528 message_buf_print = 0;
7529 }
7530
7531 /* Clear garbaged frames.
7532
7533 This function is used where the old redisplay called
7534 redraw_garbaged_frames which in turn called redraw_frame which in
7535 turn called clear_frame. The call to clear_frame was a source of
7536 flickering. I believe a clear_frame is not necessary. It should
7537 suffice in the new redisplay to invalidate all current matrices,
7538 and ensure a complete redisplay of all windows. */
7539
7540 static void
7541 clear_garbaged_frames ()
7542 {
7543 if (frame_garbaged)
7544 {
7545 Lisp_Object tail, frame;
7546 int changed_count = 0;
7547
7548 FOR_EACH_FRAME (tail, frame)
7549 {
7550 struct frame *f = XFRAME (frame);
7551
7552 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7553 {
7554 if (f->resized_p)
7555 Fredraw_frame (frame);
7556 clear_current_matrices (f);
7557 changed_count++;
7558 f->garbaged = 0;
7559 f->resized_p = 0;
7560 }
7561 }
7562
7563 frame_garbaged = 0;
7564 if (changed_count)
7565 ++windows_or_buffers_changed;
7566 }
7567 }
7568
7569
7570 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7571 is non-zero update selected_frame. Value is non-zero if the
7572 mini-windows height has been changed. */
7573
7574 static int
7575 echo_area_display (update_frame_p)
7576 int update_frame_p;
7577 {
7578 Lisp_Object mini_window;
7579 struct window *w;
7580 struct frame *f;
7581 int window_height_changed_p = 0;
7582 struct frame *sf = SELECTED_FRAME ();
7583
7584 mini_window = FRAME_MINIBUF_WINDOW (sf);
7585 w = XWINDOW (mini_window);
7586 f = XFRAME (WINDOW_FRAME (w));
7587
7588 /* Don't display if frame is invisible or not yet initialized. */
7589 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7590 return 0;
7591
7592 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7593 #ifndef MAC_OS8
7594 #ifdef HAVE_WINDOW_SYSTEM
7595 /* When Emacs starts, selected_frame may be a visible terminal
7596 frame, even if we run under a window system. If we let this
7597 through, a message would be displayed on the terminal. */
7598 if (EQ (selected_frame, Vterminal_frame)
7599 && !NILP (Vwindow_system))
7600 return 0;
7601 #endif /* HAVE_WINDOW_SYSTEM */
7602 #endif
7603
7604 /* Redraw garbaged frames. */
7605 if (frame_garbaged)
7606 clear_garbaged_frames ();
7607
7608 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7609 {
7610 echo_area_window = mini_window;
7611 window_height_changed_p = display_echo_area (w);
7612 w->must_be_updated_p = 1;
7613
7614 /* Update the display, unless called from redisplay_internal.
7615 Also don't update the screen during redisplay itself. The
7616 update will happen at the end of redisplay, and an update
7617 here could cause confusion. */
7618 if (update_frame_p && !redisplaying_p)
7619 {
7620 int n = 0;
7621
7622 /* If the display update has been interrupted by pending
7623 input, update mode lines in the frame. Due to the
7624 pending input, it might have been that redisplay hasn't
7625 been called, so that mode lines above the echo area are
7626 garbaged. This looks odd, so we prevent it here. */
7627 if (!display_completed)
7628 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7629
7630 if (window_height_changed_p
7631 /* Don't do this if Emacs is shutting down. Redisplay
7632 needs to run hooks. */
7633 && !NILP (Vrun_hooks))
7634 {
7635 /* Must update other windows. Likewise as in other
7636 cases, don't let this update be interrupted by
7637 pending input. */
7638 int count = SPECPDL_INDEX ();
7639 specbind (Qredisplay_dont_pause, Qt);
7640 windows_or_buffers_changed = 1;
7641 redisplay_internal (0);
7642 unbind_to (count, Qnil);
7643 }
7644 else if (FRAME_WINDOW_P (f) && n == 0)
7645 {
7646 /* Window configuration is the same as before.
7647 Can do with a display update of the echo area,
7648 unless we displayed some mode lines. */
7649 update_single_window (w, 1);
7650 rif->flush_display (f);
7651 }
7652 else
7653 update_frame (f, 1, 1);
7654
7655 /* If cursor is in the echo area, make sure that the next
7656 redisplay displays the minibuffer, so that the cursor will
7657 be replaced with what the minibuffer wants. */
7658 if (cursor_in_echo_area)
7659 ++windows_or_buffers_changed;
7660 }
7661 }
7662 else if (!EQ (mini_window, selected_window))
7663 windows_or_buffers_changed++;
7664
7665 /* Last displayed message is now the current message. */
7666 echo_area_buffer[1] = echo_area_buffer[0];
7667
7668 /* Prevent redisplay optimization in redisplay_internal by resetting
7669 this_line_start_pos. This is done because the mini-buffer now
7670 displays the message instead of its buffer text. */
7671 if (EQ (mini_window, selected_window))
7672 CHARPOS (this_line_start_pos) = 0;
7673
7674 return window_height_changed_p;
7675 }
7676
7677
7678 \f
7679 /***********************************************************************
7680 Frame Titles
7681 ***********************************************************************/
7682
7683
7684 /* The frame title buffering code is also used by Fformat_mode_line.
7685 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
7686
7687 /* A buffer for constructing frame titles in it; allocated from the
7688 heap in init_xdisp and resized as needed in store_frame_title_char. */
7689
7690 static char *frame_title_buf;
7691
7692 /* The buffer's end, and a current output position in it. */
7693
7694 static char *frame_title_buf_end;
7695 static char *frame_title_ptr;
7696
7697
7698 /* Store a single character C for the frame title in frame_title_buf.
7699 Re-allocate frame_title_buf if necessary. */
7700
7701 static void
7702 #ifdef PROTOTYPES
7703 store_frame_title_char (char c)
7704 #else
7705 store_frame_title_char (c)
7706 char c;
7707 #endif
7708 {
7709 /* If output position has reached the end of the allocated buffer,
7710 double the buffer's size. */
7711 if (frame_title_ptr == frame_title_buf_end)
7712 {
7713 int len = frame_title_ptr - frame_title_buf;
7714 int new_size = 2 * len * sizeof *frame_title_buf;
7715 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7716 frame_title_buf_end = frame_title_buf + new_size;
7717 frame_title_ptr = frame_title_buf + len;
7718 }
7719
7720 *frame_title_ptr++ = c;
7721 }
7722
7723
7724 /* Store part of a frame title in frame_title_buf, beginning at
7725 frame_title_ptr. STR is the string to store. Do not copy
7726 characters that yield more columns than PRECISION; PRECISION <= 0
7727 means copy the whole string. Pad with spaces until FIELD_WIDTH
7728 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7729 pad. Called from display_mode_element when it is used to build a
7730 frame title. */
7731
7732 static int
7733 store_frame_title (str, field_width, precision)
7734 const unsigned char *str;
7735 int field_width, precision;
7736 {
7737 int n = 0;
7738 int dummy, nbytes;
7739
7740 /* Copy at most PRECISION chars from STR. */
7741 nbytes = strlen (str);
7742 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7743 while (nbytes--)
7744 store_frame_title_char (*str++);
7745
7746 /* Fill up with spaces until FIELD_WIDTH reached. */
7747 while (field_width > 0
7748 && n < field_width)
7749 {
7750 store_frame_title_char (' ');
7751 ++n;
7752 }
7753
7754 return n;
7755 }
7756
7757 #ifdef HAVE_WINDOW_SYSTEM
7758
7759 /* Set the title of FRAME, if it has changed. The title format is
7760 Vicon_title_format if FRAME is iconified, otherwise it is
7761 frame_title_format. */
7762
7763 static void
7764 x_consider_frame_title (frame)
7765 Lisp_Object frame;
7766 {
7767 struct frame *f = XFRAME (frame);
7768
7769 if (FRAME_WINDOW_P (f)
7770 || FRAME_MINIBUF_ONLY_P (f)
7771 || f->explicit_name)
7772 {
7773 /* Do we have more than one visible frame on this X display? */
7774 Lisp_Object tail;
7775 Lisp_Object fmt;
7776 struct buffer *obuf;
7777 int len;
7778 struct it it;
7779
7780 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7781 {
7782 Lisp_Object other_frame = XCAR (tail);
7783 struct frame *tf = XFRAME (other_frame);
7784
7785 if (tf != f
7786 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7787 && !FRAME_MINIBUF_ONLY_P (tf)
7788 && !EQ (other_frame, tip_frame)
7789 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7790 break;
7791 }
7792
7793 /* Set global variable indicating that multiple frames exist. */
7794 multiple_frames = CONSP (tail);
7795
7796 /* Switch to the buffer of selected window of the frame. Set up
7797 frame_title_ptr so that display_mode_element will output into it;
7798 then display the title. */
7799 obuf = current_buffer;
7800 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
7801 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7802 frame_title_ptr = frame_title_buf;
7803 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7804 NULL, DEFAULT_FACE_ID);
7805 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
7806 len = frame_title_ptr - frame_title_buf;
7807 frame_title_ptr = NULL;
7808 set_buffer_internal_1 (obuf);
7809
7810 /* Set the title only if it's changed. This avoids consing in
7811 the common case where it hasn't. (If it turns out that we've
7812 already wasted too much time by walking through the list with
7813 display_mode_element, then we might need to optimize at a
7814 higher level than this.) */
7815 if (! STRINGP (f->name)
7816 || SBYTES (f->name) != len
7817 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
7818 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7819 }
7820 }
7821
7822 #endif /* not HAVE_WINDOW_SYSTEM */
7823
7824
7825
7826 \f
7827 /***********************************************************************
7828 Menu Bars
7829 ***********************************************************************/
7830
7831
7832 /* Prepare for redisplay by updating menu-bar item lists when
7833 appropriate. This can call eval. */
7834
7835 void
7836 prepare_menu_bars ()
7837 {
7838 int all_windows;
7839 struct gcpro gcpro1, gcpro2;
7840 struct frame *f;
7841 Lisp_Object tooltip_frame;
7842
7843 #ifdef HAVE_WINDOW_SYSTEM
7844 tooltip_frame = tip_frame;
7845 #else
7846 tooltip_frame = Qnil;
7847 #endif
7848
7849 /* Update all frame titles based on their buffer names, etc. We do
7850 this before the menu bars so that the buffer-menu will show the
7851 up-to-date frame titles. */
7852 #ifdef HAVE_WINDOW_SYSTEM
7853 if (windows_or_buffers_changed || update_mode_lines)
7854 {
7855 Lisp_Object tail, frame;
7856
7857 FOR_EACH_FRAME (tail, frame)
7858 {
7859 f = XFRAME (frame);
7860 if (!EQ (frame, tooltip_frame)
7861 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7862 x_consider_frame_title (frame);
7863 }
7864 }
7865 #endif /* HAVE_WINDOW_SYSTEM */
7866
7867 /* Update the menu bar item lists, if appropriate. This has to be
7868 done before any actual redisplay or generation of display lines. */
7869 all_windows = (update_mode_lines
7870 || buffer_shared > 1
7871 || windows_or_buffers_changed);
7872 if (all_windows)
7873 {
7874 Lisp_Object tail, frame;
7875 int count = SPECPDL_INDEX ();
7876
7877 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7878
7879 FOR_EACH_FRAME (tail, frame)
7880 {
7881 f = XFRAME (frame);
7882
7883 /* Ignore tooltip frame. */
7884 if (EQ (frame, tooltip_frame))
7885 continue;
7886
7887 /* If a window on this frame changed size, report that to
7888 the user and clear the size-change flag. */
7889 if (FRAME_WINDOW_SIZES_CHANGED (f))
7890 {
7891 Lisp_Object functions;
7892
7893 /* Clear flag first in case we get an error below. */
7894 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7895 functions = Vwindow_size_change_functions;
7896 GCPRO2 (tail, functions);
7897
7898 while (CONSP (functions))
7899 {
7900 call1 (XCAR (functions), frame);
7901 functions = XCDR (functions);
7902 }
7903 UNGCPRO;
7904 }
7905
7906 GCPRO1 (tail);
7907 update_menu_bar (f, 0);
7908 #ifdef HAVE_WINDOW_SYSTEM
7909 update_tool_bar (f, 0);
7910 #endif
7911 UNGCPRO;
7912 }
7913
7914 unbind_to (count, Qnil);
7915 }
7916 else
7917 {
7918 struct frame *sf = SELECTED_FRAME ();
7919 update_menu_bar (sf, 1);
7920 #ifdef HAVE_WINDOW_SYSTEM
7921 update_tool_bar (sf, 1);
7922 #endif
7923 }
7924
7925 /* Motif needs this. See comment in xmenu.c. Turn it off when
7926 pending_menu_activation is not defined. */
7927 #ifdef USE_X_TOOLKIT
7928 pending_menu_activation = 0;
7929 #endif
7930 }
7931
7932
7933 /* Update the menu bar item list for frame F. This has to be done
7934 before we start to fill in any display lines, because it can call
7935 eval.
7936
7937 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7938
7939 static void
7940 update_menu_bar (f, save_match_data)
7941 struct frame *f;
7942 int save_match_data;
7943 {
7944 Lisp_Object window;
7945 register struct window *w;
7946
7947 /* If called recursively during a menu update, do nothing. This can
7948 happen when, for instance, an activate-menubar-hook causes a
7949 redisplay. */
7950 if (inhibit_menubar_update)
7951 return;
7952
7953 window = FRAME_SELECTED_WINDOW (f);
7954 w = XWINDOW (window);
7955
7956 #if 0 /* The if statement below this if statement used to include the
7957 condition !NILP (w->update_mode_line), rather than using
7958 update_mode_lines directly, and this if statement may have
7959 been added to make that condition work. Now the if
7960 statement below matches its comment, this isn't needed. */
7961 if (update_mode_lines)
7962 w->update_mode_line = Qt;
7963 #endif
7964
7965 if (FRAME_WINDOW_P (f)
7966 ?
7967 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
7968 || defined (USE_GTK)
7969 FRAME_EXTERNAL_MENU_BAR (f)
7970 #else
7971 FRAME_MENU_BAR_LINES (f) > 0
7972 #endif
7973 : FRAME_MENU_BAR_LINES (f) > 0)
7974 {
7975 /* If the user has switched buffers or windows, we need to
7976 recompute to reflect the new bindings. But we'll
7977 recompute when update_mode_lines is set too; that means
7978 that people can use force-mode-line-update to request
7979 that the menu bar be recomputed. The adverse effect on
7980 the rest of the redisplay algorithm is about the same as
7981 windows_or_buffers_changed anyway. */
7982 if (windows_or_buffers_changed
7983 /* This used to test w->update_mode_line, but we believe
7984 there is no need to recompute the menu in that case. */
7985 || update_mode_lines
7986 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7987 < BUF_MODIFF (XBUFFER (w->buffer)))
7988 != !NILP (w->last_had_star))
7989 || ((!NILP (Vtransient_mark_mode)
7990 && !NILP (XBUFFER (w->buffer)->mark_active))
7991 != !NILP (w->region_showing)))
7992 {
7993 struct buffer *prev = current_buffer;
7994 int count = SPECPDL_INDEX ();
7995
7996 specbind (Qinhibit_menubar_update, Qt);
7997
7998 set_buffer_internal_1 (XBUFFER (w->buffer));
7999 if (save_match_data)
8000 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8001 if (NILP (Voverriding_local_map_menu_flag))
8002 {
8003 specbind (Qoverriding_terminal_local_map, Qnil);
8004 specbind (Qoverriding_local_map, Qnil);
8005 }
8006
8007 /* Run the Lucid hook. */
8008 safe_run_hooks (Qactivate_menubar_hook);
8009
8010 /* If it has changed current-menubar from previous value,
8011 really recompute the menu-bar from the value. */
8012 if (! NILP (Vlucid_menu_bar_dirty_flag))
8013 call0 (Qrecompute_lucid_menubar);
8014
8015 safe_run_hooks (Qmenu_bar_update_hook);
8016 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8017
8018 /* Redisplay the menu bar in case we changed it. */
8019 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8020 || defined (USE_GTK)
8021 if (FRAME_WINDOW_P (f)
8022 #if defined (MAC_OS)
8023 /* All frames on Mac OS share the same menubar. So only the
8024 selected frame should be allowed to set it. */
8025 && f == SELECTED_FRAME ()
8026 #endif
8027 )
8028 set_frame_menubar (f, 0, 0);
8029 else
8030 /* On a terminal screen, the menu bar is an ordinary screen
8031 line, and this makes it get updated. */
8032 w->update_mode_line = Qt;
8033 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8034 /* In the non-toolkit version, the menu bar is an ordinary screen
8035 line, and this makes it get updated. */
8036 w->update_mode_line = Qt;
8037 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8038
8039 unbind_to (count, Qnil);
8040 set_buffer_internal_1 (prev);
8041 }
8042 }
8043 }
8044
8045
8046 \f
8047 /***********************************************************************
8048 Output Cursor
8049 ***********************************************************************/
8050
8051 #ifdef HAVE_WINDOW_SYSTEM
8052
8053 /* EXPORT:
8054 Nominal cursor position -- where to draw output.
8055 HPOS and VPOS are window relative glyph matrix coordinates.
8056 X and Y are window relative pixel coordinates. */
8057
8058 struct cursor_pos output_cursor;
8059
8060
8061 /* EXPORT:
8062 Set the global variable output_cursor to CURSOR. All cursor
8063 positions are relative to updated_window. */
8064
8065 void
8066 set_output_cursor (cursor)
8067 struct cursor_pos *cursor;
8068 {
8069 output_cursor.hpos = cursor->hpos;
8070 output_cursor.vpos = cursor->vpos;
8071 output_cursor.x = cursor->x;
8072 output_cursor.y = cursor->y;
8073 }
8074
8075
8076 /* EXPORT for RIF:
8077 Set a nominal cursor position.
8078
8079 HPOS and VPOS are column/row positions in a window glyph matrix. X
8080 and Y are window text area relative pixel positions.
8081
8082 If this is done during an update, updated_window will contain the
8083 window that is being updated and the position is the future output
8084 cursor position for that window. If updated_window is null, use
8085 selected_window and display the cursor at the given position. */
8086
8087 void
8088 x_cursor_to (vpos, hpos, y, x)
8089 int vpos, hpos, y, x;
8090 {
8091 struct window *w;
8092
8093 /* If updated_window is not set, work on selected_window. */
8094 if (updated_window)
8095 w = updated_window;
8096 else
8097 w = XWINDOW (selected_window);
8098
8099 /* Set the output cursor. */
8100 output_cursor.hpos = hpos;
8101 output_cursor.vpos = vpos;
8102 output_cursor.x = x;
8103 output_cursor.y = y;
8104
8105 /* If not called as part of an update, really display the cursor.
8106 This will also set the cursor position of W. */
8107 if (updated_window == NULL)
8108 {
8109 BLOCK_INPUT;
8110 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8111 if (rif->flush_display_optional)
8112 rif->flush_display_optional (SELECTED_FRAME ());
8113 UNBLOCK_INPUT;
8114 }
8115 }
8116
8117 #endif /* HAVE_WINDOW_SYSTEM */
8118
8119 \f
8120 /***********************************************************************
8121 Tool-bars
8122 ***********************************************************************/
8123
8124 #ifdef HAVE_WINDOW_SYSTEM
8125
8126 /* Where the mouse was last time we reported a mouse event. */
8127
8128 FRAME_PTR last_mouse_frame;
8129
8130 /* Tool-bar item index of the item on which a mouse button was pressed
8131 or -1. */
8132
8133 int last_tool_bar_item;
8134
8135
8136 /* Update the tool-bar item list for frame F. This has to be done
8137 before we start to fill in any display lines. Called from
8138 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8139 and restore it here. */
8140
8141 static void
8142 update_tool_bar (f, save_match_data)
8143 struct frame *f;
8144 int save_match_data;
8145 {
8146 #ifdef USE_GTK
8147 int do_update = FRAME_EXTERNAL_TOOL_BAR(f);
8148 #else
8149 int do_update = WINDOWP (f->tool_bar_window)
8150 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8151 #endif
8152
8153 if (do_update)
8154 {
8155 Lisp_Object window;
8156 struct window *w;
8157
8158 window = FRAME_SELECTED_WINDOW (f);
8159 w = XWINDOW (window);
8160
8161 /* If the user has switched buffers or windows, we need to
8162 recompute to reflect the new bindings. But we'll
8163 recompute when update_mode_lines is set too; that means
8164 that people can use force-mode-line-update to request
8165 that the menu bar be recomputed. The adverse effect on
8166 the rest of the redisplay algorithm is about the same as
8167 windows_or_buffers_changed anyway. */
8168 if (windows_or_buffers_changed
8169 || !NILP (w->update_mode_line)
8170 || update_mode_lines
8171 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8172 < BUF_MODIFF (XBUFFER (w->buffer)))
8173 != !NILP (w->last_had_star))
8174 || ((!NILP (Vtransient_mark_mode)
8175 && !NILP (XBUFFER (w->buffer)->mark_active))
8176 != !NILP (w->region_showing)))
8177 {
8178 struct buffer *prev = current_buffer;
8179 int count = SPECPDL_INDEX ();
8180 Lisp_Object old_tool_bar;
8181 struct gcpro gcpro1;
8182
8183 /* Set current_buffer to the buffer of the selected
8184 window of the frame, so that we get the right local
8185 keymaps. */
8186 set_buffer_internal_1 (XBUFFER (w->buffer));
8187
8188 /* Save match data, if we must. */
8189 if (save_match_data)
8190 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8191
8192 /* Make sure that we don't accidentally use bogus keymaps. */
8193 if (NILP (Voverriding_local_map_menu_flag))
8194 {
8195 specbind (Qoverriding_terminal_local_map, Qnil);
8196 specbind (Qoverriding_local_map, Qnil);
8197 }
8198
8199 old_tool_bar = f->tool_bar_items;
8200 GCPRO1 (old_tool_bar);
8201
8202 /* Build desired tool-bar items from keymaps. */
8203 BLOCK_INPUT;
8204 f->tool_bar_items
8205 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
8206 UNBLOCK_INPUT;
8207
8208 /* Redisplay the tool-bar if we changed it. */
8209 if (! NILP (Fequal (old_tool_bar, f->tool_bar_items)))
8210 w->update_mode_line = Qt;
8211
8212 UNGCPRO;
8213
8214 unbind_to (count, Qnil);
8215 set_buffer_internal_1 (prev);
8216 }
8217 }
8218 }
8219
8220
8221 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8222 F's desired tool-bar contents. F->tool_bar_items must have
8223 been set up previously by calling prepare_menu_bars. */
8224
8225 static void
8226 build_desired_tool_bar_string (f)
8227 struct frame *f;
8228 {
8229 int i, size, size_needed;
8230 struct gcpro gcpro1, gcpro2, gcpro3;
8231 Lisp_Object image, plist, props;
8232
8233 image = plist = props = Qnil;
8234 GCPRO3 (image, plist, props);
8235
8236 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8237 Otherwise, make a new string. */
8238
8239 /* The size of the string we might be able to reuse. */
8240 size = (STRINGP (f->desired_tool_bar_string)
8241 ? SCHARS (f->desired_tool_bar_string)
8242 : 0);
8243
8244 /* We need one space in the string for each image. */
8245 size_needed = f->n_tool_bar_items;
8246
8247 /* Reuse f->desired_tool_bar_string, if possible. */
8248 if (size < size_needed || NILP (f->desired_tool_bar_string))
8249 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8250 make_number (' '));
8251 else
8252 {
8253 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8254 Fremove_text_properties (make_number (0), make_number (size),
8255 props, f->desired_tool_bar_string);
8256 }
8257
8258 /* Put a `display' property on the string for the images to display,
8259 put a `menu_item' property on tool-bar items with a value that
8260 is the index of the item in F's tool-bar item vector. */
8261 for (i = 0; i < f->n_tool_bar_items; ++i)
8262 {
8263 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8264
8265 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8266 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8267 int hmargin, vmargin, relief, idx, end;
8268 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
8269
8270 /* If image is a vector, choose the image according to the
8271 button state. */
8272 image = PROP (TOOL_BAR_ITEM_IMAGES);
8273 if (VECTORP (image))
8274 {
8275 if (enabled_p)
8276 idx = (selected_p
8277 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8278 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8279 else
8280 idx = (selected_p
8281 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8282 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8283
8284 xassert (ASIZE (image) >= idx);
8285 image = AREF (image, idx);
8286 }
8287 else
8288 idx = -1;
8289
8290 /* Ignore invalid image specifications. */
8291 if (!valid_image_p (image))
8292 continue;
8293
8294 /* Display the tool-bar button pressed, or depressed. */
8295 plist = Fcopy_sequence (XCDR (image));
8296
8297 /* Compute margin and relief to draw. */
8298 relief = (tool_bar_button_relief >= 0
8299 ? tool_bar_button_relief
8300 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8301 hmargin = vmargin = relief;
8302
8303 if (INTEGERP (Vtool_bar_button_margin)
8304 && XINT (Vtool_bar_button_margin) > 0)
8305 {
8306 hmargin += XFASTINT (Vtool_bar_button_margin);
8307 vmargin += XFASTINT (Vtool_bar_button_margin);
8308 }
8309 else if (CONSP (Vtool_bar_button_margin))
8310 {
8311 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8312 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8313 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8314
8315 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8316 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8317 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8318 }
8319
8320 if (auto_raise_tool_bar_buttons_p)
8321 {
8322 /* Add a `:relief' property to the image spec if the item is
8323 selected. */
8324 if (selected_p)
8325 {
8326 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8327 hmargin -= relief;
8328 vmargin -= relief;
8329 }
8330 }
8331 else
8332 {
8333 /* If image is selected, display it pressed, i.e. with a
8334 negative relief. If it's not selected, display it with a
8335 raised relief. */
8336 plist = Fplist_put (plist, QCrelief,
8337 (selected_p
8338 ? make_number (-relief)
8339 : make_number (relief)));
8340 hmargin -= relief;
8341 vmargin -= relief;
8342 }
8343
8344 /* Put a margin around the image. */
8345 if (hmargin || vmargin)
8346 {
8347 if (hmargin == vmargin)
8348 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8349 else
8350 plist = Fplist_put (plist, QCmargin,
8351 Fcons (make_number (hmargin),
8352 make_number (vmargin)));
8353 }
8354
8355 /* If button is not enabled, and we don't have special images
8356 for the disabled state, make the image appear disabled by
8357 applying an appropriate algorithm to it. */
8358 if (!enabled_p && idx < 0)
8359 plist = Fplist_put (plist, QCconversion, Qdisabled);
8360
8361 /* Put a `display' text property on the string for the image to
8362 display. Put a `menu-item' property on the string that gives
8363 the start of this item's properties in the tool-bar items
8364 vector. */
8365 image = Fcons (Qimage, plist);
8366 props = list4 (Qdisplay, image,
8367 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8368
8369 /* Let the last image hide all remaining spaces in the tool bar
8370 string. The string can be longer than needed when we reuse a
8371 previous string. */
8372 if (i + 1 == f->n_tool_bar_items)
8373 end = SCHARS (f->desired_tool_bar_string);
8374 else
8375 end = i + 1;
8376 Fadd_text_properties (make_number (i), make_number (end),
8377 props, f->desired_tool_bar_string);
8378 #undef PROP
8379 }
8380
8381 UNGCPRO;
8382 }
8383
8384
8385 /* Display one line of the tool-bar of frame IT->f. */
8386
8387 static void
8388 display_tool_bar_line (it)
8389 struct it *it;
8390 {
8391 struct glyph_row *row = it->glyph_row;
8392 int max_x = it->last_visible_x;
8393 struct glyph *last;
8394
8395 prepare_desired_row (row);
8396 row->y = it->current_y;
8397
8398 /* Note that this isn't made use of if the face hasn't a box,
8399 so there's no need to check the face here. */
8400 it->start_of_box_run_p = 1;
8401
8402 while (it->current_x < max_x)
8403 {
8404 int x_before, x, n_glyphs_before, i, nglyphs;
8405
8406 /* Get the next display element. */
8407 if (!get_next_display_element (it))
8408 break;
8409
8410 /* Produce glyphs. */
8411 x_before = it->current_x;
8412 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8413 PRODUCE_GLYPHS (it);
8414
8415 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8416 i = 0;
8417 x = x_before;
8418 while (i < nglyphs)
8419 {
8420 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
8421
8422 if (x + glyph->pixel_width > max_x)
8423 {
8424 /* Glyph doesn't fit on line. */
8425 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8426 it->current_x = x;
8427 goto out;
8428 }
8429
8430 ++it->hpos;
8431 x += glyph->pixel_width;
8432 ++i;
8433 }
8434
8435 /* Stop at line ends. */
8436 if (ITERATOR_AT_END_OF_LINE_P (it))
8437 break;
8438
8439 set_iterator_to_next (it, 1);
8440 }
8441
8442 out:;
8443
8444 row->displays_text_p = row->used[TEXT_AREA] != 0;
8445 extend_face_to_end_of_line (it);
8446 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8447 last->right_box_line_p = 1;
8448 if (last == row->glyphs[TEXT_AREA])
8449 last->left_box_line_p = 1;
8450 compute_line_metrics (it);
8451
8452 /* If line is empty, make it occupy the rest of the tool-bar. */
8453 if (!row->displays_text_p)
8454 {
8455 row->height = row->phys_height = it->last_visible_y - row->y;
8456 row->ascent = row->phys_ascent = 0;
8457 }
8458
8459 row->full_width_p = 1;
8460 row->continued_p = 0;
8461 row->truncated_on_left_p = 0;
8462 row->truncated_on_right_p = 0;
8463
8464 it->current_x = it->hpos = 0;
8465 it->current_y += row->height;
8466 ++it->vpos;
8467 ++it->glyph_row;
8468 }
8469
8470
8471 /* Value is the number of screen lines needed to make all tool-bar
8472 items of frame F visible. */
8473
8474 static int
8475 tool_bar_lines_needed (f)
8476 struct frame *f;
8477 {
8478 struct window *w = XWINDOW (f->tool_bar_window);
8479 struct it it;
8480
8481 /* Initialize an iterator for iteration over
8482 F->desired_tool_bar_string in the tool-bar window of frame F. */
8483 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8484 it.first_visible_x = 0;
8485 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8486 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8487
8488 while (!ITERATOR_AT_END_P (&it))
8489 {
8490 it.glyph_row = w->desired_matrix->rows;
8491 clear_glyph_row (it.glyph_row);
8492 display_tool_bar_line (&it);
8493 }
8494
8495 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
8496 }
8497
8498
8499 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8500 0, 1, 0,
8501 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8502 (frame)
8503 Lisp_Object frame;
8504 {
8505 struct frame *f;
8506 struct window *w;
8507 int nlines = 0;
8508
8509 if (NILP (frame))
8510 frame = selected_frame;
8511 else
8512 CHECK_FRAME (frame);
8513 f = XFRAME (frame);
8514
8515 if (WINDOWP (f->tool_bar_window)
8516 || (w = XWINDOW (f->tool_bar_window),
8517 WINDOW_TOTAL_LINES (w) > 0))
8518 {
8519 update_tool_bar (f, 1);
8520 if (f->n_tool_bar_items)
8521 {
8522 build_desired_tool_bar_string (f);
8523 nlines = tool_bar_lines_needed (f);
8524 }
8525 }
8526
8527 return make_number (nlines);
8528 }
8529
8530
8531 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
8532 height should be changed. */
8533
8534 static int
8535 redisplay_tool_bar (f)
8536 struct frame *f;
8537 {
8538 struct window *w;
8539 struct it it;
8540 struct glyph_row *row;
8541 int change_height_p = 0;
8542
8543 #ifdef USE_GTK
8544 if (FRAME_EXTERNAL_TOOL_BAR(f))
8545 update_frame_tool_bar (f);
8546 return 0;
8547 #endif
8548
8549 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8550 do anything. This means you must start with tool-bar-lines
8551 non-zero to get the auto-sizing effect. Or in other words, you
8552 can turn off tool-bars by specifying tool-bar-lines zero. */
8553 if (!WINDOWP (f->tool_bar_window)
8554 || (w = XWINDOW (f->tool_bar_window),
8555 WINDOW_TOTAL_LINES (w) == 0))
8556 return 0;
8557
8558 /* Set up an iterator for the tool-bar window. */
8559 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8560 it.first_visible_x = 0;
8561 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8562 row = it.glyph_row;
8563
8564 /* Build a string that represents the contents of the tool-bar. */
8565 build_desired_tool_bar_string (f);
8566 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8567
8568 /* Display as many lines as needed to display all tool-bar items. */
8569 while (it.current_y < it.last_visible_y)
8570 display_tool_bar_line (&it);
8571
8572 /* It doesn't make much sense to try scrolling in the tool-bar
8573 window, so don't do it. */
8574 w->desired_matrix->no_scrolling_p = 1;
8575 w->must_be_updated_p = 1;
8576
8577 if (auto_resize_tool_bars_p)
8578 {
8579 int nlines;
8580
8581 /* If we couldn't display everything, change the tool-bar's
8582 height. */
8583 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8584 change_height_p = 1;
8585
8586 /* If there are blank lines at the end, except for a partially
8587 visible blank line at the end that is smaller than
8588 FRAME_LINE_HEIGHT, change the tool-bar's height. */
8589 row = it.glyph_row - 1;
8590 if (!row->displays_text_p
8591 && row->height >= FRAME_LINE_HEIGHT (f))
8592 change_height_p = 1;
8593
8594 /* If row displays tool-bar items, but is partially visible,
8595 change the tool-bar's height. */
8596 if (row->displays_text_p
8597 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8598 change_height_p = 1;
8599
8600 /* Resize windows as needed by changing the `tool-bar-lines'
8601 frame parameter. */
8602 if (change_height_p
8603 && (nlines = tool_bar_lines_needed (f),
8604 nlines != WINDOW_TOTAL_LINES (w)))
8605 {
8606 extern Lisp_Object Qtool_bar_lines;
8607 Lisp_Object frame;
8608 int old_height = WINDOW_TOTAL_LINES (w);
8609
8610 XSETFRAME (frame, f);
8611 clear_glyph_matrix (w->desired_matrix);
8612 Fmodify_frame_parameters (frame,
8613 Fcons (Fcons (Qtool_bar_lines,
8614 make_number (nlines)),
8615 Qnil));
8616 if (WINDOW_TOTAL_LINES (w) != old_height)
8617 fonts_changed_p = 1;
8618 }
8619 }
8620
8621 return change_height_p;
8622 }
8623
8624
8625 /* Get information about the tool-bar item which is displayed in GLYPH
8626 on frame F. Return in *PROP_IDX the index where tool-bar item
8627 properties start in F->tool_bar_items. Value is zero if
8628 GLYPH doesn't display a tool-bar item. */
8629
8630 static int
8631 tool_bar_item_info (f, glyph, prop_idx)
8632 struct frame *f;
8633 struct glyph *glyph;
8634 int *prop_idx;
8635 {
8636 Lisp_Object prop;
8637 int success_p;
8638 int charpos;
8639
8640 /* This function can be called asynchronously, which means we must
8641 exclude any possibility that Fget_text_property signals an
8642 error. */
8643 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8644 charpos = max (0, charpos);
8645
8646 /* Get the text property `menu-item' at pos. The value of that
8647 property is the start index of this item's properties in
8648 F->tool_bar_items. */
8649 prop = Fget_text_property (make_number (charpos),
8650 Qmenu_item, f->current_tool_bar_string);
8651 if (INTEGERP (prop))
8652 {
8653 *prop_idx = XINT (prop);
8654 success_p = 1;
8655 }
8656 else
8657 success_p = 0;
8658
8659 return success_p;
8660 }
8661
8662 \f
8663 /* Get information about the tool-bar item at position X/Y on frame F.
8664 Return in *GLYPH a pointer to the glyph of the tool-bar item in
8665 the current matrix of the tool-bar window of F, or NULL if not
8666 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8667 item in F->tool_bar_items. Value is
8668
8669 -1 if X/Y is not on a tool-bar item
8670 0 if X/Y is on the same item that was highlighted before.
8671 1 otherwise. */
8672
8673 static int
8674 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
8675 struct frame *f;
8676 int x, y;
8677 struct glyph **glyph;
8678 int *hpos, *vpos, *prop_idx;
8679 {
8680 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8681 struct window *w = XWINDOW (f->tool_bar_window);
8682 int area;
8683
8684 /* Find the glyph under X/Y. */
8685 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area, 0);
8686 if (*glyph == NULL)
8687 return -1;
8688
8689 /* Get the start of this tool-bar item's properties in
8690 f->tool_bar_items. */
8691 if (!tool_bar_item_info (f, *glyph, prop_idx))
8692 return -1;
8693
8694 /* Is mouse on the highlighted item? */
8695 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
8696 && *vpos >= dpyinfo->mouse_face_beg_row
8697 && *vpos <= dpyinfo->mouse_face_end_row
8698 && (*vpos > dpyinfo->mouse_face_beg_row
8699 || *hpos >= dpyinfo->mouse_face_beg_col)
8700 && (*vpos < dpyinfo->mouse_face_end_row
8701 || *hpos < dpyinfo->mouse_face_end_col
8702 || dpyinfo->mouse_face_past_end))
8703 return 0;
8704
8705 return 1;
8706 }
8707
8708
8709 /* EXPORT:
8710 Handle mouse button event on the tool-bar of frame F, at
8711 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
8712 0 for button release. MODIFIERS is event modifiers for button
8713 release. */
8714
8715 void
8716 handle_tool_bar_click (f, x, y, down_p, modifiers)
8717 struct frame *f;
8718 int x, y, down_p;
8719 unsigned int modifiers;
8720 {
8721 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8722 struct window *w = XWINDOW (f->tool_bar_window);
8723 int hpos, vpos, prop_idx;
8724 struct glyph *glyph;
8725 Lisp_Object enabled_p;
8726
8727 /* If not on the highlighted tool-bar item, return. */
8728 frame_to_window_pixel_xy (w, &x, &y);
8729 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
8730 return;
8731
8732 /* If item is disabled, do nothing. */
8733 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8734 if (NILP (enabled_p))
8735 return;
8736
8737 if (down_p)
8738 {
8739 /* Show item in pressed state. */
8740 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
8741 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
8742 last_tool_bar_item = prop_idx;
8743 }
8744 else
8745 {
8746 Lisp_Object key, frame;
8747 struct input_event event;
8748 EVENT_INIT (event);
8749
8750 /* Show item in released state. */
8751 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
8752 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
8753
8754 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
8755
8756 XSETFRAME (frame, f);
8757 event.kind = TOOL_BAR_EVENT;
8758 event.frame_or_window = frame;
8759 event.arg = frame;
8760 kbd_buffer_store_event (&event);
8761
8762 event.kind = TOOL_BAR_EVENT;
8763 event.frame_or_window = frame;
8764 event.arg = key;
8765 event.modifiers = modifiers;
8766 kbd_buffer_store_event (&event);
8767 last_tool_bar_item = -1;
8768 }
8769 }
8770
8771
8772 /* Possibly highlight a tool-bar item on frame F when mouse moves to
8773 tool-bar window-relative coordinates X/Y. Called from
8774 note_mouse_highlight. */
8775
8776 static void
8777 note_tool_bar_highlight (f, x, y)
8778 struct frame *f;
8779 int x, y;
8780 {
8781 Lisp_Object window = f->tool_bar_window;
8782 struct window *w = XWINDOW (window);
8783 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8784 int hpos, vpos;
8785 struct glyph *glyph;
8786 struct glyph_row *row;
8787 int i;
8788 Lisp_Object enabled_p;
8789 int prop_idx;
8790 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
8791 int mouse_down_p, rc;
8792
8793 /* Function note_mouse_highlight is called with negative x(y
8794 values when mouse moves outside of the frame. */
8795 if (x <= 0 || y <= 0)
8796 {
8797 clear_mouse_face (dpyinfo);
8798 return;
8799 }
8800
8801 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
8802 if (rc < 0)
8803 {
8804 /* Not on tool-bar item. */
8805 clear_mouse_face (dpyinfo);
8806 return;
8807 }
8808 else if (rc == 0)
8809 /* On same tool-bar item as before. */
8810 goto set_help_echo;
8811
8812 clear_mouse_face (dpyinfo);
8813
8814 /* Mouse is down, but on different tool-bar item? */
8815 mouse_down_p = (dpyinfo->grabbed
8816 && f == last_mouse_frame
8817 && FRAME_LIVE_P (f));
8818 if (mouse_down_p
8819 && last_tool_bar_item != prop_idx)
8820 return;
8821
8822 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
8823 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
8824
8825 /* If tool-bar item is not enabled, don't highlight it. */
8826 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8827 if (!NILP (enabled_p))
8828 {
8829 /* Compute the x-position of the glyph. In front and past the
8830 image is a space. We include this in the highlighted area. */
8831 row = MATRIX_ROW (w->current_matrix, vpos);
8832 for (i = x = 0; i < hpos; ++i)
8833 x += row->glyphs[TEXT_AREA][i].pixel_width;
8834
8835 /* Record this as the current active region. */
8836 dpyinfo->mouse_face_beg_col = hpos;
8837 dpyinfo->mouse_face_beg_row = vpos;
8838 dpyinfo->mouse_face_beg_x = x;
8839 dpyinfo->mouse_face_beg_y = row->y;
8840 dpyinfo->mouse_face_past_end = 0;
8841
8842 dpyinfo->mouse_face_end_col = hpos + 1;
8843 dpyinfo->mouse_face_end_row = vpos;
8844 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
8845 dpyinfo->mouse_face_end_y = row->y;
8846 dpyinfo->mouse_face_window = window;
8847 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
8848
8849 /* Display it as active. */
8850 show_mouse_face (dpyinfo, draw);
8851 dpyinfo->mouse_face_image_state = draw;
8852 }
8853
8854 set_help_echo:
8855
8856 /* Set help_echo_string to a help string to display for this tool-bar item.
8857 XTread_socket does the rest. */
8858 help_echo_object = help_echo_window = Qnil;
8859 help_echo_pos = -1;
8860 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
8861 if (NILP (help_echo_string))
8862 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
8863 }
8864
8865 #endif /* HAVE_WINDOW_SYSTEM */
8866
8867
8868 \f
8869 /***********************************************************************
8870 Fringes
8871 ***********************************************************************/
8872
8873 #ifdef HAVE_WINDOW_SYSTEM
8874
8875 /* An arrow like this: `<-'. */
8876 static unsigned char left_bits[] = {
8877 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
8878
8879 /* Right truncation arrow bitmap `->'. */
8880 static unsigned char right_bits[] = {
8881 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
8882
8883 /* Marker for continued lines. */
8884 static unsigned char continued_bits[] = {
8885 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
8886
8887 /* Marker for continuation lines. */
8888 static unsigned char continuation_bits[] = {
8889 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
8890
8891 /* Overlay arrow bitmap. A triangular arrow. */
8892 static unsigned char ov_bits[] = {
8893 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
8894
8895 /* Bitmap drawn to indicate lines not displaying text if
8896 `indicate-empty-lines' is non-nil. */
8897 static unsigned char zv_bits[] = {
8898 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8899 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8900 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8901 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8902 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8903 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8904 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8905 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00};
8906
8907 struct fringe_bitmap fringe_bitmaps[MAX_FRINGE_BITMAPS] =
8908 {
8909 { 0, 0, 0, NULL /* NO_FRINGE_BITMAP */ },
8910 { 8, sizeof (left_bits), 0, left_bits },
8911 { 8, sizeof (right_bits), 0, right_bits },
8912 { 8, sizeof (continued_bits), 0, continued_bits },
8913 { 8, sizeof (continuation_bits), 0, continuation_bits },
8914 { 8, sizeof (ov_bits), 0, ov_bits },
8915 { 8, sizeof (zv_bits), 3, zv_bits }
8916 };
8917
8918
8919 /* Draw the bitmap WHICH in one of the left or right fringes of
8920 window W. ROW is the glyph row for which to display the bitmap; it
8921 determines the vertical position at which the bitmap has to be
8922 drawn. */
8923
8924 static void
8925 draw_fringe_bitmap (w, row, which, left_p)
8926 struct window *w;
8927 struct glyph_row *row;
8928 enum fringe_bitmap_type which;
8929 int left_p;
8930 {
8931 struct frame *f = XFRAME (WINDOW_FRAME (w));
8932 struct draw_fringe_bitmap_params p;
8933
8934 /* Convert row to frame coordinates. */
8935 p.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
8936
8937 p.which = which;
8938 p.wd = fringe_bitmaps[which].width;
8939
8940 p.h = fringe_bitmaps[which].height;
8941 p.dh = (fringe_bitmaps[which].period
8942 ? (p.y % fringe_bitmaps[which].period)
8943 : 0);
8944 p.h -= p.dh;
8945 /* Clip bitmap if too high. */
8946 if (p.h > row->height)
8947 p.h = row->height;
8948
8949 p.face = FACE_FROM_ID (f, FRINGE_FACE_ID);
8950 PREPARE_FACE_FOR_DISPLAY (f, p.face);
8951
8952 /* Clear left fringe if no bitmap to draw or if bitmap doesn't fill
8953 the fringe. */
8954 p.bx = -1;
8955 if (left_p)
8956 {
8957 int wd = WINDOW_LEFT_FRINGE_WIDTH (w);
8958 int x = window_box_left (w, (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
8959 ? LEFT_MARGIN_AREA
8960 : TEXT_AREA));
8961 if (p.wd > wd)
8962 p.wd = wd;
8963 p.x = x - p.wd - (wd - p.wd) / 2;
8964
8965 if (p.wd < wd || row->height > p.h)
8966 {
8967 /* If W has a vertical border to its left, don't draw over it. */
8968 wd -= ((!WINDOW_LEFTMOST_P (w)
8969 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
8970 ? 1 : 0);
8971 p.bx = x - wd;
8972 p.nx = wd;
8973 }
8974 }
8975 else
8976 {
8977 int x = window_box_right (w,
8978 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
8979 ? RIGHT_MARGIN_AREA
8980 : TEXT_AREA));
8981 int wd = WINDOW_RIGHT_FRINGE_WIDTH (w);
8982 if (p.wd > wd)
8983 p.wd = wd;
8984 p.x = x + (wd - p.wd) / 2;
8985 /* Clear right fringe if no bitmap to draw of if bitmap doesn't fill
8986 the fringe. */
8987 if (p.wd < wd || row->height > p.h)
8988 {
8989 p.bx = x;
8990 p.nx = wd;
8991 }
8992 }
8993
8994 if (p.bx >= 0)
8995 {
8996 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
8997
8998 p.by = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, row->y));
8999 p.ny = row->visible_height;
9000 }
9001
9002 /* Adjust y to the offset in the row to start drawing the bitmap. */
9003 p.y += (row->height - p.h) / 2;
9004
9005 rif->draw_fringe_bitmap (w, row, &p);
9006 }
9007
9008 /* Draw fringe bitmaps for glyph row ROW on window W. Call this
9009 function with input blocked. */
9010
9011 void
9012 draw_row_fringe_bitmaps (w, row)
9013 struct window *w;
9014 struct glyph_row *row;
9015 {
9016 enum fringe_bitmap_type bitmap;
9017
9018 xassert (interrupt_input_blocked);
9019
9020 /* If row is completely invisible, because of vscrolling, we
9021 don't have to draw anything. */
9022 if (row->visible_height <= 0)
9023 return;
9024
9025 if (WINDOW_LEFT_FRINGE_WIDTH (w) != 0)
9026 {
9027 /* Decide which bitmap to draw in the left fringe. */
9028 if (row->overlay_arrow_p)
9029 bitmap = OVERLAY_ARROW_BITMAP;
9030 else if (row->truncated_on_left_p)
9031 bitmap = LEFT_TRUNCATION_BITMAP;
9032 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
9033 bitmap = CONTINUATION_LINE_BITMAP;
9034 else if (row->indicate_empty_line_p)
9035 bitmap = ZV_LINE_BITMAP;
9036 else
9037 bitmap = NO_FRINGE_BITMAP;
9038
9039 draw_fringe_bitmap (w, row, bitmap, 1);
9040 }
9041
9042 if (WINDOW_RIGHT_FRINGE_WIDTH (w) != 0)
9043 {
9044 /* Decide which bitmap to draw in the right fringe. */
9045 if (row->truncated_on_right_p)
9046 bitmap = RIGHT_TRUNCATION_BITMAP;
9047 else if (row->continued_p)
9048 bitmap = CONTINUED_LINE_BITMAP;
9049 else if (row->indicate_empty_line_p && WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
9050 bitmap = ZV_LINE_BITMAP;
9051 else
9052 bitmap = NO_FRINGE_BITMAP;
9053
9054 draw_fringe_bitmap (w, row, bitmap, 0);
9055 }
9056 }
9057
9058
9059 /* Compute actual fringe widths */
9060
9061 void
9062 compute_fringe_widths (f, redraw)
9063 struct frame *f;
9064 int redraw;
9065 {
9066 int o_left = FRAME_LEFT_FRINGE_WIDTH (f);
9067 int o_right = FRAME_RIGHT_FRINGE_WIDTH (f);
9068 int o_cols = FRAME_FRINGE_COLS (f);
9069
9070 Lisp_Object left_fringe = Fassq (Qleft_fringe, f->param_alist);
9071 Lisp_Object right_fringe = Fassq (Qright_fringe, f->param_alist);
9072 int left_fringe_width, right_fringe_width;
9073
9074 if (!NILP (left_fringe))
9075 left_fringe = Fcdr (left_fringe);
9076 if (!NILP (right_fringe))
9077 right_fringe = Fcdr (right_fringe);
9078
9079 left_fringe_width = ((NILP (left_fringe) || !INTEGERP (left_fringe)) ? 8 :
9080 XINT (left_fringe));
9081 right_fringe_width = ((NILP (right_fringe) || !INTEGERP (right_fringe)) ? 8 :
9082 XINT (right_fringe));
9083
9084 if (left_fringe_width || right_fringe_width)
9085 {
9086 int left_wid = left_fringe_width >= 0 ? left_fringe_width : -left_fringe_width;
9087 int right_wid = right_fringe_width >= 0 ? right_fringe_width : -right_fringe_width;
9088 int conf_wid = left_wid + right_wid;
9089 int font_wid = FRAME_COLUMN_WIDTH (f);
9090 int cols = (left_wid + right_wid + font_wid-1) / font_wid;
9091 int real_wid = cols * font_wid;
9092 if (left_wid && right_wid)
9093 {
9094 if (left_fringe_width < 0)
9095 {
9096 /* Left fringe width is fixed, adjust right fringe if necessary */
9097 FRAME_LEFT_FRINGE_WIDTH (f) = left_wid;
9098 FRAME_RIGHT_FRINGE_WIDTH (f) = real_wid - left_wid;
9099 }
9100 else if (right_fringe_width < 0)
9101 {
9102 /* Right fringe width is fixed, adjust left fringe if necessary */
9103 FRAME_LEFT_FRINGE_WIDTH (f) = real_wid - right_wid;
9104 FRAME_RIGHT_FRINGE_WIDTH (f) = right_wid;
9105 }
9106 else
9107 {
9108 /* Adjust both fringes with an equal amount.
9109 Note that we are doing integer arithmetic here, so don't
9110 lose a pixel if the total width is an odd number. */
9111 int fill = real_wid - conf_wid;
9112 FRAME_LEFT_FRINGE_WIDTH (f) = left_wid + fill/2;
9113 FRAME_RIGHT_FRINGE_WIDTH (f) = right_wid + fill - fill/2;
9114 }
9115 }
9116 else if (left_fringe_width)
9117 {
9118 FRAME_LEFT_FRINGE_WIDTH (f) = real_wid;
9119 FRAME_RIGHT_FRINGE_WIDTH (f) = 0;
9120 }
9121 else
9122 {
9123 FRAME_LEFT_FRINGE_WIDTH (f) = 0;
9124 FRAME_RIGHT_FRINGE_WIDTH (f) = real_wid;
9125 }
9126 FRAME_FRINGE_COLS (f) = cols;
9127 }
9128 else
9129 {
9130 FRAME_LEFT_FRINGE_WIDTH (f) = 0;
9131 FRAME_RIGHT_FRINGE_WIDTH (f) = 0;
9132 FRAME_FRINGE_COLS (f) = 0;
9133 }
9134
9135 if (redraw && FRAME_VISIBLE_P (f))
9136 if (o_left != FRAME_LEFT_FRINGE_WIDTH (f) ||
9137 o_right != FRAME_RIGHT_FRINGE_WIDTH (f) ||
9138 o_cols != FRAME_FRINGE_COLS (f))
9139 redraw_frame (f);
9140 }
9141
9142 #endif /* HAVE_WINDOW_SYSTEM */
9143
9144
9145 \f
9146 /************************************************************************
9147 Horizontal scrolling
9148 ************************************************************************/
9149
9150 static int hscroll_window_tree P_ ((Lisp_Object));
9151 static int hscroll_windows P_ ((Lisp_Object));
9152
9153 /* For all leaf windows in the window tree rooted at WINDOW, set their
9154 hscroll value so that PT is (i) visible in the window, and (ii) so
9155 that it is not within a certain margin at the window's left and
9156 right border. Value is non-zero if any window's hscroll has been
9157 changed. */
9158
9159 static int
9160 hscroll_window_tree (window)
9161 Lisp_Object window;
9162 {
9163 int hscrolled_p = 0;
9164 int hscroll_relative_p = FLOATP (Vhscroll_step);
9165 int hscroll_step_abs = 0;
9166 double hscroll_step_rel = 0;
9167
9168 if (hscroll_relative_p)
9169 {
9170 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9171 if (hscroll_step_rel < 0)
9172 {
9173 hscroll_relative_p = 0;
9174 hscroll_step_abs = 0;
9175 }
9176 }
9177 else if (INTEGERP (Vhscroll_step))
9178 {
9179 hscroll_step_abs = XINT (Vhscroll_step);
9180 if (hscroll_step_abs < 0)
9181 hscroll_step_abs = 0;
9182 }
9183 else
9184 hscroll_step_abs = 0;
9185
9186 while (WINDOWP (window))
9187 {
9188 struct window *w = XWINDOW (window);
9189
9190 if (WINDOWP (w->hchild))
9191 hscrolled_p |= hscroll_window_tree (w->hchild);
9192 else if (WINDOWP (w->vchild))
9193 hscrolled_p |= hscroll_window_tree (w->vchild);
9194 else if (w->cursor.vpos >= 0)
9195 {
9196 int h_margin;
9197 int text_area_width;
9198 struct glyph_row *current_cursor_row
9199 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9200 struct glyph_row *desired_cursor_row
9201 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9202 struct glyph_row *cursor_row
9203 = (desired_cursor_row->enabled_p
9204 ? desired_cursor_row
9205 : current_cursor_row);
9206
9207 text_area_width = window_box_width (w, TEXT_AREA);
9208
9209 /* Scroll when cursor is inside this scroll margin. */
9210 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9211
9212 if ((XFASTINT (w->hscroll)
9213 && w->cursor.x <= h_margin)
9214 || (cursor_row->enabled_p
9215 && cursor_row->truncated_on_right_p
9216 && (w->cursor.x >= text_area_width - h_margin)))
9217 {
9218 struct it it;
9219 int hscroll;
9220 struct buffer *saved_current_buffer;
9221 int pt;
9222 int wanted_x;
9223
9224 /* Find point in a display of infinite width. */
9225 saved_current_buffer = current_buffer;
9226 current_buffer = XBUFFER (w->buffer);
9227
9228 if (w == XWINDOW (selected_window))
9229 pt = BUF_PT (current_buffer);
9230 else
9231 {
9232 pt = marker_position (w->pointm);
9233 pt = max (BEGV, pt);
9234 pt = min (ZV, pt);
9235 }
9236
9237 /* Move iterator to pt starting at cursor_row->start in
9238 a line with infinite width. */
9239 init_to_row_start (&it, w, cursor_row);
9240 it.last_visible_x = INFINITY;
9241 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9242 current_buffer = saved_current_buffer;
9243
9244 /* Position cursor in window. */
9245 if (!hscroll_relative_p && hscroll_step_abs == 0)
9246 hscroll = max (0, it.current_x - text_area_width / 2)
9247 / FRAME_COLUMN_WIDTH (it.f);
9248 else if (w->cursor.x >= text_area_width - h_margin)
9249 {
9250 if (hscroll_relative_p)
9251 wanted_x = text_area_width * (1 - hscroll_step_rel)
9252 - h_margin;
9253 else
9254 wanted_x = text_area_width
9255 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9256 - h_margin;
9257 hscroll
9258 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9259 }
9260 else
9261 {
9262 if (hscroll_relative_p)
9263 wanted_x = text_area_width * hscroll_step_rel
9264 + h_margin;
9265 else
9266 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9267 + h_margin;
9268 hscroll
9269 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9270 }
9271 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9272
9273 /* Don't call Fset_window_hscroll if value hasn't
9274 changed because it will prevent redisplay
9275 optimizations. */
9276 if (XFASTINT (w->hscroll) != hscroll)
9277 {
9278 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9279 w->hscroll = make_number (hscroll);
9280 hscrolled_p = 1;
9281 }
9282 }
9283 }
9284
9285 window = w->next;
9286 }
9287
9288 /* Value is non-zero if hscroll of any leaf window has been changed. */
9289 return hscrolled_p;
9290 }
9291
9292
9293 /* Set hscroll so that cursor is visible and not inside horizontal
9294 scroll margins for all windows in the tree rooted at WINDOW. See
9295 also hscroll_window_tree above. Value is non-zero if any window's
9296 hscroll has been changed. If it has, desired matrices on the frame
9297 of WINDOW are cleared. */
9298
9299 static int
9300 hscroll_windows (window)
9301 Lisp_Object window;
9302 {
9303 int hscrolled_p;
9304
9305 if (automatic_hscrolling_p)
9306 {
9307 hscrolled_p = hscroll_window_tree (window);
9308 if (hscrolled_p)
9309 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9310 }
9311 else
9312 hscrolled_p = 0;
9313 return hscrolled_p;
9314 }
9315
9316
9317 \f
9318 /************************************************************************
9319 Redisplay
9320 ************************************************************************/
9321
9322 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9323 to a non-zero value. This is sometimes handy to have in a debugger
9324 session. */
9325
9326 #if GLYPH_DEBUG
9327
9328 /* First and last unchanged row for try_window_id. */
9329
9330 int debug_first_unchanged_at_end_vpos;
9331 int debug_last_unchanged_at_beg_vpos;
9332
9333 /* Delta vpos and y. */
9334
9335 int debug_dvpos, debug_dy;
9336
9337 /* Delta in characters and bytes for try_window_id. */
9338
9339 int debug_delta, debug_delta_bytes;
9340
9341 /* Values of window_end_pos and window_end_vpos at the end of
9342 try_window_id. */
9343
9344 EMACS_INT debug_end_pos, debug_end_vpos;
9345
9346 /* Append a string to W->desired_matrix->method. FMT is a printf
9347 format string. A1...A9 are a supplement for a variable-length
9348 argument list. If trace_redisplay_p is non-zero also printf the
9349 resulting string to stderr. */
9350
9351 static void
9352 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9353 struct window *w;
9354 char *fmt;
9355 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9356 {
9357 char buffer[512];
9358 char *method = w->desired_matrix->method;
9359 int len = strlen (method);
9360 int size = sizeof w->desired_matrix->method;
9361 int remaining = size - len - 1;
9362
9363 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9364 if (len && remaining)
9365 {
9366 method[len] = '|';
9367 --remaining, ++len;
9368 }
9369
9370 strncpy (method + len, buffer, remaining);
9371
9372 if (trace_redisplay_p)
9373 fprintf (stderr, "%p (%s): %s\n",
9374 w,
9375 ((BUFFERP (w->buffer)
9376 && STRINGP (XBUFFER (w->buffer)->name))
9377 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9378 : "no buffer"),
9379 buffer);
9380 }
9381
9382 #endif /* GLYPH_DEBUG */
9383
9384
9385 /* Value is non-zero if all changes in window W, which displays
9386 current_buffer, are in the text between START and END. START is a
9387 buffer position, END is given as a distance from Z. Used in
9388 redisplay_internal for display optimization. */
9389
9390 static INLINE int
9391 text_outside_line_unchanged_p (w, start, end)
9392 struct window *w;
9393 int start, end;
9394 {
9395 int unchanged_p = 1;
9396
9397 /* If text or overlays have changed, see where. */
9398 if (XFASTINT (w->last_modified) < MODIFF
9399 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9400 {
9401 /* Gap in the line? */
9402 if (GPT < start || Z - GPT < end)
9403 unchanged_p = 0;
9404
9405 /* Changes start in front of the line, or end after it? */
9406 if (unchanged_p
9407 && (BEG_UNCHANGED < start - 1
9408 || END_UNCHANGED < end))
9409 unchanged_p = 0;
9410
9411 /* If selective display, can't optimize if changes start at the
9412 beginning of the line. */
9413 if (unchanged_p
9414 && INTEGERP (current_buffer->selective_display)
9415 && XINT (current_buffer->selective_display) > 0
9416 && (BEG_UNCHANGED < start || GPT <= start))
9417 unchanged_p = 0;
9418
9419 /* If there are overlays at the start or end of the line, these
9420 may have overlay strings with newlines in them. A change at
9421 START, for instance, may actually concern the display of such
9422 overlay strings as well, and they are displayed on different
9423 lines. So, quickly rule out this case. (For the future, it
9424 might be desirable to implement something more telling than
9425 just BEG/END_UNCHANGED.) */
9426 if (unchanged_p)
9427 {
9428 if (BEG + BEG_UNCHANGED == start
9429 && overlay_touches_p (start))
9430 unchanged_p = 0;
9431 if (END_UNCHANGED == end
9432 && overlay_touches_p (Z - end))
9433 unchanged_p = 0;
9434 }
9435 }
9436
9437 return unchanged_p;
9438 }
9439
9440
9441 /* Do a frame update, taking possible shortcuts into account. This is
9442 the main external entry point for redisplay.
9443
9444 If the last redisplay displayed an echo area message and that message
9445 is no longer requested, we clear the echo area or bring back the
9446 mini-buffer if that is in use. */
9447
9448 void
9449 redisplay ()
9450 {
9451 redisplay_internal (0);
9452 }
9453
9454
9455 /* Return 1 if point moved out of or into a composition. Otherwise
9456 return 0. PREV_BUF and PREV_PT are the last point buffer and
9457 position. BUF and PT are the current point buffer and position. */
9458
9459 int
9460 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9461 struct buffer *prev_buf, *buf;
9462 int prev_pt, pt;
9463 {
9464 int start, end;
9465 Lisp_Object prop;
9466 Lisp_Object buffer;
9467
9468 XSETBUFFER (buffer, buf);
9469 /* Check a composition at the last point if point moved within the
9470 same buffer. */
9471 if (prev_buf == buf)
9472 {
9473 if (prev_pt == pt)
9474 /* Point didn't move. */
9475 return 0;
9476
9477 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9478 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9479 && COMPOSITION_VALID_P (start, end, prop)
9480 && start < prev_pt && end > prev_pt)
9481 /* The last point was within the composition. Return 1 iff
9482 point moved out of the composition. */
9483 return (pt <= start || pt >= end);
9484 }
9485
9486 /* Check a composition at the current point. */
9487 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9488 && find_composition (pt, -1, &start, &end, &prop, buffer)
9489 && COMPOSITION_VALID_P (start, end, prop)
9490 && start < pt && end > pt);
9491 }
9492
9493
9494 /* Reconsider the setting of B->clip_changed which is displayed
9495 in window W. */
9496
9497 static INLINE void
9498 reconsider_clip_changes (w, b)
9499 struct window *w;
9500 struct buffer *b;
9501 {
9502 if (b->clip_changed
9503 && !NILP (w->window_end_valid)
9504 && w->current_matrix->buffer == b
9505 && w->current_matrix->zv == BUF_ZV (b)
9506 && w->current_matrix->begv == BUF_BEGV (b))
9507 b->clip_changed = 0;
9508
9509 /* If display wasn't paused, and W is not a tool bar window, see if
9510 point has been moved into or out of a composition. In that case,
9511 we set b->clip_changed to 1 to force updating the screen. If
9512 b->clip_changed has already been set to 1, we can skip this
9513 check. */
9514 if (!b->clip_changed
9515 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9516 {
9517 int pt;
9518
9519 if (w == XWINDOW (selected_window))
9520 pt = BUF_PT (current_buffer);
9521 else
9522 pt = marker_position (w->pointm);
9523
9524 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
9525 || pt != XINT (w->last_point))
9526 && check_point_in_composition (w->current_matrix->buffer,
9527 XINT (w->last_point),
9528 XBUFFER (w->buffer), pt))
9529 b->clip_changed = 1;
9530 }
9531 }
9532 \f
9533 #define STOP_POLLING \
9534 do { if (! polling_stopped_here) stop_polling (); \
9535 polling_stopped_here = 1; } while (0)
9536
9537 #define RESUME_POLLING \
9538 do { if (polling_stopped_here) start_polling (); \
9539 polling_stopped_here = 0; } while (0)
9540
9541
9542 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9543 response to any user action; therefore, we should preserve the echo
9544 area. (Actually, our caller does that job.) Perhaps in the future
9545 avoid recentering windows if it is not necessary; currently that
9546 causes some problems. */
9547
9548 static void
9549 redisplay_internal (preserve_echo_area)
9550 int preserve_echo_area;
9551 {
9552 struct window *w = XWINDOW (selected_window);
9553 struct frame *f = XFRAME (w->frame);
9554 int pause;
9555 int must_finish = 0;
9556 struct text_pos tlbufpos, tlendpos;
9557 int number_of_visible_frames;
9558 int count;
9559 struct frame *sf = SELECTED_FRAME ();
9560 int polling_stopped_here = 0;
9561
9562 /* Non-zero means redisplay has to consider all windows on all
9563 frames. Zero means, only selected_window is considered. */
9564 int consider_all_windows_p;
9565
9566 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9567
9568 /* No redisplay if running in batch mode or frame is not yet fully
9569 initialized, or redisplay is explicitly turned off by setting
9570 Vinhibit_redisplay. */
9571 if (noninteractive
9572 || !NILP (Vinhibit_redisplay)
9573 || !f->glyphs_initialized_p)
9574 return;
9575
9576 /* The flag redisplay_performed_directly_p is set by
9577 direct_output_for_insert when it already did the whole screen
9578 update necessary. */
9579 if (redisplay_performed_directly_p)
9580 {
9581 redisplay_performed_directly_p = 0;
9582 if (!hscroll_windows (selected_window))
9583 return;
9584 }
9585
9586 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9587 if (popup_activated ())
9588 return;
9589 #endif
9590
9591 /* I don't think this happens but let's be paranoid. */
9592 if (redisplaying_p)
9593 return;
9594
9595 /* Record a function that resets redisplaying_p to its old value
9596 when we leave this function. */
9597 count = SPECPDL_INDEX ();
9598 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
9599 ++redisplaying_p;
9600 specbind (Qinhibit_free_realized_faces, Qnil);
9601
9602 retry:
9603 pause = 0;
9604 reconsider_clip_changes (w, current_buffer);
9605
9606 /* If new fonts have been loaded that make a glyph matrix adjustment
9607 necessary, do it. */
9608 if (fonts_changed_p)
9609 {
9610 adjust_glyphs (NULL);
9611 ++windows_or_buffers_changed;
9612 fonts_changed_p = 0;
9613 }
9614
9615 /* If face_change_count is non-zero, init_iterator will free all
9616 realized faces, which includes the faces referenced from current
9617 matrices. So, we can't reuse current matrices in this case. */
9618 if (face_change_count)
9619 ++windows_or_buffers_changed;
9620
9621 if (! FRAME_WINDOW_P (sf)
9622 && previous_terminal_frame != sf)
9623 {
9624 /* Since frames on an ASCII terminal share the same display
9625 area, displaying a different frame means redisplay the whole
9626 thing. */
9627 windows_or_buffers_changed++;
9628 SET_FRAME_GARBAGED (sf);
9629 XSETFRAME (Vterminal_frame, sf);
9630 }
9631 previous_terminal_frame = sf;
9632
9633 /* Set the visible flags for all frames. Do this before checking
9634 for resized or garbaged frames; they want to know if their frames
9635 are visible. See the comment in frame.h for
9636 FRAME_SAMPLE_VISIBILITY. */
9637 {
9638 Lisp_Object tail, frame;
9639
9640 number_of_visible_frames = 0;
9641
9642 FOR_EACH_FRAME (tail, frame)
9643 {
9644 struct frame *f = XFRAME (frame);
9645
9646 FRAME_SAMPLE_VISIBILITY (f);
9647 if (FRAME_VISIBLE_P (f))
9648 ++number_of_visible_frames;
9649 clear_desired_matrices (f);
9650 }
9651 }
9652
9653 /* Notice any pending interrupt request to change frame size. */
9654 do_pending_window_change (1);
9655
9656 /* Clear frames marked as garbaged. */
9657 if (frame_garbaged)
9658 clear_garbaged_frames ();
9659
9660 /* Build menubar and tool-bar items. */
9661 prepare_menu_bars ();
9662
9663 if (windows_or_buffers_changed)
9664 update_mode_lines++;
9665
9666 /* Detect case that we need to write or remove a star in the mode line. */
9667 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
9668 {
9669 w->update_mode_line = Qt;
9670 if (buffer_shared > 1)
9671 update_mode_lines++;
9672 }
9673
9674 /* If %c is in the mode line, update it if needed. */
9675 if (!NILP (w->column_number_displayed)
9676 /* This alternative quickly identifies a common case
9677 where no change is needed. */
9678 && !(PT == XFASTINT (w->last_point)
9679 && XFASTINT (w->last_modified) >= MODIFF
9680 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9681 && (XFASTINT (w->column_number_displayed)
9682 != (int) current_column ())) /* iftc */
9683 w->update_mode_line = Qt;
9684
9685 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
9686
9687 /* The variable buffer_shared is set in redisplay_window and
9688 indicates that we redisplay a buffer in different windows. See
9689 there. */
9690 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
9691 || cursor_type_changed);
9692
9693 /* If specs for an arrow have changed, do thorough redisplay
9694 to ensure we remove any arrow that should no longer exist. */
9695 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
9696 || ! EQ (Voverlay_arrow_string, last_arrow_string))
9697 consider_all_windows_p = windows_or_buffers_changed = 1;
9698
9699 /* Normally the message* functions will have already displayed and
9700 updated the echo area, but the frame may have been trashed, or
9701 the update may have been preempted, so display the echo area
9702 again here. Checking message_cleared_p captures the case that
9703 the echo area should be cleared. */
9704 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
9705 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
9706 || (message_cleared_p
9707 && minibuf_level == 0
9708 /* If the mini-window is currently selected, this means the
9709 echo-area doesn't show through. */
9710 && !MINI_WINDOW_P (XWINDOW (selected_window))))
9711 {
9712 int window_height_changed_p = echo_area_display (0);
9713 must_finish = 1;
9714
9715 /* If we don't display the current message, don't clear the
9716 message_cleared_p flag, because, if we did, we wouldn't clear
9717 the echo area in the next redisplay which doesn't preserve
9718 the echo area. */
9719 if (!display_last_displayed_message_p)
9720 message_cleared_p = 0;
9721
9722 if (fonts_changed_p)
9723 goto retry;
9724 else if (window_height_changed_p)
9725 {
9726 consider_all_windows_p = 1;
9727 ++update_mode_lines;
9728 ++windows_or_buffers_changed;
9729
9730 /* If window configuration was changed, frames may have been
9731 marked garbaged. Clear them or we will experience
9732 surprises wrt scrolling. */
9733 if (frame_garbaged)
9734 clear_garbaged_frames ();
9735 }
9736 }
9737 else if (EQ (selected_window, minibuf_window)
9738 && (current_buffer->clip_changed
9739 || XFASTINT (w->last_modified) < MODIFF
9740 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9741 && resize_mini_window (w, 0))
9742 {
9743 /* Resized active mini-window to fit the size of what it is
9744 showing if its contents might have changed. */
9745 must_finish = 1;
9746 consider_all_windows_p = 1;
9747 ++windows_or_buffers_changed;
9748 ++update_mode_lines;
9749
9750 /* If window configuration was changed, frames may have been
9751 marked garbaged. Clear them or we will experience
9752 surprises wrt scrolling. */
9753 if (frame_garbaged)
9754 clear_garbaged_frames ();
9755 }
9756
9757
9758 /* If showing the region, and mark has changed, we must redisplay
9759 the whole window. The assignment to this_line_start_pos prevents
9760 the optimization directly below this if-statement. */
9761 if (((!NILP (Vtransient_mark_mode)
9762 && !NILP (XBUFFER (w->buffer)->mark_active))
9763 != !NILP (w->region_showing))
9764 || (!NILP (w->region_showing)
9765 && !EQ (w->region_showing,
9766 Fmarker_position (XBUFFER (w->buffer)->mark))))
9767 CHARPOS (this_line_start_pos) = 0;
9768
9769 /* Optimize the case that only the line containing the cursor in the
9770 selected window has changed. Variables starting with this_ are
9771 set in display_line and record information about the line
9772 containing the cursor. */
9773 tlbufpos = this_line_start_pos;
9774 tlendpos = this_line_end_pos;
9775 if (!consider_all_windows_p
9776 && CHARPOS (tlbufpos) > 0
9777 && NILP (w->update_mode_line)
9778 && !current_buffer->clip_changed
9779 && !current_buffer->prevent_redisplay_optimizations_p
9780 && FRAME_VISIBLE_P (XFRAME (w->frame))
9781 && !FRAME_OBSCURED_P (XFRAME (w->frame))
9782 /* Make sure recorded data applies to current buffer, etc. */
9783 && this_line_buffer == current_buffer
9784 && current_buffer == XBUFFER (w->buffer)
9785 && NILP (w->force_start)
9786 && NILP (w->optional_new_start)
9787 /* Point must be on the line that we have info recorded about. */
9788 && PT >= CHARPOS (tlbufpos)
9789 && PT <= Z - CHARPOS (tlendpos)
9790 /* All text outside that line, including its final newline,
9791 must be unchanged */
9792 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
9793 CHARPOS (tlendpos)))
9794 {
9795 if (CHARPOS (tlbufpos) > BEGV
9796 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
9797 && (CHARPOS (tlbufpos) == ZV
9798 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
9799 /* Former continuation line has disappeared by becoming empty */
9800 goto cancel;
9801 else if (XFASTINT (w->last_modified) < MODIFF
9802 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
9803 || MINI_WINDOW_P (w))
9804 {
9805 /* We have to handle the case of continuation around a
9806 wide-column character (See the comment in indent.c around
9807 line 885).
9808
9809 For instance, in the following case:
9810
9811 -------- Insert --------
9812 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
9813 J_I_ ==> J_I_ `^^' are cursors.
9814 ^^ ^^
9815 -------- --------
9816
9817 As we have to redraw the line above, we should goto cancel. */
9818
9819 struct it it;
9820 int line_height_before = this_line_pixel_height;
9821
9822 /* Note that start_display will handle the case that the
9823 line starting at tlbufpos is a continuation lines. */
9824 start_display (&it, w, tlbufpos);
9825
9826 /* Implementation note: It this still necessary? */
9827 if (it.current_x != this_line_start_x)
9828 goto cancel;
9829
9830 TRACE ((stderr, "trying display optimization 1\n"));
9831 w->cursor.vpos = -1;
9832 overlay_arrow_seen = 0;
9833 it.vpos = this_line_vpos;
9834 it.current_y = this_line_y;
9835 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
9836 display_line (&it);
9837
9838 /* If line contains point, is not continued,
9839 and ends at same distance from eob as before, we win */
9840 if (w->cursor.vpos >= 0
9841 /* Line is not continued, otherwise this_line_start_pos
9842 would have been set to 0 in display_line. */
9843 && CHARPOS (this_line_start_pos)
9844 /* Line ends as before. */
9845 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
9846 /* Line has same height as before. Otherwise other lines
9847 would have to be shifted up or down. */
9848 && this_line_pixel_height == line_height_before)
9849 {
9850 /* If this is not the window's last line, we must adjust
9851 the charstarts of the lines below. */
9852 if (it.current_y < it.last_visible_y)
9853 {
9854 struct glyph_row *row
9855 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
9856 int delta, delta_bytes;
9857
9858 if (Z - CHARPOS (tlendpos) == ZV)
9859 {
9860 /* This line ends at end of (accessible part of)
9861 buffer. There is no newline to count. */
9862 delta = (Z
9863 - CHARPOS (tlendpos)
9864 - MATRIX_ROW_START_CHARPOS (row));
9865 delta_bytes = (Z_BYTE
9866 - BYTEPOS (tlendpos)
9867 - MATRIX_ROW_START_BYTEPOS (row));
9868 }
9869 else
9870 {
9871 /* This line ends in a newline. Must take
9872 account of the newline and the rest of the
9873 text that follows. */
9874 delta = (Z
9875 - CHARPOS (tlendpos)
9876 - MATRIX_ROW_START_CHARPOS (row));
9877 delta_bytes = (Z_BYTE
9878 - BYTEPOS (tlendpos)
9879 - MATRIX_ROW_START_BYTEPOS (row));
9880 }
9881
9882 increment_matrix_positions (w->current_matrix,
9883 this_line_vpos + 1,
9884 w->current_matrix->nrows,
9885 delta, delta_bytes);
9886 }
9887
9888 /* If this row displays text now but previously didn't,
9889 or vice versa, w->window_end_vpos may have to be
9890 adjusted. */
9891 if ((it.glyph_row - 1)->displays_text_p)
9892 {
9893 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
9894 XSETINT (w->window_end_vpos, this_line_vpos);
9895 }
9896 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
9897 && this_line_vpos > 0)
9898 XSETINT (w->window_end_vpos, this_line_vpos - 1);
9899 w->window_end_valid = Qnil;
9900
9901 /* Update hint: No need to try to scroll in update_window. */
9902 w->desired_matrix->no_scrolling_p = 1;
9903
9904 #if GLYPH_DEBUG
9905 *w->desired_matrix->method = 0;
9906 debug_method_add (w, "optimization 1");
9907 #endif
9908 goto update;
9909 }
9910 else
9911 goto cancel;
9912 }
9913 else if (/* Cursor position hasn't changed. */
9914 PT == XFASTINT (w->last_point)
9915 /* Make sure the cursor was last displayed
9916 in this window. Otherwise we have to reposition it. */
9917 && 0 <= w->cursor.vpos
9918 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
9919 {
9920 if (!must_finish)
9921 {
9922 do_pending_window_change (1);
9923
9924 /* We used to always goto end_of_redisplay here, but this
9925 isn't enough if we have a blinking cursor. */
9926 if (w->cursor_off_p == w->last_cursor_off_p)
9927 goto end_of_redisplay;
9928 }
9929 goto update;
9930 }
9931 /* If highlighting the region, or if the cursor is in the echo area,
9932 then we can't just move the cursor. */
9933 else if (! (!NILP (Vtransient_mark_mode)
9934 && !NILP (current_buffer->mark_active))
9935 && (EQ (selected_window, current_buffer->last_selected_window)
9936 || highlight_nonselected_windows)
9937 && NILP (w->region_showing)
9938 && NILP (Vshow_trailing_whitespace)
9939 && !cursor_in_echo_area)
9940 {
9941 struct it it;
9942 struct glyph_row *row;
9943
9944 /* Skip from tlbufpos to PT and see where it is. Note that
9945 PT may be in invisible text. If so, we will end at the
9946 next visible position. */
9947 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
9948 NULL, DEFAULT_FACE_ID);
9949 it.current_x = this_line_start_x;
9950 it.current_y = this_line_y;
9951 it.vpos = this_line_vpos;
9952
9953 /* The call to move_it_to stops in front of PT, but
9954 moves over before-strings. */
9955 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
9956
9957 if (it.vpos == this_line_vpos
9958 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
9959 row->enabled_p))
9960 {
9961 xassert (this_line_vpos == it.vpos);
9962 xassert (this_line_y == it.current_y);
9963 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9964 #if GLYPH_DEBUG
9965 *w->desired_matrix->method = 0;
9966 debug_method_add (w, "optimization 3");
9967 #endif
9968 goto update;
9969 }
9970 else
9971 goto cancel;
9972 }
9973
9974 cancel:
9975 /* Text changed drastically or point moved off of line. */
9976 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
9977 }
9978
9979 CHARPOS (this_line_start_pos) = 0;
9980 consider_all_windows_p |= buffer_shared > 1;
9981 ++clear_face_cache_count;
9982
9983
9984 /* Build desired matrices, and update the display. If
9985 consider_all_windows_p is non-zero, do it for all windows on all
9986 frames. Otherwise do it for selected_window, only. */
9987
9988 if (consider_all_windows_p)
9989 {
9990 Lisp_Object tail, frame;
9991 int i, n = 0, size = 50;
9992 struct frame **updated
9993 = (struct frame **) alloca (size * sizeof *updated);
9994
9995 /* Clear the face cache eventually. */
9996 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
9997 {
9998 clear_face_cache (0);
9999 clear_face_cache_count = 0;
10000 }
10001
10002 /* Recompute # windows showing selected buffer. This will be
10003 incremented each time such a window is displayed. */
10004 buffer_shared = 0;
10005
10006 FOR_EACH_FRAME (tail, frame)
10007 {
10008 struct frame *f = XFRAME (frame);
10009
10010 if (FRAME_WINDOW_P (f) || f == sf)
10011 {
10012 #ifdef HAVE_WINDOW_SYSTEM
10013 if (clear_face_cache_count % 50 == 0
10014 && FRAME_WINDOW_P (f))
10015 clear_image_cache (f, 0);
10016 #endif /* HAVE_WINDOW_SYSTEM */
10017
10018 /* Mark all the scroll bars to be removed; we'll redeem
10019 the ones we want when we redisplay their windows. */
10020 if (condemn_scroll_bars_hook)
10021 condemn_scroll_bars_hook (f);
10022
10023 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10024 redisplay_windows (FRAME_ROOT_WINDOW (f));
10025
10026 /* Any scroll bars which redisplay_windows should have
10027 nuked should now go away. */
10028 if (judge_scroll_bars_hook)
10029 judge_scroll_bars_hook (f);
10030
10031 /* If fonts changed, display again. */
10032 /* ??? rms: I suspect it is a mistake to jump all the way
10033 back to retry here. It should just retry this frame. */
10034 if (fonts_changed_p)
10035 goto retry;
10036
10037 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10038 {
10039 /* See if we have to hscroll. */
10040 if (hscroll_windows (f->root_window))
10041 goto retry;
10042
10043 /* Prevent various kinds of signals during display
10044 update. stdio is not robust about handling
10045 signals, which can cause an apparent I/O
10046 error. */
10047 if (interrupt_input)
10048 unrequest_sigio ();
10049 STOP_POLLING;
10050
10051 /* Update the display. */
10052 set_window_update_flags (XWINDOW (f->root_window), 1);
10053 pause |= update_frame (f, 0, 0);
10054 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10055 if (pause)
10056 break;
10057 #endif
10058
10059 if (n == size)
10060 {
10061 int nbytes = size * sizeof *updated;
10062 struct frame **p = (struct frame **) alloca (2 * nbytes);
10063 bcopy (updated, p, nbytes);
10064 size *= 2;
10065 }
10066
10067 updated[n++] = f;
10068 }
10069 }
10070 }
10071
10072 /* Do the mark_window_display_accurate after all windows have
10073 been redisplayed because this call resets flags in buffers
10074 which are needed for proper redisplay. */
10075 for (i = 0; i < n; ++i)
10076 {
10077 struct frame *f = updated[i];
10078 mark_window_display_accurate (f->root_window, 1);
10079 if (frame_up_to_date_hook)
10080 frame_up_to_date_hook (f);
10081 }
10082 }
10083 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10084 {
10085 Lisp_Object mini_window;
10086 struct frame *mini_frame;
10087
10088 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10089 /* Use list_of_error, not Qerror, so that
10090 we catch only errors and don't run the debugger. */
10091 internal_condition_case_1 (redisplay_window_1, selected_window,
10092 list_of_error,
10093 redisplay_window_error);
10094
10095 /* Compare desired and current matrices, perform output. */
10096
10097 update:
10098 /* If fonts changed, display again. */
10099 if (fonts_changed_p)
10100 goto retry;
10101
10102 /* Prevent various kinds of signals during display update.
10103 stdio is not robust about handling signals,
10104 which can cause an apparent I/O error. */
10105 if (interrupt_input)
10106 unrequest_sigio ();
10107 STOP_POLLING;
10108
10109 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10110 {
10111 if (hscroll_windows (selected_window))
10112 goto retry;
10113
10114 XWINDOW (selected_window)->must_be_updated_p = 1;
10115 pause = update_frame (sf, 0, 0);
10116 }
10117
10118 /* We may have called echo_area_display at the top of this
10119 function. If the echo area is on another frame, that may
10120 have put text on a frame other than the selected one, so the
10121 above call to update_frame would not have caught it. Catch
10122 it here. */
10123 mini_window = FRAME_MINIBUF_WINDOW (sf);
10124 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10125
10126 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10127 {
10128 XWINDOW (mini_window)->must_be_updated_p = 1;
10129 pause |= update_frame (mini_frame, 0, 0);
10130 if (!pause && hscroll_windows (mini_window))
10131 goto retry;
10132 }
10133 }
10134
10135 /* If display was paused because of pending input, make sure we do a
10136 thorough update the next time. */
10137 if (pause)
10138 {
10139 /* Prevent the optimization at the beginning of
10140 redisplay_internal that tries a single-line update of the
10141 line containing the cursor in the selected window. */
10142 CHARPOS (this_line_start_pos) = 0;
10143
10144 /* Let the overlay arrow be updated the next time. */
10145 if (!NILP (last_arrow_position))
10146 {
10147 last_arrow_position = Qt;
10148 last_arrow_string = Qt;
10149 }
10150
10151 /* If we pause after scrolling, some rows in the current
10152 matrices of some windows are not valid. */
10153 if (!WINDOW_FULL_WIDTH_P (w)
10154 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10155 update_mode_lines = 1;
10156 }
10157 else
10158 {
10159 if (!consider_all_windows_p)
10160 {
10161 /* This has already been done above if
10162 consider_all_windows_p is set. */
10163 mark_window_display_accurate_1 (w, 1);
10164
10165 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
10166 last_arrow_string = Voverlay_arrow_string;
10167
10168 if (frame_up_to_date_hook != 0)
10169 frame_up_to_date_hook (sf);
10170 }
10171
10172 update_mode_lines = 0;
10173 windows_or_buffers_changed = 0;
10174 cursor_type_changed = 0;
10175 }
10176
10177 /* Start SIGIO interrupts coming again. Having them off during the
10178 code above makes it less likely one will discard output, but not
10179 impossible, since there might be stuff in the system buffer here.
10180 But it is much hairier to try to do anything about that. */
10181 if (interrupt_input)
10182 request_sigio ();
10183 RESUME_POLLING;
10184
10185 /* If a frame has become visible which was not before, redisplay
10186 again, so that we display it. Expose events for such a frame
10187 (which it gets when becoming visible) don't call the parts of
10188 redisplay constructing glyphs, so simply exposing a frame won't
10189 display anything in this case. So, we have to display these
10190 frames here explicitly. */
10191 if (!pause)
10192 {
10193 Lisp_Object tail, frame;
10194 int new_count = 0;
10195
10196 FOR_EACH_FRAME (tail, frame)
10197 {
10198 int this_is_visible = 0;
10199
10200 if (XFRAME (frame)->visible)
10201 this_is_visible = 1;
10202 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10203 if (XFRAME (frame)->visible)
10204 this_is_visible = 1;
10205
10206 if (this_is_visible)
10207 new_count++;
10208 }
10209
10210 if (new_count != number_of_visible_frames)
10211 windows_or_buffers_changed++;
10212 }
10213
10214 /* Change frame size now if a change is pending. */
10215 do_pending_window_change (1);
10216
10217 /* If we just did a pending size change, or have additional
10218 visible frames, redisplay again. */
10219 if (windows_or_buffers_changed && !pause)
10220 goto retry;
10221
10222 end_of_redisplay:
10223 unbind_to (count, Qnil);
10224 RESUME_POLLING;
10225 }
10226
10227
10228 /* Redisplay, but leave alone any recent echo area message unless
10229 another message has been requested in its place.
10230
10231 This is useful in situations where you need to redisplay but no
10232 user action has occurred, making it inappropriate for the message
10233 area to be cleared. See tracking_off and
10234 wait_reading_process_input for examples of these situations.
10235
10236 FROM_WHERE is an integer saying from where this function was
10237 called. This is useful for debugging. */
10238
10239 void
10240 redisplay_preserve_echo_area (from_where)
10241 int from_where;
10242 {
10243 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10244
10245 if (!NILP (echo_area_buffer[1]))
10246 {
10247 /* We have a previously displayed message, but no current
10248 message. Redisplay the previous message. */
10249 display_last_displayed_message_p = 1;
10250 redisplay_internal (1);
10251 display_last_displayed_message_p = 0;
10252 }
10253 else
10254 redisplay_internal (1);
10255 }
10256
10257
10258 /* Function registered with record_unwind_protect in
10259 redisplay_internal. Reset redisplaying_p to the value it had
10260 before redisplay_internal was called, and clear
10261 prevent_freeing_realized_faces_p. */
10262
10263 static Lisp_Object
10264 unwind_redisplay (old_redisplaying_p)
10265 Lisp_Object old_redisplaying_p;
10266 {
10267 redisplaying_p = XFASTINT (old_redisplaying_p);
10268 return Qnil;
10269 }
10270
10271
10272 /* Mark the display of window W as accurate or inaccurate. If
10273 ACCURATE_P is non-zero mark display of W as accurate. If
10274 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10275 redisplay_internal is called. */
10276
10277 static void
10278 mark_window_display_accurate_1 (w, accurate_p)
10279 struct window *w;
10280 int accurate_p;
10281 {
10282 if (BUFFERP (w->buffer))
10283 {
10284 struct buffer *b = XBUFFER (w->buffer);
10285
10286 w->last_modified
10287 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10288 w->last_overlay_modified
10289 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10290 w->last_had_star
10291 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10292
10293 if (accurate_p)
10294 {
10295 b->clip_changed = 0;
10296 b->prevent_redisplay_optimizations_p = 0;
10297
10298 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10299 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10300 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10301 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10302
10303 w->current_matrix->buffer = b;
10304 w->current_matrix->begv = BUF_BEGV (b);
10305 w->current_matrix->zv = BUF_ZV (b);
10306
10307 w->last_cursor = w->cursor;
10308 w->last_cursor_off_p = w->cursor_off_p;
10309
10310 if (w == XWINDOW (selected_window))
10311 w->last_point = make_number (BUF_PT (b));
10312 else
10313 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10314 }
10315 }
10316
10317 if (accurate_p)
10318 {
10319 w->window_end_valid = w->buffer;
10320 #if 0 /* This is incorrect with variable-height lines. */
10321 xassert (XINT (w->window_end_vpos)
10322 < (WINDOW_TOTAL_LINES (w)
10323 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10324 #endif
10325 w->update_mode_line = Qnil;
10326 }
10327 }
10328
10329
10330 /* Mark the display of windows in the window tree rooted at WINDOW as
10331 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10332 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10333 be redisplayed the next time redisplay_internal is called. */
10334
10335 void
10336 mark_window_display_accurate (window, accurate_p)
10337 Lisp_Object window;
10338 int accurate_p;
10339 {
10340 struct window *w;
10341
10342 for (; !NILP (window); window = w->next)
10343 {
10344 w = XWINDOW (window);
10345 mark_window_display_accurate_1 (w, accurate_p);
10346
10347 if (!NILP (w->vchild))
10348 mark_window_display_accurate (w->vchild, accurate_p);
10349 if (!NILP (w->hchild))
10350 mark_window_display_accurate (w->hchild, accurate_p);
10351 }
10352
10353 if (accurate_p)
10354 {
10355 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
10356 last_arrow_string = Voverlay_arrow_string;
10357 }
10358 else
10359 {
10360 /* Force a thorough redisplay the next time by setting
10361 last_arrow_position and last_arrow_string to t, which is
10362 unequal to any useful value of Voverlay_arrow_... */
10363 last_arrow_position = Qt;
10364 last_arrow_string = Qt;
10365 }
10366 }
10367
10368
10369 /* Return value in display table DP (Lisp_Char_Table *) for character
10370 C. Since a display table doesn't have any parent, we don't have to
10371 follow parent. Do not call this function directly but use the
10372 macro DISP_CHAR_VECTOR. */
10373
10374 Lisp_Object
10375 disp_char_vector (dp, c)
10376 struct Lisp_Char_Table *dp;
10377 int c;
10378 {
10379 int code[4], i;
10380 Lisp_Object val;
10381
10382 if (SINGLE_BYTE_CHAR_P (c))
10383 return (dp->contents[c]);
10384
10385 SPLIT_CHAR (c, code[0], code[1], code[2]);
10386 if (code[1] < 32)
10387 code[1] = -1;
10388 else if (code[2] < 32)
10389 code[2] = -1;
10390
10391 /* Here, the possible range of code[0] (== charset ID) is
10392 128..max_charset. Since the top level char table contains data
10393 for multibyte characters after 256th element, we must increment
10394 code[0] by 128 to get a correct index. */
10395 code[0] += 128;
10396 code[3] = -1; /* anchor */
10397
10398 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10399 {
10400 val = dp->contents[code[i]];
10401 if (!SUB_CHAR_TABLE_P (val))
10402 return (NILP (val) ? dp->defalt : val);
10403 }
10404
10405 /* Here, val is a sub char table. We return the default value of
10406 it. */
10407 return (dp->defalt);
10408 }
10409
10410
10411 \f
10412 /***********************************************************************
10413 Window Redisplay
10414 ***********************************************************************/
10415
10416 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10417
10418 static void
10419 redisplay_windows (window)
10420 Lisp_Object window;
10421 {
10422 while (!NILP (window))
10423 {
10424 struct window *w = XWINDOW (window);
10425
10426 if (!NILP (w->hchild))
10427 redisplay_windows (w->hchild);
10428 else if (!NILP (w->vchild))
10429 redisplay_windows (w->vchild);
10430 else
10431 {
10432 displayed_buffer = XBUFFER (w->buffer);
10433 /* Use list_of_error, not Qerror, so that
10434 we catch only errors and don't run the debugger. */
10435 internal_condition_case_1 (redisplay_window_0, window,
10436 list_of_error,
10437 redisplay_window_error);
10438 }
10439
10440 window = w->next;
10441 }
10442 }
10443
10444 static Lisp_Object
10445 redisplay_window_error ()
10446 {
10447 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10448 return Qnil;
10449 }
10450
10451 static Lisp_Object
10452 redisplay_window_0 (window)
10453 Lisp_Object window;
10454 {
10455 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10456 redisplay_window (window, 0);
10457 return Qnil;
10458 }
10459
10460 static Lisp_Object
10461 redisplay_window_1 (window)
10462 Lisp_Object window;
10463 {
10464 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10465 redisplay_window (window, 1);
10466 return Qnil;
10467 }
10468 \f
10469
10470 /* Increment GLYPH until it reaches END or CONDITION fails while
10471 adding (GLYPH)->pixel_width to X. */
10472
10473 #define SKIP_GLYPHS(glyph, end, x, condition) \
10474 do \
10475 { \
10476 (x) += (glyph)->pixel_width; \
10477 ++(glyph); \
10478 } \
10479 while ((glyph) < (end) && (condition))
10480
10481
10482 /* Set cursor position of W. PT is assumed to be displayed in ROW.
10483 DELTA is the number of bytes by which positions recorded in ROW
10484 differ from current buffer positions. */
10485
10486 void
10487 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10488 struct window *w;
10489 struct glyph_row *row;
10490 struct glyph_matrix *matrix;
10491 int delta, delta_bytes, dy, dvpos;
10492 {
10493 struct glyph *glyph = row->glyphs[TEXT_AREA];
10494 struct glyph *end = glyph + row->used[TEXT_AREA];
10495 /* The first glyph that starts a sequence of glyphs from string. */
10496 struct glyph *string_start;
10497 /* The X coordinate of string_start. */
10498 int string_start_x;
10499 /* The last known character position. */
10500 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10501 /* The last known character position before string_start. */
10502 int string_before_pos;
10503 int x = row->x;
10504 int pt_old = PT - delta;
10505
10506 /* Skip over glyphs not having an object at the start of the row.
10507 These are special glyphs like truncation marks on terminal
10508 frames. */
10509 if (row->displays_text_p)
10510 while (glyph < end
10511 && INTEGERP (glyph->object)
10512 && glyph->charpos < 0)
10513 {
10514 x += glyph->pixel_width;
10515 ++glyph;
10516 }
10517
10518 string_start = NULL;
10519 while (glyph < end
10520 && !INTEGERP (glyph->object)
10521 && (!BUFFERP (glyph->object)
10522 || (last_pos = glyph->charpos) < pt_old))
10523 {
10524 if (! STRINGP (glyph->object))
10525 {
10526 string_start = NULL;
10527 x += glyph->pixel_width;
10528 ++glyph;
10529 }
10530 else
10531 {
10532 string_before_pos = last_pos;
10533 string_start = glyph;
10534 string_start_x = x;
10535 /* Skip all glyphs from string. */
10536 SKIP_GLYPHS (glyph, end, x, STRINGP (glyph->object));
10537 }
10538 }
10539
10540 if (string_start
10541 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
10542 {
10543 /* We may have skipped over point because the previous glyphs
10544 are from string. As there's no easy way to know the
10545 character position of the current glyph, find the correct
10546 glyph on point by scanning from string_start again. */
10547 Lisp_Object limit;
10548 Lisp_Object string;
10549 int pos;
10550
10551 limit = make_number (pt_old + 1);
10552 end = glyph;
10553 glyph = string_start;
10554 x = string_start_x;
10555 string = glyph->object;
10556 pos = string_buffer_position (w, string, string_before_pos);
10557 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
10558 because we always put cursor after overlay strings. */
10559 while (pos == 0 && glyph < end)
10560 {
10561 string = glyph->object;
10562 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10563 if (glyph < end)
10564 pos = string_buffer_position (w, glyph->object, string_before_pos);
10565 }
10566
10567 while (glyph < end)
10568 {
10569 pos = XINT (Fnext_single_char_property_change
10570 (make_number (pos), Qdisplay, Qnil, limit));
10571 if (pos > pt_old)
10572 break;
10573 /* Skip glyphs from the same string. */
10574 string = glyph->object;
10575 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10576 /* Skip glyphs from an overlay. */
10577 while (glyph < end
10578 && ! string_buffer_position (w, glyph->object, pos))
10579 {
10580 string = glyph->object;
10581 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10582 }
10583 }
10584 }
10585
10586 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
10587 w->cursor.x = x;
10588 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
10589 w->cursor.y = row->y + dy;
10590
10591 if (w == XWINDOW (selected_window))
10592 {
10593 if (!row->continued_p
10594 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
10595 && row->x == 0)
10596 {
10597 this_line_buffer = XBUFFER (w->buffer);
10598
10599 CHARPOS (this_line_start_pos)
10600 = MATRIX_ROW_START_CHARPOS (row) + delta;
10601 BYTEPOS (this_line_start_pos)
10602 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
10603
10604 CHARPOS (this_line_end_pos)
10605 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
10606 BYTEPOS (this_line_end_pos)
10607 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
10608
10609 this_line_y = w->cursor.y;
10610 this_line_pixel_height = row->height;
10611 this_line_vpos = w->cursor.vpos;
10612 this_line_start_x = row->x;
10613 }
10614 else
10615 CHARPOS (this_line_start_pos) = 0;
10616 }
10617 }
10618
10619
10620 /* Run window scroll functions, if any, for WINDOW with new window
10621 start STARTP. Sets the window start of WINDOW to that position.
10622
10623 We assume that the window's buffer is really current. */
10624
10625 static INLINE struct text_pos
10626 run_window_scroll_functions (window, startp)
10627 Lisp_Object window;
10628 struct text_pos startp;
10629 {
10630 struct window *w = XWINDOW (window);
10631 SET_MARKER_FROM_TEXT_POS (w->start, startp);
10632
10633 if (current_buffer != XBUFFER (w->buffer))
10634 abort ();
10635
10636 if (!NILP (Vwindow_scroll_functions))
10637 {
10638 run_hook_with_args_2 (Qwindow_scroll_functions, window,
10639 make_number (CHARPOS (startp)));
10640 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10641 /* In case the hook functions switch buffers. */
10642 if (current_buffer != XBUFFER (w->buffer))
10643 set_buffer_internal_1 (XBUFFER (w->buffer));
10644 }
10645
10646 return startp;
10647 }
10648
10649
10650 /* Make sure the line containing the cursor is fully visible.
10651 A value of 1 means there is nothing to be done.
10652 (Either the line is fully visible, or it cannot be made so,
10653 or we cannot tell.)
10654 A value of 0 means the caller should do scrolling
10655 as if point had gone off the screen. */
10656
10657 static int
10658 make_cursor_line_fully_visible (w)
10659 struct window *w;
10660 {
10661 struct glyph_matrix *matrix;
10662 struct glyph_row *row;
10663 int window_height;
10664
10665 /* It's not always possible to find the cursor, e.g, when a window
10666 is full of overlay strings. Don't do anything in that case. */
10667 if (w->cursor.vpos < 0)
10668 return 1;
10669
10670 matrix = w->desired_matrix;
10671 row = MATRIX_ROW (matrix, w->cursor.vpos);
10672
10673 /* If the cursor row is not partially visible, there's nothing to do. */
10674 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
10675 return 1;
10676
10677 /* If the row the cursor is in is taller than the window's height,
10678 it's not clear what to do, so do nothing. */
10679 window_height = window_box_height (w);
10680 if (row->height >= window_height)
10681 return 1;
10682
10683 return 0;
10684
10685 #if 0
10686 /* This code used to try to scroll the window just enough to make
10687 the line visible. It returned 0 to say that the caller should
10688 allocate larger glyph matrices. */
10689
10690 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
10691 {
10692 int dy = row->height - row->visible_height;
10693 w->vscroll = 0;
10694 w->cursor.y += dy;
10695 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10696 }
10697 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
10698 {
10699 int dy = - (row->height - row->visible_height);
10700 w->vscroll = dy;
10701 w->cursor.y += dy;
10702 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10703 }
10704
10705 /* When we change the cursor y-position of the selected window,
10706 change this_line_y as well so that the display optimization for
10707 the cursor line of the selected window in redisplay_internal uses
10708 the correct y-position. */
10709 if (w == XWINDOW (selected_window))
10710 this_line_y = w->cursor.y;
10711
10712 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
10713 redisplay with larger matrices. */
10714 if (matrix->nrows < required_matrix_height (w))
10715 {
10716 fonts_changed_p = 1;
10717 return 0;
10718 }
10719
10720 return 1;
10721 #endif /* 0 */
10722 }
10723
10724
10725 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
10726 non-zero means only WINDOW is redisplayed in redisplay_internal.
10727 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
10728 in redisplay_window to bring a partially visible line into view in
10729 the case that only the cursor has moved.
10730
10731 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
10732 last screen line's vertical height extends past the end of the screen.
10733
10734 Value is
10735
10736 1 if scrolling succeeded
10737
10738 0 if scrolling didn't find point.
10739
10740 -1 if new fonts have been loaded so that we must interrupt
10741 redisplay, adjust glyph matrices, and try again. */
10742
10743 enum
10744 {
10745 SCROLLING_SUCCESS,
10746 SCROLLING_FAILED,
10747 SCROLLING_NEED_LARGER_MATRICES
10748 };
10749
10750 static int
10751 try_scrolling (window, just_this_one_p, scroll_conservatively,
10752 scroll_step, temp_scroll_step, last_line_misfit)
10753 Lisp_Object window;
10754 int just_this_one_p;
10755 EMACS_INT scroll_conservatively, scroll_step;
10756 int temp_scroll_step;
10757 int last_line_misfit;
10758 {
10759 struct window *w = XWINDOW (window);
10760 struct frame *f = XFRAME (w->frame);
10761 struct text_pos scroll_margin_pos;
10762 struct text_pos pos;
10763 struct text_pos startp;
10764 struct it it;
10765 Lisp_Object window_end;
10766 int this_scroll_margin;
10767 int dy = 0;
10768 int scroll_max;
10769 int rc;
10770 int amount_to_scroll = 0;
10771 Lisp_Object aggressive;
10772 int height;
10773 int end_scroll_margin;
10774
10775 #if GLYPH_DEBUG
10776 debug_method_add (w, "try_scrolling");
10777 #endif
10778
10779 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10780
10781 /* Compute scroll margin height in pixels. We scroll when point is
10782 within this distance from the top or bottom of the window. */
10783 if (scroll_margin > 0)
10784 {
10785 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
10786 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
10787 }
10788 else
10789 this_scroll_margin = 0;
10790
10791 /* Compute how much we should try to scroll maximally to bring point
10792 into view. */
10793 if (scroll_step || scroll_conservatively || temp_scroll_step)
10794 scroll_max = max (scroll_step,
10795 max (scroll_conservatively, temp_scroll_step));
10796 else if (NUMBERP (current_buffer->scroll_down_aggressively)
10797 || NUMBERP (current_buffer->scroll_up_aggressively))
10798 /* We're trying to scroll because of aggressive scrolling
10799 but no scroll_step is set. Choose an arbitrary one. Maybe
10800 there should be a variable for this. */
10801 scroll_max = 10;
10802 else
10803 scroll_max = 0;
10804 scroll_max *= FRAME_LINE_HEIGHT (f);
10805
10806 /* Decide whether we have to scroll down. Start at the window end
10807 and move this_scroll_margin up to find the position of the scroll
10808 margin. */
10809 window_end = Fwindow_end (window, Qt);
10810
10811 too_near_end:
10812
10813 CHARPOS (scroll_margin_pos) = XINT (window_end);
10814 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
10815
10816 end_scroll_margin = this_scroll_margin + !!last_line_misfit;
10817 if (end_scroll_margin)
10818 {
10819 start_display (&it, w, scroll_margin_pos);
10820 move_it_vertically (&it, - end_scroll_margin);
10821 scroll_margin_pos = it.current.pos;
10822 }
10823
10824 if (PT >= CHARPOS (scroll_margin_pos))
10825 {
10826 int y0;
10827
10828 /* Point is in the scroll margin at the bottom of the window, or
10829 below. Compute a new window start that makes point visible. */
10830
10831 /* Compute the distance from the scroll margin to PT.
10832 Give up if the distance is greater than scroll_max. */
10833 start_display (&it, w, scroll_margin_pos);
10834 y0 = it.current_y;
10835 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10836 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10837
10838 /* To make point visible, we have to move the window start
10839 down so that the line the cursor is in is visible, which
10840 means we have to add in the height of the cursor line. */
10841 dy = line_bottom_y (&it) - y0;
10842
10843 if (dy > scroll_max)
10844 return SCROLLING_FAILED;
10845
10846 /* Move the window start down. If scrolling conservatively,
10847 move it just enough down to make point visible. If
10848 scroll_step is set, move it down by scroll_step. */
10849 start_display (&it, w, startp);
10850
10851 if (scroll_conservatively)
10852 /* Set AMOUNT_TO_SCROLL to at least one line,
10853 and at most scroll_conservatively lines. */
10854 amount_to_scroll
10855 = min (max (dy, FRAME_LINE_HEIGHT (f)),
10856 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
10857 else if (scroll_step || temp_scroll_step)
10858 amount_to_scroll = scroll_max;
10859 else
10860 {
10861 aggressive = current_buffer->scroll_up_aggressively;
10862 height = WINDOW_BOX_TEXT_HEIGHT (w);
10863 if (NUMBERP (aggressive))
10864 amount_to_scroll = XFLOATINT (aggressive) * height;
10865 }
10866
10867 if (amount_to_scroll <= 0)
10868 return SCROLLING_FAILED;
10869
10870 /* If moving by amount_to_scroll leaves STARTP unchanged,
10871 move it down one screen line. */
10872
10873 move_it_vertically (&it, amount_to_scroll);
10874 if (CHARPOS (it.current.pos) == CHARPOS (startp))
10875 move_it_by_lines (&it, 1, 1);
10876 startp = it.current.pos;
10877 }
10878 else
10879 {
10880 /* See if point is inside the scroll margin at the top of the
10881 window. */
10882 scroll_margin_pos = startp;
10883 if (this_scroll_margin)
10884 {
10885 start_display (&it, w, startp);
10886 move_it_vertically (&it, this_scroll_margin);
10887 scroll_margin_pos = it.current.pos;
10888 }
10889
10890 if (PT < CHARPOS (scroll_margin_pos))
10891 {
10892 /* Point is in the scroll margin at the top of the window or
10893 above what is displayed in the window. */
10894 int y0;
10895
10896 /* Compute the vertical distance from PT to the scroll
10897 margin position. Give up if distance is greater than
10898 scroll_max. */
10899 SET_TEXT_POS (pos, PT, PT_BYTE);
10900 start_display (&it, w, pos);
10901 y0 = it.current_y;
10902 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
10903 it.last_visible_y, -1,
10904 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10905 dy = it.current_y - y0;
10906 if (dy > scroll_max)
10907 return SCROLLING_FAILED;
10908
10909 /* Compute new window start. */
10910 start_display (&it, w, startp);
10911
10912 if (scroll_conservatively)
10913 amount_to_scroll =
10914 max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
10915 else if (scroll_step || temp_scroll_step)
10916 amount_to_scroll = scroll_max;
10917 else
10918 {
10919 aggressive = current_buffer->scroll_down_aggressively;
10920 height = WINDOW_BOX_TEXT_HEIGHT (w);
10921 if (NUMBERP (aggressive))
10922 amount_to_scroll = XFLOATINT (aggressive) * height;
10923 }
10924
10925 if (amount_to_scroll <= 0)
10926 return SCROLLING_FAILED;
10927
10928 move_it_vertically (&it, - amount_to_scroll);
10929 startp = it.current.pos;
10930 }
10931 }
10932
10933 /* Run window scroll functions. */
10934 startp = run_window_scroll_functions (window, startp);
10935
10936 /* Display the window. Give up if new fonts are loaded, or if point
10937 doesn't appear. */
10938 if (!try_window (window, startp))
10939 rc = SCROLLING_NEED_LARGER_MATRICES;
10940 else if (w->cursor.vpos < 0)
10941 {
10942 clear_glyph_matrix (w->desired_matrix);
10943 rc = SCROLLING_FAILED;
10944 }
10945 else
10946 {
10947 /* Maybe forget recorded base line for line number display. */
10948 if (!just_this_one_p
10949 || current_buffer->clip_changed
10950 || BEG_UNCHANGED < CHARPOS (startp))
10951 w->base_line_number = Qnil;
10952
10953 /* If cursor ends up on a partially visible line,
10954 treat that as being off the bottom of the screen. */
10955 if (! make_cursor_line_fully_visible (w))
10956 {
10957 clear_glyph_matrix (w->desired_matrix);
10958 last_line_misfit = 1;
10959 goto too_near_end;
10960 }
10961 rc = SCROLLING_SUCCESS;
10962 }
10963
10964 return rc;
10965 }
10966
10967
10968 /* Compute a suitable window start for window W if display of W starts
10969 on a continuation line. Value is non-zero if a new window start
10970 was computed.
10971
10972 The new window start will be computed, based on W's width, starting
10973 from the start of the continued line. It is the start of the
10974 screen line with the minimum distance from the old start W->start. */
10975
10976 static int
10977 compute_window_start_on_continuation_line (w)
10978 struct window *w;
10979 {
10980 struct text_pos pos, start_pos;
10981 int window_start_changed_p = 0;
10982
10983 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
10984
10985 /* If window start is on a continuation line... Window start may be
10986 < BEGV in case there's invisible text at the start of the
10987 buffer (M-x rmail, for example). */
10988 if (CHARPOS (start_pos) > BEGV
10989 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
10990 {
10991 struct it it;
10992 struct glyph_row *row;
10993
10994 /* Handle the case that the window start is out of range. */
10995 if (CHARPOS (start_pos) < BEGV)
10996 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
10997 else if (CHARPOS (start_pos) > ZV)
10998 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
10999
11000 /* Find the start of the continued line. This should be fast
11001 because scan_buffer is fast (newline cache). */
11002 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11003 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11004 row, DEFAULT_FACE_ID);
11005 reseat_at_previous_visible_line_start (&it);
11006
11007 /* If the line start is "too far" away from the window start,
11008 say it takes too much time to compute a new window start. */
11009 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11010 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11011 {
11012 int min_distance, distance;
11013
11014 /* Move forward by display lines to find the new window
11015 start. If window width was enlarged, the new start can
11016 be expected to be > the old start. If window width was
11017 decreased, the new window start will be < the old start.
11018 So, we're looking for the display line start with the
11019 minimum distance from the old window start. */
11020 pos = it.current.pos;
11021 min_distance = INFINITY;
11022 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11023 distance < min_distance)
11024 {
11025 min_distance = distance;
11026 pos = it.current.pos;
11027 move_it_by_lines (&it, 1, 0);
11028 }
11029
11030 /* Set the window start there. */
11031 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11032 window_start_changed_p = 1;
11033 }
11034 }
11035
11036 return window_start_changed_p;
11037 }
11038
11039
11040 /* Try cursor movement in case text has not changed in window WINDOW,
11041 with window start STARTP. Value is
11042
11043 CURSOR_MOVEMENT_SUCCESS if successful
11044
11045 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11046
11047 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11048 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11049 we want to scroll as if scroll-step were set to 1. See the code.
11050
11051 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11052 which case we have to abort this redisplay, and adjust matrices
11053 first. */
11054
11055 enum
11056 {
11057 CURSOR_MOVEMENT_SUCCESS,
11058 CURSOR_MOVEMENT_CANNOT_BE_USED,
11059 CURSOR_MOVEMENT_MUST_SCROLL,
11060 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11061 };
11062
11063 static int
11064 try_cursor_movement (window, startp, scroll_step)
11065 Lisp_Object window;
11066 struct text_pos startp;
11067 int *scroll_step;
11068 {
11069 struct window *w = XWINDOW (window);
11070 struct frame *f = XFRAME (w->frame);
11071 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11072
11073 #if GLYPH_DEBUG
11074 if (inhibit_try_cursor_movement)
11075 return rc;
11076 #endif
11077
11078 /* Handle case where text has not changed, only point, and it has
11079 not moved off the frame. */
11080 if (/* Point may be in this window. */
11081 PT >= CHARPOS (startp)
11082 /* Selective display hasn't changed. */
11083 && !current_buffer->clip_changed
11084 /* Function force-mode-line-update is used to force a thorough
11085 redisplay. It sets either windows_or_buffers_changed or
11086 update_mode_lines. So don't take a shortcut here for these
11087 cases. */
11088 && !update_mode_lines
11089 && !windows_or_buffers_changed
11090 && !cursor_type_changed
11091 /* Can't use this case if highlighting a region. When a
11092 region exists, cursor movement has to do more than just
11093 set the cursor. */
11094 && !(!NILP (Vtransient_mark_mode)
11095 && !NILP (current_buffer->mark_active))
11096 && NILP (w->region_showing)
11097 && NILP (Vshow_trailing_whitespace)
11098 /* Right after splitting windows, last_point may be nil. */
11099 && INTEGERP (w->last_point)
11100 /* This code is not used for mini-buffer for the sake of the case
11101 of redisplaying to replace an echo area message; since in
11102 that case the mini-buffer contents per se are usually
11103 unchanged. This code is of no real use in the mini-buffer
11104 since the handling of this_line_start_pos, etc., in redisplay
11105 handles the same cases. */
11106 && !EQ (window, minibuf_window)
11107 /* When splitting windows or for new windows, it happens that
11108 redisplay is called with a nil window_end_vpos or one being
11109 larger than the window. This should really be fixed in
11110 window.c. I don't have this on my list, now, so we do
11111 approximately the same as the old redisplay code. --gerd. */
11112 && INTEGERP (w->window_end_vpos)
11113 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11114 && (FRAME_WINDOW_P (f)
11115 || !MARKERP (Voverlay_arrow_position)
11116 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
11117 {
11118 int this_scroll_margin;
11119 struct glyph_row *row = NULL;
11120
11121 #if GLYPH_DEBUG
11122 debug_method_add (w, "cursor movement");
11123 #endif
11124
11125 /* Scroll if point within this distance from the top or bottom
11126 of the window. This is a pixel value. */
11127 this_scroll_margin = max (0, scroll_margin);
11128 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11129 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11130
11131 /* Start with the row the cursor was displayed during the last
11132 not paused redisplay. Give up if that row is not valid. */
11133 if (w->last_cursor.vpos < 0
11134 || w->last_cursor.vpos >= w->current_matrix->nrows)
11135 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11136 else
11137 {
11138 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11139 if (row->mode_line_p)
11140 ++row;
11141 if (!row->enabled_p)
11142 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11143 }
11144
11145 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11146 {
11147 int scroll_p = 0;
11148 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11149
11150 if (PT > XFASTINT (w->last_point))
11151 {
11152 /* Point has moved forward. */
11153 while (MATRIX_ROW_END_CHARPOS (row) < PT
11154 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11155 {
11156 xassert (row->enabled_p);
11157 ++row;
11158 }
11159
11160 /* The end position of a row equals the start position
11161 of the next row. If PT is there, we would rather
11162 display it in the next line. */
11163 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11164 && MATRIX_ROW_END_CHARPOS (row) == PT
11165 && !cursor_row_p (w, row))
11166 ++row;
11167
11168 /* If within the scroll margin, scroll. Note that
11169 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11170 the next line would be drawn, and that
11171 this_scroll_margin can be zero. */
11172 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11173 || PT > MATRIX_ROW_END_CHARPOS (row)
11174 /* Line is completely visible last line in window
11175 and PT is to be set in the next line. */
11176 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11177 && PT == MATRIX_ROW_END_CHARPOS (row)
11178 && !row->ends_at_zv_p
11179 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11180 scroll_p = 1;
11181 }
11182 else if (PT < XFASTINT (w->last_point))
11183 {
11184 /* Cursor has to be moved backward. Note that PT >=
11185 CHARPOS (startp) because of the outer
11186 if-statement. */
11187 while (!row->mode_line_p
11188 && (MATRIX_ROW_START_CHARPOS (row) > PT
11189 || (MATRIX_ROW_START_CHARPOS (row) == PT
11190 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11191 && (row->y > this_scroll_margin
11192 || CHARPOS (startp) == BEGV))
11193 {
11194 xassert (row->enabled_p);
11195 --row;
11196 }
11197
11198 /* Consider the following case: Window starts at BEGV,
11199 there is invisible, intangible text at BEGV, so that
11200 display starts at some point START > BEGV. It can
11201 happen that we are called with PT somewhere between
11202 BEGV and START. Try to handle that case. */
11203 if (row < w->current_matrix->rows
11204 || row->mode_line_p)
11205 {
11206 row = w->current_matrix->rows;
11207 if (row->mode_line_p)
11208 ++row;
11209 }
11210
11211 /* Due to newlines in overlay strings, we may have to
11212 skip forward over overlay strings. */
11213 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11214 && MATRIX_ROW_END_CHARPOS (row) == PT
11215 && !cursor_row_p (w, row))
11216 ++row;
11217
11218 /* If within the scroll margin, scroll. */
11219 if (row->y < this_scroll_margin
11220 && CHARPOS (startp) != BEGV)
11221 scroll_p = 1;
11222 }
11223
11224 if (PT < MATRIX_ROW_START_CHARPOS (row)
11225 || PT > MATRIX_ROW_END_CHARPOS (row))
11226 {
11227 /* if PT is not in the glyph row, give up. */
11228 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11229 }
11230 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
11231 {
11232 if (PT == MATRIX_ROW_END_CHARPOS (row)
11233 && !row->ends_at_zv_p
11234 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11235 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11236 else if (row->height > window_box_height (w))
11237 {
11238 /* If we end up in a partially visible line, let's
11239 make it fully visible, except when it's taller
11240 than the window, in which case we can't do much
11241 about it. */
11242 *scroll_step = 1;
11243 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11244 }
11245 else
11246 {
11247 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11248 if (!make_cursor_line_fully_visible (w))
11249 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11250 else
11251 rc = CURSOR_MOVEMENT_SUCCESS;
11252 }
11253 }
11254 else if (scroll_p)
11255 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11256 else
11257 {
11258 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11259 rc = CURSOR_MOVEMENT_SUCCESS;
11260 }
11261 }
11262 }
11263
11264 return rc;
11265 }
11266
11267 void
11268 set_vertical_scroll_bar (w)
11269 struct window *w;
11270 {
11271 int start, end, whole;
11272
11273 /* Calculate the start and end positions for the current window.
11274 At some point, it would be nice to choose between scrollbars
11275 which reflect the whole buffer size, with special markers
11276 indicating narrowing, and scrollbars which reflect only the
11277 visible region.
11278
11279 Note that mini-buffers sometimes aren't displaying any text. */
11280 if (!MINI_WINDOW_P (w)
11281 || (w == XWINDOW (minibuf_window)
11282 && NILP (echo_area_buffer[0])))
11283 {
11284 struct buffer *buf = XBUFFER (w->buffer);
11285 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11286 start = marker_position (w->start) - BUF_BEGV (buf);
11287 /* I don't think this is guaranteed to be right. For the
11288 moment, we'll pretend it is. */
11289 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11290
11291 if (end < start)
11292 end = start;
11293 if (whole < (end - start))
11294 whole = end - start;
11295 }
11296 else
11297 start = end = whole = 0;
11298
11299 /* Indicate what this scroll bar ought to be displaying now. */
11300 set_vertical_scroll_bar_hook (w, end - start, whole, start);
11301 }
11302
11303 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11304 selected_window is redisplayed.
11305
11306 We can return without actually redisplaying the window if
11307 fonts_changed_p is nonzero. In that case, redisplay_internal will
11308 retry. */
11309
11310 static void
11311 redisplay_window (window, just_this_one_p)
11312 Lisp_Object window;
11313 int just_this_one_p;
11314 {
11315 struct window *w = XWINDOW (window);
11316 struct frame *f = XFRAME (w->frame);
11317 struct buffer *buffer = XBUFFER (w->buffer);
11318 struct buffer *old = current_buffer;
11319 struct text_pos lpoint, opoint, startp;
11320 int update_mode_line;
11321 int tem;
11322 struct it it;
11323 /* Record it now because it's overwritten. */
11324 int current_matrix_up_to_date_p = 0;
11325 /* This is less strict than current_matrix_up_to_date_p.
11326 It indictes that the buffer contents and narrowing are unchanged. */
11327 int buffer_unchanged_p = 0;
11328 int temp_scroll_step = 0;
11329 int count = SPECPDL_INDEX ();
11330 int rc;
11331 int centering_position;
11332 int last_line_misfit = 0;
11333
11334 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11335 opoint = lpoint;
11336
11337 /* W must be a leaf window here. */
11338 xassert (!NILP (w->buffer));
11339 #if GLYPH_DEBUG
11340 *w->desired_matrix->method = 0;
11341 #endif
11342
11343 specbind (Qinhibit_point_motion_hooks, Qt);
11344
11345 reconsider_clip_changes (w, buffer);
11346
11347 /* Has the mode line to be updated? */
11348 update_mode_line = (!NILP (w->update_mode_line)
11349 || update_mode_lines
11350 || buffer->clip_changed
11351 || buffer->prevent_redisplay_optimizations_p);
11352
11353 if (MINI_WINDOW_P (w))
11354 {
11355 if (w == XWINDOW (echo_area_window)
11356 && !NILP (echo_area_buffer[0]))
11357 {
11358 if (update_mode_line)
11359 /* We may have to update a tty frame's menu bar or a
11360 tool-bar. Example `M-x C-h C-h C-g'. */
11361 goto finish_menu_bars;
11362 else
11363 /* We've already displayed the echo area glyphs in this window. */
11364 goto finish_scroll_bars;
11365 }
11366 else if ((w != XWINDOW (minibuf_window)
11367 || minibuf_level == 0)
11368 /* When buffer is nonempty, redisplay window normally. */
11369 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
11370 /* Quail displays non-mini buffers in minibuffer window.
11371 In that case, redisplay the window normally. */
11372 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
11373 {
11374 /* W is a mini-buffer window, but it's not active, so clear
11375 it. */
11376 int yb = window_text_bottom_y (w);
11377 struct glyph_row *row;
11378 int y;
11379
11380 for (y = 0, row = w->desired_matrix->rows;
11381 y < yb;
11382 y += row->height, ++row)
11383 blank_row (w, row, y);
11384 goto finish_scroll_bars;
11385 }
11386
11387 clear_glyph_matrix (w->desired_matrix);
11388 }
11389
11390 /* Otherwise set up data on this window; select its buffer and point
11391 value. */
11392 /* Really select the buffer, for the sake of buffer-local
11393 variables. */
11394 set_buffer_internal_1 (XBUFFER (w->buffer));
11395 SET_TEXT_POS (opoint, PT, PT_BYTE);
11396
11397 current_matrix_up_to_date_p
11398 = (!NILP (w->window_end_valid)
11399 && !current_buffer->clip_changed
11400 && !current_buffer->prevent_redisplay_optimizations_p
11401 && XFASTINT (w->last_modified) >= MODIFF
11402 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11403
11404 buffer_unchanged_p
11405 = (!NILP (w->window_end_valid)
11406 && !current_buffer->clip_changed
11407 && XFASTINT (w->last_modified) >= MODIFF
11408 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11409
11410 /* When windows_or_buffers_changed is non-zero, we can't rely on
11411 the window end being valid, so set it to nil there. */
11412 if (windows_or_buffers_changed)
11413 {
11414 /* If window starts on a continuation line, maybe adjust the
11415 window start in case the window's width changed. */
11416 if (XMARKER (w->start)->buffer == current_buffer)
11417 compute_window_start_on_continuation_line (w);
11418
11419 w->window_end_valid = Qnil;
11420 }
11421
11422 /* Some sanity checks. */
11423 CHECK_WINDOW_END (w);
11424 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
11425 abort ();
11426 if (BYTEPOS (opoint) < CHARPOS (opoint))
11427 abort ();
11428
11429 /* If %c is in mode line, update it if needed. */
11430 if (!NILP (w->column_number_displayed)
11431 /* This alternative quickly identifies a common case
11432 where no change is needed. */
11433 && !(PT == XFASTINT (w->last_point)
11434 && XFASTINT (w->last_modified) >= MODIFF
11435 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11436 && (XFASTINT (w->column_number_displayed)
11437 != (int) current_column ())) /* iftc */
11438 update_mode_line = 1;
11439
11440 /* Count number of windows showing the selected buffer. An indirect
11441 buffer counts as its base buffer. */
11442 if (!just_this_one_p)
11443 {
11444 struct buffer *current_base, *window_base;
11445 current_base = current_buffer;
11446 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11447 if (current_base->base_buffer)
11448 current_base = current_base->base_buffer;
11449 if (window_base->base_buffer)
11450 window_base = window_base->base_buffer;
11451 if (current_base == window_base)
11452 buffer_shared++;
11453 }
11454
11455 /* Point refers normally to the selected window. For any other
11456 window, set up appropriate value. */
11457 if (!EQ (window, selected_window))
11458 {
11459 int new_pt = XMARKER (w->pointm)->charpos;
11460 int new_pt_byte = marker_byte_position (w->pointm);
11461 if (new_pt < BEGV)
11462 {
11463 new_pt = BEGV;
11464 new_pt_byte = BEGV_BYTE;
11465 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
11466 }
11467 else if (new_pt > (ZV - 1))
11468 {
11469 new_pt = ZV;
11470 new_pt_byte = ZV_BYTE;
11471 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
11472 }
11473
11474 /* We don't use SET_PT so that the point-motion hooks don't run. */
11475 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
11476 }
11477
11478 /* If any of the character widths specified in the display table
11479 have changed, invalidate the width run cache. It's true that
11480 this may be a bit late to catch such changes, but the rest of
11481 redisplay goes (non-fatally) haywire when the display table is
11482 changed, so why should we worry about doing any better? */
11483 if (current_buffer->width_run_cache)
11484 {
11485 struct Lisp_Char_Table *disptab = buffer_display_table ();
11486
11487 if (! disptab_matches_widthtab (disptab,
11488 XVECTOR (current_buffer->width_table)))
11489 {
11490 invalidate_region_cache (current_buffer,
11491 current_buffer->width_run_cache,
11492 BEG, Z);
11493 recompute_width_table (current_buffer, disptab);
11494 }
11495 }
11496
11497 /* If window-start is screwed up, choose a new one. */
11498 if (XMARKER (w->start)->buffer != current_buffer)
11499 goto recenter;
11500
11501 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11502
11503 /* If someone specified a new starting point but did not insist,
11504 check whether it can be used. */
11505 if (!NILP (w->optional_new_start)
11506 && CHARPOS (startp) >= BEGV
11507 && CHARPOS (startp) <= ZV)
11508 {
11509 w->optional_new_start = Qnil;
11510 start_display (&it, w, startp);
11511 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11512 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11513 if (IT_CHARPOS (it) == PT)
11514 w->force_start = Qt;
11515 }
11516
11517 /* Handle case where place to start displaying has been specified,
11518 unless the specified location is outside the accessible range. */
11519 if (!NILP (w->force_start)
11520 || w->frozen_window_start_p)
11521 {
11522 /* We set this later on if we have to adjust point. */
11523 int new_vpos = -1;
11524
11525 w->force_start = Qnil;
11526 w->vscroll = 0;
11527 w->window_end_valid = Qnil;
11528
11529 /* Forget any recorded base line for line number display. */
11530 if (!buffer_unchanged_p)
11531 w->base_line_number = Qnil;
11532
11533 /* Redisplay the mode line. Select the buffer properly for that.
11534 Also, run the hook window-scroll-functions
11535 because we have scrolled. */
11536 /* Note, we do this after clearing force_start because
11537 if there's an error, it is better to forget about force_start
11538 than to get into an infinite loop calling the hook functions
11539 and having them get more errors. */
11540 if (!update_mode_line
11541 || ! NILP (Vwindow_scroll_functions))
11542 {
11543 update_mode_line = 1;
11544 w->update_mode_line = Qt;
11545 startp = run_window_scroll_functions (window, startp);
11546 }
11547
11548 w->last_modified = make_number (0);
11549 w->last_overlay_modified = make_number (0);
11550 if (CHARPOS (startp) < BEGV)
11551 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
11552 else if (CHARPOS (startp) > ZV)
11553 SET_TEXT_POS (startp, ZV, ZV_BYTE);
11554
11555 /* Redisplay, then check if cursor has been set during the
11556 redisplay. Give up if new fonts were loaded. */
11557 if (!try_window (window, startp))
11558 {
11559 w->force_start = Qt;
11560 clear_glyph_matrix (w->desired_matrix);
11561 goto need_larger_matrices;
11562 }
11563
11564 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
11565 {
11566 /* If point does not appear, try to move point so it does
11567 appear. The desired matrix has been built above, so we
11568 can use it here. */
11569 new_vpos = window_box_height (w) / 2;
11570 }
11571
11572 if (!make_cursor_line_fully_visible (w))
11573 {
11574 /* Point does appear, but on a line partly visible at end of window.
11575 Move it back to a fully-visible line. */
11576 new_vpos = window_box_height (w);
11577 }
11578
11579 /* If we need to move point for either of the above reasons,
11580 now actually do it. */
11581 if (new_vpos >= 0)
11582 {
11583 struct glyph_row *row;
11584
11585 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
11586 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
11587 ++row;
11588
11589 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
11590 MATRIX_ROW_START_BYTEPOS (row));
11591
11592 if (w != XWINDOW (selected_window))
11593 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
11594 else if (current_buffer == old)
11595 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11596
11597 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
11598
11599 /* If we are highlighting the region, then we just changed
11600 the region, so redisplay to show it. */
11601 if (!NILP (Vtransient_mark_mode)
11602 && !NILP (current_buffer->mark_active))
11603 {
11604 clear_glyph_matrix (w->desired_matrix);
11605 if (!try_window (window, startp))
11606 goto need_larger_matrices;
11607 }
11608 }
11609
11610 #if GLYPH_DEBUG
11611 debug_method_add (w, "forced window start");
11612 #endif
11613 goto done;
11614 }
11615
11616 /* Handle case where text has not changed, only point, and it has
11617 not moved off the frame, and we are not retrying after hscroll.
11618 (current_matrix_up_to_date_p is nonzero when retrying.) */
11619 if (current_matrix_up_to_date_p
11620 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
11621 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
11622 {
11623 switch (rc)
11624 {
11625 case CURSOR_MOVEMENT_SUCCESS:
11626 goto done;
11627
11628 #if 0 /* try_cursor_movement never returns this value. */
11629 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
11630 goto need_larger_matrices;
11631 #endif
11632
11633 case CURSOR_MOVEMENT_MUST_SCROLL:
11634 goto try_to_scroll;
11635
11636 default:
11637 abort ();
11638 }
11639 }
11640 /* If current starting point was originally the beginning of a line
11641 but no longer is, find a new starting point. */
11642 else if (!NILP (w->start_at_line_beg)
11643 && !(CHARPOS (startp) <= BEGV
11644 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
11645 {
11646 #if GLYPH_DEBUG
11647 debug_method_add (w, "recenter 1");
11648 #endif
11649 goto recenter;
11650 }
11651
11652 /* Try scrolling with try_window_id. Value is > 0 if update has
11653 been done, it is -1 if we know that the same window start will
11654 not work. It is 0 if unsuccessful for some other reason. */
11655 else if ((tem = try_window_id (w)) != 0)
11656 {
11657 #if GLYPH_DEBUG
11658 debug_method_add (w, "try_window_id %d", tem);
11659 #endif
11660
11661 if (fonts_changed_p)
11662 goto need_larger_matrices;
11663 if (tem > 0)
11664 goto done;
11665
11666 /* Otherwise try_window_id has returned -1 which means that we
11667 don't want the alternative below this comment to execute. */
11668 }
11669 else if (CHARPOS (startp) >= BEGV
11670 && CHARPOS (startp) <= ZV
11671 && PT >= CHARPOS (startp)
11672 && (CHARPOS (startp) < ZV
11673 /* Avoid starting at end of buffer. */
11674 || CHARPOS (startp) == BEGV
11675 || (XFASTINT (w->last_modified) >= MODIFF
11676 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
11677 {
11678 #if GLYPH_DEBUG
11679 debug_method_add (w, "same window start");
11680 #endif
11681
11682 /* Try to redisplay starting at same place as before.
11683 If point has not moved off frame, accept the results. */
11684 if (!current_matrix_up_to_date_p
11685 /* Don't use try_window_reusing_current_matrix in this case
11686 because a window scroll function can have changed the
11687 buffer. */
11688 || !NILP (Vwindow_scroll_functions)
11689 || MINI_WINDOW_P (w)
11690 || !try_window_reusing_current_matrix (w))
11691 {
11692 IF_DEBUG (debug_method_add (w, "1"));
11693 try_window (window, startp);
11694 }
11695
11696 if (fonts_changed_p)
11697 goto need_larger_matrices;
11698
11699 if (w->cursor.vpos >= 0)
11700 {
11701 if (!just_this_one_p
11702 || current_buffer->clip_changed
11703 || BEG_UNCHANGED < CHARPOS (startp))
11704 /* Forget any recorded base line for line number display. */
11705 w->base_line_number = Qnil;
11706
11707 if (!make_cursor_line_fully_visible (w))
11708 {
11709 clear_glyph_matrix (w->desired_matrix);
11710 last_line_misfit = 1;
11711 }
11712 /* Drop through and scroll. */
11713 else
11714 goto done;
11715 }
11716 else
11717 clear_glyph_matrix (w->desired_matrix);
11718 }
11719
11720 try_to_scroll:
11721
11722 w->last_modified = make_number (0);
11723 w->last_overlay_modified = make_number (0);
11724
11725 /* Redisplay the mode line. Select the buffer properly for that. */
11726 if (!update_mode_line)
11727 {
11728 update_mode_line = 1;
11729 w->update_mode_line = Qt;
11730 }
11731
11732 /* Try to scroll by specified few lines. */
11733 if ((scroll_conservatively
11734 || scroll_step
11735 || temp_scroll_step
11736 || NUMBERP (current_buffer->scroll_up_aggressively)
11737 || NUMBERP (current_buffer->scroll_down_aggressively))
11738 && !current_buffer->clip_changed
11739 && CHARPOS (startp) >= BEGV
11740 && CHARPOS (startp) <= ZV)
11741 {
11742 /* The function returns -1 if new fonts were loaded, 1 if
11743 successful, 0 if not successful. */
11744 int rc = try_scrolling (window, just_this_one_p,
11745 scroll_conservatively,
11746 scroll_step,
11747 temp_scroll_step, last_line_misfit);
11748 switch (rc)
11749 {
11750 case SCROLLING_SUCCESS:
11751 goto done;
11752
11753 case SCROLLING_NEED_LARGER_MATRICES:
11754 goto need_larger_matrices;
11755
11756 case SCROLLING_FAILED:
11757 break;
11758
11759 default:
11760 abort ();
11761 }
11762 }
11763
11764 /* Finally, just choose place to start which centers point */
11765
11766 recenter:
11767 centering_position = window_box_height (w) / 2;
11768
11769 point_at_top:
11770 /* Jump here with centering_position already set to 0. */
11771
11772 #if GLYPH_DEBUG
11773 debug_method_add (w, "recenter");
11774 #endif
11775
11776 /* w->vscroll = 0; */
11777
11778 /* Forget any previously recorded base line for line number display. */
11779 if (!buffer_unchanged_p)
11780 w->base_line_number = Qnil;
11781
11782 /* Move backward half the height of the window. */
11783 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11784 it.current_y = it.last_visible_y;
11785 move_it_vertically_backward (&it, centering_position);
11786 xassert (IT_CHARPOS (it) >= BEGV);
11787
11788 /* The function move_it_vertically_backward may move over more
11789 than the specified y-distance. If it->w is small, e.g. a
11790 mini-buffer window, we may end up in front of the window's
11791 display area. Start displaying at the start of the line
11792 containing PT in this case. */
11793 if (it.current_y <= 0)
11794 {
11795 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11796 move_it_vertically (&it, 0);
11797 xassert (IT_CHARPOS (it) <= PT);
11798 it.current_y = 0;
11799 }
11800
11801 it.current_x = it.hpos = 0;
11802
11803 /* Set startp here explicitly in case that helps avoid an infinite loop
11804 in case the window-scroll-functions functions get errors. */
11805 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
11806
11807 /* Run scroll hooks. */
11808 startp = run_window_scroll_functions (window, it.current.pos);
11809
11810 /* Redisplay the window. */
11811 if (!current_matrix_up_to_date_p
11812 || windows_or_buffers_changed
11813 || cursor_type_changed
11814 /* Don't use try_window_reusing_current_matrix in this case
11815 because it can have changed the buffer. */
11816 || !NILP (Vwindow_scroll_functions)
11817 || !just_this_one_p
11818 || MINI_WINDOW_P (w)
11819 || !try_window_reusing_current_matrix (w))
11820 try_window (window, startp);
11821
11822 /* If new fonts have been loaded (due to fontsets), give up. We
11823 have to start a new redisplay since we need to re-adjust glyph
11824 matrices. */
11825 if (fonts_changed_p)
11826 goto need_larger_matrices;
11827
11828 /* If cursor did not appear assume that the middle of the window is
11829 in the first line of the window. Do it again with the next line.
11830 (Imagine a window of height 100, displaying two lines of height
11831 60. Moving back 50 from it->last_visible_y will end in the first
11832 line.) */
11833 if (w->cursor.vpos < 0)
11834 {
11835 if (!NILP (w->window_end_valid)
11836 && PT >= Z - XFASTINT (w->window_end_pos))
11837 {
11838 clear_glyph_matrix (w->desired_matrix);
11839 move_it_by_lines (&it, 1, 0);
11840 try_window (window, it.current.pos);
11841 }
11842 else if (PT < IT_CHARPOS (it))
11843 {
11844 clear_glyph_matrix (w->desired_matrix);
11845 move_it_by_lines (&it, -1, 0);
11846 try_window (window, it.current.pos);
11847 }
11848 else
11849 {
11850 /* Not much we can do about it. */
11851 }
11852 }
11853
11854 /* Consider the following case: Window starts at BEGV, there is
11855 invisible, intangible text at BEGV, so that display starts at
11856 some point START > BEGV. It can happen that we are called with
11857 PT somewhere between BEGV and START. Try to handle that case. */
11858 if (w->cursor.vpos < 0)
11859 {
11860 struct glyph_row *row = w->current_matrix->rows;
11861 if (row->mode_line_p)
11862 ++row;
11863 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11864 }
11865
11866 if (!make_cursor_line_fully_visible (w))
11867 {
11868 /* If vscroll is enabled, disable it and try again. */
11869 if (w->vscroll)
11870 {
11871 w->vscroll = 0;
11872 clear_glyph_matrix (w->desired_matrix);
11873 goto recenter;
11874 }
11875
11876 /* If centering point failed to make the whole line visible,
11877 put point at the top instead. That has to make the whole line
11878 visible, if it can be done. */
11879 centering_position = 0;
11880 goto point_at_top;
11881 }
11882
11883 done:
11884
11885 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11886 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
11887 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
11888 ? Qt : Qnil);
11889
11890 /* Display the mode line, if we must. */
11891 if ((update_mode_line
11892 /* If window not full width, must redo its mode line
11893 if (a) the window to its side is being redone and
11894 (b) we do a frame-based redisplay. This is a consequence
11895 of how inverted lines are drawn in frame-based redisplay. */
11896 || (!just_this_one_p
11897 && !FRAME_WINDOW_P (f)
11898 && !WINDOW_FULL_WIDTH_P (w))
11899 /* Line number to display. */
11900 || INTEGERP (w->base_line_pos)
11901 /* Column number is displayed and different from the one displayed. */
11902 || (!NILP (w->column_number_displayed)
11903 && (XFASTINT (w->column_number_displayed)
11904 != (int) current_column ()))) /* iftc */
11905 /* This means that the window has a mode line. */
11906 && (WINDOW_WANTS_MODELINE_P (w)
11907 || WINDOW_WANTS_HEADER_LINE_P (w)))
11908 {
11909 display_mode_lines (w);
11910
11911 /* If mode line height has changed, arrange for a thorough
11912 immediate redisplay using the correct mode line height. */
11913 if (WINDOW_WANTS_MODELINE_P (w)
11914 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
11915 {
11916 fonts_changed_p = 1;
11917 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
11918 = DESIRED_MODE_LINE_HEIGHT (w);
11919 }
11920
11921 /* If top line height has changed, arrange for a thorough
11922 immediate redisplay using the correct mode line height. */
11923 if (WINDOW_WANTS_HEADER_LINE_P (w)
11924 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
11925 {
11926 fonts_changed_p = 1;
11927 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
11928 = DESIRED_HEADER_LINE_HEIGHT (w);
11929 }
11930
11931 if (fonts_changed_p)
11932 goto need_larger_matrices;
11933 }
11934
11935 if (!line_number_displayed
11936 && !BUFFERP (w->base_line_pos))
11937 {
11938 w->base_line_pos = Qnil;
11939 w->base_line_number = Qnil;
11940 }
11941
11942 finish_menu_bars:
11943
11944 /* When we reach a frame's selected window, redo the frame's menu bar. */
11945 if (update_mode_line
11946 && EQ (FRAME_SELECTED_WINDOW (f), window))
11947 {
11948 int redisplay_menu_p = 0;
11949 int redisplay_tool_bar_p = 0;
11950
11951 if (FRAME_WINDOW_P (f))
11952 {
11953 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
11954 || defined (USE_GTK)
11955 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
11956 #else
11957 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
11958 #endif
11959 }
11960 else
11961 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
11962
11963 if (redisplay_menu_p)
11964 display_menu_bar (w);
11965
11966 #ifdef HAVE_WINDOW_SYSTEM
11967 #ifdef USE_GTK
11968 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
11969 #else
11970 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
11971 && (FRAME_TOOL_BAR_LINES (f) > 0
11972 || auto_resize_tool_bars_p);
11973
11974 #endif
11975
11976 if (redisplay_tool_bar_p)
11977 redisplay_tool_bar (f);
11978 #endif
11979 }
11980
11981 /* We go to this label, with fonts_changed_p nonzero,
11982 if it is necessary to try again using larger glyph matrices.
11983 We have to redeem the scroll bar even in this case,
11984 because the loop in redisplay_internal expects that. */
11985 need_larger_matrices:
11986 ;
11987 finish_scroll_bars:
11988
11989 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
11990 {
11991 /* Set the thumb's position and size. */
11992 set_vertical_scroll_bar (w);
11993
11994 /* Note that we actually used the scroll bar attached to this
11995 window, so it shouldn't be deleted at the end of redisplay. */
11996 redeem_scroll_bar_hook (w);
11997 }
11998
11999 /* Restore current_buffer and value of point in it. */
12000 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12001 set_buffer_internal_1 (old);
12002 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12003
12004 unbind_to (count, Qnil);
12005 }
12006
12007
12008 /* Build the complete desired matrix of WINDOW with a window start
12009 buffer position POS. Value is non-zero if successful. It is zero
12010 if fonts were loaded during redisplay which makes re-adjusting
12011 glyph matrices necessary. */
12012
12013 int
12014 try_window (window, pos)
12015 Lisp_Object window;
12016 struct text_pos pos;
12017 {
12018 struct window *w = XWINDOW (window);
12019 struct it it;
12020 struct glyph_row *last_text_row = NULL;
12021
12022 /* Make POS the new window start. */
12023 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12024
12025 /* Mark cursor position as unknown. No overlay arrow seen. */
12026 w->cursor.vpos = -1;
12027 overlay_arrow_seen = 0;
12028
12029 /* Initialize iterator and info to start at POS. */
12030 start_display (&it, w, pos);
12031
12032 /* Display all lines of W. */
12033 while (it.current_y < it.last_visible_y)
12034 {
12035 if (display_line (&it))
12036 last_text_row = it.glyph_row - 1;
12037 if (fonts_changed_p)
12038 return 0;
12039 }
12040
12041 /* If bottom moved off end of frame, change mode line percentage. */
12042 if (XFASTINT (w->window_end_pos) <= 0
12043 && Z != IT_CHARPOS (it))
12044 w->update_mode_line = Qt;
12045
12046 /* Set window_end_pos to the offset of the last character displayed
12047 on the window from the end of current_buffer. Set
12048 window_end_vpos to its row number. */
12049 if (last_text_row)
12050 {
12051 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12052 w->window_end_bytepos
12053 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12054 w->window_end_pos
12055 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12056 w->window_end_vpos
12057 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12058 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12059 ->displays_text_p);
12060 }
12061 else
12062 {
12063 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12064 w->window_end_pos = make_number (Z - ZV);
12065 w->window_end_vpos = make_number (0);
12066 }
12067
12068 /* But that is not valid info until redisplay finishes. */
12069 w->window_end_valid = Qnil;
12070 return 1;
12071 }
12072
12073
12074 \f
12075 /************************************************************************
12076 Window redisplay reusing current matrix when buffer has not changed
12077 ************************************************************************/
12078
12079 /* Try redisplay of window W showing an unchanged buffer with a
12080 different window start than the last time it was displayed by
12081 reusing its current matrix. Value is non-zero if successful.
12082 W->start is the new window start. */
12083
12084 static int
12085 try_window_reusing_current_matrix (w)
12086 struct window *w;
12087 {
12088 struct frame *f = XFRAME (w->frame);
12089 struct glyph_row *row, *bottom_row;
12090 struct it it;
12091 struct run run;
12092 struct text_pos start, new_start;
12093 int nrows_scrolled, i;
12094 struct glyph_row *last_text_row;
12095 struct glyph_row *last_reused_text_row;
12096 struct glyph_row *start_row;
12097 int start_vpos, min_y, max_y;
12098
12099 #if GLYPH_DEBUG
12100 if (inhibit_try_window_reusing)
12101 return 0;
12102 #endif
12103
12104 if (/* This function doesn't handle terminal frames. */
12105 !FRAME_WINDOW_P (f)
12106 /* Don't try to reuse the display if windows have been split
12107 or such. */
12108 || windows_or_buffers_changed
12109 || cursor_type_changed)
12110 return 0;
12111
12112 /* Can't do this if region may have changed. */
12113 if ((!NILP (Vtransient_mark_mode)
12114 && !NILP (current_buffer->mark_active))
12115 || !NILP (w->region_showing)
12116 || !NILP (Vshow_trailing_whitespace))
12117 return 0;
12118
12119 /* If top-line visibility has changed, give up. */
12120 if (WINDOW_WANTS_HEADER_LINE_P (w)
12121 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12122 return 0;
12123
12124 /* Give up if old or new display is scrolled vertically. We could
12125 make this function handle this, but right now it doesn't. */
12126 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12127 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
12128 return 0;
12129
12130 /* The variable new_start now holds the new window start. The old
12131 start `start' can be determined from the current matrix. */
12132 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12133 start = start_row->start.pos;
12134 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12135
12136 /* Clear the desired matrix for the display below. */
12137 clear_glyph_matrix (w->desired_matrix);
12138
12139 if (CHARPOS (new_start) <= CHARPOS (start))
12140 {
12141 int first_row_y;
12142
12143 /* Don't use this method if the display starts with an ellipsis
12144 displayed for invisible text. It's not easy to handle that case
12145 below, and it's certainly not worth the effort since this is
12146 not a frequent case. */
12147 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12148 return 0;
12149
12150 IF_DEBUG (debug_method_add (w, "twu1"));
12151
12152 /* Display up to a row that can be reused. The variable
12153 last_text_row is set to the last row displayed that displays
12154 text. Note that it.vpos == 0 if or if not there is a
12155 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12156 start_display (&it, w, new_start);
12157 first_row_y = it.current_y;
12158 w->cursor.vpos = -1;
12159 last_text_row = last_reused_text_row = NULL;
12160
12161 while (it.current_y < it.last_visible_y
12162 && IT_CHARPOS (it) < CHARPOS (start)
12163 && !fonts_changed_p)
12164 if (display_line (&it))
12165 last_text_row = it.glyph_row - 1;
12166
12167 /* A value of current_y < last_visible_y means that we stopped
12168 at the previous window start, which in turn means that we
12169 have at least one reusable row. */
12170 if (it.current_y < it.last_visible_y)
12171 {
12172 /* IT.vpos always starts from 0; it counts text lines. */
12173 nrows_scrolled = it.vpos;
12174
12175 /* Find PT if not already found in the lines displayed. */
12176 if (w->cursor.vpos < 0)
12177 {
12178 int dy = it.current_y - first_row_y;
12179
12180 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12181 row = row_containing_pos (w, PT, row, NULL, dy);
12182 if (row)
12183 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12184 dy, nrows_scrolled);
12185 else
12186 {
12187 clear_glyph_matrix (w->desired_matrix);
12188 return 0;
12189 }
12190 }
12191
12192 /* Scroll the display. Do it before the current matrix is
12193 changed. The problem here is that update has not yet
12194 run, i.e. part of the current matrix is not up to date.
12195 scroll_run_hook will clear the cursor, and use the
12196 current matrix to get the height of the row the cursor is
12197 in. */
12198 run.current_y = first_row_y;
12199 run.desired_y = it.current_y;
12200 run.height = it.last_visible_y - it.current_y;
12201
12202 if (run.height > 0 && run.current_y != run.desired_y)
12203 {
12204 update_begin (f);
12205 rif->update_window_begin_hook (w);
12206 rif->clear_window_mouse_face (w);
12207 rif->scroll_run_hook (w, &run);
12208 rif->update_window_end_hook (w, 0, 0);
12209 update_end (f);
12210 }
12211
12212 /* Shift current matrix down by nrows_scrolled lines. */
12213 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12214 rotate_matrix (w->current_matrix,
12215 start_vpos,
12216 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12217 nrows_scrolled);
12218
12219 /* Disable lines that must be updated. */
12220 for (i = 0; i < it.vpos; ++i)
12221 (start_row + i)->enabled_p = 0;
12222
12223 /* Re-compute Y positions. */
12224 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12225 max_y = it.last_visible_y;
12226 for (row = start_row + nrows_scrolled;
12227 row < bottom_row;
12228 ++row)
12229 {
12230 row->y = it.current_y;
12231 row->visible_height = row->height;
12232
12233 if (row->y < min_y)
12234 row->visible_height -= min_y - row->y;
12235 if (row->y + row->height > max_y)
12236 row->visible_height -= row->y + row->height - max_y;
12237
12238 it.current_y += row->height;
12239
12240 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12241 last_reused_text_row = row;
12242 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12243 break;
12244 }
12245
12246 /* Disable lines in the current matrix which are now
12247 below the window. */
12248 for (++row; row < bottom_row; ++row)
12249 row->enabled_p = 0;
12250 }
12251
12252 /* Update window_end_pos etc.; last_reused_text_row is the last
12253 reused row from the current matrix containing text, if any.
12254 The value of last_text_row is the last displayed line
12255 containing text. */
12256 if (last_reused_text_row)
12257 {
12258 w->window_end_bytepos
12259 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12260 w->window_end_pos
12261 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12262 w->window_end_vpos
12263 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12264 w->current_matrix));
12265 }
12266 else if (last_text_row)
12267 {
12268 w->window_end_bytepos
12269 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12270 w->window_end_pos
12271 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12272 w->window_end_vpos
12273 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12274 }
12275 else
12276 {
12277 /* This window must be completely empty. */
12278 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12279 w->window_end_pos = make_number (Z - ZV);
12280 w->window_end_vpos = make_number (0);
12281 }
12282 w->window_end_valid = Qnil;
12283
12284 /* Update hint: don't try scrolling again in update_window. */
12285 w->desired_matrix->no_scrolling_p = 1;
12286
12287 #if GLYPH_DEBUG
12288 debug_method_add (w, "try_window_reusing_current_matrix 1");
12289 #endif
12290 return 1;
12291 }
12292 else if (CHARPOS (new_start) > CHARPOS (start))
12293 {
12294 struct glyph_row *pt_row, *row;
12295 struct glyph_row *first_reusable_row;
12296 struct glyph_row *first_row_to_display;
12297 int dy;
12298 int yb = window_text_bottom_y (w);
12299
12300 /* Find the row starting at new_start, if there is one. Don't
12301 reuse a partially visible line at the end. */
12302 first_reusable_row = start_row;
12303 while (first_reusable_row->enabled_p
12304 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12305 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12306 < CHARPOS (new_start)))
12307 ++first_reusable_row;
12308
12309 /* Give up if there is no row to reuse. */
12310 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
12311 || !first_reusable_row->enabled_p
12312 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12313 != CHARPOS (new_start)))
12314 return 0;
12315
12316 /* We can reuse fully visible rows beginning with
12317 first_reusable_row to the end of the window. Set
12318 first_row_to_display to the first row that cannot be reused.
12319 Set pt_row to the row containing point, if there is any. */
12320 pt_row = NULL;
12321 for (first_row_to_display = first_reusable_row;
12322 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12323 ++first_row_to_display)
12324 {
12325 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12326 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
12327 pt_row = first_row_to_display;
12328 }
12329
12330 /* Start displaying at the start of first_row_to_display. */
12331 xassert (first_row_to_display->y < yb);
12332 init_to_row_start (&it, w, first_row_to_display);
12333
12334 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12335 - start_vpos);
12336 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12337 - nrows_scrolled);
12338 it.current_y = (first_row_to_display->y - first_reusable_row->y
12339 + WINDOW_HEADER_LINE_HEIGHT (w));
12340
12341 /* Display lines beginning with first_row_to_display in the
12342 desired matrix. Set last_text_row to the last row displayed
12343 that displays text. */
12344 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12345 if (pt_row == NULL)
12346 w->cursor.vpos = -1;
12347 last_text_row = NULL;
12348 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12349 if (display_line (&it))
12350 last_text_row = it.glyph_row - 1;
12351
12352 /* Give up If point isn't in a row displayed or reused. */
12353 if (w->cursor.vpos < 0)
12354 {
12355 clear_glyph_matrix (w->desired_matrix);
12356 return 0;
12357 }
12358
12359 /* If point is in a reused row, adjust y and vpos of the cursor
12360 position. */
12361 if (pt_row)
12362 {
12363 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
12364 w->current_matrix);
12365 w->cursor.y -= first_reusable_row->y;
12366 }
12367
12368 /* Scroll the display. */
12369 run.current_y = first_reusable_row->y;
12370 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
12371 run.height = it.last_visible_y - run.current_y;
12372 dy = run.current_y - run.desired_y;
12373
12374 if (run.height)
12375 {
12376 struct frame *f = XFRAME (WINDOW_FRAME (w));
12377 update_begin (f);
12378 rif->update_window_begin_hook (w);
12379 rif->clear_window_mouse_face (w);
12380 rif->scroll_run_hook (w, &run);
12381 rif->update_window_end_hook (w, 0, 0);
12382 update_end (f);
12383 }
12384
12385 /* Adjust Y positions of reused rows. */
12386 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12387 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12388 max_y = it.last_visible_y;
12389 for (row = first_reusable_row; row < first_row_to_display; ++row)
12390 {
12391 row->y -= dy;
12392 row->visible_height = row->height;
12393 if (row->y < min_y)
12394 row->visible_height -= min_y - row->y;
12395 if (row->y + row->height > max_y)
12396 row->visible_height -= row->y + row->height - max_y;
12397 }
12398
12399 /* Scroll the current matrix. */
12400 xassert (nrows_scrolled > 0);
12401 rotate_matrix (w->current_matrix,
12402 start_vpos,
12403 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12404 -nrows_scrolled);
12405
12406 /* Disable rows not reused. */
12407 for (row -= nrows_scrolled; row < bottom_row; ++row)
12408 row->enabled_p = 0;
12409
12410 /* Adjust window end. A null value of last_text_row means that
12411 the window end is in reused rows which in turn means that
12412 only its vpos can have changed. */
12413 if (last_text_row)
12414 {
12415 w->window_end_bytepos
12416 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12417 w->window_end_pos
12418 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12419 w->window_end_vpos
12420 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12421 }
12422 else
12423 {
12424 w->window_end_vpos
12425 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
12426 }
12427
12428 w->window_end_valid = Qnil;
12429 w->desired_matrix->no_scrolling_p = 1;
12430
12431 #if GLYPH_DEBUG
12432 debug_method_add (w, "try_window_reusing_current_matrix 2");
12433 #endif
12434 return 1;
12435 }
12436
12437 return 0;
12438 }
12439
12440
12441 \f
12442 /************************************************************************
12443 Window redisplay reusing current matrix when buffer has changed
12444 ************************************************************************/
12445
12446 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
12447 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
12448 int *, int *));
12449 static struct glyph_row *
12450 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
12451 struct glyph_row *));
12452
12453
12454 /* Return the last row in MATRIX displaying text. If row START is
12455 non-null, start searching with that row. IT gives the dimensions
12456 of the display. Value is null if matrix is empty; otherwise it is
12457 a pointer to the row found. */
12458
12459 static struct glyph_row *
12460 find_last_row_displaying_text (matrix, it, start)
12461 struct glyph_matrix *matrix;
12462 struct it *it;
12463 struct glyph_row *start;
12464 {
12465 struct glyph_row *row, *row_found;
12466
12467 /* Set row_found to the last row in IT->w's current matrix
12468 displaying text. The loop looks funny but think of partially
12469 visible lines. */
12470 row_found = NULL;
12471 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
12472 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12473 {
12474 xassert (row->enabled_p);
12475 row_found = row;
12476 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
12477 break;
12478 ++row;
12479 }
12480
12481 return row_found;
12482 }
12483
12484
12485 /* Return the last row in the current matrix of W that is not affected
12486 by changes at the start of current_buffer that occurred since W's
12487 current matrix was built. Value is null if no such row exists.
12488
12489 BEG_UNCHANGED us the number of characters unchanged at the start of
12490 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
12491 first changed character in current_buffer. Characters at positions <
12492 BEG + BEG_UNCHANGED are at the same buffer positions as they were
12493 when the current matrix was built. */
12494
12495 static struct glyph_row *
12496 find_last_unchanged_at_beg_row (w)
12497 struct window *w;
12498 {
12499 int first_changed_pos = BEG + BEG_UNCHANGED;
12500 struct glyph_row *row;
12501 struct glyph_row *row_found = NULL;
12502 int yb = window_text_bottom_y (w);
12503
12504 /* Find the last row displaying unchanged text. */
12505 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12506 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12507 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
12508 {
12509 if (/* If row ends before first_changed_pos, it is unchanged,
12510 except in some case. */
12511 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
12512 /* When row ends in ZV and we write at ZV it is not
12513 unchanged. */
12514 && !row->ends_at_zv_p
12515 /* When first_changed_pos is the end of a continued line,
12516 row is not unchanged because it may be no longer
12517 continued. */
12518 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
12519 && row->continued_p))
12520 row_found = row;
12521
12522 /* Stop if last visible row. */
12523 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
12524 break;
12525
12526 ++row;
12527 }
12528
12529 return row_found;
12530 }
12531
12532
12533 /* Find the first glyph row in the current matrix of W that is not
12534 affected by changes at the end of current_buffer since the
12535 time W's current matrix was built.
12536
12537 Return in *DELTA the number of chars by which buffer positions in
12538 unchanged text at the end of current_buffer must be adjusted.
12539
12540 Return in *DELTA_BYTES the corresponding number of bytes.
12541
12542 Value is null if no such row exists, i.e. all rows are affected by
12543 changes. */
12544
12545 static struct glyph_row *
12546 find_first_unchanged_at_end_row (w, delta, delta_bytes)
12547 struct window *w;
12548 int *delta, *delta_bytes;
12549 {
12550 struct glyph_row *row;
12551 struct glyph_row *row_found = NULL;
12552
12553 *delta = *delta_bytes = 0;
12554
12555 /* Display must not have been paused, otherwise the current matrix
12556 is not up to date. */
12557 if (NILP (w->window_end_valid))
12558 abort ();
12559
12560 /* A value of window_end_pos >= END_UNCHANGED means that the window
12561 end is in the range of changed text. If so, there is no
12562 unchanged row at the end of W's current matrix. */
12563 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
12564 return NULL;
12565
12566 /* Set row to the last row in W's current matrix displaying text. */
12567 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12568
12569 /* If matrix is entirely empty, no unchanged row exists. */
12570 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12571 {
12572 /* The value of row is the last glyph row in the matrix having a
12573 meaningful buffer position in it. The end position of row
12574 corresponds to window_end_pos. This allows us to translate
12575 buffer positions in the current matrix to current buffer
12576 positions for characters not in changed text. */
12577 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12578 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12579 int last_unchanged_pos, last_unchanged_pos_old;
12580 struct glyph_row *first_text_row
12581 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12582
12583 *delta = Z - Z_old;
12584 *delta_bytes = Z_BYTE - Z_BYTE_old;
12585
12586 /* Set last_unchanged_pos to the buffer position of the last
12587 character in the buffer that has not been changed. Z is the
12588 index + 1 of the last character in current_buffer, i.e. by
12589 subtracting END_UNCHANGED we get the index of the last
12590 unchanged character, and we have to add BEG to get its buffer
12591 position. */
12592 last_unchanged_pos = Z - END_UNCHANGED + BEG;
12593 last_unchanged_pos_old = last_unchanged_pos - *delta;
12594
12595 /* Search backward from ROW for a row displaying a line that
12596 starts at a minimum position >= last_unchanged_pos_old. */
12597 for (; row > first_text_row; --row)
12598 {
12599 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
12600 abort ();
12601
12602 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
12603 row_found = row;
12604 }
12605 }
12606
12607 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
12608 abort ();
12609
12610 return row_found;
12611 }
12612
12613
12614 /* Make sure that glyph rows in the current matrix of window W
12615 reference the same glyph memory as corresponding rows in the
12616 frame's frame matrix. This function is called after scrolling W's
12617 current matrix on a terminal frame in try_window_id and
12618 try_window_reusing_current_matrix. */
12619
12620 static void
12621 sync_frame_with_window_matrix_rows (w)
12622 struct window *w;
12623 {
12624 struct frame *f = XFRAME (w->frame);
12625 struct glyph_row *window_row, *window_row_end, *frame_row;
12626
12627 /* Preconditions: W must be a leaf window and full-width. Its frame
12628 must have a frame matrix. */
12629 xassert (NILP (w->hchild) && NILP (w->vchild));
12630 xassert (WINDOW_FULL_WIDTH_P (w));
12631 xassert (!FRAME_WINDOW_P (f));
12632
12633 /* If W is a full-width window, glyph pointers in W's current matrix
12634 have, by definition, to be the same as glyph pointers in the
12635 corresponding frame matrix. Note that frame matrices have no
12636 marginal areas (see build_frame_matrix). */
12637 window_row = w->current_matrix->rows;
12638 window_row_end = window_row + w->current_matrix->nrows;
12639 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
12640 while (window_row < window_row_end)
12641 {
12642 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
12643 struct glyph *end = window_row->glyphs[LAST_AREA];
12644
12645 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
12646 frame_row->glyphs[TEXT_AREA] = start;
12647 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
12648 frame_row->glyphs[LAST_AREA] = end;
12649
12650 /* Disable frame rows whose corresponding window rows have
12651 been disabled in try_window_id. */
12652 if (!window_row->enabled_p)
12653 frame_row->enabled_p = 0;
12654
12655 ++window_row, ++frame_row;
12656 }
12657 }
12658
12659
12660 /* Find the glyph row in window W containing CHARPOS. Consider all
12661 rows between START and END (not inclusive). END null means search
12662 all rows to the end of the display area of W. Value is the row
12663 containing CHARPOS or null. */
12664
12665 struct glyph_row *
12666 row_containing_pos (w, charpos, start, end, dy)
12667 struct window *w;
12668 int charpos;
12669 struct glyph_row *start, *end;
12670 int dy;
12671 {
12672 struct glyph_row *row = start;
12673 int last_y;
12674
12675 /* If we happen to start on a header-line, skip that. */
12676 if (row->mode_line_p)
12677 ++row;
12678
12679 if ((end && row >= end) || !row->enabled_p)
12680 return NULL;
12681
12682 last_y = window_text_bottom_y (w) - dy;
12683
12684 while (1)
12685 {
12686 /* Give up if we have gone too far. */
12687 if (end && row >= end)
12688 return NULL;
12689 /* This formerly returned if they were equal.
12690 I think that both quantities are of a "last plus one" type;
12691 if so, when they are equal, the row is within the screen. -- rms. */
12692 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
12693 return NULL;
12694
12695 /* If it is in this row, return this row. */
12696 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
12697 || (MATRIX_ROW_END_CHARPOS (row) == charpos
12698 /* The end position of a row equals the start
12699 position of the next row. If CHARPOS is there, we
12700 would rather display it in the next line, except
12701 when this line ends in ZV. */
12702 && !row->ends_at_zv_p
12703 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12704 && charpos >= MATRIX_ROW_START_CHARPOS (row))
12705 return row;
12706 ++row;
12707 }
12708 }
12709
12710
12711 /* Try to redisplay window W by reusing its existing display. W's
12712 current matrix must be up to date when this function is called,
12713 i.e. window_end_valid must not be nil.
12714
12715 Value is
12716
12717 1 if display has been updated
12718 0 if otherwise unsuccessful
12719 -1 if redisplay with same window start is known not to succeed
12720
12721 The following steps are performed:
12722
12723 1. Find the last row in the current matrix of W that is not
12724 affected by changes at the start of current_buffer. If no such row
12725 is found, give up.
12726
12727 2. Find the first row in W's current matrix that is not affected by
12728 changes at the end of current_buffer. Maybe there is no such row.
12729
12730 3. Display lines beginning with the row + 1 found in step 1 to the
12731 row found in step 2 or, if step 2 didn't find a row, to the end of
12732 the window.
12733
12734 4. If cursor is not known to appear on the window, give up.
12735
12736 5. If display stopped at the row found in step 2, scroll the
12737 display and current matrix as needed.
12738
12739 6. Maybe display some lines at the end of W, if we must. This can
12740 happen under various circumstances, like a partially visible line
12741 becoming fully visible, or because newly displayed lines are displayed
12742 in smaller font sizes.
12743
12744 7. Update W's window end information. */
12745
12746 static int
12747 try_window_id (w)
12748 struct window *w;
12749 {
12750 struct frame *f = XFRAME (w->frame);
12751 struct glyph_matrix *current_matrix = w->current_matrix;
12752 struct glyph_matrix *desired_matrix = w->desired_matrix;
12753 struct glyph_row *last_unchanged_at_beg_row;
12754 struct glyph_row *first_unchanged_at_end_row;
12755 struct glyph_row *row;
12756 struct glyph_row *bottom_row;
12757 int bottom_vpos;
12758 struct it it;
12759 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
12760 struct text_pos start_pos;
12761 struct run run;
12762 int first_unchanged_at_end_vpos = 0;
12763 struct glyph_row *last_text_row, *last_text_row_at_end;
12764 struct text_pos start;
12765 int first_changed_charpos, last_changed_charpos;
12766
12767 #if GLYPH_DEBUG
12768 if (inhibit_try_window_id)
12769 return 0;
12770 #endif
12771
12772 /* This is handy for debugging. */
12773 #if 0
12774 #define GIVE_UP(X) \
12775 do { \
12776 fprintf (stderr, "try_window_id give up %d\n", (X)); \
12777 return 0; \
12778 } while (0)
12779 #else
12780 #define GIVE_UP(X) return 0
12781 #endif
12782
12783 SET_TEXT_POS_FROM_MARKER (start, w->start);
12784
12785 /* Don't use this for mini-windows because these can show
12786 messages and mini-buffers, and we don't handle that here. */
12787 if (MINI_WINDOW_P (w))
12788 GIVE_UP (1);
12789
12790 /* This flag is used to prevent redisplay optimizations. */
12791 if (windows_or_buffers_changed || cursor_type_changed)
12792 GIVE_UP (2);
12793
12794 /* Verify that narrowing has not changed.
12795 Also verify that we were not told to prevent redisplay optimizations.
12796 It would be nice to further
12797 reduce the number of cases where this prevents try_window_id. */
12798 if (current_buffer->clip_changed
12799 || current_buffer->prevent_redisplay_optimizations_p)
12800 GIVE_UP (3);
12801
12802 /* Window must either use window-based redisplay or be full width. */
12803 if (!FRAME_WINDOW_P (f)
12804 && (!line_ins_del_ok
12805 || !WINDOW_FULL_WIDTH_P (w)))
12806 GIVE_UP (4);
12807
12808 /* Give up if point is not known NOT to appear in W. */
12809 if (PT < CHARPOS (start))
12810 GIVE_UP (5);
12811
12812 /* Another way to prevent redisplay optimizations. */
12813 if (XFASTINT (w->last_modified) == 0)
12814 GIVE_UP (6);
12815
12816 /* Verify that window is not hscrolled. */
12817 if (XFASTINT (w->hscroll) != 0)
12818 GIVE_UP (7);
12819
12820 /* Verify that display wasn't paused. */
12821 if (NILP (w->window_end_valid))
12822 GIVE_UP (8);
12823
12824 /* Can't use this if highlighting a region because a cursor movement
12825 will do more than just set the cursor. */
12826 if (!NILP (Vtransient_mark_mode)
12827 && !NILP (current_buffer->mark_active))
12828 GIVE_UP (9);
12829
12830 /* Likewise if highlighting trailing whitespace. */
12831 if (!NILP (Vshow_trailing_whitespace))
12832 GIVE_UP (11);
12833
12834 /* Likewise if showing a region. */
12835 if (!NILP (w->region_showing))
12836 GIVE_UP (10);
12837
12838 /* Can use this if overlay arrow position and or string have changed. */
12839 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
12840 || !EQ (last_arrow_string, Voverlay_arrow_string))
12841 GIVE_UP (12);
12842
12843
12844 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
12845 only if buffer has really changed. The reason is that the gap is
12846 initially at Z for freshly visited files. The code below would
12847 set end_unchanged to 0 in that case. */
12848 if (MODIFF > SAVE_MODIFF
12849 /* This seems to happen sometimes after saving a buffer. */
12850 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
12851 {
12852 if (GPT - BEG < BEG_UNCHANGED)
12853 BEG_UNCHANGED = GPT - BEG;
12854 if (Z - GPT < END_UNCHANGED)
12855 END_UNCHANGED = Z - GPT;
12856 }
12857
12858 /* The position of the first and last character that has been changed. */
12859 first_changed_charpos = BEG + BEG_UNCHANGED;
12860 last_changed_charpos = Z - END_UNCHANGED;
12861
12862 /* If window starts after a line end, and the last change is in
12863 front of that newline, then changes don't affect the display.
12864 This case happens with stealth-fontification. Note that although
12865 the display is unchanged, glyph positions in the matrix have to
12866 be adjusted, of course. */
12867 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12868 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12869 && ((last_changed_charpos < CHARPOS (start)
12870 && CHARPOS (start) == BEGV)
12871 || (last_changed_charpos < CHARPOS (start) - 1
12872 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
12873 {
12874 int Z_old, delta, Z_BYTE_old, delta_bytes;
12875 struct glyph_row *r0;
12876
12877 /* Compute how many chars/bytes have been added to or removed
12878 from the buffer. */
12879 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12880 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12881 delta = Z - Z_old;
12882 delta_bytes = Z_BYTE - Z_BYTE_old;
12883
12884 /* Give up if PT is not in the window. Note that it already has
12885 been checked at the start of try_window_id that PT is not in
12886 front of the window start. */
12887 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
12888 GIVE_UP (13);
12889
12890 /* If window start is unchanged, we can reuse the whole matrix
12891 as is, after adjusting glyph positions. No need to compute
12892 the window end again, since its offset from Z hasn't changed. */
12893 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
12894 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
12895 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
12896 /* PT must not be in a partially visible line. */
12897 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
12898 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
12899 {
12900 /* Adjust positions in the glyph matrix. */
12901 if (delta || delta_bytes)
12902 {
12903 struct glyph_row *r1
12904 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
12905 increment_matrix_positions (w->current_matrix,
12906 MATRIX_ROW_VPOS (r0, current_matrix),
12907 MATRIX_ROW_VPOS (r1, current_matrix),
12908 delta, delta_bytes);
12909 }
12910
12911 /* Set the cursor. */
12912 row = row_containing_pos (w, PT, r0, NULL, 0);
12913 if (row)
12914 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
12915 else
12916 abort ();
12917 return 1;
12918 }
12919 }
12920
12921 /* Handle the case that changes are all below what is displayed in
12922 the window, and that PT is in the window. This shortcut cannot
12923 be taken if ZV is visible in the window, and text has been added
12924 there that is visible in the window. */
12925 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
12926 /* ZV is not visible in the window, or there are no
12927 changes at ZV, actually. */
12928 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
12929 || first_changed_charpos == last_changed_charpos))
12930 {
12931 struct glyph_row *r0;
12932
12933 /* Give up if PT is not in the window. Note that it already has
12934 been checked at the start of try_window_id that PT is not in
12935 front of the window start. */
12936 if (PT >= MATRIX_ROW_END_CHARPOS (row))
12937 GIVE_UP (14);
12938
12939 /* If window start is unchanged, we can reuse the whole matrix
12940 as is, without changing glyph positions since no text has
12941 been added/removed in front of the window end. */
12942 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
12943 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
12944 /* PT must not be in a partially visible line. */
12945 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
12946 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
12947 {
12948 /* We have to compute the window end anew since text
12949 can have been added/removed after it. */
12950 w->window_end_pos
12951 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12952 w->window_end_bytepos
12953 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12954
12955 /* Set the cursor. */
12956 row = row_containing_pos (w, PT, r0, NULL, 0);
12957 if (row)
12958 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
12959 else
12960 abort ();
12961 return 2;
12962 }
12963 }
12964
12965 /* Give up if window start is in the changed area.
12966
12967 The condition used to read
12968
12969 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
12970
12971 but why that was tested escapes me at the moment. */
12972 if (CHARPOS (start) >= first_changed_charpos
12973 && CHARPOS (start) <= last_changed_charpos)
12974 GIVE_UP (15);
12975
12976 /* Check that window start agrees with the start of the first glyph
12977 row in its current matrix. Check this after we know the window
12978 start is not in changed text, otherwise positions would not be
12979 comparable. */
12980 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
12981 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
12982 GIVE_UP (16);
12983
12984 /* Give up if the window ends in strings. Overlay strings
12985 at the end are difficult to handle, so don't try. */
12986 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
12987 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
12988 GIVE_UP (20);
12989
12990 /* Compute the position at which we have to start displaying new
12991 lines. Some of the lines at the top of the window might be
12992 reusable because they are not displaying changed text. Find the
12993 last row in W's current matrix not affected by changes at the
12994 start of current_buffer. Value is null if changes start in the
12995 first line of window. */
12996 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
12997 if (last_unchanged_at_beg_row)
12998 {
12999 /* Avoid starting to display in the moddle of a character, a TAB
13000 for instance. This is easier than to set up the iterator
13001 exactly, and it's not a frequent case, so the additional
13002 effort wouldn't really pay off. */
13003 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13004 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13005 && last_unchanged_at_beg_row > w->current_matrix->rows)
13006 --last_unchanged_at_beg_row;
13007
13008 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13009 GIVE_UP (17);
13010
13011 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13012 GIVE_UP (18);
13013 start_pos = it.current.pos;
13014
13015 /* Start displaying new lines in the desired matrix at the same
13016 vpos we would use in the current matrix, i.e. below
13017 last_unchanged_at_beg_row. */
13018 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13019 current_matrix);
13020 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13021 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13022
13023 xassert (it.hpos == 0 && it.current_x == 0);
13024 }
13025 else
13026 {
13027 /* There are no reusable lines at the start of the window.
13028 Start displaying in the first line. */
13029 start_display (&it, w, start);
13030 start_pos = it.current.pos;
13031 }
13032
13033 /* Find the first row that is not affected by changes at the end of
13034 the buffer. Value will be null if there is no unchanged row, in
13035 which case we must redisplay to the end of the window. delta
13036 will be set to the value by which buffer positions beginning with
13037 first_unchanged_at_end_row have to be adjusted due to text
13038 changes. */
13039 first_unchanged_at_end_row
13040 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13041 IF_DEBUG (debug_delta = delta);
13042 IF_DEBUG (debug_delta_bytes = delta_bytes);
13043
13044 /* Set stop_pos to the buffer position up to which we will have to
13045 display new lines. If first_unchanged_at_end_row != NULL, this
13046 is the buffer position of the start of the line displayed in that
13047 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13048 that we don't stop at a buffer position. */
13049 stop_pos = 0;
13050 if (first_unchanged_at_end_row)
13051 {
13052 xassert (last_unchanged_at_beg_row == NULL
13053 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13054
13055 /* If this is a continuation line, move forward to the next one
13056 that isn't. Changes in lines above affect this line.
13057 Caution: this may move first_unchanged_at_end_row to a row
13058 not displaying text. */
13059 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13060 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13061 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13062 < it.last_visible_y))
13063 ++first_unchanged_at_end_row;
13064
13065 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13066 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13067 >= it.last_visible_y))
13068 first_unchanged_at_end_row = NULL;
13069 else
13070 {
13071 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13072 + delta);
13073 first_unchanged_at_end_vpos
13074 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13075 xassert (stop_pos >= Z - END_UNCHANGED);
13076 }
13077 }
13078 else if (last_unchanged_at_beg_row == NULL)
13079 GIVE_UP (19);
13080
13081
13082 #if GLYPH_DEBUG
13083
13084 /* Either there is no unchanged row at the end, or the one we have
13085 now displays text. This is a necessary condition for the window
13086 end pos calculation at the end of this function. */
13087 xassert (first_unchanged_at_end_row == NULL
13088 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13089
13090 debug_last_unchanged_at_beg_vpos
13091 = (last_unchanged_at_beg_row
13092 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13093 : -1);
13094 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13095
13096 #endif /* GLYPH_DEBUG != 0 */
13097
13098
13099 /* Display new lines. Set last_text_row to the last new line
13100 displayed which has text on it, i.e. might end up as being the
13101 line where the window_end_vpos is. */
13102 w->cursor.vpos = -1;
13103 last_text_row = NULL;
13104 overlay_arrow_seen = 0;
13105 while (it.current_y < it.last_visible_y
13106 && !fonts_changed_p
13107 && (first_unchanged_at_end_row == NULL
13108 || IT_CHARPOS (it) < stop_pos))
13109 {
13110 if (display_line (&it))
13111 last_text_row = it.glyph_row - 1;
13112 }
13113
13114 if (fonts_changed_p)
13115 return -1;
13116
13117
13118 /* Compute differences in buffer positions, y-positions etc. for
13119 lines reused at the bottom of the window. Compute what we can
13120 scroll. */
13121 if (first_unchanged_at_end_row
13122 /* No lines reused because we displayed everything up to the
13123 bottom of the window. */
13124 && it.current_y < it.last_visible_y)
13125 {
13126 dvpos = (it.vpos
13127 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13128 current_matrix));
13129 dy = it.current_y - first_unchanged_at_end_row->y;
13130 run.current_y = first_unchanged_at_end_row->y;
13131 run.desired_y = run.current_y + dy;
13132 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13133 }
13134 else
13135 {
13136 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13137 first_unchanged_at_end_row = NULL;
13138 }
13139 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13140
13141
13142 /* Find the cursor if not already found. We have to decide whether
13143 PT will appear on this window (it sometimes doesn't, but this is
13144 not a very frequent case.) This decision has to be made before
13145 the current matrix is altered. A value of cursor.vpos < 0 means
13146 that PT is either in one of the lines beginning at
13147 first_unchanged_at_end_row or below the window. Don't care for
13148 lines that might be displayed later at the window end; as
13149 mentioned, this is not a frequent case. */
13150 if (w->cursor.vpos < 0)
13151 {
13152 /* Cursor in unchanged rows at the top? */
13153 if (PT < CHARPOS (start_pos)
13154 && last_unchanged_at_beg_row)
13155 {
13156 row = row_containing_pos (w, PT,
13157 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13158 last_unchanged_at_beg_row + 1, 0);
13159 if (row)
13160 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13161 }
13162
13163 /* Start from first_unchanged_at_end_row looking for PT. */
13164 else if (first_unchanged_at_end_row)
13165 {
13166 row = row_containing_pos (w, PT - delta,
13167 first_unchanged_at_end_row, NULL, 0);
13168 if (row)
13169 set_cursor_from_row (w, row, w->current_matrix, delta,
13170 delta_bytes, dy, dvpos);
13171 }
13172
13173 /* Give up if cursor was not found. */
13174 if (w->cursor.vpos < 0)
13175 {
13176 clear_glyph_matrix (w->desired_matrix);
13177 return -1;
13178 }
13179 }
13180
13181 /* Don't let the cursor end in the scroll margins. */
13182 {
13183 int this_scroll_margin, cursor_height;
13184
13185 this_scroll_margin = max (0, scroll_margin);
13186 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13187 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13188 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13189
13190 if ((w->cursor.y < this_scroll_margin
13191 && CHARPOS (start) > BEGV)
13192 /* Don't take scroll margin into account at the bottom because
13193 old redisplay didn't do it either. */
13194 || w->cursor.y + cursor_height > it.last_visible_y)
13195 {
13196 w->cursor.vpos = -1;
13197 clear_glyph_matrix (w->desired_matrix);
13198 return -1;
13199 }
13200 }
13201
13202 /* Scroll the display. Do it before changing the current matrix so
13203 that xterm.c doesn't get confused about where the cursor glyph is
13204 found. */
13205 if (dy && run.height)
13206 {
13207 update_begin (f);
13208
13209 if (FRAME_WINDOW_P (f))
13210 {
13211 rif->update_window_begin_hook (w);
13212 rif->clear_window_mouse_face (w);
13213 rif->scroll_run_hook (w, &run);
13214 rif->update_window_end_hook (w, 0, 0);
13215 }
13216 else
13217 {
13218 /* Terminal frame. In this case, dvpos gives the number of
13219 lines to scroll by; dvpos < 0 means scroll up. */
13220 int first_unchanged_at_end_vpos
13221 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13222 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13223 int end = (WINDOW_TOP_EDGE_LINE (w)
13224 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13225 + window_internal_height (w));
13226
13227 /* Perform the operation on the screen. */
13228 if (dvpos > 0)
13229 {
13230 /* Scroll last_unchanged_at_beg_row to the end of the
13231 window down dvpos lines. */
13232 set_terminal_window (end);
13233
13234 /* On dumb terminals delete dvpos lines at the end
13235 before inserting dvpos empty lines. */
13236 if (!scroll_region_ok)
13237 ins_del_lines (end - dvpos, -dvpos);
13238
13239 /* Insert dvpos empty lines in front of
13240 last_unchanged_at_beg_row. */
13241 ins_del_lines (from, dvpos);
13242 }
13243 else if (dvpos < 0)
13244 {
13245 /* Scroll up last_unchanged_at_beg_vpos to the end of
13246 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13247 set_terminal_window (end);
13248
13249 /* Delete dvpos lines in front of
13250 last_unchanged_at_beg_vpos. ins_del_lines will set
13251 the cursor to the given vpos and emit |dvpos| delete
13252 line sequences. */
13253 ins_del_lines (from + dvpos, dvpos);
13254
13255 /* On a dumb terminal insert dvpos empty lines at the
13256 end. */
13257 if (!scroll_region_ok)
13258 ins_del_lines (end + dvpos, -dvpos);
13259 }
13260
13261 set_terminal_window (0);
13262 }
13263
13264 update_end (f);
13265 }
13266
13267 /* Shift reused rows of the current matrix to the right position.
13268 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13269 text. */
13270 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13271 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
13272 if (dvpos < 0)
13273 {
13274 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13275 bottom_vpos, dvpos);
13276 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13277 bottom_vpos, 0);
13278 }
13279 else if (dvpos > 0)
13280 {
13281 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13282 bottom_vpos, dvpos);
13283 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13284 first_unchanged_at_end_vpos + dvpos, 0);
13285 }
13286
13287 /* For frame-based redisplay, make sure that current frame and window
13288 matrix are in sync with respect to glyph memory. */
13289 if (!FRAME_WINDOW_P (f))
13290 sync_frame_with_window_matrix_rows (w);
13291
13292 /* Adjust buffer positions in reused rows. */
13293 if (delta)
13294 increment_matrix_positions (current_matrix,
13295 first_unchanged_at_end_vpos + dvpos,
13296 bottom_vpos, delta, delta_bytes);
13297
13298 /* Adjust Y positions. */
13299 if (dy)
13300 shift_glyph_matrix (w, current_matrix,
13301 first_unchanged_at_end_vpos + dvpos,
13302 bottom_vpos, dy);
13303
13304 if (first_unchanged_at_end_row)
13305 first_unchanged_at_end_row += dvpos;
13306
13307 /* If scrolling up, there may be some lines to display at the end of
13308 the window. */
13309 last_text_row_at_end = NULL;
13310 if (dy < 0)
13311 {
13312 /* Scrolling up can leave for example a partially visible line
13313 at the end of the window to be redisplayed. */
13314 /* Set last_row to the glyph row in the current matrix where the
13315 window end line is found. It has been moved up or down in
13316 the matrix by dvpos. */
13317 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13318 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13319
13320 /* If last_row is the window end line, it should display text. */
13321 xassert (last_row->displays_text_p);
13322
13323 /* If window end line was partially visible before, begin
13324 displaying at that line. Otherwise begin displaying with the
13325 line following it. */
13326 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13327 {
13328 init_to_row_start (&it, w, last_row);
13329 it.vpos = last_vpos;
13330 it.current_y = last_row->y;
13331 }
13332 else
13333 {
13334 init_to_row_end (&it, w, last_row);
13335 it.vpos = 1 + last_vpos;
13336 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13337 ++last_row;
13338 }
13339
13340 /* We may start in a continuation line. If so, we have to
13341 get the right continuation_lines_width and current_x. */
13342 it.continuation_lines_width = last_row->continuation_lines_width;
13343 it.hpos = it.current_x = 0;
13344
13345 /* Display the rest of the lines at the window end. */
13346 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13347 while (it.current_y < it.last_visible_y
13348 && !fonts_changed_p)
13349 {
13350 /* Is it always sure that the display agrees with lines in
13351 the current matrix? I don't think so, so we mark rows
13352 displayed invalid in the current matrix by setting their
13353 enabled_p flag to zero. */
13354 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13355 if (display_line (&it))
13356 last_text_row_at_end = it.glyph_row - 1;
13357 }
13358 }
13359
13360 /* Update window_end_pos and window_end_vpos. */
13361 if (first_unchanged_at_end_row
13362 && first_unchanged_at_end_row->y < it.last_visible_y
13363 && !last_text_row_at_end)
13364 {
13365 /* Window end line if one of the preserved rows from the current
13366 matrix. Set row to the last row displaying text in current
13367 matrix starting at first_unchanged_at_end_row, after
13368 scrolling. */
13369 xassert (first_unchanged_at_end_row->displays_text_p);
13370 row = find_last_row_displaying_text (w->current_matrix, &it,
13371 first_unchanged_at_end_row);
13372 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
13373
13374 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13375 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13376 w->window_end_vpos
13377 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
13378 xassert (w->window_end_bytepos >= 0);
13379 IF_DEBUG (debug_method_add (w, "A"));
13380 }
13381 else if (last_text_row_at_end)
13382 {
13383 w->window_end_pos
13384 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
13385 w->window_end_bytepos
13386 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
13387 w->window_end_vpos
13388 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
13389 xassert (w->window_end_bytepos >= 0);
13390 IF_DEBUG (debug_method_add (w, "B"));
13391 }
13392 else if (last_text_row)
13393 {
13394 /* We have displayed either to the end of the window or at the
13395 end of the window, i.e. the last row with text is to be found
13396 in the desired matrix. */
13397 w->window_end_pos
13398 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13399 w->window_end_bytepos
13400 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13401 w->window_end_vpos
13402 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
13403 xassert (w->window_end_bytepos >= 0);
13404 }
13405 else if (first_unchanged_at_end_row == NULL
13406 && last_text_row == NULL
13407 && last_text_row_at_end == NULL)
13408 {
13409 /* Displayed to end of window, but no line containing text was
13410 displayed. Lines were deleted at the end of the window. */
13411 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
13412 int vpos = XFASTINT (w->window_end_vpos);
13413 struct glyph_row *current_row = current_matrix->rows + vpos;
13414 struct glyph_row *desired_row = desired_matrix->rows + vpos;
13415
13416 for (row = NULL;
13417 row == NULL && vpos >= first_vpos;
13418 --vpos, --current_row, --desired_row)
13419 {
13420 if (desired_row->enabled_p)
13421 {
13422 if (desired_row->displays_text_p)
13423 row = desired_row;
13424 }
13425 else if (current_row->displays_text_p)
13426 row = current_row;
13427 }
13428
13429 xassert (row != NULL);
13430 w->window_end_vpos = make_number (vpos + 1);
13431 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13432 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13433 xassert (w->window_end_bytepos >= 0);
13434 IF_DEBUG (debug_method_add (w, "C"));
13435 }
13436 else
13437 abort ();
13438
13439 #if 0 /* This leads to problems, for instance when the cursor is
13440 at ZV, and the cursor line displays no text. */
13441 /* Disable rows below what's displayed in the window. This makes
13442 debugging easier. */
13443 enable_glyph_matrix_rows (current_matrix,
13444 XFASTINT (w->window_end_vpos) + 1,
13445 bottom_vpos, 0);
13446 #endif
13447
13448 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
13449 debug_end_vpos = XFASTINT (w->window_end_vpos));
13450
13451 /* Record that display has not been completed. */
13452 w->window_end_valid = Qnil;
13453 w->desired_matrix->no_scrolling_p = 1;
13454 return 3;
13455
13456 #undef GIVE_UP
13457 }
13458
13459
13460 \f
13461 /***********************************************************************
13462 More debugging support
13463 ***********************************************************************/
13464
13465 #if GLYPH_DEBUG
13466
13467 void dump_glyph_row P_ ((struct glyph_row *, int, int));
13468 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
13469 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
13470
13471
13472 /* Dump the contents of glyph matrix MATRIX on stderr.
13473
13474 GLYPHS 0 means don't show glyph contents.
13475 GLYPHS 1 means show glyphs in short form
13476 GLYPHS > 1 means show glyphs in long form. */
13477
13478 void
13479 dump_glyph_matrix (matrix, glyphs)
13480 struct glyph_matrix *matrix;
13481 int glyphs;
13482 {
13483 int i;
13484 for (i = 0; i < matrix->nrows; ++i)
13485 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
13486 }
13487
13488
13489 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
13490 the glyph row and area where the glyph comes from. */
13491
13492 void
13493 dump_glyph (row, glyph, area)
13494 struct glyph_row *row;
13495 struct glyph *glyph;
13496 int area;
13497 {
13498 if (glyph->type == CHAR_GLYPH)
13499 {
13500 fprintf (stderr,
13501 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13502 glyph - row->glyphs[TEXT_AREA],
13503 'C',
13504 glyph->charpos,
13505 (BUFFERP (glyph->object)
13506 ? 'B'
13507 : (STRINGP (glyph->object)
13508 ? 'S'
13509 : '-')),
13510 glyph->pixel_width,
13511 glyph->u.ch,
13512 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
13513 ? glyph->u.ch
13514 : '.'),
13515 glyph->face_id,
13516 glyph->left_box_line_p,
13517 glyph->right_box_line_p);
13518 }
13519 else if (glyph->type == STRETCH_GLYPH)
13520 {
13521 fprintf (stderr,
13522 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13523 glyph - row->glyphs[TEXT_AREA],
13524 'S',
13525 glyph->charpos,
13526 (BUFFERP (glyph->object)
13527 ? 'B'
13528 : (STRINGP (glyph->object)
13529 ? 'S'
13530 : '-')),
13531 glyph->pixel_width,
13532 0,
13533 '.',
13534 glyph->face_id,
13535 glyph->left_box_line_p,
13536 glyph->right_box_line_p);
13537 }
13538 else if (glyph->type == IMAGE_GLYPH)
13539 {
13540 fprintf (stderr,
13541 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13542 glyph - row->glyphs[TEXT_AREA],
13543 'I',
13544 glyph->charpos,
13545 (BUFFERP (glyph->object)
13546 ? 'B'
13547 : (STRINGP (glyph->object)
13548 ? 'S'
13549 : '-')),
13550 glyph->pixel_width,
13551 glyph->u.img_id,
13552 '.',
13553 glyph->face_id,
13554 glyph->left_box_line_p,
13555 glyph->right_box_line_p);
13556 }
13557 }
13558
13559
13560 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
13561 GLYPHS 0 means don't show glyph contents.
13562 GLYPHS 1 means show glyphs in short form
13563 GLYPHS > 1 means show glyphs in long form. */
13564
13565 void
13566 dump_glyph_row (row, vpos, glyphs)
13567 struct glyph_row *row;
13568 int vpos, glyphs;
13569 {
13570 if (glyphs != 1)
13571 {
13572 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
13573 fprintf (stderr, "=======================================================================\n");
13574
13575 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
13576 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
13577 vpos,
13578 MATRIX_ROW_START_CHARPOS (row),
13579 MATRIX_ROW_END_CHARPOS (row),
13580 row->used[TEXT_AREA],
13581 row->contains_overlapping_glyphs_p,
13582 row->enabled_p,
13583 row->truncated_on_left_p,
13584 row->truncated_on_right_p,
13585 row->overlay_arrow_p,
13586 row->continued_p,
13587 MATRIX_ROW_CONTINUATION_LINE_P (row),
13588 row->displays_text_p,
13589 row->ends_at_zv_p,
13590 row->fill_line_p,
13591 row->ends_in_middle_of_char_p,
13592 row->starts_in_middle_of_char_p,
13593 row->mouse_face_p,
13594 row->x,
13595 row->y,
13596 row->pixel_width,
13597 row->height,
13598 row->visible_height,
13599 row->ascent,
13600 row->phys_ascent);
13601 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
13602 row->end.overlay_string_index,
13603 row->continuation_lines_width);
13604 fprintf (stderr, "%9d %5d\n",
13605 CHARPOS (row->start.string_pos),
13606 CHARPOS (row->end.string_pos));
13607 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
13608 row->end.dpvec_index);
13609 }
13610
13611 if (glyphs > 1)
13612 {
13613 int area;
13614
13615 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13616 {
13617 struct glyph *glyph = row->glyphs[area];
13618 struct glyph *glyph_end = glyph + row->used[area];
13619
13620 /* Glyph for a line end in text. */
13621 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
13622 ++glyph_end;
13623
13624 if (glyph < glyph_end)
13625 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
13626
13627 for (; glyph < glyph_end; ++glyph)
13628 dump_glyph (row, glyph, area);
13629 }
13630 }
13631 else if (glyphs == 1)
13632 {
13633 int area;
13634
13635 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13636 {
13637 char *s = (char *) alloca (row->used[area] + 1);
13638 int i;
13639
13640 for (i = 0; i < row->used[area]; ++i)
13641 {
13642 struct glyph *glyph = row->glyphs[area] + i;
13643 if (glyph->type == CHAR_GLYPH
13644 && glyph->u.ch < 0x80
13645 && glyph->u.ch >= ' ')
13646 s[i] = glyph->u.ch;
13647 else
13648 s[i] = '.';
13649 }
13650
13651 s[i] = '\0';
13652 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
13653 }
13654 }
13655 }
13656
13657
13658 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
13659 Sdump_glyph_matrix, 0, 1, "p",
13660 doc: /* Dump the current matrix of the selected window to stderr.
13661 Shows contents of glyph row structures. With non-nil
13662 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
13663 glyphs in short form, otherwise show glyphs in long form. */)
13664 (glyphs)
13665 Lisp_Object glyphs;
13666 {
13667 struct window *w = XWINDOW (selected_window);
13668 struct buffer *buffer = XBUFFER (w->buffer);
13669
13670 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
13671 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
13672 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
13673 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
13674 fprintf (stderr, "=============================================\n");
13675 dump_glyph_matrix (w->current_matrix,
13676 NILP (glyphs) ? 0 : XINT (glyphs));
13677 return Qnil;
13678 }
13679
13680
13681 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
13682 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
13683 ()
13684 {
13685 struct frame *f = XFRAME (selected_frame);
13686 dump_glyph_matrix (f->current_matrix, 1);
13687 return Qnil;
13688 }
13689
13690
13691 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
13692 doc: /* Dump glyph row ROW to stderr.
13693 GLYPH 0 means don't dump glyphs.
13694 GLYPH 1 means dump glyphs in short form.
13695 GLYPH > 1 or omitted means dump glyphs in long form. */)
13696 (row, glyphs)
13697 Lisp_Object row, glyphs;
13698 {
13699 struct glyph_matrix *matrix;
13700 int vpos;
13701
13702 CHECK_NUMBER (row);
13703 matrix = XWINDOW (selected_window)->current_matrix;
13704 vpos = XINT (row);
13705 if (vpos >= 0 && vpos < matrix->nrows)
13706 dump_glyph_row (MATRIX_ROW (matrix, vpos),
13707 vpos,
13708 INTEGERP (glyphs) ? XINT (glyphs) : 2);
13709 return Qnil;
13710 }
13711
13712
13713 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
13714 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
13715 GLYPH 0 means don't dump glyphs.
13716 GLYPH 1 means dump glyphs in short form.
13717 GLYPH > 1 or omitted means dump glyphs in long form. */)
13718 (row, glyphs)
13719 Lisp_Object row, glyphs;
13720 {
13721 struct frame *sf = SELECTED_FRAME ();
13722 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
13723 int vpos;
13724
13725 CHECK_NUMBER (row);
13726 vpos = XINT (row);
13727 if (vpos >= 0 && vpos < m->nrows)
13728 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
13729 INTEGERP (glyphs) ? XINT (glyphs) : 2);
13730 return Qnil;
13731 }
13732
13733
13734 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
13735 doc: /* Toggle tracing of redisplay.
13736 With ARG, turn tracing on if and only if ARG is positive. */)
13737 (arg)
13738 Lisp_Object arg;
13739 {
13740 if (NILP (arg))
13741 trace_redisplay_p = !trace_redisplay_p;
13742 else
13743 {
13744 arg = Fprefix_numeric_value (arg);
13745 trace_redisplay_p = XINT (arg) > 0;
13746 }
13747
13748 return Qnil;
13749 }
13750
13751
13752 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
13753 doc: /* Like `format', but print result to stderr.
13754 usage: (trace-to-stderr STRING &rest OBJECTS) */)
13755 (nargs, args)
13756 int nargs;
13757 Lisp_Object *args;
13758 {
13759 Lisp_Object s = Fformat (nargs, args);
13760 fprintf (stderr, "%s", SDATA (s));
13761 return Qnil;
13762 }
13763
13764 #endif /* GLYPH_DEBUG */
13765
13766
13767 \f
13768 /***********************************************************************
13769 Building Desired Matrix Rows
13770 ***********************************************************************/
13771
13772 /* Return a temporary glyph row holding the glyphs of an overlay
13773 arrow. Only used for non-window-redisplay windows. */
13774
13775 static struct glyph_row *
13776 get_overlay_arrow_glyph_row (w)
13777 struct window *w;
13778 {
13779 struct frame *f = XFRAME (WINDOW_FRAME (w));
13780 struct buffer *buffer = XBUFFER (w->buffer);
13781 struct buffer *old = current_buffer;
13782 const unsigned char *arrow_string = SDATA (Voverlay_arrow_string);
13783 int arrow_len = SCHARS (Voverlay_arrow_string);
13784 const unsigned char *arrow_end = arrow_string + arrow_len;
13785 const unsigned char *p;
13786 struct it it;
13787 int multibyte_p;
13788 int n_glyphs_before;
13789
13790 set_buffer_temp (buffer);
13791 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
13792 it.glyph_row->used[TEXT_AREA] = 0;
13793 SET_TEXT_POS (it.position, 0, 0);
13794
13795 multibyte_p = !NILP (buffer->enable_multibyte_characters);
13796 p = arrow_string;
13797 while (p < arrow_end)
13798 {
13799 Lisp_Object face, ilisp;
13800
13801 /* Get the next character. */
13802 if (multibyte_p)
13803 it.c = string_char_and_length (p, arrow_len, &it.len);
13804 else
13805 it.c = *p, it.len = 1;
13806 p += it.len;
13807
13808 /* Get its face. */
13809 ilisp = make_number (p - arrow_string);
13810 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
13811 it.face_id = compute_char_face (f, it.c, face);
13812
13813 /* Compute its width, get its glyphs. */
13814 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
13815 SET_TEXT_POS (it.position, -1, -1);
13816 PRODUCE_GLYPHS (&it);
13817
13818 /* If this character doesn't fit any more in the line, we have
13819 to remove some glyphs. */
13820 if (it.current_x > it.last_visible_x)
13821 {
13822 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
13823 break;
13824 }
13825 }
13826
13827 set_buffer_temp (old);
13828 return it.glyph_row;
13829 }
13830
13831
13832 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
13833 glyphs are only inserted for terminal frames since we can't really
13834 win with truncation glyphs when partially visible glyphs are
13835 involved. Which glyphs to insert is determined by
13836 produce_special_glyphs. */
13837
13838 static void
13839 insert_left_trunc_glyphs (it)
13840 struct it *it;
13841 {
13842 struct it truncate_it;
13843 struct glyph *from, *end, *to, *toend;
13844
13845 xassert (!FRAME_WINDOW_P (it->f));
13846
13847 /* Get the truncation glyphs. */
13848 truncate_it = *it;
13849 truncate_it.current_x = 0;
13850 truncate_it.face_id = DEFAULT_FACE_ID;
13851 truncate_it.glyph_row = &scratch_glyph_row;
13852 truncate_it.glyph_row->used[TEXT_AREA] = 0;
13853 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
13854 truncate_it.object = make_number (0);
13855 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
13856
13857 /* Overwrite glyphs from IT with truncation glyphs. */
13858 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
13859 end = from + truncate_it.glyph_row->used[TEXT_AREA];
13860 to = it->glyph_row->glyphs[TEXT_AREA];
13861 toend = to + it->glyph_row->used[TEXT_AREA];
13862
13863 while (from < end)
13864 *to++ = *from++;
13865
13866 /* There may be padding glyphs left over. Overwrite them too. */
13867 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
13868 {
13869 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
13870 while (from < end)
13871 *to++ = *from++;
13872 }
13873
13874 if (to > toend)
13875 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
13876 }
13877
13878
13879 /* Compute the pixel height and width of IT->glyph_row.
13880
13881 Most of the time, ascent and height of a display line will be equal
13882 to the max_ascent and max_height values of the display iterator
13883 structure. This is not the case if
13884
13885 1. We hit ZV without displaying anything. In this case, max_ascent
13886 and max_height will be zero.
13887
13888 2. We have some glyphs that don't contribute to the line height.
13889 (The glyph row flag contributes_to_line_height_p is for future
13890 pixmap extensions).
13891
13892 The first case is easily covered by using default values because in
13893 these cases, the line height does not really matter, except that it
13894 must not be zero. */
13895
13896 static void
13897 compute_line_metrics (it)
13898 struct it *it;
13899 {
13900 struct glyph_row *row = it->glyph_row;
13901 int area, i;
13902
13903 if (FRAME_WINDOW_P (it->f))
13904 {
13905 int i, min_y, max_y;
13906
13907 /* The line may consist of one space only, that was added to
13908 place the cursor on it. If so, the row's height hasn't been
13909 computed yet. */
13910 if (row->height == 0)
13911 {
13912 if (it->max_ascent + it->max_descent == 0)
13913 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
13914 row->ascent = it->max_ascent;
13915 row->height = it->max_ascent + it->max_descent;
13916 row->phys_ascent = it->max_phys_ascent;
13917 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
13918 }
13919
13920 /* Compute the width of this line. */
13921 row->pixel_width = row->x;
13922 for (i = 0; i < row->used[TEXT_AREA]; ++i)
13923 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
13924
13925 xassert (row->pixel_width >= 0);
13926 xassert (row->ascent >= 0 && row->height > 0);
13927
13928 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
13929 || MATRIX_ROW_OVERLAPS_PRED_P (row));
13930
13931 /* If first line's physical ascent is larger than its logical
13932 ascent, use the physical ascent, and make the row taller.
13933 This makes accented characters fully visible. */
13934 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
13935 && row->phys_ascent > row->ascent)
13936 {
13937 row->height += row->phys_ascent - row->ascent;
13938 row->ascent = row->phys_ascent;
13939 }
13940
13941 /* Compute how much of the line is visible. */
13942 row->visible_height = row->height;
13943
13944 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
13945 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
13946
13947 if (row->y < min_y)
13948 row->visible_height -= min_y - row->y;
13949 if (row->y + row->height > max_y)
13950 row->visible_height -= row->y + row->height - max_y;
13951 }
13952 else
13953 {
13954 row->pixel_width = row->used[TEXT_AREA];
13955 if (row->continued_p)
13956 row->pixel_width -= it->continuation_pixel_width;
13957 else if (row->truncated_on_right_p)
13958 row->pixel_width -= it->truncation_pixel_width;
13959 row->ascent = row->phys_ascent = 0;
13960 row->height = row->phys_height = row->visible_height = 1;
13961 }
13962
13963 /* Compute a hash code for this row. */
13964 row->hash = 0;
13965 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13966 for (i = 0; i < row->used[area]; ++i)
13967 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
13968 + row->glyphs[area][i].u.val
13969 + row->glyphs[area][i].face_id
13970 + row->glyphs[area][i].padding_p
13971 + (row->glyphs[area][i].type << 2));
13972
13973 it->max_ascent = it->max_descent = 0;
13974 it->max_phys_ascent = it->max_phys_descent = 0;
13975 }
13976
13977
13978 /* Append one space to the glyph row of iterator IT if doing a
13979 window-based redisplay. DEFAULT_FACE_P non-zero means let the
13980 space have the default face, otherwise let it have the same face as
13981 IT->face_id. Value is non-zero if a space was added.
13982
13983 This function is called to make sure that there is always one glyph
13984 at the end of a glyph row that the cursor can be set on under
13985 window-systems. (If there weren't such a glyph we would not know
13986 how wide and tall a box cursor should be displayed).
13987
13988 At the same time this space let's a nicely handle clearing to the
13989 end of the line if the row ends in italic text. */
13990
13991 static int
13992 append_space (it, default_face_p)
13993 struct it *it;
13994 int default_face_p;
13995 {
13996 if (FRAME_WINDOW_P (it->f))
13997 {
13998 int n = it->glyph_row->used[TEXT_AREA];
13999
14000 if (it->glyph_row->glyphs[TEXT_AREA] + n
14001 < it->glyph_row->glyphs[1 + TEXT_AREA])
14002 {
14003 /* Save some values that must not be changed.
14004 Must save IT->c and IT->len because otherwise
14005 ITERATOR_AT_END_P wouldn't work anymore after
14006 append_space has been called. */
14007 enum display_element_type saved_what = it->what;
14008 int saved_c = it->c, saved_len = it->len;
14009 int saved_x = it->current_x;
14010 int saved_face_id = it->face_id;
14011 struct text_pos saved_pos;
14012 Lisp_Object saved_object;
14013 struct face *face;
14014
14015 saved_object = it->object;
14016 saved_pos = it->position;
14017
14018 it->what = IT_CHARACTER;
14019 bzero (&it->position, sizeof it->position);
14020 it->object = make_number (0);
14021 it->c = ' ';
14022 it->len = 1;
14023
14024 if (default_face_p)
14025 it->face_id = DEFAULT_FACE_ID;
14026 else if (it->face_before_selective_p)
14027 it->face_id = it->saved_face_id;
14028 face = FACE_FROM_ID (it->f, it->face_id);
14029 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14030
14031 PRODUCE_GLYPHS (it);
14032
14033 it->current_x = saved_x;
14034 it->object = saved_object;
14035 it->position = saved_pos;
14036 it->what = saved_what;
14037 it->face_id = saved_face_id;
14038 it->len = saved_len;
14039 it->c = saved_c;
14040 return 1;
14041 }
14042 }
14043
14044 return 0;
14045 }
14046
14047
14048 /* Extend the face of the last glyph in the text area of IT->glyph_row
14049 to the end of the display line. Called from display_line.
14050 If the glyph row is empty, add a space glyph to it so that we
14051 know the face to draw. Set the glyph row flag fill_line_p. */
14052
14053 static void
14054 extend_face_to_end_of_line (it)
14055 struct it *it;
14056 {
14057 struct face *face;
14058 struct frame *f = it->f;
14059
14060 /* If line is already filled, do nothing. */
14061 if (it->current_x >= it->last_visible_x)
14062 return;
14063
14064 /* Face extension extends the background and box of IT->face_id
14065 to the end of the line. If the background equals the background
14066 of the frame, we don't have to do anything. */
14067 if (it->face_before_selective_p)
14068 face = FACE_FROM_ID (it->f, it->saved_face_id);
14069 else
14070 face = FACE_FROM_ID (f, it->face_id);
14071
14072 if (FRAME_WINDOW_P (f)
14073 && face->box == FACE_NO_BOX
14074 && face->background == FRAME_BACKGROUND_PIXEL (f)
14075 && !face->stipple)
14076 return;
14077
14078 /* Set the glyph row flag indicating that the face of the last glyph
14079 in the text area has to be drawn to the end of the text area. */
14080 it->glyph_row->fill_line_p = 1;
14081
14082 /* If current character of IT is not ASCII, make sure we have the
14083 ASCII face. This will be automatically undone the next time
14084 get_next_display_element returns a multibyte character. Note
14085 that the character will always be single byte in unibyte text. */
14086 if (!SINGLE_BYTE_CHAR_P (it->c))
14087 {
14088 it->face_id = FACE_FOR_CHAR (f, face, 0);
14089 }
14090
14091 if (FRAME_WINDOW_P (f))
14092 {
14093 /* If the row is empty, add a space with the current face of IT,
14094 so that we know which face to draw. */
14095 if (it->glyph_row->used[TEXT_AREA] == 0)
14096 {
14097 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14098 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14099 it->glyph_row->used[TEXT_AREA] = 1;
14100 }
14101 }
14102 else
14103 {
14104 /* Save some values that must not be changed. */
14105 int saved_x = it->current_x;
14106 struct text_pos saved_pos;
14107 Lisp_Object saved_object;
14108 enum display_element_type saved_what = it->what;
14109 int saved_face_id = it->face_id;
14110
14111 saved_object = it->object;
14112 saved_pos = it->position;
14113
14114 it->what = IT_CHARACTER;
14115 bzero (&it->position, sizeof it->position);
14116 it->object = make_number (0);
14117 it->c = ' ';
14118 it->len = 1;
14119 it->face_id = face->id;
14120
14121 PRODUCE_GLYPHS (it);
14122
14123 while (it->current_x <= it->last_visible_x)
14124 PRODUCE_GLYPHS (it);
14125
14126 /* Don't count these blanks really. It would let us insert a left
14127 truncation glyph below and make us set the cursor on them, maybe. */
14128 it->current_x = saved_x;
14129 it->object = saved_object;
14130 it->position = saved_pos;
14131 it->what = saved_what;
14132 it->face_id = saved_face_id;
14133 }
14134 }
14135
14136
14137 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14138 trailing whitespace. */
14139
14140 static int
14141 trailing_whitespace_p (charpos)
14142 int charpos;
14143 {
14144 int bytepos = CHAR_TO_BYTE (charpos);
14145 int c = 0;
14146
14147 while (bytepos < ZV_BYTE
14148 && (c = FETCH_CHAR (bytepos),
14149 c == ' ' || c == '\t'))
14150 ++bytepos;
14151
14152 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14153 {
14154 if (bytepos != PT_BYTE)
14155 return 1;
14156 }
14157 return 0;
14158 }
14159
14160
14161 /* Highlight trailing whitespace, if any, in ROW. */
14162
14163 void
14164 highlight_trailing_whitespace (f, row)
14165 struct frame *f;
14166 struct glyph_row *row;
14167 {
14168 int used = row->used[TEXT_AREA];
14169
14170 if (used)
14171 {
14172 struct glyph *start = row->glyphs[TEXT_AREA];
14173 struct glyph *glyph = start + used - 1;
14174
14175 /* Skip over glyphs inserted to display the cursor at the
14176 end of a line, for extending the face of the last glyph
14177 to the end of the line on terminals, and for truncation
14178 and continuation glyphs. */
14179 while (glyph >= start
14180 && glyph->type == CHAR_GLYPH
14181 && INTEGERP (glyph->object))
14182 --glyph;
14183
14184 /* If last glyph is a space or stretch, and it's trailing
14185 whitespace, set the face of all trailing whitespace glyphs in
14186 IT->glyph_row to `trailing-whitespace'. */
14187 if (glyph >= start
14188 && BUFFERP (glyph->object)
14189 && (glyph->type == STRETCH_GLYPH
14190 || (glyph->type == CHAR_GLYPH
14191 && glyph->u.ch == ' '))
14192 && trailing_whitespace_p (glyph->charpos))
14193 {
14194 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
14195
14196 while (glyph >= start
14197 && BUFFERP (glyph->object)
14198 && (glyph->type == STRETCH_GLYPH
14199 || (glyph->type == CHAR_GLYPH
14200 && glyph->u.ch == ' ')))
14201 (glyph--)->face_id = face_id;
14202 }
14203 }
14204 }
14205
14206
14207 /* Value is non-zero if glyph row ROW in window W should be
14208 used to hold the cursor. */
14209
14210 static int
14211 cursor_row_p (w, row)
14212 struct window *w;
14213 struct glyph_row *row;
14214 {
14215 int cursor_row_p = 1;
14216
14217 if (PT == MATRIX_ROW_END_CHARPOS (row))
14218 {
14219 /* If the row ends with a newline from a string, we don't want
14220 the cursor there (if the row is continued it doesn't end in a
14221 newline). */
14222 if (CHARPOS (row->end.string_pos) >= 0
14223 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14224 cursor_row_p = row->continued_p;
14225
14226 /* If the row ends at ZV, display the cursor at the end of that
14227 row instead of at the start of the row below. */
14228 else if (row->ends_at_zv_p)
14229 cursor_row_p = 1;
14230 else
14231 cursor_row_p = 0;
14232 }
14233
14234 return cursor_row_p;
14235 }
14236
14237
14238 /* Construct the glyph row IT->glyph_row in the desired matrix of
14239 IT->w from text at the current position of IT. See dispextern.h
14240 for an overview of struct it. Value is non-zero if
14241 IT->glyph_row displays text, as opposed to a line displaying ZV
14242 only. */
14243
14244 static int
14245 display_line (it)
14246 struct it *it;
14247 {
14248 struct glyph_row *row = it->glyph_row;
14249
14250 /* We always start displaying at hpos zero even if hscrolled. */
14251 xassert (it->hpos == 0 && it->current_x == 0);
14252
14253 /* We must not display in a row that's not a text row. */
14254 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14255 < it->w->desired_matrix->nrows);
14256
14257 /* Is IT->w showing the region? */
14258 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
14259
14260 /* Clear the result glyph row and enable it. */
14261 prepare_desired_row (row);
14262
14263 row->y = it->current_y;
14264 row->start = it->current;
14265 row->continuation_lines_width = it->continuation_lines_width;
14266 row->displays_text_p = 1;
14267 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14268 it->starts_in_middle_of_char_p = 0;
14269
14270 /* Arrange the overlays nicely for our purposes. Usually, we call
14271 display_line on only one line at a time, in which case this
14272 can't really hurt too much, or we call it on lines which appear
14273 one after another in the buffer, in which case all calls to
14274 recenter_overlay_lists but the first will be pretty cheap. */
14275 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14276
14277 /* Move over display elements that are not visible because we are
14278 hscrolled. This may stop at an x-position < IT->first_visible_x
14279 if the first glyph is partially visible or if we hit a line end. */
14280 if (it->current_x < it->first_visible_x)
14281 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14282 MOVE_TO_POS | MOVE_TO_X);
14283
14284 /* Get the initial row height. This is either the height of the
14285 text hscrolled, if there is any, or zero. */
14286 row->ascent = it->max_ascent;
14287 row->height = it->max_ascent + it->max_descent;
14288 row->phys_ascent = it->max_phys_ascent;
14289 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14290
14291 /* Loop generating characters. The loop is left with IT on the next
14292 character to display. */
14293 while (1)
14294 {
14295 int n_glyphs_before, hpos_before, x_before;
14296 int x, i, nglyphs;
14297 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
14298
14299 /* Retrieve the next thing to display. Value is zero if end of
14300 buffer reached. */
14301 if (!get_next_display_element (it))
14302 {
14303 /* Maybe add a space at the end of this line that is used to
14304 display the cursor there under X. Set the charpos of the
14305 first glyph of blank lines not corresponding to any text
14306 to -1. */
14307 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
14308 || row->used[TEXT_AREA] == 0)
14309 {
14310 row->glyphs[TEXT_AREA]->charpos = -1;
14311 row->displays_text_p = 0;
14312
14313 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
14314 && (!MINI_WINDOW_P (it->w)
14315 || (minibuf_level && EQ (it->window, minibuf_window))))
14316 row->indicate_empty_line_p = 1;
14317 }
14318
14319 it->continuation_lines_width = 0;
14320 row->ends_at_zv_p = 1;
14321 break;
14322 }
14323
14324 /* Now, get the metrics of what we want to display. This also
14325 generates glyphs in `row' (which is IT->glyph_row). */
14326 n_glyphs_before = row->used[TEXT_AREA];
14327 x = it->current_x;
14328
14329 /* Remember the line height so far in case the next element doesn't
14330 fit on the line. */
14331 if (!it->truncate_lines_p)
14332 {
14333 ascent = it->max_ascent;
14334 descent = it->max_descent;
14335 phys_ascent = it->max_phys_ascent;
14336 phys_descent = it->max_phys_descent;
14337 }
14338
14339 PRODUCE_GLYPHS (it);
14340
14341 /* If this display element was in marginal areas, continue with
14342 the next one. */
14343 if (it->area != TEXT_AREA)
14344 {
14345 row->ascent = max (row->ascent, it->max_ascent);
14346 row->height = max (row->height, it->max_ascent + it->max_descent);
14347 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14348 row->phys_height = max (row->phys_height,
14349 it->max_phys_ascent + it->max_phys_descent);
14350 set_iterator_to_next (it, 1);
14351 continue;
14352 }
14353
14354 /* Does the display element fit on the line? If we truncate
14355 lines, we should draw past the right edge of the window. If
14356 we don't truncate, we want to stop so that we can display the
14357 continuation glyph before the right margin. If lines are
14358 continued, there are two possible strategies for characters
14359 resulting in more than 1 glyph (e.g. tabs): Display as many
14360 glyphs as possible in this line and leave the rest for the
14361 continuation line, or display the whole element in the next
14362 line. Original redisplay did the former, so we do it also. */
14363 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14364 hpos_before = it->hpos;
14365 x_before = x;
14366
14367 if (/* Not a newline. */
14368 nglyphs > 0
14369 /* Glyphs produced fit entirely in the line. */
14370 && it->current_x < it->last_visible_x)
14371 {
14372 it->hpos += nglyphs;
14373 row->ascent = max (row->ascent, it->max_ascent);
14374 row->height = max (row->height, it->max_ascent + it->max_descent);
14375 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14376 row->phys_height = max (row->phys_height,
14377 it->max_phys_ascent + it->max_phys_descent);
14378 if (it->current_x - it->pixel_width < it->first_visible_x)
14379 row->x = x - it->first_visible_x;
14380 }
14381 else
14382 {
14383 int new_x;
14384 struct glyph *glyph;
14385
14386 for (i = 0; i < nglyphs; ++i, x = new_x)
14387 {
14388 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14389 new_x = x + glyph->pixel_width;
14390
14391 if (/* Lines are continued. */
14392 !it->truncate_lines_p
14393 && (/* Glyph doesn't fit on the line. */
14394 new_x > it->last_visible_x
14395 /* Or it fits exactly on a window system frame. */
14396 || (new_x == it->last_visible_x
14397 && FRAME_WINDOW_P (it->f))))
14398 {
14399 /* End of a continued line. */
14400
14401 if (it->hpos == 0
14402 || (new_x == it->last_visible_x
14403 && FRAME_WINDOW_P (it->f)))
14404 {
14405 /* Current glyph is the only one on the line or
14406 fits exactly on the line. We must continue
14407 the line because we can't draw the cursor
14408 after the glyph. */
14409 row->continued_p = 1;
14410 it->current_x = new_x;
14411 it->continuation_lines_width += new_x;
14412 ++it->hpos;
14413 if (i == nglyphs - 1)
14414 set_iterator_to_next (it, 1);
14415 }
14416 else if (CHAR_GLYPH_PADDING_P (*glyph)
14417 && !FRAME_WINDOW_P (it->f))
14418 {
14419 /* A padding glyph that doesn't fit on this line.
14420 This means the whole character doesn't fit
14421 on the line. */
14422 row->used[TEXT_AREA] = n_glyphs_before;
14423
14424 /* Fill the rest of the row with continuation
14425 glyphs like in 20.x. */
14426 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
14427 < row->glyphs[1 + TEXT_AREA])
14428 produce_special_glyphs (it, IT_CONTINUATION);
14429
14430 row->continued_p = 1;
14431 it->current_x = x_before;
14432 it->continuation_lines_width += x_before;
14433
14434 /* Restore the height to what it was before the
14435 element not fitting on the line. */
14436 it->max_ascent = ascent;
14437 it->max_descent = descent;
14438 it->max_phys_ascent = phys_ascent;
14439 it->max_phys_descent = phys_descent;
14440 }
14441 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
14442 {
14443 /* A TAB that extends past the right edge of the
14444 window. This produces a single glyph on
14445 window system frames. We leave the glyph in
14446 this row and let it fill the row, but don't
14447 consume the TAB. */
14448 it->continuation_lines_width += it->last_visible_x;
14449 row->ends_in_middle_of_char_p = 1;
14450 row->continued_p = 1;
14451 glyph->pixel_width = it->last_visible_x - x;
14452 it->starts_in_middle_of_char_p = 1;
14453 }
14454 else
14455 {
14456 /* Something other than a TAB that draws past
14457 the right edge of the window. Restore
14458 positions to values before the element. */
14459 row->used[TEXT_AREA] = n_glyphs_before + i;
14460
14461 /* Display continuation glyphs. */
14462 if (!FRAME_WINDOW_P (it->f))
14463 produce_special_glyphs (it, IT_CONTINUATION);
14464 row->continued_p = 1;
14465
14466 it->continuation_lines_width += x;
14467
14468 if (nglyphs > 1 && i > 0)
14469 {
14470 row->ends_in_middle_of_char_p = 1;
14471 it->starts_in_middle_of_char_p = 1;
14472 }
14473
14474 /* Restore the height to what it was before the
14475 element not fitting on the line. */
14476 it->max_ascent = ascent;
14477 it->max_descent = descent;
14478 it->max_phys_ascent = phys_ascent;
14479 it->max_phys_descent = phys_descent;
14480 }
14481
14482 break;
14483 }
14484 else if (new_x > it->first_visible_x)
14485 {
14486 /* Increment number of glyphs actually displayed. */
14487 ++it->hpos;
14488
14489 if (x < it->first_visible_x)
14490 /* Glyph is partially visible, i.e. row starts at
14491 negative X position. */
14492 row->x = x - it->first_visible_x;
14493 }
14494 else
14495 {
14496 /* Glyph is completely off the left margin of the
14497 window. This should not happen because of the
14498 move_it_in_display_line at the start of this
14499 function, unless the text display area of the
14500 window is empty. */
14501 xassert (it->first_visible_x <= it->last_visible_x);
14502 }
14503 }
14504
14505 row->ascent = max (row->ascent, it->max_ascent);
14506 row->height = max (row->height, it->max_ascent + it->max_descent);
14507 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14508 row->phys_height = max (row->phys_height,
14509 it->max_phys_ascent + it->max_phys_descent);
14510
14511 /* End of this display line if row is continued. */
14512 if (row->continued_p)
14513 break;
14514 }
14515
14516 /* Is this a line end? If yes, we're also done, after making
14517 sure that a non-default face is extended up to the right
14518 margin of the window. */
14519 if (ITERATOR_AT_END_OF_LINE_P (it))
14520 {
14521 int used_before = row->used[TEXT_AREA];
14522
14523 row->ends_in_newline_from_string_p = STRINGP (it->object);
14524
14525 /* Add a space at the end of the line that is used to
14526 display the cursor there. */
14527 append_space (it, 0);
14528
14529 /* Extend the face to the end of the line. */
14530 extend_face_to_end_of_line (it);
14531
14532 /* Make sure we have the position. */
14533 if (used_before == 0)
14534 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
14535
14536 /* Consume the line end. This skips over invisible lines. */
14537 set_iterator_to_next (it, 1);
14538 it->continuation_lines_width = 0;
14539 break;
14540 }
14541
14542 /* Proceed with next display element. Note that this skips
14543 over lines invisible because of selective display. */
14544 set_iterator_to_next (it, 1);
14545
14546 /* If we truncate lines, we are done when the last displayed
14547 glyphs reach past the right margin of the window. */
14548 if (it->truncate_lines_p
14549 && (FRAME_WINDOW_P (it->f)
14550 ? (it->current_x >= it->last_visible_x)
14551 : (it->current_x > it->last_visible_x)))
14552 {
14553 /* Maybe add truncation glyphs. */
14554 if (!FRAME_WINDOW_P (it->f))
14555 {
14556 int i, n;
14557
14558 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14559 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14560 break;
14561
14562 for (n = row->used[TEXT_AREA]; i < n; ++i)
14563 {
14564 row->used[TEXT_AREA] = i;
14565 produce_special_glyphs (it, IT_TRUNCATION);
14566 }
14567 }
14568
14569 row->truncated_on_right_p = 1;
14570 it->continuation_lines_width = 0;
14571 reseat_at_next_visible_line_start (it, 0);
14572 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
14573 it->hpos = hpos_before;
14574 it->current_x = x_before;
14575 break;
14576 }
14577 }
14578
14579 /* If line is not empty and hscrolled, maybe insert truncation glyphs
14580 at the left window margin. */
14581 if (it->first_visible_x
14582 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
14583 {
14584 if (!FRAME_WINDOW_P (it->f))
14585 insert_left_trunc_glyphs (it);
14586 row->truncated_on_left_p = 1;
14587 }
14588
14589 /* If the start of this line is the overlay arrow-position, then
14590 mark this glyph row as the one containing the overlay arrow.
14591 This is clearly a mess with variable size fonts. It would be
14592 better to let it be displayed like cursors under X. */
14593 if (MARKERP (Voverlay_arrow_position)
14594 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
14595 && (MATRIX_ROW_START_CHARPOS (row)
14596 == marker_position (Voverlay_arrow_position))
14597 && STRINGP (Voverlay_arrow_string)
14598 && ! overlay_arrow_seen)
14599 {
14600 /* Overlay arrow in window redisplay is a fringe bitmap. */
14601 if (!FRAME_WINDOW_P (it->f))
14602 {
14603 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
14604 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
14605 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
14606 struct glyph *p = row->glyphs[TEXT_AREA];
14607 struct glyph *p2, *end;
14608
14609 /* Copy the arrow glyphs. */
14610 while (glyph < arrow_end)
14611 *p++ = *glyph++;
14612
14613 /* Throw away padding glyphs. */
14614 p2 = p;
14615 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
14616 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
14617 ++p2;
14618 if (p2 > p)
14619 {
14620 while (p2 < end)
14621 *p++ = *p2++;
14622 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
14623 }
14624 }
14625
14626 overlay_arrow_seen = 1;
14627 row->overlay_arrow_p = 1;
14628 }
14629
14630 /* Compute pixel dimensions of this line. */
14631 compute_line_metrics (it);
14632
14633 /* Remember the position at which this line ends. */
14634 row->end = it->current;
14635
14636 /* Maybe set the cursor. */
14637 if (it->w->cursor.vpos < 0
14638 && PT >= MATRIX_ROW_START_CHARPOS (row)
14639 && PT <= MATRIX_ROW_END_CHARPOS (row)
14640 && cursor_row_p (it->w, row))
14641 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
14642
14643 /* Highlight trailing whitespace. */
14644 if (!NILP (Vshow_trailing_whitespace))
14645 highlight_trailing_whitespace (it->f, it->glyph_row);
14646
14647 /* Prepare for the next line. This line starts horizontally at (X
14648 HPOS) = (0 0). Vertical positions are incremented. As a
14649 convenience for the caller, IT->glyph_row is set to the next
14650 row to be used. */
14651 it->current_x = it->hpos = 0;
14652 it->current_y += row->height;
14653 ++it->vpos;
14654 ++it->glyph_row;
14655 return row->displays_text_p;
14656 }
14657
14658
14659 \f
14660 /***********************************************************************
14661 Menu Bar
14662 ***********************************************************************/
14663
14664 /* Redisplay the menu bar in the frame for window W.
14665
14666 The menu bar of X frames that don't have X toolkit support is
14667 displayed in a special window W->frame->menu_bar_window.
14668
14669 The menu bar of terminal frames is treated specially as far as
14670 glyph matrices are concerned. Menu bar lines are not part of
14671 windows, so the update is done directly on the frame matrix rows
14672 for the menu bar. */
14673
14674 static void
14675 display_menu_bar (w)
14676 struct window *w;
14677 {
14678 struct frame *f = XFRAME (WINDOW_FRAME (w));
14679 struct it it;
14680 Lisp_Object items;
14681 int i;
14682
14683 /* Don't do all this for graphical frames. */
14684 #ifdef HAVE_NTGUI
14685 if (!NILP (Vwindow_system))
14686 return;
14687 #endif
14688 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
14689 if (FRAME_X_P (f))
14690 return;
14691 #endif
14692 #ifdef MAC_OS
14693 if (FRAME_MAC_P (f))
14694 return;
14695 #endif
14696
14697 #ifdef USE_X_TOOLKIT
14698 xassert (!FRAME_WINDOW_P (f));
14699 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
14700 it.first_visible_x = 0;
14701 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
14702 #else /* not USE_X_TOOLKIT */
14703 if (FRAME_WINDOW_P (f))
14704 {
14705 /* Menu bar lines are displayed in the desired matrix of the
14706 dummy window menu_bar_window. */
14707 struct window *menu_w;
14708 xassert (WINDOWP (f->menu_bar_window));
14709 menu_w = XWINDOW (f->menu_bar_window);
14710 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
14711 MENU_FACE_ID);
14712 it.first_visible_x = 0;
14713 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
14714 }
14715 else
14716 {
14717 /* This is a TTY frame, i.e. character hpos/vpos are used as
14718 pixel x/y. */
14719 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
14720 MENU_FACE_ID);
14721 it.first_visible_x = 0;
14722 it.last_visible_x = FRAME_COLS (f);
14723 }
14724 #endif /* not USE_X_TOOLKIT */
14725
14726 if (! mode_line_inverse_video)
14727 /* Force the menu-bar to be displayed in the default face. */
14728 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
14729
14730 /* Clear all rows of the menu bar. */
14731 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
14732 {
14733 struct glyph_row *row = it.glyph_row + i;
14734 clear_glyph_row (row);
14735 row->enabled_p = 1;
14736 row->full_width_p = 1;
14737 }
14738
14739 /* Display all items of the menu bar. */
14740 items = FRAME_MENU_BAR_ITEMS (it.f);
14741 for (i = 0; i < XVECTOR (items)->size; i += 4)
14742 {
14743 Lisp_Object string;
14744
14745 /* Stop at nil string. */
14746 string = AREF (items, i + 1);
14747 if (NILP (string))
14748 break;
14749
14750 /* Remember where item was displayed. */
14751 AREF (items, i + 3) = make_number (it.hpos);
14752
14753 /* Display the item, pad with one space. */
14754 if (it.current_x < it.last_visible_x)
14755 display_string (NULL, string, Qnil, 0, 0, &it,
14756 SCHARS (string) + 1, 0, 0, -1);
14757 }
14758
14759 /* Fill out the line with spaces. */
14760 if (it.current_x < it.last_visible_x)
14761 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
14762
14763 /* Compute the total height of the lines. */
14764 compute_line_metrics (&it);
14765 }
14766
14767
14768 \f
14769 /***********************************************************************
14770 Mode Line
14771 ***********************************************************************/
14772
14773 /* Redisplay mode lines in the window tree whose root is WINDOW. If
14774 FORCE is non-zero, redisplay mode lines unconditionally.
14775 Otherwise, redisplay only mode lines that are garbaged. Value is
14776 the number of windows whose mode lines were redisplayed. */
14777
14778 static int
14779 redisplay_mode_lines (window, force)
14780 Lisp_Object window;
14781 int force;
14782 {
14783 int nwindows = 0;
14784
14785 while (!NILP (window))
14786 {
14787 struct window *w = XWINDOW (window);
14788
14789 if (WINDOWP (w->hchild))
14790 nwindows += redisplay_mode_lines (w->hchild, force);
14791 else if (WINDOWP (w->vchild))
14792 nwindows += redisplay_mode_lines (w->vchild, force);
14793 else if (force
14794 || FRAME_GARBAGED_P (XFRAME (w->frame))
14795 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
14796 {
14797 struct text_pos lpoint;
14798 struct buffer *old = current_buffer;
14799
14800 /* Set the window's buffer for the mode line display. */
14801 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14802 set_buffer_internal_1 (XBUFFER (w->buffer));
14803
14804 /* Point refers normally to the selected window. For any
14805 other window, set up appropriate value. */
14806 if (!EQ (window, selected_window))
14807 {
14808 struct text_pos pt;
14809
14810 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
14811 if (CHARPOS (pt) < BEGV)
14812 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
14813 else if (CHARPOS (pt) > (ZV - 1))
14814 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
14815 else
14816 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
14817 }
14818
14819 /* Display mode lines. */
14820 clear_glyph_matrix (w->desired_matrix);
14821 if (display_mode_lines (w))
14822 {
14823 ++nwindows;
14824 w->must_be_updated_p = 1;
14825 }
14826
14827 /* Restore old settings. */
14828 set_buffer_internal_1 (old);
14829 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14830 }
14831
14832 window = w->next;
14833 }
14834
14835 return nwindows;
14836 }
14837
14838
14839 /* Display the mode and/or top line of window W. Value is the number
14840 of mode lines displayed. */
14841
14842 static int
14843 display_mode_lines (w)
14844 struct window *w;
14845 {
14846 Lisp_Object old_selected_window, old_selected_frame;
14847 int n = 0;
14848
14849 old_selected_frame = selected_frame;
14850 selected_frame = w->frame;
14851 old_selected_window = selected_window;
14852 XSETWINDOW (selected_window, w);
14853
14854 /* These will be set while the mode line specs are processed. */
14855 line_number_displayed = 0;
14856 w->column_number_displayed = Qnil;
14857
14858 if (WINDOW_WANTS_MODELINE_P (w))
14859 {
14860 struct window *sel_w = XWINDOW (old_selected_window);
14861
14862 /* Select mode line face based on the real selected window. */
14863 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
14864 current_buffer->mode_line_format);
14865 ++n;
14866 }
14867
14868 if (WINDOW_WANTS_HEADER_LINE_P (w))
14869 {
14870 display_mode_line (w, HEADER_LINE_FACE_ID,
14871 current_buffer->header_line_format);
14872 ++n;
14873 }
14874
14875 selected_frame = old_selected_frame;
14876 selected_window = old_selected_window;
14877 return n;
14878 }
14879
14880
14881 /* Display mode or top line of window W. FACE_ID specifies which line
14882 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
14883 FORMAT is the mode line format to display. Value is the pixel
14884 height of the mode line displayed. */
14885
14886 static int
14887 display_mode_line (w, face_id, format)
14888 struct window *w;
14889 enum face_id face_id;
14890 Lisp_Object format;
14891 {
14892 struct it it;
14893 struct face *face;
14894
14895 init_iterator (&it, w, -1, -1, NULL, face_id);
14896 prepare_desired_row (it.glyph_row);
14897
14898 if (! mode_line_inverse_video)
14899 /* Force the mode-line to be displayed in the default face. */
14900 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
14901
14902 /* Temporarily make frame's keyboard the current kboard so that
14903 kboard-local variables in the mode_line_format will get the right
14904 values. */
14905 push_frame_kboard (it.f);
14906 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
14907 pop_frame_kboard ();
14908
14909 /* Fill up with spaces. */
14910 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
14911
14912 compute_line_metrics (&it);
14913 it.glyph_row->full_width_p = 1;
14914 it.glyph_row->mode_line_p = 1;
14915 it.glyph_row->continued_p = 0;
14916 it.glyph_row->truncated_on_left_p = 0;
14917 it.glyph_row->truncated_on_right_p = 0;
14918
14919 /* Make a 3D mode-line have a shadow at its right end. */
14920 face = FACE_FROM_ID (it.f, face_id);
14921 extend_face_to_end_of_line (&it);
14922 if (face->box != FACE_NO_BOX)
14923 {
14924 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
14925 + it.glyph_row->used[TEXT_AREA] - 1);
14926 last->right_box_line_p = 1;
14927 }
14928
14929 return it.glyph_row->height;
14930 }
14931
14932 /* Alist that caches the results of :propertize.
14933 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
14934 Lisp_Object mode_line_proptrans_alist;
14935
14936 /* List of strings making up the mode-line. */
14937 Lisp_Object mode_line_string_list;
14938
14939 /* Base face property when building propertized mode line string. */
14940 static Lisp_Object mode_line_string_face;
14941 static Lisp_Object mode_line_string_face_prop;
14942
14943
14944 /* Contribute ELT to the mode line for window IT->w. How it
14945 translates into text depends on its data type.
14946
14947 IT describes the display environment in which we display, as usual.
14948
14949 DEPTH is the depth in recursion. It is used to prevent
14950 infinite recursion here.
14951
14952 FIELD_WIDTH is the number of characters the display of ELT should
14953 occupy in the mode line, and PRECISION is the maximum number of
14954 characters to display from ELT's representation. See
14955 display_string for details.
14956
14957 Returns the hpos of the end of the text generated by ELT.
14958
14959 PROPS is a property list to add to any string we encounter.
14960
14961 If RISKY is nonzero, remove (disregard) any properties in any string
14962 we encounter, and ignore :eval and :propertize.
14963
14964 If the global variable `frame_title_ptr' is non-NULL, then the output
14965 is passed to `store_frame_title' instead of `display_string'. */
14966
14967 static int
14968 display_mode_element (it, depth, field_width, precision, elt, props, risky)
14969 struct it *it;
14970 int depth;
14971 int field_width, precision;
14972 Lisp_Object elt, props;
14973 int risky;
14974 {
14975 int n = 0, field, prec;
14976 int literal = 0;
14977
14978 tail_recurse:
14979 if (depth > 100)
14980 elt = build_string ("*too-deep*");
14981
14982 depth++;
14983
14984 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
14985 {
14986 case Lisp_String:
14987 {
14988 /* A string: output it and check for %-constructs within it. */
14989 unsigned char c;
14990 const unsigned char *this, *lisp_string;
14991
14992 if (!NILP (props) || risky)
14993 {
14994 Lisp_Object oprops, aelt;
14995 oprops = Ftext_properties_at (make_number (0), elt);
14996
14997 if (NILP (Fequal (props, oprops)) || risky)
14998 {
14999 /* If the starting string has properties,
15000 merge the specified ones onto the existing ones. */
15001 if (! NILP (oprops) && !risky)
15002 {
15003 Lisp_Object tem;
15004
15005 oprops = Fcopy_sequence (oprops);
15006 tem = props;
15007 while (CONSP (tem))
15008 {
15009 oprops = Fplist_put (oprops, XCAR (tem),
15010 XCAR (XCDR (tem)));
15011 tem = XCDR (XCDR (tem));
15012 }
15013 props = oprops;
15014 }
15015
15016 aelt = Fassoc (elt, mode_line_proptrans_alist);
15017 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15018 {
15019 mode_line_proptrans_alist
15020 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15021 elt = XCAR (aelt);
15022 }
15023 else
15024 {
15025 Lisp_Object tem;
15026
15027 elt = Fcopy_sequence (elt);
15028 Fset_text_properties (make_number (0), Flength (elt),
15029 props, elt);
15030 /* Add this item to mode_line_proptrans_alist. */
15031 mode_line_proptrans_alist
15032 = Fcons (Fcons (elt, props),
15033 mode_line_proptrans_alist);
15034 /* Truncate mode_line_proptrans_alist
15035 to at most 50 elements. */
15036 tem = Fnthcdr (make_number (50),
15037 mode_line_proptrans_alist);
15038 if (! NILP (tem))
15039 XSETCDR (tem, Qnil);
15040 }
15041 }
15042 }
15043
15044 this = SDATA (elt);
15045 lisp_string = this;
15046
15047 if (literal)
15048 {
15049 prec = precision - n;
15050 if (frame_title_ptr)
15051 n += store_frame_title (SDATA (elt), -1, prec);
15052 else if (!NILP (mode_line_string_list))
15053 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15054 else
15055 n += display_string (NULL, elt, Qnil, 0, 0, it,
15056 0, prec, 0, STRING_MULTIBYTE (elt));
15057
15058 break;
15059 }
15060
15061 while ((precision <= 0 || n < precision)
15062 && *this
15063 && (frame_title_ptr
15064 || !NILP (mode_line_string_list)
15065 || it->current_x < it->last_visible_x))
15066 {
15067 const unsigned char *last = this;
15068
15069 /* Advance to end of string or next format specifier. */
15070 while ((c = *this++) != '\0' && c != '%')
15071 ;
15072
15073 if (this - 1 != last)
15074 {
15075 /* Output to end of string or up to '%'. Field width
15076 is length of string. Don't output more than
15077 PRECISION allows us. */
15078 --this;
15079
15080 prec = chars_in_text (last, this - last);
15081 if (precision > 0 && prec > precision - n)
15082 prec = precision - n;
15083
15084 if (frame_title_ptr)
15085 n += store_frame_title (last, 0, prec);
15086 else if (!NILP (mode_line_string_list))
15087 {
15088 int bytepos = last - lisp_string;
15089 int charpos = string_byte_to_char (elt, bytepos);
15090 n += store_mode_line_string (NULL,
15091 Fsubstring (elt, make_number (charpos),
15092 make_number (charpos + prec)),
15093 0, 0, 0, Qnil);
15094 }
15095 else
15096 {
15097 int bytepos = last - lisp_string;
15098 int charpos = string_byte_to_char (elt, bytepos);
15099 n += display_string (NULL, elt, Qnil, 0, charpos,
15100 it, 0, prec, 0,
15101 STRING_MULTIBYTE (elt));
15102 }
15103 }
15104 else /* c == '%' */
15105 {
15106 const unsigned char *percent_position = this;
15107
15108 /* Get the specified minimum width. Zero means
15109 don't pad. */
15110 field = 0;
15111 while ((c = *this++) >= '0' && c <= '9')
15112 field = field * 10 + c - '0';
15113
15114 /* Don't pad beyond the total padding allowed. */
15115 if (field_width - n > 0 && field > field_width - n)
15116 field = field_width - n;
15117
15118 /* Note that either PRECISION <= 0 or N < PRECISION. */
15119 prec = precision - n;
15120
15121 if (c == 'M')
15122 n += display_mode_element (it, depth, field, prec,
15123 Vglobal_mode_string, props,
15124 risky);
15125 else if (c != 0)
15126 {
15127 int multibyte;
15128 int bytepos, charpos;
15129 unsigned char *spec;
15130
15131 bytepos = percent_position - lisp_string;
15132 charpos = (STRING_MULTIBYTE (elt)
15133 ? string_byte_to_char (elt, bytepos)
15134 : bytepos);
15135
15136 spec
15137 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15138
15139 if (frame_title_ptr)
15140 n += store_frame_title (spec, field, prec);
15141 else if (!NILP (mode_line_string_list))
15142 {
15143 int len = strlen (spec);
15144 Lisp_Object tem = make_string (spec, len);
15145 props = Ftext_properties_at (make_number (charpos), elt);
15146 /* Should only keep face property in props */
15147 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15148 }
15149 else
15150 {
15151 int nglyphs_before, nwritten;
15152
15153 nglyphs_before = it->glyph_row->used[TEXT_AREA];
15154 nwritten = display_string (spec, Qnil, elt,
15155 charpos, 0, it,
15156 field, prec, 0,
15157 multibyte);
15158
15159 /* Assign to the glyphs written above the
15160 string where the `%x' came from, position
15161 of the `%'. */
15162 if (nwritten > 0)
15163 {
15164 struct glyph *glyph
15165 = (it->glyph_row->glyphs[TEXT_AREA]
15166 + nglyphs_before);
15167 int i;
15168
15169 for (i = 0; i < nwritten; ++i)
15170 {
15171 glyph[i].object = elt;
15172 glyph[i].charpos = charpos;
15173 }
15174
15175 n += nwritten;
15176 }
15177 }
15178 }
15179 else /* c == 0 */
15180 break;
15181 }
15182 }
15183 }
15184 break;
15185
15186 case Lisp_Symbol:
15187 /* A symbol: process the value of the symbol recursively
15188 as if it appeared here directly. Avoid error if symbol void.
15189 Special case: if value of symbol is a string, output the string
15190 literally. */
15191 {
15192 register Lisp_Object tem;
15193
15194 /* If the variable is not marked as risky to set
15195 then its contents are risky to use. */
15196 if (NILP (Fget (elt, Qrisky_local_variable)))
15197 risky = 1;
15198
15199 tem = Fboundp (elt);
15200 if (!NILP (tem))
15201 {
15202 tem = Fsymbol_value (elt);
15203 /* If value is a string, output that string literally:
15204 don't check for % within it. */
15205 if (STRINGP (tem))
15206 literal = 1;
15207
15208 if (!EQ (tem, elt))
15209 {
15210 /* Give up right away for nil or t. */
15211 elt = tem;
15212 goto tail_recurse;
15213 }
15214 }
15215 }
15216 break;
15217
15218 case Lisp_Cons:
15219 {
15220 register Lisp_Object car, tem;
15221
15222 /* A cons cell: five distinct cases.
15223 If first element is :eval or :propertize, do something special.
15224 If first element is a string or a cons, process all the elements
15225 and effectively concatenate them.
15226 If first element is a negative number, truncate displaying cdr to
15227 at most that many characters. If positive, pad (with spaces)
15228 to at least that many characters.
15229 If first element is a symbol, process the cadr or caddr recursively
15230 according to whether the symbol's value is non-nil or nil. */
15231 car = XCAR (elt);
15232 if (EQ (car, QCeval))
15233 {
15234 /* An element of the form (:eval FORM) means evaluate FORM
15235 and use the result as mode line elements. */
15236
15237 if (risky)
15238 break;
15239
15240 if (CONSP (XCDR (elt)))
15241 {
15242 Lisp_Object spec;
15243 spec = safe_eval (XCAR (XCDR (elt)));
15244 n += display_mode_element (it, depth, field_width - n,
15245 precision - n, spec, props,
15246 risky);
15247 }
15248 }
15249 else if (EQ (car, QCpropertize))
15250 {
15251 /* An element of the form (:propertize ELT PROPS...)
15252 means display ELT but applying properties PROPS. */
15253
15254 if (risky)
15255 break;
15256
15257 if (CONSP (XCDR (elt)))
15258 n += display_mode_element (it, depth, field_width - n,
15259 precision - n, XCAR (XCDR (elt)),
15260 XCDR (XCDR (elt)), risky);
15261 }
15262 else if (SYMBOLP (car))
15263 {
15264 tem = Fboundp (car);
15265 elt = XCDR (elt);
15266 if (!CONSP (elt))
15267 goto invalid;
15268 /* elt is now the cdr, and we know it is a cons cell.
15269 Use its car if CAR has a non-nil value. */
15270 if (!NILP (tem))
15271 {
15272 tem = Fsymbol_value (car);
15273 if (!NILP (tem))
15274 {
15275 elt = XCAR (elt);
15276 goto tail_recurse;
15277 }
15278 }
15279 /* Symbol's value is nil (or symbol is unbound)
15280 Get the cddr of the original list
15281 and if possible find the caddr and use that. */
15282 elt = XCDR (elt);
15283 if (NILP (elt))
15284 break;
15285 else if (!CONSP (elt))
15286 goto invalid;
15287 elt = XCAR (elt);
15288 goto tail_recurse;
15289 }
15290 else if (INTEGERP (car))
15291 {
15292 register int lim = XINT (car);
15293 elt = XCDR (elt);
15294 if (lim < 0)
15295 {
15296 /* Negative int means reduce maximum width. */
15297 if (precision <= 0)
15298 precision = -lim;
15299 else
15300 precision = min (precision, -lim);
15301 }
15302 else if (lim > 0)
15303 {
15304 /* Padding specified. Don't let it be more than
15305 current maximum. */
15306 if (precision > 0)
15307 lim = min (precision, lim);
15308
15309 /* If that's more padding than already wanted, queue it.
15310 But don't reduce padding already specified even if
15311 that is beyond the current truncation point. */
15312 field_width = max (lim, field_width);
15313 }
15314 goto tail_recurse;
15315 }
15316 else if (STRINGP (car) || CONSP (car))
15317 {
15318 register int limit = 50;
15319 /* Limit is to protect against circular lists. */
15320 while (CONSP (elt)
15321 && --limit > 0
15322 && (precision <= 0 || n < precision))
15323 {
15324 n += display_mode_element (it, depth, field_width - n,
15325 precision - n, XCAR (elt),
15326 props, risky);
15327 elt = XCDR (elt);
15328 }
15329 }
15330 }
15331 break;
15332
15333 default:
15334 invalid:
15335 elt = build_string ("*invalid*");
15336 goto tail_recurse;
15337 }
15338
15339 /* Pad to FIELD_WIDTH. */
15340 if (field_width > 0 && n < field_width)
15341 {
15342 if (frame_title_ptr)
15343 n += store_frame_title ("", field_width - n, 0);
15344 else if (!NILP (mode_line_string_list))
15345 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
15346 else
15347 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
15348 0, 0, 0);
15349 }
15350
15351 return n;
15352 }
15353
15354 /* Store a mode-line string element in mode_line_string_list.
15355
15356 If STRING is non-null, display that C string. Otherwise, the Lisp
15357 string LISP_STRING is displayed.
15358
15359 FIELD_WIDTH is the minimum number of output glyphs to produce.
15360 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15361 with spaces. FIELD_WIDTH <= 0 means don't pad.
15362
15363 PRECISION is the maximum number of characters to output from
15364 STRING. PRECISION <= 0 means don't truncate the string.
15365
15366 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
15367 properties to the string.
15368
15369 PROPS are the properties to add to the string.
15370 The mode_line_string_face face property is always added to the string.
15371 */
15372
15373 static int store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
15374 char *string;
15375 Lisp_Object lisp_string;
15376 int copy_string;
15377 int field_width;
15378 int precision;
15379 Lisp_Object props;
15380 {
15381 int len;
15382 int n = 0;
15383
15384 if (string != NULL)
15385 {
15386 len = strlen (string);
15387 if (precision > 0 && len > precision)
15388 len = precision;
15389 lisp_string = make_string (string, len);
15390 if (NILP (props))
15391 props = mode_line_string_face_prop;
15392 else if (!NILP (mode_line_string_face))
15393 {
15394 Lisp_Object face = Fplist_get (props, Qface);
15395 props = Fcopy_sequence (props);
15396 if (NILP (face))
15397 face = mode_line_string_face;
15398 else
15399 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15400 props = Fplist_put (props, Qface, face);
15401 }
15402 Fadd_text_properties (make_number (0), make_number (len),
15403 props, lisp_string);
15404 }
15405 else
15406 {
15407 len = XFASTINT (Flength (lisp_string));
15408 if (precision > 0 && len > precision)
15409 {
15410 len = precision;
15411 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
15412 precision = -1;
15413 }
15414 if (!NILP (mode_line_string_face))
15415 {
15416 Lisp_Object face;
15417 if (NILP (props))
15418 props = Ftext_properties_at (make_number (0), lisp_string);
15419 face = Fplist_get (props, Qface);
15420 if (NILP (face))
15421 face = mode_line_string_face;
15422 else
15423 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15424 props = Fcons (Qface, Fcons (face, Qnil));
15425 if (copy_string)
15426 lisp_string = Fcopy_sequence (lisp_string);
15427 }
15428 if (!NILP (props))
15429 Fadd_text_properties (make_number (0), make_number (len),
15430 props, lisp_string);
15431 }
15432
15433 if (len > 0)
15434 {
15435 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15436 n += len;
15437 }
15438
15439 if (field_width > len)
15440 {
15441 field_width -= len;
15442 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
15443 if (!NILP (props))
15444 Fadd_text_properties (make_number (0), make_number (field_width),
15445 props, lisp_string);
15446 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15447 n += field_width;
15448 }
15449
15450 return n;
15451 }
15452
15453
15454 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
15455 0, 3, 0,
15456 doc: /* Return the mode-line of selected window as a string.
15457 First optional arg FORMAT specifies a different format string (see
15458 `mode-line-format' for details) to use. If FORMAT is t, return
15459 the buffer's header-line. Second optional arg WINDOW specifies a
15460 different window to use as the context for the formatting.
15461 If third optional arg NO-PROPS is non-nil, string is not propertized. */)
15462 (format, window, no_props)
15463 Lisp_Object format, window, no_props;
15464 {
15465 struct it it;
15466 int len;
15467 struct window *w;
15468 struct buffer *old_buffer = NULL;
15469 enum face_id face_id = DEFAULT_FACE_ID;
15470
15471 if (NILP (window))
15472 window = selected_window;
15473 CHECK_WINDOW (window);
15474 w = XWINDOW (window);
15475 CHECK_BUFFER (w->buffer);
15476
15477 if (XBUFFER (w->buffer) != current_buffer)
15478 {
15479 old_buffer = current_buffer;
15480 set_buffer_internal_1 (XBUFFER (w->buffer));
15481 }
15482
15483 if (NILP (format) || EQ (format, Qt))
15484 {
15485 face_id = NILP (format)
15486 ? CURRENT_MODE_LINE_FACE_ID (w) :
15487 HEADER_LINE_FACE_ID;
15488 format = NILP (format)
15489 ? current_buffer->mode_line_format
15490 : current_buffer->header_line_format;
15491 }
15492
15493 init_iterator (&it, w, -1, -1, NULL, face_id);
15494
15495 if (NILP (no_props))
15496 {
15497 mode_line_string_face =
15498 (face_id == MODE_LINE_FACE_ID ? Qmode_line :
15499 face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive :
15500 face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
15501
15502 mode_line_string_face_prop =
15503 NILP (mode_line_string_face) ? Qnil :
15504 Fcons (Qface, Fcons (mode_line_string_face, Qnil));
15505
15506 /* We need a dummy last element in mode_line_string_list to
15507 indicate we are building the propertized mode-line string.
15508 Using mode_line_string_face_prop here GC protects it. */
15509 mode_line_string_list =
15510 Fcons (mode_line_string_face_prop, Qnil);
15511 frame_title_ptr = NULL;
15512 }
15513 else
15514 {
15515 mode_line_string_face_prop = Qnil;
15516 mode_line_string_list = Qnil;
15517 frame_title_ptr = frame_title_buf;
15518 }
15519
15520 push_frame_kboard (it.f);
15521 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15522 pop_frame_kboard ();
15523
15524 if (old_buffer)
15525 set_buffer_internal_1 (old_buffer);
15526
15527 if (NILP (no_props))
15528 {
15529 Lisp_Object str;
15530 mode_line_string_list = Fnreverse (mode_line_string_list);
15531 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
15532 make_string ("", 0));
15533 mode_line_string_face_prop = Qnil;
15534 mode_line_string_list = Qnil;
15535 return str;
15536 }
15537
15538 len = frame_title_ptr - frame_title_buf;
15539 if (len > 0 && frame_title_ptr[-1] == '-')
15540 {
15541 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
15542 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
15543 ;
15544 frame_title_ptr += 3; /* restore last non-dash + two dashes */
15545 if (len > frame_title_ptr - frame_title_buf)
15546 len = frame_title_ptr - frame_title_buf;
15547 }
15548
15549 frame_title_ptr = NULL;
15550 return make_string (frame_title_buf, len);
15551 }
15552
15553 /* Write a null-terminated, right justified decimal representation of
15554 the positive integer D to BUF using a minimal field width WIDTH. */
15555
15556 static void
15557 pint2str (buf, width, d)
15558 register char *buf;
15559 register int width;
15560 register int d;
15561 {
15562 register char *p = buf;
15563
15564 if (d <= 0)
15565 *p++ = '0';
15566 else
15567 {
15568 while (d > 0)
15569 {
15570 *p++ = d % 10 + '0';
15571 d /= 10;
15572 }
15573 }
15574
15575 for (width -= (int) (p - buf); width > 0; --width)
15576 *p++ = ' ';
15577 *p-- = '\0';
15578 while (p > buf)
15579 {
15580 d = *buf;
15581 *buf++ = *p;
15582 *p-- = d;
15583 }
15584 }
15585
15586 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
15587 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
15588 type of CODING_SYSTEM. Return updated pointer into BUF. */
15589
15590 static unsigned char invalid_eol_type[] = "(*invalid*)";
15591
15592 static char *
15593 decode_mode_spec_coding (coding_system, buf, eol_flag)
15594 Lisp_Object coding_system;
15595 register char *buf;
15596 int eol_flag;
15597 {
15598 Lisp_Object val;
15599 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
15600 const unsigned char *eol_str;
15601 int eol_str_len;
15602 /* The EOL conversion we are using. */
15603 Lisp_Object eoltype;
15604
15605 val = Fget (coding_system, Qcoding_system);
15606 eoltype = Qnil;
15607
15608 if (!VECTORP (val)) /* Not yet decided. */
15609 {
15610 if (multibyte)
15611 *buf++ = '-';
15612 if (eol_flag)
15613 eoltype = eol_mnemonic_undecided;
15614 /* Don't mention EOL conversion if it isn't decided. */
15615 }
15616 else
15617 {
15618 Lisp_Object eolvalue;
15619
15620 eolvalue = Fget (coding_system, Qeol_type);
15621
15622 if (multibyte)
15623 *buf++ = XFASTINT (AREF (val, 1));
15624
15625 if (eol_flag)
15626 {
15627 /* The EOL conversion that is normal on this system. */
15628
15629 if (NILP (eolvalue)) /* Not yet decided. */
15630 eoltype = eol_mnemonic_undecided;
15631 else if (VECTORP (eolvalue)) /* Not yet decided. */
15632 eoltype = eol_mnemonic_undecided;
15633 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
15634 eoltype = (XFASTINT (eolvalue) == 0
15635 ? eol_mnemonic_unix
15636 : (XFASTINT (eolvalue) == 1
15637 ? eol_mnemonic_dos : eol_mnemonic_mac));
15638 }
15639 }
15640
15641 if (eol_flag)
15642 {
15643 /* Mention the EOL conversion if it is not the usual one. */
15644 if (STRINGP (eoltype))
15645 {
15646 eol_str = SDATA (eoltype);
15647 eol_str_len = SBYTES (eoltype);
15648 }
15649 else if (INTEGERP (eoltype)
15650 && CHAR_VALID_P (XINT (eoltype), 0))
15651 {
15652 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
15653 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
15654 eol_str = tmp;
15655 }
15656 else
15657 {
15658 eol_str = invalid_eol_type;
15659 eol_str_len = sizeof (invalid_eol_type) - 1;
15660 }
15661 bcopy (eol_str, buf, eol_str_len);
15662 buf += eol_str_len;
15663 }
15664
15665 return buf;
15666 }
15667
15668 /* Return a string for the output of a mode line %-spec for window W,
15669 generated by character C. PRECISION >= 0 means don't return a
15670 string longer than that value. FIELD_WIDTH > 0 means pad the
15671 string returned with spaces to that value. Return 1 in *MULTIBYTE
15672 if the result is multibyte text. */
15673
15674 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
15675
15676 static char *
15677 decode_mode_spec (w, c, field_width, precision, multibyte)
15678 struct window *w;
15679 register int c;
15680 int field_width, precision;
15681 int *multibyte;
15682 {
15683 Lisp_Object obj;
15684 struct frame *f = XFRAME (WINDOW_FRAME (w));
15685 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
15686 struct buffer *b = XBUFFER (w->buffer);
15687
15688 obj = Qnil;
15689 *multibyte = 0;
15690
15691 switch (c)
15692 {
15693 case '*':
15694 if (!NILP (b->read_only))
15695 return "%";
15696 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
15697 return "*";
15698 return "-";
15699
15700 case '+':
15701 /* This differs from %* only for a modified read-only buffer. */
15702 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
15703 return "*";
15704 if (!NILP (b->read_only))
15705 return "%";
15706 return "-";
15707
15708 case '&':
15709 /* This differs from %* in ignoring read-only-ness. */
15710 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
15711 return "*";
15712 return "-";
15713
15714 case '%':
15715 return "%";
15716
15717 case '[':
15718 {
15719 int i;
15720 char *p;
15721
15722 if (command_loop_level > 5)
15723 return "[[[... ";
15724 p = decode_mode_spec_buf;
15725 for (i = 0; i < command_loop_level; i++)
15726 *p++ = '[';
15727 *p = 0;
15728 return decode_mode_spec_buf;
15729 }
15730
15731 case ']':
15732 {
15733 int i;
15734 char *p;
15735
15736 if (command_loop_level > 5)
15737 return " ...]]]";
15738 p = decode_mode_spec_buf;
15739 for (i = 0; i < command_loop_level; i++)
15740 *p++ = ']';
15741 *p = 0;
15742 return decode_mode_spec_buf;
15743 }
15744
15745 case '-':
15746 {
15747 register int i;
15748
15749 /* Let lots_of_dashes be a string of infinite length. */
15750 if (!NILP (mode_line_string_list))
15751 return "--";
15752 if (field_width <= 0
15753 || field_width > sizeof (lots_of_dashes))
15754 {
15755 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
15756 decode_mode_spec_buf[i] = '-';
15757 decode_mode_spec_buf[i] = '\0';
15758 return decode_mode_spec_buf;
15759 }
15760 else
15761 return lots_of_dashes;
15762 }
15763
15764 case 'b':
15765 obj = b->name;
15766 break;
15767
15768 case 'c':
15769 {
15770 int col = (int) current_column (); /* iftc */
15771 w->column_number_displayed = make_number (col);
15772 pint2str (decode_mode_spec_buf, field_width, col);
15773 return decode_mode_spec_buf;
15774 }
15775
15776 case 'F':
15777 /* %F displays the frame name. */
15778 if (!NILP (f->title))
15779 return (char *) SDATA (f->title);
15780 if (f->explicit_name || ! FRAME_WINDOW_P (f))
15781 return (char *) SDATA (f->name);
15782 return "Emacs";
15783
15784 case 'f':
15785 obj = b->filename;
15786 break;
15787
15788 case 'l':
15789 {
15790 int startpos = XMARKER (w->start)->charpos;
15791 int startpos_byte = marker_byte_position (w->start);
15792 int line, linepos, linepos_byte, topline;
15793 int nlines, junk;
15794 int height = WINDOW_TOTAL_LINES (w);
15795
15796 /* If we decided that this buffer isn't suitable for line numbers,
15797 don't forget that too fast. */
15798 if (EQ (w->base_line_pos, w->buffer))
15799 goto no_value;
15800 /* But do forget it, if the window shows a different buffer now. */
15801 else if (BUFFERP (w->base_line_pos))
15802 w->base_line_pos = Qnil;
15803
15804 /* If the buffer is very big, don't waste time. */
15805 if (INTEGERP (Vline_number_display_limit)
15806 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
15807 {
15808 w->base_line_pos = Qnil;
15809 w->base_line_number = Qnil;
15810 goto no_value;
15811 }
15812
15813 if (!NILP (w->base_line_number)
15814 && !NILP (w->base_line_pos)
15815 && XFASTINT (w->base_line_pos) <= startpos)
15816 {
15817 line = XFASTINT (w->base_line_number);
15818 linepos = XFASTINT (w->base_line_pos);
15819 linepos_byte = buf_charpos_to_bytepos (b, linepos);
15820 }
15821 else
15822 {
15823 line = 1;
15824 linepos = BUF_BEGV (b);
15825 linepos_byte = BUF_BEGV_BYTE (b);
15826 }
15827
15828 /* Count lines from base line to window start position. */
15829 nlines = display_count_lines (linepos, linepos_byte,
15830 startpos_byte,
15831 startpos, &junk);
15832
15833 topline = nlines + line;
15834
15835 /* Determine a new base line, if the old one is too close
15836 or too far away, or if we did not have one.
15837 "Too close" means it's plausible a scroll-down would
15838 go back past it. */
15839 if (startpos == BUF_BEGV (b))
15840 {
15841 w->base_line_number = make_number (topline);
15842 w->base_line_pos = make_number (BUF_BEGV (b));
15843 }
15844 else if (nlines < height + 25 || nlines > height * 3 + 50
15845 || linepos == BUF_BEGV (b))
15846 {
15847 int limit = BUF_BEGV (b);
15848 int limit_byte = BUF_BEGV_BYTE (b);
15849 int position;
15850 int distance = (height * 2 + 30) * line_number_display_limit_width;
15851
15852 if (startpos - distance > limit)
15853 {
15854 limit = startpos - distance;
15855 limit_byte = CHAR_TO_BYTE (limit);
15856 }
15857
15858 nlines = display_count_lines (startpos, startpos_byte,
15859 limit_byte,
15860 - (height * 2 + 30),
15861 &position);
15862 /* If we couldn't find the lines we wanted within
15863 line_number_display_limit_width chars per line,
15864 give up on line numbers for this window. */
15865 if (position == limit_byte && limit == startpos - distance)
15866 {
15867 w->base_line_pos = w->buffer;
15868 w->base_line_number = Qnil;
15869 goto no_value;
15870 }
15871
15872 w->base_line_number = make_number (topline - nlines);
15873 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
15874 }
15875
15876 /* Now count lines from the start pos to point. */
15877 nlines = display_count_lines (startpos, startpos_byte,
15878 PT_BYTE, PT, &junk);
15879
15880 /* Record that we did display the line number. */
15881 line_number_displayed = 1;
15882
15883 /* Make the string to show. */
15884 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
15885 return decode_mode_spec_buf;
15886 no_value:
15887 {
15888 char* p = decode_mode_spec_buf;
15889 int pad = field_width - 2;
15890 while (pad-- > 0)
15891 *p++ = ' ';
15892 *p++ = '?';
15893 *p++ = '?';
15894 *p = '\0';
15895 return decode_mode_spec_buf;
15896 }
15897 }
15898 break;
15899
15900 case 'm':
15901 obj = b->mode_name;
15902 break;
15903
15904 case 'n':
15905 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
15906 return " Narrow";
15907 break;
15908
15909 case 'p':
15910 {
15911 int pos = marker_position (w->start);
15912 int total = BUF_ZV (b) - BUF_BEGV (b);
15913
15914 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
15915 {
15916 if (pos <= BUF_BEGV (b))
15917 return "All";
15918 else
15919 return "Bottom";
15920 }
15921 else if (pos <= BUF_BEGV (b))
15922 return "Top";
15923 else
15924 {
15925 if (total > 1000000)
15926 /* Do it differently for a large value, to avoid overflow. */
15927 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
15928 else
15929 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
15930 /* We can't normally display a 3-digit number,
15931 so get us a 2-digit number that is close. */
15932 if (total == 100)
15933 total = 99;
15934 sprintf (decode_mode_spec_buf, "%2d%%", total);
15935 return decode_mode_spec_buf;
15936 }
15937 }
15938
15939 /* Display percentage of size above the bottom of the screen. */
15940 case 'P':
15941 {
15942 int toppos = marker_position (w->start);
15943 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
15944 int total = BUF_ZV (b) - BUF_BEGV (b);
15945
15946 if (botpos >= BUF_ZV (b))
15947 {
15948 if (toppos <= BUF_BEGV (b))
15949 return "All";
15950 else
15951 return "Bottom";
15952 }
15953 else
15954 {
15955 if (total > 1000000)
15956 /* Do it differently for a large value, to avoid overflow. */
15957 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
15958 else
15959 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
15960 /* We can't normally display a 3-digit number,
15961 so get us a 2-digit number that is close. */
15962 if (total == 100)
15963 total = 99;
15964 if (toppos <= BUF_BEGV (b))
15965 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
15966 else
15967 sprintf (decode_mode_spec_buf, "%2d%%", total);
15968 return decode_mode_spec_buf;
15969 }
15970 }
15971
15972 case 's':
15973 /* status of process */
15974 obj = Fget_buffer_process (w->buffer);
15975 if (NILP (obj))
15976 return "no process";
15977 #ifdef subprocesses
15978 obj = Fsymbol_name (Fprocess_status (obj));
15979 #endif
15980 break;
15981
15982 case 't': /* indicate TEXT or BINARY */
15983 #ifdef MODE_LINE_BINARY_TEXT
15984 return MODE_LINE_BINARY_TEXT (b);
15985 #else
15986 return "T";
15987 #endif
15988
15989 case 'z':
15990 /* coding-system (not including end-of-line format) */
15991 case 'Z':
15992 /* coding-system (including end-of-line type) */
15993 {
15994 int eol_flag = (c == 'Z');
15995 char *p = decode_mode_spec_buf;
15996
15997 if (! FRAME_WINDOW_P (f))
15998 {
15999 /* No need to mention EOL here--the terminal never needs
16000 to do EOL conversion. */
16001 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
16002 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
16003 }
16004 p = decode_mode_spec_coding (b->buffer_file_coding_system,
16005 p, eol_flag);
16006
16007 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
16008 #ifdef subprocesses
16009 obj = Fget_buffer_process (Fcurrent_buffer ());
16010 if (PROCESSP (obj))
16011 {
16012 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16013 p, eol_flag);
16014 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16015 p, eol_flag);
16016 }
16017 #endif /* subprocesses */
16018 #endif /* 0 */
16019 *p = 0;
16020 return decode_mode_spec_buf;
16021 }
16022 }
16023
16024 if (STRINGP (obj))
16025 {
16026 *multibyte = STRING_MULTIBYTE (obj);
16027 return (char *) SDATA (obj);
16028 }
16029 else
16030 return "";
16031 }
16032
16033
16034 /* Count up to COUNT lines starting from START / START_BYTE.
16035 But don't go beyond LIMIT_BYTE.
16036 Return the number of lines thus found (always nonnegative).
16037
16038 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
16039
16040 static int
16041 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16042 int start, start_byte, limit_byte, count;
16043 int *byte_pos_ptr;
16044 {
16045 register unsigned char *cursor;
16046 unsigned char *base;
16047
16048 register int ceiling;
16049 register unsigned char *ceiling_addr;
16050 int orig_count = count;
16051
16052 /* If we are not in selective display mode,
16053 check only for newlines. */
16054 int selective_display = (!NILP (current_buffer->selective_display)
16055 && !INTEGERP (current_buffer->selective_display));
16056
16057 if (count > 0)
16058 {
16059 while (start_byte < limit_byte)
16060 {
16061 ceiling = BUFFER_CEILING_OF (start_byte);
16062 ceiling = min (limit_byte - 1, ceiling);
16063 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16064 base = (cursor = BYTE_POS_ADDR (start_byte));
16065 while (1)
16066 {
16067 if (selective_display)
16068 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16069 ;
16070 else
16071 while (*cursor != '\n' && ++cursor != ceiling_addr)
16072 ;
16073
16074 if (cursor != ceiling_addr)
16075 {
16076 if (--count == 0)
16077 {
16078 start_byte += cursor - base + 1;
16079 *byte_pos_ptr = start_byte;
16080 return orig_count;
16081 }
16082 else
16083 if (++cursor == ceiling_addr)
16084 break;
16085 }
16086 else
16087 break;
16088 }
16089 start_byte += cursor - base;
16090 }
16091 }
16092 else
16093 {
16094 while (start_byte > limit_byte)
16095 {
16096 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16097 ceiling = max (limit_byte, ceiling);
16098 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16099 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16100 while (1)
16101 {
16102 if (selective_display)
16103 while (--cursor != ceiling_addr
16104 && *cursor != '\n' && *cursor != 015)
16105 ;
16106 else
16107 while (--cursor != ceiling_addr && *cursor != '\n')
16108 ;
16109
16110 if (cursor != ceiling_addr)
16111 {
16112 if (++count == 0)
16113 {
16114 start_byte += cursor - base + 1;
16115 *byte_pos_ptr = start_byte;
16116 /* When scanning backwards, we should
16117 not count the newline posterior to which we stop. */
16118 return - orig_count - 1;
16119 }
16120 }
16121 else
16122 break;
16123 }
16124 /* Here we add 1 to compensate for the last decrement
16125 of CURSOR, which took it past the valid range. */
16126 start_byte += cursor - base + 1;
16127 }
16128 }
16129
16130 *byte_pos_ptr = limit_byte;
16131
16132 if (count < 0)
16133 return - orig_count + count;
16134 return orig_count - count;
16135
16136 }
16137
16138
16139 \f
16140 /***********************************************************************
16141 Displaying strings
16142 ***********************************************************************/
16143
16144 /* Display a NUL-terminated string, starting with index START.
16145
16146 If STRING is non-null, display that C string. Otherwise, the Lisp
16147 string LISP_STRING is displayed.
16148
16149 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16150 FACE_STRING. Display STRING or LISP_STRING with the face at
16151 FACE_STRING_POS in FACE_STRING:
16152
16153 Display the string in the environment given by IT, but use the
16154 standard display table, temporarily.
16155
16156 FIELD_WIDTH is the minimum number of output glyphs to produce.
16157 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16158 with spaces. If STRING has more characters, more than FIELD_WIDTH
16159 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
16160
16161 PRECISION is the maximum number of characters to output from
16162 STRING. PRECISION < 0 means don't truncate the string.
16163
16164 This is roughly equivalent to printf format specifiers:
16165
16166 FIELD_WIDTH PRECISION PRINTF
16167 ----------------------------------------
16168 -1 -1 %s
16169 -1 10 %.10s
16170 10 -1 %10s
16171 20 10 %20.10s
16172
16173 MULTIBYTE zero means do not display multibyte chars, > 0 means do
16174 display them, and < 0 means obey the current buffer's value of
16175 enable_multibyte_characters.
16176
16177 Value is the number of glyphs produced. */
16178
16179 static int
16180 display_string (string, lisp_string, face_string, face_string_pos,
16181 start, it, field_width, precision, max_x, multibyte)
16182 unsigned char *string;
16183 Lisp_Object lisp_string;
16184 Lisp_Object face_string;
16185 int face_string_pos;
16186 int start;
16187 struct it *it;
16188 int field_width, precision, max_x;
16189 int multibyte;
16190 {
16191 int hpos_at_start = it->hpos;
16192 int saved_face_id = it->face_id;
16193 struct glyph_row *row = it->glyph_row;
16194
16195 /* Initialize the iterator IT for iteration over STRING beginning
16196 with index START. */
16197 reseat_to_string (it, string, lisp_string, start,
16198 precision, field_width, multibyte);
16199
16200 /* If displaying STRING, set up the face of the iterator
16201 from LISP_STRING, if that's given. */
16202 if (STRINGP (face_string))
16203 {
16204 int endptr;
16205 struct face *face;
16206
16207 it->face_id
16208 = face_at_string_position (it->w, face_string, face_string_pos,
16209 0, it->region_beg_charpos,
16210 it->region_end_charpos,
16211 &endptr, it->base_face_id, 0);
16212 face = FACE_FROM_ID (it->f, it->face_id);
16213 it->face_box_p = face->box != FACE_NO_BOX;
16214 }
16215
16216 /* Set max_x to the maximum allowed X position. Don't let it go
16217 beyond the right edge of the window. */
16218 if (max_x <= 0)
16219 max_x = it->last_visible_x;
16220 else
16221 max_x = min (max_x, it->last_visible_x);
16222
16223 /* Skip over display elements that are not visible. because IT->w is
16224 hscrolled. */
16225 if (it->current_x < it->first_visible_x)
16226 move_it_in_display_line_to (it, 100000, it->first_visible_x,
16227 MOVE_TO_POS | MOVE_TO_X);
16228
16229 row->ascent = it->max_ascent;
16230 row->height = it->max_ascent + it->max_descent;
16231 row->phys_ascent = it->max_phys_ascent;
16232 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16233
16234 /* This condition is for the case that we are called with current_x
16235 past last_visible_x. */
16236 while (it->current_x < max_x)
16237 {
16238 int x_before, x, n_glyphs_before, i, nglyphs;
16239
16240 /* Get the next display element. */
16241 if (!get_next_display_element (it))
16242 break;
16243
16244 /* Produce glyphs. */
16245 x_before = it->current_x;
16246 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
16247 PRODUCE_GLYPHS (it);
16248
16249 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
16250 i = 0;
16251 x = x_before;
16252 while (i < nglyphs)
16253 {
16254 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16255
16256 if (!it->truncate_lines_p
16257 && x + glyph->pixel_width > max_x)
16258 {
16259 /* End of continued line or max_x reached. */
16260 if (CHAR_GLYPH_PADDING_P (*glyph))
16261 {
16262 /* A wide character is unbreakable. */
16263 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
16264 it->current_x = x_before;
16265 }
16266 else
16267 {
16268 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
16269 it->current_x = x;
16270 }
16271 break;
16272 }
16273 else if (x + glyph->pixel_width > it->first_visible_x)
16274 {
16275 /* Glyph is at least partially visible. */
16276 ++it->hpos;
16277 if (x < it->first_visible_x)
16278 it->glyph_row->x = x - it->first_visible_x;
16279 }
16280 else
16281 {
16282 /* Glyph is off the left margin of the display area.
16283 Should not happen. */
16284 abort ();
16285 }
16286
16287 row->ascent = max (row->ascent, it->max_ascent);
16288 row->height = max (row->height, it->max_ascent + it->max_descent);
16289 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16290 row->phys_height = max (row->phys_height,
16291 it->max_phys_ascent + it->max_phys_descent);
16292 x += glyph->pixel_width;
16293 ++i;
16294 }
16295
16296 /* Stop if max_x reached. */
16297 if (i < nglyphs)
16298 break;
16299
16300 /* Stop at line ends. */
16301 if (ITERATOR_AT_END_OF_LINE_P (it))
16302 {
16303 it->continuation_lines_width = 0;
16304 break;
16305 }
16306
16307 set_iterator_to_next (it, 1);
16308
16309 /* Stop if truncating at the right edge. */
16310 if (it->truncate_lines_p
16311 && it->current_x >= it->last_visible_x)
16312 {
16313 /* Add truncation mark, but don't do it if the line is
16314 truncated at a padding space. */
16315 if (IT_CHARPOS (*it) < it->string_nchars)
16316 {
16317 if (!FRAME_WINDOW_P (it->f))
16318 {
16319 int i, n;
16320
16321 if (it->current_x > it->last_visible_x)
16322 {
16323 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16324 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16325 break;
16326 for (n = row->used[TEXT_AREA]; i < n; ++i)
16327 {
16328 row->used[TEXT_AREA] = i;
16329 produce_special_glyphs (it, IT_TRUNCATION);
16330 }
16331 }
16332 produce_special_glyphs (it, IT_TRUNCATION);
16333 }
16334 it->glyph_row->truncated_on_right_p = 1;
16335 }
16336 break;
16337 }
16338 }
16339
16340 /* Maybe insert a truncation at the left. */
16341 if (it->first_visible_x
16342 && IT_CHARPOS (*it) > 0)
16343 {
16344 if (!FRAME_WINDOW_P (it->f))
16345 insert_left_trunc_glyphs (it);
16346 it->glyph_row->truncated_on_left_p = 1;
16347 }
16348
16349 it->face_id = saved_face_id;
16350
16351 /* Value is number of columns displayed. */
16352 return it->hpos - hpos_at_start;
16353 }
16354
16355
16356 \f
16357 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
16358 appears as an element of LIST or as the car of an element of LIST.
16359 If PROPVAL is a list, compare each element against LIST in that
16360 way, and return 1/2 if any element of PROPVAL is found in LIST.
16361 Otherwise return 0. This function cannot quit.
16362 The return value is 2 if the text is invisible but with an ellipsis
16363 and 1 if it's invisible and without an ellipsis. */
16364
16365 int
16366 invisible_p (propval, list)
16367 register Lisp_Object propval;
16368 Lisp_Object list;
16369 {
16370 register Lisp_Object tail, proptail;
16371
16372 for (tail = list; CONSP (tail); tail = XCDR (tail))
16373 {
16374 register Lisp_Object tem;
16375 tem = XCAR (tail);
16376 if (EQ (propval, tem))
16377 return 1;
16378 if (CONSP (tem) && EQ (propval, XCAR (tem)))
16379 return NILP (XCDR (tem)) ? 1 : 2;
16380 }
16381
16382 if (CONSP (propval))
16383 {
16384 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
16385 {
16386 Lisp_Object propelt;
16387 propelt = XCAR (proptail);
16388 for (tail = list; CONSP (tail); tail = XCDR (tail))
16389 {
16390 register Lisp_Object tem;
16391 tem = XCAR (tail);
16392 if (EQ (propelt, tem))
16393 return 1;
16394 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
16395 return NILP (XCDR (tem)) ? 1 : 2;
16396 }
16397 }
16398 }
16399
16400 return 0;
16401 }
16402
16403 \f
16404 /***********************************************************************
16405 Glyph Display
16406 ***********************************************************************/
16407
16408 #ifdef HAVE_WINDOW_SYSTEM
16409
16410 #if GLYPH_DEBUG
16411
16412 void
16413 dump_glyph_string (s)
16414 struct glyph_string *s;
16415 {
16416 fprintf (stderr, "glyph string\n");
16417 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
16418 s->x, s->y, s->width, s->height);
16419 fprintf (stderr, " ybase = %d\n", s->ybase);
16420 fprintf (stderr, " hl = %d\n", s->hl);
16421 fprintf (stderr, " left overhang = %d, right = %d\n",
16422 s->left_overhang, s->right_overhang);
16423 fprintf (stderr, " nchars = %d\n", s->nchars);
16424 fprintf (stderr, " extends to end of line = %d\n",
16425 s->extends_to_end_of_line_p);
16426 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
16427 fprintf (stderr, " bg width = %d\n", s->background_width);
16428 }
16429
16430 #endif /* GLYPH_DEBUG */
16431
16432 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
16433 of XChar2b structures for S; it can't be allocated in
16434 init_glyph_string because it must be allocated via `alloca'. W
16435 is the window on which S is drawn. ROW and AREA are the glyph row
16436 and area within the row from which S is constructed. START is the
16437 index of the first glyph structure covered by S. HL is a
16438 face-override for drawing S. */
16439
16440 #ifdef HAVE_NTGUI
16441 #define OPTIONAL_HDC(hdc) hdc,
16442 #define DECLARE_HDC(hdc) HDC hdc;
16443 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
16444 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
16445 #endif
16446
16447 #ifndef OPTIONAL_HDC
16448 #define OPTIONAL_HDC(hdc)
16449 #define DECLARE_HDC(hdc)
16450 #define ALLOCATE_HDC(hdc, f)
16451 #define RELEASE_HDC(hdc, f)
16452 #endif
16453
16454 static void
16455 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
16456 struct glyph_string *s;
16457 DECLARE_HDC (hdc)
16458 XChar2b *char2b;
16459 struct window *w;
16460 struct glyph_row *row;
16461 enum glyph_row_area area;
16462 int start;
16463 enum draw_glyphs_face hl;
16464 {
16465 bzero (s, sizeof *s);
16466 s->w = w;
16467 s->f = XFRAME (w->frame);
16468 #ifdef HAVE_NTGUI
16469 s->hdc = hdc;
16470 #endif
16471 s->display = FRAME_X_DISPLAY (s->f);
16472 s->window = FRAME_X_WINDOW (s->f);
16473 s->char2b = char2b;
16474 s->hl = hl;
16475 s->row = row;
16476 s->area = area;
16477 s->first_glyph = row->glyphs[area] + start;
16478 s->height = row->height;
16479 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
16480
16481 /* Display the internal border below the tool-bar window. */
16482 if (s->w == XWINDOW (s->f->tool_bar_window))
16483 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
16484
16485 s->ybase = s->y + row->ascent;
16486 }
16487
16488
16489 /* Append the list of glyph strings with head H and tail T to the list
16490 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
16491
16492 static INLINE void
16493 append_glyph_string_lists (head, tail, h, t)
16494 struct glyph_string **head, **tail;
16495 struct glyph_string *h, *t;
16496 {
16497 if (h)
16498 {
16499 if (*head)
16500 (*tail)->next = h;
16501 else
16502 *head = h;
16503 h->prev = *tail;
16504 *tail = t;
16505 }
16506 }
16507
16508
16509 /* Prepend the list of glyph strings with head H and tail T to the
16510 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
16511 result. */
16512
16513 static INLINE void
16514 prepend_glyph_string_lists (head, tail, h, t)
16515 struct glyph_string **head, **tail;
16516 struct glyph_string *h, *t;
16517 {
16518 if (h)
16519 {
16520 if (*head)
16521 (*head)->prev = t;
16522 else
16523 *tail = t;
16524 t->next = *head;
16525 *head = h;
16526 }
16527 }
16528
16529
16530 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
16531 Set *HEAD and *TAIL to the resulting list. */
16532
16533 static INLINE void
16534 append_glyph_string (head, tail, s)
16535 struct glyph_string **head, **tail;
16536 struct glyph_string *s;
16537 {
16538 s->next = s->prev = NULL;
16539 append_glyph_string_lists (head, tail, s, s);
16540 }
16541
16542
16543 /* Get face and two-byte form of character glyph GLYPH on frame F.
16544 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
16545 a pointer to a realized face that is ready for display. */
16546
16547 static INLINE struct face *
16548 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
16549 struct frame *f;
16550 struct glyph *glyph;
16551 XChar2b *char2b;
16552 int *two_byte_p;
16553 {
16554 struct face *face;
16555
16556 xassert (glyph->type == CHAR_GLYPH);
16557 face = FACE_FROM_ID (f, glyph->face_id);
16558
16559 if (two_byte_p)
16560 *two_byte_p = 0;
16561
16562 if (!glyph->multibyte_p)
16563 {
16564 /* Unibyte case. We don't have to encode, but we have to make
16565 sure to use a face suitable for unibyte. */
16566 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
16567 }
16568 else if (glyph->u.ch < 128
16569 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
16570 {
16571 /* Case of ASCII in a face known to fit ASCII. */
16572 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
16573 }
16574 else
16575 {
16576 int c1, c2, charset;
16577
16578 /* Split characters into bytes. If c2 is -1 afterwards, C is
16579 really a one-byte character so that byte1 is zero. */
16580 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
16581 if (c2 > 0)
16582 STORE_XCHAR2B (char2b, c1, c2);
16583 else
16584 STORE_XCHAR2B (char2b, 0, c1);
16585
16586 /* Maybe encode the character in *CHAR2B. */
16587 if (charset != CHARSET_ASCII)
16588 {
16589 struct font_info *font_info
16590 = FONT_INFO_FROM_ID (f, face->font_info_id);
16591 if (font_info)
16592 glyph->font_type
16593 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
16594 }
16595 }
16596
16597 /* Make sure X resources of the face are allocated. */
16598 xassert (face != NULL);
16599 PREPARE_FACE_FOR_DISPLAY (f, face);
16600 return face;
16601 }
16602
16603
16604 /* Fill glyph string S with composition components specified by S->cmp.
16605
16606 FACES is an array of faces for all components of this composition.
16607 S->gidx is the index of the first component for S.
16608 OVERLAPS_P non-zero means S should draw the foreground only, and
16609 use its physical height for clipping.
16610
16611 Value is the index of a component not in S. */
16612
16613 static int
16614 fill_composite_glyph_string (s, faces, overlaps_p)
16615 struct glyph_string *s;
16616 struct face **faces;
16617 int overlaps_p;
16618 {
16619 int i;
16620
16621 xassert (s);
16622
16623 s->for_overlaps_p = overlaps_p;
16624
16625 s->face = faces[s->gidx];
16626 s->font = s->face->font;
16627 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
16628
16629 /* For all glyphs of this composition, starting at the offset
16630 S->gidx, until we reach the end of the definition or encounter a
16631 glyph that requires the different face, add it to S. */
16632 ++s->nchars;
16633 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
16634 ++s->nchars;
16635
16636 /* All glyph strings for the same composition has the same width,
16637 i.e. the width set for the first component of the composition. */
16638
16639 s->width = s->first_glyph->pixel_width;
16640
16641 /* If the specified font could not be loaded, use the frame's
16642 default font, but record the fact that we couldn't load it in
16643 the glyph string so that we can draw rectangles for the
16644 characters of the glyph string. */
16645 if (s->font == NULL)
16646 {
16647 s->font_not_found_p = 1;
16648 s->font = FRAME_FONT (s->f);
16649 }
16650
16651 /* Adjust base line for subscript/superscript text. */
16652 s->ybase += s->first_glyph->voffset;
16653
16654 xassert (s->face && s->face->gc);
16655
16656 /* This glyph string must always be drawn with 16-bit functions. */
16657 s->two_byte_p = 1;
16658
16659 return s->gidx + s->nchars;
16660 }
16661
16662
16663 /* Fill glyph string S from a sequence of character glyphs.
16664
16665 FACE_ID is the face id of the string. START is the index of the
16666 first glyph to consider, END is the index of the last + 1.
16667 OVERLAPS_P non-zero means S should draw the foreground only, and
16668 use its physical height for clipping.
16669
16670 Value is the index of the first glyph not in S. */
16671
16672 static int
16673 fill_glyph_string (s, face_id, start, end, overlaps_p)
16674 struct glyph_string *s;
16675 int face_id;
16676 int start, end, overlaps_p;
16677 {
16678 struct glyph *glyph, *last;
16679 int voffset;
16680 int glyph_not_available_p;
16681
16682 xassert (s->f == XFRAME (s->w->frame));
16683 xassert (s->nchars == 0);
16684 xassert (start >= 0 && end > start);
16685
16686 s->for_overlaps_p = overlaps_p,
16687 glyph = s->row->glyphs[s->area] + start;
16688 last = s->row->glyphs[s->area] + end;
16689 voffset = glyph->voffset;
16690
16691 glyph_not_available_p = glyph->glyph_not_available_p;
16692
16693 while (glyph < last
16694 && glyph->type == CHAR_GLYPH
16695 && glyph->voffset == voffset
16696 /* Same face id implies same font, nowadays. */
16697 && glyph->face_id == face_id
16698 && glyph->glyph_not_available_p == glyph_not_available_p)
16699 {
16700 int two_byte_p;
16701
16702 s->face = get_glyph_face_and_encoding (s->f, glyph,
16703 s->char2b + s->nchars,
16704 &two_byte_p);
16705 s->two_byte_p = two_byte_p;
16706 ++s->nchars;
16707 xassert (s->nchars <= end - start);
16708 s->width += glyph->pixel_width;
16709 ++glyph;
16710 }
16711
16712 s->font = s->face->font;
16713 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
16714
16715 /* If the specified font could not be loaded, use the frame's font,
16716 but record the fact that we couldn't load it in
16717 S->font_not_found_p so that we can draw rectangles for the
16718 characters of the glyph string. */
16719 if (s->font == NULL || glyph_not_available_p)
16720 {
16721 s->font_not_found_p = 1;
16722 s->font = FRAME_FONT (s->f);
16723 }
16724
16725 /* Adjust base line for subscript/superscript text. */
16726 s->ybase += voffset;
16727
16728 xassert (s->face && s->face->gc);
16729 return glyph - s->row->glyphs[s->area];
16730 }
16731
16732
16733 /* Fill glyph string S from image glyph S->first_glyph. */
16734
16735 static void
16736 fill_image_glyph_string (s)
16737 struct glyph_string *s;
16738 {
16739 xassert (s->first_glyph->type == IMAGE_GLYPH);
16740 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
16741 xassert (s->img);
16742 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
16743 s->font = s->face->font;
16744 s->width = s->first_glyph->pixel_width;
16745
16746 /* Adjust base line for subscript/superscript text. */
16747 s->ybase += s->first_glyph->voffset;
16748 }
16749
16750
16751 /* Fill glyph string S from a sequence of stretch glyphs.
16752
16753 ROW is the glyph row in which the glyphs are found, AREA is the
16754 area within the row. START is the index of the first glyph to
16755 consider, END is the index of the last + 1.
16756
16757 Value is the index of the first glyph not in S. */
16758
16759 static int
16760 fill_stretch_glyph_string (s, row, area, start, end)
16761 struct glyph_string *s;
16762 struct glyph_row *row;
16763 enum glyph_row_area area;
16764 int start, end;
16765 {
16766 struct glyph *glyph, *last;
16767 int voffset, face_id;
16768
16769 xassert (s->first_glyph->type == STRETCH_GLYPH);
16770
16771 glyph = s->row->glyphs[s->area] + start;
16772 last = s->row->glyphs[s->area] + end;
16773 face_id = glyph->face_id;
16774 s->face = FACE_FROM_ID (s->f, face_id);
16775 s->font = s->face->font;
16776 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
16777 s->width = glyph->pixel_width;
16778 voffset = glyph->voffset;
16779
16780 for (++glyph;
16781 (glyph < last
16782 && glyph->type == STRETCH_GLYPH
16783 && glyph->voffset == voffset
16784 && glyph->face_id == face_id);
16785 ++glyph)
16786 s->width += glyph->pixel_width;
16787
16788 /* Adjust base line for subscript/superscript text. */
16789 s->ybase += voffset;
16790
16791 /* The case that face->gc == 0 is handled when drawing the glyph
16792 string by calling PREPARE_FACE_FOR_DISPLAY. */
16793 xassert (s->face);
16794 return glyph - s->row->glyphs[s->area];
16795 }
16796
16797
16798 /* EXPORT for RIF:
16799 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
16800 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
16801 assumed to be zero. */
16802
16803 void
16804 x_get_glyph_overhangs (glyph, f, left, right)
16805 struct glyph *glyph;
16806 struct frame *f;
16807 int *left, *right;
16808 {
16809 *left = *right = 0;
16810
16811 if (glyph->type == CHAR_GLYPH)
16812 {
16813 XFontStruct *font;
16814 struct face *face;
16815 struct font_info *font_info;
16816 XChar2b char2b;
16817 XCharStruct *pcm;
16818
16819 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
16820 font = face->font;
16821 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
16822 if (font /* ++KFS: Should this be font_info ? */
16823 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
16824 {
16825 if (pcm->rbearing > pcm->width)
16826 *right = pcm->rbearing - pcm->width;
16827 if (pcm->lbearing < 0)
16828 *left = -pcm->lbearing;
16829 }
16830 }
16831 }
16832
16833
16834 /* Return the index of the first glyph preceding glyph string S that
16835 is overwritten by S because of S's left overhang. Value is -1
16836 if no glyphs are overwritten. */
16837
16838 static int
16839 left_overwritten (s)
16840 struct glyph_string *s;
16841 {
16842 int k;
16843
16844 if (s->left_overhang)
16845 {
16846 int x = 0, i;
16847 struct glyph *glyphs = s->row->glyphs[s->area];
16848 int first = s->first_glyph - glyphs;
16849
16850 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
16851 x -= glyphs[i].pixel_width;
16852
16853 k = i + 1;
16854 }
16855 else
16856 k = -1;
16857
16858 return k;
16859 }
16860
16861
16862 /* Return the index of the first glyph preceding glyph string S that
16863 is overwriting S because of its right overhang. Value is -1 if no
16864 glyph in front of S overwrites S. */
16865
16866 static int
16867 left_overwriting (s)
16868 struct glyph_string *s;
16869 {
16870 int i, k, x;
16871 struct glyph *glyphs = s->row->glyphs[s->area];
16872 int first = s->first_glyph - glyphs;
16873
16874 k = -1;
16875 x = 0;
16876 for (i = first - 1; i >= 0; --i)
16877 {
16878 int left, right;
16879 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
16880 if (x + right > 0)
16881 k = i;
16882 x -= glyphs[i].pixel_width;
16883 }
16884
16885 return k;
16886 }
16887
16888
16889 /* Return the index of the last glyph following glyph string S that is
16890 not overwritten by S because of S's right overhang. Value is -1 if
16891 no such glyph is found. */
16892
16893 static int
16894 right_overwritten (s)
16895 struct glyph_string *s;
16896 {
16897 int k = -1;
16898
16899 if (s->right_overhang)
16900 {
16901 int x = 0, i;
16902 struct glyph *glyphs = s->row->glyphs[s->area];
16903 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
16904 int end = s->row->used[s->area];
16905
16906 for (i = first; i < end && s->right_overhang > x; ++i)
16907 x += glyphs[i].pixel_width;
16908
16909 k = i;
16910 }
16911
16912 return k;
16913 }
16914
16915
16916 /* Return the index of the last glyph following glyph string S that
16917 overwrites S because of its left overhang. Value is negative
16918 if no such glyph is found. */
16919
16920 static int
16921 right_overwriting (s)
16922 struct glyph_string *s;
16923 {
16924 int i, k, x;
16925 int end = s->row->used[s->area];
16926 struct glyph *glyphs = s->row->glyphs[s->area];
16927 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
16928
16929 k = -1;
16930 x = 0;
16931 for (i = first; i < end; ++i)
16932 {
16933 int left, right;
16934 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
16935 if (x - left < 0)
16936 k = i;
16937 x += glyphs[i].pixel_width;
16938 }
16939
16940 return k;
16941 }
16942
16943
16944 /* Get face and two-byte form of character C in face FACE_ID on frame
16945 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
16946 means we want to display multibyte text. DISPLAY_P non-zero means
16947 make sure that X resources for the face returned are allocated.
16948 Value is a pointer to a realized face that is ready for display if
16949 DISPLAY_P is non-zero. */
16950
16951 static INLINE struct face *
16952 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
16953 struct frame *f;
16954 int c, face_id;
16955 XChar2b *char2b;
16956 int multibyte_p, display_p;
16957 {
16958 struct face *face = FACE_FROM_ID (f, face_id);
16959
16960 if (!multibyte_p)
16961 {
16962 /* Unibyte case. We don't have to encode, but we have to make
16963 sure to use a face suitable for unibyte. */
16964 STORE_XCHAR2B (char2b, 0, c);
16965 face_id = FACE_FOR_CHAR (f, face, c);
16966 face = FACE_FROM_ID (f, face_id);
16967 }
16968 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
16969 {
16970 /* Case of ASCII in a face known to fit ASCII. */
16971 STORE_XCHAR2B (char2b, 0, c);
16972 }
16973 else
16974 {
16975 int c1, c2, charset;
16976
16977 /* Split characters into bytes. If c2 is -1 afterwards, C is
16978 really a one-byte character so that byte1 is zero. */
16979 SPLIT_CHAR (c, charset, c1, c2);
16980 if (c2 > 0)
16981 STORE_XCHAR2B (char2b, c1, c2);
16982 else
16983 STORE_XCHAR2B (char2b, 0, c1);
16984
16985 /* Maybe encode the character in *CHAR2B. */
16986 if (face->font != NULL)
16987 {
16988 struct font_info *font_info
16989 = FONT_INFO_FROM_ID (f, face->font_info_id);
16990 if (font_info)
16991 rif->encode_char (c, char2b, font_info, 0);
16992 }
16993 }
16994
16995 /* Make sure X resources of the face are allocated. */
16996 #ifdef HAVE_X_WINDOWS
16997 if (display_p)
16998 #endif
16999 {
17000 xassert (face != NULL);
17001 PREPARE_FACE_FOR_DISPLAY (f, face);
17002 }
17003
17004 return face;
17005 }
17006
17007
17008 /* Set background width of glyph string S. START is the index of the
17009 first glyph following S. LAST_X is the right-most x-position + 1
17010 in the drawing area. */
17011
17012 static INLINE void
17013 set_glyph_string_background_width (s, start, last_x)
17014 struct glyph_string *s;
17015 int start;
17016 int last_x;
17017 {
17018 /* If the face of this glyph string has to be drawn to the end of
17019 the drawing area, set S->extends_to_end_of_line_p. */
17020 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
17021
17022 if (start == s->row->used[s->area]
17023 && s->area == TEXT_AREA
17024 && ((s->hl == DRAW_NORMAL_TEXT
17025 && (s->row->fill_line_p
17026 || s->face->background != default_face->background
17027 || s->face->stipple != default_face->stipple
17028 || s->row->mouse_face_p))
17029 || s->hl == DRAW_MOUSE_FACE
17030 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
17031 && s->row->fill_line_p)))
17032 s->extends_to_end_of_line_p = 1;
17033
17034 /* If S extends its face to the end of the line, set its
17035 background_width to the distance to the right edge of the drawing
17036 area. */
17037 if (s->extends_to_end_of_line_p)
17038 s->background_width = last_x - s->x + 1;
17039 else
17040 s->background_width = s->width;
17041 }
17042
17043
17044 /* Compute overhangs and x-positions for glyph string S and its
17045 predecessors, or successors. X is the starting x-position for S.
17046 BACKWARD_P non-zero means process predecessors. */
17047
17048 static void
17049 compute_overhangs_and_x (s, x, backward_p)
17050 struct glyph_string *s;
17051 int x;
17052 int backward_p;
17053 {
17054 if (backward_p)
17055 {
17056 while (s)
17057 {
17058 if (rif->compute_glyph_string_overhangs)
17059 rif->compute_glyph_string_overhangs (s);
17060 x -= s->width;
17061 s->x = x;
17062 s = s->prev;
17063 }
17064 }
17065 else
17066 {
17067 while (s)
17068 {
17069 if (rif->compute_glyph_string_overhangs)
17070 rif->compute_glyph_string_overhangs (s);
17071 s->x = x;
17072 x += s->width;
17073 s = s->next;
17074 }
17075 }
17076 }
17077
17078
17079
17080 /* The following macros are only called from draw_glyphs below.
17081 They reference the following parameters of that function directly:
17082 `w', `row', `area', and `overlap_p'
17083 as well as the following local variables:
17084 `s', `f', and `hdc' (in W32) */
17085
17086 #ifdef HAVE_NTGUI
17087 /* On W32, silently add local `hdc' variable to argument list of
17088 init_glyph_string. */
17089 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17090 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
17091 #else
17092 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17093 init_glyph_string (s, char2b, w, row, area, start, hl)
17094 #endif
17095
17096 /* Add a glyph string for a stretch glyph to the list of strings
17097 between HEAD and TAIL. START is the index of the stretch glyph in
17098 row area AREA of glyph row ROW. END is the index of the last glyph
17099 in that glyph row area. X is the current output position assigned
17100 to the new glyph string constructed. HL overrides that face of the
17101 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17102 is the right-most x-position of the drawing area. */
17103
17104 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
17105 and below -- keep them on one line. */
17106 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17107 do \
17108 { \
17109 s = (struct glyph_string *) alloca (sizeof *s); \
17110 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17111 START = fill_stretch_glyph_string (s, row, area, START, END); \
17112 append_glyph_string (&HEAD, &TAIL, s); \
17113 s->x = (X); \
17114 } \
17115 while (0)
17116
17117
17118 /* Add a glyph string for an image glyph to the list of strings
17119 between HEAD and TAIL. START is the index of the image glyph in
17120 row area AREA of glyph row ROW. END is the index of the last glyph
17121 in that glyph row area. X is the current output position assigned
17122 to the new glyph string constructed. HL overrides that face of the
17123 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17124 is the right-most x-position of the drawing area. */
17125
17126 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17127 do \
17128 { \
17129 s = (struct glyph_string *) alloca (sizeof *s); \
17130 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17131 fill_image_glyph_string (s); \
17132 append_glyph_string (&HEAD, &TAIL, s); \
17133 ++START; \
17134 s->x = (X); \
17135 } \
17136 while (0)
17137
17138
17139 /* Add a glyph string for a sequence of character glyphs to the list
17140 of strings between HEAD and TAIL. START is the index of the first
17141 glyph in row area AREA of glyph row ROW that is part of the new
17142 glyph string. END is the index of the last glyph in that glyph row
17143 area. X is the current output position assigned to the new glyph
17144 string constructed. HL overrides that face of the glyph; e.g. it
17145 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
17146 right-most x-position of the drawing area. */
17147
17148 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17149 do \
17150 { \
17151 int c, face_id; \
17152 XChar2b *char2b; \
17153 \
17154 c = (row)->glyphs[area][START].u.ch; \
17155 face_id = (row)->glyphs[area][START].face_id; \
17156 \
17157 s = (struct glyph_string *) alloca (sizeof *s); \
17158 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
17159 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
17160 append_glyph_string (&HEAD, &TAIL, s); \
17161 s->x = (X); \
17162 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
17163 } \
17164 while (0)
17165
17166
17167 /* Add a glyph string for a composite sequence to the list of strings
17168 between HEAD and TAIL. START is the index of the first glyph in
17169 row area AREA of glyph row ROW that is part of the new glyph
17170 string. END is the index of the last glyph in that glyph row area.
17171 X is the current output position assigned to the new glyph string
17172 constructed. HL overrides that face of the glyph; e.g. it is
17173 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
17174 x-position of the drawing area. */
17175
17176 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17177 do { \
17178 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
17179 int face_id = (row)->glyphs[area][START].face_id; \
17180 struct face *base_face = FACE_FROM_ID (f, face_id); \
17181 struct composition *cmp = composition_table[cmp_id]; \
17182 int glyph_len = cmp->glyph_len; \
17183 XChar2b *char2b; \
17184 struct face **faces; \
17185 struct glyph_string *first_s = NULL; \
17186 int n; \
17187 \
17188 base_face = base_face->ascii_face; \
17189 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
17190 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
17191 /* At first, fill in `char2b' and `faces'. */ \
17192 for (n = 0; n < glyph_len; n++) \
17193 { \
17194 int c = COMPOSITION_GLYPH (cmp, n); \
17195 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
17196 faces[n] = FACE_FROM_ID (f, this_face_id); \
17197 get_char_face_and_encoding (f, c, this_face_id, \
17198 char2b + n, 1, 1); \
17199 } \
17200 \
17201 /* Make glyph_strings for each glyph sequence that is drawable by \
17202 the same face, and append them to HEAD/TAIL. */ \
17203 for (n = 0; n < cmp->glyph_len;) \
17204 { \
17205 s = (struct glyph_string *) alloca (sizeof *s); \
17206 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
17207 append_glyph_string (&(HEAD), &(TAIL), s); \
17208 s->cmp = cmp; \
17209 s->gidx = n; \
17210 s->x = (X); \
17211 \
17212 if (n == 0) \
17213 first_s = s; \
17214 \
17215 n = fill_composite_glyph_string (s, faces, overlaps_p); \
17216 } \
17217 \
17218 ++START; \
17219 s = first_s; \
17220 } while (0)
17221
17222
17223 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
17224 of AREA of glyph row ROW on window W between indices START and END.
17225 HL overrides the face for drawing glyph strings, e.g. it is
17226 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
17227 x-positions of the drawing area.
17228
17229 This is an ugly monster macro construct because we must use alloca
17230 to allocate glyph strings (because draw_glyphs can be called
17231 asynchronously). */
17232
17233 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17234 do \
17235 { \
17236 HEAD = TAIL = NULL; \
17237 while (START < END) \
17238 { \
17239 struct glyph *first_glyph = (row)->glyphs[area] + START; \
17240 switch (first_glyph->type) \
17241 { \
17242 case CHAR_GLYPH: \
17243 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
17244 HL, X, LAST_X); \
17245 break; \
17246 \
17247 case COMPOSITE_GLYPH: \
17248 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
17249 HL, X, LAST_X); \
17250 break; \
17251 \
17252 case STRETCH_GLYPH: \
17253 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
17254 HL, X, LAST_X); \
17255 break; \
17256 \
17257 case IMAGE_GLYPH: \
17258 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
17259 HL, X, LAST_X); \
17260 break; \
17261 \
17262 default: \
17263 abort (); \
17264 } \
17265 \
17266 set_glyph_string_background_width (s, START, LAST_X); \
17267 (X) += s->width; \
17268 } \
17269 } \
17270 while (0)
17271
17272
17273 /* Draw glyphs between START and END in AREA of ROW on window W,
17274 starting at x-position X. X is relative to AREA in W. HL is a
17275 face-override with the following meaning:
17276
17277 DRAW_NORMAL_TEXT draw normally
17278 DRAW_CURSOR draw in cursor face
17279 DRAW_MOUSE_FACE draw in mouse face.
17280 DRAW_INVERSE_VIDEO draw in mode line face
17281 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
17282 DRAW_IMAGE_RAISED draw an image with a raised relief around it
17283
17284 If OVERLAPS_P is non-zero, draw only the foreground of characters
17285 and clip to the physical height of ROW.
17286
17287 Value is the x-position reached, relative to AREA of W. */
17288
17289 static int
17290 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
17291 struct window *w;
17292 int x;
17293 struct glyph_row *row;
17294 enum glyph_row_area area;
17295 int start, end;
17296 enum draw_glyphs_face hl;
17297 int overlaps_p;
17298 {
17299 struct glyph_string *head, *tail;
17300 struct glyph_string *s;
17301 int last_x, area_width;
17302 int x_reached;
17303 int i, j;
17304 struct frame *f = XFRAME (WINDOW_FRAME (w));
17305 DECLARE_HDC (hdc);
17306
17307 ALLOCATE_HDC (hdc, f);
17308
17309 /* Let's rather be paranoid than getting a SEGV. */
17310 end = min (end, row->used[area]);
17311 start = max (0, start);
17312 start = min (end, start);
17313
17314 /* Translate X to frame coordinates. Set last_x to the right
17315 end of the drawing area. */
17316 if (row->full_width_p)
17317 {
17318 /* X is relative to the left edge of W, without scroll bars
17319 or fringes. */
17320 x += WINDOW_LEFT_EDGE_X (w);
17321 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
17322 }
17323 else
17324 {
17325 int area_left = window_box_left (w, area);
17326 x += area_left;
17327 area_width = window_box_width (w, area);
17328 last_x = area_left + area_width;
17329 }
17330
17331 /* Build a doubly-linked list of glyph_string structures between
17332 head and tail from what we have to draw. Note that the macro
17333 BUILD_GLYPH_STRINGS will modify its start parameter. That's
17334 the reason we use a separate variable `i'. */
17335 i = start;
17336 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
17337 if (tail)
17338 x_reached = tail->x + tail->background_width;
17339 else
17340 x_reached = x;
17341
17342 /* If there are any glyphs with lbearing < 0 or rbearing > width in
17343 the row, redraw some glyphs in front or following the glyph
17344 strings built above. */
17345 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
17346 {
17347 int dummy_x = 0;
17348 struct glyph_string *h, *t;
17349
17350 /* Compute overhangs for all glyph strings. */
17351 if (rif->compute_glyph_string_overhangs)
17352 for (s = head; s; s = s->next)
17353 rif->compute_glyph_string_overhangs (s);
17354
17355 /* Prepend glyph strings for glyphs in front of the first glyph
17356 string that are overwritten because of the first glyph
17357 string's left overhang. The background of all strings
17358 prepended must be drawn because the first glyph string
17359 draws over it. */
17360 i = left_overwritten (head);
17361 if (i >= 0)
17362 {
17363 j = i;
17364 BUILD_GLYPH_STRINGS (j, start, h, t,
17365 DRAW_NORMAL_TEXT, dummy_x, last_x);
17366 start = i;
17367 compute_overhangs_and_x (t, head->x, 1);
17368 prepend_glyph_string_lists (&head, &tail, h, t);
17369 }
17370
17371 /* Prepend glyph strings for glyphs in front of the first glyph
17372 string that overwrite that glyph string because of their
17373 right overhang. For these strings, only the foreground must
17374 be drawn, because it draws over the glyph string at `head'.
17375 The background must not be drawn because this would overwrite
17376 right overhangs of preceding glyphs for which no glyph
17377 strings exist. */
17378 i = left_overwriting (head);
17379 if (i >= 0)
17380 {
17381 BUILD_GLYPH_STRINGS (i, start, h, t,
17382 DRAW_NORMAL_TEXT, dummy_x, last_x);
17383 for (s = h; s; s = s->next)
17384 s->background_filled_p = 1;
17385 compute_overhangs_and_x (t, head->x, 1);
17386 prepend_glyph_string_lists (&head, &tail, h, t);
17387 }
17388
17389 /* Append glyphs strings for glyphs following the last glyph
17390 string tail that are overwritten by tail. The background of
17391 these strings has to be drawn because tail's foreground draws
17392 over it. */
17393 i = right_overwritten (tail);
17394 if (i >= 0)
17395 {
17396 BUILD_GLYPH_STRINGS (end, i, h, t,
17397 DRAW_NORMAL_TEXT, x, last_x);
17398 compute_overhangs_and_x (h, tail->x + tail->width, 0);
17399 append_glyph_string_lists (&head, &tail, h, t);
17400 }
17401
17402 /* Append glyph strings for glyphs following the last glyph
17403 string tail that overwrite tail. The foreground of such
17404 glyphs has to be drawn because it writes into the background
17405 of tail. The background must not be drawn because it could
17406 paint over the foreground of following glyphs. */
17407 i = right_overwriting (tail);
17408 if (i >= 0)
17409 {
17410 BUILD_GLYPH_STRINGS (end, i, h, t,
17411 DRAW_NORMAL_TEXT, x, last_x);
17412 for (s = h; s; s = s->next)
17413 s->background_filled_p = 1;
17414 compute_overhangs_and_x (h, tail->x + tail->width, 0);
17415 append_glyph_string_lists (&head, &tail, h, t);
17416 }
17417 }
17418
17419 /* Draw all strings. */
17420 for (s = head; s; s = s->next)
17421 rif->draw_glyph_string (s);
17422
17423 if (area == TEXT_AREA
17424 && !row->full_width_p
17425 /* When drawing overlapping rows, only the glyph strings'
17426 foreground is drawn, which doesn't erase a cursor
17427 completely. */
17428 && !overlaps_p)
17429 {
17430 int x0 = head ? head->x : x;
17431 int x1 = tail ? tail->x + tail->background_width : x;
17432
17433 int text_left = window_box_left (w, TEXT_AREA);
17434 x0 -= text_left;
17435 x1 -= text_left;
17436
17437 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
17438 row->y, MATRIX_ROW_BOTTOM_Y (row));
17439 }
17440
17441 /* Value is the x-position up to which drawn, relative to AREA of W.
17442 This doesn't include parts drawn because of overhangs. */
17443 if (row->full_width_p)
17444 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
17445 else
17446 x_reached -= window_box_left (w, area);
17447
17448 RELEASE_HDC (hdc, f);
17449
17450 return x_reached;
17451 }
17452
17453
17454 /* Store one glyph for IT->char_to_display in IT->glyph_row.
17455 Called from x_produce_glyphs when IT->glyph_row is non-null. */
17456
17457 static INLINE void
17458 append_glyph (it)
17459 struct it *it;
17460 {
17461 struct glyph *glyph;
17462 enum glyph_row_area area = it->area;
17463
17464 xassert (it->glyph_row);
17465 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
17466
17467 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17468 if (glyph < it->glyph_row->glyphs[area + 1])
17469 {
17470 glyph->charpos = CHARPOS (it->position);
17471 glyph->object = it->object;
17472 glyph->pixel_width = it->pixel_width;
17473 glyph->voffset = it->voffset;
17474 glyph->type = CHAR_GLYPH;
17475 glyph->multibyte_p = it->multibyte_p;
17476 glyph->left_box_line_p = it->start_of_box_run_p;
17477 glyph->right_box_line_p = it->end_of_box_run_p;
17478 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
17479 || it->phys_descent > it->descent);
17480 glyph->padding_p = 0;
17481 glyph->glyph_not_available_p = it->glyph_not_available_p;
17482 glyph->face_id = it->face_id;
17483 glyph->u.ch = it->char_to_display;
17484 glyph->font_type = FONT_TYPE_UNKNOWN;
17485 ++it->glyph_row->used[area];
17486 }
17487 }
17488
17489 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
17490 Called from x_produce_glyphs when IT->glyph_row is non-null. */
17491
17492 static INLINE void
17493 append_composite_glyph (it)
17494 struct it *it;
17495 {
17496 struct glyph *glyph;
17497 enum glyph_row_area area = it->area;
17498
17499 xassert (it->glyph_row);
17500
17501 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17502 if (glyph < it->glyph_row->glyphs[area + 1])
17503 {
17504 glyph->charpos = CHARPOS (it->position);
17505 glyph->object = it->object;
17506 glyph->pixel_width = it->pixel_width;
17507 glyph->voffset = it->voffset;
17508 glyph->type = COMPOSITE_GLYPH;
17509 glyph->multibyte_p = it->multibyte_p;
17510 glyph->left_box_line_p = it->start_of_box_run_p;
17511 glyph->right_box_line_p = it->end_of_box_run_p;
17512 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
17513 || it->phys_descent > it->descent);
17514 glyph->padding_p = 0;
17515 glyph->glyph_not_available_p = 0;
17516 glyph->face_id = it->face_id;
17517 glyph->u.cmp_id = it->cmp_id;
17518 glyph->font_type = FONT_TYPE_UNKNOWN;
17519 ++it->glyph_row->used[area];
17520 }
17521 }
17522
17523
17524 /* Change IT->ascent and IT->height according to the setting of
17525 IT->voffset. */
17526
17527 static INLINE void
17528 take_vertical_position_into_account (it)
17529 struct it *it;
17530 {
17531 if (it->voffset)
17532 {
17533 if (it->voffset < 0)
17534 /* Increase the ascent so that we can display the text higher
17535 in the line. */
17536 it->ascent += abs (it->voffset);
17537 else
17538 /* Increase the descent so that we can display the text lower
17539 in the line. */
17540 it->descent += it->voffset;
17541 }
17542 }
17543
17544
17545 /* Produce glyphs/get display metrics for the image IT is loaded with.
17546 See the description of struct display_iterator in dispextern.h for
17547 an overview of struct display_iterator. */
17548
17549 static void
17550 produce_image_glyph (it)
17551 struct it *it;
17552 {
17553 struct image *img;
17554 struct face *face;
17555
17556 xassert (it->what == IT_IMAGE);
17557
17558 face = FACE_FROM_ID (it->f, it->face_id);
17559 img = IMAGE_FROM_ID (it->f, it->image_id);
17560 xassert (img);
17561
17562 /* Make sure X resources of the face and image are loaded. */
17563 PREPARE_FACE_FOR_DISPLAY (it->f, face);
17564 prepare_image_for_display (it->f, img);
17565
17566 it->ascent = it->phys_ascent = image_ascent (img, face);
17567 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
17568 it->pixel_width = img->width + 2 * img->hmargin;
17569
17570 it->nglyphs = 1;
17571
17572 if (face->box != FACE_NO_BOX)
17573 {
17574 if (face->box_line_width > 0)
17575 {
17576 it->ascent += face->box_line_width;
17577 it->descent += face->box_line_width;
17578 }
17579
17580 if (it->start_of_box_run_p)
17581 it->pixel_width += abs (face->box_line_width);
17582 if (it->end_of_box_run_p)
17583 it->pixel_width += abs (face->box_line_width);
17584 }
17585
17586 take_vertical_position_into_account (it);
17587
17588 if (it->glyph_row)
17589 {
17590 struct glyph *glyph;
17591 enum glyph_row_area area = it->area;
17592
17593 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17594 if (glyph < it->glyph_row->glyphs[area + 1])
17595 {
17596 glyph->charpos = CHARPOS (it->position);
17597 glyph->object = it->object;
17598 glyph->pixel_width = it->pixel_width;
17599 glyph->voffset = it->voffset;
17600 glyph->type = IMAGE_GLYPH;
17601 glyph->multibyte_p = it->multibyte_p;
17602 glyph->left_box_line_p = it->start_of_box_run_p;
17603 glyph->right_box_line_p = it->end_of_box_run_p;
17604 glyph->overlaps_vertically_p = 0;
17605 glyph->padding_p = 0;
17606 glyph->glyph_not_available_p = 0;
17607 glyph->face_id = it->face_id;
17608 glyph->u.img_id = img->id;
17609 glyph->font_type = FONT_TYPE_UNKNOWN;
17610 ++it->glyph_row->used[area];
17611 }
17612 }
17613 }
17614
17615
17616 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
17617 of the glyph, WIDTH and HEIGHT are the width and height of the
17618 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
17619 ascent of the glyph (0 <= ASCENT <= 1). */
17620
17621 static void
17622 append_stretch_glyph (it, object, width, height, ascent)
17623 struct it *it;
17624 Lisp_Object object;
17625 int width, height;
17626 double ascent;
17627 {
17628 struct glyph *glyph;
17629 enum glyph_row_area area = it->area;
17630
17631 xassert (ascent >= 0 && ascent <= 1);
17632
17633 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17634 if (glyph < it->glyph_row->glyphs[area + 1])
17635 {
17636 glyph->charpos = CHARPOS (it->position);
17637 glyph->object = object;
17638 glyph->pixel_width = width;
17639 glyph->voffset = it->voffset;
17640 glyph->type = STRETCH_GLYPH;
17641 glyph->multibyte_p = it->multibyte_p;
17642 glyph->left_box_line_p = it->start_of_box_run_p;
17643 glyph->right_box_line_p = it->end_of_box_run_p;
17644 glyph->overlaps_vertically_p = 0;
17645 glyph->padding_p = 0;
17646 glyph->glyph_not_available_p = 0;
17647 glyph->face_id = it->face_id;
17648 glyph->u.stretch.ascent = height * ascent;
17649 glyph->u.stretch.height = height;
17650 glyph->font_type = FONT_TYPE_UNKNOWN;
17651 ++it->glyph_row->used[area];
17652 }
17653 }
17654
17655
17656 /* Produce a stretch glyph for iterator IT. IT->object is the value
17657 of the glyph property displayed. The value must be a list
17658 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
17659 being recognized:
17660
17661 1. `:width WIDTH' specifies that the space should be WIDTH *
17662 canonical char width wide. WIDTH may be an integer or floating
17663 point number.
17664
17665 2. `:relative-width FACTOR' specifies that the width of the stretch
17666 should be computed from the width of the first character having the
17667 `glyph' property, and should be FACTOR times that width.
17668
17669 3. `:align-to HPOS' specifies that the space should be wide enough
17670 to reach HPOS, a value in canonical character units.
17671
17672 Exactly one of the above pairs must be present.
17673
17674 4. `:height HEIGHT' specifies that the height of the stretch produced
17675 should be HEIGHT, measured in canonical character units.
17676
17677 5. `:relative-height FACTOR' specifies that the height of the
17678 stretch should be FACTOR times the height of the characters having
17679 the glyph property.
17680
17681 Either none or exactly one of 4 or 5 must be present.
17682
17683 6. `:ascent ASCENT' specifies that ASCENT percent of the height
17684 of the stretch should be used for the ascent of the stretch.
17685 ASCENT must be in the range 0 <= ASCENT <= 100. */
17686
17687 #define NUMVAL(X) \
17688 ((INTEGERP (X) || FLOATP (X)) \
17689 ? XFLOATINT (X) \
17690 : - 1)
17691
17692
17693 static void
17694 produce_stretch_glyph (it)
17695 struct it *it;
17696 {
17697 /* (space :width WIDTH :height HEIGHT. */
17698 Lisp_Object prop, plist;
17699 int width = 0, height = 0;
17700 double ascent = 0;
17701 struct face *face = FACE_FROM_ID (it->f, it->face_id);
17702 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
17703
17704 PREPARE_FACE_FOR_DISPLAY (it->f, face);
17705
17706 /* List should start with `space'. */
17707 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
17708 plist = XCDR (it->object);
17709
17710 /* Compute the width of the stretch. */
17711 if (prop = Fplist_get (plist, QCwidth),
17712 NUMVAL (prop) > 0)
17713 /* Absolute width `:width WIDTH' specified and valid. */
17714 width = NUMVAL (prop) * FRAME_COLUMN_WIDTH (it->f);
17715 else if (prop = Fplist_get (plist, QCrelative_width),
17716 NUMVAL (prop) > 0)
17717 {
17718 /* Relative width `:relative-width FACTOR' specified and valid.
17719 Compute the width of the characters having the `glyph'
17720 property. */
17721 struct it it2;
17722 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
17723
17724 it2 = *it;
17725 if (it->multibyte_p)
17726 {
17727 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
17728 - IT_BYTEPOS (*it));
17729 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
17730 }
17731 else
17732 it2.c = *p, it2.len = 1;
17733
17734 it2.glyph_row = NULL;
17735 it2.what = IT_CHARACTER;
17736 x_produce_glyphs (&it2);
17737 width = NUMVAL (prop) * it2.pixel_width;
17738 }
17739 else if (prop = Fplist_get (plist, QCalign_to),
17740 NUMVAL (prop) > 0)
17741 width = NUMVAL (prop) * FRAME_COLUMN_WIDTH (it->f) - it->current_x;
17742 else
17743 /* Nothing specified -> width defaults to canonical char width. */
17744 width = FRAME_COLUMN_WIDTH (it->f);
17745
17746 /* Compute height. */
17747 if (prop = Fplist_get (plist, QCheight),
17748 NUMVAL (prop) > 0)
17749 height = NUMVAL (prop) * FRAME_LINE_HEIGHT (it->f);
17750 else if (prop = Fplist_get (plist, QCrelative_height),
17751 NUMVAL (prop) > 0)
17752 height = FONT_HEIGHT (font) * NUMVAL (prop);
17753 else
17754 height = FONT_HEIGHT (font);
17755
17756 /* Compute percentage of height used for ascent. If
17757 `:ascent ASCENT' is present and valid, use that. Otherwise,
17758 derive the ascent from the font in use. */
17759 if (prop = Fplist_get (plist, QCascent),
17760 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
17761 ascent = NUMVAL (prop) / 100.0;
17762 else
17763 ascent = (double) FONT_BASE (font) / FONT_HEIGHT (font);
17764
17765 if (width <= 0)
17766 width = 1;
17767 if (height <= 0)
17768 height = 1;
17769
17770 if (it->glyph_row)
17771 {
17772 Lisp_Object object = it->stack[it->sp - 1].string;
17773 if (!STRINGP (object))
17774 object = it->w->buffer;
17775 append_stretch_glyph (it, object, width, height, ascent);
17776 }
17777
17778 it->pixel_width = width;
17779 it->ascent = it->phys_ascent = height * ascent;
17780 it->descent = it->phys_descent = height - it->ascent;
17781 it->nglyphs = 1;
17782
17783 if (face->box != FACE_NO_BOX)
17784 {
17785 if (face->box_line_width > 0)
17786 {
17787 it->ascent += face->box_line_width;
17788 it->descent += face->box_line_width;
17789 }
17790
17791 if (it->start_of_box_run_p)
17792 it->pixel_width += abs (face->box_line_width);
17793 if (it->end_of_box_run_p)
17794 it->pixel_width += abs (face->box_line_width);
17795 }
17796
17797 take_vertical_position_into_account (it);
17798 }
17799
17800 /* RIF:
17801 Produce glyphs/get display metrics for the display element IT is
17802 loaded with. See the description of struct display_iterator in
17803 dispextern.h for an overview of struct display_iterator. */
17804
17805 void
17806 x_produce_glyphs (it)
17807 struct it *it;
17808 {
17809 it->glyph_not_available_p = 0;
17810
17811 if (it->what == IT_CHARACTER)
17812 {
17813 XChar2b char2b;
17814 XFontStruct *font;
17815 struct face *face = FACE_FROM_ID (it->f, it->face_id);
17816 XCharStruct *pcm;
17817 int font_not_found_p;
17818 struct font_info *font_info;
17819 int boff; /* baseline offset */
17820 /* We may change it->multibyte_p upon unibyte<->multibyte
17821 conversion. So, save the current value now and restore it
17822 later.
17823
17824 Note: It seems that we don't have to record multibyte_p in
17825 struct glyph because the character code itself tells if or
17826 not the character is multibyte. Thus, in the future, we must
17827 consider eliminating the field `multibyte_p' in the struct
17828 glyph. */
17829 int saved_multibyte_p = it->multibyte_p;
17830
17831 /* Maybe translate single-byte characters to multibyte, or the
17832 other way. */
17833 it->char_to_display = it->c;
17834 if (!ASCII_BYTE_P (it->c))
17835 {
17836 if (unibyte_display_via_language_environment
17837 && SINGLE_BYTE_CHAR_P (it->c)
17838 && (it->c >= 0240
17839 || !NILP (Vnonascii_translation_table)))
17840 {
17841 it->char_to_display = unibyte_char_to_multibyte (it->c);
17842 it->multibyte_p = 1;
17843 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
17844 face = FACE_FROM_ID (it->f, it->face_id);
17845 }
17846 else if (!SINGLE_BYTE_CHAR_P (it->c)
17847 && !it->multibyte_p)
17848 {
17849 it->multibyte_p = 1;
17850 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
17851 face = FACE_FROM_ID (it->f, it->face_id);
17852 }
17853 }
17854
17855 /* Get font to use. Encode IT->char_to_display. */
17856 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
17857 &char2b, it->multibyte_p, 0);
17858 font = face->font;
17859
17860 /* When no suitable font found, use the default font. */
17861 font_not_found_p = font == NULL;
17862 if (font_not_found_p)
17863 {
17864 font = FRAME_FONT (it->f);
17865 boff = FRAME_BASELINE_OFFSET (it->f);
17866 font_info = NULL;
17867 }
17868 else
17869 {
17870 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
17871 boff = font_info->baseline_offset;
17872 if (font_info->vertical_centering)
17873 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
17874 }
17875
17876 if (it->char_to_display >= ' '
17877 && (!it->multibyte_p || it->char_to_display < 128))
17878 {
17879 /* Either unibyte or ASCII. */
17880 int stretched_p;
17881
17882 it->nglyphs = 1;
17883
17884 pcm = rif->per_char_metric (font, &char2b,
17885 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
17886 it->ascent = FONT_BASE (font) + boff;
17887 it->descent = FONT_DESCENT (font) - boff;
17888
17889 if (pcm)
17890 {
17891 it->phys_ascent = pcm->ascent + boff;
17892 it->phys_descent = pcm->descent - boff;
17893 it->pixel_width = pcm->width;
17894 }
17895 else
17896 {
17897 it->glyph_not_available_p = 1;
17898 it->phys_ascent = FONT_BASE (font) + boff;
17899 it->phys_descent = FONT_DESCENT (font) - boff;
17900 it->pixel_width = FONT_WIDTH (font);
17901 }
17902
17903 /* If this is a space inside a region of text with
17904 `space-width' property, change its width. */
17905 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
17906 if (stretched_p)
17907 it->pixel_width *= XFLOATINT (it->space_width);
17908
17909 /* If face has a box, add the box thickness to the character
17910 height. If character has a box line to the left and/or
17911 right, add the box line width to the character's width. */
17912 if (face->box != FACE_NO_BOX)
17913 {
17914 int thick = face->box_line_width;
17915
17916 if (thick > 0)
17917 {
17918 it->ascent += thick;
17919 it->descent += thick;
17920 }
17921 else
17922 thick = -thick;
17923
17924 if (it->start_of_box_run_p)
17925 it->pixel_width += thick;
17926 if (it->end_of_box_run_p)
17927 it->pixel_width += thick;
17928 }
17929
17930 /* If face has an overline, add the height of the overline
17931 (1 pixel) and a 1 pixel margin to the character height. */
17932 if (face->overline_p)
17933 it->ascent += 2;
17934
17935 take_vertical_position_into_account (it);
17936
17937 /* If we have to actually produce glyphs, do it. */
17938 if (it->glyph_row)
17939 {
17940 if (stretched_p)
17941 {
17942 /* Translate a space with a `space-width' property
17943 into a stretch glyph. */
17944 double ascent = (double) FONT_BASE (font)
17945 / FONT_HEIGHT (font);
17946 append_stretch_glyph (it, it->object, it->pixel_width,
17947 it->ascent + it->descent, ascent);
17948 }
17949 else
17950 append_glyph (it);
17951
17952 /* If characters with lbearing or rbearing are displayed
17953 in this line, record that fact in a flag of the
17954 glyph row. This is used to optimize X output code. */
17955 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
17956 it->glyph_row->contains_overlapping_glyphs_p = 1;
17957 }
17958 }
17959 else if (it->char_to_display == '\n')
17960 {
17961 /* A newline has no width but we need the height of the line. */
17962 it->pixel_width = 0;
17963 it->nglyphs = 0;
17964 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
17965 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
17966
17967 if (face->box != FACE_NO_BOX
17968 && face->box_line_width > 0)
17969 {
17970 it->ascent += face->box_line_width;
17971 it->descent += face->box_line_width;
17972 }
17973 }
17974 else if (it->char_to_display == '\t')
17975 {
17976 int tab_width = it->tab_width * FRAME_COLUMN_WIDTH (it->f);
17977 int x = it->current_x + it->continuation_lines_width;
17978 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
17979
17980 /* If the distance from the current position to the next tab
17981 stop is less than a canonical character width, use the
17982 tab stop after that. */
17983 if (next_tab_x - x < FRAME_COLUMN_WIDTH (it->f))
17984 next_tab_x += tab_width;
17985
17986 it->pixel_width = next_tab_x - x;
17987 it->nglyphs = 1;
17988 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
17989 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
17990
17991 if (it->glyph_row)
17992 {
17993 double ascent = (double) it->ascent / (it->ascent + it->descent);
17994 append_stretch_glyph (it, it->object, it->pixel_width,
17995 it->ascent + it->descent, ascent);
17996 }
17997 }
17998 else
17999 {
18000 /* A multi-byte character. Assume that the display width of the
18001 character is the width of the character multiplied by the
18002 width of the font. */
18003
18004 /* If we found a font, this font should give us the right
18005 metrics. If we didn't find a font, use the frame's
18006 default font and calculate the width of the character
18007 from the charset width; this is what old redisplay code
18008 did. */
18009
18010 pcm = rif->per_char_metric (font, &char2b,
18011 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
18012
18013 if (font_not_found_p || !pcm)
18014 {
18015 int charset = CHAR_CHARSET (it->char_to_display);
18016
18017 it->glyph_not_available_p = 1;
18018 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
18019 * CHARSET_WIDTH (charset));
18020 it->phys_ascent = FONT_BASE (font) + boff;
18021 it->phys_descent = FONT_DESCENT (font) - boff;
18022 }
18023 else
18024 {
18025 it->pixel_width = pcm->width;
18026 it->phys_ascent = pcm->ascent + boff;
18027 it->phys_descent = pcm->descent - boff;
18028 if (it->glyph_row
18029 && (pcm->lbearing < 0
18030 || pcm->rbearing > pcm->width))
18031 it->glyph_row->contains_overlapping_glyphs_p = 1;
18032 }
18033 it->nglyphs = 1;
18034 it->ascent = FONT_BASE (font) + boff;
18035 it->descent = FONT_DESCENT (font) - boff;
18036 if (face->box != FACE_NO_BOX)
18037 {
18038 int thick = face->box_line_width;
18039
18040 if (thick > 0)
18041 {
18042 it->ascent += thick;
18043 it->descent += thick;
18044 }
18045 else
18046 thick = - thick;
18047
18048 if (it->start_of_box_run_p)
18049 it->pixel_width += thick;
18050 if (it->end_of_box_run_p)
18051 it->pixel_width += thick;
18052 }
18053
18054 /* If face has an overline, add the height of the overline
18055 (1 pixel) and a 1 pixel margin to the character height. */
18056 if (face->overline_p)
18057 it->ascent += 2;
18058
18059 take_vertical_position_into_account (it);
18060
18061 if (it->glyph_row)
18062 append_glyph (it);
18063 }
18064 it->multibyte_p = saved_multibyte_p;
18065 }
18066 else if (it->what == IT_COMPOSITION)
18067 {
18068 /* Note: A composition is represented as one glyph in the
18069 glyph matrix. There are no padding glyphs. */
18070 XChar2b char2b;
18071 XFontStruct *font;
18072 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18073 XCharStruct *pcm;
18074 int font_not_found_p;
18075 struct font_info *font_info;
18076 int boff; /* baseline offset */
18077 struct composition *cmp = composition_table[it->cmp_id];
18078
18079 /* Maybe translate single-byte characters to multibyte. */
18080 it->char_to_display = it->c;
18081 if (unibyte_display_via_language_environment
18082 && SINGLE_BYTE_CHAR_P (it->c)
18083 && (it->c >= 0240
18084 || (it->c >= 0200
18085 && !NILP (Vnonascii_translation_table))))
18086 {
18087 it->char_to_display = unibyte_char_to_multibyte (it->c);
18088 }
18089
18090 /* Get face and font to use. Encode IT->char_to_display. */
18091 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18092 face = FACE_FROM_ID (it->f, it->face_id);
18093 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18094 &char2b, it->multibyte_p, 0);
18095 font = face->font;
18096
18097 /* When no suitable font found, use the default font. */
18098 font_not_found_p = font == NULL;
18099 if (font_not_found_p)
18100 {
18101 font = FRAME_FONT (it->f);
18102 boff = FRAME_BASELINE_OFFSET (it->f);
18103 font_info = NULL;
18104 }
18105 else
18106 {
18107 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18108 boff = font_info->baseline_offset;
18109 if (font_info->vertical_centering)
18110 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18111 }
18112
18113 /* There are no padding glyphs, so there is only one glyph to
18114 produce for the composition. Important is that pixel_width,
18115 ascent and descent are the values of what is drawn by
18116 draw_glyphs (i.e. the values of the overall glyphs composed). */
18117 it->nglyphs = 1;
18118
18119 /* If we have not yet calculated pixel size data of glyphs of
18120 the composition for the current face font, calculate them
18121 now. Theoretically, we have to check all fonts for the
18122 glyphs, but that requires much time and memory space. So,
18123 here we check only the font of the first glyph. This leads
18124 to incorrect display very rarely, and C-l (recenter) can
18125 correct the display anyway. */
18126 if (cmp->font != (void *) font)
18127 {
18128 /* Ascent and descent of the font of the first character of
18129 this composition (adjusted by baseline offset). Ascent
18130 and descent of overall glyphs should not be less than
18131 them respectively. */
18132 int font_ascent = FONT_BASE (font) + boff;
18133 int font_descent = FONT_DESCENT (font) - boff;
18134 /* Bounding box of the overall glyphs. */
18135 int leftmost, rightmost, lowest, highest;
18136 int i, width, ascent, descent;
18137
18138 cmp->font = (void *) font;
18139
18140 /* Initialize the bounding box. */
18141 if (font_info
18142 && (pcm = rif->per_char_metric (font, &char2b,
18143 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
18144 {
18145 width = pcm->width;
18146 ascent = pcm->ascent;
18147 descent = pcm->descent;
18148 }
18149 else
18150 {
18151 width = FONT_WIDTH (font);
18152 ascent = FONT_BASE (font);
18153 descent = FONT_DESCENT (font);
18154 }
18155
18156 rightmost = width;
18157 lowest = - descent + boff;
18158 highest = ascent + boff;
18159 leftmost = 0;
18160
18161 if (font_info
18162 && font_info->default_ascent
18163 && CHAR_TABLE_P (Vuse_default_ascent)
18164 && !NILP (Faref (Vuse_default_ascent,
18165 make_number (it->char_to_display))))
18166 highest = font_info->default_ascent + boff;
18167
18168 /* Draw the first glyph at the normal position. It may be
18169 shifted to right later if some other glyphs are drawn at
18170 the left. */
18171 cmp->offsets[0] = 0;
18172 cmp->offsets[1] = boff;
18173
18174 /* Set cmp->offsets for the remaining glyphs. */
18175 for (i = 1; i < cmp->glyph_len; i++)
18176 {
18177 int left, right, btm, top;
18178 int ch = COMPOSITION_GLYPH (cmp, i);
18179 int face_id = FACE_FOR_CHAR (it->f, face, ch);
18180
18181 face = FACE_FROM_ID (it->f, face_id);
18182 get_char_face_and_encoding (it->f, ch, face->id,
18183 &char2b, it->multibyte_p, 0);
18184 font = face->font;
18185 if (font == NULL)
18186 {
18187 font = FRAME_FONT (it->f);
18188 boff = FRAME_BASELINE_OFFSET (it->f);
18189 font_info = NULL;
18190 }
18191 else
18192 {
18193 font_info
18194 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18195 boff = font_info->baseline_offset;
18196 if (font_info->vertical_centering)
18197 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18198 }
18199
18200 if (font_info
18201 && (pcm = rif->per_char_metric (font, &char2b,
18202 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
18203 {
18204 width = pcm->width;
18205 ascent = pcm->ascent;
18206 descent = pcm->descent;
18207 }
18208 else
18209 {
18210 width = FONT_WIDTH (font);
18211 ascent = 1;
18212 descent = 0;
18213 }
18214
18215 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
18216 {
18217 /* Relative composition with or without
18218 alternate chars. */
18219 left = (leftmost + rightmost - width) / 2;
18220 btm = - descent + boff;
18221 if (font_info && font_info->relative_compose
18222 && (! CHAR_TABLE_P (Vignore_relative_composition)
18223 || NILP (Faref (Vignore_relative_composition,
18224 make_number (ch)))))
18225 {
18226
18227 if (- descent >= font_info->relative_compose)
18228 /* One extra pixel between two glyphs. */
18229 btm = highest + 1;
18230 else if (ascent <= 0)
18231 /* One extra pixel between two glyphs. */
18232 btm = lowest - 1 - ascent - descent;
18233 }
18234 }
18235 else
18236 {
18237 /* A composition rule is specified by an integer
18238 value that encodes global and new reference
18239 points (GREF and NREF). GREF and NREF are
18240 specified by numbers as below:
18241
18242 0---1---2 -- ascent
18243 | |
18244 | |
18245 | |
18246 9--10--11 -- center
18247 | |
18248 ---3---4---5--- baseline
18249 | |
18250 6---7---8 -- descent
18251 */
18252 int rule = COMPOSITION_RULE (cmp, i);
18253 int gref, nref, grefx, grefy, nrefx, nrefy;
18254
18255 COMPOSITION_DECODE_RULE (rule, gref, nref);
18256 grefx = gref % 3, nrefx = nref % 3;
18257 grefy = gref / 3, nrefy = nref / 3;
18258
18259 left = (leftmost
18260 + grefx * (rightmost - leftmost) / 2
18261 - nrefx * width / 2);
18262 btm = ((grefy == 0 ? highest
18263 : grefy == 1 ? 0
18264 : grefy == 2 ? lowest
18265 : (highest + lowest) / 2)
18266 - (nrefy == 0 ? ascent + descent
18267 : nrefy == 1 ? descent - boff
18268 : nrefy == 2 ? 0
18269 : (ascent + descent) / 2));
18270 }
18271
18272 cmp->offsets[i * 2] = left;
18273 cmp->offsets[i * 2 + 1] = btm + descent;
18274
18275 /* Update the bounding box of the overall glyphs. */
18276 right = left + width;
18277 top = btm + descent + ascent;
18278 if (left < leftmost)
18279 leftmost = left;
18280 if (right > rightmost)
18281 rightmost = right;
18282 if (top > highest)
18283 highest = top;
18284 if (btm < lowest)
18285 lowest = btm;
18286 }
18287
18288 /* If there are glyphs whose x-offsets are negative,
18289 shift all glyphs to the right and make all x-offsets
18290 non-negative. */
18291 if (leftmost < 0)
18292 {
18293 for (i = 0; i < cmp->glyph_len; i++)
18294 cmp->offsets[i * 2] -= leftmost;
18295 rightmost -= leftmost;
18296 }
18297
18298 cmp->pixel_width = rightmost;
18299 cmp->ascent = highest;
18300 cmp->descent = - lowest;
18301 if (cmp->ascent < font_ascent)
18302 cmp->ascent = font_ascent;
18303 if (cmp->descent < font_descent)
18304 cmp->descent = font_descent;
18305 }
18306
18307 it->pixel_width = cmp->pixel_width;
18308 it->ascent = it->phys_ascent = cmp->ascent;
18309 it->descent = it->phys_descent = cmp->descent;
18310
18311 if (face->box != FACE_NO_BOX)
18312 {
18313 int thick = face->box_line_width;
18314
18315 if (thick > 0)
18316 {
18317 it->ascent += thick;
18318 it->descent += thick;
18319 }
18320 else
18321 thick = - thick;
18322
18323 if (it->start_of_box_run_p)
18324 it->pixel_width += thick;
18325 if (it->end_of_box_run_p)
18326 it->pixel_width += thick;
18327 }
18328
18329 /* If face has an overline, add the height of the overline
18330 (1 pixel) and a 1 pixel margin to the character height. */
18331 if (face->overline_p)
18332 it->ascent += 2;
18333
18334 take_vertical_position_into_account (it);
18335
18336 if (it->glyph_row)
18337 append_composite_glyph (it);
18338 }
18339 else if (it->what == IT_IMAGE)
18340 produce_image_glyph (it);
18341 else if (it->what == IT_STRETCH)
18342 produce_stretch_glyph (it);
18343
18344 /* Accumulate dimensions. Note: can't assume that it->descent > 0
18345 because this isn't true for images with `:ascent 100'. */
18346 xassert (it->ascent >= 0 && it->descent >= 0);
18347 if (it->area == TEXT_AREA)
18348 it->current_x += it->pixel_width;
18349
18350 it->descent += it->extra_line_spacing;
18351
18352 it->max_ascent = max (it->max_ascent, it->ascent);
18353 it->max_descent = max (it->max_descent, it->descent);
18354 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
18355 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
18356 }
18357
18358 /* EXPORT for RIF:
18359 Output LEN glyphs starting at START at the nominal cursor position.
18360 Advance the nominal cursor over the text. The global variable
18361 updated_window contains the window being updated, updated_row is
18362 the glyph row being updated, and updated_area is the area of that
18363 row being updated. */
18364
18365 void
18366 x_write_glyphs (start, len)
18367 struct glyph *start;
18368 int len;
18369 {
18370 int x, hpos;
18371
18372 xassert (updated_window && updated_row);
18373 BLOCK_INPUT;
18374
18375 /* Write glyphs. */
18376
18377 hpos = start - updated_row->glyphs[updated_area];
18378 x = draw_glyphs (updated_window, output_cursor.x,
18379 updated_row, updated_area,
18380 hpos, hpos + len,
18381 DRAW_NORMAL_TEXT, 0);
18382
18383 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
18384 if (updated_area == TEXT_AREA
18385 && updated_window->phys_cursor_on_p
18386 && updated_window->phys_cursor.vpos == output_cursor.vpos
18387 && updated_window->phys_cursor.hpos >= hpos
18388 && updated_window->phys_cursor.hpos < hpos + len)
18389 updated_window->phys_cursor_on_p = 0;
18390
18391 UNBLOCK_INPUT;
18392
18393 /* Advance the output cursor. */
18394 output_cursor.hpos += len;
18395 output_cursor.x = x;
18396 }
18397
18398
18399 /* EXPORT for RIF:
18400 Insert LEN glyphs from START at the nominal cursor position. */
18401
18402 void
18403 x_insert_glyphs (start, len)
18404 struct glyph *start;
18405 int len;
18406 {
18407 struct frame *f;
18408 struct window *w;
18409 int line_height, shift_by_width, shifted_region_width;
18410 struct glyph_row *row;
18411 struct glyph *glyph;
18412 int frame_x, frame_y, hpos;
18413
18414 xassert (updated_window && updated_row);
18415 BLOCK_INPUT;
18416 w = updated_window;
18417 f = XFRAME (WINDOW_FRAME (w));
18418
18419 /* Get the height of the line we are in. */
18420 row = updated_row;
18421 line_height = row->height;
18422
18423 /* Get the width of the glyphs to insert. */
18424 shift_by_width = 0;
18425 for (glyph = start; glyph < start + len; ++glyph)
18426 shift_by_width += glyph->pixel_width;
18427
18428 /* Get the width of the region to shift right. */
18429 shifted_region_width = (window_box_width (w, updated_area)
18430 - output_cursor.x
18431 - shift_by_width);
18432
18433 /* Shift right. */
18434 frame_x = window_box_left (w, updated_area) + output_cursor.x;
18435 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
18436
18437 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
18438 line_height, shift_by_width);
18439
18440 /* Write the glyphs. */
18441 hpos = start - row->glyphs[updated_area];
18442 draw_glyphs (w, output_cursor.x, row, updated_area,
18443 hpos, hpos + len,
18444 DRAW_NORMAL_TEXT, 0);
18445
18446 /* Advance the output cursor. */
18447 output_cursor.hpos += len;
18448 output_cursor.x += shift_by_width;
18449 UNBLOCK_INPUT;
18450 }
18451
18452
18453 /* EXPORT for RIF:
18454 Erase the current text line from the nominal cursor position
18455 (inclusive) to pixel column TO_X (exclusive). The idea is that
18456 everything from TO_X onward is already erased.
18457
18458 TO_X is a pixel position relative to updated_area of
18459 updated_window. TO_X == -1 means clear to the end of this area. */
18460
18461 void
18462 x_clear_end_of_line (to_x)
18463 int to_x;
18464 {
18465 struct frame *f;
18466 struct window *w = updated_window;
18467 int max_x, min_y, max_y;
18468 int from_x, from_y, to_y;
18469
18470 xassert (updated_window && updated_row);
18471 f = XFRAME (w->frame);
18472
18473 if (updated_row->full_width_p)
18474 max_x = WINDOW_TOTAL_WIDTH (w);
18475 else
18476 max_x = window_box_width (w, updated_area);
18477 max_y = window_text_bottom_y (w);
18478
18479 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
18480 of window. For TO_X > 0, truncate to end of drawing area. */
18481 if (to_x == 0)
18482 return;
18483 else if (to_x < 0)
18484 to_x = max_x;
18485 else
18486 to_x = min (to_x, max_x);
18487
18488 to_y = min (max_y, output_cursor.y + updated_row->height);
18489
18490 /* Notice if the cursor will be cleared by this operation. */
18491 if (!updated_row->full_width_p)
18492 notice_overwritten_cursor (w, updated_area,
18493 output_cursor.x, -1,
18494 updated_row->y,
18495 MATRIX_ROW_BOTTOM_Y (updated_row));
18496
18497 from_x = output_cursor.x;
18498
18499 /* Translate to frame coordinates. */
18500 if (updated_row->full_width_p)
18501 {
18502 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
18503 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
18504 }
18505 else
18506 {
18507 int area_left = window_box_left (w, updated_area);
18508 from_x += area_left;
18509 to_x += area_left;
18510 }
18511
18512 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
18513 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
18514 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
18515
18516 /* Prevent inadvertently clearing to end of the X window. */
18517 if (to_x > from_x && to_y > from_y)
18518 {
18519 BLOCK_INPUT;
18520 rif->clear_frame_area (f, from_x, from_y,
18521 to_x - from_x, to_y - from_y);
18522 UNBLOCK_INPUT;
18523 }
18524 }
18525
18526 #endif /* HAVE_WINDOW_SYSTEM */
18527
18528
18529 \f
18530 /***********************************************************************
18531 Cursor types
18532 ***********************************************************************/
18533
18534 /* Value is the internal representation of the specified cursor type
18535 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
18536 of the bar cursor. */
18537
18538 enum text_cursor_kinds
18539 get_specified_cursor_type (arg, width)
18540 Lisp_Object arg;
18541 int *width;
18542 {
18543 enum text_cursor_kinds type;
18544
18545 if (NILP (arg))
18546 return NO_CURSOR;
18547
18548 if (EQ (arg, Qbox))
18549 return FILLED_BOX_CURSOR;
18550
18551 if (EQ (arg, Qhollow))
18552 return HOLLOW_BOX_CURSOR;
18553
18554 if (EQ (arg, Qbar))
18555 {
18556 *width = 2;
18557 return BAR_CURSOR;
18558 }
18559
18560 if (CONSP (arg)
18561 && EQ (XCAR (arg), Qbar)
18562 && INTEGERP (XCDR (arg))
18563 && XINT (XCDR (arg)) >= 0)
18564 {
18565 *width = XINT (XCDR (arg));
18566 return BAR_CURSOR;
18567 }
18568
18569 if (EQ (arg, Qhbar))
18570 {
18571 *width = 2;
18572 return HBAR_CURSOR;
18573 }
18574
18575 if (CONSP (arg)
18576 && EQ (XCAR (arg), Qhbar)
18577 && INTEGERP (XCDR (arg))
18578 && XINT (XCDR (arg)) >= 0)
18579 {
18580 *width = XINT (XCDR (arg));
18581 return HBAR_CURSOR;
18582 }
18583
18584 /* Treat anything unknown as "hollow box cursor".
18585 It was bad to signal an error; people have trouble fixing
18586 .Xdefaults with Emacs, when it has something bad in it. */
18587 type = HOLLOW_BOX_CURSOR;
18588
18589 return type;
18590 }
18591
18592 /* Set the default cursor types for specified frame. */
18593 void
18594 set_frame_cursor_types (f, arg)
18595 struct frame *f;
18596 Lisp_Object arg;
18597 {
18598 int width;
18599 Lisp_Object tem;
18600
18601 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
18602 FRAME_CURSOR_WIDTH (f) = width;
18603
18604 /* By default, set up the blink-off state depending on the on-state. */
18605
18606 tem = Fassoc (arg, Vblink_cursor_alist);
18607 if (!NILP (tem))
18608 {
18609 FRAME_BLINK_OFF_CURSOR (f)
18610 = get_specified_cursor_type (XCDR (tem), &width);
18611 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
18612 }
18613 else
18614 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
18615 }
18616
18617
18618 /* Return the cursor we want to be displayed in window W. Return
18619 width of bar/hbar cursor through WIDTH arg. Return with
18620 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
18621 (i.e. if the `system caret' should track this cursor).
18622
18623 In a mini-buffer window, we want the cursor only to appear if we
18624 are reading input from this window. For the selected window, we
18625 want the cursor type given by the frame parameter or buffer local
18626 setting of cursor-type. If explicitly marked off, draw no cursor.
18627 In all other cases, we want a hollow box cursor. */
18628
18629 enum text_cursor_kinds
18630 get_window_cursor_type (w, width, active_cursor)
18631 struct window *w;
18632 int *width;
18633 int *active_cursor;
18634 {
18635 struct frame *f = XFRAME (w->frame);
18636 struct buffer *b = XBUFFER (w->buffer);
18637 int cursor_type = DEFAULT_CURSOR;
18638 Lisp_Object alt_cursor;
18639 int non_selected = 0;
18640
18641 *active_cursor = 1;
18642
18643 /* Echo area */
18644 if (cursor_in_echo_area
18645 && FRAME_HAS_MINIBUF_P (f)
18646 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
18647 {
18648 if (w == XWINDOW (echo_area_window))
18649 {
18650 *width = FRAME_CURSOR_WIDTH (f);
18651 return FRAME_DESIRED_CURSOR (f);
18652 }
18653
18654 *active_cursor = 0;
18655 non_selected = 1;
18656 }
18657
18658 /* Nonselected window or nonselected frame. */
18659 else if (w != XWINDOW (f->selected_window)
18660 #ifdef HAVE_WINDOW_SYSTEM
18661 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
18662 #endif
18663 )
18664 {
18665 *active_cursor = 0;
18666
18667 if (MINI_WINDOW_P (w) && minibuf_level == 0)
18668 return NO_CURSOR;
18669
18670 non_selected = 1;
18671 }
18672
18673 /* Never display a cursor in a window in which cursor-type is nil. */
18674 if (NILP (b->cursor_type))
18675 return NO_CURSOR;
18676
18677 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
18678 if (non_selected)
18679 {
18680 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
18681 return get_specified_cursor_type (alt_cursor, width);
18682 }
18683
18684 /* Get the normal cursor type for this window. */
18685 if (EQ (b->cursor_type, Qt))
18686 {
18687 cursor_type = FRAME_DESIRED_CURSOR (f);
18688 *width = FRAME_CURSOR_WIDTH (f);
18689 }
18690 else
18691 cursor_type = get_specified_cursor_type (b->cursor_type, width);
18692
18693 /* Use normal cursor if not blinked off. */
18694 if (!w->cursor_off_p)
18695 return cursor_type;
18696
18697 /* Cursor is blinked off, so determine how to "toggle" it. */
18698
18699 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
18700 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
18701 return get_specified_cursor_type (XCDR (alt_cursor), width);
18702
18703 /* Then see if frame has specified a specific blink off cursor type. */
18704 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
18705 {
18706 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
18707 return FRAME_BLINK_OFF_CURSOR (f);
18708 }
18709
18710 /* Finally perform built-in cursor blinking:
18711 filled box <-> hollow box
18712 wide [h]bar <-> narrow [h]bar
18713 narrow [h]bar <-> no cursor
18714 other type <-> no cursor */
18715
18716 if (cursor_type == FILLED_BOX_CURSOR)
18717 return HOLLOW_BOX_CURSOR;
18718
18719 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
18720 {
18721 *width = 1;
18722 return cursor_type;
18723 }
18724
18725 return NO_CURSOR;
18726 }
18727
18728
18729 #ifdef HAVE_WINDOW_SYSTEM
18730
18731 /* Notice when the text cursor of window W has been completely
18732 overwritten by a drawing operation that outputs glyphs in AREA
18733 starting at X0 and ending at X1 in the line starting at Y0 and
18734 ending at Y1. X coordinates are area-relative. X1 < 0 means all
18735 the rest of the line after X0 has been written. Y coordinates
18736 are window-relative. */
18737
18738 static void
18739 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
18740 struct window *w;
18741 enum glyph_row_area area;
18742 int x0, y0, x1, y1;
18743 {
18744 if (area == TEXT_AREA && w->phys_cursor_on_p)
18745 {
18746 int cx0 = w->phys_cursor.x;
18747 int cx1 = cx0 + w->phys_cursor_width;
18748 int cy0 = w->phys_cursor.y;
18749 int cy1 = cy0 + w->phys_cursor_height;
18750
18751 if (x0 <= cx0 && (x1 < 0 || x1 >= cx1))
18752 {
18753 /* The cursor image will be completely removed from the
18754 screen if the output area intersects the cursor area in
18755 y-direction. When we draw in [y0 y1[, and some part of
18756 the cursor is at y < y0, that part must have been drawn
18757 before. When scrolling, the cursor is erased before
18758 actually scrolling, so we don't come here. When not
18759 scrolling, the rows above the old cursor row must have
18760 changed, and in this case these rows must have written
18761 over the cursor image.
18762
18763 Likewise if part of the cursor is below y1, with the
18764 exception of the cursor being in the first blank row at
18765 the buffer and window end because update_text_area
18766 doesn't draw that row. (Except when it does, but
18767 that's handled in update_text_area.) */
18768
18769 if (((y0 >= cy0 && y0 < cy1) || (y1 > cy0 && y1 < cy1))
18770 && w->current_matrix->rows[w->phys_cursor.vpos].displays_text_p)
18771 w->phys_cursor_on_p = 0;
18772 }
18773 }
18774 }
18775
18776 #endif /* HAVE_WINDOW_SYSTEM */
18777
18778 \f
18779 /************************************************************************
18780 Mouse Face
18781 ************************************************************************/
18782
18783 #ifdef HAVE_WINDOW_SYSTEM
18784
18785 /* EXPORT for RIF:
18786 Fix the display of area AREA of overlapping row ROW in window W. */
18787
18788 void
18789 x_fix_overlapping_area (w, row, area)
18790 struct window *w;
18791 struct glyph_row *row;
18792 enum glyph_row_area area;
18793 {
18794 int i, x;
18795
18796 BLOCK_INPUT;
18797
18798 x = 0;
18799 for (i = 0; i < row->used[area];)
18800 {
18801 if (row->glyphs[area][i].overlaps_vertically_p)
18802 {
18803 int start = i, start_x = x;
18804
18805 do
18806 {
18807 x += row->glyphs[area][i].pixel_width;
18808 ++i;
18809 }
18810 while (i < row->used[area]
18811 && row->glyphs[area][i].overlaps_vertically_p);
18812
18813 draw_glyphs (w, start_x, row, area,
18814 start, i,
18815 DRAW_NORMAL_TEXT, 1);
18816 }
18817 else
18818 {
18819 x += row->glyphs[area][i].pixel_width;
18820 ++i;
18821 }
18822 }
18823
18824 UNBLOCK_INPUT;
18825 }
18826
18827
18828 /* EXPORT:
18829 Draw the cursor glyph of window W in glyph row ROW. See the
18830 comment of draw_glyphs for the meaning of HL. */
18831
18832 void
18833 draw_phys_cursor_glyph (w, row, hl)
18834 struct window *w;
18835 struct glyph_row *row;
18836 enum draw_glyphs_face hl;
18837 {
18838 /* If cursor hpos is out of bounds, don't draw garbage. This can
18839 happen in mini-buffer windows when switching between echo area
18840 glyphs and mini-buffer. */
18841 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
18842 {
18843 int on_p = w->phys_cursor_on_p;
18844 int x1;
18845 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
18846 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
18847 hl, 0);
18848 w->phys_cursor_on_p = on_p;
18849
18850 if (hl == DRAW_CURSOR)
18851 w->phys_cursor_width = x1 - w->phys_cursor.x;
18852 /* When we erase the cursor, and ROW is overlapped by other
18853 rows, make sure that these overlapping parts of other rows
18854 are redrawn. */
18855 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
18856 {
18857 if (row > w->current_matrix->rows
18858 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
18859 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
18860
18861 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
18862 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
18863 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
18864 }
18865 }
18866 }
18867
18868
18869 /* EXPORT:
18870 Erase the image of a cursor of window W from the screen. */
18871
18872 void
18873 erase_phys_cursor (w)
18874 struct window *w;
18875 {
18876 struct frame *f = XFRAME (w->frame);
18877 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
18878 int hpos = w->phys_cursor.hpos;
18879 int vpos = w->phys_cursor.vpos;
18880 int mouse_face_here_p = 0;
18881 struct glyph_matrix *active_glyphs = w->current_matrix;
18882 struct glyph_row *cursor_row;
18883 struct glyph *cursor_glyph;
18884 enum draw_glyphs_face hl;
18885
18886 /* No cursor displayed or row invalidated => nothing to do on the
18887 screen. */
18888 if (w->phys_cursor_type == NO_CURSOR)
18889 goto mark_cursor_off;
18890
18891 /* VPOS >= active_glyphs->nrows means that window has been resized.
18892 Don't bother to erase the cursor. */
18893 if (vpos >= active_glyphs->nrows)
18894 goto mark_cursor_off;
18895
18896 /* If row containing cursor is marked invalid, there is nothing we
18897 can do. */
18898 cursor_row = MATRIX_ROW (active_glyphs, vpos);
18899 if (!cursor_row->enabled_p)
18900 goto mark_cursor_off;
18901
18902 /* If row is completely invisible, don't attempt to delete a cursor which
18903 isn't there. This can happen if cursor is at top of a window, and
18904 we switch to a buffer with a header line in that window. */
18905 if (cursor_row->visible_height <= 0)
18906 goto mark_cursor_off;
18907
18908 /* This can happen when the new row is shorter than the old one.
18909 In this case, either draw_glyphs or clear_end_of_line
18910 should have cleared the cursor. Note that we wouldn't be
18911 able to erase the cursor in this case because we don't have a
18912 cursor glyph at hand. */
18913 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
18914 goto mark_cursor_off;
18915
18916 /* If the cursor is in the mouse face area, redisplay that when
18917 we clear the cursor. */
18918 if (! NILP (dpyinfo->mouse_face_window)
18919 && w == XWINDOW (dpyinfo->mouse_face_window)
18920 && (vpos > dpyinfo->mouse_face_beg_row
18921 || (vpos == dpyinfo->mouse_face_beg_row
18922 && hpos >= dpyinfo->mouse_face_beg_col))
18923 && (vpos < dpyinfo->mouse_face_end_row
18924 || (vpos == dpyinfo->mouse_face_end_row
18925 && hpos < dpyinfo->mouse_face_end_col))
18926 /* Don't redraw the cursor's spot in mouse face if it is at the
18927 end of a line (on a newline). The cursor appears there, but
18928 mouse highlighting does not. */
18929 && cursor_row->used[TEXT_AREA] > hpos)
18930 mouse_face_here_p = 1;
18931
18932 /* Maybe clear the display under the cursor. */
18933 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
18934 {
18935 int x, y;
18936 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
18937
18938 cursor_glyph = get_phys_cursor_glyph (w);
18939 if (cursor_glyph == NULL)
18940 goto mark_cursor_off;
18941
18942 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
18943 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
18944
18945 rif->clear_frame_area (f, x, y,
18946 cursor_glyph->pixel_width, cursor_row->visible_height);
18947 }
18948
18949 /* Erase the cursor by redrawing the character underneath it. */
18950 if (mouse_face_here_p)
18951 hl = DRAW_MOUSE_FACE;
18952 else
18953 hl = DRAW_NORMAL_TEXT;
18954 draw_phys_cursor_glyph (w, cursor_row, hl);
18955
18956 mark_cursor_off:
18957 w->phys_cursor_on_p = 0;
18958 w->phys_cursor_type = NO_CURSOR;
18959 }
18960
18961
18962 /* EXPORT:
18963 Display or clear cursor of window W. If ON is zero, clear the
18964 cursor. If it is non-zero, display the cursor. If ON is nonzero,
18965 where to put the cursor is specified by HPOS, VPOS, X and Y. */
18966
18967 void
18968 display_and_set_cursor (w, on, hpos, vpos, x, y)
18969 struct window *w;
18970 int on, hpos, vpos, x, y;
18971 {
18972 struct frame *f = XFRAME (w->frame);
18973 int new_cursor_type;
18974 int new_cursor_width;
18975 int active_cursor;
18976 struct glyph_matrix *current_glyphs;
18977 struct glyph_row *glyph_row;
18978 struct glyph *glyph;
18979
18980 /* This is pointless on invisible frames, and dangerous on garbaged
18981 windows and frames; in the latter case, the frame or window may
18982 be in the midst of changing its size, and x and y may be off the
18983 window. */
18984 if (! FRAME_VISIBLE_P (f)
18985 || FRAME_GARBAGED_P (f)
18986 || vpos >= w->current_matrix->nrows
18987 || hpos >= w->current_matrix->matrix_w)
18988 return;
18989
18990 /* If cursor is off and we want it off, return quickly. */
18991 if (!on && !w->phys_cursor_on_p)
18992 return;
18993
18994 current_glyphs = w->current_matrix;
18995 glyph_row = MATRIX_ROW (current_glyphs, vpos);
18996 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
18997
18998 /* If cursor row is not enabled, we don't really know where to
18999 display the cursor. */
19000 if (!glyph_row->enabled_p)
19001 {
19002 w->phys_cursor_on_p = 0;
19003 return;
19004 }
19005
19006 xassert (interrupt_input_blocked);
19007
19008 /* Set new_cursor_type to the cursor we want to be displayed. */
19009 new_cursor_type = get_window_cursor_type (w, &new_cursor_width, &active_cursor);
19010
19011 /* If cursor is currently being shown and we don't want it to be or
19012 it is in the wrong place, or the cursor type is not what we want,
19013 erase it. */
19014 if (w->phys_cursor_on_p
19015 && (!on
19016 || w->phys_cursor.x != x
19017 || w->phys_cursor.y != y
19018 || new_cursor_type != w->phys_cursor_type
19019 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
19020 && new_cursor_width != w->phys_cursor_width)))
19021 erase_phys_cursor (w);
19022
19023 /* Don't check phys_cursor_on_p here because that flag is only set
19024 to zero in some cases where we know that the cursor has been
19025 completely erased, to avoid the extra work of erasing the cursor
19026 twice. In other words, phys_cursor_on_p can be 1 and the cursor
19027 still not be visible, or it has only been partly erased. */
19028 if (on)
19029 {
19030 w->phys_cursor_ascent = glyph_row->ascent;
19031 w->phys_cursor_height = glyph_row->height;
19032
19033 /* Set phys_cursor_.* before x_draw_.* is called because some
19034 of them may need the information. */
19035 w->phys_cursor.x = x;
19036 w->phys_cursor.y = glyph_row->y;
19037 w->phys_cursor.hpos = hpos;
19038 w->phys_cursor.vpos = vpos;
19039 }
19040
19041 rif->draw_window_cursor (w, glyph_row, x, y,
19042 new_cursor_type, new_cursor_width,
19043 on, active_cursor);
19044 }
19045
19046
19047 /* Switch the display of W's cursor on or off, according to the value
19048 of ON. */
19049
19050 static void
19051 update_window_cursor (w, on)
19052 struct window *w;
19053 int on;
19054 {
19055 /* Don't update cursor in windows whose frame is in the process
19056 of being deleted. */
19057 if (w->current_matrix)
19058 {
19059 BLOCK_INPUT;
19060 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
19061 w->phys_cursor.x, w->phys_cursor.y);
19062 UNBLOCK_INPUT;
19063 }
19064 }
19065
19066
19067 /* Call update_window_cursor with parameter ON_P on all leaf windows
19068 in the window tree rooted at W. */
19069
19070 static void
19071 update_cursor_in_window_tree (w, on_p)
19072 struct window *w;
19073 int on_p;
19074 {
19075 while (w)
19076 {
19077 if (!NILP (w->hchild))
19078 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
19079 else if (!NILP (w->vchild))
19080 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
19081 else
19082 update_window_cursor (w, on_p);
19083
19084 w = NILP (w->next) ? 0 : XWINDOW (w->next);
19085 }
19086 }
19087
19088
19089 /* EXPORT:
19090 Display the cursor on window W, or clear it, according to ON_P.
19091 Don't change the cursor's position. */
19092
19093 void
19094 x_update_cursor (f, on_p)
19095 struct frame *f;
19096 int on_p;
19097 {
19098 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
19099 }
19100
19101
19102 /* EXPORT:
19103 Clear the cursor of window W to background color, and mark the
19104 cursor as not shown. This is used when the text where the cursor
19105 is is about to be rewritten. */
19106
19107 void
19108 x_clear_cursor (w)
19109 struct window *w;
19110 {
19111 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
19112 update_window_cursor (w, 0);
19113 }
19114
19115
19116 /* EXPORT:
19117 Display the active region described by mouse_face_* according to DRAW. */
19118
19119 void
19120 show_mouse_face (dpyinfo, draw)
19121 Display_Info *dpyinfo;
19122 enum draw_glyphs_face draw;
19123 {
19124 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
19125 struct frame *f = XFRAME (WINDOW_FRAME (w));
19126
19127 if (/* If window is in the process of being destroyed, don't bother
19128 to do anything. */
19129 w->current_matrix != NULL
19130 /* Don't update mouse highlight if hidden */
19131 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
19132 /* Recognize when we are called to operate on rows that don't exist
19133 anymore. This can happen when a window is split. */
19134 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
19135 {
19136 int phys_cursor_on_p = w->phys_cursor_on_p;
19137 struct glyph_row *row, *first, *last;
19138
19139 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
19140 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
19141
19142 for (row = first; row <= last && row->enabled_p; ++row)
19143 {
19144 int start_hpos, end_hpos, start_x;
19145
19146 /* For all but the first row, the highlight starts at column 0. */
19147 if (row == first)
19148 {
19149 start_hpos = dpyinfo->mouse_face_beg_col;
19150 start_x = dpyinfo->mouse_face_beg_x;
19151 }
19152 else
19153 {
19154 start_hpos = 0;
19155 start_x = 0;
19156 }
19157
19158 if (row == last)
19159 end_hpos = dpyinfo->mouse_face_end_col;
19160 else
19161 end_hpos = row->used[TEXT_AREA];
19162
19163 if (end_hpos > start_hpos)
19164 {
19165 draw_glyphs (w, start_x, row, TEXT_AREA,
19166 start_hpos, end_hpos,
19167 draw, 0);
19168
19169 row->mouse_face_p
19170 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
19171 }
19172 }
19173
19174 /* When we've written over the cursor, arrange for it to
19175 be displayed again. */
19176 if (phys_cursor_on_p && !w->phys_cursor_on_p)
19177 {
19178 BLOCK_INPUT;
19179 display_and_set_cursor (w, 1,
19180 w->phys_cursor.hpos, w->phys_cursor.vpos,
19181 w->phys_cursor.x, w->phys_cursor.y);
19182 UNBLOCK_INPUT;
19183 }
19184 }
19185
19186 /* Change the mouse cursor. */
19187 if (draw == DRAW_NORMAL_TEXT)
19188 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
19189 else if (draw == DRAW_MOUSE_FACE)
19190 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
19191 else
19192 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
19193 }
19194
19195 /* EXPORT:
19196 Clear out the mouse-highlighted active region.
19197 Redraw it un-highlighted first. Value is non-zero if mouse
19198 face was actually drawn unhighlighted. */
19199
19200 int
19201 clear_mouse_face (dpyinfo)
19202 Display_Info *dpyinfo;
19203 {
19204 int cleared = 0;
19205
19206 if (!NILP (dpyinfo->mouse_face_window))
19207 {
19208 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
19209 cleared = 1;
19210 }
19211
19212 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
19213 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
19214 dpyinfo->mouse_face_window = Qnil;
19215 dpyinfo->mouse_face_overlay = Qnil;
19216 return cleared;
19217 }
19218
19219
19220 /* EXPORT:
19221 Non-zero if physical cursor of window W is within mouse face. */
19222
19223 int
19224 cursor_in_mouse_face_p (w)
19225 struct window *w;
19226 {
19227 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
19228 int in_mouse_face = 0;
19229
19230 if (WINDOWP (dpyinfo->mouse_face_window)
19231 && XWINDOW (dpyinfo->mouse_face_window) == w)
19232 {
19233 int hpos = w->phys_cursor.hpos;
19234 int vpos = w->phys_cursor.vpos;
19235
19236 if (vpos >= dpyinfo->mouse_face_beg_row
19237 && vpos <= dpyinfo->mouse_face_end_row
19238 && (vpos > dpyinfo->mouse_face_beg_row
19239 || hpos >= dpyinfo->mouse_face_beg_col)
19240 && (vpos < dpyinfo->mouse_face_end_row
19241 || hpos < dpyinfo->mouse_face_end_col
19242 || dpyinfo->mouse_face_past_end))
19243 in_mouse_face = 1;
19244 }
19245
19246 return in_mouse_face;
19247 }
19248
19249
19250
19251 \f
19252 /* Find the glyph matrix position of buffer position CHARPOS in window
19253 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
19254 current glyphs must be up to date. If CHARPOS is above window
19255 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
19256 of last line in W. In the row containing CHARPOS, stop before glyphs
19257 having STOP as object. */
19258
19259 #if 1 /* This is a version of fast_find_position that's more correct
19260 in the presence of hscrolling, for example. I didn't install
19261 it right away because the problem fixed is minor, it failed
19262 in 20.x as well, and I think it's too risky to install
19263 so near the release of 21.1. 2001-09-25 gerd. */
19264
19265 static int
19266 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
19267 struct window *w;
19268 int charpos;
19269 int *hpos, *vpos, *x, *y;
19270 Lisp_Object stop;
19271 {
19272 struct glyph_row *row, *first;
19273 struct glyph *glyph, *end;
19274 int past_end = 0;
19275
19276 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19277 row = row_containing_pos (w, charpos, first, NULL, 0);
19278 if (row == NULL)
19279 {
19280 if (charpos < MATRIX_ROW_START_CHARPOS (first))
19281 {
19282 *x = *y = *hpos = *vpos = 0;
19283 return 0;
19284 }
19285 else
19286 {
19287 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
19288 past_end = 1;
19289 }
19290 }
19291
19292 *x = row->x;
19293 *y = row->y;
19294 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
19295
19296 glyph = row->glyphs[TEXT_AREA];
19297 end = glyph + row->used[TEXT_AREA];
19298
19299 /* Skip over glyphs not having an object at the start of the row.
19300 These are special glyphs like truncation marks on terminal
19301 frames. */
19302 if (row->displays_text_p)
19303 while (glyph < end
19304 && INTEGERP (glyph->object)
19305 && !EQ (stop, glyph->object)
19306 && glyph->charpos < 0)
19307 {
19308 *x += glyph->pixel_width;
19309 ++glyph;
19310 }
19311
19312 while (glyph < end
19313 && !INTEGERP (glyph->object)
19314 && !EQ (stop, glyph->object)
19315 && (!BUFFERP (glyph->object)
19316 || glyph->charpos < charpos))
19317 {
19318 *x += glyph->pixel_width;
19319 ++glyph;
19320 }
19321
19322 *hpos = glyph - row->glyphs[TEXT_AREA];
19323 return past_end;
19324 }
19325
19326 #else /* not 1 */
19327
19328 static int
19329 fast_find_position (w, pos, hpos, vpos, x, y, stop)
19330 struct window *w;
19331 int pos;
19332 int *hpos, *vpos, *x, *y;
19333 Lisp_Object stop;
19334 {
19335 int i;
19336 int lastcol;
19337 int maybe_next_line_p = 0;
19338 int line_start_position;
19339 int yb = window_text_bottom_y (w);
19340 struct glyph_row *row, *best_row;
19341 int row_vpos, best_row_vpos;
19342 int current_x;
19343
19344 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19345 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
19346
19347 while (row->y < yb)
19348 {
19349 if (row->used[TEXT_AREA])
19350 line_start_position = row->glyphs[TEXT_AREA]->charpos;
19351 else
19352 line_start_position = 0;
19353
19354 if (line_start_position > pos)
19355 break;
19356 /* If the position sought is the end of the buffer,
19357 don't include the blank lines at the bottom of the window. */
19358 else if (line_start_position == pos
19359 && pos == BUF_ZV (XBUFFER (w->buffer)))
19360 {
19361 maybe_next_line_p = 1;
19362 break;
19363 }
19364 else if (line_start_position > 0)
19365 {
19366 best_row = row;
19367 best_row_vpos = row_vpos;
19368 }
19369
19370 if (row->y + row->height >= yb)
19371 break;
19372
19373 ++row;
19374 ++row_vpos;
19375 }
19376
19377 /* Find the right column within BEST_ROW. */
19378 lastcol = 0;
19379 current_x = best_row->x;
19380 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
19381 {
19382 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
19383 int charpos = glyph->charpos;
19384
19385 if (BUFFERP (glyph->object))
19386 {
19387 if (charpos == pos)
19388 {
19389 *hpos = i;
19390 *vpos = best_row_vpos;
19391 *x = current_x;
19392 *y = best_row->y;
19393 return 1;
19394 }
19395 else if (charpos > pos)
19396 break;
19397 }
19398 else if (EQ (glyph->object, stop))
19399 break;
19400
19401 if (charpos > 0)
19402 lastcol = i;
19403 current_x += glyph->pixel_width;
19404 }
19405
19406 /* If we're looking for the end of the buffer,
19407 and we didn't find it in the line we scanned,
19408 use the start of the following line. */
19409 if (maybe_next_line_p)
19410 {
19411 ++best_row;
19412 ++best_row_vpos;
19413 lastcol = 0;
19414 current_x = best_row->x;
19415 }
19416
19417 *vpos = best_row_vpos;
19418 *hpos = lastcol + 1;
19419 *x = current_x;
19420 *y = best_row->y;
19421 return 0;
19422 }
19423
19424 #endif /* not 1 */
19425
19426
19427 /* Find the position of the glyph for position POS in OBJECT in
19428 window W's current matrix, and return in *X, *Y the pixel
19429 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
19430
19431 RIGHT_P non-zero means return the position of the right edge of the
19432 glyph, RIGHT_P zero means return the left edge position.
19433
19434 If no glyph for POS exists in the matrix, return the position of
19435 the glyph with the next smaller position that is in the matrix, if
19436 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
19437 exists in the matrix, return the position of the glyph with the
19438 next larger position in OBJECT.
19439
19440 Value is non-zero if a glyph was found. */
19441
19442 static int
19443 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
19444 struct window *w;
19445 int pos;
19446 Lisp_Object object;
19447 int *hpos, *vpos, *x, *y;
19448 int right_p;
19449 {
19450 int yb = window_text_bottom_y (w);
19451 struct glyph_row *r;
19452 struct glyph *best_glyph = NULL;
19453 struct glyph_row *best_row = NULL;
19454 int best_x = 0;
19455
19456 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19457 r->enabled_p && r->y < yb;
19458 ++r)
19459 {
19460 struct glyph *g = r->glyphs[TEXT_AREA];
19461 struct glyph *e = g + r->used[TEXT_AREA];
19462 int gx;
19463
19464 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
19465 if (EQ (g->object, object))
19466 {
19467 if (g->charpos == pos)
19468 {
19469 best_glyph = g;
19470 best_x = gx;
19471 best_row = r;
19472 goto found;
19473 }
19474 else if (best_glyph == NULL
19475 || ((abs (g->charpos - pos)
19476 < abs (best_glyph->charpos - pos))
19477 && (right_p
19478 ? g->charpos < pos
19479 : g->charpos > pos)))
19480 {
19481 best_glyph = g;
19482 best_x = gx;
19483 best_row = r;
19484 }
19485 }
19486 }
19487
19488 found:
19489
19490 if (best_glyph)
19491 {
19492 *x = best_x;
19493 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
19494
19495 if (right_p)
19496 {
19497 *x += best_glyph->pixel_width;
19498 ++*hpos;
19499 }
19500
19501 *y = best_row->y;
19502 *vpos = best_row - w->current_matrix->rows;
19503 }
19504
19505 return best_glyph != NULL;
19506 }
19507
19508
19509 /* Take proper action when mouse has moved to the mode or header line
19510 or marginal area AREA of window W, x-position X and y-position Y.
19511 X is relative to the start of the text display area of W, so the
19512 width of bitmap areas and scroll bars must be subtracted to get a
19513 position relative to the start of the mode line. */
19514
19515 static void
19516 note_mode_line_or_margin_highlight (w, x, y, area)
19517 struct window *w;
19518 int x, y;
19519 enum window_part area;
19520 {
19521 struct frame *f = XFRAME (w->frame);
19522 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
19523 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
19524 int charpos;
19525 Lisp_Object string, help, map, pos;
19526
19527 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
19528 string = mode_line_string (w, x, y, area, &charpos);
19529 else
19530 string = marginal_area_string (w, x, y, area, &charpos);
19531
19532 if (STRINGP (string))
19533 {
19534 pos = make_number (charpos);
19535
19536 /* If we're on a string with `help-echo' text property, arrange
19537 for the help to be displayed. This is done by setting the
19538 global variable help_echo_string to the help string. */
19539 help = Fget_text_property (pos, Qhelp_echo, string);
19540 if (!NILP (help))
19541 {
19542 help_echo_string = help;
19543 XSETWINDOW (help_echo_window, w);
19544 help_echo_object = string;
19545 help_echo_pos = charpos;
19546 }
19547
19548 /* Change the mouse pointer according to what is under X/Y. */
19549 map = Fget_text_property (pos, Qlocal_map, string);
19550 if (!KEYMAPP (map))
19551 map = Fget_text_property (pos, Qkeymap, string);
19552 if (KEYMAPP (map))
19553 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
19554 }
19555
19556 rif->define_frame_cursor (f, cursor);
19557 }
19558
19559
19560 /* EXPORT:
19561 Take proper action when the mouse has moved to position X, Y on
19562 frame F as regards highlighting characters that have mouse-face
19563 properties. Also de-highlighting chars where the mouse was before.
19564 X and Y can be negative or out of range. */
19565
19566 void
19567 note_mouse_highlight (f, x, y)
19568 struct frame *f;
19569 int x, y;
19570 {
19571 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
19572 enum window_part part;
19573 Lisp_Object window;
19574 struct window *w;
19575 Cursor cursor = No_Cursor;
19576 struct buffer *b;
19577
19578 /* When a menu is active, don't highlight because this looks odd. */
19579 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
19580 if (popup_activated ())
19581 return;
19582 #endif
19583
19584 if (NILP (Vmouse_highlight)
19585 || !f->glyphs_initialized_p)
19586 return;
19587
19588 dpyinfo->mouse_face_mouse_x = x;
19589 dpyinfo->mouse_face_mouse_y = y;
19590 dpyinfo->mouse_face_mouse_frame = f;
19591
19592 if (dpyinfo->mouse_face_defer)
19593 return;
19594
19595 if (gc_in_progress)
19596 {
19597 dpyinfo->mouse_face_deferred_gc = 1;
19598 return;
19599 }
19600
19601 /* Which window is that in? */
19602 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
19603
19604 /* If we were displaying active text in another window, clear that. */
19605 if (! EQ (window, dpyinfo->mouse_face_window))
19606 clear_mouse_face (dpyinfo);
19607
19608 /* Not on a window -> return. */
19609 if (!WINDOWP (window))
19610 return;
19611
19612 /* Reset help_echo_string. It will get recomputed below. */
19613 /* ++KFS: X version didn't do this, but it looks harmless. */
19614 help_echo_string = Qnil;
19615
19616 /* Convert to window-relative pixel coordinates. */
19617 w = XWINDOW (window);
19618 frame_to_window_pixel_xy (w, &x, &y);
19619
19620 /* Handle tool-bar window differently since it doesn't display a
19621 buffer. */
19622 if (EQ (window, f->tool_bar_window))
19623 {
19624 note_tool_bar_highlight (f, x, y);
19625 return;
19626 }
19627
19628 /* Mouse is on the mode, header line or margin? */
19629 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
19630 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
19631 {
19632 note_mode_line_or_margin_highlight (w, x, y, part);
19633 return;
19634 }
19635
19636 if (part == ON_VERTICAL_BORDER)
19637 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
19638 else
19639 cursor = FRAME_X_OUTPUT (f)->text_cursor;
19640
19641 /* Are we in a window whose display is up to date?
19642 And verify the buffer's text has not changed. */
19643 b = XBUFFER (w->buffer);
19644 if (part == ON_TEXT
19645 && EQ (w->window_end_valid, w->buffer)
19646 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
19647 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
19648 {
19649 int hpos, vpos, pos, i, area;
19650 struct glyph *glyph;
19651 Lisp_Object object;
19652 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
19653 Lisp_Object *overlay_vec = NULL;
19654 int len, noverlays;
19655 struct buffer *obuf;
19656 int obegv, ozv, same_region;
19657
19658 /* Find the glyph under X/Y. */
19659 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area, 0);
19660
19661 /* Clear mouse face if X/Y not over text. */
19662 if (glyph == NULL
19663 || area != TEXT_AREA
19664 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
19665 {
19666 #if defined (HAVE_NTGUI)
19667 /* ++KFS: Why is this necessary on W32 ? */
19668 clear_mouse_face (dpyinfo);
19669 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
19670 #else
19671 if (clear_mouse_face (dpyinfo))
19672 cursor = No_Cursor;
19673 #endif
19674 goto set_cursor;
19675 }
19676
19677 pos = glyph->charpos;
19678 object = glyph->object;
19679 if (!STRINGP (object) && !BUFFERP (object))
19680 goto set_cursor;
19681
19682 /* If we get an out-of-range value, return now; avoid an error. */
19683 if (BUFFERP (object) && pos > BUF_Z (b))
19684 goto set_cursor;
19685
19686 /* Make the window's buffer temporarily current for
19687 overlays_at and compute_char_face. */
19688 obuf = current_buffer;
19689 current_buffer = b;
19690 obegv = BEGV;
19691 ozv = ZV;
19692 BEGV = BEG;
19693 ZV = Z;
19694
19695 /* Is this char mouse-active or does it have help-echo? */
19696 position = make_number (pos);
19697
19698 if (BUFFERP (object))
19699 {
19700 /* Put all the overlays we want in a vector in overlay_vec.
19701 Store the length in len. If there are more than 10, make
19702 enough space for all, and try again. */
19703 len = 10;
19704 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
19705 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
19706 if (noverlays > len)
19707 {
19708 len = noverlays;
19709 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
19710 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
19711 }
19712
19713 /* Sort overlays into increasing priority order. */
19714 noverlays = sort_overlays (overlay_vec, noverlays, w);
19715 }
19716 else
19717 noverlays = 0;
19718
19719 same_region = (EQ (window, dpyinfo->mouse_face_window)
19720 && vpos >= dpyinfo->mouse_face_beg_row
19721 && vpos <= dpyinfo->mouse_face_end_row
19722 && (vpos > dpyinfo->mouse_face_beg_row
19723 || hpos >= dpyinfo->mouse_face_beg_col)
19724 && (vpos < dpyinfo->mouse_face_end_row
19725 || hpos < dpyinfo->mouse_face_end_col
19726 || dpyinfo->mouse_face_past_end));
19727
19728 if (same_region)
19729 cursor = No_Cursor;
19730
19731 /* Check mouse-face highlighting. */
19732 if (! same_region
19733 /* If there exists an overlay with mouse-face overlapping
19734 the one we are currently highlighting, we have to
19735 check if we enter the overlapping overlay, and then
19736 highlight only that. */
19737 || (OVERLAYP (dpyinfo->mouse_face_overlay)
19738 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
19739 {
19740 /* Find the highest priority overlay that has a mouse-face
19741 property. */
19742 overlay = Qnil;
19743 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
19744 {
19745 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
19746 if (!NILP (mouse_face))
19747 overlay = overlay_vec[i];
19748 }
19749
19750 /* If we're actually highlighting the same overlay as
19751 before, there's no need to do that again. */
19752 if (!NILP (overlay)
19753 && EQ (overlay, dpyinfo->mouse_face_overlay))
19754 goto check_help_echo;
19755
19756 dpyinfo->mouse_face_overlay = overlay;
19757
19758 /* Clear the display of the old active region, if any. */
19759 if (clear_mouse_face (dpyinfo))
19760 cursor = No_Cursor;
19761
19762 /* If no overlay applies, get a text property. */
19763 if (NILP (overlay))
19764 mouse_face = Fget_text_property (position, Qmouse_face, object);
19765
19766 /* Handle the overlay case. */
19767 if (!NILP (overlay))
19768 {
19769 /* Find the range of text around this char that
19770 should be active. */
19771 Lisp_Object before, after;
19772 int ignore;
19773
19774 before = Foverlay_start (overlay);
19775 after = Foverlay_end (overlay);
19776 /* Record this as the current active region. */
19777 fast_find_position (w, XFASTINT (before),
19778 &dpyinfo->mouse_face_beg_col,
19779 &dpyinfo->mouse_face_beg_row,
19780 &dpyinfo->mouse_face_beg_x,
19781 &dpyinfo->mouse_face_beg_y, Qnil);
19782
19783 dpyinfo->mouse_face_past_end
19784 = !fast_find_position (w, XFASTINT (after),
19785 &dpyinfo->mouse_face_end_col,
19786 &dpyinfo->mouse_face_end_row,
19787 &dpyinfo->mouse_face_end_x,
19788 &dpyinfo->mouse_face_end_y, Qnil);
19789 dpyinfo->mouse_face_window = window;
19790
19791 dpyinfo->mouse_face_face_id
19792 = face_at_buffer_position (w, pos, 0, 0,
19793 &ignore, pos + 1,
19794 !dpyinfo->mouse_face_hidden);
19795
19796 /* Display it as active. */
19797 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
19798 cursor = No_Cursor;
19799 }
19800 /* Handle the text property case. */
19801 else if (!NILP (mouse_face) && BUFFERP (object))
19802 {
19803 /* Find the range of text around this char that
19804 should be active. */
19805 Lisp_Object before, after, beginning, end;
19806 int ignore;
19807
19808 beginning = Fmarker_position (w->start);
19809 end = make_number (BUF_Z (XBUFFER (object))
19810 - XFASTINT (w->window_end_pos));
19811 before
19812 = Fprevious_single_property_change (make_number (pos + 1),
19813 Qmouse_face,
19814 object, beginning);
19815 after
19816 = Fnext_single_property_change (position, Qmouse_face,
19817 object, end);
19818
19819 /* Record this as the current active region. */
19820 fast_find_position (w, XFASTINT (before),
19821 &dpyinfo->mouse_face_beg_col,
19822 &dpyinfo->mouse_face_beg_row,
19823 &dpyinfo->mouse_face_beg_x,
19824 &dpyinfo->mouse_face_beg_y, Qnil);
19825 dpyinfo->mouse_face_past_end
19826 = !fast_find_position (w, XFASTINT (after),
19827 &dpyinfo->mouse_face_end_col,
19828 &dpyinfo->mouse_face_end_row,
19829 &dpyinfo->mouse_face_end_x,
19830 &dpyinfo->mouse_face_end_y, Qnil);
19831 dpyinfo->mouse_face_window = window;
19832
19833 if (BUFFERP (object))
19834 dpyinfo->mouse_face_face_id
19835 = face_at_buffer_position (w, pos, 0, 0,
19836 &ignore, pos + 1,
19837 !dpyinfo->mouse_face_hidden);
19838
19839 /* Display it as active. */
19840 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
19841 cursor = No_Cursor;
19842 }
19843 else if (!NILP (mouse_face) && STRINGP (object))
19844 {
19845 Lisp_Object b, e;
19846 int ignore;
19847
19848 b = Fprevious_single_property_change (make_number (pos + 1),
19849 Qmouse_face,
19850 object, Qnil);
19851 e = Fnext_single_property_change (position, Qmouse_face,
19852 object, Qnil);
19853 if (NILP (b))
19854 b = make_number (0);
19855 if (NILP (e))
19856 e = make_number (SCHARS (object) - 1);
19857 fast_find_string_pos (w, XINT (b), object,
19858 &dpyinfo->mouse_face_beg_col,
19859 &dpyinfo->mouse_face_beg_row,
19860 &dpyinfo->mouse_face_beg_x,
19861 &dpyinfo->mouse_face_beg_y, 0);
19862 fast_find_string_pos (w, XINT (e), object,
19863 &dpyinfo->mouse_face_end_col,
19864 &dpyinfo->mouse_face_end_row,
19865 &dpyinfo->mouse_face_end_x,
19866 &dpyinfo->mouse_face_end_y, 1);
19867 dpyinfo->mouse_face_past_end = 0;
19868 dpyinfo->mouse_face_window = window;
19869 dpyinfo->mouse_face_face_id
19870 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
19871 glyph->face_id, 1);
19872 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
19873 cursor = No_Cursor;
19874 }
19875 else if (STRINGP (object) && NILP (mouse_face))
19876 {
19877 /* A string which doesn't have mouse-face, but
19878 the text ``under'' it might have. */
19879 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
19880 int start = MATRIX_ROW_START_CHARPOS (r);
19881
19882 pos = string_buffer_position (w, object, start);
19883 if (pos > 0)
19884 mouse_face = get_char_property_and_overlay (make_number (pos),
19885 Qmouse_face,
19886 w->buffer,
19887 &overlay);
19888 if (!NILP (mouse_face) && !NILP (overlay))
19889 {
19890 Lisp_Object before = Foverlay_start (overlay);
19891 Lisp_Object after = Foverlay_end (overlay);
19892 int ignore;
19893
19894 /* Note that we might not be able to find position
19895 BEFORE in the glyph matrix if the overlay is
19896 entirely covered by a `display' property. In
19897 this case, we overshoot. So let's stop in
19898 the glyph matrix before glyphs for OBJECT. */
19899 fast_find_position (w, XFASTINT (before),
19900 &dpyinfo->mouse_face_beg_col,
19901 &dpyinfo->mouse_face_beg_row,
19902 &dpyinfo->mouse_face_beg_x,
19903 &dpyinfo->mouse_face_beg_y,
19904 object);
19905
19906 dpyinfo->mouse_face_past_end
19907 = !fast_find_position (w, XFASTINT (after),
19908 &dpyinfo->mouse_face_end_col,
19909 &dpyinfo->mouse_face_end_row,
19910 &dpyinfo->mouse_face_end_x,
19911 &dpyinfo->mouse_face_end_y,
19912 Qnil);
19913 dpyinfo->mouse_face_window = window;
19914 dpyinfo->mouse_face_face_id
19915 = face_at_buffer_position (w, pos, 0, 0,
19916 &ignore, pos + 1,
19917 !dpyinfo->mouse_face_hidden);
19918
19919 /* Display it as active. */
19920 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
19921 cursor = No_Cursor;
19922 }
19923 }
19924 }
19925
19926 check_help_echo:
19927
19928 /* Look for a `help-echo' property. */
19929 {
19930 Lisp_Object help, overlay;
19931
19932 /* Check overlays first. */
19933 help = overlay = Qnil;
19934 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
19935 {
19936 overlay = overlay_vec[i];
19937 help = Foverlay_get (overlay, Qhelp_echo);
19938 }
19939
19940 if (!NILP (help))
19941 {
19942 help_echo_string = help;
19943 help_echo_window = window;
19944 help_echo_object = overlay;
19945 help_echo_pos = pos;
19946 }
19947 else
19948 {
19949 Lisp_Object object = glyph->object;
19950 int charpos = glyph->charpos;
19951
19952 /* Try text properties. */
19953 if (STRINGP (object)
19954 && charpos >= 0
19955 && charpos < SCHARS (object))
19956 {
19957 help = Fget_text_property (make_number (charpos),
19958 Qhelp_echo, object);
19959 if (NILP (help))
19960 {
19961 /* If the string itself doesn't specify a help-echo,
19962 see if the buffer text ``under'' it does. */
19963 struct glyph_row *r
19964 = MATRIX_ROW (w->current_matrix, vpos);
19965 int start = MATRIX_ROW_START_CHARPOS (r);
19966 int pos = string_buffer_position (w, object, start);
19967 if (pos > 0)
19968 {
19969 help = Fget_char_property (make_number (pos),
19970 Qhelp_echo, w->buffer);
19971 if (!NILP (help))
19972 {
19973 charpos = pos;
19974 object = w->buffer;
19975 }
19976 }
19977 }
19978 }
19979 else if (BUFFERP (object)
19980 && charpos >= BEGV
19981 && charpos < ZV)
19982 help = Fget_text_property (make_number (charpos), Qhelp_echo,
19983 object);
19984
19985 if (!NILP (help))
19986 {
19987 help_echo_string = help;
19988 help_echo_window = window;
19989 help_echo_object = object;
19990 help_echo_pos = charpos;
19991 }
19992 }
19993 }
19994
19995 BEGV = obegv;
19996 ZV = ozv;
19997 current_buffer = obuf;
19998 }
19999
20000 set_cursor:
20001
20002 #ifndef HAVE_CARBON
20003 if (cursor != No_Cursor)
20004 #else
20005 if (bcmp (&cursor, &No_Cursor, sizeof (Cursor)))
20006 #endif
20007 rif->define_frame_cursor (f, cursor);
20008 }
20009
20010
20011 /* EXPORT for RIF:
20012 Clear any mouse-face on window W. This function is part of the
20013 redisplay interface, and is called from try_window_id and similar
20014 functions to ensure the mouse-highlight is off. */
20015
20016 void
20017 x_clear_window_mouse_face (w)
20018 struct window *w;
20019 {
20020 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20021 Lisp_Object window;
20022
20023 BLOCK_INPUT;
20024 XSETWINDOW (window, w);
20025 if (EQ (window, dpyinfo->mouse_face_window))
20026 clear_mouse_face (dpyinfo);
20027 UNBLOCK_INPUT;
20028 }
20029
20030
20031 /* EXPORT:
20032 Just discard the mouse face information for frame F, if any.
20033 This is used when the size of F is changed. */
20034
20035 void
20036 cancel_mouse_face (f)
20037 struct frame *f;
20038 {
20039 Lisp_Object window;
20040 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20041
20042 window = dpyinfo->mouse_face_window;
20043 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
20044 {
20045 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20046 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20047 dpyinfo->mouse_face_window = Qnil;
20048 }
20049 }
20050
20051
20052 #endif /* HAVE_WINDOW_SYSTEM */
20053
20054 \f
20055 /***********************************************************************
20056 Exposure Events
20057 ***********************************************************************/
20058
20059 #ifdef HAVE_WINDOW_SYSTEM
20060
20061 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
20062 which intersects rectangle R. R is in window-relative coordinates. */
20063
20064 static void
20065 expose_area (w, row, r, area)
20066 struct window *w;
20067 struct glyph_row *row;
20068 XRectangle *r;
20069 enum glyph_row_area area;
20070 {
20071 struct glyph *first = row->glyphs[area];
20072 struct glyph *end = row->glyphs[area] + row->used[area];
20073 struct glyph *last;
20074 int first_x, start_x, x;
20075
20076 if (area == TEXT_AREA && row->fill_line_p)
20077 /* If row extends face to end of line write the whole line. */
20078 draw_glyphs (w, 0, row, area,
20079 0, row->used[area],
20080 DRAW_NORMAL_TEXT, 0);
20081 else
20082 {
20083 /* Set START_X to the window-relative start position for drawing glyphs of
20084 AREA. The first glyph of the text area can be partially visible.
20085 The first glyphs of other areas cannot. */
20086 start_x = window_box_left_offset (w, area);
20087 if (area == TEXT_AREA)
20088 start_x += row->x;
20089 x = start_x;
20090
20091 /* Find the first glyph that must be redrawn. */
20092 while (first < end
20093 && x + first->pixel_width < r->x)
20094 {
20095 x += first->pixel_width;
20096 ++first;
20097 }
20098
20099 /* Find the last one. */
20100 last = first;
20101 first_x = x;
20102 while (last < end
20103 && x < r->x + r->width)
20104 {
20105 x += last->pixel_width;
20106 ++last;
20107 }
20108
20109 /* Repaint. */
20110 if (last > first)
20111 draw_glyphs (w, first_x - start_x, row, area,
20112 first - row->glyphs[area], last - row->glyphs[area],
20113 DRAW_NORMAL_TEXT, 0);
20114 }
20115 }
20116
20117
20118 /* Redraw the parts of the glyph row ROW on window W intersecting
20119 rectangle R. R is in window-relative coordinates. Value is
20120 non-zero if mouse-face was overwritten. */
20121
20122 static int
20123 expose_line (w, row, r)
20124 struct window *w;
20125 struct glyph_row *row;
20126 XRectangle *r;
20127 {
20128 xassert (row->enabled_p);
20129
20130 if (row->mode_line_p || w->pseudo_window_p)
20131 draw_glyphs (w, 0, row, TEXT_AREA,
20132 0, row->used[TEXT_AREA],
20133 DRAW_NORMAL_TEXT, 0);
20134 else
20135 {
20136 if (row->used[LEFT_MARGIN_AREA])
20137 expose_area (w, row, r, LEFT_MARGIN_AREA);
20138 if (row->used[TEXT_AREA])
20139 expose_area (w, row, r, TEXT_AREA);
20140 if (row->used[RIGHT_MARGIN_AREA])
20141 expose_area (w, row, r, RIGHT_MARGIN_AREA);
20142 draw_row_fringe_bitmaps (w, row);
20143 }
20144
20145 return row->mouse_face_p;
20146 }
20147
20148
20149 /* Redraw those parts of glyphs rows during expose event handling that
20150 overlap other rows. Redrawing of an exposed line writes over parts
20151 of lines overlapping that exposed line; this function fixes that.
20152
20153 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
20154 row in W's current matrix that is exposed and overlaps other rows.
20155 LAST_OVERLAPPING_ROW is the last such row. */
20156
20157 static void
20158 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
20159 struct window *w;
20160 struct glyph_row *first_overlapping_row;
20161 struct glyph_row *last_overlapping_row;
20162 {
20163 struct glyph_row *row;
20164
20165 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
20166 if (row->overlapping_p)
20167 {
20168 xassert (row->enabled_p && !row->mode_line_p);
20169
20170 if (row->used[LEFT_MARGIN_AREA])
20171 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
20172
20173 if (row->used[TEXT_AREA])
20174 x_fix_overlapping_area (w, row, TEXT_AREA);
20175
20176 if (row->used[RIGHT_MARGIN_AREA])
20177 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
20178 }
20179 }
20180
20181
20182 /* Return non-zero if W's cursor intersects rectangle R. */
20183
20184 static int
20185 phys_cursor_in_rect_p (w, r)
20186 struct window *w;
20187 XRectangle *r;
20188 {
20189 XRectangle cr, result;
20190 struct glyph *cursor_glyph;
20191
20192 cursor_glyph = get_phys_cursor_glyph (w);
20193 if (cursor_glyph)
20194 {
20195 /* r is relative to W's box, but w->phys_cursor.x is relative
20196 to left edge of W's TEXT area. Adjust it. */
20197 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
20198 cr.y = w->phys_cursor.y;
20199 cr.width = cursor_glyph->pixel_width;
20200 cr.height = w->phys_cursor_height;
20201 /* ++KFS: W32 version used W32-specific IntersectRect here, but
20202 I assume the effect is the same -- and this is portable. */
20203 return x_intersect_rectangles (&cr, r, &result);
20204 }
20205 else
20206 return 0;
20207 }
20208
20209
20210 /* EXPORT:
20211 Draw a vertical window border to the right of window W if W doesn't
20212 have vertical scroll bars. */
20213
20214 void
20215 x_draw_vertical_border (w)
20216 struct window *w;
20217 {
20218 /* We could do better, if we knew what type of scroll-bar the adjacent
20219 windows (on either side) have... But we don't :-(
20220 However, I think this works ok. ++KFS 2003-04-25 */
20221
20222 /* Redraw borders between horizontally adjacent windows. Don't
20223 do it for frames with vertical scroll bars because either the
20224 right scroll bar of a window, or the left scroll bar of its
20225 neighbor will suffice as a border. */
20226 if (!WINDOW_RIGHTMOST_P (w)
20227 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
20228 {
20229 int x0, x1, y0, y1;
20230
20231 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
20232 y1 -= 1;
20233
20234 rif->draw_vertical_window_border (w, x1, y0, y1);
20235 }
20236 else if (!WINDOW_LEFTMOST_P (w)
20237 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
20238 {
20239 int x0, x1, y0, y1;
20240
20241 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
20242 y1 -= 1;
20243
20244 rif->draw_vertical_window_border (w, x0, y0, y1);
20245 }
20246 }
20247
20248
20249 /* Redraw the part of window W intersection rectangle FR. Pixel
20250 coordinates in FR are frame-relative. Call this function with
20251 input blocked. Value is non-zero if the exposure overwrites
20252 mouse-face. */
20253
20254 static int
20255 expose_window (w, fr)
20256 struct window *w;
20257 XRectangle *fr;
20258 {
20259 struct frame *f = XFRAME (w->frame);
20260 XRectangle wr, r;
20261 int mouse_face_overwritten_p = 0;
20262
20263 /* If window is not yet fully initialized, do nothing. This can
20264 happen when toolkit scroll bars are used and a window is split.
20265 Reconfiguring the scroll bar will generate an expose for a newly
20266 created window. */
20267 if (w->current_matrix == NULL)
20268 return 0;
20269
20270 /* When we're currently updating the window, display and current
20271 matrix usually don't agree. Arrange for a thorough display
20272 later. */
20273 if (w == updated_window)
20274 {
20275 SET_FRAME_GARBAGED (f);
20276 return 0;
20277 }
20278
20279 /* Frame-relative pixel rectangle of W. */
20280 wr.x = WINDOW_LEFT_EDGE_X (w);
20281 wr.y = WINDOW_TOP_EDGE_Y (w);
20282 wr.width = WINDOW_TOTAL_WIDTH (w);
20283 wr.height = WINDOW_TOTAL_HEIGHT (w);
20284
20285 if (x_intersect_rectangles (fr, &wr, &r))
20286 {
20287 int yb = window_text_bottom_y (w);
20288 struct glyph_row *row;
20289 int cursor_cleared_p;
20290 struct glyph_row *first_overlapping_row, *last_overlapping_row;
20291
20292 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
20293 r.x, r.y, r.width, r.height));
20294
20295 /* Convert to window coordinates. */
20296 r.x -= WINDOW_LEFT_EDGE_X (w);
20297 r.y -= WINDOW_TOP_EDGE_Y (w);
20298
20299 /* Turn off the cursor. */
20300 if (!w->pseudo_window_p
20301 && phys_cursor_in_rect_p (w, &r))
20302 {
20303 x_clear_cursor (w);
20304 cursor_cleared_p = 1;
20305 }
20306 else
20307 cursor_cleared_p = 0;
20308
20309 /* Update lines intersecting rectangle R. */
20310 first_overlapping_row = last_overlapping_row = NULL;
20311 for (row = w->current_matrix->rows;
20312 row->enabled_p;
20313 ++row)
20314 {
20315 int y0 = row->y;
20316 int y1 = MATRIX_ROW_BOTTOM_Y (row);
20317
20318 if ((y0 >= r.y && y0 < r.y + r.height)
20319 || (y1 > r.y && y1 < r.y + r.height)
20320 || (r.y >= y0 && r.y < y1)
20321 || (r.y + r.height > y0 && r.y + r.height < y1))
20322 {
20323 if (row->overlapping_p)
20324 {
20325 if (first_overlapping_row == NULL)
20326 first_overlapping_row = row;
20327 last_overlapping_row = row;
20328 }
20329
20330 if (expose_line (w, row, &r))
20331 mouse_face_overwritten_p = 1;
20332 }
20333
20334 if (y1 >= yb)
20335 break;
20336 }
20337
20338 /* Display the mode line if there is one. */
20339 if (WINDOW_WANTS_MODELINE_P (w)
20340 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
20341 row->enabled_p)
20342 && row->y < r.y + r.height)
20343 {
20344 if (expose_line (w, row, &r))
20345 mouse_face_overwritten_p = 1;
20346 }
20347
20348 if (!w->pseudo_window_p)
20349 {
20350 /* Fix the display of overlapping rows. */
20351 if (first_overlapping_row)
20352 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
20353
20354 /* Draw border between windows. */
20355 x_draw_vertical_border (w);
20356
20357 /* Turn the cursor on again. */
20358 if (cursor_cleared_p)
20359 update_window_cursor (w, 1);
20360 }
20361 }
20362
20363 #ifdef HAVE_CARBON
20364 /* Display scroll bar for this window. */
20365 if (!NILP (w->vertical_scroll_bar))
20366 {
20367 /* ++KFS:
20368 If this doesn't work here (maybe some header files are missing),
20369 make a function in macterm.c and call it to do the job! */
20370 ControlHandle ch
20371 = SCROLL_BAR_CONTROL_HANDLE (XSCROLL_BAR (w->vertical_scroll_bar));
20372
20373 Draw1Control (ch);
20374 }
20375 #endif
20376
20377 return mouse_face_overwritten_p;
20378 }
20379
20380
20381
20382 /* Redraw (parts) of all windows in the window tree rooted at W that
20383 intersect R. R contains frame pixel coordinates. Value is
20384 non-zero if the exposure overwrites mouse-face. */
20385
20386 static int
20387 expose_window_tree (w, r)
20388 struct window *w;
20389 XRectangle *r;
20390 {
20391 struct frame *f = XFRAME (w->frame);
20392 int mouse_face_overwritten_p = 0;
20393
20394 while (w && !FRAME_GARBAGED_P (f))
20395 {
20396 if (!NILP (w->hchild))
20397 mouse_face_overwritten_p
20398 |= expose_window_tree (XWINDOW (w->hchild), r);
20399 else if (!NILP (w->vchild))
20400 mouse_face_overwritten_p
20401 |= expose_window_tree (XWINDOW (w->vchild), r);
20402 else
20403 mouse_face_overwritten_p |= expose_window (w, r);
20404
20405 w = NILP (w->next) ? NULL : XWINDOW (w->next);
20406 }
20407
20408 return mouse_face_overwritten_p;
20409 }
20410
20411
20412 /* EXPORT:
20413 Redisplay an exposed area of frame F. X and Y are the upper-left
20414 corner of the exposed rectangle. W and H are width and height of
20415 the exposed area. All are pixel values. W or H zero means redraw
20416 the entire frame. */
20417
20418 void
20419 expose_frame (f, x, y, w, h)
20420 struct frame *f;
20421 int x, y, w, h;
20422 {
20423 XRectangle r;
20424 int mouse_face_overwritten_p = 0;
20425
20426 TRACE ((stderr, "expose_frame "));
20427
20428 /* No need to redraw if frame will be redrawn soon. */
20429 if (FRAME_GARBAGED_P (f))
20430 {
20431 TRACE ((stderr, " garbaged\n"));
20432 return;
20433 }
20434
20435 #ifdef HAVE_CARBON
20436 /* MAC_TODO: this is a kludge, but if scroll bars are not activated
20437 or deactivated here, for unknown reasons, activated scroll bars
20438 are shown in deactivated frames in some instances. */
20439 if (f == FRAME_MAC_DISPLAY_INFO (f)->x_focus_frame)
20440 activate_scroll_bars (f);
20441 else
20442 deactivate_scroll_bars (f);
20443 #endif
20444
20445 /* If basic faces haven't been realized yet, there is no point in
20446 trying to redraw anything. This can happen when we get an expose
20447 event while Emacs is starting, e.g. by moving another window. */
20448 if (FRAME_FACE_CACHE (f) == NULL
20449 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
20450 {
20451 TRACE ((stderr, " no faces\n"));
20452 return;
20453 }
20454
20455 if (w == 0 || h == 0)
20456 {
20457 r.x = r.y = 0;
20458 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
20459 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
20460 }
20461 else
20462 {
20463 r.x = x;
20464 r.y = y;
20465 r.width = w;
20466 r.height = h;
20467 }
20468
20469 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
20470 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
20471
20472 if (WINDOWP (f->tool_bar_window))
20473 mouse_face_overwritten_p
20474 |= expose_window (XWINDOW (f->tool_bar_window), &r);
20475
20476 #ifdef HAVE_X_WINDOWS
20477 #ifndef MSDOS
20478 #ifndef USE_X_TOOLKIT
20479 if (WINDOWP (f->menu_bar_window))
20480 mouse_face_overwritten_p
20481 |= expose_window (XWINDOW (f->menu_bar_window), &r);
20482 #endif /* not USE_X_TOOLKIT */
20483 #endif
20484 #endif
20485
20486 /* Some window managers support a focus-follows-mouse style with
20487 delayed raising of frames. Imagine a partially obscured frame,
20488 and moving the mouse into partially obscured mouse-face on that
20489 frame. The visible part of the mouse-face will be highlighted,
20490 then the WM raises the obscured frame. With at least one WM, KDE
20491 2.1, Emacs is not getting any event for the raising of the frame
20492 (even tried with SubstructureRedirectMask), only Expose events.
20493 These expose events will draw text normally, i.e. not
20494 highlighted. Which means we must redo the highlight here.
20495 Subsume it under ``we love X''. --gerd 2001-08-15 */
20496 /* Included in Windows version because Windows most likely does not
20497 do the right thing if any third party tool offers
20498 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
20499 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
20500 {
20501 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20502 if (f == dpyinfo->mouse_face_mouse_frame)
20503 {
20504 int x = dpyinfo->mouse_face_mouse_x;
20505 int y = dpyinfo->mouse_face_mouse_y;
20506 clear_mouse_face (dpyinfo);
20507 note_mouse_highlight (f, x, y);
20508 }
20509 }
20510 }
20511
20512
20513 /* EXPORT:
20514 Determine the intersection of two rectangles R1 and R2. Return
20515 the intersection in *RESULT. Value is non-zero if RESULT is not
20516 empty. */
20517
20518 int
20519 x_intersect_rectangles (r1, r2, result)
20520 XRectangle *r1, *r2, *result;
20521 {
20522 XRectangle *left, *right;
20523 XRectangle *upper, *lower;
20524 int intersection_p = 0;
20525
20526 /* Rearrange so that R1 is the left-most rectangle. */
20527 if (r1->x < r2->x)
20528 left = r1, right = r2;
20529 else
20530 left = r2, right = r1;
20531
20532 /* X0 of the intersection is right.x0, if this is inside R1,
20533 otherwise there is no intersection. */
20534 if (right->x <= left->x + left->width)
20535 {
20536 result->x = right->x;
20537
20538 /* The right end of the intersection is the minimum of the
20539 the right ends of left and right. */
20540 result->width = (min (left->x + left->width, right->x + right->width)
20541 - result->x);
20542
20543 /* Same game for Y. */
20544 if (r1->y < r2->y)
20545 upper = r1, lower = r2;
20546 else
20547 upper = r2, lower = r1;
20548
20549 /* The upper end of the intersection is lower.y0, if this is inside
20550 of upper. Otherwise, there is no intersection. */
20551 if (lower->y <= upper->y + upper->height)
20552 {
20553 result->y = lower->y;
20554
20555 /* The lower end of the intersection is the minimum of the lower
20556 ends of upper and lower. */
20557 result->height = (min (lower->y + lower->height,
20558 upper->y + upper->height)
20559 - result->y);
20560 intersection_p = 1;
20561 }
20562 }
20563
20564 return intersection_p;
20565 }
20566
20567 #endif /* HAVE_WINDOW_SYSTEM */
20568
20569 \f
20570 /***********************************************************************
20571 Initialization
20572 ***********************************************************************/
20573
20574 void
20575 syms_of_xdisp ()
20576 {
20577 Vwith_echo_area_save_vector = Qnil;
20578 staticpro (&Vwith_echo_area_save_vector);
20579
20580 Vmessage_stack = Qnil;
20581 staticpro (&Vmessage_stack);
20582
20583 Qinhibit_redisplay = intern ("inhibit-redisplay");
20584 staticpro (&Qinhibit_redisplay);
20585
20586 message_dolog_marker1 = Fmake_marker ();
20587 staticpro (&message_dolog_marker1);
20588 message_dolog_marker2 = Fmake_marker ();
20589 staticpro (&message_dolog_marker2);
20590 message_dolog_marker3 = Fmake_marker ();
20591 staticpro (&message_dolog_marker3);
20592
20593 #if GLYPH_DEBUG
20594 defsubr (&Sdump_frame_glyph_matrix);
20595 defsubr (&Sdump_glyph_matrix);
20596 defsubr (&Sdump_glyph_row);
20597 defsubr (&Sdump_tool_bar_row);
20598 defsubr (&Strace_redisplay);
20599 defsubr (&Strace_to_stderr);
20600 #endif
20601 #ifdef HAVE_WINDOW_SYSTEM
20602 defsubr (&Stool_bar_lines_needed);
20603 #endif
20604 defsubr (&Sformat_mode_line);
20605
20606 staticpro (&Qmenu_bar_update_hook);
20607 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
20608
20609 staticpro (&Qoverriding_terminal_local_map);
20610 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
20611
20612 staticpro (&Qoverriding_local_map);
20613 Qoverriding_local_map = intern ("overriding-local-map");
20614
20615 staticpro (&Qwindow_scroll_functions);
20616 Qwindow_scroll_functions = intern ("window-scroll-functions");
20617
20618 staticpro (&Qredisplay_end_trigger_functions);
20619 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
20620
20621 staticpro (&Qinhibit_point_motion_hooks);
20622 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
20623
20624 QCdata = intern (":data");
20625 staticpro (&QCdata);
20626 Qdisplay = intern ("display");
20627 staticpro (&Qdisplay);
20628 Qspace_width = intern ("space-width");
20629 staticpro (&Qspace_width);
20630 Qraise = intern ("raise");
20631 staticpro (&Qraise);
20632 Qspace = intern ("space");
20633 staticpro (&Qspace);
20634 Qmargin = intern ("margin");
20635 staticpro (&Qmargin);
20636 Qleft_margin = intern ("left-margin");
20637 staticpro (&Qleft_margin);
20638 Qright_margin = intern ("right-margin");
20639 staticpro (&Qright_margin);
20640 Qalign_to = intern ("align-to");
20641 staticpro (&Qalign_to);
20642 QCalign_to = intern (":align-to");
20643 staticpro (&QCalign_to);
20644 Qrelative_width = intern ("relative-width");
20645 staticpro (&Qrelative_width);
20646 QCrelative_width = intern (":relative-width");
20647 staticpro (&QCrelative_width);
20648 QCrelative_height = intern (":relative-height");
20649 staticpro (&QCrelative_height);
20650 QCeval = intern (":eval");
20651 staticpro (&QCeval);
20652 QCpropertize = intern (":propertize");
20653 staticpro (&QCpropertize);
20654 QCfile = intern (":file");
20655 staticpro (&QCfile);
20656 Qfontified = intern ("fontified");
20657 staticpro (&Qfontified);
20658 Qfontification_functions = intern ("fontification-functions");
20659 staticpro (&Qfontification_functions);
20660 Qtrailing_whitespace = intern ("trailing-whitespace");
20661 staticpro (&Qtrailing_whitespace);
20662 Qimage = intern ("image");
20663 staticpro (&Qimage);
20664 Qmessage_truncate_lines = intern ("message-truncate-lines");
20665 staticpro (&Qmessage_truncate_lines);
20666 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
20667 staticpro (&Qcursor_in_non_selected_windows);
20668 Qgrow_only = intern ("grow-only");
20669 staticpro (&Qgrow_only);
20670 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
20671 staticpro (&Qinhibit_menubar_update);
20672 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
20673 staticpro (&Qinhibit_eval_during_redisplay);
20674 Qposition = intern ("position");
20675 staticpro (&Qposition);
20676 Qbuffer_position = intern ("buffer-position");
20677 staticpro (&Qbuffer_position);
20678 Qobject = intern ("object");
20679 staticpro (&Qobject);
20680 Qbar = intern ("bar");
20681 staticpro (&Qbar);
20682 Qhbar = intern ("hbar");
20683 staticpro (&Qhbar);
20684 Qbox = intern ("box");
20685 staticpro (&Qbox);
20686 Qhollow = intern ("hollow");
20687 staticpro (&Qhollow);
20688 Qrisky_local_variable = intern ("risky-local-variable");
20689 staticpro (&Qrisky_local_variable);
20690 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
20691 staticpro (&Qinhibit_free_realized_faces);
20692
20693 list_of_error = Fcons (intern ("error"), Qnil);
20694 staticpro (&list_of_error);
20695
20696 last_arrow_position = Qnil;
20697 last_arrow_string = Qnil;
20698 staticpro (&last_arrow_position);
20699 staticpro (&last_arrow_string);
20700
20701 echo_buffer[0] = echo_buffer[1] = Qnil;
20702 staticpro (&echo_buffer[0]);
20703 staticpro (&echo_buffer[1]);
20704
20705 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
20706 staticpro (&echo_area_buffer[0]);
20707 staticpro (&echo_area_buffer[1]);
20708
20709 Vmessages_buffer_name = build_string ("*Messages*");
20710 staticpro (&Vmessages_buffer_name);
20711
20712 mode_line_proptrans_alist = Qnil;
20713 staticpro (&mode_line_proptrans_alist);
20714
20715 mode_line_string_list = Qnil;
20716 staticpro (&mode_line_string_list);
20717
20718 help_echo_string = Qnil;
20719 staticpro (&help_echo_string);
20720 help_echo_object = Qnil;
20721 staticpro (&help_echo_object);
20722 help_echo_window = Qnil;
20723 staticpro (&help_echo_window);
20724 previous_help_echo_string = Qnil;
20725 staticpro (&previous_help_echo_string);
20726 help_echo_pos = -1;
20727
20728 #ifdef HAVE_WINDOW_SYSTEM
20729 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
20730 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
20731 For example, if a block cursor is over a tab, it will be drawn as
20732 wide as that tab on the display. */);
20733 x_stretch_cursor_p = 0;
20734 #endif
20735
20736 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
20737 doc: /* Non-nil means highlight trailing whitespace.
20738 The face used for trailing whitespace is `trailing-whitespace'. */);
20739 Vshow_trailing_whitespace = Qnil;
20740
20741 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
20742 doc: /* Non-nil means don't actually do any redisplay.
20743 This is used for internal purposes. */);
20744 Vinhibit_redisplay = Qnil;
20745
20746 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
20747 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
20748 Vglobal_mode_string = Qnil;
20749
20750 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
20751 doc: /* Marker for where to display an arrow on top of the buffer text.
20752 This must be the beginning of a line in order to work.
20753 See also `overlay-arrow-string'. */);
20754 Voverlay_arrow_position = Qnil;
20755
20756 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
20757 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
20758 Voverlay_arrow_string = Qnil;
20759
20760 DEFVAR_INT ("scroll-step", &scroll_step,
20761 doc: /* *The number of lines to try scrolling a window by when point moves out.
20762 If that fails to bring point back on frame, point is centered instead.
20763 If this is zero, point is always centered after it moves off frame.
20764 If you want scrolling to always be a line at a time, you should set
20765 `scroll-conservatively' to a large value rather than set this to 1. */);
20766
20767 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
20768 doc: /* *Scroll up to this many lines, to bring point back on screen.
20769 A value of zero means to scroll the text to center point vertically
20770 in the window. */);
20771 scroll_conservatively = 0;
20772
20773 DEFVAR_INT ("scroll-margin", &scroll_margin,
20774 doc: /* *Number of lines of margin at the top and bottom of a window.
20775 Recenter the window whenever point gets within this many lines
20776 of the top or bottom of the window. */);
20777 scroll_margin = 0;
20778
20779 #if GLYPH_DEBUG
20780 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
20781 #endif
20782
20783 DEFVAR_BOOL ("truncate-partial-width-windows",
20784 &truncate_partial_width_windows,
20785 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
20786 truncate_partial_width_windows = 1;
20787
20788 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
20789 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
20790 Any other value means to use the appropriate face, `mode-line',
20791 `header-line', or `menu' respectively. */);
20792 mode_line_inverse_video = 1;
20793
20794 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
20795 doc: /* *Maximum buffer size for which line number should be displayed.
20796 If the buffer is bigger than this, the line number does not appear
20797 in the mode line. A value of nil means no limit. */);
20798 Vline_number_display_limit = Qnil;
20799
20800 DEFVAR_INT ("line-number-display-limit-width",
20801 &line_number_display_limit_width,
20802 doc: /* *Maximum line width (in characters) for line number display.
20803 If the average length of the lines near point is bigger than this, then the
20804 line number may be omitted from the mode line. */);
20805 line_number_display_limit_width = 200;
20806
20807 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
20808 doc: /* *Non-nil means highlight region even in nonselected windows. */);
20809 highlight_nonselected_windows = 0;
20810
20811 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
20812 doc: /* Non-nil if more than one frame is visible on this display.
20813 Minibuffer-only frames don't count, but iconified frames do.
20814 This variable is not guaranteed to be accurate except while processing
20815 `frame-title-format' and `icon-title-format'. */);
20816
20817 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
20818 doc: /* Template for displaying the title bar of visible frames.
20819 \(Assuming the window manager supports this feature.)
20820 This variable has the same structure as `mode-line-format' (which see),
20821 and is used only on frames for which no explicit name has been set
20822 \(see `modify-frame-parameters'). */);
20823 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
20824 doc: /* Template for displaying the title bar of an iconified frame.
20825 \(Assuming the window manager supports this feature.)
20826 This variable has the same structure as `mode-line-format' (which see),
20827 and is used only on frames for which no explicit name has been set
20828 \(see `modify-frame-parameters'). */);
20829 Vicon_title_format
20830 = Vframe_title_format
20831 = Fcons (intern ("multiple-frames"),
20832 Fcons (build_string ("%b"),
20833 Fcons (Fcons (empty_string,
20834 Fcons (intern ("invocation-name"),
20835 Fcons (build_string ("@"),
20836 Fcons (intern ("system-name"),
20837 Qnil)))),
20838 Qnil)));
20839
20840 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
20841 doc: /* Maximum number of lines to keep in the message log buffer.
20842 If nil, disable message logging. If t, log messages but don't truncate
20843 the buffer when it becomes large. */);
20844 Vmessage_log_max = make_number (50);
20845
20846 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
20847 doc: /* Functions called before redisplay, if window sizes have changed.
20848 The value should be a list of functions that take one argument.
20849 Just before redisplay, for each frame, if any of its windows have changed
20850 size since the last redisplay, or have been split or deleted,
20851 all the functions in the list are called, with the frame as argument. */);
20852 Vwindow_size_change_functions = Qnil;
20853
20854 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
20855 doc: /* List of Functions to call before redisplaying a window with scrolling.
20856 Each function is called with two arguments, the window
20857 and its new display-start position. Note that the value of `window-end'
20858 is not valid when these functions are called. */);
20859 Vwindow_scroll_functions = Qnil;
20860
20861 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
20862 doc: /* *Non-nil means autoselect window with mouse pointer. */);
20863 mouse_autoselect_window = 0;
20864
20865 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
20866 doc: /* *Non-nil means automatically resize tool-bars.
20867 This increases a tool-bar's height if not all tool-bar items are visible.
20868 It decreases a tool-bar's height when it would display blank lines
20869 otherwise. */);
20870 auto_resize_tool_bars_p = 1;
20871
20872 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
20873 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
20874 auto_raise_tool_bar_buttons_p = 1;
20875
20876 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
20877 doc: /* *Margin around tool-bar buttons in pixels.
20878 If an integer, use that for both horizontal and vertical margins.
20879 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
20880 HORZ specifying the horizontal margin, and VERT specifying the
20881 vertical margin. */);
20882 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
20883
20884 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
20885 doc: /* *Relief thickness of tool-bar buttons. */);
20886 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
20887
20888 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
20889 doc: /* List of functions to call to fontify regions of text.
20890 Each function is called with one argument POS. Functions must
20891 fontify a region starting at POS in the current buffer, and give
20892 fontified regions the property `fontified'. */);
20893 Vfontification_functions = Qnil;
20894 Fmake_variable_buffer_local (Qfontification_functions);
20895
20896 DEFVAR_BOOL ("unibyte-display-via-language-environment",
20897 &unibyte_display_via_language_environment,
20898 doc: /* *Non-nil means display unibyte text according to language environment.
20899 Specifically this means that unibyte non-ASCII characters
20900 are displayed by converting them to the equivalent multibyte characters
20901 according to the current language environment. As a result, they are
20902 displayed according to the current fontset. */);
20903 unibyte_display_via_language_environment = 0;
20904
20905 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
20906 doc: /* *Maximum height for resizing mini-windows.
20907 If a float, it specifies a fraction of the mini-window frame's height.
20908 If an integer, it specifies a number of lines. */);
20909 Vmax_mini_window_height = make_float (0.25);
20910
20911 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
20912 doc: /* *How to resize mini-windows.
20913 A value of nil means don't automatically resize mini-windows.
20914 A value of t means resize them to fit the text displayed in them.
20915 A value of `grow-only', the default, means let mini-windows grow
20916 only, until their display becomes empty, at which point the windows
20917 go back to their normal size. */);
20918 Vresize_mini_windows = Qgrow_only;
20919
20920 DEFVAR_LISP ("cursor-in-non-selected-windows",
20921 &Vcursor_in_non_selected_windows,
20922 doc: /* *Cursor type to display in non-selected windows.
20923 t means to use hollow box cursor. See `cursor-type' for other values. */);
20924 Vcursor_in_non_selected_windows = Qt;
20925
20926 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
20927 doc: /* Alist specifying how to blink the cursor off.
20928 Each element has the form (ON-STATE . OFF-STATE). Whenever the
20929 `cursor-type' frame-parameter or variable equals ON-STATE,
20930 comparing using `equal', Emacs uses OFF-STATE to specify
20931 how to blink it off. */);
20932 Vblink_cursor_alist = Qnil;
20933
20934 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
20935 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
20936 automatic_hscrolling_p = 1;
20937
20938 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
20939 doc: /* *How many columns away from the window edge point is allowed to get
20940 before automatic hscrolling will horizontally scroll the window. */);
20941 hscroll_margin = 5;
20942
20943 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
20944 doc: /* *How many columns to scroll the window when point gets too close to the edge.
20945 When point is less than `automatic-hscroll-margin' columns from the window
20946 edge, automatic hscrolling will scroll the window by the amount of columns
20947 determined by this variable. If its value is a positive integer, scroll that
20948 many columns. If it's a positive floating-point number, it specifies the
20949 fraction of the window's width to scroll. If it's nil or zero, point will be
20950 centered horizontally after the scroll. Any other value, including negative
20951 numbers, are treated as if the value were zero.
20952
20953 Automatic hscrolling always moves point outside the scroll margin, so if
20954 point was more than scroll step columns inside the margin, the window will
20955 scroll more than the value given by the scroll step.
20956
20957 Note that the lower bound for automatic hscrolling specified by `scroll-left'
20958 and `scroll-right' overrides this variable's effect. */);
20959 Vhscroll_step = make_number (0);
20960
20961 DEFVAR_LISP ("image-types", &Vimage_types,
20962 doc: /* List of supported image types.
20963 Each element of the list is a symbol for a supported image type. */);
20964 Vimage_types = Qnil;
20965
20966 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
20967 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
20968 Bind this around calls to `message' to let it take effect. */);
20969 message_truncate_lines = 0;
20970
20971 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
20972 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
20973 Can be used to update submenus whose contents should vary. */);
20974 Vmenu_bar_update_hook = Qnil;
20975
20976 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
20977 doc: /* Non-nil means don't update menu bars. Internal use only. */);
20978 inhibit_menubar_update = 0;
20979
20980 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
20981 doc: /* Non-nil means don't eval Lisp during redisplay. */);
20982 inhibit_eval_during_redisplay = 0;
20983
20984 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
20985 doc: /* Non-nil means don't free realized faces. Internal use only. */);
20986 inhibit_free_realized_faces = 0;
20987
20988 #if GLYPH_DEBUG
20989 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
20990 doc: /* Inhibit try_window_id display optimization. */);
20991 inhibit_try_window_id = 0;
20992
20993 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
20994 doc: /* Inhibit try_window_reusing display optimization. */);
20995 inhibit_try_window_reusing = 0;
20996
20997 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
20998 doc: /* Inhibit try_cursor_movement display optimization. */);
20999 inhibit_try_cursor_movement = 0;
21000 #endif /* GLYPH_DEBUG */
21001 }
21002
21003
21004 /* Initialize this module when Emacs starts. */
21005
21006 void
21007 init_xdisp ()
21008 {
21009 Lisp_Object root_window;
21010 struct window *mini_w;
21011
21012 current_header_line_height = current_mode_line_height = -1;
21013
21014 CHARPOS (this_line_start_pos) = 0;
21015
21016 mini_w = XWINDOW (minibuf_window);
21017 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
21018
21019 if (!noninteractive)
21020 {
21021 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
21022 int i;
21023
21024 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
21025 set_window_height (root_window,
21026 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
21027 0);
21028 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
21029 set_window_height (minibuf_window, 1, 0);
21030
21031 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
21032 mini_w->total_cols = make_number (FRAME_COLS (f));
21033
21034 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
21035 scratch_glyph_row.glyphs[TEXT_AREA + 1]
21036 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
21037
21038 /* The default ellipsis glyphs `...'. */
21039 for (i = 0; i < 3; ++i)
21040 default_invis_vector[i] = make_number ('.');
21041 }
21042
21043 {
21044 /* Allocate the buffer for frame titles.
21045 Also used for `format-mode-line'. */
21046 int size = 100;
21047 frame_title_buf = (char *) xmalloc (size);
21048 frame_title_buf_end = frame_title_buf + size;
21049 frame_title_ptr = NULL;
21050 }
21051
21052 help_echo_showing_p = 0;
21053 }
21054
21055