Make (many) trivial substitutions for renamed and
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985,86,87,88,93,94,95,97,98,99,2000,01,02,03
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the description of direct update
37 operations, below.)
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay do? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
91 You will find a lot of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking an iterator structure (struct it)
124 argument.
125
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
131
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
138
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
146
147
148 Frame matrices.
149
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
156
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
168
169 #include <config.h>
170 #include <stdio.h>
171
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "keymap.h"
183 #include "macros.h"
184 #include "disptab.h"
185 #include "termhooks.h"
186 #include "intervals.h"
187 #include "coding.h"
188 #include "process.h"
189 #include "region-cache.h"
190 #include "fontset.h"
191 #include "blockinput.h"
192
193 #ifdef HAVE_X_WINDOWS
194 #include "xterm.h"
195 #endif
196 #ifdef WINDOWSNT
197 #include "w32term.h"
198 #endif
199 #ifdef MAC_OS
200 #include "macterm.h"
201
202 Cursor No_Cursor;
203 #endif
204
205 #ifndef FRAME_X_OUTPUT
206 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
207 #endif
208
209 #define INFINITY 10000000
210
211 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
212 || defined (USE_GTK)
213 extern void set_frame_menubar P_ ((struct frame *f, int, int));
214 extern int pending_menu_activation;
215 #endif
216
217 extern int interrupt_input;
218 extern int command_loop_level;
219
220 extern int minibuffer_auto_raise;
221 extern Lisp_Object Vminibuffer_list;
222
223 extern Lisp_Object Qface;
224 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
225
226 extern Lisp_Object Voverriding_local_map;
227 extern Lisp_Object Voverriding_local_map_menu_flag;
228 extern Lisp_Object Qmenu_item;
229 extern Lisp_Object Qwhen;
230 extern Lisp_Object Qhelp_echo;
231
232 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
233 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
234 Lisp_Object Qredisplay_end_trigger_functions;
235 Lisp_Object Qinhibit_point_motion_hooks;
236 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
237 Lisp_Object Qfontified;
238 Lisp_Object Qgrow_only;
239 Lisp_Object Qinhibit_eval_during_redisplay;
240 Lisp_Object Qbuffer_position, Qposition, Qobject;
241
242 /* Cursor shapes */
243 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
244
245 Lisp_Object Qrisky_local_variable;
246
247 /* Holds the list (error). */
248 Lisp_Object list_of_error;
249
250 /* Functions called to fontify regions of text. */
251
252 Lisp_Object Vfontification_functions;
253 Lisp_Object Qfontification_functions;
254
255 /* Non-zero means automatically select any window when the mouse
256 cursor moves into it. */
257 int mouse_autoselect_window;
258
259 /* Non-zero means draw tool bar buttons raised when the mouse moves
260 over them. */
261
262 int auto_raise_tool_bar_buttons_p;
263
264 /* Margin around tool bar buttons in pixels. */
265
266 Lisp_Object Vtool_bar_button_margin;
267
268 /* Thickness of shadow to draw around tool bar buttons. */
269
270 EMACS_INT tool_bar_button_relief;
271
272 /* Non-zero means automatically resize tool-bars so that all tool-bar
273 items are visible, and no blank lines remain. */
274
275 int auto_resize_tool_bars_p;
276
277 /* Non-zero means draw block and hollow cursor as wide as the glyph
278 under it. For example, if a block cursor is over a tab, it will be
279 drawn as wide as that tab on the display. */
280
281 int x_stretch_cursor_p;
282
283 /* Non-nil means don't actually do any redisplay. */
284
285 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
286
287 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
288
289 int inhibit_eval_during_redisplay;
290
291 /* Names of text properties relevant for redisplay. */
292
293 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
294 extern Lisp_Object Qface, Qinvisible, Qwidth;
295
296 /* Symbols used in text property values. */
297
298 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
299 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
300 Lisp_Object Qmargin;
301 extern Lisp_Object Qheight;
302 extern Lisp_Object QCwidth, QCheight, QCascent;
303
304 /* Non-nil means highlight trailing whitespace. */
305
306 Lisp_Object Vshow_trailing_whitespace;
307
308 /* Name of the face used to highlight trailing whitespace. */
309
310 Lisp_Object Qtrailing_whitespace;
311
312 /* The symbol `image' which is the car of the lists used to represent
313 images in Lisp. */
314
315 Lisp_Object Qimage;
316
317 /* Non-zero means print newline to stdout before next mini-buffer
318 message. */
319
320 int noninteractive_need_newline;
321
322 /* Non-zero means print newline to message log before next message. */
323
324 static int message_log_need_newline;
325
326 /* Three markers that message_dolog uses.
327 It could allocate them itself, but that causes trouble
328 in handling memory-full errors. */
329 static Lisp_Object message_dolog_marker1;
330 static Lisp_Object message_dolog_marker2;
331 static Lisp_Object message_dolog_marker3;
332 \f
333 /* The buffer position of the first character appearing entirely or
334 partially on the line of the selected window which contains the
335 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
336 redisplay optimization in redisplay_internal. */
337
338 static struct text_pos this_line_start_pos;
339
340 /* Number of characters past the end of the line above, including the
341 terminating newline. */
342
343 static struct text_pos this_line_end_pos;
344
345 /* The vertical positions and the height of this line. */
346
347 static int this_line_vpos;
348 static int this_line_y;
349 static int this_line_pixel_height;
350
351 /* X position at which this display line starts. Usually zero;
352 negative if first character is partially visible. */
353
354 static int this_line_start_x;
355
356 /* Buffer that this_line_.* variables are referring to. */
357
358 static struct buffer *this_line_buffer;
359
360 /* Nonzero means truncate lines in all windows less wide than the
361 frame. */
362
363 int truncate_partial_width_windows;
364
365 /* A flag to control how to display unibyte 8-bit character. */
366
367 int unibyte_display_via_language_environment;
368
369 /* Nonzero means we have more than one non-mini-buffer-only frame.
370 Not guaranteed to be accurate except while parsing
371 frame-title-format. */
372
373 int multiple_frames;
374
375 Lisp_Object Vglobal_mode_string;
376
377 /* Marker for where to display an arrow on top of the buffer text. */
378
379 Lisp_Object Voverlay_arrow_position;
380
381 /* String to display for the arrow. Only used on terminal frames. */
382
383 Lisp_Object Voverlay_arrow_string;
384
385 /* Values of those variables at last redisplay. However, if
386 Voverlay_arrow_position is a marker, last_arrow_position is its
387 numerical position. */
388
389 static Lisp_Object last_arrow_position, last_arrow_string;
390
391 /* Like mode-line-format, but for the title bar on a visible frame. */
392
393 Lisp_Object Vframe_title_format;
394
395 /* Like mode-line-format, but for the title bar on an iconified frame. */
396
397 Lisp_Object Vicon_title_format;
398
399 /* List of functions to call when a window's size changes. These
400 functions get one arg, a frame on which one or more windows' sizes
401 have changed. */
402
403 static Lisp_Object Vwindow_size_change_functions;
404
405 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
406
407 /* Nonzero if overlay arrow has been displayed once in this window. */
408
409 static int overlay_arrow_seen;
410
411 /* Nonzero means highlight the region even in nonselected windows. */
412
413 int highlight_nonselected_windows;
414
415 /* If cursor motion alone moves point off frame, try scrolling this
416 many lines up or down if that will bring it back. */
417
418 static EMACS_INT scroll_step;
419
420 /* Nonzero means scroll just far enough to bring point back on the
421 screen, when appropriate. */
422
423 static EMACS_INT scroll_conservatively;
424
425 /* Recenter the window whenever point gets within this many lines of
426 the top or bottom of the window. This value is translated into a
427 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
428 that there is really a fixed pixel height scroll margin. */
429
430 EMACS_INT scroll_margin;
431
432 /* Number of windows showing the buffer of the selected window (or
433 another buffer with the same base buffer). keyboard.c refers to
434 this. */
435
436 int buffer_shared;
437
438 /* Vector containing glyphs for an ellipsis `...'. */
439
440 static Lisp_Object default_invis_vector[3];
441
442 /* Zero means display the mode-line/header-line/menu-bar in the default face
443 (this slightly odd definition is for compatibility with previous versions
444 of emacs), non-zero means display them using their respective faces.
445
446 This variable is deprecated. */
447
448 int mode_line_inverse_video;
449
450 /* Prompt to display in front of the mini-buffer contents. */
451
452 Lisp_Object minibuf_prompt;
453
454 /* Width of current mini-buffer prompt. Only set after display_line
455 of the line that contains the prompt. */
456
457 int minibuf_prompt_width;
458
459 /* This is the window where the echo area message was displayed. It
460 is always a mini-buffer window, but it may not be the same window
461 currently active as a mini-buffer. */
462
463 Lisp_Object echo_area_window;
464
465 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
466 pushes the current message and the value of
467 message_enable_multibyte on the stack, the function restore_message
468 pops the stack and displays MESSAGE again. */
469
470 Lisp_Object Vmessage_stack;
471
472 /* Nonzero means multibyte characters were enabled when the echo area
473 message was specified. */
474
475 int message_enable_multibyte;
476
477 /* Nonzero if we should redraw the mode lines on the next redisplay. */
478
479 int update_mode_lines;
480
481 /* Nonzero if window sizes or contents have changed since last
482 redisplay that finished. */
483
484 int windows_or_buffers_changed;
485
486 /* Nonzero means a frame's cursor type has been changed. */
487
488 int cursor_type_changed;
489
490 /* Nonzero after display_mode_line if %l was used and it displayed a
491 line number. */
492
493 int line_number_displayed;
494
495 /* Maximum buffer size for which to display line numbers. */
496
497 Lisp_Object Vline_number_display_limit;
498
499 /* Line width to consider when repositioning for line number display. */
500
501 static EMACS_INT line_number_display_limit_width;
502
503 /* Number of lines to keep in the message log buffer. t means
504 infinite. nil means don't log at all. */
505
506 Lisp_Object Vmessage_log_max;
507
508 /* The name of the *Messages* buffer, a string. */
509
510 static Lisp_Object Vmessages_buffer_name;
511
512 /* Current, index 0, and last displayed echo area message. Either
513 buffers from echo_buffers, or nil to indicate no message. */
514
515 Lisp_Object echo_area_buffer[2];
516
517 /* The buffers referenced from echo_area_buffer. */
518
519 static Lisp_Object echo_buffer[2];
520
521 /* A vector saved used in with_area_buffer to reduce consing. */
522
523 static Lisp_Object Vwith_echo_area_save_vector;
524
525 /* Non-zero means display_echo_area should display the last echo area
526 message again. Set by redisplay_preserve_echo_area. */
527
528 static int display_last_displayed_message_p;
529
530 /* Nonzero if echo area is being used by print; zero if being used by
531 message. */
532
533 int message_buf_print;
534
535 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
536
537 Lisp_Object Qinhibit_menubar_update;
538 int inhibit_menubar_update;
539
540 /* Maximum height for resizing mini-windows. Either a float
541 specifying a fraction of the available height, or an integer
542 specifying a number of lines. */
543
544 Lisp_Object Vmax_mini_window_height;
545
546 /* Non-zero means messages should be displayed with truncated
547 lines instead of being continued. */
548
549 int message_truncate_lines;
550 Lisp_Object Qmessage_truncate_lines;
551
552 /* Set to 1 in clear_message to make redisplay_internal aware
553 of an emptied echo area. */
554
555 static int message_cleared_p;
556
557 /* Non-zero means we want a hollow cursor in windows that are not
558 selected. Zero means there's no cursor in such windows. */
559
560 Lisp_Object Vcursor_in_non_selected_windows;
561 Lisp_Object Qcursor_in_non_selected_windows;
562
563 /* How to blink the default frame cursor off. */
564 Lisp_Object Vblink_cursor_alist;
565
566 /* A scratch glyph row with contents used for generating truncation
567 glyphs. Also used in direct_output_for_insert. */
568
569 #define MAX_SCRATCH_GLYPHS 100
570 struct glyph_row scratch_glyph_row;
571 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
572
573 /* Ascent and height of the last line processed by move_it_to. */
574
575 static int last_max_ascent, last_height;
576
577 /* Non-zero if there's a help-echo in the echo area. */
578
579 int help_echo_showing_p;
580
581 /* If >= 0, computed, exact values of mode-line and header-line height
582 to use in the macros CURRENT_MODE_LINE_HEIGHT and
583 CURRENT_HEADER_LINE_HEIGHT. */
584
585 int current_mode_line_height, current_header_line_height;
586
587 /* The maximum distance to look ahead for text properties. Values
588 that are too small let us call compute_char_face and similar
589 functions too often which is expensive. Values that are too large
590 let us call compute_char_face and alike too often because we
591 might not be interested in text properties that far away. */
592
593 #define TEXT_PROP_DISTANCE_LIMIT 100
594
595 #if GLYPH_DEBUG
596
597 /* Variables to turn off display optimizations from Lisp. */
598
599 int inhibit_try_window_id, inhibit_try_window_reusing;
600 int inhibit_try_cursor_movement;
601
602 /* Non-zero means print traces of redisplay if compiled with
603 GLYPH_DEBUG != 0. */
604
605 int trace_redisplay_p;
606
607 #endif /* GLYPH_DEBUG */
608
609 #ifdef DEBUG_TRACE_MOVE
610 /* Non-zero means trace with TRACE_MOVE to stderr. */
611 int trace_move;
612
613 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
614 #else
615 #define TRACE_MOVE(x) (void) 0
616 #endif
617
618 /* Non-zero means automatically scroll windows horizontally to make
619 point visible. */
620
621 int automatic_hscrolling_p;
622
623 /* How close to the margin can point get before the window is scrolled
624 horizontally. */
625 EMACS_INT hscroll_margin;
626
627 /* How much to scroll horizontally when point is inside the above margin. */
628 Lisp_Object Vhscroll_step;
629
630 /* A list of symbols, one for each supported image type. */
631
632 Lisp_Object Vimage_types;
633
634 /* The variable `resize-mini-windows'. If nil, don't resize
635 mini-windows. If t, always resize them to fit the text they
636 display. If `grow-only', let mini-windows grow only until they
637 become empty. */
638
639 Lisp_Object Vresize_mini_windows;
640
641 /* Buffer being redisplayed -- for redisplay_window_error. */
642
643 struct buffer *displayed_buffer;
644
645 /* Value returned from text property handlers (see below). */
646
647 enum prop_handled
648 {
649 HANDLED_NORMALLY,
650 HANDLED_RECOMPUTE_PROPS,
651 HANDLED_OVERLAY_STRING_CONSUMED,
652 HANDLED_RETURN
653 };
654
655 /* A description of text properties that redisplay is interested
656 in. */
657
658 struct props
659 {
660 /* The name of the property. */
661 Lisp_Object *name;
662
663 /* A unique index for the property. */
664 enum prop_idx idx;
665
666 /* A handler function called to set up iterator IT from the property
667 at IT's current position. Value is used to steer handle_stop. */
668 enum prop_handled (*handler) P_ ((struct it *it));
669 };
670
671 static enum prop_handled handle_face_prop P_ ((struct it *));
672 static enum prop_handled handle_invisible_prop P_ ((struct it *));
673 static enum prop_handled handle_display_prop P_ ((struct it *));
674 static enum prop_handled handle_composition_prop P_ ((struct it *));
675 static enum prop_handled handle_overlay_change P_ ((struct it *));
676 static enum prop_handled handle_fontified_prop P_ ((struct it *));
677
678 /* Properties handled by iterators. */
679
680 static struct props it_props[] =
681 {
682 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
683 /* Handle `face' before `display' because some sub-properties of
684 `display' need to know the face. */
685 {&Qface, FACE_PROP_IDX, handle_face_prop},
686 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
687 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
688 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
689 {NULL, 0, NULL}
690 };
691
692 /* Value is the position described by X. If X is a marker, value is
693 the marker_position of X. Otherwise, value is X. */
694
695 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
696
697 /* Enumeration returned by some move_it_.* functions internally. */
698
699 enum move_it_result
700 {
701 /* Not used. Undefined value. */
702 MOVE_UNDEFINED,
703
704 /* Move ended at the requested buffer position or ZV. */
705 MOVE_POS_MATCH_OR_ZV,
706
707 /* Move ended at the requested X pixel position. */
708 MOVE_X_REACHED,
709
710 /* Move within a line ended at the end of a line that must be
711 continued. */
712 MOVE_LINE_CONTINUED,
713
714 /* Move within a line ended at the end of a line that would
715 be displayed truncated. */
716 MOVE_LINE_TRUNCATED,
717
718 /* Move within a line ended at a line end. */
719 MOVE_NEWLINE_OR_CR
720 };
721
722 /* This counter is used to clear the face cache every once in a while
723 in redisplay_internal. It is incremented for each redisplay.
724 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
725 cleared. */
726
727 #define CLEAR_FACE_CACHE_COUNT 500
728 static int clear_face_cache_count;
729
730 /* Record the previous terminal frame we displayed. */
731
732 static struct frame *previous_terminal_frame;
733
734 /* Non-zero while redisplay_internal is in progress. */
735
736 int redisplaying_p;
737
738 /* Non-zero means don't free realized faces. Bound while freeing
739 realized faces is dangerous because glyph matrices might still
740 reference them. */
741
742 int inhibit_free_realized_faces;
743 Lisp_Object Qinhibit_free_realized_faces;
744
745 /* If a string, XTread_socket generates an event to display that string.
746 (The display is done in read_char.) */
747
748 Lisp_Object help_echo_string;
749 Lisp_Object help_echo_window;
750 Lisp_Object help_echo_object;
751 int help_echo_pos;
752
753 /* Temporary variable for XTread_socket. */
754
755 Lisp_Object previous_help_echo_string;
756
757
758 \f
759 /* Function prototypes. */
760
761 static void setup_for_ellipsis P_ ((struct it *));
762 static void mark_window_display_accurate_1 P_ ((struct window *, int));
763 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
764 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
765 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
766 static int redisplay_mode_lines P_ ((Lisp_Object, int));
767 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
768
769 #if 0
770 static int invisible_text_between_p P_ ((struct it *, int, int));
771 #endif
772
773 static int next_element_from_ellipsis P_ ((struct it *));
774 static void pint2str P_ ((char *, int, int));
775 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
776 struct text_pos));
777 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
778 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
779 static void store_frame_title_char P_ ((char));
780 static int store_frame_title P_ ((const unsigned char *, int, int));
781 static void x_consider_frame_title P_ ((Lisp_Object));
782 static void handle_stop P_ ((struct it *));
783 static int tool_bar_lines_needed P_ ((struct frame *));
784 static int single_display_prop_intangible_p P_ ((Lisp_Object));
785 static void ensure_echo_area_buffers P_ ((void));
786 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
787 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
788 static int with_echo_area_buffer P_ ((struct window *, int,
789 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
790 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
791 static void clear_garbaged_frames P_ ((void));
792 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
793 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
794 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
795 static int display_echo_area P_ ((struct window *));
796 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
797 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
798 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
799 static int string_char_and_length P_ ((const unsigned char *, int, int *));
800 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
801 struct text_pos));
802 static int compute_window_start_on_continuation_line P_ ((struct window *));
803 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
804 static void insert_left_trunc_glyphs P_ ((struct it *));
805 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
806 static void extend_face_to_end_of_line P_ ((struct it *));
807 static int append_space P_ ((struct it *, int));
808 static int make_cursor_line_fully_visible P_ ((struct window *));
809 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
810 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
811 static int trailing_whitespace_p P_ ((int));
812 static int message_log_check_duplicate P_ ((int, int, int, int));
813 static void push_it P_ ((struct it *));
814 static void pop_it P_ ((struct it *));
815 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
816 static void redisplay_internal P_ ((int));
817 static int echo_area_display P_ ((int));
818 static void redisplay_windows P_ ((Lisp_Object));
819 static void redisplay_window P_ ((Lisp_Object, int));
820 static Lisp_Object redisplay_window_error ();
821 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
822 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
823 static void update_menu_bar P_ ((struct frame *, int));
824 static int try_window_reusing_current_matrix P_ ((struct window *));
825 static int try_window_id P_ ((struct window *));
826 static int display_line P_ ((struct it *));
827 static int display_mode_lines P_ ((struct window *));
828 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
829 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
830 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
831 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
832 static void display_menu_bar P_ ((struct window *));
833 static int display_count_lines P_ ((int, int, int, int, int *));
834 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
835 int, int, struct it *, int, int, int, int));
836 static void compute_line_metrics P_ ((struct it *));
837 static void run_redisplay_end_trigger_hook P_ ((struct it *));
838 static int get_overlay_strings P_ ((struct it *, int));
839 static void next_overlay_string P_ ((struct it *));
840 static void reseat P_ ((struct it *, struct text_pos, int));
841 static void reseat_1 P_ ((struct it *, struct text_pos, int));
842 static void back_to_previous_visible_line_start P_ ((struct it *));
843 static void reseat_at_previous_visible_line_start P_ ((struct it *));
844 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
845 static int next_element_from_display_vector P_ ((struct it *));
846 static int next_element_from_string P_ ((struct it *));
847 static int next_element_from_c_string P_ ((struct it *));
848 static int next_element_from_buffer P_ ((struct it *));
849 static int next_element_from_composition P_ ((struct it *));
850 static int next_element_from_image P_ ((struct it *));
851 static int next_element_from_stretch P_ ((struct it *));
852 static void load_overlay_strings P_ ((struct it *, int));
853 static int init_from_display_pos P_ ((struct it *, struct window *,
854 struct display_pos *));
855 static void reseat_to_string P_ ((struct it *, unsigned char *,
856 Lisp_Object, int, int, int, int));
857 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
858 int, int, int));
859 void move_it_vertically_backward P_ ((struct it *, int));
860 static void init_to_row_start P_ ((struct it *, struct window *,
861 struct glyph_row *));
862 static int init_to_row_end P_ ((struct it *, struct window *,
863 struct glyph_row *));
864 static void back_to_previous_line_start P_ ((struct it *));
865 static int forward_to_next_line_start P_ ((struct it *, int *));
866 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
867 Lisp_Object, int));
868 static struct text_pos string_pos P_ ((int, Lisp_Object));
869 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
870 static int number_of_chars P_ ((unsigned char *, int));
871 static void compute_stop_pos P_ ((struct it *));
872 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
873 Lisp_Object));
874 static int face_before_or_after_it_pos P_ ((struct it *, int));
875 static int next_overlay_change P_ ((int));
876 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
877 Lisp_Object, struct text_pos *,
878 int));
879 static int underlying_face_id P_ ((struct it *));
880 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
881 struct window *));
882
883 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
884 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
885
886 #ifdef HAVE_WINDOW_SYSTEM
887
888 static void update_tool_bar P_ ((struct frame *, int));
889 static void build_desired_tool_bar_string P_ ((struct frame *f));
890 static int redisplay_tool_bar P_ ((struct frame *));
891 static void display_tool_bar_line P_ ((struct it *));
892 static void notice_overwritten_cursor P_ ((struct window *,
893 enum glyph_row_area,
894 int, int, int, int));
895
896
897
898 #endif /* HAVE_WINDOW_SYSTEM */
899
900 \f
901 /***********************************************************************
902 Window display dimensions
903 ***********************************************************************/
904
905 /* Return the bottom boundary y-position for text lines in window W.
906 This is the first y position at which a line cannot start.
907 It is relative to the top of the window.
908
909 This is the height of W minus the height of a mode line, if any. */
910
911 INLINE int
912 window_text_bottom_y (w)
913 struct window *w;
914 {
915 struct frame *f = XFRAME (w->frame);
916 int height = WINDOW_TOTAL_HEIGHT (w);
917
918 if (WINDOW_WANTS_MODELINE_P (w))
919 height -= CURRENT_MODE_LINE_HEIGHT (w);
920 return height;
921 }
922
923 /* Return the pixel width of display area AREA of window W. AREA < 0
924 means return the total width of W, not including fringes to
925 the left and right of the window. */
926
927 INLINE int
928 window_box_width (w, area)
929 struct window *w;
930 int area;
931 {
932 int cols = XFASTINT (w->total_cols);
933 int pixels = 0;
934
935 if (!w->pseudo_window_p)
936 {
937 cols -= WINDOW_SCROLL_BAR_COLS (w);
938
939 if (area == TEXT_AREA)
940 {
941 if (INTEGERP (w->left_margin_cols))
942 cols -= XFASTINT (w->left_margin_cols);
943 if (INTEGERP (w->right_margin_cols))
944 cols -= XFASTINT (w->right_margin_cols);
945 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
946 }
947 else if (area == LEFT_MARGIN_AREA)
948 {
949 cols = (INTEGERP (w->left_margin_cols)
950 ? XFASTINT (w->left_margin_cols) : 0);
951 pixels = 0;
952 }
953 else if (area == RIGHT_MARGIN_AREA)
954 {
955 cols = (INTEGERP (w->right_margin_cols)
956 ? XFASTINT (w->right_margin_cols) : 0);
957 pixels = 0;
958 }
959 }
960
961 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
962 }
963
964
965 /* Return the pixel height of the display area of window W, not
966 including mode lines of W, if any. */
967
968 INLINE int
969 window_box_height (w)
970 struct window *w;
971 {
972 struct frame *f = XFRAME (w->frame);
973 int height = WINDOW_TOTAL_HEIGHT (w);
974
975 xassert (height >= 0);
976
977 /* Note: the code below that determines the mode-line/header-line
978 height is essentially the same as that contained in the macro
979 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
980 the appropriate glyph row has its `mode_line_p' flag set,
981 and if it doesn't, uses estimate_mode_line_height instead. */
982
983 if (WINDOW_WANTS_MODELINE_P (w))
984 {
985 struct glyph_row *ml_row
986 = (w->current_matrix && w->current_matrix->rows
987 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
988 : 0);
989 if (ml_row && ml_row->mode_line_p)
990 height -= ml_row->height;
991 else
992 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
993 }
994
995 if (WINDOW_WANTS_HEADER_LINE_P (w))
996 {
997 struct glyph_row *hl_row
998 = (w->current_matrix && w->current_matrix->rows
999 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1000 : 0);
1001 if (hl_row && hl_row->mode_line_p)
1002 height -= hl_row->height;
1003 else
1004 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1005 }
1006
1007 /* With a very small font and a mode-line that's taller than
1008 default, we might end up with a negative height. */
1009 return max (0, height);
1010 }
1011
1012 /* Return the window-relative coordinate of the left edge of display
1013 area AREA of window W. AREA < 0 means return the left edge of the
1014 whole window, to the right of the left fringe of W. */
1015
1016 INLINE int
1017 window_box_left_offset (w, area)
1018 struct window *w;
1019 int area;
1020 {
1021 int x;
1022
1023 if (w->pseudo_window_p)
1024 return 0;
1025
1026 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1027
1028 if (area == TEXT_AREA)
1029 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1030 + window_box_width (w, LEFT_MARGIN_AREA));
1031 else if (area == RIGHT_MARGIN_AREA)
1032 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1033 + window_box_width (w, LEFT_MARGIN_AREA)
1034 + window_box_width (w, TEXT_AREA)
1035 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1036 ? 0
1037 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1038 else if (area == LEFT_MARGIN_AREA
1039 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1040 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1041
1042 return x;
1043 }
1044
1045
1046 /* Return the window-relative coordinate of the right edge of display
1047 area AREA of window W. AREA < 0 means return the left edge of the
1048 whole window, to the left of the right fringe of W. */
1049
1050 INLINE int
1051 window_box_right_offset (w, area)
1052 struct window *w;
1053 int area;
1054 {
1055 return window_box_left_offset (w, area) + window_box_width (w, area);
1056 }
1057
1058 /* Return the frame-relative coordinate of the left edge of display
1059 area AREA of window W. AREA < 0 means return the left edge of the
1060 whole window, to the right of the left fringe of W. */
1061
1062 INLINE int
1063 window_box_left (w, area)
1064 struct window *w;
1065 int area;
1066 {
1067 struct frame *f = XFRAME (w->frame);
1068 int x;
1069
1070 if (w->pseudo_window_p)
1071 return FRAME_INTERNAL_BORDER_WIDTH (f);
1072
1073 x = (WINDOW_LEFT_EDGE_X (w)
1074 + window_box_left_offset (w, area));
1075
1076 return x;
1077 }
1078
1079
1080 /* Return the frame-relative coordinate of the right edge of display
1081 area AREA of window W. AREA < 0 means return the left edge of the
1082 whole window, to the left of the right fringe of W. */
1083
1084 INLINE int
1085 window_box_right (w, area)
1086 struct window *w;
1087 int area;
1088 {
1089 return window_box_left (w, area) + window_box_width (w, area);
1090 }
1091
1092 /* Get the bounding box of the display area AREA of window W, without
1093 mode lines, in frame-relative coordinates. AREA < 0 means the
1094 whole window, not including the left and right fringes of
1095 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1096 coordinates of the upper-left corner of the box. Return in
1097 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1098
1099 INLINE void
1100 window_box (w, area, box_x, box_y, box_width, box_height)
1101 struct window *w;
1102 int area;
1103 int *box_x, *box_y, *box_width, *box_height;
1104 {
1105 if (box_width)
1106 *box_width = window_box_width (w, area);
1107 if (box_height)
1108 *box_height = window_box_height (w);
1109 if (box_x)
1110 *box_x = window_box_left (w, area);
1111 if (box_y)
1112 {
1113 *box_y = WINDOW_TOP_EDGE_Y (w);
1114 if (WINDOW_WANTS_HEADER_LINE_P (w))
1115 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1116 }
1117 }
1118
1119
1120 /* Get the bounding box of the display area AREA of window W, without
1121 mode lines. AREA < 0 means the whole window, not including the
1122 left and right fringe of the window. Return in *TOP_LEFT_X
1123 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1124 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1125 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1126 box. */
1127
1128 INLINE void
1129 window_box_edges (w, area, top_left_x, top_left_y,
1130 bottom_right_x, bottom_right_y)
1131 struct window *w;
1132 int area;
1133 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1134 {
1135 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1136 bottom_right_y);
1137 *bottom_right_x += *top_left_x;
1138 *bottom_right_y += *top_left_y;
1139 }
1140
1141
1142 \f
1143 /***********************************************************************
1144 Utilities
1145 ***********************************************************************/
1146
1147 /* Return the bottom y-position of the line the iterator IT is in.
1148 This can modify IT's settings. */
1149
1150 int
1151 line_bottom_y (it)
1152 struct it *it;
1153 {
1154 int line_height = it->max_ascent + it->max_descent;
1155 int line_top_y = it->current_y;
1156
1157 if (line_height == 0)
1158 {
1159 if (last_height)
1160 line_height = last_height;
1161 else if (IT_CHARPOS (*it) < ZV)
1162 {
1163 move_it_by_lines (it, 1, 1);
1164 line_height = (it->max_ascent || it->max_descent
1165 ? it->max_ascent + it->max_descent
1166 : last_height);
1167 }
1168 else
1169 {
1170 struct glyph_row *row = it->glyph_row;
1171
1172 /* Use the default character height. */
1173 it->glyph_row = NULL;
1174 it->what = IT_CHARACTER;
1175 it->c = ' ';
1176 it->len = 1;
1177 PRODUCE_GLYPHS (it);
1178 line_height = it->ascent + it->descent;
1179 it->glyph_row = row;
1180 }
1181 }
1182
1183 return line_top_y + line_height;
1184 }
1185
1186
1187 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1188 1 if POS is visible and the line containing POS is fully visible.
1189 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1190 and header-lines heights. */
1191
1192 int
1193 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1194 struct window *w;
1195 int charpos, *fully, exact_mode_line_heights_p;
1196 {
1197 struct it it;
1198 struct text_pos top;
1199 int visible_p;
1200 struct buffer *old_buffer = NULL;
1201
1202 if (XBUFFER (w->buffer) != current_buffer)
1203 {
1204 old_buffer = current_buffer;
1205 set_buffer_internal_1 (XBUFFER (w->buffer));
1206 }
1207
1208 *fully = visible_p = 0;
1209 SET_TEXT_POS_FROM_MARKER (top, w->start);
1210
1211 /* Compute exact mode line heights, if requested. */
1212 if (exact_mode_line_heights_p)
1213 {
1214 if (WINDOW_WANTS_MODELINE_P (w))
1215 current_mode_line_height
1216 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1217 current_buffer->mode_line_format);
1218
1219 if (WINDOW_WANTS_HEADER_LINE_P (w))
1220 current_header_line_height
1221 = display_mode_line (w, HEADER_LINE_FACE_ID,
1222 current_buffer->header_line_format);
1223 }
1224
1225 start_display (&it, w, top);
1226 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1227 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1228
1229 /* Note that we may overshoot because of invisible text. */
1230 if (IT_CHARPOS (it) >= charpos)
1231 {
1232 int top_y = it.current_y;
1233 int bottom_y = line_bottom_y (&it);
1234 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1235
1236 if (top_y < window_top_y)
1237 visible_p = bottom_y > window_top_y;
1238 else if (top_y < it.last_visible_y)
1239 {
1240 visible_p = 1;
1241 *fully = bottom_y <= it.last_visible_y;
1242 }
1243 }
1244 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1245 {
1246 move_it_by_lines (&it, 1, 0);
1247 if (charpos < IT_CHARPOS (it))
1248 {
1249 visible_p = 1;
1250 *fully = 0;
1251 }
1252 }
1253
1254 if (old_buffer)
1255 set_buffer_internal_1 (old_buffer);
1256
1257 current_header_line_height = current_mode_line_height = -1;
1258 return visible_p;
1259 }
1260
1261
1262 /* Return the next character from STR which is MAXLEN bytes long.
1263 Return in *LEN the length of the character. This is like
1264 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1265 we find one, we return a `?', but with the length of the invalid
1266 character. */
1267
1268 static INLINE int
1269 string_char_and_length (str, maxlen, len)
1270 const unsigned char *str;
1271 int maxlen, *len;
1272 {
1273 int c;
1274
1275 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1276 if (!CHAR_VALID_P (c, 1))
1277 /* We may not change the length here because other places in Emacs
1278 don't use this function, i.e. they silently accept invalid
1279 characters. */
1280 c = '?';
1281
1282 return c;
1283 }
1284
1285
1286
1287 /* Given a position POS containing a valid character and byte position
1288 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1289
1290 static struct text_pos
1291 string_pos_nchars_ahead (pos, string, nchars)
1292 struct text_pos pos;
1293 Lisp_Object string;
1294 int nchars;
1295 {
1296 xassert (STRINGP (string) && nchars >= 0);
1297
1298 if (STRING_MULTIBYTE (string))
1299 {
1300 int rest = SBYTES (string) - BYTEPOS (pos);
1301 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1302 int len;
1303
1304 while (nchars--)
1305 {
1306 string_char_and_length (p, rest, &len);
1307 p += len, rest -= len;
1308 xassert (rest >= 0);
1309 CHARPOS (pos) += 1;
1310 BYTEPOS (pos) += len;
1311 }
1312 }
1313 else
1314 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1315
1316 return pos;
1317 }
1318
1319
1320 /* Value is the text position, i.e. character and byte position,
1321 for character position CHARPOS in STRING. */
1322
1323 static INLINE struct text_pos
1324 string_pos (charpos, string)
1325 int charpos;
1326 Lisp_Object string;
1327 {
1328 struct text_pos pos;
1329 xassert (STRINGP (string));
1330 xassert (charpos >= 0);
1331 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1332 return pos;
1333 }
1334
1335
1336 /* Value is a text position, i.e. character and byte position, for
1337 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1338 means recognize multibyte characters. */
1339
1340 static struct text_pos
1341 c_string_pos (charpos, s, multibyte_p)
1342 int charpos;
1343 unsigned char *s;
1344 int multibyte_p;
1345 {
1346 struct text_pos pos;
1347
1348 xassert (s != NULL);
1349 xassert (charpos >= 0);
1350
1351 if (multibyte_p)
1352 {
1353 int rest = strlen (s), len;
1354
1355 SET_TEXT_POS (pos, 0, 0);
1356 while (charpos--)
1357 {
1358 string_char_and_length (s, rest, &len);
1359 s += len, rest -= len;
1360 xassert (rest >= 0);
1361 CHARPOS (pos) += 1;
1362 BYTEPOS (pos) += len;
1363 }
1364 }
1365 else
1366 SET_TEXT_POS (pos, charpos, charpos);
1367
1368 return pos;
1369 }
1370
1371
1372 /* Value is the number of characters in C string S. MULTIBYTE_P
1373 non-zero means recognize multibyte characters. */
1374
1375 static int
1376 number_of_chars (s, multibyte_p)
1377 unsigned char *s;
1378 int multibyte_p;
1379 {
1380 int nchars;
1381
1382 if (multibyte_p)
1383 {
1384 int rest = strlen (s), len;
1385 unsigned char *p = (unsigned char *) s;
1386
1387 for (nchars = 0; rest > 0; ++nchars)
1388 {
1389 string_char_and_length (p, rest, &len);
1390 rest -= len, p += len;
1391 }
1392 }
1393 else
1394 nchars = strlen (s);
1395
1396 return nchars;
1397 }
1398
1399
1400 /* Compute byte position NEWPOS->bytepos corresponding to
1401 NEWPOS->charpos. POS is a known position in string STRING.
1402 NEWPOS->charpos must be >= POS.charpos. */
1403
1404 static void
1405 compute_string_pos (newpos, pos, string)
1406 struct text_pos *newpos, pos;
1407 Lisp_Object string;
1408 {
1409 xassert (STRINGP (string));
1410 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1411
1412 if (STRING_MULTIBYTE (string))
1413 *newpos = string_pos_nchars_ahead (pos, string,
1414 CHARPOS (*newpos) - CHARPOS (pos));
1415 else
1416 BYTEPOS (*newpos) = CHARPOS (*newpos);
1417 }
1418
1419 /* EXPORT:
1420 Return an estimation of the pixel height of mode or top lines on
1421 frame F. FACE_ID specifies what line's height to estimate. */
1422
1423 int
1424 estimate_mode_line_height (f, face_id)
1425 struct frame *f;
1426 enum face_id face_id;
1427 {
1428 #ifdef HAVE_WINDOW_SYSTEM
1429 if (FRAME_WINDOW_P (f))
1430 {
1431 int height = FONT_HEIGHT (FRAME_FONT (f));
1432
1433 /* This function is called so early when Emacs starts that the face
1434 cache and mode line face are not yet initialized. */
1435 if (FRAME_FACE_CACHE (f))
1436 {
1437 struct face *face = FACE_FROM_ID (f, face_id);
1438 if (face)
1439 {
1440 if (face->font)
1441 height = FONT_HEIGHT (face->font);
1442 if (face->box_line_width > 0)
1443 height += 2 * face->box_line_width;
1444 }
1445 }
1446
1447 return height;
1448 }
1449 #endif
1450
1451 return 1;
1452 }
1453
1454 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1455 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1456 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1457 not force the value into range. */
1458
1459 void
1460 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1461 FRAME_PTR f;
1462 register int pix_x, pix_y;
1463 int *x, *y;
1464 NativeRectangle *bounds;
1465 int noclip;
1466 {
1467
1468 #ifdef HAVE_WINDOW_SYSTEM
1469 if (FRAME_WINDOW_P (f))
1470 {
1471 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1472 even for negative values. */
1473 if (pix_x < 0)
1474 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1475 if (pix_y < 0)
1476 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1477
1478 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1479 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1480
1481 if (bounds)
1482 STORE_NATIVE_RECT (*bounds,
1483 FRAME_COL_TO_PIXEL_X (f, pix_x),
1484 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1485 FRAME_COLUMN_WIDTH (f) - 1,
1486 FRAME_LINE_HEIGHT (f) - 1);
1487
1488 if (!noclip)
1489 {
1490 if (pix_x < 0)
1491 pix_x = 0;
1492 else if (pix_x > FRAME_TOTAL_COLS (f))
1493 pix_x = FRAME_TOTAL_COLS (f);
1494
1495 if (pix_y < 0)
1496 pix_y = 0;
1497 else if (pix_y > FRAME_LINES (f))
1498 pix_y = FRAME_LINES (f);
1499 }
1500 }
1501 #endif
1502
1503 *x = pix_x;
1504 *y = pix_y;
1505 }
1506
1507
1508 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1509 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1510 can't tell the positions because W's display is not up to date,
1511 return 0. */
1512
1513 int
1514 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1515 struct window *w;
1516 int hpos, vpos;
1517 int *frame_x, *frame_y;
1518 {
1519 #ifdef HAVE_WINDOW_SYSTEM
1520 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1521 {
1522 int success_p;
1523
1524 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1525 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1526
1527 if (display_completed)
1528 {
1529 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1530 struct glyph *glyph = row->glyphs[TEXT_AREA];
1531 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1532
1533 hpos = row->x;
1534 vpos = row->y;
1535 while (glyph < end)
1536 {
1537 hpos += glyph->pixel_width;
1538 ++glyph;
1539 }
1540
1541 success_p = 1;
1542 }
1543 else
1544 {
1545 hpos = vpos = 0;
1546 success_p = 0;
1547 }
1548
1549 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1550 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1551 return success_p;
1552 }
1553 #endif
1554
1555 *frame_x = hpos;
1556 *frame_y = vpos;
1557 return 1;
1558 }
1559
1560
1561 #ifdef HAVE_WINDOW_SYSTEM
1562
1563 /* Find the glyph under window-relative coordinates X/Y in window W.
1564 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1565 strings. Return in *HPOS and *VPOS the row and column number of
1566 the glyph found. Return in *AREA the glyph area containing X.
1567 Value is a pointer to the glyph found or null if X/Y is not on
1568 text, or we can't tell because W's current matrix is not up to
1569 date. */
1570
1571 static struct glyph *
1572 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area, buffer_only_p)
1573 struct window *w;
1574 int x, y;
1575 int *hpos, *vpos, *area;
1576 int buffer_only_p;
1577 {
1578 struct glyph *glyph, *end;
1579 struct glyph_row *row = NULL;
1580 int x0, i;
1581
1582 /* Find row containing Y. Give up if some row is not enabled. */
1583 for (i = 0; i < w->current_matrix->nrows; ++i)
1584 {
1585 row = MATRIX_ROW (w->current_matrix, i);
1586 if (!row->enabled_p)
1587 return NULL;
1588 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1589 break;
1590 }
1591
1592 *vpos = i;
1593 *hpos = 0;
1594
1595 /* Give up if Y is not in the window. */
1596 if (i == w->current_matrix->nrows)
1597 return NULL;
1598
1599 /* Get the glyph area containing X. */
1600 if (w->pseudo_window_p)
1601 {
1602 *area = TEXT_AREA;
1603 x0 = 0;
1604 }
1605 else
1606 {
1607 if (x < window_box_left_offset (w, TEXT_AREA))
1608 {
1609 *area = LEFT_MARGIN_AREA;
1610 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1611 }
1612 else if (x < window_box_right_offset (w, TEXT_AREA))
1613 {
1614 *area = TEXT_AREA;
1615 x0 = window_box_left_offset (w, TEXT_AREA);
1616 }
1617 else
1618 {
1619 *area = RIGHT_MARGIN_AREA;
1620 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1621 }
1622 }
1623
1624 /* Find glyph containing X. */
1625 glyph = row->glyphs[*area];
1626 end = glyph + row->used[*area];
1627 while (glyph < end)
1628 {
1629 if (x < x0 + glyph->pixel_width)
1630 {
1631 if (w->pseudo_window_p)
1632 break;
1633 else if (!buffer_only_p || BUFFERP (glyph->object))
1634 break;
1635 }
1636
1637 x0 += glyph->pixel_width;
1638 ++glyph;
1639 }
1640
1641 if (glyph == end)
1642 return NULL;
1643
1644 *hpos = glyph - row->glyphs[*area];
1645 return glyph;
1646 }
1647
1648
1649 /* EXPORT:
1650 Convert frame-relative x/y to coordinates relative to window W.
1651 Takes pseudo-windows into account. */
1652
1653 void
1654 frame_to_window_pixel_xy (w, x, y)
1655 struct window *w;
1656 int *x, *y;
1657 {
1658 if (w->pseudo_window_p)
1659 {
1660 /* A pseudo-window is always full-width, and starts at the
1661 left edge of the frame, plus a frame border. */
1662 struct frame *f = XFRAME (w->frame);
1663 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1664 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1665 }
1666 else
1667 {
1668 *x -= WINDOW_LEFT_EDGE_X (w);
1669 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1670 }
1671 }
1672
1673 /* EXPORT:
1674 Return in *R the clipping rectangle for glyph string S. */
1675
1676 void
1677 get_glyph_string_clip_rect (s, nr)
1678 struct glyph_string *s;
1679 NativeRectangle *nr;
1680 {
1681 XRectangle r;
1682
1683 if (s->row->full_width_p)
1684 {
1685 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1686 r.x = WINDOW_LEFT_EDGE_X (s->w);
1687 r.width = WINDOW_TOTAL_WIDTH (s->w);
1688
1689 /* Unless displaying a mode or menu bar line, which are always
1690 fully visible, clip to the visible part of the row. */
1691 if (s->w->pseudo_window_p)
1692 r.height = s->row->visible_height;
1693 else
1694 r.height = s->height;
1695 }
1696 else
1697 {
1698 /* This is a text line that may be partially visible. */
1699 r.x = window_box_left (s->w, s->area);
1700 r.width = window_box_width (s->w, s->area);
1701 r.height = s->row->visible_height;
1702 }
1703
1704 /* If S draws overlapping rows, it's sufficient to use the top and
1705 bottom of the window for clipping because this glyph string
1706 intentionally draws over other lines. */
1707 if (s->for_overlaps_p)
1708 {
1709 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1710 r.height = window_text_bottom_y (s->w) - r.y;
1711 }
1712 else
1713 {
1714 /* Don't use S->y for clipping because it doesn't take partially
1715 visible lines into account. For example, it can be negative for
1716 partially visible lines at the top of a window. */
1717 if (!s->row->full_width_p
1718 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1719 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1720 else
1721 r.y = max (0, s->row->y);
1722
1723 /* If drawing a tool-bar window, draw it over the internal border
1724 at the top of the window. */
1725 if (s->w == XWINDOW (s->f->tool_bar_window))
1726 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1727 }
1728
1729 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1730
1731 #ifdef HAVE_NTGUI
1732 /* ++KFS: From W32 port, but it looks ok for all platforms to me. */
1733 /* If drawing the cursor, don't let glyph draw outside its
1734 advertised boundaries. Cleartype does this under some circumstances. */
1735 if (s->hl == DRAW_CURSOR)
1736 {
1737 if (s->x > r.x)
1738 {
1739 r.width -= s->x - r.x;
1740 r.x = s->x;
1741 }
1742 r.width = min (r.width, s->first_glyph->pixel_width);
1743 }
1744 #endif
1745
1746 #ifdef CONVERT_FROM_XRECT
1747 CONVERT_FROM_XRECT (r, *nr);
1748 #else
1749 *nr = r;
1750 #endif
1751 }
1752
1753 #endif /* HAVE_WINDOW_SYSTEM */
1754
1755 \f
1756 /***********************************************************************
1757 Lisp form evaluation
1758 ***********************************************************************/
1759
1760 /* Error handler for safe_eval and safe_call. */
1761
1762 static Lisp_Object
1763 safe_eval_handler (arg)
1764 Lisp_Object arg;
1765 {
1766 add_to_log ("Error during redisplay: %s", arg, Qnil);
1767 return Qnil;
1768 }
1769
1770
1771 /* Evaluate SEXPR and return the result, or nil if something went
1772 wrong. Prevent redisplay during the evaluation. */
1773
1774 Lisp_Object
1775 safe_eval (sexpr)
1776 Lisp_Object sexpr;
1777 {
1778 Lisp_Object val;
1779
1780 if (inhibit_eval_during_redisplay)
1781 val = Qnil;
1782 else
1783 {
1784 int count = SPECPDL_INDEX ();
1785 struct gcpro gcpro1;
1786
1787 GCPRO1 (sexpr);
1788 specbind (Qinhibit_redisplay, Qt);
1789 /* Use Qt to ensure debugger does not run,
1790 so there is no possibility of wanting to redisplay. */
1791 val = internal_condition_case_1 (Feval, sexpr, Qt,
1792 safe_eval_handler);
1793 UNGCPRO;
1794 val = unbind_to (count, val);
1795 }
1796
1797 return val;
1798 }
1799
1800
1801 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1802 Return the result, or nil if something went wrong. Prevent
1803 redisplay during the evaluation. */
1804
1805 Lisp_Object
1806 safe_call (nargs, args)
1807 int nargs;
1808 Lisp_Object *args;
1809 {
1810 Lisp_Object val;
1811
1812 if (inhibit_eval_during_redisplay)
1813 val = Qnil;
1814 else
1815 {
1816 int count = SPECPDL_INDEX ();
1817 struct gcpro gcpro1;
1818
1819 GCPRO1 (args[0]);
1820 gcpro1.nvars = nargs;
1821 specbind (Qinhibit_redisplay, Qt);
1822 /* Use Qt to ensure debugger does not run,
1823 so there is no possibility of wanting to redisplay. */
1824 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1825 safe_eval_handler);
1826 UNGCPRO;
1827 val = unbind_to (count, val);
1828 }
1829
1830 return val;
1831 }
1832
1833
1834 /* Call function FN with one argument ARG.
1835 Return the result, or nil if something went wrong. */
1836
1837 Lisp_Object
1838 safe_call1 (fn, arg)
1839 Lisp_Object fn, arg;
1840 {
1841 Lisp_Object args[2];
1842 args[0] = fn;
1843 args[1] = arg;
1844 return safe_call (2, args);
1845 }
1846
1847
1848 \f
1849 /***********************************************************************
1850 Debugging
1851 ***********************************************************************/
1852
1853 #if 0
1854
1855 /* Define CHECK_IT to perform sanity checks on iterators.
1856 This is for debugging. It is too slow to do unconditionally. */
1857
1858 static void
1859 check_it (it)
1860 struct it *it;
1861 {
1862 if (it->method == next_element_from_string)
1863 {
1864 xassert (STRINGP (it->string));
1865 xassert (IT_STRING_CHARPOS (*it) >= 0);
1866 }
1867 else if (it->method == next_element_from_buffer)
1868 {
1869 /* Check that character and byte positions agree. */
1870 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1871 }
1872
1873 if (it->dpvec)
1874 xassert (it->current.dpvec_index >= 0);
1875 else
1876 xassert (it->current.dpvec_index < 0);
1877 }
1878
1879 #define CHECK_IT(IT) check_it ((IT))
1880
1881 #else /* not 0 */
1882
1883 #define CHECK_IT(IT) (void) 0
1884
1885 #endif /* not 0 */
1886
1887
1888 #if GLYPH_DEBUG
1889
1890 /* Check that the window end of window W is what we expect it
1891 to be---the last row in the current matrix displaying text. */
1892
1893 static void
1894 check_window_end (w)
1895 struct window *w;
1896 {
1897 if (!MINI_WINDOW_P (w)
1898 && !NILP (w->window_end_valid))
1899 {
1900 struct glyph_row *row;
1901 xassert ((row = MATRIX_ROW (w->current_matrix,
1902 XFASTINT (w->window_end_vpos)),
1903 !row->enabled_p
1904 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1905 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1906 }
1907 }
1908
1909 #define CHECK_WINDOW_END(W) check_window_end ((W))
1910
1911 #else /* not GLYPH_DEBUG */
1912
1913 #define CHECK_WINDOW_END(W) (void) 0
1914
1915 #endif /* not GLYPH_DEBUG */
1916
1917
1918 \f
1919 /***********************************************************************
1920 Iterator initialization
1921 ***********************************************************************/
1922
1923 /* Initialize IT for displaying current_buffer in window W, starting
1924 at character position CHARPOS. CHARPOS < 0 means that no buffer
1925 position is specified which is useful when the iterator is assigned
1926 a position later. BYTEPOS is the byte position corresponding to
1927 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
1928
1929 If ROW is not null, calls to produce_glyphs with IT as parameter
1930 will produce glyphs in that row.
1931
1932 BASE_FACE_ID is the id of a base face to use. It must be one of
1933 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1934 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1935 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
1936
1937 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1938 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1939 will be initialized to use the corresponding mode line glyph row of
1940 the desired matrix of W. */
1941
1942 void
1943 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1944 struct it *it;
1945 struct window *w;
1946 int charpos, bytepos;
1947 struct glyph_row *row;
1948 enum face_id base_face_id;
1949 {
1950 int highlight_region_p;
1951
1952 /* Some precondition checks. */
1953 xassert (w != NULL && it != NULL);
1954 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1955 && charpos <= ZV));
1956
1957 /* If face attributes have been changed since the last redisplay,
1958 free realized faces now because they depend on face definitions
1959 that might have changed. Don't free faces while there might be
1960 desired matrices pending which reference these faces. */
1961 if (face_change_count && !inhibit_free_realized_faces)
1962 {
1963 face_change_count = 0;
1964 free_all_realized_faces (Qnil);
1965 }
1966
1967 /* Use one of the mode line rows of W's desired matrix if
1968 appropriate. */
1969 if (row == NULL)
1970 {
1971 if (base_face_id == MODE_LINE_FACE_ID
1972 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
1973 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1974 else if (base_face_id == HEADER_LINE_FACE_ID)
1975 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1976 }
1977
1978 /* Clear IT. */
1979 bzero (it, sizeof *it);
1980 it->current.overlay_string_index = -1;
1981 it->current.dpvec_index = -1;
1982 it->base_face_id = base_face_id;
1983
1984 /* The window in which we iterate over current_buffer: */
1985 XSETWINDOW (it->window, w);
1986 it->w = w;
1987 it->f = XFRAME (w->frame);
1988
1989 /* Extra space between lines (on window systems only). */
1990 if (base_face_id == DEFAULT_FACE_ID
1991 && FRAME_WINDOW_P (it->f))
1992 {
1993 if (NATNUMP (current_buffer->extra_line_spacing))
1994 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1995 else if (it->f->extra_line_spacing > 0)
1996 it->extra_line_spacing = it->f->extra_line_spacing;
1997 }
1998
1999 /* If realized faces have been removed, e.g. because of face
2000 attribute changes of named faces, recompute them. When running
2001 in batch mode, the face cache of Vterminal_frame is null. If
2002 we happen to get called, make a dummy face cache. */
2003 if (
2004 #ifndef WINDOWSNT
2005 noninteractive &&
2006 #endif
2007 FRAME_FACE_CACHE (it->f) == NULL)
2008 init_frame_faces (it->f);
2009 if (FRAME_FACE_CACHE (it->f)->used == 0)
2010 recompute_basic_faces (it->f);
2011
2012 /* Current value of the `space-width', and 'height' properties. */
2013 it->space_width = Qnil;
2014 it->font_height = Qnil;
2015
2016 /* Are control characters displayed as `^C'? */
2017 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2018
2019 /* -1 means everything between a CR and the following line end
2020 is invisible. >0 means lines indented more than this value are
2021 invisible. */
2022 it->selective = (INTEGERP (current_buffer->selective_display)
2023 ? XFASTINT (current_buffer->selective_display)
2024 : (!NILP (current_buffer->selective_display)
2025 ? -1 : 0));
2026 it->selective_display_ellipsis_p
2027 = !NILP (current_buffer->selective_display_ellipses);
2028
2029 /* Display table to use. */
2030 it->dp = window_display_table (w);
2031
2032 /* Are multibyte characters enabled in current_buffer? */
2033 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2034
2035 /* Non-zero if we should highlight the region. */
2036 highlight_region_p
2037 = (!NILP (Vtransient_mark_mode)
2038 && !NILP (current_buffer->mark_active)
2039 && XMARKER (current_buffer->mark)->buffer != 0);
2040
2041 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2042 start and end of a visible region in window IT->w. Set both to
2043 -1 to indicate no region. */
2044 if (highlight_region_p
2045 /* Maybe highlight only in selected window. */
2046 && (/* Either show region everywhere. */
2047 highlight_nonselected_windows
2048 /* Or show region in the selected window. */
2049 || w == XWINDOW (selected_window)
2050 /* Or show the region if we are in the mini-buffer and W is
2051 the window the mini-buffer refers to. */
2052 || (MINI_WINDOW_P (XWINDOW (selected_window))
2053 && WINDOWP (minibuf_selected_window)
2054 && w == XWINDOW (minibuf_selected_window))))
2055 {
2056 int charpos = marker_position (current_buffer->mark);
2057 it->region_beg_charpos = min (PT, charpos);
2058 it->region_end_charpos = max (PT, charpos);
2059 }
2060 else
2061 it->region_beg_charpos = it->region_end_charpos = -1;
2062
2063 /* Get the position at which the redisplay_end_trigger hook should
2064 be run, if it is to be run at all. */
2065 if (MARKERP (w->redisplay_end_trigger)
2066 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2067 it->redisplay_end_trigger_charpos
2068 = marker_position (w->redisplay_end_trigger);
2069 else if (INTEGERP (w->redisplay_end_trigger))
2070 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2071
2072 /* Correct bogus values of tab_width. */
2073 it->tab_width = XINT (current_buffer->tab_width);
2074 if (it->tab_width <= 0 || it->tab_width > 1000)
2075 it->tab_width = 8;
2076
2077 /* Are lines in the display truncated? */
2078 it->truncate_lines_p
2079 = (base_face_id != DEFAULT_FACE_ID
2080 || XINT (it->w->hscroll)
2081 || (truncate_partial_width_windows
2082 && !WINDOW_FULL_WIDTH_P (it->w))
2083 || !NILP (current_buffer->truncate_lines));
2084
2085 /* Get dimensions of truncation and continuation glyphs. These are
2086 displayed as fringe bitmaps under X, so we don't need them for such
2087 frames. */
2088 if (!FRAME_WINDOW_P (it->f))
2089 {
2090 if (it->truncate_lines_p)
2091 {
2092 /* We will need the truncation glyph. */
2093 xassert (it->glyph_row == NULL);
2094 produce_special_glyphs (it, IT_TRUNCATION);
2095 it->truncation_pixel_width = it->pixel_width;
2096 }
2097 else
2098 {
2099 /* We will need the continuation glyph. */
2100 xassert (it->glyph_row == NULL);
2101 produce_special_glyphs (it, IT_CONTINUATION);
2102 it->continuation_pixel_width = it->pixel_width;
2103 }
2104
2105 /* Reset these values to zero because the produce_special_glyphs
2106 above has changed them. */
2107 it->pixel_width = it->ascent = it->descent = 0;
2108 it->phys_ascent = it->phys_descent = 0;
2109 }
2110
2111 /* Set this after getting the dimensions of truncation and
2112 continuation glyphs, so that we don't produce glyphs when calling
2113 produce_special_glyphs, above. */
2114 it->glyph_row = row;
2115 it->area = TEXT_AREA;
2116
2117 /* Get the dimensions of the display area. The display area
2118 consists of the visible window area plus a horizontally scrolled
2119 part to the left of the window. All x-values are relative to the
2120 start of this total display area. */
2121 if (base_face_id != DEFAULT_FACE_ID)
2122 {
2123 /* Mode lines, menu bar in terminal frames. */
2124 it->first_visible_x = 0;
2125 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2126 }
2127 else
2128 {
2129 it->first_visible_x
2130 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2131 it->last_visible_x = (it->first_visible_x
2132 + window_box_width (w, TEXT_AREA));
2133
2134 /* If we truncate lines, leave room for the truncator glyph(s) at
2135 the right margin. Otherwise, leave room for the continuation
2136 glyph(s). Truncation and continuation glyphs are not inserted
2137 for window-based redisplay. */
2138 if (!FRAME_WINDOW_P (it->f))
2139 {
2140 if (it->truncate_lines_p)
2141 it->last_visible_x -= it->truncation_pixel_width;
2142 else
2143 it->last_visible_x -= it->continuation_pixel_width;
2144 }
2145
2146 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2147 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2148 }
2149
2150 /* Leave room for a border glyph. */
2151 if (!FRAME_WINDOW_P (it->f)
2152 && !WINDOW_RIGHTMOST_P (it->w))
2153 it->last_visible_x -= 1;
2154
2155 it->last_visible_y = window_text_bottom_y (w);
2156
2157 /* For mode lines and alike, arrange for the first glyph having a
2158 left box line if the face specifies a box. */
2159 if (base_face_id != DEFAULT_FACE_ID)
2160 {
2161 struct face *face;
2162
2163 it->face_id = base_face_id;
2164
2165 /* If we have a boxed mode line, make the first character appear
2166 with a left box line. */
2167 face = FACE_FROM_ID (it->f, base_face_id);
2168 if (face->box != FACE_NO_BOX)
2169 it->start_of_box_run_p = 1;
2170 }
2171
2172 /* If a buffer position was specified, set the iterator there,
2173 getting overlays and face properties from that position. */
2174 if (charpos >= BUF_BEG (current_buffer))
2175 {
2176 it->end_charpos = ZV;
2177 it->face_id = -1;
2178 IT_CHARPOS (*it) = charpos;
2179
2180 /* Compute byte position if not specified. */
2181 if (bytepos < charpos)
2182 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2183 else
2184 IT_BYTEPOS (*it) = bytepos;
2185
2186 /* Compute faces etc. */
2187 reseat (it, it->current.pos, 1);
2188 }
2189
2190 CHECK_IT (it);
2191 }
2192
2193
2194 /* Initialize IT for the display of window W with window start POS. */
2195
2196 void
2197 start_display (it, w, pos)
2198 struct it *it;
2199 struct window *w;
2200 struct text_pos pos;
2201 {
2202 struct glyph_row *row;
2203 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2204
2205 row = w->desired_matrix->rows + first_vpos;
2206 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2207
2208 if (!it->truncate_lines_p)
2209 {
2210 int start_at_line_beg_p;
2211 int first_y = it->current_y;
2212
2213 /* If window start is not at a line start, skip forward to POS to
2214 get the correct continuation lines width. */
2215 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2216 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2217 if (!start_at_line_beg_p)
2218 {
2219 int new_x;
2220
2221 reseat_at_previous_visible_line_start (it);
2222 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2223
2224 new_x = it->current_x + it->pixel_width;
2225
2226 /* If lines are continued, this line may end in the middle
2227 of a multi-glyph character (e.g. a control character
2228 displayed as \003, or in the middle of an overlay
2229 string). In this case move_it_to above will not have
2230 taken us to the start of the continuation line but to the
2231 end of the continued line. */
2232 if (it->current_x > 0
2233 && !it->truncate_lines_p /* Lines are continued. */
2234 && (/* And glyph doesn't fit on the line. */
2235 new_x > it->last_visible_x
2236 /* Or it fits exactly and we're on a window
2237 system frame. */
2238 || (new_x == it->last_visible_x
2239 && FRAME_WINDOW_P (it->f))))
2240 {
2241 if (it->current.dpvec_index >= 0
2242 || it->current.overlay_string_index >= 0)
2243 {
2244 set_iterator_to_next (it, 1);
2245 move_it_in_display_line_to (it, -1, -1, 0);
2246 }
2247
2248 it->continuation_lines_width += it->current_x;
2249 }
2250
2251 /* We're starting a new display line, not affected by the
2252 height of the continued line, so clear the appropriate
2253 fields in the iterator structure. */
2254 it->max_ascent = it->max_descent = 0;
2255 it->max_phys_ascent = it->max_phys_descent = 0;
2256
2257 it->current_y = first_y;
2258 it->vpos = 0;
2259 it->current_x = it->hpos = 0;
2260 }
2261 }
2262
2263 #if 0 /* Don't assert the following because start_display is sometimes
2264 called intentionally with a window start that is not at a
2265 line start. Please leave this code in as a comment. */
2266
2267 /* Window start should be on a line start, now. */
2268 xassert (it->continuation_lines_width
2269 || IT_CHARPOS (it) == BEGV
2270 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2271 #endif /* 0 */
2272 }
2273
2274
2275 /* Return 1 if POS is a position in ellipses displayed for invisible
2276 text. W is the window we display, for text property lookup. */
2277
2278 static int
2279 in_ellipses_for_invisible_text_p (pos, w)
2280 struct display_pos *pos;
2281 struct window *w;
2282 {
2283 Lisp_Object prop, window;
2284 int ellipses_p = 0;
2285 int charpos = CHARPOS (pos->pos);
2286
2287 /* If POS specifies a position in a display vector, this might
2288 be for an ellipsis displayed for invisible text. We won't
2289 get the iterator set up for delivering that ellipsis unless
2290 we make sure that it gets aware of the invisible text. */
2291 if (pos->dpvec_index >= 0
2292 && pos->overlay_string_index < 0
2293 && CHARPOS (pos->string_pos) < 0
2294 && charpos > BEGV
2295 && (XSETWINDOW (window, w),
2296 prop = Fget_char_property (make_number (charpos),
2297 Qinvisible, window),
2298 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2299 {
2300 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2301 window);
2302 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2303 }
2304
2305 return ellipses_p;
2306 }
2307
2308
2309 /* Initialize IT for stepping through current_buffer in window W,
2310 starting at position POS that includes overlay string and display
2311 vector/ control character translation position information. Value
2312 is zero if there are overlay strings with newlines at POS. */
2313
2314 static int
2315 init_from_display_pos (it, w, pos)
2316 struct it *it;
2317 struct window *w;
2318 struct display_pos *pos;
2319 {
2320 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2321 int i, overlay_strings_with_newlines = 0;
2322
2323 /* If POS specifies a position in a display vector, this might
2324 be for an ellipsis displayed for invisible text. We won't
2325 get the iterator set up for delivering that ellipsis unless
2326 we make sure that it gets aware of the invisible text. */
2327 if (in_ellipses_for_invisible_text_p (pos, w))
2328 {
2329 --charpos;
2330 bytepos = 0;
2331 }
2332
2333 /* Keep in mind: the call to reseat in init_iterator skips invisible
2334 text, so we might end up at a position different from POS. This
2335 is only a problem when POS is a row start after a newline and an
2336 overlay starts there with an after-string, and the overlay has an
2337 invisible property. Since we don't skip invisible text in
2338 display_line and elsewhere immediately after consuming the
2339 newline before the row start, such a POS will not be in a string,
2340 but the call to init_iterator below will move us to the
2341 after-string. */
2342 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2343
2344 for (i = 0; i < it->n_overlay_strings; ++i)
2345 {
2346 const char *s = SDATA (it->overlay_strings[i]);
2347 const char *e = s + SBYTES (it->overlay_strings[i]);
2348
2349 while (s < e && *s != '\n')
2350 ++s;
2351
2352 if (s < e)
2353 {
2354 overlay_strings_with_newlines = 1;
2355 break;
2356 }
2357 }
2358
2359 /* If position is within an overlay string, set up IT to the right
2360 overlay string. */
2361 if (pos->overlay_string_index >= 0)
2362 {
2363 int relative_index;
2364
2365 /* If the first overlay string happens to have a `display'
2366 property for an image, the iterator will be set up for that
2367 image, and we have to undo that setup first before we can
2368 correct the overlay string index. */
2369 if (it->method == next_element_from_image)
2370 pop_it (it);
2371
2372 /* We already have the first chunk of overlay strings in
2373 IT->overlay_strings. Load more until the one for
2374 pos->overlay_string_index is in IT->overlay_strings. */
2375 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2376 {
2377 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2378 it->current.overlay_string_index = 0;
2379 while (n--)
2380 {
2381 load_overlay_strings (it, 0);
2382 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2383 }
2384 }
2385
2386 it->current.overlay_string_index = pos->overlay_string_index;
2387 relative_index = (it->current.overlay_string_index
2388 % OVERLAY_STRING_CHUNK_SIZE);
2389 it->string = it->overlay_strings[relative_index];
2390 xassert (STRINGP (it->string));
2391 it->current.string_pos = pos->string_pos;
2392 it->method = next_element_from_string;
2393 }
2394
2395 #if 0 /* This is bogus because POS not having an overlay string
2396 position does not mean it's after the string. Example: A
2397 line starting with a before-string and initialization of IT
2398 to the previous row's end position. */
2399 else if (it->current.overlay_string_index >= 0)
2400 {
2401 /* If POS says we're already after an overlay string ending at
2402 POS, make sure to pop the iterator because it will be in
2403 front of that overlay string. When POS is ZV, we've thereby
2404 also ``processed'' overlay strings at ZV. */
2405 while (it->sp)
2406 pop_it (it);
2407 it->current.overlay_string_index = -1;
2408 it->method = next_element_from_buffer;
2409 if (CHARPOS (pos->pos) == ZV)
2410 it->overlay_strings_at_end_processed_p = 1;
2411 }
2412 #endif /* 0 */
2413
2414 if (CHARPOS (pos->string_pos) >= 0)
2415 {
2416 /* Recorded position is not in an overlay string, but in another
2417 string. This can only be a string from a `display' property.
2418 IT should already be filled with that string. */
2419 it->current.string_pos = pos->string_pos;
2420 xassert (STRINGP (it->string));
2421 }
2422
2423 /* Restore position in display vector translations, control
2424 character translations or ellipses. */
2425 if (pos->dpvec_index >= 0)
2426 {
2427 if (it->dpvec == NULL)
2428 get_next_display_element (it);
2429 xassert (it->dpvec && it->current.dpvec_index == 0);
2430 it->current.dpvec_index = pos->dpvec_index;
2431 }
2432
2433 CHECK_IT (it);
2434 return !overlay_strings_with_newlines;
2435 }
2436
2437
2438 /* Initialize IT for stepping through current_buffer in window W
2439 starting at ROW->start. */
2440
2441 static void
2442 init_to_row_start (it, w, row)
2443 struct it *it;
2444 struct window *w;
2445 struct glyph_row *row;
2446 {
2447 init_from_display_pos (it, w, &row->start);
2448 it->continuation_lines_width = row->continuation_lines_width;
2449 CHECK_IT (it);
2450 }
2451
2452
2453 /* Initialize IT for stepping through current_buffer in window W
2454 starting in the line following ROW, i.e. starting at ROW->end.
2455 Value is zero if there are overlay strings with newlines at ROW's
2456 end position. */
2457
2458 static int
2459 init_to_row_end (it, w, row)
2460 struct it *it;
2461 struct window *w;
2462 struct glyph_row *row;
2463 {
2464 int success = 0;
2465
2466 if (init_from_display_pos (it, w, &row->end))
2467 {
2468 if (row->continued_p)
2469 it->continuation_lines_width
2470 = row->continuation_lines_width + row->pixel_width;
2471 CHECK_IT (it);
2472 success = 1;
2473 }
2474
2475 return success;
2476 }
2477
2478
2479
2480 \f
2481 /***********************************************************************
2482 Text properties
2483 ***********************************************************************/
2484
2485 /* Called when IT reaches IT->stop_charpos. Handle text property and
2486 overlay changes. Set IT->stop_charpos to the next position where
2487 to stop. */
2488
2489 static void
2490 handle_stop (it)
2491 struct it *it;
2492 {
2493 enum prop_handled handled;
2494 int handle_overlay_change_p = 1;
2495 struct props *p;
2496
2497 it->dpvec = NULL;
2498 it->current.dpvec_index = -1;
2499
2500 do
2501 {
2502 handled = HANDLED_NORMALLY;
2503
2504 /* Call text property handlers. */
2505 for (p = it_props; p->handler; ++p)
2506 {
2507 handled = p->handler (it);
2508
2509 if (handled == HANDLED_RECOMPUTE_PROPS)
2510 break;
2511 else if (handled == HANDLED_RETURN)
2512 return;
2513 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2514 handle_overlay_change_p = 0;
2515 }
2516
2517 if (handled != HANDLED_RECOMPUTE_PROPS)
2518 {
2519 /* Don't check for overlay strings below when set to deliver
2520 characters from a display vector. */
2521 if (it->method == next_element_from_display_vector)
2522 handle_overlay_change_p = 0;
2523
2524 /* Handle overlay changes. */
2525 if (handle_overlay_change_p)
2526 handled = handle_overlay_change (it);
2527
2528 /* Determine where to stop next. */
2529 if (handled == HANDLED_NORMALLY)
2530 compute_stop_pos (it);
2531 }
2532 }
2533 while (handled == HANDLED_RECOMPUTE_PROPS);
2534 }
2535
2536
2537 /* Compute IT->stop_charpos from text property and overlay change
2538 information for IT's current position. */
2539
2540 static void
2541 compute_stop_pos (it)
2542 struct it *it;
2543 {
2544 register INTERVAL iv, next_iv;
2545 Lisp_Object object, limit, position;
2546
2547 /* If nowhere else, stop at the end. */
2548 it->stop_charpos = it->end_charpos;
2549
2550 if (STRINGP (it->string))
2551 {
2552 /* Strings are usually short, so don't limit the search for
2553 properties. */
2554 object = it->string;
2555 limit = Qnil;
2556 position = make_number (IT_STRING_CHARPOS (*it));
2557 }
2558 else
2559 {
2560 int charpos;
2561
2562 /* If next overlay change is in front of the current stop pos
2563 (which is IT->end_charpos), stop there. Note: value of
2564 next_overlay_change is point-max if no overlay change
2565 follows. */
2566 charpos = next_overlay_change (IT_CHARPOS (*it));
2567 if (charpos < it->stop_charpos)
2568 it->stop_charpos = charpos;
2569
2570 /* If showing the region, we have to stop at the region
2571 start or end because the face might change there. */
2572 if (it->region_beg_charpos > 0)
2573 {
2574 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2575 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2576 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2577 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2578 }
2579
2580 /* Set up variables for computing the stop position from text
2581 property changes. */
2582 XSETBUFFER (object, current_buffer);
2583 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2584 position = make_number (IT_CHARPOS (*it));
2585
2586 }
2587
2588 /* Get the interval containing IT's position. Value is a null
2589 interval if there isn't such an interval. */
2590 iv = validate_interval_range (object, &position, &position, 0);
2591 if (!NULL_INTERVAL_P (iv))
2592 {
2593 Lisp_Object values_here[LAST_PROP_IDX];
2594 struct props *p;
2595
2596 /* Get properties here. */
2597 for (p = it_props; p->handler; ++p)
2598 values_here[p->idx] = textget (iv->plist, *p->name);
2599
2600 /* Look for an interval following iv that has different
2601 properties. */
2602 for (next_iv = next_interval (iv);
2603 (!NULL_INTERVAL_P (next_iv)
2604 && (NILP (limit)
2605 || XFASTINT (limit) > next_iv->position));
2606 next_iv = next_interval (next_iv))
2607 {
2608 for (p = it_props; p->handler; ++p)
2609 {
2610 Lisp_Object new_value;
2611
2612 new_value = textget (next_iv->plist, *p->name);
2613 if (!EQ (values_here[p->idx], new_value))
2614 break;
2615 }
2616
2617 if (p->handler)
2618 break;
2619 }
2620
2621 if (!NULL_INTERVAL_P (next_iv))
2622 {
2623 if (INTEGERP (limit)
2624 && next_iv->position >= XFASTINT (limit))
2625 /* No text property change up to limit. */
2626 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2627 else
2628 /* Text properties change in next_iv. */
2629 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2630 }
2631 }
2632
2633 xassert (STRINGP (it->string)
2634 || (it->stop_charpos >= BEGV
2635 && it->stop_charpos >= IT_CHARPOS (*it)));
2636 }
2637
2638
2639 /* Return the position of the next overlay change after POS in
2640 current_buffer. Value is point-max if no overlay change
2641 follows. This is like `next-overlay-change' but doesn't use
2642 xmalloc. */
2643
2644 static int
2645 next_overlay_change (pos)
2646 int pos;
2647 {
2648 int noverlays;
2649 int endpos;
2650 Lisp_Object *overlays;
2651 int len;
2652 int i;
2653
2654 /* Get all overlays at the given position. */
2655 len = 10;
2656 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2657 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2658 if (noverlays > len)
2659 {
2660 len = noverlays;
2661 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2662 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2663 }
2664
2665 /* If any of these overlays ends before endpos,
2666 use its ending point instead. */
2667 for (i = 0; i < noverlays; ++i)
2668 {
2669 Lisp_Object oend;
2670 int oendpos;
2671
2672 oend = OVERLAY_END (overlays[i]);
2673 oendpos = OVERLAY_POSITION (oend);
2674 endpos = min (endpos, oendpos);
2675 }
2676
2677 return endpos;
2678 }
2679
2680
2681 \f
2682 /***********************************************************************
2683 Fontification
2684 ***********************************************************************/
2685
2686 /* Handle changes in the `fontified' property of the current buffer by
2687 calling hook functions from Qfontification_functions to fontify
2688 regions of text. */
2689
2690 static enum prop_handled
2691 handle_fontified_prop (it)
2692 struct it *it;
2693 {
2694 Lisp_Object prop, pos;
2695 enum prop_handled handled = HANDLED_NORMALLY;
2696
2697 /* Get the value of the `fontified' property at IT's current buffer
2698 position. (The `fontified' property doesn't have a special
2699 meaning in strings.) If the value is nil, call functions from
2700 Qfontification_functions. */
2701 if (!STRINGP (it->string)
2702 && it->s == NULL
2703 && !NILP (Vfontification_functions)
2704 && !NILP (Vrun_hooks)
2705 && (pos = make_number (IT_CHARPOS (*it)),
2706 prop = Fget_char_property (pos, Qfontified, Qnil),
2707 NILP (prop)))
2708 {
2709 int count = SPECPDL_INDEX ();
2710 Lisp_Object val;
2711
2712 val = Vfontification_functions;
2713 specbind (Qfontification_functions, Qnil);
2714
2715 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2716 safe_call1 (val, pos);
2717 else
2718 {
2719 Lisp_Object globals, fn;
2720 struct gcpro gcpro1, gcpro2;
2721
2722 globals = Qnil;
2723 GCPRO2 (val, globals);
2724
2725 for (; CONSP (val); val = XCDR (val))
2726 {
2727 fn = XCAR (val);
2728
2729 if (EQ (fn, Qt))
2730 {
2731 /* A value of t indicates this hook has a local
2732 binding; it means to run the global binding too.
2733 In a global value, t should not occur. If it
2734 does, we must ignore it to avoid an endless
2735 loop. */
2736 for (globals = Fdefault_value (Qfontification_functions);
2737 CONSP (globals);
2738 globals = XCDR (globals))
2739 {
2740 fn = XCAR (globals);
2741 if (!EQ (fn, Qt))
2742 safe_call1 (fn, pos);
2743 }
2744 }
2745 else
2746 safe_call1 (fn, pos);
2747 }
2748
2749 UNGCPRO;
2750 }
2751
2752 unbind_to (count, Qnil);
2753
2754 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2755 something. This avoids an endless loop if they failed to
2756 fontify the text for which reason ever. */
2757 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2758 handled = HANDLED_RECOMPUTE_PROPS;
2759 }
2760
2761 return handled;
2762 }
2763
2764
2765 \f
2766 /***********************************************************************
2767 Faces
2768 ***********************************************************************/
2769
2770 /* Set up iterator IT from face properties at its current position.
2771 Called from handle_stop. */
2772
2773 static enum prop_handled
2774 handle_face_prop (it)
2775 struct it *it;
2776 {
2777 int new_face_id, next_stop;
2778
2779 if (!STRINGP (it->string))
2780 {
2781 new_face_id
2782 = face_at_buffer_position (it->w,
2783 IT_CHARPOS (*it),
2784 it->region_beg_charpos,
2785 it->region_end_charpos,
2786 &next_stop,
2787 (IT_CHARPOS (*it)
2788 + TEXT_PROP_DISTANCE_LIMIT),
2789 0);
2790
2791 /* Is this a start of a run of characters with box face?
2792 Caveat: this can be called for a freshly initialized
2793 iterator; face_id is -1 in this case. We know that the new
2794 face will not change until limit, i.e. if the new face has a
2795 box, all characters up to limit will have one. But, as
2796 usual, we don't know whether limit is really the end. */
2797 if (new_face_id != it->face_id)
2798 {
2799 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2800
2801 /* If new face has a box but old face has not, this is
2802 the start of a run of characters with box, i.e. it has
2803 a shadow on the left side. The value of face_id of the
2804 iterator will be -1 if this is the initial call that gets
2805 the face. In this case, we have to look in front of IT's
2806 position and see whether there is a face != new_face_id. */
2807 it->start_of_box_run_p
2808 = (new_face->box != FACE_NO_BOX
2809 && (it->face_id >= 0
2810 || IT_CHARPOS (*it) == BEG
2811 || new_face_id != face_before_it_pos (it)));
2812 it->face_box_p = new_face->box != FACE_NO_BOX;
2813 }
2814 }
2815 else
2816 {
2817 int base_face_id, bufpos;
2818
2819 if (it->current.overlay_string_index >= 0)
2820 bufpos = IT_CHARPOS (*it);
2821 else
2822 bufpos = 0;
2823
2824 /* For strings from a buffer, i.e. overlay strings or strings
2825 from a `display' property, use the face at IT's current
2826 buffer position as the base face to merge with, so that
2827 overlay strings appear in the same face as surrounding
2828 text, unless they specify their own faces. */
2829 base_face_id = underlying_face_id (it);
2830
2831 new_face_id = face_at_string_position (it->w,
2832 it->string,
2833 IT_STRING_CHARPOS (*it),
2834 bufpos,
2835 it->region_beg_charpos,
2836 it->region_end_charpos,
2837 &next_stop,
2838 base_face_id, 0);
2839
2840 #if 0 /* This shouldn't be neccessary. Let's check it. */
2841 /* If IT is used to display a mode line we would really like to
2842 use the mode line face instead of the frame's default face. */
2843 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2844 && new_face_id == DEFAULT_FACE_ID)
2845 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2846 #endif
2847
2848 /* Is this a start of a run of characters with box? Caveat:
2849 this can be called for a freshly allocated iterator; face_id
2850 is -1 is this case. We know that the new face will not
2851 change until the next check pos, i.e. if the new face has a
2852 box, all characters up to that position will have a
2853 box. But, as usual, we don't know whether that position
2854 is really the end. */
2855 if (new_face_id != it->face_id)
2856 {
2857 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2858 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2859
2860 /* If new face has a box but old face hasn't, this is the
2861 start of a run of characters with box, i.e. it has a
2862 shadow on the left side. */
2863 it->start_of_box_run_p
2864 = new_face->box && (old_face == NULL || !old_face->box);
2865 it->face_box_p = new_face->box != FACE_NO_BOX;
2866 }
2867 }
2868
2869 it->face_id = new_face_id;
2870 return HANDLED_NORMALLY;
2871 }
2872
2873
2874 /* Return the ID of the face ``underlying'' IT's current position,
2875 which is in a string. If the iterator is associated with a
2876 buffer, return the face at IT's current buffer position.
2877 Otherwise, use the iterator's base_face_id. */
2878
2879 static int
2880 underlying_face_id (it)
2881 struct it *it;
2882 {
2883 int face_id = it->base_face_id, i;
2884
2885 xassert (STRINGP (it->string));
2886
2887 for (i = it->sp - 1; i >= 0; --i)
2888 if (NILP (it->stack[i].string))
2889 face_id = it->stack[i].face_id;
2890
2891 return face_id;
2892 }
2893
2894
2895 /* Compute the face one character before or after the current position
2896 of IT. BEFORE_P non-zero means get the face in front of IT's
2897 position. Value is the id of the face. */
2898
2899 static int
2900 face_before_or_after_it_pos (it, before_p)
2901 struct it *it;
2902 int before_p;
2903 {
2904 int face_id, limit;
2905 int next_check_charpos;
2906 struct text_pos pos;
2907
2908 xassert (it->s == NULL);
2909
2910 if (STRINGP (it->string))
2911 {
2912 int bufpos, base_face_id;
2913
2914 /* No face change past the end of the string (for the case
2915 we are padding with spaces). No face change before the
2916 string start. */
2917 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
2918 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2919 return it->face_id;
2920
2921 /* Set pos to the position before or after IT's current position. */
2922 if (before_p)
2923 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2924 else
2925 /* For composition, we must check the character after the
2926 composition. */
2927 pos = (it->what == IT_COMPOSITION
2928 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2929 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2930
2931 if (it->current.overlay_string_index >= 0)
2932 bufpos = IT_CHARPOS (*it);
2933 else
2934 bufpos = 0;
2935
2936 base_face_id = underlying_face_id (it);
2937
2938 /* Get the face for ASCII, or unibyte. */
2939 face_id = face_at_string_position (it->w,
2940 it->string,
2941 CHARPOS (pos),
2942 bufpos,
2943 it->region_beg_charpos,
2944 it->region_end_charpos,
2945 &next_check_charpos,
2946 base_face_id, 0);
2947
2948 /* Correct the face for charsets different from ASCII. Do it
2949 for the multibyte case only. The face returned above is
2950 suitable for unibyte text if IT->string is unibyte. */
2951 if (STRING_MULTIBYTE (it->string))
2952 {
2953 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
2954 int rest = SBYTES (it->string) - BYTEPOS (pos);
2955 int c, len;
2956 struct face *face = FACE_FROM_ID (it->f, face_id);
2957
2958 c = string_char_and_length (p, rest, &len);
2959 face_id = FACE_FOR_CHAR (it->f, face, c);
2960 }
2961 }
2962 else
2963 {
2964 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2965 || (IT_CHARPOS (*it) <= BEGV && before_p))
2966 return it->face_id;
2967
2968 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2969 pos = it->current.pos;
2970
2971 if (before_p)
2972 DEC_TEXT_POS (pos, it->multibyte_p);
2973 else
2974 {
2975 if (it->what == IT_COMPOSITION)
2976 /* For composition, we must check the position after the
2977 composition. */
2978 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2979 else
2980 INC_TEXT_POS (pos, it->multibyte_p);
2981 }
2982
2983 /* Determine face for CHARSET_ASCII, or unibyte. */
2984 face_id = face_at_buffer_position (it->w,
2985 CHARPOS (pos),
2986 it->region_beg_charpos,
2987 it->region_end_charpos,
2988 &next_check_charpos,
2989 limit, 0);
2990
2991 /* Correct the face for charsets different from ASCII. Do it
2992 for the multibyte case only. The face returned above is
2993 suitable for unibyte text if current_buffer is unibyte. */
2994 if (it->multibyte_p)
2995 {
2996 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
2997 struct face *face = FACE_FROM_ID (it->f, face_id);
2998 face_id = FACE_FOR_CHAR (it->f, face, c);
2999 }
3000 }
3001
3002 return face_id;
3003 }
3004
3005
3006 \f
3007 /***********************************************************************
3008 Invisible text
3009 ***********************************************************************/
3010
3011 /* Set up iterator IT from invisible properties at its current
3012 position. Called from handle_stop. */
3013
3014 static enum prop_handled
3015 handle_invisible_prop (it)
3016 struct it *it;
3017 {
3018 enum prop_handled handled = HANDLED_NORMALLY;
3019
3020 if (STRINGP (it->string))
3021 {
3022 extern Lisp_Object Qinvisible;
3023 Lisp_Object prop, end_charpos, limit, charpos;
3024
3025 /* Get the value of the invisible text property at the
3026 current position. Value will be nil if there is no such
3027 property. */
3028 charpos = make_number (IT_STRING_CHARPOS (*it));
3029 prop = Fget_text_property (charpos, Qinvisible, it->string);
3030
3031 if (!NILP (prop)
3032 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3033 {
3034 handled = HANDLED_RECOMPUTE_PROPS;
3035
3036 /* Get the position at which the next change of the
3037 invisible text property can be found in IT->string.
3038 Value will be nil if the property value is the same for
3039 all the rest of IT->string. */
3040 XSETINT (limit, SCHARS (it->string));
3041 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3042 it->string, limit);
3043
3044 /* Text at current position is invisible. The next
3045 change in the property is at position end_charpos.
3046 Move IT's current position to that position. */
3047 if (INTEGERP (end_charpos)
3048 && XFASTINT (end_charpos) < XFASTINT (limit))
3049 {
3050 struct text_pos old;
3051 old = it->current.string_pos;
3052 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3053 compute_string_pos (&it->current.string_pos, old, it->string);
3054 }
3055 else
3056 {
3057 /* The rest of the string is invisible. If this is an
3058 overlay string, proceed with the next overlay string
3059 or whatever comes and return a character from there. */
3060 if (it->current.overlay_string_index >= 0)
3061 {
3062 next_overlay_string (it);
3063 /* Don't check for overlay strings when we just
3064 finished processing them. */
3065 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3066 }
3067 else
3068 {
3069 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3070 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3071 }
3072 }
3073 }
3074 }
3075 else
3076 {
3077 int invis_p, newpos, next_stop, start_charpos;
3078 Lisp_Object pos, prop, overlay;
3079
3080 /* First of all, is there invisible text at this position? */
3081 start_charpos = IT_CHARPOS (*it);
3082 pos = make_number (IT_CHARPOS (*it));
3083 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3084 &overlay);
3085 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3086
3087 /* If we are on invisible text, skip over it. */
3088 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3089 {
3090 /* Record whether we have to display an ellipsis for the
3091 invisible text. */
3092 int display_ellipsis_p = invis_p == 2;
3093
3094 handled = HANDLED_RECOMPUTE_PROPS;
3095
3096 /* Loop skipping over invisible text. The loop is left at
3097 ZV or with IT on the first char being visible again. */
3098 do
3099 {
3100 /* Try to skip some invisible text. Return value is the
3101 position reached which can be equal to IT's position
3102 if there is nothing invisible here. This skips both
3103 over invisible text properties and overlays with
3104 invisible property. */
3105 newpos = skip_invisible (IT_CHARPOS (*it),
3106 &next_stop, ZV, it->window);
3107
3108 /* If we skipped nothing at all we weren't at invisible
3109 text in the first place. If everything to the end of
3110 the buffer was skipped, end the loop. */
3111 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3112 invis_p = 0;
3113 else
3114 {
3115 /* We skipped some characters but not necessarily
3116 all there are. Check if we ended up on visible
3117 text. Fget_char_property returns the property of
3118 the char before the given position, i.e. if we
3119 get invis_p = 0, this means that the char at
3120 newpos is visible. */
3121 pos = make_number (newpos);
3122 prop = Fget_char_property (pos, Qinvisible, it->window);
3123 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3124 }
3125
3126 /* If we ended up on invisible text, proceed to
3127 skip starting with next_stop. */
3128 if (invis_p)
3129 IT_CHARPOS (*it) = next_stop;
3130 }
3131 while (invis_p);
3132
3133 /* The position newpos is now either ZV or on visible text. */
3134 IT_CHARPOS (*it) = newpos;
3135 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3136
3137 /* If there are before-strings at the start of invisible
3138 text, and the text is invisible because of a text
3139 property, arrange to show before-strings because 20.x did
3140 it that way. (If the text is invisible because of an
3141 overlay property instead of a text property, this is
3142 already handled in the overlay code.) */
3143 if (NILP (overlay)
3144 && get_overlay_strings (it, start_charpos))
3145 {
3146 handled = HANDLED_RECOMPUTE_PROPS;
3147 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3148 }
3149 else if (display_ellipsis_p)
3150 setup_for_ellipsis (it);
3151 }
3152 }
3153
3154 return handled;
3155 }
3156
3157
3158 /* Make iterator IT return `...' next. */
3159
3160 static void
3161 setup_for_ellipsis (it)
3162 struct it *it;
3163 {
3164 if (it->dp
3165 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3166 {
3167 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3168 it->dpvec = v->contents;
3169 it->dpend = v->contents + v->size;
3170 }
3171 else
3172 {
3173 /* Default `...'. */
3174 it->dpvec = default_invis_vector;
3175 it->dpend = default_invis_vector + 3;
3176 }
3177
3178 /* The ellipsis display does not replace the display of the
3179 character at the new position. Indicate this by setting
3180 IT->dpvec_char_len to zero. */
3181 it->dpvec_char_len = 0;
3182
3183 it->current.dpvec_index = 0;
3184 it->method = next_element_from_display_vector;
3185 }
3186
3187
3188 \f
3189 /***********************************************************************
3190 'display' property
3191 ***********************************************************************/
3192
3193 /* Set up iterator IT from `display' property at its current position.
3194 Called from handle_stop. */
3195
3196 static enum prop_handled
3197 handle_display_prop (it)
3198 struct it *it;
3199 {
3200 Lisp_Object prop, object;
3201 struct text_pos *position;
3202 int display_replaced_p = 0;
3203
3204 if (STRINGP (it->string))
3205 {
3206 object = it->string;
3207 position = &it->current.string_pos;
3208 }
3209 else
3210 {
3211 object = it->w->buffer;
3212 position = &it->current.pos;
3213 }
3214
3215 /* Reset those iterator values set from display property values. */
3216 it->font_height = Qnil;
3217 it->space_width = Qnil;
3218 it->voffset = 0;
3219
3220 /* We don't support recursive `display' properties, i.e. string
3221 values that have a string `display' property, that have a string
3222 `display' property etc. */
3223 if (!it->string_from_display_prop_p)
3224 it->area = TEXT_AREA;
3225
3226 prop = Fget_char_property (make_number (position->charpos),
3227 Qdisplay, object);
3228 if (NILP (prop))
3229 return HANDLED_NORMALLY;
3230
3231 if (CONSP (prop)
3232 /* Simple properties. */
3233 && !EQ (XCAR (prop), Qimage)
3234 && !EQ (XCAR (prop), Qspace)
3235 && !EQ (XCAR (prop), Qwhen)
3236 && !EQ (XCAR (prop), Qspace_width)
3237 && !EQ (XCAR (prop), Qheight)
3238 && !EQ (XCAR (prop), Qraise)
3239 /* Marginal area specifications. */
3240 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3241 && !NILP (XCAR (prop)))
3242 {
3243 for (; CONSP (prop); prop = XCDR (prop))
3244 {
3245 if (handle_single_display_prop (it, XCAR (prop), object,
3246 position, display_replaced_p))
3247 display_replaced_p = 1;
3248 }
3249 }
3250 else if (VECTORP (prop))
3251 {
3252 int i;
3253 for (i = 0; i < ASIZE (prop); ++i)
3254 if (handle_single_display_prop (it, AREF (prop, i), object,
3255 position, display_replaced_p))
3256 display_replaced_p = 1;
3257 }
3258 else
3259 {
3260 if (handle_single_display_prop (it, prop, object, position, 0))
3261 display_replaced_p = 1;
3262 }
3263
3264 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3265 }
3266
3267
3268 /* Value is the position of the end of the `display' property starting
3269 at START_POS in OBJECT. */
3270
3271 static struct text_pos
3272 display_prop_end (it, object, start_pos)
3273 struct it *it;
3274 Lisp_Object object;
3275 struct text_pos start_pos;
3276 {
3277 Lisp_Object end;
3278 struct text_pos end_pos;
3279
3280 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3281 Qdisplay, object, Qnil);
3282 CHARPOS (end_pos) = XFASTINT (end);
3283 if (STRINGP (object))
3284 compute_string_pos (&end_pos, start_pos, it->string);
3285 else
3286 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3287
3288 return end_pos;
3289 }
3290
3291
3292 /* Set up IT from a single `display' sub-property value PROP. OBJECT
3293 is the object in which the `display' property was found. *POSITION
3294 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3295 means that we previously saw a display sub-property which already
3296 replaced text display with something else, for example an image;
3297 ignore such properties after the first one has been processed.
3298
3299 If PROP is a `space' or `image' sub-property, set *POSITION to the
3300 end position of the `display' property.
3301
3302 Value is non-zero if something was found which replaces the display
3303 of buffer or string text. */
3304
3305 static int
3306 handle_single_display_prop (it, prop, object, position,
3307 display_replaced_before_p)
3308 struct it *it;
3309 Lisp_Object prop;
3310 Lisp_Object object;
3311 struct text_pos *position;
3312 int display_replaced_before_p;
3313 {
3314 Lisp_Object value;
3315 int replaces_text_display_p = 0;
3316 Lisp_Object form;
3317
3318 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
3319 evaluated. If the result is nil, VALUE is ignored. */
3320 form = Qt;
3321 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3322 {
3323 prop = XCDR (prop);
3324 if (!CONSP (prop))
3325 return 0;
3326 form = XCAR (prop);
3327 prop = XCDR (prop);
3328 }
3329
3330 if (!NILP (form) && !EQ (form, Qt))
3331 {
3332 int count = SPECPDL_INDEX ();
3333 struct gcpro gcpro1;
3334
3335 /* Bind `object' to the object having the `display' property, a
3336 buffer or string. Bind `position' to the position in the
3337 object where the property was found, and `buffer-position'
3338 to the current position in the buffer. */
3339 specbind (Qobject, object);
3340 specbind (Qposition, make_number (CHARPOS (*position)));
3341 specbind (Qbuffer_position,
3342 make_number (STRINGP (object)
3343 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3344 GCPRO1 (form);
3345 form = safe_eval (form);
3346 UNGCPRO;
3347 unbind_to (count, Qnil);
3348 }
3349
3350 if (NILP (form))
3351 return 0;
3352
3353 if (CONSP (prop)
3354 && EQ (XCAR (prop), Qheight)
3355 && CONSP (XCDR (prop)))
3356 {
3357 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3358 return 0;
3359
3360 /* `(height HEIGHT)'. */
3361 it->font_height = XCAR (XCDR (prop));
3362 if (!NILP (it->font_height))
3363 {
3364 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3365 int new_height = -1;
3366
3367 if (CONSP (it->font_height)
3368 && (EQ (XCAR (it->font_height), Qplus)
3369 || EQ (XCAR (it->font_height), Qminus))
3370 && CONSP (XCDR (it->font_height))
3371 && INTEGERP (XCAR (XCDR (it->font_height))))
3372 {
3373 /* `(+ N)' or `(- N)' where N is an integer. */
3374 int steps = XINT (XCAR (XCDR (it->font_height)));
3375 if (EQ (XCAR (it->font_height), Qplus))
3376 steps = - steps;
3377 it->face_id = smaller_face (it->f, it->face_id, steps);
3378 }
3379 else if (FUNCTIONP (it->font_height))
3380 {
3381 /* Call function with current height as argument.
3382 Value is the new height. */
3383 Lisp_Object height;
3384 height = safe_call1 (it->font_height,
3385 face->lface[LFACE_HEIGHT_INDEX]);
3386 if (NUMBERP (height))
3387 new_height = XFLOATINT (height);
3388 }
3389 else if (NUMBERP (it->font_height))
3390 {
3391 /* Value is a multiple of the canonical char height. */
3392 struct face *face;
3393
3394 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3395 new_height = (XFLOATINT (it->font_height)
3396 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3397 }
3398 else
3399 {
3400 /* Evaluate IT->font_height with `height' bound to the
3401 current specified height to get the new height. */
3402 Lisp_Object value;
3403 int count = SPECPDL_INDEX ();
3404
3405 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3406 value = safe_eval (it->font_height);
3407 unbind_to (count, Qnil);
3408
3409 if (NUMBERP (value))
3410 new_height = XFLOATINT (value);
3411 }
3412
3413 if (new_height > 0)
3414 it->face_id = face_with_height (it->f, it->face_id, new_height);
3415 }
3416 }
3417 else if (CONSP (prop)
3418 && EQ (XCAR (prop), Qspace_width)
3419 && CONSP (XCDR (prop)))
3420 {
3421 /* `(space_width WIDTH)'. */
3422 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3423 return 0;
3424
3425 value = XCAR (XCDR (prop));
3426 if (NUMBERP (value) && XFLOATINT (value) > 0)
3427 it->space_width = value;
3428 }
3429 else if (CONSP (prop)
3430 && EQ (XCAR (prop), Qraise)
3431 && CONSP (XCDR (prop)))
3432 {
3433 /* `(raise FACTOR)'. */
3434 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3435 return 0;
3436
3437 #ifdef HAVE_WINDOW_SYSTEM
3438 value = XCAR (XCDR (prop));
3439 if (NUMBERP (value))
3440 {
3441 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3442 it->voffset = - (XFLOATINT (value)
3443 * (FONT_HEIGHT (face->font)));
3444 }
3445 #endif /* HAVE_WINDOW_SYSTEM */
3446 }
3447 else if (!it->string_from_display_prop_p)
3448 {
3449 /* `((margin left-margin) VALUE)' or `((margin right-margin)
3450 VALUE) or `((margin nil) VALUE)' or VALUE. */
3451 Lisp_Object location, value;
3452 struct text_pos start_pos;
3453 int valid_p;
3454
3455 /* Characters having this form of property are not displayed, so
3456 we have to find the end of the property. */
3457 start_pos = *position;
3458 *position = display_prop_end (it, object, start_pos);
3459 value = Qnil;
3460
3461 /* Let's stop at the new position and assume that all
3462 text properties change there. */
3463 it->stop_charpos = position->charpos;
3464
3465 location = Qunbound;
3466 if (CONSP (prop) && CONSP (XCAR (prop)))
3467 {
3468 Lisp_Object tem;
3469
3470 value = XCDR (prop);
3471 if (CONSP (value))
3472 value = XCAR (value);
3473
3474 tem = XCAR (prop);
3475 if (EQ (XCAR (tem), Qmargin)
3476 && (tem = XCDR (tem),
3477 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3478 (NILP (tem)
3479 || EQ (tem, Qleft_margin)
3480 || EQ (tem, Qright_margin))))
3481 location = tem;
3482 }
3483
3484 if (EQ (location, Qunbound))
3485 {
3486 location = Qnil;
3487 value = prop;
3488 }
3489
3490 #ifdef HAVE_WINDOW_SYSTEM
3491 if (FRAME_TERMCAP_P (it->f))
3492 valid_p = STRINGP (value);
3493 else
3494 valid_p = (STRINGP (value)
3495 || (CONSP (value) && EQ (XCAR (value), Qspace))
3496 || valid_image_p (value));
3497 #else /* not HAVE_WINDOW_SYSTEM */
3498 valid_p = STRINGP (value);
3499 #endif /* not HAVE_WINDOW_SYSTEM */
3500
3501 if ((EQ (location, Qleft_margin)
3502 || EQ (location, Qright_margin)
3503 || NILP (location))
3504 && valid_p
3505 && !display_replaced_before_p)
3506 {
3507 replaces_text_display_p = 1;
3508
3509 /* Save current settings of IT so that we can restore them
3510 when we are finished with the glyph property value. */
3511 push_it (it);
3512
3513 if (NILP (location))
3514 it->area = TEXT_AREA;
3515 else if (EQ (location, Qleft_margin))
3516 it->area = LEFT_MARGIN_AREA;
3517 else
3518 it->area = RIGHT_MARGIN_AREA;
3519
3520 if (STRINGP (value))
3521 {
3522 it->string = value;
3523 it->multibyte_p = STRING_MULTIBYTE (it->string);
3524 it->current.overlay_string_index = -1;
3525 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3526 it->end_charpos = it->string_nchars = SCHARS (it->string);
3527 it->method = next_element_from_string;
3528 it->stop_charpos = 0;
3529 it->string_from_display_prop_p = 1;
3530 /* Say that we haven't consumed the characters with
3531 `display' property yet. The call to pop_it in
3532 set_iterator_to_next will clean this up. */
3533 *position = start_pos;
3534 }
3535 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3536 {
3537 it->method = next_element_from_stretch;
3538 it->object = value;
3539 it->current.pos = it->position = start_pos;
3540 }
3541 #ifdef HAVE_WINDOW_SYSTEM
3542 else
3543 {
3544 it->what = IT_IMAGE;
3545 it->image_id = lookup_image (it->f, value);
3546 it->position = start_pos;
3547 it->object = NILP (object) ? it->w->buffer : object;
3548 it->method = next_element_from_image;
3549
3550 /* Say that we haven't consumed the characters with
3551 `display' property yet. The call to pop_it in
3552 set_iterator_to_next will clean this up. */
3553 *position = start_pos;
3554 }
3555 #endif /* HAVE_WINDOW_SYSTEM */
3556 }
3557 else
3558 /* Invalid property or property not supported. Restore
3559 the position to what it was before. */
3560 *position = start_pos;
3561 }
3562
3563 return replaces_text_display_p;
3564 }
3565
3566
3567 /* Check if PROP is a display sub-property value whose text should be
3568 treated as intangible. */
3569
3570 static int
3571 single_display_prop_intangible_p (prop)
3572 Lisp_Object prop;
3573 {
3574 /* Skip over `when FORM'. */
3575 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3576 {
3577 prop = XCDR (prop);
3578 if (!CONSP (prop))
3579 return 0;
3580 prop = XCDR (prop);
3581 }
3582
3583 if (STRINGP (prop))
3584 return 1;
3585
3586 if (!CONSP (prop))
3587 return 0;
3588
3589 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3590 we don't need to treat text as intangible. */
3591 if (EQ (XCAR (prop), Qmargin))
3592 {
3593 prop = XCDR (prop);
3594 if (!CONSP (prop))
3595 return 0;
3596
3597 prop = XCDR (prop);
3598 if (!CONSP (prop)
3599 || EQ (XCAR (prop), Qleft_margin)
3600 || EQ (XCAR (prop), Qright_margin))
3601 return 0;
3602 }
3603
3604 return (CONSP (prop)
3605 && (EQ (XCAR (prop), Qimage)
3606 || EQ (XCAR (prop), Qspace)));
3607 }
3608
3609
3610 /* Check if PROP is a display property value whose text should be
3611 treated as intangible. */
3612
3613 int
3614 display_prop_intangible_p (prop)
3615 Lisp_Object prop;
3616 {
3617 if (CONSP (prop)
3618 && CONSP (XCAR (prop))
3619 && !EQ (Qmargin, XCAR (XCAR (prop))))
3620 {
3621 /* A list of sub-properties. */
3622 while (CONSP (prop))
3623 {
3624 if (single_display_prop_intangible_p (XCAR (prop)))
3625 return 1;
3626 prop = XCDR (prop);
3627 }
3628 }
3629 else if (VECTORP (prop))
3630 {
3631 /* A vector of sub-properties. */
3632 int i;
3633 for (i = 0; i < ASIZE (prop); ++i)
3634 if (single_display_prop_intangible_p (AREF (prop, i)))
3635 return 1;
3636 }
3637 else
3638 return single_display_prop_intangible_p (prop);
3639
3640 return 0;
3641 }
3642
3643
3644 /* Return 1 if PROP is a display sub-property value containing STRING. */
3645
3646 static int
3647 single_display_prop_string_p (prop, string)
3648 Lisp_Object prop, string;
3649 {
3650 if (EQ (string, prop))
3651 return 1;
3652
3653 /* Skip over `when FORM'. */
3654 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3655 {
3656 prop = XCDR (prop);
3657 if (!CONSP (prop))
3658 return 0;
3659 prop = XCDR (prop);
3660 }
3661
3662 if (CONSP (prop))
3663 /* Skip over `margin LOCATION'. */
3664 if (EQ (XCAR (prop), Qmargin))
3665 {
3666 prop = XCDR (prop);
3667 if (!CONSP (prop))
3668 return 0;
3669
3670 prop = XCDR (prop);
3671 if (!CONSP (prop))
3672 return 0;
3673 }
3674
3675 return CONSP (prop) && EQ (XCAR (prop), string);
3676 }
3677
3678
3679 /* Return 1 if STRING appears in the `display' property PROP. */
3680
3681 static int
3682 display_prop_string_p (prop, string)
3683 Lisp_Object prop, string;
3684 {
3685 if (CONSP (prop)
3686 && CONSP (XCAR (prop))
3687 && !EQ (Qmargin, XCAR (XCAR (prop))))
3688 {
3689 /* A list of sub-properties. */
3690 while (CONSP (prop))
3691 {
3692 if (single_display_prop_string_p (XCAR (prop), string))
3693 return 1;
3694 prop = XCDR (prop);
3695 }
3696 }
3697 else if (VECTORP (prop))
3698 {
3699 /* A vector of sub-properties. */
3700 int i;
3701 for (i = 0; i < ASIZE (prop); ++i)
3702 if (single_display_prop_string_p (AREF (prop, i), string))
3703 return 1;
3704 }
3705 else
3706 return single_display_prop_string_p (prop, string);
3707
3708 return 0;
3709 }
3710
3711
3712 /* Determine from which buffer position in W's buffer STRING comes
3713 from. AROUND_CHARPOS is an approximate position where it could
3714 be from. Value is the buffer position or 0 if it couldn't be
3715 determined.
3716
3717 W's buffer must be current.
3718
3719 This function is necessary because we don't record buffer positions
3720 in glyphs generated from strings (to keep struct glyph small).
3721 This function may only use code that doesn't eval because it is
3722 called asynchronously from note_mouse_highlight. */
3723
3724 int
3725 string_buffer_position (w, string, around_charpos)
3726 struct window *w;
3727 Lisp_Object string;
3728 int around_charpos;
3729 {
3730 Lisp_Object limit, prop, pos;
3731 const int MAX_DISTANCE = 1000;
3732 int found = 0;
3733
3734 pos = make_number (around_charpos);
3735 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3736 while (!found && !EQ (pos, limit))
3737 {
3738 prop = Fget_char_property (pos, Qdisplay, Qnil);
3739 if (!NILP (prop) && display_prop_string_p (prop, string))
3740 found = 1;
3741 else
3742 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3743 }
3744
3745 if (!found)
3746 {
3747 pos = make_number (around_charpos);
3748 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3749 while (!found && !EQ (pos, limit))
3750 {
3751 prop = Fget_char_property (pos, Qdisplay, Qnil);
3752 if (!NILP (prop) && display_prop_string_p (prop, string))
3753 found = 1;
3754 else
3755 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3756 limit);
3757 }
3758 }
3759
3760 return found ? XINT (pos) : 0;
3761 }
3762
3763
3764 \f
3765 /***********************************************************************
3766 `composition' property
3767 ***********************************************************************/
3768
3769 /* Set up iterator IT from `composition' property at its current
3770 position. Called from handle_stop. */
3771
3772 static enum prop_handled
3773 handle_composition_prop (it)
3774 struct it *it;
3775 {
3776 Lisp_Object prop, string;
3777 int pos, pos_byte, end;
3778 enum prop_handled handled = HANDLED_NORMALLY;
3779
3780 if (STRINGP (it->string))
3781 {
3782 pos = IT_STRING_CHARPOS (*it);
3783 pos_byte = IT_STRING_BYTEPOS (*it);
3784 string = it->string;
3785 }
3786 else
3787 {
3788 pos = IT_CHARPOS (*it);
3789 pos_byte = IT_BYTEPOS (*it);
3790 string = Qnil;
3791 }
3792
3793 /* If there's a valid composition and point is not inside of the
3794 composition (in the case that the composition is from the current
3795 buffer), draw a glyph composed from the composition components. */
3796 if (find_composition (pos, -1, &pos, &end, &prop, string)
3797 && COMPOSITION_VALID_P (pos, end, prop)
3798 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3799 {
3800 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3801
3802 if (id >= 0)
3803 {
3804 it->method = next_element_from_composition;
3805 it->cmp_id = id;
3806 it->cmp_len = COMPOSITION_LENGTH (prop);
3807 /* For a terminal, draw only the first character of the
3808 components. */
3809 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3810 it->len = (STRINGP (it->string)
3811 ? string_char_to_byte (it->string, end)
3812 : CHAR_TO_BYTE (end)) - pos_byte;
3813 it->stop_charpos = end;
3814 handled = HANDLED_RETURN;
3815 }
3816 }
3817
3818 return handled;
3819 }
3820
3821
3822 \f
3823 /***********************************************************************
3824 Overlay strings
3825 ***********************************************************************/
3826
3827 /* The following structure is used to record overlay strings for
3828 later sorting in load_overlay_strings. */
3829
3830 struct overlay_entry
3831 {
3832 Lisp_Object overlay;
3833 Lisp_Object string;
3834 int priority;
3835 int after_string_p;
3836 };
3837
3838
3839 /* Set up iterator IT from overlay strings at its current position.
3840 Called from handle_stop. */
3841
3842 static enum prop_handled
3843 handle_overlay_change (it)
3844 struct it *it;
3845 {
3846 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3847 return HANDLED_RECOMPUTE_PROPS;
3848 else
3849 return HANDLED_NORMALLY;
3850 }
3851
3852
3853 /* Set up the next overlay string for delivery by IT, if there is an
3854 overlay string to deliver. Called by set_iterator_to_next when the
3855 end of the current overlay string is reached. If there are more
3856 overlay strings to display, IT->string and
3857 IT->current.overlay_string_index are set appropriately here.
3858 Otherwise IT->string is set to nil. */
3859
3860 static void
3861 next_overlay_string (it)
3862 struct it *it;
3863 {
3864 ++it->current.overlay_string_index;
3865 if (it->current.overlay_string_index == it->n_overlay_strings)
3866 {
3867 /* No more overlay strings. Restore IT's settings to what
3868 they were before overlay strings were processed, and
3869 continue to deliver from current_buffer. */
3870 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3871
3872 pop_it (it);
3873 xassert (it->stop_charpos >= BEGV
3874 && it->stop_charpos <= it->end_charpos);
3875 it->string = Qnil;
3876 it->current.overlay_string_index = -1;
3877 SET_TEXT_POS (it->current.string_pos, -1, -1);
3878 it->n_overlay_strings = 0;
3879 it->method = next_element_from_buffer;
3880
3881 /* If we're at the end of the buffer, record that we have
3882 processed the overlay strings there already, so that
3883 next_element_from_buffer doesn't try it again. */
3884 if (IT_CHARPOS (*it) >= it->end_charpos)
3885 it->overlay_strings_at_end_processed_p = 1;
3886
3887 /* If we have to display `...' for invisible text, set
3888 the iterator up for that. */
3889 if (display_ellipsis_p)
3890 setup_for_ellipsis (it);
3891 }
3892 else
3893 {
3894 /* There are more overlay strings to process. If
3895 IT->current.overlay_string_index has advanced to a position
3896 where we must load IT->overlay_strings with more strings, do
3897 it. */
3898 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3899
3900 if (it->current.overlay_string_index && i == 0)
3901 load_overlay_strings (it, 0);
3902
3903 /* Initialize IT to deliver display elements from the overlay
3904 string. */
3905 it->string = it->overlay_strings[i];
3906 it->multibyte_p = STRING_MULTIBYTE (it->string);
3907 SET_TEXT_POS (it->current.string_pos, 0, 0);
3908 it->method = next_element_from_string;
3909 it->stop_charpos = 0;
3910 }
3911
3912 CHECK_IT (it);
3913 }
3914
3915
3916 /* Compare two overlay_entry structures E1 and E2. Used as a
3917 comparison function for qsort in load_overlay_strings. Overlay
3918 strings for the same position are sorted so that
3919
3920 1. All after-strings come in front of before-strings, except
3921 when they come from the same overlay.
3922
3923 2. Within after-strings, strings are sorted so that overlay strings
3924 from overlays with higher priorities come first.
3925
3926 2. Within before-strings, strings are sorted so that overlay
3927 strings from overlays with higher priorities come last.
3928
3929 Value is analogous to strcmp. */
3930
3931
3932 static int
3933 compare_overlay_entries (e1, e2)
3934 void *e1, *e2;
3935 {
3936 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3937 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3938 int result;
3939
3940 if (entry1->after_string_p != entry2->after_string_p)
3941 {
3942 /* Let after-strings appear in front of before-strings if
3943 they come from different overlays. */
3944 if (EQ (entry1->overlay, entry2->overlay))
3945 result = entry1->after_string_p ? 1 : -1;
3946 else
3947 result = entry1->after_string_p ? -1 : 1;
3948 }
3949 else if (entry1->after_string_p)
3950 /* After-strings sorted in order of decreasing priority. */
3951 result = entry2->priority - entry1->priority;
3952 else
3953 /* Before-strings sorted in order of increasing priority. */
3954 result = entry1->priority - entry2->priority;
3955
3956 return result;
3957 }
3958
3959
3960 /* Load the vector IT->overlay_strings with overlay strings from IT's
3961 current buffer position, or from CHARPOS if that is > 0. Set
3962 IT->n_overlays to the total number of overlay strings found.
3963
3964 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3965 a time. On entry into load_overlay_strings,
3966 IT->current.overlay_string_index gives the number of overlay
3967 strings that have already been loaded by previous calls to this
3968 function.
3969
3970 IT->add_overlay_start contains an additional overlay start
3971 position to consider for taking overlay strings from, if non-zero.
3972 This position comes into play when the overlay has an `invisible'
3973 property, and both before and after-strings. When we've skipped to
3974 the end of the overlay, because of its `invisible' property, we
3975 nevertheless want its before-string to appear.
3976 IT->add_overlay_start will contain the overlay start position
3977 in this case.
3978
3979 Overlay strings are sorted so that after-string strings come in
3980 front of before-string strings. Within before and after-strings,
3981 strings are sorted by overlay priority. See also function
3982 compare_overlay_entries. */
3983
3984 static void
3985 load_overlay_strings (it, charpos)
3986 struct it *it;
3987 int charpos;
3988 {
3989 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3990 Lisp_Object ov, overlay, window, str, invisible;
3991 int start, end;
3992 int size = 20;
3993 int n = 0, i, j, invis_p;
3994 struct overlay_entry *entries
3995 = (struct overlay_entry *) alloca (size * sizeof *entries);
3996
3997 if (charpos <= 0)
3998 charpos = IT_CHARPOS (*it);
3999
4000 /* Append the overlay string STRING of overlay OVERLAY to vector
4001 `entries' which has size `size' and currently contains `n'
4002 elements. AFTER_P non-zero means STRING is an after-string of
4003 OVERLAY. */
4004 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4005 do \
4006 { \
4007 Lisp_Object priority; \
4008 \
4009 if (n == size) \
4010 { \
4011 int new_size = 2 * size; \
4012 struct overlay_entry *old = entries; \
4013 entries = \
4014 (struct overlay_entry *) alloca (new_size \
4015 * sizeof *entries); \
4016 bcopy (old, entries, size * sizeof *entries); \
4017 size = new_size; \
4018 } \
4019 \
4020 entries[n].string = (STRING); \
4021 entries[n].overlay = (OVERLAY); \
4022 priority = Foverlay_get ((OVERLAY), Qpriority); \
4023 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4024 entries[n].after_string_p = (AFTER_P); \
4025 ++n; \
4026 } \
4027 while (0)
4028
4029 /* Process overlay before the overlay center. */
4030 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
4031 {
4032 overlay = XCAR (ov);
4033 xassert (OVERLAYP (overlay));
4034 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4035 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4036
4037 if (end < charpos)
4038 break;
4039
4040 /* Skip this overlay if it doesn't start or end at IT's current
4041 position. */
4042 if (end != charpos && start != charpos)
4043 continue;
4044
4045 /* Skip this overlay if it doesn't apply to IT->w. */
4046 window = Foverlay_get (overlay, Qwindow);
4047 if (WINDOWP (window) && XWINDOW (window) != it->w)
4048 continue;
4049
4050 /* If the text ``under'' the overlay is invisible, both before-
4051 and after-strings from this overlay are visible; start and
4052 end position are indistinguishable. */
4053 invisible = Foverlay_get (overlay, Qinvisible);
4054 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4055
4056 /* If overlay has a non-empty before-string, record it. */
4057 if ((start == charpos || (end == charpos && invis_p))
4058 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4059 && SCHARS (str))
4060 RECORD_OVERLAY_STRING (overlay, str, 0);
4061
4062 /* If overlay has a non-empty after-string, record it. */
4063 if ((end == charpos || (start == charpos && invis_p))
4064 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4065 && SCHARS (str))
4066 RECORD_OVERLAY_STRING (overlay, str, 1);
4067 }
4068
4069 /* Process overlays after the overlay center. */
4070 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
4071 {
4072 overlay = XCAR (ov);
4073 xassert (OVERLAYP (overlay));
4074 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4075 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4076
4077 if (start > charpos)
4078 break;
4079
4080 /* Skip this overlay if it doesn't start or end at IT's current
4081 position. */
4082 if (end != charpos && start != charpos)
4083 continue;
4084
4085 /* Skip this overlay if it doesn't apply to IT->w. */
4086 window = Foverlay_get (overlay, Qwindow);
4087 if (WINDOWP (window) && XWINDOW (window) != it->w)
4088 continue;
4089
4090 /* If the text ``under'' the overlay is invisible, it has a zero
4091 dimension, and both before- and after-strings apply. */
4092 invisible = Foverlay_get (overlay, Qinvisible);
4093 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4094
4095 /* If overlay has a non-empty before-string, record it. */
4096 if ((start == charpos || (end == charpos && invis_p))
4097 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4098 && SCHARS (str))
4099 RECORD_OVERLAY_STRING (overlay, str, 0);
4100
4101 /* If overlay has a non-empty after-string, record it. */
4102 if ((end == charpos || (start == charpos && invis_p))
4103 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4104 && SCHARS (str))
4105 RECORD_OVERLAY_STRING (overlay, str, 1);
4106 }
4107
4108 #undef RECORD_OVERLAY_STRING
4109
4110 /* Sort entries. */
4111 if (n > 1)
4112 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4113
4114 /* Record the total number of strings to process. */
4115 it->n_overlay_strings = n;
4116
4117 /* IT->current.overlay_string_index is the number of overlay strings
4118 that have already been consumed by IT. Copy some of the
4119 remaining overlay strings to IT->overlay_strings. */
4120 i = 0;
4121 j = it->current.overlay_string_index;
4122 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4123 it->overlay_strings[i++] = entries[j++].string;
4124
4125 CHECK_IT (it);
4126 }
4127
4128
4129 /* Get the first chunk of overlay strings at IT's current buffer
4130 position, or at CHARPOS if that is > 0. Value is non-zero if at
4131 least one overlay string was found. */
4132
4133 static int
4134 get_overlay_strings (it, charpos)
4135 struct it *it;
4136 int charpos;
4137 {
4138 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4139 process. This fills IT->overlay_strings with strings, and sets
4140 IT->n_overlay_strings to the total number of strings to process.
4141 IT->pos.overlay_string_index has to be set temporarily to zero
4142 because load_overlay_strings needs this; it must be set to -1
4143 when no overlay strings are found because a zero value would
4144 indicate a position in the first overlay string. */
4145 it->current.overlay_string_index = 0;
4146 load_overlay_strings (it, charpos);
4147
4148 /* If we found overlay strings, set up IT to deliver display
4149 elements from the first one. Otherwise set up IT to deliver
4150 from current_buffer. */
4151 if (it->n_overlay_strings)
4152 {
4153 /* Make sure we know settings in current_buffer, so that we can
4154 restore meaningful values when we're done with the overlay
4155 strings. */
4156 compute_stop_pos (it);
4157 xassert (it->face_id >= 0);
4158
4159 /* Save IT's settings. They are restored after all overlay
4160 strings have been processed. */
4161 xassert (it->sp == 0);
4162 push_it (it);
4163
4164 /* Set up IT to deliver display elements from the first overlay
4165 string. */
4166 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4167 it->string = it->overlay_strings[0];
4168 it->stop_charpos = 0;
4169 xassert (STRINGP (it->string));
4170 it->end_charpos = SCHARS (it->string);
4171 it->multibyte_p = STRING_MULTIBYTE (it->string);
4172 it->method = next_element_from_string;
4173 }
4174 else
4175 {
4176 it->string = Qnil;
4177 it->current.overlay_string_index = -1;
4178 it->method = next_element_from_buffer;
4179 }
4180
4181 CHECK_IT (it);
4182
4183 /* Value is non-zero if we found at least one overlay string. */
4184 return STRINGP (it->string);
4185 }
4186
4187
4188 \f
4189 /***********************************************************************
4190 Saving and restoring state
4191 ***********************************************************************/
4192
4193 /* Save current settings of IT on IT->stack. Called, for example,
4194 before setting up IT for an overlay string, to be able to restore
4195 IT's settings to what they were after the overlay string has been
4196 processed. */
4197
4198 static void
4199 push_it (it)
4200 struct it *it;
4201 {
4202 struct iterator_stack_entry *p;
4203
4204 xassert (it->sp < 2);
4205 p = it->stack + it->sp;
4206
4207 p->stop_charpos = it->stop_charpos;
4208 xassert (it->face_id >= 0);
4209 p->face_id = it->face_id;
4210 p->string = it->string;
4211 p->pos = it->current;
4212 p->end_charpos = it->end_charpos;
4213 p->string_nchars = it->string_nchars;
4214 p->area = it->area;
4215 p->multibyte_p = it->multibyte_p;
4216 p->space_width = it->space_width;
4217 p->font_height = it->font_height;
4218 p->voffset = it->voffset;
4219 p->string_from_display_prop_p = it->string_from_display_prop_p;
4220 p->display_ellipsis_p = 0;
4221 ++it->sp;
4222 }
4223
4224
4225 /* Restore IT's settings from IT->stack. Called, for example, when no
4226 more overlay strings must be processed, and we return to delivering
4227 display elements from a buffer, or when the end of a string from a
4228 `display' property is reached and we return to delivering display
4229 elements from an overlay string, or from a buffer. */
4230
4231 static void
4232 pop_it (it)
4233 struct it *it;
4234 {
4235 struct iterator_stack_entry *p;
4236
4237 xassert (it->sp > 0);
4238 --it->sp;
4239 p = it->stack + it->sp;
4240 it->stop_charpos = p->stop_charpos;
4241 it->face_id = p->face_id;
4242 it->string = p->string;
4243 it->current = p->pos;
4244 it->end_charpos = p->end_charpos;
4245 it->string_nchars = p->string_nchars;
4246 it->area = p->area;
4247 it->multibyte_p = p->multibyte_p;
4248 it->space_width = p->space_width;
4249 it->font_height = p->font_height;
4250 it->voffset = p->voffset;
4251 it->string_from_display_prop_p = p->string_from_display_prop_p;
4252 }
4253
4254
4255 \f
4256 /***********************************************************************
4257 Moving over lines
4258 ***********************************************************************/
4259
4260 /* Set IT's current position to the previous line start. */
4261
4262 static void
4263 back_to_previous_line_start (it)
4264 struct it *it;
4265 {
4266 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4267 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4268 }
4269
4270
4271 /* Move IT to the next line start.
4272
4273 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4274 we skipped over part of the text (as opposed to moving the iterator
4275 continuously over the text). Otherwise, don't change the value
4276 of *SKIPPED_P.
4277
4278 Newlines may come from buffer text, overlay strings, or strings
4279 displayed via the `display' property. That's the reason we can't
4280 simply use find_next_newline_no_quit.
4281
4282 Note that this function may not skip over invisible text that is so
4283 because of text properties and immediately follows a newline. If
4284 it would, function reseat_at_next_visible_line_start, when called
4285 from set_iterator_to_next, would effectively make invisible
4286 characters following a newline part of the wrong glyph row, which
4287 leads to wrong cursor motion. */
4288
4289 static int
4290 forward_to_next_line_start (it, skipped_p)
4291 struct it *it;
4292 int *skipped_p;
4293 {
4294 int old_selective, newline_found_p, n;
4295 const int MAX_NEWLINE_DISTANCE = 500;
4296
4297 /* If already on a newline, just consume it to avoid unintended
4298 skipping over invisible text below. */
4299 if (it->what == IT_CHARACTER
4300 && it->c == '\n'
4301 && CHARPOS (it->position) == IT_CHARPOS (*it))
4302 {
4303 set_iterator_to_next (it, 0);
4304 it->c = 0;
4305 return 1;
4306 }
4307
4308 /* Don't handle selective display in the following. It's (a)
4309 unnecessary because it's done by the caller, and (b) leads to an
4310 infinite recursion because next_element_from_ellipsis indirectly
4311 calls this function. */
4312 old_selective = it->selective;
4313 it->selective = 0;
4314
4315 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4316 from buffer text. */
4317 for (n = newline_found_p = 0;
4318 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4319 n += STRINGP (it->string) ? 0 : 1)
4320 {
4321 if (!get_next_display_element (it))
4322 return 0;
4323 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4324 set_iterator_to_next (it, 0);
4325 }
4326
4327 /* If we didn't find a newline near enough, see if we can use a
4328 short-cut. */
4329 if (!newline_found_p)
4330 {
4331 int start = IT_CHARPOS (*it);
4332 int limit = find_next_newline_no_quit (start, 1);
4333 Lisp_Object pos;
4334
4335 xassert (!STRINGP (it->string));
4336
4337 /* If there isn't any `display' property in sight, and no
4338 overlays, we can just use the position of the newline in
4339 buffer text. */
4340 if (it->stop_charpos >= limit
4341 || ((pos = Fnext_single_property_change (make_number (start),
4342 Qdisplay,
4343 Qnil, make_number (limit)),
4344 NILP (pos))
4345 && next_overlay_change (start) == ZV))
4346 {
4347 IT_CHARPOS (*it) = limit;
4348 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4349 *skipped_p = newline_found_p = 1;
4350 }
4351 else
4352 {
4353 while (get_next_display_element (it)
4354 && !newline_found_p)
4355 {
4356 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4357 set_iterator_to_next (it, 0);
4358 }
4359 }
4360 }
4361
4362 it->selective = old_selective;
4363 return newline_found_p;
4364 }
4365
4366
4367 /* Set IT's current position to the previous visible line start. Skip
4368 invisible text that is so either due to text properties or due to
4369 selective display. Caution: this does not change IT->current_x and
4370 IT->hpos. */
4371
4372 static void
4373 back_to_previous_visible_line_start (it)
4374 struct it *it;
4375 {
4376 int visible_p = 0;
4377
4378 /* Go back one newline if not on BEGV already. */
4379 if (IT_CHARPOS (*it) > BEGV)
4380 back_to_previous_line_start (it);
4381
4382 /* Move over lines that are invisible because of selective display
4383 or text properties. */
4384 while (IT_CHARPOS (*it) > BEGV
4385 && !visible_p)
4386 {
4387 visible_p = 1;
4388
4389 /* If selective > 0, then lines indented more than that values
4390 are invisible. */
4391 if (it->selective > 0
4392 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4393 (double) it->selective)) /* iftc */
4394 visible_p = 0;
4395 else
4396 {
4397 Lisp_Object prop;
4398
4399 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
4400 Qinvisible, it->window);
4401 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4402 visible_p = 0;
4403 }
4404
4405 /* Back one more newline if the current one is invisible. */
4406 if (!visible_p)
4407 back_to_previous_line_start (it);
4408 }
4409
4410 xassert (IT_CHARPOS (*it) >= BEGV);
4411 xassert (IT_CHARPOS (*it) == BEGV
4412 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4413 CHECK_IT (it);
4414 }
4415
4416
4417 /* Reseat iterator IT at the previous visible line start. Skip
4418 invisible text that is so either due to text properties or due to
4419 selective display. At the end, update IT's overlay information,
4420 face information etc. */
4421
4422 static void
4423 reseat_at_previous_visible_line_start (it)
4424 struct it *it;
4425 {
4426 back_to_previous_visible_line_start (it);
4427 reseat (it, it->current.pos, 1);
4428 CHECK_IT (it);
4429 }
4430
4431
4432 /* Reseat iterator IT on the next visible line start in the current
4433 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4434 preceding the line start. Skip over invisible text that is so
4435 because of selective display. Compute faces, overlays etc at the
4436 new position. Note that this function does not skip over text that
4437 is invisible because of text properties. */
4438
4439 static void
4440 reseat_at_next_visible_line_start (it, on_newline_p)
4441 struct it *it;
4442 int on_newline_p;
4443 {
4444 int newline_found_p, skipped_p = 0;
4445
4446 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4447
4448 /* Skip over lines that are invisible because they are indented
4449 more than the value of IT->selective. */
4450 if (it->selective > 0)
4451 while (IT_CHARPOS (*it) < ZV
4452 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4453 (double) it->selective)) /* iftc */
4454 {
4455 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4456 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4457 }
4458
4459 /* Position on the newline if that's what's requested. */
4460 if (on_newline_p && newline_found_p)
4461 {
4462 if (STRINGP (it->string))
4463 {
4464 if (IT_STRING_CHARPOS (*it) > 0)
4465 {
4466 --IT_STRING_CHARPOS (*it);
4467 --IT_STRING_BYTEPOS (*it);
4468 }
4469 }
4470 else if (IT_CHARPOS (*it) > BEGV)
4471 {
4472 --IT_CHARPOS (*it);
4473 --IT_BYTEPOS (*it);
4474 reseat (it, it->current.pos, 0);
4475 }
4476 }
4477 else if (skipped_p)
4478 reseat (it, it->current.pos, 0);
4479
4480 CHECK_IT (it);
4481 }
4482
4483
4484 \f
4485 /***********************************************************************
4486 Changing an iterator's position
4487 ***********************************************************************/
4488
4489 /* Change IT's current position to POS in current_buffer. If FORCE_P
4490 is non-zero, always check for text properties at the new position.
4491 Otherwise, text properties are only looked up if POS >=
4492 IT->check_charpos of a property. */
4493
4494 static void
4495 reseat (it, pos, force_p)
4496 struct it *it;
4497 struct text_pos pos;
4498 int force_p;
4499 {
4500 int original_pos = IT_CHARPOS (*it);
4501
4502 reseat_1 (it, pos, 0);
4503
4504 /* Determine where to check text properties. Avoid doing it
4505 where possible because text property lookup is very expensive. */
4506 if (force_p
4507 || CHARPOS (pos) > it->stop_charpos
4508 || CHARPOS (pos) < original_pos)
4509 handle_stop (it);
4510
4511 CHECK_IT (it);
4512 }
4513
4514
4515 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4516 IT->stop_pos to POS, also. */
4517
4518 static void
4519 reseat_1 (it, pos, set_stop_p)
4520 struct it *it;
4521 struct text_pos pos;
4522 int set_stop_p;
4523 {
4524 /* Don't call this function when scanning a C string. */
4525 xassert (it->s == NULL);
4526
4527 /* POS must be a reasonable value. */
4528 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4529
4530 it->current.pos = it->position = pos;
4531 XSETBUFFER (it->object, current_buffer);
4532 it->end_charpos = ZV;
4533 it->dpvec = NULL;
4534 it->current.dpvec_index = -1;
4535 it->current.overlay_string_index = -1;
4536 IT_STRING_CHARPOS (*it) = -1;
4537 IT_STRING_BYTEPOS (*it) = -1;
4538 it->string = Qnil;
4539 it->method = next_element_from_buffer;
4540 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4541 it->sp = 0;
4542 it->face_before_selective_p = 0;
4543
4544 if (set_stop_p)
4545 it->stop_charpos = CHARPOS (pos);
4546 }
4547
4548
4549 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4550 If S is non-null, it is a C string to iterate over. Otherwise,
4551 STRING gives a Lisp string to iterate over.
4552
4553 If PRECISION > 0, don't return more then PRECISION number of
4554 characters from the string.
4555
4556 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4557 characters have been returned. FIELD_WIDTH < 0 means an infinite
4558 field width.
4559
4560 MULTIBYTE = 0 means disable processing of multibyte characters,
4561 MULTIBYTE > 0 means enable it,
4562 MULTIBYTE < 0 means use IT->multibyte_p.
4563
4564 IT must be initialized via a prior call to init_iterator before
4565 calling this function. */
4566
4567 static void
4568 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4569 struct it *it;
4570 unsigned char *s;
4571 Lisp_Object string;
4572 int charpos;
4573 int precision, field_width, multibyte;
4574 {
4575 /* No region in strings. */
4576 it->region_beg_charpos = it->region_end_charpos = -1;
4577
4578 /* No text property checks performed by default, but see below. */
4579 it->stop_charpos = -1;
4580
4581 /* Set iterator position and end position. */
4582 bzero (&it->current, sizeof it->current);
4583 it->current.overlay_string_index = -1;
4584 it->current.dpvec_index = -1;
4585 xassert (charpos >= 0);
4586
4587 /* If STRING is specified, use its multibyteness, otherwise use the
4588 setting of MULTIBYTE, if specified. */
4589 if (multibyte >= 0)
4590 it->multibyte_p = multibyte > 0;
4591
4592 if (s == NULL)
4593 {
4594 xassert (STRINGP (string));
4595 it->string = string;
4596 it->s = NULL;
4597 it->end_charpos = it->string_nchars = SCHARS (string);
4598 it->method = next_element_from_string;
4599 it->current.string_pos = string_pos (charpos, string);
4600 }
4601 else
4602 {
4603 it->s = s;
4604 it->string = Qnil;
4605
4606 /* Note that we use IT->current.pos, not it->current.string_pos,
4607 for displaying C strings. */
4608 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4609 if (it->multibyte_p)
4610 {
4611 it->current.pos = c_string_pos (charpos, s, 1);
4612 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4613 }
4614 else
4615 {
4616 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4617 it->end_charpos = it->string_nchars = strlen (s);
4618 }
4619
4620 it->method = next_element_from_c_string;
4621 }
4622
4623 /* PRECISION > 0 means don't return more than PRECISION characters
4624 from the string. */
4625 if (precision > 0 && it->end_charpos - charpos > precision)
4626 it->end_charpos = it->string_nchars = charpos + precision;
4627
4628 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4629 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4630 FIELD_WIDTH < 0 means infinite field width. This is useful for
4631 padding with `-' at the end of a mode line. */
4632 if (field_width < 0)
4633 field_width = INFINITY;
4634 if (field_width > it->end_charpos - charpos)
4635 it->end_charpos = charpos + field_width;
4636
4637 /* Use the standard display table for displaying strings. */
4638 if (DISP_TABLE_P (Vstandard_display_table))
4639 it->dp = XCHAR_TABLE (Vstandard_display_table);
4640
4641 it->stop_charpos = charpos;
4642 CHECK_IT (it);
4643 }
4644
4645
4646 \f
4647 /***********************************************************************
4648 Iteration
4649 ***********************************************************************/
4650
4651 /* Load IT's display element fields with information about the next
4652 display element from the current position of IT. Value is zero if
4653 end of buffer (or C string) is reached. */
4654
4655 int
4656 get_next_display_element (it)
4657 struct it *it;
4658 {
4659 /* Non-zero means that we found a display element. Zero means that
4660 we hit the end of what we iterate over. Performance note: the
4661 function pointer `method' used here turns out to be faster than
4662 using a sequence of if-statements. */
4663 int success_p = (*it->method) (it);
4664
4665 if (it->what == IT_CHARACTER)
4666 {
4667 /* Map via display table or translate control characters.
4668 IT->c, IT->len etc. have been set to the next character by
4669 the function call above. If we have a display table, and it
4670 contains an entry for IT->c, translate it. Don't do this if
4671 IT->c itself comes from a display table, otherwise we could
4672 end up in an infinite recursion. (An alternative could be to
4673 count the recursion depth of this function and signal an
4674 error when a certain maximum depth is reached.) Is it worth
4675 it? */
4676 if (success_p && it->dpvec == NULL)
4677 {
4678 Lisp_Object dv;
4679
4680 if (it->dp
4681 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4682 VECTORP (dv)))
4683 {
4684 struct Lisp_Vector *v = XVECTOR (dv);
4685
4686 /* Return the first character from the display table
4687 entry, if not empty. If empty, don't display the
4688 current character. */
4689 if (v->size)
4690 {
4691 it->dpvec_char_len = it->len;
4692 it->dpvec = v->contents;
4693 it->dpend = v->contents + v->size;
4694 it->current.dpvec_index = 0;
4695 it->method = next_element_from_display_vector;
4696 success_p = get_next_display_element (it);
4697 }
4698 else
4699 {
4700 set_iterator_to_next (it, 0);
4701 success_p = get_next_display_element (it);
4702 }
4703 }
4704
4705 /* Translate control characters into `\003' or `^C' form.
4706 Control characters coming from a display table entry are
4707 currently not translated because we use IT->dpvec to hold
4708 the translation. This could easily be changed but I
4709 don't believe that it is worth doing.
4710
4711 If it->multibyte_p is nonzero, eight-bit characters and
4712 non-printable multibyte characters are also translated to
4713 octal form.
4714
4715 If it->multibyte_p is zero, eight-bit characters that
4716 don't have corresponding multibyte char code are also
4717 translated to octal form. */
4718 else if ((it->c < ' '
4719 && (it->area != TEXT_AREA
4720 || (it->c != '\n' && it->c != '\t')))
4721 || (it->multibyte_p
4722 ? ((it->c >= 127
4723 && it->len == 1)
4724 || !CHAR_PRINTABLE_P (it->c))
4725 : (it->c >= 127
4726 && it->c == unibyte_char_to_multibyte (it->c))))
4727 {
4728 /* IT->c is a control character which must be displayed
4729 either as '\003' or as `^C' where the '\\' and '^'
4730 can be defined in the display table. Fill
4731 IT->ctl_chars with glyphs for what we have to
4732 display. Then, set IT->dpvec to these glyphs. */
4733 GLYPH g;
4734
4735 if (it->c < 128 && it->ctl_arrow_p)
4736 {
4737 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4738 if (it->dp
4739 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4740 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4741 g = XINT (DISP_CTRL_GLYPH (it->dp));
4742 else
4743 g = FAST_MAKE_GLYPH ('^', 0);
4744 XSETINT (it->ctl_chars[0], g);
4745
4746 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4747 XSETINT (it->ctl_chars[1], g);
4748
4749 /* Set up IT->dpvec and return first character from it. */
4750 it->dpvec_char_len = it->len;
4751 it->dpvec = it->ctl_chars;
4752 it->dpend = it->dpvec + 2;
4753 it->current.dpvec_index = 0;
4754 it->method = next_element_from_display_vector;
4755 get_next_display_element (it);
4756 }
4757 else
4758 {
4759 unsigned char str[MAX_MULTIBYTE_LENGTH];
4760 int len;
4761 int i;
4762 GLYPH escape_glyph;
4763
4764 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4765 if (it->dp
4766 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4767 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4768 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4769 else
4770 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4771
4772 if (SINGLE_BYTE_CHAR_P (it->c))
4773 str[0] = it->c, len = 1;
4774 else
4775 {
4776 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4777 if (len < 0)
4778 {
4779 /* It's an invalid character, which
4780 shouldn't happen actually, but due to
4781 bugs it may happen. Let's print the char
4782 as is, there's not much meaningful we can
4783 do with it. */
4784 str[0] = it->c;
4785 str[1] = it->c >> 8;
4786 str[2] = it->c >> 16;
4787 str[3] = it->c >> 24;
4788 len = 4;
4789 }
4790 }
4791
4792 for (i = 0; i < len; i++)
4793 {
4794 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4795 /* Insert three more glyphs into IT->ctl_chars for
4796 the octal display of the character. */
4797 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4798 XSETINT (it->ctl_chars[i * 4 + 1], g);
4799 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4800 XSETINT (it->ctl_chars[i * 4 + 2], g);
4801 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4802 XSETINT (it->ctl_chars[i * 4 + 3], g);
4803 }
4804
4805 /* Set up IT->dpvec and return the first character
4806 from it. */
4807 it->dpvec_char_len = it->len;
4808 it->dpvec = it->ctl_chars;
4809 it->dpend = it->dpvec + len * 4;
4810 it->current.dpvec_index = 0;
4811 it->method = next_element_from_display_vector;
4812 get_next_display_element (it);
4813 }
4814 }
4815 }
4816
4817 /* Adjust face id for a multibyte character. There are no
4818 multibyte character in unibyte text. */
4819 if (it->multibyte_p
4820 && success_p
4821 && FRAME_WINDOW_P (it->f))
4822 {
4823 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4824 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4825 }
4826 }
4827
4828 /* Is this character the last one of a run of characters with
4829 box? If yes, set IT->end_of_box_run_p to 1. */
4830 if (it->face_box_p
4831 && it->s == NULL)
4832 {
4833 int face_id;
4834 struct face *face;
4835
4836 it->end_of_box_run_p
4837 = ((face_id = face_after_it_pos (it),
4838 face_id != it->face_id)
4839 && (face = FACE_FROM_ID (it->f, face_id),
4840 face->box == FACE_NO_BOX));
4841 }
4842
4843 /* Value is 0 if end of buffer or string reached. */
4844 return success_p;
4845 }
4846
4847
4848 /* Move IT to the next display element.
4849
4850 RESEAT_P non-zero means if called on a newline in buffer text,
4851 skip to the next visible line start.
4852
4853 Functions get_next_display_element and set_iterator_to_next are
4854 separate because I find this arrangement easier to handle than a
4855 get_next_display_element function that also increments IT's
4856 position. The way it is we can first look at an iterator's current
4857 display element, decide whether it fits on a line, and if it does,
4858 increment the iterator position. The other way around we probably
4859 would either need a flag indicating whether the iterator has to be
4860 incremented the next time, or we would have to implement a
4861 decrement position function which would not be easy to write. */
4862
4863 void
4864 set_iterator_to_next (it, reseat_p)
4865 struct it *it;
4866 int reseat_p;
4867 {
4868 /* Reset flags indicating start and end of a sequence of characters
4869 with box. Reset them at the start of this function because
4870 moving the iterator to a new position might set them. */
4871 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4872
4873 if (it->method == next_element_from_buffer)
4874 {
4875 /* The current display element of IT is a character from
4876 current_buffer. Advance in the buffer, and maybe skip over
4877 invisible lines that are so because of selective display. */
4878 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4879 reseat_at_next_visible_line_start (it, 0);
4880 else
4881 {
4882 xassert (it->len != 0);
4883 IT_BYTEPOS (*it) += it->len;
4884 IT_CHARPOS (*it) += 1;
4885 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4886 }
4887 }
4888 else if (it->method == next_element_from_composition)
4889 {
4890 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4891 if (STRINGP (it->string))
4892 {
4893 IT_STRING_BYTEPOS (*it) += it->len;
4894 IT_STRING_CHARPOS (*it) += it->cmp_len;
4895 it->method = next_element_from_string;
4896 goto consider_string_end;
4897 }
4898 else
4899 {
4900 IT_BYTEPOS (*it) += it->len;
4901 IT_CHARPOS (*it) += it->cmp_len;
4902 it->method = next_element_from_buffer;
4903 }
4904 }
4905 else if (it->method == next_element_from_c_string)
4906 {
4907 /* Current display element of IT is from a C string. */
4908 IT_BYTEPOS (*it) += it->len;
4909 IT_CHARPOS (*it) += 1;
4910 }
4911 else if (it->method == next_element_from_display_vector)
4912 {
4913 /* Current display element of IT is from a display table entry.
4914 Advance in the display table definition. Reset it to null if
4915 end reached, and continue with characters from buffers/
4916 strings. */
4917 ++it->current.dpvec_index;
4918
4919 /* Restore face of the iterator to what they were before the
4920 display vector entry (these entries may contain faces). */
4921 it->face_id = it->saved_face_id;
4922
4923 if (it->dpvec + it->current.dpvec_index == it->dpend)
4924 {
4925 if (it->s)
4926 it->method = next_element_from_c_string;
4927 else if (STRINGP (it->string))
4928 it->method = next_element_from_string;
4929 else
4930 it->method = next_element_from_buffer;
4931
4932 it->dpvec = NULL;
4933 it->current.dpvec_index = -1;
4934
4935 /* Skip over characters which were displayed via IT->dpvec. */
4936 if (it->dpvec_char_len < 0)
4937 reseat_at_next_visible_line_start (it, 1);
4938 else if (it->dpvec_char_len > 0)
4939 {
4940 it->len = it->dpvec_char_len;
4941 set_iterator_to_next (it, reseat_p);
4942 }
4943 }
4944 }
4945 else if (it->method == next_element_from_string)
4946 {
4947 /* Current display element is a character from a Lisp string. */
4948 xassert (it->s == NULL && STRINGP (it->string));
4949 IT_STRING_BYTEPOS (*it) += it->len;
4950 IT_STRING_CHARPOS (*it) += 1;
4951
4952 consider_string_end:
4953
4954 if (it->current.overlay_string_index >= 0)
4955 {
4956 /* IT->string is an overlay string. Advance to the
4957 next, if there is one. */
4958 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
4959 next_overlay_string (it);
4960 }
4961 else
4962 {
4963 /* IT->string is not an overlay string. If we reached
4964 its end, and there is something on IT->stack, proceed
4965 with what is on the stack. This can be either another
4966 string, this time an overlay string, or a buffer. */
4967 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
4968 && it->sp > 0)
4969 {
4970 pop_it (it);
4971 if (!STRINGP (it->string))
4972 it->method = next_element_from_buffer;
4973 else
4974 goto consider_string_end;
4975 }
4976 }
4977 }
4978 else if (it->method == next_element_from_image
4979 || it->method == next_element_from_stretch)
4980 {
4981 /* The position etc with which we have to proceed are on
4982 the stack. The position may be at the end of a string,
4983 if the `display' property takes up the whole string. */
4984 pop_it (it);
4985 it->image_id = 0;
4986 if (STRINGP (it->string))
4987 {
4988 it->method = next_element_from_string;
4989 goto consider_string_end;
4990 }
4991 else
4992 it->method = next_element_from_buffer;
4993 }
4994 else
4995 /* There are no other methods defined, so this should be a bug. */
4996 abort ();
4997
4998 xassert (it->method != next_element_from_string
4999 || (STRINGP (it->string)
5000 && IT_STRING_CHARPOS (*it) >= 0));
5001 }
5002
5003
5004 /* Load IT's display element fields with information about the next
5005 display element which comes from a display table entry or from the
5006 result of translating a control character to one of the forms `^C'
5007 or `\003'. IT->dpvec holds the glyphs to return as characters. */
5008
5009 static int
5010 next_element_from_display_vector (it)
5011 struct it *it;
5012 {
5013 /* Precondition. */
5014 xassert (it->dpvec && it->current.dpvec_index >= 0);
5015
5016 /* Remember the current face id in case glyphs specify faces.
5017 IT's face is restored in set_iterator_to_next. */
5018 it->saved_face_id = it->face_id;
5019
5020 if (INTEGERP (*it->dpvec)
5021 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5022 {
5023 int lface_id;
5024 GLYPH g;
5025
5026 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5027 it->c = FAST_GLYPH_CHAR (g);
5028 it->len = CHAR_BYTES (it->c);
5029
5030 /* The entry may contain a face id to use. Such a face id is
5031 the id of a Lisp face, not a realized face. A face id of
5032 zero means no face is specified. */
5033 lface_id = FAST_GLYPH_FACE (g);
5034 if (lface_id)
5035 {
5036 /* The function returns -1 if lface_id is invalid. */
5037 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
5038 if (face_id >= 0)
5039 it->face_id = face_id;
5040 }
5041 }
5042 else
5043 /* Display table entry is invalid. Return a space. */
5044 it->c = ' ', it->len = 1;
5045
5046 /* Don't change position and object of the iterator here. They are
5047 still the values of the character that had this display table
5048 entry or was translated, and that's what we want. */
5049 it->what = IT_CHARACTER;
5050 return 1;
5051 }
5052
5053
5054 /* Load IT with the next display element from Lisp string IT->string.
5055 IT->current.string_pos is the current position within the string.
5056 If IT->current.overlay_string_index >= 0, the Lisp string is an
5057 overlay string. */
5058
5059 static int
5060 next_element_from_string (it)
5061 struct it *it;
5062 {
5063 struct text_pos position;
5064
5065 xassert (STRINGP (it->string));
5066 xassert (IT_STRING_CHARPOS (*it) >= 0);
5067 position = it->current.string_pos;
5068
5069 /* Time to check for invisible text? */
5070 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5071 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5072 {
5073 handle_stop (it);
5074
5075 /* Since a handler may have changed IT->method, we must
5076 recurse here. */
5077 return get_next_display_element (it);
5078 }
5079
5080 if (it->current.overlay_string_index >= 0)
5081 {
5082 /* Get the next character from an overlay string. In overlay
5083 strings, There is no field width or padding with spaces to
5084 do. */
5085 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5086 {
5087 it->what = IT_EOB;
5088 return 0;
5089 }
5090 else if (STRING_MULTIBYTE (it->string))
5091 {
5092 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5093 const unsigned char *s = (SDATA (it->string)
5094 + IT_STRING_BYTEPOS (*it));
5095 it->c = string_char_and_length (s, remaining, &it->len);
5096 }
5097 else
5098 {
5099 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5100 it->len = 1;
5101 }
5102 }
5103 else
5104 {
5105 /* Get the next character from a Lisp string that is not an
5106 overlay string. Such strings come from the mode line, for
5107 example. We may have to pad with spaces, or truncate the
5108 string. See also next_element_from_c_string. */
5109 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5110 {
5111 it->what = IT_EOB;
5112 return 0;
5113 }
5114 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5115 {
5116 /* Pad with spaces. */
5117 it->c = ' ', it->len = 1;
5118 CHARPOS (position) = BYTEPOS (position) = -1;
5119 }
5120 else if (STRING_MULTIBYTE (it->string))
5121 {
5122 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5123 const unsigned char *s = (SDATA (it->string)
5124 + IT_STRING_BYTEPOS (*it));
5125 it->c = string_char_and_length (s, maxlen, &it->len);
5126 }
5127 else
5128 {
5129 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5130 it->len = 1;
5131 }
5132 }
5133
5134 /* Record what we have and where it came from. Note that we store a
5135 buffer position in IT->position although it could arguably be a
5136 string position. */
5137 it->what = IT_CHARACTER;
5138 it->object = it->string;
5139 it->position = position;
5140 return 1;
5141 }
5142
5143
5144 /* Load IT with next display element from C string IT->s.
5145 IT->string_nchars is the maximum number of characters to return
5146 from the string. IT->end_charpos may be greater than
5147 IT->string_nchars when this function is called, in which case we
5148 may have to return padding spaces. Value is zero if end of string
5149 reached, including padding spaces. */
5150
5151 static int
5152 next_element_from_c_string (it)
5153 struct it *it;
5154 {
5155 int success_p = 1;
5156
5157 xassert (it->s);
5158 it->what = IT_CHARACTER;
5159 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5160 it->object = Qnil;
5161
5162 /* IT's position can be greater IT->string_nchars in case a field
5163 width or precision has been specified when the iterator was
5164 initialized. */
5165 if (IT_CHARPOS (*it) >= it->end_charpos)
5166 {
5167 /* End of the game. */
5168 it->what = IT_EOB;
5169 success_p = 0;
5170 }
5171 else if (IT_CHARPOS (*it) >= it->string_nchars)
5172 {
5173 /* Pad with spaces. */
5174 it->c = ' ', it->len = 1;
5175 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5176 }
5177 else if (it->multibyte_p)
5178 {
5179 /* Implementation note: The calls to strlen apparently aren't a
5180 performance problem because there is no noticeable performance
5181 difference between Emacs running in unibyte or multibyte mode. */
5182 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5183 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5184 maxlen, &it->len);
5185 }
5186 else
5187 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5188
5189 return success_p;
5190 }
5191
5192
5193 /* Set up IT to return characters from an ellipsis, if appropriate.
5194 The definition of the ellipsis glyphs may come from a display table
5195 entry. This function Fills IT with the first glyph from the
5196 ellipsis if an ellipsis is to be displayed. */
5197
5198 static int
5199 next_element_from_ellipsis (it)
5200 struct it *it;
5201 {
5202 if (it->selective_display_ellipsis_p)
5203 {
5204 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
5205 {
5206 /* Use the display table definition for `...'. Invalid glyphs
5207 will be handled by the method returning elements from dpvec. */
5208 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
5209 it->dpvec_char_len = it->len;
5210 it->dpvec = v->contents;
5211 it->dpend = v->contents + v->size;
5212 it->current.dpvec_index = 0;
5213 it->method = next_element_from_display_vector;
5214 }
5215 else
5216 {
5217 /* Use default `...' which is stored in default_invis_vector. */
5218 it->dpvec_char_len = it->len;
5219 it->dpvec = default_invis_vector;
5220 it->dpend = default_invis_vector + 3;
5221 it->current.dpvec_index = 0;
5222 it->method = next_element_from_display_vector;
5223 }
5224 }
5225 else
5226 {
5227 /* The face at the current position may be different from the
5228 face we find after the invisible text. Remember what it
5229 was in IT->saved_face_id, and signal that it's there by
5230 setting face_before_selective_p. */
5231 it->saved_face_id = it->face_id;
5232 it->method = next_element_from_buffer;
5233 reseat_at_next_visible_line_start (it, 1);
5234 it->face_before_selective_p = 1;
5235 }
5236
5237 return get_next_display_element (it);
5238 }
5239
5240
5241 /* Deliver an image display element. The iterator IT is already
5242 filled with image information (done in handle_display_prop). Value
5243 is always 1. */
5244
5245
5246 static int
5247 next_element_from_image (it)
5248 struct it *it;
5249 {
5250 it->what = IT_IMAGE;
5251 return 1;
5252 }
5253
5254
5255 /* Fill iterator IT with next display element from a stretch glyph
5256 property. IT->object is the value of the text property. Value is
5257 always 1. */
5258
5259 static int
5260 next_element_from_stretch (it)
5261 struct it *it;
5262 {
5263 it->what = IT_STRETCH;
5264 return 1;
5265 }
5266
5267
5268 /* Load IT with the next display element from current_buffer. Value
5269 is zero if end of buffer reached. IT->stop_charpos is the next
5270 position at which to stop and check for text properties or buffer
5271 end. */
5272
5273 static int
5274 next_element_from_buffer (it)
5275 struct it *it;
5276 {
5277 int success_p = 1;
5278
5279 /* Check this assumption, otherwise, we would never enter the
5280 if-statement, below. */
5281 xassert (IT_CHARPOS (*it) >= BEGV
5282 && IT_CHARPOS (*it) <= it->stop_charpos);
5283
5284 if (IT_CHARPOS (*it) >= it->stop_charpos)
5285 {
5286 if (IT_CHARPOS (*it) >= it->end_charpos)
5287 {
5288 int overlay_strings_follow_p;
5289
5290 /* End of the game, except when overlay strings follow that
5291 haven't been returned yet. */
5292 if (it->overlay_strings_at_end_processed_p)
5293 overlay_strings_follow_p = 0;
5294 else
5295 {
5296 it->overlay_strings_at_end_processed_p = 1;
5297 overlay_strings_follow_p = get_overlay_strings (it, 0);
5298 }
5299
5300 if (overlay_strings_follow_p)
5301 success_p = get_next_display_element (it);
5302 else
5303 {
5304 it->what = IT_EOB;
5305 it->position = it->current.pos;
5306 success_p = 0;
5307 }
5308 }
5309 else
5310 {
5311 handle_stop (it);
5312 return get_next_display_element (it);
5313 }
5314 }
5315 else
5316 {
5317 /* No face changes, overlays etc. in sight, so just return a
5318 character from current_buffer. */
5319 unsigned char *p;
5320
5321 /* Maybe run the redisplay end trigger hook. Performance note:
5322 This doesn't seem to cost measurable time. */
5323 if (it->redisplay_end_trigger_charpos
5324 && it->glyph_row
5325 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5326 run_redisplay_end_trigger_hook (it);
5327
5328 /* Get the next character, maybe multibyte. */
5329 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5330 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5331 {
5332 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5333 - IT_BYTEPOS (*it));
5334 it->c = string_char_and_length (p, maxlen, &it->len);
5335 }
5336 else
5337 it->c = *p, it->len = 1;
5338
5339 /* Record what we have and where it came from. */
5340 it->what = IT_CHARACTER;;
5341 it->object = it->w->buffer;
5342 it->position = it->current.pos;
5343
5344 /* Normally we return the character found above, except when we
5345 really want to return an ellipsis for selective display. */
5346 if (it->selective)
5347 {
5348 if (it->c == '\n')
5349 {
5350 /* A value of selective > 0 means hide lines indented more
5351 than that number of columns. */
5352 if (it->selective > 0
5353 && IT_CHARPOS (*it) + 1 < ZV
5354 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5355 IT_BYTEPOS (*it) + 1,
5356 (double) it->selective)) /* iftc */
5357 {
5358 success_p = next_element_from_ellipsis (it);
5359 it->dpvec_char_len = -1;
5360 }
5361 }
5362 else if (it->c == '\r' && it->selective == -1)
5363 {
5364 /* A value of selective == -1 means that everything from the
5365 CR to the end of the line is invisible, with maybe an
5366 ellipsis displayed for it. */
5367 success_p = next_element_from_ellipsis (it);
5368 it->dpvec_char_len = -1;
5369 }
5370 }
5371 }
5372
5373 /* Value is zero if end of buffer reached. */
5374 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5375 return success_p;
5376 }
5377
5378
5379 /* Run the redisplay end trigger hook for IT. */
5380
5381 static void
5382 run_redisplay_end_trigger_hook (it)
5383 struct it *it;
5384 {
5385 Lisp_Object args[3];
5386
5387 /* IT->glyph_row should be non-null, i.e. we should be actually
5388 displaying something, or otherwise we should not run the hook. */
5389 xassert (it->glyph_row);
5390
5391 /* Set up hook arguments. */
5392 args[0] = Qredisplay_end_trigger_functions;
5393 args[1] = it->window;
5394 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5395 it->redisplay_end_trigger_charpos = 0;
5396
5397 /* Since we are *trying* to run these functions, don't try to run
5398 them again, even if they get an error. */
5399 it->w->redisplay_end_trigger = Qnil;
5400 Frun_hook_with_args (3, args);
5401
5402 /* Notice if it changed the face of the character we are on. */
5403 handle_face_prop (it);
5404 }
5405
5406
5407 /* Deliver a composition display element. The iterator IT is already
5408 filled with composition information (done in
5409 handle_composition_prop). Value is always 1. */
5410
5411 static int
5412 next_element_from_composition (it)
5413 struct it *it;
5414 {
5415 it->what = IT_COMPOSITION;
5416 it->position = (STRINGP (it->string)
5417 ? it->current.string_pos
5418 : it->current.pos);
5419 return 1;
5420 }
5421
5422
5423 \f
5424 /***********************************************************************
5425 Moving an iterator without producing glyphs
5426 ***********************************************************************/
5427
5428 /* Move iterator IT to a specified buffer or X position within one
5429 line on the display without producing glyphs.
5430
5431 OP should be a bit mask including some or all of these bits:
5432 MOVE_TO_X: Stop on reaching x-position TO_X.
5433 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5434 Regardless of OP's value, stop in reaching the end of the display line.
5435
5436 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5437 This means, in particular, that TO_X includes window's horizontal
5438 scroll amount.
5439
5440 The return value has several possible values that
5441 say what condition caused the scan to stop:
5442
5443 MOVE_POS_MATCH_OR_ZV
5444 - when TO_POS or ZV was reached.
5445
5446 MOVE_X_REACHED
5447 -when TO_X was reached before TO_POS or ZV were reached.
5448
5449 MOVE_LINE_CONTINUED
5450 - when we reached the end of the display area and the line must
5451 be continued.
5452
5453 MOVE_LINE_TRUNCATED
5454 - when we reached the end of the display area and the line is
5455 truncated.
5456
5457 MOVE_NEWLINE_OR_CR
5458 - when we stopped at a line end, i.e. a newline or a CR and selective
5459 display is on. */
5460
5461 static enum move_it_result
5462 move_it_in_display_line_to (it, to_charpos, to_x, op)
5463 struct it *it;
5464 int to_charpos, to_x, op;
5465 {
5466 enum move_it_result result = MOVE_UNDEFINED;
5467 struct glyph_row *saved_glyph_row;
5468
5469 /* Don't produce glyphs in produce_glyphs. */
5470 saved_glyph_row = it->glyph_row;
5471 it->glyph_row = NULL;
5472
5473 while (1)
5474 {
5475 int x, i, ascent = 0, descent = 0;
5476
5477 /* Stop when ZV or TO_CHARPOS reached. */
5478 if (!get_next_display_element (it)
5479 || ((op & MOVE_TO_POS) != 0
5480 && BUFFERP (it->object)
5481 && IT_CHARPOS (*it) >= to_charpos))
5482 {
5483 result = MOVE_POS_MATCH_OR_ZV;
5484 break;
5485 }
5486
5487 /* The call to produce_glyphs will get the metrics of the
5488 display element IT is loaded with. We record in x the
5489 x-position before this display element in case it does not
5490 fit on the line. */
5491 x = it->current_x;
5492
5493 /* Remember the line height so far in case the next element doesn't
5494 fit on the line. */
5495 if (!it->truncate_lines_p)
5496 {
5497 ascent = it->max_ascent;
5498 descent = it->max_descent;
5499 }
5500
5501 PRODUCE_GLYPHS (it);
5502
5503 if (it->area != TEXT_AREA)
5504 {
5505 set_iterator_to_next (it, 1);
5506 continue;
5507 }
5508
5509 /* The number of glyphs we get back in IT->nglyphs will normally
5510 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5511 character on a terminal frame, or (iii) a line end. For the
5512 second case, IT->nglyphs - 1 padding glyphs will be present
5513 (on X frames, there is only one glyph produced for a
5514 composite character.
5515
5516 The behavior implemented below means, for continuation lines,
5517 that as many spaces of a TAB as fit on the current line are
5518 displayed there. For terminal frames, as many glyphs of a
5519 multi-glyph character are displayed in the current line, too.
5520 This is what the old redisplay code did, and we keep it that
5521 way. Under X, the whole shape of a complex character must
5522 fit on the line or it will be completely displayed in the
5523 next line.
5524
5525 Note that both for tabs and padding glyphs, all glyphs have
5526 the same width. */
5527 if (it->nglyphs)
5528 {
5529 /* More than one glyph or glyph doesn't fit on line. All
5530 glyphs have the same width. */
5531 int single_glyph_width = it->pixel_width / it->nglyphs;
5532 int new_x;
5533
5534 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5535 {
5536 new_x = x + single_glyph_width;
5537
5538 /* We want to leave anything reaching TO_X to the caller. */
5539 if ((op & MOVE_TO_X) && new_x > to_x)
5540 {
5541 it->current_x = x;
5542 result = MOVE_X_REACHED;
5543 break;
5544 }
5545 else if (/* Lines are continued. */
5546 !it->truncate_lines_p
5547 && (/* And glyph doesn't fit on the line. */
5548 new_x > it->last_visible_x
5549 /* Or it fits exactly and we're on a window
5550 system frame. */
5551 || (new_x == it->last_visible_x
5552 && FRAME_WINDOW_P (it->f))))
5553 {
5554 if (/* IT->hpos == 0 means the very first glyph
5555 doesn't fit on the line, e.g. a wide image. */
5556 it->hpos == 0
5557 || (new_x == it->last_visible_x
5558 && FRAME_WINDOW_P (it->f)))
5559 {
5560 ++it->hpos;
5561 it->current_x = new_x;
5562 if (i == it->nglyphs - 1)
5563 set_iterator_to_next (it, 1);
5564 }
5565 else
5566 {
5567 it->current_x = x;
5568 it->max_ascent = ascent;
5569 it->max_descent = descent;
5570 }
5571
5572 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5573 IT_CHARPOS (*it)));
5574 result = MOVE_LINE_CONTINUED;
5575 break;
5576 }
5577 else if (new_x > it->first_visible_x)
5578 {
5579 /* Glyph is visible. Increment number of glyphs that
5580 would be displayed. */
5581 ++it->hpos;
5582 }
5583 else
5584 {
5585 /* Glyph is completely off the left margin of the display
5586 area. Nothing to do. */
5587 }
5588 }
5589
5590 if (result != MOVE_UNDEFINED)
5591 break;
5592 }
5593 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5594 {
5595 /* Stop when TO_X specified and reached. This check is
5596 necessary here because of lines consisting of a line end,
5597 only. The line end will not produce any glyphs and we
5598 would never get MOVE_X_REACHED. */
5599 xassert (it->nglyphs == 0);
5600 result = MOVE_X_REACHED;
5601 break;
5602 }
5603
5604 /* Is this a line end? If yes, we're done. */
5605 if (ITERATOR_AT_END_OF_LINE_P (it))
5606 {
5607 result = MOVE_NEWLINE_OR_CR;
5608 break;
5609 }
5610
5611 /* The current display element has been consumed. Advance
5612 to the next. */
5613 set_iterator_to_next (it, 1);
5614
5615 /* Stop if lines are truncated and IT's current x-position is
5616 past the right edge of the window now. */
5617 if (it->truncate_lines_p
5618 && it->current_x >= it->last_visible_x)
5619 {
5620 result = MOVE_LINE_TRUNCATED;
5621 break;
5622 }
5623 }
5624
5625 /* Restore the iterator settings altered at the beginning of this
5626 function. */
5627 it->glyph_row = saved_glyph_row;
5628 return result;
5629 }
5630
5631
5632 /* Move IT forward until it satisfies one or more of the criteria in
5633 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5634
5635 OP is a bit-mask that specifies where to stop, and in particular,
5636 which of those four position arguments makes a difference. See the
5637 description of enum move_operation_enum.
5638
5639 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5640 screen line, this function will set IT to the next position >
5641 TO_CHARPOS. */
5642
5643 void
5644 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5645 struct it *it;
5646 int to_charpos, to_x, to_y, to_vpos;
5647 int op;
5648 {
5649 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5650 int line_height;
5651 int reached = 0;
5652
5653 for (;;)
5654 {
5655 if (op & MOVE_TO_VPOS)
5656 {
5657 /* If no TO_CHARPOS and no TO_X specified, stop at the
5658 start of the line TO_VPOS. */
5659 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5660 {
5661 if (it->vpos == to_vpos)
5662 {
5663 reached = 1;
5664 break;
5665 }
5666 else
5667 skip = move_it_in_display_line_to (it, -1, -1, 0);
5668 }
5669 else
5670 {
5671 /* TO_VPOS >= 0 means stop at TO_X in the line at
5672 TO_VPOS, or at TO_POS, whichever comes first. */
5673 if (it->vpos == to_vpos)
5674 {
5675 reached = 2;
5676 break;
5677 }
5678
5679 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5680
5681 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5682 {
5683 reached = 3;
5684 break;
5685 }
5686 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5687 {
5688 /* We have reached TO_X but not in the line we want. */
5689 skip = move_it_in_display_line_to (it, to_charpos,
5690 -1, MOVE_TO_POS);
5691 if (skip == MOVE_POS_MATCH_OR_ZV)
5692 {
5693 reached = 4;
5694 break;
5695 }
5696 }
5697 }
5698 }
5699 else if (op & MOVE_TO_Y)
5700 {
5701 struct it it_backup;
5702
5703 /* TO_Y specified means stop at TO_X in the line containing
5704 TO_Y---or at TO_CHARPOS if this is reached first. The
5705 problem is that we can't really tell whether the line
5706 contains TO_Y before we have completely scanned it, and
5707 this may skip past TO_X. What we do is to first scan to
5708 TO_X.
5709
5710 If TO_X is not specified, use a TO_X of zero. The reason
5711 is to make the outcome of this function more predictable.
5712 If we didn't use TO_X == 0, we would stop at the end of
5713 the line which is probably not what a caller would expect
5714 to happen. */
5715 skip = move_it_in_display_line_to (it, to_charpos,
5716 ((op & MOVE_TO_X)
5717 ? to_x : 0),
5718 (MOVE_TO_X
5719 | (op & MOVE_TO_POS)));
5720
5721 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5722 if (skip == MOVE_POS_MATCH_OR_ZV)
5723 {
5724 reached = 5;
5725 break;
5726 }
5727
5728 /* If TO_X was reached, we would like to know whether TO_Y
5729 is in the line. This can only be said if we know the
5730 total line height which requires us to scan the rest of
5731 the line. */
5732 if (skip == MOVE_X_REACHED)
5733 {
5734 it_backup = *it;
5735 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5736 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5737 op & MOVE_TO_POS);
5738 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5739 }
5740
5741 /* Now, decide whether TO_Y is in this line. */
5742 line_height = it->max_ascent + it->max_descent;
5743 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5744
5745 if (to_y >= it->current_y
5746 && to_y < it->current_y + line_height)
5747 {
5748 if (skip == MOVE_X_REACHED)
5749 /* If TO_Y is in this line and TO_X was reached above,
5750 we scanned too far. We have to restore IT's settings
5751 to the ones before skipping. */
5752 *it = it_backup;
5753 reached = 6;
5754 }
5755 else if (skip == MOVE_X_REACHED)
5756 {
5757 skip = skip2;
5758 if (skip == MOVE_POS_MATCH_OR_ZV)
5759 reached = 7;
5760 }
5761
5762 if (reached)
5763 break;
5764 }
5765 else
5766 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5767
5768 switch (skip)
5769 {
5770 case MOVE_POS_MATCH_OR_ZV:
5771 reached = 8;
5772 goto out;
5773
5774 case MOVE_NEWLINE_OR_CR:
5775 set_iterator_to_next (it, 1);
5776 it->continuation_lines_width = 0;
5777 break;
5778
5779 case MOVE_LINE_TRUNCATED:
5780 it->continuation_lines_width = 0;
5781 reseat_at_next_visible_line_start (it, 0);
5782 if ((op & MOVE_TO_POS) != 0
5783 && IT_CHARPOS (*it) > to_charpos)
5784 {
5785 reached = 9;
5786 goto out;
5787 }
5788 break;
5789
5790 case MOVE_LINE_CONTINUED:
5791 it->continuation_lines_width += it->current_x;
5792 break;
5793
5794 default:
5795 abort ();
5796 }
5797
5798 /* Reset/increment for the next run. */
5799 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5800 it->current_x = it->hpos = 0;
5801 it->current_y += it->max_ascent + it->max_descent;
5802 ++it->vpos;
5803 last_height = it->max_ascent + it->max_descent;
5804 last_max_ascent = it->max_ascent;
5805 it->max_ascent = it->max_descent = 0;
5806 }
5807
5808 out:
5809
5810 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5811 }
5812
5813
5814 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5815
5816 If DY > 0, move IT backward at least that many pixels. DY = 0
5817 means move IT backward to the preceding line start or BEGV. This
5818 function may move over more than DY pixels if IT->current_y - DY
5819 ends up in the middle of a line; in this case IT->current_y will be
5820 set to the top of the line moved to. */
5821
5822 void
5823 move_it_vertically_backward (it, dy)
5824 struct it *it;
5825 int dy;
5826 {
5827 int nlines, h;
5828 struct it it2, it3;
5829 int start_pos = IT_CHARPOS (*it);
5830
5831 xassert (dy >= 0);
5832
5833 /* Estimate how many newlines we must move back. */
5834 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
5835
5836 /* Set the iterator's position that many lines back. */
5837 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5838 back_to_previous_visible_line_start (it);
5839
5840 /* Reseat the iterator here. When moving backward, we don't want
5841 reseat to skip forward over invisible text, set up the iterator
5842 to deliver from overlay strings at the new position etc. So,
5843 use reseat_1 here. */
5844 reseat_1 (it, it->current.pos, 1);
5845
5846 /* We are now surely at a line start. */
5847 it->current_x = it->hpos = 0;
5848 it->continuation_lines_width = 0;
5849
5850 /* Move forward and see what y-distance we moved. First move to the
5851 start of the next line so that we get its height. We need this
5852 height to be able to tell whether we reached the specified
5853 y-distance. */
5854 it2 = *it;
5855 it2.max_ascent = it2.max_descent = 0;
5856 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5857 MOVE_TO_POS | MOVE_TO_VPOS);
5858 xassert (IT_CHARPOS (*it) >= BEGV);
5859 it3 = it2;
5860
5861 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5862 xassert (IT_CHARPOS (*it) >= BEGV);
5863 /* H is the actual vertical distance from the position in *IT
5864 and the starting position. */
5865 h = it2.current_y - it->current_y;
5866 /* NLINES is the distance in number of lines. */
5867 nlines = it2.vpos - it->vpos;
5868
5869 /* Correct IT's y and vpos position
5870 so that they are relative to the starting point. */
5871 it->vpos -= nlines;
5872 it->current_y -= h;
5873
5874 if (dy == 0)
5875 {
5876 /* DY == 0 means move to the start of the screen line. The
5877 value of nlines is > 0 if continuation lines were involved. */
5878 if (nlines > 0)
5879 move_it_by_lines (it, nlines, 1);
5880 xassert (IT_CHARPOS (*it) <= start_pos);
5881 }
5882 else
5883 {
5884 /* The y-position we try to reach, relative to *IT.
5885 Note that H has been subtracted in front of the if-statement. */
5886 int target_y = it->current_y + h - dy;
5887 int y0 = it3.current_y;
5888 int y1 = line_bottom_y (&it3);
5889 int line_height = y1 - y0;
5890
5891 /* If we did not reach target_y, try to move further backward if
5892 we can. If we moved too far backward, try to move forward. */
5893 if (target_y < it->current_y
5894 /* This is heuristic. In a window that's 3 lines high, with
5895 a line height of 13 pixels each, recentering with point
5896 on the bottom line will try to move -39/2 = 19 pixels
5897 backward. Try to avoid moving into the first line. */
5898 && it->current_y - target_y > line_height / 3 * 2
5899 && IT_CHARPOS (*it) > BEGV)
5900 {
5901 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
5902 target_y - it->current_y));
5903 move_it_vertically (it, target_y - it->current_y);
5904 xassert (IT_CHARPOS (*it) >= BEGV);
5905 }
5906 else if (target_y >= it->current_y + line_height
5907 && IT_CHARPOS (*it) < ZV)
5908 {
5909 /* Should move forward by at least one line, maybe more.
5910
5911 Note: Calling move_it_by_lines can be expensive on
5912 terminal frames, where compute_motion is used (via
5913 vmotion) to do the job, when there are very long lines
5914 and truncate-lines is nil. That's the reason for
5915 treating terminal frames specially here. */
5916
5917 if (!FRAME_WINDOW_P (it->f))
5918 move_it_vertically (it, target_y - (it->current_y + line_height));
5919 else
5920 {
5921 do
5922 {
5923 move_it_by_lines (it, 1, 1);
5924 }
5925 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
5926 }
5927
5928 xassert (IT_CHARPOS (*it) >= BEGV);
5929 }
5930 }
5931 }
5932
5933
5934 /* Move IT by a specified amount of pixel lines DY. DY negative means
5935 move backwards. DY = 0 means move to start of screen line. At the
5936 end, IT will be on the start of a screen line. */
5937
5938 void
5939 move_it_vertically (it, dy)
5940 struct it *it;
5941 int dy;
5942 {
5943 if (dy <= 0)
5944 move_it_vertically_backward (it, -dy);
5945 else if (dy > 0)
5946 {
5947 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5948 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5949 MOVE_TO_POS | MOVE_TO_Y);
5950 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5951
5952 /* If buffer ends in ZV without a newline, move to the start of
5953 the line to satisfy the post-condition. */
5954 if (IT_CHARPOS (*it) == ZV
5955 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5956 move_it_by_lines (it, 0, 0);
5957 }
5958 }
5959
5960
5961 /* Move iterator IT past the end of the text line it is in. */
5962
5963 void
5964 move_it_past_eol (it)
5965 struct it *it;
5966 {
5967 enum move_it_result rc;
5968
5969 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5970 if (rc == MOVE_NEWLINE_OR_CR)
5971 set_iterator_to_next (it, 0);
5972 }
5973
5974
5975 #if 0 /* Currently not used. */
5976
5977 /* Return non-zero if some text between buffer positions START_CHARPOS
5978 and END_CHARPOS is invisible. IT->window is the window for text
5979 property lookup. */
5980
5981 static int
5982 invisible_text_between_p (it, start_charpos, end_charpos)
5983 struct it *it;
5984 int start_charpos, end_charpos;
5985 {
5986 Lisp_Object prop, limit;
5987 int invisible_found_p;
5988
5989 xassert (it != NULL && start_charpos <= end_charpos);
5990
5991 /* Is text at START invisible? */
5992 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5993 it->window);
5994 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5995 invisible_found_p = 1;
5996 else
5997 {
5998 limit = Fnext_single_char_property_change (make_number (start_charpos),
5999 Qinvisible, Qnil,
6000 make_number (end_charpos));
6001 invisible_found_p = XFASTINT (limit) < end_charpos;
6002 }
6003
6004 return invisible_found_p;
6005 }
6006
6007 #endif /* 0 */
6008
6009
6010 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6011 negative means move up. DVPOS == 0 means move to the start of the
6012 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6013 NEED_Y_P is zero, IT->current_y will be left unchanged.
6014
6015 Further optimization ideas: If we would know that IT->f doesn't use
6016 a face with proportional font, we could be faster for
6017 truncate-lines nil. */
6018
6019 void
6020 move_it_by_lines (it, dvpos, need_y_p)
6021 struct it *it;
6022 int dvpos, need_y_p;
6023 {
6024 struct position pos;
6025
6026 if (!FRAME_WINDOW_P (it->f))
6027 {
6028 struct text_pos textpos;
6029
6030 /* We can use vmotion on frames without proportional fonts. */
6031 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6032 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6033 reseat (it, textpos, 1);
6034 it->vpos += pos.vpos;
6035 it->current_y += pos.vpos;
6036 }
6037 else if (dvpos == 0)
6038 {
6039 /* DVPOS == 0 means move to the start of the screen line. */
6040 move_it_vertically_backward (it, 0);
6041 xassert (it->current_x == 0 && it->hpos == 0);
6042 }
6043 else if (dvpos > 0)
6044 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6045 else
6046 {
6047 struct it it2;
6048 int start_charpos, i;
6049
6050 /* Start at the beginning of the screen line containing IT's
6051 position. */
6052 move_it_vertically_backward (it, 0);
6053
6054 /* Go back -DVPOS visible lines and reseat the iterator there. */
6055 start_charpos = IT_CHARPOS (*it);
6056 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6057 back_to_previous_visible_line_start (it);
6058 reseat (it, it->current.pos, 1);
6059 it->current_x = it->hpos = 0;
6060
6061 /* Above call may have moved too far if continuation lines
6062 are involved. Scan forward and see if it did. */
6063 it2 = *it;
6064 it2.vpos = it2.current_y = 0;
6065 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6066 it->vpos -= it2.vpos;
6067 it->current_y -= it2.current_y;
6068 it->current_x = it->hpos = 0;
6069
6070 /* If we moved too far, move IT some lines forward. */
6071 if (it2.vpos > -dvpos)
6072 {
6073 int delta = it2.vpos + dvpos;
6074 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6075 }
6076 }
6077 }
6078
6079 /* Return 1 if IT points into the middle of a display vector. */
6080
6081 int
6082 in_display_vector_p (it)
6083 struct it *it;
6084 {
6085 return (it->method == next_element_from_display_vector
6086 && it->current.dpvec_index > 0
6087 && it->dpvec + it->current.dpvec_index != it->dpend);
6088 }
6089
6090 \f
6091 /***********************************************************************
6092 Messages
6093 ***********************************************************************/
6094
6095
6096 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6097 to *Messages*. */
6098
6099 void
6100 add_to_log (format, arg1, arg2)
6101 char *format;
6102 Lisp_Object arg1, arg2;
6103 {
6104 Lisp_Object args[3];
6105 Lisp_Object msg, fmt;
6106 char *buffer;
6107 int len;
6108 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6109
6110 /* Do nothing if called asynchronously. Inserting text into
6111 a buffer may call after-change-functions and alike and
6112 that would means running Lisp asynchronously. */
6113 if (handling_signal)
6114 return;
6115
6116 fmt = msg = Qnil;
6117 GCPRO4 (fmt, msg, arg1, arg2);
6118
6119 args[0] = fmt = build_string (format);
6120 args[1] = arg1;
6121 args[2] = arg2;
6122 msg = Fformat (3, args);
6123
6124 len = SBYTES (msg) + 1;
6125 buffer = (char *) alloca (len);
6126 bcopy (SDATA (msg), buffer, len);
6127
6128 message_dolog (buffer, len - 1, 1, 0);
6129 UNGCPRO;
6130 }
6131
6132
6133 /* Output a newline in the *Messages* buffer if "needs" one. */
6134
6135 void
6136 message_log_maybe_newline ()
6137 {
6138 if (message_log_need_newline)
6139 message_dolog ("", 0, 1, 0);
6140 }
6141
6142
6143 /* Add a string M of length NBYTES to the message log, optionally
6144 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6145 nonzero, means interpret the contents of M as multibyte. This
6146 function calls low-level routines in order to bypass text property
6147 hooks, etc. which might not be safe to run. */
6148
6149 void
6150 message_dolog (m, nbytes, nlflag, multibyte)
6151 const char *m;
6152 int nbytes, nlflag, multibyte;
6153 {
6154 if (!NILP (Vmemory_full))
6155 return;
6156
6157 if (!NILP (Vmessage_log_max))
6158 {
6159 struct buffer *oldbuf;
6160 Lisp_Object oldpoint, oldbegv, oldzv;
6161 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6162 int point_at_end = 0;
6163 int zv_at_end = 0;
6164 Lisp_Object old_deactivate_mark, tem;
6165 struct gcpro gcpro1;
6166
6167 old_deactivate_mark = Vdeactivate_mark;
6168 oldbuf = current_buffer;
6169 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6170 current_buffer->undo_list = Qt;
6171
6172 oldpoint = message_dolog_marker1;
6173 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6174 oldbegv = message_dolog_marker2;
6175 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6176 oldzv = message_dolog_marker3;
6177 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6178 GCPRO1 (old_deactivate_mark);
6179
6180 if (PT == Z)
6181 point_at_end = 1;
6182 if (ZV == Z)
6183 zv_at_end = 1;
6184
6185 BEGV = BEG;
6186 BEGV_BYTE = BEG_BYTE;
6187 ZV = Z;
6188 ZV_BYTE = Z_BYTE;
6189 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6190
6191 /* Insert the string--maybe converting multibyte to single byte
6192 or vice versa, so that all the text fits the buffer. */
6193 if (multibyte
6194 && NILP (current_buffer->enable_multibyte_characters))
6195 {
6196 int i, c, char_bytes;
6197 unsigned char work[1];
6198
6199 /* Convert a multibyte string to single-byte
6200 for the *Message* buffer. */
6201 for (i = 0; i < nbytes; i += char_bytes)
6202 {
6203 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6204 work[0] = (SINGLE_BYTE_CHAR_P (c)
6205 ? c
6206 : multibyte_char_to_unibyte (c, Qnil));
6207 insert_1_both (work, 1, 1, 1, 0, 0);
6208 }
6209 }
6210 else if (! multibyte
6211 && ! NILP (current_buffer->enable_multibyte_characters))
6212 {
6213 int i, c, char_bytes;
6214 unsigned char *msg = (unsigned char *) m;
6215 unsigned char str[MAX_MULTIBYTE_LENGTH];
6216 /* Convert a single-byte string to multibyte
6217 for the *Message* buffer. */
6218 for (i = 0; i < nbytes; i++)
6219 {
6220 c = unibyte_char_to_multibyte (msg[i]);
6221 char_bytes = CHAR_STRING (c, str);
6222 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6223 }
6224 }
6225 else if (nbytes)
6226 insert_1 (m, nbytes, 1, 0, 0);
6227
6228 if (nlflag)
6229 {
6230 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6231 insert_1 ("\n", 1, 1, 0, 0);
6232
6233 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6234 this_bol = PT;
6235 this_bol_byte = PT_BYTE;
6236
6237 /* See if this line duplicates the previous one.
6238 If so, combine duplicates. */
6239 if (this_bol > BEG)
6240 {
6241 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6242 prev_bol = PT;
6243 prev_bol_byte = PT_BYTE;
6244
6245 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6246 this_bol, this_bol_byte);
6247 if (dup)
6248 {
6249 del_range_both (prev_bol, prev_bol_byte,
6250 this_bol, this_bol_byte, 0);
6251 if (dup > 1)
6252 {
6253 char dupstr[40];
6254 int duplen;
6255
6256 /* If you change this format, don't forget to also
6257 change message_log_check_duplicate. */
6258 sprintf (dupstr, " [%d times]", dup);
6259 duplen = strlen (dupstr);
6260 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6261 insert_1 (dupstr, duplen, 1, 0, 1);
6262 }
6263 }
6264 }
6265
6266 /* If we have more than the desired maximum number of lines
6267 in the *Messages* buffer now, delete the oldest ones.
6268 This is safe because we don't have undo in this buffer. */
6269
6270 if (NATNUMP (Vmessage_log_max))
6271 {
6272 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6273 -XFASTINT (Vmessage_log_max) - 1, 0);
6274 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6275 }
6276 }
6277 BEGV = XMARKER (oldbegv)->charpos;
6278 BEGV_BYTE = marker_byte_position (oldbegv);
6279
6280 if (zv_at_end)
6281 {
6282 ZV = Z;
6283 ZV_BYTE = Z_BYTE;
6284 }
6285 else
6286 {
6287 ZV = XMARKER (oldzv)->charpos;
6288 ZV_BYTE = marker_byte_position (oldzv);
6289 }
6290
6291 if (point_at_end)
6292 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6293 else
6294 /* We can't do Fgoto_char (oldpoint) because it will run some
6295 Lisp code. */
6296 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6297 XMARKER (oldpoint)->bytepos);
6298
6299 UNGCPRO;
6300 unchain_marker (oldpoint);
6301 unchain_marker (oldbegv);
6302 unchain_marker (oldzv);
6303
6304 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6305 set_buffer_internal (oldbuf);
6306 if (NILP (tem))
6307 windows_or_buffers_changed = old_windows_or_buffers_changed;
6308 message_log_need_newline = !nlflag;
6309 Vdeactivate_mark = old_deactivate_mark;
6310 }
6311 }
6312
6313
6314 /* We are at the end of the buffer after just having inserted a newline.
6315 (Note: We depend on the fact we won't be crossing the gap.)
6316 Check to see if the most recent message looks a lot like the previous one.
6317 Return 0 if different, 1 if the new one should just replace it, or a
6318 value N > 1 if we should also append " [N times]". */
6319
6320 static int
6321 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6322 int prev_bol, this_bol;
6323 int prev_bol_byte, this_bol_byte;
6324 {
6325 int i;
6326 int len = Z_BYTE - 1 - this_bol_byte;
6327 int seen_dots = 0;
6328 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6329 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6330
6331 for (i = 0; i < len; i++)
6332 {
6333 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6334 seen_dots = 1;
6335 if (p1[i] != p2[i])
6336 return seen_dots;
6337 }
6338 p1 += len;
6339 if (*p1 == '\n')
6340 return 2;
6341 if (*p1++ == ' ' && *p1++ == '[')
6342 {
6343 int n = 0;
6344 while (*p1 >= '0' && *p1 <= '9')
6345 n = n * 10 + *p1++ - '0';
6346 if (strncmp (p1, " times]\n", 8) == 0)
6347 return n+1;
6348 }
6349 return 0;
6350 }
6351
6352
6353 /* Display an echo area message M with a specified length of NBYTES
6354 bytes. The string may include null characters. If M is 0, clear
6355 out any existing message, and let the mini-buffer text show
6356 through.
6357
6358 The buffer M must continue to exist until after the echo area gets
6359 cleared or some other message gets displayed there. This means do
6360 not pass text that is stored in a Lisp string; do not pass text in
6361 a buffer that was alloca'd. */
6362
6363 void
6364 message2 (m, nbytes, multibyte)
6365 const char *m;
6366 int nbytes;
6367 int multibyte;
6368 {
6369 /* First flush out any partial line written with print. */
6370 message_log_maybe_newline ();
6371 if (m)
6372 message_dolog (m, nbytes, 1, multibyte);
6373 message2_nolog (m, nbytes, multibyte);
6374 }
6375
6376
6377 /* The non-logging counterpart of message2. */
6378
6379 void
6380 message2_nolog (m, nbytes, multibyte)
6381 const char *m;
6382 int nbytes, multibyte;
6383 {
6384 struct frame *sf = SELECTED_FRAME ();
6385 message_enable_multibyte = multibyte;
6386
6387 if (noninteractive)
6388 {
6389 if (noninteractive_need_newline)
6390 putc ('\n', stderr);
6391 noninteractive_need_newline = 0;
6392 if (m)
6393 fwrite (m, nbytes, 1, stderr);
6394 if (cursor_in_echo_area == 0)
6395 fprintf (stderr, "\n");
6396 fflush (stderr);
6397 }
6398 /* A null message buffer means that the frame hasn't really been
6399 initialized yet. Error messages get reported properly by
6400 cmd_error, so this must be just an informative message; toss it. */
6401 else if (INTERACTIVE
6402 && sf->glyphs_initialized_p
6403 && FRAME_MESSAGE_BUF (sf))
6404 {
6405 Lisp_Object mini_window;
6406 struct frame *f;
6407
6408 /* Get the frame containing the mini-buffer
6409 that the selected frame is using. */
6410 mini_window = FRAME_MINIBUF_WINDOW (sf);
6411 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6412
6413 FRAME_SAMPLE_VISIBILITY (f);
6414 if (FRAME_VISIBLE_P (sf)
6415 && ! FRAME_VISIBLE_P (f))
6416 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6417
6418 if (m)
6419 {
6420 set_message (m, Qnil, nbytes, multibyte);
6421 if (minibuffer_auto_raise)
6422 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6423 }
6424 else
6425 clear_message (1, 1);
6426
6427 do_pending_window_change (0);
6428 echo_area_display (1);
6429 do_pending_window_change (0);
6430 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6431 (*frame_up_to_date_hook) (f);
6432 }
6433 }
6434
6435
6436 /* Display an echo area message M with a specified length of NBYTES
6437 bytes. The string may include null characters. If M is not a
6438 string, clear out any existing message, and let the mini-buffer
6439 text show through. */
6440
6441 void
6442 message3 (m, nbytes, multibyte)
6443 Lisp_Object m;
6444 int nbytes;
6445 int multibyte;
6446 {
6447 struct gcpro gcpro1;
6448
6449 GCPRO1 (m);
6450
6451 /* First flush out any partial line written with print. */
6452 message_log_maybe_newline ();
6453 if (STRINGP (m))
6454 message_dolog (SDATA (m), nbytes, 1, multibyte);
6455 message3_nolog (m, nbytes, multibyte);
6456
6457 UNGCPRO;
6458 }
6459
6460
6461 /* The non-logging version of message3. */
6462
6463 void
6464 message3_nolog (m, nbytes, multibyte)
6465 Lisp_Object m;
6466 int nbytes, multibyte;
6467 {
6468 struct frame *sf = SELECTED_FRAME ();
6469 message_enable_multibyte = multibyte;
6470
6471 if (noninteractive)
6472 {
6473 if (noninteractive_need_newline)
6474 putc ('\n', stderr);
6475 noninteractive_need_newline = 0;
6476 if (STRINGP (m))
6477 fwrite (SDATA (m), nbytes, 1, stderr);
6478 if (cursor_in_echo_area == 0)
6479 fprintf (stderr, "\n");
6480 fflush (stderr);
6481 }
6482 /* A null message buffer means that the frame hasn't really been
6483 initialized yet. Error messages get reported properly by
6484 cmd_error, so this must be just an informative message; toss it. */
6485 else if (INTERACTIVE
6486 && sf->glyphs_initialized_p
6487 && FRAME_MESSAGE_BUF (sf))
6488 {
6489 Lisp_Object mini_window;
6490 Lisp_Object frame;
6491 struct frame *f;
6492
6493 /* Get the frame containing the mini-buffer
6494 that the selected frame is using. */
6495 mini_window = FRAME_MINIBUF_WINDOW (sf);
6496 frame = XWINDOW (mini_window)->frame;
6497 f = XFRAME (frame);
6498
6499 FRAME_SAMPLE_VISIBILITY (f);
6500 if (FRAME_VISIBLE_P (sf)
6501 && !FRAME_VISIBLE_P (f))
6502 Fmake_frame_visible (frame);
6503
6504 if (STRINGP (m) && SCHARS (m) > 0)
6505 {
6506 set_message (NULL, m, nbytes, multibyte);
6507 if (minibuffer_auto_raise)
6508 Fraise_frame (frame);
6509 }
6510 else
6511 clear_message (1, 1);
6512
6513 do_pending_window_change (0);
6514 echo_area_display (1);
6515 do_pending_window_change (0);
6516 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6517 (*frame_up_to_date_hook) (f);
6518 }
6519 }
6520
6521
6522 /* Display a null-terminated echo area message M. If M is 0, clear
6523 out any existing message, and let the mini-buffer text show through.
6524
6525 The buffer M must continue to exist until after the echo area gets
6526 cleared or some other message gets displayed there. Do not pass
6527 text that is stored in a Lisp string. Do not pass text in a buffer
6528 that was alloca'd. */
6529
6530 void
6531 message1 (m)
6532 char *m;
6533 {
6534 message2 (m, (m ? strlen (m) : 0), 0);
6535 }
6536
6537
6538 /* The non-logging counterpart of message1. */
6539
6540 void
6541 message1_nolog (m)
6542 char *m;
6543 {
6544 message2_nolog (m, (m ? strlen (m) : 0), 0);
6545 }
6546
6547 /* Display a message M which contains a single %s
6548 which gets replaced with STRING. */
6549
6550 void
6551 message_with_string (m, string, log)
6552 char *m;
6553 Lisp_Object string;
6554 int log;
6555 {
6556 CHECK_STRING (string);
6557
6558 if (noninteractive)
6559 {
6560 if (m)
6561 {
6562 if (noninteractive_need_newline)
6563 putc ('\n', stderr);
6564 noninteractive_need_newline = 0;
6565 fprintf (stderr, m, SDATA (string));
6566 if (cursor_in_echo_area == 0)
6567 fprintf (stderr, "\n");
6568 fflush (stderr);
6569 }
6570 }
6571 else if (INTERACTIVE)
6572 {
6573 /* The frame whose minibuffer we're going to display the message on.
6574 It may be larger than the selected frame, so we need
6575 to use its buffer, not the selected frame's buffer. */
6576 Lisp_Object mini_window;
6577 struct frame *f, *sf = SELECTED_FRAME ();
6578
6579 /* Get the frame containing the minibuffer
6580 that the selected frame is using. */
6581 mini_window = FRAME_MINIBUF_WINDOW (sf);
6582 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6583
6584 /* A null message buffer means that the frame hasn't really been
6585 initialized yet. Error messages get reported properly by
6586 cmd_error, so this must be just an informative message; toss it. */
6587 if (FRAME_MESSAGE_BUF (f))
6588 {
6589 Lisp_Object args[2], message;
6590 struct gcpro gcpro1, gcpro2;
6591
6592 args[0] = build_string (m);
6593 args[1] = message = string;
6594 GCPRO2 (args[0], message);
6595 gcpro1.nvars = 2;
6596
6597 message = Fformat (2, args);
6598
6599 if (log)
6600 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6601 else
6602 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6603
6604 UNGCPRO;
6605
6606 /* Print should start at the beginning of the message
6607 buffer next time. */
6608 message_buf_print = 0;
6609 }
6610 }
6611 }
6612
6613
6614 /* Dump an informative message to the minibuf. If M is 0, clear out
6615 any existing message, and let the mini-buffer text show through. */
6616
6617 /* VARARGS 1 */
6618 void
6619 message (m, a1, a2, a3)
6620 char *m;
6621 EMACS_INT a1, a2, a3;
6622 {
6623 if (noninteractive)
6624 {
6625 if (m)
6626 {
6627 if (noninteractive_need_newline)
6628 putc ('\n', stderr);
6629 noninteractive_need_newline = 0;
6630 fprintf (stderr, m, a1, a2, a3);
6631 if (cursor_in_echo_area == 0)
6632 fprintf (stderr, "\n");
6633 fflush (stderr);
6634 }
6635 }
6636 else if (INTERACTIVE)
6637 {
6638 /* The frame whose mini-buffer we're going to display the message
6639 on. It may be larger than the selected frame, so we need to
6640 use its buffer, not the selected frame's buffer. */
6641 Lisp_Object mini_window;
6642 struct frame *f, *sf = SELECTED_FRAME ();
6643
6644 /* Get the frame containing the mini-buffer
6645 that the selected frame is using. */
6646 mini_window = FRAME_MINIBUF_WINDOW (sf);
6647 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6648
6649 /* A null message buffer means that the frame hasn't really been
6650 initialized yet. Error messages get reported properly by
6651 cmd_error, so this must be just an informative message; toss
6652 it. */
6653 if (FRAME_MESSAGE_BUF (f))
6654 {
6655 if (m)
6656 {
6657 int len;
6658 #ifdef NO_ARG_ARRAY
6659 char *a[3];
6660 a[0] = (char *) a1;
6661 a[1] = (char *) a2;
6662 a[2] = (char *) a3;
6663
6664 len = doprnt (FRAME_MESSAGE_BUF (f),
6665 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6666 #else
6667 len = doprnt (FRAME_MESSAGE_BUF (f),
6668 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6669 (char **) &a1);
6670 #endif /* NO_ARG_ARRAY */
6671
6672 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6673 }
6674 else
6675 message1 (0);
6676
6677 /* Print should start at the beginning of the message
6678 buffer next time. */
6679 message_buf_print = 0;
6680 }
6681 }
6682 }
6683
6684
6685 /* The non-logging version of message. */
6686
6687 void
6688 message_nolog (m, a1, a2, a3)
6689 char *m;
6690 EMACS_INT a1, a2, a3;
6691 {
6692 Lisp_Object old_log_max;
6693 old_log_max = Vmessage_log_max;
6694 Vmessage_log_max = Qnil;
6695 message (m, a1, a2, a3);
6696 Vmessage_log_max = old_log_max;
6697 }
6698
6699
6700 /* Display the current message in the current mini-buffer. This is
6701 only called from error handlers in process.c, and is not time
6702 critical. */
6703
6704 void
6705 update_echo_area ()
6706 {
6707 if (!NILP (echo_area_buffer[0]))
6708 {
6709 Lisp_Object string;
6710 string = Fcurrent_message ();
6711 message3 (string, SBYTES (string),
6712 !NILP (current_buffer->enable_multibyte_characters));
6713 }
6714 }
6715
6716
6717 /* Make sure echo area buffers in `echo_buffers' are live.
6718 If they aren't, make new ones. */
6719
6720 static void
6721 ensure_echo_area_buffers ()
6722 {
6723 int i;
6724
6725 for (i = 0; i < 2; ++i)
6726 if (!BUFFERP (echo_buffer[i])
6727 || NILP (XBUFFER (echo_buffer[i])->name))
6728 {
6729 char name[30];
6730 Lisp_Object old_buffer;
6731 int j;
6732
6733 old_buffer = echo_buffer[i];
6734 sprintf (name, " *Echo Area %d*", i);
6735 echo_buffer[i] = Fget_buffer_create (build_string (name));
6736 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6737
6738 for (j = 0; j < 2; ++j)
6739 if (EQ (old_buffer, echo_area_buffer[j]))
6740 echo_area_buffer[j] = echo_buffer[i];
6741 }
6742 }
6743
6744
6745 /* Call FN with args A1..A4 with either the current or last displayed
6746 echo_area_buffer as current buffer.
6747
6748 WHICH zero means use the current message buffer
6749 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6750 from echo_buffer[] and clear it.
6751
6752 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6753 suitable buffer from echo_buffer[] and clear it.
6754
6755 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6756 that the current message becomes the last displayed one, make
6757 choose a suitable buffer for echo_area_buffer[0], and clear it.
6758
6759 Value is what FN returns. */
6760
6761 static int
6762 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6763 struct window *w;
6764 int which;
6765 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6766 EMACS_INT a1;
6767 Lisp_Object a2;
6768 EMACS_INT a3, a4;
6769 {
6770 Lisp_Object buffer;
6771 int this_one, the_other, clear_buffer_p, rc;
6772 int count = SPECPDL_INDEX ();
6773
6774 /* If buffers aren't live, make new ones. */
6775 ensure_echo_area_buffers ();
6776
6777 clear_buffer_p = 0;
6778
6779 if (which == 0)
6780 this_one = 0, the_other = 1;
6781 else if (which > 0)
6782 this_one = 1, the_other = 0;
6783 else
6784 {
6785 this_one = 0, the_other = 1;
6786 clear_buffer_p = 1;
6787
6788 /* We need a fresh one in case the current echo buffer equals
6789 the one containing the last displayed echo area message. */
6790 if (!NILP (echo_area_buffer[this_one])
6791 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6792 echo_area_buffer[this_one] = Qnil;
6793 }
6794
6795 /* Choose a suitable buffer from echo_buffer[] is we don't
6796 have one. */
6797 if (NILP (echo_area_buffer[this_one]))
6798 {
6799 echo_area_buffer[this_one]
6800 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6801 ? echo_buffer[the_other]
6802 : echo_buffer[this_one]);
6803 clear_buffer_p = 1;
6804 }
6805
6806 buffer = echo_area_buffer[this_one];
6807
6808 /* Don't get confused by reusing the buffer used for echoing
6809 for a different purpose. */
6810 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
6811 cancel_echoing ();
6812
6813 record_unwind_protect (unwind_with_echo_area_buffer,
6814 with_echo_area_buffer_unwind_data (w));
6815
6816 /* Make the echo area buffer current. Note that for display
6817 purposes, it is not necessary that the displayed window's buffer
6818 == current_buffer, except for text property lookup. So, let's
6819 only set that buffer temporarily here without doing a full
6820 Fset_window_buffer. We must also change w->pointm, though,
6821 because otherwise an assertions in unshow_buffer fails, and Emacs
6822 aborts. */
6823 set_buffer_internal_1 (XBUFFER (buffer));
6824 if (w)
6825 {
6826 w->buffer = buffer;
6827 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6828 }
6829
6830 current_buffer->undo_list = Qt;
6831 current_buffer->read_only = Qnil;
6832 specbind (Qinhibit_read_only, Qt);
6833 specbind (Qinhibit_modification_hooks, Qt);
6834
6835 if (clear_buffer_p && Z > BEG)
6836 del_range (BEG, Z);
6837
6838 xassert (BEGV >= BEG);
6839 xassert (ZV <= Z && ZV >= BEGV);
6840
6841 rc = fn (a1, a2, a3, a4);
6842
6843 xassert (BEGV >= BEG);
6844 xassert (ZV <= Z && ZV >= BEGV);
6845
6846 unbind_to (count, Qnil);
6847 return rc;
6848 }
6849
6850
6851 /* Save state that should be preserved around the call to the function
6852 FN called in with_echo_area_buffer. */
6853
6854 static Lisp_Object
6855 with_echo_area_buffer_unwind_data (w)
6856 struct window *w;
6857 {
6858 int i = 0;
6859 Lisp_Object vector;
6860
6861 /* Reduce consing by keeping one vector in
6862 Vwith_echo_area_save_vector. */
6863 vector = Vwith_echo_area_save_vector;
6864 Vwith_echo_area_save_vector = Qnil;
6865
6866 if (NILP (vector))
6867 vector = Fmake_vector (make_number (7), Qnil);
6868
6869 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6870 AREF (vector, i) = Vdeactivate_mark, ++i;
6871 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6872
6873 if (w)
6874 {
6875 XSETWINDOW (AREF (vector, i), w); ++i;
6876 AREF (vector, i) = w->buffer; ++i;
6877 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6878 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6879 }
6880 else
6881 {
6882 int end = i + 4;
6883 for (; i < end; ++i)
6884 AREF (vector, i) = Qnil;
6885 }
6886
6887 xassert (i == ASIZE (vector));
6888 return vector;
6889 }
6890
6891
6892 /* Restore global state from VECTOR which was created by
6893 with_echo_area_buffer_unwind_data. */
6894
6895 static Lisp_Object
6896 unwind_with_echo_area_buffer (vector)
6897 Lisp_Object vector;
6898 {
6899 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6900 Vdeactivate_mark = AREF (vector, 1);
6901 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6902
6903 if (WINDOWP (AREF (vector, 3)))
6904 {
6905 struct window *w;
6906 Lisp_Object buffer, charpos, bytepos;
6907
6908 w = XWINDOW (AREF (vector, 3));
6909 buffer = AREF (vector, 4);
6910 charpos = AREF (vector, 5);
6911 bytepos = AREF (vector, 6);
6912
6913 w->buffer = buffer;
6914 set_marker_both (w->pointm, buffer,
6915 XFASTINT (charpos), XFASTINT (bytepos));
6916 }
6917
6918 Vwith_echo_area_save_vector = vector;
6919 return Qnil;
6920 }
6921
6922
6923 /* Set up the echo area for use by print functions. MULTIBYTE_P
6924 non-zero means we will print multibyte. */
6925
6926 void
6927 setup_echo_area_for_printing (multibyte_p)
6928 int multibyte_p;
6929 {
6930 /* If we can't find an echo area any more, exit. */
6931 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
6932 Fkill_emacs (Qnil);
6933
6934 ensure_echo_area_buffers ();
6935
6936 if (!message_buf_print)
6937 {
6938 /* A message has been output since the last time we printed.
6939 Choose a fresh echo area buffer. */
6940 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6941 echo_area_buffer[0] = echo_buffer[1];
6942 else
6943 echo_area_buffer[0] = echo_buffer[0];
6944
6945 /* Switch to that buffer and clear it. */
6946 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6947 current_buffer->truncate_lines = Qnil;
6948
6949 if (Z > BEG)
6950 {
6951 int count = SPECPDL_INDEX ();
6952 specbind (Qinhibit_read_only, Qt);
6953 /* Note that undo recording is always disabled. */
6954 del_range (BEG, Z);
6955 unbind_to (count, Qnil);
6956 }
6957 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6958
6959 /* Set up the buffer for the multibyteness we need. */
6960 if (multibyte_p
6961 != !NILP (current_buffer->enable_multibyte_characters))
6962 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6963
6964 /* Raise the frame containing the echo area. */
6965 if (minibuffer_auto_raise)
6966 {
6967 struct frame *sf = SELECTED_FRAME ();
6968 Lisp_Object mini_window;
6969 mini_window = FRAME_MINIBUF_WINDOW (sf);
6970 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6971 }
6972
6973 message_log_maybe_newline ();
6974 message_buf_print = 1;
6975 }
6976 else
6977 {
6978 if (NILP (echo_area_buffer[0]))
6979 {
6980 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6981 echo_area_buffer[0] = echo_buffer[1];
6982 else
6983 echo_area_buffer[0] = echo_buffer[0];
6984 }
6985
6986 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6987 {
6988 /* Someone switched buffers between print requests. */
6989 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6990 current_buffer->truncate_lines = Qnil;
6991 }
6992 }
6993 }
6994
6995
6996 /* Display an echo area message in window W. Value is non-zero if W's
6997 height is changed. If display_last_displayed_message_p is
6998 non-zero, display the message that was last displayed, otherwise
6999 display the current message. */
7000
7001 static int
7002 display_echo_area (w)
7003 struct window *w;
7004 {
7005 int i, no_message_p, window_height_changed_p, count;
7006
7007 /* Temporarily disable garbage collections while displaying the echo
7008 area. This is done because a GC can print a message itself.
7009 That message would modify the echo area buffer's contents while a
7010 redisplay of the buffer is going on, and seriously confuse
7011 redisplay. */
7012 count = inhibit_garbage_collection ();
7013
7014 /* If there is no message, we must call display_echo_area_1
7015 nevertheless because it resizes the window. But we will have to
7016 reset the echo_area_buffer in question to nil at the end because
7017 with_echo_area_buffer will sets it to an empty buffer. */
7018 i = display_last_displayed_message_p ? 1 : 0;
7019 no_message_p = NILP (echo_area_buffer[i]);
7020
7021 window_height_changed_p
7022 = with_echo_area_buffer (w, display_last_displayed_message_p,
7023 display_echo_area_1,
7024 (EMACS_INT) w, Qnil, 0, 0);
7025
7026 if (no_message_p)
7027 echo_area_buffer[i] = Qnil;
7028
7029 unbind_to (count, Qnil);
7030 return window_height_changed_p;
7031 }
7032
7033
7034 /* Helper for display_echo_area. Display the current buffer which
7035 contains the current echo area message in window W, a mini-window,
7036 a pointer to which is passed in A1. A2..A4 are currently not used.
7037 Change the height of W so that all of the message is displayed.
7038 Value is non-zero if height of W was changed. */
7039
7040 static int
7041 display_echo_area_1 (a1, a2, a3, a4)
7042 EMACS_INT a1;
7043 Lisp_Object a2;
7044 EMACS_INT a3, a4;
7045 {
7046 struct window *w = (struct window *) a1;
7047 Lisp_Object window;
7048 struct text_pos start;
7049 int window_height_changed_p = 0;
7050
7051 /* Do this before displaying, so that we have a large enough glyph
7052 matrix for the display. */
7053 window_height_changed_p = resize_mini_window (w, 0);
7054
7055 /* Display. */
7056 clear_glyph_matrix (w->desired_matrix);
7057 XSETWINDOW (window, w);
7058 SET_TEXT_POS (start, BEG, BEG_BYTE);
7059 try_window (window, start);
7060
7061 return window_height_changed_p;
7062 }
7063
7064
7065 /* Resize the echo area window to exactly the size needed for the
7066 currently displayed message, if there is one. If a mini-buffer
7067 is active, don't shrink it. */
7068
7069 void
7070 resize_echo_area_exactly ()
7071 {
7072 if (BUFFERP (echo_area_buffer[0])
7073 && WINDOWP (echo_area_window))
7074 {
7075 struct window *w = XWINDOW (echo_area_window);
7076 int resized_p;
7077 Lisp_Object resize_exactly;
7078
7079 if (minibuf_level == 0)
7080 resize_exactly = Qt;
7081 else
7082 resize_exactly = Qnil;
7083
7084 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7085 (EMACS_INT) w, resize_exactly, 0, 0);
7086 if (resized_p)
7087 {
7088 ++windows_or_buffers_changed;
7089 ++update_mode_lines;
7090 redisplay_internal (0);
7091 }
7092 }
7093 }
7094
7095
7096 /* Callback function for with_echo_area_buffer, when used from
7097 resize_echo_area_exactly. A1 contains a pointer to the window to
7098 resize, EXACTLY non-nil means resize the mini-window exactly to the
7099 size of the text displayed. A3 and A4 are not used. Value is what
7100 resize_mini_window returns. */
7101
7102 static int
7103 resize_mini_window_1 (a1, exactly, a3, a4)
7104 EMACS_INT a1;
7105 Lisp_Object exactly;
7106 EMACS_INT a3, a4;
7107 {
7108 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7109 }
7110
7111
7112 /* Resize mini-window W to fit the size of its contents. EXACT:P
7113 means size the window exactly to the size needed. Otherwise, it's
7114 only enlarged until W's buffer is empty. Value is non-zero if
7115 the window height has been changed. */
7116
7117 int
7118 resize_mini_window (w, exact_p)
7119 struct window *w;
7120 int exact_p;
7121 {
7122 struct frame *f = XFRAME (w->frame);
7123 int window_height_changed_p = 0;
7124
7125 xassert (MINI_WINDOW_P (w));
7126
7127 /* Don't resize windows while redisplaying a window; it would
7128 confuse redisplay functions when the size of the window they are
7129 displaying changes from under them. Such a resizing can happen,
7130 for instance, when which-func prints a long message while
7131 we are running fontification-functions. We're running these
7132 functions with safe_call which binds inhibit-redisplay to t. */
7133 if (!NILP (Vinhibit_redisplay))
7134 return 0;
7135
7136 /* Nil means don't try to resize. */
7137 if (NILP (Vresize_mini_windows)
7138 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7139 return 0;
7140
7141 if (!FRAME_MINIBUF_ONLY_P (f))
7142 {
7143 struct it it;
7144 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7145 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7146 int height, max_height;
7147 int unit = FRAME_LINE_HEIGHT (f);
7148 struct text_pos start;
7149 struct buffer *old_current_buffer = NULL;
7150
7151 if (current_buffer != XBUFFER (w->buffer))
7152 {
7153 old_current_buffer = current_buffer;
7154 set_buffer_internal (XBUFFER (w->buffer));
7155 }
7156
7157 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7158
7159 /* Compute the max. number of lines specified by the user. */
7160 if (FLOATP (Vmax_mini_window_height))
7161 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7162 else if (INTEGERP (Vmax_mini_window_height))
7163 max_height = XINT (Vmax_mini_window_height);
7164 else
7165 max_height = total_height / 4;
7166
7167 /* Correct that max. height if it's bogus. */
7168 max_height = max (1, max_height);
7169 max_height = min (total_height, max_height);
7170
7171 /* Find out the height of the text in the window. */
7172 if (it.truncate_lines_p)
7173 height = 1;
7174 else
7175 {
7176 last_height = 0;
7177 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7178 if (it.max_ascent == 0 && it.max_descent == 0)
7179 height = it.current_y + last_height;
7180 else
7181 height = it.current_y + it.max_ascent + it.max_descent;
7182 height -= it.extra_line_spacing;
7183 height = (height + unit - 1) / unit;
7184 }
7185
7186 /* Compute a suitable window start. */
7187 if (height > max_height)
7188 {
7189 height = max_height;
7190 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7191 move_it_vertically_backward (&it, (height - 1) * unit);
7192 start = it.current.pos;
7193 }
7194 else
7195 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7196 SET_MARKER_FROM_TEXT_POS (w->start, start);
7197
7198 if (EQ (Vresize_mini_windows, Qgrow_only))
7199 {
7200 /* Let it grow only, until we display an empty message, in which
7201 case the window shrinks again. */
7202 if (height > WINDOW_TOTAL_LINES (w))
7203 {
7204 int old_height = WINDOW_TOTAL_LINES (w);
7205 freeze_window_starts (f, 1);
7206 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7207 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7208 }
7209 else if (height < WINDOW_TOTAL_LINES (w)
7210 && (exact_p || BEGV == ZV))
7211 {
7212 int old_height = WINDOW_TOTAL_LINES (w);
7213 freeze_window_starts (f, 0);
7214 shrink_mini_window (w);
7215 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7216 }
7217 }
7218 else
7219 {
7220 /* Always resize to exact size needed. */
7221 if (height > WINDOW_TOTAL_LINES (w))
7222 {
7223 int old_height = WINDOW_TOTAL_LINES (w);
7224 freeze_window_starts (f, 1);
7225 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7226 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7227 }
7228 else if (height < WINDOW_TOTAL_LINES (w))
7229 {
7230 int old_height = WINDOW_TOTAL_LINES (w);
7231 freeze_window_starts (f, 0);
7232 shrink_mini_window (w);
7233
7234 if (height)
7235 {
7236 freeze_window_starts (f, 1);
7237 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7238 }
7239
7240 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7241 }
7242 }
7243
7244 if (old_current_buffer)
7245 set_buffer_internal (old_current_buffer);
7246 }
7247
7248 return window_height_changed_p;
7249 }
7250
7251
7252 /* Value is the current message, a string, or nil if there is no
7253 current message. */
7254
7255 Lisp_Object
7256 current_message ()
7257 {
7258 Lisp_Object msg;
7259
7260 if (NILP (echo_area_buffer[0]))
7261 msg = Qnil;
7262 else
7263 {
7264 with_echo_area_buffer (0, 0, current_message_1,
7265 (EMACS_INT) &msg, Qnil, 0, 0);
7266 if (NILP (msg))
7267 echo_area_buffer[0] = Qnil;
7268 }
7269
7270 return msg;
7271 }
7272
7273
7274 static int
7275 current_message_1 (a1, a2, a3, a4)
7276 EMACS_INT a1;
7277 Lisp_Object a2;
7278 EMACS_INT a3, a4;
7279 {
7280 Lisp_Object *msg = (Lisp_Object *) a1;
7281
7282 if (Z > BEG)
7283 *msg = make_buffer_string (BEG, Z, 1);
7284 else
7285 *msg = Qnil;
7286 return 0;
7287 }
7288
7289
7290 /* Push the current message on Vmessage_stack for later restauration
7291 by restore_message. Value is non-zero if the current message isn't
7292 empty. This is a relatively infrequent operation, so it's not
7293 worth optimizing. */
7294
7295 int
7296 push_message ()
7297 {
7298 Lisp_Object msg;
7299 msg = current_message ();
7300 Vmessage_stack = Fcons (msg, Vmessage_stack);
7301 return STRINGP (msg);
7302 }
7303
7304
7305 /* Restore message display from the top of Vmessage_stack. */
7306
7307 void
7308 restore_message ()
7309 {
7310 Lisp_Object msg;
7311
7312 xassert (CONSP (Vmessage_stack));
7313 msg = XCAR (Vmessage_stack);
7314 if (STRINGP (msg))
7315 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7316 else
7317 message3_nolog (msg, 0, 0);
7318 }
7319
7320
7321 /* Handler for record_unwind_protect calling pop_message. */
7322
7323 Lisp_Object
7324 pop_message_unwind (dummy)
7325 Lisp_Object dummy;
7326 {
7327 pop_message ();
7328 return Qnil;
7329 }
7330
7331 /* Pop the top-most entry off Vmessage_stack. */
7332
7333 void
7334 pop_message ()
7335 {
7336 xassert (CONSP (Vmessage_stack));
7337 Vmessage_stack = XCDR (Vmessage_stack);
7338 }
7339
7340
7341 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7342 exits. If the stack is not empty, we have a missing pop_message
7343 somewhere. */
7344
7345 void
7346 check_message_stack ()
7347 {
7348 if (!NILP (Vmessage_stack))
7349 abort ();
7350 }
7351
7352
7353 /* Truncate to NCHARS what will be displayed in the echo area the next
7354 time we display it---but don't redisplay it now. */
7355
7356 void
7357 truncate_echo_area (nchars)
7358 int nchars;
7359 {
7360 if (nchars == 0)
7361 echo_area_buffer[0] = Qnil;
7362 /* A null message buffer means that the frame hasn't really been
7363 initialized yet. Error messages get reported properly by
7364 cmd_error, so this must be just an informative message; toss it. */
7365 else if (!noninteractive
7366 && INTERACTIVE
7367 && !NILP (echo_area_buffer[0]))
7368 {
7369 struct frame *sf = SELECTED_FRAME ();
7370 if (FRAME_MESSAGE_BUF (sf))
7371 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7372 }
7373 }
7374
7375
7376 /* Helper function for truncate_echo_area. Truncate the current
7377 message to at most NCHARS characters. */
7378
7379 static int
7380 truncate_message_1 (nchars, a2, a3, a4)
7381 EMACS_INT nchars;
7382 Lisp_Object a2;
7383 EMACS_INT a3, a4;
7384 {
7385 if (BEG + nchars < Z)
7386 del_range (BEG + nchars, Z);
7387 if (Z == BEG)
7388 echo_area_buffer[0] = Qnil;
7389 return 0;
7390 }
7391
7392
7393 /* Set the current message to a substring of S or STRING.
7394
7395 If STRING is a Lisp string, set the message to the first NBYTES
7396 bytes from STRING. NBYTES zero means use the whole string. If
7397 STRING is multibyte, the message will be displayed multibyte.
7398
7399 If S is not null, set the message to the first LEN bytes of S. LEN
7400 zero means use the whole string. MULTIBYTE_P non-zero means S is
7401 multibyte. Display the message multibyte in that case. */
7402
7403 void
7404 set_message (s, string, nbytes, multibyte_p)
7405 const char *s;
7406 Lisp_Object string;
7407 int nbytes, multibyte_p;
7408 {
7409 message_enable_multibyte
7410 = ((s && multibyte_p)
7411 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7412
7413 with_echo_area_buffer (0, -1, set_message_1,
7414 (EMACS_INT) s, string, nbytes, multibyte_p);
7415 message_buf_print = 0;
7416 help_echo_showing_p = 0;
7417 }
7418
7419
7420 /* Helper function for set_message. Arguments have the same meaning
7421 as there, with A1 corresponding to S and A2 corresponding to STRING
7422 This function is called with the echo area buffer being
7423 current. */
7424
7425 static int
7426 set_message_1 (a1, a2, nbytes, multibyte_p)
7427 EMACS_INT a1;
7428 Lisp_Object a2;
7429 EMACS_INT nbytes, multibyte_p;
7430 {
7431 const char *s = (const char *) a1;
7432 Lisp_Object string = a2;
7433
7434 xassert (BEG == Z);
7435
7436 /* Change multibyteness of the echo buffer appropriately. */
7437 if (message_enable_multibyte
7438 != !NILP (current_buffer->enable_multibyte_characters))
7439 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7440
7441 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7442
7443 /* Insert new message at BEG. */
7444 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7445
7446 if (STRINGP (string))
7447 {
7448 int nchars;
7449
7450 if (nbytes == 0)
7451 nbytes = SBYTES (string);
7452 nchars = string_byte_to_char (string, nbytes);
7453
7454 /* This function takes care of single/multibyte conversion. We
7455 just have to ensure that the echo area buffer has the right
7456 setting of enable_multibyte_characters. */
7457 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7458 }
7459 else if (s)
7460 {
7461 if (nbytes == 0)
7462 nbytes = strlen (s);
7463
7464 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7465 {
7466 /* Convert from multi-byte to single-byte. */
7467 int i, c, n;
7468 unsigned char work[1];
7469
7470 /* Convert a multibyte string to single-byte. */
7471 for (i = 0; i < nbytes; i += n)
7472 {
7473 c = string_char_and_length (s + i, nbytes - i, &n);
7474 work[0] = (SINGLE_BYTE_CHAR_P (c)
7475 ? c
7476 : multibyte_char_to_unibyte (c, Qnil));
7477 insert_1_both (work, 1, 1, 1, 0, 0);
7478 }
7479 }
7480 else if (!multibyte_p
7481 && !NILP (current_buffer->enable_multibyte_characters))
7482 {
7483 /* Convert from single-byte to multi-byte. */
7484 int i, c, n;
7485 const unsigned char *msg = (const unsigned char *) s;
7486 unsigned char str[MAX_MULTIBYTE_LENGTH];
7487
7488 /* Convert a single-byte string to multibyte. */
7489 for (i = 0; i < nbytes; i++)
7490 {
7491 c = unibyte_char_to_multibyte (msg[i]);
7492 n = CHAR_STRING (c, str);
7493 insert_1_both (str, 1, n, 1, 0, 0);
7494 }
7495 }
7496 else
7497 insert_1 (s, nbytes, 1, 0, 0);
7498 }
7499
7500 return 0;
7501 }
7502
7503
7504 /* Clear messages. CURRENT_P non-zero means clear the current
7505 message. LAST_DISPLAYED_P non-zero means clear the message
7506 last displayed. */
7507
7508 void
7509 clear_message (current_p, last_displayed_p)
7510 int current_p, last_displayed_p;
7511 {
7512 if (current_p)
7513 {
7514 echo_area_buffer[0] = Qnil;
7515 message_cleared_p = 1;
7516 }
7517
7518 if (last_displayed_p)
7519 echo_area_buffer[1] = Qnil;
7520
7521 message_buf_print = 0;
7522 }
7523
7524 /* Clear garbaged frames.
7525
7526 This function is used where the old redisplay called
7527 redraw_garbaged_frames which in turn called redraw_frame which in
7528 turn called clear_frame. The call to clear_frame was a source of
7529 flickering. I believe a clear_frame is not necessary. It should
7530 suffice in the new redisplay to invalidate all current matrices,
7531 and ensure a complete redisplay of all windows. */
7532
7533 static void
7534 clear_garbaged_frames ()
7535 {
7536 if (frame_garbaged)
7537 {
7538 Lisp_Object tail, frame;
7539 int changed_count = 0;
7540
7541 FOR_EACH_FRAME (tail, frame)
7542 {
7543 struct frame *f = XFRAME (frame);
7544
7545 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7546 {
7547 if (f->resized_p)
7548 Fredraw_frame (frame);
7549 clear_current_matrices (f);
7550 changed_count++;
7551 f->garbaged = 0;
7552 f->resized_p = 0;
7553 }
7554 }
7555
7556 frame_garbaged = 0;
7557 if (changed_count)
7558 ++windows_or_buffers_changed;
7559 }
7560 }
7561
7562
7563 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7564 is non-zero update selected_frame. Value is non-zero if the
7565 mini-windows height has been changed. */
7566
7567 static int
7568 echo_area_display (update_frame_p)
7569 int update_frame_p;
7570 {
7571 Lisp_Object mini_window;
7572 struct window *w;
7573 struct frame *f;
7574 int window_height_changed_p = 0;
7575 struct frame *sf = SELECTED_FRAME ();
7576
7577 mini_window = FRAME_MINIBUF_WINDOW (sf);
7578 w = XWINDOW (mini_window);
7579 f = XFRAME (WINDOW_FRAME (w));
7580
7581 /* Don't display if frame is invisible or not yet initialized. */
7582 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7583 return 0;
7584
7585 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7586 #ifndef MAC_OS8
7587 #ifdef HAVE_WINDOW_SYSTEM
7588 /* When Emacs starts, selected_frame may be a visible terminal
7589 frame, even if we run under a window system. If we let this
7590 through, a message would be displayed on the terminal. */
7591 if (EQ (selected_frame, Vterminal_frame)
7592 && !NILP (Vwindow_system))
7593 return 0;
7594 #endif /* HAVE_WINDOW_SYSTEM */
7595 #endif
7596
7597 /* Redraw garbaged frames. */
7598 if (frame_garbaged)
7599 clear_garbaged_frames ();
7600
7601 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7602 {
7603 echo_area_window = mini_window;
7604 window_height_changed_p = display_echo_area (w);
7605 w->must_be_updated_p = 1;
7606
7607 /* Update the display, unless called from redisplay_internal.
7608 Also don't update the screen during redisplay itself. The
7609 update will happen at the end of redisplay, and an update
7610 here could cause confusion. */
7611 if (update_frame_p && !redisplaying_p)
7612 {
7613 int n = 0;
7614
7615 /* If the display update has been interrupted by pending
7616 input, update mode lines in the frame. Due to the
7617 pending input, it might have been that redisplay hasn't
7618 been called, so that mode lines above the echo area are
7619 garbaged. This looks odd, so we prevent it here. */
7620 if (!display_completed)
7621 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7622
7623 if (window_height_changed_p
7624 /* Don't do this if Emacs is shutting down. Redisplay
7625 needs to run hooks. */
7626 && !NILP (Vrun_hooks))
7627 {
7628 /* Must update other windows. Likewise as in other
7629 cases, don't let this update be interrupted by
7630 pending input. */
7631 int count = SPECPDL_INDEX ();
7632 specbind (Qredisplay_dont_pause, Qt);
7633 windows_or_buffers_changed = 1;
7634 redisplay_internal (0);
7635 unbind_to (count, Qnil);
7636 }
7637 else if (FRAME_WINDOW_P (f) && n == 0)
7638 {
7639 /* Window configuration is the same as before.
7640 Can do with a display update of the echo area,
7641 unless we displayed some mode lines. */
7642 update_single_window (w, 1);
7643 rif->flush_display (f);
7644 }
7645 else
7646 update_frame (f, 1, 1);
7647
7648 /* If cursor is in the echo area, make sure that the next
7649 redisplay displays the minibuffer, so that the cursor will
7650 be replaced with what the minibuffer wants. */
7651 if (cursor_in_echo_area)
7652 ++windows_or_buffers_changed;
7653 }
7654 }
7655 else if (!EQ (mini_window, selected_window))
7656 windows_or_buffers_changed++;
7657
7658 /* Last displayed message is now the current message. */
7659 echo_area_buffer[1] = echo_area_buffer[0];
7660
7661 /* Prevent redisplay optimization in redisplay_internal by resetting
7662 this_line_start_pos. This is done because the mini-buffer now
7663 displays the message instead of its buffer text. */
7664 if (EQ (mini_window, selected_window))
7665 CHARPOS (this_line_start_pos) = 0;
7666
7667 return window_height_changed_p;
7668 }
7669
7670
7671 \f
7672 /***********************************************************************
7673 Frame Titles
7674 ***********************************************************************/
7675
7676
7677 /* The frame title buffering code is also used by Fformat_mode_line.
7678 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
7679
7680 /* A buffer for constructing frame titles in it; allocated from the
7681 heap in init_xdisp and resized as needed in store_frame_title_char. */
7682
7683 static char *frame_title_buf;
7684
7685 /* The buffer's end, and a current output position in it. */
7686
7687 static char *frame_title_buf_end;
7688 static char *frame_title_ptr;
7689
7690
7691 /* Store a single character C for the frame title in frame_title_buf.
7692 Re-allocate frame_title_buf if necessary. */
7693
7694 static void
7695 #ifdef PROTOTYPES
7696 store_frame_title_char (char c)
7697 #else
7698 store_frame_title_char (c)
7699 char c;
7700 #endif
7701 {
7702 /* If output position has reached the end of the allocated buffer,
7703 double the buffer's size. */
7704 if (frame_title_ptr == frame_title_buf_end)
7705 {
7706 int len = frame_title_ptr - frame_title_buf;
7707 int new_size = 2 * len * sizeof *frame_title_buf;
7708 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7709 frame_title_buf_end = frame_title_buf + new_size;
7710 frame_title_ptr = frame_title_buf + len;
7711 }
7712
7713 *frame_title_ptr++ = c;
7714 }
7715
7716
7717 /* Store part of a frame title in frame_title_buf, beginning at
7718 frame_title_ptr. STR is the string to store. Do not copy
7719 characters that yield more columns than PRECISION; PRECISION <= 0
7720 means copy the whole string. Pad with spaces until FIELD_WIDTH
7721 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7722 pad. Called from display_mode_element when it is used to build a
7723 frame title. */
7724
7725 static int
7726 store_frame_title (str, field_width, precision)
7727 const unsigned char *str;
7728 int field_width, precision;
7729 {
7730 int n = 0;
7731 int dummy, nbytes;
7732
7733 /* Copy at most PRECISION chars from STR. */
7734 nbytes = strlen (str);
7735 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7736 while (nbytes--)
7737 store_frame_title_char (*str++);
7738
7739 /* Fill up with spaces until FIELD_WIDTH reached. */
7740 while (field_width > 0
7741 && n < field_width)
7742 {
7743 store_frame_title_char (' ');
7744 ++n;
7745 }
7746
7747 return n;
7748 }
7749
7750 #ifdef HAVE_WINDOW_SYSTEM
7751
7752 /* Set the title of FRAME, if it has changed. The title format is
7753 Vicon_title_format if FRAME is iconified, otherwise it is
7754 frame_title_format. */
7755
7756 static void
7757 x_consider_frame_title (frame)
7758 Lisp_Object frame;
7759 {
7760 struct frame *f = XFRAME (frame);
7761
7762 if (FRAME_WINDOW_P (f)
7763 || FRAME_MINIBUF_ONLY_P (f)
7764 || f->explicit_name)
7765 {
7766 /* Do we have more than one visible frame on this X display? */
7767 Lisp_Object tail;
7768 Lisp_Object fmt;
7769 struct buffer *obuf;
7770 int len;
7771 struct it it;
7772
7773 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7774 {
7775 Lisp_Object other_frame = XCAR (tail);
7776 struct frame *tf = XFRAME (other_frame);
7777
7778 if (tf != f
7779 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7780 && !FRAME_MINIBUF_ONLY_P (tf)
7781 && !EQ (other_frame, tip_frame)
7782 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7783 break;
7784 }
7785
7786 /* Set global variable indicating that multiple frames exist. */
7787 multiple_frames = CONSP (tail);
7788
7789 /* Switch to the buffer of selected window of the frame. Set up
7790 frame_title_ptr so that display_mode_element will output into it;
7791 then display the title. */
7792 obuf = current_buffer;
7793 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
7794 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7795 frame_title_ptr = frame_title_buf;
7796 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7797 NULL, DEFAULT_FACE_ID);
7798 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
7799 len = frame_title_ptr - frame_title_buf;
7800 frame_title_ptr = NULL;
7801 set_buffer_internal_1 (obuf);
7802
7803 /* Set the title only if it's changed. This avoids consing in
7804 the common case where it hasn't. (If it turns out that we've
7805 already wasted too much time by walking through the list with
7806 display_mode_element, then we might need to optimize at a
7807 higher level than this.) */
7808 if (! STRINGP (f->name)
7809 || SBYTES (f->name) != len
7810 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
7811 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7812 }
7813 }
7814
7815 #endif /* not HAVE_WINDOW_SYSTEM */
7816
7817
7818
7819 \f
7820 /***********************************************************************
7821 Menu Bars
7822 ***********************************************************************/
7823
7824
7825 /* Prepare for redisplay by updating menu-bar item lists when
7826 appropriate. This can call eval. */
7827
7828 void
7829 prepare_menu_bars ()
7830 {
7831 int all_windows;
7832 struct gcpro gcpro1, gcpro2;
7833 struct frame *f;
7834 Lisp_Object tooltip_frame;
7835
7836 #ifdef HAVE_WINDOW_SYSTEM
7837 tooltip_frame = tip_frame;
7838 #else
7839 tooltip_frame = Qnil;
7840 #endif
7841
7842 /* Update all frame titles based on their buffer names, etc. We do
7843 this before the menu bars so that the buffer-menu will show the
7844 up-to-date frame titles. */
7845 #ifdef HAVE_WINDOW_SYSTEM
7846 if (windows_or_buffers_changed || update_mode_lines)
7847 {
7848 Lisp_Object tail, frame;
7849
7850 FOR_EACH_FRAME (tail, frame)
7851 {
7852 f = XFRAME (frame);
7853 if (!EQ (frame, tooltip_frame)
7854 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7855 x_consider_frame_title (frame);
7856 }
7857 }
7858 #endif /* HAVE_WINDOW_SYSTEM */
7859
7860 /* Update the menu bar item lists, if appropriate. This has to be
7861 done before any actual redisplay or generation of display lines. */
7862 all_windows = (update_mode_lines
7863 || buffer_shared > 1
7864 || windows_or_buffers_changed);
7865 if (all_windows)
7866 {
7867 Lisp_Object tail, frame;
7868 int count = SPECPDL_INDEX ();
7869
7870 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7871
7872 FOR_EACH_FRAME (tail, frame)
7873 {
7874 f = XFRAME (frame);
7875
7876 /* Ignore tooltip frame. */
7877 if (EQ (frame, tooltip_frame))
7878 continue;
7879
7880 /* If a window on this frame changed size, report that to
7881 the user and clear the size-change flag. */
7882 if (FRAME_WINDOW_SIZES_CHANGED (f))
7883 {
7884 Lisp_Object functions;
7885
7886 /* Clear flag first in case we get an error below. */
7887 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7888 functions = Vwindow_size_change_functions;
7889 GCPRO2 (tail, functions);
7890
7891 while (CONSP (functions))
7892 {
7893 call1 (XCAR (functions), frame);
7894 functions = XCDR (functions);
7895 }
7896 UNGCPRO;
7897 }
7898
7899 GCPRO1 (tail);
7900 update_menu_bar (f, 0);
7901 #ifdef HAVE_WINDOW_SYSTEM
7902 update_tool_bar (f, 0);
7903 #endif
7904 UNGCPRO;
7905 }
7906
7907 unbind_to (count, Qnil);
7908 }
7909 else
7910 {
7911 struct frame *sf = SELECTED_FRAME ();
7912 update_menu_bar (sf, 1);
7913 #ifdef HAVE_WINDOW_SYSTEM
7914 update_tool_bar (sf, 1);
7915 #endif
7916 }
7917
7918 /* Motif needs this. See comment in xmenu.c. Turn it off when
7919 pending_menu_activation is not defined. */
7920 #ifdef USE_X_TOOLKIT
7921 pending_menu_activation = 0;
7922 #endif
7923 }
7924
7925
7926 /* Update the menu bar item list for frame F. This has to be done
7927 before we start to fill in any display lines, because it can call
7928 eval.
7929
7930 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7931
7932 static void
7933 update_menu_bar (f, save_match_data)
7934 struct frame *f;
7935 int save_match_data;
7936 {
7937 Lisp_Object window;
7938 register struct window *w;
7939
7940 /* If called recursively during a menu update, do nothing. This can
7941 happen when, for instance, an activate-menubar-hook causes a
7942 redisplay. */
7943 if (inhibit_menubar_update)
7944 return;
7945
7946 window = FRAME_SELECTED_WINDOW (f);
7947 w = XWINDOW (window);
7948
7949 #if 0 /* The if statement below this if statement used to include the
7950 condition !NILP (w->update_mode_line), rather than using
7951 update_mode_lines directly, and this if statement may have
7952 been added to make that condition work. Now the if
7953 statement below matches its comment, this isn't needed. */
7954 if (update_mode_lines)
7955 w->update_mode_line = Qt;
7956 #endif
7957
7958 if (FRAME_WINDOW_P (f)
7959 ?
7960 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
7961 || defined (USE_GTK)
7962 FRAME_EXTERNAL_MENU_BAR (f)
7963 #else
7964 FRAME_MENU_BAR_LINES (f) > 0
7965 #endif
7966 : FRAME_MENU_BAR_LINES (f) > 0)
7967 {
7968 /* If the user has switched buffers or windows, we need to
7969 recompute to reflect the new bindings. But we'll
7970 recompute when update_mode_lines is set too; that means
7971 that people can use force-mode-line-update to request
7972 that the menu bar be recomputed. The adverse effect on
7973 the rest of the redisplay algorithm is about the same as
7974 windows_or_buffers_changed anyway. */
7975 if (windows_or_buffers_changed
7976 /* This used to test w->update_mode_line, but we believe
7977 there is no need to recompute the menu in that case. */
7978 || update_mode_lines
7979 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7980 < BUF_MODIFF (XBUFFER (w->buffer)))
7981 != !NILP (w->last_had_star))
7982 || ((!NILP (Vtransient_mark_mode)
7983 && !NILP (XBUFFER (w->buffer)->mark_active))
7984 != !NILP (w->region_showing)))
7985 {
7986 struct buffer *prev = current_buffer;
7987 int count = SPECPDL_INDEX ();
7988
7989 specbind (Qinhibit_menubar_update, Qt);
7990
7991 set_buffer_internal_1 (XBUFFER (w->buffer));
7992 if (save_match_data)
7993 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7994 if (NILP (Voverriding_local_map_menu_flag))
7995 {
7996 specbind (Qoverriding_terminal_local_map, Qnil);
7997 specbind (Qoverriding_local_map, Qnil);
7998 }
7999
8000 /* Run the Lucid hook. */
8001 safe_run_hooks (Qactivate_menubar_hook);
8002
8003 /* If it has changed current-menubar from previous value,
8004 really recompute the menu-bar from the value. */
8005 if (! NILP (Vlucid_menu_bar_dirty_flag))
8006 call0 (Qrecompute_lucid_menubar);
8007
8008 safe_run_hooks (Qmenu_bar_update_hook);
8009 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8010
8011 /* Redisplay the menu bar in case we changed it. */
8012 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8013 || defined (USE_GTK)
8014 if (FRAME_WINDOW_P (f)
8015 #if defined (MAC_OS)
8016 /* All frames on Mac OS share the same menubar. So only the
8017 selected frame should be allowed to set it. */
8018 && f == SELECTED_FRAME ()
8019 #endif
8020 )
8021 set_frame_menubar (f, 0, 0);
8022 else
8023 /* On a terminal screen, the menu bar is an ordinary screen
8024 line, and this makes it get updated. */
8025 w->update_mode_line = Qt;
8026 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8027 /* In the non-toolkit version, the menu bar is an ordinary screen
8028 line, and this makes it get updated. */
8029 w->update_mode_line = Qt;
8030 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8031
8032 unbind_to (count, Qnil);
8033 set_buffer_internal_1 (prev);
8034 }
8035 }
8036 }
8037
8038
8039 \f
8040 /***********************************************************************
8041 Output Cursor
8042 ***********************************************************************/
8043
8044 #ifdef HAVE_WINDOW_SYSTEM
8045
8046 /* EXPORT:
8047 Nominal cursor position -- where to draw output.
8048 HPOS and VPOS are window relative glyph matrix coordinates.
8049 X and Y are window relative pixel coordinates. */
8050
8051 struct cursor_pos output_cursor;
8052
8053
8054 /* EXPORT:
8055 Set the global variable output_cursor to CURSOR. All cursor
8056 positions are relative to updated_window. */
8057
8058 void
8059 set_output_cursor (cursor)
8060 struct cursor_pos *cursor;
8061 {
8062 output_cursor.hpos = cursor->hpos;
8063 output_cursor.vpos = cursor->vpos;
8064 output_cursor.x = cursor->x;
8065 output_cursor.y = cursor->y;
8066 }
8067
8068
8069 /* EXPORT for RIF:
8070 Set a nominal cursor position.
8071
8072 HPOS and VPOS are column/row positions in a window glyph matrix. X
8073 and Y are window text area relative pixel positions.
8074
8075 If this is done during an update, updated_window will contain the
8076 window that is being updated and the position is the future output
8077 cursor position for that window. If updated_window is null, use
8078 selected_window and display the cursor at the given position. */
8079
8080 void
8081 x_cursor_to (vpos, hpos, y, x)
8082 int vpos, hpos, y, x;
8083 {
8084 struct window *w;
8085
8086 /* If updated_window is not set, work on selected_window. */
8087 if (updated_window)
8088 w = updated_window;
8089 else
8090 w = XWINDOW (selected_window);
8091
8092 /* Set the output cursor. */
8093 output_cursor.hpos = hpos;
8094 output_cursor.vpos = vpos;
8095 output_cursor.x = x;
8096 output_cursor.y = y;
8097
8098 /* If not called as part of an update, really display the cursor.
8099 This will also set the cursor position of W. */
8100 if (updated_window == NULL)
8101 {
8102 BLOCK_INPUT;
8103 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8104 if (rif->flush_display_optional)
8105 rif->flush_display_optional (SELECTED_FRAME ());
8106 UNBLOCK_INPUT;
8107 }
8108 }
8109
8110 #endif /* HAVE_WINDOW_SYSTEM */
8111
8112 \f
8113 /***********************************************************************
8114 Tool-bars
8115 ***********************************************************************/
8116
8117 #ifdef HAVE_WINDOW_SYSTEM
8118
8119 /* Where the mouse was last time we reported a mouse event. */
8120
8121 FRAME_PTR last_mouse_frame;
8122
8123 /* Tool-bar item index of the item on which a mouse button was pressed
8124 or -1. */
8125
8126 int last_tool_bar_item;
8127
8128
8129 /* Update the tool-bar item list for frame F. This has to be done
8130 before we start to fill in any display lines. Called from
8131 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8132 and restore it here. */
8133
8134 static void
8135 update_tool_bar (f, save_match_data)
8136 struct frame *f;
8137 int save_match_data;
8138 {
8139 #ifdef USE_GTK
8140 int do_update = FRAME_EXTERNAL_TOOL_BAR(f);
8141 #else
8142 int do_update = WINDOWP (f->tool_bar_window)
8143 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8144 #endif
8145
8146 if (do_update)
8147 {
8148 Lisp_Object window;
8149 struct window *w;
8150
8151 window = FRAME_SELECTED_WINDOW (f);
8152 w = XWINDOW (window);
8153
8154 /* If the user has switched buffers or windows, we need to
8155 recompute to reflect the new bindings. But we'll
8156 recompute when update_mode_lines is set too; that means
8157 that people can use force-mode-line-update to request
8158 that the menu bar be recomputed. The adverse effect on
8159 the rest of the redisplay algorithm is about the same as
8160 windows_or_buffers_changed anyway. */
8161 if (windows_or_buffers_changed
8162 || !NILP (w->update_mode_line)
8163 || update_mode_lines
8164 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8165 < BUF_MODIFF (XBUFFER (w->buffer)))
8166 != !NILP (w->last_had_star))
8167 || ((!NILP (Vtransient_mark_mode)
8168 && !NILP (XBUFFER (w->buffer)->mark_active))
8169 != !NILP (w->region_showing)))
8170 {
8171 struct buffer *prev = current_buffer;
8172 int count = SPECPDL_INDEX ();
8173 Lisp_Object old_tool_bar;
8174 struct gcpro gcpro1;
8175
8176 /* Set current_buffer to the buffer of the selected
8177 window of the frame, so that we get the right local
8178 keymaps. */
8179 set_buffer_internal_1 (XBUFFER (w->buffer));
8180
8181 /* Save match data, if we must. */
8182 if (save_match_data)
8183 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8184
8185 /* Make sure that we don't accidentally use bogus keymaps. */
8186 if (NILP (Voverriding_local_map_menu_flag))
8187 {
8188 specbind (Qoverriding_terminal_local_map, Qnil);
8189 specbind (Qoverriding_local_map, Qnil);
8190 }
8191
8192 old_tool_bar = f->tool_bar_items;
8193 GCPRO1 (old_tool_bar);
8194
8195 /* Build desired tool-bar items from keymaps. */
8196 BLOCK_INPUT;
8197 f->tool_bar_items
8198 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
8199 UNBLOCK_INPUT;
8200
8201 /* Redisplay the tool-bar if we changed it. */
8202 if (! NILP (Fequal (old_tool_bar, f->tool_bar_items)))
8203 w->update_mode_line = Qt;
8204
8205 unbind_to (count, Qnil);
8206 set_buffer_internal_1 (prev);
8207 }
8208 }
8209 }
8210
8211
8212 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8213 F's desired tool-bar contents. F->tool_bar_items must have
8214 been set up previously by calling prepare_menu_bars. */
8215
8216 static void
8217 build_desired_tool_bar_string (f)
8218 struct frame *f;
8219 {
8220 int i, size, size_needed;
8221 struct gcpro gcpro1, gcpro2, gcpro3;
8222 Lisp_Object image, plist, props;
8223
8224 image = plist = props = Qnil;
8225 GCPRO3 (image, plist, props);
8226
8227 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8228 Otherwise, make a new string. */
8229
8230 /* The size of the string we might be able to reuse. */
8231 size = (STRINGP (f->desired_tool_bar_string)
8232 ? SCHARS (f->desired_tool_bar_string)
8233 : 0);
8234
8235 /* We need one space in the string for each image. */
8236 size_needed = f->n_tool_bar_items;
8237
8238 /* Reuse f->desired_tool_bar_string, if possible. */
8239 if (size < size_needed || NILP (f->desired_tool_bar_string))
8240 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8241 make_number (' '));
8242 else
8243 {
8244 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8245 Fremove_text_properties (make_number (0), make_number (size),
8246 props, f->desired_tool_bar_string);
8247 }
8248
8249 /* Put a `display' property on the string for the images to display,
8250 put a `menu_item' property on tool-bar items with a value that
8251 is the index of the item in F's tool-bar item vector. */
8252 for (i = 0; i < f->n_tool_bar_items; ++i)
8253 {
8254 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8255
8256 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8257 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8258 int hmargin, vmargin, relief, idx, end;
8259 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
8260
8261 /* If image is a vector, choose the image according to the
8262 button state. */
8263 image = PROP (TOOL_BAR_ITEM_IMAGES);
8264 if (VECTORP (image))
8265 {
8266 if (enabled_p)
8267 idx = (selected_p
8268 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8269 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8270 else
8271 idx = (selected_p
8272 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8273 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8274
8275 xassert (ASIZE (image) >= idx);
8276 image = AREF (image, idx);
8277 }
8278 else
8279 idx = -1;
8280
8281 /* Ignore invalid image specifications. */
8282 if (!valid_image_p (image))
8283 continue;
8284
8285 /* Display the tool-bar button pressed, or depressed. */
8286 plist = Fcopy_sequence (XCDR (image));
8287
8288 /* Compute margin and relief to draw. */
8289 relief = (tool_bar_button_relief >= 0
8290 ? tool_bar_button_relief
8291 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8292 hmargin = vmargin = relief;
8293
8294 if (INTEGERP (Vtool_bar_button_margin)
8295 && XINT (Vtool_bar_button_margin) > 0)
8296 {
8297 hmargin += XFASTINT (Vtool_bar_button_margin);
8298 vmargin += XFASTINT (Vtool_bar_button_margin);
8299 }
8300 else if (CONSP (Vtool_bar_button_margin))
8301 {
8302 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8303 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8304 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8305
8306 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8307 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8308 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8309 }
8310
8311 if (auto_raise_tool_bar_buttons_p)
8312 {
8313 /* Add a `:relief' property to the image spec if the item is
8314 selected. */
8315 if (selected_p)
8316 {
8317 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8318 hmargin -= relief;
8319 vmargin -= relief;
8320 }
8321 }
8322 else
8323 {
8324 /* If image is selected, display it pressed, i.e. with a
8325 negative relief. If it's not selected, display it with a
8326 raised relief. */
8327 plist = Fplist_put (plist, QCrelief,
8328 (selected_p
8329 ? make_number (-relief)
8330 : make_number (relief)));
8331 hmargin -= relief;
8332 vmargin -= relief;
8333 }
8334
8335 /* Put a margin around the image. */
8336 if (hmargin || vmargin)
8337 {
8338 if (hmargin == vmargin)
8339 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8340 else
8341 plist = Fplist_put (plist, QCmargin,
8342 Fcons (make_number (hmargin),
8343 make_number (vmargin)));
8344 }
8345
8346 /* If button is not enabled, and we don't have special images
8347 for the disabled state, make the image appear disabled by
8348 applying an appropriate algorithm to it. */
8349 if (!enabled_p && idx < 0)
8350 plist = Fplist_put (plist, QCconversion, Qdisabled);
8351
8352 /* Put a `display' text property on the string for the image to
8353 display. Put a `menu-item' property on the string that gives
8354 the start of this item's properties in the tool-bar items
8355 vector. */
8356 image = Fcons (Qimage, plist);
8357 props = list4 (Qdisplay, image,
8358 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8359
8360 /* Let the last image hide all remaining spaces in the tool bar
8361 string. The string can be longer than needed when we reuse a
8362 previous string. */
8363 if (i + 1 == f->n_tool_bar_items)
8364 end = SCHARS (f->desired_tool_bar_string);
8365 else
8366 end = i + 1;
8367 Fadd_text_properties (make_number (i), make_number (end),
8368 props, f->desired_tool_bar_string);
8369 #undef PROP
8370 }
8371
8372 UNGCPRO;
8373 }
8374
8375
8376 /* Display one line of the tool-bar of frame IT->f. */
8377
8378 static void
8379 display_tool_bar_line (it)
8380 struct it *it;
8381 {
8382 struct glyph_row *row = it->glyph_row;
8383 int max_x = it->last_visible_x;
8384 struct glyph *last;
8385
8386 prepare_desired_row (row);
8387 row->y = it->current_y;
8388
8389 /* Note that this isn't made use of if the face hasn't a box,
8390 so there's no need to check the face here. */
8391 it->start_of_box_run_p = 1;
8392
8393 while (it->current_x < max_x)
8394 {
8395 int x_before, x, n_glyphs_before, i, nglyphs;
8396
8397 /* Get the next display element. */
8398 if (!get_next_display_element (it))
8399 break;
8400
8401 /* Produce glyphs. */
8402 x_before = it->current_x;
8403 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8404 PRODUCE_GLYPHS (it);
8405
8406 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8407 i = 0;
8408 x = x_before;
8409 while (i < nglyphs)
8410 {
8411 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
8412
8413 if (x + glyph->pixel_width > max_x)
8414 {
8415 /* Glyph doesn't fit on line. */
8416 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8417 it->current_x = x;
8418 goto out;
8419 }
8420
8421 ++it->hpos;
8422 x += glyph->pixel_width;
8423 ++i;
8424 }
8425
8426 /* Stop at line ends. */
8427 if (ITERATOR_AT_END_OF_LINE_P (it))
8428 break;
8429
8430 set_iterator_to_next (it, 1);
8431 }
8432
8433 out:;
8434
8435 row->displays_text_p = row->used[TEXT_AREA] != 0;
8436 extend_face_to_end_of_line (it);
8437 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8438 last->right_box_line_p = 1;
8439 if (last == row->glyphs[TEXT_AREA])
8440 last->left_box_line_p = 1;
8441 compute_line_metrics (it);
8442
8443 /* If line is empty, make it occupy the rest of the tool-bar. */
8444 if (!row->displays_text_p)
8445 {
8446 row->height = row->phys_height = it->last_visible_y - row->y;
8447 row->ascent = row->phys_ascent = 0;
8448 }
8449
8450 row->full_width_p = 1;
8451 row->continued_p = 0;
8452 row->truncated_on_left_p = 0;
8453 row->truncated_on_right_p = 0;
8454
8455 it->current_x = it->hpos = 0;
8456 it->current_y += row->height;
8457 ++it->vpos;
8458 ++it->glyph_row;
8459 }
8460
8461
8462 /* Value is the number of screen lines needed to make all tool-bar
8463 items of frame F visible. */
8464
8465 static int
8466 tool_bar_lines_needed (f)
8467 struct frame *f;
8468 {
8469 struct window *w = XWINDOW (f->tool_bar_window);
8470 struct it it;
8471
8472 /* Initialize an iterator for iteration over
8473 F->desired_tool_bar_string in the tool-bar window of frame F. */
8474 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8475 it.first_visible_x = 0;
8476 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8477 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8478
8479 while (!ITERATOR_AT_END_P (&it))
8480 {
8481 it.glyph_row = w->desired_matrix->rows;
8482 clear_glyph_row (it.glyph_row);
8483 display_tool_bar_line (&it);
8484 }
8485
8486 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
8487 }
8488
8489
8490 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8491 0, 1, 0,
8492 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8493 (frame)
8494 Lisp_Object frame;
8495 {
8496 struct frame *f;
8497 struct window *w;
8498 int nlines = 0;
8499
8500 if (NILP (frame))
8501 frame = selected_frame;
8502 else
8503 CHECK_FRAME (frame);
8504 f = XFRAME (frame);
8505
8506 if (WINDOWP (f->tool_bar_window)
8507 || (w = XWINDOW (f->tool_bar_window),
8508 WINDOW_TOTAL_LINES (w) > 0))
8509 {
8510 update_tool_bar (f, 1);
8511 if (f->n_tool_bar_items)
8512 {
8513 build_desired_tool_bar_string (f);
8514 nlines = tool_bar_lines_needed (f);
8515 }
8516 }
8517
8518 return make_number (nlines);
8519 }
8520
8521
8522 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
8523 height should be changed. */
8524
8525 static int
8526 redisplay_tool_bar (f)
8527 struct frame *f;
8528 {
8529 struct window *w;
8530 struct it it;
8531 struct glyph_row *row;
8532 int change_height_p = 0;
8533
8534 #ifdef USE_GTK
8535 if (FRAME_EXTERNAL_TOOL_BAR(f))
8536 update_frame_tool_bar (f);
8537 return 0;
8538 #endif
8539
8540 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8541 do anything. This means you must start with tool-bar-lines
8542 non-zero to get the auto-sizing effect. Or in other words, you
8543 can turn off tool-bars by specifying tool-bar-lines zero. */
8544 if (!WINDOWP (f->tool_bar_window)
8545 || (w = XWINDOW (f->tool_bar_window),
8546 WINDOW_TOTAL_LINES (w) == 0))
8547 return 0;
8548
8549 /* Set up an iterator for the tool-bar window. */
8550 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8551 it.first_visible_x = 0;
8552 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8553 row = it.glyph_row;
8554
8555 /* Build a string that represents the contents of the tool-bar. */
8556 build_desired_tool_bar_string (f);
8557 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8558
8559 /* Display as many lines as needed to display all tool-bar items. */
8560 while (it.current_y < it.last_visible_y)
8561 display_tool_bar_line (&it);
8562
8563 /* It doesn't make much sense to try scrolling in the tool-bar
8564 window, so don't do it. */
8565 w->desired_matrix->no_scrolling_p = 1;
8566 w->must_be_updated_p = 1;
8567
8568 if (auto_resize_tool_bars_p)
8569 {
8570 int nlines;
8571
8572 /* If we couldn't display everything, change the tool-bar's
8573 height. */
8574 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8575 change_height_p = 1;
8576
8577 /* If there are blank lines at the end, except for a partially
8578 visible blank line at the end that is smaller than
8579 FRAME_LINE_HEIGHT, change the tool-bar's height. */
8580 row = it.glyph_row - 1;
8581 if (!row->displays_text_p
8582 && row->height >= FRAME_LINE_HEIGHT (f))
8583 change_height_p = 1;
8584
8585 /* If row displays tool-bar items, but is partially visible,
8586 change the tool-bar's height. */
8587 if (row->displays_text_p
8588 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8589 change_height_p = 1;
8590
8591 /* Resize windows as needed by changing the `tool-bar-lines'
8592 frame parameter. */
8593 if (change_height_p
8594 && (nlines = tool_bar_lines_needed (f),
8595 nlines != WINDOW_TOTAL_LINES (w)))
8596 {
8597 extern Lisp_Object Qtool_bar_lines;
8598 Lisp_Object frame;
8599 int old_height = WINDOW_TOTAL_LINES (w);
8600
8601 XSETFRAME (frame, f);
8602 clear_glyph_matrix (w->desired_matrix);
8603 Fmodify_frame_parameters (frame,
8604 Fcons (Fcons (Qtool_bar_lines,
8605 make_number (nlines)),
8606 Qnil));
8607 if (WINDOW_TOTAL_LINES (w) != old_height)
8608 fonts_changed_p = 1;
8609 }
8610 }
8611
8612 return change_height_p;
8613 }
8614
8615
8616 /* Get information about the tool-bar item which is displayed in GLYPH
8617 on frame F. Return in *PROP_IDX the index where tool-bar item
8618 properties start in F->tool_bar_items. Value is zero if
8619 GLYPH doesn't display a tool-bar item. */
8620
8621 static int
8622 tool_bar_item_info (f, glyph, prop_idx)
8623 struct frame *f;
8624 struct glyph *glyph;
8625 int *prop_idx;
8626 {
8627 Lisp_Object prop;
8628 int success_p;
8629 int charpos;
8630
8631 /* This function can be called asynchronously, which means we must
8632 exclude any possibility that Fget_text_property signals an
8633 error. */
8634 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8635 charpos = max (0, charpos);
8636
8637 /* Get the text property `menu-item' at pos. The value of that
8638 property is the start index of this item's properties in
8639 F->tool_bar_items. */
8640 prop = Fget_text_property (make_number (charpos),
8641 Qmenu_item, f->current_tool_bar_string);
8642 if (INTEGERP (prop))
8643 {
8644 *prop_idx = XINT (prop);
8645 success_p = 1;
8646 }
8647 else
8648 success_p = 0;
8649
8650 return success_p;
8651 }
8652
8653 \f
8654 /* Get information about the tool-bar item at position X/Y on frame F.
8655 Return in *GLYPH a pointer to the glyph of the tool-bar item in
8656 the current matrix of the tool-bar window of F, or NULL if not
8657 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8658 item in F->tool_bar_items. Value is
8659
8660 -1 if X/Y is not on a tool-bar item
8661 0 if X/Y is on the same item that was highlighted before.
8662 1 otherwise. */
8663
8664 static int
8665 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
8666 struct frame *f;
8667 int x, y;
8668 struct glyph **glyph;
8669 int *hpos, *vpos, *prop_idx;
8670 {
8671 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8672 struct window *w = XWINDOW (f->tool_bar_window);
8673 int area;
8674
8675 /* Find the glyph under X/Y. */
8676 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area, 0);
8677 if (*glyph == NULL)
8678 return -1;
8679
8680 /* Get the start of this tool-bar item's properties in
8681 f->tool_bar_items. */
8682 if (!tool_bar_item_info (f, *glyph, prop_idx))
8683 return -1;
8684
8685 /* Is mouse on the highlighted item? */
8686 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
8687 && *vpos >= dpyinfo->mouse_face_beg_row
8688 && *vpos <= dpyinfo->mouse_face_end_row
8689 && (*vpos > dpyinfo->mouse_face_beg_row
8690 || *hpos >= dpyinfo->mouse_face_beg_col)
8691 && (*vpos < dpyinfo->mouse_face_end_row
8692 || *hpos < dpyinfo->mouse_face_end_col
8693 || dpyinfo->mouse_face_past_end))
8694 return 0;
8695
8696 return 1;
8697 }
8698
8699
8700 /* EXPORT:
8701 Handle mouse button event on the tool-bar of frame F, at
8702 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
8703 0 for button release. MODIFIERS is event modifiers for button
8704 release. */
8705
8706 void
8707 handle_tool_bar_click (f, x, y, down_p, modifiers)
8708 struct frame *f;
8709 int x, y, down_p;
8710 unsigned int modifiers;
8711 {
8712 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8713 struct window *w = XWINDOW (f->tool_bar_window);
8714 int hpos, vpos, prop_idx;
8715 struct glyph *glyph;
8716 Lisp_Object enabled_p;
8717
8718 /* If not on the highlighted tool-bar item, return. */
8719 frame_to_window_pixel_xy (w, &x, &y);
8720 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
8721 return;
8722
8723 /* If item is disabled, do nothing. */
8724 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8725 if (NILP (enabled_p))
8726 return;
8727
8728 if (down_p)
8729 {
8730 /* Show item in pressed state. */
8731 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
8732 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
8733 last_tool_bar_item = prop_idx;
8734 }
8735 else
8736 {
8737 Lisp_Object key, frame;
8738 struct input_event event;
8739
8740 /* Show item in released state. */
8741 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
8742 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
8743
8744 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
8745
8746 XSETFRAME (frame, f);
8747 event.kind = TOOL_BAR_EVENT;
8748 event.frame_or_window = frame;
8749 event.arg = frame;
8750 kbd_buffer_store_event (&event);
8751
8752 event.kind = TOOL_BAR_EVENT;
8753 event.frame_or_window = frame;
8754 event.arg = key;
8755 event.modifiers = modifiers;
8756 kbd_buffer_store_event (&event);
8757 last_tool_bar_item = -1;
8758 }
8759 }
8760
8761
8762 /* Possibly highlight a tool-bar item on frame F when mouse moves to
8763 tool-bar window-relative coordinates X/Y. Called from
8764 note_mouse_highlight. */
8765
8766 static void
8767 note_tool_bar_highlight (f, x, y)
8768 struct frame *f;
8769 int x, y;
8770 {
8771 Lisp_Object window = f->tool_bar_window;
8772 struct window *w = XWINDOW (window);
8773 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8774 int hpos, vpos;
8775 struct glyph *glyph;
8776 struct glyph_row *row;
8777 int i;
8778 Lisp_Object enabled_p;
8779 int prop_idx;
8780 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
8781 int mouse_down_p, rc;
8782
8783 /* Function note_mouse_highlight is called with negative x(y
8784 values when mouse moves outside of the frame. */
8785 if (x <= 0 || y <= 0)
8786 {
8787 clear_mouse_face (dpyinfo);
8788 return;
8789 }
8790
8791 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
8792 if (rc < 0)
8793 {
8794 /* Not on tool-bar item. */
8795 clear_mouse_face (dpyinfo);
8796 return;
8797 }
8798 else if (rc == 0)
8799 /* On same tool-bar item as before. */
8800 goto set_help_echo;
8801
8802 clear_mouse_face (dpyinfo);
8803
8804 /* Mouse is down, but on different tool-bar item? */
8805 mouse_down_p = (dpyinfo->grabbed
8806 && f == last_mouse_frame
8807 && FRAME_LIVE_P (f));
8808 if (mouse_down_p
8809 && last_tool_bar_item != prop_idx)
8810 return;
8811
8812 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
8813 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
8814
8815 /* If tool-bar item is not enabled, don't highlight it. */
8816 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8817 if (!NILP (enabled_p))
8818 {
8819 /* Compute the x-position of the glyph. In front and past the
8820 image is a space. We include this in the highlighted area. */
8821 row = MATRIX_ROW (w->current_matrix, vpos);
8822 for (i = x = 0; i < hpos; ++i)
8823 x += row->glyphs[TEXT_AREA][i].pixel_width;
8824
8825 /* Record this as the current active region. */
8826 dpyinfo->mouse_face_beg_col = hpos;
8827 dpyinfo->mouse_face_beg_row = vpos;
8828 dpyinfo->mouse_face_beg_x = x;
8829 dpyinfo->mouse_face_beg_y = row->y;
8830 dpyinfo->mouse_face_past_end = 0;
8831
8832 dpyinfo->mouse_face_end_col = hpos + 1;
8833 dpyinfo->mouse_face_end_row = vpos;
8834 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
8835 dpyinfo->mouse_face_end_y = row->y;
8836 dpyinfo->mouse_face_window = window;
8837 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
8838
8839 /* Display it as active. */
8840 show_mouse_face (dpyinfo, draw);
8841 dpyinfo->mouse_face_image_state = draw;
8842 }
8843
8844 set_help_echo:
8845
8846 /* Set help_echo_string to a help string to display for this tool-bar item.
8847 XTread_socket does the rest. */
8848 help_echo_object = help_echo_window = Qnil;
8849 help_echo_pos = -1;
8850 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
8851 if (NILP (help_echo_string))
8852 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
8853 }
8854
8855 #endif /* HAVE_WINDOW_SYSTEM */
8856
8857
8858 \f
8859 /***********************************************************************
8860 Fringes
8861 ***********************************************************************/
8862
8863 #ifdef HAVE_WINDOW_SYSTEM
8864
8865 /* An arrow like this: `<-'. */
8866 static unsigned char left_bits[] = {
8867 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
8868
8869 /* Right truncation arrow bitmap `->'. */
8870 static unsigned char right_bits[] = {
8871 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
8872
8873 /* Marker for continued lines. */
8874 static unsigned char continued_bits[] = {
8875 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
8876
8877 /* Marker for continuation lines. */
8878 static unsigned char continuation_bits[] = {
8879 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
8880
8881 /* Overlay arrow bitmap. A triangular arrow. */
8882 static unsigned char ov_bits[] = {
8883 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
8884
8885 /* Bitmap drawn to indicate lines not displaying text if
8886 `indicate-empty-lines' is non-nil. */
8887 static unsigned char zv_bits[] = {
8888 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8889 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8890 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8891 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8892 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8893 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8894 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8895 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00};
8896
8897 struct fringe_bitmap fringe_bitmaps[MAX_FRINGE_BITMAPS] =
8898 {
8899 { 0, 0, 0, NULL /* NO_FRINGE_BITMAP */ },
8900 { 8, sizeof (left_bits), 0, left_bits },
8901 { 8, sizeof (right_bits), 0, right_bits },
8902 { 8, sizeof (continued_bits), 0, continued_bits },
8903 { 8, sizeof (continuation_bits), 0, continuation_bits },
8904 { 8, sizeof (ov_bits), 0, ov_bits },
8905 { 8, sizeof (zv_bits), 3, zv_bits }
8906 };
8907
8908
8909 /* Draw the bitmap WHICH in one of the left or right fringes of
8910 window W. ROW is the glyph row for which to display the bitmap; it
8911 determines the vertical position at which the bitmap has to be
8912 drawn. */
8913
8914 static void
8915 draw_fringe_bitmap (w, row, which, left_p)
8916 struct window *w;
8917 struct glyph_row *row;
8918 enum fringe_bitmap_type which;
8919 int left_p;
8920 {
8921 struct frame *f = XFRAME (WINDOW_FRAME (w));
8922 struct draw_fringe_bitmap_params p;
8923
8924 /* Convert row to frame coordinates. */
8925 p.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
8926
8927 p.which = which;
8928 p.wd = fringe_bitmaps[which].width;
8929
8930 p.h = fringe_bitmaps[which].height;
8931 p.dh = (fringe_bitmaps[which].period
8932 ? (p.y % fringe_bitmaps[which].period)
8933 : 0);
8934 p.h -= p.dh;
8935 /* Clip bitmap if too high. */
8936 if (p.h > row->height)
8937 p.h = row->height;
8938
8939 p.face = FACE_FROM_ID (f, FRINGE_FACE_ID);
8940 PREPARE_FACE_FOR_DISPLAY (f, p.face);
8941
8942 /* Clear left fringe if no bitmap to draw or if bitmap doesn't fill
8943 the fringe. */
8944 p.bx = -1;
8945 if (left_p)
8946 {
8947 int wd = WINDOW_LEFT_FRINGE_WIDTH (w);
8948 int x = window_box_left (w, (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
8949 ? LEFT_MARGIN_AREA
8950 : TEXT_AREA));
8951 if (p.wd > wd)
8952 p.wd = wd;
8953 p.x = x - p.wd - (wd - p.wd) / 2;
8954
8955 if (p.wd < wd || row->height > p.h)
8956 {
8957 /* If W has a vertical border to its left, don't draw over it. */
8958 wd -= ((!WINDOW_LEFTMOST_P (w)
8959 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
8960 ? 1 : 0);
8961 p.bx = x - wd;
8962 p.nx = wd;
8963 }
8964 }
8965 else
8966 {
8967 int x = window_box_right (w,
8968 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
8969 ? RIGHT_MARGIN_AREA
8970 : TEXT_AREA));
8971 int wd = WINDOW_RIGHT_FRINGE_WIDTH (w);
8972 if (p.wd > wd)
8973 p.wd = wd;
8974 p.x = x + (wd - p.wd) / 2;
8975 /* Clear right fringe if no bitmap to draw of if bitmap doesn't fill
8976 the fringe. */
8977 if (p.wd < wd || row->height > p.h)
8978 {
8979 p.bx = x;
8980 p.nx = wd;
8981 }
8982 }
8983
8984 if (p.bx >= 0)
8985 {
8986 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
8987
8988 p.by = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, row->y));
8989 p.ny = row->visible_height;
8990 }
8991
8992 /* Adjust y to the offset in the row to start drawing the bitmap. */
8993 p.y += (row->height - p.h) / 2;
8994
8995 rif->draw_fringe_bitmap (w, row, &p);
8996 }
8997
8998 /* Draw fringe bitmaps for glyph row ROW on window W. Call this
8999 function with input blocked. */
9000
9001 void
9002 draw_row_fringe_bitmaps (w, row)
9003 struct window *w;
9004 struct glyph_row *row;
9005 {
9006 struct frame *f = XFRAME (w->frame);
9007 enum fringe_bitmap_type bitmap;
9008
9009 xassert (interrupt_input_blocked);
9010
9011 /* If row is completely invisible, because of vscrolling, we
9012 don't have to draw anything. */
9013 if (row->visible_height <= 0)
9014 return;
9015
9016 if (WINDOW_LEFT_FRINGE_WIDTH (w) != 0)
9017 {
9018 /* Decide which bitmap to draw in the left fringe. */
9019 if (row->overlay_arrow_p)
9020 bitmap = OVERLAY_ARROW_BITMAP;
9021 else if (row->truncated_on_left_p)
9022 bitmap = LEFT_TRUNCATION_BITMAP;
9023 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
9024 bitmap = CONTINUATION_LINE_BITMAP;
9025 else if (row->indicate_empty_line_p)
9026 bitmap = ZV_LINE_BITMAP;
9027 else
9028 bitmap = NO_FRINGE_BITMAP;
9029
9030 draw_fringe_bitmap (w, row, bitmap, 1);
9031 }
9032
9033 if (WINDOW_RIGHT_FRINGE_WIDTH (w) != 0)
9034 {
9035 /* Decide which bitmap to draw in the right fringe. */
9036 if (row->truncated_on_right_p)
9037 bitmap = RIGHT_TRUNCATION_BITMAP;
9038 else if (row->continued_p)
9039 bitmap = CONTINUED_LINE_BITMAP;
9040 else if (row->indicate_empty_line_p && WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
9041 bitmap = ZV_LINE_BITMAP;
9042 else
9043 bitmap = NO_FRINGE_BITMAP;
9044
9045 draw_fringe_bitmap (w, row, bitmap, 0);
9046 }
9047 }
9048
9049
9050 /* Compute actual fringe widths */
9051
9052 void
9053 compute_fringe_widths (f, redraw)
9054 struct frame *f;
9055 int redraw;
9056 {
9057 int o_left = FRAME_LEFT_FRINGE_WIDTH (f);
9058 int o_right = FRAME_RIGHT_FRINGE_WIDTH (f);
9059 int o_cols = FRAME_FRINGE_COLS (f);
9060
9061 Lisp_Object left_fringe = Fassq (Qleft_fringe, f->param_alist);
9062 Lisp_Object right_fringe = Fassq (Qright_fringe, f->param_alist);
9063 int left_fringe_width, right_fringe_width;
9064
9065 if (!NILP (left_fringe))
9066 left_fringe = Fcdr (left_fringe);
9067 if (!NILP (right_fringe))
9068 right_fringe = Fcdr (right_fringe);
9069
9070 left_fringe_width = ((NILP (left_fringe) || !INTEGERP (left_fringe)) ? 8 :
9071 XINT (left_fringe));
9072 right_fringe_width = ((NILP (right_fringe) || !INTEGERP (right_fringe)) ? 8 :
9073 XINT (right_fringe));
9074
9075 if (left_fringe_width || right_fringe_width)
9076 {
9077 int left_wid = left_fringe_width >= 0 ? left_fringe_width : -left_fringe_width;
9078 int right_wid = right_fringe_width >= 0 ? right_fringe_width : -right_fringe_width;
9079 int conf_wid = left_wid + right_wid;
9080 int font_wid = FRAME_COLUMN_WIDTH (f);
9081 int cols = (left_wid + right_wid + font_wid-1) / font_wid;
9082 int real_wid = cols * font_wid;
9083 if (left_wid && right_wid)
9084 {
9085 if (left_fringe_width < 0)
9086 {
9087 /* Left fringe width is fixed, adjust right fringe if necessary */
9088 FRAME_LEFT_FRINGE_WIDTH (f) = left_wid;
9089 FRAME_RIGHT_FRINGE_WIDTH (f) = real_wid - left_wid;
9090 }
9091 else if (right_fringe_width < 0)
9092 {
9093 /* Right fringe width is fixed, adjust left fringe if necessary */
9094 FRAME_LEFT_FRINGE_WIDTH (f) = real_wid - right_wid;
9095 FRAME_RIGHT_FRINGE_WIDTH (f) = right_wid;
9096 }
9097 else
9098 {
9099 /* Adjust both fringes with an equal amount.
9100 Note that we are doing integer arithmetic here, so don't
9101 lose a pixel if the total width is an odd number. */
9102 int fill = real_wid - conf_wid;
9103 FRAME_LEFT_FRINGE_WIDTH (f) = left_wid + fill/2;
9104 FRAME_RIGHT_FRINGE_WIDTH (f) = right_wid + fill - fill/2;
9105 }
9106 }
9107 else if (left_fringe_width)
9108 {
9109 FRAME_LEFT_FRINGE_WIDTH (f) = real_wid;
9110 FRAME_RIGHT_FRINGE_WIDTH (f) = 0;
9111 }
9112 else
9113 {
9114 FRAME_LEFT_FRINGE_WIDTH (f) = 0;
9115 FRAME_RIGHT_FRINGE_WIDTH (f) = real_wid;
9116 }
9117 FRAME_FRINGE_COLS (f) = cols;
9118 }
9119 else
9120 {
9121 FRAME_LEFT_FRINGE_WIDTH (f) = 0;
9122 FRAME_RIGHT_FRINGE_WIDTH (f) = 0;
9123 FRAME_FRINGE_COLS (f) = 0;
9124 }
9125
9126 if (redraw && FRAME_VISIBLE_P (f))
9127 if (o_left != FRAME_LEFT_FRINGE_WIDTH (f) ||
9128 o_right != FRAME_RIGHT_FRINGE_WIDTH (f) ||
9129 o_cols != FRAME_FRINGE_COLS (f))
9130 redraw_frame (f);
9131 }
9132
9133 #endif /* HAVE_WINDOW_SYSTEM */
9134
9135
9136 \f
9137 /************************************************************************
9138 Horizontal scrolling
9139 ************************************************************************/
9140
9141 static int hscroll_window_tree P_ ((Lisp_Object));
9142 static int hscroll_windows P_ ((Lisp_Object));
9143
9144 /* For all leaf windows in the window tree rooted at WINDOW, set their
9145 hscroll value so that PT is (i) visible in the window, and (ii) so
9146 that it is not within a certain margin at the window's left and
9147 right border. Value is non-zero if any window's hscroll has been
9148 changed. */
9149
9150 static int
9151 hscroll_window_tree (window)
9152 Lisp_Object window;
9153 {
9154 int hscrolled_p = 0;
9155 int hscroll_relative_p = FLOATP (Vhscroll_step);
9156 int hscroll_step_abs = 0;
9157 double hscroll_step_rel = 0;
9158
9159 if (hscroll_relative_p)
9160 {
9161 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9162 if (hscroll_step_rel < 0)
9163 {
9164 hscroll_relative_p = 0;
9165 hscroll_step_abs = 0;
9166 }
9167 }
9168 else if (INTEGERP (Vhscroll_step))
9169 {
9170 hscroll_step_abs = XINT (Vhscroll_step);
9171 if (hscroll_step_abs < 0)
9172 hscroll_step_abs = 0;
9173 }
9174 else
9175 hscroll_step_abs = 0;
9176
9177 while (WINDOWP (window))
9178 {
9179 struct window *w = XWINDOW (window);
9180
9181 if (WINDOWP (w->hchild))
9182 hscrolled_p |= hscroll_window_tree (w->hchild);
9183 else if (WINDOWP (w->vchild))
9184 hscrolled_p |= hscroll_window_tree (w->vchild);
9185 else if (w->cursor.vpos >= 0)
9186 {
9187 int h_margin;
9188 int text_area_width;
9189 struct glyph_row *current_cursor_row
9190 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9191 struct glyph_row *desired_cursor_row
9192 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9193 struct glyph_row *cursor_row
9194 = (desired_cursor_row->enabled_p
9195 ? desired_cursor_row
9196 : current_cursor_row);
9197
9198 text_area_width = window_box_width (w, TEXT_AREA);
9199
9200 /* Scroll when cursor is inside this scroll margin. */
9201 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9202
9203 if ((XFASTINT (w->hscroll)
9204 && w->cursor.x <= h_margin)
9205 || (cursor_row->enabled_p
9206 && cursor_row->truncated_on_right_p
9207 && (w->cursor.x >= text_area_width - h_margin)))
9208 {
9209 struct it it;
9210 int hscroll;
9211 struct buffer *saved_current_buffer;
9212 int pt;
9213 int wanted_x;
9214
9215 /* Find point in a display of infinite width. */
9216 saved_current_buffer = current_buffer;
9217 current_buffer = XBUFFER (w->buffer);
9218
9219 if (w == XWINDOW (selected_window))
9220 pt = BUF_PT (current_buffer);
9221 else
9222 {
9223 pt = marker_position (w->pointm);
9224 pt = max (BEGV, pt);
9225 pt = min (ZV, pt);
9226 }
9227
9228 /* Move iterator to pt starting at cursor_row->start in
9229 a line with infinite width. */
9230 init_to_row_start (&it, w, cursor_row);
9231 it.last_visible_x = INFINITY;
9232 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9233 current_buffer = saved_current_buffer;
9234
9235 /* Position cursor in window. */
9236 if (!hscroll_relative_p && hscroll_step_abs == 0)
9237 hscroll = max (0, it.current_x - text_area_width / 2)
9238 / FRAME_COLUMN_WIDTH (it.f);
9239 else if (w->cursor.x >= text_area_width - h_margin)
9240 {
9241 if (hscroll_relative_p)
9242 wanted_x = text_area_width * (1 - hscroll_step_rel)
9243 - h_margin;
9244 else
9245 wanted_x = text_area_width
9246 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9247 - h_margin;
9248 hscroll
9249 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9250 }
9251 else
9252 {
9253 if (hscroll_relative_p)
9254 wanted_x = text_area_width * hscroll_step_rel
9255 + h_margin;
9256 else
9257 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9258 + h_margin;
9259 hscroll
9260 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9261 }
9262 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9263
9264 /* Don't call Fset_window_hscroll if value hasn't
9265 changed because it will prevent redisplay
9266 optimizations. */
9267 if (XFASTINT (w->hscroll) != hscroll)
9268 {
9269 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9270 w->hscroll = make_number (hscroll);
9271 hscrolled_p = 1;
9272 }
9273 }
9274 }
9275
9276 window = w->next;
9277 }
9278
9279 /* Value is non-zero if hscroll of any leaf window has been changed. */
9280 return hscrolled_p;
9281 }
9282
9283
9284 /* Set hscroll so that cursor is visible and not inside horizontal
9285 scroll margins for all windows in the tree rooted at WINDOW. See
9286 also hscroll_window_tree above. Value is non-zero if any window's
9287 hscroll has been changed. If it has, desired matrices on the frame
9288 of WINDOW are cleared. */
9289
9290 static int
9291 hscroll_windows (window)
9292 Lisp_Object window;
9293 {
9294 int hscrolled_p;
9295
9296 if (automatic_hscrolling_p)
9297 {
9298 hscrolled_p = hscroll_window_tree (window);
9299 if (hscrolled_p)
9300 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9301 }
9302 else
9303 hscrolled_p = 0;
9304 return hscrolled_p;
9305 }
9306
9307
9308 \f
9309 /************************************************************************
9310 Redisplay
9311 ************************************************************************/
9312
9313 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9314 to a non-zero value. This is sometimes handy to have in a debugger
9315 session. */
9316
9317 #if GLYPH_DEBUG
9318
9319 /* First and last unchanged row for try_window_id. */
9320
9321 int debug_first_unchanged_at_end_vpos;
9322 int debug_last_unchanged_at_beg_vpos;
9323
9324 /* Delta vpos and y. */
9325
9326 int debug_dvpos, debug_dy;
9327
9328 /* Delta in characters and bytes for try_window_id. */
9329
9330 int debug_delta, debug_delta_bytes;
9331
9332 /* Values of window_end_pos and window_end_vpos at the end of
9333 try_window_id. */
9334
9335 EMACS_INT debug_end_pos, debug_end_vpos;
9336
9337 /* Append a string to W->desired_matrix->method. FMT is a printf
9338 format string. A1...A9 are a supplement for a variable-length
9339 argument list. If trace_redisplay_p is non-zero also printf the
9340 resulting string to stderr. */
9341
9342 static void
9343 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9344 struct window *w;
9345 char *fmt;
9346 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9347 {
9348 char buffer[512];
9349 char *method = w->desired_matrix->method;
9350 int len = strlen (method);
9351 int size = sizeof w->desired_matrix->method;
9352 int remaining = size - len - 1;
9353
9354 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9355 if (len && remaining)
9356 {
9357 method[len] = '|';
9358 --remaining, ++len;
9359 }
9360
9361 strncpy (method + len, buffer, remaining);
9362
9363 if (trace_redisplay_p)
9364 fprintf (stderr, "%p (%s): %s\n",
9365 w,
9366 ((BUFFERP (w->buffer)
9367 && STRINGP (XBUFFER (w->buffer)->name))
9368 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9369 : "no buffer"),
9370 buffer);
9371 }
9372
9373 #endif /* GLYPH_DEBUG */
9374
9375
9376 /* Value is non-zero if all changes in window W, which displays
9377 current_buffer, are in the text between START and END. START is a
9378 buffer position, END is given as a distance from Z. Used in
9379 redisplay_internal for display optimization. */
9380
9381 static INLINE int
9382 text_outside_line_unchanged_p (w, start, end)
9383 struct window *w;
9384 int start, end;
9385 {
9386 int unchanged_p = 1;
9387
9388 /* If text or overlays have changed, see where. */
9389 if (XFASTINT (w->last_modified) < MODIFF
9390 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9391 {
9392 /* Gap in the line? */
9393 if (GPT < start || Z - GPT < end)
9394 unchanged_p = 0;
9395
9396 /* Changes start in front of the line, or end after it? */
9397 if (unchanged_p
9398 && (BEG_UNCHANGED < start - 1
9399 || END_UNCHANGED < end))
9400 unchanged_p = 0;
9401
9402 /* If selective display, can't optimize if changes start at the
9403 beginning of the line. */
9404 if (unchanged_p
9405 && INTEGERP (current_buffer->selective_display)
9406 && XINT (current_buffer->selective_display) > 0
9407 && (BEG_UNCHANGED < start || GPT <= start))
9408 unchanged_p = 0;
9409
9410 /* If there are overlays at the start or end of the line, these
9411 may have overlay strings with newlines in them. A change at
9412 START, for instance, may actually concern the display of such
9413 overlay strings as well, and they are displayed on different
9414 lines. So, quickly rule out this case. (For the future, it
9415 might be desirable to implement something more telling than
9416 just BEG/END_UNCHANGED.) */
9417 if (unchanged_p)
9418 {
9419 if (BEG + BEG_UNCHANGED == start
9420 && overlay_touches_p (start))
9421 unchanged_p = 0;
9422 if (END_UNCHANGED == end
9423 && overlay_touches_p (Z - end))
9424 unchanged_p = 0;
9425 }
9426 }
9427
9428 return unchanged_p;
9429 }
9430
9431
9432 /* Do a frame update, taking possible shortcuts into account. This is
9433 the main external entry point for redisplay.
9434
9435 If the last redisplay displayed an echo area message and that message
9436 is no longer requested, we clear the echo area or bring back the
9437 mini-buffer if that is in use. */
9438
9439 void
9440 redisplay ()
9441 {
9442 redisplay_internal (0);
9443 }
9444
9445
9446 /* Return 1 if point moved out of or into a composition. Otherwise
9447 return 0. PREV_BUF and PREV_PT are the last point buffer and
9448 position. BUF and PT are the current point buffer and position. */
9449
9450 int
9451 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9452 struct buffer *prev_buf, *buf;
9453 int prev_pt, pt;
9454 {
9455 int start, end;
9456 Lisp_Object prop;
9457 Lisp_Object buffer;
9458
9459 XSETBUFFER (buffer, buf);
9460 /* Check a composition at the last point if point moved within the
9461 same buffer. */
9462 if (prev_buf == buf)
9463 {
9464 if (prev_pt == pt)
9465 /* Point didn't move. */
9466 return 0;
9467
9468 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9469 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9470 && COMPOSITION_VALID_P (start, end, prop)
9471 && start < prev_pt && end > prev_pt)
9472 /* The last point was within the composition. Return 1 iff
9473 point moved out of the composition. */
9474 return (pt <= start || pt >= end);
9475 }
9476
9477 /* Check a composition at the current point. */
9478 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9479 && find_composition (pt, -1, &start, &end, &prop, buffer)
9480 && COMPOSITION_VALID_P (start, end, prop)
9481 && start < pt && end > pt);
9482 }
9483
9484
9485 /* Reconsider the setting of B->clip_changed which is displayed
9486 in window W. */
9487
9488 static INLINE void
9489 reconsider_clip_changes (w, b)
9490 struct window *w;
9491 struct buffer *b;
9492 {
9493 if (b->clip_changed
9494 && !NILP (w->window_end_valid)
9495 && w->current_matrix->buffer == b
9496 && w->current_matrix->zv == BUF_ZV (b)
9497 && w->current_matrix->begv == BUF_BEGV (b))
9498 b->clip_changed = 0;
9499
9500 /* If display wasn't paused, and W is not a tool bar window, see if
9501 point has been moved into or out of a composition. In that case,
9502 we set b->clip_changed to 1 to force updating the screen. If
9503 b->clip_changed has already been set to 1, we can skip this
9504 check. */
9505 if (!b->clip_changed
9506 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9507 {
9508 int pt;
9509
9510 if (w == XWINDOW (selected_window))
9511 pt = BUF_PT (current_buffer);
9512 else
9513 pt = marker_position (w->pointm);
9514
9515 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
9516 || pt != XINT (w->last_point))
9517 && check_point_in_composition (w->current_matrix->buffer,
9518 XINT (w->last_point),
9519 XBUFFER (w->buffer), pt))
9520 b->clip_changed = 1;
9521 }
9522 }
9523 \f
9524 #define STOP_POLLING \
9525 do { if (! polling_stopped_here) stop_polling (); \
9526 polling_stopped_here = 1; } while (0)
9527
9528 #define RESUME_POLLING \
9529 do { if (polling_stopped_here) start_polling (); \
9530 polling_stopped_here = 0; } while (0)
9531
9532
9533 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9534 response to any user action; therefore, we should preserve the echo
9535 area. (Actually, our caller does that job.) Perhaps in the future
9536 avoid recentering windows if it is not necessary; currently that
9537 causes some problems. */
9538
9539 static void
9540 redisplay_internal (preserve_echo_area)
9541 int preserve_echo_area;
9542 {
9543 struct window *w = XWINDOW (selected_window);
9544 struct frame *f = XFRAME (w->frame);
9545 int pause;
9546 int must_finish = 0;
9547 struct text_pos tlbufpos, tlendpos;
9548 int number_of_visible_frames;
9549 int count;
9550 struct frame *sf = SELECTED_FRAME ();
9551 int polling_stopped_here = 0;
9552
9553 /* Non-zero means redisplay has to consider all windows on all
9554 frames. Zero means, only selected_window is considered. */
9555 int consider_all_windows_p;
9556
9557 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9558
9559 /* No redisplay if running in batch mode or frame is not yet fully
9560 initialized, or redisplay is explicitly turned off by setting
9561 Vinhibit_redisplay. */
9562 if (noninteractive
9563 || !NILP (Vinhibit_redisplay)
9564 || !f->glyphs_initialized_p)
9565 return;
9566
9567 /* The flag redisplay_performed_directly_p is set by
9568 direct_output_for_insert when it already did the whole screen
9569 update necessary. */
9570 if (redisplay_performed_directly_p)
9571 {
9572 redisplay_performed_directly_p = 0;
9573 if (!hscroll_windows (selected_window))
9574 return;
9575 }
9576
9577 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9578 if (popup_activated ())
9579 return;
9580 #endif
9581
9582 /* I don't think this happens but let's be paranoid. */
9583 if (redisplaying_p)
9584 return;
9585
9586 /* Record a function that resets redisplaying_p to its old value
9587 when we leave this function. */
9588 count = SPECPDL_INDEX ();
9589 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
9590 ++redisplaying_p;
9591 specbind (Qinhibit_free_realized_faces, Qnil);
9592
9593 retry:
9594 pause = 0;
9595 reconsider_clip_changes (w, current_buffer);
9596
9597 /* If new fonts have been loaded that make a glyph matrix adjustment
9598 necessary, do it. */
9599 if (fonts_changed_p)
9600 {
9601 adjust_glyphs (NULL);
9602 ++windows_or_buffers_changed;
9603 fonts_changed_p = 0;
9604 }
9605
9606 /* If face_change_count is non-zero, init_iterator will free all
9607 realized faces, which includes the faces referenced from current
9608 matrices. So, we can't reuse current matrices in this case. */
9609 if (face_change_count)
9610 ++windows_or_buffers_changed;
9611
9612 if (! FRAME_WINDOW_P (sf)
9613 && previous_terminal_frame != sf)
9614 {
9615 /* Since frames on an ASCII terminal share the same display
9616 area, displaying a different frame means redisplay the whole
9617 thing. */
9618 windows_or_buffers_changed++;
9619 SET_FRAME_GARBAGED (sf);
9620 XSETFRAME (Vterminal_frame, sf);
9621 }
9622 previous_terminal_frame = sf;
9623
9624 /* Set the visible flags for all frames. Do this before checking
9625 for resized or garbaged frames; they want to know if their frames
9626 are visible. See the comment in frame.h for
9627 FRAME_SAMPLE_VISIBILITY. */
9628 {
9629 Lisp_Object tail, frame;
9630
9631 number_of_visible_frames = 0;
9632
9633 FOR_EACH_FRAME (tail, frame)
9634 {
9635 struct frame *f = XFRAME (frame);
9636
9637 FRAME_SAMPLE_VISIBILITY (f);
9638 if (FRAME_VISIBLE_P (f))
9639 ++number_of_visible_frames;
9640 clear_desired_matrices (f);
9641 }
9642 }
9643
9644 /* Notice any pending interrupt request to change frame size. */
9645 do_pending_window_change (1);
9646
9647 /* Clear frames marked as garbaged. */
9648 if (frame_garbaged)
9649 clear_garbaged_frames ();
9650
9651 /* Build menubar and tool-bar items. */
9652 prepare_menu_bars ();
9653
9654 if (windows_or_buffers_changed)
9655 update_mode_lines++;
9656
9657 /* Detect case that we need to write or remove a star in the mode line. */
9658 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
9659 {
9660 w->update_mode_line = Qt;
9661 if (buffer_shared > 1)
9662 update_mode_lines++;
9663 }
9664
9665 /* If %c is in the mode line, update it if needed. */
9666 if (!NILP (w->column_number_displayed)
9667 /* This alternative quickly identifies a common case
9668 where no change is needed. */
9669 && !(PT == XFASTINT (w->last_point)
9670 && XFASTINT (w->last_modified) >= MODIFF
9671 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9672 && (XFASTINT (w->column_number_displayed)
9673 != (int) current_column ())) /* iftc */
9674 w->update_mode_line = Qt;
9675
9676 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
9677
9678 /* The variable buffer_shared is set in redisplay_window and
9679 indicates that we redisplay a buffer in different windows. See
9680 there. */
9681 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
9682 || cursor_type_changed);
9683
9684 /* If specs for an arrow have changed, do thorough redisplay
9685 to ensure we remove any arrow that should no longer exist. */
9686 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
9687 || ! EQ (Voverlay_arrow_string, last_arrow_string))
9688 consider_all_windows_p = windows_or_buffers_changed = 1;
9689
9690 /* Normally the message* functions will have already displayed and
9691 updated the echo area, but the frame may have been trashed, or
9692 the update may have been preempted, so display the echo area
9693 again here. Checking message_cleared_p captures the case that
9694 the echo area should be cleared. */
9695 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
9696 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
9697 || (message_cleared_p
9698 && minibuf_level == 0
9699 /* If the mini-window is currently selected, this means the
9700 echo-area doesn't show through. */
9701 && !MINI_WINDOW_P (XWINDOW (selected_window))))
9702 {
9703 int window_height_changed_p = echo_area_display (0);
9704 must_finish = 1;
9705
9706 /* If we don't display the current message, don't clear the
9707 message_cleared_p flag, because, if we did, we wouldn't clear
9708 the echo area in the next redisplay which doesn't preserve
9709 the echo area. */
9710 if (!display_last_displayed_message_p)
9711 message_cleared_p = 0;
9712
9713 if (fonts_changed_p)
9714 goto retry;
9715 else if (window_height_changed_p)
9716 {
9717 consider_all_windows_p = 1;
9718 ++update_mode_lines;
9719 ++windows_or_buffers_changed;
9720
9721 /* If window configuration was changed, frames may have been
9722 marked garbaged. Clear them or we will experience
9723 surprises wrt scrolling. */
9724 if (frame_garbaged)
9725 clear_garbaged_frames ();
9726 }
9727 }
9728 else if (EQ (selected_window, minibuf_window)
9729 && (current_buffer->clip_changed
9730 || XFASTINT (w->last_modified) < MODIFF
9731 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9732 && resize_mini_window (w, 0))
9733 {
9734 /* Resized active mini-window to fit the size of what it is
9735 showing if its contents might have changed. */
9736 must_finish = 1;
9737 consider_all_windows_p = 1;
9738 ++windows_or_buffers_changed;
9739 ++update_mode_lines;
9740
9741 /* If window configuration was changed, frames may have been
9742 marked garbaged. Clear them or we will experience
9743 surprises wrt scrolling. */
9744 if (frame_garbaged)
9745 clear_garbaged_frames ();
9746 }
9747
9748
9749 /* If showing the region, and mark has changed, we must redisplay
9750 the whole window. The assignment to this_line_start_pos prevents
9751 the optimization directly below this if-statement. */
9752 if (((!NILP (Vtransient_mark_mode)
9753 && !NILP (XBUFFER (w->buffer)->mark_active))
9754 != !NILP (w->region_showing))
9755 || (!NILP (w->region_showing)
9756 && !EQ (w->region_showing,
9757 Fmarker_position (XBUFFER (w->buffer)->mark))))
9758 CHARPOS (this_line_start_pos) = 0;
9759
9760 /* Optimize the case that only the line containing the cursor in the
9761 selected window has changed. Variables starting with this_ are
9762 set in display_line and record information about the line
9763 containing the cursor. */
9764 tlbufpos = this_line_start_pos;
9765 tlendpos = this_line_end_pos;
9766 if (!consider_all_windows_p
9767 && CHARPOS (tlbufpos) > 0
9768 && NILP (w->update_mode_line)
9769 && !current_buffer->clip_changed
9770 && !current_buffer->prevent_redisplay_optimizations_p
9771 && FRAME_VISIBLE_P (XFRAME (w->frame))
9772 && !FRAME_OBSCURED_P (XFRAME (w->frame))
9773 /* Make sure recorded data applies to current buffer, etc. */
9774 && this_line_buffer == current_buffer
9775 && current_buffer == XBUFFER (w->buffer)
9776 && NILP (w->force_start)
9777 && NILP (w->optional_new_start)
9778 /* Point must be on the line that we have info recorded about. */
9779 && PT >= CHARPOS (tlbufpos)
9780 && PT <= Z - CHARPOS (tlendpos)
9781 /* All text outside that line, including its final newline,
9782 must be unchanged */
9783 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
9784 CHARPOS (tlendpos)))
9785 {
9786 if (CHARPOS (tlbufpos) > BEGV
9787 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
9788 && (CHARPOS (tlbufpos) == ZV
9789 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
9790 /* Former continuation line has disappeared by becoming empty */
9791 goto cancel;
9792 else if (XFASTINT (w->last_modified) < MODIFF
9793 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
9794 || MINI_WINDOW_P (w))
9795 {
9796 /* We have to handle the case of continuation around a
9797 wide-column character (See the comment in indent.c around
9798 line 885).
9799
9800 For instance, in the following case:
9801
9802 -------- Insert --------
9803 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
9804 J_I_ ==> J_I_ `^^' are cursors.
9805 ^^ ^^
9806 -------- --------
9807
9808 As we have to redraw the line above, we should goto cancel. */
9809
9810 struct it it;
9811 int line_height_before = this_line_pixel_height;
9812
9813 /* Note that start_display will handle the case that the
9814 line starting at tlbufpos is a continuation lines. */
9815 start_display (&it, w, tlbufpos);
9816
9817 /* Implementation note: It this still necessary? */
9818 if (it.current_x != this_line_start_x)
9819 goto cancel;
9820
9821 TRACE ((stderr, "trying display optimization 1\n"));
9822 w->cursor.vpos = -1;
9823 overlay_arrow_seen = 0;
9824 it.vpos = this_line_vpos;
9825 it.current_y = this_line_y;
9826 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
9827 display_line (&it);
9828
9829 /* If line contains point, is not continued,
9830 and ends at same distance from eob as before, we win */
9831 if (w->cursor.vpos >= 0
9832 /* Line is not continued, otherwise this_line_start_pos
9833 would have been set to 0 in display_line. */
9834 && CHARPOS (this_line_start_pos)
9835 /* Line ends as before. */
9836 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
9837 /* Line has same height as before. Otherwise other lines
9838 would have to be shifted up or down. */
9839 && this_line_pixel_height == line_height_before)
9840 {
9841 /* If this is not the window's last line, we must adjust
9842 the charstarts of the lines below. */
9843 if (it.current_y < it.last_visible_y)
9844 {
9845 struct glyph_row *row
9846 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
9847 int delta, delta_bytes;
9848
9849 if (Z - CHARPOS (tlendpos) == ZV)
9850 {
9851 /* This line ends at end of (accessible part of)
9852 buffer. There is no newline to count. */
9853 delta = (Z
9854 - CHARPOS (tlendpos)
9855 - MATRIX_ROW_START_CHARPOS (row));
9856 delta_bytes = (Z_BYTE
9857 - BYTEPOS (tlendpos)
9858 - MATRIX_ROW_START_BYTEPOS (row));
9859 }
9860 else
9861 {
9862 /* This line ends in a newline. Must take
9863 account of the newline and the rest of the
9864 text that follows. */
9865 delta = (Z
9866 - CHARPOS (tlendpos)
9867 - MATRIX_ROW_START_CHARPOS (row));
9868 delta_bytes = (Z_BYTE
9869 - BYTEPOS (tlendpos)
9870 - MATRIX_ROW_START_BYTEPOS (row));
9871 }
9872
9873 increment_matrix_positions (w->current_matrix,
9874 this_line_vpos + 1,
9875 w->current_matrix->nrows,
9876 delta, delta_bytes);
9877 }
9878
9879 /* If this row displays text now but previously didn't,
9880 or vice versa, w->window_end_vpos may have to be
9881 adjusted. */
9882 if ((it.glyph_row - 1)->displays_text_p)
9883 {
9884 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
9885 XSETINT (w->window_end_vpos, this_line_vpos);
9886 }
9887 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
9888 && this_line_vpos > 0)
9889 XSETINT (w->window_end_vpos, this_line_vpos - 1);
9890 w->window_end_valid = Qnil;
9891
9892 /* Update hint: No need to try to scroll in update_window. */
9893 w->desired_matrix->no_scrolling_p = 1;
9894
9895 #if GLYPH_DEBUG
9896 *w->desired_matrix->method = 0;
9897 debug_method_add (w, "optimization 1");
9898 #endif
9899 goto update;
9900 }
9901 else
9902 goto cancel;
9903 }
9904 else if (/* Cursor position hasn't changed. */
9905 PT == XFASTINT (w->last_point)
9906 /* Make sure the cursor was last displayed
9907 in this window. Otherwise we have to reposition it. */
9908 && 0 <= w->cursor.vpos
9909 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
9910 {
9911 if (!must_finish)
9912 {
9913 do_pending_window_change (1);
9914
9915 /* We used to always goto end_of_redisplay here, but this
9916 isn't enough if we have a blinking cursor. */
9917 if (w->cursor_off_p == w->last_cursor_off_p)
9918 goto end_of_redisplay;
9919 }
9920 goto update;
9921 }
9922 /* If highlighting the region, or if the cursor is in the echo area,
9923 then we can't just move the cursor. */
9924 else if (! (!NILP (Vtransient_mark_mode)
9925 && !NILP (current_buffer->mark_active))
9926 && (EQ (selected_window, current_buffer->last_selected_window)
9927 || highlight_nonselected_windows)
9928 && NILP (w->region_showing)
9929 && NILP (Vshow_trailing_whitespace)
9930 && !cursor_in_echo_area)
9931 {
9932 struct it it;
9933 struct glyph_row *row;
9934
9935 /* Skip from tlbufpos to PT and see where it is. Note that
9936 PT may be in invisible text. If so, we will end at the
9937 next visible position. */
9938 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
9939 NULL, DEFAULT_FACE_ID);
9940 it.current_x = this_line_start_x;
9941 it.current_y = this_line_y;
9942 it.vpos = this_line_vpos;
9943
9944 /* The call to move_it_to stops in front of PT, but
9945 moves over before-strings. */
9946 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
9947
9948 if (it.vpos == this_line_vpos
9949 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
9950 row->enabled_p))
9951 {
9952 xassert (this_line_vpos == it.vpos);
9953 xassert (this_line_y == it.current_y);
9954 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9955 #if GLYPH_DEBUG
9956 *w->desired_matrix->method = 0;
9957 debug_method_add (w, "optimization 3");
9958 #endif
9959 goto update;
9960 }
9961 else
9962 goto cancel;
9963 }
9964
9965 cancel:
9966 /* Text changed drastically or point moved off of line. */
9967 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
9968 }
9969
9970 CHARPOS (this_line_start_pos) = 0;
9971 consider_all_windows_p |= buffer_shared > 1;
9972 ++clear_face_cache_count;
9973
9974
9975 /* Build desired matrices, and update the display. If
9976 consider_all_windows_p is non-zero, do it for all windows on all
9977 frames. Otherwise do it for selected_window, only. */
9978
9979 if (consider_all_windows_p)
9980 {
9981 Lisp_Object tail, frame;
9982 int i, n = 0, size = 50;
9983 struct frame **updated
9984 = (struct frame **) alloca (size * sizeof *updated);
9985
9986 /* Clear the face cache eventually. */
9987 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
9988 {
9989 clear_face_cache (0);
9990 clear_face_cache_count = 0;
9991 }
9992
9993 /* Recompute # windows showing selected buffer. This will be
9994 incremented each time such a window is displayed. */
9995 buffer_shared = 0;
9996
9997 FOR_EACH_FRAME (tail, frame)
9998 {
9999 struct frame *f = XFRAME (frame);
10000
10001 if (FRAME_WINDOW_P (f) || f == sf)
10002 {
10003 #ifdef HAVE_WINDOW_SYSTEM
10004 if (clear_face_cache_count % 50 == 0
10005 && FRAME_WINDOW_P (f))
10006 clear_image_cache (f, 0);
10007 #endif /* HAVE_WINDOW_SYSTEM */
10008
10009 /* Mark all the scroll bars to be removed; we'll redeem
10010 the ones we want when we redisplay their windows. */
10011 if (condemn_scroll_bars_hook)
10012 condemn_scroll_bars_hook (f);
10013
10014 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10015 redisplay_windows (FRAME_ROOT_WINDOW (f));
10016
10017 /* Any scroll bars which redisplay_windows should have
10018 nuked should now go away. */
10019 if (judge_scroll_bars_hook)
10020 judge_scroll_bars_hook (f);
10021
10022 /* If fonts changed, display again. */
10023 /* ??? rms: I suspect it is a mistake to jump all the way
10024 back to retry here. It should just retry this frame. */
10025 if (fonts_changed_p)
10026 goto retry;
10027
10028 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10029 {
10030 /* See if we have to hscroll. */
10031 if (hscroll_windows (f->root_window))
10032 goto retry;
10033
10034 /* Prevent various kinds of signals during display
10035 update. stdio is not robust about handling
10036 signals, which can cause an apparent I/O
10037 error. */
10038 if (interrupt_input)
10039 unrequest_sigio ();
10040 STOP_POLLING;
10041
10042 /* Update the display. */
10043 set_window_update_flags (XWINDOW (f->root_window), 1);
10044 pause |= update_frame (f, 0, 0);
10045 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10046 if (pause)
10047 break;
10048 #endif
10049
10050 if (n == size)
10051 {
10052 int nbytes = size * sizeof *updated;
10053 struct frame **p = (struct frame **) alloca (2 * nbytes);
10054 bcopy (updated, p, nbytes);
10055 size *= 2;
10056 }
10057
10058 updated[n++] = f;
10059 }
10060 }
10061 }
10062
10063 /* Do the mark_window_display_accurate after all windows have
10064 been redisplayed because this call resets flags in buffers
10065 which are needed for proper redisplay. */
10066 for (i = 0; i < n; ++i)
10067 {
10068 struct frame *f = updated[i];
10069 mark_window_display_accurate (f->root_window, 1);
10070 if (frame_up_to_date_hook)
10071 frame_up_to_date_hook (f);
10072 }
10073 }
10074 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10075 {
10076 Lisp_Object mini_window;
10077 struct frame *mini_frame;
10078
10079 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10080 /* Use list_of_error, not Qerror, so that
10081 we catch only errors and don't run the debugger. */
10082 internal_condition_case_1 (redisplay_window_1, selected_window,
10083 list_of_error,
10084 redisplay_window_error);
10085
10086 /* Compare desired and current matrices, perform output. */
10087
10088 update:
10089 /* If fonts changed, display again. */
10090 if (fonts_changed_p)
10091 goto retry;
10092
10093 /* Prevent various kinds of signals during display update.
10094 stdio is not robust about handling signals,
10095 which can cause an apparent I/O error. */
10096 if (interrupt_input)
10097 unrequest_sigio ();
10098 STOP_POLLING;
10099
10100 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10101 {
10102 if (hscroll_windows (selected_window))
10103 goto retry;
10104
10105 XWINDOW (selected_window)->must_be_updated_p = 1;
10106 pause = update_frame (sf, 0, 0);
10107 }
10108
10109 /* We may have called echo_area_display at the top of this
10110 function. If the echo area is on another frame, that may
10111 have put text on a frame other than the selected one, so the
10112 above call to update_frame would not have caught it. Catch
10113 it here. */
10114 mini_window = FRAME_MINIBUF_WINDOW (sf);
10115 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10116
10117 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10118 {
10119 XWINDOW (mini_window)->must_be_updated_p = 1;
10120 pause |= update_frame (mini_frame, 0, 0);
10121 if (!pause && hscroll_windows (mini_window))
10122 goto retry;
10123 }
10124 }
10125
10126 /* If display was paused because of pending input, make sure we do a
10127 thorough update the next time. */
10128 if (pause)
10129 {
10130 /* Prevent the optimization at the beginning of
10131 redisplay_internal that tries a single-line update of the
10132 line containing the cursor in the selected window. */
10133 CHARPOS (this_line_start_pos) = 0;
10134
10135 /* Let the overlay arrow be updated the next time. */
10136 if (!NILP (last_arrow_position))
10137 {
10138 last_arrow_position = Qt;
10139 last_arrow_string = Qt;
10140 }
10141
10142 /* If we pause after scrolling, some rows in the current
10143 matrices of some windows are not valid. */
10144 if (!WINDOW_FULL_WIDTH_P (w)
10145 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10146 update_mode_lines = 1;
10147 }
10148 else
10149 {
10150 if (!consider_all_windows_p)
10151 {
10152 /* This has already been done above if
10153 consider_all_windows_p is set. */
10154 mark_window_display_accurate_1 (w, 1);
10155
10156 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
10157 last_arrow_string = Voverlay_arrow_string;
10158
10159 if (frame_up_to_date_hook != 0)
10160 frame_up_to_date_hook (sf);
10161 }
10162
10163 update_mode_lines = 0;
10164 windows_or_buffers_changed = 0;
10165 cursor_type_changed = 0;
10166 }
10167
10168 /* Start SIGIO interrupts coming again. Having them off during the
10169 code above makes it less likely one will discard output, but not
10170 impossible, since there might be stuff in the system buffer here.
10171 But it is much hairier to try to do anything about that. */
10172 if (interrupt_input)
10173 request_sigio ();
10174 RESUME_POLLING;
10175
10176 /* If a frame has become visible which was not before, redisplay
10177 again, so that we display it. Expose events for such a frame
10178 (which it gets when becoming visible) don't call the parts of
10179 redisplay constructing glyphs, so simply exposing a frame won't
10180 display anything in this case. So, we have to display these
10181 frames here explicitly. */
10182 if (!pause)
10183 {
10184 Lisp_Object tail, frame;
10185 int new_count = 0;
10186
10187 FOR_EACH_FRAME (tail, frame)
10188 {
10189 int this_is_visible = 0;
10190
10191 if (XFRAME (frame)->visible)
10192 this_is_visible = 1;
10193 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10194 if (XFRAME (frame)->visible)
10195 this_is_visible = 1;
10196
10197 if (this_is_visible)
10198 new_count++;
10199 }
10200
10201 if (new_count != number_of_visible_frames)
10202 windows_or_buffers_changed++;
10203 }
10204
10205 /* Change frame size now if a change is pending. */
10206 do_pending_window_change (1);
10207
10208 /* If we just did a pending size change, or have additional
10209 visible frames, redisplay again. */
10210 if (windows_or_buffers_changed && !pause)
10211 goto retry;
10212
10213 end_of_redisplay:
10214 unbind_to (count, Qnil);
10215 RESUME_POLLING;
10216 }
10217
10218
10219 /* Redisplay, but leave alone any recent echo area message unless
10220 another message has been requested in its place.
10221
10222 This is useful in situations where you need to redisplay but no
10223 user action has occurred, making it inappropriate for the message
10224 area to be cleared. See tracking_off and
10225 wait_reading_process_input for examples of these situations.
10226
10227 FROM_WHERE is an integer saying from where this function was
10228 called. This is useful for debugging. */
10229
10230 void
10231 redisplay_preserve_echo_area (from_where)
10232 int from_where;
10233 {
10234 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10235
10236 if (!NILP (echo_area_buffer[1]))
10237 {
10238 /* We have a previously displayed message, but no current
10239 message. Redisplay the previous message. */
10240 display_last_displayed_message_p = 1;
10241 redisplay_internal (1);
10242 display_last_displayed_message_p = 0;
10243 }
10244 else
10245 redisplay_internal (1);
10246 }
10247
10248
10249 /* Function registered with record_unwind_protect in
10250 redisplay_internal. Reset redisplaying_p to the value it had
10251 before redisplay_internal was called, and clear
10252 prevent_freeing_realized_faces_p. */
10253
10254 static Lisp_Object
10255 unwind_redisplay (old_redisplaying_p)
10256 Lisp_Object old_redisplaying_p;
10257 {
10258 redisplaying_p = XFASTINT (old_redisplaying_p);
10259 return Qnil;
10260 }
10261
10262
10263 /* Mark the display of window W as accurate or inaccurate. If
10264 ACCURATE_P is non-zero mark display of W as accurate. If
10265 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10266 redisplay_internal is called. */
10267
10268 static void
10269 mark_window_display_accurate_1 (w, accurate_p)
10270 struct window *w;
10271 int accurate_p;
10272 {
10273 if (BUFFERP (w->buffer))
10274 {
10275 struct buffer *b = XBUFFER (w->buffer);
10276
10277 w->last_modified
10278 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10279 w->last_overlay_modified
10280 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10281 w->last_had_star
10282 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10283
10284 if (accurate_p)
10285 {
10286 b->clip_changed = 0;
10287 b->prevent_redisplay_optimizations_p = 0;
10288
10289 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10290 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10291 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10292 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10293
10294 w->current_matrix->buffer = b;
10295 w->current_matrix->begv = BUF_BEGV (b);
10296 w->current_matrix->zv = BUF_ZV (b);
10297
10298 w->last_cursor = w->cursor;
10299 w->last_cursor_off_p = w->cursor_off_p;
10300
10301 if (w == XWINDOW (selected_window))
10302 w->last_point = make_number (BUF_PT (b));
10303 else
10304 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10305 }
10306 }
10307
10308 if (accurate_p)
10309 {
10310 w->window_end_valid = w->buffer;
10311 #if 0 /* This is incorrect with variable-height lines. */
10312 xassert (XINT (w->window_end_vpos)
10313 < (WINDOW_TOTAL_LINES (w)
10314 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10315 #endif
10316 w->update_mode_line = Qnil;
10317 }
10318 }
10319
10320
10321 /* Mark the display of windows in the window tree rooted at WINDOW as
10322 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10323 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10324 be redisplayed the next time redisplay_internal is called. */
10325
10326 void
10327 mark_window_display_accurate (window, accurate_p)
10328 Lisp_Object window;
10329 int accurate_p;
10330 {
10331 struct window *w;
10332
10333 for (; !NILP (window); window = w->next)
10334 {
10335 w = XWINDOW (window);
10336 mark_window_display_accurate_1 (w, accurate_p);
10337
10338 if (!NILP (w->vchild))
10339 mark_window_display_accurate (w->vchild, accurate_p);
10340 if (!NILP (w->hchild))
10341 mark_window_display_accurate (w->hchild, accurate_p);
10342 }
10343
10344 if (accurate_p)
10345 {
10346 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
10347 last_arrow_string = Voverlay_arrow_string;
10348 }
10349 else
10350 {
10351 /* Force a thorough redisplay the next time by setting
10352 last_arrow_position and last_arrow_string to t, which is
10353 unequal to any useful value of Voverlay_arrow_... */
10354 last_arrow_position = Qt;
10355 last_arrow_string = Qt;
10356 }
10357 }
10358
10359
10360 /* Return value in display table DP (Lisp_Char_Table *) for character
10361 C. Since a display table doesn't have any parent, we don't have to
10362 follow parent. Do not call this function directly but use the
10363 macro DISP_CHAR_VECTOR. */
10364
10365 Lisp_Object
10366 disp_char_vector (dp, c)
10367 struct Lisp_Char_Table *dp;
10368 int c;
10369 {
10370 int code[4], i;
10371 Lisp_Object val;
10372
10373 if (SINGLE_BYTE_CHAR_P (c))
10374 return (dp->contents[c]);
10375
10376 SPLIT_CHAR (c, code[0], code[1], code[2]);
10377 if (code[1] < 32)
10378 code[1] = -1;
10379 else if (code[2] < 32)
10380 code[2] = -1;
10381
10382 /* Here, the possible range of code[0] (== charset ID) is
10383 128..max_charset. Since the top level char table contains data
10384 for multibyte characters after 256th element, we must increment
10385 code[0] by 128 to get a correct index. */
10386 code[0] += 128;
10387 code[3] = -1; /* anchor */
10388
10389 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10390 {
10391 val = dp->contents[code[i]];
10392 if (!SUB_CHAR_TABLE_P (val))
10393 return (NILP (val) ? dp->defalt : val);
10394 }
10395
10396 /* Here, val is a sub char table. We return the default value of
10397 it. */
10398 return (dp->defalt);
10399 }
10400
10401
10402 \f
10403 /***********************************************************************
10404 Window Redisplay
10405 ***********************************************************************/
10406
10407 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10408
10409 static void
10410 redisplay_windows (window)
10411 Lisp_Object window;
10412 {
10413 while (!NILP (window))
10414 {
10415 struct window *w = XWINDOW (window);
10416
10417 if (!NILP (w->hchild))
10418 redisplay_windows (w->hchild);
10419 else if (!NILP (w->vchild))
10420 redisplay_windows (w->vchild);
10421 else
10422 {
10423 displayed_buffer = XBUFFER (w->buffer);
10424 /* Use list_of_error, not Qerror, so that
10425 we catch only errors and don't run the debugger. */
10426 internal_condition_case_1 (redisplay_window_0, window,
10427 list_of_error,
10428 redisplay_window_error);
10429 }
10430
10431 window = w->next;
10432 }
10433 }
10434
10435 static Lisp_Object
10436 redisplay_window_error ()
10437 {
10438 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10439 return Qnil;
10440 }
10441
10442 static Lisp_Object
10443 redisplay_window_0 (window)
10444 Lisp_Object window;
10445 {
10446 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10447 redisplay_window (window, 0);
10448 return Qnil;
10449 }
10450
10451 static Lisp_Object
10452 redisplay_window_1 (window)
10453 Lisp_Object window;
10454 {
10455 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10456 redisplay_window (window, 1);
10457 return Qnil;
10458 }
10459 \f
10460
10461 /* Increment GLYPH until it reaches END or CONDITION fails while
10462 adding (GLYPH)->pixel_width to X. */
10463
10464 #define SKIP_GLYPHS(glyph, end, x, condition) \
10465 do \
10466 { \
10467 (x) += (glyph)->pixel_width; \
10468 ++(glyph); \
10469 } \
10470 while ((glyph) < (end) && (condition))
10471
10472
10473 /* Set cursor position of W. PT is assumed to be displayed in ROW.
10474 DELTA is the number of bytes by which positions recorded in ROW
10475 differ from current buffer positions. */
10476
10477 void
10478 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10479 struct window *w;
10480 struct glyph_row *row;
10481 struct glyph_matrix *matrix;
10482 int delta, delta_bytes, dy, dvpos;
10483 {
10484 struct glyph *glyph = row->glyphs[TEXT_AREA];
10485 struct glyph *end = glyph + row->used[TEXT_AREA];
10486 /* The first glyph that starts a sequence of glyphs from string. */
10487 struct glyph *string_start;
10488 /* The X coordinate of string_start. */
10489 int string_start_x;
10490 /* The last known character position. */
10491 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10492 /* The last known character position before string_start. */
10493 int string_before_pos;
10494 int x = row->x;
10495 int pt_old = PT - delta;
10496
10497 /* Skip over glyphs not having an object at the start of the row.
10498 These are special glyphs like truncation marks on terminal
10499 frames. */
10500 if (row->displays_text_p)
10501 while (glyph < end
10502 && INTEGERP (glyph->object)
10503 && glyph->charpos < 0)
10504 {
10505 x += glyph->pixel_width;
10506 ++glyph;
10507 }
10508
10509 string_start = NULL;
10510 while (glyph < end
10511 && !INTEGERP (glyph->object)
10512 && (!BUFFERP (glyph->object)
10513 || (last_pos = glyph->charpos) < pt_old))
10514 {
10515 if (! STRINGP (glyph->object))
10516 {
10517 string_start = NULL;
10518 x += glyph->pixel_width;
10519 ++glyph;
10520 }
10521 else
10522 {
10523 string_before_pos = last_pos;
10524 string_start = glyph;
10525 string_start_x = x;
10526 /* Skip all glyphs from string. */
10527 SKIP_GLYPHS (glyph, end, x, STRINGP (glyph->object));
10528 }
10529 }
10530
10531 if (string_start
10532 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
10533 {
10534 /* We may have skipped over point because the previous glyphs
10535 are from string. As there's no easy way to know the
10536 character position of the current glyph, find the correct
10537 glyph on point by scanning from string_start again. */
10538 Lisp_Object limit;
10539 Lisp_Object string;
10540 int pos;
10541
10542 limit = make_number (pt_old + 1);
10543 end = glyph;
10544 glyph = string_start;
10545 x = string_start_x;
10546 string = glyph->object;
10547 pos = string_buffer_position (w, string, string_before_pos);
10548 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
10549 because we always put cursor after overlay strings. */
10550 while (pos == 0 && glyph < end)
10551 {
10552 string = glyph->object;
10553 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10554 if (glyph < end)
10555 pos = string_buffer_position (w, glyph->object, string_before_pos);
10556 }
10557
10558 while (glyph < end)
10559 {
10560 pos = XINT (Fnext_single_char_property_change
10561 (make_number (pos), Qdisplay, Qnil, limit));
10562 if (pos > pt_old)
10563 break;
10564 /* Skip glyphs from the same string. */
10565 string = glyph->object;
10566 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10567 /* Skip glyphs from an overlay. */
10568 while (glyph < end
10569 && ! string_buffer_position (w, glyph->object, pos))
10570 {
10571 string = glyph->object;
10572 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10573 }
10574 }
10575 }
10576
10577 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
10578 w->cursor.x = x;
10579 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
10580 w->cursor.y = row->y + dy;
10581
10582 if (w == XWINDOW (selected_window))
10583 {
10584 if (!row->continued_p
10585 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
10586 && row->x == 0)
10587 {
10588 this_line_buffer = XBUFFER (w->buffer);
10589
10590 CHARPOS (this_line_start_pos)
10591 = MATRIX_ROW_START_CHARPOS (row) + delta;
10592 BYTEPOS (this_line_start_pos)
10593 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
10594
10595 CHARPOS (this_line_end_pos)
10596 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
10597 BYTEPOS (this_line_end_pos)
10598 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
10599
10600 this_line_y = w->cursor.y;
10601 this_line_pixel_height = row->height;
10602 this_line_vpos = w->cursor.vpos;
10603 this_line_start_x = row->x;
10604 }
10605 else
10606 CHARPOS (this_line_start_pos) = 0;
10607 }
10608 }
10609
10610
10611 /* Run window scroll functions, if any, for WINDOW with new window
10612 start STARTP. Sets the window start of WINDOW to that position.
10613
10614 We assume that the window's buffer is really current. */
10615
10616 static INLINE struct text_pos
10617 run_window_scroll_functions (window, startp)
10618 Lisp_Object window;
10619 struct text_pos startp;
10620 {
10621 struct window *w = XWINDOW (window);
10622 SET_MARKER_FROM_TEXT_POS (w->start, startp);
10623
10624 if (current_buffer != XBUFFER (w->buffer))
10625 abort ();
10626
10627 if (!NILP (Vwindow_scroll_functions))
10628 {
10629 run_hook_with_args_2 (Qwindow_scroll_functions, window,
10630 make_number (CHARPOS (startp)));
10631 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10632 /* In case the hook functions switch buffers. */
10633 if (current_buffer != XBUFFER (w->buffer))
10634 set_buffer_internal_1 (XBUFFER (w->buffer));
10635 }
10636
10637 return startp;
10638 }
10639
10640
10641 /* Make sure the line containing the cursor is fully visible.
10642 A value of 1 means there is nothing to be done.
10643 (Either the line is fully visible, or it cannot be made so,
10644 or we cannot tell.)
10645 A value of 0 means the caller should do scrolling
10646 as if point had gone off the screen. */
10647
10648 static int
10649 make_cursor_line_fully_visible (w)
10650 struct window *w;
10651 {
10652 struct glyph_matrix *matrix;
10653 struct glyph_row *row;
10654 int window_height;
10655
10656 /* It's not always possible to find the cursor, e.g, when a window
10657 is full of overlay strings. Don't do anything in that case. */
10658 if (w->cursor.vpos < 0)
10659 return 1;
10660
10661 matrix = w->desired_matrix;
10662 row = MATRIX_ROW (matrix, w->cursor.vpos);
10663
10664 /* If the cursor row is not partially visible, there's nothing to do. */
10665 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
10666 return 1;
10667
10668 /* If the row the cursor is in is taller than the window's height,
10669 it's not clear what to do, so do nothing. */
10670 window_height = window_box_height (w);
10671 if (row->height >= window_height)
10672 return 1;
10673
10674 return 0;
10675
10676 #if 0
10677 /* This code used to try to scroll the window just enough to make
10678 the line visible. It returned 0 to say that the caller should
10679 allocate larger glyph matrices. */
10680
10681 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
10682 {
10683 int dy = row->height - row->visible_height;
10684 w->vscroll = 0;
10685 w->cursor.y += dy;
10686 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10687 }
10688 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
10689 {
10690 int dy = - (row->height - row->visible_height);
10691 w->vscroll = dy;
10692 w->cursor.y += dy;
10693 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10694 }
10695
10696 /* When we change the cursor y-position of the selected window,
10697 change this_line_y as well so that the display optimization for
10698 the cursor line of the selected window in redisplay_internal uses
10699 the correct y-position. */
10700 if (w == XWINDOW (selected_window))
10701 this_line_y = w->cursor.y;
10702
10703 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
10704 redisplay with larger matrices. */
10705 if (matrix->nrows < required_matrix_height (w))
10706 {
10707 fonts_changed_p = 1;
10708 return 0;
10709 }
10710
10711 return 1;
10712 #endif /* 0 */
10713 }
10714
10715
10716 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
10717 non-zero means only WINDOW is redisplayed in redisplay_internal.
10718 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
10719 in redisplay_window to bring a partially visible line into view in
10720 the case that only the cursor has moved.
10721
10722 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
10723 last screen line's vertical height extends past the end of the screen.
10724
10725 Value is
10726
10727 1 if scrolling succeeded
10728
10729 0 if scrolling didn't find point.
10730
10731 -1 if new fonts have been loaded so that we must interrupt
10732 redisplay, adjust glyph matrices, and try again. */
10733
10734 enum
10735 {
10736 SCROLLING_SUCCESS,
10737 SCROLLING_FAILED,
10738 SCROLLING_NEED_LARGER_MATRICES
10739 };
10740
10741 static int
10742 try_scrolling (window, just_this_one_p, scroll_conservatively,
10743 scroll_step, temp_scroll_step, last_line_misfit)
10744 Lisp_Object window;
10745 int just_this_one_p;
10746 EMACS_INT scroll_conservatively, scroll_step;
10747 int temp_scroll_step;
10748 int last_line_misfit;
10749 {
10750 struct window *w = XWINDOW (window);
10751 struct frame *f = XFRAME (w->frame);
10752 struct text_pos scroll_margin_pos;
10753 struct text_pos pos;
10754 struct text_pos startp;
10755 struct it it;
10756 Lisp_Object window_end;
10757 int this_scroll_margin;
10758 int dy = 0;
10759 int scroll_max;
10760 int rc;
10761 int amount_to_scroll = 0;
10762 Lisp_Object aggressive;
10763 int height;
10764 int end_scroll_margin;
10765
10766 #if GLYPH_DEBUG
10767 debug_method_add (w, "try_scrolling");
10768 #endif
10769
10770 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10771
10772 /* Compute scroll margin height in pixels. We scroll when point is
10773 within this distance from the top or bottom of the window. */
10774 if (scroll_margin > 0)
10775 {
10776 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
10777 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
10778 }
10779 else
10780 this_scroll_margin = 0;
10781
10782 /* Compute how much we should try to scroll maximally to bring point
10783 into view. */
10784 if (scroll_step || scroll_conservatively || temp_scroll_step)
10785 scroll_max = max (scroll_step,
10786 max (scroll_conservatively, temp_scroll_step));
10787 else if (NUMBERP (current_buffer->scroll_down_aggressively)
10788 || NUMBERP (current_buffer->scroll_up_aggressively))
10789 /* We're trying to scroll because of aggressive scrolling
10790 but no scroll_step is set. Choose an arbitrary one. Maybe
10791 there should be a variable for this. */
10792 scroll_max = 10;
10793 else
10794 scroll_max = 0;
10795 scroll_max *= FRAME_LINE_HEIGHT (f);
10796
10797 /* Decide whether we have to scroll down. Start at the window end
10798 and move this_scroll_margin up to find the position of the scroll
10799 margin. */
10800 window_end = Fwindow_end (window, Qt);
10801
10802 too_near_end:
10803
10804 CHARPOS (scroll_margin_pos) = XINT (window_end);
10805 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
10806
10807 end_scroll_margin = this_scroll_margin + !!last_line_misfit;
10808 if (end_scroll_margin)
10809 {
10810 start_display (&it, w, scroll_margin_pos);
10811 move_it_vertically (&it, - end_scroll_margin);
10812 scroll_margin_pos = it.current.pos;
10813 }
10814
10815 if (PT >= CHARPOS (scroll_margin_pos))
10816 {
10817 int y0;
10818
10819 /* Point is in the scroll margin at the bottom of the window, or
10820 below. Compute a new window start that makes point visible. */
10821
10822 /* Compute the distance from the scroll margin to PT.
10823 Give up if the distance is greater than scroll_max. */
10824 start_display (&it, w, scroll_margin_pos);
10825 y0 = it.current_y;
10826 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10827 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10828
10829 /* To make point visible, we have to move the window start
10830 down so that the line the cursor is in is visible, which
10831 means we have to add in the height of the cursor line. */
10832 dy = line_bottom_y (&it) - y0;
10833
10834 if (dy > scroll_max)
10835 return SCROLLING_FAILED;
10836
10837 /* Move the window start down. If scrolling conservatively,
10838 move it just enough down to make point visible. If
10839 scroll_step is set, move it down by scroll_step. */
10840 start_display (&it, w, startp);
10841
10842 if (scroll_conservatively)
10843 /* Set AMOUNT_TO_SCROLL to at least one line,
10844 and at most scroll_conservatively lines. */
10845 amount_to_scroll
10846 = min (max (dy, FRAME_LINE_HEIGHT (f)),
10847 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
10848 else if (scroll_step || temp_scroll_step)
10849 amount_to_scroll = scroll_max;
10850 else
10851 {
10852 aggressive = current_buffer->scroll_up_aggressively;
10853 height = WINDOW_BOX_TEXT_HEIGHT (w);
10854 if (NUMBERP (aggressive))
10855 amount_to_scroll = XFLOATINT (aggressive) * height;
10856 }
10857
10858 if (amount_to_scroll <= 0)
10859 return SCROLLING_FAILED;
10860
10861 /* If moving by amount_to_scroll leaves STARTP unchanged,
10862 move it down one screen line. */
10863
10864 move_it_vertically (&it, amount_to_scroll);
10865 if (CHARPOS (it.current.pos) == CHARPOS (startp))
10866 move_it_by_lines (&it, 1, 1);
10867 startp = it.current.pos;
10868 }
10869 else
10870 {
10871 /* See if point is inside the scroll margin at the top of the
10872 window. */
10873 scroll_margin_pos = startp;
10874 if (this_scroll_margin)
10875 {
10876 start_display (&it, w, startp);
10877 move_it_vertically (&it, this_scroll_margin);
10878 scroll_margin_pos = it.current.pos;
10879 }
10880
10881 if (PT < CHARPOS (scroll_margin_pos))
10882 {
10883 /* Point is in the scroll margin at the top of the window or
10884 above what is displayed in the window. */
10885 int y0;
10886
10887 /* Compute the vertical distance from PT to the scroll
10888 margin position. Give up if distance is greater than
10889 scroll_max. */
10890 SET_TEXT_POS (pos, PT, PT_BYTE);
10891 start_display (&it, w, pos);
10892 y0 = it.current_y;
10893 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
10894 it.last_visible_y, -1,
10895 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10896 dy = it.current_y - y0;
10897 if (dy > scroll_max)
10898 return SCROLLING_FAILED;
10899
10900 /* Compute new window start. */
10901 start_display (&it, w, startp);
10902
10903 if (scroll_conservatively)
10904 amount_to_scroll =
10905 max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
10906 else if (scroll_step || temp_scroll_step)
10907 amount_to_scroll = scroll_max;
10908 else
10909 {
10910 aggressive = current_buffer->scroll_down_aggressively;
10911 height = WINDOW_BOX_TEXT_HEIGHT (w);
10912 if (NUMBERP (aggressive))
10913 amount_to_scroll = XFLOATINT (aggressive) * height;
10914 }
10915
10916 if (amount_to_scroll <= 0)
10917 return SCROLLING_FAILED;
10918
10919 move_it_vertically (&it, - amount_to_scroll);
10920 startp = it.current.pos;
10921 }
10922 }
10923
10924 /* Run window scroll functions. */
10925 startp = run_window_scroll_functions (window, startp);
10926
10927 /* Display the window. Give up if new fonts are loaded, or if point
10928 doesn't appear. */
10929 if (!try_window (window, startp))
10930 rc = SCROLLING_NEED_LARGER_MATRICES;
10931 else if (w->cursor.vpos < 0)
10932 {
10933 clear_glyph_matrix (w->desired_matrix);
10934 rc = SCROLLING_FAILED;
10935 }
10936 else
10937 {
10938 /* Maybe forget recorded base line for line number display. */
10939 if (!just_this_one_p
10940 || current_buffer->clip_changed
10941 || BEG_UNCHANGED < CHARPOS (startp))
10942 w->base_line_number = Qnil;
10943
10944 /* If cursor ends up on a partially visible line,
10945 treat that as being off the bottom of the screen. */
10946 if (! make_cursor_line_fully_visible (w))
10947 {
10948 clear_glyph_matrix (w->desired_matrix);
10949 last_line_misfit = 1;
10950 goto too_near_end;
10951 }
10952 rc = SCROLLING_SUCCESS;
10953 }
10954
10955 return rc;
10956 }
10957
10958
10959 /* Compute a suitable window start for window W if display of W starts
10960 on a continuation line. Value is non-zero if a new window start
10961 was computed.
10962
10963 The new window start will be computed, based on W's width, starting
10964 from the start of the continued line. It is the start of the
10965 screen line with the minimum distance from the old start W->start. */
10966
10967 static int
10968 compute_window_start_on_continuation_line (w)
10969 struct window *w;
10970 {
10971 struct text_pos pos, start_pos;
10972 int window_start_changed_p = 0;
10973
10974 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
10975
10976 /* If window start is on a continuation line... Window start may be
10977 < BEGV in case there's invisible text at the start of the
10978 buffer (M-x rmail, for example). */
10979 if (CHARPOS (start_pos) > BEGV
10980 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
10981 {
10982 struct it it;
10983 struct glyph_row *row;
10984
10985 /* Handle the case that the window start is out of range. */
10986 if (CHARPOS (start_pos) < BEGV)
10987 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
10988 else if (CHARPOS (start_pos) > ZV)
10989 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
10990
10991 /* Find the start of the continued line. This should be fast
10992 because scan_buffer is fast (newline cache). */
10993 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
10994 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
10995 row, DEFAULT_FACE_ID);
10996 reseat_at_previous_visible_line_start (&it);
10997
10998 /* If the line start is "too far" away from the window start,
10999 say it takes too much time to compute a new window start. */
11000 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11001 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11002 {
11003 int min_distance, distance;
11004
11005 /* Move forward by display lines to find the new window
11006 start. If window width was enlarged, the new start can
11007 be expected to be > the old start. If window width was
11008 decreased, the new window start will be < the old start.
11009 So, we're looking for the display line start with the
11010 minimum distance from the old window start. */
11011 pos = it.current.pos;
11012 min_distance = INFINITY;
11013 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11014 distance < min_distance)
11015 {
11016 min_distance = distance;
11017 pos = it.current.pos;
11018 move_it_by_lines (&it, 1, 0);
11019 }
11020
11021 /* Set the window start there. */
11022 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11023 window_start_changed_p = 1;
11024 }
11025 }
11026
11027 return window_start_changed_p;
11028 }
11029
11030
11031 /* Try cursor movement in case text has not changed in window WINDOW,
11032 with window start STARTP. Value is
11033
11034 CURSOR_MOVEMENT_SUCCESS if successful
11035
11036 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11037
11038 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11039 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11040 we want to scroll as if scroll-step were set to 1. See the code.
11041
11042 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11043 which case we have to abort this redisplay, and adjust matrices
11044 first. */
11045
11046 enum
11047 {
11048 CURSOR_MOVEMENT_SUCCESS,
11049 CURSOR_MOVEMENT_CANNOT_BE_USED,
11050 CURSOR_MOVEMENT_MUST_SCROLL,
11051 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11052 };
11053
11054 static int
11055 try_cursor_movement (window, startp, scroll_step)
11056 Lisp_Object window;
11057 struct text_pos startp;
11058 int *scroll_step;
11059 {
11060 struct window *w = XWINDOW (window);
11061 struct frame *f = XFRAME (w->frame);
11062 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11063
11064 #if GLYPH_DEBUG
11065 if (inhibit_try_cursor_movement)
11066 return rc;
11067 #endif
11068
11069 /* Handle case where text has not changed, only point, and it has
11070 not moved off the frame. */
11071 if (/* Point may be in this window. */
11072 PT >= CHARPOS (startp)
11073 /* Selective display hasn't changed. */
11074 && !current_buffer->clip_changed
11075 /* Function force-mode-line-update is used to force a thorough
11076 redisplay. It sets either windows_or_buffers_changed or
11077 update_mode_lines. So don't take a shortcut here for these
11078 cases. */
11079 && !update_mode_lines
11080 && !windows_or_buffers_changed
11081 && !cursor_type_changed
11082 /* Can't use this case if highlighting a region. When a
11083 region exists, cursor movement has to do more than just
11084 set the cursor. */
11085 && !(!NILP (Vtransient_mark_mode)
11086 && !NILP (current_buffer->mark_active))
11087 && NILP (w->region_showing)
11088 && NILP (Vshow_trailing_whitespace)
11089 /* Right after splitting windows, last_point may be nil. */
11090 && INTEGERP (w->last_point)
11091 /* This code is not used for mini-buffer for the sake of the case
11092 of redisplaying to replace an echo area message; since in
11093 that case the mini-buffer contents per se are usually
11094 unchanged. This code is of no real use in the mini-buffer
11095 since the handling of this_line_start_pos, etc., in redisplay
11096 handles the same cases. */
11097 && !EQ (window, minibuf_window)
11098 /* When splitting windows or for new windows, it happens that
11099 redisplay is called with a nil window_end_vpos or one being
11100 larger than the window. This should really be fixed in
11101 window.c. I don't have this on my list, now, so we do
11102 approximately the same as the old redisplay code. --gerd. */
11103 && INTEGERP (w->window_end_vpos)
11104 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11105 && (FRAME_WINDOW_P (f)
11106 || !MARKERP (Voverlay_arrow_position)
11107 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
11108 {
11109 int this_scroll_margin;
11110 struct glyph_row *row = NULL;
11111
11112 #if GLYPH_DEBUG
11113 debug_method_add (w, "cursor movement");
11114 #endif
11115
11116 /* Scroll if point within this distance from the top or bottom
11117 of the window. This is a pixel value. */
11118 this_scroll_margin = max (0, scroll_margin);
11119 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11120 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11121
11122 /* Start with the row the cursor was displayed during the last
11123 not paused redisplay. Give up if that row is not valid. */
11124 if (w->last_cursor.vpos < 0
11125 || w->last_cursor.vpos >= w->current_matrix->nrows)
11126 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11127 else
11128 {
11129 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11130 if (row->mode_line_p)
11131 ++row;
11132 if (!row->enabled_p)
11133 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11134 }
11135
11136 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11137 {
11138 int scroll_p = 0;
11139 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11140
11141 if (PT > XFASTINT (w->last_point))
11142 {
11143 /* Point has moved forward. */
11144 while (MATRIX_ROW_END_CHARPOS (row) < PT
11145 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11146 {
11147 xassert (row->enabled_p);
11148 ++row;
11149 }
11150
11151 /* The end position of a row equals the start position
11152 of the next row. If PT is there, we would rather
11153 display it in the next line. */
11154 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11155 && MATRIX_ROW_END_CHARPOS (row) == PT
11156 && !cursor_row_p (w, row))
11157 ++row;
11158
11159 /* If within the scroll margin, scroll. Note that
11160 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11161 the next line would be drawn, and that
11162 this_scroll_margin can be zero. */
11163 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11164 || PT > MATRIX_ROW_END_CHARPOS (row)
11165 /* Line is completely visible last line in window
11166 and PT is to be set in the next line. */
11167 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11168 && PT == MATRIX_ROW_END_CHARPOS (row)
11169 && !row->ends_at_zv_p
11170 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11171 scroll_p = 1;
11172 }
11173 else if (PT < XFASTINT (w->last_point))
11174 {
11175 /* Cursor has to be moved backward. Note that PT >=
11176 CHARPOS (startp) because of the outer
11177 if-statement. */
11178 while (!row->mode_line_p
11179 && (MATRIX_ROW_START_CHARPOS (row) > PT
11180 || (MATRIX_ROW_START_CHARPOS (row) == PT
11181 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11182 && (row->y > this_scroll_margin
11183 || CHARPOS (startp) == BEGV))
11184 {
11185 xassert (row->enabled_p);
11186 --row;
11187 }
11188
11189 /* Consider the following case: Window starts at BEGV,
11190 there is invisible, intangible text at BEGV, so that
11191 display starts at some point START > BEGV. It can
11192 happen that we are called with PT somewhere between
11193 BEGV and START. Try to handle that case. */
11194 if (row < w->current_matrix->rows
11195 || row->mode_line_p)
11196 {
11197 row = w->current_matrix->rows;
11198 if (row->mode_line_p)
11199 ++row;
11200 }
11201
11202 /* Due to newlines in overlay strings, we may have to
11203 skip forward over overlay strings. */
11204 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11205 && MATRIX_ROW_END_CHARPOS (row) == PT
11206 && !cursor_row_p (w, row))
11207 ++row;
11208
11209 /* If within the scroll margin, scroll. */
11210 if (row->y < this_scroll_margin
11211 && CHARPOS (startp) != BEGV)
11212 scroll_p = 1;
11213 }
11214
11215 if (PT < MATRIX_ROW_START_CHARPOS (row)
11216 || PT > MATRIX_ROW_END_CHARPOS (row))
11217 {
11218 /* if PT is not in the glyph row, give up. */
11219 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11220 }
11221 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
11222 {
11223 if (PT == MATRIX_ROW_END_CHARPOS (row)
11224 && !row->ends_at_zv_p
11225 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11226 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11227 else if (row->height > window_box_height (w))
11228 {
11229 /* If we end up in a partially visible line, let's
11230 make it fully visible, except when it's taller
11231 than the window, in which case we can't do much
11232 about it. */
11233 *scroll_step = 1;
11234 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11235 }
11236 else
11237 {
11238 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11239 if (!make_cursor_line_fully_visible (w))
11240 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11241 else
11242 rc = CURSOR_MOVEMENT_SUCCESS;
11243 }
11244 }
11245 else if (scroll_p)
11246 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11247 else
11248 {
11249 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11250 rc = CURSOR_MOVEMENT_SUCCESS;
11251 }
11252 }
11253 }
11254
11255 return rc;
11256 }
11257
11258 void
11259 set_vertical_scroll_bar (w)
11260 struct window *w;
11261 {
11262 int start, end, whole;
11263
11264 /* Calculate the start and end positions for the current window.
11265 At some point, it would be nice to choose between scrollbars
11266 which reflect the whole buffer size, with special markers
11267 indicating narrowing, and scrollbars which reflect only the
11268 visible region.
11269
11270 Note that mini-buffers sometimes aren't displaying any text. */
11271 if (!MINI_WINDOW_P (w)
11272 || (w == XWINDOW (minibuf_window)
11273 && NILP (echo_area_buffer[0])))
11274 {
11275 struct buffer *buf = XBUFFER (w->buffer);
11276 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11277 start = marker_position (w->start) - BUF_BEGV (buf);
11278 /* I don't think this is guaranteed to be right. For the
11279 moment, we'll pretend it is. */
11280 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11281
11282 if (end < start)
11283 end = start;
11284 if (whole < (end - start))
11285 whole = end - start;
11286 }
11287 else
11288 start = end = whole = 0;
11289
11290 /* Indicate what this scroll bar ought to be displaying now. */
11291 set_vertical_scroll_bar_hook (w, end - start, whole, start);
11292 }
11293
11294 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11295 selected_window is redisplayed.
11296
11297 We can return without actually redisplaying the window if
11298 fonts_changed_p is nonzero. In that case, redisplay_internal will
11299 retry. */
11300
11301 static void
11302 redisplay_window (window, just_this_one_p)
11303 Lisp_Object window;
11304 int just_this_one_p;
11305 {
11306 struct window *w = XWINDOW (window);
11307 struct frame *f = XFRAME (w->frame);
11308 struct buffer *buffer = XBUFFER (w->buffer);
11309 struct buffer *old = current_buffer;
11310 struct text_pos lpoint, opoint, startp;
11311 int update_mode_line;
11312 int tem;
11313 struct it it;
11314 /* Record it now because it's overwritten. */
11315 int current_matrix_up_to_date_p = 0;
11316 /* This is less strict than current_matrix_up_to_date_p.
11317 It indictes that the buffer contents and narrowing are unchanged. */
11318 int buffer_unchanged_p = 0;
11319 int temp_scroll_step = 0;
11320 int count = SPECPDL_INDEX ();
11321 int rc;
11322 int centering_position;
11323 int last_line_misfit = 0;
11324
11325 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11326 opoint = lpoint;
11327
11328 /* W must be a leaf window here. */
11329 xassert (!NILP (w->buffer));
11330 #if GLYPH_DEBUG
11331 *w->desired_matrix->method = 0;
11332 #endif
11333
11334 specbind (Qinhibit_point_motion_hooks, Qt);
11335
11336 reconsider_clip_changes (w, buffer);
11337
11338 /* Has the mode line to be updated? */
11339 update_mode_line = (!NILP (w->update_mode_line)
11340 || update_mode_lines
11341 || buffer->clip_changed
11342 || buffer->prevent_redisplay_optimizations_p);
11343
11344 if (MINI_WINDOW_P (w))
11345 {
11346 if (w == XWINDOW (echo_area_window)
11347 && !NILP (echo_area_buffer[0]))
11348 {
11349 if (update_mode_line)
11350 /* We may have to update a tty frame's menu bar or a
11351 tool-bar. Example `M-x C-h C-h C-g'. */
11352 goto finish_menu_bars;
11353 else
11354 /* We've already displayed the echo area glyphs in this window. */
11355 goto finish_scroll_bars;
11356 }
11357 else if ((w != XWINDOW (minibuf_window)
11358 || minibuf_level == 0)
11359 /* When buffer is nonempty, redisplay window normally. */
11360 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
11361 /* Quail displays non-mini buffers in minibuffer window.
11362 In that case, redisplay the window normally. */
11363 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
11364 {
11365 /* W is a mini-buffer window, but it's not active, so clear
11366 it. */
11367 int yb = window_text_bottom_y (w);
11368 struct glyph_row *row;
11369 int y;
11370
11371 for (y = 0, row = w->desired_matrix->rows;
11372 y < yb;
11373 y += row->height, ++row)
11374 blank_row (w, row, y);
11375 goto finish_scroll_bars;
11376 }
11377
11378 clear_glyph_matrix (w->desired_matrix);
11379 }
11380
11381 /* Otherwise set up data on this window; select its buffer and point
11382 value. */
11383 /* Really select the buffer, for the sake of buffer-local
11384 variables. */
11385 set_buffer_internal_1 (XBUFFER (w->buffer));
11386 SET_TEXT_POS (opoint, PT, PT_BYTE);
11387
11388 current_matrix_up_to_date_p
11389 = (!NILP (w->window_end_valid)
11390 && !current_buffer->clip_changed
11391 && !current_buffer->prevent_redisplay_optimizations_p
11392 && XFASTINT (w->last_modified) >= MODIFF
11393 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11394
11395 buffer_unchanged_p
11396 = (!NILP (w->window_end_valid)
11397 && !current_buffer->clip_changed
11398 && XFASTINT (w->last_modified) >= MODIFF
11399 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11400
11401 /* When windows_or_buffers_changed is non-zero, we can't rely on
11402 the window end being valid, so set it to nil there. */
11403 if (windows_or_buffers_changed)
11404 {
11405 /* If window starts on a continuation line, maybe adjust the
11406 window start in case the window's width changed. */
11407 if (XMARKER (w->start)->buffer == current_buffer)
11408 compute_window_start_on_continuation_line (w);
11409
11410 w->window_end_valid = Qnil;
11411 }
11412
11413 /* Some sanity checks. */
11414 CHECK_WINDOW_END (w);
11415 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
11416 abort ();
11417 if (BYTEPOS (opoint) < CHARPOS (opoint))
11418 abort ();
11419
11420 /* If %c is in mode line, update it if needed. */
11421 if (!NILP (w->column_number_displayed)
11422 /* This alternative quickly identifies a common case
11423 where no change is needed. */
11424 && !(PT == XFASTINT (w->last_point)
11425 && XFASTINT (w->last_modified) >= MODIFF
11426 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11427 && (XFASTINT (w->column_number_displayed)
11428 != (int) current_column ())) /* iftc */
11429 update_mode_line = 1;
11430
11431 /* Count number of windows showing the selected buffer. An indirect
11432 buffer counts as its base buffer. */
11433 if (!just_this_one_p)
11434 {
11435 struct buffer *current_base, *window_base;
11436 current_base = current_buffer;
11437 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11438 if (current_base->base_buffer)
11439 current_base = current_base->base_buffer;
11440 if (window_base->base_buffer)
11441 window_base = window_base->base_buffer;
11442 if (current_base == window_base)
11443 buffer_shared++;
11444 }
11445
11446 /* Point refers normally to the selected window. For any other
11447 window, set up appropriate value. */
11448 if (!EQ (window, selected_window))
11449 {
11450 int new_pt = XMARKER (w->pointm)->charpos;
11451 int new_pt_byte = marker_byte_position (w->pointm);
11452 if (new_pt < BEGV)
11453 {
11454 new_pt = BEGV;
11455 new_pt_byte = BEGV_BYTE;
11456 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
11457 }
11458 else if (new_pt > (ZV - 1))
11459 {
11460 new_pt = ZV;
11461 new_pt_byte = ZV_BYTE;
11462 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
11463 }
11464
11465 /* We don't use SET_PT so that the point-motion hooks don't run. */
11466 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
11467 }
11468
11469 /* If any of the character widths specified in the display table
11470 have changed, invalidate the width run cache. It's true that
11471 this may be a bit late to catch such changes, but the rest of
11472 redisplay goes (non-fatally) haywire when the display table is
11473 changed, so why should we worry about doing any better? */
11474 if (current_buffer->width_run_cache)
11475 {
11476 struct Lisp_Char_Table *disptab = buffer_display_table ();
11477
11478 if (! disptab_matches_widthtab (disptab,
11479 XVECTOR (current_buffer->width_table)))
11480 {
11481 invalidate_region_cache (current_buffer,
11482 current_buffer->width_run_cache,
11483 BEG, Z);
11484 recompute_width_table (current_buffer, disptab);
11485 }
11486 }
11487
11488 /* If window-start is screwed up, choose a new one. */
11489 if (XMARKER (w->start)->buffer != current_buffer)
11490 goto recenter;
11491
11492 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11493
11494 /* If someone specified a new starting point but did not insist,
11495 check whether it can be used. */
11496 if (!NILP (w->optional_new_start)
11497 && CHARPOS (startp) >= BEGV
11498 && CHARPOS (startp) <= ZV)
11499 {
11500 w->optional_new_start = Qnil;
11501 start_display (&it, w, startp);
11502 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11503 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11504 if (IT_CHARPOS (it) == PT)
11505 w->force_start = Qt;
11506 }
11507
11508 /* Handle case where place to start displaying has been specified,
11509 unless the specified location is outside the accessible range. */
11510 if (!NILP (w->force_start)
11511 || w->frozen_window_start_p)
11512 {
11513 /* We set this later on if we have to adjust point. */
11514 int new_vpos = -1;
11515
11516 w->force_start = Qnil;
11517 w->vscroll = 0;
11518 w->window_end_valid = Qnil;
11519
11520 /* Forget any recorded base line for line number display. */
11521 if (!buffer_unchanged_p)
11522 w->base_line_number = Qnil;
11523
11524 /* Redisplay the mode line. Select the buffer properly for that.
11525 Also, run the hook window-scroll-functions
11526 because we have scrolled. */
11527 /* Note, we do this after clearing force_start because
11528 if there's an error, it is better to forget about force_start
11529 than to get into an infinite loop calling the hook functions
11530 and having them get more errors. */
11531 if (!update_mode_line
11532 || ! NILP (Vwindow_scroll_functions))
11533 {
11534 update_mode_line = 1;
11535 w->update_mode_line = Qt;
11536 startp = run_window_scroll_functions (window, startp);
11537 }
11538
11539 w->last_modified = make_number (0);
11540 w->last_overlay_modified = make_number (0);
11541 if (CHARPOS (startp) < BEGV)
11542 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
11543 else if (CHARPOS (startp) > ZV)
11544 SET_TEXT_POS (startp, ZV, ZV_BYTE);
11545
11546 /* Redisplay, then check if cursor has been set during the
11547 redisplay. Give up if new fonts were loaded. */
11548 if (!try_window (window, startp))
11549 {
11550 w->force_start = Qt;
11551 clear_glyph_matrix (w->desired_matrix);
11552 goto need_larger_matrices;
11553 }
11554
11555 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
11556 {
11557 /* If point does not appear, try to move point so it does
11558 appear. The desired matrix has been built above, so we
11559 can use it here. */
11560 new_vpos = window_box_height (w) / 2;
11561 }
11562
11563 if (!make_cursor_line_fully_visible (w))
11564 {
11565 /* Point does appear, but on a line partly visible at end of window.
11566 Move it back to a fully-visible line. */
11567 new_vpos = window_box_height (w);
11568 }
11569
11570 /* If we need to move point for either of the above reasons,
11571 now actually do it. */
11572 if (new_vpos >= 0)
11573 {
11574 struct glyph_row *row;
11575
11576 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
11577 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
11578 ++row;
11579
11580 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
11581 MATRIX_ROW_START_BYTEPOS (row));
11582
11583 if (w != XWINDOW (selected_window))
11584 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
11585 else if (current_buffer == old)
11586 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11587
11588 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
11589
11590 /* If we are highlighting the region, then we just changed
11591 the region, so redisplay to show it. */
11592 if (!NILP (Vtransient_mark_mode)
11593 && !NILP (current_buffer->mark_active))
11594 {
11595 clear_glyph_matrix (w->desired_matrix);
11596 if (!try_window (window, startp))
11597 goto need_larger_matrices;
11598 }
11599 }
11600
11601 #if GLYPH_DEBUG
11602 debug_method_add (w, "forced window start");
11603 #endif
11604 goto done;
11605 }
11606
11607 /* Handle case where text has not changed, only point, and it has
11608 not moved off the frame, and we are not retrying after hscroll.
11609 (current_matrix_up_to_date_p is nonzero when retrying.) */
11610 if (current_matrix_up_to_date_p
11611 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
11612 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
11613 {
11614 switch (rc)
11615 {
11616 case CURSOR_MOVEMENT_SUCCESS:
11617 goto done;
11618
11619 #if 0 /* try_cursor_movement never returns this value. */
11620 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
11621 goto need_larger_matrices;
11622 #endif
11623
11624 case CURSOR_MOVEMENT_MUST_SCROLL:
11625 goto try_to_scroll;
11626
11627 default:
11628 abort ();
11629 }
11630 }
11631 /* If current starting point was originally the beginning of a line
11632 but no longer is, find a new starting point. */
11633 else if (!NILP (w->start_at_line_beg)
11634 && !(CHARPOS (startp) <= BEGV
11635 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
11636 {
11637 #if GLYPH_DEBUG
11638 debug_method_add (w, "recenter 1");
11639 #endif
11640 goto recenter;
11641 }
11642
11643 /* Try scrolling with try_window_id. Value is > 0 if update has
11644 been done, it is -1 if we know that the same window start will
11645 not work. It is 0 if unsuccessful for some other reason. */
11646 else if ((tem = try_window_id (w)) != 0)
11647 {
11648 #if GLYPH_DEBUG
11649 debug_method_add (w, "try_window_id %d", tem);
11650 #endif
11651
11652 if (fonts_changed_p)
11653 goto need_larger_matrices;
11654 if (tem > 0)
11655 goto done;
11656
11657 /* Otherwise try_window_id has returned -1 which means that we
11658 don't want the alternative below this comment to execute. */
11659 }
11660 else if (CHARPOS (startp) >= BEGV
11661 && CHARPOS (startp) <= ZV
11662 && PT >= CHARPOS (startp)
11663 && (CHARPOS (startp) < ZV
11664 /* Avoid starting at end of buffer. */
11665 || CHARPOS (startp) == BEGV
11666 || (XFASTINT (w->last_modified) >= MODIFF
11667 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
11668 {
11669 #if GLYPH_DEBUG
11670 debug_method_add (w, "same window start");
11671 #endif
11672
11673 /* Try to redisplay starting at same place as before.
11674 If point has not moved off frame, accept the results. */
11675 if (!current_matrix_up_to_date_p
11676 /* Don't use try_window_reusing_current_matrix in this case
11677 because a window scroll function can have changed the
11678 buffer. */
11679 || !NILP (Vwindow_scroll_functions)
11680 || MINI_WINDOW_P (w)
11681 || !try_window_reusing_current_matrix (w))
11682 {
11683 IF_DEBUG (debug_method_add (w, "1"));
11684 try_window (window, startp);
11685 }
11686
11687 if (fonts_changed_p)
11688 goto need_larger_matrices;
11689
11690 if (w->cursor.vpos >= 0)
11691 {
11692 if (!just_this_one_p
11693 || current_buffer->clip_changed
11694 || BEG_UNCHANGED < CHARPOS (startp))
11695 /* Forget any recorded base line for line number display. */
11696 w->base_line_number = Qnil;
11697
11698 if (!make_cursor_line_fully_visible (w))
11699 {
11700 clear_glyph_matrix (w->desired_matrix);
11701 last_line_misfit = 1;
11702 }
11703 /* Drop through and scroll. */
11704 else
11705 goto done;
11706 }
11707 else
11708 clear_glyph_matrix (w->desired_matrix);
11709 }
11710
11711 try_to_scroll:
11712
11713 w->last_modified = make_number (0);
11714 w->last_overlay_modified = make_number (0);
11715
11716 /* Redisplay the mode line. Select the buffer properly for that. */
11717 if (!update_mode_line)
11718 {
11719 update_mode_line = 1;
11720 w->update_mode_line = Qt;
11721 }
11722
11723 /* Try to scroll by specified few lines. */
11724 if ((scroll_conservatively
11725 || scroll_step
11726 || temp_scroll_step
11727 || NUMBERP (current_buffer->scroll_up_aggressively)
11728 || NUMBERP (current_buffer->scroll_down_aggressively))
11729 && !current_buffer->clip_changed
11730 && CHARPOS (startp) >= BEGV
11731 && CHARPOS (startp) <= ZV)
11732 {
11733 /* The function returns -1 if new fonts were loaded, 1 if
11734 successful, 0 if not successful. */
11735 int rc = try_scrolling (window, just_this_one_p,
11736 scroll_conservatively,
11737 scroll_step,
11738 temp_scroll_step, last_line_misfit);
11739 switch (rc)
11740 {
11741 case SCROLLING_SUCCESS:
11742 goto done;
11743
11744 case SCROLLING_NEED_LARGER_MATRICES:
11745 goto need_larger_matrices;
11746
11747 case SCROLLING_FAILED:
11748 break;
11749
11750 default:
11751 abort ();
11752 }
11753 }
11754
11755 /* Finally, just choose place to start which centers point */
11756
11757 recenter:
11758 centering_position = window_box_height (w) / 2;
11759
11760 point_at_top:
11761 /* Jump here with centering_position already set to 0. */
11762
11763 #if GLYPH_DEBUG
11764 debug_method_add (w, "recenter");
11765 #endif
11766
11767 /* w->vscroll = 0; */
11768
11769 /* Forget any previously recorded base line for line number display. */
11770 if (!buffer_unchanged_p)
11771 w->base_line_number = Qnil;
11772
11773 /* Move backward half the height of the window. */
11774 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11775 it.current_y = it.last_visible_y;
11776 move_it_vertically_backward (&it, centering_position);
11777 xassert (IT_CHARPOS (it) >= BEGV);
11778
11779 /* The function move_it_vertically_backward may move over more
11780 than the specified y-distance. If it->w is small, e.g. a
11781 mini-buffer window, we may end up in front of the window's
11782 display area. Start displaying at the start of the line
11783 containing PT in this case. */
11784 if (it.current_y <= 0)
11785 {
11786 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11787 move_it_vertically (&it, 0);
11788 xassert (IT_CHARPOS (it) <= PT);
11789 it.current_y = 0;
11790 }
11791
11792 it.current_x = it.hpos = 0;
11793
11794 /* Set startp here explicitly in case that helps avoid an infinite loop
11795 in case the window-scroll-functions functions get errors. */
11796 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
11797
11798 /* Run scroll hooks. */
11799 startp = run_window_scroll_functions (window, it.current.pos);
11800
11801 /* Redisplay the window. */
11802 if (!current_matrix_up_to_date_p
11803 || windows_or_buffers_changed
11804 || cursor_type_changed
11805 /* Don't use try_window_reusing_current_matrix in this case
11806 because it can have changed the buffer. */
11807 || !NILP (Vwindow_scroll_functions)
11808 || !just_this_one_p
11809 || MINI_WINDOW_P (w)
11810 || !try_window_reusing_current_matrix (w))
11811 try_window (window, startp);
11812
11813 /* If new fonts have been loaded (due to fontsets), give up. We
11814 have to start a new redisplay since we need to re-adjust glyph
11815 matrices. */
11816 if (fonts_changed_p)
11817 goto need_larger_matrices;
11818
11819 /* If cursor did not appear assume that the middle of the window is
11820 in the first line of the window. Do it again with the next line.
11821 (Imagine a window of height 100, displaying two lines of height
11822 60. Moving back 50 from it->last_visible_y will end in the first
11823 line.) */
11824 if (w->cursor.vpos < 0)
11825 {
11826 if (!NILP (w->window_end_valid)
11827 && PT >= Z - XFASTINT (w->window_end_pos))
11828 {
11829 clear_glyph_matrix (w->desired_matrix);
11830 move_it_by_lines (&it, 1, 0);
11831 try_window (window, it.current.pos);
11832 }
11833 else if (PT < IT_CHARPOS (it))
11834 {
11835 clear_glyph_matrix (w->desired_matrix);
11836 move_it_by_lines (&it, -1, 0);
11837 try_window (window, it.current.pos);
11838 }
11839 else
11840 {
11841 /* Not much we can do about it. */
11842 }
11843 }
11844
11845 /* Consider the following case: Window starts at BEGV, there is
11846 invisible, intangible text at BEGV, so that display starts at
11847 some point START > BEGV. It can happen that we are called with
11848 PT somewhere between BEGV and START. Try to handle that case. */
11849 if (w->cursor.vpos < 0)
11850 {
11851 struct glyph_row *row = w->current_matrix->rows;
11852 if (row->mode_line_p)
11853 ++row;
11854 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11855 }
11856
11857 if (!make_cursor_line_fully_visible (w))
11858 {
11859 /* If vscroll is enabled, disable it and try again. */
11860 if (w->vscroll)
11861 {
11862 w->vscroll = 0;
11863 clear_glyph_matrix (w->desired_matrix);
11864 goto recenter;
11865 }
11866
11867 /* If centering point failed to make the whole line visible,
11868 put point at the top instead. That has to make the whole line
11869 visible, if it can be done. */
11870 centering_position = 0;
11871 goto point_at_top;
11872 }
11873
11874 done:
11875
11876 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11877 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
11878 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
11879 ? Qt : Qnil);
11880
11881 /* Display the mode line, if we must. */
11882 if ((update_mode_line
11883 /* If window not full width, must redo its mode line
11884 if (a) the window to its side is being redone and
11885 (b) we do a frame-based redisplay. This is a consequence
11886 of how inverted lines are drawn in frame-based redisplay. */
11887 || (!just_this_one_p
11888 && !FRAME_WINDOW_P (f)
11889 && !WINDOW_FULL_WIDTH_P (w))
11890 /* Line number to display. */
11891 || INTEGERP (w->base_line_pos)
11892 /* Column number is displayed and different from the one displayed. */
11893 || (!NILP (w->column_number_displayed)
11894 && (XFASTINT (w->column_number_displayed)
11895 != (int) current_column ()))) /* iftc */
11896 /* This means that the window has a mode line. */
11897 && (WINDOW_WANTS_MODELINE_P (w)
11898 || WINDOW_WANTS_HEADER_LINE_P (w)))
11899 {
11900 display_mode_lines (w);
11901
11902 /* If mode line height has changed, arrange for a thorough
11903 immediate redisplay using the correct mode line height. */
11904 if (WINDOW_WANTS_MODELINE_P (w)
11905 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
11906 {
11907 fonts_changed_p = 1;
11908 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
11909 = DESIRED_MODE_LINE_HEIGHT (w);
11910 }
11911
11912 /* If top line height has changed, arrange for a thorough
11913 immediate redisplay using the correct mode line height. */
11914 if (WINDOW_WANTS_HEADER_LINE_P (w)
11915 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
11916 {
11917 fonts_changed_p = 1;
11918 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
11919 = DESIRED_HEADER_LINE_HEIGHT (w);
11920 }
11921
11922 if (fonts_changed_p)
11923 goto need_larger_matrices;
11924 }
11925
11926 if (!line_number_displayed
11927 && !BUFFERP (w->base_line_pos))
11928 {
11929 w->base_line_pos = Qnil;
11930 w->base_line_number = Qnil;
11931 }
11932
11933 finish_menu_bars:
11934
11935 /* When we reach a frame's selected window, redo the frame's menu bar. */
11936 if (update_mode_line
11937 && EQ (FRAME_SELECTED_WINDOW (f), window))
11938 {
11939 int redisplay_menu_p = 0;
11940 int redisplay_tool_bar_p = 0;
11941
11942 if (FRAME_WINDOW_P (f))
11943 {
11944 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
11945 || defined (USE_GTK)
11946 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
11947 #else
11948 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
11949 #endif
11950 }
11951 else
11952 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
11953
11954 if (redisplay_menu_p)
11955 display_menu_bar (w);
11956
11957 #ifdef HAVE_WINDOW_SYSTEM
11958 #ifdef USE_GTK
11959 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
11960 #else
11961 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
11962 && (FRAME_TOOL_BAR_LINES (f) > 0
11963 || auto_resize_tool_bars_p);
11964
11965 #endif
11966
11967 if (redisplay_tool_bar_p)
11968 redisplay_tool_bar (f);
11969 #endif
11970 }
11971
11972 /* We go to this label, with fonts_changed_p nonzero,
11973 if it is necessary to try again using larger glyph matrices.
11974 We have to redeem the scroll bar even in this case,
11975 because the loop in redisplay_internal expects that. */
11976 need_larger_matrices:
11977 ;
11978 finish_scroll_bars:
11979
11980 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
11981 {
11982 /* Set the thumb's position and size. */
11983 set_vertical_scroll_bar (w);
11984
11985 /* Note that we actually used the scroll bar attached to this
11986 window, so it shouldn't be deleted at the end of redisplay. */
11987 redeem_scroll_bar_hook (w);
11988 }
11989
11990 /* Restore current_buffer and value of point in it. */
11991 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
11992 set_buffer_internal_1 (old);
11993 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
11994
11995 unbind_to (count, Qnil);
11996 }
11997
11998
11999 /* Build the complete desired matrix of WINDOW with a window start
12000 buffer position POS. Value is non-zero if successful. It is zero
12001 if fonts were loaded during redisplay which makes re-adjusting
12002 glyph matrices necessary. */
12003
12004 int
12005 try_window (window, pos)
12006 Lisp_Object window;
12007 struct text_pos pos;
12008 {
12009 struct window *w = XWINDOW (window);
12010 struct it it;
12011 struct glyph_row *last_text_row = NULL;
12012
12013 /* Make POS the new window start. */
12014 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12015
12016 /* Mark cursor position as unknown. No overlay arrow seen. */
12017 w->cursor.vpos = -1;
12018 overlay_arrow_seen = 0;
12019
12020 /* Initialize iterator and info to start at POS. */
12021 start_display (&it, w, pos);
12022
12023 /* Display all lines of W. */
12024 while (it.current_y < it.last_visible_y)
12025 {
12026 if (display_line (&it))
12027 last_text_row = it.glyph_row - 1;
12028 if (fonts_changed_p)
12029 return 0;
12030 }
12031
12032 /* If bottom moved off end of frame, change mode line percentage. */
12033 if (XFASTINT (w->window_end_pos) <= 0
12034 && Z != IT_CHARPOS (it))
12035 w->update_mode_line = Qt;
12036
12037 /* Set window_end_pos to the offset of the last character displayed
12038 on the window from the end of current_buffer. Set
12039 window_end_vpos to its row number. */
12040 if (last_text_row)
12041 {
12042 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12043 w->window_end_bytepos
12044 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12045 w->window_end_pos
12046 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12047 w->window_end_vpos
12048 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12049 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12050 ->displays_text_p);
12051 }
12052 else
12053 {
12054 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12055 w->window_end_pos = make_number (Z - ZV);
12056 w->window_end_vpos = make_number (0);
12057 }
12058
12059 /* But that is not valid info until redisplay finishes. */
12060 w->window_end_valid = Qnil;
12061 return 1;
12062 }
12063
12064
12065 \f
12066 /************************************************************************
12067 Window redisplay reusing current matrix when buffer has not changed
12068 ************************************************************************/
12069
12070 /* Try redisplay of window W showing an unchanged buffer with a
12071 different window start than the last time it was displayed by
12072 reusing its current matrix. Value is non-zero if successful.
12073 W->start is the new window start. */
12074
12075 static int
12076 try_window_reusing_current_matrix (w)
12077 struct window *w;
12078 {
12079 struct frame *f = XFRAME (w->frame);
12080 struct glyph_row *row, *bottom_row;
12081 struct it it;
12082 struct run run;
12083 struct text_pos start, new_start;
12084 int nrows_scrolled, i;
12085 struct glyph_row *last_text_row;
12086 struct glyph_row *last_reused_text_row;
12087 struct glyph_row *start_row;
12088 int start_vpos, min_y, max_y;
12089
12090 #if GLYPH_DEBUG
12091 if (inhibit_try_window_reusing)
12092 return 0;
12093 #endif
12094
12095 if (/* This function doesn't handle terminal frames. */
12096 !FRAME_WINDOW_P (f)
12097 /* Don't try to reuse the display if windows have been split
12098 or such. */
12099 || windows_or_buffers_changed
12100 || cursor_type_changed)
12101 return 0;
12102
12103 /* Can't do this if region may have changed. */
12104 if ((!NILP (Vtransient_mark_mode)
12105 && !NILP (current_buffer->mark_active))
12106 || !NILP (w->region_showing)
12107 || !NILP (Vshow_trailing_whitespace))
12108 return 0;
12109
12110 /* If top-line visibility has changed, give up. */
12111 if (WINDOW_WANTS_HEADER_LINE_P (w)
12112 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12113 return 0;
12114
12115 /* Give up if old or new display is scrolled vertically. We could
12116 make this function handle this, but right now it doesn't. */
12117 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12118 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
12119 return 0;
12120
12121 /* The variable new_start now holds the new window start. The old
12122 start `start' can be determined from the current matrix. */
12123 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12124 start = start_row->start.pos;
12125 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12126
12127 /* Clear the desired matrix for the display below. */
12128 clear_glyph_matrix (w->desired_matrix);
12129
12130 if (CHARPOS (new_start) <= CHARPOS (start))
12131 {
12132 int first_row_y;
12133
12134 /* Don't use this method if the display starts with an ellipsis
12135 displayed for invisible text. It's not easy to handle that case
12136 below, and it's certainly not worth the effort since this is
12137 not a frequent case. */
12138 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12139 return 0;
12140
12141 IF_DEBUG (debug_method_add (w, "twu1"));
12142
12143 /* Display up to a row that can be reused. The variable
12144 last_text_row is set to the last row displayed that displays
12145 text. Note that it.vpos == 0 if or if not there is a
12146 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12147 start_display (&it, w, new_start);
12148 first_row_y = it.current_y;
12149 w->cursor.vpos = -1;
12150 last_text_row = last_reused_text_row = NULL;
12151
12152 while (it.current_y < it.last_visible_y
12153 && IT_CHARPOS (it) < CHARPOS (start)
12154 && !fonts_changed_p)
12155 if (display_line (&it))
12156 last_text_row = it.glyph_row - 1;
12157
12158 /* A value of current_y < last_visible_y means that we stopped
12159 at the previous window start, which in turn means that we
12160 have at least one reusable row. */
12161 if (it.current_y < it.last_visible_y)
12162 {
12163 /* IT.vpos always starts from 0; it counts text lines. */
12164 nrows_scrolled = it.vpos;
12165
12166 /* Find PT if not already found in the lines displayed. */
12167 if (w->cursor.vpos < 0)
12168 {
12169 int dy = it.current_y - first_row_y;
12170
12171 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12172 row = row_containing_pos (w, PT, row, NULL, dy);
12173 if (row)
12174 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12175 dy, nrows_scrolled);
12176 else
12177 {
12178 clear_glyph_matrix (w->desired_matrix);
12179 return 0;
12180 }
12181 }
12182
12183 /* Scroll the display. Do it before the current matrix is
12184 changed. The problem here is that update has not yet
12185 run, i.e. part of the current matrix is not up to date.
12186 scroll_run_hook will clear the cursor, and use the
12187 current matrix to get the height of the row the cursor is
12188 in. */
12189 run.current_y = first_row_y;
12190 run.desired_y = it.current_y;
12191 run.height = it.last_visible_y - it.current_y;
12192
12193 if (run.height > 0 && run.current_y != run.desired_y)
12194 {
12195 update_begin (f);
12196 rif->update_window_begin_hook (w);
12197 rif->clear_window_mouse_face (w);
12198 rif->scroll_run_hook (w, &run);
12199 rif->update_window_end_hook (w, 0, 0);
12200 update_end (f);
12201 }
12202
12203 /* Shift current matrix down by nrows_scrolled lines. */
12204 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12205 rotate_matrix (w->current_matrix,
12206 start_vpos,
12207 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12208 nrows_scrolled);
12209
12210 /* Disable lines that must be updated. */
12211 for (i = 0; i < it.vpos; ++i)
12212 (start_row + i)->enabled_p = 0;
12213
12214 /* Re-compute Y positions. */
12215 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12216 max_y = it.last_visible_y;
12217 for (row = start_row + nrows_scrolled;
12218 row < bottom_row;
12219 ++row)
12220 {
12221 row->y = it.current_y;
12222 row->visible_height = row->height;
12223
12224 if (row->y < min_y)
12225 row->visible_height -= min_y - row->y;
12226 if (row->y + row->height > max_y)
12227 row->visible_height -= row->y + row->height - max_y;
12228
12229 it.current_y += row->height;
12230
12231 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12232 last_reused_text_row = row;
12233 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12234 break;
12235 }
12236
12237 /* Disable lines in the current matrix which are now
12238 below the window. */
12239 for (++row; row < bottom_row; ++row)
12240 row->enabled_p = 0;
12241 }
12242
12243 /* Update window_end_pos etc.; last_reused_text_row is the last
12244 reused row from the current matrix containing text, if any.
12245 The value of last_text_row is the last displayed line
12246 containing text. */
12247 if (last_reused_text_row)
12248 {
12249 w->window_end_bytepos
12250 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12251 w->window_end_pos
12252 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12253 w->window_end_vpos
12254 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12255 w->current_matrix));
12256 }
12257 else if (last_text_row)
12258 {
12259 w->window_end_bytepos
12260 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12261 w->window_end_pos
12262 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12263 w->window_end_vpos
12264 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12265 }
12266 else
12267 {
12268 /* This window must be completely empty. */
12269 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12270 w->window_end_pos = make_number (Z - ZV);
12271 w->window_end_vpos = make_number (0);
12272 }
12273 w->window_end_valid = Qnil;
12274
12275 /* Update hint: don't try scrolling again in update_window. */
12276 w->desired_matrix->no_scrolling_p = 1;
12277
12278 #if GLYPH_DEBUG
12279 debug_method_add (w, "try_window_reusing_current_matrix 1");
12280 #endif
12281 return 1;
12282 }
12283 else if (CHARPOS (new_start) > CHARPOS (start))
12284 {
12285 struct glyph_row *pt_row, *row;
12286 struct glyph_row *first_reusable_row;
12287 struct glyph_row *first_row_to_display;
12288 int dy;
12289 int yb = window_text_bottom_y (w);
12290
12291 /* Find the row starting at new_start, if there is one. Don't
12292 reuse a partially visible line at the end. */
12293 first_reusable_row = start_row;
12294 while (first_reusable_row->enabled_p
12295 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12296 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12297 < CHARPOS (new_start)))
12298 ++first_reusable_row;
12299
12300 /* Give up if there is no row to reuse. */
12301 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
12302 || !first_reusable_row->enabled_p
12303 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12304 != CHARPOS (new_start)))
12305 return 0;
12306
12307 /* We can reuse fully visible rows beginning with
12308 first_reusable_row to the end of the window. Set
12309 first_row_to_display to the first row that cannot be reused.
12310 Set pt_row to the row containing point, if there is any. */
12311 pt_row = NULL;
12312 for (first_row_to_display = first_reusable_row;
12313 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12314 ++first_row_to_display)
12315 {
12316 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12317 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
12318 pt_row = first_row_to_display;
12319 }
12320
12321 /* Start displaying at the start of first_row_to_display. */
12322 xassert (first_row_to_display->y < yb);
12323 init_to_row_start (&it, w, first_row_to_display);
12324
12325 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12326 - start_vpos);
12327 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12328 - nrows_scrolled);
12329 it.current_y = (first_row_to_display->y - first_reusable_row->y
12330 + WINDOW_HEADER_LINE_HEIGHT (w));
12331
12332 /* Display lines beginning with first_row_to_display in the
12333 desired matrix. Set last_text_row to the last row displayed
12334 that displays text. */
12335 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12336 if (pt_row == NULL)
12337 w->cursor.vpos = -1;
12338 last_text_row = NULL;
12339 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12340 if (display_line (&it))
12341 last_text_row = it.glyph_row - 1;
12342
12343 /* Give up If point isn't in a row displayed or reused. */
12344 if (w->cursor.vpos < 0)
12345 {
12346 clear_glyph_matrix (w->desired_matrix);
12347 return 0;
12348 }
12349
12350 /* If point is in a reused row, adjust y and vpos of the cursor
12351 position. */
12352 if (pt_row)
12353 {
12354 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
12355 w->current_matrix);
12356 w->cursor.y -= first_reusable_row->y;
12357 }
12358
12359 /* Scroll the display. */
12360 run.current_y = first_reusable_row->y;
12361 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
12362 run.height = it.last_visible_y - run.current_y;
12363 dy = run.current_y - run.desired_y;
12364
12365 if (run.height)
12366 {
12367 struct frame *f = XFRAME (WINDOW_FRAME (w));
12368 update_begin (f);
12369 rif->update_window_begin_hook (w);
12370 rif->clear_window_mouse_face (w);
12371 rif->scroll_run_hook (w, &run);
12372 rif->update_window_end_hook (w, 0, 0);
12373 update_end (f);
12374 }
12375
12376 /* Adjust Y positions of reused rows. */
12377 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12378 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12379 max_y = it.last_visible_y;
12380 for (row = first_reusable_row; row < first_row_to_display; ++row)
12381 {
12382 row->y -= dy;
12383 row->visible_height = row->height;
12384 if (row->y < min_y)
12385 row->visible_height -= min_y - row->y;
12386 if (row->y + row->height > max_y)
12387 row->visible_height -= row->y + row->height - max_y;
12388 }
12389
12390 /* Scroll the current matrix. */
12391 xassert (nrows_scrolled > 0);
12392 rotate_matrix (w->current_matrix,
12393 start_vpos,
12394 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12395 -nrows_scrolled);
12396
12397 /* Disable rows not reused. */
12398 for (row -= nrows_scrolled; row < bottom_row; ++row)
12399 row->enabled_p = 0;
12400
12401 /* Adjust window end. A null value of last_text_row means that
12402 the window end is in reused rows which in turn means that
12403 only its vpos can have changed. */
12404 if (last_text_row)
12405 {
12406 w->window_end_bytepos
12407 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12408 w->window_end_pos
12409 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12410 w->window_end_vpos
12411 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12412 }
12413 else
12414 {
12415 w->window_end_vpos
12416 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
12417 }
12418
12419 w->window_end_valid = Qnil;
12420 w->desired_matrix->no_scrolling_p = 1;
12421
12422 #if GLYPH_DEBUG
12423 debug_method_add (w, "try_window_reusing_current_matrix 2");
12424 #endif
12425 return 1;
12426 }
12427
12428 return 0;
12429 }
12430
12431
12432 \f
12433 /************************************************************************
12434 Window redisplay reusing current matrix when buffer has changed
12435 ************************************************************************/
12436
12437 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
12438 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
12439 int *, int *));
12440 static struct glyph_row *
12441 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
12442 struct glyph_row *));
12443
12444
12445 /* Return the last row in MATRIX displaying text. If row START is
12446 non-null, start searching with that row. IT gives the dimensions
12447 of the display. Value is null if matrix is empty; otherwise it is
12448 a pointer to the row found. */
12449
12450 static struct glyph_row *
12451 find_last_row_displaying_text (matrix, it, start)
12452 struct glyph_matrix *matrix;
12453 struct it *it;
12454 struct glyph_row *start;
12455 {
12456 struct glyph_row *row, *row_found;
12457
12458 /* Set row_found to the last row in IT->w's current matrix
12459 displaying text. The loop looks funny but think of partially
12460 visible lines. */
12461 row_found = NULL;
12462 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
12463 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12464 {
12465 xassert (row->enabled_p);
12466 row_found = row;
12467 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
12468 break;
12469 ++row;
12470 }
12471
12472 return row_found;
12473 }
12474
12475
12476 /* Return the last row in the current matrix of W that is not affected
12477 by changes at the start of current_buffer that occurred since W's
12478 current matrix was built. Value is null if no such row exists.
12479
12480 BEG_UNCHANGED us the number of characters unchanged at the start of
12481 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
12482 first changed character in current_buffer. Characters at positions <
12483 BEG + BEG_UNCHANGED are at the same buffer positions as they were
12484 when the current matrix was built. */
12485
12486 static struct glyph_row *
12487 find_last_unchanged_at_beg_row (w)
12488 struct window *w;
12489 {
12490 int first_changed_pos = BEG + BEG_UNCHANGED;
12491 struct glyph_row *row;
12492 struct glyph_row *row_found = NULL;
12493 int yb = window_text_bottom_y (w);
12494
12495 /* Find the last row displaying unchanged text. */
12496 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12497 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12498 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
12499 {
12500 if (/* If row ends before first_changed_pos, it is unchanged,
12501 except in some case. */
12502 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
12503 /* When row ends in ZV and we write at ZV it is not
12504 unchanged. */
12505 && !row->ends_at_zv_p
12506 /* When first_changed_pos is the end of a continued line,
12507 row is not unchanged because it may be no longer
12508 continued. */
12509 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
12510 && row->continued_p))
12511 row_found = row;
12512
12513 /* Stop if last visible row. */
12514 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
12515 break;
12516
12517 ++row;
12518 }
12519
12520 return row_found;
12521 }
12522
12523
12524 /* Find the first glyph row in the current matrix of W that is not
12525 affected by changes at the end of current_buffer since the
12526 time W's current matrix was built.
12527
12528 Return in *DELTA the number of chars by which buffer positions in
12529 unchanged text at the end of current_buffer must be adjusted.
12530
12531 Return in *DELTA_BYTES the corresponding number of bytes.
12532
12533 Value is null if no such row exists, i.e. all rows are affected by
12534 changes. */
12535
12536 static struct glyph_row *
12537 find_first_unchanged_at_end_row (w, delta, delta_bytes)
12538 struct window *w;
12539 int *delta, *delta_bytes;
12540 {
12541 struct glyph_row *row;
12542 struct glyph_row *row_found = NULL;
12543
12544 *delta = *delta_bytes = 0;
12545
12546 /* Display must not have been paused, otherwise the current matrix
12547 is not up to date. */
12548 if (NILP (w->window_end_valid))
12549 abort ();
12550
12551 /* A value of window_end_pos >= END_UNCHANGED means that the window
12552 end is in the range of changed text. If so, there is no
12553 unchanged row at the end of W's current matrix. */
12554 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
12555 return NULL;
12556
12557 /* Set row to the last row in W's current matrix displaying text. */
12558 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12559
12560 /* If matrix is entirely empty, no unchanged row exists. */
12561 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12562 {
12563 /* The value of row is the last glyph row in the matrix having a
12564 meaningful buffer position in it. The end position of row
12565 corresponds to window_end_pos. This allows us to translate
12566 buffer positions in the current matrix to current buffer
12567 positions for characters not in changed text. */
12568 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12569 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12570 int last_unchanged_pos, last_unchanged_pos_old;
12571 struct glyph_row *first_text_row
12572 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12573
12574 *delta = Z - Z_old;
12575 *delta_bytes = Z_BYTE - Z_BYTE_old;
12576
12577 /* Set last_unchanged_pos to the buffer position of the last
12578 character in the buffer that has not been changed. Z is the
12579 index + 1 of the last character in current_buffer, i.e. by
12580 subtracting END_UNCHANGED we get the index of the last
12581 unchanged character, and we have to add BEG to get its buffer
12582 position. */
12583 last_unchanged_pos = Z - END_UNCHANGED + BEG;
12584 last_unchanged_pos_old = last_unchanged_pos - *delta;
12585
12586 /* Search backward from ROW for a row displaying a line that
12587 starts at a minimum position >= last_unchanged_pos_old. */
12588 for (; row > first_text_row; --row)
12589 {
12590 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
12591 abort ();
12592
12593 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
12594 row_found = row;
12595 }
12596 }
12597
12598 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
12599 abort ();
12600
12601 return row_found;
12602 }
12603
12604
12605 /* Make sure that glyph rows in the current matrix of window W
12606 reference the same glyph memory as corresponding rows in the
12607 frame's frame matrix. This function is called after scrolling W's
12608 current matrix on a terminal frame in try_window_id and
12609 try_window_reusing_current_matrix. */
12610
12611 static void
12612 sync_frame_with_window_matrix_rows (w)
12613 struct window *w;
12614 {
12615 struct frame *f = XFRAME (w->frame);
12616 struct glyph_row *window_row, *window_row_end, *frame_row;
12617
12618 /* Preconditions: W must be a leaf window and full-width. Its frame
12619 must have a frame matrix. */
12620 xassert (NILP (w->hchild) && NILP (w->vchild));
12621 xassert (WINDOW_FULL_WIDTH_P (w));
12622 xassert (!FRAME_WINDOW_P (f));
12623
12624 /* If W is a full-width window, glyph pointers in W's current matrix
12625 have, by definition, to be the same as glyph pointers in the
12626 corresponding frame matrix. Note that frame matrices have no
12627 marginal areas (see build_frame_matrix). */
12628 window_row = w->current_matrix->rows;
12629 window_row_end = window_row + w->current_matrix->nrows;
12630 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
12631 while (window_row < window_row_end)
12632 {
12633 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
12634 struct glyph *end = window_row->glyphs[LAST_AREA];
12635
12636 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
12637 frame_row->glyphs[TEXT_AREA] = start;
12638 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
12639 frame_row->glyphs[LAST_AREA] = end;
12640
12641 /* Disable frame rows whose corresponding window rows have
12642 been disabled in try_window_id. */
12643 if (!window_row->enabled_p)
12644 frame_row->enabled_p = 0;
12645
12646 ++window_row, ++frame_row;
12647 }
12648 }
12649
12650
12651 /* Find the glyph row in window W containing CHARPOS. Consider all
12652 rows between START and END (not inclusive). END null means search
12653 all rows to the end of the display area of W. Value is the row
12654 containing CHARPOS or null. */
12655
12656 struct glyph_row *
12657 row_containing_pos (w, charpos, start, end, dy)
12658 struct window *w;
12659 int charpos;
12660 struct glyph_row *start, *end;
12661 int dy;
12662 {
12663 struct glyph_row *row = start;
12664 int last_y;
12665
12666 /* If we happen to start on a header-line, skip that. */
12667 if (row->mode_line_p)
12668 ++row;
12669
12670 if ((end && row >= end) || !row->enabled_p)
12671 return NULL;
12672
12673 last_y = window_text_bottom_y (w) - dy;
12674
12675 while (1)
12676 {
12677 /* Give up if we have gone too far. */
12678 if (end && row >= end)
12679 return NULL;
12680 /* This formerly returned if they were equal.
12681 I think that both quantities are of a "last plus one" type;
12682 if so, when they are equal, the row is within the screen. -- rms. */
12683 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
12684 return NULL;
12685
12686 /* If it is in this row, return this row. */
12687 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
12688 || (MATRIX_ROW_END_CHARPOS (row) == charpos
12689 /* The end position of a row equals the start
12690 position of the next row. If CHARPOS is there, we
12691 would rather display it in the next line, except
12692 when this line ends in ZV. */
12693 && !row->ends_at_zv_p
12694 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12695 && charpos >= MATRIX_ROW_START_CHARPOS (row))
12696 return row;
12697 ++row;
12698 }
12699 }
12700
12701
12702 /* Try to redisplay window W by reusing its existing display. W's
12703 current matrix must be up to date when this function is called,
12704 i.e. window_end_valid must not be nil.
12705
12706 Value is
12707
12708 1 if display has been updated
12709 0 if otherwise unsuccessful
12710 -1 if redisplay with same window start is known not to succeed
12711
12712 The following steps are performed:
12713
12714 1. Find the last row in the current matrix of W that is not
12715 affected by changes at the start of current_buffer. If no such row
12716 is found, give up.
12717
12718 2. Find the first row in W's current matrix that is not affected by
12719 changes at the end of current_buffer. Maybe there is no such row.
12720
12721 3. Display lines beginning with the row + 1 found in step 1 to the
12722 row found in step 2 or, if step 2 didn't find a row, to the end of
12723 the window.
12724
12725 4. If cursor is not known to appear on the window, give up.
12726
12727 5. If display stopped at the row found in step 2, scroll the
12728 display and current matrix as needed.
12729
12730 6. Maybe display some lines at the end of W, if we must. This can
12731 happen under various circumstances, like a partially visible line
12732 becoming fully visible, or because newly displayed lines are displayed
12733 in smaller font sizes.
12734
12735 7. Update W's window end information. */
12736
12737 static int
12738 try_window_id (w)
12739 struct window *w;
12740 {
12741 struct frame *f = XFRAME (w->frame);
12742 struct glyph_matrix *current_matrix = w->current_matrix;
12743 struct glyph_matrix *desired_matrix = w->desired_matrix;
12744 struct glyph_row *last_unchanged_at_beg_row;
12745 struct glyph_row *first_unchanged_at_end_row;
12746 struct glyph_row *row;
12747 struct glyph_row *bottom_row;
12748 int bottom_vpos;
12749 struct it it;
12750 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
12751 struct text_pos start_pos;
12752 struct run run;
12753 int first_unchanged_at_end_vpos = 0;
12754 struct glyph_row *last_text_row, *last_text_row_at_end;
12755 struct text_pos start;
12756 int first_changed_charpos, last_changed_charpos;
12757
12758 #if GLYPH_DEBUG
12759 if (inhibit_try_window_id)
12760 return 0;
12761 #endif
12762
12763 /* This is handy for debugging. */
12764 #if 0
12765 #define GIVE_UP(X) \
12766 do { \
12767 fprintf (stderr, "try_window_id give up %d\n", (X)); \
12768 return 0; \
12769 } while (0)
12770 #else
12771 #define GIVE_UP(X) return 0
12772 #endif
12773
12774 SET_TEXT_POS_FROM_MARKER (start, w->start);
12775
12776 /* Don't use this for mini-windows because these can show
12777 messages and mini-buffers, and we don't handle that here. */
12778 if (MINI_WINDOW_P (w))
12779 GIVE_UP (1);
12780
12781 /* This flag is used to prevent redisplay optimizations. */
12782 if (windows_or_buffers_changed || cursor_type_changed)
12783 GIVE_UP (2);
12784
12785 /* Verify that narrowing has not changed.
12786 Also verify that we were not told to prevent redisplay optimizations.
12787 It would be nice to further
12788 reduce the number of cases where this prevents try_window_id. */
12789 if (current_buffer->clip_changed
12790 || current_buffer->prevent_redisplay_optimizations_p)
12791 GIVE_UP (3);
12792
12793 /* Window must either use window-based redisplay or be full width. */
12794 if (!FRAME_WINDOW_P (f)
12795 && (!line_ins_del_ok
12796 || !WINDOW_FULL_WIDTH_P (w)))
12797 GIVE_UP (4);
12798
12799 /* Give up if point is not known NOT to appear in W. */
12800 if (PT < CHARPOS (start))
12801 GIVE_UP (5);
12802
12803 /* Another way to prevent redisplay optimizations. */
12804 if (XFASTINT (w->last_modified) == 0)
12805 GIVE_UP (6);
12806
12807 /* Verify that window is not hscrolled. */
12808 if (XFASTINT (w->hscroll) != 0)
12809 GIVE_UP (7);
12810
12811 /* Verify that display wasn't paused. */
12812 if (NILP (w->window_end_valid))
12813 GIVE_UP (8);
12814
12815 /* Can't use this if highlighting a region because a cursor movement
12816 will do more than just set the cursor. */
12817 if (!NILP (Vtransient_mark_mode)
12818 && !NILP (current_buffer->mark_active))
12819 GIVE_UP (9);
12820
12821 /* Likewise if highlighting trailing whitespace. */
12822 if (!NILP (Vshow_trailing_whitespace))
12823 GIVE_UP (11);
12824
12825 /* Likewise if showing a region. */
12826 if (!NILP (w->region_showing))
12827 GIVE_UP (10);
12828
12829 /* Can use this if overlay arrow position and or string have changed. */
12830 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
12831 || !EQ (last_arrow_string, Voverlay_arrow_string))
12832 GIVE_UP (12);
12833
12834
12835 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
12836 only if buffer has really changed. The reason is that the gap is
12837 initially at Z for freshly visited files. The code below would
12838 set end_unchanged to 0 in that case. */
12839 if (MODIFF > SAVE_MODIFF
12840 /* This seems to happen sometimes after saving a buffer. */
12841 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
12842 {
12843 if (GPT - BEG < BEG_UNCHANGED)
12844 BEG_UNCHANGED = GPT - BEG;
12845 if (Z - GPT < END_UNCHANGED)
12846 END_UNCHANGED = Z - GPT;
12847 }
12848
12849 /* The position of the first and last character that has been changed. */
12850 first_changed_charpos = BEG + BEG_UNCHANGED;
12851 last_changed_charpos = Z - END_UNCHANGED;
12852
12853 /* If window starts after a line end, and the last change is in
12854 front of that newline, then changes don't affect the display.
12855 This case happens with stealth-fontification. Note that although
12856 the display is unchanged, glyph positions in the matrix have to
12857 be adjusted, of course. */
12858 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12859 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12860 && ((last_changed_charpos < CHARPOS (start)
12861 && CHARPOS (start) == BEGV)
12862 || (last_changed_charpos < CHARPOS (start) - 1
12863 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
12864 {
12865 int Z_old, delta, Z_BYTE_old, delta_bytes;
12866 struct glyph_row *r0;
12867
12868 /* Compute how many chars/bytes have been added to or removed
12869 from the buffer. */
12870 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12871 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12872 delta = Z - Z_old;
12873 delta_bytes = Z_BYTE - Z_BYTE_old;
12874
12875 /* Give up if PT is not in the window. Note that it already has
12876 been checked at the start of try_window_id that PT is not in
12877 front of the window start. */
12878 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
12879 GIVE_UP (13);
12880
12881 /* If window start is unchanged, we can reuse the whole matrix
12882 as is, after adjusting glyph positions. No need to compute
12883 the window end again, since its offset from Z hasn't changed. */
12884 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
12885 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
12886 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
12887 {
12888 /* Adjust positions in the glyph matrix. */
12889 if (delta || delta_bytes)
12890 {
12891 struct glyph_row *r1
12892 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
12893 increment_matrix_positions (w->current_matrix,
12894 MATRIX_ROW_VPOS (r0, current_matrix),
12895 MATRIX_ROW_VPOS (r1, current_matrix),
12896 delta, delta_bytes);
12897 }
12898
12899 /* Set the cursor. */
12900 row = row_containing_pos (w, PT, r0, NULL, 0);
12901 if (row)
12902 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
12903 else
12904 abort ();
12905 return 1;
12906 }
12907 }
12908
12909 /* Handle the case that changes are all below what is displayed in
12910 the window, and that PT is in the window. This shortcut cannot
12911 be taken if ZV is visible in the window, and text has been added
12912 there that is visible in the window. */
12913 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
12914 /* ZV is not visible in the window, or there are no
12915 changes at ZV, actually. */
12916 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
12917 || first_changed_charpos == last_changed_charpos))
12918 {
12919 struct glyph_row *r0;
12920
12921 /* Give up if PT is not in the window. Note that it already has
12922 been checked at the start of try_window_id that PT is not in
12923 front of the window start. */
12924 if (PT >= MATRIX_ROW_END_CHARPOS (row))
12925 GIVE_UP (14);
12926
12927 /* If window start is unchanged, we can reuse the whole matrix
12928 as is, without changing glyph positions since no text has
12929 been added/removed in front of the window end. */
12930 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
12931 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
12932 {
12933 /* We have to compute the window end anew since text
12934 can have been added/removed after it. */
12935 w->window_end_pos
12936 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12937 w->window_end_bytepos
12938 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12939
12940 /* Set the cursor. */
12941 row = row_containing_pos (w, PT, r0, NULL, 0);
12942 if (row)
12943 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
12944 else
12945 abort ();
12946 return 2;
12947 }
12948 }
12949
12950 /* Give up if window start is in the changed area.
12951
12952 The condition used to read
12953
12954 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
12955
12956 but why that was tested escapes me at the moment. */
12957 if (CHARPOS (start) >= first_changed_charpos
12958 && CHARPOS (start) <= last_changed_charpos)
12959 GIVE_UP (15);
12960
12961 /* Check that window start agrees with the start of the first glyph
12962 row in its current matrix. Check this after we know the window
12963 start is not in changed text, otherwise positions would not be
12964 comparable. */
12965 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
12966 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
12967 GIVE_UP (16);
12968
12969 /* Give up if the window ends in strings. Overlay strings
12970 at the end are difficult to handle, so don't try. */
12971 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
12972 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
12973 GIVE_UP (20);
12974
12975 /* Compute the position at which we have to start displaying new
12976 lines. Some of the lines at the top of the window might be
12977 reusable because they are not displaying changed text. Find the
12978 last row in W's current matrix not affected by changes at the
12979 start of current_buffer. Value is null if changes start in the
12980 first line of window. */
12981 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
12982 if (last_unchanged_at_beg_row)
12983 {
12984 /* Avoid starting to display in the moddle of a character, a TAB
12985 for instance. This is easier than to set up the iterator
12986 exactly, and it's not a frequent case, so the additional
12987 effort wouldn't really pay off. */
12988 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
12989 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
12990 && last_unchanged_at_beg_row > w->current_matrix->rows)
12991 --last_unchanged_at_beg_row;
12992
12993 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
12994 GIVE_UP (17);
12995
12996 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
12997 GIVE_UP (18);
12998 start_pos = it.current.pos;
12999
13000 /* Start displaying new lines in the desired matrix at the same
13001 vpos we would use in the current matrix, i.e. below
13002 last_unchanged_at_beg_row. */
13003 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13004 current_matrix);
13005 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13006 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13007
13008 xassert (it.hpos == 0 && it.current_x == 0);
13009 }
13010 else
13011 {
13012 /* There are no reusable lines at the start of the window.
13013 Start displaying in the first line. */
13014 start_display (&it, w, start);
13015 start_pos = it.current.pos;
13016 }
13017
13018 /* Find the first row that is not affected by changes at the end of
13019 the buffer. Value will be null if there is no unchanged row, in
13020 which case we must redisplay to the end of the window. delta
13021 will be set to the value by which buffer positions beginning with
13022 first_unchanged_at_end_row have to be adjusted due to text
13023 changes. */
13024 first_unchanged_at_end_row
13025 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13026 IF_DEBUG (debug_delta = delta);
13027 IF_DEBUG (debug_delta_bytes = delta_bytes);
13028
13029 /* Set stop_pos to the buffer position up to which we will have to
13030 display new lines. If first_unchanged_at_end_row != NULL, this
13031 is the buffer position of the start of the line displayed in that
13032 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13033 that we don't stop at a buffer position. */
13034 stop_pos = 0;
13035 if (first_unchanged_at_end_row)
13036 {
13037 xassert (last_unchanged_at_beg_row == NULL
13038 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13039
13040 /* If this is a continuation line, move forward to the next one
13041 that isn't. Changes in lines above affect this line.
13042 Caution: this may move first_unchanged_at_end_row to a row
13043 not displaying text. */
13044 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13045 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13046 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13047 < it.last_visible_y))
13048 ++first_unchanged_at_end_row;
13049
13050 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13051 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13052 >= it.last_visible_y))
13053 first_unchanged_at_end_row = NULL;
13054 else
13055 {
13056 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13057 + delta);
13058 first_unchanged_at_end_vpos
13059 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13060 xassert (stop_pos >= Z - END_UNCHANGED);
13061 }
13062 }
13063 else if (last_unchanged_at_beg_row == NULL)
13064 GIVE_UP (19);
13065
13066
13067 #if GLYPH_DEBUG
13068
13069 /* Either there is no unchanged row at the end, or the one we have
13070 now displays text. This is a necessary condition for the window
13071 end pos calculation at the end of this function. */
13072 xassert (first_unchanged_at_end_row == NULL
13073 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13074
13075 debug_last_unchanged_at_beg_vpos
13076 = (last_unchanged_at_beg_row
13077 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13078 : -1);
13079 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13080
13081 #endif /* GLYPH_DEBUG != 0 */
13082
13083
13084 /* Display new lines. Set last_text_row to the last new line
13085 displayed which has text on it, i.e. might end up as being the
13086 line where the window_end_vpos is. */
13087 w->cursor.vpos = -1;
13088 last_text_row = NULL;
13089 overlay_arrow_seen = 0;
13090 while (it.current_y < it.last_visible_y
13091 && !fonts_changed_p
13092 && (first_unchanged_at_end_row == NULL
13093 || IT_CHARPOS (it) < stop_pos))
13094 {
13095 if (display_line (&it))
13096 last_text_row = it.glyph_row - 1;
13097 }
13098
13099 if (fonts_changed_p)
13100 return -1;
13101
13102
13103 /* Compute differences in buffer positions, y-positions etc. for
13104 lines reused at the bottom of the window. Compute what we can
13105 scroll. */
13106 if (first_unchanged_at_end_row
13107 /* No lines reused because we displayed everything up to the
13108 bottom of the window. */
13109 && it.current_y < it.last_visible_y)
13110 {
13111 dvpos = (it.vpos
13112 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13113 current_matrix));
13114 dy = it.current_y - first_unchanged_at_end_row->y;
13115 run.current_y = first_unchanged_at_end_row->y;
13116 run.desired_y = run.current_y + dy;
13117 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13118 }
13119 else
13120 {
13121 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13122 first_unchanged_at_end_row = NULL;
13123 }
13124 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13125
13126
13127 /* Find the cursor if not already found. We have to decide whether
13128 PT will appear on this window (it sometimes doesn't, but this is
13129 not a very frequent case.) This decision has to be made before
13130 the current matrix is altered. A value of cursor.vpos < 0 means
13131 that PT is either in one of the lines beginning at
13132 first_unchanged_at_end_row or below the window. Don't care for
13133 lines that might be displayed later at the window end; as
13134 mentioned, this is not a frequent case. */
13135 if (w->cursor.vpos < 0)
13136 {
13137 /* Cursor in unchanged rows at the top? */
13138 if (PT < CHARPOS (start_pos)
13139 && last_unchanged_at_beg_row)
13140 {
13141 row = row_containing_pos (w, PT,
13142 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13143 last_unchanged_at_beg_row + 1, 0);
13144 if (row)
13145 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13146 }
13147
13148 /* Start from first_unchanged_at_end_row looking for PT. */
13149 else if (first_unchanged_at_end_row)
13150 {
13151 row = row_containing_pos (w, PT - delta,
13152 first_unchanged_at_end_row, NULL, 0);
13153 if (row)
13154 set_cursor_from_row (w, row, w->current_matrix, delta,
13155 delta_bytes, dy, dvpos);
13156 }
13157
13158 /* Give up if cursor was not found. */
13159 if (w->cursor.vpos < 0)
13160 {
13161 clear_glyph_matrix (w->desired_matrix);
13162 return -1;
13163 }
13164 }
13165
13166 /* Don't let the cursor end in the scroll margins. */
13167 {
13168 int this_scroll_margin, cursor_height;
13169
13170 this_scroll_margin = max (0, scroll_margin);
13171 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13172 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13173 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13174
13175 if ((w->cursor.y < this_scroll_margin
13176 && CHARPOS (start) > BEGV)
13177 /* Don't take scroll margin into account at the bottom because
13178 old redisplay didn't do it either. */
13179 || w->cursor.y + cursor_height > it.last_visible_y)
13180 {
13181 w->cursor.vpos = -1;
13182 clear_glyph_matrix (w->desired_matrix);
13183 return -1;
13184 }
13185 }
13186
13187 /* Scroll the display. Do it before changing the current matrix so
13188 that xterm.c doesn't get confused about where the cursor glyph is
13189 found. */
13190 if (dy && run.height)
13191 {
13192 update_begin (f);
13193
13194 if (FRAME_WINDOW_P (f))
13195 {
13196 rif->update_window_begin_hook (w);
13197 rif->clear_window_mouse_face (w);
13198 rif->scroll_run_hook (w, &run);
13199 rif->update_window_end_hook (w, 0, 0);
13200 }
13201 else
13202 {
13203 /* Terminal frame. In this case, dvpos gives the number of
13204 lines to scroll by; dvpos < 0 means scroll up. */
13205 int first_unchanged_at_end_vpos
13206 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13207 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13208 int end = (WINDOW_TOP_EDGE_LINE (w)
13209 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13210 + window_internal_height (w));
13211
13212 /* Perform the operation on the screen. */
13213 if (dvpos > 0)
13214 {
13215 /* Scroll last_unchanged_at_beg_row to the end of the
13216 window down dvpos lines. */
13217 set_terminal_window (end);
13218
13219 /* On dumb terminals delete dvpos lines at the end
13220 before inserting dvpos empty lines. */
13221 if (!scroll_region_ok)
13222 ins_del_lines (end - dvpos, -dvpos);
13223
13224 /* Insert dvpos empty lines in front of
13225 last_unchanged_at_beg_row. */
13226 ins_del_lines (from, dvpos);
13227 }
13228 else if (dvpos < 0)
13229 {
13230 /* Scroll up last_unchanged_at_beg_vpos to the end of
13231 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13232 set_terminal_window (end);
13233
13234 /* Delete dvpos lines in front of
13235 last_unchanged_at_beg_vpos. ins_del_lines will set
13236 the cursor to the given vpos and emit |dvpos| delete
13237 line sequences. */
13238 ins_del_lines (from + dvpos, dvpos);
13239
13240 /* On a dumb terminal insert dvpos empty lines at the
13241 end. */
13242 if (!scroll_region_ok)
13243 ins_del_lines (end + dvpos, -dvpos);
13244 }
13245
13246 set_terminal_window (0);
13247 }
13248
13249 update_end (f);
13250 }
13251
13252 /* Shift reused rows of the current matrix to the right position.
13253 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13254 text. */
13255 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13256 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
13257 if (dvpos < 0)
13258 {
13259 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13260 bottom_vpos, dvpos);
13261 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13262 bottom_vpos, 0);
13263 }
13264 else if (dvpos > 0)
13265 {
13266 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13267 bottom_vpos, dvpos);
13268 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13269 first_unchanged_at_end_vpos + dvpos, 0);
13270 }
13271
13272 /* For frame-based redisplay, make sure that current frame and window
13273 matrix are in sync with respect to glyph memory. */
13274 if (!FRAME_WINDOW_P (f))
13275 sync_frame_with_window_matrix_rows (w);
13276
13277 /* Adjust buffer positions in reused rows. */
13278 if (delta)
13279 increment_matrix_positions (current_matrix,
13280 first_unchanged_at_end_vpos + dvpos,
13281 bottom_vpos, delta, delta_bytes);
13282
13283 /* Adjust Y positions. */
13284 if (dy)
13285 shift_glyph_matrix (w, current_matrix,
13286 first_unchanged_at_end_vpos + dvpos,
13287 bottom_vpos, dy);
13288
13289 if (first_unchanged_at_end_row)
13290 first_unchanged_at_end_row += dvpos;
13291
13292 /* If scrolling up, there may be some lines to display at the end of
13293 the window. */
13294 last_text_row_at_end = NULL;
13295 if (dy < 0)
13296 {
13297 /* Scrolling up can leave for example a partially visible line
13298 at the end of the window to be redisplayed. */
13299 /* Set last_row to the glyph row in the current matrix where the
13300 window end line is found. It has been moved up or down in
13301 the matrix by dvpos. */
13302 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13303 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13304
13305 /* If last_row is the window end line, it should display text. */
13306 xassert (last_row->displays_text_p);
13307
13308 /* If window end line was partially visible before, begin
13309 displaying at that line. Otherwise begin displaying with the
13310 line following it. */
13311 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13312 {
13313 init_to_row_start (&it, w, last_row);
13314 it.vpos = last_vpos;
13315 it.current_y = last_row->y;
13316 }
13317 else
13318 {
13319 init_to_row_end (&it, w, last_row);
13320 it.vpos = 1 + last_vpos;
13321 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13322 ++last_row;
13323 }
13324
13325 /* We may start in a continuation line. If so, we have to
13326 get the right continuation_lines_width and current_x. */
13327 it.continuation_lines_width = last_row->continuation_lines_width;
13328 it.hpos = it.current_x = 0;
13329
13330 /* Display the rest of the lines at the window end. */
13331 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13332 while (it.current_y < it.last_visible_y
13333 && !fonts_changed_p)
13334 {
13335 /* Is it always sure that the display agrees with lines in
13336 the current matrix? I don't think so, so we mark rows
13337 displayed invalid in the current matrix by setting their
13338 enabled_p flag to zero. */
13339 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13340 if (display_line (&it))
13341 last_text_row_at_end = it.glyph_row - 1;
13342 }
13343 }
13344
13345 /* Update window_end_pos and window_end_vpos. */
13346 if (first_unchanged_at_end_row
13347 && first_unchanged_at_end_row->y < it.last_visible_y
13348 && !last_text_row_at_end)
13349 {
13350 /* Window end line if one of the preserved rows from the current
13351 matrix. Set row to the last row displaying text in current
13352 matrix starting at first_unchanged_at_end_row, after
13353 scrolling. */
13354 xassert (first_unchanged_at_end_row->displays_text_p);
13355 row = find_last_row_displaying_text (w->current_matrix, &it,
13356 first_unchanged_at_end_row);
13357 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
13358
13359 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13360 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13361 w->window_end_vpos
13362 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
13363 xassert (w->window_end_bytepos >= 0);
13364 IF_DEBUG (debug_method_add (w, "A"));
13365 }
13366 else if (last_text_row_at_end)
13367 {
13368 w->window_end_pos
13369 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
13370 w->window_end_bytepos
13371 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
13372 w->window_end_vpos
13373 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
13374 xassert (w->window_end_bytepos >= 0);
13375 IF_DEBUG (debug_method_add (w, "B"));
13376 }
13377 else if (last_text_row)
13378 {
13379 /* We have displayed either to the end of the window or at the
13380 end of the window, i.e. the last row with text is to be found
13381 in the desired matrix. */
13382 w->window_end_pos
13383 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13384 w->window_end_bytepos
13385 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13386 w->window_end_vpos
13387 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
13388 xassert (w->window_end_bytepos >= 0);
13389 }
13390 else if (first_unchanged_at_end_row == NULL
13391 && last_text_row == NULL
13392 && last_text_row_at_end == NULL)
13393 {
13394 /* Displayed to end of window, but no line containing text was
13395 displayed. Lines were deleted at the end of the window. */
13396 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
13397 int vpos = XFASTINT (w->window_end_vpos);
13398 struct glyph_row *current_row = current_matrix->rows + vpos;
13399 struct glyph_row *desired_row = desired_matrix->rows + vpos;
13400
13401 for (row = NULL;
13402 row == NULL && vpos >= first_vpos;
13403 --vpos, --current_row, --desired_row)
13404 {
13405 if (desired_row->enabled_p)
13406 {
13407 if (desired_row->displays_text_p)
13408 row = desired_row;
13409 }
13410 else if (current_row->displays_text_p)
13411 row = current_row;
13412 }
13413
13414 xassert (row != NULL);
13415 w->window_end_vpos = make_number (vpos + 1);
13416 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13417 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13418 xassert (w->window_end_bytepos >= 0);
13419 IF_DEBUG (debug_method_add (w, "C"));
13420 }
13421 else
13422 abort ();
13423
13424 #if 0 /* This leads to problems, for instance when the cursor is
13425 at ZV, and the cursor line displays no text. */
13426 /* Disable rows below what's displayed in the window. This makes
13427 debugging easier. */
13428 enable_glyph_matrix_rows (current_matrix,
13429 XFASTINT (w->window_end_vpos) + 1,
13430 bottom_vpos, 0);
13431 #endif
13432
13433 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
13434 debug_end_vpos = XFASTINT (w->window_end_vpos));
13435
13436 /* Record that display has not been completed. */
13437 w->window_end_valid = Qnil;
13438 w->desired_matrix->no_scrolling_p = 1;
13439 return 3;
13440
13441 #undef GIVE_UP
13442 }
13443
13444
13445 \f
13446 /***********************************************************************
13447 More debugging support
13448 ***********************************************************************/
13449
13450 #if GLYPH_DEBUG
13451
13452 void dump_glyph_row P_ ((struct glyph_row *, int, int));
13453 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
13454 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
13455
13456
13457 /* Dump the contents of glyph matrix MATRIX on stderr.
13458
13459 GLYPHS 0 means don't show glyph contents.
13460 GLYPHS 1 means show glyphs in short form
13461 GLYPHS > 1 means show glyphs in long form. */
13462
13463 void
13464 dump_glyph_matrix (matrix, glyphs)
13465 struct glyph_matrix *matrix;
13466 int glyphs;
13467 {
13468 int i;
13469 for (i = 0; i < matrix->nrows; ++i)
13470 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
13471 }
13472
13473
13474 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
13475 the glyph row and area where the glyph comes from. */
13476
13477 void
13478 dump_glyph (row, glyph, area)
13479 struct glyph_row *row;
13480 struct glyph *glyph;
13481 int area;
13482 {
13483 if (glyph->type == CHAR_GLYPH)
13484 {
13485 fprintf (stderr,
13486 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13487 glyph - row->glyphs[TEXT_AREA],
13488 'C',
13489 glyph->charpos,
13490 (BUFFERP (glyph->object)
13491 ? 'B'
13492 : (STRINGP (glyph->object)
13493 ? 'S'
13494 : '-')),
13495 glyph->pixel_width,
13496 glyph->u.ch,
13497 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
13498 ? glyph->u.ch
13499 : '.'),
13500 glyph->face_id,
13501 glyph->left_box_line_p,
13502 glyph->right_box_line_p);
13503 }
13504 else if (glyph->type == STRETCH_GLYPH)
13505 {
13506 fprintf (stderr,
13507 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13508 glyph - row->glyphs[TEXT_AREA],
13509 'S',
13510 glyph->charpos,
13511 (BUFFERP (glyph->object)
13512 ? 'B'
13513 : (STRINGP (glyph->object)
13514 ? 'S'
13515 : '-')),
13516 glyph->pixel_width,
13517 0,
13518 '.',
13519 glyph->face_id,
13520 glyph->left_box_line_p,
13521 glyph->right_box_line_p);
13522 }
13523 else if (glyph->type == IMAGE_GLYPH)
13524 {
13525 fprintf (stderr,
13526 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13527 glyph - row->glyphs[TEXT_AREA],
13528 'I',
13529 glyph->charpos,
13530 (BUFFERP (glyph->object)
13531 ? 'B'
13532 : (STRINGP (glyph->object)
13533 ? 'S'
13534 : '-')),
13535 glyph->pixel_width,
13536 glyph->u.img_id,
13537 '.',
13538 glyph->face_id,
13539 glyph->left_box_line_p,
13540 glyph->right_box_line_p);
13541 }
13542 }
13543
13544
13545 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
13546 GLYPHS 0 means don't show glyph contents.
13547 GLYPHS 1 means show glyphs in short form
13548 GLYPHS > 1 means show glyphs in long form. */
13549
13550 void
13551 dump_glyph_row (row, vpos, glyphs)
13552 struct glyph_row *row;
13553 int vpos, glyphs;
13554 {
13555 if (glyphs != 1)
13556 {
13557 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
13558 fprintf (stderr, "=======================================================================\n");
13559
13560 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
13561 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
13562 vpos,
13563 MATRIX_ROW_START_CHARPOS (row),
13564 MATRIX_ROW_END_CHARPOS (row),
13565 row->used[TEXT_AREA],
13566 row->contains_overlapping_glyphs_p,
13567 row->enabled_p,
13568 row->truncated_on_left_p,
13569 row->truncated_on_right_p,
13570 row->overlay_arrow_p,
13571 row->continued_p,
13572 MATRIX_ROW_CONTINUATION_LINE_P (row),
13573 row->displays_text_p,
13574 row->ends_at_zv_p,
13575 row->fill_line_p,
13576 row->ends_in_middle_of_char_p,
13577 row->starts_in_middle_of_char_p,
13578 row->mouse_face_p,
13579 row->x,
13580 row->y,
13581 row->pixel_width,
13582 row->height,
13583 row->visible_height,
13584 row->ascent,
13585 row->phys_ascent);
13586 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
13587 row->end.overlay_string_index,
13588 row->continuation_lines_width);
13589 fprintf (stderr, "%9d %5d\n",
13590 CHARPOS (row->start.string_pos),
13591 CHARPOS (row->end.string_pos));
13592 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
13593 row->end.dpvec_index);
13594 }
13595
13596 if (glyphs > 1)
13597 {
13598 int area;
13599
13600 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13601 {
13602 struct glyph *glyph = row->glyphs[area];
13603 struct glyph *glyph_end = glyph + row->used[area];
13604
13605 /* Glyph for a line end in text. */
13606 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
13607 ++glyph_end;
13608
13609 if (glyph < glyph_end)
13610 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
13611
13612 for (; glyph < glyph_end; ++glyph)
13613 dump_glyph (row, glyph, area);
13614 }
13615 }
13616 else if (glyphs == 1)
13617 {
13618 int area;
13619
13620 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13621 {
13622 char *s = (char *) alloca (row->used[area] + 1);
13623 int i;
13624
13625 for (i = 0; i < row->used[area]; ++i)
13626 {
13627 struct glyph *glyph = row->glyphs[area] + i;
13628 if (glyph->type == CHAR_GLYPH
13629 && glyph->u.ch < 0x80
13630 && glyph->u.ch >= ' ')
13631 s[i] = glyph->u.ch;
13632 else
13633 s[i] = '.';
13634 }
13635
13636 s[i] = '\0';
13637 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
13638 }
13639 }
13640 }
13641
13642
13643 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
13644 Sdump_glyph_matrix, 0, 1, "p",
13645 doc: /* Dump the current matrix of the selected window to stderr.
13646 Shows contents of glyph row structures. With non-nil
13647 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
13648 glyphs in short form, otherwise show glyphs in long form. */)
13649 (glyphs)
13650 Lisp_Object glyphs;
13651 {
13652 struct window *w = XWINDOW (selected_window);
13653 struct buffer *buffer = XBUFFER (w->buffer);
13654
13655 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
13656 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
13657 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
13658 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
13659 fprintf (stderr, "=============================================\n");
13660 dump_glyph_matrix (w->current_matrix,
13661 NILP (glyphs) ? 0 : XINT (glyphs));
13662 return Qnil;
13663 }
13664
13665
13666 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
13667 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
13668 ()
13669 {
13670 struct frame *f = XFRAME (selected_frame);
13671 dump_glyph_matrix (f->current_matrix, 1);
13672 return Qnil;
13673 }
13674
13675
13676 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
13677 doc: /* Dump glyph row ROW to stderr.
13678 GLYPH 0 means don't dump glyphs.
13679 GLYPH 1 means dump glyphs in short form.
13680 GLYPH > 1 or omitted means dump glyphs in long form. */)
13681 (row, glyphs)
13682 Lisp_Object row, glyphs;
13683 {
13684 struct glyph_matrix *matrix;
13685 int vpos;
13686
13687 CHECK_NUMBER (row);
13688 matrix = XWINDOW (selected_window)->current_matrix;
13689 vpos = XINT (row);
13690 if (vpos >= 0 && vpos < matrix->nrows)
13691 dump_glyph_row (MATRIX_ROW (matrix, vpos),
13692 vpos,
13693 INTEGERP (glyphs) ? XINT (glyphs) : 2);
13694 return Qnil;
13695 }
13696
13697
13698 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
13699 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
13700 GLYPH 0 means don't dump glyphs.
13701 GLYPH 1 means dump glyphs in short form.
13702 GLYPH > 1 or omitted means dump glyphs in long form. */)
13703 (row, glyphs)
13704 Lisp_Object row, glyphs;
13705 {
13706 struct frame *sf = SELECTED_FRAME ();
13707 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
13708 int vpos;
13709
13710 CHECK_NUMBER (row);
13711 vpos = XINT (row);
13712 if (vpos >= 0 && vpos < m->nrows)
13713 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
13714 INTEGERP (glyphs) ? XINT (glyphs) : 2);
13715 return Qnil;
13716 }
13717
13718
13719 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
13720 doc: /* Toggle tracing of redisplay.
13721 With ARG, turn tracing on if and only if ARG is positive. */)
13722 (arg)
13723 Lisp_Object arg;
13724 {
13725 if (NILP (arg))
13726 trace_redisplay_p = !trace_redisplay_p;
13727 else
13728 {
13729 arg = Fprefix_numeric_value (arg);
13730 trace_redisplay_p = XINT (arg) > 0;
13731 }
13732
13733 return Qnil;
13734 }
13735
13736
13737 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
13738 doc: /* Like `format', but print result to stderr.
13739 usage: (trace-to-stderr STRING &rest OBJECTS) */)
13740 (nargs, args)
13741 int nargs;
13742 Lisp_Object *args;
13743 {
13744 Lisp_Object s = Fformat (nargs, args);
13745 fprintf (stderr, "%s", SDATA (s));
13746 return Qnil;
13747 }
13748
13749 #endif /* GLYPH_DEBUG */
13750
13751
13752 \f
13753 /***********************************************************************
13754 Building Desired Matrix Rows
13755 ***********************************************************************/
13756
13757 /* Return a temporary glyph row holding the glyphs of an overlay
13758 arrow. Only used for non-window-redisplay windows. */
13759
13760 static struct glyph_row *
13761 get_overlay_arrow_glyph_row (w)
13762 struct window *w;
13763 {
13764 struct frame *f = XFRAME (WINDOW_FRAME (w));
13765 struct buffer *buffer = XBUFFER (w->buffer);
13766 struct buffer *old = current_buffer;
13767 const unsigned char *arrow_string = SDATA (Voverlay_arrow_string);
13768 int arrow_len = SCHARS (Voverlay_arrow_string);
13769 const unsigned char *arrow_end = arrow_string + arrow_len;
13770 const unsigned char *p;
13771 struct it it;
13772 int multibyte_p;
13773 int n_glyphs_before;
13774
13775 set_buffer_temp (buffer);
13776 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
13777 it.glyph_row->used[TEXT_AREA] = 0;
13778 SET_TEXT_POS (it.position, 0, 0);
13779
13780 multibyte_p = !NILP (buffer->enable_multibyte_characters);
13781 p = arrow_string;
13782 while (p < arrow_end)
13783 {
13784 Lisp_Object face, ilisp;
13785
13786 /* Get the next character. */
13787 if (multibyte_p)
13788 it.c = string_char_and_length (p, arrow_len, &it.len);
13789 else
13790 it.c = *p, it.len = 1;
13791 p += it.len;
13792
13793 /* Get its face. */
13794 ilisp = make_number (p - arrow_string);
13795 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
13796 it.face_id = compute_char_face (f, it.c, face);
13797
13798 /* Compute its width, get its glyphs. */
13799 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
13800 SET_TEXT_POS (it.position, -1, -1);
13801 PRODUCE_GLYPHS (&it);
13802
13803 /* If this character doesn't fit any more in the line, we have
13804 to remove some glyphs. */
13805 if (it.current_x > it.last_visible_x)
13806 {
13807 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
13808 break;
13809 }
13810 }
13811
13812 set_buffer_temp (old);
13813 return it.glyph_row;
13814 }
13815
13816
13817 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
13818 glyphs are only inserted for terminal frames since we can't really
13819 win with truncation glyphs when partially visible glyphs are
13820 involved. Which glyphs to insert is determined by
13821 produce_special_glyphs. */
13822
13823 static void
13824 insert_left_trunc_glyphs (it)
13825 struct it *it;
13826 {
13827 struct it truncate_it;
13828 struct glyph *from, *end, *to, *toend;
13829
13830 xassert (!FRAME_WINDOW_P (it->f));
13831
13832 /* Get the truncation glyphs. */
13833 truncate_it = *it;
13834 truncate_it.current_x = 0;
13835 truncate_it.face_id = DEFAULT_FACE_ID;
13836 truncate_it.glyph_row = &scratch_glyph_row;
13837 truncate_it.glyph_row->used[TEXT_AREA] = 0;
13838 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
13839 truncate_it.object = make_number (0);
13840 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
13841
13842 /* Overwrite glyphs from IT with truncation glyphs. */
13843 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
13844 end = from + truncate_it.glyph_row->used[TEXT_AREA];
13845 to = it->glyph_row->glyphs[TEXT_AREA];
13846 toend = to + it->glyph_row->used[TEXT_AREA];
13847
13848 while (from < end)
13849 *to++ = *from++;
13850
13851 /* There may be padding glyphs left over. Overwrite them too. */
13852 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
13853 {
13854 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
13855 while (from < end)
13856 *to++ = *from++;
13857 }
13858
13859 if (to > toend)
13860 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
13861 }
13862
13863
13864 /* Compute the pixel height and width of IT->glyph_row.
13865
13866 Most of the time, ascent and height of a display line will be equal
13867 to the max_ascent and max_height values of the display iterator
13868 structure. This is not the case if
13869
13870 1. We hit ZV without displaying anything. In this case, max_ascent
13871 and max_height will be zero.
13872
13873 2. We have some glyphs that don't contribute to the line height.
13874 (The glyph row flag contributes_to_line_height_p is for future
13875 pixmap extensions).
13876
13877 The first case is easily covered by using default values because in
13878 these cases, the line height does not really matter, except that it
13879 must not be zero. */
13880
13881 static void
13882 compute_line_metrics (it)
13883 struct it *it;
13884 {
13885 struct glyph_row *row = it->glyph_row;
13886 int area, i;
13887
13888 if (FRAME_WINDOW_P (it->f))
13889 {
13890 int i, min_y, max_y;
13891
13892 /* The line may consist of one space only, that was added to
13893 place the cursor on it. If so, the row's height hasn't been
13894 computed yet. */
13895 if (row->height == 0)
13896 {
13897 if (it->max_ascent + it->max_descent == 0)
13898 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
13899 row->ascent = it->max_ascent;
13900 row->height = it->max_ascent + it->max_descent;
13901 row->phys_ascent = it->max_phys_ascent;
13902 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
13903 }
13904
13905 /* Compute the width of this line. */
13906 row->pixel_width = row->x;
13907 for (i = 0; i < row->used[TEXT_AREA]; ++i)
13908 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
13909
13910 xassert (row->pixel_width >= 0);
13911 xassert (row->ascent >= 0 && row->height > 0);
13912
13913 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
13914 || MATRIX_ROW_OVERLAPS_PRED_P (row));
13915
13916 /* If first line's physical ascent is larger than its logical
13917 ascent, use the physical ascent, and make the row taller.
13918 This makes accented characters fully visible. */
13919 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
13920 && row->phys_ascent > row->ascent)
13921 {
13922 row->height += row->phys_ascent - row->ascent;
13923 row->ascent = row->phys_ascent;
13924 }
13925
13926 /* Compute how much of the line is visible. */
13927 row->visible_height = row->height;
13928
13929 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
13930 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
13931
13932 if (row->y < min_y)
13933 row->visible_height -= min_y - row->y;
13934 if (row->y + row->height > max_y)
13935 row->visible_height -= row->y + row->height - max_y;
13936 }
13937 else
13938 {
13939 row->pixel_width = row->used[TEXT_AREA];
13940 if (row->continued_p)
13941 row->pixel_width -= it->continuation_pixel_width;
13942 else if (row->truncated_on_right_p)
13943 row->pixel_width -= it->truncation_pixel_width;
13944 row->ascent = row->phys_ascent = 0;
13945 row->height = row->phys_height = row->visible_height = 1;
13946 }
13947
13948 /* Compute a hash code for this row. */
13949 row->hash = 0;
13950 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13951 for (i = 0; i < row->used[area]; ++i)
13952 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
13953 + row->glyphs[area][i].u.val
13954 + row->glyphs[area][i].face_id
13955 + row->glyphs[area][i].padding_p
13956 + (row->glyphs[area][i].type << 2));
13957
13958 it->max_ascent = it->max_descent = 0;
13959 it->max_phys_ascent = it->max_phys_descent = 0;
13960 }
13961
13962
13963 /* Append one space to the glyph row of iterator IT if doing a
13964 window-based redisplay. DEFAULT_FACE_P non-zero means let the
13965 space have the default face, otherwise let it have the same face as
13966 IT->face_id. Value is non-zero if a space was added.
13967
13968 This function is called to make sure that there is always one glyph
13969 at the end of a glyph row that the cursor can be set on under
13970 window-systems. (If there weren't such a glyph we would not know
13971 how wide and tall a box cursor should be displayed).
13972
13973 At the same time this space let's a nicely handle clearing to the
13974 end of the line if the row ends in italic text. */
13975
13976 static int
13977 append_space (it, default_face_p)
13978 struct it *it;
13979 int default_face_p;
13980 {
13981 if (FRAME_WINDOW_P (it->f))
13982 {
13983 int n = it->glyph_row->used[TEXT_AREA];
13984
13985 if (it->glyph_row->glyphs[TEXT_AREA] + n
13986 < it->glyph_row->glyphs[1 + TEXT_AREA])
13987 {
13988 /* Save some values that must not be changed.
13989 Must save IT->c and IT->len because otherwise
13990 ITERATOR_AT_END_P wouldn't work anymore after
13991 append_space has been called. */
13992 enum display_element_type saved_what = it->what;
13993 int saved_c = it->c, saved_len = it->len;
13994 int saved_x = it->current_x;
13995 int saved_face_id = it->face_id;
13996 struct text_pos saved_pos;
13997 Lisp_Object saved_object;
13998 struct face *face;
13999
14000 saved_object = it->object;
14001 saved_pos = it->position;
14002
14003 it->what = IT_CHARACTER;
14004 bzero (&it->position, sizeof it->position);
14005 it->object = make_number (0);
14006 it->c = ' ';
14007 it->len = 1;
14008
14009 if (default_face_p)
14010 it->face_id = DEFAULT_FACE_ID;
14011 else if (it->face_before_selective_p)
14012 it->face_id = it->saved_face_id;
14013 face = FACE_FROM_ID (it->f, it->face_id);
14014 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14015
14016 PRODUCE_GLYPHS (it);
14017
14018 it->current_x = saved_x;
14019 it->object = saved_object;
14020 it->position = saved_pos;
14021 it->what = saved_what;
14022 it->face_id = saved_face_id;
14023 it->len = saved_len;
14024 it->c = saved_c;
14025 return 1;
14026 }
14027 }
14028
14029 return 0;
14030 }
14031
14032
14033 /* Extend the face of the last glyph in the text area of IT->glyph_row
14034 to the end of the display line. Called from display_line.
14035 If the glyph row is empty, add a space glyph to it so that we
14036 know the face to draw. Set the glyph row flag fill_line_p. */
14037
14038 static void
14039 extend_face_to_end_of_line (it)
14040 struct it *it;
14041 {
14042 struct face *face;
14043 struct frame *f = it->f;
14044
14045 /* If line is already filled, do nothing. */
14046 if (it->current_x >= it->last_visible_x)
14047 return;
14048
14049 /* Face extension extends the background and box of IT->face_id
14050 to the end of the line. If the background equals the background
14051 of the frame, we don't have to do anything. */
14052 if (it->face_before_selective_p)
14053 face = FACE_FROM_ID (it->f, it->saved_face_id);
14054 else
14055 face = FACE_FROM_ID (f, it->face_id);
14056
14057 if (FRAME_WINDOW_P (f)
14058 && face->box == FACE_NO_BOX
14059 && face->background == FRAME_BACKGROUND_PIXEL (f)
14060 && !face->stipple)
14061 return;
14062
14063 /* Set the glyph row flag indicating that the face of the last glyph
14064 in the text area has to be drawn to the end of the text area. */
14065 it->glyph_row->fill_line_p = 1;
14066
14067 /* If current character of IT is not ASCII, make sure we have the
14068 ASCII face. This will be automatically undone the next time
14069 get_next_display_element returns a multibyte character. Note
14070 that the character will always be single byte in unibyte text. */
14071 if (!SINGLE_BYTE_CHAR_P (it->c))
14072 {
14073 it->face_id = FACE_FOR_CHAR (f, face, 0);
14074 }
14075
14076 if (FRAME_WINDOW_P (f))
14077 {
14078 /* If the row is empty, add a space with the current face of IT,
14079 so that we know which face to draw. */
14080 if (it->glyph_row->used[TEXT_AREA] == 0)
14081 {
14082 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14083 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14084 it->glyph_row->used[TEXT_AREA] = 1;
14085 }
14086 }
14087 else
14088 {
14089 /* Save some values that must not be changed. */
14090 int saved_x = it->current_x;
14091 struct text_pos saved_pos;
14092 Lisp_Object saved_object;
14093 enum display_element_type saved_what = it->what;
14094 int saved_face_id = it->face_id;
14095
14096 saved_object = it->object;
14097 saved_pos = it->position;
14098
14099 it->what = IT_CHARACTER;
14100 bzero (&it->position, sizeof it->position);
14101 it->object = make_number (0);
14102 it->c = ' ';
14103 it->len = 1;
14104 it->face_id = face->id;
14105
14106 PRODUCE_GLYPHS (it);
14107
14108 while (it->current_x <= it->last_visible_x)
14109 PRODUCE_GLYPHS (it);
14110
14111 /* Don't count these blanks really. It would let us insert a left
14112 truncation glyph below and make us set the cursor on them, maybe. */
14113 it->current_x = saved_x;
14114 it->object = saved_object;
14115 it->position = saved_pos;
14116 it->what = saved_what;
14117 it->face_id = saved_face_id;
14118 }
14119 }
14120
14121
14122 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14123 trailing whitespace. */
14124
14125 static int
14126 trailing_whitespace_p (charpos)
14127 int charpos;
14128 {
14129 int bytepos = CHAR_TO_BYTE (charpos);
14130 int c = 0;
14131
14132 while (bytepos < ZV_BYTE
14133 && (c = FETCH_CHAR (bytepos),
14134 c == ' ' || c == '\t'))
14135 ++bytepos;
14136
14137 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14138 {
14139 if (bytepos != PT_BYTE)
14140 return 1;
14141 }
14142 return 0;
14143 }
14144
14145
14146 /* Highlight trailing whitespace, if any, in ROW. */
14147
14148 void
14149 highlight_trailing_whitespace (f, row)
14150 struct frame *f;
14151 struct glyph_row *row;
14152 {
14153 int used = row->used[TEXT_AREA];
14154
14155 if (used)
14156 {
14157 struct glyph *start = row->glyphs[TEXT_AREA];
14158 struct glyph *glyph = start + used - 1;
14159
14160 /* Skip over glyphs inserted to display the cursor at the
14161 end of a line, for extending the face of the last glyph
14162 to the end of the line on terminals, and for truncation
14163 and continuation glyphs. */
14164 while (glyph >= start
14165 && glyph->type == CHAR_GLYPH
14166 && INTEGERP (glyph->object))
14167 --glyph;
14168
14169 /* If last glyph is a space or stretch, and it's trailing
14170 whitespace, set the face of all trailing whitespace glyphs in
14171 IT->glyph_row to `trailing-whitespace'. */
14172 if (glyph >= start
14173 && BUFFERP (glyph->object)
14174 && (glyph->type == STRETCH_GLYPH
14175 || (glyph->type == CHAR_GLYPH
14176 && glyph->u.ch == ' '))
14177 && trailing_whitespace_p (glyph->charpos))
14178 {
14179 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
14180
14181 while (glyph >= start
14182 && BUFFERP (glyph->object)
14183 && (glyph->type == STRETCH_GLYPH
14184 || (glyph->type == CHAR_GLYPH
14185 && glyph->u.ch == ' ')))
14186 (glyph--)->face_id = face_id;
14187 }
14188 }
14189 }
14190
14191
14192 /* Value is non-zero if glyph row ROW in window W should be
14193 used to hold the cursor. */
14194
14195 static int
14196 cursor_row_p (w, row)
14197 struct window *w;
14198 struct glyph_row *row;
14199 {
14200 int cursor_row_p = 1;
14201
14202 if (PT == MATRIX_ROW_END_CHARPOS (row))
14203 {
14204 /* If the row ends with a newline from a string, we don't want
14205 the cursor there (if the row is continued it doesn't end in a
14206 newline). */
14207 if (CHARPOS (row->end.string_pos) >= 0
14208 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14209 cursor_row_p = row->continued_p;
14210
14211 /* If the row ends at ZV, display the cursor at the end of that
14212 row instead of at the start of the row below. */
14213 else if (row->ends_at_zv_p)
14214 cursor_row_p = 1;
14215 else
14216 cursor_row_p = 0;
14217 }
14218
14219 return cursor_row_p;
14220 }
14221
14222
14223 /* Construct the glyph row IT->glyph_row in the desired matrix of
14224 IT->w from text at the current position of IT. See dispextern.h
14225 for an overview of struct it. Value is non-zero if
14226 IT->glyph_row displays text, as opposed to a line displaying ZV
14227 only. */
14228
14229 static int
14230 display_line (it)
14231 struct it *it;
14232 {
14233 struct glyph_row *row = it->glyph_row;
14234
14235 /* We always start displaying at hpos zero even if hscrolled. */
14236 xassert (it->hpos == 0 && it->current_x == 0);
14237
14238 /* We must not display in a row that's not a text row. */
14239 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14240 < it->w->desired_matrix->nrows);
14241
14242 /* Is IT->w showing the region? */
14243 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
14244
14245 /* Clear the result glyph row and enable it. */
14246 prepare_desired_row (row);
14247
14248 row->y = it->current_y;
14249 row->start = it->current;
14250 row->continuation_lines_width = it->continuation_lines_width;
14251 row->displays_text_p = 1;
14252 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14253 it->starts_in_middle_of_char_p = 0;
14254
14255 /* Arrange the overlays nicely for our purposes. Usually, we call
14256 display_line on only one line at a time, in which case this
14257 can't really hurt too much, or we call it on lines which appear
14258 one after another in the buffer, in which case all calls to
14259 recenter_overlay_lists but the first will be pretty cheap. */
14260 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14261
14262 /* Move over display elements that are not visible because we are
14263 hscrolled. This may stop at an x-position < IT->first_visible_x
14264 if the first glyph is partially visible or if we hit a line end. */
14265 if (it->current_x < it->first_visible_x)
14266 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14267 MOVE_TO_POS | MOVE_TO_X);
14268
14269 /* Get the initial row height. This is either the height of the
14270 text hscrolled, if there is any, or zero. */
14271 row->ascent = it->max_ascent;
14272 row->height = it->max_ascent + it->max_descent;
14273 row->phys_ascent = it->max_phys_ascent;
14274 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14275
14276 /* Loop generating characters. The loop is left with IT on the next
14277 character to display. */
14278 while (1)
14279 {
14280 int n_glyphs_before, hpos_before, x_before;
14281 int x, i, nglyphs;
14282 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
14283
14284 /* Retrieve the next thing to display. Value is zero if end of
14285 buffer reached. */
14286 if (!get_next_display_element (it))
14287 {
14288 /* Maybe add a space at the end of this line that is used to
14289 display the cursor there under X. Set the charpos of the
14290 first glyph of blank lines not corresponding to any text
14291 to -1. */
14292 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
14293 || row->used[TEXT_AREA] == 0)
14294 {
14295 row->glyphs[TEXT_AREA]->charpos = -1;
14296 row->displays_text_p = 0;
14297
14298 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
14299 && (!MINI_WINDOW_P (it->w)
14300 || (minibuf_level && EQ (it->window, minibuf_window))))
14301 row->indicate_empty_line_p = 1;
14302 }
14303
14304 it->continuation_lines_width = 0;
14305 row->ends_at_zv_p = 1;
14306 break;
14307 }
14308
14309 /* Now, get the metrics of what we want to display. This also
14310 generates glyphs in `row' (which is IT->glyph_row). */
14311 n_glyphs_before = row->used[TEXT_AREA];
14312 x = it->current_x;
14313
14314 /* Remember the line height so far in case the next element doesn't
14315 fit on the line. */
14316 if (!it->truncate_lines_p)
14317 {
14318 ascent = it->max_ascent;
14319 descent = it->max_descent;
14320 phys_ascent = it->max_phys_ascent;
14321 phys_descent = it->max_phys_descent;
14322 }
14323
14324 PRODUCE_GLYPHS (it);
14325
14326 /* If this display element was in marginal areas, continue with
14327 the next one. */
14328 if (it->area != TEXT_AREA)
14329 {
14330 row->ascent = max (row->ascent, it->max_ascent);
14331 row->height = max (row->height, it->max_ascent + it->max_descent);
14332 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14333 row->phys_height = max (row->phys_height,
14334 it->max_phys_ascent + it->max_phys_descent);
14335 set_iterator_to_next (it, 1);
14336 continue;
14337 }
14338
14339 /* Does the display element fit on the line? If we truncate
14340 lines, we should draw past the right edge of the window. If
14341 we don't truncate, we want to stop so that we can display the
14342 continuation glyph before the right margin. If lines are
14343 continued, there are two possible strategies for characters
14344 resulting in more than 1 glyph (e.g. tabs): Display as many
14345 glyphs as possible in this line and leave the rest for the
14346 continuation line, or display the whole element in the next
14347 line. Original redisplay did the former, so we do it also. */
14348 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14349 hpos_before = it->hpos;
14350 x_before = x;
14351
14352 if (/* Not a newline. */
14353 nglyphs > 0
14354 /* Glyphs produced fit entirely in the line. */
14355 && it->current_x < it->last_visible_x)
14356 {
14357 it->hpos += nglyphs;
14358 row->ascent = max (row->ascent, it->max_ascent);
14359 row->height = max (row->height, it->max_ascent + it->max_descent);
14360 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14361 row->phys_height = max (row->phys_height,
14362 it->max_phys_ascent + it->max_phys_descent);
14363 if (it->current_x - it->pixel_width < it->first_visible_x)
14364 row->x = x - it->first_visible_x;
14365 }
14366 else
14367 {
14368 int new_x;
14369 struct glyph *glyph;
14370
14371 for (i = 0; i < nglyphs; ++i, x = new_x)
14372 {
14373 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14374 new_x = x + glyph->pixel_width;
14375
14376 if (/* Lines are continued. */
14377 !it->truncate_lines_p
14378 && (/* Glyph doesn't fit on the line. */
14379 new_x > it->last_visible_x
14380 /* Or it fits exactly on a window system frame. */
14381 || (new_x == it->last_visible_x
14382 && FRAME_WINDOW_P (it->f))))
14383 {
14384 /* End of a continued line. */
14385
14386 if (it->hpos == 0
14387 || (new_x == it->last_visible_x
14388 && FRAME_WINDOW_P (it->f)))
14389 {
14390 /* Current glyph is the only one on the line or
14391 fits exactly on the line. We must continue
14392 the line because we can't draw the cursor
14393 after the glyph. */
14394 row->continued_p = 1;
14395 it->current_x = new_x;
14396 it->continuation_lines_width += new_x;
14397 ++it->hpos;
14398 if (i == nglyphs - 1)
14399 set_iterator_to_next (it, 1);
14400 }
14401 else if (CHAR_GLYPH_PADDING_P (*glyph)
14402 && !FRAME_WINDOW_P (it->f))
14403 {
14404 /* A padding glyph that doesn't fit on this line.
14405 This means the whole character doesn't fit
14406 on the line. */
14407 row->used[TEXT_AREA] = n_glyphs_before;
14408
14409 /* Fill the rest of the row with continuation
14410 glyphs like in 20.x. */
14411 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
14412 < row->glyphs[1 + TEXT_AREA])
14413 produce_special_glyphs (it, IT_CONTINUATION);
14414
14415 row->continued_p = 1;
14416 it->current_x = x_before;
14417 it->continuation_lines_width += x_before;
14418
14419 /* Restore the height to what it was before the
14420 element not fitting on the line. */
14421 it->max_ascent = ascent;
14422 it->max_descent = descent;
14423 it->max_phys_ascent = phys_ascent;
14424 it->max_phys_descent = phys_descent;
14425 }
14426 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
14427 {
14428 /* A TAB that extends past the right edge of the
14429 window. This produces a single glyph on
14430 window system frames. We leave the glyph in
14431 this row and let it fill the row, but don't
14432 consume the TAB. */
14433 it->continuation_lines_width += it->last_visible_x;
14434 row->ends_in_middle_of_char_p = 1;
14435 row->continued_p = 1;
14436 glyph->pixel_width = it->last_visible_x - x;
14437 it->starts_in_middle_of_char_p = 1;
14438 }
14439 else
14440 {
14441 /* Something other than a TAB that draws past
14442 the right edge of the window. Restore
14443 positions to values before the element. */
14444 row->used[TEXT_AREA] = n_glyphs_before + i;
14445
14446 /* Display continuation glyphs. */
14447 if (!FRAME_WINDOW_P (it->f))
14448 produce_special_glyphs (it, IT_CONTINUATION);
14449 row->continued_p = 1;
14450
14451 it->continuation_lines_width += x;
14452
14453 if (nglyphs > 1 && i > 0)
14454 {
14455 row->ends_in_middle_of_char_p = 1;
14456 it->starts_in_middle_of_char_p = 1;
14457 }
14458
14459 /* Restore the height to what it was before the
14460 element not fitting on the line. */
14461 it->max_ascent = ascent;
14462 it->max_descent = descent;
14463 it->max_phys_ascent = phys_ascent;
14464 it->max_phys_descent = phys_descent;
14465 }
14466
14467 break;
14468 }
14469 else if (new_x > it->first_visible_x)
14470 {
14471 /* Increment number of glyphs actually displayed. */
14472 ++it->hpos;
14473
14474 if (x < it->first_visible_x)
14475 /* Glyph is partially visible, i.e. row starts at
14476 negative X position. */
14477 row->x = x - it->first_visible_x;
14478 }
14479 else
14480 {
14481 /* Glyph is completely off the left margin of the
14482 window. This should not happen because of the
14483 move_it_in_display_line at the start of this
14484 function, unless the text display area of the
14485 window is empty. */
14486 xassert (it->first_visible_x <= it->last_visible_x);
14487 }
14488 }
14489
14490 row->ascent = max (row->ascent, it->max_ascent);
14491 row->height = max (row->height, it->max_ascent + it->max_descent);
14492 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14493 row->phys_height = max (row->phys_height,
14494 it->max_phys_ascent + it->max_phys_descent);
14495
14496 /* End of this display line if row is continued. */
14497 if (row->continued_p)
14498 break;
14499 }
14500
14501 /* Is this a line end? If yes, we're also done, after making
14502 sure that a non-default face is extended up to the right
14503 margin of the window. */
14504 if (ITERATOR_AT_END_OF_LINE_P (it))
14505 {
14506 int used_before = row->used[TEXT_AREA];
14507
14508 row->ends_in_newline_from_string_p = STRINGP (it->object);
14509
14510 /* Add a space at the end of the line that is used to
14511 display the cursor there. */
14512 append_space (it, 0);
14513
14514 /* Extend the face to the end of the line. */
14515 extend_face_to_end_of_line (it);
14516
14517 /* Make sure we have the position. */
14518 if (used_before == 0)
14519 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
14520
14521 /* Consume the line end. This skips over invisible lines. */
14522 set_iterator_to_next (it, 1);
14523 it->continuation_lines_width = 0;
14524 break;
14525 }
14526
14527 /* Proceed with next display element. Note that this skips
14528 over lines invisible because of selective display. */
14529 set_iterator_to_next (it, 1);
14530
14531 /* If we truncate lines, we are done when the last displayed
14532 glyphs reach past the right margin of the window. */
14533 if (it->truncate_lines_p
14534 && (FRAME_WINDOW_P (it->f)
14535 ? (it->current_x >= it->last_visible_x)
14536 : (it->current_x > it->last_visible_x)))
14537 {
14538 /* Maybe add truncation glyphs. */
14539 if (!FRAME_WINDOW_P (it->f))
14540 {
14541 int i, n;
14542
14543 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14544 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14545 break;
14546
14547 for (n = row->used[TEXT_AREA]; i < n; ++i)
14548 {
14549 row->used[TEXT_AREA] = i;
14550 produce_special_glyphs (it, IT_TRUNCATION);
14551 }
14552 }
14553
14554 row->truncated_on_right_p = 1;
14555 it->continuation_lines_width = 0;
14556 reseat_at_next_visible_line_start (it, 0);
14557 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
14558 it->hpos = hpos_before;
14559 it->current_x = x_before;
14560 break;
14561 }
14562 }
14563
14564 /* If line is not empty and hscrolled, maybe insert truncation glyphs
14565 at the left window margin. */
14566 if (it->first_visible_x
14567 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
14568 {
14569 if (!FRAME_WINDOW_P (it->f))
14570 insert_left_trunc_glyphs (it);
14571 row->truncated_on_left_p = 1;
14572 }
14573
14574 /* If the start of this line is the overlay arrow-position, then
14575 mark this glyph row as the one containing the overlay arrow.
14576 This is clearly a mess with variable size fonts. It would be
14577 better to let it be displayed like cursors under X. */
14578 if (MARKERP (Voverlay_arrow_position)
14579 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
14580 && (MATRIX_ROW_START_CHARPOS (row)
14581 == marker_position (Voverlay_arrow_position))
14582 && STRINGP (Voverlay_arrow_string)
14583 && ! overlay_arrow_seen)
14584 {
14585 /* Overlay arrow in window redisplay is a fringe bitmap. */
14586 if (!FRAME_WINDOW_P (it->f))
14587 {
14588 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
14589 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
14590 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
14591 struct glyph *p = row->glyphs[TEXT_AREA];
14592 struct glyph *p2, *end;
14593
14594 /* Copy the arrow glyphs. */
14595 while (glyph < arrow_end)
14596 *p++ = *glyph++;
14597
14598 /* Throw away padding glyphs. */
14599 p2 = p;
14600 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
14601 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
14602 ++p2;
14603 if (p2 > p)
14604 {
14605 while (p2 < end)
14606 *p++ = *p2++;
14607 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
14608 }
14609 }
14610
14611 overlay_arrow_seen = 1;
14612 row->overlay_arrow_p = 1;
14613 }
14614
14615 /* Compute pixel dimensions of this line. */
14616 compute_line_metrics (it);
14617
14618 /* Remember the position at which this line ends. */
14619 row->end = it->current;
14620
14621 /* Maybe set the cursor. */
14622 if (it->w->cursor.vpos < 0
14623 && PT >= MATRIX_ROW_START_CHARPOS (row)
14624 && PT <= MATRIX_ROW_END_CHARPOS (row)
14625 && cursor_row_p (it->w, row))
14626 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
14627
14628 /* Highlight trailing whitespace. */
14629 if (!NILP (Vshow_trailing_whitespace))
14630 highlight_trailing_whitespace (it->f, it->glyph_row);
14631
14632 /* Prepare for the next line. This line starts horizontally at (X
14633 HPOS) = (0 0). Vertical positions are incremented. As a
14634 convenience for the caller, IT->glyph_row is set to the next
14635 row to be used. */
14636 it->current_x = it->hpos = 0;
14637 it->current_y += row->height;
14638 ++it->vpos;
14639 ++it->glyph_row;
14640 return row->displays_text_p;
14641 }
14642
14643
14644 \f
14645 /***********************************************************************
14646 Menu Bar
14647 ***********************************************************************/
14648
14649 /* Redisplay the menu bar in the frame for window W.
14650
14651 The menu bar of X frames that don't have X toolkit support is
14652 displayed in a special window W->frame->menu_bar_window.
14653
14654 The menu bar of terminal frames is treated specially as far as
14655 glyph matrices are concerned. Menu bar lines are not part of
14656 windows, so the update is done directly on the frame matrix rows
14657 for the menu bar. */
14658
14659 static void
14660 display_menu_bar (w)
14661 struct window *w;
14662 {
14663 struct frame *f = XFRAME (WINDOW_FRAME (w));
14664 struct it it;
14665 Lisp_Object items;
14666 int i;
14667
14668 /* Don't do all this for graphical frames. */
14669 #ifdef HAVE_NTGUI
14670 if (!NILP (Vwindow_system))
14671 return;
14672 #endif
14673 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
14674 if (FRAME_X_P (f))
14675 return;
14676 #endif
14677 #ifdef MAC_OS
14678 if (FRAME_MAC_P (f))
14679 return;
14680 #endif
14681
14682 #ifdef USE_X_TOOLKIT
14683 xassert (!FRAME_WINDOW_P (f));
14684 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
14685 it.first_visible_x = 0;
14686 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
14687 #else /* not USE_X_TOOLKIT */
14688 if (FRAME_WINDOW_P (f))
14689 {
14690 /* Menu bar lines are displayed in the desired matrix of the
14691 dummy window menu_bar_window. */
14692 struct window *menu_w;
14693 xassert (WINDOWP (f->menu_bar_window));
14694 menu_w = XWINDOW (f->menu_bar_window);
14695 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
14696 MENU_FACE_ID);
14697 it.first_visible_x = 0;
14698 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
14699 }
14700 else
14701 {
14702 /* This is a TTY frame, i.e. character hpos/vpos are used as
14703 pixel x/y. */
14704 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
14705 MENU_FACE_ID);
14706 it.first_visible_x = 0;
14707 it.last_visible_x = FRAME_COLS (f);
14708 }
14709 #endif /* not USE_X_TOOLKIT */
14710
14711 if (! mode_line_inverse_video)
14712 /* Force the menu-bar to be displayed in the default face. */
14713 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
14714
14715 /* Clear all rows of the menu bar. */
14716 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
14717 {
14718 struct glyph_row *row = it.glyph_row + i;
14719 clear_glyph_row (row);
14720 row->enabled_p = 1;
14721 row->full_width_p = 1;
14722 }
14723
14724 /* Display all items of the menu bar. */
14725 items = FRAME_MENU_BAR_ITEMS (it.f);
14726 for (i = 0; i < XVECTOR (items)->size; i += 4)
14727 {
14728 Lisp_Object string;
14729
14730 /* Stop at nil string. */
14731 string = AREF (items, i + 1);
14732 if (NILP (string))
14733 break;
14734
14735 /* Remember where item was displayed. */
14736 AREF (items, i + 3) = make_number (it.hpos);
14737
14738 /* Display the item, pad with one space. */
14739 if (it.current_x < it.last_visible_x)
14740 display_string (NULL, string, Qnil, 0, 0, &it,
14741 SCHARS (string) + 1, 0, 0, -1);
14742 }
14743
14744 /* Fill out the line with spaces. */
14745 if (it.current_x < it.last_visible_x)
14746 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
14747
14748 /* Compute the total height of the lines. */
14749 compute_line_metrics (&it);
14750 }
14751
14752
14753 \f
14754 /***********************************************************************
14755 Mode Line
14756 ***********************************************************************/
14757
14758 /* Redisplay mode lines in the window tree whose root is WINDOW. If
14759 FORCE is non-zero, redisplay mode lines unconditionally.
14760 Otherwise, redisplay only mode lines that are garbaged. Value is
14761 the number of windows whose mode lines were redisplayed. */
14762
14763 static int
14764 redisplay_mode_lines (window, force)
14765 Lisp_Object window;
14766 int force;
14767 {
14768 int nwindows = 0;
14769
14770 while (!NILP (window))
14771 {
14772 struct window *w = XWINDOW (window);
14773
14774 if (WINDOWP (w->hchild))
14775 nwindows += redisplay_mode_lines (w->hchild, force);
14776 else if (WINDOWP (w->vchild))
14777 nwindows += redisplay_mode_lines (w->vchild, force);
14778 else if (force
14779 || FRAME_GARBAGED_P (XFRAME (w->frame))
14780 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
14781 {
14782 struct text_pos lpoint;
14783 struct buffer *old = current_buffer;
14784
14785 /* Set the window's buffer for the mode line display. */
14786 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14787 set_buffer_internal_1 (XBUFFER (w->buffer));
14788
14789 /* Point refers normally to the selected window. For any
14790 other window, set up appropriate value. */
14791 if (!EQ (window, selected_window))
14792 {
14793 struct text_pos pt;
14794
14795 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
14796 if (CHARPOS (pt) < BEGV)
14797 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
14798 else if (CHARPOS (pt) > (ZV - 1))
14799 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
14800 else
14801 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
14802 }
14803
14804 /* Display mode lines. */
14805 clear_glyph_matrix (w->desired_matrix);
14806 if (display_mode_lines (w))
14807 {
14808 ++nwindows;
14809 w->must_be_updated_p = 1;
14810 }
14811
14812 /* Restore old settings. */
14813 set_buffer_internal_1 (old);
14814 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14815 }
14816
14817 window = w->next;
14818 }
14819
14820 return nwindows;
14821 }
14822
14823
14824 /* Display the mode and/or top line of window W. Value is the number
14825 of mode lines displayed. */
14826
14827 static int
14828 display_mode_lines (w)
14829 struct window *w;
14830 {
14831 Lisp_Object old_selected_window, old_selected_frame;
14832 int n = 0;
14833
14834 old_selected_frame = selected_frame;
14835 selected_frame = w->frame;
14836 old_selected_window = selected_window;
14837 XSETWINDOW (selected_window, w);
14838
14839 /* These will be set while the mode line specs are processed. */
14840 line_number_displayed = 0;
14841 w->column_number_displayed = Qnil;
14842
14843 if (WINDOW_WANTS_MODELINE_P (w))
14844 {
14845 struct window *sel_w = XWINDOW (old_selected_window);
14846
14847 /* Select mode line face based on the real selected window. */
14848 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
14849 current_buffer->mode_line_format);
14850 ++n;
14851 }
14852
14853 if (WINDOW_WANTS_HEADER_LINE_P (w))
14854 {
14855 display_mode_line (w, HEADER_LINE_FACE_ID,
14856 current_buffer->header_line_format);
14857 ++n;
14858 }
14859
14860 selected_frame = old_selected_frame;
14861 selected_window = old_selected_window;
14862 return n;
14863 }
14864
14865
14866 /* Display mode or top line of window W. FACE_ID specifies which line
14867 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
14868 FORMAT is the mode line format to display. Value is the pixel
14869 height of the mode line displayed. */
14870
14871 static int
14872 display_mode_line (w, face_id, format)
14873 struct window *w;
14874 enum face_id face_id;
14875 Lisp_Object format;
14876 {
14877 struct it it;
14878 struct face *face;
14879
14880 init_iterator (&it, w, -1, -1, NULL, face_id);
14881 prepare_desired_row (it.glyph_row);
14882
14883 if (! mode_line_inverse_video)
14884 /* Force the mode-line to be displayed in the default face. */
14885 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
14886
14887 /* Temporarily make frame's keyboard the current kboard so that
14888 kboard-local variables in the mode_line_format will get the right
14889 values. */
14890 push_frame_kboard (it.f);
14891 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
14892 pop_frame_kboard ();
14893
14894 /* Fill up with spaces. */
14895 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
14896
14897 compute_line_metrics (&it);
14898 it.glyph_row->full_width_p = 1;
14899 it.glyph_row->mode_line_p = 1;
14900 it.glyph_row->continued_p = 0;
14901 it.glyph_row->truncated_on_left_p = 0;
14902 it.glyph_row->truncated_on_right_p = 0;
14903
14904 /* Make a 3D mode-line have a shadow at its right end. */
14905 face = FACE_FROM_ID (it.f, face_id);
14906 extend_face_to_end_of_line (&it);
14907 if (face->box != FACE_NO_BOX)
14908 {
14909 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
14910 + it.glyph_row->used[TEXT_AREA] - 1);
14911 last->right_box_line_p = 1;
14912 }
14913
14914 return it.glyph_row->height;
14915 }
14916
14917 /* Alist that caches the results of :propertize.
14918 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
14919 Lisp_Object mode_line_proptrans_alist;
14920
14921 /* List of strings making up the mode-line. */
14922 Lisp_Object mode_line_string_list;
14923
14924 /* Base face property when building propertized mode line string. */
14925 static Lisp_Object mode_line_string_face;
14926 static Lisp_Object mode_line_string_face_prop;
14927
14928
14929 /* Contribute ELT to the mode line for window IT->w. How it
14930 translates into text depends on its data type.
14931
14932 IT describes the display environment in which we display, as usual.
14933
14934 DEPTH is the depth in recursion. It is used to prevent
14935 infinite recursion here.
14936
14937 FIELD_WIDTH is the number of characters the display of ELT should
14938 occupy in the mode line, and PRECISION is the maximum number of
14939 characters to display from ELT's representation. See
14940 display_string for details.
14941
14942 Returns the hpos of the end of the text generated by ELT.
14943
14944 PROPS is a property list to add to any string we encounter.
14945
14946 If RISKY is nonzero, remove (disregard) any properties in any string
14947 we encounter, and ignore :eval and :propertize.
14948
14949 If the global variable `frame_title_ptr' is non-NULL, then the output
14950 is passed to `store_frame_title' instead of `display_string'. */
14951
14952 static int
14953 display_mode_element (it, depth, field_width, precision, elt, props, risky)
14954 struct it *it;
14955 int depth;
14956 int field_width, precision;
14957 Lisp_Object elt, props;
14958 int risky;
14959 {
14960 int n = 0, field, prec;
14961 int literal = 0;
14962
14963 tail_recurse:
14964 if (depth > 100)
14965 elt = build_string ("*too-deep*");
14966
14967 depth++;
14968
14969 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
14970 {
14971 case Lisp_String:
14972 {
14973 /* A string: output it and check for %-constructs within it. */
14974 unsigned char c;
14975 const unsigned char *this, *lisp_string;
14976
14977 if (!NILP (props) || risky)
14978 {
14979 Lisp_Object oprops, aelt;
14980 oprops = Ftext_properties_at (make_number (0), elt);
14981
14982 if (NILP (Fequal (props, oprops)) || risky)
14983 {
14984 /* If the starting string has properties,
14985 merge the specified ones onto the existing ones. */
14986 if (! NILP (oprops) && !risky)
14987 {
14988 Lisp_Object tem;
14989
14990 oprops = Fcopy_sequence (oprops);
14991 tem = props;
14992 while (CONSP (tem))
14993 {
14994 oprops = Fplist_put (oprops, XCAR (tem),
14995 XCAR (XCDR (tem)));
14996 tem = XCDR (XCDR (tem));
14997 }
14998 props = oprops;
14999 }
15000
15001 aelt = Fassoc (elt, mode_line_proptrans_alist);
15002 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15003 {
15004 mode_line_proptrans_alist
15005 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15006 elt = XCAR (aelt);
15007 }
15008 else
15009 {
15010 Lisp_Object tem;
15011
15012 elt = Fcopy_sequence (elt);
15013 Fset_text_properties (make_number (0), Flength (elt),
15014 props, elt);
15015 /* Add this item to mode_line_proptrans_alist. */
15016 mode_line_proptrans_alist
15017 = Fcons (Fcons (elt, props),
15018 mode_line_proptrans_alist);
15019 /* Truncate mode_line_proptrans_alist
15020 to at most 50 elements. */
15021 tem = Fnthcdr (make_number (50),
15022 mode_line_proptrans_alist);
15023 if (! NILP (tem))
15024 XSETCDR (tem, Qnil);
15025 }
15026 }
15027 }
15028
15029 this = SDATA (elt);
15030 lisp_string = this;
15031
15032 if (literal)
15033 {
15034 prec = precision - n;
15035 if (frame_title_ptr)
15036 n += store_frame_title (SDATA (elt), -1, prec);
15037 else if (!NILP (mode_line_string_list))
15038 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15039 else
15040 n += display_string (NULL, elt, Qnil, 0, 0, it,
15041 0, prec, 0, STRING_MULTIBYTE (elt));
15042
15043 break;
15044 }
15045
15046 while ((precision <= 0 || n < precision)
15047 && *this
15048 && (frame_title_ptr
15049 || !NILP (mode_line_string_list)
15050 || it->current_x < it->last_visible_x))
15051 {
15052 const unsigned char *last = this;
15053
15054 /* Advance to end of string or next format specifier. */
15055 while ((c = *this++) != '\0' && c != '%')
15056 ;
15057
15058 if (this - 1 != last)
15059 {
15060 /* Output to end of string or up to '%'. Field width
15061 is length of string. Don't output more than
15062 PRECISION allows us. */
15063 --this;
15064
15065 prec = chars_in_text (last, this - last);
15066 if (precision > 0 && prec > precision - n)
15067 prec = precision - n;
15068
15069 if (frame_title_ptr)
15070 n += store_frame_title (last, 0, prec);
15071 else if (!NILP (mode_line_string_list))
15072 {
15073 int bytepos = last - lisp_string;
15074 int charpos = string_byte_to_char (elt, bytepos);
15075 n += store_mode_line_string (NULL,
15076 Fsubstring (elt, make_number (charpos),
15077 make_number (charpos + prec)),
15078 0, 0, 0, Qnil);
15079 }
15080 else
15081 {
15082 int bytepos = last - lisp_string;
15083 int charpos = string_byte_to_char (elt, bytepos);
15084 n += display_string (NULL, elt, Qnil, 0, charpos,
15085 it, 0, prec, 0,
15086 STRING_MULTIBYTE (elt));
15087 }
15088 }
15089 else /* c == '%' */
15090 {
15091 const unsigned char *percent_position = this;
15092
15093 /* Get the specified minimum width. Zero means
15094 don't pad. */
15095 field = 0;
15096 while ((c = *this++) >= '0' && c <= '9')
15097 field = field * 10 + c - '0';
15098
15099 /* Don't pad beyond the total padding allowed. */
15100 if (field_width - n > 0 && field > field_width - n)
15101 field = field_width - n;
15102
15103 /* Note that either PRECISION <= 0 or N < PRECISION. */
15104 prec = precision - n;
15105
15106 if (c == 'M')
15107 n += display_mode_element (it, depth, field, prec,
15108 Vglobal_mode_string, props,
15109 risky);
15110 else if (c != 0)
15111 {
15112 int multibyte;
15113 int bytepos, charpos;
15114 unsigned char *spec;
15115
15116 bytepos = percent_position - lisp_string;
15117 charpos = (STRING_MULTIBYTE (elt)
15118 ? string_byte_to_char (elt, bytepos)
15119 : bytepos);
15120
15121 spec
15122 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15123
15124 if (frame_title_ptr)
15125 n += store_frame_title (spec, field, prec);
15126 else if (!NILP (mode_line_string_list))
15127 {
15128 int len = strlen (spec);
15129 Lisp_Object tem = make_string (spec, len);
15130 props = Ftext_properties_at (make_number (charpos), elt);
15131 /* Should only keep face property in props */
15132 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15133 }
15134 else
15135 {
15136 int nglyphs_before, nwritten;
15137
15138 nglyphs_before = it->glyph_row->used[TEXT_AREA];
15139 nwritten = display_string (spec, Qnil, elt,
15140 charpos, 0, it,
15141 field, prec, 0,
15142 multibyte);
15143
15144 /* Assign to the glyphs written above the
15145 string where the `%x' came from, position
15146 of the `%'. */
15147 if (nwritten > 0)
15148 {
15149 struct glyph *glyph
15150 = (it->glyph_row->glyphs[TEXT_AREA]
15151 + nglyphs_before);
15152 int i;
15153
15154 for (i = 0; i < nwritten; ++i)
15155 {
15156 glyph[i].object = elt;
15157 glyph[i].charpos = charpos;
15158 }
15159
15160 n += nwritten;
15161 }
15162 }
15163 }
15164 else /* c == 0 */
15165 break;
15166 }
15167 }
15168 }
15169 break;
15170
15171 case Lisp_Symbol:
15172 /* A symbol: process the value of the symbol recursively
15173 as if it appeared here directly. Avoid error if symbol void.
15174 Special case: if value of symbol is a string, output the string
15175 literally. */
15176 {
15177 register Lisp_Object tem;
15178
15179 /* If the variable is not marked as risky to set
15180 then its contents are risky to use. */
15181 if (NILP (Fget (elt, Qrisky_local_variable)))
15182 risky = 1;
15183
15184 tem = Fboundp (elt);
15185 if (!NILP (tem))
15186 {
15187 tem = Fsymbol_value (elt);
15188 /* If value is a string, output that string literally:
15189 don't check for % within it. */
15190 if (STRINGP (tem))
15191 literal = 1;
15192
15193 if (!EQ (tem, elt))
15194 {
15195 /* Give up right away for nil or t. */
15196 elt = tem;
15197 goto tail_recurse;
15198 }
15199 }
15200 }
15201 break;
15202
15203 case Lisp_Cons:
15204 {
15205 register Lisp_Object car, tem;
15206
15207 /* A cons cell: five distinct cases.
15208 If first element is :eval or :propertize, do something special.
15209 If first element is a string or a cons, process all the elements
15210 and effectively concatenate them.
15211 If first element is a negative number, truncate displaying cdr to
15212 at most that many characters. If positive, pad (with spaces)
15213 to at least that many characters.
15214 If first element is a symbol, process the cadr or caddr recursively
15215 according to whether the symbol's value is non-nil or nil. */
15216 car = XCAR (elt);
15217 if (EQ (car, QCeval))
15218 {
15219 /* An element of the form (:eval FORM) means evaluate FORM
15220 and use the result as mode line elements. */
15221
15222 if (risky)
15223 break;
15224
15225 if (CONSP (XCDR (elt)))
15226 {
15227 Lisp_Object spec;
15228 spec = safe_eval (XCAR (XCDR (elt)));
15229 n += display_mode_element (it, depth, field_width - n,
15230 precision - n, spec, props,
15231 risky);
15232 }
15233 }
15234 else if (EQ (car, QCpropertize))
15235 {
15236 /* An element of the form (:propertize ELT PROPS...)
15237 means display ELT but applying properties PROPS. */
15238
15239 if (risky)
15240 break;
15241
15242 if (CONSP (XCDR (elt)))
15243 n += display_mode_element (it, depth, field_width - n,
15244 precision - n, XCAR (XCDR (elt)),
15245 XCDR (XCDR (elt)), risky);
15246 }
15247 else if (SYMBOLP (car))
15248 {
15249 tem = Fboundp (car);
15250 elt = XCDR (elt);
15251 if (!CONSP (elt))
15252 goto invalid;
15253 /* elt is now the cdr, and we know it is a cons cell.
15254 Use its car if CAR has a non-nil value. */
15255 if (!NILP (tem))
15256 {
15257 tem = Fsymbol_value (car);
15258 if (!NILP (tem))
15259 {
15260 elt = XCAR (elt);
15261 goto tail_recurse;
15262 }
15263 }
15264 /* Symbol's value is nil (or symbol is unbound)
15265 Get the cddr of the original list
15266 and if possible find the caddr and use that. */
15267 elt = XCDR (elt);
15268 if (NILP (elt))
15269 break;
15270 else if (!CONSP (elt))
15271 goto invalid;
15272 elt = XCAR (elt);
15273 goto tail_recurse;
15274 }
15275 else if (INTEGERP (car))
15276 {
15277 register int lim = XINT (car);
15278 elt = XCDR (elt);
15279 if (lim < 0)
15280 {
15281 /* Negative int means reduce maximum width. */
15282 if (precision <= 0)
15283 precision = -lim;
15284 else
15285 precision = min (precision, -lim);
15286 }
15287 else if (lim > 0)
15288 {
15289 /* Padding specified. Don't let it be more than
15290 current maximum. */
15291 if (precision > 0)
15292 lim = min (precision, lim);
15293
15294 /* If that's more padding than already wanted, queue it.
15295 But don't reduce padding already specified even if
15296 that is beyond the current truncation point. */
15297 field_width = max (lim, field_width);
15298 }
15299 goto tail_recurse;
15300 }
15301 else if (STRINGP (car) || CONSP (car))
15302 {
15303 register int limit = 50;
15304 /* Limit is to protect against circular lists. */
15305 while (CONSP (elt)
15306 && --limit > 0
15307 && (precision <= 0 || n < precision))
15308 {
15309 n += display_mode_element (it, depth, field_width - n,
15310 precision - n, XCAR (elt),
15311 props, risky);
15312 elt = XCDR (elt);
15313 }
15314 }
15315 }
15316 break;
15317
15318 default:
15319 invalid:
15320 elt = build_string ("*invalid*");
15321 goto tail_recurse;
15322 }
15323
15324 /* Pad to FIELD_WIDTH. */
15325 if (field_width > 0 && n < field_width)
15326 {
15327 if (frame_title_ptr)
15328 n += store_frame_title ("", field_width - n, 0);
15329 else if (!NILP (mode_line_string_list))
15330 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
15331 else
15332 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
15333 0, 0, 0);
15334 }
15335
15336 return n;
15337 }
15338
15339 /* Store a mode-line string element in mode_line_string_list.
15340
15341 If STRING is non-null, display that C string. Otherwise, the Lisp
15342 string LISP_STRING is displayed.
15343
15344 FIELD_WIDTH is the minimum number of output glyphs to produce.
15345 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15346 with spaces. FIELD_WIDTH <= 0 means don't pad.
15347
15348 PRECISION is the maximum number of characters to output from
15349 STRING. PRECISION <= 0 means don't truncate the string.
15350
15351 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
15352 properties to the string.
15353
15354 PROPS are the properties to add to the string.
15355 The mode_line_string_face face property is always added to the string.
15356 */
15357
15358 static int store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
15359 char *string;
15360 Lisp_Object lisp_string;
15361 int copy_string;
15362 int field_width;
15363 int precision;
15364 Lisp_Object props;
15365 {
15366 int len;
15367 int n = 0;
15368
15369 if (string != NULL)
15370 {
15371 len = strlen (string);
15372 if (precision > 0 && len > precision)
15373 len = precision;
15374 lisp_string = make_string (string, len);
15375 if (NILP (props))
15376 props = mode_line_string_face_prop;
15377 else if (!NILP (mode_line_string_face))
15378 {
15379 Lisp_Object face = Fplist_get (props, Qface);
15380 props = Fcopy_sequence (props);
15381 if (NILP (face))
15382 face = mode_line_string_face;
15383 else
15384 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15385 props = Fplist_put (props, Qface, face);
15386 }
15387 Fadd_text_properties (make_number (0), make_number (len),
15388 props, lisp_string);
15389 }
15390 else
15391 {
15392 len = XFASTINT (Flength (lisp_string));
15393 if (precision > 0 && len > precision)
15394 {
15395 len = precision;
15396 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
15397 precision = -1;
15398 }
15399 if (!NILP (mode_line_string_face))
15400 {
15401 Lisp_Object face;
15402 if (NILP (props))
15403 props = Ftext_properties_at (make_number (0), lisp_string);
15404 face = Fplist_get (props, Qface);
15405 if (NILP (face))
15406 face = mode_line_string_face;
15407 else
15408 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15409 props = Fcons (Qface, Fcons (face, Qnil));
15410 if (copy_string)
15411 lisp_string = Fcopy_sequence (lisp_string);
15412 }
15413 if (!NILP (props))
15414 Fadd_text_properties (make_number (0), make_number (len),
15415 props, lisp_string);
15416 }
15417
15418 if (len > 0)
15419 {
15420 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15421 n += len;
15422 }
15423
15424 if (field_width > len)
15425 {
15426 field_width -= len;
15427 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
15428 if (!NILP (props))
15429 Fadd_text_properties (make_number (0), make_number (field_width),
15430 props, lisp_string);
15431 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15432 n += field_width;
15433 }
15434
15435 return n;
15436 }
15437
15438
15439 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
15440 0, 3, 0,
15441 doc: /* Return the mode-line of selected window as a string.
15442 First optional arg FORMAT specifies a different format string (see
15443 `mode-line-format' for details) to use. If FORMAT is t, return
15444 the buffer's header-line. Second optional arg WINDOW specifies a
15445 different window to use as the context for the formatting.
15446 If third optional arg NO-PROPS is non-nil, string is not propertized. */)
15447 (format, window, no_props)
15448 Lisp_Object format, window, no_props;
15449 {
15450 struct it it;
15451 int len;
15452 struct window *w;
15453 struct buffer *old_buffer = NULL;
15454 enum face_id face_id = DEFAULT_FACE_ID;
15455
15456 if (NILP (window))
15457 window = selected_window;
15458 CHECK_WINDOW (window);
15459 w = XWINDOW (window);
15460 CHECK_BUFFER (w->buffer);
15461
15462 if (XBUFFER (w->buffer) != current_buffer)
15463 {
15464 old_buffer = current_buffer;
15465 set_buffer_internal_1 (XBUFFER (w->buffer));
15466 }
15467
15468 if (NILP (format) || EQ (format, Qt))
15469 {
15470 face_id = NILP (format)
15471 ? CURRENT_MODE_LINE_FACE_ID (w) :
15472 HEADER_LINE_FACE_ID;
15473 format = NILP (format)
15474 ? current_buffer->mode_line_format
15475 : current_buffer->header_line_format;
15476 }
15477
15478 init_iterator (&it, w, -1, -1, NULL, face_id);
15479
15480 if (NILP (no_props))
15481 {
15482 mode_line_string_face =
15483 (face_id == MODE_LINE_FACE_ID ? Qmode_line :
15484 face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive :
15485 face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
15486
15487 mode_line_string_face_prop =
15488 NILP (mode_line_string_face) ? Qnil :
15489 Fcons (Qface, Fcons (mode_line_string_face, Qnil));
15490
15491 /* We need a dummy last element in mode_line_string_list to
15492 indicate we are building the propertized mode-line string.
15493 Using mode_line_string_face_prop here GC protects it. */
15494 mode_line_string_list =
15495 Fcons (mode_line_string_face_prop, Qnil);
15496 frame_title_ptr = NULL;
15497 }
15498 else
15499 {
15500 mode_line_string_face_prop = Qnil;
15501 mode_line_string_list = Qnil;
15502 frame_title_ptr = frame_title_buf;
15503 }
15504
15505 push_frame_kboard (it.f);
15506 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15507 pop_frame_kboard ();
15508
15509 if (old_buffer)
15510 set_buffer_internal_1 (old_buffer);
15511
15512 if (NILP (no_props))
15513 {
15514 Lisp_Object str;
15515 mode_line_string_list = Fnreverse (mode_line_string_list);
15516 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
15517 make_string ("", 0));
15518 mode_line_string_face_prop = Qnil;
15519 mode_line_string_list = Qnil;
15520 return str;
15521 }
15522
15523 len = frame_title_ptr - frame_title_buf;
15524 if (len > 0 && frame_title_ptr[-1] == '-')
15525 {
15526 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
15527 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
15528 ;
15529 frame_title_ptr += 3; /* restore last non-dash + two dashes */
15530 if (len > frame_title_ptr - frame_title_buf)
15531 len = frame_title_ptr - frame_title_buf;
15532 }
15533
15534 frame_title_ptr = NULL;
15535 return make_string (frame_title_buf, len);
15536 }
15537
15538 /* Write a null-terminated, right justified decimal representation of
15539 the positive integer D to BUF using a minimal field width WIDTH. */
15540
15541 static void
15542 pint2str (buf, width, d)
15543 register char *buf;
15544 register int width;
15545 register int d;
15546 {
15547 register char *p = buf;
15548
15549 if (d <= 0)
15550 *p++ = '0';
15551 else
15552 {
15553 while (d > 0)
15554 {
15555 *p++ = d % 10 + '0';
15556 d /= 10;
15557 }
15558 }
15559
15560 for (width -= (int) (p - buf); width > 0; --width)
15561 *p++ = ' ';
15562 *p-- = '\0';
15563 while (p > buf)
15564 {
15565 d = *buf;
15566 *buf++ = *p;
15567 *p-- = d;
15568 }
15569 }
15570
15571 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
15572 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
15573 type of CODING_SYSTEM. Return updated pointer into BUF. */
15574
15575 static unsigned char invalid_eol_type[] = "(*invalid*)";
15576
15577 static char *
15578 decode_mode_spec_coding (coding_system, buf, eol_flag)
15579 Lisp_Object coding_system;
15580 register char *buf;
15581 int eol_flag;
15582 {
15583 Lisp_Object val;
15584 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
15585 const unsigned char *eol_str;
15586 int eol_str_len;
15587 /* The EOL conversion we are using. */
15588 Lisp_Object eoltype;
15589
15590 val = Fget (coding_system, Qcoding_system);
15591 eoltype = Qnil;
15592
15593 if (!VECTORP (val)) /* Not yet decided. */
15594 {
15595 if (multibyte)
15596 *buf++ = '-';
15597 if (eol_flag)
15598 eoltype = eol_mnemonic_undecided;
15599 /* Don't mention EOL conversion if it isn't decided. */
15600 }
15601 else
15602 {
15603 Lisp_Object eolvalue;
15604
15605 eolvalue = Fget (coding_system, Qeol_type);
15606
15607 if (multibyte)
15608 *buf++ = XFASTINT (AREF (val, 1));
15609
15610 if (eol_flag)
15611 {
15612 /* The EOL conversion that is normal on this system. */
15613
15614 if (NILP (eolvalue)) /* Not yet decided. */
15615 eoltype = eol_mnemonic_undecided;
15616 else if (VECTORP (eolvalue)) /* Not yet decided. */
15617 eoltype = eol_mnemonic_undecided;
15618 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
15619 eoltype = (XFASTINT (eolvalue) == 0
15620 ? eol_mnemonic_unix
15621 : (XFASTINT (eolvalue) == 1
15622 ? eol_mnemonic_dos : eol_mnemonic_mac));
15623 }
15624 }
15625
15626 if (eol_flag)
15627 {
15628 /* Mention the EOL conversion if it is not the usual one. */
15629 if (STRINGP (eoltype))
15630 {
15631 eol_str = SDATA (eoltype);
15632 eol_str_len = SBYTES (eoltype);
15633 }
15634 else if (INTEGERP (eoltype)
15635 && CHAR_VALID_P (XINT (eoltype), 0))
15636 {
15637 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
15638 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
15639 eol_str = tmp;
15640 }
15641 else
15642 {
15643 eol_str = invalid_eol_type;
15644 eol_str_len = sizeof (invalid_eol_type) - 1;
15645 }
15646 bcopy (eol_str, buf, eol_str_len);
15647 buf += eol_str_len;
15648 }
15649
15650 return buf;
15651 }
15652
15653 /* Return a string for the output of a mode line %-spec for window W,
15654 generated by character C. PRECISION >= 0 means don't return a
15655 string longer than that value. FIELD_WIDTH > 0 means pad the
15656 string returned with spaces to that value. Return 1 in *MULTIBYTE
15657 if the result is multibyte text. */
15658
15659 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
15660
15661 static char *
15662 decode_mode_spec (w, c, field_width, precision, multibyte)
15663 struct window *w;
15664 register int c;
15665 int field_width, precision;
15666 int *multibyte;
15667 {
15668 Lisp_Object obj;
15669 struct frame *f = XFRAME (WINDOW_FRAME (w));
15670 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
15671 struct buffer *b = XBUFFER (w->buffer);
15672
15673 obj = Qnil;
15674 *multibyte = 0;
15675
15676 switch (c)
15677 {
15678 case '*':
15679 if (!NILP (b->read_only))
15680 return "%";
15681 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
15682 return "*";
15683 return "-";
15684
15685 case '+':
15686 /* This differs from %* only for a modified read-only buffer. */
15687 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
15688 return "*";
15689 if (!NILP (b->read_only))
15690 return "%";
15691 return "-";
15692
15693 case '&':
15694 /* This differs from %* in ignoring read-only-ness. */
15695 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
15696 return "*";
15697 return "-";
15698
15699 case '%':
15700 return "%";
15701
15702 case '[':
15703 {
15704 int i;
15705 char *p;
15706
15707 if (command_loop_level > 5)
15708 return "[[[... ";
15709 p = decode_mode_spec_buf;
15710 for (i = 0; i < command_loop_level; i++)
15711 *p++ = '[';
15712 *p = 0;
15713 return decode_mode_spec_buf;
15714 }
15715
15716 case ']':
15717 {
15718 int i;
15719 char *p;
15720
15721 if (command_loop_level > 5)
15722 return " ...]]]";
15723 p = decode_mode_spec_buf;
15724 for (i = 0; i < command_loop_level; i++)
15725 *p++ = ']';
15726 *p = 0;
15727 return decode_mode_spec_buf;
15728 }
15729
15730 case '-':
15731 {
15732 register int i;
15733
15734 /* Let lots_of_dashes be a string of infinite length. */
15735 if (!NILP (mode_line_string_list))
15736 return "--";
15737 if (field_width <= 0
15738 || field_width > sizeof (lots_of_dashes))
15739 {
15740 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
15741 decode_mode_spec_buf[i] = '-';
15742 decode_mode_spec_buf[i] = '\0';
15743 return decode_mode_spec_buf;
15744 }
15745 else
15746 return lots_of_dashes;
15747 }
15748
15749 case 'b':
15750 obj = b->name;
15751 break;
15752
15753 case 'c':
15754 {
15755 int col = (int) current_column (); /* iftc */
15756 w->column_number_displayed = make_number (col);
15757 pint2str (decode_mode_spec_buf, field_width, col);
15758 return decode_mode_spec_buf;
15759 }
15760
15761 case 'F':
15762 /* %F displays the frame name. */
15763 if (!NILP (f->title))
15764 return (char *) SDATA (f->title);
15765 if (f->explicit_name || ! FRAME_WINDOW_P (f))
15766 return (char *) SDATA (f->name);
15767 return "Emacs";
15768
15769 case 'f':
15770 obj = b->filename;
15771 break;
15772
15773 case 'l':
15774 {
15775 int startpos = XMARKER (w->start)->charpos;
15776 int startpos_byte = marker_byte_position (w->start);
15777 int line, linepos, linepos_byte, topline;
15778 int nlines, junk;
15779 int height = WINDOW_TOTAL_LINES (w);
15780
15781 /* If we decided that this buffer isn't suitable for line numbers,
15782 don't forget that too fast. */
15783 if (EQ (w->base_line_pos, w->buffer))
15784 goto no_value;
15785 /* But do forget it, if the window shows a different buffer now. */
15786 else if (BUFFERP (w->base_line_pos))
15787 w->base_line_pos = Qnil;
15788
15789 /* If the buffer is very big, don't waste time. */
15790 if (INTEGERP (Vline_number_display_limit)
15791 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
15792 {
15793 w->base_line_pos = Qnil;
15794 w->base_line_number = Qnil;
15795 goto no_value;
15796 }
15797
15798 if (!NILP (w->base_line_number)
15799 && !NILP (w->base_line_pos)
15800 && XFASTINT (w->base_line_pos) <= startpos)
15801 {
15802 line = XFASTINT (w->base_line_number);
15803 linepos = XFASTINT (w->base_line_pos);
15804 linepos_byte = buf_charpos_to_bytepos (b, linepos);
15805 }
15806 else
15807 {
15808 line = 1;
15809 linepos = BUF_BEGV (b);
15810 linepos_byte = BUF_BEGV_BYTE (b);
15811 }
15812
15813 /* Count lines from base line to window start position. */
15814 nlines = display_count_lines (linepos, linepos_byte,
15815 startpos_byte,
15816 startpos, &junk);
15817
15818 topline = nlines + line;
15819
15820 /* Determine a new base line, if the old one is too close
15821 or too far away, or if we did not have one.
15822 "Too close" means it's plausible a scroll-down would
15823 go back past it. */
15824 if (startpos == BUF_BEGV (b))
15825 {
15826 w->base_line_number = make_number (topline);
15827 w->base_line_pos = make_number (BUF_BEGV (b));
15828 }
15829 else if (nlines < height + 25 || nlines > height * 3 + 50
15830 || linepos == BUF_BEGV (b))
15831 {
15832 int limit = BUF_BEGV (b);
15833 int limit_byte = BUF_BEGV_BYTE (b);
15834 int position;
15835 int distance = (height * 2 + 30) * line_number_display_limit_width;
15836
15837 if (startpos - distance > limit)
15838 {
15839 limit = startpos - distance;
15840 limit_byte = CHAR_TO_BYTE (limit);
15841 }
15842
15843 nlines = display_count_lines (startpos, startpos_byte,
15844 limit_byte,
15845 - (height * 2 + 30),
15846 &position);
15847 /* If we couldn't find the lines we wanted within
15848 line_number_display_limit_width chars per line,
15849 give up on line numbers for this window. */
15850 if (position == limit_byte && limit == startpos - distance)
15851 {
15852 w->base_line_pos = w->buffer;
15853 w->base_line_number = Qnil;
15854 goto no_value;
15855 }
15856
15857 w->base_line_number = make_number (topline - nlines);
15858 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
15859 }
15860
15861 /* Now count lines from the start pos to point. */
15862 nlines = display_count_lines (startpos, startpos_byte,
15863 PT_BYTE, PT, &junk);
15864
15865 /* Record that we did display the line number. */
15866 line_number_displayed = 1;
15867
15868 /* Make the string to show. */
15869 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
15870 return decode_mode_spec_buf;
15871 no_value:
15872 {
15873 char* p = decode_mode_spec_buf;
15874 int pad = field_width - 2;
15875 while (pad-- > 0)
15876 *p++ = ' ';
15877 *p++ = '?';
15878 *p++ = '?';
15879 *p = '\0';
15880 return decode_mode_spec_buf;
15881 }
15882 }
15883 break;
15884
15885 case 'm':
15886 obj = b->mode_name;
15887 break;
15888
15889 case 'n':
15890 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
15891 return " Narrow";
15892 break;
15893
15894 case 'p':
15895 {
15896 int pos = marker_position (w->start);
15897 int total = BUF_ZV (b) - BUF_BEGV (b);
15898
15899 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
15900 {
15901 if (pos <= BUF_BEGV (b))
15902 return "All";
15903 else
15904 return "Bottom";
15905 }
15906 else if (pos <= BUF_BEGV (b))
15907 return "Top";
15908 else
15909 {
15910 if (total > 1000000)
15911 /* Do it differently for a large value, to avoid overflow. */
15912 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
15913 else
15914 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
15915 /* We can't normally display a 3-digit number,
15916 so get us a 2-digit number that is close. */
15917 if (total == 100)
15918 total = 99;
15919 sprintf (decode_mode_spec_buf, "%2d%%", total);
15920 return decode_mode_spec_buf;
15921 }
15922 }
15923
15924 /* Display percentage of size above the bottom of the screen. */
15925 case 'P':
15926 {
15927 int toppos = marker_position (w->start);
15928 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
15929 int total = BUF_ZV (b) - BUF_BEGV (b);
15930
15931 if (botpos >= BUF_ZV (b))
15932 {
15933 if (toppos <= BUF_BEGV (b))
15934 return "All";
15935 else
15936 return "Bottom";
15937 }
15938 else
15939 {
15940 if (total > 1000000)
15941 /* Do it differently for a large value, to avoid overflow. */
15942 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
15943 else
15944 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
15945 /* We can't normally display a 3-digit number,
15946 so get us a 2-digit number that is close. */
15947 if (total == 100)
15948 total = 99;
15949 if (toppos <= BUF_BEGV (b))
15950 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
15951 else
15952 sprintf (decode_mode_spec_buf, "%2d%%", total);
15953 return decode_mode_spec_buf;
15954 }
15955 }
15956
15957 case 's':
15958 /* status of process */
15959 obj = Fget_buffer_process (w->buffer);
15960 if (NILP (obj))
15961 return "no process";
15962 #ifdef subprocesses
15963 obj = Fsymbol_name (Fprocess_status (obj));
15964 #endif
15965 break;
15966
15967 case 't': /* indicate TEXT or BINARY */
15968 #ifdef MODE_LINE_BINARY_TEXT
15969 return MODE_LINE_BINARY_TEXT (b);
15970 #else
15971 return "T";
15972 #endif
15973
15974 case 'z':
15975 /* coding-system (not including end-of-line format) */
15976 case 'Z':
15977 /* coding-system (including end-of-line type) */
15978 {
15979 int eol_flag = (c == 'Z');
15980 char *p = decode_mode_spec_buf;
15981
15982 if (! FRAME_WINDOW_P (f))
15983 {
15984 /* No need to mention EOL here--the terminal never needs
15985 to do EOL conversion. */
15986 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
15987 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
15988 }
15989 p = decode_mode_spec_coding (b->buffer_file_coding_system,
15990 p, eol_flag);
15991
15992 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
15993 #ifdef subprocesses
15994 obj = Fget_buffer_process (Fcurrent_buffer ());
15995 if (PROCESSP (obj))
15996 {
15997 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
15998 p, eol_flag);
15999 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16000 p, eol_flag);
16001 }
16002 #endif /* subprocesses */
16003 #endif /* 0 */
16004 *p = 0;
16005 return decode_mode_spec_buf;
16006 }
16007 }
16008
16009 if (STRINGP (obj))
16010 {
16011 *multibyte = STRING_MULTIBYTE (obj);
16012 return (char *) SDATA (obj);
16013 }
16014 else
16015 return "";
16016 }
16017
16018
16019 /* Count up to COUNT lines starting from START / START_BYTE.
16020 But don't go beyond LIMIT_BYTE.
16021 Return the number of lines thus found (always nonnegative).
16022
16023 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
16024
16025 static int
16026 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16027 int start, start_byte, limit_byte, count;
16028 int *byte_pos_ptr;
16029 {
16030 register unsigned char *cursor;
16031 unsigned char *base;
16032
16033 register int ceiling;
16034 register unsigned char *ceiling_addr;
16035 int orig_count = count;
16036
16037 /* If we are not in selective display mode,
16038 check only for newlines. */
16039 int selective_display = (!NILP (current_buffer->selective_display)
16040 && !INTEGERP (current_buffer->selective_display));
16041
16042 if (count > 0)
16043 {
16044 while (start_byte < limit_byte)
16045 {
16046 ceiling = BUFFER_CEILING_OF (start_byte);
16047 ceiling = min (limit_byte - 1, ceiling);
16048 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16049 base = (cursor = BYTE_POS_ADDR (start_byte));
16050 while (1)
16051 {
16052 if (selective_display)
16053 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16054 ;
16055 else
16056 while (*cursor != '\n' && ++cursor != ceiling_addr)
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 return orig_count;
16066 }
16067 else
16068 if (++cursor == ceiling_addr)
16069 break;
16070 }
16071 else
16072 break;
16073 }
16074 start_byte += cursor - base;
16075 }
16076 }
16077 else
16078 {
16079 while (start_byte > limit_byte)
16080 {
16081 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16082 ceiling = max (limit_byte, ceiling);
16083 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16084 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16085 while (1)
16086 {
16087 if (selective_display)
16088 while (--cursor != ceiling_addr
16089 && *cursor != '\n' && *cursor != 015)
16090 ;
16091 else
16092 while (--cursor != ceiling_addr && *cursor != '\n')
16093 ;
16094
16095 if (cursor != ceiling_addr)
16096 {
16097 if (++count == 0)
16098 {
16099 start_byte += cursor - base + 1;
16100 *byte_pos_ptr = start_byte;
16101 /* When scanning backwards, we should
16102 not count the newline posterior to which we stop. */
16103 return - orig_count - 1;
16104 }
16105 }
16106 else
16107 break;
16108 }
16109 /* Here we add 1 to compensate for the last decrement
16110 of CURSOR, which took it past the valid range. */
16111 start_byte += cursor - base + 1;
16112 }
16113 }
16114
16115 *byte_pos_ptr = limit_byte;
16116
16117 if (count < 0)
16118 return - orig_count + count;
16119 return orig_count - count;
16120
16121 }
16122
16123
16124 \f
16125 /***********************************************************************
16126 Displaying strings
16127 ***********************************************************************/
16128
16129 /* Display a NUL-terminated string, starting with index START.
16130
16131 If STRING is non-null, display that C string. Otherwise, the Lisp
16132 string LISP_STRING is displayed.
16133
16134 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16135 FACE_STRING. Display STRING or LISP_STRING with the face at
16136 FACE_STRING_POS in FACE_STRING:
16137
16138 Display the string in the environment given by IT, but use the
16139 standard display table, temporarily.
16140
16141 FIELD_WIDTH is the minimum number of output glyphs to produce.
16142 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16143 with spaces. If STRING has more characters, more than FIELD_WIDTH
16144 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
16145
16146 PRECISION is the maximum number of characters to output from
16147 STRING. PRECISION < 0 means don't truncate the string.
16148
16149 This is roughly equivalent to printf format specifiers:
16150
16151 FIELD_WIDTH PRECISION PRINTF
16152 ----------------------------------------
16153 -1 -1 %s
16154 -1 10 %.10s
16155 10 -1 %10s
16156 20 10 %20.10s
16157
16158 MULTIBYTE zero means do not display multibyte chars, > 0 means do
16159 display them, and < 0 means obey the current buffer's value of
16160 enable_multibyte_characters.
16161
16162 Value is the number of glyphs produced. */
16163
16164 static int
16165 display_string (string, lisp_string, face_string, face_string_pos,
16166 start, it, field_width, precision, max_x, multibyte)
16167 unsigned char *string;
16168 Lisp_Object lisp_string;
16169 Lisp_Object face_string;
16170 int face_string_pos;
16171 int start;
16172 struct it *it;
16173 int field_width, precision, max_x;
16174 int multibyte;
16175 {
16176 int hpos_at_start = it->hpos;
16177 int saved_face_id = it->face_id;
16178 struct glyph_row *row = it->glyph_row;
16179
16180 /* Initialize the iterator IT for iteration over STRING beginning
16181 with index START. */
16182 reseat_to_string (it, string, lisp_string, start,
16183 precision, field_width, multibyte);
16184
16185 /* If displaying STRING, set up the face of the iterator
16186 from LISP_STRING, if that's given. */
16187 if (STRINGP (face_string))
16188 {
16189 int endptr;
16190 struct face *face;
16191
16192 it->face_id
16193 = face_at_string_position (it->w, face_string, face_string_pos,
16194 0, it->region_beg_charpos,
16195 it->region_end_charpos,
16196 &endptr, it->base_face_id, 0);
16197 face = FACE_FROM_ID (it->f, it->face_id);
16198 it->face_box_p = face->box != FACE_NO_BOX;
16199 }
16200
16201 /* Set max_x to the maximum allowed X position. Don't let it go
16202 beyond the right edge of the window. */
16203 if (max_x <= 0)
16204 max_x = it->last_visible_x;
16205 else
16206 max_x = min (max_x, it->last_visible_x);
16207
16208 /* Skip over display elements that are not visible. because IT->w is
16209 hscrolled. */
16210 if (it->current_x < it->first_visible_x)
16211 move_it_in_display_line_to (it, 100000, it->first_visible_x,
16212 MOVE_TO_POS | MOVE_TO_X);
16213
16214 row->ascent = it->max_ascent;
16215 row->height = it->max_ascent + it->max_descent;
16216 row->phys_ascent = it->max_phys_ascent;
16217 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16218
16219 /* This condition is for the case that we are called with current_x
16220 past last_visible_x. */
16221 while (it->current_x < max_x)
16222 {
16223 int x_before, x, n_glyphs_before, i, nglyphs;
16224
16225 /* Get the next display element. */
16226 if (!get_next_display_element (it))
16227 break;
16228
16229 /* Produce glyphs. */
16230 x_before = it->current_x;
16231 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
16232 PRODUCE_GLYPHS (it);
16233
16234 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
16235 i = 0;
16236 x = x_before;
16237 while (i < nglyphs)
16238 {
16239 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16240
16241 if (!it->truncate_lines_p
16242 && x + glyph->pixel_width > max_x)
16243 {
16244 /* End of continued line or max_x reached. */
16245 if (CHAR_GLYPH_PADDING_P (*glyph))
16246 {
16247 /* A wide character is unbreakable. */
16248 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
16249 it->current_x = x_before;
16250 }
16251 else
16252 {
16253 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
16254 it->current_x = x;
16255 }
16256 break;
16257 }
16258 else if (x + glyph->pixel_width > it->first_visible_x)
16259 {
16260 /* Glyph is at least partially visible. */
16261 ++it->hpos;
16262 if (x < it->first_visible_x)
16263 it->glyph_row->x = x - it->first_visible_x;
16264 }
16265 else
16266 {
16267 /* Glyph is off the left margin of the display area.
16268 Should not happen. */
16269 abort ();
16270 }
16271
16272 row->ascent = max (row->ascent, it->max_ascent);
16273 row->height = max (row->height, it->max_ascent + it->max_descent);
16274 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16275 row->phys_height = max (row->phys_height,
16276 it->max_phys_ascent + it->max_phys_descent);
16277 x += glyph->pixel_width;
16278 ++i;
16279 }
16280
16281 /* Stop if max_x reached. */
16282 if (i < nglyphs)
16283 break;
16284
16285 /* Stop at line ends. */
16286 if (ITERATOR_AT_END_OF_LINE_P (it))
16287 {
16288 it->continuation_lines_width = 0;
16289 break;
16290 }
16291
16292 set_iterator_to_next (it, 1);
16293
16294 /* Stop if truncating at the right edge. */
16295 if (it->truncate_lines_p
16296 && it->current_x >= it->last_visible_x)
16297 {
16298 /* Add truncation mark, but don't do it if the line is
16299 truncated at a padding space. */
16300 if (IT_CHARPOS (*it) < it->string_nchars)
16301 {
16302 if (!FRAME_WINDOW_P (it->f))
16303 {
16304 int i, n;
16305
16306 if (it->current_x > it->last_visible_x)
16307 {
16308 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16309 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16310 break;
16311 for (n = row->used[TEXT_AREA]; i < n; ++i)
16312 {
16313 row->used[TEXT_AREA] = i;
16314 produce_special_glyphs (it, IT_TRUNCATION);
16315 }
16316 }
16317 produce_special_glyphs (it, IT_TRUNCATION);
16318 }
16319 it->glyph_row->truncated_on_right_p = 1;
16320 }
16321 break;
16322 }
16323 }
16324
16325 /* Maybe insert a truncation at the left. */
16326 if (it->first_visible_x
16327 && IT_CHARPOS (*it) > 0)
16328 {
16329 if (!FRAME_WINDOW_P (it->f))
16330 insert_left_trunc_glyphs (it);
16331 it->glyph_row->truncated_on_left_p = 1;
16332 }
16333
16334 it->face_id = saved_face_id;
16335
16336 /* Value is number of columns displayed. */
16337 return it->hpos - hpos_at_start;
16338 }
16339
16340
16341 \f
16342 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
16343 appears as an element of LIST or as the car of an element of LIST.
16344 If PROPVAL is a list, compare each element against LIST in that
16345 way, and return 1/2 if any element of PROPVAL is found in LIST.
16346 Otherwise return 0. This function cannot quit.
16347 The return value is 2 if the text is invisible but with an ellipsis
16348 and 1 if it's invisible and without an ellipsis. */
16349
16350 int
16351 invisible_p (propval, list)
16352 register Lisp_Object propval;
16353 Lisp_Object list;
16354 {
16355 register Lisp_Object tail, proptail;
16356
16357 for (tail = list; CONSP (tail); tail = XCDR (tail))
16358 {
16359 register Lisp_Object tem;
16360 tem = XCAR (tail);
16361 if (EQ (propval, tem))
16362 return 1;
16363 if (CONSP (tem) && EQ (propval, XCAR (tem)))
16364 return NILP (XCDR (tem)) ? 1 : 2;
16365 }
16366
16367 if (CONSP (propval))
16368 {
16369 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
16370 {
16371 Lisp_Object propelt;
16372 propelt = XCAR (proptail);
16373 for (tail = list; CONSP (tail); tail = XCDR (tail))
16374 {
16375 register Lisp_Object tem;
16376 tem = XCAR (tail);
16377 if (EQ (propelt, tem))
16378 return 1;
16379 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
16380 return NILP (XCDR (tem)) ? 1 : 2;
16381 }
16382 }
16383 }
16384
16385 return 0;
16386 }
16387
16388 \f
16389 /***********************************************************************
16390 Glyph Display
16391 ***********************************************************************/
16392
16393 #ifdef HAVE_WINDOW_SYSTEM
16394
16395 #if GLYPH_DEBUG
16396
16397 void
16398 dump_glyph_string (s)
16399 struct glyph_string *s;
16400 {
16401 fprintf (stderr, "glyph string\n");
16402 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
16403 s->x, s->y, s->width, s->height);
16404 fprintf (stderr, " ybase = %d\n", s->ybase);
16405 fprintf (stderr, " hl = %d\n", s->hl);
16406 fprintf (stderr, " left overhang = %d, right = %d\n",
16407 s->left_overhang, s->right_overhang);
16408 fprintf (stderr, " nchars = %d\n", s->nchars);
16409 fprintf (stderr, " extends to end of line = %d\n",
16410 s->extends_to_end_of_line_p);
16411 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
16412 fprintf (stderr, " bg width = %d\n", s->background_width);
16413 }
16414
16415 #endif /* GLYPH_DEBUG */
16416
16417 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
16418 of XChar2b structures for S; it can't be allocated in
16419 init_glyph_string because it must be allocated via `alloca'. W
16420 is the window on which S is drawn. ROW and AREA are the glyph row
16421 and area within the row from which S is constructed. START is the
16422 index of the first glyph structure covered by S. HL is a
16423 face-override for drawing S. */
16424
16425 #ifdef HAVE_NTGUI
16426 #define OPTIONAL_HDC(hdc) hdc,
16427 #define DECLARE_HDC(hdc) HDC hdc;
16428 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
16429 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
16430 #endif
16431
16432 #ifndef OPTIONAL_HDC
16433 #define OPTIONAL_HDC(hdc)
16434 #define DECLARE_HDC(hdc)
16435 #define ALLOCATE_HDC(hdc, f)
16436 #define RELEASE_HDC(hdc, f)
16437 #endif
16438
16439 static void
16440 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
16441 struct glyph_string *s;
16442 DECLARE_HDC (hdc)
16443 XChar2b *char2b;
16444 struct window *w;
16445 struct glyph_row *row;
16446 enum glyph_row_area area;
16447 int start;
16448 enum draw_glyphs_face hl;
16449 {
16450 bzero (s, sizeof *s);
16451 s->w = w;
16452 s->f = XFRAME (w->frame);
16453 #ifdef HAVE_NTGUI
16454 s->hdc = hdc;
16455 #endif
16456 s->display = FRAME_X_DISPLAY (s->f);
16457 s->window = FRAME_X_WINDOW (s->f);
16458 s->char2b = char2b;
16459 s->hl = hl;
16460 s->row = row;
16461 s->area = area;
16462 s->first_glyph = row->glyphs[area] + start;
16463 s->height = row->height;
16464 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
16465
16466 /* Display the internal border below the tool-bar window. */
16467 if (s->w == XWINDOW (s->f->tool_bar_window))
16468 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
16469
16470 s->ybase = s->y + row->ascent;
16471 }
16472
16473
16474 /* Append the list of glyph strings with head H and tail T to the list
16475 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
16476
16477 static INLINE void
16478 append_glyph_string_lists (head, tail, h, t)
16479 struct glyph_string **head, **tail;
16480 struct glyph_string *h, *t;
16481 {
16482 if (h)
16483 {
16484 if (*head)
16485 (*tail)->next = h;
16486 else
16487 *head = h;
16488 h->prev = *tail;
16489 *tail = t;
16490 }
16491 }
16492
16493
16494 /* Prepend the list of glyph strings with head H and tail T to the
16495 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
16496 result. */
16497
16498 static INLINE void
16499 prepend_glyph_string_lists (head, tail, h, t)
16500 struct glyph_string **head, **tail;
16501 struct glyph_string *h, *t;
16502 {
16503 if (h)
16504 {
16505 if (*head)
16506 (*head)->prev = t;
16507 else
16508 *tail = t;
16509 t->next = *head;
16510 *head = h;
16511 }
16512 }
16513
16514
16515 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
16516 Set *HEAD and *TAIL to the resulting list. */
16517
16518 static INLINE void
16519 append_glyph_string (head, tail, s)
16520 struct glyph_string **head, **tail;
16521 struct glyph_string *s;
16522 {
16523 s->next = s->prev = NULL;
16524 append_glyph_string_lists (head, tail, s, s);
16525 }
16526
16527
16528 /* Get face and two-byte form of character glyph GLYPH on frame F.
16529 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
16530 a pointer to a realized face that is ready for display. */
16531
16532 static INLINE struct face *
16533 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
16534 struct frame *f;
16535 struct glyph *glyph;
16536 XChar2b *char2b;
16537 int *two_byte_p;
16538 {
16539 struct face *face;
16540
16541 xassert (glyph->type == CHAR_GLYPH);
16542 face = FACE_FROM_ID (f, glyph->face_id);
16543
16544 if (two_byte_p)
16545 *two_byte_p = 0;
16546
16547 if (!glyph->multibyte_p)
16548 {
16549 /* Unibyte case. We don't have to encode, but we have to make
16550 sure to use a face suitable for unibyte. */
16551 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
16552 }
16553 else if (glyph->u.ch < 128
16554 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
16555 {
16556 /* Case of ASCII in a face known to fit ASCII. */
16557 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
16558 }
16559 else
16560 {
16561 int c1, c2, charset;
16562
16563 /* Split characters into bytes. If c2 is -1 afterwards, C is
16564 really a one-byte character so that byte1 is zero. */
16565 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
16566 if (c2 > 0)
16567 STORE_XCHAR2B (char2b, c1, c2);
16568 else
16569 STORE_XCHAR2B (char2b, 0, c1);
16570
16571 /* Maybe encode the character in *CHAR2B. */
16572 if (charset != CHARSET_ASCII)
16573 {
16574 struct font_info *font_info
16575 = FONT_INFO_FROM_ID (f, face->font_info_id);
16576 if (font_info)
16577 glyph->font_type
16578 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
16579 }
16580 }
16581
16582 /* Make sure X resources of the face are allocated. */
16583 xassert (face != NULL);
16584 PREPARE_FACE_FOR_DISPLAY (f, face);
16585 return face;
16586 }
16587
16588
16589 /* Fill glyph string S with composition components specified by S->cmp.
16590
16591 FACES is an array of faces for all components of this composition.
16592 S->gidx is the index of the first component for S.
16593 OVERLAPS_P non-zero means S should draw the foreground only, and
16594 use its physical height for clipping.
16595
16596 Value is the index of a component not in S. */
16597
16598 static int
16599 fill_composite_glyph_string (s, faces, overlaps_p)
16600 struct glyph_string *s;
16601 struct face **faces;
16602 int overlaps_p;
16603 {
16604 int i;
16605
16606 xassert (s);
16607
16608 s->for_overlaps_p = overlaps_p;
16609
16610 s->face = faces[s->gidx];
16611 s->font = s->face->font;
16612 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
16613
16614 /* For all glyphs of this composition, starting at the offset
16615 S->gidx, until we reach the end of the definition or encounter a
16616 glyph that requires the different face, add it to S. */
16617 ++s->nchars;
16618 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
16619 ++s->nchars;
16620
16621 /* All glyph strings for the same composition has the same width,
16622 i.e. the width set for the first component of the composition. */
16623
16624 s->width = s->first_glyph->pixel_width;
16625
16626 /* If the specified font could not be loaded, use the frame's
16627 default font, but record the fact that we couldn't load it in
16628 the glyph string so that we can draw rectangles for the
16629 characters of the glyph string. */
16630 if (s->font == NULL)
16631 {
16632 s->font_not_found_p = 1;
16633 s->font = FRAME_FONT (s->f);
16634 }
16635
16636 /* Adjust base line for subscript/superscript text. */
16637 s->ybase += s->first_glyph->voffset;
16638
16639 xassert (s->face && s->face->gc);
16640
16641 /* This glyph string must always be drawn with 16-bit functions. */
16642 s->two_byte_p = 1;
16643
16644 return s->gidx + s->nchars;
16645 }
16646
16647
16648 /* Fill glyph string S from a sequence of character glyphs.
16649
16650 FACE_ID is the face id of the string. START is the index of the
16651 first glyph to consider, END is the index of the last + 1.
16652 OVERLAPS_P non-zero means S should draw the foreground only, and
16653 use its physical height for clipping.
16654
16655 Value is the index of the first glyph not in S. */
16656
16657 static int
16658 fill_glyph_string (s, face_id, start, end, overlaps_p)
16659 struct glyph_string *s;
16660 int face_id;
16661 int start, end, overlaps_p;
16662 {
16663 struct glyph *glyph, *last;
16664 int voffset;
16665 int glyph_not_available_p;
16666
16667 xassert (s->f == XFRAME (s->w->frame));
16668 xassert (s->nchars == 0);
16669 xassert (start >= 0 && end > start);
16670
16671 s->for_overlaps_p = overlaps_p,
16672 glyph = s->row->glyphs[s->area] + start;
16673 last = s->row->glyphs[s->area] + end;
16674 voffset = glyph->voffset;
16675
16676 glyph_not_available_p = glyph->glyph_not_available_p;
16677
16678 while (glyph < last
16679 && glyph->type == CHAR_GLYPH
16680 && glyph->voffset == voffset
16681 /* Same face id implies same font, nowadays. */
16682 && glyph->face_id == face_id
16683 && glyph->glyph_not_available_p == glyph_not_available_p)
16684 {
16685 int two_byte_p;
16686
16687 s->face = get_glyph_face_and_encoding (s->f, glyph,
16688 s->char2b + s->nchars,
16689 &two_byte_p);
16690 s->two_byte_p = two_byte_p;
16691 ++s->nchars;
16692 xassert (s->nchars <= end - start);
16693 s->width += glyph->pixel_width;
16694 ++glyph;
16695 }
16696
16697 s->font = s->face->font;
16698 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
16699
16700 /* If the specified font could not be loaded, use the frame's font,
16701 but record the fact that we couldn't load it in
16702 S->font_not_found_p so that we can draw rectangles for the
16703 characters of the glyph string. */
16704 if (s->font == NULL || glyph_not_available_p)
16705 {
16706 s->font_not_found_p = 1;
16707 s->font = FRAME_FONT (s->f);
16708 }
16709
16710 /* Adjust base line for subscript/superscript text. */
16711 s->ybase += voffset;
16712
16713 xassert (s->face && s->face->gc);
16714 return glyph - s->row->glyphs[s->area];
16715 }
16716
16717
16718 /* Fill glyph string S from image glyph S->first_glyph. */
16719
16720 static void
16721 fill_image_glyph_string (s)
16722 struct glyph_string *s;
16723 {
16724 xassert (s->first_glyph->type == IMAGE_GLYPH);
16725 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
16726 xassert (s->img);
16727 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
16728 s->font = s->face->font;
16729 s->width = s->first_glyph->pixel_width;
16730
16731 /* Adjust base line for subscript/superscript text. */
16732 s->ybase += s->first_glyph->voffset;
16733 }
16734
16735
16736 /* Fill glyph string S from a sequence of stretch glyphs.
16737
16738 ROW is the glyph row in which the glyphs are found, AREA is the
16739 area within the row. START is the index of the first glyph to
16740 consider, END is the index of the last + 1.
16741
16742 Value is the index of the first glyph not in S. */
16743
16744 static int
16745 fill_stretch_glyph_string (s, row, area, start, end)
16746 struct glyph_string *s;
16747 struct glyph_row *row;
16748 enum glyph_row_area area;
16749 int start, end;
16750 {
16751 struct glyph *glyph, *last;
16752 int voffset, face_id;
16753
16754 xassert (s->first_glyph->type == STRETCH_GLYPH);
16755
16756 glyph = s->row->glyphs[s->area] + start;
16757 last = s->row->glyphs[s->area] + end;
16758 face_id = glyph->face_id;
16759 s->face = FACE_FROM_ID (s->f, face_id);
16760 s->font = s->face->font;
16761 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
16762 s->width = glyph->pixel_width;
16763 voffset = glyph->voffset;
16764
16765 for (++glyph;
16766 (glyph < last
16767 && glyph->type == STRETCH_GLYPH
16768 && glyph->voffset == voffset
16769 && glyph->face_id == face_id);
16770 ++glyph)
16771 s->width += glyph->pixel_width;
16772
16773 /* Adjust base line for subscript/superscript text. */
16774 s->ybase += voffset;
16775
16776 /* The case that face->gc == 0 is handled when drawing the glyph
16777 string by calling PREPARE_FACE_FOR_DISPLAY. */
16778 xassert (s->face);
16779 return glyph - s->row->glyphs[s->area];
16780 }
16781
16782
16783 /* EXPORT for RIF:
16784 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
16785 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
16786 assumed to be zero. */
16787
16788 void
16789 x_get_glyph_overhangs (glyph, f, left, right)
16790 struct glyph *glyph;
16791 struct frame *f;
16792 int *left, *right;
16793 {
16794 *left = *right = 0;
16795
16796 if (glyph->type == CHAR_GLYPH)
16797 {
16798 XFontStruct *font;
16799 struct face *face;
16800 struct font_info *font_info;
16801 XChar2b char2b;
16802 XCharStruct *pcm;
16803
16804 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
16805 font = face->font;
16806 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
16807 if (font /* ++KFS: Should this be font_info ? */
16808 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
16809 {
16810 if (pcm->rbearing > pcm->width)
16811 *right = pcm->rbearing - pcm->width;
16812 if (pcm->lbearing < 0)
16813 *left = -pcm->lbearing;
16814 }
16815 }
16816 }
16817
16818
16819 /* Return the index of the first glyph preceding glyph string S that
16820 is overwritten by S because of S's left overhang. Value is -1
16821 if no glyphs are overwritten. */
16822
16823 static int
16824 left_overwritten (s)
16825 struct glyph_string *s;
16826 {
16827 int k;
16828
16829 if (s->left_overhang)
16830 {
16831 int x = 0, i;
16832 struct glyph *glyphs = s->row->glyphs[s->area];
16833 int first = s->first_glyph - glyphs;
16834
16835 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
16836 x -= glyphs[i].pixel_width;
16837
16838 k = i + 1;
16839 }
16840 else
16841 k = -1;
16842
16843 return k;
16844 }
16845
16846
16847 /* Return the index of the first glyph preceding glyph string S that
16848 is overwriting S because of its right overhang. Value is -1 if no
16849 glyph in front of S overwrites S. */
16850
16851 static int
16852 left_overwriting (s)
16853 struct glyph_string *s;
16854 {
16855 int i, k, x;
16856 struct glyph *glyphs = s->row->glyphs[s->area];
16857 int first = s->first_glyph - glyphs;
16858
16859 k = -1;
16860 x = 0;
16861 for (i = first - 1; i >= 0; --i)
16862 {
16863 int left, right;
16864 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
16865 if (x + right > 0)
16866 k = i;
16867 x -= glyphs[i].pixel_width;
16868 }
16869
16870 return k;
16871 }
16872
16873
16874 /* Return the index of the last glyph following glyph string S that is
16875 not overwritten by S because of S's right overhang. Value is -1 if
16876 no such glyph is found. */
16877
16878 static int
16879 right_overwritten (s)
16880 struct glyph_string *s;
16881 {
16882 int k = -1;
16883
16884 if (s->right_overhang)
16885 {
16886 int x = 0, i;
16887 struct glyph *glyphs = s->row->glyphs[s->area];
16888 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
16889 int end = s->row->used[s->area];
16890
16891 for (i = first; i < end && s->right_overhang > x; ++i)
16892 x += glyphs[i].pixel_width;
16893
16894 k = i;
16895 }
16896
16897 return k;
16898 }
16899
16900
16901 /* Return the index of the last glyph following glyph string S that
16902 overwrites S because of its left overhang. Value is negative
16903 if no such glyph is found. */
16904
16905 static int
16906 right_overwriting (s)
16907 struct glyph_string *s;
16908 {
16909 int i, k, x;
16910 int end = s->row->used[s->area];
16911 struct glyph *glyphs = s->row->glyphs[s->area];
16912 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
16913
16914 k = -1;
16915 x = 0;
16916 for (i = first; i < end; ++i)
16917 {
16918 int left, right;
16919 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
16920 if (x - left < 0)
16921 k = i;
16922 x += glyphs[i].pixel_width;
16923 }
16924
16925 return k;
16926 }
16927
16928
16929 /* Get face and two-byte form of character C in face FACE_ID on frame
16930 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
16931 means we want to display multibyte text. DISPLAY_P non-zero means
16932 make sure that X resources for the face returned are allocated.
16933 Value is a pointer to a realized face that is ready for display if
16934 DISPLAY_P is non-zero. */
16935
16936 static INLINE struct face *
16937 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
16938 struct frame *f;
16939 int c, face_id;
16940 XChar2b *char2b;
16941 int multibyte_p, display_p;
16942 {
16943 struct face *face = FACE_FROM_ID (f, face_id);
16944
16945 if (!multibyte_p)
16946 {
16947 /* Unibyte case. We don't have to encode, but we have to make
16948 sure to use a face suitable for unibyte. */
16949 STORE_XCHAR2B (char2b, 0, c);
16950 face_id = FACE_FOR_CHAR (f, face, c);
16951 face = FACE_FROM_ID (f, face_id);
16952 }
16953 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
16954 {
16955 /* Case of ASCII in a face known to fit ASCII. */
16956 STORE_XCHAR2B (char2b, 0, c);
16957 }
16958 else
16959 {
16960 int c1, c2, charset;
16961
16962 /* Split characters into bytes. If c2 is -1 afterwards, C is
16963 really a one-byte character so that byte1 is zero. */
16964 SPLIT_CHAR (c, charset, c1, c2);
16965 if (c2 > 0)
16966 STORE_XCHAR2B (char2b, c1, c2);
16967 else
16968 STORE_XCHAR2B (char2b, 0, c1);
16969
16970 /* Maybe encode the character in *CHAR2B. */
16971 if (face->font != NULL)
16972 {
16973 struct font_info *font_info
16974 = FONT_INFO_FROM_ID (f, face->font_info_id);
16975 if (font_info)
16976 rif->encode_char (c, char2b, font_info, 0);
16977 }
16978 }
16979
16980 /* Make sure X resources of the face are allocated. */
16981 #ifdef HAVE_X_WINDOWS
16982 if (display_p)
16983 #endif
16984 {
16985 xassert (face != NULL);
16986 PREPARE_FACE_FOR_DISPLAY (f, face);
16987 }
16988
16989 return face;
16990 }
16991
16992
16993 /* Set background width of glyph string S. START is the index of the
16994 first glyph following S. LAST_X is the right-most x-position + 1
16995 in the drawing area. */
16996
16997 static INLINE void
16998 set_glyph_string_background_width (s, start, last_x)
16999 struct glyph_string *s;
17000 int start;
17001 int last_x;
17002 {
17003 /* If the face of this glyph string has to be drawn to the end of
17004 the drawing area, set S->extends_to_end_of_line_p. */
17005 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
17006
17007 if (start == s->row->used[s->area]
17008 && s->area == TEXT_AREA
17009 && ((s->hl == DRAW_NORMAL_TEXT
17010 && (s->row->fill_line_p
17011 || s->face->background != default_face->background
17012 || s->face->stipple != default_face->stipple
17013 || s->row->mouse_face_p))
17014 || s->hl == DRAW_MOUSE_FACE
17015 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
17016 && s->row->fill_line_p)))
17017 s->extends_to_end_of_line_p = 1;
17018
17019 /* If S extends its face to the end of the line, set its
17020 background_width to the distance to the right edge of the drawing
17021 area. */
17022 if (s->extends_to_end_of_line_p)
17023 s->background_width = last_x - s->x + 1;
17024 else
17025 s->background_width = s->width;
17026 }
17027
17028
17029 /* Compute overhangs and x-positions for glyph string S and its
17030 predecessors, or successors. X is the starting x-position for S.
17031 BACKWARD_P non-zero means process predecessors. */
17032
17033 static void
17034 compute_overhangs_and_x (s, x, backward_p)
17035 struct glyph_string *s;
17036 int x;
17037 int backward_p;
17038 {
17039 if (backward_p)
17040 {
17041 while (s)
17042 {
17043 if (rif->compute_glyph_string_overhangs)
17044 rif->compute_glyph_string_overhangs (s);
17045 x -= s->width;
17046 s->x = x;
17047 s = s->prev;
17048 }
17049 }
17050 else
17051 {
17052 while (s)
17053 {
17054 if (rif->compute_glyph_string_overhangs)
17055 rif->compute_glyph_string_overhangs (s);
17056 s->x = x;
17057 x += s->width;
17058 s = s->next;
17059 }
17060 }
17061 }
17062
17063
17064
17065 /* The following macros are only called from draw_glyphs below.
17066 They reference the following parameters of that function directly:
17067 `w', `row', `area', and `overlap_p'
17068 as well as the following local variables:
17069 `s', `f', and `hdc' (in W32) */
17070
17071 #ifdef HAVE_NTGUI
17072 /* On W32, silently add local `hdc' variable to argument list of
17073 init_glyph_string. */
17074 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17075 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
17076 #else
17077 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17078 init_glyph_string (s, char2b, w, row, area, start, hl)
17079 #endif
17080
17081 /* Add a glyph string for a stretch glyph to the list of strings
17082 between HEAD and TAIL. START is the index of the stretch glyph in
17083 row area AREA of glyph row ROW. END is the index of the last glyph
17084 in that glyph row area. X is the current output position assigned
17085 to the new glyph string constructed. HL overrides that face of the
17086 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17087 is the right-most x-position of the drawing area. */
17088
17089 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
17090 and below -- keep them on one line. */
17091 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17092 do \
17093 { \
17094 s = (struct glyph_string *) alloca (sizeof *s); \
17095 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17096 START = fill_stretch_glyph_string (s, row, area, START, END); \
17097 append_glyph_string (&HEAD, &TAIL, s); \
17098 s->x = (X); \
17099 } \
17100 while (0)
17101
17102
17103 /* Add a glyph string for an image glyph to the list of strings
17104 between HEAD and TAIL. START is the index of the image glyph in
17105 row area AREA of glyph row ROW. END is the index of the last glyph
17106 in that glyph row area. X is the current output position assigned
17107 to the new glyph string constructed. HL overrides that face of the
17108 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17109 is the right-most x-position of the drawing area. */
17110
17111 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17112 do \
17113 { \
17114 s = (struct glyph_string *) alloca (sizeof *s); \
17115 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17116 fill_image_glyph_string (s); \
17117 append_glyph_string (&HEAD, &TAIL, s); \
17118 ++START; \
17119 s->x = (X); \
17120 } \
17121 while (0)
17122
17123
17124 /* Add a glyph string for a sequence of character glyphs to the list
17125 of strings between HEAD and TAIL. START is the index of the first
17126 glyph in row area AREA of glyph row ROW that is part of the new
17127 glyph string. END is the index of the last glyph in that glyph row
17128 area. X is the current output position assigned to the new glyph
17129 string constructed. HL overrides that face of the glyph; e.g. it
17130 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
17131 right-most x-position of the drawing area. */
17132
17133 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17134 do \
17135 { \
17136 int c, face_id; \
17137 XChar2b *char2b; \
17138 \
17139 c = (row)->glyphs[area][START].u.ch; \
17140 face_id = (row)->glyphs[area][START].face_id; \
17141 \
17142 s = (struct glyph_string *) alloca (sizeof *s); \
17143 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
17144 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
17145 append_glyph_string (&HEAD, &TAIL, s); \
17146 s->x = (X); \
17147 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
17148 } \
17149 while (0)
17150
17151
17152 /* Add a glyph string for a composite sequence to the list of strings
17153 between HEAD and TAIL. START is the index of the first glyph in
17154 row area AREA of glyph row ROW that is part of the new glyph
17155 string. END is the index of the last glyph in that glyph row area.
17156 X is the current output position assigned to the new glyph string
17157 constructed. HL overrides that face of the glyph; e.g. it is
17158 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
17159 x-position of the drawing area. */
17160
17161 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17162 do { \
17163 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
17164 int face_id = (row)->glyphs[area][START].face_id; \
17165 struct face *base_face = FACE_FROM_ID (f, face_id); \
17166 struct composition *cmp = composition_table[cmp_id]; \
17167 int glyph_len = cmp->glyph_len; \
17168 XChar2b *char2b; \
17169 struct face **faces; \
17170 struct glyph_string *first_s = NULL; \
17171 int n; \
17172 \
17173 base_face = base_face->ascii_face; \
17174 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
17175 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
17176 /* At first, fill in `char2b' and `faces'. */ \
17177 for (n = 0; n < glyph_len; n++) \
17178 { \
17179 int c = COMPOSITION_GLYPH (cmp, n); \
17180 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
17181 faces[n] = FACE_FROM_ID (f, this_face_id); \
17182 get_char_face_and_encoding (f, c, this_face_id, \
17183 char2b + n, 1, 1); \
17184 } \
17185 \
17186 /* Make glyph_strings for each glyph sequence that is drawable by \
17187 the same face, and append them to HEAD/TAIL. */ \
17188 for (n = 0; n < cmp->glyph_len;) \
17189 { \
17190 s = (struct glyph_string *) alloca (sizeof *s); \
17191 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
17192 append_glyph_string (&(HEAD), &(TAIL), s); \
17193 s->cmp = cmp; \
17194 s->gidx = n; \
17195 s->x = (X); \
17196 \
17197 if (n == 0) \
17198 first_s = s; \
17199 \
17200 n = fill_composite_glyph_string (s, faces, overlaps_p); \
17201 } \
17202 \
17203 ++START; \
17204 s = first_s; \
17205 } while (0)
17206
17207
17208 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
17209 of AREA of glyph row ROW on window W between indices START and END.
17210 HL overrides the face for drawing glyph strings, e.g. it is
17211 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
17212 x-positions of the drawing area.
17213
17214 This is an ugly monster macro construct because we must use alloca
17215 to allocate glyph strings (because draw_glyphs can be called
17216 asynchronously). */
17217
17218 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17219 do \
17220 { \
17221 HEAD = TAIL = NULL; \
17222 while (START < END) \
17223 { \
17224 struct glyph *first_glyph = (row)->glyphs[area] + START; \
17225 switch (first_glyph->type) \
17226 { \
17227 case CHAR_GLYPH: \
17228 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
17229 HL, X, LAST_X); \
17230 break; \
17231 \
17232 case COMPOSITE_GLYPH: \
17233 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
17234 HL, X, LAST_X); \
17235 break; \
17236 \
17237 case STRETCH_GLYPH: \
17238 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
17239 HL, X, LAST_X); \
17240 break; \
17241 \
17242 case IMAGE_GLYPH: \
17243 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
17244 HL, X, LAST_X); \
17245 break; \
17246 \
17247 default: \
17248 abort (); \
17249 } \
17250 \
17251 set_glyph_string_background_width (s, START, LAST_X); \
17252 (X) += s->width; \
17253 } \
17254 } \
17255 while (0)
17256
17257
17258 /* Draw glyphs between START and END in AREA of ROW on window W,
17259 starting at x-position X. X is relative to AREA in W. HL is a
17260 face-override with the following meaning:
17261
17262 DRAW_NORMAL_TEXT draw normally
17263 DRAW_CURSOR draw in cursor face
17264 DRAW_MOUSE_FACE draw in mouse face.
17265 DRAW_INVERSE_VIDEO draw in mode line face
17266 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
17267 DRAW_IMAGE_RAISED draw an image with a raised relief around it
17268
17269 If OVERLAPS_P is non-zero, draw only the foreground of characters
17270 and clip to the physical height of ROW.
17271
17272 Value is the x-position reached, relative to AREA of W. */
17273
17274 static int
17275 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
17276 struct window *w;
17277 int x;
17278 struct glyph_row *row;
17279 enum glyph_row_area area;
17280 int start, end;
17281 enum draw_glyphs_face hl;
17282 int overlaps_p;
17283 {
17284 struct glyph_string *head, *tail;
17285 struct glyph_string *s;
17286 int last_x, area_width;
17287 int x_reached;
17288 int i, j;
17289 struct frame *f = XFRAME (WINDOW_FRAME (w));
17290 DECLARE_HDC (hdc);
17291
17292 ALLOCATE_HDC (hdc, f);
17293
17294 /* Let's rather be paranoid than getting a SEGV. */
17295 end = min (end, row->used[area]);
17296 start = max (0, start);
17297 start = min (end, start);
17298
17299 /* Translate X to frame coordinates. Set last_x to the right
17300 end of the drawing area. */
17301 if (row->full_width_p)
17302 {
17303 /* X is relative to the left edge of W, without scroll bars
17304 or fringes. */
17305 x += WINDOW_LEFT_EDGE_X (w);
17306 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
17307 }
17308 else
17309 {
17310 int area_left = window_box_left (w, area);
17311 x += area_left;
17312 area_width = window_box_width (w, area);
17313 last_x = area_left + area_width;
17314 }
17315
17316 /* Build a doubly-linked list of glyph_string structures between
17317 head and tail from what we have to draw. Note that the macro
17318 BUILD_GLYPH_STRINGS will modify its start parameter. That's
17319 the reason we use a separate variable `i'. */
17320 i = start;
17321 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
17322 if (tail)
17323 x_reached = tail->x + tail->background_width;
17324 else
17325 x_reached = x;
17326
17327 /* If there are any glyphs with lbearing < 0 or rbearing > width in
17328 the row, redraw some glyphs in front or following the glyph
17329 strings built above. */
17330 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
17331 {
17332 int dummy_x = 0;
17333 struct glyph_string *h, *t;
17334
17335 /* Compute overhangs for all glyph strings. */
17336 if (rif->compute_glyph_string_overhangs)
17337 for (s = head; s; s = s->next)
17338 rif->compute_glyph_string_overhangs (s);
17339
17340 /* Prepend glyph strings for glyphs in front of the first glyph
17341 string that are overwritten because of the first glyph
17342 string's left overhang. The background of all strings
17343 prepended must be drawn because the first glyph string
17344 draws over it. */
17345 i = left_overwritten (head);
17346 if (i >= 0)
17347 {
17348 j = i;
17349 BUILD_GLYPH_STRINGS (j, start, h, t,
17350 DRAW_NORMAL_TEXT, dummy_x, last_x);
17351 start = i;
17352 compute_overhangs_and_x (t, head->x, 1);
17353 prepend_glyph_string_lists (&head, &tail, h, t);
17354 }
17355
17356 /* Prepend glyph strings for glyphs in front of the first glyph
17357 string that overwrite that glyph string because of their
17358 right overhang. For these strings, only the foreground must
17359 be drawn, because it draws over the glyph string at `head'.
17360 The background must not be drawn because this would overwrite
17361 right overhangs of preceding glyphs for which no glyph
17362 strings exist. */
17363 i = left_overwriting (head);
17364 if (i >= 0)
17365 {
17366 BUILD_GLYPH_STRINGS (i, start, h, t,
17367 DRAW_NORMAL_TEXT, dummy_x, last_x);
17368 for (s = h; s; s = s->next)
17369 s->background_filled_p = 1;
17370 compute_overhangs_and_x (t, head->x, 1);
17371 prepend_glyph_string_lists (&head, &tail, h, t);
17372 }
17373
17374 /* Append glyphs strings for glyphs following the last glyph
17375 string tail that are overwritten by tail. The background of
17376 these strings has to be drawn because tail's foreground draws
17377 over it. */
17378 i = right_overwritten (tail);
17379 if (i >= 0)
17380 {
17381 BUILD_GLYPH_STRINGS (end, i, h, t,
17382 DRAW_NORMAL_TEXT, x, last_x);
17383 compute_overhangs_and_x (h, tail->x + tail->width, 0);
17384 append_glyph_string_lists (&head, &tail, h, t);
17385 }
17386
17387 /* Append glyph strings for glyphs following the last glyph
17388 string tail that overwrite tail. The foreground of such
17389 glyphs has to be drawn because it writes into the background
17390 of tail. The background must not be drawn because it could
17391 paint over the foreground of following glyphs. */
17392 i = right_overwriting (tail);
17393 if (i >= 0)
17394 {
17395 BUILD_GLYPH_STRINGS (end, i, h, t,
17396 DRAW_NORMAL_TEXT, x, last_x);
17397 for (s = h; s; s = s->next)
17398 s->background_filled_p = 1;
17399 compute_overhangs_and_x (h, tail->x + tail->width, 0);
17400 append_glyph_string_lists (&head, &tail, h, t);
17401 }
17402 }
17403
17404 /* Draw all strings. */
17405 for (s = head; s; s = s->next)
17406 rif->draw_glyph_string (s);
17407
17408 if (area == TEXT_AREA
17409 && !row->full_width_p
17410 /* When drawing overlapping rows, only the glyph strings'
17411 foreground is drawn, which doesn't erase a cursor
17412 completely. */
17413 && !overlaps_p)
17414 {
17415 int x0 = head ? head->x : x;
17416 int x1 = tail ? tail->x + tail->background_width : x;
17417
17418 int text_left = window_box_left (w, TEXT_AREA);
17419 x0 -= text_left;
17420 x1 -= text_left;
17421
17422 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
17423 row->y, MATRIX_ROW_BOTTOM_Y (row));
17424 }
17425
17426 /* Value is the x-position up to which drawn, relative to AREA of W.
17427 This doesn't include parts drawn because of overhangs. */
17428 if (row->full_width_p)
17429 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
17430 else
17431 x_reached -= window_box_left (w, area);
17432
17433 RELEASE_HDC (hdc, f);
17434
17435 return x_reached;
17436 }
17437
17438
17439 /* Store one glyph for IT->char_to_display in IT->glyph_row.
17440 Called from x_produce_glyphs when IT->glyph_row is non-null. */
17441
17442 static INLINE void
17443 append_glyph (it)
17444 struct it *it;
17445 {
17446 struct glyph *glyph;
17447 enum glyph_row_area area = it->area;
17448
17449 xassert (it->glyph_row);
17450 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
17451
17452 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17453 if (glyph < it->glyph_row->glyphs[area + 1])
17454 {
17455 glyph->charpos = CHARPOS (it->position);
17456 glyph->object = it->object;
17457 glyph->pixel_width = it->pixel_width;
17458 glyph->voffset = it->voffset;
17459 glyph->type = CHAR_GLYPH;
17460 glyph->multibyte_p = it->multibyte_p;
17461 glyph->left_box_line_p = it->start_of_box_run_p;
17462 glyph->right_box_line_p = it->end_of_box_run_p;
17463 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
17464 || it->phys_descent > it->descent);
17465 glyph->padding_p = 0;
17466 glyph->glyph_not_available_p = it->glyph_not_available_p;
17467 glyph->face_id = it->face_id;
17468 glyph->u.ch = it->char_to_display;
17469 glyph->font_type = FONT_TYPE_UNKNOWN;
17470 ++it->glyph_row->used[area];
17471 }
17472 }
17473
17474 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
17475 Called from x_produce_glyphs when IT->glyph_row is non-null. */
17476
17477 static INLINE void
17478 append_composite_glyph (it)
17479 struct it *it;
17480 {
17481 struct glyph *glyph;
17482 enum glyph_row_area area = it->area;
17483
17484 xassert (it->glyph_row);
17485
17486 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17487 if (glyph < it->glyph_row->glyphs[area + 1])
17488 {
17489 glyph->charpos = CHARPOS (it->position);
17490 glyph->object = it->object;
17491 glyph->pixel_width = it->pixel_width;
17492 glyph->voffset = it->voffset;
17493 glyph->type = COMPOSITE_GLYPH;
17494 glyph->multibyte_p = it->multibyte_p;
17495 glyph->left_box_line_p = it->start_of_box_run_p;
17496 glyph->right_box_line_p = it->end_of_box_run_p;
17497 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
17498 || it->phys_descent > it->descent);
17499 glyph->padding_p = 0;
17500 glyph->glyph_not_available_p = 0;
17501 glyph->face_id = it->face_id;
17502 glyph->u.cmp_id = it->cmp_id;
17503 glyph->font_type = FONT_TYPE_UNKNOWN;
17504 ++it->glyph_row->used[area];
17505 }
17506 }
17507
17508
17509 /* Change IT->ascent and IT->height according to the setting of
17510 IT->voffset. */
17511
17512 static INLINE void
17513 take_vertical_position_into_account (it)
17514 struct it *it;
17515 {
17516 if (it->voffset)
17517 {
17518 if (it->voffset < 0)
17519 /* Increase the ascent so that we can display the text higher
17520 in the line. */
17521 it->ascent += abs (it->voffset);
17522 else
17523 /* Increase the descent so that we can display the text lower
17524 in the line. */
17525 it->descent += it->voffset;
17526 }
17527 }
17528
17529
17530 /* Produce glyphs/get display metrics for the image IT is loaded with.
17531 See the description of struct display_iterator in dispextern.h for
17532 an overview of struct display_iterator. */
17533
17534 static void
17535 produce_image_glyph (it)
17536 struct it *it;
17537 {
17538 struct image *img;
17539 struct face *face;
17540
17541 xassert (it->what == IT_IMAGE);
17542
17543 face = FACE_FROM_ID (it->f, it->face_id);
17544 img = IMAGE_FROM_ID (it->f, it->image_id);
17545 xassert (img);
17546
17547 /* Make sure X resources of the face and image are loaded. */
17548 PREPARE_FACE_FOR_DISPLAY (it->f, face);
17549 prepare_image_for_display (it->f, img);
17550
17551 it->ascent = it->phys_ascent = image_ascent (img, face);
17552 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
17553 it->pixel_width = img->width + 2 * img->hmargin;
17554
17555 it->nglyphs = 1;
17556
17557 if (face->box != FACE_NO_BOX)
17558 {
17559 if (face->box_line_width > 0)
17560 {
17561 it->ascent += face->box_line_width;
17562 it->descent += face->box_line_width;
17563 }
17564
17565 if (it->start_of_box_run_p)
17566 it->pixel_width += abs (face->box_line_width);
17567 if (it->end_of_box_run_p)
17568 it->pixel_width += abs (face->box_line_width);
17569 }
17570
17571 take_vertical_position_into_account (it);
17572
17573 if (it->glyph_row)
17574 {
17575 struct glyph *glyph;
17576 enum glyph_row_area area = it->area;
17577
17578 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17579 if (glyph < it->glyph_row->glyphs[area + 1])
17580 {
17581 glyph->charpos = CHARPOS (it->position);
17582 glyph->object = it->object;
17583 glyph->pixel_width = it->pixel_width;
17584 glyph->voffset = it->voffset;
17585 glyph->type = IMAGE_GLYPH;
17586 glyph->multibyte_p = it->multibyte_p;
17587 glyph->left_box_line_p = it->start_of_box_run_p;
17588 glyph->right_box_line_p = it->end_of_box_run_p;
17589 glyph->overlaps_vertically_p = 0;
17590 glyph->padding_p = 0;
17591 glyph->glyph_not_available_p = 0;
17592 glyph->face_id = it->face_id;
17593 glyph->u.img_id = img->id;
17594 glyph->font_type = FONT_TYPE_UNKNOWN;
17595 ++it->glyph_row->used[area];
17596 }
17597 }
17598 }
17599
17600
17601 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
17602 of the glyph, WIDTH and HEIGHT are the width and height of the
17603 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
17604 ascent of the glyph (0 <= ASCENT <= 1). */
17605
17606 static void
17607 append_stretch_glyph (it, object, width, height, ascent)
17608 struct it *it;
17609 Lisp_Object object;
17610 int width, height;
17611 double ascent;
17612 {
17613 struct glyph *glyph;
17614 enum glyph_row_area area = it->area;
17615
17616 xassert (ascent >= 0 && ascent <= 1);
17617
17618 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17619 if (glyph < it->glyph_row->glyphs[area + 1])
17620 {
17621 glyph->charpos = CHARPOS (it->position);
17622 glyph->object = object;
17623 glyph->pixel_width = width;
17624 glyph->voffset = it->voffset;
17625 glyph->type = STRETCH_GLYPH;
17626 glyph->multibyte_p = it->multibyte_p;
17627 glyph->left_box_line_p = it->start_of_box_run_p;
17628 glyph->right_box_line_p = it->end_of_box_run_p;
17629 glyph->overlaps_vertically_p = 0;
17630 glyph->padding_p = 0;
17631 glyph->glyph_not_available_p = 0;
17632 glyph->face_id = it->face_id;
17633 glyph->u.stretch.ascent = height * ascent;
17634 glyph->u.stretch.height = height;
17635 glyph->font_type = FONT_TYPE_UNKNOWN;
17636 ++it->glyph_row->used[area];
17637 }
17638 }
17639
17640
17641 /* Produce a stretch glyph for iterator IT. IT->object is the value
17642 of the glyph property displayed. The value must be a list
17643 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
17644 being recognized:
17645
17646 1. `:width WIDTH' specifies that the space should be WIDTH *
17647 canonical char width wide. WIDTH may be an integer or floating
17648 point number.
17649
17650 2. `:relative-width FACTOR' specifies that the width of the stretch
17651 should be computed from the width of the first character having the
17652 `glyph' property, and should be FACTOR times that width.
17653
17654 3. `:align-to HPOS' specifies that the space should be wide enough
17655 to reach HPOS, a value in canonical character units.
17656
17657 Exactly one of the above pairs must be present.
17658
17659 4. `:height HEIGHT' specifies that the height of the stretch produced
17660 should be HEIGHT, measured in canonical character units.
17661
17662 5. `:relative-height FACTOR' specifies that the height of the
17663 stretch should be FACTOR times the height of the characters having
17664 the glyph property.
17665
17666 Either none or exactly one of 4 or 5 must be present.
17667
17668 6. `:ascent ASCENT' specifies that ASCENT percent of the height
17669 of the stretch should be used for the ascent of the stretch.
17670 ASCENT must be in the range 0 <= ASCENT <= 100. */
17671
17672 #define NUMVAL(X) \
17673 ((INTEGERP (X) || FLOATP (X)) \
17674 ? XFLOATINT (X) \
17675 : - 1)
17676
17677
17678 static void
17679 produce_stretch_glyph (it)
17680 struct it *it;
17681 {
17682 /* (space :width WIDTH :height HEIGHT. */
17683 Lisp_Object prop, plist;
17684 int width = 0, height = 0;
17685 double ascent = 0;
17686 struct face *face = FACE_FROM_ID (it->f, it->face_id);
17687 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
17688
17689 PREPARE_FACE_FOR_DISPLAY (it->f, face);
17690
17691 /* List should start with `space'. */
17692 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
17693 plist = XCDR (it->object);
17694
17695 /* Compute the width of the stretch. */
17696 if (prop = Fplist_get (plist, QCwidth),
17697 NUMVAL (prop) > 0)
17698 /* Absolute width `:width WIDTH' specified and valid. */
17699 width = NUMVAL (prop) * FRAME_COLUMN_WIDTH (it->f);
17700 else if (prop = Fplist_get (plist, QCrelative_width),
17701 NUMVAL (prop) > 0)
17702 {
17703 /* Relative width `:relative-width FACTOR' specified and valid.
17704 Compute the width of the characters having the `glyph'
17705 property. */
17706 struct it it2;
17707 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
17708
17709 it2 = *it;
17710 if (it->multibyte_p)
17711 {
17712 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
17713 - IT_BYTEPOS (*it));
17714 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
17715 }
17716 else
17717 it2.c = *p, it2.len = 1;
17718
17719 it2.glyph_row = NULL;
17720 it2.what = IT_CHARACTER;
17721 x_produce_glyphs (&it2);
17722 width = NUMVAL (prop) * it2.pixel_width;
17723 }
17724 else if (prop = Fplist_get (plist, QCalign_to),
17725 NUMVAL (prop) > 0)
17726 width = NUMVAL (prop) * FRAME_COLUMN_WIDTH (it->f) - it->current_x;
17727 else
17728 /* Nothing specified -> width defaults to canonical char width. */
17729 width = FRAME_COLUMN_WIDTH (it->f);
17730
17731 /* Compute height. */
17732 if (prop = Fplist_get (plist, QCheight),
17733 NUMVAL (prop) > 0)
17734 height = NUMVAL (prop) * FRAME_LINE_HEIGHT (it->f);
17735 else if (prop = Fplist_get (plist, QCrelative_height),
17736 NUMVAL (prop) > 0)
17737 height = FONT_HEIGHT (font) * NUMVAL (prop);
17738 else
17739 height = FONT_HEIGHT (font);
17740
17741 /* Compute percentage of height used for ascent. If
17742 `:ascent ASCENT' is present and valid, use that. Otherwise,
17743 derive the ascent from the font in use. */
17744 if (prop = Fplist_get (plist, QCascent),
17745 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
17746 ascent = NUMVAL (prop) / 100.0;
17747 else
17748 ascent = (double) FONT_BASE (font) / FONT_HEIGHT (font);
17749
17750 if (width <= 0)
17751 width = 1;
17752 if (height <= 0)
17753 height = 1;
17754
17755 if (it->glyph_row)
17756 {
17757 Lisp_Object object = it->stack[it->sp - 1].string;
17758 if (!STRINGP (object))
17759 object = it->w->buffer;
17760 append_stretch_glyph (it, object, width, height, ascent);
17761 }
17762
17763 it->pixel_width = width;
17764 it->ascent = it->phys_ascent = height * ascent;
17765 it->descent = it->phys_descent = height - it->ascent;
17766 it->nglyphs = 1;
17767
17768 if (face->box != FACE_NO_BOX)
17769 {
17770 if (face->box_line_width > 0)
17771 {
17772 it->ascent += face->box_line_width;
17773 it->descent += face->box_line_width;
17774 }
17775
17776 if (it->start_of_box_run_p)
17777 it->pixel_width += abs (face->box_line_width);
17778 if (it->end_of_box_run_p)
17779 it->pixel_width += abs (face->box_line_width);
17780 }
17781
17782 take_vertical_position_into_account (it);
17783 }
17784
17785 /* RIF:
17786 Produce glyphs/get display metrics for the display element IT is
17787 loaded with. See the description of struct display_iterator in
17788 dispextern.h for an overview of struct display_iterator. */
17789
17790 void
17791 x_produce_glyphs (it)
17792 struct it *it;
17793 {
17794 it->glyph_not_available_p = 0;
17795
17796 if (it->what == IT_CHARACTER)
17797 {
17798 XChar2b char2b;
17799 XFontStruct *font;
17800 struct face *face = FACE_FROM_ID (it->f, it->face_id);
17801 XCharStruct *pcm;
17802 int font_not_found_p;
17803 struct font_info *font_info;
17804 int boff; /* baseline offset */
17805 /* We may change it->multibyte_p upon unibyte<->multibyte
17806 conversion. So, save the current value now and restore it
17807 later.
17808
17809 Note: It seems that we don't have to record multibyte_p in
17810 struct glyph because the character code itself tells if or
17811 not the character is multibyte. Thus, in the future, we must
17812 consider eliminating the field `multibyte_p' in the struct
17813 glyph. */
17814 int saved_multibyte_p = it->multibyte_p;
17815
17816 /* Maybe translate single-byte characters to multibyte, or the
17817 other way. */
17818 it->char_to_display = it->c;
17819 if (!ASCII_BYTE_P (it->c))
17820 {
17821 if (unibyte_display_via_language_environment
17822 && SINGLE_BYTE_CHAR_P (it->c)
17823 && (it->c >= 0240
17824 || !NILP (Vnonascii_translation_table)))
17825 {
17826 it->char_to_display = unibyte_char_to_multibyte (it->c);
17827 it->multibyte_p = 1;
17828 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
17829 face = FACE_FROM_ID (it->f, it->face_id);
17830 }
17831 else if (!SINGLE_BYTE_CHAR_P (it->c)
17832 && !it->multibyte_p)
17833 {
17834 it->multibyte_p = 1;
17835 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
17836 face = FACE_FROM_ID (it->f, it->face_id);
17837 }
17838 }
17839
17840 /* Get font to use. Encode IT->char_to_display. */
17841 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
17842 &char2b, it->multibyte_p, 0);
17843 font = face->font;
17844
17845 /* When no suitable font found, use the default font. */
17846 font_not_found_p = font == NULL;
17847 if (font_not_found_p)
17848 {
17849 font = FRAME_FONT (it->f);
17850 boff = FRAME_BASELINE_OFFSET (it->f);
17851 font_info = NULL;
17852 }
17853 else
17854 {
17855 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
17856 boff = font_info->baseline_offset;
17857 if (font_info->vertical_centering)
17858 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
17859 }
17860
17861 if (it->char_to_display >= ' '
17862 && (!it->multibyte_p || it->char_to_display < 128))
17863 {
17864 /* Either unibyte or ASCII. */
17865 int stretched_p;
17866
17867 it->nglyphs = 1;
17868
17869 pcm = rif->per_char_metric (font, &char2b,
17870 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
17871 it->ascent = FONT_BASE (font) + boff;
17872 it->descent = FONT_DESCENT (font) - boff;
17873
17874 if (pcm)
17875 {
17876 it->phys_ascent = pcm->ascent + boff;
17877 it->phys_descent = pcm->descent - boff;
17878 it->pixel_width = pcm->width;
17879 }
17880 else
17881 {
17882 it->glyph_not_available_p = 1;
17883 it->phys_ascent = FONT_BASE (font) + boff;
17884 it->phys_descent = FONT_DESCENT (font) - boff;
17885 it->pixel_width = FONT_WIDTH (font);
17886 }
17887
17888 /* If this is a space inside a region of text with
17889 `space-width' property, change its width. */
17890 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
17891 if (stretched_p)
17892 it->pixel_width *= XFLOATINT (it->space_width);
17893
17894 /* If face has a box, add the box thickness to the character
17895 height. If character has a box line to the left and/or
17896 right, add the box line width to the character's width. */
17897 if (face->box != FACE_NO_BOX)
17898 {
17899 int thick = face->box_line_width;
17900
17901 if (thick > 0)
17902 {
17903 it->ascent += thick;
17904 it->descent += thick;
17905 }
17906 else
17907 thick = -thick;
17908
17909 if (it->start_of_box_run_p)
17910 it->pixel_width += thick;
17911 if (it->end_of_box_run_p)
17912 it->pixel_width += thick;
17913 }
17914
17915 /* If face has an overline, add the height of the overline
17916 (1 pixel) and a 1 pixel margin to the character height. */
17917 if (face->overline_p)
17918 it->ascent += 2;
17919
17920 take_vertical_position_into_account (it);
17921
17922 /* If we have to actually produce glyphs, do it. */
17923 if (it->glyph_row)
17924 {
17925 if (stretched_p)
17926 {
17927 /* Translate a space with a `space-width' property
17928 into a stretch glyph. */
17929 double ascent = (double) FONT_BASE (font)
17930 / FONT_HEIGHT (font);
17931 append_stretch_glyph (it, it->object, it->pixel_width,
17932 it->ascent + it->descent, ascent);
17933 }
17934 else
17935 append_glyph (it);
17936
17937 /* If characters with lbearing or rbearing are displayed
17938 in this line, record that fact in a flag of the
17939 glyph row. This is used to optimize X output code. */
17940 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
17941 it->glyph_row->contains_overlapping_glyphs_p = 1;
17942 }
17943 }
17944 else if (it->char_to_display == '\n')
17945 {
17946 /* A newline has no width but we need the height of the line. */
17947 it->pixel_width = 0;
17948 it->nglyphs = 0;
17949 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
17950 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
17951
17952 if (face->box != FACE_NO_BOX
17953 && face->box_line_width > 0)
17954 {
17955 it->ascent += face->box_line_width;
17956 it->descent += face->box_line_width;
17957 }
17958 }
17959 else if (it->char_to_display == '\t')
17960 {
17961 int tab_width = it->tab_width * FRAME_COLUMN_WIDTH (it->f);
17962 int x = it->current_x + it->continuation_lines_width;
17963 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
17964
17965 /* If the distance from the current position to the next tab
17966 stop is less than a canonical character width, use the
17967 tab stop after that. */
17968 if (next_tab_x - x < FRAME_COLUMN_WIDTH (it->f))
17969 next_tab_x += tab_width;
17970
17971 it->pixel_width = next_tab_x - x;
17972 it->nglyphs = 1;
17973 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
17974 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
17975
17976 if (it->glyph_row)
17977 {
17978 double ascent = (double) it->ascent / (it->ascent + it->descent);
17979 append_stretch_glyph (it, it->object, it->pixel_width,
17980 it->ascent + it->descent, ascent);
17981 }
17982 }
17983 else
17984 {
17985 /* A multi-byte character. Assume that the display width of the
17986 character is the width of the character multiplied by the
17987 width of the font. */
17988
17989 /* If we found a font, this font should give us the right
17990 metrics. If we didn't find a font, use the frame's
17991 default font and calculate the width of the character
17992 from the charset width; this is what old redisplay code
17993 did. */
17994
17995 pcm = rif->per_char_metric (font, &char2b,
17996 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
17997
17998 if (font_not_found_p || !pcm)
17999 {
18000 int charset = CHAR_CHARSET (it->char_to_display);
18001
18002 it->glyph_not_available_p = 1;
18003 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
18004 * CHARSET_WIDTH (charset));
18005 it->phys_ascent = FONT_BASE (font) + boff;
18006 it->phys_descent = FONT_DESCENT (font) - boff;
18007 }
18008 else
18009 {
18010 it->pixel_width = pcm->width;
18011 it->phys_ascent = pcm->ascent + boff;
18012 it->phys_descent = pcm->descent - boff;
18013 if (it->glyph_row
18014 && (pcm->lbearing < 0
18015 || pcm->rbearing > pcm->width))
18016 it->glyph_row->contains_overlapping_glyphs_p = 1;
18017 }
18018 it->nglyphs = 1;
18019 it->ascent = FONT_BASE (font) + boff;
18020 it->descent = FONT_DESCENT (font) - boff;
18021 if (face->box != FACE_NO_BOX)
18022 {
18023 int thick = face->box_line_width;
18024
18025 if (thick > 0)
18026 {
18027 it->ascent += thick;
18028 it->descent += thick;
18029 }
18030 else
18031 thick = - thick;
18032
18033 if (it->start_of_box_run_p)
18034 it->pixel_width += thick;
18035 if (it->end_of_box_run_p)
18036 it->pixel_width += thick;
18037 }
18038
18039 /* If face has an overline, add the height of the overline
18040 (1 pixel) and a 1 pixel margin to the character height. */
18041 if (face->overline_p)
18042 it->ascent += 2;
18043
18044 take_vertical_position_into_account (it);
18045
18046 if (it->glyph_row)
18047 append_glyph (it);
18048 }
18049 it->multibyte_p = saved_multibyte_p;
18050 }
18051 else if (it->what == IT_COMPOSITION)
18052 {
18053 /* Note: A composition is represented as one glyph in the
18054 glyph matrix. There are no padding glyphs. */
18055 XChar2b char2b;
18056 XFontStruct *font;
18057 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18058 XCharStruct *pcm;
18059 int font_not_found_p;
18060 struct font_info *font_info;
18061 int boff; /* baseline offset */
18062 struct composition *cmp = composition_table[it->cmp_id];
18063
18064 /* Maybe translate single-byte characters to multibyte. */
18065 it->char_to_display = it->c;
18066 if (unibyte_display_via_language_environment
18067 && SINGLE_BYTE_CHAR_P (it->c)
18068 && (it->c >= 0240
18069 || (it->c >= 0200
18070 && !NILP (Vnonascii_translation_table))))
18071 {
18072 it->char_to_display = unibyte_char_to_multibyte (it->c);
18073 }
18074
18075 /* Get face and font to use. Encode IT->char_to_display. */
18076 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18077 face = FACE_FROM_ID (it->f, it->face_id);
18078 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18079 &char2b, it->multibyte_p, 0);
18080 font = face->font;
18081
18082 /* When no suitable font found, use the default font. */
18083 font_not_found_p = font == NULL;
18084 if (font_not_found_p)
18085 {
18086 font = FRAME_FONT (it->f);
18087 boff = FRAME_BASELINE_OFFSET (it->f);
18088 font_info = NULL;
18089 }
18090 else
18091 {
18092 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18093 boff = font_info->baseline_offset;
18094 if (font_info->vertical_centering)
18095 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18096 }
18097
18098 /* There are no padding glyphs, so there is only one glyph to
18099 produce for the composition. Important is that pixel_width,
18100 ascent and descent are the values of what is drawn by
18101 draw_glyphs (i.e. the values of the overall glyphs composed). */
18102 it->nglyphs = 1;
18103
18104 /* If we have not yet calculated pixel size data of glyphs of
18105 the composition for the current face font, calculate them
18106 now. Theoretically, we have to check all fonts for the
18107 glyphs, but that requires much time and memory space. So,
18108 here we check only the font of the first glyph. This leads
18109 to incorrect display very rarely, and C-l (recenter) can
18110 correct the display anyway. */
18111 if (cmp->font != (void *) font)
18112 {
18113 /* Ascent and descent of the font of the first character of
18114 this composition (adjusted by baseline offset). Ascent
18115 and descent of overall glyphs should not be less than
18116 them respectively. */
18117 int font_ascent = FONT_BASE (font) + boff;
18118 int font_descent = FONT_DESCENT (font) - boff;
18119 /* Bounding box of the overall glyphs. */
18120 int leftmost, rightmost, lowest, highest;
18121 int i, width, ascent, descent;
18122
18123 cmp->font = (void *) font;
18124
18125 /* Initialize the bounding box. */
18126 if (font_info
18127 && (pcm = rif->per_char_metric (font, &char2b,
18128 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
18129 {
18130 width = pcm->width;
18131 ascent = pcm->ascent;
18132 descent = pcm->descent;
18133 }
18134 else
18135 {
18136 width = FONT_WIDTH (font);
18137 ascent = FONT_BASE (font);
18138 descent = FONT_DESCENT (font);
18139 }
18140
18141 rightmost = width;
18142 lowest = - descent + boff;
18143 highest = ascent + boff;
18144 leftmost = 0;
18145
18146 if (font_info
18147 && font_info->default_ascent
18148 && CHAR_TABLE_P (Vuse_default_ascent)
18149 && !NILP (Faref (Vuse_default_ascent,
18150 make_number (it->char_to_display))))
18151 highest = font_info->default_ascent + boff;
18152
18153 /* Draw the first glyph at the normal position. It may be
18154 shifted to right later if some other glyphs are drawn at
18155 the left. */
18156 cmp->offsets[0] = 0;
18157 cmp->offsets[1] = boff;
18158
18159 /* Set cmp->offsets for the remaining glyphs. */
18160 for (i = 1; i < cmp->glyph_len; i++)
18161 {
18162 int left, right, btm, top;
18163 int ch = COMPOSITION_GLYPH (cmp, i);
18164 int face_id = FACE_FOR_CHAR (it->f, face, ch);
18165
18166 face = FACE_FROM_ID (it->f, face_id);
18167 get_char_face_and_encoding (it->f, ch, face->id,
18168 &char2b, it->multibyte_p, 0);
18169 font = face->font;
18170 if (font == NULL)
18171 {
18172 font = FRAME_FONT (it->f);
18173 boff = FRAME_BASELINE_OFFSET (it->f);
18174 font_info = NULL;
18175 }
18176 else
18177 {
18178 font_info
18179 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18180 boff = font_info->baseline_offset;
18181 if (font_info->vertical_centering)
18182 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18183 }
18184
18185 if (font_info
18186 && (pcm = rif->per_char_metric (font, &char2b,
18187 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
18188 {
18189 width = pcm->width;
18190 ascent = pcm->ascent;
18191 descent = pcm->descent;
18192 }
18193 else
18194 {
18195 width = FONT_WIDTH (font);
18196 ascent = 1;
18197 descent = 0;
18198 }
18199
18200 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
18201 {
18202 /* Relative composition with or without
18203 alternate chars. */
18204 left = (leftmost + rightmost - width) / 2;
18205 btm = - descent + boff;
18206 if (font_info && font_info->relative_compose
18207 && (! CHAR_TABLE_P (Vignore_relative_composition)
18208 || NILP (Faref (Vignore_relative_composition,
18209 make_number (ch)))))
18210 {
18211
18212 if (- descent >= font_info->relative_compose)
18213 /* One extra pixel between two glyphs. */
18214 btm = highest + 1;
18215 else if (ascent <= 0)
18216 /* One extra pixel between two glyphs. */
18217 btm = lowest - 1 - ascent - descent;
18218 }
18219 }
18220 else
18221 {
18222 /* A composition rule is specified by an integer
18223 value that encodes global and new reference
18224 points (GREF and NREF). GREF and NREF are
18225 specified by numbers as below:
18226
18227 0---1---2 -- ascent
18228 | |
18229 | |
18230 | |
18231 9--10--11 -- center
18232 | |
18233 ---3---4---5--- baseline
18234 | |
18235 6---7---8 -- descent
18236 */
18237 int rule = COMPOSITION_RULE (cmp, i);
18238 int gref, nref, grefx, grefy, nrefx, nrefy;
18239
18240 COMPOSITION_DECODE_RULE (rule, gref, nref);
18241 grefx = gref % 3, nrefx = nref % 3;
18242 grefy = gref / 3, nrefy = nref / 3;
18243
18244 left = (leftmost
18245 + grefx * (rightmost - leftmost) / 2
18246 - nrefx * width / 2);
18247 btm = ((grefy == 0 ? highest
18248 : grefy == 1 ? 0
18249 : grefy == 2 ? lowest
18250 : (highest + lowest) / 2)
18251 - (nrefy == 0 ? ascent + descent
18252 : nrefy == 1 ? descent - boff
18253 : nrefy == 2 ? 0
18254 : (ascent + descent) / 2));
18255 }
18256
18257 cmp->offsets[i * 2] = left;
18258 cmp->offsets[i * 2 + 1] = btm + descent;
18259
18260 /* Update the bounding box of the overall glyphs. */
18261 right = left + width;
18262 top = btm + descent + ascent;
18263 if (left < leftmost)
18264 leftmost = left;
18265 if (right > rightmost)
18266 rightmost = right;
18267 if (top > highest)
18268 highest = top;
18269 if (btm < lowest)
18270 lowest = btm;
18271 }
18272
18273 /* If there are glyphs whose x-offsets are negative,
18274 shift all glyphs to the right and make all x-offsets
18275 non-negative. */
18276 if (leftmost < 0)
18277 {
18278 for (i = 0; i < cmp->glyph_len; i++)
18279 cmp->offsets[i * 2] -= leftmost;
18280 rightmost -= leftmost;
18281 }
18282
18283 cmp->pixel_width = rightmost;
18284 cmp->ascent = highest;
18285 cmp->descent = - lowest;
18286 if (cmp->ascent < font_ascent)
18287 cmp->ascent = font_ascent;
18288 if (cmp->descent < font_descent)
18289 cmp->descent = font_descent;
18290 }
18291
18292 it->pixel_width = cmp->pixel_width;
18293 it->ascent = it->phys_ascent = cmp->ascent;
18294 it->descent = it->phys_descent = cmp->descent;
18295
18296 if (face->box != FACE_NO_BOX)
18297 {
18298 int thick = face->box_line_width;
18299
18300 if (thick > 0)
18301 {
18302 it->ascent += thick;
18303 it->descent += thick;
18304 }
18305 else
18306 thick = - thick;
18307
18308 if (it->start_of_box_run_p)
18309 it->pixel_width += thick;
18310 if (it->end_of_box_run_p)
18311 it->pixel_width += thick;
18312 }
18313
18314 /* If face has an overline, add the height of the overline
18315 (1 pixel) and a 1 pixel margin to the character height. */
18316 if (face->overline_p)
18317 it->ascent += 2;
18318
18319 take_vertical_position_into_account (it);
18320
18321 if (it->glyph_row)
18322 append_composite_glyph (it);
18323 }
18324 else if (it->what == IT_IMAGE)
18325 produce_image_glyph (it);
18326 else if (it->what == IT_STRETCH)
18327 produce_stretch_glyph (it);
18328
18329 /* Accumulate dimensions. Note: can't assume that it->descent > 0
18330 because this isn't true for images with `:ascent 100'. */
18331 xassert (it->ascent >= 0 && it->descent >= 0);
18332 if (it->area == TEXT_AREA)
18333 it->current_x += it->pixel_width;
18334
18335 it->descent += it->extra_line_spacing;
18336
18337 it->max_ascent = max (it->max_ascent, it->ascent);
18338 it->max_descent = max (it->max_descent, it->descent);
18339 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
18340 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
18341 }
18342
18343 /* EXPORT for RIF:
18344 Output LEN glyphs starting at START at the nominal cursor position.
18345 Advance the nominal cursor over the text. The global variable
18346 updated_window contains the window being updated, updated_row is
18347 the glyph row being updated, and updated_area is the area of that
18348 row being updated. */
18349
18350 void
18351 x_write_glyphs (start, len)
18352 struct glyph *start;
18353 int len;
18354 {
18355 int x, hpos;
18356
18357 xassert (updated_window && updated_row);
18358 BLOCK_INPUT;
18359
18360 /* Write glyphs. */
18361
18362 hpos = start - updated_row->glyphs[updated_area];
18363 x = draw_glyphs (updated_window, output_cursor.x,
18364 updated_row, updated_area,
18365 hpos, hpos + len,
18366 DRAW_NORMAL_TEXT, 0);
18367
18368 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
18369 if (updated_area == TEXT_AREA
18370 && updated_window->phys_cursor_on_p
18371 && updated_window->phys_cursor.vpos == output_cursor.vpos
18372 && updated_window->phys_cursor.hpos >= hpos
18373 && updated_window->phys_cursor.hpos < hpos + len)
18374 updated_window->phys_cursor_on_p = 0;
18375
18376 UNBLOCK_INPUT;
18377
18378 /* Advance the output cursor. */
18379 output_cursor.hpos += len;
18380 output_cursor.x = x;
18381 }
18382
18383
18384 /* EXPORT for RIF:
18385 Insert LEN glyphs from START at the nominal cursor position. */
18386
18387 void
18388 x_insert_glyphs (start, len)
18389 struct glyph *start;
18390 int len;
18391 {
18392 struct frame *f;
18393 struct window *w;
18394 int line_height, shift_by_width, shifted_region_width;
18395 struct glyph_row *row;
18396 struct glyph *glyph;
18397 int frame_x, frame_y, hpos;
18398
18399 xassert (updated_window && updated_row);
18400 BLOCK_INPUT;
18401 w = updated_window;
18402 f = XFRAME (WINDOW_FRAME (w));
18403
18404 /* Get the height of the line we are in. */
18405 row = updated_row;
18406 line_height = row->height;
18407
18408 /* Get the width of the glyphs to insert. */
18409 shift_by_width = 0;
18410 for (glyph = start; glyph < start + len; ++glyph)
18411 shift_by_width += glyph->pixel_width;
18412
18413 /* Get the width of the region to shift right. */
18414 shifted_region_width = (window_box_width (w, updated_area)
18415 - output_cursor.x
18416 - shift_by_width);
18417
18418 /* Shift right. */
18419 frame_x = window_box_left (w, updated_area) + output_cursor.x;
18420 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
18421
18422 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
18423 line_height, shift_by_width);
18424
18425 /* Write the glyphs. */
18426 hpos = start - row->glyphs[updated_area];
18427 draw_glyphs (w, output_cursor.x, row, updated_area,
18428 hpos, hpos + len,
18429 DRAW_NORMAL_TEXT, 0);
18430
18431 /* Advance the output cursor. */
18432 output_cursor.hpos += len;
18433 output_cursor.x += shift_by_width;
18434 UNBLOCK_INPUT;
18435 }
18436
18437
18438 /* EXPORT for RIF:
18439 Erase the current text line from the nominal cursor position
18440 (inclusive) to pixel column TO_X (exclusive). The idea is that
18441 everything from TO_X onward is already erased.
18442
18443 TO_X is a pixel position relative to updated_area of
18444 updated_window. TO_X == -1 means clear to the end of this area. */
18445
18446 void
18447 x_clear_end_of_line (to_x)
18448 int to_x;
18449 {
18450 struct frame *f;
18451 struct window *w = updated_window;
18452 int max_x, min_y, max_y;
18453 int from_x, from_y, to_y;
18454
18455 xassert (updated_window && updated_row);
18456 f = XFRAME (w->frame);
18457
18458 if (updated_row->full_width_p)
18459 max_x = WINDOW_TOTAL_WIDTH (w);
18460 else
18461 max_x = window_box_width (w, updated_area);
18462 max_y = window_text_bottom_y (w);
18463
18464 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
18465 of window. For TO_X > 0, truncate to end of drawing area. */
18466 if (to_x == 0)
18467 return;
18468 else if (to_x < 0)
18469 to_x = max_x;
18470 else
18471 to_x = min (to_x, max_x);
18472
18473 to_y = min (max_y, output_cursor.y + updated_row->height);
18474
18475 /* Notice if the cursor will be cleared by this operation. */
18476 if (!updated_row->full_width_p)
18477 notice_overwritten_cursor (w, updated_area,
18478 output_cursor.x, -1,
18479 updated_row->y,
18480 MATRIX_ROW_BOTTOM_Y (updated_row));
18481
18482 from_x = output_cursor.x;
18483
18484 /* Translate to frame coordinates. */
18485 if (updated_row->full_width_p)
18486 {
18487 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
18488 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
18489 }
18490 else
18491 {
18492 int area_left = window_box_left (w, updated_area);
18493 from_x += area_left;
18494 to_x += area_left;
18495 }
18496
18497 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
18498 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
18499 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
18500
18501 /* Prevent inadvertently clearing to end of the X window. */
18502 if (to_x > from_x && to_y > from_y)
18503 {
18504 BLOCK_INPUT;
18505 rif->clear_frame_area (f, from_x, from_y,
18506 to_x - from_x, to_y - from_y);
18507 UNBLOCK_INPUT;
18508 }
18509 }
18510
18511 #endif /* HAVE_WINDOW_SYSTEM */
18512
18513
18514 \f
18515 /***********************************************************************
18516 Cursor types
18517 ***********************************************************************/
18518
18519 /* Value is the internal representation of the specified cursor type
18520 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
18521 of the bar cursor. */
18522
18523 enum text_cursor_kinds
18524 get_specified_cursor_type (arg, width)
18525 Lisp_Object arg;
18526 int *width;
18527 {
18528 enum text_cursor_kinds type;
18529
18530 if (NILP (arg))
18531 return NO_CURSOR;
18532
18533 if (EQ (arg, Qbox))
18534 return FILLED_BOX_CURSOR;
18535
18536 if (EQ (arg, Qhollow))
18537 return HOLLOW_BOX_CURSOR;
18538
18539 if (EQ (arg, Qbar))
18540 {
18541 *width = 2;
18542 return BAR_CURSOR;
18543 }
18544
18545 if (CONSP (arg)
18546 && EQ (XCAR (arg), Qbar)
18547 && INTEGERP (XCDR (arg))
18548 && XINT (XCDR (arg)) >= 0)
18549 {
18550 *width = XINT (XCDR (arg));
18551 return BAR_CURSOR;
18552 }
18553
18554 if (EQ (arg, Qhbar))
18555 {
18556 *width = 2;
18557 return HBAR_CURSOR;
18558 }
18559
18560 if (CONSP (arg)
18561 && EQ (XCAR (arg), Qhbar)
18562 && INTEGERP (XCDR (arg))
18563 && XINT (XCDR (arg)) >= 0)
18564 {
18565 *width = XINT (XCDR (arg));
18566 return HBAR_CURSOR;
18567 }
18568
18569 /* Treat anything unknown as "hollow box cursor".
18570 It was bad to signal an error; people have trouble fixing
18571 .Xdefaults with Emacs, when it has something bad in it. */
18572 type = HOLLOW_BOX_CURSOR;
18573
18574 return type;
18575 }
18576
18577 /* Set the default cursor types for specified frame. */
18578 void
18579 set_frame_cursor_types (f, arg)
18580 struct frame *f;
18581 Lisp_Object arg;
18582 {
18583 int width;
18584 Lisp_Object tem;
18585
18586 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
18587 FRAME_CURSOR_WIDTH (f) = width;
18588
18589 /* By default, set up the blink-off state depending on the on-state. */
18590
18591 tem = Fassoc (arg, Vblink_cursor_alist);
18592 if (!NILP (tem))
18593 {
18594 FRAME_BLINK_OFF_CURSOR (f)
18595 = get_specified_cursor_type (XCDR (tem), &width);
18596 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
18597 }
18598 else
18599 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
18600 }
18601
18602
18603 /* Return the cursor we want to be displayed in window W. Return
18604 width of bar/hbar cursor through WIDTH arg. Return with
18605 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
18606 (i.e. if the `system caret' should track this cursor).
18607
18608 In a mini-buffer window, we want the cursor only to appear if we
18609 are reading input from this window. For the selected window, we
18610 want the cursor type given by the frame parameter or buffer local
18611 setting of cursor-type. If explicitly marked off, draw no cursor.
18612 In all other cases, we want a hollow box cursor. */
18613
18614 enum text_cursor_kinds
18615 get_window_cursor_type (w, width, active_cursor)
18616 struct window *w;
18617 int *width;
18618 int *active_cursor;
18619 {
18620 struct frame *f = XFRAME (w->frame);
18621 struct buffer *b = XBUFFER (w->buffer);
18622 int cursor_type = DEFAULT_CURSOR;
18623 Lisp_Object alt_cursor;
18624 int non_selected = 0;
18625
18626 *active_cursor = 1;
18627
18628 /* Echo area */
18629 if (cursor_in_echo_area
18630 && FRAME_HAS_MINIBUF_P (f)
18631 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
18632 {
18633 if (w == XWINDOW (echo_area_window))
18634 {
18635 *width = FRAME_CURSOR_WIDTH (f);
18636 return FRAME_DESIRED_CURSOR (f);
18637 }
18638
18639 *active_cursor = 0;
18640 non_selected = 1;
18641 }
18642
18643 /* Nonselected window or nonselected frame. */
18644 else if (w != XWINDOW (f->selected_window)
18645 #ifdef HAVE_WINDOW_SYSTEM
18646 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
18647 #endif
18648 )
18649 {
18650 *active_cursor = 0;
18651
18652 if (MINI_WINDOW_P (w) && minibuf_level == 0)
18653 return NO_CURSOR;
18654
18655 non_selected = 1;
18656 }
18657
18658 /* Never display a cursor in a window in which cursor-type is nil. */
18659 if (NILP (b->cursor_type))
18660 return NO_CURSOR;
18661
18662 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
18663 if (non_selected)
18664 {
18665 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
18666 return get_specified_cursor_type (alt_cursor, width);
18667 }
18668
18669 /* Get the normal cursor type for this window. */
18670 if (EQ (b->cursor_type, Qt))
18671 {
18672 cursor_type = FRAME_DESIRED_CURSOR (f);
18673 *width = FRAME_CURSOR_WIDTH (f);
18674 }
18675 else
18676 cursor_type = get_specified_cursor_type (b->cursor_type, width);
18677
18678 /* Use normal cursor if not blinked off. */
18679 if (!w->cursor_off_p)
18680 return cursor_type;
18681
18682 /* Cursor is blinked off, so determine how to "toggle" it. */
18683
18684 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
18685 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
18686 return get_specified_cursor_type (XCDR (alt_cursor), width);
18687
18688 /* Then see if frame has specified a specific blink off cursor type. */
18689 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
18690 {
18691 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
18692 return FRAME_BLINK_OFF_CURSOR (f);
18693 }
18694
18695 /* Finally perform built-in cursor blinking:
18696 filled box <-> hollow box
18697 wide [h]bar <-> narrow [h]bar
18698 narrow [h]bar <-> no cursor
18699 other type <-> no cursor */
18700
18701 if (cursor_type == FILLED_BOX_CURSOR)
18702 return HOLLOW_BOX_CURSOR;
18703
18704 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
18705 {
18706 *width = 1;
18707 return cursor_type;
18708 }
18709
18710 return NO_CURSOR;
18711 }
18712
18713
18714 #ifdef HAVE_WINDOW_SYSTEM
18715
18716 /* Notice when the text cursor of window W has been completely
18717 overwritten by a drawing operation that outputs glyphs in AREA
18718 starting at X0 and ending at X1 in the line starting at Y0 and
18719 ending at Y1. X coordinates are area-relative. X1 < 0 means all
18720 the rest of the line after X0 has been written. Y coordinates
18721 are window-relative. */
18722
18723 static void
18724 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
18725 struct window *w;
18726 enum glyph_row_area area;
18727 int x0, y0, x1, y1;
18728 {
18729 if (area == TEXT_AREA && w->phys_cursor_on_p)
18730 {
18731 int cx0 = w->phys_cursor.x;
18732 int cx1 = cx0 + w->phys_cursor_width;
18733 int cy0 = w->phys_cursor.y;
18734 int cy1 = cy0 + w->phys_cursor_height;
18735
18736 if (x0 <= cx0 && (x1 < 0 || x1 >= cx1))
18737 {
18738 /* The cursor image will be completely removed from the
18739 screen if the output area intersects the cursor area in
18740 y-direction. When we draw in [y0 y1[, and some part of
18741 the cursor is at y < y0, that part must have been drawn
18742 before. When scrolling, the cursor is erased before
18743 actually scrolling, so we don't come here. When not
18744 scrolling, the rows above the old cursor row must have
18745 changed, and in this case these rows must have written
18746 over the cursor image.
18747
18748 Likewise if part of the cursor is below y1, with the
18749 exception of the cursor being in the first blank row at
18750 the buffer and window end because update_text_area
18751 doesn't draw that row. (Except when it does, but
18752 that's handled in update_text_area.) */
18753
18754 if (((y0 >= cy0 && y0 < cy1) || (y1 > cy0 && y1 < cy1))
18755 && w->current_matrix->rows[w->phys_cursor.vpos].displays_text_p)
18756 w->phys_cursor_on_p = 0;
18757 }
18758 }
18759 }
18760
18761 #endif /* HAVE_WINDOW_SYSTEM */
18762
18763 \f
18764 /************************************************************************
18765 Mouse Face
18766 ************************************************************************/
18767
18768 #ifdef HAVE_WINDOW_SYSTEM
18769
18770 /* EXPORT for RIF:
18771 Fix the display of area AREA of overlapping row ROW in window W. */
18772
18773 void
18774 x_fix_overlapping_area (w, row, area)
18775 struct window *w;
18776 struct glyph_row *row;
18777 enum glyph_row_area area;
18778 {
18779 int i, x;
18780
18781 BLOCK_INPUT;
18782
18783 x = window_box_left_offset (w, area);
18784 if (area == TEXT_AREA)
18785 x += row->x;
18786
18787 for (i = 0; i < row->used[area];)
18788 {
18789 if (row->glyphs[area][i].overlaps_vertically_p)
18790 {
18791 int start = i, start_x = x;
18792
18793 do
18794 {
18795 x += row->glyphs[area][i].pixel_width;
18796 ++i;
18797 }
18798 while (i < row->used[area]
18799 && row->glyphs[area][i].overlaps_vertically_p);
18800
18801 draw_glyphs (w, start_x, row, area,
18802 start, i,
18803 DRAW_NORMAL_TEXT, 1);
18804 }
18805 else
18806 {
18807 x += row->glyphs[area][i].pixel_width;
18808 ++i;
18809 }
18810 }
18811
18812 UNBLOCK_INPUT;
18813 }
18814
18815
18816 /* EXPORT:
18817 Draw the cursor glyph of window W in glyph row ROW. See the
18818 comment of draw_glyphs for the meaning of HL. */
18819
18820 void
18821 draw_phys_cursor_glyph (w, row, hl)
18822 struct window *w;
18823 struct glyph_row *row;
18824 enum draw_glyphs_face hl;
18825 {
18826 /* If cursor hpos is out of bounds, don't draw garbage. This can
18827 happen in mini-buffer windows when switching between echo area
18828 glyphs and mini-buffer. */
18829 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
18830 {
18831 int on_p = w->phys_cursor_on_p;
18832 int x1;
18833 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
18834 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
18835 hl, 0);
18836 w->phys_cursor_on_p = on_p;
18837
18838 if (hl == DRAW_CURSOR)
18839 w->phys_cursor_width = x1 - w->phys_cursor.x;
18840 /* When we erase the cursor, and ROW is overlapped by other
18841 rows, make sure that these overlapping parts of other rows
18842 are redrawn. */
18843 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
18844 {
18845 if (row > w->current_matrix->rows
18846 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
18847 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
18848
18849 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
18850 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
18851 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
18852 }
18853 }
18854 }
18855
18856
18857 /* EXPORT:
18858 Erase the image of a cursor of window W from the screen. */
18859
18860 void
18861 erase_phys_cursor (w)
18862 struct window *w;
18863 {
18864 struct frame *f = XFRAME (w->frame);
18865 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
18866 int hpos = w->phys_cursor.hpos;
18867 int vpos = w->phys_cursor.vpos;
18868 int mouse_face_here_p = 0;
18869 struct glyph_matrix *active_glyphs = w->current_matrix;
18870 struct glyph_row *cursor_row;
18871 struct glyph *cursor_glyph;
18872 enum draw_glyphs_face hl;
18873
18874 /* No cursor displayed or row invalidated => nothing to do on the
18875 screen. */
18876 if (w->phys_cursor_type == NO_CURSOR)
18877 goto mark_cursor_off;
18878
18879 /* VPOS >= active_glyphs->nrows means that window has been resized.
18880 Don't bother to erase the cursor. */
18881 if (vpos >= active_glyphs->nrows)
18882 goto mark_cursor_off;
18883
18884 /* If row containing cursor is marked invalid, there is nothing we
18885 can do. */
18886 cursor_row = MATRIX_ROW (active_glyphs, vpos);
18887 if (!cursor_row->enabled_p)
18888 goto mark_cursor_off;
18889
18890 /* If row is completely invisible, don't attempt to delete a cursor which
18891 isn't there. This can happen if cursor is at top of a window, and
18892 we switch to a buffer with a header line in that window. */
18893 if (cursor_row->visible_height <= 0)
18894 goto mark_cursor_off;
18895
18896 /* This can happen when the new row is shorter than the old one.
18897 In this case, either draw_glyphs or clear_end_of_line
18898 should have cleared the cursor. Note that we wouldn't be
18899 able to erase the cursor in this case because we don't have a
18900 cursor glyph at hand. */
18901 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
18902 goto mark_cursor_off;
18903
18904 /* If the cursor is in the mouse face area, redisplay that when
18905 we clear the cursor. */
18906 if (! NILP (dpyinfo->mouse_face_window)
18907 && w == XWINDOW (dpyinfo->mouse_face_window)
18908 && (vpos > dpyinfo->mouse_face_beg_row
18909 || (vpos == dpyinfo->mouse_face_beg_row
18910 && hpos >= dpyinfo->mouse_face_beg_col))
18911 && (vpos < dpyinfo->mouse_face_end_row
18912 || (vpos == dpyinfo->mouse_face_end_row
18913 && hpos < dpyinfo->mouse_face_end_col))
18914 /* Don't redraw the cursor's spot in mouse face if it is at the
18915 end of a line (on a newline). The cursor appears there, but
18916 mouse highlighting does not. */
18917 && cursor_row->used[TEXT_AREA] > hpos)
18918 mouse_face_here_p = 1;
18919
18920 /* Maybe clear the display under the cursor. */
18921 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
18922 {
18923 int x, y;
18924 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
18925
18926 cursor_glyph = get_phys_cursor_glyph (w);
18927 if (cursor_glyph == NULL)
18928 goto mark_cursor_off;
18929
18930 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
18931 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
18932
18933 rif->clear_frame_area (f, x, y,
18934 cursor_glyph->pixel_width, cursor_row->visible_height);
18935 }
18936
18937 /* Erase the cursor by redrawing the character underneath it. */
18938 if (mouse_face_here_p)
18939 hl = DRAW_MOUSE_FACE;
18940 else
18941 hl = DRAW_NORMAL_TEXT;
18942 draw_phys_cursor_glyph (w, cursor_row, hl);
18943
18944 mark_cursor_off:
18945 w->phys_cursor_on_p = 0;
18946 w->phys_cursor_type = NO_CURSOR;
18947 }
18948
18949
18950 /* EXPORT:
18951 Display or clear cursor of window W. If ON is zero, clear the
18952 cursor. If it is non-zero, display the cursor. If ON is nonzero,
18953 where to put the cursor is specified by HPOS, VPOS, X and Y. */
18954
18955 void
18956 display_and_set_cursor (w, on, hpos, vpos, x, y)
18957 struct window *w;
18958 int on, hpos, vpos, x, y;
18959 {
18960 struct frame *f = XFRAME (w->frame);
18961 int new_cursor_type;
18962 int new_cursor_width;
18963 int active_cursor;
18964 struct glyph_matrix *current_glyphs;
18965 struct glyph_row *glyph_row;
18966 struct glyph *glyph;
18967
18968 /* This is pointless on invisible frames, and dangerous on garbaged
18969 windows and frames; in the latter case, the frame or window may
18970 be in the midst of changing its size, and x and y may be off the
18971 window. */
18972 if (! FRAME_VISIBLE_P (f)
18973 || FRAME_GARBAGED_P (f)
18974 || vpos >= w->current_matrix->nrows
18975 || hpos >= w->current_matrix->matrix_w)
18976 return;
18977
18978 /* If cursor is off and we want it off, return quickly. */
18979 if (!on && !w->phys_cursor_on_p)
18980 return;
18981
18982 current_glyphs = w->current_matrix;
18983 glyph_row = MATRIX_ROW (current_glyphs, vpos);
18984 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
18985
18986 /* If cursor row is not enabled, we don't really know where to
18987 display the cursor. */
18988 if (!glyph_row->enabled_p)
18989 {
18990 w->phys_cursor_on_p = 0;
18991 return;
18992 }
18993
18994 xassert (interrupt_input_blocked);
18995
18996 /* Set new_cursor_type to the cursor we want to be displayed. */
18997 new_cursor_type = get_window_cursor_type (w, &new_cursor_width, &active_cursor);
18998
18999 /* If cursor is currently being shown and we don't want it to be or
19000 it is in the wrong place, or the cursor type is not what we want,
19001 erase it. */
19002 if (w->phys_cursor_on_p
19003 && (!on
19004 || w->phys_cursor.x != x
19005 || w->phys_cursor.y != y
19006 || new_cursor_type != w->phys_cursor_type
19007 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
19008 && new_cursor_width != w->phys_cursor_width)))
19009 erase_phys_cursor (w);
19010
19011 /* Don't check phys_cursor_on_p here because that flag is only set
19012 to zero in some cases where we know that the cursor has been
19013 completely erased, to avoid the extra work of erasing the cursor
19014 twice. In other words, phys_cursor_on_p can be 1 and the cursor
19015 still not be visible, or it has only been partly erased. */
19016 if (on)
19017 {
19018 w->phys_cursor_ascent = glyph_row->ascent;
19019 w->phys_cursor_height = glyph_row->height;
19020
19021 /* Set phys_cursor_.* before x_draw_.* is called because some
19022 of them may need the information. */
19023 w->phys_cursor.x = x;
19024 w->phys_cursor.y = glyph_row->y;
19025 w->phys_cursor.hpos = hpos;
19026 w->phys_cursor.vpos = vpos;
19027 }
19028
19029 rif->draw_window_cursor (w, glyph_row, x, y,
19030 new_cursor_type, new_cursor_width,
19031 on, active_cursor);
19032 }
19033
19034
19035 /* Switch the display of W's cursor on or off, according to the value
19036 of ON. */
19037
19038 static void
19039 update_window_cursor (w, on)
19040 struct window *w;
19041 int on;
19042 {
19043 /* Don't update cursor in windows whose frame is in the process
19044 of being deleted. */
19045 if (w->current_matrix)
19046 {
19047 BLOCK_INPUT;
19048 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
19049 w->phys_cursor.x, w->phys_cursor.y);
19050 UNBLOCK_INPUT;
19051 }
19052 }
19053
19054
19055 /* Call update_window_cursor with parameter ON_P on all leaf windows
19056 in the window tree rooted at W. */
19057
19058 static void
19059 update_cursor_in_window_tree (w, on_p)
19060 struct window *w;
19061 int on_p;
19062 {
19063 while (w)
19064 {
19065 if (!NILP (w->hchild))
19066 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
19067 else if (!NILP (w->vchild))
19068 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
19069 else
19070 update_window_cursor (w, on_p);
19071
19072 w = NILP (w->next) ? 0 : XWINDOW (w->next);
19073 }
19074 }
19075
19076
19077 /* EXPORT:
19078 Display the cursor on window W, or clear it, according to ON_P.
19079 Don't change the cursor's position. */
19080
19081 void
19082 x_update_cursor (f, on_p)
19083 struct frame *f;
19084 int on_p;
19085 {
19086 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
19087 }
19088
19089
19090 /* EXPORT:
19091 Clear the cursor of window W to background color, and mark the
19092 cursor as not shown. This is used when the text where the cursor
19093 is is about to be rewritten. */
19094
19095 void
19096 x_clear_cursor (w)
19097 struct window *w;
19098 {
19099 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
19100 update_window_cursor (w, 0);
19101 }
19102
19103
19104 /* EXPORT:
19105 Display the active region described by mouse_face_* according to DRAW. */
19106
19107 void
19108 show_mouse_face (dpyinfo, draw)
19109 Display_Info *dpyinfo;
19110 enum draw_glyphs_face draw;
19111 {
19112 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
19113 struct frame *f = XFRAME (WINDOW_FRAME (w));
19114
19115 if (/* If window is in the process of being destroyed, don't bother
19116 to do anything. */
19117 w->current_matrix != NULL
19118 /* Don't update mouse highlight if hidden */
19119 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
19120 /* Recognize when we are called to operate on rows that don't exist
19121 anymore. This can happen when a window is split. */
19122 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
19123 {
19124 int phys_cursor_on_p = w->phys_cursor_on_p;
19125 struct glyph_row *row, *first, *last;
19126
19127 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
19128 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
19129
19130 for (row = first; row <= last && row->enabled_p; ++row)
19131 {
19132 int start_hpos, end_hpos, start_x;
19133
19134 /* For all but the first row, the highlight starts at column 0. */
19135 if (row == first)
19136 {
19137 start_hpos = dpyinfo->mouse_face_beg_col;
19138 start_x = dpyinfo->mouse_face_beg_x;
19139 }
19140 else
19141 {
19142 start_hpos = 0;
19143 start_x = 0;
19144 }
19145
19146 if (row == last)
19147 end_hpos = dpyinfo->mouse_face_end_col;
19148 else
19149 end_hpos = row->used[TEXT_AREA];
19150
19151 if (end_hpos > start_hpos)
19152 {
19153 draw_glyphs (w, start_x, row, TEXT_AREA,
19154 start_hpos, end_hpos,
19155 draw, 0);
19156
19157 row->mouse_face_p
19158 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
19159 }
19160 }
19161
19162 /* When we've written over the cursor, arrange for it to
19163 be displayed again. */
19164 if (phys_cursor_on_p && !w->phys_cursor_on_p)
19165 {
19166 BLOCK_INPUT;
19167 display_and_set_cursor (w, 1,
19168 w->phys_cursor.hpos, w->phys_cursor.vpos,
19169 w->phys_cursor.x, w->phys_cursor.y);
19170 UNBLOCK_INPUT;
19171 }
19172 }
19173
19174 /* Change the mouse cursor. */
19175 if (draw == DRAW_NORMAL_TEXT)
19176 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
19177 else if (draw == DRAW_MOUSE_FACE)
19178 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
19179 else
19180 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
19181 }
19182
19183 /* EXPORT:
19184 Clear out the mouse-highlighted active region.
19185 Redraw it un-highlighted first. Value is non-zero if mouse
19186 face was actually drawn unhighlighted. */
19187
19188 int
19189 clear_mouse_face (dpyinfo)
19190 Display_Info *dpyinfo;
19191 {
19192 int cleared = 0;
19193
19194 if (!NILP (dpyinfo->mouse_face_window))
19195 {
19196 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
19197 cleared = 1;
19198 }
19199
19200 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
19201 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
19202 dpyinfo->mouse_face_window = Qnil;
19203 dpyinfo->mouse_face_overlay = Qnil;
19204 return cleared;
19205 }
19206
19207
19208 /* EXPORT:
19209 Non-zero if physical cursor of window W is within mouse face. */
19210
19211 int
19212 cursor_in_mouse_face_p (w)
19213 struct window *w;
19214 {
19215 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
19216 int in_mouse_face = 0;
19217
19218 if (WINDOWP (dpyinfo->mouse_face_window)
19219 && XWINDOW (dpyinfo->mouse_face_window) == w)
19220 {
19221 int hpos = w->phys_cursor.hpos;
19222 int vpos = w->phys_cursor.vpos;
19223
19224 if (vpos >= dpyinfo->mouse_face_beg_row
19225 && vpos <= dpyinfo->mouse_face_end_row
19226 && (vpos > dpyinfo->mouse_face_beg_row
19227 || hpos >= dpyinfo->mouse_face_beg_col)
19228 && (vpos < dpyinfo->mouse_face_end_row
19229 || hpos < dpyinfo->mouse_face_end_col
19230 || dpyinfo->mouse_face_past_end))
19231 in_mouse_face = 1;
19232 }
19233
19234 return in_mouse_face;
19235 }
19236
19237
19238
19239 \f
19240 /* Find the glyph matrix position of buffer position CHARPOS in window
19241 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
19242 current glyphs must be up to date. If CHARPOS is above window
19243 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
19244 of last line in W. In the row containing CHARPOS, stop before glyphs
19245 having STOP as object. */
19246
19247 #if 0 /* This is a version of fast_find_position that's more correct
19248 in the presence of hscrolling, for example. I didn't install
19249 it right away because the problem fixed is minor, it failed
19250 in 20.x as well, and I think it's too risky to install
19251 so near the release of 21.1. 2001-09-25 gerd. */
19252
19253 static int
19254 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
19255 struct window *w;
19256 int charpos;
19257 int *hpos, *vpos, *x, *y;
19258 Lisp_Object stop;
19259 {
19260 struct glyph_row *row, *first;
19261 struct glyph *glyph, *end;
19262 int i, past_end = 0;
19263
19264 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19265 row = row_containing_pos (w, charpos, first, NULL, 0);
19266 if (row == NULL)
19267 {
19268 if (charpos < MATRIX_ROW_START_CHARPOS (first))
19269 {
19270 *x = *y = *hpos = *vpos = 0;
19271 return 0;
19272 }
19273 else
19274 {
19275 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
19276 past_end = 1;
19277 }
19278 }
19279
19280 *x = row->x;
19281 *y = row->y;
19282 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
19283
19284 glyph = row->glyphs[TEXT_AREA];
19285 end = glyph + row->used[TEXT_AREA];
19286
19287 /* Skip over glyphs not having an object at the start of the row.
19288 These are special glyphs like truncation marks on terminal
19289 frames. */
19290 if (row->displays_text_p)
19291 while (glyph < end
19292 && INTEGERP (glyph->object)
19293 && !EQ (stop, glyph->object)
19294 && glyph->charpos < 0)
19295 {
19296 *x += glyph->pixel_width;
19297 ++glyph;
19298 }
19299
19300 while (glyph < end
19301 && !INTEGERP (glyph->object)
19302 && !EQ (stop, glyph->object)
19303 && (!BUFFERP (glyph->object)
19304 || glyph->charpos < charpos))
19305 {
19306 *x += glyph->pixel_width;
19307 ++glyph;
19308 }
19309
19310 *hpos = glyph - row->glyphs[TEXT_AREA];
19311 return past_end;
19312 }
19313
19314 #else /* not 0 */
19315
19316 static int
19317 fast_find_position (w, pos, hpos, vpos, x, y, stop)
19318 struct window *w;
19319 int pos;
19320 int *hpos, *vpos, *x, *y;
19321 Lisp_Object stop;
19322 {
19323 int i;
19324 int lastcol;
19325 int maybe_next_line_p = 0;
19326 int line_start_position;
19327 int yb = window_text_bottom_y (w);
19328 struct glyph_row *row, *best_row;
19329 int row_vpos, best_row_vpos;
19330 int current_x;
19331
19332 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19333 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
19334
19335 while (row->y < yb)
19336 {
19337 if (row->used[TEXT_AREA])
19338 line_start_position = row->glyphs[TEXT_AREA]->charpos;
19339 else
19340 line_start_position = 0;
19341
19342 if (line_start_position > pos)
19343 break;
19344 /* If the position sought is the end of the buffer,
19345 don't include the blank lines at the bottom of the window. */
19346 else if (line_start_position == pos
19347 && pos == BUF_ZV (XBUFFER (w->buffer)))
19348 {
19349 maybe_next_line_p = 1;
19350 break;
19351 }
19352 else if (line_start_position > 0)
19353 {
19354 best_row = row;
19355 best_row_vpos = row_vpos;
19356 }
19357
19358 if (row->y + row->height >= yb)
19359 break;
19360
19361 ++row;
19362 ++row_vpos;
19363 }
19364
19365 /* Find the right column within BEST_ROW. */
19366 lastcol = 0;
19367 current_x = best_row->x;
19368 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
19369 {
19370 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
19371 int charpos = glyph->charpos;
19372
19373 if (BUFFERP (glyph->object))
19374 {
19375 if (charpos == pos)
19376 {
19377 *hpos = i;
19378 *vpos = best_row_vpos;
19379 *x = current_x;
19380 *y = best_row->y;
19381 return 1;
19382 }
19383 else if (charpos > pos)
19384 break;
19385 }
19386 else if (EQ (glyph->object, stop))
19387 break;
19388
19389 if (charpos > 0)
19390 lastcol = i;
19391 current_x += glyph->pixel_width;
19392 }
19393
19394 /* If we're looking for the end of the buffer,
19395 and we didn't find it in the line we scanned,
19396 use the start of the following line. */
19397 if (maybe_next_line_p)
19398 {
19399 ++best_row;
19400 ++best_row_vpos;
19401 lastcol = 0;
19402 current_x = best_row->x;
19403 }
19404
19405 *vpos = best_row_vpos;
19406 *hpos = lastcol + 1;
19407 *x = current_x;
19408 *y = best_row->y;
19409 return 0;
19410 }
19411
19412 #endif /* not 0 */
19413
19414
19415 /* Find the position of the glyph for position POS in OBJECT in
19416 window W's current matrix, and return in *X, *Y the pixel
19417 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
19418
19419 RIGHT_P non-zero means return the position of the right edge of the
19420 glyph, RIGHT_P zero means return the left edge position.
19421
19422 If no glyph for POS exists in the matrix, return the position of
19423 the glyph with the next smaller position that is in the matrix, if
19424 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
19425 exists in the matrix, return the position of the glyph with the
19426 next larger position in OBJECT.
19427
19428 Value is non-zero if a glyph was found. */
19429
19430 static int
19431 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
19432 struct window *w;
19433 int pos;
19434 Lisp_Object object;
19435 int *hpos, *vpos, *x, *y;
19436 int right_p;
19437 {
19438 int yb = window_text_bottom_y (w);
19439 struct glyph_row *r;
19440 struct glyph *best_glyph = NULL;
19441 struct glyph_row *best_row = NULL;
19442 int best_x = 0;
19443
19444 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19445 r->enabled_p && r->y < yb;
19446 ++r)
19447 {
19448 struct glyph *g = r->glyphs[TEXT_AREA];
19449 struct glyph *e = g + r->used[TEXT_AREA];
19450 int gx;
19451
19452 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
19453 if (EQ (g->object, object))
19454 {
19455 if (g->charpos == pos)
19456 {
19457 best_glyph = g;
19458 best_x = gx;
19459 best_row = r;
19460 goto found;
19461 }
19462 else if (best_glyph == NULL
19463 || ((abs (g->charpos - pos)
19464 < abs (best_glyph->charpos - pos))
19465 && (right_p
19466 ? g->charpos < pos
19467 : g->charpos > pos)))
19468 {
19469 best_glyph = g;
19470 best_x = gx;
19471 best_row = r;
19472 }
19473 }
19474 }
19475
19476 found:
19477
19478 if (best_glyph)
19479 {
19480 *x = best_x;
19481 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
19482
19483 if (right_p)
19484 {
19485 *x += best_glyph->pixel_width;
19486 ++*hpos;
19487 }
19488
19489 *y = best_row->y;
19490 *vpos = best_row - w->current_matrix->rows;
19491 }
19492
19493 return best_glyph != NULL;
19494 }
19495
19496
19497 /* Take proper action when mouse has moved to the mode or header line
19498 or marginal area AREA of window W, x-position X and y-position Y.
19499 X is relative to the start of the text display area of W, so the
19500 width of bitmap areas and scroll bars must be subtracted to get a
19501 position relative to the start of the mode line. */
19502
19503 static void
19504 note_mode_line_or_margin_highlight (w, x, y, area)
19505 struct window *w;
19506 int x, y;
19507 enum window_part area;
19508 {
19509 struct frame *f = XFRAME (w->frame);
19510 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
19511 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
19512 int charpos;
19513 Lisp_Object string, help, map, pos;
19514
19515 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
19516 string = mode_line_string (w, x, y, area, &charpos);
19517 else
19518 string = marginal_area_string (w, x, y, area, &charpos);
19519
19520 if (STRINGP (string))
19521 {
19522 pos = make_number (charpos);
19523
19524 /* If we're on a string with `help-echo' text property, arrange
19525 for the help to be displayed. This is done by setting the
19526 global variable help_echo_string to the help string. */
19527 help = Fget_text_property (pos, Qhelp_echo, string);
19528 if (!NILP (help))
19529 {
19530 help_echo_string = help;
19531 XSETWINDOW (help_echo_window, w);
19532 help_echo_object = string;
19533 help_echo_pos = charpos;
19534 }
19535
19536 /* Change the mouse pointer according to what is under X/Y. */
19537 map = Fget_text_property (pos, Qlocal_map, string);
19538 if (!KEYMAPP (map))
19539 map = Fget_text_property (pos, Qkeymap, string);
19540 if (KEYMAPP (map))
19541 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
19542 }
19543
19544 rif->define_frame_cursor (f, cursor);
19545 }
19546
19547
19548 /* EXPORT:
19549 Take proper action when the mouse has moved to position X, Y on
19550 frame F as regards highlighting characters that have mouse-face
19551 properties. Also de-highlighting chars where the mouse was before.
19552 X and Y can be negative or out of range. */
19553
19554 void
19555 note_mouse_highlight (f, x, y)
19556 struct frame *f;
19557 int x, y;
19558 {
19559 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
19560 enum window_part part;
19561 Lisp_Object window;
19562 struct window *w;
19563 Cursor cursor = No_Cursor;
19564 struct buffer *b;
19565
19566 /* When a menu is active, don't highlight because this looks odd. */
19567 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
19568 if (popup_activated ())
19569 return;
19570 #endif
19571
19572 if (NILP (Vmouse_highlight)
19573 || !f->glyphs_initialized_p)
19574 return;
19575
19576 dpyinfo->mouse_face_mouse_x = x;
19577 dpyinfo->mouse_face_mouse_y = y;
19578 dpyinfo->mouse_face_mouse_frame = f;
19579
19580 if (dpyinfo->mouse_face_defer)
19581 return;
19582
19583 if (gc_in_progress)
19584 {
19585 dpyinfo->mouse_face_deferred_gc = 1;
19586 return;
19587 }
19588
19589 /* Which window is that in? */
19590 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
19591
19592 /* If we were displaying active text in another window, clear that. */
19593 if (! EQ (window, dpyinfo->mouse_face_window))
19594 clear_mouse_face (dpyinfo);
19595
19596 /* Not on a window -> return. */
19597 if (!WINDOWP (window))
19598 return;
19599
19600 /* Reset help_echo_string. It will get recomputed below. */
19601 /* ++KFS: X version didn't do this, but it looks harmless. */
19602 help_echo_string = Qnil;
19603
19604 /* Convert to window-relative pixel coordinates. */
19605 w = XWINDOW (window);
19606 frame_to_window_pixel_xy (w, &x, &y);
19607
19608 /* Handle tool-bar window differently since it doesn't display a
19609 buffer. */
19610 if (EQ (window, f->tool_bar_window))
19611 {
19612 note_tool_bar_highlight (f, x, y);
19613 return;
19614 }
19615
19616 /* Mouse is on the mode, header line or margin? */
19617 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
19618 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
19619 {
19620 note_mode_line_or_margin_highlight (w, x, y, part);
19621 return;
19622 }
19623
19624 if (part == ON_VERTICAL_BORDER)
19625 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
19626 else
19627 cursor = FRAME_X_OUTPUT (f)->text_cursor;
19628
19629 /* Are we in a window whose display is up to date?
19630 And verify the buffer's text has not changed. */
19631 b = XBUFFER (w->buffer);
19632 if (part == ON_TEXT
19633 && EQ (w->window_end_valid, w->buffer)
19634 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
19635 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
19636 {
19637 int hpos, vpos, pos, i, area;
19638 struct glyph *glyph;
19639 Lisp_Object object;
19640 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
19641 Lisp_Object *overlay_vec = NULL;
19642 int len, noverlays;
19643 struct buffer *obuf;
19644 int obegv, ozv, same_region;
19645
19646 /* Find the glyph under X/Y. */
19647 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area, 0);
19648
19649 /* Clear mouse face if X/Y not over text. */
19650 if (glyph == NULL
19651 || area != TEXT_AREA
19652 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
19653 {
19654 #if defined (HAVE_NTGUI)
19655 /* ++KFS: Why is this necessary on W32 ? */
19656 clear_mouse_face (dpyinfo);
19657 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
19658 #else
19659 if (clear_mouse_face (dpyinfo))
19660 cursor = No_Cursor;
19661 #endif
19662 goto set_cursor;
19663 }
19664
19665 pos = glyph->charpos;
19666 object = glyph->object;
19667 if (!STRINGP (object) && !BUFFERP (object))
19668 goto set_cursor;
19669
19670 /* If we get an out-of-range value, return now; avoid an error. */
19671 if (BUFFERP (object) && pos > BUF_Z (b))
19672 goto set_cursor;
19673
19674 /* Make the window's buffer temporarily current for
19675 overlays_at and compute_char_face. */
19676 obuf = current_buffer;
19677 current_buffer = b;
19678 obegv = BEGV;
19679 ozv = ZV;
19680 BEGV = BEG;
19681 ZV = Z;
19682
19683 /* Is this char mouse-active or does it have help-echo? */
19684 position = make_number (pos);
19685
19686 if (BUFFERP (object))
19687 {
19688 /* Put all the overlays we want in a vector in overlay_vec.
19689 Store the length in len. If there are more than 10, make
19690 enough space for all, and try again. */
19691 len = 10;
19692 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
19693 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
19694 if (noverlays > len)
19695 {
19696 len = noverlays;
19697 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
19698 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
19699 }
19700
19701 /* Sort overlays into increasing priority order. */
19702 noverlays = sort_overlays (overlay_vec, noverlays, w);
19703 }
19704 else
19705 noverlays = 0;
19706
19707 same_region = (EQ (window, dpyinfo->mouse_face_window)
19708 && vpos >= dpyinfo->mouse_face_beg_row
19709 && vpos <= dpyinfo->mouse_face_end_row
19710 && (vpos > dpyinfo->mouse_face_beg_row
19711 || hpos >= dpyinfo->mouse_face_beg_col)
19712 && (vpos < dpyinfo->mouse_face_end_row
19713 || hpos < dpyinfo->mouse_face_end_col
19714 || dpyinfo->mouse_face_past_end));
19715
19716 if (same_region)
19717 cursor = No_Cursor;
19718
19719 /* Check mouse-face highlighting. */
19720 if (! same_region
19721 /* If there exists an overlay with mouse-face overlapping
19722 the one we are currently highlighting, we have to
19723 check if we enter the overlapping overlay, and then
19724 highlight only that. */
19725 || (OVERLAYP (dpyinfo->mouse_face_overlay)
19726 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
19727 {
19728 /* Find the highest priority overlay that has a mouse-face
19729 property. */
19730 overlay = Qnil;
19731 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
19732 {
19733 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
19734 if (!NILP (mouse_face))
19735 overlay = overlay_vec[i];
19736 }
19737
19738 /* If we're actually highlighting the same overlay as
19739 before, there's no need to do that again. */
19740 if (!NILP (overlay)
19741 && EQ (overlay, dpyinfo->mouse_face_overlay))
19742 goto check_help_echo;
19743
19744 dpyinfo->mouse_face_overlay = overlay;
19745
19746 /* Clear the display of the old active region, if any. */
19747 if (clear_mouse_face (dpyinfo))
19748 cursor = No_Cursor;
19749
19750 /* If no overlay applies, get a text property. */
19751 if (NILP (overlay))
19752 mouse_face = Fget_text_property (position, Qmouse_face, object);
19753
19754 /* Handle the overlay case. */
19755 if (!NILP (overlay))
19756 {
19757 /* Find the range of text around this char that
19758 should be active. */
19759 Lisp_Object before, after;
19760 int ignore;
19761
19762 before = Foverlay_start (overlay);
19763 after = Foverlay_end (overlay);
19764 /* Record this as the current active region. */
19765 fast_find_position (w, XFASTINT (before),
19766 &dpyinfo->mouse_face_beg_col,
19767 &dpyinfo->mouse_face_beg_row,
19768 &dpyinfo->mouse_face_beg_x,
19769 &dpyinfo->mouse_face_beg_y, Qnil);
19770
19771 dpyinfo->mouse_face_past_end
19772 = !fast_find_position (w, XFASTINT (after),
19773 &dpyinfo->mouse_face_end_col,
19774 &dpyinfo->mouse_face_end_row,
19775 &dpyinfo->mouse_face_end_x,
19776 &dpyinfo->mouse_face_end_y, Qnil);
19777 dpyinfo->mouse_face_window = window;
19778
19779 dpyinfo->mouse_face_face_id
19780 = face_at_buffer_position (w, pos, 0, 0,
19781 &ignore, pos + 1,
19782 !dpyinfo->mouse_face_hidden);
19783
19784 /* Display it as active. */
19785 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
19786 cursor = No_Cursor;
19787 }
19788 /* Handle the text property case. */
19789 else if (!NILP (mouse_face) && BUFFERP (object))
19790 {
19791 /* Find the range of text around this char that
19792 should be active. */
19793 Lisp_Object before, after, beginning, end;
19794 int ignore;
19795
19796 beginning = Fmarker_position (w->start);
19797 end = make_number (BUF_Z (XBUFFER (object))
19798 - XFASTINT (w->window_end_pos));
19799 before
19800 = Fprevious_single_property_change (make_number (pos + 1),
19801 Qmouse_face,
19802 object, beginning);
19803 after
19804 = Fnext_single_property_change (position, Qmouse_face,
19805 object, end);
19806
19807 /* Record this as the current active region. */
19808 fast_find_position (w, XFASTINT (before),
19809 &dpyinfo->mouse_face_beg_col,
19810 &dpyinfo->mouse_face_beg_row,
19811 &dpyinfo->mouse_face_beg_x,
19812 &dpyinfo->mouse_face_beg_y, Qnil);
19813 dpyinfo->mouse_face_past_end
19814 = !fast_find_position (w, XFASTINT (after),
19815 &dpyinfo->mouse_face_end_col,
19816 &dpyinfo->mouse_face_end_row,
19817 &dpyinfo->mouse_face_end_x,
19818 &dpyinfo->mouse_face_end_y, Qnil);
19819 dpyinfo->mouse_face_window = window;
19820
19821 if (BUFFERP (object))
19822 dpyinfo->mouse_face_face_id
19823 = face_at_buffer_position (w, pos, 0, 0,
19824 &ignore, pos + 1,
19825 !dpyinfo->mouse_face_hidden);
19826
19827 /* Display it as active. */
19828 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
19829 cursor = No_Cursor;
19830 }
19831 else if (!NILP (mouse_face) && STRINGP (object))
19832 {
19833 Lisp_Object b, e;
19834 int ignore;
19835
19836 b = Fprevious_single_property_change (make_number (pos + 1),
19837 Qmouse_face,
19838 object, Qnil);
19839 e = Fnext_single_property_change (position, Qmouse_face,
19840 object, Qnil);
19841 if (NILP (b))
19842 b = make_number (0);
19843 if (NILP (e))
19844 e = make_number (SCHARS (object) - 1);
19845 fast_find_string_pos (w, XINT (b), object,
19846 &dpyinfo->mouse_face_beg_col,
19847 &dpyinfo->mouse_face_beg_row,
19848 &dpyinfo->mouse_face_beg_x,
19849 &dpyinfo->mouse_face_beg_y, 0);
19850 fast_find_string_pos (w, XINT (e), object,
19851 &dpyinfo->mouse_face_end_col,
19852 &dpyinfo->mouse_face_end_row,
19853 &dpyinfo->mouse_face_end_x,
19854 &dpyinfo->mouse_face_end_y, 1);
19855 dpyinfo->mouse_face_past_end = 0;
19856 dpyinfo->mouse_face_window = window;
19857 dpyinfo->mouse_face_face_id
19858 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
19859 glyph->face_id, 1);
19860 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
19861 cursor = No_Cursor;
19862 }
19863 else if (STRINGP (object) && NILP (mouse_face))
19864 {
19865 /* A string which doesn't have mouse-face, but
19866 the text ``under'' it might have. */
19867 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
19868 int start = MATRIX_ROW_START_CHARPOS (r);
19869
19870 pos = string_buffer_position (w, object, start);
19871 if (pos > 0)
19872 mouse_face = get_char_property_and_overlay (make_number (pos),
19873 Qmouse_face,
19874 w->buffer,
19875 &overlay);
19876 if (!NILP (mouse_face) && !NILP (overlay))
19877 {
19878 Lisp_Object before = Foverlay_start (overlay);
19879 Lisp_Object after = Foverlay_end (overlay);
19880 int ignore;
19881
19882 /* Note that we might not be able to find position
19883 BEFORE in the glyph matrix if the overlay is
19884 entirely covered by a `display' property. In
19885 this case, we overshoot. So let's stop in
19886 the glyph matrix before glyphs for OBJECT. */
19887 fast_find_position (w, XFASTINT (before),
19888 &dpyinfo->mouse_face_beg_col,
19889 &dpyinfo->mouse_face_beg_row,
19890 &dpyinfo->mouse_face_beg_x,
19891 &dpyinfo->mouse_face_beg_y,
19892 object);
19893
19894 dpyinfo->mouse_face_past_end
19895 = !fast_find_position (w, XFASTINT (after),
19896 &dpyinfo->mouse_face_end_col,
19897 &dpyinfo->mouse_face_end_row,
19898 &dpyinfo->mouse_face_end_x,
19899 &dpyinfo->mouse_face_end_y,
19900 Qnil);
19901 dpyinfo->mouse_face_window = window;
19902 dpyinfo->mouse_face_face_id
19903 = face_at_buffer_position (w, pos, 0, 0,
19904 &ignore, pos + 1,
19905 !dpyinfo->mouse_face_hidden);
19906
19907 /* Display it as active. */
19908 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
19909 cursor = No_Cursor;
19910 }
19911 }
19912 }
19913
19914 check_help_echo:
19915
19916 /* Look for a `help-echo' property. */
19917 {
19918 Lisp_Object help, overlay;
19919
19920 /* Check overlays first. */
19921 help = overlay = Qnil;
19922 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
19923 {
19924 overlay = overlay_vec[i];
19925 help = Foverlay_get (overlay, Qhelp_echo);
19926 }
19927
19928 if (!NILP (help))
19929 {
19930 help_echo_string = help;
19931 help_echo_window = window;
19932 help_echo_object = overlay;
19933 help_echo_pos = pos;
19934 }
19935 else
19936 {
19937 Lisp_Object object = glyph->object;
19938 int charpos = glyph->charpos;
19939
19940 /* Try text properties. */
19941 if (STRINGP (object)
19942 && charpos >= 0
19943 && charpos < SCHARS (object))
19944 {
19945 help = Fget_text_property (make_number (charpos),
19946 Qhelp_echo, object);
19947 if (NILP (help))
19948 {
19949 /* If the string itself doesn't specify a help-echo,
19950 see if the buffer text ``under'' it does. */
19951 struct glyph_row *r
19952 = MATRIX_ROW (w->current_matrix, vpos);
19953 int start = MATRIX_ROW_START_CHARPOS (r);
19954 int pos = string_buffer_position (w, object, start);
19955 if (pos > 0)
19956 {
19957 help = Fget_char_property (make_number (pos),
19958 Qhelp_echo, w->buffer);
19959 if (!NILP (help))
19960 {
19961 charpos = pos;
19962 object = w->buffer;
19963 }
19964 }
19965 }
19966 }
19967 else if (BUFFERP (object)
19968 && charpos >= BEGV
19969 && charpos < ZV)
19970 help = Fget_text_property (make_number (charpos), Qhelp_echo,
19971 object);
19972
19973 if (!NILP (help))
19974 {
19975 help_echo_string = help;
19976 help_echo_window = window;
19977 help_echo_object = object;
19978 help_echo_pos = charpos;
19979 }
19980 }
19981 }
19982
19983 BEGV = obegv;
19984 ZV = ozv;
19985 current_buffer = obuf;
19986 }
19987
19988 set_cursor:
19989
19990 #ifndef HAVE_CARBON
19991 if (cursor != No_Cursor)
19992 #else
19993 if (bcmp (&cursor, &No_Cursor, sizeof (Cursor)))
19994 #endif
19995 rif->define_frame_cursor (f, cursor);
19996 }
19997
19998
19999 /* EXPORT for RIF:
20000 Clear any mouse-face on window W. This function is part of the
20001 redisplay interface, and is called from try_window_id and similar
20002 functions to ensure the mouse-highlight is off. */
20003
20004 void
20005 x_clear_window_mouse_face (w)
20006 struct window *w;
20007 {
20008 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20009 Lisp_Object window;
20010
20011 BLOCK_INPUT;
20012 XSETWINDOW (window, w);
20013 if (EQ (window, dpyinfo->mouse_face_window))
20014 clear_mouse_face (dpyinfo);
20015 UNBLOCK_INPUT;
20016 }
20017
20018
20019 /* EXPORT:
20020 Just discard the mouse face information for frame F, if any.
20021 This is used when the size of F is changed. */
20022
20023 void
20024 cancel_mouse_face (f)
20025 struct frame *f;
20026 {
20027 Lisp_Object window;
20028 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20029
20030 window = dpyinfo->mouse_face_window;
20031 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
20032 {
20033 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20034 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20035 dpyinfo->mouse_face_window = Qnil;
20036 }
20037 }
20038
20039
20040 #endif /* HAVE_WINDOW_SYSTEM */
20041
20042 \f
20043 /***********************************************************************
20044 Exposure Events
20045 ***********************************************************************/
20046
20047 #ifdef HAVE_WINDOW_SYSTEM
20048
20049 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
20050 which intersects rectangle R. R is in window-relative coordinates. */
20051
20052 static void
20053 expose_area (w, row, r, area)
20054 struct window *w;
20055 struct glyph_row *row;
20056 XRectangle *r;
20057 enum glyph_row_area area;
20058 {
20059 struct glyph *first = row->glyphs[area];
20060 struct glyph *end = row->glyphs[area] + row->used[area];
20061 struct glyph *last;
20062 int first_x, start_x, x;
20063
20064 if (area == TEXT_AREA && row->fill_line_p)
20065 /* If row extends face to end of line write the whole line. */
20066 draw_glyphs (w, 0, row, area,
20067 0, row->used[area],
20068 DRAW_NORMAL_TEXT, 0);
20069 else
20070 {
20071 /* Set START_X to the window-relative start position for drawing glyphs of
20072 AREA. The first glyph of the text area can be partially visible.
20073 The first glyphs of other areas cannot. */
20074 start_x = window_box_left_offset (w, area);
20075 if (area == TEXT_AREA)
20076 start_x += row->x;
20077 x = start_x;
20078
20079 /* Find the first glyph that must be redrawn. */
20080 while (first < end
20081 && x + first->pixel_width < r->x)
20082 {
20083 x += first->pixel_width;
20084 ++first;
20085 }
20086
20087 /* Find the last one. */
20088 last = first;
20089 first_x = x;
20090 while (last < end
20091 && x < r->x + r->width)
20092 {
20093 x += last->pixel_width;
20094 ++last;
20095 }
20096
20097 /* Repaint. */
20098 if (last > first)
20099 draw_glyphs (w, first_x - start_x, row, area,
20100 first - row->glyphs[area], last - row->glyphs[area],
20101 DRAW_NORMAL_TEXT, 0);
20102 }
20103 }
20104
20105
20106 /* Redraw the parts of the glyph row ROW on window W intersecting
20107 rectangle R. R is in window-relative coordinates. Value is
20108 non-zero if mouse-face was overwritten. */
20109
20110 static int
20111 expose_line (w, row, r)
20112 struct window *w;
20113 struct glyph_row *row;
20114 XRectangle *r;
20115 {
20116 xassert (row->enabled_p);
20117
20118 if (row->mode_line_p || w->pseudo_window_p)
20119 draw_glyphs (w, 0, row, TEXT_AREA,
20120 0, row->used[TEXT_AREA],
20121 DRAW_NORMAL_TEXT, 0);
20122 else
20123 {
20124 if (row->used[LEFT_MARGIN_AREA])
20125 expose_area (w, row, r, LEFT_MARGIN_AREA);
20126 if (row->used[TEXT_AREA])
20127 expose_area (w, row, r, TEXT_AREA);
20128 if (row->used[RIGHT_MARGIN_AREA])
20129 expose_area (w, row, r, RIGHT_MARGIN_AREA);
20130 draw_row_fringe_bitmaps (w, row);
20131 }
20132
20133 return row->mouse_face_p;
20134 }
20135
20136
20137 /* Redraw those parts of glyphs rows during expose event handling that
20138 overlap other rows. Redrawing of an exposed line writes over parts
20139 of lines overlapping that exposed line; this function fixes that.
20140
20141 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
20142 row in W's current matrix that is exposed and overlaps other rows.
20143 LAST_OVERLAPPING_ROW is the last such row. */
20144
20145 static void
20146 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
20147 struct window *w;
20148 struct glyph_row *first_overlapping_row;
20149 struct glyph_row *last_overlapping_row;
20150 {
20151 struct glyph_row *row;
20152
20153 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
20154 if (row->overlapping_p)
20155 {
20156 xassert (row->enabled_p && !row->mode_line_p);
20157
20158 if (row->used[LEFT_MARGIN_AREA])
20159 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
20160
20161 if (row->used[TEXT_AREA])
20162 x_fix_overlapping_area (w, row, TEXT_AREA);
20163
20164 if (row->used[RIGHT_MARGIN_AREA])
20165 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
20166 }
20167 }
20168
20169
20170 /* Return non-zero if W's cursor intersects rectangle R. */
20171
20172 static int
20173 phys_cursor_in_rect_p (w, r)
20174 struct window *w;
20175 XRectangle *r;
20176 {
20177 XRectangle cr, result;
20178 struct glyph *cursor_glyph;
20179
20180 cursor_glyph = get_phys_cursor_glyph (w);
20181 if (cursor_glyph)
20182 {
20183 cr.x = w->phys_cursor.x;
20184 cr.y = w->phys_cursor.y;
20185 cr.width = cursor_glyph->pixel_width;
20186 cr.height = w->phys_cursor_height;
20187 /* ++KFS: W32 version used W32-specific IntersectRect here, but
20188 I assume the effect is the same -- and this is portable. */
20189 return x_intersect_rectangles (&cr, r, &result);
20190 }
20191 else
20192 return 0;
20193 }
20194
20195
20196 /* EXPORT:
20197 Draw a vertical window border to the right of window W if W doesn't
20198 have vertical scroll bars. */
20199
20200 void
20201 x_draw_vertical_border (w)
20202 struct window *w;
20203 {
20204 struct frame *f = XFRAME (WINDOW_FRAME (w));
20205
20206 /* We could do better, if we knew what type of scroll-bar the adjacent
20207 windows (on either side) have... But we don't :-(
20208 However, I think this works ok. ++KFS 2003-04-25 */
20209
20210 /* Redraw borders between horizontally adjacent windows. Don't
20211 do it for frames with vertical scroll bars because either the
20212 right scroll bar of a window, or the left scroll bar of its
20213 neighbor will suffice as a border. */
20214 if (!WINDOW_RIGHTMOST_P (w)
20215 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
20216 {
20217 int x0, x1, y0, y1;
20218
20219 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
20220 y1 -= 1;
20221
20222 rif->draw_vertical_window_border (w, x1, y0, y1);
20223 }
20224 else if (!WINDOW_LEFTMOST_P (w)
20225 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
20226 {
20227 int x0, x1, y0, y1;
20228
20229 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
20230 y1 -= 1;
20231
20232 rif->draw_vertical_window_border (w, x0, y0, y1);
20233 }
20234 }
20235
20236
20237 /* Redraw the part of window W intersection rectangle FR. Pixel
20238 coordinates in FR are frame-relative. Call this function with
20239 input blocked. Value is non-zero if the exposure overwrites
20240 mouse-face. */
20241
20242 static int
20243 expose_window (w, fr)
20244 struct window *w;
20245 XRectangle *fr;
20246 {
20247 struct frame *f = XFRAME (w->frame);
20248 XRectangle wr, r;
20249 int mouse_face_overwritten_p = 0;
20250
20251 /* If window is not yet fully initialized, do nothing. This can
20252 happen when toolkit scroll bars are used and a window is split.
20253 Reconfiguring the scroll bar will generate an expose for a newly
20254 created window. */
20255 if (w->current_matrix == NULL)
20256 return 0;
20257
20258 /* When we're currently updating the window, display and current
20259 matrix usually don't agree. Arrange for a thorough display
20260 later. */
20261 if (w == updated_window)
20262 {
20263 SET_FRAME_GARBAGED (f);
20264 return 0;
20265 }
20266
20267 /* Frame-relative pixel rectangle of W. */
20268 wr.x = WINDOW_LEFT_EDGE_X (w);
20269 wr.y = WINDOW_TOP_EDGE_Y (w);
20270 wr.width = WINDOW_TOTAL_WIDTH (w);
20271 wr.height = WINDOW_TOTAL_HEIGHT (w);
20272
20273 if (x_intersect_rectangles (fr, &wr, &r))
20274 {
20275 int yb = window_text_bottom_y (w);
20276 struct glyph_row *row;
20277 int cursor_cleared_p;
20278 struct glyph_row *first_overlapping_row, *last_overlapping_row;
20279
20280 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
20281 r.x, r.y, r.width, r.height));
20282
20283 /* Convert to window coordinates. */
20284 r.x = FRAME_TO_WINDOW_PIXEL_X (w, r.x);
20285 r.y = FRAME_TO_WINDOW_PIXEL_Y (w, r.y);
20286
20287 /* Turn off the cursor. */
20288 if (!w->pseudo_window_p
20289 && phys_cursor_in_rect_p (w, &r))
20290 {
20291 x_clear_cursor (w);
20292 cursor_cleared_p = 1;
20293 }
20294 else
20295 cursor_cleared_p = 0;
20296
20297 /* Update lines intersecting rectangle R. */
20298 first_overlapping_row = last_overlapping_row = NULL;
20299 for (row = w->current_matrix->rows;
20300 row->enabled_p;
20301 ++row)
20302 {
20303 int y0 = row->y;
20304 int y1 = MATRIX_ROW_BOTTOM_Y (row);
20305
20306 if ((y0 >= r.y && y0 < r.y + r.height)
20307 || (y1 > r.y && y1 < r.y + r.height)
20308 || (r.y >= y0 && r.y < y1)
20309 || (r.y + r.height > y0 && r.y + r.height < y1))
20310 {
20311 if (row->overlapping_p)
20312 {
20313 if (first_overlapping_row == NULL)
20314 first_overlapping_row = row;
20315 last_overlapping_row = row;
20316 }
20317
20318 if (expose_line (w, row, &r))
20319 mouse_face_overwritten_p = 1;
20320 }
20321
20322 if (y1 >= yb)
20323 break;
20324 }
20325
20326 /* Display the mode line if there is one. */
20327 if (WINDOW_WANTS_MODELINE_P (w)
20328 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
20329 row->enabled_p)
20330 && row->y < r.y + r.height)
20331 {
20332 if (expose_line (w, row, &r))
20333 mouse_face_overwritten_p = 1;
20334 }
20335
20336 if (!w->pseudo_window_p)
20337 {
20338 /* Fix the display of overlapping rows. */
20339 if (first_overlapping_row)
20340 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
20341
20342 /* Draw border between windows. */
20343 x_draw_vertical_border (w);
20344
20345 /* Turn the cursor on again. */
20346 if (cursor_cleared_p)
20347 update_window_cursor (w, 1);
20348 }
20349 }
20350
20351 #ifdef HAVE_CARBON
20352 /* Display scroll bar for this window. */
20353 if (!NILP (w->vertical_scroll_bar))
20354 {
20355 /* ++KFS:
20356 If this doesn't work here (maybe some header files are missing),
20357 make a function in macterm.c and call it to do the job! */
20358 ControlHandle ch
20359 = SCROLL_BAR_CONTROL_HANDLE (XSCROLL_BAR (w->vertical_scroll_bar));
20360
20361 Draw1Control (ch);
20362 }
20363 #endif
20364
20365 return mouse_face_overwritten_p;
20366 }
20367
20368
20369
20370 /* Redraw (parts) of all windows in the window tree rooted at W that
20371 intersect R. R contains frame pixel coordinates. Value is
20372 non-zero if the exposure overwrites mouse-face. */
20373
20374 static int
20375 expose_window_tree (w, r)
20376 struct window *w;
20377 XRectangle *r;
20378 {
20379 struct frame *f = XFRAME (w->frame);
20380 int mouse_face_overwritten_p = 0;
20381
20382 while (w && !FRAME_GARBAGED_P (f))
20383 {
20384 if (!NILP (w->hchild))
20385 mouse_face_overwritten_p
20386 |= expose_window_tree (XWINDOW (w->hchild), r);
20387 else if (!NILP (w->vchild))
20388 mouse_face_overwritten_p
20389 |= expose_window_tree (XWINDOW (w->vchild), r);
20390 else
20391 mouse_face_overwritten_p |= expose_window (w, r);
20392
20393 w = NILP (w->next) ? NULL : XWINDOW (w->next);
20394 }
20395
20396 return mouse_face_overwritten_p;
20397 }
20398
20399
20400 /* EXPORT:
20401 Redisplay an exposed area of frame F. X and Y are the upper-left
20402 corner of the exposed rectangle. W and H are width and height of
20403 the exposed area. All are pixel values. W or H zero means redraw
20404 the entire frame. */
20405
20406 void
20407 expose_frame (f, x, y, w, h)
20408 struct frame *f;
20409 int x, y, w, h;
20410 {
20411 XRectangle r;
20412 int mouse_face_overwritten_p = 0;
20413
20414 TRACE ((stderr, "expose_frame "));
20415
20416 /* No need to redraw if frame will be redrawn soon. */
20417 if (FRAME_GARBAGED_P (f))
20418 {
20419 TRACE ((stderr, " garbaged\n"));
20420 return;
20421 }
20422
20423 #ifdef HAVE_CARBON
20424 /* MAC_TODO: this is a kludge, but if scroll bars are not activated
20425 or deactivated here, for unknown reasons, activated scroll bars
20426 are shown in deactivated frames in some instances. */
20427 if (f == FRAME_MAC_DISPLAY_INFO (f)->x_focus_frame)
20428 activate_scroll_bars (f);
20429 else
20430 deactivate_scroll_bars (f);
20431 #endif
20432
20433 /* If basic faces haven't been realized yet, there is no point in
20434 trying to redraw anything. This can happen when we get an expose
20435 event while Emacs is starting, e.g. by moving another window. */
20436 if (FRAME_FACE_CACHE (f) == NULL
20437 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
20438 {
20439 TRACE ((stderr, " no faces\n"));
20440 return;
20441 }
20442
20443 if (w == 0 || h == 0)
20444 {
20445 r.x = r.y = 0;
20446 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
20447 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
20448 }
20449 else
20450 {
20451 r.x = x;
20452 r.y = y;
20453 r.width = w;
20454 r.height = h;
20455 }
20456
20457 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
20458 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
20459
20460 if (WINDOWP (f->tool_bar_window))
20461 mouse_face_overwritten_p
20462 |= expose_window (XWINDOW (f->tool_bar_window), &r);
20463
20464 #ifdef HAVE_X_WINDOWS
20465 #ifndef MSDOS
20466 #ifndef USE_X_TOOLKIT
20467 if (WINDOWP (f->menu_bar_window))
20468 mouse_face_overwritten_p
20469 |= expose_window (XWINDOW (f->menu_bar_window), &r);
20470 #endif /* not USE_X_TOOLKIT */
20471 #endif
20472 #endif
20473
20474 /* Some window managers support a focus-follows-mouse style with
20475 delayed raising of frames. Imagine a partially obscured frame,
20476 and moving the mouse into partially obscured mouse-face on that
20477 frame. The visible part of the mouse-face will be highlighted,
20478 then the WM raises the obscured frame. With at least one WM, KDE
20479 2.1, Emacs is not getting any event for the raising of the frame
20480 (even tried with SubstructureRedirectMask), only Expose events.
20481 These expose events will draw text normally, i.e. not
20482 highlighted. Which means we must redo the highlight here.
20483 Subsume it under ``we love X''. --gerd 2001-08-15 */
20484 /* Included in Windows version because Windows most likely does not
20485 do the right thing if any third party tool offers
20486 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
20487 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
20488 {
20489 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20490 if (f == dpyinfo->mouse_face_mouse_frame)
20491 {
20492 int x = dpyinfo->mouse_face_mouse_x;
20493 int y = dpyinfo->mouse_face_mouse_y;
20494 clear_mouse_face (dpyinfo);
20495 note_mouse_highlight (f, x, y);
20496 }
20497 }
20498 }
20499
20500
20501 /* EXPORT:
20502 Determine the intersection of two rectangles R1 and R2. Return
20503 the intersection in *RESULT. Value is non-zero if RESULT is not
20504 empty. */
20505
20506 int
20507 x_intersect_rectangles (r1, r2, result)
20508 XRectangle *r1, *r2, *result;
20509 {
20510 XRectangle *left, *right;
20511 XRectangle *upper, *lower;
20512 int intersection_p = 0;
20513
20514 /* Rearrange so that R1 is the left-most rectangle. */
20515 if (r1->x < r2->x)
20516 left = r1, right = r2;
20517 else
20518 left = r2, right = r1;
20519
20520 /* X0 of the intersection is right.x0, if this is inside R1,
20521 otherwise there is no intersection. */
20522 if (right->x <= left->x + left->width)
20523 {
20524 result->x = right->x;
20525
20526 /* The right end of the intersection is the minimum of the
20527 the right ends of left and right. */
20528 result->width = (min (left->x + left->width, right->x + right->width)
20529 - result->x);
20530
20531 /* Same game for Y. */
20532 if (r1->y < r2->y)
20533 upper = r1, lower = r2;
20534 else
20535 upper = r2, lower = r1;
20536
20537 /* The upper end of the intersection is lower.y0, if this is inside
20538 of upper. Otherwise, there is no intersection. */
20539 if (lower->y <= upper->y + upper->height)
20540 {
20541 result->y = lower->y;
20542
20543 /* The lower end of the intersection is the minimum of the lower
20544 ends of upper and lower. */
20545 result->height = (min (lower->y + lower->height,
20546 upper->y + upper->height)
20547 - result->y);
20548 intersection_p = 1;
20549 }
20550 }
20551
20552 return intersection_p;
20553 }
20554
20555 #endif /* HAVE_WINDOW_SYSTEM */
20556
20557 \f
20558 /***********************************************************************
20559 Initialization
20560 ***********************************************************************/
20561
20562 void
20563 syms_of_xdisp ()
20564 {
20565 Vwith_echo_area_save_vector = Qnil;
20566 staticpro (&Vwith_echo_area_save_vector);
20567
20568 Vmessage_stack = Qnil;
20569 staticpro (&Vmessage_stack);
20570
20571 Qinhibit_redisplay = intern ("inhibit-redisplay");
20572 staticpro (&Qinhibit_redisplay);
20573
20574 message_dolog_marker1 = Fmake_marker ();
20575 staticpro (&message_dolog_marker1);
20576 message_dolog_marker2 = Fmake_marker ();
20577 staticpro (&message_dolog_marker2);
20578 message_dolog_marker3 = Fmake_marker ();
20579 staticpro (&message_dolog_marker3);
20580
20581 #if GLYPH_DEBUG
20582 defsubr (&Sdump_frame_glyph_matrix);
20583 defsubr (&Sdump_glyph_matrix);
20584 defsubr (&Sdump_glyph_row);
20585 defsubr (&Sdump_tool_bar_row);
20586 defsubr (&Strace_redisplay);
20587 defsubr (&Strace_to_stderr);
20588 #endif
20589 #ifdef HAVE_WINDOW_SYSTEM
20590 defsubr (&Stool_bar_lines_needed);
20591 #endif
20592 defsubr (&Sformat_mode_line);
20593
20594 staticpro (&Qmenu_bar_update_hook);
20595 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
20596
20597 staticpro (&Qoverriding_terminal_local_map);
20598 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
20599
20600 staticpro (&Qoverriding_local_map);
20601 Qoverriding_local_map = intern ("overriding-local-map");
20602
20603 staticpro (&Qwindow_scroll_functions);
20604 Qwindow_scroll_functions = intern ("window-scroll-functions");
20605
20606 staticpro (&Qredisplay_end_trigger_functions);
20607 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
20608
20609 staticpro (&Qinhibit_point_motion_hooks);
20610 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
20611
20612 QCdata = intern (":data");
20613 staticpro (&QCdata);
20614 Qdisplay = intern ("display");
20615 staticpro (&Qdisplay);
20616 Qspace_width = intern ("space-width");
20617 staticpro (&Qspace_width);
20618 Qraise = intern ("raise");
20619 staticpro (&Qraise);
20620 Qspace = intern ("space");
20621 staticpro (&Qspace);
20622 Qmargin = intern ("margin");
20623 staticpro (&Qmargin);
20624 Qleft_margin = intern ("left-margin");
20625 staticpro (&Qleft_margin);
20626 Qright_margin = intern ("right-margin");
20627 staticpro (&Qright_margin);
20628 Qalign_to = intern ("align-to");
20629 staticpro (&Qalign_to);
20630 QCalign_to = intern (":align-to");
20631 staticpro (&QCalign_to);
20632 Qrelative_width = intern ("relative-width");
20633 staticpro (&Qrelative_width);
20634 QCrelative_width = intern (":relative-width");
20635 staticpro (&QCrelative_width);
20636 QCrelative_height = intern (":relative-height");
20637 staticpro (&QCrelative_height);
20638 QCeval = intern (":eval");
20639 staticpro (&QCeval);
20640 QCpropertize = intern (":propertize");
20641 staticpro (&QCpropertize);
20642 QCfile = intern (":file");
20643 staticpro (&QCfile);
20644 Qfontified = intern ("fontified");
20645 staticpro (&Qfontified);
20646 Qfontification_functions = intern ("fontification-functions");
20647 staticpro (&Qfontification_functions);
20648 Qtrailing_whitespace = intern ("trailing-whitespace");
20649 staticpro (&Qtrailing_whitespace);
20650 Qimage = intern ("image");
20651 staticpro (&Qimage);
20652 Qmessage_truncate_lines = intern ("message-truncate-lines");
20653 staticpro (&Qmessage_truncate_lines);
20654 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
20655 staticpro (&Qcursor_in_non_selected_windows);
20656 Qgrow_only = intern ("grow-only");
20657 staticpro (&Qgrow_only);
20658 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
20659 staticpro (&Qinhibit_menubar_update);
20660 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
20661 staticpro (&Qinhibit_eval_during_redisplay);
20662 Qposition = intern ("position");
20663 staticpro (&Qposition);
20664 Qbuffer_position = intern ("buffer-position");
20665 staticpro (&Qbuffer_position);
20666 Qobject = intern ("object");
20667 staticpro (&Qobject);
20668 Qbar = intern ("bar");
20669 staticpro (&Qbar);
20670 Qhbar = intern ("hbar");
20671 staticpro (&Qhbar);
20672 Qbox = intern ("box");
20673 staticpro (&Qbox);
20674 Qhollow = intern ("hollow");
20675 staticpro (&Qhollow);
20676 Qrisky_local_variable = intern ("risky-local-variable");
20677 staticpro (&Qrisky_local_variable);
20678 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
20679 staticpro (&Qinhibit_free_realized_faces);
20680
20681 list_of_error = Fcons (intern ("error"), Qnil);
20682 staticpro (&list_of_error);
20683
20684 last_arrow_position = Qnil;
20685 last_arrow_string = Qnil;
20686 staticpro (&last_arrow_position);
20687 staticpro (&last_arrow_string);
20688
20689 echo_buffer[0] = echo_buffer[1] = Qnil;
20690 staticpro (&echo_buffer[0]);
20691 staticpro (&echo_buffer[1]);
20692
20693 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
20694 staticpro (&echo_area_buffer[0]);
20695 staticpro (&echo_area_buffer[1]);
20696
20697 Vmessages_buffer_name = build_string ("*Messages*");
20698 staticpro (&Vmessages_buffer_name);
20699
20700 mode_line_proptrans_alist = Qnil;
20701 staticpro (&mode_line_proptrans_alist);
20702
20703 mode_line_string_list = Qnil;
20704 staticpro (&mode_line_string_list);
20705
20706 help_echo_string = Qnil;
20707 staticpro (&help_echo_string);
20708 help_echo_object = Qnil;
20709 staticpro (&help_echo_object);
20710 help_echo_window = Qnil;
20711 staticpro (&help_echo_window);
20712 previous_help_echo_string = Qnil;
20713 staticpro (&previous_help_echo_string);
20714 help_echo_pos = -1;
20715
20716 #ifdef HAVE_WINDOW_SYSTEM
20717 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
20718 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
20719 For example, if a block cursor is over a tab, it will be drawn as
20720 wide as that tab on the display. */);
20721 x_stretch_cursor_p = 0;
20722 #endif
20723
20724 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
20725 doc: /* Non-nil means highlight trailing whitespace.
20726 The face used for trailing whitespace is `trailing-whitespace'. */);
20727 Vshow_trailing_whitespace = Qnil;
20728
20729 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
20730 doc: /* Non-nil means don't actually do any redisplay.
20731 This is used for internal purposes. */);
20732 Vinhibit_redisplay = Qnil;
20733
20734 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
20735 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
20736 Vglobal_mode_string = Qnil;
20737
20738 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
20739 doc: /* Marker for where to display an arrow on top of the buffer text.
20740 This must be the beginning of a line in order to work.
20741 See also `overlay-arrow-string'. */);
20742 Voverlay_arrow_position = Qnil;
20743
20744 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
20745 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
20746 Voverlay_arrow_string = Qnil;
20747
20748 DEFVAR_INT ("scroll-step", &scroll_step,
20749 doc: /* *The number of lines to try scrolling a window by when point moves out.
20750 If that fails to bring point back on frame, point is centered instead.
20751 If this is zero, point is always centered after it moves off frame.
20752 If you want scrolling to always be a line at a time, you should set
20753 `scroll-conservatively' to a large value rather than set this to 1. */);
20754
20755 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
20756 doc: /* *Scroll up to this many lines, to bring point back on screen.
20757 A value of zero means to scroll the text to center point vertically
20758 in the window. */);
20759 scroll_conservatively = 0;
20760
20761 DEFVAR_INT ("scroll-margin", &scroll_margin,
20762 doc: /* *Number of lines of margin at the top and bottom of a window.
20763 Recenter the window whenever point gets within this many lines
20764 of the top or bottom of the window. */);
20765 scroll_margin = 0;
20766
20767 #if GLYPH_DEBUG
20768 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
20769 #endif
20770
20771 DEFVAR_BOOL ("truncate-partial-width-windows",
20772 &truncate_partial_width_windows,
20773 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
20774 truncate_partial_width_windows = 1;
20775
20776 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
20777 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
20778 Any other value means to use the appropriate face, `mode-line',
20779 `header-line', or `menu' respectively. */);
20780 mode_line_inverse_video = 1;
20781
20782 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
20783 doc: /* *Maximum buffer size for which line number should be displayed.
20784 If the buffer is bigger than this, the line number does not appear
20785 in the mode line. A value of nil means no limit. */);
20786 Vline_number_display_limit = Qnil;
20787
20788 DEFVAR_INT ("line-number-display-limit-width",
20789 &line_number_display_limit_width,
20790 doc: /* *Maximum line width (in characters) for line number display.
20791 If the average length of the lines near point is bigger than this, then the
20792 line number may be omitted from the mode line. */);
20793 line_number_display_limit_width = 200;
20794
20795 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
20796 doc: /* *Non-nil means highlight region even in nonselected windows. */);
20797 highlight_nonselected_windows = 0;
20798
20799 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
20800 doc: /* Non-nil if more than one frame is visible on this display.
20801 Minibuffer-only frames don't count, but iconified frames do.
20802 This variable is not guaranteed to be accurate except while processing
20803 `frame-title-format' and `icon-title-format'. */);
20804
20805 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
20806 doc: /* Template for displaying the title bar of visible frames.
20807 \(Assuming the window manager supports this feature.)
20808 This variable has the same structure as `mode-line-format' (which see),
20809 and is used only on frames for which no explicit name has been set
20810 \(see `modify-frame-parameters'). */);
20811 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
20812 doc: /* Template for displaying the title bar of an iconified frame.
20813 \(Assuming the window manager supports this feature.)
20814 This variable has the same structure as `mode-line-format' (which see),
20815 and is used only on frames for which no explicit name has been set
20816 \(see `modify-frame-parameters'). */);
20817 Vicon_title_format
20818 = Vframe_title_format
20819 = Fcons (intern ("multiple-frames"),
20820 Fcons (build_string ("%b"),
20821 Fcons (Fcons (empty_string,
20822 Fcons (intern ("invocation-name"),
20823 Fcons (build_string ("@"),
20824 Fcons (intern ("system-name"),
20825 Qnil)))),
20826 Qnil)));
20827
20828 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
20829 doc: /* Maximum number of lines to keep in the message log buffer.
20830 If nil, disable message logging. If t, log messages but don't truncate
20831 the buffer when it becomes large. */);
20832 Vmessage_log_max = make_number (50);
20833
20834 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
20835 doc: /* Functions called before redisplay, if window sizes have changed.
20836 The value should be a list of functions that take one argument.
20837 Just before redisplay, for each frame, if any of its windows have changed
20838 size since the last redisplay, or have been split or deleted,
20839 all the functions in the list are called, with the frame as argument. */);
20840 Vwindow_size_change_functions = Qnil;
20841
20842 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
20843 doc: /* List of Functions to call before redisplaying a window with scrolling.
20844 Each function is called with two arguments, the window
20845 and its new display-start position. Note that the value of `window-end'
20846 is not valid when these functions are called. */);
20847 Vwindow_scroll_functions = Qnil;
20848
20849 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
20850 doc: /* *Non-nil means autoselect window with mouse pointer. */);
20851 mouse_autoselect_window = 0;
20852
20853 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
20854 doc: /* *Non-nil means automatically resize tool-bars.
20855 This increases a tool-bar's height if not all tool-bar items are visible.
20856 It decreases a tool-bar's height when it would display blank lines
20857 otherwise. */);
20858 auto_resize_tool_bars_p = 1;
20859
20860 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
20861 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
20862 auto_raise_tool_bar_buttons_p = 1;
20863
20864 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
20865 doc: /* *Margin around tool-bar buttons in pixels.
20866 If an integer, use that for both horizontal and vertical margins.
20867 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
20868 HORZ specifying the horizontal margin, and VERT specifying the
20869 vertical margin. */);
20870 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
20871
20872 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
20873 doc: /* *Relief thickness of tool-bar buttons. */);
20874 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
20875
20876 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
20877 doc: /* List of functions to call to fontify regions of text.
20878 Each function is called with one argument POS. Functions must
20879 fontify a region starting at POS in the current buffer, and give
20880 fontified regions the property `fontified'. */);
20881 Vfontification_functions = Qnil;
20882 Fmake_variable_buffer_local (Qfontification_functions);
20883
20884 DEFVAR_BOOL ("unibyte-display-via-language-environment",
20885 &unibyte_display_via_language_environment,
20886 doc: /* *Non-nil means display unibyte text according to language environment.
20887 Specifically this means that unibyte non-ASCII characters
20888 are displayed by converting them to the equivalent multibyte characters
20889 according to the current language environment. As a result, they are
20890 displayed according to the current fontset. */);
20891 unibyte_display_via_language_environment = 0;
20892
20893 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
20894 doc: /* *Maximum height for resizing mini-windows.
20895 If a float, it specifies a fraction of the mini-window frame's height.
20896 If an integer, it specifies a number of lines. */);
20897 Vmax_mini_window_height = make_float (0.25);
20898
20899 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
20900 doc: /* *How to resize mini-windows.
20901 A value of nil means don't automatically resize mini-windows.
20902 A value of t means resize them to fit the text displayed in them.
20903 A value of `grow-only', the default, means let mini-windows grow
20904 only, until their display becomes empty, at which point the windows
20905 go back to their normal size. */);
20906 Vresize_mini_windows = Qgrow_only;
20907
20908 DEFVAR_LISP ("cursor-in-non-selected-windows",
20909 &Vcursor_in_non_selected_windows,
20910 doc: /* *Cursor type to display in non-selected windows.
20911 t means to use hollow box cursor. See `cursor-type' for other values. */);
20912 Vcursor_in_non_selected_windows = Qt;
20913
20914 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
20915 doc: /* Alist specifying how to blink the cursor off.
20916 Each element has the form (ON-STATE . OFF-STATE). Whenever the
20917 `cursor-type' frame-parameter or variable equals ON-STATE,
20918 comparing using `equal', Emacs uses OFF-STATE to specify
20919 how to blink it off. */);
20920 Vblink_cursor_alist = Qnil;
20921
20922 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
20923 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
20924 automatic_hscrolling_p = 1;
20925
20926 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
20927 doc: /* *How many columns away from the window edge point is allowed to get
20928 before automatic hscrolling will horizontally scroll the window. */);
20929 hscroll_margin = 5;
20930
20931 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
20932 doc: /* *How many columns to scroll the window when point gets too close to the edge.
20933 When point is less than `automatic-hscroll-margin' columns from the window
20934 edge, automatic hscrolling will scroll the window by the amount of columns
20935 determined by this variable. If its value is a positive integer, scroll that
20936 many columns. If it's a positive floating-point number, it specifies the
20937 fraction of the window's width to scroll. If it's nil or zero, point will be
20938 centered horizontally after the scroll. Any other value, including negative
20939 numbers, are treated as if the value were zero.
20940
20941 Automatic hscrolling always moves point outside the scroll margin, so if
20942 point was more than scroll step columns inside the margin, the window will
20943 scroll more than the value given by the scroll step.
20944
20945 Note that the lower bound for automatic hscrolling specified by `scroll-left'
20946 and `scroll-right' overrides this variable's effect. */);
20947 Vhscroll_step = make_number (0);
20948
20949 DEFVAR_LISP ("image-types", &Vimage_types,
20950 doc: /* List of supported image types.
20951 Each element of the list is a symbol for a supported image type. */);
20952 Vimage_types = Qnil;
20953
20954 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
20955 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
20956 Bind this around calls to `message' to let it take effect. */);
20957 message_truncate_lines = 0;
20958
20959 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
20960 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
20961 Can be used to update submenus whose contents should vary. */);
20962 Vmenu_bar_update_hook = Qnil;
20963
20964 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
20965 doc: /* Non-nil means don't update menu bars. Internal use only. */);
20966 inhibit_menubar_update = 0;
20967
20968 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
20969 doc: /* Non-nil means don't eval Lisp during redisplay. */);
20970 inhibit_eval_during_redisplay = 0;
20971
20972 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
20973 doc: /* Non-nil means don't free realized faces. Internal use only. */);
20974 inhibit_free_realized_faces = 0;
20975
20976 #if GLYPH_DEBUG
20977 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
20978 doc: /* Inhibit try_window_id display optimization. */);
20979 inhibit_try_window_id = 0;
20980
20981 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
20982 doc: /* Inhibit try_window_reusing display optimization. */);
20983 inhibit_try_window_reusing = 0;
20984
20985 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
20986 doc: /* Inhibit try_cursor_movement display optimization. */);
20987 inhibit_try_cursor_movement = 0;
20988 #endif /* GLYPH_DEBUG */
20989 }
20990
20991
20992 /* Initialize this module when Emacs starts. */
20993
20994 void
20995 init_xdisp ()
20996 {
20997 Lisp_Object root_window;
20998 struct window *mini_w;
20999
21000 current_header_line_height = current_mode_line_height = -1;
21001
21002 CHARPOS (this_line_start_pos) = 0;
21003
21004 mini_w = XWINDOW (minibuf_window);
21005 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
21006
21007 if (!noninteractive)
21008 {
21009 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
21010 int i;
21011
21012 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
21013 set_window_height (root_window,
21014 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
21015 0);
21016 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
21017 set_window_height (minibuf_window, 1, 0);
21018
21019 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
21020 mini_w->total_cols = make_number (FRAME_COLS (f));
21021
21022 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
21023 scratch_glyph_row.glyphs[TEXT_AREA + 1]
21024 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
21025
21026 /* The default ellipsis glyphs `...'. */
21027 for (i = 0; i < 3; ++i)
21028 default_invis_vector[i] = make_number ('.');
21029 }
21030
21031 {
21032 /* Allocate the buffer for frame titles.
21033 Also used for `format-mode-line'. */
21034 int size = 100;
21035 frame_title_buf = (char *) xmalloc (size);
21036 frame_title_buf_end = frame_title_buf + size;
21037 frame_title_ptr = NULL;
21038 }
21039
21040 help_echo_showing_p = 0;
21041 }
21042
21043