]> code.delx.au - gnu-emacs/blob - src/xdisp.c
* xdisp.c (update_tool_bar): BLOCK_INPUT before calling
[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, 2001, 2002, 2003
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 CANON_Y_UNIT, which means that
428 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 struct frame *f = XFRAME (w->frame);
916 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
917
918 if (WINDOW_WANTS_MODELINE_P (w))
919 height -= CURRENT_MODE_LINE_HEIGHT (w);
920 return height;
921 }
922
923
924 /* Return the pixel width of display area AREA of window W. AREA < 0
925 means return the total width of W, not including fringes to
926 the left and right of the window. */
927
928 INLINE int
929 window_box_width (w, area)
930 struct window *w;
931 int area;
932 {
933 struct frame *f = XFRAME (w->frame);
934 int width = XFASTINT (w->width);
935
936 if (!w->pseudo_window_p)
937 {
938 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FRINGE_COLS (f);
939
940 if (area == TEXT_AREA)
941 {
942 if (INTEGERP (w->left_margin_width))
943 width -= XFASTINT (w->left_margin_width);
944 if (INTEGERP (w->right_margin_width))
945 width -= XFASTINT (w->right_margin_width);
946 }
947 else if (area == LEFT_MARGIN_AREA)
948 width = (INTEGERP (w->left_margin_width)
949 ? XFASTINT (w->left_margin_width) : 0);
950 else if (area == RIGHT_MARGIN_AREA)
951 width = (INTEGERP (w->right_margin_width)
952 ? XFASTINT (w->right_margin_width) : 0);
953 }
954
955 return width * CANON_X_UNIT (f);
956 }
957
958
959 /* Return the pixel height of the display area of window W, not
960 including mode lines of W, if any. */
961
962 INLINE int
963 window_box_height (w)
964 struct window *w;
965 {
966 struct frame *f = XFRAME (w->frame);
967 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
968
969 xassert (height >= 0);
970
971 /* Note: the code below that determines the mode-line/header-line
972 height is essentially the same as that contained in the macro
973 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
974 the appropriate glyph row has its `mode_line_p' flag set,
975 and if it doesn't, uses estimate_mode_line_height instead. */
976
977 if (WINDOW_WANTS_MODELINE_P (w))
978 {
979 struct glyph_row *ml_row
980 = (w->current_matrix && w->current_matrix->rows
981 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
982 : 0);
983 if (ml_row && ml_row->mode_line_p)
984 height -= ml_row->height;
985 else
986 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
987 }
988
989 if (WINDOW_WANTS_HEADER_LINE_P (w))
990 {
991 struct glyph_row *hl_row
992 = (w->current_matrix && w->current_matrix->rows
993 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
994 : 0);
995 if (hl_row && hl_row->mode_line_p)
996 height -= hl_row->height;
997 else
998 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
999 }
1000
1001 /* With a very small font and a mode-line that's taller than
1002 default, we might end up with a negative height. */
1003 return max (0, height);
1004 }
1005
1006
1007 /* Return the frame-relative coordinate of the left edge of display
1008 area AREA of window W. AREA < 0 means return the left edge of the
1009 whole window, to the right of the left fringe of W. */
1010
1011 INLINE int
1012 window_box_left (w, area)
1013 struct window *w;
1014 int area;
1015 {
1016 struct frame *f = XFRAME (w->frame);
1017 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
1018
1019 if (!w->pseudo_window_p)
1020 {
1021 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
1022 + FRAME_LEFT_FRINGE_WIDTH (f));
1023
1024 if (area == TEXT_AREA)
1025 x += window_box_width (w, LEFT_MARGIN_AREA);
1026 else if (area == RIGHT_MARGIN_AREA)
1027 x += (window_box_width (w, LEFT_MARGIN_AREA)
1028 + window_box_width (w, TEXT_AREA));
1029 }
1030
1031 return x;
1032 }
1033
1034
1035 /* Return the frame-relative coordinate of the right edge of display
1036 area AREA of window W. AREA < 0 means return the left edge of the
1037 whole window, to the left of the right fringe of W. */
1038
1039 INLINE int
1040 window_box_right (w, area)
1041 struct window *w;
1042 int area;
1043 {
1044 return window_box_left (w, area) + window_box_width (w, area);
1045 }
1046
1047
1048 /* Get the bounding box of the display area AREA of window W, without
1049 mode lines, in frame-relative coordinates. AREA < 0 means the
1050 whole window, not including the left and right fringes of
1051 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1052 coordinates of the upper-left corner of the box. Return in
1053 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1054
1055 INLINE void
1056 window_box (w, area, box_x, box_y, box_width, box_height)
1057 struct window *w;
1058 int area;
1059 int *box_x, *box_y, *box_width, *box_height;
1060 {
1061 struct frame *f = XFRAME (w->frame);
1062
1063 *box_width = window_box_width (w, area);
1064 *box_height = window_box_height (w);
1065 *box_x = window_box_left (w, area);
1066 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
1067 + XFASTINT (w->top) * CANON_Y_UNIT (f));
1068 if (WINDOW_WANTS_HEADER_LINE_P (w))
1069 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1070 }
1071
1072
1073 /* Get the bounding box of the display area AREA of window W, without
1074 mode lines. AREA < 0 means the whole window, not including the
1075 left and right fringe of the window. Return in *TOP_LEFT_X
1076 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1077 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1078 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1079 box. */
1080
1081 INLINE void
1082 window_box_edges (w, area, top_left_x, top_left_y,
1083 bottom_right_x, bottom_right_y)
1084 struct window *w;
1085 int area;
1086 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1087 {
1088 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1089 bottom_right_y);
1090 *bottom_right_x += *top_left_x;
1091 *bottom_right_y += *top_left_y;
1092 }
1093
1094
1095 \f
1096 /***********************************************************************
1097 Utilities
1098 ***********************************************************************/
1099
1100 /* Return the bottom y-position of the line the iterator IT is in.
1101 This can modify IT's settings. */
1102
1103 int
1104 line_bottom_y (it)
1105 struct it *it;
1106 {
1107 int line_height = it->max_ascent + it->max_descent;
1108 int line_top_y = it->current_y;
1109
1110 if (line_height == 0)
1111 {
1112 if (last_height)
1113 line_height = last_height;
1114 else if (IT_CHARPOS (*it) < ZV)
1115 {
1116 move_it_by_lines (it, 1, 1);
1117 line_height = (it->max_ascent || it->max_descent
1118 ? it->max_ascent + it->max_descent
1119 : last_height);
1120 }
1121 else
1122 {
1123 struct glyph_row *row = it->glyph_row;
1124
1125 /* Use the default character height. */
1126 it->glyph_row = NULL;
1127 it->what = IT_CHARACTER;
1128 it->c = ' ';
1129 it->len = 1;
1130 PRODUCE_GLYPHS (it);
1131 line_height = it->ascent + it->descent;
1132 it->glyph_row = row;
1133 }
1134 }
1135
1136 return line_top_y + line_height;
1137 }
1138
1139
1140 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1141 1 if POS is visible and the line containing POS is fully visible.
1142 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1143 and header-lines heights. */
1144
1145 int
1146 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1147 struct window *w;
1148 int charpos, *fully, exact_mode_line_heights_p;
1149 {
1150 struct it it;
1151 struct text_pos top;
1152 int visible_p;
1153 struct buffer *old_buffer = NULL;
1154
1155 if (XBUFFER (w->buffer) != current_buffer)
1156 {
1157 old_buffer = current_buffer;
1158 set_buffer_internal_1 (XBUFFER (w->buffer));
1159 }
1160
1161 *fully = visible_p = 0;
1162 SET_TEXT_POS_FROM_MARKER (top, w->start);
1163
1164 /* Compute exact mode line heights, if requested. */
1165 if (exact_mode_line_heights_p)
1166 {
1167 if (WINDOW_WANTS_MODELINE_P (w))
1168 current_mode_line_height
1169 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1170 current_buffer->mode_line_format);
1171
1172 if (WINDOW_WANTS_HEADER_LINE_P (w))
1173 current_header_line_height
1174 = display_mode_line (w, HEADER_LINE_FACE_ID,
1175 current_buffer->header_line_format);
1176 }
1177
1178 start_display (&it, w, top);
1179 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1180 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1181
1182 /* Note that we may overshoot because of invisible text. */
1183 if (IT_CHARPOS (it) >= charpos)
1184 {
1185 int top_y = it.current_y;
1186 int bottom_y = line_bottom_y (&it);
1187 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1188
1189 if (top_y < window_top_y)
1190 visible_p = bottom_y > window_top_y;
1191 else if (top_y < it.last_visible_y)
1192 {
1193 visible_p = 1;
1194 *fully = bottom_y <= it.last_visible_y;
1195 }
1196 }
1197 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1198 {
1199 move_it_by_lines (&it, 1, 0);
1200 if (charpos < IT_CHARPOS (it))
1201 {
1202 visible_p = 1;
1203 *fully = 0;
1204 }
1205 }
1206
1207 if (old_buffer)
1208 set_buffer_internal_1 (old_buffer);
1209
1210 current_header_line_height = current_mode_line_height = -1;
1211 return visible_p;
1212 }
1213
1214
1215 /* Return the next character from STR which is MAXLEN bytes long.
1216 Return in *LEN the length of the character. This is like
1217 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1218 we find one, we return a `?', but with the length of the invalid
1219 character. */
1220
1221 static INLINE int
1222 string_char_and_length (str, maxlen, len)
1223 const unsigned char *str;
1224 int maxlen, *len;
1225 {
1226 int c;
1227
1228 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1229 if (!CHAR_VALID_P (c, 1))
1230 /* We may not change the length here because other places in Emacs
1231 don't use this function, i.e. they silently accept invalid
1232 characters. */
1233 c = '?';
1234
1235 return c;
1236 }
1237
1238
1239
1240 /* Given a position POS containing a valid character and byte position
1241 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1242
1243 static struct text_pos
1244 string_pos_nchars_ahead (pos, string, nchars)
1245 struct text_pos pos;
1246 Lisp_Object string;
1247 int nchars;
1248 {
1249 xassert (STRINGP (string) && nchars >= 0);
1250
1251 if (STRING_MULTIBYTE (string))
1252 {
1253 int rest = SBYTES (string) - BYTEPOS (pos);
1254 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1255 int len;
1256
1257 while (nchars--)
1258 {
1259 string_char_and_length (p, rest, &len);
1260 p += len, rest -= len;
1261 xassert (rest >= 0);
1262 CHARPOS (pos) += 1;
1263 BYTEPOS (pos) += len;
1264 }
1265 }
1266 else
1267 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1268
1269 return pos;
1270 }
1271
1272
1273 /* Value is the text position, i.e. character and byte position,
1274 for character position CHARPOS in STRING. */
1275
1276 static INLINE struct text_pos
1277 string_pos (charpos, string)
1278 int charpos;
1279 Lisp_Object string;
1280 {
1281 struct text_pos pos;
1282 xassert (STRINGP (string));
1283 xassert (charpos >= 0);
1284 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1285 return pos;
1286 }
1287
1288
1289 /* Value is a text position, i.e. character and byte position, for
1290 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1291 means recognize multibyte characters. */
1292
1293 static struct text_pos
1294 c_string_pos (charpos, s, multibyte_p)
1295 int charpos;
1296 unsigned char *s;
1297 int multibyte_p;
1298 {
1299 struct text_pos pos;
1300
1301 xassert (s != NULL);
1302 xassert (charpos >= 0);
1303
1304 if (multibyte_p)
1305 {
1306 int rest = strlen (s), len;
1307
1308 SET_TEXT_POS (pos, 0, 0);
1309 while (charpos--)
1310 {
1311 string_char_and_length (s, rest, &len);
1312 s += len, rest -= len;
1313 xassert (rest >= 0);
1314 CHARPOS (pos) += 1;
1315 BYTEPOS (pos) += len;
1316 }
1317 }
1318 else
1319 SET_TEXT_POS (pos, charpos, charpos);
1320
1321 return pos;
1322 }
1323
1324
1325 /* Value is the number of characters in C string S. MULTIBYTE_P
1326 non-zero means recognize multibyte characters. */
1327
1328 static int
1329 number_of_chars (s, multibyte_p)
1330 unsigned char *s;
1331 int multibyte_p;
1332 {
1333 int nchars;
1334
1335 if (multibyte_p)
1336 {
1337 int rest = strlen (s), len;
1338 unsigned char *p = (unsigned char *) s;
1339
1340 for (nchars = 0; rest > 0; ++nchars)
1341 {
1342 string_char_and_length (p, rest, &len);
1343 rest -= len, p += len;
1344 }
1345 }
1346 else
1347 nchars = strlen (s);
1348
1349 return nchars;
1350 }
1351
1352
1353 /* Compute byte position NEWPOS->bytepos corresponding to
1354 NEWPOS->charpos. POS is a known position in string STRING.
1355 NEWPOS->charpos must be >= POS.charpos. */
1356
1357 static void
1358 compute_string_pos (newpos, pos, string)
1359 struct text_pos *newpos, pos;
1360 Lisp_Object string;
1361 {
1362 xassert (STRINGP (string));
1363 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1364
1365 if (STRING_MULTIBYTE (string))
1366 *newpos = string_pos_nchars_ahead (pos, string,
1367 CHARPOS (*newpos) - CHARPOS (pos));
1368 else
1369 BYTEPOS (*newpos) = CHARPOS (*newpos);
1370 }
1371
1372 /* EXPORT:
1373 Return an estimation of the pixel height of mode or top lines on
1374 frame F. FACE_ID specifies what line's height to estimate. */
1375
1376 int
1377 estimate_mode_line_height (f, face_id)
1378 struct frame *f;
1379 enum face_id face_id;
1380 {
1381 #ifdef HAVE_WINDOW_SYSTEM
1382 if (FRAME_WINDOW_P (f))
1383 {
1384 int height = FONT_HEIGHT (FRAME_FONT (f));
1385
1386 /* This function is called so early when Emacs starts that the face
1387 cache and mode line face are not yet initialized. */
1388 if (FRAME_FACE_CACHE (f))
1389 {
1390 struct face *face = FACE_FROM_ID (f, face_id);
1391 if (face)
1392 {
1393 if (face->font)
1394 height = FONT_HEIGHT (face->font);
1395 if (face->box_line_width > 0)
1396 height += 2 * face->box_line_width;
1397 }
1398 }
1399
1400 return height;
1401 }
1402 #endif
1403
1404 return 1;
1405 }
1406
1407 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1408 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1409 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1410 not force the value into range. */
1411
1412 void
1413 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1414 FRAME_PTR f;
1415 register int pix_x, pix_y;
1416 int *x, *y;
1417 NativeRectangle *bounds;
1418 int noclip;
1419 {
1420
1421 #ifdef HAVE_WINDOW_SYSTEM
1422 if (FRAME_WINDOW_P (f))
1423 {
1424 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
1425 even for negative values. */
1426 if (pix_x < 0)
1427 pix_x -= FONT_WIDTH (FRAME_FONT (f)) - 1;
1428 if (pix_y < 0)
1429 pix_y -= FRAME_X_OUTPUT(f)->line_height - 1;
1430
1431 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
1432 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
1433
1434 if (bounds)
1435 STORE_NATIVE_RECT (*bounds,
1436 CHAR_TO_PIXEL_COL (f, pix_x),
1437 CHAR_TO_PIXEL_ROW (f, pix_y),
1438 FONT_WIDTH (FRAME_FONT (f)) - 1,
1439 FRAME_X_OUTPUT (f)->line_height - 1);
1440
1441 if (!noclip)
1442 {
1443 if (pix_x < 0)
1444 pix_x = 0;
1445 else if (pix_x > FRAME_WINDOW_WIDTH (f))
1446 pix_x = FRAME_WINDOW_WIDTH (f);
1447
1448 if (pix_y < 0)
1449 pix_y = 0;
1450 else if (pix_y > f->height)
1451 pix_y = f->height;
1452 }
1453 }
1454 #endif
1455
1456 *x = pix_x;
1457 *y = pix_y;
1458 }
1459
1460
1461 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1462 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1463 can't tell the positions because W's display is not up to date,
1464 return 0. */
1465
1466 int
1467 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1468 struct window *w;
1469 int hpos, vpos;
1470 int *frame_x, *frame_y;
1471 {
1472 #ifdef HAVE_WINDOW_SYSTEM
1473 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1474 {
1475 int success_p;
1476
1477 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1478 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1479
1480 if (display_completed)
1481 {
1482 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1483 struct glyph *glyph = row->glyphs[TEXT_AREA];
1484 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1485
1486 hpos = row->x;
1487 vpos = row->y;
1488 while (glyph < end)
1489 {
1490 hpos += glyph->pixel_width;
1491 ++glyph;
1492 }
1493
1494 success_p = 1;
1495 }
1496 else
1497 {
1498 hpos = vpos = 0;
1499 success_p = 0;
1500 }
1501
1502 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1503 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1504 return success_p;
1505 }
1506 #endif
1507
1508 *frame_x = hpos;
1509 *frame_y = vpos;
1510 return 1;
1511 }
1512
1513
1514 #ifdef HAVE_WINDOW_SYSTEM
1515
1516 /* Find the glyph under window-relative coordinates X/Y in window W.
1517 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1518 strings. Return in *HPOS and *VPOS the row and column number of
1519 the glyph found. Return in *AREA the glyph area containing X.
1520 Value is a pointer to the glyph found or null if X/Y is not on
1521 text, or we can't tell because W's current matrix is not up to
1522 date. */
1523
1524 static struct glyph *
1525 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area, buffer_only_p)
1526 struct window *w;
1527 int x, y;
1528 int *hpos, *vpos, *area;
1529 int buffer_only_p;
1530 {
1531 struct glyph *glyph, *end;
1532 struct glyph_row *row = NULL;
1533 int x0, i, left_area_width;
1534
1535 /* Find row containing Y. Give up if some row is not enabled. */
1536 for (i = 0; i < w->current_matrix->nrows; ++i)
1537 {
1538 row = MATRIX_ROW (w->current_matrix, i);
1539 if (!row->enabled_p)
1540 return NULL;
1541 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1542 break;
1543 }
1544
1545 *vpos = i;
1546 *hpos = 0;
1547
1548 /* Give up if Y is not in the window. */
1549 if (i == w->current_matrix->nrows)
1550 return NULL;
1551
1552 /* Get the glyph area containing X. */
1553 if (w->pseudo_window_p)
1554 {
1555 *area = TEXT_AREA;
1556 x0 = 0;
1557 }
1558 else
1559 {
1560 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
1561 if (x < left_area_width)
1562 {
1563 *area = LEFT_MARGIN_AREA;
1564 x0 = 0;
1565 }
1566 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
1567 {
1568 *area = TEXT_AREA;
1569 x0 = row->x + left_area_width;
1570 }
1571 else
1572 {
1573 *area = RIGHT_MARGIN_AREA;
1574 x0 = left_area_width + window_box_width (w, TEXT_AREA);
1575 }
1576 }
1577
1578 /* Find glyph containing X. */
1579 glyph = row->glyphs[*area];
1580 end = glyph + row->used[*area];
1581 while (glyph < end)
1582 {
1583 if (x < x0 + glyph->pixel_width)
1584 {
1585 if (w->pseudo_window_p)
1586 break;
1587 else if (!buffer_only_p || BUFFERP (glyph->object))
1588 break;
1589 }
1590
1591 x0 += glyph->pixel_width;
1592 ++glyph;
1593 }
1594
1595 if (glyph == end)
1596 return NULL;
1597
1598 *hpos = glyph - row->glyphs[*area];
1599 return glyph;
1600 }
1601
1602
1603 /* EXPORT:
1604 Convert frame-relative x/y to coordinates relative to window W.
1605 Takes pseudo-windows into account. */
1606
1607 void
1608 frame_to_window_pixel_xy (w, x, y)
1609 struct window *w;
1610 int *x, *y;
1611 {
1612 if (w->pseudo_window_p)
1613 {
1614 /* A pseudo-window is always full-width, and starts at the
1615 left edge of the frame, plus a frame border. */
1616 struct frame *f = XFRAME (w->frame);
1617 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
1618 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1619 }
1620 else
1621 {
1622 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
1623 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1624 }
1625 }
1626
1627 /* EXPORT:
1628 Return in *R the clipping rectangle for glyph string S. */
1629
1630 void
1631 get_glyph_string_clip_rect (s, nr)
1632 struct glyph_string *s;
1633 NativeRectangle *nr;
1634 {
1635 XRectangle r;
1636
1637 if (s->row->full_width_p)
1638 {
1639 /* Draw full-width. X coordinates are relative to S->w->left. */
1640 int canon_x = CANON_X_UNIT (s->f);
1641
1642 r.x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
1643 r.width = XFASTINT (s->w->width) * canon_x;
1644
1645 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
1646 {
1647 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
1648 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
1649 r.x -= width;
1650 }
1651
1652 r.x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
1653
1654 /* Unless displaying a mode or menu bar line, which are always
1655 fully visible, clip to the visible part of the row. */
1656 if (s->w->pseudo_window_p)
1657 r.height = s->row->visible_height;
1658 else
1659 r.height = s->height;
1660 }
1661 else
1662 {
1663 /* This is a text line that may be partially visible. */
1664 r.x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
1665 r.width = window_box_width (s->w, s->area);
1666 r.height = s->row->visible_height;
1667 }
1668
1669 /* If S draws overlapping rows, it's sufficient to use the top and
1670 bottom of the window for clipping because this glyph string
1671 intentionally draws over other lines. */
1672 if (s->for_overlaps_p)
1673 {
1674 r.y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
1675 r.height = window_text_bottom_y (s->w) - r.y;
1676 }
1677 else
1678 {
1679 /* Don't use S->y for clipping because it doesn't take partially
1680 visible lines into account. For example, it can be negative for
1681 partially visible lines at the top of a window. */
1682 if (!s->row->full_width_p
1683 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1684 r.y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
1685 else
1686 r.y = max (0, s->row->y);
1687
1688 /* If drawing a tool-bar window, draw it over the internal border
1689 at the top of the window. */
1690 if (s->w == XWINDOW (s->f->tool_bar_window))
1691 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1692 }
1693
1694 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1695
1696 #ifdef HAVE_NTGUI
1697 /* ++KFS: From W32 port, but it looks ok for all platforms to me. */
1698 /* If drawing the cursor, don't let glyph draw outside its
1699 advertised boundaries. Cleartype does this under some circumstances. */
1700 if (s->hl == DRAW_CURSOR)
1701 {
1702 if (s->x > r.x)
1703 {
1704 r.width -= s->x - r.x;
1705 r.x = s->x;
1706 }
1707 r.width = min (r.width, s->first_glyph->pixel_width);
1708 }
1709 #endif
1710
1711 #ifdef CONVERT_FROM_XRECT
1712 CONVERT_FROM_XRECT (r, *nr);
1713 #else
1714 *nr = r;
1715 #endif
1716 }
1717
1718 #endif /* HAVE_WINDOW_SYSTEM */
1719
1720 \f
1721 /***********************************************************************
1722 Lisp form evaluation
1723 ***********************************************************************/
1724
1725 /* Error handler for safe_eval and safe_call. */
1726
1727 static Lisp_Object
1728 safe_eval_handler (arg)
1729 Lisp_Object arg;
1730 {
1731 add_to_log ("Error during redisplay: %s", arg, Qnil);
1732 return Qnil;
1733 }
1734
1735
1736 /* Evaluate SEXPR and return the result, or nil if something went
1737 wrong. Prevent redisplay during the evaluation. */
1738
1739 Lisp_Object
1740 safe_eval (sexpr)
1741 Lisp_Object sexpr;
1742 {
1743 Lisp_Object val;
1744
1745 if (inhibit_eval_during_redisplay)
1746 val = Qnil;
1747 else
1748 {
1749 int count = SPECPDL_INDEX ();
1750 struct gcpro gcpro1;
1751
1752 GCPRO1 (sexpr);
1753 specbind (Qinhibit_redisplay, Qt);
1754 /* Use Qt to ensure debugger does not run,
1755 so there is no possibility of wanting to redisplay. */
1756 val = internal_condition_case_1 (Feval, sexpr, Qt,
1757 safe_eval_handler);
1758 UNGCPRO;
1759 val = unbind_to (count, val);
1760 }
1761
1762 return val;
1763 }
1764
1765
1766 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1767 Return the result, or nil if something went wrong. Prevent
1768 redisplay during the evaluation. */
1769
1770 Lisp_Object
1771 safe_call (nargs, args)
1772 int nargs;
1773 Lisp_Object *args;
1774 {
1775 Lisp_Object val;
1776
1777 if (inhibit_eval_during_redisplay)
1778 val = Qnil;
1779 else
1780 {
1781 int count = SPECPDL_INDEX ();
1782 struct gcpro gcpro1;
1783
1784 GCPRO1 (args[0]);
1785 gcpro1.nvars = nargs;
1786 specbind (Qinhibit_redisplay, Qt);
1787 /* Use Qt to ensure debugger does not run,
1788 so there is no possibility of wanting to redisplay. */
1789 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1790 safe_eval_handler);
1791 UNGCPRO;
1792 val = unbind_to (count, val);
1793 }
1794
1795 return val;
1796 }
1797
1798
1799 /* Call function FN with one argument ARG.
1800 Return the result, or nil if something went wrong. */
1801
1802 Lisp_Object
1803 safe_call1 (fn, arg)
1804 Lisp_Object fn, arg;
1805 {
1806 Lisp_Object args[2];
1807 args[0] = fn;
1808 args[1] = arg;
1809 return safe_call (2, args);
1810 }
1811
1812
1813 \f
1814 /***********************************************************************
1815 Debugging
1816 ***********************************************************************/
1817
1818 #if 0
1819
1820 /* Define CHECK_IT to perform sanity checks on iterators.
1821 This is for debugging. It is too slow to do unconditionally. */
1822
1823 static void
1824 check_it (it)
1825 struct it *it;
1826 {
1827 if (it->method == next_element_from_string)
1828 {
1829 xassert (STRINGP (it->string));
1830 xassert (IT_STRING_CHARPOS (*it) >= 0);
1831 }
1832 else if (it->method == next_element_from_buffer)
1833 {
1834 /* Check that character and byte positions agree. */
1835 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1836 }
1837
1838 if (it->dpvec)
1839 xassert (it->current.dpvec_index >= 0);
1840 else
1841 xassert (it->current.dpvec_index < 0);
1842 }
1843
1844 #define CHECK_IT(IT) check_it ((IT))
1845
1846 #else /* not 0 */
1847
1848 #define CHECK_IT(IT) (void) 0
1849
1850 #endif /* not 0 */
1851
1852
1853 #if GLYPH_DEBUG
1854
1855 /* Check that the window end of window W is what we expect it
1856 to be---the last row in the current matrix displaying text. */
1857
1858 static void
1859 check_window_end (w)
1860 struct window *w;
1861 {
1862 if (!MINI_WINDOW_P (w)
1863 && !NILP (w->window_end_valid))
1864 {
1865 struct glyph_row *row;
1866 xassert ((row = MATRIX_ROW (w->current_matrix,
1867 XFASTINT (w->window_end_vpos)),
1868 !row->enabled_p
1869 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1870 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1871 }
1872 }
1873
1874 #define CHECK_WINDOW_END(W) check_window_end ((W))
1875
1876 #else /* not GLYPH_DEBUG */
1877
1878 #define CHECK_WINDOW_END(W) (void) 0
1879
1880 #endif /* not GLYPH_DEBUG */
1881
1882
1883 \f
1884 /***********************************************************************
1885 Iterator initialization
1886 ***********************************************************************/
1887
1888 /* Initialize IT for displaying current_buffer in window W, starting
1889 at character position CHARPOS. CHARPOS < 0 means that no buffer
1890 position is specified which is useful when the iterator is assigned
1891 a position later. BYTEPOS is the byte position corresponding to
1892 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
1893
1894 If ROW is not null, calls to produce_glyphs with IT as parameter
1895 will produce glyphs in that row.
1896
1897 BASE_FACE_ID is the id of a base face to use. It must be one of
1898 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1899 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1900 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
1901
1902 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1903 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1904 will be initialized to use the corresponding mode line glyph row of
1905 the desired matrix of W. */
1906
1907 void
1908 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1909 struct it *it;
1910 struct window *w;
1911 int charpos, bytepos;
1912 struct glyph_row *row;
1913 enum face_id base_face_id;
1914 {
1915 int highlight_region_p;
1916
1917 /* Some precondition checks. */
1918 xassert (w != NULL && it != NULL);
1919 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1920 && charpos <= ZV));
1921
1922 /* If face attributes have been changed since the last redisplay,
1923 free realized faces now because they depend on face definitions
1924 that might have changed. Don't free faces while there might be
1925 desired matrices pending which reference these faces. */
1926 if (face_change_count && !inhibit_free_realized_faces)
1927 {
1928 face_change_count = 0;
1929 free_all_realized_faces (Qnil);
1930 }
1931
1932 /* Use one of the mode line rows of W's desired matrix if
1933 appropriate. */
1934 if (row == NULL)
1935 {
1936 if (base_face_id == MODE_LINE_FACE_ID
1937 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
1938 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1939 else if (base_face_id == HEADER_LINE_FACE_ID)
1940 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1941 }
1942
1943 /* Clear IT. */
1944 bzero (it, sizeof *it);
1945 it->current.overlay_string_index = -1;
1946 it->current.dpvec_index = -1;
1947 it->base_face_id = base_face_id;
1948
1949 /* The window in which we iterate over current_buffer: */
1950 XSETWINDOW (it->window, w);
1951 it->w = w;
1952 it->f = XFRAME (w->frame);
1953
1954 /* Extra space between lines (on window systems only). */
1955 if (base_face_id == DEFAULT_FACE_ID
1956 && FRAME_WINDOW_P (it->f))
1957 {
1958 if (NATNUMP (current_buffer->extra_line_spacing))
1959 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1960 else if (it->f->extra_line_spacing > 0)
1961 it->extra_line_spacing = it->f->extra_line_spacing;
1962 }
1963
1964 /* If realized faces have been removed, e.g. because of face
1965 attribute changes of named faces, recompute them. When running
1966 in batch mode, the face cache of Vterminal_frame is null. If
1967 we happen to get called, make a dummy face cache. */
1968 if (
1969 #ifndef WINDOWSNT
1970 noninteractive &&
1971 #endif
1972 FRAME_FACE_CACHE (it->f) == NULL)
1973 init_frame_faces (it->f);
1974 if (FRAME_FACE_CACHE (it->f)->used == 0)
1975 recompute_basic_faces (it->f);
1976
1977 /* Current value of the `space-width', and 'height' properties. */
1978 it->space_width = Qnil;
1979 it->font_height = Qnil;
1980
1981 /* Are control characters displayed as `^C'? */
1982 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1983
1984 /* -1 means everything between a CR and the following line end
1985 is invisible. >0 means lines indented more than this value are
1986 invisible. */
1987 it->selective = (INTEGERP (current_buffer->selective_display)
1988 ? XFASTINT (current_buffer->selective_display)
1989 : (!NILP (current_buffer->selective_display)
1990 ? -1 : 0));
1991 it->selective_display_ellipsis_p
1992 = !NILP (current_buffer->selective_display_ellipses);
1993
1994 /* Display table to use. */
1995 it->dp = window_display_table (w);
1996
1997 /* Are multibyte characters enabled in current_buffer? */
1998 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1999
2000 /* Non-zero if we should highlight the region. */
2001 highlight_region_p
2002 = (!NILP (Vtransient_mark_mode)
2003 && !NILP (current_buffer->mark_active)
2004 && XMARKER (current_buffer->mark)->buffer != 0);
2005
2006 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2007 start and end of a visible region in window IT->w. Set both to
2008 -1 to indicate no region. */
2009 if (highlight_region_p
2010 /* Maybe highlight only in selected window. */
2011 && (/* Either show region everywhere. */
2012 highlight_nonselected_windows
2013 /* Or show region in the selected window. */
2014 || w == XWINDOW (selected_window)
2015 /* Or show the region if we are in the mini-buffer and W is
2016 the window the mini-buffer refers to. */
2017 || (MINI_WINDOW_P (XWINDOW (selected_window))
2018 && WINDOWP (minibuf_selected_window)
2019 && w == XWINDOW (minibuf_selected_window))))
2020 {
2021 int charpos = marker_position (current_buffer->mark);
2022 it->region_beg_charpos = min (PT, charpos);
2023 it->region_end_charpos = max (PT, charpos);
2024 }
2025 else
2026 it->region_beg_charpos = it->region_end_charpos = -1;
2027
2028 /* Get the position at which the redisplay_end_trigger hook should
2029 be run, if it is to be run at all. */
2030 if (MARKERP (w->redisplay_end_trigger)
2031 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2032 it->redisplay_end_trigger_charpos
2033 = marker_position (w->redisplay_end_trigger);
2034 else if (INTEGERP (w->redisplay_end_trigger))
2035 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2036
2037 /* Correct bogus values of tab_width. */
2038 it->tab_width = XINT (current_buffer->tab_width);
2039 if (it->tab_width <= 0 || it->tab_width > 1000)
2040 it->tab_width = 8;
2041
2042 /* Are lines in the display truncated? */
2043 it->truncate_lines_p
2044 = (base_face_id != DEFAULT_FACE_ID
2045 || XINT (it->w->hscroll)
2046 || (truncate_partial_width_windows
2047 && !WINDOW_FULL_WIDTH_P (it->w))
2048 || !NILP (current_buffer->truncate_lines));
2049
2050 /* Get dimensions of truncation and continuation glyphs. These are
2051 displayed as fringe bitmaps under X, so we don't need them for such
2052 frames. */
2053 if (!FRAME_WINDOW_P (it->f))
2054 {
2055 if (it->truncate_lines_p)
2056 {
2057 /* We will need the truncation glyph. */
2058 xassert (it->glyph_row == NULL);
2059 produce_special_glyphs (it, IT_TRUNCATION);
2060 it->truncation_pixel_width = it->pixel_width;
2061 }
2062 else
2063 {
2064 /* We will need the continuation glyph. */
2065 xassert (it->glyph_row == NULL);
2066 produce_special_glyphs (it, IT_CONTINUATION);
2067 it->continuation_pixel_width = it->pixel_width;
2068 }
2069
2070 /* Reset these values to zero because the produce_special_glyphs
2071 above has changed them. */
2072 it->pixel_width = it->ascent = it->descent = 0;
2073 it->phys_ascent = it->phys_descent = 0;
2074 }
2075
2076 /* Set this after getting the dimensions of truncation and
2077 continuation glyphs, so that we don't produce glyphs when calling
2078 produce_special_glyphs, above. */
2079 it->glyph_row = row;
2080 it->area = TEXT_AREA;
2081
2082 /* Get the dimensions of the display area. The display area
2083 consists of the visible window area plus a horizontally scrolled
2084 part to the left of the window. All x-values are relative to the
2085 start of this total display area. */
2086 if (base_face_id != DEFAULT_FACE_ID)
2087 {
2088 /* Mode lines, menu bar in terminal frames. */
2089 it->first_visible_x = 0;
2090 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
2091 }
2092 else
2093 {
2094 it->first_visible_x
2095 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
2096 it->last_visible_x = (it->first_visible_x
2097 + window_box_width (w, TEXT_AREA));
2098
2099 /* If we truncate lines, leave room for the truncator glyph(s) at
2100 the right margin. Otherwise, leave room for the continuation
2101 glyph(s). Truncation and continuation glyphs are not inserted
2102 for window-based redisplay. */
2103 if (!FRAME_WINDOW_P (it->f))
2104 {
2105 if (it->truncate_lines_p)
2106 it->last_visible_x -= it->truncation_pixel_width;
2107 else
2108 it->last_visible_x -= it->continuation_pixel_width;
2109 }
2110
2111 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2112 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
2113 }
2114
2115 /* Leave room for a border glyph. */
2116 if (!FRAME_WINDOW_P (it->f)
2117 && !WINDOW_RIGHTMOST_P (it->w))
2118 it->last_visible_x -= 1;
2119
2120 it->last_visible_y = window_text_bottom_y (w);
2121
2122 /* For mode lines and alike, arrange for the first glyph having a
2123 left box line if the face specifies a box. */
2124 if (base_face_id != DEFAULT_FACE_ID)
2125 {
2126 struct face *face;
2127
2128 it->face_id = base_face_id;
2129
2130 /* If we have a boxed mode line, make the first character appear
2131 with a left box line. */
2132 face = FACE_FROM_ID (it->f, base_face_id);
2133 if (face->box != FACE_NO_BOX)
2134 it->start_of_box_run_p = 1;
2135 }
2136
2137 /* If a buffer position was specified, set the iterator there,
2138 getting overlays and face properties from that position. */
2139 if (charpos >= BUF_BEG (current_buffer))
2140 {
2141 it->end_charpos = ZV;
2142 it->face_id = -1;
2143 IT_CHARPOS (*it) = charpos;
2144
2145 /* Compute byte position if not specified. */
2146 if (bytepos < charpos)
2147 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2148 else
2149 IT_BYTEPOS (*it) = bytepos;
2150
2151 /* Compute faces etc. */
2152 reseat (it, it->current.pos, 1);
2153 }
2154
2155 CHECK_IT (it);
2156 }
2157
2158
2159 /* Initialize IT for the display of window W with window start POS. */
2160
2161 void
2162 start_display (it, w, pos)
2163 struct it *it;
2164 struct window *w;
2165 struct text_pos pos;
2166 {
2167 struct glyph_row *row;
2168 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2169
2170 row = w->desired_matrix->rows + first_vpos;
2171 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2172
2173 if (!it->truncate_lines_p)
2174 {
2175 int start_at_line_beg_p;
2176 int first_y = it->current_y;
2177
2178 /* If window start is not at a line start, skip forward to POS to
2179 get the correct continuation lines width. */
2180 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2181 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2182 if (!start_at_line_beg_p)
2183 {
2184 int new_x;
2185
2186 reseat_at_previous_visible_line_start (it);
2187 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2188
2189 new_x = it->current_x + it->pixel_width;
2190
2191 /* If lines are continued, this line may end in the middle
2192 of a multi-glyph character (e.g. a control character
2193 displayed as \003, or in the middle of an overlay
2194 string). In this case move_it_to above will not have
2195 taken us to the start of the continuation line but to the
2196 end of the continued line. */
2197 if (it->current_x > 0
2198 && !it->truncate_lines_p /* Lines are continued. */
2199 && (/* And glyph doesn't fit on the line. */
2200 new_x > it->last_visible_x
2201 /* Or it fits exactly and we're on a window
2202 system frame. */
2203 || (new_x == it->last_visible_x
2204 && FRAME_WINDOW_P (it->f))))
2205 {
2206 if (it->current.dpvec_index >= 0
2207 || it->current.overlay_string_index >= 0)
2208 {
2209 set_iterator_to_next (it, 1);
2210 move_it_in_display_line_to (it, -1, -1, 0);
2211 }
2212
2213 it->continuation_lines_width += it->current_x;
2214 }
2215
2216 /* We're starting a new display line, not affected by the
2217 height of the continued line, so clear the appropriate
2218 fields in the iterator structure. */
2219 it->max_ascent = it->max_descent = 0;
2220 it->max_phys_ascent = it->max_phys_descent = 0;
2221
2222 it->current_y = first_y;
2223 it->vpos = 0;
2224 it->current_x = it->hpos = 0;
2225 }
2226 }
2227
2228 #if 0 /* Don't assert the following because start_display is sometimes
2229 called intentionally with a window start that is not at a
2230 line start. Please leave this code in as a comment. */
2231
2232 /* Window start should be on a line start, now. */
2233 xassert (it->continuation_lines_width
2234 || IT_CHARPOS (it) == BEGV
2235 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2236 #endif /* 0 */
2237 }
2238
2239
2240 /* Return 1 if POS is a position in ellipses displayed for invisible
2241 text. W is the window we display, for text property lookup. */
2242
2243 static int
2244 in_ellipses_for_invisible_text_p (pos, w)
2245 struct display_pos *pos;
2246 struct window *w;
2247 {
2248 Lisp_Object prop, window;
2249 int ellipses_p = 0;
2250 int charpos = CHARPOS (pos->pos);
2251
2252 /* If POS specifies a position in a display vector, this might
2253 be for an ellipsis displayed for invisible text. We won't
2254 get the iterator set up for delivering that ellipsis unless
2255 we make sure that it gets aware of the invisible text. */
2256 if (pos->dpvec_index >= 0
2257 && pos->overlay_string_index < 0
2258 && CHARPOS (pos->string_pos) < 0
2259 && charpos > BEGV
2260 && (XSETWINDOW (window, w),
2261 prop = Fget_char_property (make_number (charpos),
2262 Qinvisible, window),
2263 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2264 {
2265 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2266 window);
2267 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2268 }
2269
2270 return ellipses_p;
2271 }
2272
2273
2274 /* Initialize IT for stepping through current_buffer in window W,
2275 starting at position POS that includes overlay string and display
2276 vector/ control character translation position information. Value
2277 is zero if there are overlay strings with newlines at POS. */
2278
2279 static int
2280 init_from_display_pos (it, w, pos)
2281 struct it *it;
2282 struct window *w;
2283 struct display_pos *pos;
2284 {
2285 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2286 int i, overlay_strings_with_newlines = 0;
2287
2288 /* If POS specifies a position in a display vector, this might
2289 be for an ellipsis displayed for invisible text. We won't
2290 get the iterator set up for delivering that ellipsis unless
2291 we make sure that it gets aware of the invisible text. */
2292 if (in_ellipses_for_invisible_text_p (pos, w))
2293 {
2294 --charpos;
2295 bytepos = 0;
2296 }
2297
2298 /* Keep in mind: the call to reseat in init_iterator skips invisible
2299 text, so we might end up at a position different from POS. This
2300 is only a problem when POS is a row start after a newline and an
2301 overlay starts there with an after-string, and the overlay has an
2302 invisible property. Since we don't skip invisible text in
2303 display_line and elsewhere immediately after consuming the
2304 newline before the row start, such a POS will not be in a string,
2305 but the call to init_iterator below will move us to the
2306 after-string. */
2307 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2308
2309 for (i = 0; i < it->n_overlay_strings; ++i)
2310 {
2311 const char *s = SDATA (it->overlay_strings[i]);
2312 const char *e = s + SBYTES (it->overlay_strings[i]);
2313
2314 while (s < e && *s != '\n')
2315 ++s;
2316
2317 if (s < e)
2318 {
2319 overlay_strings_with_newlines = 1;
2320 break;
2321 }
2322 }
2323
2324 /* If position is within an overlay string, set up IT to the right
2325 overlay string. */
2326 if (pos->overlay_string_index >= 0)
2327 {
2328 int relative_index;
2329
2330 /* If the first overlay string happens to have a `display'
2331 property for an image, the iterator will be set up for that
2332 image, and we have to undo that setup first before we can
2333 correct the overlay string index. */
2334 if (it->method == next_element_from_image)
2335 pop_it (it);
2336
2337 /* We already have the first chunk of overlay strings in
2338 IT->overlay_strings. Load more until the one for
2339 pos->overlay_string_index is in IT->overlay_strings. */
2340 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2341 {
2342 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2343 it->current.overlay_string_index = 0;
2344 while (n--)
2345 {
2346 load_overlay_strings (it, 0);
2347 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2348 }
2349 }
2350
2351 it->current.overlay_string_index = pos->overlay_string_index;
2352 relative_index = (it->current.overlay_string_index
2353 % OVERLAY_STRING_CHUNK_SIZE);
2354 it->string = it->overlay_strings[relative_index];
2355 xassert (STRINGP (it->string));
2356 it->current.string_pos = pos->string_pos;
2357 it->method = next_element_from_string;
2358 }
2359
2360 #if 0 /* This is bogus because POS not having an overlay string
2361 position does not mean it's after the string. Example: A
2362 line starting with a before-string and initialization of IT
2363 to the previous row's end position. */
2364 else if (it->current.overlay_string_index >= 0)
2365 {
2366 /* If POS says we're already after an overlay string ending at
2367 POS, make sure to pop the iterator because it will be in
2368 front of that overlay string. When POS is ZV, we've thereby
2369 also ``processed'' overlay strings at ZV. */
2370 while (it->sp)
2371 pop_it (it);
2372 it->current.overlay_string_index = -1;
2373 it->method = next_element_from_buffer;
2374 if (CHARPOS (pos->pos) == ZV)
2375 it->overlay_strings_at_end_processed_p = 1;
2376 }
2377 #endif /* 0 */
2378
2379 if (CHARPOS (pos->string_pos) >= 0)
2380 {
2381 /* Recorded position is not in an overlay string, but in another
2382 string. This can only be a string from a `display' property.
2383 IT should already be filled with that string. */
2384 it->current.string_pos = pos->string_pos;
2385 xassert (STRINGP (it->string));
2386 }
2387
2388 /* Restore position in display vector translations, control
2389 character translations or ellipses. */
2390 if (pos->dpvec_index >= 0)
2391 {
2392 if (it->dpvec == NULL)
2393 get_next_display_element (it);
2394 xassert (it->dpvec && it->current.dpvec_index == 0);
2395 it->current.dpvec_index = pos->dpvec_index;
2396 }
2397
2398 CHECK_IT (it);
2399 return !overlay_strings_with_newlines;
2400 }
2401
2402
2403 /* Initialize IT for stepping through current_buffer in window W
2404 starting at ROW->start. */
2405
2406 static void
2407 init_to_row_start (it, w, row)
2408 struct it *it;
2409 struct window *w;
2410 struct glyph_row *row;
2411 {
2412 init_from_display_pos (it, w, &row->start);
2413 it->continuation_lines_width = row->continuation_lines_width;
2414 CHECK_IT (it);
2415 }
2416
2417
2418 /* Initialize IT for stepping through current_buffer in window W
2419 starting in the line following ROW, i.e. starting at ROW->end.
2420 Value is zero if there are overlay strings with newlines at ROW's
2421 end position. */
2422
2423 static int
2424 init_to_row_end (it, w, row)
2425 struct it *it;
2426 struct window *w;
2427 struct glyph_row *row;
2428 {
2429 int success = 0;
2430
2431 if (init_from_display_pos (it, w, &row->end))
2432 {
2433 if (row->continued_p)
2434 it->continuation_lines_width
2435 = row->continuation_lines_width + row->pixel_width;
2436 CHECK_IT (it);
2437 success = 1;
2438 }
2439
2440 return success;
2441 }
2442
2443
2444
2445 \f
2446 /***********************************************************************
2447 Text properties
2448 ***********************************************************************/
2449
2450 /* Called when IT reaches IT->stop_charpos. Handle text property and
2451 overlay changes. Set IT->stop_charpos to the next position where
2452 to stop. */
2453
2454 static void
2455 handle_stop (it)
2456 struct it *it;
2457 {
2458 enum prop_handled handled;
2459 int handle_overlay_change_p = 1;
2460 struct props *p;
2461
2462 it->dpvec = NULL;
2463 it->current.dpvec_index = -1;
2464
2465 do
2466 {
2467 handled = HANDLED_NORMALLY;
2468
2469 /* Call text property handlers. */
2470 for (p = it_props; p->handler; ++p)
2471 {
2472 handled = p->handler (it);
2473
2474 if (handled == HANDLED_RECOMPUTE_PROPS)
2475 break;
2476 else if (handled == HANDLED_RETURN)
2477 return;
2478 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2479 handle_overlay_change_p = 0;
2480 }
2481
2482 if (handled != HANDLED_RECOMPUTE_PROPS)
2483 {
2484 /* Don't check for overlay strings below when set to deliver
2485 characters from a display vector. */
2486 if (it->method == next_element_from_display_vector)
2487 handle_overlay_change_p = 0;
2488
2489 /* Handle overlay changes. */
2490 if (handle_overlay_change_p)
2491 handled = handle_overlay_change (it);
2492
2493 /* Determine where to stop next. */
2494 if (handled == HANDLED_NORMALLY)
2495 compute_stop_pos (it);
2496 }
2497 }
2498 while (handled == HANDLED_RECOMPUTE_PROPS);
2499 }
2500
2501
2502 /* Compute IT->stop_charpos from text property and overlay change
2503 information for IT's current position. */
2504
2505 static void
2506 compute_stop_pos (it)
2507 struct it *it;
2508 {
2509 register INTERVAL iv, next_iv;
2510 Lisp_Object object, limit, position;
2511
2512 /* If nowhere else, stop at the end. */
2513 it->stop_charpos = it->end_charpos;
2514
2515 if (STRINGP (it->string))
2516 {
2517 /* Strings are usually short, so don't limit the search for
2518 properties. */
2519 object = it->string;
2520 limit = Qnil;
2521 position = make_number (IT_STRING_CHARPOS (*it));
2522 }
2523 else
2524 {
2525 int charpos;
2526
2527 /* If next overlay change is in front of the current stop pos
2528 (which is IT->end_charpos), stop there. Note: value of
2529 next_overlay_change is point-max if no overlay change
2530 follows. */
2531 charpos = next_overlay_change (IT_CHARPOS (*it));
2532 if (charpos < it->stop_charpos)
2533 it->stop_charpos = charpos;
2534
2535 /* If showing the region, we have to stop at the region
2536 start or end because the face might change there. */
2537 if (it->region_beg_charpos > 0)
2538 {
2539 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2540 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2541 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2542 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2543 }
2544
2545 /* Set up variables for computing the stop position from text
2546 property changes. */
2547 XSETBUFFER (object, current_buffer);
2548 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2549 position = make_number (IT_CHARPOS (*it));
2550
2551 }
2552
2553 /* Get the interval containing IT's position. Value is a null
2554 interval if there isn't such an interval. */
2555 iv = validate_interval_range (object, &position, &position, 0);
2556 if (!NULL_INTERVAL_P (iv))
2557 {
2558 Lisp_Object values_here[LAST_PROP_IDX];
2559 struct props *p;
2560
2561 /* Get properties here. */
2562 for (p = it_props; p->handler; ++p)
2563 values_here[p->idx] = textget (iv->plist, *p->name);
2564
2565 /* Look for an interval following iv that has different
2566 properties. */
2567 for (next_iv = next_interval (iv);
2568 (!NULL_INTERVAL_P (next_iv)
2569 && (NILP (limit)
2570 || XFASTINT (limit) > next_iv->position));
2571 next_iv = next_interval (next_iv))
2572 {
2573 for (p = it_props; p->handler; ++p)
2574 {
2575 Lisp_Object new_value;
2576
2577 new_value = textget (next_iv->plist, *p->name);
2578 if (!EQ (values_here[p->idx], new_value))
2579 break;
2580 }
2581
2582 if (p->handler)
2583 break;
2584 }
2585
2586 if (!NULL_INTERVAL_P (next_iv))
2587 {
2588 if (INTEGERP (limit)
2589 && next_iv->position >= XFASTINT (limit))
2590 /* No text property change up to limit. */
2591 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2592 else
2593 /* Text properties change in next_iv. */
2594 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2595 }
2596 }
2597
2598 xassert (STRINGP (it->string)
2599 || (it->stop_charpos >= BEGV
2600 && it->stop_charpos >= IT_CHARPOS (*it)));
2601 }
2602
2603
2604 /* Return the position of the next overlay change after POS in
2605 current_buffer. Value is point-max if no overlay change
2606 follows. This is like `next-overlay-change' but doesn't use
2607 xmalloc. */
2608
2609 static int
2610 next_overlay_change (pos)
2611 int pos;
2612 {
2613 int noverlays;
2614 int endpos;
2615 Lisp_Object *overlays;
2616 int len;
2617 int i;
2618
2619 /* Get all overlays at the given position. */
2620 len = 10;
2621 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2622 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2623 if (noverlays > len)
2624 {
2625 len = noverlays;
2626 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2627 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2628 }
2629
2630 /* If any of these overlays ends before endpos,
2631 use its ending point instead. */
2632 for (i = 0; i < noverlays; ++i)
2633 {
2634 Lisp_Object oend;
2635 int oendpos;
2636
2637 oend = OVERLAY_END (overlays[i]);
2638 oendpos = OVERLAY_POSITION (oend);
2639 endpos = min (endpos, oendpos);
2640 }
2641
2642 return endpos;
2643 }
2644
2645
2646 \f
2647 /***********************************************************************
2648 Fontification
2649 ***********************************************************************/
2650
2651 /* Handle changes in the `fontified' property of the current buffer by
2652 calling hook functions from Qfontification_functions to fontify
2653 regions of text. */
2654
2655 static enum prop_handled
2656 handle_fontified_prop (it)
2657 struct it *it;
2658 {
2659 Lisp_Object prop, pos;
2660 enum prop_handled handled = HANDLED_NORMALLY;
2661
2662 /* Get the value of the `fontified' property at IT's current buffer
2663 position. (The `fontified' property doesn't have a special
2664 meaning in strings.) If the value is nil, call functions from
2665 Qfontification_functions. */
2666 if (!STRINGP (it->string)
2667 && it->s == NULL
2668 && !NILP (Vfontification_functions)
2669 && !NILP (Vrun_hooks)
2670 && (pos = make_number (IT_CHARPOS (*it)),
2671 prop = Fget_char_property (pos, Qfontified, Qnil),
2672 NILP (prop)))
2673 {
2674 int count = SPECPDL_INDEX ();
2675 Lisp_Object val;
2676
2677 val = Vfontification_functions;
2678 specbind (Qfontification_functions, Qnil);
2679
2680 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2681 safe_call1 (val, pos);
2682 else
2683 {
2684 Lisp_Object globals, fn;
2685 struct gcpro gcpro1, gcpro2;
2686
2687 globals = Qnil;
2688 GCPRO2 (val, globals);
2689
2690 for (; CONSP (val); val = XCDR (val))
2691 {
2692 fn = XCAR (val);
2693
2694 if (EQ (fn, Qt))
2695 {
2696 /* A value of t indicates this hook has a local
2697 binding; it means to run the global binding too.
2698 In a global value, t should not occur. If it
2699 does, we must ignore it to avoid an endless
2700 loop. */
2701 for (globals = Fdefault_value (Qfontification_functions);
2702 CONSP (globals);
2703 globals = XCDR (globals))
2704 {
2705 fn = XCAR (globals);
2706 if (!EQ (fn, Qt))
2707 safe_call1 (fn, pos);
2708 }
2709 }
2710 else
2711 safe_call1 (fn, pos);
2712 }
2713
2714 UNGCPRO;
2715 }
2716
2717 unbind_to (count, Qnil);
2718
2719 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2720 something. This avoids an endless loop if they failed to
2721 fontify the text for which reason ever. */
2722 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2723 handled = HANDLED_RECOMPUTE_PROPS;
2724 }
2725
2726 return handled;
2727 }
2728
2729
2730 \f
2731 /***********************************************************************
2732 Faces
2733 ***********************************************************************/
2734
2735 /* Set up iterator IT from face properties at its current position.
2736 Called from handle_stop. */
2737
2738 static enum prop_handled
2739 handle_face_prop (it)
2740 struct it *it;
2741 {
2742 int new_face_id, next_stop;
2743
2744 if (!STRINGP (it->string))
2745 {
2746 new_face_id
2747 = face_at_buffer_position (it->w,
2748 IT_CHARPOS (*it),
2749 it->region_beg_charpos,
2750 it->region_end_charpos,
2751 &next_stop,
2752 (IT_CHARPOS (*it)
2753 + TEXT_PROP_DISTANCE_LIMIT),
2754 0);
2755
2756 /* Is this a start of a run of characters with box face?
2757 Caveat: this can be called for a freshly initialized
2758 iterator; face_id is -1 in this case. We know that the new
2759 face will not change until limit, i.e. if the new face has a
2760 box, all characters up to limit will have one. But, as
2761 usual, we don't know whether limit is really the end. */
2762 if (new_face_id != it->face_id)
2763 {
2764 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2765
2766 /* If new face has a box but old face has not, this is
2767 the start of a run of characters with box, i.e. it has
2768 a shadow on the left side. The value of face_id of the
2769 iterator will be -1 if this is the initial call that gets
2770 the face. In this case, we have to look in front of IT's
2771 position and see whether there is a face != new_face_id. */
2772 it->start_of_box_run_p
2773 = (new_face->box != FACE_NO_BOX
2774 && (it->face_id >= 0
2775 || IT_CHARPOS (*it) == BEG
2776 || new_face_id != face_before_it_pos (it)));
2777 it->face_box_p = new_face->box != FACE_NO_BOX;
2778 }
2779 }
2780 else
2781 {
2782 int base_face_id, bufpos;
2783
2784 if (it->current.overlay_string_index >= 0)
2785 bufpos = IT_CHARPOS (*it);
2786 else
2787 bufpos = 0;
2788
2789 /* For strings from a buffer, i.e. overlay strings or strings
2790 from a `display' property, use the face at IT's current
2791 buffer position as the base face to merge with, so that
2792 overlay strings appear in the same face as surrounding
2793 text, unless they specify their own faces. */
2794 base_face_id = underlying_face_id (it);
2795
2796 new_face_id = face_at_string_position (it->w,
2797 it->string,
2798 IT_STRING_CHARPOS (*it),
2799 bufpos,
2800 it->region_beg_charpos,
2801 it->region_end_charpos,
2802 &next_stop,
2803 base_face_id, 0);
2804
2805 #if 0 /* This shouldn't be neccessary. Let's check it. */
2806 /* If IT is used to display a mode line we would really like to
2807 use the mode line face instead of the frame's default face. */
2808 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2809 && new_face_id == DEFAULT_FACE_ID)
2810 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2811 #endif
2812
2813 /* Is this a start of a run of characters with box? Caveat:
2814 this can be called for a freshly allocated iterator; face_id
2815 is -1 is this case. We know that the new face will not
2816 change until the next check pos, i.e. if the new face has a
2817 box, all characters up to that position will have a
2818 box. But, as usual, we don't know whether that position
2819 is really the end. */
2820 if (new_face_id != it->face_id)
2821 {
2822 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2823 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2824
2825 /* If new face has a box but old face hasn't, this is the
2826 start of a run of characters with box, i.e. it has a
2827 shadow on the left side. */
2828 it->start_of_box_run_p
2829 = new_face->box && (old_face == NULL || !old_face->box);
2830 it->face_box_p = new_face->box != FACE_NO_BOX;
2831 }
2832 }
2833
2834 it->face_id = new_face_id;
2835 return HANDLED_NORMALLY;
2836 }
2837
2838
2839 /* Return the ID of the face ``underlying'' IT's current position,
2840 which is in a string. If the iterator is associated with a
2841 buffer, return the face at IT's current buffer position.
2842 Otherwise, use the iterator's base_face_id. */
2843
2844 static int
2845 underlying_face_id (it)
2846 struct it *it;
2847 {
2848 int face_id = it->base_face_id, i;
2849
2850 xassert (STRINGP (it->string));
2851
2852 for (i = it->sp - 1; i >= 0; --i)
2853 if (NILP (it->stack[i].string))
2854 face_id = it->stack[i].face_id;
2855
2856 return face_id;
2857 }
2858
2859
2860 /* Compute the face one character before or after the current position
2861 of IT. BEFORE_P non-zero means get the face in front of IT's
2862 position. Value is the id of the face. */
2863
2864 static int
2865 face_before_or_after_it_pos (it, before_p)
2866 struct it *it;
2867 int before_p;
2868 {
2869 int face_id, limit;
2870 int next_check_charpos;
2871 struct text_pos pos;
2872
2873 xassert (it->s == NULL);
2874
2875 if (STRINGP (it->string))
2876 {
2877 int bufpos, base_face_id;
2878
2879 /* No face change past the end of the string (for the case
2880 we are padding with spaces). No face change before the
2881 string start. */
2882 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
2883 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2884 return it->face_id;
2885
2886 /* Set pos to the position before or after IT's current position. */
2887 if (before_p)
2888 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2889 else
2890 /* For composition, we must check the character after the
2891 composition. */
2892 pos = (it->what == IT_COMPOSITION
2893 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2894 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2895
2896 if (it->current.overlay_string_index >= 0)
2897 bufpos = IT_CHARPOS (*it);
2898 else
2899 bufpos = 0;
2900
2901 base_face_id = underlying_face_id (it);
2902
2903 /* Get the face for ASCII, or unibyte. */
2904 face_id = face_at_string_position (it->w,
2905 it->string,
2906 CHARPOS (pos),
2907 bufpos,
2908 it->region_beg_charpos,
2909 it->region_end_charpos,
2910 &next_check_charpos,
2911 base_face_id, 0);
2912
2913 /* Correct the face for charsets different from ASCII. Do it
2914 for the multibyte case only. The face returned above is
2915 suitable for unibyte text if IT->string is unibyte. */
2916 if (STRING_MULTIBYTE (it->string))
2917 {
2918 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
2919 int rest = SBYTES (it->string) - BYTEPOS (pos);
2920 int c, len;
2921 struct face *face = FACE_FROM_ID (it->f, face_id);
2922
2923 c = string_char_and_length (p, rest, &len);
2924 face_id = FACE_FOR_CHAR (it->f, face, c);
2925 }
2926 }
2927 else
2928 {
2929 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2930 || (IT_CHARPOS (*it) <= BEGV && before_p))
2931 return it->face_id;
2932
2933 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2934 pos = it->current.pos;
2935
2936 if (before_p)
2937 DEC_TEXT_POS (pos, it->multibyte_p);
2938 else
2939 {
2940 if (it->what == IT_COMPOSITION)
2941 /* For composition, we must check the position after the
2942 composition. */
2943 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2944 else
2945 INC_TEXT_POS (pos, it->multibyte_p);
2946 }
2947
2948 /* Determine face for CHARSET_ASCII, or unibyte. */
2949 face_id = face_at_buffer_position (it->w,
2950 CHARPOS (pos),
2951 it->region_beg_charpos,
2952 it->region_end_charpos,
2953 &next_check_charpos,
2954 limit, 0);
2955
2956 /* Correct the face for charsets different from ASCII. Do it
2957 for the multibyte case only. The face returned above is
2958 suitable for unibyte text if current_buffer is unibyte. */
2959 if (it->multibyte_p)
2960 {
2961 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
2962 struct face *face = FACE_FROM_ID (it->f, face_id);
2963 face_id = FACE_FOR_CHAR (it->f, face, c);
2964 }
2965 }
2966
2967 return face_id;
2968 }
2969
2970
2971 \f
2972 /***********************************************************************
2973 Invisible text
2974 ***********************************************************************/
2975
2976 /* Set up iterator IT from invisible properties at its current
2977 position. Called from handle_stop. */
2978
2979 static enum prop_handled
2980 handle_invisible_prop (it)
2981 struct it *it;
2982 {
2983 enum prop_handled handled = HANDLED_NORMALLY;
2984
2985 if (STRINGP (it->string))
2986 {
2987 extern Lisp_Object Qinvisible;
2988 Lisp_Object prop, end_charpos, limit, charpos;
2989
2990 /* Get the value of the invisible text property at the
2991 current position. Value will be nil if there is no such
2992 property. */
2993 charpos = make_number (IT_STRING_CHARPOS (*it));
2994 prop = Fget_text_property (charpos, Qinvisible, it->string);
2995
2996 if (!NILP (prop)
2997 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2998 {
2999 handled = HANDLED_RECOMPUTE_PROPS;
3000
3001 /* Get the position at which the next change of the
3002 invisible text property can be found in IT->string.
3003 Value will be nil if the property value is the same for
3004 all the rest of IT->string. */
3005 XSETINT (limit, SCHARS (it->string));
3006 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3007 it->string, limit);
3008
3009 /* Text at current position is invisible. The next
3010 change in the property is at position end_charpos.
3011 Move IT's current position to that position. */
3012 if (INTEGERP (end_charpos)
3013 && XFASTINT (end_charpos) < XFASTINT (limit))
3014 {
3015 struct text_pos old;
3016 old = it->current.string_pos;
3017 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3018 compute_string_pos (&it->current.string_pos, old, it->string);
3019 }
3020 else
3021 {
3022 /* The rest of the string is invisible. If this is an
3023 overlay string, proceed with the next overlay string
3024 or whatever comes and return a character from there. */
3025 if (it->current.overlay_string_index >= 0)
3026 {
3027 next_overlay_string (it);
3028 /* Don't check for overlay strings when we just
3029 finished processing them. */
3030 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3031 }
3032 else
3033 {
3034 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3035 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3036 }
3037 }
3038 }
3039 }
3040 else
3041 {
3042 int invis_p, newpos, next_stop, start_charpos;
3043 Lisp_Object pos, prop, overlay;
3044
3045 /* First of all, is there invisible text at this position? */
3046 start_charpos = IT_CHARPOS (*it);
3047 pos = make_number (IT_CHARPOS (*it));
3048 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3049 &overlay);
3050 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3051
3052 /* If we are on invisible text, skip over it. */
3053 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3054 {
3055 /* Record whether we have to display an ellipsis for the
3056 invisible text. */
3057 int display_ellipsis_p = invis_p == 2;
3058
3059 handled = HANDLED_RECOMPUTE_PROPS;
3060
3061 /* Loop skipping over invisible text. The loop is left at
3062 ZV or with IT on the first char being visible again. */
3063 do
3064 {
3065 /* Try to skip some invisible text. Return value is the
3066 position reached which can be equal to IT's position
3067 if there is nothing invisible here. This skips both
3068 over invisible text properties and overlays with
3069 invisible property. */
3070 newpos = skip_invisible (IT_CHARPOS (*it),
3071 &next_stop, ZV, it->window);
3072
3073 /* If we skipped nothing at all we weren't at invisible
3074 text in the first place. If everything to the end of
3075 the buffer was skipped, end the loop. */
3076 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3077 invis_p = 0;
3078 else
3079 {
3080 /* We skipped some characters but not necessarily
3081 all there are. Check if we ended up on visible
3082 text. Fget_char_property returns the property of
3083 the char before the given position, i.e. if we
3084 get invis_p = 0, this means that the char at
3085 newpos is visible. */
3086 pos = make_number (newpos);
3087 prop = Fget_char_property (pos, Qinvisible, it->window);
3088 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3089 }
3090
3091 /* If we ended up on invisible text, proceed to
3092 skip starting with next_stop. */
3093 if (invis_p)
3094 IT_CHARPOS (*it) = next_stop;
3095 }
3096 while (invis_p);
3097
3098 /* The position newpos is now either ZV or on visible text. */
3099 IT_CHARPOS (*it) = newpos;
3100 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3101
3102 /* If there are before-strings at the start of invisible
3103 text, and the text is invisible because of a text
3104 property, arrange to show before-strings because 20.x did
3105 it that way. (If the text is invisible because of an
3106 overlay property instead of a text property, this is
3107 already handled in the overlay code.) */
3108 if (NILP (overlay)
3109 && get_overlay_strings (it, start_charpos))
3110 {
3111 handled = HANDLED_RECOMPUTE_PROPS;
3112 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3113 }
3114 else if (display_ellipsis_p)
3115 setup_for_ellipsis (it);
3116 }
3117 }
3118
3119 return handled;
3120 }
3121
3122
3123 /* Make iterator IT return `...' next. */
3124
3125 static void
3126 setup_for_ellipsis (it)
3127 struct it *it;
3128 {
3129 if (it->dp
3130 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3131 {
3132 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3133 it->dpvec = v->contents;
3134 it->dpend = v->contents + v->size;
3135 }
3136 else
3137 {
3138 /* Default `...'. */
3139 it->dpvec = default_invis_vector;
3140 it->dpend = default_invis_vector + 3;
3141 }
3142
3143 /* The ellipsis display does not replace the display of the
3144 character at the new position. Indicate this by setting
3145 IT->dpvec_char_len to zero. */
3146 it->dpvec_char_len = 0;
3147
3148 it->current.dpvec_index = 0;
3149 it->method = next_element_from_display_vector;
3150 }
3151
3152
3153 \f
3154 /***********************************************************************
3155 'display' property
3156 ***********************************************************************/
3157
3158 /* Set up iterator IT from `display' property at its current position.
3159 Called from handle_stop. */
3160
3161 static enum prop_handled
3162 handle_display_prop (it)
3163 struct it *it;
3164 {
3165 Lisp_Object prop, object;
3166 struct text_pos *position;
3167 int display_replaced_p = 0;
3168
3169 if (STRINGP (it->string))
3170 {
3171 object = it->string;
3172 position = &it->current.string_pos;
3173 }
3174 else
3175 {
3176 object = it->w->buffer;
3177 position = &it->current.pos;
3178 }
3179
3180 /* Reset those iterator values set from display property values. */
3181 it->font_height = Qnil;
3182 it->space_width = Qnil;
3183 it->voffset = 0;
3184
3185 /* We don't support recursive `display' properties, i.e. string
3186 values that have a string `display' property, that have a string
3187 `display' property etc. */
3188 if (!it->string_from_display_prop_p)
3189 it->area = TEXT_AREA;
3190
3191 prop = Fget_char_property (make_number (position->charpos),
3192 Qdisplay, object);
3193 if (NILP (prop))
3194 return HANDLED_NORMALLY;
3195
3196 if (CONSP (prop)
3197 /* Simple properties. */
3198 && !EQ (XCAR (prop), Qimage)
3199 && !EQ (XCAR (prop), Qspace)
3200 && !EQ (XCAR (prop), Qwhen)
3201 && !EQ (XCAR (prop), Qspace_width)
3202 && !EQ (XCAR (prop), Qheight)
3203 && !EQ (XCAR (prop), Qraise)
3204 /* Marginal area specifications. */
3205 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3206 && !NILP (XCAR (prop)))
3207 {
3208 for (; CONSP (prop); prop = XCDR (prop))
3209 {
3210 if (handle_single_display_prop (it, XCAR (prop), object,
3211 position, display_replaced_p))
3212 display_replaced_p = 1;
3213 }
3214 }
3215 else if (VECTORP (prop))
3216 {
3217 int i;
3218 for (i = 0; i < ASIZE (prop); ++i)
3219 if (handle_single_display_prop (it, AREF (prop, i), object,
3220 position, display_replaced_p))
3221 display_replaced_p = 1;
3222 }
3223 else
3224 {
3225 if (handle_single_display_prop (it, prop, object, position, 0))
3226 display_replaced_p = 1;
3227 }
3228
3229 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3230 }
3231
3232
3233 /* Value is the position of the end of the `display' property starting
3234 at START_POS in OBJECT. */
3235
3236 static struct text_pos
3237 display_prop_end (it, object, start_pos)
3238 struct it *it;
3239 Lisp_Object object;
3240 struct text_pos start_pos;
3241 {
3242 Lisp_Object end;
3243 struct text_pos end_pos;
3244
3245 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3246 Qdisplay, object, Qnil);
3247 CHARPOS (end_pos) = XFASTINT (end);
3248 if (STRINGP (object))
3249 compute_string_pos (&end_pos, start_pos, it->string);
3250 else
3251 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3252
3253 return end_pos;
3254 }
3255
3256
3257 /* Set up IT from a single `display' sub-property value PROP. OBJECT
3258 is the object in which the `display' property was found. *POSITION
3259 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3260 means that we previously saw a display sub-property which already
3261 replaced text display with something else, for example an image;
3262 ignore such properties after the first one has been processed.
3263
3264 If PROP is a `space' or `image' sub-property, set *POSITION to the
3265 end position of the `display' property.
3266
3267 Value is non-zero if something was found which replaces the display
3268 of buffer or string text. */
3269
3270 static int
3271 handle_single_display_prop (it, prop, object, position,
3272 display_replaced_before_p)
3273 struct it *it;
3274 Lisp_Object prop;
3275 Lisp_Object object;
3276 struct text_pos *position;
3277 int display_replaced_before_p;
3278 {
3279 Lisp_Object value;
3280 int replaces_text_display_p = 0;
3281 Lisp_Object form;
3282
3283 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
3284 evaluated. If the result is nil, VALUE is ignored. */
3285 form = Qt;
3286 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3287 {
3288 prop = XCDR (prop);
3289 if (!CONSP (prop))
3290 return 0;
3291 form = XCAR (prop);
3292 prop = XCDR (prop);
3293 }
3294
3295 if (!NILP (form) && !EQ (form, Qt))
3296 {
3297 int count = SPECPDL_INDEX ();
3298 struct gcpro gcpro1;
3299
3300 /* Bind `object' to the object having the `display' property, a
3301 buffer or string. Bind `position' to the position in the
3302 object where the property was found, and `buffer-position'
3303 to the current position in the buffer. */
3304 specbind (Qobject, object);
3305 specbind (Qposition, make_number (CHARPOS (*position)));
3306 specbind (Qbuffer_position,
3307 make_number (STRINGP (object)
3308 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3309 GCPRO1 (form);
3310 form = safe_eval (form);
3311 UNGCPRO;
3312 unbind_to (count, Qnil);
3313 }
3314
3315 if (NILP (form))
3316 return 0;
3317
3318 if (CONSP (prop)
3319 && EQ (XCAR (prop), Qheight)
3320 && CONSP (XCDR (prop)))
3321 {
3322 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3323 return 0;
3324
3325 /* `(height HEIGHT)'. */
3326 it->font_height = XCAR (XCDR (prop));
3327 if (!NILP (it->font_height))
3328 {
3329 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3330 int new_height = -1;
3331
3332 if (CONSP (it->font_height)
3333 && (EQ (XCAR (it->font_height), Qplus)
3334 || EQ (XCAR (it->font_height), Qminus))
3335 && CONSP (XCDR (it->font_height))
3336 && INTEGERP (XCAR (XCDR (it->font_height))))
3337 {
3338 /* `(+ N)' or `(- N)' where N is an integer. */
3339 int steps = XINT (XCAR (XCDR (it->font_height)));
3340 if (EQ (XCAR (it->font_height), Qplus))
3341 steps = - steps;
3342 it->face_id = smaller_face (it->f, it->face_id, steps);
3343 }
3344 else if (FUNCTIONP (it->font_height))
3345 {
3346 /* Call function with current height as argument.
3347 Value is the new height. */
3348 Lisp_Object height;
3349 height = safe_call1 (it->font_height,
3350 face->lface[LFACE_HEIGHT_INDEX]);
3351 if (NUMBERP (height))
3352 new_height = XFLOATINT (height);
3353 }
3354 else if (NUMBERP (it->font_height))
3355 {
3356 /* Value is a multiple of the canonical char height. */
3357 struct face *face;
3358
3359 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3360 new_height = (XFLOATINT (it->font_height)
3361 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3362 }
3363 else
3364 {
3365 /* Evaluate IT->font_height with `height' bound to the
3366 current specified height to get the new height. */
3367 Lisp_Object value;
3368 int count = SPECPDL_INDEX ();
3369
3370 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3371 value = safe_eval (it->font_height);
3372 unbind_to (count, Qnil);
3373
3374 if (NUMBERP (value))
3375 new_height = XFLOATINT (value);
3376 }
3377
3378 if (new_height > 0)
3379 it->face_id = face_with_height (it->f, it->face_id, new_height);
3380 }
3381 }
3382 else if (CONSP (prop)
3383 && EQ (XCAR (prop), Qspace_width)
3384 && CONSP (XCDR (prop)))
3385 {
3386 /* `(space_width WIDTH)'. */
3387 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3388 return 0;
3389
3390 value = XCAR (XCDR (prop));
3391 if (NUMBERP (value) && XFLOATINT (value) > 0)
3392 it->space_width = value;
3393 }
3394 else if (CONSP (prop)
3395 && EQ (XCAR (prop), Qraise)
3396 && CONSP (XCDR (prop)))
3397 {
3398 /* `(raise FACTOR)'. */
3399 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3400 return 0;
3401
3402 #ifdef HAVE_WINDOW_SYSTEM
3403 value = XCAR (XCDR (prop));
3404 if (NUMBERP (value))
3405 {
3406 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3407 it->voffset = - (XFLOATINT (value)
3408 * (FONT_HEIGHT (face->font)));
3409 }
3410 #endif /* HAVE_WINDOW_SYSTEM */
3411 }
3412 else if (!it->string_from_display_prop_p)
3413 {
3414 /* `((margin left-margin) VALUE)' or `((margin right-margin)
3415 VALUE) or `((margin nil) VALUE)' or VALUE. */
3416 Lisp_Object location, value;
3417 struct text_pos start_pos;
3418 int valid_p;
3419
3420 /* Characters having this form of property are not displayed, so
3421 we have to find the end of the property. */
3422 start_pos = *position;
3423 *position = display_prop_end (it, object, start_pos);
3424 value = Qnil;
3425
3426 /* Let's stop at the new position and assume that all
3427 text properties change there. */
3428 it->stop_charpos = position->charpos;
3429
3430 location = Qunbound;
3431 if (CONSP (prop) && CONSP (XCAR (prop)))
3432 {
3433 Lisp_Object tem;
3434
3435 value = XCDR (prop);
3436 if (CONSP (value))
3437 value = XCAR (value);
3438
3439 tem = XCAR (prop);
3440 if (EQ (XCAR (tem), Qmargin)
3441 && (tem = XCDR (tem),
3442 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3443 (NILP (tem)
3444 || EQ (tem, Qleft_margin)
3445 || EQ (tem, Qright_margin))))
3446 location = tem;
3447 }
3448
3449 if (EQ (location, Qunbound))
3450 {
3451 location = Qnil;
3452 value = prop;
3453 }
3454
3455 #ifdef HAVE_WINDOW_SYSTEM
3456 if (FRAME_TERMCAP_P (it->f))
3457 valid_p = STRINGP (value);
3458 else
3459 valid_p = (STRINGP (value)
3460 || (CONSP (value) && EQ (XCAR (value), Qspace))
3461 || valid_image_p (value));
3462 #else /* not HAVE_WINDOW_SYSTEM */
3463 valid_p = STRINGP (value);
3464 #endif /* not HAVE_WINDOW_SYSTEM */
3465
3466 if ((EQ (location, Qleft_margin)
3467 || EQ (location, Qright_margin)
3468 || NILP (location))
3469 && valid_p
3470 && !display_replaced_before_p)
3471 {
3472 replaces_text_display_p = 1;
3473
3474 /* Save current settings of IT so that we can restore them
3475 when we are finished with the glyph property value. */
3476 push_it (it);
3477
3478 if (NILP (location))
3479 it->area = TEXT_AREA;
3480 else if (EQ (location, Qleft_margin))
3481 it->area = LEFT_MARGIN_AREA;
3482 else
3483 it->area = RIGHT_MARGIN_AREA;
3484
3485 if (STRINGP (value))
3486 {
3487 it->string = value;
3488 it->multibyte_p = STRING_MULTIBYTE (it->string);
3489 it->current.overlay_string_index = -1;
3490 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3491 it->end_charpos = it->string_nchars = SCHARS (it->string);
3492 it->method = next_element_from_string;
3493 it->stop_charpos = 0;
3494 it->string_from_display_prop_p = 1;
3495 /* Say that we haven't consumed the characters with
3496 `display' property yet. The call to pop_it in
3497 set_iterator_to_next will clean this up. */
3498 *position = start_pos;
3499 }
3500 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3501 {
3502 it->method = next_element_from_stretch;
3503 it->object = value;
3504 it->current.pos = it->position = start_pos;
3505 }
3506 #ifdef HAVE_WINDOW_SYSTEM
3507 else
3508 {
3509 it->what = IT_IMAGE;
3510 it->image_id = lookup_image (it->f, value);
3511 it->position = start_pos;
3512 it->object = NILP (object) ? it->w->buffer : object;
3513 it->method = next_element_from_image;
3514
3515 /* Say that we haven't consumed the characters with
3516 `display' property yet. The call to pop_it in
3517 set_iterator_to_next will clean this up. */
3518 *position = start_pos;
3519 }
3520 #endif /* HAVE_WINDOW_SYSTEM */
3521 }
3522 else
3523 /* Invalid property or property not supported. Restore
3524 the position to what it was before. */
3525 *position = start_pos;
3526 }
3527
3528 return replaces_text_display_p;
3529 }
3530
3531
3532 /* Check if PROP is a display sub-property value whose text should be
3533 treated as intangible. */
3534
3535 static int
3536 single_display_prop_intangible_p (prop)
3537 Lisp_Object prop;
3538 {
3539 /* Skip over `when FORM'. */
3540 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3541 {
3542 prop = XCDR (prop);
3543 if (!CONSP (prop))
3544 return 0;
3545 prop = XCDR (prop);
3546 }
3547
3548 if (STRINGP (prop))
3549 return 1;
3550
3551 if (!CONSP (prop))
3552 return 0;
3553
3554 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3555 we don't need to treat text as intangible. */
3556 if (EQ (XCAR (prop), Qmargin))
3557 {
3558 prop = XCDR (prop);
3559 if (!CONSP (prop))
3560 return 0;
3561
3562 prop = XCDR (prop);
3563 if (!CONSP (prop)
3564 || EQ (XCAR (prop), Qleft_margin)
3565 || EQ (XCAR (prop), Qright_margin))
3566 return 0;
3567 }
3568
3569 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3570 }
3571
3572
3573 /* Check if PROP is a display property value whose text should be
3574 treated as intangible. */
3575
3576 int
3577 display_prop_intangible_p (prop)
3578 Lisp_Object prop;
3579 {
3580 if (CONSP (prop)
3581 && CONSP (XCAR (prop))
3582 && !EQ (Qmargin, XCAR (XCAR (prop))))
3583 {
3584 /* A list of sub-properties. */
3585 while (CONSP (prop))
3586 {
3587 if (single_display_prop_intangible_p (XCAR (prop)))
3588 return 1;
3589 prop = XCDR (prop);
3590 }
3591 }
3592 else if (VECTORP (prop))
3593 {
3594 /* A vector of sub-properties. */
3595 int i;
3596 for (i = 0; i < ASIZE (prop); ++i)
3597 if (single_display_prop_intangible_p (AREF (prop, i)))
3598 return 1;
3599 }
3600 else
3601 return single_display_prop_intangible_p (prop);
3602
3603 return 0;
3604 }
3605
3606
3607 /* Return 1 if PROP is a display sub-property value containing STRING. */
3608
3609 static int
3610 single_display_prop_string_p (prop, string)
3611 Lisp_Object prop, string;
3612 {
3613 if (EQ (string, prop))
3614 return 1;
3615
3616 /* Skip over `when FORM'. */
3617 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3618 {
3619 prop = XCDR (prop);
3620 if (!CONSP (prop))
3621 return 0;
3622 prop = XCDR (prop);
3623 }
3624
3625 if (CONSP (prop))
3626 /* Skip over `margin LOCATION'. */
3627 if (EQ (XCAR (prop), Qmargin))
3628 {
3629 prop = XCDR (prop);
3630 if (!CONSP (prop))
3631 return 0;
3632
3633 prop = XCDR (prop);
3634 if (!CONSP (prop))
3635 return 0;
3636 }
3637
3638 return CONSP (prop) && EQ (XCAR (prop), string);
3639 }
3640
3641
3642 /* Return 1 if STRING appears in the `display' property PROP. */
3643
3644 static int
3645 display_prop_string_p (prop, string)
3646 Lisp_Object prop, string;
3647 {
3648 if (CONSP (prop)
3649 && CONSP (XCAR (prop))
3650 && !EQ (Qmargin, XCAR (XCAR (prop))))
3651 {
3652 /* A list of sub-properties. */
3653 while (CONSP (prop))
3654 {
3655 if (single_display_prop_string_p (XCAR (prop), string))
3656 return 1;
3657 prop = XCDR (prop);
3658 }
3659 }
3660 else if (VECTORP (prop))
3661 {
3662 /* A vector of sub-properties. */
3663 int i;
3664 for (i = 0; i < ASIZE (prop); ++i)
3665 if (single_display_prop_string_p (AREF (prop, i), string))
3666 return 1;
3667 }
3668 else
3669 return single_display_prop_string_p (prop, string);
3670
3671 return 0;
3672 }
3673
3674
3675 /* Determine from which buffer position in W's buffer STRING comes
3676 from. AROUND_CHARPOS is an approximate position where it could
3677 be from. Value is the buffer position or 0 if it couldn't be
3678 determined.
3679
3680 W's buffer must be current.
3681
3682 This function is necessary because we don't record buffer positions
3683 in glyphs generated from strings (to keep struct glyph small).
3684 This function may only use code that doesn't eval because it is
3685 called asynchronously from note_mouse_highlight. */
3686
3687 int
3688 string_buffer_position (w, string, around_charpos)
3689 struct window *w;
3690 Lisp_Object string;
3691 int around_charpos;
3692 {
3693 Lisp_Object limit, prop, pos;
3694 const int MAX_DISTANCE = 1000;
3695 int found = 0;
3696
3697 pos = make_number (around_charpos);
3698 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3699 while (!found && !EQ (pos, limit))
3700 {
3701 prop = Fget_char_property (pos, Qdisplay, Qnil);
3702 if (!NILP (prop) && display_prop_string_p (prop, string))
3703 found = 1;
3704 else
3705 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3706 }
3707
3708 if (!found)
3709 {
3710 pos = make_number (around_charpos);
3711 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3712 while (!found && !EQ (pos, limit))
3713 {
3714 prop = Fget_char_property (pos, Qdisplay, Qnil);
3715 if (!NILP (prop) && display_prop_string_p (prop, string))
3716 found = 1;
3717 else
3718 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3719 limit);
3720 }
3721 }
3722
3723 return found ? XINT (pos) : 0;
3724 }
3725
3726
3727 \f
3728 /***********************************************************************
3729 `composition' property
3730 ***********************************************************************/
3731
3732 /* Set up iterator IT from `composition' property at its current
3733 position. Called from handle_stop. */
3734
3735 static enum prop_handled
3736 handle_composition_prop (it)
3737 struct it *it;
3738 {
3739 Lisp_Object prop, string;
3740 int pos, pos_byte, end;
3741 enum prop_handled handled = HANDLED_NORMALLY;
3742
3743 if (STRINGP (it->string))
3744 {
3745 pos = IT_STRING_CHARPOS (*it);
3746 pos_byte = IT_STRING_BYTEPOS (*it);
3747 string = it->string;
3748 }
3749 else
3750 {
3751 pos = IT_CHARPOS (*it);
3752 pos_byte = IT_BYTEPOS (*it);
3753 string = Qnil;
3754 }
3755
3756 /* If there's a valid composition and point is not inside of the
3757 composition (in the case that the composition is from the current
3758 buffer), draw a glyph composed from the composition components. */
3759 if (find_composition (pos, -1, &pos, &end, &prop, string)
3760 && COMPOSITION_VALID_P (pos, end, prop)
3761 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3762 {
3763 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3764
3765 if (id >= 0)
3766 {
3767 it->method = next_element_from_composition;
3768 it->cmp_id = id;
3769 it->cmp_len = COMPOSITION_LENGTH (prop);
3770 /* For a terminal, draw only the first character of the
3771 components. */
3772 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3773 it->len = (STRINGP (it->string)
3774 ? string_char_to_byte (it->string, end)
3775 : CHAR_TO_BYTE (end)) - pos_byte;
3776 it->stop_charpos = end;
3777 handled = HANDLED_RETURN;
3778 }
3779 }
3780
3781 return handled;
3782 }
3783
3784
3785 \f
3786 /***********************************************************************
3787 Overlay strings
3788 ***********************************************************************/
3789
3790 /* The following structure is used to record overlay strings for
3791 later sorting in load_overlay_strings. */
3792
3793 struct overlay_entry
3794 {
3795 Lisp_Object overlay;
3796 Lisp_Object string;
3797 int priority;
3798 int after_string_p;
3799 };
3800
3801
3802 /* Set up iterator IT from overlay strings at its current position.
3803 Called from handle_stop. */
3804
3805 static enum prop_handled
3806 handle_overlay_change (it)
3807 struct it *it;
3808 {
3809 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3810 return HANDLED_RECOMPUTE_PROPS;
3811 else
3812 return HANDLED_NORMALLY;
3813 }
3814
3815
3816 /* Set up the next overlay string for delivery by IT, if there is an
3817 overlay string to deliver. Called by set_iterator_to_next when the
3818 end of the current overlay string is reached. If there are more
3819 overlay strings to display, IT->string and
3820 IT->current.overlay_string_index are set appropriately here.
3821 Otherwise IT->string is set to nil. */
3822
3823 static void
3824 next_overlay_string (it)
3825 struct it *it;
3826 {
3827 ++it->current.overlay_string_index;
3828 if (it->current.overlay_string_index == it->n_overlay_strings)
3829 {
3830 /* No more overlay strings. Restore IT's settings to what
3831 they were before overlay strings were processed, and
3832 continue to deliver from current_buffer. */
3833 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3834
3835 pop_it (it);
3836 xassert (it->stop_charpos >= BEGV
3837 && it->stop_charpos <= it->end_charpos);
3838 it->string = Qnil;
3839 it->current.overlay_string_index = -1;
3840 SET_TEXT_POS (it->current.string_pos, -1, -1);
3841 it->n_overlay_strings = 0;
3842 it->method = next_element_from_buffer;
3843
3844 /* If we're at the end of the buffer, record that we have
3845 processed the overlay strings there already, so that
3846 next_element_from_buffer doesn't try it again. */
3847 if (IT_CHARPOS (*it) >= it->end_charpos)
3848 it->overlay_strings_at_end_processed_p = 1;
3849
3850 /* If we have to display `...' for invisible text, set
3851 the iterator up for that. */
3852 if (display_ellipsis_p)
3853 setup_for_ellipsis (it);
3854 }
3855 else
3856 {
3857 /* There are more overlay strings to process. If
3858 IT->current.overlay_string_index has advanced to a position
3859 where we must load IT->overlay_strings with more strings, do
3860 it. */
3861 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3862
3863 if (it->current.overlay_string_index && i == 0)
3864 load_overlay_strings (it, 0);
3865
3866 /* Initialize IT to deliver display elements from the overlay
3867 string. */
3868 it->string = it->overlay_strings[i];
3869 it->multibyte_p = STRING_MULTIBYTE (it->string);
3870 SET_TEXT_POS (it->current.string_pos, 0, 0);
3871 it->method = next_element_from_string;
3872 it->stop_charpos = 0;
3873 }
3874
3875 CHECK_IT (it);
3876 }
3877
3878
3879 /* Compare two overlay_entry structures E1 and E2. Used as a
3880 comparison function for qsort in load_overlay_strings. Overlay
3881 strings for the same position are sorted so that
3882
3883 1. All after-strings come in front of before-strings, except
3884 when they come from the same overlay.
3885
3886 2. Within after-strings, strings are sorted so that overlay strings
3887 from overlays with higher priorities come first.
3888
3889 2. Within before-strings, strings are sorted so that overlay
3890 strings from overlays with higher priorities come last.
3891
3892 Value is analogous to strcmp. */
3893
3894
3895 static int
3896 compare_overlay_entries (e1, e2)
3897 void *e1, *e2;
3898 {
3899 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3900 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3901 int result;
3902
3903 if (entry1->after_string_p != entry2->after_string_p)
3904 {
3905 /* Let after-strings appear in front of before-strings if
3906 they come from different overlays. */
3907 if (EQ (entry1->overlay, entry2->overlay))
3908 result = entry1->after_string_p ? 1 : -1;
3909 else
3910 result = entry1->after_string_p ? -1 : 1;
3911 }
3912 else if (entry1->after_string_p)
3913 /* After-strings sorted in order of decreasing priority. */
3914 result = entry2->priority - entry1->priority;
3915 else
3916 /* Before-strings sorted in order of increasing priority. */
3917 result = entry1->priority - entry2->priority;
3918
3919 return result;
3920 }
3921
3922
3923 /* Load the vector IT->overlay_strings with overlay strings from IT's
3924 current buffer position, or from CHARPOS if that is > 0. Set
3925 IT->n_overlays to the total number of overlay strings found.
3926
3927 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3928 a time. On entry into load_overlay_strings,
3929 IT->current.overlay_string_index gives the number of overlay
3930 strings that have already been loaded by previous calls to this
3931 function.
3932
3933 IT->add_overlay_start contains an additional overlay start
3934 position to consider for taking overlay strings from, if non-zero.
3935 This position comes into play when the overlay has an `invisible'
3936 property, and both before and after-strings. When we've skipped to
3937 the end of the overlay, because of its `invisible' property, we
3938 nevertheless want its before-string to appear.
3939 IT->add_overlay_start will contain the overlay start position
3940 in this case.
3941
3942 Overlay strings are sorted so that after-string strings come in
3943 front of before-string strings. Within before and after-strings,
3944 strings are sorted by overlay priority. See also function
3945 compare_overlay_entries. */
3946
3947 static void
3948 load_overlay_strings (it, charpos)
3949 struct it *it;
3950 int charpos;
3951 {
3952 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3953 Lisp_Object ov, overlay, window, str, invisible;
3954 int start, end;
3955 int size = 20;
3956 int n = 0, i, j, invis_p;
3957 struct overlay_entry *entries
3958 = (struct overlay_entry *) alloca (size * sizeof *entries);
3959
3960 if (charpos <= 0)
3961 charpos = IT_CHARPOS (*it);
3962
3963 /* Append the overlay string STRING of overlay OVERLAY to vector
3964 `entries' which has size `size' and currently contains `n'
3965 elements. AFTER_P non-zero means STRING is an after-string of
3966 OVERLAY. */
3967 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3968 do \
3969 { \
3970 Lisp_Object priority; \
3971 \
3972 if (n == size) \
3973 { \
3974 int new_size = 2 * size; \
3975 struct overlay_entry *old = entries; \
3976 entries = \
3977 (struct overlay_entry *) alloca (new_size \
3978 * sizeof *entries); \
3979 bcopy (old, entries, size * sizeof *entries); \
3980 size = new_size; \
3981 } \
3982 \
3983 entries[n].string = (STRING); \
3984 entries[n].overlay = (OVERLAY); \
3985 priority = Foverlay_get ((OVERLAY), Qpriority); \
3986 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3987 entries[n].after_string_p = (AFTER_P); \
3988 ++n; \
3989 } \
3990 while (0)
3991
3992 /* Process overlay before the overlay center. */
3993 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3994 {
3995 overlay = XCAR (ov);
3996 xassert (OVERLAYP (overlay));
3997 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3998 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3999
4000 if (end < charpos)
4001 break;
4002
4003 /* Skip this overlay if it doesn't start or end at IT's current
4004 position. */
4005 if (end != charpos && start != charpos)
4006 continue;
4007
4008 /* Skip this overlay if it doesn't apply to IT->w. */
4009 window = Foverlay_get (overlay, Qwindow);
4010 if (WINDOWP (window) && XWINDOW (window) != it->w)
4011 continue;
4012
4013 /* If the text ``under'' the overlay is invisible, both before-
4014 and after-strings from this overlay are visible; start and
4015 end position are indistinguishable. */
4016 invisible = Foverlay_get (overlay, Qinvisible);
4017 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4018
4019 /* If overlay has a non-empty before-string, record it. */
4020 if ((start == charpos || (end == charpos && invis_p))
4021 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4022 && SCHARS (str))
4023 RECORD_OVERLAY_STRING (overlay, str, 0);
4024
4025 /* If overlay has a non-empty after-string, record it. */
4026 if ((end == charpos || (start == charpos && invis_p))
4027 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4028 && SCHARS (str))
4029 RECORD_OVERLAY_STRING (overlay, str, 1);
4030 }
4031
4032 /* Process overlays after the overlay center. */
4033 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
4034 {
4035 overlay = XCAR (ov);
4036 xassert (OVERLAYP (overlay));
4037 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4038 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4039
4040 if (start > charpos)
4041 break;
4042
4043 /* Skip this overlay if it doesn't start or end at IT's current
4044 position. */
4045 if (end != charpos && start != charpos)
4046 continue;
4047
4048 /* Skip this overlay if it doesn't apply to IT->w. */
4049 window = Foverlay_get (overlay, Qwindow);
4050 if (WINDOWP (window) && XWINDOW (window) != it->w)
4051 continue;
4052
4053 /* If the text ``under'' the overlay is invisible, it has a zero
4054 dimension, and both before- and after-strings apply. */
4055 invisible = Foverlay_get (overlay, Qinvisible);
4056 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4057
4058 /* If overlay has a non-empty before-string, record it. */
4059 if ((start == charpos || (end == charpos && invis_p))
4060 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4061 && SCHARS (str))
4062 RECORD_OVERLAY_STRING (overlay, str, 0);
4063
4064 /* If overlay has a non-empty after-string, record it. */
4065 if ((end == charpos || (start == charpos && invis_p))
4066 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4067 && SCHARS (str))
4068 RECORD_OVERLAY_STRING (overlay, str, 1);
4069 }
4070
4071 #undef RECORD_OVERLAY_STRING
4072
4073 /* Sort entries. */
4074 if (n > 1)
4075 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4076
4077 /* Record the total number of strings to process. */
4078 it->n_overlay_strings = n;
4079
4080 /* IT->current.overlay_string_index is the number of overlay strings
4081 that have already been consumed by IT. Copy some of the
4082 remaining overlay strings to IT->overlay_strings. */
4083 i = 0;
4084 j = it->current.overlay_string_index;
4085 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4086 it->overlay_strings[i++] = entries[j++].string;
4087
4088 CHECK_IT (it);
4089 }
4090
4091
4092 /* Get the first chunk of overlay strings at IT's current buffer
4093 position, or at CHARPOS if that is > 0. Value is non-zero if at
4094 least one overlay string was found. */
4095
4096 static int
4097 get_overlay_strings (it, charpos)
4098 struct it *it;
4099 int charpos;
4100 {
4101 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4102 process. This fills IT->overlay_strings with strings, and sets
4103 IT->n_overlay_strings to the total number of strings to process.
4104 IT->pos.overlay_string_index has to be set temporarily to zero
4105 because load_overlay_strings needs this; it must be set to -1
4106 when no overlay strings are found because a zero value would
4107 indicate a position in the first overlay string. */
4108 it->current.overlay_string_index = 0;
4109 load_overlay_strings (it, charpos);
4110
4111 /* If we found overlay strings, set up IT to deliver display
4112 elements from the first one. Otherwise set up IT to deliver
4113 from current_buffer. */
4114 if (it->n_overlay_strings)
4115 {
4116 /* Make sure we know settings in current_buffer, so that we can
4117 restore meaningful values when we're done with the overlay
4118 strings. */
4119 compute_stop_pos (it);
4120 xassert (it->face_id >= 0);
4121
4122 /* Save IT's settings. They are restored after all overlay
4123 strings have been processed. */
4124 xassert (it->sp == 0);
4125 push_it (it);
4126
4127 /* Set up IT to deliver display elements from the first overlay
4128 string. */
4129 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4130 it->string = it->overlay_strings[0];
4131 it->stop_charpos = 0;
4132 xassert (STRINGP (it->string));
4133 it->end_charpos = SCHARS (it->string);
4134 it->multibyte_p = STRING_MULTIBYTE (it->string);
4135 it->method = next_element_from_string;
4136 }
4137 else
4138 {
4139 it->string = Qnil;
4140 it->current.overlay_string_index = -1;
4141 it->method = next_element_from_buffer;
4142 }
4143
4144 CHECK_IT (it);
4145
4146 /* Value is non-zero if we found at least one overlay string. */
4147 return STRINGP (it->string);
4148 }
4149
4150
4151 \f
4152 /***********************************************************************
4153 Saving and restoring state
4154 ***********************************************************************/
4155
4156 /* Save current settings of IT on IT->stack. Called, for example,
4157 before setting up IT for an overlay string, to be able to restore
4158 IT's settings to what they were after the overlay string has been
4159 processed. */
4160
4161 static void
4162 push_it (it)
4163 struct it *it;
4164 {
4165 struct iterator_stack_entry *p;
4166
4167 xassert (it->sp < 2);
4168 p = it->stack + it->sp;
4169
4170 p->stop_charpos = it->stop_charpos;
4171 xassert (it->face_id >= 0);
4172 p->face_id = it->face_id;
4173 p->string = it->string;
4174 p->pos = it->current;
4175 p->end_charpos = it->end_charpos;
4176 p->string_nchars = it->string_nchars;
4177 p->area = it->area;
4178 p->multibyte_p = it->multibyte_p;
4179 p->space_width = it->space_width;
4180 p->font_height = it->font_height;
4181 p->voffset = it->voffset;
4182 p->string_from_display_prop_p = it->string_from_display_prop_p;
4183 p->display_ellipsis_p = 0;
4184 ++it->sp;
4185 }
4186
4187
4188 /* Restore IT's settings from IT->stack. Called, for example, when no
4189 more overlay strings must be processed, and we return to delivering
4190 display elements from a buffer, or when the end of a string from a
4191 `display' property is reached and we return to delivering display
4192 elements from an overlay string, or from a buffer. */
4193
4194 static void
4195 pop_it (it)
4196 struct it *it;
4197 {
4198 struct iterator_stack_entry *p;
4199
4200 xassert (it->sp > 0);
4201 --it->sp;
4202 p = it->stack + it->sp;
4203 it->stop_charpos = p->stop_charpos;
4204 it->face_id = p->face_id;
4205 it->string = p->string;
4206 it->current = p->pos;
4207 it->end_charpos = p->end_charpos;
4208 it->string_nchars = p->string_nchars;
4209 it->area = p->area;
4210 it->multibyte_p = p->multibyte_p;
4211 it->space_width = p->space_width;
4212 it->font_height = p->font_height;
4213 it->voffset = p->voffset;
4214 it->string_from_display_prop_p = p->string_from_display_prop_p;
4215 }
4216
4217
4218 \f
4219 /***********************************************************************
4220 Moving over lines
4221 ***********************************************************************/
4222
4223 /* Set IT's current position to the previous line start. */
4224
4225 static void
4226 back_to_previous_line_start (it)
4227 struct it *it;
4228 {
4229 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4230 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4231 }
4232
4233
4234 /* Move IT to the next line start.
4235
4236 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4237 we skipped over part of the text (as opposed to moving the iterator
4238 continuously over the text). Otherwise, don't change the value
4239 of *SKIPPED_P.
4240
4241 Newlines may come from buffer text, overlay strings, or strings
4242 displayed via the `display' property. That's the reason we can't
4243 simply use find_next_newline_no_quit.
4244
4245 Note that this function may not skip over invisible text that is so
4246 because of text properties and immediately follows a newline. If
4247 it would, function reseat_at_next_visible_line_start, when called
4248 from set_iterator_to_next, would effectively make invisible
4249 characters following a newline part of the wrong glyph row, which
4250 leads to wrong cursor motion. */
4251
4252 static int
4253 forward_to_next_line_start (it, skipped_p)
4254 struct it *it;
4255 int *skipped_p;
4256 {
4257 int old_selective, newline_found_p, n;
4258 const int MAX_NEWLINE_DISTANCE = 500;
4259
4260 /* If already on a newline, just consume it to avoid unintended
4261 skipping over invisible text below. */
4262 if (it->what == IT_CHARACTER
4263 && it->c == '\n'
4264 && CHARPOS (it->position) == IT_CHARPOS (*it))
4265 {
4266 set_iterator_to_next (it, 0);
4267 it->c = 0;
4268 return 1;
4269 }
4270
4271 /* Don't handle selective display in the following. It's (a)
4272 unnecessary because it's done by the caller, and (b) leads to an
4273 infinite recursion because next_element_from_ellipsis indirectly
4274 calls this function. */
4275 old_selective = it->selective;
4276 it->selective = 0;
4277
4278 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4279 from buffer text. */
4280 for (n = newline_found_p = 0;
4281 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4282 n += STRINGP (it->string) ? 0 : 1)
4283 {
4284 if (!get_next_display_element (it))
4285 return 0;
4286 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4287 set_iterator_to_next (it, 0);
4288 }
4289
4290 /* If we didn't find a newline near enough, see if we can use a
4291 short-cut. */
4292 if (!newline_found_p)
4293 {
4294 int start = IT_CHARPOS (*it);
4295 int limit = find_next_newline_no_quit (start, 1);
4296 Lisp_Object pos;
4297
4298 xassert (!STRINGP (it->string));
4299
4300 /* If there isn't any `display' property in sight, and no
4301 overlays, we can just use the position of the newline in
4302 buffer text. */
4303 if (it->stop_charpos >= limit
4304 || ((pos = Fnext_single_property_change (make_number (start),
4305 Qdisplay,
4306 Qnil, make_number (limit)),
4307 NILP (pos))
4308 && next_overlay_change (start) == ZV))
4309 {
4310 IT_CHARPOS (*it) = limit;
4311 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4312 *skipped_p = newline_found_p = 1;
4313 }
4314 else
4315 {
4316 while (get_next_display_element (it)
4317 && !newline_found_p)
4318 {
4319 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4320 set_iterator_to_next (it, 0);
4321 }
4322 }
4323 }
4324
4325 it->selective = old_selective;
4326 return newline_found_p;
4327 }
4328
4329
4330 /* Set IT's current position to the previous visible line start. Skip
4331 invisible text that is so either due to text properties or due to
4332 selective display. Caution: this does not change IT->current_x and
4333 IT->hpos. */
4334
4335 static void
4336 back_to_previous_visible_line_start (it)
4337 struct it *it;
4338 {
4339 int visible_p = 0;
4340
4341 /* Go back one newline if not on BEGV already. */
4342 if (IT_CHARPOS (*it) > BEGV)
4343 back_to_previous_line_start (it);
4344
4345 /* Move over lines that are invisible because of selective display
4346 or text properties. */
4347 while (IT_CHARPOS (*it) > BEGV
4348 && !visible_p)
4349 {
4350 visible_p = 1;
4351
4352 /* If selective > 0, then lines indented more than that values
4353 are invisible. */
4354 if (it->selective > 0
4355 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4356 (double) it->selective)) /* iftc */
4357 visible_p = 0;
4358 else
4359 {
4360 Lisp_Object prop;
4361
4362 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
4363 Qinvisible, it->window);
4364 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4365 visible_p = 0;
4366 }
4367
4368 /* Back one more newline if the current one is invisible. */
4369 if (!visible_p)
4370 back_to_previous_line_start (it);
4371 }
4372
4373 xassert (IT_CHARPOS (*it) >= BEGV);
4374 xassert (IT_CHARPOS (*it) == BEGV
4375 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4376 CHECK_IT (it);
4377 }
4378
4379
4380 /* Reseat iterator IT at the previous visible line start. Skip
4381 invisible text that is so either due to text properties or due to
4382 selective display. At the end, update IT's overlay information,
4383 face information etc. */
4384
4385 static void
4386 reseat_at_previous_visible_line_start (it)
4387 struct it *it;
4388 {
4389 back_to_previous_visible_line_start (it);
4390 reseat (it, it->current.pos, 1);
4391 CHECK_IT (it);
4392 }
4393
4394
4395 /* Reseat iterator IT on the next visible line start in the current
4396 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4397 preceding the line start. Skip over invisible text that is so
4398 because of selective display. Compute faces, overlays etc at the
4399 new position. Note that this function does not skip over text that
4400 is invisible because of text properties. */
4401
4402 static void
4403 reseat_at_next_visible_line_start (it, on_newline_p)
4404 struct it *it;
4405 int on_newline_p;
4406 {
4407 int newline_found_p, skipped_p = 0;
4408
4409 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4410
4411 /* Skip over lines that are invisible because they are indented
4412 more than the value of IT->selective. */
4413 if (it->selective > 0)
4414 while (IT_CHARPOS (*it) < ZV
4415 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4416 (double) it->selective)) /* iftc */
4417 {
4418 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4419 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4420 }
4421
4422 /* Position on the newline if that's what's requested. */
4423 if (on_newline_p && newline_found_p)
4424 {
4425 if (STRINGP (it->string))
4426 {
4427 if (IT_STRING_CHARPOS (*it) > 0)
4428 {
4429 --IT_STRING_CHARPOS (*it);
4430 --IT_STRING_BYTEPOS (*it);
4431 }
4432 }
4433 else if (IT_CHARPOS (*it) > BEGV)
4434 {
4435 --IT_CHARPOS (*it);
4436 --IT_BYTEPOS (*it);
4437 reseat (it, it->current.pos, 0);
4438 }
4439 }
4440 else if (skipped_p)
4441 reseat (it, it->current.pos, 0);
4442
4443 CHECK_IT (it);
4444 }
4445
4446
4447 \f
4448 /***********************************************************************
4449 Changing an iterator's position
4450 ***********************************************************************/
4451
4452 /* Change IT's current position to POS in current_buffer. If FORCE_P
4453 is non-zero, always check for text properties at the new position.
4454 Otherwise, text properties are only looked up if POS >=
4455 IT->check_charpos of a property. */
4456
4457 static void
4458 reseat (it, pos, force_p)
4459 struct it *it;
4460 struct text_pos pos;
4461 int force_p;
4462 {
4463 int original_pos = IT_CHARPOS (*it);
4464
4465 reseat_1 (it, pos, 0);
4466
4467 /* Determine where to check text properties. Avoid doing it
4468 where possible because text property lookup is very expensive. */
4469 if (force_p
4470 || CHARPOS (pos) > it->stop_charpos
4471 || CHARPOS (pos) < original_pos)
4472 handle_stop (it);
4473
4474 CHECK_IT (it);
4475 }
4476
4477
4478 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4479 IT->stop_pos to POS, also. */
4480
4481 static void
4482 reseat_1 (it, pos, set_stop_p)
4483 struct it *it;
4484 struct text_pos pos;
4485 int set_stop_p;
4486 {
4487 /* Don't call this function when scanning a C string. */
4488 xassert (it->s == NULL);
4489
4490 /* POS must be a reasonable value. */
4491 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4492
4493 it->current.pos = it->position = pos;
4494 XSETBUFFER (it->object, current_buffer);
4495 it->end_charpos = ZV;
4496 it->dpvec = NULL;
4497 it->current.dpvec_index = -1;
4498 it->current.overlay_string_index = -1;
4499 IT_STRING_CHARPOS (*it) = -1;
4500 IT_STRING_BYTEPOS (*it) = -1;
4501 it->string = Qnil;
4502 it->method = next_element_from_buffer;
4503 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4504 it->sp = 0;
4505 it->face_before_selective_p = 0;
4506
4507 if (set_stop_p)
4508 it->stop_charpos = CHARPOS (pos);
4509 }
4510
4511
4512 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4513 If S is non-null, it is a C string to iterate over. Otherwise,
4514 STRING gives a Lisp string to iterate over.
4515
4516 If PRECISION > 0, don't return more then PRECISION number of
4517 characters from the string.
4518
4519 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4520 characters have been returned. FIELD_WIDTH < 0 means an infinite
4521 field width.
4522
4523 MULTIBYTE = 0 means disable processing of multibyte characters,
4524 MULTIBYTE > 0 means enable it,
4525 MULTIBYTE < 0 means use IT->multibyte_p.
4526
4527 IT must be initialized via a prior call to init_iterator before
4528 calling this function. */
4529
4530 static void
4531 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4532 struct it *it;
4533 unsigned char *s;
4534 Lisp_Object string;
4535 int charpos;
4536 int precision, field_width, multibyte;
4537 {
4538 /* No region in strings. */
4539 it->region_beg_charpos = it->region_end_charpos = -1;
4540
4541 /* No text property checks performed by default, but see below. */
4542 it->stop_charpos = -1;
4543
4544 /* Set iterator position and end position. */
4545 bzero (&it->current, sizeof it->current);
4546 it->current.overlay_string_index = -1;
4547 it->current.dpvec_index = -1;
4548 xassert (charpos >= 0);
4549
4550 /* If STRING is specified, use its multibyteness, otherwise use the
4551 setting of MULTIBYTE, if specified. */
4552 if (multibyte >= 0)
4553 it->multibyte_p = multibyte > 0;
4554
4555 if (s == NULL)
4556 {
4557 xassert (STRINGP (string));
4558 it->string = string;
4559 it->s = NULL;
4560 it->end_charpos = it->string_nchars = SCHARS (string);
4561 it->method = next_element_from_string;
4562 it->current.string_pos = string_pos (charpos, string);
4563 }
4564 else
4565 {
4566 it->s = s;
4567 it->string = Qnil;
4568
4569 /* Note that we use IT->current.pos, not it->current.string_pos,
4570 for displaying C strings. */
4571 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4572 if (it->multibyte_p)
4573 {
4574 it->current.pos = c_string_pos (charpos, s, 1);
4575 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4576 }
4577 else
4578 {
4579 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4580 it->end_charpos = it->string_nchars = strlen (s);
4581 }
4582
4583 it->method = next_element_from_c_string;
4584 }
4585
4586 /* PRECISION > 0 means don't return more than PRECISION characters
4587 from the string. */
4588 if (precision > 0 && it->end_charpos - charpos > precision)
4589 it->end_charpos = it->string_nchars = charpos + precision;
4590
4591 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4592 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4593 FIELD_WIDTH < 0 means infinite field width. This is useful for
4594 padding with `-' at the end of a mode line. */
4595 if (field_width < 0)
4596 field_width = INFINITY;
4597 if (field_width > it->end_charpos - charpos)
4598 it->end_charpos = charpos + field_width;
4599
4600 /* Use the standard display table for displaying strings. */
4601 if (DISP_TABLE_P (Vstandard_display_table))
4602 it->dp = XCHAR_TABLE (Vstandard_display_table);
4603
4604 it->stop_charpos = charpos;
4605 CHECK_IT (it);
4606 }
4607
4608
4609 \f
4610 /***********************************************************************
4611 Iteration
4612 ***********************************************************************/
4613
4614 /* Load IT's display element fields with information about the next
4615 display element from the current position of IT. Value is zero if
4616 end of buffer (or C string) is reached. */
4617
4618 int
4619 get_next_display_element (it)
4620 struct it *it;
4621 {
4622 /* Non-zero means that we found a display element. Zero means that
4623 we hit the end of what we iterate over. Performance note: the
4624 function pointer `method' used here turns out to be faster than
4625 using a sequence of if-statements. */
4626 int success_p = (*it->method) (it);
4627
4628 if (it->what == IT_CHARACTER)
4629 {
4630 /* Map via display table or translate control characters.
4631 IT->c, IT->len etc. have been set to the next character by
4632 the function call above. If we have a display table, and it
4633 contains an entry for IT->c, translate it. Don't do this if
4634 IT->c itself comes from a display table, otherwise we could
4635 end up in an infinite recursion. (An alternative could be to
4636 count the recursion depth of this function and signal an
4637 error when a certain maximum depth is reached.) Is it worth
4638 it? */
4639 if (success_p && it->dpvec == NULL)
4640 {
4641 Lisp_Object dv;
4642
4643 if (it->dp
4644 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4645 VECTORP (dv)))
4646 {
4647 struct Lisp_Vector *v = XVECTOR (dv);
4648
4649 /* Return the first character from the display table
4650 entry, if not empty. If empty, don't display the
4651 current character. */
4652 if (v->size)
4653 {
4654 it->dpvec_char_len = it->len;
4655 it->dpvec = v->contents;
4656 it->dpend = v->contents + v->size;
4657 it->current.dpvec_index = 0;
4658 it->method = next_element_from_display_vector;
4659 success_p = get_next_display_element (it);
4660 }
4661 else
4662 {
4663 set_iterator_to_next (it, 0);
4664 success_p = get_next_display_element (it);
4665 }
4666 }
4667
4668 /* Translate control characters into `\003' or `^C' form.
4669 Control characters coming from a display table entry are
4670 currently not translated because we use IT->dpvec to hold
4671 the translation. This could easily be changed but I
4672 don't believe that it is worth doing.
4673
4674 If it->multibyte_p is nonzero, eight-bit characters and
4675 non-printable multibyte characters are also translated to
4676 octal form.
4677
4678 If it->multibyte_p is zero, eight-bit characters that
4679 don't have corresponding multibyte char code are also
4680 translated to octal form. */
4681 else if ((it->c < ' '
4682 && (it->area != TEXT_AREA
4683 || (it->c != '\n' && it->c != '\t')))
4684 || (it->multibyte_p
4685 ? ((it->c >= 127
4686 && it->len == 1)
4687 || !CHAR_PRINTABLE_P (it->c))
4688 : (it->c >= 127
4689 && it->c == unibyte_char_to_multibyte (it->c))))
4690 {
4691 /* IT->c is a control character which must be displayed
4692 either as '\003' or as `^C' where the '\\' and '^'
4693 can be defined in the display table. Fill
4694 IT->ctl_chars with glyphs for what we have to
4695 display. Then, set IT->dpvec to these glyphs. */
4696 GLYPH g;
4697
4698 if (it->c < 128 && it->ctl_arrow_p)
4699 {
4700 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4701 if (it->dp
4702 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4703 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4704 g = XINT (DISP_CTRL_GLYPH (it->dp));
4705 else
4706 g = FAST_MAKE_GLYPH ('^', 0);
4707 XSETINT (it->ctl_chars[0], g);
4708
4709 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4710 XSETINT (it->ctl_chars[1], g);
4711
4712 /* Set up IT->dpvec and return first character from it. */
4713 it->dpvec_char_len = it->len;
4714 it->dpvec = it->ctl_chars;
4715 it->dpend = it->dpvec + 2;
4716 it->current.dpvec_index = 0;
4717 it->method = next_element_from_display_vector;
4718 get_next_display_element (it);
4719 }
4720 else
4721 {
4722 unsigned char str[MAX_MULTIBYTE_LENGTH];
4723 int len;
4724 int i;
4725 GLYPH escape_glyph;
4726
4727 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4728 if (it->dp
4729 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4730 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4731 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4732 else
4733 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4734
4735 if (SINGLE_BYTE_CHAR_P (it->c))
4736 str[0] = it->c, len = 1;
4737 else
4738 {
4739 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4740 if (len < 0)
4741 {
4742 /* It's an invalid character, which
4743 shouldn't happen actually, but due to
4744 bugs it may happen. Let's print the char
4745 as is, there's not much meaningful we can
4746 do with it. */
4747 str[0] = it->c;
4748 str[1] = it->c >> 8;
4749 str[2] = it->c >> 16;
4750 str[3] = it->c >> 24;
4751 len = 4;
4752 }
4753 }
4754
4755 for (i = 0; i < len; i++)
4756 {
4757 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4758 /* Insert three more glyphs into IT->ctl_chars for
4759 the octal display of the character. */
4760 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4761 XSETINT (it->ctl_chars[i * 4 + 1], g);
4762 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4763 XSETINT (it->ctl_chars[i * 4 + 2], g);
4764 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4765 XSETINT (it->ctl_chars[i * 4 + 3], g);
4766 }
4767
4768 /* Set up IT->dpvec and return the first character
4769 from it. */
4770 it->dpvec_char_len = it->len;
4771 it->dpvec = it->ctl_chars;
4772 it->dpend = it->dpvec + len * 4;
4773 it->current.dpvec_index = 0;
4774 it->method = next_element_from_display_vector;
4775 get_next_display_element (it);
4776 }
4777 }
4778 }
4779
4780 /* Adjust face id for a multibyte character. There are no
4781 multibyte character in unibyte text. */
4782 if (it->multibyte_p
4783 && success_p
4784 && FRAME_WINDOW_P (it->f))
4785 {
4786 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4787 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4788 }
4789 }
4790
4791 /* Is this character the last one of a run of characters with
4792 box? If yes, set IT->end_of_box_run_p to 1. */
4793 if (it->face_box_p
4794 && it->s == NULL)
4795 {
4796 int face_id;
4797 struct face *face;
4798
4799 it->end_of_box_run_p
4800 = ((face_id = face_after_it_pos (it),
4801 face_id != it->face_id)
4802 && (face = FACE_FROM_ID (it->f, face_id),
4803 face->box == FACE_NO_BOX));
4804 }
4805
4806 /* Value is 0 if end of buffer or string reached. */
4807 return success_p;
4808 }
4809
4810
4811 /* Move IT to the next display element.
4812
4813 RESEAT_P non-zero means if called on a newline in buffer text,
4814 skip to the next visible line start.
4815
4816 Functions get_next_display_element and set_iterator_to_next are
4817 separate because I find this arrangement easier to handle than a
4818 get_next_display_element function that also increments IT's
4819 position. The way it is we can first look at an iterator's current
4820 display element, decide whether it fits on a line, and if it does,
4821 increment the iterator position. The other way around we probably
4822 would either need a flag indicating whether the iterator has to be
4823 incremented the next time, or we would have to implement a
4824 decrement position function which would not be easy to write. */
4825
4826 void
4827 set_iterator_to_next (it, reseat_p)
4828 struct it *it;
4829 int reseat_p;
4830 {
4831 /* Reset flags indicating start and end of a sequence of characters
4832 with box. Reset them at the start of this function because
4833 moving the iterator to a new position might set them. */
4834 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4835
4836 if (it->method == next_element_from_buffer)
4837 {
4838 /* The current display element of IT is a character from
4839 current_buffer. Advance in the buffer, and maybe skip over
4840 invisible lines that are so because of selective display. */
4841 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4842 reseat_at_next_visible_line_start (it, 0);
4843 else
4844 {
4845 xassert (it->len != 0);
4846 IT_BYTEPOS (*it) += it->len;
4847 IT_CHARPOS (*it) += 1;
4848 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4849 }
4850 }
4851 else if (it->method == next_element_from_composition)
4852 {
4853 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4854 if (STRINGP (it->string))
4855 {
4856 IT_STRING_BYTEPOS (*it) += it->len;
4857 IT_STRING_CHARPOS (*it) += it->cmp_len;
4858 it->method = next_element_from_string;
4859 goto consider_string_end;
4860 }
4861 else
4862 {
4863 IT_BYTEPOS (*it) += it->len;
4864 IT_CHARPOS (*it) += it->cmp_len;
4865 it->method = next_element_from_buffer;
4866 }
4867 }
4868 else if (it->method == next_element_from_c_string)
4869 {
4870 /* Current display element of IT is from a C string. */
4871 IT_BYTEPOS (*it) += it->len;
4872 IT_CHARPOS (*it) += 1;
4873 }
4874 else if (it->method == next_element_from_display_vector)
4875 {
4876 /* Current display element of IT is from a display table entry.
4877 Advance in the display table definition. Reset it to null if
4878 end reached, and continue with characters from buffers/
4879 strings. */
4880 ++it->current.dpvec_index;
4881
4882 /* Restore face of the iterator to what they were before the
4883 display vector entry (these entries may contain faces). */
4884 it->face_id = it->saved_face_id;
4885
4886 if (it->dpvec + it->current.dpvec_index == it->dpend)
4887 {
4888 if (it->s)
4889 it->method = next_element_from_c_string;
4890 else if (STRINGP (it->string))
4891 it->method = next_element_from_string;
4892 else
4893 it->method = next_element_from_buffer;
4894
4895 it->dpvec = NULL;
4896 it->current.dpvec_index = -1;
4897
4898 /* Skip over characters which were displayed via IT->dpvec. */
4899 if (it->dpvec_char_len < 0)
4900 reseat_at_next_visible_line_start (it, 1);
4901 else if (it->dpvec_char_len > 0)
4902 {
4903 it->len = it->dpvec_char_len;
4904 set_iterator_to_next (it, reseat_p);
4905 }
4906 }
4907 }
4908 else if (it->method == next_element_from_string)
4909 {
4910 /* Current display element is a character from a Lisp string. */
4911 xassert (it->s == NULL && STRINGP (it->string));
4912 IT_STRING_BYTEPOS (*it) += it->len;
4913 IT_STRING_CHARPOS (*it) += 1;
4914
4915 consider_string_end:
4916
4917 if (it->current.overlay_string_index >= 0)
4918 {
4919 /* IT->string is an overlay string. Advance to the
4920 next, if there is one. */
4921 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
4922 next_overlay_string (it);
4923 }
4924 else
4925 {
4926 /* IT->string is not an overlay string. If we reached
4927 its end, and there is something on IT->stack, proceed
4928 with what is on the stack. This can be either another
4929 string, this time an overlay string, or a buffer. */
4930 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
4931 && it->sp > 0)
4932 {
4933 pop_it (it);
4934 if (!STRINGP (it->string))
4935 it->method = next_element_from_buffer;
4936 else
4937 goto consider_string_end;
4938 }
4939 }
4940 }
4941 else if (it->method == next_element_from_image
4942 || it->method == next_element_from_stretch)
4943 {
4944 /* The position etc with which we have to proceed are on
4945 the stack. The position may be at the end of a string,
4946 if the `display' property takes up the whole string. */
4947 pop_it (it);
4948 it->image_id = 0;
4949 if (STRINGP (it->string))
4950 {
4951 it->method = next_element_from_string;
4952 goto consider_string_end;
4953 }
4954 else
4955 it->method = next_element_from_buffer;
4956 }
4957 else
4958 /* There are no other methods defined, so this should be a bug. */
4959 abort ();
4960
4961 xassert (it->method != next_element_from_string
4962 || (STRINGP (it->string)
4963 && IT_STRING_CHARPOS (*it) >= 0));
4964 }
4965
4966
4967 /* Load IT's display element fields with information about the next
4968 display element which comes from a display table entry or from the
4969 result of translating a control character to one of the forms `^C'
4970 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4971
4972 static int
4973 next_element_from_display_vector (it)
4974 struct it *it;
4975 {
4976 /* Precondition. */
4977 xassert (it->dpvec && it->current.dpvec_index >= 0);
4978
4979 /* Remember the current face id in case glyphs specify faces.
4980 IT's face is restored in set_iterator_to_next. */
4981 it->saved_face_id = it->face_id;
4982
4983 if (INTEGERP (*it->dpvec)
4984 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4985 {
4986 int lface_id;
4987 GLYPH g;
4988
4989 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4990 it->c = FAST_GLYPH_CHAR (g);
4991 it->len = CHAR_BYTES (it->c);
4992
4993 /* The entry may contain a face id to use. Such a face id is
4994 the id of a Lisp face, not a realized face. A face id of
4995 zero means no face is specified. */
4996 lface_id = FAST_GLYPH_FACE (g);
4997 if (lface_id)
4998 {
4999 /* The function returns -1 if lface_id is invalid. */
5000 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
5001 if (face_id >= 0)
5002 it->face_id = face_id;
5003 }
5004 }
5005 else
5006 /* Display table entry is invalid. Return a space. */
5007 it->c = ' ', it->len = 1;
5008
5009 /* Don't change position and object of the iterator here. They are
5010 still the values of the character that had this display table
5011 entry or was translated, and that's what we want. */
5012 it->what = IT_CHARACTER;
5013 return 1;
5014 }
5015
5016
5017 /* Load IT with the next display element from Lisp string IT->string.
5018 IT->current.string_pos is the current position within the string.
5019 If IT->current.overlay_string_index >= 0, the Lisp string is an
5020 overlay string. */
5021
5022 static int
5023 next_element_from_string (it)
5024 struct it *it;
5025 {
5026 struct text_pos position;
5027
5028 xassert (STRINGP (it->string));
5029 xassert (IT_STRING_CHARPOS (*it) >= 0);
5030 position = it->current.string_pos;
5031
5032 /* Time to check for invisible text? */
5033 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5034 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5035 {
5036 handle_stop (it);
5037
5038 /* Since a handler may have changed IT->method, we must
5039 recurse here. */
5040 return get_next_display_element (it);
5041 }
5042
5043 if (it->current.overlay_string_index >= 0)
5044 {
5045 /* Get the next character from an overlay string. In overlay
5046 strings, There is no field width or padding with spaces to
5047 do. */
5048 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5049 {
5050 it->what = IT_EOB;
5051 return 0;
5052 }
5053 else if (STRING_MULTIBYTE (it->string))
5054 {
5055 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5056 const unsigned char *s = (SDATA (it->string)
5057 + IT_STRING_BYTEPOS (*it));
5058 it->c = string_char_and_length (s, remaining, &it->len);
5059 }
5060 else
5061 {
5062 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5063 it->len = 1;
5064 }
5065 }
5066 else
5067 {
5068 /* Get the next character from a Lisp string that is not an
5069 overlay string. Such strings come from the mode line, for
5070 example. We may have to pad with spaces, or truncate the
5071 string. See also next_element_from_c_string. */
5072 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5073 {
5074 it->what = IT_EOB;
5075 return 0;
5076 }
5077 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5078 {
5079 /* Pad with spaces. */
5080 it->c = ' ', it->len = 1;
5081 CHARPOS (position) = BYTEPOS (position) = -1;
5082 }
5083 else if (STRING_MULTIBYTE (it->string))
5084 {
5085 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5086 const unsigned char *s = (SDATA (it->string)
5087 + IT_STRING_BYTEPOS (*it));
5088 it->c = string_char_and_length (s, maxlen, &it->len);
5089 }
5090 else
5091 {
5092 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5093 it->len = 1;
5094 }
5095 }
5096
5097 /* Record what we have and where it came from. Note that we store a
5098 buffer position in IT->position although it could arguably be a
5099 string position. */
5100 it->what = IT_CHARACTER;
5101 it->object = it->string;
5102 it->position = position;
5103 return 1;
5104 }
5105
5106
5107 /* Load IT with next display element from C string IT->s.
5108 IT->string_nchars is the maximum number of characters to return
5109 from the string. IT->end_charpos may be greater than
5110 IT->string_nchars when this function is called, in which case we
5111 may have to return padding spaces. Value is zero if end of string
5112 reached, including padding spaces. */
5113
5114 static int
5115 next_element_from_c_string (it)
5116 struct it *it;
5117 {
5118 int success_p = 1;
5119
5120 xassert (it->s);
5121 it->what = IT_CHARACTER;
5122 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5123 it->object = Qnil;
5124
5125 /* IT's position can be greater IT->string_nchars in case a field
5126 width or precision has been specified when the iterator was
5127 initialized. */
5128 if (IT_CHARPOS (*it) >= it->end_charpos)
5129 {
5130 /* End of the game. */
5131 it->what = IT_EOB;
5132 success_p = 0;
5133 }
5134 else if (IT_CHARPOS (*it) >= it->string_nchars)
5135 {
5136 /* Pad with spaces. */
5137 it->c = ' ', it->len = 1;
5138 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5139 }
5140 else if (it->multibyte_p)
5141 {
5142 /* Implementation note: The calls to strlen apparently aren't a
5143 performance problem because there is no noticeable performance
5144 difference between Emacs running in unibyte or multibyte mode. */
5145 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5146 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5147 maxlen, &it->len);
5148 }
5149 else
5150 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5151
5152 return success_p;
5153 }
5154
5155
5156 /* Set up IT to return characters from an ellipsis, if appropriate.
5157 The definition of the ellipsis glyphs may come from a display table
5158 entry. This function Fills IT with the first glyph from the
5159 ellipsis if an ellipsis is to be displayed. */
5160
5161 static int
5162 next_element_from_ellipsis (it)
5163 struct it *it;
5164 {
5165 if (it->selective_display_ellipsis_p)
5166 {
5167 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
5168 {
5169 /* Use the display table definition for `...'. Invalid glyphs
5170 will be handled by the method returning elements from dpvec. */
5171 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
5172 it->dpvec_char_len = it->len;
5173 it->dpvec = v->contents;
5174 it->dpend = v->contents + v->size;
5175 it->current.dpvec_index = 0;
5176 it->method = next_element_from_display_vector;
5177 }
5178 else
5179 {
5180 /* Use default `...' which is stored in default_invis_vector. */
5181 it->dpvec_char_len = it->len;
5182 it->dpvec = default_invis_vector;
5183 it->dpend = default_invis_vector + 3;
5184 it->current.dpvec_index = 0;
5185 it->method = next_element_from_display_vector;
5186 }
5187 }
5188 else
5189 {
5190 /* The face at the current position may be different from the
5191 face we find after the invisible text. Remember what it
5192 was in IT->saved_face_id, and signal that it's there by
5193 setting face_before_selective_p. */
5194 it->saved_face_id = it->face_id;
5195 it->method = next_element_from_buffer;
5196 reseat_at_next_visible_line_start (it, 1);
5197 it->face_before_selective_p = 1;
5198 }
5199
5200 return get_next_display_element (it);
5201 }
5202
5203
5204 /* Deliver an image display element. The iterator IT is already
5205 filled with image information (done in handle_display_prop). Value
5206 is always 1. */
5207
5208
5209 static int
5210 next_element_from_image (it)
5211 struct it *it;
5212 {
5213 it->what = IT_IMAGE;
5214 return 1;
5215 }
5216
5217
5218 /* Fill iterator IT with next display element from a stretch glyph
5219 property. IT->object is the value of the text property. Value is
5220 always 1. */
5221
5222 static int
5223 next_element_from_stretch (it)
5224 struct it *it;
5225 {
5226 it->what = IT_STRETCH;
5227 return 1;
5228 }
5229
5230
5231 /* Load IT with the next display element from current_buffer. Value
5232 is zero if end of buffer reached. IT->stop_charpos is the next
5233 position at which to stop and check for text properties or buffer
5234 end. */
5235
5236 static int
5237 next_element_from_buffer (it)
5238 struct it *it;
5239 {
5240 int success_p = 1;
5241
5242 /* Check this assumption, otherwise, we would never enter the
5243 if-statement, below. */
5244 xassert (IT_CHARPOS (*it) >= BEGV
5245 && IT_CHARPOS (*it) <= it->stop_charpos);
5246
5247 if (IT_CHARPOS (*it) >= it->stop_charpos)
5248 {
5249 if (IT_CHARPOS (*it) >= it->end_charpos)
5250 {
5251 int overlay_strings_follow_p;
5252
5253 /* End of the game, except when overlay strings follow that
5254 haven't been returned yet. */
5255 if (it->overlay_strings_at_end_processed_p)
5256 overlay_strings_follow_p = 0;
5257 else
5258 {
5259 it->overlay_strings_at_end_processed_p = 1;
5260 overlay_strings_follow_p = get_overlay_strings (it, 0);
5261 }
5262
5263 if (overlay_strings_follow_p)
5264 success_p = get_next_display_element (it);
5265 else
5266 {
5267 it->what = IT_EOB;
5268 it->position = it->current.pos;
5269 success_p = 0;
5270 }
5271 }
5272 else
5273 {
5274 handle_stop (it);
5275 return get_next_display_element (it);
5276 }
5277 }
5278 else
5279 {
5280 /* No face changes, overlays etc. in sight, so just return a
5281 character from current_buffer. */
5282 unsigned char *p;
5283
5284 /* Maybe run the redisplay end trigger hook. Performance note:
5285 This doesn't seem to cost measurable time. */
5286 if (it->redisplay_end_trigger_charpos
5287 && it->glyph_row
5288 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5289 run_redisplay_end_trigger_hook (it);
5290
5291 /* Get the next character, maybe multibyte. */
5292 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5293 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5294 {
5295 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5296 - IT_BYTEPOS (*it));
5297 it->c = string_char_and_length (p, maxlen, &it->len);
5298 }
5299 else
5300 it->c = *p, it->len = 1;
5301
5302 /* Record what we have and where it came from. */
5303 it->what = IT_CHARACTER;;
5304 it->object = it->w->buffer;
5305 it->position = it->current.pos;
5306
5307 /* Normally we return the character found above, except when we
5308 really want to return an ellipsis for selective display. */
5309 if (it->selective)
5310 {
5311 if (it->c == '\n')
5312 {
5313 /* A value of selective > 0 means hide lines indented more
5314 than that number of columns. */
5315 if (it->selective > 0
5316 && IT_CHARPOS (*it) + 1 < ZV
5317 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5318 IT_BYTEPOS (*it) + 1,
5319 (double) it->selective)) /* iftc */
5320 {
5321 success_p = next_element_from_ellipsis (it);
5322 it->dpvec_char_len = -1;
5323 }
5324 }
5325 else if (it->c == '\r' && it->selective == -1)
5326 {
5327 /* A value of selective == -1 means that everything from the
5328 CR to the end of the line is invisible, with maybe an
5329 ellipsis displayed for it. */
5330 success_p = next_element_from_ellipsis (it);
5331 it->dpvec_char_len = -1;
5332 }
5333 }
5334 }
5335
5336 /* Value is zero if end of buffer reached. */
5337 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5338 return success_p;
5339 }
5340
5341
5342 /* Run the redisplay end trigger hook for IT. */
5343
5344 static void
5345 run_redisplay_end_trigger_hook (it)
5346 struct it *it;
5347 {
5348 Lisp_Object args[3];
5349
5350 /* IT->glyph_row should be non-null, i.e. we should be actually
5351 displaying something, or otherwise we should not run the hook. */
5352 xassert (it->glyph_row);
5353
5354 /* Set up hook arguments. */
5355 args[0] = Qredisplay_end_trigger_functions;
5356 args[1] = it->window;
5357 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5358 it->redisplay_end_trigger_charpos = 0;
5359
5360 /* Since we are *trying* to run these functions, don't try to run
5361 them again, even if they get an error. */
5362 it->w->redisplay_end_trigger = Qnil;
5363 Frun_hook_with_args (3, args);
5364
5365 /* Notice if it changed the face of the character we are on. */
5366 handle_face_prop (it);
5367 }
5368
5369
5370 /* Deliver a composition display element. The iterator IT is already
5371 filled with composition information (done in
5372 handle_composition_prop). Value is always 1. */
5373
5374 static int
5375 next_element_from_composition (it)
5376 struct it *it;
5377 {
5378 it->what = IT_COMPOSITION;
5379 it->position = (STRINGP (it->string)
5380 ? it->current.string_pos
5381 : it->current.pos);
5382 return 1;
5383 }
5384
5385
5386 \f
5387 /***********************************************************************
5388 Moving an iterator without producing glyphs
5389 ***********************************************************************/
5390
5391 /* Move iterator IT to a specified buffer or X position within one
5392 line on the display without producing glyphs.
5393
5394 OP should be a bit mask including some or all of these bits:
5395 MOVE_TO_X: Stop on reaching x-position TO_X.
5396 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5397 Regardless of OP's value, stop in reaching the end of the display line.
5398
5399 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5400 This means, in particular, that TO_X includes window's horizontal
5401 scroll amount.
5402
5403 The return value has several possible values that
5404 say what condition caused the scan to stop:
5405
5406 MOVE_POS_MATCH_OR_ZV
5407 - when TO_POS or ZV was reached.
5408
5409 MOVE_X_REACHED
5410 -when TO_X was reached before TO_POS or ZV were reached.
5411
5412 MOVE_LINE_CONTINUED
5413 - when we reached the end of the display area and the line must
5414 be continued.
5415
5416 MOVE_LINE_TRUNCATED
5417 - when we reached the end of the display area and the line is
5418 truncated.
5419
5420 MOVE_NEWLINE_OR_CR
5421 - when we stopped at a line end, i.e. a newline or a CR and selective
5422 display is on. */
5423
5424 static enum move_it_result
5425 move_it_in_display_line_to (it, to_charpos, to_x, op)
5426 struct it *it;
5427 int to_charpos, to_x, op;
5428 {
5429 enum move_it_result result = MOVE_UNDEFINED;
5430 struct glyph_row *saved_glyph_row;
5431
5432 /* Don't produce glyphs in produce_glyphs. */
5433 saved_glyph_row = it->glyph_row;
5434 it->glyph_row = NULL;
5435
5436 while (1)
5437 {
5438 int x, i, ascent = 0, descent = 0;
5439
5440 /* Stop when ZV or TO_CHARPOS reached. */
5441 if (!get_next_display_element (it)
5442 || ((op & MOVE_TO_POS) != 0
5443 && BUFFERP (it->object)
5444 && IT_CHARPOS (*it) >= to_charpos))
5445 {
5446 result = MOVE_POS_MATCH_OR_ZV;
5447 break;
5448 }
5449
5450 /* The call to produce_glyphs will get the metrics of the
5451 display element IT is loaded with. We record in x the
5452 x-position before this display element in case it does not
5453 fit on the line. */
5454 x = it->current_x;
5455
5456 /* Remember the line height so far in case the next element doesn't
5457 fit on the line. */
5458 if (!it->truncate_lines_p)
5459 {
5460 ascent = it->max_ascent;
5461 descent = it->max_descent;
5462 }
5463
5464 PRODUCE_GLYPHS (it);
5465
5466 if (it->area != TEXT_AREA)
5467 {
5468 set_iterator_to_next (it, 1);
5469 continue;
5470 }
5471
5472 /* The number of glyphs we get back in IT->nglyphs will normally
5473 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5474 character on a terminal frame, or (iii) a line end. For the
5475 second case, IT->nglyphs - 1 padding glyphs will be present
5476 (on X frames, there is only one glyph produced for a
5477 composite character.
5478
5479 The behavior implemented below means, for continuation lines,
5480 that as many spaces of a TAB as fit on the current line are
5481 displayed there. For terminal frames, as many glyphs of a
5482 multi-glyph character are displayed in the current line, too.
5483 This is what the old redisplay code did, and we keep it that
5484 way. Under X, the whole shape of a complex character must
5485 fit on the line or it will be completely displayed in the
5486 next line.
5487
5488 Note that both for tabs and padding glyphs, all glyphs have
5489 the same width. */
5490 if (it->nglyphs)
5491 {
5492 /* More than one glyph or glyph doesn't fit on line. All
5493 glyphs have the same width. */
5494 int single_glyph_width = it->pixel_width / it->nglyphs;
5495 int new_x;
5496
5497 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5498 {
5499 new_x = x + single_glyph_width;
5500
5501 /* We want to leave anything reaching TO_X to the caller. */
5502 if ((op & MOVE_TO_X) && new_x > to_x)
5503 {
5504 it->current_x = x;
5505 result = MOVE_X_REACHED;
5506 break;
5507 }
5508 else if (/* Lines are continued. */
5509 !it->truncate_lines_p
5510 && (/* And glyph doesn't fit on the line. */
5511 new_x > it->last_visible_x
5512 /* Or it fits exactly and we're on a window
5513 system frame. */
5514 || (new_x == it->last_visible_x
5515 && FRAME_WINDOW_P (it->f))))
5516 {
5517 if (/* IT->hpos == 0 means the very first glyph
5518 doesn't fit on the line, e.g. a wide image. */
5519 it->hpos == 0
5520 || (new_x == it->last_visible_x
5521 && FRAME_WINDOW_P (it->f)))
5522 {
5523 ++it->hpos;
5524 it->current_x = new_x;
5525 if (i == it->nglyphs - 1)
5526 set_iterator_to_next (it, 1);
5527 }
5528 else
5529 {
5530 it->current_x = x;
5531 it->max_ascent = ascent;
5532 it->max_descent = descent;
5533 }
5534
5535 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5536 IT_CHARPOS (*it)));
5537 result = MOVE_LINE_CONTINUED;
5538 break;
5539 }
5540 else if (new_x > it->first_visible_x)
5541 {
5542 /* Glyph is visible. Increment number of glyphs that
5543 would be displayed. */
5544 ++it->hpos;
5545 }
5546 else
5547 {
5548 /* Glyph is completely off the left margin of the display
5549 area. Nothing to do. */
5550 }
5551 }
5552
5553 if (result != MOVE_UNDEFINED)
5554 break;
5555 }
5556 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5557 {
5558 /* Stop when TO_X specified and reached. This check is
5559 necessary here because of lines consisting of a line end,
5560 only. The line end will not produce any glyphs and we
5561 would never get MOVE_X_REACHED. */
5562 xassert (it->nglyphs == 0);
5563 result = MOVE_X_REACHED;
5564 break;
5565 }
5566
5567 /* Is this a line end? If yes, we're done. */
5568 if (ITERATOR_AT_END_OF_LINE_P (it))
5569 {
5570 result = MOVE_NEWLINE_OR_CR;
5571 break;
5572 }
5573
5574 /* The current display element has been consumed. Advance
5575 to the next. */
5576 set_iterator_to_next (it, 1);
5577
5578 /* Stop if lines are truncated and IT's current x-position is
5579 past the right edge of the window now. */
5580 if (it->truncate_lines_p
5581 && it->current_x >= it->last_visible_x)
5582 {
5583 result = MOVE_LINE_TRUNCATED;
5584 break;
5585 }
5586 }
5587
5588 /* Restore the iterator settings altered at the beginning of this
5589 function. */
5590 it->glyph_row = saved_glyph_row;
5591 return result;
5592 }
5593
5594
5595 /* Move IT forward until it satisfies one or more of the criteria in
5596 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5597
5598 OP is a bit-mask that specifies where to stop, and in particular,
5599 which of those four position arguments makes a difference. See the
5600 description of enum move_operation_enum.
5601
5602 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5603 screen line, this function will set IT to the next position >
5604 TO_CHARPOS. */
5605
5606 void
5607 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5608 struct it *it;
5609 int to_charpos, to_x, to_y, to_vpos;
5610 int op;
5611 {
5612 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5613 int line_height;
5614 int reached = 0;
5615
5616 for (;;)
5617 {
5618 if (op & MOVE_TO_VPOS)
5619 {
5620 /* If no TO_CHARPOS and no TO_X specified, stop at the
5621 start of the line TO_VPOS. */
5622 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5623 {
5624 if (it->vpos == to_vpos)
5625 {
5626 reached = 1;
5627 break;
5628 }
5629 else
5630 skip = move_it_in_display_line_to (it, -1, -1, 0);
5631 }
5632 else
5633 {
5634 /* TO_VPOS >= 0 means stop at TO_X in the line at
5635 TO_VPOS, or at TO_POS, whichever comes first. */
5636 if (it->vpos == to_vpos)
5637 {
5638 reached = 2;
5639 break;
5640 }
5641
5642 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5643
5644 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5645 {
5646 reached = 3;
5647 break;
5648 }
5649 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5650 {
5651 /* We have reached TO_X but not in the line we want. */
5652 skip = move_it_in_display_line_to (it, to_charpos,
5653 -1, MOVE_TO_POS);
5654 if (skip == MOVE_POS_MATCH_OR_ZV)
5655 {
5656 reached = 4;
5657 break;
5658 }
5659 }
5660 }
5661 }
5662 else if (op & MOVE_TO_Y)
5663 {
5664 struct it it_backup;
5665
5666 /* TO_Y specified means stop at TO_X in the line containing
5667 TO_Y---or at TO_CHARPOS if this is reached first. The
5668 problem is that we can't really tell whether the line
5669 contains TO_Y before we have completely scanned it, and
5670 this may skip past TO_X. What we do is to first scan to
5671 TO_X.
5672
5673 If TO_X is not specified, use a TO_X of zero. The reason
5674 is to make the outcome of this function more predictable.
5675 If we didn't use TO_X == 0, we would stop at the end of
5676 the line which is probably not what a caller would expect
5677 to happen. */
5678 skip = move_it_in_display_line_to (it, to_charpos,
5679 ((op & MOVE_TO_X)
5680 ? to_x : 0),
5681 (MOVE_TO_X
5682 | (op & MOVE_TO_POS)));
5683
5684 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5685 if (skip == MOVE_POS_MATCH_OR_ZV)
5686 {
5687 reached = 5;
5688 break;
5689 }
5690
5691 /* If TO_X was reached, we would like to know whether TO_Y
5692 is in the line. This can only be said if we know the
5693 total line height which requires us to scan the rest of
5694 the line. */
5695 if (skip == MOVE_X_REACHED)
5696 {
5697 it_backup = *it;
5698 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5699 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5700 op & MOVE_TO_POS);
5701 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5702 }
5703
5704 /* Now, decide whether TO_Y is in this line. */
5705 line_height = it->max_ascent + it->max_descent;
5706 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5707
5708 if (to_y >= it->current_y
5709 && to_y < it->current_y + line_height)
5710 {
5711 if (skip == MOVE_X_REACHED)
5712 /* If TO_Y is in this line and TO_X was reached above,
5713 we scanned too far. We have to restore IT's settings
5714 to the ones before skipping. */
5715 *it = it_backup;
5716 reached = 6;
5717 }
5718 else if (skip == MOVE_X_REACHED)
5719 {
5720 skip = skip2;
5721 if (skip == MOVE_POS_MATCH_OR_ZV)
5722 reached = 7;
5723 }
5724
5725 if (reached)
5726 break;
5727 }
5728 else
5729 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5730
5731 switch (skip)
5732 {
5733 case MOVE_POS_MATCH_OR_ZV:
5734 reached = 8;
5735 goto out;
5736
5737 case MOVE_NEWLINE_OR_CR:
5738 set_iterator_to_next (it, 1);
5739 it->continuation_lines_width = 0;
5740 break;
5741
5742 case MOVE_LINE_TRUNCATED:
5743 it->continuation_lines_width = 0;
5744 reseat_at_next_visible_line_start (it, 0);
5745 if ((op & MOVE_TO_POS) != 0
5746 && IT_CHARPOS (*it) > to_charpos)
5747 {
5748 reached = 9;
5749 goto out;
5750 }
5751 break;
5752
5753 case MOVE_LINE_CONTINUED:
5754 it->continuation_lines_width += it->current_x;
5755 break;
5756
5757 default:
5758 abort ();
5759 }
5760
5761 /* Reset/increment for the next run. */
5762 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5763 it->current_x = it->hpos = 0;
5764 it->current_y += it->max_ascent + it->max_descent;
5765 ++it->vpos;
5766 last_height = it->max_ascent + it->max_descent;
5767 last_max_ascent = it->max_ascent;
5768 it->max_ascent = it->max_descent = 0;
5769 }
5770
5771 out:
5772
5773 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5774 }
5775
5776
5777 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5778
5779 If DY > 0, move IT backward at least that many pixels. DY = 0
5780 means move IT backward to the preceding line start or BEGV. This
5781 function may move over more than DY pixels if IT->current_y - DY
5782 ends up in the middle of a line; in this case IT->current_y will be
5783 set to the top of the line moved to. */
5784
5785 void
5786 move_it_vertically_backward (it, dy)
5787 struct it *it;
5788 int dy;
5789 {
5790 int nlines, h;
5791 struct it it2, it3;
5792 int start_pos = IT_CHARPOS (*it);
5793
5794 xassert (dy >= 0);
5795
5796 /* Estimate how many newlines we must move back. */
5797 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5798
5799 /* Set the iterator's position that many lines back. */
5800 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5801 back_to_previous_visible_line_start (it);
5802
5803 /* Reseat the iterator here. When moving backward, we don't want
5804 reseat to skip forward over invisible text, set up the iterator
5805 to deliver from overlay strings at the new position etc. So,
5806 use reseat_1 here. */
5807 reseat_1 (it, it->current.pos, 1);
5808
5809 /* We are now surely at a line start. */
5810 it->current_x = it->hpos = 0;
5811 it->continuation_lines_width = 0;
5812
5813 /* Move forward and see what y-distance we moved. First move to the
5814 start of the next line so that we get its height. We need this
5815 height to be able to tell whether we reached the specified
5816 y-distance. */
5817 it2 = *it;
5818 it2.max_ascent = it2.max_descent = 0;
5819 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5820 MOVE_TO_POS | MOVE_TO_VPOS);
5821 xassert (IT_CHARPOS (*it) >= BEGV);
5822 it3 = it2;
5823
5824 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5825 xassert (IT_CHARPOS (*it) >= BEGV);
5826 h = it2.current_y - it->current_y;
5827 nlines = it2.vpos - it->vpos;
5828
5829 /* Correct IT's y and vpos position. */
5830 it->vpos -= nlines;
5831 it->current_y -= h;
5832
5833 if (dy == 0)
5834 {
5835 /* DY == 0 means move to the start of the screen line. The
5836 value of nlines is > 0 if continuation lines were involved. */
5837 if (nlines > 0)
5838 move_it_by_lines (it, nlines, 1);
5839 xassert (IT_CHARPOS (*it) <= start_pos);
5840 }
5841 else if (nlines)
5842 {
5843 /* The y-position we try to reach. Note that h has been
5844 subtracted in front of the if-statement. */
5845 int target_y = it->current_y + h - dy;
5846 int y0 = it3.current_y;
5847 int y1 = line_bottom_y (&it3);
5848 int line_height = y1 - y0;
5849
5850 /* If we did not reach target_y, try to move further backward if
5851 we can. If we moved too far backward, try to move forward. */
5852 if (target_y < it->current_y
5853 /* This is heuristic. In a window that's 3 lines high, with
5854 a line height of 13 pixels each, recentering with point
5855 on the bottom line will try to move -39/2 = 19 pixels
5856 backward. Try to avoid moving into the first line. */
5857 && it->current_y - target_y > line_height / 3 * 2
5858 && IT_CHARPOS (*it) > BEGV)
5859 {
5860 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
5861 target_y - it->current_y));
5862 move_it_vertically (it, target_y - it->current_y);
5863 xassert (IT_CHARPOS (*it) >= BEGV);
5864 }
5865 else if (target_y >= it->current_y + line_height
5866 && IT_CHARPOS (*it) < ZV)
5867 {
5868 /* Should move forward by at least one line, maybe more.
5869
5870 Note: Calling move_it_by_lines can be expensive on
5871 terminal frames, where compute_motion is used (via
5872 vmotion) to do the job, when there are very long lines
5873 and truncate-lines is nil. That's the reason for
5874 treating terminal frames specially here. */
5875
5876 if (!FRAME_WINDOW_P (it->f))
5877 move_it_vertically (it, target_y - (it->current_y + line_height));
5878 else
5879 {
5880 do
5881 {
5882 move_it_by_lines (it, 1, 1);
5883 }
5884 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
5885 }
5886
5887 xassert (IT_CHARPOS (*it) >= BEGV);
5888 }
5889 }
5890 }
5891
5892
5893 /* Move IT by a specified amount of pixel lines DY. DY negative means
5894 move backwards. DY = 0 means move to start of screen line. At the
5895 end, IT will be on the start of a screen line. */
5896
5897 void
5898 move_it_vertically (it, dy)
5899 struct it *it;
5900 int dy;
5901 {
5902 if (dy <= 0)
5903 move_it_vertically_backward (it, -dy);
5904 else if (dy > 0)
5905 {
5906 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5907 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5908 MOVE_TO_POS | MOVE_TO_Y);
5909 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5910
5911 /* If buffer ends in ZV without a newline, move to the start of
5912 the line to satisfy the post-condition. */
5913 if (IT_CHARPOS (*it) == ZV
5914 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5915 move_it_by_lines (it, 0, 0);
5916 }
5917 }
5918
5919
5920 /* Move iterator IT past the end of the text line it is in. */
5921
5922 void
5923 move_it_past_eol (it)
5924 struct it *it;
5925 {
5926 enum move_it_result rc;
5927
5928 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5929 if (rc == MOVE_NEWLINE_OR_CR)
5930 set_iterator_to_next (it, 0);
5931 }
5932
5933
5934 #if 0 /* Currently not used. */
5935
5936 /* Return non-zero if some text between buffer positions START_CHARPOS
5937 and END_CHARPOS is invisible. IT->window is the window for text
5938 property lookup. */
5939
5940 static int
5941 invisible_text_between_p (it, start_charpos, end_charpos)
5942 struct it *it;
5943 int start_charpos, end_charpos;
5944 {
5945 Lisp_Object prop, limit;
5946 int invisible_found_p;
5947
5948 xassert (it != NULL && start_charpos <= end_charpos);
5949
5950 /* Is text at START invisible? */
5951 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5952 it->window);
5953 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5954 invisible_found_p = 1;
5955 else
5956 {
5957 limit = Fnext_single_char_property_change (make_number (start_charpos),
5958 Qinvisible, Qnil,
5959 make_number (end_charpos));
5960 invisible_found_p = XFASTINT (limit) < end_charpos;
5961 }
5962
5963 return invisible_found_p;
5964 }
5965
5966 #endif /* 0 */
5967
5968
5969 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5970 negative means move up. DVPOS == 0 means move to the start of the
5971 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5972 NEED_Y_P is zero, IT->current_y will be left unchanged.
5973
5974 Further optimization ideas: If we would know that IT->f doesn't use
5975 a face with proportional font, we could be faster for
5976 truncate-lines nil. */
5977
5978 void
5979 move_it_by_lines (it, dvpos, need_y_p)
5980 struct it *it;
5981 int dvpos, need_y_p;
5982 {
5983 struct position pos;
5984
5985 if (!FRAME_WINDOW_P (it->f))
5986 {
5987 struct text_pos textpos;
5988
5989 /* We can use vmotion on frames without proportional fonts. */
5990 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5991 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5992 reseat (it, textpos, 1);
5993 it->vpos += pos.vpos;
5994 it->current_y += pos.vpos;
5995 }
5996 else if (dvpos == 0)
5997 {
5998 /* DVPOS == 0 means move to the start of the screen line. */
5999 move_it_vertically_backward (it, 0);
6000 xassert (it->current_x == 0 && it->hpos == 0);
6001 }
6002 else if (dvpos > 0)
6003 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6004 else
6005 {
6006 struct it it2;
6007 int start_charpos, i;
6008
6009 /* Start at the beginning of the screen line containing IT's
6010 position. */
6011 move_it_vertically_backward (it, 0);
6012
6013 /* Go back -DVPOS visible lines and reseat the iterator there. */
6014 start_charpos = IT_CHARPOS (*it);
6015 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6016 back_to_previous_visible_line_start (it);
6017 reseat (it, it->current.pos, 1);
6018 it->current_x = it->hpos = 0;
6019
6020 /* Above call may have moved too far if continuation lines
6021 are involved. Scan forward and see if it did. */
6022 it2 = *it;
6023 it2.vpos = it2.current_y = 0;
6024 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6025 it->vpos -= it2.vpos;
6026 it->current_y -= it2.current_y;
6027 it->current_x = it->hpos = 0;
6028
6029 /* If we moved too far, move IT some lines forward. */
6030 if (it2.vpos > -dvpos)
6031 {
6032 int delta = it2.vpos + dvpos;
6033 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6034 }
6035 }
6036 }
6037
6038 /* Return 1 if IT points into the middle of a display vector. */
6039
6040 int
6041 in_display_vector_p (it)
6042 struct it *it;
6043 {
6044 return (it->method == next_element_from_display_vector
6045 && it->current.dpvec_index > 0
6046 && it->dpvec + it->current.dpvec_index != it->dpend);
6047 }
6048
6049 \f
6050 /***********************************************************************
6051 Messages
6052 ***********************************************************************/
6053
6054
6055 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6056 to *Messages*. */
6057
6058 void
6059 add_to_log (format, arg1, arg2)
6060 char *format;
6061 Lisp_Object arg1, arg2;
6062 {
6063 Lisp_Object args[3];
6064 Lisp_Object msg, fmt;
6065 char *buffer;
6066 int len;
6067 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6068
6069 /* Do nothing if called asynchronously. Inserting text into
6070 a buffer may call after-change-functions and alike and
6071 that would means running Lisp asynchronously. */
6072 if (handling_signal)
6073 return;
6074
6075 fmt = msg = Qnil;
6076 GCPRO4 (fmt, msg, arg1, arg2);
6077
6078 args[0] = fmt = build_string (format);
6079 args[1] = arg1;
6080 args[2] = arg2;
6081 msg = Fformat (3, args);
6082
6083 len = SBYTES (msg) + 1;
6084 buffer = (char *) alloca (len);
6085 bcopy (SDATA (msg), buffer, len);
6086
6087 message_dolog (buffer, len - 1, 1, 0);
6088 UNGCPRO;
6089 }
6090
6091
6092 /* Output a newline in the *Messages* buffer if "needs" one. */
6093
6094 void
6095 message_log_maybe_newline ()
6096 {
6097 if (message_log_need_newline)
6098 message_dolog ("", 0, 1, 0);
6099 }
6100
6101
6102 /* Add a string M of length NBYTES to the message log, optionally
6103 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6104 nonzero, means interpret the contents of M as multibyte. This
6105 function calls low-level routines in order to bypass text property
6106 hooks, etc. which might not be safe to run. */
6107
6108 void
6109 message_dolog (m, nbytes, nlflag, multibyte)
6110 const char *m;
6111 int nbytes, nlflag, multibyte;
6112 {
6113 if (!NILP (Vmemory_full))
6114 return;
6115
6116 if (!NILP (Vmessage_log_max))
6117 {
6118 struct buffer *oldbuf;
6119 Lisp_Object oldpoint, oldbegv, oldzv;
6120 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6121 int point_at_end = 0;
6122 int zv_at_end = 0;
6123 Lisp_Object old_deactivate_mark, tem;
6124 struct gcpro gcpro1;
6125
6126 old_deactivate_mark = Vdeactivate_mark;
6127 oldbuf = current_buffer;
6128 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6129 current_buffer->undo_list = Qt;
6130
6131 oldpoint = message_dolog_marker1;
6132 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6133 oldbegv = message_dolog_marker2;
6134 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6135 oldzv = message_dolog_marker3;
6136 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6137 GCPRO1 (old_deactivate_mark);
6138
6139 if (PT == Z)
6140 point_at_end = 1;
6141 if (ZV == Z)
6142 zv_at_end = 1;
6143
6144 BEGV = BEG;
6145 BEGV_BYTE = BEG_BYTE;
6146 ZV = Z;
6147 ZV_BYTE = Z_BYTE;
6148 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6149
6150 /* Insert the string--maybe converting multibyte to single byte
6151 or vice versa, so that all the text fits the buffer. */
6152 if (multibyte
6153 && NILP (current_buffer->enable_multibyte_characters))
6154 {
6155 int i, c, char_bytes;
6156 unsigned char work[1];
6157
6158 /* Convert a multibyte string to single-byte
6159 for the *Message* buffer. */
6160 for (i = 0; i < nbytes; i += char_bytes)
6161 {
6162 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6163 work[0] = (SINGLE_BYTE_CHAR_P (c)
6164 ? c
6165 : multibyte_char_to_unibyte (c, Qnil));
6166 insert_1_both (work, 1, 1, 1, 0, 0);
6167 }
6168 }
6169 else if (! multibyte
6170 && ! NILP (current_buffer->enable_multibyte_characters))
6171 {
6172 int i, c, char_bytes;
6173 unsigned char *msg = (unsigned char *) m;
6174 unsigned char str[MAX_MULTIBYTE_LENGTH];
6175 /* Convert a single-byte string to multibyte
6176 for the *Message* buffer. */
6177 for (i = 0; i < nbytes; i++)
6178 {
6179 c = unibyte_char_to_multibyte (msg[i]);
6180 char_bytes = CHAR_STRING (c, str);
6181 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6182 }
6183 }
6184 else if (nbytes)
6185 insert_1 (m, nbytes, 1, 0, 0);
6186
6187 if (nlflag)
6188 {
6189 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6190 insert_1 ("\n", 1, 1, 0, 0);
6191
6192 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6193 this_bol = PT;
6194 this_bol_byte = PT_BYTE;
6195
6196 /* See if this line duplicates the previous one.
6197 If so, combine duplicates. */
6198 if (this_bol > BEG)
6199 {
6200 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6201 prev_bol = PT;
6202 prev_bol_byte = PT_BYTE;
6203
6204 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6205 this_bol, this_bol_byte);
6206 if (dup)
6207 {
6208 del_range_both (prev_bol, prev_bol_byte,
6209 this_bol, this_bol_byte, 0);
6210 if (dup > 1)
6211 {
6212 char dupstr[40];
6213 int duplen;
6214
6215 /* If you change this format, don't forget to also
6216 change message_log_check_duplicate. */
6217 sprintf (dupstr, " [%d times]", dup);
6218 duplen = strlen (dupstr);
6219 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6220 insert_1 (dupstr, duplen, 1, 0, 1);
6221 }
6222 }
6223 }
6224
6225 /* If we have more than the desired maximum number of lines
6226 in the *Messages* buffer now, delete the oldest ones.
6227 This is safe because we don't have undo in this buffer. */
6228
6229 if (NATNUMP (Vmessage_log_max))
6230 {
6231 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6232 -XFASTINT (Vmessage_log_max) - 1, 0);
6233 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6234 }
6235 }
6236 BEGV = XMARKER (oldbegv)->charpos;
6237 BEGV_BYTE = marker_byte_position (oldbegv);
6238
6239 if (zv_at_end)
6240 {
6241 ZV = Z;
6242 ZV_BYTE = Z_BYTE;
6243 }
6244 else
6245 {
6246 ZV = XMARKER (oldzv)->charpos;
6247 ZV_BYTE = marker_byte_position (oldzv);
6248 }
6249
6250 if (point_at_end)
6251 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6252 else
6253 /* We can't do Fgoto_char (oldpoint) because it will run some
6254 Lisp code. */
6255 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6256 XMARKER (oldpoint)->bytepos);
6257
6258 UNGCPRO;
6259 unchain_marker (oldpoint);
6260 unchain_marker (oldbegv);
6261 unchain_marker (oldzv);
6262
6263 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6264 set_buffer_internal (oldbuf);
6265 if (NILP (tem))
6266 windows_or_buffers_changed = old_windows_or_buffers_changed;
6267 message_log_need_newline = !nlflag;
6268 Vdeactivate_mark = old_deactivate_mark;
6269 }
6270 }
6271
6272
6273 /* We are at the end of the buffer after just having inserted a newline.
6274 (Note: We depend on the fact we won't be crossing the gap.)
6275 Check to see if the most recent message looks a lot like the previous one.
6276 Return 0 if different, 1 if the new one should just replace it, or a
6277 value N > 1 if we should also append " [N times]". */
6278
6279 static int
6280 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6281 int prev_bol, this_bol;
6282 int prev_bol_byte, this_bol_byte;
6283 {
6284 int i;
6285 int len = Z_BYTE - 1 - this_bol_byte;
6286 int seen_dots = 0;
6287 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6288 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6289
6290 for (i = 0; i < len; i++)
6291 {
6292 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6293 seen_dots = 1;
6294 if (p1[i] != p2[i])
6295 return seen_dots;
6296 }
6297 p1 += len;
6298 if (*p1 == '\n')
6299 return 2;
6300 if (*p1++ == ' ' && *p1++ == '[')
6301 {
6302 int n = 0;
6303 while (*p1 >= '0' && *p1 <= '9')
6304 n = n * 10 + *p1++ - '0';
6305 if (strncmp (p1, " times]\n", 8) == 0)
6306 return n+1;
6307 }
6308 return 0;
6309 }
6310
6311
6312 /* Display an echo area message M with a specified length of NBYTES
6313 bytes. The string may include null characters. If M is 0, clear
6314 out any existing message, and let the mini-buffer text show
6315 through.
6316
6317 The buffer M must continue to exist until after the echo area gets
6318 cleared or some other message gets displayed there. This means do
6319 not pass text that is stored in a Lisp string; do not pass text in
6320 a buffer that was alloca'd. */
6321
6322 void
6323 message2 (m, nbytes, multibyte)
6324 const char *m;
6325 int nbytes;
6326 int multibyte;
6327 {
6328 /* First flush out any partial line written with print. */
6329 message_log_maybe_newline ();
6330 if (m)
6331 message_dolog (m, nbytes, 1, multibyte);
6332 message2_nolog (m, nbytes, multibyte);
6333 }
6334
6335
6336 /* The non-logging counterpart of message2. */
6337
6338 void
6339 message2_nolog (m, nbytes, multibyte)
6340 const char *m;
6341 int nbytes, multibyte;
6342 {
6343 struct frame *sf = SELECTED_FRAME ();
6344 message_enable_multibyte = multibyte;
6345
6346 if (noninteractive)
6347 {
6348 if (noninteractive_need_newline)
6349 putc ('\n', stderr);
6350 noninteractive_need_newline = 0;
6351 if (m)
6352 fwrite (m, nbytes, 1, stderr);
6353 if (cursor_in_echo_area == 0)
6354 fprintf (stderr, "\n");
6355 fflush (stderr);
6356 }
6357 /* A null message buffer means that the frame hasn't really been
6358 initialized yet. Error messages get reported properly by
6359 cmd_error, so this must be just an informative message; toss it. */
6360 else if (INTERACTIVE
6361 && sf->glyphs_initialized_p
6362 && FRAME_MESSAGE_BUF (sf))
6363 {
6364 Lisp_Object mini_window;
6365 struct frame *f;
6366
6367 /* Get the frame containing the mini-buffer
6368 that the selected frame is using. */
6369 mini_window = FRAME_MINIBUF_WINDOW (sf);
6370 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6371
6372 FRAME_SAMPLE_VISIBILITY (f);
6373 if (FRAME_VISIBLE_P (sf)
6374 && ! FRAME_VISIBLE_P (f))
6375 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6376
6377 if (m)
6378 {
6379 set_message (m, Qnil, nbytes, multibyte);
6380 if (minibuffer_auto_raise)
6381 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6382 }
6383 else
6384 clear_message (1, 1);
6385
6386 do_pending_window_change (0);
6387 echo_area_display (1);
6388 do_pending_window_change (0);
6389 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6390 (*frame_up_to_date_hook) (f);
6391 }
6392 }
6393
6394
6395 /* Display an echo area message M with a specified length of NBYTES
6396 bytes. The string may include null characters. If M is not a
6397 string, clear out any existing message, and let the mini-buffer
6398 text show through. */
6399
6400 void
6401 message3 (m, nbytes, multibyte)
6402 Lisp_Object m;
6403 int nbytes;
6404 int multibyte;
6405 {
6406 struct gcpro gcpro1;
6407
6408 GCPRO1 (m);
6409
6410 /* First flush out any partial line written with print. */
6411 message_log_maybe_newline ();
6412 if (STRINGP (m))
6413 message_dolog (SDATA (m), nbytes, 1, multibyte);
6414 message3_nolog (m, nbytes, multibyte);
6415
6416 UNGCPRO;
6417 }
6418
6419
6420 /* The non-logging version of message3. */
6421
6422 void
6423 message3_nolog (m, nbytes, multibyte)
6424 Lisp_Object m;
6425 int nbytes, multibyte;
6426 {
6427 struct frame *sf = SELECTED_FRAME ();
6428 message_enable_multibyte = multibyte;
6429
6430 if (noninteractive)
6431 {
6432 if (noninteractive_need_newline)
6433 putc ('\n', stderr);
6434 noninteractive_need_newline = 0;
6435 if (STRINGP (m))
6436 fwrite (SDATA (m), nbytes, 1, stderr);
6437 if (cursor_in_echo_area == 0)
6438 fprintf (stderr, "\n");
6439 fflush (stderr);
6440 }
6441 /* A null message buffer means that the frame hasn't really been
6442 initialized yet. Error messages get reported properly by
6443 cmd_error, so this must be just an informative message; toss it. */
6444 else if (INTERACTIVE
6445 && sf->glyphs_initialized_p
6446 && FRAME_MESSAGE_BUF (sf))
6447 {
6448 Lisp_Object mini_window;
6449 Lisp_Object frame;
6450 struct frame *f;
6451
6452 /* Get the frame containing the mini-buffer
6453 that the selected frame is using. */
6454 mini_window = FRAME_MINIBUF_WINDOW (sf);
6455 frame = XWINDOW (mini_window)->frame;
6456 f = XFRAME (frame);
6457
6458 FRAME_SAMPLE_VISIBILITY (f);
6459 if (FRAME_VISIBLE_P (sf)
6460 && !FRAME_VISIBLE_P (f))
6461 Fmake_frame_visible (frame);
6462
6463 if (STRINGP (m) && SCHARS (m) > 0)
6464 {
6465 set_message (NULL, m, nbytes, multibyte);
6466 if (minibuffer_auto_raise)
6467 Fraise_frame (frame);
6468 }
6469 else
6470 clear_message (1, 1);
6471
6472 do_pending_window_change (0);
6473 echo_area_display (1);
6474 do_pending_window_change (0);
6475 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6476 (*frame_up_to_date_hook) (f);
6477 }
6478 }
6479
6480
6481 /* Display a null-terminated echo area message M. If M is 0, clear
6482 out any existing message, and let the mini-buffer text show through.
6483
6484 The buffer M must continue to exist until after the echo area gets
6485 cleared or some other message gets displayed there. Do not pass
6486 text that is stored in a Lisp string. Do not pass text in a buffer
6487 that was alloca'd. */
6488
6489 void
6490 message1 (m)
6491 char *m;
6492 {
6493 message2 (m, (m ? strlen (m) : 0), 0);
6494 }
6495
6496
6497 /* The non-logging counterpart of message1. */
6498
6499 void
6500 message1_nolog (m)
6501 char *m;
6502 {
6503 message2_nolog (m, (m ? strlen (m) : 0), 0);
6504 }
6505
6506 /* Display a message M which contains a single %s
6507 which gets replaced with STRING. */
6508
6509 void
6510 message_with_string (m, string, log)
6511 char *m;
6512 Lisp_Object string;
6513 int log;
6514 {
6515 CHECK_STRING (string);
6516
6517 if (noninteractive)
6518 {
6519 if (m)
6520 {
6521 if (noninteractive_need_newline)
6522 putc ('\n', stderr);
6523 noninteractive_need_newline = 0;
6524 fprintf (stderr, m, SDATA (string));
6525 if (cursor_in_echo_area == 0)
6526 fprintf (stderr, "\n");
6527 fflush (stderr);
6528 }
6529 }
6530 else if (INTERACTIVE)
6531 {
6532 /* The frame whose minibuffer we're going to display the message on.
6533 It may be larger than the selected frame, so we need
6534 to use its buffer, not the selected frame's buffer. */
6535 Lisp_Object mini_window;
6536 struct frame *f, *sf = SELECTED_FRAME ();
6537
6538 /* Get the frame containing the minibuffer
6539 that the selected frame is using. */
6540 mini_window = FRAME_MINIBUF_WINDOW (sf);
6541 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6542
6543 /* A null message buffer means that the frame hasn't really been
6544 initialized yet. Error messages get reported properly by
6545 cmd_error, so this must be just an informative message; toss it. */
6546 if (FRAME_MESSAGE_BUF (f))
6547 {
6548 Lisp_Object args[2], message;
6549 struct gcpro gcpro1, gcpro2;
6550
6551 args[0] = build_string (m);
6552 args[1] = message = string;
6553 GCPRO2 (args[0], message);
6554 gcpro1.nvars = 2;
6555
6556 message = Fformat (2, args);
6557
6558 if (log)
6559 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6560 else
6561 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6562
6563 UNGCPRO;
6564
6565 /* Print should start at the beginning of the message
6566 buffer next time. */
6567 message_buf_print = 0;
6568 }
6569 }
6570 }
6571
6572
6573 /* Dump an informative message to the minibuf. If M is 0, clear out
6574 any existing message, and let the mini-buffer text show through. */
6575
6576 /* VARARGS 1 */
6577 void
6578 message (m, a1, a2, a3)
6579 char *m;
6580 EMACS_INT a1, a2, a3;
6581 {
6582 if (noninteractive)
6583 {
6584 if (m)
6585 {
6586 if (noninteractive_need_newline)
6587 putc ('\n', stderr);
6588 noninteractive_need_newline = 0;
6589 fprintf (stderr, m, a1, a2, a3);
6590 if (cursor_in_echo_area == 0)
6591 fprintf (stderr, "\n");
6592 fflush (stderr);
6593 }
6594 }
6595 else if (INTERACTIVE)
6596 {
6597 /* The frame whose mini-buffer we're going to display the message
6598 on. It may be larger than the selected frame, so we need to
6599 use its buffer, not the selected frame's buffer. */
6600 Lisp_Object mini_window;
6601 struct frame *f, *sf = SELECTED_FRAME ();
6602
6603 /* Get the frame containing the mini-buffer
6604 that the selected frame is using. */
6605 mini_window = FRAME_MINIBUF_WINDOW (sf);
6606 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6607
6608 /* A null message buffer means that the frame hasn't really been
6609 initialized yet. Error messages get reported properly by
6610 cmd_error, so this must be just an informative message; toss
6611 it. */
6612 if (FRAME_MESSAGE_BUF (f))
6613 {
6614 if (m)
6615 {
6616 int len;
6617 #ifdef NO_ARG_ARRAY
6618 char *a[3];
6619 a[0] = (char *) a1;
6620 a[1] = (char *) a2;
6621 a[2] = (char *) a3;
6622
6623 len = doprnt (FRAME_MESSAGE_BUF (f),
6624 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6625 #else
6626 len = doprnt (FRAME_MESSAGE_BUF (f),
6627 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6628 (char **) &a1);
6629 #endif /* NO_ARG_ARRAY */
6630
6631 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6632 }
6633 else
6634 message1 (0);
6635
6636 /* Print should start at the beginning of the message
6637 buffer next time. */
6638 message_buf_print = 0;
6639 }
6640 }
6641 }
6642
6643
6644 /* The non-logging version of message. */
6645
6646 void
6647 message_nolog (m, a1, a2, a3)
6648 char *m;
6649 EMACS_INT a1, a2, a3;
6650 {
6651 Lisp_Object old_log_max;
6652 old_log_max = Vmessage_log_max;
6653 Vmessage_log_max = Qnil;
6654 message (m, a1, a2, a3);
6655 Vmessage_log_max = old_log_max;
6656 }
6657
6658
6659 /* Display the current message in the current mini-buffer. This is
6660 only called from error handlers in process.c, and is not time
6661 critical. */
6662
6663 void
6664 update_echo_area ()
6665 {
6666 if (!NILP (echo_area_buffer[0]))
6667 {
6668 Lisp_Object string;
6669 string = Fcurrent_message ();
6670 message3 (string, SBYTES (string),
6671 !NILP (current_buffer->enable_multibyte_characters));
6672 }
6673 }
6674
6675
6676 /* Make sure echo area buffers in `echo_buffers' are live.
6677 If they aren't, make new ones. */
6678
6679 static void
6680 ensure_echo_area_buffers ()
6681 {
6682 int i;
6683
6684 for (i = 0; i < 2; ++i)
6685 if (!BUFFERP (echo_buffer[i])
6686 || NILP (XBUFFER (echo_buffer[i])->name))
6687 {
6688 char name[30];
6689 Lisp_Object old_buffer;
6690 int j;
6691
6692 old_buffer = echo_buffer[i];
6693 sprintf (name, " *Echo Area %d*", i);
6694 echo_buffer[i] = Fget_buffer_create (build_string (name));
6695 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6696
6697 for (j = 0; j < 2; ++j)
6698 if (EQ (old_buffer, echo_area_buffer[j]))
6699 echo_area_buffer[j] = echo_buffer[i];
6700 }
6701 }
6702
6703
6704 /* Call FN with args A1..A4 with either the current or last displayed
6705 echo_area_buffer as current buffer.
6706
6707 WHICH zero means use the current message buffer
6708 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6709 from echo_buffer[] and clear it.
6710
6711 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6712 suitable buffer from echo_buffer[] and clear it.
6713
6714 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6715 that the current message becomes the last displayed one, make
6716 choose a suitable buffer for echo_area_buffer[0], and clear it.
6717
6718 Value is what FN returns. */
6719
6720 static int
6721 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6722 struct window *w;
6723 int which;
6724 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6725 EMACS_INT a1;
6726 Lisp_Object a2;
6727 EMACS_INT a3, a4;
6728 {
6729 Lisp_Object buffer;
6730 int this_one, the_other, clear_buffer_p, rc;
6731 int count = SPECPDL_INDEX ();
6732
6733 /* If buffers aren't live, make new ones. */
6734 ensure_echo_area_buffers ();
6735
6736 clear_buffer_p = 0;
6737
6738 if (which == 0)
6739 this_one = 0, the_other = 1;
6740 else if (which > 0)
6741 this_one = 1, the_other = 0;
6742 else
6743 {
6744 this_one = 0, the_other = 1;
6745 clear_buffer_p = 1;
6746
6747 /* We need a fresh one in case the current echo buffer equals
6748 the one containing the last displayed echo area message. */
6749 if (!NILP (echo_area_buffer[this_one])
6750 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6751 echo_area_buffer[this_one] = Qnil;
6752 }
6753
6754 /* Choose a suitable buffer from echo_buffer[] is we don't
6755 have one. */
6756 if (NILP (echo_area_buffer[this_one]))
6757 {
6758 echo_area_buffer[this_one]
6759 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6760 ? echo_buffer[the_other]
6761 : echo_buffer[this_one]);
6762 clear_buffer_p = 1;
6763 }
6764
6765 buffer = echo_area_buffer[this_one];
6766
6767 /* Don't get confused by reusing the buffer used for echoing
6768 for a different purpose. */
6769 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
6770 cancel_echoing ();
6771
6772 record_unwind_protect (unwind_with_echo_area_buffer,
6773 with_echo_area_buffer_unwind_data (w));
6774
6775 /* Make the echo area buffer current. Note that for display
6776 purposes, it is not necessary that the displayed window's buffer
6777 == current_buffer, except for text property lookup. So, let's
6778 only set that buffer temporarily here without doing a full
6779 Fset_window_buffer. We must also change w->pointm, though,
6780 because otherwise an assertions in unshow_buffer fails, and Emacs
6781 aborts. */
6782 set_buffer_internal_1 (XBUFFER (buffer));
6783 if (w)
6784 {
6785 w->buffer = buffer;
6786 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6787 }
6788
6789 current_buffer->undo_list = Qt;
6790 current_buffer->read_only = Qnil;
6791 specbind (Qinhibit_read_only, Qt);
6792 specbind (Qinhibit_modification_hooks, Qt);
6793
6794 if (clear_buffer_p && Z > BEG)
6795 del_range (BEG, Z);
6796
6797 xassert (BEGV >= BEG);
6798 xassert (ZV <= Z && ZV >= BEGV);
6799
6800 rc = fn (a1, a2, a3, a4);
6801
6802 xassert (BEGV >= BEG);
6803 xassert (ZV <= Z && ZV >= BEGV);
6804
6805 unbind_to (count, Qnil);
6806 return rc;
6807 }
6808
6809
6810 /* Save state that should be preserved around the call to the function
6811 FN called in with_echo_area_buffer. */
6812
6813 static Lisp_Object
6814 with_echo_area_buffer_unwind_data (w)
6815 struct window *w;
6816 {
6817 int i = 0;
6818 Lisp_Object vector;
6819
6820 /* Reduce consing by keeping one vector in
6821 Vwith_echo_area_save_vector. */
6822 vector = Vwith_echo_area_save_vector;
6823 Vwith_echo_area_save_vector = Qnil;
6824
6825 if (NILP (vector))
6826 vector = Fmake_vector (make_number (7), Qnil);
6827
6828 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6829 AREF (vector, i) = Vdeactivate_mark, ++i;
6830 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6831
6832 if (w)
6833 {
6834 XSETWINDOW (AREF (vector, i), w); ++i;
6835 AREF (vector, i) = w->buffer; ++i;
6836 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6837 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6838 }
6839 else
6840 {
6841 int end = i + 4;
6842 for (; i < end; ++i)
6843 AREF (vector, i) = Qnil;
6844 }
6845
6846 xassert (i == ASIZE (vector));
6847 return vector;
6848 }
6849
6850
6851 /* Restore global state from VECTOR which was created by
6852 with_echo_area_buffer_unwind_data. */
6853
6854 static Lisp_Object
6855 unwind_with_echo_area_buffer (vector)
6856 Lisp_Object vector;
6857 {
6858 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6859 Vdeactivate_mark = AREF (vector, 1);
6860 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6861
6862 if (WINDOWP (AREF (vector, 3)))
6863 {
6864 struct window *w;
6865 Lisp_Object buffer, charpos, bytepos;
6866
6867 w = XWINDOW (AREF (vector, 3));
6868 buffer = AREF (vector, 4);
6869 charpos = AREF (vector, 5);
6870 bytepos = AREF (vector, 6);
6871
6872 w->buffer = buffer;
6873 set_marker_both (w->pointm, buffer,
6874 XFASTINT (charpos), XFASTINT (bytepos));
6875 }
6876
6877 Vwith_echo_area_save_vector = vector;
6878 return Qnil;
6879 }
6880
6881
6882 /* Set up the echo area for use by print functions. MULTIBYTE_P
6883 non-zero means we will print multibyte. */
6884
6885 void
6886 setup_echo_area_for_printing (multibyte_p)
6887 int multibyte_p;
6888 {
6889 /* If we can't find an echo area any more, exit. */
6890 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
6891 Fkill_emacs (Qnil);
6892
6893 ensure_echo_area_buffers ();
6894
6895 if (!message_buf_print)
6896 {
6897 /* A message has been output since the last time we printed.
6898 Choose a fresh echo area buffer. */
6899 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6900 echo_area_buffer[0] = echo_buffer[1];
6901 else
6902 echo_area_buffer[0] = echo_buffer[0];
6903
6904 /* Switch to that buffer and clear it. */
6905 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6906 current_buffer->truncate_lines = Qnil;
6907
6908 if (Z > BEG)
6909 {
6910 int count = SPECPDL_INDEX ();
6911 specbind (Qinhibit_read_only, Qt);
6912 /* Note that undo recording is always disabled. */
6913 del_range (BEG, Z);
6914 unbind_to (count, Qnil);
6915 }
6916 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6917
6918 /* Set up the buffer for the multibyteness we need. */
6919 if (multibyte_p
6920 != !NILP (current_buffer->enable_multibyte_characters))
6921 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6922
6923 /* Raise the frame containing the echo area. */
6924 if (minibuffer_auto_raise)
6925 {
6926 struct frame *sf = SELECTED_FRAME ();
6927 Lisp_Object mini_window;
6928 mini_window = FRAME_MINIBUF_WINDOW (sf);
6929 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6930 }
6931
6932 message_log_maybe_newline ();
6933 message_buf_print = 1;
6934 }
6935 else
6936 {
6937 if (NILP (echo_area_buffer[0]))
6938 {
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
6945 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6946 {
6947 /* Someone switched buffers between print requests. */
6948 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6949 current_buffer->truncate_lines = Qnil;
6950 }
6951 }
6952 }
6953
6954
6955 /* Display an echo area message in window W. Value is non-zero if W's
6956 height is changed. If display_last_displayed_message_p is
6957 non-zero, display the message that was last displayed, otherwise
6958 display the current message. */
6959
6960 static int
6961 display_echo_area (w)
6962 struct window *w;
6963 {
6964 int i, no_message_p, window_height_changed_p, count;
6965
6966 /* Temporarily disable garbage collections while displaying the echo
6967 area. This is done because a GC can print a message itself.
6968 That message would modify the echo area buffer's contents while a
6969 redisplay of the buffer is going on, and seriously confuse
6970 redisplay. */
6971 count = inhibit_garbage_collection ();
6972
6973 /* If there is no message, we must call display_echo_area_1
6974 nevertheless because it resizes the window. But we will have to
6975 reset the echo_area_buffer in question to nil at the end because
6976 with_echo_area_buffer will sets it to an empty buffer. */
6977 i = display_last_displayed_message_p ? 1 : 0;
6978 no_message_p = NILP (echo_area_buffer[i]);
6979
6980 window_height_changed_p
6981 = with_echo_area_buffer (w, display_last_displayed_message_p,
6982 display_echo_area_1,
6983 (EMACS_INT) w, Qnil, 0, 0);
6984
6985 if (no_message_p)
6986 echo_area_buffer[i] = Qnil;
6987
6988 unbind_to (count, Qnil);
6989 return window_height_changed_p;
6990 }
6991
6992
6993 /* Helper for display_echo_area. Display the current buffer which
6994 contains the current echo area message in window W, a mini-window,
6995 a pointer to which is passed in A1. A2..A4 are currently not used.
6996 Change the height of W so that all of the message is displayed.
6997 Value is non-zero if height of W was changed. */
6998
6999 static int
7000 display_echo_area_1 (a1, a2, a3, a4)
7001 EMACS_INT a1;
7002 Lisp_Object a2;
7003 EMACS_INT a3, a4;
7004 {
7005 struct window *w = (struct window *) a1;
7006 Lisp_Object window;
7007 struct text_pos start;
7008 int window_height_changed_p = 0;
7009
7010 /* Do this before displaying, so that we have a large enough glyph
7011 matrix for the display. */
7012 window_height_changed_p = resize_mini_window (w, 0);
7013
7014 /* Display. */
7015 clear_glyph_matrix (w->desired_matrix);
7016 XSETWINDOW (window, w);
7017 SET_TEXT_POS (start, BEG, BEG_BYTE);
7018 try_window (window, start);
7019
7020 return window_height_changed_p;
7021 }
7022
7023
7024 /* Resize the echo area window to exactly the size needed for the
7025 currently displayed message, if there is one. If a mini-buffer
7026 is active, don't shrink it. */
7027
7028 void
7029 resize_echo_area_exactly ()
7030 {
7031 if (BUFFERP (echo_area_buffer[0])
7032 && WINDOWP (echo_area_window))
7033 {
7034 struct window *w = XWINDOW (echo_area_window);
7035 int resized_p;
7036 Lisp_Object resize_exactly;
7037
7038 if (minibuf_level == 0)
7039 resize_exactly = Qt;
7040 else
7041 resize_exactly = Qnil;
7042
7043 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7044 (EMACS_INT) w, resize_exactly, 0, 0);
7045 if (resized_p)
7046 {
7047 ++windows_or_buffers_changed;
7048 ++update_mode_lines;
7049 redisplay_internal (0);
7050 }
7051 }
7052 }
7053
7054
7055 /* Callback function for with_echo_area_buffer, when used from
7056 resize_echo_area_exactly. A1 contains a pointer to the window to
7057 resize, EXACTLY non-nil means resize the mini-window exactly to the
7058 size of the text displayed. A3 and A4 are not used. Value is what
7059 resize_mini_window returns. */
7060
7061 static int
7062 resize_mini_window_1 (a1, exactly, a3, a4)
7063 EMACS_INT a1;
7064 Lisp_Object exactly;
7065 EMACS_INT a3, a4;
7066 {
7067 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7068 }
7069
7070
7071 /* Resize mini-window W to fit the size of its contents. EXACT:P
7072 means size the window exactly to the size needed. Otherwise, it's
7073 only enlarged until W's buffer is empty. Value is non-zero if
7074 the window height has been changed. */
7075
7076 int
7077 resize_mini_window (w, exact_p)
7078 struct window *w;
7079 int exact_p;
7080 {
7081 struct frame *f = XFRAME (w->frame);
7082 int window_height_changed_p = 0;
7083
7084 xassert (MINI_WINDOW_P (w));
7085
7086 /* Don't resize windows while redisplaying a window; it would
7087 confuse redisplay functions when the size of the window they are
7088 displaying changes from under them. Such a resizing can happen,
7089 for instance, when which-func prints a long message while
7090 we are running fontification-functions. We're running these
7091 functions with safe_call which binds inhibit-redisplay to t. */
7092 if (!NILP (Vinhibit_redisplay))
7093 return 0;
7094
7095 /* Nil means don't try to resize. */
7096 if (NILP (Vresize_mini_windows)
7097 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7098 return 0;
7099
7100 if (!FRAME_MINIBUF_ONLY_P (f))
7101 {
7102 struct it it;
7103 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7104 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
7105 int height, max_height;
7106 int unit = CANON_Y_UNIT (f);
7107 struct text_pos start;
7108 struct buffer *old_current_buffer = NULL;
7109
7110 if (current_buffer != XBUFFER (w->buffer))
7111 {
7112 old_current_buffer = current_buffer;
7113 set_buffer_internal (XBUFFER (w->buffer));
7114 }
7115
7116 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7117
7118 /* Compute the max. number of lines specified by the user. */
7119 if (FLOATP (Vmax_mini_window_height))
7120 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_HEIGHT (f);
7121 else if (INTEGERP (Vmax_mini_window_height))
7122 max_height = XINT (Vmax_mini_window_height);
7123 else
7124 max_height = total_height / 4;
7125
7126 /* Correct that max. height if it's bogus. */
7127 max_height = max (1, max_height);
7128 max_height = min (total_height, max_height);
7129
7130 /* Find out the height of the text in the window. */
7131 if (it.truncate_lines_p)
7132 height = 1;
7133 else
7134 {
7135 last_height = 0;
7136 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7137 if (it.max_ascent == 0 && it.max_descent == 0)
7138 height = it.current_y + last_height;
7139 else
7140 height = it.current_y + it.max_ascent + it.max_descent;
7141 height -= it.extra_line_spacing;
7142 height = (height + unit - 1) / unit;
7143 }
7144
7145 /* Compute a suitable window start. */
7146 if (height > max_height)
7147 {
7148 height = max_height;
7149 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7150 move_it_vertically_backward (&it, (height - 1) * unit);
7151 start = it.current.pos;
7152 }
7153 else
7154 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7155 SET_MARKER_FROM_TEXT_POS (w->start, start);
7156
7157 if (EQ (Vresize_mini_windows, Qgrow_only))
7158 {
7159 /* Let it grow only, until we display an empty message, in which
7160 case the window shrinks again. */
7161 if (height > XFASTINT (w->height))
7162 {
7163 int old_height = XFASTINT (w->height);
7164 freeze_window_starts (f, 1);
7165 grow_mini_window (w, height - XFASTINT (w->height));
7166 window_height_changed_p = XFASTINT (w->height) != old_height;
7167 }
7168 else if (height < XFASTINT (w->height)
7169 && (exact_p || BEGV == ZV))
7170 {
7171 int old_height = XFASTINT (w->height);
7172 freeze_window_starts (f, 0);
7173 shrink_mini_window (w);
7174 window_height_changed_p = XFASTINT (w->height) != old_height;
7175 }
7176 }
7177 else
7178 {
7179 /* Always resize to exact size needed. */
7180 if (height > XFASTINT (w->height))
7181 {
7182 int old_height = XFASTINT (w->height);
7183 freeze_window_starts (f, 1);
7184 grow_mini_window (w, height - XFASTINT (w->height));
7185 window_height_changed_p = XFASTINT (w->height) != old_height;
7186 }
7187 else if (height < XFASTINT (w->height))
7188 {
7189 int old_height = XFASTINT (w->height);
7190 freeze_window_starts (f, 0);
7191 shrink_mini_window (w);
7192
7193 if (height)
7194 {
7195 freeze_window_starts (f, 1);
7196 grow_mini_window (w, height - XFASTINT (w->height));
7197 }
7198
7199 window_height_changed_p = XFASTINT (w->height) != old_height;
7200 }
7201 }
7202
7203 if (old_current_buffer)
7204 set_buffer_internal (old_current_buffer);
7205 }
7206
7207 return window_height_changed_p;
7208 }
7209
7210
7211 /* Value is the current message, a string, or nil if there is no
7212 current message. */
7213
7214 Lisp_Object
7215 current_message ()
7216 {
7217 Lisp_Object msg;
7218
7219 if (NILP (echo_area_buffer[0]))
7220 msg = Qnil;
7221 else
7222 {
7223 with_echo_area_buffer (0, 0, current_message_1,
7224 (EMACS_INT) &msg, Qnil, 0, 0);
7225 if (NILP (msg))
7226 echo_area_buffer[0] = Qnil;
7227 }
7228
7229 return msg;
7230 }
7231
7232
7233 static int
7234 current_message_1 (a1, a2, a3, a4)
7235 EMACS_INT a1;
7236 Lisp_Object a2;
7237 EMACS_INT a3, a4;
7238 {
7239 Lisp_Object *msg = (Lisp_Object *) a1;
7240
7241 if (Z > BEG)
7242 *msg = make_buffer_string (BEG, Z, 1);
7243 else
7244 *msg = Qnil;
7245 return 0;
7246 }
7247
7248
7249 /* Push the current message on Vmessage_stack for later restauration
7250 by restore_message. Value is non-zero if the current message isn't
7251 empty. This is a relatively infrequent operation, so it's not
7252 worth optimizing. */
7253
7254 int
7255 push_message ()
7256 {
7257 Lisp_Object msg;
7258 msg = current_message ();
7259 Vmessage_stack = Fcons (msg, Vmessage_stack);
7260 return STRINGP (msg);
7261 }
7262
7263
7264 /* Restore message display from the top of Vmessage_stack. */
7265
7266 void
7267 restore_message ()
7268 {
7269 Lisp_Object msg;
7270
7271 xassert (CONSP (Vmessage_stack));
7272 msg = XCAR (Vmessage_stack);
7273 if (STRINGP (msg))
7274 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7275 else
7276 message3_nolog (msg, 0, 0);
7277 }
7278
7279
7280 /* Handler for record_unwind_protect calling pop_message. */
7281
7282 Lisp_Object
7283 pop_message_unwind (dummy)
7284 Lisp_Object dummy;
7285 {
7286 pop_message ();
7287 return Qnil;
7288 }
7289
7290 /* Pop the top-most entry off Vmessage_stack. */
7291
7292 void
7293 pop_message ()
7294 {
7295 xassert (CONSP (Vmessage_stack));
7296 Vmessage_stack = XCDR (Vmessage_stack);
7297 }
7298
7299
7300 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7301 exits. If the stack is not empty, we have a missing pop_message
7302 somewhere. */
7303
7304 void
7305 check_message_stack ()
7306 {
7307 if (!NILP (Vmessage_stack))
7308 abort ();
7309 }
7310
7311
7312 /* Truncate to NCHARS what will be displayed in the echo area the next
7313 time we display it---but don't redisplay it now. */
7314
7315 void
7316 truncate_echo_area (nchars)
7317 int nchars;
7318 {
7319 if (nchars == 0)
7320 echo_area_buffer[0] = Qnil;
7321 /* A null message buffer means that the frame hasn't really been
7322 initialized yet. Error messages get reported properly by
7323 cmd_error, so this must be just an informative message; toss it. */
7324 else if (!noninteractive
7325 && INTERACTIVE
7326 && !NILP (echo_area_buffer[0]))
7327 {
7328 struct frame *sf = SELECTED_FRAME ();
7329 if (FRAME_MESSAGE_BUF (sf))
7330 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7331 }
7332 }
7333
7334
7335 /* Helper function for truncate_echo_area. Truncate the current
7336 message to at most NCHARS characters. */
7337
7338 static int
7339 truncate_message_1 (nchars, a2, a3, a4)
7340 EMACS_INT nchars;
7341 Lisp_Object a2;
7342 EMACS_INT a3, a4;
7343 {
7344 if (BEG + nchars < Z)
7345 del_range (BEG + nchars, Z);
7346 if (Z == BEG)
7347 echo_area_buffer[0] = Qnil;
7348 return 0;
7349 }
7350
7351
7352 /* Set the current message to a substring of S or STRING.
7353
7354 If STRING is a Lisp string, set the message to the first NBYTES
7355 bytes from STRING. NBYTES zero means use the whole string. If
7356 STRING is multibyte, the message will be displayed multibyte.
7357
7358 If S is not null, set the message to the first LEN bytes of S. LEN
7359 zero means use the whole string. MULTIBYTE_P non-zero means S is
7360 multibyte. Display the message multibyte in that case. */
7361
7362 void
7363 set_message (s, string, nbytes, multibyte_p)
7364 const char *s;
7365 Lisp_Object string;
7366 int nbytes, multibyte_p;
7367 {
7368 message_enable_multibyte
7369 = ((s && multibyte_p)
7370 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7371
7372 with_echo_area_buffer (0, -1, set_message_1,
7373 (EMACS_INT) s, string, nbytes, multibyte_p);
7374 message_buf_print = 0;
7375 help_echo_showing_p = 0;
7376 }
7377
7378
7379 /* Helper function for set_message. Arguments have the same meaning
7380 as there, with A1 corresponding to S and A2 corresponding to STRING
7381 This function is called with the echo area buffer being
7382 current. */
7383
7384 static int
7385 set_message_1 (a1, a2, nbytes, multibyte_p)
7386 EMACS_INT a1;
7387 Lisp_Object a2;
7388 EMACS_INT nbytes, multibyte_p;
7389 {
7390 const char *s = (const char *) a1;
7391 Lisp_Object string = a2;
7392
7393 xassert (BEG == Z);
7394
7395 /* Change multibyteness of the echo buffer appropriately. */
7396 if (message_enable_multibyte
7397 != !NILP (current_buffer->enable_multibyte_characters))
7398 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7399
7400 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7401
7402 /* Insert new message at BEG. */
7403 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7404
7405 if (STRINGP (string))
7406 {
7407 int nchars;
7408
7409 if (nbytes == 0)
7410 nbytes = SBYTES (string);
7411 nchars = string_byte_to_char (string, nbytes);
7412
7413 /* This function takes care of single/multibyte conversion. We
7414 just have to ensure that the echo area buffer has the right
7415 setting of enable_multibyte_characters. */
7416 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7417 }
7418 else if (s)
7419 {
7420 if (nbytes == 0)
7421 nbytes = strlen (s);
7422
7423 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7424 {
7425 /* Convert from multi-byte to single-byte. */
7426 int i, c, n;
7427 unsigned char work[1];
7428
7429 /* Convert a multibyte string to single-byte. */
7430 for (i = 0; i < nbytes; i += n)
7431 {
7432 c = string_char_and_length (s + i, nbytes - i, &n);
7433 work[0] = (SINGLE_BYTE_CHAR_P (c)
7434 ? c
7435 : multibyte_char_to_unibyte (c, Qnil));
7436 insert_1_both (work, 1, 1, 1, 0, 0);
7437 }
7438 }
7439 else if (!multibyte_p
7440 && !NILP (current_buffer->enable_multibyte_characters))
7441 {
7442 /* Convert from single-byte to multi-byte. */
7443 int i, c, n;
7444 const unsigned char *msg = (const unsigned char *) s;
7445 unsigned char str[MAX_MULTIBYTE_LENGTH];
7446
7447 /* Convert a single-byte string to multibyte. */
7448 for (i = 0; i < nbytes; i++)
7449 {
7450 c = unibyte_char_to_multibyte (msg[i]);
7451 n = CHAR_STRING (c, str);
7452 insert_1_both (str, 1, n, 1, 0, 0);
7453 }
7454 }
7455 else
7456 insert_1 (s, nbytes, 1, 0, 0);
7457 }
7458
7459 return 0;
7460 }
7461
7462
7463 /* Clear messages. CURRENT_P non-zero means clear the current
7464 message. LAST_DISPLAYED_P non-zero means clear the message
7465 last displayed. */
7466
7467 void
7468 clear_message (current_p, last_displayed_p)
7469 int current_p, last_displayed_p;
7470 {
7471 if (current_p)
7472 {
7473 echo_area_buffer[0] = Qnil;
7474 message_cleared_p = 1;
7475 }
7476
7477 if (last_displayed_p)
7478 echo_area_buffer[1] = Qnil;
7479
7480 message_buf_print = 0;
7481 }
7482
7483 /* Clear garbaged frames.
7484
7485 This function is used where the old redisplay called
7486 redraw_garbaged_frames which in turn called redraw_frame which in
7487 turn called clear_frame. The call to clear_frame was a source of
7488 flickering. I believe a clear_frame is not necessary. It should
7489 suffice in the new redisplay to invalidate all current matrices,
7490 and ensure a complete redisplay of all windows. */
7491
7492 static void
7493 clear_garbaged_frames ()
7494 {
7495 if (frame_garbaged)
7496 {
7497 Lisp_Object tail, frame;
7498 int changed_count = 0;
7499
7500 FOR_EACH_FRAME (tail, frame)
7501 {
7502 struct frame *f = XFRAME (frame);
7503
7504 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7505 {
7506 if (f->resized_p)
7507 Fredraw_frame (frame);
7508 clear_current_matrices (f);
7509 changed_count++;
7510 f->garbaged = 0;
7511 f->resized_p = 0;
7512 }
7513 }
7514
7515 frame_garbaged = 0;
7516 if (changed_count)
7517 ++windows_or_buffers_changed;
7518 }
7519 }
7520
7521
7522 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7523 is non-zero update selected_frame. Value is non-zero if the
7524 mini-windows height has been changed. */
7525
7526 static int
7527 echo_area_display (update_frame_p)
7528 int update_frame_p;
7529 {
7530 Lisp_Object mini_window;
7531 struct window *w;
7532 struct frame *f;
7533 int window_height_changed_p = 0;
7534 struct frame *sf = SELECTED_FRAME ();
7535
7536 mini_window = FRAME_MINIBUF_WINDOW (sf);
7537 w = XWINDOW (mini_window);
7538 f = XFRAME (WINDOW_FRAME (w));
7539
7540 /* Don't display if frame is invisible or not yet initialized. */
7541 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7542 return 0;
7543
7544 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7545 #ifndef MAC_OS8
7546 #ifdef HAVE_WINDOW_SYSTEM
7547 /* When Emacs starts, selected_frame may be a visible terminal
7548 frame, even if we run under a window system. If we let this
7549 through, a message would be displayed on the terminal. */
7550 if (EQ (selected_frame, Vterminal_frame)
7551 && !NILP (Vwindow_system))
7552 return 0;
7553 #endif /* HAVE_WINDOW_SYSTEM */
7554 #endif
7555
7556 /* Redraw garbaged frames. */
7557 if (frame_garbaged)
7558 clear_garbaged_frames ();
7559
7560 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7561 {
7562 echo_area_window = mini_window;
7563 window_height_changed_p = display_echo_area (w);
7564 w->must_be_updated_p = 1;
7565
7566 /* Update the display, unless called from redisplay_internal.
7567 Also don't update the screen during redisplay itself. The
7568 update will happen at the end of redisplay, and an update
7569 here could cause confusion. */
7570 if (update_frame_p && !redisplaying_p)
7571 {
7572 int n = 0;
7573
7574 /* If the display update has been interrupted by pending
7575 input, update mode lines in the frame. Due to the
7576 pending input, it might have been that redisplay hasn't
7577 been called, so that mode lines above the echo area are
7578 garbaged. This looks odd, so we prevent it here. */
7579 if (!display_completed)
7580 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7581
7582 if (window_height_changed_p
7583 /* Don't do this if Emacs is shutting down. Redisplay
7584 needs to run hooks. */
7585 && !NILP (Vrun_hooks))
7586 {
7587 /* Must update other windows. Likewise as in other
7588 cases, don't let this update be interrupted by
7589 pending input. */
7590 int count = SPECPDL_INDEX ();
7591 specbind (Qredisplay_dont_pause, Qt);
7592 windows_or_buffers_changed = 1;
7593 redisplay_internal (0);
7594 unbind_to (count, Qnil);
7595 }
7596 else if (FRAME_WINDOW_P (f) && n == 0)
7597 {
7598 /* Window configuration is the same as before.
7599 Can do with a display update of the echo area,
7600 unless we displayed some mode lines. */
7601 update_single_window (w, 1);
7602 rif->flush_display (f);
7603 }
7604 else
7605 update_frame (f, 1, 1);
7606
7607 /* If cursor is in the echo area, make sure that the next
7608 redisplay displays the minibuffer, so that the cursor will
7609 be replaced with what the minibuffer wants. */
7610 if (cursor_in_echo_area)
7611 ++windows_or_buffers_changed;
7612 }
7613 }
7614 else if (!EQ (mini_window, selected_window))
7615 windows_or_buffers_changed++;
7616
7617 /* Last displayed message is now the current message. */
7618 echo_area_buffer[1] = echo_area_buffer[0];
7619
7620 /* Prevent redisplay optimization in redisplay_internal by resetting
7621 this_line_start_pos. This is done because the mini-buffer now
7622 displays the message instead of its buffer text. */
7623 if (EQ (mini_window, selected_window))
7624 CHARPOS (this_line_start_pos) = 0;
7625
7626 return window_height_changed_p;
7627 }
7628
7629
7630 \f
7631 /***********************************************************************
7632 Frame Titles
7633 ***********************************************************************/
7634
7635
7636 /* The frame title buffering code is also used by Fformat_mode_line.
7637 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
7638
7639 /* A buffer for constructing frame titles in it; allocated from the
7640 heap in init_xdisp and resized as needed in store_frame_title_char. */
7641
7642 static char *frame_title_buf;
7643
7644 /* The buffer's end, and a current output position in it. */
7645
7646 static char *frame_title_buf_end;
7647 static char *frame_title_ptr;
7648
7649
7650 /* Store a single character C for the frame title in frame_title_buf.
7651 Re-allocate frame_title_buf if necessary. */
7652
7653 static void
7654 #ifdef PROTOTYPES
7655 store_frame_title_char (char c)
7656 #else
7657 store_frame_title_char (c)
7658 char c;
7659 #endif
7660 {
7661 /* If output position has reached the end of the allocated buffer,
7662 double the buffer's size. */
7663 if (frame_title_ptr == frame_title_buf_end)
7664 {
7665 int len = frame_title_ptr - frame_title_buf;
7666 int new_size = 2 * len * sizeof *frame_title_buf;
7667 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7668 frame_title_buf_end = frame_title_buf + new_size;
7669 frame_title_ptr = frame_title_buf + len;
7670 }
7671
7672 *frame_title_ptr++ = c;
7673 }
7674
7675
7676 /* Store part of a frame title in frame_title_buf, beginning at
7677 frame_title_ptr. STR is the string to store. Do not copy
7678 characters that yield more columns than PRECISION; PRECISION <= 0
7679 means copy the whole string. Pad with spaces until FIELD_WIDTH
7680 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7681 pad. Called from display_mode_element when it is used to build a
7682 frame title. */
7683
7684 static int
7685 store_frame_title (str, field_width, precision)
7686 const unsigned char *str;
7687 int field_width, precision;
7688 {
7689 int n = 0;
7690 int dummy, nbytes;
7691
7692 /* Copy at most PRECISION chars from STR. */
7693 nbytes = strlen (str);
7694 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7695 while (nbytes--)
7696 store_frame_title_char (*str++);
7697
7698 /* Fill up with spaces until FIELD_WIDTH reached. */
7699 while (field_width > 0
7700 && n < field_width)
7701 {
7702 store_frame_title_char (' ');
7703 ++n;
7704 }
7705
7706 return n;
7707 }
7708
7709 #ifdef HAVE_WINDOW_SYSTEM
7710
7711 /* Set the title of FRAME, if it has changed. The title format is
7712 Vicon_title_format if FRAME is iconified, otherwise it is
7713 frame_title_format. */
7714
7715 static void
7716 x_consider_frame_title (frame)
7717 Lisp_Object frame;
7718 {
7719 struct frame *f = XFRAME (frame);
7720
7721 if (FRAME_WINDOW_P (f)
7722 || FRAME_MINIBUF_ONLY_P (f)
7723 || f->explicit_name)
7724 {
7725 /* Do we have more than one visible frame on this X display? */
7726 Lisp_Object tail;
7727 Lisp_Object fmt;
7728 struct buffer *obuf;
7729 int len;
7730 struct it it;
7731
7732 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7733 {
7734 Lisp_Object other_frame = XCAR (tail);
7735 struct frame *tf = XFRAME (other_frame);
7736
7737 if (tf != f
7738 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7739 && !FRAME_MINIBUF_ONLY_P (tf)
7740 && !EQ (other_frame, tip_frame)
7741 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7742 break;
7743 }
7744
7745 /* Set global variable indicating that multiple frames exist. */
7746 multiple_frames = CONSP (tail);
7747
7748 /* Switch to the buffer of selected window of the frame. Set up
7749 frame_title_ptr so that display_mode_element will output into it;
7750 then display the title. */
7751 obuf = current_buffer;
7752 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
7753 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7754 frame_title_ptr = frame_title_buf;
7755 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7756 NULL, DEFAULT_FACE_ID);
7757 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
7758 len = frame_title_ptr - frame_title_buf;
7759 frame_title_ptr = NULL;
7760 set_buffer_internal_1 (obuf);
7761
7762 /* Set the title only if it's changed. This avoids consing in
7763 the common case where it hasn't. (If it turns out that we've
7764 already wasted too much time by walking through the list with
7765 display_mode_element, then we might need to optimize at a
7766 higher level than this.) */
7767 if (! STRINGP (f->name)
7768 || SBYTES (f->name) != len
7769 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
7770 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7771 }
7772 }
7773
7774 #endif /* not HAVE_WINDOW_SYSTEM */
7775
7776
7777
7778 \f
7779 /***********************************************************************
7780 Menu Bars
7781 ***********************************************************************/
7782
7783
7784 /* Prepare for redisplay by updating menu-bar item lists when
7785 appropriate. This can call eval. */
7786
7787 void
7788 prepare_menu_bars ()
7789 {
7790 int all_windows;
7791 struct gcpro gcpro1, gcpro2;
7792 struct frame *f;
7793 Lisp_Object tooltip_frame;
7794
7795 #ifdef HAVE_WINDOW_SYSTEM
7796 tooltip_frame = tip_frame;
7797 #else
7798 tooltip_frame = Qnil;
7799 #endif
7800
7801 /* Update all frame titles based on their buffer names, etc. We do
7802 this before the menu bars so that the buffer-menu will show the
7803 up-to-date frame titles. */
7804 #ifdef HAVE_WINDOW_SYSTEM
7805 if (windows_or_buffers_changed || update_mode_lines)
7806 {
7807 Lisp_Object tail, frame;
7808
7809 FOR_EACH_FRAME (tail, frame)
7810 {
7811 f = XFRAME (frame);
7812 if (!EQ (frame, tooltip_frame)
7813 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7814 x_consider_frame_title (frame);
7815 }
7816 }
7817 #endif /* HAVE_WINDOW_SYSTEM */
7818
7819 /* Update the menu bar item lists, if appropriate. This has to be
7820 done before any actual redisplay or generation of display lines. */
7821 all_windows = (update_mode_lines
7822 || buffer_shared > 1
7823 || windows_or_buffers_changed);
7824 if (all_windows)
7825 {
7826 Lisp_Object tail, frame;
7827 int count = SPECPDL_INDEX ();
7828
7829 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7830
7831 FOR_EACH_FRAME (tail, frame)
7832 {
7833 f = XFRAME (frame);
7834
7835 /* Ignore tooltip frame. */
7836 if (EQ (frame, tooltip_frame))
7837 continue;
7838
7839 /* If a window on this frame changed size, report that to
7840 the user and clear the size-change flag. */
7841 if (FRAME_WINDOW_SIZES_CHANGED (f))
7842 {
7843 Lisp_Object functions;
7844
7845 /* Clear flag first in case we get an error below. */
7846 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7847 functions = Vwindow_size_change_functions;
7848 GCPRO2 (tail, functions);
7849
7850 while (CONSP (functions))
7851 {
7852 call1 (XCAR (functions), frame);
7853 functions = XCDR (functions);
7854 }
7855 UNGCPRO;
7856 }
7857
7858 GCPRO1 (tail);
7859 update_menu_bar (f, 0);
7860 #ifdef HAVE_WINDOW_SYSTEM
7861 update_tool_bar (f, 0);
7862 #endif
7863 UNGCPRO;
7864 }
7865
7866 unbind_to (count, Qnil);
7867 }
7868 else
7869 {
7870 struct frame *sf = SELECTED_FRAME ();
7871 update_menu_bar (sf, 1);
7872 #ifdef HAVE_WINDOW_SYSTEM
7873 update_tool_bar (sf, 1);
7874 #endif
7875 }
7876
7877 /* Motif needs this. See comment in xmenu.c. Turn it off when
7878 pending_menu_activation is not defined. */
7879 #ifdef USE_X_TOOLKIT
7880 pending_menu_activation = 0;
7881 #endif
7882 }
7883
7884
7885 /* Update the menu bar item list for frame F. This has to be done
7886 before we start to fill in any display lines, because it can call
7887 eval.
7888
7889 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7890
7891 static void
7892 update_menu_bar (f, save_match_data)
7893 struct frame *f;
7894 int save_match_data;
7895 {
7896 Lisp_Object window;
7897 register struct window *w;
7898
7899 /* If called recursively during a menu update, do nothing. This can
7900 happen when, for instance, an activate-menubar-hook causes a
7901 redisplay. */
7902 if (inhibit_menubar_update)
7903 return;
7904
7905 window = FRAME_SELECTED_WINDOW (f);
7906 w = XWINDOW (window);
7907
7908 #if 0 /* The if statement below this if statement used to include the
7909 condition !NILP (w->update_mode_line), rather than using
7910 update_mode_lines directly, and this if statement may have
7911 been added to make that condition work. Now the if
7912 statement below matches its comment, this isn't needed. */
7913 if (update_mode_lines)
7914 w->update_mode_line = Qt;
7915 #endif
7916
7917 if (FRAME_WINDOW_P (f)
7918 ?
7919 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
7920 || defined (USE_GTK)
7921 FRAME_EXTERNAL_MENU_BAR (f)
7922 #else
7923 FRAME_MENU_BAR_LINES (f) > 0
7924 #endif
7925 : FRAME_MENU_BAR_LINES (f) > 0)
7926 {
7927 /* If the user has switched buffers or windows, we need to
7928 recompute to reflect the new bindings. But we'll
7929 recompute when update_mode_lines is set too; that means
7930 that people can use force-mode-line-update to request
7931 that the menu bar be recomputed. The adverse effect on
7932 the rest of the redisplay algorithm is about the same as
7933 windows_or_buffers_changed anyway. */
7934 if (windows_or_buffers_changed
7935 /* This used to test w->update_mode_line, but we believe
7936 there is no need to recompute the menu in that case. */
7937 || update_mode_lines
7938 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7939 < BUF_MODIFF (XBUFFER (w->buffer)))
7940 != !NILP (w->last_had_star))
7941 || ((!NILP (Vtransient_mark_mode)
7942 && !NILP (XBUFFER (w->buffer)->mark_active))
7943 != !NILP (w->region_showing)))
7944 {
7945 struct buffer *prev = current_buffer;
7946 int count = SPECPDL_INDEX ();
7947
7948 specbind (Qinhibit_menubar_update, Qt);
7949
7950 set_buffer_internal_1 (XBUFFER (w->buffer));
7951 if (save_match_data)
7952 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7953 if (NILP (Voverriding_local_map_menu_flag))
7954 {
7955 specbind (Qoverriding_terminal_local_map, Qnil);
7956 specbind (Qoverriding_local_map, Qnil);
7957 }
7958
7959 /* Run the Lucid hook. */
7960 safe_run_hooks (Qactivate_menubar_hook);
7961
7962 /* If it has changed current-menubar from previous value,
7963 really recompute the menu-bar from the value. */
7964 if (! NILP (Vlucid_menu_bar_dirty_flag))
7965 call0 (Qrecompute_lucid_menubar);
7966
7967 safe_run_hooks (Qmenu_bar_update_hook);
7968 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7969
7970 /* Redisplay the menu bar in case we changed it. */
7971 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
7972 || defined (USE_GTK)
7973 if (FRAME_WINDOW_P (f)
7974 #if defined (MAC_OS)
7975 /* All frames on Mac OS share the same menubar. So only the
7976 selected frame should be allowed to set it. */
7977 && f == SELECTED_FRAME ()
7978 #endif
7979 )
7980 set_frame_menubar (f, 0, 0);
7981 else
7982 /* On a terminal screen, the menu bar is an ordinary screen
7983 line, and this makes it get updated. */
7984 w->update_mode_line = Qt;
7985 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
7986 /* In the non-toolkit version, the menu bar is an ordinary screen
7987 line, and this makes it get updated. */
7988 w->update_mode_line = Qt;
7989 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
7990
7991 unbind_to (count, Qnil);
7992 set_buffer_internal_1 (prev);
7993 }
7994 }
7995 }
7996
7997
7998 \f
7999 /***********************************************************************
8000 Output Cursor
8001 ***********************************************************************/
8002
8003 #ifdef HAVE_WINDOW_SYSTEM
8004
8005 /* EXPORT:
8006 Nominal cursor position -- where to draw output.
8007 HPOS and VPOS are window relative glyph matrix coordinates.
8008 X and Y are window relative pixel coordinates. */
8009
8010 struct cursor_pos output_cursor;
8011
8012
8013 /* EXPORT:
8014 Set the global variable output_cursor to CURSOR. All cursor
8015 positions are relative to updated_window. */
8016
8017 void
8018 set_output_cursor (cursor)
8019 struct cursor_pos *cursor;
8020 {
8021 output_cursor.hpos = cursor->hpos;
8022 output_cursor.vpos = cursor->vpos;
8023 output_cursor.x = cursor->x;
8024 output_cursor.y = cursor->y;
8025 }
8026
8027
8028 /* EXPORT for RIF:
8029 Set a nominal cursor position.
8030
8031 HPOS and VPOS are column/row positions in a window glyph matrix. X
8032 and Y are window text area relative pixel positions.
8033
8034 If this is done during an update, updated_window will contain the
8035 window that is being updated and the position is the future output
8036 cursor position for that window. If updated_window is null, use
8037 selected_window and display the cursor at the given position. */
8038
8039 void
8040 x_cursor_to (vpos, hpos, y, x)
8041 int vpos, hpos, y, x;
8042 {
8043 struct window *w;
8044
8045 /* If updated_window is not set, work on selected_window. */
8046 if (updated_window)
8047 w = updated_window;
8048 else
8049 w = XWINDOW (selected_window);
8050
8051 /* Set the output cursor. */
8052 output_cursor.hpos = hpos;
8053 output_cursor.vpos = vpos;
8054 output_cursor.x = x;
8055 output_cursor.y = y;
8056
8057 /* If not called as part of an update, really display the cursor.
8058 This will also set the cursor position of W. */
8059 if (updated_window == NULL)
8060 {
8061 BLOCK_INPUT;
8062 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8063 if (rif->flush_display_optional)
8064 rif->flush_display_optional (SELECTED_FRAME ());
8065 UNBLOCK_INPUT;
8066 }
8067 }
8068
8069 #endif /* HAVE_WINDOW_SYSTEM */
8070
8071 \f
8072 /***********************************************************************
8073 Tool-bars
8074 ***********************************************************************/
8075
8076 #ifdef HAVE_WINDOW_SYSTEM
8077
8078 /* Where the mouse was last time we reported a mouse event. */
8079
8080 FRAME_PTR last_mouse_frame;
8081
8082 /* Tool-bar item index of the item on which a mouse button was pressed
8083 or -1. */
8084
8085 int last_tool_bar_item;
8086
8087
8088 /* Update the tool-bar item list for frame F. This has to be done
8089 before we start to fill in any display lines. Called from
8090 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8091 and restore it here. */
8092
8093 static void
8094 update_tool_bar (f, save_match_data)
8095 struct frame *f;
8096 int save_match_data;
8097 {
8098 #ifdef USE_GTK
8099 int do_update = FRAME_EXTERNAL_TOOL_BAR(f);
8100 #else
8101 int do_update = WINDOWP (f->tool_bar_window)
8102 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0;
8103 #endif
8104
8105 if (do_update)
8106 {
8107 Lisp_Object window;
8108 struct window *w;
8109
8110 window = FRAME_SELECTED_WINDOW (f);
8111 w = XWINDOW (window);
8112
8113 /* If the user has switched buffers or windows, we need to
8114 recompute to reflect the new bindings. But we'll
8115 recompute when update_mode_lines is set too; that means
8116 that people can use force-mode-line-update to request
8117 that the menu bar be recomputed. The adverse effect on
8118 the rest of the redisplay algorithm is about the same as
8119 windows_or_buffers_changed anyway. */
8120 if (windows_or_buffers_changed
8121 || !NILP (w->update_mode_line)
8122 || update_mode_lines
8123 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8124 < BUF_MODIFF (XBUFFER (w->buffer)))
8125 != !NILP (w->last_had_star))
8126 || ((!NILP (Vtransient_mark_mode)
8127 && !NILP (XBUFFER (w->buffer)->mark_active))
8128 != !NILP (w->region_showing)))
8129 {
8130 struct buffer *prev = current_buffer;
8131 int count = SPECPDL_INDEX ();
8132 Lisp_Object old_tool_bar;
8133 struct gcpro gcpro1;
8134
8135 /* Set current_buffer to the buffer of the selected
8136 window of the frame, so that we get the right local
8137 keymaps. */
8138 set_buffer_internal_1 (XBUFFER (w->buffer));
8139
8140 /* Save match data, if we must. */
8141 if (save_match_data)
8142 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8143
8144 /* Make sure that we don't accidentally use bogus keymaps. */
8145 if (NILP (Voverriding_local_map_menu_flag))
8146 {
8147 specbind (Qoverriding_terminal_local_map, Qnil);
8148 specbind (Qoverriding_local_map, Qnil);
8149 }
8150
8151 old_tool_bar = f->tool_bar_items;
8152 GCPRO1 (old_tool_bar);
8153
8154 /* Build desired tool-bar items from keymaps. */
8155 BLOCK_INPUT;
8156 f->tool_bar_items
8157 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
8158 UNBLOCK_INPUT;
8159
8160 /* Redisplay the tool-bar if we changed it. */
8161 if (! NILP (Fequal (old_tool_bar, f->tool_bar_items)))
8162 w->update_mode_line = Qt;
8163
8164 unbind_to (count, Qnil);
8165 set_buffer_internal_1 (prev);
8166 }
8167 }
8168 }
8169
8170
8171 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8172 F's desired tool-bar contents. F->tool_bar_items must have
8173 been set up previously by calling prepare_menu_bars. */
8174
8175 static void
8176 build_desired_tool_bar_string (f)
8177 struct frame *f;
8178 {
8179 int i, size, size_needed;
8180 struct gcpro gcpro1, gcpro2, gcpro3;
8181 Lisp_Object image, plist, props;
8182
8183 image = plist = props = Qnil;
8184 GCPRO3 (image, plist, props);
8185
8186 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8187 Otherwise, make a new string. */
8188
8189 /* The size of the string we might be able to reuse. */
8190 size = (STRINGP (f->desired_tool_bar_string)
8191 ? SCHARS (f->desired_tool_bar_string)
8192 : 0);
8193
8194 /* We need one space in the string for each image. */
8195 size_needed = f->n_tool_bar_items;
8196
8197 /* Reuse f->desired_tool_bar_string, if possible. */
8198 if (size < size_needed || NILP (f->desired_tool_bar_string))
8199 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8200 make_number (' '));
8201 else
8202 {
8203 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8204 Fremove_text_properties (make_number (0), make_number (size),
8205 props, f->desired_tool_bar_string);
8206 }
8207
8208 /* Put a `display' property on the string for the images to display,
8209 put a `menu_item' property on tool-bar items with a value that
8210 is the index of the item in F's tool-bar item vector. */
8211 for (i = 0; i < f->n_tool_bar_items; ++i)
8212 {
8213 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8214
8215 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8216 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8217 int hmargin, vmargin, relief, idx, end;
8218 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
8219
8220 /* If image is a vector, choose the image according to the
8221 button state. */
8222 image = PROP (TOOL_BAR_ITEM_IMAGES);
8223 if (VECTORP (image))
8224 {
8225 if (enabled_p)
8226 idx = (selected_p
8227 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8228 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8229 else
8230 idx = (selected_p
8231 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8232 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8233
8234 xassert (ASIZE (image) >= idx);
8235 image = AREF (image, idx);
8236 }
8237 else
8238 idx = -1;
8239
8240 /* Ignore invalid image specifications. */
8241 if (!valid_image_p (image))
8242 continue;
8243
8244 /* Display the tool-bar button pressed, or depressed. */
8245 plist = Fcopy_sequence (XCDR (image));
8246
8247 /* Compute margin and relief to draw. */
8248 relief = (tool_bar_button_relief >= 0
8249 ? tool_bar_button_relief
8250 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8251 hmargin = vmargin = relief;
8252
8253 if (INTEGERP (Vtool_bar_button_margin)
8254 && XINT (Vtool_bar_button_margin) > 0)
8255 {
8256 hmargin += XFASTINT (Vtool_bar_button_margin);
8257 vmargin += XFASTINT (Vtool_bar_button_margin);
8258 }
8259 else if (CONSP (Vtool_bar_button_margin))
8260 {
8261 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8262 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8263 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8264
8265 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8266 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8267 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8268 }
8269
8270 if (auto_raise_tool_bar_buttons_p)
8271 {
8272 /* Add a `:relief' property to the image spec if the item is
8273 selected. */
8274 if (selected_p)
8275 {
8276 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8277 hmargin -= relief;
8278 vmargin -= relief;
8279 }
8280 }
8281 else
8282 {
8283 /* If image is selected, display it pressed, i.e. with a
8284 negative relief. If it's not selected, display it with a
8285 raised relief. */
8286 plist = Fplist_put (plist, QCrelief,
8287 (selected_p
8288 ? make_number (-relief)
8289 : make_number (relief)));
8290 hmargin -= relief;
8291 vmargin -= relief;
8292 }
8293
8294 /* Put a margin around the image. */
8295 if (hmargin || vmargin)
8296 {
8297 if (hmargin == vmargin)
8298 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8299 else
8300 plist = Fplist_put (plist, QCmargin,
8301 Fcons (make_number (hmargin),
8302 make_number (vmargin)));
8303 }
8304
8305 /* If button is not enabled, and we don't have special images
8306 for the disabled state, make the image appear disabled by
8307 applying an appropriate algorithm to it. */
8308 if (!enabled_p && idx < 0)
8309 plist = Fplist_put (plist, QCconversion, Qdisabled);
8310
8311 /* Put a `display' text property on the string for the image to
8312 display. Put a `menu-item' property on the string that gives
8313 the start of this item's properties in the tool-bar items
8314 vector. */
8315 image = Fcons (Qimage, plist);
8316 props = list4 (Qdisplay, image,
8317 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8318
8319 /* Let the last image hide all remaining spaces in the tool bar
8320 string. The string can be longer than needed when we reuse a
8321 previous string. */
8322 if (i + 1 == f->n_tool_bar_items)
8323 end = SCHARS (f->desired_tool_bar_string);
8324 else
8325 end = i + 1;
8326 Fadd_text_properties (make_number (i), make_number (end),
8327 props, f->desired_tool_bar_string);
8328 #undef PROP
8329 }
8330
8331 UNGCPRO;
8332 }
8333
8334
8335 /* Display one line of the tool-bar of frame IT->f. */
8336
8337 static void
8338 display_tool_bar_line (it)
8339 struct it *it;
8340 {
8341 struct glyph_row *row = it->glyph_row;
8342 int max_x = it->last_visible_x;
8343 struct glyph *last;
8344
8345 prepare_desired_row (row);
8346 row->y = it->current_y;
8347
8348 /* Note that this isn't made use of if the face hasn't a box,
8349 so there's no need to check the face here. */
8350 it->start_of_box_run_p = 1;
8351
8352 while (it->current_x < max_x)
8353 {
8354 int x_before, x, n_glyphs_before, i, nglyphs;
8355
8356 /* Get the next display element. */
8357 if (!get_next_display_element (it))
8358 break;
8359
8360 /* Produce glyphs. */
8361 x_before = it->current_x;
8362 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8363 PRODUCE_GLYPHS (it);
8364
8365 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8366 i = 0;
8367 x = x_before;
8368 while (i < nglyphs)
8369 {
8370 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
8371
8372 if (x + glyph->pixel_width > max_x)
8373 {
8374 /* Glyph doesn't fit on line. */
8375 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8376 it->current_x = x;
8377 goto out;
8378 }
8379
8380 ++it->hpos;
8381 x += glyph->pixel_width;
8382 ++i;
8383 }
8384
8385 /* Stop at line ends. */
8386 if (ITERATOR_AT_END_OF_LINE_P (it))
8387 break;
8388
8389 set_iterator_to_next (it, 1);
8390 }
8391
8392 out:;
8393
8394 row->displays_text_p = row->used[TEXT_AREA] != 0;
8395 extend_face_to_end_of_line (it);
8396 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8397 last->right_box_line_p = 1;
8398 if (last == row->glyphs[TEXT_AREA])
8399 last->left_box_line_p = 1;
8400 compute_line_metrics (it);
8401
8402 /* If line is empty, make it occupy the rest of the tool-bar. */
8403 if (!row->displays_text_p)
8404 {
8405 row->height = row->phys_height = it->last_visible_y - row->y;
8406 row->ascent = row->phys_ascent = 0;
8407 }
8408
8409 row->full_width_p = 1;
8410 row->continued_p = 0;
8411 row->truncated_on_left_p = 0;
8412 row->truncated_on_right_p = 0;
8413
8414 it->current_x = it->hpos = 0;
8415 it->current_y += row->height;
8416 ++it->vpos;
8417 ++it->glyph_row;
8418 }
8419
8420
8421 /* Value is the number of screen lines needed to make all tool-bar
8422 items of frame F visible. */
8423
8424 static int
8425 tool_bar_lines_needed (f)
8426 struct frame *f;
8427 {
8428 struct window *w = XWINDOW (f->tool_bar_window);
8429 struct it it;
8430
8431 /* Initialize an iterator for iteration over
8432 F->desired_tool_bar_string in the tool-bar window of frame F. */
8433 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8434 it.first_visible_x = 0;
8435 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
8436 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8437
8438 while (!ITERATOR_AT_END_P (&it))
8439 {
8440 it.glyph_row = w->desired_matrix->rows;
8441 clear_glyph_row (it.glyph_row);
8442 display_tool_bar_line (&it);
8443 }
8444
8445 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
8446 }
8447
8448
8449 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8450 0, 1, 0,
8451 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8452 (frame)
8453 Lisp_Object frame;
8454 {
8455 struct frame *f;
8456 struct window *w;
8457 int nlines = 0;
8458
8459 if (NILP (frame))
8460 frame = selected_frame;
8461 else
8462 CHECK_FRAME (frame);
8463 f = XFRAME (frame);
8464
8465 if (WINDOWP (f->tool_bar_window)
8466 || (w = XWINDOW (f->tool_bar_window),
8467 XFASTINT (w->height) > 0))
8468 {
8469 update_tool_bar (f, 1);
8470 if (f->n_tool_bar_items)
8471 {
8472 build_desired_tool_bar_string (f);
8473 nlines = tool_bar_lines_needed (f);
8474 }
8475 }
8476
8477 return make_number (nlines);
8478 }
8479
8480
8481 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
8482 height should be changed. */
8483
8484 static int
8485 redisplay_tool_bar (f)
8486 struct frame *f;
8487 {
8488 struct window *w;
8489 struct it it;
8490 struct glyph_row *row;
8491 int change_height_p = 0;
8492
8493 #ifdef USE_GTK
8494 if (FRAME_EXTERNAL_TOOL_BAR(f))
8495 update_frame_tool_bar (f);
8496 return 0;
8497 #endif
8498
8499 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8500 do anything. This means you must start with tool-bar-lines
8501 non-zero to get the auto-sizing effect. Or in other words, you
8502 can turn off tool-bars by specifying tool-bar-lines zero. */
8503 if (!WINDOWP (f->tool_bar_window)
8504 || (w = XWINDOW (f->tool_bar_window),
8505 XFASTINT (w->height) == 0))
8506 return 0;
8507
8508 /* Set up an iterator for the tool-bar window. */
8509 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8510 it.first_visible_x = 0;
8511 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
8512 row = it.glyph_row;
8513
8514 /* Build a string that represents the contents of the tool-bar. */
8515 build_desired_tool_bar_string (f);
8516 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8517
8518 /* Display as many lines as needed to display all tool-bar items. */
8519 while (it.current_y < it.last_visible_y)
8520 display_tool_bar_line (&it);
8521
8522 /* It doesn't make much sense to try scrolling in the tool-bar
8523 window, so don't do it. */
8524 w->desired_matrix->no_scrolling_p = 1;
8525 w->must_be_updated_p = 1;
8526
8527 if (auto_resize_tool_bars_p)
8528 {
8529 int nlines;
8530
8531 /* If we couldn't display everything, change the tool-bar's
8532 height. */
8533 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8534 change_height_p = 1;
8535
8536 /* If there are blank lines at the end, except for a partially
8537 visible blank line at the end that is smaller than
8538 CANON_Y_UNIT, change the tool-bar's height. */
8539 row = it.glyph_row - 1;
8540 if (!row->displays_text_p
8541 && row->height >= CANON_Y_UNIT (f))
8542 change_height_p = 1;
8543
8544 /* If row displays tool-bar items, but is partially visible,
8545 change the tool-bar's height. */
8546 if (row->displays_text_p
8547 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8548 change_height_p = 1;
8549
8550 /* Resize windows as needed by changing the `tool-bar-lines'
8551 frame parameter. */
8552 if (change_height_p
8553 && (nlines = tool_bar_lines_needed (f),
8554 nlines != XFASTINT (w->height)))
8555 {
8556 extern Lisp_Object Qtool_bar_lines;
8557 Lisp_Object frame;
8558 int old_height = XFASTINT (w->height);
8559
8560 XSETFRAME (frame, f);
8561 clear_glyph_matrix (w->desired_matrix);
8562 Fmodify_frame_parameters (frame,
8563 Fcons (Fcons (Qtool_bar_lines,
8564 make_number (nlines)),
8565 Qnil));
8566 if (XFASTINT (w->height) != old_height)
8567 fonts_changed_p = 1;
8568 }
8569 }
8570
8571 return change_height_p;
8572 }
8573
8574
8575 /* Get information about the tool-bar item which is displayed in GLYPH
8576 on frame F. Return in *PROP_IDX the index where tool-bar item
8577 properties start in F->tool_bar_items. Value is zero if
8578 GLYPH doesn't display a tool-bar item. */
8579
8580 static int
8581 tool_bar_item_info (f, glyph, prop_idx)
8582 struct frame *f;
8583 struct glyph *glyph;
8584 int *prop_idx;
8585 {
8586 Lisp_Object prop;
8587 int success_p;
8588 int charpos;
8589
8590 /* This function can be called asynchronously, which means we must
8591 exclude any possibility that Fget_text_property signals an
8592 error. */
8593 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8594 charpos = max (0, charpos);
8595
8596 /* Get the text property `menu-item' at pos. The value of that
8597 property is the start index of this item's properties in
8598 F->tool_bar_items. */
8599 prop = Fget_text_property (make_number (charpos),
8600 Qmenu_item, f->current_tool_bar_string);
8601 if (INTEGERP (prop))
8602 {
8603 *prop_idx = XINT (prop);
8604 success_p = 1;
8605 }
8606 else
8607 success_p = 0;
8608
8609 return success_p;
8610 }
8611
8612 \f
8613 /* Get information about the tool-bar item at position X/Y on frame F.
8614 Return in *GLYPH a pointer to the glyph of the tool-bar item in
8615 the current matrix of the tool-bar window of F, or NULL if not
8616 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8617 item in F->tool_bar_items. Value is
8618
8619 -1 if X/Y is not on a tool-bar item
8620 0 if X/Y is on the same item that was highlighted before.
8621 1 otherwise. */
8622
8623 static int
8624 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
8625 struct frame *f;
8626 int x, y;
8627 struct glyph **glyph;
8628 int *hpos, *vpos, *prop_idx;
8629 {
8630 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8631 struct window *w = XWINDOW (f->tool_bar_window);
8632 int area;
8633
8634 /* Find the glyph under X/Y. */
8635 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area, 0);
8636 if (*glyph == NULL)
8637 return -1;
8638
8639 /* Get the start of this tool-bar item's properties in
8640 f->tool_bar_items. */
8641 if (!tool_bar_item_info (f, *glyph, prop_idx))
8642 return -1;
8643
8644 /* Is mouse on the highlighted item? */
8645 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
8646 && *vpos >= dpyinfo->mouse_face_beg_row
8647 && *vpos <= dpyinfo->mouse_face_end_row
8648 && (*vpos > dpyinfo->mouse_face_beg_row
8649 || *hpos >= dpyinfo->mouse_face_beg_col)
8650 && (*vpos < dpyinfo->mouse_face_end_row
8651 || *hpos < dpyinfo->mouse_face_end_col
8652 || dpyinfo->mouse_face_past_end))
8653 return 0;
8654
8655 return 1;
8656 }
8657
8658
8659 /* EXPORT:
8660 Handle mouse button event on the tool-bar of frame F, at
8661 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
8662 0 for button release. MODIFIERS is event modifiers for button
8663 release. */
8664
8665 void
8666 handle_tool_bar_click (f, x, y, down_p, modifiers)
8667 struct frame *f;
8668 int x, y, down_p;
8669 unsigned int modifiers;
8670 {
8671 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8672 struct window *w = XWINDOW (f->tool_bar_window);
8673 int hpos, vpos, prop_idx;
8674 struct glyph *glyph;
8675 Lisp_Object enabled_p;
8676
8677 /* If not on the highlighted tool-bar item, return. */
8678 frame_to_window_pixel_xy (w, &x, &y);
8679 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
8680 return;
8681
8682 /* If item is disabled, do nothing. */
8683 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8684 if (NILP (enabled_p))
8685 return;
8686
8687 if (down_p)
8688 {
8689 /* Show item in pressed state. */
8690 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
8691 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
8692 last_tool_bar_item = prop_idx;
8693 }
8694 else
8695 {
8696 Lisp_Object key, frame;
8697 struct input_event event;
8698
8699 /* Show item in released state. */
8700 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
8701 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
8702
8703 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
8704
8705 XSETFRAME (frame, f);
8706 event.kind = TOOL_BAR_EVENT;
8707 event.frame_or_window = frame;
8708 event.arg = frame;
8709 kbd_buffer_store_event (&event);
8710
8711 event.kind = TOOL_BAR_EVENT;
8712 event.frame_or_window = frame;
8713 event.arg = key;
8714 event.modifiers = modifiers;
8715 kbd_buffer_store_event (&event);
8716 last_tool_bar_item = -1;
8717 }
8718 }
8719
8720
8721 /* Possibly highlight a tool-bar item on frame F when mouse moves to
8722 tool-bar window-relative coordinates X/Y. Called from
8723 note_mouse_highlight. */
8724
8725 static void
8726 note_tool_bar_highlight (f, x, y)
8727 struct frame *f;
8728 int x, y;
8729 {
8730 Lisp_Object window = f->tool_bar_window;
8731 struct window *w = XWINDOW (window);
8732 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8733 int hpos, vpos;
8734 struct glyph *glyph;
8735 struct glyph_row *row;
8736 int i;
8737 Lisp_Object enabled_p;
8738 int prop_idx;
8739 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
8740 int mouse_down_p, rc;
8741
8742 /* Function note_mouse_highlight is called with negative x(y
8743 values when mouse moves outside of the frame. */
8744 if (x <= 0 || y <= 0)
8745 {
8746 clear_mouse_face (dpyinfo);
8747 return;
8748 }
8749
8750 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
8751 if (rc < 0)
8752 {
8753 /* Not on tool-bar item. */
8754 clear_mouse_face (dpyinfo);
8755 return;
8756 }
8757 else if (rc == 0)
8758 /* On same tool-bar item as before. */
8759 goto set_help_echo;
8760
8761 clear_mouse_face (dpyinfo);
8762
8763 /* Mouse is down, but on different tool-bar item? */
8764 mouse_down_p = (dpyinfo->grabbed
8765 && f == last_mouse_frame
8766 && FRAME_LIVE_P (f));
8767 if (mouse_down_p
8768 && last_tool_bar_item != prop_idx)
8769 return;
8770
8771 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
8772 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
8773
8774 /* If tool-bar item is not enabled, don't highlight it. */
8775 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8776 if (!NILP (enabled_p))
8777 {
8778 /* Compute the x-position of the glyph. In front and past the
8779 image is a space. We include this is the highlighted area. */
8780 row = MATRIX_ROW (w->current_matrix, vpos);
8781 for (i = x = 0; i < hpos; ++i)
8782 x += row->glyphs[TEXT_AREA][i].pixel_width;
8783
8784 /* Record this as the current active region. */
8785 dpyinfo->mouse_face_beg_col = hpos;
8786 dpyinfo->mouse_face_beg_row = vpos;
8787 dpyinfo->mouse_face_beg_x = x;
8788 dpyinfo->mouse_face_beg_y = row->y;
8789 dpyinfo->mouse_face_past_end = 0;
8790
8791 dpyinfo->mouse_face_end_col = hpos + 1;
8792 dpyinfo->mouse_face_end_row = vpos;
8793 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
8794 dpyinfo->mouse_face_end_y = row->y;
8795 dpyinfo->mouse_face_window = window;
8796 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
8797
8798 /* Display it as active. */
8799 show_mouse_face (dpyinfo, draw);
8800 dpyinfo->mouse_face_image_state = draw;
8801 }
8802
8803 set_help_echo:
8804
8805 /* Set help_echo_string to a help string to display for this tool-bar item.
8806 XTread_socket does the rest. */
8807 help_echo_object = help_echo_window = Qnil;
8808 help_echo_pos = -1;
8809 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
8810 if (NILP (help_echo_string))
8811 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
8812 }
8813
8814 #endif /* HAVE_WINDOW_SYSTEM */
8815
8816
8817 \f
8818 /***********************************************************************
8819 Fringes
8820 ***********************************************************************/
8821
8822 #ifdef HAVE_WINDOW_SYSTEM
8823
8824 /* An arrow like this: `<-'. */
8825 static unsigned char left_bits[] = {
8826 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
8827
8828 /* Right truncation arrow bitmap `->'. */
8829 static unsigned char right_bits[] = {
8830 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
8831
8832 /* Marker for continued lines. */
8833 static unsigned char continued_bits[] = {
8834 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
8835
8836 /* Marker for continuation lines. */
8837 static unsigned char continuation_bits[] = {
8838 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
8839
8840 /* Overlay arrow bitmap. A triangular arrow. */
8841 static unsigned char ov_bits[] = {
8842 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
8843
8844 /* Bitmap drawn to indicate lines not displaying text if
8845 `indicate-empty-lines' is non-nil. */
8846 static unsigned char zv_bits[] = {
8847 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8848 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8849 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8850 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8851 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8852 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8853 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8854 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00};
8855
8856 struct fringe_bitmap fringe_bitmaps[MAX_FRINGE_BITMAPS] =
8857 {
8858 { 0, 0, 0, NULL /* NO_FRINGE_BITMAP */ },
8859 { 8, sizeof (left_bits), 0, left_bits },
8860 { 8, sizeof (right_bits), 0, right_bits },
8861 { 8, sizeof (continued_bits), 0, continued_bits },
8862 { 8, sizeof (continuation_bits), 0, continuation_bits },
8863 { 8, sizeof (ov_bits), 0, ov_bits },
8864 { 8, sizeof (zv_bits), 3, zv_bits }
8865 };
8866
8867
8868 /* Draw the bitmap WHICH in one of the left or right fringes of
8869 window W. ROW is the glyph row for which to display the bitmap; it
8870 determines the vertical position at which the bitmap has to be
8871 drawn. */
8872
8873 static void
8874 draw_fringe_bitmap (w, row, which, left_p)
8875 struct window *w;
8876 struct glyph_row *row;
8877 enum fringe_bitmap_type which;
8878 int left_p;
8879 {
8880 struct frame *f = XFRAME (WINDOW_FRAME (w));
8881 struct draw_fringe_bitmap_params p;
8882
8883 /* Convert row to frame coordinates. */
8884 p.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
8885
8886 p.which = which;
8887 p.wd = fringe_bitmaps[which].width;
8888
8889 p.h = fringe_bitmaps[which].height;
8890 p.dh = (fringe_bitmaps[which].period
8891 ? (p.y % fringe_bitmaps[which].period)
8892 : 0);
8893 p.h -= p.dh;
8894 /* Clip bitmap if too high. */
8895 if (p.h > row->height)
8896 p.h = row->height;
8897
8898 p.face = FACE_FROM_ID (f, FRINGE_FACE_ID);
8899 PREPARE_FACE_FOR_DISPLAY (f, p.face);
8900
8901 /* Clear left fringe if no bitmap to draw or if bitmap doesn't fill
8902 the fringe. */
8903 p.bx = -1;
8904 if (left_p)
8905 {
8906 if (p.wd > FRAME_X_LEFT_FRINGE_WIDTH (f))
8907 p.wd = FRAME_X_LEFT_FRINGE_WIDTH (f);
8908 p.x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
8909 - p.wd
8910 - (FRAME_X_LEFT_FRINGE_WIDTH (f) - p.wd) / 2);
8911 if (p.wd < FRAME_X_LEFT_FRINGE_WIDTH (f) || row->height > p.h)
8912 {
8913 /* If W has a vertical border to its left, don't draw over it. */
8914 int border = ((XFASTINT (w->left) > 0
8915 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8916 ? 1 : 0);
8917 p.bx = (window_box_left (w, -1)
8918 - FRAME_X_LEFT_FRINGE_WIDTH (f)
8919 + border);
8920 p.nx = (FRAME_X_LEFT_FRINGE_WIDTH (f) - border);
8921 }
8922 }
8923 else
8924 {
8925 if (p.wd > FRAME_X_RIGHT_FRINGE_WIDTH (f))
8926 p.wd = FRAME_X_RIGHT_FRINGE_WIDTH (f);
8927 p.x = (window_box_right (w, -1)
8928 + (FRAME_X_RIGHT_FRINGE_WIDTH (f) - p.wd) / 2);
8929 /* Clear right fringe if no bitmap to draw of if bitmap doesn't fill
8930 the fringe. */
8931 if (p.wd < FRAME_X_RIGHT_FRINGE_WIDTH (f) || row->height > p.h)
8932 {
8933 p.bx = window_box_right (w, -1);
8934 p.nx = FRAME_X_RIGHT_FRINGE_WIDTH (f);
8935 }
8936 }
8937
8938 if (p.bx >= 0)
8939 {
8940 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
8941
8942 p.by = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, row->y));
8943 p.ny = row->visible_height;
8944 }
8945
8946 /* Adjust y to the offset in the row to start drawing the bitmap. */
8947 p.y += (row->height - p.h) / 2;
8948
8949 rif->draw_fringe_bitmap (w, row, &p);
8950 }
8951
8952 /* Draw fringe bitmaps for glyph row ROW on window W. Call this
8953 function with input blocked. */
8954
8955 void
8956 draw_row_fringe_bitmaps (w, row)
8957 struct window *w;
8958 struct glyph_row *row;
8959 {
8960 struct frame *f = XFRAME (w->frame);
8961 enum fringe_bitmap_type bitmap;
8962
8963 xassert (interrupt_input_blocked);
8964
8965 /* If row is completely invisible, because of vscrolling, we
8966 don't have to draw anything. */
8967 if (row->visible_height <= 0)
8968 return;
8969
8970 if (FRAME_X_LEFT_FRINGE_WIDTH (f) != 0)
8971 {
8972 /* Decide which bitmap to draw in the left fringe. */
8973 if (row->overlay_arrow_p)
8974 bitmap = OVERLAY_ARROW_BITMAP;
8975 else if (row->truncated_on_left_p)
8976 bitmap = LEFT_TRUNCATION_BITMAP;
8977 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
8978 bitmap = CONTINUATION_LINE_BITMAP;
8979 else if (row->indicate_empty_line_p)
8980 bitmap = ZV_LINE_BITMAP;
8981 else
8982 bitmap = NO_FRINGE_BITMAP;
8983
8984 draw_fringe_bitmap (w, row, bitmap, 1);
8985 }
8986
8987 if (FRAME_X_RIGHT_FRINGE_WIDTH (f) != 0)
8988 {
8989 /* Decide which bitmap to draw in the right fringe. */
8990 if (row->truncated_on_right_p)
8991 bitmap = RIGHT_TRUNCATION_BITMAP;
8992 else if (row->continued_p)
8993 bitmap = CONTINUED_LINE_BITMAP;
8994 else if (row->indicate_empty_line_p && FRAME_X_LEFT_FRINGE_WIDTH (f) == 0)
8995 bitmap = ZV_LINE_BITMAP;
8996 else
8997 bitmap = NO_FRINGE_BITMAP;
8998
8999 draw_fringe_bitmap (w, row, bitmap, 0);
9000 }
9001 }
9002
9003
9004 /* Compute actual fringe widths */
9005
9006 void
9007 compute_fringe_widths (f, redraw)
9008 struct frame *f;
9009 int redraw;
9010 {
9011 int o_left = FRAME_X_LEFT_FRINGE_WIDTH (f);
9012 int o_right = FRAME_X_RIGHT_FRINGE_WIDTH (f);
9013 int o_cols = FRAME_X_FRINGE_COLS (f);
9014
9015 Lisp_Object left_fringe = Fassq (Qleft_fringe, f->param_alist);
9016 Lisp_Object right_fringe = Fassq (Qright_fringe, f->param_alist);
9017 int left_fringe_width, right_fringe_width;
9018
9019 if (!NILP (left_fringe))
9020 left_fringe = Fcdr (left_fringe);
9021 if (!NILP (right_fringe))
9022 right_fringe = Fcdr (right_fringe);
9023
9024 left_fringe_width = ((NILP (left_fringe) || !INTEGERP (left_fringe)) ? 8 :
9025 XINT (left_fringe));
9026 right_fringe_width = ((NILP (right_fringe) || !INTEGERP (right_fringe)) ? 8 :
9027 XINT (right_fringe));
9028
9029 if (left_fringe_width || right_fringe_width)
9030 {
9031 int left_wid = left_fringe_width >= 0 ? left_fringe_width : -left_fringe_width;
9032 int right_wid = right_fringe_width >= 0 ? right_fringe_width : -right_fringe_width;
9033 int conf_wid = left_wid + right_wid;
9034 int font_wid = FONT_WIDTH (FRAME_FONT (f));
9035 int cols = (left_wid + right_wid + font_wid-1) / font_wid;
9036 int real_wid = cols * font_wid;
9037 if (left_wid && right_wid)
9038 {
9039 if (left_fringe_width < 0)
9040 {
9041 /* Left fringe width is fixed, adjust right fringe if necessary */
9042 FRAME_X_LEFT_FRINGE_WIDTH (f) = left_wid;
9043 FRAME_X_RIGHT_FRINGE_WIDTH (f) = real_wid - left_wid;
9044 }
9045 else if (right_fringe_width < 0)
9046 {
9047 /* Right fringe width is fixed, adjust left fringe if necessary */
9048 FRAME_X_LEFT_FRINGE_WIDTH (f) = real_wid - right_wid;
9049 FRAME_X_RIGHT_FRINGE_WIDTH (f) = right_wid;
9050 }
9051 else
9052 {
9053 /* Adjust both fringes with an equal amount.
9054 Note that we are doing integer arithmetic here, so don't
9055 lose a pixel if the total width is an odd number. */
9056 int fill = real_wid - conf_wid;
9057 FRAME_X_LEFT_FRINGE_WIDTH (f) = left_wid + fill/2;
9058 FRAME_X_RIGHT_FRINGE_WIDTH (f) = right_wid + fill - fill/2;
9059 }
9060 }
9061 else if (left_fringe_width)
9062 {
9063 FRAME_X_LEFT_FRINGE_WIDTH (f) = real_wid;
9064 FRAME_X_RIGHT_FRINGE_WIDTH (f) = 0;
9065 }
9066 else
9067 {
9068 FRAME_X_LEFT_FRINGE_WIDTH (f) = 0;
9069 FRAME_X_RIGHT_FRINGE_WIDTH (f) = real_wid;
9070 }
9071 FRAME_X_FRINGE_COLS (f) = cols;
9072 FRAME_X_FRINGE_WIDTH (f) = real_wid;
9073 }
9074 else
9075 {
9076 FRAME_X_LEFT_FRINGE_WIDTH (f) = 0;
9077 FRAME_X_RIGHT_FRINGE_WIDTH (f) = 0;
9078 FRAME_X_FRINGE_COLS (f) = 0;
9079 FRAME_X_FRINGE_WIDTH (f) = 0;
9080 }
9081
9082 if (redraw && FRAME_VISIBLE_P (f))
9083 if (o_left != FRAME_X_LEFT_FRINGE_WIDTH (f) ||
9084 o_right != FRAME_X_RIGHT_FRINGE_WIDTH (f) ||
9085 o_cols != FRAME_X_FRINGE_COLS (f))
9086 redraw_frame (f);
9087 }
9088
9089 #endif /* HAVE_WINDOW_SYSTEM */
9090
9091
9092 \f
9093 /************************************************************************
9094 Horizontal scrolling
9095 ************************************************************************/
9096
9097 static int hscroll_window_tree P_ ((Lisp_Object));
9098 static int hscroll_windows P_ ((Lisp_Object));
9099
9100 /* For all leaf windows in the window tree rooted at WINDOW, set their
9101 hscroll value so that PT is (i) visible in the window, and (ii) so
9102 that it is not within a certain margin at the window's left and
9103 right border. Value is non-zero if any window's hscroll has been
9104 changed. */
9105
9106 static int
9107 hscroll_window_tree (window)
9108 Lisp_Object window;
9109 {
9110 int hscrolled_p = 0;
9111 int hscroll_relative_p = FLOATP (Vhscroll_step);
9112 int hscroll_step_abs = 0;
9113 double hscroll_step_rel = 0;
9114
9115 if (hscroll_relative_p)
9116 {
9117 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9118 if (hscroll_step_rel < 0)
9119 {
9120 hscroll_relative_p = 0;
9121 hscroll_step_abs = 0;
9122 }
9123 }
9124 else if (INTEGERP (Vhscroll_step))
9125 {
9126 hscroll_step_abs = XINT (Vhscroll_step);
9127 if (hscroll_step_abs < 0)
9128 hscroll_step_abs = 0;
9129 }
9130 else
9131 hscroll_step_abs = 0;
9132
9133 while (WINDOWP (window))
9134 {
9135 struct window *w = XWINDOW (window);
9136
9137 if (WINDOWP (w->hchild))
9138 hscrolled_p |= hscroll_window_tree (w->hchild);
9139 else if (WINDOWP (w->vchild))
9140 hscrolled_p |= hscroll_window_tree (w->vchild);
9141 else if (w->cursor.vpos >= 0)
9142 {
9143 int h_margin, text_area_x, text_area_y;
9144 int text_area_width, text_area_height;
9145 struct glyph_row *current_cursor_row
9146 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9147 struct glyph_row *desired_cursor_row
9148 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9149 struct glyph_row *cursor_row
9150 = (desired_cursor_row->enabled_p
9151 ? desired_cursor_row
9152 : current_cursor_row);
9153
9154 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
9155 &text_area_width, &text_area_height);
9156
9157 /* Scroll when cursor is inside this scroll margin. */
9158 h_margin = hscroll_margin * CANON_X_UNIT (XFRAME (w->frame));
9159
9160 if ((XFASTINT (w->hscroll)
9161 && w->cursor.x <= h_margin)
9162 || (cursor_row->enabled_p
9163 && cursor_row->truncated_on_right_p
9164 && (w->cursor.x >= text_area_width - h_margin)))
9165 {
9166 struct it it;
9167 int hscroll;
9168 struct buffer *saved_current_buffer;
9169 int pt;
9170 int wanted_x;
9171
9172 /* Find point in a display of infinite width. */
9173 saved_current_buffer = current_buffer;
9174 current_buffer = XBUFFER (w->buffer);
9175
9176 if (w == XWINDOW (selected_window))
9177 pt = BUF_PT (current_buffer);
9178 else
9179 {
9180 pt = marker_position (w->pointm);
9181 pt = max (BEGV, pt);
9182 pt = min (ZV, pt);
9183 }
9184
9185 /* Move iterator to pt starting at cursor_row->start in
9186 a line with infinite width. */
9187 init_to_row_start (&it, w, cursor_row);
9188 it.last_visible_x = INFINITY;
9189 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9190 current_buffer = saved_current_buffer;
9191
9192 /* Position cursor in window. */
9193 if (!hscroll_relative_p && hscroll_step_abs == 0)
9194 hscroll = max (0, it.current_x - text_area_width / 2)
9195 / CANON_X_UNIT (it.f);
9196 else if (w->cursor.x >= text_area_width - h_margin)
9197 {
9198 if (hscroll_relative_p)
9199 wanted_x = text_area_width * (1 - hscroll_step_rel)
9200 - h_margin;
9201 else
9202 wanted_x = text_area_width
9203 - hscroll_step_abs * CANON_X_UNIT (it.f)
9204 - h_margin;
9205 hscroll
9206 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
9207 }
9208 else
9209 {
9210 if (hscroll_relative_p)
9211 wanted_x = text_area_width * hscroll_step_rel
9212 + h_margin;
9213 else
9214 wanted_x = hscroll_step_abs * CANON_X_UNIT (it.f)
9215 + h_margin;
9216 hscroll
9217 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
9218 }
9219 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9220
9221 /* Don't call Fset_window_hscroll if value hasn't
9222 changed because it will prevent redisplay
9223 optimizations. */
9224 if (XFASTINT (w->hscroll) != hscroll)
9225 {
9226 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9227 w->hscroll = make_number (hscroll);
9228 hscrolled_p = 1;
9229 }
9230 }
9231 }
9232
9233 window = w->next;
9234 }
9235
9236 /* Value is non-zero if hscroll of any leaf window has been changed. */
9237 return hscrolled_p;
9238 }
9239
9240
9241 /* Set hscroll so that cursor is visible and not inside horizontal
9242 scroll margins for all windows in the tree rooted at WINDOW. See
9243 also hscroll_window_tree above. Value is non-zero if any window's
9244 hscroll has been changed. If it has, desired matrices on the frame
9245 of WINDOW are cleared. */
9246
9247 static int
9248 hscroll_windows (window)
9249 Lisp_Object window;
9250 {
9251 int hscrolled_p;
9252
9253 if (automatic_hscrolling_p)
9254 {
9255 hscrolled_p = hscroll_window_tree (window);
9256 if (hscrolled_p)
9257 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9258 }
9259 else
9260 hscrolled_p = 0;
9261 return hscrolled_p;
9262 }
9263
9264
9265 \f
9266 /************************************************************************
9267 Redisplay
9268 ************************************************************************/
9269
9270 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9271 to a non-zero value. This is sometimes handy to have in a debugger
9272 session. */
9273
9274 #if GLYPH_DEBUG
9275
9276 /* First and last unchanged row for try_window_id. */
9277
9278 int debug_first_unchanged_at_end_vpos;
9279 int debug_last_unchanged_at_beg_vpos;
9280
9281 /* Delta vpos and y. */
9282
9283 int debug_dvpos, debug_dy;
9284
9285 /* Delta in characters and bytes for try_window_id. */
9286
9287 int debug_delta, debug_delta_bytes;
9288
9289 /* Values of window_end_pos and window_end_vpos at the end of
9290 try_window_id. */
9291
9292 EMACS_INT debug_end_pos, debug_end_vpos;
9293
9294 /* Append a string to W->desired_matrix->method. FMT is a printf
9295 format string. A1...A9 are a supplement for a variable-length
9296 argument list. If trace_redisplay_p is non-zero also printf the
9297 resulting string to stderr. */
9298
9299 static void
9300 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9301 struct window *w;
9302 char *fmt;
9303 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9304 {
9305 char buffer[512];
9306 char *method = w->desired_matrix->method;
9307 int len = strlen (method);
9308 int size = sizeof w->desired_matrix->method;
9309 int remaining = size - len - 1;
9310
9311 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9312 if (len && remaining)
9313 {
9314 method[len] = '|';
9315 --remaining, ++len;
9316 }
9317
9318 strncpy (method + len, buffer, remaining);
9319
9320 if (trace_redisplay_p)
9321 fprintf (stderr, "%p (%s): %s\n",
9322 w,
9323 ((BUFFERP (w->buffer)
9324 && STRINGP (XBUFFER (w->buffer)->name))
9325 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9326 : "no buffer"),
9327 buffer);
9328 }
9329
9330 #endif /* GLYPH_DEBUG */
9331
9332
9333 /* Value is non-zero if all changes in window W, which displays
9334 current_buffer, are in the text between START and END. START is a
9335 buffer position, END is given as a distance from Z. Used in
9336 redisplay_internal for display optimization. */
9337
9338 static INLINE int
9339 text_outside_line_unchanged_p (w, start, end)
9340 struct window *w;
9341 int start, end;
9342 {
9343 int unchanged_p = 1;
9344
9345 /* If text or overlays have changed, see where. */
9346 if (XFASTINT (w->last_modified) < MODIFF
9347 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9348 {
9349 /* Gap in the line? */
9350 if (GPT < start || Z - GPT < end)
9351 unchanged_p = 0;
9352
9353 /* Changes start in front of the line, or end after it? */
9354 if (unchanged_p
9355 && (BEG_UNCHANGED < start - 1
9356 || END_UNCHANGED < end))
9357 unchanged_p = 0;
9358
9359 /* If selective display, can't optimize if changes start at the
9360 beginning of the line. */
9361 if (unchanged_p
9362 && INTEGERP (current_buffer->selective_display)
9363 && XINT (current_buffer->selective_display) > 0
9364 && (BEG_UNCHANGED < start || GPT <= start))
9365 unchanged_p = 0;
9366
9367 /* If there are overlays at the start or end of the line, these
9368 may have overlay strings with newlines in them. A change at
9369 START, for instance, may actually concern the display of such
9370 overlay strings as well, and they are displayed on different
9371 lines. So, quickly rule out this case. (For the future, it
9372 might be desirable to implement something more telling than
9373 just BEG/END_UNCHANGED.) */
9374 if (unchanged_p)
9375 {
9376 if (BEG + BEG_UNCHANGED == start
9377 && overlay_touches_p (start))
9378 unchanged_p = 0;
9379 if (END_UNCHANGED == end
9380 && overlay_touches_p (Z - end))
9381 unchanged_p = 0;
9382 }
9383 }
9384
9385 return unchanged_p;
9386 }
9387
9388
9389 /* Do a frame update, taking possible shortcuts into account. This is
9390 the main external entry point for redisplay.
9391
9392 If the last redisplay displayed an echo area message and that message
9393 is no longer requested, we clear the echo area or bring back the
9394 mini-buffer if that is in use. */
9395
9396 void
9397 redisplay ()
9398 {
9399 redisplay_internal (0);
9400 }
9401
9402
9403 /* Return 1 if point moved out of or into a composition. Otherwise
9404 return 0. PREV_BUF and PREV_PT are the last point buffer and
9405 position. BUF and PT are the current point buffer and position. */
9406
9407 int
9408 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9409 struct buffer *prev_buf, *buf;
9410 int prev_pt, pt;
9411 {
9412 int start, end;
9413 Lisp_Object prop;
9414 Lisp_Object buffer;
9415
9416 XSETBUFFER (buffer, buf);
9417 /* Check a composition at the last point if point moved within the
9418 same buffer. */
9419 if (prev_buf == buf)
9420 {
9421 if (prev_pt == pt)
9422 /* Point didn't move. */
9423 return 0;
9424
9425 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9426 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9427 && COMPOSITION_VALID_P (start, end, prop)
9428 && start < prev_pt && end > prev_pt)
9429 /* The last point was within the composition. Return 1 iff
9430 point moved out of the composition. */
9431 return (pt <= start || pt >= end);
9432 }
9433
9434 /* Check a composition at the current point. */
9435 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9436 && find_composition (pt, -1, &start, &end, &prop, buffer)
9437 && COMPOSITION_VALID_P (start, end, prop)
9438 && start < pt && end > pt);
9439 }
9440
9441
9442 /* Reconsider the setting of B->clip_changed which is displayed
9443 in window W. */
9444
9445 static INLINE void
9446 reconsider_clip_changes (w, b)
9447 struct window *w;
9448 struct buffer *b;
9449 {
9450 if (b->clip_changed
9451 && !NILP (w->window_end_valid)
9452 && w->current_matrix->buffer == b
9453 && w->current_matrix->zv == BUF_ZV (b)
9454 && w->current_matrix->begv == BUF_BEGV (b))
9455 b->clip_changed = 0;
9456
9457 /* If display wasn't paused, and W is not a tool bar window, see if
9458 point has been moved into or out of a composition. In that case,
9459 we set b->clip_changed to 1 to force updating the screen. If
9460 b->clip_changed has already been set to 1, we can skip this
9461 check. */
9462 if (!b->clip_changed
9463 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9464 {
9465 int pt;
9466
9467 if (w == XWINDOW (selected_window))
9468 pt = BUF_PT (current_buffer);
9469 else
9470 pt = marker_position (w->pointm);
9471
9472 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
9473 || pt != XINT (w->last_point))
9474 && check_point_in_composition (w->current_matrix->buffer,
9475 XINT (w->last_point),
9476 XBUFFER (w->buffer), pt))
9477 b->clip_changed = 1;
9478 }
9479 }
9480 \f
9481 #define STOP_POLLING \
9482 do { if (! polling_stopped_here) stop_polling (); \
9483 polling_stopped_here = 1; } while (0)
9484
9485 #define RESUME_POLLING \
9486 do { if (polling_stopped_here) start_polling (); \
9487 polling_stopped_here = 0; } while (0)
9488
9489
9490 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9491 response to any user action; therefore, we should preserve the echo
9492 area. (Actually, our caller does that job.) Perhaps in the future
9493 avoid recentering windows if it is not necessary; currently that
9494 causes some problems. */
9495
9496 static void
9497 redisplay_internal (preserve_echo_area)
9498 int preserve_echo_area;
9499 {
9500 struct window *w = XWINDOW (selected_window);
9501 struct frame *f = XFRAME (w->frame);
9502 int pause;
9503 int must_finish = 0;
9504 struct text_pos tlbufpos, tlendpos;
9505 int number_of_visible_frames;
9506 int count;
9507 struct frame *sf = SELECTED_FRAME ();
9508 int polling_stopped_here = 0;
9509
9510 /* Non-zero means redisplay has to consider all windows on all
9511 frames. Zero means, only selected_window is considered. */
9512 int consider_all_windows_p;
9513
9514 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9515
9516 /* No redisplay if running in batch mode or frame is not yet fully
9517 initialized, or redisplay is explicitly turned off by setting
9518 Vinhibit_redisplay. */
9519 if (noninteractive
9520 || !NILP (Vinhibit_redisplay)
9521 || !f->glyphs_initialized_p)
9522 return;
9523
9524 /* The flag redisplay_performed_directly_p is set by
9525 direct_output_for_insert when it already did the whole screen
9526 update necessary. */
9527 if (redisplay_performed_directly_p)
9528 {
9529 redisplay_performed_directly_p = 0;
9530 if (!hscroll_windows (selected_window))
9531 return;
9532 }
9533
9534 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9535 if (popup_activated ())
9536 return;
9537 #endif
9538
9539 /* I don't think this happens but let's be paranoid. */
9540 if (redisplaying_p)
9541 return;
9542
9543 /* Record a function that resets redisplaying_p to its old value
9544 when we leave this function. */
9545 count = SPECPDL_INDEX ();
9546 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
9547 ++redisplaying_p;
9548 specbind (Qinhibit_free_realized_faces, Qnil);
9549
9550 retry:
9551 pause = 0;
9552 reconsider_clip_changes (w, current_buffer);
9553
9554 /* If new fonts have been loaded that make a glyph matrix adjustment
9555 necessary, do it. */
9556 if (fonts_changed_p)
9557 {
9558 adjust_glyphs (NULL);
9559 ++windows_or_buffers_changed;
9560 fonts_changed_p = 0;
9561 }
9562
9563 /* If face_change_count is non-zero, init_iterator will free all
9564 realized faces, which includes the faces referenced from current
9565 matrices. So, we can't reuse current matrices in this case. */
9566 if (face_change_count)
9567 ++windows_or_buffers_changed;
9568
9569 if (! FRAME_WINDOW_P (sf)
9570 && previous_terminal_frame != sf)
9571 {
9572 /* Since frames on an ASCII terminal share the same display
9573 area, displaying a different frame means redisplay the whole
9574 thing. */
9575 windows_or_buffers_changed++;
9576 SET_FRAME_GARBAGED (sf);
9577 XSETFRAME (Vterminal_frame, sf);
9578 }
9579 previous_terminal_frame = sf;
9580
9581 /* Set the visible flags for all frames. Do this before checking
9582 for resized or garbaged frames; they want to know if their frames
9583 are visible. See the comment in frame.h for
9584 FRAME_SAMPLE_VISIBILITY. */
9585 {
9586 Lisp_Object tail, frame;
9587
9588 number_of_visible_frames = 0;
9589
9590 FOR_EACH_FRAME (tail, frame)
9591 {
9592 struct frame *f = XFRAME (frame);
9593
9594 FRAME_SAMPLE_VISIBILITY (f);
9595 if (FRAME_VISIBLE_P (f))
9596 ++number_of_visible_frames;
9597 clear_desired_matrices (f);
9598 }
9599 }
9600
9601 /* Notice any pending interrupt request to change frame size. */
9602 do_pending_window_change (1);
9603
9604 /* Clear frames marked as garbaged. */
9605 if (frame_garbaged)
9606 clear_garbaged_frames ();
9607
9608 /* Build menubar and tool-bar items. */
9609 prepare_menu_bars ();
9610
9611 if (windows_or_buffers_changed)
9612 update_mode_lines++;
9613
9614 /* Detect case that we need to write or remove a star in the mode line. */
9615 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
9616 {
9617 w->update_mode_line = Qt;
9618 if (buffer_shared > 1)
9619 update_mode_lines++;
9620 }
9621
9622 /* If %c is in the mode line, update it if needed. */
9623 if (!NILP (w->column_number_displayed)
9624 /* This alternative quickly identifies a common case
9625 where no change is needed. */
9626 && !(PT == XFASTINT (w->last_point)
9627 && XFASTINT (w->last_modified) >= MODIFF
9628 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9629 && (XFASTINT (w->column_number_displayed)
9630 != (int) current_column ())) /* iftc */
9631 w->update_mode_line = Qt;
9632
9633 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
9634
9635 /* The variable buffer_shared is set in redisplay_window and
9636 indicates that we redisplay a buffer in different windows. See
9637 there. */
9638 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
9639 || cursor_type_changed);
9640
9641 /* If specs for an arrow have changed, do thorough redisplay
9642 to ensure we remove any arrow that should no longer exist. */
9643 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
9644 || ! EQ (Voverlay_arrow_string, last_arrow_string))
9645 consider_all_windows_p = windows_or_buffers_changed = 1;
9646
9647 /* Normally the message* functions will have already displayed and
9648 updated the echo area, but the frame may have been trashed, or
9649 the update may have been preempted, so display the echo area
9650 again here. Checking message_cleared_p captures the case that
9651 the echo area should be cleared. */
9652 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
9653 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
9654 || (message_cleared_p
9655 && minibuf_level == 0
9656 /* If the mini-window is currently selected, this means the
9657 echo-area doesn't show through. */
9658 && !MINI_WINDOW_P (XWINDOW (selected_window))))
9659 {
9660 int window_height_changed_p = echo_area_display (0);
9661 must_finish = 1;
9662
9663 /* If we don't display the current message, don't clear the
9664 message_cleared_p flag, because, if we did, we wouldn't clear
9665 the echo area in the next redisplay which doesn't preserve
9666 the echo area. */
9667 if (!display_last_displayed_message_p)
9668 message_cleared_p = 0;
9669
9670 if (fonts_changed_p)
9671 goto retry;
9672 else if (window_height_changed_p)
9673 {
9674 consider_all_windows_p = 1;
9675 ++update_mode_lines;
9676 ++windows_or_buffers_changed;
9677
9678 /* If window configuration was changed, frames may have been
9679 marked garbaged. Clear them or we will experience
9680 surprises wrt scrolling. */
9681 if (frame_garbaged)
9682 clear_garbaged_frames ();
9683 }
9684 }
9685 else if (EQ (selected_window, minibuf_window)
9686 && (current_buffer->clip_changed
9687 || XFASTINT (w->last_modified) < MODIFF
9688 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9689 && resize_mini_window (w, 0))
9690 {
9691 /* Resized active mini-window to fit the size of what it is
9692 showing if its contents might have changed. */
9693 must_finish = 1;
9694 consider_all_windows_p = 1;
9695 ++windows_or_buffers_changed;
9696 ++update_mode_lines;
9697
9698 /* If window configuration was changed, frames may have been
9699 marked garbaged. Clear them or we will experience
9700 surprises wrt scrolling. */
9701 if (frame_garbaged)
9702 clear_garbaged_frames ();
9703 }
9704
9705
9706 /* If showing the region, and mark has changed, we must redisplay
9707 the whole window. The assignment to this_line_start_pos prevents
9708 the optimization directly below this if-statement. */
9709 if (((!NILP (Vtransient_mark_mode)
9710 && !NILP (XBUFFER (w->buffer)->mark_active))
9711 != !NILP (w->region_showing))
9712 || (!NILP (w->region_showing)
9713 && !EQ (w->region_showing,
9714 Fmarker_position (XBUFFER (w->buffer)->mark))))
9715 CHARPOS (this_line_start_pos) = 0;
9716
9717 /* Optimize the case that only the line containing the cursor in the
9718 selected window has changed. Variables starting with this_ are
9719 set in display_line and record information about the line
9720 containing the cursor. */
9721 tlbufpos = this_line_start_pos;
9722 tlendpos = this_line_end_pos;
9723 if (!consider_all_windows_p
9724 && CHARPOS (tlbufpos) > 0
9725 && NILP (w->update_mode_line)
9726 && !current_buffer->clip_changed
9727 && !current_buffer->prevent_redisplay_optimizations_p
9728 && FRAME_VISIBLE_P (XFRAME (w->frame))
9729 && !FRAME_OBSCURED_P (XFRAME (w->frame))
9730 /* Make sure recorded data applies to current buffer, etc. */
9731 && this_line_buffer == current_buffer
9732 && current_buffer == XBUFFER (w->buffer)
9733 && NILP (w->force_start)
9734 && NILP (w->optional_new_start)
9735 /* Point must be on the line that we have info recorded about. */
9736 && PT >= CHARPOS (tlbufpos)
9737 && PT <= Z - CHARPOS (tlendpos)
9738 /* All text outside that line, including its final newline,
9739 must be unchanged */
9740 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
9741 CHARPOS (tlendpos)))
9742 {
9743 if (CHARPOS (tlbufpos) > BEGV
9744 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
9745 && (CHARPOS (tlbufpos) == ZV
9746 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
9747 /* Former continuation line has disappeared by becoming empty */
9748 goto cancel;
9749 else if (XFASTINT (w->last_modified) < MODIFF
9750 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
9751 || MINI_WINDOW_P (w))
9752 {
9753 /* We have to handle the case of continuation around a
9754 wide-column character (See the comment in indent.c around
9755 line 885).
9756
9757 For instance, in the following case:
9758
9759 -------- Insert --------
9760 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
9761 J_I_ ==> J_I_ `^^' are cursors.
9762 ^^ ^^
9763 -------- --------
9764
9765 As we have to redraw the line above, we should goto cancel. */
9766
9767 struct it it;
9768 int line_height_before = this_line_pixel_height;
9769
9770 /* Note that start_display will handle the case that the
9771 line starting at tlbufpos is a continuation lines. */
9772 start_display (&it, w, tlbufpos);
9773
9774 /* Implementation note: It this still necessary? */
9775 if (it.current_x != this_line_start_x)
9776 goto cancel;
9777
9778 TRACE ((stderr, "trying display optimization 1\n"));
9779 w->cursor.vpos = -1;
9780 overlay_arrow_seen = 0;
9781 it.vpos = this_line_vpos;
9782 it.current_y = this_line_y;
9783 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
9784 display_line (&it);
9785
9786 /* If line contains point, is not continued,
9787 and ends at same distance from eob as before, we win */
9788 if (w->cursor.vpos >= 0
9789 /* Line is not continued, otherwise this_line_start_pos
9790 would have been set to 0 in display_line. */
9791 && CHARPOS (this_line_start_pos)
9792 /* Line ends as before. */
9793 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
9794 /* Line has same height as before. Otherwise other lines
9795 would have to be shifted up or down. */
9796 && this_line_pixel_height == line_height_before)
9797 {
9798 /* If this is not the window's last line, we must adjust
9799 the charstarts of the lines below. */
9800 if (it.current_y < it.last_visible_y)
9801 {
9802 struct glyph_row *row
9803 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
9804 int delta, delta_bytes;
9805
9806 if (Z - CHARPOS (tlendpos) == ZV)
9807 {
9808 /* This line ends at end of (accessible part of)
9809 buffer. There is no newline to count. */
9810 delta = (Z
9811 - CHARPOS (tlendpos)
9812 - MATRIX_ROW_START_CHARPOS (row));
9813 delta_bytes = (Z_BYTE
9814 - BYTEPOS (tlendpos)
9815 - MATRIX_ROW_START_BYTEPOS (row));
9816 }
9817 else
9818 {
9819 /* This line ends in a newline. Must take
9820 account of the newline and the rest of the
9821 text that follows. */
9822 delta = (Z
9823 - CHARPOS (tlendpos)
9824 - MATRIX_ROW_START_CHARPOS (row));
9825 delta_bytes = (Z_BYTE
9826 - BYTEPOS (tlendpos)
9827 - MATRIX_ROW_START_BYTEPOS (row));
9828 }
9829
9830 increment_matrix_positions (w->current_matrix,
9831 this_line_vpos + 1,
9832 w->current_matrix->nrows,
9833 delta, delta_bytes);
9834 }
9835
9836 /* If this row displays text now but previously didn't,
9837 or vice versa, w->window_end_vpos may have to be
9838 adjusted. */
9839 if ((it.glyph_row - 1)->displays_text_p)
9840 {
9841 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
9842 XSETINT (w->window_end_vpos, this_line_vpos);
9843 }
9844 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
9845 && this_line_vpos > 0)
9846 XSETINT (w->window_end_vpos, this_line_vpos - 1);
9847 w->window_end_valid = Qnil;
9848
9849 /* Update hint: No need to try to scroll in update_window. */
9850 w->desired_matrix->no_scrolling_p = 1;
9851
9852 #if GLYPH_DEBUG
9853 *w->desired_matrix->method = 0;
9854 debug_method_add (w, "optimization 1");
9855 #endif
9856 goto update;
9857 }
9858 else
9859 goto cancel;
9860 }
9861 else if (/* Cursor position hasn't changed. */
9862 PT == XFASTINT (w->last_point)
9863 /* Make sure the cursor was last displayed
9864 in this window. Otherwise we have to reposition it. */
9865 && 0 <= w->cursor.vpos
9866 && XINT (w->height) > w->cursor.vpos)
9867 {
9868 if (!must_finish)
9869 {
9870 do_pending_window_change (1);
9871
9872 /* We used to always goto end_of_redisplay here, but this
9873 isn't enough if we have a blinking cursor. */
9874 if (w->cursor_off_p == w->last_cursor_off_p)
9875 goto end_of_redisplay;
9876 }
9877 goto update;
9878 }
9879 /* If highlighting the region, or if the cursor is in the echo area,
9880 then we can't just move the cursor. */
9881 else if (! (!NILP (Vtransient_mark_mode)
9882 && !NILP (current_buffer->mark_active))
9883 && (EQ (selected_window, current_buffer->last_selected_window)
9884 || highlight_nonselected_windows)
9885 && NILP (w->region_showing)
9886 && NILP (Vshow_trailing_whitespace)
9887 && !cursor_in_echo_area)
9888 {
9889 struct it it;
9890 struct glyph_row *row;
9891
9892 /* Skip from tlbufpos to PT and see where it is. Note that
9893 PT may be in invisible text. If so, we will end at the
9894 next visible position. */
9895 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
9896 NULL, DEFAULT_FACE_ID);
9897 it.current_x = this_line_start_x;
9898 it.current_y = this_line_y;
9899 it.vpos = this_line_vpos;
9900
9901 /* The call to move_it_to stops in front of PT, but
9902 moves over before-strings. */
9903 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
9904
9905 if (it.vpos == this_line_vpos
9906 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
9907 row->enabled_p))
9908 {
9909 xassert (this_line_vpos == it.vpos);
9910 xassert (this_line_y == it.current_y);
9911 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9912 #if GLYPH_DEBUG
9913 *w->desired_matrix->method = 0;
9914 debug_method_add (w, "optimization 3");
9915 #endif
9916 goto update;
9917 }
9918 else
9919 goto cancel;
9920 }
9921
9922 cancel:
9923 /* Text changed drastically or point moved off of line. */
9924 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
9925 }
9926
9927 CHARPOS (this_line_start_pos) = 0;
9928 consider_all_windows_p |= buffer_shared > 1;
9929 ++clear_face_cache_count;
9930
9931
9932 /* Build desired matrices, and update the display. If
9933 consider_all_windows_p is non-zero, do it for all windows on all
9934 frames. Otherwise do it for selected_window, only. */
9935
9936 if (consider_all_windows_p)
9937 {
9938 Lisp_Object tail, frame;
9939 int i, n = 0, size = 50;
9940 struct frame **updated
9941 = (struct frame **) alloca (size * sizeof *updated);
9942
9943 /* Clear the face cache eventually. */
9944 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
9945 {
9946 clear_face_cache (0);
9947 clear_face_cache_count = 0;
9948 }
9949
9950 /* Recompute # windows showing selected buffer. This will be
9951 incremented each time such a window is displayed. */
9952 buffer_shared = 0;
9953
9954 FOR_EACH_FRAME (tail, frame)
9955 {
9956 struct frame *f = XFRAME (frame);
9957
9958 if (FRAME_WINDOW_P (f) || f == sf)
9959 {
9960 #ifdef HAVE_WINDOW_SYSTEM
9961 if (clear_face_cache_count % 50 == 0
9962 && FRAME_WINDOW_P (f))
9963 clear_image_cache (f, 0);
9964 #endif /* HAVE_WINDOW_SYSTEM */
9965
9966 /* Mark all the scroll bars to be removed; we'll redeem
9967 the ones we want when we redisplay their windows. */
9968 if (condemn_scroll_bars_hook)
9969 condemn_scroll_bars_hook (f);
9970
9971 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
9972 redisplay_windows (FRAME_ROOT_WINDOW (f));
9973
9974 /* Any scroll bars which redisplay_windows should have
9975 nuked should now go away. */
9976 if (judge_scroll_bars_hook)
9977 judge_scroll_bars_hook (f);
9978
9979 /* If fonts changed, display again. */
9980 /* ??? rms: I suspect it is a mistake to jump all the way
9981 back to retry here. It should just retry this frame. */
9982 if (fonts_changed_p)
9983 goto retry;
9984
9985 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
9986 {
9987 /* See if we have to hscroll. */
9988 if (hscroll_windows (f->root_window))
9989 goto retry;
9990
9991 /* Prevent various kinds of signals during display
9992 update. stdio is not robust about handling
9993 signals, which can cause an apparent I/O
9994 error. */
9995 if (interrupt_input)
9996 unrequest_sigio ();
9997 STOP_POLLING;
9998
9999 /* Update the display. */
10000 set_window_update_flags (XWINDOW (f->root_window), 1);
10001 pause |= update_frame (f, 0, 0);
10002 if (pause)
10003 break;
10004
10005 if (n == size)
10006 {
10007 int nbytes = size * sizeof *updated;
10008 struct frame **p = (struct frame **) alloca (2 * nbytes);
10009 bcopy (updated, p, nbytes);
10010 size *= 2;
10011 }
10012
10013 updated[n++] = f;
10014 }
10015 }
10016 }
10017
10018 /* Do the mark_window_display_accurate after all windows have
10019 been redisplayed because this call resets flags in buffers
10020 which are needed for proper redisplay. */
10021 for (i = 0; i < n; ++i)
10022 {
10023 struct frame *f = updated[i];
10024 mark_window_display_accurate (f->root_window, 1);
10025 if (frame_up_to_date_hook)
10026 frame_up_to_date_hook (f);
10027 }
10028 }
10029 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10030 {
10031 Lisp_Object mini_window;
10032 struct frame *mini_frame;
10033
10034 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10035 /* Use list_of_error, not Qerror, so that
10036 we catch only errors and don't run the debugger. */
10037 internal_condition_case_1 (redisplay_window_1, selected_window,
10038 list_of_error,
10039 redisplay_window_error);
10040
10041 /* Compare desired and current matrices, perform output. */
10042
10043 update:
10044 /* If fonts changed, display again. */
10045 if (fonts_changed_p)
10046 goto retry;
10047
10048 /* Prevent various kinds of signals during display update.
10049 stdio is not robust about handling signals,
10050 which can cause an apparent I/O error. */
10051 if (interrupt_input)
10052 unrequest_sigio ();
10053 STOP_POLLING;
10054
10055 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10056 {
10057 if (hscroll_windows (selected_window))
10058 goto retry;
10059
10060 XWINDOW (selected_window)->must_be_updated_p = 1;
10061 pause = update_frame (sf, 0, 0);
10062 }
10063
10064 /* We may have called echo_area_display at the top of this
10065 function. If the echo area is on another frame, that may
10066 have put text on a frame other than the selected one, so the
10067 above call to update_frame would not have caught it. Catch
10068 it here. */
10069 mini_window = FRAME_MINIBUF_WINDOW (sf);
10070 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10071
10072 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10073 {
10074 XWINDOW (mini_window)->must_be_updated_p = 1;
10075 pause |= update_frame (mini_frame, 0, 0);
10076 if (!pause && hscroll_windows (mini_window))
10077 goto retry;
10078 }
10079 }
10080
10081 /* If display was paused because of pending input, make sure we do a
10082 thorough update the next time. */
10083 if (pause)
10084 {
10085 /* Prevent the optimization at the beginning of
10086 redisplay_internal that tries a single-line update of the
10087 line containing the cursor in the selected window. */
10088 CHARPOS (this_line_start_pos) = 0;
10089
10090 /* Let the overlay arrow be updated the next time. */
10091 if (!NILP (last_arrow_position))
10092 {
10093 last_arrow_position = Qt;
10094 last_arrow_string = Qt;
10095 }
10096
10097 /* If we pause after scrolling, some rows in the current
10098 matrices of some windows are not valid. */
10099 if (!WINDOW_FULL_WIDTH_P (w)
10100 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10101 update_mode_lines = 1;
10102 }
10103 else
10104 {
10105 if (!consider_all_windows_p)
10106 {
10107 /* This has already been done above if
10108 consider_all_windows_p is set. */
10109 mark_window_display_accurate_1 (w, 1);
10110
10111 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
10112 last_arrow_string = Voverlay_arrow_string;
10113
10114 if (frame_up_to_date_hook != 0)
10115 frame_up_to_date_hook (sf);
10116 }
10117
10118 update_mode_lines = 0;
10119 windows_or_buffers_changed = 0;
10120 cursor_type_changed = 0;
10121 }
10122
10123 /* Start SIGIO interrupts coming again. Having them off during the
10124 code above makes it less likely one will discard output, but not
10125 impossible, since there might be stuff in the system buffer here.
10126 But it is much hairier to try to do anything about that. */
10127 if (interrupt_input)
10128 request_sigio ();
10129 RESUME_POLLING;
10130
10131 /* If a frame has become visible which was not before, redisplay
10132 again, so that we display it. Expose events for such a frame
10133 (which it gets when becoming visible) don't call the parts of
10134 redisplay constructing glyphs, so simply exposing a frame won't
10135 display anything in this case. So, we have to display these
10136 frames here explicitly. */
10137 if (!pause)
10138 {
10139 Lisp_Object tail, frame;
10140 int new_count = 0;
10141
10142 FOR_EACH_FRAME (tail, frame)
10143 {
10144 int this_is_visible = 0;
10145
10146 if (XFRAME (frame)->visible)
10147 this_is_visible = 1;
10148 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10149 if (XFRAME (frame)->visible)
10150 this_is_visible = 1;
10151
10152 if (this_is_visible)
10153 new_count++;
10154 }
10155
10156 if (new_count != number_of_visible_frames)
10157 windows_or_buffers_changed++;
10158 }
10159
10160 /* Change frame size now if a change is pending. */
10161 do_pending_window_change (1);
10162
10163 /* If we just did a pending size change, or have additional
10164 visible frames, redisplay again. */
10165 if (windows_or_buffers_changed && !pause)
10166 goto retry;
10167
10168 end_of_redisplay:
10169 unbind_to (count, Qnil);
10170 RESUME_POLLING;
10171 }
10172
10173
10174 /* Redisplay, but leave alone any recent echo area message unless
10175 another message has been requested in its place.
10176
10177 This is useful in situations where you need to redisplay but no
10178 user action has occurred, making it inappropriate for the message
10179 area to be cleared. See tracking_off and
10180 wait_reading_process_input for examples of these situations.
10181
10182 FROM_WHERE is an integer saying from where this function was
10183 called. This is useful for debugging. */
10184
10185 void
10186 redisplay_preserve_echo_area (from_where)
10187 int from_where;
10188 {
10189 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10190
10191 if (!NILP (echo_area_buffer[1]))
10192 {
10193 /* We have a previously displayed message, but no current
10194 message. Redisplay the previous message. */
10195 display_last_displayed_message_p = 1;
10196 redisplay_internal (1);
10197 display_last_displayed_message_p = 0;
10198 }
10199 else
10200 redisplay_internal (1);
10201 }
10202
10203
10204 /* Function registered with record_unwind_protect in
10205 redisplay_internal. Reset redisplaying_p to the value it had
10206 before redisplay_internal was called, and clear
10207 prevent_freeing_realized_faces_p. */
10208
10209 static Lisp_Object
10210 unwind_redisplay (old_redisplaying_p)
10211 Lisp_Object old_redisplaying_p;
10212 {
10213 redisplaying_p = XFASTINT (old_redisplaying_p);
10214 return Qnil;
10215 }
10216
10217
10218 /* Mark the display of window W as accurate or inaccurate. If
10219 ACCURATE_P is non-zero mark display of W as accurate. If
10220 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10221 redisplay_internal is called. */
10222
10223 static void
10224 mark_window_display_accurate_1 (w, accurate_p)
10225 struct window *w;
10226 int accurate_p;
10227 {
10228 if (BUFFERP (w->buffer))
10229 {
10230 struct buffer *b = XBUFFER (w->buffer);
10231
10232 w->last_modified
10233 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10234 w->last_overlay_modified
10235 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10236 w->last_had_star
10237 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10238
10239 if (accurate_p)
10240 {
10241 b->clip_changed = 0;
10242 b->prevent_redisplay_optimizations_p = 0;
10243
10244 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10245 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10246 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10247 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10248
10249 w->current_matrix->buffer = b;
10250 w->current_matrix->begv = BUF_BEGV (b);
10251 w->current_matrix->zv = BUF_ZV (b);
10252
10253 w->last_cursor = w->cursor;
10254 w->last_cursor_off_p = w->cursor_off_p;
10255
10256 if (w == XWINDOW (selected_window))
10257 w->last_point = make_number (BUF_PT (b));
10258 else
10259 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10260 }
10261 }
10262
10263 if (accurate_p)
10264 {
10265 w->window_end_valid = w->buffer;
10266 #if 0 /* This is incorrect with variable-height lines. */
10267 xassert (XINT (w->window_end_vpos)
10268 < (XINT (w->height)
10269 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10270 #endif
10271 w->update_mode_line = Qnil;
10272 }
10273 }
10274
10275
10276 /* Mark the display of windows in the window tree rooted at WINDOW as
10277 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10278 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10279 be redisplayed the next time redisplay_internal is called. */
10280
10281 void
10282 mark_window_display_accurate (window, accurate_p)
10283 Lisp_Object window;
10284 int accurate_p;
10285 {
10286 struct window *w;
10287
10288 for (; !NILP (window); window = w->next)
10289 {
10290 w = XWINDOW (window);
10291 mark_window_display_accurate_1 (w, accurate_p);
10292
10293 if (!NILP (w->vchild))
10294 mark_window_display_accurate (w->vchild, accurate_p);
10295 if (!NILP (w->hchild))
10296 mark_window_display_accurate (w->hchild, accurate_p);
10297 }
10298
10299 if (accurate_p)
10300 {
10301 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
10302 last_arrow_string = Voverlay_arrow_string;
10303 }
10304 else
10305 {
10306 /* Force a thorough redisplay the next time by setting
10307 last_arrow_position and last_arrow_string to t, which is
10308 unequal to any useful value of Voverlay_arrow_... */
10309 last_arrow_position = Qt;
10310 last_arrow_string = Qt;
10311 }
10312 }
10313
10314
10315 /* Return value in display table DP (Lisp_Char_Table *) for character
10316 C. Since a display table doesn't have any parent, we don't have to
10317 follow parent. Do not call this function directly but use the
10318 macro DISP_CHAR_VECTOR. */
10319
10320 Lisp_Object
10321 disp_char_vector (dp, c)
10322 struct Lisp_Char_Table *dp;
10323 int c;
10324 {
10325 int code[4], i;
10326 Lisp_Object val;
10327
10328 if (SINGLE_BYTE_CHAR_P (c))
10329 return (dp->contents[c]);
10330
10331 SPLIT_CHAR (c, code[0], code[1], code[2]);
10332 if (code[1] < 32)
10333 code[1] = -1;
10334 else if (code[2] < 32)
10335 code[2] = -1;
10336
10337 /* Here, the possible range of code[0] (== charset ID) is
10338 128..max_charset. Since the top level char table contains data
10339 for multibyte characters after 256th element, we must increment
10340 code[0] by 128 to get a correct index. */
10341 code[0] += 128;
10342 code[3] = -1; /* anchor */
10343
10344 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10345 {
10346 val = dp->contents[code[i]];
10347 if (!SUB_CHAR_TABLE_P (val))
10348 return (NILP (val) ? dp->defalt : val);
10349 }
10350
10351 /* Here, val is a sub char table. We return the default value of
10352 it. */
10353 return (dp->defalt);
10354 }
10355
10356
10357 \f
10358 /***********************************************************************
10359 Window Redisplay
10360 ***********************************************************************/
10361
10362 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10363
10364 static void
10365 redisplay_windows (window)
10366 Lisp_Object window;
10367 {
10368 while (!NILP (window))
10369 {
10370 struct window *w = XWINDOW (window);
10371
10372 if (!NILP (w->hchild))
10373 redisplay_windows (w->hchild);
10374 else if (!NILP (w->vchild))
10375 redisplay_windows (w->vchild);
10376 else
10377 {
10378 displayed_buffer = XBUFFER (w->buffer);
10379 /* Use list_of_error, not Qerror, so that
10380 we catch only errors and don't run the debugger. */
10381 internal_condition_case_1 (redisplay_window_0, window,
10382 list_of_error,
10383 redisplay_window_error);
10384 }
10385
10386 window = w->next;
10387 }
10388 }
10389
10390 static Lisp_Object
10391 redisplay_window_error ()
10392 {
10393 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10394 return Qnil;
10395 }
10396
10397 static Lisp_Object
10398 redisplay_window_0 (window)
10399 Lisp_Object window;
10400 {
10401 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10402 redisplay_window (window, 0);
10403 return Qnil;
10404 }
10405
10406 static Lisp_Object
10407 redisplay_window_1 (window)
10408 Lisp_Object window;
10409 {
10410 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10411 redisplay_window (window, 1);
10412 return Qnil;
10413 }
10414 \f
10415
10416 /* Increment GLYPH until it reaches END or CONDITION fails while
10417 adding (GLYPH)->pixel_width to X. */
10418
10419 #define SKIP_GLYPHS(glyph, end, x, condition) \
10420 do \
10421 { \
10422 (x) += (glyph)->pixel_width; \
10423 ++(glyph); \
10424 } \
10425 while ((glyph) < (end) && (condition))
10426
10427
10428 /* Set cursor position of W. PT is assumed to be displayed in ROW.
10429 DELTA is the number of bytes by which positions recorded in ROW
10430 differ from current buffer positions. */
10431
10432 void
10433 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10434 struct window *w;
10435 struct glyph_row *row;
10436 struct glyph_matrix *matrix;
10437 int delta, delta_bytes, dy, dvpos;
10438 {
10439 struct glyph *glyph = row->glyphs[TEXT_AREA];
10440 struct glyph *end = glyph + row->used[TEXT_AREA];
10441 /* The first glyph that starts a sequence of glyphs from string. */
10442 struct glyph *string_start;
10443 /* The X coordinate of string_start. */
10444 int string_start_x;
10445 /* The last known character position. */
10446 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10447 /* The last known character position before string_start. */
10448 int string_before_pos;
10449 int x = row->x;
10450 int pt_old = PT - delta;
10451
10452 /* Skip over glyphs not having an object at the start of the row.
10453 These are special glyphs like truncation marks on terminal
10454 frames. */
10455 if (row->displays_text_p)
10456 while (glyph < end
10457 && INTEGERP (glyph->object)
10458 && glyph->charpos < 0)
10459 {
10460 x += glyph->pixel_width;
10461 ++glyph;
10462 }
10463
10464 string_start = NULL;
10465 while (glyph < end
10466 && !INTEGERP (glyph->object)
10467 && (!BUFFERP (glyph->object)
10468 || (last_pos = glyph->charpos) < pt_old))
10469 {
10470 if (! STRINGP (glyph->object))
10471 {
10472 string_start = NULL;
10473 x += glyph->pixel_width;
10474 ++glyph;
10475 }
10476 else
10477 {
10478 string_before_pos = last_pos;
10479 string_start = glyph;
10480 string_start_x = x;
10481 /* Skip all glyphs from string. */
10482 SKIP_GLYPHS (glyph, end, x, STRINGP (glyph->object));
10483 }
10484 }
10485
10486 if (string_start
10487 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
10488 {
10489 /* We may have skipped over point because the previous glyphs
10490 are from string. As there's no easy way to know the
10491 character position of the current glyph, find the correct
10492 glyph on point by scanning from string_start again. */
10493 Lisp_Object limit;
10494 Lisp_Object string;
10495 int pos;
10496
10497 limit = make_number (pt_old + 1);
10498 end = glyph;
10499 glyph = string_start;
10500 x = string_start_x;
10501 string = glyph->object;
10502 pos = string_buffer_position (w, string, string_before_pos);
10503 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
10504 because we always put cursor after overlay strings. */
10505 while (pos == 0 && glyph < end)
10506 {
10507 string = glyph->object;
10508 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10509 if (glyph < end)
10510 pos = string_buffer_position (w, glyph->object, string_before_pos);
10511 }
10512
10513 while (glyph < end)
10514 {
10515 pos = XINT (Fnext_single_char_property_change
10516 (make_number (pos), Qdisplay, Qnil, limit));
10517 if (pos > pt_old)
10518 break;
10519 /* Skip glyphs from the same string. */
10520 string = glyph->object;
10521 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10522 /* Skip glyphs from an overlay. */
10523 while (glyph < end
10524 && ! string_buffer_position (w, glyph->object, pos))
10525 {
10526 string = glyph->object;
10527 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10528 }
10529 }
10530 }
10531
10532 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
10533 w->cursor.x = x;
10534 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
10535 w->cursor.y = row->y + dy;
10536
10537 if (w == XWINDOW (selected_window))
10538 {
10539 if (!row->continued_p
10540 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
10541 && row->x == 0)
10542 {
10543 this_line_buffer = XBUFFER (w->buffer);
10544
10545 CHARPOS (this_line_start_pos)
10546 = MATRIX_ROW_START_CHARPOS (row) + delta;
10547 BYTEPOS (this_line_start_pos)
10548 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
10549
10550 CHARPOS (this_line_end_pos)
10551 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
10552 BYTEPOS (this_line_end_pos)
10553 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
10554
10555 this_line_y = w->cursor.y;
10556 this_line_pixel_height = row->height;
10557 this_line_vpos = w->cursor.vpos;
10558 this_line_start_x = row->x;
10559 }
10560 else
10561 CHARPOS (this_line_start_pos) = 0;
10562 }
10563 }
10564
10565
10566 /* Run window scroll functions, if any, for WINDOW with new window
10567 start STARTP. Sets the window start of WINDOW to that position.
10568
10569 We assume that the window's buffer is really current. */
10570
10571 static INLINE struct text_pos
10572 run_window_scroll_functions (window, startp)
10573 Lisp_Object window;
10574 struct text_pos startp;
10575 {
10576 struct window *w = XWINDOW (window);
10577 SET_MARKER_FROM_TEXT_POS (w->start, startp);
10578
10579 if (current_buffer != XBUFFER (w->buffer))
10580 abort ();
10581
10582 if (!NILP (Vwindow_scroll_functions))
10583 {
10584 run_hook_with_args_2 (Qwindow_scroll_functions, window,
10585 make_number (CHARPOS (startp)));
10586 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10587 /* In case the hook functions switch buffers. */
10588 if (current_buffer != XBUFFER (w->buffer))
10589 set_buffer_internal_1 (XBUFFER (w->buffer));
10590 }
10591
10592 return startp;
10593 }
10594
10595
10596 /* Make sure the line containing the cursor is fully visible.
10597 A value of 1 means there is nothing to be done.
10598 (Either the line is fully visible, or it cannot be made so,
10599 or we cannot tell.)
10600 A value of 0 means the caller should do scrolling
10601 as if point had gone off the screen. */
10602
10603 static int
10604 make_cursor_line_fully_visible (w)
10605 struct window *w;
10606 {
10607 struct glyph_matrix *matrix;
10608 struct glyph_row *row;
10609 int window_height;
10610
10611 /* It's not always possible to find the cursor, e.g, when a window
10612 is full of overlay strings. Don't do anything in that case. */
10613 if (w->cursor.vpos < 0)
10614 return 1;
10615
10616 matrix = w->desired_matrix;
10617 row = MATRIX_ROW (matrix, w->cursor.vpos);
10618
10619 /* If the cursor row is not partially visible, there's nothing to do. */
10620 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
10621 return 1;
10622
10623 /* If the row the cursor is in is taller than the window's height,
10624 it's not clear what to do, so do nothing. */
10625 window_height = window_box_height (w);
10626 if (row->height >= window_height)
10627 return 1;
10628
10629 return 0;
10630
10631 #if 0
10632 /* This code used to try to scroll the window just enough to make
10633 the line visible. It returned 0 to say that the caller should
10634 allocate larger glyph matrices. */
10635
10636 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
10637 {
10638 int dy = row->height - row->visible_height;
10639 w->vscroll = 0;
10640 w->cursor.y += dy;
10641 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10642 }
10643 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
10644 {
10645 int dy = - (row->height - row->visible_height);
10646 w->vscroll = dy;
10647 w->cursor.y += dy;
10648 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10649 }
10650
10651 /* When we change the cursor y-position of the selected window,
10652 change this_line_y as well so that the display optimization for
10653 the cursor line of the selected window in redisplay_internal uses
10654 the correct y-position. */
10655 if (w == XWINDOW (selected_window))
10656 this_line_y = w->cursor.y;
10657
10658 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
10659 redisplay with larger matrices. */
10660 if (matrix->nrows < required_matrix_height (w))
10661 {
10662 fonts_changed_p = 1;
10663 return 0;
10664 }
10665
10666 return 1;
10667 #endif /* 0 */
10668 }
10669
10670
10671 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
10672 non-zero means only WINDOW is redisplayed in redisplay_internal.
10673 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
10674 in redisplay_window to bring a partially visible line into view in
10675 the case that only the cursor has moved.
10676
10677 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
10678 last screen line's vertical height extends past the end of the screen.
10679
10680 Value is
10681
10682 1 if scrolling succeeded
10683
10684 0 if scrolling didn't find point.
10685
10686 -1 if new fonts have been loaded so that we must interrupt
10687 redisplay, adjust glyph matrices, and try again. */
10688
10689 enum
10690 {
10691 SCROLLING_SUCCESS,
10692 SCROLLING_FAILED,
10693 SCROLLING_NEED_LARGER_MATRICES
10694 };
10695
10696 static int
10697 try_scrolling (window, just_this_one_p, scroll_conservatively,
10698 scroll_step, temp_scroll_step, last_line_misfit)
10699 Lisp_Object window;
10700 int just_this_one_p;
10701 EMACS_INT scroll_conservatively, scroll_step;
10702 int temp_scroll_step;
10703 int last_line_misfit;
10704 {
10705 struct window *w = XWINDOW (window);
10706 struct frame *f = XFRAME (w->frame);
10707 struct text_pos scroll_margin_pos;
10708 struct text_pos pos;
10709 struct text_pos startp;
10710 struct it it;
10711 Lisp_Object window_end;
10712 int this_scroll_margin;
10713 int dy = 0;
10714 int scroll_max;
10715 int rc;
10716 int amount_to_scroll = 0;
10717 Lisp_Object aggressive;
10718 int height;
10719 int end_scroll_margin;
10720
10721 #if GLYPH_DEBUG
10722 debug_method_add (w, "try_scrolling");
10723 #endif
10724
10725 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10726
10727 /* Compute scroll margin height in pixels. We scroll when point is
10728 within this distance from the top or bottom of the window. */
10729 if (scroll_margin > 0)
10730 {
10731 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
10732 this_scroll_margin *= CANON_Y_UNIT (f);
10733 }
10734 else
10735 this_scroll_margin = 0;
10736
10737 /* Compute how much we should try to scroll maximally to bring point
10738 into view. */
10739 if (scroll_step || scroll_conservatively || temp_scroll_step)
10740 scroll_max = max (scroll_step,
10741 max (scroll_conservatively, temp_scroll_step));
10742 else if (NUMBERP (current_buffer->scroll_down_aggressively)
10743 || NUMBERP (current_buffer->scroll_up_aggressively))
10744 /* We're trying to scroll because of aggressive scrolling
10745 but no scroll_step is set. Choose an arbitrary one. Maybe
10746 there should be a variable for this. */
10747 scroll_max = 10;
10748 else
10749 scroll_max = 0;
10750 scroll_max *= CANON_Y_UNIT (f);
10751
10752 /* Decide whether we have to scroll down. Start at the window end
10753 and move this_scroll_margin up to find the position of the scroll
10754 margin. */
10755 window_end = Fwindow_end (window, Qt);
10756
10757 too_near_end:
10758
10759 CHARPOS (scroll_margin_pos) = XINT (window_end);
10760 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
10761
10762 end_scroll_margin = this_scroll_margin + !!last_line_misfit;
10763 if (end_scroll_margin)
10764 {
10765 start_display (&it, w, scroll_margin_pos);
10766 move_it_vertically (&it, - end_scroll_margin);
10767 scroll_margin_pos = it.current.pos;
10768 }
10769
10770 if (PT >= CHARPOS (scroll_margin_pos))
10771 {
10772 int y0;
10773
10774 /* Point is in the scroll margin at the bottom of the window, or
10775 below. Compute a new window start that makes point visible. */
10776
10777 /* Compute the distance from the scroll margin to PT.
10778 Give up if the distance is greater than scroll_max. */
10779 start_display (&it, w, scroll_margin_pos);
10780 y0 = it.current_y;
10781 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10782 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10783
10784 /* To make point visible, we have to move the window start
10785 down so that the line the cursor is in is visible, which
10786 means we have to add in the height of the cursor line. */
10787 dy = line_bottom_y (&it) - y0;
10788
10789 if (dy > scroll_max)
10790 return SCROLLING_FAILED;
10791
10792 /* Move the window start down. If scrolling conservatively,
10793 move it just enough down to make point visible. If
10794 scroll_step is set, move it down by scroll_step. */
10795 start_display (&it, w, startp);
10796
10797 if (scroll_conservatively)
10798 /* Set AMOUNT_TO_SCROLL to at least one line,
10799 and at most scroll_conservatively lines. */
10800 amount_to_scroll
10801 = min (max (dy, CANON_Y_UNIT (f)),
10802 CANON_Y_UNIT (f) * scroll_conservatively);
10803 else if (scroll_step || temp_scroll_step)
10804 amount_to_scroll = scroll_max;
10805 else
10806 {
10807 aggressive = current_buffer->scroll_up_aggressively;
10808 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
10809 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10810 if (NUMBERP (aggressive))
10811 amount_to_scroll = XFLOATINT (aggressive) * height;
10812 }
10813
10814 if (amount_to_scroll <= 0)
10815 return SCROLLING_FAILED;
10816
10817 /* If moving by amount_to_scroll leaves STARTP unchanged,
10818 move it down one screen line. */
10819
10820 move_it_vertically (&it, amount_to_scroll);
10821 if (CHARPOS (it.current.pos) == CHARPOS (startp))
10822 move_it_by_lines (&it, 1, 1);
10823 startp = it.current.pos;
10824 }
10825 else
10826 {
10827 /* See if point is inside the scroll margin at the top of the
10828 window. */
10829 scroll_margin_pos = startp;
10830 if (this_scroll_margin)
10831 {
10832 start_display (&it, w, startp);
10833 move_it_vertically (&it, this_scroll_margin);
10834 scroll_margin_pos = it.current.pos;
10835 }
10836
10837 if (PT < CHARPOS (scroll_margin_pos))
10838 {
10839 /* Point is in the scroll margin at the top of the window or
10840 above what is displayed in the window. */
10841 int y0;
10842
10843 /* Compute the vertical distance from PT to the scroll
10844 margin position. Give up if distance is greater than
10845 scroll_max. */
10846 SET_TEXT_POS (pos, PT, PT_BYTE);
10847 start_display (&it, w, pos);
10848 y0 = it.current_y;
10849 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
10850 it.last_visible_y, -1,
10851 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10852 dy = it.current_y - y0;
10853 if (dy > scroll_max)
10854 return SCROLLING_FAILED;
10855
10856 /* Compute new window start. */
10857 start_display (&it, w, startp);
10858
10859 if (scroll_conservatively)
10860 amount_to_scroll =
10861 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
10862 else if (scroll_step || temp_scroll_step)
10863 amount_to_scroll = scroll_max;
10864 else
10865 {
10866 aggressive = current_buffer->scroll_down_aggressively;
10867 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
10868 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10869 if (NUMBERP (aggressive))
10870 amount_to_scroll = XFLOATINT (aggressive) * height;
10871 }
10872
10873 if (amount_to_scroll <= 0)
10874 return SCROLLING_FAILED;
10875
10876 move_it_vertically (&it, - amount_to_scroll);
10877 startp = it.current.pos;
10878 }
10879 }
10880
10881 /* Run window scroll functions. */
10882 startp = run_window_scroll_functions (window, startp);
10883
10884 /* Display the window. Give up if new fonts are loaded, or if point
10885 doesn't appear. */
10886 if (!try_window (window, startp))
10887 rc = SCROLLING_NEED_LARGER_MATRICES;
10888 else if (w->cursor.vpos < 0)
10889 {
10890 clear_glyph_matrix (w->desired_matrix);
10891 rc = SCROLLING_FAILED;
10892 }
10893 else
10894 {
10895 /* Maybe forget recorded base line for line number display. */
10896 if (!just_this_one_p
10897 || current_buffer->clip_changed
10898 || BEG_UNCHANGED < CHARPOS (startp))
10899 w->base_line_number = Qnil;
10900
10901 /* If cursor ends up on a partially visible line,
10902 treat that as being off the bottom of the screen. */
10903 if (! make_cursor_line_fully_visible (w))
10904 {
10905 clear_glyph_matrix (w->desired_matrix);
10906 last_line_misfit = 1;
10907 goto too_near_end;
10908 }
10909 rc = SCROLLING_SUCCESS;
10910 }
10911
10912 return rc;
10913 }
10914
10915
10916 /* Compute a suitable window start for window W if display of W starts
10917 on a continuation line. Value is non-zero if a new window start
10918 was computed.
10919
10920 The new window start will be computed, based on W's width, starting
10921 from the start of the continued line. It is the start of the
10922 screen line with the minimum distance from the old start W->start. */
10923
10924 static int
10925 compute_window_start_on_continuation_line (w)
10926 struct window *w;
10927 {
10928 struct text_pos pos, start_pos;
10929 int window_start_changed_p = 0;
10930
10931 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
10932
10933 /* If window start is on a continuation line... Window start may be
10934 < BEGV in case there's invisible text at the start of the
10935 buffer (M-x rmail, for example). */
10936 if (CHARPOS (start_pos) > BEGV
10937 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
10938 {
10939 struct it it;
10940 struct glyph_row *row;
10941
10942 /* Handle the case that the window start is out of range. */
10943 if (CHARPOS (start_pos) < BEGV)
10944 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
10945 else if (CHARPOS (start_pos) > ZV)
10946 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
10947
10948 /* Find the start of the continued line. This should be fast
10949 because scan_buffer is fast (newline cache). */
10950 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
10951 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
10952 row, DEFAULT_FACE_ID);
10953 reseat_at_previous_visible_line_start (&it);
10954
10955 /* If the line start is "too far" away from the window start,
10956 say it takes too much time to compute a new window start. */
10957 if (CHARPOS (start_pos) - IT_CHARPOS (it)
10958 < XFASTINT (w->height) * XFASTINT (w->width))
10959 {
10960 int min_distance, distance;
10961
10962 /* Move forward by display lines to find the new window
10963 start. If window width was enlarged, the new start can
10964 be expected to be > the old start. If window width was
10965 decreased, the new window start will be < the old start.
10966 So, we're looking for the display line start with the
10967 minimum distance from the old window start. */
10968 pos = it.current.pos;
10969 min_distance = INFINITY;
10970 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
10971 distance < min_distance)
10972 {
10973 min_distance = distance;
10974 pos = it.current.pos;
10975 move_it_by_lines (&it, 1, 0);
10976 }
10977
10978 /* Set the window start there. */
10979 SET_MARKER_FROM_TEXT_POS (w->start, pos);
10980 window_start_changed_p = 1;
10981 }
10982 }
10983
10984 return window_start_changed_p;
10985 }
10986
10987
10988 /* Try cursor movement in case text has not changed in window WINDOW,
10989 with window start STARTP. Value is
10990
10991 CURSOR_MOVEMENT_SUCCESS if successful
10992
10993 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
10994
10995 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
10996 display. *SCROLL_STEP is set to 1, under certain circumstances, if
10997 we want to scroll as if scroll-step were set to 1. See the code.
10998
10999 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11000 which case we have to abort this redisplay, and adjust matrices
11001 first. */
11002
11003 enum
11004 {
11005 CURSOR_MOVEMENT_SUCCESS,
11006 CURSOR_MOVEMENT_CANNOT_BE_USED,
11007 CURSOR_MOVEMENT_MUST_SCROLL,
11008 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11009 };
11010
11011 static int
11012 try_cursor_movement (window, startp, scroll_step)
11013 Lisp_Object window;
11014 struct text_pos startp;
11015 int *scroll_step;
11016 {
11017 struct window *w = XWINDOW (window);
11018 struct frame *f = XFRAME (w->frame);
11019 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11020
11021 #if GLYPH_DEBUG
11022 if (inhibit_try_cursor_movement)
11023 return rc;
11024 #endif
11025
11026 /* Handle case where text has not changed, only point, and it has
11027 not moved off the frame. */
11028 if (/* Point may be in this window. */
11029 PT >= CHARPOS (startp)
11030 /* Selective display hasn't changed. */
11031 && !current_buffer->clip_changed
11032 /* Function force-mode-line-update is used to force a thorough
11033 redisplay. It sets either windows_or_buffers_changed or
11034 update_mode_lines. So don't take a shortcut here for these
11035 cases. */
11036 && !update_mode_lines
11037 && !windows_or_buffers_changed
11038 && !cursor_type_changed
11039 /* Can't use this case if highlighting a region. When a
11040 region exists, cursor movement has to do more than just
11041 set the cursor. */
11042 && !(!NILP (Vtransient_mark_mode)
11043 && !NILP (current_buffer->mark_active))
11044 && NILP (w->region_showing)
11045 && NILP (Vshow_trailing_whitespace)
11046 /* Right after splitting windows, last_point may be nil. */
11047 && INTEGERP (w->last_point)
11048 /* This code is not used for mini-buffer for the sake of the case
11049 of redisplaying to replace an echo area message; since in
11050 that case the mini-buffer contents per se are usually
11051 unchanged. This code is of no real use in the mini-buffer
11052 since the handling of this_line_start_pos, etc., in redisplay
11053 handles the same cases. */
11054 && !EQ (window, minibuf_window)
11055 /* When splitting windows or for new windows, it happens that
11056 redisplay is called with a nil window_end_vpos or one being
11057 larger than the window. This should really be fixed in
11058 window.c. I don't have this on my list, now, so we do
11059 approximately the same as the old redisplay code. --gerd. */
11060 && INTEGERP (w->window_end_vpos)
11061 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11062 && (FRAME_WINDOW_P (f)
11063 || !MARKERP (Voverlay_arrow_position)
11064 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
11065 {
11066 int this_scroll_margin;
11067 struct glyph_row *row = NULL;
11068
11069 #if GLYPH_DEBUG
11070 debug_method_add (w, "cursor movement");
11071 #endif
11072
11073 /* Scroll if point within this distance from the top or bottom
11074 of the window. This is a pixel value. */
11075 this_scroll_margin = max (0, scroll_margin);
11076 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
11077 this_scroll_margin *= CANON_Y_UNIT (f);
11078
11079 /* Start with the row the cursor was displayed during the last
11080 not paused redisplay. Give up if that row is not valid. */
11081 if (w->last_cursor.vpos < 0
11082 || w->last_cursor.vpos >= w->current_matrix->nrows)
11083 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11084 else
11085 {
11086 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11087 if (row->mode_line_p)
11088 ++row;
11089 if (!row->enabled_p)
11090 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11091 }
11092
11093 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11094 {
11095 int scroll_p = 0;
11096 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11097
11098 if (PT > XFASTINT (w->last_point))
11099 {
11100 /* Point has moved forward. */
11101 while (MATRIX_ROW_END_CHARPOS (row) < PT
11102 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11103 {
11104 xassert (row->enabled_p);
11105 ++row;
11106 }
11107
11108 /* The end position of a row equals the start position
11109 of the next row. If PT is there, we would rather
11110 display it in the next line. */
11111 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11112 && MATRIX_ROW_END_CHARPOS (row) == PT
11113 && !cursor_row_p (w, row))
11114 ++row;
11115
11116 /* If within the scroll margin, scroll. Note that
11117 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11118 the next line would be drawn, and that
11119 this_scroll_margin can be zero. */
11120 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11121 || PT > MATRIX_ROW_END_CHARPOS (row)
11122 /* Line is completely visible last line in window
11123 and PT is to be set in the next line. */
11124 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11125 && PT == MATRIX_ROW_END_CHARPOS (row)
11126 && !row->ends_at_zv_p
11127 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11128 scroll_p = 1;
11129 }
11130 else if (PT < XFASTINT (w->last_point))
11131 {
11132 /* Cursor has to be moved backward. Note that PT >=
11133 CHARPOS (startp) because of the outer
11134 if-statement. */
11135 while (!row->mode_line_p
11136 && (MATRIX_ROW_START_CHARPOS (row) > PT
11137 || (MATRIX_ROW_START_CHARPOS (row) == PT
11138 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11139 && (row->y > this_scroll_margin
11140 || CHARPOS (startp) == BEGV))
11141 {
11142 xassert (row->enabled_p);
11143 --row;
11144 }
11145
11146 /* Consider the following case: Window starts at BEGV,
11147 there is invisible, intangible text at BEGV, so that
11148 display starts at some point START > BEGV. It can
11149 happen that we are called with PT somewhere between
11150 BEGV and START. Try to handle that case. */
11151 if (row < w->current_matrix->rows
11152 || row->mode_line_p)
11153 {
11154 row = w->current_matrix->rows;
11155 if (row->mode_line_p)
11156 ++row;
11157 }
11158
11159 /* Due to newlines in overlay strings, we may have to
11160 skip forward over overlay strings. */
11161 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11162 && MATRIX_ROW_END_CHARPOS (row) == PT
11163 && !cursor_row_p (w, row))
11164 ++row;
11165
11166 /* If within the scroll margin, scroll. */
11167 if (row->y < this_scroll_margin
11168 && CHARPOS (startp) != BEGV)
11169 scroll_p = 1;
11170 }
11171
11172 if (PT < MATRIX_ROW_START_CHARPOS (row)
11173 || PT > MATRIX_ROW_END_CHARPOS (row))
11174 {
11175 /* if PT is not in the glyph row, give up. */
11176 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11177 }
11178 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
11179 {
11180 if (PT == MATRIX_ROW_END_CHARPOS (row)
11181 && !row->ends_at_zv_p
11182 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11183 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11184 else if (row->height > window_box_height (w))
11185 {
11186 /* If we end up in a partially visible line, let's
11187 make it fully visible, except when it's taller
11188 than the window, in which case we can't do much
11189 about it. */
11190 *scroll_step = 1;
11191 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11192 }
11193 else
11194 {
11195 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11196 if (!make_cursor_line_fully_visible (w))
11197 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11198 else
11199 rc = CURSOR_MOVEMENT_SUCCESS;
11200 }
11201 }
11202 else if (scroll_p)
11203 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11204 else
11205 {
11206 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11207 rc = CURSOR_MOVEMENT_SUCCESS;
11208 }
11209 }
11210 }
11211
11212 return rc;
11213 }
11214
11215 void
11216 set_vertical_scroll_bar (w)
11217 struct window *w;
11218 {
11219 int start, end, whole;
11220
11221 /* Calculate the start and end positions for the current window.
11222 At some point, it would be nice to choose between scrollbars
11223 which reflect the whole buffer size, with special markers
11224 indicating narrowing, and scrollbars which reflect only the
11225 visible region.
11226
11227 Note that mini-buffers sometimes aren't displaying any text. */
11228 if (!MINI_WINDOW_P (w)
11229 || (w == XWINDOW (minibuf_window)
11230 && NILP (echo_area_buffer[0])))
11231 {
11232 struct buffer *buf = XBUFFER (w->buffer);
11233 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11234 start = marker_position (w->start) - BUF_BEGV (buf);
11235 /* I don't think this is guaranteed to be right. For the
11236 moment, we'll pretend it is. */
11237 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11238
11239 if (end < start)
11240 end = start;
11241 if (whole < (end - start))
11242 whole = end - start;
11243 }
11244 else
11245 start = end = whole = 0;
11246
11247 /* Indicate what this scroll bar ought to be displaying now. */
11248 set_vertical_scroll_bar_hook (w, end - start, whole, start);
11249 }
11250
11251 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11252 selected_window is redisplayed.
11253
11254 We can return without actually redisplaying the window if
11255 fonts_changed_p is nonzero. In that case, redisplay_internal will
11256 retry. */
11257
11258 static void
11259 redisplay_window (window, just_this_one_p)
11260 Lisp_Object window;
11261 int just_this_one_p;
11262 {
11263 struct window *w = XWINDOW (window);
11264 struct frame *f = XFRAME (w->frame);
11265 struct buffer *buffer = XBUFFER (w->buffer);
11266 struct buffer *old = current_buffer;
11267 struct text_pos lpoint, opoint, startp;
11268 int update_mode_line;
11269 int tem;
11270 struct it it;
11271 /* Record it now because it's overwritten. */
11272 int current_matrix_up_to_date_p = 0;
11273 /* This is less strict than current_matrix_up_to_date_p.
11274 It indictes that the buffer contents and narrowing are unchanged. */
11275 int buffer_unchanged_p = 0;
11276 int temp_scroll_step = 0;
11277 int count = SPECPDL_INDEX ();
11278 int rc;
11279 int centering_position;
11280 int last_line_misfit = 0;
11281
11282 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11283 opoint = lpoint;
11284
11285 /* W must be a leaf window here. */
11286 xassert (!NILP (w->buffer));
11287 #if GLYPH_DEBUG
11288 *w->desired_matrix->method = 0;
11289 #endif
11290
11291 specbind (Qinhibit_point_motion_hooks, Qt);
11292
11293 reconsider_clip_changes (w, buffer);
11294
11295 /* Has the mode line to be updated? */
11296 update_mode_line = (!NILP (w->update_mode_line)
11297 || update_mode_lines
11298 || buffer->clip_changed
11299 || buffer->prevent_redisplay_optimizations_p);
11300
11301 if (MINI_WINDOW_P (w))
11302 {
11303 if (w == XWINDOW (echo_area_window)
11304 && !NILP (echo_area_buffer[0]))
11305 {
11306 if (update_mode_line)
11307 /* We may have to update a tty frame's menu bar or a
11308 tool-bar. Example `M-x C-h C-h C-g'. */
11309 goto finish_menu_bars;
11310 else
11311 /* We've already displayed the echo area glyphs in this window. */
11312 goto finish_scroll_bars;
11313 }
11314 else if ((w != XWINDOW (minibuf_window)
11315 || minibuf_level == 0)
11316 /* When buffer is nonempty, redisplay window normally. */
11317 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
11318 /* Quail displays non-mini buffers in minibuffer window.
11319 In that case, redisplay the window normally. */
11320 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
11321 {
11322 /* W is a mini-buffer window, but it's not active, so clear
11323 it. */
11324 int yb = window_text_bottom_y (w);
11325 struct glyph_row *row;
11326 int y;
11327
11328 for (y = 0, row = w->desired_matrix->rows;
11329 y < yb;
11330 y += row->height, ++row)
11331 blank_row (w, row, y);
11332 goto finish_scroll_bars;
11333 }
11334
11335 clear_glyph_matrix (w->desired_matrix);
11336 }
11337
11338 /* Otherwise set up data on this window; select its buffer and point
11339 value. */
11340 /* Really select the buffer, for the sake of buffer-local
11341 variables. */
11342 set_buffer_internal_1 (XBUFFER (w->buffer));
11343 SET_TEXT_POS (opoint, PT, PT_BYTE);
11344
11345 current_matrix_up_to_date_p
11346 = (!NILP (w->window_end_valid)
11347 && !current_buffer->clip_changed
11348 && !current_buffer->prevent_redisplay_optimizations_p
11349 && XFASTINT (w->last_modified) >= MODIFF
11350 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11351
11352 buffer_unchanged_p
11353 = (!NILP (w->window_end_valid)
11354 && !current_buffer->clip_changed
11355 && XFASTINT (w->last_modified) >= MODIFF
11356 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11357
11358 /* When windows_or_buffers_changed is non-zero, we can't rely on
11359 the window end being valid, so set it to nil there. */
11360 if (windows_or_buffers_changed)
11361 {
11362 /* If window starts on a continuation line, maybe adjust the
11363 window start in case the window's width changed. */
11364 if (XMARKER (w->start)->buffer == current_buffer)
11365 compute_window_start_on_continuation_line (w);
11366
11367 w->window_end_valid = Qnil;
11368 }
11369
11370 /* Some sanity checks. */
11371 CHECK_WINDOW_END (w);
11372 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
11373 abort ();
11374 if (BYTEPOS (opoint) < CHARPOS (opoint))
11375 abort ();
11376
11377 /* If %c is in mode line, update it if needed. */
11378 if (!NILP (w->column_number_displayed)
11379 /* This alternative quickly identifies a common case
11380 where no change is needed. */
11381 && !(PT == XFASTINT (w->last_point)
11382 && XFASTINT (w->last_modified) >= MODIFF
11383 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11384 && (XFASTINT (w->column_number_displayed)
11385 != (int) current_column ())) /* iftc */
11386 update_mode_line = 1;
11387
11388 /* Count number of windows showing the selected buffer. An indirect
11389 buffer counts as its base buffer. */
11390 if (!just_this_one_p)
11391 {
11392 struct buffer *current_base, *window_base;
11393 current_base = current_buffer;
11394 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11395 if (current_base->base_buffer)
11396 current_base = current_base->base_buffer;
11397 if (window_base->base_buffer)
11398 window_base = window_base->base_buffer;
11399 if (current_base == window_base)
11400 buffer_shared++;
11401 }
11402
11403 /* Point refers normally to the selected window. For any other
11404 window, set up appropriate value. */
11405 if (!EQ (window, selected_window))
11406 {
11407 int new_pt = XMARKER (w->pointm)->charpos;
11408 int new_pt_byte = marker_byte_position (w->pointm);
11409 if (new_pt < BEGV)
11410 {
11411 new_pt = BEGV;
11412 new_pt_byte = BEGV_BYTE;
11413 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
11414 }
11415 else if (new_pt > (ZV - 1))
11416 {
11417 new_pt = ZV;
11418 new_pt_byte = ZV_BYTE;
11419 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
11420 }
11421
11422 /* We don't use SET_PT so that the point-motion hooks don't run. */
11423 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
11424 }
11425
11426 /* If any of the character widths specified in the display table
11427 have changed, invalidate the width run cache. It's true that
11428 this may be a bit late to catch such changes, but the rest of
11429 redisplay goes (non-fatally) haywire when the display table is
11430 changed, so why should we worry about doing any better? */
11431 if (current_buffer->width_run_cache)
11432 {
11433 struct Lisp_Char_Table *disptab = buffer_display_table ();
11434
11435 if (! disptab_matches_widthtab (disptab,
11436 XVECTOR (current_buffer->width_table)))
11437 {
11438 invalidate_region_cache (current_buffer,
11439 current_buffer->width_run_cache,
11440 BEG, Z);
11441 recompute_width_table (current_buffer, disptab);
11442 }
11443 }
11444
11445 /* If window-start is screwed up, choose a new one. */
11446 if (XMARKER (w->start)->buffer != current_buffer)
11447 goto recenter;
11448
11449 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11450
11451 /* If someone specified a new starting point but did not insist,
11452 check whether it can be used. */
11453 if (!NILP (w->optional_new_start)
11454 && CHARPOS (startp) >= BEGV
11455 && CHARPOS (startp) <= ZV)
11456 {
11457 w->optional_new_start = Qnil;
11458 start_display (&it, w, startp);
11459 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11460 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11461 if (IT_CHARPOS (it) == PT)
11462 w->force_start = Qt;
11463 }
11464
11465 /* Handle case where place to start displaying has been specified,
11466 unless the specified location is outside the accessible range. */
11467 if (!NILP (w->force_start)
11468 || w->frozen_window_start_p)
11469 {
11470 /* We set this later on if we have to adjust point. */
11471 int new_vpos = -1;
11472
11473 w->force_start = Qnil;
11474 w->vscroll = 0;
11475 w->window_end_valid = Qnil;
11476
11477 /* Forget any recorded base line for line number display. */
11478 if (!buffer_unchanged_p)
11479 w->base_line_number = Qnil;
11480
11481 /* Redisplay the mode line. Select the buffer properly for that.
11482 Also, run the hook window-scroll-functions
11483 because we have scrolled. */
11484 /* Note, we do this after clearing force_start because
11485 if there's an error, it is better to forget about force_start
11486 than to get into an infinite loop calling the hook functions
11487 and having them get more errors. */
11488 if (!update_mode_line
11489 || ! NILP (Vwindow_scroll_functions))
11490 {
11491 update_mode_line = 1;
11492 w->update_mode_line = Qt;
11493 startp = run_window_scroll_functions (window, startp);
11494 }
11495
11496 w->last_modified = make_number (0);
11497 w->last_overlay_modified = make_number (0);
11498 if (CHARPOS (startp) < BEGV)
11499 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
11500 else if (CHARPOS (startp) > ZV)
11501 SET_TEXT_POS (startp, ZV, ZV_BYTE);
11502
11503 /* Redisplay, then check if cursor has been set during the
11504 redisplay. Give up if new fonts were loaded. */
11505 if (!try_window (window, startp))
11506 {
11507 w->force_start = Qt;
11508 clear_glyph_matrix (w->desired_matrix);
11509 goto need_larger_matrices;
11510 }
11511
11512 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
11513 {
11514 /* If point does not appear, try to move point so it does
11515 appear. The desired matrix has been built above, so we
11516 can use it here. */
11517 new_vpos = window_box_height (w) / 2;
11518 }
11519
11520 if (!make_cursor_line_fully_visible (w))
11521 {
11522 /* Point does appear, but on a line partly visible at end of window.
11523 Move it back to a fully-visible line. */
11524 new_vpos = window_box_height (w);
11525 }
11526
11527 /* If we need to move point for either of the above reasons,
11528 now actually do it. */
11529 if (new_vpos >= 0)
11530 {
11531 struct glyph_row *row;
11532
11533 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
11534 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
11535 ++row;
11536
11537 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
11538 MATRIX_ROW_START_BYTEPOS (row));
11539
11540 if (w != XWINDOW (selected_window))
11541 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
11542 else if (current_buffer == old)
11543 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11544
11545 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
11546
11547 /* If we are highlighting the region, then we just changed
11548 the region, so redisplay to show it. */
11549 if (!NILP (Vtransient_mark_mode)
11550 && !NILP (current_buffer->mark_active))
11551 {
11552 clear_glyph_matrix (w->desired_matrix);
11553 if (!try_window (window, startp))
11554 goto need_larger_matrices;
11555 }
11556 }
11557
11558 #if GLYPH_DEBUG
11559 debug_method_add (w, "forced window start");
11560 #endif
11561 goto done;
11562 }
11563
11564 /* Handle case where text has not changed, only point, and it has
11565 not moved off the frame, and we are not retrying after hscroll.
11566 (current_matrix_up_to_date_p is nonzero when retrying.) */
11567 if (current_matrix_up_to_date_p
11568 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
11569 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
11570 {
11571 switch (rc)
11572 {
11573 case CURSOR_MOVEMENT_SUCCESS:
11574 goto done;
11575
11576 #if 0 /* try_cursor_movement never returns this value. */
11577 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
11578 goto need_larger_matrices;
11579 #endif
11580
11581 case CURSOR_MOVEMENT_MUST_SCROLL:
11582 goto try_to_scroll;
11583
11584 default:
11585 abort ();
11586 }
11587 }
11588 /* If current starting point was originally the beginning of a line
11589 but no longer is, find a new starting point. */
11590 else if (!NILP (w->start_at_line_beg)
11591 && !(CHARPOS (startp) <= BEGV
11592 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
11593 {
11594 #if GLYPH_DEBUG
11595 debug_method_add (w, "recenter 1");
11596 #endif
11597 goto recenter;
11598 }
11599
11600 /* Try scrolling with try_window_id. Value is > 0 if update has
11601 been done, it is -1 if we know that the same window start will
11602 not work. It is 0 if unsuccessful for some other reason. */
11603 else if ((tem = try_window_id (w)) != 0)
11604 {
11605 #if GLYPH_DEBUG
11606 debug_method_add (w, "try_window_id %d", tem);
11607 #endif
11608
11609 if (fonts_changed_p)
11610 goto need_larger_matrices;
11611 if (tem > 0)
11612 goto done;
11613
11614 /* Otherwise try_window_id has returned -1 which means that we
11615 don't want the alternative below this comment to execute. */
11616 }
11617 else if (CHARPOS (startp) >= BEGV
11618 && CHARPOS (startp) <= ZV
11619 && PT >= CHARPOS (startp)
11620 && (CHARPOS (startp) < ZV
11621 /* Avoid starting at end of buffer. */
11622 || CHARPOS (startp) == BEGV
11623 || (XFASTINT (w->last_modified) >= MODIFF
11624 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
11625 {
11626 #if GLYPH_DEBUG
11627 debug_method_add (w, "same window start");
11628 #endif
11629
11630 /* Try to redisplay starting at same place as before.
11631 If point has not moved off frame, accept the results. */
11632 if (!current_matrix_up_to_date_p
11633 /* Don't use try_window_reusing_current_matrix in this case
11634 because a window scroll function can have changed the
11635 buffer. */
11636 || !NILP (Vwindow_scroll_functions)
11637 || MINI_WINDOW_P (w)
11638 || !try_window_reusing_current_matrix (w))
11639 {
11640 IF_DEBUG (debug_method_add (w, "1"));
11641 try_window (window, startp);
11642 }
11643
11644 if (fonts_changed_p)
11645 goto need_larger_matrices;
11646
11647 if (w->cursor.vpos >= 0)
11648 {
11649 if (!just_this_one_p
11650 || current_buffer->clip_changed
11651 || BEG_UNCHANGED < CHARPOS (startp))
11652 /* Forget any recorded base line for line number display. */
11653 w->base_line_number = Qnil;
11654
11655 if (!make_cursor_line_fully_visible (w))
11656 {
11657 clear_glyph_matrix (w->desired_matrix);
11658 last_line_misfit = 1;
11659 }
11660 /* Drop through and scroll. */
11661 else
11662 goto done;
11663 }
11664 else
11665 clear_glyph_matrix (w->desired_matrix);
11666 }
11667
11668 try_to_scroll:
11669
11670 w->last_modified = make_number (0);
11671 w->last_overlay_modified = make_number (0);
11672
11673 /* Redisplay the mode line. Select the buffer properly for that. */
11674 if (!update_mode_line)
11675 {
11676 update_mode_line = 1;
11677 w->update_mode_line = Qt;
11678 }
11679
11680 /* Try to scroll by specified few lines. */
11681 if ((scroll_conservatively
11682 || scroll_step
11683 || temp_scroll_step
11684 || NUMBERP (current_buffer->scroll_up_aggressively)
11685 || NUMBERP (current_buffer->scroll_down_aggressively))
11686 && !current_buffer->clip_changed
11687 && CHARPOS (startp) >= BEGV
11688 && CHARPOS (startp) <= ZV)
11689 {
11690 /* The function returns -1 if new fonts were loaded, 1 if
11691 successful, 0 if not successful. */
11692 int rc = try_scrolling (window, just_this_one_p,
11693 scroll_conservatively,
11694 scroll_step,
11695 temp_scroll_step, last_line_misfit);
11696 switch (rc)
11697 {
11698 case SCROLLING_SUCCESS:
11699 goto done;
11700
11701 case SCROLLING_NEED_LARGER_MATRICES:
11702 goto need_larger_matrices;
11703
11704 case SCROLLING_FAILED:
11705 break;
11706
11707 default:
11708 abort ();
11709 }
11710 }
11711
11712 /* Finally, just choose place to start which centers point */
11713
11714 recenter:
11715 centering_position = window_box_height (w) / 2;
11716
11717 point_at_top:
11718 /* Jump here with centering_position already set to 0. */
11719
11720 #if GLYPH_DEBUG
11721 debug_method_add (w, "recenter");
11722 #endif
11723
11724 /* w->vscroll = 0; */
11725
11726 /* Forget any previously recorded base line for line number display. */
11727 if (!buffer_unchanged_p)
11728 w->base_line_number = Qnil;
11729
11730 /* Move backward half the height of the window. */
11731 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11732 it.current_y = it.last_visible_y;
11733 move_it_vertically_backward (&it, centering_position);
11734 xassert (IT_CHARPOS (it) >= BEGV);
11735
11736 /* The function move_it_vertically_backward may move over more
11737 than the specified y-distance. If it->w is small, e.g. a
11738 mini-buffer window, we may end up in front of the window's
11739 display area. Start displaying at the start of the line
11740 containing PT in this case. */
11741 if (it.current_y <= 0)
11742 {
11743 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11744 move_it_vertically (&it, 0);
11745 xassert (IT_CHARPOS (it) <= PT);
11746 it.current_y = 0;
11747 }
11748
11749 it.current_x = it.hpos = 0;
11750
11751 /* Set startp here explicitly in case that helps avoid an infinite loop
11752 in case the window-scroll-functions functions get errors. */
11753 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
11754
11755 /* Run scroll hooks. */
11756 startp = run_window_scroll_functions (window, it.current.pos);
11757
11758 /* Redisplay the window. */
11759 if (!current_matrix_up_to_date_p
11760 || windows_or_buffers_changed
11761 || cursor_type_changed
11762 /* Don't use try_window_reusing_current_matrix in this case
11763 because it can have changed the buffer. */
11764 || !NILP (Vwindow_scroll_functions)
11765 || !just_this_one_p
11766 || MINI_WINDOW_P (w)
11767 || !try_window_reusing_current_matrix (w))
11768 try_window (window, startp);
11769
11770 /* If new fonts have been loaded (due to fontsets), give up. We
11771 have to start a new redisplay since we need to re-adjust glyph
11772 matrices. */
11773 if (fonts_changed_p)
11774 goto need_larger_matrices;
11775
11776 /* If cursor did not appear assume that the middle of the window is
11777 in the first line of the window. Do it again with the next line.
11778 (Imagine a window of height 100, displaying two lines of height
11779 60. Moving back 50 from it->last_visible_y will end in the first
11780 line.) */
11781 if (w->cursor.vpos < 0)
11782 {
11783 if (!NILP (w->window_end_valid)
11784 && PT >= Z - XFASTINT (w->window_end_pos))
11785 {
11786 clear_glyph_matrix (w->desired_matrix);
11787 move_it_by_lines (&it, 1, 0);
11788 try_window (window, it.current.pos);
11789 }
11790 else if (PT < IT_CHARPOS (it))
11791 {
11792 clear_glyph_matrix (w->desired_matrix);
11793 move_it_by_lines (&it, -1, 0);
11794 try_window (window, it.current.pos);
11795 }
11796 else
11797 {
11798 /* Not much we can do about it. */
11799 }
11800 }
11801
11802 /* Consider the following case: Window starts at BEGV, there is
11803 invisible, intangible text at BEGV, so that display starts at
11804 some point START > BEGV. It can happen that we are called with
11805 PT somewhere between BEGV and START. Try to handle that case. */
11806 if (w->cursor.vpos < 0)
11807 {
11808 struct glyph_row *row = w->current_matrix->rows;
11809 if (row->mode_line_p)
11810 ++row;
11811 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11812 }
11813
11814 if (!make_cursor_line_fully_visible (w))
11815 {
11816 /* If vscroll is enabled, disable it and try again. */
11817 if (w->vscroll)
11818 {
11819 w->vscroll = 0;
11820 clear_glyph_matrix (w->desired_matrix);
11821 goto recenter;
11822 }
11823
11824 /* If centering point failed to make the whole line visible,
11825 put point at the top instead. That has to make the whole line
11826 visible, if it can be done. */
11827 centering_position = 0;
11828 goto point_at_top;
11829 }
11830
11831 done:
11832
11833 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11834 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
11835 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
11836 ? Qt : Qnil);
11837
11838 /* Display the mode line, if we must. */
11839 if ((update_mode_line
11840 /* If window not full width, must redo its mode line
11841 if (a) the window to its side is being redone and
11842 (b) we do a frame-based redisplay. This is a consequence
11843 of how inverted lines are drawn in frame-based redisplay. */
11844 || (!just_this_one_p
11845 && !FRAME_WINDOW_P (f)
11846 && !WINDOW_FULL_WIDTH_P (w))
11847 /* Line number to display. */
11848 || INTEGERP (w->base_line_pos)
11849 /* Column number is displayed and different from the one displayed. */
11850 || (!NILP (w->column_number_displayed)
11851 && (XFASTINT (w->column_number_displayed)
11852 != (int) current_column ()))) /* iftc */
11853 /* This means that the window has a mode line. */
11854 && (WINDOW_WANTS_MODELINE_P (w)
11855 || WINDOW_WANTS_HEADER_LINE_P (w)))
11856 {
11857 display_mode_lines (w);
11858
11859 /* If mode line height has changed, arrange for a thorough
11860 immediate redisplay using the correct mode line height. */
11861 if (WINDOW_WANTS_MODELINE_P (w)
11862 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
11863 {
11864 fonts_changed_p = 1;
11865 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
11866 = DESIRED_MODE_LINE_HEIGHT (w);
11867 }
11868
11869 /* If top line height has changed, arrange for a thorough
11870 immediate redisplay using the correct mode line height. */
11871 if (WINDOW_WANTS_HEADER_LINE_P (w)
11872 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
11873 {
11874 fonts_changed_p = 1;
11875 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
11876 = DESIRED_HEADER_LINE_HEIGHT (w);
11877 }
11878
11879 if (fonts_changed_p)
11880 goto need_larger_matrices;
11881 }
11882
11883 if (!line_number_displayed
11884 && !BUFFERP (w->base_line_pos))
11885 {
11886 w->base_line_pos = Qnil;
11887 w->base_line_number = Qnil;
11888 }
11889
11890 finish_menu_bars:
11891
11892 /* When we reach a frame's selected window, redo the frame's menu bar. */
11893 if (update_mode_line
11894 && EQ (FRAME_SELECTED_WINDOW (f), window))
11895 {
11896 int redisplay_menu_p = 0;
11897 int redisplay_tool_bar_p = 0;
11898
11899 if (FRAME_WINDOW_P (f))
11900 {
11901 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
11902 || defined (USE_GTK)
11903 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
11904 #else
11905 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
11906 #endif
11907 }
11908 else
11909 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
11910
11911 if (redisplay_menu_p)
11912 display_menu_bar (w);
11913
11914 #ifdef HAVE_WINDOW_SYSTEM
11915 #ifdef USE_GTK
11916 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
11917 #else
11918 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
11919 && (FRAME_TOOL_BAR_LINES (f) > 0
11920 || auto_resize_tool_bars_p);
11921
11922 #endif
11923
11924 if (redisplay_tool_bar_p)
11925 redisplay_tool_bar (f);
11926 #endif
11927 }
11928
11929 /* We go to this label, with fonts_changed_p nonzero,
11930 if it is necessary to try again using larger glyph matrices.
11931 We have to redeem the scroll bar even in this case,
11932 because the loop in redisplay_internal expects that. */
11933 need_larger_matrices:
11934 ;
11935 finish_scroll_bars:
11936
11937 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
11938 {
11939 /* Set the thumb's position and size. */
11940 set_vertical_scroll_bar (w);
11941
11942 /* Note that we actually used the scroll bar attached to this
11943 window, so it shouldn't be deleted at the end of redisplay. */
11944 redeem_scroll_bar_hook (w);
11945 }
11946
11947 /* Restore current_buffer and value of point in it. */
11948 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
11949 set_buffer_internal_1 (old);
11950 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
11951
11952 unbind_to (count, Qnil);
11953 }
11954
11955
11956 /* Build the complete desired matrix of WINDOW with a window start
11957 buffer position POS. Value is non-zero if successful. It is zero
11958 if fonts were loaded during redisplay which makes re-adjusting
11959 glyph matrices necessary. */
11960
11961 int
11962 try_window (window, pos)
11963 Lisp_Object window;
11964 struct text_pos pos;
11965 {
11966 struct window *w = XWINDOW (window);
11967 struct it it;
11968 struct glyph_row *last_text_row = NULL;
11969
11970 /* Make POS the new window start. */
11971 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
11972
11973 /* Mark cursor position as unknown. No overlay arrow seen. */
11974 w->cursor.vpos = -1;
11975 overlay_arrow_seen = 0;
11976
11977 /* Initialize iterator and info to start at POS. */
11978 start_display (&it, w, pos);
11979
11980 /* Display all lines of W. */
11981 while (it.current_y < it.last_visible_y)
11982 {
11983 if (display_line (&it))
11984 last_text_row = it.glyph_row - 1;
11985 if (fonts_changed_p)
11986 return 0;
11987 }
11988
11989 /* If bottom moved off end of frame, change mode line percentage. */
11990 if (XFASTINT (w->window_end_pos) <= 0
11991 && Z != IT_CHARPOS (it))
11992 w->update_mode_line = Qt;
11993
11994 /* Set window_end_pos to the offset of the last character displayed
11995 on the window from the end of current_buffer. Set
11996 window_end_vpos to its row number. */
11997 if (last_text_row)
11998 {
11999 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12000 w->window_end_bytepos
12001 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12002 w->window_end_pos
12003 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12004 w->window_end_vpos
12005 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12006 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12007 ->displays_text_p);
12008 }
12009 else
12010 {
12011 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12012 w->window_end_pos = make_number (Z - ZV);
12013 w->window_end_vpos = make_number (0);
12014 }
12015
12016 /* But that is not valid info until redisplay finishes. */
12017 w->window_end_valid = Qnil;
12018 return 1;
12019 }
12020
12021
12022 \f
12023 /************************************************************************
12024 Window redisplay reusing current matrix when buffer has not changed
12025 ************************************************************************/
12026
12027 /* Try redisplay of window W showing an unchanged buffer with a
12028 different window start than the last time it was displayed by
12029 reusing its current matrix. Value is non-zero if successful.
12030 W->start is the new window start. */
12031
12032 static int
12033 try_window_reusing_current_matrix (w)
12034 struct window *w;
12035 {
12036 struct frame *f = XFRAME (w->frame);
12037 struct glyph_row *row, *bottom_row;
12038 struct it it;
12039 struct run run;
12040 struct text_pos start, new_start;
12041 int nrows_scrolled, i;
12042 struct glyph_row *last_text_row;
12043 struct glyph_row *last_reused_text_row;
12044 struct glyph_row *start_row;
12045 int start_vpos, min_y, max_y;
12046
12047 #if GLYPH_DEBUG
12048 if (inhibit_try_window_reusing)
12049 return 0;
12050 #endif
12051
12052 if (/* This function doesn't handle terminal frames. */
12053 !FRAME_WINDOW_P (f)
12054 /* Don't try to reuse the display if windows have been split
12055 or such. */
12056 || windows_or_buffers_changed
12057 || cursor_type_changed)
12058 return 0;
12059
12060 /* Can't do this if region may have changed. */
12061 if ((!NILP (Vtransient_mark_mode)
12062 && !NILP (current_buffer->mark_active))
12063 || !NILP (w->region_showing)
12064 || !NILP (Vshow_trailing_whitespace))
12065 return 0;
12066
12067 /* If top-line visibility has changed, give up. */
12068 if (WINDOW_WANTS_HEADER_LINE_P (w)
12069 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12070 return 0;
12071
12072 /* Give up if old or new display is scrolled vertically. We could
12073 make this function handle this, but right now it doesn't. */
12074 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12075 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
12076 return 0;
12077
12078 /* The variable new_start now holds the new window start. The old
12079 start `start' can be determined from the current matrix. */
12080 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12081 start = start_row->start.pos;
12082 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12083
12084 /* Clear the desired matrix for the display below. */
12085 clear_glyph_matrix (w->desired_matrix);
12086
12087 if (CHARPOS (new_start) <= CHARPOS (start))
12088 {
12089 int first_row_y;
12090
12091 /* Don't use this method if the display starts with an ellipsis
12092 displayed for invisible text. It's not easy to handle that case
12093 below, and it's certainly not worth the effort since this is
12094 not a frequent case. */
12095 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12096 return 0;
12097
12098 IF_DEBUG (debug_method_add (w, "twu1"));
12099
12100 /* Display up to a row that can be reused. The variable
12101 last_text_row is set to the last row displayed that displays
12102 text. Note that it.vpos == 0 if or if not there is a
12103 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12104 start_display (&it, w, new_start);
12105 first_row_y = it.current_y;
12106 w->cursor.vpos = -1;
12107 last_text_row = last_reused_text_row = NULL;
12108
12109 while (it.current_y < it.last_visible_y
12110 && IT_CHARPOS (it) < CHARPOS (start)
12111 && !fonts_changed_p)
12112 if (display_line (&it))
12113 last_text_row = it.glyph_row - 1;
12114
12115 /* A value of current_y < last_visible_y means that we stopped
12116 at the previous window start, which in turn means that we
12117 have at least one reusable row. */
12118 if (it.current_y < it.last_visible_y)
12119 {
12120 /* IT.vpos always starts from 0; it counts text lines. */
12121 nrows_scrolled = it.vpos;
12122
12123 /* Find PT if not already found in the lines displayed. */
12124 if (w->cursor.vpos < 0)
12125 {
12126 int dy = it.current_y - first_row_y;
12127
12128 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12129 row = row_containing_pos (w, PT, row, NULL, dy);
12130 if (row)
12131 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12132 dy, nrows_scrolled);
12133 else
12134 {
12135 clear_glyph_matrix (w->desired_matrix);
12136 return 0;
12137 }
12138 }
12139
12140 /* Scroll the display. Do it before the current matrix is
12141 changed. The problem here is that update has not yet
12142 run, i.e. part of the current matrix is not up to date.
12143 scroll_run_hook will clear the cursor, and use the
12144 current matrix to get the height of the row the cursor is
12145 in. */
12146 run.current_y = first_row_y;
12147 run.desired_y = it.current_y;
12148 run.height = it.last_visible_y - it.current_y;
12149
12150 if (run.height > 0 && run.current_y != run.desired_y)
12151 {
12152 update_begin (f);
12153 rif->update_window_begin_hook (w);
12154 rif->clear_window_mouse_face (w);
12155 rif->scroll_run_hook (w, &run);
12156 rif->update_window_end_hook (w, 0, 0);
12157 update_end (f);
12158 }
12159
12160 /* Shift current matrix down by nrows_scrolled lines. */
12161 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12162 rotate_matrix (w->current_matrix,
12163 start_vpos,
12164 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12165 nrows_scrolled);
12166
12167 /* Disable lines that must be updated. */
12168 for (i = 0; i < it.vpos; ++i)
12169 (start_row + i)->enabled_p = 0;
12170
12171 /* Re-compute Y positions. */
12172 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
12173 max_y = it.last_visible_y;
12174 for (row = start_row + nrows_scrolled;
12175 row < bottom_row;
12176 ++row)
12177 {
12178 row->y = it.current_y;
12179 row->visible_height = row->height;
12180
12181 if (row->y < min_y)
12182 row->visible_height -= min_y - row->y;
12183 if (row->y + row->height > max_y)
12184 row->visible_height -= row->y + row->height - max_y;
12185
12186 it.current_y += row->height;
12187
12188 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12189 last_reused_text_row = row;
12190 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12191 break;
12192 }
12193
12194 /* Disable lines in the current matrix which are now
12195 below the window. */
12196 for (++row; row < bottom_row; ++row)
12197 row->enabled_p = 0;
12198 }
12199
12200 /* Update window_end_pos etc.; last_reused_text_row is the last
12201 reused row from the current matrix containing text, if any.
12202 The value of last_text_row is the last displayed line
12203 containing text. */
12204 if (last_reused_text_row)
12205 {
12206 w->window_end_bytepos
12207 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12208 w->window_end_pos
12209 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12210 w->window_end_vpos
12211 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12212 w->current_matrix));
12213 }
12214 else if (last_text_row)
12215 {
12216 w->window_end_bytepos
12217 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12218 w->window_end_pos
12219 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12220 w->window_end_vpos
12221 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12222 }
12223 else
12224 {
12225 /* This window must be completely empty. */
12226 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12227 w->window_end_pos = make_number (Z - ZV);
12228 w->window_end_vpos = make_number (0);
12229 }
12230 w->window_end_valid = Qnil;
12231
12232 /* Update hint: don't try scrolling again in update_window. */
12233 w->desired_matrix->no_scrolling_p = 1;
12234
12235 #if GLYPH_DEBUG
12236 debug_method_add (w, "try_window_reusing_current_matrix 1");
12237 #endif
12238 return 1;
12239 }
12240 else if (CHARPOS (new_start) > CHARPOS (start))
12241 {
12242 struct glyph_row *pt_row, *row;
12243 struct glyph_row *first_reusable_row;
12244 struct glyph_row *first_row_to_display;
12245 int dy;
12246 int yb = window_text_bottom_y (w);
12247
12248 /* Find the row starting at new_start, if there is one. Don't
12249 reuse a partially visible line at the end. */
12250 first_reusable_row = start_row;
12251 while (first_reusable_row->enabled_p
12252 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12253 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12254 < CHARPOS (new_start)))
12255 ++first_reusable_row;
12256
12257 /* Give up if there is no row to reuse. */
12258 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
12259 || !first_reusable_row->enabled_p
12260 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12261 != CHARPOS (new_start)))
12262 return 0;
12263
12264 /* We can reuse fully visible rows beginning with
12265 first_reusable_row to the end of the window. Set
12266 first_row_to_display to the first row that cannot be reused.
12267 Set pt_row to the row containing point, if there is any. */
12268 pt_row = NULL;
12269 for (first_row_to_display = first_reusable_row;
12270 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12271 ++first_row_to_display)
12272 {
12273 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12274 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
12275 pt_row = first_row_to_display;
12276 }
12277
12278 /* Start displaying at the start of first_row_to_display. */
12279 xassert (first_row_to_display->y < yb);
12280 init_to_row_start (&it, w, first_row_to_display);
12281
12282 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12283 - start_vpos);
12284 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12285 - nrows_scrolled);
12286 it.current_y = (first_row_to_display->y - first_reusable_row->y
12287 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
12288
12289 /* Display lines beginning with first_row_to_display in the
12290 desired matrix. Set last_text_row to the last row displayed
12291 that displays text. */
12292 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12293 if (pt_row == NULL)
12294 w->cursor.vpos = -1;
12295 last_text_row = NULL;
12296 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12297 if (display_line (&it))
12298 last_text_row = it.glyph_row - 1;
12299
12300 /* Give up If point isn't in a row displayed or reused. */
12301 if (w->cursor.vpos < 0)
12302 {
12303 clear_glyph_matrix (w->desired_matrix);
12304 return 0;
12305 }
12306
12307 /* If point is in a reused row, adjust y and vpos of the cursor
12308 position. */
12309 if (pt_row)
12310 {
12311 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
12312 w->current_matrix);
12313 w->cursor.y -= first_reusable_row->y;
12314 }
12315
12316 /* Scroll the display. */
12317 run.current_y = first_reusable_row->y;
12318 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
12319 run.height = it.last_visible_y - run.current_y;
12320 dy = run.current_y - run.desired_y;
12321
12322 if (run.height)
12323 {
12324 struct frame *f = XFRAME (WINDOW_FRAME (w));
12325 update_begin (f);
12326 rif->update_window_begin_hook (w);
12327 rif->clear_window_mouse_face (w);
12328 rif->scroll_run_hook (w, &run);
12329 rif->update_window_end_hook (w, 0, 0);
12330 update_end (f);
12331 }
12332
12333 /* Adjust Y positions of reused rows. */
12334 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12335 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
12336 max_y = it.last_visible_y;
12337 for (row = first_reusable_row; row < first_row_to_display; ++row)
12338 {
12339 row->y -= dy;
12340 row->visible_height = row->height;
12341 if (row->y < min_y)
12342 row->visible_height -= min_y - row->y;
12343 if (row->y + row->height > max_y)
12344 row->visible_height -= row->y + row->height - max_y;
12345 }
12346
12347 /* Scroll the current matrix. */
12348 xassert (nrows_scrolled > 0);
12349 rotate_matrix (w->current_matrix,
12350 start_vpos,
12351 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12352 -nrows_scrolled);
12353
12354 /* Disable rows not reused. */
12355 for (row -= nrows_scrolled; row < bottom_row; ++row)
12356 row->enabled_p = 0;
12357
12358 /* Adjust window end. A null value of last_text_row means that
12359 the window end is in reused rows which in turn means that
12360 only its vpos can have changed. */
12361 if (last_text_row)
12362 {
12363 w->window_end_bytepos
12364 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12365 w->window_end_pos
12366 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12367 w->window_end_vpos
12368 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12369 }
12370 else
12371 {
12372 w->window_end_vpos
12373 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
12374 }
12375
12376 w->window_end_valid = Qnil;
12377 w->desired_matrix->no_scrolling_p = 1;
12378
12379 #if GLYPH_DEBUG
12380 debug_method_add (w, "try_window_reusing_current_matrix 2");
12381 #endif
12382 return 1;
12383 }
12384
12385 return 0;
12386 }
12387
12388
12389 \f
12390 /************************************************************************
12391 Window redisplay reusing current matrix when buffer has changed
12392 ************************************************************************/
12393
12394 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
12395 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
12396 int *, int *));
12397 static struct glyph_row *
12398 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
12399 struct glyph_row *));
12400
12401
12402 /* Return the last row in MATRIX displaying text. If row START is
12403 non-null, start searching with that row. IT gives the dimensions
12404 of the display. Value is null if matrix is empty; otherwise it is
12405 a pointer to the row found. */
12406
12407 static struct glyph_row *
12408 find_last_row_displaying_text (matrix, it, start)
12409 struct glyph_matrix *matrix;
12410 struct it *it;
12411 struct glyph_row *start;
12412 {
12413 struct glyph_row *row, *row_found;
12414
12415 /* Set row_found to the last row in IT->w's current matrix
12416 displaying text. The loop looks funny but think of partially
12417 visible lines. */
12418 row_found = NULL;
12419 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
12420 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12421 {
12422 xassert (row->enabled_p);
12423 row_found = row;
12424 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
12425 break;
12426 ++row;
12427 }
12428
12429 return row_found;
12430 }
12431
12432
12433 /* Return the last row in the current matrix of W that is not affected
12434 by changes at the start of current_buffer that occurred since W's
12435 current matrix was built. Value is null if no such row exists.
12436
12437 BEG_UNCHANGED us the number of characters unchanged at the start of
12438 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
12439 first changed character in current_buffer. Characters at positions <
12440 BEG + BEG_UNCHANGED are at the same buffer positions as they were
12441 when the current matrix was built. */
12442
12443 static struct glyph_row *
12444 find_last_unchanged_at_beg_row (w)
12445 struct window *w;
12446 {
12447 int first_changed_pos = BEG + BEG_UNCHANGED;
12448 struct glyph_row *row;
12449 struct glyph_row *row_found = NULL;
12450 int yb = window_text_bottom_y (w);
12451
12452 /* Find the last row displaying unchanged text. */
12453 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12454 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12455 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
12456 {
12457 if (/* If row ends before first_changed_pos, it is unchanged,
12458 except in some case. */
12459 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
12460 /* When row ends in ZV and we write at ZV it is not
12461 unchanged. */
12462 && !row->ends_at_zv_p
12463 /* When first_changed_pos is the end of a continued line,
12464 row is not unchanged because it may be no longer
12465 continued. */
12466 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
12467 && row->continued_p))
12468 row_found = row;
12469
12470 /* Stop if last visible row. */
12471 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
12472 break;
12473
12474 ++row;
12475 }
12476
12477 return row_found;
12478 }
12479
12480
12481 /* Find the first glyph row in the current matrix of W that is not
12482 affected by changes at the end of current_buffer since the
12483 time W's current matrix was built.
12484
12485 Return in *DELTA the number of chars by which buffer positions in
12486 unchanged text at the end of current_buffer must be adjusted.
12487
12488 Return in *DELTA_BYTES the corresponding number of bytes.
12489
12490 Value is null if no such row exists, i.e. all rows are affected by
12491 changes. */
12492
12493 static struct glyph_row *
12494 find_first_unchanged_at_end_row (w, delta, delta_bytes)
12495 struct window *w;
12496 int *delta, *delta_bytes;
12497 {
12498 struct glyph_row *row;
12499 struct glyph_row *row_found = NULL;
12500
12501 *delta = *delta_bytes = 0;
12502
12503 /* Display must not have been paused, otherwise the current matrix
12504 is not up to date. */
12505 if (NILP (w->window_end_valid))
12506 abort ();
12507
12508 /* A value of window_end_pos >= END_UNCHANGED means that the window
12509 end is in the range of changed text. If so, there is no
12510 unchanged row at the end of W's current matrix. */
12511 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
12512 return NULL;
12513
12514 /* Set row to the last row in W's current matrix displaying text. */
12515 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12516
12517 /* If matrix is entirely empty, no unchanged row exists. */
12518 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12519 {
12520 /* The value of row is the last glyph row in the matrix having a
12521 meaningful buffer position in it. The end position of row
12522 corresponds to window_end_pos. This allows us to translate
12523 buffer positions in the current matrix to current buffer
12524 positions for characters not in changed text. */
12525 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12526 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12527 int last_unchanged_pos, last_unchanged_pos_old;
12528 struct glyph_row *first_text_row
12529 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12530
12531 *delta = Z - Z_old;
12532 *delta_bytes = Z_BYTE - Z_BYTE_old;
12533
12534 /* Set last_unchanged_pos to the buffer position of the last
12535 character in the buffer that has not been changed. Z is the
12536 index + 1 of the last character in current_buffer, i.e. by
12537 subtracting END_UNCHANGED we get the index of the last
12538 unchanged character, and we have to add BEG to get its buffer
12539 position. */
12540 last_unchanged_pos = Z - END_UNCHANGED + BEG;
12541 last_unchanged_pos_old = last_unchanged_pos - *delta;
12542
12543 /* Search backward from ROW for a row displaying a line that
12544 starts at a minimum position >= last_unchanged_pos_old. */
12545 for (; row > first_text_row; --row)
12546 {
12547 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
12548 abort ();
12549
12550 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
12551 row_found = row;
12552 }
12553 }
12554
12555 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
12556 abort ();
12557
12558 return row_found;
12559 }
12560
12561
12562 /* Make sure that glyph rows in the current matrix of window W
12563 reference the same glyph memory as corresponding rows in the
12564 frame's frame matrix. This function is called after scrolling W's
12565 current matrix on a terminal frame in try_window_id and
12566 try_window_reusing_current_matrix. */
12567
12568 static void
12569 sync_frame_with_window_matrix_rows (w)
12570 struct window *w;
12571 {
12572 struct frame *f = XFRAME (w->frame);
12573 struct glyph_row *window_row, *window_row_end, *frame_row;
12574
12575 /* Preconditions: W must be a leaf window and full-width. Its frame
12576 must have a frame matrix. */
12577 xassert (NILP (w->hchild) && NILP (w->vchild));
12578 xassert (WINDOW_FULL_WIDTH_P (w));
12579 xassert (!FRAME_WINDOW_P (f));
12580
12581 /* If W is a full-width window, glyph pointers in W's current matrix
12582 have, by definition, to be the same as glyph pointers in the
12583 corresponding frame matrix. Note that frame matrices have no
12584 marginal areas (see build_frame_matrix). */
12585 window_row = w->current_matrix->rows;
12586 window_row_end = window_row + w->current_matrix->nrows;
12587 frame_row = f->current_matrix->rows + XFASTINT (w->top);
12588 while (window_row < window_row_end)
12589 {
12590 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
12591 struct glyph *end = window_row->glyphs[LAST_AREA];
12592
12593 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
12594 frame_row->glyphs[TEXT_AREA] = start;
12595 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
12596 frame_row->glyphs[LAST_AREA] = end;
12597
12598 /* Disable frame rows whose corresponding window rows have
12599 been disabled in try_window_id. */
12600 if (!window_row->enabled_p)
12601 frame_row->enabled_p = 0;
12602
12603 ++window_row, ++frame_row;
12604 }
12605 }
12606
12607
12608 /* Find the glyph row in window W containing CHARPOS. Consider all
12609 rows between START and END (not inclusive). END null means search
12610 all rows to the end of the display area of W. Value is the row
12611 containing CHARPOS or null. */
12612
12613 struct glyph_row *
12614 row_containing_pos (w, charpos, start, end, dy)
12615 struct window *w;
12616 int charpos;
12617 struct glyph_row *start, *end;
12618 int dy;
12619 {
12620 struct glyph_row *row = start;
12621 int last_y;
12622
12623 /* If we happen to start on a header-line, skip that. */
12624 if (row->mode_line_p)
12625 ++row;
12626
12627 if ((end && row >= end) || !row->enabled_p)
12628 return NULL;
12629
12630 last_y = window_text_bottom_y (w) - dy;
12631
12632 while (1)
12633 {
12634 /* Give up if we have gone too far. */
12635 if (end && row >= end)
12636 return NULL;
12637 /* This formerly returned if they were equal.
12638 I think that both quantities are of a "last plus one" type;
12639 if so, when they are equal, the row is within the screen. -- rms. */
12640 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
12641 return NULL;
12642
12643 /* If it is in this row, return this row. */
12644 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
12645 || (MATRIX_ROW_END_CHARPOS (row) == charpos
12646 /* The end position of a row equals the start
12647 position of the next row. If CHARPOS is there, we
12648 would rather display it in the next line, except
12649 when this line ends in ZV. */
12650 && !row->ends_at_zv_p
12651 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12652 && charpos >= MATRIX_ROW_START_CHARPOS (row))
12653 return row;
12654 ++row;
12655 }
12656 }
12657
12658
12659 /* Try to redisplay window W by reusing its existing display. W's
12660 current matrix must be up to date when this function is called,
12661 i.e. window_end_valid must not be nil.
12662
12663 Value is
12664
12665 1 if display has been updated
12666 0 if otherwise unsuccessful
12667 -1 if redisplay with same window start is known not to succeed
12668
12669 The following steps are performed:
12670
12671 1. Find the last row in the current matrix of W that is not
12672 affected by changes at the start of current_buffer. If no such row
12673 is found, give up.
12674
12675 2. Find the first row in W's current matrix that is not affected by
12676 changes at the end of current_buffer. Maybe there is no such row.
12677
12678 3. Display lines beginning with the row + 1 found in step 1 to the
12679 row found in step 2 or, if step 2 didn't find a row, to the end of
12680 the window.
12681
12682 4. If cursor is not known to appear on the window, give up.
12683
12684 5. If display stopped at the row found in step 2, scroll the
12685 display and current matrix as needed.
12686
12687 6. Maybe display some lines at the end of W, if we must. This can
12688 happen under various circumstances, like a partially visible line
12689 becoming fully visible, or because newly displayed lines are displayed
12690 in smaller font sizes.
12691
12692 7. Update W's window end information. */
12693
12694 static int
12695 try_window_id (w)
12696 struct window *w;
12697 {
12698 struct frame *f = XFRAME (w->frame);
12699 struct glyph_matrix *current_matrix = w->current_matrix;
12700 struct glyph_matrix *desired_matrix = w->desired_matrix;
12701 struct glyph_row *last_unchanged_at_beg_row;
12702 struct glyph_row *first_unchanged_at_end_row;
12703 struct glyph_row *row;
12704 struct glyph_row *bottom_row;
12705 int bottom_vpos;
12706 struct it it;
12707 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
12708 struct text_pos start_pos;
12709 struct run run;
12710 int first_unchanged_at_end_vpos = 0;
12711 struct glyph_row *last_text_row, *last_text_row_at_end;
12712 struct text_pos start;
12713 int first_changed_charpos, last_changed_charpos;
12714
12715 #if GLYPH_DEBUG
12716 if (inhibit_try_window_id)
12717 return 0;
12718 #endif
12719
12720 /* This is handy for debugging. */
12721 #if 0
12722 #define GIVE_UP(X) \
12723 do { \
12724 fprintf (stderr, "try_window_id give up %d\n", (X)); \
12725 return 0; \
12726 } while (0)
12727 #else
12728 #define GIVE_UP(X) return 0
12729 #endif
12730
12731 SET_TEXT_POS_FROM_MARKER (start, w->start);
12732
12733 /* Don't use this for mini-windows because these can show
12734 messages and mini-buffers, and we don't handle that here. */
12735 if (MINI_WINDOW_P (w))
12736 GIVE_UP (1);
12737
12738 /* This flag is used to prevent redisplay optimizations. */
12739 if (windows_or_buffers_changed || cursor_type_changed)
12740 GIVE_UP (2);
12741
12742 /* Verify that narrowing has not changed.
12743 Also verify that we were not told to prevent redisplay optimizations.
12744 It would be nice to further
12745 reduce the number of cases where this prevents try_window_id. */
12746 if (current_buffer->clip_changed
12747 || current_buffer->prevent_redisplay_optimizations_p)
12748 GIVE_UP (3);
12749
12750 /* Window must either use window-based redisplay or be full width. */
12751 if (!FRAME_WINDOW_P (f)
12752 && (!line_ins_del_ok
12753 || !WINDOW_FULL_WIDTH_P (w)))
12754 GIVE_UP (4);
12755
12756 /* Give up if point is not known NOT to appear in W. */
12757 if (PT < CHARPOS (start))
12758 GIVE_UP (5);
12759
12760 /* Another way to prevent redisplay optimizations. */
12761 if (XFASTINT (w->last_modified) == 0)
12762 GIVE_UP (6);
12763
12764 /* Verify that window is not hscrolled. */
12765 if (XFASTINT (w->hscroll) != 0)
12766 GIVE_UP (7);
12767
12768 /* Verify that display wasn't paused. */
12769 if (NILP (w->window_end_valid))
12770 GIVE_UP (8);
12771
12772 /* Can't use this if highlighting a region because a cursor movement
12773 will do more than just set the cursor. */
12774 if (!NILP (Vtransient_mark_mode)
12775 && !NILP (current_buffer->mark_active))
12776 GIVE_UP (9);
12777
12778 /* Likewise if highlighting trailing whitespace. */
12779 if (!NILP (Vshow_trailing_whitespace))
12780 GIVE_UP (11);
12781
12782 /* Likewise if showing a region. */
12783 if (!NILP (w->region_showing))
12784 GIVE_UP (10);
12785
12786 /* Can use this if overlay arrow position and or string have changed. */
12787 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
12788 || !EQ (last_arrow_string, Voverlay_arrow_string))
12789 GIVE_UP (12);
12790
12791
12792 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
12793 only if buffer has really changed. The reason is that the gap is
12794 initially at Z for freshly visited files. The code below would
12795 set end_unchanged to 0 in that case. */
12796 if (MODIFF > SAVE_MODIFF
12797 /* This seems to happen sometimes after saving a buffer. */
12798 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
12799 {
12800 if (GPT - BEG < BEG_UNCHANGED)
12801 BEG_UNCHANGED = GPT - BEG;
12802 if (Z - GPT < END_UNCHANGED)
12803 END_UNCHANGED = Z - GPT;
12804 }
12805
12806 /* The position of the first and last character that has been changed. */
12807 first_changed_charpos = BEG + BEG_UNCHANGED;
12808 last_changed_charpos = Z - END_UNCHANGED;
12809
12810 /* If window starts after a line end, and the last change is in
12811 front of that newline, then changes don't affect the display.
12812 This case happens with stealth-fontification. Note that although
12813 the display is unchanged, glyph positions in the matrix have to
12814 be adjusted, of course. */
12815 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12816 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12817 && ((last_changed_charpos < CHARPOS (start)
12818 && CHARPOS (start) == BEGV)
12819 || (last_changed_charpos < CHARPOS (start) - 1
12820 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
12821 {
12822 int Z_old, delta, Z_BYTE_old, delta_bytes;
12823 struct glyph_row *r0;
12824
12825 /* Compute how many chars/bytes have been added to or removed
12826 from the buffer. */
12827 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12828 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12829 delta = Z - Z_old;
12830 delta_bytes = Z_BYTE - Z_BYTE_old;
12831
12832 /* Give up if PT is not in the window. Note that it already has
12833 been checked at the start of try_window_id that PT is not in
12834 front of the window start. */
12835 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
12836 GIVE_UP (13);
12837
12838 /* If window start is unchanged, we can reuse the whole matrix
12839 as is, after adjusting glyph positions. No need to compute
12840 the window end again, since its offset from Z hasn't changed. */
12841 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
12842 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
12843 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
12844 {
12845 /* Adjust positions in the glyph matrix. */
12846 if (delta || delta_bytes)
12847 {
12848 struct glyph_row *r1
12849 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
12850 increment_matrix_positions (w->current_matrix,
12851 MATRIX_ROW_VPOS (r0, current_matrix),
12852 MATRIX_ROW_VPOS (r1, current_matrix),
12853 delta, delta_bytes);
12854 }
12855
12856 /* Set the cursor. */
12857 row = row_containing_pos (w, PT, r0, NULL, 0);
12858 if (row)
12859 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
12860 else
12861 abort ();
12862 return 1;
12863 }
12864 }
12865
12866 /* Handle the case that changes are all below what is displayed in
12867 the window, and that PT is in the window. This shortcut cannot
12868 be taken if ZV is visible in the window, and text has been added
12869 there that is visible in the window. */
12870 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
12871 /* ZV is not visible in the window, or there are no
12872 changes at ZV, actually. */
12873 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
12874 || first_changed_charpos == last_changed_charpos))
12875 {
12876 struct glyph_row *r0;
12877
12878 /* Give up if PT is not in the window. Note that it already has
12879 been checked at the start of try_window_id that PT is not in
12880 front of the window start. */
12881 if (PT >= MATRIX_ROW_END_CHARPOS (row))
12882 GIVE_UP (14);
12883
12884 /* If window start is unchanged, we can reuse the whole matrix
12885 as is, without changing glyph positions since no text has
12886 been added/removed in front of the window end. */
12887 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
12888 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
12889 {
12890 /* We have to compute the window end anew since text
12891 can have been added/removed after it. */
12892 w->window_end_pos
12893 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12894 w->window_end_bytepos
12895 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12896
12897 /* Set the cursor. */
12898 row = row_containing_pos (w, PT, r0, NULL, 0);
12899 if (row)
12900 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
12901 else
12902 abort ();
12903 return 2;
12904 }
12905 }
12906
12907 /* Give up if window start is in the changed area.
12908
12909 The condition used to read
12910
12911 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
12912
12913 but why that was tested escapes me at the moment. */
12914 if (CHARPOS (start) >= first_changed_charpos
12915 && CHARPOS (start) <= last_changed_charpos)
12916 GIVE_UP (15);
12917
12918 /* Check that window start agrees with the start of the first glyph
12919 row in its current matrix. Check this after we know the window
12920 start is not in changed text, otherwise positions would not be
12921 comparable. */
12922 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
12923 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
12924 GIVE_UP (16);
12925
12926 /* Give up if the window ends in strings. Overlay strings
12927 at the end are difficult to handle, so don't try. */
12928 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
12929 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
12930 GIVE_UP (20);
12931
12932 /* Compute the position at which we have to start displaying new
12933 lines. Some of the lines at the top of the window might be
12934 reusable because they are not displaying changed text. Find the
12935 last row in W's current matrix not affected by changes at the
12936 start of current_buffer. Value is null if changes start in the
12937 first line of window. */
12938 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
12939 if (last_unchanged_at_beg_row)
12940 {
12941 /* Avoid starting to display in the moddle of a character, a TAB
12942 for instance. This is easier than to set up the iterator
12943 exactly, and it's not a frequent case, so the additional
12944 effort wouldn't really pay off. */
12945 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
12946 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
12947 && last_unchanged_at_beg_row > w->current_matrix->rows)
12948 --last_unchanged_at_beg_row;
12949
12950 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
12951 GIVE_UP (17);
12952
12953 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
12954 GIVE_UP (18);
12955 start_pos = it.current.pos;
12956
12957 /* Start displaying new lines in the desired matrix at the same
12958 vpos we would use in the current matrix, i.e. below
12959 last_unchanged_at_beg_row. */
12960 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
12961 current_matrix);
12962 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
12963 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
12964
12965 xassert (it.hpos == 0 && it.current_x == 0);
12966 }
12967 else
12968 {
12969 /* There are no reusable lines at the start of the window.
12970 Start displaying in the first line. */
12971 start_display (&it, w, start);
12972 start_pos = it.current.pos;
12973 }
12974
12975 /* Find the first row that is not affected by changes at the end of
12976 the buffer. Value will be null if there is no unchanged row, in
12977 which case we must redisplay to the end of the window. delta
12978 will be set to the value by which buffer positions beginning with
12979 first_unchanged_at_end_row have to be adjusted due to text
12980 changes. */
12981 first_unchanged_at_end_row
12982 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
12983 IF_DEBUG (debug_delta = delta);
12984 IF_DEBUG (debug_delta_bytes = delta_bytes);
12985
12986 /* Set stop_pos to the buffer position up to which we will have to
12987 display new lines. If first_unchanged_at_end_row != NULL, this
12988 is the buffer position of the start of the line displayed in that
12989 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
12990 that we don't stop at a buffer position. */
12991 stop_pos = 0;
12992 if (first_unchanged_at_end_row)
12993 {
12994 xassert (last_unchanged_at_beg_row == NULL
12995 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
12996
12997 /* If this is a continuation line, move forward to the next one
12998 that isn't. Changes in lines above affect this line.
12999 Caution: this may move first_unchanged_at_end_row to a row
13000 not displaying text. */
13001 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13002 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13003 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13004 < it.last_visible_y))
13005 ++first_unchanged_at_end_row;
13006
13007 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13008 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13009 >= it.last_visible_y))
13010 first_unchanged_at_end_row = NULL;
13011 else
13012 {
13013 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13014 + delta);
13015 first_unchanged_at_end_vpos
13016 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13017 xassert (stop_pos >= Z - END_UNCHANGED);
13018 }
13019 }
13020 else if (last_unchanged_at_beg_row == NULL)
13021 GIVE_UP (19);
13022
13023
13024 #if GLYPH_DEBUG
13025
13026 /* Either there is no unchanged row at the end, or the one we have
13027 now displays text. This is a necessary condition for the window
13028 end pos calculation at the end of this function. */
13029 xassert (first_unchanged_at_end_row == NULL
13030 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13031
13032 debug_last_unchanged_at_beg_vpos
13033 = (last_unchanged_at_beg_row
13034 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13035 : -1);
13036 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13037
13038 #endif /* GLYPH_DEBUG != 0 */
13039
13040
13041 /* Display new lines. Set last_text_row to the last new line
13042 displayed which has text on it, i.e. might end up as being the
13043 line where the window_end_vpos is. */
13044 w->cursor.vpos = -1;
13045 last_text_row = NULL;
13046 overlay_arrow_seen = 0;
13047 while (it.current_y < it.last_visible_y
13048 && !fonts_changed_p
13049 && (first_unchanged_at_end_row == NULL
13050 || IT_CHARPOS (it) < stop_pos))
13051 {
13052 if (display_line (&it))
13053 last_text_row = it.glyph_row - 1;
13054 }
13055
13056 if (fonts_changed_p)
13057 return -1;
13058
13059
13060 /* Compute differences in buffer positions, y-positions etc. for
13061 lines reused at the bottom of the window. Compute what we can
13062 scroll. */
13063 if (first_unchanged_at_end_row
13064 /* No lines reused because we displayed everything up to the
13065 bottom of the window. */
13066 && it.current_y < it.last_visible_y)
13067 {
13068 dvpos = (it.vpos
13069 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13070 current_matrix));
13071 dy = it.current_y - first_unchanged_at_end_row->y;
13072 run.current_y = first_unchanged_at_end_row->y;
13073 run.desired_y = run.current_y + dy;
13074 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13075 }
13076 else
13077 {
13078 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13079 first_unchanged_at_end_row = NULL;
13080 }
13081 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13082
13083
13084 /* Find the cursor if not already found. We have to decide whether
13085 PT will appear on this window (it sometimes doesn't, but this is
13086 not a very frequent case.) This decision has to be made before
13087 the current matrix is altered. A value of cursor.vpos < 0 means
13088 that PT is either in one of the lines beginning at
13089 first_unchanged_at_end_row or below the window. Don't care for
13090 lines that might be displayed later at the window end; as
13091 mentioned, this is not a frequent case. */
13092 if (w->cursor.vpos < 0)
13093 {
13094 /* Cursor in unchanged rows at the top? */
13095 if (PT < CHARPOS (start_pos)
13096 && last_unchanged_at_beg_row)
13097 {
13098 row = row_containing_pos (w, PT,
13099 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13100 last_unchanged_at_beg_row + 1, 0);
13101 if (row)
13102 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13103 }
13104
13105 /* Start from first_unchanged_at_end_row looking for PT. */
13106 else if (first_unchanged_at_end_row)
13107 {
13108 row = row_containing_pos (w, PT - delta,
13109 first_unchanged_at_end_row, NULL, 0);
13110 if (row)
13111 set_cursor_from_row (w, row, w->current_matrix, delta,
13112 delta_bytes, dy, dvpos);
13113 }
13114
13115 /* Give up if cursor was not found. */
13116 if (w->cursor.vpos < 0)
13117 {
13118 clear_glyph_matrix (w->desired_matrix);
13119 return -1;
13120 }
13121 }
13122
13123 /* Don't let the cursor end in the scroll margins. */
13124 {
13125 int this_scroll_margin, cursor_height;
13126
13127 this_scroll_margin = max (0, scroll_margin);
13128 this_scroll_margin = min (this_scroll_margin,
13129 XFASTINT (w->height) / 4);
13130 this_scroll_margin *= CANON_Y_UNIT (it.f);
13131 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13132
13133 if ((w->cursor.y < this_scroll_margin
13134 && CHARPOS (start) > BEGV)
13135 /* Don't take scroll margin into account at the bottom because
13136 old redisplay didn't do it either. */
13137 || w->cursor.y + cursor_height > it.last_visible_y)
13138 {
13139 w->cursor.vpos = -1;
13140 clear_glyph_matrix (w->desired_matrix);
13141 return -1;
13142 }
13143 }
13144
13145 /* Scroll the display. Do it before changing the current matrix so
13146 that xterm.c doesn't get confused about where the cursor glyph is
13147 found. */
13148 if (dy && run.height)
13149 {
13150 update_begin (f);
13151
13152 if (FRAME_WINDOW_P (f))
13153 {
13154 rif->update_window_begin_hook (w);
13155 rif->clear_window_mouse_face (w);
13156 rif->scroll_run_hook (w, &run);
13157 rif->update_window_end_hook (w, 0, 0);
13158 }
13159 else
13160 {
13161 /* Terminal frame. In this case, dvpos gives the number of
13162 lines to scroll by; dvpos < 0 means scroll up. */
13163 int first_unchanged_at_end_vpos
13164 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13165 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
13166 int end = (XFASTINT (w->top)
13167 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13168 + window_internal_height (w));
13169
13170 /* Perform the operation on the screen. */
13171 if (dvpos > 0)
13172 {
13173 /* Scroll last_unchanged_at_beg_row to the end of the
13174 window down dvpos lines. */
13175 set_terminal_window (end);
13176
13177 /* On dumb terminals delete dvpos lines at the end
13178 before inserting dvpos empty lines. */
13179 if (!scroll_region_ok)
13180 ins_del_lines (end - dvpos, -dvpos);
13181
13182 /* Insert dvpos empty lines in front of
13183 last_unchanged_at_beg_row. */
13184 ins_del_lines (from, dvpos);
13185 }
13186 else if (dvpos < 0)
13187 {
13188 /* Scroll up last_unchanged_at_beg_vpos to the end of
13189 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13190 set_terminal_window (end);
13191
13192 /* Delete dvpos lines in front of
13193 last_unchanged_at_beg_vpos. ins_del_lines will set
13194 the cursor to the given vpos and emit |dvpos| delete
13195 line sequences. */
13196 ins_del_lines (from + dvpos, dvpos);
13197
13198 /* On a dumb terminal insert dvpos empty lines at the
13199 end. */
13200 if (!scroll_region_ok)
13201 ins_del_lines (end + dvpos, -dvpos);
13202 }
13203
13204 set_terminal_window (0);
13205 }
13206
13207 update_end (f);
13208 }
13209
13210 /* Shift reused rows of the current matrix to the right position.
13211 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13212 text. */
13213 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13214 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
13215 if (dvpos < 0)
13216 {
13217 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13218 bottom_vpos, dvpos);
13219 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13220 bottom_vpos, 0);
13221 }
13222 else if (dvpos > 0)
13223 {
13224 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13225 bottom_vpos, dvpos);
13226 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13227 first_unchanged_at_end_vpos + dvpos, 0);
13228 }
13229
13230 /* For frame-based redisplay, make sure that current frame and window
13231 matrix are in sync with respect to glyph memory. */
13232 if (!FRAME_WINDOW_P (f))
13233 sync_frame_with_window_matrix_rows (w);
13234
13235 /* Adjust buffer positions in reused rows. */
13236 if (delta)
13237 increment_matrix_positions (current_matrix,
13238 first_unchanged_at_end_vpos + dvpos,
13239 bottom_vpos, delta, delta_bytes);
13240
13241 /* Adjust Y positions. */
13242 if (dy)
13243 shift_glyph_matrix (w, current_matrix,
13244 first_unchanged_at_end_vpos + dvpos,
13245 bottom_vpos, dy);
13246
13247 if (first_unchanged_at_end_row)
13248 first_unchanged_at_end_row += dvpos;
13249
13250 /* If scrolling up, there may be some lines to display at the end of
13251 the window. */
13252 last_text_row_at_end = NULL;
13253 if (dy < 0)
13254 {
13255 /* Scrolling up can leave for example a partially visible line
13256 at the end of the window to be redisplayed. */
13257 /* Set last_row to the glyph row in the current matrix where the
13258 window end line is found. It has been moved up or down in
13259 the matrix by dvpos. */
13260 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13261 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13262
13263 /* If last_row is the window end line, it should display text. */
13264 xassert (last_row->displays_text_p);
13265
13266 /* If window end line was partially visible before, begin
13267 displaying at that line. Otherwise begin displaying with the
13268 line following it. */
13269 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13270 {
13271 init_to_row_start (&it, w, last_row);
13272 it.vpos = last_vpos;
13273 it.current_y = last_row->y;
13274 }
13275 else
13276 {
13277 init_to_row_end (&it, w, last_row);
13278 it.vpos = 1 + last_vpos;
13279 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13280 ++last_row;
13281 }
13282
13283 /* We may start in a continuation line. If so, we have to
13284 get the right continuation_lines_width and current_x. */
13285 it.continuation_lines_width = last_row->continuation_lines_width;
13286 it.hpos = it.current_x = 0;
13287
13288 /* Display the rest of the lines at the window end. */
13289 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13290 while (it.current_y < it.last_visible_y
13291 && !fonts_changed_p)
13292 {
13293 /* Is it always sure that the display agrees with lines in
13294 the current matrix? I don't think so, so we mark rows
13295 displayed invalid in the current matrix by setting their
13296 enabled_p flag to zero. */
13297 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13298 if (display_line (&it))
13299 last_text_row_at_end = it.glyph_row - 1;
13300 }
13301 }
13302
13303 /* Update window_end_pos and window_end_vpos. */
13304 if (first_unchanged_at_end_row
13305 && first_unchanged_at_end_row->y < it.last_visible_y
13306 && !last_text_row_at_end)
13307 {
13308 /* Window end line if one of the preserved rows from the current
13309 matrix. Set row to the last row displaying text in current
13310 matrix starting at first_unchanged_at_end_row, after
13311 scrolling. */
13312 xassert (first_unchanged_at_end_row->displays_text_p);
13313 row = find_last_row_displaying_text (w->current_matrix, &it,
13314 first_unchanged_at_end_row);
13315 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
13316
13317 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13318 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13319 w->window_end_vpos
13320 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
13321 xassert (w->window_end_bytepos >= 0);
13322 IF_DEBUG (debug_method_add (w, "A"));
13323 }
13324 else if (last_text_row_at_end)
13325 {
13326 w->window_end_pos
13327 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
13328 w->window_end_bytepos
13329 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
13330 w->window_end_vpos
13331 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
13332 xassert (w->window_end_bytepos >= 0);
13333 IF_DEBUG (debug_method_add (w, "B"));
13334 }
13335 else if (last_text_row)
13336 {
13337 /* We have displayed either to the end of the window or at the
13338 end of the window, i.e. the last row with text is to be found
13339 in the desired matrix. */
13340 w->window_end_pos
13341 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13342 w->window_end_bytepos
13343 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13344 w->window_end_vpos
13345 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
13346 xassert (w->window_end_bytepos >= 0);
13347 }
13348 else if (first_unchanged_at_end_row == NULL
13349 && last_text_row == NULL
13350 && last_text_row_at_end == NULL)
13351 {
13352 /* Displayed to end of window, but no line containing text was
13353 displayed. Lines were deleted at the end of the window. */
13354 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
13355 int vpos = XFASTINT (w->window_end_vpos);
13356 struct glyph_row *current_row = current_matrix->rows + vpos;
13357 struct glyph_row *desired_row = desired_matrix->rows + vpos;
13358
13359 for (row = NULL;
13360 row == NULL && vpos >= first_vpos;
13361 --vpos, --current_row, --desired_row)
13362 {
13363 if (desired_row->enabled_p)
13364 {
13365 if (desired_row->displays_text_p)
13366 row = desired_row;
13367 }
13368 else if (current_row->displays_text_p)
13369 row = current_row;
13370 }
13371
13372 xassert (row != NULL);
13373 w->window_end_vpos = make_number (vpos + 1);
13374 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13375 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13376 xassert (w->window_end_bytepos >= 0);
13377 IF_DEBUG (debug_method_add (w, "C"));
13378 }
13379 else
13380 abort ();
13381
13382 #if 0 /* This leads to problems, for instance when the cursor is
13383 at ZV, and the cursor line displays no text. */
13384 /* Disable rows below what's displayed in the window. This makes
13385 debugging easier. */
13386 enable_glyph_matrix_rows (current_matrix,
13387 XFASTINT (w->window_end_vpos) + 1,
13388 bottom_vpos, 0);
13389 #endif
13390
13391 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
13392 debug_end_vpos = XFASTINT (w->window_end_vpos));
13393
13394 /* Record that display has not been completed. */
13395 w->window_end_valid = Qnil;
13396 w->desired_matrix->no_scrolling_p = 1;
13397 return 3;
13398
13399 #undef GIVE_UP
13400 }
13401
13402
13403 \f
13404 /***********************************************************************
13405 More debugging support
13406 ***********************************************************************/
13407
13408 #if GLYPH_DEBUG
13409
13410 void dump_glyph_row P_ ((struct glyph_row *, int, int));
13411 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
13412 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
13413
13414
13415 /* Dump the contents of glyph matrix MATRIX on stderr.
13416
13417 GLYPHS 0 means don't show glyph contents.
13418 GLYPHS 1 means show glyphs in short form
13419 GLYPHS > 1 means show glyphs in long form. */
13420
13421 void
13422 dump_glyph_matrix (matrix, glyphs)
13423 struct glyph_matrix *matrix;
13424 int glyphs;
13425 {
13426 int i;
13427 for (i = 0; i < matrix->nrows; ++i)
13428 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
13429 }
13430
13431
13432 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
13433 the glyph row and area where the glyph comes from. */
13434
13435 void
13436 dump_glyph (row, glyph, area)
13437 struct glyph_row *row;
13438 struct glyph *glyph;
13439 int area;
13440 {
13441 if (glyph->type == CHAR_GLYPH)
13442 {
13443 fprintf (stderr,
13444 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13445 glyph - row->glyphs[TEXT_AREA],
13446 'C',
13447 glyph->charpos,
13448 (BUFFERP (glyph->object)
13449 ? 'B'
13450 : (STRINGP (glyph->object)
13451 ? 'S'
13452 : '-')),
13453 glyph->pixel_width,
13454 glyph->u.ch,
13455 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
13456 ? glyph->u.ch
13457 : '.'),
13458 glyph->face_id,
13459 glyph->left_box_line_p,
13460 glyph->right_box_line_p);
13461 }
13462 else if (glyph->type == STRETCH_GLYPH)
13463 {
13464 fprintf (stderr,
13465 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13466 glyph - row->glyphs[TEXT_AREA],
13467 'S',
13468 glyph->charpos,
13469 (BUFFERP (glyph->object)
13470 ? 'B'
13471 : (STRINGP (glyph->object)
13472 ? 'S'
13473 : '-')),
13474 glyph->pixel_width,
13475 0,
13476 '.',
13477 glyph->face_id,
13478 glyph->left_box_line_p,
13479 glyph->right_box_line_p);
13480 }
13481 else if (glyph->type == IMAGE_GLYPH)
13482 {
13483 fprintf (stderr,
13484 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13485 glyph - row->glyphs[TEXT_AREA],
13486 'I',
13487 glyph->charpos,
13488 (BUFFERP (glyph->object)
13489 ? 'B'
13490 : (STRINGP (glyph->object)
13491 ? 'S'
13492 : '-')),
13493 glyph->pixel_width,
13494 glyph->u.img_id,
13495 '.',
13496 glyph->face_id,
13497 glyph->left_box_line_p,
13498 glyph->right_box_line_p);
13499 }
13500 }
13501
13502
13503 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
13504 GLYPHS 0 means don't show glyph contents.
13505 GLYPHS 1 means show glyphs in short form
13506 GLYPHS > 1 means show glyphs in long form. */
13507
13508 void
13509 dump_glyph_row (row, vpos, glyphs)
13510 struct glyph_row *row;
13511 int vpos, glyphs;
13512 {
13513 if (glyphs != 1)
13514 {
13515 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
13516 fprintf (stderr, "=======================================================================\n");
13517
13518 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
13519 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
13520 vpos,
13521 MATRIX_ROW_START_CHARPOS (row),
13522 MATRIX_ROW_END_CHARPOS (row),
13523 row->used[TEXT_AREA],
13524 row->contains_overlapping_glyphs_p,
13525 row->enabled_p,
13526 row->truncated_on_left_p,
13527 row->truncated_on_right_p,
13528 row->overlay_arrow_p,
13529 row->continued_p,
13530 MATRIX_ROW_CONTINUATION_LINE_P (row),
13531 row->displays_text_p,
13532 row->ends_at_zv_p,
13533 row->fill_line_p,
13534 row->ends_in_middle_of_char_p,
13535 row->starts_in_middle_of_char_p,
13536 row->mouse_face_p,
13537 row->x,
13538 row->y,
13539 row->pixel_width,
13540 row->height,
13541 row->visible_height,
13542 row->ascent,
13543 row->phys_ascent);
13544 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
13545 row->end.overlay_string_index,
13546 row->continuation_lines_width);
13547 fprintf (stderr, "%9d %5d\n",
13548 CHARPOS (row->start.string_pos),
13549 CHARPOS (row->end.string_pos));
13550 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
13551 row->end.dpvec_index);
13552 }
13553
13554 if (glyphs > 1)
13555 {
13556 int area;
13557
13558 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13559 {
13560 struct glyph *glyph = row->glyphs[area];
13561 struct glyph *glyph_end = glyph + row->used[area];
13562
13563 /* Glyph for a line end in text. */
13564 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
13565 ++glyph_end;
13566
13567 if (glyph < glyph_end)
13568 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
13569
13570 for (; glyph < glyph_end; ++glyph)
13571 dump_glyph (row, glyph, area);
13572 }
13573 }
13574 else if (glyphs == 1)
13575 {
13576 int area;
13577
13578 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13579 {
13580 char *s = (char *) alloca (row->used[area] + 1);
13581 int i;
13582
13583 for (i = 0; i < row->used[area]; ++i)
13584 {
13585 struct glyph *glyph = row->glyphs[area] + i;
13586 if (glyph->type == CHAR_GLYPH
13587 && glyph->u.ch < 0x80
13588 && glyph->u.ch >= ' ')
13589 s[i] = glyph->u.ch;
13590 else
13591 s[i] = '.';
13592 }
13593
13594 s[i] = '\0';
13595 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
13596 }
13597 }
13598 }
13599
13600
13601 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
13602 Sdump_glyph_matrix, 0, 1, "p",
13603 doc: /* Dump the current matrix of the selected window to stderr.
13604 Shows contents of glyph row structures. With non-nil
13605 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
13606 glyphs in short form, otherwise show glyphs in long form. */)
13607 (glyphs)
13608 Lisp_Object glyphs;
13609 {
13610 struct window *w = XWINDOW (selected_window);
13611 struct buffer *buffer = XBUFFER (w->buffer);
13612
13613 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
13614 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
13615 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
13616 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
13617 fprintf (stderr, "=============================================\n");
13618 dump_glyph_matrix (w->current_matrix,
13619 NILP (glyphs) ? 0 : XINT (glyphs));
13620 return Qnil;
13621 }
13622
13623
13624 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
13625 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
13626 ()
13627 {
13628 struct frame *f = XFRAME (selected_frame);
13629 dump_glyph_matrix (f->current_matrix, 1);
13630 return Qnil;
13631 }
13632
13633
13634 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
13635 doc: /* Dump glyph row ROW to stderr.
13636 GLYPH 0 means don't dump glyphs.
13637 GLYPH 1 means dump glyphs in short form.
13638 GLYPH > 1 or omitted means dump glyphs in long form. */)
13639 (row, glyphs)
13640 Lisp_Object row, glyphs;
13641 {
13642 struct glyph_matrix *matrix;
13643 int vpos;
13644
13645 CHECK_NUMBER (row);
13646 matrix = XWINDOW (selected_window)->current_matrix;
13647 vpos = XINT (row);
13648 if (vpos >= 0 && vpos < matrix->nrows)
13649 dump_glyph_row (MATRIX_ROW (matrix, vpos),
13650 vpos,
13651 INTEGERP (glyphs) ? XINT (glyphs) : 2);
13652 return Qnil;
13653 }
13654
13655
13656 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
13657 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
13658 GLYPH 0 means don't dump glyphs.
13659 GLYPH 1 means dump glyphs in short form.
13660 GLYPH > 1 or omitted means dump glyphs in long form. */)
13661 (row, glyphs)
13662 Lisp_Object row, glyphs;
13663 {
13664 struct frame *sf = SELECTED_FRAME ();
13665 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
13666 int vpos;
13667
13668 CHECK_NUMBER (row);
13669 vpos = XINT (row);
13670 if (vpos >= 0 && vpos < m->nrows)
13671 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
13672 INTEGERP (glyphs) ? XINT (glyphs) : 2);
13673 return Qnil;
13674 }
13675
13676
13677 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
13678 doc: /* Toggle tracing of redisplay.
13679 With ARG, turn tracing on if and only if ARG is positive. */)
13680 (arg)
13681 Lisp_Object arg;
13682 {
13683 if (NILP (arg))
13684 trace_redisplay_p = !trace_redisplay_p;
13685 else
13686 {
13687 arg = Fprefix_numeric_value (arg);
13688 trace_redisplay_p = XINT (arg) > 0;
13689 }
13690
13691 return Qnil;
13692 }
13693
13694
13695 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
13696 doc: /* Like `format', but print result to stderr.
13697 usage: (trace-to-stderr STRING &rest OBJECTS) */)
13698 (nargs, args)
13699 int nargs;
13700 Lisp_Object *args;
13701 {
13702 Lisp_Object s = Fformat (nargs, args);
13703 fprintf (stderr, "%s", SDATA (s));
13704 return Qnil;
13705 }
13706
13707 #endif /* GLYPH_DEBUG */
13708
13709
13710 \f
13711 /***********************************************************************
13712 Building Desired Matrix Rows
13713 ***********************************************************************/
13714
13715 /* Return a temporary glyph row holding the glyphs of an overlay
13716 arrow. Only used for non-window-redisplay windows. */
13717
13718 static struct glyph_row *
13719 get_overlay_arrow_glyph_row (w)
13720 struct window *w;
13721 {
13722 struct frame *f = XFRAME (WINDOW_FRAME (w));
13723 struct buffer *buffer = XBUFFER (w->buffer);
13724 struct buffer *old = current_buffer;
13725 const unsigned char *arrow_string = SDATA (Voverlay_arrow_string);
13726 int arrow_len = SCHARS (Voverlay_arrow_string);
13727 const unsigned char *arrow_end = arrow_string + arrow_len;
13728 const unsigned char *p;
13729 struct it it;
13730 int multibyte_p;
13731 int n_glyphs_before;
13732
13733 set_buffer_temp (buffer);
13734 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
13735 it.glyph_row->used[TEXT_AREA] = 0;
13736 SET_TEXT_POS (it.position, 0, 0);
13737
13738 multibyte_p = !NILP (buffer->enable_multibyte_characters);
13739 p = arrow_string;
13740 while (p < arrow_end)
13741 {
13742 Lisp_Object face, ilisp;
13743
13744 /* Get the next character. */
13745 if (multibyte_p)
13746 it.c = string_char_and_length (p, arrow_len, &it.len);
13747 else
13748 it.c = *p, it.len = 1;
13749 p += it.len;
13750
13751 /* Get its face. */
13752 ilisp = make_number (p - arrow_string);
13753 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
13754 it.face_id = compute_char_face (f, it.c, face);
13755
13756 /* Compute its width, get its glyphs. */
13757 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
13758 SET_TEXT_POS (it.position, -1, -1);
13759 PRODUCE_GLYPHS (&it);
13760
13761 /* If this character doesn't fit any more in the line, we have
13762 to remove some glyphs. */
13763 if (it.current_x > it.last_visible_x)
13764 {
13765 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
13766 break;
13767 }
13768 }
13769
13770 set_buffer_temp (old);
13771 return it.glyph_row;
13772 }
13773
13774
13775 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
13776 glyphs are only inserted for terminal frames since we can't really
13777 win with truncation glyphs when partially visible glyphs are
13778 involved. Which glyphs to insert is determined by
13779 produce_special_glyphs. */
13780
13781 static void
13782 insert_left_trunc_glyphs (it)
13783 struct it *it;
13784 {
13785 struct it truncate_it;
13786 struct glyph *from, *end, *to, *toend;
13787
13788 xassert (!FRAME_WINDOW_P (it->f));
13789
13790 /* Get the truncation glyphs. */
13791 truncate_it = *it;
13792 truncate_it.current_x = 0;
13793 truncate_it.face_id = DEFAULT_FACE_ID;
13794 truncate_it.glyph_row = &scratch_glyph_row;
13795 truncate_it.glyph_row->used[TEXT_AREA] = 0;
13796 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
13797 truncate_it.object = make_number (0);
13798 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
13799
13800 /* Overwrite glyphs from IT with truncation glyphs. */
13801 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
13802 end = from + truncate_it.glyph_row->used[TEXT_AREA];
13803 to = it->glyph_row->glyphs[TEXT_AREA];
13804 toend = to + it->glyph_row->used[TEXT_AREA];
13805
13806 while (from < end)
13807 *to++ = *from++;
13808
13809 /* There may be padding glyphs left over. Overwrite them too. */
13810 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
13811 {
13812 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
13813 while (from < end)
13814 *to++ = *from++;
13815 }
13816
13817 if (to > toend)
13818 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
13819 }
13820
13821
13822 /* Compute the pixel height and width of IT->glyph_row.
13823
13824 Most of the time, ascent and height of a display line will be equal
13825 to the max_ascent and max_height values of the display iterator
13826 structure. This is not the case if
13827
13828 1. We hit ZV without displaying anything. In this case, max_ascent
13829 and max_height will be zero.
13830
13831 2. We have some glyphs that don't contribute to the line height.
13832 (The glyph row flag contributes_to_line_height_p is for future
13833 pixmap extensions).
13834
13835 The first case is easily covered by using default values because in
13836 these cases, the line height does not really matter, except that it
13837 must not be zero. */
13838
13839 static void
13840 compute_line_metrics (it)
13841 struct it *it;
13842 {
13843 struct glyph_row *row = it->glyph_row;
13844 int area, i;
13845
13846 if (FRAME_WINDOW_P (it->f))
13847 {
13848 int i, min_y, max_y;
13849
13850 /* The line may consist of one space only, that was added to
13851 place the cursor on it. If so, the row's height hasn't been
13852 computed yet. */
13853 if (row->height == 0)
13854 {
13855 if (it->max_ascent + it->max_descent == 0)
13856 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
13857 row->ascent = it->max_ascent;
13858 row->height = it->max_ascent + it->max_descent;
13859 row->phys_ascent = it->max_phys_ascent;
13860 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
13861 }
13862
13863 /* Compute the width of this line. */
13864 row->pixel_width = row->x;
13865 for (i = 0; i < row->used[TEXT_AREA]; ++i)
13866 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
13867
13868 xassert (row->pixel_width >= 0);
13869 xassert (row->ascent >= 0 && row->height > 0);
13870
13871 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
13872 || MATRIX_ROW_OVERLAPS_PRED_P (row));
13873
13874 /* If first line's physical ascent is larger than its logical
13875 ascent, use the physical ascent, and make the row taller.
13876 This makes accented characters fully visible. */
13877 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
13878 && row->phys_ascent > row->ascent)
13879 {
13880 row->height += row->phys_ascent - row->ascent;
13881 row->ascent = row->phys_ascent;
13882 }
13883
13884 /* Compute how much of the line is visible. */
13885 row->visible_height = row->height;
13886
13887 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
13888 max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
13889
13890 if (row->y < min_y)
13891 row->visible_height -= min_y - row->y;
13892 if (row->y + row->height > max_y)
13893 row->visible_height -= row->y + row->height - max_y;
13894 }
13895 else
13896 {
13897 row->pixel_width = row->used[TEXT_AREA];
13898 if (row->continued_p)
13899 row->pixel_width -= it->continuation_pixel_width;
13900 else if (row->truncated_on_right_p)
13901 row->pixel_width -= it->truncation_pixel_width;
13902 row->ascent = row->phys_ascent = 0;
13903 row->height = row->phys_height = row->visible_height = 1;
13904 }
13905
13906 /* Compute a hash code for this row. */
13907 row->hash = 0;
13908 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13909 for (i = 0; i < row->used[area]; ++i)
13910 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
13911 + row->glyphs[area][i].u.val
13912 + row->glyphs[area][i].face_id
13913 + row->glyphs[area][i].padding_p
13914 + (row->glyphs[area][i].type << 2));
13915
13916 it->max_ascent = it->max_descent = 0;
13917 it->max_phys_ascent = it->max_phys_descent = 0;
13918 }
13919
13920
13921 /* Append one space to the glyph row of iterator IT if doing a
13922 window-based redisplay. DEFAULT_FACE_P non-zero means let the
13923 space have the default face, otherwise let it have the same face as
13924 IT->face_id. Value is non-zero if a space was added.
13925
13926 This function is called to make sure that there is always one glyph
13927 at the end of a glyph row that the cursor can be set on under
13928 window-systems. (If there weren't such a glyph we would not know
13929 how wide and tall a box cursor should be displayed).
13930
13931 At the same time this space let's a nicely handle clearing to the
13932 end of the line if the row ends in italic text. */
13933
13934 static int
13935 append_space (it, default_face_p)
13936 struct it *it;
13937 int default_face_p;
13938 {
13939 if (FRAME_WINDOW_P (it->f))
13940 {
13941 int n = it->glyph_row->used[TEXT_AREA];
13942
13943 if (it->glyph_row->glyphs[TEXT_AREA] + n
13944 < it->glyph_row->glyphs[1 + TEXT_AREA])
13945 {
13946 /* Save some values that must not be changed.
13947 Must save IT->c and IT->len because otherwise
13948 ITERATOR_AT_END_P wouldn't work anymore after
13949 append_space has been called. */
13950 enum display_element_type saved_what = it->what;
13951 int saved_c = it->c, saved_len = it->len;
13952 int saved_x = it->current_x;
13953 int saved_face_id = it->face_id;
13954 struct text_pos saved_pos;
13955 Lisp_Object saved_object;
13956 struct face *face;
13957
13958 saved_object = it->object;
13959 saved_pos = it->position;
13960
13961 it->what = IT_CHARACTER;
13962 bzero (&it->position, sizeof it->position);
13963 it->object = make_number (0);
13964 it->c = ' ';
13965 it->len = 1;
13966
13967 if (default_face_p)
13968 it->face_id = DEFAULT_FACE_ID;
13969 else if (it->face_before_selective_p)
13970 it->face_id = it->saved_face_id;
13971 face = FACE_FROM_ID (it->f, it->face_id);
13972 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
13973
13974 PRODUCE_GLYPHS (it);
13975
13976 it->current_x = saved_x;
13977 it->object = saved_object;
13978 it->position = saved_pos;
13979 it->what = saved_what;
13980 it->face_id = saved_face_id;
13981 it->len = saved_len;
13982 it->c = saved_c;
13983 return 1;
13984 }
13985 }
13986
13987 return 0;
13988 }
13989
13990
13991 /* Extend the face of the last glyph in the text area of IT->glyph_row
13992 to the end of the display line. Called from display_line.
13993 If the glyph row is empty, add a space glyph to it so that we
13994 know the face to draw. Set the glyph row flag fill_line_p. */
13995
13996 static void
13997 extend_face_to_end_of_line (it)
13998 struct it *it;
13999 {
14000 struct face *face;
14001 struct frame *f = it->f;
14002
14003 /* If line is already filled, do nothing. */
14004 if (it->current_x >= it->last_visible_x)
14005 return;
14006
14007 /* Face extension extends the background and box of IT->face_id
14008 to the end of the line. If the background equals the background
14009 of the frame, we don't have to do anything. */
14010 if (it->face_before_selective_p)
14011 face = FACE_FROM_ID (it->f, it->saved_face_id);
14012 else
14013 face = FACE_FROM_ID (f, it->face_id);
14014
14015 if (FRAME_WINDOW_P (f)
14016 && face->box == FACE_NO_BOX
14017 && face->background == FRAME_BACKGROUND_PIXEL (f)
14018 && !face->stipple)
14019 return;
14020
14021 /* Set the glyph row flag indicating that the face of the last glyph
14022 in the text area has to be drawn to the end of the text area. */
14023 it->glyph_row->fill_line_p = 1;
14024
14025 /* If current character of IT is not ASCII, make sure we have the
14026 ASCII face. This will be automatically undone the next time
14027 get_next_display_element returns a multibyte character. Note
14028 that the character will always be single byte in unibyte text. */
14029 if (!SINGLE_BYTE_CHAR_P (it->c))
14030 {
14031 it->face_id = FACE_FOR_CHAR (f, face, 0);
14032 }
14033
14034 if (FRAME_WINDOW_P (f))
14035 {
14036 /* If the row is empty, add a space with the current face of IT,
14037 so that we know which face to draw. */
14038 if (it->glyph_row->used[TEXT_AREA] == 0)
14039 {
14040 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14041 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14042 it->glyph_row->used[TEXT_AREA] = 1;
14043 }
14044 }
14045 else
14046 {
14047 /* Save some values that must not be changed. */
14048 int saved_x = it->current_x;
14049 struct text_pos saved_pos;
14050 Lisp_Object saved_object;
14051 enum display_element_type saved_what = it->what;
14052 int saved_face_id = it->face_id;
14053
14054 saved_object = it->object;
14055 saved_pos = it->position;
14056
14057 it->what = IT_CHARACTER;
14058 bzero (&it->position, sizeof it->position);
14059 it->object = make_number (0);
14060 it->c = ' ';
14061 it->len = 1;
14062 it->face_id = face->id;
14063
14064 PRODUCE_GLYPHS (it);
14065
14066 while (it->current_x <= it->last_visible_x)
14067 PRODUCE_GLYPHS (it);
14068
14069 /* Don't count these blanks really. It would let us insert a left
14070 truncation glyph below and make us set the cursor on them, maybe. */
14071 it->current_x = saved_x;
14072 it->object = saved_object;
14073 it->position = saved_pos;
14074 it->what = saved_what;
14075 it->face_id = saved_face_id;
14076 }
14077 }
14078
14079
14080 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14081 trailing whitespace. */
14082
14083 static int
14084 trailing_whitespace_p (charpos)
14085 int charpos;
14086 {
14087 int bytepos = CHAR_TO_BYTE (charpos);
14088 int c = 0;
14089
14090 while (bytepos < ZV_BYTE
14091 && (c = FETCH_CHAR (bytepos),
14092 c == ' ' || c == '\t'))
14093 ++bytepos;
14094
14095 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14096 {
14097 if (bytepos != PT_BYTE)
14098 return 1;
14099 }
14100 return 0;
14101 }
14102
14103
14104 /* Highlight trailing whitespace, if any, in ROW. */
14105
14106 void
14107 highlight_trailing_whitespace (f, row)
14108 struct frame *f;
14109 struct glyph_row *row;
14110 {
14111 int used = row->used[TEXT_AREA];
14112
14113 if (used)
14114 {
14115 struct glyph *start = row->glyphs[TEXT_AREA];
14116 struct glyph *glyph = start + used - 1;
14117
14118 /* Skip over glyphs inserted to display the cursor at the
14119 end of a line, for extending the face of the last glyph
14120 to the end of the line on terminals, and for truncation
14121 and continuation glyphs. */
14122 while (glyph >= start
14123 && glyph->type == CHAR_GLYPH
14124 && INTEGERP (glyph->object))
14125 --glyph;
14126
14127 /* If last glyph is a space or stretch, and it's trailing
14128 whitespace, set the face of all trailing whitespace glyphs in
14129 IT->glyph_row to `trailing-whitespace'. */
14130 if (glyph >= start
14131 && BUFFERP (glyph->object)
14132 && (glyph->type == STRETCH_GLYPH
14133 || (glyph->type == CHAR_GLYPH
14134 && glyph->u.ch == ' '))
14135 && trailing_whitespace_p (glyph->charpos))
14136 {
14137 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
14138
14139 while (glyph >= start
14140 && BUFFERP (glyph->object)
14141 && (glyph->type == STRETCH_GLYPH
14142 || (glyph->type == CHAR_GLYPH
14143 && glyph->u.ch == ' ')))
14144 (glyph--)->face_id = face_id;
14145 }
14146 }
14147 }
14148
14149
14150 /* Value is non-zero if glyph row ROW in window W should be
14151 used to hold the cursor. */
14152
14153 static int
14154 cursor_row_p (w, row)
14155 struct window *w;
14156 struct glyph_row *row;
14157 {
14158 int cursor_row_p = 1;
14159
14160 if (PT == MATRIX_ROW_END_CHARPOS (row))
14161 {
14162 /* If the row ends with a newline from a string, we don't want
14163 the cursor there (if the row is continued it doesn't end in a
14164 newline). */
14165 if (CHARPOS (row->end.string_pos) >= 0
14166 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14167 cursor_row_p = row->continued_p;
14168
14169 /* If the row ends at ZV, display the cursor at the end of that
14170 row instead of at the start of the row below. */
14171 else if (row->ends_at_zv_p)
14172 cursor_row_p = 1;
14173 else
14174 cursor_row_p = 0;
14175 }
14176
14177 return cursor_row_p;
14178 }
14179
14180
14181 /* Construct the glyph row IT->glyph_row in the desired matrix of
14182 IT->w from text at the current position of IT. See dispextern.h
14183 for an overview of struct it. Value is non-zero if
14184 IT->glyph_row displays text, as opposed to a line displaying ZV
14185 only. */
14186
14187 static int
14188 display_line (it)
14189 struct it *it;
14190 {
14191 struct glyph_row *row = it->glyph_row;
14192
14193 /* We always start displaying at hpos zero even if hscrolled. */
14194 xassert (it->hpos == 0 && it->current_x == 0);
14195
14196 /* We must not display in a row that's not a text row. */
14197 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14198 < it->w->desired_matrix->nrows);
14199
14200 /* Is IT->w showing the region? */
14201 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
14202
14203 /* Clear the result glyph row and enable it. */
14204 prepare_desired_row (row);
14205
14206 row->y = it->current_y;
14207 row->start = it->current;
14208 row->continuation_lines_width = it->continuation_lines_width;
14209 row->displays_text_p = 1;
14210 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14211 it->starts_in_middle_of_char_p = 0;
14212
14213 /* Arrange the overlays nicely for our purposes. Usually, we call
14214 display_line on only one line at a time, in which case this
14215 can't really hurt too much, or we call it on lines which appear
14216 one after another in the buffer, in which case all calls to
14217 recenter_overlay_lists but the first will be pretty cheap. */
14218 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14219
14220 /* Move over display elements that are not visible because we are
14221 hscrolled. This may stop at an x-position < IT->first_visible_x
14222 if the first glyph is partially visible or if we hit a line end. */
14223 if (it->current_x < it->first_visible_x)
14224 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14225 MOVE_TO_POS | MOVE_TO_X);
14226
14227 /* Get the initial row height. This is either the height of the
14228 text hscrolled, if there is any, or zero. */
14229 row->ascent = it->max_ascent;
14230 row->height = it->max_ascent + it->max_descent;
14231 row->phys_ascent = it->max_phys_ascent;
14232 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14233
14234 /* Loop generating characters. The loop is left with IT on the next
14235 character to display. */
14236 while (1)
14237 {
14238 int n_glyphs_before, hpos_before, x_before;
14239 int x, i, nglyphs;
14240 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
14241
14242 /* Retrieve the next thing to display. Value is zero if end of
14243 buffer reached. */
14244 if (!get_next_display_element (it))
14245 {
14246 /* Maybe add a space at the end of this line that is used to
14247 display the cursor there under X. Set the charpos of the
14248 first glyph of blank lines not corresponding to any text
14249 to -1. */
14250 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
14251 || row->used[TEXT_AREA] == 0)
14252 {
14253 row->glyphs[TEXT_AREA]->charpos = -1;
14254 row->displays_text_p = 0;
14255
14256 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
14257 && (!MINI_WINDOW_P (it->w)
14258 || (minibuf_level && EQ (it->window, minibuf_window))))
14259 row->indicate_empty_line_p = 1;
14260 }
14261
14262 it->continuation_lines_width = 0;
14263 row->ends_at_zv_p = 1;
14264 break;
14265 }
14266
14267 /* Now, get the metrics of what we want to display. This also
14268 generates glyphs in `row' (which is IT->glyph_row). */
14269 n_glyphs_before = row->used[TEXT_AREA];
14270 x = it->current_x;
14271
14272 /* Remember the line height so far in case the next element doesn't
14273 fit on the line. */
14274 if (!it->truncate_lines_p)
14275 {
14276 ascent = it->max_ascent;
14277 descent = it->max_descent;
14278 phys_ascent = it->max_phys_ascent;
14279 phys_descent = it->max_phys_descent;
14280 }
14281
14282 PRODUCE_GLYPHS (it);
14283
14284 /* If this display element was in marginal areas, continue with
14285 the next one. */
14286 if (it->area != TEXT_AREA)
14287 {
14288 row->ascent = max (row->ascent, it->max_ascent);
14289 row->height = max (row->height, it->max_ascent + it->max_descent);
14290 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14291 row->phys_height = max (row->phys_height,
14292 it->max_phys_ascent + it->max_phys_descent);
14293 set_iterator_to_next (it, 1);
14294 continue;
14295 }
14296
14297 /* Does the display element fit on the line? If we truncate
14298 lines, we should draw past the right edge of the window. If
14299 we don't truncate, we want to stop so that we can display the
14300 continuation glyph before the right margin. If lines are
14301 continued, there are two possible strategies for characters
14302 resulting in more than 1 glyph (e.g. tabs): Display as many
14303 glyphs as possible in this line and leave the rest for the
14304 continuation line, or display the whole element in the next
14305 line. Original redisplay did the former, so we do it also. */
14306 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14307 hpos_before = it->hpos;
14308 x_before = x;
14309
14310 if (/* Not a newline. */
14311 nglyphs > 0
14312 /* Glyphs produced fit entirely in the line. */
14313 && it->current_x < it->last_visible_x)
14314 {
14315 it->hpos += nglyphs;
14316 row->ascent = max (row->ascent, it->max_ascent);
14317 row->height = max (row->height, it->max_ascent + it->max_descent);
14318 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14319 row->phys_height = max (row->phys_height,
14320 it->max_phys_ascent + it->max_phys_descent);
14321 if (it->current_x - it->pixel_width < it->first_visible_x)
14322 row->x = x - it->first_visible_x;
14323 }
14324 else
14325 {
14326 int new_x;
14327 struct glyph *glyph;
14328
14329 for (i = 0; i < nglyphs; ++i, x = new_x)
14330 {
14331 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14332 new_x = x + glyph->pixel_width;
14333
14334 if (/* Lines are continued. */
14335 !it->truncate_lines_p
14336 && (/* Glyph doesn't fit on the line. */
14337 new_x > it->last_visible_x
14338 /* Or it fits exactly on a window system frame. */
14339 || (new_x == it->last_visible_x
14340 && FRAME_WINDOW_P (it->f))))
14341 {
14342 /* End of a continued line. */
14343
14344 if (it->hpos == 0
14345 || (new_x == it->last_visible_x
14346 && FRAME_WINDOW_P (it->f)))
14347 {
14348 /* Current glyph is the only one on the line or
14349 fits exactly on the line. We must continue
14350 the line because we can't draw the cursor
14351 after the glyph. */
14352 row->continued_p = 1;
14353 it->current_x = new_x;
14354 it->continuation_lines_width += new_x;
14355 ++it->hpos;
14356 if (i == nglyphs - 1)
14357 set_iterator_to_next (it, 1);
14358 }
14359 else if (CHAR_GLYPH_PADDING_P (*glyph)
14360 && !FRAME_WINDOW_P (it->f))
14361 {
14362 /* A padding glyph that doesn't fit on this line.
14363 This means the whole character doesn't fit
14364 on the line. */
14365 row->used[TEXT_AREA] = n_glyphs_before;
14366
14367 /* Fill the rest of the row with continuation
14368 glyphs like in 20.x. */
14369 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
14370 < row->glyphs[1 + TEXT_AREA])
14371 produce_special_glyphs (it, IT_CONTINUATION);
14372
14373 row->continued_p = 1;
14374 it->current_x = x_before;
14375 it->continuation_lines_width += x_before;
14376
14377 /* Restore the height to what it was before the
14378 element not fitting on the line. */
14379 it->max_ascent = ascent;
14380 it->max_descent = descent;
14381 it->max_phys_ascent = phys_ascent;
14382 it->max_phys_descent = phys_descent;
14383 }
14384 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
14385 {
14386 /* A TAB that extends past the right edge of the
14387 window. This produces a single glyph on
14388 window system frames. We leave the glyph in
14389 this row and let it fill the row, but don't
14390 consume the TAB. */
14391 it->continuation_lines_width += it->last_visible_x;
14392 row->ends_in_middle_of_char_p = 1;
14393 row->continued_p = 1;
14394 glyph->pixel_width = it->last_visible_x - x;
14395 it->starts_in_middle_of_char_p = 1;
14396 }
14397 else
14398 {
14399 /* Something other than a TAB that draws past
14400 the right edge of the window. Restore
14401 positions to values before the element. */
14402 row->used[TEXT_AREA] = n_glyphs_before + i;
14403
14404 /* Display continuation glyphs. */
14405 if (!FRAME_WINDOW_P (it->f))
14406 produce_special_glyphs (it, IT_CONTINUATION);
14407 row->continued_p = 1;
14408
14409 it->continuation_lines_width += x;
14410
14411 if (nglyphs > 1 && i > 0)
14412 {
14413 row->ends_in_middle_of_char_p = 1;
14414 it->starts_in_middle_of_char_p = 1;
14415 }
14416
14417 /* Restore the height to what it was before the
14418 element not fitting on the line. */
14419 it->max_ascent = ascent;
14420 it->max_descent = descent;
14421 it->max_phys_ascent = phys_ascent;
14422 it->max_phys_descent = phys_descent;
14423 }
14424
14425 break;
14426 }
14427 else if (new_x > it->first_visible_x)
14428 {
14429 /* Increment number of glyphs actually displayed. */
14430 ++it->hpos;
14431
14432 if (x < it->first_visible_x)
14433 /* Glyph is partially visible, i.e. row starts at
14434 negative X position. */
14435 row->x = x - it->first_visible_x;
14436 }
14437 else
14438 {
14439 /* Glyph is completely off the left margin of the
14440 window. This should not happen because of the
14441 move_it_in_display_line at the start of this
14442 function, unless the text display area of the
14443 window is empty. */
14444 xassert (it->first_visible_x <= it->last_visible_x);
14445 }
14446 }
14447
14448 row->ascent = max (row->ascent, it->max_ascent);
14449 row->height = max (row->height, it->max_ascent + it->max_descent);
14450 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14451 row->phys_height = max (row->phys_height,
14452 it->max_phys_ascent + it->max_phys_descent);
14453
14454 /* End of this display line if row is continued. */
14455 if (row->continued_p)
14456 break;
14457 }
14458
14459 /* Is this a line end? If yes, we're also done, after making
14460 sure that a non-default face is extended up to the right
14461 margin of the window. */
14462 if (ITERATOR_AT_END_OF_LINE_P (it))
14463 {
14464 int used_before = row->used[TEXT_AREA];
14465
14466 row->ends_in_newline_from_string_p = STRINGP (it->object);
14467
14468 /* Add a space at the end of the line that is used to
14469 display the cursor there. */
14470 append_space (it, 0);
14471
14472 /* Extend the face to the end of the line. */
14473 extend_face_to_end_of_line (it);
14474
14475 /* Make sure we have the position. */
14476 if (used_before == 0)
14477 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
14478
14479 /* Consume the line end. This skips over invisible lines. */
14480 set_iterator_to_next (it, 1);
14481 it->continuation_lines_width = 0;
14482 break;
14483 }
14484
14485 /* Proceed with next display element. Note that this skips
14486 over lines invisible because of selective display. */
14487 set_iterator_to_next (it, 1);
14488
14489 /* If we truncate lines, we are done when the last displayed
14490 glyphs reach past the right margin of the window. */
14491 if (it->truncate_lines_p
14492 && (FRAME_WINDOW_P (it->f)
14493 ? (it->current_x >= it->last_visible_x)
14494 : (it->current_x > it->last_visible_x)))
14495 {
14496 /* Maybe add truncation glyphs. */
14497 if (!FRAME_WINDOW_P (it->f))
14498 {
14499 int i, n;
14500
14501 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14502 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14503 break;
14504
14505 for (n = row->used[TEXT_AREA]; i < n; ++i)
14506 {
14507 row->used[TEXT_AREA] = i;
14508 produce_special_glyphs (it, IT_TRUNCATION);
14509 }
14510 }
14511
14512 row->truncated_on_right_p = 1;
14513 it->continuation_lines_width = 0;
14514 reseat_at_next_visible_line_start (it, 0);
14515 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
14516 it->hpos = hpos_before;
14517 it->current_x = x_before;
14518 break;
14519 }
14520 }
14521
14522 /* If line is not empty and hscrolled, maybe insert truncation glyphs
14523 at the left window margin. */
14524 if (it->first_visible_x
14525 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
14526 {
14527 if (!FRAME_WINDOW_P (it->f))
14528 insert_left_trunc_glyphs (it);
14529 row->truncated_on_left_p = 1;
14530 }
14531
14532 /* If the start of this line is the overlay arrow-position, then
14533 mark this glyph row as the one containing the overlay arrow.
14534 This is clearly a mess with variable size fonts. It would be
14535 better to let it be displayed like cursors under X. */
14536 if (MARKERP (Voverlay_arrow_position)
14537 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
14538 && (MATRIX_ROW_START_CHARPOS (row)
14539 == marker_position (Voverlay_arrow_position))
14540 && STRINGP (Voverlay_arrow_string)
14541 && ! overlay_arrow_seen)
14542 {
14543 /* Overlay arrow in window redisplay is a fringe bitmap. */
14544 if (!FRAME_WINDOW_P (it->f))
14545 {
14546 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
14547 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
14548 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
14549 struct glyph *p = row->glyphs[TEXT_AREA];
14550 struct glyph *p2, *end;
14551
14552 /* Copy the arrow glyphs. */
14553 while (glyph < arrow_end)
14554 *p++ = *glyph++;
14555
14556 /* Throw away padding glyphs. */
14557 p2 = p;
14558 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
14559 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
14560 ++p2;
14561 if (p2 > p)
14562 {
14563 while (p2 < end)
14564 *p++ = *p2++;
14565 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
14566 }
14567 }
14568
14569 overlay_arrow_seen = 1;
14570 row->overlay_arrow_p = 1;
14571 }
14572
14573 /* Compute pixel dimensions of this line. */
14574 compute_line_metrics (it);
14575
14576 /* Remember the position at which this line ends. */
14577 row->end = it->current;
14578
14579 /* Maybe set the cursor. */
14580 if (it->w->cursor.vpos < 0
14581 && PT >= MATRIX_ROW_START_CHARPOS (row)
14582 && PT <= MATRIX_ROW_END_CHARPOS (row)
14583 && cursor_row_p (it->w, row))
14584 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
14585
14586 /* Highlight trailing whitespace. */
14587 if (!NILP (Vshow_trailing_whitespace))
14588 highlight_trailing_whitespace (it->f, it->glyph_row);
14589
14590 /* Prepare for the next line. This line starts horizontally at (X
14591 HPOS) = (0 0). Vertical positions are incremented. As a
14592 convenience for the caller, IT->glyph_row is set to the next
14593 row to be used. */
14594 it->current_x = it->hpos = 0;
14595 it->current_y += row->height;
14596 ++it->vpos;
14597 ++it->glyph_row;
14598 return row->displays_text_p;
14599 }
14600
14601
14602 \f
14603 /***********************************************************************
14604 Menu Bar
14605 ***********************************************************************/
14606
14607 /* Redisplay the menu bar in the frame for window W.
14608
14609 The menu bar of X frames that don't have X toolkit support is
14610 displayed in a special window W->frame->menu_bar_window.
14611
14612 The menu bar of terminal frames is treated specially as far as
14613 glyph matrices are concerned. Menu bar lines are not part of
14614 windows, so the update is done directly on the frame matrix rows
14615 for the menu bar. */
14616
14617 static void
14618 display_menu_bar (w)
14619 struct window *w;
14620 {
14621 struct frame *f = XFRAME (WINDOW_FRAME (w));
14622 struct it it;
14623 Lisp_Object items;
14624 int i;
14625
14626 /* Don't do all this for graphical frames. */
14627 #ifdef HAVE_NTGUI
14628 if (!NILP (Vwindow_system))
14629 return;
14630 #endif
14631 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
14632 if (FRAME_X_P (f))
14633 return;
14634 #endif
14635 #ifdef MAC_OS
14636 if (FRAME_MAC_P (f))
14637 return;
14638 #endif
14639
14640 #ifdef USE_X_TOOLKIT
14641 xassert (!FRAME_WINDOW_P (f));
14642 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
14643 it.first_visible_x = 0;
14644 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
14645 #else /* not USE_X_TOOLKIT */
14646 if (FRAME_WINDOW_P (f))
14647 {
14648 /* Menu bar lines are displayed in the desired matrix of the
14649 dummy window menu_bar_window. */
14650 struct window *menu_w;
14651 xassert (WINDOWP (f->menu_bar_window));
14652 menu_w = XWINDOW (f->menu_bar_window);
14653 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
14654 MENU_FACE_ID);
14655 it.first_visible_x = 0;
14656 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
14657 }
14658 else
14659 {
14660 /* This is a TTY frame, i.e. character hpos/vpos are used as
14661 pixel x/y. */
14662 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
14663 MENU_FACE_ID);
14664 it.first_visible_x = 0;
14665 it.last_visible_x = FRAME_WIDTH (f);
14666 }
14667 #endif /* not USE_X_TOOLKIT */
14668
14669 if (! mode_line_inverse_video)
14670 /* Force the menu-bar to be displayed in the default face. */
14671 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
14672
14673 /* Clear all rows of the menu bar. */
14674 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
14675 {
14676 struct glyph_row *row = it.glyph_row + i;
14677 clear_glyph_row (row);
14678 row->enabled_p = 1;
14679 row->full_width_p = 1;
14680 }
14681
14682 /* Display all items of the menu bar. */
14683 items = FRAME_MENU_BAR_ITEMS (it.f);
14684 for (i = 0; i < XVECTOR (items)->size; i += 4)
14685 {
14686 Lisp_Object string;
14687
14688 /* Stop at nil string. */
14689 string = AREF (items, i + 1);
14690 if (NILP (string))
14691 break;
14692
14693 /* Remember where item was displayed. */
14694 AREF (items, i + 3) = make_number (it.hpos);
14695
14696 /* Display the item, pad with one space. */
14697 if (it.current_x < it.last_visible_x)
14698 display_string (NULL, string, Qnil, 0, 0, &it,
14699 SCHARS (string) + 1, 0, 0, -1);
14700 }
14701
14702 /* Fill out the line with spaces. */
14703 if (it.current_x < it.last_visible_x)
14704 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
14705
14706 /* Compute the total height of the lines. */
14707 compute_line_metrics (&it);
14708 }
14709
14710
14711 \f
14712 /***********************************************************************
14713 Mode Line
14714 ***********************************************************************/
14715
14716 /* Redisplay mode lines in the window tree whose root is WINDOW. If
14717 FORCE is non-zero, redisplay mode lines unconditionally.
14718 Otherwise, redisplay only mode lines that are garbaged. Value is
14719 the number of windows whose mode lines were redisplayed. */
14720
14721 static int
14722 redisplay_mode_lines (window, force)
14723 Lisp_Object window;
14724 int force;
14725 {
14726 int nwindows = 0;
14727
14728 while (!NILP (window))
14729 {
14730 struct window *w = XWINDOW (window);
14731
14732 if (WINDOWP (w->hchild))
14733 nwindows += redisplay_mode_lines (w->hchild, force);
14734 else if (WINDOWP (w->vchild))
14735 nwindows += redisplay_mode_lines (w->vchild, force);
14736 else if (force
14737 || FRAME_GARBAGED_P (XFRAME (w->frame))
14738 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
14739 {
14740 struct text_pos lpoint;
14741 struct buffer *old = current_buffer;
14742
14743 /* Set the window's buffer for the mode line display. */
14744 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14745 set_buffer_internal_1 (XBUFFER (w->buffer));
14746
14747 /* Point refers normally to the selected window. For any
14748 other window, set up appropriate value. */
14749 if (!EQ (window, selected_window))
14750 {
14751 struct text_pos pt;
14752
14753 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
14754 if (CHARPOS (pt) < BEGV)
14755 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
14756 else if (CHARPOS (pt) > (ZV - 1))
14757 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
14758 else
14759 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
14760 }
14761
14762 /* Display mode lines. */
14763 clear_glyph_matrix (w->desired_matrix);
14764 if (display_mode_lines (w))
14765 {
14766 ++nwindows;
14767 w->must_be_updated_p = 1;
14768 }
14769
14770 /* Restore old settings. */
14771 set_buffer_internal_1 (old);
14772 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14773 }
14774
14775 window = w->next;
14776 }
14777
14778 return nwindows;
14779 }
14780
14781
14782 /* Display the mode and/or top line of window W. Value is the number
14783 of mode lines displayed. */
14784
14785 static int
14786 display_mode_lines (w)
14787 struct window *w;
14788 {
14789 Lisp_Object old_selected_window, old_selected_frame;
14790 int n = 0;
14791
14792 old_selected_frame = selected_frame;
14793 selected_frame = w->frame;
14794 old_selected_window = selected_window;
14795 XSETWINDOW (selected_window, w);
14796
14797 /* These will be set while the mode line specs are processed. */
14798 line_number_displayed = 0;
14799 w->column_number_displayed = Qnil;
14800
14801 if (WINDOW_WANTS_MODELINE_P (w))
14802 {
14803 struct window *sel_w = XWINDOW (old_selected_window);
14804
14805 /* Select mode line face based on the real selected window. */
14806 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
14807 current_buffer->mode_line_format);
14808 ++n;
14809 }
14810
14811 if (WINDOW_WANTS_HEADER_LINE_P (w))
14812 {
14813 display_mode_line (w, HEADER_LINE_FACE_ID,
14814 current_buffer->header_line_format);
14815 ++n;
14816 }
14817
14818 selected_frame = old_selected_frame;
14819 selected_window = old_selected_window;
14820 return n;
14821 }
14822
14823
14824 /* Display mode or top line of window W. FACE_ID specifies which line
14825 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
14826 FORMAT is the mode line format to display. Value is the pixel
14827 height of the mode line displayed. */
14828
14829 static int
14830 display_mode_line (w, face_id, format)
14831 struct window *w;
14832 enum face_id face_id;
14833 Lisp_Object format;
14834 {
14835 struct it it;
14836 struct face *face;
14837
14838 init_iterator (&it, w, -1, -1, NULL, face_id);
14839 prepare_desired_row (it.glyph_row);
14840
14841 if (! mode_line_inverse_video)
14842 /* Force the mode-line to be displayed in the default face. */
14843 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
14844
14845 /* Temporarily make frame's keyboard the current kboard so that
14846 kboard-local variables in the mode_line_format will get the right
14847 values. */
14848 push_frame_kboard (it.f);
14849 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
14850 pop_frame_kboard ();
14851
14852 /* Fill up with spaces. */
14853 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
14854
14855 compute_line_metrics (&it);
14856 it.glyph_row->full_width_p = 1;
14857 it.glyph_row->mode_line_p = 1;
14858 it.glyph_row->continued_p = 0;
14859 it.glyph_row->truncated_on_left_p = 0;
14860 it.glyph_row->truncated_on_right_p = 0;
14861
14862 /* Make a 3D mode-line have a shadow at its right end. */
14863 face = FACE_FROM_ID (it.f, face_id);
14864 extend_face_to_end_of_line (&it);
14865 if (face->box != FACE_NO_BOX)
14866 {
14867 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
14868 + it.glyph_row->used[TEXT_AREA] - 1);
14869 last->right_box_line_p = 1;
14870 }
14871
14872 return it.glyph_row->height;
14873 }
14874
14875 /* Alist that caches the results of :propertize.
14876 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
14877 Lisp_Object mode_line_proptrans_alist;
14878
14879 /* List of strings making up the mode-line. */
14880 Lisp_Object mode_line_string_list;
14881
14882 /* Base face property when building propertized mode line string. */
14883 static Lisp_Object mode_line_string_face;
14884 static Lisp_Object mode_line_string_face_prop;
14885
14886
14887 /* Contribute ELT to the mode line for window IT->w. How it
14888 translates into text depends on its data type.
14889
14890 IT describes the display environment in which we display, as usual.
14891
14892 DEPTH is the depth in recursion. It is used to prevent
14893 infinite recursion here.
14894
14895 FIELD_WIDTH is the number of characters the display of ELT should
14896 occupy in the mode line, and PRECISION is the maximum number of
14897 characters to display from ELT's representation. See
14898 display_string for details.
14899
14900 Returns the hpos of the end of the text generated by ELT.
14901
14902 PROPS is a property list to add to any string we encounter.
14903
14904 If RISKY is nonzero, remove (disregard) any properties in any string
14905 we encounter, and ignore :eval and :propertize.
14906
14907 If the global variable `frame_title_ptr' is non-NULL, then the output
14908 is passed to `store_frame_title' instead of `display_string'. */
14909
14910 static int
14911 display_mode_element (it, depth, field_width, precision, elt, props, risky)
14912 struct it *it;
14913 int depth;
14914 int field_width, precision;
14915 Lisp_Object elt, props;
14916 int risky;
14917 {
14918 int n = 0, field, prec;
14919 int literal = 0;
14920
14921 tail_recurse:
14922 if (depth > 10)
14923 goto invalid;
14924
14925 depth++;
14926
14927 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
14928 {
14929 case Lisp_String:
14930 {
14931 /* A string: output it and check for %-constructs within it. */
14932 unsigned char c;
14933 const unsigned char *this, *lisp_string;
14934
14935 if (!NILP (props) || risky)
14936 {
14937 Lisp_Object oprops, aelt;
14938 oprops = Ftext_properties_at (make_number (0), elt);
14939
14940 if (NILP (Fequal (props, oprops)) || risky)
14941 {
14942 /* If the starting string has properties,
14943 merge the specified ones onto the existing ones. */
14944 if (! NILP (oprops) && !risky)
14945 {
14946 Lisp_Object tem;
14947
14948 oprops = Fcopy_sequence (oprops);
14949 tem = props;
14950 while (CONSP (tem))
14951 {
14952 oprops = Fplist_put (oprops, XCAR (tem),
14953 XCAR (XCDR (tem)));
14954 tem = XCDR (XCDR (tem));
14955 }
14956 props = oprops;
14957 }
14958
14959 aelt = Fassoc (elt, mode_line_proptrans_alist);
14960 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
14961 {
14962 mode_line_proptrans_alist
14963 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
14964 elt = XCAR (aelt);
14965 }
14966 else
14967 {
14968 Lisp_Object tem;
14969
14970 elt = Fcopy_sequence (elt);
14971 Fset_text_properties (make_number (0), Flength (elt),
14972 props, elt);
14973 /* Add this item to mode_line_proptrans_alist. */
14974 mode_line_proptrans_alist
14975 = Fcons (Fcons (elt, props),
14976 mode_line_proptrans_alist);
14977 /* Truncate mode_line_proptrans_alist
14978 to at most 50 elements. */
14979 tem = Fnthcdr (make_number (50),
14980 mode_line_proptrans_alist);
14981 if (! NILP (tem))
14982 XSETCDR (tem, Qnil);
14983 }
14984 }
14985 }
14986
14987 this = SDATA (elt);
14988 lisp_string = this;
14989
14990 if (literal)
14991 {
14992 prec = precision - n;
14993 if (frame_title_ptr)
14994 n += store_frame_title (SDATA (elt), -1, prec);
14995 else if (!NILP (mode_line_string_list))
14996 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
14997 else
14998 n += display_string (NULL, elt, Qnil, 0, 0, it,
14999 0, prec, 0, STRING_MULTIBYTE (elt));
15000
15001 break;
15002 }
15003
15004 while ((precision <= 0 || n < precision)
15005 && *this
15006 && (frame_title_ptr
15007 || !NILP (mode_line_string_list)
15008 || it->current_x < it->last_visible_x))
15009 {
15010 const unsigned char *last = this;
15011
15012 /* Advance to end of string or next format specifier. */
15013 while ((c = *this++) != '\0' && c != '%')
15014 ;
15015
15016 if (this - 1 != last)
15017 {
15018 /* Output to end of string or up to '%'. Field width
15019 is length of string. Don't output more than
15020 PRECISION allows us. */
15021 --this;
15022
15023 prec = chars_in_text (last, this - last);
15024 if (precision > 0 && prec > precision - n)
15025 prec = precision - n;
15026
15027 if (frame_title_ptr)
15028 n += store_frame_title (last, 0, prec);
15029 else if (!NILP (mode_line_string_list))
15030 {
15031 int bytepos = last - lisp_string;
15032 int charpos = string_byte_to_char (elt, bytepos);
15033 n += store_mode_line_string (NULL,
15034 Fsubstring (elt, make_number (charpos),
15035 make_number (charpos + prec)),
15036 0, 0, 0, Qnil);
15037 }
15038 else
15039 {
15040 int bytepos = last - lisp_string;
15041 int charpos = string_byte_to_char (elt, bytepos);
15042 n += display_string (NULL, elt, Qnil, 0, charpos,
15043 it, 0, prec, 0,
15044 STRING_MULTIBYTE (elt));
15045 }
15046 }
15047 else /* c == '%' */
15048 {
15049 const unsigned char *percent_position = this;
15050
15051 /* Get the specified minimum width. Zero means
15052 don't pad. */
15053 field = 0;
15054 while ((c = *this++) >= '0' && c <= '9')
15055 field = field * 10 + c - '0';
15056
15057 /* Don't pad beyond the total padding allowed. */
15058 if (field_width - n > 0 && field > field_width - n)
15059 field = field_width - n;
15060
15061 /* Note that either PRECISION <= 0 or N < PRECISION. */
15062 prec = precision - n;
15063
15064 if (c == 'M')
15065 n += display_mode_element (it, depth, field, prec,
15066 Vglobal_mode_string, props,
15067 risky);
15068 else if (c != 0)
15069 {
15070 int multibyte;
15071 int bytepos, charpos;
15072 unsigned char *spec;
15073
15074 bytepos = percent_position - lisp_string;
15075 charpos = (STRING_MULTIBYTE (elt)
15076 ? string_byte_to_char (elt, bytepos)
15077 : bytepos);
15078
15079 spec
15080 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15081
15082 if (frame_title_ptr)
15083 n += store_frame_title (spec, field, prec);
15084 else if (!NILP (mode_line_string_list))
15085 {
15086 int len = strlen (spec);
15087 Lisp_Object tem = make_string (spec, len);
15088 props = Ftext_properties_at (make_number (charpos), elt);
15089 /* Should only keep face property in props */
15090 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15091 }
15092 else
15093 {
15094 int nglyphs_before, nwritten;
15095
15096 nglyphs_before = it->glyph_row->used[TEXT_AREA];
15097 nwritten = display_string (spec, Qnil, elt,
15098 charpos, 0, it,
15099 field, prec, 0,
15100 multibyte);
15101
15102 /* Assign to the glyphs written above the
15103 string where the `%x' came from, position
15104 of the `%'. */
15105 if (nwritten > 0)
15106 {
15107 struct glyph *glyph
15108 = (it->glyph_row->glyphs[TEXT_AREA]
15109 + nglyphs_before);
15110 int i;
15111
15112 for (i = 0; i < nwritten; ++i)
15113 {
15114 glyph[i].object = elt;
15115 glyph[i].charpos = charpos;
15116 }
15117
15118 n += nwritten;
15119 }
15120 }
15121 }
15122 else /* c == 0 */
15123 break;
15124 }
15125 }
15126 }
15127 break;
15128
15129 case Lisp_Symbol:
15130 /* A symbol: process the value of the symbol recursively
15131 as if it appeared here directly. Avoid error if symbol void.
15132 Special case: if value of symbol is a string, output the string
15133 literally. */
15134 {
15135 register Lisp_Object tem;
15136
15137 /* If the variable is not marked as risky to set
15138 then its contents are risky to use. */
15139 if (NILP (Fget (elt, Qrisky_local_variable)))
15140 risky = 1;
15141
15142 tem = Fboundp (elt);
15143 if (!NILP (tem))
15144 {
15145 tem = Fsymbol_value (elt);
15146 /* If value is a string, output that string literally:
15147 don't check for % within it. */
15148 if (STRINGP (tem))
15149 literal = 1;
15150
15151 if (!EQ (tem, elt))
15152 {
15153 /* Give up right away for nil or t. */
15154 elt = tem;
15155 goto tail_recurse;
15156 }
15157 }
15158 }
15159 break;
15160
15161 case Lisp_Cons:
15162 {
15163 register Lisp_Object car, tem;
15164
15165 /* A cons cell: five distinct cases.
15166 If first element is :eval or :propertize, do something special.
15167 If first element is a string or a cons, process all the elements
15168 and effectively concatenate them.
15169 If first element is a negative number, truncate displaying cdr to
15170 at most that many characters. If positive, pad (with spaces)
15171 to at least that many characters.
15172 If first element is a symbol, process the cadr or caddr recursively
15173 according to whether the symbol's value is non-nil or nil. */
15174 car = XCAR (elt);
15175 if (EQ (car, QCeval))
15176 {
15177 /* An element of the form (:eval FORM) means evaluate FORM
15178 and use the result as mode line elements. */
15179
15180 if (risky)
15181 break;
15182
15183 if (CONSP (XCDR (elt)))
15184 {
15185 Lisp_Object spec;
15186 spec = safe_eval (XCAR (XCDR (elt)));
15187 n += display_mode_element (it, depth, field_width - n,
15188 precision - n, spec, props,
15189 risky);
15190 }
15191 }
15192 else if (EQ (car, QCpropertize))
15193 {
15194 /* An element of the form (:propertize ELT PROPS...)
15195 means display ELT but applying properties PROPS. */
15196
15197 if (risky)
15198 break;
15199
15200 if (CONSP (XCDR (elt)))
15201 n += display_mode_element (it, depth, field_width - n,
15202 precision - n, XCAR (XCDR (elt)),
15203 XCDR (XCDR (elt)), risky);
15204 }
15205 else if (SYMBOLP (car))
15206 {
15207 tem = Fboundp (car);
15208 elt = XCDR (elt);
15209 if (!CONSP (elt))
15210 goto invalid;
15211 /* elt is now the cdr, and we know it is a cons cell.
15212 Use its car if CAR has a non-nil value. */
15213 if (!NILP (tem))
15214 {
15215 tem = Fsymbol_value (car);
15216 if (!NILP (tem))
15217 {
15218 elt = XCAR (elt);
15219 goto tail_recurse;
15220 }
15221 }
15222 /* Symbol's value is nil (or symbol is unbound)
15223 Get the cddr of the original list
15224 and if possible find the caddr and use that. */
15225 elt = XCDR (elt);
15226 if (NILP (elt))
15227 break;
15228 else if (!CONSP (elt))
15229 goto invalid;
15230 elt = XCAR (elt);
15231 goto tail_recurse;
15232 }
15233 else if (INTEGERP (car))
15234 {
15235 register int lim = XINT (car);
15236 elt = XCDR (elt);
15237 if (lim < 0)
15238 {
15239 /* Negative int means reduce maximum width. */
15240 if (precision <= 0)
15241 precision = -lim;
15242 else
15243 precision = min (precision, -lim);
15244 }
15245 else if (lim > 0)
15246 {
15247 /* Padding specified. Don't let it be more than
15248 current maximum. */
15249 if (precision > 0)
15250 lim = min (precision, lim);
15251
15252 /* If that's more padding than already wanted, queue it.
15253 But don't reduce padding already specified even if
15254 that is beyond the current truncation point. */
15255 field_width = max (lim, field_width);
15256 }
15257 goto tail_recurse;
15258 }
15259 else if (STRINGP (car) || CONSP (car))
15260 {
15261 register int limit = 50;
15262 /* Limit is to protect against circular lists. */
15263 while (CONSP (elt)
15264 && --limit > 0
15265 && (precision <= 0 || n < precision))
15266 {
15267 n += display_mode_element (it, depth, field_width - n,
15268 precision - n, XCAR (elt),
15269 props, risky);
15270 elt = XCDR (elt);
15271 }
15272 }
15273 }
15274 break;
15275
15276 default:
15277 invalid:
15278 if (frame_title_ptr)
15279 n += store_frame_title ("*invalid*", 0, precision - n);
15280 else if (!NILP (mode_line_string_list))
15281 n += store_mode_line_string ("*invalid*", Qnil, 0, 0, precision - n, Qnil);
15282 else
15283 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
15284 precision - n, 0, 0);
15285 return n;
15286 }
15287
15288 /* Pad to FIELD_WIDTH. */
15289 if (field_width > 0 && n < field_width)
15290 {
15291 if (frame_title_ptr)
15292 n += store_frame_title ("", field_width - n, 0);
15293 else if (!NILP (mode_line_string_list))
15294 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
15295 else
15296 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
15297 0, 0, 0);
15298 }
15299
15300 return n;
15301 }
15302
15303 /* Store a mode-line string element in mode_line_string_list.
15304
15305 If STRING is non-null, display that C string. Otherwise, the Lisp
15306 string LISP_STRING is displayed.
15307
15308 FIELD_WIDTH is the minimum number of output glyphs to produce.
15309 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15310 with spaces. FIELD_WIDTH <= 0 means don't pad.
15311
15312 PRECISION is the maximum number of characters to output from
15313 STRING. PRECISION <= 0 means don't truncate the string.
15314
15315 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
15316 properties to the string.
15317
15318 PROPS are the properties to add to the string.
15319 The mode_line_string_face face property is always added to the string.
15320 */
15321
15322 static int store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
15323 char *string;
15324 Lisp_Object lisp_string;
15325 int copy_string;
15326 int field_width;
15327 int precision;
15328 Lisp_Object props;
15329 {
15330 int len;
15331 int n = 0;
15332
15333 if (string != NULL)
15334 {
15335 len = strlen (string);
15336 if (precision > 0 && len > precision)
15337 len = precision;
15338 lisp_string = make_string (string, len);
15339 if (NILP (props))
15340 props = mode_line_string_face_prop;
15341 else if (!NILP (mode_line_string_face))
15342 {
15343 Lisp_Object face = Fplist_get (props, Qface);
15344 props = Fcopy_sequence (props);
15345 if (NILP (face))
15346 face = mode_line_string_face;
15347 else
15348 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15349 props = Fplist_put (props, Qface, face);
15350 }
15351 Fadd_text_properties (make_number (0), make_number (len),
15352 props, lisp_string);
15353 }
15354 else
15355 {
15356 len = XFASTINT (Flength (lisp_string));
15357 if (precision > 0 && len > precision)
15358 {
15359 len = precision;
15360 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
15361 precision = -1;
15362 }
15363 if (!NILP (mode_line_string_face))
15364 {
15365 Lisp_Object face;
15366 if (NILP (props))
15367 props = Ftext_properties_at (make_number (0), lisp_string);
15368 face = Fplist_get (props, Qface);
15369 if (NILP (face))
15370 face = mode_line_string_face;
15371 else
15372 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15373 props = Fcons (Qface, Fcons (face, Qnil));
15374 if (copy_string)
15375 lisp_string = Fcopy_sequence (lisp_string);
15376 }
15377 if (!NILP (props))
15378 Fadd_text_properties (make_number (0), make_number (len),
15379 props, lisp_string);
15380 }
15381
15382 if (len > 0)
15383 {
15384 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15385 n += len;
15386 }
15387
15388 if (field_width > len)
15389 {
15390 field_width -= len;
15391 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
15392 if (!NILP (props))
15393 Fadd_text_properties (make_number (0), make_number (field_width),
15394 props, lisp_string);
15395 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15396 n += field_width;
15397 }
15398
15399 return n;
15400 }
15401
15402
15403 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
15404 0, 3, 0,
15405 doc: /* Return the mode-line of selected window as a string.
15406 First optional arg FORMAT specifies a different format string (see
15407 `mode-line-format' for details) to use. If FORMAT is t, return
15408 the buffer's header-line. Second optional arg WINDOW specifies a
15409 different window to use as the context for the formatting.
15410 If third optional arg NO-PROPS is non-nil, string is not propertized. */)
15411 (format, window, no_props)
15412 Lisp_Object format, window, no_props;
15413 {
15414 struct it it;
15415 int len;
15416 struct window *w;
15417 struct buffer *old_buffer = NULL;
15418 enum face_id face_id = DEFAULT_FACE_ID;
15419
15420 if (NILP (window))
15421 window = selected_window;
15422 CHECK_WINDOW (window);
15423 w = XWINDOW (window);
15424 CHECK_BUFFER (w->buffer);
15425
15426 if (XBUFFER (w->buffer) != current_buffer)
15427 {
15428 old_buffer = current_buffer;
15429 set_buffer_internal_1 (XBUFFER (w->buffer));
15430 }
15431
15432 if (NILP (format) || EQ (format, Qt))
15433 {
15434 face_id = NILP (format)
15435 ? CURRENT_MODE_LINE_FACE_ID (w) :
15436 HEADER_LINE_FACE_ID;
15437 format = NILP (format)
15438 ? current_buffer->mode_line_format
15439 : current_buffer->header_line_format;
15440 }
15441
15442 init_iterator (&it, w, -1, -1, NULL, face_id);
15443
15444 if (NILP (no_props))
15445 {
15446 mode_line_string_face =
15447 (face_id == MODE_LINE_FACE_ID ? Qmode_line :
15448 face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive :
15449 face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
15450
15451 mode_line_string_face_prop =
15452 NILP (mode_line_string_face) ? Qnil :
15453 Fcons (Qface, Fcons (mode_line_string_face, Qnil));
15454
15455 /* We need a dummy last element in mode_line_string_list to
15456 indicate we are building the propertized mode-line string.
15457 Using mode_line_string_face_prop here GC protects it. */
15458 mode_line_string_list =
15459 Fcons (mode_line_string_face_prop, Qnil);
15460 frame_title_ptr = NULL;
15461 }
15462 else
15463 {
15464 mode_line_string_face_prop = Qnil;
15465 mode_line_string_list = Qnil;
15466 frame_title_ptr = frame_title_buf;
15467 }
15468
15469 push_frame_kboard (it.f);
15470 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15471 pop_frame_kboard ();
15472
15473 if (old_buffer)
15474 set_buffer_internal_1 (old_buffer);
15475
15476 if (NILP (no_props))
15477 {
15478 Lisp_Object str;
15479 mode_line_string_list = Fnreverse (mode_line_string_list);
15480 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
15481 make_string ("", 0));
15482 mode_line_string_face_prop = Qnil;
15483 mode_line_string_list = Qnil;
15484 return str;
15485 }
15486
15487 len = frame_title_ptr - frame_title_buf;
15488 if (len > 0 && frame_title_ptr[-1] == '-')
15489 {
15490 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
15491 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
15492 ;
15493 frame_title_ptr += 3; /* restore last non-dash + two dashes */
15494 if (len > frame_title_ptr - frame_title_buf)
15495 len = frame_title_ptr - frame_title_buf;
15496 }
15497
15498 frame_title_ptr = NULL;
15499 return make_string (frame_title_buf, len);
15500 }
15501
15502 /* Write a null-terminated, right justified decimal representation of
15503 the positive integer D to BUF using a minimal field width WIDTH. */
15504
15505 static void
15506 pint2str (buf, width, d)
15507 register char *buf;
15508 register int width;
15509 register int d;
15510 {
15511 register char *p = buf;
15512
15513 if (d <= 0)
15514 *p++ = '0';
15515 else
15516 {
15517 while (d > 0)
15518 {
15519 *p++ = d % 10 + '0';
15520 d /= 10;
15521 }
15522 }
15523
15524 for (width -= (int) (p - buf); width > 0; --width)
15525 *p++ = ' ';
15526 *p-- = '\0';
15527 while (p > buf)
15528 {
15529 d = *buf;
15530 *buf++ = *p;
15531 *p-- = d;
15532 }
15533 }
15534
15535 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
15536 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
15537 type of CODING_SYSTEM. Return updated pointer into BUF. */
15538
15539 static unsigned char invalid_eol_type[] = "(*invalid*)";
15540
15541 static char *
15542 decode_mode_spec_coding (coding_system, buf, eol_flag)
15543 Lisp_Object coding_system;
15544 register char *buf;
15545 int eol_flag;
15546 {
15547 Lisp_Object val;
15548 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
15549 const unsigned char *eol_str;
15550 int eol_str_len;
15551 /* The EOL conversion we are using. */
15552 Lisp_Object eoltype;
15553
15554 val = Fget (coding_system, Qcoding_system);
15555 eoltype = Qnil;
15556
15557 if (!VECTORP (val)) /* Not yet decided. */
15558 {
15559 if (multibyte)
15560 *buf++ = '-';
15561 if (eol_flag)
15562 eoltype = eol_mnemonic_undecided;
15563 /* Don't mention EOL conversion if it isn't decided. */
15564 }
15565 else
15566 {
15567 Lisp_Object eolvalue;
15568
15569 eolvalue = Fget (coding_system, Qeol_type);
15570
15571 if (multibyte)
15572 *buf++ = XFASTINT (AREF (val, 1));
15573
15574 if (eol_flag)
15575 {
15576 /* The EOL conversion that is normal on this system. */
15577
15578 if (NILP (eolvalue)) /* Not yet decided. */
15579 eoltype = eol_mnemonic_undecided;
15580 else if (VECTORP (eolvalue)) /* Not yet decided. */
15581 eoltype = eol_mnemonic_undecided;
15582 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
15583 eoltype = (XFASTINT (eolvalue) == 0
15584 ? eol_mnemonic_unix
15585 : (XFASTINT (eolvalue) == 1
15586 ? eol_mnemonic_dos : eol_mnemonic_mac));
15587 }
15588 }
15589
15590 if (eol_flag)
15591 {
15592 /* Mention the EOL conversion if it is not the usual one. */
15593 if (STRINGP (eoltype))
15594 {
15595 eol_str = SDATA (eoltype);
15596 eol_str_len = SBYTES (eoltype);
15597 }
15598 else if (INTEGERP (eoltype)
15599 && CHAR_VALID_P (XINT (eoltype), 0))
15600 {
15601 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
15602 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
15603 eol_str = tmp;
15604 }
15605 else
15606 {
15607 eol_str = invalid_eol_type;
15608 eol_str_len = sizeof (invalid_eol_type) - 1;
15609 }
15610 bcopy (eol_str, buf, eol_str_len);
15611 buf += eol_str_len;
15612 }
15613
15614 return buf;
15615 }
15616
15617 /* Return a string for the output of a mode line %-spec for window W,
15618 generated by character C. PRECISION >= 0 means don't return a
15619 string longer than that value. FIELD_WIDTH > 0 means pad the
15620 string returned with spaces to that value. Return 1 in *MULTIBYTE
15621 if the result is multibyte text. */
15622
15623 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
15624
15625 static char *
15626 decode_mode_spec (w, c, field_width, precision, multibyte)
15627 struct window *w;
15628 register int c;
15629 int field_width, precision;
15630 int *multibyte;
15631 {
15632 Lisp_Object obj;
15633 struct frame *f = XFRAME (WINDOW_FRAME (w));
15634 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
15635 struct buffer *b = XBUFFER (w->buffer);
15636
15637 obj = Qnil;
15638 *multibyte = 0;
15639
15640 switch (c)
15641 {
15642 case '*':
15643 if (!NILP (b->read_only))
15644 return "%";
15645 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
15646 return "*";
15647 return "-";
15648
15649 case '+':
15650 /* This differs from %* only for a modified read-only buffer. */
15651 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
15652 return "*";
15653 if (!NILP (b->read_only))
15654 return "%";
15655 return "-";
15656
15657 case '&':
15658 /* This differs from %* in ignoring read-only-ness. */
15659 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
15660 return "*";
15661 return "-";
15662
15663 case '%':
15664 return "%";
15665
15666 case '[':
15667 {
15668 int i;
15669 char *p;
15670
15671 if (command_loop_level > 5)
15672 return "[[[... ";
15673 p = decode_mode_spec_buf;
15674 for (i = 0; i < command_loop_level; i++)
15675 *p++ = '[';
15676 *p = 0;
15677 return decode_mode_spec_buf;
15678 }
15679
15680 case ']':
15681 {
15682 int i;
15683 char *p;
15684
15685 if (command_loop_level > 5)
15686 return " ...]]]";
15687 p = decode_mode_spec_buf;
15688 for (i = 0; i < command_loop_level; i++)
15689 *p++ = ']';
15690 *p = 0;
15691 return decode_mode_spec_buf;
15692 }
15693
15694 case '-':
15695 {
15696 register int i;
15697
15698 /* Let lots_of_dashes be a string of infinite length. */
15699 if (!NILP (mode_line_string_list))
15700 return "--";
15701 if (field_width <= 0
15702 || field_width > sizeof (lots_of_dashes))
15703 {
15704 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
15705 decode_mode_spec_buf[i] = '-';
15706 decode_mode_spec_buf[i] = '\0';
15707 return decode_mode_spec_buf;
15708 }
15709 else
15710 return lots_of_dashes;
15711 }
15712
15713 case 'b':
15714 obj = b->name;
15715 break;
15716
15717 case 'c':
15718 {
15719 int col = (int) current_column (); /* iftc */
15720 w->column_number_displayed = make_number (col);
15721 pint2str (decode_mode_spec_buf, field_width, col);
15722 return decode_mode_spec_buf;
15723 }
15724
15725 case 'F':
15726 /* %F displays the frame name. */
15727 if (!NILP (f->title))
15728 return (char *) SDATA (f->title);
15729 if (f->explicit_name || ! FRAME_WINDOW_P (f))
15730 return (char *) SDATA (f->name);
15731 return "Emacs";
15732
15733 case 'f':
15734 obj = b->filename;
15735 break;
15736
15737 case 'l':
15738 {
15739 int startpos = XMARKER (w->start)->charpos;
15740 int startpos_byte = marker_byte_position (w->start);
15741 int line, linepos, linepos_byte, topline;
15742 int nlines, junk;
15743 int height = XFASTINT (w->height);
15744
15745 /* If we decided that this buffer isn't suitable for line numbers,
15746 don't forget that too fast. */
15747 if (EQ (w->base_line_pos, w->buffer))
15748 goto no_value;
15749 /* But do forget it, if the window shows a different buffer now. */
15750 else if (BUFFERP (w->base_line_pos))
15751 w->base_line_pos = Qnil;
15752
15753 /* If the buffer is very big, don't waste time. */
15754 if (INTEGERP (Vline_number_display_limit)
15755 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
15756 {
15757 w->base_line_pos = Qnil;
15758 w->base_line_number = Qnil;
15759 goto no_value;
15760 }
15761
15762 if (!NILP (w->base_line_number)
15763 && !NILP (w->base_line_pos)
15764 && XFASTINT (w->base_line_pos) <= startpos)
15765 {
15766 line = XFASTINT (w->base_line_number);
15767 linepos = XFASTINT (w->base_line_pos);
15768 linepos_byte = buf_charpos_to_bytepos (b, linepos);
15769 }
15770 else
15771 {
15772 line = 1;
15773 linepos = BUF_BEGV (b);
15774 linepos_byte = BUF_BEGV_BYTE (b);
15775 }
15776
15777 /* Count lines from base line to window start position. */
15778 nlines = display_count_lines (linepos, linepos_byte,
15779 startpos_byte,
15780 startpos, &junk);
15781
15782 topline = nlines + line;
15783
15784 /* Determine a new base line, if the old one is too close
15785 or too far away, or if we did not have one.
15786 "Too close" means it's plausible a scroll-down would
15787 go back past it. */
15788 if (startpos == BUF_BEGV (b))
15789 {
15790 w->base_line_number = make_number (topline);
15791 w->base_line_pos = make_number (BUF_BEGV (b));
15792 }
15793 else if (nlines < height + 25 || nlines > height * 3 + 50
15794 || linepos == BUF_BEGV (b))
15795 {
15796 int limit = BUF_BEGV (b);
15797 int limit_byte = BUF_BEGV_BYTE (b);
15798 int position;
15799 int distance = (height * 2 + 30) * line_number_display_limit_width;
15800
15801 if (startpos - distance > limit)
15802 {
15803 limit = startpos - distance;
15804 limit_byte = CHAR_TO_BYTE (limit);
15805 }
15806
15807 nlines = display_count_lines (startpos, startpos_byte,
15808 limit_byte,
15809 - (height * 2 + 30),
15810 &position);
15811 /* If we couldn't find the lines we wanted within
15812 line_number_display_limit_width chars per line,
15813 give up on line numbers for this window. */
15814 if (position == limit_byte && limit == startpos - distance)
15815 {
15816 w->base_line_pos = w->buffer;
15817 w->base_line_number = Qnil;
15818 goto no_value;
15819 }
15820
15821 w->base_line_number = make_number (topline - nlines);
15822 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
15823 }
15824
15825 /* Now count lines from the start pos to point. */
15826 nlines = display_count_lines (startpos, startpos_byte,
15827 PT_BYTE, PT, &junk);
15828
15829 /* Record that we did display the line number. */
15830 line_number_displayed = 1;
15831
15832 /* Make the string to show. */
15833 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
15834 return decode_mode_spec_buf;
15835 no_value:
15836 {
15837 char* p = decode_mode_spec_buf;
15838 int pad = field_width - 2;
15839 while (pad-- > 0)
15840 *p++ = ' ';
15841 *p++ = '?';
15842 *p++ = '?';
15843 *p = '\0';
15844 return decode_mode_spec_buf;
15845 }
15846 }
15847 break;
15848
15849 case 'm':
15850 obj = b->mode_name;
15851 break;
15852
15853 case 'n':
15854 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
15855 return " Narrow";
15856 break;
15857
15858 case 'p':
15859 {
15860 int pos = marker_position (w->start);
15861 int total = BUF_ZV (b) - BUF_BEGV (b);
15862
15863 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
15864 {
15865 if (pos <= BUF_BEGV (b))
15866 return "All";
15867 else
15868 return "Bottom";
15869 }
15870 else if (pos <= BUF_BEGV (b))
15871 return "Top";
15872 else
15873 {
15874 if (total > 1000000)
15875 /* Do it differently for a large value, to avoid overflow. */
15876 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
15877 else
15878 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
15879 /* We can't normally display a 3-digit number,
15880 so get us a 2-digit number that is close. */
15881 if (total == 100)
15882 total = 99;
15883 sprintf (decode_mode_spec_buf, "%2d%%", total);
15884 return decode_mode_spec_buf;
15885 }
15886 }
15887
15888 /* Display percentage of size above the bottom of the screen. */
15889 case 'P':
15890 {
15891 int toppos = marker_position (w->start);
15892 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
15893 int total = BUF_ZV (b) - BUF_BEGV (b);
15894
15895 if (botpos >= BUF_ZV (b))
15896 {
15897 if (toppos <= BUF_BEGV (b))
15898 return "All";
15899 else
15900 return "Bottom";
15901 }
15902 else
15903 {
15904 if (total > 1000000)
15905 /* Do it differently for a large value, to avoid overflow. */
15906 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
15907 else
15908 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
15909 /* We can't normally display a 3-digit number,
15910 so get us a 2-digit number that is close. */
15911 if (total == 100)
15912 total = 99;
15913 if (toppos <= BUF_BEGV (b))
15914 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
15915 else
15916 sprintf (decode_mode_spec_buf, "%2d%%", total);
15917 return decode_mode_spec_buf;
15918 }
15919 }
15920
15921 case 's':
15922 /* status of process */
15923 obj = Fget_buffer_process (w->buffer);
15924 if (NILP (obj))
15925 return "no process";
15926 #ifdef subprocesses
15927 obj = Fsymbol_name (Fprocess_status (obj));
15928 #endif
15929 break;
15930
15931 case 't': /* indicate TEXT or BINARY */
15932 #ifdef MODE_LINE_BINARY_TEXT
15933 return MODE_LINE_BINARY_TEXT (b);
15934 #else
15935 return "T";
15936 #endif
15937
15938 case 'z':
15939 /* coding-system (not including end-of-line format) */
15940 case 'Z':
15941 /* coding-system (including end-of-line type) */
15942 {
15943 int eol_flag = (c == 'Z');
15944 char *p = decode_mode_spec_buf;
15945
15946 if (! FRAME_WINDOW_P (f))
15947 {
15948 /* No need to mention EOL here--the terminal never needs
15949 to do EOL conversion. */
15950 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
15951 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
15952 }
15953 p = decode_mode_spec_coding (b->buffer_file_coding_system,
15954 p, eol_flag);
15955
15956 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
15957 #ifdef subprocesses
15958 obj = Fget_buffer_process (Fcurrent_buffer ());
15959 if (PROCESSP (obj))
15960 {
15961 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
15962 p, eol_flag);
15963 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
15964 p, eol_flag);
15965 }
15966 #endif /* subprocesses */
15967 #endif /* 0 */
15968 *p = 0;
15969 return decode_mode_spec_buf;
15970 }
15971 }
15972
15973 if (STRINGP (obj))
15974 {
15975 *multibyte = STRING_MULTIBYTE (obj);
15976 return (char *) SDATA (obj);
15977 }
15978 else
15979 return "";
15980 }
15981
15982
15983 /* Count up to COUNT lines starting from START / START_BYTE.
15984 But don't go beyond LIMIT_BYTE.
15985 Return the number of lines thus found (always nonnegative).
15986
15987 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
15988
15989 static int
15990 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
15991 int start, start_byte, limit_byte, count;
15992 int *byte_pos_ptr;
15993 {
15994 register unsigned char *cursor;
15995 unsigned char *base;
15996
15997 register int ceiling;
15998 register unsigned char *ceiling_addr;
15999 int orig_count = count;
16000
16001 /* If we are not in selective display mode,
16002 check only for newlines. */
16003 int selective_display = (!NILP (current_buffer->selective_display)
16004 && !INTEGERP (current_buffer->selective_display));
16005
16006 if (count > 0)
16007 {
16008 while (start_byte < limit_byte)
16009 {
16010 ceiling = BUFFER_CEILING_OF (start_byte);
16011 ceiling = min (limit_byte - 1, ceiling);
16012 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16013 base = (cursor = BYTE_POS_ADDR (start_byte));
16014 while (1)
16015 {
16016 if (selective_display)
16017 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16018 ;
16019 else
16020 while (*cursor != '\n' && ++cursor != ceiling_addr)
16021 ;
16022
16023 if (cursor != ceiling_addr)
16024 {
16025 if (--count == 0)
16026 {
16027 start_byte += cursor - base + 1;
16028 *byte_pos_ptr = start_byte;
16029 return orig_count;
16030 }
16031 else
16032 if (++cursor == ceiling_addr)
16033 break;
16034 }
16035 else
16036 break;
16037 }
16038 start_byte += cursor - base;
16039 }
16040 }
16041 else
16042 {
16043 while (start_byte > limit_byte)
16044 {
16045 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16046 ceiling = max (limit_byte, ceiling);
16047 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16048 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16049 while (1)
16050 {
16051 if (selective_display)
16052 while (--cursor != ceiling_addr
16053 && *cursor != '\n' && *cursor != 015)
16054 ;
16055 else
16056 while (--cursor != ceiling_addr && *cursor != '\n')
16057 ;
16058
16059 if (cursor != ceiling_addr)
16060 {
16061 if (++count == 0)
16062 {
16063 start_byte += cursor - base + 1;
16064 *byte_pos_ptr = start_byte;
16065 /* When scanning backwards, we should
16066 not count the newline posterior to which we stop. */
16067 return - orig_count - 1;
16068 }
16069 }
16070 else
16071 break;
16072 }
16073 /* Here we add 1 to compensate for the last decrement
16074 of CURSOR, which took it past the valid range. */
16075 start_byte += cursor - base + 1;
16076 }
16077 }
16078
16079 *byte_pos_ptr = limit_byte;
16080
16081 if (count < 0)
16082 return - orig_count + count;
16083 return orig_count - count;
16084
16085 }
16086
16087
16088 \f
16089 /***********************************************************************
16090 Displaying strings
16091 ***********************************************************************/
16092
16093 /* Display a NUL-terminated string, starting with index START.
16094
16095 If STRING is non-null, display that C string. Otherwise, the Lisp
16096 string LISP_STRING is displayed.
16097
16098 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16099 FACE_STRING. Display STRING or LISP_STRING with the face at
16100 FACE_STRING_POS in FACE_STRING:
16101
16102 Display the string in the environment given by IT, but use the
16103 standard display table, temporarily.
16104
16105 FIELD_WIDTH is the minimum number of output glyphs to produce.
16106 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16107 with spaces. If STRING has more characters, more than FIELD_WIDTH
16108 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
16109
16110 PRECISION is the maximum number of characters to output from
16111 STRING. PRECISION < 0 means don't truncate the string.
16112
16113 This is roughly equivalent to printf format specifiers:
16114
16115 FIELD_WIDTH PRECISION PRINTF
16116 ----------------------------------------
16117 -1 -1 %s
16118 -1 10 %.10s
16119 10 -1 %10s
16120 20 10 %20.10s
16121
16122 MULTIBYTE zero means do not display multibyte chars, > 0 means do
16123 display them, and < 0 means obey the current buffer's value of
16124 enable_multibyte_characters.
16125
16126 Value is the number of glyphs produced. */
16127
16128 static int
16129 display_string (string, lisp_string, face_string, face_string_pos,
16130 start, it, field_width, precision, max_x, multibyte)
16131 unsigned char *string;
16132 Lisp_Object lisp_string;
16133 Lisp_Object face_string;
16134 int face_string_pos;
16135 int start;
16136 struct it *it;
16137 int field_width, precision, max_x;
16138 int multibyte;
16139 {
16140 int hpos_at_start = it->hpos;
16141 int saved_face_id = it->face_id;
16142 struct glyph_row *row = it->glyph_row;
16143
16144 /* Initialize the iterator IT for iteration over STRING beginning
16145 with index START. */
16146 reseat_to_string (it, string, lisp_string, start,
16147 precision, field_width, multibyte);
16148
16149 /* If displaying STRING, set up the face of the iterator
16150 from LISP_STRING, if that's given. */
16151 if (STRINGP (face_string))
16152 {
16153 int endptr;
16154 struct face *face;
16155
16156 it->face_id
16157 = face_at_string_position (it->w, face_string, face_string_pos,
16158 0, it->region_beg_charpos,
16159 it->region_end_charpos,
16160 &endptr, it->base_face_id, 0);
16161 face = FACE_FROM_ID (it->f, it->face_id);
16162 it->face_box_p = face->box != FACE_NO_BOX;
16163 }
16164
16165 /* Set max_x to the maximum allowed X position. Don't let it go
16166 beyond the right edge of the window. */
16167 if (max_x <= 0)
16168 max_x = it->last_visible_x;
16169 else
16170 max_x = min (max_x, it->last_visible_x);
16171
16172 /* Skip over display elements that are not visible. because IT->w is
16173 hscrolled. */
16174 if (it->current_x < it->first_visible_x)
16175 move_it_in_display_line_to (it, 100000, it->first_visible_x,
16176 MOVE_TO_POS | MOVE_TO_X);
16177
16178 row->ascent = it->max_ascent;
16179 row->height = it->max_ascent + it->max_descent;
16180 row->phys_ascent = it->max_phys_ascent;
16181 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16182
16183 /* This condition is for the case that we are called with current_x
16184 past last_visible_x. */
16185 while (it->current_x < max_x)
16186 {
16187 int x_before, x, n_glyphs_before, i, nglyphs;
16188
16189 /* Get the next display element. */
16190 if (!get_next_display_element (it))
16191 break;
16192
16193 /* Produce glyphs. */
16194 x_before = it->current_x;
16195 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
16196 PRODUCE_GLYPHS (it);
16197
16198 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
16199 i = 0;
16200 x = x_before;
16201 while (i < nglyphs)
16202 {
16203 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16204
16205 if (!it->truncate_lines_p
16206 && x + glyph->pixel_width > max_x)
16207 {
16208 /* End of continued line or max_x reached. */
16209 if (CHAR_GLYPH_PADDING_P (*glyph))
16210 {
16211 /* A wide character is unbreakable. */
16212 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
16213 it->current_x = x_before;
16214 }
16215 else
16216 {
16217 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
16218 it->current_x = x;
16219 }
16220 break;
16221 }
16222 else if (x + glyph->pixel_width > it->first_visible_x)
16223 {
16224 /* Glyph is at least partially visible. */
16225 ++it->hpos;
16226 if (x < it->first_visible_x)
16227 it->glyph_row->x = x - it->first_visible_x;
16228 }
16229 else
16230 {
16231 /* Glyph is off the left margin of the display area.
16232 Should not happen. */
16233 abort ();
16234 }
16235
16236 row->ascent = max (row->ascent, it->max_ascent);
16237 row->height = max (row->height, it->max_ascent + it->max_descent);
16238 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16239 row->phys_height = max (row->phys_height,
16240 it->max_phys_ascent + it->max_phys_descent);
16241 x += glyph->pixel_width;
16242 ++i;
16243 }
16244
16245 /* Stop if max_x reached. */
16246 if (i < nglyphs)
16247 break;
16248
16249 /* Stop at line ends. */
16250 if (ITERATOR_AT_END_OF_LINE_P (it))
16251 {
16252 it->continuation_lines_width = 0;
16253 break;
16254 }
16255
16256 set_iterator_to_next (it, 1);
16257
16258 /* Stop if truncating at the right edge. */
16259 if (it->truncate_lines_p
16260 && it->current_x >= it->last_visible_x)
16261 {
16262 /* Add truncation mark, but don't do it if the line is
16263 truncated at a padding space. */
16264 if (IT_CHARPOS (*it) < it->string_nchars)
16265 {
16266 if (!FRAME_WINDOW_P (it->f))
16267 {
16268 int i, n;
16269
16270 if (it->current_x > it->last_visible_x)
16271 {
16272 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16273 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16274 break;
16275 for (n = row->used[TEXT_AREA]; i < n; ++i)
16276 {
16277 row->used[TEXT_AREA] = i;
16278 produce_special_glyphs (it, IT_TRUNCATION);
16279 }
16280 }
16281 produce_special_glyphs (it, IT_TRUNCATION);
16282 }
16283 it->glyph_row->truncated_on_right_p = 1;
16284 }
16285 break;
16286 }
16287 }
16288
16289 /* Maybe insert a truncation at the left. */
16290 if (it->first_visible_x
16291 && IT_CHARPOS (*it) > 0)
16292 {
16293 if (!FRAME_WINDOW_P (it->f))
16294 insert_left_trunc_glyphs (it);
16295 it->glyph_row->truncated_on_left_p = 1;
16296 }
16297
16298 it->face_id = saved_face_id;
16299
16300 /* Value is number of columns displayed. */
16301 return it->hpos - hpos_at_start;
16302 }
16303
16304
16305 \f
16306 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
16307 appears as an element of LIST or as the car of an element of LIST.
16308 If PROPVAL is a list, compare each element against LIST in that
16309 way, and return 1/2 if any element of PROPVAL is found in LIST.
16310 Otherwise return 0. This function cannot quit.
16311 The return value is 2 if the text is invisible but with an ellipsis
16312 and 1 if it's invisible and without an ellipsis. */
16313
16314 int
16315 invisible_p (propval, list)
16316 register Lisp_Object propval;
16317 Lisp_Object list;
16318 {
16319 register Lisp_Object tail, proptail;
16320
16321 for (tail = list; CONSP (tail); tail = XCDR (tail))
16322 {
16323 register Lisp_Object tem;
16324 tem = XCAR (tail);
16325 if (EQ (propval, tem))
16326 return 1;
16327 if (CONSP (tem) && EQ (propval, XCAR (tem)))
16328 return NILP (XCDR (tem)) ? 1 : 2;
16329 }
16330
16331 if (CONSP (propval))
16332 {
16333 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
16334 {
16335 Lisp_Object propelt;
16336 propelt = XCAR (proptail);
16337 for (tail = list; CONSP (tail); tail = XCDR (tail))
16338 {
16339 register Lisp_Object tem;
16340 tem = XCAR (tail);
16341 if (EQ (propelt, tem))
16342 return 1;
16343 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
16344 return NILP (XCDR (tem)) ? 1 : 2;
16345 }
16346 }
16347 }
16348
16349 return 0;
16350 }
16351
16352 \f
16353 /***********************************************************************
16354 Glyph Display
16355 ***********************************************************************/
16356
16357 #ifdef HAVE_WINDOW_SYSTEM
16358
16359 #if GLYPH_DEBUG
16360
16361 void
16362 dump_glyph_string (s)
16363 struct glyph_string *s;
16364 {
16365 fprintf (stderr, "glyph string\n");
16366 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
16367 s->x, s->y, s->width, s->height);
16368 fprintf (stderr, " ybase = %d\n", s->ybase);
16369 fprintf (stderr, " hl = %d\n", s->hl);
16370 fprintf (stderr, " left overhang = %d, right = %d\n",
16371 s->left_overhang, s->right_overhang);
16372 fprintf (stderr, " nchars = %d\n", s->nchars);
16373 fprintf (stderr, " extends to end of line = %d\n",
16374 s->extends_to_end_of_line_p);
16375 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
16376 fprintf (stderr, " bg width = %d\n", s->background_width);
16377 }
16378
16379 #endif /* GLYPH_DEBUG */
16380
16381 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
16382 of XChar2b structures for S; it can't be allocated in
16383 init_glyph_string because it must be allocated via `alloca'. W
16384 is the window on which S is drawn. ROW and AREA are the glyph row
16385 and area within the row from which S is constructed. START is the
16386 index of the first glyph structure covered by S. HL is a
16387 face-override for drawing S. */
16388
16389 #ifdef HAVE_NTGUI
16390 #define OPTIONAL_HDC(hdc) hdc,
16391 #define DECLARE_HDC(hdc) HDC hdc;
16392 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
16393 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
16394 #endif
16395
16396 #ifndef OPTIONAL_HDC
16397 #define OPTIONAL_HDC(hdc)
16398 #define DECLARE_HDC(hdc)
16399 #define ALLOCATE_HDC(hdc, f)
16400 #define RELEASE_HDC(hdc, f)
16401 #endif
16402
16403 static void
16404 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
16405 struct glyph_string *s;
16406 DECLARE_HDC (hdc)
16407 XChar2b *char2b;
16408 struct window *w;
16409 struct glyph_row *row;
16410 enum glyph_row_area area;
16411 int start;
16412 enum draw_glyphs_face hl;
16413 {
16414 bzero (s, sizeof *s);
16415 s->w = w;
16416 s->f = XFRAME (w->frame);
16417 #ifdef HAVE_NTGUI
16418 s->hdc = hdc;
16419 #endif
16420 s->display = FRAME_X_DISPLAY (s->f);
16421 s->window = FRAME_X_WINDOW (s->f);
16422 s->char2b = char2b;
16423 s->hl = hl;
16424 s->row = row;
16425 s->area = area;
16426 s->first_glyph = row->glyphs[area] + start;
16427 s->height = row->height;
16428 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
16429
16430 /* Display the internal border below the tool-bar window. */
16431 if (s->w == XWINDOW (s->f->tool_bar_window))
16432 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
16433
16434 s->ybase = s->y + row->ascent;
16435 }
16436
16437
16438 /* Append the list of glyph strings with head H and tail T to the list
16439 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
16440
16441 static INLINE void
16442 append_glyph_string_lists (head, tail, h, t)
16443 struct glyph_string **head, **tail;
16444 struct glyph_string *h, *t;
16445 {
16446 if (h)
16447 {
16448 if (*head)
16449 (*tail)->next = h;
16450 else
16451 *head = h;
16452 h->prev = *tail;
16453 *tail = t;
16454 }
16455 }
16456
16457
16458 /* Prepend the list of glyph strings with head H and tail T to the
16459 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
16460 result. */
16461
16462 static INLINE void
16463 prepend_glyph_string_lists (head, tail, h, t)
16464 struct glyph_string **head, **tail;
16465 struct glyph_string *h, *t;
16466 {
16467 if (h)
16468 {
16469 if (*head)
16470 (*head)->prev = t;
16471 else
16472 *tail = t;
16473 t->next = *head;
16474 *head = h;
16475 }
16476 }
16477
16478
16479 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
16480 Set *HEAD and *TAIL to the resulting list. */
16481
16482 static INLINE void
16483 append_glyph_string (head, tail, s)
16484 struct glyph_string **head, **tail;
16485 struct glyph_string *s;
16486 {
16487 s->next = s->prev = NULL;
16488 append_glyph_string_lists (head, tail, s, s);
16489 }
16490
16491
16492 /* Get face and two-byte form of character glyph GLYPH on frame F.
16493 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
16494 a pointer to a realized face that is ready for display. */
16495
16496 static INLINE struct face *
16497 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
16498 struct frame *f;
16499 struct glyph *glyph;
16500 XChar2b *char2b;
16501 int *two_byte_p;
16502 {
16503 struct face *face;
16504
16505 xassert (glyph->type == CHAR_GLYPH);
16506 face = FACE_FROM_ID (f, glyph->face_id);
16507
16508 if (two_byte_p)
16509 *two_byte_p = 0;
16510
16511 if (!glyph->multibyte_p)
16512 {
16513 /* Unibyte case. We don't have to encode, but we have to make
16514 sure to use a face suitable for unibyte. */
16515 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
16516 }
16517 else if (glyph->u.ch < 128
16518 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
16519 {
16520 /* Case of ASCII in a face known to fit ASCII. */
16521 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
16522 }
16523 else
16524 {
16525 int c1, c2, charset;
16526
16527 /* Split characters into bytes. If c2 is -1 afterwards, C is
16528 really a one-byte character so that byte1 is zero. */
16529 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
16530 if (c2 > 0)
16531 STORE_XCHAR2B (char2b, c1, c2);
16532 else
16533 STORE_XCHAR2B (char2b, 0, c1);
16534
16535 /* Maybe encode the character in *CHAR2B. */
16536 if (charset != CHARSET_ASCII)
16537 {
16538 struct font_info *font_info
16539 = FONT_INFO_FROM_ID (f, face->font_info_id);
16540 if (font_info)
16541 glyph->font_type
16542 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
16543 }
16544 }
16545
16546 /* Make sure X resources of the face are allocated. */
16547 xassert (face != NULL);
16548 PREPARE_FACE_FOR_DISPLAY (f, face);
16549 return face;
16550 }
16551
16552
16553 /* Fill glyph string S with composition components specified by S->cmp.
16554
16555 FACES is an array of faces for all components of this composition.
16556 S->gidx is the index of the first component for S.
16557 OVERLAPS_P non-zero means S should draw the foreground only, and
16558 use its physical height for clipping.
16559
16560 Value is the index of a component not in S. */
16561
16562 static int
16563 fill_composite_glyph_string (s, faces, overlaps_p)
16564 struct glyph_string *s;
16565 struct face **faces;
16566 int overlaps_p;
16567 {
16568 int i;
16569
16570 xassert (s);
16571
16572 s->for_overlaps_p = overlaps_p;
16573
16574 s->face = faces[s->gidx];
16575 s->font = s->face->font;
16576 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
16577
16578 /* For all glyphs of this composition, starting at the offset
16579 S->gidx, until we reach the end of the definition or encounter a
16580 glyph that requires the different face, add it to S. */
16581 ++s->nchars;
16582 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
16583 ++s->nchars;
16584
16585 /* All glyph strings for the same composition has the same width,
16586 i.e. the width set for the first component of the composition. */
16587
16588 s->width = s->first_glyph->pixel_width;
16589
16590 /* If the specified font could not be loaded, use the frame's
16591 default font, but record the fact that we couldn't load it in
16592 the glyph string so that we can draw rectangles for the
16593 characters of the glyph string. */
16594 if (s->font == NULL)
16595 {
16596 s->font_not_found_p = 1;
16597 s->font = FRAME_FONT (s->f);
16598 }
16599
16600 /* Adjust base line for subscript/superscript text. */
16601 s->ybase += s->first_glyph->voffset;
16602
16603 xassert (s->face && s->face->gc);
16604
16605 /* This glyph string must always be drawn with 16-bit functions. */
16606 s->two_byte_p = 1;
16607
16608 return s->gidx + s->nchars;
16609 }
16610
16611
16612 /* Fill glyph string S from a sequence of character glyphs.
16613
16614 FACE_ID is the face id of the string. START is the index of the
16615 first glyph to consider, END is the index of the last + 1.
16616 OVERLAPS_P non-zero means S should draw the foreground only, and
16617 use its physical height for clipping.
16618
16619 Value is the index of the first glyph not in S. */
16620
16621 static int
16622 fill_glyph_string (s, face_id, start, end, overlaps_p)
16623 struct glyph_string *s;
16624 int face_id;
16625 int start, end, overlaps_p;
16626 {
16627 struct glyph *glyph, *last;
16628 int voffset;
16629 int glyph_not_available_p;
16630
16631 xassert (s->f == XFRAME (s->w->frame));
16632 xassert (s->nchars == 0);
16633 xassert (start >= 0 && end > start);
16634
16635 s->for_overlaps_p = overlaps_p,
16636 glyph = s->row->glyphs[s->area] + start;
16637 last = s->row->glyphs[s->area] + end;
16638 voffset = glyph->voffset;
16639
16640 glyph_not_available_p = glyph->glyph_not_available_p;
16641
16642 while (glyph < last
16643 && glyph->type == CHAR_GLYPH
16644 && glyph->voffset == voffset
16645 /* Same face id implies same font, nowadays. */
16646 && glyph->face_id == face_id
16647 && glyph->glyph_not_available_p == glyph_not_available_p)
16648 {
16649 int two_byte_p;
16650
16651 s->face = get_glyph_face_and_encoding (s->f, glyph,
16652 s->char2b + s->nchars,
16653 &two_byte_p);
16654 s->two_byte_p = two_byte_p;
16655 ++s->nchars;
16656 xassert (s->nchars <= end - start);
16657 s->width += glyph->pixel_width;
16658 ++glyph;
16659 }
16660
16661 s->font = s->face->font;
16662 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
16663
16664 /* If the specified font could not be loaded, use the frame's font,
16665 but record the fact that we couldn't load it in
16666 S->font_not_found_p so that we can draw rectangles for the
16667 characters of the glyph string. */
16668 if (s->font == NULL || glyph_not_available_p)
16669 {
16670 s->font_not_found_p = 1;
16671 s->font = FRAME_FONT (s->f);
16672 }
16673
16674 /* Adjust base line for subscript/superscript text. */
16675 s->ybase += voffset;
16676
16677 xassert (s->face && s->face->gc);
16678 return glyph - s->row->glyphs[s->area];
16679 }
16680
16681
16682 /* Fill glyph string S from image glyph S->first_glyph. */
16683
16684 static void
16685 fill_image_glyph_string (s)
16686 struct glyph_string *s;
16687 {
16688 xassert (s->first_glyph->type == IMAGE_GLYPH);
16689 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
16690 xassert (s->img);
16691 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
16692 s->font = s->face->font;
16693 s->width = s->first_glyph->pixel_width;
16694
16695 /* Adjust base line for subscript/superscript text. */
16696 s->ybase += s->first_glyph->voffset;
16697 }
16698
16699
16700 /* Fill glyph string S from a sequence of stretch glyphs.
16701
16702 ROW is the glyph row in which the glyphs are found, AREA is the
16703 area within the row. START is the index of the first glyph to
16704 consider, END is the index of the last + 1.
16705
16706 Value is the index of the first glyph not in S. */
16707
16708 static int
16709 fill_stretch_glyph_string (s, row, area, start, end)
16710 struct glyph_string *s;
16711 struct glyph_row *row;
16712 enum glyph_row_area area;
16713 int start, end;
16714 {
16715 struct glyph *glyph, *last;
16716 int voffset, face_id;
16717
16718 xassert (s->first_glyph->type == STRETCH_GLYPH);
16719
16720 glyph = s->row->glyphs[s->area] + start;
16721 last = s->row->glyphs[s->area] + end;
16722 face_id = glyph->face_id;
16723 s->face = FACE_FROM_ID (s->f, face_id);
16724 s->font = s->face->font;
16725 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
16726 s->width = glyph->pixel_width;
16727 voffset = glyph->voffset;
16728
16729 for (++glyph;
16730 (glyph < last
16731 && glyph->type == STRETCH_GLYPH
16732 && glyph->voffset == voffset
16733 && glyph->face_id == face_id);
16734 ++glyph)
16735 s->width += glyph->pixel_width;
16736
16737 /* Adjust base line for subscript/superscript text. */
16738 s->ybase += voffset;
16739
16740 /* The case that face->gc == 0 is handled when drawing the glyph
16741 string by calling PREPARE_FACE_FOR_DISPLAY. */
16742 xassert (s->face);
16743 return glyph - s->row->glyphs[s->area];
16744 }
16745
16746
16747 /* EXPORT for RIF:
16748 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
16749 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
16750 assumed to be zero. */
16751
16752 void
16753 x_get_glyph_overhangs (glyph, f, left, right)
16754 struct glyph *glyph;
16755 struct frame *f;
16756 int *left, *right;
16757 {
16758 *left = *right = 0;
16759
16760 if (glyph->type == CHAR_GLYPH)
16761 {
16762 XFontStruct *font;
16763 struct face *face;
16764 struct font_info *font_info;
16765 XChar2b char2b;
16766 XCharStruct *pcm;
16767
16768 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
16769 font = face->font;
16770 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
16771 if (font /* ++KFS: Should this be font_info ? */
16772 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
16773 {
16774 if (pcm->rbearing > pcm->width)
16775 *right = pcm->rbearing - pcm->width;
16776 if (pcm->lbearing < 0)
16777 *left = -pcm->lbearing;
16778 }
16779 }
16780 }
16781
16782
16783 /* Return the index of the first glyph preceding glyph string S that
16784 is overwritten by S because of S's left overhang. Value is -1
16785 if no glyphs are overwritten. */
16786
16787 static int
16788 left_overwritten (s)
16789 struct glyph_string *s;
16790 {
16791 int k;
16792
16793 if (s->left_overhang)
16794 {
16795 int x = 0, i;
16796 struct glyph *glyphs = s->row->glyphs[s->area];
16797 int first = s->first_glyph - glyphs;
16798
16799 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
16800 x -= glyphs[i].pixel_width;
16801
16802 k = i + 1;
16803 }
16804 else
16805 k = -1;
16806
16807 return k;
16808 }
16809
16810
16811 /* Return the index of the first glyph preceding glyph string S that
16812 is overwriting S because of its right overhang. Value is -1 if no
16813 glyph in front of S overwrites S. */
16814
16815 static int
16816 left_overwriting (s)
16817 struct glyph_string *s;
16818 {
16819 int i, k, x;
16820 struct glyph *glyphs = s->row->glyphs[s->area];
16821 int first = s->first_glyph - glyphs;
16822
16823 k = -1;
16824 x = 0;
16825 for (i = first - 1; i >= 0; --i)
16826 {
16827 int left, right;
16828 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
16829 if (x + right > 0)
16830 k = i;
16831 x -= glyphs[i].pixel_width;
16832 }
16833
16834 return k;
16835 }
16836
16837
16838 /* Return the index of the last glyph following glyph string S that is
16839 not overwritten by S because of S's right overhang. Value is -1 if
16840 no such glyph is found. */
16841
16842 static int
16843 right_overwritten (s)
16844 struct glyph_string *s;
16845 {
16846 int k = -1;
16847
16848 if (s->right_overhang)
16849 {
16850 int x = 0, i;
16851 struct glyph *glyphs = s->row->glyphs[s->area];
16852 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
16853 int end = s->row->used[s->area];
16854
16855 for (i = first; i < end && s->right_overhang > x; ++i)
16856 x += glyphs[i].pixel_width;
16857
16858 k = i;
16859 }
16860
16861 return k;
16862 }
16863
16864
16865 /* Return the index of the last glyph following glyph string S that
16866 overwrites S because of its left overhang. Value is negative
16867 if no such glyph is found. */
16868
16869 static int
16870 right_overwriting (s)
16871 struct glyph_string *s;
16872 {
16873 int i, k, x;
16874 int end = s->row->used[s->area];
16875 struct glyph *glyphs = s->row->glyphs[s->area];
16876 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
16877
16878 k = -1;
16879 x = 0;
16880 for (i = first; i < end; ++i)
16881 {
16882 int left, right;
16883 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
16884 if (x - left < 0)
16885 k = i;
16886 x += glyphs[i].pixel_width;
16887 }
16888
16889 return k;
16890 }
16891
16892
16893 /* Get face and two-byte form of character C in face FACE_ID on frame
16894 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
16895 means we want to display multibyte text. DISPLAY_P non-zero means
16896 make sure that X resources for the face returned are allocated.
16897 Value is a pointer to a realized face that is ready for display if
16898 DISPLAY_P is non-zero. */
16899
16900 static INLINE struct face *
16901 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
16902 struct frame *f;
16903 int c, face_id;
16904 XChar2b *char2b;
16905 int multibyte_p, display_p;
16906 {
16907 struct face *face = FACE_FROM_ID (f, face_id);
16908
16909 if (!multibyte_p)
16910 {
16911 /* Unibyte case. We don't have to encode, but we have to make
16912 sure to use a face suitable for unibyte. */
16913 STORE_XCHAR2B (char2b, 0, c);
16914 face_id = FACE_FOR_CHAR (f, face, c);
16915 face = FACE_FROM_ID (f, face_id);
16916 }
16917 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
16918 {
16919 /* Case of ASCII in a face known to fit ASCII. */
16920 STORE_XCHAR2B (char2b, 0, c);
16921 }
16922 else
16923 {
16924 int c1, c2, charset;
16925
16926 /* Split characters into bytes. If c2 is -1 afterwards, C is
16927 really a one-byte character so that byte1 is zero. */
16928 SPLIT_CHAR (c, charset, c1, c2);
16929 if (c2 > 0)
16930 STORE_XCHAR2B (char2b, c1, c2);
16931 else
16932 STORE_XCHAR2B (char2b, 0, c1);
16933
16934 /* Maybe encode the character in *CHAR2B. */
16935 if (face->font != NULL)
16936 {
16937 struct font_info *font_info
16938 = FONT_INFO_FROM_ID (f, face->font_info_id);
16939 if (font_info)
16940 rif->encode_char (c, char2b, font_info, 0);
16941 }
16942 }
16943
16944 /* Make sure X resources of the face are allocated. */
16945 #ifdef HAVE_X_WINDOWS
16946 if (display_p)
16947 #endif
16948 {
16949 xassert (face != NULL);
16950 PREPARE_FACE_FOR_DISPLAY (f, face);
16951 }
16952
16953 return face;
16954 }
16955
16956
16957 /* Set background width of glyph string S. START is the index of the
16958 first glyph following S. LAST_X is the right-most x-position + 1
16959 in the drawing area. */
16960
16961 static INLINE void
16962 set_glyph_string_background_width (s, start, last_x)
16963 struct glyph_string *s;
16964 int start;
16965 int last_x;
16966 {
16967 /* If the face of this glyph string has to be drawn to the end of
16968 the drawing area, set S->extends_to_end_of_line_p. */
16969 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
16970
16971 if (start == s->row->used[s->area]
16972 && s->area == TEXT_AREA
16973 && ((s->hl == DRAW_NORMAL_TEXT
16974 && (s->row->fill_line_p
16975 || s->face->background != default_face->background
16976 || s->face->stipple != default_face->stipple
16977 || s->row->mouse_face_p))
16978 || s->hl == DRAW_MOUSE_FACE
16979 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
16980 && s->row->fill_line_p)))
16981 s->extends_to_end_of_line_p = 1;
16982
16983 /* If S extends its face to the end of the line, set its
16984 background_width to the distance to the right edge of the drawing
16985 area. */
16986 if (s->extends_to_end_of_line_p)
16987 s->background_width = last_x - s->x + 1;
16988 else
16989 s->background_width = s->width;
16990 }
16991
16992
16993 /* Compute overhangs and x-positions for glyph string S and its
16994 predecessors, or successors. X is the starting x-position for S.
16995 BACKWARD_P non-zero means process predecessors. */
16996
16997 static void
16998 compute_overhangs_and_x (s, x, backward_p)
16999 struct glyph_string *s;
17000 int x;
17001 int backward_p;
17002 {
17003 if (backward_p)
17004 {
17005 while (s)
17006 {
17007 if (rif->compute_glyph_string_overhangs)
17008 rif->compute_glyph_string_overhangs (s);
17009 x -= s->width;
17010 s->x = x;
17011 s = s->prev;
17012 }
17013 }
17014 else
17015 {
17016 while (s)
17017 {
17018 if (rif->compute_glyph_string_overhangs)
17019 rif->compute_glyph_string_overhangs (s);
17020 s->x = x;
17021 x += s->width;
17022 s = s->next;
17023 }
17024 }
17025 }
17026
17027
17028
17029 /* The following macros are only called from draw_glyphs below.
17030 They reference the following parameters of that function directly:
17031 `w', `row', `area', and `overlap_p'
17032 as well as the following local variables:
17033 `s', `f', and `hdc' (in W32) */
17034
17035 #ifdef HAVE_NTGUI
17036 /* On W32, silently add local `hdc' variable to argument list of
17037 init_glyph_string. */
17038 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17039 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
17040 #else
17041 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17042 init_glyph_string (s, char2b, w, row, area, start, hl)
17043 #endif
17044
17045 /* Add a glyph string for a stretch glyph to the list of strings
17046 between HEAD and TAIL. START is the index of the stretch glyph in
17047 row area AREA of glyph row ROW. END is the index of the last glyph
17048 in that glyph row area. X is the current output position assigned
17049 to the new glyph string constructed. HL overrides that face of the
17050 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17051 is the right-most x-position of the drawing area. */
17052
17053 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
17054 and below -- keep them on one line. */
17055 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17056 do \
17057 { \
17058 s = (struct glyph_string *) alloca (sizeof *s); \
17059 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17060 START = fill_stretch_glyph_string (s, row, area, START, END); \
17061 append_glyph_string (&HEAD, &TAIL, s); \
17062 s->x = (X); \
17063 } \
17064 while (0)
17065
17066
17067 /* Add a glyph string for an image glyph to the list of strings
17068 between HEAD and TAIL. START is the index of the image glyph in
17069 row area AREA of glyph row ROW. END is the index of the last glyph
17070 in that glyph row area. X is the current output position assigned
17071 to the new glyph string constructed. HL overrides that face of the
17072 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17073 is the right-most x-position of the drawing area. */
17074
17075 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17076 do \
17077 { \
17078 s = (struct glyph_string *) alloca (sizeof *s); \
17079 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17080 fill_image_glyph_string (s); \
17081 append_glyph_string (&HEAD, &TAIL, s); \
17082 ++START; \
17083 s->x = (X); \
17084 } \
17085 while (0)
17086
17087
17088 /* Add a glyph string for a sequence of character glyphs to the list
17089 of strings between HEAD and TAIL. START is the index of the first
17090 glyph in row area AREA of glyph row ROW that is part of the new
17091 glyph string. END is the index of the last glyph in that glyph row
17092 area. X is the current output position assigned to the new glyph
17093 string constructed. HL overrides that face of the glyph; e.g. it
17094 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
17095 right-most x-position of the drawing area. */
17096
17097 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17098 do \
17099 { \
17100 int c, face_id; \
17101 XChar2b *char2b; \
17102 \
17103 c = (row)->glyphs[area][START].u.ch; \
17104 face_id = (row)->glyphs[area][START].face_id; \
17105 \
17106 s = (struct glyph_string *) alloca (sizeof *s); \
17107 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
17108 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
17109 append_glyph_string (&HEAD, &TAIL, s); \
17110 s->x = (X); \
17111 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
17112 } \
17113 while (0)
17114
17115
17116 /* Add a glyph string for a composite sequence to the list of strings
17117 between HEAD and TAIL. START is the index of the first glyph in
17118 row area AREA of glyph row ROW that is part of the new glyph
17119 string. END is the index of the last glyph in that glyph row area.
17120 X is the current output position assigned to the new glyph string
17121 constructed. HL overrides that face of the glyph; e.g. it is
17122 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
17123 x-position of the drawing area. */
17124
17125 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17126 do { \
17127 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
17128 int face_id = (row)->glyphs[area][START].face_id; \
17129 struct face *base_face = FACE_FROM_ID (f, face_id); \
17130 struct composition *cmp = composition_table[cmp_id]; \
17131 int glyph_len = cmp->glyph_len; \
17132 XChar2b *char2b; \
17133 struct face **faces; \
17134 struct glyph_string *first_s = NULL; \
17135 int n; \
17136 \
17137 base_face = base_face->ascii_face; \
17138 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
17139 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
17140 /* At first, fill in `char2b' and `faces'. */ \
17141 for (n = 0; n < glyph_len; n++) \
17142 { \
17143 int c = COMPOSITION_GLYPH (cmp, n); \
17144 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
17145 faces[n] = FACE_FROM_ID (f, this_face_id); \
17146 get_char_face_and_encoding (f, c, this_face_id, \
17147 char2b + n, 1, 1); \
17148 } \
17149 \
17150 /* Make glyph_strings for each glyph sequence that is drawable by \
17151 the same face, and append them to HEAD/TAIL. */ \
17152 for (n = 0; n < cmp->glyph_len;) \
17153 { \
17154 s = (struct glyph_string *) alloca (sizeof *s); \
17155 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
17156 append_glyph_string (&(HEAD), &(TAIL), s); \
17157 s->cmp = cmp; \
17158 s->gidx = n; \
17159 s->x = (X); \
17160 \
17161 if (n == 0) \
17162 first_s = s; \
17163 \
17164 n = fill_composite_glyph_string (s, faces, overlaps_p); \
17165 } \
17166 \
17167 ++START; \
17168 s = first_s; \
17169 } while (0)
17170
17171
17172 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
17173 of AREA of glyph row ROW on window W between indices START and END.
17174 HL overrides the face for drawing glyph strings, e.g. it is
17175 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
17176 x-positions of the drawing area.
17177
17178 This is an ugly monster macro construct because we must use alloca
17179 to allocate glyph strings (because draw_glyphs can be called
17180 asynchronously). */
17181
17182 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17183 do \
17184 { \
17185 HEAD = TAIL = NULL; \
17186 while (START < END) \
17187 { \
17188 struct glyph *first_glyph = (row)->glyphs[area] + START; \
17189 switch (first_glyph->type) \
17190 { \
17191 case CHAR_GLYPH: \
17192 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
17193 HL, X, LAST_X); \
17194 break; \
17195 \
17196 case COMPOSITE_GLYPH: \
17197 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
17198 HL, X, LAST_X); \
17199 break; \
17200 \
17201 case STRETCH_GLYPH: \
17202 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
17203 HL, X, LAST_X); \
17204 break; \
17205 \
17206 case IMAGE_GLYPH: \
17207 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
17208 HL, X, LAST_X); \
17209 break; \
17210 \
17211 default: \
17212 abort (); \
17213 } \
17214 \
17215 set_glyph_string_background_width (s, START, LAST_X); \
17216 (X) += s->width; \
17217 } \
17218 } \
17219 while (0)
17220
17221
17222 /* Draw glyphs between START and END in AREA of ROW on window W,
17223 starting at x-position X. X is relative to AREA in W. HL is a
17224 face-override with the following meaning:
17225
17226 DRAW_NORMAL_TEXT draw normally
17227 DRAW_CURSOR draw in cursor face
17228 DRAW_MOUSE_FACE draw in mouse face.
17229 DRAW_INVERSE_VIDEO draw in mode line face
17230 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
17231 DRAW_IMAGE_RAISED draw an image with a raised relief around it
17232
17233 If OVERLAPS_P is non-zero, draw only the foreground of characters
17234 and clip to the physical height of ROW.
17235
17236 Value is the x-position reached, relative to AREA of W. */
17237
17238 static int
17239 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
17240 struct window *w;
17241 int x;
17242 struct glyph_row *row;
17243 enum glyph_row_area area;
17244 int start, end;
17245 enum draw_glyphs_face hl;
17246 int overlaps_p;
17247 {
17248 struct glyph_string *head, *tail;
17249 struct glyph_string *s;
17250 int last_x, area_width;
17251 int x_reached;
17252 int i, j;
17253 struct frame *f = XFRAME (WINDOW_FRAME (w));
17254 DECLARE_HDC (hdc);
17255
17256 ALLOCATE_HDC (hdc, f);
17257
17258 /* Let's rather be paranoid than getting a SEGV. */
17259 end = min (end, row->used[area]);
17260 start = max (0, start);
17261 start = min (end, start);
17262
17263 /* Translate X to frame coordinates. Set last_x to the right
17264 end of the drawing area. */
17265 if (row->full_width_p)
17266 {
17267 /* X is relative to the left edge of W, without scroll bars
17268 or fringes. */
17269 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
17270
17271 x += window_left_x;
17272 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
17273 last_x = window_left_x + area_width;
17274
17275 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
17276 {
17277 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
17278 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
17279 last_x += width;
17280 else
17281 x -= width;
17282 }
17283
17284 x += FRAME_INTERNAL_BORDER_WIDTH (f);
17285 /* ++KFS: W32 and MAC versions had -= in next line (bug??) */
17286 last_x += FRAME_INTERNAL_BORDER_WIDTH (f);
17287 }
17288 else
17289 {
17290 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
17291 area_width = window_box_width (w, area);
17292 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
17293 }
17294
17295 /* Build a doubly-linked list of glyph_string structures between
17296 head and tail from what we have to draw. Note that the macro
17297 BUILD_GLYPH_STRINGS will modify its start parameter. That's
17298 the reason we use a separate variable `i'. */
17299 i = start;
17300 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
17301 if (tail)
17302 x_reached = tail->x + tail->background_width;
17303 else
17304 x_reached = x;
17305
17306 /* If there are any glyphs with lbearing < 0 or rbearing > width in
17307 the row, redraw some glyphs in front or following the glyph
17308 strings built above. */
17309 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
17310 {
17311 int dummy_x = 0;
17312 struct glyph_string *h, *t;
17313
17314 /* Compute overhangs for all glyph strings. */
17315 if (rif->compute_glyph_string_overhangs)
17316 for (s = head; s; s = s->next)
17317 rif->compute_glyph_string_overhangs (s);
17318
17319 /* Prepend glyph strings for glyphs in front of the first glyph
17320 string that are overwritten because of the first glyph
17321 string's left overhang. The background of all strings
17322 prepended must be drawn because the first glyph string
17323 draws over it. */
17324 i = left_overwritten (head);
17325 if (i >= 0)
17326 {
17327 j = i;
17328 BUILD_GLYPH_STRINGS (j, start, h, t,
17329 DRAW_NORMAL_TEXT, dummy_x, last_x);
17330 start = i;
17331 compute_overhangs_and_x (t, head->x, 1);
17332 prepend_glyph_string_lists (&head, &tail, h, t);
17333 }
17334
17335 /* Prepend glyph strings for glyphs in front of the first glyph
17336 string that overwrite that glyph string because of their
17337 right overhang. For these strings, only the foreground must
17338 be drawn, because it draws over the glyph string at `head'.
17339 The background must not be drawn because this would overwrite
17340 right overhangs of preceding glyphs for which no glyph
17341 strings exist. */
17342 i = left_overwriting (head);
17343 if (i >= 0)
17344 {
17345 BUILD_GLYPH_STRINGS (i, start, h, t,
17346 DRAW_NORMAL_TEXT, dummy_x, last_x);
17347 for (s = h; s; s = s->next)
17348 s->background_filled_p = 1;
17349 compute_overhangs_and_x (t, head->x, 1);
17350 prepend_glyph_string_lists (&head, &tail, h, t);
17351 }
17352
17353 /* Append glyphs strings for glyphs following the last glyph
17354 string tail that are overwritten by tail. The background of
17355 these strings has to be drawn because tail's foreground draws
17356 over it. */
17357 i = right_overwritten (tail);
17358 if (i >= 0)
17359 {
17360 BUILD_GLYPH_STRINGS (end, i, h, t,
17361 DRAW_NORMAL_TEXT, x, last_x);
17362 compute_overhangs_and_x (h, tail->x + tail->width, 0);
17363 append_glyph_string_lists (&head, &tail, h, t);
17364 }
17365
17366 /* Append glyph strings for glyphs following the last glyph
17367 string tail that overwrite tail. The foreground of such
17368 glyphs has to be drawn because it writes into the background
17369 of tail. The background must not be drawn because it could
17370 paint over the foreground of following glyphs. */
17371 i = right_overwriting (tail);
17372 if (i >= 0)
17373 {
17374 BUILD_GLYPH_STRINGS (end, i, h, t,
17375 DRAW_NORMAL_TEXT, x, last_x);
17376 for (s = h; s; s = s->next)
17377 s->background_filled_p = 1;
17378 compute_overhangs_and_x (h, tail->x + tail->width, 0);
17379 append_glyph_string_lists (&head, &tail, h, t);
17380 }
17381 }
17382
17383 /* Draw all strings. */
17384 for (s = head; s; s = s->next)
17385 rif->draw_glyph_string (s);
17386
17387 if (area == TEXT_AREA
17388 && !row->full_width_p
17389 /* When drawing overlapping rows, only the glyph strings'
17390 foreground is drawn, which doesn't erase a cursor
17391 completely. */
17392 && !overlaps_p)
17393 {
17394 int x0 = head ? head->x : x;
17395 int x1 = tail ? tail->x + tail->background_width : x;
17396
17397 x0 = FRAME_TO_WINDOW_PIXEL_X (w, x0);
17398 x1 = FRAME_TO_WINDOW_PIXEL_X (w, x1);
17399
17400 /* ++KFS: W32 and MAC versions had following test here:
17401 if (!row->full_width_p && XFASTINT (w->left_margin_width) != 0)
17402 */
17403
17404 if (XFASTINT (w->left_margin_width) != 0)
17405 {
17406 int left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
17407 x0 -= left_area_width;
17408 x1 -= left_area_width;
17409 }
17410
17411 notice_overwritten_cursor (w, area, x0, x1,
17412 row->y, MATRIX_ROW_BOTTOM_Y (row));
17413 }
17414
17415 /* Value is the x-position up to which drawn, relative to AREA of W.
17416 This doesn't include parts drawn because of overhangs. */
17417 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
17418 if (!row->full_width_p)
17419 {
17420 /* ++KFS: W32 and MAC versions only had this test here:
17421 if (area > LEFT_MARGIN_AREA)
17422 */
17423
17424 if (area > LEFT_MARGIN_AREA && XFASTINT (w->left_margin_width) != 0)
17425 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
17426 if (area > TEXT_AREA)
17427 x_reached -= window_box_width (w, TEXT_AREA);
17428 }
17429
17430 RELEASE_HDC (hdc, f);
17431
17432 return x_reached;
17433 }
17434
17435
17436 /* Store one glyph for IT->char_to_display in IT->glyph_row.
17437 Called from x_produce_glyphs when IT->glyph_row is non-null. */
17438
17439 static INLINE void
17440 append_glyph (it)
17441 struct it *it;
17442 {
17443 struct glyph *glyph;
17444 enum glyph_row_area area = it->area;
17445
17446 xassert (it->glyph_row);
17447 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
17448
17449 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17450 if (glyph < it->glyph_row->glyphs[area + 1])
17451 {
17452 glyph->charpos = CHARPOS (it->position);
17453 glyph->object = it->object;
17454 glyph->pixel_width = it->pixel_width;
17455 glyph->voffset = it->voffset;
17456 glyph->type = CHAR_GLYPH;
17457 glyph->multibyte_p = it->multibyte_p;
17458 glyph->left_box_line_p = it->start_of_box_run_p;
17459 glyph->right_box_line_p = it->end_of_box_run_p;
17460 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
17461 || it->phys_descent > it->descent);
17462 glyph->padding_p = 0;
17463 glyph->glyph_not_available_p = it->glyph_not_available_p;
17464 glyph->face_id = it->face_id;
17465 glyph->u.ch = it->char_to_display;
17466 glyph->font_type = FONT_TYPE_UNKNOWN;
17467 ++it->glyph_row->used[area];
17468 }
17469 }
17470
17471 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
17472 Called from x_produce_glyphs when IT->glyph_row is non-null. */
17473
17474 static INLINE void
17475 append_composite_glyph (it)
17476 struct it *it;
17477 {
17478 struct glyph *glyph;
17479 enum glyph_row_area area = it->area;
17480
17481 xassert (it->glyph_row);
17482
17483 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17484 if (glyph < it->glyph_row->glyphs[area + 1])
17485 {
17486 glyph->charpos = CHARPOS (it->position);
17487 glyph->object = it->object;
17488 glyph->pixel_width = it->pixel_width;
17489 glyph->voffset = it->voffset;
17490 glyph->type = COMPOSITE_GLYPH;
17491 glyph->multibyte_p = it->multibyte_p;
17492 glyph->left_box_line_p = it->start_of_box_run_p;
17493 glyph->right_box_line_p = it->end_of_box_run_p;
17494 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
17495 || it->phys_descent > it->descent);
17496 glyph->padding_p = 0;
17497 glyph->glyph_not_available_p = 0;
17498 glyph->face_id = it->face_id;
17499 glyph->u.cmp_id = it->cmp_id;
17500 glyph->font_type = FONT_TYPE_UNKNOWN;
17501 ++it->glyph_row->used[area];
17502 }
17503 }
17504
17505
17506 /* Change IT->ascent and IT->height according to the setting of
17507 IT->voffset. */
17508
17509 static INLINE void
17510 take_vertical_position_into_account (it)
17511 struct it *it;
17512 {
17513 if (it->voffset)
17514 {
17515 if (it->voffset < 0)
17516 /* Increase the ascent so that we can display the text higher
17517 in the line. */
17518 it->ascent += abs (it->voffset);
17519 else
17520 /* Increase the descent so that we can display the text lower
17521 in the line. */
17522 it->descent += it->voffset;
17523 }
17524 }
17525
17526
17527 /* Produce glyphs/get display metrics for the image IT is loaded with.
17528 See the description of struct display_iterator in dispextern.h for
17529 an overview of struct display_iterator. */
17530
17531 static void
17532 produce_image_glyph (it)
17533 struct it *it;
17534 {
17535 struct image *img;
17536 struct face *face;
17537
17538 xassert (it->what == IT_IMAGE);
17539
17540 face = FACE_FROM_ID (it->f, it->face_id);
17541 img = IMAGE_FROM_ID (it->f, it->image_id);
17542 xassert (img);
17543
17544 /* Make sure X resources of the face and image are loaded. */
17545 PREPARE_FACE_FOR_DISPLAY (it->f, face);
17546 prepare_image_for_display (it->f, img);
17547
17548 it->ascent = it->phys_ascent = image_ascent (img, face);
17549 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
17550 it->pixel_width = img->width + 2 * img->hmargin;
17551
17552 it->nglyphs = 1;
17553
17554 if (face->box != FACE_NO_BOX)
17555 {
17556 if (face->box_line_width > 0)
17557 {
17558 it->ascent += face->box_line_width;
17559 it->descent += face->box_line_width;
17560 }
17561
17562 if (it->start_of_box_run_p)
17563 it->pixel_width += abs (face->box_line_width);
17564 if (it->end_of_box_run_p)
17565 it->pixel_width += abs (face->box_line_width);
17566 }
17567
17568 take_vertical_position_into_account (it);
17569
17570 if (it->glyph_row)
17571 {
17572 struct glyph *glyph;
17573 enum glyph_row_area area = it->area;
17574
17575 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17576 if (glyph < it->glyph_row->glyphs[area + 1])
17577 {
17578 glyph->charpos = CHARPOS (it->position);
17579 glyph->object = it->object;
17580 glyph->pixel_width = it->pixel_width;
17581 glyph->voffset = it->voffset;
17582 glyph->type = IMAGE_GLYPH;
17583 glyph->multibyte_p = it->multibyte_p;
17584 glyph->left_box_line_p = it->start_of_box_run_p;
17585 glyph->right_box_line_p = it->end_of_box_run_p;
17586 glyph->overlaps_vertically_p = 0;
17587 glyph->padding_p = 0;
17588 glyph->glyph_not_available_p = 0;
17589 glyph->face_id = it->face_id;
17590 glyph->u.img_id = img->id;
17591 glyph->font_type = FONT_TYPE_UNKNOWN;
17592 ++it->glyph_row->used[area];
17593 }
17594 }
17595 }
17596
17597
17598 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
17599 of the glyph, WIDTH and HEIGHT are the width and height of the
17600 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
17601 ascent of the glyph (0 <= ASCENT <= 1). */
17602
17603 static void
17604 append_stretch_glyph (it, object, width, height, ascent)
17605 struct it *it;
17606 Lisp_Object object;
17607 int width, height;
17608 double ascent;
17609 {
17610 struct glyph *glyph;
17611 enum glyph_row_area area = it->area;
17612
17613 xassert (ascent >= 0 && ascent <= 1);
17614
17615 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17616 if (glyph < it->glyph_row->glyphs[area + 1])
17617 {
17618 glyph->charpos = CHARPOS (it->position);
17619 glyph->object = object;
17620 glyph->pixel_width = width;
17621 glyph->voffset = it->voffset;
17622 glyph->type = STRETCH_GLYPH;
17623 glyph->multibyte_p = it->multibyte_p;
17624 glyph->left_box_line_p = it->start_of_box_run_p;
17625 glyph->right_box_line_p = it->end_of_box_run_p;
17626 glyph->overlaps_vertically_p = 0;
17627 glyph->padding_p = 0;
17628 glyph->glyph_not_available_p = 0;
17629 glyph->face_id = it->face_id;
17630 glyph->u.stretch.ascent = height * ascent;
17631 glyph->u.stretch.height = height;
17632 glyph->font_type = FONT_TYPE_UNKNOWN;
17633 ++it->glyph_row->used[area];
17634 }
17635 }
17636
17637
17638 /* Produce a stretch glyph for iterator IT. IT->object is the value
17639 of the glyph property displayed. The value must be a list
17640 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
17641 being recognized:
17642
17643 1. `:width WIDTH' specifies that the space should be WIDTH *
17644 canonical char width wide. WIDTH may be an integer or floating
17645 point number.
17646
17647 2. `:relative-width FACTOR' specifies that the width of the stretch
17648 should be computed from the width of the first character having the
17649 `glyph' property, and should be FACTOR times that width.
17650
17651 3. `:align-to HPOS' specifies that the space should be wide enough
17652 to reach HPOS, a value in canonical character units.
17653
17654 Exactly one of the above pairs must be present.
17655
17656 4. `:height HEIGHT' specifies that the height of the stretch produced
17657 should be HEIGHT, measured in canonical character units.
17658
17659 5. `:relative-height FACTOR' specifies that the height of the
17660 stretch should be FACTOR times the height of the characters having
17661 the glyph property.
17662
17663 Either none or exactly one of 4 or 5 must be present.
17664
17665 6. `:ascent ASCENT' specifies that ASCENT percent of the height
17666 of the stretch should be used for the ascent of the stretch.
17667 ASCENT must be in the range 0 <= ASCENT <= 100. */
17668
17669 #define NUMVAL(X) \
17670 ((INTEGERP (X) || FLOATP (X)) \
17671 ? XFLOATINT (X) \
17672 : - 1)
17673
17674
17675 static void
17676 produce_stretch_glyph (it)
17677 struct it *it;
17678 {
17679 /* (space :width WIDTH :height HEIGHT. */
17680 Lisp_Object prop, plist;
17681 int width = 0, height = 0;
17682 double ascent = 0;
17683 struct face *face = FACE_FROM_ID (it->f, it->face_id);
17684 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
17685
17686 PREPARE_FACE_FOR_DISPLAY (it->f, face);
17687
17688 /* List should start with `space'. */
17689 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
17690 plist = XCDR (it->object);
17691
17692 /* Compute the width of the stretch. */
17693 if (prop = Fplist_get (plist, QCwidth),
17694 NUMVAL (prop) > 0)
17695 /* Absolute width `:width WIDTH' specified and valid. */
17696 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
17697 else if (prop = Fplist_get (plist, QCrelative_width),
17698 NUMVAL (prop) > 0)
17699 {
17700 /* Relative width `:relative-width FACTOR' specified and valid.
17701 Compute the width of the characters having the `glyph'
17702 property. */
17703 struct it it2;
17704 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
17705
17706 it2 = *it;
17707 if (it->multibyte_p)
17708 {
17709 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
17710 - IT_BYTEPOS (*it));
17711 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
17712 }
17713 else
17714 it2.c = *p, it2.len = 1;
17715
17716 it2.glyph_row = NULL;
17717 it2.what = IT_CHARACTER;
17718 x_produce_glyphs (&it2);
17719 width = NUMVAL (prop) * it2.pixel_width;
17720 }
17721 else if (prop = Fplist_get (plist, QCalign_to),
17722 NUMVAL (prop) > 0)
17723 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
17724 else
17725 /* Nothing specified -> width defaults to canonical char width. */
17726 width = CANON_X_UNIT (it->f);
17727
17728 /* Compute height. */
17729 if (prop = Fplist_get (plist, QCheight),
17730 NUMVAL (prop) > 0)
17731 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
17732 else if (prop = Fplist_get (plist, QCrelative_height),
17733 NUMVAL (prop) > 0)
17734 height = FONT_HEIGHT (font) * NUMVAL (prop);
17735 else
17736 height = FONT_HEIGHT (font);
17737
17738 /* Compute percentage of height used for ascent. If
17739 `:ascent ASCENT' is present and valid, use that. Otherwise,
17740 derive the ascent from the font in use. */
17741 if (prop = Fplist_get (plist, QCascent),
17742 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
17743 ascent = NUMVAL (prop) / 100.0;
17744 else
17745 ascent = (double) FONT_BASE (font) / FONT_HEIGHT (font);
17746
17747 if (width <= 0)
17748 width = 1;
17749 if (height <= 0)
17750 height = 1;
17751
17752 if (it->glyph_row)
17753 {
17754 Lisp_Object object = it->stack[it->sp - 1].string;
17755 if (!STRINGP (object))
17756 object = it->w->buffer;
17757 append_stretch_glyph (it, object, width, height, ascent);
17758 }
17759
17760 it->pixel_width = width;
17761 it->ascent = it->phys_ascent = height * ascent;
17762 it->descent = it->phys_descent = height - it->ascent;
17763 it->nglyphs = 1;
17764
17765 if (face->box != FACE_NO_BOX)
17766 {
17767 if (face->box_line_width > 0)
17768 {
17769 it->ascent += face->box_line_width;
17770 it->descent += face->box_line_width;
17771 }
17772
17773 if (it->start_of_box_run_p)
17774 it->pixel_width += abs (face->box_line_width);
17775 if (it->end_of_box_run_p)
17776 it->pixel_width += abs (face->box_line_width);
17777 }
17778
17779 take_vertical_position_into_account (it);
17780 }
17781
17782 /* RIF:
17783 Produce glyphs/get display metrics for the display element IT is
17784 loaded with. See the description of struct display_iterator in
17785 dispextern.h for an overview of struct display_iterator. */
17786
17787 void
17788 x_produce_glyphs (it)
17789 struct it *it;
17790 {
17791 it->glyph_not_available_p = 0;
17792
17793 if (it->what == IT_CHARACTER)
17794 {
17795 XChar2b char2b;
17796 XFontStruct *font;
17797 struct face *face = FACE_FROM_ID (it->f, it->face_id);
17798 XCharStruct *pcm;
17799 int font_not_found_p;
17800 struct font_info *font_info;
17801 int boff; /* baseline offset */
17802 /* We may change it->multibyte_p upon unibyte<->multibyte
17803 conversion. So, save the current value now and restore it
17804 later.
17805
17806 Note: It seems that we don't have to record multibyte_p in
17807 struct glyph because the character code itself tells if or
17808 not the character is multibyte. Thus, in the future, we must
17809 consider eliminating the field `multibyte_p' in the struct
17810 glyph. */
17811 int saved_multibyte_p = it->multibyte_p;
17812
17813 /* Maybe translate single-byte characters to multibyte, or the
17814 other way. */
17815 it->char_to_display = it->c;
17816 if (!ASCII_BYTE_P (it->c))
17817 {
17818 if (unibyte_display_via_language_environment
17819 && SINGLE_BYTE_CHAR_P (it->c)
17820 && (it->c >= 0240
17821 || !NILP (Vnonascii_translation_table)))
17822 {
17823 it->char_to_display = unibyte_char_to_multibyte (it->c);
17824 it->multibyte_p = 1;
17825 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
17826 face = FACE_FROM_ID (it->f, it->face_id);
17827 }
17828 else if (!SINGLE_BYTE_CHAR_P (it->c)
17829 && !it->multibyte_p)
17830 {
17831 it->multibyte_p = 1;
17832 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
17833 face = FACE_FROM_ID (it->f, it->face_id);
17834 }
17835 }
17836
17837 /* Get font to use. Encode IT->char_to_display. */
17838 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
17839 &char2b, it->multibyte_p, 0);
17840 font = face->font;
17841
17842 /* When no suitable font found, use the default font. */
17843 font_not_found_p = font == NULL;
17844 if (font_not_found_p)
17845 {
17846 font = FRAME_FONT (it->f);
17847 boff = FRAME_BASELINE_OFFSET (it->f);
17848 font_info = NULL;
17849 }
17850 else
17851 {
17852 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
17853 boff = font_info->baseline_offset;
17854 if (font_info->vertical_centering)
17855 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
17856 }
17857
17858 if (it->char_to_display >= ' '
17859 && (!it->multibyte_p || it->char_to_display < 128))
17860 {
17861 /* Either unibyte or ASCII. */
17862 int stretched_p;
17863
17864 it->nglyphs = 1;
17865
17866 pcm = rif->per_char_metric (font, &char2b,
17867 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
17868 it->ascent = FONT_BASE (font) + boff;
17869 it->descent = FONT_DESCENT (font) - boff;
17870
17871 if (pcm)
17872 {
17873 it->phys_ascent = pcm->ascent + boff;
17874 it->phys_descent = pcm->descent - boff;
17875 it->pixel_width = pcm->width;
17876 }
17877 else
17878 {
17879 it->glyph_not_available_p = 1;
17880 it->phys_ascent = FONT_BASE (font) + boff;
17881 it->phys_descent = FONT_DESCENT (font) - boff;
17882 it->pixel_width = FONT_WIDTH (font);
17883 }
17884
17885 /* If this is a space inside a region of text with
17886 `space-width' property, change its width. */
17887 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
17888 if (stretched_p)
17889 it->pixel_width *= XFLOATINT (it->space_width);
17890
17891 /* If face has a box, add the box thickness to the character
17892 height. If character has a box line to the left and/or
17893 right, add the box line width to the character's width. */
17894 if (face->box != FACE_NO_BOX)
17895 {
17896 int thick = face->box_line_width;
17897
17898 if (thick > 0)
17899 {
17900 it->ascent += thick;
17901 it->descent += thick;
17902 }
17903 else
17904 thick = -thick;
17905
17906 if (it->start_of_box_run_p)
17907 it->pixel_width += thick;
17908 if (it->end_of_box_run_p)
17909 it->pixel_width += thick;
17910 }
17911
17912 /* If face has an overline, add the height of the overline
17913 (1 pixel) and a 1 pixel margin to the character height. */
17914 if (face->overline_p)
17915 it->ascent += 2;
17916
17917 take_vertical_position_into_account (it);
17918
17919 /* If we have to actually produce glyphs, do it. */
17920 if (it->glyph_row)
17921 {
17922 if (stretched_p)
17923 {
17924 /* Translate a space with a `space-width' property
17925 into a stretch glyph. */
17926 double ascent = (double) FONT_BASE (font)
17927 / FONT_HEIGHT (font);
17928 append_stretch_glyph (it, it->object, it->pixel_width,
17929 it->ascent + it->descent, ascent);
17930 }
17931 else
17932 append_glyph (it);
17933
17934 /* If characters with lbearing or rbearing are displayed
17935 in this line, record that fact in a flag of the
17936 glyph row. This is used to optimize X output code. */
17937 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
17938 it->glyph_row->contains_overlapping_glyphs_p = 1;
17939 }
17940 }
17941 else if (it->char_to_display == '\n')
17942 {
17943 /* A newline has no width but we need the height of the line. */
17944 it->pixel_width = 0;
17945 it->nglyphs = 0;
17946 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
17947 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
17948
17949 if (face->box != FACE_NO_BOX
17950 && face->box_line_width > 0)
17951 {
17952 it->ascent += face->box_line_width;
17953 it->descent += face->box_line_width;
17954 }
17955 }
17956 else if (it->char_to_display == '\t')
17957 {
17958 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
17959 int x = it->current_x + it->continuation_lines_width;
17960 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
17961
17962 /* If the distance from the current position to the next tab
17963 stop is less than a canonical character width, use the
17964 tab stop after that. */
17965 if (next_tab_x - x < CANON_X_UNIT (it->f))
17966 next_tab_x += tab_width;
17967
17968 it->pixel_width = next_tab_x - x;
17969 it->nglyphs = 1;
17970 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
17971 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
17972
17973 if (it->glyph_row)
17974 {
17975 double ascent = (double) it->ascent / (it->ascent + it->descent);
17976 append_stretch_glyph (it, it->object, it->pixel_width,
17977 it->ascent + it->descent, ascent);
17978 }
17979 }
17980 else
17981 {
17982 /* A multi-byte character. Assume that the display width of the
17983 character is the width of the character multiplied by the
17984 width of the font. */
17985
17986 /* If we found a font, this font should give us the right
17987 metrics. If we didn't find a font, use the frame's
17988 default font and calculate the width of the character
17989 from the charset width; this is what old redisplay code
17990 did. */
17991
17992 pcm = rif->per_char_metric (font, &char2b,
17993 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
17994
17995 if (font_not_found_p || !pcm)
17996 {
17997 int charset = CHAR_CHARSET (it->char_to_display);
17998
17999 it->glyph_not_available_p = 1;
18000 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
18001 * CHARSET_WIDTH (charset));
18002 it->phys_ascent = FONT_BASE (font) + boff;
18003 it->phys_descent = FONT_DESCENT (font) - boff;
18004 }
18005 else
18006 {
18007 it->pixel_width = pcm->width;
18008 it->phys_ascent = pcm->ascent + boff;
18009 it->phys_descent = pcm->descent - boff;
18010 if (it->glyph_row
18011 && (pcm->lbearing < 0
18012 || pcm->rbearing > pcm->width))
18013 it->glyph_row->contains_overlapping_glyphs_p = 1;
18014 }
18015 it->nglyphs = 1;
18016 it->ascent = FONT_BASE (font) + boff;
18017 it->descent = FONT_DESCENT (font) - boff;
18018 if (face->box != FACE_NO_BOX)
18019 {
18020 int thick = face->box_line_width;
18021
18022 if (thick > 0)
18023 {
18024 it->ascent += thick;
18025 it->descent += thick;
18026 }
18027 else
18028 thick = - thick;
18029
18030 if (it->start_of_box_run_p)
18031 it->pixel_width += thick;
18032 if (it->end_of_box_run_p)
18033 it->pixel_width += thick;
18034 }
18035
18036 /* If face has an overline, add the height of the overline
18037 (1 pixel) and a 1 pixel margin to the character height. */
18038 if (face->overline_p)
18039 it->ascent += 2;
18040
18041 take_vertical_position_into_account (it);
18042
18043 if (it->glyph_row)
18044 append_glyph (it);
18045 }
18046 it->multibyte_p = saved_multibyte_p;
18047 }
18048 else if (it->what == IT_COMPOSITION)
18049 {
18050 /* Note: A composition is represented as one glyph in the
18051 glyph matrix. There are no padding glyphs. */
18052 XChar2b char2b;
18053 XFontStruct *font;
18054 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18055 XCharStruct *pcm;
18056 int font_not_found_p;
18057 struct font_info *font_info;
18058 int boff; /* baseline offset */
18059 struct composition *cmp = composition_table[it->cmp_id];
18060
18061 /* Maybe translate single-byte characters to multibyte. */
18062 it->char_to_display = it->c;
18063 if (unibyte_display_via_language_environment
18064 && SINGLE_BYTE_CHAR_P (it->c)
18065 && (it->c >= 0240
18066 || (it->c >= 0200
18067 && !NILP (Vnonascii_translation_table))))
18068 {
18069 it->char_to_display = unibyte_char_to_multibyte (it->c);
18070 }
18071
18072 /* Get face and font to use. Encode IT->char_to_display. */
18073 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18074 face = FACE_FROM_ID (it->f, it->face_id);
18075 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18076 &char2b, it->multibyte_p, 0);
18077 font = face->font;
18078
18079 /* When no suitable font found, use the default font. */
18080 font_not_found_p = font == NULL;
18081 if (font_not_found_p)
18082 {
18083 font = FRAME_FONT (it->f);
18084 boff = FRAME_BASELINE_OFFSET (it->f);
18085 font_info = NULL;
18086 }
18087 else
18088 {
18089 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18090 boff = font_info->baseline_offset;
18091 if (font_info->vertical_centering)
18092 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18093 }
18094
18095 /* There are no padding glyphs, so there is only one glyph to
18096 produce for the composition. Important is that pixel_width,
18097 ascent and descent are the values of what is drawn by
18098 draw_glyphs (i.e. the values of the overall glyphs composed). */
18099 it->nglyphs = 1;
18100
18101 /* If we have not yet calculated pixel size data of glyphs of
18102 the composition for the current face font, calculate them
18103 now. Theoretically, we have to check all fonts for the
18104 glyphs, but that requires much time and memory space. So,
18105 here we check only the font of the first glyph. This leads
18106 to incorrect display very rarely, and C-l (recenter) can
18107 correct the display anyway. */
18108 if (cmp->font != (void *) font)
18109 {
18110 /* Ascent and descent of the font of the first character of
18111 this composition (adjusted by baseline offset). Ascent
18112 and descent of overall glyphs should not be less than
18113 them respectively. */
18114 int font_ascent = FONT_BASE (font) + boff;
18115 int font_descent = FONT_DESCENT (font) - boff;
18116 /* Bounding box of the overall glyphs. */
18117 int leftmost, rightmost, lowest, highest;
18118 int i, width, ascent, descent;
18119
18120 cmp->font = (void *) font;
18121
18122 /* Initialize the bounding box. */
18123 if (font_info
18124 && (pcm = rif->per_char_metric (font, &char2b,
18125 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
18126 {
18127 width = pcm->width;
18128 ascent = pcm->ascent;
18129 descent = pcm->descent;
18130 }
18131 else
18132 {
18133 width = FONT_WIDTH (font);
18134 ascent = FONT_BASE (font);
18135 descent = FONT_DESCENT (font);
18136 }
18137
18138 rightmost = width;
18139 lowest = - descent + boff;
18140 highest = ascent + boff;
18141 leftmost = 0;
18142
18143 if (font_info
18144 && font_info->default_ascent
18145 && CHAR_TABLE_P (Vuse_default_ascent)
18146 && !NILP (Faref (Vuse_default_ascent,
18147 make_number (it->char_to_display))))
18148 highest = font_info->default_ascent + boff;
18149
18150 /* Draw the first glyph at the normal position. It may be
18151 shifted to right later if some other glyphs are drawn at
18152 the left. */
18153 cmp->offsets[0] = 0;
18154 cmp->offsets[1] = boff;
18155
18156 /* Set cmp->offsets for the remaining glyphs. */
18157 for (i = 1; i < cmp->glyph_len; i++)
18158 {
18159 int left, right, btm, top;
18160 int ch = COMPOSITION_GLYPH (cmp, i);
18161 int face_id = FACE_FOR_CHAR (it->f, face, ch);
18162
18163 face = FACE_FROM_ID (it->f, face_id);
18164 get_char_face_and_encoding (it->f, ch, face->id,
18165 &char2b, it->multibyte_p, 0);
18166 font = face->font;
18167 if (font == NULL)
18168 {
18169 font = FRAME_FONT (it->f);
18170 boff = FRAME_BASELINE_OFFSET (it->f);
18171 font_info = NULL;
18172 }
18173 else
18174 {
18175 font_info
18176 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18177 boff = font_info->baseline_offset;
18178 if (font_info->vertical_centering)
18179 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18180 }
18181
18182 if (font_info
18183 && (pcm = rif->per_char_metric (font, &char2b,
18184 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
18185 {
18186 width = pcm->width;
18187 ascent = pcm->ascent;
18188 descent = pcm->descent;
18189 }
18190 else
18191 {
18192 width = FONT_WIDTH (font);
18193 ascent = 1;
18194 descent = 0;
18195 }
18196
18197 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
18198 {
18199 /* Relative composition with or without
18200 alternate chars. */
18201 left = (leftmost + rightmost - width) / 2;
18202 btm = - descent + boff;
18203 if (font_info && font_info->relative_compose
18204 && (! CHAR_TABLE_P (Vignore_relative_composition)
18205 || NILP (Faref (Vignore_relative_composition,
18206 make_number (ch)))))
18207 {
18208
18209 if (- descent >= font_info->relative_compose)
18210 /* One extra pixel between two glyphs. */
18211 btm = highest + 1;
18212 else if (ascent <= 0)
18213 /* One extra pixel between two glyphs. */
18214 btm = lowest - 1 - ascent - descent;
18215 }
18216 }
18217 else
18218 {
18219 /* A composition rule is specified by an integer
18220 value that encodes global and new reference
18221 points (GREF and NREF). GREF and NREF are
18222 specified by numbers as below:
18223
18224 0---1---2 -- ascent
18225 | |
18226 | |
18227 | |
18228 9--10--11 -- center
18229 | |
18230 ---3---4---5--- baseline
18231 | |
18232 6---7---8 -- descent
18233 */
18234 int rule = COMPOSITION_RULE (cmp, i);
18235 int gref, nref, grefx, grefy, nrefx, nrefy;
18236
18237 COMPOSITION_DECODE_RULE (rule, gref, nref);
18238 grefx = gref % 3, nrefx = nref % 3;
18239 grefy = gref / 3, nrefy = nref / 3;
18240
18241 left = (leftmost
18242 + grefx * (rightmost - leftmost) / 2
18243 - nrefx * width / 2);
18244 btm = ((grefy == 0 ? highest
18245 : grefy == 1 ? 0
18246 : grefy == 2 ? lowest
18247 : (highest + lowest) / 2)
18248 - (nrefy == 0 ? ascent + descent
18249 : nrefy == 1 ? descent - boff
18250 : nrefy == 2 ? 0
18251 : (ascent + descent) / 2));
18252 }
18253
18254 cmp->offsets[i * 2] = left;
18255 cmp->offsets[i * 2 + 1] = btm + descent;
18256
18257 /* Update the bounding box of the overall glyphs. */
18258 right = left + width;
18259 top = btm + descent + ascent;
18260 if (left < leftmost)
18261 leftmost = left;
18262 if (right > rightmost)
18263 rightmost = right;
18264 if (top > highest)
18265 highest = top;
18266 if (btm < lowest)
18267 lowest = btm;
18268 }
18269
18270 /* If there are glyphs whose x-offsets are negative,
18271 shift all glyphs to the right and make all x-offsets
18272 non-negative. */
18273 if (leftmost < 0)
18274 {
18275 for (i = 0; i < cmp->glyph_len; i++)
18276 cmp->offsets[i * 2] -= leftmost;
18277 rightmost -= leftmost;
18278 }
18279
18280 cmp->pixel_width = rightmost;
18281 cmp->ascent = highest;
18282 cmp->descent = - lowest;
18283 if (cmp->ascent < font_ascent)
18284 cmp->ascent = font_ascent;
18285 if (cmp->descent < font_descent)
18286 cmp->descent = font_descent;
18287 }
18288
18289 it->pixel_width = cmp->pixel_width;
18290 it->ascent = it->phys_ascent = cmp->ascent;
18291 it->descent = it->phys_descent = cmp->descent;
18292
18293 if (face->box != FACE_NO_BOX)
18294 {
18295 int thick = face->box_line_width;
18296
18297 if (thick > 0)
18298 {
18299 it->ascent += thick;
18300 it->descent += thick;
18301 }
18302 else
18303 thick = - thick;
18304
18305 if (it->start_of_box_run_p)
18306 it->pixel_width += thick;
18307 if (it->end_of_box_run_p)
18308 it->pixel_width += thick;
18309 }
18310
18311 /* If face has an overline, add the height of the overline
18312 (1 pixel) and a 1 pixel margin to the character height. */
18313 if (face->overline_p)
18314 it->ascent += 2;
18315
18316 take_vertical_position_into_account (it);
18317
18318 if (it->glyph_row)
18319 append_composite_glyph (it);
18320 }
18321 else if (it->what == IT_IMAGE)
18322 produce_image_glyph (it);
18323 else if (it->what == IT_STRETCH)
18324 produce_stretch_glyph (it);
18325
18326 /* Accumulate dimensions. Note: can't assume that it->descent > 0
18327 because this isn't true for images with `:ascent 100'. */
18328 xassert (it->ascent >= 0 && it->descent >= 0);
18329 if (it->area == TEXT_AREA)
18330 it->current_x += it->pixel_width;
18331
18332 it->descent += it->extra_line_spacing;
18333
18334 it->max_ascent = max (it->max_ascent, it->ascent);
18335 it->max_descent = max (it->max_descent, it->descent);
18336 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
18337 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
18338 }
18339
18340 /* EXPORT for RIF:
18341 Output LEN glyphs starting at START at the nominal cursor position.
18342 Advance the nominal cursor over the text. The global variable
18343 updated_window contains the window being updated, updated_row is
18344 the glyph row being updated, and updated_area is the area of that
18345 row being updated. */
18346
18347 void
18348 x_write_glyphs (start, len)
18349 struct glyph *start;
18350 int len;
18351 {
18352 int x, hpos;
18353
18354 xassert (updated_window && updated_row);
18355 BLOCK_INPUT;
18356
18357 /* Write glyphs. */
18358
18359 hpos = start - updated_row->glyphs[updated_area];
18360 x = draw_glyphs (updated_window, output_cursor.x,
18361 updated_row, updated_area,
18362 hpos, hpos + len,
18363 DRAW_NORMAL_TEXT, 0);
18364
18365 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
18366 if (updated_area == TEXT_AREA
18367 && updated_window->phys_cursor_on_p
18368 && updated_window->phys_cursor.vpos == output_cursor.vpos
18369 && updated_window->phys_cursor.hpos >= hpos
18370 && updated_window->phys_cursor.hpos < hpos + len)
18371 updated_window->phys_cursor_on_p = 0;
18372
18373 UNBLOCK_INPUT;
18374
18375 /* Advance the output cursor. */
18376 output_cursor.hpos += len;
18377 output_cursor.x = x;
18378 }
18379
18380
18381 /* EXPORT for RIF:
18382 Insert LEN glyphs from START at the nominal cursor position. */
18383
18384 void
18385 x_insert_glyphs (start, len)
18386 struct glyph *start;
18387 int len;
18388 {
18389 struct frame *f;
18390 struct window *w;
18391 int line_height, shift_by_width, shifted_region_width;
18392 struct glyph_row *row;
18393 struct glyph *glyph;
18394 int frame_x, frame_y, hpos;
18395
18396 xassert (updated_window && updated_row);
18397 BLOCK_INPUT;
18398 w = updated_window;
18399 f = XFRAME (WINDOW_FRAME (w));
18400
18401 /* Get the height of the line we are in. */
18402 row = updated_row;
18403 line_height = row->height;
18404
18405 /* Get the width of the glyphs to insert. */
18406 shift_by_width = 0;
18407 for (glyph = start; glyph < start + len; ++glyph)
18408 shift_by_width += glyph->pixel_width;
18409
18410 /* Get the width of the region to shift right. */
18411 shifted_region_width = (window_box_width (w, updated_area)
18412 - output_cursor.x
18413 - shift_by_width);
18414
18415 /* Shift right. */
18416 frame_x = window_box_left (w, updated_area) + output_cursor.x;
18417 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
18418
18419 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
18420 line_height, shift_by_width);
18421
18422 /* Write the glyphs. */
18423 hpos = start - row->glyphs[updated_area];
18424 draw_glyphs (w, output_cursor.x, row, updated_area,
18425 hpos, hpos + len,
18426 DRAW_NORMAL_TEXT, 0);
18427
18428 /* Advance the output cursor. */
18429 output_cursor.hpos += len;
18430 output_cursor.x += shift_by_width;
18431 UNBLOCK_INPUT;
18432 }
18433
18434
18435 /* EXPORT for RIF:
18436 Erase the current text line from the nominal cursor position
18437 (inclusive) to pixel column TO_X (exclusive). The idea is that
18438 everything from TO_X onward is already erased.
18439
18440 TO_X is a pixel position relative to updated_area of
18441 updated_window. TO_X == -1 means clear to the end of this area. */
18442
18443 void
18444 x_clear_end_of_line (to_x)
18445 int to_x;
18446 {
18447 struct frame *f;
18448 struct window *w = updated_window;
18449 int max_x, min_y, max_y;
18450 int from_x, from_y, to_y;
18451
18452 xassert (updated_window && updated_row);
18453 f = XFRAME (w->frame);
18454
18455 if (updated_row->full_width_p)
18456 {
18457 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
18458 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
18459 && !w->pseudo_window_p)
18460 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
18461 }
18462 else
18463 max_x = window_box_width (w, updated_area);
18464 max_y = window_text_bottom_y (w);
18465
18466 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
18467 of window. For TO_X > 0, truncate to end of drawing area. */
18468 if (to_x == 0)
18469 return;
18470 else if (to_x < 0)
18471 to_x = max_x;
18472 else
18473 to_x = min (to_x, max_x);
18474
18475 to_y = min (max_y, output_cursor.y + updated_row->height);
18476
18477 /* Notice if the cursor will be cleared by this operation. */
18478 if (!updated_row->full_width_p)
18479 notice_overwritten_cursor (w, updated_area,
18480 output_cursor.x, -1,
18481 updated_row->y,
18482 MATRIX_ROW_BOTTOM_Y (updated_row));
18483
18484 from_x = output_cursor.x;
18485
18486 /* Translate to frame coordinates. */
18487 if (updated_row->full_width_p)
18488 {
18489 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
18490 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
18491 }
18492 else
18493 {
18494 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
18495 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
18496 }
18497
18498 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
18499 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
18500 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
18501
18502 /* Prevent inadvertently clearing to end of the X window. */
18503 if (to_x > from_x && to_y > from_y)
18504 {
18505 BLOCK_INPUT;
18506 rif->clear_frame_area (f, from_x, from_y,
18507 to_x - from_x, to_y - from_y);
18508 UNBLOCK_INPUT;
18509 }
18510 }
18511
18512 #endif /* HAVE_WINDOW_SYSTEM */
18513
18514
18515 \f
18516 /***********************************************************************
18517 Cursor types
18518 ***********************************************************************/
18519
18520 /* Value is the internal representation of the specified cursor type
18521 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
18522 of the bar cursor. */
18523
18524 enum text_cursor_kinds
18525 get_specified_cursor_type (arg, width)
18526 Lisp_Object arg;
18527 int *width;
18528 {
18529 enum text_cursor_kinds type;
18530
18531 if (NILP (arg))
18532 return NO_CURSOR;
18533
18534 if (EQ (arg, Qbox))
18535 return FILLED_BOX_CURSOR;
18536
18537 if (EQ (arg, Qhollow))
18538 return HOLLOW_BOX_CURSOR;
18539
18540 if (EQ (arg, Qbar))
18541 {
18542 *width = 2;
18543 return BAR_CURSOR;
18544 }
18545
18546 if (CONSP (arg)
18547 && EQ (XCAR (arg), Qbar)
18548 && INTEGERP (XCDR (arg))
18549 && XINT (XCDR (arg)) >= 0)
18550 {
18551 *width = XINT (XCDR (arg));
18552 return BAR_CURSOR;
18553 }
18554
18555 if (EQ (arg, Qhbar))
18556 {
18557 *width = 2;
18558 return HBAR_CURSOR;
18559 }
18560
18561 if (CONSP (arg)
18562 && EQ (XCAR (arg), Qhbar)
18563 && INTEGERP (XCDR (arg))
18564 && XINT (XCDR (arg)) >= 0)
18565 {
18566 *width = XINT (XCDR (arg));
18567 return HBAR_CURSOR;
18568 }
18569
18570 /* Treat anything unknown as "hollow box cursor".
18571 It was bad to signal an error; people have trouble fixing
18572 .Xdefaults with Emacs, when it has something bad in it. */
18573 type = HOLLOW_BOX_CURSOR;
18574
18575 return type;
18576 }
18577
18578 /* Set the default cursor types for specified frame. */
18579 void
18580 set_frame_cursor_types (f, arg)
18581 struct frame *f;
18582 Lisp_Object arg;
18583 {
18584 int width;
18585 Lisp_Object tem;
18586
18587 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
18588 FRAME_CURSOR_WIDTH (f) = width;
18589
18590 /* By default, set up the blink-off state depending on the on-state. */
18591
18592 tem = Fassoc (arg, Vblink_cursor_alist);
18593 if (!NILP (tem))
18594 {
18595 FRAME_BLINK_OFF_CURSOR (f)
18596 = get_specified_cursor_type (XCDR (tem), &width);
18597 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
18598 }
18599 else
18600 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
18601 }
18602
18603
18604 /* Return the cursor we want to be displayed in window W. Return
18605 width of bar/hbar cursor through WIDTH arg. Return with
18606 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
18607 (i.e. if the `system caret' should track this cursor).
18608
18609 In a mini-buffer window, we want the cursor only to appear if we
18610 are reading input from this window. For the selected window, we
18611 want the cursor type given by the frame parameter or buffer local
18612 setting of cursor-type. If explicitly marked off, draw no cursor.
18613 In all other cases, we want a hollow box cursor. */
18614
18615 enum text_cursor_kinds
18616 get_window_cursor_type (w, width, active_cursor)
18617 struct window *w;
18618 int *width;
18619 int *active_cursor;
18620 {
18621 struct frame *f = XFRAME (w->frame);
18622 struct buffer *b = XBUFFER (w->buffer);
18623 int cursor_type = DEFAULT_CURSOR;
18624 Lisp_Object alt_cursor;
18625 int non_selected = 0;
18626
18627 *active_cursor = 1;
18628
18629 /* Echo area */
18630 if (cursor_in_echo_area
18631 && FRAME_HAS_MINIBUF_P (f)
18632 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
18633 {
18634 if (w == XWINDOW (echo_area_window))
18635 {
18636 *width = FRAME_CURSOR_WIDTH (f);
18637 return FRAME_DESIRED_CURSOR (f);
18638 }
18639
18640 *active_cursor = 0;
18641 non_selected = 1;
18642 }
18643
18644 /* Nonselected window or nonselected frame. */
18645 else if (w != XWINDOW (f->selected_window)
18646 #ifdef HAVE_WINDOW_SYSTEM
18647 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
18648 #endif
18649 )
18650 {
18651 *active_cursor = 0;
18652
18653 if (MINI_WINDOW_P (w) && minibuf_level == 0)
18654 return NO_CURSOR;
18655
18656 non_selected = 1;
18657 }
18658
18659 /* Never display a cursor in a window in which cursor-type is nil. */
18660 if (NILP (b->cursor_type))
18661 return NO_CURSOR;
18662
18663 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
18664 if (non_selected)
18665 {
18666 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
18667 return get_specified_cursor_type (alt_cursor, width);
18668 }
18669
18670 /* Get the normal cursor type for this window. */
18671 if (EQ (b->cursor_type, Qt))
18672 {
18673 cursor_type = FRAME_DESIRED_CURSOR (f);
18674 *width = FRAME_CURSOR_WIDTH (f);
18675 }
18676 else
18677 cursor_type = get_specified_cursor_type (b->cursor_type, width);
18678
18679 /* Use normal cursor if not blinked off. */
18680 if (!w->cursor_off_p)
18681 return cursor_type;
18682
18683 /* Cursor is blinked off, so determine how to "toggle" it. */
18684
18685 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
18686 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
18687 return get_specified_cursor_type (XCDR (alt_cursor), width);
18688
18689 /* Then see if frame has specified a specific blink off cursor type. */
18690 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
18691 {
18692 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
18693 return FRAME_BLINK_OFF_CURSOR (f);
18694 }
18695
18696 /* Finally perform built-in cursor blinking:
18697 filled box <-> hollow box
18698 wide [h]bar <-> narrow [h]bar
18699 narrow [h]bar <-> no cursor
18700 other type <-> no cursor */
18701
18702 if (cursor_type == FILLED_BOX_CURSOR)
18703 return HOLLOW_BOX_CURSOR;
18704
18705 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
18706 {
18707 *width = 1;
18708 return cursor_type;
18709 }
18710
18711 return NO_CURSOR;
18712 }
18713
18714
18715 #ifdef HAVE_WINDOW_SYSTEM
18716
18717 /* Notice when the text cursor of window W has been completely
18718 overwritten by a drawing operation that outputs glyphs in AREA
18719 starting at X0 and ending at X1 in the line starting at Y0 and
18720 ending at Y1. X coordinates are area-relative. X1 < 0 means all
18721 the rest of the line after X0 has been written. Y coordinates
18722 are window-relative. */
18723
18724 static void
18725 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
18726 struct window *w;
18727 enum glyph_row_area area;
18728 int x0, y0, x1, y1;
18729 {
18730 if (area == TEXT_AREA && w->phys_cursor_on_p)
18731 {
18732 int cx0 = w->phys_cursor.x;
18733 int cx1 = cx0 + w->phys_cursor_width;
18734 int cy0 = w->phys_cursor.y;
18735 int cy1 = cy0 + w->phys_cursor_height;
18736
18737 if (x0 <= cx0 && (x1 < 0 || x1 >= cx1))
18738 {
18739 /* The cursor image will be completely removed from the
18740 screen if the output area intersects the cursor area in
18741 y-direction. When we draw in [y0 y1[, and some part of
18742 the cursor is at y < y0, that part must have been drawn
18743 before. When scrolling, the cursor is erased before
18744 actually scrolling, so we don't come here. When not
18745 scrolling, the rows above the old cursor row must have
18746 changed, and in this case these rows must have written
18747 over the cursor image.
18748
18749 Likewise if part of the cursor is below y1, with the
18750 exception of the cursor being in the first blank row at
18751 the buffer and window end because update_text_area
18752 doesn't draw that row. (Except when it does, but
18753 that's handled in update_text_area.) */
18754
18755 if (((y0 >= cy0 && y0 < cy1) || (y1 > cy0 && y1 < cy1))
18756 && w->current_matrix->rows[w->phys_cursor.vpos].displays_text_p)
18757 w->phys_cursor_on_p = 0;
18758 }
18759 }
18760 }
18761
18762 #endif /* HAVE_WINDOW_SYSTEM */
18763
18764 \f
18765 /************************************************************************
18766 Mouse Face
18767 ************************************************************************/
18768
18769 #ifdef HAVE_WINDOW_SYSTEM
18770
18771 /* EXPORT for RIF:
18772 Fix the display of area AREA of overlapping row ROW in window W. */
18773
18774 void
18775 x_fix_overlapping_area (w, row, area)
18776 struct window *w;
18777 struct glyph_row *row;
18778 enum glyph_row_area area;
18779 {
18780 int i, x;
18781
18782 BLOCK_INPUT;
18783
18784 if (area == LEFT_MARGIN_AREA)
18785 x = 0;
18786 else if (area == TEXT_AREA)
18787 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
18788 else
18789 x = (window_box_width (w, LEFT_MARGIN_AREA)
18790 + window_box_width (w, TEXT_AREA));
18791
18792 for (i = 0; i < row->used[area];)
18793 {
18794 if (row->glyphs[area][i].overlaps_vertically_p)
18795 {
18796 int start = i, start_x = x;
18797
18798 do
18799 {
18800 x += row->glyphs[area][i].pixel_width;
18801 ++i;
18802 }
18803 while (i < row->used[area]
18804 && row->glyphs[area][i].overlaps_vertically_p);
18805
18806 draw_glyphs (w, start_x, row, area,
18807 start, i,
18808 DRAW_NORMAL_TEXT, 1);
18809 }
18810 else
18811 {
18812 x += row->glyphs[area][i].pixel_width;
18813 ++i;
18814 }
18815 }
18816
18817 UNBLOCK_INPUT;
18818 }
18819
18820
18821 /* EXPORT:
18822 Draw the cursor glyph of window W in glyph row ROW. See the
18823 comment of draw_glyphs for the meaning of HL. */
18824
18825 void
18826 draw_phys_cursor_glyph (w, row, hl)
18827 struct window *w;
18828 struct glyph_row *row;
18829 enum draw_glyphs_face hl;
18830 {
18831 /* If cursor hpos is out of bounds, don't draw garbage. This can
18832 happen in mini-buffer windows when switching between echo area
18833 glyphs and mini-buffer. */
18834 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
18835 {
18836 int on_p = w->phys_cursor_on_p;
18837 int x1;
18838 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
18839 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
18840 hl, 0);
18841 w->phys_cursor_on_p = on_p;
18842
18843 if (hl == DRAW_CURSOR)
18844 w->phys_cursor_width = x1 - w->phys_cursor.x;
18845 /* When we erase the cursor, and ROW is overlapped by other
18846 rows, make sure that these overlapping parts of other rows
18847 are redrawn. */
18848 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
18849 {
18850 if (row > w->current_matrix->rows
18851 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
18852 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
18853
18854 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
18855 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
18856 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
18857 }
18858 }
18859 }
18860
18861
18862 /* EXPORT:
18863 Erase the image of a cursor of window W from the screen. */
18864
18865 void
18866 erase_phys_cursor (w)
18867 struct window *w;
18868 {
18869 struct frame *f = XFRAME (w->frame);
18870 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
18871 int hpos = w->phys_cursor.hpos;
18872 int vpos = w->phys_cursor.vpos;
18873 int mouse_face_here_p = 0;
18874 struct glyph_matrix *active_glyphs = w->current_matrix;
18875 struct glyph_row *cursor_row;
18876 struct glyph *cursor_glyph;
18877 enum draw_glyphs_face hl;
18878
18879 /* No cursor displayed or row invalidated => nothing to do on the
18880 screen. */
18881 if (w->phys_cursor_type == NO_CURSOR)
18882 goto mark_cursor_off;
18883
18884 /* VPOS >= active_glyphs->nrows means that window has been resized.
18885 Don't bother to erase the cursor. */
18886 if (vpos >= active_glyphs->nrows)
18887 goto mark_cursor_off;
18888
18889 /* If row containing cursor is marked invalid, there is nothing we
18890 can do. */
18891 cursor_row = MATRIX_ROW (active_glyphs, vpos);
18892 if (!cursor_row->enabled_p)
18893 goto mark_cursor_off;
18894
18895 /* If row is completely invisible, don't attempt to delete a cursor which
18896 isn't there. This can happen if cursor is at top of a window, and
18897 we switch to a buffer with a header line in that window. */
18898 if (cursor_row->visible_height <= 0)
18899 goto mark_cursor_off;
18900
18901 /* This can happen when the new row is shorter than the old one.
18902 In this case, either draw_glyphs or clear_end_of_line
18903 should have cleared the cursor. Note that we wouldn't be
18904 able to erase the cursor in this case because we don't have a
18905 cursor glyph at hand. */
18906 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
18907 goto mark_cursor_off;
18908
18909 /* If the cursor is in the mouse face area, redisplay that when
18910 we clear the cursor. */
18911 if (! NILP (dpyinfo->mouse_face_window)
18912 && w == XWINDOW (dpyinfo->mouse_face_window)
18913 && (vpos > dpyinfo->mouse_face_beg_row
18914 || (vpos == dpyinfo->mouse_face_beg_row
18915 && hpos >= dpyinfo->mouse_face_beg_col))
18916 && (vpos < dpyinfo->mouse_face_end_row
18917 || (vpos == dpyinfo->mouse_face_end_row
18918 && hpos < dpyinfo->mouse_face_end_col))
18919 /* Don't redraw the cursor's spot in mouse face if it is at the
18920 end of a line (on a newline). The cursor appears there, but
18921 mouse highlighting does not. */
18922 && cursor_row->used[TEXT_AREA] > hpos)
18923 mouse_face_here_p = 1;
18924
18925 /* Maybe clear the display under the cursor. */
18926 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
18927 {
18928 int x, y;
18929 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
18930
18931 cursor_glyph = get_phys_cursor_glyph (w);
18932 if (cursor_glyph == NULL)
18933 goto mark_cursor_off;
18934
18935 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
18936 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
18937
18938 rif->clear_frame_area (f, x, y,
18939 cursor_glyph->pixel_width, cursor_row->visible_height);
18940 }
18941
18942 /* Erase the cursor by redrawing the character underneath it. */
18943 if (mouse_face_here_p)
18944 hl = DRAW_MOUSE_FACE;
18945 else
18946 hl = DRAW_NORMAL_TEXT;
18947 draw_phys_cursor_glyph (w, cursor_row, hl);
18948
18949 mark_cursor_off:
18950 w->phys_cursor_on_p = 0;
18951 w->phys_cursor_type = NO_CURSOR;
18952 }
18953
18954
18955 /* EXPORT:
18956 Display or clear cursor of window W. If ON is zero, clear the
18957 cursor. If it is non-zero, display the cursor. If ON is nonzero,
18958 where to put the cursor is specified by HPOS, VPOS, X and Y. */
18959
18960 void
18961 display_and_set_cursor (w, on, hpos, vpos, x, y)
18962 struct window *w;
18963 int on, hpos, vpos, x, y;
18964 {
18965 struct frame *f = XFRAME (w->frame);
18966 int new_cursor_type;
18967 int new_cursor_width;
18968 int active_cursor;
18969 struct glyph_matrix *current_glyphs;
18970 struct glyph_row *glyph_row;
18971 struct glyph *glyph;
18972
18973 /* This is pointless on invisible frames, and dangerous on garbaged
18974 windows and frames; in the latter case, the frame or window may
18975 be in the midst of changing its size, and x and y may be off the
18976 window. */
18977 if (! FRAME_VISIBLE_P (f)
18978 || FRAME_GARBAGED_P (f)
18979 || vpos >= w->current_matrix->nrows
18980 || hpos >= w->current_matrix->matrix_w)
18981 return;
18982
18983 /* If cursor is off and we want it off, return quickly. */
18984 if (!on && !w->phys_cursor_on_p)
18985 return;
18986
18987 current_glyphs = w->current_matrix;
18988 glyph_row = MATRIX_ROW (current_glyphs, vpos);
18989 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
18990
18991 /* If cursor row is not enabled, we don't really know where to
18992 display the cursor. */
18993 if (!glyph_row->enabled_p)
18994 {
18995 w->phys_cursor_on_p = 0;
18996 return;
18997 }
18998
18999 xassert (interrupt_input_blocked);
19000
19001 /* Set new_cursor_type to the cursor we want to be displayed. */
19002 new_cursor_type = get_window_cursor_type (w, &new_cursor_width, &active_cursor);
19003
19004 /* If cursor is currently being shown and we don't want it to be or
19005 it is in the wrong place, or the cursor type is not what we want,
19006 erase it. */
19007 if (w->phys_cursor_on_p
19008 && (!on
19009 || w->phys_cursor.x != x
19010 || w->phys_cursor.y != y
19011 || new_cursor_type != w->phys_cursor_type
19012 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
19013 && new_cursor_width != w->phys_cursor_width)))
19014 erase_phys_cursor (w);
19015
19016 /* Don't check phys_cursor_on_p here because that flag is only set
19017 to zero in some cases where we know that the cursor has been
19018 completely erased, to avoid the extra work of erasing the cursor
19019 twice. In other words, phys_cursor_on_p can be 1 and the cursor
19020 still not be visible, or it has only been partly erased. */
19021 if (on)
19022 {
19023 w->phys_cursor_ascent = glyph_row->ascent;
19024 w->phys_cursor_height = glyph_row->height;
19025
19026 /* Set phys_cursor_.* before x_draw_.* is called because some
19027 of them may need the information. */
19028 w->phys_cursor.x = x;
19029 w->phys_cursor.y = glyph_row->y;
19030 w->phys_cursor.hpos = hpos;
19031 w->phys_cursor.vpos = vpos;
19032 }
19033
19034 rif->draw_window_cursor (w, glyph_row, x, y,
19035 new_cursor_type, new_cursor_width,
19036 on, active_cursor);
19037 }
19038
19039
19040 /* Switch the display of W's cursor on or off, according to the value
19041 of ON. */
19042
19043 static void
19044 update_window_cursor (w, on)
19045 struct window *w;
19046 int on;
19047 {
19048 /* Don't update cursor in windows whose frame is in the process
19049 of being deleted. */
19050 if (w->current_matrix)
19051 {
19052 BLOCK_INPUT;
19053 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
19054 w->phys_cursor.x, w->phys_cursor.y);
19055 UNBLOCK_INPUT;
19056 }
19057 }
19058
19059
19060 /* Call update_window_cursor with parameter ON_P on all leaf windows
19061 in the window tree rooted at W. */
19062
19063 static void
19064 update_cursor_in_window_tree (w, on_p)
19065 struct window *w;
19066 int on_p;
19067 {
19068 while (w)
19069 {
19070 if (!NILP (w->hchild))
19071 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
19072 else if (!NILP (w->vchild))
19073 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
19074 else
19075 update_window_cursor (w, on_p);
19076
19077 w = NILP (w->next) ? 0 : XWINDOW (w->next);
19078 }
19079 }
19080
19081
19082 /* EXPORT:
19083 Display the cursor on window W, or clear it, according to ON_P.
19084 Don't change the cursor's position. */
19085
19086 void
19087 x_update_cursor (f, on_p)
19088 struct frame *f;
19089 int on_p;
19090 {
19091 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
19092 }
19093
19094
19095 /* EXPORT:
19096 Clear the cursor of window W to background color, and mark the
19097 cursor as not shown. This is used when the text where the cursor
19098 is is about to be rewritten. */
19099
19100 void
19101 x_clear_cursor (w)
19102 struct window *w;
19103 {
19104 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
19105 update_window_cursor (w, 0);
19106 }
19107
19108
19109 /* EXPORT:
19110 Display the active region described by mouse_face_* according to DRAW. */
19111
19112 void
19113 show_mouse_face (dpyinfo, draw)
19114 Display_Info *dpyinfo;
19115 enum draw_glyphs_face draw;
19116 {
19117 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
19118 struct frame *f = XFRAME (WINDOW_FRAME (w));
19119
19120 if (/* If window is in the process of being destroyed, don't bother
19121 to do anything. */
19122 w->current_matrix != NULL
19123 /* Don't update mouse highlight if hidden */
19124 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
19125 /* Recognize when we are called to operate on rows that don't exist
19126 anymore. This can happen when a window is split. */
19127 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
19128 {
19129 int phys_cursor_on_p = w->phys_cursor_on_p;
19130 struct glyph_row *row, *first, *last;
19131
19132 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
19133 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
19134
19135 for (row = first; row <= last && row->enabled_p; ++row)
19136 {
19137 int start_hpos, end_hpos, start_x;
19138
19139 /* For all but the first row, the highlight starts at column 0. */
19140 if (row == first)
19141 {
19142 start_hpos = dpyinfo->mouse_face_beg_col;
19143 start_x = dpyinfo->mouse_face_beg_x;
19144 }
19145 else
19146 {
19147 start_hpos = 0;
19148 start_x = 0;
19149 }
19150
19151 if (row == last)
19152 end_hpos = dpyinfo->mouse_face_end_col;
19153 else
19154 end_hpos = row->used[TEXT_AREA];
19155
19156 if (end_hpos > start_hpos)
19157 {
19158 draw_glyphs (w, start_x, row, TEXT_AREA,
19159 start_hpos, end_hpos,
19160 draw, 0);
19161
19162 row->mouse_face_p
19163 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
19164 }
19165 }
19166
19167 /* When we've written over the cursor, arrange for it to
19168 be displayed again. */
19169 if (phys_cursor_on_p && !w->phys_cursor_on_p)
19170 {
19171 BLOCK_INPUT;
19172 display_and_set_cursor (w, 1,
19173 w->phys_cursor.hpos, w->phys_cursor.vpos,
19174 w->phys_cursor.x, w->phys_cursor.y);
19175 UNBLOCK_INPUT;
19176 }
19177 }
19178
19179 /* Change the mouse cursor. */
19180 if (draw == DRAW_NORMAL_TEXT)
19181 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
19182 else if (draw == DRAW_MOUSE_FACE)
19183 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
19184 else
19185 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
19186 }
19187
19188 /* EXPORT:
19189 Clear out the mouse-highlighted active region.
19190 Redraw it un-highlighted first. Value is non-zero if mouse
19191 face was actually drawn unhighlighted. */
19192
19193 int
19194 clear_mouse_face (dpyinfo)
19195 Display_Info *dpyinfo;
19196 {
19197 int cleared = 0;
19198
19199 if (!NILP (dpyinfo->mouse_face_window))
19200 {
19201 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
19202 cleared = 1;
19203 }
19204
19205 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
19206 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
19207 dpyinfo->mouse_face_window = Qnil;
19208 dpyinfo->mouse_face_overlay = Qnil;
19209 return cleared;
19210 }
19211
19212
19213 /* EXPORT:
19214 Non-zero if physical cursor of window W is within mouse face. */
19215
19216 int
19217 cursor_in_mouse_face_p (w)
19218 struct window *w;
19219 {
19220 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
19221 int in_mouse_face = 0;
19222
19223 if (WINDOWP (dpyinfo->mouse_face_window)
19224 && XWINDOW (dpyinfo->mouse_face_window) == w)
19225 {
19226 int hpos = w->phys_cursor.hpos;
19227 int vpos = w->phys_cursor.vpos;
19228
19229 if (vpos >= dpyinfo->mouse_face_beg_row
19230 && vpos <= dpyinfo->mouse_face_end_row
19231 && (vpos > dpyinfo->mouse_face_beg_row
19232 || hpos >= dpyinfo->mouse_face_beg_col)
19233 && (vpos < dpyinfo->mouse_face_end_row
19234 || hpos < dpyinfo->mouse_face_end_col
19235 || dpyinfo->mouse_face_past_end))
19236 in_mouse_face = 1;
19237 }
19238
19239 return in_mouse_face;
19240 }
19241
19242
19243
19244 \f
19245 /* Find the glyph matrix position of buffer position CHARPOS in window
19246 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
19247 current glyphs must be up to date. If CHARPOS is above window
19248 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
19249 of last line in W. In the row containing CHARPOS, stop before glyphs
19250 having STOP as object. */
19251
19252 #if 0 /* This is a version of fast_find_position that's more correct
19253 in the presence of hscrolling, for example. I didn't install
19254 it right away because the problem fixed is minor, it failed
19255 in 20.x as well, and I think it's too risky to install
19256 so near the release of 21.1. 2001-09-25 gerd. */
19257
19258 static int
19259 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
19260 struct window *w;
19261 int charpos;
19262 int *hpos, *vpos, *x, *y;
19263 Lisp_Object stop;
19264 {
19265 struct glyph_row *row, *first;
19266 struct glyph *glyph, *end;
19267 int i, past_end = 0;
19268
19269 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19270 row = row_containing_pos (w, charpos, first, NULL, 0);
19271 if (row == NULL)
19272 {
19273 if (charpos < MATRIX_ROW_START_CHARPOS (first))
19274 {
19275 *x = *y = *hpos = *vpos = 0;
19276 return 0;
19277 }
19278 else
19279 {
19280 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
19281 past_end = 1;
19282 }
19283 }
19284
19285 *x = row->x;
19286 *y = row->y;
19287 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
19288
19289 glyph = row->glyphs[TEXT_AREA];
19290 end = glyph + row->used[TEXT_AREA];
19291
19292 /* Skip over glyphs not having an object at the start of the row.
19293 These are special glyphs like truncation marks on terminal
19294 frames. */
19295 if (row->displays_text_p)
19296 while (glyph < end
19297 && INTEGERP (glyph->object)
19298 && !EQ (stop, glyph->object)
19299 && glyph->charpos < 0)
19300 {
19301 *x += glyph->pixel_width;
19302 ++glyph;
19303 }
19304
19305 while (glyph < end
19306 && !INTEGERP (glyph->object)
19307 && !EQ (stop, glyph->object)
19308 && (!BUFFERP (glyph->object)
19309 || glyph->charpos < charpos))
19310 {
19311 *x += glyph->pixel_width;
19312 ++glyph;
19313 }
19314
19315 *hpos = glyph - row->glyphs[TEXT_AREA];
19316 return past_end;
19317 }
19318
19319 #else /* not 0 */
19320
19321 static int
19322 fast_find_position (w, pos, hpos, vpos, x, y, stop)
19323 struct window *w;
19324 int pos;
19325 int *hpos, *vpos, *x, *y;
19326 Lisp_Object stop;
19327 {
19328 int i;
19329 int lastcol;
19330 int maybe_next_line_p = 0;
19331 int line_start_position;
19332 int yb = window_text_bottom_y (w);
19333 struct glyph_row *row, *best_row;
19334 int row_vpos, best_row_vpos;
19335 int current_x;
19336
19337 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19338 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
19339
19340 while (row->y < yb)
19341 {
19342 if (row->used[TEXT_AREA])
19343 line_start_position = row->glyphs[TEXT_AREA]->charpos;
19344 else
19345 line_start_position = 0;
19346
19347 if (line_start_position > pos)
19348 break;
19349 /* If the position sought is the end of the buffer,
19350 don't include the blank lines at the bottom of the window. */
19351 else if (line_start_position == pos
19352 && pos == BUF_ZV (XBUFFER (w->buffer)))
19353 {
19354 maybe_next_line_p = 1;
19355 break;
19356 }
19357 else if (line_start_position > 0)
19358 {
19359 best_row = row;
19360 best_row_vpos = row_vpos;
19361 }
19362
19363 if (row->y + row->height >= yb)
19364 break;
19365
19366 ++row;
19367 ++row_vpos;
19368 }
19369
19370 /* Find the right column within BEST_ROW. */
19371 lastcol = 0;
19372 current_x = best_row->x;
19373 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
19374 {
19375 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
19376 int charpos = glyph->charpos;
19377
19378 if (BUFFERP (glyph->object))
19379 {
19380 if (charpos == pos)
19381 {
19382 *hpos = i;
19383 *vpos = best_row_vpos;
19384 *x = current_x;
19385 *y = best_row->y;
19386 return 1;
19387 }
19388 else if (charpos > pos)
19389 break;
19390 }
19391 else if (EQ (glyph->object, stop))
19392 break;
19393
19394 if (charpos > 0)
19395 lastcol = i;
19396 current_x += glyph->pixel_width;
19397 }
19398
19399 /* If we're looking for the end of the buffer,
19400 and we didn't find it in the line we scanned,
19401 use the start of the following line. */
19402 if (maybe_next_line_p)
19403 {
19404 ++best_row;
19405 ++best_row_vpos;
19406 lastcol = 0;
19407 current_x = best_row->x;
19408 }
19409
19410 *vpos = best_row_vpos;
19411 *hpos = lastcol + 1;
19412 *x = current_x;
19413 *y = best_row->y;
19414 return 0;
19415 }
19416
19417 #endif /* not 0 */
19418
19419
19420 /* Find the position of the glyph for position POS in OBJECT in
19421 window W's current matrix, and return in *X, *Y the pixel
19422 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
19423
19424 RIGHT_P non-zero means return the position of the right edge of the
19425 glyph, RIGHT_P zero means return the left edge position.
19426
19427 If no glyph for POS exists in the matrix, return the position of
19428 the glyph with the next smaller position that is in the matrix, if
19429 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
19430 exists in the matrix, return the position of the glyph with the
19431 next larger position in OBJECT.
19432
19433 Value is non-zero if a glyph was found. */
19434
19435 static int
19436 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
19437 struct window *w;
19438 int pos;
19439 Lisp_Object object;
19440 int *hpos, *vpos, *x, *y;
19441 int right_p;
19442 {
19443 int yb = window_text_bottom_y (w);
19444 struct glyph_row *r;
19445 struct glyph *best_glyph = NULL;
19446 struct glyph_row *best_row = NULL;
19447 int best_x = 0;
19448
19449 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19450 r->enabled_p && r->y < yb;
19451 ++r)
19452 {
19453 struct glyph *g = r->glyphs[TEXT_AREA];
19454 struct glyph *e = g + r->used[TEXT_AREA];
19455 int gx;
19456
19457 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
19458 if (EQ (g->object, object))
19459 {
19460 if (g->charpos == pos)
19461 {
19462 best_glyph = g;
19463 best_x = gx;
19464 best_row = r;
19465 goto found;
19466 }
19467 else if (best_glyph == NULL
19468 || ((abs (g->charpos - pos)
19469 < abs (best_glyph->charpos - pos))
19470 && (right_p
19471 ? g->charpos < pos
19472 : g->charpos > pos)))
19473 {
19474 best_glyph = g;
19475 best_x = gx;
19476 best_row = r;
19477 }
19478 }
19479 }
19480
19481 found:
19482
19483 if (best_glyph)
19484 {
19485 *x = best_x;
19486 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
19487
19488 if (right_p)
19489 {
19490 *x += best_glyph->pixel_width;
19491 ++*hpos;
19492 }
19493
19494 *y = best_row->y;
19495 *vpos = best_row - w->current_matrix->rows;
19496 }
19497
19498 return best_glyph != NULL;
19499 }
19500
19501
19502 /* Take proper action when mouse has moved to the mode or header line
19503 or marginal area AREA of window W, x-position X and y-position Y.
19504 X is relative to the start of the text display area of W, so the
19505 width of bitmap areas and scroll bars must be subtracted to get a
19506 position relative to the start of the mode line. */
19507
19508 static void
19509 note_mode_line_or_margin_highlight (w, x, y, area)
19510 struct window *w;
19511 int x, y;
19512 enum window_part area;
19513 {
19514 struct frame *f = XFRAME (w->frame);
19515 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
19516 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
19517 int charpos;
19518 Lisp_Object string, help, map, pos;
19519
19520 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
19521 string = mode_line_string (w, x, y, area, &charpos);
19522 else
19523 string = marginal_area_string (w, x, y, area, &charpos);
19524
19525 if (STRINGP (string))
19526 {
19527 pos = make_number (charpos);
19528
19529 /* If we're on a string with `help-echo' text property, arrange
19530 for the help to be displayed. This is done by setting the
19531 global variable help_echo_string to the help string. */
19532 help = Fget_text_property (pos, Qhelp_echo, string);
19533 if (!NILP (help))
19534 {
19535 help_echo_string = help;
19536 XSETWINDOW (help_echo_window, w);
19537 help_echo_object = string;
19538 help_echo_pos = charpos;
19539 }
19540
19541 /* Change the mouse pointer according to what is under X/Y. */
19542 map = Fget_text_property (pos, Qlocal_map, string);
19543 if (!KEYMAPP (map))
19544 map = Fget_text_property (pos, Qkeymap, string);
19545 if (KEYMAPP (map))
19546 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
19547 }
19548
19549 rif->define_frame_cursor (f, cursor);
19550 }
19551
19552
19553 /* EXPORT:
19554 Take proper action when the mouse has moved to position X, Y on
19555 frame F as regards highlighting characters that have mouse-face
19556 properties. Also de-highlighting chars where the mouse was before.
19557 X and Y can be negative or out of range. */
19558
19559 void
19560 note_mouse_highlight (f, x, y)
19561 struct frame *f;
19562 int x, y;
19563 {
19564 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
19565 enum window_part part;
19566 Lisp_Object window;
19567 struct window *w;
19568 Cursor cursor = No_Cursor;
19569 struct buffer *b;
19570
19571 /* When a menu is active, don't highlight because this looks odd. */
19572 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
19573 if (popup_activated ())
19574 return;
19575 #endif
19576
19577 if (NILP (Vmouse_highlight)
19578 || !f->glyphs_initialized_p)
19579 return;
19580
19581 dpyinfo->mouse_face_mouse_x = x;
19582 dpyinfo->mouse_face_mouse_y = y;
19583 dpyinfo->mouse_face_mouse_frame = f;
19584
19585 if (dpyinfo->mouse_face_defer)
19586 return;
19587
19588 if (gc_in_progress)
19589 {
19590 dpyinfo->mouse_face_deferred_gc = 1;
19591 return;
19592 }
19593
19594 /* Which window is that in? */
19595 window = window_from_coordinates (f, x, y, &part, 1);
19596
19597 /* If we were displaying active text in another window, clear that. */
19598 if (! EQ (window, dpyinfo->mouse_face_window))
19599 clear_mouse_face (dpyinfo);
19600
19601 /* Not on a window -> return. */
19602 if (!WINDOWP (window))
19603 return;
19604
19605 /* Reset help_echo_string. It will get recomputed below. */
19606 /* ++KFS: X version didn't do this, but it looks harmless. */
19607 help_echo_string = Qnil;
19608
19609 /* Convert to window-relative pixel coordinates. */
19610 w = XWINDOW (window);
19611 frame_to_window_pixel_xy (w, &x, &y);
19612
19613 /* Handle tool-bar window differently since it doesn't display a
19614 buffer. */
19615 if (EQ (window, f->tool_bar_window))
19616 {
19617 note_tool_bar_highlight (f, x, y);
19618 return;
19619 }
19620
19621 /* Mouse is on the mode, header line or margin? */
19622 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
19623 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
19624 {
19625 note_mode_line_or_margin_highlight (w, x, y, part);
19626 return;
19627 }
19628
19629 if (part == ON_VERTICAL_BORDER)
19630 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
19631 else
19632 cursor = FRAME_X_OUTPUT (f)->text_cursor;
19633
19634 /* Are we in a window whose display is up to date?
19635 And verify the buffer's text has not changed. */
19636 b = XBUFFER (w->buffer);
19637 if (part == ON_TEXT
19638 && EQ (w->window_end_valid, w->buffer)
19639 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
19640 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
19641 {
19642 int hpos, vpos, pos, i, area;
19643 struct glyph *glyph;
19644 Lisp_Object object;
19645 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
19646 Lisp_Object *overlay_vec = NULL;
19647 int len, noverlays;
19648 struct buffer *obuf;
19649 int obegv, ozv, same_region;
19650
19651 /* Find the glyph under X/Y. */
19652 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area, 0);
19653
19654 /* Clear mouse face if X/Y not over text. */
19655 if (glyph == NULL
19656 || area != TEXT_AREA
19657 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
19658 {
19659 #if defined (HAVE_NTGUI)
19660 /* ++KFS: Why is this necessary on W32 ? */
19661 clear_mouse_face (dpyinfo);
19662 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
19663 #else
19664 if (clear_mouse_face (dpyinfo))
19665 cursor = No_Cursor;
19666 #endif
19667 goto set_cursor;
19668 }
19669
19670 pos = glyph->charpos;
19671 object = glyph->object;
19672 if (!STRINGP (object) && !BUFFERP (object))
19673 goto set_cursor;
19674
19675 /* If we get an out-of-range value, return now; avoid an error. */
19676 if (BUFFERP (object) && pos > BUF_Z (b))
19677 goto set_cursor;
19678
19679 /* Make the window's buffer temporarily current for
19680 overlays_at and compute_char_face. */
19681 obuf = current_buffer;
19682 current_buffer = b;
19683 obegv = BEGV;
19684 ozv = ZV;
19685 BEGV = BEG;
19686 ZV = Z;
19687
19688 /* Is this char mouse-active or does it have help-echo? */
19689 position = make_number (pos);
19690
19691 if (BUFFERP (object))
19692 {
19693 /* Put all the overlays we want in a vector in overlay_vec.
19694 Store the length in len. If there are more than 10, make
19695 enough space for all, and try again. */
19696 len = 10;
19697 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
19698 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
19699 if (noverlays > len)
19700 {
19701 len = noverlays;
19702 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
19703 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
19704 }
19705
19706 /* Sort overlays into increasing priority order. */
19707 noverlays = sort_overlays (overlay_vec, noverlays, w);
19708 }
19709 else
19710 noverlays = 0;
19711
19712 same_region = (EQ (window, dpyinfo->mouse_face_window)
19713 && vpos >= dpyinfo->mouse_face_beg_row
19714 && vpos <= dpyinfo->mouse_face_end_row
19715 && (vpos > dpyinfo->mouse_face_beg_row
19716 || hpos >= dpyinfo->mouse_face_beg_col)
19717 && (vpos < dpyinfo->mouse_face_end_row
19718 || hpos < dpyinfo->mouse_face_end_col
19719 || dpyinfo->mouse_face_past_end));
19720
19721 if (same_region)
19722 cursor = No_Cursor;
19723
19724 /* Check mouse-face highlighting. */
19725 if (! same_region
19726 /* If there exists an overlay with mouse-face overlapping
19727 the one we are currently highlighting, we have to
19728 check if we enter the overlapping overlay, and then
19729 highlight only that. */
19730 || (OVERLAYP (dpyinfo->mouse_face_overlay)
19731 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
19732 {
19733 /* Find the highest priority overlay that has a mouse-face
19734 property. */
19735 overlay = Qnil;
19736 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
19737 {
19738 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
19739 if (!NILP (mouse_face))
19740 overlay = overlay_vec[i];
19741 }
19742
19743 /* If we're actually highlighting the same overlay as
19744 before, there's no need to do that again. */
19745 if (!NILP (overlay)
19746 && EQ (overlay, dpyinfo->mouse_face_overlay))
19747 goto check_help_echo;
19748
19749 dpyinfo->mouse_face_overlay = overlay;
19750
19751 /* Clear the display of the old active region, if any. */
19752 if (clear_mouse_face (dpyinfo))
19753 cursor = No_Cursor;
19754
19755 /* If no overlay applies, get a text property. */
19756 if (NILP (overlay))
19757 mouse_face = Fget_text_property (position, Qmouse_face, object);
19758
19759 /* Handle the overlay case. */
19760 if (!NILP (overlay))
19761 {
19762 /* Find the range of text around this char that
19763 should be active. */
19764 Lisp_Object before, after;
19765 int ignore;
19766
19767 before = Foverlay_start (overlay);
19768 after = Foverlay_end (overlay);
19769 /* Record this as the current active region. */
19770 fast_find_position (w, XFASTINT (before),
19771 &dpyinfo->mouse_face_beg_col,
19772 &dpyinfo->mouse_face_beg_row,
19773 &dpyinfo->mouse_face_beg_x,
19774 &dpyinfo->mouse_face_beg_y, Qnil);
19775
19776 dpyinfo->mouse_face_past_end
19777 = !fast_find_position (w, XFASTINT (after),
19778 &dpyinfo->mouse_face_end_col,
19779 &dpyinfo->mouse_face_end_row,
19780 &dpyinfo->mouse_face_end_x,
19781 &dpyinfo->mouse_face_end_y, Qnil);
19782 dpyinfo->mouse_face_window = window;
19783
19784 dpyinfo->mouse_face_face_id
19785 = face_at_buffer_position (w, pos, 0, 0,
19786 &ignore, pos + 1,
19787 !dpyinfo->mouse_face_hidden);
19788
19789 /* Display it as active. */
19790 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
19791 cursor = No_Cursor;
19792 }
19793 /* Handle the text property case. */
19794 else if (!NILP (mouse_face) && BUFFERP (object))
19795 {
19796 /* Find the range of text around this char that
19797 should be active. */
19798 Lisp_Object before, after, beginning, end;
19799 int ignore;
19800
19801 beginning = Fmarker_position (w->start);
19802 end = make_number (BUF_Z (XBUFFER (object))
19803 - XFASTINT (w->window_end_pos));
19804 before
19805 = Fprevious_single_property_change (make_number (pos + 1),
19806 Qmouse_face,
19807 object, beginning);
19808 after
19809 = Fnext_single_property_change (position, Qmouse_face,
19810 object, end);
19811
19812 /* Record this as the current active region. */
19813 fast_find_position (w, XFASTINT (before),
19814 &dpyinfo->mouse_face_beg_col,
19815 &dpyinfo->mouse_face_beg_row,
19816 &dpyinfo->mouse_face_beg_x,
19817 &dpyinfo->mouse_face_beg_y, Qnil);
19818 dpyinfo->mouse_face_past_end
19819 = !fast_find_position (w, XFASTINT (after),
19820 &dpyinfo->mouse_face_end_col,
19821 &dpyinfo->mouse_face_end_row,
19822 &dpyinfo->mouse_face_end_x,
19823 &dpyinfo->mouse_face_end_y, Qnil);
19824 dpyinfo->mouse_face_window = window;
19825
19826 if (BUFFERP (object))
19827 dpyinfo->mouse_face_face_id
19828 = face_at_buffer_position (w, pos, 0, 0,
19829 &ignore, pos + 1,
19830 !dpyinfo->mouse_face_hidden);
19831
19832 /* Display it as active. */
19833 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
19834 cursor = No_Cursor;
19835 }
19836 else if (!NILP (mouse_face) && STRINGP (object))
19837 {
19838 Lisp_Object b, e;
19839 int ignore;
19840
19841 b = Fprevious_single_property_change (make_number (pos + 1),
19842 Qmouse_face,
19843 object, Qnil);
19844 e = Fnext_single_property_change (position, Qmouse_face,
19845 object, Qnil);
19846 if (NILP (b))
19847 b = make_number (0);
19848 if (NILP (e))
19849 e = make_number (SCHARS (object) - 1);
19850 fast_find_string_pos (w, XINT (b), object,
19851 &dpyinfo->mouse_face_beg_col,
19852 &dpyinfo->mouse_face_beg_row,
19853 &dpyinfo->mouse_face_beg_x,
19854 &dpyinfo->mouse_face_beg_y, 0);
19855 fast_find_string_pos (w, XINT (e), object,
19856 &dpyinfo->mouse_face_end_col,
19857 &dpyinfo->mouse_face_end_row,
19858 &dpyinfo->mouse_face_end_x,
19859 &dpyinfo->mouse_face_end_y, 1);
19860 dpyinfo->mouse_face_past_end = 0;
19861 dpyinfo->mouse_face_window = window;
19862 dpyinfo->mouse_face_face_id
19863 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
19864 glyph->face_id, 1);
19865 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
19866 cursor = No_Cursor;
19867 }
19868 else if (STRINGP (object) && NILP (mouse_face))
19869 {
19870 /* A string which doesn't have mouse-face, but
19871 the text ``under'' it might have. */
19872 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
19873 int start = MATRIX_ROW_START_CHARPOS (r);
19874
19875 pos = string_buffer_position (w, object, start);
19876 if (pos > 0)
19877 mouse_face = get_char_property_and_overlay (make_number (pos),
19878 Qmouse_face,
19879 w->buffer,
19880 &overlay);
19881 if (!NILP (mouse_face) && !NILP (overlay))
19882 {
19883 Lisp_Object before = Foverlay_start (overlay);
19884 Lisp_Object after = Foverlay_end (overlay);
19885 int ignore;
19886
19887 /* Note that we might not be able to find position
19888 BEFORE in the glyph matrix if the overlay is
19889 entirely covered by a `display' property. In
19890 this case, we overshoot. So let's stop in
19891 the glyph matrix before glyphs for OBJECT. */
19892 fast_find_position (w, XFASTINT (before),
19893 &dpyinfo->mouse_face_beg_col,
19894 &dpyinfo->mouse_face_beg_row,
19895 &dpyinfo->mouse_face_beg_x,
19896 &dpyinfo->mouse_face_beg_y,
19897 object);
19898
19899 dpyinfo->mouse_face_past_end
19900 = !fast_find_position (w, XFASTINT (after),
19901 &dpyinfo->mouse_face_end_col,
19902 &dpyinfo->mouse_face_end_row,
19903 &dpyinfo->mouse_face_end_x,
19904 &dpyinfo->mouse_face_end_y,
19905 Qnil);
19906 dpyinfo->mouse_face_window = window;
19907 dpyinfo->mouse_face_face_id
19908 = face_at_buffer_position (w, pos, 0, 0,
19909 &ignore, pos + 1,
19910 !dpyinfo->mouse_face_hidden);
19911
19912 /* Display it as active. */
19913 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
19914 cursor = No_Cursor;
19915 }
19916 }
19917 }
19918
19919 check_help_echo:
19920
19921 /* Look for a `help-echo' property. */
19922 {
19923 Lisp_Object help, overlay;
19924
19925 /* Check overlays first. */
19926 help = overlay = Qnil;
19927 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
19928 {
19929 overlay = overlay_vec[i];
19930 help = Foverlay_get (overlay, Qhelp_echo);
19931 }
19932
19933 if (!NILP (help))
19934 {
19935 help_echo_string = help;
19936 help_echo_window = window;
19937 help_echo_object = overlay;
19938 help_echo_pos = pos;
19939 }
19940 else
19941 {
19942 Lisp_Object object = glyph->object;
19943 int charpos = glyph->charpos;
19944
19945 /* Try text properties. */
19946 if (STRINGP (object)
19947 && charpos >= 0
19948 && charpos < SCHARS (object))
19949 {
19950 help = Fget_text_property (make_number (charpos),
19951 Qhelp_echo, object);
19952 if (NILP (help))
19953 {
19954 /* If the string itself doesn't specify a help-echo,
19955 see if the buffer text ``under'' it does. */
19956 struct glyph_row *r
19957 = MATRIX_ROW (w->current_matrix, vpos);
19958 int start = MATRIX_ROW_START_CHARPOS (r);
19959 int pos = string_buffer_position (w, object, start);
19960 if (pos > 0)
19961 {
19962 help = Fget_char_property (make_number (pos),
19963 Qhelp_echo, w->buffer);
19964 if (!NILP (help))
19965 {
19966 charpos = pos;
19967 object = w->buffer;
19968 }
19969 }
19970 }
19971 }
19972 else if (BUFFERP (object)
19973 && charpos >= BEGV
19974 && charpos < ZV)
19975 help = Fget_text_property (make_number (charpos), Qhelp_echo,
19976 object);
19977
19978 if (!NILP (help))
19979 {
19980 help_echo_string = help;
19981 help_echo_window = window;
19982 help_echo_object = object;
19983 help_echo_pos = charpos;
19984 }
19985 }
19986 }
19987
19988 BEGV = obegv;
19989 ZV = ozv;
19990 current_buffer = obuf;
19991 }
19992
19993 set_cursor:
19994
19995 #ifndef HAVE_CARBON
19996 if (cursor != No_Cursor)
19997 #else
19998 if (bcmp (&cursor, &No_Cursor, sizeof (Cursor)))
19999 #endif
20000 rif->define_frame_cursor (f, cursor);
20001 }
20002
20003
20004 /* EXPORT for RIF:
20005 Clear any mouse-face on window W. This function is part of the
20006 redisplay interface, and is called from try_window_id and similar
20007 functions to ensure the mouse-highlight is off. */
20008
20009 void
20010 x_clear_window_mouse_face (w)
20011 struct window *w;
20012 {
20013 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20014 Lisp_Object window;
20015
20016 BLOCK_INPUT;
20017 XSETWINDOW (window, w);
20018 if (EQ (window, dpyinfo->mouse_face_window))
20019 clear_mouse_face (dpyinfo);
20020 UNBLOCK_INPUT;
20021 }
20022
20023
20024 /* EXPORT:
20025 Just discard the mouse face information for frame F, if any.
20026 This is used when the size of F is changed. */
20027
20028 void
20029 cancel_mouse_face (f)
20030 struct frame *f;
20031 {
20032 Lisp_Object window;
20033 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20034
20035 window = dpyinfo->mouse_face_window;
20036 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
20037 {
20038 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20039 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20040 dpyinfo->mouse_face_window = Qnil;
20041 }
20042 }
20043
20044
20045 #endif /* HAVE_WINDOW_SYSTEM */
20046
20047 \f
20048 /***********************************************************************
20049 Exposure Events
20050 ***********************************************************************/
20051
20052 #ifdef HAVE_WINDOW_SYSTEM
20053
20054 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
20055 which intersects rectangle R. R is in window-relative coordinates. */
20056
20057 static void
20058 expose_area (w, row, r, area)
20059 struct window *w;
20060 struct glyph_row *row;
20061 XRectangle *r;
20062 enum glyph_row_area area;
20063 {
20064 struct glyph *first = row->glyphs[area];
20065 struct glyph *end = row->glyphs[area] + row->used[area];
20066 struct glyph *last;
20067 int first_x, start_x, x;
20068
20069 if (area == TEXT_AREA && row->fill_line_p)
20070 /* If row extends face to end of line write the whole line. */
20071 draw_glyphs (w, 0, row, area,
20072 0, row->used[area],
20073 DRAW_NORMAL_TEXT, 0);
20074 else
20075 {
20076 /* Set START_X to the window-relative start position for drawing glyphs of
20077 AREA. The first glyph of the text area can be partially visible.
20078 The first glyphs of other areas cannot. */
20079 if (area == LEFT_MARGIN_AREA)
20080 start_x = 0;
20081 else if (area == TEXT_AREA)
20082 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
20083 else
20084 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
20085 + window_box_width (w, TEXT_AREA));
20086 x = start_x;
20087
20088 /* Find the first glyph that must be redrawn. */
20089 while (first < end
20090 && x + first->pixel_width < r->x)
20091 {
20092 x += first->pixel_width;
20093 ++first;
20094 }
20095
20096 /* Find the last one. */
20097 last = first;
20098 first_x = x;
20099 while (last < end
20100 && x < r->x + r->width)
20101 {
20102 x += last->pixel_width;
20103 ++last;
20104 }
20105
20106 /* Repaint. */
20107 if (last > first)
20108 draw_glyphs (w, first_x - start_x, row, area,
20109 first - row->glyphs[area], last - row->glyphs[area],
20110 DRAW_NORMAL_TEXT, 0);
20111 }
20112 }
20113
20114
20115 /* Redraw the parts of the glyph row ROW on window W intersecting
20116 rectangle R. R is in window-relative coordinates. Value is
20117 non-zero if mouse-face was overwritten. */
20118
20119 static int
20120 expose_line (w, row, r)
20121 struct window *w;
20122 struct glyph_row *row;
20123 XRectangle *r;
20124 {
20125 xassert (row->enabled_p);
20126
20127 if (row->mode_line_p || w->pseudo_window_p)
20128 draw_glyphs (w, 0, row, TEXT_AREA,
20129 0, row->used[TEXT_AREA],
20130 DRAW_NORMAL_TEXT, 0);
20131 else
20132 {
20133 if (row->used[LEFT_MARGIN_AREA])
20134 expose_area (w, row, r, LEFT_MARGIN_AREA);
20135 if (row->used[TEXT_AREA])
20136 expose_area (w, row, r, TEXT_AREA);
20137 if (row->used[RIGHT_MARGIN_AREA])
20138 expose_area (w, row, r, RIGHT_MARGIN_AREA);
20139 draw_row_fringe_bitmaps (w, row);
20140 }
20141
20142 return row->mouse_face_p;
20143 }
20144
20145
20146 /* Redraw those parts of glyphs rows during expose event handling that
20147 overlap other rows. Redrawing of an exposed line writes over parts
20148 of lines overlapping that exposed line; this function fixes that.
20149
20150 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
20151 row in W's current matrix that is exposed and overlaps other rows.
20152 LAST_OVERLAPPING_ROW is the last such row. */
20153
20154 static void
20155 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
20156 struct window *w;
20157 struct glyph_row *first_overlapping_row;
20158 struct glyph_row *last_overlapping_row;
20159 {
20160 struct glyph_row *row;
20161
20162 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
20163 if (row->overlapping_p)
20164 {
20165 xassert (row->enabled_p && !row->mode_line_p);
20166
20167 if (row->used[LEFT_MARGIN_AREA])
20168 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
20169
20170 if (row->used[TEXT_AREA])
20171 x_fix_overlapping_area (w, row, TEXT_AREA);
20172
20173 if (row->used[RIGHT_MARGIN_AREA])
20174 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
20175 }
20176 }
20177
20178
20179 /* Return non-zero if W's cursor intersects rectangle R. */
20180
20181 static int
20182 phys_cursor_in_rect_p (w, r)
20183 struct window *w;
20184 XRectangle *r;
20185 {
20186 XRectangle cr, result;
20187 struct glyph *cursor_glyph;
20188
20189 cursor_glyph = get_phys_cursor_glyph (w);
20190 if (cursor_glyph)
20191 {
20192 cr.x = w->phys_cursor.x;
20193 cr.y = w->phys_cursor.y;
20194 cr.width = cursor_glyph->pixel_width;
20195 cr.height = w->phys_cursor_height;
20196 /* ++KFS: W32 version used W32-specific IntersectRect here, but
20197 I assume the effect is the same -- and this is portable. */
20198 return x_intersect_rectangles (&cr, r, &result);
20199 }
20200 else
20201 return 0;
20202 }
20203
20204
20205 /* EXPORT:
20206 Draw a vertical window border to the right of window W if W doesn't
20207 have vertical scroll bars. */
20208
20209 void
20210 x_draw_vertical_border (w)
20211 struct window *w;
20212 {
20213 struct frame *f = XFRAME (WINDOW_FRAME (w));
20214
20215 /* Redraw borders between horizontally adjacent windows. Don't
20216 do it for frames with vertical scroll bars because either the
20217 right scroll bar of a window, or the left scroll bar of its
20218 neighbor will suffice as a border. */
20219 if (!WINDOW_RIGHTMOST_P (w)
20220 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
20221 {
20222 int x0, x1, y0, y1;
20223
20224 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
20225 x1 += FRAME_X_RIGHT_FRINGE_WIDTH (f);
20226 y1 -= 1;
20227
20228 rif->draw_vertical_window_border (w, x1, y0, y1);
20229 }
20230 }
20231
20232
20233 /* Redraw the part of window W intersection rectangle FR. Pixel
20234 coordinates in FR are frame-relative. Call this function with
20235 input blocked. Value is non-zero if the exposure overwrites
20236 mouse-face. */
20237
20238 static int
20239 expose_window (w, fr)
20240 struct window *w;
20241 XRectangle *fr;
20242 {
20243 struct frame *f = XFRAME (w->frame);
20244 XRectangle wr, r;
20245 int mouse_face_overwritten_p = 0;
20246
20247 /* If window is not yet fully initialized, do nothing. This can
20248 happen when toolkit scroll bars are used and a window is split.
20249 Reconfiguring the scroll bar will generate an expose for a newly
20250 created window. */
20251 if (w->current_matrix == NULL)
20252 return 0;
20253
20254 /* When we're currently updating the window, display and current
20255 matrix usually don't agree. Arrange for a thorough display
20256 later. */
20257 if (w == updated_window)
20258 {
20259 SET_FRAME_GARBAGED (f);
20260 return 0;
20261 }
20262
20263 /* Frame-relative pixel rectangle of W. */
20264 wr.x = XFASTINT (w->left) * CANON_X_UNIT (f);
20265 wr.y = XFASTINT (w->top) * CANON_Y_UNIT (f);
20266 wr.width = XFASTINT (w->width) * CANON_X_UNIT (f);
20267 wr.height = XFASTINT (w->height) * CANON_Y_UNIT (f);
20268
20269 if (x_intersect_rectangles (fr, &wr, &r))
20270 {
20271 int yb = window_text_bottom_y (w);
20272 struct glyph_row *row;
20273 int cursor_cleared_p;
20274 struct glyph_row *first_overlapping_row, *last_overlapping_row;
20275
20276 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
20277 r.x, r.y, r.width, r.height));
20278
20279 /* Convert to window coordinates. */
20280 r.x = FRAME_TO_WINDOW_PIXEL_X (w, r.x);
20281 r.y = FRAME_TO_WINDOW_PIXEL_Y (w, r.y);
20282
20283 /* Turn off the cursor. */
20284 if (!w->pseudo_window_p
20285 && phys_cursor_in_rect_p (w, &r))
20286 {
20287 x_clear_cursor (w);
20288 cursor_cleared_p = 1;
20289 }
20290 else
20291 cursor_cleared_p = 0;
20292
20293 /* Update lines intersecting rectangle R. */
20294 first_overlapping_row = last_overlapping_row = NULL;
20295 for (row = w->current_matrix->rows;
20296 row->enabled_p;
20297 ++row)
20298 {
20299 int y0 = row->y;
20300 int y1 = MATRIX_ROW_BOTTOM_Y (row);
20301
20302 if ((y0 >= r.y && y0 < r.y + r.height)
20303 || (y1 > r.y && y1 < r.y + r.height)
20304 || (r.y >= y0 && r.y < y1)
20305 || (r.y + r.height > y0 && r.y + r.height < y1))
20306 {
20307 if (row->overlapping_p)
20308 {
20309 if (first_overlapping_row == NULL)
20310 first_overlapping_row = row;
20311 last_overlapping_row = row;
20312 }
20313
20314 if (expose_line (w, row, &r))
20315 mouse_face_overwritten_p = 1;
20316 }
20317
20318 if (y1 >= yb)
20319 break;
20320 }
20321
20322 /* Display the mode line if there is one. */
20323 if (WINDOW_WANTS_MODELINE_P (w)
20324 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
20325 row->enabled_p)
20326 && row->y < r.y + r.height)
20327 {
20328 if (expose_line (w, row, &r))
20329 mouse_face_overwritten_p = 1;
20330 }
20331
20332 if (!w->pseudo_window_p)
20333 {
20334 /* Fix the display of overlapping rows. */
20335 if (first_overlapping_row)
20336 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
20337
20338 /* Draw border between windows. */
20339 x_draw_vertical_border (w);
20340
20341 /* Turn the cursor on again. */
20342 if (cursor_cleared_p)
20343 update_window_cursor (w, 1);
20344 }
20345 }
20346
20347 #ifdef HAVE_CARBON
20348 /* Display scroll bar for this window. */
20349 if (!NILP (w->vertical_scroll_bar))
20350 {
20351 /* ++KFS:
20352 If this doesn't work here (maybe some header files are missing),
20353 make a function in macterm.c and call it to do the job! */
20354 ControlHandle ch
20355 = SCROLL_BAR_CONTROL_HANDLE (XSCROLL_BAR (w->vertical_scroll_bar));
20356
20357 Draw1Control (ch);
20358 }
20359 #endif
20360
20361 return mouse_face_overwritten_p;
20362 }
20363
20364
20365
20366 /* Redraw (parts) of all windows in the window tree rooted at W that
20367 intersect R. R contains frame pixel coordinates. Value is
20368 non-zero if the exposure overwrites mouse-face. */
20369
20370 static int
20371 expose_window_tree (w, r)
20372 struct window *w;
20373 XRectangle *r;
20374 {
20375 struct frame *f = XFRAME (w->frame);
20376 int mouse_face_overwritten_p = 0;
20377
20378 while (w && !FRAME_GARBAGED_P (f))
20379 {
20380 if (!NILP (w->hchild))
20381 mouse_face_overwritten_p
20382 |= expose_window_tree (XWINDOW (w->hchild), r);
20383 else if (!NILP (w->vchild))
20384 mouse_face_overwritten_p
20385 |= expose_window_tree (XWINDOW (w->vchild), r);
20386 else
20387 mouse_face_overwritten_p |= expose_window (w, r);
20388
20389 w = NILP (w->next) ? NULL : XWINDOW (w->next);
20390 }
20391
20392 return mouse_face_overwritten_p;
20393 }
20394
20395
20396 /* EXPORT:
20397 Redisplay an exposed area of frame F. X and Y are the upper-left
20398 corner of the exposed rectangle. W and H are width and height of
20399 the exposed area. All are pixel values. W or H zero means redraw
20400 the entire frame. */
20401
20402 void
20403 expose_frame (f, x, y, w, h)
20404 struct frame *f;
20405 int x, y, w, h;
20406 {
20407 XRectangle r;
20408 int mouse_face_overwritten_p = 0;
20409
20410 TRACE ((stderr, "expose_frame "));
20411
20412 /* No need to redraw if frame will be redrawn soon. */
20413 if (FRAME_GARBAGED_P (f))
20414 {
20415 TRACE ((stderr, " garbaged\n"));
20416 return;
20417 }
20418
20419 #ifdef HAVE_CARBON
20420 /* MAC_TODO: this is a kludge, but if scroll bars are not activated
20421 or deactivated here, for unknown reasons, activated scroll bars
20422 are shown in deactivated frames in some instances. */
20423 if (f == FRAME_MAC_DISPLAY_INFO (f)->x_focus_frame)
20424 activate_scroll_bars (f);
20425 else
20426 deactivate_scroll_bars (f);
20427 #endif
20428
20429 /* If basic faces haven't been realized yet, there is no point in
20430 trying to redraw anything. This can happen when we get an expose
20431 event while Emacs is starting, e.g. by moving another window. */
20432 if (FRAME_FACE_CACHE (f) == NULL
20433 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
20434 {
20435 TRACE ((stderr, " no faces\n"));
20436 return;
20437 }
20438
20439 if (w == 0 || h == 0)
20440 {
20441 r.x = r.y = 0;
20442 r.width = CANON_X_UNIT (f) * f->width;
20443 r.height = CANON_Y_UNIT (f) * f->height;
20444 }
20445 else
20446 {
20447 r.x = x;
20448 r.y = y;
20449 r.width = w;
20450 r.height = h;
20451 }
20452
20453 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
20454 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
20455
20456 if (WINDOWP (f->tool_bar_window))
20457 mouse_face_overwritten_p
20458 |= expose_window (XWINDOW (f->tool_bar_window), &r);
20459
20460 #ifdef HAVE_X_WINDOWS
20461 #ifndef MSDOS
20462 #ifndef USE_X_TOOLKIT
20463 if (WINDOWP (f->menu_bar_window))
20464 mouse_face_overwritten_p
20465 |= expose_window (XWINDOW (f->menu_bar_window), &r);
20466 #endif /* not USE_X_TOOLKIT */
20467 #endif
20468 #endif
20469
20470 /* Some window managers support a focus-follows-mouse style with
20471 delayed raising of frames. Imagine a partially obscured frame,
20472 and moving the mouse into partially obscured mouse-face on that
20473 frame. The visible part of the mouse-face will be highlighted,
20474 then the WM raises the obscured frame. With at least one WM, KDE
20475 2.1, Emacs is not getting any event for the raising of the frame
20476 (even tried with SubstructureRedirectMask), only Expose events.
20477 These expose events will draw text normally, i.e. not
20478 highlighted. Which means we must redo the highlight here.
20479 Subsume it under ``we love X''. --gerd 2001-08-15 */
20480 /* Included in Windows version because Windows most likely does not
20481 do the right thing if any third party tool offers
20482 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
20483 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
20484 {
20485 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20486 if (f == dpyinfo->mouse_face_mouse_frame)
20487 {
20488 int x = dpyinfo->mouse_face_mouse_x;
20489 int y = dpyinfo->mouse_face_mouse_y;
20490 clear_mouse_face (dpyinfo);
20491 note_mouse_highlight (f, x, y);
20492 }
20493 }
20494 }
20495
20496
20497 /* EXPORT:
20498 Determine the intersection of two rectangles R1 and R2. Return
20499 the intersection in *RESULT. Value is non-zero if RESULT is not
20500 empty. */
20501
20502 int
20503 x_intersect_rectangles (r1, r2, result)
20504 XRectangle *r1, *r2, *result;
20505 {
20506 XRectangle *left, *right;
20507 XRectangle *upper, *lower;
20508 int intersection_p = 0;
20509
20510 /* Rearrange so that R1 is the left-most rectangle. */
20511 if (r1->x < r2->x)
20512 left = r1, right = r2;
20513 else
20514 left = r2, right = r1;
20515
20516 /* X0 of the intersection is right.x0, if this is inside R1,
20517 otherwise there is no intersection. */
20518 if (right->x <= left->x + left->width)
20519 {
20520 result->x = right->x;
20521
20522 /* The right end of the intersection is the minimum of the
20523 the right ends of left and right. */
20524 result->width = (min (left->x + left->width, right->x + right->width)
20525 - result->x);
20526
20527 /* Same game for Y. */
20528 if (r1->y < r2->y)
20529 upper = r1, lower = r2;
20530 else
20531 upper = r2, lower = r1;
20532
20533 /* The upper end of the intersection is lower.y0, if this is inside
20534 of upper. Otherwise, there is no intersection. */
20535 if (lower->y <= upper->y + upper->height)
20536 {
20537 result->y = lower->y;
20538
20539 /* The lower end of the intersection is the minimum of the lower
20540 ends of upper and lower. */
20541 result->height = (min (lower->y + lower->height,
20542 upper->y + upper->height)
20543 - result->y);
20544 intersection_p = 1;
20545 }
20546 }
20547
20548 return intersection_p;
20549 }
20550
20551 #endif /* HAVE_WINDOW_SYSTEM */
20552
20553 \f
20554 /***********************************************************************
20555 Initialization
20556 ***********************************************************************/
20557
20558 void
20559 syms_of_xdisp ()
20560 {
20561 Vwith_echo_area_save_vector = Qnil;
20562 staticpro (&Vwith_echo_area_save_vector);
20563
20564 Vmessage_stack = Qnil;
20565 staticpro (&Vmessage_stack);
20566
20567 Qinhibit_redisplay = intern ("inhibit-redisplay");
20568 staticpro (&Qinhibit_redisplay);
20569
20570 message_dolog_marker1 = Fmake_marker ();
20571 staticpro (&message_dolog_marker1);
20572 message_dolog_marker2 = Fmake_marker ();
20573 staticpro (&message_dolog_marker2);
20574 message_dolog_marker3 = Fmake_marker ();
20575 staticpro (&message_dolog_marker3);
20576
20577 #if GLYPH_DEBUG
20578 defsubr (&Sdump_frame_glyph_matrix);
20579 defsubr (&Sdump_glyph_matrix);
20580 defsubr (&Sdump_glyph_row);
20581 defsubr (&Sdump_tool_bar_row);
20582 defsubr (&Strace_redisplay);
20583 defsubr (&Strace_to_stderr);
20584 #endif
20585 #ifdef HAVE_WINDOW_SYSTEM
20586 defsubr (&Stool_bar_lines_needed);
20587 #endif
20588 defsubr (&Sformat_mode_line);
20589
20590 staticpro (&Qmenu_bar_update_hook);
20591 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
20592
20593 staticpro (&Qoverriding_terminal_local_map);
20594 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
20595
20596 staticpro (&Qoverriding_local_map);
20597 Qoverriding_local_map = intern ("overriding-local-map");
20598
20599 staticpro (&Qwindow_scroll_functions);
20600 Qwindow_scroll_functions = intern ("window-scroll-functions");
20601
20602 staticpro (&Qredisplay_end_trigger_functions);
20603 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
20604
20605 staticpro (&Qinhibit_point_motion_hooks);
20606 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
20607
20608 QCdata = intern (":data");
20609 staticpro (&QCdata);
20610 Qdisplay = intern ("display");
20611 staticpro (&Qdisplay);
20612 Qspace_width = intern ("space-width");
20613 staticpro (&Qspace_width);
20614 Qraise = intern ("raise");
20615 staticpro (&Qraise);
20616 Qspace = intern ("space");
20617 staticpro (&Qspace);
20618 Qmargin = intern ("margin");
20619 staticpro (&Qmargin);
20620 Qleft_margin = intern ("left-margin");
20621 staticpro (&Qleft_margin);
20622 Qright_margin = intern ("right-margin");
20623 staticpro (&Qright_margin);
20624 Qalign_to = intern ("align-to");
20625 staticpro (&Qalign_to);
20626 QCalign_to = intern (":align-to");
20627 staticpro (&QCalign_to);
20628 Qrelative_width = intern ("relative-width");
20629 staticpro (&Qrelative_width);
20630 QCrelative_width = intern (":relative-width");
20631 staticpro (&QCrelative_width);
20632 QCrelative_height = intern (":relative-height");
20633 staticpro (&QCrelative_height);
20634 QCeval = intern (":eval");
20635 staticpro (&QCeval);
20636 QCpropertize = intern (":propertize");
20637 staticpro (&QCpropertize);
20638 QCfile = intern (":file");
20639 staticpro (&QCfile);
20640 Qfontified = intern ("fontified");
20641 staticpro (&Qfontified);
20642 Qfontification_functions = intern ("fontification-functions");
20643 staticpro (&Qfontification_functions);
20644 Qtrailing_whitespace = intern ("trailing-whitespace");
20645 staticpro (&Qtrailing_whitespace);
20646 Qimage = intern ("image");
20647 staticpro (&Qimage);
20648 Qmessage_truncate_lines = intern ("message-truncate-lines");
20649 staticpro (&Qmessage_truncate_lines);
20650 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
20651 staticpro (&Qcursor_in_non_selected_windows);
20652 Qgrow_only = intern ("grow-only");
20653 staticpro (&Qgrow_only);
20654 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
20655 staticpro (&Qinhibit_menubar_update);
20656 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
20657 staticpro (&Qinhibit_eval_during_redisplay);
20658 Qposition = intern ("position");
20659 staticpro (&Qposition);
20660 Qbuffer_position = intern ("buffer-position");
20661 staticpro (&Qbuffer_position);
20662 Qobject = intern ("object");
20663 staticpro (&Qobject);
20664 Qbar = intern ("bar");
20665 staticpro (&Qbar);
20666 Qhbar = intern ("hbar");
20667 staticpro (&Qhbar);
20668 Qbox = intern ("box");
20669 staticpro (&Qbox);
20670 Qhollow = intern ("hollow");
20671 staticpro (&Qhollow);
20672 Qrisky_local_variable = intern ("risky-local-variable");
20673 staticpro (&Qrisky_local_variable);
20674 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
20675 staticpro (&Qinhibit_free_realized_faces);
20676
20677 list_of_error = Fcons (intern ("error"), Qnil);
20678 staticpro (&list_of_error);
20679
20680 last_arrow_position = Qnil;
20681 last_arrow_string = Qnil;
20682 staticpro (&last_arrow_position);
20683 staticpro (&last_arrow_string);
20684
20685 echo_buffer[0] = echo_buffer[1] = Qnil;
20686 staticpro (&echo_buffer[0]);
20687 staticpro (&echo_buffer[1]);
20688
20689 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
20690 staticpro (&echo_area_buffer[0]);
20691 staticpro (&echo_area_buffer[1]);
20692
20693 Vmessages_buffer_name = build_string ("*Messages*");
20694 staticpro (&Vmessages_buffer_name);
20695
20696 mode_line_proptrans_alist = Qnil;
20697 staticpro (&mode_line_proptrans_alist);
20698
20699 mode_line_string_list = Qnil;
20700 staticpro (&mode_line_string_list);
20701
20702 help_echo_string = Qnil;
20703 staticpro (&help_echo_string);
20704 help_echo_object = Qnil;
20705 staticpro (&help_echo_object);
20706 help_echo_window = Qnil;
20707 staticpro (&help_echo_window);
20708 previous_help_echo_string = Qnil;
20709 staticpro (&previous_help_echo_string);
20710 help_echo_pos = -1;
20711
20712 #ifdef HAVE_WINDOW_SYSTEM
20713 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
20714 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
20715 For example, if a block cursor is over a tab, it will be drawn as
20716 wide as that tab on the display. */);
20717 x_stretch_cursor_p = 0;
20718 #endif
20719
20720 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
20721 doc: /* Non-nil means highlight trailing whitespace.
20722 The face used for trailing whitespace is `trailing-whitespace'. */);
20723 Vshow_trailing_whitespace = Qnil;
20724
20725 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
20726 doc: /* Non-nil means don't actually do any redisplay.
20727 This is used for internal purposes. */);
20728 Vinhibit_redisplay = Qnil;
20729
20730 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
20731 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
20732 Vglobal_mode_string = Qnil;
20733
20734 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
20735 doc: /* Marker for where to display an arrow on top of the buffer text.
20736 This must be the beginning of a line in order to work.
20737 See also `overlay-arrow-string'. */);
20738 Voverlay_arrow_position = Qnil;
20739
20740 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
20741 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
20742 Voverlay_arrow_string = Qnil;
20743
20744 DEFVAR_INT ("scroll-step", &scroll_step,
20745 doc: /* *The number of lines to try scrolling a window by when point moves out.
20746 If that fails to bring point back on frame, point is centered instead.
20747 If this is zero, point is always centered after it moves off frame.
20748 If you want scrolling to always be a line at a time, you should set
20749 `scroll-conservatively' to a large value rather than set this to 1. */);
20750
20751 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
20752 doc: /* *Scroll up to this many lines, to bring point back on screen.
20753 A value of zero means to scroll the text to center point vertically
20754 in the window. */);
20755 scroll_conservatively = 0;
20756
20757 DEFVAR_INT ("scroll-margin", &scroll_margin,
20758 doc: /* *Number of lines of margin at the top and bottom of a window.
20759 Recenter the window whenever point gets within this many lines
20760 of the top or bottom of the window. */);
20761 scroll_margin = 0;
20762
20763 #if GLYPH_DEBUG
20764 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
20765 #endif
20766
20767 DEFVAR_BOOL ("truncate-partial-width-windows",
20768 &truncate_partial_width_windows,
20769 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
20770 truncate_partial_width_windows = 1;
20771
20772 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
20773 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
20774 Any other value means to use the appropriate face, `mode-line',
20775 `header-line', or `menu' respectively. */);
20776 mode_line_inverse_video = 1;
20777
20778 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
20779 doc: /* *Maximum buffer size for which line number should be displayed.
20780 If the buffer is bigger than this, the line number does not appear
20781 in the mode line. A value of nil means no limit. */);
20782 Vline_number_display_limit = Qnil;
20783
20784 DEFVAR_INT ("line-number-display-limit-width",
20785 &line_number_display_limit_width,
20786 doc: /* *Maximum line width (in characters) for line number display.
20787 If the average length of the lines near point is bigger than this, then the
20788 line number may be omitted from the mode line. */);
20789 line_number_display_limit_width = 200;
20790
20791 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
20792 doc: /* *Non-nil means highlight region even in nonselected windows. */);
20793 highlight_nonselected_windows = 0;
20794
20795 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
20796 doc: /* Non-nil if more than one frame is visible on this display.
20797 Minibuffer-only frames don't count, but iconified frames do.
20798 This variable is not guaranteed to be accurate except while processing
20799 `frame-title-format' and `icon-title-format'. */);
20800
20801 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
20802 doc: /* Template for displaying the title bar of visible frames.
20803 \(Assuming the window manager supports this feature.)
20804 This variable has the same structure as `mode-line-format' (which see),
20805 and is used only on frames for which no explicit name has been set
20806 \(see `modify-frame-parameters'). */);
20807 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
20808 doc: /* Template for displaying the title bar of an iconified frame.
20809 \(Assuming the window manager supports this feature.)
20810 This variable has the same structure as `mode-line-format' (which see),
20811 and is used only on frames for which no explicit name has been set
20812 \(see `modify-frame-parameters'). */);
20813 Vicon_title_format
20814 = Vframe_title_format
20815 = Fcons (intern ("multiple-frames"),
20816 Fcons (build_string ("%b"),
20817 Fcons (Fcons (empty_string,
20818 Fcons (intern ("invocation-name"),
20819 Fcons (build_string ("@"),
20820 Fcons (intern ("system-name"),
20821 Qnil)))),
20822 Qnil)));
20823
20824 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
20825 doc: /* Maximum number of lines to keep in the message log buffer.
20826 If nil, disable message logging. If t, log messages but don't truncate
20827 the buffer when it becomes large. */);
20828 Vmessage_log_max = make_number (50);
20829
20830 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
20831 doc: /* Functions called before redisplay, if window sizes have changed.
20832 The value should be a list of functions that take one argument.
20833 Just before redisplay, for each frame, if any of its windows have changed
20834 size since the last redisplay, or have been split or deleted,
20835 all the functions in the list are called, with the frame as argument. */);
20836 Vwindow_size_change_functions = Qnil;
20837
20838 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
20839 doc: /* List of Functions to call before redisplaying a window with scrolling.
20840 Each function is called with two arguments, the window
20841 and its new display-start position. Note that the value of `window-end'
20842 is not valid when these functions are called. */);
20843 Vwindow_scroll_functions = Qnil;
20844
20845 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
20846 doc: /* *Non-nil means autoselect window with mouse pointer. */);
20847 mouse_autoselect_window = 0;
20848
20849 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
20850 doc: /* *Non-nil means automatically resize tool-bars.
20851 This increases a tool-bar's height if not all tool-bar items are visible.
20852 It decreases a tool-bar's height when it would display blank lines
20853 otherwise. */);
20854 auto_resize_tool_bars_p = 1;
20855
20856 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
20857 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
20858 auto_raise_tool_bar_buttons_p = 1;
20859
20860 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
20861 doc: /* *Margin around tool-bar buttons in pixels.
20862 If an integer, use that for both horizontal and vertical margins.
20863 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
20864 HORZ specifying the horizontal margin, and VERT specifying the
20865 vertical margin. */);
20866 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
20867
20868 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
20869 doc: /* *Relief thickness of tool-bar buttons. */);
20870 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
20871
20872 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
20873 doc: /* List of functions to call to fontify regions of text.
20874 Each function is called with one argument POS. Functions must
20875 fontify a region starting at POS in the current buffer, and give
20876 fontified regions the property `fontified'. */);
20877 Vfontification_functions = Qnil;
20878 Fmake_variable_buffer_local (Qfontification_functions);
20879
20880 DEFVAR_BOOL ("unibyte-display-via-language-environment",
20881 &unibyte_display_via_language_environment,
20882 doc: /* *Non-nil means display unibyte text according to language environment.
20883 Specifically this means that unibyte non-ASCII characters
20884 are displayed by converting them to the equivalent multibyte characters
20885 according to the current language environment. As a result, they are
20886 displayed according to the current fontset. */);
20887 unibyte_display_via_language_environment = 0;
20888
20889 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
20890 doc: /* *Maximum height for resizing mini-windows.
20891 If a float, it specifies a fraction of the mini-window frame's height.
20892 If an integer, it specifies a number of lines. */);
20893 Vmax_mini_window_height = make_float (0.25);
20894
20895 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
20896 doc: /* *How to resize mini-windows.
20897 A value of nil means don't automatically resize mini-windows.
20898 A value of t means resize them to fit the text displayed in them.
20899 A value of `grow-only', the default, means let mini-windows grow
20900 only, until their display becomes empty, at which point the windows
20901 go back to their normal size. */);
20902 Vresize_mini_windows = Qgrow_only;
20903
20904 DEFVAR_LISP ("cursor-in-non-selected-windows",
20905 &Vcursor_in_non_selected_windows,
20906 doc: /* *Cursor type to display in non-selected windows.
20907 t means to use hollow box cursor. See `cursor-type' for other values. */);
20908 Vcursor_in_non_selected_windows = Qt;
20909
20910 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
20911 doc: /* Alist specifying how to blink the cursor off.
20912 Each element has the form (ON-STATE . OFF-STATE). Whenever the
20913 `cursor-type' frame-parameter or variable equals ON-STATE,
20914 comparing using `equal', Emacs uses OFF-STATE to specify
20915 how to blink it off. */);
20916 Vblink_cursor_alist = Qnil;
20917
20918 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
20919 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
20920 automatic_hscrolling_p = 1;
20921
20922 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
20923 doc: /* *How many columns away from the window edge point is allowed to get
20924 before automatic hscrolling will horizontally scroll the window. */);
20925 hscroll_margin = 5;
20926
20927 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
20928 doc: /* *How many columns to scroll the window when point gets too close to the edge.
20929 When point is less than `automatic-hscroll-margin' columns from the window
20930 edge, automatic hscrolling will scroll the window by the amount of columns
20931 determined by this variable. If its value is a positive integer, scroll that
20932 many columns. If it's a positive floating-point number, it specifies the
20933 fraction of the window's width to scroll. If it's nil or zero, point will be
20934 centered horizontally after the scroll. Any other value, including negative
20935 numbers, are treated as if the value were zero.
20936
20937 Automatic hscrolling always moves point outside the scroll margin, so if
20938 point was more than scroll step columns inside the margin, the window will
20939 scroll more than the value given by the scroll step.
20940
20941 Note that the lower bound for automatic hscrolling specified by `scroll-left'
20942 and `scroll-right' overrides this variable's effect. */);
20943 Vhscroll_step = make_number (0);
20944
20945 DEFVAR_LISP ("image-types", &Vimage_types,
20946 doc: /* List of supported image types.
20947 Each element of the list is a symbol for a supported image type. */);
20948 Vimage_types = Qnil;
20949
20950 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
20951 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
20952 Bind this around calls to `message' to let it take effect. */);
20953 message_truncate_lines = 0;
20954
20955 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
20956 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
20957 Can be used to update submenus whose contents should vary. */);
20958 Vmenu_bar_update_hook = Qnil;
20959
20960 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
20961 doc: /* Non-nil means don't update menu bars. Internal use only. */);
20962 inhibit_menubar_update = 0;
20963
20964 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
20965 doc: /* Non-nil means don't eval Lisp during redisplay. */);
20966 inhibit_eval_during_redisplay = 0;
20967
20968 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
20969 doc: /* Non-nil means don't free realized faces. Internal use only. */);
20970 inhibit_free_realized_faces = 0;
20971
20972 #if GLYPH_DEBUG
20973 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
20974 doc: /* Inhibit try_window_id display optimization. */);
20975 inhibit_try_window_id = 0;
20976
20977 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
20978 doc: /* Inhibit try_window_reusing display optimization. */);
20979 inhibit_try_window_reusing = 0;
20980
20981 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
20982 doc: /* Inhibit try_cursor_movement display optimization. */);
20983 inhibit_try_cursor_movement = 0;
20984 #endif /* GLYPH_DEBUG */
20985 }
20986
20987
20988 /* Initialize this module when Emacs starts. */
20989
20990 void
20991 init_xdisp ()
20992 {
20993 Lisp_Object root_window;
20994 struct window *mini_w;
20995
20996 current_header_line_height = current_mode_line_height = -1;
20997
20998 CHARPOS (this_line_start_pos) = 0;
20999
21000 mini_w = XWINDOW (minibuf_window);
21001 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
21002
21003 if (!noninteractive)
21004 {
21005 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
21006 int i;
21007
21008 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
21009 set_window_height (root_window,
21010 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
21011 0);
21012 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
21013 set_window_height (minibuf_window, 1, 0);
21014
21015 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
21016 mini_w->width = make_number (FRAME_WIDTH (f));
21017
21018 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
21019 scratch_glyph_row.glyphs[TEXT_AREA + 1]
21020 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
21021
21022 /* The default ellipsis glyphs `...'. */
21023 for (i = 0; i < 3; ++i)
21024 default_invis_vector[i] = make_number ('.');
21025 }
21026
21027 {
21028 /* Allocate the buffer for frame titles.
21029 Also used for `format-mode-line'. */
21030 int size = 100;
21031 frame_title_buf = (char *) xmalloc (size);
21032 frame_title_buf_end = frame_title_buf + size;
21033 frame_title_ptr = NULL;
21034 }
21035
21036 help_echo_showing_p = 0;
21037 }
21038
21039