]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(add_system_logical_colors_to_map): New function.
[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 ov, overlay, window, str, invisible;
3990 int start, end;
3991 int size = 20;
3992 int n = 0, i, j, invis_p;
3993 struct overlay_entry *entries
3994 = (struct overlay_entry *) alloca (size * sizeof *entries);
3995
3996 if (charpos <= 0)
3997 charpos = IT_CHARPOS (*it);
3998
3999 /* Append the overlay string STRING of overlay OVERLAY to vector
4000 `entries' which has size `size' and currently contains `n'
4001 elements. AFTER_P non-zero means STRING is an after-string of
4002 OVERLAY. */
4003 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4004 do \
4005 { \
4006 Lisp_Object priority; \
4007 \
4008 if (n == size) \
4009 { \
4010 int new_size = 2 * size; \
4011 struct overlay_entry *old = entries; \
4012 entries = \
4013 (struct overlay_entry *) alloca (new_size \
4014 * sizeof *entries); \
4015 bcopy (old, entries, size * sizeof *entries); \
4016 size = new_size; \
4017 } \
4018 \
4019 entries[n].string = (STRING); \
4020 entries[n].overlay = (OVERLAY); \
4021 priority = Foverlay_get ((OVERLAY), Qpriority); \
4022 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4023 entries[n].after_string_p = (AFTER_P); \
4024 ++n; \
4025 } \
4026 while (0)
4027
4028 /* Process overlay before the overlay center. */
4029 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
4030 {
4031 overlay = XCAR (ov);
4032 xassert (OVERLAYP (overlay));
4033 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4034 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4035
4036 if (end < charpos)
4037 break;
4038
4039 /* Skip this overlay if it doesn't start or end at IT's current
4040 position. */
4041 if (end != charpos && start != charpos)
4042 continue;
4043
4044 /* Skip this overlay if it doesn't apply to IT->w. */
4045 window = Foverlay_get (overlay, Qwindow);
4046 if (WINDOWP (window) && XWINDOW (window) != it->w)
4047 continue;
4048
4049 /* If the text ``under'' the overlay is invisible, both before-
4050 and after-strings from this overlay are visible; start and
4051 end position are indistinguishable. */
4052 invisible = Foverlay_get (overlay, Qinvisible);
4053 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4054
4055 /* If overlay has a non-empty before-string, record it. */
4056 if ((start == charpos || (end == charpos && invis_p))
4057 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4058 && SCHARS (str))
4059 RECORD_OVERLAY_STRING (overlay, str, 0);
4060
4061 /* If overlay has a non-empty after-string, record it. */
4062 if ((end == charpos || (start == charpos && invis_p))
4063 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4064 && SCHARS (str))
4065 RECORD_OVERLAY_STRING (overlay, str, 1);
4066 }
4067
4068 /* Process overlays after the overlay center. */
4069 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
4070 {
4071 overlay = XCAR (ov);
4072 xassert (OVERLAYP (overlay));
4073 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4074 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4075
4076 if (start > charpos)
4077 break;
4078
4079 /* Skip this overlay if it doesn't start or end at IT's current
4080 position. */
4081 if (end != charpos && start != charpos)
4082 continue;
4083
4084 /* Skip this overlay if it doesn't apply to IT->w. */
4085 window = Foverlay_get (overlay, Qwindow);
4086 if (WINDOWP (window) && XWINDOW (window) != it->w)
4087 continue;
4088
4089 /* If the text ``under'' the overlay is invisible, it has a zero
4090 dimension, and both before- and after-strings apply. */
4091 invisible = Foverlay_get (overlay, Qinvisible);
4092 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4093
4094 /* If overlay has a non-empty before-string, record it. */
4095 if ((start == charpos || (end == charpos && invis_p))
4096 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4097 && SCHARS (str))
4098 RECORD_OVERLAY_STRING (overlay, str, 0);
4099
4100 /* If overlay has a non-empty after-string, record it. */
4101 if ((end == charpos || (start == charpos && invis_p))
4102 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4103 && SCHARS (str))
4104 RECORD_OVERLAY_STRING (overlay, str, 1);
4105 }
4106
4107 #undef RECORD_OVERLAY_STRING
4108
4109 /* Sort entries. */
4110 if (n > 1)
4111 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4112
4113 /* Record the total number of strings to process. */
4114 it->n_overlay_strings = n;
4115
4116 /* IT->current.overlay_string_index is the number of overlay strings
4117 that have already been consumed by IT. Copy some of the
4118 remaining overlay strings to IT->overlay_strings. */
4119 i = 0;
4120 j = it->current.overlay_string_index;
4121 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4122 it->overlay_strings[i++] = entries[j++].string;
4123
4124 CHECK_IT (it);
4125 }
4126
4127
4128 /* Get the first chunk of overlay strings at IT's current buffer
4129 position, or at CHARPOS if that is > 0. Value is non-zero if at
4130 least one overlay string was found. */
4131
4132 static int
4133 get_overlay_strings (it, charpos)
4134 struct it *it;
4135 int charpos;
4136 {
4137 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4138 process. This fills IT->overlay_strings with strings, and sets
4139 IT->n_overlay_strings to the total number of strings to process.
4140 IT->pos.overlay_string_index has to be set temporarily to zero
4141 because load_overlay_strings needs this; it must be set to -1
4142 when no overlay strings are found because a zero value would
4143 indicate a position in the first overlay string. */
4144 it->current.overlay_string_index = 0;
4145 load_overlay_strings (it, charpos);
4146
4147 /* If we found overlay strings, set up IT to deliver display
4148 elements from the first one. Otherwise set up IT to deliver
4149 from current_buffer. */
4150 if (it->n_overlay_strings)
4151 {
4152 /* Make sure we know settings in current_buffer, so that we can
4153 restore meaningful values when we're done with the overlay
4154 strings. */
4155 compute_stop_pos (it);
4156 xassert (it->face_id >= 0);
4157
4158 /* Save IT's settings. They are restored after all overlay
4159 strings have been processed. */
4160 xassert (it->sp == 0);
4161 push_it (it);
4162
4163 /* Set up IT to deliver display elements from the first overlay
4164 string. */
4165 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4166 it->string = it->overlay_strings[0];
4167 it->stop_charpos = 0;
4168 xassert (STRINGP (it->string));
4169 it->end_charpos = SCHARS (it->string);
4170 it->multibyte_p = STRING_MULTIBYTE (it->string);
4171 it->method = next_element_from_string;
4172 }
4173 else
4174 {
4175 it->string = Qnil;
4176 it->current.overlay_string_index = -1;
4177 it->method = next_element_from_buffer;
4178 }
4179
4180 CHECK_IT (it);
4181
4182 /* Value is non-zero if we found at least one overlay string. */
4183 return STRINGP (it->string);
4184 }
4185
4186
4187 \f
4188 /***********************************************************************
4189 Saving and restoring state
4190 ***********************************************************************/
4191
4192 /* Save current settings of IT on IT->stack. Called, for example,
4193 before setting up IT for an overlay string, to be able to restore
4194 IT's settings to what they were after the overlay string has been
4195 processed. */
4196
4197 static void
4198 push_it (it)
4199 struct it *it;
4200 {
4201 struct iterator_stack_entry *p;
4202
4203 xassert (it->sp < 2);
4204 p = it->stack + it->sp;
4205
4206 p->stop_charpos = it->stop_charpos;
4207 xassert (it->face_id >= 0);
4208 p->face_id = it->face_id;
4209 p->string = it->string;
4210 p->pos = it->current;
4211 p->end_charpos = it->end_charpos;
4212 p->string_nchars = it->string_nchars;
4213 p->area = it->area;
4214 p->multibyte_p = it->multibyte_p;
4215 p->space_width = it->space_width;
4216 p->font_height = it->font_height;
4217 p->voffset = it->voffset;
4218 p->string_from_display_prop_p = it->string_from_display_prop_p;
4219 p->display_ellipsis_p = 0;
4220 ++it->sp;
4221 }
4222
4223
4224 /* Restore IT's settings from IT->stack. Called, for example, when no
4225 more overlay strings must be processed, and we return to delivering
4226 display elements from a buffer, or when the end of a string from a
4227 `display' property is reached and we return to delivering display
4228 elements from an overlay string, or from a buffer. */
4229
4230 static void
4231 pop_it (it)
4232 struct it *it;
4233 {
4234 struct iterator_stack_entry *p;
4235
4236 xassert (it->sp > 0);
4237 --it->sp;
4238 p = it->stack + it->sp;
4239 it->stop_charpos = p->stop_charpos;
4240 it->face_id = p->face_id;
4241 it->string = p->string;
4242 it->current = p->pos;
4243 it->end_charpos = p->end_charpos;
4244 it->string_nchars = p->string_nchars;
4245 it->area = p->area;
4246 it->multibyte_p = p->multibyte_p;
4247 it->space_width = p->space_width;
4248 it->font_height = p->font_height;
4249 it->voffset = p->voffset;
4250 it->string_from_display_prop_p = p->string_from_display_prop_p;
4251 }
4252
4253
4254 \f
4255 /***********************************************************************
4256 Moving over lines
4257 ***********************************************************************/
4258
4259 /* Set IT's current position to the previous line start. */
4260
4261 static void
4262 back_to_previous_line_start (it)
4263 struct it *it;
4264 {
4265 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4266 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4267 }
4268
4269
4270 /* Move IT to the next line start.
4271
4272 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4273 we skipped over part of the text (as opposed to moving the iterator
4274 continuously over the text). Otherwise, don't change the value
4275 of *SKIPPED_P.
4276
4277 Newlines may come from buffer text, overlay strings, or strings
4278 displayed via the `display' property. That's the reason we can't
4279 simply use find_next_newline_no_quit.
4280
4281 Note that this function may not skip over invisible text that is so
4282 because of text properties and immediately follows a newline. If
4283 it would, function reseat_at_next_visible_line_start, when called
4284 from set_iterator_to_next, would effectively make invisible
4285 characters following a newline part of the wrong glyph row, which
4286 leads to wrong cursor motion. */
4287
4288 static int
4289 forward_to_next_line_start (it, skipped_p)
4290 struct it *it;
4291 int *skipped_p;
4292 {
4293 int old_selective, newline_found_p, n;
4294 const int MAX_NEWLINE_DISTANCE = 500;
4295
4296 /* If already on a newline, just consume it to avoid unintended
4297 skipping over invisible text below. */
4298 if (it->what == IT_CHARACTER
4299 && it->c == '\n'
4300 && CHARPOS (it->position) == IT_CHARPOS (*it))
4301 {
4302 set_iterator_to_next (it, 0);
4303 it->c = 0;
4304 return 1;
4305 }
4306
4307 /* Don't handle selective display in the following. It's (a)
4308 unnecessary because it's done by the caller, and (b) leads to an
4309 infinite recursion because next_element_from_ellipsis indirectly
4310 calls this function. */
4311 old_selective = it->selective;
4312 it->selective = 0;
4313
4314 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4315 from buffer text. */
4316 for (n = newline_found_p = 0;
4317 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4318 n += STRINGP (it->string) ? 0 : 1)
4319 {
4320 if (!get_next_display_element (it))
4321 return 0;
4322 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4323 set_iterator_to_next (it, 0);
4324 }
4325
4326 /* If we didn't find a newline near enough, see if we can use a
4327 short-cut. */
4328 if (!newline_found_p)
4329 {
4330 int start = IT_CHARPOS (*it);
4331 int limit = find_next_newline_no_quit (start, 1);
4332 Lisp_Object pos;
4333
4334 xassert (!STRINGP (it->string));
4335
4336 /* If there isn't any `display' property in sight, and no
4337 overlays, we can just use the position of the newline in
4338 buffer text. */
4339 if (it->stop_charpos >= limit
4340 || ((pos = Fnext_single_property_change (make_number (start),
4341 Qdisplay,
4342 Qnil, make_number (limit)),
4343 NILP (pos))
4344 && next_overlay_change (start) == ZV))
4345 {
4346 IT_CHARPOS (*it) = limit;
4347 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4348 *skipped_p = newline_found_p = 1;
4349 }
4350 else
4351 {
4352 while (get_next_display_element (it)
4353 && !newline_found_p)
4354 {
4355 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4356 set_iterator_to_next (it, 0);
4357 }
4358 }
4359 }
4360
4361 it->selective = old_selective;
4362 return newline_found_p;
4363 }
4364
4365
4366 /* Set IT's current position to the previous visible line start. Skip
4367 invisible text that is so either due to text properties or due to
4368 selective display. Caution: this does not change IT->current_x and
4369 IT->hpos. */
4370
4371 static void
4372 back_to_previous_visible_line_start (it)
4373 struct it *it;
4374 {
4375 int visible_p = 0;
4376
4377 /* Go back one newline if not on BEGV already. */
4378 if (IT_CHARPOS (*it) > BEGV)
4379 back_to_previous_line_start (it);
4380
4381 /* Move over lines that are invisible because of selective display
4382 or text properties. */
4383 while (IT_CHARPOS (*it) > BEGV
4384 && !visible_p)
4385 {
4386 visible_p = 1;
4387
4388 /* If selective > 0, then lines indented more than that values
4389 are invisible. */
4390 if (it->selective > 0
4391 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4392 (double) it->selective)) /* iftc */
4393 visible_p = 0;
4394 else
4395 {
4396 Lisp_Object prop;
4397
4398 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
4399 Qinvisible, it->window);
4400 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4401 visible_p = 0;
4402 }
4403
4404 /* Back one more newline if the current one is invisible. */
4405 if (!visible_p)
4406 back_to_previous_line_start (it);
4407 }
4408
4409 xassert (IT_CHARPOS (*it) >= BEGV);
4410 xassert (IT_CHARPOS (*it) == BEGV
4411 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4412 CHECK_IT (it);
4413 }
4414
4415
4416 /* Reseat iterator IT at the previous visible line start. Skip
4417 invisible text that is so either due to text properties or due to
4418 selective display. At the end, update IT's overlay information,
4419 face information etc. */
4420
4421 static void
4422 reseat_at_previous_visible_line_start (it)
4423 struct it *it;
4424 {
4425 back_to_previous_visible_line_start (it);
4426 reseat (it, it->current.pos, 1);
4427 CHECK_IT (it);
4428 }
4429
4430
4431 /* Reseat iterator IT on the next visible line start in the current
4432 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4433 preceding the line start. Skip over invisible text that is so
4434 because of selective display. Compute faces, overlays etc at the
4435 new position. Note that this function does not skip over text that
4436 is invisible because of text properties. */
4437
4438 static void
4439 reseat_at_next_visible_line_start (it, on_newline_p)
4440 struct it *it;
4441 int on_newline_p;
4442 {
4443 int newline_found_p, skipped_p = 0;
4444
4445 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4446
4447 /* Skip over lines that are invisible because they are indented
4448 more than the value of IT->selective. */
4449 if (it->selective > 0)
4450 while (IT_CHARPOS (*it) < ZV
4451 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4452 (double) it->selective)) /* iftc */
4453 {
4454 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4455 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4456 }
4457
4458 /* Position on the newline if that's what's requested. */
4459 if (on_newline_p && newline_found_p)
4460 {
4461 if (STRINGP (it->string))
4462 {
4463 if (IT_STRING_CHARPOS (*it) > 0)
4464 {
4465 --IT_STRING_CHARPOS (*it);
4466 --IT_STRING_BYTEPOS (*it);
4467 }
4468 }
4469 else if (IT_CHARPOS (*it) > BEGV)
4470 {
4471 --IT_CHARPOS (*it);
4472 --IT_BYTEPOS (*it);
4473 reseat (it, it->current.pos, 0);
4474 }
4475 }
4476 else if (skipped_p)
4477 reseat (it, it->current.pos, 0);
4478
4479 CHECK_IT (it);
4480 }
4481
4482
4483 \f
4484 /***********************************************************************
4485 Changing an iterator's position
4486 ***********************************************************************/
4487
4488 /* Change IT's current position to POS in current_buffer. If FORCE_P
4489 is non-zero, always check for text properties at the new position.
4490 Otherwise, text properties are only looked up if POS >=
4491 IT->check_charpos of a property. */
4492
4493 static void
4494 reseat (it, pos, force_p)
4495 struct it *it;
4496 struct text_pos pos;
4497 int force_p;
4498 {
4499 int original_pos = IT_CHARPOS (*it);
4500
4501 reseat_1 (it, pos, 0);
4502
4503 /* Determine where to check text properties. Avoid doing it
4504 where possible because text property lookup is very expensive. */
4505 if (force_p
4506 || CHARPOS (pos) > it->stop_charpos
4507 || CHARPOS (pos) < original_pos)
4508 handle_stop (it);
4509
4510 CHECK_IT (it);
4511 }
4512
4513
4514 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4515 IT->stop_pos to POS, also. */
4516
4517 static void
4518 reseat_1 (it, pos, set_stop_p)
4519 struct it *it;
4520 struct text_pos pos;
4521 int set_stop_p;
4522 {
4523 /* Don't call this function when scanning a C string. */
4524 xassert (it->s == NULL);
4525
4526 /* POS must be a reasonable value. */
4527 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4528
4529 it->current.pos = it->position = pos;
4530 XSETBUFFER (it->object, current_buffer);
4531 it->end_charpos = ZV;
4532 it->dpvec = NULL;
4533 it->current.dpvec_index = -1;
4534 it->current.overlay_string_index = -1;
4535 IT_STRING_CHARPOS (*it) = -1;
4536 IT_STRING_BYTEPOS (*it) = -1;
4537 it->string = Qnil;
4538 it->method = next_element_from_buffer;
4539 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4540 it->sp = 0;
4541 it->face_before_selective_p = 0;
4542
4543 if (set_stop_p)
4544 it->stop_charpos = CHARPOS (pos);
4545 }
4546
4547
4548 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4549 If S is non-null, it is a C string to iterate over. Otherwise,
4550 STRING gives a Lisp string to iterate over.
4551
4552 If PRECISION > 0, don't return more then PRECISION number of
4553 characters from the string.
4554
4555 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4556 characters have been returned. FIELD_WIDTH < 0 means an infinite
4557 field width.
4558
4559 MULTIBYTE = 0 means disable processing of multibyte characters,
4560 MULTIBYTE > 0 means enable it,
4561 MULTIBYTE < 0 means use IT->multibyte_p.
4562
4563 IT must be initialized via a prior call to init_iterator before
4564 calling this function. */
4565
4566 static void
4567 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4568 struct it *it;
4569 unsigned char *s;
4570 Lisp_Object string;
4571 int charpos;
4572 int precision, field_width, multibyte;
4573 {
4574 /* No region in strings. */
4575 it->region_beg_charpos = it->region_end_charpos = -1;
4576
4577 /* No text property checks performed by default, but see below. */
4578 it->stop_charpos = -1;
4579
4580 /* Set iterator position and end position. */
4581 bzero (&it->current, sizeof it->current);
4582 it->current.overlay_string_index = -1;
4583 it->current.dpvec_index = -1;
4584 xassert (charpos >= 0);
4585
4586 /* If STRING is specified, use its multibyteness, otherwise use the
4587 setting of MULTIBYTE, if specified. */
4588 if (multibyte >= 0)
4589 it->multibyte_p = multibyte > 0;
4590
4591 if (s == NULL)
4592 {
4593 xassert (STRINGP (string));
4594 it->string = string;
4595 it->s = NULL;
4596 it->end_charpos = it->string_nchars = SCHARS (string);
4597 it->method = next_element_from_string;
4598 it->current.string_pos = string_pos (charpos, string);
4599 }
4600 else
4601 {
4602 it->s = s;
4603 it->string = Qnil;
4604
4605 /* Note that we use IT->current.pos, not it->current.string_pos,
4606 for displaying C strings. */
4607 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4608 if (it->multibyte_p)
4609 {
4610 it->current.pos = c_string_pos (charpos, s, 1);
4611 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4612 }
4613 else
4614 {
4615 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4616 it->end_charpos = it->string_nchars = strlen (s);
4617 }
4618
4619 it->method = next_element_from_c_string;
4620 }
4621
4622 /* PRECISION > 0 means don't return more than PRECISION characters
4623 from the string. */
4624 if (precision > 0 && it->end_charpos - charpos > precision)
4625 it->end_charpos = it->string_nchars = charpos + precision;
4626
4627 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4628 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4629 FIELD_WIDTH < 0 means infinite field width. This is useful for
4630 padding with `-' at the end of a mode line. */
4631 if (field_width < 0)
4632 field_width = INFINITY;
4633 if (field_width > it->end_charpos - charpos)
4634 it->end_charpos = charpos + field_width;
4635
4636 /* Use the standard display table for displaying strings. */
4637 if (DISP_TABLE_P (Vstandard_display_table))
4638 it->dp = XCHAR_TABLE (Vstandard_display_table);
4639
4640 it->stop_charpos = charpos;
4641 CHECK_IT (it);
4642 }
4643
4644
4645 \f
4646 /***********************************************************************
4647 Iteration
4648 ***********************************************************************/
4649
4650 /* Load IT's display element fields with information about the next
4651 display element from the current position of IT. Value is zero if
4652 end of buffer (or C string) is reached. */
4653
4654 int
4655 get_next_display_element (it)
4656 struct it *it;
4657 {
4658 /* Non-zero means that we found a display element. Zero means that
4659 we hit the end of what we iterate over. Performance note: the
4660 function pointer `method' used here turns out to be faster than
4661 using a sequence of if-statements. */
4662 int success_p = (*it->method) (it);
4663
4664 if (it->what == IT_CHARACTER)
4665 {
4666 /* Map via display table or translate control characters.
4667 IT->c, IT->len etc. have been set to the next character by
4668 the function call above. If we have a display table, and it
4669 contains an entry for IT->c, translate it. Don't do this if
4670 IT->c itself comes from a display table, otherwise we could
4671 end up in an infinite recursion. (An alternative could be to
4672 count the recursion depth of this function and signal an
4673 error when a certain maximum depth is reached.) Is it worth
4674 it? */
4675 if (success_p && it->dpvec == NULL)
4676 {
4677 Lisp_Object dv;
4678
4679 if (it->dp
4680 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4681 VECTORP (dv)))
4682 {
4683 struct Lisp_Vector *v = XVECTOR (dv);
4684
4685 /* Return the first character from the display table
4686 entry, if not empty. If empty, don't display the
4687 current character. */
4688 if (v->size)
4689 {
4690 it->dpvec_char_len = it->len;
4691 it->dpvec = v->contents;
4692 it->dpend = v->contents + v->size;
4693 it->current.dpvec_index = 0;
4694 it->method = next_element_from_display_vector;
4695 success_p = get_next_display_element (it);
4696 }
4697 else
4698 {
4699 set_iterator_to_next (it, 0);
4700 success_p = get_next_display_element (it);
4701 }
4702 }
4703
4704 /* Translate control characters into `\003' or `^C' form.
4705 Control characters coming from a display table entry are
4706 currently not translated because we use IT->dpvec to hold
4707 the translation. This could easily be changed but I
4708 don't believe that it is worth doing.
4709
4710 If it->multibyte_p is nonzero, eight-bit characters and
4711 non-printable multibyte characters are also translated to
4712 octal form.
4713
4714 If it->multibyte_p is zero, eight-bit characters that
4715 don't have corresponding multibyte char code are also
4716 translated to octal form. */
4717 else if ((it->c < ' '
4718 && (it->area != TEXT_AREA
4719 || (it->c != '\n' && it->c != '\t')))
4720 || (it->multibyte_p
4721 ? ((it->c >= 127
4722 && it->len == 1)
4723 || !CHAR_PRINTABLE_P (it->c))
4724 : (it->c >= 127
4725 && it->c == unibyte_char_to_multibyte (it->c))))
4726 {
4727 /* IT->c is a control character which must be displayed
4728 either as '\003' or as `^C' where the '\\' and '^'
4729 can be defined in the display table. Fill
4730 IT->ctl_chars with glyphs for what we have to
4731 display. Then, set IT->dpvec to these glyphs. */
4732 GLYPH g;
4733
4734 if (it->c < 128 && it->ctl_arrow_p)
4735 {
4736 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4737 if (it->dp
4738 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4739 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4740 g = XINT (DISP_CTRL_GLYPH (it->dp));
4741 else
4742 g = FAST_MAKE_GLYPH ('^', 0);
4743 XSETINT (it->ctl_chars[0], g);
4744
4745 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4746 XSETINT (it->ctl_chars[1], g);
4747
4748 /* Set up IT->dpvec and return first character from it. */
4749 it->dpvec_char_len = it->len;
4750 it->dpvec = it->ctl_chars;
4751 it->dpend = it->dpvec + 2;
4752 it->current.dpvec_index = 0;
4753 it->method = next_element_from_display_vector;
4754 get_next_display_element (it);
4755 }
4756 else
4757 {
4758 unsigned char str[MAX_MULTIBYTE_LENGTH];
4759 int len;
4760 int i;
4761 GLYPH escape_glyph;
4762
4763 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4764 if (it->dp
4765 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4766 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4767 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4768 else
4769 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4770
4771 if (SINGLE_BYTE_CHAR_P (it->c))
4772 str[0] = it->c, len = 1;
4773 else
4774 {
4775 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4776 if (len < 0)
4777 {
4778 /* It's an invalid character, which
4779 shouldn't happen actually, but due to
4780 bugs it may happen. Let's print the char
4781 as is, there's not much meaningful we can
4782 do with it. */
4783 str[0] = it->c;
4784 str[1] = it->c >> 8;
4785 str[2] = it->c >> 16;
4786 str[3] = it->c >> 24;
4787 len = 4;
4788 }
4789 }
4790
4791 for (i = 0; i < len; i++)
4792 {
4793 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4794 /* Insert three more glyphs into IT->ctl_chars for
4795 the octal display of the character. */
4796 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4797 XSETINT (it->ctl_chars[i * 4 + 1], g);
4798 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4799 XSETINT (it->ctl_chars[i * 4 + 2], g);
4800 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4801 XSETINT (it->ctl_chars[i * 4 + 3], g);
4802 }
4803
4804 /* Set up IT->dpvec and return the first character
4805 from it. */
4806 it->dpvec_char_len = it->len;
4807 it->dpvec = it->ctl_chars;
4808 it->dpend = it->dpvec + len * 4;
4809 it->current.dpvec_index = 0;
4810 it->method = next_element_from_display_vector;
4811 get_next_display_element (it);
4812 }
4813 }
4814 }
4815
4816 /* Adjust face id for a multibyte character. There are no
4817 multibyte character in unibyte text. */
4818 if (it->multibyte_p
4819 && success_p
4820 && FRAME_WINDOW_P (it->f))
4821 {
4822 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4823 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4824 }
4825 }
4826
4827 /* Is this character the last one of a run of characters with
4828 box? If yes, set IT->end_of_box_run_p to 1. */
4829 if (it->face_box_p
4830 && it->s == NULL)
4831 {
4832 int face_id;
4833 struct face *face;
4834
4835 it->end_of_box_run_p
4836 = ((face_id = face_after_it_pos (it),
4837 face_id != it->face_id)
4838 && (face = FACE_FROM_ID (it->f, face_id),
4839 face->box == FACE_NO_BOX));
4840 }
4841
4842 /* Value is 0 if end of buffer or string reached. */
4843 return success_p;
4844 }
4845
4846
4847 /* Move IT to the next display element.
4848
4849 RESEAT_P non-zero means if called on a newline in buffer text,
4850 skip to the next visible line start.
4851
4852 Functions get_next_display_element and set_iterator_to_next are
4853 separate because I find this arrangement easier to handle than a
4854 get_next_display_element function that also increments IT's
4855 position. The way it is we can first look at an iterator's current
4856 display element, decide whether it fits on a line, and if it does,
4857 increment the iterator position. The other way around we probably
4858 would either need a flag indicating whether the iterator has to be
4859 incremented the next time, or we would have to implement a
4860 decrement position function which would not be easy to write. */
4861
4862 void
4863 set_iterator_to_next (it, reseat_p)
4864 struct it *it;
4865 int reseat_p;
4866 {
4867 /* Reset flags indicating start and end of a sequence of characters
4868 with box. Reset them at the start of this function because
4869 moving the iterator to a new position might set them. */
4870 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4871
4872 if (it->method == next_element_from_buffer)
4873 {
4874 /* The current display element of IT is a character from
4875 current_buffer. Advance in the buffer, and maybe skip over
4876 invisible lines that are so because of selective display. */
4877 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4878 reseat_at_next_visible_line_start (it, 0);
4879 else
4880 {
4881 xassert (it->len != 0);
4882 IT_BYTEPOS (*it) += it->len;
4883 IT_CHARPOS (*it) += 1;
4884 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4885 }
4886 }
4887 else if (it->method == next_element_from_composition)
4888 {
4889 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4890 if (STRINGP (it->string))
4891 {
4892 IT_STRING_BYTEPOS (*it) += it->len;
4893 IT_STRING_CHARPOS (*it) += it->cmp_len;
4894 it->method = next_element_from_string;
4895 goto consider_string_end;
4896 }
4897 else
4898 {
4899 IT_BYTEPOS (*it) += it->len;
4900 IT_CHARPOS (*it) += it->cmp_len;
4901 it->method = next_element_from_buffer;
4902 }
4903 }
4904 else if (it->method == next_element_from_c_string)
4905 {
4906 /* Current display element of IT is from a C string. */
4907 IT_BYTEPOS (*it) += it->len;
4908 IT_CHARPOS (*it) += 1;
4909 }
4910 else if (it->method == next_element_from_display_vector)
4911 {
4912 /* Current display element of IT is from a display table entry.
4913 Advance in the display table definition. Reset it to null if
4914 end reached, and continue with characters from buffers/
4915 strings. */
4916 ++it->current.dpvec_index;
4917
4918 /* Restore face of the iterator to what they were before the
4919 display vector entry (these entries may contain faces). */
4920 it->face_id = it->saved_face_id;
4921
4922 if (it->dpvec + it->current.dpvec_index == it->dpend)
4923 {
4924 if (it->s)
4925 it->method = next_element_from_c_string;
4926 else if (STRINGP (it->string))
4927 it->method = next_element_from_string;
4928 else
4929 it->method = next_element_from_buffer;
4930
4931 it->dpvec = NULL;
4932 it->current.dpvec_index = -1;
4933
4934 /* Skip over characters which were displayed via IT->dpvec. */
4935 if (it->dpvec_char_len < 0)
4936 reseat_at_next_visible_line_start (it, 1);
4937 else if (it->dpvec_char_len > 0)
4938 {
4939 it->len = it->dpvec_char_len;
4940 set_iterator_to_next (it, reseat_p);
4941 }
4942 }
4943 }
4944 else if (it->method == next_element_from_string)
4945 {
4946 /* Current display element is a character from a Lisp string. */
4947 xassert (it->s == NULL && STRINGP (it->string));
4948 IT_STRING_BYTEPOS (*it) += it->len;
4949 IT_STRING_CHARPOS (*it) += 1;
4950
4951 consider_string_end:
4952
4953 if (it->current.overlay_string_index >= 0)
4954 {
4955 /* IT->string is an overlay string. Advance to the
4956 next, if there is one. */
4957 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
4958 next_overlay_string (it);
4959 }
4960 else
4961 {
4962 /* IT->string is not an overlay string. If we reached
4963 its end, and there is something on IT->stack, proceed
4964 with what is on the stack. This can be either another
4965 string, this time an overlay string, or a buffer. */
4966 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
4967 && it->sp > 0)
4968 {
4969 pop_it (it);
4970 if (!STRINGP (it->string))
4971 it->method = next_element_from_buffer;
4972 else
4973 goto consider_string_end;
4974 }
4975 }
4976 }
4977 else if (it->method == next_element_from_image
4978 || it->method == next_element_from_stretch)
4979 {
4980 /* The position etc with which we have to proceed are on
4981 the stack. The position may be at the end of a string,
4982 if the `display' property takes up the whole string. */
4983 pop_it (it);
4984 it->image_id = 0;
4985 if (STRINGP (it->string))
4986 {
4987 it->method = next_element_from_string;
4988 goto consider_string_end;
4989 }
4990 else
4991 it->method = next_element_from_buffer;
4992 }
4993 else
4994 /* There are no other methods defined, so this should be a bug. */
4995 abort ();
4996
4997 xassert (it->method != next_element_from_string
4998 || (STRINGP (it->string)
4999 && IT_STRING_CHARPOS (*it) >= 0));
5000 }
5001
5002
5003 /* Load IT's display element fields with information about the next
5004 display element which comes from a display table entry or from the
5005 result of translating a control character to one of the forms `^C'
5006 or `\003'. IT->dpvec holds the glyphs to return as characters. */
5007
5008 static int
5009 next_element_from_display_vector (it)
5010 struct it *it;
5011 {
5012 /* Precondition. */
5013 xassert (it->dpvec && it->current.dpvec_index >= 0);
5014
5015 /* Remember the current face id in case glyphs specify faces.
5016 IT's face is restored in set_iterator_to_next. */
5017 it->saved_face_id = it->face_id;
5018
5019 if (INTEGERP (*it->dpvec)
5020 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5021 {
5022 int lface_id;
5023 GLYPH g;
5024
5025 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5026 it->c = FAST_GLYPH_CHAR (g);
5027 it->len = CHAR_BYTES (it->c);
5028
5029 /* The entry may contain a face id to use. Such a face id is
5030 the id of a Lisp face, not a realized face. A face id of
5031 zero means no face is specified. */
5032 lface_id = FAST_GLYPH_FACE (g);
5033 if (lface_id)
5034 {
5035 /* The function returns -1 if lface_id is invalid. */
5036 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
5037 if (face_id >= 0)
5038 it->face_id = face_id;
5039 }
5040 }
5041 else
5042 /* Display table entry is invalid. Return a space. */
5043 it->c = ' ', it->len = 1;
5044
5045 /* Don't change position and object of the iterator here. They are
5046 still the values of the character that had this display table
5047 entry or was translated, and that's what we want. */
5048 it->what = IT_CHARACTER;
5049 return 1;
5050 }
5051
5052
5053 /* Load IT with the next display element from Lisp string IT->string.
5054 IT->current.string_pos is the current position within the string.
5055 If IT->current.overlay_string_index >= 0, the Lisp string is an
5056 overlay string. */
5057
5058 static int
5059 next_element_from_string (it)
5060 struct it *it;
5061 {
5062 struct text_pos position;
5063
5064 xassert (STRINGP (it->string));
5065 xassert (IT_STRING_CHARPOS (*it) >= 0);
5066 position = it->current.string_pos;
5067
5068 /* Time to check for invisible text? */
5069 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5070 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5071 {
5072 handle_stop (it);
5073
5074 /* Since a handler may have changed IT->method, we must
5075 recurse here. */
5076 return get_next_display_element (it);
5077 }
5078
5079 if (it->current.overlay_string_index >= 0)
5080 {
5081 /* Get the next character from an overlay string. In overlay
5082 strings, There is no field width or padding with spaces to
5083 do. */
5084 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5085 {
5086 it->what = IT_EOB;
5087 return 0;
5088 }
5089 else if (STRING_MULTIBYTE (it->string))
5090 {
5091 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5092 const unsigned char *s = (SDATA (it->string)
5093 + IT_STRING_BYTEPOS (*it));
5094 it->c = string_char_and_length (s, remaining, &it->len);
5095 }
5096 else
5097 {
5098 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5099 it->len = 1;
5100 }
5101 }
5102 else
5103 {
5104 /* Get the next character from a Lisp string that is not an
5105 overlay string. Such strings come from the mode line, for
5106 example. We may have to pad with spaces, or truncate the
5107 string. See also next_element_from_c_string. */
5108 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5109 {
5110 it->what = IT_EOB;
5111 return 0;
5112 }
5113 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5114 {
5115 /* Pad with spaces. */
5116 it->c = ' ', it->len = 1;
5117 CHARPOS (position) = BYTEPOS (position) = -1;
5118 }
5119 else if (STRING_MULTIBYTE (it->string))
5120 {
5121 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5122 const unsigned char *s = (SDATA (it->string)
5123 + IT_STRING_BYTEPOS (*it));
5124 it->c = string_char_and_length (s, maxlen, &it->len);
5125 }
5126 else
5127 {
5128 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5129 it->len = 1;
5130 }
5131 }
5132
5133 /* Record what we have and where it came from. Note that we store a
5134 buffer position in IT->position although it could arguably be a
5135 string position. */
5136 it->what = IT_CHARACTER;
5137 it->object = it->string;
5138 it->position = position;
5139 return 1;
5140 }
5141
5142
5143 /* Load IT with next display element from C string IT->s.
5144 IT->string_nchars is the maximum number of characters to return
5145 from the string. IT->end_charpos may be greater than
5146 IT->string_nchars when this function is called, in which case we
5147 may have to return padding spaces. Value is zero if end of string
5148 reached, including padding spaces. */
5149
5150 static int
5151 next_element_from_c_string (it)
5152 struct it *it;
5153 {
5154 int success_p = 1;
5155
5156 xassert (it->s);
5157 it->what = IT_CHARACTER;
5158 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5159 it->object = Qnil;
5160
5161 /* IT's position can be greater IT->string_nchars in case a field
5162 width or precision has been specified when the iterator was
5163 initialized. */
5164 if (IT_CHARPOS (*it) >= it->end_charpos)
5165 {
5166 /* End of the game. */
5167 it->what = IT_EOB;
5168 success_p = 0;
5169 }
5170 else if (IT_CHARPOS (*it) >= it->string_nchars)
5171 {
5172 /* Pad with spaces. */
5173 it->c = ' ', it->len = 1;
5174 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5175 }
5176 else if (it->multibyte_p)
5177 {
5178 /* Implementation note: The calls to strlen apparently aren't a
5179 performance problem because there is no noticeable performance
5180 difference between Emacs running in unibyte or multibyte mode. */
5181 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5182 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5183 maxlen, &it->len);
5184 }
5185 else
5186 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5187
5188 return success_p;
5189 }
5190
5191
5192 /* Set up IT to return characters from an ellipsis, if appropriate.
5193 The definition of the ellipsis glyphs may come from a display table
5194 entry. This function Fills IT with the first glyph from the
5195 ellipsis if an ellipsis is to be displayed. */
5196
5197 static int
5198 next_element_from_ellipsis (it)
5199 struct it *it;
5200 {
5201 if (it->selective_display_ellipsis_p)
5202 {
5203 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
5204 {
5205 /* Use the display table definition for `...'. Invalid glyphs
5206 will be handled by the method returning elements from dpvec. */
5207 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
5208 it->dpvec_char_len = it->len;
5209 it->dpvec = v->contents;
5210 it->dpend = v->contents + v->size;
5211 it->current.dpvec_index = 0;
5212 it->method = next_element_from_display_vector;
5213 }
5214 else
5215 {
5216 /* Use default `...' which is stored in default_invis_vector. */
5217 it->dpvec_char_len = it->len;
5218 it->dpvec = default_invis_vector;
5219 it->dpend = default_invis_vector + 3;
5220 it->current.dpvec_index = 0;
5221 it->method = next_element_from_display_vector;
5222 }
5223 }
5224 else
5225 {
5226 /* The face at the current position may be different from the
5227 face we find after the invisible text. Remember what it
5228 was in IT->saved_face_id, and signal that it's there by
5229 setting face_before_selective_p. */
5230 it->saved_face_id = it->face_id;
5231 it->method = next_element_from_buffer;
5232 reseat_at_next_visible_line_start (it, 1);
5233 it->face_before_selective_p = 1;
5234 }
5235
5236 return get_next_display_element (it);
5237 }
5238
5239
5240 /* Deliver an image display element. The iterator IT is already
5241 filled with image information (done in handle_display_prop). Value
5242 is always 1. */
5243
5244
5245 static int
5246 next_element_from_image (it)
5247 struct it *it;
5248 {
5249 it->what = IT_IMAGE;
5250 return 1;
5251 }
5252
5253
5254 /* Fill iterator IT with next display element from a stretch glyph
5255 property. IT->object is the value of the text property. Value is
5256 always 1. */
5257
5258 static int
5259 next_element_from_stretch (it)
5260 struct it *it;
5261 {
5262 it->what = IT_STRETCH;
5263 return 1;
5264 }
5265
5266
5267 /* Load IT with the next display element from current_buffer. Value
5268 is zero if end of buffer reached. IT->stop_charpos is the next
5269 position at which to stop and check for text properties or buffer
5270 end. */
5271
5272 static int
5273 next_element_from_buffer (it)
5274 struct it *it;
5275 {
5276 int success_p = 1;
5277
5278 /* Check this assumption, otherwise, we would never enter the
5279 if-statement, below. */
5280 xassert (IT_CHARPOS (*it) >= BEGV
5281 && IT_CHARPOS (*it) <= it->stop_charpos);
5282
5283 if (IT_CHARPOS (*it) >= it->stop_charpos)
5284 {
5285 if (IT_CHARPOS (*it) >= it->end_charpos)
5286 {
5287 int overlay_strings_follow_p;
5288
5289 /* End of the game, except when overlay strings follow that
5290 haven't been returned yet. */
5291 if (it->overlay_strings_at_end_processed_p)
5292 overlay_strings_follow_p = 0;
5293 else
5294 {
5295 it->overlay_strings_at_end_processed_p = 1;
5296 overlay_strings_follow_p = get_overlay_strings (it, 0);
5297 }
5298
5299 if (overlay_strings_follow_p)
5300 success_p = get_next_display_element (it);
5301 else
5302 {
5303 it->what = IT_EOB;
5304 it->position = it->current.pos;
5305 success_p = 0;
5306 }
5307 }
5308 else
5309 {
5310 handle_stop (it);
5311 return get_next_display_element (it);
5312 }
5313 }
5314 else
5315 {
5316 /* No face changes, overlays etc. in sight, so just return a
5317 character from current_buffer. */
5318 unsigned char *p;
5319
5320 /* Maybe run the redisplay end trigger hook. Performance note:
5321 This doesn't seem to cost measurable time. */
5322 if (it->redisplay_end_trigger_charpos
5323 && it->glyph_row
5324 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5325 run_redisplay_end_trigger_hook (it);
5326
5327 /* Get the next character, maybe multibyte. */
5328 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5329 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5330 {
5331 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5332 - IT_BYTEPOS (*it));
5333 it->c = string_char_and_length (p, maxlen, &it->len);
5334 }
5335 else
5336 it->c = *p, it->len = 1;
5337
5338 /* Record what we have and where it came from. */
5339 it->what = IT_CHARACTER;;
5340 it->object = it->w->buffer;
5341 it->position = it->current.pos;
5342
5343 /* Normally we return the character found above, except when we
5344 really want to return an ellipsis for selective display. */
5345 if (it->selective)
5346 {
5347 if (it->c == '\n')
5348 {
5349 /* A value of selective > 0 means hide lines indented more
5350 than that number of columns. */
5351 if (it->selective > 0
5352 && IT_CHARPOS (*it) + 1 < ZV
5353 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5354 IT_BYTEPOS (*it) + 1,
5355 (double) it->selective)) /* iftc */
5356 {
5357 success_p = next_element_from_ellipsis (it);
5358 it->dpvec_char_len = -1;
5359 }
5360 }
5361 else if (it->c == '\r' && it->selective == -1)
5362 {
5363 /* A value of selective == -1 means that everything from the
5364 CR to the end of the line is invisible, with maybe an
5365 ellipsis displayed for it. */
5366 success_p = next_element_from_ellipsis (it);
5367 it->dpvec_char_len = -1;
5368 }
5369 }
5370 }
5371
5372 /* Value is zero if end of buffer reached. */
5373 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5374 return success_p;
5375 }
5376
5377
5378 /* Run the redisplay end trigger hook for IT. */
5379
5380 static void
5381 run_redisplay_end_trigger_hook (it)
5382 struct it *it;
5383 {
5384 Lisp_Object args[3];
5385
5386 /* IT->glyph_row should be non-null, i.e. we should be actually
5387 displaying something, or otherwise we should not run the hook. */
5388 xassert (it->glyph_row);
5389
5390 /* Set up hook arguments. */
5391 args[0] = Qredisplay_end_trigger_functions;
5392 args[1] = it->window;
5393 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5394 it->redisplay_end_trigger_charpos = 0;
5395
5396 /* Since we are *trying* to run these functions, don't try to run
5397 them again, even if they get an error. */
5398 it->w->redisplay_end_trigger = Qnil;
5399 Frun_hook_with_args (3, args);
5400
5401 /* Notice if it changed the face of the character we are on. */
5402 handle_face_prop (it);
5403 }
5404
5405
5406 /* Deliver a composition display element. The iterator IT is already
5407 filled with composition information (done in
5408 handle_composition_prop). Value is always 1. */
5409
5410 static int
5411 next_element_from_composition (it)
5412 struct it *it;
5413 {
5414 it->what = IT_COMPOSITION;
5415 it->position = (STRINGP (it->string)
5416 ? it->current.string_pos
5417 : it->current.pos);
5418 return 1;
5419 }
5420
5421
5422 \f
5423 /***********************************************************************
5424 Moving an iterator without producing glyphs
5425 ***********************************************************************/
5426
5427 /* Move iterator IT to a specified buffer or X position within one
5428 line on the display without producing glyphs.
5429
5430 OP should be a bit mask including some or all of these bits:
5431 MOVE_TO_X: Stop on reaching x-position TO_X.
5432 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5433 Regardless of OP's value, stop in reaching the end of the display line.
5434
5435 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5436 This means, in particular, that TO_X includes window's horizontal
5437 scroll amount.
5438
5439 The return value has several possible values that
5440 say what condition caused the scan to stop:
5441
5442 MOVE_POS_MATCH_OR_ZV
5443 - when TO_POS or ZV was reached.
5444
5445 MOVE_X_REACHED
5446 -when TO_X was reached before TO_POS or ZV were reached.
5447
5448 MOVE_LINE_CONTINUED
5449 - when we reached the end of the display area and the line must
5450 be continued.
5451
5452 MOVE_LINE_TRUNCATED
5453 - when we reached the end of the display area and the line is
5454 truncated.
5455
5456 MOVE_NEWLINE_OR_CR
5457 - when we stopped at a line end, i.e. a newline or a CR and selective
5458 display is on. */
5459
5460 static enum move_it_result
5461 move_it_in_display_line_to (it, to_charpos, to_x, op)
5462 struct it *it;
5463 int to_charpos, to_x, op;
5464 {
5465 enum move_it_result result = MOVE_UNDEFINED;
5466 struct glyph_row *saved_glyph_row;
5467
5468 /* Don't produce glyphs in produce_glyphs. */
5469 saved_glyph_row = it->glyph_row;
5470 it->glyph_row = NULL;
5471
5472 while (1)
5473 {
5474 int x, i, ascent = 0, descent = 0;
5475
5476 /* Stop when ZV or TO_CHARPOS reached. */
5477 if (!get_next_display_element (it)
5478 || ((op & MOVE_TO_POS) != 0
5479 && BUFFERP (it->object)
5480 && IT_CHARPOS (*it) >= to_charpos))
5481 {
5482 result = MOVE_POS_MATCH_OR_ZV;
5483 break;
5484 }
5485
5486 /* The call to produce_glyphs will get the metrics of the
5487 display element IT is loaded with. We record in x the
5488 x-position before this display element in case it does not
5489 fit on the line. */
5490 x = it->current_x;
5491
5492 /* Remember the line height so far in case the next element doesn't
5493 fit on the line. */
5494 if (!it->truncate_lines_p)
5495 {
5496 ascent = it->max_ascent;
5497 descent = it->max_descent;
5498 }
5499
5500 PRODUCE_GLYPHS (it);
5501
5502 if (it->area != TEXT_AREA)
5503 {
5504 set_iterator_to_next (it, 1);
5505 continue;
5506 }
5507
5508 /* The number of glyphs we get back in IT->nglyphs will normally
5509 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5510 character on a terminal frame, or (iii) a line end. For the
5511 second case, IT->nglyphs - 1 padding glyphs will be present
5512 (on X frames, there is only one glyph produced for a
5513 composite character.
5514
5515 The behavior implemented below means, for continuation lines,
5516 that as many spaces of a TAB as fit on the current line are
5517 displayed there. For terminal frames, as many glyphs of a
5518 multi-glyph character are displayed in the current line, too.
5519 This is what the old redisplay code did, and we keep it that
5520 way. Under X, the whole shape of a complex character must
5521 fit on the line or it will be completely displayed in the
5522 next line.
5523
5524 Note that both for tabs and padding glyphs, all glyphs have
5525 the same width. */
5526 if (it->nglyphs)
5527 {
5528 /* More than one glyph or glyph doesn't fit on line. All
5529 glyphs have the same width. */
5530 int single_glyph_width = it->pixel_width / it->nglyphs;
5531 int new_x;
5532
5533 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5534 {
5535 new_x = x + single_glyph_width;
5536
5537 /* We want to leave anything reaching TO_X to the caller. */
5538 if ((op & MOVE_TO_X) && new_x > to_x)
5539 {
5540 it->current_x = x;
5541 result = MOVE_X_REACHED;
5542 break;
5543 }
5544 else if (/* Lines are continued. */
5545 !it->truncate_lines_p
5546 && (/* And glyph doesn't fit on the line. */
5547 new_x > it->last_visible_x
5548 /* Or it fits exactly and we're on a window
5549 system frame. */
5550 || (new_x == it->last_visible_x
5551 && FRAME_WINDOW_P (it->f))))
5552 {
5553 if (/* IT->hpos == 0 means the very first glyph
5554 doesn't fit on the line, e.g. a wide image. */
5555 it->hpos == 0
5556 || (new_x == it->last_visible_x
5557 && FRAME_WINDOW_P (it->f)))
5558 {
5559 ++it->hpos;
5560 it->current_x = new_x;
5561 if (i == it->nglyphs - 1)
5562 set_iterator_to_next (it, 1);
5563 }
5564 else
5565 {
5566 it->current_x = x;
5567 it->max_ascent = ascent;
5568 it->max_descent = descent;
5569 }
5570
5571 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5572 IT_CHARPOS (*it)));
5573 result = MOVE_LINE_CONTINUED;
5574 break;
5575 }
5576 else if (new_x > it->first_visible_x)
5577 {
5578 /* Glyph is visible. Increment number of glyphs that
5579 would be displayed. */
5580 ++it->hpos;
5581 }
5582 else
5583 {
5584 /* Glyph is completely off the left margin of the display
5585 area. Nothing to do. */
5586 }
5587 }
5588
5589 if (result != MOVE_UNDEFINED)
5590 break;
5591 }
5592 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5593 {
5594 /* Stop when TO_X specified and reached. This check is
5595 necessary here because of lines consisting of a line end,
5596 only. The line end will not produce any glyphs and we
5597 would never get MOVE_X_REACHED. */
5598 xassert (it->nglyphs == 0);
5599 result = MOVE_X_REACHED;
5600 break;
5601 }
5602
5603 /* Is this a line end? If yes, we're done. */
5604 if (ITERATOR_AT_END_OF_LINE_P (it))
5605 {
5606 result = MOVE_NEWLINE_OR_CR;
5607 break;
5608 }
5609
5610 /* The current display element has been consumed. Advance
5611 to the next. */
5612 set_iterator_to_next (it, 1);
5613
5614 /* Stop if lines are truncated and IT's current x-position is
5615 past the right edge of the window now. */
5616 if (it->truncate_lines_p
5617 && it->current_x >= it->last_visible_x)
5618 {
5619 result = MOVE_LINE_TRUNCATED;
5620 break;
5621 }
5622 }
5623
5624 /* Restore the iterator settings altered at the beginning of this
5625 function. */
5626 it->glyph_row = saved_glyph_row;
5627 return result;
5628 }
5629
5630
5631 /* Move IT forward until it satisfies one or more of the criteria in
5632 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5633
5634 OP is a bit-mask that specifies where to stop, and in particular,
5635 which of those four position arguments makes a difference. See the
5636 description of enum move_operation_enum.
5637
5638 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5639 screen line, this function will set IT to the next position >
5640 TO_CHARPOS. */
5641
5642 void
5643 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5644 struct it *it;
5645 int to_charpos, to_x, to_y, to_vpos;
5646 int op;
5647 {
5648 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5649 int line_height;
5650 int reached = 0;
5651
5652 for (;;)
5653 {
5654 if (op & MOVE_TO_VPOS)
5655 {
5656 /* If no TO_CHARPOS and no TO_X specified, stop at the
5657 start of the line TO_VPOS. */
5658 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5659 {
5660 if (it->vpos == to_vpos)
5661 {
5662 reached = 1;
5663 break;
5664 }
5665 else
5666 skip = move_it_in_display_line_to (it, -1, -1, 0);
5667 }
5668 else
5669 {
5670 /* TO_VPOS >= 0 means stop at TO_X in the line at
5671 TO_VPOS, or at TO_POS, whichever comes first. */
5672 if (it->vpos == to_vpos)
5673 {
5674 reached = 2;
5675 break;
5676 }
5677
5678 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5679
5680 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5681 {
5682 reached = 3;
5683 break;
5684 }
5685 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5686 {
5687 /* We have reached TO_X but not in the line we want. */
5688 skip = move_it_in_display_line_to (it, to_charpos,
5689 -1, MOVE_TO_POS);
5690 if (skip == MOVE_POS_MATCH_OR_ZV)
5691 {
5692 reached = 4;
5693 break;
5694 }
5695 }
5696 }
5697 }
5698 else if (op & MOVE_TO_Y)
5699 {
5700 struct it it_backup;
5701
5702 /* TO_Y specified means stop at TO_X in the line containing
5703 TO_Y---or at TO_CHARPOS if this is reached first. The
5704 problem is that we can't really tell whether the line
5705 contains TO_Y before we have completely scanned it, and
5706 this may skip past TO_X. What we do is to first scan to
5707 TO_X.
5708
5709 If TO_X is not specified, use a TO_X of zero. The reason
5710 is to make the outcome of this function more predictable.
5711 If we didn't use TO_X == 0, we would stop at the end of
5712 the line which is probably not what a caller would expect
5713 to happen. */
5714 skip = move_it_in_display_line_to (it, to_charpos,
5715 ((op & MOVE_TO_X)
5716 ? to_x : 0),
5717 (MOVE_TO_X
5718 | (op & MOVE_TO_POS)));
5719
5720 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5721 if (skip == MOVE_POS_MATCH_OR_ZV)
5722 {
5723 reached = 5;
5724 break;
5725 }
5726
5727 /* If TO_X was reached, we would like to know whether TO_Y
5728 is in the line. This can only be said if we know the
5729 total line height which requires us to scan the rest of
5730 the line. */
5731 if (skip == MOVE_X_REACHED)
5732 {
5733 it_backup = *it;
5734 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5735 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5736 op & MOVE_TO_POS);
5737 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5738 }
5739
5740 /* Now, decide whether TO_Y is in this line. */
5741 line_height = it->max_ascent + it->max_descent;
5742 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5743
5744 if (to_y >= it->current_y
5745 && to_y < it->current_y + line_height)
5746 {
5747 if (skip == MOVE_X_REACHED)
5748 /* If TO_Y is in this line and TO_X was reached above,
5749 we scanned too far. We have to restore IT's settings
5750 to the ones before skipping. */
5751 *it = it_backup;
5752 reached = 6;
5753 }
5754 else if (skip == MOVE_X_REACHED)
5755 {
5756 skip = skip2;
5757 if (skip == MOVE_POS_MATCH_OR_ZV)
5758 reached = 7;
5759 }
5760
5761 if (reached)
5762 break;
5763 }
5764 else
5765 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5766
5767 switch (skip)
5768 {
5769 case MOVE_POS_MATCH_OR_ZV:
5770 reached = 8;
5771 goto out;
5772
5773 case MOVE_NEWLINE_OR_CR:
5774 set_iterator_to_next (it, 1);
5775 it->continuation_lines_width = 0;
5776 break;
5777
5778 case MOVE_LINE_TRUNCATED:
5779 it->continuation_lines_width = 0;
5780 reseat_at_next_visible_line_start (it, 0);
5781 if ((op & MOVE_TO_POS) != 0
5782 && IT_CHARPOS (*it) > to_charpos)
5783 {
5784 reached = 9;
5785 goto out;
5786 }
5787 break;
5788
5789 case MOVE_LINE_CONTINUED:
5790 it->continuation_lines_width += it->current_x;
5791 break;
5792
5793 default:
5794 abort ();
5795 }
5796
5797 /* Reset/increment for the next run. */
5798 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5799 it->current_x = it->hpos = 0;
5800 it->current_y += it->max_ascent + it->max_descent;
5801 ++it->vpos;
5802 last_height = it->max_ascent + it->max_descent;
5803 last_max_ascent = it->max_ascent;
5804 it->max_ascent = it->max_descent = 0;
5805 }
5806
5807 out:
5808
5809 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5810 }
5811
5812
5813 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5814
5815 If DY > 0, move IT backward at least that many pixels. DY = 0
5816 means move IT backward to the preceding line start or BEGV. This
5817 function may move over more than DY pixels if IT->current_y - DY
5818 ends up in the middle of a line; in this case IT->current_y will be
5819 set to the top of the line moved to. */
5820
5821 void
5822 move_it_vertically_backward (it, dy)
5823 struct it *it;
5824 int dy;
5825 {
5826 int nlines, h;
5827 struct it it2, it3;
5828 int start_pos = IT_CHARPOS (*it);
5829
5830 xassert (dy >= 0);
5831
5832 /* Estimate how many newlines we must move back. */
5833 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
5834
5835 /* Set the iterator's position that many lines back. */
5836 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5837 back_to_previous_visible_line_start (it);
5838
5839 /* Reseat the iterator here. When moving backward, we don't want
5840 reseat to skip forward over invisible text, set up the iterator
5841 to deliver from overlay strings at the new position etc. So,
5842 use reseat_1 here. */
5843 reseat_1 (it, it->current.pos, 1);
5844
5845 /* We are now surely at a line start. */
5846 it->current_x = it->hpos = 0;
5847 it->continuation_lines_width = 0;
5848
5849 /* Move forward and see what y-distance we moved. First move to the
5850 start of the next line so that we get its height. We need this
5851 height to be able to tell whether we reached the specified
5852 y-distance. */
5853 it2 = *it;
5854 it2.max_ascent = it2.max_descent = 0;
5855 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5856 MOVE_TO_POS | MOVE_TO_VPOS);
5857 xassert (IT_CHARPOS (*it) >= BEGV);
5858 it3 = it2;
5859
5860 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5861 xassert (IT_CHARPOS (*it) >= BEGV);
5862 /* H is the actual vertical distance from the position in *IT
5863 and the starting position. */
5864 h = it2.current_y - it->current_y;
5865 /* NLINES is the distance in number of lines. */
5866 nlines = it2.vpos - it->vpos;
5867
5868 /* Correct IT's y and vpos position
5869 so that they are relative to the starting point. */
5870 it->vpos -= nlines;
5871 it->current_y -= h;
5872
5873 if (dy == 0)
5874 {
5875 /* DY == 0 means move to the start of the screen line. The
5876 value of nlines is > 0 if continuation lines were involved. */
5877 if (nlines > 0)
5878 move_it_by_lines (it, nlines, 1);
5879 xassert (IT_CHARPOS (*it) <= start_pos);
5880 }
5881 else
5882 {
5883 /* The y-position we try to reach, relative to *IT.
5884 Note that H has been subtracted in front of the if-statement. */
5885 int target_y = it->current_y + h - dy;
5886 int y0 = it3.current_y;
5887 int y1 = line_bottom_y (&it3);
5888 int line_height = y1 - y0;
5889
5890 /* If we did not reach target_y, try to move further backward if
5891 we can. If we moved too far backward, try to move forward. */
5892 if (target_y < it->current_y
5893 /* This is heuristic. In a window that's 3 lines high, with
5894 a line height of 13 pixels each, recentering with point
5895 on the bottom line will try to move -39/2 = 19 pixels
5896 backward. Try to avoid moving into the first line. */
5897 && it->current_y - target_y > line_height / 3 * 2
5898 && IT_CHARPOS (*it) > BEGV)
5899 {
5900 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
5901 target_y - it->current_y));
5902 move_it_vertically (it, target_y - it->current_y);
5903 xassert (IT_CHARPOS (*it) >= BEGV);
5904 }
5905 else if (target_y >= it->current_y + line_height
5906 && IT_CHARPOS (*it) < ZV)
5907 {
5908 /* Should move forward by at least one line, maybe more.
5909
5910 Note: Calling move_it_by_lines can be expensive on
5911 terminal frames, where compute_motion is used (via
5912 vmotion) to do the job, when there are very long lines
5913 and truncate-lines is nil. That's the reason for
5914 treating terminal frames specially here. */
5915
5916 if (!FRAME_WINDOW_P (it->f))
5917 move_it_vertically (it, target_y - (it->current_y + line_height));
5918 else
5919 {
5920 do
5921 {
5922 move_it_by_lines (it, 1, 1);
5923 }
5924 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
5925 }
5926
5927 xassert (IT_CHARPOS (*it) >= BEGV);
5928 }
5929 }
5930 }
5931
5932
5933 /* Move IT by a specified amount of pixel lines DY. DY negative means
5934 move backwards. DY = 0 means move to start of screen line. At the
5935 end, IT will be on the start of a screen line. */
5936
5937 void
5938 move_it_vertically (it, dy)
5939 struct it *it;
5940 int dy;
5941 {
5942 if (dy <= 0)
5943 move_it_vertically_backward (it, -dy);
5944 else if (dy > 0)
5945 {
5946 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5947 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5948 MOVE_TO_POS | MOVE_TO_Y);
5949 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5950
5951 /* If buffer ends in ZV without a newline, move to the start of
5952 the line to satisfy the post-condition. */
5953 if (IT_CHARPOS (*it) == ZV
5954 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5955 move_it_by_lines (it, 0, 0);
5956 }
5957 }
5958
5959
5960 /* Move iterator IT past the end of the text line it is in. */
5961
5962 void
5963 move_it_past_eol (it)
5964 struct it *it;
5965 {
5966 enum move_it_result rc;
5967
5968 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5969 if (rc == MOVE_NEWLINE_OR_CR)
5970 set_iterator_to_next (it, 0);
5971 }
5972
5973
5974 #if 0 /* Currently not used. */
5975
5976 /* Return non-zero if some text between buffer positions START_CHARPOS
5977 and END_CHARPOS is invisible. IT->window is the window for text
5978 property lookup. */
5979
5980 static int
5981 invisible_text_between_p (it, start_charpos, end_charpos)
5982 struct it *it;
5983 int start_charpos, end_charpos;
5984 {
5985 Lisp_Object prop, limit;
5986 int invisible_found_p;
5987
5988 xassert (it != NULL && start_charpos <= end_charpos);
5989
5990 /* Is text at START invisible? */
5991 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5992 it->window);
5993 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5994 invisible_found_p = 1;
5995 else
5996 {
5997 limit = Fnext_single_char_property_change (make_number (start_charpos),
5998 Qinvisible, Qnil,
5999 make_number (end_charpos));
6000 invisible_found_p = XFASTINT (limit) < end_charpos;
6001 }
6002
6003 return invisible_found_p;
6004 }
6005
6006 #endif /* 0 */
6007
6008
6009 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6010 negative means move up. DVPOS == 0 means move to the start of the
6011 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6012 NEED_Y_P is zero, IT->current_y will be left unchanged.
6013
6014 Further optimization ideas: If we would know that IT->f doesn't use
6015 a face with proportional font, we could be faster for
6016 truncate-lines nil. */
6017
6018 void
6019 move_it_by_lines (it, dvpos, need_y_p)
6020 struct it *it;
6021 int dvpos, need_y_p;
6022 {
6023 struct position pos;
6024
6025 if (!FRAME_WINDOW_P (it->f))
6026 {
6027 struct text_pos textpos;
6028
6029 /* We can use vmotion on frames without proportional fonts. */
6030 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6031 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6032 reseat (it, textpos, 1);
6033 it->vpos += pos.vpos;
6034 it->current_y += pos.vpos;
6035 }
6036 else if (dvpos == 0)
6037 {
6038 /* DVPOS == 0 means move to the start of the screen line. */
6039 move_it_vertically_backward (it, 0);
6040 xassert (it->current_x == 0 && it->hpos == 0);
6041 }
6042 else if (dvpos > 0)
6043 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6044 else
6045 {
6046 struct it it2;
6047 int start_charpos, i;
6048
6049 /* Start at the beginning of the screen line containing IT's
6050 position. */
6051 move_it_vertically_backward (it, 0);
6052
6053 /* Go back -DVPOS visible lines and reseat the iterator there. */
6054 start_charpos = IT_CHARPOS (*it);
6055 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6056 back_to_previous_visible_line_start (it);
6057 reseat (it, it->current.pos, 1);
6058 it->current_x = it->hpos = 0;
6059
6060 /* Above call may have moved too far if continuation lines
6061 are involved. Scan forward and see if it did. */
6062 it2 = *it;
6063 it2.vpos = it2.current_y = 0;
6064 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6065 it->vpos -= it2.vpos;
6066 it->current_y -= it2.current_y;
6067 it->current_x = it->hpos = 0;
6068
6069 /* If we moved too far, move IT some lines forward. */
6070 if (it2.vpos > -dvpos)
6071 {
6072 int delta = it2.vpos + dvpos;
6073 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6074 }
6075 }
6076 }
6077
6078 /* Return 1 if IT points into the middle of a display vector. */
6079
6080 int
6081 in_display_vector_p (it)
6082 struct it *it;
6083 {
6084 return (it->method == next_element_from_display_vector
6085 && it->current.dpvec_index > 0
6086 && it->dpvec + it->current.dpvec_index != it->dpend);
6087 }
6088
6089 \f
6090 /***********************************************************************
6091 Messages
6092 ***********************************************************************/
6093
6094
6095 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6096 to *Messages*. */
6097
6098 void
6099 add_to_log (format, arg1, arg2)
6100 char *format;
6101 Lisp_Object arg1, arg2;
6102 {
6103 Lisp_Object args[3];
6104 Lisp_Object msg, fmt;
6105 char *buffer;
6106 int len;
6107 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6108
6109 /* Do nothing if called asynchronously. Inserting text into
6110 a buffer may call after-change-functions and alike and
6111 that would means running Lisp asynchronously. */
6112 if (handling_signal)
6113 return;
6114
6115 fmt = msg = Qnil;
6116 GCPRO4 (fmt, msg, arg1, arg2);
6117
6118 args[0] = fmt = build_string (format);
6119 args[1] = arg1;
6120 args[2] = arg2;
6121 msg = Fformat (3, args);
6122
6123 len = SBYTES (msg) + 1;
6124 buffer = (char *) alloca (len);
6125 bcopy (SDATA (msg), buffer, len);
6126
6127 message_dolog (buffer, len - 1, 1, 0);
6128 UNGCPRO;
6129 }
6130
6131
6132 /* Output a newline in the *Messages* buffer if "needs" one. */
6133
6134 void
6135 message_log_maybe_newline ()
6136 {
6137 if (message_log_need_newline)
6138 message_dolog ("", 0, 1, 0);
6139 }
6140
6141
6142 /* Add a string M of length NBYTES to the message log, optionally
6143 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6144 nonzero, means interpret the contents of M as multibyte. This
6145 function calls low-level routines in order to bypass text property
6146 hooks, etc. which might not be safe to run. */
6147
6148 void
6149 message_dolog (m, nbytes, nlflag, multibyte)
6150 const char *m;
6151 int nbytes, nlflag, multibyte;
6152 {
6153 if (!NILP (Vmemory_full))
6154 return;
6155
6156 if (!NILP (Vmessage_log_max))
6157 {
6158 struct buffer *oldbuf;
6159 Lisp_Object oldpoint, oldbegv, oldzv;
6160 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6161 int point_at_end = 0;
6162 int zv_at_end = 0;
6163 Lisp_Object old_deactivate_mark, tem;
6164 struct gcpro gcpro1;
6165
6166 old_deactivate_mark = Vdeactivate_mark;
6167 oldbuf = current_buffer;
6168 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6169 current_buffer->undo_list = Qt;
6170
6171 oldpoint = message_dolog_marker1;
6172 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6173 oldbegv = message_dolog_marker2;
6174 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6175 oldzv = message_dolog_marker3;
6176 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6177 GCPRO1 (old_deactivate_mark);
6178
6179 if (PT == Z)
6180 point_at_end = 1;
6181 if (ZV == Z)
6182 zv_at_end = 1;
6183
6184 BEGV = BEG;
6185 BEGV_BYTE = BEG_BYTE;
6186 ZV = Z;
6187 ZV_BYTE = Z_BYTE;
6188 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6189
6190 /* Insert the string--maybe converting multibyte to single byte
6191 or vice versa, so that all the text fits the buffer. */
6192 if (multibyte
6193 && NILP (current_buffer->enable_multibyte_characters))
6194 {
6195 int i, c, char_bytes;
6196 unsigned char work[1];
6197
6198 /* Convert a multibyte string to single-byte
6199 for the *Message* buffer. */
6200 for (i = 0; i < nbytes; i += char_bytes)
6201 {
6202 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6203 work[0] = (SINGLE_BYTE_CHAR_P (c)
6204 ? c
6205 : multibyte_char_to_unibyte (c, Qnil));
6206 insert_1_both (work, 1, 1, 1, 0, 0);
6207 }
6208 }
6209 else if (! multibyte
6210 && ! NILP (current_buffer->enable_multibyte_characters))
6211 {
6212 int i, c, char_bytes;
6213 unsigned char *msg = (unsigned char *) m;
6214 unsigned char str[MAX_MULTIBYTE_LENGTH];
6215 /* Convert a single-byte string to multibyte
6216 for the *Message* buffer. */
6217 for (i = 0; i < nbytes; i++)
6218 {
6219 c = unibyte_char_to_multibyte (msg[i]);
6220 char_bytes = CHAR_STRING (c, str);
6221 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6222 }
6223 }
6224 else if (nbytes)
6225 insert_1 (m, nbytes, 1, 0, 0);
6226
6227 if (nlflag)
6228 {
6229 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6230 insert_1 ("\n", 1, 1, 0, 0);
6231
6232 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6233 this_bol = PT;
6234 this_bol_byte = PT_BYTE;
6235
6236 /* See if this line duplicates the previous one.
6237 If so, combine duplicates. */
6238 if (this_bol > BEG)
6239 {
6240 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6241 prev_bol = PT;
6242 prev_bol_byte = PT_BYTE;
6243
6244 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6245 this_bol, this_bol_byte);
6246 if (dup)
6247 {
6248 del_range_both (prev_bol, prev_bol_byte,
6249 this_bol, this_bol_byte, 0);
6250 if (dup > 1)
6251 {
6252 char dupstr[40];
6253 int duplen;
6254
6255 /* If you change this format, don't forget to also
6256 change message_log_check_duplicate. */
6257 sprintf (dupstr, " [%d times]", dup);
6258 duplen = strlen (dupstr);
6259 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6260 insert_1 (dupstr, duplen, 1, 0, 1);
6261 }
6262 }
6263 }
6264
6265 /* If we have more than the desired maximum number of lines
6266 in the *Messages* buffer now, delete the oldest ones.
6267 This is safe because we don't have undo in this buffer. */
6268
6269 if (NATNUMP (Vmessage_log_max))
6270 {
6271 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6272 -XFASTINT (Vmessage_log_max) - 1, 0);
6273 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6274 }
6275 }
6276 BEGV = XMARKER (oldbegv)->charpos;
6277 BEGV_BYTE = marker_byte_position (oldbegv);
6278
6279 if (zv_at_end)
6280 {
6281 ZV = Z;
6282 ZV_BYTE = Z_BYTE;
6283 }
6284 else
6285 {
6286 ZV = XMARKER (oldzv)->charpos;
6287 ZV_BYTE = marker_byte_position (oldzv);
6288 }
6289
6290 if (point_at_end)
6291 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6292 else
6293 /* We can't do Fgoto_char (oldpoint) because it will run some
6294 Lisp code. */
6295 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6296 XMARKER (oldpoint)->bytepos);
6297
6298 UNGCPRO;
6299 unchain_marker (oldpoint);
6300 unchain_marker (oldbegv);
6301 unchain_marker (oldzv);
6302
6303 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6304 set_buffer_internal (oldbuf);
6305 if (NILP (tem))
6306 windows_or_buffers_changed = old_windows_or_buffers_changed;
6307 message_log_need_newline = !nlflag;
6308 Vdeactivate_mark = old_deactivate_mark;
6309 }
6310 }
6311
6312
6313 /* We are at the end of the buffer after just having inserted a newline.
6314 (Note: We depend on the fact we won't be crossing the gap.)
6315 Check to see if the most recent message looks a lot like the previous one.
6316 Return 0 if different, 1 if the new one should just replace it, or a
6317 value N > 1 if we should also append " [N times]". */
6318
6319 static int
6320 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6321 int prev_bol, this_bol;
6322 int prev_bol_byte, this_bol_byte;
6323 {
6324 int i;
6325 int len = Z_BYTE - 1 - this_bol_byte;
6326 int seen_dots = 0;
6327 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6328 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6329
6330 for (i = 0; i < len; i++)
6331 {
6332 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6333 seen_dots = 1;
6334 if (p1[i] != p2[i])
6335 return seen_dots;
6336 }
6337 p1 += len;
6338 if (*p1 == '\n')
6339 return 2;
6340 if (*p1++ == ' ' && *p1++ == '[')
6341 {
6342 int n = 0;
6343 while (*p1 >= '0' && *p1 <= '9')
6344 n = n * 10 + *p1++ - '0';
6345 if (strncmp (p1, " times]\n", 8) == 0)
6346 return n+1;
6347 }
6348 return 0;
6349 }
6350
6351
6352 /* Display an echo area message M with a specified length of NBYTES
6353 bytes. The string may include null characters. If M is 0, clear
6354 out any existing message, and let the mini-buffer text show
6355 through.
6356
6357 The buffer M must continue to exist until after the echo area gets
6358 cleared or some other message gets displayed there. This means do
6359 not pass text that is stored in a Lisp string; do not pass text in
6360 a buffer that was alloca'd. */
6361
6362 void
6363 message2 (m, nbytes, multibyte)
6364 const char *m;
6365 int nbytes;
6366 int multibyte;
6367 {
6368 /* First flush out any partial line written with print. */
6369 message_log_maybe_newline ();
6370 if (m)
6371 message_dolog (m, nbytes, 1, multibyte);
6372 message2_nolog (m, nbytes, multibyte);
6373 }
6374
6375
6376 /* The non-logging counterpart of message2. */
6377
6378 void
6379 message2_nolog (m, nbytes, multibyte)
6380 const char *m;
6381 int nbytes, multibyte;
6382 {
6383 struct frame *sf = SELECTED_FRAME ();
6384 message_enable_multibyte = multibyte;
6385
6386 if (noninteractive)
6387 {
6388 if (noninteractive_need_newline)
6389 putc ('\n', stderr);
6390 noninteractive_need_newline = 0;
6391 if (m)
6392 fwrite (m, nbytes, 1, stderr);
6393 if (cursor_in_echo_area == 0)
6394 fprintf (stderr, "\n");
6395 fflush (stderr);
6396 }
6397 /* A null message buffer means that the frame hasn't really been
6398 initialized yet. Error messages get reported properly by
6399 cmd_error, so this must be just an informative message; toss it. */
6400 else if (INTERACTIVE
6401 && sf->glyphs_initialized_p
6402 && FRAME_MESSAGE_BUF (sf))
6403 {
6404 Lisp_Object mini_window;
6405 struct frame *f;
6406
6407 /* Get the frame containing the mini-buffer
6408 that the selected frame is using. */
6409 mini_window = FRAME_MINIBUF_WINDOW (sf);
6410 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6411
6412 FRAME_SAMPLE_VISIBILITY (f);
6413 if (FRAME_VISIBLE_P (sf)
6414 && ! FRAME_VISIBLE_P (f))
6415 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6416
6417 if (m)
6418 {
6419 set_message (m, Qnil, nbytes, multibyte);
6420 if (minibuffer_auto_raise)
6421 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6422 }
6423 else
6424 clear_message (1, 1);
6425
6426 do_pending_window_change (0);
6427 echo_area_display (1);
6428 do_pending_window_change (0);
6429 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6430 (*frame_up_to_date_hook) (f);
6431 }
6432 }
6433
6434
6435 /* Display an echo area message M with a specified length of NBYTES
6436 bytes. The string may include null characters. If M is not a
6437 string, clear out any existing message, and let the mini-buffer
6438 text show through. */
6439
6440 void
6441 message3 (m, nbytes, multibyte)
6442 Lisp_Object m;
6443 int nbytes;
6444 int multibyte;
6445 {
6446 struct gcpro gcpro1;
6447
6448 GCPRO1 (m);
6449
6450 /* First flush out any partial line written with print. */
6451 message_log_maybe_newline ();
6452 if (STRINGP (m))
6453 message_dolog (SDATA (m), nbytes, 1, multibyte);
6454 message3_nolog (m, nbytes, multibyte);
6455
6456 UNGCPRO;
6457 }
6458
6459
6460 /* The non-logging version of message3. */
6461
6462 void
6463 message3_nolog (m, nbytes, multibyte)
6464 Lisp_Object m;
6465 int nbytes, multibyte;
6466 {
6467 struct frame *sf = SELECTED_FRAME ();
6468 message_enable_multibyte = multibyte;
6469
6470 if (noninteractive)
6471 {
6472 if (noninteractive_need_newline)
6473 putc ('\n', stderr);
6474 noninteractive_need_newline = 0;
6475 if (STRINGP (m))
6476 fwrite (SDATA (m), nbytes, 1, stderr);
6477 if (cursor_in_echo_area == 0)
6478 fprintf (stderr, "\n");
6479 fflush (stderr);
6480 }
6481 /* A null message buffer means that the frame hasn't really been
6482 initialized yet. Error messages get reported properly by
6483 cmd_error, so this must be just an informative message; toss it. */
6484 else if (INTERACTIVE
6485 && sf->glyphs_initialized_p
6486 && FRAME_MESSAGE_BUF (sf))
6487 {
6488 Lisp_Object mini_window;
6489 Lisp_Object frame;
6490 struct frame *f;
6491
6492 /* Get the frame containing the mini-buffer
6493 that the selected frame is using. */
6494 mini_window = FRAME_MINIBUF_WINDOW (sf);
6495 frame = XWINDOW (mini_window)->frame;
6496 f = XFRAME (frame);
6497
6498 FRAME_SAMPLE_VISIBILITY (f);
6499 if (FRAME_VISIBLE_P (sf)
6500 && !FRAME_VISIBLE_P (f))
6501 Fmake_frame_visible (frame);
6502
6503 if (STRINGP (m) && SCHARS (m) > 0)
6504 {
6505 set_message (NULL, m, nbytes, multibyte);
6506 if (minibuffer_auto_raise)
6507 Fraise_frame (frame);
6508 }
6509 else
6510 clear_message (1, 1);
6511
6512 do_pending_window_change (0);
6513 echo_area_display (1);
6514 do_pending_window_change (0);
6515 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6516 (*frame_up_to_date_hook) (f);
6517 }
6518 }
6519
6520
6521 /* Display a null-terminated echo area message M. If M is 0, clear
6522 out any existing message, and let the mini-buffer text show through.
6523
6524 The buffer M must continue to exist until after the echo area gets
6525 cleared or some other message gets displayed there. Do not pass
6526 text that is stored in a Lisp string. Do not pass text in a buffer
6527 that was alloca'd. */
6528
6529 void
6530 message1 (m)
6531 char *m;
6532 {
6533 message2 (m, (m ? strlen (m) : 0), 0);
6534 }
6535
6536
6537 /* The non-logging counterpart of message1. */
6538
6539 void
6540 message1_nolog (m)
6541 char *m;
6542 {
6543 message2_nolog (m, (m ? strlen (m) : 0), 0);
6544 }
6545
6546 /* Display a message M which contains a single %s
6547 which gets replaced with STRING. */
6548
6549 void
6550 message_with_string (m, string, log)
6551 char *m;
6552 Lisp_Object string;
6553 int log;
6554 {
6555 CHECK_STRING (string);
6556
6557 if (noninteractive)
6558 {
6559 if (m)
6560 {
6561 if (noninteractive_need_newline)
6562 putc ('\n', stderr);
6563 noninteractive_need_newline = 0;
6564 fprintf (stderr, m, SDATA (string));
6565 if (cursor_in_echo_area == 0)
6566 fprintf (stderr, "\n");
6567 fflush (stderr);
6568 }
6569 }
6570 else if (INTERACTIVE)
6571 {
6572 /* The frame whose minibuffer we're going to display the message on.
6573 It may be larger than the selected frame, so we need
6574 to use its buffer, not the selected frame's buffer. */
6575 Lisp_Object mini_window;
6576 struct frame *f, *sf = SELECTED_FRAME ();
6577
6578 /* Get the frame containing the minibuffer
6579 that the selected frame is using. */
6580 mini_window = FRAME_MINIBUF_WINDOW (sf);
6581 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6582
6583 /* A null message buffer means that the frame hasn't really been
6584 initialized yet. Error messages get reported properly by
6585 cmd_error, so this must be just an informative message; toss it. */
6586 if (FRAME_MESSAGE_BUF (f))
6587 {
6588 Lisp_Object args[2], message;
6589 struct gcpro gcpro1, gcpro2;
6590
6591 args[0] = build_string (m);
6592 args[1] = message = string;
6593 GCPRO2 (args[0], message);
6594 gcpro1.nvars = 2;
6595
6596 message = Fformat (2, args);
6597
6598 if (log)
6599 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6600 else
6601 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6602
6603 UNGCPRO;
6604
6605 /* Print should start at the beginning of the message
6606 buffer next time. */
6607 message_buf_print = 0;
6608 }
6609 }
6610 }
6611
6612
6613 /* Dump an informative message to the minibuf. If M is 0, clear out
6614 any existing message, and let the mini-buffer text show through. */
6615
6616 /* VARARGS 1 */
6617 void
6618 message (m, a1, a2, a3)
6619 char *m;
6620 EMACS_INT a1, a2, a3;
6621 {
6622 if (noninteractive)
6623 {
6624 if (m)
6625 {
6626 if (noninteractive_need_newline)
6627 putc ('\n', stderr);
6628 noninteractive_need_newline = 0;
6629 fprintf (stderr, m, a1, a2, a3);
6630 if (cursor_in_echo_area == 0)
6631 fprintf (stderr, "\n");
6632 fflush (stderr);
6633 }
6634 }
6635 else if (INTERACTIVE)
6636 {
6637 /* The frame whose mini-buffer we're going to display the message
6638 on. It may be larger than the selected frame, so we need to
6639 use its buffer, not the selected frame's buffer. */
6640 Lisp_Object mini_window;
6641 struct frame *f, *sf = SELECTED_FRAME ();
6642
6643 /* Get the frame containing the mini-buffer
6644 that the selected frame is using. */
6645 mini_window = FRAME_MINIBUF_WINDOW (sf);
6646 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6647
6648 /* A null message buffer means that the frame hasn't really been
6649 initialized yet. Error messages get reported properly by
6650 cmd_error, so this must be just an informative message; toss
6651 it. */
6652 if (FRAME_MESSAGE_BUF (f))
6653 {
6654 if (m)
6655 {
6656 int len;
6657 #ifdef NO_ARG_ARRAY
6658 char *a[3];
6659 a[0] = (char *) a1;
6660 a[1] = (char *) a2;
6661 a[2] = (char *) a3;
6662
6663 len = doprnt (FRAME_MESSAGE_BUF (f),
6664 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6665 #else
6666 len = doprnt (FRAME_MESSAGE_BUF (f),
6667 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6668 (char **) &a1);
6669 #endif /* NO_ARG_ARRAY */
6670
6671 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6672 }
6673 else
6674 message1 (0);
6675
6676 /* Print should start at the beginning of the message
6677 buffer next time. */
6678 message_buf_print = 0;
6679 }
6680 }
6681 }
6682
6683
6684 /* The non-logging version of message. */
6685
6686 void
6687 message_nolog (m, a1, a2, a3)
6688 char *m;
6689 EMACS_INT a1, a2, a3;
6690 {
6691 Lisp_Object old_log_max;
6692 old_log_max = Vmessage_log_max;
6693 Vmessage_log_max = Qnil;
6694 message (m, a1, a2, a3);
6695 Vmessage_log_max = old_log_max;
6696 }
6697
6698
6699 /* Display the current message in the current mini-buffer. This is
6700 only called from error handlers in process.c, and is not time
6701 critical. */
6702
6703 void
6704 update_echo_area ()
6705 {
6706 if (!NILP (echo_area_buffer[0]))
6707 {
6708 Lisp_Object string;
6709 string = Fcurrent_message ();
6710 message3 (string, SBYTES (string),
6711 !NILP (current_buffer->enable_multibyte_characters));
6712 }
6713 }
6714
6715
6716 /* Make sure echo area buffers in `echo_buffers' are live.
6717 If they aren't, make new ones. */
6718
6719 static void
6720 ensure_echo_area_buffers ()
6721 {
6722 int i;
6723
6724 for (i = 0; i < 2; ++i)
6725 if (!BUFFERP (echo_buffer[i])
6726 || NILP (XBUFFER (echo_buffer[i])->name))
6727 {
6728 char name[30];
6729 Lisp_Object old_buffer;
6730 int j;
6731
6732 old_buffer = echo_buffer[i];
6733 sprintf (name, " *Echo Area %d*", i);
6734 echo_buffer[i] = Fget_buffer_create (build_string (name));
6735 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6736
6737 for (j = 0; j < 2; ++j)
6738 if (EQ (old_buffer, echo_area_buffer[j]))
6739 echo_area_buffer[j] = echo_buffer[i];
6740 }
6741 }
6742
6743
6744 /* Call FN with args A1..A4 with either the current or last displayed
6745 echo_area_buffer as current buffer.
6746
6747 WHICH zero means use the current message buffer
6748 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6749 from echo_buffer[] and clear it.
6750
6751 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6752 suitable buffer from echo_buffer[] and clear it.
6753
6754 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6755 that the current message becomes the last displayed one, make
6756 choose a suitable buffer for echo_area_buffer[0], and clear it.
6757
6758 Value is what FN returns. */
6759
6760 static int
6761 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6762 struct window *w;
6763 int which;
6764 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6765 EMACS_INT a1;
6766 Lisp_Object a2;
6767 EMACS_INT a3, a4;
6768 {
6769 Lisp_Object buffer;
6770 int this_one, the_other, clear_buffer_p, rc;
6771 int count = SPECPDL_INDEX ();
6772
6773 /* If buffers aren't live, make new ones. */
6774 ensure_echo_area_buffers ();
6775
6776 clear_buffer_p = 0;
6777
6778 if (which == 0)
6779 this_one = 0, the_other = 1;
6780 else if (which > 0)
6781 this_one = 1, the_other = 0;
6782 else
6783 {
6784 this_one = 0, the_other = 1;
6785 clear_buffer_p = 1;
6786
6787 /* We need a fresh one in case the current echo buffer equals
6788 the one containing the last displayed echo area message. */
6789 if (!NILP (echo_area_buffer[this_one])
6790 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6791 echo_area_buffer[this_one] = Qnil;
6792 }
6793
6794 /* Choose a suitable buffer from echo_buffer[] is we don't
6795 have one. */
6796 if (NILP (echo_area_buffer[this_one]))
6797 {
6798 echo_area_buffer[this_one]
6799 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6800 ? echo_buffer[the_other]
6801 : echo_buffer[this_one]);
6802 clear_buffer_p = 1;
6803 }
6804
6805 buffer = echo_area_buffer[this_one];
6806
6807 /* Don't get confused by reusing the buffer used for echoing
6808 for a different purpose. */
6809 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
6810 cancel_echoing ();
6811
6812 record_unwind_protect (unwind_with_echo_area_buffer,
6813 with_echo_area_buffer_unwind_data (w));
6814
6815 /* Make the echo area buffer current. Note that for display
6816 purposes, it is not necessary that the displayed window's buffer
6817 == current_buffer, except for text property lookup. So, let's
6818 only set that buffer temporarily here without doing a full
6819 Fset_window_buffer. We must also change w->pointm, though,
6820 because otherwise an assertions in unshow_buffer fails, and Emacs
6821 aborts. */
6822 set_buffer_internal_1 (XBUFFER (buffer));
6823 if (w)
6824 {
6825 w->buffer = buffer;
6826 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6827 }
6828
6829 current_buffer->undo_list = Qt;
6830 current_buffer->read_only = Qnil;
6831 specbind (Qinhibit_read_only, Qt);
6832 specbind (Qinhibit_modification_hooks, Qt);
6833
6834 if (clear_buffer_p && Z > BEG)
6835 del_range (BEG, Z);
6836
6837 xassert (BEGV >= BEG);
6838 xassert (ZV <= Z && ZV >= BEGV);
6839
6840 rc = fn (a1, a2, a3, a4);
6841
6842 xassert (BEGV >= BEG);
6843 xassert (ZV <= Z && ZV >= BEGV);
6844
6845 unbind_to (count, Qnil);
6846 return rc;
6847 }
6848
6849
6850 /* Save state that should be preserved around the call to the function
6851 FN called in with_echo_area_buffer. */
6852
6853 static Lisp_Object
6854 with_echo_area_buffer_unwind_data (w)
6855 struct window *w;
6856 {
6857 int i = 0;
6858 Lisp_Object vector;
6859
6860 /* Reduce consing by keeping one vector in
6861 Vwith_echo_area_save_vector. */
6862 vector = Vwith_echo_area_save_vector;
6863 Vwith_echo_area_save_vector = Qnil;
6864
6865 if (NILP (vector))
6866 vector = Fmake_vector (make_number (7), Qnil);
6867
6868 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6869 AREF (vector, i) = Vdeactivate_mark, ++i;
6870 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6871
6872 if (w)
6873 {
6874 XSETWINDOW (AREF (vector, i), w); ++i;
6875 AREF (vector, i) = w->buffer; ++i;
6876 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6877 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6878 }
6879 else
6880 {
6881 int end = i + 4;
6882 for (; i < end; ++i)
6883 AREF (vector, i) = Qnil;
6884 }
6885
6886 xassert (i == ASIZE (vector));
6887 return vector;
6888 }
6889
6890
6891 /* Restore global state from VECTOR which was created by
6892 with_echo_area_buffer_unwind_data. */
6893
6894 static Lisp_Object
6895 unwind_with_echo_area_buffer (vector)
6896 Lisp_Object vector;
6897 {
6898 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6899 Vdeactivate_mark = AREF (vector, 1);
6900 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6901
6902 if (WINDOWP (AREF (vector, 3)))
6903 {
6904 struct window *w;
6905 Lisp_Object buffer, charpos, bytepos;
6906
6907 w = XWINDOW (AREF (vector, 3));
6908 buffer = AREF (vector, 4);
6909 charpos = AREF (vector, 5);
6910 bytepos = AREF (vector, 6);
6911
6912 w->buffer = buffer;
6913 set_marker_both (w->pointm, buffer,
6914 XFASTINT (charpos), XFASTINT (bytepos));
6915 }
6916
6917 Vwith_echo_area_save_vector = vector;
6918 return Qnil;
6919 }
6920
6921
6922 /* Set up the echo area for use by print functions. MULTIBYTE_P
6923 non-zero means we will print multibyte. */
6924
6925 void
6926 setup_echo_area_for_printing (multibyte_p)
6927 int multibyte_p;
6928 {
6929 /* If we can't find an echo area any more, exit. */
6930 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
6931 Fkill_emacs (Qnil);
6932
6933 ensure_echo_area_buffers ();
6934
6935 if (!message_buf_print)
6936 {
6937 /* A message has been output since the last time we printed.
6938 Choose a fresh echo area buffer. */
6939 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6940 echo_area_buffer[0] = echo_buffer[1];
6941 else
6942 echo_area_buffer[0] = echo_buffer[0];
6943
6944 /* Switch to that buffer and clear it. */
6945 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6946 current_buffer->truncate_lines = Qnil;
6947
6948 if (Z > BEG)
6949 {
6950 int count = SPECPDL_INDEX ();
6951 specbind (Qinhibit_read_only, Qt);
6952 /* Note that undo recording is always disabled. */
6953 del_range (BEG, Z);
6954 unbind_to (count, Qnil);
6955 }
6956 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6957
6958 /* Set up the buffer for the multibyteness we need. */
6959 if (multibyte_p
6960 != !NILP (current_buffer->enable_multibyte_characters))
6961 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6962
6963 /* Raise the frame containing the echo area. */
6964 if (minibuffer_auto_raise)
6965 {
6966 struct frame *sf = SELECTED_FRAME ();
6967 Lisp_Object mini_window;
6968 mini_window = FRAME_MINIBUF_WINDOW (sf);
6969 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6970 }
6971
6972 message_log_maybe_newline ();
6973 message_buf_print = 1;
6974 }
6975 else
6976 {
6977 if (NILP (echo_area_buffer[0]))
6978 {
6979 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6980 echo_area_buffer[0] = echo_buffer[1];
6981 else
6982 echo_area_buffer[0] = echo_buffer[0];
6983 }
6984
6985 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6986 {
6987 /* Someone switched buffers between print requests. */
6988 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6989 current_buffer->truncate_lines = Qnil;
6990 }
6991 }
6992 }
6993
6994
6995 /* Display an echo area message in window W. Value is non-zero if W's
6996 height is changed. If display_last_displayed_message_p is
6997 non-zero, display the message that was last displayed, otherwise
6998 display the current message. */
6999
7000 static int
7001 display_echo_area (w)
7002 struct window *w;
7003 {
7004 int i, no_message_p, window_height_changed_p, count;
7005
7006 /* Temporarily disable garbage collections while displaying the echo
7007 area. This is done because a GC can print a message itself.
7008 That message would modify the echo area buffer's contents while a
7009 redisplay of the buffer is going on, and seriously confuse
7010 redisplay. */
7011 count = inhibit_garbage_collection ();
7012
7013 /* If there is no message, we must call display_echo_area_1
7014 nevertheless because it resizes the window. But we will have to
7015 reset the echo_area_buffer in question to nil at the end because
7016 with_echo_area_buffer will sets it to an empty buffer. */
7017 i = display_last_displayed_message_p ? 1 : 0;
7018 no_message_p = NILP (echo_area_buffer[i]);
7019
7020 window_height_changed_p
7021 = with_echo_area_buffer (w, display_last_displayed_message_p,
7022 display_echo_area_1,
7023 (EMACS_INT) w, Qnil, 0, 0);
7024
7025 if (no_message_p)
7026 echo_area_buffer[i] = Qnil;
7027
7028 unbind_to (count, Qnil);
7029 return window_height_changed_p;
7030 }
7031
7032
7033 /* Helper for display_echo_area. Display the current buffer which
7034 contains the current echo area message in window W, a mini-window,
7035 a pointer to which is passed in A1. A2..A4 are currently not used.
7036 Change the height of W so that all of the message is displayed.
7037 Value is non-zero if height of W was changed. */
7038
7039 static int
7040 display_echo_area_1 (a1, a2, a3, a4)
7041 EMACS_INT a1;
7042 Lisp_Object a2;
7043 EMACS_INT a3, a4;
7044 {
7045 struct window *w = (struct window *) a1;
7046 Lisp_Object window;
7047 struct text_pos start;
7048 int window_height_changed_p = 0;
7049
7050 /* Do this before displaying, so that we have a large enough glyph
7051 matrix for the display. */
7052 window_height_changed_p = resize_mini_window (w, 0);
7053
7054 /* Display. */
7055 clear_glyph_matrix (w->desired_matrix);
7056 XSETWINDOW (window, w);
7057 SET_TEXT_POS (start, BEG, BEG_BYTE);
7058 try_window (window, start);
7059
7060 return window_height_changed_p;
7061 }
7062
7063
7064 /* Resize the echo area window to exactly the size needed for the
7065 currently displayed message, if there is one. If a mini-buffer
7066 is active, don't shrink it. */
7067
7068 void
7069 resize_echo_area_exactly ()
7070 {
7071 if (BUFFERP (echo_area_buffer[0])
7072 && WINDOWP (echo_area_window))
7073 {
7074 struct window *w = XWINDOW (echo_area_window);
7075 int resized_p;
7076 Lisp_Object resize_exactly;
7077
7078 if (minibuf_level == 0)
7079 resize_exactly = Qt;
7080 else
7081 resize_exactly = Qnil;
7082
7083 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7084 (EMACS_INT) w, resize_exactly, 0, 0);
7085 if (resized_p)
7086 {
7087 ++windows_or_buffers_changed;
7088 ++update_mode_lines;
7089 redisplay_internal (0);
7090 }
7091 }
7092 }
7093
7094
7095 /* Callback function for with_echo_area_buffer, when used from
7096 resize_echo_area_exactly. A1 contains a pointer to the window to
7097 resize, EXACTLY non-nil means resize the mini-window exactly to the
7098 size of the text displayed. A3 and A4 are not used. Value is what
7099 resize_mini_window returns. */
7100
7101 static int
7102 resize_mini_window_1 (a1, exactly, a3, a4)
7103 EMACS_INT a1;
7104 Lisp_Object exactly;
7105 EMACS_INT a3, a4;
7106 {
7107 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7108 }
7109
7110
7111 /* Resize mini-window W to fit the size of its contents. EXACT:P
7112 means size the window exactly to the size needed. Otherwise, it's
7113 only enlarged until W's buffer is empty. Value is non-zero if
7114 the window height has been changed. */
7115
7116 int
7117 resize_mini_window (w, exact_p)
7118 struct window *w;
7119 int exact_p;
7120 {
7121 struct frame *f = XFRAME (w->frame);
7122 int window_height_changed_p = 0;
7123
7124 xassert (MINI_WINDOW_P (w));
7125
7126 /* Don't resize windows while redisplaying a window; it would
7127 confuse redisplay functions when the size of the window they are
7128 displaying changes from under them. Such a resizing can happen,
7129 for instance, when which-func prints a long message while
7130 we are running fontification-functions. We're running these
7131 functions with safe_call which binds inhibit-redisplay to t. */
7132 if (!NILP (Vinhibit_redisplay))
7133 return 0;
7134
7135 /* Nil means don't try to resize. */
7136 if (NILP (Vresize_mini_windows)
7137 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7138 return 0;
7139
7140 if (!FRAME_MINIBUF_ONLY_P (f))
7141 {
7142 struct it it;
7143 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7144 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7145 int height, max_height;
7146 int unit = FRAME_LINE_HEIGHT (f);
7147 struct text_pos start;
7148 struct buffer *old_current_buffer = NULL;
7149
7150 if (current_buffer != XBUFFER (w->buffer))
7151 {
7152 old_current_buffer = current_buffer;
7153 set_buffer_internal (XBUFFER (w->buffer));
7154 }
7155
7156 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7157
7158 /* Compute the max. number of lines specified by the user. */
7159 if (FLOATP (Vmax_mini_window_height))
7160 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7161 else if (INTEGERP (Vmax_mini_window_height))
7162 max_height = XINT (Vmax_mini_window_height);
7163 else
7164 max_height = total_height / 4;
7165
7166 /* Correct that max. height if it's bogus. */
7167 max_height = max (1, max_height);
7168 max_height = min (total_height, max_height);
7169
7170 /* Find out the height of the text in the window. */
7171 if (it.truncate_lines_p)
7172 height = 1;
7173 else
7174 {
7175 last_height = 0;
7176 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7177 if (it.max_ascent == 0 && it.max_descent == 0)
7178 height = it.current_y + last_height;
7179 else
7180 height = it.current_y + it.max_ascent + it.max_descent;
7181 height -= it.extra_line_spacing;
7182 height = (height + unit - 1) / unit;
7183 }
7184
7185 /* Compute a suitable window start. */
7186 if (height > max_height)
7187 {
7188 height = max_height;
7189 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7190 move_it_vertically_backward (&it, (height - 1) * unit);
7191 start = it.current.pos;
7192 }
7193 else
7194 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7195 SET_MARKER_FROM_TEXT_POS (w->start, start);
7196
7197 if (EQ (Vresize_mini_windows, Qgrow_only))
7198 {
7199 /* Let it grow only, until we display an empty message, in which
7200 case the window shrinks again. */
7201 if (height > WINDOW_TOTAL_LINES (w))
7202 {
7203 int old_height = WINDOW_TOTAL_LINES (w);
7204 freeze_window_starts (f, 1);
7205 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7206 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7207 }
7208 else if (height < WINDOW_TOTAL_LINES (w)
7209 && (exact_p || BEGV == ZV))
7210 {
7211 int old_height = WINDOW_TOTAL_LINES (w);
7212 freeze_window_starts (f, 0);
7213 shrink_mini_window (w);
7214 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7215 }
7216 }
7217 else
7218 {
7219 /* Always resize to exact size needed. */
7220 if (height > WINDOW_TOTAL_LINES (w))
7221 {
7222 int old_height = WINDOW_TOTAL_LINES (w);
7223 freeze_window_starts (f, 1);
7224 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7225 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7226 }
7227 else if (height < WINDOW_TOTAL_LINES (w))
7228 {
7229 int old_height = WINDOW_TOTAL_LINES (w);
7230 freeze_window_starts (f, 0);
7231 shrink_mini_window (w);
7232
7233 if (height)
7234 {
7235 freeze_window_starts (f, 1);
7236 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7237 }
7238
7239 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7240 }
7241 }
7242
7243 if (old_current_buffer)
7244 set_buffer_internal (old_current_buffer);
7245 }
7246
7247 return window_height_changed_p;
7248 }
7249
7250
7251 /* Value is the current message, a string, or nil if there is no
7252 current message. */
7253
7254 Lisp_Object
7255 current_message ()
7256 {
7257 Lisp_Object msg;
7258
7259 if (NILP (echo_area_buffer[0]))
7260 msg = Qnil;
7261 else
7262 {
7263 with_echo_area_buffer (0, 0, current_message_1,
7264 (EMACS_INT) &msg, Qnil, 0, 0);
7265 if (NILP (msg))
7266 echo_area_buffer[0] = Qnil;
7267 }
7268
7269 return msg;
7270 }
7271
7272
7273 static int
7274 current_message_1 (a1, a2, a3, a4)
7275 EMACS_INT a1;
7276 Lisp_Object a2;
7277 EMACS_INT a3, a4;
7278 {
7279 Lisp_Object *msg = (Lisp_Object *) a1;
7280
7281 if (Z > BEG)
7282 *msg = make_buffer_string (BEG, Z, 1);
7283 else
7284 *msg = Qnil;
7285 return 0;
7286 }
7287
7288
7289 /* Push the current message on Vmessage_stack for later restauration
7290 by restore_message. Value is non-zero if the current message isn't
7291 empty. This is a relatively infrequent operation, so it's not
7292 worth optimizing. */
7293
7294 int
7295 push_message ()
7296 {
7297 Lisp_Object msg;
7298 msg = current_message ();
7299 Vmessage_stack = Fcons (msg, Vmessage_stack);
7300 return STRINGP (msg);
7301 }
7302
7303
7304 /* Restore message display from the top of Vmessage_stack. */
7305
7306 void
7307 restore_message ()
7308 {
7309 Lisp_Object msg;
7310
7311 xassert (CONSP (Vmessage_stack));
7312 msg = XCAR (Vmessage_stack);
7313 if (STRINGP (msg))
7314 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7315 else
7316 message3_nolog (msg, 0, 0);
7317 }
7318
7319
7320 /* Handler for record_unwind_protect calling pop_message. */
7321
7322 Lisp_Object
7323 pop_message_unwind (dummy)
7324 Lisp_Object dummy;
7325 {
7326 pop_message ();
7327 return Qnil;
7328 }
7329
7330 /* Pop the top-most entry off Vmessage_stack. */
7331
7332 void
7333 pop_message ()
7334 {
7335 xassert (CONSP (Vmessage_stack));
7336 Vmessage_stack = XCDR (Vmessage_stack);
7337 }
7338
7339
7340 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7341 exits. If the stack is not empty, we have a missing pop_message
7342 somewhere. */
7343
7344 void
7345 check_message_stack ()
7346 {
7347 if (!NILP (Vmessage_stack))
7348 abort ();
7349 }
7350
7351
7352 /* Truncate to NCHARS what will be displayed in the echo area the next
7353 time we display it---but don't redisplay it now. */
7354
7355 void
7356 truncate_echo_area (nchars)
7357 int nchars;
7358 {
7359 if (nchars == 0)
7360 echo_area_buffer[0] = Qnil;
7361 /* A null message buffer means that the frame hasn't really been
7362 initialized yet. Error messages get reported properly by
7363 cmd_error, so this must be just an informative message; toss it. */
7364 else if (!noninteractive
7365 && INTERACTIVE
7366 && !NILP (echo_area_buffer[0]))
7367 {
7368 struct frame *sf = SELECTED_FRAME ();
7369 if (FRAME_MESSAGE_BUF (sf))
7370 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7371 }
7372 }
7373
7374
7375 /* Helper function for truncate_echo_area. Truncate the current
7376 message to at most NCHARS characters. */
7377
7378 static int
7379 truncate_message_1 (nchars, a2, a3, a4)
7380 EMACS_INT nchars;
7381 Lisp_Object a2;
7382 EMACS_INT a3, a4;
7383 {
7384 if (BEG + nchars < Z)
7385 del_range (BEG + nchars, Z);
7386 if (Z == BEG)
7387 echo_area_buffer[0] = Qnil;
7388 return 0;
7389 }
7390
7391
7392 /* Set the current message to a substring of S or STRING.
7393
7394 If STRING is a Lisp string, set the message to the first NBYTES
7395 bytes from STRING. NBYTES zero means use the whole string. If
7396 STRING is multibyte, the message will be displayed multibyte.
7397
7398 If S is not null, set the message to the first LEN bytes of S. LEN
7399 zero means use the whole string. MULTIBYTE_P non-zero means S is
7400 multibyte. Display the message multibyte in that case. */
7401
7402 void
7403 set_message (s, string, nbytes, multibyte_p)
7404 const char *s;
7405 Lisp_Object string;
7406 int nbytes, multibyte_p;
7407 {
7408 message_enable_multibyte
7409 = ((s && multibyte_p)
7410 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7411
7412 with_echo_area_buffer (0, -1, set_message_1,
7413 (EMACS_INT) s, string, nbytes, multibyte_p);
7414 message_buf_print = 0;
7415 help_echo_showing_p = 0;
7416 }
7417
7418
7419 /* Helper function for set_message. Arguments have the same meaning
7420 as there, with A1 corresponding to S and A2 corresponding to STRING
7421 This function is called with the echo area buffer being
7422 current. */
7423
7424 static int
7425 set_message_1 (a1, a2, nbytes, multibyte_p)
7426 EMACS_INT a1;
7427 Lisp_Object a2;
7428 EMACS_INT nbytes, multibyte_p;
7429 {
7430 const char *s = (const char *) a1;
7431 Lisp_Object string = a2;
7432
7433 xassert (BEG == Z);
7434
7435 /* Change multibyteness of the echo buffer appropriately. */
7436 if (message_enable_multibyte
7437 != !NILP (current_buffer->enable_multibyte_characters))
7438 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7439
7440 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7441
7442 /* Insert new message at BEG. */
7443 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7444
7445 if (STRINGP (string))
7446 {
7447 int nchars;
7448
7449 if (nbytes == 0)
7450 nbytes = SBYTES (string);
7451 nchars = string_byte_to_char (string, nbytes);
7452
7453 /* This function takes care of single/multibyte conversion. We
7454 just have to ensure that the echo area buffer has the right
7455 setting of enable_multibyte_characters. */
7456 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7457 }
7458 else if (s)
7459 {
7460 if (nbytes == 0)
7461 nbytes = strlen (s);
7462
7463 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7464 {
7465 /* Convert from multi-byte to single-byte. */
7466 int i, c, n;
7467 unsigned char work[1];
7468
7469 /* Convert a multibyte string to single-byte. */
7470 for (i = 0; i < nbytes; i += n)
7471 {
7472 c = string_char_and_length (s + i, nbytes - i, &n);
7473 work[0] = (SINGLE_BYTE_CHAR_P (c)
7474 ? c
7475 : multibyte_char_to_unibyte (c, Qnil));
7476 insert_1_both (work, 1, 1, 1, 0, 0);
7477 }
7478 }
7479 else if (!multibyte_p
7480 && !NILP (current_buffer->enable_multibyte_characters))
7481 {
7482 /* Convert from single-byte to multi-byte. */
7483 int i, c, n;
7484 const unsigned char *msg = (const unsigned char *) s;
7485 unsigned char str[MAX_MULTIBYTE_LENGTH];
7486
7487 /* Convert a single-byte string to multibyte. */
7488 for (i = 0; i < nbytes; i++)
7489 {
7490 c = unibyte_char_to_multibyte (msg[i]);
7491 n = CHAR_STRING (c, str);
7492 insert_1_both (str, 1, n, 1, 0, 0);
7493 }
7494 }
7495 else
7496 insert_1 (s, nbytes, 1, 0, 0);
7497 }
7498
7499 return 0;
7500 }
7501
7502
7503 /* Clear messages. CURRENT_P non-zero means clear the current
7504 message. LAST_DISPLAYED_P non-zero means clear the message
7505 last displayed. */
7506
7507 void
7508 clear_message (current_p, last_displayed_p)
7509 int current_p, last_displayed_p;
7510 {
7511 if (current_p)
7512 {
7513 echo_area_buffer[0] = Qnil;
7514 message_cleared_p = 1;
7515 }
7516
7517 if (last_displayed_p)
7518 echo_area_buffer[1] = Qnil;
7519
7520 message_buf_print = 0;
7521 }
7522
7523 /* Clear garbaged frames.
7524
7525 This function is used where the old redisplay called
7526 redraw_garbaged_frames which in turn called redraw_frame which in
7527 turn called clear_frame. The call to clear_frame was a source of
7528 flickering. I believe a clear_frame is not necessary. It should
7529 suffice in the new redisplay to invalidate all current matrices,
7530 and ensure a complete redisplay of all windows. */
7531
7532 static void
7533 clear_garbaged_frames ()
7534 {
7535 if (frame_garbaged)
7536 {
7537 Lisp_Object tail, frame;
7538 int changed_count = 0;
7539
7540 FOR_EACH_FRAME (tail, frame)
7541 {
7542 struct frame *f = XFRAME (frame);
7543
7544 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7545 {
7546 if (f->resized_p)
7547 Fredraw_frame (frame);
7548 clear_current_matrices (f);
7549 changed_count++;
7550 f->garbaged = 0;
7551 f->resized_p = 0;
7552 }
7553 }
7554
7555 frame_garbaged = 0;
7556 if (changed_count)
7557 ++windows_or_buffers_changed;
7558 }
7559 }
7560
7561
7562 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7563 is non-zero update selected_frame. Value is non-zero if the
7564 mini-windows height has been changed. */
7565
7566 static int
7567 echo_area_display (update_frame_p)
7568 int update_frame_p;
7569 {
7570 Lisp_Object mini_window;
7571 struct window *w;
7572 struct frame *f;
7573 int window_height_changed_p = 0;
7574 struct frame *sf = SELECTED_FRAME ();
7575
7576 mini_window = FRAME_MINIBUF_WINDOW (sf);
7577 w = XWINDOW (mini_window);
7578 f = XFRAME (WINDOW_FRAME (w));
7579
7580 /* Don't display if frame is invisible or not yet initialized. */
7581 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7582 return 0;
7583
7584 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7585 #ifndef MAC_OS8
7586 #ifdef HAVE_WINDOW_SYSTEM
7587 /* When Emacs starts, selected_frame may be a visible terminal
7588 frame, even if we run under a window system. If we let this
7589 through, a message would be displayed on the terminal. */
7590 if (EQ (selected_frame, Vterminal_frame)
7591 && !NILP (Vwindow_system))
7592 return 0;
7593 #endif /* HAVE_WINDOW_SYSTEM */
7594 #endif
7595
7596 /* Redraw garbaged frames. */
7597 if (frame_garbaged)
7598 clear_garbaged_frames ();
7599
7600 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7601 {
7602 echo_area_window = mini_window;
7603 window_height_changed_p = display_echo_area (w);
7604 w->must_be_updated_p = 1;
7605
7606 /* Update the display, unless called from redisplay_internal.
7607 Also don't update the screen during redisplay itself. The
7608 update will happen at the end of redisplay, and an update
7609 here could cause confusion. */
7610 if (update_frame_p && !redisplaying_p)
7611 {
7612 int n = 0;
7613
7614 /* If the display update has been interrupted by pending
7615 input, update mode lines in the frame. Due to the
7616 pending input, it might have been that redisplay hasn't
7617 been called, so that mode lines above the echo area are
7618 garbaged. This looks odd, so we prevent it here. */
7619 if (!display_completed)
7620 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7621
7622 if (window_height_changed_p
7623 /* Don't do this if Emacs is shutting down. Redisplay
7624 needs to run hooks. */
7625 && !NILP (Vrun_hooks))
7626 {
7627 /* Must update other windows. Likewise as in other
7628 cases, don't let this update be interrupted by
7629 pending input. */
7630 int count = SPECPDL_INDEX ();
7631 specbind (Qredisplay_dont_pause, Qt);
7632 windows_or_buffers_changed = 1;
7633 redisplay_internal (0);
7634 unbind_to (count, Qnil);
7635 }
7636 else if (FRAME_WINDOW_P (f) && n == 0)
7637 {
7638 /* Window configuration is the same as before.
7639 Can do with a display update of the echo area,
7640 unless we displayed some mode lines. */
7641 update_single_window (w, 1);
7642 rif->flush_display (f);
7643 }
7644 else
7645 update_frame (f, 1, 1);
7646
7647 /* If cursor is in the echo area, make sure that the next
7648 redisplay displays the minibuffer, so that the cursor will
7649 be replaced with what the minibuffer wants. */
7650 if (cursor_in_echo_area)
7651 ++windows_or_buffers_changed;
7652 }
7653 }
7654 else if (!EQ (mini_window, selected_window))
7655 windows_or_buffers_changed++;
7656
7657 /* Last displayed message is now the current message. */
7658 echo_area_buffer[1] = echo_area_buffer[0];
7659
7660 /* Prevent redisplay optimization in redisplay_internal by resetting
7661 this_line_start_pos. This is done because the mini-buffer now
7662 displays the message instead of its buffer text. */
7663 if (EQ (mini_window, selected_window))
7664 CHARPOS (this_line_start_pos) = 0;
7665
7666 return window_height_changed_p;
7667 }
7668
7669
7670 \f
7671 /***********************************************************************
7672 Frame Titles
7673 ***********************************************************************/
7674
7675
7676 /* The frame title buffering code is also used by Fformat_mode_line.
7677 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
7678
7679 /* A buffer for constructing frame titles in it; allocated from the
7680 heap in init_xdisp and resized as needed in store_frame_title_char. */
7681
7682 static char *frame_title_buf;
7683
7684 /* The buffer's end, and a current output position in it. */
7685
7686 static char *frame_title_buf_end;
7687 static char *frame_title_ptr;
7688
7689
7690 /* Store a single character C for the frame title in frame_title_buf.
7691 Re-allocate frame_title_buf if necessary. */
7692
7693 static void
7694 #ifdef PROTOTYPES
7695 store_frame_title_char (char c)
7696 #else
7697 store_frame_title_char (c)
7698 char c;
7699 #endif
7700 {
7701 /* If output position has reached the end of the allocated buffer,
7702 double the buffer's size. */
7703 if (frame_title_ptr == frame_title_buf_end)
7704 {
7705 int len = frame_title_ptr - frame_title_buf;
7706 int new_size = 2 * len * sizeof *frame_title_buf;
7707 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7708 frame_title_buf_end = frame_title_buf + new_size;
7709 frame_title_ptr = frame_title_buf + len;
7710 }
7711
7712 *frame_title_ptr++ = c;
7713 }
7714
7715
7716 /* Store part of a frame title in frame_title_buf, beginning at
7717 frame_title_ptr. STR is the string to store. Do not copy
7718 characters that yield more columns than PRECISION; PRECISION <= 0
7719 means copy the whole string. Pad with spaces until FIELD_WIDTH
7720 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7721 pad. Called from display_mode_element when it is used to build a
7722 frame title. */
7723
7724 static int
7725 store_frame_title (str, field_width, precision)
7726 const unsigned char *str;
7727 int field_width, precision;
7728 {
7729 int n = 0;
7730 int dummy, nbytes;
7731
7732 /* Copy at most PRECISION chars from STR. */
7733 nbytes = strlen (str);
7734 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7735 while (nbytes--)
7736 store_frame_title_char (*str++);
7737
7738 /* Fill up with spaces until FIELD_WIDTH reached. */
7739 while (field_width > 0
7740 && n < field_width)
7741 {
7742 store_frame_title_char (' ');
7743 ++n;
7744 }
7745
7746 return n;
7747 }
7748
7749 #ifdef HAVE_WINDOW_SYSTEM
7750
7751 /* Set the title of FRAME, if it has changed. The title format is
7752 Vicon_title_format if FRAME is iconified, otherwise it is
7753 frame_title_format. */
7754
7755 static void
7756 x_consider_frame_title (frame)
7757 Lisp_Object frame;
7758 {
7759 struct frame *f = XFRAME (frame);
7760
7761 if (FRAME_WINDOW_P (f)
7762 || FRAME_MINIBUF_ONLY_P (f)
7763 || f->explicit_name)
7764 {
7765 /* Do we have more than one visible frame on this X display? */
7766 Lisp_Object tail;
7767 Lisp_Object fmt;
7768 struct buffer *obuf;
7769 int len;
7770 struct it it;
7771
7772 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7773 {
7774 Lisp_Object other_frame = XCAR (tail);
7775 struct frame *tf = XFRAME (other_frame);
7776
7777 if (tf != f
7778 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7779 && !FRAME_MINIBUF_ONLY_P (tf)
7780 && !EQ (other_frame, tip_frame)
7781 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7782 break;
7783 }
7784
7785 /* Set global variable indicating that multiple frames exist. */
7786 multiple_frames = CONSP (tail);
7787
7788 /* Switch to the buffer of selected window of the frame. Set up
7789 frame_title_ptr so that display_mode_element will output into it;
7790 then display the title. */
7791 obuf = current_buffer;
7792 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
7793 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7794 frame_title_ptr = frame_title_buf;
7795 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7796 NULL, DEFAULT_FACE_ID);
7797 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
7798 len = frame_title_ptr - frame_title_buf;
7799 frame_title_ptr = NULL;
7800 set_buffer_internal_1 (obuf);
7801
7802 /* Set the title only if it's changed. This avoids consing in
7803 the common case where it hasn't. (If it turns out that we've
7804 already wasted too much time by walking through the list with
7805 display_mode_element, then we might need to optimize at a
7806 higher level than this.) */
7807 if (! STRINGP (f->name)
7808 || SBYTES (f->name) != len
7809 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
7810 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7811 }
7812 }
7813
7814 #endif /* not HAVE_WINDOW_SYSTEM */
7815
7816
7817
7818 \f
7819 /***********************************************************************
7820 Menu Bars
7821 ***********************************************************************/
7822
7823
7824 /* Prepare for redisplay by updating menu-bar item lists when
7825 appropriate. This can call eval. */
7826
7827 void
7828 prepare_menu_bars ()
7829 {
7830 int all_windows;
7831 struct gcpro gcpro1, gcpro2;
7832 struct frame *f;
7833 Lisp_Object tooltip_frame;
7834
7835 #ifdef HAVE_WINDOW_SYSTEM
7836 tooltip_frame = tip_frame;
7837 #else
7838 tooltip_frame = Qnil;
7839 #endif
7840
7841 /* Update all frame titles based on their buffer names, etc. We do
7842 this before the menu bars so that the buffer-menu will show the
7843 up-to-date frame titles. */
7844 #ifdef HAVE_WINDOW_SYSTEM
7845 if (windows_or_buffers_changed || update_mode_lines)
7846 {
7847 Lisp_Object tail, frame;
7848
7849 FOR_EACH_FRAME (tail, frame)
7850 {
7851 f = XFRAME (frame);
7852 if (!EQ (frame, tooltip_frame)
7853 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7854 x_consider_frame_title (frame);
7855 }
7856 }
7857 #endif /* HAVE_WINDOW_SYSTEM */
7858
7859 /* Update the menu bar item lists, if appropriate. This has to be
7860 done before any actual redisplay or generation of display lines. */
7861 all_windows = (update_mode_lines
7862 || buffer_shared > 1
7863 || windows_or_buffers_changed);
7864 if (all_windows)
7865 {
7866 Lisp_Object tail, frame;
7867 int count = SPECPDL_INDEX ();
7868
7869 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7870
7871 FOR_EACH_FRAME (tail, frame)
7872 {
7873 f = XFRAME (frame);
7874
7875 /* Ignore tooltip frame. */
7876 if (EQ (frame, tooltip_frame))
7877 continue;
7878
7879 /* If a window on this frame changed size, report that to
7880 the user and clear the size-change flag. */
7881 if (FRAME_WINDOW_SIZES_CHANGED (f))
7882 {
7883 Lisp_Object functions;
7884
7885 /* Clear flag first in case we get an error below. */
7886 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7887 functions = Vwindow_size_change_functions;
7888 GCPRO2 (tail, functions);
7889
7890 while (CONSP (functions))
7891 {
7892 call1 (XCAR (functions), frame);
7893 functions = XCDR (functions);
7894 }
7895 UNGCPRO;
7896 }
7897
7898 GCPRO1 (tail);
7899 update_menu_bar (f, 0);
7900 #ifdef HAVE_WINDOW_SYSTEM
7901 update_tool_bar (f, 0);
7902 #endif
7903 UNGCPRO;
7904 }
7905
7906 unbind_to (count, Qnil);
7907 }
7908 else
7909 {
7910 struct frame *sf = SELECTED_FRAME ();
7911 update_menu_bar (sf, 1);
7912 #ifdef HAVE_WINDOW_SYSTEM
7913 update_tool_bar (sf, 1);
7914 #endif
7915 }
7916
7917 /* Motif needs this. See comment in xmenu.c. Turn it off when
7918 pending_menu_activation is not defined. */
7919 #ifdef USE_X_TOOLKIT
7920 pending_menu_activation = 0;
7921 #endif
7922 }
7923
7924
7925 /* Update the menu bar item list for frame F. This has to be done
7926 before we start to fill in any display lines, because it can call
7927 eval.
7928
7929 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7930
7931 static void
7932 update_menu_bar (f, save_match_data)
7933 struct frame *f;
7934 int save_match_data;
7935 {
7936 Lisp_Object window;
7937 register struct window *w;
7938
7939 /* If called recursively during a menu update, do nothing. This can
7940 happen when, for instance, an activate-menubar-hook causes a
7941 redisplay. */
7942 if (inhibit_menubar_update)
7943 return;
7944
7945 window = FRAME_SELECTED_WINDOW (f);
7946 w = XWINDOW (window);
7947
7948 #if 0 /* The if statement below this if statement used to include the
7949 condition !NILP (w->update_mode_line), rather than using
7950 update_mode_lines directly, and this if statement may have
7951 been added to make that condition work. Now the if
7952 statement below matches its comment, this isn't needed. */
7953 if (update_mode_lines)
7954 w->update_mode_line = Qt;
7955 #endif
7956
7957 if (FRAME_WINDOW_P (f)
7958 ?
7959 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
7960 || defined (USE_GTK)
7961 FRAME_EXTERNAL_MENU_BAR (f)
7962 #else
7963 FRAME_MENU_BAR_LINES (f) > 0
7964 #endif
7965 : FRAME_MENU_BAR_LINES (f) > 0)
7966 {
7967 /* If the user has switched buffers or windows, we need to
7968 recompute to reflect the new bindings. But we'll
7969 recompute when update_mode_lines is set too; that means
7970 that people can use force-mode-line-update to request
7971 that the menu bar be recomputed. The adverse effect on
7972 the rest of the redisplay algorithm is about the same as
7973 windows_or_buffers_changed anyway. */
7974 if (windows_or_buffers_changed
7975 /* This used to test w->update_mode_line, but we believe
7976 there is no need to recompute the menu in that case. */
7977 || update_mode_lines
7978 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7979 < BUF_MODIFF (XBUFFER (w->buffer)))
7980 != !NILP (w->last_had_star))
7981 || ((!NILP (Vtransient_mark_mode)
7982 && !NILP (XBUFFER (w->buffer)->mark_active))
7983 != !NILP (w->region_showing)))
7984 {
7985 struct buffer *prev = current_buffer;
7986 int count = SPECPDL_INDEX ();
7987
7988 specbind (Qinhibit_menubar_update, Qt);
7989
7990 set_buffer_internal_1 (XBUFFER (w->buffer));
7991 if (save_match_data)
7992 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7993 if (NILP (Voverriding_local_map_menu_flag))
7994 {
7995 specbind (Qoverriding_terminal_local_map, Qnil);
7996 specbind (Qoverriding_local_map, Qnil);
7997 }
7998
7999 /* Run the Lucid hook. */
8000 safe_run_hooks (Qactivate_menubar_hook);
8001
8002 /* If it has changed current-menubar from previous value,
8003 really recompute the menu-bar from the value. */
8004 if (! NILP (Vlucid_menu_bar_dirty_flag))
8005 call0 (Qrecompute_lucid_menubar);
8006
8007 safe_run_hooks (Qmenu_bar_update_hook);
8008 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8009
8010 /* Redisplay the menu bar in case we changed it. */
8011 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8012 || defined (USE_GTK)
8013 if (FRAME_WINDOW_P (f)
8014 #if defined (MAC_OS)
8015 /* All frames on Mac OS share the same menubar. So only the
8016 selected frame should be allowed to set it. */
8017 && f == SELECTED_FRAME ()
8018 #endif
8019 )
8020 set_frame_menubar (f, 0, 0);
8021 else
8022 /* On a terminal screen, the menu bar is an ordinary screen
8023 line, and this makes it get updated. */
8024 w->update_mode_line = Qt;
8025 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8026 /* In the non-toolkit version, the menu bar is an ordinary screen
8027 line, and this makes it get updated. */
8028 w->update_mode_line = Qt;
8029 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8030
8031 unbind_to (count, Qnil);
8032 set_buffer_internal_1 (prev);
8033 }
8034 }
8035 }
8036
8037
8038 \f
8039 /***********************************************************************
8040 Output Cursor
8041 ***********************************************************************/
8042
8043 #ifdef HAVE_WINDOW_SYSTEM
8044
8045 /* EXPORT:
8046 Nominal cursor position -- where to draw output.
8047 HPOS and VPOS are window relative glyph matrix coordinates.
8048 X and Y are window relative pixel coordinates. */
8049
8050 struct cursor_pos output_cursor;
8051
8052
8053 /* EXPORT:
8054 Set the global variable output_cursor to CURSOR. All cursor
8055 positions are relative to updated_window. */
8056
8057 void
8058 set_output_cursor (cursor)
8059 struct cursor_pos *cursor;
8060 {
8061 output_cursor.hpos = cursor->hpos;
8062 output_cursor.vpos = cursor->vpos;
8063 output_cursor.x = cursor->x;
8064 output_cursor.y = cursor->y;
8065 }
8066
8067
8068 /* EXPORT for RIF:
8069 Set a nominal cursor position.
8070
8071 HPOS and VPOS are column/row positions in a window glyph matrix. X
8072 and Y are window text area relative pixel positions.
8073
8074 If this is done during an update, updated_window will contain the
8075 window that is being updated and the position is the future output
8076 cursor position for that window. If updated_window is null, use
8077 selected_window and display the cursor at the given position. */
8078
8079 void
8080 x_cursor_to (vpos, hpos, y, x)
8081 int vpos, hpos, y, x;
8082 {
8083 struct window *w;
8084
8085 /* If updated_window is not set, work on selected_window. */
8086 if (updated_window)
8087 w = updated_window;
8088 else
8089 w = XWINDOW (selected_window);
8090
8091 /* Set the output cursor. */
8092 output_cursor.hpos = hpos;
8093 output_cursor.vpos = vpos;
8094 output_cursor.x = x;
8095 output_cursor.y = y;
8096
8097 /* If not called as part of an update, really display the cursor.
8098 This will also set the cursor position of W. */
8099 if (updated_window == NULL)
8100 {
8101 BLOCK_INPUT;
8102 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8103 if (rif->flush_display_optional)
8104 rif->flush_display_optional (SELECTED_FRAME ());
8105 UNBLOCK_INPUT;
8106 }
8107 }
8108
8109 #endif /* HAVE_WINDOW_SYSTEM */
8110
8111 \f
8112 /***********************************************************************
8113 Tool-bars
8114 ***********************************************************************/
8115
8116 #ifdef HAVE_WINDOW_SYSTEM
8117
8118 /* Where the mouse was last time we reported a mouse event. */
8119
8120 FRAME_PTR last_mouse_frame;
8121
8122 /* Tool-bar item index of the item on which a mouse button was pressed
8123 or -1. */
8124
8125 int last_tool_bar_item;
8126
8127
8128 /* Update the tool-bar item list for frame F. This has to be done
8129 before we start to fill in any display lines. Called from
8130 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8131 and restore it here. */
8132
8133 static void
8134 update_tool_bar (f, save_match_data)
8135 struct frame *f;
8136 int save_match_data;
8137 {
8138 #ifdef USE_GTK
8139 int do_update = FRAME_EXTERNAL_TOOL_BAR(f);
8140 #else
8141 int do_update = WINDOWP (f->tool_bar_window)
8142 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8143 #endif
8144
8145 if (do_update)
8146 {
8147 Lisp_Object window;
8148 struct window *w;
8149
8150 window = FRAME_SELECTED_WINDOW (f);
8151 w = XWINDOW (window);
8152
8153 /* If the user has switched buffers or windows, we need to
8154 recompute to reflect the new bindings. But we'll
8155 recompute when update_mode_lines is set too; that means
8156 that people can use force-mode-line-update to request
8157 that the menu bar be recomputed. The adverse effect on
8158 the rest of the redisplay algorithm is about the same as
8159 windows_or_buffers_changed anyway. */
8160 if (windows_or_buffers_changed
8161 || !NILP (w->update_mode_line)
8162 || update_mode_lines
8163 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8164 < BUF_MODIFF (XBUFFER (w->buffer)))
8165 != !NILP (w->last_had_star))
8166 || ((!NILP (Vtransient_mark_mode)
8167 && !NILP (XBUFFER (w->buffer)->mark_active))
8168 != !NILP (w->region_showing)))
8169 {
8170 struct buffer *prev = current_buffer;
8171 int count = SPECPDL_INDEX ();
8172 Lisp_Object old_tool_bar;
8173 struct gcpro gcpro1;
8174
8175 /* Set current_buffer to the buffer of the selected
8176 window of the frame, so that we get the right local
8177 keymaps. */
8178 set_buffer_internal_1 (XBUFFER (w->buffer));
8179
8180 /* Save match data, if we must. */
8181 if (save_match_data)
8182 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8183
8184 /* Make sure that we don't accidentally use bogus keymaps. */
8185 if (NILP (Voverriding_local_map_menu_flag))
8186 {
8187 specbind (Qoverriding_terminal_local_map, Qnil);
8188 specbind (Qoverriding_local_map, Qnil);
8189 }
8190
8191 old_tool_bar = f->tool_bar_items;
8192 GCPRO1 (old_tool_bar);
8193
8194 /* Build desired tool-bar items from keymaps. */
8195 BLOCK_INPUT;
8196 f->tool_bar_items
8197 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
8198 UNBLOCK_INPUT;
8199
8200 /* Redisplay the tool-bar if we changed it. */
8201 if (! NILP (Fequal (old_tool_bar, f->tool_bar_items)))
8202 w->update_mode_line = Qt;
8203
8204 UNGCPRO;
8205
8206 unbind_to (count, Qnil);
8207 set_buffer_internal_1 (prev);
8208 }
8209 }
8210 }
8211
8212
8213 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8214 F's desired tool-bar contents. F->tool_bar_items must have
8215 been set up previously by calling prepare_menu_bars. */
8216
8217 static void
8218 build_desired_tool_bar_string (f)
8219 struct frame *f;
8220 {
8221 int i, size, size_needed;
8222 struct gcpro gcpro1, gcpro2, gcpro3;
8223 Lisp_Object image, plist, props;
8224
8225 image = plist = props = Qnil;
8226 GCPRO3 (image, plist, props);
8227
8228 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8229 Otherwise, make a new string. */
8230
8231 /* The size of the string we might be able to reuse. */
8232 size = (STRINGP (f->desired_tool_bar_string)
8233 ? SCHARS (f->desired_tool_bar_string)
8234 : 0);
8235
8236 /* We need one space in the string for each image. */
8237 size_needed = f->n_tool_bar_items;
8238
8239 /* Reuse f->desired_tool_bar_string, if possible. */
8240 if (size < size_needed || NILP (f->desired_tool_bar_string))
8241 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8242 make_number (' '));
8243 else
8244 {
8245 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8246 Fremove_text_properties (make_number (0), make_number (size),
8247 props, f->desired_tool_bar_string);
8248 }
8249
8250 /* Put a `display' property on the string for the images to display,
8251 put a `menu_item' property on tool-bar items with a value that
8252 is the index of the item in F's tool-bar item vector. */
8253 for (i = 0; i < f->n_tool_bar_items; ++i)
8254 {
8255 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8256
8257 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8258 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8259 int hmargin, vmargin, relief, idx, end;
8260 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
8261
8262 /* If image is a vector, choose the image according to the
8263 button state. */
8264 image = PROP (TOOL_BAR_ITEM_IMAGES);
8265 if (VECTORP (image))
8266 {
8267 if (enabled_p)
8268 idx = (selected_p
8269 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8270 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8271 else
8272 idx = (selected_p
8273 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8274 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8275
8276 xassert (ASIZE (image) >= idx);
8277 image = AREF (image, idx);
8278 }
8279 else
8280 idx = -1;
8281
8282 /* Ignore invalid image specifications. */
8283 if (!valid_image_p (image))
8284 continue;
8285
8286 /* Display the tool-bar button pressed, or depressed. */
8287 plist = Fcopy_sequence (XCDR (image));
8288
8289 /* Compute margin and relief to draw. */
8290 relief = (tool_bar_button_relief >= 0
8291 ? tool_bar_button_relief
8292 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8293 hmargin = vmargin = relief;
8294
8295 if (INTEGERP (Vtool_bar_button_margin)
8296 && XINT (Vtool_bar_button_margin) > 0)
8297 {
8298 hmargin += XFASTINT (Vtool_bar_button_margin);
8299 vmargin += XFASTINT (Vtool_bar_button_margin);
8300 }
8301 else if (CONSP (Vtool_bar_button_margin))
8302 {
8303 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8304 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8305 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8306
8307 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8308 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8309 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8310 }
8311
8312 if (auto_raise_tool_bar_buttons_p)
8313 {
8314 /* Add a `:relief' property to the image spec if the item is
8315 selected. */
8316 if (selected_p)
8317 {
8318 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8319 hmargin -= relief;
8320 vmargin -= relief;
8321 }
8322 }
8323 else
8324 {
8325 /* If image is selected, display it pressed, i.e. with a
8326 negative relief. If it's not selected, display it with a
8327 raised relief. */
8328 plist = Fplist_put (plist, QCrelief,
8329 (selected_p
8330 ? make_number (-relief)
8331 : make_number (relief)));
8332 hmargin -= relief;
8333 vmargin -= relief;
8334 }
8335
8336 /* Put a margin around the image. */
8337 if (hmargin || vmargin)
8338 {
8339 if (hmargin == vmargin)
8340 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8341 else
8342 plist = Fplist_put (plist, QCmargin,
8343 Fcons (make_number (hmargin),
8344 make_number (vmargin)));
8345 }
8346
8347 /* If button is not enabled, and we don't have special images
8348 for the disabled state, make the image appear disabled by
8349 applying an appropriate algorithm to it. */
8350 if (!enabled_p && idx < 0)
8351 plist = Fplist_put (plist, QCconversion, Qdisabled);
8352
8353 /* Put a `display' text property on the string for the image to
8354 display. Put a `menu-item' property on the string that gives
8355 the start of this item's properties in the tool-bar items
8356 vector. */
8357 image = Fcons (Qimage, plist);
8358 props = list4 (Qdisplay, image,
8359 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8360
8361 /* Let the last image hide all remaining spaces in the tool bar
8362 string. The string can be longer than needed when we reuse a
8363 previous string. */
8364 if (i + 1 == f->n_tool_bar_items)
8365 end = SCHARS (f->desired_tool_bar_string);
8366 else
8367 end = i + 1;
8368 Fadd_text_properties (make_number (i), make_number (end),
8369 props, f->desired_tool_bar_string);
8370 #undef PROP
8371 }
8372
8373 UNGCPRO;
8374 }
8375
8376
8377 /* Display one line of the tool-bar of frame IT->f. */
8378
8379 static void
8380 display_tool_bar_line (it)
8381 struct it *it;
8382 {
8383 struct glyph_row *row = it->glyph_row;
8384 int max_x = it->last_visible_x;
8385 struct glyph *last;
8386
8387 prepare_desired_row (row);
8388 row->y = it->current_y;
8389
8390 /* Note that this isn't made use of if the face hasn't a box,
8391 so there's no need to check the face here. */
8392 it->start_of_box_run_p = 1;
8393
8394 while (it->current_x < max_x)
8395 {
8396 int x_before, x, n_glyphs_before, i, nglyphs;
8397
8398 /* Get the next display element. */
8399 if (!get_next_display_element (it))
8400 break;
8401
8402 /* Produce glyphs. */
8403 x_before = it->current_x;
8404 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8405 PRODUCE_GLYPHS (it);
8406
8407 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8408 i = 0;
8409 x = x_before;
8410 while (i < nglyphs)
8411 {
8412 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
8413
8414 if (x + glyph->pixel_width > max_x)
8415 {
8416 /* Glyph doesn't fit on line. */
8417 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8418 it->current_x = x;
8419 goto out;
8420 }
8421
8422 ++it->hpos;
8423 x += glyph->pixel_width;
8424 ++i;
8425 }
8426
8427 /* Stop at line ends. */
8428 if (ITERATOR_AT_END_OF_LINE_P (it))
8429 break;
8430
8431 set_iterator_to_next (it, 1);
8432 }
8433
8434 out:;
8435
8436 row->displays_text_p = row->used[TEXT_AREA] != 0;
8437 extend_face_to_end_of_line (it);
8438 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8439 last->right_box_line_p = 1;
8440 if (last == row->glyphs[TEXT_AREA])
8441 last->left_box_line_p = 1;
8442 compute_line_metrics (it);
8443
8444 /* If line is empty, make it occupy the rest of the tool-bar. */
8445 if (!row->displays_text_p)
8446 {
8447 row->height = row->phys_height = it->last_visible_y - row->y;
8448 row->ascent = row->phys_ascent = 0;
8449 }
8450
8451 row->full_width_p = 1;
8452 row->continued_p = 0;
8453 row->truncated_on_left_p = 0;
8454 row->truncated_on_right_p = 0;
8455
8456 it->current_x = it->hpos = 0;
8457 it->current_y += row->height;
8458 ++it->vpos;
8459 ++it->glyph_row;
8460 }
8461
8462
8463 /* Value is the number of screen lines needed to make all tool-bar
8464 items of frame F visible. */
8465
8466 static int
8467 tool_bar_lines_needed (f)
8468 struct frame *f;
8469 {
8470 struct window *w = XWINDOW (f->tool_bar_window);
8471 struct it it;
8472
8473 /* Initialize an iterator for iteration over
8474 F->desired_tool_bar_string in the tool-bar window of frame F. */
8475 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8476 it.first_visible_x = 0;
8477 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8478 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8479
8480 while (!ITERATOR_AT_END_P (&it))
8481 {
8482 it.glyph_row = w->desired_matrix->rows;
8483 clear_glyph_row (it.glyph_row);
8484 display_tool_bar_line (&it);
8485 }
8486
8487 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
8488 }
8489
8490
8491 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8492 0, 1, 0,
8493 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8494 (frame)
8495 Lisp_Object frame;
8496 {
8497 struct frame *f;
8498 struct window *w;
8499 int nlines = 0;
8500
8501 if (NILP (frame))
8502 frame = selected_frame;
8503 else
8504 CHECK_FRAME (frame);
8505 f = XFRAME (frame);
8506
8507 if (WINDOWP (f->tool_bar_window)
8508 || (w = XWINDOW (f->tool_bar_window),
8509 WINDOW_TOTAL_LINES (w) > 0))
8510 {
8511 update_tool_bar (f, 1);
8512 if (f->n_tool_bar_items)
8513 {
8514 build_desired_tool_bar_string (f);
8515 nlines = tool_bar_lines_needed (f);
8516 }
8517 }
8518
8519 return make_number (nlines);
8520 }
8521
8522
8523 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
8524 height should be changed. */
8525
8526 static int
8527 redisplay_tool_bar (f)
8528 struct frame *f;
8529 {
8530 struct window *w;
8531 struct it it;
8532 struct glyph_row *row;
8533 int change_height_p = 0;
8534
8535 #ifdef USE_GTK
8536 if (FRAME_EXTERNAL_TOOL_BAR(f))
8537 update_frame_tool_bar (f);
8538 return 0;
8539 #endif
8540
8541 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8542 do anything. This means you must start with tool-bar-lines
8543 non-zero to get the auto-sizing effect. Or in other words, you
8544 can turn off tool-bars by specifying tool-bar-lines zero. */
8545 if (!WINDOWP (f->tool_bar_window)
8546 || (w = XWINDOW (f->tool_bar_window),
8547 WINDOW_TOTAL_LINES (w) == 0))
8548 return 0;
8549
8550 /* Set up an iterator for the tool-bar window. */
8551 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8552 it.first_visible_x = 0;
8553 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8554 row = it.glyph_row;
8555
8556 /* Build a string that represents the contents of the tool-bar. */
8557 build_desired_tool_bar_string (f);
8558 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8559
8560 /* Display as many lines as needed to display all tool-bar items. */
8561 while (it.current_y < it.last_visible_y)
8562 display_tool_bar_line (&it);
8563
8564 /* It doesn't make much sense to try scrolling in the tool-bar
8565 window, so don't do it. */
8566 w->desired_matrix->no_scrolling_p = 1;
8567 w->must_be_updated_p = 1;
8568
8569 if (auto_resize_tool_bars_p)
8570 {
8571 int nlines;
8572
8573 /* If we couldn't display everything, change the tool-bar's
8574 height. */
8575 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8576 change_height_p = 1;
8577
8578 /* If there are blank lines at the end, except for a partially
8579 visible blank line at the end that is smaller than
8580 FRAME_LINE_HEIGHT, change the tool-bar's height. */
8581 row = it.glyph_row - 1;
8582 if (!row->displays_text_p
8583 && row->height >= FRAME_LINE_HEIGHT (f))
8584 change_height_p = 1;
8585
8586 /* If row displays tool-bar items, but is partially visible,
8587 change the tool-bar's height. */
8588 if (row->displays_text_p
8589 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8590 change_height_p = 1;
8591
8592 /* Resize windows as needed by changing the `tool-bar-lines'
8593 frame parameter. */
8594 if (change_height_p
8595 && (nlines = tool_bar_lines_needed (f),
8596 nlines != WINDOW_TOTAL_LINES (w)))
8597 {
8598 extern Lisp_Object Qtool_bar_lines;
8599 Lisp_Object frame;
8600 int old_height = WINDOW_TOTAL_LINES (w);
8601
8602 XSETFRAME (frame, f);
8603 clear_glyph_matrix (w->desired_matrix);
8604 Fmodify_frame_parameters (frame,
8605 Fcons (Fcons (Qtool_bar_lines,
8606 make_number (nlines)),
8607 Qnil));
8608 if (WINDOW_TOTAL_LINES (w) != old_height)
8609 fonts_changed_p = 1;
8610 }
8611 }
8612
8613 return change_height_p;
8614 }
8615
8616
8617 /* Get information about the tool-bar item which is displayed in GLYPH
8618 on frame F. Return in *PROP_IDX the index where tool-bar item
8619 properties start in F->tool_bar_items. Value is zero if
8620 GLYPH doesn't display a tool-bar item. */
8621
8622 static int
8623 tool_bar_item_info (f, glyph, prop_idx)
8624 struct frame *f;
8625 struct glyph *glyph;
8626 int *prop_idx;
8627 {
8628 Lisp_Object prop;
8629 int success_p;
8630 int charpos;
8631
8632 /* This function can be called asynchronously, which means we must
8633 exclude any possibility that Fget_text_property signals an
8634 error. */
8635 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8636 charpos = max (0, charpos);
8637
8638 /* Get the text property `menu-item' at pos. The value of that
8639 property is the start index of this item's properties in
8640 F->tool_bar_items. */
8641 prop = Fget_text_property (make_number (charpos),
8642 Qmenu_item, f->current_tool_bar_string);
8643 if (INTEGERP (prop))
8644 {
8645 *prop_idx = XINT (prop);
8646 success_p = 1;
8647 }
8648 else
8649 success_p = 0;
8650
8651 return success_p;
8652 }
8653
8654 \f
8655 /* Get information about the tool-bar item at position X/Y on frame F.
8656 Return in *GLYPH a pointer to the glyph of the tool-bar item in
8657 the current matrix of the tool-bar window of F, or NULL if not
8658 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8659 item in F->tool_bar_items. Value is
8660
8661 -1 if X/Y is not on a tool-bar item
8662 0 if X/Y is on the same item that was highlighted before.
8663 1 otherwise. */
8664
8665 static int
8666 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
8667 struct frame *f;
8668 int x, y;
8669 struct glyph **glyph;
8670 int *hpos, *vpos, *prop_idx;
8671 {
8672 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8673 struct window *w = XWINDOW (f->tool_bar_window);
8674 int area;
8675
8676 /* Find the glyph under X/Y. */
8677 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area, 0);
8678 if (*glyph == NULL)
8679 return -1;
8680
8681 /* Get the start of this tool-bar item's properties in
8682 f->tool_bar_items. */
8683 if (!tool_bar_item_info (f, *glyph, prop_idx))
8684 return -1;
8685
8686 /* Is mouse on the highlighted item? */
8687 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
8688 && *vpos >= dpyinfo->mouse_face_beg_row
8689 && *vpos <= dpyinfo->mouse_face_end_row
8690 && (*vpos > dpyinfo->mouse_face_beg_row
8691 || *hpos >= dpyinfo->mouse_face_beg_col)
8692 && (*vpos < dpyinfo->mouse_face_end_row
8693 || *hpos < dpyinfo->mouse_face_end_col
8694 || dpyinfo->mouse_face_past_end))
8695 return 0;
8696
8697 return 1;
8698 }
8699
8700
8701 /* EXPORT:
8702 Handle mouse button event on the tool-bar of frame F, at
8703 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
8704 0 for button release. MODIFIERS is event modifiers for button
8705 release. */
8706
8707 void
8708 handle_tool_bar_click (f, x, y, down_p, modifiers)
8709 struct frame *f;
8710 int x, y, down_p;
8711 unsigned int modifiers;
8712 {
8713 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8714 struct window *w = XWINDOW (f->tool_bar_window);
8715 int hpos, vpos, prop_idx;
8716 struct glyph *glyph;
8717 Lisp_Object enabled_p;
8718
8719 /* If not on the highlighted tool-bar item, return. */
8720 frame_to_window_pixel_xy (w, &x, &y);
8721 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
8722 return;
8723
8724 /* If item is disabled, do nothing. */
8725 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8726 if (NILP (enabled_p))
8727 return;
8728
8729 if (down_p)
8730 {
8731 /* Show item in pressed state. */
8732 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
8733 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
8734 last_tool_bar_item = prop_idx;
8735 }
8736 else
8737 {
8738 Lisp_Object key, frame;
8739 struct input_event event;
8740
8741 /* Show item in released state. */
8742 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
8743 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
8744
8745 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
8746
8747 XSETFRAME (frame, f);
8748 event.kind = TOOL_BAR_EVENT;
8749 event.frame_or_window = frame;
8750 event.arg = frame;
8751 kbd_buffer_store_event (&event);
8752
8753 event.kind = TOOL_BAR_EVENT;
8754 event.frame_or_window = frame;
8755 event.arg = key;
8756 event.modifiers = modifiers;
8757 kbd_buffer_store_event (&event);
8758 last_tool_bar_item = -1;
8759 }
8760 }
8761
8762
8763 /* Possibly highlight a tool-bar item on frame F when mouse moves to
8764 tool-bar window-relative coordinates X/Y. Called from
8765 note_mouse_highlight. */
8766
8767 static void
8768 note_tool_bar_highlight (f, x, y)
8769 struct frame *f;
8770 int x, y;
8771 {
8772 Lisp_Object window = f->tool_bar_window;
8773 struct window *w = XWINDOW (window);
8774 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8775 int hpos, vpos;
8776 struct glyph *glyph;
8777 struct glyph_row *row;
8778 int i;
8779 Lisp_Object enabled_p;
8780 int prop_idx;
8781 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
8782 int mouse_down_p, rc;
8783
8784 /* Function note_mouse_highlight is called with negative x(y
8785 values when mouse moves outside of the frame. */
8786 if (x <= 0 || y <= 0)
8787 {
8788 clear_mouse_face (dpyinfo);
8789 return;
8790 }
8791
8792 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
8793 if (rc < 0)
8794 {
8795 /* Not on tool-bar item. */
8796 clear_mouse_face (dpyinfo);
8797 return;
8798 }
8799 else if (rc == 0)
8800 /* On same tool-bar item as before. */
8801 goto set_help_echo;
8802
8803 clear_mouse_face (dpyinfo);
8804
8805 /* Mouse is down, but on different tool-bar item? */
8806 mouse_down_p = (dpyinfo->grabbed
8807 && f == last_mouse_frame
8808 && FRAME_LIVE_P (f));
8809 if (mouse_down_p
8810 && last_tool_bar_item != prop_idx)
8811 return;
8812
8813 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
8814 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
8815
8816 /* If tool-bar item is not enabled, don't highlight it. */
8817 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8818 if (!NILP (enabled_p))
8819 {
8820 /* Compute the x-position of the glyph. In front and past the
8821 image is a space. We include this in the highlighted area. */
8822 row = MATRIX_ROW (w->current_matrix, vpos);
8823 for (i = x = 0; i < hpos; ++i)
8824 x += row->glyphs[TEXT_AREA][i].pixel_width;
8825
8826 /* Record this as the current active region. */
8827 dpyinfo->mouse_face_beg_col = hpos;
8828 dpyinfo->mouse_face_beg_row = vpos;
8829 dpyinfo->mouse_face_beg_x = x;
8830 dpyinfo->mouse_face_beg_y = row->y;
8831 dpyinfo->mouse_face_past_end = 0;
8832
8833 dpyinfo->mouse_face_end_col = hpos + 1;
8834 dpyinfo->mouse_face_end_row = vpos;
8835 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
8836 dpyinfo->mouse_face_end_y = row->y;
8837 dpyinfo->mouse_face_window = window;
8838 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
8839
8840 /* Display it as active. */
8841 show_mouse_face (dpyinfo, draw);
8842 dpyinfo->mouse_face_image_state = draw;
8843 }
8844
8845 set_help_echo:
8846
8847 /* Set help_echo_string to a help string to display for this tool-bar item.
8848 XTread_socket does the rest. */
8849 help_echo_object = help_echo_window = Qnil;
8850 help_echo_pos = -1;
8851 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
8852 if (NILP (help_echo_string))
8853 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
8854 }
8855
8856 #endif /* HAVE_WINDOW_SYSTEM */
8857
8858
8859 \f
8860 /***********************************************************************
8861 Fringes
8862 ***********************************************************************/
8863
8864 #ifdef HAVE_WINDOW_SYSTEM
8865
8866 /* An arrow like this: `<-'. */
8867 static unsigned char left_bits[] = {
8868 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
8869
8870 /* Right truncation arrow bitmap `->'. */
8871 static unsigned char right_bits[] = {
8872 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
8873
8874 /* Marker for continued lines. */
8875 static unsigned char continued_bits[] = {
8876 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
8877
8878 /* Marker for continuation lines. */
8879 static unsigned char continuation_bits[] = {
8880 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
8881
8882 /* Overlay arrow bitmap. A triangular arrow. */
8883 static unsigned char ov_bits[] = {
8884 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
8885
8886 /* Bitmap drawn to indicate lines not displaying text if
8887 `indicate-empty-lines' is non-nil. */
8888 static unsigned char zv_bits[] = {
8889 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8890 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8891 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8892 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8893 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8894 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8895 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8896 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00};
8897
8898 struct fringe_bitmap fringe_bitmaps[MAX_FRINGE_BITMAPS] =
8899 {
8900 { 0, 0, 0, NULL /* NO_FRINGE_BITMAP */ },
8901 { 8, sizeof (left_bits), 0, left_bits },
8902 { 8, sizeof (right_bits), 0, right_bits },
8903 { 8, sizeof (continued_bits), 0, continued_bits },
8904 { 8, sizeof (continuation_bits), 0, continuation_bits },
8905 { 8, sizeof (ov_bits), 0, ov_bits },
8906 { 8, sizeof (zv_bits), 3, zv_bits }
8907 };
8908
8909
8910 /* Draw the bitmap WHICH in one of the left or right fringes of
8911 window W. ROW is the glyph row for which to display the bitmap; it
8912 determines the vertical position at which the bitmap has to be
8913 drawn. */
8914
8915 static void
8916 draw_fringe_bitmap (w, row, which, left_p)
8917 struct window *w;
8918 struct glyph_row *row;
8919 enum fringe_bitmap_type which;
8920 int left_p;
8921 {
8922 struct frame *f = XFRAME (WINDOW_FRAME (w));
8923 struct draw_fringe_bitmap_params p;
8924
8925 /* Convert row to frame coordinates. */
8926 p.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
8927
8928 p.which = which;
8929 p.wd = fringe_bitmaps[which].width;
8930
8931 p.h = fringe_bitmaps[which].height;
8932 p.dh = (fringe_bitmaps[which].period
8933 ? (p.y % fringe_bitmaps[which].period)
8934 : 0);
8935 p.h -= p.dh;
8936 /* Clip bitmap if too high. */
8937 if (p.h > row->height)
8938 p.h = row->height;
8939
8940 p.face = FACE_FROM_ID (f, FRINGE_FACE_ID);
8941 PREPARE_FACE_FOR_DISPLAY (f, p.face);
8942
8943 /* Clear left fringe if no bitmap to draw or if bitmap doesn't fill
8944 the fringe. */
8945 p.bx = -1;
8946 if (left_p)
8947 {
8948 int wd = WINDOW_LEFT_FRINGE_WIDTH (w);
8949 int x = window_box_left (w, (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
8950 ? LEFT_MARGIN_AREA
8951 : TEXT_AREA));
8952 if (p.wd > wd)
8953 p.wd = wd;
8954 p.x = x - p.wd - (wd - p.wd) / 2;
8955
8956 if (p.wd < wd || row->height > p.h)
8957 {
8958 /* If W has a vertical border to its left, don't draw over it. */
8959 wd -= ((!WINDOW_LEFTMOST_P (w)
8960 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
8961 ? 1 : 0);
8962 p.bx = x - wd;
8963 p.nx = wd;
8964 }
8965 }
8966 else
8967 {
8968 int x = window_box_right (w,
8969 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
8970 ? RIGHT_MARGIN_AREA
8971 : TEXT_AREA));
8972 int wd = WINDOW_RIGHT_FRINGE_WIDTH (w);
8973 if (p.wd > wd)
8974 p.wd = wd;
8975 p.x = x + (wd - p.wd) / 2;
8976 /* Clear right fringe if no bitmap to draw of if bitmap doesn't fill
8977 the fringe. */
8978 if (p.wd < wd || row->height > p.h)
8979 {
8980 p.bx = x;
8981 p.nx = wd;
8982 }
8983 }
8984
8985 if (p.bx >= 0)
8986 {
8987 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
8988
8989 p.by = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, row->y));
8990 p.ny = row->visible_height;
8991 }
8992
8993 /* Adjust y to the offset in the row to start drawing the bitmap. */
8994 p.y += (row->height - p.h) / 2;
8995
8996 rif->draw_fringe_bitmap (w, row, &p);
8997 }
8998
8999 /* Draw fringe bitmaps for glyph row ROW on window W. Call this
9000 function with input blocked. */
9001
9002 void
9003 draw_row_fringe_bitmaps (w, row)
9004 struct window *w;
9005 struct glyph_row *row;
9006 {
9007 enum fringe_bitmap_type bitmap;
9008
9009 xassert (interrupt_input_blocked);
9010
9011 /* If row is completely invisible, because of vscrolling, we
9012 don't have to draw anything. */
9013 if (row->visible_height <= 0)
9014 return;
9015
9016 if (WINDOW_LEFT_FRINGE_WIDTH (w) != 0)
9017 {
9018 /* Decide which bitmap to draw in the left fringe. */
9019 if (row->overlay_arrow_p)
9020 bitmap = OVERLAY_ARROW_BITMAP;
9021 else if (row->truncated_on_left_p)
9022 bitmap = LEFT_TRUNCATION_BITMAP;
9023 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
9024 bitmap = CONTINUATION_LINE_BITMAP;
9025 else if (row->indicate_empty_line_p)
9026 bitmap = ZV_LINE_BITMAP;
9027 else
9028 bitmap = NO_FRINGE_BITMAP;
9029
9030 draw_fringe_bitmap (w, row, bitmap, 1);
9031 }
9032
9033 if (WINDOW_RIGHT_FRINGE_WIDTH (w) != 0)
9034 {
9035 /* Decide which bitmap to draw in the right fringe. */
9036 if (row->truncated_on_right_p)
9037 bitmap = RIGHT_TRUNCATION_BITMAP;
9038 else if (row->continued_p)
9039 bitmap = CONTINUED_LINE_BITMAP;
9040 else if (row->indicate_empty_line_p && WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
9041 bitmap = ZV_LINE_BITMAP;
9042 else
9043 bitmap = NO_FRINGE_BITMAP;
9044
9045 draw_fringe_bitmap (w, row, bitmap, 0);
9046 }
9047 }
9048
9049
9050 /* Compute actual fringe widths */
9051
9052 void
9053 compute_fringe_widths (f, redraw)
9054 struct frame *f;
9055 int redraw;
9056 {
9057 int o_left = FRAME_LEFT_FRINGE_WIDTH (f);
9058 int o_right = FRAME_RIGHT_FRINGE_WIDTH (f);
9059 int o_cols = FRAME_FRINGE_COLS (f);
9060
9061 Lisp_Object left_fringe = Fassq (Qleft_fringe, f->param_alist);
9062 Lisp_Object right_fringe = Fassq (Qright_fringe, f->param_alist);
9063 int left_fringe_width, right_fringe_width;
9064
9065 if (!NILP (left_fringe))
9066 left_fringe = Fcdr (left_fringe);
9067 if (!NILP (right_fringe))
9068 right_fringe = Fcdr (right_fringe);
9069
9070 left_fringe_width = ((NILP (left_fringe) || !INTEGERP (left_fringe)) ? 8 :
9071 XINT (left_fringe));
9072 right_fringe_width = ((NILP (right_fringe) || !INTEGERP (right_fringe)) ? 8 :
9073 XINT (right_fringe));
9074
9075 if (left_fringe_width || right_fringe_width)
9076 {
9077 int left_wid = left_fringe_width >= 0 ? left_fringe_width : -left_fringe_width;
9078 int right_wid = right_fringe_width >= 0 ? right_fringe_width : -right_fringe_width;
9079 int conf_wid = left_wid + right_wid;
9080 int font_wid = FRAME_COLUMN_WIDTH (f);
9081 int cols = (left_wid + right_wid + font_wid-1) / font_wid;
9082 int real_wid = cols * font_wid;
9083 if (left_wid && right_wid)
9084 {
9085 if (left_fringe_width < 0)
9086 {
9087 /* Left fringe width is fixed, adjust right fringe if necessary */
9088 FRAME_LEFT_FRINGE_WIDTH (f) = left_wid;
9089 FRAME_RIGHT_FRINGE_WIDTH (f) = real_wid - left_wid;
9090 }
9091 else if (right_fringe_width < 0)
9092 {
9093 /* Right fringe width is fixed, adjust left fringe if necessary */
9094 FRAME_LEFT_FRINGE_WIDTH (f) = real_wid - right_wid;
9095 FRAME_RIGHT_FRINGE_WIDTH (f) = right_wid;
9096 }
9097 else
9098 {
9099 /* Adjust both fringes with an equal amount.
9100 Note that we are doing integer arithmetic here, so don't
9101 lose a pixel if the total width is an odd number. */
9102 int fill = real_wid - conf_wid;
9103 FRAME_LEFT_FRINGE_WIDTH (f) = left_wid + fill/2;
9104 FRAME_RIGHT_FRINGE_WIDTH (f) = right_wid + fill - fill/2;
9105 }
9106 }
9107 else if (left_fringe_width)
9108 {
9109 FRAME_LEFT_FRINGE_WIDTH (f) = real_wid;
9110 FRAME_RIGHT_FRINGE_WIDTH (f) = 0;
9111 }
9112 else
9113 {
9114 FRAME_LEFT_FRINGE_WIDTH (f) = 0;
9115 FRAME_RIGHT_FRINGE_WIDTH (f) = real_wid;
9116 }
9117 FRAME_FRINGE_COLS (f) = cols;
9118 }
9119 else
9120 {
9121 FRAME_LEFT_FRINGE_WIDTH (f) = 0;
9122 FRAME_RIGHT_FRINGE_WIDTH (f) = 0;
9123 FRAME_FRINGE_COLS (f) = 0;
9124 }
9125
9126 if (redraw && FRAME_VISIBLE_P (f))
9127 if (o_left != FRAME_LEFT_FRINGE_WIDTH (f) ||
9128 o_right != FRAME_RIGHT_FRINGE_WIDTH (f) ||
9129 o_cols != FRAME_FRINGE_COLS (f))
9130 redraw_frame (f);
9131 }
9132
9133 #endif /* HAVE_WINDOW_SYSTEM */
9134
9135
9136 \f
9137 /************************************************************************
9138 Horizontal scrolling
9139 ************************************************************************/
9140
9141 static int hscroll_window_tree P_ ((Lisp_Object));
9142 static int hscroll_windows P_ ((Lisp_Object));
9143
9144 /* For all leaf windows in the window tree rooted at WINDOW, set their
9145 hscroll value so that PT is (i) visible in the window, and (ii) so
9146 that it is not within a certain margin at the window's left and
9147 right border. Value is non-zero if any window's hscroll has been
9148 changed. */
9149
9150 static int
9151 hscroll_window_tree (window)
9152 Lisp_Object window;
9153 {
9154 int hscrolled_p = 0;
9155 int hscroll_relative_p = FLOATP (Vhscroll_step);
9156 int hscroll_step_abs = 0;
9157 double hscroll_step_rel = 0;
9158
9159 if (hscroll_relative_p)
9160 {
9161 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9162 if (hscroll_step_rel < 0)
9163 {
9164 hscroll_relative_p = 0;
9165 hscroll_step_abs = 0;
9166 }
9167 }
9168 else if (INTEGERP (Vhscroll_step))
9169 {
9170 hscroll_step_abs = XINT (Vhscroll_step);
9171 if (hscroll_step_abs < 0)
9172 hscroll_step_abs = 0;
9173 }
9174 else
9175 hscroll_step_abs = 0;
9176
9177 while (WINDOWP (window))
9178 {
9179 struct window *w = XWINDOW (window);
9180
9181 if (WINDOWP (w->hchild))
9182 hscrolled_p |= hscroll_window_tree (w->hchild);
9183 else if (WINDOWP (w->vchild))
9184 hscrolled_p |= hscroll_window_tree (w->vchild);
9185 else if (w->cursor.vpos >= 0)
9186 {
9187 int h_margin;
9188 int text_area_width;
9189 struct glyph_row *current_cursor_row
9190 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9191 struct glyph_row *desired_cursor_row
9192 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9193 struct glyph_row *cursor_row
9194 = (desired_cursor_row->enabled_p
9195 ? desired_cursor_row
9196 : current_cursor_row);
9197
9198 text_area_width = window_box_width (w, TEXT_AREA);
9199
9200 /* Scroll when cursor is inside this scroll margin. */
9201 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9202
9203 if ((XFASTINT (w->hscroll)
9204 && w->cursor.x <= h_margin)
9205 || (cursor_row->enabled_p
9206 && cursor_row->truncated_on_right_p
9207 && (w->cursor.x >= text_area_width - h_margin)))
9208 {
9209 struct it it;
9210 int hscroll;
9211 struct buffer *saved_current_buffer;
9212 int pt;
9213 int wanted_x;
9214
9215 /* Find point in a display of infinite width. */
9216 saved_current_buffer = current_buffer;
9217 current_buffer = XBUFFER (w->buffer);
9218
9219 if (w == XWINDOW (selected_window))
9220 pt = BUF_PT (current_buffer);
9221 else
9222 {
9223 pt = marker_position (w->pointm);
9224 pt = max (BEGV, pt);
9225 pt = min (ZV, pt);
9226 }
9227
9228 /* Move iterator to pt starting at cursor_row->start in
9229 a line with infinite width. */
9230 init_to_row_start (&it, w, cursor_row);
9231 it.last_visible_x = INFINITY;
9232 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9233 current_buffer = saved_current_buffer;
9234
9235 /* Position cursor in window. */
9236 if (!hscroll_relative_p && hscroll_step_abs == 0)
9237 hscroll = max (0, it.current_x - text_area_width / 2)
9238 / FRAME_COLUMN_WIDTH (it.f);
9239 else if (w->cursor.x >= text_area_width - h_margin)
9240 {
9241 if (hscroll_relative_p)
9242 wanted_x = text_area_width * (1 - hscroll_step_rel)
9243 - h_margin;
9244 else
9245 wanted_x = text_area_width
9246 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9247 - h_margin;
9248 hscroll
9249 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9250 }
9251 else
9252 {
9253 if (hscroll_relative_p)
9254 wanted_x = text_area_width * hscroll_step_rel
9255 + h_margin;
9256 else
9257 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9258 + h_margin;
9259 hscroll
9260 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9261 }
9262 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9263
9264 /* Don't call Fset_window_hscroll if value hasn't
9265 changed because it will prevent redisplay
9266 optimizations. */
9267 if (XFASTINT (w->hscroll) != hscroll)
9268 {
9269 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9270 w->hscroll = make_number (hscroll);
9271 hscrolled_p = 1;
9272 }
9273 }
9274 }
9275
9276 window = w->next;
9277 }
9278
9279 /* Value is non-zero if hscroll of any leaf window has been changed. */
9280 return hscrolled_p;
9281 }
9282
9283
9284 /* Set hscroll so that cursor is visible and not inside horizontal
9285 scroll margins for all windows in the tree rooted at WINDOW. See
9286 also hscroll_window_tree above. Value is non-zero if any window's
9287 hscroll has been changed. If it has, desired matrices on the frame
9288 of WINDOW are cleared. */
9289
9290 static int
9291 hscroll_windows (window)
9292 Lisp_Object window;
9293 {
9294 int hscrolled_p;
9295
9296 if (automatic_hscrolling_p)
9297 {
9298 hscrolled_p = hscroll_window_tree (window);
9299 if (hscrolled_p)
9300 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9301 }
9302 else
9303 hscrolled_p = 0;
9304 return hscrolled_p;
9305 }
9306
9307
9308 \f
9309 /************************************************************************
9310 Redisplay
9311 ************************************************************************/
9312
9313 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9314 to a non-zero value. This is sometimes handy to have in a debugger
9315 session. */
9316
9317 #if GLYPH_DEBUG
9318
9319 /* First and last unchanged row for try_window_id. */
9320
9321 int debug_first_unchanged_at_end_vpos;
9322 int debug_last_unchanged_at_beg_vpos;
9323
9324 /* Delta vpos and y. */
9325
9326 int debug_dvpos, debug_dy;
9327
9328 /* Delta in characters and bytes for try_window_id. */
9329
9330 int debug_delta, debug_delta_bytes;
9331
9332 /* Values of window_end_pos and window_end_vpos at the end of
9333 try_window_id. */
9334
9335 EMACS_INT debug_end_pos, debug_end_vpos;
9336
9337 /* Append a string to W->desired_matrix->method. FMT is a printf
9338 format string. A1...A9 are a supplement for a variable-length
9339 argument list. If trace_redisplay_p is non-zero also printf the
9340 resulting string to stderr. */
9341
9342 static void
9343 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9344 struct window *w;
9345 char *fmt;
9346 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9347 {
9348 char buffer[512];
9349 char *method = w->desired_matrix->method;
9350 int len = strlen (method);
9351 int size = sizeof w->desired_matrix->method;
9352 int remaining = size - len - 1;
9353
9354 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9355 if (len && remaining)
9356 {
9357 method[len] = '|';
9358 --remaining, ++len;
9359 }
9360
9361 strncpy (method + len, buffer, remaining);
9362
9363 if (trace_redisplay_p)
9364 fprintf (stderr, "%p (%s): %s\n",
9365 w,
9366 ((BUFFERP (w->buffer)
9367 && STRINGP (XBUFFER (w->buffer)->name))
9368 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9369 : "no buffer"),
9370 buffer);
9371 }
9372
9373 #endif /* GLYPH_DEBUG */
9374
9375
9376 /* Value is non-zero if all changes in window W, which displays
9377 current_buffer, are in the text between START and END. START is a
9378 buffer position, END is given as a distance from Z. Used in
9379 redisplay_internal for display optimization. */
9380
9381 static INLINE int
9382 text_outside_line_unchanged_p (w, start, end)
9383 struct window *w;
9384 int start, end;
9385 {
9386 int unchanged_p = 1;
9387
9388 /* If text or overlays have changed, see where. */
9389 if (XFASTINT (w->last_modified) < MODIFF
9390 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9391 {
9392 /* Gap in the line? */
9393 if (GPT < start || Z - GPT < end)
9394 unchanged_p = 0;
9395
9396 /* Changes start in front of the line, or end after it? */
9397 if (unchanged_p
9398 && (BEG_UNCHANGED < start - 1
9399 || END_UNCHANGED < end))
9400 unchanged_p = 0;
9401
9402 /* If selective display, can't optimize if changes start at the
9403 beginning of the line. */
9404 if (unchanged_p
9405 && INTEGERP (current_buffer->selective_display)
9406 && XINT (current_buffer->selective_display) > 0
9407 && (BEG_UNCHANGED < start || GPT <= start))
9408 unchanged_p = 0;
9409
9410 /* If there are overlays at the start or end of the line, these
9411 may have overlay strings with newlines in them. A change at
9412 START, for instance, may actually concern the display of such
9413 overlay strings as well, and they are displayed on different
9414 lines. So, quickly rule out this case. (For the future, it
9415 might be desirable to implement something more telling than
9416 just BEG/END_UNCHANGED.) */
9417 if (unchanged_p)
9418 {
9419 if (BEG + BEG_UNCHANGED == start
9420 && overlay_touches_p (start))
9421 unchanged_p = 0;
9422 if (END_UNCHANGED == end
9423 && overlay_touches_p (Z - end))
9424 unchanged_p = 0;
9425 }
9426 }
9427
9428 return unchanged_p;
9429 }
9430
9431
9432 /* Do a frame update, taking possible shortcuts into account. This is
9433 the main external entry point for redisplay.
9434
9435 If the last redisplay displayed an echo area message and that message
9436 is no longer requested, we clear the echo area or bring back the
9437 mini-buffer if that is in use. */
9438
9439 void
9440 redisplay ()
9441 {
9442 redisplay_internal (0);
9443 }
9444
9445
9446 /* Return 1 if point moved out of or into a composition. Otherwise
9447 return 0. PREV_BUF and PREV_PT are the last point buffer and
9448 position. BUF and PT are the current point buffer and position. */
9449
9450 int
9451 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9452 struct buffer *prev_buf, *buf;
9453 int prev_pt, pt;
9454 {
9455 int start, end;
9456 Lisp_Object prop;
9457 Lisp_Object buffer;
9458
9459 XSETBUFFER (buffer, buf);
9460 /* Check a composition at the last point if point moved within the
9461 same buffer. */
9462 if (prev_buf == buf)
9463 {
9464 if (prev_pt == pt)
9465 /* Point didn't move. */
9466 return 0;
9467
9468 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9469 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9470 && COMPOSITION_VALID_P (start, end, prop)
9471 && start < prev_pt && end > prev_pt)
9472 /* The last point was within the composition. Return 1 iff
9473 point moved out of the composition. */
9474 return (pt <= start || pt >= end);
9475 }
9476
9477 /* Check a composition at the current point. */
9478 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9479 && find_composition (pt, -1, &start, &end, &prop, buffer)
9480 && COMPOSITION_VALID_P (start, end, prop)
9481 && start < pt && end > pt);
9482 }
9483
9484
9485 /* Reconsider the setting of B->clip_changed which is displayed
9486 in window W. */
9487
9488 static INLINE void
9489 reconsider_clip_changes (w, b)
9490 struct window *w;
9491 struct buffer *b;
9492 {
9493 if (b->clip_changed
9494 && !NILP (w->window_end_valid)
9495 && w->current_matrix->buffer == b
9496 && w->current_matrix->zv == BUF_ZV (b)
9497 && w->current_matrix->begv == BUF_BEGV (b))
9498 b->clip_changed = 0;
9499
9500 /* If display wasn't paused, and W is not a tool bar window, see if
9501 point has been moved into or out of a composition. In that case,
9502 we set b->clip_changed to 1 to force updating the screen. If
9503 b->clip_changed has already been set to 1, we can skip this
9504 check. */
9505 if (!b->clip_changed
9506 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9507 {
9508 int pt;
9509
9510 if (w == XWINDOW (selected_window))
9511 pt = BUF_PT (current_buffer);
9512 else
9513 pt = marker_position (w->pointm);
9514
9515 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
9516 || pt != XINT (w->last_point))
9517 && check_point_in_composition (w->current_matrix->buffer,
9518 XINT (w->last_point),
9519 XBUFFER (w->buffer), pt))
9520 b->clip_changed = 1;
9521 }
9522 }
9523 \f
9524 #define STOP_POLLING \
9525 do { if (! polling_stopped_here) stop_polling (); \
9526 polling_stopped_here = 1; } while (0)
9527
9528 #define RESUME_POLLING \
9529 do { if (polling_stopped_here) start_polling (); \
9530 polling_stopped_here = 0; } while (0)
9531
9532
9533 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9534 response to any user action; therefore, we should preserve the echo
9535 area. (Actually, our caller does that job.) Perhaps in the future
9536 avoid recentering windows if it is not necessary; currently that
9537 causes some problems. */
9538
9539 static void
9540 redisplay_internal (preserve_echo_area)
9541 int preserve_echo_area;
9542 {
9543 struct window *w = XWINDOW (selected_window);
9544 struct frame *f = XFRAME (w->frame);
9545 int pause;
9546 int must_finish = 0;
9547 struct text_pos tlbufpos, tlendpos;
9548 int number_of_visible_frames;
9549 int count;
9550 struct frame *sf = SELECTED_FRAME ();
9551 int polling_stopped_here = 0;
9552
9553 /* Non-zero means redisplay has to consider all windows on all
9554 frames. Zero means, only selected_window is considered. */
9555 int consider_all_windows_p;
9556
9557 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9558
9559 /* No redisplay if running in batch mode or frame is not yet fully
9560 initialized, or redisplay is explicitly turned off by setting
9561 Vinhibit_redisplay. */
9562 if (noninteractive
9563 || !NILP (Vinhibit_redisplay)
9564 || !f->glyphs_initialized_p)
9565 return;
9566
9567 /* The flag redisplay_performed_directly_p is set by
9568 direct_output_for_insert when it already did the whole screen
9569 update necessary. */
9570 if (redisplay_performed_directly_p)
9571 {
9572 redisplay_performed_directly_p = 0;
9573 if (!hscroll_windows (selected_window))
9574 return;
9575 }
9576
9577 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9578 if (popup_activated ())
9579 return;
9580 #endif
9581
9582 /* I don't think this happens but let's be paranoid. */
9583 if (redisplaying_p)
9584 return;
9585
9586 /* Record a function that resets redisplaying_p to its old value
9587 when we leave this function. */
9588 count = SPECPDL_INDEX ();
9589 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
9590 ++redisplaying_p;
9591 specbind (Qinhibit_free_realized_faces, Qnil);
9592
9593 retry:
9594 pause = 0;
9595 reconsider_clip_changes (w, current_buffer);
9596
9597 /* If new fonts have been loaded that make a glyph matrix adjustment
9598 necessary, do it. */
9599 if (fonts_changed_p)
9600 {
9601 adjust_glyphs (NULL);
9602 ++windows_or_buffers_changed;
9603 fonts_changed_p = 0;
9604 }
9605
9606 /* If face_change_count is non-zero, init_iterator will free all
9607 realized faces, which includes the faces referenced from current
9608 matrices. So, we can't reuse current matrices in this case. */
9609 if (face_change_count)
9610 ++windows_or_buffers_changed;
9611
9612 if (! FRAME_WINDOW_P (sf)
9613 && previous_terminal_frame != sf)
9614 {
9615 /* Since frames on an ASCII terminal share the same display
9616 area, displaying a different frame means redisplay the whole
9617 thing. */
9618 windows_or_buffers_changed++;
9619 SET_FRAME_GARBAGED (sf);
9620 XSETFRAME (Vterminal_frame, sf);
9621 }
9622 previous_terminal_frame = sf;
9623
9624 /* Set the visible flags for all frames. Do this before checking
9625 for resized or garbaged frames; they want to know if their frames
9626 are visible. See the comment in frame.h for
9627 FRAME_SAMPLE_VISIBILITY. */
9628 {
9629 Lisp_Object tail, frame;
9630
9631 number_of_visible_frames = 0;
9632
9633 FOR_EACH_FRAME (tail, frame)
9634 {
9635 struct frame *f = XFRAME (frame);
9636
9637 FRAME_SAMPLE_VISIBILITY (f);
9638 if (FRAME_VISIBLE_P (f))
9639 ++number_of_visible_frames;
9640 clear_desired_matrices (f);
9641 }
9642 }
9643
9644 /* Notice any pending interrupt request to change frame size. */
9645 do_pending_window_change (1);
9646
9647 /* Clear frames marked as garbaged. */
9648 if (frame_garbaged)
9649 clear_garbaged_frames ();
9650
9651 /* Build menubar and tool-bar items. */
9652 prepare_menu_bars ();
9653
9654 if (windows_or_buffers_changed)
9655 update_mode_lines++;
9656
9657 /* Detect case that we need to write or remove a star in the mode line. */
9658 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
9659 {
9660 w->update_mode_line = Qt;
9661 if (buffer_shared > 1)
9662 update_mode_lines++;
9663 }
9664
9665 /* If %c is in the mode line, update it if needed. */
9666 if (!NILP (w->column_number_displayed)
9667 /* This alternative quickly identifies a common case
9668 where no change is needed. */
9669 && !(PT == XFASTINT (w->last_point)
9670 && XFASTINT (w->last_modified) >= MODIFF
9671 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9672 && (XFASTINT (w->column_number_displayed)
9673 != (int) current_column ())) /* iftc */
9674 w->update_mode_line = Qt;
9675
9676 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
9677
9678 /* The variable buffer_shared is set in redisplay_window and
9679 indicates that we redisplay a buffer in different windows. See
9680 there. */
9681 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
9682 || cursor_type_changed);
9683
9684 /* If specs for an arrow have changed, do thorough redisplay
9685 to ensure we remove any arrow that should no longer exist. */
9686 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
9687 || ! EQ (Voverlay_arrow_string, last_arrow_string))
9688 consider_all_windows_p = windows_or_buffers_changed = 1;
9689
9690 /* Normally the message* functions will have already displayed and
9691 updated the echo area, but the frame may have been trashed, or
9692 the update may have been preempted, so display the echo area
9693 again here. Checking message_cleared_p captures the case that
9694 the echo area should be cleared. */
9695 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
9696 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
9697 || (message_cleared_p
9698 && minibuf_level == 0
9699 /* If the mini-window is currently selected, this means the
9700 echo-area doesn't show through. */
9701 && !MINI_WINDOW_P (XWINDOW (selected_window))))
9702 {
9703 int window_height_changed_p = echo_area_display (0);
9704 must_finish = 1;
9705
9706 /* If we don't display the current message, don't clear the
9707 message_cleared_p flag, because, if we did, we wouldn't clear
9708 the echo area in the next redisplay which doesn't preserve
9709 the echo area. */
9710 if (!display_last_displayed_message_p)
9711 message_cleared_p = 0;
9712
9713 if (fonts_changed_p)
9714 goto retry;
9715 else if (window_height_changed_p)
9716 {
9717 consider_all_windows_p = 1;
9718 ++update_mode_lines;
9719 ++windows_or_buffers_changed;
9720
9721 /* If window configuration was changed, frames may have been
9722 marked garbaged. Clear them or we will experience
9723 surprises wrt scrolling. */
9724 if (frame_garbaged)
9725 clear_garbaged_frames ();
9726 }
9727 }
9728 else if (EQ (selected_window, minibuf_window)
9729 && (current_buffer->clip_changed
9730 || XFASTINT (w->last_modified) < MODIFF
9731 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9732 && resize_mini_window (w, 0))
9733 {
9734 /* Resized active mini-window to fit the size of what it is
9735 showing if its contents might have changed. */
9736 must_finish = 1;
9737 consider_all_windows_p = 1;
9738 ++windows_or_buffers_changed;
9739 ++update_mode_lines;
9740
9741 /* If window configuration was changed, frames may have been
9742 marked garbaged. Clear them or we will experience
9743 surprises wrt scrolling. */
9744 if (frame_garbaged)
9745 clear_garbaged_frames ();
9746 }
9747
9748
9749 /* If showing the region, and mark has changed, we must redisplay
9750 the whole window. The assignment to this_line_start_pos prevents
9751 the optimization directly below this if-statement. */
9752 if (((!NILP (Vtransient_mark_mode)
9753 && !NILP (XBUFFER (w->buffer)->mark_active))
9754 != !NILP (w->region_showing))
9755 || (!NILP (w->region_showing)
9756 && !EQ (w->region_showing,
9757 Fmarker_position (XBUFFER (w->buffer)->mark))))
9758 CHARPOS (this_line_start_pos) = 0;
9759
9760 /* Optimize the case that only the line containing the cursor in the
9761 selected window has changed. Variables starting with this_ are
9762 set in display_line and record information about the line
9763 containing the cursor. */
9764 tlbufpos = this_line_start_pos;
9765 tlendpos = this_line_end_pos;
9766 if (!consider_all_windows_p
9767 && CHARPOS (tlbufpos) > 0
9768 && NILP (w->update_mode_line)
9769 && !current_buffer->clip_changed
9770 && !current_buffer->prevent_redisplay_optimizations_p
9771 && FRAME_VISIBLE_P (XFRAME (w->frame))
9772 && !FRAME_OBSCURED_P (XFRAME (w->frame))
9773 /* Make sure recorded data applies to current buffer, etc. */
9774 && this_line_buffer == current_buffer
9775 && current_buffer == XBUFFER (w->buffer)
9776 && NILP (w->force_start)
9777 && NILP (w->optional_new_start)
9778 /* Point must be on the line that we have info recorded about. */
9779 && PT >= CHARPOS (tlbufpos)
9780 && PT <= Z - CHARPOS (tlendpos)
9781 /* All text outside that line, including its final newline,
9782 must be unchanged */
9783 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
9784 CHARPOS (tlendpos)))
9785 {
9786 if (CHARPOS (tlbufpos) > BEGV
9787 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
9788 && (CHARPOS (tlbufpos) == ZV
9789 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
9790 /* Former continuation line has disappeared by becoming empty */
9791 goto cancel;
9792 else if (XFASTINT (w->last_modified) < MODIFF
9793 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
9794 || MINI_WINDOW_P (w))
9795 {
9796 /* We have to handle the case of continuation around a
9797 wide-column character (See the comment in indent.c around
9798 line 885).
9799
9800 For instance, in the following case:
9801
9802 -------- Insert --------
9803 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
9804 J_I_ ==> J_I_ `^^' are cursors.
9805 ^^ ^^
9806 -------- --------
9807
9808 As we have to redraw the line above, we should goto cancel. */
9809
9810 struct it it;
9811 int line_height_before = this_line_pixel_height;
9812
9813 /* Note that start_display will handle the case that the
9814 line starting at tlbufpos is a continuation lines. */
9815 start_display (&it, w, tlbufpos);
9816
9817 /* Implementation note: It this still necessary? */
9818 if (it.current_x != this_line_start_x)
9819 goto cancel;
9820
9821 TRACE ((stderr, "trying display optimization 1\n"));
9822 w->cursor.vpos = -1;
9823 overlay_arrow_seen = 0;
9824 it.vpos = this_line_vpos;
9825 it.current_y = this_line_y;
9826 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
9827 display_line (&it);
9828
9829 /* If line contains point, is not continued,
9830 and ends at same distance from eob as before, we win */
9831 if (w->cursor.vpos >= 0
9832 /* Line is not continued, otherwise this_line_start_pos
9833 would have been set to 0 in display_line. */
9834 && CHARPOS (this_line_start_pos)
9835 /* Line ends as before. */
9836 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
9837 /* Line has same height as before. Otherwise other lines
9838 would have to be shifted up or down. */
9839 && this_line_pixel_height == line_height_before)
9840 {
9841 /* If this is not the window's last line, we must adjust
9842 the charstarts of the lines below. */
9843 if (it.current_y < it.last_visible_y)
9844 {
9845 struct glyph_row *row
9846 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
9847 int delta, delta_bytes;
9848
9849 if (Z - CHARPOS (tlendpos) == ZV)
9850 {
9851 /* This line ends at end of (accessible part of)
9852 buffer. There is no newline to count. */
9853 delta = (Z
9854 - CHARPOS (tlendpos)
9855 - MATRIX_ROW_START_CHARPOS (row));
9856 delta_bytes = (Z_BYTE
9857 - BYTEPOS (tlendpos)
9858 - MATRIX_ROW_START_BYTEPOS (row));
9859 }
9860 else
9861 {
9862 /* This line ends in a newline. Must take
9863 account of the newline and the rest of the
9864 text that follows. */
9865 delta = (Z
9866 - CHARPOS (tlendpos)
9867 - MATRIX_ROW_START_CHARPOS (row));
9868 delta_bytes = (Z_BYTE
9869 - BYTEPOS (tlendpos)
9870 - MATRIX_ROW_START_BYTEPOS (row));
9871 }
9872
9873 increment_matrix_positions (w->current_matrix,
9874 this_line_vpos + 1,
9875 w->current_matrix->nrows,
9876 delta, delta_bytes);
9877 }
9878
9879 /* If this row displays text now but previously didn't,
9880 or vice versa, w->window_end_vpos may have to be
9881 adjusted. */
9882 if ((it.glyph_row - 1)->displays_text_p)
9883 {
9884 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
9885 XSETINT (w->window_end_vpos, this_line_vpos);
9886 }
9887 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
9888 && this_line_vpos > 0)
9889 XSETINT (w->window_end_vpos, this_line_vpos - 1);
9890 w->window_end_valid = Qnil;
9891
9892 /* Update hint: No need to try to scroll in update_window. */
9893 w->desired_matrix->no_scrolling_p = 1;
9894
9895 #if GLYPH_DEBUG
9896 *w->desired_matrix->method = 0;
9897 debug_method_add (w, "optimization 1");
9898 #endif
9899 goto update;
9900 }
9901 else
9902 goto cancel;
9903 }
9904 else if (/* Cursor position hasn't changed. */
9905 PT == XFASTINT (w->last_point)
9906 /* Make sure the cursor was last displayed
9907 in this window. Otherwise we have to reposition it. */
9908 && 0 <= w->cursor.vpos
9909 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
9910 {
9911 if (!must_finish)
9912 {
9913 do_pending_window_change (1);
9914
9915 /* We used to always goto end_of_redisplay here, but this
9916 isn't enough if we have a blinking cursor. */
9917 if (w->cursor_off_p == w->last_cursor_off_p)
9918 goto end_of_redisplay;
9919 }
9920 goto update;
9921 }
9922 /* If highlighting the region, or if the cursor is in the echo area,
9923 then we can't just move the cursor. */
9924 else if (! (!NILP (Vtransient_mark_mode)
9925 && !NILP (current_buffer->mark_active))
9926 && (EQ (selected_window, current_buffer->last_selected_window)
9927 || highlight_nonselected_windows)
9928 && NILP (w->region_showing)
9929 && NILP (Vshow_trailing_whitespace)
9930 && !cursor_in_echo_area)
9931 {
9932 struct it it;
9933 struct glyph_row *row;
9934
9935 /* Skip from tlbufpos to PT and see where it is. Note that
9936 PT may be in invisible text. If so, we will end at the
9937 next visible position. */
9938 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
9939 NULL, DEFAULT_FACE_ID);
9940 it.current_x = this_line_start_x;
9941 it.current_y = this_line_y;
9942 it.vpos = this_line_vpos;
9943
9944 /* The call to move_it_to stops in front of PT, but
9945 moves over before-strings. */
9946 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
9947
9948 if (it.vpos == this_line_vpos
9949 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
9950 row->enabled_p))
9951 {
9952 xassert (this_line_vpos == it.vpos);
9953 xassert (this_line_y == it.current_y);
9954 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9955 #if GLYPH_DEBUG
9956 *w->desired_matrix->method = 0;
9957 debug_method_add (w, "optimization 3");
9958 #endif
9959 goto update;
9960 }
9961 else
9962 goto cancel;
9963 }
9964
9965 cancel:
9966 /* Text changed drastically or point moved off of line. */
9967 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
9968 }
9969
9970 CHARPOS (this_line_start_pos) = 0;
9971 consider_all_windows_p |= buffer_shared > 1;
9972 ++clear_face_cache_count;
9973
9974
9975 /* Build desired matrices, and update the display. If
9976 consider_all_windows_p is non-zero, do it for all windows on all
9977 frames. Otherwise do it for selected_window, only. */
9978
9979 if (consider_all_windows_p)
9980 {
9981 Lisp_Object tail, frame;
9982 int i, n = 0, size = 50;
9983 struct frame **updated
9984 = (struct frame **) alloca (size * sizeof *updated);
9985
9986 /* Clear the face cache eventually. */
9987 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
9988 {
9989 clear_face_cache (0);
9990 clear_face_cache_count = 0;
9991 }
9992
9993 /* Recompute # windows showing selected buffer. This will be
9994 incremented each time such a window is displayed. */
9995 buffer_shared = 0;
9996
9997 FOR_EACH_FRAME (tail, frame)
9998 {
9999 struct frame *f = XFRAME (frame);
10000
10001 if (FRAME_WINDOW_P (f) || f == sf)
10002 {
10003 #ifdef HAVE_WINDOW_SYSTEM
10004 if (clear_face_cache_count % 50 == 0
10005 && FRAME_WINDOW_P (f))
10006 clear_image_cache (f, 0);
10007 #endif /* HAVE_WINDOW_SYSTEM */
10008
10009 /* Mark all the scroll bars to be removed; we'll redeem
10010 the ones we want when we redisplay their windows. */
10011 if (condemn_scroll_bars_hook)
10012 condemn_scroll_bars_hook (f);
10013
10014 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10015 redisplay_windows (FRAME_ROOT_WINDOW (f));
10016
10017 /* Any scroll bars which redisplay_windows should have
10018 nuked should now go away. */
10019 if (judge_scroll_bars_hook)
10020 judge_scroll_bars_hook (f);
10021
10022 /* If fonts changed, display again. */
10023 /* ??? rms: I suspect it is a mistake to jump all the way
10024 back to retry here. It should just retry this frame. */
10025 if (fonts_changed_p)
10026 goto retry;
10027
10028 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10029 {
10030 /* See if we have to hscroll. */
10031 if (hscroll_windows (f->root_window))
10032 goto retry;
10033
10034 /* Prevent various kinds of signals during display
10035 update. stdio is not robust about handling
10036 signals, which can cause an apparent I/O
10037 error. */
10038 if (interrupt_input)
10039 unrequest_sigio ();
10040 STOP_POLLING;
10041
10042 /* Update the display. */
10043 set_window_update_flags (XWINDOW (f->root_window), 1);
10044 pause |= update_frame (f, 0, 0);
10045 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10046 if (pause)
10047 break;
10048 #endif
10049
10050 if (n == size)
10051 {
10052 int nbytes = size * sizeof *updated;
10053 struct frame **p = (struct frame **) alloca (2 * nbytes);
10054 bcopy (updated, p, nbytes);
10055 size *= 2;
10056 }
10057
10058 updated[n++] = f;
10059 }
10060 }
10061 }
10062
10063 /* Do the mark_window_display_accurate after all windows have
10064 been redisplayed because this call resets flags in buffers
10065 which are needed for proper redisplay. */
10066 for (i = 0; i < n; ++i)
10067 {
10068 struct frame *f = updated[i];
10069 mark_window_display_accurate (f->root_window, 1);
10070 if (frame_up_to_date_hook)
10071 frame_up_to_date_hook (f);
10072 }
10073 }
10074 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10075 {
10076 Lisp_Object mini_window;
10077 struct frame *mini_frame;
10078
10079 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10080 /* Use list_of_error, not Qerror, so that
10081 we catch only errors and don't run the debugger. */
10082 internal_condition_case_1 (redisplay_window_1, selected_window,
10083 list_of_error,
10084 redisplay_window_error);
10085
10086 /* Compare desired and current matrices, perform output. */
10087
10088 update:
10089 /* If fonts changed, display again. */
10090 if (fonts_changed_p)
10091 goto retry;
10092
10093 /* Prevent various kinds of signals during display update.
10094 stdio is not robust about handling signals,
10095 which can cause an apparent I/O error. */
10096 if (interrupt_input)
10097 unrequest_sigio ();
10098 STOP_POLLING;
10099
10100 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10101 {
10102 if (hscroll_windows (selected_window))
10103 goto retry;
10104
10105 XWINDOW (selected_window)->must_be_updated_p = 1;
10106 pause = update_frame (sf, 0, 0);
10107 }
10108
10109 /* We may have called echo_area_display at the top of this
10110 function. If the echo area is on another frame, that may
10111 have put text on a frame other than the selected one, so the
10112 above call to update_frame would not have caught it. Catch
10113 it here. */
10114 mini_window = FRAME_MINIBUF_WINDOW (sf);
10115 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10116
10117 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10118 {
10119 XWINDOW (mini_window)->must_be_updated_p = 1;
10120 pause |= update_frame (mini_frame, 0, 0);
10121 if (!pause && hscroll_windows (mini_window))
10122 goto retry;
10123 }
10124 }
10125
10126 /* If display was paused because of pending input, make sure we do a
10127 thorough update the next time. */
10128 if (pause)
10129 {
10130 /* Prevent the optimization at the beginning of
10131 redisplay_internal that tries a single-line update of the
10132 line containing the cursor in the selected window. */
10133 CHARPOS (this_line_start_pos) = 0;
10134
10135 /* Let the overlay arrow be updated the next time. */
10136 if (!NILP (last_arrow_position))
10137 {
10138 last_arrow_position = Qt;
10139 last_arrow_string = Qt;
10140 }
10141
10142 /* If we pause after scrolling, some rows in the current
10143 matrices of some windows are not valid. */
10144 if (!WINDOW_FULL_WIDTH_P (w)
10145 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10146 update_mode_lines = 1;
10147 }
10148 else
10149 {
10150 if (!consider_all_windows_p)
10151 {
10152 /* This has already been done above if
10153 consider_all_windows_p is set. */
10154 mark_window_display_accurate_1 (w, 1);
10155
10156 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
10157 last_arrow_string = Voverlay_arrow_string;
10158
10159 if (frame_up_to_date_hook != 0)
10160 frame_up_to_date_hook (sf);
10161 }
10162
10163 update_mode_lines = 0;
10164 windows_or_buffers_changed = 0;
10165 cursor_type_changed = 0;
10166 }
10167
10168 /* Start SIGIO interrupts coming again. Having them off during the
10169 code above makes it less likely one will discard output, but not
10170 impossible, since there might be stuff in the system buffer here.
10171 But it is much hairier to try to do anything about that. */
10172 if (interrupt_input)
10173 request_sigio ();
10174 RESUME_POLLING;
10175
10176 /* If a frame has become visible which was not before, redisplay
10177 again, so that we display it. Expose events for such a frame
10178 (which it gets when becoming visible) don't call the parts of
10179 redisplay constructing glyphs, so simply exposing a frame won't
10180 display anything in this case. So, we have to display these
10181 frames here explicitly. */
10182 if (!pause)
10183 {
10184 Lisp_Object tail, frame;
10185 int new_count = 0;
10186
10187 FOR_EACH_FRAME (tail, frame)
10188 {
10189 int this_is_visible = 0;
10190
10191 if (XFRAME (frame)->visible)
10192 this_is_visible = 1;
10193 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10194 if (XFRAME (frame)->visible)
10195 this_is_visible = 1;
10196
10197 if (this_is_visible)
10198 new_count++;
10199 }
10200
10201 if (new_count != number_of_visible_frames)
10202 windows_or_buffers_changed++;
10203 }
10204
10205 /* Change frame size now if a change is pending. */
10206 do_pending_window_change (1);
10207
10208 /* If we just did a pending size change, or have additional
10209 visible frames, redisplay again. */
10210 if (windows_or_buffers_changed && !pause)
10211 goto retry;
10212
10213 end_of_redisplay:
10214 unbind_to (count, Qnil);
10215 RESUME_POLLING;
10216 }
10217
10218
10219 /* Redisplay, but leave alone any recent echo area message unless
10220 another message has been requested in its place.
10221
10222 This is useful in situations where you need to redisplay but no
10223 user action has occurred, making it inappropriate for the message
10224 area to be cleared. See tracking_off and
10225 wait_reading_process_input for examples of these situations.
10226
10227 FROM_WHERE is an integer saying from where this function was
10228 called. This is useful for debugging. */
10229
10230 void
10231 redisplay_preserve_echo_area (from_where)
10232 int from_where;
10233 {
10234 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10235
10236 if (!NILP (echo_area_buffer[1]))
10237 {
10238 /* We have a previously displayed message, but no current
10239 message. Redisplay the previous message. */
10240 display_last_displayed_message_p = 1;
10241 redisplay_internal (1);
10242 display_last_displayed_message_p = 0;
10243 }
10244 else
10245 redisplay_internal (1);
10246 }
10247
10248
10249 /* Function registered with record_unwind_protect in
10250 redisplay_internal. Reset redisplaying_p to the value it had
10251 before redisplay_internal was called, and clear
10252 prevent_freeing_realized_faces_p. */
10253
10254 static Lisp_Object
10255 unwind_redisplay (old_redisplaying_p)
10256 Lisp_Object old_redisplaying_p;
10257 {
10258 redisplaying_p = XFASTINT (old_redisplaying_p);
10259 return Qnil;
10260 }
10261
10262
10263 /* Mark the display of window W as accurate or inaccurate. If
10264 ACCURATE_P is non-zero mark display of W as accurate. If
10265 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10266 redisplay_internal is called. */
10267
10268 static void
10269 mark_window_display_accurate_1 (w, accurate_p)
10270 struct window *w;
10271 int accurate_p;
10272 {
10273 if (BUFFERP (w->buffer))
10274 {
10275 struct buffer *b = XBUFFER (w->buffer);
10276
10277 w->last_modified
10278 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10279 w->last_overlay_modified
10280 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10281 w->last_had_star
10282 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10283
10284 if (accurate_p)
10285 {
10286 b->clip_changed = 0;
10287 b->prevent_redisplay_optimizations_p = 0;
10288
10289 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10290 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10291 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10292 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10293
10294 w->current_matrix->buffer = b;
10295 w->current_matrix->begv = BUF_BEGV (b);
10296 w->current_matrix->zv = BUF_ZV (b);
10297
10298 w->last_cursor = w->cursor;
10299 w->last_cursor_off_p = w->cursor_off_p;
10300
10301 if (w == XWINDOW (selected_window))
10302 w->last_point = make_number (BUF_PT (b));
10303 else
10304 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10305 }
10306 }
10307
10308 if (accurate_p)
10309 {
10310 w->window_end_valid = w->buffer;
10311 #if 0 /* This is incorrect with variable-height lines. */
10312 xassert (XINT (w->window_end_vpos)
10313 < (WINDOW_TOTAL_LINES (w)
10314 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10315 #endif
10316 w->update_mode_line = Qnil;
10317 }
10318 }
10319
10320
10321 /* Mark the display of windows in the window tree rooted at WINDOW as
10322 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10323 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10324 be redisplayed the next time redisplay_internal is called. */
10325
10326 void
10327 mark_window_display_accurate (window, accurate_p)
10328 Lisp_Object window;
10329 int accurate_p;
10330 {
10331 struct window *w;
10332
10333 for (; !NILP (window); window = w->next)
10334 {
10335 w = XWINDOW (window);
10336 mark_window_display_accurate_1 (w, accurate_p);
10337
10338 if (!NILP (w->vchild))
10339 mark_window_display_accurate (w->vchild, accurate_p);
10340 if (!NILP (w->hchild))
10341 mark_window_display_accurate (w->hchild, accurate_p);
10342 }
10343
10344 if (accurate_p)
10345 {
10346 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
10347 last_arrow_string = Voverlay_arrow_string;
10348 }
10349 else
10350 {
10351 /* Force a thorough redisplay the next time by setting
10352 last_arrow_position and last_arrow_string to t, which is
10353 unequal to any useful value of Voverlay_arrow_... */
10354 last_arrow_position = Qt;
10355 last_arrow_string = Qt;
10356 }
10357 }
10358
10359
10360 /* Return value in display table DP (Lisp_Char_Table *) for character
10361 C. Since a display table doesn't have any parent, we don't have to
10362 follow parent. Do not call this function directly but use the
10363 macro DISP_CHAR_VECTOR. */
10364
10365 Lisp_Object
10366 disp_char_vector (dp, c)
10367 struct Lisp_Char_Table *dp;
10368 int c;
10369 {
10370 int code[4], i;
10371 Lisp_Object val;
10372
10373 if (SINGLE_BYTE_CHAR_P (c))
10374 return (dp->contents[c]);
10375
10376 SPLIT_CHAR (c, code[0], code[1], code[2]);
10377 if (code[1] < 32)
10378 code[1] = -1;
10379 else if (code[2] < 32)
10380 code[2] = -1;
10381
10382 /* Here, the possible range of code[0] (== charset ID) is
10383 128..max_charset. Since the top level char table contains data
10384 for multibyte characters after 256th element, we must increment
10385 code[0] by 128 to get a correct index. */
10386 code[0] += 128;
10387 code[3] = -1; /* anchor */
10388
10389 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10390 {
10391 val = dp->contents[code[i]];
10392 if (!SUB_CHAR_TABLE_P (val))
10393 return (NILP (val) ? dp->defalt : val);
10394 }
10395
10396 /* Here, val is a sub char table. We return the default value of
10397 it. */
10398 return (dp->defalt);
10399 }
10400
10401
10402 \f
10403 /***********************************************************************
10404 Window Redisplay
10405 ***********************************************************************/
10406
10407 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10408
10409 static void
10410 redisplay_windows (window)
10411 Lisp_Object window;
10412 {
10413 while (!NILP (window))
10414 {
10415 struct window *w = XWINDOW (window);
10416
10417 if (!NILP (w->hchild))
10418 redisplay_windows (w->hchild);
10419 else if (!NILP (w->vchild))
10420 redisplay_windows (w->vchild);
10421 else
10422 {
10423 displayed_buffer = XBUFFER (w->buffer);
10424 /* Use list_of_error, not Qerror, so that
10425 we catch only errors and don't run the debugger. */
10426 internal_condition_case_1 (redisplay_window_0, window,
10427 list_of_error,
10428 redisplay_window_error);
10429 }
10430
10431 window = w->next;
10432 }
10433 }
10434
10435 static Lisp_Object
10436 redisplay_window_error ()
10437 {
10438 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10439 return Qnil;
10440 }
10441
10442 static Lisp_Object
10443 redisplay_window_0 (window)
10444 Lisp_Object window;
10445 {
10446 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10447 redisplay_window (window, 0);
10448 return Qnil;
10449 }
10450
10451 static Lisp_Object
10452 redisplay_window_1 (window)
10453 Lisp_Object window;
10454 {
10455 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10456 redisplay_window (window, 1);
10457 return Qnil;
10458 }
10459 \f
10460
10461 /* Increment GLYPH until it reaches END or CONDITION fails while
10462 adding (GLYPH)->pixel_width to X. */
10463
10464 #define SKIP_GLYPHS(glyph, end, x, condition) \
10465 do \
10466 { \
10467 (x) += (glyph)->pixel_width; \
10468 ++(glyph); \
10469 } \
10470 while ((glyph) < (end) && (condition))
10471
10472
10473 /* Set cursor position of W. PT is assumed to be displayed in ROW.
10474 DELTA is the number of bytes by which positions recorded in ROW
10475 differ from current buffer positions. */
10476
10477 void
10478 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10479 struct window *w;
10480 struct glyph_row *row;
10481 struct glyph_matrix *matrix;
10482 int delta, delta_bytes, dy, dvpos;
10483 {
10484 struct glyph *glyph = row->glyphs[TEXT_AREA];
10485 struct glyph *end = glyph + row->used[TEXT_AREA];
10486 /* The first glyph that starts a sequence of glyphs from string. */
10487 struct glyph *string_start;
10488 /* The X coordinate of string_start. */
10489 int string_start_x;
10490 /* The last known character position. */
10491 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10492 /* The last known character position before string_start. */
10493 int string_before_pos;
10494 int x = row->x;
10495 int pt_old = PT - delta;
10496
10497 /* Skip over glyphs not having an object at the start of the row.
10498 These are special glyphs like truncation marks on terminal
10499 frames. */
10500 if (row->displays_text_p)
10501 while (glyph < end
10502 && INTEGERP (glyph->object)
10503 && glyph->charpos < 0)
10504 {
10505 x += glyph->pixel_width;
10506 ++glyph;
10507 }
10508
10509 string_start = NULL;
10510 while (glyph < end
10511 && !INTEGERP (glyph->object)
10512 && (!BUFFERP (glyph->object)
10513 || (last_pos = glyph->charpos) < pt_old))
10514 {
10515 if (! STRINGP (glyph->object))
10516 {
10517 string_start = NULL;
10518 x += glyph->pixel_width;
10519 ++glyph;
10520 }
10521 else
10522 {
10523 string_before_pos = last_pos;
10524 string_start = glyph;
10525 string_start_x = x;
10526 /* Skip all glyphs from string. */
10527 SKIP_GLYPHS (glyph, end, x, STRINGP (glyph->object));
10528 }
10529 }
10530
10531 if (string_start
10532 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
10533 {
10534 /* We may have skipped over point because the previous glyphs
10535 are from string. As there's no easy way to know the
10536 character position of the current glyph, find the correct
10537 glyph on point by scanning from string_start again. */
10538 Lisp_Object limit;
10539 Lisp_Object string;
10540 int pos;
10541
10542 limit = make_number (pt_old + 1);
10543 end = glyph;
10544 glyph = string_start;
10545 x = string_start_x;
10546 string = glyph->object;
10547 pos = string_buffer_position (w, string, string_before_pos);
10548 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
10549 because we always put cursor after overlay strings. */
10550 while (pos == 0 && glyph < end)
10551 {
10552 string = glyph->object;
10553 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10554 if (glyph < end)
10555 pos = string_buffer_position (w, glyph->object, string_before_pos);
10556 }
10557
10558 while (glyph < end)
10559 {
10560 pos = XINT (Fnext_single_char_property_change
10561 (make_number (pos), Qdisplay, Qnil, limit));
10562 if (pos > pt_old)
10563 break;
10564 /* Skip glyphs from the same string. */
10565 string = glyph->object;
10566 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10567 /* Skip glyphs from an overlay. */
10568 while (glyph < end
10569 && ! string_buffer_position (w, glyph->object, pos))
10570 {
10571 string = glyph->object;
10572 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10573 }
10574 }
10575 }
10576
10577 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
10578 w->cursor.x = x;
10579 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
10580 w->cursor.y = row->y + dy;
10581
10582 if (w == XWINDOW (selected_window))
10583 {
10584 if (!row->continued_p
10585 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
10586 && row->x == 0)
10587 {
10588 this_line_buffer = XBUFFER (w->buffer);
10589
10590 CHARPOS (this_line_start_pos)
10591 = MATRIX_ROW_START_CHARPOS (row) + delta;
10592 BYTEPOS (this_line_start_pos)
10593 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
10594
10595 CHARPOS (this_line_end_pos)
10596 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
10597 BYTEPOS (this_line_end_pos)
10598 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
10599
10600 this_line_y = w->cursor.y;
10601 this_line_pixel_height = row->height;
10602 this_line_vpos = w->cursor.vpos;
10603 this_line_start_x = row->x;
10604 }
10605 else
10606 CHARPOS (this_line_start_pos) = 0;
10607 }
10608 }
10609
10610
10611 /* Run window scroll functions, if any, for WINDOW with new window
10612 start STARTP. Sets the window start of WINDOW to that position.
10613
10614 We assume that the window's buffer is really current. */
10615
10616 static INLINE struct text_pos
10617 run_window_scroll_functions (window, startp)
10618 Lisp_Object window;
10619 struct text_pos startp;
10620 {
10621 struct window *w = XWINDOW (window);
10622 SET_MARKER_FROM_TEXT_POS (w->start, startp);
10623
10624 if (current_buffer != XBUFFER (w->buffer))
10625 abort ();
10626
10627 if (!NILP (Vwindow_scroll_functions))
10628 {
10629 run_hook_with_args_2 (Qwindow_scroll_functions, window,
10630 make_number (CHARPOS (startp)));
10631 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10632 /* In case the hook functions switch buffers. */
10633 if (current_buffer != XBUFFER (w->buffer))
10634 set_buffer_internal_1 (XBUFFER (w->buffer));
10635 }
10636
10637 return startp;
10638 }
10639
10640
10641 /* Make sure the line containing the cursor is fully visible.
10642 A value of 1 means there is nothing to be done.
10643 (Either the line is fully visible, or it cannot be made so,
10644 or we cannot tell.)
10645 A value of 0 means the caller should do scrolling
10646 as if point had gone off the screen. */
10647
10648 static int
10649 make_cursor_line_fully_visible (w)
10650 struct window *w;
10651 {
10652 struct glyph_matrix *matrix;
10653 struct glyph_row *row;
10654 int window_height;
10655
10656 /* It's not always possible to find the cursor, e.g, when a window
10657 is full of overlay strings. Don't do anything in that case. */
10658 if (w->cursor.vpos < 0)
10659 return 1;
10660
10661 matrix = w->desired_matrix;
10662 row = MATRIX_ROW (matrix, w->cursor.vpos);
10663
10664 /* If the cursor row is not partially visible, there's nothing to do. */
10665 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
10666 return 1;
10667
10668 /* If the row the cursor is in is taller than the window's height,
10669 it's not clear what to do, so do nothing. */
10670 window_height = window_box_height (w);
10671 if (row->height >= window_height)
10672 return 1;
10673
10674 return 0;
10675
10676 #if 0
10677 /* This code used to try to scroll the window just enough to make
10678 the line visible. It returned 0 to say that the caller should
10679 allocate larger glyph matrices. */
10680
10681 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
10682 {
10683 int dy = row->height - row->visible_height;
10684 w->vscroll = 0;
10685 w->cursor.y += dy;
10686 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10687 }
10688 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
10689 {
10690 int dy = - (row->height - row->visible_height);
10691 w->vscroll = dy;
10692 w->cursor.y += dy;
10693 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10694 }
10695
10696 /* When we change the cursor y-position of the selected window,
10697 change this_line_y as well so that the display optimization for
10698 the cursor line of the selected window in redisplay_internal uses
10699 the correct y-position. */
10700 if (w == XWINDOW (selected_window))
10701 this_line_y = w->cursor.y;
10702
10703 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
10704 redisplay with larger matrices. */
10705 if (matrix->nrows < required_matrix_height (w))
10706 {
10707 fonts_changed_p = 1;
10708 return 0;
10709 }
10710
10711 return 1;
10712 #endif /* 0 */
10713 }
10714
10715
10716 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
10717 non-zero means only WINDOW is redisplayed in redisplay_internal.
10718 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
10719 in redisplay_window to bring a partially visible line into view in
10720 the case that only the cursor has moved.
10721
10722 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
10723 last screen line's vertical height extends past the end of the screen.
10724
10725 Value is
10726
10727 1 if scrolling succeeded
10728
10729 0 if scrolling didn't find point.
10730
10731 -1 if new fonts have been loaded so that we must interrupt
10732 redisplay, adjust glyph matrices, and try again. */
10733
10734 enum
10735 {
10736 SCROLLING_SUCCESS,
10737 SCROLLING_FAILED,
10738 SCROLLING_NEED_LARGER_MATRICES
10739 };
10740
10741 static int
10742 try_scrolling (window, just_this_one_p, scroll_conservatively,
10743 scroll_step, temp_scroll_step, last_line_misfit)
10744 Lisp_Object window;
10745 int just_this_one_p;
10746 EMACS_INT scroll_conservatively, scroll_step;
10747 int temp_scroll_step;
10748 int last_line_misfit;
10749 {
10750 struct window *w = XWINDOW (window);
10751 struct frame *f = XFRAME (w->frame);
10752 struct text_pos scroll_margin_pos;
10753 struct text_pos pos;
10754 struct text_pos startp;
10755 struct it it;
10756 Lisp_Object window_end;
10757 int this_scroll_margin;
10758 int dy = 0;
10759 int scroll_max;
10760 int rc;
10761 int amount_to_scroll = 0;
10762 Lisp_Object aggressive;
10763 int height;
10764 int end_scroll_margin;
10765
10766 #if GLYPH_DEBUG
10767 debug_method_add (w, "try_scrolling");
10768 #endif
10769
10770 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10771
10772 /* Compute scroll margin height in pixels. We scroll when point is
10773 within this distance from the top or bottom of the window. */
10774 if (scroll_margin > 0)
10775 {
10776 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
10777 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
10778 }
10779 else
10780 this_scroll_margin = 0;
10781
10782 /* Compute how much we should try to scroll maximally to bring point
10783 into view. */
10784 if (scroll_step || scroll_conservatively || temp_scroll_step)
10785 scroll_max = max (scroll_step,
10786 max (scroll_conservatively, temp_scroll_step));
10787 else if (NUMBERP (current_buffer->scroll_down_aggressively)
10788 || NUMBERP (current_buffer->scroll_up_aggressively))
10789 /* We're trying to scroll because of aggressive scrolling
10790 but no scroll_step is set. Choose an arbitrary one. Maybe
10791 there should be a variable for this. */
10792 scroll_max = 10;
10793 else
10794 scroll_max = 0;
10795 scroll_max *= FRAME_LINE_HEIGHT (f);
10796
10797 /* Decide whether we have to scroll down. Start at the window end
10798 and move this_scroll_margin up to find the position of the scroll
10799 margin. */
10800 window_end = Fwindow_end (window, Qt);
10801
10802 too_near_end:
10803
10804 CHARPOS (scroll_margin_pos) = XINT (window_end);
10805 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
10806
10807 end_scroll_margin = this_scroll_margin + !!last_line_misfit;
10808 if (end_scroll_margin)
10809 {
10810 start_display (&it, w, scroll_margin_pos);
10811 move_it_vertically (&it, - end_scroll_margin);
10812 scroll_margin_pos = it.current.pos;
10813 }
10814
10815 if (PT >= CHARPOS (scroll_margin_pos))
10816 {
10817 int y0;
10818
10819 /* Point is in the scroll margin at the bottom of the window, or
10820 below. Compute a new window start that makes point visible. */
10821
10822 /* Compute the distance from the scroll margin to PT.
10823 Give up if the distance is greater than scroll_max. */
10824 start_display (&it, w, scroll_margin_pos);
10825 y0 = it.current_y;
10826 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10827 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10828
10829 /* To make point visible, we have to move the window start
10830 down so that the line the cursor is in is visible, which
10831 means we have to add in the height of the cursor line. */
10832 dy = line_bottom_y (&it) - y0;
10833
10834 if (dy > scroll_max)
10835 return SCROLLING_FAILED;
10836
10837 /* Move the window start down. If scrolling conservatively,
10838 move it just enough down to make point visible. If
10839 scroll_step is set, move it down by scroll_step. */
10840 start_display (&it, w, startp);
10841
10842 if (scroll_conservatively)
10843 /* Set AMOUNT_TO_SCROLL to at least one line,
10844 and at most scroll_conservatively lines. */
10845 amount_to_scroll
10846 = min (max (dy, FRAME_LINE_HEIGHT (f)),
10847 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
10848 else if (scroll_step || temp_scroll_step)
10849 amount_to_scroll = scroll_max;
10850 else
10851 {
10852 aggressive = current_buffer->scroll_up_aggressively;
10853 height = WINDOW_BOX_TEXT_HEIGHT (w);
10854 if (NUMBERP (aggressive))
10855 amount_to_scroll = XFLOATINT (aggressive) * height;
10856 }
10857
10858 if (amount_to_scroll <= 0)
10859 return SCROLLING_FAILED;
10860
10861 /* If moving by amount_to_scroll leaves STARTP unchanged,
10862 move it down one screen line. */
10863
10864 move_it_vertically (&it, amount_to_scroll);
10865 if (CHARPOS (it.current.pos) == CHARPOS (startp))
10866 move_it_by_lines (&it, 1, 1);
10867 startp = it.current.pos;
10868 }
10869 else
10870 {
10871 /* See if point is inside the scroll margin at the top of the
10872 window. */
10873 scroll_margin_pos = startp;
10874 if (this_scroll_margin)
10875 {
10876 start_display (&it, w, startp);
10877 move_it_vertically (&it, this_scroll_margin);
10878 scroll_margin_pos = it.current.pos;
10879 }
10880
10881 if (PT < CHARPOS (scroll_margin_pos))
10882 {
10883 /* Point is in the scroll margin at the top of the window or
10884 above what is displayed in the window. */
10885 int y0;
10886
10887 /* Compute the vertical distance from PT to the scroll
10888 margin position. Give up if distance is greater than
10889 scroll_max. */
10890 SET_TEXT_POS (pos, PT, PT_BYTE);
10891 start_display (&it, w, pos);
10892 y0 = it.current_y;
10893 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
10894 it.last_visible_y, -1,
10895 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10896 dy = it.current_y - y0;
10897 if (dy > scroll_max)
10898 return SCROLLING_FAILED;
10899
10900 /* Compute new window start. */
10901 start_display (&it, w, startp);
10902
10903 if (scroll_conservatively)
10904 amount_to_scroll =
10905 max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
10906 else if (scroll_step || temp_scroll_step)
10907 amount_to_scroll = scroll_max;
10908 else
10909 {
10910 aggressive = current_buffer->scroll_down_aggressively;
10911 height = WINDOW_BOX_TEXT_HEIGHT (w);
10912 if (NUMBERP (aggressive))
10913 amount_to_scroll = XFLOATINT (aggressive) * height;
10914 }
10915
10916 if (amount_to_scroll <= 0)
10917 return SCROLLING_FAILED;
10918
10919 move_it_vertically (&it, - amount_to_scroll);
10920 startp = it.current.pos;
10921 }
10922 }
10923
10924 /* Run window scroll functions. */
10925 startp = run_window_scroll_functions (window, startp);
10926
10927 /* Display the window. Give up if new fonts are loaded, or if point
10928 doesn't appear. */
10929 if (!try_window (window, startp))
10930 rc = SCROLLING_NEED_LARGER_MATRICES;
10931 else if (w->cursor.vpos < 0)
10932 {
10933 clear_glyph_matrix (w->desired_matrix);
10934 rc = SCROLLING_FAILED;
10935 }
10936 else
10937 {
10938 /* Maybe forget recorded base line for line number display. */
10939 if (!just_this_one_p
10940 || current_buffer->clip_changed
10941 || BEG_UNCHANGED < CHARPOS (startp))
10942 w->base_line_number = Qnil;
10943
10944 /* If cursor ends up on a partially visible line,
10945 treat that as being off the bottom of the screen. */
10946 if (! make_cursor_line_fully_visible (w))
10947 {
10948 clear_glyph_matrix (w->desired_matrix);
10949 last_line_misfit = 1;
10950 goto too_near_end;
10951 }
10952 rc = SCROLLING_SUCCESS;
10953 }
10954
10955 return rc;
10956 }
10957
10958
10959 /* Compute a suitable window start for window W if display of W starts
10960 on a continuation line. Value is non-zero if a new window start
10961 was computed.
10962
10963 The new window start will be computed, based on W's width, starting
10964 from the start of the continued line. It is the start of the
10965 screen line with the minimum distance from the old start W->start. */
10966
10967 static int
10968 compute_window_start_on_continuation_line (w)
10969 struct window *w;
10970 {
10971 struct text_pos pos, start_pos;
10972 int window_start_changed_p = 0;
10973
10974 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
10975
10976 /* If window start is on a continuation line... Window start may be
10977 < BEGV in case there's invisible text at the start of the
10978 buffer (M-x rmail, for example). */
10979 if (CHARPOS (start_pos) > BEGV
10980 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
10981 {
10982 struct it it;
10983 struct glyph_row *row;
10984
10985 /* Handle the case that the window start is out of range. */
10986 if (CHARPOS (start_pos) < BEGV)
10987 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
10988 else if (CHARPOS (start_pos) > ZV)
10989 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
10990
10991 /* Find the start of the continued line. This should be fast
10992 because scan_buffer is fast (newline cache). */
10993 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
10994 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
10995 row, DEFAULT_FACE_ID);
10996 reseat_at_previous_visible_line_start (&it);
10997
10998 /* If the line start is "too far" away from the window start,
10999 say it takes too much time to compute a new window start. */
11000 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11001 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11002 {
11003 int min_distance, distance;
11004
11005 /* Move forward by display lines to find the new window
11006 start. If window width was enlarged, the new start can
11007 be expected to be > the old start. If window width was
11008 decreased, the new window start will be < the old start.
11009 So, we're looking for the display line start with the
11010 minimum distance from the old window start. */
11011 pos = it.current.pos;
11012 min_distance = INFINITY;
11013 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11014 distance < min_distance)
11015 {
11016 min_distance = distance;
11017 pos = it.current.pos;
11018 move_it_by_lines (&it, 1, 0);
11019 }
11020
11021 /* Set the window start there. */
11022 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11023 window_start_changed_p = 1;
11024 }
11025 }
11026
11027 return window_start_changed_p;
11028 }
11029
11030
11031 /* Try cursor movement in case text has not changed in window WINDOW,
11032 with window start STARTP. Value is
11033
11034 CURSOR_MOVEMENT_SUCCESS if successful
11035
11036 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11037
11038 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11039 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11040 we want to scroll as if scroll-step were set to 1. See the code.
11041
11042 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11043 which case we have to abort this redisplay, and adjust matrices
11044 first. */
11045
11046 enum
11047 {
11048 CURSOR_MOVEMENT_SUCCESS,
11049 CURSOR_MOVEMENT_CANNOT_BE_USED,
11050 CURSOR_MOVEMENT_MUST_SCROLL,
11051 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11052 };
11053
11054 static int
11055 try_cursor_movement (window, startp, scroll_step)
11056 Lisp_Object window;
11057 struct text_pos startp;
11058 int *scroll_step;
11059 {
11060 struct window *w = XWINDOW (window);
11061 struct frame *f = XFRAME (w->frame);
11062 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11063
11064 #if GLYPH_DEBUG
11065 if (inhibit_try_cursor_movement)
11066 return rc;
11067 #endif
11068
11069 /* Handle case where text has not changed, only point, and it has
11070 not moved off the frame. */
11071 if (/* Point may be in this window. */
11072 PT >= CHARPOS (startp)
11073 /* Selective display hasn't changed. */
11074 && !current_buffer->clip_changed
11075 /* Function force-mode-line-update is used to force a thorough
11076 redisplay. It sets either windows_or_buffers_changed or
11077 update_mode_lines. So don't take a shortcut here for these
11078 cases. */
11079 && !update_mode_lines
11080 && !windows_or_buffers_changed
11081 && !cursor_type_changed
11082 /* Can't use this case if highlighting a region. When a
11083 region exists, cursor movement has to do more than just
11084 set the cursor. */
11085 && !(!NILP (Vtransient_mark_mode)
11086 && !NILP (current_buffer->mark_active))
11087 && NILP (w->region_showing)
11088 && NILP (Vshow_trailing_whitespace)
11089 /* Right after splitting windows, last_point may be nil. */
11090 && INTEGERP (w->last_point)
11091 /* This code is not used for mini-buffer for the sake of the case
11092 of redisplaying to replace an echo area message; since in
11093 that case the mini-buffer contents per se are usually
11094 unchanged. This code is of no real use in the mini-buffer
11095 since the handling of this_line_start_pos, etc., in redisplay
11096 handles the same cases. */
11097 && !EQ (window, minibuf_window)
11098 /* When splitting windows or for new windows, it happens that
11099 redisplay is called with a nil window_end_vpos or one being
11100 larger than the window. This should really be fixed in
11101 window.c. I don't have this on my list, now, so we do
11102 approximately the same as the old redisplay code. --gerd. */
11103 && INTEGERP (w->window_end_vpos)
11104 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11105 && (FRAME_WINDOW_P (f)
11106 || !MARKERP (Voverlay_arrow_position)
11107 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
11108 {
11109 int this_scroll_margin;
11110 struct glyph_row *row = NULL;
11111
11112 #if GLYPH_DEBUG
11113 debug_method_add (w, "cursor movement");
11114 #endif
11115
11116 /* Scroll if point within this distance from the top or bottom
11117 of the window. This is a pixel value. */
11118 this_scroll_margin = max (0, scroll_margin);
11119 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11120 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11121
11122 /* Start with the row the cursor was displayed during the last
11123 not paused redisplay. Give up if that row is not valid. */
11124 if (w->last_cursor.vpos < 0
11125 || w->last_cursor.vpos >= w->current_matrix->nrows)
11126 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11127 else
11128 {
11129 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11130 if (row->mode_line_p)
11131 ++row;
11132 if (!row->enabled_p)
11133 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11134 }
11135
11136 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11137 {
11138 int scroll_p = 0;
11139 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11140
11141 if (PT > XFASTINT (w->last_point))
11142 {
11143 /* Point has moved forward. */
11144 while (MATRIX_ROW_END_CHARPOS (row) < PT
11145 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11146 {
11147 xassert (row->enabled_p);
11148 ++row;
11149 }
11150
11151 /* The end position of a row equals the start position
11152 of the next row. If PT is there, we would rather
11153 display it in the next line. */
11154 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11155 && MATRIX_ROW_END_CHARPOS (row) == PT
11156 && !cursor_row_p (w, row))
11157 ++row;
11158
11159 /* If within the scroll margin, scroll. Note that
11160 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11161 the next line would be drawn, and that
11162 this_scroll_margin can be zero. */
11163 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11164 || PT > MATRIX_ROW_END_CHARPOS (row)
11165 /* Line is completely visible last line in window
11166 and PT is to be set in the next line. */
11167 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11168 && PT == MATRIX_ROW_END_CHARPOS (row)
11169 && !row->ends_at_zv_p
11170 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11171 scroll_p = 1;
11172 }
11173 else if (PT < XFASTINT (w->last_point))
11174 {
11175 /* Cursor has to be moved backward. Note that PT >=
11176 CHARPOS (startp) because of the outer
11177 if-statement. */
11178 while (!row->mode_line_p
11179 && (MATRIX_ROW_START_CHARPOS (row) > PT
11180 || (MATRIX_ROW_START_CHARPOS (row) == PT
11181 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11182 && (row->y > this_scroll_margin
11183 || CHARPOS (startp) == BEGV))
11184 {
11185 xassert (row->enabled_p);
11186 --row;
11187 }
11188
11189 /* Consider the following case: Window starts at BEGV,
11190 there is invisible, intangible text at BEGV, so that
11191 display starts at some point START > BEGV. It can
11192 happen that we are called with PT somewhere between
11193 BEGV and START. Try to handle that case. */
11194 if (row < w->current_matrix->rows
11195 || row->mode_line_p)
11196 {
11197 row = w->current_matrix->rows;
11198 if (row->mode_line_p)
11199 ++row;
11200 }
11201
11202 /* Due to newlines in overlay strings, we may have to
11203 skip forward over overlay strings. */
11204 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11205 && MATRIX_ROW_END_CHARPOS (row) == PT
11206 && !cursor_row_p (w, row))
11207 ++row;
11208
11209 /* If within the scroll margin, scroll. */
11210 if (row->y < this_scroll_margin
11211 && CHARPOS (startp) != BEGV)
11212 scroll_p = 1;
11213 }
11214
11215 if (PT < MATRIX_ROW_START_CHARPOS (row)
11216 || PT > MATRIX_ROW_END_CHARPOS (row))
11217 {
11218 /* if PT is not in the glyph row, give up. */
11219 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11220 }
11221 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
11222 {
11223 if (PT == MATRIX_ROW_END_CHARPOS (row)
11224 && !row->ends_at_zv_p
11225 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11226 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11227 else if (row->height > window_box_height (w))
11228 {
11229 /* If we end up in a partially visible line, let's
11230 make it fully visible, except when it's taller
11231 than the window, in which case we can't do much
11232 about it. */
11233 *scroll_step = 1;
11234 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11235 }
11236 else
11237 {
11238 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11239 if (!make_cursor_line_fully_visible (w))
11240 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11241 else
11242 rc = CURSOR_MOVEMENT_SUCCESS;
11243 }
11244 }
11245 else if (scroll_p)
11246 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11247 else
11248 {
11249 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11250 rc = CURSOR_MOVEMENT_SUCCESS;
11251 }
11252 }
11253 }
11254
11255 return rc;
11256 }
11257
11258 void
11259 set_vertical_scroll_bar (w)
11260 struct window *w;
11261 {
11262 int start, end, whole;
11263
11264 /* Calculate the start and end positions for the current window.
11265 At some point, it would be nice to choose between scrollbars
11266 which reflect the whole buffer size, with special markers
11267 indicating narrowing, and scrollbars which reflect only the
11268 visible region.
11269
11270 Note that mini-buffers sometimes aren't displaying any text. */
11271 if (!MINI_WINDOW_P (w)
11272 || (w == XWINDOW (minibuf_window)
11273 && NILP (echo_area_buffer[0])))
11274 {
11275 struct buffer *buf = XBUFFER (w->buffer);
11276 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11277 start = marker_position (w->start) - BUF_BEGV (buf);
11278 /* I don't think this is guaranteed to be right. For the
11279 moment, we'll pretend it is. */
11280 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11281
11282 if (end < start)
11283 end = start;
11284 if (whole < (end - start))
11285 whole = end - start;
11286 }
11287 else
11288 start = end = whole = 0;
11289
11290 /* Indicate what this scroll bar ought to be displaying now. */
11291 set_vertical_scroll_bar_hook (w, end - start, whole, start);
11292 }
11293
11294 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11295 selected_window is redisplayed.
11296
11297 We can return without actually redisplaying the window if
11298 fonts_changed_p is nonzero. In that case, redisplay_internal will
11299 retry. */
11300
11301 static void
11302 redisplay_window (window, just_this_one_p)
11303 Lisp_Object window;
11304 int just_this_one_p;
11305 {
11306 struct window *w = XWINDOW (window);
11307 struct frame *f = XFRAME (w->frame);
11308 struct buffer *buffer = XBUFFER (w->buffer);
11309 struct buffer *old = current_buffer;
11310 struct text_pos lpoint, opoint, startp;
11311 int update_mode_line;
11312 int tem;
11313 struct it it;
11314 /* Record it now because it's overwritten. */
11315 int current_matrix_up_to_date_p = 0;
11316 /* This is less strict than current_matrix_up_to_date_p.
11317 It indictes that the buffer contents and narrowing are unchanged. */
11318 int buffer_unchanged_p = 0;
11319 int temp_scroll_step = 0;
11320 int count = SPECPDL_INDEX ();
11321 int rc;
11322 int centering_position;
11323 int last_line_misfit = 0;
11324
11325 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11326 opoint = lpoint;
11327
11328 /* W must be a leaf window here. */
11329 xassert (!NILP (w->buffer));
11330 #if GLYPH_DEBUG
11331 *w->desired_matrix->method = 0;
11332 #endif
11333
11334 specbind (Qinhibit_point_motion_hooks, Qt);
11335
11336 reconsider_clip_changes (w, buffer);
11337
11338 /* Has the mode line to be updated? */
11339 update_mode_line = (!NILP (w->update_mode_line)
11340 || update_mode_lines
11341 || buffer->clip_changed
11342 || buffer->prevent_redisplay_optimizations_p);
11343
11344 if (MINI_WINDOW_P (w))
11345 {
11346 if (w == XWINDOW (echo_area_window)
11347 && !NILP (echo_area_buffer[0]))
11348 {
11349 if (update_mode_line)
11350 /* We may have to update a tty frame's menu bar or a
11351 tool-bar. Example `M-x C-h C-h C-g'. */
11352 goto finish_menu_bars;
11353 else
11354 /* We've already displayed the echo area glyphs in this window. */
11355 goto finish_scroll_bars;
11356 }
11357 else if ((w != XWINDOW (minibuf_window)
11358 || minibuf_level == 0)
11359 /* When buffer is nonempty, redisplay window normally. */
11360 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
11361 /* Quail displays non-mini buffers in minibuffer window.
11362 In that case, redisplay the window normally. */
11363 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
11364 {
11365 /* W is a mini-buffer window, but it's not active, so clear
11366 it. */
11367 int yb = window_text_bottom_y (w);
11368 struct glyph_row *row;
11369 int y;
11370
11371 for (y = 0, row = w->desired_matrix->rows;
11372 y < yb;
11373 y += row->height, ++row)
11374 blank_row (w, row, y);
11375 goto finish_scroll_bars;
11376 }
11377
11378 clear_glyph_matrix (w->desired_matrix);
11379 }
11380
11381 /* Otherwise set up data on this window; select its buffer and point
11382 value. */
11383 /* Really select the buffer, for the sake of buffer-local
11384 variables. */
11385 set_buffer_internal_1 (XBUFFER (w->buffer));
11386 SET_TEXT_POS (opoint, PT, PT_BYTE);
11387
11388 current_matrix_up_to_date_p
11389 = (!NILP (w->window_end_valid)
11390 && !current_buffer->clip_changed
11391 && !current_buffer->prevent_redisplay_optimizations_p
11392 && XFASTINT (w->last_modified) >= MODIFF
11393 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11394
11395 buffer_unchanged_p
11396 = (!NILP (w->window_end_valid)
11397 && !current_buffer->clip_changed
11398 && XFASTINT (w->last_modified) >= MODIFF
11399 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11400
11401 /* When windows_or_buffers_changed is non-zero, we can't rely on
11402 the window end being valid, so set it to nil there. */
11403 if (windows_or_buffers_changed)
11404 {
11405 /* If window starts on a continuation line, maybe adjust the
11406 window start in case the window's width changed. */
11407 if (XMARKER (w->start)->buffer == current_buffer)
11408 compute_window_start_on_continuation_line (w);
11409
11410 w->window_end_valid = Qnil;
11411 }
11412
11413 /* Some sanity checks. */
11414 CHECK_WINDOW_END (w);
11415 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
11416 abort ();
11417 if (BYTEPOS (opoint) < CHARPOS (opoint))
11418 abort ();
11419
11420 /* If %c is in mode line, update it if needed. */
11421 if (!NILP (w->column_number_displayed)
11422 /* This alternative quickly identifies a common case
11423 where no change is needed. */
11424 && !(PT == XFASTINT (w->last_point)
11425 && XFASTINT (w->last_modified) >= MODIFF
11426 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11427 && (XFASTINT (w->column_number_displayed)
11428 != (int) current_column ())) /* iftc */
11429 update_mode_line = 1;
11430
11431 /* Count number of windows showing the selected buffer. An indirect
11432 buffer counts as its base buffer. */
11433 if (!just_this_one_p)
11434 {
11435 struct buffer *current_base, *window_base;
11436 current_base = current_buffer;
11437 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11438 if (current_base->base_buffer)
11439 current_base = current_base->base_buffer;
11440 if (window_base->base_buffer)
11441 window_base = window_base->base_buffer;
11442 if (current_base == window_base)
11443 buffer_shared++;
11444 }
11445
11446 /* Point refers normally to the selected window. For any other
11447 window, set up appropriate value. */
11448 if (!EQ (window, selected_window))
11449 {
11450 int new_pt = XMARKER (w->pointm)->charpos;
11451 int new_pt_byte = marker_byte_position (w->pointm);
11452 if (new_pt < BEGV)
11453 {
11454 new_pt = BEGV;
11455 new_pt_byte = BEGV_BYTE;
11456 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
11457 }
11458 else if (new_pt > (ZV - 1))
11459 {
11460 new_pt = ZV;
11461 new_pt_byte = ZV_BYTE;
11462 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
11463 }
11464
11465 /* We don't use SET_PT so that the point-motion hooks don't run. */
11466 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
11467 }
11468
11469 /* If any of the character widths specified in the display table
11470 have changed, invalidate the width run cache. It's true that
11471 this may be a bit late to catch such changes, but the rest of
11472 redisplay goes (non-fatally) haywire when the display table is
11473 changed, so why should we worry about doing any better? */
11474 if (current_buffer->width_run_cache)
11475 {
11476 struct Lisp_Char_Table *disptab = buffer_display_table ();
11477
11478 if (! disptab_matches_widthtab (disptab,
11479 XVECTOR (current_buffer->width_table)))
11480 {
11481 invalidate_region_cache (current_buffer,
11482 current_buffer->width_run_cache,
11483 BEG, Z);
11484 recompute_width_table (current_buffer, disptab);
11485 }
11486 }
11487
11488 /* If window-start is screwed up, choose a new one. */
11489 if (XMARKER (w->start)->buffer != current_buffer)
11490 goto recenter;
11491
11492 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11493
11494 /* If someone specified a new starting point but did not insist,
11495 check whether it can be used. */
11496 if (!NILP (w->optional_new_start)
11497 && CHARPOS (startp) >= BEGV
11498 && CHARPOS (startp) <= ZV)
11499 {
11500 w->optional_new_start = Qnil;
11501 start_display (&it, w, startp);
11502 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11503 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11504 if (IT_CHARPOS (it) == PT)
11505 w->force_start = Qt;
11506 }
11507
11508 /* Handle case where place to start displaying has been specified,
11509 unless the specified location is outside the accessible range. */
11510 if (!NILP (w->force_start)
11511 || w->frozen_window_start_p)
11512 {
11513 /* We set this later on if we have to adjust point. */
11514 int new_vpos = -1;
11515
11516 w->force_start = Qnil;
11517 w->vscroll = 0;
11518 w->window_end_valid = Qnil;
11519
11520 /* Forget any recorded base line for line number display. */
11521 if (!buffer_unchanged_p)
11522 w->base_line_number = Qnil;
11523
11524 /* Redisplay the mode line. Select the buffer properly for that.
11525 Also, run the hook window-scroll-functions
11526 because we have scrolled. */
11527 /* Note, we do this after clearing force_start because
11528 if there's an error, it is better to forget about force_start
11529 than to get into an infinite loop calling the hook functions
11530 and having them get more errors. */
11531 if (!update_mode_line
11532 || ! NILP (Vwindow_scroll_functions))
11533 {
11534 update_mode_line = 1;
11535 w->update_mode_line = Qt;
11536 startp = run_window_scroll_functions (window, startp);
11537 }
11538
11539 w->last_modified = make_number (0);
11540 w->last_overlay_modified = make_number (0);
11541 if (CHARPOS (startp) < BEGV)
11542 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
11543 else if (CHARPOS (startp) > ZV)
11544 SET_TEXT_POS (startp, ZV, ZV_BYTE);
11545
11546 /* Redisplay, then check if cursor has been set during the
11547 redisplay. Give up if new fonts were loaded. */
11548 if (!try_window (window, startp))
11549 {
11550 w->force_start = Qt;
11551 clear_glyph_matrix (w->desired_matrix);
11552 goto need_larger_matrices;
11553 }
11554
11555 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
11556 {
11557 /* If point does not appear, try to move point so it does
11558 appear. The desired matrix has been built above, so we
11559 can use it here. */
11560 new_vpos = window_box_height (w) / 2;
11561 }
11562
11563 if (!make_cursor_line_fully_visible (w))
11564 {
11565 /* Point does appear, but on a line partly visible at end of window.
11566 Move it back to a fully-visible line. */
11567 new_vpos = window_box_height (w);
11568 }
11569
11570 /* If we need to move point for either of the above reasons,
11571 now actually do it. */
11572 if (new_vpos >= 0)
11573 {
11574 struct glyph_row *row;
11575
11576 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
11577 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
11578 ++row;
11579
11580 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
11581 MATRIX_ROW_START_BYTEPOS (row));
11582
11583 if (w != XWINDOW (selected_window))
11584 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
11585 else if (current_buffer == old)
11586 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11587
11588 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
11589
11590 /* If we are highlighting the region, then we just changed
11591 the region, so redisplay to show it. */
11592 if (!NILP (Vtransient_mark_mode)
11593 && !NILP (current_buffer->mark_active))
11594 {
11595 clear_glyph_matrix (w->desired_matrix);
11596 if (!try_window (window, startp))
11597 goto need_larger_matrices;
11598 }
11599 }
11600
11601 #if GLYPH_DEBUG
11602 debug_method_add (w, "forced window start");
11603 #endif
11604 goto done;
11605 }
11606
11607 /* Handle case where text has not changed, only point, and it has
11608 not moved off the frame, and we are not retrying after hscroll.
11609 (current_matrix_up_to_date_p is nonzero when retrying.) */
11610 if (current_matrix_up_to_date_p
11611 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
11612 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
11613 {
11614 switch (rc)
11615 {
11616 case CURSOR_MOVEMENT_SUCCESS:
11617 goto done;
11618
11619 #if 0 /* try_cursor_movement never returns this value. */
11620 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
11621 goto need_larger_matrices;
11622 #endif
11623
11624 case CURSOR_MOVEMENT_MUST_SCROLL:
11625 goto try_to_scroll;
11626
11627 default:
11628 abort ();
11629 }
11630 }
11631 /* If current starting point was originally the beginning of a line
11632 but no longer is, find a new starting point. */
11633 else if (!NILP (w->start_at_line_beg)
11634 && !(CHARPOS (startp) <= BEGV
11635 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
11636 {
11637 #if GLYPH_DEBUG
11638 debug_method_add (w, "recenter 1");
11639 #endif
11640 goto recenter;
11641 }
11642
11643 /* Try scrolling with try_window_id. Value is > 0 if update has
11644 been done, it is -1 if we know that the same window start will
11645 not work. It is 0 if unsuccessful for some other reason. */
11646 else if ((tem = try_window_id (w)) != 0)
11647 {
11648 #if GLYPH_DEBUG
11649 debug_method_add (w, "try_window_id %d", tem);
11650 #endif
11651
11652 if (fonts_changed_p)
11653 goto need_larger_matrices;
11654 if (tem > 0)
11655 goto done;
11656
11657 /* Otherwise try_window_id has returned -1 which means that we
11658 don't want the alternative below this comment to execute. */
11659 }
11660 else if (CHARPOS (startp) >= BEGV
11661 && CHARPOS (startp) <= ZV
11662 && PT >= CHARPOS (startp)
11663 && (CHARPOS (startp) < ZV
11664 /* Avoid starting at end of buffer. */
11665 || CHARPOS (startp) == BEGV
11666 || (XFASTINT (w->last_modified) >= MODIFF
11667 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
11668 {
11669 #if GLYPH_DEBUG
11670 debug_method_add (w, "same window start");
11671 #endif
11672
11673 /* Try to redisplay starting at same place as before.
11674 If point has not moved off frame, accept the results. */
11675 if (!current_matrix_up_to_date_p
11676 /* Don't use try_window_reusing_current_matrix in this case
11677 because a window scroll function can have changed the
11678 buffer. */
11679 || !NILP (Vwindow_scroll_functions)
11680 || MINI_WINDOW_P (w)
11681 || !try_window_reusing_current_matrix (w))
11682 {
11683 IF_DEBUG (debug_method_add (w, "1"));
11684 try_window (window, startp);
11685 }
11686
11687 if (fonts_changed_p)
11688 goto need_larger_matrices;
11689
11690 if (w->cursor.vpos >= 0)
11691 {
11692 if (!just_this_one_p
11693 || current_buffer->clip_changed
11694 || BEG_UNCHANGED < CHARPOS (startp))
11695 /* Forget any recorded base line for line number display. */
11696 w->base_line_number = Qnil;
11697
11698 if (!make_cursor_line_fully_visible (w))
11699 {
11700 clear_glyph_matrix (w->desired_matrix);
11701 last_line_misfit = 1;
11702 }
11703 /* Drop through and scroll. */
11704 else
11705 goto done;
11706 }
11707 else
11708 clear_glyph_matrix (w->desired_matrix);
11709 }
11710
11711 try_to_scroll:
11712
11713 w->last_modified = make_number (0);
11714 w->last_overlay_modified = make_number (0);
11715
11716 /* Redisplay the mode line. Select the buffer properly for that. */
11717 if (!update_mode_line)
11718 {
11719 update_mode_line = 1;
11720 w->update_mode_line = Qt;
11721 }
11722
11723 /* Try to scroll by specified few lines. */
11724 if ((scroll_conservatively
11725 || scroll_step
11726 || temp_scroll_step
11727 || NUMBERP (current_buffer->scroll_up_aggressively)
11728 || NUMBERP (current_buffer->scroll_down_aggressively))
11729 && !current_buffer->clip_changed
11730 && CHARPOS (startp) >= BEGV
11731 && CHARPOS (startp) <= ZV)
11732 {
11733 /* The function returns -1 if new fonts were loaded, 1 if
11734 successful, 0 if not successful. */
11735 int rc = try_scrolling (window, just_this_one_p,
11736 scroll_conservatively,
11737 scroll_step,
11738 temp_scroll_step, last_line_misfit);
11739 switch (rc)
11740 {
11741 case SCROLLING_SUCCESS:
11742 goto done;
11743
11744 case SCROLLING_NEED_LARGER_MATRICES:
11745 goto need_larger_matrices;
11746
11747 case SCROLLING_FAILED:
11748 break;
11749
11750 default:
11751 abort ();
11752 }
11753 }
11754
11755 /* Finally, just choose place to start which centers point */
11756
11757 recenter:
11758 centering_position = window_box_height (w) / 2;
11759
11760 point_at_top:
11761 /* Jump here with centering_position already set to 0. */
11762
11763 #if GLYPH_DEBUG
11764 debug_method_add (w, "recenter");
11765 #endif
11766
11767 /* w->vscroll = 0; */
11768
11769 /* Forget any previously recorded base line for line number display. */
11770 if (!buffer_unchanged_p)
11771 w->base_line_number = Qnil;
11772
11773 /* Move backward half the height of the window. */
11774 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11775 it.current_y = it.last_visible_y;
11776 move_it_vertically_backward (&it, centering_position);
11777 xassert (IT_CHARPOS (it) >= BEGV);
11778
11779 /* The function move_it_vertically_backward may move over more
11780 than the specified y-distance. If it->w is small, e.g. a
11781 mini-buffer window, we may end up in front of the window's
11782 display area. Start displaying at the start of the line
11783 containing PT in this case. */
11784 if (it.current_y <= 0)
11785 {
11786 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11787 move_it_vertically (&it, 0);
11788 xassert (IT_CHARPOS (it) <= PT);
11789 it.current_y = 0;
11790 }
11791
11792 it.current_x = it.hpos = 0;
11793
11794 /* Set startp here explicitly in case that helps avoid an infinite loop
11795 in case the window-scroll-functions functions get errors. */
11796 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
11797
11798 /* Run scroll hooks. */
11799 startp = run_window_scroll_functions (window, it.current.pos);
11800
11801 /* Redisplay the window. */
11802 if (!current_matrix_up_to_date_p
11803 || windows_or_buffers_changed
11804 || cursor_type_changed
11805 /* Don't use try_window_reusing_current_matrix in this case
11806 because it can have changed the buffer. */
11807 || !NILP (Vwindow_scroll_functions)
11808 || !just_this_one_p
11809 || MINI_WINDOW_P (w)
11810 || !try_window_reusing_current_matrix (w))
11811 try_window (window, startp);
11812
11813 /* If new fonts have been loaded (due to fontsets), give up. We
11814 have to start a new redisplay since we need to re-adjust glyph
11815 matrices. */
11816 if (fonts_changed_p)
11817 goto need_larger_matrices;
11818
11819 /* If cursor did not appear assume that the middle of the window is
11820 in the first line of the window. Do it again with the next line.
11821 (Imagine a window of height 100, displaying two lines of height
11822 60. Moving back 50 from it->last_visible_y will end in the first
11823 line.) */
11824 if (w->cursor.vpos < 0)
11825 {
11826 if (!NILP (w->window_end_valid)
11827 && PT >= Z - XFASTINT (w->window_end_pos))
11828 {
11829 clear_glyph_matrix (w->desired_matrix);
11830 move_it_by_lines (&it, 1, 0);
11831 try_window (window, it.current.pos);
11832 }
11833 else if (PT < IT_CHARPOS (it))
11834 {
11835 clear_glyph_matrix (w->desired_matrix);
11836 move_it_by_lines (&it, -1, 0);
11837 try_window (window, it.current.pos);
11838 }
11839 else
11840 {
11841 /* Not much we can do about it. */
11842 }
11843 }
11844
11845 /* Consider the following case: Window starts at BEGV, there is
11846 invisible, intangible text at BEGV, so that display starts at
11847 some point START > BEGV. It can happen that we are called with
11848 PT somewhere between BEGV and START. Try to handle that case. */
11849 if (w->cursor.vpos < 0)
11850 {
11851 struct glyph_row *row = w->current_matrix->rows;
11852 if (row->mode_line_p)
11853 ++row;
11854 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11855 }
11856
11857 if (!make_cursor_line_fully_visible (w))
11858 {
11859 /* If vscroll is enabled, disable it and try again. */
11860 if (w->vscroll)
11861 {
11862 w->vscroll = 0;
11863 clear_glyph_matrix (w->desired_matrix);
11864 goto recenter;
11865 }
11866
11867 /* If centering point failed to make the whole line visible,
11868 put point at the top instead. That has to make the whole line
11869 visible, if it can be done. */
11870 centering_position = 0;
11871 goto point_at_top;
11872 }
11873
11874 done:
11875
11876 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11877 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
11878 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
11879 ? Qt : Qnil);
11880
11881 /* Display the mode line, if we must. */
11882 if ((update_mode_line
11883 /* If window not full width, must redo its mode line
11884 if (a) the window to its side is being redone and
11885 (b) we do a frame-based redisplay. This is a consequence
11886 of how inverted lines are drawn in frame-based redisplay. */
11887 || (!just_this_one_p
11888 && !FRAME_WINDOW_P (f)
11889 && !WINDOW_FULL_WIDTH_P (w))
11890 /* Line number to display. */
11891 || INTEGERP (w->base_line_pos)
11892 /* Column number is displayed and different from the one displayed. */
11893 || (!NILP (w->column_number_displayed)
11894 && (XFASTINT (w->column_number_displayed)
11895 != (int) current_column ()))) /* iftc */
11896 /* This means that the window has a mode line. */
11897 && (WINDOW_WANTS_MODELINE_P (w)
11898 || WINDOW_WANTS_HEADER_LINE_P (w)))
11899 {
11900 display_mode_lines (w);
11901
11902 /* If mode line height has changed, arrange for a thorough
11903 immediate redisplay using the correct mode line height. */
11904 if (WINDOW_WANTS_MODELINE_P (w)
11905 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
11906 {
11907 fonts_changed_p = 1;
11908 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
11909 = DESIRED_MODE_LINE_HEIGHT (w);
11910 }
11911
11912 /* If top line height has changed, arrange for a thorough
11913 immediate redisplay using the correct mode line height. */
11914 if (WINDOW_WANTS_HEADER_LINE_P (w)
11915 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
11916 {
11917 fonts_changed_p = 1;
11918 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
11919 = DESIRED_HEADER_LINE_HEIGHT (w);
11920 }
11921
11922 if (fonts_changed_p)
11923 goto need_larger_matrices;
11924 }
11925
11926 if (!line_number_displayed
11927 && !BUFFERP (w->base_line_pos))
11928 {
11929 w->base_line_pos = Qnil;
11930 w->base_line_number = Qnil;
11931 }
11932
11933 finish_menu_bars:
11934
11935 /* When we reach a frame's selected window, redo the frame's menu bar. */
11936 if (update_mode_line
11937 && EQ (FRAME_SELECTED_WINDOW (f), window))
11938 {
11939 int redisplay_menu_p = 0;
11940 int redisplay_tool_bar_p = 0;
11941
11942 if (FRAME_WINDOW_P (f))
11943 {
11944 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
11945 || defined (USE_GTK)
11946 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
11947 #else
11948 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
11949 #endif
11950 }
11951 else
11952 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
11953
11954 if (redisplay_menu_p)
11955 display_menu_bar (w);
11956
11957 #ifdef HAVE_WINDOW_SYSTEM
11958 #ifdef USE_GTK
11959 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
11960 #else
11961 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
11962 && (FRAME_TOOL_BAR_LINES (f) > 0
11963 || auto_resize_tool_bars_p);
11964
11965 #endif
11966
11967 if (redisplay_tool_bar_p)
11968 redisplay_tool_bar (f);
11969 #endif
11970 }
11971
11972 /* We go to this label, with fonts_changed_p nonzero,
11973 if it is necessary to try again using larger glyph matrices.
11974 We have to redeem the scroll bar even in this case,
11975 because the loop in redisplay_internal expects that. */
11976 need_larger_matrices:
11977 ;
11978 finish_scroll_bars:
11979
11980 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
11981 {
11982 /* Set the thumb's position and size. */
11983 set_vertical_scroll_bar (w);
11984
11985 /* Note that we actually used the scroll bar attached to this
11986 window, so it shouldn't be deleted at the end of redisplay. */
11987 redeem_scroll_bar_hook (w);
11988 }
11989
11990 /* Restore current_buffer and value of point in it. */
11991 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
11992 set_buffer_internal_1 (old);
11993 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
11994
11995 unbind_to (count, Qnil);
11996 }
11997
11998
11999 /* Build the complete desired matrix of WINDOW with a window start
12000 buffer position POS. Value is non-zero if successful. It is zero
12001 if fonts were loaded during redisplay which makes re-adjusting
12002 glyph matrices necessary. */
12003
12004 int
12005 try_window (window, pos)
12006 Lisp_Object window;
12007 struct text_pos pos;
12008 {
12009 struct window *w = XWINDOW (window);
12010 struct it it;
12011 struct glyph_row *last_text_row = NULL;
12012
12013 /* Make POS the new window start. */
12014 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12015
12016 /* Mark cursor position as unknown. No overlay arrow seen. */
12017 w->cursor.vpos = -1;
12018 overlay_arrow_seen = 0;
12019
12020 /* Initialize iterator and info to start at POS. */
12021 start_display (&it, w, pos);
12022
12023 /* Display all lines of W. */
12024 while (it.current_y < it.last_visible_y)
12025 {
12026 if (display_line (&it))
12027 last_text_row = it.glyph_row - 1;
12028 if (fonts_changed_p)
12029 return 0;
12030 }
12031
12032 /* If bottom moved off end of frame, change mode line percentage. */
12033 if (XFASTINT (w->window_end_pos) <= 0
12034 && Z != IT_CHARPOS (it))
12035 w->update_mode_line = Qt;
12036
12037 /* Set window_end_pos to the offset of the last character displayed
12038 on the window from the end of current_buffer. Set
12039 window_end_vpos to its row number. */
12040 if (last_text_row)
12041 {
12042 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12043 w->window_end_bytepos
12044 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12045 w->window_end_pos
12046 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12047 w->window_end_vpos
12048 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12049 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12050 ->displays_text_p);
12051 }
12052 else
12053 {
12054 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12055 w->window_end_pos = make_number (Z - ZV);
12056 w->window_end_vpos = make_number (0);
12057 }
12058
12059 /* But that is not valid info until redisplay finishes. */
12060 w->window_end_valid = Qnil;
12061 return 1;
12062 }
12063
12064
12065 \f
12066 /************************************************************************
12067 Window redisplay reusing current matrix when buffer has not changed
12068 ************************************************************************/
12069
12070 /* Try redisplay of window W showing an unchanged buffer with a
12071 different window start than the last time it was displayed by
12072 reusing its current matrix. Value is non-zero if successful.
12073 W->start is the new window start. */
12074
12075 static int
12076 try_window_reusing_current_matrix (w)
12077 struct window *w;
12078 {
12079 struct frame *f = XFRAME (w->frame);
12080 struct glyph_row *row, *bottom_row;
12081 struct it it;
12082 struct run run;
12083 struct text_pos start, new_start;
12084 int nrows_scrolled, i;
12085 struct glyph_row *last_text_row;
12086 struct glyph_row *last_reused_text_row;
12087 struct glyph_row *start_row;
12088 int start_vpos, min_y, max_y;
12089
12090 #if GLYPH_DEBUG
12091 if (inhibit_try_window_reusing)
12092 return 0;
12093 #endif
12094
12095 if (/* This function doesn't handle terminal frames. */
12096 !FRAME_WINDOW_P (f)
12097 /* Don't try to reuse the display if windows have been split
12098 or such. */
12099 || windows_or_buffers_changed
12100 || cursor_type_changed)
12101 return 0;
12102
12103 /* Can't do this if region may have changed. */
12104 if ((!NILP (Vtransient_mark_mode)
12105 && !NILP (current_buffer->mark_active))
12106 || !NILP (w->region_showing)
12107 || !NILP (Vshow_trailing_whitespace))
12108 return 0;
12109
12110 /* If top-line visibility has changed, give up. */
12111 if (WINDOW_WANTS_HEADER_LINE_P (w)
12112 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12113 return 0;
12114
12115 /* Give up if old or new display is scrolled vertically. We could
12116 make this function handle this, but right now it doesn't. */
12117 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12118 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
12119 return 0;
12120
12121 /* The variable new_start now holds the new window start. The old
12122 start `start' can be determined from the current matrix. */
12123 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12124 start = start_row->start.pos;
12125 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12126
12127 /* Clear the desired matrix for the display below. */
12128 clear_glyph_matrix (w->desired_matrix);
12129
12130 if (CHARPOS (new_start) <= CHARPOS (start))
12131 {
12132 int first_row_y;
12133
12134 /* Don't use this method if the display starts with an ellipsis
12135 displayed for invisible text. It's not easy to handle that case
12136 below, and it's certainly not worth the effort since this is
12137 not a frequent case. */
12138 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12139 return 0;
12140
12141 IF_DEBUG (debug_method_add (w, "twu1"));
12142
12143 /* Display up to a row that can be reused. The variable
12144 last_text_row is set to the last row displayed that displays
12145 text. Note that it.vpos == 0 if or if not there is a
12146 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12147 start_display (&it, w, new_start);
12148 first_row_y = it.current_y;
12149 w->cursor.vpos = -1;
12150 last_text_row = last_reused_text_row = NULL;
12151
12152 while (it.current_y < it.last_visible_y
12153 && IT_CHARPOS (it) < CHARPOS (start)
12154 && !fonts_changed_p)
12155 if (display_line (&it))
12156 last_text_row = it.glyph_row - 1;
12157
12158 /* A value of current_y < last_visible_y means that we stopped
12159 at the previous window start, which in turn means that we
12160 have at least one reusable row. */
12161 if (it.current_y < it.last_visible_y)
12162 {
12163 /* IT.vpos always starts from 0; it counts text lines. */
12164 nrows_scrolled = it.vpos;
12165
12166 /* Find PT if not already found in the lines displayed. */
12167 if (w->cursor.vpos < 0)
12168 {
12169 int dy = it.current_y - first_row_y;
12170
12171 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12172 row = row_containing_pos (w, PT, row, NULL, dy);
12173 if (row)
12174 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12175 dy, nrows_scrolled);
12176 else
12177 {
12178 clear_glyph_matrix (w->desired_matrix);
12179 return 0;
12180 }
12181 }
12182
12183 /* Scroll the display. Do it before the current matrix is
12184 changed. The problem here is that update has not yet
12185 run, i.e. part of the current matrix is not up to date.
12186 scroll_run_hook will clear the cursor, and use the
12187 current matrix to get the height of the row the cursor is
12188 in. */
12189 run.current_y = first_row_y;
12190 run.desired_y = it.current_y;
12191 run.height = it.last_visible_y - it.current_y;
12192
12193 if (run.height > 0 && run.current_y != run.desired_y)
12194 {
12195 update_begin (f);
12196 rif->update_window_begin_hook (w);
12197 rif->clear_window_mouse_face (w);
12198 rif->scroll_run_hook (w, &run);
12199 rif->update_window_end_hook (w, 0, 0);
12200 update_end (f);
12201 }
12202
12203 /* Shift current matrix down by nrows_scrolled lines. */
12204 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12205 rotate_matrix (w->current_matrix,
12206 start_vpos,
12207 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12208 nrows_scrolled);
12209
12210 /* Disable lines that must be updated. */
12211 for (i = 0; i < it.vpos; ++i)
12212 (start_row + i)->enabled_p = 0;
12213
12214 /* Re-compute Y positions. */
12215 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12216 max_y = it.last_visible_y;
12217 for (row = start_row + nrows_scrolled;
12218 row < bottom_row;
12219 ++row)
12220 {
12221 row->y = it.current_y;
12222 row->visible_height = row->height;
12223
12224 if (row->y < min_y)
12225 row->visible_height -= min_y - row->y;
12226 if (row->y + row->height > max_y)
12227 row->visible_height -= row->y + row->height - max_y;
12228
12229 it.current_y += row->height;
12230
12231 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12232 last_reused_text_row = row;
12233 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12234 break;
12235 }
12236
12237 /* Disable lines in the current matrix which are now
12238 below the window. */
12239 for (++row; row < bottom_row; ++row)
12240 row->enabled_p = 0;
12241 }
12242
12243 /* Update window_end_pos etc.; last_reused_text_row is the last
12244 reused row from the current matrix containing text, if any.
12245 The value of last_text_row is the last displayed line
12246 containing text. */
12247 if (last_reused_text_row)
12248 {
12249 w->window_end_bytepos
12250 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12251 w->window_end_pos
12252 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12253 w->window_end_vpos
12254 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12255 w->current_matrix));
12256 }
12257 else if (last_text_row)
12258 {
12259 w->window_end_bytepos
12260 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12261 w->window_end_pos
12262 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12263 w->window_end_vpos
12264 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12265 }
12266 else
12267 {
12268 /* This window must be completely empty. */
12269 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12270 w->window_end_pos = make_number (Z - ZV);
12271 w->window_end_vpos = make_number (0);
12272 }
12273 w->window_end_valid = Qnil;
12274
12275 /* Update hint: don't try scrolling again in update_window. */
12276 w->desired_matrix->no_scrolling_p = 1;
12277
12278 #if GLYPH_DEBUG
12279 debug_method_add (w, "try_window_reusing_current_matrix 1");
12280 #endif
12281 return 1;
12282 }
12283 else if (CHARPOS (new_start) > CHARPOS (start))
12284 {
12285 struct glyph_row *pt_row, *row;
12286 struct glyph_row *first_reusable_row;
12287 struct glyph_row *first_row_to_display;
12288 int dy;
12289 int yb = window_text_bottom_y (w);
12290
12291 /* Find the row starting at new_start, if there is one. Don't
12292 reuse a partially visible line at the end. */
12293 first_reusable_row = start_row;
12294 while (first_reusable_row->enabled_p
12295 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12296 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12297 < CHARPOS (new_start)))
12298 ++first_reusable_row;
12299
12300 /* Give up if there is no row to reuse. */
12301 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
12302 || !first_reusable_row->enabled_p
12303 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12304 != CHARPOS (new_start)))
12305 return 0;
12306
12307 /* We can reuse fully visible rows beginning with
12308 first_reusable_row to the end of the window. Set
12309 first_row_to_display to the first row that cannot be reused.
12310 Set pt_row to the row containing point, if there is any. */
12311 pt_row = NULL;
12312 for (first_row_to_display = first_reusable_row;
12313 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12314 ++first_row_to_display)
12315 {
12316 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12317 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
12318 pt_row = first_row_to_display;
12319 }
12320
12321 /* Start displaying at the start of first_row_to_display. */
12322 xassert (first_row_to_display->y < yb);
12323 init_to_row_start (&it, w, first_row_to_display);
12324
12325 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12326 - start_vpos);
12327 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12328 - nrows_scrolled);
12329 it.current_y = (first_row_to_display->y - first_reusable_row->y
12330 + WINDOW_HEADER_LINE_HEIGHT (w));
12331
12332 /* Display lines beginning with first_row_to_display in the
12333 desired matrix. Set last_text_row to the last row displayed
12334 that displays text. */
12335 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12336 if (pt_row == NULL)
12337 w->cursor.vpos = -1;
12338 last_text_row = NULL;
12339 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12340 if (display_line (&it))
12341 last_text_row = it.glyph_row - 1;
12342
12343 /* Give up If point isn't in a row displayed or reused. */
12344 if (w->cursor.vpos < 0)
12345 {
12346 clear_glyph_matrix (w->desired_matrix);
12347 return 0;
12348 }
12349
12350 /* If point is in a reused row, adjust y and vpos of the cursor
12351 position. */
12352 if (pt_row)
12353 {
12354 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
12355 w->current_matrix);
12356 w->cursor.y -= first_reusable_row->y;
12357 }
12358
12359 /* Scroll the display. */
12360 run.current_y = first_reusable_row->y;
12361 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
12362 run.height = it.last_visible_y - run.current_y;
12363 dy = run.current_y - run.desired_y;
12364
12365 if (run.height)
12366 {
12367 struct frame *f = XFRAME (WINDOW_FRAME (w));
12368 update_begin (f);
12369 rif->update_window_begin_hook (w);
12370 rif->clear_window_mouse_face (w);
12371 rif->scroll_run_hook (w, &run);
12372 rif->update_window_end_hook (w, 0, 0);
12373 update_end (f);
12374 }
12375
12376 /* Adjust Y positions of reused rows. */
12377 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12378 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12379 max_y = it.last_visible_y;
12380 for (row = first_reusable_row; row < first_row_to_display; ++row)
12381 {
12382 row->y -= dy;
12383 row->visible_height = row->height;
12384 if (row->y < min_y)
12385 row->visible_height -= min_y - row->y;
12386 if (row->y + row->height > max_y)
12387 row->visible_height -= row->y + row->height - max_y;
12388 }
12389
12390 /* Scroll the current matrix. */
12391 xassert (nrows_scrolled > 0);
12392 rotate_matrix (w->current_matrix,
12393 start_vpos,
12394 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12395 -nrows_scrolled);
12396
12397 /* Disable rows not reused. */
12398 for (row -= nrows_scrolled; row < bottom_row; ++row)
12399 row->enabled_p = 0;
12400
12401 /* Adjust window end. A null value of last_text_row means that
12402 the window end is in reused rows which in turn means that
12403 only its vpos can have changed. */
12404 if (last_text_row)
12405 {
12406 w->window_end_bytepos
12407 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12408 w->window_end_pos
12409 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12410 w->window_end_vpos
12411 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12412 }
12413 else
12414 {
12415 w->window_end_vpos
12416 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
12417 }
12418
12419 w->window_end_valid = Qnil;
12420 w->desired_matrix->no_scrolling_p = 1;
12421
12422 #if GLYPH_DEBUG
12423 debug_method_add (w, "try_window_reusing_current_matrix 2");
12424 #endif
12425 return 1;
12426 }
12427
12428 return 0;
12429 }
12430
12431
12432 \f
12433 /************************************************************************
12434 Window redisplay reusing current matrix when buffer has changed
12435 ************************************************************************/
12436
12437 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
12438 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
12439 int *, int *));
12440 static struct glyph_row *
12441 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
12442 struct glyph_row *));
12443
12444
12445 /* Return the last row in MATRIX displaying text. If row START is
12446 non-null, start searching with that row. IT gives the dimensions
12447 of the display. Value is null if matrix is empty; otherwise it is
12448 a pointer to the row found. */
12449
12450 static struct glyph_row *
12451 find_last_row_displaying_text (matrix, it, start)
12452 struct glyph_matrix *matrix;
12453 struct it *it;
12454 struct glyph_row *start;
12455 {
12456 struct glyph_row *row, *row_found;
12457
12458 /* Set row_found to the last row in IT->w's current matrix
12459 displaying text. The loop looks funny but think of partially
12460 visible lines. */
12461 row_found = NULL;
12462 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
12463 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12464 {
12465 xassert (row->enabled_p);
12466 row_found = row;
12467 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
12468 break;
12469 ++row;
12470 }
12471
12472 return row_found;
12473 }
12474
12475
12476 /* Return the last row in the current matrix of W that is not affected
12477 by changes at the start of current_buffer that occurred since W's
12478 current matrix was built. Value is null if no such row exists.
12479
12480 BEG_UNCHANGED us the number of characters unchanged at the start of
12481 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
12482 first changed character in current_buffer. Characters at positions <
12483 BEG + BEG_UNCHANGED are at the same buffer positions as they were
12484 when the current matrix was built. */
12485
12486 static struct glyph_row *
12487 find_last_unchanged_at_beg_row (w)
12488 struct window *w;
12489 {
12490 int first_changed_pos = BEG + BEG_UNCHANGED;
12491 struct glyph_row *row;
12492 struct glyph_row *row_found = NULL;
12493 int yb = window_text_bottom_y (w);
12494
12495 /* Find the last row displaying unchanged text. */
12496 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12497 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12498 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
12499 {
12500 if (/* If row ends before first_changed_pos, it is unchanged,
12501 except in some case. */
12502 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
12503 /* When row ends in ZV and we write at ZV it is not
12504 unchanged. */
12505 && !row->ends_at_zv_p
12506 /* When first_changed_pos is the end of a continued line,
12507 row is not unchanged because it may be no longer
12508 continued. */
12509 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
12510 && row->continued_p))
12511 row_found = row;
12512
12513 /* Stop if last visible row. */
12514 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
12515 break;
12516
12517 ++row;
12518 }
12519
12520 return row_found;
12521 }
12522
12523
12524 /* Find the first glyph row in the current matrix of W that is not
12525 affected by changes at the end of current_buffer since the
12526 time W's current matrix was built.
12527
12528 Return in *DELTA the number of chars by which buffer positions in
12529 unchanged text at the end of current_buffer must be adjusted.
12530
12531 Return in *DELTA_BYTES the corresponding number of bytes.
12532
12533 Value is null if no such row exists, i.e. all rows are affected by
12534 changes. */
12535
12536 static struct glyph_row *
12537 find_first_unchanged_at_end_row (w, delta, delta_bytes)
12538 struct window *w;
12539 int *delta, *delta_bytes;
12540 {
12541 struct glyph_row *row;
12542 struct glyph_row *row_found = NULL;
12543
12544 *delta = *delta_bytes = 0;
12545
12546 /* Display must not have been paused, otherwise the current matrix
12547 is not up to date. */
12548 if (NILP (w->window_end_valid))
12549 abort ();
12550
12551 /* A value of window_end_pos >= END_UNCHANGED means that the window
12552 end is in the range of changed text. If so, there is no
12553 unchanged row at the end of W's current matrix. */
12554 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
12555 return NULL;
12556
12557 /* Set row to the last row in W's current matrix displaying text. */
12558 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12559
12560 /* If matrix is entirely empty, no unchanged row exists. */
12561 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12562 {
12563 /* The value of row is the last glyph row in the matrix having a
12564 meaningful buffer position in it. The end position of row
12565 corresponds to window_end_pos. This allows us to translate
12566 buffer positions in the current matrix to current buffer
12567 positions for characters not in changed text. */
12568 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12569 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12570 int last_unchanged_pos, last_unchanged_pos_old;
12571 struct glyph_row *first_text_row
12572 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12573
12574 *delta = Z - Z_old;
12575 *delta_bytes = Z_BYTE - Z_BYTE_old;
12576
12577 /* Set last_unchanged_pos to the buffer position of the last
12578 character in the buffer that has not been changed. Z is the
12579 index + 1 of the last character in current_buffer, i.e. by
12580 subtracting END_UNCHANGED we get the index of the last
12581 unchanged character, and we have to add BEG to get its buffer
12582 position. */
12583 last_unchanged_pos = Z - END_UNCHANGED + BEG;
12584 last_unchanged_pos_old = last_unchanged_pos - *delta;
12585
12586 /* Search backward from ROW for a row displaying a line that
12587 starts at a minimum position >= last_unchanged_pos_old. */
12588 for (; row > first_text_row; --row)
12589 {
12590 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
12591 abort ();
12592
12593 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
12594 row_found = row;
12595 }
12596 }
12597
12598 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
12599 abort ();
12600
12601 return row_found;
12602 }
12603
12604
12605 /* Make sure that glyph rows in the current matrix of window W
12606 reference the same glyph memory as corresponding rows in the
12607 frame's frame matrix. This function is called after scrolling W's
12608 current matrix on a terminal frame in try_window_id and
12609 try_window_reusing_current_matrix. */
12610
12611 static void
12612 sync_frame_with_window_matrix_rows (w)
12613 struct window *w;
12614 {
12615 struct frame *f = XFRAME (w->frame);
12616 struct glyph_row *window_row, *window_row_end, *frame_row;
12617
12618 /* Preconditions: W must be a leaf window and full-width. Its frame
12619 must have a frame matrix. */
12620 xassert (NILP (w->hchild) && NILP (w->vchild));
12621 xassert (WINDOW_FULL_WIDTH_P (w));
12622 xassert (!FRAME_WINDOW_P (f));
12623
12624 /* If W is a full-width window, glyph pointers in W's current matrix
12625 have, by definition, to be the same as glyph pointers in the
12626 corresponding frame matrix. Note that frame matrices have no
12627 marginal areas (see build_frame_matrix). */
12628 window_row = w->current_matrix->rows;
12629 window_row_end = window_row + w->current_matrix->nrows;
12630 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
12631 while (window_row < window_row_end)
12632 {
12633 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
12634 struct glyph *end = window_row->glyphs[LAST_AREA];
12635
12636 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
12637 frame_row->glyphs[TEXT_AREA] = start;
12638 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
12639 frame_row->glyphs[LAST_AREA] = end;
12640
12641 /* Disable frame rows whose corresponding window rows have
12642 been disabled in try_window_id. */
12643 if (!window_row->enabled_p)
12644 frame_row->enabled_p = 0;
12645
12646 ++window_row, ++frame_row;
12647 }
12648 }
12649
12650
12651 /* Find the glyph row in window W containing CHARPOS. Consider all
12652 rows between START and END (not inclusive). END null means search
12653 all rows to the end of the display area of W. Value is the row
12654 containing CHARPOS or null. */
12655
12656 struct glyph_row *
12657 row_containing_pos (w, charpos, start, end, dy)
12658 struct window *w;
12659 int charpos;
12660 struct glyph_row *start, *end;
12661 int dy;
12662 {
12663 struct glyph_row *row = start;
12664 int last_y;
12665
12666 /* If we happen to start on a header-line, skip that. */
12667 if (row->mode_line_p)
12668 ++row;
12669
12670 if ((end && row >= end) || !row->enabled_p)
12671 return NULL;
12672
12673 last_y = window_text_bottom_y (w) - dy;
12674
12675 while (1)
12676 {
12677 /* Give up if we have gone too far. */
12678 if (end && row >= end)
12679 return NULL;
12680 /* This formerly returned if they were equal.
12681 I think that both quantities are of a "last plus one" type;
12682 if so, when they are equal, the row is within the screen. -- rms. */
12683 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
12684 return NULL;
12685
12686 /* If it is in this row, return this row. */
12687 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
12688 || (MATRIX_ROW_END_CHARPOS (row) == charpos
12689 /* The end position of a row equals the start
12690 position of the next row. If CHARPOS is there, we
12691 would rather display it in the next line, except
12692 when this line ends in ZV. */
12693 && !row->ends_at_zv_p
12694 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12695 && charpos >= MATRIX_ROW_START_CHARPOS (row))
12696 return row;
12697 ++row;
12698 }
12699 }
12700
12701
12702 /* Try to redisplay window W by reusing its existing display. W's
12703 current matrix must be up to date when this function is called,
12704 i.e. window_end_valid must not be nil.
12705
12706 Value is
12707
12708 1 if display has been updated
12709 0 if otherwise unsuccessful
12710 -1 if redisplay with same window start is known not to succeed
12711
12712 The following steps are performed:
12713
12714 1. Find the last row in the current matrix of W that is not
12715 affected by changes at the start of current_buffer. If no such row
12716 is found, give up.
12717
12718 2. Find the first row in W's current matrix that is not affected by
12719 changes at the end of current_buffer. Maybe there is no such row.
12720
12721 3. Display lines beginning with the row + 1 found in step 1 to the
12722 row found in step 2 or, if step 2 didn't find a row, to the end of
12723 the window.
12724
12725 4. If cursor is not known to appear on the window, give up.
12726
12727 5. If display stopped at the row found in step 2, scroll the
12728 display and current matrix as needed.
12729
12730 6. Maybe display some lines at the end of W, if we must. This can
12731 happen under various circumstances, like a partially visible line
12732 becoming fully visible, or because newly displayed lines are displayed
12733 in smaller font sizes.
12734
12735 7. Update W's window end information. */
12736
12737 static int
12738 try_window_id (w)
12739 struct window *w;
12740 {
12741 struct frame *f = XFRAME (w->frame);
12742 struct glyph_matrix *current_matrix = w->current_matrix;
12743 struct glyph_matrix *desired_matrix = w->desired_matrix;
12744 struct glyph_row *last_unchanged_at_beg_row;
12745 struct glyph_row *first_unchanged_at_end_row;
12746 struct glyph_row *row;
12747 struct glyph_row *bottom_row;
12748 int bottom_vpos;
12749 struct it it;
12750 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
12751 struct text_pos start_pos;
12752 struct run run;
12753 int first_unchanged_at_end_vpos = 0;
12754 struct glyph_row *last_text_row, *last_text_row_at_end;
12755 struct text_pos start;
12756 int first_changed_charpos, last_changed_charpos;
12757
12758 #if GLYPH_DEBUG
12759 if (inhibit_try_window_id)
12760 return 0;
12761 #endif
12762
12763 /* This is handy for debugging. */
12764 #if 0
12765 #define GIVE_UP(X) \
12766 do { \
12767 fprintf (stderr, "try_window_id give up %d\n", (X)); \
12768 return 0; \
12769 } while (0)
12770 #else
12771 #define GIVE_UP(X) return 0
12772 #endif
12773
12774 SET_TEXT_POS_FROM_MARKER (start, w->start);
12775
12776 /* Don't use this for mini-windows because these can show
12777 messages and mini-buffers, and we don't handle that here. */
12778 if (MINI_WINDOW_P (w))
12779 GIVE_UP (1);
12780
12781 /* This flag is used to prevent redisplay optimizations. */
12782 if (windows_or_buffers_changed || cursor_type_changed)
12783 GIVE_UP (2);
12784
12785 /* Verify that narrowing has not changed.
12786 Also verify that we were not told to prevent redisplay optimizations.
12787 It would be nice to further
12788 reduce the number of cases where this prevents try_window_id. */
12789 if (current_buffer->clip_changed
12790 || current_buffer->prevent_redisplay_optimizations_p)
12791 GIVE_UP (3);
12792
12793 /* Window must either use window-based redisplay or be full width. */
12794 if (!FRAME_WINDOW_P (f)
12795 && (!line_ins_del_ok
12796 || !WINDOW_FULL_WIDTH_P (w)))
12797 GIVE_UP (4);
12798
12799 /* Give up if point is not known NOT to appear in W. */
12800 if (PT < CHARPOS (start))
12801 GIVE_UP (5);
12802
12803 /* Another way to prevent redisplay optimizations. */
12804 if (XFASTINT (w->last_modified) == 0)
12805 GIVE_UP (6);
12806
12807 /* Verify that window is not hscrolled. */
12808 if (XFASTINT (w->hscroll) != 0)
12809 GIVE_UP (7);
12810
12811 /* Verify that display wasn't paused. */
12812 if (NILP (w->window_end_valid))
12813 GIVE_UP (8);
12814
12815 /* Can't use this if highlighting a region because a cursor movement
12816 will do more than just set the cursor. */
12817 if (!NILP (Vtransient_mark_mode)
12818 && !NILP (current_buffer->mark_active))
12819 GIVE_UP (9);
12820
12821 /* Likewise if highlighting trailing whitespace. */
12822 if (!NILP (Vshow_trailing_whitespace))
12823 GIVE_UP (11);
12824
12825 /* Likewise if showing a region. */
12826 if (!NILP (w->region_showing))
12827 GIVE_UP (10);
12828
12829 /* Can use this if overlay arrow position and or string have changed. */
12830 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
12831 || !EQ (last_arrow_string, Voverlay_arrow_string))
12832 GIVE_UP (12);
12833
12834
12835 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
12836 only if buffer has really changed. The reason is that the gap is
12837 initially at Z for freshly visited files. The code below would
12838 set end_unchanged to 0 in that case. */
12839 if (MODIFF > SAVE_MODIFF
12840 /* This seems to happen sometimes after saving a buffer. */
12841 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
12842 {
12843 if (GPT - BEG < BEG_UNCHANGED)
12844 BEG_UNCHANGED = GPT - BEG;
12845 if (Z - GPT < END_UNCHANGED)
12846 END_UNCHANGED = Z - GPT;
12847 }
12848
12849 /* The position of the first and last character that has been changed. */
12850 first_changed_charpos = BEG + BEG_UNCHANGED;
12851 last_changed_charpos = Z - END_UNCHANGED;
12852
12853 /* If window starts after a line end, and the last change is in
12854 front of that newline, then changes don't affect the display.
12855 This case happens with stealth-fontification. Note that although
12856 the display is unchanged, glyph positions in the matrix have to
12857 be adjusted, of course. */
12858 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12859 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12860 && ((last_changed_charpos < CHARPOS (start)
12861 && CHARPOS (start) == BEGV)
12862 || (last_changed_charpos < CHARPOS (start) - 1
12863 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
12864 {
12865 int Z_old, delta, Z_BYTE_old, delta_bytes;
12866 struct glyph_row *r0;
12867
12868 /* Compute how many chars/bytes have been added to or removed
12869 from the buffer. */
12870 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12871 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12872 delta = Z - Z_old;
12873 delta_bytes = Z_BYTE - Z_BYTE_old;
12874
12875 /* Give up if PT is not in the window. Note that it already has
12876 been checked at the start of try_window_id that PT is not in
12877 front of the window start. */
12878 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
12879 GIVE_UP (13);
12880
12881 /* If window start is unchanged, we can reuse the whole matrix
12882 as is, after adjusting glyph positions. No need to compute
12883 the window end again, since its offset from Z hasn't changed. */
12884 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
12885 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
12886 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
12887 /* PT must not be in a partially visible line. */
12888 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
12889 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
12890 {
12891 /* Adjust positions in the glyph matrix. */
12892 if (delta || delta_bytes)
12893 {
12894 struct glyph_row *r1
12895 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
12896 increment_matrix_positions (w->current_matrix,
12897 MATRIX_ROW_VPOS (r0, current_matrix),
12898 MATRIX_ROW_VPOS (r1, current_matrix),
12899 delta, delta_bytes);
12900 }
12901
12902 /* Set the cursor. */
12903 row = row_containing_pos (w, PT, r0, NULL, 0);
12904 if (row)
12905 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
12906 else
12907 abort ();
12908 return 1;
12909 }
12910 }
12911
12912 /* Handle the case that changes are all below what is displayed in
12913 the window, and that PT is in the window. This shortcut cannot
12914 be taken if ZV is visible in the window, and text has been added
12915 there that is visible in the window. */
12916 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
12917 /* ZV is not visible in the window, or there are no
12918 changes at ZV, actually. */
12919 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
12920 || first_changed_charpos == last_changed_charpos))
12921 {
12922 struct glyph_row *r0;
12923
12924 /* Give up if PT is not in the window. Note that it already has
12925 been checked at the start of try_window_id that PT is not in
12926 front of the window start. */
12927 if (PT >= MATRIX_ROW_END_CHARPOS (row))
12928 GIVE_UP (14);
12929
12930 /* If window start is unchanged, we can reuse the whole matrix
12931 as is, without changing glyph positions since no text has
12932 been added/removed in front of the window end. */
12933 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
12934 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
12935 /* PT must not be in a partially visible line. */
12936 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
12937 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
12938 {
12939 /* We have to compute the window end anew since text
12940 can have been added/removed after it. */
12941 w->window_end_pos
12942 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12943 w->window_end_bytepos
12944 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12945
12946 /* Set the cursor. */
12947 row = row_containing_pos (w, PT, r0, NULL, 0);
12948 if (row)
12949 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
12950 else
12951 abort ();
12952 return 2;
12953 }
12954 }
12955
12956 /* Give up if window start is in the changed area.
12957
12958 The condition used to read
12959
12960 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
12961
12962 but why that was tested escapes me at the moment. */
12963 if (CHARPOS (start) >= first_changed_charpos
12964 && CHARPOS (start) <= last_changed_charpos)
12965 GIVE_UP (15);
12966
12967 /* Check that window start agrees with the start of the first glyph
12968 row in its current matrix. Check this after we know the window
12969 start is not in changed text, otherwise positions would not be
12970 comparable. */
12971 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
12972 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
12973 GIVE_UP (16);
12974
12975 /* Give up if the window ends in strings. Overlay strings
12976 at the end are difficult to handle, so don't try. */
12977 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
12978 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
12979 GIVE_UP (20);
12980
12981 /* Compute the position at which we have to start displaying new
12982 lines. Some of the lines at the top of the window might be
12983 reusable because they are not displaying changed text. Find the
12984 last row in W's current matrix not affected by changes at the
12985 start of current_buffer. Value is null if changes start in the
12986 first line of window. */
12987 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
12988 if (last_unchanged_at_beg_row)
12989 {
12990 /* Avoid starting to display in the moddle of a character, a TAB
12991 for instance. This is easier than to set up the iterator
12992 exactly, and it's not a frequent case, so the additional
12993 effort wouldn't really pay off. */
12994 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
12995 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
12996 && last_unchanged_at_beg_row > w->current_matrix->rows)
12997 --last_unchanged_at_beg_row;
12998
12999 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13000 GIVE_UP (17);
13001
13002 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13003 GIVE_UP (18);
13004 start_pos = it.current.pos;
13005
13006 /* Start displaying new lines in the desired matrix at the same
13007 vpos we would use in the current matrix, i.e. below
13008 last_unchanged_at_beg_row. */
13009 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13010 current_matrix);
13011 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13012 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13013
13014 xassert (it.hpos == 0 && it.current_x == 0);
13015 }
13016 else
13017 {
13018 /* There are no reusable lines at the start of the window.
13019 Start displaying in the first line. */
13020 start_display (&it, w, start);
13021 start_pos = it.current.pos;
13022 }
13023
13024 /* Find the first row that is not affected by changes at the end of
13025 the buffer. Value will be null if there is no unchanged row, in
13026 which case we must redisplay to the end of the window. delta
13027 will be set to the value by which buffer positions beginning with
13028 first_unchanged_at_end_row have to be adjusted due to text
13029 changes. */
13030 first_unchanged_at_end_row
13031 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13032 IF_DEBUG (debug_delta = delta);
13033 IF_DEBUG (debug_delta_bytes = delta_bytes);
13034
13035 /* Set stop_pos to the buffer position up to which we will have to
13036 display new lines. If first_unchanged_at_end_row != NULL, this
13037 is the buffer position of the start of the line displayed in that
13038 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13039 that we don't stop at a buffer position. */
13040 stop_pos = 0;
13041 if (first_unchanged_at_end_row)
13042 {
13043 xassert (last_unchanged_at_beg_row == NULL
13044 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13045
13046 /* If this is a continuation line, move forward to the next one
13047 that isn't. Changes in lines above affect this line.
13048 Caution: this may move first_unchanged_at_end_row to a row
13049 not displaying text. */
13050 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13051 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13052 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13053 < it.last_visible_y))
13054 ++first_unchanged_at_end_row;
13055
13056 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13057 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13058 >= it.last_visible_y))
13059 first_unchanged_at_end_row = NULL;
13060 else
13061 {
13062 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13063 + delta);
13064 first_unchanged_at_end_vpos
13065 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13066 xassert (stop_pos >= Z - END_UNCHANGED);
13067 }
13068 }
13069 else if (last_unchanged_at_beg_row == NULL)
13070 GIVE_UP (19);
13071
13072
13073 #if GLYPH_DEBUG
13074
13075 /* Either there is no unchanged row at the end, or the one we have
13076 now displays text. This is a necessary condition for the window
13077 end pos calculation at the end of this function. */
13078 xassert (first_unchanged_at_end_row == NULL
13079 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13080
13081 debug_last_unchanged_at_beg_vpos
13082 = (last_unchanged_at_beg_row
13083 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13084 : -1);
13085 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13086
13087 #endif /* GLYPH_DEBUG != 0 */
13088
13089
13090 /* Display new lines. Set last_text_row to the last new line
13091 displayed which has text on it, i.e. might end up as being the
13092 line where the window_end_vpos is. */
13093 w->cursor.vpos = -1;
13094 last_text_row = NULL;
13095 overlay_arrow_seen = 0;
13096 while (it.current_y < it.last_visible_y
13097 && !fonts_changed_p
13098 && (first_unchanged_at_end_row == NULL
13099 || IT_CHARPOS (it) < stop_pos))
13100 {
13101 if (display_line (&it))
13102 last_text_row = it.glyph_row - 1;
13103 }
13104
13105 if (fonts_changed_p)
13106 return -1;
13107
13108
13109 /* Compute differences in buffer positions, y-positions etc. for
13110 lines reused at the bottom of the window. Compute what we can
13111 scroll. */
13112 if (first_unchanged_at_end_row
13113 /* No lines reused because we displayed everything up to the
13114 bottom of the window. */
13115 && it.current_y < it.last_visible_y)
13116 {
13117 dvpos = (it.vpos
13118 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13119 current_matrix));
13120 dy = it.current_y - first_unchanged_at_end_row->y;
13121 run.current_y = first_unchanged_at_end_row->y;
13122 run.desired_y = run.current_y + dy;
13123 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13124 }
13125 else
13126 {
13127 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13128 first_unchanged_at_end_row = NULL;
13129 }
13130 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13131
13132
13133 /* Find the cursor if not already found. We have to decide whether
13134 PT will appear on this window (it sometimes doesn't, but this is
13135 not a very frequent case.) This decision has to be made before
13136 the current matrix is altered. A value of cursor.vpos < 0 means
13137 that PT is either in one of the lines beginning at
13138 first_unchanged_at_end_row or below the window. Don't care for
13139 lines that might be displayed later at the window end; as
13140 mentioned, this is not a frequent case. */
13141 if (w->cursor.vpos < 0)
13142 {
13143 /* Cursor in unchanged rows at the top? */
13144 if (PT < CHARPOS (start_pos)
13145 && last_unchanged_at_beg_row)
13146 {
13147 row = row_containing_pos (w, PT,
13148 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13149 last_unchanged_at_beg_row + 1, 0);
13150 if (row)
13151 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13152 }
13153
13154 /* Start from first_unchanged_at_end_row looking for PT. */
13155 else if (first_unchanged_at_end_row)
13156 {
13157 row = row_containing_pos (w, PT - delta,
13158 first_unchanged_at_end_row, NULL, 0);
13159 if (row)
13160 set_cursor_from_row (w, row, w->current_matrix, delta,
13161 delta_bytes, dy, dvpos);
13162 }
13163
13164 /* Give up if cursor was not found. */
13165 if (w->cursor.vpos < 0)
13166 {
13167 clear_glyph_matrix (w->desired_matrix);
13168 return -1;
13169 }
13170 }
13171
13172 /* Don't let the cursor end in the scroll margins. */
13173 {
13174 int this_scroll_margin, cursor_height;
13175
13176 this_scroll_margin = max (0, scroll_margin);
13177 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13178 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13179 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13180
13181 if ((w->cursor.y < this_scroll_margin
13182 && CHARPOS (start) > BEGV)
13183 /* Don't take scroll margin into account at the bottom because
13184 old redisplay didn't do it either. */
13185 || w->cursor.y + cursor_height > it.last_visible_y)
13186 {
13187 w->cursor.vpos = -1;
13188 clear_glyph_matrix (w->desired_matrix);
13189 return -1;
13190 }
13191 }
13192
13193 /* Scroll the display. Do it before changing the current matrix so
13194 that xterm.c doesn't get confused about where the cursor glyph is
13195 found. */
13196 if (dy && run.height)
13197 {
13198 update_begin (f);
13199
13200 if (FRAME_WINDOW_P (f))
13201 {
13202 rif->update_window_begin_hook (w);
13203 rif->clear_window_mouse_face (w);
13204 rif->scroll_run_hook (w, &run);
13205 rif->update_window_end_hook (w, 0, 0);
13206 }
13207 else
13208 {
13209 /* Terminal frame. In this case, dvpos gives the number of
13210 lines to scroll by; dvpos < 0 means scroll up. */
13211 int first_unchanged_at_end_vpos
13212 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13213 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13214 int end = (WINDOW_TOP_EDGE_LINE (w)
13215 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13216 + window_internal_height (w));
13217
13218 /* Perform the operation on the screen. */
13219 if (dvpos > 0)
13220 {
13221 /* Scroll last_unchanged_at_beg_row to the end of the
13222 window down dvpos lines. */
13223 set_terminal_window (end);
13224
13225 /* On dumb terminals delete dvpos lines at the end
13226 before inserting dvpos empty lines. */
13227 if (!scroll_region_ok)
13228 ins_del_lines (end - dvpos, -dvpos);
13229
13230 /* Insert dvpos empty lines in front of
13231 last_unchanged_at_beg_row. */
13232 ins_del_lines (from, dvpos);
13233 }
13234 else if (dvpos < 0)
13235 {
13236 /* Scroll up last_unchanged_at_beg_vpos to the end of
13237 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13238 set_terminal_window (end);
13239
13240 /* Delete dvpos lines in front of
13241 last_unchanged_at_beg_vpos. ins_del_lines will set
13242 the cursor to the given vpos and emit |dvpos| delete
13243 line sequences. */
13244 ins_del_lines (from + dvpos, dvpos);
13245
13246 /* On a dumb terminal insert dvpos empty lines at the
13247 end. */
13248 if (!scroll_region_ok)
13249 ins_del_lines (end + dvpos, -dvpos);
13250 }
13251
13252 set_terminal_window (0);
13253 }
13254
13255 update_end (f);
13256 }
13257
13258 /* Shift reused rows of the current matrix to the right position.
13259 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13260 text. */
13261 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13262 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
13263 if (dvpos < 0)
13264 {
13265 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13266 bottom_vpos, dvpos);
13267 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13268 bottom_vpos, 0);
13269 }
13270 else if (dvpos > 0)
13271 {
13272 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13273 bottom_vpos, dvpos);
13274 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13275 first_unchanged_at_end_vpos + dvpos, 0);
13276 }
13277
13278 /* For frame-based redisplay, make sure that current frame and window
13279 matrix are in sync with respect to glyph memory. */
13280 if (!FRAME_WINDOW_P (f))
13281 sync_frame_with_window_matrix_rows (w);
13282
13283 /* Adjust buffer positions in reused rows. */
13284 if (delta)
13285 increment_matrix_positions (current_matrix,
13286 first_unchanged_at_end_vpos + dvpos,
13287 bottom_vpos, delta, delta_bytes);
13288
13289 /* Adjust Y positions. */
13290 if (dy)
13291 shift_glyph_matrix (w, current_matrix,
13292 first_unchanged_at_end_vpos + dvpos,
13293 bottom_vpos, dy);
13294
13295 if (first_unchanged_at_end_row)
13296 first_unchanged_at_end_row += dvpos;
13297
13298 /* If scrolling up, there may be some lines to display at the end of
13299 the window. */
13300 last_text_row_at_end = NULL;
13301 if (dy < 0)
13302 {
13303 /* Scrolling up can leave for example a partially visible line
13304 at the end of the window to be redisplayed. */
13305 /* Set last_row to the glyph row in the current matrix where the
13306 window end line is found. It has been moved up or down in
13307 the matrix by dvpos. */
13308 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13309 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13310
13311 /* If last_row is the window end line, it should display text. */
13312 xassert (last_row->displays_text_p);
13313
13314 /* If window end line was partially visible before, begin
13315 displaying at that line. Otherwise begin displaying with the
13316 line following it. */
13317 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13318 {
13319 init_to_row_start (&it, w, last_row);
13320 it.vpos = last_vpos;
13321 it.current_y = last_row->y;
13322 }
13323 else
13324 {
13325 init_to_row_end (&it, w, last_row);
13326 it.vpos = 1 + last_vpos;
13327 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13328 ++last_row;
13329 }
13330
13331 /* We may start in a continuation line. If so, we have to
13332 get the right continuation_lines_width and current_x. */
13333 it.continuation_lines_width = last_row->continuation_lines_width;
13334 it.hpos = it.current_x = 0;
13335
13336 /* Display the rest of the lines at the window end. */
13337 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13338 while (it.current_y < it.last_visible_y
13339 && !fonts_changed_p)
13340 {
13341 /* Is it always sure that the display agrees with lines in
13342 the current matrix? I don't think so, so we mark rows
13343 displayed invalid in the current matrix by setting their
13344 enabled_p flag to zero. */
13345 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13346 if (display_line (&it))
13347 last_text_row_at_end = it.glyph_row - 1;
13348 }
13349 }
13350
13351 /* Update window_end_pos and window_end_vpos. */
13352 if (first_unchanged_at_end_row
13353 && first_unchanged_at_end_row->y < it.last_visible_y
13354 && !last_text_row_at_end)
13355 {
13356 /* Window end line if one of the preserved rows from the current
13357 matrix. Set row to the last row displaying text in current
13358 matrix starting at first_unchanged_at_end_row, after
13359 scrolling. */
13360 xassert (first_unchanged_at_end_row->displays_text_p);
13361 row = find_last_row_displaying_text (w->current_matrix, &it,
13362 first_unchanged_at_end_row);
13363 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
13364
13365 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13366 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13367 w->window_end_vpos
13368 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
13369 xassert (w->window_end_bytepos >= 0);
13370 IF_DEBUG (debug_method_add (w, "A"));
13371 }
13372 else if (last_text_row_at_end)
13373 {
13374 w->window_end_pos
13375 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
13376 w->window_end_bytepos
13377 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
13378 w->window_end_vpos
13379 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
13380 xassert (w->window_end_bytepos >= 0);
13381 IF_DEBUG (debug_method_add (w, "B"));
13382 }
13383 else if (last_text_row)
13384 {
13385 /* We have displayed either to the end of the window or at the
13386 end of the window, i.e. the last row with text is to be found
13387 in the desired matrix. */
13388 w->window_end_pos
13389 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13390 w->window_end_bytepos
13391 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13392 w->window_end_vpos
13393 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
13394 xassert (w->window_end_bytepos >= 0);
13395 }
13396 else if (first_unchanged_at_end_row == NULL
13397 && last_text_row == NULL
13398 && last_text_row_at_end == NULL)
13399 {
13400 /* Displayed to end of window, but no line containing text was
13401 displayed. Lines were deleted at the end of the window. */
13402 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
13403 int vpos = XFASTINT (w->window_end_vpos);
13404 struct glyph_row *current_row = current_matrix->rows + vpos;
13405 struct glyph_row *desired_row = desired_matrix->rows + vpos;
13406
13407 for (row = NULL;
13408 row == NULL && vpos >= first_vpos;
13409 --vpos, --current_row, --desired_row)
13410 {
13411 if (desired_row->enabled_p)
13412 {
13413 if (desired_row->displays_text_p)
13414 row = desired_row;
13415 }
13416 else if (current_row->displays_text_p)
13417 row = current_row;
13418 }
13419
13420 xassert (row != NULL);
13421 w->window_end_vpos = make_number (vpos + 1);
13422 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13423 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13424 xassert (w->window_end_bytepos >= 0);
13425 IF_DEBUG (debug_method_add (w, "C"));
13426 }
13427 else
13428 abort ();
13429
13430 #if 0 /* This leads to problems, for instance when the cursor is
13431 at ZV, and the cursor line displays no text. */
13432 /* Disable rows below what's displayed in the window. This makes
13433 debugging easier. */
13434 enable_glyph_matrix_rows (current_matrix,
13435 XFASTINT (w->window_end_vpos) + 1,
13436 bottom_vpos, 0);
13437 #endif
13438
13439 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
13440 debug_end_vpos = XFASTINT (w->window_end_vpos));
13441
13442 /* Record that display has not been completed. */
13443 w->window_end_valid = Qnil;
13444 w->desired_matrix->no_scrolling_p = 1;
13445 return 3;
13446
13447 #undef GIVE_UP
13448 }
13449
13450
13451 \f
13452 /***********************************************************************
13453 More debugging support
13454 ***********************************************************************/
13455
13456 #if GLYPH_DEBUG
13457
13458 void dump_glyph_row P_ ((struct glyph_row *, int, int));
13459 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
13460 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
13461
13462
13463 /* Dump the contents of glyph matrix MATRIX on stderr.
13464
13465 GLYPHS 0 means don't show glyph contents.
13466 GLYPHS 1 means show glyphs in short form
13467 GLYPHS > 1 means show glyphs in long form. */
13468
13469 void
13470 dump_glyph_matrix (matrix, glyphs)
13471 struct glyph_matrix *matrix;
13472 int glyphs;
13473 {
13474 int i;
13475 for (i = 0; i < matrix->nrows; ++i)
13476 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
13477 }
13478
13479
13480 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
13481 the glyph row and area where the glyph comes from. */
13482
13483 void
13484 dump_glyph (row, glyph, area)
13485 struct glyph_row *row;
13486 struct glyph *glyph;
13487 int area;
13488 {
13489 if (glyph->type == CHAR_GLYPH)
13490 {
13491 fprintf (stderr,
13492 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13493 glyph - row->glyphs[TEXT_AREA],
13494 'C',
13495 glyph->charpos,
13496 (BUFFERP (glyph->object)
13497 ? 'B'
13498 : (STRINGP (glyph->object)
13499 ? 'S'
13500 : '-')),
13501 glyph->pixel_width,
13502 glyph->u.ch,
13503 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
13504 ? glyph->u.ch
13505 : '.'),
13506 glyph->face_id,
13507 glyph->left_box_line_p,
13508 glyph->right_box_line_p);
13509 }
13510 else if (glyph->type == STRETCH_GLYPH)
13511 {
13512 fprintf (stderr,
13513 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13514 glyph - row->glyphs[TEXT_AREA],
13515 'S',
13516 glyph->charpos,
13517 (BUFFERP (glyph->object)
13518 ? 'B'
13519 : (STRINGP (glyph->object)
13520 ? 'S'
13521 : '-')),
13522 glyph->pixel_width,
13523 0,
13524 '.',
13525 glyph->face_id,
13526 glyph->left_box_line_p,
13527 glyph->right_box_line_p);
13528 }
13529 else if (glyph->type == IMAGE_GLYPH)
13530 {
13531 fprintf (stderr,
13532 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13533 glyph - row->glyphs[TEXT_AREA],
13534 'I',
13535 glyph->charpos,
13536 (BUFFERP (glyph->object)
13537 ? 'B'
13538 : (STRINGP (glyph->object)
13539 ? 'S'
13540 : '-')),
13541 glyph->pixel_width,
13542 glyph->u.img_id,
13543 '.',
13544 glyph->face_id,
13545 glyph->left_box_line_p,
13546 glyph->right_box_line_p);
13547 }
13548 }
13549
13550
13551 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
13552 GLYPHS 0 means don't show glyph contents.
13553 GLYPHS 1 means show glyphs in short form
13554 GLYPHS > 1 means show glyphs in long form. */
13555
13556 void
13557 dump_glyph_row (row, vpos, glyphs)
13558 struct glyph_row *row;
13559 int vpos, glyphs;
13560 {
13561 if (glyphs != 1)
13562 {
13563 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
13564 fprintf (stderr, "=======================================================================\n");
13565
13566 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
13567 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
13568 vpos,
13569 MATRIX_ROW_START_CHARPOS (row),
13570 MATRIX_ROW_END_CHARPOS (row),
13571 row->used[TEXT_AREA],
13572 row->contains_overlapping_glyphs_p,
13573 row->enabled_p,
13574 row->truncated_on_left_p,
13575 row->truncated_on_right_p,
13576 row->overlay_arrow_p,
13577 row->continued_p,
13578 MATRIX_ROW_CONTINUATION_LINE_P (row),
13579 row->displays_text_p,
13580 row->ends_at_zv_p,
13581 row->fill_line_p,
13582 row->ends_in_middle_of_char_p,
13583 row->starts_in_middle_of_char_p,
13584 row->mouse_face_p,
13585 row->x,
13586 row->y,
13587 row->pixel_width,
13588 row->height,
13589 row->visible_height,
13590 row->ascent,
13591 row->phys_ascent);
13592 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
13593 row->end.overlay_string_index,
13594 row->continuation_lines_width);
13595 fprintf (stderr, "%9d %5d\n",
13596 CHARPOS (row->start.string_pos),
13597 CHARPOS (row->end.string_pos));
13598 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
13599 row->end.dpvec_index);
13600 }
13601
13602 if (glyphs > 1)
13603 {
13604 int area;
13605
13606 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13607 {
13608 struct glyph *glyph = row->glyphs[area];
13609 struct glyph *glyph_end = glyph + row->used[area];
13610
13611 /* Glyph for a line end in text. */
13612 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
13613 ++glyph_end;
13614
13615 if (glyph < glyph_end)
13616 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
13617
13618 for (; glyph < glyph_end; ++glyph)
13619 dump_glyph (row, glyph, area);
13620 }
13621 }
13622 else if (glyphs == 1)
13623 {
13624 int area;
13625
13626 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13627 {
13628 char *s = (char *) alloca (row->used[area] + 1);
13629 int i;
13630
13631 for (i = 0; i < row->used[area]; ++i)
13632 {
13633 struct glyph *glyph = row->glyphs[area] + i;
13634 if (glyph->type == CHAR_GLYPH
13635 && glyph->u.ch < 0x80
13636 && glyph->u.ch >= ' ')
13637 s[i] = glyph->u.ch;
13638 else
13639 s[i] = '.';
13640 }
13641
13642 s[i] = '\0';
13643 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
13644 }
13645 }
13646 }
13647
13648
13649 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
13650 Sdump_glyph_matrix, 0, 1, "p",
13651 doc: /* Dump the current matrix of the selected window to stderr.
13652 Shows contents of glyph row structures. With non-nil
13653 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
13654 glyphs in short form, otherwise show glyphs in long form. */)
13655 (glyphs)
13656 Lisp_Object glyphs;
13657 {
13658 struct window *w = XWINDOW (selected_window);
13659 struct buffer *buffer = XBUFFER (w->buffer);
13660
13661 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
13662 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
13663 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
13664 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
13665 fprintf (stderr, "=============================================\n");
13666 dump_glyph_matrix (w->current_matrix,
13667 NILP (glyphs) ? 0 : XINT (glyphs));
13668 return Qnil;
13669 }
13670
13671
13672 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
13673 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
13674 ()
13675 {
13676 struct frame *f = XFRAME (selected_frame);
13677 dump_glyph_matrix (f->current_matrix, 1);
13678 return Qnil;
13679 }
13680
13681
13682 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
13683 doc: /* Dump glyph row ROW to stderr.
13684 GLYPH 0 means don't dump glyphs.
13685 GLYPH 1 means dump glyphs in short form.
13686 GLYPH > 1 or omitted means dump glyphs in long form. */)
13687 (row, glyphs)
13688 Lisp_Object row, glyphs;
13689 {
13690 struct glyph_matrix *matrix;
13691 int vpos;
13692
13693 CHECK_NUMBER (row);
13694 matrix = XWINDOW (selected_window)->current_matrix;
13695 vpos = XINT (row);
13696 if (vpos >= 0 && vpos < matrix->nrows)
13697 dump_glyph_row (MATRIX_ROW (matrix, vpos),
13698 vpos,
13699 INTEGERP (glyphs) ? XINT (glyphs) : 2);
13700 return Qnil;
13701 }
13702
13703
13704 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
13705 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
13706 GLYPH 0 means don't dump glyphs.
13707 GLYPH 1 means dump glyphs in short form.
13708 GLYPH > 1 or omitted means dump glyphs in long form. */)
13709 (row, glyphs)
13710 Lisp_Object row, glyphs;
13711 {
13712 struct frame *sf = SELECTED_FRAME ();
13713 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
13714 int vpos;
13715
13716 CHECK_NUMBER (row);
13717 vpos = XINT (row);
13718 if (vpos >= 0 && vpos < m->nrows)
13719 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
13720 INTEGERP (glyphs) ? XINT (glyphs) : 2);
13721 return Qnil;
13722 }
13723
13724
13725 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
13726 doc: /* Toggle tracing of redisplay.
13727 With ARG, turn tracing on if and only if ARG is positive. */)
13728 (arg)
13729 Lisp_Object arg;
13730 {
13731 if (NILP (arg))
13732 trace_redisplay_p = !trace_redisplay_p;
13733 else
13734 {
13735 arg = Fprefix_numeric_value (arg);
13736 trace_redisplay_p = XINT (arg) > 0;
13737 }
13738
13739 return Qnil;
13740 }
13741
13742
13743 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
13744 doc: /* Like `format', but print result to stderr.
13745 usage: (trace-to-stderr STRING &rest OBJECTS) */)
13746 (nargs, args)
13747 int nargs;
13748 Lisp_Object *args;
13749 {
13750 Lisp_Object s = Fformat (nargs, args);
13751 fprintf (stderr, "%s", SDATA (s));
13752 return Qnil;
13753 }
13754
13755 #endif /* GLYPH_DEBUG */
13756
13757
13758 \f
13759 /***********************************************************************
13760 Building Desired Matrix Rows
13761 ***********************************************************************/
13762
13763 /* Return a temporary glyph row holding the glyphs of an overlay
13764 arrow. Only used for non-window-redisplay windows. */
13765
13766 static struct glyph_row *
13767 get_overlay_arrow_glyph_row (w)
13768 struct window *w;
13769 {
13770 struct frame *f = XFRAME (WINDOW_FRAME (w));
13771 struct buffer *buffer = XBUFFER (w->buffer);
13772 struct buffer *old = current_buffer;
13773 const unsigned char *arrow_string = SDATA (Voverlay_arrow_string);
13774 int arrow_len = SCHARS (Voverlay_arrow_string);
13775 const unsigned char *arrow_end = arrow_string + arrow_len;
13776 const unsigned char *p;
13777 struct it it;
13778 int multibyte_p;
13779 int n_glyphs_before;
13780
13781 set_buffer_temp (buffer);
13782 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
13783 it.glyph_row->used[TEXT_AREA] = 0;
13784 SET_TEXT_POS (it.position, 0, 0);
13785
13786 multibyte_p = !NILP (buffer->enable_multibyte_characters);
13787 p = arrow_string;
13788 while (p < arrow_end)
13789 {
13790 Lisp_Object face, ilisp;
13791
13792 /* Get the next character. */
13793 if (multibyte_p)
13794 it.c = string_char_and_length (p, arrow_len, &it.len);
13795 else
13796 it.c = *p, it.len = 1;
13797 p += it.len;
13798
13799 /* Get its face. */
13800 ilisp = make_number (p - arrow_string);
13801 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
13802 it.face_id = compute_char_face (f, it.c, face);
13803
13804 /* Compute its width, get its glyphs. */
13805 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
13806 SET_TEXT_POS (it.position, -1, -1);
13807 PRODUCE_GLYPHS (&it);
13808
13809 /* If this character doesn't fit any more in the line, we have
13810 to remove some glyphs. */
13811 if (it.current_x > it.last_visible_x)
13812 {
13813 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
13814 break;
13815 }
13816 }
13817
13818 set_buffer_temp (old);
13819 return it.glyph_row;
13820 }
13821
13822
13823 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
13824 glyphs are only inserted for terminal frames since we can't really
13825 win with truncation glyphs when partially visible glyphs are
13826 involved. Which glyphs to insert is determined by
13827 produce_special_glyphs. */
13828
13829 static void
13830 insert_left_trunc_glyphs (it)
13831 struct it *it;
13832 {
13833 struct it truncate_it;
13834 struct glyph *from, *end, *to, *toend;
13835
13836 xassert (!FRAME_WINDOW_P (it->f));
13837
13838 /* Get the truncation glyphs. */
13839 truncate_it = *it;
13840 truncate_it.current_x = 0;
13841 truncate_it.face_id = DEFAULT_FACE_ID;
13842 truncate_it.glyph_row = &scratch_glyph_row;
13843 truncate_it.glyph_row->used[TEXT_AREA] = 0;
13844 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
13845 truncate_it.object = make_number (0);
13846 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
13847
13848 /* Overwrite glyphs from IT with truncation glyphs. */
13849 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
13850 end = from + truncate_it.glyph_row->used[TEXT_AREA];
13851 to = it->glyph_row->glyphs[TEXT_AREA];
13852 toend = to + it->glyph_row->used[TEXT_AREA];
13853
13854 while (from < end)
13855 *to++ = *from++;
13856
13857 /* There may be padding glyphs left over. Overwrite them too. */
13858 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
13859 {
13860 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
13861 while (from < end)
13862 *to++ = *from++;
13863 }
13864
13865 if (to > toend)
13866 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
13867 }
13868
13869
13870 /* Compute the pixel height and width of IT->glyph_row.
13871
13872 Most of the time, ascent and height of a display line will be equal
13873 to the max_ascent and max_height values of the display iterator
13874 structure. This is not the case if
13875
13876 1. We hit ZV without displaying anything. In this case, max_ascent
13877 and max_height will be zero.
13878
13879 2. We have some glyphs that don't contribute to the line height.
13880 (The glyph row flag contributes_to_line_height_p is for future
13881 pixmap extensions).
13882
13883 The first case is easily covered by using default values because in
13884 these cases, the line height does not really matter, except that it
13885 must not be zero. */
13886
13887 static void
13888 compute_line_metrics (it)
13889 struct it *it;
13890 {
13891 struct glyph_row *row = it->glyph_row;
13892 int area, i;
13893
13894 if (FRAME_WINDOW_P (it->f))
13895 {
13896 int i, min_y, max_y;
13897
13898 /* The line may consist of one space only, that was added to
13899 place the cursor on it. If so, the row's height hasn't been
13900 computed yet. */
13901 if (row->height == 0)
13902 {
13903 if (it->max_ascent + it->max_descent == 0)
13904 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
13905 row->ascent = it->max_ascent;
13906 row->height = it->max_ascent + it->max_descent;
13907 row->phys_ascent = it->max_phys_ascent;
13908 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
13909 }
13910
13911 /* Compute the width of this line. */
13912 row->pixel_width = row->x;
13913 for (i = 0; i < row->used[TEXT_AREA]; ++i)
13914 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
13915
13916 xassert (row->pixel_width >= 0);
13917 xassert (row->ascent >= 0 && row->height > 0);
13918
13919 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
13920 || MATRIX_ROW_OVERLAPS_PRED_P (row));
13921
13922 /* If first line's physical ascent is larger than its logical
13923 ascent, use the physical ascent, and make the row taller.
13924 This makes accented characters fully visible. */
13925 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
13926 && row->phys_ascent > row->ascent)
13927 {
13928 row->height += row->phys_ascent - row->ascent;
13929 row->ascent = row->phys_ascent;
13930 }
13931
13932 /* Compute how much of the line is visible. */
13933 row->visible_height = row->height;
13934
13935 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
13936 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
13937
13938 if (row->y < min_y)
13939 row->visible_height -= min_y - row->y;
13940 if (row->y + row->height > max_y)
13941 row->visible_height -= row->y + row->height - max_y;
13942 }
13943 else
13944 {
13945 row->pixel_width = row->used[TEXT_AREA];
13946 if (row->continued_p)
13947 row->pixel_width -= it->continuation_pixel_width;
13948 else if (row->truncated_on_right_p)
13949 row->pixel_width -= it->truncation_pixel_width;
13950 row->ascent = row->phys_ascent = 0;
13951 row->height = row->phys_height = row->visible_height = 1;
13952 }
13953
13954 /* Compute a hash code for this row. */
13955 row->hash = 0;
13956 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13957 for (i = 0; i < row->used[area]; ++i)
13958 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
13959 + row->glyphs[area][i].u.val
13960 + row->glyphs[area][i].face_id
13961 + row->glyphs[area][i].padding_p
13962 + (row->glyphs[area][i].type << 2));
13963
13964 it->max_ascent = it->max_descent = 0;
13965 it->max_phys_ascent = it->max_phys_descent = 0;
13966 }
13967
13968
13969 /* Append one space to the glyph row of iterator IT if doing a
13970 window-based redisplay. DEFAULT_FACE_P non-zero means let the
13971 space have the default face, otherwise let it have the same face as
13972 IT->face_id. Value is non-zero if a space was added.
13973
13974 This function is called to make sure that there is always one glyph
13975 at the end of a glyph row that the cursor can be set on under
13976 window-systems. (If there weren't such a glyph we would not know
13977 how wide and tall a box cursor should be displayed).
13978
13979 At the same time this space let's a nicely handle clearing to the
13980 end of the line if the row ends in italic text. */
13981
13982 static int
13983 append_space (it, default_face_p)
13984 struct it *it;
13985 int default_face_p;
13986 {
13987 if (FRAME_WINDOW_P (it->f))
13988 {
13989 int n = it->glyph_row->used[TEXT_AREA];
13990
13991 if (it->glyph_row->glyphs[TEXT_AREA] + n
13992 < it->glyph_row->glyphs[1 + TEXT_AREA])
13993 {
13994 /* Save some values that must not be changed.
13995 Must save IT->c and IT->len because otherwise
13996 ITERATOR_AT_END_P wouldn't work anymore after
13997 append_space has been called. */
13998 enum display_element_type saved_what = it->what;
13999 int saved_c = it->c, saved_len = it->len;
14000 int saved_x = it->current_x;
14001 int saved_face_id = it->face_id;
14002 struct text_pos saved_pos;
14003 Lisp_Object saved_object;
14004 struct face *face;
14005
14006 saved_object = it->object;
14007 saved_pos = it->position;
14008
14009 it->what = IT_CHARACTER;
14010 bzero (&it->position, sizeof it->position);
14011 it->object = make_number (0);
14012 it->c = ' ';
14013 it->len = 1;
14014
14015 if (default_face_p)
14016 it->face_id = DEFAULT_FACE_ID;
14017 else if (it->face_before_selective_p)
14018 it->face_id = it->saved_face_id;
14019 face = FACE_FROM_ID (it->f, it->face_id);
14020 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14021
14022 PRODUCE_GLYPHS (it);
14023
14024 it->current_x = saved_x;
14025 it->object = saved_object;
14026 it->position = saved_pos;
14027 it->what = saved_what;
14028 it->face_id = saved_face_id;
14029 it->len = saved_len;
14030 it->c = saved_c;
14031 return 1;
14032 }
14033 }
14034
14035 return 0;
14036 }
14037
14038
14039 /* Extend the face of the last glyph in the text area of IT->glyph_row
14040 to the end of the display line. Called from display_line.
14041 If the glyph row is empty, add a space glyph to it so that we
14042 know the face to draw. Set the glyph row flag fill_line_p. */
14043
14044 static void
14045 extend_face_to_end_of_line (it)
14046 struct it *it;
14047 {
14048 struct face *face;
14049 struct frame *f = it->f;
14050
14051 /* If line is already filled, do nothing. */
14052 if (it->current_x >= it->last_visible_x)
14053 return;
14054
14055 /* Face extension extends the background and box of IT->face_id
14056 to the end of the line. If the background equals the background
14057 of the frame, we don't have to do anything. */
14058 if (it->face_before_selective_p)
14059 face = FACE_FROM_ID (it->f, it->saved_face_id);
14060 else
14061 face = FACE_FROM_ID (f, it->face_id);
14062
14063 if (FRAME_WINDOW_P (f)
14064 && face->box == FACE_NO_BOX
14065 && face->background == FRAME_BACKGROUND_PIXEL (f)
14066 && !face->stipple)
14067 return;
14068
14069 /* Set the glyph row flag indicating that the face of the last glyph
14070 in the text area has to be drawn to the end of the text area. */
14071 it->glyph_row->fill_line_p = 1;
14072
14073 /* If current character of IT is not ASCII, make sure we have the
14074 ASCII face. This will be automatically undone the next time
14075 get_next_display_element returns a multibyte character. Note
14076 that the character will always be single byte in unibyte text. */
14077 if (!SINGLE_BYTE_CHAR_P (it->c))
14078 {
14079 it->face_id = FACE_FOR_CHAR (f, face, 0);
14080 }
14081
14082 if (FRAME_WINDOW_P (f))
14083 {
14084 /* If the row is empty, add a space with the current face of IT,
14085 so that we know which face to draw. */
14086 if (it->glyph_row->used[TEXT_AREA] == 0)
14087 {
14088 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14089 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14090 it->glyph_row->used[TEXT_AREA] = 1;
14091 }
14092 }
14093 else
14094 {
14095 /* Save some values that must not be changed. */
14096 int saved_x = it->current_x;
14097 struct text_pos saved_pos;
14098 Lisp_Object saved_object;
14099 enum display_element_type saved_what = it->what;
14100 int saved_face_id = it->face_id;
14101
14102 saved_object = it->object;
14103 saved_pos = it->position;
14104
14105 it->what = IT_CHARACTER;
14106 bzero (&it->position, sizeof it->position);
14107 it->object = make_number (0);
14108 it->c = ' ';
14109 it->len = 1;
14110 it->face_id = face->id;
14111
14112 PRODUCE_GLYPHS (it);
14113
14114 while (it->current_x <= it->last_visible_x)
14115 PRODUCE_GLYPHS (it);
14116
14117 /* Don't count these blanks really. It would let us insert a left
14118 truncation glyph below and make us set the cursor on them, maybe. */
14119 it->current_x = saved_x;
14120 it->object = saved_object;
14121 it->position = saved_pos;
14122 it->what = saved_what;
14123 it->face_id = saved_face_id;
14124 }
14125 }
14126
14127
14128 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14129 trailing whitespace. */
14130
14131 static int
14132 trailing_whitespace_p (charpos)
14133 int charpos;
14134 {
14135 int bytepos = CHAR_TO_BYTE (charpos);
14136 int c = 0;
14137
14138 while (bytepos < ZV_BYTE
14139 && (c = FETCH_CHAR (bytepos),
14140 c == ' ' || c == '\t'))
14141 ++bytepos;
14142
14143 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14144 {
14145 if (bytepos != PT_BYTE)
14146 return 1;
14147 }
14148 return 0;
14149 }
14150
14151
14152 /* Highlight trailing whitespace, if any, in ROW. */
14153
14154 void
14155 highlight_trailing_whitespace (f, row)
14156 struct frame *f;
14157 struct glyph_row *row;
14158 {
14159 int used = row->used[TEXT_AREA];
14160
14161 if (used)
14162 {
14163 struct glyph *start = row->glyphs[TEXT_AREA];
14164 struct glyph *glyph = start + used - 1;
14165
14166 /* Skip over glyphs inserted to display the cursor at the
14167 end of a line, for extending the face of the last glyph
14168 to the end of the line on terminals, and for truncation
14169 and continuation glyphs. */
14170 while (glyph >= start
14171 && glyph->type == CHAR_GLYPH
14172 && INTEGERP (glyph->object))
14173 --glyph;
14174
14175 /* If last glyph is a space or stretch, and it's trailing
14176 whitespace, set the face of all trailing whitespace glyphs in
14177 IT->glyph_row to `trailing-whitespace'. */
14178 if (glyph >= start
14179 && BUFFERP (glyph->object)
14180 && (glyph->type == STRETCH_GLYPH
14181 || (glyph->type == CHAR_GLYPH
14182 && glyph->u.ch == ' '))
14183 && trailing_whitespace_p (glyph->charpos))
14184 {
14185 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
14186
14187 while (glyph >= start
14188 && BUFFERP (glyph->object)
14189 && (glyph->type == STRETCH_GLYPH
14190 || (glyph->type == CHAR_GLYPH
14191 && glyph->u.ch == ' ')))
14192 (glyph--)->face_id = face_id;
14193 }
14194 }
14195 }
14196
14197
14198 /* Value is non-zero if glyph row ROW in window W should be
14199 used to hold the cursor. */
14200
14201 static int
14202 cursor_row_p (w, row)
14203 struct window *w;
14204 struct glyph_row *row;
14205 {
14206 int cursor_row_p = 1;
14207
14208 if (PT == MATRIX_ROW_END_CHARPOS (row))
14209 {
14210 /* If the row ends with a newline from a string, we don't want
14211 the cursor there (if the row is continued it doesn't end in a
14212 newline). */
14213 if (CHARPOS (row->end.string_pos) >= 0
14214 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14215 cursor_row_p = row->continued_p;
14216
14217 /* If the row ends at ZV, display the cursor at the end of that
14218 row instead of at the start of the row below. */
14219 else if (row->ends_at_zv_p)
14220 cursor_row_p = 1;
14221 else
14222 cursor_row_p = 0;
14223 }
14224
14225 return cursor_row_p;
14226 }
14227
14228
14229 /* Construct the glyph row IT->glyph_row in the desired matrix of
14230 IT->w from text at the current position of IT. See dispextern.h
14231 for an overview of struct it. Value is non-zero if
14232 IT->glyph_row displays text, as opposed to a line displaying ZV
14233 only. */
14234
14235 static int
14236 display_line (it)
14237 struct it *it;
14238 {
14239 struct glyph_row *row = it->glyph_row;
14240
14241 /* We always start displaying at hpos zero even if hscrolled. */
14242 xassert (it->hpos == 0 && it->current_x == 0);
14243
14244 /* We must not display in a row that's not a text row. */
14245 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14246 < it->w->desired_matrix->nrows);
14247
14248 /* Is IT->w showing the region? */
14249 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
14250
14251 /* Clear the result glyph row and enable it. */
14252 prepare_desired_row (row);
14253
14254 row->y = it->current_y;
14255 row->start = it->current;
14256 row->continuation_lines_width = it->continuation_lines_width;
14257 row->displays_text_p = 1;
14258 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14259 it->starts_in_middle_of_char_p = 0;
14260
14261 /* Arrange the overlays nicely for our purposes. Usually, we call
14262 display_line on only one line at a time, in which case this
14263 can't really hurt too much, or we call it on lines which appear
14264 one after another in the buffer, in which case all calls to
14265 recenter_overlay_lists but the first will be pretty cheap. */
14266 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14267
14268 /* Move over display elements that are not visible because we are
14269 hscrolled. This may stop at an x-position < IT->first_visible_x
14270 if the first glyph is partially visible or if we hit a line end. */
14271 if (it->current_x < it->first_visible_x)
14272 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14273 MOVE_TO_POS | MOVE_TO_X);
14274
14275 /* Get the initial row height. This is either the height of the
14276 text hscrolled, if there is any, or zero. */
14277 row->ascent = it->max_ascent;
14278 row->height = it->max_ascent + it->max_descent;
14279 row->phys_ascent = it->max_phys_ascent;
14280 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14281
14282 /* Loop generating characters. The loop is left with IT on the next
14283 character to display. */
14284 while (1)
14285 {
14286 int n_glyphs_before, hpos_before, x_before;
14287 int x, i, nglyphs;
14288 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
14289
14290 /* Retrieve the next thing to display. Value is zero if end of
14291 buffer reached. */
14292 if (!get_next_display_element (it))
14293 {
14294 /* Maybe add a space at the end of this line that is used to
14295 display the cursor there under X. Set the charpos of the
14296 first glyph of blank lines not corresponding to any text
14297 to -1. */
14298 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
14299 || row->used[TEXT_AREA] == 0)
14300 {
14301 row->glyphs[TEXT_AREA]->charpos = -1;
14302 row->displays_text_p = 0;
14303
14304 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
14305 && (!MINI_WINDOW_P (it->w)
14306 || (minibuf_level && EQ (it->window, minibuf_window))))
14307 row->indicate_empty_line_p = 1;
14308 }
14309
14310 it->continuation_lines_width = 0;
14311 row->ends_at_zv_p = 1;
14312 break;
14313 }
14314
14315 /* Now, get the metrics of what we want to display. This also
14316 generates glyphs in `row' (which is IT->glyph_row). */
14317 n_glyphs_before = row->used[TEXT_AREA];
14318 x = it->current_x;
14319
14320 /* Remember the line height so far in case the next element doesn't
14321 fit on the line. */
14322 if (!it->truncate_lines_p)
14323 {
14324 ascent = it->max_ascent;
14325 descent = it->max_descent;
14326 phys_ascent = it->max_phys_ascent;
14327 phys_descent = it->max_phys_descent;
14328 }
14329
14330 PRODUCE_GLYPHS (it);
14331
14332 /* If this display element was in marginal areas, continue with
14333 the next one. */
14334 if (it->area != TEXT_AREA)
14335 {
14336 row->ascent = max (row->ascent, it->max_ascent);
14337 row->height = max (row->height, it->max_ascent + it->max_descent);
14338 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14339 row->phys_height = max (row->phys_height,
14340 it->max_phys_ascent + it->max_phys_descent);
14341 set_iterator_to_next (it, 1);
14342 continue;
14343 }
14344
14345 /* Does the display element fit on the line? If we truncate
14346 lines, we should draw past the right edge of the window. If
14347 we don't truncate, we want to stop so that we can display the
14348 continuation glyph before the right margin. If lines are
14349 continued, there are two possible strategies for characters
14350 resulting in more than 1 glyph (e.g. tabs): Display as many
14351 glyphs as possible in this line and leave the rest for the
14352 continuation line, or display the whole element in the next
14353 line. Original redisplay did the former, so we do it also. */
14354 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14355 hpos_before = it->hpos;
14356 x_before = x;
14357
14358 if (/* Not a newline. */
14359 nglyphs > 0
14360 /* Glyphs produced fit entirely in the line. */
14361 && it->current_x < it->last_visible_x)
14362 {
14363 it->hpos += nglyphs;
14364 row->ascent = max (row->ascent, it->max_ascent);
14365 row->height = max (row->height, it->max_ascent + it->max_descent);
14366 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14367 row->phys_height = max (row->phys_height,
14368 it->max_phys_ascent + it->max_phys_descent);
14369 if (it->current_x - it->pixel_width < it->first_visible_x)
14370 row->x = x - it->first_visible_x;
14371 }
14372 else
14373 {
14374 int new_x;
14375 struct glyph *glyph;
14376
14377 for (i = 0; i < nglyphs; ++i, x = new_x)
14378 {
14379 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14380 new_x = x + glyph->pixel_width;
14381
14382 if (/* Lines are continued. */
14383 !it->truncate_lines_p
14384 && (/* Glyph doesn't fit on the line. */
14385 new_x > it->last_visible_x
14386 /* Or it fits exactly on a window system frame. */
14387 || (new_x == it->last_visible_x
14388 && FRAME_WINDOW_P (it->f))))
14389 {
14390 /* End of a continued line. */
14391
14392 if (it->hpos == 0
14393 || (new_x == it->last_visible_x
14394 && FRAME_WINDOW_P (it->f)))
14395 {
14396 /* Current glyph is the only one on the line or
14397 fits exactly on the line. We must continue
14398 the line because we can't draw the cursor
14399 after the glyph. */
14400 row->continued_p = 1;
14401 it->current_x = new_x;
14402 it->continuation_lines_width += new_x;
14403 ++it->hpos;
14404 if (i == nglyphs - 1)
14405 set_iterator_to_next (it, 1);
14406 }
14407 else if (CHAR_GLYPH_PADDING_P (*glyph)
14408 && !FRAME_WINDOW_P (it->f))
14409 {
14410 /* A padding glyph that doesn't fit on this line.
14411 This means the whole character doesn't fit
14412 on the line. */
14413 row->used[TEXT_AREA] = n_glyphs_before;
14414
14415 /* Fill the rest of the row with continuation
14416 glyphs like in 20.x. */
14417 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
14418 < row->glyphs[1 + TEXT_AREA])
14419 produce_special_glyphs (it, IT_CONTINUATION);
14420
14421 row->continued_p = 1;
14422 it->current_x = x_before;
14423 it->continuation_lines_width += x_before;
14424
14425 /* Restore the height to what it was before the
14426 element not fitting on the line. */
14427 it->max_ascent = ascent;
14428 it->max_descent = descent;
14429 it->max_phys_ascent = phys_ascent;
14430 it->max_phys_descent = phys_descent;
14431 }
14432 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
14433 {
14434 /* A TAB that extends past the right edge of the
14435 window. This produces a single glyph on
14436 window system frames. We leave the glyph in
14437 this row and let it fill the row, but don't
14438 consume the TAB. */
14439 it->continuation_lines_width += it->last_visible_x;
14440 row->ends_in_middle_of_char_p = 1;
14441 row->continued_p = 1;
14442 glyph->pixel_width = it->last_visible_x - x;
14443 it->starts_in_middle_of_char_p = 1;
14444 }
14445 else
14446 {
14447 /* Something other than a TAB that draws past
14448 the right edge of the window. Restore
14449 positions to values before the element. */
14450 row->used[TEXT_AREA] = n_glyphs_before + i;
14451
14452 /* Display continuation glyphs. */
14453 if (!FRAME_WINDOW_P (it->f))
14454 produce_special_glyphs (it, IT_CONTINUATION);
14455 row->continued_p = 1;
14456
14457 it->continuation_lines_width += x;
14458
14459 if (nglyphs > 1 && i > 0)
14460 {
14461 row->ends_in_middle_of_char_p = 1;
14462 it->starts_in_middle_of_char_p = 1;
14463 }
14464
14465 /* Restore the height to what it was before the
14466 element not fitting on the line. */
14467 it->max_ascent = ascent;
14468 it->max_descent = descent;
14469 it->max_phys_ascent = phys_ascent;
14470 it->max_phys_descent = phys_descent;
14471 }
14472
14473 break;
14474 }
14475 else if (new_x > it->first_visible_x)
14476 {
14477 /* Increment number of glyphs actually displayed. */
14478 ++it->hpos;
14479
14480 if (x < it->first_visible_x)
14481 /* Glyph is partially visible, i.e. row starts at
14482 negative X position. */
14483 row->x = x - it->first_visible_x;
14484 }
14485 else
14486 {
14487 /* Glyph is completely off the left margin of the
14488 window. This should not happen because of the
14489 move_it_in_display_line at the start of this
14490 function, unless the text display area of the
14491 window is empty. */
14492 xassert (it->first_visible_x <= it->last_visible_x);
14493 }
14494 }
14495
14496 row->ascent = max (row->ascent, it->max_ascent);
14497 row->height = max (row->height, it->max_ascent + it->max_descent);
14498 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14499 row->phys_height = max (row->phys_height,
14500 it->max_phys_ascent + it->max_phys_descent);
14501
14502 /* End of this display line if row is continued. */
14503 if (row->continued_p)
14504 break;
14505 }
14506
14507 /* Is this a line end? If yes, we're also done, after making
14508 sure that a non-default face is extended up to the right
14509 margin of the window. */
14510 if (ITERATOR_AT_END_OF_LINE_P (it))
14511 {
14512 int used_before = row->used[TEXT_AREA];
14513
14514 row->ends_in_newline_from_string_p = STRINGP (it->object);
14515
14516 /* Add a space at the end of the line that is used to
14517 display the cursor there. */
14518 append_space (it, 0);
14519
14520 /* Extend the face to the end of the line. */
14521 extend_face_to_end_of_line (it);
14522
14523 /* Make sure we have the position. */
14524 if (used_before == 0)
14525 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
14526
14527 /* Consume the line end. This skips over invisible lines. */
14528 set_iterator_to_next (it, 1);
14529 it->continuation_lines_width = 0;
14530 break;
14531 }
14532
14533 /* Proceed with next display element. Note that this skips
14534 over lines invisible because of selective display. */
14535 set_iterator_to_next (it, 1);
14536
14537 /* If we truncate lines, we are done when the last displayed
14538 glyphs reach past the right margin of the window. */
14539 if (it->truncate_lines_p
14540 && (FRAME_WINDOW_P (it->f)
14541 ? (it->current_x >= it->last_visible_x)
14542 : (it->current_x > it->last_visible_x)))
14543 {
14544 /* Maybe add truncation glyphs. */
14545 if (!FRAME_WINDOW_P (it->f))
14546 {
14547 int i, n;
14548
14549 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14550 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14551 break;
14552
14553 for (n = row->used[TEXT_AREA]; i < n; ++i)
14554 {
14555 row->used[TEXT_AREA] = i;
14556 produce_special_glyphs (it, IT_TRUNCATION);
14557 }
14558 }
14559
14560 row->truncated_on_right_p = 1;
14561 it->continuation_lines_width = 0;
14562 reseat_at_next_visible_line_start (it, 0);
14563 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
14564 it->hpos = hpos_before;
14565 it->current_x = x_before;
14566 break;
14567 }
14568 }
14569
14570 /* If line is not empty and hscrolled, maybe insert truncation glyphs
14571 at the left window margin. */
14572 if (it->first_visible_x
14573 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
14574 {
14575 if (!FRAME_WINDOW_P (it->f))
14576 insert_left_trunc_glyphs (it);
14577 row->truncated_on_left_p = 1;
14578 }
14579
14580 /* If the start of this line is the overlay arrow-position, then
14581 mark this glyph row as the one containing the overlay arrow.
14582 This is clearly a mess with variable size fonts. It would be
14583 better to let it be displayed like cursors under X. */
14584 if (MARKERP (Voverlay_arrow_position)
14585 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
14586 && (MATRIX_ROW_START_CHARPOS (row)
14587 == marker_position (Voverlay_arrow_position))
14588 && STRINGP (Voverlay_arrow_string)
14589 && ! overlay_arrow_seen)
14590 {
14591 /* Overlay arrow in window redisplay is a fringe bitmap. */
14592 if (!FRAME_WINDOW_P (it->f))
14593 {
14594 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
14595 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
14596 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
14597 struct glyph *p = row->glyphs[TEXT_AREA];
14598 struct glyph *p2, *end;
14599
14600 /* Copy the arrow glyphs. */
14601 while (glyph < arrow_end)
14602 *p++ = *glyph++;
14603
14604 /* Throw away padding glyphs. */
14605 p2 = p;
14606 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
14607 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
14608 ++p2;
14609 if (p2 > p)
14610 {
14611 while (p2 < end)
14612 *p++ = *p2++;
14613 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
14614 }
14615 }
14616
14617 overlay_arrow_seen = 1;
14618 row->overlay_arrow_p = 1;
14619 }
14620
14621 /* Compute pixel dimensions of this line. */
14622 compute_line_metrics (it);
14623
14624 /* Remember the position at which this line ends. */
14625 row->end = it->current;
14626
14627 /* Maybe set the cursor. */
14628 if (it->w->cursor.vpos < 0
14629 && PT >= MATRIX_ROW_START_CHARPOS (row)
14630 && PT <= MATRIX_ROW_END_CHARPOS (row)
14631 && cursor_row_p (it->w, row))
14632 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
14633
14634 /* Highlight trailing whitespace. */
14635 if (!NILP (Vshow_trailing_whitespace))
14636 highlight_trailing_whitespace (it->f, it->glyph_row);
14637
14638 /* Prepare for the next line. This line starts horizontally at (X
14639 HPOS) = (0 0). Vertical positions are incremented. As a
14640 convenience for the caller, IT->glyph_row is set to the next
14641 row to be used. */
14642 it->current_x = it->hpos = 0;
14643 it->current_y += row->height;
14644 ++it->vpos;
14645 ++it->glyph_row;
14646 return row->displays_text_p;
14647 }
14648
14649
14650 \f
14651 /***********************************************************************
14652 Menu Bar
14653 ***********************************************************************/
14654
14655 /* Redisplay the menu bar in the frame for window W.
14656
14657 The menu bar of X frames that don't have X toolkit support is
14658 displayed in a special window W->frame->menu_bar_window.
14659
14660 The menu bar of terminal frames is treated specially as far as
14661 glyph matrices are concerned. Menu bar lines are not part of
14662 windows, so the update is done directly on the frame matrix rows
14663 for the menu bar. */
14664
14665 static void
14666 display_menu_bar (w)
14667 struct window *w;
14668 {
14669 struct frame *f = XFRAME (WINDOW_FRAME (w));
14670 struct it it;
14671 Lisp_Object items;
14672 int i;
14673
14674 /* Don't do all this for graphical frames. */
14675 #ifdef HAVE_NTGUI
14676 if (!NILP (Vwindow_system))
14677 return;
14678 #endif
14679 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
14680 if (FRAME_X_P (f))
14681 return;
14682 #endif
14683 #ifdef MAC_OS
14684 if (FRAME_MAC_P (f))
14685 return;
14686 #endif
14687
14688 #ifdef USE_X_TOOLKIT
14689 xassert (!FRAME_WINDOW_P (f));
14690 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
14691 it.first_visible_x = 0;
14692 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
14693 #else /* not USE_X_TOOLKIT */
14694 if (FRAME_WINDOW_P (f))
14695 {
14696 /* Menu bar lines are displayed in the desired matrix of the
14697 dummy window menu_bar_window. */
14698 struct window *menu_w;
14699 xassert (WINDOWP (f->menu_bar_window));
14700 menu_w = XWINDOW (f->menu_bar_window);
14701 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
14702 MENU_FACE_ID);
14703 it.first_visible_x = 0;
14704 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
14705 }
14706 else
14707 {
14708 /* This is a TTY frame, i.e. character hpos/vpos are used as
14709 pixel x/y. */
14710 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
14711 MENU_FACE_ID);
14712 it.first_visible_x = 0;
14713 it.last_visible_x = FRAME_COLS (f);
14714 }
14715 #endif /* not USE_X_TOOLKIT */
14716
14717 if (! mode_line_inverse_video)
14718 /* Force the menu-bar to be displayed in the default face. */
14719 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
14720
14721 /* Clear all rows of the menu bar. */
14722 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
14723 {
14724 struct glyph_row *row = it.glyph_row + i;
14725 clear_glyph_row (row);
14726 row->enabled_p = 1;
14727 row->full_width_p = 1;
14728 }
14729
14730 /* Display all items of the menu bar. */
14731 items = FRAME_MENU_BAR_ITEMS (it.f);
14732 for (i = 0; i < XVECTOR (items)->size; i += 4)
14733 {
14734 Lisp_Object string;
14735
14736 /* Stop at nil string. */
14737 string = AREF (items, i + 1);
14738 if (NILP (string))
14739 break;
14740
14741 /* Remember where item was displayed. */
14742 AREF (items, i + 3) = make_number (it.hpos);
14743
14744 /* Display the item, pad with one space. */
14745 if (it.current_x < it.last_visible_x)
14746 display_string (NULL, string, Qnil, 0, 0, &it,
14747 SCHARS (string) + 1, 0, 0, -1);
14748 }
14749
14750 /* Fill out the line with spaces. */
14751 if (it.current_x < it.last_visible_x)
14752 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
14753
14754 /* Compute the total height of the lines. */
14755 compute_line_metrics (&it);
14756 }
14757
14758
14759 \f
14760 /***********************************************************************
14761 Mode Line
14762 ***********************************************************************/
14763
14764 /* Redisplay mode lines in the window tree whose root is WINDOW. If
14765 FORCE is non-zero, redisplay mode lines unconditionally.
14766 Otherwise, redisplay only mode lines that are garbaged. Value is
14767 the number of windows whose mode lines were redisplayed. */
14768
14769 static int
14770 redisplay_mode_lines (window, force)
14771 Lisp_Object window;
14772 int force;
14773 {
14774 int nwindows = 0;
14775
14776 while (!NILP (window))
14777 {
14778 struct window *w = XWINDOW (window);
14779
14780 if (WINDOWP (w->hchild))
14781 nwindows += redisplay_mode_lines (w->hchild, force);
14782 else if (WINDOWP (w->vchild))
14783 nwindows += redisplay_mode_lines (w->vchild, force);
14784 else if (force
14785 || FRAME_GARBAGED_P (XFRAME (w->frame))
14786 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
14787 {
14788 struct text_pos lpoint;
14789 struct buffer *old = current_buffer;
14790
14791 /* Set the window's buffer for the mode line display. */
14792 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14793 set_buffer_internal_1 (XBUFFER (w->buffer));
14794
14795 /* Point refers normally to the selected window. For any
14796 other window, set up appropriate value. */
14797 if (!EQ (window, selected_window))
14798 {
14799 struct text_pos pt;
14800
14801 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
14802 if (CHARPOS (pt) < BEGV)
14803 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
14804 else if (CHARPOS (pt) > (ZV - 1))
14805 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
14806 else
14807 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
14808 }
14809
14810 /* Display mode lines. */
14811 clear_glyph_matrix (w->desired_matrix);
14812 if (display_mode_lines (w))
14813 {
14814 ++nwindows;
14815 w->must_be_updated_p = 1;
14816 }
14817
14818 /* Restore old settings. */
14819 set_buffer_internal_1 (old);
14820 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14821 }
14822
14823 window = w->next;
14824 }
14825
14826 return nwindows;
14827 }
14828
14829
14830 /* Display the mode and/or top line of window W. Value is the number
14831 of mode lines displayed. */
14832
14833 static int
14834 display_mode_lines (w)
14835 struct window *w;
14836 {
14837 Lisp_Object old_selected_window, old_selected_frame;
14838 int n = 0;
14839
14840 old_selected_frame = selected_frame;
14841 selected_frame = w->frame;
14842 old_selected_window = selected_window;
14843 XSETWINDOW (selected_window, w);
14844
14845 /* These will be set while the mode line specs are processed. */
14846 line_number_displayed = 0;
14847 w->column_number_displayed = Qnil;
14848
14849 if (WINDOW_WANTS_MODELINE_P (w))
14850 {
14851 struct window *sel_w = XWINDOW (old_selected_window);
14852
14853 /* Select mode line face based on the real selected window. */
14854 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
14855 current_buffer->mode_line_format);
14856 ++n;
14857 }
14858
14859 if (WINDOW_WANTS_HEADER_LINE_P (w))
14860 {
14861 display_mode_line (w, HEADER_LINE_FACE_ID,
14862 current_buffer->header_line_format);
14863 ++n;
14864 }
14865
14866 selected_frame = old_selected_frame;
14867 selected_window = old_selected_window;
14868 return n;
14869 }
14870
14871
14872 /* Display mode or top line of window W. FACE_ID specifies which line
14873 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
14874 FORMAT is the mode line format to display. Value is the pixel
14875 height of the mode line displayed. */
14876
14877 static int
14878 display_mode_line (w, face_id, format)
14879 struct window *w;
14880 enum face_id face_id;
14881 Lisp_Object format;
14882 {
14883 struct it it;
14884 struct face *face;
14885
14886 init_iterator (&it, w, -1, -1, NULL, face_id);
14887 prepare_desired_row (it.glyph_row);
14888
14889 if (! mode_line_inverse_video)
14890 /* Force the mode-line to be displayed in the default face. */
14891 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
14892
14893 /* Temporarily make frame's keyboard the current kboard so that
14894 kboard-local variables in the mode_line_format will get the right
14895 values. */
14896 push_frame_kboard (it.f);
14897 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
14898 pop_frame_kboard ();
14899
14900 /* Fill up with spaces. */
14901 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
14902
14903 compute_line_metrics (&it);
14904 it.glyph_row->full_width_p = 1;
14905 it.glyph_row->mode_line_p = 1;
14906 it.glyph_row->continued_p = 0;
14907 it.glyph_row->truncated_on_left_p = 0;
14908 it.glyph_row->truncated_on_right_p = 0;
14909
14910 /* Make a 3D mode-line have a shadow at its right end. */
14911 face = FACE_FROM_ID (it.f, face_id);
14912 extend_face_to_end_of_line (&it);
14913 if (face->box != FACE_NO_BOX)
14914 {
14915 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
14916 + it.glyph_row->used[TEXT_AREA] - 1);
14917 last->right_box_line_p = 1;
14918 }
14919
14920 return it.glyph_row->height;
14921 }
14922
14923 /* Alist that caches the results of :propertize.
14924 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
14925 Lisp_Object mode_line_proptrans_alist;
14926
14927 /* List of strings making up the mode-line. */
14928 Lisp_Object mode_line_string_list;
14929
14930 /* Base face property when building propertized mode line string. */
14931 static Lisp_Object mode_line_string_face;
14932 static Lisp_Object mode_line_string_face_prop;
14933
14934
14935 /* Contribute ELT to the mode line for window IT->w. How it
14936 translates into text depends on its data type.
14937
14938 IT describes the display environment in which we display, as usual.
14939
14940 DEPTH is the depth in recursion. It is used to prevent
14941 infinite recursion here.
14942
14943 FIELD_WIDTH is the number of characters the display of ELT should
14944 occupy in the mode line, and PRECISION is the maximum number of
14945 characters to display from ELT's representation. See
14946 display_string for details.
14947
14948 Returns the hpos of the end of the text generated by ELT.
14949
14950 PROPS is a property list to add to any string we encounter.
14951
14952 If RISKY is nonzero, remove (disregard) any properties in any string
14953 we encounter, and ignore :eval and :propertize.
14954
14955 If the global variable `frame_title_ptr' is non-NULL, then the output
14956 is passed to `store_frame_title' instead of `display_string'. */
14957
14958 static int
14959 display_mode_element (it, depth, field_width, precision, elt, props, risky)
14960 struct it *it;
14961 int depth;
14962 int field_width, precision;
14963 Lisp_Object elt, props;
14964 int risky;
14965 {
14966 int n = 0, field, prec;
14967 int literal = 0;
14968
14969 tail_recurse:
14970 if (depth > 100)
14971 elt = build_string ("*too-deep*");
14972
14973 depth++;
14974
14975 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
14976 {
14977 case Lisp_String:
14978 {
14979 /* A string: output it and check for %-constructs within it. */
14980 unsigned char c;
14981 const unsigned char *this, *lisp_string;
14982
14983 if (!NILP (props) || risky)
14984 {
14985 Lisp_Object oprops, aelt;
14986 oprops = Ftext_properties_at (make_number (0), elt);
14987
14988 if (NILP (Fequal (props, oprops)) || risky)
14989 {
14990 /* If the starting string has properties,
14991 merge the specified ones onto the existing ones. */
14992 if (! NILP (oprops) && !risky)
14993 {
14994 Lisp_Object tem;
14995
14996 oprops = Fcopy_sequence (oprops);
14997 tem = props;
14998 while (CONSP (tem))
14999 {
15000 oprops = Fplist_put (oprops, XCAR (tem),
15001 XCAR (XCDR (tem)));
15002 tem = XCDR (XCDR (tem));
15003 }
15004 props = oprops;
15005 }
15006
15007 aelt = Fassoc (elt, mode_line_proptrans_alist);
15008 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15009 {
15010 mode_line_proptrans_alist
15011 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15012 elt = XCAR (aelt);
15013 }
15014 else
15015 {
15016 Lisp_Object tem;
15017
15018 elt = Fcopy_sequence (elt);
15019 Fset_text_properties (make_number (0), Flength (elt),
15020 props, elt);
15021 /* Add this item to mode_line_proptrans_alist. */
15022 mode_line_proptrans_alist
15023 = Fcons (Fcons (elt, props),
15024 mode_line_proptrans_alist);
15025 /* Truncate mode_line_proptrans_alist
15026 to at most 50 elements. */
15027 tem = Fnthcdr (make_number (50),
15028 mode_line_proptrans_alist);
15029 if (! NILP (tem))
15030 XSETCDR (tem, Qnil);
15031 }
15032 }
15033 }
15034
15035 this = SDATA (elt);
15036 lisp_string = this;
15037
15038 if (literal)
15039 {
15040 prec = precision - n;
15041 if (frame_title_ptr)
15042 n += store_frame_title (SDATA (elt), -1, prec);
15043 else if (!NILP (mode_line_string_list))
15044 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15045 else
15046 n += display_string (NULL, elt, Qnil, 0, 0, it,
15047 0, prec, 0, STRING_MULTIBYTE (elt));
15048
15049 break;
15050 }
15051
15052 while ((precision <= 0 || n < precision)
15053 && *this
15054 && (frame_title_ptr
15055 || !NILP (mode_line_string_list)
15056 || it->current_x < it->last_visible_x))
15057 {
15058 const unsigned char *last = this;
15059
15060 /* Advance to end of string or next format specifier. */
15061 while ((c = *this++) != '\0' && c != '%')
15062 ;
15063
15064 if (this - 1 != last)
15065 {
15066 /* Output to end of string or up to '%'. Field width
15067 is length of string. Don't output more than
15068 PRECISION allows us. */
15069 --this;
15070
15071 prec = chars_in_text (last, this - last);
15072 if (precision > 0 && prec > precision - n)
15073 prec = precision - n;
15074
15075 if (frame_title_ptr)
15076 n += store_frame_title (last, 0, prec);
15077 else if (!NILP (mode_line_string_list))
15078 {
15079 int bytepos = last - lisp_string;
15080 int charpos = string_byte_to_char (elt, bytepos);
15081 n += store_mode_line_string (NULL,
15082 Fsubstring (elt, make_number (charpos),
15083 make_number (charpos + prec)),
15084 0, 0, 0, Qnil);
15085 }
15086 else
15087 {
15088 int bytepos = last - lisp_string;
15089 int charpos = string_byte_to_char (elt, bytepos);
15090 n += display_string (NULL, elt, Qnil, 0, charpos,
15091 it, 0, prec, 0,
15092 STRING_MULTIBYTE (elt));
15093 }
15094 }
15095 else /* c == '%' */
15096 {
15097 const unsigned char *percent_position = this;
15098
15099 /* Get the specified minimum width. Zero means
15100 don't pad. */
15101 field = 0;
15102 while ((c = *this++) >= '0' && c <= '9')
15103 field = field * 10 + c - '0';
15104
15105 /* Don't pad beyond the total padding allowed. */
15106 if (field_width - n > 0 && field > field_width - n)
15107 field = field_width - n;
15108
15109 /* Note that either PRECISION <= 0 or N < PRECISION. */
15110 prec = precision - n;
15111
15112 if (c == 'M')
15113 n += display_mode_element (it, depth, field, prec,
15114 Vglobal_mode_string, props,
15115 risky);
15116 else if (c != 0)
15117 {
15118 int multibyte;
15119 int bytepos, charpos;
15120 unsigned char *spec;
15121
15122 bytepos = percent_position - lisp_string;
15123 charpos = (STRING_MULTIBYTE (elt)
15124 ? string_byte_to_char (elt, bytepos)
15125 : bytepos);
15126
15127 spec
15128 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15129
15130 if (frame_title_ptr)
15131 n += store_frame_title (spec, field, prec);
15132 else if (!NILP (mode_line_string_list))
15133 {
15134 int len = strlen (spec);
15135 Lisp_Object tem = make_string (spec, len);
15136 props = Ftext_properties_at (make_number (charpos), elt);
15137 /* Should only keep face property in props */
15138 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15139 }
15140 else
15141 {
15142 int nglyphs_before, nwritten;
15143
15144 nglyphs_before = it->glyph_row->used[TEXT_AREA];
15145 nwritten = display_string (spec, Qnil, elt,
15146 charpos, 0, it,
15147 field, prec, 0,
15148 multibyte);
15149
15150 /* Assign to the glyphs written above the
15151 string where the `%x' came from, position
15152 of the `%'. */
15153 if (nwritten > 0)
15154 {
15155 struct glyph *glyph
15156 = (it->glyph_row->glyphs[TEXT_AREA]
15157 + nglyphs_before);
15158 int i;
15159
15160 for (i = 0; i < nwritten; ++i)
15161 {
15162 glyph[i].object = elt;
15163 glyph[i].charpos = charpos;
15164 }
15165
15166 n += nwritten;
15167 }
15168 }
15169 }
15170 else /* c == 0 */
15171 break;
15172 }
15173 }
15174 }
15175 break;
15176
15177 case Lisp_Symbol:
15178 /* A symbol: process the value of the symbol recursively
15179 as if it appeared here directly. Avoid error if symbol void.
15180 Special case: if value of symbol is a string, output the string
15181 literally. */
15182 {
15183 register Lisp_Object tem;
15184
15185 /* If the variable is not marked as risky to set
15186 then its contents are risky to use. */
15187 if (NILP (Fget (elt, Qrisky_local_variable)))
15188 risky = 1;
15189
15190 tem = Fboundp (elt);
15191 if (!NILP (tem))
15192 {
15193 tem = Fsymbol_value (elt);
15194 /* If value is a string, output that string literally:
15195 don't check for % within it. */
15196 if (STRINGP (tem))
15197 literal = 1;
15198
15199 if (!EQ (tem, elt))
15200 {
15201 /* Give up right away for nil or t. */
15202 elt = tem;
15203 goto tail_recurse;
15204 }
15205 }
15206 }
15207 break;
15208
15209 case Lisp_Cons:
15210 {
15211 register Lisp_Object car, tem;
15212
15213 /* A cons cell: five distinct cases.
15214 If first element is :eval or :propertize, do something special.
15215 If first element is a string or a cons, process all the elements
15216 and effectively concatenate them.
15217 If first element is a negative number, truncate displaying cdr to
15218 at most that many characters. If positive, pad (with spaces)
15219 to at least that many characters.
15220 If first element is a symbol, process the cadr or caddr recursively
15221 according to whether the symbol's value is non-nil or nil. */
15222 car = XCAR (elt);
15223 if (EQ (car, QCeval))
15224 {
15225 /* An element of the form (:eval FORM) means evaluate FORM
15226 and use the result as mode line elements. */
15227
15228 if (risky)
15229 break;
15230
15231 if (CONSP (XCDR (elt)))
15232 {
15233 Lisp_Object spec;
15234 spec = safe_eval (XCAR (XCDR (elt)));
15235 n += display_mode_element (it, depth, field_width - n,
15236 precision - n, spec, props,
15237 risky);
15238 }
15239 }
15240 else if (EQ (car, QCpropertize))
15241 {
15242 /* An element of the form (:propertize ELT PROPS...)
15243 means display ELT but applying properties PROPS. */
15244
15245 if (risky)
15246 break;
15247
15248 if (CONSP (XCDR (elt)))
15249 n += display_mode_element (it, depth, field_width - n,
15250 precision - n, XCAR (XCDR (elt)),
15251 XCDR (XCDR (elt)), risky);
15252 }
15253 else if (SYMBOLP (car))
15254 {
15255 tem = Fboundp (car);
15256 elt = XCDR (elt);
15257 if (!CONSP (elt))
15258 goto invalid;
15259 /* elt is now the cdr, and we know it is a cons cell.
15260 Use its car if CAR has a non-nil value. */
15261 if (!NILP (tem))
15262 {
15263 tem = Fsymbol_value (car);
15264 if (!NILP (tem))
15265 {
15266 elt = XCAR (elt);
15267 goto tail_recurse;
15268 }
15269 }
15270 /* Symbol's value is nil (or symbol is unbound)
15271 Get the cddr of the original list
15272 and if possible find the caddr and use that. */
15273 elt = XCDR (elt);
15274 if (NILP (elt))
15275 break;
15276 else if (!CONSP (elt))
15277 goto invalid;
15278 elt = XCAR (elt);
15279 goto tail_recurse;
15280 }
15281 else if (INTEGERP (car))
15282 {
15283 register int lim = XINT (car);
15284 elt = XCDR (elt);
15285 if (lim < 0)
15286 {
15287 /* Negative int means reduce maximum width. */
15288 if (precision <= 0)
15289 precision = -lim;
15290 else
15291 precision = min (precision, -lim);
15292 }
15293 else if (lim > 0)
15294 {
15295 /* Padding specified. Don't let it be more than
15296 current maximum. */
15297 if (precision > 0)
15298 lim = min (precision, lim);
15299
15300 /* If that's more padding than already wanted, queue it.
15301 But don't reduce padding already specified even if
15302 that is beyond the current truncation point. */
15303 field_width = max (lim, field_width);
15304 }
15305 goto tail_recurse;
15306 }
15307 else if (STRINGP (car) || CONSP (car))
15308 {
15309 register int limit = 50;
15310 /* Limit is to protect against circular lists. */
15311 while (CONSP (elt)
15312 && --limit > 0
15313 && (precision <= 0 || n < precision))
15314 {
15315 n += display_mode_element (it, depth, field_width - n,
15316 precision - n, XCAR (elt),
15317 props, risky);
15318 elt = XCDR (elt);
15319 }
15320 }
15321 }
15322 break;
15323
15324 default:
15325 invalid:
15326 elt = build_string ("*invalid*");
15327 goto tail_recurse;
15328 }
15329
15330 /* Pad to FIELD_WIDTH. */
15331 if (field_width > 0 && n < field_width)
15332 {
15333 if (frame_title_ptr)
15334 n += store_frame_title ("", field_width - n, 0);
15335 else if (!NILP (mode_line_string_list))
15336 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
15337 else
15338 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
15339 0, 0, 0);
15340 }
15341
15342 return n;
15343 }
15344
15345 /* Store a mode-line string element in mode_line_string_list.
15346
15347 If STRING is non-null, display that C string. Otherwise, the Lisp
15348 string LISP_STRING is displayed.
15349
15350 FIELD_WIDTH is the minimum number of output glyphs to produce.
15351 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15352 with spaces. FIELD_WIDTH <= 0 means don't pad.
15353
15354 PRECISION is the maximum number of characters to output from
15355 STRING. PRECISION <= 0 means don't truncate the string.
15356
15357 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
15358 properties to the string.
15359
15360 PROPS are the properties to add to the string.
15361 The mode_line_string_face face property is always added to the string.
15362 */
15363
15364 static int store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
15365 char *string;
15366 Lisp_Object lisp_string;
15367 int copy_string;
15368 int field_width;
15369 int precision;
15370 Lisp_Object props;
15371 {
15372 int len;
15373 int n = 0;
15374
15375 if (string != NULL)
15376 {
15377 len = strlen (string);
15378 if (precision > 0 && len > precision)
15379 len = precision;
15380 lisp_string = make_string (string, len);
15381 if (NILP (props))
15382 props = mode_line_string_face_prop;
15383 else if (!NILP (mode_line_string_face))
15384 {
15385 Lisp_Object face = Fplist_get (props, Qface);
15386 props = Fcopy_sequence (props);
15387 if (NILP (face))
15388 face = mode_line_string_face;
15389 else
15390 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15391 props = Fplist_put (props, Qface, face);
15392 }
15393 Fadd_text_properties (make_number (0), make_number (len),
15394 props, lisp_string);
15395 }
15396 else
15397 {
15398 len = XFASTINT (Flength (lisp_string));
15399 if (precision > 0 && len > precision)
15400 {
15401 len = precision;
15402 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
15403 precision = -1;
15404 }
15405 if (!NILP (mode_line_string_face))
15406 {
15407 Lisp_Object face;
15408 if (NILP (props))
15409 props = Ftext_properties_at (make_number (0), lisp_string);
15410 face = Fplist_get (props, Qface);
15411 if (NILP (face))
15412 face = mode_line_string_face;
15413 else
15414 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15415 props = Fcons (Qface, Fcons (face, Qnil));
15416 if (copy_string)
15417 lisp_string = Fcopy_sequence (lisp_string);
15418 }
15419 if (!NILP (props))
15420 Fadd_text_properties (make_number (0), make_number (len),
15421 props, lisp_string);
15422 }
15423
15424 if (len > 0)
15425 {
15426 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15427 n += len;
15428 }
15429
15430 if (field_width > len)
15431 {
15432 field_width -= len;
15433 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
15434 if (!NILP (props))
15435 Fadd_text_properties (make_number (0), make_number (field_width),
15436 props, lisp_string);
15437 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15438 n += field_width;
15439 }
15440
15441 return n;
15442 }
15443
15444
15445 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
15446 0, 3, 0,
15447 doc: /* Return the mode-line of selected window as a string.
15448 First optional arg FORMAT specifies a different format string (see
15449 `mode-line-format' for details) to use. If FORMAT is t, return
15450 the buffer's header-line. Second optional arg WINDOW specifies a
15451 different window to use as the context for the formatting.
15452 If third optional arg NO-PROPS is non-nil, string is not propertized. */)
15453 (format, window, no_props)
15454 Lisp_Object format, window, no_props;
15455 {
15456 struct it it;
15457 int len;
15458 struct window *w;
15459 struct buffer *old_buffer = NULL;
15460 enum face_id face_id = DEFAULT_FACE_ID;
15461
15462 if (NILP (window))
15463 window = selected_window;
15464 CHECK_WINDOW (window);
15465 w = XWINDOW (window);
15466 CHECK_BUFFER (w->buffer);
15467
15468 if (XBUFFER (w->buffer) != current_buffer)
15469 {
15470 old_buffer = current_buffer;
15471 set_buffer_internal_1 (XBUFFER (w->buffer));
15472 }
15473
15474 if (NILP (format) || EQ (format, Qt))
15475 {
15476 face_id = NILP (format)
15477 ? CURRENT_MODE_LINE_FACE_ID (w) :
15478 HEADER_LINE_FACE_ID;
15479 format = NILP (format)
15480 ? current_buffer->mode_line_format
15481 : current_buffer->header_line_format;
15482 }
15483
15484 init_iterator (&it, w, -1, -1, NULL, face_id);
15485
15486 if (NILP (no_props))
15487 {
15488 mode_line_string_face =
15489 (face_id == MODE_LINE_FACE_ID ? Qmode_line :
15490 face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive :
15491 face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
15492
15493 mode_line_string_face_prop =
15494 NILP (mode_line_string_face) ? Qnil :
15495 Fcons (Qface, Fcons (mode_line_string_face, Qnil));
15496
15497 /* We need a dummy last element in mode_line_string_list to
15498 indicate we are building the propertized mode-line string.
15499 Using mode_line_string_face_prop here GC protects it. */
15500 mode_line_string_list =
15501 Fcons (mode_line_string_face_prop, Qnil);
15502 frame_title_ptr = NULL;
15503 }
15504 else
15505 {
15506 mode_line_string_face_prop = Qnil;
15507 mode_line_string_list = Qnil;
15508 frame_title_ptr = frame_title_buf;
15509 }
15510
15511 push_frame_kboard (it.f);
15512 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15513 pop_frame_kboard ();
15514
15515 if (old_buffer)
15516 set_buffer_internal_1 (old_buffer);
15517
15518 if (NILP (no_props))
15519 {
15520 Lisp_Object str;
15521 mode_line_string_list = Fnreverse (mode_line_string_list);
15522 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
15523 make_string ("", 0));
15524 mode_line_string_face_prop = Qnil;
15525 mode_line_string_list = Qnil;
15526 return str;
15527 }
15528
15529 len = frame_title_ptr - frame_title_buf;
15530 if (len > 0 && frame_title_ptr[-1] == '-')
15531 {
15532 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
15533 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
15534 ;
15535 frame_title_ptr += 3; /* restore last non-dash + two dashes */
15536 if (len > frame_title_ptr - frame_title_buf)
15537 len = frame_title_ptr - frame_title_buf;
15538 }
15539
15540 frame_title_ptr = NULL;
15541 return make_string (frame_title_buf, len);
15542 }
15543
15544 /* Write a null-terminated, right justified decimal representation of
15545 the positive integer D to BUF using a minimal field width WIDTH. */
15546
15547 static void
15548 pint2str (buf, width, d)
15549 register char *buf;
15550 register int width;
15551 register int d;
15552 {
15553 register char *p = buf;
15554
15555 if (d <= 0)
15556 *p++ = '0';
15557 else
15558 {
15559 while (d > 0)
15560 {
15561 *p++ = d % 10 + '0';
15562 d /= 10;
15563 }
15564 }
15565
15566 for (width -= (int) (p - buf); width > 0; --width)
15567 *p++ = ' ';
15568 *p-- = '\0';
15569 while (p > buf)
15570 {
15571 d = *buf;
15572 *buf++ = *p;
15573 *p-- = d;
15574 }
15575 }
15576
15577 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
15578 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
15579 type of CODING_SYSTEM. Return updated pointer into BUF. */
15580
15581 static unsigned char invalid_eol_type[] = "(*invalid*)";
15582
15583 static char *
15584 decode_mode_spec_coding (coding_system, buf, eol_flag)
15585 Lisp_Object coding_system;
15586 register char *buf;
15587 int eol_flag;
15588 {
15589 Lisp_Object val;
15590 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
15591 const unsigned char *eol_str;
15592 int eol_str_len;
15593 /* The EOL conversion we are using. */
15594 Lisp_Object eoltype;
15595
15596 val = Fget (coding_system, Qcoding_system);
15597 eoltype = Qnil;
15598
15599 if (!VECTORP (val)) /* Not yet decided. */
15600 {
15601 if (multibyte)
15602 *buf++ = '-';
15603 if (eol_flag)
15604 eoltype = eol_mnemonic_undecided;
15605 /* Don't mention EOL conversion if it isn't decided. */
15606 }
15607 else
15608 {
15609 Lisp_Object eolvalue;
15610
15611 eolvalue = Fget (coding_system, Qeol_type);
15612
15613 if (multibyte)
15614 *buf++ = XFASTINT (AREF (val, 1));
15615
15616 if (eol_flag)
15617 {
15618 /* The EOL conversion that is normal on this system. */
15619
15620 if (NILP (eolvalue)) /* Not yet decided. */
15621 eoltype = eol_mnemonic_undecided;
15622 else if (VECTORP (eolvalue)) /* Not yet decided. */
15623 eoltype = eol_mnemonic_undecided;
15624 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
15625 eoltype = (XFASTINT (eolvalue) == 0
15626 ? eol_mnemonic_unix
15627 : (XFASTINT (eolvalue) == 1
15628 ? eol_mnemonic_dos : eol_mnemonic_mac));
15629 }
15630 }
15631
15632 if (eol_flag)
15633 {
15634 /* Mention the EOL conversion if it is not the usual one. */
15635 if (STRINGP (eoltype))
15636 {
15637 eol_str = SDATA (eoltype);
15638 eol_str_len = SBYTES (eoltype);
15639 }
15640 else if (INTEGERP (eoltype)
15641 && CHAR_VALID_P (XINT (eoltype), 0))
15642 {
15643 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
15644 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
15645 eol_str = tmp;
15646 }
15647 else
15648 {
15649 eol_str = invalid_eol_type;
15650 eol_str_len = sizeof (invalid_eol_type) - 1;
15651 }
15652 bcopy (eol_str, buf, eol_str_len);
15653 buf += eol_str_len;
15654 }
15655
15656 return buf;
15657 }
15658
15659 /* Return a string for the output of a mode line %-spec for window W,
15660 generated by character C. PRECISION >= 0 means don't return a
15661 string longer than that value. FIELD_WIDTH > 0 means pad the
15662 string returned with spaces to that value. Return 1 in *MULTIBYTE
15663 if the result is multibyte text. */
15664
15665 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
15666
15667 static char *
15668 decode_mode_spec (w, c, field_width, precision, multibyte)
15669 struct window *w;
15670 register int c;
15671 int field_width, precision;
15672 int *multibyte;
15673 {
15674 Lisp_Object obj;
15675 struct frame *f = XFRAME (WINDOW_FRAME (w));
15676 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
15677 struct buffer *b = XBUFFER (w->buffer);
15678
15679 obj = Qnil;
15680 *multibyte = 0;
15681
15682 switch (c)
15683 {
15684 case '*':
15685 if (!NILP (b->read_only))
15686 return "%";
15687 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
15688 return "*";
15689 return "-";
15690
15691 case '+':
15692 /* This differs from %* only for a modified read-only buffer. */
15693 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
15694 return "*";
15695 if (!NILP (b->read_only))
15696 return "%";
15697 return "-";
15698
15699 case '&':
15700 /* This differs from %* in ignoring read-only-ness. */
15701 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
15702 return "*";
15703 return "-";
15704
15705 case '%':
15706 return "%";
15707
15708 case '[':
15709 {
15710 int i;
15711 char *p;
15712
15713 if (command_loop_level > 5)
15714 return "[[[... ";
15715 p = decode_mode_spec_buf;
15716 for (i = 0; i < command_loop_level; i++)
15717 *p++ = '[';
15718 *p = 0;
15719 return decode_mode_spec_buf;
15720 }
15721
15722 case ']':
15723 {
15724 int i;
15725 char *p;
15726
15727 if (command_loop_level > 5)
15728 return " ...]]]";
15729 p = decode_mode_spec_buf;
15730 for (i = 0; i < command_loop_level; i++)
15731 *p++ = ']';
15732 *p = 0;
15733 return decode_mode_spec_buf;
15734 }
15735
15736 case '-':
15737 {
15738 register int i;
15739
15740 /* Let lots_of_dashes be a string of infinite length. */
15741 if (!NILP (mode_line_string_list))
15742 return "--";
15743 if (field_width <= 0
15744 || field_width > sizeof (lots_of_dashes))
15745 {
15746 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
15747 decode_mode_spec_buf[i] = '-';
15748 decode_mode_spec_buf[i] = '\0';
15749 return decode_mode_spec_buf;
15750 }
15751 else
15752 return lots_of_dashes;
15753 }
15754
15755 case 'b':
15756 obj = b->name;
15757 break;
15758
15759 case 'c':
15760 {
15761 int col = (int) current_column (); /* iftc */
15762 w->column_number_displayed = make_number (col);
15763 pint2str (decode_mode_spec_buf, field_width, col);
15764 return decode_mode_spec_buf;
15765 }
15766
15767 case 'F':
15768 /* %F displays the frame name. */
15769 if (!NILP (f->title))
15770 return (char *) SDATA (f->title);
15771 if (f->explicit_name || ! FRAME_WINDOW_P (f))
15772 return (char *) SDATA (f->name);
15773 return "Emacs";
15774
15775 case 'f':
15776 obj = b->filename;
15777 break;
15778
15779 case 'l':
15780 {
15781 int startpos = XMARKER (w->start)->charpos;
15782 int startpos_byte = marker_byte_position (w->start);
15783 int line, linepos, linepos_byte, topline;
15784 int nlines, junk;
15785 int height = WINDOW_TOTAL_LINES (w);
15786
15787 /* If we decided that this buffer isn't suitable for line numbers,
15788 don't forget that too fast. */
15789 if (EQ (w->base_line_pos, w->buffer))
15790 goto no_value;
15791 /* But do forget it, if the window shows a different buffer now. */
15792 else if (BUFFERP (w->base_line_pos))
15793 w->base_line_pos = Qnil;
15794
15795 /* If the buffer is very big, don't waste time. */
15796 if (INTEGERP (Vline_number_display_limit)
15797 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
15798 {
15799 w->base_line_pos = Qnil;
15800 w->base_line_number = Qnil;
15801 goto no_value;
15802 }
15803
15804 if (!NILP (w->base_line_number)
15805 && !NILP (w->base_line_pos)
15806 && XFASTINT (w->base_line_pos) <= startpos)
15807 {
15808 line = XFASTINT (w->base_line_number);
15809 linepos = XFASTINT (w->base_line_pos);
15810 linepos_byte = buf_charpos_to_bytepos (b, linepos);
15811 }
15812 else
15813 {
15814 line = 1;
15815 linepos = BUF_BEGV (b);
15816 linepos_byte = BUF_BEGV_BYTE (b);
15817 }
15818
15819 /* Count lines from base line to window start position. */
15820 nlines = display_count_lines (linepos, linepos_byte,
15821 startpos_byte,
15822 startpos, &junk);
15823
15824 topline = nlines + line;
15825
15826 /* Determine a new base line, if the old one is too close
15827 or too far away, or if we did not have one.
15828 "Too close" means it's plausible a scroll-down would
15829 go back past it. */
15830 if (startpos == BUF_BEGV (b))
15831 {
15832 w->base_line_number = make_number (topline);
15833 w->base_line_pos = make_number (BUF_BEGV (b));
15834 }
15835 else if (nlines < height + 25 || nlines > height * 3 + 50
15836 || linepos == BUF_BEGV (b))
15837 {
15838 int limit = BUF_BEGV (b);
15839 int limit_byte = BUF_BEGV_BYTE (b);
15840 int position;
15841 int distance = (height * 2 + 30) * line_number_display_limit_width;
15842
15843 if (startpos - distance > limit)
15844 {
15845 limit = startpos - distance;
15846 limit_byte = CHAR_TO_BYTE (limit);
15847 }
15848
15849 nlines = display_count_lines (startpos, startpos_byte,
15850 limit_byte,
15851 - (height * 2 + 30),
15852 &position);
15853 /* If we couldn't find the lines we wanted within
15854 line_number_display_limit_width chars per line,
15855 give up on line numbers for this window. */
15856 if (position == limit_byte && limit == startpos - distance)
15857 {
15858 w->base_line_pos = w->buffer;
15859 w->base_line_number = Qnil;
15860 goto no_value;
15861 }
15862
15863 w->base_line_number = make_number (topline - nlines);
15864 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
15865 }
15866
15867 /* Now count lines from the start pos to point. */
15868 nlines = display_count_lines (startpos, startpos_byte,
15869 PT_BYTE, PT, &junk);
15870
15871 /* Record that we did display the line number. */
15872 line_number_displayed = 1;
15873
15874 /* Make the string to show. */
15875 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
15876 return decode_mode_spec_buf;
15877 no_value:
15878 {
15879 char* p = decode_mode_spec_buf;
15880 int pad = field_width - 2;
15881 while (pad-- > 0)
15882 *p++ = ' ';
15883 *p++ = '?';
15884 *p++ = '?';
15885 *p = '\0';
15886 return decode_mode_spec_buf;
15887 }
15888 }
15889 break;
15890
15891 case 'm':
15892 obj = b->mode_name;
15893 break;
15894
15895 case 'n':
15896 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
15897 return " Narrow";
15898 break;
15899
15900 case 'p':
15901 {
15902 int pos = marker_position (w->start);
15903 int total = BUF_ZV (b) - BUF_BEGV (b);
15904
15905 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
15906 {
15907 if (pos <= BUF_BEGV (b))
15908 return "All";
15909 else
15910 return "Bottom";
15911 }
15912 else if (pos <= BUF_BEGV (b))
15913 return "Top";
15914 else
15915 {
15916 if (total > 1000000)
15917 /* Do it differently for a large value, to avoid overflow. */
15918 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
15919 else
15920 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
15921 /* We can't normally display a 3-digit number,
15922 so get us a 2-digit number that is close. */
15923 if (total == 100)
15924 total = 99;
15925 sprintf (decode_mode_spec_buf, "%2d%%", total);
15926 return decode_mode_spec_buf;
15927 }
15928 }
15929
15930 /* Display percentage of size above the bottom of the screen. */
15931 case 'P':
15932 {
15933 int toppos = marker_position (w->start);
15934 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
15935 int total = BUF_ZV (b) - BUF_BEGV (b);
15936
15937 if (botpos >= BUF_ZV (b))
15938 {
15939 if (toppos <= BUF_BEGV (b))
15940 return "All";
15941 else
15942 return "Bottom";
15943 }
15944 else
15945 {
15946 if (total > 1000000)
15947 /* Do it differently for a large value, to avoid overflow. */
15948 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
15949 else
15950 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
15951 /* We can't normally display a 3-digit number,
15952 so get us a 2-digit number that is close. */
15953 if (total == 100)
15954 total = 99;
15955 if (toppos <= BUF_BEGV (b))
15956 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
15957 else
15958 sprintf (decode_mode_spec_buf, "%2d%%", total);
15959 return decode_mode_spec_buf;
15960 }
15961 }
15962
15963 case 's':
15964 /* status of process */
15965 obj = Fget_buffer_process (w->buffer);
15966 if (NILP (obj))
15967 return "no process";
15968 #ifdef subprocesses
15969 obj = Fsymbol_name (Fprocess_status (obj));
15970 #endif
15971 break;
15972
15973 case 't': /* indicate TEXT or BINARY */
15974 #ifdef MODE_LINE_BINARY_TEXT
15975 return MODE_LINE_BINARY_TEXT (b);
15976 #else
15977 return "T";
15978 #endif
15979
15980 case 'z':
15981 /* coding-system (not including end-of-line format) */
15982 case 'Z':
15983 /* coding-system (including end-of-line type) */
15984 {
15985 int eol_flag = (c == 'Z');
15986 char *p = decode_mode_spec_buf;
15987
15988 if (! FRAME_WINDOW_P (f))
15989 {
15990 /* No need to mention EOL here--the terminal never needs
15991 to do EOL conversion. */
15992 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
15993 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
15994 }
15995 p = decode_mode_spec_coding (b->buffer_file_coding_system,
15996 p, eol_flag);
15997
15998 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
15999 #ifdef subprocesses
16000 obj = Fget_buffer_process (Fcurrent_buffer ());
16001 if (PROCESSP (obj))
16002 {
16003 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16004 p, eol_flag);
16005 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16006 p, eol_flag);
16007 }
16008 #endif /* subprocesses */
16009 #endif /* 0 */
16010 *p = 0;
16011 return decode_mode_spec_buf;
16012 }
16013 }
16014
16015 if (STRINGP (obj))
16016 {
16017 *multibyte = STRING_MULTIBYTE (obj);
16018 return (char *) SDATA (obj);
16019 }
16020 else
16021 return "";
16022 }
16023
16024
16025 /* Count up to COUNT lines starting from START / START_BYTE.
16026 But don't go beyond LIMIT_BYTE.
16027 Return the number of lines thus found (always nonnegative).
16028
16029 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
16030
16031 static int
16032 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16033 int start, start_byte, limit_byte, count;
16034 int *byte_pos_ptr;
16035 {
16036 register unsigned char *cursor;
16037 unsigned char *base;
16038
16039 register int ceiling;
16040 register unsigned char *ceiling_addr;
16041 int orig_count = count;
16042
16043 /* If we are not in selective display mode,
16044 check only for newlines. */
16045 int selective_display = (!NILP (current_buffer->selective_display)
16046 && !INTEGERP (current_buffer->selective_display));
16047
16048 if (count > 0)
16049 {
16050 while (start_byte < limit_byte)
16051 {
16052 ceiling = BUFFER_CEILING_OF (start_byte);
16053 ceiling = min (limit_byte - 1, ceiling);
16054 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16055 base = (cursor = BYTE_POS_ADDR (start_byte));
16056 while (1)
16057 {
16058 if (selective_display)
16059 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16060 ;
16061 else
16062 while (*cursor != '\n' && ++cursor != ceiling_addr)
16063 ;
16064
16065 if (cursor != ceiling_addr)
16066 {
16067 if (--count == 0)
16068 {
16069 start_byte += cursor - base + 1;
16070 *byte_pos_ptr = start_byte;
16071 return orig_count;
16072 }
16073 else
16074 if (++cursor == ceiling_addr)
16075 break;
16076 }
16077 else
16078 break;
16079 }
16080 start_byte += cursor - base;
16081 }
16082 }
16083 else
16084 {
16085 while (start_byte > limit_byte)
16086 {
16087 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16088 ceiling = max (limit_byte, ceiling);
16089 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16090 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16091 while (1)
16092 {
16093 if (selective_display)
16094 while (--cursor != ceiling_addr
16095 && *cursor != '\n' && *cursor != 015)
16096 ;
16097 else
16098 while (--cursor != ceiling_addr && *cursor != '\n')
16099 ;
16100
16101 if (cursor != ceiling_addr)
16102 {
16103 if (++count == 0)
16104 {
16105 start_byte += cursor - base + 1;
16106 *byte_pos_ptr = start_byte;
16107 /* When scanning backwards, we should
16108 not count the newline posterior to which we stop. */
16109 return - orig_count - 1;
16110 }
16111 }
16112 else
16113 break;
16114 }
16115 /* Here we add 1 to compensate for the last decrement
16116 of CURSOR, which took it past the valid range. */
16117 start_byte += cursor - base + 1;
16118 }
16119 }
16120
16121 *byte_pos_ptr = limit_byte;
16122
16123 if (count < 0)
16124 return - orig_count + count;
16125 return orig_count - count;
16126
16127 }
16128
16129
16130 \f
16131 /***********************************************************************
16132 Displaying strings
16133 ***********************************************************************/
16134
16135 /* Display a NUL-terminated string, starting with index START.
16136
16137 If STRING is non-null, display that C string. Otherwise, the Lisp
16138 string LISP_STRING is displayed.
16139
16140 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16141 FACE_STRING. Display STRING or LISP_STRING with the face at
16142 FACE_STRING_POS in FACE_STRING:
16143
16144 Display the string in the environment given by IT, but use the
16145 standard display table, temporarily.
16146
16147 FIELD_WIDTH is the minimum number of output glyphs to produce.
16148 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16149 with spaces. If STRING has more characters, more than FIELD_WIDTH
16150 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
16151
16152 PRECISION is the maximum number of characters to output from
16153 STRING. PRECISION < 0 means don't truncate the string.
16154
16155 This is roughly equivalent to printf format specifiers:
16156
16157 FIELD_WIDTH PRECISION PRINTF
16158 ----------------------------------------
16159 -1 -1 %s
16160 -1 10 %.10s
16161 10 -1 %10s
16162 20 10 %20.10s
16163
16164 MULTIBYTE zero means do not display multibyte chars, > 0 means do
16165 display them, and < 0 means obey the current buffer's value of
16166 enable_multibyte_characters.
16167
16168 Value is the number of glyphs produced. */
16169
16170 static int
16171 display_string (string, lisp_string, face_string, face_string_pos,
16172 start, it, field_width, precision, max_x, multibyte)
16173 unsigned char *string;
16174 Lisp_Object lisp_string;
16175 Lisp_Object face_string;
16176 int face_string_pos;
16177 int start;
16178 struct it *it;
16179 int field_width, precision, max_x;
16180 int multibyte;
16181 {
16182 int hpos_at_start = it->hpos;
16183 int saved_face_id = it->face_id;
16184 struct glyph_row *row = it->glyph_row;
16185
16186 /* Initialize the iterator IT for iteration over STRING beginning
16187 with index START. */
16188 reseat_to_string (it, string, lisp_string, start,
16189 precision, field_width, multibyte);
16190
16191 /* If displaying STRING, set up the face of the iterator
16192 from LISP_STRING, if that's given. */
16193 if (STRINGP (face_string))
16194 {
16195 int endptr;
16196 struct face *face;
16197
16198 it->face_id
16199 = face_at_string_position (it->w, face_string, face_string_pos,
16200 0, it->region_beg_charpos,
16201 it->region_end_charpos,
16202 &endptr, it->base_face_id, 0);
16203 face = FACE_FROM_ID (it->f, it->face_id);
16204 it->face_box_p = face->box != FACE_NO_BOX;
16205 }
16206
16207 /* Set max_x to the maximum allowed X position. Don't let it go
16208 beyond the right edge of the window. */
16209 if (max_x <= 0)
16210 max_x = it->last_visible_x;
16211 else
16212 max_x = min (max_x, it->last_visible_x);
16213
16214 /* Skip over display elements that are not visible. because IT->w is
16215 hscrolled. */
16216 if (it->current_x < it->first_visible_x)
16217 move_it_in_display_line_to (it, 100000, it->first_visible_x,
16218 MOVE_TO_POS | MOVE_TO_X);
16219
16220 row->ascent = it->max_ascent;
16221 row->height = it->max_ascent + it->max_descent;
16222 row->phys_ascent = it->max_phys_ascent;
16223 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16224
16225 /* This condition is for the case that we are called with current_x
16226 past last_visible_x. */
16227 while (it->current_x < max_x)
16228 {
16229 int x_before, x, n_glyphs_before, i, nglyphs;
16230
16231 /* Get the next display element. */
16232 if (!get_next_display_element (it))
16233 break;
16234
16235 /* Produce glyphs. */
16236 x_before = it->current_x;
16237 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
16238 PRODUCE_GLYPHS (it);
16239
16240 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
16241 i = 0;
16242 x = x_before;
16243 while (i < nglyphs)
16244 {
16245 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16246
16247 if (!it->truncate_lines_p
16248 && x + glyph->pixel_width > max_x)
16249 {
16250 /* End of continued line or max_x reached. */
16251 if (CHAR_GLYPH_PADDING_P (*glyph))
16252 {
16253 /* A wide character is unbreakable. */
16254 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
16255 it->current_x = x_before;
16256 }
16257 else
16258 {
16259 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
16260 it->current_x = x;
16261 }
16262 break;
16263 }
16264 else if (x + glyph->pixel_width > it->first_visible_x)
16265 {
16266 /* Glyph is at least partially visible. */
16267 ++it->hpos;
16268 if (x < it->first_visible_x)
16269 it->glyph_row->x = x - it->first_visible_x;
16270 }
16271 else
16272 {
16273 /* Glyph is off the left margin of the display area.
16274 Should not happen. */
16275 abort ();
16276 }
16277
16278 row->ascent = max (row->ascent, it->max_ascent);
16279 row->height = max (row->height, it->max_ascent + it->max_descent);
16280 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16281 row->phys_height = max (row->phys_height,
16282 it->max_phys_ascent + it->max_phys_descent);
16283 x += glyph->pixel_width;
16284 ++i;
16285 }
16286
16287 /* Stop if max_x reached. */
16288 if (i < nglyphs)
16289 break;
16290
16291 /* Stop at line ends. */
16292 if (ITERATOR_AT_END_OF_LINE_P (it))
16293 {
16294 it->continuation_lines_width = 0;
16295 break;
16296 }
16297
16298 set_iterator_to_next (it, 1);
16299
16300 /* Stop if truncating at the right edge. */
16301 if (it->truncate_lines_p
16302 && it->current_x >= it->last_visible_x)
16303 {
16304 /* Add truncation mark, but don't do it if the line is
16305 truncated at a padding space. */
16306 if (IT_CHARPOS (*it) < it->string_nchars)
16307 {
16308 if (!FRAME_WINDOW_P (it->f))
16309 {
16310 int i, n;
16311
16312 if (it->current_x > it->last_visible_x)
16313 {
16314 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16315 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16316 break;
16317 for (n = row->used[TEXT_AREA]; i < n; ++i)
16318 {
16319 row->used[TEXT_AREA] = i;
16320 produce_special_glyphs (it, IT_TRUNCATION);
16321 }
16322 }
16323 produce_special_glyphs (it, IT_TRUNCATION);
16324 }
16325 it->glyph_row->truncated_on_right_p = 1;
16326 }
16327 break;
16328 }
16329 }
16330
16331 /* Maybe insert a truncation at the left. */
16332 if (it->first_visible_x
16333 && IT_CHARPOS (*it) > 0)
16334 {
16335 if (!FRAME_WINDOW_P (it->f))
16336 insert_left_trunc_glyphs (it);
16337 it->glyph_row->truncated_on_left_p = 1;
16338 }
16339
16340 it->face_id = saved_face_id;
16341
16342 /* Value is number of columns displayed. */
16343 return it->hpos - hpos_at_start;
16344 }
16345
16346
16347 \f
16348 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
16349 appears as an element of LIST or as the car of an element of LIST.
16350 If PROPVAL is a list, compare each element against LIST in that
16351 way, and return 1/2 if any element of PROPVAL is found in LIST.
16352 Otherwise return 0. This function cannot quit.
16353 The return value is 2 if the text is invisible but with an ellipsis
16354 and 1 if it's invisible and without an ellipsis. */
16355
16356 int
16357 invisible_p (propval, list)
16358 register Lisp_Object propval;
16359 Lisp_Object list;
16360 {
16361 register Lisp_Object tail, proptail;
16362
16363 for (tail = list; CONSP (tail); tail = XCDR (tail))
16364 {
16365 register Lisp_Object tem;
16366 tem = XCAR (tail);
16367 if (EQ (propval, tem))
16368 return 1;
16369 if (CONSP (tem) && EQ (propval, XCAR (tem)))
16370 return NILP (XCDR (tem)) ? 1 : 2;
16371 }
16372
16373 if (CONSP (propval))
16374 {
16375 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
16376 {
16377 Lisp_Object propelt;
16378 propelt = XCAR (proptail);
16379 for (tail = list; CONSP (tail); tail = XCDR (tail))
16380 {
16381 register Lisp_Object tem;
16382 tem = XCAR (tail);
16383 if (EQ (propelt, tem))
16384 return 1;
16385 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
16386 return NILP (XCDR (tem)) ? 1 : 2;
16387 }
16388 }
16389 }
16390
16391 return 0;
16392 }
16393
16394 \f
16395 /***********************************************************************
16396 Glyph Display
16397 ***********************************************************************/
16398
16399 #ifdef HAVE_WINDOW_SYSTEM
16400
16401 #if GLYPH_DEBUG
16402
16403 void
16404 dump_glyph_string (s)
16405 struct glyph_string *s;
16406 {
16407 fprintf (stderr, "glyph string\n");
16408 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
16409 s->x, s->y, s->width, s->height);
16410 fprintf (stderr, " ybase = %d\n", s->ybase);
16411 fprintf (stderr, " hl = %d\n", s->hl);
16412 fprintf (stderr, " left overhang = %d, right = %d\n",
16413 s->left_overhang, s->right_overhang);
16414 fprintf (stderr, " nchars = %d\n", s->nchars);
16415 fprintf (stderr, " extends to end of line = %d\n",
16416 s->extends_to_end_of_line_p);
16417 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
16418 fprintf (stderr, " bg width = %d\n", s->background_width);
16419 }
16420
16421 #endif /* GLYPH_DEBUG */
16422
16423 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
16424 of XChar2b structures for S; it can't be allocated in
16425 init_glyph_string because it must be allocated via `alloca'. W
16426 is the window on which S is drawn. ROW and AREA are the glyph row
16427 and area within the row from which S is constructed. START is the
16428 index of the first glyph structure covered by S. HL is a
16429 face-override for drawing S. */
16430
16431 #ifdef HAVE_NTGUI
16432 #define OPTIONAL_HDC(hdc) hdc,
16433 #define DECLARE_HDC(hdc) HDC hdc;
16434 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
16435 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
16436 #endif
16437
16438 #ifndef OPTIONAL_HDC
16439 #define OPTIONAL_HDC(hdc)
16440 #define DECLARE_HDC(hdc)
16441 #define ALLOCATE_HDC(hdc, f)
16442 #define RELEASE_HDC(hdc, f)
16443 #endif
16444
16445 static void
16446 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
16447 struct glyph_string *s;
16448 DECLARE_HDC (hdc)
16449 XChar2b *char2b;
16450 struct window *w;
16451 struct glyph_row *row;
16452 enum glyph_row_area area;
16453 int start;
16454 enum draw_glyphs_face hl;
16455 {
16456 bzero (s, sizeof *s);
16457 s->w = w;
16458 s->f = XFRAME (w->frame);
16459 #ifdef HAVE_NTGUI
16460 s->hdc = hdc;
16461 #endif
16462 s->display = FRAME_X_DISPLAY (s->f);
16463 s->window = FRAME_X_WINDOW (s->f);
16464 s->char2b = char2b;
16465 s->hl = hl;
16466 s->row = row;
16467 s->area = area;
16468 s->first_glyph = row->glyphs[area] + start;
16469 s->height = row->height;
16470 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
16471
16472 /* Display the internal border below the tool-bar window. */
16473 if (s->w == XWINDOW (s->f->tool_bar_window))
16474 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
16475
16476 s->ybase = s->y + row->ascent;
16477 }
16478
16479
16480 /* Append the list of glyph strings with head H and tail T to the list
16481 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
16482
16483 static INLINE void
16484 append_glyph_string_lists (head, tail, h, t)
16485 struct glyph_string **head, **tail;
16486 struct glyph_string *h, *t;
16487 {
16488 if (h)
16489 {
16490 if (*head)
16491 (*tail)->next = h;
16492 else
16493 *head = h;
16494 h->prev = *tail;
16495 *tail = t;
16496 }
16497 }
16498
16499
16500 /* Prepend the list of glyph strings with head H and tail T to the
16501 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
16502 result. */
16503
16504 static INLINE void
16505 prepend_glyph_string_lists (head, tail, h, t)
16506 struct glyph_string **head, **tail;
16507 struct glyph_string *h, *t;
16508 {
16509 if (h)
16510 {
16511 if (*head)
16512 (*head)->prev = t;
16513 else
16514 *tail = t;
16515 t->next = *head;
16516 *head = h;
16517 }
16518 }
16519
16520
16521 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
16522 Set *HEAD and *TAIL to the resulting list. */
16523
16524 static INLINE void
16525 append_glyph_string (head, tail, s)
16526 struct glyph_string **head, **tail;
16527 struct glyph_string *s;
16528 {
16529 s->next = s->prev = NULL;
16530 append_glyph_string_lists (head, tail, s, s);
16531 }
16532
16533
16534 /* Get face and two-byte form of character glyph GLYPH on frame F.
16535 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
16536 a pointer to a realized face that is ready for display. */
16537
16538 static INLINE struct face *
16539 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
16540 struct frame *f;
16541 struct glyph *glyph;
16542 XChar2b *char2b;
16543 int *two_byte_p;
16544 {
16545 struct face *face;
16546
16547 xassert (glyph->type == CHAR_GLYPH);
16548 face = FACE_FROM_ID (f, glyph->face_id);
16549
16550 if (two_byte_p)
16551 *two_byte_p = 0;
16552
16553 if (!glyph->multibyte_p)
16554 {
16555 /* Unibyte case. We don't have to encode, but we have to make
16556 sure to use a face suitable for unibyte. */
16557 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
16558 }
16559 else if (glyph->u.ch < 128
16560 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
16561 {
16562 /* Case of ASCII in a face known to fit ASCII. */
16563 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
16564 }
16565 else
16566 {
16567 int c1, c2, charset;
16568
16569 /* Split characters into bytes. If c2 is -1 afterwards, C is
16570 really a one-byte character so that byte1 is zero. */
16571 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
16572 if (c2 > 0)
16573 STORE_XCHAR2B (char2b, c1, c2);
16574 else
16575 STORE_XCHAR2B (char2b, 0, c1);
16576
16577 /* Maybe encode the character in *CHAR2B. */
16578 if (charset != CHARSET_ASCII)
16579 {
16580 struct font_info *font_info
16581 = FONT_INFO_FROM_ID (f, face->font_info_id);
16582 if (font_info)
16583 glyph->font_type
16584 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
16585 }
16586 }
16587
16588 /* Make sure X resources of the face are allocated. */
16589 xassert (face != NULL);
16590 PREPARE_FACE_FOR_DISPLAY (f, face);
16591 return face;
16592 }
16593
16594
16595 /* Fill glyph string S with composition components specified by S->cmp.
16596
16597 FACES is an array of faces for all components of this composition.
16598 S->gidx is the index of the first component for S.
16599 OVERLAPS_P non-zero means S should draw the foreground only, and
16600 use its physical height for clipping.
16601
16602 Value is the index of a component not in S. */
16603
16604 static int
16605 fill_composite_glyph_string (s, faces, overlaps_p)
16606 struct glyph_string *s;
16607 struct face **faces;
16608 int overlaps_p;
16609 {
16610 int i;
16611
16612 xassert (s);
16613
16614 s->for_overlaps_p = overlaps_p;
16615
16616 s->face = faces[s->gidx];
16617 s->font = s->face->font;
16618 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
16619
16620 /* For all glyphs of this composition, starting at the offset
16621 S->gidx, until we reach the end of the definition or encounter a
16622 glyph that requires the different face, add it to S. */
16623 ++s->nchars;
16624 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
16625 ++s->nchars;
16626
16627 /* All glyph strings for the same composition has the same width,
16628 i.e. the width set for the first component of the composition. */
16629
16630 s->width = s->first_glyph->pixel_width;
16631
16632 /* If the specified font could not be loaded, use the frame's
16633 default font, but record the fact that we couldn't load it in
16634 the glyph string so that we can draw rectangles for the
16635 characters of the glyph string. */
16636 if (s->font == NULL)
16637 {
16638 s->font_not_found_p = 1;
16639 s->font = FRAME_FONT (s->f);
16640 }
16641
16642 /* Adjust base line for subscript/superscript text. */
16643 s->ybase += s->first_glyph->voffset;
16644
16645 xassert (s->face && s->face->gc);
16646
16647 /* This glyph string must always be drawn with 16-bit functions. */
16648 s->two_byte_p = 1;
16649
16650 return s->gidx + s->nchars;
16651 }
16652
16653
16654 /* Fill glyph string S from a sequence of character glyphs.
16655
16656 FACE_ID is the face id of the string. START is the index of the
16657 first glyph to consider, END is the index of the last + 1.
16658 OVERLAPS_P non-zero means S should draw the foreground only, and
16659 use its physical height for clipping.
16660
16661 Value is the index of the first glyph not in S. */
16662
16663 static int
16664 fill_glyph_string (s, face_id, start, end, overlaps_p)
16665 struct glyph_string *s;
16666 int face_id;
16667 int start, end, overlaps_p;
16668 {
16669 struct glyph *glyph, *last;
16670 int voffset;
16671 int glyph_not_available_p;
16672
16673 xassert (s->f == XFRAME (s->w->frame));
16674 xassert (s->nchars == 0);
16675 xassert (start >= 0 && end > start);
16676
16677 s->for_overlaps_p = overlaps_p,
16678 glyph = s->row->glyphs[s->area] + start;
16679 last = s->row->glyphs[s->area] + end;
16680 voffset = glyph->voffset;
16681
16682 glyph_not_available_p = glyph->glyph_not_available_p;
16683
16684 while (glyph < last
16685 && glyph->type == CHAR_GLYPH
16686 && glyph->voffset == voffset
16687 /* Same face id implies same font, nowadays. */
16688 && glyph->face_id == face_id
16689 && glyph->glyph_not_available_p == glyph_not_available_p)
16690 {
16691 int two_byte_p;
16692
16693 s->face = get_glyph_face_and_encoding (s->f, glyph,
16694 s->char2b + s->nchars,
16695 &two_byte_p);
16696 s->two_byte_p = two_byte_p;
16697 ++s->nchars;
16698 xassert (s->nchars <= end - start);
16699 s->width += glyph->pixel_width;
16700 ++glyph;
16701 }
16702
16703 s->font = s->face->font;
16704 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
16705
16706 /* If the specified font could not be loaded, use the frame's font,
16707 but record the fact that we couldn't load it in
16708 S->font_not_found_p so that we can draw rectangles for the
16709 characters of the glyph string. */
16710 if (s->font == NULL || glyph_not_available_p)
16711 {
16712 s->font_not_found_p = 1;
16713 s->font = FRAME_FONT (s->f);
16714 }
16715
16716 /* Adjust base line for subscript/superscript text. */
16717 s->ybase += voffset;
16718
16719 xassert (s->face && s->face->gc);
16720 return glyph - s->row->glyphs[s->area];
16721 }
16722
16723
16724 /* Fill glyph string S from image glyph S->first_glyph. */
16725
16726 static void
16727 fill_image_glyph_string (s)
16728 struct glyph_string *s;
16729 {
16730 xassert (s->first_glyph->type == IMAGE_GLYPH);
16731 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
16732 xassert (s->img);
16733 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
16734 s->font = s->face->font;
16735 s->width = s->first_glyph->pixel_width;
16736
16737 /* Adjust base line for subscript/superscript text. */
16738 s->ybase += s->first_glyph->voffset;
16739 }
16740
16741
16742 /* Fill glyph string S from a sequence of stretch glyphs.
16743
16744 ROW is the glyph row in which the glyphs are found, AREA is the
16745 area within the row. START is the index of the first glyph to
16746 consider, END is the index of the last + 1.
16747
16748 Value is the index of the first glyph not in S. */
16749
16750 static int
16751 fill_stretch_glyph_string (s, row, area, start, end)
16752 struct glyph_string *s;
16753 struct glyph_row *row;
16754 enum glyph_row_area area;
16755 int start, end;
16756 {
16757 struct glyph *glyph, *last;
16758 int voffset, face_id;
16759
16760 xassert (s->first_glyph->type == STRETCH_GLYPH);
16761
16762 glyph = s->row->glyphs[s->area] + start;
16763 last = s->row->glyphs[s->area] + end;
16764 face_id = glyph->face_id;
16765 s->face = FACE_FROM_ID (s->f, face_id);
16766 s->font = s->face->font;
16767 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
16768 s->width = glyph->pixel_width;
16769 voffset = glyph->voffset;
16770
16771 for (++glyph;
16772 (glyph < last
16773 && glyph->type == STRETCH_GLYPH
16774 && glyph->voffset == voffset
16775 && glyph->face_id == face_id);
16776 ++glyph)
16777 s->width += glyph->pixel_width;
16778
16779 /* Adjust base line for subscript/superscript text. */
16780 s->ybase += voffset;
16781
16782 /* The case that face->gc == 0 is handled when drawing the glyph
16783 string by calling PREPARE_FACE_FOR_DISPLAY. */
16784 xassert (s->face);
16785 return glyph - s->row->glyphs[s->area];
16786 }
16787
16788
16789 /* EXPORT for RIF:
16790 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
16791 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
16792 assumed to be zero. */
16793
16794 void
16795 x_get_glyph_overhangs (glyph, f, left, right)
16796 struct glyph *glyph;
16797 struct frame *f;
16798 int *left, *right;
16799 {
16800 *left = *right = 0;
16801
16802 if (glyph->type == CHAR_GLYPH)
16803 {
16804 XFontStruct *font;
16805 struct face *face;
16806 struct font_info *font_info;
16807 XChar2b char2b;
16808 XCharStruct *pcm;
16809
16810 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
16811 font = face->font;
16812 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
16813 if (font /* ++KFS: Should this be font_info ? */
16814 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
16815 {
16816 if (pcm->rbearing > pcm->width)
16817 *right = pcm->rbearing - pcm->width;
16818 if (pcm->lbearing < 0)
16819 *left = -pcm->lbearing;
16820 }
16821 }
16822 }
16823
16824
16825 /* Return the index of the first glyph preceding glyph string S that
16826 is overwritten by S because of S's left overhang. Value is -1
16827 if no glyphs are overwritten. */
16828
16829 static int
16830 left_overwritten (s)
16831 struct glyph_string *s;
16832 {
16833 int k;
16834
16835 if (s->left_overhang)
16836 {
16837 int x = 0, i;
16838 struct glyph *glyphs = s->row->glyphs[s->area];
16839 int first = s->first_glyph - glyphs;
16840
16841 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
16842 x -= glyphs[i].pixel_width;
16843
16844 k = i + 1;
16845 }
16846 else
16847 k = -1;
16848
16849 return k;
16850 }
16851
16852
16853 /* Return the index of the first glyph preceding glyph string S that
16854 is overwriting S because of its right overhang. Value is -1 if no
16855 glyph in front of S overwrites S. */
16856
16857 static int
16858 left_overwriting (s)
16859 struct glyph_string *s;
16860 {
16861 int i, k, x;
16862 struct glyph *glyphs = s->row->glyphs[s->area];
16863 int first = s->first_glyph - glyphs;
16864
16865 k = -1;
16866 x = 0;
16867 for (i = first - 1; i >= 0; --i)
16868 {
16869 int left, right;
16870 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
16871 if (x + right > 0)
16872 k = i;
16873 x -= glyphs[i].pixel_width;
16874 }
16875
16876 return k;
16877 }
16878
16879
16880 /* Return the index of the last glyph following glyph string S that is
16881 not overwritten by S because of S's right overhang. Value is -1 if
16882 no such glyph is found. */
16883
16884 static int
16885 right_overwritten (s)
16886 struct glyph_string *s;
16887 {
16888 int k = -1;
16889
16890 if (s->right_overhang)
16891 {
16892 int x = 0, i;
16893 struct glyph *glyphs = s->row->glyphs[s->area];
16894 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
16895 int end = s->row->used[s->area];
16896
16897 for (i = first; i < end && s->right_overhang > x; ++i)
16898 x += glyphs[i].pixel_width;
16899
16900 k = i;
16901 }
16902
16903 return k;
16904 }
16905
16906
16907 /* Return the index of the last glyph following glyph string S that
16908 overwrites S because of its left overhang. Value is negative
16909 if no such glyph is found. */
16910
16911 static int
16912 right_overwriting (s)
16913 struct glyph_string *s;
16914 {
16915 int i, k, x;
16916 int end = s->row->used[s->area];
16917 struct glyph *glyphs = s->row->glyphs[s->area];
16918 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
16919
16920 k = -1;
16921 x = 0;
16922 for (i = first; i < end; ++i)
16923 {
16924 int left, right;
16925 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
16926 if (x - left < 0)
16927 k = i;
16928 x += glyphs[i].pixel_width;
16929 }
16930
16931 return k;
16932 }
16933
16934
16935 /* Get face and two-byte form of character C in face FACE_ID on frame
16936 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
16937 means we want to display multibyte text. DISPLAY_P non-zero means
16938 make sure that X resources for the face returned are allocated.
16939 Value is a pointer to a realized face that is ready for display if
16940 DISPLAY_P is non-zero. */
16941
16942 static INLINE struct face *
16943 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
16944 struct frame *f;
16945 int c, face_id;
16946 XChar2b *char2b;
16947 int multibyte_p, display_p;
16948 {
16949 struct face *face = FACE_FROM_ID (f, face_id);
16950
16951 if (!multibyte_p)
16952 {
16953 /* Unibyte case. We don't have to encode, but we have to make
16954 sure to use a face suitable for unibyte. */
16955 STORE_XCHAR2B (char2b, 0, c);
16956 face_id = FACE_FOR_CHAR (f, face, c);
16957 face = FACE_FROM_ID (f, face_id);
16958 }
16959 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
16960 {
16961 /* Case of ASCII in a face known to fit ASCII. */
16962 STORE_XCHAR2B (char2b, 0, c);
16963 }
16964 else
16965 {
16966 int c1, c2, charset;
16967
16968 /* Split characters into bytes. If c2 is -1 afterwards, C is
16969 really a one-byte character so that byte1 is zero. */
16970 SPLIT_CHAR (c, charset, c1, c2);
16971 if (c2 > 0)
16972 STORE_XCHAR2B (char2b, c1, c2);
16973 else
16974 STORE_XCHAR2B (char2b, 0, c1);
16975
16976 /* Maybe encode the character in *CHAR2B. */
16977 if (face->font != NULL)
16978 {
16979 struct font_info *font_info
16980 = FONT_INFO_FROM_ID (f, face->font_info_id);
16981 if (font_info)
16982 rif->encode_char (c, char2b, font_info, 0);
16983 }
16984 }
16985
16986 /* Make sure X resources of the face are allocated. */
16987 #ifdef HAVE_X_WINDOWS
16988 if (display_p)
16989 #endif
16990 {
16991 xassert (face != NULL);
16992 PREPARE_FACE_FOR_DISPLAY (f, face);
16993 }
16994
16995 return face;
16996 }
16997
16998
16999 /* Set background width of glyph string S. START is the index of the
17000 first glyph following S. LAST_X is the right-most x-position + 1
17001 in the drawing area. */
17002
17003 static INLINE void
17004 set_glyph_string_background_width (s, start, last_x)
17005 struct glyph_string *s;
17006 int start;
17007 int last_x;
17008 {
17009 /* If the face of this glyph string has to be drawn to the end of
17010 the drawing area, set S->extends_to_end_of_line_p. */
17011 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
17012
17013 if (start == s->row->used[s->area]
17014 && s->area == TEXT_AREA
17015 && ((s->hl == DRAW_NORMAL_TEXT
17016 && (s->row->fill_line_p
17017 || s->face->background != default_face->background
17018 || s->face->stipple != default_face->stipple
17019 || s->row->mouse_face_p))
17020 || s->hl == DRAW_MOUSE_FACE
17021 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
17022 && s->row->fill_line_p)))
17023 s->extends_to_end_of_line_p = 1;
17024
17025 /* If S extends its face to the end of the line, set its
17026 background_width to the distance to the right edge of the drawing
17027 area. */
17028 if (s->extends_to_end_of_line_p)
17029 s->background_width = last_x - s->x + 1;
17030 else
17031 s->background_width = s->width;
17032 }
17033
17034
17035 /* Compute overhangs and x-positions for glyph string S and its
17036 predecessors, or successors. X is the starting x-position for S.
17037 BACKWARD_P non-zero means process predecessors. */
17038
17039 static void
17040 compute_overhangs_and_x (s, x, backward_p)
17041 struct glyph_string *s;
17042 int x;
17043 int backward_p;
17044 {
17045 if (backward_p)
17046 {
17047 while (s)
17048 {
17049 if (rif->compute_glyph_string_overhangs)
17050 rif->compute_glyph_string_overhangs (s);
17051 x -= s->width;
17052 s->x = x;
17053 s = s->prev;
17054 }
17055 }
17056 else
17057 {
17058 while (s)
17059 {
17060 if (rif->compute_glyph_string_overhangs)
17061 rif->compute_glyph_string_overhangs (s);
17062 s->x = x;
17063 x += s->width;
17064 s = s->next;
17065 }
17066 }
17067 }
17068
17069
17070
17071 /* The following macros are only called from draw_glyphs below.
17072 They reference the following parameters of that function directly:
17073 `w', `row', `area', and `overlap_p'
17074 as well as the following local variables:
17075 `s', `f', and `hdc' (in W32) */
17076
17077 #ifdef HAVE_NTGUI
17078 /* On W32, silently add local `hdc' variable to argument list of
17079 init_glyph_string. */
17080 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17081 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
17082 #else
17083 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17084 init_glyph_string (s, char2b, w, row, area, start, hl)
17085 #endif
17086
17087 /* Add a glyph string for a stretch glyph to the list of strings
17088 between HEAD and TAIL. START is the index of the stretch glyph in
17089 row area AREA of glyph row ROW. END is the index of the last glyph
17090 in that glyph row area. X is the current output position assigned
17091 to the new glyph string constructed. HL overrides that face of the
17092 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17093 is the right-most x-position of the drawing area. */
17094
17095 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
17096 and below -- keep them on one line. */
17097 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17098 do \
17099 { \
17100 s = (struct glyph_string *) alloca (sizeof *s); \
17101 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17102 START = fill_stretch_glyph_string (s, row, area, START, END); \
17103 append_glyph_string (&HEAD, &TAIL, s); \
17104 s->x = (X); \
17105 } \
17106 while (0)
17107
17108
17109 /* Add a glyph string for an image glyph to the list of strings
17110 between HEAD and TAIL. START is the index of the image glyph in
17111 row area AREA of glyph row ROW. END is the index of the last glyph
17112 in that glyph row area. X is the current output position assigned
17113 to the new glyph string constructed. HL overrides that face of the
17114 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17115 is the right-most x-position of the drawing area. */
17116
17117 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17118 do \
17119 { \
17120 s = (struct glyph_string *) alloca (sizeof *s); \
17121 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17122 fill_image_glyph_string (s); \
17123 append_glyph_string (&HEAD, &TAIL, s); \
17124 ++START; \
17125 s->x = (X); \
17126 } \
17127 while (0)
17128
17129
17130 /* Add a glyph string for a sequence of character glyphs to the list
17131 of strings between HEAD and TAIL. START is the index of the first
17132 glyph in row area AREA of glyph row ROW that is part of the new
17133 glyph string. END is the index of the last glyph in that glyph row
17134 area. X is the current output position assigned to the new glyph
17135 string constructed. HL overrides that face of the glyph; e.g. it
17136 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
17137 right-most x-position of the drawing area. */
17138
17139 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17140 do \
17141 { \
17142 int c, face_id; \
17143 XChar2b *char2b; \
17144 \
17145 c = (row)->glyphs[area][START].u.ch; \
17146 face_id = (row)->glyphs[area][START].face_id; \
17147 \
17148 s = (struct glyph_string *) alloca (sizeof *s); \
17149 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
17150 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
17151 append_glyph_string (&HEAD, &TAIL, s); \
17152 s->x = (X); \
17153 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
17154 } \
17155 while (0)
17156
17157
17158 /* Add a glyph string for a composite sequence to the list of strings
17159 between HEAD and TAIL. START is the index of the first glyph in
17160 row area AREA of glyph row ROW that is part of the new glyph
17161 string. END is the index of the last glyph in that glyph row area.
17162 X is the current output position assigned to the new glyph string
17163 constructed. HL overrides that face of the glyph; e.g. it is
17164 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
17165 x-position of the drawing area. */
17166
17167 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17168 do { \
17169 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
17170 int face_id = (row)->glyphs[area][START].face_id; \
17171 struct face *base_face = FACE_FROM_ID (f, face_id); \
17172 struct composition *cmp = composition_table[cmp_id]; \
17173 int glyph_len = cmp->glyph_len; \
17174 XChar2b *char2b; \
17175 struct face **faces; \
17176 struct glyph_string *first_s = NULL; \
17177 int n; \
17178 \
17179 base_face = base_face->ascii_face; \
17180 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
17181 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
17182 /* At first, fill in `char2b' and `faces'. */ \
17183 for (n = 0; n < glyph_len; n++) \
17184 { \
17185 int c = COMPOSITION_GLYPH (cmp, n); \
17186 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
17187 faces[n] = FACE_FROM_ID (f, this_face_id); \
17188 get_char_face_and_encoding (f, c, this_face_id, \
17189 char2b + n, 1, 1); \
17190 } \
17191 \
17192 /* Make glyph_strings for each glyph sequence that is drawable by \
17193 the same face, and append them to HEAD/TAIL. */ \
17194 for (n = 0; n < cmp->glyph_len;) \
17195 { \
17196 s = (struct glyph_string *) alloca (sizeof *s); \
17197 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
17198 append_glyph_string (&(HEAD), &(TAIL), s); \
17199 s->cmp = cmp; \
17200 s->gidx = n; \
17201 s->x = (X); \
17202 \
17203 if (n == 0) \
17204 first_s = s; \
17205 \
17206 n = fill_composite_glyph_string (s, faces, overlaps_p); \
17207 } \
17208 \
17209 ++START; \
17210 s = first_s; \
17211 } while (0)
17212
17213
17214 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
17215 of AREA of glyph row ROW on window W between indices START and END.
17216 HL overrides the face for drawing glyph strings, e.g. it is
17217 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
17218 x-positions of the drawing area.
17219
17220 This is an ugly monster macro construct because we must use alloca
17221 to allocate glyph strings (because draw_glyphs can be called
17222 asynchronously). */
17223
17224 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17225 do \
17226 { \
17227 HEAD = TAIL = NULL; \
17228 while (START < END) \
17229 { \
17230 struct glyph *first_glyph = (row)->glyphs[area] + START; \
17231 switch (first_glyph->type) \
17232 { \
17233 case CHAR_GLYPH: \
17234 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
17235 HL, X, LAST_X); \
17236 break; \
17237 \
17238 case COMPOSITE_GLYPH: \
17239 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
17240 HL, X, LAST_X); \
17241 break; \
17242 \
17243 case STRETCH_GLYPH: \
17244 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
17245 HL, X, LAST_X); \
17246 break; \
17247 \
17248 case IMAGE_GLYPH: \
17249 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
17250 HL, X, LAST_X); \
17251 break; \
17252 \
17253 default: \
17254 abort (); \
17255 } \
17256 \
17257 set_glyph_string_background_width (s, START, LAST_X); \
17258 (X) += s->width; \
17259 } \
17260 } \
17261 while (0)
17262
17263
17264 /* Draw glyphs between START and END in AREA of ROW on window W,
17265 starting at x-position X. X is relative to AREA in W. HL is a
17266 face-override with the following meaning:
17267
17268 DRAW_NORMAL_TEXT draw normally
17269 DRAW_CURSOR draw in cursor face
17270 DRAW_MOUSE_FACE draw in mouse face.
17271 DRAW_INVERSE_VIDEO draw in mode line face
17272 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
17273 DRAW_IMAGE_RAISED draw an image with a raised relief around it
17274
17275 If OVERLAPS_P is non-zero, draw only the foreground of characters
17276 and clip to the physical height of ROW.
17277
17278 Value is the x-position reached, relative to AREA of W. */
17279
17280 static int
17281 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
17282 struct window *w;
17283 int x;
17284 struct glyph_row *row;
17285 enum glyph_row_area area;
17286 int start, end;
17287 enum draw_glyphs_face hl;
17288 int overlaps_p;
17289 {
17290 struct glyph_string *head, *tail;
17291 struct glyph_string *s;
17292 int last_x, area_width;
17293 int x_reached;
17294 int i, j;
17295 struct frame *f = XFRAME (WINDOW_FRAME (w));
17296 DECLARE_HDC (hdc);
17297
17298 ALLOCATE_HDC (hdc, f);
17299
17300 /* Let's rather be paranoid than getting a SEGV. */
17301 end = min (end, row->used[area]);
17302 start = max (0, start);
17303 start = min (end, start);
17304
17305 /* Translate X to frame coordinates. Set last_x to the right
17306 end of the drawing area. */
17307 if (row->full_width_p)
17308 {
17309 /* X is relative to the left edge of W, without scroll bars
17310 or fringes. */
17311 x += WINDOW_LEFT_EDGE_X (w);
17312 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
17313 }
17314 else
17315 {
17316 int area_left = window_box_left (w, area);
17317 x += area_left;
17318 area_width = window_box_width (w, area);
17319 last_x = area_left + area_width;
17320 }
17321
17322 /* Build a doubly-linked list of glyph_string structures between
17323 head and tail from what we have to draw. Note that the macro
17324 BUILD_GLYPH_STRINGS will modify its start parameter. That's
17325 the reason we use a separate variable `i'. */
17326 i = start;
17327 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
17328 if (tail)
17329 x_reached = tail->x + tail->background_width;
17330 else
17331 x_reached = x;
17332
17333 /* If there are any glyphs with lbearing < 0 or rbearing > width in
17334 the row, redraw some glyphs in front or following the glyph
17335 strings built above. */
17336 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
17337 {
17338 int dummy_x = 0;
17339 struct glyph_string *h, *t;
17340
17341 /* Compute overhangs for all glyph strings. */
17342 if (rif->compute_glyph_string_overhangs)
17343 for (s = head; s; s = s->next)
17344 rif->compute_glyph_string_overhangs (s);
17345
17346 /* Prepend glyph strings for glyphs in front of the first glyph
17347 string that are overwritten because of the first glyph
17348 string's left overhang. The background of all strings
17349 prepended must be drawn because the first glyph string
17350 draws over it. */
17351 i = left_overwritten (head);
17352 if (i >= 0)
17353 {
17354 j = i;
17355 BUILD_GLYPH_STRINGS (j, start, h, t,
17356 DRAW_NORMAL_TEXT, dummy_x, last_x);
17357 start = i;
17358 compute_overhangs_and_x (t, head->x, 1);
17359 prepend_glyph_string_lists (&head, &tail, h, t);
17360 }
17361
17362 /* Prepend glyph strings for glyphs in front of the first glyph
17363 string that overwrite that glyph string because of their
17364 right overhang. For these strings, only the foreground must
17365 be drawn, because it draws over the glyph string at `head'.
17366 The background must not be drawn because this would overwrite
17367 right overhangs of preceding glyphs for which no glyph
17368 strings exist. */
17369 i = left_overwriting (head);
17370 if (i >= 0)
17371 {
17372 BUILD_GLYPH_STRINGS (i, start, h, t,
17373 DRAW_NORMAL_TEXT, dummy_x, last_x);
17374 for (s = h; s; s = s->next)
17375 s->background_filled_p = 1;
17376 compute_overhangs_and_x (t, head->x, 1);
17377 prepend_glyph_string_lists (&head, &tail, h, t);
17378 }
17379
17380 /* Append glyphs strings for glyphs following the last glyph
17381 string tail that are overwritten by tail. The background of
17382 these strings has to be drawn because tail's foreground draws
17383 over it. */
17384 i = right_overwritten (tail);
17385 if (i >= 0)
17386 {
17387 BUILD_GLYPH_STRINGS (end, i, h, t,
17388 DRAW_NORMAL_TEXT, x, last_x);
17389 compute_overhangs_and_x (h, tail->x + tail->width, 0);
17390 append_glyph_string_lists (&head, &tail, h, t);
17391 }
17392
17393 /* Append glyph strings for glyphs following the last glyph
17394 string tail that overwrite tail. The foreground of such
17395 glyphs has to be drawn because it writes into the background
17396 of tail. The background must not be drawn because it could
17397 paint over the foreground of following glyphs. */
17398 i = right_overwriting (tail);
17399 if (i >= 0)
17400 {
17401 BUILD_GLYPH_STRINGS (end, i, h, t,
17402 DRAW_NORMAL_TEXT, x, last_x);
17403 for (s = h; s; s = s->next)
17404 s->background_filled_p = 1;
17405 compute_overhangs_and_x (h, tail->x + tail->width, 0);
17406 append_glyph_string_lists (&head, &tail, h, t);
17407 }
17408 }
17409
17410 /* Draw all strings. */
17411 for (s = head; s; s = s->next)
17412 rif->draw_glyph_string (s);
17413
17414 if (area == TEXT_AREA
17415 && !row->full_width_p
17416 /* When drawing overlapping rows, only the glyph strings'
17417 foreground is drawn, which doesn't erase a cursor
17418 completely. */
17419 && !overlaps_p)
17420 {
17421 int x0 = head ? head->x : x;
17422 int x1 = tail ? tail->x + tail->background_width : x;
17423
17424 int text_left = window_box_left (w, TEXT_AREA);
17425 x0 -= text_left;
17426 x1 -= text_left;
17427
17428 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
17429 row->y, MATRIX_ROW_BOTTOM_Y (row));
17430 }
17431
17432 /* Value is the x-position up to which drawn, relative to AREA of W.
17433 This doesn't include parts drawn because of overhangs. */
17434 if (row->full_width_p)
17435 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
17436 else
17437 x_reached -= window_box_left (w, area);
17438
17439 RELEASE_HDC (hdc, f);
17440
17441 return x_reached;
17442 }
17443
17444
17445 /* Store one glyph for IT->char_to_display in IT->glyph_row.
17446 Called from x_produce_glyphs when IT->glyph_row is non-null. */
17447
17448 static INLINE void
17449 append_glyph (it)
17450 struct it *it;
17451 {
17452 struct glyph *glyph;
17453 enum glyph_row_area area = it->area;
17454
17455 xassert (it->glyph_row);
17456 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
17457
17458 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17459 if (glyph < it->glyph_row->glyphs[area + 1])
17460 {
17461 glyph->charpos = CHARPOS (it->position);
17462 glyph->object = it->object;
17463 glyph->pixel_width = it->pixel_width;
17464 glyph->voffset = it->voffset;
17465 glyph->type = CHAR_GLYPH;
17466 glyph->multibyte_p = it->multibyte_p;
17467 glyph->left_box_line_p = it->start_of_box_run_p;
17468 glyph->right_box_line_p = it->end_of_box_run_p;
17469 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
17470 || it->phys_descent > it->descent);
17471 glyph->padding_p = 0;
17472 glyph->glyph_not_available_p = it->glyph_not_available_p;
17473 glyph->face_id = it->face_id;
17474 glyph->u.ch = it->char_to_display;
17475 glyph->font_type = FONT_TYPE_UNKNOWN;
17476 ++it->glyph_row->used[area];
17477 }
17478 }
17479
17480 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
17481 Called from x_produce_glyphs when IT->glyph_row is non-null. */
17482
17483 static INLINE void
17484 append_composite_glyph (it)
17485 struct it *it;
17486 {
17487 struct glyph *glyph;
17488 enum glyph_row_area area = it->area;
17489
17490 xassert (it->glyph_row);
17491
17492 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17493 if (glyph < it->glyph_row->glyphs[area + 1])
17494 {
17495 glyph->charpos = CHARPOS (it->position);
17496 glyph->object = it->object;
17497 glyph->pixel_width = it->pixel_width;
17498 glyph->voffset = it->voffset;
17499 glyph->type = COMPOSITE_GLYPH;
17500 glyph->multibyte_p = it->multibyte_p;
17501 glyph->left_box_line_p = it->start_of_box_run_p;
17502 glyph->right_box_line_p = it->end_of_box_run_p;
17503 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
17504 || it->phys_descent > it->descent);
17505 glyph->padding_p = 0;
17506 glyph->glyph_not_available_p = 0;
17507 glyph->face_id = it->face_id;
17508 glyph->u.cmp_id = it->cmp_id;
17509 glyph->font_type = FONT_TYPE_UNKNOWN;
17510 ++it->glyph_row->used[area];
17511 }
17512 }
17513
17514
17515 /* Change IT->ascent and IT->height according to the setting of
17516 IT->voffset. */
17517
17518 static INLINE void
17519 take_vertical_position_into_account (it)
17520 struct it *it;
17521 {
17522 if (it->voffset)
17523 {
17524 if (it->voffset < 0)
17525 /* Increase the ascent so that we can display the text higher
17526 in the line. */
17527 it->ascent += abs (it->voffset);
17528 else
17529 /* Increase the descent so that we can display the text lower
17530 in the line. */
17531 it->descent += it->voffset;
17532 }
17533 }
17534
17535
17536 /* Produce glyphs/get display metrics for the image IT is loaded with.
17537 See the description of struct display_iterator in dispextern.h for
17538 an overview of struct display_iterator. */
17539
17540 static void
17541 produce_image_glyph (it)
17542 struct it *it;
17543 {
17544 struct image *img;
17545 struct face *face;
17546
17547 xassert (it->what == IT_IMAGE);
17548
17549 face = FACE_FROM_ID (it->f, it->face_id);
17550 img = IMAGE_FROM_ID (it->f, it->image_id);
17551 xassert (img);
17552
17553 /* Make sure X resources of the face and image are loaded. */
17554 PREPARE_FACE_FOR_DISPLAY (it->f, face);
17555 prepare_image_for_display (it->f, img);
17556
17557 it->ascent = it->phys_ascent = image_ascent (img, face);
17558 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
17559 it->pixel_width = img->width + 2 * img->hmargin;
17560
17561 it->nglyphs = 1;
17562
17563 if (face->box != FACE_NO_BOX)
17564 {
17565 if (face->box_line_width > 0)
17566 {
17567 it->ascent += face->box_line_width;
17568 it->descent += face->box_line_width;
17569 }
17570
17571 if (it->start_of_box_run_p)
17572 it->pixel_width += abs (face->box_line_width);
17573 if (it->end_of_box_run_p)
17574 it->pixel_width += abs (face->box_line_width);
17575 }
17576
17577 take_vertical_position_into_account (it);
17578
17579 if (it->glyph_row)
17580 {
17581 struct glyph *glyph;
17582 enum glyph_row_area area = it->area;
17583
17584 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17585 if (glyph < it->glyph_row->glyphs[area + 1])
17586 {
17587 glyph->charpos = CHARPOS (it->position);
17588 glyph->object = it->object;
17589 glyph->pixel_width = it->pixel_width;
17590 glyph->voffset = it->voffset;
17591 glyph->type = IMAGE_GLYPH;
17592 glyph->multibyte_p = it->multibyte_p;
17593 glyph->left_box_line_p = it->start_of_box_run_p;
17594 glyph->right_box_line_p = it->end_of_box_run_p;
17595 glyph->overlaps_vertically_p = 0;
17596 glyph->padding_p = 0;
17597 glyph->glyph_not_available_p = 0;
17598 glyph->face_id = it->face_id;
17599 glyph->u.img_id = img->id;
17600 glyph->font_type = FONT_TYPE_UNKNOWN;
17601 ++it->glyph_row->used[area];
17602 }
17603 }
17604 }
17605
17606
17607 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
17608 of the glyph, WIDTH and HEIGHT are the width and height of the
17609 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
17610 ascent of the glyph (0 <= ASCENT <= 1). */
17611
17612 static void
17613 append_stretch_glyph (it, object, width, height, ascent)
17614 struct it *it;
17615 Lisp_Object object;
17616 int width, height;
17617 double ascent;
17618 {
17619 struct glyph *glyph;
17620 enum glyph_row_area area = it->area;
17621
17622 xassert (ascent >= 0 && ascent <= 1);
17623
17624 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17625 if (glyph < it->glyph_row->glyphs[area + 1])
17626 {
17627 glyph->charpos = CHARPOS (it->position);
17628 glyph->object = object;
17629 glyph->pixel_width = width;
17630 glyph->voffset = it->voffset;
17631 glyph->type = STRETCH_GLYPH;
17632 glyph->multibyte_p = it->multibyte_p;
17633 glyph->left_box_line_p = it->start_of_box_run_p;
17634 glyph->right_box_line_p = it->end_of_box_run_p;
17635 glyph->overlaps_vertically_p = 0;
17636 glyph->padding_p = 0;
17637 glyph->glyph_not_available_p = 0;
17638 glyph->face_id = it->face_id;
17639 glyph->u.stretch.ascent = height * ascent;
17640 glyph->u.stretch.height = height;
17641 glyph->font_type = FONT_TYPE_UNKNOWN;
17642 ++it->glyph_row->used[area];
17643 }
17644 }
17645
17646
17647 /* Produce a stretch glyph for iterator IT. IT->object is the value
17648 of the glyph property displayed. The value must be a list
17649 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
17650 being recognized:
17651
17652 1. `:width WIDTH' specifies that the space should be WIDTH *
17653 canonical char width wide. WIDTH may be an integer or floating
17654 point number.
17655
17656 2. `:relative-width FACTOR' specifies that the width of the stretch
17657 should be computed from the width of the first character having the
17658 `glyph' property, and should be FACTOR times that width.
17659
17660 3. `:align-to HPOS' specifies that the space should be wide enough
17661 to reach HPOS, a value in canonical character units.
17662
17663 Exactly one of the above pairs must be present.
17664
17665 4. `:height HEIGHT' specifies that the height of the stretch produced
17666 should be HEIGHT, measured in canonical character units.
17667
17668 5. `:relative-height FACTOR' specifies that the height of the
17669 stretch should be FACTOR times the height of the characters having
17670 the glyph property.
17671
17672 Either none or exactly one of 4 or 5 must be present.
17673
17674 6. `:ascent ASCENT' specifies that ASCENT percent of the height
17675 of the stretch should be used for the ascent of the stretch.
17676 ASCENT must be in the range 0 <= ASCENT <= 100. */
17677
17678 #define NUMVAL(X) \
17679 ((INTEGERP (X) || FLOATP (X)) \
17680 ? XFLOATINT (X) \
17681 : - 1)
17682
17683
17684 static void
17685 produce_stretch_glyph (it)
17686 struct it *it;
17687 {
17688 /* (space :width WIDTH :height HEIGHT. */
17689 Lisp_Object prop, plist;
17690 int width = 0, height = 0;
17691 double ascent = 0;
17692 struct face *face = FACE_FROM_ID (it->f, it->face_id);
17693 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
17694
17695 PREPARE_FACE_FOR_DISPLAY (it->f, face);
17696
17697 /* List should start with `space'. */
17698 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
17699 plist = XCDR (it->object);
17700
17701 /* Compute the width of the stretch. */
17702 if (prop = Fplist_get (plist, QCwidth),
17703 NUMVAL (prop) > 0)
17704 /* Absolute width `:width WIDTH' specified and valid. */
17705 width = NUMVAL (prop) * FRAME_COLUMN_WIDTH (it->f);
17706 else if (prop = Fplist_get (plist, QCrelative_width),
17707 NUMVAL (prop) > 0)
17708 {
17709 /* Relative width `:relative-width FACTOR' specified and valid.
17710 Compute the width of the characters having the `glyph'
17711 property. */
17712 struct it it2;
17713 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
17714
17715 it2 = *it;
17716 if (it->multibyte_p)
17717 {
17718 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
17719 - IT_BYTEPOS (*it));
17720 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
17721 }
17722 else
17723 it2.c = *p, it2.len = 1;
17724
17725 it2.glyph_row = NULL;
17726 it2.what = IT_CHARACTER;
17727 x_produce_glyphs (&it2);
17728 width = NUMVAL (prop) * it2.pixel_width;
17729 }
17730 else if (prop = Fplist_get (plist, QCalign_to),
17731 NUMVAL (prop) > 0)
17732 width = NUMVAL (prop) * FRAME_COLUMN_WIDTH (it->f) - it->current_x;
17733 else
17734 /* Nothing specified -> width defaults to canonical char width. */
17735 width = FRAME_COLUMN_WIDTH (it->f);
17736
17737 /* Compute height. */
17738 if (prop = Fplist_get (plist, QCheight),
17739 NUMVAL (prop) > 0)
17740 height = NUMVAL (prop) * FRAME_LINE_HEIGHT (it->f);
17741 else if (prop = Fplist_get (plist, QCrelative_height),
17742 NUMVAL (prop) > 0)
17743 height = FONT_HEIGHT (font) * NUMVAL (prop);
17744 else
17745 height = FONT_HEIGHT (font);
17746
17747 /* Compute percentage of height used for ascent. If
17748 `:ascent ASCENT' is present and valid, use that. Otherwise,
17749 derive the ascent from the font in use. */
17750 if (prop = Fplist_get (plist, QCascent),
17751 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
17752 ascent = NUMVAL (prop) / 100.0;
17753 else
17754 ascent = (double) FONT_BASE (font) / FONT_HEIGHT (font);
17755
17756 if (width <= 0)
17757 width = 1;
17758 if (height <= 0)
17759 height = 1;
17760
17761 if (it->glyph_row)
17762 {
17763 Lisp_Object object = it->stack[it->sp - 1].string;
17764 if (!STRINGP (object))
17765 object = it->w->buffer;
17766 append_stretch_glyph (it, object, width, height, ascent);
17767 }
17768
17769 it->pixel_width = width;
17770 it->ascent = it->phys_ascent = height * ascent;
17771 it->descent = it->phys_descent = height - it->ascent;
17772 it->nglyphs = 1;
17773
17774 if (face->box != FACE_NO_BOX)
17775 {
17776 if (face->box_line_width > 0)
17777 {
17778 it->ascent += face->box_line_width;
17779 it->descent += face->box_line_width;
17780 }
17781
17782 if (it->start_of_box_run_p)
17783 it->pixel_width += abs (face->box_line_width);
17784 if (it->end_of_box_run_p)
17785 it->pixel_width += abs (face->box_line_width);
17786 }
17787
17788 take_vertical_position_into_account (it);
17789 }
17790
17791 /* RIF:
17792 Produce glyphs/get display metrics for the display element IT is
17793 loaded with. See the description of struct display_iterator in
17794 dispextern.h for an overview of struct display_iterator. */
17795
17796 void
17797 x_produce_glyphs (it)
17798 struct it *it;
17799 {
17800 it->glyph_not_available_p = 0;
17801
17802 if (it->what == IT_CHARACTER)
17803 {
17804 XChar2b char2b;
17805 XFontStruct *font;
17806 struct face *face = FACE_FROM_ID (it->f, it->face_id);
17807 XCharStruct *pcm;
17808 int font_not_found_p;
17809 struct font_info *font_info;
17810 int boff; /* baseline offset */
17811 /* We may change it->multibyte_p upon unibyte<->multibyte
17812 conversion. So, save the current value now and restore it
17813 later.
17814
17815 Note: It seems that we don't have to record multibyte_p in
17816 struct glyph because the character code itself tells if or
17817 not the character is multibyte. Thus, in the future, we must
17818 consider eliminating the field `multibyte_p' in the struct
17819 glyph. */
17820 int saved_multibyte_p = it->multibyte_p;
17821
17822 /* Maybe translate single-byte characters to multibyte, or the
17823 other way. */
17824 it->char_to_display = it->c;
17825 if (!ASCII_BYTE_P (it->c))
17826 {
17827 if (unibyte_display_via_language_environment
17828 && SINGLE_BYTE_CHAR_P (it->c)
17829 && (it->c >= 0240
17830 || !NILP (Vnonascii_translation_table)))
17831 {
17832 it->char_to_display = unibyte_char_to_multibyte (it->c);
17833 it->multibyte_p = 1;
17834 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
17835 face = FACE_FROM_ID (it->f, it->face_id);
17836 }
17837 else if (!SINGLE_BYTE_CHAR_P (it->c)
17838 && !it->multibyte_p)
17839 {
17840 it->multibyte_p = 1;
17841 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
17842 face = FACE_FROM_ID (it->f, it->face_id);
17843 }
17844 }
17845
17846 /* Get font to use. Encode IT->char_to_display. */
17847 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
17848 &char2b, it->multibyte_p, 0);
17849 font = face->font;
17850
17851 /* When no suitable font found, use the default font. */
17852 font_not_found_p = font == NULL;
17853 if (font_not_found_p)
17854 {
17855 font = FRAME_FONT (it->f);
17856 boff = FRAME_BASELINE_OFFSET (it->f);
17857 font_info = NULL;
17858 }
17859 else
17860 {
17861 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
17862 boff = font_info->baseline_offset;
17863 if (font_info->vertical_centering)
17864 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
17865 }
17866
17867 if (it->char_to_display >= ' '
17868 && (!it->multibyte_p || it->char_to_display < 128))
17869 {
17870 /* Either unibyte or ASCII. */
17871 int stretched_p;
17872
17873 it->nglyphs = 1;
17874
17875 pcm = rif->per_char_metric (font, &char2b,
17876 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
17877 it->ascent = FONT_BASE (font) + boff;
17878 it->descent = FONT_DESCENT (font) - boff;
17879
17880 if (pcm)
17881 {
17882 it->phys_ascent = pcm->ascent + boff;
17883 it->phys_descent = pcm->descent - boff;
17884 it->pixel_width = pcm->width;
17885 }
17886 else
17887 {
17888 it->glyph_not_available_p = 1;
17889 it->phys_ascent = FONT_BASE (font) + boff;
17890 it->phys_descent = FONT_DESCENT (font) - boff;
17891 it->pixel_width = FONT_WIDTH (font);
17892 }
17893
17894 /* If this is a space inside a region of text with
17895 `space-width' property, change its width. */
17896 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
17897 if (stretched_p)
17898 it->pixel_width *= XFLOATINT (it->space_width);
17899
17900 /* If face has a box, add the box thickness to the character
17901 height. If character has a box line to the left and/or
17902 right, add the box line width to the character's width. */
17903 if (face->box != FACE_NO_BOX)
17904 {
17905 int thick = face->box_line_width;
17906
17907 if (thick > 0)
17908 {
17909 it->ascent += thick;
17910 it->descent += thick;
17911 }
17912 else
17913 thick = -thick;
17914
17915 if (it->start_of_box_run_p)
17916 it->pixel_width += thick;
17917 if (it->end_of_box_run_p)
17918 it->pixel_width += thick;
17919 }
17920
17921 /* If face has an overline, add the height of the overline
17922 (1 pixel) and a 1 pixel margin to the character height. */
17923 if (face->overline_p)
17924 it->ascent += 2;
17925
17926 take_vertical_position_into_account (it);
17927
17928 /* If we have to actually produce glyphs, do it. */
17929 if (it->glyph_row)
17930 {
17931 if (stretched_p)
17932 {
17933 /* Translate a space with a `space-width' property
17934 into a stretch glyph. */
17935 double ascent = (double) FONT_BASE (font)
17936 / FONT_HEIGHT (font);
17937 append_stretch_glyph (it, it->object, it->pixel_width,
17938 it->ascent + it->descent, ascent);
17939 }
17940 else
17941 append_glyph (it);
17942
17943 /* If characters with lbearing or rbearing are displayed
17944 in this line, record that fact in a flag of the
17945 glyph row. This is used to optimize X output code. */
17946 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
17947 it->glyph_row->contains_overlapping_glyphs_p = 1;
17948 }
17949 }
17950 else if (it->char_to_display == '\n')
17951 {
17952 /* A newline has no width but we need the height of the line. */
17953 it->pixel_width = 0;
17954 it->nglyphs = 0;
17955 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
17956 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
17957
17958 if (face->box != FACE_NO_BOX
17959 && face->box_line_width > 0)
17960 {
17961 it->ascent += face->box_line_width;
17962 it->descent += face->box_line_width;
17963 }
17964 }
17965 else if (it->char_to_display == '\t')
17966 {
17967 int tab_width = it->tab_width * FRAME_COLUMN_WIDTH (it->f);
17968 int x = it->current_x + it->continuation_lines_width;
17969 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
17970
17971 /* If the distance from the current position to the next tab
17972 stop is less than a canonical character width, use the
17973 tab stop after that. */
17974 if (next_tab_x - x < FRAME_COLUMN_WIDTH (it->f))
17975 next_tab_x += tab_width;
17976
17977 it->pixel_width = next_tab_x - x;
17978 it->nglyphs = 1;
17979 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
17980 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
17981
17982 if (it->glyph_row)
17983 {
17984 double ascent = (double) it->ascent / (it->ascent + it->descent);
17985 append_stretch_glyph (it, it->object, it->pixel_width,
17986 it->ascent + it->descent, ascent);
17987 }
17988 }
17989 else
17990 {
17991 /* A multi-byte character. Assume that the display width of the
17992 character is the width of the character multiplied by the
17993 width of the font. */
17994
17995 /* If we found a font, this font should give us the right
17996 metrics. If we didn't find a font, use the frame's
17997 default font and calculate the width of the character
17998 from the charset width; this is what old redisplay code
17999 did. */
18000
18001 pcm = rif->per_char_metric (font, &char2b,
18002 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
18003
18004 if (font_not_found_p || !pcm)
18005 {
18006 int charset = CHAR_CHARSET (it->char_to_display);
18007
18008 it->glyph_not_available_p = 1;
18009 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
18010 * CHARSET_WIDTH (charset));
18011 it->phys_ascent = FONT_BASE (font) + boff;
18012 it->phys_descent = FONT_DESCENT (font) - boff;
18013 }
18014 else
18015 {
18016 it->pixel_width = pcm->width;
18017 it->phys_ascent = pcm->ascent + boff;
18018 it->phys_descent = pcm->descent - boff;
18019 if (it->glyph_row
18020 && (pcm->lbearing < 0
18021 || pcm->rbearing > pcm->width))
18022 it->glyph_row->contains_overlapping_glyphs_p = 1;
18023 }
18024 it->nglyphs = 1;
18025 it->ascent = FONT_BASE (font) + boff;
18026 it->descent = FONT_DESCENT (font) - boff;
18027 if (face->box != FACE_NO_BOX)
18028 {
18029 int thick = face->box_line_width;
18030
18031 if (thick > 0)
18032 {
18033 it->ascent += thick;
18034 it->descent += thick;
18035 }
18036 else
18037 thick = - thick;
18038
18039 if (it->start_of_box_run_p)
18040 it->pixel_width += thick;
18041 if (it->end_of_box_run_p)
18042 it->pixel_width += thick;
18043 }
18044
18045 /* If face has an overline, add the height of the overline
18046 (1 pixel) and a 1 pixel margin to the character height. */
18047 if (face->overline_p)
18048 it->ascent += 2;
18049
18050 take_vertical_position_into_account (it);
18051
18052 if (it->glyph_row)
18053 append_glyph (it);
18054 }
18055 it->multibyte_p = saved_multibyte_p;
18056 }
18057 else if (it->what == IT_COMPOSITION)
18058 {
18059 /* Note: A composition is represented as one glyph in the
18060 glyph matrix. There are no padding glyphs. */
18061 XChar2b char2b;
18062 XFontStruct *font;
18063 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18064 XCharStruct *pcm;
18065 int font_not_found_p;
18066 struct font_info *font_info;
18067 int boff; /* baseline offset */
18068 struct composition *cmp = composition_table[it->cmp_id];
18069
18070 /* Maybe translate single-byte characters to multibyte. */
18071 it->char_to_display = it->c;
18072 if (unibyte_display_via_language_environment
18073 && SINGLE_BYTE_CHAR_P (it->c)
18074 && (it->c >= 0240
18075 || (it->c >= 0200
18076 && !NILP (Vnonascii_translation_table))))
18077 {
18078 it->char_to_display = unibyte_char_to_multibyte (it->c);
18079 }
18080
18081 /* Get face and font to use. Encode IT->char_to_display. */
18082 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18083 face = FACE_FROM_ID (it->f, it->face_id);
18084 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18085 &char2b, it->multibyte_p, 0);
18086 font = face->font;
18087
18088 /* When no suitable font found, use the default font. */
18089 font_not_found_p = font == NULL;
18090 if (font_not_found_p)
18091 {
18092 font = FRAME_FONT (it->f);
18093 boff = FRAME_BASELINE_OFFSET (it->f);
18094 font_info = NULL;
18095 }
18096 else
18097 {
18098 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18099 boff = font_info->baseline_offset;
18100 if (font_info->vertical_centering)
18101 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18102 }
18103
18104 /* There are no padding glyphs, so there is only one glyph to
18105 produce for the composition. Important is that pixel_width,
18106 ascent and descent are the values of what is drawn by
18107 draw_glyphs (i.e. the values of the overall glyphs composed). */
18108 it->nglyphs = 1;
18109
18110 /* If we have not yet calculated pixel size data of glyphs of
18111 the composition for the current face font, calculate them
18112 now. Theoretically, we have to check all fonts for the
18113 glyphs, but that requires much time and memory space. So,
18114 here we check only the font of the first glyph. This leads
18115 to incorrect display very rarely, and C-l (recenter) can
18116 correct the display anyway. */
18117 if (cmp->font != (void *) font)
18118 {
18119 /* Ascent and descent of the font of the first character of
18120 this composition (adjusted by baseline offset). Ascent
18121 and descent of overall glyphs should not be less than
18122 them respectively. */
18123 int font_ascent = FONT_BASE (font) + boff;
18124 int font_descent = FONT_DESCENT (font) - boff;
18125 /* Bounding box of the overall glyphs. */
18126 int leftmost, rightmost, lowest, highest;
18127 int i, width, ascent, descent;
18128
18129 cmp->font = (void *) font;
18130
18131 /* Initialize the bounding box. */
18132 if (font_info
18133 && (pcm = rif->per_char_metric (font, &char2b,
18134 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
18135 {
18136 width = pcm->width;
18137 ascent = pcm->ascent;
18138 descent = pcm->descent;
18139 }
18140 else
18141 {
18142 width = FONT_WIDTH (font);
18143 ascent = FONT_BASE (font);
18144 descent = FONT_DESCENT (font);
18145 }
18146
18147 rightmost = width;
18148 lowest = - descent + boff;
18149 highest = ascent + boff;
18150 leftmost = 0;
18151
18152 if (font_info
18153 && font_info->default_ascent
18154 && CHAR_TABLE_P (Vuse_default_ascent)
18155 && !NILP (Faref (Vuse_default_ascent,
18156 make_number (it->char_to_display))))
18157 highest = font_info->default_ascent + boff;
18158
18159 /* Draw the first glyph at the normal position. It may be
18160 shifted to right later if some other glyphs are drawn at
18161 the left. */
18162 cmp->offsets[0] = 0;
18163 cmp->offsets[1] = boff;
18164
18165 /* Set cmp->offsets for the remaining glyphs. */
18166 for (i = 1; i < cmp->glyph_len; i++)
18167 {
18168 int left, right, btm, top;
18169 int ch = COMPOSITION_GLYPH (cmp, i);
18170 int face_id = FACE_FOR_CHAR (it->f, face, ch);
18171
18172 face = FACE_FROM_ID (it->f, face_id);
18173 get_char_face_and_encoding (it->f, ch, face->id,
18174 &char2b, it->multibyte_p, 0);
18175 font = face->font;
18176 if (font == NULL)
18177 {
18178 font = FRAME_FONT (it->f);
18179 boff = FRAME_BASELINE_OFFSET (it->f);
18180 font_info = NULL;
18181 }
18182 else
18183 {
18184 font_info
18185 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18186 boff = font_info->baseline_offset;
18187 if (font_info->vertical_centering)
18188 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18189 }
18190
18191 if (font_info
18192 && (pcm = rif->per_char_metric (font, &char2b,
18193 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
18194 {
18195 width = pcm->width;
18196 ascent = pcm->ascent;
18197 descent = pcm->descent;
18198 }
18199 else
18200 {
18201 width = FONT_WIDTH (font);
18202 ascent = 1;
18203 descent = 0;
18204 }
18205
18206 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
18207 {
18208 /* Relative composition with or without
18209 alternate chars. */
18210 left = (leftmost + rightmost - width) / 2;
18211 btm = - descent + boff;
18212 if (font_info && font_info->relative_compose
18213 && (! CHAR_TABLE_P (Vignore_relative_composition)
18214 || NILP (Faref (Vignore_relative_composition,
18215 make_number (ch)))))
18216 {
18217
18218 if (- descent >= font_info->relative_compose)
18219 /* One extra pixel between two glyphs. */
18220 btm = highest + 1;
18221 else if (ascent <= 0)
18222 /* One extra pixel between two glyphs. */
18223 btm = lowest - 1 - ascent - descent;
18224 }
18225 }
18226 else
18227 {
18228 /* A composition rule is specified by an integer
18229 value that encodes global and new reference
18230 points (GREF and NREF). GREF and NREF are
18231 specified by numbers as below:
18232
18233 0---1---2 -- ascent
18234 | |
18235 | |
18236 | |
18237 9--10--11 -- center
18238 | |
18239 ---3---4---5--- baseline
18240 | |
18241 6---7---8 -- descent
18242 */
18243 int rule = COMPOSITION_RULE (cmp, i);
18244 int gref, nref, grefx, grefy, nrefx, nrefy;
18245
18246 COMPOSITION_DECODE_RULE (rule, gref, nref);
18247 grefx = gref % 3, nrefx = nref % 3;
18248 grefy = gref / 3, nrefy = nref / 3;
18249
18250 left = (leftmost
18251 + grefx * (rightmost - leftmost) / 2
18252 - nrefx * width / 2);
18253 btm = ((grefy == 0 ? highest
18254 : grefy == 1 ? 0
18255 : grefy == 2 ? lowest
18256 : (highest + lowest) / 2)
18257 - (nrefy == 0 ? ascent + descent
18258 : nrefy == 1 ? descent - boff
18259 : nrefy == 2 ? 0
18260 : (ascent + descent) / 2));
18261 }
18262
18263 cmp->offsets[i * 2] = left;
18264 cmp->offsets[i * 2 + 1] = btm + descent;
18265
18266 /* Update the bounding box of the overall glyphs. */
18267 right = left + width;
18268 top = btm + descent + ascent;
18269 if (left < leftmost)
18270 leftmost = left;
18271 if (right > rightmost)
18272 rightmost = right;
18273 if (top > highest)
18274 highest = top;
18275 if (btm < lowest)
18276 lowest = btm;
18277 }
18278
18279 /* If there are glyphs whose x-offsets are negative,
18280 shift all glyphs to the right and make all x-offsets
18281 non-negative. */
18282 if (leftmost < 0)
18283 {
18284 for (i = 0; i < cmp->glyph_len; i++)
18285 cmp->offsets[i * 2] -= leftmost;
18286 rightmost -= leftmost;
18287 }
18288
18289 cmp->pixel_width = rightmost;
18290 cmp->ascent = highest;
18291 cmp->descent = - lowest;
18292 if (cmp->ascent < font_ascent)
18293 cmp->ascent = font_ascent;
18294 if (cmp->descent < font_descent)
18295 cmp->descent = font_descent;
18296 }
18297
18298 it->pixel_width = cmp->pixel_width;
18299 it->ascent = it->phys_ascent = cmp->ascent;
18300 it->descent = it->phys_descent = cmp->descent;
18301
18302 if (face->box != FACE_NO_BOX)
18303 {
18304 int thick = face->box_line_width;
18305
18306 if (thick > 0)
18307 {
18308 it->ascent += thick;
18309 it->descent += thick;
18310 }
18311 else
18312 thick = - thick;
18313
18314 if (it->start_of_box_run_p)
18315 it->pixel_width += thick;
18316 if (it->end_of_box_run_p)
18317 it->pixel_width += thick;
18318 }
18319
18320 /* If face has an overline, add the height of the overline
18321 (1 pixel) and a 1 pixel margin to the character height. */
18322 if (face->overline_p)
18323 it->ascent += 2;
18324
18325 take_vertical_position_into_account (it);
18326
18327 if (it->glyph_row)
18328 append_composite_glyph (it);
18329 }
18330 else if (it->what == IT_IMAGE)
18331 produce_image_glyph (it);
18332 else if (it->what == IT_STRETCH)
18333 produce_stretch_glyph (it);
18334
18335 /* Accumulate dimensions. Note: can't assume that it->descent > 0
18336 because this isn't true for images with `:ascent 100'. */
18337 xassert (it->ascent >= 0 && it->descent >= 0);
18338 if (it->area == TEXT_AREA)
18339 it->current_x += it->pixel_width;
18340
18341 it->descent += it->extra_line_spacing;
18342
18343 it->max_ascent = max (it->max_ascent, it->ascent);
18344 it->max_descent = max (it->max_descent, it->descent);
18345 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
18346 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
18347 }
18348
18349 /* EXPORT for RIF:
18350 Output LEN glyphs starting at START at the nominal cursor position.
18351 Advance the nominal cursor over the text. The global variable
18352 updated_window contains the window being updated, updated_row is
18353 the glyph row being updated, and updated_area is the area of that
18354 row being updated. */
18355
18356 void
18357 x_write_glyphs (start, len)
18358 struct glyph *start;
18359 int len;
18360 {
18361 int x, hpos;
18362
18363 xassert (updated_window && updated_row);
18364 BLOCK_INPUT;
18365
18366 /* Write glyphs. */
18367
18368 hpos = start - updated_row->glyphs[updated_area];
18369 x = draw_glyphs (updated_window, output_cursor.x,
18370 updated_row, updated_area,
18371 hpos, hpos + len,
18372 DRAW_NORMAL_TEXT, 0);
18373
18374 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
18375 if (updated_area == TEXT_AREA
18376 && updated_window->phys_cursor_on_p
18377 && updated_window->phys_cursor.vpos == output_cursor.vpos
18378 && updated_window->phys_cursor.hpos >= hpos
18379 && updated_window->phys_cursor.hpos < hpos + len)
18380 updated_window->phys_cursor_on_p = 0;
18381
18382 UNBLOCK_INPUT;
18383
18384 /* Advance the output cursor. */
18385 output_cursor.hpos += len;
18386 output_cursor.x = x;
18387 }
18388
18389
18390 /* EXPORT for RIF:
18391 Insert LEN glyphs from START at the nominal cursor position. */
18392
18393 void
18394 x_insert_glyphs (start, len)
18395 struct glyph *start;
18396 int len;
18397 {
18398 struct frame *f;
18399 struct window *w;
18400 int line_height, shift_by_width, shifted_region_width;
18401 struct glyph_row *row;
18402 struct glyph *glyph;
18403 int frame_x, frame_y, hpos;
18404
18405 xassert (updated_window && updated_row);
18406 BLOCK_INPUT;
18407 w = updated_window;
18408 f = XFRAME (WINDOW_FRAME (w));
18409
18410 /* Get the height of the line we are in. */
18411 row = updated_row;
18412 line_height = row->height;
18413
18414 /* Get the width of the glyphs to insert. */
18415 shift_by_width = 0;
18416 for (glyph = start; glyph < start + len; ++glyph)
18417 shift_by_width += glyph->pixel_width;
18418
18419 /* Get the width of the region to shift right. */
18420 shifted_region_width = (window_box_width (w, updated_area)
18421 - output_cursor.x
18422 - shift_by_width);
18423
18424 /* Shift right. */
18425 frame_x = window_box_left (w, updated_area) + output_cursor.x;
18426 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
18427
18428 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
18429 line_height, shift_by_width);
18430
18431 /* Write the glyphs. */
18432 hpos = start - row->glyphs[updated_area];
18433 draw_glyphs (w, output_cursor.x, row, updated_area,
18434 hpos, hpos + len,
18435 DRAW_NORMAL_TEXT, 0);
18436
18437 /* Advance the output cursor. */
18438 output_cursor.hpos += len;
18439 output_cursor.x += shift_by_width;
18440 UNBLOCK_INPUT;
18441 }
18442
18443
18444 /* EXPORT for RIF:
18445 Erase the current text line from the nominal cursor position
18446 (inclusive) to pixel column TO_X (exclusive). The idea is that
18447 everything from TO_X onward is already erased.
18448
18449 TO_X is a pixel position relative to updated_area of
18450 updated_window. TO_X == -1 means clear to the end of this area. */
18451
18452 void
18453 x_clear_end_of_line (to_x)
18454 int to_x;
18455 {
18456 struct frame *f;
18457 struct window *w = updated_window;
18458 int max_x, min_y, max_y;
18459 int from_x, from_y, to_y;
18460
18461 xassert (updated_window && updated_row);
18462 f = XFRAME (w->frame);
18463
18464 if (updated_row->full_width_p)
18465 max_x = WINDOW_TOTAL_WIDTH (w);
18466 else
18467 max_x = window_box_width (w, updated_area);
18468 max_y = window_text_bottom_y (w);
18469
18470 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
18471 of window. For TO_X > 0, truncate to end of drawing area. */
18472 if (to_x == 0)
18473 return;
18474 else if (to_x < 0)
18475 to_x = max_x;
18476 else
18477 to_x = min (to_x, max_x);
18478
18479 to_y = min (max_y, output_cursor.y + updated_row->height);
18480
18481 /* Notice if the cursor will be cleared by this operation. */
18482 if (!updated_row->full_width_p)
18483 notice_overwritten_cursor (w, updated_area,
18484 output_cursor.x, -1,
18485 updated_row->y,
18486 MATRIX_ROW_BOTTOM_Y (updated_row));
18487
18488 from_x = output_cursor.x;
18489
18490 /* Translate to frame coordinates. */
18491 if (updated_row->full_width_p)
18492 {
18493 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
18494 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
18495 }
18496 else
18497 {
18498 int area_left = window_box_left (w, updated_area);
18499 from_x += area_left;
18500 to_x += area_left;
18501 }
18502
18503 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
18504 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
18505 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
18506
18507 /* Prevent inadvertently clearing to end of the X window. */
18508 if (to_x > from_x && to_y > from_y)
18509 {
18510 BLOCK_INPUT;
18511 rif->clear_frame_area (f, from_x, from_y,
18512 to_x - from_x, to_y - from_y);
18513 UNBLOCK_INPUT;
18514 }
18515 }
18516
18517 #endif /* HAVE_WINDOW_SYSTEM */
18518
18519
18520 \f
18521 /***********************************************************************
18522 Cursor types
18523 ***********************************************************************/
18524
18525 /* Value is the internal representation of the specified cursor type
18526 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
18527 of the bar cursor. */
18528
18529 enum text_cursor_kinds
18530 get_specified_cursor_type (arg, width)
18531 Lisp_Object arg;
18532 int *width;
18533 {
18534 enum text_cursor_kinds type;
18535
18536 if (NILP (arg))
18537 return NO_CURSOR;
18538
18539 if (EQ (arg, Qbox))
18540 return FILLED_BOX_CURSOR;
18541
18542 if (EQ (arg, Qhollow))
18543 return HOLLOW_BOX_CURSOR;
18544
18545 if (EQ (arg, Qbar))
18546 {
18547 *width = 2;
18548 return BAR_CURSOR;
18549 }
18550
18551 if (CONSP (arg)
18552 && EQ (XCAR (arg), Qbar)
18553 && INTEGERP (XCDR (arg))
18554 && XINT (XCDR (arg)) >= 0)
18555 {
18556 *width = XINT (XCDR (arg));
18557 return BAR_CURSOR;
18558 }
18559
18560 if (EQ (arg, Qhbar))
18561 {
18562 *width = 2;
18563 return HBAR_CURSOR;
18564 }
18565
18566 if (CONSP (arg)
18567 && EQ (XCAR (arg), Qhbar)
18568 && INTEGERP (XCDR (arg))
18569 && XINT (XCDR (arg)) >= 0)
18570 {
18571 *width = XINT (XCDR (arg));
18572 return HBAR_CURSOR;
18573 }
18574
18575 /* Treat anything unknown as "hollow box cursor".
18576 It was bad to signal an error; people have trouble fixing
18577 .Xdefaults with Emacs, when it has something bad in it. */
18578 type = HOLLOW_BOX_CURSOR;
18579
18580 return type;
18581 }
18582
18583 /* Set the default cursor types for specified frame. */
18584 void
18585 set_frame_cursor_types (f, arg)
18586 struct frame *f;
18587 Lisp_Object arg;
18588 {
18589 int width;
18590 Lisp_Object tem;
18591
18592 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
18593 FRAME_CURSOR_WIDTH (f) = width;
18594
18595 /* By default, set up the blink-off state depending on the on-state. */
18596
18597 tem = Fassoc (arg, Vblink_cursor_alist);
18598 if (!NILP (tem))
18599 {
18600 FRAME_BLINK_OFF_CURSOR (f)
18601 = get_specified_cursor_type (XCDR (tem), &width);
18602 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
18603 }
18604 else
18605 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
18606 }
18607
18608
18609 /* Return the cursor we want to be displayed in window W. Return
18610 width of bar/hbar cursor through WIDTH arg. Return with
18611 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
18612 (i.e. if the `system caret' should track this cursor).
18613
18614 In a mini-buffer window, we want the cursor only to appear if we
18615 are reading input from this window. For the selected window, we
18616 want the cursor type given by the frame parameter or buffer local
18617 setting of cursor-type. If explicitly marked off, draw no cursor.
18618 In all other cases, we want a hollow box cursor. */
18619
18620 enum text_cursor_kinds
18621 get_window_cursor_type (w, width, active_cursor)
18622 struct window *w;
18623 int *width;
18624 int *active_cursor;
18625 {
18626 struct frame *f = XFRAME (w->frame);
18627 struct buffer *b = XBUFFER (w->buffer);
18628 int cursor_type = DEFAULT_CURSOR;
18629 Lisp_Object alt_cursor;
18630 int non_selected = 0;
18631
18632 *active_cursor = 1;
18633
18634 /* Echo area */
18635 if (cursor_in_echo_area
18636 && FRAME_HAS_MINIBUF_P (f)
18637 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
18638 {
18639 if (w == XWINDOW (echo_area_window))
18640 {
18641 *width = FRAME_CURSOR_WIDTH (f);
18642 return FRAME_DESIRED_CURSOR (f);
18643 }
18644
18645 *active_cursor = 0;
18646 non_selected = 1;
18647 }
18648
18649 /* Nonselected window or nonselected frame. */
18650 else if (w != XWINDOW (f->selected_window)
18651 #ifdef HAVE_WINDOW_SYSTEM
18652 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
18653 #endif
18654 )
18655 {
18656 *active_cursor = 0;
18657
18658 if (MINI_WINDOW_P (w) && minibuf_level == 0)
18659 return NO_CURSOR;
18660
18661 non_selected = 1;
18662 }
18663
18664 /* Never display a cursor in a window in which cursor-type is nil. */
18665 if (NILP (b->cursor_type))
18666 return NO_CURSOR;
18667
18668 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
18669 if (non_selected)
18670 {
18671 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
18672 return get_specified_cursor_type (alt_cursor, width);
18673 }
18674
18675 /* Get the normal cursor type for this window. */
18676 if (EQ (b->cursor_type, Qt))
18677 {
18678 cursor_type = FRAME_DESIRED_CURSOR (f);
18679 *width = FRAME_CURSOR_WIDTH (f);
18680 }
18681 else
18682 cursor_type = get_specified_cursor_type (b->cursor_type, width);
18683
18684 /* Use normal cursor if not blinked off. */
18685 if (!w->cursor_off_p)
18686 return cursor_type;
18687
18688 /* Cursor is blinked off, so determine how to "toggle" it. */
18689
18690 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
18691 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
18692 return get_specified_cursor_type (XCDR (alt_cursor), width);
18693
18694 /* Then see if frame has specified a specific blink off cursor type. */
18695 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
18696 {
18697 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
18698 return FRAME_BLINK_OFF_CURSOR (f);
18699 }
18700
18701 /* Finally perform built-in cursor blinking:
18702 filled box <-> hollow box
18703 wide [h]bar <-> narrow [h]bar
18704 narrow [h]bar <-> no cursor
18705 other type <-> no cursor */
18706
18707 if (cursor_type == FILLED_BOX_CURSOR)
18708 return HOLLOW_BOX_CURSOR;
18709
18710 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
18711 {
18712 *width = 1;
18713 return cursor_type;
18714 }
18715
18716 return NO_CURSOR;
18717 }
18718
18719
18720 #ifdef HAVE_WINDOW_SYSTEM
18721
18722 /* Notice when the text cursor of window W has been completely
18723 overwritten by a drawing operation that outputs glyphs in AREA
18724 starting at X0 and ending at X1 in the line starting at Y0 and
18725 ending at Y1. X coordinates are area-relative. X1 < 0 means all
18726 the rest of the line after X0 has been written. Y coordinates
18727 are window-relative. */
18728
18729 static void
18730 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
18731 struct window *w;
18732 enum glyph_row_area area;
18733 int x0, y0, x1, y1;
18734 {
18735 if (area == TEXT_AREA && w->phys_cursor_on_p)
18736 {
18737 int cx0 = w->phys_cursor.x;
18738 int cx1 = cx0 + w->phys_cursor_width;
18739 int cy0 = w->phys_cursor.y;
18740 int cy1 = cy0 + w->phys_cursor_height;
18741
18742 if (x0 <= cx0 && (x1 < 0 || x1 >= cx1))
18743 {
18744 /* The cursor image will be completely removed from the
18745 screen if the output area intersects the cursor area in
18746 y-direction. When we draw in [y0 y1[, and some part of
18747 the cursor is at y < y0, that part must have been drawn
18748 before. When scrolling, the cursor is erased before
18749 actually scrolling, so we don't come here. When not
18750 scrolling, the rows above the old cursor row must have
18751 changed, and in this case these rows must have written
18752 over the cursor image.
18753
18754 Likewise if part of the cursor is below y1, with the
18755 exception of the cursor being in the first blank row at
18756 the buffer and window end because update_text_area
18757 doesn't draw that row. (Except when it does, but
18758 that's handled in update_text_area.) */
18759
18760 if (((y0 >= cy0 && y0 < cy1) || (y1 > cy0 && y1 < cy1))
18761 && w->current_matrix->rows[w->phys_cursor.vpos].displays_text_p)
18762 w->phys_cursor_on_p = 0;
18763 }
18764 }
18765 }
18766
18767 #endif /* HAVE_WINDOW_SYSTEM */
18768
18769 \f
18770 /************************************************************************
18771 Mouse Face
18772 ************************************************************************/
18773
18774 #ifdef HAVE_WINDOW_SYSTEM
18775
18776 /* EXPORT for RIF:
18777 Fix the display of area AREA of overlapping row ROW in window W. */
18778
18779 void
18780 x_fix_overlapping_area (w, row, area)
18781 struct window *w;
18782 struct glyph_row *row;
18783 enum glyph_row_area area;
18784 {
18785 int i, x;
18786
18787 BLOCK_INPUT;
18788
18789 x = window_box_left_offset (w, area);
18790 if (area == TEXT_AREA)
18791 x += row->x;
18792
18793 for (i = 0; i < row->used[area];)
18794 {
18795 if (row->glyphs[area][i].overlaps_vertically_p)
18796 {
18797 int start = i, start_x = x;
18798
18799 do
18800 {
18801 x += row->glyphs[area][i].pixel_width;
18802 ++i;
18803 }
18804 while (i < row->used[area]
18805 && row->glyphs[area][i].overlaps_vertically_p);
18806
18807 draw_glyphs (w, start_x, row, area,
18808 start, i,
18809 DRAW_NORMAL_TEXT, 1);
18810 }
18811 else
18812 {
18813 x += row->glyphs[area][i].pixel_width;
18814 ++i;
18815 }
18816 }
18817
18818 UNBLOCK_INPUT;
18819 }
18820
18821
18822 /* EXPORT:
18823 Draw the cursor glyph of window W in glyph row ROW. See the
18824 comment of draw_glyphs for the meaning of HL. */
18825
18826 void
18827 draw_phys_cursor_glyph (w, row, hl)
18828 struct window *w;
18829 struct glyph_row *row;
18830 enum draw_glyphs_face hl;
18831 {
18832 /* If cursor hpos is out of bounds, don't draw garbage. This can
18833 happen in mini-buffer windows when switching between echo area
18834 glyphs and mini-buffer. */
18835 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
18836 {
18837 int on_p = w->phys_cursor_on_p;
18838 int x1;
18839 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
18840 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
18841 hl, 0);
18842 w->phys_cursor_on_p = on_p;
18843
18844 if (hl == DRAW_CURSOR)
18845 w->phys_cursor_width = x1 - w->phys_cursor.x;
18846 /* When we erase the cursor, and ROW is overlapped by other
18847 rows, make sure that these overlapping parts of other rows
18848 are redrawn. */
18849 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
18850 {
18851 if (row > w->current_matrix->rows
18852 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
18853 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
18854
18855 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
18856 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
18857 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
18858 }
18859 }
18860 }
18861
18862
18863 /* EXPORT:
18864 Erase the image of a cursor of window W from the screen. */
18865
18866 void
18867 erase_phys_cursor (w)
18868 struct window *w;
18869 {
18870 struct frame *f = XFRAME (w->frame);
18871 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
18872 int hpos = w->phys_cursor.hpos;
18873 int vpos = w->phys_cursor.vpos;
18874 int mouse_face_here_p = 0;
18875 struct glyph_matrix *active_glyphs = w->current_matrix;
18876 struct glyph_row *cursor_row;
18877 struct glyph *cursor_glyph;
18878 enum draw_glyphs_face hl;
18879
18880 /* No cursor displayed or row invalidated => nothing to do on the
18881 screen. */
18882 if (w->phys_cursor_type == NO_CURSOR)
18883 goto mark_cursor_off;
18884
18885 /* VPOS >= active_glyphs->nrows means that window has been resized.
18886 Don't bother to erase the cursor. */
18887 if (vpos >= active_glyphs->nrows)
18888 goto mark_cursor_off;
18889
18890 /* If row containing cursor is marked invalid, there is nothing we
18891 can do. */
18892 cursor_row = MATRIX_ROW (active_glyphs, vpos);
18893 if (!cursor_row->enabled_p)
18894 goto mark_cursor_off;
18895
18896 /* If row is completely invisible, don't attempt to delete a cursor which
18897 isn't there. This can happen if cursor is at top of a window, and
18898 we switch to a buffer with a header line in that window. */
18899 if (cursor_row->visible_height <= 0)
18900 goto mark_cursor_off;
18901
18902 /* This can happen when the new row is shorter than the old one.
18903 In this case, either draw_glyphs or clear_end_of_line
18904 should have cleared the cursor. Note that we wouldn't be
18905 able to erase the cursor in this case because we don't have a
18906 cursor glyph at hand. */
18907 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
18908 goto mark_cursor_off;
18909
18910 /* If the cursor is in the mouse face area, redisplay that when
18911 we clear the cursor. */
18912 if (! NILP (dpyinfo->mouse_face_window)
18913 && w == XWINDOW (dpyinfo->mouse_face_window)
18914 && (vpos > dpyinfo->mouse_face_beg_row
18915 || (vpos == dpyinfo->mouse_face_beg_row
18916 && hpos >= dpyinfo->mouse_face_beg_col))
18917 && (vpos < dpyinfo->mouse_face_end_row
18918 || (vpos == dpyinfo->mouse_face_end_row
18919 && hpos < dpyinfo->mouse_face_end_col))
18920 /* Don't redraw the cursor's spot in mouse face if it is at the
18921 end of a line (on a newline). The cursor appears there, but
18922 mouse highlighting does not. */
18923 && cursor_row->used[TEXT_AREA] > hpos)
18924 mouse_face_here_p = 1;
18925
18926 /* Maybe clear the display under the cursor. */
18927 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
18928 {
18929 int x, y;
18930 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
18931
18932 cursor_glyph = get_phys_cursor_glyph (w);
18933 if (cursor_glyph == NULL)
18934 goto mark_cursor_off;
18935
18936 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
18937 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
18938
18939 rif->clear_frame_area (f, x, y,
18940 cursor_glyph->pixel_width, cursor_row->visible_height);
18941 }
18942
18943 /* Erase the cursor by redrawing the character underneath it. */
18944 if (mouse_face_here_p)
18945 hl = DRAW_MOUSE_FACE;
18946 else
18947 hl = DRAW_NORMAL_TEXT;
18948 draw_phys_cursor_glyph (w, cursor_row, hl);
18949
18950 mark_cursor_off:
18951 w->phys_cursor_on_p = 0;
18952 w->phys_cursor_type = NO_CURSOR;
18953 }
18954
18955
18956 /* EXPORT:
18957 Display or clear cursor of window W. If ON is zero, clear the
18958 cursor. If it is non-zero, display the cursor. If ON is nonzero,
18959 where to put the cursor is specified by HPOS, VPOS, X and Y. */
18960
18961 void
18962 display_and_set_cursor (w, on, hpos, vpos, x, y)
18963 struct window *w;
18964 int on, hpos, vpos, x, y;
18965 {
18966 struct frame *f = XFRAME (w->frame);
18967 int new_cursor_type;
18968 int new_cursor_width;
18969 int active_cursor;
18970 struct glyph_matrix *current_glyphs;
18971 struct glyph_row *glyph_row;
18972 struct glyph *glyph;
18973
18974 /* This is pointless on invisible frames, and dangerous on garbaged
18975 windows and frames; in the latter case, the frame or window may
18976 be in the midst of changing its size, and x and y may be off the
18977 window. */
18978 if (! FRAME_VISIBLE_P (f)
18979 || FRAME_GARBAGED_P (f)
18980 || vpos >= w->current_matrix->nrows
18981 || hpos >= w->current_matrix->matrix_w)
18982 return;
18983
18984 /* If cursor is off and we want it off, return quickly. */
18985 if (!on && !w->phys_cursor_on_p)
18986 return;
18987
18988 current_glyphs = w->current_matrix;
18989 glyph_row = MATRIX_ROW (current_glyphs, vpos);
18990 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
18991
18992 /* If cursor row is not enabled, we don't really know where to
18993 display the cursor. */
18994 if (!glyph_row->enabled_p)
18995 {
18996 w->phys_cursor_on_p = 0;
18997 return;
18998 }
18999
19000 xassert (interrupt_input_blocked);
19001
19002 /* Set new_cursor_type to the cursor we want to be displayed. */
19003 new_cursor_type = get_window_cursor_type (w, &new_cursor_width, &active_cursor);
19004
19005 /* If cursor is currently being shown and we don't want it to be or
19006 it is in the wrong place, or the cursor type is not what we want,
19007 erase it. */
19008 if (w->phys_cursor_on_p
19009 && (!on
19010 || w->phys_cursor.x != x
19011 || w->phys_cursor.y != y
19012 || new_cursor_type != w->phys_cursor_type
19013 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
19014 && new_cursor_width != w->phys_cursor_width)))
19015 erase_phys_cursor (w);
19016
19017 /* Don't check phys_cursor_on_p here because that flag is only set
19018 to zero in some cases where we know that the cursor has been
19019 completely erased, to avoid the extra work of erasing the cursor
19020 twice. In other words, phys_cursor_on_p can be 1 and the cursor
19021 still not be visible, or it has only been partly erased. */
19022 if (on)
19023 {
19024 w->phys_cursor_ascent = glyph_row->ascent;
19025 w->phys_cursor_height = glyph_row->height;
19026
19027 /* Set phys_cursor_.* before x_draw_.* is called because some
19028 of them may need the information. */
19029 w->phys_cursor.x = x;
19030 w->phys_cursor.y = glyph_row->y;
19031 w->phys_cursor.hpos = hpos;
19032 w->phys_cursor.vpos = vpos;
19033 }
19034
19035 rif->draw_window_cursor (w, glyph_row, x, y,
19036 new_cursor_type, new_cursor_width,
19037 on, active_cursor);
19038 }
19039
19040
19041 /* Switch the display of W's cursor on or off, according to the value
19042 of ON. */
19043
19044 static void
19045 update_window_cursor (w, on)
19046 struct window *w;
19047 int on;
19048 {
19049 /* Don't update cursor in windows whose frame is in the process
19050 of being deleted. */
19051 if (w->current_matrix)
19052 {
19053 BLOCK_INPUT;
19054 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
19055 w->phys_cursor.x, w->phys_cursor.y);
19056 UNBLOCK_INPUT;
19057 }
19058 }
19059
19060
19061 /* Call update_window_cursor with parameter ON_P on all leaf windows
19062 in the window tree rooted at W. */
19063
19064 static void
19065 update_cursor_in_window_tree (w, on_p)
19066 struct window *w;
19067 int on_p;
19068 {
19069 while (w)
19070 {
19071 if (!NILP (w->hchild))
19072 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
19073 else if (!NILP (w->vchild))
19074 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
19075 else
19076 update_window_cursor (w, on_p);
19077
19078 w = NILP (w->next) ? 0 : XWINDOW (w->next);
19079 }
19080 }
19081
19082
19083 /* EXPORT:
19084 Display the cursor on window W, or clear it, according to ON_P.
19085 Don't change the cursor's position. */
19086
19087 void
19088 x_update_cursor (f, on_p)
19089 struct frame *f;
19090 int on_p;
19091 {
19092 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
19093 }
19094
19095
19096 /* EXPORT:
19097 Clear the cursor of window W to background color, and mark the
19098 cursor as not shown. This is used when the text where the cursor
19099 is is about to be rewritten. */
19100
19101 void
19102 x_clear_cursor (w)
19103 struct window *w;
19104 {
19105 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
19106 update_window_cursor (w, 0);
19107 }
19108
19109
19110 /* EXPORT:
19111 Display the active region described by mouse_face_* according to DRAW. */
19112
19113 void
19114 show_mouse_face (dpyinfo, draw)
19115 Display_Info *dpyinfo;
19116 enum draw_glyphs_face draw;
19117 {
19118 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
19119 struct frame *f = XFRAME (WINDOW_FRAME (w));
19120
19121 if (/* If window is in the process of being destroyed, don't bother
19122 to do anything. */
19123 w->current_matrix != NULL
19124 /* Don't update mouse highlight if hidden */
19125 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
19126 /* Recognize when we are called to operate on rows that don't exist
19127 anymore. This can happen when a window is split. */
19128 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
19129 {
19130 int phys_cursor_on_p = w->phys_cursor_on_p;
19131 struct glyph_row *row, *first, *last;
19132
19133 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
19134 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
19135
19136 for (row = first; row <= last && row->enabled_p; ++row)
19137 {
19138 int start_hpos, end_hpos, start_x;
19139
19140 /* For all but the first row, the highlight starts at column 0. */
19141 if (row == first)
19142 {
19143 start_hpos = dpyinfo->mouse_face_beg_col;
19144 start_x = dpyinfo->mouse_face_beg_x;
19145 }
19146 else
19147 {
19148 start_hpos = 0;
19149 start_x = 0;
19150 }
19151
19152 if (row == last)
19153 end_hpos = dpyinfo->mouse_face_end_col;
19154 else
19155 end_hpos = row->used[TEXT_AREA];
19156
19157 if (end_hpos > start_hpos)
19158 {
19159 draw_glyphs (w, start_x, row, TEXT_AREA,
19160 start_hpos, end_hpos,
19161 draw, 0);
19162
19163 row->mouse_face_p
19164 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
19165 }
19166 }
19167
19168 /* When we've written over the cursor, arrange for it to
19169 be displayed again. */
19170 if (phys_cursor_on_p && !w->phys_cursor_on_p)
19171 {
19172 BLOCK_INPUT;
19173 display_and_set_cursor (w, 1,
19174 w->phys_cursor.hpos, w->phys_cursor.vpos,
19175 w->phys_cursor.x, w->phys_cursor.y);
19176 UNBLOCK_INPUT;
19177 }
19178 }
19179
19180 /* Change the mouse cursor. */
19181 if (draw == DRAW_NORMAL_TEXT)
19182 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
19183 else if (draw == DRAW_MOUSE_FACE)
19184 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
19185 else
19186 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
19187 }
19188
19189 /* EXPORT:
19190 Clear out the mouse-highlighted active region.
19191 Redraw it un-highlighted first. Value is non-zero if mouse
19192 face was actually drawn unhighlighted. */
19193
19194 int
19195 clear_mouse_face (dpyinfo)
19196 Display_Info *dpyinfo;
19197 {
19198 int cleared = 0;
19199
19200 if (!NILP (dpyinfo->mouse_face_window))
19201 {
19202 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
19203 cleared = 1;
19204 }
19205
19206 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
19207 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
19208 dpyinfo->mouse_face_window = Qnil;
19209 dpyinfo->mouse_face_overlay = Qnil;
19210 return cleared;
19211 }
19212
19213
19214 /* EXPORT:
19215 Non-zero if physical cursor of window W is within mouse face. */
19216
19217 int
19218 cursor_in_mouse_face_p (w)
19219 struct window *w;
19220 {
19221 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
19222 int in_mouse_face = 0;
19223
19224 if (WINDOWP (dpyinfo->mouse_face_window)
19225 && XWINDOW (dpyinfo->mouse_face_window) == w)
19226 {
19227 int hpos = w->phys_cursor.hpos;
19228 int vpos = w->phys_cursor.vpos;
19229
19230 if (vpos >= dpyinfo->mouse_face_beg_row
19231 && vpos <= dpyinfo->mouse_face_end_row
19232 && (vpos > dpyinfo->mouse_face_beg_row
19233 || hpos >= dpyinfo->mouse_face_beg_col)
19234 && (vpos < dpyinfo->mouse_face_end_row
19235 || hpos < dpyinfo->mouse_face_end_col
19236 || dpyinfo->mouse_face_past_end))
19237 in_mouse_face = 1;
19238 }
19239
19240 return in_mouse_face;
19241 }
19242
19243
19244
19245 \f
19246 /* Find the glyph matrix position of buffer position CHARPOS in window
19247 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
19248 current glyphs must be up to date. If CHARPOS is above window
19249 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
19250 of last line in W. In the row containing CHARPOS, stop before glyphs
19251 having STOP as object. */
19252
19253 #if 0 /* This is a version of fast_find_position that's more correct
19254 in the presence of hscrolling, for example. I didn't install
19255 it right away because the problem fixed is minor, it failed
19256 in 20.x as well, and I think it's too risky to install
19257 so near the release of 21.1. 2001-09-25 gerd. */
19258
19259 static int
19260 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
19261 struct window *w;
19262 int charpos;
19263 int *hpos, *vpos, *x, *y;
19264 Lisp_Object stop;
19265 {
19266 struct glyph_row *row, *first;
19267 struct glyph *glyph, *end;
19268 int i, past_end = 0;
19269
19270 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19271 row = row_containing_pos (w, charpos, first, NULL, 0);
19272 if (row == NULL)
19273 {
19274 if (charpos < MATRIX_ROW_START_CHARPOS (first))
19275 {
19276 *x = *y = *hpos = *vpos = 0;
19277 return 0;
19278 }
19279 else
19280 {
19281 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
19282 past_end = 1;
19283 }
19284 }
19285
19286 *x = row->x;
19287 *y = row->y;
19288 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
19289
19290 glyph = row->glyphs[TEXT_AREA];
19291 end = glyph + row->used[TEXT_AREA];
19292
19293 /* Skip over glyphs not having an object at the start of the row.
19294 These are special glyphs like truncation marks on terminal
19295 frames. */
19296 if (row->displays_text_p)
19297 while (glyph < end
19298 && INTEGERP (glyph->object)
19299 && !EQ (stop, glyph->object)
19300 && glyph->charpos < 0)
19301 {
19302 *x += glyph->pixel_width;
19303 ++glyph;
19304 }
19305
19306 while (glyph < end
19307 && !INTEGERP (glyph->object)
19308 && !EQ (stop, glyph->object)
19309 && (!BUFFERP (glyph->object)
19310 || glyph->charpos < charpos))
19311 {
19312 *x += glyph->pixel_width;
19313 ++glyph;
19314 }
19315
19316 *hpos = glyph - row->glyphs[TEXT_AREA];
19317 return past_end;
19318 }
19319
19320 #else /* not 0 */
19321
19322 static int
19323 fast_find_position (w, pos, hpos, vpos, x, y, stop)
19324 struct window *w;
19325 int pos;
19326 int *hpos, *vpos, *x, *y;
19327 Lisp_Object stop;
19328 {
19329 int i;
19330 int lastcol;
19331 int maybe_next_line_p = 0;
19332 int line_start_position;
19333 int yb = window_text_bottom_y (w);
19334 struct glyph_row *row, *best_row;
19335 int row_vpos, best_row_vpos;
19336 int current_x;
19337
19338 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19339 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
19340
19341 while (row->y < yb)
19342 {
19343 if (row->used[TEXT_AREA])
19344 line_start_position = row->glyphs[TEXT_AREA]->charpos;
19345 else
19346 line_start_position = 0;
19347
19348 if (line_start_position > pos)
19349 break;
19350 /* If the position sought is the end of the buffer,
19351 don't include the blank lines at the bottom of the window. */
19352 else if (line_start_position == pos
19353 && pos == BUF_ZV (XBUFFER (w->buffer)))
19354 {
19355 maybe_next_line_p = 1;
19356 break;
19357 }
19358 else if (line_start_position > 0)
19359 {
19360 best_row = row;
19361 best_row_vpos = row_vpos;
19362 }
19363
19364 if (row->y + row->height >= yb)
19365 break;
19366
19367 ++row;
19368 ++row_vpos;
19369 }
19370
19371 /* Find the right column within BEST_ROW. */
19372 lastcol = 0;
19373 current_x = best_row->x;
19374 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
19375 {
19376 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
19377 int charpos = glyph->charpos;
19378
19379 if (BUFFERP (glyph->object))
19380 {
19381 if (charpos == pos)
19382 {
19383 *hpos = i;
19384 *vpos = best_row_vpos;
19385 *x = current_x;
19386 *y = best_row->y;
19387 return 1;
19388 }
19389 else if (charpos > pos)
19390 break;
19391 }
19392 else if (EQ (glyph->object, stop))
19393 break;
19394
19395 if (charpos > 0)
19396 lastcol = i;
19397 current_x += glyph->pixel_width;
19398 }
19399
19400 /* If we're looking for the end of the buffer,
19401 and we didn't find it in the line we scanned,
19402 use the start of the following line. */
19403 if (maybe_next_line_p)
19404 {
19405 ++best_row;
19406 ++best_row_vpos;
19407 lastcol = 0;
19408 current_x = best_row->x;
19409 }
19410
19411 *vpos = best_row_vpos;
19412 *hpos = lastcol + 1;
19413 *x = current_x;
19414 *y = best_row->y;
19415 return 0;
19416 }
19417
19418 #endif /* not 0 */
19419
19420
19421 /* Find the position of the glyph for position POS in OBJECT in
19422 window W's current matrix, and return in *X, *Y the pixel
19423 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
19424
19425 RIGHT_P non-zero means return the position of the right edge of the
19426 glyph, RIGHT_P zero means return the left edge position.
19427
19428 If no glyph for POS exists in the matrix, return the position of
19429 the glyph with the next smaller position that is in the matrix, if
19430 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
19431 exists in the matrix, return the position of the glyph with the
19432 next larger position in OBJECT.
19433
19434 Value is non-zero if a glyph was found. */
19435
19436 static int
19437 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
19438 struct window *w;
19439 int pos;
19440 Lisp_Object object;
19441 int *hpos, *vpos, *x, *y;
19442 int right_p;
19443 {
19444 int yb = window_text_bottom_y (w);
19445 struct glyph_row *r;
19446 struct glyph *best_glyph = NULL;
19447 struct glyph_row *best_row = NULL;
19448 int best_x = 0;
19449
19450 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19451 r->enabled_p && r->y < yb;
19452 ++r)
19453 {
19454 struct glyph *g = r->glyphs[TEXT_AREA];
19455 struct glyph *e = g + r->used[TEXT_AREA];
19456 int gx;
19457
19458 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
19459 if (EQ (g->object, object))
19460 {
19461 if (g->charpos == pos)
19462 {
19463 best_glyph = g;
19464 best_x = gx;
19465 best_row = r;
19466 goto found;
19467 }
19468 else if (best_glyph == NULL
19469 || ((abs (g->charpos - pos)
19470 < abs (best_glyph->charpos - pos))
19471 && (right_p
19472 ? g->charpos < pos
19473 : g->charpos > pos)))
19474 {
19475 best_glyph = g;
19476 best_x = gx;
19477 best_row = r;
19478 }
19479 }
19480 }
19481
19482 found:
19483
19484 if (best_glyph)
19485 {
19486 *x = best_x;
19487 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
19488
19489 if (right_p)
19490 {
19491 *x += best_glyph->pixel_width;
19492 ++*hpos;
19493 }
19494
19495 *y = best_row->y;
19496 *vpos = best_row - w->current_matrix->rows;
19497 }
19498
19499 return best_glyph != NULL;
19500 }
19501
19502
19503 /* Take proper action when mouse has moved to the mode or header line
19504 or marginal area AREA of window W, x-position X and y-position Y.
19505 X is relative to the start of the text display area of W, so the
19506 width of bitmap areas and scroll bars must be subtracted to get a
19507 position relative to the start of the mode line. */
19508
19509 static void
19510 note_mode_line_or_margin_highlight (w, x, y, area)
19511 struct window *w;
19512 int x, y;
19513 enum window_part area;
19514 {
19515 struct frame *f = XFRAME (w->frame);
19516 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
19517 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
19518 int charpos;
19519 Lisp_Object string, help, map, pos;
19520
19521 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
19522 string = mode_line_string (w, x, y, area, &charpos);
19523 else
19524 string = marginal_area_string (w, x, y, area, &charpos);
19525
19526 if (STRINGP (string))
19527 {
19528 pos = make_number (charpos);
19529
19530 /* If we're on a string with `help-echo' text property, arrange
19531 for the help to be displayed. This is done by setting the
19532 global variable help_echo_string to the help string. */
19533 help = Fget_text_property (pos, Qhelp_echo, string);
19534 if (!NILP (help))
19535 {
19536 help_echo_string = help;
19537 XSETWINDOW (help_echo_window, w);
19538 help_echo_object = string;
19539 help_echo_pos = charpos;
19540 }
19541
19542 /* Change the mouse pointer according to what is under X/Y. */
19543 map = Fget_text_property (pos, Qlocal_map, string);
19544 if (!KEYMAPP (map))
19545 map = Fget_text_property (pos, Qkeymap, string);
19546 if (KEYMAPP (map))
19547 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
19548 }
19549
19550 rif->define_frame_cursor (f, cursor);
19551 }
19552
19553
19554 /* EXPORT:
19555 Take proper action when the mouse has moved to position X, Y on
19556 frame F as regards highlighting characters that have mouse-face
19557 properties. Also de-highlighting chars where the mouse was before.
19558 X and Y can be negative or out of range. */
19559
19560 void
19561 note_mouse_highlight (f, x, y)
19562 struct frame *f;
19563 int x, y;
19564 {
19565 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
19566 enum window_part part;
19567 Lisp_Object window;
19568 struct window *w;
19569 Cursor cursor = No_Cursor;
19570 struct buffer *b;
19571
19572 /* When a menu is active, don't highlight because this looks odd. */
19573 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
19574 if (popup_activated ())
19575 return;
19576 #endif
19577
19578 if (NILP (Vmouse_highlight)
19579 || !f->glyphs_initialized_p)
19580 return;
19581
19582 dpyinfo->mouse_face_mouse_x = x;
19583 dpyinfo->mouse_face_mouse_y = y;
19584 dpyinfo->mouse_face_mouse_frame = f;
19585
19586 if (dpyinfo->mouse_face_defer)
19587 return;
19588
19589 if (gc_in_progress)
19590 {
19591 dpyinfo->mouse_face_deferred_gc = 1;
19592 return;
19593 }
19594
19595 /* Which window is that in? */
19596 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
19597
19598 /* If we were displaying active text in another window, clear that. */
19599 if (! EQ (window, dpyinfo->mouse_face_window))
19600 clear_mouse_face (dpyinfo);
19601
19602 /* Not on a window -> return. */
19603 if (!WINDOWP (window))
19604 return;
19605
19606 /* Reset help_echo_string. It will get recomputed below. */
19607 /* ++KFS: X version didn't do this, but it looks harmless. */
19608 help_echo_string = Qnil;
19609
19610 /* Convert to window-relative pixel coordinates. */
19611 w = XWINDOW (window);
19612 frame_to_window_pixel_xy (w, &x, &y);
19613
19614 /* Handle tool-bar window differently since it doesn't display a
19615 buffer. */
19616 if (EQ (window, f->tool_bar_window))
19617 {
19618 note_tool_bar_highlight (f, x, y);
19619 return;
19620 }
19621
19622 /* Mouse is on the mode, header line or margin? */
19623 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
19624 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
19625 {
19626 note_mode_line_or_margin_highlight (w, x, y, part);
19627 return;
19628 }
19629
19630 if (part == ON_VERTICAL_BORDER)
19631 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
19632 else
19633 cursor = FRAME_X_OUTPUT (f)->text_cursor;
19634
19635 /* Are we in a window whose display is up to date?
19636 And verify the buffer's text has not changed. */
19637 b = XBUFFER (w->buffer);
19638 if (part == ON_TEXT
19639 && EQ (w->window_end_valid, w->buffer)
19640 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
19641 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
19642 {
19643 int hpos, vpos, pos, i, area;
19644 struct glyph *glyph;
19645 Lisp_Object object;
19646 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
19647 Lisp_Object *overlay_vec = NULL;
19648 int len, noverlays;
19649 struct buffer *obuf;
19650 int obegv, ozv, same_region;
19651
19652 /* Find the glyph under X/Y. */
19653 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area, 0);
19654
19655 /* Clear mouse face if X/Y not over text. */
19656 if (glyph == NULL
19657 || area != TEXT_AREA
19658 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
19659 {
19660 #if defined (HAVE_NTGUI)
19661 /* ++KFS: Why is this necessary on W32 ? */
19662 clear_mouse_face (dpyinfo);
19663 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
19664 #else
19665 if (clear_mouse_face (dpyinfo))
19666 cursor = No_Cursor;
19667 #endif
19668 goto set_cursor;
19669 }
19670
19671 pos = glyph->charpos;
19672 object = glyph->object;
19673 if (!STRINGP (object) && !BUFFERP (object))
19674 goto set_cursor;
19675
19676 /* If we get an out-of-range value, return now; avoid an error. */
19677 if (BUFFERP (object) && pos > BUF_Z (b))
19678 goto set_cursor;
19679
19680 /* Make the window's buffer temporarily current for
19681 overlays_at and compute_char_face. */
19682 obuf = current_buffer;
19683 current_buffer = b;
19684 obegv = BEGV;
19685 ozv = ZV;
19686 BEGV = BEG;
19687 ZV = Z;
19688
19689 /* Is this char mouse-active or does it have help-echo? */
19690 position = make_number (pos);
19691
19692 if (BUFFERP (object))
19693 {
19694 /* Put all the overlays we want in a vector in overlay_vec.
19695 Store the length in len. If there are more than 10, make
19696 enough space for all, and try again. */
19697 len = 10;
19698 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
19699 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
19700 if (noverlays > len)
19701 {
19702 len = noverlays;
19703 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
19704 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
19705 }
19706
19707 /* Sort overlays into increasing priority order. */
19708 noverlays = sort_overlays (overlay_vec, noverlays, w);
19709 }
19710 else
19711 noverlays = 0;
19712
19713 same_region = (EQ (window, dpyinfo->mouse_face_window)
19714 && vpos >= dpyinfo->mouse_face_beg_row
19715 && vpos <= dpyinfo->mouse_face_end_row
19716 && (vpos > dpyinfo->mouse_face_beg_row
19717 || hpos >= dpyinfo->mouse_face_beg_col)
19718 && (vpos < dpyinfo->mouse_face_end_row
19719 || hpos < dpyinfo->mouse_face_end_col
19720 || dpyinfo->mouse_face_past_end));
19721
19722 if (same_region)
19723 cursor = No_Cursor;
19724
19725 /* Check mouse-face highlighting. */
19726 if (! same_region
19727 /* If there exists an overlay with mouse-face overlapping
19728 the one we are currently highlighting, we have to
19729 check if we enter the overlapping overlay, and then
19730 highlight only that. */
19731 || (OVERLAYP (dpyinfo->mouse_face_overlay)
19732 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
19733 {
19734 /* Find the highest priority overlay that has a mouse-face
19735 property. */
19736 overlay = Qnil;
19737 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
19738 {
19739 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
19740 if (!NILP (mouse_face))
19741 overlay = overlay_vec[i];
19742 }
19743
19744 /* If we're actually highlighting the same overlay as
19745 before, there's no need to do that again. */
19746 if (!NILP (overlay)
19747 && EQ (overlay, dpyinfo->mouse_face_overlay))
19748 goto check_help_echo;
19749
19750 dpyinfo->mouse_face_overlay = overlay;
19751
19752 /* Clear the display of the old active region, if any. */
19753 if (clear_mouse_face (dpyinfo))
19754 cursor = No_Cursor;
19755
19756 /* If no overlay applies, get a text property. */
19757 if (NILP (overlay))
19758 mouse_face = Fget_text_property (position, Qmouse_face, object);
19759
19760 /* Handle the overlay case. */
19761 if (!NILP (overlay))
19762 {
19763 /* Find the range of text around this char that
19764 should be active. */
19765 Lisp_Object before, after;
19766 int ignore;
19767
19768 before = Foverlay_start (overlay);
19769 after = Foverlay_end (overlay);
19770 /* Record this as the current active region. */
19771 fast_find_position (w, XFASTINT (before),
19772 &dpyinfo->mouse_face_beg_col,
19773 &dpyinfo->mouse_face_beg_row,
19774 &dpyinfo->mouse_face_beg_x,
19775 &dpyinfo->mouse_face_beg_y, Qnil);
19776
19777 dpyinfo->mouse_face_past_end
19778 = !fast_find_position (w, XFASTINT (after),
19779 &dpyinfo->mouse_face_end_col,
19780 &dpyinfo->mouse_face_end_row,
19781 &dpyinfo->mouse_face_end_x,
19782 &dpyinfo->mouse_face_end_y, Qnil);
19783 dpyinfo->mouse_face_window = window;
19784
19785 dpyinfo->mouse_face_face_id
19786 = face_at_buffer_position (w, pos, 0, 0,
19787 &ignore, pos + 1,
19788 !dpyinfo->mouse_face_hidden);
19789
19790 /* Display it as active. */
19791 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
19792 cursor = No_Cursor;
19793 }
19794 /* Handle the text property case. */
19795 else if (!NILP (mouse_face) && BUFFERP (object))
19796 {
19797 /* Find the range of text around this char that
19798 should be active. */
19799 Lisp_Object before, after, beginning, end;
19800 int ignore;
19801
19802 beginning = Fmarker_position (w->start);
19803 end = make_number (BUF_Z (XBUFFER (object))
19804 - XFASTINT (w->window_end_pos));
19805 before
19806 = Fprevious_single_property_change (make_number (pos + 1),
19807 Qmouse_face,
19808 object, beginning);
19809 after
19810 = Fnext_single_property_change (position, Qmouse_face,
19811 object, end);
19812
19813 /* Record this as the current active region. */
19814 fast_find_position (w, XFASTINT (before),
19815 &dpyinfo->mouse_face_beg_col,
19816 &dpyinfo->mouse_face_beg_row,
19817 &dpyinfo->mouse_face_beg_x,
19818 &dpyinfo->mouse_face_beg_y, Qnil);
19819 dpyinfo->mouse_face_past_end
19820 = !fast_find_position (w, XFASTINT (after),
19821 &dpyinfo->mouse_face_end_col,
19822 &dpyinfo->mouse_face_end_row,
19823 &dpyinfo->mouse_face_end_x,
19824 &dpyinfo->mouse_face_end_y, Qnil);
19825 dpyinfo->mouse_face_window = window;
19826
19827 if (BUFFERP (object))
19828 dpyinfo->mouse_face_face_id
19829 = face_at_buffer_position (w, pos, 0, 0,
19830 &ignore, pos + 1,
19831 !dpyinfo->mouse_face_hidden);
19832
19833 /* Display it as active. */
19834 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
19835 cursor = No_Cursor;
19836 }
19837 else if (!NILP (mouse_face) && STRINGP (object))
19838 {
19839 Lisp_Object b, e;
19840 int ignore;
19841
19842 b = Fprevious_single_property_change (make_number (pos + 1),
19843 Qmouse_face,
19844 object, Qnil);
19845 e = Fnext_single_property_change (position, Qmouse_face,
19846 object, Qnil);
19847 if (NILP (b))
19848 b = make_number (0);
19849 if (NILP (e))
19850 e = make_number (SCHARS (object) - 1);
19851 fast_find_string_pos (w, XINT (b), object,
19852 &dpyinfo->mouse_face_beg_col,
19853 &dpyinfo->mouse_face_beg_row,
19854 &dpyinfo->mouse_face_beg_x,
19855 &dpyinfo->mouse_face_beg_y, 0);
19856 fast_find_string_pos (w, XINT (e), object,
19857 &dpyinfo->mouse_face_end_col,
19858 &dpyinfo->mouse_face_end_row,
19859 &dpyinfo->mouse_face_end_x,
19860 &dpyinfo->mouse_face_end_y, 1);
19861 dpyinfo->mouse_face_past_end = 0;
19862 dpyinfo->mouse_face_window = window;
19863 dpyinfo->mouse_face_face_id
19864 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
19865 glyph->face_id, 1);
19866 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
19867 cursor = No_Cursor;
19868 }
19869 else if (STRINGP (object) && NILP (mouse_face))
19870 {
19871 /* A string which doesn't have mouse-face, but
19872 the text ``under'' it might have. */
19873 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
19874 int start = MATRIX_ROW_START_CHARPOS (r);
19875
19876 pos = string_buffer_position (w, object, start);
19877 if (pos > 0)
19878 mouse_face = get_char_property_and_overlay (make_number (pos),
19879 Qmouse_face,
19880 w->buffer,
19881 &overlay);
19882 if (!NILP (mouse_face) && !NILP (overlay))
19883 {
19884 Lisp_Object before = Foverlay_start (overlay);
19885 Lisp_Object after = Foverlay_end (overlay);
19886 int ignore;
19887
19888 /* Note that we might not be able to find position
19889 BEFORE in the glyph matrix if the overlay is
19890 entirely covered by a `display' property. In
19891 this case, we overshoot. So let's stop in
19892 the glyph matrix before glyphs for OBJECT. */
19893 fast_find_position (w, XFASTINT (before),
19894 &dpyinfo->mouse_face_beg_col,
19895 &dpyinfo->mouse_face_beg_row,
19896 &dpyinfo->mouse_face_beg_x,
19897 &dpyinfo->mouse_face_beg_y,
19898 object);
19899
19900 dpyinfo->mouse_face_past_end
19901 = !fast_find_position (w, XFASTINT (after),
19902 &dpyinfo->mouse_face_end_col,
19903 &dpyinfo->mouse_face_end_row,
19904 &dpyinfo->mouse_face_end_x,
19905 &dpyinfo->mouse_face_end_y,
19906 Qnil);
19907 dpyinfo->mouse_face_window = window;
19908 dpyinfo->mouse_face_face_id
19909 = face_at_buffer_position (w, pos, 0, 0,
19910 &ignore, pos + 1,
19911 !dpyinfo->mouse_face_hidden);
19912
19913 /* Display it as active. */
19914 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
19915 cursor = No_Cursor;
19916 }
19917 }
19918 }
19919
19920 check_help_echo:
19921
19922 /* Look for a `help-echo' property. */
19923 {
19924 Lisp_Object help, overlay;
19925
19926 /* Check overlays first. */
19927 help = overlay = Qnil;
19928 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
19929 {
19930 overlay = overlay_vec[i];
19931 help = Foverlay_get (overlay, Qhelp_echo);
19932 }
19933
19934 if (!NILP (help))
19935 {
19936 help_echo_string = help;
19937 help_echo_window = window;
19938 help_echo_object = overlay;
19939 help_echo_pos = pos;
19940 }
19941 else
19942 {
19943 Lisp_Object object = glyph->object;
19944 int charpos = glyph->charpos;
19945
19946 /* Try text properties. */
19947 if (STRINGP (object)
19948 && charpos >= 0
19949 && charpos < SCHARS (object))
19950 {
19951 help = Fget_text_property (make_number (charpos),
19952 Qhelp_echo, object);
19953 if (NILP (help))
19954 {
19955 /* If the string itself doesn't specify a help-echo,
19956 see if the buffer text ``under'' it does. */
19957 struct glyph_row *r
19958 = MATRIX_ROW (w->current_matrix, vpos);
19959 int start = MATRIX_ROW_START_CHARPOS (r);
19960 int pos = string_buffer_position (w, object, start);
19961 if (pos > 0)
19962 {
19963 help = Fget_char_property (make_number (pos),
19964 Qhelp_echo, w->buffer);
19965 if (!NILP (help))
19966 {
19967 charpos = pos;
19968 object = w->buffer;
19969 }
19970 }
19971 }
19972 }
19973 else if (BUFFERP (object)
19974 && charpos >= BEGV
19975 && charpos < ZV)
19976 help = Fget_text_property (make_number (charpos), Qhelp_echo,
19977 object);
19978
19979 if (!NILP (help))
19980 {
19981 help_echo_string = help;
19982 help_echo_window = window;
19983 help_echo_object = object;
19984 help_echo_pos = charpos;
19985 }
19986 }
19987 }
19988
19989 BEGV = obegv;
19990 ZV = ozv;
19991 current_buffer = obuf;
19992 }
19993
19994 set_cursor:
19995
19996 #ifndef HAVE_CARBON
19997 if (cursor != No_Cursor)
19998 #else
19999 if (bcmp (&cursor, &No_Cursor, sizeof (Cursor)))
20000 #endif
20001 rif->define_frame_cursor (f, cursor);
20002 }
20003
20004
20005 /* EXPORT for RIF:
20006 Clear any mouse-face on window W. This function is part of the
20007 redisplay interface, and is called from try_window_id and similar
20008 functions to ensure the mouse-highlight is off. */
20009
20010 void
20011 x_clear_window_mouse_face (w)
20012 struct window *w;
20013 {
20014 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20015 Lisp_Object window;
20016
20017 BLOCK_INPUT;
20018 XSETWINDOW (window, w);
20019 if (EQ (window, dpyinfo->mouse_face_window))
20020 clear_mouse_face (dpyinfo);
20021 UNBLOCK_INPUT;
20022 }
20023
20024
20025 /* EXPORT:
20026 Just discard the mouse face information for frame F, if any.
20027 This is used when the size of F is changed. */
20028
20029 void
20030 cancel_mouse_face (f)
20031 struct frame *f;
20032 {
20033 Lisp_Object window;
20034 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20035
20036 window = dpyinfo->mouse_face_window;
20037 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
20038 {
20039 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20040 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20041 dpyinfo->mouse_face_window = Qnil;
20042 }
20043 }
20044
20045
20046 #endif /* HAVE_WINDOW_SYSTEM */
20047
20048 \f
20049 /***********************************************************************
20050 Exposure Events
20051 ***********************************************************************/
20052
20053 #ifdef HAVE_WINDOW_SYSTEM
20054
20055 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
20056 which intersects rectangle R. R is in window-relative coordinates. */
20057
20058 static void
20059 expose_area (w, row, r, area)
20060 struct window *w;
20061 struct glyph_row *row;
20062 XRectangle *r;
20063 enum glyph_row_area area;
20064 {
20065 struct glyph *first = row->glyphs[area];
20066 struct glyph *end = row->glyphs[area] + row->used[area];
20067 struct glyph *last;
20068 int first_x, start_x, x;
20069
20070 if (area == TEXT_AREA && row->fill_line_p)
20071 /* If row extends face to end of line write the whole line. */
20072 draw_glyphs (w, 0, row, area,
20073 0, row->used[area],
20074 DRAW_NORMAL_TEXT, 0);
20075 else
20076 {
20077 /* Set START_X to the window-relative start position for drawing glyphs of
20078 AREA. The first glyph of the text area can be partially visible.
20079 The first glyphs of other areas cannot. */
20080 start_x = window_box_left_offset (w, area);
20081 if (area == TEXT_AREA)
20082 start_x += row->x;
20083 x = start_x;
20084
20085 /* Find the first glyph that must be redrawn. */
20086 while (first < end
20087 && x + first->pixel_width < r->x)
20088 {
20089 x += first->pixel_width;
20090 ++first;
20091 }
20092
20093 /* Find the last one. */
20094 last = first;
20095 first_x = x;
20096 while (last < end
20097 && x < r->x + r->width)
20098 {
20099 x += last->pixel_width;
20100 ++last;
20101 }
20102
20103 /* Repaint. */
20104 if (last > first)
20105 draw_glyphs (w, first_x - start_x, row, area,
20106 first - row->glyphs[area], last - row->glyphs[area],
20107 DRAW_NORMAL_TEXT, 0);
20108 }
20109 }
20110
20111
20112 /* Redraw the parts of the glyph row ROW on window W intersecting
20113 rectangle R. R is in window-relative coordinates. Value is
20114 non-zero if mouse-face was overwritten. */
20115
20116 static int
20117 expose_line (w, row, r)
20118 struct window *w;
20119 struct glyph_row *row;
20120 XRectangle *r;
20121 {
20122 xassert (row->enabled_p);
20123
20124 if (row->mode_line_p || w->pseudo_window_p)
20125 draw_glyphs (w, 0, row, TEXT_AREA,
20126 0, row->used[TEXT_AREA],
20127 DRAW_NORMAL_TEXT, 0);
20128 else
20129 {
20130 if (row->used[LEFT_MARGIN_AREA])
20131 expose_area (w, row, r, LEFT_MARGIN_AREA);
20132 if (row->used[TEXT_AREA])
20133 expose_area (w, row, r, TEXT_AREA);
20134 if (row->used[RIGHT_MARGIN_AREA])
20135 expose_area (w, row, r, RIGHT_MARGIN_AREA);
20136 draw_row_fringe_bitmaps (w, row);
20137 }
20138
20139 return row->mouse_face_p;
20140 }
20141
20142
20143 /* Redraw those parts of glyphs rows during expose event handling that
20144 overlap other rows. Redrawing of an exposed line writes over parts
20145 of lines overlapping that exposed line; this function fixes that.
20146
20147 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
20148 row in W's current matrix that is exposed and overlaps other rows.
20149 LAST_OVERLAPPING_ROW is the last such row. */
20150
20151 static void
20152 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
20153 struct window *w;
20154 struct glyph_row *first_overlapping_row;
20155 struct glyph_row *last_overlapping_row;
20156 {
20157 struct glyph_row *row;
20158
20159 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
20160 if (row->overlapping_p)
20161 {
20162 xassert (row->enabled_p && !row->mode_line_p);
20163
20164 if (row->used[LEFT_MARGIN_AREA])
20165 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
20166
20167 if (row->used[TEXT_AREA])
20168 x_fix_overlapping_area (w, row, TEXT_AREA);
20169
20170 if (row->used[RIGHT_MARGIN_AREA])
20171 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
20172 }
20173 }
20174
20175
20176 /* Return non-zero if W's cursor intersects rectangle R. */
20177
20178 static int
20179 phys_cursor_in_rect_p (w, r)
20180 struct window *w;
20181 XRectangle *r;
20182 {
20183 XRectangle cr, result;
20184 struct glyph *cursor_glyph;
20185
20186 cursor_glyph = get_phys_cursor_glyph (w);
20187 if (cursor_glyph)
20188 {
20189 cr.x = w->phys_cursor.x;
20190 cr.y = w->phys_cursor.y;
20191 cr.width = cursor_glyph->pixel_width;
20192 cr.height = w->phys_cursor_height;
20193 /* ++KFS: W32 version used W32-specific IntersectRect here, but
20194 I assume the effect is the same -- and this is portable. */
20195 return x_intersect_rectangles (&cr, r, &result);
20196 }
20197 else
20198 return 0;
20199 }
20200
20201
20202 /* EXPORT:
20203 Draw a vertical window border to the right of window W if W doesn't
20204 have vertical scroll bars. */
20205
20206 void
20207 x_draw_vertical_border (w)
20208 struct window *w;
20209 {
20210 /* We could do better, if we knew what type of scroll-bar the adjacent
20211 windows (on either side) have... But we don't :-(
20212 However, I think this works ok. ++KFS 2003-04-25 */
20213
20214 /* Redraw borders between horizontally adjacent windows. Don't
20215 do it for frames with vertical scroll bars because either the
20216 right scroll bar of a window, or the left scroll bar of its
20217 neighbor will suffice as a border. */
20218 if (!WINDOW_RIGHTMOST_P (w)
20219 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
20220 {
20221 int x0, x1, y0, y1;
20222
20223 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
20224 y1 -= 1;
20225
20226 rif->draw_vertical_window_border (w, x1, y0, y1);
20227 }
20228 else if (!WINDOW_LEFTMOST_P (w)
20229 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
20230 {
20231 int x0, x1, y0, y1;
20232
20233 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
20234 y1 -= 1;
20235
20236 rif->draw_vertical_window_border (w, x0, y0, y1);
20237 }
20238 }
20239
20240
20241 /* Redraw the part of window W intersection rectangle FR. Pixel
20242 coordinates in FR are frame-relative. Call this function with
20243 input blocked. Value is non-zero if the exposure overwrites
20244 mouse-face. */
20245
20246 static int
20247 expose_window (w, fr)
20248 struct window *w;
20249 XRectangle *fr;
20250 {
20251 struct frame *f = XFRAME (w->frame);
20252 XRectangle wr, r;
20253 int mouse_face_overwritten_p = 0;
20254
20255 /* If window is not yet fully initialized, do nothing. This can
20256 happen when toolkit scroll bars are used and a window is split.
20257 Reconfiguring the scroll bar will generate an expose for a newly
20258 created window. */
20259 if (w->current_matrix == NULL)
20260 return 0;
20261
20262 /* When we're currently updating the window, display and current
20263 matrix usually don't agree. Arrange for a thorough display
20264 later. */
20265 if (w == updated_window)
20266 {
20267 SET_FRAME_GARBAGED (f);
20268 return 0;
20269 }
20270
20271 /* Frame-relative pixel rectangle of W. */
20272 wr.x = WINDOW_LEFT_EDGE_X (w);
20273 wr.y = WINDOW_TOP_EDGE_Y (w);
20274 wr.width = WINDOW_TOTAL_WIDTH (w);
20275 wr.height = WINDOW_TOTAL_HEIGHT (w);
20276
20277 if (x_intersect_rectangles (fr, &wr, &r))
20278 {
20279 int yb = window_text_bottom_y (w);
20280 struct glyph_row *row;
20281 int cursor_cleared_p;
20282 struct glyph_row *first_overlapping_row, *last_overlapping_row;
20283
20284 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
20285 r.x, r.y, r.width, r.height));
20286
20287 /* Convert to window coordinates. */
20288 r.x -= WINDOW_LEFT_EDGE_X (w);
20289 r.y -= WINDOW_TOP_EDGE_Y (w);
20290
20291 /* Turn off the cursor. */
20292 if (!w->pseudo_window_p
20293 && phys_cursor_in_rect_p (w, &r))
20294 {
20295 x_clear_cursor (w);
20296 cursor_cleared_p = 1;
20297 }
20298 else
20299 cursor_cleared_p = 0;
20300
20301 /* Update lines intersecting rectangle R. */
20302 first_overlapping_row = last_overlapping_row = NULL;
20303 for (row = w->current_matrix->rows;
20304 row->enabled_p;
20305 ++row)
20306 {
20307 int y0 = row->y;
20308 int y1 = MATRIX_ROW_BOTTOM_Y (row);
20309
20310 if ((y0 >= r.y && y0 < r.y + r.height)
20311 || (y1 > r.y && y1 < r.y + r.height)
20312 || (r.y >= y0 && r.y < y1)
20313 || (r.y + r.height > y0 && r.y + r.height < y1))
20314 {
20315 if (row->overlapping_p)
20316 {
20317 if (first_overlapping_row == NULL)
20318 first_overlapping_row = row;
20319 last_overlapping_row = row;
20320 }
20321
20322 if (expose_line (w, row, &r))
20323 mouse_face_overwritten_p = 1;
20324 }
20325
20326 if (y1 >= yb)
20327 break;
20328 }
20329
20330 /* Display the mode line if there is one. */
20331 if (WINDOW_WANTS_MODELINE_P (w)
20332 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
20333 row->enabled_p)
20334 && row->y < r.y + r.height)
20335 {
20336 if (expose_line (w, row, &r))
20337 mouse_face_overwritten_p = 1;
20338 }
20339
20340 if (!w->pseudo_window_p)
20341 {
20342 /* Fix the display of overlapping rows. */
20343 if (first_overlapping_row)
20344 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
20345
20346 /* Draw border between windows. */
20347 x_draw_vertical_border (w);
20348
20349 /* Turn the cursor on again. */
20350 if (cursor_cleared_p)
20351 update_window_cursor (w, 1);
20352 }
20353 }
20354
20355 #ifdef HAVE_CARBON
20356 /* Display scroll bar for this window. */
20357 if (!NILP (w->vertical_scroll_bar))
20358 {
20359 /* ++KFS:
20360 If this doesn't work here (maybe some header files are missing),
20361 make a function in macterm.c and call it to do the job! */
20362 ControlHandle ch
20363 = SCROLL_BAR_CONTROL_HANDLE (XSCROLL_BAR (w->vertical_scroll_bar));
20364
20365 Draw1Control (ch);
20366 }
20367 #endif
20368
20369 return mouse_face_overwritten_p;
20370 }
20371
20372
20373
20374 /* Redraw (parts) of all windows in the window tree rooted at W that
20375 intersect R. R contains frame pixel coordinates. Value is
20376 non-zero if the exposure overwrites mouse-face. */
20377
20378 static int
20379 expose_window_tree (w, r)
20380 struct window *w;
20381 XRectangle *r;
20382 {
20383 struct frame *f = XFRAME (w->frame);
20384 int mouse_face_overwritten_p = 0;
20385
20386 while (w && !FRAME_GARBAGED_P (f))
20387 {
20388 if (!NILP (w->hchild))
20389 mouse_face_overwritten_p
20390 |= expose_window_tree (XWINDOW (w->hchild), r);
20391 else if (!NILP (w->vchild))
20392 mouse_face_overwritten_p
20393 |= expose_window_tree (XWINDOW (w->vchild), r);
20394 else
20395 mouse_face_overwritten_p |= expose_window (w, r);
20396
20397 w = NILP (w->next) ? NULL : XWINDOW (w->next);
20398 }
20399
20400 return mouse_face_overwritten_p;
20401 }
20402
20403
20404 /* EXPORT:
20405 Redisplay an exposed area of frame F. X and Y are the upper-left
20406 corner of the exposed rectangle. W and H are width and height of
20407 the exposed area. All are pixel values. W or H zero means redraw
20408 the entire frame. */
20409
20410 void
20411 expose_frame (f, x, y, w, h)
20412 struct frame *f;
20413 int x, y, w, h;
20414 {
20415 XRectangle r;
20416 int mouse_face_overwritten_p = 0;
20417
20418 TRACE ((stderr, "expose_frame "));
20419
20420 /* No need to redraw if frame will be redrawn soon. */
20421 if (FRAME_GARBAGED_P (f))
20422 {
20423 TRACE ((stderr, " garbaged\n"));
20424 return;
20425 }
20426
20427 #ifdef HAVE_CARBON
20428 /* MAC_TODO: this is a kludge, but if scroll bars are not activated
20429 or deactivated here, for unknown reasons, activated scroll bars
20430 are shown in deactivated frames in some instances. */
20431 if (f == FRAME_MAC_DISPLAY_INFO (f)->x_focus_frame)
20432 activate_scroll_bars (f);
20433 else
20434 deactivate_scroll_bars (f);
20435 #endif
20436
20437 /* If basic faces haven't been realized yet, there is no point in
20438 trying to redraw anything. This can happen when we get an expose
20439 event while Emacs is starting, e.g. by moving another window. */
20440 if (FRAME_FACE_CACHE (f) == NULL
20441 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
20442 {
20443 TRACE ((stderr, " no faces\n"));
20444 return;
20445 }
20446
20447 if (w == 0 || h == 0)
20448 {
20449 r.x = r.y = 0;
20450 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
20451 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
20452 }
20453 else
20454 {
20455 r.x = x;
20456 r.y = y;
20457 r.width = w;
20458 r.height = h;
20459 }
20460
20461 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
20462 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
20463
20464 if (WINDOWP (f->tool_bar_window))
20465 mouse_face_overwritten_p
20466 |= expose_window (XWINDOW (f->tool_bar_window), &r);
20467
20468 #ifdef HAVE_X_WINDOWS
20469 #ifndef MSDOS
20470 #ifndef USE_X_TOOLKIT
20471 if (WINDOWP (f->menu_bar_window))
20472 mouse_face_overwritten_p
20473 |= expose_window (XWINDOW (f->menu_bar_window), &r);
20474 #endif /* not USE_X_TOOLKIT */
20475 #endif
20476 #endif
20477
20478 /* Some window managers support a focus-follows-mouse style with
20479 delayed raising of frames. Imagine a partially obscured frame,
20480 and moving the mouse into partially obscured mouse-face on that
20481 frame. The visible part of the mouse-face will be highlighted,
20482 then the WM raises the obscured frame. With at least one WM, KDE
20483 2.1, Emacs is not getting any event for the raising of the frame
20484 (even tried with SubstructureRedirectMask), only Expose events.
20485 These expose events will draw text normally, i.e. not
20486 highlighted. Which means we must redo the highlight here.
20487 Subsume it under ``we love X''. --gerd 2001-08-15 */
20488 /* Included in Windows version because Windows most likely does not
20489 do the right thing if any third party tool offers
20490 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
20491 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
20492 {
20493 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20494 if (f == dpyinfo->mouse_face_mouse_frame)
20495 {
20496 int x = dpyinfo->mouse_face_mouse_x;
20497 int y = dpyinfo->mouse_face_mouse_y;
20498 clear_mouse_face (dpyinfo);
20499 note_mouse_highlight (f, x, y);
20500 }
20501 }
20502 }
20503
20504
20505 /* EXPORT:
20506 Determine the intersection of two rectangles R1 and R2. Return
20507 the intersection in *RESULT. Value is non-zero if RESULT is not
20508 empty. */
20509
20510 int
20511 x_intersect_rectangles (r1, r2, result)
20512 XRectangle *r1, *r2, *result;
20513 {
20514 XRectangle *left, *right;
20515 XRectangle *upper, *lower;
20516 int intersection_p = 0;
20517
20518 /* Rearrange so that R1 is the left-most rectangle. */
20519 if (r1->x < r2->x)
20520 left = r1, right = r2;
20521 else
20522 left = r2, right = r1;
20523
20524 /* X0 of the intersection is right.x0, if this is inside R1,
20525 otherwise there is no intersection. */
20526 if (right->x <= left->x + left->width)
20527 {
20528 result->x = right->x;
20529
20530 /* The right end of the intersection is the minimum of the
20531 the right ends of left and right. */
20532 result->width = (min (left->x + left->width, right->x + right->width)
20533 - result->x);
20534
20535 /* Same game for Y. */
20536 if (r1->y < r2->y)
20537 upper = r1, lower = r2;
20538 else
20539 upper = r2, lower = r1;
20540
20541 /* The upper end of the intersection is lower.y0, if this is inside
20542 of upper. Otherwise, there is no intersection. */
20543 if (lower->y <= upper->y + upper->height)
20544 {
20545 result->y = lower->y;
20546
20547 /* The lower end of the intersection is the minimum of the lower
20548 ends of upper and lower. */
20549 result->height = (min (lower->y + lower->height,
20550 upper->y + upper->height)
20551 - result->y);
20552 intersection_p = 1;
20553 }
20554 }
20555
20556 return intersection_p;
20557 }
20558
20559 #endif /* HAVE_WINDOW_SYSTEM */
20560
20561 \f
20562 /***********************************************************************
20563 Initialization
20564 ***********************************************************************/
20565
20566 void
20567 syms_of_xdisp ()
20568 {
20569 Vwith_echo_area_save_vector = Qnil;
20570 staticpro (&Vwith_echo_area_save_vector);
20571
20572 Vmessage_stack = Qnil;
20573 staticpro (&Vmessage_stack);
20574
20575 Qinhibit_redisplay = intern ("inhibit-redisplay");
20576 staticpro (&Qinhibit_redisplay);
20577
20578 message_dolog_marker1 = Fmake_marker ();
20579 staticpro (&message_dolog_marker1);
20580 message_dolog_marker2 = Fmake_marker ();
20581 staticpro (&message_dolog_marker2);
20582 message_dolog_marker3 = Fmake_marker ();
20583 staticpro (&message_dolog_marker3);
20584
20585 #if GLYPH_DEBUG
20586 defsubr (&Sdump_frame_glyph_matrix);
20587 defsubr (&Sdump_glyph_matrix);
20588 defsubr (&Sdump_glyph_row);
20589 defsubr (&Sdump_tool_bar_row);
20590 defsubr (&Strace_redisplay);
20591 defsubr (&Strace_to_stderr);
20592 #endif
20593 #ifdef HAVE_WINDOW_SYSTEM
20594 defsubr (&Stool_bar_lines_needed);
20595 #endif
20596 defsubr (&Sformat_mode_line);
20597
20598 staticpro (&Qmenu_bar_update_hook);
20599 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
20600
20601 staticpro (&Qoverriding_terminal_local_map);
20602 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
20603
20604 staticpro (&Qoverriding_local_map);
20605 Qoverriding_local_map = intern ("overriding-local-map");
20606
20607 staticpro (&Qwindow_scroll_functions);
20608 Qwindow_scroll_functions = intern ("window-scroll-functions");
20609
20610 staticpro (&Qredisplay_end_trigger_functions);
20611 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
20612
20613 staticpro (&Qinhibit_point_motion_hooks);
20614 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
20615
20616 QCdata = intern (":data");
20617 staticpro (&QCdata);
20618 Qdisplay = intern ("display");
20619 staticpro (&Qdisplay);
20620 Qspace_width = intern ("space-width");
20621 staticpro (&Qspace_width);
20622 Qraise = intern ("raise");
20623 staticpro (&Qraise);
20624 Qspace = intern ("space");
20625 staticpro (&Qspace);
20626 Qmargin = intern ("margin");
20627 staticpro (&Qmargin);
20628 Qleft_margin = intern ("left-margin");
20629 staticpro (&Qleft_margin);
20630 Qright_margin = intern ("right-margin");
20631 staticpro (&Qright_margin);
20632 Qalign_to = intern ("align-to");
20633 staticpro (&Qalign_to);
20634 QCalign_to = intern (":align-to");
20635 staticpro (&QCalign_to);
20636 Qrelative_width = intern ("relative-width");
20637 staticpro (&Qrelative_width);
20638 QCrelative_width = intern (":relative-width");
20639 staticpro (&QCrelative_width);
20640 QCrelative_height = intern (":relative-height");
20641 staticpro (&QCrelative_height);
20642 QCeval = intern (":eval");
20643 staticpro (&QCeval);
20644 QCpropertize = intern (":propertize");
20645 staticpro (&QCpropertize);
20646 QCfile = intern (":file");
20647 staticpro (&QCfile);
20648 Qfontified = intern ("fontified");
20649 staticpro (&Qfontified);
20650 Qfontification_functions = intern ("fontification-functions");
20651 staticpro (&Qfontification_functions);
20652 Qtrailing_whitespace = intern ("trailing-whitespace");
20653 staticpro (&Qtrailing_whitespace);
20654 Qimage = intern ("image");
20655 staticpro (&Qimage);
20656 Qmessage_truncate_lines = intern ("message-truncate-lines");
20657 staticpro (&Qmessage_truncate_lines);
20658 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
20659 staticpro (&Qcursor_in_non_selected_windows);
20660 Qgrow_only = intern ("grow-only");
20661 staticpro (&Qgrow_only);
20662 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
20663 staticpro (&Qinhibit_menubar_update);
20664 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
20665 staticpro (&Qinhibit_eval_during_redisplay);
20666 Qposition = intern ("position");
20667 staticpro (&Qposition);
20668 Qbuffer_position = intern ("buffer-position");
20669 staticpro (&Qbuffer_position);
20670 Qobject = intern ("object");
20671 staticpro (&Qobject);
20672 Qbar = intern ("bar");
20673 staticpro (&Qbar);
20674 Qhbar = intern ("hbar");
20675 staticpro (&Qhbar);
20676 Qbox = intern ("box");
20677 staticpro (&Qbox);
20678 Qhollow = intern ("hollow");
20679 staticpro (&Qhollow);
20680 Qrisky_local_variable = intern ("risky-local-variable");
20681 staticpro (&Qrisky_local_variable);
20682 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
20683 staticpro (&Qinhibit_free_realized_faces);
20684
20685 list_of_error = Fcons (intern ("error"), Qnil);
20686 staticpro (&list_of_error);
20687
20688 last_arrow_position = Qnil;
20689 last_arrow_string = Qnil;
20690 staticpro (&last_arrow_position);
20691 staticpro (&last_arrow_string);
20692
20693 echo_buffer[0] = echo_buffer[1] = Qnil;
20694 staticpro (&echo_buffer[0]);
20695 staticpro (&echo_buffer[1]);
20696
20697 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
20698 staticpro (&echo_area_buffer[0]);
20699 staticpro (&echo_area_buffer[1]);
20700
20701 Vmessages_buffer_name = build_string ("*Messages*");
20702 staticpro (&Vmessages_buffer_name);
20703
20704 mode_line_proptrans_alist = Qnil;
20705 staticpro (&mode_line_proptrans_alist);
20706
20707 mode_line_string_list = Qnil;
20708 staticpro (&mode_line_string_list);
20709
20710 help_echo_string = Qnil;
20711 staticpro (&help_echo_string);
20712 help_echo_object = Qnil;
20713 staticpro (&help_echo_object);
20714 help_echo_window = Qnil;
20715 staticpro (&help_echo_window);
20716 previous_help_echo_string = Qnil;
20717 staticpro (&previous_help_echo_string);
20718 help_echo_pos = -1;
20719
20720 #ifdef HAVE_WINDOW_SYSTEM
20721 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
20722 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
20723 For example, if a block cursor is over a tab, it will be drawn as
20724 wide as that tab on the display. */);
20725 x_stretch_cursor_p = 0;
20726 #endif
20727
20728 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
20729 doc: /* Non-nil means highlight trailing whitespace.
20730 The face used for trailing whitespace is `trailing-whitespace'. */);
20731 Vshow_trailing_whitespace = Qnil;
20732
20733 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
20734 doc: /* Non-nil means don't actually do any redisplay.
20735 This is used for internal purposes. */);
20736 Vinhibit_redisplay = Qnil;
20737
20738 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
20739 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
20740 Vglobal_mode_string = Qnil;
20741
20742 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
20743 doc: /* Marker for where to display an arrow on top of the buffer text.
20744 This must be the beginning of a line in order to work.
20745 See also `overlay-arrow-string'. */);
20746 Voverlay_arrow_position = Qnil;
20747
20748 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
20749 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
20750 Voverlay_arrow_string = Qnil;
20751
20752 DEFVAR_INT ("scroll-step", &scroll_step,
20753 doc: /* *The number of lines to try scrolling a window by when point moves out.
20754 If that fails to bring point back on frame, point is centered instead.
20755 If this is zero, point is always centered after it moves off frame.
20756 If you want scrolling to always be a line at a time, you should set
20757 `scroll-conservatively' to a large value rather than set this to 1. */);
20758
20759 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
20760 doc: /* *Scroll up to this many lines, to bring point back on screen.
20761 A value of zero means to scroll the text to center point vertically
20762 in the window. */);
20763 scroll_conservatively = 0;
20764
20765 DEFVAR_INT ("scroll-margin", &scroll_margin,
20766 doc: /* *Number of lines of margin at the top and bottom of a window.
20767 Recenter the window whenever point gets within this many lines
20768 of the top or bottom of the window. */);
20769 scroll_margin = 0;
20770
20771 #if GLYPH_DEBUG
20772 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
20773 #endif
20774
20775 DEFVAR_BOOL ("truncate-partial-width-windows",
20776 &truncate_partial_width_windows,
20777 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
20778 truncate_partial_width_windows = 1;
20779
20780 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
20781 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
20782 Any other value means to use the appropriate face, `mode-line',
20783 `header-line', or `menu' respectively. */);
20784 mode_line_inverse_video = 1;
20785
20786 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
20787 doc: /* *Maximum buffer size for which line number should be displayed.
20788 If the buffer is bigger than this, the line number does not appear
20789 in the mode line. A value of nil means no limit. */);
20790 Vline_number_display_limit = Qnil;
20791
20792 DEFVAR_INT ("line-number-display-limit-width",
20793 &line_number_display_limit_width,
20794 doc: /* *Maximum line width (in characters) for line number display.
20795 If the average length of the lines near point is bigger than this, then the
20796 line number may be omitted from the mode line. */);
20797 line_number_display_limit_width = 200;
20798
20799 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
20800 doc: /* *Non-nil means highlight region even in nonselected windows. */);
20801 highlight_nonselected_windows = 0;
20802
20803 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
20804 doc: /* Non-nil if more than one frame is visible on this display.
20805 Minibuffer-only frames don't count, but iconified frames do.
20806 This variable is not guaranteed to be accurate except while processing
20807 `frame-title-format' and `icon-title-format'. */);
20808
20809 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
20810 doc: /* Template for displaying the title bar of visible frames.
20811 \(Assuming the window manager supports this feature.)
20812 This variable has the same structure as `mode-line-format' (which see),
20813 and is used only on frames for which no explicit name has been set
20814 \(see `modify-frame-parameters'). */);
20815 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
20816 doc: /* Template for displaying the title bar of an iconified frame.
20817 \(Assuming the window manager supports this feature.)
20818 This variable has the same structure as `mode-line-format' (which see),
20819 and is used only on frames for which no explicit name has been set
20820 \(see `modify-frame-parameters'). */);
20821 Vicon_title_format
20822 = Vframe_title_format
20823 = Fcons (intern ("multiple-frames"),
20824 Fcons (build_string ("%b"),
20825 Fcons (Fcons (empty_string,
20826 Fcons (intern ("invocation-name"),
20827 Fcons (build_string ("@"),
20828 Fcons (intern ("system-name"),
20829 Qnil)))),
20830 Qnil)));
20831
20832 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
20833 doc: /* Maximum number of lines to keep in the message log buffer.
20834 If nil, disable message logging. If t, log messages but don't truncate
20835 the buffer when it becomes large. */);
20836 Vmessage_log_max = make_number (50);
20837
20838 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
20839 doc: /* Functions called before redisplay, if window sizes have changed.
20840 The value should be a list of functions that take one argument.
20841 Just before redisplay, for each frame, if any of its windows have changed
20842 size since the last redisplay, or have been split or deleted,
20843 all the functions in the list are called, with the frame as argument. */);
20844 Vwindow_size_change_functions = Qnil;
20845
20846 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
20847 doc: /* List of Functions to call before redisplaying a window with scrolling.
20848 Each function is called with two arguments, the window
20849 and its new display-start position. Note that the value of `window-end'
20850 is not valid when these functions are called. */);
20851 Vwindow_scroll_functions = Qnil;
20852
20853 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
20854 doc: /* *Non-nil means autoselect window with mouse pointer. */);
20855 mouse_autoselect_window = 0;
20856
20857 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
20858 doc: /* *Non-nil means automatically resize tool-bars.
20859 This increases a tool-bar's height if not all tool-bar items are visible.
20860 It decreases a tool-bar's height when it would display blank lines
20861 otherwise. */);
20862 auto_resize_tool_bars_p = 1;
20863
20864 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
20865 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
20866 auto_raise_tool_bar_buttons_p = 1;
20867
20868 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
20869 doc: /* *Margin around tool-bar buttons in pixels.
20870 If an integer, use that for both horizontal and vertical margins.
20871 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
20872 HORZ specifying the horizontal margin, and VERT specifying the
20873 vertical margin. */);
20874 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
20875
20876 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
20877 doc: /* *Relief thickness of tool-bar buttons. */);
20878 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
20879
20880 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
20881 doc: /* List of functions to call to fontify regions of text.
20882 Each function is called with one argument POS. Functions must
20883 fontify a region starting at POS in the current buffer, and give
20884 fontified regions the property `fontified'. */);
20885 Vfontification_functions = Qnil;
20886 Fmake_variable_buffer_local (Qfontification_functions);
20887
20888 DEFVAR_BOOL ("unibyte-display-via-language-environment",
20889 &unibyte_display_via_language_environment,
20890 doc: /* *Non-nil means display unibyte text according to language environment.
20891 Specifically this means that unibyte non-ASCII characters
20892 are displayed by converting them to the equivalent multibyte characters
20893 according to the current language environment. As a result, they are
20894 displayed according to the current fontset. */);
20895 unibyte_display_via_language_environment = 0;
20896
20897 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
20898 doc: /* *Maximum height for resizing mini-windows.
20899 If a float, it specifies a fraction of the mini-window frame's height.
20900 If an integer, it specifies a number of lines. */);
20901 Vmax_mini_window_height = make_float (0.25);
20902
20903 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
20904 doc: /* *How to resize mini-windows.
20905 A value of nil means don't automatically resize mini-windows.
20906 A value of t means resize them to fit the text displayed in them.
20907 A value of `grow-only', the default, means let mini-windows grow
20908 only, until their display becomes empty, at which point the windows
20909 go back to their normal size. */);
20910 Vresize_mini_windows = Qgrow_only;
20911
20912 DEFVAR_LISP ("cursor-in-non-selected-windows",
20913 &Vcursor_in_non_selected_windows,
20914 doc: /* *Cursor type to display in non-selected windows.
20915 t means to use hollow box cursor. See `cursor-type' for other values. */);
20916 Vcursor_in_non_selected_windows = Qt;
20917
20918 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
20919 doc: /* Alist specifying how to blink the cursor off.
20920 Each element has the form (ON-STATE . OFF-STATE). Whenever the
20921 `cursor-type' frame-parameter or variable equals ON-STATE,
20922 comparing using `equal', Emacs uses OFF-STATE to specify
20923 how to blink it off. */);
20924 Vblink_cursor_alist = Qnil;
20925
20926 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
20927 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
20928 automatic_hscrolling_p = 1;
20929
20930 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
20931 doc: /* *How many columns away from the window edge point is allowed to get
20932 before automatic hscrolling will horizontally scroll the window. */);
20933 hscroll_margin = 5;
20934
20935 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
20936 doc: /* *How many columns to scroll the window when point gets too close to the edge.
20937 When point is less than `automatic-hscroll-margin' columns from the window
20938 edge, automatic hscrolling will scroll the window by the amount of columns
20939 determined by this variable. If its value is a positive integer, scroll that
20940 many columns. If it's a positive floating-point number, it specifies the
20941 fraction of the window's width to scroll. If it's nil or zero, point will be
20942 centered horizontally after the scroll. Any other value, including negative
20943 numbers, are treated as if the value were zero.
20944
20945 Automatic hscrolling always moves point outside the scroll margin, so if
20946 point was more than scroll step columns inside the margin, the window will
20947 scroll more than the value given by the scroll step.
20948
20949 Note that the lower bound for automatic hscrolling specified by `scroll-left'
20950 and `scroll-right' overrides this variable's effect. */);
20951 Vhscroll_step = make_number (0);
20952
20953 DEFVAR_LISP ("image-types", &Vimage_types,
20954 doc: /* List of supported image types.
20955 Each element of the list is a symbol for a supported image type. */);
20956 Vimage_types = Qnil;
20957
20958 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
20959 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
20960 Bind this around calls to `message' to let it take effect. */);
20961 message_truncate_lines = 0;
20962
20963 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
20964 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
20965 Can be used to update submenus whose contents should vary. */);
20966 Vmenu_bar_update_hook = Qnil;
20967
20968 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
20969 doc: /* Non-nil means don't update menu bars. Internal use only. */);
20970 inhibit_menubar_update = 0;
20971
20972 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
20973 doc: /* Non-nil means don't eval Lisp during redisplay. */);
20974 inhibit_eval_during_redisplay = 0;
20975
20976 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
20977 doc: /* Non-nil means don't free realized faces. Internal use only. */);
20978 inhibit_free_realized_faces = 0;
20979
20980 #if GLYPH_DEBUG
20981 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
20982 doc: /* Inhibit try_window_id display optimization. */);
20983 inhibit_try_window_id = 0;
20984
20985 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
20986 doc: /* Inhibit try_window_reusing display optimization. */);
20987 inhibit_try_window_reusing = 0;
20988
20989 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
20990 doc: /* Inhibit try_cursor_movement display optimization. */);
20991 inhibit_try_cursor_movement = 0;
20992 #endif /* GLYPH_DEBUG */
20993 }
20994
20995
20996 /* Initialize this module when Emacs starts. */
20997
20998 void
20999 init_xdisp ()
21000 {
21001 Lisp_Object root_window;
21002 struct window *mini_w;
21003
21004 current_header_line_height = current_mode_line_height = -1;
21005
21006 CHARPOS (this_line_start_pos) = 0;
21007
21008 mini_w = XWINDOW (minibuf_window);
21009 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
21010
21011 if (!noninteractive)
21012 {
21013 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
21014 int i;
21015
21016 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
21017 set_window_height (root_window,
21018 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
21019 0);
21020 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
21021 set_window_height (minibuf_window, 1, 0);
21022
21023 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
21024 mini_w->total_cols = make_number (FRAME_COLS (f));
21025
21026 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
21027 scratch_glyph_row.glyphs[TEXT_AREA + 1]
21028 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
21029
21030 /* The default ellipsis glyphs `...'. */
21031 for (i = 0; i < 3; ++i)
21032 default_invis_vector[i] = make_number ('.');
21033 }
21034
21035 {
21036 /* Allocate the buffer for frame titles.
21037 Also used for `format-mode-line'. */
21038 int size = 100;
21039 frame_title_buf = (char *) xmalloc (size);
21040 frame_title_buf_end = frame_title_buf + size;
21041 frame_title_ptr = NULL;
21042 }
21043
21044 help_echo_showing_p = 0;
21045 }
21046
21047