]> code.delx.au - gnu-emacs/blob - src/xdisp.c
Revision: miles@gnu.org--gnu-2004/emacs--unicode--0--patch-43
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985,86,87,88,93,94,95,97,98,99,2000,01,02,03,04
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the description of direct update
37 operations, below.)
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay do? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
91 You will find a lot of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking an iterator structure (struct it)
124 argument.
125
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
131
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
138
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
146
147
148 Frame matrices.
149
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
156
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
168
169 #include <config.h>
170 #include <stdio.h>
171
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "character.h"
180 #include "charset.h"
181 #include "indent.h"
182 #include "commands.h"
183 #include "keymap.h"
184 #include "macros.h"
185 #include "disptab.h"
186 #include "termhooks.h"
187 #include "intervals.h"
188 #include "coding.h"
189 #include "process.h"
190 #include "region-cache.h"
191 #include "fontset.h"
192 #include "blockinput.h"
193
194 #ifdef HAVE_X_WINDOWS
195 #include "xterm.h"
196 #endif
197 #ifdef WINDOWSNT
198 #include "w32term.h"
199 #endif
200 #ifdef MAC_OS
201 #include "macterm.h"
202 #endif
203
204 #ifndef FRAME_X_OUTPUT
205 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
206 #endif
207
208 #define INFINITY 10000000
209
210 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
211 || defined (USE_GTK)
212 extern void set_frame_menubar P_ ((struct frame *f, int, int));
213 extern int pending_menu_activation;
214 #endif
215
216 extern int interrupt_input;
217 extern int command_loop_level;
218
219 extern int minibuffer_auto_raise;
220 extern Lisp_Object Vminibuffer_list;
221
222 extern Lisp_Object Qface;
223 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
224
225 extern Lisp_Object Voverriding_local_map;
226 extern Lisp_Object Voverriding_local_map_menu_flag;
227 extern Lisp_Object Qmenu_item;
228 extern Lisp_Object Qwhen;
229 extern Lisp_Object Qhelp_echo;
230
231 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
232 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
233 Lisp_Object Qredisplay_end_trigger_functions;
234 Lisp_Object Qinhibit_point_motion_hooks;
235 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
236 Lisp_Object Qfontified;
237 Lisp_Object Qgrow_only;
238 Lisp_Object Qinhibit_eval_during_redisplay;
239 Lisp_Object Qbuffer_position, Qposition, Qobject;
240
241 /* Cursor shapes */
242 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
243
244 /* Pointer shapes */
245 Lisp_Object Qarrow, Qhand, Qtext;
246
247 Lisp_Object Qrisky_local_variable;
248
249 /* Holds the list (error). */
250 Lisp_Object list_of_error;
251
252 /* Functions called to fontify regions of text. */
253
254 Lisp_Object Vfontification_functions;
255 Lisp_Object Qfontification_functions;
256
257 /* Non-zero means automatically select any window when the mouse
258 cursor moves into it. */
259 int mouse_autoselect_window;
260
261 /* Non-zero means draw tool bar buttons raised when the mouse moves
262 over them. */
263
264 int auto_raise_tool_bar_buttons_p;
265
266 /* Margin around tool bar buttons in pixels. */
267
268 Lisp_Object Vtool_bar_button_margin;
269
270 /* Thickness of shadow to draw around tool bar buttons. */
271
272 EMACS_INT tool_bar_button_relief;
273
274 /* Non-zero means automatically resize tool-bars so that all tool-bar
275 items are visible, and no blank lines remain. */
276
277 int auto_resize_tool_bars_p;
278
279 /* Non-zero means draw block and hollow cursor as wide as the glyph
280 under it. For example, if a block cursor is over a tab, it will be
281 drawn as wide as that tab on the display. */
282
283 int x_stretch_cursor_p;
284
285 /* Non-nil means don't actually do any redisplay. */
286
287 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
288
289 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
290
291 int inhibit_eval_during_redisplay;
292
293 /* Names of text properties relevant for redisplay. */
294
295 Lisp_Object Qdisplay;
296 extern Lisp_Object Qface, Qinvisible, Qwidth;
297
298 /* Symbols used in text property values. */
299
300 Lisp_Object Vdisplay_pixels_per_inch;
301 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
302 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
303 Lisp_Object Qslice;
304 Lisp_Object Qcenter;
305 Lisp_Object Qmargin, Qpointer;
306 Lisp_Object Qline_height, Qtotal;
307 extern Lisp_Object Qheight;
308 extern Lisp_Object QCwidth, QCheight, QCascent;
309 extern Lisp_Object Qscroll_bar;
310 extern Lisp_Object Qcursor;
311
312 /* Non-nil means highlight trailing whitespace. */
313
314 Lisp_Object Vshow_trailing_whitespace;
315
316 #ifdef HAVE_WINDOW_SYSTEM
317 extern Lisp_Object Voverflow_newline_into_fringe;
318
319 /* Test if overflow newline into fringe. Called with iterator IT
320 at or past right window margin, and with IT->current_x set. */
321
322 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
323 (!NILP (Voverflow_newline_into_fringe) \
324 && FRAME_WINDOW_P (it->f) \
325 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
326 && it->current_x == it->last_visible_x)
327
328 #endif /* HAVE_WINDOW_SYSTEM */
329
330 /* Non-nil means show the text cursor in void text areas
331 i.e. in blank areas after eol and eob. This used to be
332 the default in 21.3. */
333
334 Lisp_Object Vvoid_text_area_pointer;
335
336 /* Name of the face used to highlight trailing whitespace. */
337
338 Lisp_Object Qtrailing_whitespace;
339
340 /* The symbol `image' which is the car of the lists used to represent
341 images in Lisp. */
342
343 Lisp_Object Qimage;
344
345 /* The image map types. */
346 Lisp_Object QCmap, QCpointer;
347 Lisp_Object Qrect, Qcircle, Qpoly;
348
349 /* Non-zero means print newline to stdout before next mini-buffer
350 message. */
351
352 int noninteractive_need_newline;
353
354 /* Non-zero means print newline to message log before next message. */
355
356 static int message_log_need_newline;
357
358 /* Three markers that message_dolog uses.
359 It could allocate them itself, but that causes trouble
360 in handling memory-full errors. */
361 static Lisp_Object message_dolog_marker1;
362 static Lisp_Object message_dolog_marker2;
363 static Lisp_Object message_dolog_marker3;
364 \f
365 /* The buffer position of the first character appearing entirely or
366 partially on the line of the selected window which contains the
367 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
368 redisplay optimization in redisplay_internal. */
369
370 static struct text_pos this_line_start_pos;
371
372 /* Number of characters past the end of the line above, including the
373 terminating newline. */
374
375 static struct text_pos this_line_end_pos;
376
377 /* The vertical positions and the height of this line. */
378
379 static int this_line_vpos;
380 static int this_line_y;
381 static int this_line_pixel_height;
382
383 /* X position at which this display line starts. Usually zero;
384 negative if first character is partially visible. */
385
386 static int this_line_start_x;
387
388 /* Buffer that this_line_.* variables are referring to. */
389
390 static struct buffer *this_line_buffer;
391
392 /* Nonzero means truncate lines in all windows less wide than the
393 frame. */
394
395 int truncate_partial_width_windows;
396
397 /* A flag to control how to display unibyte 8-bit character. */
398
399 int unibyte_display_via_language_environment;
400
401 /* Nonzero means we have more than one non-mini-buffer-only frame.
402 Not guaranteed to be accurate except while parsing
403 frame-title-format. */
404
405 int multiple_frames;
406
407 Lisp_Object Vglobal_mode_string;
408
409
410 /* List of variables (symbols) which hold markers for overlay arrows.
411 The symbols on this list are examined during redisplay to determine
412 where to display overlay arrows. */
413
414 Lisp_Object Voverlay_arrow_variable_list;
415
416 /* Marker for where to display an arrow on top of the buffer text. */
417
418 Lisp_Object Voverlay_arrow_position;
419
420 /* String to display for the arrow. Only used on terminal frames. */
421
422 Lisp_Object Voverlay_arrow_string;
423
424 /* Values of those variables at last redisplay are stored as
425 properties on `overlay-arrow-position' symbol. However, if
426 Voverlay_arrow_position is a marker, last-arrow-position is its
427 numerical position. */
428
429 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
430
431 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
432 properties on a symbol in overlay-arrow-variable-list. */
433
434 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
435
436 /* Like mode-line-format, but for the title bar on a visible frame. */
437
438 Lisp_Object Vframe_title_format;
439
440 /* Like mode-line-format, but for the title bar on an iconified frame. */
441
442 Lisp_Object Vicon_title_format;
443
444 /* List of functions to call when a window's size changes. These
445 functions get one arg, a frame on which one or more windows' sizes
446 have changed. */
447
448 static Lisp_Object Vwindow_size_change_functions;
449
450 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
451
452 /* Nonzero if overlay arrow has been displayed once in this window. */
453
454 static int overlay_arrow_seen;
455
456 /* Nonzero means highlight the region even in nonselected windows. */
457
458 int highlight_nonselected_windows;
459
460 /* If cursor motion alone moves point off frame, try scrolling this
461 many lines up or down if that will bring it back. */
462
463 static EMACS_INT scroll_step;
464
465 /* Nonzero means scroll just far enough to bring point back on the
466 screen, when appropriate. */
467
468 static EMACS_INT scroll_conservatively;
469
470 /* Recenter the window whenever point gets within this many lines of
471 the top or bottom of the window. This value is translated into a
472 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
473 that there is really a fixed pixel height scroll margin. */
474
475 EMACS_INT scroll_margin;
476
477 /* Number of windows showing the buffer of the selected window (or
478 another buffer with the same base buffer). keyboard.c refers to
479 this. */
480
481 int buffer_shared;
482
483 /* Vector containing glyphs for an ellipsis `...'. */
484
485 static Lisp_Object default_invis_vector[3];
486
487 /* Zero means display the mode-line/header-line/menu-bar in the default face
488 (this slightly odd definition is for compatibility with previous versions
489 of emacs), non-zero means display them using their respective faces.
490
491 This variable is deprecated. */
492
493 int mode_line_inverse_video;
494
495 /* Prompt to display in front of the mini-buffer contents. */
496
497 Lisp_Object minibuf_prompt;
498
499 /* Width of current mini-buffer prompt. Only set after display_line
500 of the line that contains the prompt. */
501
502 int minibuf_prompt_width;
503
504 /* This is the window where the echo area message was displayed. It
505 is always a mini-buffer window, but it may not be the same window
506 currently active as a mini-buffer. */
507
508 Lisp_Object echo_area_window;
509
510 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
511 pushes the current message and the value of
512 message_enable_multibyte on the stack, the function restore_message
513 pops the stack and displays MESSAGE again. */
514
515 Lisp_Object Vmessage_stack;
516
517 /* Nonzero means multibyte characters were enabled when the echo area
518 message was specified. */
519
520 int message_enable_multibyte;
521
522 /* Nonzero if we should redraw the mode lines on the next redisplay. */
523
524 int update_mode_lines;
525
526 /* Nonzero if window sizes or contents have changed since last
527 redisplay that finished. */
528
529 int windows_or_buffers_changed;
530
531 /* Nonzero means a frame's cursor type has been changed. */
532
533 int cursor_type_changed;
534
535 /* Nonzero after display_mode_line if %l was used and it displayed a
536 line number. */
537
538 int line_number_displayed;
539
540 /* Maximum buffer size for which to display line numbers. */
541
542 Lisp_Object Vline_number_display_limit;
543
544 /* Line width to consider when repositioning for line number display. */
545
546 static EMACS_INT line_number_display_limit_width;
547
548 /* Number of lines to keep in the message log buffer. t means
549 infinite. nil means don't log at all. */
550
551 Lisp_Object Vmessage_log_max;
552
553 /* The name of the *Messages* buffer, a string. */
554
555 static Lisp_Object Vmessages_buffer_name;
556
557 /* Current, index 0, and last displayed echo area message. Either
558 buffers from echo_buffers, or nil to indicate no message. */
559
560 Lisp_Object echo_area_buffer[2];
561
562 /* The buffers referenced from echo_area_buffer. */
563
564 static Lisp_Object echo_buffer[2];
565
566 /* A vector saved used in with_area_buffer to reduce consing. */
567
568 static Lisp_Object Vwith_echo_area_save_vector;
569
570 /* Non-zero means display_echo_area should display the last echo area
571 message again. Set by redisplay_preserve_echo_area. */
572
573 static int display_last_displayed_message_p;
574
575 /* Nonzero if echo area is being used by print; zero if being used by
576 message. */
577
578 int message_buf_print;
579
580 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
581
582 Lisp_Object Qinhibit_menubar_update;
583 int inhibit_menubar_update;
584
585 /* Maximum height for resizing mini-windows. Either a float
586 specifying a fraction of the available height, or an integer
587 specifying a number of lines. */
588
589 Lisp_Object Vmax_mini_window_height;
590
591 /* Non-zero means messages should be displayed with truncated
592 lines instead of being continued. */
593
594 int message_truncate_lines;
595 Lisp_Object Qmessage_truncate_lines;
596
597 /* Set to 1 in clear_message to make redisplay_internal aware
598 of an emptied echo area. */
599
600 static int message_cleared_p;
601
602 /* Non-zero means we want a hollow cursor in windows that are not
603 selected. Zero means there's no cursor in such windows. */
604
605 Lisp_Object Vcursor_in_non_selected_windows;
606 Lisp_Object Qcursor_in_non_selected_windows;
607
608 /* How to blink the default frame cursor off. */
609 Lisp_Object Vblink_cursor_alist;
610
611 /* A scratch glyph row with contents used for generating truncation
612 glyphs. Also used in direct_output_for_insert. */
613
614 #define MAX_SCRATCH_GLYPHS 100
615 struct glyph_row scratch_glyph_row;
616 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
617
618 /* Ascent and height of the last line processed by move_it_to. */
619
620 static int last_max_ascent, last_height;
621
622 /* Non-zero if there's a help-echo in the echo area. */
623
624 int help_echo_showing_p;
625
626 /* If >= 0, computed, exact values of mode-line and header-line height
627 to use in the macros CURRENT_MODE_LINE_HEIGHT and
628 CURRENT_HEADER_LINE_HEIGHT. */
629
630 int current_mode_line_height, current_header_line_height;
631
632 /* The maximum distance to look ahead for text properties. Values
633 that are too small let us call compute_char_face and similar
634 functions too often which is expensive. Values that are too large
635 let us call compute_char_face and alike too often because we
636 might not be interested in text properties that far away. */
637
638 #define TEXT_PROP_DISTANCE_LIMIT 100
639
640 #if GLYPH_DEBUG
641
642 /* Variables to turn off display optimizations from Lisp. */
643
644 int inhibit_try_window_id, inhibit_try_window_reusing;
645 int inhibit_try_cursor_movement;
646
647 /* Non-zero means print traces of redisplay if compiled with
648 GLYPH_DEBUG != 0. */
649
650 int trace_redisplay_p;
651
652 #endif /* GLYPH_DEBUG */
653
654 #ifdef DEBUG_TRACE_MOVE
655 /* Non-zero means trace with TRACE_MOVE to stderr. */
656 int trace_move;
657
658 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
659 #else
660 #define TRACE_MOVE(x) (void) 0
661 #endif
662
663 /* Non-zero means automatically scroll windows horizontally to make
664 point visible. */
665
666 int automatic_hscrolling_p;
667
668 /* How close to the margin can point get before the window is scrolled
669 horizontally. */
670 EMACS_INT hscroll_margin;
671
672 /* How much to scroll horizontally when point is inside the above margin. */
673 Lisp_Object Vhscroll_step;
674
675 /* The variable `resize-mini-windows'. If nil, don't resize
676 mini-windows. If t, always resize them to fit the text they
677 display. If `grow-only', let mini-windows grow only until they
678 become empty. */
679
680 Lisp_Object Vresize_mini_windows;
681
682 /* Buffer being redisplayed -- for redisplay_window_error. */
683
684 struct buffer *displayed_buffer;
685
686 /* Value returned from text property handlers (see below). */
687
688 enum prop_handled
689 {
690 HANDLED_NORMALLY,
691 HANDLED_RECOMPUTE_PROPS,
692 HANDLED_OVERLAY_STRING_CONSUMED,
693 HANDLED_RETURN
694 };
695
696 /* A description of text properties that redisplay is interested
697 in. */
698
699 struct props
700 {
701 /* The name of the property. */
702 Lisp_Object *name;
703
704 /* A unique index for the property. */
705 enum prop_idx idx;
706
707 /* A handler function called to set up iterator IT from the property
708 at IT's current position. Value is used to steer handle_stop. */
709 enum prop_handled (*handler) P_ ((struct it *it));
710 };
711
712 static enum prop_handled handle_face_prop P_ ((struct it *));
713 static enum prop_handled handle_invisible_prop P_ ((struct it *));
714 static enum prop_handled handle_display_prop P_ ((struct it *));
715 static enum prop_handled handle_composition_prop P_ ((struct it *));
716 static enum prop_handled handle_overlay_change P_ ((struct it *));
717 static enum prop_handled handle_fontified_prop P_ ((struct it *));
718 static enum prop_handled handle_auto_composed_prop P_ ((struct it *));
719
720 /* Properties handled by iterators. */
721
722 static struct props it_props[] =
723 {
724 {&Qauto_composed, AUTO_COMPOSED_PROP_IDX, handle_auto_composed_prop},
725 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
726 /* Handle `face' before `display' because some sub-properties of
727 `display' need to know the face. */
728 {&Qface, FACE_PROP_IDX, handle_face_prop},
729 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
730 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
731 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
732 {NULL, 0, NULL}
733 };
734
735 /* Value is the position described by X. If X is a marker, value is
736 the marker_position of X. Otherwise, value is X. */
737
738 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
739
740 /* Enumeration returned by some move_it_.* functions internally. */
741
742 enum move_it_result
743 {
744 /* Not used. Undefined value. */
745 MOVE_UNDEFINED,
746
747 /* Move ended at the requested buffer position or ZV. */
748 MOVE_POS_MATCH_OR_ZV,
749
750 /* Move ended at the requested X pixel position. */
751 MOVE_X_REACHED,
752
753 /* Move within a line ended at the end of a line that must be
754 continued. */
755 MOVE_LINE_CONTINUED,
756
757 /* Move within a line ended at the end of a line that would
758 be displayed truncated. */
759 MOVE_LINE_TRUNCATED,
760
761 /* Move within a line ended at a line end. */
762 MOVE_NEWLINE_OR_CR
763 };
764
765 /* This counter is used to clear the face cache every once in a while
766 in redisplay_internal. It is incremented for each redisplay.
767 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
768 cleared. */
769
770 #define CLEAR_FACE_CACHE_COUNT 500
771 static int clear_face_cache_count;
772
773 /* Record the previous terminal frame we displayed. */
774
775 static struct frame *previous_terminal_frame;
776
777 /* Non-zero while redisplay_internal is in progress. */
778
779 int redisplaying_p;
780
781 /* Non-zero means don't free realized faces. Bound while freeing
782 realized faces is dangerous because glyph matrices might still
783 reference them. */
784
785 int inhibit_free_realized_faces;
786 Lisp_Object Qinhibit_free_realized_faces;
787
788 /* If a string, XTread_socket generates an event to display that string.
789 (The display is done in read_char.) */
790
791 Lisp_Object help_echo_string;
792 Lisp_Object help_echo_window;
793 Lisp_Object help_echo_object;
794 int help_echo_pos;
795
796 /* Temporary variable for XTread_socket. */
797
798 Lisp_Object previous_help_echo_string;
799
800 /* Null glyph slice */
801
802 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
803
804 \f
805 /* Function prototypes. */
806
807 static void setup_for_ellipsis P_ ((struct it *));
808 static void mark_window_display_accurate_1 P_ ((struct window *, int));
809 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
810 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
811 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
812 static int redisplay_mode_lines P_ ((Lisp_Object, int));
813 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
814
815 #if 0
816 static int invisible_text_between_p P_ ((struct it *, int, int));
817 #endif
818
819 static int next_element_from_ellipsis P_ ((struct it *));
820 static void pint2str P_ ((char *, int, int));
821 static void pint2hrstr P_ ((char *, int, int));
822 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
823 struct text_pos));
824 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
825 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
826 static void store_frame_title_char P_ ((char));
827 static int store_frame_title P_ ((const unsigned char *, int, int));
828 static void x_consider_frame_title P_ ((Lisp_Object));
829 static void handle_stop P_ ((struct it *));
830 static int tool_bar_lines_needed P_ ((struct frame *));
831 static int single_display_prop_intangible_p P_ ((Lisp_Object));
832 static void ensure_echo_area_buffers P_ ((void));
833 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
834 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
835 static int with_echo_area_buffer P_ ((struct window *, int,
836 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
837 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
838 static void clear_garbaged_frames P_ ((void));
839 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
840 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
841 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
842 static int display_echo_area P_ ((struct window *));
843 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
844 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
845 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
846 static int string_char_and_length P_ ((const unsigned char *, int, int *));
847 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
848 struct text_pos));
849 static int compute_window_start_on_continuation_line P_ ((struct window *));
850 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
851 static void insert_left_trunc_glyphs P_ ((struct it *));
852 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
853 Lisp_Object));
854 static void extend_face_to_end_of_line P_ ((struct it *));
855 static int append_space_for_newline P_ ((struct it *, int));
856 static int make_cursor_line_fully_visible P_ ((struct window *, int));
857 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
858 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
859 static int trailing_whitespace_p P_ ((int));
860 static int message_log_check_duplicate P_ ((int, int, int, int));
861 static void push_it P_ ((struct it *));
862 static void pop_it P_ ((struct it *));
863 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
864 static void select_frame_for_redisplay P_ ((Lisp_Object));
865 static void redisplay_internal P_ ((int));
866 static int echo_area_display P_ ((int));
867 static void redisplay_windows P_ ((Lisp_Object));
868 static void redisplay_window P_ ((Lisp_Object, int));
869 static Lisp_Object redisplay_window_error ();
870 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
871 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
872 static void update_menu_bar P_ ((struct frame *, int));
873 static int try_window_reusing_current_matrix P_ ((struct window *));
874 static int try_window_id P_ ((struct window *));
875 static int display_line P_ ((struct it *));
876 static int display_mode_lines P_ ((struct window *));
877 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
878 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
879 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
880 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
881 static void display_menu_bar P_ ((struct window *));
882 static int display_count_lines P_ ((int, int, int, int, int *));
883 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
884 int, int, struct it *, int, int, int, int));
885 static void compute_line_metrics P_ ((struct it *));
886 static void run_redisplay_end_trigger_hook P_ ((struct it *));
887 static int get_overlay_strings P_ ((struct it *, int));
888 static void next_overlay_string P_ ((struct it *));
889 static void reseat P_ ((struct it *, struct text_pos, int));
890 static void reseat_1 P_ ((struct it *, struct text_pos, int));
891 static void back_to_previous_visible_line_start P_ ((struct it *));
892 static void reseat_at_previous_visible_line_start P_ ((struct it *));
893 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
894 static int next_element_from_display_vector P_ ((struct it *));
895 static int next_element_from_string P_ ((struct it *));
896 static int next_element_from_c_string P_ ((struct it *));
897 static int next_element_from_buffer P_ ((struct it *));
898 static int next_element_from_composition P_ ((struct it *));
899 static int next_element_from_image P_ ((struct it *));
900 static int next_element_from_stretch P_ ((struct it *));
901 static void load_overlay_strings P_ ((struct it *, int));
902 static int init_from_display_pos P_ ((struct it *, struct window *,
903 struct display_pos *));
904 static void reseat_to_string P_ ((struct it *, unsigned char *,
905 Lisp_Object, int, int, int, int));
906 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
907 int, int, int));
908 void move_it_vertically_backward P_ ((struct it *, int));
909 static void init_to_row_start P_ ((struct it *, struct window *,
910 struct glyph_row *));
911 static int init_to_row_end P_ ((struct it *, struct window *,
912 struct glyph_row *));
913 static void back_to_previous_line_start P_ ((struct it *));
914 static int forward_to_next_line_start P_ ((struct it *, int *));
915 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
916 Lisp_Object, int));
917 static struct text_pos string_pos P_ ((int, Lisp_Object));
918 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
919 static int number_of_chars P_ ((unsigned char *, int));
920 static void compute_stop_pos P_ ((struct it *));
921 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
922 Lisp_Object));
923 static int face_before_or_after_it_pos P_ ((struct it *, int));
924 static int next_overlay_change P_ ((int));
925 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
926 Lisp_Object, struct text_pos *,
927 int));
928 static int underlying_face_id P_ ((struct it *));
929 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
930 struct window *));
931
932 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
933 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
934
935 #ifdef HAVE_WINDOW_SYSTEM
936
937 static void update_tool_bar P_ ((struct frame *, int));
938 static void build_desired_tool_bar_string P_ ((struct frame *f));
939 static int redisplay_tool_bar P_ ((struct frame *));
940 static void display_tool_bar_line P_ ((struct it *));
941 static void notice_overwritten_cursor P_ ((struct window *,
942 enum glyph_row_area,
943 int, int, int, int));
944
945
946
947 #endif /* HAVE_WINDOW_SYSTEM */
948
949 \f
950 /***********************************************************************
951 Window display dimensions
952 ***********************************************************************/
953
954 /* Return the bottom boundary y-position for text lines in window W.
955 This is the first y position at which a line cannot start.
956 It is relative to the top of the window.
957
958 This is the height of W minus the height of a mode line, if any. */
959
960 INLINE int
961 window_text_bottom_y (w)
962 struct window *w;
963 {
964 int height = WINDOW_TOTAL_HEIGHT (w);
965
966 if (WINDOW_WANTS_MODELINE_P (w))
967 height -= CURRENT_MODE_LINE_HEIGHT (w);
968 return height;
969 }
970
971 /* Return the pixel width of display area AREA of window W. AREA < 0
972 means return the total width of W, not including fringes to
973 the left and right of the window. */
974
975 INLINE int
976 window_box_width (w, area)
977 struct window *w;
978 int area;
979 {
980 int cols = XFASTINT (w->total_cols);
981 int pixels = 0;
982
983 if (!w->pseudo_window_p)
984 {
985 cols -= WINDOW_SCROLL_BAR_COLS (w);
986
987 if (area == TEXT_AREA)
988 {
989 if (INTEGERP (w->left_margin_cols))
990 cols -= XFASTINT (w->left_margin_cols);
991 if (INTEGERP (w->right_margin_cols))
992 cols -= XFASTINT (w->right_margin_cols);
993 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
994 }
995 else if (area == LEFT_MARGIN_AREA)
996 {
997 cols = (INTEGERP (w->left_margin_cols)
998 ? XFASTINT (w->left_margin_cols) : 0);
999 pixels = 0;
1000 }
1001 else if (area == RIGHT_MARGIN_AREA)
1002 {
1003 cols = (INTEGERP (w->right_margin_cols)
1004 ? XFASTINT (w->right_margin_cols) : 0);
1005 pixels = 0;
1006 }
1007 }
1008
1009 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1010 }
1011
1012
1013 /* Return the pixel height of the display area of window W, not
1014 including mode lines of W, if any. */
1015
1016 INLINE int
1017 window_box_height (w)
1018 struct window *w;
1019 {
1020 struct frame *f = XFRAME (w->frame);
1021 int height = WINDOW_TOTAL_HEIGHT (w);
1022
1023 xassert (height >= 0);
1024
1025 /* Note: the code below that determines the mode-line/header-line
1026 height is essentially the same as that contained in the macro
1027 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1028 the appropriate glyph row has its `mode_line_p' flag set,
1029 and if it doesn't, uses estimate_mode_line_height instead. */
1030
1031 if (WINDOW_WANTS_MODELINE_P (w))
1032 {
1033 struct glyph_row *ml_row
1034 = (w->current_matrix && w->current_matrix->rows
1035 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1036 : 0);
1037 if (ml_row && ml_row->mode_line_p)
1038 height -= ml_row->height;
1039 else
1040 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1041 }
1042
1043 if (WINDOW_WANTS_HEADER_LINE_P (w))
1044 {
1045 struct glyph_row *hl_row
1046 = (w->current_matrix && w->current_matrix->rows
1047 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1048 : 0);
1049 if (hl_row && hl_row->mode_line_p)
1050 height -= hl_row->height;
1051 else
1052 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1053 }
1054
1055 /* With a very small font and a mode-line that's taller than
1056 default, we might end up with a negative height. */
1057 return max (0, height);
1058 }
1059
1060 /* Return the window-relative coordinate of the left edge of display
1061 area AREA of window W. AREA < 0 means return the left edge of the
1062 whole window, to the right of the left fringe of W. */
1063
1064 INLINE int
1065 window_box_left_offset (w, area)
1066 struct window *w;
1067 int area;
1068 {
1069 int x;
1070
1071 if (w->pseudo_window_p)
1072 return 0;
1073
1074 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1075
1076 if (area == TEXT_AREA)
1077 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1078 + window_box_width (w, LEFT_MARGIN_AREA));
1079 else if (area == RIGHT_MARGIN_AREA)
1080 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1081 + window_box_width (w, LEFT_MARGIN_AREA)
1082 + window_box_width (w, TEXT_AREA)
1083 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1084 ? 0
1085 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1086 else if (area == LEFT_MARGIN_AREA
1087 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1088 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1089
1090 return x;
1091 }
1092
1093
1094 /* Return the window-relative coordinate of the right edge of display
1095 area AREA of window W. AREA < 0 means return the left edge of the
1096 whole window, to the left of the right fringe of W. */
1097
1098 INLINE int
1099 window_box_right_offset (w, area)
1100 struct window *w;
1101 int area;
1102 {
1103 return window_box_left_offset (w, area) + window_box_width (w, area);
1104 }
1105
1106 /* Return the frame-relative coordinate of the left edge of display
1107 area AREA of window W. AREA < 0 means return the left edge of the
1108 whole window, to the right of the left fringe of W. */
1109
1110 INLINE int
1111 window_box_left (w, area)
1112 struct window *w;
1113 int area;
1114 {
1115 struct frame *f = XFRAME (w->frame);
1116 int x;
1117
1118 if (w->pseudo_window_p)
1119 return FRAME_INTERNAL_BORDER_WIDTH (f);
1120
1121 x = (WINDOW_LEFT_EDGE_X (w)
1122 + window_box_left_offset (w, area));
1123
1124 return x;
1125 }
1126
1127
1128 /* Return the frame-relative coordinate of the right edge of display
1129 area AREA of window W. AREA < 0 means return the left edge of the
1130 whole window, to the left of the right fringe of W. */
1131
1132 INLINE int
1133 window_box_right (w, area)
1134 struct window *w;
1135 int area;
1136 {
1137 return window_box_left (w, area) + window_box_width (w, area);
1138 }
1139
1140 /* Get the bounding box of the display area AREA of window W, without
1141 mode lines, in frame-relative coordinates. AREA < 0 means the
1142 whole window, not including the left and right fringes of
1143 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1144 coordinates of the upper-left corner of the box. Return in
1145 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1146
1147 INLINE void
1148 window_box (w, area, box_x, box_y, box_width, box_height)
1149 struct window *w;
1150 int area;
1151 int *box_x, *box_y, *box_width, *box_height;
1152 {
1153 if (box_width)
1154 *box_width = window_box_width (w, area);
1155 if (box_height)
1156 *box_height = window_box_height (w);
1157 if (box_x)
1158 *box_x = window_box_left (w, area);
1159 if (box_y)
1160 {
1161 *box_y = WINDOW_TOP_EDGE_Y (w);
1162 if (WINDOW_WANTS_HEADER_LINE_P (w))
1163 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1164 }
1165 }
1166
1167
1168 /* Get the bounding box of the display area AREA of window W, without
1169 mode lines. AREA < 0 means the whole window, not including the
1170 left and right fringe of the window. Return in *TOP_LEFT_X
1171 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1172 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1173 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1174 box. */
1175
1176 INLINE void
1177 window_box_edges (w, area, top_left_x, top_left_y,
1178 bottom_right_x, bottom_right_y)
1179 struct window *w;
1180 int area;
1181 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1182 {
1183 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1184 bottom_right_y);
1185 *bottom_right_x += *top_left_x;
1186 *bottom_right_y += *top_left_y;
1187 }
1188
1189
1190 \f
1191 /***********************************************************************
1192 Utilities
1193 ***********************************************************************/
1194
1195 /* Return the bottom y-position of the line the iterator IT is in.
1196 This can modify IT's settings. */
1197
1198 int
1199 line_bottom_y (it)
1200 struct it *it;
1201 {
1202 int line_height = it->max_ascent + it->max_descent;
1203 int line_top_y = it->current_y;
1204
1205 if (line_height == 0)
1206 {
1207 if (last_height)
1208 line_height = last_height;
1209 else if (IT_CHARPOS (*it) < ZV)
1210 {
1211 move_it_by_lines (it, 1, 1);
1212 line_height = (it->max_ascent || it->max_descent
1213 ? it->max_ascent + it->max_descent
1214 : last_height);
1215 }
1216 else
1217 {
1218 struct glyph_row *row = it->glyph_row;
1219
1220 /* Use the default character height. */
1221 it->glyph_row = NULL;
1222 it->what = IT_CHARACTER;
1223 it->c = ' ';
1224 it->len = 1;
1225 PRODUCE_GLYPHS (it);
1226 line_height = it->ascent + it->descent;
1227 it->glyph_row = row;
1228 }
1229 }
1230
1231 return line_top_y + line_height;
1232 }
1233
1234
1235 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1236 1 if POS is visible and the line containing POS is fully visible.
1237 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1238 and header-lines heights. */
1239
1240 int
1241 pos_visible_p (w, charpos, fully, x, y, exact_mode_line_heights_p)
1242 struct window *w;
1243 int charpos, *fully, *x, *y, exact_mode_line_heights_p;
1244 {
1245 struct it it;
1246 struct text_pos top;
1247 int visible_p;
1248 struct buffer *old_buffer = NULL;
1249
1250 if (XBUFFER (w->buffer) != current_buffer)
1251 {
1252 old_buffer = current_buffer;
1253 set_buffer_internal_1 (XBUFFER (w->buffer));
1254 }
1255
1256 *fully = visible_p = 0;
1257 SET_TEXT_POS_FROM_MARKER (top, w->start);
1258
1259 /* Compute exact mode line heights, if requested. */
1260 if (exact_mode_line_heights_p)
1261 {
1262 if (WINDOW_WANTS_MODELINE_P (w))
1263 current_mode_line_height
1264 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1265 current_buffer->mode_line_format);
1266
1267 if (WINDOW_WANTS_HEADER_LINE_P (w))
1268 current_header_line_height
1269 = display_mode_line (w, HEADER_LINE_FACE_ID,
1270 current_buffer->header_line_format);
1271 }
1272
1273 start_display (&it, w, top);
1274 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1275 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1276
1277 /* Note that we may overshoot because of invisible text. */
1278 if (IT_CHARPOS (it) >= charpos)
1279 {
1280 int top_y = it.current_y;
1281 int bottom_y = line_bottom_y (&it);
1282 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1283
1284 if (top_y < window_top_y)
1285 visible_p = bottom_y > window_top_y;
1286 else if (top_y < it.last_visible_y)
1287 {
1288 visible_p = 1;
1289 *fully = bottom_y <= it.last_visible_y;
1290 }
1291 if (visible_p && x)
1292 {
1293 *x = it.current_x;
1294 *y = max (top_y + it.max_ascent - it.ascent, window_top_y);
1295 }
1296 }
1297 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1298 {
1299 struct it it2;
1300
1301 it2 = it;
1302 move_it_by_lines (&it, 1, 0);
1303 if (charpos < IT_CHARPOS (it))
1304 {
1305 visible_p = 1;
1306 if (x)
1307 {
1308 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1309 *x = it2.current_x;
1310 *y = it2.current_y + it2.max_ascent - it2.ascent;
1311 }
1312 }
1313 }
1314
1315 if (old_buffer)
1316 set_buffer_internal_1 (old_buffer);
1317
1318 current_header_line_height = current_mode_line_height = -1;
1319
1320 return visible_p;
1321 }
1322
1323
1324 /* Return the next character from STR which is MAXLEN bytes long.
1325 Return in *LEN the length of the character. This is like
1326 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1327 we find one, we return a `?', but with the length of the invalid
1328 character. */
1329
1330 static INLINE int
1331 string_char_and_length (str, maxlen, len)
1332 const unsigned char *str;
1333 int maxlen, *len;
1334 {
1335 int c;
1336
1337 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1338 if (!CHAR_VALID_P (c, 1))
1339 /* We may not change the length here because other places in Emacs
1340 don't use this function, i.e. they silently accept invalid
1341 characters. */
1342 c = '?';
1343
1344 return c;
1345 }
1346
1347
1348
1349 /* Given a position POS containing a valid character and byte position
1350 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1351
1352 static struct text_pos
1353 string_pos_nchars_ahead (pos, string, nchars)
1354 struct text_pos pos;
1355 Lisp_Object string;
1356 int nchars;
1357 {
1358 xassert (STRINGP (string) && nchars >= 0);
1359
1360 if (STRING_MULTIBYTE (string))
1361 {
1362 int rest = SBYTES (string) - BYTEPOS (pos);
1363 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1364 int len;
1365
1366 while (nchars--)
1367 {
1368 string_char_and_length (p, rest, &len);
1369 p += len, rest -= len;
1370 xassert (rest >= 0);
1371 CHARPOS (pos) += 1;
1372 BYTEPOS (pos) += len;
1373 }
1374 }
1375 else
1376 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1377
1378 return pos;
1379 }
1380
1381
1382 /* Value is the text position, i.e. character and byte position,
1383 for character position CHARPOS in STRING. */
1384
1385 static INLINE struct text_pos
1386 string_pos (charpos, string)
1387 int charpos;
1388 Lisp_Object string;
1389 {
1390 struct text_pos pos;
1391 xassert (STRINGP (string));
1392 xassert (charpos >= 0);
1393 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1394 return pos;
1395 }
1396
1397
1398 /* Value is a text position, i.e. character and byte position, for
1399 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1400 means recognize multibyte characters. */
1401
1402 static struct text_pos
1403 c_string_pos (charpos, s, multibyte_p)
1404 int charpos;
1405 unsigned char *s;
1406 int multibyte_p;
1407 {
1408 struct text_pos pos;
1409
1410 xassert (s != NULL);
1411 xassert (charpos >= 0);
1412
1413 if (multibyte_p)
1414 {
1415 int rest = strlen (s), len;
1416
1417 SET_TEXT_POS (pos, 0, 0);
1418 while (charpos--)
1419 {
1420 string_char_and_length (s, rest, &len);
1421 s += len, rest -= len;
1422 xassert (rest >= 0);
1423 CHARPOS (pos) += 1;
1424 BYTEPOS (pos) += len;
1425 }
1426 }
1427 else
1428 SET_TEXT_POS (pos, charpos, charpos);
1429
1430 return pos;
1431 }
1432
1433
1434 /* Value is the number of characters in C string S. MULTIBYTE_P
1435 non-zero means recognize multibyte characters. */
1436
1437 static int
1438 number_of_chars (s, multibyte_p)
1439 unsigned char *s;
1440 int multibyte_p;
1441 {
1442 int nchars;
1443
1444 if (multibyte_p)
1445 {
1446 int rest = strlen (s), len;
1447 unsigned char *p = (unsigned char *) s;
1448
1449 for (nchars = 0; rest > 0; ++nchars)
1450 {
1451 string_char_and_length (p, rest, &len);
1452 rest -= len, p += len;
1453 }
1454 }
1455 else
1456 nchars = strlen (s);
1457
1458 return nchars;
1459 }
1460
1461
1462 /* Compute byte position NEWPOS->bytepos corresponding to
1463 NEWPOS->charpos. POS is a known position in string STRING.
1464 NEWPOS->charpos must be >= POS.charpos. */
1465
1466 static void
1467 compute_string_pos (newpos, pos, string)
1468 struct text_pos *newpos, pos;
1469 Lisp_Object string;
1470 {
1471 xassert (STRINGP (string));
1472 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1473
1474 if (STRING_MULTIBYTE (string))
1475 *newpos = string_pos_nchars_ahead (pos, string,
1476 CHARPOS (*newpos) - CHARPOS (pos));
1477 else
1478 BYTEPOS (*newpos) = CHARPOS (*newpos);
1479 }
1480
1481 /* EXPORT:
1482 Return an estimation of the pixel height of mode or top lines on
1483 frame F. FACE_ID specifies what line's height to estimate. */
1484
1485 int
1486 estimate_mode_line_height (f, face_id)
1487 struct frame *f;
1488 enum face_id face_id;
1489 {
1490 #ifdef HAVE_WINDOW_SYSTEM
1491 if (FRAME_WINDOW_P (f))
1492 {
1493 int height = FONT_HEIGHT (FRAME_FONT (f));
1494
1495 /* This function is called so early when Emacs starts that the face
1496 cache and mode line face are not yet initialized. */
1497 if (FRAME_FACE_CACHE (f))
1498 {
1499 struct face *face = FACE_FROM_ID (f, face_id);
1500 if (face)
1501 {
1502 if (face->font)
1503 height = FONT_HEIGHT (face->font);
1504 if (face->box_line_width > 0)
1505 height += 2 * face->box_line_width;
1506 }
1507 }
1508
1509 return height;
1510 }
1511 #endif
1512
1513 return 1;
1514 }
1515
1516 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1517 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1518 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1519 not force the value into range. */
1520
1521 void
1522 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1523 FRAME_PTR f;
1524 register int pix_x, pix_y;
1525 int *x, *y;
1526 NativeRectangle *bounds;
1527 int noclip;
1528 {
1529
1530 #ifdef HAVE_WINDOW_SYSTEM
1531 if (FRAME_WINDOW_P (f))
1532 {
1533 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1534 even for negative values. */
1535 if (pix_x < 0)
1536 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1537 if (pix_y < 0)
1538 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1539
1540 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1541 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1542
1543 if (bounds)
1544 STORE_NATIVE_RECT (*bounds,
1545 FRAME_COL_TO_PIXEL_X (f, pix_x),
1546 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1547 FRAME_COLUMN_WIDTH (f) - 1,
1548 FRAME_LINE_HEIGHT (f) - 1);
1549
1550 if (!noclip)
1551 {
1552 if (pix_x < 0)
1553 pix_x = 0;
1554 else if (pix_x > FRAME_TOTAL_COLS (f))
1555 pix_x = FRAME_TOTAL_COLS (f);
1556
1557 if (pix_y < 0)
1558 pix_y = 0;
1559 else if (pix_y > FRAME_LINES (f))
1560 pix_y = FRAME_LINES (f);
1561 }
1562 }
1563 #endif
1564
1565 *x = pix_x;
1566 *y = pix_y;
1567 }
1568
1569
1570 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1571 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1572 can't tell the positions because W's display is not up to date,
1573 return 0. */
1574
1575 int
1576 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1577 struct window *w;
1578 int hpos, vpos;
1579 int *frame_x, *frame_y;
1580 {
1581 #ifdef HAVE_WINDOW_SYSTEM
1582 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1583 {
1584 int success_p;
1585
1586 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1587 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1588
1589 if (display_completed)
1590 {
1591 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1592 struct glyph *glyph = row->glyphs[TEXT_AREA];
1593 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1594
1595 hpos = row->x;
1596 vpos = row->y;
1597 while (glyph < end)
1598 {
1599 hpos += glyph->pixel_width;
1600 ++glyph;
1601 }
1602
1603 /* If first glyph is partially visible, its first visible position is still 0. */
1604 if (hpos < 0)
1605 hpos = 0;
1606
1607 success_p = 1;
1608 }
1609 else
1610 {
1611 hpos = vpos = 0;
1612 success_p = 0;
1613 }
1614
1615 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1616 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1617 return success_p;
1618 }
1619 #endif
1620
1621 *frame_x = hpos;
1622 *frame_y = vpos;
1623 return 1;
1624 }
1625
1626
1627 #ifdef HAVE_WINDOW_SYSTEM
1628
1629 /* Find the glyph under window-relative coordinates X/Y in window W.
1630 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1631 strings. Return in *HPOS and *VPOS the row and column number of
1632 the glyph found. Return in *AREA the glyph area containing X.
1633 Value is a pointer to the glyph found or null if X/Y is not on
1634 text, or we can't tell because W's current matrix is not up to
1635 date. */
1636
1637 static struct glyph *
1638 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1639 struct window *w;
1640 int x, y;
1641 int *hpos, *vpos, *dx, *dy, *area;
1642 {
1643 struct glyph *glyph, *end;
1644 struct glyph_row *row = NULL;
1645 int x0, i;
1646
1647 /* Find row containing Y. Give up if some row is not enabled. */
1648 for (i = 0; i < w->current_matrix->nrows; ++i)
1649 {
1650 row = MATRIX_ROW (w->current_matrix, i);
1651 if (!row->enabled_p)
1652 return NULL;
1653 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1654 break;
1655 }
1656
1657 *vpos = i;
1658 *hpos = 0;
1659
1660 /* Give up if Y is not in the window. */
1661 if (i == w->current_matrix->nrows)
1662 return NULL;
1663
1664 /* Get the glyph area containing X. */
1665 if (w->pseudo_window_p)
1666 {
1667 *area = TEXT_AREA;
1668 x0 = 0;
1669 }
1670 else
1671 {
1672 if (x < window_box_left_offset (w, TEXT_AREA))
1673 {
1674 *area = LEFT_MARGIN_AREA;
1675 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1676 }
1677 else if (x < window_box_right_offset (w, TEXT_AREA))
1678 {
1679 *area = TEXT_AREA;
1680 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1681 }
1682 else
1683 {
1684 *area = RIGHT_MARGIN_AREA;
1685 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1686 }
1687 }
1688
1689 /* Find glyph containing X. */
1690 glyph = row->glyphs[*area];
1691 end = glyph + row->used[*area];
1692 x -= x0;
1693 while (glyph < end && x >= glyph->pixel_width)
1694 {
1695 x -= glyph->pixel_width;
1696 ++glyph;
1697 }
1698
1699 if (glyph == end)
1700 return NULL;
1701
1702 if (dx)
1703 {
1704 *dx = x;
1705 *dy = y - (row->y + row->ascent - glyph->ascent);
1706 }
1707
1708 *hpos = glyph - row->glyphs[*area];
1709 return glyph;
1710 }
1711
1712
1713 /* EXPORT:
1714 Convert frame-relative x/y to coordinates relative to window W.
1715 Takes pseudo-windows into account. */
1716
1717 void
1718 frame_to_window_pixel_xy (w, x, y)
1719 struct window *w;
1720 int *x, *y;
1721 {
1722 if (w->pseudo_window_p)
1723 {
1724 /* A pseudo-window is always full-width, and starts at the
1725 left edge of the frame, plus a frame border. */
1726 struct frame *f = XFRAME (w->frame);
1727 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1728 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1729 }
1730 else
1731 {
1732 *x -= WINDOW_LEFT_EDGE_X (w);
1733 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1734 }
1735 }
1736
1737 /* EXPORT:
1738 Return in *R the clipping rectangle for glyph string S. */
1739
1740 void
1741 get_glyph_string_clip_rect (s, nr)
1742 struct glyph_string *s;
1743 NativeRectangle *nr;
1744 {
1745 XRectangle r;
1746
1747 if (s->row->full_width_p)
1748 {
1749 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1750 r.x = WINDOW_LEFT_EDGE_X (s->w);
1751 r.width = WINDOW_TOTAL_WIDTH (s->w);
1752
1753 /* Unless displaying a mode or menu bar line, which are always
1754 fully visible, clip to the visible part of the row. */
1755 if (s->w->pseudo_window_p)
1756 r.height = s->row->visible_height;
1757 else
1758 r.height = s->height;
1759 }
1760 else
1761 {
1762 /* This is a text line that may be partially visible. */
1763 r.x = window_box_left (s->w, s->area);
1764 r.width = window_box_width (s->w, s->area);
1765 r.height = s->row->visible_height;
1766 }
1767
1768 /* If S draws overlapping rows, it's sufficient to use the top and
1769 bottom of the window for clipping because this glyph string
1770 intentionally draws over other lines. */
1771 if (s->for_overlaps_p)
1772 {
1773 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1774 r.height = window_text_bottom_y (s->w) - r.y;
1775 }
1776 else
1777 {
1778 /* Don't use S->y for clipping because it doesn't take partially
1779 visible lines into account. For example, it can be negative for
1780 partially visible lines at the top of a window. */
1781 if (!s->row->full_width_p
1782 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1783 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1784 else
1785 r.y = max (0, s->row->y);
1786
1787 /* If drawing a tool-bar window, draw it over the internal border
1788 at the top of the window. */
1789 if (s->w == XWINDOW (s->f->tool_bar_window))
1790 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1791 }
1792
1793 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1794
1795 /* If drawing the cursor, don't let glyph draw outside its
1796 advertised boundaries. Cleartype does this under some circumstances. */
1797 if (s->hl == DRAW_CURSOR)
1798 {
1799 struct glyph *glyph = s->first_glyph;
1800 int height;
1801
1802 if (s->x > r.x)
1803 {
1804 r.width -= s->x - r.x;
1805 r.x = s->x;
1806 }
1807 r.width = min (r.width, glyph->pixel_width);
1808
1809 /* Don't draw cursor glyph taller than our actual glyph. */
1810 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1811 if (height < r.height)
1812 {
1813 int max_y = r.y + r.height;
1814 r.y = min (max_y, s->ybase + glyph->descent - height);
1815 r.height = min (max_y - r.y, height);
1816 }
1817 }
1818
1819 #ifdef CONVERT_FROM_XRECT
1820 CONVERT_FROM_XRECT (r, *nr);
1821 #else
1822 *nr = r;
1823 #endif
1824 }
1825
1826 #endif /* HAVE_WINDOW_SYSTEM */
1827
1828 \f
1829 /***********************************************************************
1830 Lisp form evaluation
1831 ***********************************************************************/
1832
1833 /* Error handler for safe_eval and safe_call. */
1834
1835 static Lisp_Object
1836 safe_eval_handler (arg)
1837 Lisp_Object arg;
1838 {
1839 add_to_log ("Error during redisplay: %s", arg, Qnil);
1840 return Qnil;
1841 }
1842
1843
1844 /* Evaluate SEXPR and return the result, or nil if something went
1845 wrong. Prevent redisplay during the evaluation. */
1846
1847 Lisp_Object
1848 safe_eval (sexpr)
1849 Lisp_Object sexpr;
1850 {
1851 Lisp_Object val;
1852
1853 if (inhibit_eval_during_redisplay)
1854 val = Qnil;
1855 else
1856 {
1857 int count = SPECPDL_INDEX ();
1858 struct gcpro gcpro1;
1859
1860 GCPRO1 (sexpr);
1861 specbind (Qinhibit_redisplay, Qt);
1862 /* Use Qt to ensure debugger does not run,
1863 so there is no possibility of wanting to redisplay. */
1864 val = internal_condition_case_1 (Feval, sexpr, Qt,
1865 safe_eval_handler);
1866 UNGCPRO;
1867 val = unbind_to (count, val);
1868 }
1869
1870 return val;
1871 }
1872
1873
1874 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1875 Return the result, or nil if something went wrong. Prevent
1876 redisplay during the evaluation. */
1877
1878 Lisp_Object
1879 safe_call (nargs, args)
1880 int nargs;
1881 Lisp_Object *args;
1882 {
1883 Lisp_Object val;
1884
1885 if (inhibit_eval_during_redisplay)
1886 val = Qnil;
1887 else
1888 {
1889 int count = SPECPDL_INDEX ();
1890 struct gcpro gcpro1;
1891
1892 GCPRO1 (args[0]);
1893 gcpro1.nvars = nargs;
1894 specbind (Qinhibit_redisplay, Qt);
1895 /* Use Qt to ensure debugger does not run,
1896 so there is no possibility of wanting to redisplay. */
1897 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1898 safe_eval_handler);
1899 UNGCPRO;
1900 val = unbind_to (count, val);
1901 }
1902
1903 return val;
1904 }
1905
1906
1907 /* Call function FN with one argument ARG.
1908 Return the result, or nil if something went wrong. */
1909
1910 Lisp_Object
1911 safe_call1 (fn, arg)
1912 Lisp_Object fn, arg;
1913 {
1914 Lisp_Object args[2];
1915 args[0] = fn;
1916 args[1] = arg;
1917 return safe_call (2, args);
1918 }
1919
1920
1921 \f
1922 /***********************************************************************
1923 Debugging
1924 ***********************************************************************/
1925
1926 #if 0
1927
1928 /* Define CHECK_IT to perform sanity checks on iterators.
1929 This is for debugging. It is too slow to do unconditionally. */
1930
1931 static void
1932 check_it (it)
1933 struct it *it;
1934 {
1935 if (it->method == next_element_from_string)
1936 {
1937 xassert (STRINGP (it->string));
1938 xassert (IT_STRING_CHARPOS (*it) >= 0);
1939 }
1940 else
1941 {
1942 xassert (IT_STRING_CHARPOS (*it) < 0);
1943 if (it->method == next_element_from_buffer)
1944 {
1945 /* Check that character and byte positions agree. */
1946 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1947 }
1948 }
1949
1950 if (it->dpvec)
1951 xassert (it->current.dpvec_index >= 0);
1952 else
1953 xassert (it->current.dpvec_index < 0);
1954 }
1955
1956 #define CHECK_IT(IT) check_it ((IT))
1957
1958 #else /* not 0 */
1959
1960 #define CHECK_IT(IT) (void) 0
1961
1962 #endif /* not 0 */
1963
1964
1965 #if GLYPH_DEBUG
1966
1967 /* Check that the window end of window W is what we expect it
1968 to be---the last row in the current matrix displaying text. */
1969
1970 static void
1971 check_window_end (w)
1972 struct window *w;
1973 {
1974 if (!MINI_WINDOW_P (w)
1975 && !NILP (w->window_end_valid))
1976 {
1977 struct glyph_row *row;
1978 xassert ((row = MATRIX_ROW (w->current_matrix,
1979 XFASTINT (w->window_end_vpos)),
1980 !row->enabled_p
1981 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1982 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1983 }
1984 }
1985
1986 #define CHECK_WINDOW_END(W) check_window_end ((W))
1987
1988 #else /* not GLYPH_DEBUG */
1989
1990 #define CHECK_WINDOW_END(W) (void) 0
1991
1992 #endif /* not GLYPH_DEBUG */
1993
1994
1995 \f
1996 /***********************************************************************
1997 Iterator initialization
1998 ***********************************************************************/
1999
2000 /* Initialize IT for displaying current_buffer in window W, starting
2001 at character position CHARPOS. CHARPOS < 0 means that no buffer
2002 position is specified which is useful when the iterator is assigned
2003 a position later. BYTEPOS is the byte position corresponding to
2004 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2005
2006 If ROW is not null, calls to produce_glyphs with IT as parameter
2007 will produce glyphs in that row.
2008
2009 BASE_FACE_ID is the id of a base face to use. It must be one of
2010 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2011 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2012 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2013
2014 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2015 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2016 will be initialized to use the corresponding mode line glyph row of
2017 the desired matrix of W. */
2018
2019 void
2020 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2021 struct it *it;
2022 struct window *w;
2023 int charpos, bytepos;
2024 struct glyph_row *row;
2025 enum face_id base_face_id;
2026 {
2027 int highlight_region_p;
2028
2029 /* Some precondition checks. */
2030 xassert (w != NULL && it != NULL);
2031 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2032 && charpos <= ZV));
2033
2034 /* If face attributes have been changed since the last redisplay,
2035 free realized faces now because they depend on face definitions
2036 that might have changed. Don't free faces while there might be
2037 desired matrices pending which reference these faces. */
2038 if (face_change_count && !inhibit_free_realized_faces)
2039 {
2040 face_change_count = 0;
2041 free_all_realized_faces (Qnil);
2042 }
2043
2044 /* Use one of the mode line rows of W's desired matrix if
2045 appropriate. */
2046 if (row == NULL)
2047 {
2048 if (base_face_id == MODE_LINE_FACE_ID
2049 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2050 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2051 else if (base_face_id == HEADER_LINE_FACE_ID)
2052 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2053 }
2054
2055 /* Clear IT. */
2056 bzero (it, sizeof *it);
2057 it->current.overlay_string_index = -1;
2058 it->current.dpvec_index = -1;
2059 it->base_face_id = base_face_id;
2060 it->string = Qnil;
2061 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2062
2063 /* The window in which we iterate over current_buffer: */
2064 XSETWINDOW (it->window, w);
2065 it->w = w;
2066 it->f = XFRAME (w->frame);
2067
2068 /* Extra space between lines (on window systems only). */
2069 if (base_face_id == DEFAULT_FACE_ID
2070 && FRAME_WINDOW_P (it->f))
2071 {
2072 if (NATNUMP (current_buffer->extra_line_spacing))
2073 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2074 else if (FLOATP (current_buffer->extra_line_spacing))
2075 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2076 * FRAME_LINE_HEIGHT (it->f));
2077 else if (it->f->extra_line_spacing > 0)
2078 it->extra_line_spacing = it->f->extra_line_spacing;
2079 }
2080
2081 /* If realized faces have been removed, e.g. because of face
2082 attribute changes of named faces, recompute them. When running
2083 in batch mode, the face cache of Vterminal_frame is null. If
2084 we happen to get called, make a dummy face cache. */
2085 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2086 init_frame_faces (it->f);
2087 if (FRAME_FACE_CACHE (it->f)->used == 0)
2088 recompute_basic_faces (it->f);
2089
2090 /* Current value of the `slice', `space-width', and 'height' properties. */
2091 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2092 it->space_width = Qnil;
2093 it->font_height = Qnil;
2094 it->override_ascent = -1;
2095
2096 /* Are control characters displayed as `^C'? */
2097 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2098
2099 /* -1 means everything between a CR and the following line end
2100 is invisible. >0 means lines indented more than this value are
2101 invisible. */
2102 it->selective = (INTEGERP (current_buffer->selective_display)
2103 ? XFASTINT (current_buffer->selective_display)
2104 : (!NILP (current_buffer->selective_display)
2105 ? -1 : 0));
2106 it->selective_display_ellipsis_p
2107 = !NILP (current_buffer->selective_display_ellipses);
2108
2109 /* Display table to use. */
2110 it->dp = window_display_table (w);
2111
2112 /* Are multibyte characters enabled in current_buffer? */
2113 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2114
2115 /* Non-zero if we should highlight the region. */
2116 highlight_region_p
2117 = (!NILP (Vtransient_mark_mode)
2118 && !NILP (current_buffer->mark_active)
2119 && XMARKER (current_buffer->mark)->buffer != 0);
2120
2121 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2122 start and end of a visible region in window IT->w. Set both to
2123 -1 to indicate no region. */
2124 if (highlight_region_p
2125 /* Maybe highlight only in selected window. */
2126 && (/* Either show region everywhere. */
2127 highlight_nonselected_windows
2128 /* Or show region in the selected window. */
2129 || w == XWINDOW (selected_window)
2130 /* Or show the region if we are in the mini-buffer and W is
2131 the window the mini-buffer refers to. */
2132 || (MINI_WINDOW_P (XWINDOW (selected_window))
2133 && WINDOWP (minibuf_selected_window)
2134 && w == XWINDOW (minibuf_selected_window))))
2135 {
2136 int charpos = marker_position (current_buffer->mark);
2137 it->region_beg_charpos = min (PT, charpos);
2138 it->region_end_charpos = max (PT, charpos);
2139 }
2140 else
2141 it->region_beg_charpos = it->region_end_charpos = -1;
2142
2143 /* Get the position at which the redisplay_end_trigger hook should
2144 be run, if it is to be run at all. */
2145 if (MARKERP (w->redisplay_end_trigger)
2146 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2147 it->redisplay_end_trigger_charpos
2148 = marker_position (w->redisplay_end_trigger);
2149 else if (INTEGERP (w->redisplay_end_trigger))
2150 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2151
2152 /* Correct bogus values of tab_width. */
2153 it->tab_width = XINT (current_buffer->tab_width);
2154 if (it->tab_width <= 0 || it->tab_width > 1000)
2155 it->tab_width = 8;
2156
2157 /* Are lines in the display truncated? */
2158 it->truncate_lines_p
2159 = (base_face_id != DEFAULT_FACE_ID
2160 || XINT (it->w->hscroll)
2161 || (truncate_partial_width_windows
2162 && !WINDOW_FULL_WIDTH_P (it->w))
2163 || !NILP (current_buffer->truncate_lines));
2164
2165 /* Get dimensions of truncation and continuation glyphs. These are
2166 displayed as fringe bitmaps under X, so we don't need them for such
2167 frames. */
2168 if (!FRAME_WINDOW_P (it->f))
2169 {
2170 if (it->truncate_lines_p)
2171 {
2172 /* We will need the truncation glyph. */
2173 xassert (it->glyph_row == NULL);
2174 produce_special_glyphs (it, IT_TRUNCATION);
2175 it->truncation_pixel_width = it->pixel_width;
2176 }
2177 else
2178 {
2179 /* We will need the continuation glyph. */
2180 xassert (it->glyph_row == NULL);
2181 produce_special_glyphs (it, IT_CONTINUATION);
2182 it->continuation_pixel_width = it->pixel_width;
2183 }
2184
2185 /* Reset these values to zero because the produce_special_glyphs
2186 above has changed them. */
2187 it->pixel_width = it->ascent = it->descent = 0;
2188 it->phys_ascent = it->phys_descent = 0;
2189 }
2190
2191 /* Set this after getting the dimensions of truncation and
2192 continuation glyphs, so that we don't produce glyphs when calling
2193 produce_special_glyphs, above. */
2194 it->glyph_row = row;
2195 it->area = TEXT_AREA;
2196
2197 /* Get the dimensions of the display area. The display area
2198 consists of the visible window area plus a horizontally scrolled
2199 part to the left of the window. All x-values are relative to the
2200 start of this total display area. */
2201 if (base_face_id != DEFAULT_FACE_ID)
2202 {
2203 /* Mode lines, menu bar in terminal frames. */
2204 it->first_visible_x = 0;
2205 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2206 }
2207 else
2208 {
2209 it->first_visible_x
2210 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2211 it->last_visible_x = (it->first_visible_x
2212 + window_box_width (w, TEXT_AREA));
2213
2214 /* If we truncate lines, leave room for the truncator glyph(s) at
2215 the right margin. Otherwise, leave room for the continuation
2216 glyph(s). Truncation and continuation glyphs are not inserted
2217 for window-based redisplay. */
2218 if (!FRAME_WINDOW_P (it->f))
2219 {
2220 if (it->truncate_lines_p)
2221 it->last_visible_x -= it->truncation_pixel_width;
2222 else
2223 it->last_visible_x -= it->continuation_pixel_width;
2224 }
2225
2226 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2227 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2228 }
2229
2230 /* Leave room for a border glyph. */
2231 if (!FRAME_WINDOW_P (it->f)
2232 && !WINDOW_RIGHTMOST_P (it->w))
2233 it->last_visible_x -= 1;
2234
2235 it->last_visible_y = window_text_bottom_y (w);
2236
2237 /* For mode lines and alike, arrange for the first glyph having a
2238 left box line if the face specifies a box. */
2239 if (base_face_id != DEFAULT_FACE_ID)
2240 {
2241 struct face *face;
2242
2243 it->face_id = base_face_id;
2244
2245 /* If we have a boxed mode line, make the first character appear
2246 with a left box line. */
2247 face = FACE_FROM_ID (it->f, base_face_id);
2248 if (face->box != FACE_NO_BOX)
2249 it->start_of_box_run_p = 1;
2250 }
2251
2252 /* If a buffer position was specified, set the iterator there,
2253 getting overlays and face properties from that position. */
2254 if (charpos >= BUF_BEG (current_buffer))
2255 {
2256 it->end_charpos = ZV;
2257 it->face_id = -1;
2258 IT_CHARPOS (*it) = charpos;
2259
2260 /* Compute byte position if not specified. */
2261 if (bytepos < charpos)
2262 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2263 else
2264 IT_BYTEPOS (*it) = bytepos;
2265
2266 it->start = it->current;
2267
2268 /* Compute faces etc. */
2269 reseat (it, it->current.pos, 1);
2270 }
2271
2272 CHECK_IT (it);
2273 }
2274
2275
2276 /* Initialize IT for the display of window W with window start POS. */
2277
2278 void
2279 start_display (it, w, pos)
2280 struct it *it;
2281 struct window *w;
2282 struct text_pos pos;
2283 {
2284 struct glyph_row *row;
2285 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2286
2287 row = w->desired_matrix->rows + first_vpos;
2288 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2289 it->first_vpos = first_vpos;
2290
2291 if (!it->truncate_lines_p)
2292 {
2293 int start_at_line_beg_p;
2294 int first_y = it->current_y;
2295
2296 /* If window start is not at a line start, skip forward to POS to
2297 get the correct continuation lines width. */
2298 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2299 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2300 if (!start_at_line_beg_p)
2301 {
2302 int new_x;
2303
2304 reseat_at_previous_visible_line_start (it);
2305 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2306
2307 new_x = it->current_x + it->pixel_width;
2308
2309 /* If lines are continued, this line may end in the middle
2310 of a multi-glyph character (e.g. a control character
2311 displayed as \003, or in the middle of an overlay
2312 string). In this case move_it_to above will not have
2313 taken us to the start of the continuation line but to the
2314 end of the continued line. */
2315 if (it->current_x > 0
2316 && !it->truncate_lines_p /* Lines are continued. */
2317 && (/* And glyph doesn't fit on the line. */
2318 new_x > it->last_visible_x
2319 /* Or it fits exactly and we're on a window
2320 system frame. */
2321 || (new_x == it->last_visible_x
2322 && FRAME_WINDOW_P (it->f))))
2323 {
2324 if (it->current.dpvec_index >= 0
2325 || it->current.overlay_string_index >= 0)
2326 {
2327 set_iterator_to_next (it, 1);
2328 move_it_in_display_line_to (it, -1, -1, 0);
2329 }
2330
2331 it->continuation_lines_width += it->current_x;
2332 }
2333
2334 /* We're starting a new display line, not affected by the
2335 height of the continued line, so clear the appropriate
2336 fields in the iterator structure. */
2337 it->max_ascent = it->max_descent = 0;
2338 it->max_phys_ascent = it->max_phys_descent = 0;
2339
2340 it->current_y = first_y;
2341 it->vpos = 0;
2342 it->current_x = it->hpos = 0;
2343 }
2344 }
2345
2346 #if 0 /* Don't assert the following because start_display is sometimes
2347 called intentionally with a window start that is not at a
2348 line start. Please leave this code in as a comment. */
2349
2350 /* Window start should be on a line start, now. */
2351 xassert (it->continuation_lines_width
2352 || IT_CHARPOS (it) == BEGV
2353 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2354 #endif /* 0 */
2355 }
2356
2357
2358 /* Return 1 if POS is a position in ellipses displayed for invisible
2359 text. W is the window we display, for text property lookup. */
2360
2361 static int
2362 in_ellipses_for_invisible_text_p (pos, w)
2363 struct display_pos *pos;
2364 struct window *w;
2365 {
2366 Lisp_Object prop, window;
2367 int ellipses_p = 0;
2368 int charpos = CHARPOS (pos->pos);
2369
2370 /* If POS specifies a position in a display vector, this might
2371 be for an ellipsis displayed for invisible text. We won't
2372 get the iterator set up for delivering that ellipsis unless
2373 we make sure that it gets aware of the invisible text. */
2374 if (pos->dpvec_index >= 0
2375 && pos->overlay_string_index < 0
2376 && CHARPOS (pos->string_pos) < 0
2377 && charpos > BEGV
2378 && (XSETWINDOW (window, w),
2379 prop = Fget_char_property (make_number (charpos),
2380 Qinvisible, window),
2381 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2382 {
2383 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2384 window);
2385 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2386 }
2387
2388 return ellipses_p;
2389 }
2390
2391
2392 /* Initialize IT for stepping through current_buffer in window W,
2393 starting at position POS that includes overlay string and display
2394 vector/ control character translation position information. Value
2395 is zero if there are overlay strings with newlines at POS. */
2396
2397 static int
2398 init_from_display_pos (it, w, pos)
2399 struct it *it;
2400 struct window *w;
2401 struct display_pos *pos;
2402 {
2403 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2404 int i, overlay_strings_with_newlines = 0;
2405
2406 /* If POS specifies a position in a display vector, this might
2407 be for an ellipsis displayed for invisible text. We won't
2408 get the iterator set up for delivering that ellipsis unless
2409 we make sure that it gets aware of the invisible text. */
2410 if (in_ellipses_for_invisible_text_p (pos, w))
2411 {
2412 --charpos;
2413 bytepos = 0;
2414 }
2415
2416 /* Keep in mind: the call to reseat in init_iterator skips invisible
2417 text, so we might end up at a position different from POS. This
2418 is only a problem when POS is a row start after a newline and an
2419 overlay starts there with an after-string, and the overlay has an
2420 invisible property. Since we don't skip invisible text in
2421 display_line and elsewhere immediately after consuming the
2422 newline before the row start, such a POS will not be in a string,
2423 but the call to init_iterator below will move us to the
2424 after-string. */
2425 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2426
2427 for (i = 0; i < it->n_overlay_strings; ++i)
2428 {
2429 const char *s = SDATA (it->overlay_strings[i]);
2430 const char *e = s + SBYTES (it->overlay_strings[i]);
2431
2432 while (s < e && *s != '\n')
2433 ++s;
2434
2435 if (s < e)
2436 {
2437 overlay_strings_with_newlines = 1;
2438 break;
2439 }
2440 }
2441
2442 /* If position is within an overlay string, set up IT to the right
2443 overlay string. */
2444 if (pos->overlay_string_index >= 0)
2445 {
2446 int relative_index;
2447
2448 /* If the first overlay string happens to have a `display'
2449 property for an image, the iterator will be set up for that
2450 image, and we have to undo that setup first before we can
2451 correct the overlay string index. */
2452 if (it->method == next_element_from_image)
2453 pop_it (it);
2454
2455 /* We already have the first chunk of overlay strings in
2456 IT->overlay_strings. Load more until the one for
2457 pos->overlay_string_index is in IT->overlay_strings. */
2458 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2459 {
2460 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2461 it->current.overlay_string_index = 0;
2462 while (n--)
2463 {
2464 load_overlay_strings (it, 0);
2465 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2466 }
2467 }
2468
2469 it->current.overlay_string_index = pos->overlay_string_index;
2470 relative_index = (it->current.overlay_string_index
2471 % OVERLAY_STRING_CHUNK_SIZE);
2472 it->string = it->overlay_strings[relative_index];
2473 xassert (STRINGP (it->string));
2474 it->current.string_pos = pos->string_pos;
2475 it->method = next_element_from_string;
2476 }
2477
2478 #if 0 /* This is bogus because POS not having an overlay string
2479 position does not mean it's after the string. Example: A
2480 line starting with a before-string and initialization of IT
2481 to the previous row's end position. */
2482 else if (it->current.overlay_string_index >= 0)
2483 {
2484 /* If POS says we're already after an overlay string ending at
2485 POS, make sure to pop the iterator because it will be in
2486 front of that overlay string. When POS is ZV, we've thereby
2487 also ``processed'' overlay strings at ZV. */
2488 while (it->sp)
2489 pop_it (it);
2490 it->current.overlay_string_index = -1;
2491 it->method = next_element_from_buffer;
2492 if (CHARPOS (pos->pos) == ZV)
2493 it->overlay_strings_at_end_processed_p = 1;
2494 }
2495 #endif /* 0 */
2496
2497 if (CHARPOS (pos->string_pos) >= 0)
2498 {
2499 /* Recorded position is not in an overlay string, but in another
2500 string. This can only be a string from a `display' property.
2501 IT should already be filled with that string. */
2502 it->current.string_pos = pos->string_pos;
2503 xassert (STRINGP (it->string));
2504 }
2505
2506 /* Restore position in display vector translations, control
2507 character translations or ellipses. */
2508 if (pos->dpvec_index >= 0)
2509 {
2510 if (it->dpvec == NULL)
2511 get_next_display_element (it);
2512 xassert (it->dpvec && it->current.dpvec_index == 0);
2513 it->current.dpvec_index = pos->dpvec_index;
2514 }
2515
2516 CHECK_IT (it);
2517 return !overlay_strings_with_newlines;
2518 }
2519
2520
2521 /* Initialize IT for stepping through current_buffer in window W
2522 starting at ROW->start. */
2523
2524 static void
2525 init_to_row_start (it, w, row)
2526 struct it *it;
2527 struct window *w;
2528 struct glyph_row *row;
2529 {
2530 init_from_display_pos (it, w, &row->start);
2531 it->start = row->start;
2532 it->continuation_lines_width = row->continuation_lines_width;
2533 CHECK_IT (it);
2534 }
2535
2536
2537 /* Initialize IT for stepping through current_buffer in window W
2538 starting in the line following ROW, i.e. starting at ROW->end.
2539 Value is zero if there are overlay strings with newlines at ROW's
2540 end position. */
2541
2542 static int
2543 init_to_row_end (it, w, row)
2544 struct it *it;
2545 struct window *w;
2546 struct glyph_row *row;
2547 {
2548 int success = 0;
2549
2550 if (init_from_display_pos (it, w, &row->end))
2551 {
2552 if (row->continued_p)
2553 it->continuation_lines_width
2554 = row->continuation_lines_width + row->pixel_width;
2555 CHECK_IT (it);
2556 success = 1;
2557 }
2558
2559 return success;
2560 }
2561
2562
2563
2564 \f
2565 /***********************************************************************
2566 Text properties
2567 ***********************************************************************/
2568
2569 /* Called when IT reaches IT->stop_charpos. Handle text property and
2570 overlay changes. Set IT->stop_charpos to the next position where
2571 to stop. */
2572
2573 static void
2574 handle_stop (it)
2575 struct it *it;
2576 {
2577 enum prop_handled handled;
2578 int handle_overlay_change_p = 1;
2579 struct props *p;
2580
2581 it->dpvec = NULL;
2582 it->current.dpvec_index = -1;
2583
2584 do
2585 {
2586 handled = HANDLED_NORMALLY;
2587
2588 /* Call text property handlers. */
2589 for (p = it_props; p->handler; ++p)
2590 {
2591 handled = p->handler (it);
2592
2593 if (handled == HANDLED_RECOMPUTE_PROPS)
2594 break;
2595 else if (handled == HANDLED_RETURN)
2596 return;
2597 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2598 handle_overlay_change_p = 0;
2599 }
2600
2601 if (handled != HANDLED_RECOMPUTE_PROPS)
2602 {
2603 /* Don't check for overlay strings below when set to deliver
2604 characters from a display vector. */
2605 if (it->method == next_element_from_display_vector)
2606 handle_overlay_change_p = 0;
2607
2608 /* Handle overlay changes. */
2609 if (handle_overlay_change_p)
2610 handled = handle_overlay_change (it);
2611
2612 /* Determine where to stop next. */
2613 if (handled == HANDLED_NORMALLY)
2614 compute_stop_pos (it);
2615 }
2616 }
2617 while (handled == HANDLED_RECOMPUTE_PROPS);
2618 }
2619
2620
2621 /* Compute IT->stop_charpos from text property and overlay change
2622 information for IT's current position. */
2623
2624 static void
2625 compute_stop_pos (it)
2626 struct it *it;
2627 {
2628 register INTERVAL iv, next_iv;
2629 Lisp_Object object, limit, position;
2630
2631 /* If nowhere else, stop at the end. */
2632 it->stop_charpos = it->end_charpos;
2633
2634 if (STRINGP (it->string))
2635 {
2636 /* Strings are usually short, so don't limit the search for
2637 properties. */
2638 object = it->string;
2639 limit = Qnil;
2640 position = make_number (IT_STRING_CHARPOS (*it));
2641 }
2642 else
2643 {
2644 int charpos;
2645
2646 /* If next overlay change is in front of the current stop pos
2647 (which is IT->end_charpos), stop there. Note: value of
2648 next_overlay_change is point-max if no overlay change
2649 follows. */
2650 charpos = next_overlay_change (IT_CHARPOS (*it));
2651 if (charpos < it->stop_charpos)
2652 it->stop_charpos = charpos;
2653
2654 /* If showing the region, we have to stop at the region
2655 start or end because the face might change there. */
2656 if (it->region_beg_charpos > 0)
2657 {
2658 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2659 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2660 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2661 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2662 }
2663
2664 /* Set up variables for computing the stop position from text
2665 property changes. */
2666 XSETBUFFER (object, current_buffer);
2667 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2668 position = make_number (IT_CHARPOS (*it));
2669
2670 }
2671
2672 /* Get the interval containing IT's position. Value is a null
2673 interval if there isn't such an interval. */
2674 iv = validate_interval_range (object, &position, &position, 0);
2675 if (!NULL_INTERVAL_P (iv))
2676 {
2677 Lisp_Object values_here[LAST_PROP_IDX];
2678 struct props *p;
2679
2680 /* Get properties here. */
2681 for (p = it_props; p->handler; ++p)
2682 values_here[p->idx] = textget (iv->plist, *p->name);
2683
2684 /* Look for an interval following iv that has different
2685 properties. */
2686 for (next_iv = next_interval (iv);
2687 (!NULL_INTERVAL_P (next_iv)
2688 && (NILP (limit)
2689 || XFASTINT (limit) > next_iv->position));
2690 next_iv = next_interval (next_iv))
2691 {
2692 for (p = it_props; p->handler; ++p)
2693 {
2694 Lisp_Object new_value;
2695
2696 new_value = textget (next_iv->plist, *p->name);
2697 if (!EQ (values_here[p->idx], new_value))
2698 break;
2699 }
2700
2701 if (p->handler)
2702 break;
2703 }
2704
2705 if (!NULL_INTERVAL_P (next_iv))
2706 {
2707 if (INTEGERP (limit)
2708 && next_iv->position >= XFASTINT (limit))
2709 /* No text property change up to limit. */
2710 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2711 else
2712 /* Text properties change in next_iv. */
2713 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2714 }
2715 }
2716
2717 xassert (STRINGP (it->string)
2718 || (it->stop_charpos >= BEGV
2719 && it->stop_charpos >= IT_CHARPOS (*it)));
2720 }
2721
2722
2723 /* Return the position of the next overlay change after POS in
2724 current_buffer. Value is point-max if no overlay change
2725 follows. This is like `next-overlay-change' but doesn't use
2726 xmalloc. */
2727
2728 static int
2729 next_overlay_change (pos)
2730 int pos;
2731 {
2732 int noverlays;
2733 int endpos;
2734 Lisp_Object *overlays;
2735 int i;
2736
2737 /* Get all overlays at the given position. */
2738 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
2739
2740 /* If any of these overlays ends before endpos,
2741 use its ending point instead. */
2742 for (i = 0; i < noverlays; ++i)
2743 {
2744 Lisp_Object oend;
2745 int oendpos;
2746
2747 oend = OVERLAY_END (overlays[i]);
2748 oendpos = OVERLAY_POSITION (oend);
2749 endpos = min (endpos, oendpos);
2750 }
2751
2752 return endpos;
2753 }
2754
2755
2756 \f
2757 /***********************************************************************
2758 Fontification
2759 ***********************************************************************/
2760
2761 /* Handle changes in the `fontified' property of the current buffer by
2762 calling hook functions from Qfontification_functions to fontify
2763 regions of text. */
2764
2765 static enum prop_handled
2766 handle_fontified_prop (it)
2767 struct it *it;
2768 {
2769 Lisp_Object prop, pos;
2770 enum prop_handled handled = HANDLED_NORMALLY;
2771
2772 /* Get the value of the `fontified' property at IT's current buffer
2773 position. (The `fontified' property doesn't have a special
2774 meaning in strings.) If the value is nil, call functions from
2775 Qfontification_functions. */
2776 if (!STRINGP (it->string)
2777 && it->s == NULL
2778 && !NILP (Vfontification_functions)
2779 && !NILP (Vrun_hooks)
2780 && (pos = make_number (IT_CHARPOS (*it)),
2781 prop = Fget_char_property (pos, Qfontified, Qnil),
2782 NILP (prop)))
2783 {
2784 int count = SPECPDL_INDEX ();
2785 Lisp_Object val;
2786
2787 val = Vfontification_functions;
2788 specbind (Qfontification_functions, Qnil);
2789
2790 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2791 safe_call1 (val, pos);
2792 else
2793 {
2794 Lisp_Object globals, fn;
2795 struct gcpro gcpro1, gcpro2;
2796
2797 globals = Qnil;
2798 GCPRO2 (val, globals);
2799
2800 for (; CONSP (val); val = XCDR (val))
2801 {
2802 fn = XCAR (val);
2803
2804 if (EQ (fn, Qt))
2805 {
2806 /* A value of t indicates this hook has a local
2807 binding; it means to run the global binding too.
2808 In a global value, t should not occur. If it
2809 does, we must ignore it to avoid an endless
2810 loop. */
2811 for (globals = Fdefault_value (Qfontification_functions);
2812 CONSP (globals);
2813 globals = XCDR (globals))
2814 {
2815 fn = XCAR (globals);
2816 if (!EQ (fn, Qt))
2817 safe_call1 (fn, pos);
2818 }
2819 }
2820 else
2821 safe_call1 (fn, pos);
2822 }
2823
2824 UNGCPRO;
2825 }
2826
2827 unbind_to (count, Qnil);
2828
2829 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2830 something. This avoids an endless loop if they failed to
2831 fontify the text for which reason ever. */
2832 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2833 handled = HANDLED_RECOMPUTE_PROPS;
2834 }
2835
2836 return handled;
2837 }
2838
2839
2840 \f
2841 /***********************************************************************
2842 Faces
2843 ***********************************************************************/
2844
2845 /* Set up iterator IT from face properties at its current position.
2846 Called from handle_stop. */
2847
2848 static enum prop_handled
2849 handle_face_prop (it)
2850 struct it *it;
2851 {
2852 int new_face_id, next_stop;
2853
2854 if (!STRINGP (it->string))
2855 {
2856 new_face_id
2857 = face_at_buffer_position (it->w,
2858 IT_CHARPOS (*it),
2859 it->region_beg_charpos,
2860 it->region_end_charpos,
2861 &next_stop,
2862 (IT_CHARPOS (*it)
2863 + TEXT_PROP_DISTANCE_LIMIT),
2864 0);
2865
2866 /* Is this a start of a run of characters with box face?
2867 Caveat: this can be called for a freshly initialized
2868 iterator; face_id is -1 in this case. We know that the new
2869 face will not change until limit, i.e. if the new face has a
2870 box, all characters up to limit will have one. But, as
2871 usual, we don't know whether limit is really the end. */
2872 if (new_face_id != it->face_id)
2873 {
2874 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2875
2876 /* If new face has a box but old face has not, this is
2877 the start of a run of characters with box, i.e. it has
2878 a shadow on the left side. The value of face_id of the
2879 iterator will be -1 if this is the initial call that gets
2880 the face. In this case, we have to look in front of IT's
2881 position and see whether there is a face != new_face_id. */
2882 it->start_of_box_run_p
2883 = (new_face->box != FACE_NO_BOX
2884 && (it->face_id >= 0
2885 || IT_CHARPOS (*it) == BEG
2886 || new_face_id != face_before_it_pos (it)));
2887 it->face_box_p = new_face->box != FACE_NO_BOX;
2888 }
2889 }
2890 else
2891 {
2892 int base_face_id, bufpos;
2893
2894 if (it->current.overlay_string_index >= 0)
2895 bufpos = IT_CHARPOS (*it);
2896 else
2897 bufpos = 0;
2898
2899 /* For strings from a buffer, i.e. overlay strings or strings
2900 from a `display' property, use the face at IT's current
2901 buffer position as the base face to merge with, so that
2902 overlay strings appear in the same face as surrounding
2903 text, unless they specify their own faces. */
2904 base_face_id = underlying_face_id (it);
2905
2906 new_face_id = face_at_string_position (it->w,
2907 it->string,
2908 IT_STRING_CHARPOS (*it),
2909 bufpos,
2910 it->region_beg_charpos,
2911 it->region_end_charpos,
2912 &next_stop,
2913 base_face_id, 0);
2914
2915 #if 0 /* This shouldn't be neccessary. Let's check it. */
2916 /* If IT is used to display a mode line we would really like to
2917 use the mode line face instead of the frame's default face. */
2918 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2919 && new_face_id == DEFAULT_FACE_ID)
2920 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2921 #endif
2922
2923 /* Is this a start of a run of characters with box? Caveat:
2924 this can be called for a freshly allocated iterator; face_id
2925 is -1 is this case. We know that the new face will not
2926 change until the next check pos, i.e. if the new face has a
2927 box, all characters up to that position will have a
2928 box. But, as usual, we don't know whether that position
2929 is really the end. */
2930 if (new_face_id != it->face_id)
2931 {
2932 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2933 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2934
2935 /* If new face has a box but old face hasn't, this is the
2936 start of a run of characters with box, i.e. it has a
2937 shadow on the left side. */
2938 it->start_of_box_run_p
2939 = new_face->box && (old_face == NULL || !old_face->box);
2940 it->face_box_p = new_face->box != FACE_NO_BOX;
2941 }
2942 }
2943
2944 it->face_id = new_face_id;
2945 return HANDLED_NORMALLY;
2946 }
2947
2948
2949 /* Return the ID of the face ``underlying'' IT's current position,
2950 which is in a string. If the iterator is associated with a
2951 buffer, return the face at IT's current buffer position.
2952 Otherwise, use the iterator's base_face_id. */
2953
2954 static int
2955 underlying_face_id (it)
2956 struct it *it;
2957 {
2958 int face_id = it->base_face_id, i;
2959
2960 xassert (STRINGP (it->string));
2961
2962 for (i = it->sp - 1; i >= 0; --i)
2963 if (NILP (it->stack[i].string))
2964 face_id = it->stack[i].face_id;
2965
2966 return face_id;
2967 }
2968
2969
2970 /* Compute the face one character before or after the current position
2971 of IT. BEFORE_P non-zero means get the face in front of IT's
2972 position. Value is the id of the face. */
2973
2974 static int
2975 face_before_or_after_it_pos (it, before_p)
2976 struct it *it;
2977 int before_p;
2978 {
2979 int face_id, limit;
2980 int next_check_charpos;
2981 struct text_pos pos;
2982
2983 xassert (it->s == NULL);
2984
2985 if (STRINGP (it->string))
2986 {
2987 int bufpos, base_face_id;
2988
2989 /* No face change past the end of the string (for the case
2990 we are padding with spaces). No face change before the
2991 string start. */
2992 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
2993 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2994 return it->face_id;
2995
2996 /* Set pos to the position before or after IT's current position. */
2997 if (before_p)
2998 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2999 else
3000 /* For composition, we must check the character after the
3001 composition. */
3002 pos = (it->what == IT_COMPOSITION
3003 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3004 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3005
3006 if (it->current.overlay_string_index >= 0)
3007 bufpos = IT_CHARPOS (*it);
3008 else
3009 bufpos = 0;
3010
3011 base_face_id = underlying_face_id (it);
3012
3013 /* Get the face for ASCII, or unibyte. */
3014 face_id = face_at_string_position (it->w,
3015 it->string,
3016 CHARPOS (pos),
3017 bufpos,
3018 it->region_beg_charpos,
3019 it->region_end_charpos,
3020 &next_check_charpos,
3021 base_face_id, 0);
3022
3023 /* Correct the face for charsets different from ASCII. Do it
3024 for the multibyte case only. The face returned above is
3025 suitable for unibyte text if IT->string is unibyte. */
3026 if (STRING_MULTIBYTE (it->string))
3027 {
3028 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3029 int rest = SBYTES (it->string) - BYTEPOS (pos);
3030 int c, len;
3031 struct face *face = FACE_FROM_ID (it->f, face_id);
3032
3033 c = string_char_and_length (p, rest, &len);
3034 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3035 }
3036 }
3037 else
3038 {
3039 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3040 || (IT_CHARPOS (*it) <= BEGV && before_p))
3041 return it->face_id;
3042
3043 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3044 pos = it->current.pos;
3045
3046 if (before_p)
3047 DEC_TEXT_POS (pos, it->multibyte_p);
3048 else
3049 {
3050 if (it->what == IT_COMPOSITION)
3051 /* For composition, we must check the position after the
3052 composition. */
3053 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3054 else
3055 INC_TEXT_POS (pos, it->multibyte_p);
3056 }
3057
3058 /* Determine face for CHARSET_ASCII, or unibyte. */
3059 face_id = face_at_buffer_position (it->w,
3060 CHARPOS (pos),
3061 it->region_beg_charpos,
3062 it->region_end_charpos,
3063 &next_check_charpos,
3064 limit, 0);
3065
3066 /* Correct the face for charsets different from ASCII. Do it
3067 for the multibyte case only. The face returned above is
3068 suitable for unibyte text if current_buffer is unibyte. */
3069 if (it->multibyte_p)
3070 {
3071 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3072 struct face *face = FACE_FROM_ID (it->f, face_id);
3073 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3074 }
3075 }
3076
3077 return face_id;
3078 }
3079
3080
3081 \f
3082 /***********************************************************************
3083 Invisible text
3084 ***********************************************************************/
3085
3086 /* Set up iterator IT from invisible properties at its current
3087 position. Called from handle_stop. */
3088
3089 static enum prop_handled
3090 handle_invisible_prop (it)
3091 struct it *it;
3092 {
3093 enum prop_handled handled = HANDLED_NORMALLY;
3094
3095 if (STRINGP (it->string))
3096 {
3097 extern Lisp_Object Qinvisible;
3098 Lisp_Object prop, end_charpos, limit, charpos;
3099
3100 /* Get the value of the invisible text property at the
3101 current position. Value will be nil if there is no such
3102 property. */
3103 charpos = make_number (IT_STRING_CHARPOS (*it));
3104 prop = Fget_text_property (charpos, Qinvisible, it->string);
3105
3106 if (!NILP (prop)
3107 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3108 {
3109 handled = HANDLED_RECOMPUTE_PROPS;
3110
3111 /* Get the position at which the next change of the
3112 invisible text property can be found in IT->string.
3113 Value will be nil if the property value is the same for
3114 all the rest of IT->string. */
3115 XSETINT (limit, SCHARS (it->string));
3116 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3117 it->string, limit);
3118
3119 /* Text at current position is invisible. The next
3120 change in the property is at position end_charpos.
3121 Move IT's current position to that position. */
3122 if (INTEGERP (end_charpos)
3123 && XFASTINT (end_charpos) < XFASTINT (limit))
3124 {
3125 struct text_pos old;
3126 old = it->current.string_pos;
3127 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3128 compute_string_pos (&it->current.string_pos, old, it->string);
3129 }
3130 else
3131 {
3132 /* The rest of the string is invisible. If this is an
3133 overlay string, proceed with the next overlay string
3134 or whatever comes and return a character from there. */
3135 if (it->current.overlay_string_index >= 0)
3136 {
3137 next_overlay_string (it);
3138 /* Don't check for overlay strings when we just
3139 finished processing them. */
3140 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3141 }
3142 else
3143 {
3144 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3145 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3146 }
3147 }
3148 }
3149 }
3150 else
3151 {
3152 int invis_p, newpos, next_stop, start_charpos;
3153 Lisp_Object pos, prop, overlay;
3154
3155 /* First of all, is there invisible text at this position? */
3156 start_charpos = IT_CHARPOS (*it);
3157 pos = make_number (IT_CHARPOS (*it));
3158 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3159 &overlay);
3160 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3161
3162 /* If we are on invisible text, skip over it. */
3163 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3164 {
3165 /* Record whether we have to display an ellipsis for the
3166 invisible text. */
3167 int display_ellipsis_p = invis_p == 2;
3168
3169 handled = HANDLED_RECOMPUTE_PROPS;
3170
3171 /* Loop skipping over invisible text. The loop is left at
3172 ZV or with IT on the first char being visible again. */
3173 do
3174 {
3175 /* Try to skip some invisible text. Return value is the
3176 position reached which can be equal to IT's position
3177 if there is nothing invisible here. This skips both
3178 over invisible text properties and overlays with
3179 invisible property. */
3180 newpos = skip_invisible (IT_CHARPOS (*it),
3181 &next_stop, ZV, it->window);
3182
3183 /* If we skipped nothing at all we weren't at invisible
3184 text in the first place. If everything to the end of
3185 the buffer was skipped, end the loop. */
3186 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3187 invis_p = 0;
3188 else
3189 {
3190 /* We skipped some characters but not necessarily
3191 all there are. Check if we ended up on visible
3192 text. Fget_char_property returns the property of
3193 the char before the given position, i.e. if we
3194 get invis_p = 0, this means that the char at
3195 newpos is visible. */
3196 pos = make_number (newpos);
3197 prop = Fget_char_property (pos, Qinvisible, it->window);
3198 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3199 }
3200
3201 /* If we ended up on invisible text, proceed to
3202 skip starting with next_stop. */
3203 if (invis_p)
3204 IT_CHARPOS (*it) = next_stop;
3205 }
3206 while (invis_p);
3207
3208 /* The position newpos is now either ZV or on visible text. */
3209 IT_CHARPOS (*it) = newpos;
3210 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3211
3212 /* If there are before-strings at the start of invisible
3213 text, and the text is invisible because of a text
3214 property, arrange to show before-strings because 20.x did
3215 it that way. (If the text is invisible because of an
3216 overlay property instead of a text property, this is
3217 already handled in the overlay code.) */
3218 if (NILP (overlay)
3219 && get_overlay_strings (it, start_charpos))
3220 {
3221 handled = HANDLED_RECOMPUTE_PROPS;
3222 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3223 }
3224 else if (display_ellipsis_p)
3225 setup_for_ellipsis (it);
3226 }
3227 }
3228
3229 return handled;
3230 }
3231
3232
3233 /* Make iterator IT return `...' next. */
3234
3235 static void
3236 setup_for_ellipsis (it)
3237 struct it *it;
3238 {
3239 if (it->dp
3240 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3241 {
3242 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3243 it->dpvec = v->contents;
3244 it->dpend = v->contents + v->size;
3245 }
3246 else
3247 {
3248 /* Default `...'. */
3249 it->dpvec = default_invis_vector;
3250 it->dpend = default_invis_vector + 3;
3251 }
3252
3253 /* The ellipsis display does not replace the display of the
3254 character at the new position. Indicate this by setting
3255 IT->dpvec_char_len to zero. */
3256 it->dpvec_char_len = 0;
3257
3258 it->current.dpvec_index = 0;
3259 it->method = next_element_from_display_vector;
3260 }
3261
3262
3263 \f
3264 /***********************************************************************
3265 'display' property
3266 ***********************************************************************/
3267
3268 /* Set up iterator IT from `display' property at its current position.
3269 Called from handle_stop. */
3270
3271 static enum prop_handled
3272 handle_display_prop (it)
3273 struct it *it;
3274 {
3275 Lisp_Object prop, object;
3276 struct text_pos *position;
3277 int display_replaced_p = 0;
3278
3279 if (STRINGP (it->string))
3280 {
3281 object = it->string;
3282 position = &it->current.string_pos;
3283 }
3284 else
3285 {
3286 object = it->w->buffer;
3287 position = &it->current.pos;
3288 }
3289
3290 /* Reset those iterator values set from display property values. */
3291 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3292 it->space_width = Qnil;
3293 it->font_height = Qnil;
3294 it->voffset = 0;
3295
3296 /* We don't support recursive `display' properties, i.e. string
3297 values that have a string `display' property, that have a string
3298 `display' property etc. */
3299 if (!it->string_from_display_prop_p)
3300 it->area = TEXT_AREA;
3301
3302 prop = Fget_char_property (make_number (position->charpos),
3303 Qdisplay, object);
3304 if (NILP (prop))
3305 return HANDLED_NORMALLY;
3306
3307 if (CONSP (prop)
3308 /* Simple properties. */
3309 && !EQ (XCAR (prop), Qimage)
3310 && !EQ (XCAR (prop), Qspace)
3311 && !EQ (XCAR (prop), Qwhen)
3312 && !EQ (XCAR (prop), Qslice)
3313 && !EQ (XCAR (prop), Qspace_width)
3314 && !EQ (XCAR (prop), Qheight)
3315 && !EQ (XCAR (prop), Qraise)
3316 /* Marginal area specifications. */
3317 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3318 && !EQ (XCAR (prop), Qleft_fringe)
3319 && !EQ (XCAR (prop), Qright_fringe)
3320 && !NILP (XCAR (prop)))
3321 {
3322 for (; CONSP (prop); prop = XCDR (prop))
3323 {
3324 if (handle_single_display_prop (it, XCAR (prop), object,
3325 position, display_replaced_p))
3326 display_replaced_p = 1;
3327 }
3328 }
3329 else if (VECTORP (prop))
3330 {
3331 int i;
3332 for (i = 0; i < ASIZE (prop); ++i)
3333 if (handle_single_display_prop (it, AREF (prop, i), object,
3334 position, display_replaced_p))
3335 display_replaced_p = 1;
3336 }
3337 else
3338 {
3339 if (handle_single_display_prop (it, prop, object, position, 0))
3340 display_replaced_p = 1;
3341 }
3342
3343 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3344 }
3345
3346
3347 /* Value is the position of the end of the `display' property starting
3348 at START_POS in OBJECT. */
3349
3350 static struct text_pos
3351 display_prop_end (it, object, start_pos)
3352 struct it *it;
3353 Lisp_Object object;
3354 struct text_pos start_pos;
3355 {
3356 Lisp_Object end;
3357 struct text_pos end_pos;
3358
3359 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3360 Qdisplay, object, Qnil);
3361 CHARPOS (end_pos) = XFASTINT (end);
3362 if (STRINGP (object))
3363 compute_string_pos (&end_pos, start_pos, it->string);
3364 else
3365 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3366
3367 return end_pos;
3368 }
3369
3370
3371 /* Set up IT from a single `display' sub-property value PROP. OBJECT
3372 is the object in which the `display' property was found. *POSITION
3373 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3374 means that we previously saw a display sub-property which already
3375 replaced text display with something else, for example an image;
3376 ignore such properties after the first one has been processed.
3377
3378 If PROP is a `space' or `image' sub-property, set *POSITION to the
3379 end position of the `display' property.
3380
3381 Value is non-zero if something was found which replaces the display
3382 of buffer or string text. */
3383
3384 static int
3385 handle_single_display_prop (it, prop, object, position,
3386 display_replaced_before_p)
3387 struct it *it;
3388 Lisp_Object prop;
3389 Lisp_Object object;
3390 struct text_pos *position;
3391 int display_replaced_before_p;
3392 {
3393 Lisp_Object value;
3394 int replaces_text_display_p = 0;
3395 Lisp_Object form;
3396
3397 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
3398 evaluated. If the result is nil, VALUE is ignored. */
3399 form = Qt;
3400 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3401 {
3402 prop = XCDR (prop);
3403 if (!CONSP (prop))
3404 return 0;
3405 form = XCAR (prop);
3406 prop = XCDR (prop);
3407 }
3408
3409 if (!NILP (form) && !EQ (form, Qt))
3410 {
3411 int count = SPECPDL_INDEX ();
3412 struct gcpro gcpro1;
3413
3414 /* Bind `object' to the object having the `display' property, a
3415 buffer or string. Bind `position' to the position in the
3416 object where the property was found, and `buffer-position'
3417 to the current position in the buffer. */
3418 specbind (Qobject, object);
3419 specbind (Qposition, make_number (CHARPOS (*position)));
3420 specbind (Qbuffer_position,
3421 make_number (STRINGP (object)
3422 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3423 GCPRO1 (form);
3424 form = safe_eval (form);
3425 UNGCPRO;
3426 unbind_to (count, Qnil);
3427 }
3428
3429 if (NILP (form))
3430 return 0;
3431
3432 if (CONSP (prop)
3433 && EQ (XCAR (prop), Qheight)
3434 && CONSP (XCDR (prop)))
3435 {
3436 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3437 return 0;
3438
3439 /* `(height HEIGHT)'. */
3440 it->font_height = XCAR (XCDR (prop));
3441 if (!NILP (it->font_height))
3442 {
3443 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3444 int new_height = -1;
3445
3446 if (CONSP (it->font_height)
3447 && (EQ (XCAR (it->font_height), Qplus)
3448 || EQ (XCAR (it->font_height), Qminus))
3449 && CONSP (XCDR (it->font_height))
3450 && INTEGERP (XCAR (XCDR (it->font_height))))
3451 {
3452 /* `(+ N)' or `(- N)' where N is an integer. */
3453 int steps = XINT (XCAR (XCDR (it->font_height)));
3454 if (EQ (XCAR (it->font_height), Qplus))
3455 steps = - steps;
3456 it->face_id = smaller_face (it->f, it->face_id, steps);
3457 }
3458 else if (FUNCTIONP (it->font_height))
3459 {
3460 /* Call function with current height as argument.
3461 Value is the new height. */
3462 Lisp_Object height;
3463 height = safe_call1 (it->font_height,
3464 face->lface[LFACE_HEIGHT_INDEX]);
3465 if (NUMBERP (height))
3466 new_height = XFLOATINT (height);
3467 }
3468 else if (NUMBERP (it->font_height))
3469 {
3470 /* Value is a multiple of the canonical char height. */
3471 struct face *face;
3472
3473 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3474 new_height = (XFLOATINT (it->font_height)
3475 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3476 }
3477 else
3478 {
3479 /* Evaluate IT->font_height with `height' bound to the
3480 current specified height to get the new height. */
3481 Lisp_Object value;
3482 int count = SPECPDL_INDEX ();
3483
3484 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3485 value = safe_eval (it->font_height);
3486 unbind_to (count, Qnil);
3487
3488 if (NUMBERP (value))
3489 new_height = XFLOATINT (value);
3490 }
3491
3492 if (new_height > 0)
3493 it->face_id = face_with_height (it->f, it->face_id, new_height);
3494 }
3495 }
3496 else if (CONSP (prop)
3497 && EQ (XCAR (prop), Qspace_width)
3498 && CONSP (XCDR (prop)))
3499 {
3500 /* `(space_width WIDTH)'. */
3501 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3502 return 0;
3503
3504 value = XCAR (XCDR (prop));
3505 if (NUMBERP (value) && XFLOATINT (value) > 0)
3506 it->space_width = value;
3507 }
3508 else if (CONSP (prop)
3509 && EQ (XCAR (prop), Qslice))
3510 {
3511 /* `(slice X Y WIDTH HEIGHT)'. */
3512 Lisp_Object tem;
3513
3514 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3515 return 0;
3516
3517 if (tem = XCDR (prop), CONSP (tem))
3518 {
3519 it->slice.x = XCAR (tem);
3520 if (tem = XCDR (tem), CONSP (tem))
3521 {
3522 it->slice.y = XCAR (tem);
3523 if (tem = XCDR (tem), CONSP (tem))
3524 {
3525 it->slice.width = XCAR (tem);
3526 if (tem = XCDR (tem), CONSP (tem))
3527 it->slice.height = XCAR (tem);
3528 }
3529 }
3530 }
3531 }
3532 else if (CONSP (prop)
3533 && EQ (XCAR (prop), Qraise)
3534 && CONSP (XCDR (prop)))
3535 {
3536 /* `(raise FACTOR)'. */
3537 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3538 return 0;
3539
3540 #ifdef HAVE_WINDOW_SYSTEM
3541 value = XCAR (XCDR (prop));
3542 if (NUMBERP (value))
3543 {
3544 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3545 it->voffset = - (XFLOATINT (value)
3546 * (FONT_HEIGHT (face->font)));
3547 }
3548 #endif /* HAVE_WINDOW_SYSTEM */
3549 }
3550 else if (!it->string_from_display_prop_p)
3551 {
3552 /* `((margin left-margin) VALUE)' or `((margin right-margin)
3553 VALUE) or `((margin nil) VALUE)' or VALUE. */
3554 Lisp_Object location, value;
3555 struct text_pos start_pos;
3556 int valid_p;
3557
3558 /* Characters having this form of property are not displayed, so
3559 we have to find the end of the property. */
3560 start_pos = *position;
3561 *position = display_prop_end (it, object, start_pos);
3562 value = Qnil;
3563
3564 /* Let's stop at the new position and assume that all
3565 text properties change there. */
3566 it->stop_charpos = position->charpos;
3567
3568 if (CONSP (prop)
3569 && (EQ (XCAR (prop), Qleft_fringe)
3570 || EQ (XCAR (prop), Qright_fringe))
3571 && CONSP (XCDR (prop)))
3572 {
3573 unsigned face_id = DEFAULT_FACE_ID;
3574
3575 /* Save current settings of IT so that we can restore them
3576 when we are finished with the glyph property value. */
3577
3578 /* `(left-fringe BITMAP FACE)'. */
3579 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3580 return 0;
3581
3582 #ifdef HAVE_WINDOW_SYSTEM
3583 value = XCAR (XCDR (prop));
3584 if (!NUMBERP (value)
3585 || !valid_fringe_bitmap_id_p (XINT (value)))
3586 return 0;
3587
3588 if (CONSP (XCDR (XCDR (prop))))
3589 {
3590 Lisp_Object face_name = XCAR (XCDR (XCDR (prop)));
3591
3592 face_id = lookup_named_face (it->f, face_name);
3593 if (face_id < 0)
3594 return 0;
3595 }
3596
3597 push_it (it);
3598
3599 it->area = TEXT_AREA;
3600 it->what = IT_IMAGE;
3601 it->image_id = -1; /* no image */
3602 it->position = start_pos;
3603 it->object = NILP (object) ? it->w->buffer : object;
3604 it->method = next_element_from_image;
3605 it->face_id = face_id;
3606
3607 /* Say that we haven't consumed the characters with
3608 `display' property yet. The call to pop_it in
3609 set_iterator_to_next will clean this up. */
3610 *position = start_pos;
3611
3612 if (EQ (XCAR (prop), Qleft_fringe))
3613 {
3614 it->left_user_fringe_bitmap = XINT (value);
3615 it->left_user_fringe_face_id = face_id;
3616 }
3617 else
3618 {
3619 it->right_user_fringe_bitmap = XINT (value);
3620 it->right_user_fringe_face_id = face_id;
3621 }
3622 #endif /* HAVE_WINDOW_SYSTEM */
3623 return 1;
3624 }
3625
3626 location = Qunbound;
3627 if (CONSP (prop) && CONSP (XCAR (prop)))
3628 {
3629 Lisp_Object tem;
3630
3631 value = XCDR (prop);
3632 if (CONSP (value))
3633 value = XCAR (value);
3634
3635 tem = XCAR (prop);
3636 if (EQ (XCAR (tem), Qmargin)
3637 && (tem = XCDR (tem),
3638 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3639 (NILP (tem)
3640 || EQ (tem, Qleft_margin)
3641 || EQ (tem, Qright_margin))))
3642 location = tem;
3643 }
3644
3645 if (EQ (location, Qunbound))
3646 {
3647 location = Qnil;
3648 value = prop;
3649 }
3650
3651 valid_p = (STRINGP (value)
3652 #ifdef HAVE_WINDOW_SYSTEM
3653 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
3654 #endif /* not HAVE_WINDOW_SYSTEM */
3655 || (CONSP (value) && EQ (XCAR (value), Qspace)));
3656
3657 if ((EQ (location, Qleft_margin)
3658 || EQ (location, Qright_margin)
3659 || NILP (location))
3660 && valid_p
3661 && !display_replaced_before_p)
3662 {
3663 replaces_text_display_p = 1;
3664
3665 /* Save current settings of IT so that we can restore them
3666 when we are finished with the glyph property value. */
3667 push_it (it);
3668
3669 if (NILP (location))
3670 it->area = TEXT_AREA;
3671 else if (EQ (location, Qleft_margin))
3672 it->area = LEFT_MARGIN_AREA;
3673 else
3674 it->area = RIGHT_MARGIN_AREA;
3675
3676 if (STRINGP (value))
3677 {
3678 it->string = value;
3679 it->multibyte_p = STRING_MULTIBYTE (it->string);
3680 it->current.overlay_string_index = -1;
3681 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3682 it->end_charpos = it->string_nchars = SCHARS (it->string);
3683 it->method = next_element_from_string;
3684 it->stop_charpos = 0;
3685 it->string_from_display_prop_p = 1;
3686 /* Say that we haven't consumed the characters with
3687 `display' property yet. The call to pop_it in
3688 set_iterator_to_next will clean this up. */
3689 *position = start_pos;
3690 }
3691 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3692 {
3693 it->method = next_element_from_stretch;
3694 it->object = value;
3695 it->current.pos = it->position = start_pos;
3696 }
3697 #ifdef HAVE_WINDOW_SYSTEM
3698 else
3699 {
3700 it->what = IT_IMAGE;
3701 it->image_id = lookup_image (it->f, value);
3702 it->position = start_pos;
3703 it->object = NILP (object) ? it->w->buffer : object;
3704 it->method = next_element_from_image;
3705
3706 /* Say that we haven't consumed the characters with
3707 `display' property yet. The call to pop_it in
3708 set_iterator_to_next will clean this up. */
3709 *position = start_pos;
3710 }
3711 #endif /* HAVE_WINDOW_SYSTEM */
3712 }
3713 else
3714 /* Invalid property or property not supported. Restore
3715 the position to what it was before. */
3716 *position = start_pos;
3717 }
3718
3719 return replaces_text_display_p;
3720 }
3721
3722
3723 /* Check if PROP is a display sub-property value whose text should be
3724 treated as intangible. */
3725
3726 static int
3727 single_display_prop_intangible_p (prop)
3728 Lisp_Object prop;
3729 {
3730 /* Skip over `when FORM'. */
3731 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3732 {
3733 prop = XCDR (prop);
3734 if (!CONSP (prop))
3735 return 0;
3736 prop = XCDR (prop);
3737 }
3738
3739 if (STRINGP (prop))
3740 return 1;
3741
3742 if (!CONSP (prop))
3743 return 0;
3744
3745 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3746 we don't need to treat text as intangible. */
3747 if (EQ (XCAR (prop), Qmargin))
3748 {
3749 prop = XCDR (prop);
3750 if (!CONSP (prop))
3751 return 0;
3752
3753 prop = XCDR (prop);
3754 if (!CONSP (prop)
3755 || EQ (XCAR (prop), Qleft_margin)
3756 || EQ (XCAR (prop), Qright_margin))
3757 return 0;
3758 }
3759
3760 return (CONSP (prop)
3761 && (EQ (XCAR (prop), Qimage)
3762 || EQ (XCAR (prop), Qspace)));
3763 }
3764
3765
3766 /* Check if PROP is a display property value whose text should be
3767 treated as intangible. */
3768
3769 int
3770 display_prop_intangible_p (prop)
3771 Lisp_Object prop;
3772 {
3773 if (CONSP (prop)
3774 && CONSP (XCAR (prop))
3775 && !EQ (Qmargin, XCAR (XCAR (prop))))
3776 {
3777 /* A list of sub-properties. */
3778 while (CONSP (prop))
3779 {
3780 if (single_display_prop_intangible_p (XCAR (prop)))
3781 return 1;
3782 prop = XCDR (prop);
3783 }
3784 }
3785 else if (VECTORP (prop))
3786 {
3787 /* A vector of sub-properties. */
3788 int i;
3789 for (i = 0; i < ASIZE (prop); ++i)
3790 if (single_display_prop_intangible_p (AREF (prop, i)))
3791 return 1;
3792 }
3793 else
3794 return single_display_prop_intangible_p (prop);
3795
3796 return 0;
3797 }
3798
3799
3800 /* Return 1 if PROP is a display sub-property value containing STRING. */
3801
3802 static int
3803 single_display_prop_string_p (prop, string)
3804 Lisp_Object prop, string;
3805 {
3806 if (EQ (string, prop))
3807 return 1;
3808
3809 /* Skip over `when FORM'. */
3810 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3811 {
3812 prop = XCDR (prop);
3813 if (!CONSP (prop))
3814 return 0;
3815 prop = XCDR (prop);
3816 }
3817
3818 if (CONSP (prop))
3819 /* Skip over `margin LOCATION'. */
3820 if (EQ (XCAR (prop), Qmargin))
3821 {
3822 prop = XCDR (prop);
3823 if (!CONSP (prop))
3824 return 0;
3825
3826 prop = XCDR (prop);
3827 if (!CONSP (prop))
3828 return 0;
3829 }
3830
3831 return CONSP (prop) && EQ (XCAR (prop), string);
3832 }
3833
3834
3835 /* Return 1 if STRING appears in the `display' property PROP. */
3836
3837 static int
3838 display_prop_string_p (prop, string)
3839 Lisp_Object prop, string;
3840 {
3841 if (CONSP (prop)
3842 && CONSP (XCAR (prop))
3843 && !EQ (Qmargin, XCAR (XCAR (prop))))
3844 {
3845 /* A list of sub-properties. */
3846 while (CONSP (prop))
3847 {
3848 if (single_display_prop_string_p (XCAR (prop), string))
3849 return 1;
3850 prop = XCDR (prop);
3851 }
3852 }
3853 else if (VECTORP (prop))
3854 {
3855 /* A vector of sub-properties. */
3856 int i;
3857 for (i = 0; i < ASIZE (prop); ++i)
3858 if (single_display_prop_string_p (AREF (prop, i), string))
3859 return 1;
3860 }
3861 else
3862 return single_display_prop_string_p (prop, string);
3863
3864 return 0;
3865 }
3866
3867
3868 /* Determine from which buffer position in W's buffer STRING comes
3869 from. AROUND_CHARPOS is an approximate position where it could
3870 be from. Value is the buffer position or 0 if it couldn't be
3871 determined.
3872
3873 W's buffer must be current.
3874
3875 This function is necessary because we don't record buffer positions
3876 in glyphs generated from strings (to keep struct glyph small).
3877 This function may only use code that doesn't eval because it is
3878 called asynchronously from note_mouse_highlight. */
3879
3880 int
3881 string_buffer_position (w, string, around_charpos)
3882 struct window *w;
3883 Lisp_Object string;
3884 int around_charpos;
3885 {
3886 Lisp_Object limit, prop, pos;
3887 const int MAX_DISTANCE = 1000;
3888 int found = 0;
3889
3890 pos = make_number (around_charpos);
3891 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3892 while (!found && !EQ (pos, limit))
3893 {
3894 prop = Fget_char_property (pos, Qdisplay, Qnil);
3895 if (!NILP (prop) && display_prop_string_p (prop, string))
3896 found = 1;
3897 else
3898 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3899 }
3900
3901 if (!found)
3902 {
3903 pos = make_number (around_charpos);
3904 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3905 while (!found && !EQ (pos, limit))
3906 {
3907 prop = Fget_char_property (pos, Qdisplay, Qnil);
3908 if (!NILP (prop) && display_prop_string_p (prop, string))
3909 found = 1;
3910 else
3911 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3912 limit);
3913 }
3914 }
3915
3916 return found ? XINT (pos) : 0;
3917 }
3918
3919
3920 \f
3921 /***********************************************************************
3922 `composition' property
3923 ***********************************************************************/
3924
3925 static enum prop_handled
3926 handle_auto_composed_prop (it)
3927 struct it *it;
3928 {
3929 enum prop_handled handled = HANDLED_NORMALLY;
3930
3931 if (FUNCTIONP (Vauto_composition_function))
3932 {
3933 Lisp_Object val;
3934 EMACS_INT pos, this_pos;
3935
3936 if (STRINGP (it->string))
3937 pos = IT_STRING_CHARPOS (*it);
3938 else
3939 pos = IT_CHARPOS (*it);
3940 this_pos = pos;
3941
3942 val =Fget_char_property (make_number (pos), Qauto_composed, it->string);
3943 if (! NILP (val))
3944 {
3945 Lisp_Object limit = Qnil, next;
3946
3947 /* As Fnext_single_char_property_change is very slow, we
3948 limit the search to the current line. */
3949 if (STRINGP (it->string))
3950 limit = make_number (SCHARS (it->string));
3951 else
3952 limit = make_number (find_next_newline_no_quit (pos, 1));
3953
3954 next = (Fnext_single_property_change
3955 (make_number (pos), Qauto_composed, it->string, limit));
3956 if (XINT (next) < XINT (limit))
3957 {
3958 /* The current point is auto-composed, but there exist
3959 characters not yet composed beyond the auto-composed
3960 region. There's a possiblity that the last
3961 characters in the region may be newly composed. */
3962 int charpos = XINT (next) - 1, bytepos, c;
3963
3964 if (STRINGP (it->string))
3965 {
3966 bytepos = string_char_to_byte (it->string, charpos);
3967 c = SDATA (it->string)[bytepos];
3968 }
3969 else
3970 {
3971 bytepos = CHAR_TO_BYTE (charpos);
3972 c = FETCH_BYTE (bytepos);
3973 }
3974 if (c != '\n')
3975 /* If the last character is not newline, it may be
3976 composed with the following characters. */
3977 val = Qnil, pos = charpos + 1;
3978 }
3979 }
3980 if (NILP (val))
3981 {
3982 int count = SPECPDL_INDEX ();
3983 Lisp_Object args[3];
3984
3985 args[0] = Vauto_composition_function;
3986 specbind (Qauto_composition_function, Qnil);
3987 args[1] = make_number (pos);
3988 args[2] = it->string;
3989 safe_call (3, args);
3990 unbind_to (count, Qnil);
3991
3992 if (this_pos == pos)
3993 {
3994 val = Fget_char_property (args[1], Qauto_composed, it->string);
3995 /* Return HANDLED_RECOMPUTE_PROPS only if function composed
3996 something. This avoids an endless loop if they failed to
3997 fontify the text for which reason ever. */
3998 if (! NILP (val))
3999 handled = HANDLED_RECOMPUTE_PROPS;
4000 }
4001 else
4002 handled = HANDLED_RECOMPUTE_PROPS;
4003 }
4004 }
4005
4006 return handled;
4007 }
4008
4009 /* Set up iterator IT from `composition' property at its current
4010 position. Called from handle_stop. */
4011
4012 static enum prop_handled
4013 handle_composition_prop (it)
4014 struct it *it;
4015 {
4016 Lisp_Object prop, string;
4017 EMACS_INT pos, pos_byte, start, end;
4018 enum prop_handled handled = HANDLED_NORMALLY;
4019
4020 if (STRINGP (it->string))
4021 {
4022 pos = IT_STRING_CHARPOS (*it);
4023 pos_byte = IT_STRING_BYTEPOS (*it);
4024 string = it->string;
4025 }
4026 else
4027 {
4028 pos = IT_CHARPOS (*it);
4029 pos_byte = IT_BYTEPOS (*it);
4030 string = Qnil;
4031 }
4032
4033 /* If there's a valid composition and point is not inside of the
4034 composition (in the case that the composition is from the current
4035 buffer), draw a glyph composed from the composition components. */
4036 if (find_composition (pos, -1, &start, &end, &prop, string)
4037 && COMPOSITION_VALID_P (start, end, prop)
4038 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4039 {
4040 int id;
4041
4042 if (start != pos)
4043 {
4044 if (STRINGP (it->string))
4045 pos_byte = string_char_to_byte (it->string, start);
4046 else
4047 pos_byte = CHAR_TO_BYTE (start);
4048 }
4049 id = get_composition_id (start, pos_byte, end - start, prop, string);
4050
4051 if (id >= 0)
4052 {
4053 it->method = next_element_from_composition;
4054 it->cmp_id = id;
4055 it->cmp_len = COMPOSITION_LENGTH (prop);
4056 /* For a terminal, draw only the first character of the
4057 components. */
4058 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4059 it->len = (STRINGP (it->string)
4060 ? string_char_to_byte (it->string, end)
4061 : CHAR_TO_BYTE (end)) - pos_byte;
4062 it->stop_charpos = end;
4063 handled = HANDLED_RETURN;
4064 }
4065 }
4066
4067 return handled;
4068 }
4069
4070
4071 \f
4072 /***********************************************************************
4073 Overlay strings
4074 ***********************************************************************/
4075
4076 /* The following structure is used to record overlay strings for
4077 later sorting in load_overlay_strings. */
4078
4079 struct overlay_entry
4080 {
4081 Lisp_Object overlay;
4082 Lisp_Object string;
4083 int priority;
4084 int after_string_p;
4085 };
4086
4087
4088 /* Set up iterator IT from overlay strings at its current position.
4089 Called from handle_stop. */
4090
4091 static enum prop_handled
4092 handle_overlay_change (it)
4093 struct it *it;
4094 {
4095 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4096 return HANDLED_RECOMPUTE_PROPS;
4097 else
4098 return HANDLED_NORMALLY;
4099 }
4100
4101
4102 /* Set up the next overlay string for delivery by IT, if there is an
4103 overlay string to deliver. Called by set_iterator_to_next when the
4104 end of the current overlay string is reached. If there are more
4105 overlay strings to display, IT->string and
4106 IT->current.overlay_string_index are set appropriately here.
4107 Otherwise IT->string is set to nil. */
4108
4109 static void
4110 next_overlay_string (it)
4111 struct it *it;
4112 {
4113 ++it->current.overlay_string_index;
4114 if (it->current.overlay_string_index == it->n_overlay_strings)
4115 {
4116 /* No more overlay strings. Restore IT's settings to what
4117 they were before overlay strings were processed, and
4118 continue to deliver from current_buffer. */
4119 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4120
4121 pop_it (it);
4122 xassert (it->stop_charpos >= BEGV
4123 && it->stop_charpos <= it->end_charpos);
4124 it->string = Qnil;
4125 it->current.overlay_string_index = -1;
4126 SET_TEXT_POS (it->current.string_pos, -1, -1);
4127 it->n_overlay_strings = 0;
4128 it->method = next_element_from_buffer;
4129
4130 /* If we're at the end of the buffer, record that we have
4131 processed the overlay strings there already, so that
4132 next_element_from_buffer doesn't try it again. */
4133 if (IT_CHARPOS (*it) >= it->end_charpos)
4134 it->overlay_strings_at_end_processed_p = 1;
4135
4136 /* If we have to display `...' for invisible text, set
4137 the iterator up for that. */
4138 if (display_ellipsis_p)
4139 setup_for_ellipsis (it);
4140 }
4141 else
4142 {
4143 /* There are more overlay strings to process. If
4144 IT->current.overlay_string_index has advanced to a position
4145 where we must load IT->overlay_strings with more strings, do
4146 it. */
4147 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4148
4149 if (it->current.overlay_string_index && i == 0)
4150 load_overlay_strings (it, 0);
4151
4152 /* Initialize IT to deliver display elements from the overlay
4153 string. */
4154 it->string = it->overlay_strings[i];
4155 it->multibyte_p = STRING_MULTIBYTE (it->string);
4156 SET_TEXT_POS (it->current.string_pos, 0, 0);
4157 it->method = next_element_from_string;
4158 it->stop_charpos = 0;
4159 }
4160
4161 CHECK_IT (it);
4162 }
4163
4164
4165 /* Compare two overlay_entry structures E1 and E2. Used as a
4166 comparison function for qsort in load_overlay_strings. Overlay
4167 strings for the same position are sorted so that
4168
4169 1. All after-strings come in front of before-strings, except
4170 when they come from the same overlay.
4171
4172 2. Within after-strings, strings are sorted so that overlay strings
4173 from overlays with higher priorities come first.
4174
4175 2. Within before-strings, strings are sorted so that overlay
4176 strings from overlays with higher priorities come last.
4177
4178 Value is analogous to strcmp. */
4179
4180
4181 static int
4182 compare_overlay_entries (e1, e2)
4183 void *e1, *e2;
4184 {
4185 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4186 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4187 int result;
4188
4189 if (entry1->after_string_p != entry2->after_string_p)
4190 {
4191 /* Let after-strings appear in front of before-strings if
4192 they come from different overlays. */
4193 if (EQ (entry1->overlay, entry2->overlay))
4194 result = entry1->after_string_p ? 1 : -1;
4195 else
4196 result = entry1->after_string_p ? -1 : 1;
4197 }
4198 else if (entry1->after_string_p)
4199 /* After-strings sorted in order of decreasing priority. */
4200 result = entry2->priority - entry1->priority;
4201 else
4202 /* Before-strings sorted in order of increasing priority. */
4203 result = entry1->priority - entry2->priority;
4204
4205 return result;
4206 }
4207
4208
4209 /* Load the vector IT->overlay_strings with overlay strings from IT's
4210 current buffer position, or from CHARPOS if that is > 0. Set
4211 IT->n_overlays to the total number of overlay strings found.
4212
4213 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4214 a time. On entry into load_overlay_strings,
4215 IT->current.overlay_string_index gives the number of overlay
4216 strings that have already been loaded by previous calls to this
4217 function.
4218
4219 IT->add_overlay_start contains an additional overlay start
4220 position to consider for taking overlay strings from, if non-zero.
4221 This position comes into play when the overlay has an `invisible'
4222 property, and both before and after-strings. When we've skipped to
4223 the end of the overlay, because of its `invisible' property, we
4224 nevertheless want its before-string to appear.
4225 IT->add_overlay_start will contain the overlay start position
4226 in this case.
4227
4228 Overlay strings are sorted so that after-string strings come in
4229 front of before-string strings. Within before and after-strings,
4230 strings are sorted by overlay priority. See also function
4231 compare_overlay_entries. */
4232
4233 static void
4234 load_overlay_strings (it, charpos)
4235 struct it *it;
4236 int charpos;
4237 {
4238 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4239 Lisp_Object overlay, window, str, invisible;
4240 struct Lisp_Overlay *ov;
4241 int start, end;
4242 int size = 20;
4243 int n = 0, i, j, invis_p;
4244 struct overlay_entry *entries
4245 = (struct overlay_entry *) alloca (size * sizeof *entries);
4246
4247 if (charpos <= 0)
4248 charpos = IT_CHARPOS (*it);
4249
4250 /* Append the overlay string STRING of overlay OVERLAY to vector
4251 `entries' which has size `size' and currently contains `n'
4252 elements. AFTER_P non-zero means STRING is an after-string of
4253 OVERLAY. */
4254 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4255 do \
4256 { \
4257 Lisp_Object priority; \
4258 \
4259 if (n == size) \
4260 { \
4261 int new_size = 2 * size; \
4262 struct overlay_entry *old = entries; \
4263 entries = \
4264 (struct overlay_entry *) alloca (new_size \
4265 * sizeof *entries); \
4266 bcopy (old, entries, size * sizeof *entries); \
4267 size = new_size; \
4268 } \
4269 \
4270 entries[n].string = (STRING); \
4271 entries[n].overlay = (OVERLAY); \
4272 priority = Foverlay_get ((OVERLAY), Qpriority); \
4273 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4274 entries[n].after_string_p = (AFTER_P); \
4275 ++n; \
4276 } \
4277 while (0)
4278
4279 /* Process overlay before the overlay center. */
4280 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4281 {
4282 XSETMISC (overlay, ov);
4283 xassert (OVERLAYP (overlay));
4284 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4285 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4286
4287 if (end < charpos)
4288 break;
4289
4290 /* Skip this overlay if it doesn't start or end at IT's current
4291 position. */
4292 if (end != charpos && start != charpos)
4293 continue;
4294
4295 /* Skip this overlay if it doesn't apply to IT->w. */
4296 window = Foverlay_get (overlay, Qwindow);
4297 if (WINDOWP (window) && XWINDOW (window) != it->w)
4298 continue;
4299
4300 /* If the text ``under'' the overlay is invisible, both before-
4301 and after-strings from this overlay are visible; start and
4302 end position are indistinguishable. */
4303 invisible = Foverlay_get (overlay, Qinvisible);
4304 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4305
4306 /* If overlay has a non-empty before-string, record it. */
4307 if ((start == charpos || (end == charpos && invis_p))
4308 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4309 && SCHARS (str))
4310 RECORD_OVERLAY_STRING (overlay, str, 0);
4311
4312 /* If overlay has a non-empty after-string, record it. */
4313 if ((end == charpos || (start == charpos && invis_p))
4314 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4315 && SCHARS (str))
4316 RECORD_OVERLAY_STRING (overlay, str, 1);
4317 }
4318
4319 /* Process overlays after the overlay center. */
4320 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4321 {
4322 XSETMISC (overlay, ov);
4323 xassert (OVERLAYP (overlay));
4324 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4325 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4326
4327 if (start > charpos)
4328 break;
4329
4330 /* Skip this overlay if it doesn't start or end at IT's current
4331 position. */
4332 if (end != charpos && start != charpos)
4333 continue;
4334
4335 /* Skip this overlay if it doesn't apply to IT->w. */
4336 window = Foverlay_get (overlay, Qwindow);
4337 if (WINDOWP (window) && XWINDOW (window) != it->w)
4338 continue;
4339
4340 /* If the text ``under'' the overlay is invisible, it has a zero
4341 dimension, and both before- and after-strings apply. */
4342 invisible = Foverlay_get (overlay, Qinvisible);
4343 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4344
4345 /* If overlay has a non-empty before-string, record it. */
4346 if ((start == charpos || (end == charpos && invis_p))
4347 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4348 && SCHARS (str))
4349 RECORD_OVERLAY_STRING (overlay, str, 0);
4350
4351 /* If overlay has a non-empty after-string, record it. */
4352 if ((end == charpos || (start == charpos && invis_p))
4353 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4354 && SCHARS (str))
4355 RECORD_OVERLAY_STRING (overlay, str, 1);
4356 }
4357
4358 #undef RECORD_OVERLAY_STRING
4359
4360 /* Sort entries. */
4361 if (n > 1)
4362 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4363
4364 /* Record the total number of strings to process. */
4365 it->n_overlay_strings = n;
4366
4367 /* IT->current.overlay_string_index is the number of overlay strings
4368 that have already been consumed by IT. Copy some of the
4369 remaining overlay strings to IT->overlay_strings. */
4370 i = 0;
4371 j = it->current.overlay_string_index;
4372 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4373 it->overlay_strings[i++] = entries[j++].string;
4374
4375 CHECK_IT (it);
4376 }
4377
4378
4379 /* Get the first chunk of overlay strings at IT's current buffer
4380 position, or at CHARPOS if that is > 0. Value is non-zero if at
4381 least one overlay string was found. */
4382
4383 static int
4384 get_overlay_strings (it, charpos)
4385 struct it *it;
4386 int charpos;
4387 {
4388 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4389 process. This fills IT->overlay_strings with strings, and sets
4390 IT->n_overlay_strings to the total number of strings to process.
4391 IT->pos.overlay_string_index has to be set temporarily to zero
4392 because load_overlay_strings needs this; it must be set to -1
4393 when no overlay strings are found because a zero value would
4394 indicate a position in the first overlay string. */
4395 it->current.overlay_string_index = 0;
4396 load_overlay_strings (it, charpos);
4397
4398 /* If we found overlay strings, set up IT to deliver display
4399 elements from the first one. Otherwise set up IT to deliver
4400 from current_buffer. */
4401 if (it->n_overlay_strings)
4402 {
4403 /* Make sure we know settings in current_buffer, so that we can
4404 restore meaningful values when we're done with the overlay
4405 strings. */
4406 compute_stop_pos (it);
4407 xassert (it->face_id >= 0);
4408
4409 /* Save IT's settings. They are restored after all overlay
4410 strings have been processed. */
4411 xassert (it->sp == 0);
4412 push_it (it);
4413
4414 /* Set up IT to deliver display elements from the first overlay
4415 string. */
4416 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4417 it->string = it->overlay_strings[0];
4418 it->stop_charpos = 0;
4419 xassert (STRINGP (it->string));
4420 it->end_charpos = SCHARS (it->string);
4421 it->multibyte_p = STRING_MULTIBYTE (it->string);
4422 it->method = next_element_from_string;
4423 }
4424 else
4425 {
4426 it->string = Qnil;
4427 it->current.overlay_string_index = -1;
4428 it->method = next_element_from_buffer;
4429 }
4430
4431 CHECK_IT (it);
4432
4433 /* Value is non-zero if we found at least one overlay string. */
4434 return STRINGP (it->string);
4435 }
4436
4437
4438 \f
4439 /***********************************************************************
4440 Saving and restoring state
4441 ***********************************************************************/
4442
4443 /* Save current settings of IT on IT->stack. Called, for example,
4444 before setting up IT for an overlay string, to be able to restore
4445 IT's settings to what they were after the overlay string has been
4446 processed. */
4447
4448 static void
4449 push_it (it)
4450 struct it *it;
4451 {
4452 struct iterator_stack_entry *p;
4453
4454 xassert (it->sp < 2);
4455 p = it->stack + it->sp;
4456
4457 p->stop_charpos = it->stop_charpos;
4458 xassert (it->face_id >= 0);
4459 p->face_id = it->face_id;
4460 p->string = it->string;
4461 p->pos = it->current;
4462 p->end_charpos = it->end_charpos;
4463 p->string_nchars = it->string_nchars;
4464 p->area = it->area;
4465 p->multibyte_p = it->multibyte_p;
4466 p->slice = it->slice;
4467 p->space_width = it->space_width;
4468 p->font_height = it->font_height;
4469 p->voffset = it->voffset;
4470 p->string_from_display_prop_p = it->string_from_display_prop_p;
4471 p->display_ellipsis_p = 0;
4472 ++it->sp;
4473 }
4474
4475
4476 /* Restore IT's settings from IT->stack. Called, for example, when no
4477 more overlay strings must be processed, and we return to delivering
4478 display elements from a buffer, or when the end of a string from a
4479 `display' property is reached and we return to delivering display
4480 elements from an overlay string, or from a buffer. */
4481
4482 static void
4483 pop_it (it)
4484 struct it *it;
4485 {
4486 struct iterator_stack_entry *p;
4487
4488 xassert (it->sp > 0);
4489 --it->sp;
4490 p = it->stack + it->sp;
4491 it->stop_charpos = p->stop_charpos;
4492 it->face_id = p->face_id;
4493 it->string = p->string;
4494 it->current = p->pos;
4495 it->end_charpos = p->end_charpos;
4496 it->string_nchars = p->string_nchars;
4497 it->area = p->area;
4498 it->multibyte_p = p->multibyte_p;
4499 it->slice = p->slice;
4500 it->space_width = p->space_width;
4501 it->font_height = p->font_height;
4502 it->voffset = p->voffset;
4503 it->string_from_display_prop_p = p->string_from_display_prop_p;
4504 }
4505
4506
4507 \f
4508 /***********************************************************************
4509 Moving over lines
4510 ***********************************************************************/
4511
4512 /* Set IT's current position to the previous line start. */
4513
4514 static void
4515 back_to_previous_line_start (it)
4516 struct it *it;
4517 {
4518 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4519 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4520 }
4521
4522
4523 /* Move IT to the next line start.
4524
4525 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4526 we skipped over part of the text (as opposed to moving the iterator
4527 continuously over the text). Otherwise, don't change the value
4528 of *SKIPPED_P.
4529
4530 Newlines may come from buffer text, overlay strings, or strings
4531 displayed via the `display' property. That's the reason we can't
4532 simply use find_next_newline_no_quit.
4533
4534 Note that this function may not skip over invisible text that is so
4535 because of text properties and immediately follows a newline. If
4536 it would, function reseat_at_next_visible_line_start, when called
4537 from set_iterator_to_next, would effectively make invisible
4538 characters following a newline part of the wrong glyph row, which
4539 leads to wrong cursor motion. */
4540
4541 static int
4542 forward_to_next_line_start (it, skipped_p)
4543 struct it *it;
4544 int *skipped_p;
4545 {
4546 int old_selective, newline_found_p, n;
4547 const int MAX_NEWLINE_DISTANCE = 500;
4548
4549 /* If already on a newline, just consume it to avoid unintended
4550 skipping over invisible text below. */
4551 if (it->what == IT_CHARACTER
4552 && it->c == '\n'
4553 && CHARPOS (it->position) == IT_CHARPOS (*it))
4554 {
4555 set_iterator_to_next (it, 0);
4556 it->c = 0;
4557 return 1;
4558 }
4559
4560 /* Don't handle selective display in the following. It's (a)
4561 unnecessary because it's done by the caller, and (b) leads to an
4562 infinite recursion because next_element_from_ellipsis indirectly
4563 calls this function. */
4564 old_selective = it->selective;
4565 it->selective = 0;
4566
4567 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4568 from buffer text. */
4569 for (n = newline_found_p = 0;
4570 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4571 n += STRINGP (it->string) ? 0 : 1)
4572 {
4573 if (!get_next_display_element (it))
4574 return 0;
4575 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4576 set_iterator_to_next (it, 0);
4577 }
4578
4579 /* If we didn't find a newline near enough, see if we can use a
4580 short-cut. */
4581 if (!newline_found_p)
4582 {
4583 int start = IT_CHARPOS (*it);
4584 int limit = find_next_newline_no_quit (start, 1);
4585 Lisp_Object pos;
4586
4587 xassert (!STRINGP (it->string));
4588
4589 /* If there isn't any `display' property in sight, and no
4590 overlays, we can just use the position of the newline in
4591 buffer text. */
4592 if (it->stop_charpos >= limit
4593 || ((pos = Fnext_single_property_change (make_number (start),
4594 Qdisplay,
4595 Qnil, make_number (limit)),
4596 NILP (pos))
4597 && next_overlay_change (start) == ZV))
4598 {
4599 IT_CHARPOS (*it) = limit;
4600 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4601 *skipped_p = newline_found_p = 1;
4602 }
4603 else
4604 {
4605 while (get_next_display_element (it)
4606 && !newline_found_p)
4607 {
4608 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4609 set_iterator_to_next (it, 0);
4610 }
4611 }
4612 }
4613
4614 it->selective = old_selective;
4615 return newline_found_p;
4616 }
4617
4618
4619 /* Set IT's current position to the previous visible line start. Skip
4620 invisible text that is so either due to text properties or due to
4621 selective display. Caution: this does not change IT->current_x and
4622 IT->hpos. */
4623
4624 static void
4625 back_to_previous_visible_line_start (it)
4626 struct it *it;
4627 {
4628 int visible_p = 0;
4629
4630 /* Go back one newline if not on BEGV already. */
4631 if (IT_CHARPOS (*it) > BEGV)
4632 back_to_previous_line_start (it);
4633
4634 /* Move over lines that are invisible because of selective display
4635 or text properties. */
4636 while (IT_CHARPOS (*it) > BEGV
4637 && !visible_p)
4638 {
4639 visible_p = 1;
4640
4641 /* If selective > 0, then lines indented more than that values
4642 are invisible. */
4643 if (it->selective > 0
4644 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4645 (double) it->selective)) /* iftc */
4646 visible_p = 0;
4647 else
4648 {
4649 Lisp_Object prop;
4650
4651 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
4652 Qinvisible, it->window);
4653 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4654 visible_p = 0;
4655 }
4656
4657 if (visible_p)
4658 {
4659 struct it it2 = *it;
4660
4661 if (handle_display_prop (&it2) == HANDLED_RETURN)
4662 visible_p = 0;
4663 }
4664
4665 /* Back one more newline if the current one is invisible. */
4666 if (!visible_p)
4667 back_to_previous_line_start (it);
4668 }
4669
4670 xassert (IT_CHARPOS (*it) >= BEGV);
4671 xassert (IT_CHARPOS (*it) == BEGV
4672 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4673 CHECK_IT (it);
4674 }
4675
4676
4677 /* Reseat iterator IT at the previous visible line start. Skip
4678 invisible text that is so either due to text properties or due to
4679 selective display. At the end, update IT's overlay information,
4680 face information etc. */
4681
4682 static void
4683 reseat_at_previous_visible_line_start (it)
4684 struct it *it;
4685 {
4686 back_to_previous_visible_line_start (it);
4687 reseat (it, it->current.pos, 1);
4688 CHECK_IT (it);
4689 }
4690
4691
4692 /* Reseat iterator IT on the next visible line start in the current
4693 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4694 preceding the line start. Skip over invisible text that is so
4695 because of selective display. Compute faces, overlays etc at the
4696 new position. Note that this function does not skip over text that
4697 is invisible because of text properties. */
4698
4699 static void
4700 reseat_at_next_visible_line_start (it, on_newline_p)
4701 struct it *it;
4702 int on_newline_p;
4703 {
4704 int newline_found_p, skipped_p = 0;
4705
4706 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4707
4708 /* Skip over lines that are invisible because they are indented
4709 more than the value of IT->selective. */
4710 if (it->selective > 0)
4711 while (IT_CHARPOS (*it) < ZV
4712 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4713 (double) it->selective)) /* iftc */
4714 {
4715 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4716 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4717 }
4718
4719 /* Position on the newline if that's what's requested. */
4720 if (on_newline_p && newline_found_p)
4721 {
4722 if (STRINGP (it->string))
4723 {
4724 if (IT_STRING_CHARPOS (*it) > 0)
4725 {
4726 --IT_STRING_CHARPOS (*it);
4727 --IT_STRING_BYTEPOS (*it);
4728 }
4729 }
4730 else if (IT_CHARPOS (*it) > BEGV)
4731 {
4732 --IT_CHARPOS (*it);
4733 --IT_BYTEPOS (*it);
4734 reseat (it, it->current.pos, 0);
4735 }
4736 }
4737 else if (skipped_p)
4738 reseat (it, it->current.pos, 0);
4739
4740 CHECK_IT (it);
4741 }
4742
4743
4744 \f
4745 /***********************************************************************
4746 Changing an iterator's position
4747 ***********************************************************************/
4748
4749 /* Change IT's current position to POS in current_buffer. If FORCE_P
4750 is non-zero, always check for text properties at the new position.
4751 Otherwise, text properties are only looked up if POS >=
4752 IT->check_charpos of a property. */
4753
4754 static void
4755 reseat (it, pos, force_p)
4756 struct it *it;
4757 struct text_pos pos;
4758 int force_p;
4759 {
4760 int original_pos = IT_CHARPOS (*it);
4761
4762 reseat_1 (it, pos, 0);
4763
4764 /* Determine where to check text properties. Avoid doing it
4765 where possible because text property lookup is very expensive. */
4766 if (force_p
4767 || CHARPOS (pos) > it->stop_charpos
4768 || CHARPOS (pos) < original_pos)
4769 handle_stop (it);
4770
4771 CHECK_IT (it);
4772 }
4773
4774
4775 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4776 IT->stop_pos to POS, also. */
4777
4778 static void
4779 reseat_1 (it, pos, set_stop_p)
4780 struct it *it;
4781 struct text_pos pos;
4782 int set_stop_p;
4783 {
4784 /* Don't call this function when scanning a C string. */
4785 xassert (it->s == NULL);
4786
4787 /* POS must be a reasonable value. */
4788 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4789
4790 it->current.pos = it->position = pos;
4791 XSETBUFFER (it->object, current_buffer);
4792 it->end_charpos = ZV;
4793 it->dpvec = NULL;
4794 it->current.dpvec_index = -1;
4795 it->current.overlay_string_index = -1;
4796 IT_STRING_CHARPOS (*it) = -1;
4797 IT_STRING_BYTEPOS (*it) = -1;
4798 it->string = Qnil;
4799 it->method = next_element_from_buffer;
4800 /* RMS: I added this to fix a bug in move_it_vertically_backward
4801 where it->area continued to relate to the starting point
4802 for the backward motion. Bug report from
4803 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4804 However, I am not sure whether reseat still does the right thing
4805 in general after this change. */
4806 it->area = TEXT_AREA;
4807 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4808 it->sp = 0;
4809 it->face_before_selective_p = 0;
4810
4811 if (set_stop_p)
4812 it->stop_charpos = CHARPOS (pos);
4813 }
4814
4815
4816 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4817 If S is non-null, it is a C string to iterate over. Otherwise,
4818 STRING gives a Lisp string to iterate over.
4819
4820 If PRECISION > 0, don't return more then PRECISION number of
4821 characters from the string.
4822
4823 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4824 characters have been returned. FIELD_WIDTH < 0 means an infinite
4825 field width.
4826
4827 MULTIBYTE = 0 means disable processing of multibyte characters,
4828 MULTIBYTE > 0 means enable it,
4829 MULTIBYTE < 0 means use IT->multibyte_p.
4830
4831 IT must be initialized via a prior call to init_iterator before
4832 calling this function. */
4833
4834 static void
4835 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4836 struct it *it;
4837 unsigned char *s;
4838 Lisp_Object string;
4839 int charpos;
4840 int precision, field_width, multibyte;
4841 {
4842 /* No region in strings. */
4843 it->region_beg_charpos = it->region_end_charpos = -1;
4844
4845 /* No text property checks performed by default, but see below. */
4846 it->stop_charpos = -1;
4847
4848 /* Set iterator position and end position. */
4849 bzero (&it->current, sizeof it->current);
4850 it->current.overlay_string_index = -1;
4851 it->current.dpvec_index = -1;
4852 xassert (charpos >= 0);
4853
4854 /* If STRING is specified, use its multibyteness, otherwise use the
4855 setting of MULTIBYTE, if specified. */
4856 if (multibyte >= 0)
4857 it->multibyte_p = multibyte > 0;
4858
4859 if (s == NULL)
4860 {
4861 xassert (STRINGP (string));
4862 it->string = string;
4863 it->s = NULL;
4864 it->end_charpos = it->string_nchars = SCHARS (string);
4865 it->method = next_element_from_string;
4866 it->current.string_pos = string_pos (charpos, string);
4867 }
4868 else
4869 {
4870 it->s = s;
4871 it->string = Qnil;
4872
4873 /* Note that we use IT->current.pos, not it->current.string_pos,
4874 for displaying C strings. */
4875 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4876 if (it->multibyte_p)
4877 {
4878 it->current.pos = c_string_pos (charpos, s, 1);
4879 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4880 }
4881 else
4882 {
4883 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4884 it->end_charpos = it->string_nchars = strlen (s);
4885 }
4886
4887 it->method = next_element_from_c_string;
4888 }
4889
4890 /* PRECISION > 0 means don't return more than PRECISION characters
4891 from the string. */
4892 if (precision > 0 && it->end_charpos - charpos > precision)
4893 it->end_charpos = it->string_nchars = charpos + precision;
4894
4895 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4896 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4897 FIELD_WIDTH < 0 means infinite field width. This is useful for
4898 padding with `-' at the end of a mode line. */
4899 if (field_width < 0)
4900 field_width = INFINITY;
4901 if (field_width > it->end_charpos - charpos)
4902 it->end_charpos = charpos + field_width;
4903
4904 /* Use the standard display table for displaying strings. */
4905 if (DISP_TABLE_P (Vstandard_display_table))
4906 it->dp = XCHAR_TABLE (Vstandard_display_table);
4907
4908 it->stop_charpos = charpos;
4909 CHECK_IT (it);
4910 }
4911
4912
4913 \f
4914 /***********************************************************************
4915 Iteration
4916 ***********************************************************************/
4917
4918 /* Load IT's display element fields with information about the next
4919 display element from the current position of IT. Value is zero if
4920 end of buffer (or C string) is reached. */
4921
4922 int
4923 get_next_display_element (it)
4924 struct it *it;
4925 {
4926 /* Non-zero means that we found a display element. Zero means that
4927 we hit the end of what we iterate over. Performance note: the
4928 function pointer `method' used here turns out to be faster than
4929 using a sequence of if-statements. */
4930 int success_p = (*it->method) (it);
4931
4932 if (it->what == IT_CHARACTER)
4933 {
4934 /* Map via display table or translate control characters.
4935 IT->c, IT->len etc. have been set to the next character by
4936 the function call above. If we have a display table, and it
4937 contains an entry for IT->c, translate it. Don't do this if
4938 IT->c itself comes from a display table, otherwise we could
4939 end up in an infinite recursion. (An alternative could be to
4940 count the recursion depth of this function and signal an
4941 error when a certain maximum depth is reached.) Is it worth
4942 it? */
4943 if (success_p && it->dpvec == NULL)
4944 {
4945 Lisp_Object dv;
4946
4947 if (it->dp
4948 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4949 VECTORP (dv)))
4950 {
4951 struct Lisp_Vector *v = XVECTOR (dv);
4952
4953 /* Return the first character from the display table
4954 entry, if not empty. If empty, don't display the
4955 current character. */
4956 if (v->size)
4957 {
4958 it->dpvec_char_len = it->len;
4959 it->dpvec = v->contents;
4960 it->dpend = v->contents + v->size;
4961 it->current.dpvec_index = 0;
4962 it->method = next_element_from_display_vector;
4963 success_p = get_next_display_element (it);
4964 }
4965 else
4966 {
4967 set_iterator_to_next (it, 0);
4968 success_p = get_next_display_element (it);
4969 }
4970 }
4971
4972 /* Translate control characters into `\003' or `^C' form.
4973 Control characters coming from a display table entry are
4974 currently not translated because we use IT->dpvec to hold
4975 the translation. This could easily be changed but I
4976 don't believe that it is worth doing.
4977
4978 If it->multibyte_p is nonzero, eight-bit characters and
4979 non-printable multibyte characters are also translated to
4980 octal form.
4981
4982 If it->multibyte_p is zero, eight-bit characters that
4983 don't have corresponding multibyte char code are also
4984 translated to octal form. */
4985 else if ((it->c < ' '
4986 && (it->area != TEXT_AREA
4987 || (it->c != '\n' && it->c != '\t')))
4988 || (it->c != '\n' && it->c != '\t'
4989 && (it->multibyte_p ? !CHAR_PRINTABLE_P (it->c)
4990 : it->c == 127)))
4991 {
4992 /* IT->c is a control character which must be displayed
4993 either as '\003' or as `^C' where the '\\' and '^'
4994 can be defined in the display table. Fill
4995 IT->ctl_chars with glyphs for what we have to
4996 display. Then, set IT->dpvec to these glyphs. */
4997 GLYPH g;
4998
4999 if (it->c < 128 && it->ctl_arrow_p)
5000 {
5001 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5002 if (it->dp
5003 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5004 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5005 g = XINT (DISP_CTRL_GLYPH (it->dp));
5006 else
5007 g = FAST_MAKE_GLYPH ('^', 0);
5008 XSETINT (it->ctl_chars[0], g);
5009
5010 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
5011 XSETINT (it->ctl_chars[1], g);
5012
5013 /* Set up IT->dpvec and return first character from it. */
5014 it->dpvec_char_len = it->len;
5015 it->dpvec = it->ctl_chars;
5016 it->dpend = it->dpvec + 2;
5017 it->current.dpvec_index = 0;
5018 it->method = next_element_from_display_vector;
5019 get_next_display_element (it);
5020 }
5021 else
5022 {
5023 unsigned char str[MAX_MULTIBYTE_LENGTH];
5024 int len;
5025 int i;
5026 GLYPH escape_glyph;
5027
5028 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5029 if (it->dp
5030 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5031 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5032 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5033 else
5034 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
5035
5036 if (CHAR_BYTE8_P (it->c))
5037 {
5038 str[0] = CHAR_TO_BYTE8 (it->c);
5039 len = 1;
5040 }
5041 else if (it->c < 256)
5042 {
5043 str[0] = it->c;
5044 len = 1;
5045 }
5046 else
5047 {
5048 /* It's an invalid character, which
5049 shouldn't happen actually, but due to
5050 bugs it may happen. Let's print the char
5051 as is, there's not much meaningful we can
5052 do with it. */
5053 str[0] = it->c;
5054 str[1] = it->c >> 8;
5055 str[2] = it->c >> 16;
5056 str[3] = it->c >> 24;
5057 len = 4;
5058 }
5059
5060 for (i = 0; i < len; i++)
5061 {
5062 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5063 /* Insert three more glyphs into IT->ctl_chars for
5064 the octal display of the character. */
5065 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
5066 XSETINT (it->ctl_chars[i * 4 + 1], g);
5067 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
5068 XSETINT (it->ctl_chars[i * 4 + 2], g);
5069 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
5070 XSETINT (it->ctl_chars[i * 4 + 3], g);
5071 }
5072
5073 /* Set up IT->dpvec and return the first character
5074 from it. */
5075 it->dpvec_char_len = it->len;
5076 it->dpvec = it->ctl_chars;
5077 it->dpend = it->dpvec + len * 4;
5078 it->current.dpvec_index = 0;
5079 it->method = next_element_from_display_vector;
5080 get_next_display_element (it);
5081 }
5082 }
5083 }
5084
5085 /* Adjust face id for a multibyte character. There are no
5086 multibyte character in unibyte text. */
5087 if (it->multibyte_p
5088 && success_p
5089 && FRAME_WINDOW_P (it->f))
5090 {
5091 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5092 int pos = (it->s ? -1
5093 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
5094 : IT_CHARPOS (*it));
5095
5096 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
5097 }
5098 }
5099
5100 /* Is this character the last one of a run of characters with
5101 box? If yes, set IT->end_of_box_run_p to 1. */
5102 if (it->face_box_p
5103 && it->s == NULL)
5104 {
5105 int face_id;
5106 struct face *face;
5107
5108 it->end_of_box_run_p
5109 = ((face_id = face_after_it_pos (it),
5110 face_id != it->face_id)
5111 && (face = FACE_FROM_ID (it->f, face_id),
5112 face->box == FACE_NO_BOX));
5113 }
5114
5115 /* Value is 0 if end of buffer or string reached. */
5116 return success_p;
5117 }
5118
5119
5120 /* Move IT to the next display element.
5121
5122 RESEAT_P non-zero means if called on a newline in buffer text,
5123 skip to the next visible line start.
5124
5125 Functions get_next_display_element and set_iterator_to_next are
5126 separate because I find this arrangement easier to handle than a
5127 get_next_display_element function that also increments IT's
5128 position. The way it is we can first look at an iterator's current
5129 display element, decide whether it fits on a line, and if it does,
5130 increment the iterator position. The other way around we probably
5131 would either need a flag indicating whether the iterator has to be
5132 incremented the next time, or we would have to implement a
5133 decrement position function which would not be easy to write. */
5134
5135 void
5136 set_iterator_to_next (it, reseat_p)
5137 struct it *it;
5138 int reseat_p;
5139 {
5140 /* Reset flags indicating start and end of a sequence of characters
5141 with box. Reset them at the start of this function because
5142 moving the iterator to a new position might set them. */
5143 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5144
5145 if (it->method == next_element_from_buffer)
5146 {
5147 /* The current display element of IT is a character from
5148 current_buffer. Advance in the buffer, and maybe skip over
5149 invisible lines that are so because of selective display. */
5150 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5151 reseat_at_next_visible_line_start (it, 0);
5152 else
5153 {
5154 xassert (it->len != 0);
5155 IT_BYTEPOS (*it) += it->len;
5156 IT_CHARPOS (*it) += 1;
5157 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5158 }
5159 }
5160 else if (it->method == next_element_from_composition)
5161 {
5162 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
5163 if (STRINGP (it->string))
5164 {
5165 IT_STRING_BYTEPOS (*it) += it->len;
5166 IT_STRING_CHARPOS (*it) += it->cmp_len;
5167 it->method = next_element_from_string;
5168 goto consider_string_end;
5169 }
5170 else
5171 {
5172 IT_BYTEPOS (*it) += it->len;
5173 IT_CHARPOS (*it) += it->cmp_len;
5174 it->method = next_element_from_buffer;
5175 }
5176 }
5177 else if (it->method == next_element_from_c_string)
5178 {
5179 /* Current display element of IT is from a C string. */
5180 IT_BYTEPOS (*it) += it->len;
5181 IT_CHARPOS (*it) += 1;
5182 }
5183 else if (it->method == next_element_from_display_vector)
5184 {
5185 /* Current display element of IT is from a display table entry.
5186 Advance in the display table definition. Reset it to null if
5187 end reached, and continue with characters from buffers/
5188 strings. */
5189 ++it->current.dpvec_index;
5190
5191 /* Restore face of the iterator to what they were before the
5192 display vector entry (these entries may contain faces). */
5193 it->face_id = it->saved_face_id;
5194
5195 if (it->dpvec + it->current.dpvec_index == it->dpend)
5196 {
5197 if (it->s)
5198 it->method = next_element_from_c_string;
5199 else if (STRINGP (it->string))
5200 it->method = next_element_from_string;
5201 else
5202 it->method = next_element_from_buffer;
5203
5204 it->dpvec = NULL;
5205 it->current.dpvec_index = -1;
5206
5207 /* Skip over characters which were displayed via IT->dpvec. */
5208 if (it->dpvec_char_len < 0)
5209 reseat_at_next_visible_line_start (it, 1);
5210 else if (it->dpvec_char_len > 0)
5211 {
5212 it->len = it->dpvec_char_len;
5213 set_iterator_to_next (it, reseat_p);
5214 }
5215 }
5216 }
5217 else if (it->method == next_element_from_string)
5218 {
5219 /* Current display element is a character from a Lisp string. */
5220 xassert (it->s == NULL && STRINGP (it->string));
5221 IT_STRING_BYTEPOS (*it) += it->len;
5222 IT_STRING_CHARPOS (*it) += 1;
5223
5224 consider_string_end:
5225
5226 if (it->current.overlay_string_index >= 0)
5227 {
5228 /* IT->string is an overlay string. Advance to the
5229 next, if there is one. */
5230 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5231 next_overlay_string (it);
5232 }
5233 else
5234 {
5235 /* IT->string is not an overlay string. If we reached
5236 its end, and there is something on IT->stack, proceed
5237 with what is on the stack. This can be either another
5238 string, this time an overlay string, or a buffer. */
5239 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5240 && it->sp > 0)
5241 {
5242 pop_it (it);
5243 if (!STRINGP (it->string))
5244 it->method = next_element_from_buffer;
5245 else
5246 goto consider_string_end;
5247 }
5248 }
5249 }
5250 else if (it->method == next_element_from_image
5251 || it->method == next_element_from_stretch)
5252 {
5253 /* The position etc with which we have to proceed are on
5254 the stack. The position may be at the end of a string,
5255 if the `display' property takes up the whole string. */
5256 pop_it (it);
5257 it->image_id = 0;
5258 if (STRINGP (it->string))
5259 {
5260 it->method = next_element_from_string;
5261 goto consider_string_end;
5262 }
5263 else
5264 it->method = next_element_from_buffer;
5265 }
5266 else
5267 /* There are no other methods defined, so this should be a bug. */
5268 abort ();
5269
5270 xassert (it->method != next_element_from_string
5271 || (STRINGP (it->string)
5272 && IT_STRING_CHARPOS (*it) >= 0));
5273 }
5274
5275
5276 /* Load IT's display element fields with information about the next
5277 display element which comes from a display table entry or from the
5278 result of translating a control character to one of the forms `^C'
5279 or `\003'. IT->dpvec holds the glyphs to return as characters. */
5280
5281 static int
5282 next_element_from_display_vector (it)
5283 struct it *it;
5284 {
5285 /* Precondition. */
5286 xassert (it->dpvec && it->current.dpvec_index >= 0);
5287
5288 /* Remember the current face id in case glyphs specify faces.
5289 IT's face is restored in set_iterator_to_next. */
5290 it->saved_face_id = it->face_id;
5291
5292 if (INTEGERP (*it->dpvec)
5293 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5294 {
5295 int lface_id;
5296 GLYPH g;
5297
5298 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5299 it->c = FAST_GLYPH_CHAR (g);
5300 it->len = CHAR_BYTES (it->c);
5301
5302 /* The entry may contain a face id to use. Such a face id is
5303 the id of a Lisp face, not a realized face. A face id of
5304 zero means no face is specified. */
5305 lface_id = FAST_GLYPH_FACE (g);
5306 if (lface_id)
5307 {
5308 /* The function returns -1 if lface_id is invalid. */
5309 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
5310 if (face_id >= 0)
5311 it->face_id = face_id;
5312 }
5313 }
5314 else
5315 /* Display table entry is invalid. Return a space. */
5316 it->c = ' ', it->len = 1;
5317
5318 /* Don't change position and object of the iterator here. They are
5319 still the values of the character that had this display table
5320 entry or was translated, and that's what we want. */
5321 it->what = IT_CHARACTER;
5322 return 1;
5323 }
5324
5325
5326 /* Load IT with the next display element from Lisp string IT->string.
5327 IT->current.string_pos is the current position within the string.
5328 If IT->current.overlay_string_index >= 0, the Lisp string is an
5329 overlay string. */
5330
5331 static int
5332 next_element_from_string (it)
5333 struct it *it;
5334 {
5335 struct text_pos position;
5336
5337 xassert (STRINGP (it->string));
5338 xassert (IT_STRING_CHARPOS (*it) >= 0);
5339 position = it->current.string_pos;
5340
5341 /* Time to check for invisible text? */
5342 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5343 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5344 {
5345 handle_stop (it);
5346
5347 /* Since a handler may have changed IT->method, we must
5348 recurse here. */
5349 return get_next_display_element (it);
5350 }
5351
5352 if (it->current.overlay_string_index >= 0)
5353 {
5354 /* Get the next character from an overlay string. In overlay
5355 strings, There is no field width or padding with spaces to
5356 do. */
5357 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5358 {
5359 it->what = IT_EOB;
5360 return 0;
5361 }
5362 else if (STRING_MULTIBYTE (it->string))
5363 {
5364 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5365 const unsigned char *s = (SDATA (it->string)
5366 + IT_STRING_BYTEPOS (*it));
5367 it->c = string_char_and_length (s, remaining, &it->len);
5368 }
5369 else
5370 {
5371 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5372 it->len = 1;
5373 }
5374 }
5375 else
5376 {
5377 /* Get the next character from a Lisp string that is not an
5378 overlay string. Such strings come from the mode line, for
5379 example. We may have to pad with spaces, or truncate the
5380 string. See also next_element_from_c_string. */
5381 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5382 {
5383 it->what = IT_EOB;
5384 return 0;
5385 }
5386 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5387 {
5388 /* Pad with spaces. */
5389 it->c = ' ', it->len = 1;
5390 CHARPOS (position) = BYTEPOS (position) = -1;
5391 }
5392 else if (STRING_MULTIBYTE (it->string))
5393 {
5394 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5395 const unsigned char *s = (SDATA (it->string)
5396 + IT_STRING_BYTEPOS (*it));
5397 it->c = string_char_and_length (s, maxlen, &it->len);
5398 }
5399 else
5400 {
5401 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5402 it->len = 1;
5403 }
5404 }
5405
5406 /* Record what we have and where it came from. Note that we store a
5407 buffer position in IT->position although it could arguably be a
5408 string position. */
5409 it->what = IT_CHARACTER;
5410 it->object = it->string;
5411 it->position = position;
5412 return 1;
5413 }
5414
5415
5416 /* Load IT with next display element from C string IT->s.
5417 IT->string_nchars is the maximum number of characters to return
5418 from the string. IT->end_charpos may be greater than
5419 IT->string_nchars when this function is called, in which case we
5420 may have to return padding spaces. Value is zero if end of string
5421 reached, including padding spaces. */
5422
5423 static int
5424 next_element_from_c_string (it)
5425 struct it *it;
5426 {
5427 int success_p = 1;
5428
5429 xassert (it->s);
5430 it->what = IT_CHARACTER;
5431 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5432 it->object = Qnil;
5433
5434 /* IT's position can be greater IT->string_nchars in case a field
5435 width or precision has been specified when the iterator was
5436 initialized. */
5437 if (IT_CHARPOS (*it) >= it->end_charpos)
5438 {
5439 /* End of the game. */
5440 it->what = IT_EOB;
5441 success_p = 0;
5442 }
5443 else if (IT_CHARPOS (*it) >= it->string_nchars)
5444 {
5445 /* Pad with spaces. */
5446 it->c = ' ', it->len = 1;
5447 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5448 }
5449 else if (it->multibyte_p)
5450 {
5451 /* Implementation note: The calls to strlen apparently aren't a
5452 performance problem because there is no noticeable performance
5453 difference between Emacs running in unibyte or multibyte mode. */
5454 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5455 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5456 maxlen, &it->len);
5457 }
5458 else
5459 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5460
5461 return success_p;
5462 }
5463
5464
5465 /* Set up IT to return characters from an ellipsis, if appropriate.
5466 The definition of the ellipsis glyphs may come from a display table
5467 entry. This function Fills IT with the first glyph from the
5468 ellipsis if an ellipsis is to be displayed. */
5469
5470 static int
5471 next_element_from_ellipsis (it)
5472 struct it *it;
5473 {
5474 if (it->selective_display_ellipsis_p)
5475 {
5476 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
5477 {
5478 /* Use the display table definition for `...'. Invalid glyphs
5479 will be handled by the method returning elements from dpvec. */
5480 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
5481 it->dpvec_char_len = it->len;
5482 it->dpvec = v->contents;
5483 it->dpend = v->contents + v->size;
5484 it->current.dpvec_index = 0;
5485 it->method = next_element_from_display_vector;
5486 }
5487 else
5488 {
5489 /* Use default `...' which is stored in default_invis_vector. */
5490 it->dpvec_char_len = it->len;
5491 it->dpvec = default_invis_vector;
5492 it->dpend = default_invis_vector + 3;
5493 it->current.dpvec_index = 0;
5494 it->method = next_element_from_display_vector;
5495 }
5496 }
5497 else
5498 {
5499 /* The face at the current position may be different from the
5500 face we find after the invisible text. Remember what it
5501 was in IT->saved_face_id, and signal that it's there by
5502 setting face_before_selective_p. */
5503 it->saved_face_id = it->face_id;
5504 it->method = next_element_from_buffer;
5505 reseat_at_next_visible_line_start (it, 1);
5506 it->face_before_selective_p = 1;
5507 }
5508
5509 return get_next_display_element (it);
5510 }
5511
5512
5513 /* Deliver an image display element. The iterator IT is already
5514 filled with image information (done in handle_display_prop). Value
5515 is always 1. */
5516
5517
5518 static int
5519 next_element_from_image (it)
5520 struct it *it;
5521 {
5522 it->what = IT_IMAGE;
5523 return 1;
5524 }
5525
5526
5527 /* Fill iterator IT with next display element from a stretch glyph
5528 property. IT->object is the value of the text property. Value is
5529 always 1. */
5530
5531 static int
5532 next_element_from_stretch (it)
5533 struct it *it;
5534 {
5535 it->what = IT_STRETCH;
5536 return 1;
5537 }
5538
5539
5540 /* Load IT with the next display element from current_buffer. Value
5541 is zero if end of buffer reached. IT->stop_charpos is the next
5542 position at which to stop and check for text properties or buffer
5543 end. */
5544
5545 static int
5546 next_element_from_buffer (it)
5547 struct it *it;
5548 {
5549 int success_p = 1;
5550
5551 /* Check this assumption, otherwise, we would never enter the
5552 if-statement, below. */
5553 xassert (IT_CHARPOS (*it) >= BEGV
5554 && IT_CHARPOS (*it) <= it->stop_charpos);
5555
5556 if (IT_CHARPOS (*it) >= it->stop_charpos)
5557 {
5558 if (IT_CHARPOS (*it) >= it->end_charpos)
5559 {
5560 int overlay_strings_follow_p;
5561
5562 /* End of the game, except when overlay strings follow that
5563 haven't been returned yet. */
5564 if (it->overlay_strings_at_end_processed_p)
5565 overlay_strings_follow_p = 0;
5566 else
5567 {
5568 it->overlay_strings_at_end_processed_p = 1;
5569 overlay_strings_follow_p = get_overlay_strings (it, 0);
5570 }
5571
5572 if (overlay_strings_follow_p)
5573 success_p = get_next_display_element (it);
5574 else
5575 {
5576 it->what = IT_EOB;
5577 it->position = it->current.pos;
5578 success_p = 0;
5579 }
5580 }
5581 else
5582 {
5583 handle_stop (it);
5584 return get_next_display_element (it);
5585 }
5586 }
5587 else
5588 {
5589 /* No face changes, overlays etc. in sight, so just return a
5590 character from current_buffer. */
5591 unsigned char *p;
5592
5593 /* Maybe run the redisplay end trigger hook. Performance note:
5594 This doesn't seem to cost measurable time. */
5595 if (it->redisplay_end_trigger_charpos
5596 && it->glyph_row
5597 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5598 run_redisplay_end_trigger_hook (it);
5599
5600 /* Get the next character, maybe multibyte. */
5601 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5602 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5603 {
5604 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5605 - IT_BYTEPOS (*it));
5606 it->c = string_char_and_length (p, maxlen, &it->len);
5607 }
5608 else
5609 it->c = *p, it->len = 1;
5610
5611 /* Record what we have and where it came from. */
5612 it->what = IT_CHARACTER;;
5613 it->object = it->w->buffer;
5614 it->position = it->current.pos;
5615
5616 /* Normally we return the character found above, except when we
5617 really want to return an ellipsis for selective display. */
5618 if (it->selective)
5619 {
5620 if (it->c == '\n')
5621 {
5622 /* A value of selective > 0 means hide lines indented more
5623 than that number of columns. */
5624 if (it->selective > 0
5625 && IT_CHARPOS (*it) + 1 < ZV
5626 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5627 IT_BYTEPOS (*it) + 1,
5628 (double) it->selective)) /* iftc */
5629 {
5630 success_p = next_element_from_ellipsis (it);
5631 it->dpvec_char_len = -1;
5632 }
5633 }
5634 else if (it->c == '\r' && it->selective == -1)
5635 {
5636 /* A value of selective == -1 means that everything from the
5637 CR to the end of the line is invisible, with maybe an
5638 ellipsis displayed for it. */
5639 success_p = next_element_from_ellipsis (it);
5640 it->dpvec_char_len = -1;
5641 }
5642 }
5643 }
5644
5645 /* Value is zero if end of buffer reached. */
5646 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5647 return success_p;
5648 }
5649
5650
5651 /* Run the redisplay end trigger hook for IT. */
5652
5653 static void
5654 run_redisplay_end_trigger_hook (it)
5655 struct it *it;
5656 {
5657 Lisp_Object args[3];
5658
5659 /* IT->glyph_row should be non-null, i.e. we should be actually
5660 displaying something, or otherwise we should not run the hook. */
5661 xassert (it->glyph_row);
5662
5663 /* Set up hook arguments. */
5664 args[0] = Qredisplay_end_trigger_functions;
5665 args[1] = it->window;
5666 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5667 it->redisplay_end_trigger_charpos = 0;
5668
5669 /* Since we are *trying* to run these functions, don't try to run
5670 them again, even if they get an error. */
5671 it->w->redisplay_end_trigger = Qnil;
5672 Frun_hook_with_args (3, args);
5673
5674 /* Notice if it changed the face of the character we are on. */
5675 handle_face_prop (it);
5676 }
5677
5678
5679 /* Deliver a composition display element. The iterator IT is already
5680 filled with composition information (done in
5681 handle_composition_prop). Value is always 1. */
5682
5683 static int
5684 next_element_from_composition (it)
5685 struct it *it;
5686 {
5687 it->what = IT_COMPOSITION;
5688 it->position = (STRINGP (it->string)
5689 ? it->current.string_pos
5690 : it->current.pos);
5691 return 1;
5692 }
5693
5694
5695 \f
5696 /***********************************************************************
5697 Moving an iterator without producing glyphs
5698 ***********************************************************************/
5699
5700 /* Move iterator IT to a specified buffer or X position within one
5701 line on the display without producing glyphs.
5702
5703 OP should be a bit mask including some or all of these bits:
5704 MOVE_TO_X: Stop on reaching x-position TO_X.
5705 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5706 Regardless of OP's value, stop in reaching the end of the display line.
5707
5708 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5709 This means, in particular, that TO_X includes window's horizontal
5710 scroll amount.
5711
5712 The return value has several possible values that
5713 say what condition caused the scan to stop:
5714
5715 MOVE_POS_MATCH_OR_ZV
5716 - when TO_POS or ZV was reached.
5717
5718 MOVE_X_REACHED
5719 -when TO_X was reached before TO_POS or ZV were reached.
5720
5721 MOVE_LINE_CONTINUED
5722 - when we reached the end of the display area and the line must
5723 be continued.
5724
5725 MOVE_LINE_TRUNCATED
5726 - when we reached the end of the display area and the line is
5727 truncated.
5728
5729 MOVE_NEWLINE_OR_CR
5730 - when we stopped at a line end, i.e. a newline or a CR and selective
5731 display is on. */
5732
5733 static enum move_it_result
5734 move_it_in_display_line_to (it, to_charpos, to_x, op)
5735 struct it *it;
5736 int to_charpos, to_x, op;
5737 {
5738 enum move_it_result result = MOVE_UNDEFINED;
5739 struct glyph_row *saved_glyph_row;
5740
5741 /* Don't produce glyphs in produce_glyphs. */
5742 saved_glyph_row = it->glyph_row;
5743 it->glyph_row = NULL;
5744
5745 #define BUFFER_POS_REACHED_P() \
5746 ((op & MOVE_TO_POS) != 0 \
5747 && BUFFERP (it->object) \
5748 && IT_CHARPOS (*it) >= to_charpos)
5749
5750 while (1)
5751 {
5752 int x, i, ascent = 0, descent = 0;
5753
5754 /* Stop when ZV reached.
5755 We used to stop here when TO_CHARPOS reached as well, but that is
5756 too soon if this glyph does not fit on this line. So we handle it
5757 explicitly below. */
5758 if (!get_next_display_element (it)
5759 || (it->truncate_lines_p
5760 && BUFFER_POS_REACHED_P ()))
5761 {
5762 result = MOVE_POS_MATCH_OR_ZV;
5763 break;
5764 }
5765
5766 /* The call to produce_glyphs will get the metrics of the
5767 display element IT is loaded with. We record in x the
5768 x-position before this display element in case it does not
5769 fit on the line. */
5770 x = it->current_x;
5771
5772 /* Remember the line height so far in case the next element doesn't
5773 fit on the line. */
5774 if (!it->truncate_lines_p)
5775 {
5776 ascent = it->max_ascent;
5777 descent = it->max_descent;
5778 }
5779
5780 PRODUCE_GLYPHS (it);
5781
5782 if (it->area != TEXT_AREA)
5783 {
5784 set_iterator_to_next (it, 1);
5785 continue;
5786 }
5787
5788 /* The number of glyphs we get back in IT->nglyphs will normally
5789 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5790 character on a terminal frame, or (iii) a line end. For the
5791 second case, IT->nglyphs - 1 padding glyphs will be present
5792 (on X frames, there is only one glyph produced for a
5793 composite character.
5794
5795 The behavior implemented below means, for continuation lines,
5796 that as many spaces of a TAB as fit on the current line are
5797 displayed there. For terminal frames, as many glyphs of a
5798 multi-glyph character are displayed in the current line, too.
5799 This is what the old redisplay code did, and we keep it that
5800 way. Under X, the whole shape of a complex character must
5801 fit on the line or it will be completely displayed in the
5802 next line.
5803
5804 Note that both for tabs and padding glyphs, all glyphs have
5805 the same width. */
5806 if (it->nglyphs)
5807 {
5808 /* More than one glyph or glyph doesn't fit on line. All
5809 glyphs have the same width. */
5810 int single_glyph_width = it->pixel_width / it->nglyphs;
5811 int new_x;
5812
5813 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5814 {
5815 new_x = x + single_glyph_width;
5816
5817 /* We want to leave anything reaching TO_X to the caller. */
5818 if ((op & MOVE_TO_X) && new_x > to_x)
5819 {
5820 if (BUFFER_POS_REACHED_P ())
5821 goto buffer_pos_reached;
5822 it->current_x = x;
5823 result = MOVE_X_REACHED;
5824 break;
5825 }
5826 else if (/* Lines are continued. */
5827 !it->truncate_lines_p
5828 && (/* And glyph doesn't fit on the line. */
5829 new_x > it->last_visible_x
5830 /* Or it fits exactly and we're on a window
5831 system frame. */
5832 || (new_x == it->last_visible_x
5833 && FRAME_WINDOW_P (it->f))))
5834 {
5835 if (/* IT->hpos == 0 means the very first glyph
5836 doesn't fit on the line, e.g. a wide image. */
5837 it->hpos == 0
5838 || (new_x == it->last_visible_x
5839 && FRAME_WINDOW_P (it->f)))
5840 {
5841 ++it->hpos;
5842 it->current_x = new_x;
5843 if (i == it->nglyphs - 1)
5844 {
5845 set_iterator_to_next (it, 1);
5846 #ifdef HAVE_WINDOW_SYSTEM
5847 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5848 {
5849 if (!get_next_display_element (it))
5850 {
5851 result = MOVE_POS_MATCH_OR_ZV;
5852 break;
5853 }
5854 if (BUFFER_POS_REACHED_P ())
5855 {
5856 if (ITERATOR_AT_END_OF_LINE_P (it))
5857 result = MOVE_POS_MATCH_OR_ZV;
5858 else
5859 result = MOVE_LINE_CONTINUED;
5860 break;
5861 }
5862 if (ITERATOR_AT_END_OF_LINE_P (it))
5863 {
5864 result = MOVE_NEWLINE_OR_CR;
5865 break;
5866 }
5867 }
5868 #endif /* HAVE_WINDOW_SYSTEM */
5869 }
5870 }
5871 else
5872 {
5873 it->current_x = x;
5874 it->max_ascent = ascent;
5875 it->max_descent = descent;
5876 }
5877
5878 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5879 IT_CHARPOS (*it)));
5880 result = MOVE_LINE_CONTINUED;
5881 break;
5882 }
5883 else if (BUFFER_POS_REACHED_P ())
5884 goto buffer_pos_reached;
5885 else if (new_x > it->first_visible_x)
5886 {
5887 /* Glyph is visible. Increment number of glyphs that
5888 would be displayed. */
5889 ++it->hpos;
5890 }
5891 else
5892 {
5893 /* Glyph is completely off the left margin of the display
5894 area. Nothing to do. */
5895 }
5896 }
5897
5898 if (result != MOVE_UNDEFINED)
5899 break;
5900 }
5901 else if (BUFFER_POS_REACHED_P ())
5902 {
5903 buffer_pos_reached:
5904 it->current_x = x;
5905 it->max_ascent = ascent;
5906 it->max_descent = descent;
5907 result = MOVE_POS_MATCH_OR_ZV;
5908 break;
5909 }
5910 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5911 {
5912 /* Stop when TO_X specified and reached. This check is
5913 necessary here because of lines consisting of a line end,
5914 only. The line end will not produce any glyphs and we
5915 would never get MOVE_X_REACHED. */
5916 xassert (it->nglyphs == 0);
5917 result = MOVE_X_REACHED;
5918 break;
5919 }
5920
5921 /* Is this a line end? If yes, we're done. */
5922 if (ITERATOR_AT_END_OF_LINE_P (it))
5923 {
5924 result = MOVE_NEWLINE_OR_CR;
5925 break;
5926 }
5927
5928 /* The current display element has been consumed. Advance
5929 to the next. */
5930 set_iterator_to_next (it, 1);
5931
5932 /* Stop if lines are truncated and IT's current x-position is
5933 past the right edge of the window now. */
5934 if (it->truncate_lines_p
5935 && it->current_x >= it->last_visible_x)
5936 {
5937 #ifdef HAVE_WINDOW_SYSTEM
5938 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5939 {
5940 if (!get_next_display_element (it)
5941 || BUFFER_POS_REACHED_P ())
5942 {
5943 result = MOVE_POS_MATCH_OR_ZV;
5944 break;
5945 }
5946 if (ITERATOR_AT_END_OF_LINE_P (it))
5947 {
5948 result = MOVE_NEWLINE_OR_CR;
5949 break;
5950 }
5951 }
5952 #endif /* HAVE_WINDOW_SYSTEM */
5953 result = MOVE_LINE_TRUNCATED;
5954 break;
5955 }
5956 }
5957
5958 #undef BUFFER_POS_REACHED_P
5959
5960 /* Restore the iterator settings altered at the beginning of this
5961 function. */
5962 it->glyph_row = saved_glyph_row;
5963 return result;
5964 }
5965
5966
5967 /* Move IT forward until it satisfies one or more of the criteria in
5968 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5969
5970 OP is a bit-mask that specifies where to stop, and in particular,
5971 which of those four position arguments makes a difference. See the
5972 description of enum move_operation_enum.
5973
5974 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5975 screen line, this function will set IT to the next position >
5976 TO_CHARPOS. */
5977
5978 void
5979 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5980 struct it *it;
5981 int to_charpos, to_x, to_y, to_vpos;
5982 int op;
5983 {
5984 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5985 int line_height;
5986 int reached = 0;
5987
5988 for (;;)
5989 {
5990 if (op & MOVE_TO_VPOS)
5991 {
5992 /* If no TO_CHARPOS and no TO_X specified, stop at the
5993 start of the line TO_VPOS. */
5994 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5995 {
5996 if (it->vpos == to_vpos)
5997 {
5998 reached = 1;
5999 break;
6000 }
6001 else
6002 skip = move_it_in_display_line_to (it, -1, -1, 0);
6003 }
6004 else
6005 {
6006 /* TO_VPOS >= 0 means stop at TO_X in the line at
6007 TO_VPOS, or at TO_POS, whichever comes first. */
6008 if (it->vpos == to_vpos)
6009 {
6010 reached = 2;
6011 break;
6012 }
6013
6014 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6015
6016 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6017 {
6018 reached = 3;
6019 break;
6020 }
6021 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6022 {
6023 /* We have reached TO_X but not in the line we want. */
6024 skip = move_it_in_display_line_to (it, to_charpos,
6025 -1, MOVE_TO_POS);
6026 if (skip == MOVE_POS_MATCH_OR_ZV)
6027 {
6028 reached = 4;
6029 break;
6030 }
6031 }
6032 }
6033 }
6034 else if (op & MOVE_TO_Y)
6035 {
6036 struct it it_backup;
6037
6038 /* TO_Y specified means stop at TO_X in the line containing
6039 TO_Y---or at TO_CHARPOS if this is reached first. The
6040 problem is that we can't really tell whether the line
6041 contains TO_Y before we have completely scanned it, and
6042 this may skip past TO_X. What we do is to first scan to
6043 TO_X.
6044
6045 If TO_X is not specified, use a TO_X of zero. The reason
6046 is to make the outcome of this function more predictable.
6047 If we didn't use TO_X == 0, we would stop at the end of
6048 the line which is probably not what a caller would expect
6049 to happen. */
6050 skip = move_it_in_display_line_to (it, to_charpos,
6051 ((op & MOVE_TO_X)
6052 ? to_x : 0),
6053 (MOVE_TO_X
6054 | (op & MOVE_TO_POS)));
6055
6056 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6057 if (skip == MOVE_POS_MATCH_OR_ZV)
6058 {
6059 reached = 5;
6060 break;
6061 }
6062
6063 /* If TO_X was reached, we would like to know whether TO_Y
6064 is in the line. This can only be said if we know the
6065 total line height which requires us to scan the rest of
6066 the line. */
6067 if (skip == MOVE_X_REACHED)
6068 {
6069 /* Wait! We can conclude that TO_Y is in the line if
6070 the already scanned glyphs make the line tall enough
6071 because further scanning doesn't make it shorter. */
6072 line_height = it->max_ascent + it->max_descent;
6073 if (to_y >= it->current_y
6074 && to_y < it->current_y + line_height)
6075 {
6076 reached = 6;
6077 break;
6078 }
6079 it_backup = *it;
6080 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6081 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6082 op & MOVE_TO_POS);
6083 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6084 }
6085
6086 /* Now, decide whether TO_Y is in this line. */
6087 line_height = it->max_ascent + it->max_descent;
6088 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6089
6090 if (to_y >= it->current_y
6091 && to_y < it->current_y + line_height)
6092 {
6093 if (skip == MOVE_X_REACHED)
6094 /* If TO_Y is in this line and TO_X was reached above,
6095 we scanned too far. We have to restore IT's settings
6096 to the ones before skipping. */
6097 *it = it_backup;
6098 reached = 6;
6099 }
6100 else if (skip == MOVE_X_REACHED)
6101 {
6102 skip = skip2;
6103 if (skip == MOVE_POS_MATCH_OR_ZV)
6104 reached = 7;
6105 }
6106
6107 if (reached)
6108 break;
6109 }
6110 else
6111 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6112
6113 switch (skip)
6114 {
6115 case MOVE_POS_MATCH_OR_ZV:
6116 reached = 8;
6117 goto out;
6118
6119 case MOVE_NEWLINE_OR_CR:
6120 set_iterator_to_next (it, 1);
6121 it->continuation_lines_width = 0;
6122 break;
6123
6124 case MOVE_LINE_TRUNCATED:
6125 it->continuation_lines_width = 0;
6126 reseat_at_next_visible_line_start (it, 0);
6127 if ((op & MOVE_TO_POS) != 0
6128 && IT_CHARPOS (*it) > to_charpos)
6129 {
6130 reached = 9;
6131 goto out;
6132 }
6133 break;
6134
6135 case MOVE_LINE_CONTINUED:
6136 it->continuation_lines_width += it->current_x;
6137 break;
6138
6139 default:
6140 abort ();
6141 }
6142
6143 /* Reset/increment for the next run. */
6144 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6145 it->current_x = it->hpos = 0;
6146 it->current_y += it->max_ascent + it->max_descent;
6147 ++it->vpos;
6148 last_height = it->max_ascent + it->max_descent;
6149 last_max_ascent = it->max_ascent;
6150 it->max_ascent = it->max_descent = 0;
6151 }
6152
6153 out:
6154
6155 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6156 }
6157
6158
6159 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6160
6161 If DY > 0, move IT backward at least that many pixels. DY = 0
6162 means move IT backward to the preceding line start or BEGV. This
6163 function may move over more than DY pixels if IT->current_y - DY
6164 ends up in the middle of a line; in this case IT->current_y will be
6165 set to the top of the line moved to. */
6166
6167 void
6168 move_it_vertically_backward (it, dy)
6169 struct it *it;
6170 int dy;
6171 {
6172 int nlines, h;
6173 struct it it2, it3;
6174 int start_pos = IT_CHARPOS (*it);
6175
6176 xassert (dy >= 0);
6177
6178 /* Estimate how many newlines we must move back. */
6179 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6180
6181 /* Set the iterator's position that many lines back. */
6182 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6183 back_to_previous_visible_line_start (it);
6184
6185 /* Reseat the iterator here. When moving backward, we don't want
6186 reseat to skip forward over invisible text, set up the iterator
6187 to deliver from overlay strings at the new position etc. So,
6188 use reseat_1 here. */
6189 reseat_1 (it, it->current.pos, 1);
6190
6191 /* We are now surely at a line start. */
6192 it->current_x = it->hpos = 0;
6193 it->continuation_lines_width = 0;
6194
6195 /* Move forward and see what y-distance we moved. First move to the
6196 start of the next line so that we get its height. We need this
6197 height to be able to tell whether we reached the specified
6198 y-distance. */
6199 it2 = *it;
6200 it2.max_ascent = it2.max_descent = 0;
6201 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6202 MOVE_TO_POS | MOVE_TO_VPOS);
6203 xassert (IT_CHARPOS (*it) >= BEGV);
6204 it3 = it2;
6205
6206 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6207 xassert (IT_CHARPOS (*it) >= BEGV);
6208 /* H is the actual vertical distance from the position in *IT
6209 and the starting position. */
6210 h = it2.current_y - it->current_y;
6211 /* NLINES is the distance in number of lines. */
6212 nlines = it2.vpos - it->vpos;
6213
6214 /* Correct IT's y and vpos position
6215 so that they are relative to the starting point. */
6216 it->vpos -= nlines;
6217 it->current_y -= h;
6218
6219 if (dy == 0)
6220 {
6221 /* DY == 0 means move to the start of the screen line. The
6222 value of nlines is > 0 if continuation lines were involved. */
6223 if (nlines > 0)
6224 move_it_by_lines (it, nlines, 1);
6225 xassert (IT_CHARPOS (*it) <= start_pos);
6226 }
6227 else
6228 {
6229 /* The y-position we try to reach, relative to *IT.
6230 Note that H has been subtracted in front of the if-statement. */
6231 int target_y = it->current_y + h - dy;
6232 int y0 = it3.current_y;
6233 int y1 = line_bottom_y (&it3);
6234 int line_height = y1 - y0;
6235
6236 /* If we did not reach target_y, try to move further backward if
6237 we can. If we moved too far backward, try to move forward. */
6238 if (target_y < it->current_y
6239 /* This is heuristic. In a window that's 3 lines high, with
6240 a line height of 13 pixels each, recentering with point
6241 on the bottom line will try to move -39/2 = 19 pixels
6242 backward. Try to avoid moving into the first line. */
6243 && it->current_y - target_y > line_height / 3 * 2
6244 && IT_CHARPOS (*it) > BEGV)
6245 {
6246 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6247 target_y - it->current_y));
6248 move_it_vertically (it, target_y - it->current_y);
6249 xassert (IT_CHARPOS (*it) >= BEGV);
6250 }
6251 else if (target_y >= it->current_y + line_height
6252 && IT_CHARPOS (*it) < ZV)
6253 {
6254 /* Should move forward by at least one line, maybe more.
6255
6256 Note: Calling move_it_by_lines can be expensive on
6257 terminal frames, where compute_motion is used (via
6258 vmotion) to do the job, when there are very long lines
6259 and truncate-lines is nil. That's the reason for
6260 treating terminal frames specially here. */
6261
6262 if (!FRAME_WINDOW_P (it->f))
6263 move_it_vertically (it, target_y - (it->current_y + line_height));
6264 else
6265 {
6266 do
6267 {
6268 move_it_by_lines (it, 1, 1);
6269 }
6270 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6271 }
6272
6273 xassert (IT_CHARPOS (*it) >= BEGV);
6274 }
6275 }
6276 }
6277
6278
6279 /* Move IT by a specified amount of pixel lines DY. DY negative means
6280 move backwards. DY = 0 means move to start of screen line. At the
6281 end, IT will be on the start of a screen line. */
6282
6283 void
6284 move_it_vertically (it, dy)
6285 struct it *it;
6286 int dy;
6287 {
6288 if (dy <= 0)
6289 move_it_vertically_backward (it, -dy);
6290 else if (dy > 0)
6291 {
6292 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6293 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6294 MOVE_TO_POS | MOVE_TO_Y);
6295 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6296
6297 /* If buffer ends in ZV without a newline, move to the start of
6298 the line to satisfy the post-condition. */
6299 if (IT_CHARPOS (*it) == ZV
6300 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6301 move_it_by_lines (it, 0, 0);
6302 }
6303 }
6304
6305
6306 /* Move iterator IT past the end of the text line it is in. */
6307
6308 void
6309 move_it_past_eol (it)
6310 struct it *it;
6311 {
6312 enum move_it_result rc;
6313
6314 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6315 if (rc == MOVE_NEWLINE_OR_CR)
6316 set_iterator_to_next (it, 0);
6317 }
6318
6319
6320 #if 0 /* Currently not used. */
6321
6322 /* Return non-zero if some text between buffer positions START_CHARPOS
6323 and END_CHARPOS is invisible. IT->window is the window for text
6324 property lookup. */
6325
6326 static int
6327 invisible_text_between_p (it, start_charpos, end_charpos)
6328 struct it *it;
6329 int start_charpos, end_charpos;
6330 {
6331 Lisp_Object prop, limit;
6332 int invisible_found_p;
6333
6334 xassert (it != NULL && start_charpos <= end_charpos);
6335
6336 /* Is text at START invisible? */
6337 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6338 it->window);
6339 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6340 invisible_found_p = 1;
6341 else
6342 {
6343 limit = Fnext_single_char_property_change (make_number (start_charpos),
6344 Qinvisible, Qnil,
6345 make_number (end_charpos));
6346 invisible_found_p = XFASTINT (limit) < end_charpos;
6347 }
6348
6349 return invisible_found_p;
6350 }
6351
6352 #endif /* 0 */
6353
6354
6355 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6356 negative means move up. DVPOS == 0 means move to the start of the
6357 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6358 NEED_Y_P is zero, IT->current_y will be left unchanged.
6359
6360 Further optimization ideas: If we would know that IT->f doesn't use
6361 a face with proportional font, we could be faster for
6362 truncate-lines nil. */
6363
6364 void
6365 move_it_by_lines (it, dvpos, need_y_p)
6366 struct it *it;
6367 int dvpos, need_y_p;
6368 {
6369 struct position pos;
6370
6371 if (!FRAME_WINDOW_P (it->f))
6372 {
6373 struct text_pos textpos;
6374
6375 /* We can use vmotion on frames without proportional fonts. */
6376 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6377 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6378 reseat (it, textpos, 1);
6379 it->vpos += pos.vpos;
6380 it->current_y += pos.vpos;
6381 }
6382 else if (dvpos == 0)
6383 {
6384 /* DVPOS == 0 means move to the start of the screen line. */
6385 move_it_vertically_backward (it, 0);
6386 xassert (it->current_x == 0 && it->hpos == 0);
6387 }
6388 else if (dvpos > 0)
6389 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6390 else
6391 {
6392 struct it it2;
6393 int start_charpos, i;
6394
6395 /* Start at the beginning of the screen line containing IT's
6396 position. */
6397 move_it_vertically_backward (it, 0);
6398
6399 /* Go back -DVPOS visible lines and reseat the iterator there. */
6400 start_charpos = IT_CHARPOS (*it);
6401 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6402 back_to_previous_visible_line_start (it);
6403 reseat (it, it->current.pos, 1);
6404 it->current_x = it->hpos = 0;
6405
6406 /* Above call may have moved too far if continuation lines
6407 are involved. Scan forward and see if it did. */
6408 it2 = *it;
6409 it2.vpos = it2.current_y = 0;
6410 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6411 it->vpos -= it2.vpos;
6412 it->current_y -= it2.current_y;
6413 it->current_x = it->hpos = 0;
6414
6415 /* If we moved too far, move IT some lines forward. */
6416 if (it2.vpos > -dvpos)
6417 {
6418 int delta = it2.vpos + dvpos;
6419 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6420 }
6421 }
6422 }
6423
6424 /* Return 1 if IT points into the middle of a display vector. */
6425
6426 int
6427 in_display_vector_p (it)
6428 struct it *it;
6429 {
6430 return (it->method == next_element_from_display_vector
6431 && it->current.dpvec_index > 0
6432 && it->dpvec + it->current.dpvec_index != it->dpend);
6433 }
6434
6435 \f
6436 /***********************************************************************
6437 Messages
6438 ***********************************************************************/
6439
6440
6441 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6442 to *Messages*. */
6443
6444 void
6445 add_to_log (format, arg1, arg2)
6446 char *format;
6447 Lisp_Object arg1, arg2;
6448 {
6449 Lisp_Object args[3];
6450 Lisp_Object msg, fmt;
6451 char *buffer;
6452 int len;
6453 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6454 USE_SAFE_ALLOCA;
6455
6456 /* Do nothing if called asynchronously. Inserting text into
6457 a buffer may call after-change-functions and alike and
6458 that would means running Lisp asynchronously. */
6459 if (handling_signal)
6460 return;
6461
6462 fmt = msg = Qnil;
6463 GCPRO4 (fmt, msg, arg1, arg2);
6464
6465 args[0] = fmt = build_string (format);
6466 args[1] = arg1;
6467 args[2] = arg2;
6468 msg = Fformat (3, args);
6469
6470 len = SBYTES (msg) + 1;
6471 SAFE_ALLOCA (buffer, char *, len);
6472 bcopy (SDATA (msg), buffer, len);
6473
6474 message_dolog (buffer, len - 1, 1, 0);
6475 SAFE_FREE (len);
6476
6477 UNGCPRO;
6478 }
6479
6480
6481 /* Output a newline in the *Messages* buffer if "needs" one. */
6482
6483 void
6484 message_log_maybe_newline ()
6485 {
6486 if (message_log_need_newline)
6487 message_dolog ("", 0, 1, 0);
6488 }
6489
6490
6491 /* Add a string M of length NBYTES to the message log, optionally
6492 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6493 nonzero, means interpret the contents of M as multibyte. This
6494 function calls low-level routines in order to bypass text property
6495 hooks, etc. which might not be safe to run. */
6496
6497 void
6498 message_dolog (m, nbytes, nlflag, multibyte)
6499 const char *m;
6500 int nbytes, nlflag, multibyte;
6501 {
6502 if (!NILP (Vmemory_full))
6503 return;
6504
6505 if (!NILP (Vmessage_log_max))
6506 {
6507 struct buffer *oldbuf;
6508 Lisp_Object oldpoint, oldbegv, oldzv;
6509 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6510 int point_at_end = 0;
6511 int zv_at_end = 0;
6512 Lisp_Object old_deactivate_mark, tem;
6513 struct gcpro gcpro1;
6514
6515 old_deactivate_mark = Vdeactivate_mark;
6516 oldbuf = current_buffer;
6517 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6518 current_buffer->undo_list = Qt;
6519
6520 oldpoint = message_dolog_marker1;
6521 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6522 oldbegv = message_dolog_marker2;
6523 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6524 oldzv = message_dolog_marker3;
6525 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6526 GCPRO1 (old_deactivate_mark);
6527
6528 if (PT == Z)
6529 point_at_end = 1;
6530 if (ZV == Z)
6531 zv_at_end = 1;
6532
6533 BEGV = BEG;
6534 BEGV_BYTE = BEG_BYTE;
6535 ZV = Z;
6536 ZV_BYTE = Z_BYTE;
6537 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6538
6539 /* Insert the string--maybe converting multibyte to single byte
6540 or vice versa, so that all the text fits the buffer. */
6541 if (multibyte
6542 && NILP (current_buffer->enable_multibyte_characters))
6543 {
6544 int i, c, char_bytes;
6545 unsigned char work[1];
6546
6547 /* Convert a multibyte string to single-byte
6548 for the *Message* buffer. */
6549 for (i = 0; i < nbytes; i += char_bytes)
6550 {
6551 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6552 work[0] = (ASCII_CHAR_P (c)
6553 ? c
6554 : multibyte_char_to_unibyte (c, Qnil));
6555 insert_1_both (work, 1, 1, 1, 0, 0);
6556 }
6557 }
6558 else if (! multibyte
6559 && ! NILP (current_buffer->enable_multibyte_characters))
6560 {
6561 int i, c, char_bytes;
6562 unsigned char *msg = (unsigned char *) m;
6563 unsigned char str[MAX_MULTIBYTE_LENGTH];
6564 /* Convert a single-byte string to multibyte
6565 for the *Message* buffer. */
6566 for (i = 0; i < nbytes; i++)
6567 {
6568 c = unibyte_char_to_multibyte (msg[i]);
6569 char_bytes = CHAR_STRING (c, str);
6570 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6571 }
6572 }
6573 else if (nbytes)
6574 insert_1 (m, nbytes, 1, 0, 0);
6575
6576 if (nlflag)
6577 {
6578 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6579 insert_1 ("\n", 1, 1, 0, 0);
6580
6581 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6582 this_bol = PT;
6583 this_bol_byte = PT_BYTE;
6584
6585 /* See if this line duplicates the previous one.
6586 If so, combine duplicates. */
6587 if (this_bol > BEG)
6588 {
6589 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6590 prev_bol = PT;
6591 prev_bol_byte = PT_BYTE;
6592
6593 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6594 this_bol, this_bol_byte);
6595 if (dup)
6596 {
6597 del_range_both (prev_bol, prev_bol_byte,
6598 this_bol, this_bol_byte, 0);
6599 if (dup > 1)
6600 {
6601 char dupstr[40];
6602 int duplen;
6603
6604 /* If you change this format, don't forget to also
6605 change message_log_check_duplicate. */
6606 sprintf (dupstr, " [%d times]", dup);
6607 duplen = strlen (dupstr);
6608 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6609 insert_1 (dupstr, duplen, 1, 0, 1);
6610 }
6611 }
6612 }
6613
6614 /* If we have more than the desired maximum number of lines
6615 in the *Messages* buffer now, delete the oldest ones.
6616 This is safe because we don't have undo in this buffer. */
6617
6618 if (NATNUMP (Vmessage_log_max))
6619 {
6620 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6621 -XFASTINT (Vmessage_log_max) - 1, 0);
6622 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6623 }
6624 }
6625 BEGV = XMARKER (oldbegv)->charpos;
6626 BEGV_BYTE = marker_byte_position (oldbegv);
6627
6628 if (zv_at_end)
6629 {
6630 ZV = Z;
6631 ZV_BYTE = Z_BYTE;
6632 }
6633 else
6634 {
6635 ZV = XMARKER (oldzv)->charpos;
6636 ZV_BYTE = marker_byte_position (oldzv);
6637 }
6638
6639 if (point_at_end)
6640 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6641 else
6642 /* We can't do Fgoto_char (oldpoint) because it will run some
6643 Lisp code. */
6644 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6645 XMARKER (oldpoint)->bytepos);
6646
6647 UNGCPRO;
6648 unchain_marker (XMARKER (oldpoint));
6649 unchain_marker (XMARKER (oldbegv));
6650 unchain_marker (XMARKER (oldzv));
6651
6652 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6653 set_buffer_internal (oldbuf);
6654 if (NILP (tem))
6655 windows_or_buffers_changed = old_windows_or_buffers_changed;
6656 message_log_need_newline = !nlflag;
6657 Vdeactivate_mark = old_deactivate_mark;
6658 }
6659 }
6660
6661
6662 /* We are at the end of the buffer after just having inserted a newline.
6663 (Note: We depend on the fact we won't be crossing the gap.)
6664 Check to see if the most recent message looks a lot like the previous one.
6665 Return 0 if different, 1 if the new one should just replace it, or a
6666 value N > 1 if we should also append " [N times]". */
6667
6668 static int
6669 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6670 int prev_bol, this_bol;
6671 int prev_bol_byte, this_bol_byte;
6672 {
6673 int i;
6674 int len = Z_BYTE - 1 - this_bol_byte;
6675 int seen_dots = 0;
6676 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6677 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6678
6679 for (i = 0; i < len; i++)
6680 {
6681 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6682 seen_dots = 1;
6683 if (p1[i] != p2[i])
6684 return seen_dots;
6685 }
6686 p1 += len;
6687 if (*p1 == '\n')
6688 return 2;
6689 if (*p1++ == ' ' && *p1++ == '[')
6690 {
6691 int n = 0;
6692 while (*p1 >= '0' && *p1 <= '9')
6693 n = n * 10 + *p1++ - '0';
6694 if (strncmp (p1, " times]\n", 8) == 0)
6695 return n+1;
6696 }
6697 return 0;
6698 }
6699
6700
6701 /* Display an echo area message M with a specified length of NBYTES
6702 bytes. The string may include null characters. If M is 0, clear
6703 out any existing message, and let the mini-buffer text show
6704 through.
6705
6706 The buffer M must continue to exist until after the echo area gets
6707 cleared or some other message gets displayed there. This means do
6708 not pass text that is stored in a Lisp string; do not pass text in
6709 a buffer that was alloca'd. */
6710
6711 void
6712 message2 (m, nbytes, multibyte)
6713 const char *m;
6714 int nbytes;
6715 int multibyte;
6716 {
6717 /* First flush out any partial line written with print. */
6718 message_log_maybe_newline ();
6719 if (m)
6720 message_dolog (m, nbytes, 1, multibyte);
6721 message2_nolog (m, nbytes, multibyte);
6722 }
6723
6724
6725 /* The non-logging counterpart of message2. */
6726
6727 void
6728 message2_nolog (m, nbytes, multibyte)
6729 const char *m;
6730 int nbytes, multibyte;
6731 {
6732 struct frame *sf = SELECTED_FRAME ();
6733 message_enable_multibyte = multibyte;
6734
6735 if (noninteractive)
6736 {
6737 if (noninteractive_need_newline)
6738 putc ('\n', stderr);
6739 noninteractive_need_newline = 0;
6740 if (m)
6741 fwrite (m, nbytes, 1, stderr);
6742 if (cursor_in_echo_area == 0)
6743 fprintf (stderr, "\n");
6744 fflush (stderr);
6745 }
6746 /* A null message buffer means that the frame hasn't really been
6747 initialized yet. Error messages get reported properly by
6748 cmd_error, so this must be just an informative message; toss it. */
6749 else if (INTERACTIVE
6750 && sf->glyphs_initialized_p
6751 && FRAME_MESSAGE_BUF (sf))
6752 {
6753 Lisp_Object mini_window;
6754 struct frame *f;
6755
6756 /* Get the frame containing the mini-buffer
6757 that the selected frame is using. */
6758 mini_window = FRAME_MINIBUF_WINDOW (sf);
6759 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6760
6761 FRAME_SAMPLE_VISIBILITY (f);
6762 if (FRAME_VISIBLE_P (sf)
6763 && ! FRAME_VISIBLE_P (f))
6764 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6765
6766 if (m)
6767 {
6768 set_message (m, Qnil, nbytes, multibyte);
6769 if (minibuffer_auto_raise)
6770 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6771 }
6772 else
6773 clear_message (1, 1);
6774
6775 do_pending_window_change (0);
6776 echo_area_display (1);
6777 do_pending_window_change (0);
6778 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6779 (*frame_up_to_date_hook) (f);
6780 }
6781 }
6782
6783
6784 /* Display an echo area message M with a specified length of NBYTES
6785 bytes. The string may include null characters. If M is not a
6786 string, clear out any existing message, and let the mini-buffer
6787 text show through. */
6788
6789 void
6790 message3 (m, nbytes, multibyte)
6791 Lisp_Object m;
6792 int nbytes;
6793 int multibyte;
6794 {
6795 struct gcpro gcpro1;
6796
6797 GCPRO1 (m);
6798
6799 /* First flush out any partial line written with print. */
6800 message_log_maybe_newline ();
6801 if (STRINGP (m))
6802 message_dolog (SDATA (m), nbytes, 1, multibyte);
6803 message3_nolog (m, nbytes, multibyte);
6804
6805 UNGCPRO;
6806 }
6807
6808
6809 /* The non-logging version of message3. */
6810
6811 void
6812 message3_nolog (m, nbytes, multibyte)
6813 Lisp_Object m;
6814 int nbytes, multibyte;
6815 {
6816 struct frame *sf = SELECTED_FRAME ();
6817 message_enable_multibyte = multibyte;
6818
6819 if (noninteractive)
6820 {
6821 if (noninteractive_need_newline)
6822 putc ('\n', stderr);
6823 noninteractive_need_newline = 0;
6824 if (STRINGP (m))
6825 fwrite (SDATA (m), nbytes, 1, stderr);
6826 if (cursor_in_echo_area == 0)
6827 fprintf (stderr, "\n");
6828 fflush (stderr);
6829 }
6830 /* A null message buffer means that the frame hasn't really been
6831 initialized yet. Error messages get reported properly by
6832 cmd_error, so this must be just an informative message; toss it. */
6833 else if (INTERACTIVE
6834 && sf->glyphs_initialized_p
6835 && FRAME_MESSAGE_BUF (sf))
6836 {
6837 Lisp_Object mini_window;
6838 Lisp_Object frame;
6839 struct frame *f;
6840
6841 /* Get the frame containing the mini-buffer
6842 that the selected frame is using. */
6843 mini_window = FRAME_MINIBUF_WINDOW (sf);
6844 frame = XWINDOW (mini_window)->frame;
6845 f = XFRAME (frame);
6846
6847 FRAME_SAMPLE_VISIBILITY (f);
6848 if (FRAME_VISIBLE_P (sf)
6849 && !FRAME_VISIBLE_P (f))
6850 Fmake_frame_visible (frame);
6851
6852 if (STRINGP (m) && SCHARS (m) > 0)
6853 {
6854 set_message (NULL, m, nbytes, multibyte);
6855 if (minibuffer_auto_raise)
6856 Fraise_frame (frame);
6857 }
6858 else
6859 clear_message (1, 1);
6860
6861 do_pending_window_change (0);
6862 echo_area_display (1);
6863 do_pending_window_change (0);
6864 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6865 (*frame_up_to_date_hook) (f);
6866 }
6867 }
6868
6869
6870 /* Display a null-terminated echo area message M. If M is 0, clear
6871 out any existing message, and let the mini-buffer text show through.
6872
6873 The buffer M must continue to exist until after the echo area gets
6874 cleared or some other message gets displayed there. Do not pass
6875 text that is stored in a Lisp string. Do not pass text in a buffer
6876 that was alloca'd. */
6877
6878 void
6879 message1 (m)
6880 char *m;
6881 {
6882 message2 (m, (m ? strlen (m) : 0), 0);
6883 }
6884
6885
6886 /* The non-logging counterpart of message1. */
6887
6888 void
6889 message1_nolog (m)
6890 char *m;
6891 {
6892 message2_nolog (m, (m ? strlen (m) : 0), 0);
6893 }
6894
6895 /* Display a message M which contains a single %s
6896 which gets replaced with STRING. */
6897
6898 void
6899 message_with_string (m, string, log)
6900 char *m;
6901 Lisp_Object string;
6902 int log;
6903 {
6904 CHECK_STRING (string);
6905
6906 if (noninteractive)
6907 {
6908 if (m)
6909 {
6910 if (noninteractive_need_newline)
6911 putc ('\n', stderr);
6912 noninteractive_need_newline = 0;
6913 fprintf (stderr, m, SDATA (string));
6914 if (cursor_in_echo_area == 0)
6915 fprintf (stderr, "\n");
6916 fflush (stderr);
6917 }
6918 }
6919 else if (INTERACTIVE)
6920 {
6921 /* The frame whose minibuffer we're going to display the message on.
6922 It may be larger than the selected frame, so we need
6923 to use its buffer, not the selected frame's buffer. */
6924 Lisp_Object mini_window;
6925 struct frame *f, *sf = SELECTED_FRAME ();
6926
6927 /* Get the frame containing the minibuffer
6928 that the selected frame is using. */
6929 mini_window = FRAME_MINIBUF_WINDOW (sf);
6930 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6931
6932 /* A null message buffer means that the frame hasn't really been
6933 initialized yet. Error messages get reported properly by
6934 cmd_error, so this must be just an informative message; toss it. */
6935 if (FRAME_MESSAGE_BUF (f))
6936 {
6937 Lisp_Object args[2], message;
6938 struct gcpro gcpro1, gcpro2;
6939
6940 args[0] = build_string (m);
6941 args[1] = message = string;
6942 GCPRO2 (args[0], message);
6943 gcpro1.nvars = 2;
6944
6945 message = Fformat (2, args);
6946
6947 if (log)
6948 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6949 else
6950 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6951
6952 UNGCPRO;
6953
6954 /* Print should start at the beginning of the message
6955 buffer next time. */
6956 message_buf_print = 0;
6957 }
6958 }
6959 }
6960
6961
6962 /* Dump an informative message to the minibuf. If M is 0, clear out
6963 any existing message, and let the mini-buffer text show through. */
6964
6965 /* VARARGS 1 */
6966 void
6967 message (m, a1, a2, a3)
6968 char *m;
6969 EMACS_INT a1, a2, a3;
6970 {
6971 if (noninteractive)
6972 {
6973 if (m)
6974 {
6975 if (noninteractive_need_newline)
6976 putc ('\n', stderr);
6977 noninteractive_need_newline = 0;
6978 fprintf (stderr, m, a1, a2, a3);
6979 if (cursor_in_echo_area == 0)
6980 fprintf (stderr, "\n");
6981 fflush (stderr);
6982 }
6983 }
6984 else if (INTERACTIVE)
6985 {
6986 /* The frame whose mini-buffer we're going to display the message
6987 on. It may be larger than the selected frame, so we need to
6988 use its buffer, not the selected frame's buffer. */
6989 Lisp_Object mini_window;
6990 struct frame *f, *sf = SELECTED_FRAME ();
6991
6992 /* Get the frame containing the mini-buffer
6993 that the selected frame is using. */
6994 mini_window = FRAME_MINIBUF_WINDOW (sf);
6995 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6996
6997 /* A null message buffer means that the frame hasn't really been
6998 initialized yet. Error messages get reported properly by
6999 cmd_error, so this must be just an informative message; toss
7000 it. */
7001 if (FRAME_MESSAGE_BUF (f))
7002 {
7003 if (m)
7004 {
7005 int len;
7006 #ifdef NO_ARG_ARRAY
7007 char *a[3];
7008 a[0] = (char *) a1;
7009 a[1] = (char *) a2;
7010 a[2] = (char *) a3;
7011
7012 len = doprnt (FRAME_MESSAGE_BUF (f),
7013 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7014 #else
7015 len = doprnt (FRAME_MESSAGE_BUF (f),
7016 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7017 (char **) &a1);
7018 #endif /* NO_ARG_ARRAY */
7019
7020 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7021 }
7022 else
7023 message1 (0);
7024
7025 /* Print should start at the beginning of the message
7026 buffer next time. */
7027 message_buf_print = 0;
7028 }
7029 }
7030 }
7031
7032
7033 /* The non-logging version of message. */
7034
7035 void
7036 message_nolog (m, a1, a2, a3)
7037 char *m;
7038 EMACS_INT a1, a2, a3;
7039 {
7040 Lisp_Object old_log_max;
7041 old_log_max = Vmessage_log_max;
7042 Vmessage_log_max = Qnil;
7043 message (m, a1, a2, a3);
7044 Vmessage_log_max = old_log_max;
7045 }
7046
7047
7048 /* Display the current message in the current mini-buffer. This is
7049 only called from error handlers in process.c, and is not time
7050 critical. */
7051
7052 void
7053 update_echo_area ()
7054 {
7055 if (!NILP (echo_area_buffer[0]))
7056 {
7057 Lisp_Object string;
7058 string = Fcurrent_message ();
7059 message3 (string, SBYTES (string),
7060 !NILP (current_buffer->enable_multibyte_characters));
7061 }
7062 }
7063
7064
7065 /* Make sure echo area buffers in `echo_buffers' are live.
7066 If they aren't, make new ones. */
7067
7068 static void
7069 ensure_echo_area_buffers ()
7070 {
7071 int i;
7072
7073 for (i = 0; i < 2; ++i)
7074 if (!BUFFERP (echo_buffer[i])
7075 || NILP (XBUFFER (echo_buffer[i])->name))
7076 {
7077 char name[30];
7078 Lisp_Object old_buffer;
7079 int j;
7080
7081 old_buffer = echo_buffer[i];
7082 sprintf (name, " *Echo Area %d*", i);
7083 echo_buffer[i] = Fget_buffer_create (build_string (name));
7084 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7085
7086 for (j = 0; j < 2; ++j)
7087 if (EQ (old_buffer, echo_area_buffer[j]))
7088 echo_area_buffer[j] = echo_buffer[i];
7089 }
7090 }
7091
7092
7093 /* Call FN with args A1..A4 with either the current or last displayed
7094 echo_area_buffer as current buffer.
7095
7096 WHICH zero means use the current message buffer
7097 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7098 from echo_buffer[] and clear it.
7099
7100 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7101 suitable buffer from echo_buffer[] and clear it.
7102
7103 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
7104 that the current message becomes the last displayed one, make
7105 choose a suitable buffer for echo_area_buffer[0], and clear it.
7106
7107 Value is what FN returns. */
7108
7109 static int
7110 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7111 struct window *w;
7112 int which;
7113 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7114 EMACS_INT a1;
7115 Lisp_Object a2;
7116 EMACS_INT a3, a4;
7117 {
7118 Lisp_Object buffer;
7119 int this_one, the_other, clear_buffer_p, rc;
7120 int count = SPECPDL_INDEX ();
7121
7122 /* If buffers aren't live, make new ones. */
7123 ensure_echo_area_buffers ();
7124
7125 clear_buffer_p = 0;
7126
7127 if (which == 0)
7128 this_one = 0, the_other = 1;
7129 else if (which > 0)
7130 this_one = 1, the_other = 0;
7131 else
7132 {
7133 this_one = 0, the_other = 1;
7134 clear_buffer_p = 1;
7135
7136 /* We need a fresh one in case the current echo buffer equals
7137 the one containing the last displayed echo area message. */
7138 if (!NILP (echo_area_buffer[this_one])
7139 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
7140 echo_area_buffer[this_one] = Qnil;
7141 }
7142
7143 /* Choose a suitable buffer from echo_buffer[] is we don't
7144 have one. */
7145 if (NILP (echo_area_buffer[this_one]))
7146 {
7147 echo_area_buffer[this_one]
7148 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7149 ? echo_buffer[the_other]
7150 : echo_buffer[this_one]);
7151 clear_buffer_p = 1;
7152 }
7153
7154 buffer = echo_area_buffer[this_one];
7155
7156 /* Don't get confused by reusing the buffer used for echoing
7157 for a different purpose. */
7158 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7159 cancel_echoing ();
7160
7161 record_unwind_protect (unwind_with_echo_area_buffer,
7162 with_echo_area_buffer_unwind_data (w));
7163
7164 /* Make the echo area buffer current. Note that for display
7165 purposes, it is not necessary that the displayed window's buffer
7166 == current_buffer, except for text property lookup. So, let's
7167 only set that buffer temporarily here without doing a full
7168 Fset_window_buffer. We must also change w->pointm, though,
7169 because otherwise an assertions in unshow_buffer fails, and Emacs
7170 aborts. */
7171 set_buffer_internal_1 (XBUFFER (buffer));
7172 if (w)
7173 {
7174 w->buffer = buffer;
7175 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7176 }
7177
7178 current_buffer->undo_list = Qt;
7179 current_buffer->read_only = Qnil;
7180 specbind (Qinhibit_read_only, Qt);
7181 specbind (Qinhibit_modification_hooks, Qt);
7182
7183 if (clear_buffer_p && Z > BEG)
7184 del_range (BEG, Z);
7185
7186 xassert (BEGV >= BEG);
7187 xassert (ZV <= Z && ZV >= BEGV);
7188
7189 rc = fn (a1, a2, a3, a4);
7190
7191 xassert (BEGV >= BEG);
7192 xassert (ZV <= Z && ZV >= BEGV);
7193
7194 unbind_to (count, Qnil);
7195 return rc;
7196 }
7197
7198
7199 /* Save state that should be preserved around the call to the function
7200 FN called in with_echo_area_buffer. */
7201
7202 static Lisp_Object
7203 with_echo_area_buffer_unwind_data (w)
7204 struct window *w;
7205 {
7206 int i = 0;
7207 Lisp_Object vector;
7208
7209 /* Reduce consing by keeping one vector in
7210 Vwith_echo_area_save_vector. */
7211 vector = Vwith_echo_area_save_vector;
7212 Vwith_echo_area_save_vector = Qnil;
7213
7214 if (NILP (vector))
7215 vector = Fmake_vector (make_number (7), Qnil);
7216
7217 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7218 AREF (vector, i) = Vdeactivate_mark, ++i;
7219 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7220
7221 if (w)
7222 {
7223 XSETWINDOW (AREF (vector, i), w); ++i;
7224 AREF (vector, i) = w->buffer; ++i;
7225 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7226 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7227 }
7228 else
7229 {
7230 int end = i + 4;
7231 for (; i < end; ++i)
7232 AREF (vector, i) = Qnil;
7233 }
7234
7235 xassert (i == ASIZE (vector));
7236 return vector;
7237 }
7238
7239
7240 /* Restore global state from VECTOR which was created by
7241 with_echo_area_buffer_unwind_data. */
7242
7243 static Lisp_Object
7244 unwind_with_echo_area_buffer (vector)
7245 Lisp_Object vector;
7246 {
7247 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7248 Vdeactivate_mark = AREF (vector, 1);
7249 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7250
7251 if (WINDOWP (AREF (vector, 3)))
7252 {
7253 struct window *w;
7254 Lisp_Object buffer, charpos, bytepos;
7255
7256 w = XWINDOW (AREF (vector, 3));
7257 buffer = AREF (vector, 4);
7258 charpos = AREF (vector, 5);
7259 bytepos = AREF (vector, 6);
7260
7261 w->buffer = buffer;
7262 set_marker_both (w->pointm, buffer,
7263 XFASTINT (charpos), XFASTINT (bytepos));
7264 }
7265
7266 Vwith_echo_area_save_vector = vector;
7267 return Qnil;
7268 }
7269
7270
7271 /* Set up the echo area for use by print functions. MULTIBYTE_P
7272 non-zero means we will print multibyte. */
7273
7274 void
7275 setup_echo_area_for_printing (multibyte_p)
7276 int multibyte_p;
7277 {
7278 /* If we can't find an echo area any more, exit. */
7279 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7280 Fkill_emacs (Qnil);
7281
7282 ensure_echo_area_buffers ();
7283
7284 if (!message_buf_print)
7285 {
7286 /* A message has been output since the last time we printed.
7287 Choose a fresh echo area buffer. */
7288 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7289 echo_area_buffer[0] = echo_buffer[1];
7290 else
7291 echo_area_buffer[0] = echo_buffer[0];
7292
7293 /* Switch to that buffer and clear it. */
7294 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7295 current_buffer->truncate_lines = Qnil;
7296
7297 if (Z > BEG)
7298 {
7299 int count = SPECPDL_INDEX ();
7300 specbind (Qinhibit_read_only, Qt);
7301 /* Note that undo recording is always disabled. */
7302 del_range (BEG, Z);
7303 unbind_to (count, Qnil);
7304 }
7305 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7306
7307 /* Set up the buffer for the multibyteness we need. */
7308 if (multibyte_p
7309 != !NILP (current_buffer->enable_multibyte_characters))
7310 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7311
7312 /* Raise the frame containing the echo area. */
7313 if (minibuffer_auto_raise)
7314 {
7315 struct frame *sf = SELECTED_FRAME ();
7316 Lisp_Object mini_window;
7317 mini_window = FRAME_MINIBUF_WINDOW (sf);
7318 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7319 }
7320
7321 message_log_maybe_newline ();
7322 message_buf_print = 1;
7323 }
7324 else
7325 {
7326 if (NILP (echo_area_buffer[0]))
7327 {
7328 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7329 echo_area_buffer[0] = echo_buffer[1];
7330 else
7331 echo_area_buffer[0] = echo_buffer[0];
7332 }
7333
7334 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7335 {
7336 /* Someone switched buffers between print requests. */
7337 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7338 current_buffer->truncate_lines = Qnil;
7339 }
7340 }
7341 }
7342
7343
7344 /* Display an echo area message in window W. Value is non-zero if W's
7345 height is changed. If display_last_displayed_message_p is
7346 non-zero, display the message that was last displayed, otherwise
7347 display the current message. */
7348
7349 static int
7350 display_echo_area (w)
7351 struct window *w;
7352 {
7353 int i, no_message_p, window_height_changed_p, count;
7354
7355 /* Temporarily disable garbage collections while displaying the echo
7356 area. This is done because a GC can print a message itself.
7357 That message would modify the echo area buffer's contents while a
7358 redisplay of the buffer is going on, and seriously confuse
7359 redisplay. */
7360 count = inhibit_garbage_collection ();
7361
7362 /* If there is no message, we must call display_echo_area_1
7363 nevertheless because it resizes the window. But we will have to
7364 reset the echo_area_buffer in question to nil at the end because
7365 with_echo_area_buffer will sets it to an empty buffer. */
7366 i = display_last_displayed_message_p ? 1 : 0;
7367 no_message_p = NILP (echo_area_buffer[i]);
7368
7369 window_height_changed_p
7370 = with_echo_area_buffer (w, display_last_displayed_message_p,
7371 display_echo_area_1,
7372 (EMACS_INT) w, Qnil, 0, 0);
7373
7374 if (no_message_p)
7375 echo_area_buffer[i] = Qnil;
7376
7377 unbind_to (count, Qnil);
7378 return window_height_changed_p;
7379 }
7380
7381
7382 /* Helper for display_echo_area. Display the current buffer which
7383 contains the current echo area message in window W, a mini-window,
7384 a pointer to which is passed in A1. A2..A4 are currently not used.
7385 Change the height of W so that all of the message is displayed.
7386 Value is non-zero if height of W was changed. */
7387
7388 static int
7389 display_echo_area_1 (a1, a2, a3, a4)
7390 EMACS_INT a1;
7391 Lisp_Object a2;
7392 EMACS_INT a3, a4;
7393 {
7394 struct window *w = (struct window *) a1;
7395 Lisp_Object window;
7396 struct text_pos start;
7397 int window_height_changed_p = 0;
7398
7399 /* Do this before displaying, so that we have a large enough glyph
7400 matrix for the display. */
7401 window_height_changed_p = resize_mini_window (w, 0);
7402
7403 /* Display. */
7404 clear_glyph_matrix (w->desired_matrix);
7405 XSETWINDOW (window, w);
7406 SET_TEXT_POS (start, BEG, BEG_BYTE);
7407 try_window (window, start);
7408
7409 return window_height_changed_p;
7410 }
7411
7412
7413 /* Resize the echo area window to exactly the size needed for the
7414 currently displayed message, if there is one. If a mini-buffer
7415 is active, don't shrink it. */
7416
7417 void
7418 resize_echo_area_exactly ()
7419 {
7420 if (BUFFERP (echo_area_buffer[0])
7421 && WINDOWP (echo_area_window))
7422 {
7423 struct window *w = XWINDOW (echo_area_window);
7424 int resized_p;
7425 Lisp_Object resize_exactly;
7426
7427 if (minibuf_level == 0)
7428 resize_exactly = Qt;
7429 else
7430 resize_exactly = Qnil;
7431
7432 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7433 (EMACS_INT) w, resize_exactly, 0, 0);
7434 if (resized_p)
7435 {
7436 ++windows_or_buffers_changed;
7437 ++update_mode_lines;
7438 redisplay_internal (0);
7439 }
7440 }
7441 }
7442
7443
7444 /* Callback function for with_echo_area_buffer, when used from
7445 resize_echo_area_exactly. A1 contains a pointer to the window to
7446 resize, EXACTLY non-nil means resize the mini-window exactly to the
7447 size of the text displayed. A3 and A4 are not used. Value is what
7448 resize_mini_window returns. */
7449
7450 static int
7451 resize_mini_window_1 (a1, exactly, a3, a4)
7452 EMACS_INT a1;
7453 Lisp_Object exactly;
7454 EMACS_INT a3, a4;
7455 {
7456 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7457 }
7458
7459
7460 /* Resize mini-window W to fit the size of its contents. EXACT:P
7461 means size the window exactly to the size needed. Otherwise, it's
7462 only enlarged until W's buffer is empty. Value is non-zero if
7463 the window height has been changed. */
7464
7465 int
7466 resize_mini_window (w, exact_p)
7467 struct window *w;
7468 int exact_p;
7469 {
7470 struct frame *f = XFRAME (w->frame);
7471 int window_height_changed_p = 0;
7472
7473 xassert (MINI_WINDOW_P (w));
7474
7475 /* Don't resize windows while redisplaying a window; it would
7476 confuse redisplay functions when the size of the window they are
7477 displaying changes from under them. Such a resizing can happen,
7478 for instance, when which-func prints a long message while
7479 we are running fontification-functions. We're running these
7480 functions with safe_call which binds inhibit-redisplay to t. */
7481 if (!NILP (Vinhibit_redisplay))
7482 return 0;
7483
7484 /* Nil means don't try to resize. */
7485 if (NILP (Vresize_mini_windows)
7486 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7487 return 0;
7488
7489 if (!FRAME_MINIBUF_ONLY_P (f))
7490 {
7491 struct it it;
7492 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7493 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7494 int height, max_height;
7495 int unit = FRAME_LINE_HEIGHT (f);
7496 struct text_pos start;
7497 struct buffer *old_current_buffer = NULL;
7498
7499 if (current_buffer != XBUFFER (w->buffer))
7500 {
7501 old_current_buffer = current_buffer;
7502 set_buffer_internal (XBUFFER (w->buffer));
7503 }
7504
7505 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7506
7507 /* Compute the max. number of lines specified by the user. */
7508 if (FLOATP (Vmax_mini_window_height))
7509 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7510 else if (INTEGERP (Vmax_mini_window_height))
7511 max_height = XINT (Vmax_mini_window_height);
7512 else
7513 max_height = total_height / 4;
7514
7515 /* Correct that max. height if it's bogus. */
7516 max_height = max (1, max_height);
7517 max_height = min (total_height, max_height);
7518
7519 /* Find out the height of the text in the window. */
7520 if (it.truncate_lines_p)
7521 height = 1;
7522 else
7523 {
7524 last_height = 0;
7525 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7526 if (it.max_ascent == 0 && it.max_descent == 0)
7527 height = it.current_y + last_height;
7528 else
7529 height = it.current_y + it.max_ascent + it.max_descent;
7530 height -= it.extra_line_spacing;
7531 height = (height + unit - 1) / unit;
7532 }
7533
7534 /* Compute a suitable window start. */
7535 if (height > max_height)
7536 {
7537 height = max_height;
7538 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7539 move_it_vertically_backward (&it, (height - 1) * unit);
7540 start = it.current.pos;
7541 }
7542 else
7543 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7544 SET_MARKER_FROM_TEXT_POS (w->start, start);
7545
7546 if (EQ (Vresize_mini_windows, Qgrow_only))
7547 {
7548 /* Let it grow only, until we display an empty message, in which
7549 case the window shrinks again. */
7550 if (height > WINDOW_TOTAL_LINES (w))
7551 {
7552 int old_height = WINDOW_TOTAL_LINES (w);
7553 freeze_window_starts (f, 1);
7554 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7555 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7556 }
7557 else if (height < WINDOW_TOTAL_LINES (w)
7558 && (exact_p || BEGV == ZV))
7559 {
7560 int old_height = WINDOW_TOTAL_LINES (w);
7561 freeze_window_starts (f, 0);
7562 shrink_mini_window (w);
7563 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7564 }
7565 }
7566 else
7567 {
7568 /* Always resize to exact size needed. */
7569 if (height > WINDOW_TOTAL_LINES (w))
7570 {
7571 int old_height = WINDOW_TOTAL_LINES (w);
7572 freeze_window_starts (f, 1);
7573 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7574 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7575 }
7576 else if (height < WINDOW_TOTAL_LINES (w))
7577 {
7578 int old_height = WINDOW_TOTAL_LINES (w);
7579 freeze_window_starts (f, 0);
7580 shrink_mini_window (w);
7581
7582 if (height)
7583 {
7584 freeze_window_starts (f, 1);
7585 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7586 }
7587
7588 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7589 }
7590 }
7591
7592 if (old_current_buffer)
7593 set_buffer_internal (old_current_buffer);
7594 }
7595
7596 return window_height_changed_p;
7597 }
7598
7599
7600 /* Value is the current message, a string, or nil if there is no
7601 current message. */
7602
7603 Lisp_Object
7604 current_message ()
7605 {
7606 Lisp_Object msg;
7607
7608 if (NILP (echo_area_buffer[0]))
7609 msg = Qnil;
7610 else
7611 {
7612 with_echo_area_buffer (0, 0, current_message_1,
7613 (EMACS_INT) &msg, Qnil, 0, 0);
7614 if (NILP (msg))
7615 echo_area_buffer[0] = Qnil;
7616 }
7617
7618 return msg;
7619 }
7620
7621
7622 static int
7623 current_message_1 (a1, a2, a3, a4)
7624 EMACS_INT a1;
7625 Lisp_Object a2;
7626 EMACS_INT a3, a4;
7627 {
7628 Lisp_Object *msg = (Lisp_Object *) a1;
7629
7630 if (Z > BEG)
7631 *msg = make_buffer_string (BEG, Z, 1);
7632 else
7633 *msg = Qnil;
7634 return 0;
7635 }
7636
7637
7638 /* Push the current message on Vmessage_stack for later restauration
7639 by restore_message. Value is non-zero if the current message isn't
7640 empty. This is a relatively infrequent operation, so it's not
7641 worth optimizing. */
7642
7643 int
7644 push_message ()
7645 {
7646 Lisp_Object msg;
7647 msg = current_message ();
7648 Vmessage_stack = Fcons (msg, Vmessage_stack);
7649 return STRINGP (msg);
7650 }
7651
7652
7653 /* Restore message display from the top of Vmessage_stack. */
7654
7655 void
7656 restore_message ()
7657 {
7658 Lisp_Object msg;
7659
7660 xassert (CONSP (Vmessage_stack));
7661 msg = XCAR (Vmessage_stack);
7662 if (STRINGP (msg))
7663 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7664 else
7665 message3_nolog (msg, 0, 0);
7666 }
7667
7668
7669 /* Handler for record_unwind_protect calling pop_message. */
7670
7671 Lisp_Object
7672 pop_message_unwind (dummy)
7673 Lisp_Object dummy;
7674 {
7675 pop_message ();
7676 return Qnil;
7677 }
7678
7679 /* Pop the top-most entry off Vmessage_stack. */
7680
7681 void
7682 pop_message ()
7683 {
7684 xassert (CONSP (Vmessage_stack));
7685 Vmessage_stack = XCDR (Vmessage_stack);
7686 }
7687
7688
7689 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7690 exits. If the stack is not empty, we have a missing pop_message
7691 somewhere. */
7692
7693 void
7694 check_message_stack ()
7695 {
7696 if (!NILP (Vmessage_stack))
7697 abort ();
7698 }
7699
7700
7701 /* Truncate to NCHARS what will be displayed in the echo area the next
7702 time we display it---but don't redisplay it now. */
7703
7704 void
7705 truncate_echo_area (nchars)
7706 int nchars;
7707 {
7708 if (nchars == 0)
7709 echo_area_buffer[0] = Qnil;
7710 /* A null message buffer means that the frame hasn't really been
7711 initialized yet. Error messages get reported properly by
7712 cmd_error, so this must be just an informative message; toss it. */
7713 else if (!noninteractive
7714 && INTERACTIVE
7715 && !NILP (echo_area_buffer[0]))
7716 {
7717 struct frame *sf = SELECTED_FRAME ();
7718 if (FRAME_MESSAGE_BUF (sf))
7719 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7720 }
7721 }
7722
7723
7724 /* Helper function for truncate_echo_area. Truncate the current
7725 message to at most NCHARS characters. */
7726
7727 static int
7728 truncate_message_1 (nchars, a2, a3, a4)
7729 EMACS_INT nchars;
7730 Lisp_Object a2;
7731 EMACS_INT a3, a4;
7732 {
7733 if (BEG + nchars < Z)
7734 del_range (BEG + nchars, Z);
7735 if (Z == BEG)
7736 echo_area_buffer[0] = Qnil;
7737 return 0;
7738 }
7739
7740
7741 /* Set the current message to a substring of S or STRING.
7742
7743 If STRING is a Lisp string, set the message to the first NBYTES
7744 bytes from STRING. NBYTES zero means use the whole string. If
7745 STRING is multibyte, the message will be displayed multibyte.
7746
7747 If S is not null, set the message to the first LEN bytes of S. LEN
7748 zero means use the whole string. MULTIBYTE_P non-zero means S is
7749 multibyte. Display the message multibyte in that case. */
7750
7751 void
7752 set_message (s, string, nbytes, multibyte_p)
7753 const char *s;
7754 Lisp_Object string;
7755 int nbytes, multibyte_p;
7756 {
7757 message_enable_multibyte
7758 = ((s && multibyte_p)
7759 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7760
7761 with_echo_area_buffer (0, -1, set_message_1,
7762 (EMACS_INT) s, string, nbytes, multibyte_p);
7763 message_buf_print = 0;
7764 help_echo_showing_p = 0;
7765 }
7766
7767
7768 /* Helper function for set_message. Arguments have the same meaning
7769 as there, with A1 corresponding to S and A2 corresponding to STRING
7770 This function is called with the echo area buffer being
7771 current. */
7772
7773 static int
7774 set_message_1 (a1, a2, nbytes, multibyte_p)
7775 EMACS_INT a1;
7776 Lisp_Object a2;
7777 EMACS_INT nbytes, multibyte_p;
7778 {
7779 const char *s = (const char *) a1;
7780 Lisp_Object string = a2;
7781
7782 xassert (BEG == Z);
7783
7784 /* Change multibyteness of the echo buffer appropriately. */
7785 if (message_enable_multibyte
7786 != !NILP (current_buffer->enable_multibyte_characters))
7787 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7788
7789 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7790
7791 /* Insert new message at BEG. */
7792 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7793
7794 if (STRINGP (string))
7795 {
7796 int nchars;
7797
7798 if (nbytes == 0)
7799 nbytes = SBYTES (string);
7800 nchars = string_byte_to_char (string, nbytes);
7801
7802 /* This function takes care of single/multibyte conversion. We
7803 just have to ensure that the echo area buffer has the right
7804 setting of enable_multibyte_characters. */
7805 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7806 }
7807 else if (s)
7808 {
7809 if (nbytes == 0)
7810 nbytes = strlen (s);
7811
7812 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7813 {
7814 /* Convert from multi-byte to single-byte. */
7815 int i, c, n;
7816 unsigned char work[1];
7817
7818 /* Convert a multibyte string to single-byte. */
7819 for (i = 0; i < nbytes; i += n)
7820 {
7821 c = string_char_and_length (s + i, nbytes - i, &n);
7822 work[0] = (ASCII_CHAR_P (c)
7823 ? c
7824 : multibyte_char_to_unibyte (c, Qnil));
7825 insert_1_both (work, 1, 1, 1, 0, 0);
7826 }
7827 }
7828 else if (!multibyte_p
7829 && !NILP (current_buffer->enable_multibyte_characters))
7830 {
7831 /* Convert from single-byte to multi-byte. */
7832 int i, c, n;
7833 const unsigned char *msg = (const unsigned char *) s;
7834 unsigned char str[MAX_MULTIBYTE_LENGTH];
7835
7836 /* Convert a single-byte string to multibyte. */
7837 for (i = 0; i < nbytes; i++)
7838 {
7839 c = unibyte_char_to_multibyte (msg[i]);
7840 n = CHAR_STRING (c, str);
7841 insert_1_both (str, 1, n, 1, 0, 0);
7842 }
7843 }
7844 else
7845 insert_1 (s, nbytes, 1, 0, 0);
7846 }
7847
7848 return 0;
7849 }
7850
7851
7852 /* Clear messages. CURRENT_P non-zero means clear the current
7853 message. LAST_DISPLAYED_P non-zero means clear the message
7854 last displayed. */
7855
7856 void
7857 clear_message (current_p, last_displayed_p)
7858 int current_p, last_displayed_p;
7859 {
7860 if (current_p)
7861 {
7862 echo_area_buffer[0] = Qnil;
7863 message_cleared_p = 1;
7864 }
7865
7866 if (last_displayed_p)
7867 echo_area_buffer[1] = Qnil;
7868
7869 message_buf_print = 0;
7870 }
7871
7872 /* Clear garbaged frames.
7873
7874 This function is used where the old redisplay called
7875 redraw_garbaged_frames which in turn called redraw_frame which in
7876 turn called clear_frame. The call to clear_frame was a source of
7877 flickering. I believe a clear_frame is not necessary. It should
7878 suffice in the new redisplay to invalidate all current matrices,
7879 and ensure a complete redisplay of all windows. */
7880
7881 static void
7882 clear_garbaged_frames ()
7883 {
7884 if (frame_garbaged)
7885 {
7886 Lisp_Object tail, frame;
7887 int changed_count = 0;
7888
7889 FOR_EACH_FRAME (tail, frame)
7890 {
7891 struct frame *f = XFRAME (frame);
7892
7893 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7894 {
7895 if (f->resized_p)
7896 {
7897 Fredraw_frame (frame);
7898 f->force_flush_display_p = 1;
7899 }
7900 clear_current_matrices (f);
7901 changed_count++;
7902 f->garbaged = 0;
7903 f->resized_p = 0;
7904 }
7905 }
7906
7907 frame_garbaged = 0;
7908 if (changed_count)
7909 ++windows_or_buffers_changed;
7910 }
7911 }
7912
7913
7914 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7915 is non-zero update selected_frame. Value is non-zero if the
7916 mini-windows height has been changed. */
7917
7918 static int
7919 echo_area_display (update_frame_p)
7920 int update_frame_p;
7921 {
7922 Lisp_Object mini_window;
7923 struct window *w;
7924 struct frame *f;
7925 int window_height_changed_p = 0;
7926 struct frame *sf = SELECTED_FRAME ();
7927
7928 mini_window = FRAME_MINIBUF_WINDOW (sf);
7929 w = XWINDOW (mini_window);
7930 f = XFRAME (WINDOW_FRAME (w));
7931
7932 /* Don't display if frame is invisible or not yet initialized. */
7933 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7934 return 0;
7935
7936 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7937 #ifndef MAC_OS8
7938 #ifdef HAVE_WINDOW_SYSTEM
7939 /* When Emacs starts, selected_frame may be a visible terminal
7940 frame, even if we run under a window system. If we let this
7941 through, a message would be displayed on the terminal. */
7942 if (EQ (selected_frame, Vterminal_frame)
7943 && !NILP (Vwindow_system))
7944 return 0;
7945 #endif /* HAVE_WINDOW_SYSTEM */
7946 #endif
7947
7948 /* Redraw garbaged frames. */
7949 if (frame_garbaged)
7950 clear_garbaged_frames ();
7951
7952 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7953 {
7954 echo_area_window = mini_window;
7955 window_height_changed_p = display_echo_area (w);
7956 w->must_be_updated_p = 1;
7957
7958 /* Update the display, unless called from redisplay_internal.
7959 Also don't update the screen during redisplay itself. The
7960 update will happen at the end of redisplay, and an update
7961 here could cause confusion. */
7962 if (update_frame_p && !redisplaying_p)
7963 {
7964 int n = 0;
7965
7966 /* If the display update has been interrupted by pending
7967 input, update mode lines in the frame. Due to the
7968 pending input, it might have been that redisplay hasn't
7969 been called, so that mode lines above the echo area are
7970 garbaged. This looks odd, so we prevent it here. */
7971 if (!display_completed)
7972 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7973
7974 if (window_height_changed_p
7975 /* Don't do this if Emacs is shutting down. Redisplay
7976 needs to run hooks. */
7977 && !NILP (Vrun_hooks))
7978 {
7979 /* Must update other windows. Likewise as in other
7980 cases, don't let this update be interrupted by
7981 pending input. */
7982 int count = SPECPDL_INDEX ();
7983 specbind (Qredisplay_dont_pause, Qt);
7984 windows_or_buffers_changed = 1;
7985 redisplay_internal (0);
7986 unbind_to (count, Qnil);
7987 }
7988 else if (FRAME_WINDOW_P (f) && n == 0)
7989 {
7990 /* Window configuration is the same as before.
7991 Can do with a display update of the echo area,
7992 unless we displayed some mode lines. */
7993 update_single_window (w, 1);
7994 rif->flush_display (f);
7995 }
7996 else
7997 update_frame (f, 1, 1);
7998
7999 /* If cursor is in the echo area, make sure that the next
8000 redisplay displays the minibuffer, so that the cursor will
8001 be replaced with what the minibuffer wants. */
8002 if (cursor_in_echo_area)
8003 ++windows_or_buffers_changed;
8004 }
8005 }
8006 else if (!EQ (mini_window, selected_window))
8007 windows_or_buffers_changed++;
8008
8009 /* Last displayed message is now the current message. */
8010 echo_area_buffer[1] = echo_area_buffer[0];
8011
8012 /* Prevent redisplay optimization in redisplay_internal by resetting
8013 this_line_start_pos. This is done because the mini-buffer now
8014 displays the message instead of its buffer text. */
8015 if (EQ (mini_window, selected_window))
8016 CHARPOS (this_line_start_pos) = 0;
8017
8018 return window_height_changed_p;
8019 }
8020
8021
8022 \f
8023 /***********************************************************************
8024 Frame Titles
8025 ***********************************************************************/
8026
8027
8028 /* The frame title buffering code is also used by Fformat_mode_line.
8029 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
8030
8031 /* A buffer for constructing frame titles in it; allocated from the
8032 heap in init_xdisp and resized as needed in store_frame_title_char. */
8033
8034 static char *frame_title_buf;
8035
8036 /* The buffer's end, and a current output position in it. */
8037
8038 static char *frame_title_buf_end;
8039 static char *frame_title_ptr;
8040
8041
8042 /* Store a single character C for the frame title in frame_title_buf.
8043 Re-allocate frame_title_buf if necessary. */
8044
8045 static void
8046 #ifdef PROTOTYPES
8047 store_frame_title_char (char c)
8048 #else
8049 store_frame_title_char (c)
8050 char c;
8051 #endif
8052 {
8053 /* If output position has reached the end of the allocated buffer,
8054 double the buffer's size. */
8055 if (frame_title_ptr == frame_title_buf_end)
8056 {
8057 int len = frame_title_ptr - frame_title_buf;
8058 int new_size = 2 * len * sizeof *frame_title_buf;
8059 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
8060 frame_title_buf_end = frame_title_buf + new_size;
8061 frame_title_ptr = frame_title_buf + len;
8062 }
8063
8064 *frame_title_ptr++ = c;
8065 }
8066
8067
8068 /* Store part of a frame title in frame_title_buf, beginning at
8069 frame_title_ptr. STR is the string to store. Do not copy
8070 characters that yield more columns than PRECISION; PRECISION <= 0
8071 means copy the whole string. Pad with spaces until FIELD_WIDTH
8072 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8073 pad. Called from display_mode_element when it is used to build a
8074 frame title. */
8075
8076 static int
8077 store_frame_title (str, field_width, precision)
8078 const unsigned char *str;
8079 int field_width, precision;
8080 {
8081 int n = 0;
8082 int dummy, nbytes;
8083
8084 /* Copy at most PRECISION chars from STR. */
8085 nbytes = strlen (str);
8086 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
8087 while (nbytes--)
8088 store_frame_title_char (*str++);
8089
8090 /* Fill up with spaces until FIELD_WIDTH reached. */
8091 while (field_width > 0
8092 && n < field_width)
8093 {
8094 store_frame_title_char (' ');
8095 ++n;
8096 }
8097
8098 return n;
8099 }
8100
8101 #ifdef HAVE_WINDOW_SYSTEM
8102
8103 /* Set the title of FRAME, if it has changed. The title format is
8104 Vicon_title_format if FRAME is iconified, otherwise it is
8105 frame_title_format. */
8106
8107 static void
8108 x_consider_frame_title (frame)
8109 Lisp_Object frame;
8110 {
8111 struct frame *f = XFRAME (frame);
8112
8113 if (FRAME_WINDOW_P (f)
8114 || FRAME_MINIBUF_ONLY_P (f)
8115 || f->explicit_name)
8116 {
8117 /* Do we have more than one visible frame on this X display? */
8118 Lisp_Object tail;
8119 Lisp_Object fmt;
8120 struct buffer *obuf;
8121 int len;
8122 struct it it;
8123
8124 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8125 {
8126 Lisp_Object other_frame = XCAR (tail);
8127 struct frame *tf = XFRAME (other_frame);
8128
8129 if (tf != f
8130 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8131 && !FRAME_MINIBUF_ONLY_P (tf)
8132 && !EQ (other_frame, tip_frame)
8133 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8134 break;
8135 }
8136
8137 /* Set global variable indicating that multiple frames exist. */
8138 multiple_frames = CONSP (tail);
8139
8140 /* Switch to the buffer of selected window of the frame. Set up
8141 frame_title_ptr so that display_mode_element will output into it;
8142 then display the title. */
8143 obuf = current_buffer;
8144 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8145 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8146 frame_title_ptr = frame_title_buf;
8147 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8148 NULL, DEFAULT_FACE_ID);
8149 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8150 len = frame_title_ptr - frame_title_buf;
8151 frame_title_ptr = NULL;
8152 set_buffer_internal_1 (obuf);
8153
8154 /* Set the title only if it's changed. This avoids consing in
8155 the common case where it hasn't. (If it turns out that we've
8156 already wasted too much time by walking through the list with
8157 display_mode_element, then we might need to optimize at a
8158 higher level than this.) */
8159 if (! STRINGP (f->name)
8160 || SBYTES (f->name) != len
8161 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
8162 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
8163 }
8164 }
8165
8166 #endif /* not HAVE_WINDOW_SYSTEM */
8167
8168
8169
8170 \f
8171 /***********************************************************************
8172 Menu Bars
8173 ***********************************************************************/
8174
8175
8176 /* Prepare for redisplay by updating menu-bar item lists when
8177 appropriate. This can call eval. */
8178
8179 void
8180 prepare_menu_bars ()
8181 {
8182 int all_windows;
8183 struct gcpro gcpro1, gcpro2;
8184 struct frame *f;
8185 Lisp_Object tooltip_frame;
8186
8187 #ifdef HAVE_WINDOW_SYSTEM
8188 tooltip_frame = tip_frame;
8189 #else
8190 tooltip_frame = Qnil;
8191 #endif
8192
8193 /* Update all frame titles based on their buffer names, etc. We do
8194 this before the menu bars so that the buffer-menu will show the
8195 up-to-date frame titles. */
8196 #ifdef HAVE_WINDOW_SYSTEM
8197 if (windows_or_buffers_changed || update_mode_lines)
8198 {
8199 Lisp_Object tail, frame;
8200
8201 FOR_EACH_FRAME (tail, frame)
8202 {
8203 f = XFRAME (frame);
8204 if (!EQ (frame, tooltip_frame)
8205 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8206 x_consider_frame_title (frame);
8207 }
8208 }
8209 #endif /* HAVE_WINDOW_SYSTEM */
8210
8211 /* Update the menu bar item lists, if appropriate. This has to be
8212 done before any actual redisplay or generation of display lines. */
8213 all_windows = (update_mode_lines
8214 || buffer_shared > 1
8215 || windows_or_buffers_changed);
8216 if (all_windows)
8217 {
8218 Lisp_Object tail, frame;
8219 int count = SPECPDL_INDEX ();
8220
8221 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8222
8223 FOR_EACH_FRAME (tail, frame)
8224 {
8225 f = XFRAME (frame);
8226
8227 /* Ignore tooltip frame. */
8228 if (EQ (frame, tooltip_frame))
8229 continue;
8230
8231 /* If a window on this frame changed size, report that to
8232 the user and clear the size-change flag. */
8233 if (FRAME_WINDOW_SIZES_CHANGED (f))
8234 {
8235 Lisp_Object functions;
8236
8237 /* Clear flag first in case we get an error below. */
8238 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8239 functions = Vwindow_size_change_functions;
8240 GCPRO2 (tail, functions);
8241
8242 while (CONSP (functions))
8243 {
8244 call1 (XCAR (functions), frame);
8245 functions = XCDR (functions);
8246 }
8247 UNGCPRO;
8248 }
8249
8250 GCPRO1 (tail);
8251 update_menu_bar (f, 0);
8252 #ifdef HAVE_WINDOW_SYSTEM
8253 update_tool_bar (f, 0);
8254 #endif
8255 UNGCPRO;
8256 }
8257
8258 unbind_to (count, Qnil);
8259 }
8260 else
8261 {
8262 struct frame *sf = SELECTED_FRAME ();
8263 update_menu_bar (sf, 1);
8264 #ifdef HAVE_WINDOW_SYSTEM
8265 update_tool_bar (sf, 1);
8266 #endif
8267 }
8268
8269 /* Motif needs this. See comment in xmenu.c. Turn it off when
8270 pending_menu_activation is not defined. */
8271 #ifdef USE_X_TOOLKIT
8272 pending_menu_activation = 0;
8273 #endif
8274 }
8275
8276
8277 /* Update the menu bar item list for frame F. This has to be done
8278 before we start to fill in any display lines, because it can call
8279 eval.
8280
8281 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8282
8283 static void
8284 update_menu_bar (f, save_match_data)
8285 struct frame *f;
8286 int save_match_data;
8287 {
8288 Lisp_Object window;
8289 register struct window *w;
8290
8291 /* If called recursively during a menu update, do nothing. This can
8292 happen when, for instance, an activate-menubar-hook causes a
8293 redisplay. */
8294 if (inhibit_menubar_update)
8295 return;
8296
8297 window = FRAME_SELECTED_WINDOW (f);
8298 w = XWINDOW (window);
8299
8300 #if 0 /* The if statement below this if statement used to include the
8301 condition !NILP (w->update_mode_line), rather than using
8302 update_mode_lines directly, and this if statement may have
8303 been added to make that condition work. Now the if
8304 statement below matches its comment, this isn't needed. */
8305 if (update_mode_lines)
8306 w->update_mode_line = Qt;
8307 #endif
8308
8309 if (FRAME_WINDOW_P (f)
8310 ?
8311 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8312 || defined (USE_GTK)
8313 FRAME_EXTERNAL_MENU_BAR (f)
8314 #else
8315 FRAME_MENU_BAR_LINES (f) > 0
8316 #endif
8317 : FRAME_MENU_BAR_LINES (f) > 0)
8318 {
8319 /* If the user has switched buffers or windows, we need to
8320 recompute to reflect the new bindings. But we'll
8321 recompute when update_mode_lines is set too; that means
8322 that people can use force-mode-line-update to request
8323 that the menu bar be recomputed. The adverse effect on
8324 the rest of the redisplay algorithm is about the same as
8325 windows_or_buffers_changed anyway. */
8326 if (windows_or_buffers_changed
8327 /* This used to test w->update_mode_line, but we believe
8328 there is no need to recompute the menu in that case. */
8329 || update_mode_lines
8330 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8331 < BUF_MODIFF (XBUFFER (w->buffer)))
8332 != !NILP (w->last_had_star))
8333 || ((!NILP (Vtransient_mark_mode)
8334 && !NILP (XBUFFER (w->buffer)->mark_active))
8335 != !NILP (w->region_showing)))
8336 {
8337 struct buffer *prev = current_buffer;
8338 int count = SPECPDL_INDEX ();
8339
8340 specbind (Qinhibit_menubar_update, Qt);
8341
8342 set_buffer_internal_1 (XBUFFER (w->buffer));
8343 if (save_match_data)
8344 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8345 if (NILP (Voverriding_local_map_menu_flag))
8346 {
8347 specbind (Qoverriding_terminal_local_map, Qnil);
8348 specbind (Qoverriding_local_map, Qnil);
8349 }
8350
8351 /* Run the Lucid hook. */
8352 safe_run_hooks (Qactivate_menubar_hook);
8353
8354 /* If it has changed current-menubar from previous value,
8355 really recompute the menu-bar from the value. */
8356 if (! NILP (Vlucid_menu_bar_dirty_flag))
8357 call0 (Qrecompute_lucid_menubar);
8358
8359 safe_run_hooks (Qmenu_bar_update_hook);
8360 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8361
8362 /* Redisplay the menu bar in case we changed it. */
8363 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8364 || defined (USE_GTK)
8365 if (FRAME_WINDOW_P (f)
8366 #if defined (MAC_OS)
8367 /* All frames on Mac OS share the same menubar. So only the
8368 selected frame should be allowed to set it. */
8369 && f == SELECTED_FRAME ()
8370 #endif
8371 )
8372 set_frame_menubar (f, 0, 0);
8373 else
8374 /* On a terminal screen, the menu bar is an ordinary screen
8375 line, and this makes it get updated. */
8376 w->update_mode_line = Qt;
8377 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8378 /* In the non-toolkit version, the menu bar is an ordinary screen
8379 line, and this makes it get updated. */
8380 w->update_mode_line = Qt;
8381 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8382
8383 unbind_to (count, Qnil);
8384 set_buffer_internal_1 (prev);
8385 }
8386 }
8387 }
8388
8389
8390 \f
8391 /***********************************************************************
8392 Output Cursor
8393 ***********************************************************************/
8394
8395 #ifdef HAVE_WINDOW_SYSTEM
8396
8397 /* EXPORT:
8398 Nominal cursor position -- where to draw output.
8399 HPOS and VPOS are window relative glyph matrix coordinates.
8400 X and Y are window relative pixel coordinates. */
8401
8402 struct cursor_pos output_cursor;
8403
8404
8405 /* EXPORT:
8406 Set the global variable output_cursor to CURSOR. All cursor
8407 positions are relative to updated_window. */
8408
8409 void
8410 set_output_cursor (cursor)
8411 struct cursor_pos *cursor;
8412 {
8413 output_cursor.hpos = cursor->hpos;
8414 output_cursor.vpos = cursor->vpos;
8415 output_cursor.x = cursor->x;
8416 output_cursor.y = cursor->y;
8417 }
8418
8419
8420 /* EXPORT for RIF:
8421 Set a nominal cursor position.
8422
8423 HPOS and VPOS are column/row positions in a window glyph matrix. X
8424 and Y are window text area relative pixel positions.
8425
8426 If this is done during an update, updated_window will contain the
8427 window that is being updated and the position is the future output
8428 cursor position for that window. If updated_window is null, use
8429 selected_window and display the cursor at the given position. */
8430
8431 void
8432 x_cursor_to (vpos, hpos, y, x)
8433 int vpos, hpos, y, x;
8434 {
8435 struct window *w;
8436
8437 /* If updated_window is not set, work on selected_window. */
8438 if (updated_window)
8439 w = updated_window;
8440 else
8441 w = XWINDOW (selected_window);
8442
8443 /* Set the output cursor. */
8444 output_cursor.hpos = hpos;
8445 output_cursor.vpos = vpos;
8446 output_cursor.x = x;
8447 output_cursor.y = y;
8448
8449 /* If not called as part of an update, really display the cursor.
8450 This will also set the cursor position of W. */
8451 if (updated_window == NULL)
8452 {
8453 BLOCK_INPUT;
8454 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8455 if (rif->flush_display_optional)
8456 rif->flush_display_optional (SELECTED_FRAME ());
8457 UNBLOCK_INPUT;
8458 }
8459 }
8460
8461 #endif /* HAVE_WINDOW_SYSTEM */
8462
8463 \f
8464 /***********************************************************************
8465 Tool-bars
8466 ***********************************************************************/
8467
8468 #ifdef HAVE_WINDOW_SYSTEM
8469
8470 /* Where the mouse was last time we reported a mouse event. */
8471
8472 FRAME_PTR last_mouse_frame;
8473
8474 /* Tool-bar item index of the item on which a mouse button was pressed
8475 or -1. */
8476
8477 int last_tool_bar_item;
8478
8479
8480 /* Update the tool-bar item list for frame F. This has to be done
8481 before we start to fill in any display lines. Called from
8482 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8483 and restore it here. */
8484
8485 static void
8486 update_tool_bar (f, save_match_data)
8487 struct frame *f;
8488 int save_match_data;
8489 {
8490 #ifdef USE_GTK
8491 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8492 #else
8493 int do_update = WINDOWP (f->tool_bar_window)
8494 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8495 #endif
8496
8497 if (do_update)
8498 {
8499 Lisp_Object window;
8500 struct window *w;
8501
8502 window = FRAME_SELECTED_WINDOW (f);
8503 w = XWINDOW (window);
8504
8505 /* If the user has switched buffers or windows, we need to
8506 recompute to reflect the new bindings. But we'll
8507 recompute when update_mode_lines is set too; that means
8508 that people can use force-mode-line-update to request
8509 that the menu bar be recomputed. The adverse effect on
8510 the rest of the redisplay algorithm is about the same as
8511 windows_or_buffers_changed anyway. */
8512 if (windows_or_buffers_changed
8513 || !NILP (w->update_mode_line)
8514 || update_mode_lines
8515 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8516 < BUF_MODIFF (XBUFFER (w->buffer)))
8517 != !NILP (w->last_had_star))
8518 || ((!NILP (Vtransient_mark_mode)
8519 && !NILP (XBUFFER (w->buffer)->mark_active))
8520 != !NILP (w->region_showing)))
8521 {
8522 struct buffer *prev = current_buffer;
8523 int count = SPECPDL_INDEX ();
8524 Lisp_Object old_tool_bar;
8525 struct gcpro gcpro1;
8526
8527 /* Set current_buffer to the buffer of the selected
8528 window of the frame, so that we get the right local
8529 keymaps. */
8530 set_buffer_internal_1 (XBUFFER (w->buffer));
8531
8532 /* Save match data, if we must. */
8533 if (save_match_data)
8534 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8535
8536 /* Make sure that we don't accidentally use bogus keymaps. */
8537 if (NILP (Voverriding_local_map_menu_flag))
8538 {
8539 specbind (Qoverriding_terminal_local_map, Qnil);
8540 specbind (Qoverriding_local_map, Qnil);
8541 }
8542
8543 old_tool_bar = f->tool_bar_items;
8544 GCPRO1 (old_tool_bar);
8545
8546 /* Build desired tool-bar items from keymaps. */
8547 BLOCK_INPUT;
8548 f->tool_bar_items
8549 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
8550 UNBLOCK_INPUT;
8551
8552 /* Redisplay the tool-bar if we changed it. */
8553 if (! NILP (Fequal (old_tool_bar, f->tool_bar_items)))
8554 w->update_mode_line = Qt;
8555
8556 UNGCPRO;
8557
8558 unbind_to (count, Qnil);
8559 set_buffer_internal_1 (prev);
8560 }
8561 }
8562 }
8563
8564
8565 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8566 F's desired tool-bar contents. F->tool_bar_items must have
8567 been set up previously by calling prepare_menu_bars. */
8568
8569 static void
8570 build_desired_tool_bar_string (f)
8571 struct frame *f;
8572 {
8573 int i, size, size_needed;
8574 struct gcpro gcpro1, gcpro2, gcpro3;
8575 Lisp_Object image, plist, props;
8576
8577 image = plist = props = Qnil;
8578 GCPRO3 (image, plist, props);
8579
8580 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8581 Otherwise, make a new string. */
8582
8583 /* The size of the string we might be able to reuse. */
8584 size = (STRINGP (f->desired_tool_bar_string)
8585 ? SCHARS (f->desired_tool_bar_string)
8586 : 0);
8587
8588 /* We need one space in the string for each image. */
8589 size_needed = f->n_tool_bar_items;
8590
8591 /* Reuse f->desired_tool_bar_string, if possible. */
8592 if (size < size_needed || NILP (f->desired_tool_bar_string))
8593 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8594 make_number (' '));
8595 else
8596 {
8597 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8598 Fremove_text_properties (make_number (0), make_number (size),
8599 props, f->desired_tool_bar_string);
8600 }
8601
8602 /* Put a `display' property on the string for the images to display,
8603 put a `menu_item' property on tool-bar items with a value that
8604 is the index of the item in F's tool-bar item vector. */
8605 for (i = 0; i < f->n_tool_bar_items; ++i)
8606 {
8607 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8608
8609 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8610 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8611 int hmargin, vmargin, relief, idx, end;
8612 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8613
8614 /* If image is a vector, choose the image according to the
8615 button state. */
8616 image = PROP (TOOL_BAR_ITEM_IMAGES);
8617 if (VECTORP (image))
8618 {
8619 if (enabled_p)
8620 idx = (selected_p
8621 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8622 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8623 else
8624 idx = (selected_p
8625 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8626 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8627
8628 xassert (ASIZE (image) >= idx);
8629 image = AREF (image, idx);
8630 }
8631 else
8632 idx = -1;
8633
8634 /* Ignore invalid image specifications. */
8635 if (!valid_image_p (image))
8636 continue;
8637
8638 /* Display the tool-bar button pressed, or depressed. */
8639 plist = Fcopy_sequence (XCDR (image));
8640
8641 /* Compute margin and relief to draw. */
8642 relief = (tool_bar_button_relief >= 0
8643 ? tool_bar_button_relief
8644 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8645 hmargin = vmargin = relief;
8646
8647 if (INTEGERP (Vtool_bar_button_margin)
8648 && XINT (Vtool_bar_button_margin) > 0)
8649 {
8650 hmargin += XFASTINT (Vtool_bar_button_margin);
8651 vmargin += XFASTINT (Vtool_bar_button_margin);
8652 }
8653 else if (CONSP (Vtool_bar_button_margin))
8654 {
8655 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8656 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8657 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8658
8659 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8660 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8661 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8662 }
8663
8664 if (auto_raise_tool_bar_buttons_p)
8665 {
8666 /* Add a `:relief' property to the image spec if the item is
8667 selected. */
8668 if (selected_p)
8669 {
8670 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8671 hmargin -= relief;
8672 vmargin -= relief;
8673 }
8674 }
8675 else
8676 {
8677 /* If image is selected, display it pressed, i.e. with a
8678 negative relief. If it's not selected, display it with a
8679 raised relief. */
8680 plist = Fplist_put (plist, QCrelief,
8681 (selected_p
8682 ? make_number (-relief)
8683 : make_number (relief)));
8684 hmargin -= relief;
8685 vmargin -= relief;
8686 }
8687
8688 /* Put a margin around the image. */
8689 if (hmargin || vmargin)
8690 {
8691 if (hmargin == vmargin)
8692 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8693 else
8694 plist = Fplist_put (plist, QCmargin,
8695 Fcons (make_number (hmargin),
8696 make_number (vmargin)));
8697 }
8698
8699 /* If button is not enabled, and we don't have special images
8700 for the disabled state, make the image appear disabled by
8701 applying an appropriate algorithm to it. */
8702 if (!enabled_p && idx < 0)
8703 plist = Fplist_put (plist, QCconversion, Qdisabled);
8704
8705 /* Put a `display' text property on the string for the image to
8706 display. Put a `menu-item' property on the string that gives
8707 the start of this item's properties in the tool-bar items
8708 vector. */
8709 image = Fcons (Qimage, plist);
8710 props = list4 (Qdisplay, image,
8711 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8712
8713 /* Let the last image hide all remaining spaces in the tool bar
8714 string. The string can be longer than needed when we reuse a
8715 previous string. */
8716 if (i + 1 == f->n_tool_bar_items)
8717 end = SCHARS (f->desired_tool_bar_string);
8718 else
8719 end = i + 1;
8720 Fadd_text_properties (make_number (i), make_number (end),
8721 props, f->desired_tool_bar_string);
8722 #undef PROP
8723 }
8724
8725 UNGCPRO;
8726 }
8727
8728
8729 /* Display one line of the tool-bar of frame IT->f. */
8730
8731 static void
8732 display_tool_bar_line (it)
8733 struct it *it;
8734 {
8735 struct glyph_row *row = it->glyph_row;
8736 int max_x = it->last_visible_x;
8737 struct glyph *last;
8738
8739 prepare_desired_row (row);
8740 row->y = it->current_y;
8741
8742 /* Note that this isn't made use of if the face hasn't a box,
8743 so there's no need to check the face here. */
8744 it->start_of_box_run_p = 1;
8745
8746 while (it->current_x < max_x)
8747 {
8748 int x_before, x, n_glyphs_before, i, nglyphs;
8749
8750 /* Get the next display element. */
8751 if (!get_next_display_element (it))
8752 break;
8753
8754 /* Produce glyphs. */
8755 x_before = it->current_x;
8756 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8757 PRODUCE_GLYPHS (it);
8758
8759 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8760 i = 0;
8761 x = x_before;
8762 while (i < nglyphs)
8763 {
8764 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
8765
8766 if (x + glyph->pixel_width > max_x)
8767 {
8768 /* Glyph doesn't fit on line. */
8769 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8770 it->current_x = x;
8771 goto out;
8772 }
8773
8774 ++it->hpos;
8775 x += glyph->pixel_width;
8776 ++i;
8777 }
8778
8779 /* Stop at line ends. */
8780 if (ITERATOR_AT_END_OF_LINE_P (it))
8781 break;
8782
8783 set_iterator_to_next (it, 1);
8784 }
8785
8786 out:;
8787
8788 row->displays_text_p = row->used[TEXT_AREA] != 0;
8789 extend_face_to_end_of_line (it);
8790 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8791 last->right_box_line_p = 1;
8792 if (last == row->glyphs[TEXT_AREA])
8793 last->left_box_line_p = 1;
8794 compute_line_metrics (it);
8795
8796 /* If line is empty, make it occupy the rest of the tool-bar. */
8797 if (!row->displays_text_p)
8798 {
8799 row->height = row->phys_height = it->last_visible_y - row->y;
8800 row->ascent = row->phys_ascent = 0;
8801 }
8802
8803 row->full_width_p = 1;
8804 row->continued_p = 0;
8805 row->truncated_on_left_p = 0;
8806 row->truncated_on_right_p = 0;
8807
8808 it->current_x = it->hpos = 0;
8809 it->current_y += row->height;
8810 ++it->vpos;
8811 ++it->glyph_row;
8812 }
8813
8814
8815 /* Value is the number of screen lines needed to make all tool-bar
8816 items of frame F visible. */
8817
8818 static int
8819 tool_bar_lines_needed (f)
8820 struct frame *f;
8821 {
8822 struct window *w = XWINDOW (f->tool_bar_window);
8823 struct it it;
8824
8825 /* Initialize an iterator for iteration over
8826 F->desired_tool_bar_string in the tool-bar window of frame F. */
8827 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8828 it.first_visible_x = 0;
8829 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8830 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8831
8832 while (!ITERATOR_AT_END_P (&it))
8833 {
8834 it.glyph_row = w->desired_matrix->rows;
8835 clear_glyph_row (it.glyph_row);
8836 display_tool_bar_line (&it);
8837 }
8838
8839 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
8840 }
8841
8842
8843 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8844 0, 1, 0,
8845 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8846 (frame)
8847 Lisp_Object frame;
8848 {
8849 struct frame *f;
8850 struct window *w;
8851 int nlines = 0;
8852
8853 if (NILP (frame))
8854 frame = selected_frame;
8855 else
8856 CHECK_FRAME (frame);
8857 f = XFRAME (frame);
8858
8859 if (WINDOWP (f->tool_bar_window)
8860 || (w = XWINDOW (f->tool_bar_window),
8861 WINDOW_TOTAL_LINES (w) > 0))
8862 {
8863 update_tool_bar (f, 1);
8864 if (f->n_tool_bar_items)
8865 {
8866 build_desired_tool_bar_string (f);
8867 nlines = tool_bar_lines_needed (f);
8868 }
8869 }
8870
8871 return make_number (nlines);
8872 }
8873
8874
8875 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
8876 height should be changed. */
8877
8878 static int
8879 redisplay_tool_bar (f)
8880 struct frame *f;
8881 {
8882 struct window *w;
8883 struct it it;
8884 struct glyph_row *row;
8885 int change_height_p = 0;
8886
8887 #ifdef USE_GTK
8888 if (FRAME_EXTERNAL_TOOL_BAR (f))
8889 update_frame_tool_bar (f);
8890 return 0;
8891 #endif
8892
8893 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8894 do anything. This means you must start with tool-bar-lines
8895 non-zero to get the auto-sizing effect. Or in other words, you
8896 can turn off tool-bars by specifying tool-bar-lines zero. */
8897 if (!WINDOWP (f->tool_bar_window)
8898 || (w = XWINDOW (f->tool_bar_window),
8899 WINDOW_TOTAL_LINES (w) == 0))
8900 return 0;
8901
8902 /* Set up an iterator for the tool-bar window. */
8903 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8904 it.first_visible_x = 0;
8905 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8906 row = it.glyph_row;
8907
8908 /* Build a string that represents the contents of the tool-bar. */
8909 build_desired_tool_bar_string (f);
8910 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8911
8912 /* Display as many lines as needed to display all tool-bar items. */
8913 while (it.current_y < it.last_visible_y)
8914 display_tool_bar_line (&it);
8915
8916 /* It doesn't make much sense to try scrolling in the tool-bar
8917 window, so don't do it. */
8918 w->desired_matrix->no_scrolling_p = 1;
8919 w->must_be_updated_p = 1;
8920
8921 if (auto_resize_tool_bars_p)
8922 {
8923 int nlines;
8924
8925 /* If we couldn't display everything, change the tool-bar's
8926 height. */
8927 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8928 change_height_p = 1;
8929
8930 /* If there are blank lines at the end, except for a partially
8931 visible blank line at the end that is smaller than
8932 FRAME_LINE_HEIGHT, change the tool-bar's height. */
8933 row = it.glyph_row - 1;
8934 if (!row->displays_text_p
8935 && row->height >= FRAME_LINE_HEIGHT (f))
8936 change_height_p = 1;
8937
8938 /* If row displays tool-bar items, but is partially visible,
8939 change the tool-bar's height. */
8940 if (row->displays_text_p
8941 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8942 change_height_p = 1;
8943
8944 /* Resize windows as needed by changing the `tool-bar-lines'
8945 frame parameter. */
8946 if (change_height_p
8947 && (nlines = tool_bar_lines_needed (f),
8948 nlines != WINDOW_TOTAL_LINES (w)))
8949 {
8950 extern Lisp_Object Qtool_bar_lines;
8951 Lisp_Object frame;
8952 int old_height = WINDOW_TOTAL_LINES (w);
8953
8954 XSETFRAME (frame, f);
8955 clear_glyph_matrix (w->desired_matrix);
8956 Fmodify_frame_parameters (frame,
8957 Fcons (Fcons (Qtool_bar_lines,
8958 make_number (nlines)),
8959 Qnil));
8960 if (WINDOW_TOTAL_LINES (w) != old_height)
8961 fonts_changed_p = 1;
8962 }
8963 }
8964
8965 return change_height_p;
8966 }
8967
8968
8969 /* Get information about the tool-bar item which is displayed in GLYPH
8970 on frame F. Return in *PROP_IDX the index where tool-bar item
8971 properties start in F->tool_bar_items. Value is zero if
8972 GLYPH doesn't display a tool-bar item. */
8973
8974 static int
8975 tool_bar_item_info (f, glyph, prop_idx)
8976 struct frame *f;
8977 struct glyph *glyph;
8978 int *prop_idx;
8979 {
8980 Lisp_Object prop;
8981 int success_p;
8982 int charpos;
8983
8984 /* This function can be called asynchronously, which means we must
8985 exclude any possibility that Fget_text_property signals an
8986 error. */
8987 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8988 charpos = max (0, charpos);
8989
8990 /* Get the text property `menu-item' at pos. The value of that
8991 property is the start index of this item's properties in
8992 F->tool_bar_items. */
8993 prop = Fget_text_property (make_number (charpos),
8994 Qmenu_item, f->current_tool_bar_string);
8995 if (INTEGERP (prop))
8996 {
8997 *prop_idx = XINT (prop);
8998 success_p = 1;
8999 }
9000 else
9001 success_p = 0;
9002
9003 return success_p;
9004 }
9005
9006 \f
9007 /* Get information about the tool-bar item at position X/Y on frame F.
9008 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9009 the current matrix of the tool-bar window of F, or NULL if not
9010 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9011 item in F->tool_bar_items. Value is
9012
9013 -1 if X/Y is not on a tool-bar item
9014 0 if X/Y is on the same item that was highlighted before.
9015 1 otherwise. */
9016
9017 static int
9018 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9019 struct frame *f;
9020 int x, y;
9021 struct glyph **glyph;
9022 int *hpos, *vpos, *prop_idx;
9023 {
9024 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9025 struct window *w = XWINDOW (f->tool_bar_window);
9026 int area;
9027
9028 /* Find the glyph under X/Y. */
9029 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9030 if (*glyph == NULL)
9031 return -1;
9032
9033 /* Get the start of this tool-bar item's properties in
9034 f->tool_bar_items. */
9035 if (!tool_bar_item_info (f, *glyph, prop_idx))
9036 return -1;
9037
9038 /* Is mouse on the highlighted item? */
9039 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9040 && *vpos >= dpyinfo->mouse_face_beg_row
9041 && *vpos <= dpyinfo->mouse_face_end_row
9042 && (*vpos > dpyinfo->mouse_face_beg_row
9043 || *hpos >= dpyinfo->mouse_face_beg_col)
9044 && (*vpos < dpyinfo->mouse_face_end_row
9045 || *hpos < dpyinfo->mouse_face_end_col
9046 || dpyinfo->mouse_face_past_end))
9047 return 0;
9048
9049 return 1;
9050 }
9051
9052
9053 /* EXPORT:
9054 Handle mouse button event on the tool-bar of frame F, at
9055 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9056 0 for button release. MODIFIERS is event modifiers for button
9057 release. */
9058
9059 void
9060 handle_tool_bar_click (f, x, y, down_p, modifiers)
9061 struct frame *f;
9062 int x, y, down_p;
9063 unsigned int modifiers;
9064 {
9065 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9066 struct window *w = XWINDOW (f->tool_bar_window);
9067 int hpos, vpos, prop_idx;
9068 struct glyph *glyph;
9069 Lisp_Object enabled_p;
9070
9071 /* If not on the highlighted tool-bar item, return. */
9072 frame_to_window_pixel_xy (w, &x, &y);
9073 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9074 return;
9075
9076 /* If item is disabled, do nothing. */
9077 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9078 if (NILP (enabled_p))
9079 return;
9080
9081 if (down_p)
9082 {
9083 /* Show item in pressed state. */
9084 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9085 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9086 last_tool_bar_item = prop_idx;
9087 }
9088 else
9089 {
9090 Lisp_Object key, frame;
9091 struct input_event event;
9092 EVENT_INIT (event);
9093
9094 /* Show item in released state. */
9095 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9096 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9097
9098 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9099
9100 XSETFRAME (frame, f);
9101 event.kind = TOOL_BAR_EVENT;
9102 event.frame_or_window = frame;
9103 event.arg = frame;
9104 kbd_buffer_store_event (&event);
9105
9106 event.kind = TOOL_BAR_EVENT;
9107 event.frame_or_window = frame;
9108 event.arg = key;
9109 event.modifiers = modifiers;
9110 kbd_buffer_store_event (&event);
9111 last_tool_bar_item = -1;
9112 }
9113 }
9114
9115
9116 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9117 tool-bar window-relative coordinates X/Y. Called from
9118 note_mouse_highlight. */
9119
9120 static void
9121 note_tool_bar_highlight (f, x, y)
9122 struct frame *f;
9123 int x, y;
9124 {
9125 Lisp_Object window = f->tool_bar_window;
9126 struct window *w = XWINDOW (window);
9127 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9128 int hpos, vpos;
9129 struct glyph *glyph;
9130 struct glyph_row *row;
9131 int i;
9132 Lisp_Object enabled_p;
9133 int prop_idx;
9134 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9135 int mouse_down_p, rc;
9136
9137 /* Function note_mouse_highlight is called with negative x(y
9138 values when mouse moves outside of the frame. */
9139 if (x <= 0 || y <= 0)
9140 {
9141 clear_mouse_face (dpyinfo);
9142 return;
9143 }
9144
9145 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9146 if (rc < 0)
9147 {
9148 /* Not on tool-bar item. */
9149 clear_mouse_face (dpyinfo);
9150 return;
9151 }
9152 else if (rc == 0)
9153 /* On same tool-bar item as before. */
9154 goto set_help_echo;
9155
9156 clear_mouse_face (dpyinfo);
9157
9158 /* Mouse is down, but on different tool-bar item? */
9159 mouse_down_p = (dpyinfo->grabbed
9160 && f == last_mouse_frame
9161 && FRAME_LIVE_P (f));
9162 if (mouse_down_p
9163 && last_tool_bar_item != prop_idx)
9164 return;
9165
9166 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9167 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9168
9169 /* If tool-bar item is not enabled, don't highlight it. */
9170 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9171 if (!NILP (enabled_p))
9172 {
9173 /* Compute the x-position of the glyph. In front and past the
9174 image is a space. We include this in the highlighted area. */
9175 row = MATRIX_ROW (w->current_matrix, vpos);
9176 for (i = x = 0; i < hpos; ++i)
9177 x += row->glyphs[TEXT_AREA][i].pixel_width;
9178
9179 /* Record this as the current active region. */
9180 dpyinfo->mouse_face_beg_col = hpos;
9181 dpyinfo->mouse_face_beg_row = vpos;
9182 dpyinfo->mouse_face_beg_x = x;
9183 dpyinfo->mouse_face_beg_y = row->y;
9184 dpyinfo->mouse_face_past_end = 0;
9185
9186 dpyinfo->mouse_face_end_col = hpos + 1;
9187 dpyinfo->mouse_face_end_row = vpos;
9188 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9189 dpyinfo->mouse_face_end_y = row->y;
9190 dpyinfo->mouse_face_window = window;
9191 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9192
9193 /* Display it as active. */
9194 show_mouse_face (dpyinfo, draw);
9195 dpyinfo->mouse_face_image_state = draw;
9196 }
9197
9198 set_help_echo:
9199
9200 /* Set help_echo_string to a help string to display for this tool-bar item.
9201 XTread_socket does the rest. */
9202 help_echo_object = help_echo_window = Qnil;
9203 help_echo_pos = -1;
9204 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9205 if (NILP (help_echo_string))
9206 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9207 }
9208
9209 #endif /* HAVE_WINDOW_SYSTEM */
9210
9211
9212 \f
9213 /************************************************************************
9214 Horizontal scrolling
9215 ************************************************************************/
9216
9217 static int hscroll_window_tree P_ ((Lisp_Object));
9218 static int hscroll_windows P_ ((Lisp_Object));
9219
9220 /* For all leaf windows in the window tree rooted at WINDOW, set their
9221 hscroll value so that PT is (i) visible in the window, and (ii) so
9222 that it is not within a certain margin at the window's left and
9223 right border. Value is non-zero if any window's hscroll has been
9224 changed. */
9225
9226 static int
9227 hscroll_window_tree (window)
9228 Lisp_Object window;
9229 {
9230 int hscrolled_p = 0;
9231 int hscroll_relative_p = FLOATP (Vhscroll_step);
9232 int hscroll_step_abs = 0;
9233 double hscroll_step_rel = 0;
9234
9235 if (hscroll_relative_p)
9236 {
9237 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9238 if (hscroll_step_rel < 0)
9239 {
9240 hscroll_relative_p = 0;
9241 hscroll_step_abs = 0;
9242 }
9243 }
9244 else if (INTEGERP (Vhscroll_step))
9245 {
9246 hscroll_step_abs = XINT (Vhscroll_step);
9247 if (hscroll_step_abs < 0)
9248 hscroll_step_abs = 0;
9249 }
9250 else
9251 hscroll_step_abs = 0;
9252
9253 while (WINDOWP (window))
9254 {
9255 struct window *w = XWINDOW (window);
9256
9257 if (WINDOWP (w->hchild))
9258 hscrolled_p |= hscroll_window_tree (w->hchild);
9259 else if (WINDOWP (w->vchild))
9260 hscrolled_p |= hscroll_window_tree (w->vchild);
9261 else if (w->cursor.vpos >= 0)
9262 {
9263 int h_margin;
9264 int text_area_width;
9265 struct glyph_row *current_cursor_row
9266 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9267 struct glyph_row *desired_cursor_row
9268 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9269 struct glyph_row *cursor_row
9270 = (desired_cursor_row->enabled_p
9271 ? desired_cursor_row
9272 : current_cursor_row);
9273
9274 text_area_width = window_box_width (w, TEXT_AREA);
9275
9276 /* Scroll when cursor is inside this scroll margin. */
9277 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9278
9279 if ((XFASTINT (w->hscroll)
9280 && w->cursor.x <= h_margin)
9281 || (cursor_row->enabled_p
9282 && cursor_row->truncated_on_right_p
9283 && (w->cursor.x >= text_area_width - h_margin)))
9284 {
9285 struct it it;
9286 int hscroll;
9287 struct buffer *saved_current_buffer;
9288 int pt;
9289 int wanted_x;
9290
9291 /* Find point in a display of infinite width. */
9292 saved_current_buffer = current_buffer;
9293 current_buffer = XBUFFER (w->buffer);
9294
9295 if (w == XWINDOW (selected_window))
9296 pt = BUF_PT (current_buffer);
9297 else
9298 {
9299 pt = marker_position (w->pointm);
9300 pt = max (BEGV, pt);
9301 pt = min (ZV, pt);
9302 }
9303
9304 /* Move iterator to pt starting at cursor_row->start in
9305 a line with infinite width. */
9306 init_to_row_start (&it, w, cursor_row);
9307 it.last_visible_x = INFINITY;
9308 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9309 current_buffer = saved_current_buffer;
9310
9311 /* Position cursor in window. */
9312 if (!hscroll_relative_p && hscroll_step_abs == 0)
9313 hscroll = max (0, (it.current_x
9314 - (ITERATOR_AT_END_OF_LINE_P (&it)
9315 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9316 : (text_area_width / 2))))
9317 / FRAME_COLUMN_WIDTH (it.f);
9318 else if (w->cursor.x >= text_area_width - h_margin)
9319 {
9320 if (hscroll_relative_p)
9321 wanted_x = text_area_width * (1 - hscroll_step_rel)
9322 - h_margin;
9323 else
9324 wanted_x = text_area_width
9325 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9326 - h_margin;
9327 hscroll
9328 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9329 }
9330 else
9331 {
9332 if (hscroll_relative_p)
9333 wanted_x = text_area_width * hscroll_step_rel
9334 + h_margin;
9335 else
9336 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9337 + h_margin;
9338 hscroll
9339 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9340 }
9341 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9342
9343 /* Don't call Fset_window_hscroll if value hasn't
9344 changed because it will prevent redisplay
9345 optimizations. */
9346 if (XFASTINT (w->hscroll) != hscroll)
9347 {
9348 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9349 w->hscroll = make_number (hscroll);
9350 hscrolled_p = 1;
9351 }
9352 }
9353 }
9354
9355 window = w->next;
9356 }
9357
9358 /* Value is non-zero if hscroll of any leaf window has been changed. */
9359 return hscrolled_p;
9360 }
9361
9362
9363 /* Set hscroll so that cursor is visible and not inside horizontal
9364 scroll margins for all windows in the tree rooted at WINDOW. See
9365 also hscroll_window_tree above. Value is non-zero if any window's
9366 hscroll has been changed. If it has, desired matrices on the frame
9367 of WINDOW are cleared. */
9368
9369 static int
9370 hscroll_windows (window)
9371 Lisp_Object window;
9372 {
9373 int hscrolled_p;
9374
9375 if (automatic_hscrolling_p)
9376 {
9377 hscrolled_p = hscroll_window_tree (window);
9378 if (hscrolled_p)
9379 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9380 }
9381 else
9382 hscrolled_p = 0;
9383 return hscrolled_p;
9384 }
9385
9386
9387 \f
9388 /************************************************************************
9389 Redisplay
9390 ************************************************************************/
9391
9392 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9393 to a non-zero value. This is sometimes handy to have in a debugger
9394 session. */
9395
9396 #if GLYPH_DEBUG
9397
9398 /* First and last unchanged row for try_window_id. */
9399
9400 int debug_first_unchanged_at_end_vpos;
9401 int debug_last_unchanged_at_beg_vpos;
9402
9403 /* Delta vpos and y. */
9404
9405 int debug_dvpos, debug_dy;
9406
9407 /* Delta in characters and bytes for try_window_id. */
9408
9409 int debug_delta, debug_delta_bytes;
9410
9411 /* Values of window_end_pos and window_end_vpos at the end of
9412 try_window_id. */
9413
9414 EMACS_INT debug_end_pos, debug_end_vpos;
9415
9416 /* Append a string to W->desired_matrix->method. FMT is a printf
9417 format string. A1...A9 are a supplement for a variable-length
9418 argument list. If trace_redisplay_p is non-zero also printf the
9419 resulting string to stderr. */
9420
9421 static void
9422 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9423 struct window *w;
9424 char *fmt;
9425 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9426 {
9427 char buffer[512];
9428 char *method = w->desired_matrix->method;
9429 int len = strlen (method);
9430 int size = sizeof w->desired_matrix->method;
9431 int remaining = size - len - 1;
9432
9433 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9434 if (len && remaining)
9435 {
9436 method[len] = '|';
9437 --remaining, ++len;
9438 }
9439
9440 strncpy (method + len, buffer, remaining);
9441
9442 if (trace_redisplay_p)
9443 fprintf (stderr, "%p (%s): %s\n",
9444 w,
9445 ((BUFFERP (w->buffer)
9446 && STRINGP (XBUFFER (w->buffer)->name))
9447 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9448 : "no buffer"),
9449 buffer);
9450 }
9451
9452 #endif /* GLYPH_DEBUG */
9453
9454
9455 /* Value is non-zero if all changes in window W, which displays
9456 current_buffer, are in the text between START and END. START is a
9457 buffer position, END is given as a distance from Z. Used in
9458 redisplay_internal for display optimization. */
9459
9460 static INLINE int
9461 text_outside_line_unchanged_p (w, start, end)
9462 struct window *w;
9463 int start, end;
9464 {
9465 int unchanged_p = 1;
9466
9467 /* If text or overlays have changed, see where. */
9468 if (XFASTINT (w->last_modified) < MODIFF
9469 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9470 {
9471 /* Gap in the line? */
9472 if (GPT < start || Z - GPT < end)
9473 unchanged_p = 0;
9474
9475 /* Changes start in front of the line, or end after it? */
9476 if (unchanged_p
9477 && (BEG_UNCHANGED < start - 1
9478 || END_UNCHANGED < end))
9479 unchanged_p = 0;
9480
9481 /* If selective display, can't optimize if changes start at the
9482 beginning of the line. */
9483 if (unchanged_p
9484 && INTEGERP (current_buffer->selective_display)
9485 && XINT (current_buffer->selective_display) > 0
9486 && (BEG_UNCHANGED < start || GPT <= start))
9487 unchanged_p = 0;
9488
9489 /* If there are overlays at the start or end of the line, these
9490 may have overlay strings with newlines in them. A change at
9491 START, for instance, may actually concern the display of such
9492 overlay strings as well, and they are displayed on different
9493 lines. So, quickly rule out this case. (For the future, it
9494 might be desirable to implement something more telling than
9495 just BEG/END_UNCHANGED.) */
9496 if (unchanged_p)
9497 {
9498 if (BEG + BEG_UNCHANGED == start
9499 && overlay_touches_p (start))
9500 unchanged_p = 0;
9501 if (END_UNCHANGED == end
9502 && overlay_touches_p (Z - end))
9503 unchanged_p = 0;
9504 }
9505 }
9506
9507 return unchanged_p;
9508 }
9509
9510
9511 /* Do a frame update, taking possible shortcuts into account. This is
9512 the main external entry point for redisplay.
9513
9514 If the last redisplay displayed an echo area message and that message
9515 is no longer requested, we clear the echo area or bring back the
9516 mini-buffer if that is in use. */
9517
9518 void
9519 redisplay ()
9520 {
9521 redisplay_internal (0);
9522 }
9523
9524
9525 static Lisp_Object
9526 overlay_arrow_string_or_property (var, pbitmap)
9527 Lisp_Object var;
9528 int *pbitmap;
9529 {
9530 Lisp_Object pstr = Fget (var, Qoverlay_arrow_string);
9531 Lisp_Object bitmap;
9532
9533 if (pbitmap)
9534 {
9535 *pbitmap = 0;
9536 if (bitmap = Fget (var, Qoverlay_arrow_bitmap), INTEGERP (bitmap))
9537 *pbitmap = XINT (bitmap);
9538 }
9539
9540 if (!NILP (pstr))
9541 return pstr;
9542 return Voverlay_arrow_string;
9543 }
9544
9545 /* Return 1 if there are any overlay-arrows in current_buffer. */
9546 static int
9547 overlay_arrow_in_current_buffer_p ()
9548 {
9549 Lisp_Object vlist;
9550
9551 for (vlist = Voverlay_arrow_variable_list;
9552 CONSP (vlist);
9553 vlist = XCDR (vlist))
9554 {
9555 Lisp_Object var = XCAR (vlist);
9556 Lisp_Object val;
9557
9558 if (!SYMBOLP (var))
9559 continue;
9560 val = find_symbol_value (var);
9561 if (MARKERP (val)
9562 && current_buffer == XMARKER (val)->buffer)
9563 return 1;
9564 }
9565 return 0;
9566 }
9567
9568
9569 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9570 has changed. */
9571
9572 static int
9573 overlay_arrows_changed_p ()
9574 {
9575 Lisp_Object vlist;
9576
9577 for (vlist = Voverlay_arrow_variable_list;
9578 CONSP (vlist);
9579 vlist = XCDR (vlist))
9580 {
9581 Lisp_Object var = XCAR (vlist);
9582 Lisp_Object val, pstr;
9583
9584 if (!SYMBOLP (var))
9585 continue;
9586 val = find_symbol_value (var);
9587 if (!MARKERP (val))
9588 continue;
9589 if (! EQ (COERCE_MARKER (val),
9590 Fget (var, Qlast_arrow_position))
9591 || ! (pstr = overlay_arrow_string_or_property (var, 0),
9592 EQ (pstr, Fget (var, Qlast_arrow_string))))
9593 return 1;
9594 }
9595 return 0;
9596 }
9597
9598 /* Mark overlay arrows to be updated on next redisplay. */
9599
9600 static void
9601 update_overlay_arrows (up_to_date)
9602 int up_to_date;
9603 {
9604 Lisp_Object vlist;
9605
9606 for (vlist = Voverlay_arrow_variable_list;
9607 CONSP (vlist);
9608 vlist = XCDR (vlist))
9609 {
9610 Lisp_Object var = XCAR (vlist);
9611
9612 if (!SYMBOLP (var))
9613 continue;
9614
9615 if (up_to_date > 0)
9616 {
9617 Lisp_Object val = find_symbol_value (var);
9618 Fput (var, Qlast_arrow_position,
9619 COERCE_MARKER (val));
9620 Fput (var, Qlast_arrow_string,
9621 overlay_arrow_string_or_property (var, 0));
9622 }
9623 else if (up_to_date < 0
9624 || !NILP (Fget (var, Qlast_arrow_position)))
9625 {
9626 Fput (var, Qlast_arrow_position, Qt);
9627 Fput (var, Qlast_arrow_string, Qt);
9628 }
9629 }
9630 }
9631
9632
9633 /* Return overlay arrow string at row, or nil. */
9634
9635 static Lisp_Object
9636 overlay_arrow_at_row (f, row, pbitmap)
9637 struct frame *f;
9638 struct glyph_row *row;
9639 int *pbitmap;
9640 {
9641 Lisp_Object vlist;
9642
9643 for (vlist = Voverlay_arrow_variable_list;
9644 CONSP (vlist);
9645 vlist = XCDR (vlist))
9646 {
9647 Lisp_Object var = XCAR (vlist);
9648 Lisp_Object val;
9649
9650 if (!SYMBOLP (var))
9651 continue;
9652
9653 val = find_symbol_value (var);
9654
9655 if (MARKERP (val)
9656 && current_buffer == XMARKER (val)->buffer
9657 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
9658 {
9659 val = overlay_arrow_string_or_property (var, pbitmap);
9660 if (FRAME_WINDOW_P (f))
9661 return Qt;
9662 else if (STRINGP (val))
9663 return val;
9664 break;
9665 }
9666 }
9667
9668 *pbitmap = 0;
9669 return Qnil;
9670 }
9671
9672 /* Return 1 if point moved out of or into a composition. Otherwise
9673 return 0. PREV_BUF and PREV_PT are the last point buffer and
9674 position. BUF and PT are the current point buffer and position. */
9675
9676 int
9677 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9678 struct buffer *prev_buf, *buf;
9679 int prev_pt, pt;
9680 {
9681 EMACS_INT start, end;
9682 Lisp_Object prop;
9683 Lisp_Object buffer;
9684
9685 XSETBUFFER (buffer, buf);
9686 /* Check a composition at the last point if point moved within the
9687 same buffer. */
9688 if (prev_buf == buf)
9689 {
9690 if (prev_pt == pt)
9691 /* Point didn't move. */
9692 return 0;
9693
9694 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9695 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9696 && COMPOSITION_VALID_P (start, end, prop)
9697 && start < prev_pt && end > prev_pt)
9698 /* The last point was within the composition. Return 1 iff
9699 point moved out of the composition. */
9700 return (pt <= start || pt >= end);
9701 }
9702
9703 /* Check a composition at the current point. */
9704 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9705 && find_composition (pt, -1, &start, &end, &prop, buffer)
9706 && COMPOSITION_VALID_P (start, end, prop)
9707 && start < pt && end > pt);
9708 }
9709
9710
9711 /* Reconsider the setting of B->clip_changed which is displayed
9712 in window W. */
9713
9714 static INLINE void
9715 reconsider_clip_changes (w, b)
9716 struct window *w;
9717 struct buffer *b;
9718 {
9719 if (b->clip_changed
9720 && !NILP (w->window_end_valid)
9721 && w->current_matrix->buffer == b
9722 && w->current_matrix->zv == BUF_ZV (b)
9723 && w->current_matrix->begv == BUF_BEGV (b))
9724 b->clip_changed = 0;
9725
9726 /* If display wasn't paused, and W is not a tool bar window, see if
9727 point has been moved into or out of a composition. In that case,
9728 we set b->clip_changed to 1 to force updating the screen. If
9729 b->clip_changed has already been set to 1, we can skip this
9730 check. */
9731 if (!b->clip_changed
9732 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9733 {
9734 int pt;
9735
9736 if (w == XWINDOW (selected_window))
9737 pt = BUF_PT (current_buffer);
9738 else
9739 pt = marker_position (w->pointm);
9740
9741 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
9742 || pt != XINT (w->last_point))
9743 && check_point_in_composition (w->current_matrix->buffer,
9744 XINT (w->last_point),
9745 XBUFFER (w->buffer), pt))
9746 b->clip_changed = 1;
9747 }
9748 }
9749 \f
9750
9751 /* Select FRAME to forward the values of frame-local variables into C
9752 variables so that the redisplay routines can access those values
9753 directly. */
9754
9755 static void
9756 select_frame_for_redisplay (frame)
9757 Lisp_Object frame;
9758 {
9759 Lisp_Object tail, sym, val;
9760 Lisp_Object old = selected_frame;
9761
9762 selected_frame = frame;
9763
9764 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
9765 if (CONSP (XCAR (tail))
9766 && (sym = XCAR (XCAR (tail)),
9767 SYMBOLP (sym))
9768 && (sym = indirect_variable (sym),
9769 val = SYMBOL_VALUE (sym),
9770 (BUFFER_LOCAL_VALUEP (val)
9771 || SOME_BUFFER_LOCAL_VALUEP (val)))
9772 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9773 Fsymbol_value (sym);
9774
9775 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
9776 if (CONSP (XCAR (tail))
9777 && (sym = XCAR (XCAR (tail)),
9778 SYMBOLP (sym))
9779 && (sym = indirect_variable (sym),
9780 val = SYMBOL_VALUE (sym),
9781 (BUFFER_LOCAL_VALUEP (val)
9782 || SOME_BUFFER_LOCAL_VALUEP (val)))
9783 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9784 Fsymbol_value (sym);
9785 }
9786
9787
9788 #define STOP_POLLING \
9789 do { if (! polling_stopped_here) stop_polling (); \
9790 polling_stopped_here = 1; } while (0)
9791
9792 #define RESUME_POLLING \
9793 do { if (polling_stopped_here) start_polling (); \
9794 polling_stopped_here = 0; } while (0)
9795
9796
9797 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9798 response to any user action; therefore, we should preserve the echo
9799 area. (Actually, our caller does that job.) Perhaps in the future
9800 avoid recentering windows if it is not necessary; currently that
9801 causes some problems. */
9802
9803 static void
9804 redisplay_internal (preserve_echo_area)
9805 int preserve_echo_area;
9806 {
9807 struct window *w = XWINDOW (selected_window);
9808 struct frame *f = XFRAME (w->frame);
9809 int pause;
9810 int must_finish = 0;
9811 struct text_pos tlbufpos, tlendpos;
9812 int number_of_visible_frames;
9813 int count;
9814 struct frame *sf = SELECTED_FRAME ();
9815 int polling_stopped_here = 0;
9816
9817 /* Non-zero means redisplay has to consider all windows on all
9818 frames. Zero means, only selected_window is considered. */
9819 int consider_all_windows_p;
9820
9821 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9822
9823 /* No redisplay if running in batch mode or frame is not yet fully
9824 initialized, or redisplay is explicitly turned off by setting
9825 Vinhibit_redisplay. */
9826 if (noninteractive
9827 || !NILP (Vinhibit_redisplay)
9828 || !f->glyphs_initialized_p)
9829 return;
9830
9831 /* The flag redisplay_performed_directly_p is set by
9832 direct_output_for_insert when it already did the whole screen
9833 update necessary. */
9834 if (redisplay_performed_directly_p)
9835 {
9836 redisplay_performed_directly_p = 0;
9837 if (!hscroll_windows (selected_window))
9838 return;
9839 }
9840
9841 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9842 if (popup_activated ())
9843 return;
9844 #endif
9845
9846 /* I don't think this happens but let's be paranoid. */
9847 if (redisplaying_p)
9848 return;
9849
9850 /* Record a function that resets redisplaying_p to its old value
9851 when we leave this function. */
9852 count = SPECPDL_INDEX ();
9853 record_unwind_protect (unwind_redisplay,
9854 Fcons (make_number (redisplaying_p), selected_frame));
9855 ++redisplaying_p;
9856 specbind (Qinhibit_free_realized_faces, Qnil);
9857
9858 retry:
9859 pause = 0;
9860 reconsider_clip_changes (w, current_buffer);
9861
9862 /* If new fonts have been loaded that make a glyph matrix adjustment
9863 necessary, do it. */
9864 if (fonts_changed_p)
9865 {
9866 adjust_glyphs (NULL);
9867 ++windows_or_buffers_changed;
9868 fonts_changed_p = 0;
9869 }
9870
9871 /* If face_change_count is non-zero, init_iterator will free all
9872 realized faces, which includes the faces referenced from current
9873 matrices. So, we can't reuse current matrices in this case. */
9874 if (face_change_count)
9875 ++windows_or_buffers_changed;
9876
9877 if (! FRAME_WINDOW_P (sf)
9878 && previous_terminal_frame != sf)
9879 {
9880 /* Since frames on an ASCII terminal share the same display
9881 area, displaying a different frame means redisplay the whole
9882 thing. */
9883 windows_or_buffers_changed++;
9884 SET_FRAME_GARBAGED (sf);
9885 XSETFRAME (Vterminal_frame, sf);
9886 }
9887 previous_terminal_frame = sf;
9888
9889 /* Set the visible flags for all frames. Do this before checking
9890 for resized or garbaged frames; they want to know if their frames
9891 are visible. See the comment in frame.h for
9892 FRAME_SAMPLE_VISIBILITY. */
9893 {
9894 Lisp_Object tail, frame;
9895
9896 number_of_visible_frames = 0;
9897
9898 FOR_EACH_FRAME (tail, frame)
9899 {
9900 struct frame *f = XFRAME (frame);
9901
9902 FRAME_SAMPLE_VISIBILITY (f);
9903 if (FRAME_VISIBLE_P (f))
9904 ++number_of_visible_frames;
9905 clear_desired_matrices (f);
9906 }
9907 }
9908
9909 /* Notice any pending interrupt request to change frame size. */
9910 do_pending_window_change (1);
9911
9912 /* Clear frames marked as garbaged. */
9913 if (frame_garbaged)
9914 clear_garbaged_frames ();
9915
9916 /* Build menubar and tool-bar items. */
9917 prepare_menu_bars ();
9918
9919 if (windows_or_buffers_changed)
9920 update_mode_lines++;
9921
9922 /* Detect case that we need to write or remove a star in the mode line. */
9923 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
9924 {
9925 w->update_mode_line = Qt;
9926 if (buffer_shared > 1)
9927 update_mode_lines++;
9928 }
9929
9930 /* If %c is in the mode line, update it if needed. */
9931 if (!NILP (w->column_number_displayed)
9932 /* This alternative quickly identifies a common case
9933 where no change is needed. */
9934 && !(PT == XFASTINT (w->last_point)
9935 && XFASTINT (w->last_modified) >= MODIFF
9936 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9937 && (XFASTINT (w->column_number_displayed)
9938 != (int) current_column ())) /* iftc */
9939 w->update_mode_line = Qt;
9940
9941 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
9942
9943 /* The variable buffer_shared is set in redisplay_window and
9944 indicates that we redisplay a buffer in different windows. See
9945 there. */
9946 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
9947 || cursor_type_changed);
9948
9949 /* If specs for an arrow have changed, do thorough redisplay
9950 to ensure we remove any arrow that should no longer exist. */
9951 if (overlay_arrows_changed_p ())
9952 consider_all_windows_p = windows_or_buffers_changed = 1;
9953
9954 /* Normally the message* functions will have already displayed and
9955 updated the echo area, but the frame may have been trashed, or
9956 the update may have been preempted, so display the echo area
9957 again here. Checking message_cleared_p captures the case that
9958 the echo area should be cleared. */
9959 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
9960 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
9961 || (message_cleared_p
9962 && minibuf_level == 0
9963 /* If the mini-window is currently selected, this means the
9964 echo-area doesn't show through. */
9965 && !MINI_WINDOW_P (XWINDOW (selected_window))))
9966 {
9967 int window_height_changed_p = echo_area_display (0);
9968 must_finish = 1;
9969
9970 /* If we don't display the current message, don't clear the
9971 message_cleared_p flag, because, if we did, we wouldn't clear
9972 the echo area in the next redisplay which doesn't preserve
9973 the echo area. */
9974 if (!display_last_displayed_message_p)
9975 message_cleared_p = 0;
9976
9977 if (fonts_changed_p)
9978 goto retry;
9979 else if (window_height_changed_p)
9980 {
9981 consider_all_windows_p = 1;
9982 ++update_mode_lines;
9983 ++windows_or_buffers_changed;
9984
9985 /* If window configuration was changed, frames may have been
9986 marked garbaged. Clear them or we will experience
9987 surprises wrt scrolling. */
9988 if (frame_garbaged)
9989 clear_garbaged_frames ();
9990 }
9991 }
9992 else if (EQ (selected_window, minibuf_window)
9993 && (current_buffer->clip_changed
9994 || XFASTINT (w->last_modified) < MODIFF
9995 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9996 && resize_mini_window (w, 0))
9997 {
9998 /* Resized active mini-window to fit the size of what it is
9999 showing if its contents might have changed. */
10000 must_finish = 1;
10001 consider_all_windows_p = 1;
10002 ++windows_or_buffers_changed;
10003 ++update_mode_lines;
10004
10005 /* If window configuration was changed, frames may have been
10006 marked garbaged. Clear them or we will experience
10007 surprises wrt scrolling. */
10008 if (frame_garbaged)
10009 clear_garbaged_frames ();
10010 }
10011
10012
10013 /* If showing the region, and mark has changed, we must redisplay
10014 the whole window. The assignment to this_line_start_pos prevents
10015 the optimization directly below this if-statement. */
10016 if (((!NILP (Vtransient_mark_mode)
10017 && !NILP (XBUFFER (w->buffer)->mark_active))
10018 != !NILP (w->region_showing))
10019 || (!NILP (w->region_showing)
10020 && !EQ (w->region_showing,
10021 Fmarker_position (XBUFFER (w->buffer)->mark))))
10022 CHARPOS (this_line_start_pos) = 0;
10023
10024 /* Optimize the case that only the line containing the cursor in the
10025 selected window has changed. Variables starting with this_ are
10026 set in display_line and record information about the line
10027 containing the cursor. */
10028 tlbufpos = this_line_start_pos;
10029 tlendpos = this_line_end_pos;
10030 if (!consider_all_windows_p
10031 && CHARPOS (tlbufpos) > 0
10032 && NILP (w->update_mode_line)
10033 && !current_buffer->clip_changed
10034 && !current_buffer->prevent_redisplay_optimizations_p
10035 && FRAME_VISIBLE_P (XFRAME (w->frame))
10036 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10037 /* Make sure recorded data applies to current buffer, etc. */
10038 && this_line_buffer == current_buffer
10039 && current_buffer == XBUFFER (w->buffer)
10040 && NILP (w->force_start)
10041 && NILP (w->optional_new_start)
10042 /* Point must be on the line that we have info recorded about. */
10043 && PT >= CHARPOS (tlbufpos)
10044 && PT <= Z - CHARPOS (tlendpos)
10045 /* All text outside that line, including its final newline,
10046 must be unchanged */
10047 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10048 CHARPOS (tlendpos)))
10049 {
10050 if (CHARPOS (tlbufpos) > BEGV
10051 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10052 && (CHARPOS (tlbufpos) == ZV
10053 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10054 /* Former continuation line has disappeared by becoming empty */
10055 goto cancel;
10056 else if (XFASTINT (w->last_modified) < MODIFF
10057 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10058 || MINI_WINDOW_P (w))
10059 {
10060 /* We have to handle the case of continuation around a
10061 wide-column character (See the comment in indent.c around
10062 line 885).
10063
10064 For instance, in the following case:
10065
10066 -------- Insert --------
10067 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10068 J_I_ ==> J_I_ `^^' are cursors.
10069 ^^ ^^
10070 -------- --------
10071
10072 As we have to redraw the line above, we should goto cancel. */
10073
10074 struct it it;
10075 int line_height_before = this_line_pixel_height;
10076
10077 /* Note that start_display will handle the case that the
10078 line starting at tlbufpos is a continuation lines. */
10079 start_display (&it, w, tlbufpos);
10080
10081 /* Implementation note: It this still necessary? */
10082 if (it.current_x != this_line_start_x)
10083 goto cancel;
10084
10085 TRACE ((stderr, "trying display optimization 1\n"));
10086 w->cursor.vpos = -1;
10087 overlay_arrow_seen = 0;
10088 it.vpos = this_line_vpos;
10089 it.current_y = this_line_y;
10090 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10091 display_line (&it);
10092
10093 /* If line contains point, is not continued,
10094 and ends at same distance from eob as before, we win */
10095 if (w->cursor.vpos >= 0
10096 /* Line is not continued, otherwise this_line_start_pos
10097 would have been set to 0 in display_line. */
10098 && CHARPOS (this_line_start_pos)
10099 /* Line ends as before. */
10100 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10101 /* Line has same height as before. Otherwise other lines
10102 would have to be shifted up or down. */
10103 && this_line_pixel_height == line_height_before)
10104 {
10105 /* If this is not the window's last line, we must adjust
10106 the charstarts of the lines below. */
10107 if (it.current_y < it.last_visible_y)
10108 {
10109 struct glyph_row *row
10110 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10111 int delta, delta_bytes;
10112
10113 if (Z - CHARPOS (tlendpos) == ZV)
10114 {
10115 /* This line ends at end of (accessible part of)
10116 buffer. There is no newline to count. */
10117 delta = (Z
10118 - CHARPOS (tlendpos)
10119 - MATRIX_ROW_START_CHARPOS (row));
10120 delta_bytes = (Z_BYTE
10121 - BYTEPOS (tlendpos)
10122 - MATRIX_ROW_START_BYTEPOS (row));
10123 }
10124 else
10125 {
10126 /* This line ends in a newline. Must take
10127 account of the newline and the rest of the
10128 text that follows. */
10129 delta = (Z
10130 - CHARPOS (tlendpos)
10131 - MATRIX_ROW_START_CHARPOS (row));
10132 delta_bytes = (Z_BYTE
10133 - BYTEPOS (tlendpos)
10134 - MATRIX_ROW_START_BYTEPOS (row));
10135 }
10136
10137 increment_matrix_positions (w->current_matrix,
10138 this_line_vpos + 1,
10139 w->current_matrix->nrows,
10140 delta, delta_bytes);
10141 }
10142
10143 /* If this row displays text now but previously didn't,
10144 or vice versa, w->window_end_vpos may have to be
10145 adjusted. */
10146 if ((it.glyph_row - 1)->displays_text_p)
10147 {
10148 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10149 XSETINT (w->window_end_vpos, this_line_vpos);
10150 }
10151 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10152 && this_line_vpos > 0)
10153 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10154 w->window_end_valid = Qnil;
10155
10156 /* Update hint: No need to try to scroll in update_window. */
10157 w->desired_matrix->no_scrolling_p = 1;
10158
10159 #if GLYPH_DEBUG
10160 *w->desired_matrix->method = 0;
10161 debug_method_add (w, "optimization 1");
10162 #endif
10163 #ifdef HAVE_WINDOW_SYSTEM
10164 update_window_fringes (w, 0);
10165 #endif
10166 goto update;
10167 }
10168 else
10169 goto cancel;
10170 }
10171 else if (/* Cursor position hasn't changed. */
10172 PT == XFASTINT (w->last_point)
10173 /* Make sure the cursor was last displayed
10174 in this window. Otherwise we have to reposition it. */
10175 && 0 <= w->cursor.vpos
10176 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10177 {
10178 if (!must_finish)
10179 {
10180 do_pending_window_change (1);
10181
10182 /* We used to always goto end_of_redisplay here, but this
10183 isn't enough if we have a blinking cursor. */
10184 if (w->cursor_off_p == w->last_cursor_off_p)
10185 goto end_of_redisplay;
10186 }
10187 goto update;
10188 }
10189 /* If highlighting the region, or if the cursor is in the echo area,
10190 then we can't just move the cursor. */
10191 else if (! (!NILP (Vtransient_mark_mode)
10192 && !NILP (current_buffer->mark_active))
10193 && (EQ (selected_window, current_buffer->last_selected_window)
10194 || highlight_nonselected_windows)
10195 && NILP (w->region_showing)
10196 && NILP (Vshow_trailing_whitespace)
10197 && !cursor_in_echo_area)
10198 {
10199 struct it it;
10200 struct glyph_row *row;
10201
10202 /* Skip from tlbufpos to PT and see where it is. Note that
10203 PT may be in invisible text. If so, we will end at the
10204 next visible position. */
10205 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10206 NULL, DEFAULT_FACE_ID);
10207 it.current_x = this_line_start_x;
10208 it.current_y = this_line_y;
10209 it.vpos = this_line_vpos;
10210
10211 /* The call to move_it_to stops in front of PT, but
10212 moves over before-strings. */
10213 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10214
10215 if (it.vpos == this_line_vpos
10216 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10217 row->enabled_p))
10218 {
10219 xassert (this_line_vpos == it.vpos);
10220 xassert (this_line_y == it.current_y);
10221 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10222 #if GLYPH_DEBUG
10223 *w->desired_matrix->method = 0;
10224 debug_method_add (w, "optimization 3");
10225 #endif
10226 goto update;
10227 }
10228 else
10229 goto cancel;
10230 }
10231
10232 cancel:
10233 /* Text changed drastically or point moved off of line. */
10234 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10235 }
10236
10237 CHARPOS (this_line_start_pos) = 0;
10238 consider_all_windows_p |= buffer_shared > 1;
10239 ++clear_face_cache_count;
10240
10241
10242 /* Build desired matrices, and update the display. If
10243 consider_all_windows_p is non-zero, do it for all windows on all
10244 frames. Otherwise do it for selected_window, only. */
10245
10246 if (consider_all_windows_p)
10247 {
10248 Lisp_Object tail, frame;
10249 int i, n = 0, size = 50;
10250 struct frame **updated
10251 = (struct frame **) alloca (size * sizeof *updated);
10252
10253 /* Clear the face cache eventually. */
10254 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10255 {
10256 clear_face_cache (0);
10257 clear_face_cache_count = 0;
10258 }
10259
10260 /* Recompute # windows showing selected buffer. This will be
10261 incremented each time such a window is displayed. */
10262 buffer_shared = 0;
10263
10264 FOR_EACH_FRAME (tail, frame)
10265 {
10266 struct frame *f = XFRAME (frame);
10267
10268 if (FRAME_WINDOW_P (f) || f == sf)
10269 {
10270 if (! EQ (frame, selected_frame))
10271 /* Select the frame, for the sake of frame-local
10272 variables. */
10273 select_frame_for_redisplay (frame);
10274
10275 #ifdef HAVE_WINDOW_SYSTEM
10276 if (clear_face_cache_count % 50 == 0
10277 && FRAME_WINDOW_P (f))
10278 clear_image_cache (f, 0);
10279 #endif /* HAVE_WINDOW_SYSTEM */
10280
10281 /* Mark all the scroll bars to be removed; we'll redeem
10282 the ones we want when we redisplay their windows. */
10283 if (condemn_scroll_bars_hook)
10284 condemn_scroll_bars_hook (f);
10285
10286 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10287 redisplay_windows (FRAME_ROOT_WINDOW (f));
10288
10289 /* Any scroll bars which redisplay_windows should have
10290 nuked should now go away. */
10291 if (judge_scroll_bars_hook)
10292 judge_scroll_bars_hook (f);
10293
10294 /* If fonts changed, display again. */
10295 /* ??? rms: I suspect it is a mistake to jump all the way
10296 back to retry here. It should just retry this frame. */
10297 if (fonts_changed_p)
10298 goto retry;
10299
10300 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10301 {
10302 /* See if we have to hscroll. */
10303 if (hscroll_windows (f->root_window))
10304 goto retry;
10305
10306 /* Prevent various kinds of signals during display
10307 update. stdio is not robust about handling
10308 signals, which can cause an apparent I/O
10309 error. */
10310 if (interrupt_input)
10311 unrequest_sigio ();
10312 STOP_POLLING;
10313
10314 /* Update the display. */
10315 set_window_update_flags (XWINDOW (f->root_window), 1);
10316 pause |= update_frame (f, 0, 0);
10317 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10318 if (pause)
10319 break;
10320 #endif
10321
10322 if (n == size)
10323 {
10324 int nbytes = size * sizeof *updated;
10325 struct frame **p = (struct frame **) alloca (2 * nbytes);
10326 bcopy (updated, p, nbytes);
10327 size *= 2;
10328 }
10329
10330 updated[n++] = f;
10331 }
10332 }
10333 }
10334
10335 if (!pause)
10336 {
10337 /* Do the mark_window_display_accurate after all windows have
10338 been redisplayed because this call resets flags in buffers
10339 which are needed for proper redisplay. */
10340 for (i = 0; i < n; ++i)
10341 {
10342 struct frame *f = updated[i];
10343 mark_window_display_accurate (f->root_window, 1);
10344 if (frame_up_to_date_hook)
10345 frame_up_to_date_hook (f);
10346 }
10347 }
10348 }
10349 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10350 {
10351 Lisp_Object mini_window;
10352 struct frame *mini_frame;
10353
10354 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10355 /* Use list_of_error, not Qerror, so that
10356 we catch only errors and don't run the debugger. */
10357 internal_condition_case_1 (redisplay_window_1, selected_window,
10358 list_of_error,
10359 redisplay_window_error);
10360
10361 /* Compare desired and current matrices, perform output. */
10362
10363 update:
10364 /* If fonts changed, display again. */
10365 if (fonts_changed_p)
10366 goto retry;
10367
10368 /* Prevent various kinds of signals during display update.
10369 stdio is not robust about handling signals,
10370 which can cause an apparent I/O error. */
10371 if (interrupt_input)
10372 unrequest_sigio ();
10373 STOP_POLLING;
10374
10375 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10376 {
10377 if (hscroll_windows (selected_window))
10378 goto retry;
10379
10380 XWINDOW (selected_window)->must_be_updated_p = 1;
10381 pause = update_frame (sf, 0, 0);
10382 }
10383
10384 /* We may have called echo_area_display at the top of this
10385 function. If the echo area is on another frame, that may
10386 have put text on a frame other than the selected one, so the
10387 above call to update_frame would not have caught it. Catch
10388 it here. */
10389 mini_window = FRAME_MINIBUF_WINDOW (sf);
10390 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10391
10392 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10393 {
10394 XWINDOW (mini_window)->must_be_updated_p = 1;
10395 pause |= update_frame (mini_frame, 0, 0);
10396 if (!pause && hscroll_windows (mini_window))
10397 goto retry;
10398 }
10399 }
10400
10401 /* If display was paused because of pending input, make sure we do a
10402 thorough update the next time. */
10403 if (pause)
10404 {
10405 /* Prevent the optimization at the beginning of
10406 redisplay_internal that tries a single-line update of the
10407 line containing the cursor in the selected window. */
10408 CHARPOS (this_line_start_pos) = 0;
10409
10410 /* Let the overlay arrow be updated the next time. */
10411 update_overlay_arrows (0);
10412
10413 /* If we pause after scrolling, some rows in the current
10414 matrices of some windows are not valid. */
10415 if (!WINDOW_FULL_WIDTH_P (w)
10416 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10417 update_mode_lines = 1;
10418 }
10419 else
10420 {
10421 if (!consider_all_windows_p)
10422 {
10423 /* This has already been done above if
10424 consider_all_windows_p is set. */
10425 mark_window_display_accurate_1 (w, 1);
10426
10427 /* Say overlay arrows are up to date. */
10428 update_overlay_arrows (1);
10429
10430 if (frame_up_to_date_hook != 0)
10431 frame_up_to_date_hook (sf);
10432 }
10433
10434 update_mode_lines = 0;
10435 windows_or_buffers_changed = 0;
10436 cursor_type_changed = 0;
10437 }
10438
10439 /* Start SIGIO interrupts coming again. Having them off during the
10440 code above makes it less likely one will discard output, but not
10441 impossible, since there might be stuff in the system buffer here.
10442 But it is much hairier to try to do anything about that. */
10443 if (interrupt_input)
10444 request_sigio ();
10445 RESUME_POLLING;
10446
10447 /* If a frame has become visible which was not before, redisplay
10448 again, so that we display it. Expose events for such a frame
10449 (which it gets when becoming visible) don't call the parts of
10450 redisplay constructing glyphs, so simply exposing a frame won't
10451 display anything in this case. So, we have to display these
10452 frames here explicitly. */
10453 if (!pause)
10454 {
10455 Lisp_Object tail, frame;
10456 int new_count = 0;
10457
10458 FOR_EACH_FRAME (tail, frame)
10459 {
10460 int this_is_visible = 0;
10461
10462 if (XFRAME (frame)->visible)
10463 this_is_visible = 1;
10464 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10465 if (XFRAME (frame)->visible)
10466 this_is_visible = 1;
10467
10468 if (this_is_visible)
10469 new_count++;
10470 }
10471
10472 if (new_count != number_of_visible_frames)
10473 windows_or_buffers_changed++;
10474 }
10475
10476 /* Change frame size now if a change is pending. */
10477 do_pending_window_change (1);
10478
10479 /* If we just did a pending size change, or have additional
10480 visible frames, redisplay again. */
10481 if (windows_or_buffers_changed && !pause)
10482 goto retry;
10483
10484 end_of_redisplay:
10485 unbind_to (count, Qnil);
10486 RESUME_POLLING;
10487 }
10488
10489
10490 /* Redisplay, but leave alone any recent echo area message unless
10491 another message has been requested in its place.
10492
10493 This is useful in situations where you need to redisplay but no
10494 user action has occurred, making it inappropriate for the message
10495 area to be cleared. See tracking_off and
10496 wait_reading_process_output for examples of these situations.
10497
10498 FROM_WHERE is an integer saying from where this function was
10499 called. This is useful for debugging. */
10500
10501 void
10502 redisplay_preserve_echo_area (from_where)
10503 int from_where;
10504 {
10505 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10506
10507 if (!NILP (echo_area_buffer[1]))
10508 {
10509 /* We have a previously displayed message, but no current
10510 message. Redisplay the previous message. */
10511 display_last_displayed_message_p = 1;
10512 redisplay_internal (1);
10513 display_last_displayed_message_p = 0;
10514 }
10515 else
10516 redisplay_internal (1);
10517 }
10518
10519
10520 /* Function registered with record_unwind_protect in
10521 redisplay_internal. Reset redisplaying_p to the value it had
10522 before redisplay_internal was called, and clear
10523 prevent_freeing_realized_faces_p. It also selects the previously
10524 selected frame. */
10525
10526 static Lisp_Object
10527 unwind_redisplay (val)
10528 Lisp_Object val;
10529 {
10530 Lisp_Object old_redisplaying_p, old_frame;
10531
10532 old_redisplaying_p = XCAR (val);
10533 redisplaying_p = XFASTINT (old_redisplaying_p);
10534 old_frame = XCDR (val);
10535 if (! EQ (old_frame, selected_frame))
10536 select_frame_for_redisplay (old_frame);
10537 return Qnil;
10538 }
10539
10540
10541 /* Mark the display of window W as accurate or inaccurate. If
10542 ACCURATE_P is non-zero mark display of W as accurate. If
10543 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10544 redisplay_internal is called. */
10545
10546 static void
10547 mark_window_display_accurate_1 (w, accurate_p)
10548 struct window *w;
10549 int accurate_p;
10550 {
10551 if (BUFFERP (w->buffer))
10552 {
10553 struct buffer *b = XBUFFER (w->buffer);
10554
10555 w->last_modified
10556 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10557 w->last_overlay_modified
10558 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10559 w->last_had_star
10560 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10561
10562 if (accurate_p)
10563 {
10564 b->clip_changed = 0;
10565 b->prevent_redisplay_optimizations_p = 0;
10566
10567 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10568 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10569 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10570 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10571
10572 w->current_matrix->buffer = b;
10573 w->current_matrix->begv = BUF_BEGV (b);
10574 w->current_matrix->zv = BUF_ZV (b);
10575
10576 w->last_cursor = w->cursor;
10577 w->last_cursor_off_p = w->cursor_off_p;
10578
10579 if (w == XWINDOW (selected_window))
10580 w->last_point = make_number (BUF_PT (b));
10581 else
10582 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10583 }
10584 }
10585
10586 if (accurate_p)
10587 {
10588 w->window_end_valid = w->buffer;
10589 #if 0 /* This is incorrect with variable-height lines. */
10590 xassert (XINT (w->window_end_vpos)
10591 < (WINDOW_TOTAL_LINES (w)
10592 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10593 #endif
10594 w->update_mode_line = Qnil;
10595 }
10596 }
10597
10598
10599 /* Mark the display of windows in the window tree rooted at WINDOW as
10600 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10601 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10602 be redisplayed the next time redisplay_internal is called. */
10603
10604 void
10605 mark_window_display_accurate (window, accurate_p)
10606 Lisp_Object window;
10607 int accurate_p;
10608 {
10609 struct window *w;
10610
10611 for (; !NILP (window); window = w->next)
10612 {
10613 w = XWINDOW (window);
10614 mark_window_display_accurate_1 (w, accurate_p);
10615
10616 if (!NILP (w->vchild))
10617 mark_window_display_accurate (w->vchild, accurate_p);
10618 if (!NILP (w->hchild))
10619 mark_window_display_accurate (w->hchild, accurate_p);
10620 }
10621
10622 if (accurate_p)
10623 {
10624 update_overlay_arrows (1);
10625 }
10626 else
10627 {
10628 /* Force a thorough redisplay the next time by setting
10629 last_arrow_position and last_arrow_string to t, which is
10630 unequal to any useful value of Voverlay_arrow_... */
10631 update_overlay_arrows (-1);
10632 }
10633 }
10634
10635
10636 /* Return value in display table DP (Lisp_Char_Table *) for character
10637 C. Since a display table doesn't have any parent, we don't have to
10638 follow parent. Do not call this function directly but use the
10639 macro DISP_CHAR_VECTOR. */
10640
10641 Lisp_Object
10642 disp_char_vector (dp, c)
10643 struct Lisp_Char_Table *dp;
10644 int c;
10645 {
10646 Lisp_Object val;
10647
10648 if (ASCII_CHAR_P (c))
10649 {
10650 val = dp->ascii;
10651 if (SUB_CHAR_TABLE_P (val))
10652 val = XSUB_CHAR_TABLE (val)->contents[c];
10653 }
10654 else
10655 {
10656 Lisp_Object table;
10657
10658 XSETCHAR_TABLE (table, dp);
10659 val = char_table_ref (table, c);
10660 }
10661 if (NILP (val))
10662 val = dp->defalt;
10663 return val;
10664 }
10665
10666
10667 \f
10668 /***********************************************************************
10669 Window Redisplay
10670 ***********************************************************************/
10671
10672 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10673
10674 static void
10675 redisplay_windows (window)
10676 Lisp_Object window;
10677 {
10678 while (!NILP (window))
10679 {
10680 struct window *w = XWINDOW (window);
10681
10682 if (!NILP (w->hchild))
10683 redisplay_windows (w->hchild);
10684 else if (!NILP (w->vchild))
10685 redisplay_windows (w->vchild);
10686 else
10687 {
10688 displayed_buffer = XBUFFER (w->buffer);
10689 /* Use list_of_error, not Qerror, so that
10690 we catch only errors and don't run the debugger. */
10691 internal_condition_case_1 (redisplay_window_0, window,
10692 list_of_error,
10693 redisplay_window_error);
10694 }
10695
10696 window = w->next;
10697 }
10698 }
10699
10700 static Lisp_Object
10701 redisplay_window_error ()
10702 {
10703 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10704 return Qnil;
10705 }
10706
10707 static Lisp_Object
10708 redisplay_window_0 (window)
10709 Lisp_Object window;
10710 {
10711 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10712 redisplay_window (window, 0);
10713 return Qnil;
10714 }
10715
10716 static Lisp_Object
10717 redisplay_window_1 (window)
10718 Lisp_Object window;
10719 {
10720 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10721 redisplay_window (window, 1);
10722 return Qnil;
10723 }
10724 \f
10725
10726 /* Increment GLYPH until it reaches END or CONDITION fails while
10727 adding (GLYPH)->pixel_width to X. */
10728
10729 #define SKIP_GLYPHS(glyph, end, x, condition) \
10730 do \
10731 { \
10732 (x) += (glyph)->pixel_width; \
10733 ++(glyph); \
10734 } \
10735 while ((glyph) < (end) && (condition))
10736
10737
10738 /* Set cursor position of W. PT is assumed to be displayed in ROW.
10739 DELTA is the number of bytes by which positions recorded in ROW
10740 differ from current buffer positions. */
10741
10742 void
10743 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10744 struct window *w;
10745 struct glyph_row *row;
10746 struct glyph_matrix *matrix;
10747 int delta, delta_bytes, dy, dvpos;
10748 {
10749 struct glyph *glyph = row->glyphs[TEXT_AREA];
10750 struct glyph *end = glyph + row->used[TEXT_AREA];
10751 struct glyph *cursor = NULL;
10752 /* The first glyph that starts a sequence of glyphs from string. */
10753 struct glyph *string_start;
10754 /* The X coordinate of string_start. */
10755 int string_start_x;
10756 /* The last known character position. */
10757 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10758 /* The last known character position before string_start. */
10759 int string_before_pos;
10760 int x = row->x;
10761 int cursor_x = x;
10762 int cursor_from_overlay_pos = 0;
10763 int pt_old = PT - delta;
10764
10765 /* Skip over glyphs not having an object at the start of the row.
10766 These are special glyphs like truncation marks on terminal
10767 frames. */
10768 if (row->displays_text_p)
10769 while (glyph < end
10770 && INTEGERP (glyph->object)
10771 && glyph->charpos < 0)
10772 {
10773 x += glyph->pixel_width;
10774 ++glyph;
10775 }
10776
10777 string_start = NULL;
10778 while (glyph < end
10779 && !INTEGERP (glyph->object)
10780 && (!BUFFERP (glyph->object)
10781 || (last_pos = glyph->charpos) < pt_old))
10782 {
10783 if (! STRINGP (glyph->object))
10784 {
10785 string_start = NULL;
10786 x += glyph->pixel_width;
10787 ++glyph;
10788 if (cursor_from_overlay_pos
10789 && last_pos > cursor_from_overlay_pos)
10790 {
10791 cursor_from_overlay_pos = 0;
10792 cursor = 0;
10793 }
10794 }
10795 else
10796 {
10797 string_before_pos = last_pos;
10798 string_start = glyph;
10799 string_start_x = x;
10800 /* Skip all glyphs from string. */
10801 do
10802 {
10803 int pos;
10804 if ((cursor == NULL || glyph > cursor)
10805 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
10806 Qcursor, (glyph)->object))
10807 && (pos = string_buffer_position (w, glyph->object,
10808 string_before_pos),
10809 (pos == 0 /* From overlay */
10810 || pos == pt_old)))
10811 {
10812 /* Estimate overlay buffer position from the buffer
10813 positions of the glyphs before and after the overlay.
10814 Add 1 to last_pos so that if point corresponds to the
10815 glyph right after the overlay, we still use a 'cursor'
10816 property found in that overlay. */
10817 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
10818 cursor = glyph;
10819 cursor_x = x;
10820 }
10821 x += glyph->pixel_width;
10822 ++glyph;
10823 }
10824 while (glyph < end && STRINGP (glyph->object));
10825 }
10826 }
10827
10828 if (cursor != NULL)
10829 {
10830 glyph = cursor;
10831 x = cursor_x;
10832 }
10833 else if (string_start
10834 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
10835 {
10836 /* We may have skipped over point because the previous glyphs
10837 are from string. As there's no easy way to know the
10838 character position of the current glyph, find the correct
10839 glyph on point by scanning from string_start again. */
10840 Lisp_Object limit;
10841 Lisp_Object string;
10842 int pos;
10843
10844 limit = make_number (pt_old + 1);
10845 end = glyph;
10846 glyph = string_start;
10847 x = string_start_x;
10848 string = glyph->object;
10849 pos = string_buffer_position (w, string, string_before_pos);
10850 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
10851 because we always put cursor after overlay strings. */
10852 while (pos == 0 && glyph < end)
10853 {
10854 string = glyph->object;
10855 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10856 if (glyph < end)
10857 pos = string_buffer_position (w, glyph->object, string_before_pos);
10858 }
10859
10860 while (glyph < end)
10861 {
10862 pos = XINT (Fnext_single_char_property_change
10863 (make_number (pos), Qdisplay, Qnil, limit));
10864 if (pos > pt_old)
10865 break;
10866 /* Skip glyphs from the same string. */
10867 string = glyph->object;
10868 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10869 /* Skip glyphs from an overlay. */
10870 while (glyph < end
10871 && ! string_buffer_position (w, glyph->object, pos))
10872 {
10873 string = glyph->object;
10874 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10875 }
10876 }
10877 }
10878
10879 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
10880 w->cursor.x = x;
10881 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
10882 w->cursor.y = row->y + dy;
10883
10884 if (w == XWINDOW (selected_window))
10885 {
10886 if (!row->continued_p
10887 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
10888 && row->x == 0)
10889 {
10890 this_line_buffer = XBUFFER (w->buffer);
10891
10892 CHARPOS (this_line_start_pos)
10893 = MATRIX_ROW_START_CHARPOS (row) + delta;
10894 BYTEPOS (this_line_start_pos)
10895 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
10896
10897 CHARPOS (this_line_end_pos)
10898 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
10899 BYTEPOS (this_line_end_pos)
10900 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
10901
10902 this_line_y = w->cursor.y;
10903 this_line_pixel_height = row->height;
10904 this_line_vpos = w->cursor.vpos;
10905 this_line_start_x = row->x;
10906 }
10907 else
10908 CHARPOS (this_line_start_pos) = 0;
10909 }
10910 }
10911
10912
10913 /* Run window scroll functions, if any, for WINDOW with new window
10914 start STARTP. Sets the window start of WINDOW to that position.
10915
10916 We assume that the window's buffer is really current. */
10917
10918 static INLINE struct text_pos
10919 run_window_scroll_functions (window, startp)
10920 Lisp_Object window;
10921 struct text_pos startp;
10922 {
10923 struct window *w = XWINDOW (window);
10924 SET_MARKER_FROM_TEXT_POS (w->start, startp);
10925
10926 if (current_buffer != XBUFFER (w->buffer))
10927 abort ();
10928
10929 if (!NILP (Vwindow_scroll_functions))
10930 {
10931 run_hook_with_args_2 (Qwindow_scroll_functions, window,
10932 make_number (CHARPOS (startp)));
10933 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10934 /* In case the hook functions switch buffers. */
10935 if (current_buffer != XBUFFER (w->buffer))
10936 set_buffer_internal_1 (XBUFFER (w->buffer));
10937 }
10938
10939 return startp;
10940 }
10941
10942
10943 /* Make sure the line containing the cursor is fully visible.
10944 A value of 1 means there is nothing to be done.
10945 (Either the line is fully visible, or it cannot be made so,
10946 or we cannot tell.)
10947
10948 If FORCE_P is non-zero, return 0 even if partial visible cursor row
10949 is higher than window.
10950
10951 A value of 0 means the caller should do scrolling
10952 as if point had gone off the screen. */
10953
10954 static int
10955 make_cursor_line_fully_visible (w, force_p)
10956 struct window *w;
10957 int force_p;
10958 {
10959 struct glyph_matrix *matrix;
10960 struct glyph_row *row;
10961 int window_height;
10962
10963 /* It's not always possible to find the cursor, e.g, when a window
10964 is full of overlay strings. Don't do anything in that case. */
10965 if (w->cursor.vpos < 0)
10966 return 1;
10967
10968 matrix = w->desired_matrix;
10969 row = MATRIX_ROW (matrix, w->cursor.vpos);
10970
10971 /* If the cursor row is not partially visible, there's nothing to do. */
10972 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
10973 return 1;
10974
10975 /* If the row the cursor is in is taller than the window's height,
10976 it's not clear what to do, so do nothing. */
10977 window_height = window_box_height (w);
10978 if (row->height >= window_height)
10979 {
10980 if (!force_p || w->vscroll)
10981 return 1;
10982 }
10983 return 0;
10984
10985 #if 0
10986 /* This code used to try to scroll the window just enough to make
10987 the line visible. It returned 0 to say that the caller should
10988 allocate larger glyph matrices. */
10989
10990 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
10991 {
10992 int dy = row->height - row->visible_height;
10993 w->vscroll = 0;
10994 w->cursor.y += dy;
10995 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10996 }
10997 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
10998 {
10999 int dy = - (row->height - row->visible_height);
11000 w->vscroll = dy;
11001 w->cursor.y += dy;
11002 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11003 }
11004
11005 /* When we change the cursor y-position of the selected window,
11006 change this_line_y as well so that the display optimization for
11007 the cursor line of the selected window in redisplay_internal uses
11008 the correct y-position. */
11009 if (w == XWINDOW (selected_window))
11010 this_line_y = w->cursor.y;
11011
11012 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11013 redisplay with larger matrices. */
11014 if (matrix->nrows < required_matrix_height (w))
11015 {
11016 fonts_changed_p = 1;
11017 return 0;
11018 }
11019
11020 return 1;
11021 #endif /* 0 */
11022 }
11023
11024
11025 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11026 non-zero means only WINDOW is redisplayed in redisplay_internal.
11027 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11028 in redisplay_window to bring a partially visible line into view in
11029 the case that only the cursor has moved.
11030
11031 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11032 last screen line's vertical height extends past the end of the screen.
11033
11034 Value is
11035
11036 1 if scrolling succeeded
11037
11038 0 if scrolling didn't find point.
11039
11040 -1 if new fonts have been loaded so that we must interrupt
11041 redisplay, adjust glyph matrices, and try again. */
11042
11043 enum
11044 {
11045 SCROLLING_SUCCESS,
11046 SCROLLING_FAILED,
11047 SCROLLING_NEED_LARGER_MATRICES
11048 };
11049
11050 static int
11051 try_scrolling (window, just_this_one_p, scroll_conservatively,
11052 scroll_step, temp_scroll_step, last_line_misfit)
11053 Lisp_Object window;
11054 int just_this_one_p;
11055 EMACS_INT scroll_conservatively, scroll_step;
11056 int temp_scroll_step;
11057 int last_line_misfit;
11058 {
11059 struct window *w = XWINDOW (window);
11060 struct frame *f = XFRAME (w->frame);
11061 struct text_pos scroll_margin_pos;
11062 struct text_pos pos;
11063 struct text_pos startp;
11064 struct it it;
11065 Lisp_Object window_end;
11066 int this_scroll_margin;
11067 int dy = 0;
11068 int scroll_max;
11069 int rc;
11070 int amount_to_scroll = 0;
11071 Lisp_Object aggressive;
11072 int height;
11073 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11074
11075 #if GLYPH_DEBUG
11076 debug_method_add (w, "try_scrolling");
11077 #endif
11078
11079 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11080
11081 /* Compute scroll margin height in pixels. We scroll when point is
11082 within this distance from the top or bottom of the window. */
11083 if (scroll_margin > 0)
11084 {
11085 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11086 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11087 }
11088 else
11089 this_scroll_margin = 0;
11090
11091 /* Force scroll_conservatively to have a reasonable value so it doesn't
11092 cause an overflow while computing how much to scroll. */
11093 if (scroll_conservatively)
11094 scroll_conservatively = min (scroll_conservatively,
11095 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11096
11097 /* Compute how much we should try to scroll maximally to bring point
11098 into view. */
11099 if (scroll_step || scroll_conservatively || temp_scroll_step)
11100 scroll_max = max (scroll_step,
11101 max (scroll_conservatively, temp_scroll_step));
11102 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11103 || NUMBERP (current_buffer->scroll_up_aggressively))
11104 /* We're trying to scroll because of aggressive scrolling
11105 but no scroll_step is set. Choose an arbitrary one. Maybe
11106 there should be a variable for this. */
11107 scroll_max = 10;
11108 else
11109 scroll_max = 0;
11110 scroll_max *= FRAME_LINE_HEIGHT (f);
11111
11112 /* Decide whether we have to scroll down. Start at the window end
11113 and move this_scroll_margin up to find the position of the scroll
11114 margin. */
11115 window_end = Fwindow_end (window, Qt);
11116
11117 too_near_end:
11118
11119 CHARPOS (scroll_margin_pos) = XINT (window_end);
11120 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11121
11122 if (this_scroll_margin || extra_scroll_margin_lines)
11123 {
11124 start_display (&it, w, scroll_margin_pos);
11125 if (this_scroll_margin)
11126 move_it_vertically (&it, - this_scroll_margin);
11127 if (extra_scroll_margin_lines)
11128 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11129 scroll_margin_pos = it.current.pos;
11130 }
11131
11132 if (PT >= CHARPOS (scroll_margin_pos))
11133 {
11134 int y0;
11135
11136 /* Point is in the scroll margin at the bottom of the window, or
11137 below. Compute a new window start that makes point visible. */
11138
11139 /* Compute the distance from the scroll margin to PT.
11140 Give up if the distance is greater than scroll_max. */
11141 start_display (&it, w, scroll_margin_pos);
11142 y0 = it.current_y;
11143 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11144 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11145
11146 /* To make point visible, we have to move the window start
11147 down so that the line the cursor is in is visible, which
11148 means we have to add in the height of the cursor line. */
11149 dy = line_bottom_y (&it) - y0;
11150
11151 if (dy > scroll_max)
11152 return SCROLLING_FAILED;
11153
11154 /* Move the window start down. If scrolling conservatively,
11155 move it just enough down to make point visible. If
11156 scroll_step is set, move it down by scroll_step. */
11157 start_display (&it, w, startp);
11158
11159 if (scroll_conservatively)
11160 /* Set AMOUNT_TO_SCROLL to at least one line,
11161 and at most scroll_conservatively lines. */
11162 amount_to_scroll
11163 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11164 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11165 else if (scroll_step || temp_scroll_step)
11166 amount_to_scroll = scroll_max;
11167 else
11168 {
11169 aggressive = current_buffer->scroll_up_aggressively;
11170 height = WINDOW_BOX_TEXT_HEIGHT (w);
11171 if (NUMBERP (aggressive))
11172 {
11173 double float_amount = XFLOATINT (aggressive) * height;
11174 amount_to_scroll = float_amount;
11175 if (amount_to_scroll == 0 && float_amount > 0)
11176 amount_to_scroll = 1;
11177 }
11178 }
11179
11180 if (amount_to_scroll <= 0)
11181 return SCROLLING_FAILED;
11182
11183 /* If moving by amount_to_scroll leaves STARTP unchanged,
11184 move it down one screen line. */
11185
11186 move_it_vertically (&it, amount_to_scroll);
11187 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11188 move_it_by_lines (&it, 1, 1);
11189 startp = it.current.pos;
11190 }
11191 else
11192 {
11193 /* See if point is inside the scroll margin at the top of the
11194 window. */
11195 scroll_margin_pos = startp;
11196 if (this_scroll_margin)
11197 {
11198 start_display (&it, w, startp);
11199 move_it_vertically (&it, this_scroll_margin);
11200 scroll_margin_pos = it.current.pos;
11201 }
11202
11203 if (PT < CHARPOS (scroll_margin_pos))
11204 {
11205 /* Point is in the scroll margin at the top of the window or
11206 above what is displayed in the window. */
11207 int y0;
11208
11209 /* Compute the vertical distance from PT to the scroll
11210 margin position. Give up if distance is greater than
11211 scroll_max. */
11212 SET_TEXT_POS (pos, PT, PT_BYTE);
11213 start_display (&it, w, pos);
11214 y0 = it.current_y;
11215 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11216 it.last_visible_y, -1,
11217 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11218 dy = it.current_y - y0;
11219 if (dy > scroll_max)
11220 return SCROLLING_FAILED;
11221
11222 /* Compute new window start. */
11223 start_display (&it, w, startp);
11224
11225 if (scroll_conservatively)
11226 amount_to_scroll
11227 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11228 else if (scroll_step || temp_scroll_step)
11229 amount_to_scroll = scroll_max;
11230 else
11231 {
11232 aggressive = current_buffer->scroll_down_aggressively;
11233 height = WINDOW_BOX_TEXT_HEIGHT (w);
11234 if (NUMBERP (aggressive))
11235 {
11236 double float_amount = XFLOATINT (aggressive) * height;
11237 amount_to_scroll = float_amount;
11238 if (amount_to_scroll == 0 && float_amount > 0)
11239 amount_to_scroll = 1;
11240 }
11241 }
11242
11243 if (amount_to_scroll <= 0)
11244 return SCROLLING_FAILED;
11245
11246 move_it_vertically (&it, - amount_to_scroll);
11247 startp = it.current.pos;
11248 }
11249 }
11250
11251 /* Run window scroll functions. */
11252 startp = run_window_scroll_functions (window, startp);
11253
11254 /* Display the window. Give up if new fonts are loaded, or if point
11255 doesn't appear. */
11256 if (!try_window (window, startp))
11257 rc = SCROLLING_NEED_LARGER_MATRICES;
11258 else if (w->cursor.vpos < 0)
11259 {
11260 clear_glyph_matrix (w->desired_matrix);
11261 rc = SCROLLING_FAILED;
11262 }
11263 else
11264 {
11265 /* Maybe forget recorded base line for line number display. */
11266 if (!just_this_one_p
11267 || current_buffer->clip_changed
11268 || BEG_UNCHANGED < CHARPOS (startp))
11269 w->base_line_number = Qnil;
11270
11271 /* If cursor ends up on a partially visible line,
11272 treat that as being off the bottom of the screen. */
11273 if (! make_cursor_line_fully_visible (w, extra_scroll_margin_lines <= 1))
11274 {
11275 clear_glyph_matrix (w->desired_matrix);
11276 ++extra_scroll_margin_lines;
11277 goto too_near_end;
11278 }
11279 rc = SCROLLING_SUCCESS;
11280 }
11281
11282 return rc;
11283 }
11284
11285
11286 /* Compute a suitable window start for window W if display of W starts
11287 on a continuation line. Value is non-zero if a new window start
11288 was computed.
11289
11290 The new window start will be computed, based on W's width, starting
11291 from the start of the continued line. It is the start of the
11292 screen line with the minimum distance from the old start W->start. */
11293
11294 static int
11295 compute_window_start_on_continuation_line (w)
11296 struct window *w;
11297 {
11298 struct text_pos pos, start_pos;
11299 int window_start_changed_p = 0;
11300
11301 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11302
11303 /* If window start is on a continuation line... Window start may be
11304 < BEGV in case there's invisible text at the start of the
11305 buffer (M-x rmail, for example). */
11306 if (CHARPOS (start_pos) > BEGV
11307 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11308 {
11309 struct it it;
11310 struct glyph_row *row;
11311
11312 /* Handle the case that the window start is out of range. */
11313 if (CHARPOS (start_pos) < BEGV)
11314 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11315 else if (CHARPOS (start_pos) > ZV)
11316 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11317
11318 /* Find the start of the continued line. This should be fast
11319 because scan_buffer is fast (newline cache). */
11320 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11321 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11322 row, DEFAULT_FACE_ID);
11323 reseat_at_previous_visible_line_start (&it);
11324
11325 /* If the line start is "too far" away from the window start,
11326 say it takes too much time to compute a new window start. */
11327 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11328 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11329 {
11330 int min_distance, distance;
11331
11332 /* Move forward by display lines to find the new window
11333 start. If window width was enlarged, the new start can
11334 be expected to be > the old start. If window width was
11335 decreased, the new window start will be < the old start.
11336 So, we're looking for the display line start with the
11337 minimum distance from the old window start. */
11338 pos = it.current.pos;
11339 min_distance = INFINITY;
11340 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11341 distance < min_distance)
11342 {
11343 min_distance = distance;
11344 pos = it.current.pos;
11345 move_it_by_lines (&it, 1, 0);
11346 }
11347
11348 /* Set the window start there. */
11349 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11350 window_start_changed_p = 1;
11351 }
11352 }
11353
11354 return window_start_changed_p;
11355 }
11356
11357
11358 /* Try cursor movement in case text has not changed in window WINDOW,
11359 with window start STARTP. Value is
11360
11361 CURSOR_MOVEMENT_SUCCESS if successful
11362
11363 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11364
11365 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11366 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11367 we want to scroll as if scroll-step were set to 1. See the code.
11368
11369 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11370 which case we have to abort this redisplay, and adjust matrices
11371 first. */
11372
11373 enum
11374 {
11375 CURSOR_MOVEMENT_SUCCESS,
11376 CURSOR_MOVEMENT_CANNOT_BE_USED,
11377 CURSOR_MOVEMENT_MUST_SCROLL,
11378 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11379 };
11380
11381 static int
11382 try_cursor_movement (window, startp, scroll_step)
11383 Lisp_Object window;
11384 struct text_pos startp;
11385 int *scroll_step;
11386 {
11387 struct window *w = XWINDOW (window);
11388 struct frame *f = XFRAME (w->frame);
11389 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11390
11391 #if GLYPH_DEBUG
11392 if (inhibit_try_cursor_movement)
11393 return rc;
11394 #endif
11395
11396 /* Handle case where text has not changed, only point, and it has
11397 not moved off the frame. */
11398 if (/* Point may be in this window. */
11399 PT >= CHARPOS (startp)
11400 /* Selective display hasn't changed. */
11401 && !current_buffer->clip_changed
11402 /* Function force-mode-line-update is used to force a thorough
11403 redisplay. It sets either windows_or_buffers_changed or
11404 update_mode_lines. So don't take a shortcut here for these
11405 cases. */
11406 && !update_mode_lines
11407 && !windows_or_buffers_changed
11408 && !cursor_type_changed
11409 /* Can't use this case if highlighting a region. When a
11410 region exists, cursor movement has to do more than just
11411 set the cursor. */
11412 && !(!NILP (Vtransient_mark_mode)
11413 && !NILP (current_buffer->mark_active))
11414 && NILP (w->region_showing)
11415 && NILP (Vshow_trailing_whitespace)
11416 /* Right after splitting windows, last_point may be nil. */
11417 && INTEGERP (w->last_point)
11418 /* This code is not used for mini-buffer for the sake of the case
11419 of redisplaying to replace an echo area message; since in
11420 that case the mini-buffer contents per se are usually
11421 unchanged. This code is of no real use in the mini-buffer
11422 since the handling of this_line_start_pos, etc., in redisplay
11423 handles the same cases. */
11424 && !EQ (window, minibuf_window)
11425 /* When splitting windows or for new windows, it happens that
11426 redisplay is called with a nil window_end_vpos or one being
11427 larger than the window. This should really be fixed in
11428 window.c. I don't have this on my list, now, so we do
11429 approximately the same as the old redisplay code. --gerd. */
11430 && INTEGERP (w->window_end_vpos)
11431 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11432 && (FRAME_WINDOW_P (f)
11433 || !overlay_arrow_in_current_buffer_p ()))
11434 {
11435 int this_scroll_margin, top_scroll_margin;
11436 struct glyph_row *row = NULL;
11437
11438 #if GLYPH_DEBUG
11439 debug_method_add (w, "cursor movement");
11440 #endif
11441
11442 /* Scroll if point within this distance from the top or bottom
11443 of the window. This is a pixel value. */
11444 this_scroll_margin = max (0, scroll_margin);
11445 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11446 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11447
11448 top_scroll_margin = this_scroll_margin;
11449 if (WINDOW_WANTS_HEADER_LINE_P (w))
11450 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
11451
11452 /* Start with the row the cursor was displayed during the last
11453 not paused redisplay. Give up if that row is not valid. */
11454 if (w->last_cursor.vpos < 0
11455 || w->last_cursor.vpos >= w->current_matrix->nrows)
11456 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11457 else
11458 {
11459 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11460 if (row->mode_line_p)
11461 ++row;
11462 if (!row->enabled_p)
11463 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11464 }
11465
11466 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11467 {
11468 int scroll_p = 0;
11469 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11470
11471 if (PT > XFASTINT (w->last_point))
11472 {
11473 /* Point has moved forward. */
11474 while (MATRIX_ROW_END_CHARPOS (row) < PT
11475 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11476 {
11477 xassert (row->enabled_p);
11478 ++row;
11479 }
11480
11481 /* The end position of a row equals the start position
11482 of the next row. If PT is there, we would rather
11483 display it in the next line. */
11484 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11485 && MATRIX_ROW_END_CHARPOS (row) == PT
11486 && !cursor_row_p (w, row))
11487 ++row;
11488
11489 /* If within the scroll margin, scroll. Note that
11490 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11491 the next line would be drawn, and that
11492 this_scroll_margin can be zero. */
11493 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11494 || PT > MATRIX_ROW_END_CHARPOS (row)
11495 /* Line is completely visible last line in window
11496 and PT is to be set in the next line. */
11497 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11498 && PT == MATRIX_ROW_END_CHARPOS (row)
11499 && !row->ends_at_zv_p
11500 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11501 scroll_p = 1;
11502 }
11503 else if (PT < XFASTINT (w->last_point))
11504 {
11505 /* Cursor has to be moved backward. Note that PT >=
11506 CHARPOS (startp) because of the outer if-statement. */
11507 while (!row->mode_line_p
11508 && (MATRIX_ROW_START_CHARPOS (row) > PT
11509 || (MATRIX_ROW_START_CHARPOS (row) == PT
11510 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11511 && (row->y > top_scroll_margin
11512 || CHARPOS (startp) == BEGV))
11513 {
11514 xassert (row->enabled_p);
11515 --row;
11516 }
11517
11518 /* Consider the following case: Window starts at BEGV,
11519 there is invisible, intangible text at BEGV, so that
11520 display starts at some point START > BEGV. It can
11521 happen that we are called with PT somewhere between
11522 BEGV and START. Try to handle that case. */
11523 if (row < w->current_matrix->rows
11524 || row->mode_line_p)
11525 {
11526 row = w->current_matrix->rows;
11527 if (row->mode_line_p)
11528 ++row;
11529 }
11530
11531 /* Due to newlines in overlay strings, we may have to
11532 skip forward over overlay strings. */
11533 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11534 && MATRIX_ROW_END_CHARPOS (row) == PT
11535 && !cursor_row_p (w, row))
11536 ++row;
11537
11538 /* If within the scroll margin, scroll. */
11539 if (row->y < top_scroll_margin
11540 && CHARPOS (startp) != BEGV)
11541 scroll_p = 1;
11542 }
11543
11544 if (PT < MATRIX_ROW_START_CHARPOS (row)
11545 || PT > MATRIX_ROW_END_CHARPOS (row))
11546 {
11547 /* if PT is not in the glyph row, give up. */
11548 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11549 }
11550 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
11551 {
11552 if (PT == MATRIX_ROW_END_CHARPOS (row)
11553 && !row->ends_at_zv_p
11554 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11555 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11556 else if (row->height > window_box_height (w))
11557 {
11558 /* If we end up in a partially visible line, let's
11559 make it fully visible, except when it's taller
11560 than the window, in which case we can't do much
11561 about it. */
11562 *scroll_step = 1;
11563 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11564 }
11565 else
11566 {
11567 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11568 if (!make_cursor_line_fully_visible (w, 0))
11569 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11570 else
11571 rc = CURSOR_MOVEMENT_SUCCESS;
11572 }
11573 }
11574 else if (scroll_p)
11575 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11576 else
11577 {
11578 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11579 rc = CURSOR_MOVEMENT_SUCCESS;
11580 }
11581 }
11582 }
11583
11584 return rc;
11585 }
11586
11587 void
11588 set_vertical_scroll_bar (w)
11589 struct window *w;
11590 {
11591 int start, end, whole;
11592
11593 /* Calculate the start and end positions for the current window.
11594 At some point, it would be nice to choose between scrollbars
11595 which reflect the whole buffer size, with special markers
11596 indicating narrowing, and scrollbars which reflect only the
11597 visible region.
11598
11599 Note that mini-buffers sometimes aren't displaying any text. */
11600 if (!MINI_WINDOW_P (w)
11601 || (w == XWINDOW (minibuf_window)
11602 && NILP (echo_area_buffer[0])))
11603 {
11604 struct buffer *buf = XBUFFER (w->buffer);
11605 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11606 start = marker_position (w->start) - BUF_BEGV (buf);
11607 /* I don't think this is guaranteed to be right. For the
11608 moment, we'll pretend it is. */
11609 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11610
11611 if (end < start)
11612 end = start;
11613 if (whole < (end - start))
11614 whole = end - start;
11615 }
11616 else
11617 start = end = whole = 0;
11618
11619 /* Indicate what this scroll bar ought to be displaying now. */
11620 set_vertical_scroll_bar_hook (w, end - start, whole, start);
11621 }
11622
11623
11624 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11625 selected_window is redisplayed.
11626
11627 We can return without actually redisplaying the window if
11628 fonts_changed_p is nonzero. In that case, redisplay_internal will
11629 retry. */
11630
11631 static void
11632 redisplay_window (window, just_this_one_p)
11633 Lisp_Object window;
11634 int just_this_one_p;
11635 {
11636 struct window *w = XWINDOW (window);
11637 struct frame *f = XFRAME (w->frame);
11638 struct buffer *buffer = XBUFFER (w->buffer);
11639 struct buffer *old = current_buffer;
11640 struct text_pos lpoint, opoint, startp;
11641 int update_mode_line;
11642 int tem;
11643 struct it it;
11644 /* Record it now because it's overwritten. */
11645 int current_matrix_up_to_date_p = 0;
11646 int used_current_matrix_p = 0;
11647 /* This is less strict than current_matrix_up_to_date_p.
11648 It indictes that the buffer contents and narrowing are unchanged. */
11649 int buffer_unchanged_p = 0;
11650 int temp_scroll_step = 0;
11651 int count = SPECPDL_INDEX ();
11652 int rc;
11653 int centering_position;
11654 int last_line_misfit = 0;
11655
11656 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11657 opoint = lpoint;
11658
11659 /* W must be a leaf window here. */
11660 xassert (!NILP (w->buffer));
11661 #if GLYPH_DEBUG
11662 *w->desired_matrix->method = 0;
11663 #endif
11664
11665 specbind (Qinhibit_point_motion_hooks, Qt);
11666
11667 reconsider_clip_changes (w, buffer);
11668
11669 /* Has the mode line to be updated? */
11670 update_mode_line = (!NILP (w->update_mode_line)
11671 || update_mode_lines
11672 || buffer->clip_changed
11673 || buffer->prevent_redisplay_optimizations_p);
11674
11675 if (MINI_WINDOW_P (w))
11676 {
11677 if (w == XWINDOW (echo_area_window)
11678 && !NILP (echo_area_buffer[0]))
11679 {
11680 if (update_mode_line)
11681 /* We may have to update a tty frame's menu bar or a
11682 tool-bar. Example `M-x C-h C-h C-g'. */
11683 goto finish_menu_bars;
11684 else
11685 /* We've already displayed the echo area glyphs in this window. */
11686 goto finish_scroll_bars;
11687 }
11688 else if ((w != XWINDOW (minibuf_window)
11689 || minibuf_level == 0)
11690 /* When buffer is nonempty, redisplay window normally. */
11691 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
11692 /* Quail displays non-mini buffers in minibuffer window.
11693 In that case, redisplay the window normally. */
11694 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
11695 {
11696 /* W is a mini-buffer window, but it's not active, so clear
11697 it. */
11698 int yb = window_text_bottom_y (w);
11699 struct glyph_row *row;
11700 int y;
11701
11702 for (y = 0, row = w->desired_matrix->rows;
11703 y < yb;
11704 y += row->height, ++row)
11705 blank_row (w, row, y);
11706 goto finish_scroll_bars;
11707 }
11708
11709 clear_glyph_matrix (w->desired_matrix);
11710 }
11711
11712 /* Otherwise set up data on this window; select its buffer and point
11713 value. */
11714 /* Really select the buffer, for the sake of buffer-local
11715 variables. */
11716 set_buffer_internal_1 (XBUFFER (w->buffer));
11717 SET_TEXT_POS (opoint, PT, PT_BYTE);
11718
11719 current_matrix_up_to_date_p
11720 = (!NILP (w->window_end_valid)
11721 && !current_buffer->clip_changed
11722 && !current_buffer->prevent_redisplay_optimizations_p
11723 && XFASTINT (w->last_modified) >= MODIFF
11724 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11725
11726 buffer_unchanged_p
11727 = (!NILP (w->window_end_valid)
11728 && !current_buffer->clip_changed
11729 && XFASTINT (w->last_modified) >= MODIFF
11730 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11731
11732 /* When windows_or_buffers_changed is non-zero, we can't rely on
11733 the window end being valid, so set it to nil there. */
11734 if (windows_or_buffers_changed)
11735 {
11736 /* If window starts on a continuation line, maybe adjust the
11737 window start in case the window's width changed. */
11738 if (XMARKER (w->start)->buffer == current_buffer)
11739 compute_window_start_on_continuation_line (w);
11740
11741 w->window_end_valid = Qnil;
11742 }
11743
11744 /* Some sanity checks. */
11745 CHECK_WINDOW_END (w);
11746 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
11747 abort ();
11748 if (BYTEPOS (opoint) < CHARPOS (opoint))
11749 abort ();
11750
11751 /* If %c is in mode line, update it if needed. */
11752 if (!NILP (w->column_number_displayed)
11753 /* This alternative quickly identifies a common case
11754 where no change is needed. */
11755 && !(PT == XFASTINT (w->last_point)
11756 && XFASTINT (w->last_modified) >= MODIFF
11757 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11758 && (XFASTINT (w->column_number_displayed)
11759 != (int) current_column ())) /* iftc */
11760 update_mode_line = 1;
11761
11762 /* Count number of windows showing the selected buffer. An indirect
11763 buffer counts as its base buffer. */
11764 if (!just_this_one_p)
11765 {
11766 struct buffer *current_base, *window_base;
11767 current_base = current_buffer;
11768 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11769 if (current_base->base_buffer)
11770 current_base = current_base->base_buffer;
11771 if (window_base->base_buffer)
11772 window_base = window_base->base_buffer;
11773 if (current_base == window_base)
11774 buffer_shared++;
11775 }
11776
11777 /* Point refers normally to the selected window. For any other
11778 window, set up appropriate value. */
11779 if (!EQ (window, selected_window))
11780 {
11781 int new_pt = XMARKER (w->pointm)->charpos;
11782 int new_pt_byte = marker_byte_position (w->pointm);
11783 if (new_pt < BEGV)
11784 {
11785 new_pt = BEGV;
11786 new_pt_byte = BEGV_BYTE;
11787 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
11788 }
11789 else if (new_pt > (ZV - 1))
11790 {
11791 new_pt = ZV;
11792 new_pt_byte = ZV_BYTE;
11793 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
11794 }
11795
11796 /* We don't use SET_PT so that the point-motion hooks don't run. */
11797 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
11798 }
11799
11800 /* If any of the character widths specified in the display table
11801 have changed, invalidate the width run cache. It's true that
11802 this may be a bit late to catch such changes, but the rest of
11803 redisplay goes (non-fatally) haywire when the display table is
11804 changed, so why should we worry about doing any better? */
11805 if (current_buffer->width_run_cache)
11806 {
11807 struct Lisp_Char_Table *disptab = buffer_display_table ();
11808
11809 if (! disptab_matches_widthtab (disptab,
11810 XVECTOR (current_buffer->width_table)))
11811 {
11812 invalidate_region_cache (current_buffer,
11813 current_buffer->width_run_cache,
11814 BEG, Z);
11815 recompute_width_table (current_buffer, disptab);
11816 }
11817 }
11818
11819 /* If window-start is screwed up, choose a new one. */
11820 if (XMARKER (w->start)->buffer != current_buffer)
11821 goto recenter;
11822
11823 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11824
11825 /* If someone specified a new starting point but did not insist,
11826 check whether it can be used. */
11827 if (!NILP (w->optional_new_start)
11828 && CHARPOS (startp) >= BEGV
11829 && CHARPOS (startp) <= ZV)
11830 {
11831 w->optional_new_start = Qnil;
11832 start_display (&it, w, startp);
11833 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11834 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11835 if (IT_CHARPOS (it) == PT)
11836 w->force_start = Qt;
11837 /* IT may overshoot PT if text at PT is invisible. */
11838 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
11839 w->force_start = Qt;
11840
11841
11842 }
11843
11844 /* Handle case where place to start displaying has been specified,
11845 unless the specified location is outside the accessible range. */
11846 if (!NILP (w->force_start)
11847 || w->frozen_window_start_p)
11848 {
11849 /* We set this later on if we have to adjust point. */
11850 int new_vpos = -1;
11851
11852 w->force_start = Qnil;
11853 w->vscroll = 0;
11854 w->window_end_valid = Qnil;
11855
11856 /* Forget any recorded base line for line number display. */
11857 if (!buffer_unchanged_p)
11858 w->base_line_number = Qnil;
11859
11860 /* Redisplay the mode line. Select the buffer properly for that.
11861 Also, run the hook window-scroll-functions
11862 because we have scrolled. */
11863 /* Note, we do this after clearing force_start because
11864 if there's an error, it is better to forget about force_start
11865 than to get into an infinite loop calling the hook functions
11866 and having them get more errors. */
11867 if (!update_mode_line
11868 || ! NILP (Vwindow_scroll_functions))
11869 {
11870 update_mode_line = 1;
11871 w->update_mode_line = Qt;
11872 startp = run_window_scroll_functions (window, startp);
11873 }
11874
11875 w->last_modified = make_number (0);
11876 w->last_overlay_modified = make_number (0);
11877 if (CHARPOS (startp) < BEGV)
11878 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
11879 else if (CHARPOS (startp) > ZV)
11880 SET_TEXT_POS (startp, ZV, ZV_BYTE);
11881
11882 /* Redisplay, then check if cursor has been set during the
11883 redisplay. Give up if new fonts were loaded. */
11884 if (!try_window (window, startp))
11885 {
11886 w->force_start = Qt;
11887 clear_glyph_matrix (w->desired_matrix);
11888 goto need_larger_matrices;
11889 }
11890
11891 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
11892 {
11893 /* If point does not appear, try to move point so it does
11894 appear. The desired matrix has been built above, so we
11895 can use it here. */
11896 new_vpos = window_box_height (w) / 2;
11897 }
11898
11899 if (!make_cursor_line_fully_visible (w, 0))
11900 {
11901 /* Point does appear, but on a line partly visible at end of window.
11902 Move it back to a fully-visible line. */
11903 new_vpos = window_box_height (w);
11904 }
11905
11906 /* If we need to move point for either of the above reasons,
11907 now actually do it. */
11908 if (new_vpos >= 0)
11909 {
11910 struct glyph_row *row;
11911
11912 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
11913 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
11914 ++row;
11915
11916 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
11917 MATRIX_ROW_START_BYTEPOS (row));
11918
11919 if (w != XWINDOW (selected_window))
11920 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
11921 else if (current_buffer == old)
11922 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11923
11924 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
11925
11926 /* If we are highlighting the region, then we just changed
11927 the region, so redisplay to show it. */
11928 if (!NILP (Vtransient_mark_mode)
11929 && !NILP (current_buffer->mark_active))
11930 {
11931 clear_glyph_matrix (w->desired_matrix);
11932 if (!try_window (window, startp))
11933 goto need_larger_matrices;
11934 }
11935 }
11936
11937 #if GLYPH_DEBUG
11938 debug_method_add (w, "forced window start");
11939 #endif
11940 goto done;
11941 }
11942
11943 /* Handle case where text has not changed, only point, and it has
11944 not moved off the frame, and we are not retrying after hscroll.
11945 (current_matrix_up_to_date_p is nonzero when retrying.) */
11946 if (current_matrix_up_to_date_p
11947 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
11948 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
11949 {
11950 switch (rc)
11951 {
11952 case CURSOR_MOVEMENT_SUCCESS:
11953 used_current_matrix_p = 1;
11954 goto done;
11955
11956 #if 0 /* try_cursor_movement never returns this value. */
11957 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
11958 goto need_larger_matrices;
11959 #endif
11960
11961 case CURSOR_MOVEMENT_MUST_SCROLL:
11962 goto try_to_scroll;
11963
11964 default:
11965 abort ();
11966 }
11967 }
11968 /* If current starting point was originally the beginning of a line
11969 but no longer is, find a new starting point. */
11970 else if (!NILP (w->start_at_line_beg)
11971 && !(CHARPOS (startp) <= BEGV
11972 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
11973 {
11974 #if GLYPH_DEBUG
11975 debug_method_add (w, "recenter 1");
11976 #endif
11977 goto recenter;
11978 }
11979
11980 /* Try scrolling with try_window_id. Value is > 0 if update has
11981 been done, it is -1 if we know that the same window start will
11982 not work. It is 0 if unsuccessful for some other reason. */
11983 else if ((tem = try_window_id (w)) != 0)
11984 {
11985 #if GLYPH_DEBUG
11986 debug_method_add (w, "try_window_id %d", tem);
11987 #endif
11988
11989 if (fonts_changed_p)
11990 goto need_larger_matrices;
11991 if (tem > 0)
11992 goto done;
11993
11994 /* Otherwise try_window_id has returned -1 which means that we
11995 don't want the alternative below this comment to execute. */
11996 }
11997 else if (CHARPOS (startp) >= BEGV
11998 && CHARPOS (startp) <= ZV
11999 && PT >= CHARPOS (startp)
12000 && (CHARPOS (startp) < ZV
12001 /* Avoid starting at end of buffer. */
12002 || CHARPOS (startp) == BEGV
12003 || (XFASTINT (w->last_modified) >= MODIFF
12004 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
12005 {
12006 #if GLYPH_DEBUG
12007 debug_method_add (w, "same window start");
12008 #endif
12009
12010 /* Try to redisplay starting at same place as before.
12011 If point has not moved off frame, accept the results. */
12012 if (!current_matrix_up_to_date_p
12013 /* Don't use try_window_reusing_current_matrix in this case
12014 because a window scroll function can have changed the
12015 buffer. */
12016 || !NILP (Vwindow_scroll_functions)
12017 || MINI_WINDOW_P (w)
12018 || !(used_current_matrix_p
12019 = try_window_reusing_current_matrix (w)))
12020 {
12021 IF_DEBUG (debug_method_add (w, "1"));
12022 try_window (window, startp);
12023 }
12024
12025 if (fonts_changed_p)
12026 goto need_larger_matrices;
12027
12028 if (w->cursor.vpos >= 0)
12029 {
12030 if (!just_this_one_p
12031 || current_buffer->clip_changed
12032 || BEG_UNCHANGED < CHARPOS (startp))
12033 /* Forget any recorded base line for line number display. */
12034 w->base_line_number = Qnil;
12035
12036 if (!make_cursor_line_fully_visible (w, 1))
12037 {
12038 clear_glyph_matrix (w->desired_matrix);
12039 last_line_misfit = 1;
12040 }
12041 /* Drop through and scroll. */
12042 else
12043 goto done;
12044 }
12045 else
12046 clear_glyph_matrix (w->desired_matrix);
12047 }
12048
12049 try_to_scroll:
12050
12051 w->last_modified = make_number (0);
12052 w->last_overlay_modified = make_number (0);
12053
12054 /* Redisplay the mode line. Select the buffer properly for that. */
12055 if (!update_mode_line)
12056 {
12057 update_mode_line = 1;
12058 w->update_mode_line = Qt;
12059 }
12060
12061 /* Try to scroll by specified few lines. */
12062 if ((scroll_conservatively
12063 || scroll_step
12064 || temp_scroll_step
12065 || NUMBERP (current_buffer->scroll_up_aggressively)
12066 || NUMBERP (current_buffer->scroll_down_aggressively))
12067 && !current_buffer->clip_changed
12068 && CHARPOS (startp) >= BEGV
12069 && CHARPOS (startp) <= ZV)
12070 {
12071 /* The function returns -1 if new fonts were loaded, 1 if
12072 successful, 0 if not successful. */
12073 int rc = try_scrolling (window, just_this_one_p,
12074 scroll_conservatively,
12075 scroll_step,
12076 temp_scroll_step, last_line_misfit);
12077 switch (rc)
12078 {
12079 case SCROLLING_SUCCESS:
12080 goto done;
12081
12082 case SCROLLING_NEED_LARGER_MATRICES:
12083 goto need_larger_matrices;
12084
12085 case SCROLLING_FAILED:
12086 break;
12087
12088 default:
12089 abort ();
12090 }
12091 }
12092
12093 /* Finally, just choose place to start which centers point */
12094
12095 recenter:
12096 centering_position = window_box_height (w) / 2;
12097
12098 point_at_top:
12099 /* Jump here with centering_position already set to 0. */
12100
12101 #if GLYPH_DEBUG
12102 debug_method_add (w, "recenter");
12103 #endif
12104
12105 /* w->vscroll = 0; */
12106
12107 /* Forget any previously recorded base line for line number display. */
12108 if (!buffer_unchanged_p)
12109 w->base_line_number = Qnil;
12110
12111 /* Move backward half the height of the window. */
12112 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12113 it.current_y = it.last_visible_y;
12114 move_it_vertically_backward (&it, centering_position);
12115 xassert (IT_CHARPOS (it) >= BEGV);
12116
12117 /* The function move_it_vertically_backward may move over more
12118 than the specified y-distance. If it->w is small, e.g. a
12119 mini-buffer window, we may end up in front of the window's
12120 display area. Start displaying at the start of the line
12121 containing PT in this case. */
12122 if (it.current_y <= 0)
12123 {
12124 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12125 move_it_vertically (&it, 0);
12126 xassert (IT_CHARPOS (it) <= PT);
12127 it.current_y = 0;
12128 }
12129
12130 it.current_x = it.hpos = 0;
12131
12132 /* Set startp here explicitly in case that helps avoid an infinite loop
12133 in case the window-scroll-functions functions get errors. */
12134 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12135
12136 /* Run scroll hooks. */
12137 startp = run_window_scroll_functions (window, it.current.pos);
12138
12139 /* Redisplay the window. */
12140 if (!current_matrix_up_to_date_p
12141 || windows_or_buffers_changed
12142 || cursor_type_changed
12143 /* Don't use try_window_reusing_current_matrix in this case
12144 because it can have changed the buffer. */
12145 || !NILP (Vwindow_scroll_functions)
12146 || !just_this_one_p
12147 || MINI_WINDOW_P (w)
12148 || !(used_current_matrix_p
12149 = try_window_reusing_current_matrix (w)))
12150 try_window (window, startp);
12151
12152 /* If new fonts have been loaded (due to fontsets), give up. We
12153 have to start a new redisplay since we need to re-adjust glyph
12154 matrices. */
12155 if (fonts_changed_p)
12156 goto need_larger_matrices;
12157
12158 /* If cursor did not appear assume that the middle of the window is
12159 in the first line of the window. Do it again with the next line.
12160 (Imagine a window of height 100, displaying two lines of height
12161 60. Moving back 50 from it->last_visible_y will end in the first
12162 line.) */
12163 if (w->cursor.vpos < 0)
12164 {
12165 if (!NILP (w->window_end_valid)
12166 && PT >= Z - XFASTINT (w->window_end_pos))
12167 {
12168 clear_glyph_matrix (w->desired_matrix);
12169 move_it_by_lines (&it, 1, 0);
12170 try_window (window, it.current.pos);
12171 }
12172 else if (PT < IT_CHARPOS (it))
12173 {
12174 clear_glyph_matrix (w->desired_matrix);
12175 move_it_by_lines (&it, -1, 0);
12176 try_window (window, it.current.pos);
12177 }
12178 else
12179 {
12180 /* Not much we can do about it. */
12181 }
12182 }
12183
12184 /* Consider the following case: Window starts at BEGV, there is
12185 invisible, intangible text at BEGV, so that display starts at
12186 some point START > BEGV. It can happen that we are called with
12187 PT somewhere between BEGV and START. Try to handle that case. */
12188 if (w->cursor.vpos < 0)
12189 {
12190 struct glyph_row *row = w->current_matrix->rows;
12191 if (row->mode_line_p)
12192 ++row;
12193 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12194 }
12195
12196 if (!make_cursor_line_fully_visible (w, centering_position > 0))
12197 {
12198 /* If vscroll is enabled, disable it and try again. */
12199 if (w->vscroll)
12200 {
12201 w->vscroll = 0;
12202 clear_glyph_matrix (w->desired_matrix);
12203 goto recenter;
12204 }
12205
12206 /* If centering point failed to make the whole line visible,
12207 put point at the top instead. That has to make the whole line
12208 visible, if it can be done. */
12209 clear_glyph_matrix (w->desired_matrix);
12210 centering_position = 0;
12211 goto point_at_top;
12212 }
12213
12214 done:
12215
12216 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12217 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12218 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12219 ? Qt : Qnil);
12220
12221 /* Display the mode line, if we must. */
12222 if ((update_mode_line
12223 /* If window not full width, must redo its mode line
12224 if (a) the window to its side is being redone and
12225 (b) we do a frame-based redisplay. This is a consequence
12226 of how inverted lines are drawn in frame-based redisplay. */
12227 || (!just_this_one_p
12228 && !FRAME_WINDOW_P (f)
12229 && !WINDOW_FULL_WIDTH_P (w))
12230 /* Line number to display. */
12231 || INTEGERP (w->base_line_pos)
12232 /* Column number is displayed and different from the one displayed. */
12233 || (!NILP (w->column_number_displayed)
12234 && (XFASTINT (w->column_number_displayed)
12235 != (int) current_column ()))) /* iftc */
12236 /* This means that the window has a mode line. */
12237 && (WINDOW_WANTS_MODELINE_P (w)
12238 || WINDOW_WANTS_HEADER_LINE_P (w)))
12239 {
12240 display_mode_lines (w);
12241
12242 /* If mode line height has changed, arrange for a thorough
12243 immediate redisplay using the correct mode line height. */
12244 if (WINDOW_WANTS_MODELINE_P (w)
12245 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12246 {
12247 fonts_changed_p = 1;
12248 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12249 = DESIRED_MODE_LINE_HEIGHT (w);
12250 }
12251
12252 /* If top line height has changed, arrange for a thorough
12253 immediate redisplay using the correct mode line height. */
12254 if (WINDOW_WANTS_HEADER_LINE_P (w)
12255 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12256 {
12257 fonts_changed_p = 1;
12258 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12259 = DESIRED_HEADER_LINE_HEIGHT (w);
12260 }
12261
12262 if (fonts_changed_p)
12263 goto need_larger_matrices;
12264 }
12265
12266 if (!line_number_displayed
12267 && !BUFFERP (w->base_line_pos))
12268 {
12269 w->base_line_pos = Qnil;
12270 w->base_line_number = Qnil;
12271 }
12272
12273 finish_menu_bars:
12274
12275 /* When we reach a frame's selected window, redo the frame's menu bar. */
12276 if (update_mode_line
12277 && EQ (FRAME_SELECTED_WINDOW (f), window))
12278 {
12279 int redisplay_menu_p = 0;
12280 int redisplay_tool_bar_p = 0;
12281
12282 if (FRAME_WINDOW_P (f))
12283 {
12284 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12285 || defined (USE_GTK)
12286 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12287 #else
12288 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12289 #endif
12290 }
12291 else
12292 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12293
12294 if (redisplay_menu_p)
12295 display_menu_bar (w);
12296
12297 #ifdef HAVE_WINDOW_SYSTEM
12298 #ifdef USE_GTK
12299 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12300 #else
12301 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12302 && (FRAME_TOOL_BAR_LINES (f) > 0
12303 || auto_resize_tool_bars_p);
12304
12305 #endif
12306
12307 if (redisplay_tool_bar_p)
12308 redisplay_tool_bar (f);
12309 #endif
12310 }
12311
12312 #ifdef HAVE_WINDOW_SYSTEM
12313 if (update_window_fringes (w, 0)
12314 && !just_this_one_p
12315 && (used_current_matrix_p || overlay_arrow_seen)
12316 && !w->pseudo_window_p)
12317 {
12318 update_begin (f);
12319 BLOCK_INPUT;
12320 draw_window_fringes (w);
12321 UNBLOCK_INPUT;
12322 update_end (f);
12323 }
12324 #endif /* HAVE_WINDOW_SYSTEM */
12325
12326 /* We go to this label, with fonts_changed_p nonzero,
12327 if it is necessary to try again using larger glyph matrices.
12328 We have to redeem the scroll bar even in this case,
12329 because the loop in redisplay_internal expects that. */
12330 need_larger_matrices:
12331 ;
12332 finish_scroll_bars:
12333
12334 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12335 {
12336 /* Set the thumb's position and size. */
12337 set_vertical_scroll_bar (w);
12338
12339 /* Note that we actually used the scroll bar attached to this
12340 window, so it shouldn't be deleted at the end of redisplay. */
12341 redeem_scroll_bar_hook (w);
12342 }
12343
12344 /* Restore current_buffer and value of point in it. */
12345 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12346 set_buffer_internal_1 (old);
12347 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12348
12349 unbind_to (count, Qnil);
12350 }
12351
12352
12353 /* Build the complete desired matrix of WINDOW with a window start
12354 buffer position POS. Value is non-zero if successful. It is zero
12355 if fonts were loaded during redisplay which makes re-adjusting
12356 glyph matrices necessary. */
12357
12358 int
12359 try_window (window, pos)
12360 Lisp_Object window;
12361 struct text_pos pos;
12362 {
12363 struct window *w = XWINDOW (window);
12364 struct it it;
12365 struct glyph_row *last_text_row = NULL;
12366
12367 /* Make POS the new window start. */
12368 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12369
12370 /* Mark cursor position as unknown. No overlay arrow seen. */
12371 w->cursor.vpos = -1;
12372 overlay_arrow_seen = 0;
12373
12374 /* Initialize iterator and info to start at POS. */
12375 start_display (&it, w, pos);
12376
12377 /* Display all lines of W. */
12378 while (it.current_y < it.last_visible_y)
12379 {
12380 if (display_line (&it))
12381 last_text_row = it.glyph_row - 1;
12382 if (fonts_changed_p)
12383 return 0;
12384 }
12385
12386 /* If bottom moved off end of frame, change mode line percentage. */
12387 if (XFASTINT (w->window_end_pos) <= 0
12388 && Z != IT_CHARPOS (it))
12389 w->update_mode_line = Qt;
12390
12391 /* Set window_end_pos to the offset of the last character displayed
12392 on the window from the end of current_buffer. Set
12393 window_end_vpos to its row number. */
12394 if (last_text_row)
12395 {
12396 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12397 w->window_end_bytepos
12398 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12399 w->window_end_pos
12400 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12401 w->window_end_vpos
12402 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12403 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12404 ->displays_text_p);
12405 }
12406 else
12407 {
12408 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12409 w->window_end_pos = make_number (Z - ZV);
12410 w->window_end_vpos = make_number (0);
12411 }
12412
12413 /* But that is not valid info until redisplay finishes. */
12414 w->window_end_valid = Qnil;
12415 return 1;
12416 }
12417
12418
12419 \f
12420 /************************************************************************
12421 Window redisplay reusing current matrix when buffer has not changed
12422 ************************************************************************/
12423
12424 /* Try redisplay of window W showing an unchanged buffer with a
12425 different window start than the last time it was displayed by
12426 reusing its current matrix. Value is non-zero if successful.
12427 W->start is the new window start. */
12428
12429 static int
12430 try_window_reusing_current_matrix (w)
12431 struct window *w;
12432 {
12433 struct frame *f = XFRAME (w->frame);
12434 struct glyph_row *row, *bottom_row;
12435 struct it it;
12436 struct run run;
12437 struct text_pos start, new_start;
12438 int nrows_scrolled, i;
12439 struct glyph_row *last_text_row;
12440 struct glyph_row *last_reused_text_row;
12441 struct glyph_row *start_row;
12442 int start_vpos, min_y, max_y;
12443
12444 #if GLYPH_DEBUG
12445 if (inhibit_try_window_reusing)
12446 return 0;
12447 #endif
12448
12449 if (/* This function doesn't handle terminal frames. */
12450 !FRAME_WINDOW_P (f)
12451 /* Don't try to reuse the display if windows have been split
12452 or such. */
12453 || windows_or_buffers_changed
12454 || cursor_type_changed)
12455 return 0;
12456
12457 /* Can't do this if region may have changed. */
12458 if ((!NILP (Vtransient_mark_mode)
12459 && !NILP (current_buffer->mark_active))
12460 || !NILP (w->region_showing)
12461 || !NILP (Vshow_trailing_whitespace))
12462 return 0;
12463
12464 /* If top-line visibility has changed, give up. */
12465 if (WINDOW_WANTS_HEADER_LINE_P (w)
12466 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12467 return 0;
12468
12469 /* Give up if old or new display is scrolled vertically. We could
12470 make this function handle this, but right now it doesn't. */
12471 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12472 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
12473 return 0;
12474
12475 /* The variable new_start now holds the new window start. The old
12476 start `start' can be determined from the current matrix. */
12477 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12478 start = start_row->start.pos;
12479 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12480
12481 /* Clear the desired matrix for the display below. */
12482 clear_glyph_matrix (w->desired_matrix);
12483
12484 if (CHARPOS (new_start) <= CHARPOS (start))
12485 {
12486 int first_row_y;
12487
12488 /* Don't use this method if the display starts with an ellipsis
12489 displayed for invisible text. It's not easy to handle that case
12490 below, and it's certainly not worth the effort since this is
12491 not a frequent case. */
12492 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12493 return 0;
12494
12495 IF_DEBUG (debug_method_add (w, "twu1"));
12496
12497 /* Display up to a row that can be reused. The variable
12498 last_text_row is set to the last row displayed that displays
12499 text. Note that it.vpos == 0 if or if not there is a
12500 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12501 start_display (&it, w, new_start);
12502 first_row_y = it.current_y;
12503 w->cursor.vpos = -1;
12504 last_text_row = last_reused_text_row = NULL;
12505
12506 while (it.current_y < it.last_visible_y
12507 && !fonts_changed_p)
12508 {
12509 /* If we have reached into the characters in the START row,
12510 that means the line boundaries have changed. So we
12511 can't start copying with the row START. Maybe it will
12512 work to start copying with the following row. */
12513 while (IT_CHARPOS (it) > CHARPOS (start))
12514 {
12515 /* Advance to the next row as the "start". */
12516 start_row++;
12517 start = start_row->start.pos;
12518 /* If there are no more rows to try, or just one, give up. */
12519 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
12520 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row)
12521 || CHARPOS (start) == ZV)
12522 {
12523 clear_glyph_matrix (w->desired_matrix);
12524 return 0;
12525 }
12526
12527 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12528 }
12529 /* If we have reached alignment,
12530 we can copy the rest of the rows. */
12531 if (IT_CHARPOS (it) == CHARPOS (start))
12532 break;
12533
12534 if (display_line (&it))
12535 last_text_row = it.glyph_row - 1;
12536 }
12537
12538 /* A value of current_y < last_visible_y means that we stopped
12539 at the previous window start, which in turn means that we
12540 have at least one reusable row. */
12541 if (it.current_y < it.last_visible_y)
12542 {
12543 /* IT.vpos always starts from 0; it counts text lines. */
12544 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
12545
12546 /* Find PT if not already found in the lines displayed. */
12547 if (w->cursor.vpos < 0)
12548 {
12549 int dy = it.current_y - start_row->y;
12550
12551 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12552 row = row_containing_pos (w, PT, row, NULL, dy);
12553 if (row)
12554 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12555 dy, nrows_scrolled);
12556 else
12557 {
12558 clear_glyph_matrix (w->desired_matrix);
12559 return 0;
12560 }
12561 }
12562
12563 /* Scroll the display. Do it before the current matrix is
12564 changed. The problem here is that update has not yet
12565 run, i.e. part of the current matrix is not up to date.
12566 scroll_run_hook will clear the cursor, and use the
12567 current matrix to get the height of the row the cursor is
12568 in. */
12569 run.current_y = start_row->y;
12570 run.desired_y = it.current_y;
12571 run.height = it.last_visible_y - it.current_y;
12572
12573 if (run.height > 0 && run.current_y != run.desired_y)
12574 {
12575 update_begin (f);
12576 rif->update_window_begin_hook (w);
12577 rif->clear_window_mouse_face (w);
12578 rif->scroll_run_hook (w, &run);
12579 rif->update_window_end_hook (w, 0, 0);
12580 update_end (f);
12581 }
12582
12583 /* Shift current matrix down by nrows_scrolled lines. */
12584 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12585 rotate_matrix (w->current_matrix,
12586 start_vpos,
12587 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12588 nrows_scrolled);
12589
12590 /* Disable lines that must be updated. */
12591 for (i = 0; i < it.vpos; ++i)
12592 (start_row + i)->enabled_p = 0;
12593
12594 /* Re-compute Y positions. */
12595 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12596 max_y = it.last_visible_y;
12597 for (row = start_row + nrows_scrolled;
12598 row < bottom_row;
12599 ++row)
12600 {
12601 row->y = it.current_y;
12602 row->visible_height = row->height;
12603
12604 if (row->y < min_y)
12605 row->visible_height -= min_y - row->y;
12606 if (row->y + row->height > max_y)
12607 row->visible_height -= row->y + row->height - max_y;
12608 row->redraw_fringe_bitmaps_p = 1;
12609
12610 it.current_y += row->height;
12611
12612 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12613 last_reused_text_row = row;
12614 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12615 break;
12616 }
12617
12618 /* Disable lines in the current matrix which are now
12619 below the window. */
12620 for (++row; row < bottom_row; ++row)
12621 row->enabled_p = 0;
12622 }
12623
12624 /* Update window_end_pos etc.; last_reused_text_row is the last
12625 reused row from the current matrix containing text, if any.
12626 The value of last_text_row is the last displayed line
12627 containing text. */
12628 if (last_reused_text_row)
12629 {
12630 w->window_end_bytepos
12631 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12632 w->window_end_pos
12633 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12634 w->window_end_vpos
12635 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12636 w->current_matrix));
12637 }
12638 else if (last_text_row)
12639 {
12640 w->window_end_bytepos
12641 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12642 w->window_end_pos
12643 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12644 w->window_end_vpos
12645 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12646 }
12647 else
12648 {
12649 /* This window must be completely empty. */
12650 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12651 w->window_end_pos = make_number (Z - ZV);
12652 w->window_end_vpos = make_number (0);
12653 }
12654 w->window_end_valid = Qnil;
12655
12656 /* Update hint: don't try scrolling again in update_window. */
12657 w->desired_matrix->no_scrolling_p = 1;
12658
12659 #if GLYPH_DEBUG
12660 debug_method_add (w, "try_window_reusing_current_matrix 1");
12661 #endif
12662 return 1;
12663 }
12664 else if (CHARPOS (new_start) > CHARPOS (start))
12665 {
12666 struct glyph_row *pt_row, *row;
12667 struct glyph_row *first_reusable_row;
12668 struct glyph_row *first_row_to_display;
12669 int dy;
12670 int yb = window_text_bottom_y (w);
12671
12672 /* Find the row starting at new_start, if there is one. Don't
12673 reuse a partially visible line at the end. */
12674 first_reusable_row = start_row;
12675 while (first_reusable_row->enabled_p
12676 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12677 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12678 < CHARPOS (new_start)))
12679 ++first_reusable_row;
12680
12681 /* Give up if there is no row to reuse. */
12682 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
12683 || !first_reusable_row->enabled_p
12684 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12685 != CHARPOS (new_start)))
12686 return 0;
12687
12688 /* We can reuse fully visible rows beginning with
12689 first_reusable_row to the end of the window. Set
12690 first_row_to_display to the first row that cannot be reused.
12691 Set pt_row to the row containing point, if there is any. */
12692 pt_row = NULL;
12693 for (first_row_to_display = first_reusable_row;
12694 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12695 ++first_row_to_display)
12696 {
12697 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12698 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
12699 pt_row = first_row_to_display;
12700 }
12701
12702 /* Start displaying at the start of first_row_to_display. */
12703 xassert (first_row_to_display->y < yb);
12704 init_to_row_start (&it, w, first_row_to_display);
12705
12706 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12707 - start_vpos);
12708 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12709 - nrows_scrolled);
12710 it.current_y = (first_row_to_display->y - first_reusable_row->y
12711 + WINDOW_HEADER_LINE_HEIGHT (w));
12712
12713 /* Display lines beginning with first_row_to_display in the
12714 desired matrix. Set last_text_row to the last row displayed
12715 that displays text. */
12716 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12717 if (pt_row == NULL)
12718 w->cursor.vpos = -1;
12719 last_text_row = NULL;
12720 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12721 if (display_line (&it))
12722 last_text_row = it.glyph_row - 1;
12723
12724 /* Give up If point isn't in a row displayed or reused. */
12725 if (w->cursor.vpos < 0)
12726 {
12727 clear_glyph_matrix (w->desired_matrix);
12728 return 0;
12729 }
12730
12731 /* If point is in a reused row, adjust y and vpos of the cursor
12732 position. */
12733 if (pt_row)
12734 {
12735 w->cursor.vpos -= nrows_scrolled;
12736 w->cursor.y -= first_reusable_row->y - start_row->y;
12737 }
12738
12739 /* Scroll the display. */
12740 run.current_y = first_reusable_row->y;
12741 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
12742 run.height = it.last_visible_y - run.current_y;
12743 dy = run.current_y - run.desired_y;
12744
12745 if (run.height)
12746 {
12747 update_begin (f);
12748 rif->update_window_begin_hook (w);
12749 rif->clear_window_mouse_face (w);
12750 rif->scroll_run_hook (w, &run);
12751 rif->update_window_end_hook (w, 0, 0);
12752 update_end (f);
12753 }
12754
12755 /* Adjust Y positions of reused rows. */
12756 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12757 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12758 max_y = it.last_visible_y;
12759 for (row = first_reusable_row; row < first_row_to_display; ++row)
12760 {
12761 row->y -= dy;
12762 row->visible_height = row->height;
12763 if (row->y < min_y)
12764 row->visible_height -= min_y - row->y;
12765 if (row->y + row->height > max_y)
12766 row->visible_height -= row->y + row->height - max_y;
12767 row->redraw_fringe_bitmaps_p = 1;
12768 }
12769
12770 /* Scroll the current matrix. */
12771 xassert (nrows_scrolled > 0);
12772 rotate_matrix (w->current_matrix,
12773 start_vpos,
12774 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12775 -nrows_scrolled);
12776
12777 /* Disable rows not reused. */
12778 for (row -= nrows_scrolled; row < bottom_row; ++row)
12779 row->enabled_p = 0;
12780
12781 /* Point may have moved to a different line, so we cannot assume that
12782 the previous cursor position is valid; locate the correct row. */
12783 if (pt_row)
12784 {
12785 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12786 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
12787 row++)
12788 {
12789 w->cursor.vpos++;
12790 w->cursor.y = row->y;
12791 }
12792 if (row < bottom_row)
12793 {
12794 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
12795 while (glyph->charpos < PT)
12796 {
12797 w->cursor.hpos++;
12798 w->cursor.x += glyph->pixel_width;
12799 glyph++;
12800 }
12801 }
12802 }
12803
12804 /* Adjust window end. A null value of last_text_row means that
12805 the window end is in reused rows which in turn means that
12806 only its vpos can have changed. */
12807 if (last_text_row)
12808 {
12809 w->window_end_bytepos
12810 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12811 w->window_end_pos
12812 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12813 w->window_end_vpos
12814 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12815 }
12816 else
12817 {
12818 w->window_end_vpos
12819 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
12820 }
12821
12822 w->window_end_valid = Qnil;
12823 w->desired_matrix->no_scrolling_p = 1;
12824
12825 #if GLYPH_DEBUG
12826 debug_method_add (w, "try_window_reusing_current_matrix 2");
12827 #endif
12828 return 1;
12829 }
12830
12831 return 0;
12832 }
12833
12834
12835 \f
12836 /************************************************************************
12837 Window redisplay reusing current matrix when buffer has changed
12838 ************************************************************************/
12839
12840 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
12841 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
12842 int *, int *));
12843 static struct glyph_row *
12844 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
12845 struct glyph_row *));
12846
12847
12848 /* Return the last row in MATRIX displaying text. If row START is
12849 non-null, start searching with that row. IT gives the dimensions
12850 of the display. Value is null if matrix is empty; otherwise it is
12851 a pointer to the row found. */
12852
12853 static struct glyph_row *
12854 find_last_row_displaying_text (matrix, it, start)
12855 struct glyph_matrix *matrix;
12856 struct it *it;
12857 struct glyph_row *start;
12858 {
12859 struct glyph_row *row, *row_found;
12860
12861 /* Set row_found to the last row in IT->w's current matrix
12862 displaying text. The loop looks funny but think of partially
12863 visible lines. */
12864 row_found = NULL;
12865 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
12866 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12867 {
12868 xassert (row->enabled_p);
12869 row_found = row;
12870 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
12871 break;
12872 ++row;
12873 }
12874
12875 return row_found;
12876 }
12877
12878
12879 /* Return the last row in the current matrix of W that is not affected
12880 by changes at the start of current_buffer that occurred since W's
12881 current matrix was built. Value is null if no such row exists.
12882
12883 BEG_UNCHANGED us the number of characters unchanged at the start of
12884 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
12885 first changed character in current_buffer. Characters at positions <
12886 BEG + BEG_UNCHANGED are at the same buffer positions as they were
12887 when the current matrix was built. */
12888
12889 static struct glyph_row *
12890 find_last_unchanged_at_beg_row (w)
12891 struct window *w;
12892 {
12893 int first_changed_pos = BEG + BEG_UNCHANGED;
12894 struct glyph_row *row;
12895 struct glyph_row *row_found = NULL;
12896 int yb = window_text_bottom_y (w);
12897
12898 /* Find the last row displaying unchanged text. */
12899 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12900 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12901 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
12902 {
12903 if (/* If row ends before first_changed_pos, it is unchanged,
12904 except in some case. */
12905 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
12906 /* When row ends in ZV and we write at ZV it is not
12907 unchanged. */
12908 && !row->ends_at_zv_p
12909 /* When first_changed_pos is the end of a continued line,
12910 row is not unchanged because it may be no longer
12911 continued. */
12912 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
12913 && (row->continued_p
12914 || row->exact_window_width_line_p)))
12915 row_found = row;
12916
12917 /* Stop if last visible row. */
12918 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
12919 break;
12920
12921 ++row;
12922 }
12923
12924 return row_found;
12925 }
12926
12927
12928 /* Find the first glyph row in the current matrix of W that is not
12929 affected by changes at the end of current_buffer since the
12930 time W's current matrix was built.
12931
12932 Return in *DELTA the number of chars by which buffer positions in
12933 unchanged text at the end of current_buffer must be adjusted.
12934
12935 Return in *DELTA_BYTES the corresponding number of bytes.
12936
12937 Value is null if no such row exists, i.e. all rows are affected by
12938 changes. */
12939
12940 static struct glyph_row *
12941 find_first_unchanged_at_end_row (w, delta, delta_bytes)
12942 struct window *w;
12943 int *delta, *delta_bytes;
12944 {
12945 struct glyph_row *row;
12946 struct glyph_row *row_found = NULL;
12947
12948 *delta = *delta_bytes = 0;
12949
12950 /* Display must not have been paused, otherwise the current matrix
12951 is not up to date. */
12952 if (NILP (w->window_end_valid))
12953 abort ();
12954
12955 /* A value of window_end_pos >= END_UNCHANGED means that the window
12956 end is in the range of changed text. If so, there is no
12957 unchanged row at the end of W's current matrix. */
12958 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
12959 return NULL;
12960
12961 /* Set row to the last row in W's current matrix displaying text. */
12962 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12963
12964 /* If matrix is entirely empty, no unchanged row exists. */
12965 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12966 {
12967 /* The value of row is the last glyph row in the matrix having a
12968 meaningful buffer position in it. The end position of row
12969 corresponds to window_end_pos. This allows us to translate
12970 buffer positions in the current matrix to current buffer
12971 positions for characters not in changed text. */
12972 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12973 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12974 int last_unchanged_pos, last_unchanged_pos_old;
12975 struct glyph_row *first_text_row
12976 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12977
12978 *delta = Z - Z_old;
12979 *delta_bytes = Z_BYTE - Z_BYTE_old;
12980
12981 /* Set last_unchanged_pos to the buffer position of the last
12982 character in the buffer that has not been changed. Z is the
12983 index + 1 of the last character in current_buffer, i.e. by
12984 subtracting END_UNCHANGED we get the index of the last
12985 unchanged character, and we have to add BEG to get its buffer
12986 position. */
12987 last_unchanged_pos = Z - END_UNCHANGED + BEG;
12988 last_unchanged_pos_old = last_unchanged_pos - *delta;
12989
12990 /* Search backward from ROW for a row displaying a line that
12991 starts at a minimum position >= last_unchanged_pos_old. */
12992 for (; row > first_text_row; --row)
12993 {
12994 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
12995 abort ();
12996
12997 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
12998 row_found = row;
12999 }
13000 }
13001
13002 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
13003 abort ();
13004
13005 return row_found;
13006 }
13007
13008
13009 /* Make sure that glyph rows in the current matrix of window W
13010 reference the same glyph memory as corresponding rows in the
13011 frame's frame matrix. This function is called after scrolling W's
13012 current matrix on a terminal frame in try_window_id and
13013 try_window_reusing_current_matrix. */
13014
13015 static void
13016 sync_frame_with_window_matrix_rows (w)
13017 struct window *w;
13018 {
13019 struct frame *f = XFRAME (w->frame);
13020 struct glyph_row *window_row, *window_row_end, *frame_row;
13021
13022 /* Preconditions: W must be a leaf window and full-width. Its frame
13023 must have a frame matrix. */
13024 xassert (NILP (w->hchild) && NILP (w->vchild));
13025 xassert (WINDOW_FULL_WIDTH_P (w));
13026 xassert (!FRAME_WINDOW_P (f));
13027
13028 /* If W is a full-width window, glyph pointers in W's current matrix
13029 have, by definition, to be the same as glyph pointers in the
13030 corresponding frame matrix. Note that frame matrices have no
13031 marginal areas (see build_frame_matrix). */
13032 window_row = w->current_matrix->rows;
13033 window_row_end = window_row + w->current_matrix->nrows;
13034 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
13035 while (window_row < window_row_end)
13036 {
13037 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
13038 struct glyph *end = window_row->glyphs[LAST_AREA];
13039
13040 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
13041 frame_row->glyphs[TEXT_AREA] = start;
13042 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
13043 frame_row->glyphs[LAST_AREA] = end;
13044
13045 /* Disable frame rows whose corresponding window rows have
13046 been disabled in try_window_id. */
13047 if (!window_row->enabled_p)
13048 frame_row->enabled_p = 0;
13049
13050 ++window_row, ++frame_row;
13051 }
13052 }
13053
13054
13055 /* Find the glyph row in window W containing CHARPOS. Consider all
13056 rows between START and END (not inclusive). END null means search
13057 all rows to the end of the display area of W. Value is the row
13058 containing CHARPOS or null. */
13059
13060 struct glyph_row *
13061 row_containing_pos (w, charpos, start, end, dy)
13062 struct window *w;
13063 int charpos;
13064 struct glyph_row *start, *end;
13065 int dy;
13066 {
13067 struct glyph_row *row = start;
13068 int last_y;
13069
13070 /* If we happen to start on a header-line, skip that. */
13071 if (row->mode_line_p)
13072 ++row;
13073
13074 if ((end && row >= end) || !row->enabled_p)
13075 return NULL;
13076
13077 last_y = window_text_bottom_y (w) - dy;
13078
13079 while (1)
13080 {
13081 /* Give up if we have gone too far. */
13082 if (end && row >= end)
13083 return NULL;
13084 /* This formerly returned if they were equal.
13085 I think that both quantities are of a "last plus one" type;
13086 if so, when they are equal, the row is within the screen. -- rms. */
13087 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13088 return NULL;
13089
13090 /* If it is in this row, return this row. */
13091 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13092 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13093 /* The end position of a row equals the start
13094 position of the next row. If CHARPOS is there, we
13095 would rather display it in the next line, except
13096 when this line ends in ZV. */
13097 && !row->ends_at_zv_p
13098 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13099 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13100 return row;
13101 ++row;
13102 }
13103 }
13104
13105
13106 /* Try to redisplay window W by reusing its existing display. W's
13107 current matrix must be up to date when this function is called,
13108 i.e. window_end_valid must not be nil.
13109
13110 Value is
13111
13112 1 if display has been updated
13113 0 if otherwise unsuccessful
13114 -1 if redisplay with same window start is known not to succeed
13115
13116 The following steps are performed:
13117
13118 1. Find the last row in the current matrix of W that is not
13119 affected by changes at the start of current_buffer. If no such row
13120 is found, give up.
13121
13122 2. Find the first row in W's current matrix that is not affected by
13123 changes at the end of current_buffer. Maybe there is no such row.
13124
13125 3. Display lines beginning with the row + 1 found in step 1 to the
13126 row found in step 2 or, if step 2 didn't find a row, to the end of
13127 the window.
13128
13129 4. If cursor is not known to appear on the window, give up.
13130
13131 5. If display stopped at the row found in step 2, scroll the
13132 display and current matrix as needed.
13133
13134 6. Maybe display some lines at the end of W, if we must. This can
13135 happen under various circumstances, like a partially visible line
13136 becoming fully visible, or because newly displayed lines are displayed
13137 in smaller font sizes.
13138
13139 7. Update W's window end information. */
13140
13141 static int
13142 try_window_id (w)
13143 struct window *w;
13144 {
13145 struct frame *f = XFRAME (w->frame);
13146 struct glyph_matrix *current_matrix = w->current_matrix;
13147 struct glyph_matrix *desired_matrix = w->desired_matrix;
13148 struct glyph_row *last_unchanged_at_beg_row;
13149 struct glyph_row *first_unchanged_at_end_row;
13150 struct glyph_row *row;
13151 struct glyph_row *bottom_row;
13152 int bottom_vpos;
13153 struct it it;
13154 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13155 struct text_pos start_pos;
13156 struct run run;
13157 int first_unchanged_at_end_vpos = 0;
13158 struct glyph_row *last_text_row, *last_text_row_at_end;
13159 struct text_pos start;
13160 int first_changed_charpos, last_changed_charpos;
13161
13162 #if GLYPH_DEBUG
13163 if (inhibit_try_window_id)
13164 return 0;
13165 #endif
13166
13167 /* This is handy for debugging. */
13168 #if 0
13169 #define GIVE_UP(X) \
13170 do { \
13171 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13172 return 0; \
13173 } while (0)
13174 #else
13175 #define GIVE_UP(X) return 0
13176 #endif
13177
13178 SET_TEXT_POS_FROM_MARKER (start, w->start);
13179
13180 /* Don't use this for mini-windows because these can show
13181 messages and mini-buffers, and we don't handle that here. */
13182 if (MINI_WINDOW_P (w))
13183 GIVE_UP (1);
13184
13185 /* This flag is used to prevent redisplay optimizations. */
13186 if (windows_or_buffers_changed || cursor_type_changed)
13187 GIVE_UP (2);
13188
13189 /* Verify that narrowing has not changed.
13190 Also verify that we were not told to prevent redisplay optimizations.
13191 It would be nice to further
13192 reduce the number of cases where this prevents try_window_id. */
13193 if (current_buffer->clip_changed
13194 || current_buffer->prevent_redisplay_optimizations_p)
13195 GIVE_UP (3);
13196
13197 /* Window must either use window-based redisplay or be full width. */
13198 if (!FRAME_WINDOW_P (f)
13199 && (!line_ins_del_ok
13200 || !WINDOW_FULL_WIDTH_P (w)))
13201 GIVE_UP (4);
13202
13203 /* Give up if point is not known NOT to appear in W. */
13204 if (PT < CHARPOS (start))
13205 GIVE_UP (5);
13206
13207 /* Another way to prevent redisplay optimizations. */
13208 if (XFASTINT (w->last_modified) == 0)
13209 GIVE_UP (6);
13210
13211 /* Verify that window is not hscrolled. */
13212 if (XFASTINT (w->hscroll) != 0)
13213 GIVE_UP (7);
13214
13215 /* Verify that display wasn't paused. */
13216 if (NILP (w->window_end_valid))
13217 GIVE_UP (8);
13218
13219 /* Can't use this if highlighting a region because a cursor movement
13220 will do more than just set the cursor. */
13221 if (!NILP (Vtransient_mark_mode)
13222 && !NILP (current_buffer->mark_active))
13223 GIVE_UP (9);
13224
13225 /* Likewise if highlighting trailing whitespace. */
13226 if (!NILP (Vshow_trailing_whitespace))
13227 GIVE_UP (11);
13228
13229 /* Likewise if showing a region. */
13230 if (!NILP (w->region_showing))
13231 GIVE_UP (10);
13232
13233 /* Can use this if overlay arrow position and or string have changed. */
13234 if (overlay_arrows_changed_p ())
13235 GIVE_UP (12);
13236
13237
13238 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13239 only if buffer has really changed. The reason is that the gap is
13240 initially at Z for freshly visited files. The code below would
13241 set end_unchanged to 0 in that case. */
13242 if (MODIFF > SAVE_MODIFF
13243 /* This seems to happen sometimes after saving a buffer. */
13244 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13245 {
13246 if (GPT - BEG < BEG_UNCHANGED)
13247 BEG_UNCHANGED = GPT - BEG;
13248 if (Z - GPT < END_UNCHANGED)
13249 END_UNCHANGED = Z - GPT;
13250 }
13251
13252 /* The position of the first and last character that has been changed. */
13253 first_changed_charpos = BEG + BEG_UNCHANGED;
13254 last_changed_charpos = Z - END_UNCHANGED;
13255
13256 /* If window starts after a line end, and the last change is in
13257 front of that newline, then changes don't affect the display.
13258 This case happens with stealth-fontification. Note that although
13259 the display is unchanged, glyph positions in the matrix have to
13260 be adjusted, of course. */
13261 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13262 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13263 && ((last_changed_charpos < CHARPOS (start)
13264 && CHARPOS (start) == BEGV)
13265 || (last_changed_charpos < CHARPOS (start) - 1
13266 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13267 {
13268 int Z_old, delta, Z_BYTE_old, delta_bytes;
13269 struct glyph_row *r0;
13270
13271 /* Compute how many chars/bytes have been added to or removed
13272 from the buffer. */
13273 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13274 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13275 delta = Z - Z_old;
13276 delta_bytes = Z_BYTE - Z_BYTE_old;
13277
13278 /* Give up if PT is not in the window. Note that it already has
13279 been checked at the start of try_window_id that PT is not in
13280 front of the window start. */
13281 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13282 GIVE_UP (13);
13283
13284 /* If window start is unchanged, we can reuse the whole matrix
13285 as is, after adjusting glyph positions. No need to compute
13286 the window end again, since its offset from Z hasn't changed. */
13287 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13288 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13289 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13290 /* PT must not be in a partially visible line. */
13291 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13292 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13293 {
13294 /* Adjust positions in the glyph matrix. */
13295 if (delta || delta_bytes)
13296 {
13297 struct glyph_row *r1
13298 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13299 increment_matrix_positions (w->current_matrix,
13300 MATRIX_ROW_VPOS (r0, current_matrix),
13301 MATRIX_ROW_VPOS (r1, current_matrix),
13302 delta, delta_bytes);
13303 }
13304
13305 /* Set the cursor. */
13306 row = row_containing_pos (w, PT, r0, NULL, 0);
13307 if (row)
13308 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13309 else
13310 abort ();
13311 return 1;
13312 }
13313 }
13314
13315 /* Handle the case that changes are all below what is displayed in
13316 the window, and that PT is in the window. This shortcut cannot
13317 be taken if ZV is visible in the window, and text has been added
13318 there that is visible in the window. */
13319 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13320 /* ZV is not visible in the window, or there are no
13321 changes at ZV, actually. */
13322 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13323 || first_changed_charpos == last_changed_charpos))
13324 {
13325 struct glyph_row *r0;
13326
13327 /* Give up if PT is not in the window. Note that it already has
13328 been checked at the start of try_window_id that PT is not in
13329 front of the window start. */
13330 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13331 GIVE_UP (14);
13332
13333 /* If window start is unchanged, we can reuse the whole matrix
13334 as is, without changing glyph positions since no text has
13335 been added/removed in front of the window end. */
13336 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13337 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13338 /* PT must not be in a partially visible line. */
13339 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13340 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13341 {
13342 /* We have to compute the window end anew since text
13343 can have been added/removed after it. */
13344 w->window_end_pos
13345 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13346 w->window_end_bytepos
13347 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13348
13349 /* Set the cursor. */
13350 row = row_containing_pos (w, PT, r0, NULL, 0);
13351 if (row)
13352 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13353 else
13354 abort ();
13355 return 2;
13356 }
13357 }
13358
13359 /* Give up if window start is in the changed area.
13360
13361 The condition used to read
13362
13363 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13364
13365 but why that was tested escapes me at the moment. */
13366 if (CHARPOS (start) >= first_changed_charpos
13367 && CHARPOS (start) <= last_changed_charpos)
13368 GIVE_UP (15);
13369
13370 /* Check that window start agrees with the start of the first glyph
13371 row in its current matrix. Check this after we know the window
13372 start is not in changed text, otherwise positions would not be
13373 comparable. */
13374 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13375 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13376 GIVE_UP (16);
13377
13378 /* Give up if the window ends in strings. Overlay strings
13379 at the end are difficult to handle, so don't try. */
13380 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13381 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13382 GIVE_UP (20);
13383
13384 /* Compute the position at which we have to start displaying new
13385 lines. Some of the lines at the top of the window might be
13386 reusable because they are not displaying changed text. Find the
13387 last row in W's current matrix not affected by changes at the
13388 start of current_buffer. Value is null if changes start in the
13389 first line of window. */
13390 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13391 if (last_unchanged_at_beg_row)
13392 {
13393 /* Avoid starting to display in the moddle of a character, a TAB
13394 for instance. This is easier than to set up the iterator
13395 exactly, and it's not a frequent case, so the additional
13396 effort wouldn't really pay off. */
13397 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13398 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13399 && last_unchanged_at_beg_row > w->current_matrix->rows)
13400 --last_unchanged_at_beg_row;
13401
13402 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13403 GIVE_UP (17);
13404
13405 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13406 GIVE_UP (18);
13407 start_pos = it.current.pos;
13408
13409 /* Start displaying new lines in the desired matrix at the same
13410 vpos we would use in the current matrix, i.e. below
13411 last_unchanged_at_beg_row. */
13412 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13413 current_matrix);
13414 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13415 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13416
13417 xassert (it.hpos == 0 && it.current_x == 0);
13418 }
13419 else
13420 {
13421 /* There are no reusable lines at the start of the window.
13422 Start displaying in the first text line. */
13423 start_display (&it, w, start);
13424 it.vpos = it.first_vpos;
13425 start_pos = it.current.pos;
13426 }
13427
13428 /* Find the first row that is not affected by changes at the end of
13429 the buffer. Value will be null if there is no unchanged row, in
13430 which case we must redisplay to the end of the window. delta
13431 will be set to the value by which buffer positions beginning with
13432 first_unchanged_at_end_row have to be adjusted due to text
13433 changes. */
13434 first_unchanged_at_end_row
13435 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13436 IF_DEBUG (debug_delta = delta);
13437 IF_DEBUG (debug_delta_bytes = delta_bytes);
13438
13439 /* Set stop_pos to the buffer position up to which we will have to
13440 display new lines. If first_unchanged_at_end_row != NULL, this
13441 is the buffer position of the start of the line displayed in that
13442 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13443 that we don't stop at a buffer position. */
13444 stop_pos = 0;
13445 if (first_unchanged_at_end_row)
13446 {
13447 xassert (last_unchanged_at_beg_row == NULL
13448 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13449
13450 /* If this is a continuation line, move forward to the next one
13451 that isn't. Changes in lines above affect this line.
13452 Caution: this may move first_unchanged_at_end_row to a row
13453 not displaying text. */
13454 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13455 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13456 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13457 < it.last_visible_y))
13458 ++first_unchanged_at_end_row;
13459
13460 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13461 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13462 >= it.last_visible_y))
13463 first_unchanged_at_end_row = NULL;
13464 else
13465 {
13466 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13467 + delta);
13468 first_unchanged_at_end_vpos
13469 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13470 xassert (stop_pos >= Z - END_UNCHANGED);
13471 }
13472 }
13473 else if (last_unchanged_at_beg_row == NULL)
13474 GIVE_UP (19);
13475
13476
13477 #if GLYPH_DEBUG
13478
13479 /* Either there is no unchanged row at the end, or the one we have
13480 now displays text. This is a necessary condition for the window
13481 end pos calculation at the end of this function. */
13482 xassert (first_unchanged_at_end_row == NULL
13483 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13484
13485 debug_last_unchanged_at_beg_vpos
13486 = (last_unchanged_at_beg_row
13487 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13488 : -1);
13489 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13490
13491 #endif /* GLYPH_DEBUG != 0 */
13492
13493
13494 /* Display new lines. Set last_text_row to the last new line
13495 displayed which has text on it, i.e. might end up as being the
13496 line where the window_end_vpos is. */
13497 w->cursor.vpos = -1;
13498 last_text_row = NULL;
13499 overlay_arrow_seen = 0;
13500 while (it.current_y < it.last_visible_y
13501 && !fonts_changed_p
13502 && (first_unchanged_at_end_row == NULL
13503 || IT_CHARPOS (it) < stop_pos))
13504 {
13505 if (display_line (&it))
13506 last_text_row = it.glyph_row - 1;
13507 }
13508
13509 if (fonts_changed_p)
13510 return -1;
13511
13512
13513 /* Compute differences in buffer positions, y-positions etc. for
13514 lines reused at the bottom of the window. Compute what we can
13515 scroll. */
13516 if (first_unchanged_at_end_row
13517 /* No lines reused because we displayed everything up to the
13518 bottom of the window. */
13519 && it.current_y < it.last_visible_y)
13520 {
13521 dvpos = (it.vpos
13522 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13523 current_matrix));
13524 dy = it.current_y - first_unchanged_at_end_row->y;
13525 run.current_y = first_unchanged_at_end_row->y;
13526 run.desired_y = run.current_y + dy;
13527 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13528 }
13529 else
13530 {
13531 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13532 first_unchanged_at_end_row = NULL;
13533 }
13534 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13535
13536
13537 /* Find the cursor if not already found. We have to decide whether
13538 PT will appear on this window (it sometimes doesn't, but this is
13539 not a very frequent case.) This decision has to be made before
13540 the current matrix is altered. A value of cursor.vpos < 0 means
13541 that PT is either in one of the lines beginning at
13542 first_unchanged_at_end_row or below the window. Don't care for
13543 lines that might be displayed later at the window end; as
13544 mentioned, this is not a frequent case. */
13545 if (w->cursor.vpos < 0)
13546 {
13547 /* Cursor in unchanged rows at the top? */
13548 if (PT < CHARPOS (start_pos)
13549 && last_unchanged_at_beg_row)
13550 {
13551 row = row_containing_pos (w, PT,
13552 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13553 last_unchanged_at_beg_row + 1, 0);
13554 if (row)
13555 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13556 }
13557
13558 /* Start from first_unchanged_at_end_row looking for PT. */
13559 else if (first_unchanged_at_end_row)
13560 {
13561 row = row_containing_pos (w, PT - delta,
13562 first_unchanged_at_end_row, NULL, 0);
13563 if (row)
13564 set_cursor_from_row (w, row, w->current_matrix, delta,
13565 delta_bytes, dy, dvpos);
13566 }
13567
13568 /* Give up if cursor was not found. */
13569 if (w->cursor.vpos < 0)
13570 {
13571 clear_glyph_matrix (w->desired_matrix);
13572 return -1;
13573 }
13574 }
13575
13576 /* Don't let the cursor end in the scroll margins. */
13577 {
13578 int this_scroll_margin, cursor_height;
13579
13580 this_scroll_margin = max (0, scroll_margin);
13581 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13582 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13583 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13584
13585 if ((w->cursor.y < this_scroll_margin
13586 && CHARPOS (start) > BEGV)
13587 /* Old redisplay didn't take scroll margin into account at the bottom,
13588 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
13589 || w->cursor.y + cursor_height + this_scroll_margin > it.last_visible_y)
13590 {
13591 w->cursor.vpos = -1;
13592 clear_glyph_matrix (w->desired_matrix);
13593 return -1;
13594 }
13595 }
13596
13597 /* Scroll the display. Do it before changing the current matrix so
13598 that xterm.c doesn't get confused about where the cursor glyph is
13599 found. */
13600 if (dy && run.height)
13601 {
13602 update_begin (f);
13603
13604 if (FRAME_WINDOW_P (f))
13605 {
13606 rif->update_window_begin_hook (w);
13607 rif->clear_window_mouse_face (w);
13608 rif->scroll_run_hook (w, &run);
13609 rif->update_window_end_hook (w, 0, 0);
13610 }
13611 else
13612 {
13613 /* Terminal frame. In this case, dvpos gives the number of
13614 lines to scroll by; dvpos < 0 means scroll up. */
13615 int first_unchanged_at_end_vpos
13616 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13617 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13618 int end = (WINDOW_TOP_EDGE_LINE (w)
13619 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13620 + window_internal_height (w));
13621
13622 /* Perform the operation on the screen. */
13623 if (dvpos > 0)
13624 {
13625 /* Scroll last_unchanged_at_beg_row to the end of the
13626 window down dvpos lines. */
13627 set_terminal_window (end);
13628
13629 /* On dumb terminals delete dvpos lines at the end
13630 before inserting dvpos empty lines. */
13631 if (!scroll_region_ok)
13632 ins_del_lines (end - dvpos, -dvpos);
13633
13634 /* Insert dvpos empty lines in front of
13635 last_unchanged_at_beg_row. */
13636 ins_del_lines (from, dvpos);
13637 }
13638 else if (dvpos < 0)
13639 {
13640 /* Scroll up last_unchanged_at_beg_vpos to the end of
13641 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13642 set_terminal_window (end);
13643
13644 /* Delete dvpos lines in front of
13645 last_unchanged_at_beg_vpos. ins_del_lines will set
13646 the cursor to the given vpos and emit |dvpos| delete
13647 line sequences. */
13648 ins_del_lines (from + dvpos, dvpos);
13649
13650 /* On a dumb terminal insert dvpos empty lines at the
13651 end. */
13652 if (!scroll_region_ok)
13653 ins_del_lines (end + dvpos, -dvpos);
13654 }
13655
13656 set_terminal_window (0);
13657 }
13658
13659 update_end (f);
13660 }
13661
13662 /* Shift reused rows of the current matrix to the right position.
13663 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13664 text. */
13665 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13666 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
13667 if (dvpos < 0)
13668 {
13669 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13670 bottom_vpos, dvpos);
13671 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13672 bottom_vpos, 0);
13673 }
13674 else if (dvpos > 0)
13675 {
13676 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13677 bottom_vpos, dvpos);
13678 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13679 first_unchanged_at_end_vpos + dvpos, 0);
13680 }
13681
13682 /* For frame-based redisplay, make sure that current frame and window
13683 matrix are in sync with respect to glyph memory. */
13684 if (!FRAME_WINDOW_P (f))
13685 sync_frame_with_window_matrix_rows (w);
13686
13687 /* Adjust buffer positions in reused rows. */
13688 if (delta)
13689 increment_matrix_positions (current_matrix,
13690 first_unchanged_at_end_vpos + dvpos,
13691 bottom_vpos, delta, delta_bytes);
13692
13693 /* Adjust Y positions. */
13694 if (dy)
13695 shift_glyph_matrix (w, current_matrix,
13696 first_unchanged_at_end_vpos + dvpos,
13697 bottom_vpos, dy);
13698
13699 if (first_unchanged_at_end_row)
13700 first_unchanged_at_end_row += dvpos;
13701
13702 /* If scrolling up, there may be some lines to display at the end of
13703 the window. */
13704 last_text_row_at_end = NULL;
13705 if (dy < 0)
13706 {
13707 /* Scrolling up can leave for example a partially visible line
13708 at the end of the window to be redisplayed. */
13709 /* Set last_row to the glyph row in the current matrix where the
13710 window end line is found. It has been moved up or down in
13711 the matrix by dvpos. */
13712 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13713 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13714
13715 /* If last_row is the window end line, it should display text. */
13716 xassert (last_row->displays_text_p);
13717
13718 /* If window end line was partially visible before, begin
13719 displaying at that line. Otherwise begin displaying with the
13720 line following it. */
13721 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13722 {
13723 init_to_row_start (&it, w, last_row);
13724 it.vpos = last_vpos;
13725 it.current_y = last_row->y;
13726 }
13727 else
13728 {
13729 init_to_row_end (&it, w, last_row);
13730 it.vpos = 1 + last_vpos;
13731 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13732 ++last_row;
13733 }
13734
13735 /* We may start in a continuation line. If so, we have to
13736 get the right continuation_lines_width and current_x. */
13737 it.continuation_lines_width = last_row->continuation_lines_width;
13738 it.hpos = it.current_x = 0;
13739
13740 /* Display the rest of the lines at the window end. */
13741 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13742 while (it.current_y < it.last_visible_y
13743 && !fonts_changed_p)
13744 {
13745 /* Is it always sure that the display agrees with lines in
13746 the current matrix? I don't think so, so we mark rows
13747 displayed invalid in the current matrix by setting their
13748 enabled_p flag to zero. */
13749 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13750 if (display_line (&it))
13751 last_text_row_at_end = it.glyph_row - 1;
13752 }
13753 }
13754
13755 /* Update window_end_pos and window_end_vpos. */
13756 if (first_unchanged_at_end_row
13757 && first_unchanged_at_end_row->y < it.last_visible_y
13758 && !last_text_row_at_end)
13759 {
13760 /* Window end line if one of the preserved rows from the current
13761 matrix. Set row to the last row displaying text in current
13762 matrix starting at first_unchanged_at_end_row, after
13763 scrolling. */
13764 xassert (first_unchanged_at_end_row->displays_text_p);
13765 row = find_last_row_displaying_text (w->current_matrix, &it,
13766 first_unchanged_at_end_row);
13767 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
13768
13769 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13770 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13771 w->window_end_vpos
13772 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
13773 xassert (w->window_end_bytepos >= 0);
13774 IF_DEBUG (debug_method_add (w, "A"));
13775 }
13776 else if (last_text_row_at_end)
13777 {
13778 w->window_end_pos
13779 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
13780 w->window_end_bytepos
13781 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
13782 w->window_end_vpos
13783 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
13784 xassert (w->window_end_bytepos >= 0);
13785 IF_DEBUG (debug_method_add (w, "B"));
13786 }
13787 else if (last_text_row)
13788 {
13789 /* We have displayed either to the end of the window or at the
13790 end of the window, i.e. the last row with text is to be found
13791 in the desired matrix. */
13792 w->window_end_pos
13793 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13794 w->window_end_bytepos
13795 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13796 w->window_end_vpos
13797 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
13798 xassert (w->window_end_bytepos >= 0);
13799 }
13800 else if (first_unchanged_at_end_row == NULL
13801 && last_text_row == NULL
13802 && last_text_row_at_end == NULL)
13803 {
13804 /* Displayed to end of window, but no line containing text was
13805 displayed. Lines were deleted at the end of the window. */
13806 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
13807 int vpos = XFASTINT (w->window_end_vpos);
13808 struct glyph_row *current_row = current_matrix->rows + vpos;
13809 struct glyph_row *desired_row = desired_matrix->rows + vpos;
13810
13811 for (row = NULL;
13812 row == NULL && vpos >= first_vpos;
13813 --vpos, --current_row, --desired_row)
13814 {
13815 if (desired_row->enabled_p)
13816 {
13817 if (desired_row->displays_text_p)
13818 row = desired_row;
13819 }
13820 else if (current_row->displays_text_p)
13821 row = current_row;
13822 }
13823
13824 xassert (row != NULL);
13825 w->window_end_vpos = make_number (vpos + 1);
13826 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13827 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13828 xassert (w->window_end_bytepos >= 0);
13829 IF_DEBUG (debug_method_add (w, "C"));
13830 }
13831 else
13832 abort ();
13833
13834 #if 0 /* This leads to problems, for instance when the cursor is
13835 at ZV, and the cursor line displays no text. */
13836 /* Disable rows below what's displayed in the window. This makes
13837 debugging easier. */
13838 enable_glyph_matrix_rows (current_matrix,
13839 XFASTINT (w->window_end_vpos) + 1,
13840 bottom_vpos, 0);
13841 #endif
13842
13843 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
13844 debug_end_vpos = XFASTINT (w->window_end_vpos));
13845
13846 /* Record that display has not been completed. */
13847 w->window_end_valid = Qnil;
13848 w->desired_matrix->no_scrolling_p = 1;
13849 return 3;
13850
13851 #undef GIVE_UP
13852 }
13853
13854
13855 \f
13856 /***********************************************************************
13857 More debugging support
13858 ***********************************************************************/
13859
13860 #if GLYPH_DEBUG
13861
13862 void dump_glyph_row P_ ((struct glyph_row *, int, int));
13863 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
13864 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
13865
13866
13867 /* Dump the contents of glyph matrix MATRIX on stderr.
13868
13869 GLYPHS 0 means don't show glyph contents.
13870 GLYPHS 1 means show glyphs in short form
13871 GLYPHS > 1 means show glyphs in long form. */
13872
13873 void
13874 dump_glyph_matrix (matrix, glyphs)
13875 struct glyph_matrix *matrix;
13876 int glyphs;
13877 {
13878 int i;
13879 for (i = 0; i < matrix->nrows; ++i)
13880 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
13881 }
13882
13883
13884 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
13885 the glyph row and area where the glyph comes from. */
13886
13887 void
13888 dump_glyph (row, glyph, area)
13889 struct glyph_row *row;
13890 struct glyph *glyph;
13891 int area;
13892 {
13893 if (glyph->type == CHAR_GLYPH)
13894 {
13895 fprintf (stderr,
13896 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13897 glyph - row->glyphs[TEXT_AREA],
13898 'C',
13899 glyph->charpos,
13900 (BUFFERP (glyph->object)
13901 ? 'B'
13902 : (STRINGP (glyph->object)
13903 ? 'S'
13904 : '-')),
13905 glyph->pixel_width,
13906 glyph->u.ch,
13907 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
13908 ? glyph->u.ch
13909 : '.'),
13910 glyph->face_id,
13911 glyph->left_box_line_p,
13912 glyph->right_box_line_p);
13913 }
13914 else if (glyph->type == STRETCH_GLYPH)
13915 {
13916 fprintf (stderr,
13917 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13918 glyph - row->glyphs[TEXT_AREA],
13919 'S',
13920 glyph->charpos,
13921 (BUFFERP (glyph->object)
13922 ? 'B'
13923 : (STRINGP (glyph->object)
13924 ? 'S'
13925 : '-')),
13926 glyph->pixel_width,
13927 0,
13928 '.',
13929 glyph->face_id,
13930 glyph->left_box_line_p,
13931 glyph->right_box_line_p);
13932 }
13933 else if (glyph->type == IMAGE_GLYPH)
13934 {
13935 fprintf (stderr,
13936 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13937 glyph - row->glyphs[TEXT_AREA],
13938 'I',
13939 glyph->charpos,
13940 (BUFFERP (glyph->object)
13941 ? 'B'
13942 : (STRINGP (glyph->object)
13943 ? 'S'
13944 : '-')),
13945 glyph->pixel_width,
13946 glyph->u.img_id,
13947 '.',
13948 glyph->face_id,
13949 glyph->left_box_line_p,
13950 glyph->right_box_line_p);
13951 }
13952 }
13953
13954
13955 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
13956 GLYPHS 0 means don't show glyph contents.
13957 GLYPHS 1 means show glyphs in short form
13958 GLYPHS > 1 means show glyphs in long form. */
13959
13960 void
13961 dump_glyph_row (row, vpos, glyphs)
13962 struct glyph_row *row;
13963 int vpos, glyphs;
13964 {
13965 if (glyphs != 1)
13966 {
13967 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
13968 fprintf (stderr, "=======================================================================\n");
13969
13970 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
13971 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
13972 vpos,
13973 MATRIX_ROW_START_CHARPOS (row),
13974 MATRIX_ROW_END_CHARPOS (row),
13975 row->used[TEXT_AREA],
13976 row->contains_overlapping_glyphs_p,
13977 row->enabled_p,
13978 row->truncated_on_left_p,
13979 row->truncated_on_right_p,
13980 row->overlay_arrow_p,
13981 row->continued_p,
13982 MATRIX_ROW_CONTINUATION_LINE_P (row),
13983 row->displays_text_p,
13984 row->ends_at_zv_p,
13985 row->fill_line_p,
13986 row->ends_in_middle_of_char_p,
13987 row->starts_in_middle_of_char_p,
13988 row->mouse_face_p,
13989 row->x,
13990 row->y,
13991 row->pixel_width,
13992 row->height,
13993 row->visible_height,
13994 row->ascent,
13995 row->phys_ascent);
13996 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
13997 row->end.overlay_string_index,
13998 row->continuation_lines_width);
13999 fprintf (stderr, "%9d %5d\n",
14000 CHARPOS (row->start.string_pos),
14001 CHARPOS (row->end.string_pos));
14002 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
14003 row->end.dpvec_index);
14004 }
14005
14006 if (glyphs > 1)
14007 {
14008 int area;
14009
14010 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14011 {
14012 struct glyph *glyph = row->glyphs[area];
14013 struct glyph *glyph_end = glyph + row->used[area];
14014
14015 /* Glyph for a line end in text. */
14016 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
14017 ++glyph_end;
14018
14019 if (glyph < glyph_end)
14020 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
14021
14022 for (; glyph < glyph_end; ++glyph)
14023 dump_glyph (row, glyph, area);
14024 }
14025 }
14026 else if (glyphs == 1)
14027 {
14028 int area;
14029
14030 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14031 {
14032 char *s = (char *) alloca (row->used[area] + 1);
14033 int i;
14034
14035 for (i = 0; i < row->used[area]; ++i)
14036 {
14037 struct glyph *glyph = row->glyphs[area] + i;
14038 if (glyph->type == CHAR_GLYPH
14039 && glyph->u.ch < 0x80
14040 && glyph->u.ch >= ' ')
14041 s[i] = glyph->u.ch;
14042 else
14043 s[i] = '.';
14044 }
14045
14046 s[i] = '\0';
14047 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
14048 }
14049 }
14050 }
14051
14052
14053 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
14054 Sdump_glyph_matrix, 0, 1, "p",
14055 doc: /* Dump the current matrix of the selected window to stderr.
14056 Shows contents of glyph row structures. With non-nil
14057 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
14058 glyphs in short form, otherwise show glyphs in long form. */)
14059 (glyphs)
14060 Lisp_Object glyphs;
14061 {
14062 struct window *w = XWINDOW (selected_window);
14063 struct buffer *buffer = XBUFFER (w->buffer);
14064
14065 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14066 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14067 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14068 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14069 fprintf (stderr, "=============================================\n");
14070 dump_glyph_matrix (w->current_matrix,
14071 NILP (glyphs) ? 0 : XINT (glyphs));
14072 return Qnil;
14073 }
14074
14075
14076 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14077 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14078 ()
14079 {
14080 struct frame *f = XFRAME (selected_frame);
14081 dump_glyph_matrix (f->current_matrix, 1);
14082 return Qnil;
14083 }
14084
14085
14086 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
14087 doc: /* Dump glyph row ROW to stderr.
14088 GLYPH 0 means don't dump glyphs.
14089 GLYPH 1 means dump glyphs in short form.
14090 GLYPH > 1 or omitted means dump glyphs in long form. */)
14091 (row, glyphs)
14092 Lisp_Object row, glyphs;
14093 {
14094 struct glyph_matrix *matrix;
14095 int vpos;
14096
14097 CHECK_NUMBER (row);
14098 matrix = XWINDOW (selected_window)->current_matrix;
14099 vpos = XINT (row);
14100 if (vpos >= 0 && vpos < matrix->nrows)
14101 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14102 vpos,
14103 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14104 return Qnil;
14105 }
14106
14107
14108 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14109 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14110 GLYPH 0 means don't dump glyphs.
14111 GLYPH 1 means dump glyphs in short form.
14112 GLYPH > 1 or omitted means dump glyphs in long form. */)
14113 (row, glyphs)
14114 Lisp_Object row, glyphs;
14115 {
14116 struct frame *sf = SELECTED_FRAME ();
14117 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14118 int vpos;
14119
14120 CHECK_NUMBER (row);
14121 vpos = XINT (row);
14122 if (vpos >= 0 && vpos < m->nrows)
14123 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14124 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14125 return Qnil;
14126 }
14127
14128
14129 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14130 doc: /* Toggle tracing of redisplay.
14131 With ARG, turn tracing on if and only if ARG is positive. */)
14132 (arg)
14133 Lisp_Object arg;
14134 {
14135 if (NILP (arg))
14136 trace_redisplay_p = !trace_redisplay_p;
14137 else
14138 {
14139 arg = Fprefix_numeric_value (arg);
14140 trace_redisplay_p = XINT (arg) > 0;
14141 }
14142
14143 return Qnil;
14144 }
14145
14146
14147 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14148 doc: /* Like `format', but print result to stderr.
14149 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14150 (nargs, args)
14151 int nargs;
14152 Lisp_Object *args;
14153 {
14154 Lisp_Object s = Fformat (nargs, args);
14155 fprintf (stderr, "%s", SDATA (s));
14156 return Qnil;
14157 }
14158
14159 #endif /* GLYPH_DEBUG */
14160
14161
14162 \f
14163 /***********************************************************************
14164 Building Desired Matrix Rows
14165 ***********************************************************************/
14166
14167 /* Return a temporary glyph row holding the glyphs of an overlay
14168 arrow. Only used for non-window-redisplay windows. */
14169
14170 static struct glyph_row *
14171 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14172 struct window *w;
14173 Lisp_Object overlay_arrow_string;
14174 {
14175 struct frame *f = XFRAME (WINDOW_FRAME (w));
14176 struct buffer *buffer = XBUFFER (w->buffer);
14177 struct buffer *old = current_buffer;
14178 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14179 int arrow_len = SCHARS (overlay_arrow_string);
14180 const unsigned char *arrow_end = arrow_string + arrow_len;
14181 const unsigned char *p;
14182 struct it it;
14183 int multibyte_p;
14184 int n_glyphs_before;
14185
14186 set_buffer_temp (buffer);
14187 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14188 it.glyph_row->used[TEXT_AREA] = 0;
14189 SET_TEXT_POS (it.position, 0, 0);
14190
14191 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14192 p = arrow_string;
14193 while (p < arrow_end)
14194 {
14195 Lisp_Object face, ilisp;
14196
14197 /* Get the next character. */
14198 if (multibyte_p)
14199 it.c = string_char_and_length (p, arrow_len, &it.len);
14200 else
14201 it.c = *p, it.len = 1;
14202 p += it.len;
14203
14204 /* Get its face. */
14205 ilisp = make_number (p - arrow_string);
14206 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
14207 it.face_id = compute_char_face (f, it.c, face);
14208
14209 /* Compute its width, get its glyphs. */
14210 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14211 SET_TEXT_POS (it.position, -1, -1);
14212 PRODUCE_GLYPHS (&it);
14213
14214 /* If this character doesn't fit any more in the line, we have
14215 to remove some glyphs. */
14216 if (it.current_x > it.last_visible_x)
14217 {
14218 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14219 break;
14220 }
14221 }
14222
14223 set_buffer_temp (old);
14224 return it.glyph_row;
14225 }
14226
14227
14228 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14229 glyphs are only inserted for terminal frames since we can't really
14230 win with truncation glyphs when partially visible glyphs are
14231 involved. Which glyphs to insert is determined by
14232 produce_special_glyphs. */
14233
14234 static void
14235 insert_left_trunc_glyphs (it)
14236 struct it *it;
14237 {
14238 struct it truncate_it;
14239 struct glyph *from, *end, *to, *toend;
14240
14241 xassert (!FRAME_WINDOW_P (it->f));
14242
14243 /* Get the truncation glyphs. */
14244 truncate_it = *it;
14245 truncate_it.current_x = 0;
14246 truncate_it.face_id = DEFAULT_FACE_ID;
14247 truncate_it.glyph_row = &scratch_glyph_row;
14248 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14249 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14250 truncate_it.object = make_number (0);
14251 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14252
14253 /* Overwrite glyphs from IT with truncation glyphs. */
14254 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14255 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14256 to = it->glyph_row->glyphs[TEXT_AREA];
14257 toend = to + it->glyph_row->used[TEXT_AREA];
14258
14259 while (from < end)
14260 *to++ = *from++;
14261
14262 /* There may be padding glyphs left over. Overwrite them too. */
14263 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14264 {
14265 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14266 while (from < end)
14267 *to++ = *from++;
14268 }
14269
14270 if (to > toend)
14271 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14272 }
14273
14274
14275 /* Compute the pixel height and width of IT->glyph_row.
14276
14277 Most of the time, ascent and height of a display line will be equal
14278 to the max_ascent and max_height values of the display iterator
14279 structure. This is not the case if
14280
14281 1. We hit ZV without displaying anything. In this case, max_ascent
14282 and max_height will be zero.
14283
14284 2. We have some glyphs that don't contribute to the line height.
14285 (The glyph row flag contributes_to_line_height_p is for future
14286 pixmap extensions).
14287
14288 The first case is easily covered by using default values because in
14289 these cases, the line height does not really matter, except that it
14290 must not be zero. */
14291
14292 static void
14293 compute_line_metrics (it)
14294 struct it *it;
14295 {
14296 struct glyph_row *row = it->glyph_row;
14297 int area, i;
14298
14299 if (FRAME_WINDOW_P (it->f))
14300 {
14301 int i, min_y, max_y;
14302
14303 /* The line may consist of one space only, that was added to
14304 place the cursor on it. If so, the row's height hasn't been
14305 computed yet. */
14306 if (row->height == 0)
14307 {
14308 if (it->max_ascent + it->max_descent == 0)
14309 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14310 row->ascent = it->max_ascent;
14311 row->height = it->max_ascent + it->max_descent;
14312 row->phys_ascent = it->max_phys_ascent;
14313 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14314 }
14315
14316 /* Compute the width of this line. */
14317 row->pixel_width = row->x;
14318 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14319 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14320
14321 xassert (row->pixel_width >= 0);
14322 xassert (row->ascent >= 0 && row->height > 0);
14323
14324 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14325 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14326
14327 /* If first line's physical ascent is larger than its logical
14328 ascent, use the physical ascent, and make the row taller.
14329 This makes accented characters fully visible. */
14330 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14331 && row->phys_ascent > row->ascent)
14332 {
14333 row->height += row->phys_ascent - row->ascent;
14334 row->ascent = row->phys_ascent;
14335 }
14336
14337 /* Compute how much of the line is visible. */
14338 row->visible_height = row->height;
14339
14340 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14341 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14342
14343 if (row->y < min_y)
14344 row->visible_height -= min_y - row->y;
14345 if (row->y + row->height > max_y)
14346 row->visible_height -= row->y + row->height - max_y;
14347 }
14348 else
14349 {
14350 row->pixel_width = row->used[TEXT_AREA];
14351 if (row->continued_p)
14352 row->pixel_width -= it->continuation_pixel_width;
14353 else if (row->truncated_on_right_p)
14354 row->pixel_width -= it->truncation_pixel_width;
14355 row->ascent = row->phys_ascent = 0;
14356 row->height = row->phys_height = row->visible_height = 1;
14357 }
14358
14359 /* Compute a hash code for this row. */
14360 row->hash = 0;
14361 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14362 for (i = 0; i < row->used[area]; ++i)
14363 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14364 + row->glyphs[area][i].u.val
14365 + row->glyphs[area][i].face_id
14366 + row->glyphs[area][i].padding_p
14367 + (row->glyphs[area][i].type << 2));
14368
14369 it->max_ascent = it->max_descent = 0;
14370 it->max_phys_ascent = it->max_phys_descent = 0;
14371 }
14372
14373
14374 /* Append one space to the glyph row of iterator IT if doing a
14375 window-based redisplay. The space has the same face as
14376 IT->face_id. Value is non-zero if a space was added.
14377
14378 This function is called to make sure that there is always one glyph
14379 at the end of a glyph row that the cursor can be set on under
14380 window-systems. (If there weren't such a glyph we would not know
14381 how wide and tall a box cursor should be displayed).
14382
14383 At the same time this space let's a nicely handle clearing to the
14384 end of the line if the row ends in italic text. */
14385
14386 static int
14387 append_space_for_newline (it, default_face_p)
14388 struct it *it;
14389 int default_face_p;
14390 {
14391 if (FRAME_WINDOW_P (it->f))
14392 {
14393 int n = it->glyph_row->used[TEXT_AREA];
14394
14395 if (it->glyph_row->glyphs[TEXT_AREA] + n
14396 < it->glyph_row->glyphs[1 + TEXT_AREA])
14397 {
14398 /* Save some values that must not be changed.
14399 Must save IT->c and IT->len because otherwise
14400 ITERATOR_AT_END_P wouldn't work anymore after
14401 append_space_for_newline has been called. */
14402 enum display_element_type saved_what = it->what;
14403 int saved_c = it->c, saved_len = it->len;
14404 int saved_x = it->current_x;
14405 int saved_face_id = it->face_id;
14406 struct text_pos saved_pos;
14407 Lisp_Object saved_object;
14408 struct face *face;
14409
14410 saved_object = it->object;
14411 saved_pos = it->position;
14412
14413 it->what = IT_CHARACTER;
14414 bzero (&it->position, sizeof it->position);
14415 it->object = make_number (0);
14416 it->c = ' ';
14417 it->len = 1;
14418
14419 if (default_face_p)
14420 it->face_id = DEFAULT_FACE_ID;
14421 else if (it->face_before_selective_p)
14422 it->face_id = it->saved_face_id;
14423 face = FACE_FROM_ID (it->f, it->face_id);
14424 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
14425
14426 PRODUCE_GLYPHS (it);
14427
14428 it->override_ascent = -1;
14429 it->constrain_row_ascent_descent_p = 0;
14430 it->current_x = saved_x;
14431 it->object = saved_object;
14432 it->position = saved_pos;
14433 it->what = saved_what;
14434 it->face_id = saved_face_id;
14435 it->len = saved_len;
14436 it->c = saved_c;
14437 return 1;
14438 }
14439 }
14440
14441 return 0;
14442 }
14443
14444
14445 /* Extend the face of the last glyph in the text area of IT->glyph_row
14446 to the end of the display line. Called from display_line.
14447 If the glyph row is empty, add a space glyph to it so that we
14448 know the face to draw. Set the glyph row flag fill_line_p. */
14449
14450 static void
14451 extend_face_to_end_of_line (it)
14452 struct it *it;
14453 {
14454 struct face *face;
14455 struct frame *f = it->f;
14456
14457 /* If line is already filled, do nothing. */
14458 if (it->current_x >= it->last_visible_x)
14459 return;
14460
14461 /* Face extension extends the background and box of IT->face_id
14462 to the end of the line. If the background equals the background
14463 of the frame, we don't have to do anything. */
14464 if (it->face_before_selective_p)
14465 face = FACE_FROM_ID (it->f, it->saved_face_id);
14466 else
14467 face = FACE_FROM_ID (f, it->face_id);
14468
14469 if (FRAME_WINDOW_P (f)
14470 && face->box == FACE_NO_BOX
14471 && face->background == FRAME_BACKGROUND_PIXEL (f)
14472 && !face->stipple)
14473 return;
14474
14475 /* Set the glyph row flag indicating that the face of the last glyph
14476 in the text area has to be drawn to the end of the text area. */
14477 it->glyph_row->fill_line_p = 1;
14478
14479 /* If current character of IT is not ASCII, make sure we have the
14480 ASCII face. This will be automatically undone the next time
14481 get_next_display_element returns a multibyte character. Note
14482 that the character will always be single byte in unibyte text. */
14483 if (!ASCII_CHAR_P (it->c))
14484 {
14485 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
14486 }
14487
14488 if (FRAME_WINDOW_P (f))
14489 {
14490 /* If the row is empty, add a space with the current face of IT,
14491 so that we know which face to draw. */
14492 if (it->glyph_row->used[TEXT_AREA] == 0)
14493 {
14494 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14495 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14496 it->glyph_row->used[TEXT_AREA] = 1;
14497 }
14498 }
14499 else
14500 {
14501 /* Save some values that must not be changed. */
14502 int saved_x = it->current_x;
14503 struct text_pos saved_pos;
14504 Lisp_Object saved_object;
14505 enum display_element_type saved_what = it->what;
14506 int saved_face_id = it->face_id;
14507
14508 saved_object = it->object;
14509 saved_pos = it->position;
14510
14511 it->what = IT_CHARACTER;
14512 bzero (&it->position, sizeof it->position);
14513 it->object = make_number (0);
14514 it->c = ' ';
14515 it->len = 1;
14516 it->face_id = face->id;
14517
14518 PRODUCE_GLYPHS (it);
14519
14520 while (it->current_x <= it->last_visible_x)
14521 PRODUCE_GLYPHS (it);
14522
14523 /* Don't count these blanks really. It would let us insert a left
14524 truncation glyph below and make us set the cursor on them, maybe. */
14525 it->current_x = saved_x;
14526 it->object = saved_object;
14527 it->position = saved_pos;
14528 it->what = saved_what;
14529 it->face_id = saved_face_id;
14530 }
14531 }
14532
14533
14534 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14535 trailing whitespace. */
14536
14537 static int
14538 trailing_whitespace_p (charpos)
14539 int charpos;
14540 {
14541 int bytepos = CHAR_TO_BYTE (charpos);
14542 int c = 0;
14543
14544 while (bytepos < ZV_BYTE
14545 && (c = FETCH_CHAR (bytepos),
14546 c == ' ' || c == '\t'))
14547 ++bytepos;
14548
14549 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14550 {
14551 if (bytepos != PT_BYTE)
14552 return 1;
14553 }
14554 return 0;
14555 }
14556
14557
14558 /* Highlight trailing whitespace, if any, in ROW. */
14559
14560 void
14561 highlight_trailing_whitespace (f, row)
14562 struct frame *f;
14563 struct glyph_row *row;
14564 {
14565 int used = row->used[TEXT_AREA];
14566
14567 if (used)
14568 {
14569 struct glyph *start = row->glyphs[TEXT_AREA];
14570 struct glyph *glyph = start + used - 1;
14571
14572 /* Skip over glyphs inserted to display the cursor at the
14573 end of a line, for extending the face of the last glyph
14574 to the end of the line on terminals, and for truncation
14575 and continuation glyphs. */
14576 while (glyph >= start
14577 && glyph->type == CHAR_GLYPH
14578 && INTEGERP (glyph->object))
14579 --glyph;
14580
14581 /* If last glyph is a space or stretch, and it's trailing
14582 whitespace, set the face of all trailing whitespace glyphs in
14583 IT->glyph_row to `trailing-whitespace'. */
14584 if (glyph >= start
14585 && BUFFERP (glyph->object)
14586 && (glyph->type == STRETCH_GLYPH
14587 || (glyph->type == CHAR_GLYPH
14588 && glyph->u.ch == ' '))
14589 && trailing_whitespace_p (glyph->charpos))
14590 {
14591 int face_id = lookup_named_face (f, Qtrailing_whitespace);
14592
14593 while (glyph >= start
14594 && BUFFERP (glyph->object)
14595 && (glyph->type == STRETCH_GLYPH
14596 || (glyph->type == CHAR_GLYPH
14597 && glyph->u.ch == ' ')))
14598 (glyph--)->face_id = face_id;
14599 }
14600 }
14601 }
14602
14603
14604 /* Value is non-zero if glyph row ROW in window W should be
14605 used to hold the cursor. */
14606
14607 static int
14608 cursor_row_p (w, row)
14609 struct window *w;
14610 struct glyph_row *row;
14611 {
14612 int cursor_row_p = 1;
14613
14614 if (PT == MATRIX_ROW_END_CHARPOS (row))
14615 {
14616 /* If the row ends with a newline from a string, we don't want
14617 the cursor there (if the row is continued it doesn't end in a
14618 newline). */
14619 if (CHARPOS (row->end.string_pos) >= 0
14620 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14621 cursor_row_p = row->continued_p;
14622
14623 /* If the row ends at ZV, display the cursor at the end of that
14624 row instead of at the start of the row below. */
14625 else if (row->ends_at_zv_p)
14626 cursor_row_p = 1;
14627 else
14628 cursor_row_p = 0;
14629 }
14630
14631 return cursor_row_p;
14632 }
14633
14634
14635 /* Construct the glyph row IT->glyph_row in the desired matrix of
14636 IT->w from text at the current position of IT. See dispextern.h
14637 for an overview of struct it. Value is non-zero if
14638 IT->glyph_row displays text, as opposed to a line displaying ZV
14639 only. */
14640
14641 static int
14642 display_line (it)
14643 struct it *it;
14644 {
14645 struct glyph_row *row = it->glyph_row;
14646 int overlay_arrow_bitmap;
14647 Lisp_Object overlay_arrow_string;
14648
14649 /* We always start displaying at hpos zero even if hscrolled. */
14650 xassert (it->hpos == 0 && it->current_x == 0);
14651
14652 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14653 >= it->w->desired_matrix->nrows)
14654 {
14655 it->w->nrows_scale_factor++;
14656 fonts_changed_p = 1;
14657 return 0;
14658 }
14659
14660 /* Is IT->w showing the region? */
14661 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
14662
14663 /* Clear the result glyph row and enable it. */
14664 prepare_desired_row (row);
14665
14666 row->y = it->current_y;
14667 row->start = it->start;
14668 row->continuation_lines_width = it->continuation_lines_width;
14669 row->displays_text_p = 1;
14670 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14671 it->starts_in_middle_of_char_p = 0;
14672
14673 /* Arrange the overlays nicely for our purposes. Usually, we call
14674 display_line on only one line at a time, in which case this
14675 can't really hurt too much, or we call it on lines which appear
14676 one after another in the buffer, in which case all calls to
14677 recenter_overlay_lists but the first will be pretty cheap. */
14678 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14679
14680 /* Move over display elements that are not visible because we are
14681 hscrolled. This may stop at an x-position < IT->first_visible_x
14682 if the first glyph is partially visible or if we hit a line end. */
14683 if (it->current_x < it->first_visible_x)
14684 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14685 MOVE_TO_POS | MOVE_TO_X);
14686
14687 /* Get the initial row height. This is either the height of the
14688 text hscrolled, if there is any, or zero. */
14689 row->ascent = it->max_ascent;
14690 row->height = it->max_ascent + it->max_descent;
14691 row->phys_ascent = it->max_phys_ascent;
14692 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14693
14694 /* Loop generating characters. The loop is left with IT on the next
14695 character to display. */
14696 while (1)
14697 {
14698 int n_glyphs_before, hpos_before, x_before;
14699 int x, i, nglyphs;
14700 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
14701
14702 /* Retrieve the next thing to display. Value is zero if end of
14703 buffer reached. */
14704 if (!get_next_display_element (it))
14705 {
14706 /* Maybe add a space at the end of this line that is used to
14707 display the cursor there under X. Set the charpos of the
14708 first glyph of blank lines not corresponding to any text
14709 to -1. */
14710 #ifdef HAVE_WINDOW_SYSTEM
14711 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14712 row->exact_window_width_line_p = 1;
14713 else
14714 #endif /* HAVE_WINDOW_SYSTEM */
14715 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
14716 || row->used[TEXT_AREA] == 0)
14717 {
14718 row->glyphs[TEXT_AREA]->charpos = -1;
14719 row->displays_text_p = 0;
14720
14721 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
14722 && (!MINI_WINDOW_P (it->w)
14723 || (minibuf_level && EQ (it->window, minibuf_window))))
14724 row->indicate_empty_line_p = 1;
14725 }
14726
14727 it->continuation_lines_width = 0;
14728 row->ends_at_zv_p = 1;
14729 break;
14730 }
14731
14732 /* Now, get the metrics of what we want to display. This also
14733 generates glyphs in `row' (which is IT->glyph_row). */
14734 n_glyphs_before = row->used[TEXT_AREA];
14735 x = it->current_x;
14736
14737 /* Remember the line height so far in case the next element doesn't
14738 fit on the line. */
14739 if (!it->truncate_lines_p)
14740 {
14741 ascent = it->max_ascent;
14742 descent = it->max_descent;
14743 phys_ascent = it->max_phys_ascent;
14744 phys_descent = it->max_phys_descent;
14745 }
14746
14747 PRODUCE_GLYPHS (it);
14748
14749 /* If this display element was in marginal areas, continue with
14750 the next one. */
14751 if (it->area != TEXT_AREA)
14752 {
14753 row->ascent = max (row->ascent, it->max_ascent);
14754 row->height = max (row->height, it->max_ascent + it->max_descent);
14755 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14756 row->phys_height = max (row->phys_height,
14757 it->max_phys_ascent + it->max_phys_descent);
14758 set_iterator_to_next (it, 1);
14759 continue;
14760 }
14761
14762 /* Does the display element fit on the line? If we truncate
14763 lines, we should draw past the right edge of the window. If
14764 we don't truncate, we want to stop so that we can display the
14765 continuation glyph before the right margin. If lines are
14766 continued, there are two possible strategies for characters
14767 resulting in more than 1 glyph (e.g. tabs): Display as many
14768 glyphs as possible in this line and leave the rest for the
14769 continuation line, or display the whole element in the next
14770 line. Original redisplay did the former, so we do it also. */
14771 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14772 hpos_before = it->hpos;
14773 x_before = x;
14774
14775 if (/* Not a newline. */
14776 nglyphs > 0
14777 /* Glyphs produced fit entirely in the line. */
14778 && it->current_x < it->last_visible_x)
14779 {
14780 it->hpos += nglyphs;
14781 row->ascent = max (row->ascent, it->max_ascent);
14782 row->height = max (row->height, it->max_ascent + it->max_descent);
14783 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14784 row->phys_height = max (row->phys_height,
14785 it->max_phys_ascent + it->max_phys_descent);
14786 if (it->current_x - it->pixel_width < it->first_visible_x)
14787 row->x = x - it->first_visible_x;
14788 }
14789 else
14790 {
14791 int new_x;
14792 struct glyph *glyph;
14793
14794 for (i = 0; i < nglyphs; ++i, x = new_x)
14795 {
14796 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14797 new_x = x + glyph->pixel_width;
14798
14799 if (/* Lines are continued. */
14800 !it->truncate_lines_p
14801 && (/* Glyph doesn't fit on the line. */
14802 new_x > it->last_visible_x
14803 /* Or it fits exactly on a window system frame. */
14804 || (new_x == it->last_visible_x
14805 && FRAME_WINDOW_P (it->f))))
14806 {
14807 /* End of a continued line. */
14808
14809 if (it->hpos == 0
14810 || (new_x == it->last_visible_x
14811 && FRAME_WINDOW_P (it->f)))
14812 {
14813 /* Current glyph is the only one on the line or
14814 fits exactly on the line. We must continue
14815 the line because we can't draw the cursor
14816 after the glyph. */
14817 row->continued_p = 1;
14818 it->current_x = new_x;
14819 it->continuation_lines_width += new_x;
14820 ++it->hpos;
14821 if (i == nglyphs - 1)
14822 {
14823 set_iterator_to_next (it, 1);
14824 #ifdef HAVE_WINDOW_SYSTEM
14825 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14826 {
14827 if (!get_next_display_element (it))
14828 {
14829 row->exact_window_width_line_p = 1;
14830 it->continuation_lines_width = 0;
14831 row->continued_p = 0;
14832 row->ends_at_zv_p = 1;
14833 }
14834 else if (ITERATOR_AT_END_OF_LINE_P (it))
14835 {
14836 row->continued_p = 0;
14837 row->exact_window_width_line_p = 1;
14838 }
14839 }
14840 #endif /* HAVE_WINDOW_SYSTEM */
14841 }
14842 }
14843 else if (CHAR_GLYPH_PADDING_P (*glyph)
14844 && !FRAME_WINDOW_P (it->f))
14845 {
14846 /* A padding glyph that doesn't fit on this line.
14847 This means the whole character doesn't fit
14848 on the line. */
14849 row->used[TEXT_AREA] = n_glyphs_before;
14850
14851 /* Fill the rest of the row with continuation
14852 glyphs like in 20.x. */
14853 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
14854 < row->glyphs[1 + TEXT_AREA])
14855 produce_special_glyphs (it, IT_CONTINUATION);
14856
14857 row->continued_p = 1;
14858 it->current_x = x_before;
14859 it->continuation_lines_width += x_before;
14860
14861 /* Restore the height to what it was before the
14862 element not fitting on the line. */
14863 it->max_ascent = ascent;
14864 it->max_descent = descent;
14865 it->max_phys_ascent = phys_ascent;
14866 it->max_phys_descent = phys_descent;
14867 }
14868 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
14869 {
14870 /* A TAB that extends past the right edge of the
14871 window. This produces a single glyph on
14872 window system frames. We leave the glyph in
14873 this row and let it fill the row, but don't
14874 consume the TAB. */
14875 it->continuation_lines_width += it->last_visible_x;
14876 row->ends_in_middle_of_char_p = 1;
14877 row->continued_p = 1;
14878 glyph->pixel_width = it->last_visible_x - x;
14879 it->starts_in_middle_of_char_p = 1;
14880 }
14881 else
14882 {
14883 /* Something other than a TAB that draws past
14884 the right edge of the window. Restore
14885 positions to values before the element. */
14886 row->used[TEXT_AREA] = n_glyphs_before + i;
14887
14888 /* Display continuation glyphs. */
14889 if (!FRAME_WINDOW_P (it->f))
14890 produce_special_glyphs (it, IT_CONTINUATION);
14891 row->continued_p = 1;
14892
14893 it->continuation_lines_width += x;
14894
14895 if (nglyphs > 1 && i > 0)
14896 {
14897 row->ends_in_middle_of_char_p = 1;
14898 it->starts_in_middle_of_char_p = 1;
14899 }
14900
14901 /* Restore the height to what it was before the
14902 element not fitting on the line. */
14903 it->max_ascent = ascent;
14904 it->max_descent = descent;
14905 it->max_phys_ascent = phys_ascent;
14906 it->max_phys_descent = phys_descent;
14907 }
14908
14909 break;
14910 }
14911 else if (new_x > it->first_visible_x)
14912 {
14913 /* Increment number of glyphs actually displayed. */
14914 ++it->hpos;
14915
14916 if (x < it->first_visible_x)
14917 /* Glyph is partially visible, i.e. row starts at
14918 negative X position. */
14919 row->x = x - it->first_visible_x;
14920 }
14921 else
14922 {
14923 /* Glyph is completely off the left margin of the
14924 window. This should not happen because of the
14925 move_it_in_display_line at the start of this
14926 function, unless the text display area of the
14927 window is empty. */
14928 xassert (it->first_visible_x <= it->last_visible_x);
14929 }
14930 }
14931
14932 row->ascent = max (row->ascent, it->max_ascent);
14933 row->height = max (row->height, it->max_ascent + it->max_descent);
14934 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14935 row->phys_height = max (row->phys_height,
14936 it->max_phys_ascent + it->max_phys_descent);
14937
14938 /* End of this display line if row is continued. */
14939 if (row->continued_p || row->ends_at_zv_p)
14940 break;
14941 }
14942
14943 at_end_of_line:
14944 /* Is this a line end? If yes, we're also done, after making
14945 sure that a non-default face is extended up to the right
14946 margin of the window. */
14947 if (ITERATOR_AT_END_OF_LINE_P (it))
14948 {
14949 int used_before = row->used[TEXT_AREA];
14950
14951 row->ends_in_newline_from_string_p = STRINGP (it->object);
14952
14953 #ifdef HAVE_WINDOW_SYSTEM
14954 /* Add a space at the end of the line that is used to
14955 display the cursor there. */
14956 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14957 append_space_for_newline (it, 0);
14958 #endif /* HAVE_WINDOW_SYSTEM */
14959
14960 /* Extend the face to the end of the line. */
14961 extend_face_to_end_of_line (it);
14962
14963 /* Make sure we have the position. */
14964 if (used_before == 0)
14965 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
14966
14967 /* Consume the line end. This skips over invisible lines. */
14968 set_iterator_to_next (it, 1);
14969 it->continuation_lines_width = 0;
14970 break;
14971 }
14972
14973 /* Proceed with next display element. Note that this skips
14974 over lines invisible because of selective display. */
14975 set_iterator_to_next (it, 1);
14976
14977 /* If we truncate lines, we are done when the last displayed
14978 glyphs reach past the right margin of the window. */
14979 if (it->truncate_lines_p
14980 && (FRAME_WINDOW_P (it->f)
14981 ? (it->current_x >= it->last_visible_x)
14982 : (it->current_x > it->last_visible_x)))
14983 {
14984 /* Maybe add truncation glyphs. */
14985 if (!FRAME_WINDOW_P (it->f))
14986 {
14987 int i, n;
14988
14989 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14990 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14991 break;
14992
14993 for (n = row->used[TEXT_AREA]; i < n; ++i)
14994 {
14995 row->used[TEXT_AREA] = i;
14996 produce_special_glyphs (it, IT_TRUNCATION);
14997 }
14998 }
14999 #ifdef HAVE_WINDOW_SYSTEM
15000 else
15001 {
15002 /* Don't truncate if we can overflow newline into fringe. */
15003 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15004 {
15005 if (!get_next_display_element (it))
15006 {
15007 #ifdef HAVE_WINDOW_SYSTEM
15008 it->continuation_lines_width = 0;
15009 row->ends_at_zv_p = 1;
15010 row->exact_window_width_line_p = 1;
15011 break;
15012 #endif /* HAVE_WINDOW_SYSTEM */
15013 }
15014 if (ITERATOR_AT_END_OF_LINE_P (it))
15015 {
15016 row->exact_window_width_line_p = 1;
15017 goto at_end_of_line;
15018 }
15019 }
15020 }
15021 #endif /* HAVE_WINDOW_SYSTEM */
15022
15023 row->truncated_on_right_p = 1;
15024 it->continuation_lines_width = 0;
15025 reseat_at_next_visible_line_start (it, 0);
15026 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
15027 it->hpos = hpos_before;
15028 it->current_x = x_before;
15029 break;
15030 }
15031 }
15032
15033 /* If line is not empty and hscrolled, maybe insert truncation glyphs
15034 at the left window margin. */
15035 if (it->first_visible_x
15036 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
15037 {
15038 if (!FRAME_WINDOW_P (it->f))
15039 insert_left_trunc_glyphs (it);
15040 row->truncated_on_left_p = 1;
15041 }
15042
15043 /* If the start of this line is the overlay arrow-position, then
15044 mark this glyph row as the one containing the overlay arrow.
15045 This is clearly a mess with variable size fonts. It would be
15046 better to let it be displayed like cursors under X. */
15047 if (! overlay_arrow_seen
15048 && (overlay_arrow_string
15049 = overlay_arrow_at_row (it->f, row, &overlay_arrow_bitmap),
15050 !NILP (overlay_arrow_string)))
15051 {
15052 /* Overlay arrow in window redisplay is a fringe bitmap. */
15053 if (!FRAME_WINDOW_P (it->f))
15054 {
15055 struct glyph_row *arrow_row
15056 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
15057 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15058 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15059 struct glyph *p = row->glyphs[TEXT_AREA];
15060 struct glyph *p2, *end;
15061
15062 /* Copy the arrow glyphs. */
15063 while (glyph < arrow_end)
15064 *p++ = *glyph++;
15065
15066 /* Throw away padding glyphs. */
15067 p2 = p;
15068 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15069 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15070 ++p2;
15071 if (p2 > p)
15072 {
15073 while (p2 < end)
15074 *p++ = *p2++;
15075 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
15076 }
15077 }
15078
15079 overlay_arrow_seen = 1;
15080 it->w->overlay_arrow_bitmap = overlay_arrow_bitmap;
15081 row->overlay_arrow_p = 1;
15082 }
15083
15084 /* Compute pixel dimensions of this line. */
15085 compute_line_metrics (it);
15086
15087 /* Remember the position at which this line ends. */
15088 row->end = it->current;
15089
15090 /* Save fringe bitmaps in this row. */
15091 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
15092 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
15093 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
15094 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
15095
15096 it->left_user_fringe_bitmap = 0;
15097 it->left_user_fringe_face_id = 0;
15098 it->right_user_fringe_bitmap = 0;
15099 it->right_user_fringe_face_id = 0;
15100
15101 /* Maybe set the cursor. */
15102 if (it->w->cursor.vpos < 0
15103 && PT >= MATRIX_ROW_START_CHARPOS (row)
15104 && PT <= MATRIX_ROW_END_CHARPOS (row)
15105 && cursor_row_p (it->w, row))
15106 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15107
15108 /* Highlight trailing whitespace. */
15109 if (!NILP (Vshow_trailing_whitespace))
15110 highlight_trailing_whitespace (it->f, it->glyph_row);
15111
15112 /* Prepare for the next line. This line starts horizontally at (X
15113 HPOS) = (0 0). Vertical positions are incremented. As a
15114 convenience for the caller, IT->glyph_row is set to the next
15115 row to be used. */
15116 it->current_x = it->hpos = 0;
15117 it->current_y += row->height;
15118 ++it->vpos;
15119 ++it->glyph_row;
15120 it->start = it->current;
15121 return row->displays_text_p;
15122 }
15123
15124
15125 \f
15126 /***********************************************************************
15127 Menu Bar
15128 ***********************************************************************/
15129
15130 /* Redisplay the menu bar in the frame for window W.
15131
15132 The menu bar of X frames that don't have X toolkit support is
15133 displayed in a special window W->frame->menu_bar_window.
15134
15135 The menu bar of terminal frames is treated specially as far as
15136 glyph matrices are concerned. Menu bar lines are not part of
15137 windows, so the update is done directly on the frame matrix rows
15138 for the menu bar. */
15139
15140 static void
15141 display_menu_bar (w)
15142 struct window *w;
15143 {
15144 struct frame *f = XFRAME (WINDOW_FRAME (w));
15145 struct it it;
15146 Lisp_Object items;
15147 int i;
15148
15149 /* Don't do all this for graphical frames. */
15150 #ifdef HAVE_NTGUI
15151 if (!NILP (Vwindow_system))
15152 return;
15153 #endif
15154 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15155 if (FRAME_X_P (f))
15156 return;
15157 #endif
15158 #ifdef MAC_OS
15159 if (FRAME_MAC_P (f))
15160 return;
15161 #endif
15162
15163 #ifdef USE_X_TOOLKIT
15164 xassert (!FRAME_WINDOW_P (f));
15165 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
15166 it.first_visible_x = 0;
15167 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15168 #else /* not USE_X_TOOLKIT */
15169 if (FRAME_WINDOW_P (f))
15170 {
15171 /* Menu bar lines are displayed in the desired matrix of the
15172 dummy window menu_bar_window. */
15173 struct window *menu_w;
15174 xassert (WINDOWP (f->menu_bar_window));
15175 menu_w = XWINDOW (f->menu_bar_window);
15176 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
15177 MENU_FACE_ID);
15178 it.first_visible_x = 0;
15179 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15180 }
15181 else
15182 {
15183 /* This is a TTY frame, i.e. character hpos/vpos are used as
15184 pixel x/y. */
15185 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
15186 MENU_FACE_ID);
15187 it.first_visible_x = 0;
15188 it.last_visible_x = FRAME_COLS (f);
15189 }
15190 #endif /* not USE_X_TOOLKIT */
15191
15192 if (! mode_line_inverse_video)
15193 /* Force the menu-bar to be displayed in the default face. */
15194 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15195
15196 /* Clear all rows of the menu bar. */
15197 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
15198 {
15199 struct glyph_row *row = it.glyph_row + i;
15200 clear_glyph_row (row);
15201 row->enabled_p = 1;
15202 row->full_width_p = 1;
15203 }
15204
15205 /* Display all items of the menu bar. */
15206 items = FRAME_MENU_BAR_ITEMS (it.f);
15207 for (i = 0; i < XVECTOR (items)->size; i += 4)
15208 {
15209 Lisp_Object string;
15210
15211 /* Stop at nil string. */
15212 string = AREF (items, i + 1);
15213 if (NILP (string))
15214 break;
15215
15216 /* Remember where item was displayed. */
15217 AREF (items, i + 3) = make_number (it.hpos);
15218
15219 /* Display the item, pad with one space. */
15220 if (it.current_x < it.last_visible_x)
15221 display_string (NULL, string, Qnil, 0, 0, &it,
15222 SCHARS (string) + 1, 0, 0, -1);
15223 }
15224
15225 /* Fill out the line with spaces. */
15226 if (it.current_x < it.last_visible_x)
15227 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15228
15229 /* Compute the total height of the lines. */
15230 compute_line_metrics (&it);
15231 }
15232
15233
15234 \f
15235 /***********************************************************************
15236 Mode Line
15237 ***********************************************************************/
15238
15239 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15240 FORCE is non-zero, redisplay mode lines unconditionally.
15241 Otherwise, redisplay only mode lines that are garbaged. Value is
15242 the number of windows whose mode lines were redisplayed. */
15243
15244 static int
15245 redisplay_mode_lines (window, force)
15246 Lisp_Object window;
15247 int force;
15248 {
15249 int nwindows = 0;
15250
15251 while (!NILP (window))
15252 {
15253 struct window *w = XWINDOW (window);
15254
15255 if (WINDOWP (w->hchild))
15256 nwindows += redisplay_mode_lines (w->hchild, force);
15257 else if (WINDOWP (w->vchild))
15258 nwindows += redisplay_mode_lines (w->vchild, force);
15259 else if (force
15260 || FRAME_GARBAGED_P (XFRAME (w->frame))
15261 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15262 {
15263 struct text_pos lpoint;
15264 struct buffer *old = current_buffer;
15265
15266 /* Set the window's buffer for the mode line display. */
15267 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15268 set_buffer_internal_1 (XBUFFER (w->buffer));
15269
15270 /* Point refers normally to the selected window. For any
15271 other window, set up appropriate value. */
15272 if (!EQ (window, selected_window))
15273 {
15274 struct text_pos pt;
15275
15276 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15277 if (CHARPOS (pt) < BEGV)
15278 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15279 else if (CHARPOS (pt) > (ZV - 1))
15280 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15281 else
15282 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15283 }
15284
15285 /* Display mode lines. */
15286 clear_glyph_matrix (w->desired_matrix);
15287 if (display_mode_lines (w))
15288 {
15289 ++nwindows;
15290 w->must_be_updated_p = 1;
15291 }
15292
15293 /* Restore old settings. */
15294 set_buffer_internal_1 (old);
15295 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15296 }
15297
15298 window = w->next;
15299 }
15300
15301 return nwindows;
15302 }
15303
15304
15305 /* Display the mode and/or top line of window W. Value is the number
15306 of mode lines displayed. */
15307
15308 static int
15309 display_mode_lines (w)
15310 struct window *w;
15311 {
15312 Lisp_Object old_selected_window, old_selected_frame;
15313 int n = 0;
15314
15315 old_selected_frame = selected_frame;
15316 selected_frame = w->frame;
15317 old_selected_window = selected_window;
15318 XSETWINDOW (selected_window, w);
15319
15320 /* These will be set while the mode line specs are processed. */
15321 line_number_displayed = 0;
15322 w->column_number_displayed = Qnil;
15323
15324 if (WINDOW_WANTS_MODELINE_P (w))
15325 {
15326 struct window *sel_w = XWINDOW (old_selected_window);
15327
15328 /* Select mode line face based on the real selected window. */
15329 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15330 current_buffer->mode_line_format);
15331 ++n;
15332 }
15333
15334 if (WINDOW_WANTS_HEADER_LINE_P (w))
15335 {
15336 display_mode_line (w, HEADER_LINE_FACE_ID,
15337 current_buffer->header_line_format);
15338 ++n;
15339 }
15340
15341 selected_frame = old_selected_frame;
15342 selected_window = old_selected_window;
15343 return n;
15344 }
15345
15346
15347 /* Display mode or top line of window W. FACE_ID specifies which line
15348 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15349 FORMAT is the mode line format to display. Value is the pixel
15350 height of the mode line displayed. */
15351
15352 static int
15353 display_mode_line (w, face_id, format)
15354 struct window *w;
15355 enum face_id face_id;
15356 Lisp_Object format;
15357 {
15358 struct it it;
15359 struct face *face;
15360
15361 init_iterator (&it, w, -1, -1, NULL, face_id);
15362 prepare_desired_row (it.glyph_row);
15363
15364 it.glyph_row->mode_line_p = 1;
15365
15366 if (! mode_line_inverse_video)
15367 /* Force the mode-line to be displayed in the default face. */
15368 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15369
15370 /* Temporarily make frame's keyboard the current kboard so that
15371 kboard-local variables in the mode_line_format will get the right
15372 values. */
15373 push_frame_kboard (it.f);
15374 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15375 pop_frame_kboard ();
15376
15377 /* Fill up with spaces. */
15378 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15379
15380 compute_line_metrics (&it);
15381 it.glyph_row->full_width_p = 1;
15382 it.glyph_row->continued_p = 0;
15383 it.glyph_row->truncated_on_left_p = 0;
15384 it.glyph_row->truncated_on_right_p = 0;
15385
15386 /* Make a 3D mode-line have a shadow at its right end. */
15387 face = FACE_FROM_ID (it.f, face_id);
15388 extend_face_to_end_of_line (&it);
15389 if (face->box != FACE_NO_BOX)
15390 {
15391 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15392 + it.glyph_row->used[TEXT_AREA] - 1);
15393 last->right_box_line_p = 1;
15394 }
15395
15396 return it.glyph_row->height;
15397 }
15398
15399 /* Alist that caches the results of :propertize.
15400 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
15401 Lisp_Object mode_line_proptrans_alist;
15402
15403 /* List of strings making up the mode-line. */
15404 Lisp_Object mode_line_string_list;
15405
15406 /* Base face property when building propertized mode line string. */
15407 static Lisp_Object mode_line_string_face;
15408 static Lisp_Object mode_line_string_face_prop;
15409
15410
15411 /* Contribute ELT to the mode line for window IT->w. How it
15412 translates into text depends on its data type.
15413
15414 IT describes the display environment in which we display, as usual.
15415
15416 DEPTH is the depth in recursion. It is used to prevent
15417 infinite recursion here.
15418
15419 FIELD_WIDTH is the number of characters the display of ELT should
15420 occupy in the mode line, and PRECISION is the maximum number of
15421 characters to display from ELT's representation. See
15422 display_string for details.
15423
15424 Returns the hpos of the end of the text generated by ELT.
15425
15426 PROPS is a property list to add to any string we encounter.
15427
15428 If RISKY is nonzero, remove (disregard) any properties in any string
15429 we encounter, and ignore :eval and :propertize.
15430
15431 If the global variable `frame_title_ptr' is non-NULL, then the output
15432 is passed to `store_frame_title' instead of `display_string'. */
15433
15434 static int
15435 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15436 struct it *it;
15437 int depth;
15438 int field_width, precision;
15439 Lisp_Object elt, props;
15440 int risky;
15441 {
15442 int n = 0, field, prec;
15443 int literal = 0;
15444
15445 tail_recurse:
15446 if (depth > 100)
15447 elt = build_string ("*too-deep*");
15448
15449 depth++;
15450
15451 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15452 {
15453 case Lisp_String:
15454 {
15455 /* A string: output it and check for %-constructs within it. */
15456 unsigned char c;
15457 const unsigned char *this, *lisp_string;
15458
15459 if (!NILP (props) || risky)
15460 {
15461 Lisp_Object oprops, aelt;
15462 oprops = Ftext_properties_at (make_number (0), elt);
15463
15464 /* If the starting string's properties are not what
15465 we want, translate the string. Also, if the string
15466 is risky, do that anyway. */
15467
15468 if (NILP (Fequal (props, oprops)) || risky)
15469 {
15470 /* If the starting string has properties,
15471 merge the specified ones onto the existing ones. */
15472 if (! NILP (oprops) && !risky)
15473 {
15474 Lisp_Object tem;
15475
15476 oprops = Fcopy_sequence (oprops);
15477 tem = props;
15478 while (CONSP (tem))
15479 {
15480 oprops = Fplist_put (oprops, XCAR (tem),
15481 XCAR (XCDR (tem)));
15482 tem = XCDR (XCDR (tem));
15483 }
15484 props = oprops;
15485 }
15486
15487 aelt = Fassoc (elt, mode_line_proptrans_alist);
15488 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15489 {
15490 mode_line_proptrans_alist
15491 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15492 elt = XCAR (aelt);
15493 }
15494 else
15495 {
15496 Lisp_Object tem;
15497
15498 elt = Fcopy_sequence (elt);
15499 Fset_text_properties (make_number (0), Flength (elt),
15500 props, elt);
15501 /* Add this item to mode_line_proptrans_alist. */
15502 mode_line_proptrans_alist
15503 = Fcons (Fcons (elt, props),
15504 mode_line_proptrans_alist);
15505 /* Truncate mode_line_proptrans_alist
15506 to at most 50 elements. */
15507 tem = Fnthcdr (make_number (50),
15508 mode_line_proptrans_alist);
15509 if (! NILP (tem))
15510 XSETCDR (tem, Qnil);
15511 }
15512 }
15513 }
15514
15515 this = SDATA (elt);
15516 lisp_string = this;
15517
15518 if (literal)
15519 {
15520 prec = precision - n;
15521 if (frame_title_ptr)
15522 n += store_frame_title (SDATA (elt), -1, prec);
15523 else if (!NILP (mode_line_string_list))
15524 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15525 else
15526 n += display_string (NULL, elt, Qnil, 0, 0, it,
15527 0, prec, 0, STRING_MULTIBYTE (elt));
15528
15529 break;
15530 }
15531
15532 while ((precision <= 0 || n < precision)
15533 && *this
15534 && (frame_title_ptr
15535 || !NILP (mode_line_string_list)
15536 || it->current_x < it->last_visible_x))
15537 {
15538 const unsigned char *last = this;
15539
15540 /* Advance to end of string or next format specifier. */
15541 while ((c = *this++) != '\0' && c != '%')
15542 ;
15543
15544 if (this - 1 != last)
15545 {
15546 /* Output to end of string or up to '%'. Field width
15547 is length of string. Don't output more than
15548 PRECISION allows us. */
15549 --this;
15550
15551 prec = chars_in_text (last, this - last);
15552 if (precision > 0 && prec > precision - n)
15553 prec = precision - n;
15554
15555 if (frame_title_ptr)
15556 n += store_frame_title (last, 0, prec);
15557 else if (!NILP (mode_line_string_list))
15558 {
15559 int bytepos = last - lisp_string;
15560 int charpos = string_byte_to_char (elt, bytepos);
15561 n += store_mode_line_string (NULL,
15562 Fsubstring (elt, make_number (charpos),
15563 make_number (charpos + prec)),
15564 0, 0, 0, Qnil);
15565 }
15566 else
15567 {
15568 int bytepos = last - lisp_string;
15569 int charpos = string_byte_to_char (elt, bytepos);
15570 n += display_string (NULL, elt, Qnil, 0, charpos,
15571 it, 0, prec, 0,
15572 STRING_MULTIBYTE (elt));
15573 }
15574 }
15575 else /* c == '%' */
15576 {
15577 const unsigned char *percent_position = this;
15578
15579 /* Get the specified minimum width. Zero means
15580 don't pad. */
15581 field = 0;
15582 while ((c = *this++) >= '0' && c <= '9')
15583 field = field * 10 + c - '0';
15584
15585 /* Don't pad beyond the total padding allowed. */
15586 if (field_width - n > 0 && field > field_width - n)
15587 field = field_width - n;
15588
15589 /* Note that either PRECISION <= 0 or N < PRECISION. */
15590 prec = precision - n;
15591
15592 if (c == 'M')
15593 n += display_mode_element (it, depth, field, prec,
15594 Vglobal_mode_string, props,
15595 risky);
15596 else if (c != 0)
15597 {
15598 int multibyte;
15599 int bytepos, charpos;
15600 unsigned char *spec;
15601
15602 bytepos = percent_position - lisp_string;
15603 charpos = (STRING_MULTIBYTE (elt)
15604 ? string_byte_to_char (elt, bytepos)
15605 : bytepos);
15606
15607 spec
15608 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15609
15610 if (frame_title_ptr)
15611 n += store_frame_title (spec, field, prec);
15612 else if (!NILP (mode_line_string_list))
15613 {
15614 int len = strlen (spec);
15615 Lisp_Object tem = make_string (spec, len);
15616 props = Ftext_properties_at (make_number (charpos), elt);
15617 /* Should only keep face property in props */
15618 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15619 }
15620 else
15621 {
15622 int nglyphs_before, nwritten;
15623
15624 nglyphs_before = it->glyph_row->used[TEXT_AREA];
15625 nwritten = display_string (spec, Qnil, elt,
15626 charpos, 0, it,
15627 field, prec, 0,
15628 multibyte);
15629
15630 /* Assign to the glyphs written above the
15631 string where the `%x' came from, position
15632 of the `%'. */
15633 if (nwritten > 0)
15634 {
15635 struct glyph *glyph
15636 = (it->glyph_row->glyphs[TEXT_AREA]
15637 + nglyphs_before);
15638 int i;
15639
15640 for (i = 0; i < nwritten; ++i)
15641 {
15642 glyph[i].object = elt;
15643 glyph[i].charpos = charpos;
15644 }
15645
15646 n += nwritten;
15647 }
15648 }
15649 }
15650 else /* c == 0 */
15651 break;
15652 }
15653 }
15654 }
15655 break;
15656
15657 case Lisp_Symbol:
15658 /* A symbol: process the value of the symbol recursively
15659 as if it appeared here directly. Avoid error if symbol void.
15660 Special case: if value of symbol is a string, output the string
15661 literally. */
15662 {
15663 register Lisp_Object tem;
15664
15665 /* If the variable is not marked as risky to set
15666 then its contents are risky to use. */
15667 if (NILP (Fget (elt, Qrisky_local_variable)))
15668 risky = 1;
15669
15670 tem = Fboundp (elt);
15671 if (!NILP (tem))
15672 {
15673 tem = Fsymbol_value (elt);
15674 /* If value is a string, output that string literally:
15675 don't check for % within it. */
15676 if (STRINGP (tem))
15677 literal = 1;
15678
15679 if (!EQ (tem, elt))
15680 {
15681 /* Give up right away for nil or t. */
15682 elt = tem;
15683 goto tail_recurse;
15684 }
15685 }
15686 }
15687 break;
15688
15689 case Lisp_Cons:
15690 {
15691 register Lisp_Object car, tem;
15692
15693 /* A cons cell: five distinct cases.
15694 If first element is :eval or :propertize, do something special.
15695 If first element is a string or a cons, process all the elements
15696 and effectively concatenate them.
15697 If first element is a negative number, truncate displaying cdr to
15698 at most that many characters. If positive, pad (with spaces)
15699 to at least that many characters.
15700 If first element is a symbol, process the cadr or caddr recursively
15701 according to whether the symbol's value is non-nil or nil. */
15702 car = XCAR (elt);
15703 if (EQ (car, QCeval))
15704 {
15705 /* An element of the form (:eval FORM) means evaluate FORM
15706 and use the result as mode line elements. */
15707
15708 if (risky)
15709 break;
15710
15711 if (CONSP (XCDR (elt)))
15712 {
15713 Lisp_Object spec;
15714 spec = safe_eval (XCAR (XCDR (elt)));
15715 n += display_mode_element (it, depth, field_width - n,
15716 precision - n, spec, props,
15717 risky);
15718 }
15719 }
15720 else if (EQ (car, QCpropertize))
15721 {
15722 /* An element of the form (:propertize ELT PROPS...)
15723 means display ELT but applying properties PROPS. */
15724
15725 if (risky)
15726 break;
15727
15728 if (CONSP (XCDR (elt)))
15729 n += display_mode_element (it, depth, field_width - n,
15730 precision - n, XCAR (XCDR (elt)),
15731 XCDR (XCDR (elt)), risky);
15732 }
15733 else if (SYMBOLP (car))
15734 {
15735 tem = Fboundp (car);
15736 elt = XCDR (elt);
15737 if (!CONSP (elt))
15738 goto invalid;
15739 /* elt is now the cdr, and we know it is a cons cell.
15740 Use its car if CAR has a non-nil value. */
15741 if (!NILP (tem))
15742 {
15743 tem = Fsymbol_value (car);
15744 if (!NILP (tem))
15745 {
15746 elt = XCAR (elt);
15747 goto tail_recurse;
15748 }
15749 }
15750 /* Symbol's value is nil (or symbol is unbound)
15751 Get the cddr of the original list
15752 and if possible find the caddr and use that. */
15753 elt = XCDR (elt);
15754 if (NILP (elt))
15755 break;
15756 else if (!CONSP (elt))
15757 goto invalid;
15758 elt = XCAR (elt);
15759 goto tail_recurse;
15760 }
15761 else if (INTEGERP (car))
15762 {
15763 register int lim = XINT (car);
15764 elt = XCDR (elt);
15765 if (lim < 0)
15766 {
15767 /* Negative int means reduce maximum width. */
15768 if (precision <= 0)
15769 precision = -lim;
15770 else
15771 precision = min (precision, -lim);
15772 }
15773 else if (lim > 0)
15774 {
15775 /* Padding specified. Don't let it be more than
15776 current maximum. */
15777 if (precision > 0)
15778 lim = min (precision, lim);
15779
15780 /* If that's more padding than already wanted, queue it.
15781 But don't reduce padding already specified even if
15782 that is beyond the current truncation point. */
15783 field_width = max (lim, field_width);
15784 }
15785 goto tail_recurse;
15786 }
15787 else if (STRINGP (car) || CONSP (car))
15788 {
15789 register int limit = 50;
15790 /* Limit is to protect against circular lists. */
15791 while (CONSP (elt)
15792 && --limit > 0
15793 && (precision <= 0 || n < precision))
15794 {
15795 n += display_mode_element (it, depth, field_width - n,
15796 precision - n, XCAR (elt),
15797 props, risky);
15798 elt = XCDR (elt);
15799 }
15800 }
15801 }
15802 break;
15803
15804 default:
15805 invalid:
15806 elt = build_string ("*invalid*");
15807 goto tail_recurse;
15808 }
15809
15810 /* Pad to FIELD_WIDTH. */
15811 if (field_width > 0 && n < field_width)
15812 {
15813 if (frame_title_ptr)
15814 n += store_frame_title ("", field_width - n, 0);
15815 else if (!NILP (mode_line_string_list))
15816 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
15817 else
15818 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
15819 0, 0, 0);
15820 }
15821
15822 return n;
15823 }
15824
15825 /* Store a mode-line string element in mode_line_string_list.
15826
15827 If STRING is non-null, display that C string. Otherwise, the Lisp
15828 string LISP_STRING is displayed.
15829
15830 FIELD_WIDTH is the minimum number of output glyphs to produce.
15831 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15832 with spaces. FIELD_WIDTH <= 0 means don't pad.
15833
15834 PRECISION is the maximum number of characters to output from
15835 STRING. PRECISION <= 0 means don't truncate the string.
15836
15837 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
15838 properties to the string.
15839
15840 PROPS are the properties to add to the string.
15841 The mode_line_string_face face property is always added to the string.
15842 */
15843
15844 static int
15845 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
15846 char *string;
15847 Lisp_Object lisp_string;
15848 int copy_string;
15849 int field_width;
15850 int precision;
15851 Lisp_Object props;
15852 {
15853 int len;
15854 int n = 0;
15855
15856 if (string != NULL)
15857 {
15858 len = strlen (string);
15859 if (precision > 0 && len > precision)
15860 len = precision;
15861 lisp_string = make_string (string, len);
15862 if (NILP (props))
15863 props = mode_line_string_face_prop;
15864 else if (!NILP (mode_line_string_face))
15865 {
15866 Lisp_Object face = Fplist_get (props, Qface);
15867 props = Fcopy_sequence (props);
15868 if (NILP (face))
15869 face = mode_line_string_face;
15870 else
15871 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15872 props = Fplist_put (props, Qface, face);
15873 }
15874 Fadd_text_properties (make_number (0), make_number (len),
15875 props, lisp_string);
15876 }
15877 else
15878 {
15879 len = XFASTINT (Flength (lisp_string));
15880 if (precision > 0 && len > precision)
15881 {
15882 len = precision;
15883 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
15884 precision = -1;
15885 }
15886 if (!NILP (mode_line_string_face))
15887 {
15888 Lisp_Object face;
15889 if (NILP (props))
15890 props = Ftext_properties_at (make_number (0), lisp_string);
15891 face = Fplist_get (props, Qface);
15892 if (NILP (face))
15893 face = mode_line_string_face;
15894 else
15895 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15896 props = Fcons (Qface, Fcons (face, Qnil));
15897 if (copy_string)
15898 lisp_string = Fcopy_sequence (lisp_string);
15899 }
15900 if (!NILP (props))
15901 Fadd_text_properties (make_number (0), make_number (len),
15902 props, lisp_string);
15903 }
15904
15905 if (len > 0)
15906 {
15907 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15908 n += len;
15909 }
15910
15911 if (field_width > len)
15912 {
15913 field_width -= len;
15914 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
15915 if (!NILP (props))
15916 Fadd_text_properties (make_number (0), make_number (field_width),
15917 props, lisp_string);
15918 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15919 n += field_width;
15920 }
15921
15922 return n;
15923 }
15924
15925
15926 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
15927 0, 4, 0,
15928 doc: /* Return the mode-line of selected window as a string.
15929 First optional arg FORMAT specifies a different format string (see
15930 `mode-line-format' for details) to use. If FORMAT is t, return
15931 the buffer's header-line. Second optional arg WINDOW specifies a
15932 different window to use as the context for the formatting.
15933 If third optional arg NO-PROPS is non-nil, string is not propertized.
15934 Fourth optional arg BUFFER specifies which buffer to use. */)
15935 (format, window, no_props, buffer)
15936 Lisp_Object format, window, no_props, buffer;
15937 {
15938 struct it it;
15939 int len;
15940 struct window *w;
15941 struct buffer *old_buffer = NULL;
15942 enum face_id face_id = DEFAULT_FACE_ID;
15943
15944 if (NILP (window))
15945 window = selected_window;
15946 CHECK_WINDOW (window);
15947 w = XWINDOW (window);
15948
15949 if (NILP (buffer))
15950 buffer = w->buffer;
15951
15952 CHECK_BUFFER (buffer);
15953
15954 if (XBUFFER (buffer) != current_buffer)
15955 {
15956 old_buffer = current_buffer;
15957 set_buffer_internal_1 (XBUFFER (buffer));
15958 }
15959
15960 if (NILP (format) || EQ (format, Qt))
15961 {
15962 face_id = (NILP (format)
15963 ? CURRENT_MODE_LINE_FACE_ID (w)
15964 : HEADER_LINE_FACE_ID);
15965 format = (NILP (format)
15966 ? current_buffer->mode_line_format
15967 : current_buffer->header_line_format);
15968 }
15969
15970 init_iterator (&it, w, -1, -1, NULL, face_id);
15971
15972 if (NILP (no_props))
15973 {
15974 mode_line_string_face
15975 = (face_id == MODE_LINE_FACE_ID ? Qmode_line
15976 : face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive
15977 : face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
15978
15979 mode_line_string_face_prop
15980 = (NILP (mode_line_string_face) ? Qnil
15981 : Fcons (Qface, Fcons (mode_line_string_face, Qnil)));
15982
15983 /* We need a dummy last element in mode_line_string_list to
15984 indicate we are building the propertized mode-line string.
15985 Using mode_line_string_face_prop here GC protects it. */
15986 mode_line_string_list
15987 = Fcons (mode_line_string_face_prop, Qnil);
15988 frame_title_ptr = NULL;
15989 }
15990 else
15991 {
15992 mode_line_string_face_prop = Qnil;
15993 mode_line_string_list = Qnil;
15994 frame_title_ptr = frame_title_buf;
15995 }
15996
15997 push_frame_kboard (it.f);
15998 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15999 pop_frame_kboard ();
16000
16001 if (old_buffer)
16002 set_buffer_internal_1 (old_buffer);
16003
16004 if (NILP (no_props))
16005 {
16006 Lisp_Object str;
16007 mode_line_string_list = Fnreverse (mode_line_string_list);
16008 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
16009 make_string ("", 0));
16010 mode_line_string_face_prop = Qnil;
16011 mode_line_string_list = Qnil;
16012 return str;
16013 }
16014
16015 len = frame_title_ptr - frame_title_buf;
16016 if (len > 0 && frame_title_ptr[-1] == '-')
16017 {
16018 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
16019 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
16020 ;
16021 frame_title_ptr += 3; /* restore last non-dash + two dashes */
16022 if (len > frame_title_ptr - frame_title_buf)
16023 len = frame_title_ptr - frame_title_buf;
16024 }
16025
16026 frame_title_ptr = NULL;
16027 return make_string (frame_title_buf, len);
16028 }
16029
16030 /* Write a null-terminated, right justified decimal representation of
16031 the positive integer D to BUF using a minimal field width WIDTH. */
16032
16033 static void
16034 pint2str (buf, width, d)
16035 register char *buf;
16036 register int width;
16037 register int d;
16038 {
16039 register char *p = buf;
16040
16041 if (d <= 0)
16042 *p++ = '0';
16043 else
16044 {
16045 while (d > 0)
16046 {
16047 *p++ = d % 10 + '0';
16048 d /= 10;
16049 }
16050 }
16051
16052 for (width -= (int) (p - buf); width > 0; --width)
16053 *p++ = ' ';
16054 *p-- = '\0';
16055 while (p > buf)
16056 {
16057 d = *buf;
16058 *buf++ = *p;
16059 *p-- = d;
16060 }
16061 }
16062
16063 /* Write a null-terminated, right justified decimal and "human
16064 readable" representation of the nonnegative integer D to BUF using
16065 a minimal field width WIDTH. D should be smaller than 999.5e24. */
16066
16067 static const char power_letter[] =
16068 {
16069 0, /* not used */
16070 'k', /* kilo */
16071 'M', /* mega */
16072 'G', /* giga */
16073 'T', /* tera */
16074 'P', /* peta */
16075 'E', /* exa */
16076 'Z', /* zetta */
16077 'Y' /* yotta */
16078 };
16079
16080 static void
16081 pint2hrstr (buf, width, d)
16082 char *buf;
16083 int width;
16084 int d;
16085 {
16086 /* We aim to represent the nonnegative integer D as
16087 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
16088 int quotient = d;
16089 int remainder = 0;
16090 /* -1 means: do not use TENTHS. */
16091 int tenths = -1;
16092 int exponent = 0;
16093
16094 /* Length of QUOTIENT.TENTHS as a string. */
16095 int length;
16096
16097 char * psuffix;
16098 char * p;
16099
16100 if (1000 <= quotient)
16101 {
16102 /* Scale to the appropriate EXPONENT. */
16103 do
16104 {
16105 remainder = quotient % 1000;
16106 quotient /= 1000;
16107 exponent++;
16108 }
16109 while (1000 <= quotient);
16110
16111 /* Round to nearest and decide whether to use TENTHS or not. */
16112 if (quotient <= 9)
16113 {
16114 tenths = remainder / 100;
16115 if (50 <= remainder % 100)
16116 if (tenths < 9)
16117 tenths++;
16118 else
16119 {
16120 quotient++;
16121 if (quotient == 10)
16122 tenths = -1;
16123 else
16124 tenths = 0;
16125 }
16126 }
16127 else
16128 if (500 <= remainder)
16129 if (quotient < 999)
16130 quotient++;
16131 else
16132 {
16133 quotient = 1;
16134 exponent++;
16135 tenths = 0;
16136 }
16137 }
16138
16139 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
16140 if (tenths == -1 && quotient <= 99)
16141 if (quotient <= 9)
16142 length = 1;
16143 else
16144 length = 2;
16145 else
16146 length = 3;
16147 p = psuffix = buf + max (width, length);
16148
16149 /* Print EXPONENT. */
16150 if (exponent)
16151 *psuffix++ = power_letter[exponent];
16152 *psuffix = '\0';
16153
16154 /* Print TENTHS. */
16155 if (tenths >= 0)
16156 {
16157 *--p = '0' + tenths;
16158 *--p = '.';
16159 }
16160
16161 /* Print QUOTIENT. */
16162 do
16163 {
16164 int digit = quotient % 10;
16165 *--p = '0' + digit;
16166 }
16167 while ((quotient /= 10) != 0);
16168
16169 /* Print leading spaces. */
16170 while (buf < p)
16171 *--p = ' ';
16172 }
16173
16174 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
16175 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
16176 type of CODING_SYSTEM. Return updated pointer into BUF. */
16177
16178 static unsigned char invalid_eol_type[] = "(*invalid*)";
16179
16180 static char *
16181 decode_mode_spec_coding (coding_system, buf, eol_flag)
16182 Lisp_Object coding_system;
16183 register char *buf;
16184 int eol_flag;
16185 {
16186 Lisp_Object val;
16187 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
16188 const unsigned char *eol_str;
16189 int eol_str_len;
16190 /* The EOL conversion we are using. */
16191 Lisp_Object eoltype;
16192
16193 val = CODING_SYSTEM_SPEC (coding_system);
16194 eoltype = Qnil;
16195
16196 if (!VECTORP (val)) /* Not yet decided. */
16197 {
16198 if (multibyte)
16199 *buf++ = '-';
16200 if (eol_flag)
16201 eoltype = eol_mnemonic_undecided;
16202 /* Don't mention EOL conversion if it isn't decided. */
16203 }
16204 else
16205 {
16206 Lisp_Object attrs;
16207 Lisp_Object eolvalue;
16208
16209 attrs = AREF (val, 0);
16210 eolvalue = AREF (val, 2);
16211
16212 if (multibyte)
16213 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
16214
16215 if (eol_flag)
16216 {
16217 /* The EOL conversion that is normal on this system. */
16218
16219 if (NILP (eolvalue)) /* Not yet decided. */
16220 eoltype = eol_mnemonic_undecided;
16221 else if (VECTORP (eolvalue)) /* Not yet decided. */
16222 eoltype = eol_mnemonic_undecided;
16223 else /* eolvalue is Qunix, Qdos, or Qmac. */
16224 eoltype = (EQ (eolvalue, Qunix)
16225 ? eol_mnemonic_unix
16226 : (EQ (eolvalue, Qdos) == 1
16227 ? eol_mnemonic_dos : eol_mnemonic_mac));
16228 }
16229 }
16230
16231 if (eol_flag)
16232 {
16233 /* Mention the EOL conversion if it is not the usual one. */
16234 if (STRINGP (eoltype))
16235 {
16236 eol_str = SDATA (eoltype);
16237 eol_str_len = SBYTES (eoltype);
16238 }
16239 else if (CHARACTERP (eoltype))
16240 {
16241 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16242 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16243 eol_str = tmp;
16244 }
16245 else
16246 {
16247 eol_str = invalid_eol_type;
16248 eol_str_len = sizeof (invalid_eol_type) - 1;
16249 }
16250 bcopy (eol_str, buf, eol_str_len);
16251 buf += eol_str_len;
16252 }
16253
16254 return buf;
16255 }
16256
16257 /* Return a string for the output of a mode line %-spec for window W,
16258 generated by character C. PRECISION >= 0 means don't return a
16259 string longer than that value. FIELD_WIDTH > 0 means pad the
16260 string returned with spaces to that value. Return 1 in *MULTIBYTE
16261 if the result is multibyte text.
16262
16263 Note we operate on the current buffer for most purposes,
16264 the exception being w->base_line_pos. */
16265
16266 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16267
16268 static char *
16269 decode_mode_spec (w, c, field_width, precision, multibyte)
16270 struct window *w;
16271 register int c;
16272 int field_width, precision;
16273 int *multibyte;
16274 {
16275 Lisp_Object obj;
16276 struct frame *f = XFRAME (WINDOW_FRAME (w));
16277 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16278 struct buffer *b = current_buffer;
16279
16280 obj = Qnil;
16281 *multibyte = 0;
16282
16283 switch (c)
16284 {
16285 case '*':
16286 if (!NILP (b->read_only))
16287 return "%";
16288 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16289 return "*";
16290 return "-";
16291
16292 case '+':
16293 /* This differs from %* only for a modified read-only buffer. */
16294 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16295 return "*";
16296 if (!NILP (b->read_only))
16297 return "%";
16298 return "-";
16299
16300 case '&':
16301 /* This differs from %* in ignoring read-only-ness. */
16302 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16303 return "*";
16304 return "-";
16305
16306 case '%':
16307 return "%";
16308
16309 case '[':
16310 {
16311 int i;
16312 char *p;
16313
16314 if (command_loop_level > 5)
16315 return "[[[... ";
16316 p = decode_mode_spec_buf;
16317 for (i = 0; i < command_loop_level; i++)
16318 *p++ = '[';
16319 *p = 0;
16320 return decode_mode_spec_buf;
16321 }
16322
16323 case ']':
16324 {
16325 int i;
16326 char *p;
16327
16328 if (command_loop_level > 5)
16329 return " ...]]]";
16330 p = decode_mode_spec_buf;
16331 for (i = 0; i < command_loop_level; i++)
16332 *p++ = ']';
16333 *p = 0;
16334 return decode_mode_spec_buf;
16335 }
16336
16337 case '-':
16338 {
16339 register int i;
16340
16341 /* Let lots_of_dashes be a string of infinite length. */
16342 if (!NILP (mode_line_string_list))
16343 return "--";
16344 if (field_width <= 0
16345 || field_width > sizeof (lots_of_dashes))
16346 {
16347 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16348 decode_mode_spec_buf[i] = '-';
16349 decode_mode_spec_buf[i] = '\0';
16350 return decode_mode_spec_buf;
16351 }
16352 else
16353 return lots_of_dashes;
16354 }
16355
16356 case 'b':
16357 obj = b->name;
16358 break;
16359
16360 case 'c':
16361 {
16362 int col = (int) current_column (); /* iftc */
16363 w->column_number_displayed = make_number (col);
16364 pint2str (decode_mode_spec_buf, field_width, col);
16365 return decode_mode_spec_buf;
16366 }
16367
16368 case 'F':
16369 /* %F displays the frame name. */
16370 if (!NILP (f->title))
16371 return (char *) SDATA (f->title);
16372 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16373 return (char *) SDATA (f->name);
16374 return "Emacs";
16375
16376 case 'f':
16377 obj = b->filename;
16378 break;
16379
16380 case 'i':
16381 {
16382 int size = ZV - BEGV;
16383 pint2str (decode_mode_spec_buf, field_width, size);
16384 return decode_mode_spec_buf;
16385 }
16386
16387 case 'I':
16388 {
16389 int size = ZV - BEGV;
16390 pint2hrstr (decode_mode_spec_buf, field_width, size);
16391 return decode_mode_spec_buf;
16392 }
16393
16394 case 'l':
16395 {
16396 int startpos = XMARKER (w->start)->charpos;
16397 int startpos_byte = marker_byte_position (w->start);
16398 int line, linepos, linepos_byte, topline;
16399 int nlines, junk;
16400 int height = WINDOW_TOTAL_LINES (w);
16401
16402 /* If we decided that this buffer isn't suitable for line numbers,
16403 don't forget that too fast. */
16404 if (EQ (w->base_line_pos, w->buffer))
16405 goto no_value;
16406 /* But do forget it, if the window shows a different buffer now. */
16407 else if (BUFFERP (w->base_line_pos))
16408 w->base_line_pos = Qnil;
16409
16410 /* If the buffer is very big, don't waste time. */
16411 if (INTEGERP (Vline_number_display_limit)
16412 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16413 {
16414 w->base_line_pos = Qnil;
16415 w->base_line_number = Qnil;
16416 goto no_value;
16417 }
16418
16419 if (!NILP (w->base_line_number)
16420 && !NILP (w->base_line_pos)
16421 && XFASTINT (w->base_line_pos) <= startpos)
16422 {
16423 line = XFASTINT (w->base_line_number);
16424 linepos = XFASTINT (w->base_line_pos);
16425 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16426 }
16427 else
16428 {
16429 line = 1;
16430 linepos = BUF_BEGV (b);
16431 linepos_byte = BUF_BEGV_BYTE (b);
16432 }
16433
16434 /* Count lines from base line to window start position. */
16435 nlines = display_count_lines (linepos, linepos_byte,
16436 startpos_byte,
16437 startpos, &junk);
16438
16439 topline = nlines + line;
16440
16441 /* Determine a new base line, if the old one is too close
16442 or too far away, or if we did not have one.
16443 "Too close" means it's plausible a scroll-down would
16444 go back past it. */
16445 if (startpos == BUF_BEGV (b))
16446 {
16447 w->base_line_number = make_number (topline);
16448 w->base_line_pos = make_number (BUF_BEGV (b));
16449 }
16450 else if (nlines < height + 25 || nlines > height * 3 + 50
16451 || linepos == BUF_BEGV (b))
16452 {
16453 int limit = BUF_BEGV (b);
16454 int limit_byte = BUF_BEGV_BYTE (b);
16455 int position;
16456 int distance = (height * 2 + 30) * line_number_display_limit_width;
16457
16458 if (startpos - distance > limit)
16459 {
16460 limit = startpos - distance;
16461 limit_byte = CHAR_TO_BYTE (limit);
16462 }
16463
16464 nlines = display_count_lines (startpos, startpos_byte,
16465 limit_byte,
16466 - (height * 2 + 30),
16467 &position);
16468 /* If we couldn't find the lines we wanted within
16469 line_number_display_limit_width chars per line,
16470 give up on line numbers for this window. */
16471 if (position == limit_byte && limit == startpos - distance)
16472 {
16473 w->base_line_pos = w->buffer;
16474 w->base_line_number = Qnil;
16475 goto no_value;
16476 }
16477
16478 w->base_line_number = make_number (topline - nlines);
16479 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
16480 }
16481
16482 /* Now count lines from the start pos to point. */
16483 nlines = display_count_lines (startpos, startpos_byte,
16484 PT_BYTE, PT, &junk);
16485
16486 /* Record that we did display the line number. */
16487 line_number_displayed = 1;
16488
16489 /* Make the string to show. */
16490 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
16491 return decode_mode_spec_buf;
16492 no_value:
16493 {
16494 char* p = decode_mode_spec_buf;
16495 int pad = field_width - 2;
16496 while (pad-- > 0)
16497 *p++ = ' ';
16498 *p++ = '?';
16499 *p++ = '?';
16500 *p = '\0';
16501 return decode_mode_spec_buf;
16502 }
16503 }
16504 break;
16505
16506 case 'm':
16507 obj = b->mode_name;
16508 break;
16509
16510 case 'n':
16511 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
16512 return " Narrow";
16513 break;
16514
16515 case 'p':
16516 {
16517 int pos = marker_position (w->start);
16518 int total = BUF_ZV (b) - BUF_BEGV (b);
16519
16520 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
16521 {
16522 if (pos <= BUF_BEGV (b))
16523 return "All";
16524 else
16525 return "Bottom";
16526 }
16527 else if (pos <= BUF_BEGV (b))
16528 return "Top";
16529 else
16530 {
16531 if (total > 1000000)
16532 /* Do it differently for a large value, to avoid overflow. */
16533 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16534 else
16535 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
16536 /* We can't normally display a 3-digit number,
16537 so get us a 2-digit number that is close. */
16538 if (total == 100)
16539 total = 99;
16540 sprintf (decode_mode_spec_buf, "%2d%%", total);
16541 return decode_mode_spec_buf;
16542 }
16543 }
16544
16545 /* Display percentage of size above the bottom of the screen. */
16546 case 'P':
16547 {
16548 int toppos = marker_position (w->start);
16549 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16550 int total = BUF_ZV (b) - BUF_BEGV (b);
16551
16552 if (botpos >= BUF_ZV (b))
16553 {
16554 if (toppos <= BUF_BEGV (b))
16555 return "All";
16556 else
16557 return "Bottom";
16558 }
16559 else
16560 {
16561 if (total > 1000000)
16562 /* Do it differently for a large value, to avoid overflow. */
16563 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16564 else
16565 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
16566 /* We can't normally display a 3-digit number,
16567 so get us a 2-digit number that is close. */
16568 if (total == 100)
16569 total = 99;
16570 if (toppos <= BUF_BEGV (b))
16571 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
16572 else
16573 sprintf (decode_mode_spec_buf, "%2d%%", total);
16574 return decode_mode_spec_buf;
16575 }
16576 }
16577
16578 case 's':
16579 /* status of process */
16580 obj = Fget_buffer_process (Fcurrent_buffer ());
16581 if (NILP (obj))
16582 return "no process";
16583 #ifdef subprocesses
16584 obj = Fsymbol_name (Fprocess_status (obj));
16585 #endif
16586 break;
16587
16588 case 't': /* indicate TEXT or BINARY */
16589 #ifdef MODE_LINE_BINARY_TEXT
16590 return MODE_LINE_BINARY_TEXT (b);
16591 #else
16592 return "T";
16593 #endif
16594
16595 case 'z':
16596 /* coding-system (not including end-of-line format) */
16597 case 'Z':
16598 /* coding-system (including end-of-line type) */
16599 {
16600 int eol_flag = (c == 'Z');
16601 char *p = decode_mode_spec_buf;
16602
16603 if (! FRAME_WINDOW_P (f))
16604 {
16605 /* No need to mention EOL here--the terminal never needs
16606 to do EOL conversion. */
16607 p = decode_mode_spec_coding (CODING_ID_NAME (keyboard_coding.id),
16608 p, 0);
16609 p = decode_mode_spec_coding (CODING_ID_NAME (terminal_coding.id),
16610 p, 0);
16611 }
16612 p = decode_mode_spec_coding (b->buffer_file_coding_system,
16613 p, eol_flag);
16614
16615 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
16616 #ifdef subprocesses
16617 obj = Fget_buffer_process (Fcurrent_buffer ());
16618 if (PROCESSP (obj))
16619 {
16620 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16621 p, eol_flag);
16622 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16623 p, eol_flag);
16624 }
16625 #endif /* subprocesses */
16626 #endif /* 0 */
16627 *p = 0;
16628 return decode_mode_spec_buf;
16629 }
16630 }
16631
16632 if (STRINGP (obj))
16633 {
16634 *multibyte = STRING_MULTIBYTE (obj);
16635 return (char *) SDATA (obj);
16636 }
16637 else
16638 return "";
16639 }
16640
16641
16642 /* Count up to COUNT lines starting from START / START_BYTE.
16643 But don't go beyond LIMIT_BYTE.
16644 Return the number of lines thus found (always nonnegative).
16645
16646 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
16647
16648 static int
16649 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16650 int start, start_byte, limit_byte, count;
16651 int *byte_pos_ptr;
16652 {
16653 register unsigned char *cursor;
16654 unsigned char *base;
16655
16656 register int ceiling;
16657 register unsigned char *ceiling_addr;
16658 int orig_count = count;
16659
16660 /* If we are not in selective display mode,
16661 check only for newlines. */
16662 int selective_display = (!NILP (current_buffer->selective_display)
16663 && !INTEGERP (current_buffer->selective_display));
16664
16665 if (count > 0)
16666 {
16667 while (start_byte < limit_byte)
16668 {
16669 ceiling = BUFFER_CEILING_OF (start_byte);
16670 ceiling = min (limit_byte - 1, ceiling);
16671 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16672 base = (cursor = BYTE_POS_ADDR (start_byte));
16673 while (1)
16674 {
16675 if (selective_display)
16676 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16677 ;
16678 else
16679 while (*cursor != '\n' && ++cursor != ceiling_addr)
16680 ;
16681
16682 if (cursor != ceiling_addr)
16683 {
16684 if (--count == 0)
16685 {
16686 start_byte += cursor - base + 1;
16687 *byte_pos_ptr = start_byte;
16688 return orig_count;
16689 }
16690 else
16691 if (++cursor == ceiling_addr)
16692 break;
16693 }
16694 else
16695 break;
16696 }
16697 start_byte += cursor - base;
16698 }
16699 }
16700 else
16701 {
16702 while (start_byte > limit_byte)
16703 {
16704 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16705 ceiling = max (limit_byte, ceiling);
16706 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16707 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16708 while (1)
16709 {
16710 if (selective_display)
16711 while (--cursor != ceiling_addr
16712 && *cursor != '\n' && *cursor != 015)
16713 ;
16714 else
16715 while (--cursor != ceiling_addr && *cursor != '\n')
16716 ;
16717
16718 if (cursor != ceiling_addr)
16719 {
16720 if (++count == 0)
16721 {
16722 start_byte += cursor - base + 1;
16723 *byte_pos_ptr = start_byte;
16724 /* When scanning backwards, we should
16725 not count the newline posterior to which we stop. */
16726 return - orig_count - 1;
16727 }
16728 }
16729 else
16730 break;
16731 }
16732 /* Here we add 1 to compensate for the last decrement
16733 of CURSOR, which took it past the valid range. */
16734 start_byte += cursor - base + 1;
16735 }
16736 }
16737
16738 *byte_pos_ptr = limit_byte;
16739
16740 if (count < 0)
16741 return - orig_count + count;
16742 return orig_count - count;
16743
16744 }
16745
16746
16747 \f
16748 /***********************************************************************
16749 Displaying strings
16750 ***********************************************************************/
16751
16752 /* Display a NUL-terminated string, starting with index START.
16753
16754 If STRING is non-null, display that C string. Otherwise, the Lisp
16755 string LISP_STRING is displayed.
16756
16757 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16758 FACE_STRING. Display STRING or LISP_STRING with the face at
16759 FACE_STRING_POS in FACE_STRING:
16760
16761 Display the string in the environment given by IT, but use the
16762 standard display table, temporarily.
16763
16764 FIELD_WIDTH is the minimum number of output glyphs to produce.
16765 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16766 with spaces. If STRING has more characters, more than FIELD_WIDTH
16767 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
16768
16769 PRECISION is the maximum number of characters to output from
16770 STRING. PRECISION < 0 means don't truncate the string.
16771
16772 This is roughly equivalent to printf format specifiers:
16773
16774 FIELD_WIDTH PRECISION PRINTF
16775 ----------------------------------------
16776 -1 -1 %s
16777 -1 10 %.10s
16778 10 -1 %10s
16779 20 10 %20.10s
16780
16781 MULTIBYTE zero means do not display multibyte chars, > 0 means do
16782 display them, and < 0 means obey the current buffer's value of
16783 enable_multibyte_characters.
16784
16785 Value is the number of glyphs produced. */
16786
16787 static int
16788 display_string (string, lisp_string, face_string, face_string_pos,
16789 start, it, field_width, precision, max_x, multibyte)
16790 unsigned char *string;
16791 Lisp_Object lisp_string;
16792 Lisp_Object face_string;
16793 int face_string_pos;
16794 int start;
16795 struct it *it;
16796 int field_width, precision, max_x;
16797 int multibyte;
16798 {
16799 int hpos_at_start = it->hpos;
16800 int saved_face_id = it->face_id;
16801 struct glyph_row *row = it->glyph_row;
16802
16803 /* Initialize the iterator IT for iteration over STRING beginning
16804 with index START. */
16805 reseat_to_string (it, string, lisp_string, start,
16806 precision, field_width, multibyte);
16807
16808 /* If displaying STRING, set up the face of the iterator
16809 from LISP_STRING, if that's given. */
16810 if (STRINGP (face_string))
16811 {
16812 int endptr;
16813 struct face *face;
16814
16815 it->face_id
16816 = face_at_string_position (it->w, face_string, face_string_pos,
16817 0, it->region_beg_charpos,
16818 it->region_end_charpos,
16819 &endptr, it->base_face_id, 0);
16820 face = FACE_FROM_ID (it->f, it->face_id);
16821 it->face_box_p = face->box != FACE_NO_BOX;
16822 }
16823
16824 /* Set max_x to the maximum allowed X position. Don't let it go
16825 beyond the right edge of the window. */
16826 if (max_x <= 0)
16827 max_x = it->last_visible_x;
16828 else
16829 max_x = min (max_x, it->last_visible_x);
16830
16831 /* Skip over display elements that are not visible. because IT->w is
16832 hscrolled. */
16833 if (it->current_x < it->first_visible_x)
16834 move_it_in_display_line_to (it, 100000, it->first_visible_x,
16835 MOVE_TO_POS | MOVE_TO_X);
16836
16837 row->ascent = it->max_ascent;
16838 row->height = it->max_ascent + it->max_descent;
16839 row->phys_ascent = it->max_phys_ascent;
16840 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16841
16842 /* This condition is for the case that we are called with current_x
16843 past last_visible_x. */
16844 while (it->current_x < max_x)
16845 {
16846 int x_before, x, n_glyphs_before, i, nglyphs;
16847
16848 /* Get the next display element. */
16849 if (!get_next_display_element (it))
16850 break;
16851
16852 /* Produce glyphs. */
16853 x_before = it->current_x;
16854 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
16855 PRODUCE_GLYPHS (it);
16856
16857 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
16858 i = 0;
16859 x = x_before;
16860 while (i < nglyphs)
16861 {
16862 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16863
16864 if (!it->truncate_lines_p
16865 && x + glyph->pixel_width > max_x)
16866 {
16867 /* End of continued line or max_x reached. */
16868 if (CHAR_GLYPH_PADDING_P (*glyph))
16869 {
16870 /* A wide character is unbreakable. */
16871 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
16872 it->current_x = x_before;
16873 }
16874 else
16875 {
16876 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
16877 it->current_x = x;
16878 }
16879 break;
16880 }
16881 else if (x + glyph->pixel_width >= it->first_visible_x)
16882 {
16883 /* Glyph is at least partially visible. */
16884 ++it->hpos;
16885 if (x < it->first_visible_x)
16886 it->glyph_row->x = x - it->first_visible_x;
16887 }
16888 else
16889 {
16890 /* Glyph is off the left margin of the display area.
16891 Should not happen. */
16892 abort ();
16893 }
16894
16895 row->ascent = max (row->ascent, it->max_ascent);
16896 row->height = max (row->height, it->max_ascent + it->max_descent);
16897 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16898 row->phys_height = max (row->phys_height,
16899 it->max_phys_ascent + it->max_phys_descent);
16900 x += glyph->pixel_width;
16901 ++i;
16902 }
16903
16904 /* Stop if max_x reached. */
16905 if (i < nglyphs)
16906 break;
16907
16908 /* Stop at line ends. */
16909 if (ITERATOR_AT_END_OF_LINE_P (it))
16910 {
16911 it->continuation_lines_width = 0;
16912 break;
16913 }
16914
16915 set_iterator_to_next (it, 1);
16916
16917 /* Stop if truncating at the right edge. */
16918 if (it->truncate_lines_p
16919 && it->current_x >= it->last_visible_x)
16920 {
16921 /* Add truncation mark, but don't do it if the line is
16922 truncated at a padding space. */
16923 if (IT_CHARPOS (*it) < it->string_nchars)
16924 {
16925 if (!FRAME_WINDOW_P (it->f))
16926 {
16927 int i, n;
16928
16929 if (it->current_x > it->last_visible_x)
16930 {
16931 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16932 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16933 break;
16934 for (n = row->used[TEXT_AREA]; i < n; ++i)
16935 {
16936 row->used[TEXT_AREA] = i;
16937 produce_special_glyphs (it, IT_TRUNCATION);
16938 }
16939 }
16940 produce_special_glyphs (it, IT_TRUNCATION);
16941 }
16942 it->glyph_row->truncated_on_right_p = 1;
16943 }
16944 break;
16945 }
16946 }
16947
16948 /* Maybe insert a truncation at the left. */
16949 if (it->first_visible_x
16950 && IT_CHARPOS (*it) > 0)
16951 {
16952 if (!FRAME_WINDOW_P (it->f))
16953 insert_left_trunc_glyphs (it);
16954 it->glyph_row->truncated_on_left_p = 1;
16955 }
16956
16957 it->face_id = saved_face_id;
16958
16959 /* Value is number of columns displayed. */
16960 return it->hpos - hpos_at_start;
16961 }
16962
16963
16964 \f
16965 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
16966 appears as an element of LIST or as the car of an element of LIST.
16967 If PROPVAL is a list, compare each element against LIST in that
16968 way, and return 1/2 if any element of PROPVAL is found in LIST.
16969 Otherwise return 0. This function cannot quit.
16970 The return value is 2 if the text is invisible but with an ellipsis
16971 and 1 if it's invisible and without an ellipsis. */
16972
16973 int
16974 invisible_p (propval, list)
16975 register Lisp_Object propval;
16976 Lisp_Object list;
16977 {
16978 register Lisp_Object tail, proptail;
16979
16980 for (tail = list; CONSP (tail); tail = XCDR (tail))
16981 {
16982 register Lisp_Object tem;
16983 tem = XCAR (tail);
16984 if (EQ (propval, tem))
16985 return 1;
16986 if (CONSP (tem) && EQ (propval, XCAR (tem)))
16987 return NILP (XCDR (tem)) ? 1 : 2;
16988 }
16989
16990 if (CONSP (propval))
16991 {
16992 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
16993 {
16994 Lisp_Object propelt;
16995 propelt = XCAR (proptail);
16996 for (tail = list; CONSP (tail); tail = XCDR (tail))
16997 {
16998 register Lisp_Object tem;
16999 tem = XCAR (tail);
17000 if (EQ (propelt, tem))
17001 return 1;
17002 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
17003 return NILP (XCDR (tem)) ? 1 : 2;
17004 }
17005 }
17006 }
17007
17008 return 0;
17009 }
17010
17011 /* Calculate a width or height in pixels from a specification using
17012 the following elements:
17013
17014 SPEC ::=
17015 NUM - a (fractional) multiple of the default font width/height
17016 (NUM) - specifies exactly NUM pixels
17017 UNIT - a fixed number of pixels, see below.
17018 ELEMENT - size of a display element in pixels, see below.
17019 (NUM . SPEC) - equals NUM * SPEC
17020 (+ SPEC SPEC ...) - add pixel values
17021 (- SPEC SPEC ...) - subtract pixel values
17022 (- SPEC) - negate pixel value
17023
17024 NUM ::=
17025 INT or FLOAT - a number constant
17026 SYMBOL - use symbol's (buffer local) variable binding.
17027
17028 UNIT ::=
17029 in - pixels per inch *)
17030 mm - pixels per 1/1000 meter *)
17031 cm - pixels per 1/100 meter *)
17032 width - width of current font in pixels.
17033 height - height of current font in pixels.
17034
17035 *) using the ratio(s) defined in display-pixels-per-inch.
17036
17037 ELEMENT ::=
17038
17039 left-fringe - left fringe width in pixels
17040 right-fringe - right fringe width in pixels
17041
17042 left-margin - left margin width in pixels
17043 right-margin - right margin width in pixels
17044
17045 scroll-bar - scroll-bar area width in pixels
17046
17047 Examples:
17048
17049 Pixels corresponding to 5 inches:
17050 (5 . in)
17051
17052 Total width of non-text areas on left side of window (if scroll-bar is on left):
17053 '(space :width (+ left-fringe left-margin scroll-bar))
17054
17055 Align to first text column (in header line):
17056 '(space :align-to 0)
17057
17058 Align to middle of text area minus half the width of variable `my-image'
17059 containing a loaded image:
17060 '(space :align-to (0.5 . (- text my-image)))
17061
17062 Width of left margin minus width of 1 character in the default font:
17063 '(space :width (- left-margin 1))
17064
17065 Width of left margin minus width of 2 characters in the current font:
17066 '(space :width (- left-margin (2 . width)))
17067
17068 Center 1 character over left-margin (in header line):
17069 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
17070
17071 Different ways to express width of left fringe plus left margin minus one pixel:
17072 '(space :width (- (+ left-fringe left-margin) (1)))
17073 '(space :width (+ left-fringe left-margin (- (1))))
17074 '(space :width (+ left-fringe left-margin (-1)))
17075
17076 */
17077
17078 #define NUMVAL(X) \
17079 ((INTEGERP (X) || FLOATP (X)) \
17080 ? XFLOATINT (X) \
17081 : - 1)
17082
17083 int
17084 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
17085 double *res;
17086 struct it *it;
17087 Lisp_Object prop;
17088 void *font;
17089 int width_p, *align_to;
17090 {
17091 double pixels;
17092
17093 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
17094 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
17095
17096 if (NILP (prop))
17097 return OK_PIXELS (0);
17098
17099 if (SYMBOLP (prop))
17100 {
17101 if (SCHARS (SYMBOL_NAME (prop)) == 2)
17102 {
17103 char *unit = SDATA (SYMBOL_NAME (prop));
17104
17105 if (unit[0] == 'i' && unit[1] == 'n')
17106 pixels = 1.0;
17107 else if (unit[0] == 'm' && unit[1] == 'm')
17108 pixels = 25.4;
17109 else if (unit[0] == 'c' && unit[1] == 'm')
17110 pixels = 2.54;
17111 else
17112 pixels = 0;
17113 if (pixels > 0)
17114 {
17115 double ppi;
17116 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
17117 || (CONSP (Vdisplay_pixels_per_inch)
17118 && (ppi = (width_p
17119 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
17120 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
17121 ppi > 0)))
17122 return OK_PIXELS (ppi / pixels);
17123
17124 return 0;
17125 }
17126 }
17127
17128 #ifdef HAVE_WINDOW_SYSTEM
17129 if (EQ (prop, Qheight))
17130 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
17131 if (EQ (prop, Qwidth))
17132 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
17133 #else
17134 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
17135 return OK_PIXELS (1);
17136 #endif
17137
17138 if (EQ (prop, Qtext))
17139 return OK_PIXELS (width_p
17140 ? window_box_width (it->w, TEXT_AREA)
17141 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
17142
17143 if (align_to && *align_to < 0)
17144 {
17145 *res = 0;
17146 if (EQ (prop, Qleft))
17147 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
17148 if (EQ (prop, Qright))
17149 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
17150 if (EQ (prop, Qcenter))
17151 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
17152 + window_box_width (it->w, TEXT_AREA) / 2);
17153 if (EQ (prop, Qleft_fringe))
17154 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17155 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
17156 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
17157 if (EQ (prop, Qright_fringe))
17158 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17159 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17160 : window_box_right_offset (it->w, TEXT_AREA));
17161 if (EQ (prop, Qleft_margin))
17162 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
17163 if (EQ (prop, Qright_margin))
17164 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
17165 if (EQ (prop, Qscroll_bar))
17166 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
17167 ? 0
17168 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17169 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17170 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
17171 : 0)));
17172 }
17173 else
17174 {
17175 if (EQ (prop, Qleft_fringe))
17176 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
17177 if (EQ (prop, Qright_fringe))
17178 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
17179 if (EQ (prop, Qleft_margin))
17180 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
17181 if (EQ (prop, Qright_margin))
17182 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
17183 if (EQ (prop, Qscroll_bar))
17184 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
17185 }
17186
17187 prop = Fbuffer_local_value (prop, it->w->buffer);
17188 }
17189
17190 if (INTEGERP (prop) || FLOATP (prop))
17191 {
17192 int base_unit = (width_p
17193 ? FRAME_COLUMN_WIDTH (it->f)
17194 : FRAME_LINE_HEIGHT (it->f));
17195 return OK_PIXELS (XFLOATINT (prop) * base_unit);
17196 }
17197
17198 if (CONSP (prop))
17199 {
17200 Lisp_Object car = XCAR (prop);
17201 Lisp_Object cdr = XCDR (prop);
17202
17203 if (SYMBOLP (car))
17204 {
17205 #ifdef HAVE_WINDOW_SYSTEM
17206 if (valid_image_p (prop))
17207 {
17208 int id = lookup_image (it->f, prop);
17209 struct image *img = IMAGE_FROM_ID (it->f, id);
17210
17211 return OK_PIXELS (width_p ? img->width : img->height);
17212 }
17213 #endif
17214 if (EQ (car, Qplus) || EQ (car, Qminus))
17215 {
17216 int first = 1;
17217 double px;
17218
17219 pixels = 0;
17220 while (CONSP (cdr))
17221 {
17222 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
17223 font, width_p, align_to))
17224 return 0;
17225 if (first)
17226 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
17227 else
17228 pixels += px;
17229 cdr = XCDR (cdr);
17230 }
17231 if (EQ (car, Qminus))
17232 pixels = -pixels;
17233 return OK_PIXELS (pixels);
17234 }
17235
17236 car = Fbuffer_local_value (car, it->w->buffer);
17237 }
17238
17239 if (INTEGERP (car) || FLOATP (car))
17240 {
17241 double fact;
17242 pixels = XFLOATINT (car);
17243 if (NILP (cdr))
17244 return OK_PIXELS (pixels);
17245 if (calc_pixel_width_or_height (&fact, it, cdr,
17246 font, width_p, align_to))
17247 return OK_PIXELS (pixels * fact);
17248 return 0;
17249 }
17250
17251 return 0;
17252 }
17253
17254 return 0;
17255 }
17256
17257 \f
17258 /***********************************************************************
17259 Glyph Display
17260 ***********************************************************************/
17261
17262 #ifdef HAVE_WINDOW_SYSTEM
17263
17264 #if GLYPH_DEBUG
17265
17266 void
17267 dump_glyph_string (s)
17268 struct glyph_string *s;
17269 {
17270 fprintf (stderr, "glyph string\n");
17271 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17272 s->x, s->y, s->width, s->height);
17273 fprintf (stderr, " ybase = %d\n", s->ybase);
17274 fprintf (stderr, " hl = %d\n", s->hl);
17275 fprintf (stderr, " left overhang = %d, right = %d\n",
17276 s->left_overhang, s->right_overhang);
17277 fprintf (stderr, " nchars = %d\n", s->nchars);
17278 fprintf (stderr, " extends to end of line = %d\n",
17279 s->extends_to_end_of_line_p);
17280 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17281 fprintf (stderr, " bg width = %d\n", s->background_width);
17282 }
17283
17284 #endif /* GLYPH_DEBUG */
17285
17286 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17287 of XChar2b structures for S; it can't be allocated in
17288 init_glyph_string because it must be allocated via `alloca'. W
17289 is the window on which S is drawn. ROW and AREA are the glyph row
17290 and area within the row from which S is constructed. START is the
17291 index of the first glyph structure covered by S. HL is a
17292 face-override for drawing S. */
17293
17294 #ifdef HAVE_NTGUI
17295 #define OPTIONAL_HDC(hdc) hdc,
17296 #define DECLARE_HDC(hdc) HDC hdc;
17297 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17298 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17299 #endif
17300
17301 #ifndef OPTIONAL_HDC
17302 #define OPTIONAL_HDC(hdc)
17303 #define DECLARE_HDC(hdc)
17304 #define ALLOCATE_HDC(hdc, f)
17305 #define RELEASE_HDC(hdc, f)
17306 #endif
17307
17308 static void
17309 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17310 struct glyph_string *s;
17311 DECLARE_HDC (hdc)
17312 XChar2b *char2b;
17313 struct window *w;
17314 struct glyph_row *row;
17315 enum glyph_row_area area;
17316 int start;
17317 enum draw_glyphs_face hl;
17318 {
17319 bzero (s, sizeof *s);
17320 s->w = w;
17321 s->f = XFRAME (w->frame);
17322 #ifdef HAVE_NTGUI
17323 s->hdc = hdc;
17324 #endif
17325 s->display = FRAME_X_DISPLAY (s->f);
17326 s->window = FRAME_X_WINDOW (s->f);
17327 s->char2b = char2b;
17328 s->hl = hl;
17329 s->row = row;
17330 s->area = area;
17331 s->first_glyph = row->glyphs[area] + start;
17332 s->height = row->height;
17333 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17334
17335 /* Display the internal border below the tool-bar window. */
17336 if (s->w == XWINDOW (s->f->tool_bar_window))
17337 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17338
17339 s->ybase = s->y + row->ascent;
17340 }
17341
17342
17343 /* Append the list of glyph strings with head H and tail T to the list
17344 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17345
17346 static INLINE void
17347 append_glyph_string_lists (head, tail, h, t)
17348 struct glyph_string **head, **tail;
17349 struct glyph_string *h, *t;
17350 {
17351 if (h)
17352 {
17353 if (*head)
17354 (*tail)->next = h;
17355 else
17356 *head = h;
17357 h->prev = *tail;
17358 *tail = t;
17359 }
17360 }
17361
17362
17363 /* Prepend the list of glyph strings with head H and tail T to the
17364 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17365 result. */
17366
17367 static INLINE void
17368 prepend_glyph_string_lists (head, tail, h, t)
17369 struct glyph_string **head, **tail;
17370 struct glyph_string *h, *t;
17371 {
17372 if (h)
17373 {
17374 if (*head)
17375 (*head)->prev = t;
17376 else
17377 *tail = t;
17378 t->next = *head;
17379 *head = h;
17380 }
17381 }
17382
17383
17384 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17385 Set *HEAD and *TAIL to the resulting list. */
17386
17387 static INLINE void
17388 append_glyph_string (head, tail, s)
17389 struct glyph_string **head, **tail;
17390 struct glyph_string *s;
17391 {
17392 s->next = s->prev = NULL;
17393 append_glyph_string_lists (head, tail, s, s);
17394 }
17395
17396
17397 /* Get face and two-byte form of character glyph GLYPH on frame F.
17398 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17399 a pointer to a realized face that is ready for display. */
17400
17401 static INLINE struct face *
17402 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17403 struct frame *f;
17404 struct glyph *glyph;
17405 XChar2b *char2b;
17406 int *two_byte_p;
17407 {
17408 struct face *face;
17409
17410 xassert (glyph->type == CHAR_GLYPH);
17411 face = FACE_FROM_ID (f, glyph->face_id);
17412
17413 if (two_byte_p)
17414 *two_byte_p = 0;
17415
17416 if (!glyph->multibyte_p)
17417 {
17418 /* Unibyte case. We don't have to encode, but we have to make
17419 sure to use a face suitable for unibyte. */
17420 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17421 }
17422 else if (glyph->u.ch < 128
17423 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17424 {
17425 /* Case of ASCII in a face known to fit ASCII. */
17426 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17427 }
17428 else
17429 {
17430 struct font_info *font_info
17431 = FONT_INFO_FROM_ID (f, face->font_info_id);
17432 if (font_info)
17433 {
17434 struct charset *charset = CHARSET_FROM_ID (font_info->charset);
17435 unsigned code = ENCODE_CHAR (charset, glyph->u.ch);
17436
17437 if (CHARSET_DIMENSION (charset) == 1)
17438 STORE_XCHAR2B (char2b, 0, code);
17439 else
17440 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
17441
17442 /* Maybe encode the character in *CHAR2B. */
17443 if (CHARSET_ID (charset) != charset_ascii)
17444 {
17445 glyph->font_type
17446 = rif->encode_char (glyph->u.ch, char2b, font_info, charset,
17447 two_byte_p);
17448 }
17449 }
17450 }
17451
17452 /* Make sure X resources of the face are allocated. */
17453 xassert (face != NULL);
17454 PREPARE_FACE_FOR_DISPLAY (f, face);
17455 return face;
17456 }
17457
17458
17459 /* Fill glyph string S with composition components specified by S->cmp.
17460
17461 FACES is an array of faces for all components of this composition.
17462 S->gidx is the index of the first component for S.
17463 OVERLAPS_P non-zero means S should draw the foreground only, and
17464 use its physical height for clipping.
17465
17466 Value is the index of a component not in S. */
17467
17468 static int
17469 fill_composite_glyph_string (s, faces, overlaps_p)
17470 struct glyph_string *s;
17471 struct face **faces;
17472 int overlaps_p;
17473 {
17474 int i;
17475
17476 xassert (s);
17477
17478 s->for_overlaps_p = overlaps_p;
17479
17480 s->face = faces[s->gidx];
17481 s->font = s->face->font;
17482 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17483
17484 /* For all glyphs of this composition, starting at the offset
17485 S->gidx, until we reach the end of the definition or encounter a
17486 glyph that requires the different face, add it to S. */
17487 ++s->nchars;
17488 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
17489 ++s->nchars;
17490
17491 /* All glyph strings for the same composition has the same width,
17492 i.e. the width set for the first component of the composition. */
17493
17494 s->width = s->first_glyph->pixel_width;
17495
17496 /* If the specified font could not be loaded, use the frame's
17497 default font, but record the fact that we couldn't load it in
17498 the glyph string so that we can draw rectangles for the
17499 characters of the glyph string. */
17500 if (s->font == NULL)
17501 {
17502 s->font_not_found_p = 1;
17503 s->font = FRAME_FONT (s->f);
17504 }
17505
17506 /* Adjust base line for subscript/superscript text. */
17507 s->ybase += s->first_glyph->voffset;
17508
17509 xassert (s->face && s->face->gc);
17510
17511 /* This glyph string must always be drawn with 16-bit functions. */
17512 s->two_byte_p = 1;
17513
17514 return s->gidx + s->nchars;
17515 }
17516
17517
17518 /* Fill glyph string S from a sequence of character glyphs.
17519
17520 FACE_ID is the face id of the string. START is the index of the
17521 first glyph to consider, END is the index of the last + 1.
17522 OVERLAPS_P non-zero means S should draw the foreground only, and
17523 use its physical height for clipping.
17524
17525 Value is the index of the first glyph not in S. */
17526
17527 static int
17528 fill_glyph_string (s, face_id, start, end, overlaps_p)
17529 struct glyph_string *s;
17530 int face_id;
17531 int start, end, overlaps_p;
17532 {
17533 struct glyph *glyph, *last;
17534 int voffset;
17535 int glyph_not_available_p;
17536
17537 xassert (s->f == XFRAME (s->w->frame));
17538 xassert (s->nchars == 0);
17539 xassert (start >= 0 && end > start);
17540
17541 s->for_overlaps_p = overlaps_p,
17542 glyph = s->row->glyphs[s->area] + start;
17543 last = s->row->glyphs[s->area] + end;
17544 voffset = glyph->voffset;
17545
17546 glyph_not_available_p = glyph->glyph_not_available_p;
17547
17548 while (glyph < last
17549 && glyph->type == CHAR_GLYPH
17550 && glyph->voffset == voffset
17551 /* Same face id implies same font, nowadays. */
17552 && glyph->face_id == face_id
17553 && glyph->glyph_not_available_p == glyph_not_available_p)
17554 {
17555 int two_byte_p;
17556
17557 s->face = get_glyph_face_and_encoding (s->f, glyph,
17558 s->char2b + s->nchars,
17559 &two_byte_p);
17560 s->two_byte_p = two_byte_p;
17561 ++s->nchars;
17562 xassert (s->nchars <= end - start);
17563 s->width += glyph->pixel_width;
17564 ++glyph;
17565 }
17566
17567 s->font = s->face->font;
17568 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17569
17570 /* If the specified font could not be loaded, use the frame's font,
17571 but record the fact that we couldn't load it in
17572 S->font_not_found_p so that we can draw rectangles for the
17573 characters of the glyph string. */
17574 if (s->font == NULL || glyph_not_available_p)
17575 {
17576 s->font_not_found_p = 1;
17577 s->font = FRAME_FONT (s->f);
17578 }
17579
17580 /* Adjust base line for subscript/superscript text. */
17581 s->ybase += voffset;
17582
17583 xassert (s->face && s->face->gc);
17584 return glyph - s->row->glyphs[s->area];
17585 }
17586
17587
17588 /* Fill glyph string S from image glyph S->first_glyph. */
17589
17590 static void
17591 fill_image_glyph_string (s)
17592 struct glyph_string *s;
17593 {
17594 xassert (s->first_glyph->type == IMAGE_GLYPH);
17595 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
17596 xassert (s->img);
17597 s->slice = s->first_glyph->slice;
17598 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
17599 s->font = s->face->font;
17600 s->width = s->first_glyph->pixel_width;
17601
17602 /* Adjust base line for subscript/superscript text. */
17603 s->ybase += s->first_glyph->voffset;
17604 }
17605
17606
17607 /* Fill glyph string S from a sequence of stretch glyphs.
17608
17609 ROW is the glyph row in which the glyphs are found, AREA is the
17610 area within the row. START is the index of the first glyph to
17611 consider, END is the index of the last + 1.
17612
17613 Value is the index of the first glyph not in S. */
17614
17615 static int
17616 fill_stretch_glyph_string (s, row, area, start, end)
17617 struct glyph_string *s;
17618 struct glyph_row *row;
17619 enum glyph_row_area area;
17620 int start, end;
17621 {
17622 struct glyph *glyph, *last;
17623 int voffset, face_id;
17624
17625 xassert (s->first_glyph->type == STRETCH_GLYPH);
17626
17627 glyph = s->row->glyphs[s->area] + start;
17628 last = s->row->glyphs[s->area] + end;
17629 face_id = glyph->face_id;
17630 s->face = FACE_FROM_ID (s->f, face_id);
17631 s->font = s->face->font;
17632 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17633 s->width = glyph->pixel_width;
17634 voffset = glyph->voffset;
17635
17636 for (++glyph;
17637 (glyph < last
17638 && glyph->type == STRETCH_GLYPH
17639 && glyph->voffset == voffset
17640 && glyph->face_id == face_id);
17641 ++glyph)
17642 s->width += glyph->pixel_width;
17643
17644 /* Adjust base line for subscript/superscript text. */
17645 s->ybase += voffset;
17646
17647 /* The case that face->gc == 0 is handled when drawing the glyph
17648 string by calling PREPARE_FACE_FOR_DISPLAY. */
17649 xassert (s->face);
17650 return glyph - s->row->glyphs[s->area];
17651 }
17652
17653
17654 /* EXPORT for RIF:
17655 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
17656 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
17657 assumed to be zero. */
17658
17659 void
17660 x_get_glyph_overhangs (glyph, f, left, right)
17661 struct glyph *glyph;
17662 struct frame *f;
17663 int *left, *right;
17664 {
17665 *left = *right = 0;
17666
17667 if (glyph->type == CHAR_GLYPH)
17668 {
17669 XFontStruct *font;
17670 struct face *face;
17671 struct font_info *font_info;
17672 XChar2b char2b;
17673 XCharStruct *pcm;
17674
17675 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
17676 font = face->font;
17677 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
17678 if (font /* ++KFS: Should this be font_info ? */
17679 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
17680 {
17681 if (pcm->rbearing > pcm->width)
17682 *right = pcm->rbearing - pcm->width;
17683 if (pcm->lbearing < 0)
17684 *left = -pcm->lbearing;
17685 }
17686 }
17687 else if (glyph->type == COMPOSITE_GLYPH)
17688 {
17689 struct composition *cmp = composition_table[glyph->u.cmp_id];
17690
17691 *right = cmp->rbearing - cmp->pixel_width;
17692 *left = - cmp->lbearing;
17693 }
17694 }
17695
17696
17697 /* Return the index of the first glyph preceding glyph string S that
17698 is overwritten by S because of S's left overhang. Value is -1
17699 if no glyphs are overwritten. */
17700
17701 static int
17702 left_overwritten (s)
17703 struct glyph_string *s;
17704 {
17705 int k;
17706
17707 if (s->left_overhang)
17708 {
17709 int x = 0, i;
17710 struct glyph *glyphs = s->row->glyphs[s->area];
17711 int first = s->first_glyph - glyphs;
17712
17713 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
17714 x -= glyphs[i].pixel_width;
17715
17716 k = i + 1;
17717 }
17718 else
17719 k = -1;
17720
17721 return k;
17722 }
17723
17724
17725 /* Return the index of the first glyph preceding glyph string S that
17726 is overwriting S because of its right overhang. Value is -1 if no
17727 glyph in front of S overwrites S. */
17728
17729 static int
17730 left_overwriting (s)
17731 struct glyph_string *s;
17732 {
17733 int i, k, x;
17734 struct glyph *glyphs = s->row->glyphs[s->area];
17735 int first = s->first_glyph - glyphs;
17736
17737 k = -1;
17738 x = 0;
17739 for (i = first - 1; i >= 0; --i)
17740 {
17741 int left, right;
17742 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17743 if (x + right > 0)
17744 k = i;
17745 x -= glyphs[i].pixel_width;
17746 }
17747
17748 return k;
17749 }
17750
17751
17752 /* Return the index of the last glyph following glyph string S that is
17753 not overwritten by S because of S's right overhang. Value is -1 if
17754 no such glyph is found. */
17755
17756 static int
17757 right_overwritten (s)
17758 struct glyph_string *s;
17759 {
17760 int k = -1;
17761
17762 if (s->right_overhang)
17763 {
17764 int x = 0, i;
17765 struct glyph *glyphs = s->row->glyphs[s->area];
17766 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17767 int end = s->row->used[s->area];
17768
17769 for (i = first; i < end && s->right_overhang > x; ++i)
17770 x += glyphs[i].pixel_width;
17771
17772 k = i;
17773 }
17774
17775 return k;
17776 }
17777
17778
17779 /* Return the index of the last glyph following glyph string S that
17780 overwrites S because of its left overhang. Value is negative
17781 if no such glyph is found. */
17782
17783 static int
17784 right_overwriting (s)
17785 struct glyph_string *s;
17786 {
17787 int i, k, x;
17788 int end = s->row->used[s->area];
17789 struct glyph *glyphs = s->row->glyphs[s->area];
17790 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17791
17792 k = -1;
17793 x = 0;
17794 for (i = first; i < end; ++i)
17795 {
17796 int left, right;
17797 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17798 if (x - left < 0)
17799 k = i;
17800 x += glyphs[i].pixel_width;
17801 }
17802
17803 return k;
17804 }
17805
17806
17807 /* Get face and two-byte form of character C in face FACE_ID on frame
17808 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
17809 means we want to display multibyte text. DISPLAY_P non-zero means
17810 make sure that X resources for the face returned are allocated.
17811 Value is a pointer to a realized face that is ready for display if
17812 DISPLAY_P is non-zero. */
17813
17814 static INLINE struct face *
17815 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
17816 struct frame *f;
17817 int c, face_id;
17818 XChar2b *char2b;
17819 int multibyte_p, display_p;
17820 {
17821 struct face *face = FACE_FROM_ID (f, face_id);
17822
17823 if (!multibyte_p)
17824 {
17825 /* Unibyte case. We don't have to encode, but we have to make
17826 sure to use a face suitable for unibyte. */
17827 STORE_XCHAR2B (char2b, 0, c);
17828 face_id = FACE_FOR_CHAR (f, face, c, -1, Qnil);
17829 face = FACE_FROM_ID (f, face_id);
17830 }
17831 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
17832 {
17833 /* Case of ASCII in a face known to fit ASCII. */
17834 STORE_XCHAR2B (char2b, 0, c);
17835 }
17836 else if (face->font != NULL)
17837 {
17838 struct font_info *font_info
17839 = FONT_INFO_FROM_ID (f, face->font_info_id);
17840 struct charset *charset = CHARSET_FROM_ID (font_info->charset);
17841 unsigned code = ENCODE_CHAR (charset, c);
17842
17843 if (CHARSET_DIMENSION (charset) == 1)
17844 STORE_XCHAR2B (char2b, 0, code);
17845 else
17846 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
17847 /* Maybe encode the character in *CHAR2B. */
17848 rif->encode_char (c, char2b, font_info, charset, NULL);
17849 }
17850
17851 /* Make sure X resources of the face are allocated. */
17852 #ifdef HAVE_X_WINDOWS
17853 if (display_p)
17854 #endif
17855 {
17856 xassert (face != NULL);
17857 PREPARE_FACE_FOR_DISPLAY (f, face);
17858 }
17859
17860 return face;
17861 }
17862
17863
17864 /* Set background width of glyph string S. START is the index of the
17865 first glyph following S. LAST_X is the right-most x-position + 1
17866 in the drawing area. */
17867
17868 static INLINE void
17869 set_glyph_string_background_width (s, start, last_x)
17870 struct glyph_string *s;
17871 int start;
17872 int last_x;
17873 {
17874 /* If the face of this glyph string has to be drawn to the end of
17875 the drawing area, set S->extends_to_end_of_line_p. */
17876 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
17877
17878 if (start == s->row->used[s->area]
17879 && s->area == TEXT_AREA
17880 && ((s->hl == DRAW_NORMAL_TEXT
17881 && (s->row->fill_line_p
17882 || s->face->background != default_face->background
17883 || s->face->stipple != default_face->stipple
17884 || s->row->mouse_face_p))
17885 || s->hl == DRAW_MOUSE_FACE
17886 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
17887 && s->row->fill_line_p)))
17888 s->extends_to_end_of_line_p = 1;
17889
17890 /* If S extends its face to the end of the line, set its
17891 background_width to the distance to the right edge of the drawing
17892 area. */
17893 if (s->extends_to_end_of_line_p)
17894 s->background_width = last_x - s->x + 1;
17895 else
17896 s->background_width = s->width;
17897 }
17898
17899
17900 /* Compute overhangs and x-positions for glyph string S and its
17901 predecessors, or successors. X is the starting x-position for S.
17902 BACKWARD_P non-zero means process predecessors. */
17903
17904 static void
17905 compute_overhangs_and_x (s, x, backward_p)
17906 struct glyph_string *s;
17907 int x;
17908 int backward_p;
17909 {
17910 if (backward_p)
17911 {
17912 while (s)
17913 {
17914 if (rif->compute_glyph_string_overhangs)
17915 rif->compute_glyph_string_overhangs (s);
17916 x -= s->width;
17917 s->x = x;
17918 s = s->prev;
17919 }
17920 }
17921 else
17922 {
17923 while (s)
17924 {
17925 if (rif->compute_glyph_string_overhangs)
17926 rif->compute_glyph_string_overhangs (s);
17927 s->x = x;
17928 x += s->width;
17929 s = s->next;
17930 }
17931 }
17932 }
17933
17934
17935
17936 /* The following macros are only called from draw_glyphs below.
17937 They reference the following parameters of that function directly:
17938 `w', `row', `area', and `overlap_p'
17939 as well as the following local variables:
17940 `s', `f', and `hdc' (in W32) */
17941
17942 #ifdef HAVE_NTGUI
17943 /* On W32, silently add local `hdc' variable to argument list of
17944 init_glyph_string. */
17945 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17946 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
17947 #else
17948 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17949 init_glyph_string (s, char2b, w, row, area, start, hl)
17950 #endif
17951
17952 /* Add a glyph string for a stretch glyph to the list of strings
17953 between HEAD and TAIL. START is the index of the stretch glyph in
17954 row area AREA of glyph row ROW. END is the index of the last glyph
17955 in that glyph row area. X is the current output position assigned
17956 to the new glyph string constructed. HL overrides that face of the
17957 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17958 is the right-most x-position of the drawing area. */
17959
17960 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
17961 and below -- keep them on one line. */
17962 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17963 do \
17964 { \
17965 s = (struct glyph_string *) alloca (sizeof *s); \
17966 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17967 START = fill_stretch_glyph_string (s, row, area, START, END); \
17968 append_glyph_string (&HEAD, &TAIL, s); \
17969 s->x = (X); \
17970 } \
17971 while (0)
17972
17973
17974 /* Add a glyph string for an image glyph to the list of strings
17975 between HEAD and TAIL. START is the index of the image glyph in
17976 row area AREA of glyph row ROW. END is the index of the last glyph
17977 in that glyph row area. X is the current output position assigned
17978 to the new glyph string constructed. HL overrides that face of the
17979 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17980 is the right-most x-position of the drawing area. */
17981
17982 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17983 do \
17984 { \
17985 s = (struct glyph_string *) alloca (sizeof *s); \
17986 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17987 fill_image_glyph_string (s); \
17988 append_glyph_string (&HEAD, &TAIL, s); \
17989 ++START; \
17990 s->x = (X); \
17991 } \
17992 while (0)
17993
17994
17995 /* Add a glyph string for a sequence of character glyphs to the list
17996 of strings between HEAD and TAIL. START is the index of the first
17997 glyph in row area AREA of glyph row ROW that is part of the new
17998 glyph string. END is the index of the last glyph in that glyph row
17999 area. X is the current output position assigned to the new glyph
18000 string constructed. HL overrides that face of the glyph; e.g. it
18001 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
18002 right-most x-position of the drawing area. */
18003
18004 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18005 do \
18006 { \
18007 int face_id; \
18008 XChar2b *char2b; \
18009 \
18010 face_id = (row)->glyphs[area][START].face_id; \
18011 \
18012 s = (struct glyph_string *) alloca (sizeof *s); \
18013 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
18014 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
18015 append_glyph_string (&HEAD, &TAIL, s); \
18016 s->x = (X); \
18017 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
18018 } \
18019 while (0)
18020
18021
18022 /* Add a glyph string for a composite sequence to the list of strings
18023 between HEAD and TAIL. START is the index of the first glyph in
18024 row area AREA of glyph row ROW that is part of the new glyph
18025 string. END is the index of the last glyph in that glyph row area.
18026 X is the current output position assigned to the new glyph string
18027 constructed. HL overrides that face of the glyph; e.g. it is
18028 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
18029 x-position of the drawing area. */
18030
18031 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18032 do { \
18033 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
18034 int face_id = (row)->glyphs[area][START].face_id; \
18035 struct face *base_face = FACE_FROM_ID (f, face_id); \
18036 struct composition *cmp = composition_table[cmp_id]; \
18037 int glyph_len = cmp->glyph_len; \
18038 XChar2b *char2b; \
18039 struct face **faces; \
18040 struct glyph_string *first_s = NULL; \
18041 int n; \
18042 \
18043 base_face = base_face->ascii_face; \
18044 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
18045 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
18046 /* At first, fill in `char2b' and `faces'. */ \
18047 for (n = 0; n < glyph_len; n++) \
18048 { \
18049 int c = COMPOSITION_GLYPH (cmp, n); \
18050 int this_face_id = FACE_FOR_CHAR (f, base_face, c, -1, Qnil); \
18051 faces[n] = FACE_FROM_ID (f, this_face_id); \
18052 get_char_face_and_encoding (f, c, this_face_id, \
18053 char2b + n, 1, 1); \
18054 } \
18055 \
18056 /* Make glyph_strings for each glyph sequence that is drawable by \
18057 the same face, and append them to HEAD/TAIL. */ \
18058 for (n = 0; n < cmp->glyph_len;) \
18059 { \
18060 s = (struct glyph_string *) alloca (sizeof *s); \
18061 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
18062 append_glyph_string (&(HEAD), &(TAIL), s); \
18063 s->cmp = cmp; \
18064 s->gidx = n; \
18065 s->x = (X); \
18066 \
18067 if (n == 0) \
18068 first_s = s; \
18069 \
18070 n = fill_composite_glyph_string (s, faces, overlaps_p); \
18071 } \
18072 \
18073 ++START; \
18074 s = first_s; \
18075 } while (0)
18076
18077
18078 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
18079 of AREA of glyph row ROW on window W between indices START and END.
18080 HL overrides the face for drawing glyph strings, e.g. it is
18081 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
18082 x-positions of the drawing area.
18083
18084 This is an ugly monster macro construct because we must use alloca
18085 to allocate glyph strings (because draw_glyphs can be called
18086 asynchronously). */
18087
18088 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18089 do \
18090 { \
18091 HEAD = TAIL = NULL; \
18092 while (START < END) \
18093 { \
18094 struct glyph *first_glyph = (row)->glyphs[area] + START; \
18095 switch (first_glyph->type) \
18096 { \
18097 case CHAR_GLYPH: \
18098 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
18099 HL, X, LAST_X); \
18100 break; \
18101 \
18102 case COMPOSITE_GLYPH: \
18103 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
18104 HL, X, LAST_X); \
18105 break; \
18106 \
18107 case STRETCH_GLYPH: \
18108 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
18109 HL, X, LAST_X); \
18110 break; \
18111 \
18112 case IMAGE_GLYPH: \
18113 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
18114 HL, X, LAST_X); \
18115 break; \
18116 \
18117 default: \
18118 abort (); \
18119 } \
18120 \
18121 if (s) \
18122 { \
18123 set_glyph_string_background_width (s, START, LAST_X); \
18124 (X) += s->width; \
18125 } \
18126 } \
18127 } \
18128 while (0)
18129
18130
18131 /* Draw glyphs between START and END in AREA of ROW on window W,
18132 starting at x-position X. X is relative to AREA in W. HL is a
18133 face-override with the following meaning:
18134
18135 DRAW_NORMAL_TEXT draw normally
18136 DRAW_CURSOR draw in cursor face
18137 DRAW_MOUSE_FACE draw in mouse face.
18138 DRAW_INVERSE_VIDEO draw in mode line face
18139 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
18140 DRAW_IMAGE_RAISED draw an image with a raised relief around it
18141
18142 If OVERLAPS_P is non-zero, draw only the foreground of characters
18143 and clip to the physical height of ROW.
18144
18145 Value is the x-position reached, relative to AREA of W. */
18146
18147 static int
18148 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
18149 struct window *w;
18150 int x;
18151 struct glyph_row *row;
18152 enum glyph_row_area area;
18153 EMACS_INT start, end;
18154 enum draw_glyphs_face hl;
18155 int overlaps_p;
18156 {
18157 struct glyph_string *head, *tail;
18158 struct glyph_string *s;
18159 int last_x, area_width;
18160 int x_reached;
18161 int i, j;
18162 struct frame *f = XFRAME (WINDOW_FRAME (w));
18163 DECLARE_HDC (hdc);
18164
18165 ALLOCATE_HDC (hdc, f);
18166
18167 /* Let's rather be paranoid than getting a SEGV. */
18168 end = min (end, row->used[area]);
18169 start = max (0, start);
18170 start = min (end, start);
18171
18172 /* Translate X to frame coordinates. Set last_x to the right
18173 end of the drawing area. */
18174 if (row->full_width_p)
18175 {
18176 /* X is relative to the left edge of W, without scroll bars
18177 or fringes. */
18178 x += WINDOW_LEFT_EDGE_X (w);
18179 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
18180 }
18181 else
18182 {
18183 int area_left = window_box_left (w, area);
18184 x += area_left;
18185 area_width = window_box_width (w, area);
18186 last_x = area_left + area_width;
18187 }
18188
18189 /* Build a doubly-linked list of glyph_string structures between
18190 head and tail from what we have to draw. Note that the macro
18191 BUILD_GLYPH_STRINGS will modify its start parameter. That's
18192 the reason we use a separate variable `i'. */
18193 i = start;
18194 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
18195 if (tail)
18196 x_reached = tail->x + tail->background_width;
18197 else
18198 x_reached = x;
18199
18200 /* If there are any glyphs with lbearing < 0 or rbearing > width in
18201 the row, redraw some glyphs in front or following the glyph
18202 strings built above. */
18203 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
18204 {
18205 int dummy_x = 0;
18206 struct glyph_string *h, *t;
18207
18208 /* Compute overhangs for all glyph strings. */
18209 if (rif->compute_glyph_string_overhangs)
18210 for (s = head; s; s = s->next)
18211 rif->compute_glyph_string_overhangs (s);
18212
18213 /* Prepend glyph strings for glyphs in front of the first glyph
18214 string that are overwritten because of the first glyph
18215 string's left overhang. The background of all strings
18216 prepended must be drawn because the first glyph string
18217 draws over it. */
18218 i = left_overwritten (head);
18219 if (i >= 0)
18220 {
18221 j = i;
18222 BUILD_GLYPH_STRINGS (j, start, h, t,
18223 DRAW_NORMAL_TEXT, dummy_x, last_x);
18224 start = i;
18225 compute_overhangs_and_x (t, head->x, 1);
18226 prepend_glyph_string_lists (&head, &tail, h, t);
18227 }
18228
18229 /* Prepend glyph strings for glyphs in front of the first glyph
18230 string that overwrite that glyph string because of their
18231 right overhang. For these strings, only the foreground must
18232 be drawn, because it draws over the glyph string at `head'.
18233 The background must not be drawn because this would overwrite
18234 right overhangs of preceding glyphs for which no glyph
18235 strings exist. */
18236 i = left_overwriting (head);
18237 if (i >= 0)
18238 {
18239 BUILD_GLYPH_STRINGS (i, start, h, t,
18240 DRAW_NORMAL_TEXT, dummy_x, last_x);
18241 for (s = h; s; s = s->next)
18242 s->background_filled_p = 1;
18243 compute_overhangs_and_x (t, head->x, 1);
18244 prepend_glyph_string_lists (&head, &tail, h, t);
18245 }
18246
18247 /* Append glyphs strings for glyphs following the last glyph
18248 string tail that are overwritten by tail. The background of
18249 these strings has to be drawn because tail's foreground draws
18250 over it. */
18251 i = right_overwritten (tail);
18252 if (i >= 0)
18253 {
18254 BUILD_GLYPH_STRINGS (end, i, h, t,
18255 DRAW_NORMAL_TEXT, x, last_x);
18256 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18257 append_glyph_string_lists (&head, &tail, h, t);
18258 }
18259
18260 /* Append glyph strings for glyphs following the last glyph
18261 string tail that overwrite tail. The foreground of such
18262 glyphs has to be drawn because it writes into the background
18263 of tail. The background must not be drawn because it could
18264 paint over the foreground of following glyphs. */
18265 i = right_overwriting (tail);
18266 if (i >= 0)
18267 {
18268 BUILD_GLYPH_STRINGS (end, i, h, t,
18269 DRAW_NORMAL_TEXT, x, last_x);
18270 for (s = h; s; s = s->next)
18271 s->background_filled_p = 1;
18272 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18273 append_glyph_string_lists (&head, &tail, h, t);
18274 }
18275 }
18276
18277 /* Draw all strings. */
18278 for (s = head; s; s = s->next)
18279 rif->draw_glyph_string (s);
18280
18281 if (area == TEXT_AREA
18282 && !row->full_width_p
18283 /* When drawing overlapping rows, only the glyph strings'
18284 foreground is drawn, which doesn't erase a cursor
18285 completely. */
18286 && !overlaps_p)
18287 {
18288 int x0 = head ? head->x : x;
18289 int x1 = tail ? tail->x + tail->background_width : x;
18290
18291 int text_left = window_box_left (w, TEXT_AREA);
18292 x0 -= text_left;
18293 x1 -= text_left;
18294
18295 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18296 row->y, MATRIX_ROW_BOTTOM_Y (row));
18297 }
18298
18299 /* Value is the x-position up to which drawn, relative to AREA of W.
18300 This doesn't include parts drawn because of overhangs. */
18301 if (row->full_width_p)
18302 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18303 else
18304 x_reached -= window_box_left (w, area);
18305
18306 RELEASE_HDC (hdc, f);
18307
18308 return x_reached;
18309 }
18310
18311
18312 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18313 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18314
18315 static INLINE void
18316 append_glyph (it)
18317 struct it *it;
18318 {
18319 struct glyph *glyph;
18320 enum glyph_row_area area = it->area;
18321
18322 xassert (it->glyph_row);
18323 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18324
18325 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18326 if (glyph < it->glyph_row->glyphs[area + 1])
18327 {
18328 glyph->charpos = CHARPOS (it->position);
18329 glyph->object = it->object;
18330 glyph->pixel_width = it->pixel_width;
18331 glyph->ascent = it->ascent;
18332 glyph->descent = it->descent;
18333 glyph->voffset = it->voffset;
18334 glyph->type = CHAR_GLYPH;
18335 glyph->multibyte_p = it->multibyte_p;
18336 glyph->left_box_line_p = it->start_of_box_run_p;
18337 glyph->right_box_line_p = it->end_of_box_run_p;
18338 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18339 || it->phys_descent > it->descent);
18340 glyph->padding_p = 0;
18341 glyph->glyph_not_available_p = it->glyph_not_available_p;
18342 glyph->face_id = it->face_id;
18343 glyph->u.ch = it->char_to_display;
18344 glyph->slice = null_glyph_slice;
18345 glyph->font_type = FONT_TYPE_UNKNOWN;
18346 ++it->glyph_row->used[area];
18347 }
18348 else if (!fonts_changed_p)
18349 {
18350 it->w->ncols_scale_factor++;
18351 fonts_changed_p = 1;
18352 }
18353 }
18354
18355 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18356 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18357
18358 static INLINE void
18359 append_composite_glyph (it)
18360 struct it *it;
18361 {
18362 struct glyph *glyph;
18363 enum glyph_row_area area = it->area;
18364
18365 xassert (it->glyph_row);
18366
18367 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18368 if (glyph < it->glyph_row->glyphs[area + 1])
18369 {
18370 glyph->charpos = CHARPOS (it->position);
18371 glyph->object = it->object;
18372 glyph->pixel_width = it->pixel_width;
18373 glyph->ascent = it->ascent;
18374 glyph->descent = it->descent;
18375 glyph->voffset = it->voffset;
18376 glyph->type = COMPOSITE_GLYPH;
18377 glyph->multibyte_p = it->multibyte_p;
18378 glyph->left_box_line_p = it->start_of_box_run_p;
18379 glyph->right_box_line_p = it->end_of_box_run_p;
18380 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18381 || it->phys_descent > it->descent);
18382 glyph->padding_p = 0;
18383 glyph->glyph_not_available_p = 0;
18384 glyph->face_id = it->face_id;
18385 glyph->u.cmp_id = it->cmp_id;
18386 glyph->slice = null_glyph_slice;
18387 glyph->font_type = FONT_TYPE_UNKNOWN;
18388 ++it->glyph_row->used[area];
18389 }
18390 else if (!fonts_changed_p)
18391 {
18392 it->w->ncols_scale_factor++;
18393 fonts_changed_p = 1;
18394 }
18395 }
18396
18397
18398 /* Change IT->ascent and IT->height according to the setting of
18399 IT->voffset. */
18400
18401 static INLINE void
18402 take_vertical_position_into_account (it)
18403 struct it *it;
18404 {
18405 if (it->voffset)
18406 {
18407 if (it->voffset < 0)
18408 /* Increase the ascent so that we can display the text higher
18409 in the line. */
18410 it->ascent -= it->voffset;
18411 else
18412 /* Increase the descent so that we can display the text lower
18413 in the line. */
18414 it->descent += it->voffset;
18415 }
18416 }
18417
18418
18419 /* Produce glyphs/get display metrics for the image IT is loaded with.
18420 See the description of struct display_iterator in dispextern.h for
18421 an overview of struct display_iterator. */
18422
18423 static void
18424 produce_image_glyph (it)
18425 struct it *it;
18426 {
18427 struct image *img;
18428 struct face *face;
18429 int face_ascent, glyph_ascent;
18430 struct glyph_slice slice;
18431
18432 xassert (it->what == IT_IMAGE);
18433
18434 face = FACE_FROM_ID (it->f, it->face_id);
18435 xassert (face);
18436 /* Make sure X resources of the face is loaded. */
18437 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18438
18439 if (it->image_id < 0)
18440 {
18441 /* Fringe bitmap. */
18442 it->ascent = it->phys_ascent = 0;
18443 it->descent = it->phys_descent = 0;
18444 it->pixel_width = 0;
18445 it->nglyphs = 0;
18446 return;
18447 }
18448
18449 img = IMAGE_FROM_ID (it->f, it->image_id);
18450 xassert (img);
18451 /* Make sure X resources of the image is loaded. */
18452 prepare_image_for_display (it->f, img);
18453
18454 slice.x = slice.y = 0;
18455 slice.width = img->width;
18456 slice.height = img->height;
18457
18458 if (INTEGERP (it->slice.x))
18459 slice.x = XINT (it->slice.x);
18460 else if (FLOATP (it->slice.x))
18461 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
18462
18463 if (INTEGERP (it->slice.y))
18464 slice.y = XINT (it->slice.y);
18465 else if (FLOATP (it->slice.y))
18466 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
18467
18468 if (INTEGERP (it->slice.width))
18469 slice.width = XINT (it->slice.width);
18470 else if (FLOATP (it->slice.width))
18471 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
18472
18473 if (INTEGERP (it->slice.height))
18474 slice.height = XINT (it->slice.height);
18475 else if (FLOATP (it->slice.height))
18476 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
18477
18478 if (slice.x >= img->width)
18479 slice.x = img->width;
18480 if (slice.y >= img->height)
18481 slice.y = img->height;
18482 if (slice.x + slice.width >= img->width)
18483 slice.width = img->width - slice.x;
18484 if (slice.y + slice.height > img->height)
18485 slice.height = img->height - slice.y;
18486
18487 if (slice.width == 0 || slice.height == 0)
18488 return;
18489
18490 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
18491
18492 it->descent = slice.height - glyph_ascent;
18493 if (slice.y == 0)
18494 it->descent += img->vmargin;
18495 if (slice.y + slice.height == img->height)
18496 it->descent += img->vmargin;
18497 it->phys_descent = it->descent;
18498
18499 it->pixel_width = slice.width;
18500 if (slice.x == 0)
18501 it->pixel_width += img->hmargin;
18502 if (slice.x + slice.width == img->width)
18503 it->pixel_width += img->hmargin;
18504
18505 /* It's quite possible for images to have an ascent greater than
18506 their height, so don't get confused in that case. */
18507 if (it->descent < 0)
18508 it->descent = 0;
18509
18510 #if 0 /* this breaks image tiling */
18511 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
18512 face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
18513 if (face_ascent > it->ascent)
18514 it->ascent = it->phys_ascent = face_ascent;
18515 #endif
18516
18517 it->nglyphs = 1;
18518
18519 if (face->box != FACE_NO_BOX)
18520 {
18521 if (face->box_line_width > 0)
18522 {
18523 if (slice.y == 0)
18524 it->ascent += face->box_line_width;
18525 if (slice.y + slice.height == img->height)
18526 it->descent += face->box_line_width;
18527 }
18528
18529 if (it->start_of_box_run_p && slice.x == 0)
18530 it->pixel_width += abs (face->box_line_width);
18531 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
18532 it->pixel_width += abs (face->box_line_width);
18533 }
18534
18535 take_vertical_position_into_account (it);
18536
18537 if (it->glyph_row)
18538 {
18539 struct glyph *glyph;
18540 enum glyph_row_area area = it->area;
18541
18542 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18543 if (glyph < it->glyph_row->glyphs[area + 1])
18544 {
18545 glyph->charpos = CHARPOS (it->position);
18546 glyph->object = it->object;
18547 glyph->pixel_width = it->pixel_width;
18548 glyph->ascent = glyph_ascent;
18549 glyph->descent = it->descent;
18550 glyph->voffset = it->voffset;
18551 glyph->type = IMAGE_GLYPH;
18552 glyph->multibyte_p = it->multibyte_p;
18553 glyph->left_box_line_p = it->start_of_box_run_p;
18554 glyph->right_box_line_p = it->end_of_box_run_p;
18555 glyph->overlaps_vertically_p = 0;
18556 glyph->padding_p = 0;
18557 glyph->glyph_not_available_p = 0;
18558 glyph->face_id = it->face_id;
18559 glyph->u.img_id = img->id;
18560 glyph->slice = slice;
18561 glyph->font_type = FONT_TYPE_UNKNOWN;
18562 ++it->glyph_row->used[area];
18563 }
18564 else if (!fonts_changed_p)
18565 {
18566 it->w->ncols_scale_factor++;
18567 fonts_changed_p = 1;
18568 }
18569 }
18570 }
18571
18572
18573 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
18574 of the glyph, WIDTH and HEIGHT are the width and height of the
18575 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
18576
18577 static void
18578 append_stretch_glyph (it, object, width, height, ascent)
18579 struct it *it;
18580 Lisp_Object object;
18581 int width, height;
18582 int ascent;
18583 {
18584 struct glyph *glyph;
18585 enum glyph_row_area area = it->area;
18586
18587 xassert (ascent >= 0 && ascent <= height);
18588
18589 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18590 if (glyph < it->glyph_row->glyphs[area + 1])
18591 {
18592 glyph->charpos = CHARPOS (it->position);
18593 glyph->object = object;
18594 glyph->pixel_width = width;
18595 glyph->ascent = ascent;
18596 glyph->descent = height - ascent;
18597 glyph->voffset = it->voffset;
18598 glyph->type = STRETCH_GLYPH;
18599 glyph->multibyte_p = it->multibyte_p;
18600 glyph->left_box_line_p = it->start_of_box_run_p;
18601 glyph->right_box_line_p = it->end_of_box_run_p;
18602 glyph->overlaps_vertically_p = 0;
18603 glyph->padding_p = 0;
18604 glyph->glyph_not_available_p = 0;
18605 glyph->face_id = it->face_id;
18606 glyph->u.stretch.ascent = ascent;
18607 glyph->u.stretch.height = height;
18608 glyph->slice = null_glyph_slice;
18609 glyph->font_type = FONT_TYPE_UNKNOWN;
18610 ++it->glyph_row->used[area];
18611 }
18612 else if (!fonts_changed_p)
18613 {
18614 it->w->ncols_scale_factor++;
18615 fonts_changed_p = 1;
18616 }
18617 }
18618
18619
18620 /* Produce a stretch glyph for iterator IT. IT->object is the value
18621 of the glyph property displayed. The value must be a list
18622 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
18623 being recognized:
18624
18625 1. `:width WIDTH' specifies that the space should be WIDTH *
18626 canonical char width wide. WIDTH may be an integer or floating
18627 point number.
18628
18629 2. `:relative-width FACTOR' specifies that the width of the stretch
18630 should be computed from the width of the first character having the
18631 `glyph' property, and should be FACTOR times that width.
18632
18633 3. `:align-to HPOS' specifies that the space should be wide enough
18634 to reach HPOS, a value in canonical character units.
18635
18636 Exactly one of the above pairs must be present.
18637
18638 4. `:height HEIGHT' specifies that the height of the stretch produced
18639 should be HEIGHT, measured in canonical character units.
18640
18641 5. `:relative-height FACTOR' specifies that the height of the
18642 stretch should be FACTOR times the height of the characters having
18643 the glyph property.
18644
18645 Either none or exactly one of 4 or 5 must be present.
18646
18647 6. `:ascent ASCENT' specifies that ASCENT percent of the height
18648 of the stretch should be used for the ascent of the stretch.
18649 ASCENT must be in the range 0 <= ASCENT <= 100. */
18650
18651 static void
18652 produce_stretch_glyph (it)
18653 struct it *it;
18654 {
18655 /* (space :width WIDTH :height HEIGHT ...) */
18656 Lisp_Object prop, plist;
18657 int width = 0, height = 0, align_to = -1;
18658 int zero_width_ok_p = 0, zero_height_ok_p = 0;
18659 int ascent = 0;
18660 double tem;
18661 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18662 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
18663
18664 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18665
18666 /* List should start with `space'. */
18667 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
18668 plist = XCDR (it->object);
18669
18670 /* Compute the width of the stretch. */
18671 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
18672 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
18673 {
18674 /* Absolute width `:width WIDTH' specified and valid. */
18675 zero_width_ok_p = 1;
18676 width = (int)tem;
18677 }
18678 else if (prop = Fplist_get (plist, QCrelative_width),
18679 NUMVAL (prop) > 0)
18680 {
18681 /* Relative width `:relative-width FACTOR' specified and valid.
18682 Compute the width of the characters having the `glyph'
18683 property. */
18684 struct it it2;
18685 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
18686
18687 it2 = *it;
18688 if (it->multibyte_p)
18689 {
18690 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
18691 - IT_BYTEPOS (*it));
18692 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
18693 }
18694 else
18695 it2.c = *p, it2.len = 1;
18696
18697 it2.glyph_row = NULL;
18698 it2.what = IT_CHARACTER;
18699 x_produce_glyphs (&it2);
18700 width = NUMVAL (prop) * it2.pixel_width;
18701 }
18702 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
18703 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
18704 {
18705 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
18706 align_to = (align_to < 0
18707 ? 0
18708 : align_to - window_box_left_offset (it->w, TEXT_AREA));
18709 else if (align_to < 0)
18710 align_to = window_box_left_offset (it->w, TEXT_AREA);
18711 width = max (0, (int)tem + align_to - it->current_x);
18712 zero_width_ok_p = 1;
18713 }
18714 else
18715 /* Nothing specified -> width defaults to canonical char width. */
18716 width = FRAME_COLUMN_WIDTH (it->f);
18717
18718 if (width <= 0 && (width < 0 || !zero_width_ok_p))
18719 width = 1;
18720
18721 /* Compute height. */
18722 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
18723 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18724 {
18725 height = (int)tem;
18726 zero_height_ok_p = 1;
18727 }
18728 else if (prop = Fplist_get (plist, QCrelative_height),
18729 NUMVAL (prop) > 0)
18730 height = FONT_HEIGHT (font) * NUMVAL (prop);
18731 else
18732 height = FONT_HEIGHT (font);
18733
18734 if (height <= 0 && (height < 0 || !zero_height_ok_p))
18735 height = 1;
18736
18737 /* Compute percentage of height used for ascent. If
18738 `:ascent ASCENT' is present and valid, use that. Otherwise,
18739 derive the ascent from the font in use. */
18740 if (prop = Fplist_get (plist, QCascent),
18741 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
18742 ascent = height * NUMVAL (prop) / 100.0;
18743 else if (!NILP (prop)
18744 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18745 ascent = min (max (0, (int)tem), height);
18746 else
18747 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
18748
18749 if (width > 0 && height > 0 && it->glyph_row)
18750 {
18751 Lisp_Object object = it->stack[it->sp - 1].string;
18752 if (!STRINGP (object))
18753 object = it->w->buffer;
18754 append_stretch_glyph (it, object, width, height, ascent);
18755 }
18756
18757 it->pixel_width = width;
18758 it->ascent = it->phys_ascent = ascent;
18759 it->descent = it->phys_descent = height - it->ascent;
18760 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
18761
18762 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
18763 {
18764 if (face->box_line_width > 0)
18765 {
18766 it->ascent += face->box_line_width;
18767 it->descent += face->box_line_width;
18768 }
18769
18770 if (it->start_of_box_run_p)
18771 it->pixel_width += abs (face->box_line_width);
18772 if (it->end_of_box_run_p)
18773 it->pixel_width += abs (face->box_line_width);
18774 }
18775
18776 take_vertical_position_into_account (it);
18777 }
18778
18779 /* Calculate line-height and line-spacing properties.
18780 An integer value specifies explicit pixel value.
18781 A float value specifies relative value to current face height.
18782 A cons (float . face-name) specifies relative value to
18783 height of specified face font.
18784
18785 Returns height in pixels, or nil. */
18786
18787 static Lisp_Object
18788 calc_line_height_property (it, prop, font, boff, total)
18789 struct it *it;
18790 Lisp_Object prop;
18791 XFontStruct *font;
18792 int boff, *total;
18793 {
18794 Lisp_Object position, val;
18795 Lisp_Object face_name = Qnil;
18796 int ascent, descent, height, override;
18797
18798 if (STRINGP (it->object))
18799 position = make_number (IT_STRING_CHARPOS (*it));
18800 else
18801 position = make_number (IT_CHARPOS (*it));
18802
18803 val = Fget_char_property (position, prop, it->object);
18804
18805 if (NILP (val))
18806 return val;
18807
18808 if (total && CONSP (val) && EQ (XCAR (val), Qtotal))
18809 {
18810 *total = 1;
18811 val = XCDR (val);
18812 }
18813
18814 if (INTEGERP (val))
18815 return val;
18816
18817 if (CONSP (val))
18818 {
18819 face_name = XCDR (val);
18820 val = XCAR (val);
18821 }
18822 else if (SYMBOLP (val))
18823 {
18824 face_name = val;
18825 val = Qnil;
18826 }
18827
18828 override = EQ (prop, Qline_height);
18829
18830 if (NILP (face_name))
18831 {
18832 font = FRAME_FONT (it->f);
18833 boff = FRAME_BASELINE_OFFSET (it->f);
18834 }
18835 else if (EQ (face_name, Qt))
18836 {
18837 override = 0;
18838 }
18839 else
18840 {
18841 int face_id;
18842 struct face *face;
18843 struct font_info *font_info;
18844
18845 face_id = lookup_named_face (it->f, face_name);
18846 if (face_id < 0)
18847 return make_number (-1);
18848
18849 face = FACE_FROM_ID (it->f, face_id);
18850 font = face->font;
18851 if (font == NULL)
18852 return make_number (-1);
18853
18854 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18855 boff = font_info->baseline_offset;
18856 if (font_info->vertical_centering)
18857 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18858 }
18859
18860 ascent = FONT_BASE (font) + boff;
18861 descent = FONT_DESCENT (font) - boff;
18862
18863 if (override)
18864 {
18865 it->override_ascent = ascent;
18866 it->override_descent = descent;
18867 it->override_boff = boff;
18868 }
18869
18870 height = ascent + descent;
18871 if (FLOATP (val))
18872 height = (int)(XFLOAT_DATA (val) * height);
18873 else if (INTEGERP (val))
18874 height *= XINT (val);
18875
18876 return make_number (height);
18877 }
18878
18879
18880 /* RIF:
18881 Produce glyphs/get display metrics for the display element IT is
18882 loaded with. See the description of struct display_iterator in
18883 dispextern.h for an overview of struct display_iterator. */
18884
18885 void
18886 x_produce_glyphs (it)
18887 struct it *it;
18888 {
18889 int extra_line_spacing = it->extra_line_spacing;
18890
18891 it->glyph_not_available_p = 0;
18892
18893 if (it->what == IT_CHARACTER)
18894 {
18895 XChar2b char2b;
18896 XFontStruct *font;
18897 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18898 XCharStruct *pcm;
18899 int font_not_found_p;
18900 struct font_info *font_info;
18901 int boff; /* baseline offset */
18902 /* We may change it->multibyte_p upon unibyte<->multibyte
18903 conversion. So, save the current value now and restore it
18904 later.
18905
18906 Note: It seems that we don't have to record multibyte_p in
18907 struct glyph because the character code itself tells if or
18908 not the character is multibyte. Thus, in the future, we must
18909 consider eliminating the field `multibyte_p' in the struct
18910 glyph. */
18911 int saved_multibyte_p = it->multibyte_p;
18912
18913 /* Maybe translate single-byte characters to multibyte, or the
18914 other way. */
18915 it->char_to_display = it->c;
18916 if (!ASCII_BYTE_P (it->c)
18917 && ! it->multibyte_p)
18918 {
18919 if (SINGLE_BYTE_CHAR_P (it->c)
18920 && unibyte_display_via_language_environment)
18921 it->char_to_display = unibyte_char_to_multibyte (it->c);
18922 if (! SINGLE_BYTE_CHAR_P (it->c))
18923 {
18924 it->multibyte_p = 1;
18925 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
18926 -1, Qnil);
18927 face = FACE_FROM_ID (it->f, it->face_id);
18928 }
18929 }
18930
18931 /* Get font to use. Encode IT->char_to_display. */
18932 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18933 &char2b, it->multibyte_p, 0);
18934 font = face->font;
18935
18936 /* When no suitable font found, use the default font. */
18937 font_not_found_p = font == NULL;
18938 if (font_not_found_p)
18939 {
18940 font = FRAME_FONT (it->f);
18941 boff = FRAME_BASELINE_OFFSET (it->f);
18942 font_info = NULL;
18943 }
18944 else
18945 {
18946 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18947 boff = font_info->baseline_offset;
18948 if (font_info->vertical_centering)
18949 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18950 }
18951
18952 if (it->char_to_display >= ' '
18953 && (!it->multibyte_p || it->char_to_display < 128))
18954 {
18955 /* Either unibyte or ASCII. */
18956 int stretched_p;
18957
18958 it->nglyphs = 1;
18959
18960 pcm = rif->per_char_metric (font, &char2b,
18961 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
18962
18963 if (it->override_ascent >= 0)
18964 {
18965 it->ascent = it->override_ascent;
18966 it->descent = it->override_descent;
18967 boff = it->override_boff;
18968 }
18969 else
18970 {
18971 it->ascent = FONT_BASE (font) + boff;
18972 it->descent = FONT_DESCENT (font) - boff;
18973 }
18974
18975 if (pcm)
18976 {
18977 it->phys_ascent = pcm->ascent + boff;
18978 it->phys_descent = pcm->descent - boff;
18979 it->pixel_width = pcm->width;
18980 }
18981 else
18982 {
18983 it->glyph_not_available_p = 1;
18984 it->phys_ascent = it->ascent;
18985 it->phys_descent = it->descent;
18986 it->pixel_width = FONT_WIDTH (font);
18987 }
18988
18989 if (it->constrain_row_ascent_descent_p)
18990 {
18991 if (it->descent > it->max_descent)
18992 {
18993 it->ascent += it->descent - it->max_descent;
18994 it->descent = it->max_descent;
18995 }
18996 if (it->ascent > it->max_ascent)
18997 {
18998 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
18999 it->ascent = it->max_ascent;
19000 }
19001 it->phys_ascent = min (it->phys_ascent, it->ascent);
19002 it->phys_descent = min (it->phys_descent, it->descent);
19003 extra_line_spacing = 0;
19004 }
19005
19006 /* If this is a space inside a region of text with
19007 `space-width' property, change its width. */
19008 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
19009 if (stretched_p)
19010 it->pixel_width *= XFLOATINT (it->space_width);
19011
19012 /* If face has a box, add the box thickness to the character
19013 height. If character has a box line to the left and/or
19014 right, add the box line width to the character's width. */
19015 if (face->box != FACE_NO_BOX)
19016 {
19017 int thick = face->box_line_width;
19018
19019 if (thick > 0)
19020 {
19021 it->ascent += thick;
19022 it->descent += thick;
19023 }
19024 else
19025 thick = -thick;
19026
19027 if (it->start_of_box_run_p)
19028 it->pixel_width += thick;
19029 if (it->end_of_box_run_p)
19030 it->pixel_width += thick;
19031 }
19032
19033 /* If face has an overline, add the height of the overline
19034 (1 pixel) and a 1 pixel margin to the character height. */
19035 if (face->overline_p)
19036 it->ascent += 2;
19037
19038 if (it->constrain_row_ascent_descent_p)
19039 {
19040 if (it->ascent > it->max_ascent)
19041 it->ascent = it->max_ascent;
19042 if (it->descent > it->max_descent)
19043 it->descent = it->max_descent;
19044 }
19045
19046 take_vertical_position_into_account (it);
19047
19048 /* If we have to actually produce glyphs, do it. */
19049 if (it->glyph_row)
19050 {
19051 if (stretched_p)
19052 {
19053 /* Translate a space with a `space-width' property
19054 into a stretch glyph. */
19055 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
19056 / FONT_HEIGHT (font));
19057 append_stretch_glyph (it, it->object, it->pixel_width,
19058 it->ascent + it->descent, ascent);
19059 }
19060 else
19061 append_glyph (it);
19062
19063 /* If characters with lbearing or rbearing are displayed
19064 in this line, record that fact in a flag of the
19065 glyph row. This is used to optimize X output code. */
19066 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
19067 it->glyph_row->contains_overlapping_glyphs_p = 1;
19068 }
19069 }
19070 else if (it->char_to_display == '\n')
19071 {
19072 /* A newline has no width but we need the height of the line.
19073 But if previous part of the line set a height, don't
19074 increase that height */
19075
19076 Lisp_Object height;
19077
19078 it->override_ascent = -1;
19079 it->pixel_width = 0;
19080 it->nglyphs = 0;
19081
19082 height = calc_line_height_property(it, Qline_height, font, boff, 0);
19083
19084 if (it->override_ascent >= 0)
19085 {
19086 it->ascent = it->override_ascent;
19087 it->descent = it->override_descent;
19088 boff = it->override_boff;
19089 }
19090 else
19091 {
19092 it->ascent = FONT_BASE (font) + boff;
19093 it->descent = FONT_DESCENT (font) - boff;
19094 }
19095
19096 if (EQ (height, make_number(0)))
19097 {
19098 if (it->descent > it->max_descent)
19099 {
19100 it->ascent += it->descent - it->max_descent;
19101 it->descent = it->max_descent;
19102 }
19103 if (it->ascent > it->max_ascent)
19104 {
19105 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19106 it->ascent = it->max_ascent;
19107 }
19108 it->phys_ascent = min (it->phys_ascent, it->ascent);
19109 it->phys_descent = min (it->phys_descent, it->descent);
19110 it->constrain_row_ascent_descent_p = 1;
19111 extra_line_spacing = 0;
19112 }
19113 else
19114 {
19115 Lisp_Object spacing;
19116 int total = 0;
19117
19118 it->phys_ascent = it->ascent;
19119 it->phys_descent = it->descent;
19120
19121 if ((it->max_ascent > 0 || it->max_descent > 0)
19122 && face->box != FACE_NO_BOX
19123 && face->box_line_width > 0)
19124 {
19125 it->ascent += face->box_line_width;
19126 it->descent += face->box_line_width;
19127 }
19128 if (!NILP (height)
19129 && XINT (height) > it->ascent + it->descent)
19130 it->ascent = XINT (height) - it->descent;
19131
19132 spacing = calc_line_height_property(it, Qline_spacing, font, boff, &total);
19133 if (INTEGERP (spacing))
19134 {
19135 extra_line_spacing = XINT (spacing);
19136 if (total)
19137 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19138 }
19139 }
19140 }
19141 else if (it->char_to_display == '\t')
19142 {
19143 int tab_width = it->tab_width * FRAME_COLUMN_WIDTH (it->f);
19144 int x = it->current_x + it->continuation_lines_width;
19145 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
19146
19147 /* If the distance from the current position to the next tab
19148 stop is less than a canonical character width, use the
19149 tab stop after that. */
19150 if (next_tab_x - x < FRAME_COLUMN_WIDTH (it->f))
19151 next_tab_x += tab_width;
19152
19153 it->pixel_width = next_tab_x - x;
19154 it->nglyphs = 1;
19155 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
19156 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
19157
19158 if (it->glyph_row)
19159 {
19160 append_stretch_glyph (it, it->object, it->pixel_width,
19161 it->ascent + it->descent, it->ascent);
19162 }
19163 }
19164 else
19165 {
19166 /* A multi-byte character. Assume that the display width of the
19167 character is the width of the character multiplied by the
19168 width of the font. */
19169
19170 /* If we found a font, this font should give us the right
19171 metrics. If we didn't find a font, use the frame's
19172 default font and calculate the width of the character by
19173 multiplying the width of font by the width of the
19174 character. */
19175
19176 pcm = rif->per_char_metric (font, &char2b,
19177 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
19178
19179 if (font_not_found_p || !pcm)
19180 {
19181 it->glyph_not_available_p = 1;
19182 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
19183 * CHAR_WIDTH (it->char_to_display));
19184 it->phys_ascent = FONT_BASE (font) + boff;
19185 it->phys_descent = FONT_DESCENT (font) - boff;
19186 }
19187 else
19188 {
19189 it->pixel_width = pcm->width;
19190 it->phys_ascent = pcm->ascent + boff;
19191 it->phys_descent = pcm->descent - boff;
19192 if (it->glyph_row
19193 && (pcm->lbearing < 0
19194 || pcm->rbearing > pcm->width))
19195 it->glyph_row->contains_overlapping_glyphs_p = 1;
19196 }
19197 it->nglyphs = 1;
19198 it->ascent = FONT_BASE (font) + boff;
19199 it->descent = FONT_DESCENT (font) - boff;
19200 if (face->box != FACE_NO_BOX)
19201 {
19202 int thick = face->box_line_width;
19203
19204 if (thick > 0)
19205 {
19206 it->ascent += thick;
19207 it->descent += thick;
19208 }
19209 else
19210 thick = - thick;
19211
19212 if (it->start_of_box_run_p)
19213 it->pixel_width += thick;
19214 if (it->end_of_box_run_p)
19215 it->pixel_width += thick;
19216 }
19217
19218 /* If face has an overline, add the height of the overline
19219 (1 pixel) and a 1 pixel margin to the character height. */
19220 if (face->overline_p)
19221 it->ascent += 2;
19222
19223 take_vertical_position_into_account (it);
19224
19225 if (it->glyph_row)
19226 append_glyph (it);
19227 }
19228 it->multibyte_p = saved_multibyte_p;
19229 }
19230 else if (it->what == IT_COMPOSITION)
19231 {
19232 /* Note: A composition is represented as one glyph in the
19233 glyph matrix. There are no padding glyphs. */
19234 XChar2b char2b;
19235 XFontStruct *font;
19236 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19237 XCharStruct *pcm;
19238 int font_not_found_p;
19239 struct font_info *font_info;
19240 int boff; /* baseline offset */
19241 struct composition *cmp = composition_table[it->cmp_id];
19242 int pos;
19243
19244 /* Maybe translate single-byte characters to multibyte. */
19245 it->char_to_display = it->c;
19246 if (unibyte_display_via_language_environment
19247 && it->c >= 0200)
19248 {
19249 it->char_to_display = unibyte_char_to_multibyte (it->c);
19250 }
19251
19252 /* Get face and font to use. Encode IT->char_to_display. */
19253 pos = STRINGP (it->string) ? IT_STRING_CHARPOS (*it) : IT_CHARPOS (*it);
19254 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
19255 pos, it->string);
19256 face = FACE_FROM_ID (it->f, it->face_id);
19257 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19258 &char2b, it->multibyte_p, 0);
19259 font = face->font;
19260
19261 /* When no suitable font found, use the default font. */
19262 font_not_found_p = font == NULL;
19263 if (font_not_found_p)
19264 {
19265 font = FRAME_FONT (it->f);
19266 boff = FRAME_BASELINE_OFFSET (it->f);
19267 font_info = NULL;
19268 }
19269 else
19270 {
19271 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19272 boff = font_info->baseline_offset;
19273 if (font_info->vertical_centering)
19274 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19275 }
19276
19277 /* There are no padding glyphs, so there is only one glyph to
19278 produce for the composition. Important is that pixel_width,
19279 ascent and descent are the values of what is drawn by
19280 draw_glyphs (i.e. the values of the overall glyphs composed). */
19281 it->nglyphs = 1;
19282
19283 /* If we have not yet calculated pixel size data of glyphs of
19284 the composition for the current face font, calculate them
19285 now. Theoretically, we have to check all fonts for the
19286 glyphs, but that requires much time and memory space. So,
19287 here we check only the font of the first glyph. This leads
19288 to incorrect display, but it's very rare, and C-l (recenter)
19289 can correct the display anyway. */
19290 if (cmp->glyph_len == 0)
19291 {
19292 cmp->lbearing = cmp->rbearing = 0;
19293 cmp->pixel_width = cmp->ascent = cmp->descent = 0;
19294 }
19295 else if (cmp->font != (void *) font)
19296 {
19297 /* Ascent and descent of the font of the first character of
19298 this composition (adjusted by baseline offset). Ascent
19299 and descent of overall glyphs should not be less than
19300 them respectively. */
19301 int font_ascent = FONT_BASE (font) + boff;
19302 int font_descent = FONT_DESCENT (font) - boff;
19303 int font_height = FONT_HEIGHT (font);
19304 /* Bounding box of the overall glyphs. */
19305 int leftmost, rightmost, lowest, highest;
19306 int lbearing, rbearing;
19307 int i, width, ascent, descent;
19308
19309 cmp->font = (void *) font;
19310
19311 /* Initialize the bounding box. */
19312 if (font_info
19313 && (pcm = rif->per_char_metric (font, &char2b,
19314 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19315 {
19316 width = pcm->width;
19317 ascent = pcm->ascent;
19318 descent = pcm->descent;
19319 lbearing = pcm->lbearing;
19320 if (lbearing > 0)
19321 lbearing = 0;
19322 rbearing = pcm->rbearing;
19323 if (rbearing < width)
19324 rbearing = width;
19325 }
19326 else
19327 {
19328 width = FONT_WIDTH (font);
19329 ascent = FONT_BASE (font);
19330 descent = FONT_DESCENT (font);
19331 lbearing = 0;
19332 rbearing = width;
19333 }
19334
19335 rightmost = width;
19336 lowest = - descent + boff;
19337 highest = ascent + boff;
19338 leftmost = 0;
19339
19340 if (font_info
19341 && font_info->default_ascent
19342 && CHAR_TABLE_P (Vuse_default_ascent)
19343 && !NILP (Faref (Vuse_default_ascent,
19344 make_number (it->char_to_display))))
19345 highest = font_info->default_ascent + boff;
19346
19347 /* Draw the first glyph at the normal position. It may be
19348 shifted to right later if some other glyphs are drawn at
19349 the left. */
19350 cmp->offsets[0] = 0;
19351 cmp->offsets[1] = boff;
19352 cmp->lbearing = lbearing;
19353 cmp->rbearing = rbearing;
19354
19355 /* Set cmp->offsets for the remaining glyphs. */
19356 for (i = 1; i < cmp->glyph_len; i++)
19357 {
19358 int left, right, btm, top;
19359 int ch = COMPOSITION_GLYPH (cmp, i);
19360 int face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
19361
19362 face = FACE_FROM_ID (it->f, face_id);
19363 get_char_face_and_encoding (it->f, ch, face->id,
19364 &char2b, it->multibyte_p, 0);
19365 font = face->font;
19366 if (font == NULL)
19367 {
19368 font = FRAME_FONT (it->f);
19369 boff = FRAME_BASELINE_OFFSET (it->f);
19370 font_info = NULL;
19371 }
19372 else
19373 {
19374 font_info
19375 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19376 boff = font_info->baseline_offset;
19377 if (font_info->vertical_centering)
19378 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19379 }
19380
19381 if (font_info
19382 && (pcm = rif->per_char_metric (font, &char2b,
19383 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19384 {
19385 width = pcm->width;
19386 ascent = pcm->ascent;
19387 descent = pcm->descent;
19388 lbearing = pcm->lbearing;
19389 if (lbearing > 0)
19390 lbearing = 0;
19391 rbearing = pcm->rbearing;
19392 if (rbearing < width)
19393 rbearing = width;
19394 }
19395 else
19396 {
19397 width = FONT_WIDTH (font);
19398 ascent = 1;
19399 descent = 0;
19400 lbearing = 0;
19401 rbearing = width;
19402 }
19403
19404 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19405 {
19406 /* Relative composition with or without
19407 alternate chars. */
19408 left = (leftmost + rightmost - width) / 2;
19409 btm = - descent + boff;
19410 if (font_info && font_info->relative_compose
19411 && (! CHAR_TABLE_P (Vignore_relative_composition)
19412 || NILP (Faref (Vignore_relative_composition,
19413 make_number (ch)))))
19414 {
19415
19416 if (- descent >= font_info->relative_compose)
19417 /* One extra pixel between two glyphs. */
19418 btm = highest + 1;
19419 else if (ascent <= 0)
19420 /* One extra pixel between two glyphs. */
19421 btm = lowest - 1 - ascent - descent;
19422 }
19423 }
19424 else
19425 {
19426 /* A composition rule is specified by an integer
19427 value that encodes global and new reference
19428 points (GREF and NREF). GREF and NREF are
19429 specified by numbers as below:
19430
19431 0---1---2 -- ascent
19432 | |
19433 | |
19434 | |
19435 9--10--11 -- center
19436 | |
19437 ---3---4---5--- baseline
19438 | |
19439 6---7---8 -- descent
19440 */
19441 int rule = COMPOSITION_RULE (cmp, i);
19442 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
19443
19444 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
19445 grefx = gref % 3, nrefx = nref % 3;
19446 grefy = gref / 3, nrefy = nref / 3;
19447 if (xoff)
19448 xoff = font_height * (xoff - 128) / 256;
19449 if (yoff)
19450 yoff = font_height * (yoff - 128) / 256;
19451
19452 left = (leftmost
19453 + grefx * (rightmost - leftmost) / 2
19454 - nrefx * width / 2
19455 + xoff);
19456
19457 btm = ((grefy == 0 ? highest
19458 : grefy == 1 ? 0
19459 : grefy == 2 ? lowest
19460 : (highest + lowest) / 2)
19461 - (nrefy == 0 ? ascent + descent
19462 : nrefy == 1 ? descent - boff
19463 : nrefy == 2 ? 0
19464 : (ascent + descent) / 2)
19465 + yoff);
19466 }
19467
19468 cmp->offsets[i * 2] = left;
19469 cmp->offsets[i * 2 + 1] = btm + descent;
19470
19471 /* Update the bounding box of the overall glyphs. */
19472 if (width > 0)
19473 {
19474 right = left + width;
19475 if (left < leftmost)
19476 leftmost = left;
19477 if (right > rightmost)
19478 rightmost = right;
19479 }
19480 top = btm + descent + ascent;
19481 if (top > highest)
19482 highest = top;
19483 if (btm < lowest)
19484 lowest = btm;
19485
19486 if (cmp->lbearing > left + lbearing)
19487 cmp->lbearing = left + lbearing;
19488 if (cmp->rbearing < left + rbearing)
19489 cmp->rbearing = left + rbearing;
19490 }
19491
19492 /* If there are glyphs whose x-offsets are negative,
19493 shift all glyphs to the right and make all x-offsets
19494 non-negative. */
19495 if (leftmost < 0)
19496 {
19497 for (i = 0; i < cmp->glyph_len; i++)
19498 cmp->offsets[i * 2] -= leftmost;
19499 rightmost -= leftmost;
19500 cmp->lbearing -= leftmost;
19501 cmp->rbearing -= leftmost;
19502 }
19503
19504 cmp->pixel_width = rightmost;
19505 cmp->ascent = highest;
19506 cmp->descent = - lowest;
19507 if (cmp->ascent < font_ascent)
19508 cmp->ascent = font_ascent;
19509 if (cmp->descent < font_descent)
19510 cmp->descent = font_descent;
19511 }
19512
19513 if (it->glyph_row
19514 && (cmp->lbearing < 0
19515 || cmp->rbearing > cmp->pixel_width))
19516 it->glyph_row->contains_overlapping_glyphs_p = 1;
19517
19518 it->pixel_width = cmp->pixel_width;
19519 it->ascent = it->phys_ascent = cmp->ascent;
19520 it->descent = it->phys_descent = cmp->descent;
19521
19522 if (face->box != FACE_NO_BOX)
19523 {
19524 int thick = face->box_line_width;
19525
19526 if (thick > 0)
19527 {
19528 it->ascent += thick;
19529 it->descent += thick;
19530 }
19531 else
19532 thick = - thick;
19533
19534 if (it->start_of_box_run_p)
19535 it->pixel_width += thick;
19536 if (it->end_of_box_run_p)
19537 it->pixel_width += thick;
19538 }
19539
19540 /* If face has an overline, add the height of the overline
19541 (1 pixel) and a 1 pixel margin to the character height. */
19542 if (face->overline_p)
19543 it->ascent += 2;
19544
19545 take_vertical_position_into_account (it);
19546
19547 if (it->glyph_row)
19548 append_composite_glyph (it);
19549 }
19550 else if (it->what == IT_IMAGE)
19551 produce_image_glyph (it);
19552 else if (it->what == IT_STRETCH)
19553 produce_stretch_glyph (it);
19554
19555 /* Accumulate dimensions. Note: can't assume that it->descent > 0
19556 because this isn't true for images with `:ascent 100'. */
19557 xassert (it->ascent >= 0 && it->descent >= 0);
19558 if (it->area == TEXT_AREA)
19559 it->current_x += it->pixel_width;
19560
19561 if (extra_line_spacing > 0)
19562 it->descent += extra_line_spacing;
19563
19564 it->max_ascent = max (it->max_ascent, it->ascent);
19565 it->max_descent = max (it->max_descent, it->descent);
19566 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
19567 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
19568 }
19569
19570 /* EXPORT for RIF:
19571 Output LEN glyphs starting at START at the nominal cursor position.
19572 Advance the nominal cursor over the text. The global variable
19573 updated_window contains the window being updated, updated_row is
19574 the glyph row being updated, and updated_area is the area of that
19575 row being updated. */
19576
19577 void
19578 x_write_glyphs (start, len)
19579 struct glyph *start;
19580 int len;
19581 {
19582 int x, hpos;
19583
19584 xassert (updated_window && updated_row);
19585 BLOCK_INPUT;
19586
19587 /* Write glyphs. */
19588
19589 hpos = start - updated_row->glyphs[updated_area];
19590 x = draw_glyphs (updated_window, output_cursor.x,
19591 updated_row, updated_area,
19592 hpos, hpos + len,
19593 DRAW_NORMAL_TEXT, 0);
19594
19595 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
19596 if (updated_area == TEXT_AREA
19597 && updated_window->phys_cursor_on_p
19598 && updated_window->phys_cursor.vpos == output_cursor.vpos
19599 && updated_window->phys_cursor.hpos >= hpos
19600 && updated_window->phys_cursor.hpos < hpos + len)
19601 updated_window->phys_cursor_on_p = 0;
19602
19603 UNBLOCK_INPUT;
19604
19605 /* Advance the output cursor. */
19606 output_cursor.hpos += len;
19607 output_cursor.x = x;
19608 }
19609
19610
19611 /* EXPORT for RIF:
19612 Insert LEN glyphs from START at the nominal cursor position. */
19613
19614 void
19615 x_insert_glyphs (start, len)
19616 struct glyph *start;
19617 int len;
19618 {
19619 struct frame *f;
19620 struct window *w;
19621 int line_height, shift_by_width, shifted_region_width;
19622 struct glyph_row *row;
19623 struct glyph *glyph;
19624 int frame_x, frame_y;
19625 EMACS_INT hpos;
19626
19627 xassert (updated_window && updated_row);
19628 BLOCK_INPUT;
19629 w = updated_window;
19630 f = XFRAME (WINDOW_FRAME (w));
19631
19632 /* Get the height of the line we are in. */
19633 row = updated_row;
19634 line_height = row->height;
19635
19636 /* Get the width of the glyphs to insert. */
19637 shift_by_width = 0;
19638 for (glyph = start; glyph < start + len; ++glyph)
19639 shift_by_width += glyph->pixel_width;
19640
19641 /* Get the width of the region to shift right. */
19642 shifted_region_width = (window_box_width (w, updated_area)
19643 - output_cursor.x
19644 - shift_by_width);
19645
19646 /* Shift right. */
19647 frame_x = window_box_left (w, updated_area) + output_cursor.x;
19648 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
19649
19650 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
19651 line_height, shift_by_width);
19652
19653 /* Write the glyphs. */
19654 hpos = start - row->glyphs[updated_area];
19655 draw_glyphs (w, output_cursor.x, row, updated_area,
19656 hpos, hpos + len,
19657 DRAW_NORMAL_TEXT, 0);
19658
19659 /* Advance the output cursor. */
19660 output_cursor.hpos += len;
19661 output_cursor.x += shift_by_width;
19662 UNBLOCK_INPUT;
19663 }
19664
19665
19666 /* EXPORT for RIF:
19667 Erase the current text line from the nominal cursor position
19668 (inclusive) to pixel column TO_X (exclusive). The idea is that
19669 everything from TO_X onward is already erased.
19670
19671 TO_X is a pixel position relative to updated_area of
19672 updated_window. TO_X == -1 means clear to the end of this area. */
19673
19674 void
19675 x_clear_end_of_line (to_x)
19676 int to_x;
19677 {
19678 struct frame *f;
19679 struct window *w = updated_window;
19680 int max_x, min_y, max_y;
19681 int from_x, from_y, to_y;
19682
19683 xassert (updated_window && updated_row);
19684 f = XFRAME (w->frame);
19685
19686 if (updated_row->full_width_p)
19687 max_x = WINDOW_TOTAL_WIDTH (w);
19688 else
19689 max_x = window_box_width (w, updated_area);
19690 max_y = window_text_bottom_y (w);
19691
19692 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
19693 of window. For TO_X > 0, truncate to end of drawing area. */
19694 if (to_x == 0)
19695 return;
19696 else if (to_x < 0)
19697 to_x = max_x;
19698 else
19699 to_x = min (to_x, max_x);
19700
19701 to_y = min (max_y, output_cursor.y + updated_row->height);
19702
19703 /* Notice if the cursor will be cleared by this operation. */
19704 if (!updated_row->full_width_p)
19705 notice_overwritten_cursor (w, updated_area,
19706 output_cursor.x, -1,
19707 updated_row->y,
19708 MATRIX_ROW_BOTTOM_Y (updated_row));
19709
19710 from_x = output_cursor.x;
19711
19712 /* Translate to frame coordinates. */
19713 if (updated_row->full_width_p)
19714 {
19715 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
19716 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
19717 }
19718 else
19719 {
19720 int area_left = window_box_left (w, updated_area);
19721 from_x += area_left;
19722 to_x += area_left;
19723 }
19724
19725 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
19726 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
19727 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
19728
19729 /* Prevent inadvertently clearing to end of the X window. */
19730 if (to_x > from_x && to_y > from_y)
19731 {
19732 BLOCK_INPUT;
19733 rif->clear_frame_area (f, from_x, from_y,
19734 to_x - from_x, to_y - from_y);
19735 UNBLOCK_INPUT;
19736 }
19737 }
19738
19739 #endif /* HAVE_WINDOW_SYSTEM */
19740
19741
19742 \f
19743 /***********************************************************************
19744 Cursor types
19745 ***********************************************************************/
19746
19747 /* Value is the internal representation of the specified cursor type
19748 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
19749 of the bar cursor. */
19750
19751 static enum text_cursor_kinds
19752 get_specified_cursor_type (arg, width)
19753 Lisp_Object arg;
19754 int *width;
19755 {
19756 enum text_cursor_kinds type;
19757
19758 if (NILP (arg))
19759 return NO_CURSOR;
19760
19761 if (EQ (arg, Qbox))
19762 return FILLED_BOX_CURSOR;
19763
19764 if (EQ (arg, Qhollow))
19765 return HOLLOW_BOX_CURSOR;
19766
19767 if (EQ (arg, Qbar))
19768 {
19769 *width = 2;
19770 return BAR_CURSOR;
19771 }
19772
19773 if (CONSP (arg)
19774 && EQ (XCAR (arg), Qbar)
19775 && INTEGERP (XCDR (arg))
19776 && XINT (XCDR (arg)) >= 0)
19777 {
19778 *width = XINT (XCDR (arg));
19779 return BAR_CURSOR;
19780 }
19781
19782 if (EQ (arg, Qhbar))
19783 {
19784 *width = 2;
19785 return HBAR_CURSOR;
19786 }
19787
19788 if (CONSP (arg)
19789 && EQ (XCAR (arg), Qhbar)
19790 && INTEGERP (XCDR (arg))
19791 && XINT (XCDR (arg)) >= 0)
19792 {
19793 *width = XINT (XCDR (arg));
19794 return HBAR_CURSOR;
19795 }
19796
19797 /* Treat anything unknown as "hollow box cursor".
19798 It was bad to signal an error; people have trouble fixing
19799 .Xdefaults with Emacs, when it has something bad in it. */
19800 type = HOLLOW_BOX_CURSOR;
19801
19802 return type;
19803 }
19804
19805 /* Set the default cursor types for specified frame. */
19806 void
19807 set_frame_cursor_types (f, arg)
19808 struct frame *f;
19809 Lisp_Object arg;
19810 {
19811 int width;
19812 Lisp_Object tem;
19813
19814 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
19815 FRAME_CURSOR_WIDTH (f) = width;
19816
19817 /* By default, set up the blink-off state depending on the on-state. */
19818
19819 tem = Fassoc (arg, Vblink_cursor_alist);
19820 if (!NILP (tem))
19821 {
19822 FRAME_BLINK_OFF_CURSOR (f)
19823 = get_specified_cursor_type (XCDR (tem), &width);
19824 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
19825 }
19826 else
19827 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
19828 }
19829
19830
19831 /* Return the cursor we want to be displayed in window W. Return
19832 width of bar/hbar cursor through WIDTH arg. Return with
19833 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
19834 (i.e. if the `system caret' should track this cursor).
19835
19836 In a mini-buffer window, we want the cursor only to appear if we
19837 are reading input from this window. For the selected window, we
19838 want the cursor type given by the frame parameter or buffer local
19839 setting of cursor-type. If explicitly marked off, draw no cursor.
19840 In all other cases, we want a hollow box cursor. */
19841
19842 static enum text_cursor_kinds
19843 get_window_cursor_type (w, glyph, width, active_cursor)
19844 struct window *w;
19845 struct glyph *glyph;
19846 int *width;
19847 int *active_cursor;
19848 {
19849 struct frame *f = XFRAME (w->frame);
19850 struct buffer *b = XBUFFER (w->buffer);
19851 int cursor_type = DEFAULT_CURSOR;
19852 Lisp_Object alt_cursor;
19853 int non_selected = 0;
19854
19855 *active_cursor = 1;
19856
19857 /* Echo area */
19858 if (cursor_in_echo_area
19859 && FRAME_HAS_MINIBUF_P (f)
19860 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
19861 {
19862 if (w == XWINDOW (echo_area_window))
19863 {
19864 *width = FRAME_CURSOR_WIDTH (f);
19865 return FRAME_DESIRED_CURSOR (f);
19866 }
19867
19868 *active_cursor = 0;
19869 non_selected = 1;
19870 }
19871
19872 /* Nonselected window or nonselected frame. */
19873 else if (w != XWINDOW (f->selected_window)
19874 #ifdef HAVE_WINDOW_SYSTEM
19875 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
19876 #endif
19877 )
19878 {
19879 *active_cursor = 0;
19880
19881 if (MINI_WINDOW_P (w) && minibuf_level == 0)
19882 return NO_CURSOR;
19883
19884 non_selected = 1;
19885 }
19886
19887 /* Never display a cursor in a window in which cursor-type is nil. */
19888 if (NILP (b->cursor_type))
19889 return NO_CURSOR;
19890
19891 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
19892 if (non_selected)
19893 {
19894 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
19895 return get_specified_cursor_type (alt_cursor, width);
19896 }
19897
19898 /* Get the normal cursor type for this window. */
19899 if (EQ (b->cursor_type, Qt))
19900 {
19901 cursor_type = FRAME_DESIRED_CURSOR (f);
19902 *width = FRAME_CURSOR_WIDTH (f);
19903 }
19904 else
19905 cursor_type = get_specified_cursor_type (b->cursor_type, width);
19906
19907 /* Use normal cursor if not blinked off. */
19908 if (!w->cursor_off_p)
19909 {
19910 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
19911 if (cursor_type == FILLED_BOX_CURSOR)
19912 cursor_type = HOLLOW_BOX_CURSOR;
19913 }
19914 return cursor_type;
19915 }
19916
19917 /* Cursor is blinked off, so determine how to "toggle" it. */
19918
19919 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
19920 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
19921 return get_specified_cursor_type (XCDR (alt_cursor), width);
19922
19923 /* Then see if frame has specified a specific blink off cursor type. */
19924 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
19925 {
19926 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
19927 return FRAME_BLINK_OFF_CURSOR (f);
19928 }
19929
19930 #if 0
19931 /* Some people liked having a permanently visible blinking cursor,
19932 while others had very strong opinions against it. So it was
19933 decided to remove it. KFS 2003-09-03 */
19934
19935 /* Finally perform built-in cursor blinking:
19936 filled box <-> hollow box
19937 wide [h]bar <-> narrow [h]bar
19938 narrow [h]bar <-> no cursor
19939 other type <-> no cursor */
19940
19941 if (cursor_type == FILLED_BOX_CURSOR)
19942 return HOLLOW_BOX_CURSOR;
19943
19944 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
19945 {
19946 *width = 1;
19947 return cursor_type;
19948 }
19949 #endif
19950
19951 return NO_CURSOR;
19952 }
19953
19954
19955 #ifdef HAVE_WINDOW_SYSTEM
19956
19957 /* Notice when the text cursor of window W has been completely
19958 overwritten by a drawing operation that outputs glyphs in AREA
19959 starting at X0 and ending at X1 in the line starting at Y0 and
19960 ending at Y1. X coordinates are area-relative. X1 < 0 means all
19961 the rest of the line after X0 has been written. Y coordinates
19962 are window-relative. */
19963
19964 static void
19965 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
19966 struct window *w;
19967 enum glyph_row_area area;
19968 int x0, y0, x1, y1;
19969 {
19970 int cx0, cx1, cy0, cy1;
19971 struct glyph_row *row;
19972
19973 if (!w->phys_cursor_on_p)
19974 return;
19975 if (area != TEXT_AREA)
19976 return;
19977
19978 row = w->current_matrix->rows + w->phys_cursor.vpos;
19979 if (!row->displays_text_p)
19980 return;
19981
19982 if (row->cursor_in_fringe_p)
19983 {
19984 row->cursor_in_fringe_p = 0;
19985 draw_fringe_bitmap (w, row, 0);
19986 w->phys_cursor_on_p = 0;
19987 return;
19988 }
19989
19990 cx0 = w->phys_cursor.x;
19991 cx1 = cx0 + w->phys_cursor_width;
19992 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
19993 return;
19994
19995 /* The cursor image will be completely removed from the
19996 screen if the output area intersects the cursor area in
19997 y-direction. When we draw in [y0 y1[, and some part of
19998 the cursor is at y < y0, that part must have been drawn
19999 before. When scrolling, the cursor is erased before
20000 actually scrolling, so we don't come here. When not
20001 scrolling, the rows above the old cursor row must have
20002 changed, and in this case these rows must have written
20003 over the cursor image.
20004
20005 Likewise if part of the cursor is below y1, with the
20006 exception of the cursor being in the first blank row at
20007 the buffer and window end because update_text_area
20008 doesn't draw that row. (Except when it does, but
20009 that's handled in update_text_area.) */
20010
20011 cy0 = w->phys_cursor.y;
20012 cy1 = cy0 + w->phys_cursor_height;
20013 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
20014 return;
20015
20016 w->phys_cursor_on_p = 0;
20017 }
20018
20019 #endif /* HAVE_WINDOW_SYSTEM */
20020
20021 \f
20022 /************************************************************************
20023 Mouse Face
20024 ************************************************************************/
20025
20026 #ifdef HAVE_WINDOW_SYSTEM
20027
20028 /* EXPORT for RIF:
20029 Fix the display of area AREA of overlapping row ROW in window W. */
20030
20031 void
20032 x_fix_overlapping_area (w, row, area)
20033 struct window *w;
20034 struct glyph_row *row;
20035 enum glyph_row_area area;
20036 {
20037 int i, x;
20038
20039 BLOCK_INPUT;
20040
20041 x = 0;
20042 for (i = 0; i < row->used[area];)
20043 {
20044 if (row->glyphs[area][i].overlaps_vertically_p)
20045 {
20046 int start = i, start_x = x;
20047
20048 do
20049 {
20050 x += row->glyphs[area][i].pixel_width;
20051 ++i;
20052 }
20053 while (i < row->used[area]
20054 && row->glyphs[area][i].overlaps_vertically_p);
20055
20056 draw_glyphs (w, start_x, row, area,
20057 start, i,
20058 DRAW_NORMAL_TEXT, 1);
20059 }
20060 else
20061 {
20062 x += row->glyphs[area][i].pixel_width;
20063 ++i;
20064 }
20065 }
20066
20067 UNBLOCK_INPUT;
20068 }
20069
20070
20071 /* EXPORT:
20072 Draw the cursor glyph of window W in glyph row ROW. See the
20073 comment of draw_glyphs for the meaning of HL. */
20074
20075 void
20076 draw_phys_cursor_glyph (w, row, hl)
20077 struct window *w;
20078 struct glyph_row *row;
20079 enum draw_glyphs_face hl;
20080 {
20081 /* If cursor hpos is out of bounds, don't draw garbage. This can
20082 happen in mini-buffer windows when switching between echo area
20083 glyphs and mini-buffer. */
20084 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
20085 {
20086 int on_p = w->phys_cursor_on_p;
20087 int x1;
20088 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
20089 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
20090 hl, 0);
20091 w->phys_cursor_on_p = on_p;
20092
20093 if (hl == DRAW_CURSOR)
20094 w->phys_cursor_width = x1 - w->phys_cursor.x;
20095 /* When we erase the cursor, and ROW is overlapped by other
20096 rows, make sure that these overlapping parts of other rows
20097 are redrawn. */
20098 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
20099 {
20100 if (row > w->current_matrix->rows
20101 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
20102 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
20103
20104 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
20105 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
20106 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
20107 }
20108 }
20109 }
20110
20111
20112 /* EXPORT:
20113 Erase the image of a cursor of window W from the screen. */
20114
20115 void
20116 erase_phys_cursor (w)
20117 struct window *w;
20118 {
20119 struct frame *f = XFRAME (w->frame);
20120 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20121 int hpos = w->phys_cursor.hpos;
20122 int vpos = w->phys_cursor.vpos;
20123 int mouse_face_here_p = 0;
20124 struct glyph_matrix *active_glyphs = w->current_matrix;
20125 struct glyph_row *cursor_row;
20126 struct glyph *cursor_glyph;
20127 enum draw_glyphs_face hl;
20128
20129 /* No cursor displayed or row invalidated => nothing to do on the
20130 screen. */
20131 if (w->phys_cursor_type == NO_CURSOR)
20132 goto mark_cursor_off;
20133
20134 /* VPOS >= active_glyphs->nrows means that window has been resized.
20135 Don't bother to erase the cursor. */
20136 if (vpos >= active_glyphs->nrows)
20137 goto mark_cursor_off;
20138
20139 /* If row containing cursor is marked invalid, there is nothing we
20140 can do. */
20141 cursor_row = MATRIX_ROW (active_glyphs, vpos);
20142 if (!cursor_row->enabled_p)
20143 goto mark_cursor_off;
20144
20145 /* If row is completely invisible, don't attempt to delete a cursor which
20146 isn't there. This can happen if cursor is at top of a window, and
20147 we switch to a buffer with a header line in that window. */
20148 if (cursor_row->visible_height <= 0)
20149 goto mark_cursor_off;
20150
20151 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
20152 if (cursor_row->cursor_in_fringe_p)
20153 {
20154 cursor_row->cursor_in_fringe_p = 0;
20155 draw_fringe_bitmap (w, cursor_row, 0);
20156 goto mark_cursor_off;
20157 }
20158
20159 /* This can happen when the new row is shorter than the old one.
20160 In this case, either draw_glyphs or clear_end_of_line
20161 should have cleared the cursor. Note that we wouldn't be
20162 able to erase the cursor in this case because we don't have a
20163 cursor glyph at hand. */
20164 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
20165 goto mark_cursor_off;
20166
20167 /* If the cursor is in the mouse face area, redisplay that when
20168 we clear the cursor. */
20169 if (! NILP (dpyinfo->mouse_face_window)
20170 && w == XWINDOW (dpyinfo->mouse_face_window)
20171 && (vpos > dpyinfo->mouse_face_beg_row
20172 || (vpos == dpyinfo->mouse_face_beg_row
20173 && hpos >= dpyinfo->mouse_face_beg_col))
20174 && (vpos < dpyinfo->mouse_face_end_row
20175 || (vpos == dpyinfo->mouse_face_end_row
20176 && hpos < dpyinfo->mouse_face_end_col))
20177 /* Don't redraw the cursor's spot in mouse face if it is at the
20178 end of a line (on a newline). The cursor appears there, but
20179 mouse highlighting does not. */
20180 && cursor_row->used[TEXT_AREA] > hpos)
20181 mouse_face_here_p = 1;
20182
20183 /* Maybe clear the display under the cursor. */
20184 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
20185 {
20186 int x, y;
20187 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
20188
20189 cursor_glyph = get_phys_cursor_glyph (w);
20190 if (cursor_glyph == NULL)
20191 goto mark_cursor_off;
20192
20193 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
20194 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
20195
20196 rif->clear_frame_area (f, x, y,
20197 cursor_glyph->pixel_width, cursor_row->visible_height);
20198 }
20199
20200 /* Erase the cursor by redrawing the character underneath it. */
20201 if (mouse_face_here_p)
20202 hl = DRAW_MOUSE_FACE;
20203 else
20204 hl = DRAW_NORMAL_TEXT;
20205 draw_phys_cursor_glyph (w, cursor_row, hl);
20206
20207 mark_cursor_off:
20208 w->phys_cursor_on_p = 0;
20209 w->phys_cursor_type = NO_CURSOR;
20210 }
20211
20212
20213 /* EXPORT:
20214 Display or clear cursor of window W. If ON is zero, clear the
20215 cursor. If it is non-zero, display the cursor. If ON is nonzero,
20216 where to put the cursor is specified by HPOS, VPOS, X and Y. */
20217
20218 void
20219 display_and_set_cursor (w, on, hpos, vpos, x, y)
20220 struct window *w;
20221 int on, hpos, vpos, x, y;
20222 {
20223 struct frame *f = XFRAME (w->frame);
20224 int new_cursor_type;
20225 int new_cursor_width;
20226 int active_cursor;
20227 struct glyph_row *glyph_row;
20228 struct glyph *glyph;
20229
20230 /* This is pointless on invisible frames, and dangerous on garbaged
20231 windows and frames; in the latter case, the frame or window may
20232 be in the midst of changing its size, and x and y may be off the
20233 window. */
20234 if (! FRAME_VISIBLE_P (f)
20235 || FRAME_GARBAGED_P (f)
20236 || vpos >= w->current_matrix->nrows
20237 || hpos >= w->current_matrix->matrix_w)
20238 return;
20239
20240 /* If cursor is off and we want it off, return quickly. */
20241 if (!on && !w->phys_cursor_on_p)
20242 return;
20243
20244 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
20245 /* If cursor row is not enabled, we don't really know where to
20246 display the cursor. */
20247 if (!glyph_row->enabled_p)
20248 {
20249 w->phys_cursor_on_p = 0;
20250 return;
20251 }
20252
20253 glyph = NULL;
20254 if (!glyph_row->exact_window_width_line_p
20255 || hpos < glyph_row->used[TEXT_AREA])
20256 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
20257
20258 xassert (interrupt_input_blocked);
20259
20260 /* Set new_cursor_type to the cursor we want to be displayed. */
20261 new_cursor_type = get_window_cursor_type (w, glyph,
20262 &new_cursor_width, &active_cursor);
20263
20264 /* If cursor is currently being shown and we don't want it to be or
20265 it is in the wrong place, or the cursor type is not what we want,
20266 erase it. */
20267 if (w->phys_cursor_on_p
20268 && (!on
20269 || w->phys_cursor.x != x
20270 || w->phys_cursor.y != y
20271 || new_cursor_type != w->phys_cursor_type
20272 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
20273 && new_cursor_width != w->phys_cursor_width)))
20274 erase_phys_cursor (w);
20275
20276 /* Don't check phys_cursor_on_p here because that flag is only set
20277 to zero in some cases where we know that the cursor has been
20278 completely erased, to avoid the extra work of erasing the cursor
20279 twice. In other words, phys_cursor_on_p can be 1 and the cursor
20280 still not be visible, or it has only been partly erased. */
20281 if (on)
20282 {
20283 w->phys_cursor_ascent = glyph_row->ascent;
20284 w->phys_cursor_height = glyph_row->height;
20285
20286 /* Set phys_cursor_.* before x_draw_.* is called because some
20287 of them may need the information. */
20288 w->phys_cursor.x = x;
20289 w->phys_cursor.y = glyph_row->y;
20290 w->phys_cursor.hpos = hpos;
20291 w->phys_cursor.vpos = vpos;
20292 }
20293
20294 rif->draw_window_cursor (w, glyph_row, x, y,
20295 new_cursor_type, new_cursor_width,
20296 on, active_cursor);
20297 }
20298
20299
20300 /* Switch the display of W's cursor on or off, according to the value
20301 of ON. */
20302
20303 static void
20304 update_window_cursor (w, on)
20305 struct window *w;
20306 int on;
20307 {
20308 /* Don't update cursor in windows whose frame is in the process
20309 of being deleted. */
20310 if (w->current_matrix)
20311 {
20312 BLOCK_INPUT;
20313 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20314 w->phys_cursor.x, w->phys_cursor.y);
20315 UNBLOCK_INPUT;
20316 }
20317 }
20318
20319
20320 /* Call update_window_cursor with parameter ON_P on all leaf windows
20321 in the window tree rooted at W. */
20322
20323 static void
20324 update_cursor_in_window_tree (w, on_p)
20325 struct window *w;
20326 int on_p;
20327 {
20328 while (w)
20329 {
20330 if (!NILP (w->hchild))
20331 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20332 else if (!NILP (w->vchild))
20333 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20334 else
20335 update_window_cursor (w, on_p);
20336
20337 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20338 }
20339 }
20340
20341
20342 /* EXPORT:
20343 Display the cursor on window W, or clear it, according to ON_P.
20344 Don't change the cursor's position. */
20345
20346 void
20347 x_update_cursor (f, on_p)
20348 struct frame *f;
20349 int on_p;
20350 {
20351 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20352 }
20353
20354
20355 /* EXPORT:
20356 Clear the cursor of window W to background color, and mark the
20357 cursor as not shown. This is used when the text where the cursor
20358 is is about to be rewritten. */
20359
20360 void
20361 x_clear_cursor (w)
20362 struct window *w;
20363 {
20364 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20365 update_window_cursor (w, 0);
20366 }
20367
20368
20369 /* EXPORT:
20370 Display the active region described by mouse_face_* according to DRAW. */
20371
20372 void
20373 show_mouse_face (dpyinfo, draw)
20374 Display_Info *dpyinfo;
20375 enum draw_glyphs_face draw;
20376 {
20377 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20378 struct frame *f = XFRAME (WINDOW_FRAME (w));
20379
20380 if (/* If window is in the process of being destroyed, don't bother
20381 to do anything. */
20382 w->current_matrix != NULL
20383 /* Don't update mouse highlight if hidden */
20384 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20385 /* Recognize when we are called to operate on rows that don't exist
20386 anymore. This can happen when a window is split. */
20387 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20388 {
20389 int phys_cursor_on_p = w->phys_cursor_on_p;
20390 struct glyph_row *row, *first, *last;
20391
20392 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20393 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20394
20395 for (row = first; row <= last && row->enabled_p; ++row)
20396 {
20397 int start_hpos, end_hpos, start_x;
20398
20399 /* For all but the first row, the highlight starts at column 0. */
20400 if (row == first)
20401 {
20402 start_hpos = dpyinfo->mouse_face_beg_col;
20403 start_x = dpyinfo->mouse_face_beg_x;
20404 }
20405 else
20406 {
20407 start_hpos = 0;
20408 start_x = 0;
20409 }
20410
20411 if (row == last)
20412 end_hpos = dpyinfo->mouse_face_end_col;
20413 else
20414 end_hpos = row->used[TEXT_AREA];
20415
20416 if (end_hpos > start_hpos)
20417 {
20418 draw_glyphs (w, start_x, row, TEXT_AREA,
20419 start_hpos, end_hpos,
20420 draw, 0);
20421
20422 row->mouse_face_p
20423 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20424 }
20425 }
20426
20427 /* When we've written over the cursor, arrange for it to
20428 be displayed again. */
20429 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20430 {
20431 BLOCK_INPUT;
20432 display_and_set_cursor (w, 1,
20433 w->phys_cursor.hpos, w->phys_cursor.vpos,
20434 w->phys_cursor.x, w->phys_cursor.y);
20435 UNBLOCK_INPUT;
20436 }
20437 }
20438
20439 /* Change the mouse cursor. */
20440 if (draw == DRAW_NORMAL_TEXT)
20441 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
20442 else if (draw == DRAW_MOUSE_FACE)
20443 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
20444 else
20445 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
20446 }
20447
20448 /* EXPORT:
20449 Clear out the mouse-highlighted active region.
20450 Redraw it un-highlighted first. Value is non-zero if mouse
20451 face was actually drawn unhighlighted. */
20452
20453 int
20454 clear_mouse_face (dpyinfo)
20455 Display_Info *dpyinfo;
20456 {
20457 int cleared = 0;
20458
20459 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
20460 {
20461 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
20462 cleared = 1;
20463 }
20464
20465 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20466 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20467 dpyinfo->mouse_face_window = Qnil;
20468 dpyinfo->mouse_face_overlay = Qnil;
20469 return cleared;
20470 }
20471
20472
20473 /* EXPORT:
20474 Non-zero if physical cursor of window W is within mouse face. */
20475
20476 int
20477 cursor_in_mouse_face_p (w)
20478 struct window *w;
20479 {
20480 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20481 int in_mouse_face = 0;
20482
20483 if (WINDOWP (dpyinfo->mouse_face_window)
20484 && XWINDOW (dpyinfo->mouse_face_window) == w)
20485 {
20486 int hpos = w->phys_cursor.hpos;
20487 int vpos = w->phys_cursor.vpos;
20488
20489 if (vpos >= dpyinfo->mouse_face_beg_row
20490 && vpos <= dpyinfo->mouse_face_end_row
20491 && (vpos > dpyinfo->mouse_face_beg_row
20492 || hpos >= dpyinfo->mouse_face_beg_col)
20493 && (vpos < dpyinfo->mouse_face_end_row
20494 || hpos < dpyinfo->mouse_face_end_col
20495 || dpyinfo->mouse_face_past_end))
20496 in_mouse_face = 1;
20497 }
20498
20499 return in_mouse_face;
20500 }
20501
20502
20503
20504 \f
20505 /* Find the glyph matrix position of buffer position CHARPOS in window
20506 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
20507 current glyphs must be up to date. If CHARPOS is above window
20508 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
20509 of last line in W. In the row containing CHARPOS, stop before glyphs
20510 having STOP as object. */
20511
20512 #if 1 /* This is a version of fast_find_position that's more correct
20513 in the presence of hscrolling, for example. I didn't install
20514 it right away because the problem fixed is minor, it failed
20515 in 20.x as well, and I think it's too risky to install
20516 so near the release of 21.1. 2001-09-25 gerd. */
20517
20518 static int
20519 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
20520 struct window *w;
20521 EMACS_INT charpos;
20522 int *hpos, *vpos, *x, *y;
20523 Lisp_Object stop;
20524 {
20525 struct glyph_row *row, *first;
20526 struct glyph *glyph, *end;
20527 int past_end = 0;
20528
20529 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20530 row = row_containing_pos (w, charpos, first, NULL, 0);
20531 if (row == NULL)
20532 {
20533 if (charpos < MATRIX_ROW_START_CHARPOS (first))
20534 {
20535 *x = *y = *hpos = *vpos = 0;
20536 return 1;
20537 }
20538 else
20539 {
20540 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
20541 past_end = 1;
20542 }
20543 }
20544
20545 *x = row->x;
20546 *y = row->y;
20547 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20548
20549 glyph = row->glyphs[TEXT_AREA];
20550 end = glyph + row->used[TEXT_AREA];
20551
20552 /* Skip over glyphs not having an object at the start of the row.
20553 These are special glyphs like truncation marks on terminal
20554 frames. */
20555 if (row->displays_text_p)
20556 while (glyph < end
20557 && INTEGERP (glyph->object)
20558 && !EQ (stop, glyph->object)
20559 && glyph->charpos < 0)
20560 {
20561 *x += glyph->pixel_width;
20562 ++glyph;
20563 }
20564
20565 while (glyph < end
20566 && !INTEGERP (glyph->object)
20567 && !EQ (stop, glyph->object)
20568 && (!BUFFERP (glyph->object)
20569 || glyph->charpos < charpos))
20570 {
20571 *x += glyph->pixel_width;
20572 ++glyph;
20573 }
20574
20575 *hpos = glyph - row->glyphs[TEXT_AREA];
20576 return !past_end;
20577 }
20578
20579 #else /* not 1 */
20580
20581 static int
20582 fast_find_position (w, pos, hpos, vpos, x, y, stop)
20583 struct window *w;
20584 EMACS_INT pos;
20585 int *hpos, *vpos, *x, *y;
20586 Lisp_Object stop;
20587 {
20588 int i;
20589 int lastcol;
20590 int maybe_next_line_p = 0;
20591 int line_start_position;
20592 int yb = window_text_bottom_y (w);
20593 struct glyph_row *row, *best_row;
20594 int row_vpos, best_row_vpos;
20595 int current_x;
20596
20597 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20598 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20599
20600 while (row->y < yb)
20601 {
20602 if (row->used[TEXT_AREA])
20603 line_start_position = row->glyphs[TEXT_AREA]->charpos;
20604 else
20605 line_start_position = 0;
20606
20607 if (line_start_position > pos)
20608 break;
20609 /* If the position sought is the end of the buffer,
20610 don't include the blank lines at the bottom of the window. */
20611 else if (line_start_position == pos
20612 && pos == BUF_ZV (XBUFFER (w->buffer)))
20613 {
20614 maybe_next_line_p = 1;
20615 break;
20616 }
20617 else if (line_start_position > 0)
20618 {
20619 best_row = row;
20620 best_row_vpos = row_vpos;
20621 }
20622
20623 if (row->y + row->height >= yb)
20624 break;
20625
20626 ++row;
20627 ++row_vpos;
20628 }
20629
20630 /* Find the right column within BEST_ROW. */
20631 lastcol = 0;
20632 current_x = best_row->x;
20633 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
20634 {
20635 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
20636 int charpos = glyph->charpos;
20637
20638 if (BUFFERP (glyph->object))
20639 {
20640 if (charpos == pos)
20641 {
20642 *hpos = i;
20643 *vpos = best_row_vpos;
20644 *x = current_x;
20645 *y = best_row->y;
20646 return 1;
20647 }
20648 else if (charpos > pos)
20649 break;
20650 }
20651 else if (EQ (glyph->object, stop))
20652 break;
20653
20654 if (charpos > 0)
20655 lastcol = i;
20656 current_x += glyph->pixel_width;
20657 }
20658
20659 /* If we're looking for the end of the buffer,
20660 and we didn't find it in the line we scanned,
20661 use the start of the following line. */
20662 if (maybe_next_line_p)
20663 {
20664 ++best_row;
20665 ++best_row_vpos;
20666 lastcol = 0;
20667 current_x = best_row->x;
20668 }
20669
20670 *vpos = best_row_vpos;
20671 *hpos = lastcol + 1;
20672 *x = current_x;
20673 *y = best_row->y;
20674 return 0;
20675 }
20676
20677 #endif /* not 1 */
20678
20679
20680 /* Find the position of the glyph for position POS in OBJECT in
20681 window W's current matrix, and return in *X, *Y the pixel
20682 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
20683
20684 RIGHT_P non-zero means return the position of the right edge of the
20685 glyph, RIGHT_P zero means return the left edge position.
20686
20687 If no glyph for POS exists in the matrix, return the position of
20688 the glyph with the next smaller position that is in the matrix, if
20689 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
20690 exists in the matrix, return the position of the glyph with the
20691 next larger position in OBJECT.
20692
20693 Value is non-zero if a glyph was found. */
20694
20695 static int
20696 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
20697 struct window *w;
20698 EMACS_INT pos;
20699 Lisp_Object object;
20700 int *hpos, *vpos, *x, *y;
20701 int right_p;
20702 {
20703 int yb = window_text_bottom_y (w);
20704 struct glyph_row *r;
20705 struct glyph *best_glyph = NULL;
20706 struct glyph_row *best_row = NULL;
20707 int best_x = 0;
20708
20709 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20710 r->enabled_p && r->y < yb;
20711 ++r)
20712 {
20713 struct glyph *g = r->glyphs[TEXT_AREA];
20714 struct glyph *e = g + r->used[TEXT_AREA];
20715 int gx;
20716
20717 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
20718 if (EQ (g->object, object))
20719 {
20720 if (g->charpos == pos)
20721 {
20722 best_glyph = g;
20723 best_x = gx;
20724 best_row = r;
20725 goto found;
20726 }
20727 else if (best_glyph == NULL
20728 || ((abs (g->charpos - pos)
20729 < abs (best_glyph->charpos - pos))
20730 && (right_p
20731 ? g->charpos < pos
20732 : g->charpos > pos)))
20733 {
20734 best_glyph = g;
20735 best_x = gx;
20736 best_row = r;
20737 }
20738 }
20739 }
20740
20741 found:
20742
20743 if (best_glyph)
20744 {
20745 *x = best_x;
20746 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
20747
20748 if (right_p)
20749 {
20750 *x += best_glyph->pixel_width;
20751 ++*hpos;
20752 }
20753
20754 *y = best_row->y;
20755 *vpos = best_row - w->current_matrix->rows;
20756 }
20757
20758 return best_glyph != NULL;
20759 }
20760
20761
20762 /* See if position X, Y is within a hot-spot of an image. */
20763
20764 static int
20765 on_hot_spot_p (hot_spot, x, y)
20766 Lisp_Object hot_spot;
20767 int x, y;
20768 {
20769 if (!CONSP (hot_spot))
20770 return 0;
20771
20772 if (EQ (XCAR (hot_spot), Qrect))
20773 {
20774 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
20775 Lisp_Object rect = XCDR (hot_spot);
20776 Lisp_Object tem;
20777 if (!CONSP (rect))
20778 return 0;
20779 if (!CONSP (XCAR (rect)))
20780 return 0;
20781 if (!CONSP (XCDR (rect)))
20782 return 0;
20783 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
20784 return 0;
20785 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
20786 return 0;
20787 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
20788 return 0;
20789 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
20790 return 0;
20791 return 1;
20792 }
20793 else if (EQ (XCAR (hot_spot), Qcircle))
20794 {
20795 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
20796 Lisp_Object circ = XCDR (hot_spot);
20797 Lisp_Object lr, lx0, ly0;
20798 if (CONSP (circ)
20799 && CONSP (XCAR (circ))
20800 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
20801 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
20802 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
20803 {
20804 double r = XFLOATINT (lr);
20805 double dx = XINT (lx0) - x;
20806 double dy = XINT (ly0) - y;
20807 return (dx * dx + dy * dy <= r * r);
20808 }
20809 }
20810 else if (EQ (XCAR (hot_spot), Qpoly))
20811 {
20812 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
20813 if (VECTORP (XCDR (hot_spot)))
20814 {
20815 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
20816 Lisp_Object *poly = v->contents;
20817 int n = v->size;
20818 int i;
20819 int inside = 0;
20820 Lisp_Object lx, ly;
20821 int x0, y0;
20822
20823 /* Need an even number of coordinates, and at least 3 edges. */
20824 if (n < 6 || n & 1)
20825 return 0;
20826
20827 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
20828 If count is odd, we are inside polygon. Pixels on edges
20829 may or may not be included depending on actual geometry of the
20830 polygon. */
20831 if ((lx = poly[n-2], !INTEGERP (lx))
20832 || (ly = poly[n-1], !INTEGERP (lx)))
20833 return 0;
20834 x0 = XINT (lx), y0 = XINT (ly);
20835 for (i = 0; i < n; i += 2)
20836 {
20837 int x1 = x0, y1 = y0;
20838 if ((lx = poly[i], !INTEGERP (lx))
20839 || (ly = poly[i+1], !INTEGERP (ly)))
20840 return 0;
20841 x0 = XINT (lx), y0 = XINT (ly);
20842
20843 /* Does this segment cross the X line? */
20844 if (x0 >= x)
20845 {
20846 if (x1 >= x)
20847 continue;
20848 }
20849 else if (x1 < x)
20850 continue;
20851 if (y > y0 && y > y1)
20852 continue;
20853 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
20854 inside = !inside;
20855 }
20856 return inside;
20857 }
20858 }
20859 /* If we don't understand the format, pretend we're not in the hot-spot. */
20860 return 0;
20861 }
20862
20863 Lisp_Object
20864 find_hot_spot (map, x, y)
20865 Lisp_Object map;
20866 int x, y;
20867 {
20868 while (CONSP (map))
20869 {
20870 if (CONSP (XCAR (map))
20871 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
20872 return XCAR (map);
20873 map = XCDR (map);
20874 }
20875
20876 return Qnil;
20877 }
20878
20879 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
20880 3, 3, 0,
20881 doc: /* Lookup in image map MAP coordinates X and Y.
20882 An image map is an alist where each element has the format (AREA ID PLIST).
20883 An AREA is specified as either a rectangle, a circle, or a polygon:
20884 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
20885 pixel coordinates of the upper left and bottom right corners.
20886 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
20887 and the radius of the circle; r may be a float or integer.
20888 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
20889 vector describes one corner in the polygon.
20890 Returns the alist element for the first matching AREA in MAP. */)
20891 (map, x, y)
20892 Lisp_Object map;
20893 Lisp_Object x, y;
20894 {
20895 if (NILP (map))
20896 return Qnil;
20897
20898 CHECK_NUMBER (x);
20899 CHECK_NUMBER (y);
20900
20901 return find_hot_spot (map, XINT (x), XINT (y));
20902 }
20903
20904
20905 /* Display frame CURSOR, optionally using shape defined by POINTER. */
20906 static void
20907 define_frame_cursor1 (f, cursor, pointer)
20908 struct frame *f;
20909 Cursor cursor;
20910 Lisp_Object pointer;
20911 {
20912 if (!NILP (pointer))
20913 {
20914 if (EQ (pointer, Qarrow))
20915 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20916 else if (EQ (pointer, Qhand))
20917 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
20918 else if (EQ (pointer, Qtext))
20919 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20920 else if (EQ (pointer, intern ("hdrag")))
20921 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
20922 #ifdef HAVE_X_WINDOWS
20923 else if (EQ (pointer, intern ("vdrag")))
20924 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
20925 #endif
20926 else if (EQ (pointer, intern ("hourglass")))
20927 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
20928 else if (EQ (pointer, Qmodeline))
20929 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
20930 else
20931 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20932 }
20933
20934 if (cursor != No_Cursor)
20935 rif->define_frame_cursor (f, cursor);
20936 }
20937
20938 /* Take proper action when mouse has moved to the mode or header line
20939 or marginal area AREA of window W, x-position X and y-position Y.
20940 X is relative to the start of the text display area of W, so the
20941 width of bitmap areas and scroll bars must be subtracted to get a
20942 position relative to the start of the mode line. */
20943
20944 static void
20945 note_mode_line_or_margin_highlight (w, x, y, area)
20946 struct window *w;
20947 int x, y;
20948 enum window_part area;
20949 {
20950 struct frame *f = XFRAME (w->frame);
20951 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20952 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20953 Lisp_Object pointer = Qnil;
20954 int charpos, dx, dy, width, height;
20955 Lisp_Object string, object = Qnil;
20956 Lisp_Object pos, help;
20957
20958 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
20959 string = mode_line_string (w, area, &x, &y, &charpos,
20960 &object, &dx, &dy, &width, &height);
20961 else
20962 {
20963 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
20964 string = marginal_area_string (w, area, &x, &y, &charpos,
20965 &object, &dx, &dy, &width, &height);
20966 }
20967
20968 help = Qnil;
20969
20970 if (IMAGEP (object))
20971 {
20972 Lisp_Object image_map, hotspot;
20973 if ((image_map = Fplist_get (XCDR (object), QCmap),
20974 !NILP (image_map))
20975 && (hotspot = find_hot_spot (image_map, dx, dy),
20976 CONSP (hotspot))
20977 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
20978 {
20979 Lisp_Object area_id, plist;
20980
20981 area_id = XCAR (hotspot);
20982 /* Could check AREA_ID to see if we enter/leave this hot-spot.
20983 If so, we could look for mouse-enter, mouse-leave
20984 properties in PLIST (and do something...). */
20985 if ((plist = XCDR (hotspot), CONSP (plist)))
20986 {
20987 pointer = Fplist_get (plist, Qpointer);
20988 if (NILP (pointer))
20989 pointer = Qhand;
20990 help = Fplist_get (plist, Qhelp_echo);
20991 if (!NILP (help))
20992 {
20993 help_echo_string = help;
20994 /* Is this correct? ++kfs */
20995 XSETWINDOW (help_echo_window, w);
20996 help_echo_object = w->buffer;
20997 help_echo_pos = charpos;
20998 }
20999 }
21000 if (NILP (pointer))
21001 pointer = Fplist_get (XCDR (object), QCpointer);
21002 }
21003 }
21004
21005 if (STRINGP (string))
21006 {
21007 pos = make_number (charpos);
21008 /* If we're on a string with `help-echo' text property, arrange
21009 for the help to be displayed. This is done by setting the
21010 global variable help_echo_string to the help string. */
21011 help = Fget_text_property (pos, Qhelp_echo, string);
21012 if (!NILP (help))
21013 {
21014 help_echo_string = help;
21015 XSETWINDOW (help_echo_window, w);
21016 help_echo_object = string;
21017 help_echo_pos = charpos;
21018 }
21019
21020 if (NILP (pointer))
21021 pointer = Fget_text_property (pos, Qpointer, string);
21022
21023 /* Change the mouse pointer according to what is under X/Y. */
21024 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
21025 {
21026 Lisp_Object map;
21027 map = Fget_text_property (pos, Qlocal_map, string);
21028 if (!KEYMAPP (map))
21029 map = Fget_text_property (pos, Qkeymap, string);
21030 if (!KEYMAPP (map))
21031 cursor = dpyinfo->vertical_scroll_bar_cursor;
21032 }
21033 }
21034
21035 define_frame_cursor1 (f, cursor, pointer);
21036 }
21037
21038
21039 /* EXPORT:
21040 Take proper action when the mouse has moved to position X, Y on
21041 frame F as regards highlighting characters that have mouse-face
21042 properties. Also de-highlighting chars where the mouse was before.
21043 X and Y can be negative or out of range. */
21044
21045 void
21046 note_mouse_highlight (f, x, y)
21047 struct frame *f;
21048 int x, y;
21049 {
21050 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21051 enum window_part part;
21052 Lisp_Object window;
21053 struct window *w;
21054 Cursor cursor = No_Cursor;
21055 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
21056 struct buffer *b;
21057
21058 /* When a menu is active, don't highlight because this looks odd. */
21059 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
21060 if (popup_activated ())
21061 return;
21062 #endif
21063
21064 if (NILP (Vmouse_highlight)
21065 || !f->glyphs_initialized_p)
21066 return;
21067
21068 dpyinfo->mouse_face_mouse_x = x;
21069 dpyinfo->mouse_face_mouse_y = y;
21070 dpyinfo->mouse_face_mouse_frame = f;
21071
21072 if (dpyinfo->mouse_face_defer)
21073 return;
21074
21075 if (gc_in_progress)
21076 {
21077 dpyinfo->mouse_face_deferred_gc = 1;
21078 return;
21079 }
21080
21081 /* Which window is that in? */
21082 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
21083
21084 /* If we were displaying active text in another window, clear that. */
21085 if (! EQ (window, dpyinfo->mouse_face_window))
21086 clear_mouse_face (dpyinfo);
21087
21088 /* Not on a window -> return. */
21089 if (!WINDOWP (window))
21090 return;
21091
21092 /* Reset help_echo_string. It will get recomputed below. */
21093 help_echo_string = Qnil;
21094
21095 /* Convert to window-relative pixel coordinates. */
21096 w = XWINDOW (window);
21097 frame_to_window_pixel_xy (w, &x, &y);
21098
21099 /* Handle tool-bar window differently since it doesn't display a
21100 buffer. */
21101 if (EQ (window, f->tool_bar_window))
21102 {
21103 note_tool_bar_highlight (f, x, y);
21104 return;
21105 }
21106
21107 /* Mouse is on the mode, header line or margin? */
21108 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
21109 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
21110 {
21111 note_mode_line_or_margin_highlight (w, x, y, part);
21112 return;
21113 }
21114
21115 if (part == ON_VERTICAL_BORDER)
21116 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21117 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
21118 || part == ON_SCROLL_BAR)
21119 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21120 else
21121 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21122
21123 /* Are we in a window whose display is up to date?
21124 And verify the buffer's text has not changed. */
21125 b = XBUFFER (w->buffer);
21126 if (part == ON_TEXT
21127 && EQ (w->window_end_valid, w->buffer)
21128 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
21129 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
21130 {
21131 int hpos, vpos, pos, i, dx, dy, area;
21132 struct glyph *glyph;
21133 Lisp_Object object;
21134 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
21135 Lisp_Object *overlay_vec = NULL;
21136 int noverlays;
21137 struct buffer *obuf;
21138 int obegv, ozv, same_region;
21139
21140 /* Find the glyph under X/Y. */
21141 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
21142
21143 /* Look for :pointer property on image. */
21144 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21145 {
21146 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21147 if (img != NULL && IMAGEP (img->spec))
21148 {
21149 Lisp_Object image_map, hotspot;
21150 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
21151 !NILP (image_map))
21152 && (hotspot = find_hot_spot (image_map,
21153 glyph->slice.x + dx,
21154 glyph->slice.y + dy),
21155 CONSP (hotspot))
21156 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21157 {
21158 Lisp_Object area_id, plist;
21159
21160 area_id = XCAR (hotspot);
21161 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21162 If so, we could look for mouse-enter, mouse-leave
21163 properties in PLIST (and do something...). */
21164 if ((plist = XCDR (hotspot), CONSP (plist)))
21165 {
21166 pointer = Fplist_get (plist, Qpointer);
21167 if (NILP (pointer))
21168 pointer = Qhand;
21169 help_echo_string = Fplist_get (plist, Qhelp_echo);
21170 if (!NILP (help_echo_string))
21171 {
21172 help_echo_window = window;
21173 help_echo_object = glyph->object;
21174 help_echo_pos = glyph->charpos;
21175 }
21176 }
21177 }
21178 if (NILP (pointer))
21179 pointer = Fplist_get (XCDR (img->spec), QCpointer);
21180 }
21181 }
21182
21183 /* Clear mouse face if X/Y not over text. */
21184 if (glyph == NULL
21185 || area != TEXT_AREA
21186 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
21187 {
21188 if (clear_mouse_face (dpyinfo))
21189 cursor = No_Cursor;
21190 if (NILP (pointer))
21191 {
21192 if (area != TEXT_AREA)
21193 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21194 else
21195 pointer = Vvoid_text_area_pointer;
21196 }
21197 goto set_cursor;
21198 }
21199
21200 pos = glyph->charpos;
21201 object = glyph->object;
21202 if (!STRINGP (object) && !BUFFERP (object))
21203 goto set_cursor;
21204
21205 /* If we get an out-of-range value, return now; avoid an error. */
21206 if (BUFFERP (object) && pos > BUF_Z (b))
21207 goto set_cursor;
21208
21209 /* Make the window's buffer temporarily current for
21210 overlays_at and compute_char_face. */
21211 obuf = current_buffer;
21212 current_buffer = b;
21213 obegv = BEGV;
21214 ozv = ZV;
21215 BEGV = BEG;
21216 ZV = Z;
21217
21218 /* Is this char mouse-active or does it have help-echo? */
21219 position = make_number (pos);
21220
21221 if (BUFFERP (object))
21222 {
21223 /* Put all the overlays we want in a vector in overlay_vec. */
21224 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
21225 /* Sort overlays into increasing priority order. */
21226 noverlays = sort_overlays (overlay_vec, noverlays, w);
21227 }
21228 else
21229 noverlays = 0;
21230
21231 same_region = (EQ (window, dpyinfo->mouse_face_window)
21232 && vpos >= dpyinfo->mouse_face_beg_row
21233 && vpos <= dpyinfo->mouse_face_end_row
21234 && (vpos > dpyinfo->mouse_face_beg_row
21235 || hpos >= dpyinfo->mouse_face_beg_col)
21236 && (vpos < dpyinfo->mouse_face_end_row
21237 || hpos < dpyinfo->mouse_face_end_col
21238 || dpyinfo->mouse_face_past_end));
21239
21240 if (same_region)
21241 cursor = No_Cursor;
21242
21243 /* Check mouse-face highlighting. */
21244 if (! same_region
21245 /* If there exists an overlay with mouse-face overlapping
21246 the one we are currently highlighting, we have to
21247 check if we enter the overlapping overlay, and then
21248 highlight only that. */
21249 || (OVERLAYP (dpyinfo->mouse_face_overlay)
21250 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
21251 {
21252 /* Find the highest priority overlay that has a mouse-face
21253 property. */
21254 overlay = Qnil;
21255 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
21256 {
21257 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
21258 if (!NILP (mouse_face))
21259 overlay = overlay_vec[i];
21260 }
21261
21262 /* If we're actually highlighting the same overlay as
21263 before, there's no need to do that again. */
21264 if (!NILP (overlay)
21265 && EQ (overlay, dpyinfo->mouse_face_overlay))
21266 goto check_help_echo;
21267
21268 dpyinfo->mouse_face_overlay = overlay;
21269
21270 /* Clear the display of the old active region, if any. */
21271 if (clear_mouse_face (dpyinfo))
21272 cursor = No_Cursor;
21273
21274 /* If no overlay applies, get a text property. */
21275 if (NILP (overlay))
21276 mouse_face = Fget_text_property (position, Qmouse_face, object);
21277
21278 /* Handle the overlay case. */
21279 if (!NILP (overlay))
21280 {
21281 /* Find the range of text around this char that
21282 should be active. */
21283 Lisp_Object before, after;
21284 int ignore;
21285
21286 before = Foverlay_start (overlay);
21287 after = Foverlay_end (overlay);
21288 /* Record this as the current active region. */
21289 fast_find_position (w, XFASTINT (before),
21290 &dpyinfo->mouse_face_beg_col,
21291 &dpyinfo->mouse_face_beg_row,
21292 &dpyinfo->mouse_face_beg_x,
21293 &dpyinfo->mouse_face_beg_y, Qnil);
21294
21295 dpyinfo->mouse_face_past_end
21296 = !fast_find_position (w, XFASTINT (after),
21297 &dpyinfo->mouse_face_end_col,
21298 &dpyinfo->mouse_face_end_row,
21299 &dpyinfo->mouse_face_end_x,
21300 &dpyinfo->mouse_face_end_y, Qnil);
21301 dpyinfo->mouse_face_window = window;
21302
21303 dpyinfo->mouse_face_face_id
21304 = face_at_buffer_position (w, pos, 0, 0,
21305 &ignore, pos + 1,
21306 !dpyinfo->mouse_face_hidden);
21307
21308 /* Display it as active. */
21309 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21310 cursor = No_Cursor;
21311 }
21312 /* Handle the text property case. */
21313 else if (!NILP (mouse_face) && BUFFERP (object))
21314 {
21315 /* Find the range of text around this char that
21316 should be active. */
21317 Lisp_Object before, after, beginning, end;
21318 int ignore;
21319
21320 beginning = Fmarker_position (w->start);
21321 end = make_number (BUF_Z (XBUFFER (object))
21322 - XFASTINT (w->window_end_pos));
21323 before
21324 = Fprevious_single_property_change (make_number (pos + 1),
21325 Qmouse_face,
21326 object, beginning);
21327 after
21328 = Fnext_single_property_change (position, Qmouse_face,
21329 object, end);
21330
21331 /* Record this as the current active region. */
21332 fast_find_position (w, XFASTINT (before),
21333 &dpyinfo->mouse_face_beg_col,
21334 &dpyinfo->mouse_face_beg_row,
21335 &dpyinfo->mouse_face_beg_x,
21336 &dpyinfo->mouse_face_beg_y, Qnil);
21337 dpyinfo->mouse_face_past_end
21338 = !fast_find_position (w, XFASTINT (after),
21339 &dpyinfo->mouse_face_end_col,
21340 &dpyinfo->mouse_face_end_row,
21341 &dpyinfo->mouse_face_end_x,
21342 &dpyinfo->mouse_face_end_y, Qnil);
21343 dpyinfo->mouse_face_window = window;
21344
21345 if (BUFFERP (object))
21346 dpyinfo->mouse_face_face_id
21347 = face_at_buffer_position (w, pos, 0, 0,
21348 &ignore, pos + 1,
21349 !dpyinfo->mouse_face_hidden);
21350
21351 /* Display it as active. */
21352 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21353 cursor = No_Cursor;
21354 }
21355 else if (!NILP (mouse_face) && STRINGP (object))
21356 {
21357 Lisp_Object b, e;
21358 int ignore;
21359
21360 b = Fprevious_single_property_change (make_number (pos + 1),
21361 Qmouse_face,
21362 object, Qnil);
21363 e = Fnext_single_property_change (position, Qmouse_face,
21364 object, Qnil);
21365 if (NILP (b))
21366 b = make_number (0);
21367 if (NILP (e))
21368 e = make_number (SCHARS (object) - 1);
21369 fast_find_string_pos (w, XINT (b), object,
21370 &dpyinfo->mouse_face_beg_col,
21371 &dpyinfo->mouse_face_beg_row,
21372 &dpyinfo->mouse_face_beg_x,
21373 &dpyinfo->mouse_face_beg_y, 0);
21374 fast_find_string_pos (w, XINT (e), object,
21375 &dpyinfo->mouse_face_end_col,
21376 &dpyinfo->mouse_face_end_row,
21377 &dpyinfo->mouse_face_end_x,
21378 &dpyinfo->mouse_face_end_y, 1);
21379 dpyinfo->mouse_face_past_end = 0;
21380 dpyinfo->mouse_face_window = window;
21381 dpyinfo->mouse_face_face_id
21382 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
21383 glyph->face_id, 1);
21384 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21385 cursor = No_Cursor;
21386 }
21387 else if (STRINGP (object) && NILP (mouse_face))
21388 {
21389 /* A string which doesn't have mouse-face, but
21390 the text ``under'' it might have. */
21391 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
21392 int start = MATRIX_ROW_START_CHARPOS (r);
21393
21394 pos = string_buffer_position (w, object, start);
21395 if (pos > 0)
21396 mouse_face = get_char_property_and_overlay (make_number (pos),
21397 Qmouse_face,
21398 w->buffer,
21399 &overlay);
21400 if (!NILP (mouse_face) && !NILP (overlay))
21401 {
21402 Lisp_Object before = Foverlay_start (overlay);
21403 Lisp_Object after = Foverlay_end (overlay);
21404 int ignore;
21405
21406 /* Note that we might not be able to find position
21407 BEFORE in the glyph matrix if the overlay is
21408 entirely covered by a `display' property. In
21409 this case, we overshoot. So let's stop in
21410 the glyph matrix before glyphs for OBJECT. */
21411 fast_find_position (w, XFASTINT (before),
21412 &dpyinfo->mouse_face_beg_col,
21413 &dpyinfo->mouse_face_beg_row,
21414 &dpyinfo->mouse_face_beg_x,
21415 &dpyinfo->mouse_face_beg_y,
21416 object);
21417
21418 dpyinfo->mouse_face_past_end
21419 = !fast_find_position (w, XFASTINT (after),
21420 &dpyinfo->mouse_face_end_col,
21421 &dpyinfo->mouse_face_end_row,
21422 &dpyinfo->mouse_face_end_x,
21423 &dpyinfo->mouse_face_end_y,
21424 Qnil);
21425 dpyinfo->mouse_face_window = window;
21426 dpyinfo->mouse_face_face_id
21427 = face_at_buffer_position (w, pos, 0, 0,
21428 &ignore, pos + 1,
21429 !dpyinfo->mouse_face_hidden);
21430
21431 /* Display it as active. */
21432 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21433 cursor = No_Cursor;
21434 }
21435 }
21436 }
21437
21438 check_help_echo:
21439
21440 /* Look for a `help-echo' property. */
21441 if (NILP (help_echo_string)) {
21442 Lisp_Object help, overlay;
21443
21444 /* Check overlays first. */
21445 help = overlay = Qnil;
21446 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
21447 {
21448 overlay = overlay_vec[i];
21449 help = Foverlay_get (overlay, Qhelp_echo);
21450 }
21451
21452 if (!NILP (help))
21453 {
21454 help_echo_string = help;
21455 help_echo_window = window;
21456 help_echo_object = overlay;
21457 help_echo_pos = pos;
21458 }
21459 else
21460 {
21461 Lisp_Object object = glyph->object;
21462 int charpos = glyph->charpos;
21463
21464 /* Try text properties. */
21465 if (STRINGP (object)
21466 && charpos >= 0
21467 && charpos < SCHARS (object))
21468 {
21469 help = Fget_text_property (make_number (charpos),
21470 Qhelp_echo, object);
21471 if (NILP (help))
21472 {
21473 /* If the string itself doesn't specify a help-echo,
21474 see if the buffer text ``under'' it does. */
21475 struct glyph_row *r
21476 = MATRIX_ROW (w->current_matrix, vpos);
21477 int start = MATRIX_ROW_START_CHARPOS (r);
21478 int pos = string_buffer_position (w, object, start);
21479 if (pos > 0)
21480 {
21481 help = Fget_char_property (make_number (pos),
21482 Qhelp_echo, w->buffer);
21483 if (!NILP (help))
21484 {
21485 charpos = pos;
21486 object = w->buffer;
21487 }
21488 }
21489 }
21490 }
21491 else if (BUFFERP (object)
21492 && charpos >= BEGV
21493 && charpos < ZV)
21494 help = Fget_text_property (make_number (charpos), Qhelp_echo,
21495 object);
21496
21497 if (!NILP (help))
21498 {
21499 help_echo_string = help;
21500 help_echo_window = window;
21501 help_echo_object = object;
21502 help_echo_pos = charpos;
21503 }
21504 }
21505 }
21506
21507 /* Look for a `pointer' property. */
21508 if (NILP (pointer))
21509 {
21510 /* Check overlays first. */
21511 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
21512 pointer = Foverlay_get (overlay_vec[i], Qpointer);
21513
21514 if (NILP (pointer))
21515 {
21516 Lisp_Object object = glyph->object;
21517 int charpos = glyph->charpos;
21518
21519 /* Try text properties. */
21520 if (STRINGP (object)
21521 && charpos >= 0
21522 && charpos < SCHARS (object))
21523 {
21524 pointer = Fget_text_property (make_number (charpos),
21525 Qpointer, object);
21526 if (NILP (pointer))
21527 {
21528 /* If the string itself doesn't specify a pointer,
21529 see if the buffer text ``under'' it does. */
21530 struct glyph_row *r
21531 = MATRIX_ROW (w->current_matrix, vpos);
21532 int start = MATRIX_ROW_START_CHARPOS (r);
21533 int pos = string_buffer_position (w, object, start);
21534 if (pos > 0)
21535 pointer = Fget_char_property (make_number (pos),
21536 Qpointer, w->buffer);
21537 }
21538 }
21539 else if (BUFFERP (object)
21540 && charpos >= BEGV
21541 && charpos < ZV)
21542 pointer = Fget_text_property (make_number (charpos),
21543 Qpointer, object);
21544 }
21545 }
21546
21547 BEGV = obegv;
21548 ZV = ozv;
21549 current_buffer = obuf;
21550 }
21551
21552 set_cursor:
21553
21554 define_frame_cursor1 (f, cursor, pointer);
21555 }
21556
21557
21558 /* EXPORT for RIF:
21559 Clear any mouse-face on window W. This function is part of the
21560 redisplay interface, and is called from try_window_id and similar
21561 functions to ensure the mouse-highlight is off. */
21562
21563 void
21564 x_clear_window_mouse_face (w)
21565 struct window *w;
21566 {
21567 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21568 Lisp_Object window;
21569
21570 BLOCK_INPUT;
21571 XSETWINDOW (window, w);
21572 if (EQ (window, dpyinfo->mouse_face_window))
21573 clear_mouse_face (dpyinfo);
21574 UNBLOCK_INPUT;
21575 }
21576
21577
21578 /* EXPORT:
21579 Just discard the mouse face information for frame F, if any.
21580 This is used when the size of F is changed. */
21581
21582 void
21583 cancel_mouse_face (f)
21584 struct frame *f;
21585 {
21586 Lisp_Object window;
21587 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21588
21589 window = dpyinfo->mouse_face_window;
21590 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
21591 {
21592 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21593 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21594 dpyinfo->mouse_face_window = Qnil;
21595 }
21596 }
21597
21598
21599 #endif /* HAVE_WINDOW_SYSTEM */
21600
21601 \f
21602 /***********************************************************************
21603 Exposure Events
21604 ***********************************************************************/
21605
21606 #ifdef HAVE_WINDOW_SYSTEM
21607
21608 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
21609 which intersects rectangle R. R is in window-relative coordinates. */
21610
21611 static void
21612 expose_area (w, row, r, area)
21613 struct window *w;
21614 struct glyph_row *row;
21615 XRectangle *r;
21616 enum glyph_row_area area;
21617 {
21618 struct glyph *first = row->glyphs[area];
21619 struct glyph *end = row->glyphs[area] + row->used[area];
21620 struct glyph *last;
21621 int first_x, start_x, x;
21622
21623 if (area == TEXT_AREA && row->fill_line_p)
21624 /* If row extends face to end of line write the whole line. */
21625 draw_glyphs (w, 0, row, area,
21626 0, row->used[area],
21627 DRAW_NORMAL_TEXT, 0);
21628 else
21629 {
21630 /* Set START_X to the window-relative start position for drawing glyphs of
21631 AREA. The first glyph of the text area can be partially visible.
21632 The first glyphs of other areas cannot. */
21633 start_x = window_box_left_offset (w, area);
21634 x = start_x;
21635 if (area == TEXT_AREA)
21636 x += row->x;
21637
21638 /* Find the first glyph that must be redrawn. */
21639 while (first < end
21640 && x + first->pixel_width < r->x)
21641 {
21642 x += first->pixel_width;
21643 ++first;
21644 }
21645
21646 /* Find the last one. */
21647 last = first;
21648 first_x = x;
21649 while (last < end
21650 && x < r->x + r->width)
21651 {
21652 x += last->pixel_width;
21653 ++last;
21654 }
21655
21656 /* Repaint. */
21657 if (last > first)
21658 draw_glyphs (w, first_x - start_x, row, area,
21659 first - row->glyphs[area], last - row->glyphs[area],
21660 DRAW_NORMAL_TEXT, 0);
21661 }
21662 }
21663
21664
21665 /* Redraw the parts of the glyph row ROW on window W intersecting
21666 rectangle R. R is in window-relative coordinates. Value is
21667 non-zero if mouse-face was overwritten. */
21668
21669 static int
21670 expose_line (w, row, r)
21671 struct window *w;
21672 struct glyph_row *row;
21673 XRectangle *r;
21674 {
21675 xassert (row->enabled_p);
21676
21677 if (row->mode_line_p || w->pseudo_window_p)
21678 draw_glyphs (w, 0, row, TEXT_AREA,
21679 0, row->used[TEXT_AREA],
21680 DRAW_NORMAL_TEXT, 0);
21681 else
21682 {
21683 if (row->used[LEFT_MARGIN_AREA])
21684 expose_area (w, row, r, LEFT_MARGIN_AREA);
21685 if (row->used[TEXT_AREA])
21686 expose_area (w, row, r, TEXT_AREA);
21687 if (row->used[RIGHT_MARGIN_AREA])
21688 expose_area (w, row, r, RIGHT_MARGIN_AREA);
21689 draw_row_fringe_bitmaps (w, row);
21690 }
21691
21692 return row->mouse_face_p;
21693 }
21694
21695
21696 /* Redraw those parts of glyphs rows during expose event handling that
21697 overlap other rows. Redrawing of an exposed line writes over parts
21698 of lines overlapping that exposed line; this function fixes that.
21699
21700 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
21701 row in W's current matrix that is exposed and overlaps other rows.
21702 LAST_OVERLAPPING_ROW is the last such row. */
21703
21704 static void
21705 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
21706 struct window *w;
21707 struct glyph_row *first_overlapping_row;
21708 struct glyph_row *last_overlapping_row;
21709 {
21710 struct glyph_row *row;
21711
21712 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
21713 if (row->overlapping_p)
21714 {
21715 xassert (row->enabled_p && !row->mode_line_p);
21716
21717 if (row->used[LEFT_MARGIN_AREA])
21718 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
21719
21720 if (row->used[TEXT_AREA])
21721 x_fix_overlapping_area (w, row, TEXT_AREA);
21722
21723 if (row->used[RIGHT_MARGIN_AREA])
21724 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
21725 }
21726 }
21727
21728
21729 /* Return non-zero if W's cursor intersects rectangle R. */
21730
21731 static int
21732 phys_cursor_in_rect_p (w, r)
21733 struct window *w;
21734 XRectangle *r;
21735 {
21736 XRectangle cr, result;
21737 struct glyph *cursor_glyph;
21738
21739 cursor_glyph = get_phys_cursor_glyph (w);
21740 if (cursor_glyph)
21741 {
21742 /* r is relative to W's box, but w->phys_cursor.x is relative
21743 to left edge of W's TEXT area. Adjust it. */
21744 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
21745 cr.y = w->phys_cursor.y;
21746 cr.width = cursor_glyph->pixel_width;
21747 cr.height = w->phys_cursor_height;
21748 /* ++KFS: W32 version used W32-specific IntersectRect here, but
21749 I assume the effect is the same -- and this is portable. */
21750 return x_intersect_rectangles (&cr, r, &result);
21751 }
21752 else
21753 return 0;
21754 }
21755
21756
21757 /* EXPORT:
21758 Draw a vertical window border to the right of window W if W doesn't
21759 have vertical scroll bars. */
21760
21761 void
21762 x_draw_vertical_border (w)
21763 struct window *w;
21764 {
21765 /* We could do better, if we knew what type of scroll-bar the adjacent
21766 windows (on either side) have... But we don't :-(
21767 However, I think this works ok. ++KFS 2003-04-25 */
21768
21769 /* Redraw borders between horizontally adjacent windows. Don't
21770 do it for frames with vertical scroll bars because either the
21771 right scroll bar of a window, or the left scroll bar of its
21772 neighbor will suffice as a border. */
21773 if (!WINDOW_RIGHTMOST_P (w)
21774 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
21775 {
21776 int x0, x1, y0, y1;
21777
21778 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21779 y1 -= 1;
21780
21781 rif->draw_vertical_window_border (w, x1, y0, y1);
21782 }
21783 else if (!WINDOW_LEFTMOST_P (w)
21784 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
21785 {
21786 int x0, x1, y0, y1;
21787
21788 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21789 y1 -= 1;
21790
21791 rif->draw_vertical_window_border (w, x0, y0, y1);
21792 }
21793 }
21794
21795
21796 /* Redraw the part of window W intersection rectangle FR. Pixel
21797 coordinates in FR are frame-relative. Call this function with
21798 input blocked. Value is non-zero if the exposure overwrites
21799 mouse-face. */
21800
21801 static int
21802 expose_window (w, fr)
21803 struct window *w;
21804 XRectangle *fr;
21805 {
21806 struct frame *f = XFRAME (w->frame);
21807 XRectangle wr, r;
21808 int mouse_face_overwritten_p = 0;
21809
21810 /* If window is not yet fully initialized, do nothing. This can
21811 happen when toolkit scroll bars are used and a window is split.
21812 Reconfiguring the scroll bar will generate an expose for a newly
21813 created window. */
21814 if (w->current_matrix == NULL)
21815 return 0;
21816
21817 /* When we're currently updating the window, display and current
21818 matrix usually don't agree. Arrange for a thorough display
21819 later. */
21820 if (w == updated_window)
21821 {
21822 SET_FRAME_GARBAGED (f);
21823 return 0;
21824 }
21825
21826 /* Frame-relative pixel rectangle of W. */
21827 wr.x = WINDOW_LEFT_EDGE_X (w);
21828 wr.y = WINDOW_TOP_EDGE_Y (w);
21829 wr.width = WINDOW_TOTAL_WIDTH (w);
21830 wr.height = WINDOW_TOTAL_HEIGHT (w);
21831
21832 if (x_intersect_rectangles (fr, &wr, &r))
21833 {
21834 int yb = window_text_bottom_y (w);
21835 struct glyph_row *row;
21836 int cursor_cleared_p;
21837 struct glyph_row *first_overlapping_row, *last_overlapping_row;
21838
21839 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
21840 r.x, r.y, r.width, r.height));
21841
21842 /* Convert to window coordinates. */
21843 r.x -= WINDOW_LEFT_EDGE_X (w);
21844 r.y -= WINDOW_TOP_EDGE_Y (w);
21845
21846 /* Turn off the cursor. */
21847 if (!w->pseudo_window_p
21848 && phys_cursor_in_rect_p (w, &r))
21849 {
21850 x_clear_cursor (w);
21851 cursor_cleared_p = 1;
21852 }
21853 else
21854 cursor_cleared_p = 0;
21855
21856 /* Update lines intersecting rectangle R. */
21857 first_overlapping_row = last_overlapping_row = NULL;
21858 for (row = w->current_matrix->rows;
21859 row->enabled_p;
21860 ++row)
21861 {
21862 int y0 = row->y;
21863 int y1 = MATRIX_ROW_BOTTOM_Y (row);
21864
21865 if ((y0 >= r.y && y0 < r.y + r.height)
21866 || (y1 > r.y && y1 < r.y + r.height)
21867 || (r.y >= y0 && r.y < y1)
21868 || (r.y + r.height > y0 && r.y + r.height < y1))
21869 {
21870 if (row->overlapping_p)
21871 {
21872 if (first_overlapping_row == NULL)
21873 first_overlapping_row = row;
21874 last_overlapping_row = row;
21875 }
21876
21877 if (expose_line (w, row, &r))
21878 mouse_face_overwritten_p = 1;
21879 }
21880
21881 if (y1 >= yb)
21882 break;
21883 }
21884
21885 /* Display the mode line if there is one. */
21886 if (WINDOW_WANTS_MODELINE_P (w)
21887 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
21888 row->enabled_p)
21889 && row->y < r.y + r.height)
21890 {
21891 if (expose_line (w, row, &r))
21892 mouse_face_overwritten_p = 1;
21893 }
21894
21895 if (!w->pseudo_window_p)
21896 {
21897 /* Fix the display of overlapping rows. */
21898 if (first_overlapping_row)
21899 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
21900
21901 /* Draw border between windows. */
21902 x_draw_vertical_border (w);
21903
21904 /* Turn the cursor on again. */
21905 if (cursor_cleared_p)
21906 update_window_cursor (w, 1);
21907 }
21908 }
21909
21910 #ifdef HAVE_CARBON
21911 /* Display scroll bar for this window. */
21912 if (!NILP (w->vertical_scroll_bar))
21913 {
21914 /* ++KFS:
21915 If this doesn't work here (maybe some header files are missing),
21916 make a function in macterm.c and call it to do the job! */
21917 ControlHandle ch
21918 = SCROLL_BAR_CONTROL_HANDLE (XSCROLL_BAR (w->vertical_scroll_bar));
21919
21920 Draw1Control (ch);
21921 }
21922 #endif
21923
21924 return mouse_face_overwritten_p;
21925 }
21926
21927
21928
21929 /* Redraw (parts) of all windows in the window tree rooted at W that
21930 intersect R. R contains frame pixel coordinates. Value is
21931 non-zero if the exposure overwrites mouse-face. */
21932
21933 static int
21934 expose_window_tree (w, r)
21935 struct window *w;
21936 XRectangle *r;
21937 {
21938 struct frame *f = XFRAME (w->frame);
21939 int mouse_face_overwritten_p = 0;
21940
21941 while (w && !FRAME_GARBAGED_P (f))
21942 {
21943 if (!NILP (w->hchild))
21944 mouse_face_overwritten_p
21945 |= expose_window_tree (XWINDOW (w->hchild), r);
21946 else if (!NILP (w->vchild))
21947 mouse_face_overwritten_p
21948 |= expose_window_tree (XWINDOW (w->vchild), r);
21949 else
21950 mouse_face_overwritten_p |= expose_window (w, r);
21951
21952 w = NILP (w->next) ? NULL : XWINDOW (w->next);
21953 }
21954
21955 return mouse_face_overwritten_p;
21956 }
21957
21958
21959 /* EXPORT:
21960 Redisplay an exposed area of frame F. X and Y are the upper-left
21961 corner of the exposed rectangle. W and H are width and height of
21962 the exposed area. All are pixel values. W or H zero means redraw
21963 the entire frame. */
21964
21965 void
21966 expose_frame (f, x, y, w, h)
21967 struct frame *f;
21968 int x, y, w, h;
21969 {
21970 XRectangle r;
21971 int mouse_face_overwritten_p = 0;
21972
21973 TRACE ((stderr, "expose_frame "));
21974
21975 /* No need to redraw if frame will be redrawn soon. */
21976 if (FRAME_GARBAGED_P (f))
21977 {
21978 TRACE ((stderr, " garbaged\n"));
21979 return;
21980 }
21981
21982 #ifdef HAVE_CARBON
21983 /* MAC_TODO: this is a kludge, but if scroll bars are not activated
21984 or deactivated here, for unknown reasons, activated scroll bars
21985 are shown in deactivated frames in some instances. */
21986 if (f == FRAME_MAC_DISPLAY_INFO (f)->x_focus_frame)
21987 activate_scroll_bars (f);
21988 else
21989 deactivate_scroll_bars (f);
21990 #endif
21991
21992 /* If basic faces haven't been realized yet, there is no point in
21993 trying to redraw anything. This can happen when we get an expose
21994 event while Emacs is starting, e.g. by moving another window. */
21995 if (FRAME_FACE_CACHE (f) == NULL
21996 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
21997 {
21998 TRACE ((stderr, " no faces\n"));
21999 return;
22000 }
22001
22002 if (w == 0 || h == 0)
22003 {
22004 r.x = r.y = 0;
22005 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
22006 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
22007 }
22008 else
22009 {
22010 r.x = x;
22011 r.y = y;
22012 r.width = w;
22013 r.height = h;
22014 }
22015
22016 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
22017 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
22018
22019 if (WINDOWP (f->tool_bar_window))
22020 mouse_face_overwritten_p
22021 |= expose_window (XWINDOW (f->tool_bar_window), &r);
22022
22023 #ifdef HAVE_X_WINDOWS
22024 #ifndef MSDOS
22025 #ifndef USE_X_TOOLKIT
22026 if (WINDOWP (f->menu_bar_window))
22027 mouse_face_overwritten_p
22028 |= expose_window (XWINDOW (f->menu_bar_window), &r);
22029 #endif /* not USE_X_TOOLKIT */
22030 #endif
22031 #endif
22032
22033 /* Some window managers support a focus-follows-mouse style with
22034 delayed raising of frames. Imagine a partially obscured frame,
22035 and moving the mouse into partially obscured mouse-face on that
22036 frame. The visible part of the mouse-face will be highlighted,
22037 then the WM raises the obscured frame. With at least one WM, KDE
22038 2.1, Emacs is not getting any event for the raising of the frame
22039 (even tried with SubstructureRedirectMask), only Expose events.
22040 These expose events will draw text normally, i.e. not
22041 highlighted. Which means we must redo the highlight here.
22042 Subsume it under ``we love X''. --gerd 2001-08-15 */
22043 /* Included in Windows version because Windows most likely does not
22044 do the right thing if any third party tool offers
22045 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
22046 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
22047 {
22048 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22049 if (f == dpyinfo->mouse_face_mouse_frame)
22050 {
22051 int x = dpyinfo->mouse_face_mouse_x;
22052 int y = dpyinfo->mouse_face_mouse_y;
22053 clear_mouse_face (dpyinfo);
22054 note_mouse_highlight (f, x, y);
22055 }
22056 }
22057 }
22058
22059
22060 /* EXPORT:
22061 Determine the intersection of two rectangles R1 and R2. Return
22062 the intersection in *RESULT. Value is non-zero if RESULT is not
22063 empty. */
22064
22065 int
22066 x_intersect_rectangles (r1, r2, result)
22067 XRectangle *r1, *r2, *result;
22068 {
22069 XRectangle *left, *right;
22070 XRectangle *upper, *lower;
22071 int intersection_p = 0;
22072
22073 /* Rearrange so that R1 is the left-most rectangle. */
22074 if (r1->x < r2->x)
22075 left = r1, right = r2;
22076 else
22077 left = r2, right = r1;
22078
22079 /* X0 of the intersection is right.x0, if this is inside R1,
22080 otherwise there is no intersection. */
22081 if (right->x <= left->x + left->width)
22082 {
22083 result->x = right->x;
22084
22085 /* The right end of the intersection is the minimum of the
22086 the right ends of left and right. */
22087 result->width = (min (left->x + left->width, right->x + right->width)
22088 - result->x);
22089
22090 /* Same game for Y. */
22091 if (r1->y < r2->y)
22092 upper = r1, lower = r2;
22093 else
22094 upper = r2, lower = r1;
22095
22096 /* The upper end of the intersection is lower.y0, if this is inside
22097 of upper. Otherwise, there is no intersection. */
22098 if (lower->y <= upper->y + upper->height)
22099 {
22100 result->y = lower->y;
22101
22102 /* The lower end of the intersection is the minimum of the lower
22103 ends of upper and lower. */
22104 result->height = (min (lower->y + lower->height,
22105 upper->y + upper->height)
22106 - result->y);
22107 intersection_p = 1;
22108 }
22109 }
22110
22111 return intersection_p;
22112 }
22113
22114 #endif /* HAVE_WINDOW_SYSTEM */
22115
22116 \f
22117 /***********************************************************************
22118 Initialization
22119 ***********************************************************************/
22120
22121 void
22122 syms_of_xdisp ()
22123 {
22124 Vwith_echo_area_save_vector = Qnil;
22125 staticpro (&Vwith_echo_area_save_vector);
22126
22127 Vmessage_stack = Qnil;
22128 staticpro (&Vmessage_stack);
22129
22130 Qinhibit_redisplay = intern ("inhibit-redisplay");
22131 staticpro (&Qinhibit_redisplay);
22132
22133 message_dolog_marker1 = Fmake_marker ();
22134 staticpro (&message_dolog_marker1);
22135 message_dolog_marker2 = Fmake_marker ();
22136 staticpro (&message_dolog_marker2);
22137 message_dolog_marker3 = Fmake_marker ();
22138 staticpro (&message_dolog_marker3);
22139
22140 #if GLYPH_DEBUG
22141 defsubr (&Sdump_frame_glyph_matrix);
22142 defsubr (&Sdump_glyph_matrix);
22143 defsubr (&Sdump_glyph_row);
22144 defsubr (&Sdump_tool_bar_row);
22145 defsubr (&Strace_redisplay);
22146 defsubr (&Strace_to_stderr);
22147 #endif
22148 #ifdef HAVE_WINDOW_SYSTEM
22149 defsubr (&Stool_bar_lines_needed);
22150 defsubr (&Slookup_image_map);
22151 #endif
22152 defsubr (&Sformat_mode_line);
22153
22154 staticpro (&Qmenu_bar_update_hook);
22155 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
22156
22157 staticpro (&Qoverriding_terminal_local_map);
22158 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
22159
22160 staticpro (&Qoverriding_local_map);
22161 Qoverriding_local_map = intern ("overriding-local-map");
22162
22163 staticpro (&Qwindow_scroll_functions);
22164 Qwindow_scroll_functions = intern ("window-scroll-functions");
22165
22166 staticpro (&Qredisplay_end_trigger_functions);
22167 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
22168
22169 staticpro (&Qinhibit_point_motion_hooks);
22170 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
22171
22172 QCdata = intern (":data");
22173 staticpro (&QCdata);
22174 Qdisplay = intern ("display");
22175 staticpro (&Qdisplay);
22176 Qspace_width = intern ("space-width");
22177 staticpro (&Qspace_width);
22178 Qraise = intern ("raise");
22179 staticpro (&Qraise);
22180 Qslice = intern ("slice");
22181 staticpro (&Qslice);
22182 Qspace = intern ("space");
22183 staticpro (&Qspace);
22184 Qmargin = intern ("margin");
22185 staticpro (&Qmargin);
22186 Qpointer = intern ("pointer");
22187 staticpro (&Qpointer);
22188 Qleft_margin = intern ("left-margin");
22189 staticpro (&Qleft_margin);
22190 Qright_margin = intern ("right-margin");
22191 staticpro (&Qright_margin);
22192 Qcenter = intern ("center");
22193 staticpro (&Qcenter);
22194 Qline_height = intern ("line-height");
22195 staticpro (&Qline_height);
22196 Qtotal = intern ("total");
22197 staticpro (&Qtotal);
22198 QCalign_to = intern (":align-to");
22199 staticpro (&QCalign_to);
22200 QCrelative_width = intern (":relative-width");
22201 staticpro (&QCrelative_width);
22202 QCrelative_height = intern (":relative-height");
22203 staticpro (&QCrelative_height);
22204 QCeval = intern (":eval");
22205 staticpro (&QCeval);
22206 QCpropertize = intern (":propertize");
22207 staticpro (&QCpropertize);
22208 QCfile = intern (":file");
22209 staticpro (&QCfile);
22210 Qfontified = intern ("fontified");
22211 staticpro (&Qfontified);
22212 Qfontification_functions = intern ("fontification-functions");
22213 staticpro (&Qfontification_functions);
22214 Qtrailing_whitespace = intern ("trailing-whitespace");
22215 staticpro (&Qtrailing_whitespace);
22216 Qimage = intern ("image");
22217 staticpro (&Qimage);
22218 QCmap = intern (":map");
22219 staticpro (&QCmap);
22220 QCpointer = intern (":pointer");
22221 staticpro (&QCpointer);
22222 Qrect = intern ("rect");
22223 staticpro (&Qrect);
22224 Qcircle = intern ("circle");
22225 staticpro (&Qcircle);
22226 Qpoly = intern ("poly");
22227 staticpro (&Qpoly);
22228 Qmessage_truncate_lines = intern ("message-truncate-lines");
22229 staticpro (&Qmessage_truncate_lines);
22230 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
22231 staticpro (&Qcursor_in_non_selected_windows);
22232 Qgrow_only = intern ("grow-only");
22233 staticpro (&Qgrow_only);
22234 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
22235 staticpro (&Qinhibit_menubar_update);
22236 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
22237 staticpro (&Qinhibit_eval_during_redisplay);
22238 Qposition = intern ("position");
22239 staticpro (&Qposition);
22240 Qbuffer_position = intern ("buffer-position");
22241 staticpro (&Qbuffer_position);
22242 Qobject = intern ("object");
22243 staticpro (&Qobject);
22244 Qbar = intern ("bar");
22245 staticpro (&Qbar);
22246 Qhbar = intern ("hbar");
22247 staticpro (&Qhbar);
22248 Qbox = intern ("box");
22249 staticpro (&Qbox);
22250 Qhollow = intern ("hollow");
22251 staticpro (&Qhollow);
22252 Qhand = intern ("hand");
22253 staticpro (&Qhand);
22254 Qarrow = intern ("arrow");
22255 staticpro (&Qarrow);
22256 Qtext = intern ("text");
22257 staticpro (&Qtext);
22258 Qrisky_local_variable = intern ("risky-local-variable");
22259 staticpro (&Qrisky_local_variable);
22260 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
22261 staticpro (&Qinhibit_free_realized_faces);
22262
22263 list_of_error = Fcons (Fcons (intern ("error"),
22264 Fcons (intern ("void-variable"), Qnil)),
22265 Qnil);
22266 staticpro (&list_of_error);
22267
22268 Qlast_arrow_position = intern ("last-arrow-position");
22269 staticpro (&Qlast_arrow_position);
22270 Qlast_arrow_string = intern ("last-arrow-string");
22271 staticpro (&Qlast_arrow_string);
22272
22273 Qoverlay_arrow_string = intern ("overlay-arrow-string");
22274 staticpro (&Qoverlay_arrow_string);
22275 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
22276 staticpro (&Qoverlay_arrow_bitmap);
22277
22278 echo_buffer[0] = echo_buffer[1] = Qnil;
22279 staticpro (&echo_buffer[0]);
22280 staticpro (&echo_buffer[1]);
22281
22282 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
22283 staticpro (&echo_area_buffer[0]);
22284 staticpro (&echo_area_buffer[1]);
22285
22286 Vmessages_buffer_name = build_string ("*Messages*");
22287 staticpro (&Vmessages_buffer_name);
22288
22289 mode_line_proptrans_alist = Qnil;
22290 staticpro (&mode_line_proptrans_alist);
22291
22292 mode_line_string_list = Qnil;
22293 staticpro (&mode_line_string_list);
22294
22295 help_echo_string = Qnil;
22296 staticpro (&help_echo_string);
22297 help_echo_object = Qnil;
22298 staticpro (&help_echo_object);
22299 help_echo_window = Qnil;
22300 staticpro (&help_echo_window);
22301 previous_help_echo_string = Qnil;
22302 staticpro (&previous_help_echo_string);
22303 help_echo_pos = -1;
22304
22305 #ifdef HAVE_WINDOW_SYSTEM
22306 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
22307 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
22308 For example, if a block cursor is over a tab, it will be drawn as
22309 wide as that tab on the display. */);
22310 x_stretch_cursor_p = 0;
22311 #endif
22312
22313 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
22314 doc: /* *Non-nil means highlight trailing whitespace.
22315 The face used for trailing whitespace is `trailing-whitespace'. */);
22316 Vshow_trailing_whitespace = Qnil;
22317
22318 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
22319 doc: /* *The pointer shape to show in void text areas.
22320 Nil means to show the text pointer. Other options are `arrow', `text',
22321 `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
22322 Vvoid_text_area_pointer = Qarrow;
22323
22324 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
22325 doc: /* Non-nil means don't actually do any redisplay.
22326 This is used for internal purposes. */);
22327 Vinhibit_redisplay = Qnil;
22328
22329 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
22330 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
22331 Vglobal_mode_string = Qnil;
22332
22333 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
22334 doc: /* Marker for where to display an arrow on top of the buffer text.
22335 This must be the beginning of a line in order to work.
22336 See also `overlay-arrow-string'. */);
22337 Voverlay_arrow_position = Qnil;
22338
22339 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
22340 doc: /* String to display as an arrow in non-window frames.
22341 See also `overlay-arrow-position'. */);
22342 Voverlay_arrow_string = Qnil;
22343
22344 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
22345 doc: /* List of variables (symbols) which hold markers for overlay arrows.
22346 The symbols on this list are examined during redisplay to determine
22347 where to display overlay arrows. */);
22348 Voverlay_arrow_variable_list
22349 = Fcons (intern ("overlay-arrow-position"), Qnil);
22350
22351 DEFVAR_INT ("scroll-step", &scroll_step,
22352 doc: /* *The number of lines to try scrolling a window by when point moves out.
22353 If that fails to bring point back on frame, point is centered instead.
22354 If this is zero, point is always centered after it moves off frame.
22355 If you want scrolling to always be a line at a time, you should set
22356 `scroll-conservatively' to a large value rather than set this to 1. */);
22357
22358 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
22359 doc: /* *Scroll up to this many lines, to bring point back on screen.
22360 A value of zero means to scroll the text to center point vertically
22361 in the window. */);
22362 scroll_conservatively = 0;
22363
22364 DEFVAR_INT ("scroll-margin", &scroll_margin,
22365 doc: /* *Number of lines of margin at the top and bottom of a window.
22366 Recenter the window whenever point gets within this many lines
22367 of the top or bottom of the window. */);
22368 scroll_margin = 0;
22369
22370 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
22371 doc: /* Pixels per inch on current display.
22372 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
22373 Vdisplay_pixels_per_inch = make_float (72.0);
22374
22375 #if GLYPH_DEBUG
22376 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
22377 #endif
22378
22379 DEFVAR_BOOL ("truncate-partial-width-windows",
22380 &truncate_partial_width_windows,
22381 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
22382 truncate_partial_width_windows = 1;
22383
22384 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
22385 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
22386 Any other value means to use the appropriate face, `mode-line',
22387 `header-line', or `menu' respectively. */);
22388 mode_line_inverse_video = 1;
22389
22390 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
22391 doc: /* *Maximum buffer size for which line number should be displayed.
22392 If the buffer is bigger than this, the line number does not appear
22393 in the mode line. A value of nil means no limit. */);
22394 Vline_number_display_limit = Qnil;
22395
22396 DEFVAR_INT ("line-number-display-limit-width",
22397 &line_number_display_limit_width,
22398 doc: /* *Maximum line width (in characters) for line number display.
22399 If the average length of the lines near point is bigger than this, then the
22400 line number may be omitted from the mode line. */);
22401 line_number_display_limit_width = 200;
22402
22403 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
22404 doc: /* *Non-nil means highlight region even in nonselected windows. */);
22405 highlight_nonselected_windows = 0;
22406
22407 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
22408 doc: /* Non-nil if more than one frame is visible on this display.
22409 Minibuffer-only frames don't count, but iconified frames do.
22410 This variable is not guaranteed to be accurate except while processing
22411 `frame-title-format' and `icon-title-format'. */);
22412
22413 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
22414 doc: /* Template for displaying the title bar of visible frames.
22415 \(Assuming the window manager supports this feature.)
22416 This variable has the same structure as `mode-line-format' (which see),
22417 and is used only on frames for which no explicit name has been set
22418 \(see `modify-frame-parameters'). */);
22419
22420 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
22421 doc: /* Template for displaying the title bar of an iconified frame.
22422 \(Assuming the window manager supports this feature.)
22423 This variable has the same structure as `mode-line-format' (which see),
22424 and is used only on frames for which no explicit name has been set
22425 \(see `modify-frame-parameters'). */);
22426 Vicon_title_format
22427 = Vframe_title_format
22428 = Fcons (intern ("multiple-frames"),
22429 Fcons (build_string ("%b"),
22430 Fcons (Fcons (empty_string,
22431 Fcons (intern ("invocation-name"),
22432 Fcons (build_string ("@"),
22433 Fcons (intern ("system-name"),
22434 Qnil)))),
22435 Qnil)));
22436
22437 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
22438 doc: /* Maximum number of lines to keep in the message log buffer.
22439 If nil, disable message logging. If t, log messages but don't truncate
22440 the buffer when it becomes large. */);
22441 Vmessage_log_max = make_number (50);
22442
22443 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
22444 doc: /* Functions called before redisplay, if window sizes have changed.
22445 The value should be a list of functions that take one argument.
22446 Just before redisplay, for each frame, if any of its windows have changed
22447 size since the last redisplay, or have been split or deleted,
22448 all the functions in the list are called, with the frame as argument. */);
22449 Vwindow_size_change_functions = Qnil;
22450
22451 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
22452 doc: /* List of functions to call before redisplaying a window with scrolling.
22453 Each function is called with two arguments, the window
22454 and its new display-start position. Note that the value of `window-end'
22455 is not valid when these functions are called. */);
22456 Vwindow_scroll_functions = Qnil;
22457
22458 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
22459 doc: /* *Non-nil means autoselect window with mouse pointer. */);
22460 mouse_autoselect_window = 0;
22461
22462 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
22463 doc: /* *Non-nil means automatically resize tool-bars.
22464 This increases a tool-bar's height if not all tool-bar items are visible.
22465 It decreases a tool-bar's height when it would display blank lines
22466 otherwise. */);
22467 auto_resize_tool_bars_p = 1;
22468
22469 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
22470 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
22471 auto_raise_tool_bar_buttons_p = 1;
22472
22473 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
22474 doc: /* *Margin around tool-bar buttons in pixels.
22475 If an integer, use that for both horizontal and vertical margins.
22476 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
22477 HORZ specifying the horizontal margin, and VERT specifying the
22478 vertical margin. */);
22479 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
22480
22481 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
22482 doc: /* *Relief thickness of tool-bar buttons. */);
22483 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
22484
22485 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
22486 doc: /* List of functions to call to fontify regions of text.
22487 Each function is called with one argument POS. Functions must
22488 fontify a region starting at POS in the current buffer, and give
22489 fontified regions the property `fontified'. */);
22490 Vfontification_functions = Qnil;
22491 Fmake_variable_buffer_local (Qfontification_functions);
22492
22493 DEFVAR_BOOL ("unibyte-display-via-language-environment",
22494 &unibyte_display_via_language_environment,
22495 doc: /* *Non-nil means display unibyte text according to language environment.
22496 Specifically this means that unibyte non-ASCII characters
22497 are displayed by converting them to the equivalent multibyte characters
22498 according to the current language environment. As a result, they are
22499 displayed according to the current fontset. */);
22500 unibyte_display_via_language_environment = 0;
22501
22502 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
22503 doc: /* *Maximum height for resizing mini-windows.
22504 If a float, it specifies a fraction of the mini-window frame's height.
22505 If an integer, it specifies a number of lines. */);
22506 Vmax_mini_window_height = make_float (0.25);
22507
22508 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
22509 doc: /* *How to resize mini-windows.
22510 A value of nil means don't automatically resize mini-windows.
22511 A value of t means resize them to fit the text displayed in them.
22512 A value of `grow-only', the default, means let mini-windows grow
22513 only, until their display becomes empty, at which point the windows
22514 go back to their normal size. */);
22515 Vresize_mini_windows = Qgrow_only;
22516
22517 DEFVAR_LISP ("cursor-in-non-selected-windows",
22518 &Vcursor_in_non_selected_windows,
22519 doc: /* *Cursor type to display in non-selected windows.
22520 t means to use hollow box cursor. See `cursor-type' for other values. */);
22521 Vcursor_in_non_selected_windows = Qt;
22522
22523 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
22524 doc: /* Alist specifying how to blink the cursor off.
22525 Each element has the form (ON-STATE . OFF-STATE). Whenever the
22526 `cursor-type' frame-parameter or variable equals ON-STATE,
22527 comparing using `equal', Emacs uses OFF-STATE to specify
22528 how to blink it off. */);
22529 Vblink_cursor_alist = Qnil;
22530
22531 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
22532 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
22533 automatic_hscrolling_p = 1;
22534
22535 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
22536 doc: /* *How many columns away from the window edge point is allowed to get
22537 before automatic hscrolling will horizontally scroll the window. */);
22538 hscroll_margin = 5;
22539
22540 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
22541 doc: /* *How many columns to scroll the window when point gets too close to the edge.
22542 When point is less than `automatic-hscroll-margin' columns from the window
22543 edge, automatic hscrolling will scroll the window by the amount of columns
22544 determined by this variable. If its value is a positive integer, scroll that
22545 many columns. If it's a positive floating-point number, it specifies the
22546 fraction of the window's width to scroll. If it's nil or zero, point will be
22547 centered horizontally after the scroll. Any other value, including negative
22548 numbers, are treated as if the value were zero.
22549
22550 Automatic hscrolling always moves point outside the scroll margin, so if
22551 point was more than scroll step columns inside the margin, the window will
22552 scroll more than the value given by the scroll step.
22553
22554 Note that the lower bound for automatic hscrolling specified by `scroll-left'
22555 and `scroll-right' overrides this variable's effect. */);
22556 Vhscroll_step = make_number (0);
22557
22558 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
22559 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
22560 Bind this around calls to `message' to let it take effect. */);
22561 message_truncate_lines = 0;
22562
22563 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
22564 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
22565 Can be used to update submenus whose contents should vary. */);
22566 Vmenu_bar_update_hook = Qnil;
22567
22568 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
22569 doc: /* Non-nil means don't update menu bars. Internal use only. */);
22570 inhibit_menubar_update = 0;
22571
22572 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
22573 doc: /* Non-nil means don't eval Lisp during redisplay. */);
22574 inhibit_eval_during_redisplay = 0;
22575
22576 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
22577 doc: /* Non-nil means don't free realized faces. Internal use only. */);
22578 inhibit_free_realized_faces = 0;
22579
22580 #if GLYPH_DEBUG
22581 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
22582 doc: /* Inhibit try_window_id display optimization. */);
22583 inhibit_try_window_id = 0;
22584
22585 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
22586 doc: /* Inhibit try_window_reusing display optimization. */);
22587 inhibit_try_window_reusing = 0;
22588
22589 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
22590 doc: /* Inhibit try_cursor_movement display optimization. */);
22591 inhibit_try_cursor_movement = 0;
22592 #endif /* GLYPH_DEBUG */
22593 }
22594
22595
22596 /* Initialize this module when Emacs starts. */
22597
22598 void
22599 init_xdisp ()
22600 {
22601 Lisp_Object root_window;
22602 struct window *mini_w;
22603
22604 current_header_line_height = current_mode_line_height = -1;
22605
22606 CHARPOS (this_line_start_pos) = 0;
22607
22608 mini_w = XWINDOW (minibuf_window);
22609 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
22610
22611 if (!noninteractive)
22612 {
22613 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
22614 int i;
22615
22616 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
22617 set_window_height (root_window,
22618 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
22619 0);
22620 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
22621 set_window_height (minibuf_window, 1, 0);
22622
22623 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
22624 mini_w->total_cols = make_number (FRAME_COLS (f));
22625
22626 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
22627 scratch_glyph_row.glyphs[TEXT_AREA + 1]
22628 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
22629
22630 /* The default ellipsis glyphs `...'. */
22631 for (i = 0; i < 3; ++i)
22632 default_invis_vector[i] = make_number ('.');
22633 }
22634
22635 {
22636 /* Allocate the buffer for frame titles.
22637 Also used for `format-mode-line'. */
22638 int size = 100;
22639 frame_title_buf = (char *) xmalloc (size);
22640 frame_title_buf_end = frame_title_buf + size;
22641 frame_title_ptr = NULL;
22642 }
22643
22644 help_echo_showing_p = 0;
22645 }
22646
22647
22648 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
22649 (do not change this comment) */