Merged in changes from CVS trunk.
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985,86,87,88,93,94,95,97,98,99,2000,01,02,03,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 "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "keymap.h"
183 #include "macros.h"
184 #include "disptab.h"
185 #include "termhooks.h"
186 #include "intervals.h"
187 #include "coding.h"
188 #include "process.h"
189 #include "region-cache.h"
190 #include "fontset.h"
191 #include "blockinput.h"
192
193 #ifdef HAVE_X_WINDOWS
194 #include "xterm.h"
195 #endif
196 #ifdef WINDOWSNT
197 #include "w32term.h"
198 #endif
199 #ifdef MAC_OS
200 #include "macterm.h"
201 #endif
202
203 #ifndef FRAME_X_OUTPUT
204 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
205 #endif
206
207 #define INFINITY 10000000
208
209 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
210 || defined (USE_GTK)
211 extern void set_frame_menubar P_ ((struct frame *f, int, int));
212 extern int pending_menu_activation;
213 #endif
214
215 extern int interrupt_input;
216 extern int command_loop_level;
217
218 extern int minibuffer_auto_raise;
219 extern Lisp_Object Vminibuffer_list;
220
221 extern Lisp_Object Qface;
222 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
223
224 extern Lisp_Object Voverriding_local_map;
225 extern Lisp_Object Voverriding_local_map_menu_flag;
226 extern Lisp_Object Qmenu_item;
227 extern Lisp_Object Qwhen;
228 extern Lisp_Object Qhelp_echo;
229
230 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
231 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
232 Lisp_Object Qredisplay_end_trigger_functions;
233 Lisp_Object Qinhibit_point_motion_hooks;
234 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
235 Lisp_Object Qfontified;
236 Lisp_Object Qgrow_only;
237 Lisp_Object Qinhibit_eval_during_redisplay;
238 Lisp_Object Qbuffer_position, Qposition, Qobject;
239
240 /* Cursor shapes */
241 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
242
243 /* Pointer shapes */
244 Lisp_Object Qarrow, Qhand, Qtext;
245
246 Lisp_Object Qrisky_local_variable;
247
248 /* Holds the list (error). */
249 Lisp_Object list_of_error;
250
251 /* Functions called to fontify regions of text. */
252
253 Lisp_Object Vfontification_functions;
254 Lisp_Object Qfontification_functions;
255
256 /* Non-zero means automatically select any window when the mouse
257 cursor moves into it. */
258 int mouse_autoselect_window;
259
260 /* Non-zero means draw tool bar buttons raised when the mouse moves
261 over them. */
262
263 int auto_raise_tool_bar_buttons_p;
264
265 /* Margin around tool bar buttons in pixels. */
266
267 Lisp_Object Vtool_bar_button_margin;
268
269 /* Thickness of shadow to draw around tool bar buttons. */
270
271 EMACS_INT tool_bar_button_relief;
272
273 /* Non-zero means automatically resize tool-bars so that all tool-bar
274 items are visible, and no blank lines remain. */
275
276 int auto_resize_tool_bars_p;
277
278 /* Non-zero means draw block and hollow cursor as wide as the glyph
279 under it. For example, if a block cursor is over a tab, it will be
280 drawn as wide as that tab on the display. */
281
282 int x_stretch_cursor_p;
283
284 /* Non-nil means don't actually do any redisplay. */
285
286 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
287
288 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
289
290 int inhibit_eval_during_redisplay;
291
292 /* Names of text properties relevant for redisplay. */
293
294 Lisp_Object Qdisplay;
295 extern Lisp_Object Qface, Qinvisible, Qwidth;
296
297 /* Symbols used in text property values. */
298
299 Lisp_Object Vdisplay_pixels_per_inch;
300 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
301 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
302 Lisp_Object Qslice;
303 Lisp_Object Qcenter;
304 Lisp_Object Qmargin, Qpointer;
305 Lisp_Object Qline_height, Qtotal;
306 extern Lisp_Object Qheight;
307 extern Lisp_Object QCwidth, QCheight, QCascent;
308 extern Lisp_Object Qscroll_bar;
309 extern Lisp_Object Qcursor;
310
311 /* Non-nil means highlight trailing whitespace. */
312
313 Lisp_Object Vshow_trailing_whitespace;
314
315 #ifdef HAVE_WINDOW_SYSTEM
316 extern Lisp_Object Voverflow_newline_into_fringe;
317
318 /* Test if overflow newline into fringe. Called with iterator IT
319 at or past right window margin, and with IT->current_x set. */
320
321 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
322 (!NILP (Voverflow_newline_into_fringe) \
323 && FRAME_WINDOW_P (it->f) \
324 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
325 && it->current_x == it->last_visible_x)
326
327 #endif /* HAVE_WINDOW_SYSTEM */
328
329 /* Non-nil means show the text cursor in void text areas
330 i.e. in blank areas after eol and eob. This used to be
331 the default in 21.3. */
332
333 Lisp_Object Vvoid_text_area_pointer;
334
335 /* Name of the face used to highlight trailing whitespace. */
336
337 Lisp_Object Qtrailing_whitespace;
338
339 /* The symbol `image' which is the car of the lists used to represent
340 images in Lisp. */
341
342 Lisp_Object Qimage;
343
344 /* The image map types. */
345 Lisp_Object QCmap, QCpointer;
346 Lisp_Object Qrect, Qcircle, Qpoly;
347
348 /* Non-zero means print newline to stdout before next mini-buffer
349 message. */
350
351 int noninteractive_need_newline;
352
353 /* Non-zero means print newline to message log before next message. */
354
355 static int message_log_need_newline;
356
357 /* Three markers that message_dolog uses.
358 It could allocate them itself, but that causes trouble
359 in handling memory-full errors. */
360 static Lisp_Object message_dolog_marker1;
361 static Lisp_Object message_dolog_marker2;
362 static Lisp_Object message_dolog_marker3;
363 \f
364 /* The buffer position of the first character appearing entirely or
365 partially on the line of the selected window which contains the
366 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
367 redisplay optimization in redisplay_internal. */
368
369 static struct text_pos this_line_start_pos;
370
371 /* Number of characters past the end of the line above, including the
372 terminating newline. */
373
374 static struct text_pos this_line_end_pos;
375
376 /* The vertical positions and the height of this line. */
377
378 static int this_line_vpos;
379 static int this_line_y;
380 static int this_line_pixel_height;
381
382 /* X position at which this display line starts. Usually zero;
383 negative if first character is partially visible. */
384
385 static int this_line_start_x;
386
387 /* Buffer that this_line_.* variables are referring to. */
388
389 static struct buffer *this_line_buffer;
390
391 /* Nonzero means truncate lines in all windows less wide than the
392 frame. */
393
394 int truncate_partial_width_windows;
395
396 /* A flag to control how to display unibyte 8-bit character. */
397
398 int unibyte_display_via_language_environment;
399
400 /* Nonzero means we have more than one non-mini-buffer-only frame.
401 Not guaranteed to be accurate except while parsing
402 frame-title-format. */
403
404 int multiple_frames;
405
406 Lisp_Object Vglobal_mode_string;
407
408
409 /* List of variables (symbols) which hold markers for overlay arrows.
410 The symbols on this list are examined during redisplay to determine
411 where to display overlay arrows. */
412
413 Lisp_Object Voverlay_arrow_variable_list;
414
415 /* Marker for where to display an arrow on top of the buffer text. */
416
417 Lisp_Object Voverlay_arrow_position;
418
419 /* String to display for the arrow. Only used on terminal frames. */
420
421 Lisp_Object Voverlay_arrow_string;
422
423 /* Values of those variables at last redisplay are stored as
424 properties on `overlay-arrow-position' symbol. However, if
425 Voverlay_arrow_position is a marker, last-arrow-position is its
426 numerical position. */
427
428 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
429
430 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
431 properties on a symbol in overlay-arrow-variable-list. */
432
433 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
434
435 /* Like mode-line-format, but for the title bar on a visible frame. */
436
437 Lisp_Object Vframe_title_format;
438
439 /* Like mode-line-format, but for the title bar on an iconified frame. */
440
441 Lisp_Object Vicon_title_format;
442
443 /* List of functions to call when a window's size changes. These
444 functions get one arg, a frame on which one or more windows' sizes
445 have changed. */
446
447 static Lisp_Object Vwindow_size_change_functions;
448
449 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
450
451 /* Nonzero if overlay arrow has been displayed once in this window. */
452
453 static int overlay_arrow_seen;
454
455 /* Nonzero means highlight the region even in nonselected windows. */
456
457 int highlight_nonselected_windows;
458
459 /* If cursor motion alone moves point off frame, try scrolling this
460 many lines up or down if that will bring it back. */
461
462 static EMACS_INT scroll_step;
463
464 /* Nonzero means scroll just far enough to bring point back on the
465 screen, when appropriate. */
466
467 static EMACS_INT scroll_conservatively;
468
469 /* Recenter the window whenever point gets within this many lines of
470 the top or bottom of the window. This value is translated into a
471 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
472 that there is really a fixed pixel height scroll margin. */
473
474 EMACS_INT scroll_margin;
475
476 /* Number of windows showing the buffer of the selected window (or
477 another buffer with the same base buffer). keyboard.c refers to
478 this. */
479
480 int buffer_shared;
481
482 /* Vector containing glyphs for an ellipsis `...'. */
483
484 static Lisp_Object default_invis_vector[3];
485
486 /* Zero means display the mode-line/header-line/menu-bar in the default face
487 (this slightly odd definition is for compatibility with previous versions
488 of emacs), non-zero means display them using their respective faces.
489
490 This variable is deprecated. */
491
492 int mode_line_inverse_video;
493
494 /* Prompt to display in front of the mini-buffer contents. */
495
496 Lisp_Object minibuf_prompt;
497
498 /* Width of current mini-buffer prompt. Only set after display_line
499 of the line that contains the prompt. */
500
501 int minibuf_prompt_width;
502
503 /* This is the window where the echo area message was displayed. It
504 is always a mini-buffer window, but it may not be the same window
505 currently active as a mini-buffer. */
506
507 Lisp_Object echo_area_window;
508
509 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
510 pushes the current message and the value of
511 message_enable_multibyte on the stack, the function restore_message
512 pops the stack and displays MESSAGE again. */
513
514 Lisp_Object Vmessage_stack;
515
516 /* Nonzero means multibyte characters were enabled when the echo area
517 message was specified. */
518
519 int message_enable_multibyte;
520
521 /* Nonzero if we should redraw the mode lines on the next redisplay. */
522
523 int update_mode_lines;
524
525 /* Nonzero if window sizes or contents have changed since last
526 redisplay that finished. */
527
528 int windows_or_buffers_changed;
529
530 /* Nonzero means a frame's cursor type has been changed. */
531
532 int cursor_type_changed;
533
534 /* Nonzero after display_mode_line if %l was used and it displayed a
535 line number. */
536
537 int line_number_displayed;
538
539 /* Maximum buffer size for which to display line numbers. */
540
541 Lisp_Object Vline_number_display_limit;
542
543 /* Line width to consider when repositioning for line number display. */
544
545 static EMACS_INT line_number_display_limit_width;
546
547 /* Number of lines to keep in the message log buffer. t means
548 infinite. nil means don't log at all. */
549
550 Lisp_Object Vmessage_log_max;
551
552 /* The name of the *Messages* buffer, a string. */
553
554 static Lisp_Object Vmessages_buffer_name;
555
556 /* Current, index 0, and last displayed echo area message. Either
557 buffers from echo_buffers, or nil to indicate no message. */
558
559 Lisp_Object echo_area_buffer[2];
560
561 /* The buffers referenced from echo_area_buffer. */
562
563 static Lisp_Object echo_buffer[2];
564
565 /* A vector saved used in with_area_buffer to reduce consing. */
566
567 static Lisp_Object Vwith_echo_area_save_vector;
568
569 /* Non-zero means display_echo_area should display the last echo area
570 message again. Set by redisplay_preserve_echo_area. */
571
572 static int display_last_displayed_message_p;
573
574 /* Nonzero if echo area is being used by print; zero if being used by
575 message. */
576
577 int message_buf_print;
578
579 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
580
581 Lisp_Object Qinhibit_menubar_update;
582 int inhibit_menubar_update;
583
584 /* Maximum height for resizing mini-windows. Either a float
585 specifying a fraction of the available height, or an integer
586 specifying a number of lines. */
587
588 Lisp_Object Vmax_mini_window_height;
589
590 /* Non-zero means messages should be displayed with truncated
591 lines instead of being continued. */
592
593 int message_truncate_lines;
594 Lisp_Object Qmessage_truncate_lines;
595
596 /* Set to 1 in clear_message to make redisplay_internal aware
597 of an emptied echo area. */
598
599 static int message_cleared_p;
600
601 /* Non-zero means we want a hollow cursor in windows that are not
602 selected. Zero means there's no cursor in such windows. */
603
604 Lisp_Object Vcursor_in_non_selected_windows;
605 Lisp_Object Qcursor_in_non_selected_windows;
606
607 /* How to blink the default frame cursor off. */
608 Lisp_Object Vblink_cursor_alist;
609
610 /* A scratch glyph row with contents used for generating truncation
611 glyphs. Also used in direct_output_for_insert. */
612
613 #define MAX_SCRATCH_GLYPHS 100
614 struct glyph_row scratch_glyph_row;
615 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
616
617 /* Ascent and height of the last line processed by move_it_to. */
618
619 static int last_max_ascent, last_height;
620
621 /* Non-zero if there's a help-echo in the echo area. */
622
623 int help_echo_showing_p;
624
625 /* If >= 0, computed, exact values of mode-line and header-line height
626 to use in the macros CURRENT_MODE_LINE_HEIGHT and
627 CURRENT_HEADER_LINE_HEIGHT. */
628
629 int current_mode_line_height, current_header_line_height;
630
631 /* The maximum distance to look ahead for text properties. Values
632 that are too small let us call compute_char_face and similar
633 functions too often which is expensive. Values that are too large
634 let us call compute_char_face and alike too often because we
635 might not be interested in text properties that far away. */
636
637 #define TEXT_PROP_DISTANCE_LIMIT 100
638
639 #if GLYPH_DEBUG
640
641 /* Variables to turn off display optimizations from Lisp. */
642
643 int inhibit_try_window_id, inhibit_try_window_reusing;
644 int inhibit_try_cursor_movement;
645
646 /* Non-zero means print traces of redisplay if compiled with
647 GLYPH_DEBUG != 0. */
648
649 int trace_redisplay_p;
650
651 #endif /* GLYPH_DEBUG */
652
653 #ifdef DEBUG_TRACE_MOVE
654 /* Non-zero means trace with TRACE_MOVE to stderr. */
655 int trace_move;
656
657 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
658 #else
659 #define TRACE_MOVE(x) (void) 0
660 #endif
661
662 /* Non-zero means automatically scroll windows horizontally to make
663 point visible. */
664
665 int automatic_hscrolling_p;
666
667 /* How close to the margin can point get before the window is scrolled
668 horizontally. */
669 EMACS_INT hscroll_margin;
670
671 /* How much to scroll horizontally when point is inside the above margin. */
672 Lisp_Object Vhscroll_step;
673
674 /* The variable `resize-mini-windows'. If nil, don't resize
675 mini-windows. If t, always resize them to fit the text they
676 display. If `grow-only', let mini-windows grow only until they
677 become empty. */
678
679 Lisp_Object Vresize_mini_windows;
680
681 /* Buffer being redisplayed -- for redisplay_window_error. */
682
683 struct buffer *displayed_buffer;
684
685 /* Value returned from text property handlers (see below). */
686
687 enum prop_handled
688 {
689 HANDLED_NORMALLY,
690 HANDLED_RECOMPUTE_PROPS,
691 HANDLED_OVERLAY_STRING_CONSUMED,
692 HANDLED_RETURN
693 };
694
695 /* A description of text properties that redisplay is interested
696 in. */
697
698 struct props
699 {
700 /* The name of the property. */
701 Lisp_Object *name;
702
703 /* A unique index for the property. */
704 enum prop_idx idx;
705
706 /* A handler function called to set up iterator IT from the property
707 at IT's current position. Value is used to steer handle_stop. */
708 enum prop_handled (*handler) P_ ((struct it *it));
709 };
710
711 static enum prop_handled handle_face_prop P_ ((struct it *));
712 static enum prop_handled handle_invisible_prop P_ ((struct it *));
713 static enum prop_handled handle_display_prop P_ ((struct it *));
714 static enum prop_handled handle_composition_prop P_ ((struct it *));
715 static enum prop_handled handle_overlay_change P_ ((struct it *));
716 static enum prop_handled handle_fontified_prop P_ ((struct it *));
717
718 /* Properties handled by iterators. */
719
720 static struct props it_props[] =
721 {
722 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
723 /* Handle `face' before `display' because some sub-properties of
724 `display' need to know the face. */
725 {&Qface, FACE_PROP_IDX, handle_face_prop},
726 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
727 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
728 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
729 {NULL, 0, NULL}
730 };
731
732 /* Value is the position described by X. If X is a marker, value is
733 the marker_position of X. Otherwise, value is X. */
734
735 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
736
737 /* Enumeration returned by some move_it_.* functions internally. */
738
739 enum move_it_result
740 {
741 /* Not used. Undefined value. */
742 MOVE_UNDEFINED,
743
744 /* Move ended at the requested buffer position or ZV. */
745 MOVE_POS_MATCH_OR_ZV,
746
747 /* Move ended at the requested X pixel position. */
748 MOVE_X_REACHED,
749
750 /* Move within a line ended at the end of a line that must be
751 continued. */
752 MOVE_LINE_CONTINUED,
753
754 /* Move within a line ended at the end of a line that would
755 be displayed truncated. */
756 MOVE_LINE_TRUNCATED,
757
758 /* Move within a line ended at a line end. */
759 MOVE_NEWLINE_OR_CR
760 };
761
762 /* This counter is used to clear the face cache every once in a while
763 in redisplay_internal. It is incremented for each redisplay.
764 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
765 cleared. */
766
767 #define CLEAR_FACE_CACHE_COUNT 500
768 static int clear_face_cache_count;
769
770 /* Non-zero while redisplay_internal is in progress. */
771
772 int redisplaying_p;
773
774 /* Non-zero means don't free realized faces. Bound while freeing
775 realized faces is dangerous because glyph matrices might still
776 reference them. */
777
778 int inhibit_free_realized_faces;
779 Lisp_Object Qinhibit_free_realized_faces;
780
781 /* If a string, XTread_socket generates an event to display that string.
782 (The display is done in read_char.) */
783
784 Lisp_Object help_echo_string;
785 Lisp_Object help_echo_window;
786 Lisp_Object help_echo_object;
787 int help_echo_pos;
788
789 /* Temporary variable for XTread_socket. */
790
791 Lisp_Object previous_help_echo_string;
792
793 /* Null glyph slice */
794
795 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
796
797 \f
798 /* Function prototypes. */
799
800 static void setup_for_ellipsis P_ ((struct it *));
801 static void mark_window_display_accurate_1 P_ ((struct window *, int));
802 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
803 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
804 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
805 static int redisplay_mode_lines P_ ((Lisp_Object, int));
806 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
807
808 #if 0
809 static int invisible_text_between_p P_ ((struct it *, int, int));
810 #endif
811
812 static int next_element_from_ellipsis P_ ((struct it *));
813 static void pint2str P_ ((char *, int, int));
814 static void pint2hrstr P_ ((char *, int, int));
815 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
816 struct text_pos));
817 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
818 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
819 static void store_frame_title_char P_ ((char));
820 static int store_frame_title P_ ((const unsigned char *, int, int));
821 static void x_consider_frame_title P_ ((Lisp_Object));
822 static void handle_stop P_ ((struct it *));
823 static int tool_bar_lines_needed P_ ((struct frame *));
824 static int single_display_prop_intangible_p P_ ((Lisp_Object));
825 static void ensure_echo_area_buffers P_ ((void));
826 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
827 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
828 static int with_echo_area_buffer P_ ((struct window *, int,
829 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
830 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
831 static void clear_garbaged_frames P_ ((void));
832 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
833 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
834 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
835 static int display_echo_area P_ ((struct window *));
836 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
837 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
838 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
839 static int string_char_and_length P_ ((const unsigned char *, int, int *));
840 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
841 struct text_pos));
842 static int compute_window_start_on_continuation_line P_ ((struct window *));
843 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
844 static void insert_left_trunc_glyphs P_ ((struct it *));
845 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
846 Lisp_Object));
847 static void extend_face_to_end_of_line P_ ((struct it *));
848 static int append_space_for_newline P_ ((struct it *, int));
849 static int make_cursor_line_fully_visible P_ ((struct window *, int));
850 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
851 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
852 static int trailing_whitespace_p P_ ((int));
853 static int message_log_check_duplicate P_ ((int, int, int, int));
854 static void push_it P_ ((struct it *));
855 static void pop_it P_ ((struct it *));
856 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
857 static void select_frame_for_redisplay P_ ((Lisp_Object));
858 static void redisplay_internal P_ ((int));
859 static int echo_area_display P_ ((int));
860 static void redisplay_windows P_ ((Lisp_Object));
861 static void redisplay_window P_ ((Lisp_Object, int));
862 static Lisp_Object redisplay_window_error ();
863 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
864 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
865 static void update_menu_bar P_ ((struct frame *, int));
866 static int try_window_reusing_current_matrix P_ ((struct window *));
867 static int try_window_id P_ ((struct window *));
868 static int display_line P_ ((struct it *));
869 static int display_mode_lines P_ ((struct window *));
870 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
871 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
872 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
873 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
874 static void display_menu_bar P_ ((struct window *));
875 static int display_count_lines P_ ((int, int, int, int, int *));
876 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
877 int, int, struct it *, int, int, int, int));
878 static void compute_line_metrics P_ ((struct it *));
879 static void run_redisplay_end_trigger_hook P_ ((struct it *));
880 static int get_overlay_strings P_ ((struct it *, int));
881 static void next_overlay_string P_ ((struct it *));
882 static void reseat P_ ((struct it *, struct text_pos, int));
883 static void reseat_1 P_ ((struct it *, struct text_pos, int));
884 static void back_to_previous_visible_line_start P_ ((struct it *));
885 static void reseat_at_previous_visible_line_start P_ ((struct it *));
886 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
887 static int next_element_from_display_vector P_ ((struct it *));
888 static int next_element_from_string P_ ((struct it *));
889 static int next_element_from_c_string P_ ((struct it *));
890 static int next_element_from_buffer P_ ((struct it *));
891 static int next_element_from_composition P_ ((struct it *));
892 static int next_element_from_image P_ ((struct it *));
893 static int next_element_from_stretch P_ ((struct it *));
894 static void load_overlay_strings P_ ((struct it *, int));
895 static int init_from_display_pos P_ ((struct it *, struct window *,
896 struct display_pos *));
897 static void reseat_to_string P_ ((struct it *, unsigned char *,
898 Lisp_Object, int, int, int, int));
899 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
900 int, int, int));
901 void move_it_vertically_backward P_ ((struct it *, int));
902 static void init_to_row_start P_ ((struct it *, struct window *,
903 struct glyph_row *));
904 static int init_to_row_end P_ ((struct it *, struct window *,
905 struct glyph_row *));
906 static void back_to_previous_line_start P_ ((struct it *));
907 static int forward_to_next_line_start P_ ((struct it *, int *));
908 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
909 Lisp_Object, int));
910 static struct text_pos string_pos P_ ((int, Lisp_Object));
911 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
912 static int number_of_chars P_ ((unsigned char *, int));
913 static void compute_stop_pos P_ ((struct it *));
914 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
915 Lisp_Object));
916 static int face_before_or_after_it_pos P_ ((struct it *, int));
917 static int next_overlay_change P_ ((int));
918 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
919 Lisp_Object, struct text_pos *,
920 int));
921 static int underlying_face_id P_ ((struct it *));
922 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
923 struct window *));
924
925 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
926 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
927
928 #ifdef HAVE_WINDOW_SYSTEM
929
930 static void update_tool_bar P_ ((struct frame *, int));
931 static void build_desired_tool_bar_string P_ ((struct frame *f));
932 static int redisplay_tool_bar P_ ((struct frame *));
933 static void display_tool_bar_line P_ ((struct it *));
934 static void notice_overwritten_cursor P_ ((struct window *,
935 enum glyph_row_area,
936 int, int, int, int));
937
938
939
940 #endif /* HAVE_WINDOW_SYSTEM */
941
942 \f
943 /***********************************************************************
944 Window display dimensions
945 ***********************************************************************/
946
947 /* Return the bottom boundary y-position for text lines in window W.
948 This is the first y position at which a line cannot start.
949 It is relative to the top of the window.
950
951 This is the height of W minus the height of a mode line, if any. */
952
953 INLINE int
954 window_text_bottom_y (w)
955 struct window *w;
956 {
957 int height = WINDOW_TOTAL_HEIGHT (w);
958
959 if (WINDOW_WANTS_MODELINE_P (w))
960 height -= CURRENT_MODE_LINE_HEIGHT (w);
961 return height;
962 }
963
964 /* Return the pixel width of display area AREA of window W. AREA < 0
965 means return the total width of W, not including fringes to
966 the left and right of the window. */
967
968 INLINE int
969 window_box_width (w, area)
970 struct window *w;
971 int area;
972 {
973 int cols = XFASTINT (w->total_cols);
974 int pixels = 0;
975
976 if (!w->pseudo_window_p)
977 {
978 cols -= WINDOW_SCROLL_BAR_COLS (w);
979
980 if (area == TEXT_AREA)
981 {
982 if (INTEGERP (w->left_margin_cols))
983 cols -= XFASTINT (w->left_margin_cols);
984 if (INTEGERP (w->right_margin_cols))
985 cols -= XFASTINT (w->right_margin_cols);
986 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
987 }
988 else if (area == LEFT_MARGIN_AREA)
989 {
990 cols = (INTEGERP (w->left_margin_cols)
991 ? XFASTINT (w->left_margin_cols) : 0);
992 pixels = 0;
993 }
994 else if (area == RIGHT_MARGIN_AREA)
995 {
996 cols = (INTEGERP (w->right_margin_cols)
997 ? XFASTINT (w->right_margin_cols) : 0);
998 pixels = 0;
999 }
1000 }
1001
1002 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1003 }
1004
1005
1006 /* Return the pixel height of the display area of window W, not
1007 including mode lines of W, if any. */
1008
1009 INLINE int
1010 window_box_height (w)
1011 struct window *w;
1012 {
1013 struct frame *f = XFRAME (w->frame);
1014 int height = WINDOW_TOTAL_HEIGHT (w);
1015
1016 xassert (height >= 0);
1017
1018 /* Note: the code below that determines the mode-line/header-line
1019 height is essentially the same as that contained in the macro
1020 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1021 the appropriate glyph row has its `mode_line_p' flag set,
1022 and if it doesn't, uses estimate_mode_line_height instead. */
1023
1024 if (WINDOW_WANTS_MODELINE_P (w))
1025 {
1026 struct glyph_row *ml_row
1027 = (w->current_matrix && w->current_matrix->rows
1028 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1029 : 0);
1030 if (ml_row && ml_row->mode_line_p)
1031 height -= ml_row->height;
1032 else
1033 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1034 }
1035
1036 if (WINDOW_WANTS_HEADER_LINE_P (w))
1037 {
1038 struct glyph_row *hl_row
1039 = (w->current_matrix && w->current_matrix->rows
1040 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1041 : 0);
1042 if (hl_row && hl_row->mode_line_p)
1043 height -= hl_row->height;
1044 else
1045 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1046 }
1047
1048 /* With a very small font and a mode-line that's taller than
1049 default, we might end up with a negative height. */
1050 return max (0, height);
1051 }
1052
1053 /* Return the window-relative coordinate of the left edge of display
1054 area AREA of window W. AREA < 0 means return the left edge of the
1055 whole window, to the right of the left fringe of W. */
1056
1057 INLINE int
1058 window_box_left_offset (w, area)
1059 struct window *w;
1060 int area;
1061 {
1062 int x;
1063
1064 if (w->pseudo_window_p)
1065 return 0;
1066
1067 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1068
1069 if (area == TEXT_AREA)
1070 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1071 + window_box_width (w, LEFT_MARGIN_AREA));
1072 else if (area == RIGHT_MARGIN_AREA)
1073 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1074 + window_box_width (w, LEFT_MARGIN_AREA)
1075 + window_box_width (w, TEXT_AREA)
1076 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1077 ? 0
1078 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1079 else if (area == LEFT_MARGIN_AREA
1080 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1081 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1082
1083 return x;
1084 }
1085
1086
1087 /* Return the window-relative coordinate of the right edge of display
1088 area AREA of window W. AREA < 0 means return the left edge of the
1089 whole window, to the left of the right fringe of W. */
1090
1091 INLINE int
1092 window_box_right_offset (w, area)
1093 struct window *w;
1094 int area;
1095 {
1096 return window_box_left_offset (w, area) + window_box_width (w, area);
1097 }
1098
1099 /* Return the frame-relative coordinate of the left edge of display
1100 area AREA of window W. AREA < 0 means return the left edge of the
1101 whole window, to the right of the left fringe of W. */
1102
1103 INLINE int
1104 window_box_left (w, area)
1105 struct window *w;
1106 int area;
1107 {
1108 struct frame *f = XFRAME (w->frame);
1109 int x;
1110
1111 if (w->pseudo_window_p)
1112 return FRAME_INTERNAL_BORDER_WIDTH (f);
1113
1114 x = (WINDOW_LEFT_EDGE_X (w)
1115 + window_box_left_offset (w, area));
1116
1117 return x;
1118 }
1119
1120
1121 /* Return the frame-relative coordinate of the right edge of display
1122 area AREA of window W. AREA < 0 means return the left edge of the
1123 whole window, to the left of the right fringe of W. */
1124
1125 INLINE int
1126 window_box_right (w, area)
1127 struct window *w;
1128 int area;
1129 {
1130 return window_box_left (w, area) + window_box_width (w, area);
1131 }
1132
1133 /* Get the bounding box of the display area AREA of window W, without
1134 mode lines, in frame-relative coordinates. AREA < 0 means the
1135 whole window, not including the left and right fringes of
1136 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1137 coordinates of the upper-left corner of the box. Return in
1138 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1139
1140 INLINE void
1141 window_box (w, area, box_x, box_y, box_width, box_height)
1142 struct window *w;
1143 int area;
1144 int *box_x, *box_y, *box_width, *box_height;
1145 {
1146 if (box_width)
1147 *box_width = window_box_width (w, area);
1148 if (box_height)
1149 *box_height = window_box_height (w);
1150 if (box_x)
1151 *box_x = window_box_left (w, area);
1152 if (box_y)
1153 {
1154 *box_y = WINDOW_TOP_EDGE_Y (w);
1155 if (WINDOW_WANTS_HEADER_LINE_P (w))
1156 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1157 }
1158 }
1159
1160
1161 /* Get the bounding box of the display area AREA of window W, without
1162 mode lines. AREA < 0 means the whole window, not including the
1163 left and right fringe of the window. Return in *TOP_LEFT_X
1164 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1165 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1166 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1167 box. */
1168
1169 INLINE void
1170 window_box_edges (w, area, top_left_x, top_left_y,
1171 bottom_right_x, bottom_right_y)
1172 struct window *w;
1173 int area;
1174 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1175 {
1176 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1177 bottom_right_y);
1178 *bottom_right_x += *top_left_x;
1179 *bottom_right_y += *top_left_y;
1180 }
1181
1182
1183 \f
1184 /***********************************************************************
1185 Utilities
1186 ***********************************************************************/
1187
1188 /* Return the bottom y-position of the line the iterator IT is in.
1189 This can modify IT's settings. */
1190
1191 int
1192 line_bottom_y (it)
1193 struct it *it;
1194 {
1195 int line_height = it->max_ascent + it->max_descent;
1196 int line_top_y = it->current_y;
1197
1198 if (line_height == 0)
1199 {
1200 if (last_height)
1201 line_height = last_height;
1202 else if (IT_CHARPOS (*it) < ZV)
1203 {
1204 move_it_by_lines (it, 1, 1);
1205 line_height = (it->max_ascent || it->max_descent
1206 ? it->max_ascent + it->max_descent
1207 : last_height);
1208 }
1209 else
1210 {
1211 struct glyph_row *row = it->glyph_row;
1212
1213 /* Use the default character height. */
1214 it->glyph_row = NULL;
1215 it->what = IT_CHARACTER;
1216 it->c = ' ';
1217 it->len = 1;
1218 PRODUCE_GLYPHS (it);
1219 line_height = it->ascent + it->descent;
1220 it->glyph_row = row;
1221 }
1222 }
1223
1224 return line_top_y + line_height;
1225 }
1226
1227
1228 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1229 1 if POS is visible and the line containing POS is fully visible.
1230 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1231 and header-lines heights. */
1232
1233 int
1234 pos_visible_p (w, charpos, fully, x, y, exact_mode_line_heights_p)
1235 struct window *w;
1236 int charpos, *fully, *x, *y, exact_mode_line_heights_p;
1237 {
1238 struct it it;
1239 struct text_pos top;
1240 int visible_p;
1241 struct buffer *old_buffer = NULL;
1242
1243 if (XBUFFER (w->buffer) != current_buffer)
1244 {
1245 old_buffer = current_buffer;
1246 set_buffer_internal_1 (XBUFFER (w->buffer));
1247 }
1248
1249 *fully = visible_p = 0;
1250 SET_TEXT_POS_FROM_MARKER (top, w->start);
1251
1252 /* Compute exact mode line heights, if requested. */
1253 if (exact_mode_line_heights_p)
1254 {
1255 if (WINDOW_WANTS_MODELINE_P (w))
1256 current_mode_line_height
1257 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1258 current_buffer->mode_line_format);
1259
1260 if (WINDOW_WANTS_HEADER_LINE_P (w))
1261 current_header_line_height
1262 = display_mode_line (w, HEADER_LINE_FACE_ID,
1263 current_buffer->header_line_format);
1264 }
1265
1266 start_display (&it, w, top);
1267 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1268 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1269
1270 /* Note that we may overshoot because of invisible text. */
1271 if (IT_CHARPOS (it) >= charpos)
1272 {
1273 int top_y = it.current_y;
1274 int bottom_y = line_bottom_y (&it);
1275 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1276
1277 if (top_y < window_top_y)
1278 visible_p = bottom_y > window_top_y;
1279 else if (top_y < it.last_visible_y)
1280 {
1281 visible_p = 1;
1282 *fully = bottom_y <= it.last_visible_y;
1283 }
1284 if (visible_p && x)
1285 {
1286 *x = it.current_x;
1287 *y = max (top_y + it.max_ascent - it.ascent, window_top_y);
1288 }
1289 }
1290 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1291 {
1292 struct it it2;
1293
1294 it2 = it;
1295 move_it_by_lines (&it, 1, 0);
1296 if (charpos < IT_CHARPOS (it))
1297 {
1298 visible_p = 1;
1299 if (x)
1300 {
1301 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1302 *x = it2.current_x;
1303 *y = it2.current_y + it2.max_ascent - it2.ascent;
1304 }
1305 }
1306 }
1307
1308 if (old_buffer)
1309 set_buffer_internal_1 (old_buffer);
1310
1311 current_header_line_height = current_mode_line_height = -1;
1312
1313 return visible_p;
1314 }
1315
1316
1317 /* Return the next character from STR which is MAXLEN bytes long.
1318 Return in *LEN the length of the character. This is like
1319 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1320 we find one, we return a `?', but with the length of the invalid
1321 character. */
1322
1323 static INLINE int
1324 string_char_and_length (str, maxlen, len)
1325 const unsigned char *str;
1326 int maxlen, *len;
1327 {
1328 int c;
1329
1330 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1331 if (!CHAR_VALID_P (c, 1))
1332 /* We may not change the length here because other places in Emacs
1333 don't use this function, i.e. they silently accept invalid
1334 characters. */
1335 c = '?';
1336
1337 return c;
1338 }
1339
1340
1341
1342 /* Given a position POS containing a valid character and byte position
1343 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1344
1345 static struct text_pos
1346 string_pos_nchars_ahead (pos, string, nchars)
1347 struct text_pos pos;
1348 Lisp_Object string;
1349 int nchars;
1350 {
1351 xassert (STRINGP (string) && nchars >= 0);
1352
1353 if (STRING_MULTIBYTE (string))
1354 {
1355 int rest = SBYTES (string) - BYTEPOS (pos);
1356 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1357 int len;
1358
1359 while (nchars--)
1360 {
1361 string_char_and_length (p, rest, &len);
1362 p += len, rest -= len;
1363 xassert (rest >= 0);
1364 CHARPOS (pos) += 1;
1365 BYTEPOS (pos) += len;
1366 }
1367 }
1368 else
1369 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1370
1371 return pos;
1372 }
1373
1374
1375 /* Value is the text position, i.e. character and byte position,
1376 for character position CHARPOS in STRING. */
1377
1378 static INLINE struct text_pos
1379 string_pos (charpos, string)
1380 int charpos;
1381 Lisp_Object string;
1382 {
1383 struct text_pos pos;
1384 xassert (STRINGP (string));
1385 xassert (charpos >= 0);
1386 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1387 return pos;
1388 }
1389
1390
1391 /* Value is a text position, i.e. character and byte position, for
1392 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1393 means recognize multibyte characters. */
1394
1395 static struct text_pos
1396 c_string_pos (charpos, s, multibyte_p)
1397 int charpos;
1398 unsigned char *s;
1399 int multibyte_p;
1400 {
1401 struct text_pos pos;
1402
1403 xassert (s != NULL);
1404 xassert (charpos >= 0);
1405
1406 if (multibyte_p)
1407 {
1408 int rest = strlen (s), len;
1409
1410 SET_TEXT_POS (pos, 0, 0);
1411 while (charpos--)
1412 {
1413 string_char_and_length (s, rest, &len);
1414 s += len, rest -= len;
1415 xassert (rest >= 0);
1416 CHARPOS (pos) += 1;
1417 BYTEPOS (pos) += len;
1418 }
1419 }
1420 else
1421 SET_TEXT_POS (pos, charpos, charpos);
1422
1423 return pos;
1424 }
1425
1426
1427 /* Value is the number of characters in C string S. MULTIBYTE_P
1428 non-zero means recognize multibyte characters. */
1429
1430 static int
1431 number_of_chars (s, multibyte_p)
1432 unsigned char *s;
1433 int multibyte_p;
1434 {
1435 int nchars;
1436
1437 if (multibyte_p)
1438 {
1439 int rest = strlen (s), len;
1440 unsigned char *p = (unsigned char *) s;
1441
1442 for (nchars = 0; rest > 0; ++nchars)
1443 {
1444 string_char_and_length (p, rest, &len);
1445 rest -= len, p += len;
1446 }
1447 }
1448 else
1449 nchars = strlen (s);
1450
1451 return nchars;
1452 }
1453
1454
1455 /* Compute byte position NEWPOS->bytepos corresponding to
1456 NEWPOS->charpos. POS is a known position in string STRING.
1457 NEWPOS->charpos must be >= POS.charpos. */
1458
1459 static void
1460 compute_string_pos (newpos, pos, string)
1461 struct text_pos *newpos, pos;
1462 Lisp_Object string;
1463 {
1464 xassert (STRINGP (string));
1465 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1466
1467 if (STRING_MULTIBYTE (string))
1468 *newpos = string_pos_nchars_ahead (pos, string,
1469 CHARPOS (*newpos) - CHARPOS (pos));
1470 else
1471 BYTEPOS (*newpos) = CHARPOS (*newpos);
1472 }
1473
1474 /* EXPORT:
1475 Return an estimation of the pixel height of mode or top lines on
1476 frame F. FACE_ID specifies what line's height to estimate. */
1477
1478 int
1479 estimate_mode_line_height (f, face_id)
1480 struct frame *f;
1481 enum face_id face_id;
1482 {
1483 #ifdef HAVE_WINDOW_SYSTEM
1484 if (FRAME_WINDOW_P (f))
1485 {
1486 int height = FONT_HEIGHT (FRAME_FONT (f));
1487
1488 /* This function is called so early when Emacs starts that the face
1489 cache and mode line face are not yet initialized. */
1490 if (FRAME_FACE_CACHE (f))
1491 {
1492 struct face *face = FACE_FROM_ID (f, face_id);
1493 if (face)
1494 {
1495 if (face->font)
1496 height = FONT_HEIGHT (face->font);
1497 if (face->box_line_width > 0)
1498 height += 2 * face->box_line_width;
1499 }
1500 }
1501
1502 return height;
1503 }
1504 #endif
1505
1506 return 1;
1507 }
1508
1509 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1510 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1511 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1512 not force the value into range. */
1513
1514 void
1515 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1516 FRAME_PTR f;
1517 register int pix_x, pix_y;
1518 int *x, *y;
1519 NativeRectangle *bounds;
1520 int noclip;
1521 {
1522
1523 #ifdef HAVE_WINDOW_SYSTEM
1524 if (FRAME_WINDOW_P (f))
1525 {
1526 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1527 even for negative values. */
1528 if (pix_x < 0)
1529 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1530 if (pix_y < 0)
1531 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1532
1533 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1534 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1535
1536 if (bounds)
1537 STORE_NATIVE_RECT (*bounds,
1538 FRAME_COL_TO_PIXEL_X (f, pix_x),
1539 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1540 FRAME_COLUMN_WIDTH (f) - 1,
1541 FRAME_LINE_HEIGHT (f) - 1);
1542
1543 if (!noclip)
1544 {
1545 if (pix_x < 0)
1546 pix_x = 0;
1547 else if (pix_x > FRAME_TOTAL_COLS (f))
1548 pix_x = FRAME_TOTAL_COLS (f);
1549
1550 if (pix_y < 0)
1551 pix_y = 0;
1552 else if (pix_y > FRAME_LINES (f))
1553 pix_y = FRAME_LINES (f);
1554 }
1555 }
1556 #endif
1557
1558 *x = pix_x;
1559 *y = pix_y;
1560 }
1561
1562
1563 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1564 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1565 can't tell the positions because W's display is not up to date,
1566 return 0. */
1567
1568 int
1569 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1570 struct window *w;
1571 int hpos, vpos;
1572 int *frame_x, *frame_y;
1573 {
1574 #ifdef HAVE_WINDOW_SYSTEM
1575 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1576 {
1577 int success_p;
1578
1579 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1580 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1581
1582 if (display_completed)
1583 {
1584 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1585 struct glyph *glyph = row->glyphs[TEXT_AREA];
1586 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1587
1588 hpos = row->x;
1589 vpos = row->y;
1590 while (glyph < end)
1591 {
1592 hpos += glyph->pixel_width;
1593 ++glyph;
1594 }
1595
1596 /* If first glyph is partially visible, its first visible position is still 0. */
1597 if (hpos < 0)
1598 hpos = 0;
1599
1600 success_p = 1;
1601 }
1602 else
1603 {
1604 hpos = vpos = 0;
1605 success_p = 0;
1606 }
1607
1608 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1609 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1610 return success_p;
1611 }
1612 #endif
1613
1614 *frame_x = hpos;
1615 *frame_y = vpos;
1616 return 1;
1617 }
1618
1619
1620 #ifdef HAVE_WINDOW_SYSTEM
1621
1622 /* Find the glyph under window-relative coordinates X/Y in window W.
1623 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1624 strings. Return in *HPOS and *VPOS the row and column number of
1625 the glyph found. Return in *AREA the glyph area containing X.
1626 Value is a pointer to the glyph found or null if X/Y is not on
1627 text, or we can't tell because W's current matrix is not up to
1628 date. */
1629
1630 static struct glyph *
1631 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1632 struct window *w;
1633 int x, y;
1634 int *hpos, *vpos, *dx, *dy, *area;
1635 {
1636 struct glyph *glyph, *end;
1637 struct glyph_row *row = NULL;
1638 int x0, i;
1639
1640 /* Find row containing Y. Give up if some row is not enabled. */
1641 for (i = 0; i < w->current_matrix->nrows; ++i)
1642 {
1643 row = MATRIX_ROW (w->current_matrix, i);
1644 if (!row->enabled_p)
1645 return NULL;
1646 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1647 break;
1648 }
1649
1650 *vpos = i;
1651 *hpos = 0;
1652
1653 /* Give up if Y is not in the window. */
1654 if (i == w->current_matrix->nrows)
1655 return NULL;
1656
1657 /* Get the glyph area containing X. */
1658 if (w->pseudo_window_p)
1659 {
1660 *area = TEXT_AREA;
1661 x0 = 0;
1662 }
1663 else
1664 {
1665 if (x < window_box_left_offset (w, TEXT_AREA))
1666 {
1667 *area = LEFT_MARGIN_AREA;
1668 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1669 }
1670 else if (x < window_box_right_offset (w, TEXT_AREA))
1671 {
1672 *area = TEXT_AREA;
1673 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1674 }
1675 else
1676 {
1677 *area = RIGHT_MARGIN_AREA;
1678 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1679 }
1680 }
1681
1682 /* Find glyph containing X. */
1683 glyph = row->glyphs[*area];
1684 end = glyph + row->used[*area];
1685 x -= x0;
1686 while (glyph < end && x >= glyph->pixel_width)
1687 {
1688 x -= glyph->pixel_width;
1689 ++glyph;
1690 }
1691
1692 if (glyph == end)
1693 return NULL;
1694
1695 if (dx)
1696 {
1697 *dx = x;
1698 *dy = y - (row->y + row->ascent - glyph->ascent);
1699 }
1700
1701 *hpos = glyph - row->glyphs[*area];
1702 return glyph;
1703 }
1704
1705
1706 /* EXPORT:
1707 Convert frame-relative x/y to coordinates relative to window W.
1708 Takes pseudo-windows into account. */
1709
1710 void
1711 frame_to_window_pixel_xy (w, x, y)
1712 struct window *w;
1713 int *x, *y;
1714 {
1715 if (w->pseudo_window_p)
1716 {
1717 /* A pseudo-window is always full-width, and starts at the
1718 left edge of the frame, plus a frame border. */
1719 struct frame *f = XFRAME (w->frame);
1720 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1721 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1722 }
1723 else
1724 {
1725 *x -= WINDOW_LEFT_EDGE_X (w);
1726 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1727 }
1728 }
1729
1730 /* EXPORT:
1731 Return in *R the clipping rectangle for glyph string S. */
1732
1733 void
1734 get_glyph_string_clip_rect (s, nr)
1735 struct glyph_string *s;
1736 NativeRectangle *nr;
1737 {
1738 XRectangle r;
1739
1740 if (s->row->full_width_p)
1741 {
1742 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1743 r.x = WINDOW_LEFT_EDGE_X (s->w);
1744 r.width = WINDOW_TOTAL_WIDTH (s->w);
1745
1746 /* Unless displaying a mode or menu bar line, which are always
1747 fully visible, clip to the visible part of the row. */
1748 if (s->w->pseudo_window_p)
1749 r.height = s->row->visible_height;
1750 else
1751 r.height = s->height;
1752 }
1753 else
1754 {
1755 /* This is a text line that may be partially visible. */
1756 r.x = window_box_left (s->w, s->area);
1757 r.width = window_box_width (s->w, s->area);
1758 r.height = s->row->visible_height;
1759 }
1760
1761 /* If S draws overlapping rows, it's sufficient to use the top and
1762 bottom of the window for clipping because this glyph string
1763 intentionally draws over other lines. */
1764 if (s->for_overlaps_p)
1765 {
1766 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1767 r.height = window_text_bottom_y (s->w) - r.y;
1768 }
1769 else
1770 {
1771 /* Don't use S->y for clipping because it doesn't take partially
1772 visible lines into account. For example, it can be negative for
1773 partially visible lines at the top of a window. */
1774 if (!s->row->full_width_p
1775 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1776 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1777 else
1778 r.y = max (0, s->row->y);
1779
1780 /* If drawing a tool-bar window, draw it over the internal border
1781 at the top of the window. */
1782 if (s->w == XWINDOW (s->f->tool_bar_window))
1783 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1784 }
1785
1786 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1787
1788 /* If drawing the cursor, don't let glyph draw outside its
1789 advertised boundaries. Cleartype does this under some circumstances. */
1790 if (s->hl == DRAW_CURSOR)
1791 {
1792 struct glyph *glyph = s->first_glyph;
1793 int height;
1794
1795 if (s->x > r.x)
1796 {
1797 r.width -= s->x - r.x;
1798 r.x = s->x;
1799 }
1800 r.width = min (r.width, glyph->pixel_width);
1801
1802 /* Don't draw cursor glyph taller than our actual glyph. */
1803 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1804 if (height < r.height)
1805 {
1806 int max_y = r.y + r.height;
1807 r.y = min (max_y, s->ybase + glyph->descent - height);
1808 r.height = min (max_y - r.y, height);
1809 }
1810 }
1811
1812 #ifdef CONVERT_FROM_XRECT
1813 CONVERT_FROM_XRECT (r, *nr);
1814 #else
1815 *nr = r;
1816 #endif
1817 }
1818
1819 #endif /* HAVE_WINDOW_SYSTEM */
1820
1821 \f
1822 /***********************************************************************
1823 Lisp form evaluation
1824 ***********************************************************************/
1825
1826 /* Error handler for safe_eval and safe_call. */
1827
1828 static Lisp_Object
1829 safe_eval_handler (arg)
1830 Lisp_Object arg;
1831 {
1832 add_to_log ("Error during redisplay: %s", arg, Qnil);
1833 return Qnil;
1834 }
1835
1836
1837 /* Evaluate SEXPR and return the result, or nil if something went
1838 wrong. Prevent redisplay during the evaluation. */
1839
1840 Lisp_Object
1841 safe_eval (sexpr)
1842 Lisp_Object sexpr;
1843 {
1844 Lisp_Object val;
1845
1846 if (inhibit_eval_during_redisplay)
1847 val = Qnil;
1848 else
1849 {
1850 int count = SPECPDL_INDEX ();
1851 struct gcpro gcpro1;
1852
1853 GCPRO1 (sexpr);
1854 specbind (Qinhibit_redisplay, Qt);
1855 /* Use Qt to ensure debugger does not run,
1856 so there is no possibility of wanting to redisplay. */
1857 val = internal_condition_case_1 (Feval, sexpr, Qt,
1858 safe_eval_handler);
1859 UNGCPRO;
1860 val = unbind_to (count, val);
1861 }
1862
1863 return val;
1864 }
1865
1866
1867 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1868 Return the result, or nil if something went wrong. Prevent
1869 redisplay during the evaluation. */
1870
1871 Lisp_Object
1872 safe_call (nargs, args)
1873 int nargs;
1874 Lisp_Object *args;
1875 {
1876 Lisp_Object val;
1877
1878 if (inhibit_eval_during_redisplay)
1879 val = Qnil;
1880 else
1881 {
1882 int count = SPECPDL_INDEX ();
1883 struct gcpro gcpro1;
1884
1885 GCPRO1 (args[0]);
1886 gcpro1.nvars = nargs;
1887 specbind (Qinhibit_redisplay, Qt);
1888 /* Use Qt to ensure debugger does not run,
1889 so there is no possibility of wanting to redisplay. */
1890 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1891 safe_eval_handler);
1892 UNGCPRO;
1893 val = unbind_to (count, val);
1894 }
1895
1896 return val;
1897 }
1898
1899
1900 /* Call function FN with one argument ARG.
1901 Return the result, or nil if something went wrong. */
1902
1903 Lisp_Object
1904 safe_call1 (fn, arg)
1905 Lisp_Object fn, arg;
1906 {
1907 Lisp_Object args[2];
1908 args[0] = fn;
1909 args[1] = arg;
1910 return safe_call (2, args);
1911 }
1912
1913
1914 \f
1915 /***********************************************************************
1916 Debugging
1917 ***********************************************************************/
1918
1919 #if 0
1920
1921 /* Define CHECK_IT to perform sanity checks on iterators.
1922 This is for debugging. It is too slow to do unconditionally. */
1923
1924 static void
1925 check_it (it)
1926 struct it *it;
1927 {
1928 if (it->method == next_element_from_string)
1929 {
1930 xassert (STRINGP (it->string));
1931 xassert (IT_STRING_CHARPOS (*it) >= 0);
1932 }
1933 else
1934 {
1935 xassert (IT_STRING_CHARPOS (*it) < 0);
1936 if (it->method == next_element_from_buffer)
1937 {
1938 /* Check that character and byte positions agree. */
1939 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1940 }
1941 }
1942
1943 if (it->dpvec)
1944 xassert (it->current.dpvec_index >= 0);
1945 else
1946 xassert (it->current.dpvec_index < 0);
1947 }
1948
1949 #define CHECK_IT(IT) check_it ((IT))
1950
1951 #else /* not 0 */
1952
1953 #define CHECK_IT(IT) (void) 0
1954
1955 #endif /* not 0 */
1956
1957
1958 #if GLYPH_DEBUG
1959
1960 /* Check that the window end of window W is what we expect it
1961 to be---the last row in the current matrix displaying text. */
1962
1963 static void
1964 check_window_end (w)
1965 struct window *w;
1966 {
1967 if (!MINI_WINDOW_P (w)
1968 && !NILP (w->window_end_valid))
1969 {
1970 struct glyph_row *row;
1971 xassert ((row = MATRIX_ROW (w->current_matrix,
1972 XFASTINT (w->window_end_vpos)),
1973 !row->enabled_p
1974 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1975 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1976 }
1977 }
1978
1979 #define CHECK_WINDOW_END(W) check_window_end ((W))
1980
1981 #else /* not GLYPH_DEBUG */
1982
1983 #define CHECK_WINDOW_END(W) (void) 0
1984
1985 #endif /* not GLYPH_DEBUG */
1986
1987
1988 \f
1989 /***********************************************************************
1990 Iterator initialization
1991 ***********************************************************************/
1992
1993 /* Initialize IT for displaying current_buffer in window W, starting
1994 at character position CHARPOS. CHARPOS < 0 means that no buffer
1995 position is specified which is useful when the iterator is assigned
1996 a position later. BYTEPOS is the byte position corresponding to
1997 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
1998
1999 If ROW is not null, calls to produce_glyphs with IT as parameter
2000 will produce glyphs in that row.
2001
2002 BASE_FACE_ID is the id of a base face to use. It must be one of
2003 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2004 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2005 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2006
2007 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2008 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2009 will be initialized to use the corresponding mode line glyph row of
2010 the desired matrix of W. */
2011
2012 void
2013 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2014 struct it *it;
2015 struct window *w;
2016 int charpos, bytepos;
2017 struct glyph_row *row;
2018 enum face_id base_face_id;
2019 {
2020 int highlight_region_p;
2021
2022 /* Some precondition checks. */
2023 xassert (w != NULL && it != NULL);
2024 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2025 && charpos <= ZV));
2026
2027 /* If face attributes have been changed since the last redisplay,
2028 free realized faces now because they depend on face definitions
2029 that might have changed. Don't free faces while there might be
2030 desired matrices pending which reference these faces. */
2031 if (face_change_count && !inhibit_free_realized_faces)
2032 {
2033 face_change_count = 0;
2034 free_all_realized_faces (Qnil);
2035 }
2036
2037 /* Use one of the mode line rows of W's desired matrix if
2038 appropriate. */
2039 if (row == NULL)
2040 {
2041 if (base_face_id == MODE_LINE_FACE_ID
2042 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2043 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2044 else if (base_face_id == HEADER_LINE_FACE_ID)
2045 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2046 }
2047
2048 /* Clear IT. */
2049 bzero (it, sizeof *it);
2050 it->current.overlay_string_index = -1;
2051 it->current.dpvec_index = -1;
2052 it->base_face_id = base_face_id;
2053 it->string = Qnil;
2054 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2055
2056 /* The window in which we iterate over current_buffer: */
2057 XSETWINDOW (it->window, w);
2058 it->w = w;
2059 it->f = XFRAME (w->frame);
2060
2061 /* Extra space between lines (on window systems only). */
2062 if (base_face_id == DEFAULT_FACE_ID
2063 && FRAME_WINDOW_P (it->f))
2064 {
2065 if (NATNUMP (current_buffer->extra_line_spacing))
2066 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2067 else if (FLOATP (current_buffer->extra_line_spacing))
2068 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2069 * FRAME_LINE_HEIGHT (it->f));
2070 else if (it->f->extra_line_spacing > 0)
2071 it->extra_line_spacing = it->f->extra_line_spacing;
2072 }
2073
2074 /* If realized faces have been removed, e.g. because of face
2075 attribute changes of named faces, recompute them. When running
2076 in batch mode, the face cache of the initial frame is null. If
2077 we happen to get called, make a dummy face cache. */
2078 if (FRAME_FACE_CACHE (it->f) == NULL)
2079 init_frame_faces (it->f);
2080 if (FRAME_FACE_CACHE (it->f)->used == 0)
2081 recompute_basic_faces (it->f);
2082
2083 /* Current value of the `slice', `space-width', and 'height' properties. */
2084 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2085 it->space_width = Qnil;
2086 it->font_height = Qnil;
2087 it->override_ascent = -1;
2088
2089 /* Are control characters displayed as `^C'? */
2090 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2091
2092 /* -1 means everything between a CR and the following line end
2093 is invisible. >0 means lines indented more than this value are
2094 invisible. */
2095 it->selective = (INTEGERP (current_buffer->selective_display)
2096 ? XFASTINT (current_buffer->selective_display)
2097 : (!NILP (current_buffer->selective_display)
2098 ? -1 : 0));
2099 it->selective_display_ellipsis_p
2100 = !NILP (current_buffer->selective_display_ellipses);
2101
2102 /* Display table to use. */
2103 it->dp = window_display_table (w);
2104
2105 /* Are multibyte characters enabled in current_buffer? */
2106 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2107
2108 /* Non-zero if we should highlight the region. */
2109 highlight_region_p
2110 = (!NILP (Vtransient_mark_mode)
2111 && !NILP (current_buffer->mark_active)
2112 && XMARKER (current_buffer->mark)->buffer != 0);
2113
2114 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2115 start and end of a visible region in window IT->w. Set both to
2116 -1 to indicate no region. */
2117 if (highlight_region_p
2118 /* Maybe highlight only in selected window. */
2119 && (/* Either show region everywhere. */
2120 highlight_nonselected_windows
2121 /* Or show region in the selected window. */
2122 || w == XWINDOW (selected_window)
2123 /* Or show the region if we are in the mini-buffer and W is
2124 the window the mini-buffer refers to. */
2125 || (MINI_WINDOW_P (XWINDOW (selected_window))
2126 && WINDOWP (minibuf_selected_window)
2127 && w == XWINDOW (minibuf_selected_window))))
2128 {
2129 int charpos = marker_position (current_buffer->mark);
2130 it->region_beg_charpos = min (PT, charpos);
2131 it->region_end_charpos = max (PT, charpos);
2132 }
2133 else
2134 it->region_beg_charpos = it->region_end_charpos = -1;
2135
2136 /* Get the position at which the redisplay_end_trigger hook should
2137 be run, if it is to be run at all. */
2138 if (MARKERP (w->redisplay_end_trigger)
2139 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2140 it->redisplay_end_trigger_charpos
2141 = marker_position (w->redisplay_end_trigger);
2142 else if (INTEGERP (w->redisplay_end_trigger))
2143 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2144
2145 /* Correct bogus values of tab_width. */
2146 it->tab_width = XINT (current_buffer->tab_width);
2147 if (it->tab_width <= 0 || it->tab_width > 1000)
2148 it->tab_width = 8;
2149
2150 /* Are lines in the display truncated? */
2151 it->truncate_lines_p
2152 = (base_face_id != DEFAULT_FACE_ID
2153 || XINT (it->w->hscroll)
2154 || (truncate_partial_width_windows
2155 && !WINDOW_FULL_WIDTH_P (it->w))
2156 || !NILP (current_buffer->truncate_lines));
2157
2158 /* Get dimensions of truncation and continuation glyphs. These are
2159 displayed as fringe bitmaps under X, so we don't need them for such
2160 frames. */
2161 if (!FRAME_WINDOW_P (it->f))
2162 {
2163 if (it->truncate_lines_p)
2164 {
2165 /* We will need the truncation glyph. */
2166 xassert (it->glyph_row == NULL);
2167 produce_special_glyphs (it, IT_TRUNCATION);
2168 it->truncation_pixel_width = it->pixel_width;
2169 }
2170 else
2171 {
2172 /* We will need the continuation glyph. */
2173 xassert (it->glyph_row == NULL);
2174 produce_special_glyphs (it, IT_CONTINUATION);
2175 it->continuation_pixel_width = it->pixel_width;
2176 }
2177
2178 /* Reset these values to zero because the produce_special_glyphs
2179 above has changed them. */
2180 it->pixel_width = it->ascent = it->descent = 0;
2181 it->phys_ascent = it->phys_descent = 0;
2182 }
2183
2184 /* Set this after getting the dimensions of truncation and
2185 continuation glyphs, so that we don't produce glyphs when calling
2186 produce_special_glyphs, above. */
2187 it->glyph_row = row;
2188 it->area = TEXT_AREA;
2189
2190 /* Get the dimensions of the display area. The display area
2191 consists of the visible window area plus a horizontally scrolled
2192 part to the left of the window. All x-values are relative to the
2193 start of this total display area. */
2194 if (base_face_id != DEFAULT_FACE_ID)
2195 {
2196 /* Mode lines, menu bar in terminal frames. */
2197 it->first_visible_x = 0;
2198 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2199 }
2200 else
2201 {
2202 it->first_visible_x
2203 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2204 it->last_visible_x = (it->first_visible_x
2205 + window_box_width (w, TEXT_AREA));
2206
2207 /* If we truncate lines, leave room for the truncator glyph(s) at
2208 the right margin. Otherwise, leave room for the continuation
2209 glyph(s). Truncation and continuation glyphs are not inserted
2210 for window-based redisplay. */
2211 if (!FRAME_WINDOW_P (it->f))
2212 {
2213 if (it->truncate_lines_p)
2214 it->last_visible_x -= it->truncation_pixel_width;
2215 else
2216 it->last_visible_x -= it->continuation_pixel_width;
2217 }
2218
2219 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2220 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2221 }
2222
2223 /* Leave room for a border glyph. */
2224 if (!FRAME_WINDOW_P (it->f)
2225 && !WINDOW_RIGHTMOST_P (it->w))
2226 it->last_visible_x -= 1;
2227
2228 it->last_visible_y = window_text_bottom_y (w);
2229
2230 /* For mode lines and alike, arrange for the first glyph having a
2231 left box line if the face specifies a box. */
2232 if (base_face_id != DEFAULT_FACE_ID)
2233 {
2234 struct face *face;
2235
2236 it->face_id = base_face_id;
2237
2238 /* If we have a boxed mode line, make the first character appear
2239 with a left box line. */
2240 face = FACE_FROM_ID (it->f, base_face_id);
2241 if (face->box != FACE_NO_BOX)
2242 it->start_of_box_run_p = 1;
2243 }
2244
2245 /* If a buffer position was specified, set the iterator there,
2246 getting overlays and face properties from that position. */
2247 if (charpos >= BUF_BEG (current_buffer))
2248 {
2249 it->end_charpos = ZV;
2250 it->face_id = -1;
2251 IT_CHARPOS (*it) = charpos;
2252
2253 /* Compute byte position if not specified. */
2254 if (bytepos < charpos)
2255 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2256 else
2257 IT_BYTEPOS (*it) = bytepos;
2258
2259 it->start = it->current;
2260
2261 /* Compute faces etc. */
2262 reseat (it, it->current.pos, 1);
2263 }
2264
2265 CHECK_IT (it);
2266 }
2267
2268
2269 /* Initialize IT for the display of window W with window start POS. */
2270
2271 void
2272 start_display (it, w, pos)
2273 struct it *it;
2274 struct window *w;
2275 struct text_pos pos;
2276 {
2277 struct glyph_row *row;
2278 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2279
2280 row = w->desired_matrix->rows + first_vpos;
2281 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2282 it->first_vpos = first_vpos;
2283
2284 if (!it->truncate_lines_p)
2285 {
2286 int start_at_line_beg_p;
2287 int first_y = it->current_y;
2288
2289 /* If window start is not at a line start, skip forward to POS to
2290 get the correct continuation lines width. */
2291 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2292 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2293 if (!start_at_line_beg_p)
2294 {
2295 int new_x;
2296
2297 reseat_at_previous_visible_line_start (it);
2298 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2299
2300 new_x = it->current_x + it->pixel_width;
2301
2302 /* If lines are continued, this line may end in the middle
2303 of a multi-glyph character (e.g. a control character
2304 displayed as \003, or in the middle of an overlay
2305 string). In this case move_it_to above will not have
2306 taken us to the start of the continuation line but to the
2307 end of the continued line. */
2308 if (it->current_x > 0
2309 && !it->truncate_lines_p /* Lines are continued. */
2310 && (/* And glyph doesn't fit on the line. */
2311 new_x > it->last_visible_x
2312 /* Or it fits exactly and we're on a window
2313 system frame. */
2314 || (new_x == it->last_visible_x
2315 && FRAME_WINDOW_P (it->f))))
2316 {
2317 if (it->current.dpvec_index >= 0
2318 || it->current.overlay_string_index >= 0)
2319 {
2320 set_iterator_to_next (it, 1);
2321 move_it_in_display_line_to (it, -1, -1, 0);
2322 }
2323
2324 it->continuation_lines_width += it->current_x;
2325 }
2326
2327 /* We're starting a new display line, not affected by the
2328 height of the continued line, so clear the appropriate
2329 fields in the iterator structure. */
2330 it->max_ascent = it->max_descent = 0;
2331 it->max_phys_ascent = it->max_phys_descent = 0;
2332
2333 it->current_y = first_y;
2334 it->vpos = 0;
2335 it->current_x = it->hpos = 0;
2336 }
2337 }
2338
2339 #if 0 /* Don't assert the following because start_display is sometimes
2340 called intentionally with a window start that is not at a
2341 line start. Please leave this code in as a comment. */
2342
2343 /* Window start should be on a line start, now. */
2344 xassert (it->continuation_lines_width
2345 || IT_CHARPOS (it) == BEGV
2346 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2347 #endif /* 0 */
2348 }
2349
2350
2351 /* Return 1 if POS is a position in ellipses displayed for invisible
2352 text. W is the window we display, for text property lookup. */
2353
2354 static int
2355 in_ellipses_for_invisible_text_p (pos, w)
2356 struct display_pos *pos;
2357 struct window *w;
2358 {
2359 Lisp_Object prop, window;
2360 int ellipses_p = 0;
2361 int charpos = CHARPOS (pos->pos);
2362
2363 /* If POS specifies a position in a display vector, this might
2364 be for an ellipsis displayed for invisible text. We won't
2365 get the iterator set up for delivering that ellipsis unless
2366 we make sure that it gets aware of the invisible text. */
2367 if (pos->dpvec_index >= 0
2368 && pos->overlay_string_index < 0
2369 && CHARPOS (pos->string_pos) < 0
2370 && charpos > BEGV
2371 && (XSETWINDOW (window, w),
2372 prop = Fget_char_property (make_number (charpos),
2373 Qinvisible, window),
2374 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2375 {
2376 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2377 window);
2378 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2379 }
2380
2381 return ellipses_p;
2382 }
2383
2384
2385 /* Initialize IT for stepping through current_buffer in window W,
2386 starting at position POS that includes overlay string and display
2387 vector/ control character translation position information. Value
2388 is zero if there are overlay strings with newlines at POS. */
2389
2390 static int
2391 init_from_display_pos (it, w, pos)
2392 struct it *it;
2393 struct window *w;
2394 struct display_pos *pos;
2395 {
2396 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2397 int i, overlay_strings_with_newlines = 0;
2398
2399 /* If POS specifies a position in a display vector, this might
2400 be for an ellipsis displayed for invisible text. We won't
2401 get the iterator set up for delivering that ellipsis unless
2402 we make sure that it gets aware of the invisible text. */
2403 if (in_ellipses_for_invisible_text_p (pos, w))
2404 {
2405 --charpos;
2406 bytepos = 0;
2407 }
2408
2409 /* Keep in mind: the call to reseat in init_iterator skips invisible
2410 text, so we might end up at a position different from POS. This
2411 is only a problem when POS is a row start after a newline and an
2412 overlay starts there with an after-string, and the overlay has an
2413 invisible property. Since we don't skip invisible text in
2414 display_line and elsewhere immediately after consuming the
2415 newline before the row start, such a POS will not be in a string,
2416 but the call to init_iterator below will move us to the
2417 after-string. */
2418 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2419
2420 for (i = 0; i < it->n_overlay_strings; ++i)
2421 {
2422 const char *s = SDATA (it->overlay_strings[i]);
2423 const char *e = s + SBYTES (it->overlay_strings[i]);
2424
2425 while (s < e && *s != '\n')
2426 ++s;
2427
2428 if (s < e)
2429 {
2430 overlay_strings_with_newlines = 1;
2431 break;
2432 }
2433 }
2434
2435 /* If position is within an overlay string, set up IT to the right
2436 overlay string. */
2437 if (pos->overlay_string_index >= 0)
2438 {
2439 int relative_index;
2440
2441 /* If the first overlay string happens to have a `display'
2442 property for an image, the iterator will be set up for that
2443 image, and we have to undo that setup first before we can
2444 correct the overlay string index. */
2445 if (it->method == next_element_from_image)
2446 pop_it (it);
2447
2448 /* We already have the first chunk of overlay strings in
2449 IT->overlay_strings. Load more until the one for
2450 pos->overlay_string_index is in IT->overlay_strings. */
2451 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2452 {
2453 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2454 it->current.overlay_string_index = 0;
2455 while (n--)
2456 {
2457 load_overlay_strings (it, 0);
2458 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2459 }
2460 }
2461
2462 it->current.overlay_string_index = pos->overlay_string_index;
2463 relative_index = (it->current.overlay_string_index
2464 % OVERLAY_STRING_CHUNK_SIZE);
2465 it->string = it->overlay_strings[relative_index];
2466 xassert (STRINGP (it->string));
2467 it->current.string_pos = pos->string_pos;
2468 it->method = next_element_from_string;
2469 }
2470
2471 #if 0 /* This is bogus because POS not having an overlay string
2472 position does not mean it's after the string. Example: A
2473 line starting with a before-string and initialization of IT
2474 to the previous row's end position. */
2475 else if (it->current.overlay_string_index >= 0)
2476 {
2477 /* If POS says we're already after an overlay string ending at
2478 POS, make sure to pop the iterator because it will be in
2479 front of that overlay string. When POS is ZV, we've thereby
2480 also ``processed'' overlay strings at ZV. */
2481 while (it->sp)
2482 pop_it (it);
2483 it->current.overlay_string_index = -1;
2484 it->method = next_element_from_buffer;
2485 if (CHARPOS (pos->pos) == ZV)
2486 it->overlay_strings_at_end_processed_p = 1;
2487 }
2488 #endif /* 0 */
2489
2490 if (CHARPOS (pos->string_pos) >= 0)
2491 {
2492 /* Recorded position is not in an overlay string, but in another
2493 string. This can only be a string from a `display' property.
2494 IT should already be filled with that string. */
2495 it->current.string_pos = pos->string_pos;
2496 xassert (STRINGP (it->string));
2497 }
2498
2499 /* Restore position in display vector translations, control
2500 character translations or ellipses. */
2501 if (pos->dpvec_index >= 0)
2502 {
2503 if (it->dpvec == NULL)
2504 get_next_display_element (it);
2505 xassert (it->dpvec && it->current.dpvec_index == 0);
2506 it->current.dpvec_index = pos->dpvec_index;
2507 }
2508
2509 CHECK_IT (it);
2510 return !overlay_strings_with_newlines;
2511 }
2512
2513
2514 /* Initialize IT for stepping through current_buffer in window W
2515 starting at ROW->start. */
2516
2517 static void
2518 init_to_row_start (it, w, row)
2519 struct it *it;
2520 struct window *w;
2521 struct glyph_row *row;
2522 {
2523 init_from_display_pos (it, w, &row->start);
2524 it->start = row->start;
2525 it->continuation_lines_width = row->continuation_lines_width;
2526 CHECK_IT (it);
2527 }
2528
2529
2530 /* Initialize IT for stepping through current_buffer in window W
2531 starting in the line following ROW, i.e. starting at ROW->end.
2532 Value is zero if there are overlay strings with newlines at ROW's
2533 end position. */
2534
2535 static int
2536 init_to_row_end (it, w, row)
2537 struct it *it;
2538 struct window *w;
2539 struct glyph_row *row;
2540 {
2541 int success = 0;
2542
2543 if (init_from_display_pos (it, w, &row->end))
2544 {
2545 if (row->continued_p)
2546 it->continuation_lines_width
2547 = row->continuation_lines_width + row->pixel_width;
2548 CHECK_IT (it);
2549 success = 1;
2550 }
2551
2552 return success;
2553 }
2554
2555
2556
2557 \f
2558 /***********************************************************************
2559 Text properties
2560 ***********************************************************************/
2561
2562 /* Called when IT reaches IT->stop_charpos. Handle text property and
2563 overlay changes. Set IT->stop_charpos to the next position where
2564 to stop. */
2565
2566 static void
2567 handle_stop (it)
2568 struct it *it;
2569 {
2570 enum prop_handled handled;
2571 int handle_overlay_change_p = 1;
2572 struct props *p;
2573
2574 it->dpvec = NULL;
2575 it->current.dpvec_index = -1;
2576
2577 do
2578 {
2579 handled = HANDLED_NORMALLY;
2580
2581 /* Call text property handlers. */
2582 for (p = it_props; p->handler; ++p)
2583 {
2584 handled = p->handler (it);
2585
2586 if (handled == HANDLED_RECOMPUTE_PROPS)
2587 break;
2588 else if (handled == HANDLED_RETURN)
2589 return;
2590 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2591 handle_overlay_change_p = 0;
2592 }
2593
2594 if (handled != HANDLED_RECOMPUTE_PROPS)
2595 {
2596 /* Don't check for overlay strings below when set to deliver
2597 characters from a display vector. */
2598 if (it->method == next_element_from_display_vector)
2599 handle_overlay_change_p = 0;
2600
2601 /* Handle overlay changes. */
2602 if (handle_overlay_change_p)
2603 handled = handle_overlay_change (it);
2604
2605 /* Determine where to stop next. */
2606 if (handled == HANDLED_NORMALLY)
2607 compute_stop_pos (it);
2608 }
2609 }
2610 while (handled == HANDLED_RECOMPUTE_PROPS);
2611 }
2612
2613
2614 /* Compute IT->stop_charpos from text property and overlay change
2615 information for IT's current position. */
2616
2617 static void
2618 compute_stop_pos (it)
2619 struct it *it;
2620 {
2621 register INTERVAL iv, next_iv;
2622 Lisp_Object object, limit, position;
2623
2624 /* If nowhere else, stop at the end. */
2625 it->stop_charpos = it->end_charpos;
2626
2627 if (STRINGP (it->string))
2628 {
2629 /* Strings are usually short, so don't limit the search for
2630 properties. */
2631 object = it->string;
2632 limit = Qnil;
2633 position = make_number (IT_STRING_CHARPOS (*it));
2634 }
2635 else
2636 {
2637 int charpos;
2638
2639 /* If next overlay change is in front of the current stop pos
2640 (which is IT->end_charpos), stop there. Note: value of
2641 next_overlay_change is point-max if no overlay change
2642 follows. */
2643 charpos = next_overlay_change (IT_CHARPOS (*it));
2644 if (charpos < it->stop_charpos)
2645 it->stop_charpos = charpos;
2646
2647 /* If showing the region, we have to stop at the region
2648 start or end because the face might change there. */
2649 if (it->region_beg_charpos > 0)
2650 {
2651 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2652 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2653 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2654 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2655 }
2656
2657 /* Set up variables for computing the stop position from text
2658 property changes. */
2659 XSETBUFFER (object, current_buffer);
2660 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2661 position = make_number (IT_CHARPOS (*it));
2662
2663 }
2664
2665 /* Get the interval containing IT's position. Value is a null
2666 interval if there isn't such an interval. */
2667 iv = validate_interval_range (object, &position, &position, 0);
2668 if (!NULL_INTERVAL_P (iv))
2669 {
2670 Lisp_Object values_here[LAST_PROP_IDX];
2671 struct props *p;
2672
2673 /* Get properties here. */
2674 for (p = it_props; p->handler; ++p)
2675 values_here[p->idx] = textget (iv->plist, *p->name);
2676
2677 /* Look for an interval following iv that has different
2678 properties. */
2679 for (next_iv = next_interval (iv);
2680 (!NULL_INTERVAL_P (next_iv)
2681 && (NILP (limit)
2682 || XFASTINT (limit) > next_iv->position));
2683 next_iv = next_interval (next_iv))
2684 {
2685 for (p = it_props; p->handler; ++p)
2686 {
2687 Lisp_Object new_value;
2688
2689 new_value = textget (next_iv->plist, *p->name);
2690 if (!EQ (values_here[p->idx], new_value))
2691 break;
2692 }
2693
2694 if (p->handler)
2695 break;
2696 }
2697
2698 if (!NULL_INTERVAL_P (next_iv))
2699 {
2700 if (INTEGERP (limit)
2701 && next_iv->position >= XFASTINT (limit))
2702 /* No text property change up to limit. */
2703 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2704 else
2705 /* Text properties change in next_iv. */
2706 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2707 }
2708 }
2709
2710 xassert (STRINGP (it->string)
2711 || (it->stop_charpos >= BEGV
2712 && it->stop_charpos >= IT_CHARPOS (*it)));
2713 }
2714
2715
2716 /* Return the position of the next overlay change after POS in
2717 current_buffer. Value is point-max if no overlay change
2718 follows. This is like `next-overlay-change' but doesn't use
2719 xmalloc. */
2720
2721 static int
2722 next_overlay_change (pos)
2723 int pos;
2724 {
2725 int noverlays;
2726 int endpos;
2727 Lisp_Object *overlays;
2728 int i;
2729
2730 /* Get all overlays at the given position. */
2731 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
2732
2733 /* If any of these overlays ends before endpos,
2734 use its ending point instead. */
2735 for (i = 0; i < noverlays; ++i)
2736 {
2737 Lisp_Object oend;
2738 int oendpos;
2739
2740 oend = OVERLAY_END (overlays[i]);
2741 oendpos = OVERLAY_POSITION (oend);
2742 endpos = min (endpos, oendpos);
2743 }
2744
2745 return endpos;
2746 }
2747
2748
2749 \f
2750 /***********************************************************************
2751 Fontification
2752 ***********************************************************************/
2753
2754 /* Handle changes in the `fontified' property of the current buffer by
2755 calling hook functions from Qfontification_functions to fontify
2756 regions of text. */
2757
2758 static enum prop_handled
2759 handle_fontified_prop (it)
2760 struct it *it;
2761 {
2762 Lisp_Object prop, pos;
2763 enum prop_handled handled = HANDLED_NORMALLY;
2764
2765 /* Get the value of the `fontified' property at IT's current buffer
2766 position. (The `fontified' property doesn't have a special
2767 meaning in strings.) If the value is nil, call functions from
2768 Qfontification_functions. */
2769 if (!STRINGP (it->string)
2770 && it->s == NULL
2771 && !NILP (Vfontification_functions)
2772 && !NILP (Vrun_hooks)
2773 && (pos = make_number (IT_CHARPOS (*it)),
2774 prop = Fget_char_property (pos, Qfontified, Qnil),
2775 NILP (prop)))
2776 {
2777 int count = SPECPDL_INDEX ();
2778 Lisp_Object val;
2779
2780 val = Vfontification_functions;
2781 specbind (Qfontification_functions, Qnil);
2782
2783 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2784 safe_call1 (val, pos);
2785 else
2786 {
2787 Lisp_Object globals, fn;
2788 struct gcpro gcpro1, gcpro2;
2789
2790 globals = Qnil;
2791 GCPRO2 (val, globals);
2792
2793 for (; CONSP (val); val = XCDR (val))
2794 {
2795 fn = XCAR (val);
2796
2797 if (EQ (fn, Qt))
2798 {
2799 /* A value of t indicates this hook has a local
2800 binding; it means to run the global binding too.
2801 In a global value, t should not occur. If it
2802 does, we must ignore it to avoid an endless
2803 loop. */
2804 for (globals = Fdefault_value (Qfontification_functions);
2805 CONSP (globals);
2806 globals = XCDR (globals))
2807 {
2808 fn = XCAR (globals);
2809 if (!EQ (fn, Qt))
2810 safe_call1 (fn, pos);
2811 }
2812 }
2813 else
2814 safe_call1 (fn, pos);
2815 }
2816
2817 UNGCPRO;
2818 }
2819
2820 unbind_to (count, Qnil);
2821
2822 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2823 something. This avoids an endless loop if they failed to
2824 fontify the text for which reason ever. */
2825 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2826 handled = HANDLED_RECOMPUTE_PROPS;
2827 }
2828
2829 return handled;
2830 }
2831
2832
2833 \f
2834 /***********************************************************************
2835 Faces
2836 ***********************************************************************/
2837
2838 /* Set up iterator IT from face properties at its current position.
2839 Called from handle_stop. */
2840
2841 static enum prop_handled
2842 handle_face_prop (it)
2843 struct it *it;
2844 {
2845 int new_face_id, next_stop;
2846
2847 if (!STRINGP (it->string))
2848 {
2849 new_face_id
2850 = face_at_buffer_position (it->w,
2851 IT_CHARPOS (*it),
2852 it->region_beg_charpos,
2853 it->region_end_charpos,
2854 &next_stop,
2855 (IT_CHARPOS (*it)
2856 + TEXT_PROP_DISTANCE_LIMIT),
2857 0);
2858
2859 /* Is this a start of a run of characters with box face?
2860 Caveat: this can be called for a freshly initialized
2861 iterator; face_id is -1 in this case. We know that the new
2862 face will not change until limit, i.e. if the new face has a
2863 box, all characters up to limit will have one. But, as
2864 usual, we don't know whether limit is really the end. */
2865 if (new_face_id != it->face_id)
2866 {
2867 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2868
2869 /* If new face has a box but old face has not, this is
2870 the start of a run of characters with box, i.e. it has
2871 a shadow on the left side. The value of face_id of the
2872 iterator will be -1 if this is the initial call that gets
2873 the face. In this case, we have to look in front of IT's
2874 position and see whether there is a face != new_face_id. */
2875 it->start_of_box_run_p
2876 = (new_face->box != FACE_NO_BOX
2877 && (it->face_id >= 0
2878 || IT_CHARPOS (*it) == BEG
2879 || new_face_id != face_before_it_pos (it)));
2880 it->face_box_p = new_face->box != FACE_NO_BOX;
2881 }
2882 }
2883 else
2884 {
2885 int base_face_id, bufpos;
2886
2887 if (it->current.overlay_string_index >= 0)
2888 bufpos = IT_CHARPOS (*it);
2889 else
2890 bufpos = 0;
2891
2892 /* For strings from a buffer, i.e. overlay strings or strings
2893 from a `display' property, use the face at IT's current
2894 buffer position as the base face to merge with, so that
2895 overlay strings appear in the same face as surrounding
2896 text, unless they specify their own faces. */
2897 base_face_id = underlying_face_id (it);
2898
2899 new_face_id = face_at_string_position (it->w,
2900 it->string,
2901 IT_STRING_CHARPOS (*it),
2902 bufpos,
2903 it->region_beg_charpos,
2904 it->region_end_charpos,
2905 &next_stop,
2906 base_face_id, 0);
2907
2908 #if 0 /* This shouldn't be neccessary. Let's check it. */
2909 /* If IT is used to display a mode line we would really like to
2910 use the mode line face instead of the frame's default face. */
2911 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2912 && new_face_id == DEFAULT_FACE_ID)
2913 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2914 #endif
2915
2916 /* Is this a start of a run of characters with box? Caveat:
2917 this can be called for a freshly allocated iterator; face_id
2918 is -1 is this case. We know that the new face will not
2919 change until the next check pos, i.e. if the new face has a
2920 box, all characters up to that position will have a
2921 box. But, as usual, we don't know whether that position
2922 is really the end. */
2923 if (new_face_id != it->face_id)
2924 {
2925 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2926 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2927
2928 /* If new face has a box but old face hasn't, this is the
2929 start of a run of characters with box, i.e. it has a
2930 shadow on the left side. */
2931 it->start_of_box_run_p
2932 = new_face->box && (old_face == NULL || !old_face->box);
2933 it->face_box_p = new_face->box != FACE_NO_BOX;
2934 }
2935 }
2936
2937 it->face_id = new_face_id;
2938 return HANDLED_NORMALLY;
2939 }
2940
2941
2942 /* Return the ID of the face ``underlying'' IT's current position,
2943 which is in a string. If the iterator is associated with a
2944 buffer, return the face at IT's current buffer position.
2945 Otherwise, use the iterator's base_face_id. */
2946
2947 static int
2948 underlying_face_id (it)
2949 struct it *it;
2950 {
2951 int face_id = it->base_face_id, i;
2952
2953 xassert (STRINGP (it->string));
2954
2955 for (i = it->sp - 1; i >= 0; --i)
2956 if (NILP (it->stack[i].string))
2957 face_id = it->stack[i].face_id;
2958
2959 return face_id;
2960 }
2961
2962
2963 /* Compute the face one character before or after the current position
2964 of IT. BEFORE_P non-zero means get the face in front of IT's
2965 position. Value is the id of the face. */
2966
2967 static int
2968 face_before_or_after_it_pos (it, before_p)
2969 struct it *it;
2970 int before_p;
2971 {
2972 int face_id, limit;
2973 int next_check_charpos;
2974 struct text_pos pos;
2975
2976 xassert (it->s == NULL);
2977
2978 if (STRINGP (it->string))
2979 {
2980 int bufpos, base_face_id;
2981
2982 /* No face change past the end of the string (for the case
2983 we are padding with spaces). No face change before the
2984 string start. */
2985 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
2986 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2987 return it->face_id;
2988
2989 /* Set pos to the position before or after IT's current position. */
2990 if (before_p)
2991 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2992 else
2993 /* For composition, we must check the character after the
2994 composition. */
2995 pos = (it->what == IT_COMPOSITION
2996 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2997 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2998
2999 if (it->current.overlay_string_index >= 0)
3000 bufpos = IT_CHARPOS (*it);
3001 else
3002 bufpos = 0;
3003
3004 base_face_id = underlying_face_id (it);
3005
3006 /* Get the face for ASCII, or unibyte. */
3007 face_id = face_at_string_position (it->w,
3008 it->string,
3009 CHARPOS (pos),
3010 bufpos,
3011 it->region_beg_charpos,
3012 it->region_end_charpos,
3013 &next_check_charpos,
3014 base_face_id, 0);
3015
3016 /* Correct the face for charsets different from ASCII. Do it
3017 for the multibyte case only. The face returned above is
3018 suitable for unibyte text if IT->string is unibyte. */
3019 if (STRING_MULTIBYTE (it->string))
3020 {
3021 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3022 int rest = SBYTES (it->string) - BYTEPOS (pos);
3023 int c, len;
3024 struct face *face = FACE_FROM_ID (it->f, face_id);
3025
3026 c = string_char_and_length (p, rest, &len);
3027 face_id = FACE_FOR_CHAR (it->f, face, c);
3028 }
3029 }
3030 else
3031 {
3032 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3033 || (IT_CHARPOS (*it) <= BEGV && before_p))
3034 return it->face_id;
3035
3036 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3037 pos = it->current.pos;
3038
3039 if (before_p)
3040 DEC_TEXT_POS (pos, it->multibyte_p);
3041 else
3042 {
3043 if (it->what == IT_COMPOSITION)
3044 /* For composition, we must check the position after the
3045 composition. */
3046 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3047 else
3048 INC_TEXT_POS (pos, it->multibyte_p);
3049 }
3050
3051 /* Determine face for CHARSET_ASCII, or unibyte. */
3052 face_id = face_at_buffer_position (it->w,
3053 CHARPOS (pos),
3054 it->region_beg_charpos,
3055 it->region_end_charpos,
3056 &next_check_charpos,
3057 limit, 0);
3058
3059 /* Correct the face for charsets different from ASCII. Do it
3060 for the multibyte case only. The face returned above is
3061 suitable for unibyte text if current_buffer is unibyte. */
3062 if (it->multibyte_p)
3063 {
3064 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3065 struct face *face = FACE_FROM_ID (it->f, face_id);
3066 face_id = FACE_FOR_CHAR (it->f, face, c);
3067 }
3068 }
3069
3070 return face_id;
3071 }
3072
3073
3074 \f
3075 /***********************************************************************
3076 Invisible text
3077 ***********************************************************************/
3078
3079 /* Set up iterator IT from invisible properties at its current
3080 position. Called from handle_stop. */
3081
3082 static enum prop_handled
3083 handle_invisible_prop (it)
3084 struct it *it;
3085 {
3086 enum prop_handled handled = HANDLED_NORMALLY;
3087
3088 if (STRINGP (it->string))
3089 {
3090 extern Lisp_Object Qinvisible;
3091 Lisp_Object prop, end_charpos, limit, charpos;
3092
3093 /* Get the value of the invisible text property at the
3094 current position. Value will be nil if there is no such
3095 property. */
3096 charpos = make_number (IT_STRING_CHARPOS (*it));
3097 prop = Fget_text_property (charpos, Qinvisible, it->string);
3098
3099 if (!NILP (prop)
3100 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3101 {
3102 handled = HANDLED_RECOMPUTE_PROPS;
3103
3104 /* Get the position at which the next change of the
3105 invisible text property can be found in IT->string.
3106 Value will be nil if the property value is the same for
3107 all the rest of IT->string. */
3108 XSETINT (limit, SCHARS (it->string));
3109 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3110 it->string, limit);
3111
3112 /* Text at current position is invisible. The next
3113 change in the property is at position end_charpos.
3114 Move IT's current position to that position. */
3115 if (INTEGERP (end_charpos)
3116 && XFASTINT (end_charpos) < XFASTINT (limit))
3117 {
3118 struct text_pos old;
3119 old = it->current.string_pos;
3120 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3121 compute_string_pos (&it->current.string_pos, old, it->string);
3122 }
3123 else
3124 {
3125 /* The rest of the string is invisible. If this is an
3126 overlay string, proceed with the next overlay string
3127 or whatever comes and return a character from there. */
3128 if (it->current.overlay_string_index >= 0)
3129 {
3130 next_overlay_string (it);
3131 /* Don't check for overlay strings when we just
3132 finished processing them. */
3133 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3134 }
3135 else
3136 {
3137 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3138 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3139 }
3140 }
3141 }
3142 }
3143 else
3144 {
3145 int invis_p, newpos, next_stop, start_charpos;
3146 Lisp_Object pos, prop, overlay;
3147
3148 /* First of all, is there invisible text at this position? */
3149 start_charpos = IT_CHARPOS (*it);
3150 pos = make_number (IT_CHARPOS (*it));
3151 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3152 &overlay);
3153 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3154
3155 /* If we are on invisible text, skip over it. */
3156 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3157 {
3158 /* Record whether we have to display an ellipsis for the
3159 invisible text. */
3160 int display_ellipsis_p = invis_p == 2;
3161
3162 handled = HANDLED_RECOMPUTE_PROPS;
3163
3164 /* Loop skipping over invisible text. The loop is left at
3165 ZV or with IT on the first char being visible again. */
3166 do
3167 {
3168 /* Try to skip some invisible text. Return value is the
3169 position reached which can be equal to IT's position
3170 if there is nothing invisible here. This skips both
3171 over invisible text properties and overlays with
3172 invisible property. */
3173 newpos = skip_invisible (IT_CHARPOS (*it),
3174 &next_stop, ZV, it->window);
3175
3176 /* If we skipped nothing at all we weren't at invisible
3177 text in the first place. If everything to the end of
3178 the buffer was skipped, end the loop. */
3179 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3180 invis_p = 0;
3181 else
3182 {
3183 /* We skipped some characters but not necessarily
3184 all there are. Check if we ended up on visible
3185 text. Fget_char_property returns the property of
3186 the char before the given position, i.e. if we
3187 get invis_p = 0, this means that the char at
3188 newpos is visible. */
3189 pos = make_number (newpos);
3190 prop = Fget_char_property (pos, Qinvisible, it->window);
3191 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3192 }
3193
3194 /* If we ended up on invisible text, proceed to
3195 skip starting with next_stop. */
3196 if (invis_p)
3197 IT_CHARPOS (*it) = next_stop;
3198 }
3199 while (invis_p);
3200
3201 /* The position newpos is now either ZV or on visible text. */
3202 IT_CHARPOS (*it) = newpos;
3203 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3204
3205 /* If there are before-strings at the start of invisible
3206 text, and the text is invisible because of a text
3207 property, arrange to show before-strings because 20.x did
3208 it that way. (If the text is invisible because of an
3209 overlay property instead of a text property, this is
3210 already handled in the overlay code.) */
3211 if (NILP (overlay)
3212 && get_overlay_strings (it, start_charpos))
3213 {
3214 handled = HANDLED_RECOMPUTE_PROPS;
3215 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3216 }
3217 else if (display_ellipsis_p)
3218 setup_for_ellipsis (it);
3219 }
3220 }
3221
3222 return handled;
3223 }
3224
3225
3226 /* Make iterator IT return `...' next. */
3227
3228 static void
3229 setup_for_ellipsis (it)
3230 struct it *it;
3231 {
3232 if (it->dp
3233 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3234 {
3235 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3236 it->dpvec = v->contents;
3237 it->dpend = v->contents + v->size;
3238 }
3239 else
3240 {
3241 /* Default `...'. */
3242 it->dpvec = default_invis_vector;
3243 it->dpend = default_invis_vector + 3;
3244 }
3245
3246 /* The ellipsis display does not replace the display of the
3247 character at the new position. Indicate this by setting
3248 IT->dpvec_char_len to zero. */
3249 it->dpvec_char_len = 0;
3250
3251 it->current.dpvec_index = 0;
3252 it->method = next_element_from_display_vector;
3253 }
3254
3255
3256 \f
3257 /***********************************************************************
3258 'display' property
3259 ***********************************************************************/
3260
3261 /* Set up iterator IT from `display' property at its current position.
3262 Called from handle_stop. */
3263
3264 static enum prop_handled
3265 handle_display_prop (it)
3266 struct it *it;
3267 {
3268 Lisp_Object prop, object;
3269 struct text_pos *position;
3270 int display_replaced_p = 0;
3271
3272 if (STRINGP (it->string))
3273 {
3274 object = it->string;
3275 position = &it->current.string_pos;
3276 }
3277 else
3278 {
3279 object = it->w->buffer;
3280 position = &it->current.pos;
3281 }
3282
3283 /* Reset those iterator values set from display property values. */
3284 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3285 it->space_width = Qnil;
3286 it->font_height = Qnil;
3287 it->voffset = 0;
3288
3289 /* We don't support recursive `display' properties, i.e. string
3290 values that have a string `display' property, that have a string
3291 `display' property etc. */
3292 if (!it->string_from_display_prop_p)
3293 it->area = TEXT_AREA;
3294
3295 prop = Fget_char_property (make_number (position->charpos),
3296 Qdisplay, object);
3297 if (NILP (prop))
3298 return HANDLED_NORMALLY;
3299
3300 if (CONSP (prop)
3301 /* Simple properties. */
3302 && !EQ (XCAR (prop), Qimage)
3303 && !EQ (XCAR (prop), Qspace)
3304 && !EQ (XCAR (prop), Qwhen)
3305 && !EQ (XCAR (prop), Qslice)
3306 && !EQ (XCAR (prop), Qspace_width)
3307 && !EQ (XCAR (prop), Qheight)
3308 && !EQ (XCAR (prop), Qraise)
3309 /* Marginal area specifications. */
3310 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3311 && !EQ (XCAR (prop), Qleft_fringe)
3312 && !EQ (XCAR (prop), Qright_fringe)
3313 && !NILP (XCAR (prop)))
3314 {
3315 for (; CONSP (prop); prop = XCDR (prop))
3316 {
3317 if (handle_single_display_prop (it, XCAR (prop), object,
3318 position, display_replaced_p))
3319 display_replaced_p = 1;
3320 }
3321 }
3322 else if (VECTORP (prop))
3323 {
3324 int i;
3325 for (i = 0; i < ASIZE (prop); ++i)
3326 if (handle_single_display_prop (it, AREF (prop, i), object,
3327 position, display_replaced_p))
3328 display_replaced_p = 1;
3329 }
3330 else
3331 {
3332 if (handle_single_display_prop (it, prop, object, position, 0))
3333 display_replaced_p = 1;
3334 }
3335
3336 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3337 }
3338
3339
3340 /* Value is the position of the end of the `display' property starting
3341 at START_POS in OBJECT. */
3342
3343 static struct text_pos
3344 display_prop_end (it, object, start_pos)
3345 struct it *it;
3346 Lisp_Object object;
3347 struct text_pos start_pos;
3348 {
3349 Lisp_Object end;
3350 struct text_pos end_pos;
3351
3352 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3353 Qdisplay, object, Qnil);
3354 CHARPOS (end_pos) = XFASTINT (end);
3355 if (STRINGP (object))
3356 compute_string_pos (&end_pos, start_pos, it->string);
3357 else
3358 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3359
3360 return end_pos;
3361 }
3362
3363
3364 /* Set up IT from a single `display' sub-property value PROP. OBJECT
3365 is the object in which the `display' property was found. *POSITION
3366 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3367 means that we previously saw a display sub-property which already
3368 replaced text display with something else, for example an image;
3369 ignore such properties after the first one has been processed.
3370
3371 If PROP is a `space' or `image' sub-property, set *POSITION to the
3372 end position of the `display' property.
3373
3374 Value is non-zero if something was found which replaces the display
3375 of buffer or string text. */
3376
3377 static int
3378 handle_single_display_prop (it, prop, object, position,
3379 display_replaced_before_p)
3380 struct it *it;
3381 Lisp_Object prop;
3382 Lisp_Object object;
3383 struct text_pos *position;
3384 int display_replaced_before_p;
3385 {
3386 Lisp_Object value;
3387 int replaces_text_display_p = 0;
3388 Lisp_Object form;
3389
3390 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
3391 evaluated. If the result is nil, VALUE is ignored. */
3392 form = Qt;
3393 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3394 {
3395 prop = XCDR (prop);
3396 if (!CONSP (prop))
3397 return 0;
3398 form = XCAR (prop);
3399 prop = XCDR (prop);
3400 }
3401
3402 if (!NILP (form) && !EQ (form, Qt))
3403 {
3404 int count = SPECPDL_INDEX ();
3405 struct gcpro gcpro1;
3406
3407 /* Bind `object' to the object having the `display' property, a
3408 buffer or string. Bind `position' to the position in the
3409 object where the property was found, and `buffer-position'
3410 to the current position in the buffer. */
3411 specbind (Qobject, object);
3412 specbind (Qposition, make_number (CHARPOS (*position)));
3413 specbind (Qbuffer_position,
3414 make_number (STRINGP (object)
3415 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3416 GCPRO1 (form);
3417 form = safe_eval (form);
3418 UNGCPRO;
3419 unbind_to (count, Qnil);
3420 }
3421
3422 if (NILP (form))
3423 return 0;
3424
3425 if (CONSP (prop)
3426 && EQ (XCAR (prop), Qheight)
3427 && CONSP (XCDR (prop)))
3428 {
3429 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3430 return 0;
3431
3432 /* `(height HEIGHT)'. */
3433 it->font_height = XCAR (XCDR (prop));
3434 if (!NILP (it->font_height))
3435 {
3436 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3437 int new_height = -1;
3438
3439 if (CONSP (it->font_height)
3440 && (EQ (XCAR (it->font_height), Qplus)
3441 || EQ (XCAR (it->font_height), Qminus))
3442 && CONSP (XCDR (it->font_height))
3443 && INTEGERP (XCAR (XCDR (it->font_height))))
3444 {
3445 /* `(+ N)' or `(- N)' where N is an integer. */
3446 int steps = XINT (XCAR (XCDR (it->font_height)));
3447 if (EQ (XCAR (it->font_height), Qplus))
3448 steps = - steps;
3449 it->face_id = smaller_face (it->f, it->face_id, steps);
3450 }
3451 else if (FUNCTIONP (it->font_height))
3452 {
3453 /* Call function with current height as argument.
3454 Value is the new height. */
3455 Lisp_Object height;
3456 height = safe_call1 (it->font_height,
3457 face->lface[LFACE_HEIGHT_INDEX]);
3458 if (NUMBERP (height))
3459 new_height = XFLOATINT (height);
3460 }
3461 else if (NUMBERP (it->font_height))
3462 {
3463 /* Value is a multiple of the canonical char height. */
3464 struct face *face;
3465
3466 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3467 new_height = (XFLOATINT (it->font_height)
3468 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3469 }
3470 else
3471 {
3472 /* Evaluate IT->font_height with `height' bound to the
3473 current specified height to get the new height. */
3474 Lisp_Object value;
3475 int count = SPECPDL_INDEX ();
3476
3477 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3478 value = safe_eval (it->font_height);
3479 unbind_to (count, Qnil);
3480
3481 if (NUMBERP (value))
3482 new_height = XFLOATINT (value);
3483 }
3484
3485 if (new_height > 0)
3486 it->face_id = face_with_height (it->f, it->face_id, new_height);
3487 }
3488 }
3489 else if (CONSP (prop)
3490 && EQ (XCAR (prop), Qspace_width)
3491 && CONSP (XCDR (prop)))
3492 {
3493 /* `(space_width WIDTH)'. */
3494 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3495 return 0;
3496
3497 value = XCAR (XCDR (prop));
3498 if (NUMBERP (value) && XFLOATINT (value) > 0)
3499 it->space_width = value;
3500 }
3501 else if (CONSP (prop)
3502 && EQ (XCAR (prop), Qslice))
3503 {
3504 /* `(slice X Y WIDTH HEIGHT)'. */
3505 Lisp_Object tem;
3506
3507 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3508 return 0;
3509
3510 if (tem = XCDR (prop), CONSP (tem))
3511 {
3512 it->slice.x = XCAR (tem);
3513 if (tem = XCDR (tem), CONSP (tem))
3514 {
3515 it->slice.y = XCAR (tem);
3516 if (tem = XCDR (tem), CONSP (tem))
3517 {
3518 it->slice.width = XCAR (tem);
3519 if (tem = XCDR (tem), CONSP (tem))
3520 it->slice.height = XCAR (tem);
3521 }
3522 }
3523 }
3524 }
3525 else if (CONSP (prop)
3526 && EQ (XCAR (prop), Qraise)
3527 && CONSP (XCDR (prop)))
3528 {
3529 /* `(raise FACTOR)'. */
3530 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3531 return 0;
3532
3533 #ifdef HAVE_WINDOW_SYSTEM
3534 value = XCAR (XCDR (prop));
3535 if (NUMBERP (value))
3536 {
3537 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3538 it->voffset = - (XFLOATINT (value)
3539 * (FONT_HEIGHT (face->font)));
3540 }
3541 #endif /* HAVE_WINDOW_SYSTEM */
3542 }
3543 else if (!it->string_from_display_prop_p)
3544 {
3545 /* `((margin left-margin) VALUE)' or `((margin right-margin)
3546 VALUE) or `((margin nil) VALUE)' or VALUE. */
3547 Lisp_Object location, value;
3548 struct text_pos start_pos;
3549 int valid_p;
3550
3551 /* Characters having this form of property are not displayed, so
3552 we have to find the end of the property. */
3553 start_pos = *position;
3554 *position = display_prop_end (it, object, start_pos);
3555 value = Qnil;
3556
3557 /* Let's stop at the new position and assume that all
3558 text properties change there. */
3559 it->stop_charpos = position->charpos;
3560
3561 if (CONSP (prop)
3562 && (EQ (XCAR (prop), Qleft_fringe)
3563 || EQ (XCAR (prop), Qright_fringe))
3564 && CONSP (XCDR (prop)))
3565 {
3566 unsigned face_id = DEFAULT_FACE_ID;
3567
3568 /* Save current settings of IT so that we can restore them
3569 when we are finished with the glyph property value. */
3570
3571 /* `(left-fringe BITMAP FACE)'. */
3572 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3573 return 0;
3574
3575 #ifdef HAVE_WINDOW_SYSTEM
3576 value = XCAR (XCDR (prop));
3577 if (!NUMBERP (value)
3578 || !valid_fringe_bitmap_id_p (XINT (value)))
3579 return 0;
3580
3581 if (CONSP (XCDR (XCDR (prop))))
3582 {
3583 Lisp_Object face_name = XCAR (XCDR (XCDR (prop)));
3584
3585 face_id = lookup_named_face (it->f, face_name, 'A');
3586 if (face_id < 0)
3587 return 0;
3588 }
3589
3590 push_it (it);
3591
3592 it->area = TEXT_AREA;
3593 it->what = IT_IMAGE;
3594 it->image_id = -1; /* no image */
3595 it->position = start_pos;
3596 it->object = NILP (object) ? it->w->buffer : object;
3597 it->method = next_element_from_image;
3598 it->face_id = face_id;
3599
3600 /* Say that we haven't consumed the characters with
3601 `display' property yet. The call to pop_it in
3602 set_iterator_to_next will clean this up. */
3603 *position = start_pos;
3604
3605 if (EQ (XCAR (prop), Qleft_fringe))
3606 {
3607 it->left_user_fringe_bitmap = XINT (value);
3608 it->left_user_fringe_face_id = face_id;
3609 }
3610 else
3611 {
3612 it->right_user_fringe_bitmap = XINT (value);
3613 it->right_user_fringe_face_id = face_id;
3614 }
3615 #endif /* HAVE_WINDOW_SYSTEM */
3616 return 1;
3617 }
3618
3619 location = Qunbound;
3620 if (CONSP (prop) && CONSP (XCAR (prop)))
3621 {
3622 Lisp_Object tem;
3623
3624 value = XCDR (prop);
3625 if (CONSP (value))
3626 value = XCAR (value);
3627
3628 tem = XCAR (prop);
3629 if (EQ (XCAR (tem), Qmargin)
3630 && (tem = XCDR (tem),
3631 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3632 (NILP (tem)
3633 || EQ (tem, Qleft_margin)
3634 || EQ (tem, Qright_margin))))
3635 location = tem;
3636 }
3637
3638 if (EQ (location, Qunbound))
3639 {
3640 location = Qnil;
3641 value = prop;
3642 }
3643
3644 valid_p = (STRINGP (value)
3645 #ifdef HAVE_WINDOW_SYSTEM
3646 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
3647 #endif /* not HAVE_WINDOW_SYSTEM */
3648 || (CONSP (value) && EQ (XCAR (value), Qspace)));
3649
3650 if ((EQ (location, Qleft_margin)
3651 || EQ (location, Qright_margin)
3652 || NILP (location))
3653 && valid_p
3654 && !display_replaced_before_p)
3655 {
3656 replaces_text_display_p = 1;
3657
3658 /* Save current settings of IT so that we can restore them
3659 when we are finished with the glyph property value. */
3660 push_it (it);
3661
3662 if (NILP (location))
3663 it->area = TEXT_AREA;
3664 else if (EQ (location, Qleft_margin))
3665 it->area = LEFT_MARGIN_AREA;
3666 else
3667 it->area = RIGHT_MARGIN_AREA;
3668
3669 if (STRINGP (value))
3670 {
3671 it->string = value;
3672 it->multibyte_p = STRING_MULTIBYTE (it->string);
3673 it->current.overlay_string_index = -1;
3674 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3675 it->end_charpos = it->string_nchars = SCHARS (it->string);
3676 it->method = next_element_from_string;
3677 it->stop_charpos = 0;
3678 it->string_from_display_prop_p = 1;
3679 /* Say that we haven't consumed the characters with
3680 `display' property yet. The call to pop_it in
3681 set_iterator_to_next will clean this up. */
3682 *position = start_pos;
3683 }
3684 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3685 {
3686 it->method = next_element_from_stretch;
3687 it->object = value;
3688 it->current.pos = it->position = start_pos;
3689 }
3690 #ifdef HAVE_WINDOW_SYSTEM
3691 else
3692 {
3693 it->what = IT_IMAGE;
3694 it->image_id = lookup_image (it->f, value);
3695 it->position = start_pos;
3696 it->object = NILP (object) ? it->w->buffer : object;
3697 it->method = next_element_from_image;
3698
3699 /* Say that we haven't consumed the characters with
3700 `display' property yet. The call to pop_it in
3701 set_iterator_to_next will clean this up. */
3702 *position = start_pos;
3703 }
3704 #endif /* HAVE_WINDOW_SYSTEM */
3705 }
3706 else
3707 /* Invalid property or property not supported. Restore
3708 the position to what it was before. */
3709 *position = start_pos;
3710 }
3711
3712 return replaces_text_display_p;
3713 }
3714
3715
3716 /* Check if PROP is a display sub-property value whose text should be
3717 treated as intangible. */
3718
3719 static int
3720 single_display_prop_intangible_p (prop)
3721 Lisp_Object prop;
3722 {
3723 /* Skip over `when FORM'. */
3724 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3725 {
3726 prop = XCDR (prop);
3727 if (!CONSP (prop))
3728 return 0;
3729 prop = XCDR (prop);
3730 }
3731
3732 if (STRINGP (prop))
3733 return 1;
3734
3735 if (!CONSP (prop))
3736 return 0;
3737
3738 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3739 we don't need to treat text as intangible. */
3740 if (EQ (XCAR (prop), Qmargin))
3741 {
3742 prop = XCDR (prop);
3743 if (!CONSP (prop))
3744 return 0;
3745
3746 prop = XCDR (prop);
3747 if (!CONSP (prop)
3748 || EQ (XCAR (prop), Qleft_margin)
3749 || EQ (XCAR (prop), Qright_margin))
3750 return 0;
3751 }
3752
3753 return (CONSP (prop)
3754 && (EQ (XCAR (prop), Qimage)
3755 || EQ (XCAR (prop), Qspace)));
3756 }
3757
3758
3759 /* Check if PROP is a display property value whose text should be
3760 treated as intangible. */
3761
3762 int
3763 display_prop_intangible_p (prop)
3764 Lisp_Object prop;
3765 {
3766 if (CONSP (prop)
3767 && CONSP (XCAR (prop))
3768 && !EQ (Qmargin, XCAR (XCAR (prop))))
3769 {
3770 /* A list of sub-properties. */
3771 while (CONSP (prop))
3772 {
3773 if (single_display_prop_intangible_p (XCAR (prop)))
3774 return 1;
3775 prop = XCDR (prop);
3776 }
3777 }
3778 else if (VECTORP (prop))
3779 {
3780 /* A vector of sub-properties. */
3781 int i;
3782 for (i = 0; i < ASIZE (prop); ++i)
3783 if (single_display_prop_intangible_p (AREF (prop, i)))
3784 return 1;
3785 }
3786 else
3787 return single_display_prop_intangible_p (prop);
3788
3789 return 0;
3790 }
3791
3792
3793 /* Return 1 if PROP is a display sub-property value containing STRING. */
3794
3795 static int
3796 single_display_prop_string_p (prop, string)
3797 Lisp_Object prop, string;
3798 {
3799 if (EQ (string, prop))
3800 return 1;
3801
3802 /* Skip over `when FORM'. */
3803 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3804 {
3805 prop = XCDR (prop);
3806 if (!CONSP (prop))
3807 return 0;
3808 prop = XCDR (prop);
3809 }
3810
3811 if (CONSP (prop))
3812 /* Skip over `margin LOCATION'. */
3813 if (EQ (XCAR (prop), Qmargin))
3814 {
3815 prop = XCDR (prop);
3816 if (!CONSP (prop))
3817 return 0;
3818
3819 prop = XCDR (prop);
3820 if (!CONSP (prop))
3821 return 0;
3822 }
3823
3824 return CONSP (prop) && EQ (XCAR (prop), string);
3825 }
3826
3827
3828 /* Return 1 if STRING appears in the `display' property PROP. */
3829
3830 static int
3831 display_prop_string_p (prop, string)
3832 Lisp_Object prop, string;
3833 {
3834 if (CONSP (prop)
3835 && CONSP (XCAR (prop))
3836 && !EQ (Qmargin, XCAR (XCAR (prop))))
3837 {
3838 /* A list of sub-properties. */
3839 while (CONSP (prop))
3840 {
3841 if (single_display_prop_string_p (XCAR (prop), string))
3842 return 1;
3843 prop = XCDR (prop);
3844 }
3845 }
3846 else if (VECTORP (prop))
3847 {
3848 /* A vector of sub-properties. */
3849 int i;
3850 for (i = 0; i < ASIZE (prop); ++i)
3851 if (single_display_prop_string_p (AREF (prop, i), string))
3852 return 1;
3853 }
3854 else
3855 return single_display_prop_string_p (prop, string);
3856
3857 return 0;
3858 }
3859
3860
3861 /* Determine from which buffer position in W's buffer STRING comes
3862 from. AROUND_CHARPOS is an approximate position where it could
3863 be from. Value is the buffer position or 0 if it couldn't be
3864 determined.
3865
3866 W's buffer must be current.
3867
3868 This function is necessary because we don't record buffer positions
3869 in glyphs generated from strings (to keep struct glyph small).
3870 This function may only use code that doesn't eval because it is
3871 called asynchronously from note_mouse_highlight. */
3872
3873 int
3874 string_buffer_position (w, string, around_charpos)
3875 struct window *w;
3876 Lisp_Object string;
3877 int around_charpos;
3878 {
3879 Lisp_Object limit, prop, pos;
3880 const int MAX_DISTANCE = 1000;
3881 int found = 0;
3882
3883 pos = make_number (around_charpos);
3884 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3885 while (!found && !EQ (pos, limit))
3886 {
3887 prop = Fget_char_property (pos, Qdisplay, Qnil);
3888 if (!NILP (prop) && display_prop_string_p (prop, string))
3889 found = 1;
3890 else
3891 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3892 }
3893
3894 if (!found)
3895 {
3896 pos = make_number (around_charpos);
3897 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3898 while (!found && !EQ (pos, limit))
3899 {
3900 prop = Fget_char_property (pos, Qdisplay, Qnil);
3901 if (!NILP (prop) && display_prop_string_p (prop, string))
3902 found = 1;
3903 else
3904 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3905 limit);
3906 }
3907 }
3908
3909 return found ? XINT (pos) : 0;
3910 }
3911
3912
3913 \f
3914 /***********************************************************************
3915 `composition' property
3916 ***********************************************************************/
3917
3918 /* Set up iterator IT from `composition' property at its current
3919 position. Called from handle_stop. */
3920
3921 static enum prop_handled
3922 handle_composition_prop (it)
3923 struct it *it;
3924 {
3925 Lisp_Object prop, string;
3926 int pos, pos_byte, end;
3927 enum prop_handled handled = HANDLED_NORMALLY;
3928
3929 if (STRINGP (it->string))
3930 {
3931 pos = IT_STRING_CHARPOS (*it);
3932 pos_byte = IT_STRING_BYTEPOS (*it);
3933 string = it->string;
3934 }
3935 else
3936 {
3937 pos = IT_CHARPOS (*it);
3938 pos_byte = IT_BYTEPOS (*it);
3939 string = Qnil;
3940 }
3941
3942 /* If there's a valid composition and point is not inside of the
3943 composition (in the case that the composition is from the current
3944 buffer), draw a glyph composed from the composition components. */
3945 if (find_composition (pos, -1, &pos, &end, &prop, string)
3946 && COMPOSITION_VALID_P (pos, end, prop)
3947 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3948 {
3949 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3950
3951 if (id >= 0)
3952 {
3953 it->method = next_element_from_composition;
3954 it->cmp_id = id;
3955 it->cmp_len = COMPOSITION_LENGTH (prop);
3956 /* For a terminal, draw only the first character of the
3957 components. */
3958 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3959 it->len = (STRINGP (it->string)
3960 ? string_char_to_byte (it->string, end)
3961 : CHAR_TO_BYTE (end)) - pos_byte;
3962 it->stop_charpos = end;
3963 handled = HANDLED_RETURN;
3964 }
3965 }
3966
3967 return handled;
3968 }
3969
3970
3971 \f
3972 /***********************************************************************
3973 Overlay strings
3974 ***********************************************************************/
3975
3976 /* The following structure is used to record overlay strings for
3977 later sorting in load_overlay_strings. */
3978
3979 struct overlay_entry
3980 {
3981 Lisp_Object overlay;
3982 Lisp_Object string;
3983 int priority;
3984 int after_string_p;
3985 };
3986
3987
3988 /* Set up iterator IT from overlay strings at its current position.
3989 Called from handle_stop. */
3990
3991 static enum prop_handled
3992 handle_overlay_change (it)
3993 struct it *it;
3994 {
3995 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3996 return HANDLED_RECOMPUTE_PROPS;
3997 else
3998 return HANDLED_NORMALLY;
3999 }
4000
4001
4002 /* Set up the next overlay string for delivery by IT, if there is an
4003 overlay string to deliver. Called by set_iterator_to_next when the
4004 end of the current overlay string is reached. If there are more
4005 overlay strings to display, IT->string and
4006 IT->current.overlay_string_index are set appropriately here.
4007 Otherwise IT->string is set to nil. */
4008
4009 static void
4010 next_overlay_string (it)
4011 struct it *it;
4012 {
4013 ++it->current.overlay_string_index;
4014 if (it->current.overlay_string_index == it->n_overlay_strings)
4015 {
4016 /* No more overlay strings. Restore IT's settings to what
4017 they were before overlay strings were processed, and
4018 continue to deliver from current_buffer. */
4019 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4020
4021 pop_it (it);
4022 xassert (it->stop_charpos >= BEGV
4023 && it->stop_charpos <= it->end_charpos);
4024 it->string = Qnil;
4025 it->current.overlay_string_index = -1;
4026 SET_TEXT_POS (it->current.string_pos, -1, -1);
4027 it->n_overlay_strings = 0;
4028 it->method = next_element_from_buffer;
4029
4030 /* If we're at the end of the buffer, record that we have
4031 processed the overlay strings there already, so that
4032 next_element_from_buffer doesn't try it again. */
4033 if (IT_CHARPOS (*it) >= it->end_charpos)
4034 it->overlay_strings_at_end_processed_p = 1;
4035
4036 /* If we have to display `...' for invisible text, set
4037 the iterator up for that. */
4038 if (display_ellipsis_p)
4039 setup_for_ellipsis (it);
4040 }
4041 else
4042 {
4043 /* There are more overlay strings to process. If
4044 IT->current.overlay_string_index has advanced to a position
4045 where we must load IT->overlay_strings with more strings, do
4046 it. */
4047 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4048
4049 if (it->current.overlay_string_index && i == 0)
4050 load_overlay_strings (it, 0);
4051
4052 /* Initialize IT to deliver display elements from the overlay
4053 string. */
4054 it->string = it->overlay_strings[i];
4055 it->multibyte_p = STRING_MULTIBYTE (it->string);
4056 SET_TEXT_POS (it->current.string_pos, 0, 0);
4057 it->method = next_element_from_string;
4058 it->stop_charpos = 0;
4059 }
4060
4061 CHECK_IT (it);
4062 }
4063
4064
4065 /* Compare two overlay_entry structures E1 and E2. Used as a
4066 comparison function for qsort in load_overlay_strings. Overlay
4067 strings for the same position are sorted so that
4068
4069 1. All after-strings come in front of before-strings, except
4070 when they come from the same overlay.
4071
4072 2. Within after-strings, strings are sorted so that overlay strings
4073 from overlays with higher priorities come first.
4074
4075 2. Within before-strings, strings are sorted so that overlay
4076 strings from overlays with higher priorities come last.
4077
4078 Value is analogous to strcmp. */
4079
4080
4081 static int
4082 compare_overlay_entries (e1, e2)
4083 void *e1, *e2;
4084 {
4085 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4086 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4087 int result;
4088
4089 if (entry1->after_string_p != entry2->after_string_p)
4090 {
4091 /* Let after-strings appear in front of before-strings if
4092 they come from different overlays. */
4093 if (EQ (entry1->overlay, entry2->overlay))
4094 result = entry1->after_string_p ? 1 : -1;
4095 else
4096 result = entry1->after_string_p ? -1 : 1;
4097 }
4098 else if (entry1->after_string_p)
4099 /* After-strings sorted in order of decreasing priority. */
4100 result = entry2->priority - entry1->priority;
4101 else
4102 /* Before-strings sorted in order of increasing priority. */
4103 result = entry1->priority - entry2->priority;
4104
4105 return result;
4106 }
4107
4108
4109 /* Load the vector IT->overlay_strings with overlay strings from IT's
4110 current buffer position, or from CHARPOS if that is > 0. Set
4111 IT->n_overlays to the total number of overlay strings found.
4112
4113 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4114 a time. On entry into load_overlay_strings,
4115 IT->current.overlay_string_index gives the number of overlay
4116 strings that have already been loaded by previous calls to this
4117 function.
4118
4119 IT->add_overlay_start contains an additional overlay start
4120 position to consider for taking overlay strings from, if non-zero.
4121 This position comes into play when the overlay has an `invisible'
4122 property, and both before and after-strings. When we've skipped to
4123 the end of the overlay, because of its `invisible' property, we
4124 nevertheless want its before-string to appear.
4125 IT->add_overlay_start will contain the overlay start position
4126 in this case.
4127
4128 Overlay strings are sorted so that after-string strings come in
4129 front of before-string strings. Within before and after-strings,
4130 strings are sorted by overlay priority. See also function
4131 compare_overlay_entries. */
4132
4133 static void
4134 load_overlay_strings (it, charpos)
4135 struct it *it;
4136 int charpos;
4137 {
4138 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4139 Lisp_Object overlay, window, str, invisible;
4140 struct Lisp_Overlay *ov;
4141 int start, end;
4142 int size = 20;
4143 int n = 0, i, j, invis_p;
4144 struct overlay_entry *entries
4145 = (struct overlay_entry *) alloca (size * sizeof *entries);
4146
4147 if (charpos <= 0)
4148 charpos = IT_CHARPOS (*it);
4149
4150 /* Append the overlay string STRING of overlay OVERLAY to vector
4151 `entries' which has size `size' and currently contains `n'
4152 elements. AFTER_P non-zero means STRING is an after-string of
4153 OVERLAY. */
4154 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4155 do \
4156 { \
4157 Lisp_Object priority; \
4158 \
4159 if (n == size) \
4160 { \
4161 int new_size = 2 * size; \
4162 struct overlay_entry *old = entries; \
4163 entries = \
4164 (struct overlay_entry *) alloca (new_size \
4165 * sizeof *entries); \
4166 bcopy (old, entries, size * sizeof *entries); \
4167 size = new_size; \
4168 } \
4169 \
4170 entries[n].string = (STRING); \
4171 entries[n].overlay = (OVERLAY); \
4172 priority = Foverlay_get ((OVERLAY), Qpriority); \
4173 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4174 entries[n].after_string_p = (AFTER_P); \
4175 ++n; \
4176 } \
4177 while (0)
4178
4179 /* Process overlay before the overlay center. */
4180 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4181 {
4182 XSETMISC (overlay, ov);
4183 xassert (OVERLAYP (overlay));
4184 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4185 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4186
4187 if (end < charpos)
4188 break;
4189
4190 /* Skip this overlay if it doesn't start or end at IT's current
4191 position. */
4192 if (end != charpos && start != charpos)
4193 continue;
4194
4195 /* Skip this overlay if it doesn't apply to IT->w. */
4196 window = Foverlay_get (overlay, Qwindow);
4197 if (WINDOWP (window) && XWINDOW (window) != it->w)
4198 continue;
4199
4200 /* If the text ``under'' the overlay is invisible, both before-
4201 and after-strings from this overlay are visible; start and
4202 end position are indistinguishable. */
4203 invisible = Foverlay_get (overlay, Qinvisible);
4204 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4205
4206 /* If overlay has a non-empty before-string, record it. */
4207 if ((start == charpos || (end == charpos && invis_p))
4208 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4209 && SCHARS (str))
4210 RECORD_OVERLAY_STRING (overlay, str, 0);
4211
4212 /* If overlay has a non-empty after-string, record it. */
4213 if ((end == charpos || (start == charpos && invis_p))
4214 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4215 && SCHARS (str))
4216 RECORD_OVERLAY_STRING (overlay, str, 1);
4217 }
4218
4219 /* Process overlays after the overlay center. */
4220 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4221 {
4222 XSETMISC (overlay, ov);
4223 xassert (OVERLAYP (overlay));
4224 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4225 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4226
4227 if (start > charpos)
4228 break;
4229
4230 /* Skip this overlay if it doesn't start or end at IT's current
4231 position. */
4232 if (end != charpos && start != charpos)
4233 continue;
4234
4235 /* Skip this overlay if it doesn't apply to IT->w. */
4236 window = Foverlay_get (overlay, Qwindow);
4237 if (WINDOWP (window) && XWINDOW (window) != it->w)
4238 continue;
4239
4240 /* If the text ``under'' the overlay is invisible, it has a zero
4241 dimension, and both before- and after-strings apply. */
4242 invisible = Foverlay_get (overlay, Qinvisible);
4243 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4244
4245 /* If overlay has a non-empty before-string, record it. */
4246 if ((start == charpos || (end == charpos && invis_p))
4247 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4248 && SCHARS (str))
4249 RECORD_OVERLAY_STRING (overlay, str, 0);
4250
4251 /* If overlay has a non-empty after-string, record it. */
4252 if ((end == charpos || (start == charpos && invis_p))
4253 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4254 && SCHARS (str))
4255 RECORD_OVERLAY_STRING (overlay, str, 1);
4256 }
4257
4258 #undef RECORD_OVERLAY_STRING
4259
4260 /* Sort entries. */
4261 if (n > 1)
4262 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4263
4264 /* Record the total number of strings to process. */
4265 it->n_overlay_strings = n;
4266
4267 /* IT->current.overlay_string_index is the number of overlay strings
4268 that have already been consumed by IT. Copy some of the
4269 remaining overlay strings to IT->overlay_strings. */
4270 i = 0;
4271 j = it->current.overlay_string_index;
4272 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4273 it->overlay_strings[i++] = entries[j++].string;
4274
4275 CHECK_IT (it);
4276 }
4277
4278
4279 /* Get the first chunk of overlay strings at IT's current buffer
4280 position, or at CHARPOS if that is > 0. Value is non-zero if at
4281 least one overlay string was found. */
4282
4283 static int
4284 get_overlay_strings (it, charpos)
4285 struct it *it;
4286 int charpos;
4287 {
4288 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4289 process. This fills IT->overlay_strings with strings, and sets
4290 IT->n_overlay_strings to the total number of strings to process.
4291 IT->pos.overlay_string_index has to be set temporarily to zero
4292 because load_overlay_strings needs this; it must be set to -1
4293 when no overlay strings are found because a zero value would
4294 indicate a position in the first overlay string. */
4295 it->current.overlay_string_index = 0;
4296 load_overlay_strings (it, charpos);
4297
4298 /* If we found overlay strings, set up IT to deliver display
4299 elements from the first one. Otherwise set up IT to deliver
4300 from current_buffer. */
4301 if (it->n_overlay_strings)
4302 {
4303 /* Make sure we know settings in current_buffer, so that we can
4304 restore meaningful values when we're done with the overlay
4305 strings. */
4306 compute_stop_pos (it);
4307 xassert (it->face_id >= 0);
4308
4309 /* Save IT's settings. They are restored after all overlay
4310 strings have been processed. */
4311 xassert (it->sp == 0);
4312 push_it (it);
4313
4314 /* Set up IT to deliver display elements from the first overlay
4315 string. */
4316 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4317 it->string = it->overlay_strings[0];
4318 it->stop_charpos = 0;
4319 xassert (STRINGP (it->string));
4320 it->end_charpos = SCHARS (it->string);
4321 it->multibyte_p = STRING_MULTIBYTE (it->string);
4322 it->method = next_element_from_string;
4323 }
4324 else
4325 {
4326 it->string = Qnil;
4327 it->current.overlay_string_index = -1;
4328 it->method = next_element_from_buffer;
4329 }
4330
4331 CHECK_IT (it);
4332
4333 /* Value is non-zero if we found at least one overlay string. */
4334 return STRINGP (it->string);
4335 }
4336
4337
4338 \f
4339 /***********************************************************************
4340 Saving and restoring state
4341 ***********************************************************************/
4342
4343 /* Save current settings of IT on IT->stack. Called, for example,
4344 before setting up IT for an overlay string, to be able to restore
4345 IT's settings to what they were after the overlay string has been
4346 processed. */
4347
4348 static void
4349 push_it (it)
4350 struct it *it;
4351 {
4352 struct iterator_stack_entry *p;
4353
4354 xassert (it->sp < 2);
4355 p = it->stack + it->sp;
4356
4357 p->stop_charpos = it->stop_charpos;
4358 xassert (it->face_id >= 0);
4359 p->face_id = it->face_id;
4360 p->string = it->string;
4361 p->pos = it->current;
4362 p->end_charpos = it->end_charpos;
4363 p->string_nchars = it->string_nchars;
4364 p->area = it->area;
4365 p->multibyte_p = it->multibyte_p;
4366 p->slice = it->slice;
4367 p->space_width = it->space_width;
4368 p->font_height = it->font_height;
4369 p->voffset = it->voffset;
4370 p->string_from_display_prop_p = it->string_from_display_prop_p;
4371 p->display_ellipsis_p = 0;
4372 ++it->sp;
4373 }
4374
4375
4376 /* Restore IT's settings from IT->stack. Called, for example, when no
4377 more overlay strings must be processed, and we return to delivering
4378 display elements from a buffer, or when the end of a string from a
4379 `display' property is reached and we return to delivering display
4380 elements from an overlay string, or from a buffer. */
4381
4382 static void
4383 pop_it (it)
4384 struct it *it;
4385 {
4386 struct iterator_stack_entry *p;
4387
4388 xassert (it->sp > 0);
4389 --it->sp;
4390 p = it->stack + it->sp;
4391 it->stop_charpos = p->stop_charpos;
4392 it->face_id = p->face_id;
4393 it->string = p->string;
4394 it->current = p->pos;
4395 it->end_charpos = p->end_charpos;
4396 it->string_nchars = p->string_nchars;
4397 it->area = p->area;
4398 it->multibyte_p = p->multibyte_p;
4399 it->slice = p->slice;
4400 it->space_width = p->space_width;
4401 it->font_height = p->font_height;
4402 it->voffset = p->voffset;
4403 it->string_from_display_prop_p = p->string_from_display_prop_p;
4404 }
4405
4406
4407 \f
4408 /***********************************************************************
4409 Moving over lines
4410 ***********************************************************************/
4411
4412 /* Set IT's current position to the previous line start. */
4413
4414 static void
4415 back_to_previous_line_start (it)
4416 struct it *it;
4417 {
4418 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4419 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4420 }
4421
4422
4423 /* Move IT to the next line start.
4424
4425 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4426 we skipped over part of the text (as opposed to moving the iterator
4427 continuously over the text). Otherwise, don't change the value
4428 of *SKIPPED_P.
4429
4430 Newlines may come from buffer text, overlay strings, or strings
4431 displayed via the `display' property. That's the reason we can't
4432 simply use find_next_newline_no_quit.
4433
4434 Note that this function may not skip over invisible text that is so
4435 because of text properties and immediately follows a newline. If
4436 it would, function reseat_at_next_visible_line_start, when called
4437 from set_iterator_to_next, would effectively make invisible
4438 characters following a newline part of the wrong glyph row, which
4439 leads to wrong cursor motion. */
4440
4441 static int
4442 forward_to_next_line_start (it, skipped_p)
4443 struct it *it;
4444 int *skipped_p;
4445 {
4446 int old_selective, newline_found_p, n;
4447 const int MAX_NEWLINE_DISTANCE = 500;
4448
4449 /* If already on a newline, just consume it to avoid unintended
4450 skipping over invisible text below. */
4451 if (it->what == IT_CHARACTER
4452 && it->c == '\n'
4453 && CHARPOS (it->position) == IT_CHARPOS (*it))
4454 {
4455 set_iterator_to_next (it, 0);
4456 it->c = 0;
4457 return 1;
4458 }
4459
4460 /* Don't handle selective display in the following. It's (a)
4461 unnecessary because it's done by the caller, and (b) leads to an
4462 infinite recursion because next_element_from_ellipsis indirectly
4463 calls this function. */
4464 old_selective = it->selective;
4465 it->selective = 0;
4466
4467 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4468 from buffer text. */
4469 for (n = newline_found_p = 0;
4470 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4471 n += STRINGP (it->string) ? 0 : 1)
4472 {
4473 if (!get_next_display_element (it))
4474 return 0;
4475 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4476 set_iterator_to_next (it, 0);
4477 }
4478
4479 /* If we didn't find a newline near enough, see if we can use a
4480 short-cut. */
4481 if (!newline_found_p)
4482 {
4483 int start = IT_CHARPOS (*it);
4484 int limit = find_next_newline_no_quit (start, 1);
4485 Lisp_Object pos;
4486
4487 xassert (!STRINGP (it->string));
4488
4489 /* If there isn't any `display' property in sight, and no
4490 overlays, we can just use the position of the newline in
4491 buffer text. */
4492 if (it->stop_charpos >= limit
4493 || ((pos = Fnext_single_property_change (make_number (start),
4494 Qdisplay,
4495 Qnil, make_number (limit)),
4496 NILP (pos))
4497 && next_overlay_change (start) == ZV))
4498 {
4499 IT_CHARPOS (*it) = limit;
4500 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4501 *skipped_p = newline_found_p = 1;
4502 }
4503 else
4504 {
4505 while (get_next_display_element (it)
4506 && !newline_found_p)
4507 {
4508 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4509 set_iterator_to_next (it, 0);
4510 }
4511 }
4512 }
4513
4514 it->selective = old_selective;
4515 return newline_found_p;
4516 }
4517
4518
4519 /* Set IT's current position to the previous visible line start. Skip
4520 invisible text that is so either due to text properties or due to
4521 selective display. Caution: this does not change IT->current_x and
4522 IT->hpos. */
4523
4524 static void
4525 back_to_previous_visible_line_start (it)
4526 struct it *it;
4527 {
4528 int visible_p = 0;
4529
4530 /* Go back one newline if not on BEGV already. */
4531 if (IT_CHARPOS (*it) > BEGV)
4532 back_to_previous_line_start (it);
4533
4534 /* Move over lines that are invisible because of selective display
4535 or text properties. */
4536 while (IT_CHARPOS (*it) > BEGV
4537 && !visible_p)
4538 {
4539 visible_p = 1;
4540
4541 /* If selective > 0, then lines indented more than that values
4542 are invisible. */
4543 if (it->selective > 0
4544 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4545 (double) it->selective)) /* iftc */
4546 visible_p = 0;
4547 else
4548 {
4549 Lisp_Object prop;
4550
4551 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
4552 Qinvisible, it->window);
4553 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4554 visible_p = 0;
4555 }
4556
4557 if (visible_p)
4558 {
4559 struct it it2 = *it;
4560
4561 if (handle_display_prop (&it2) == HANDLED_RETURN)
4562 visible_p = 0;
4563 }
4564
4565 /* Back one more newline if the current one is invisible. */
4566 if (!visible_p)
4567 back_to_previous_line_start (it);
4568 }
4569
4570 xassert (IT_CHARPOS (*it) >= BEGV);
4571 xassert (IT_CHARPOS (*it) == BEGV
4572 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4573 CHECK_IT (it);
4574 }
4575
4576
4577 /* Reseat iterator IT at the previous visible line start. Skip
4578 invisible text that is so either due to text properties or due to
4579 selective display. At the end, update IT's overlay information,
4580 face information etc. */
4581
4582 static void
4583 reseat_at_previous_visible_line_start (it)
4584 struct it *it;
4585 {
4586 back_to_previous_visible_line_start (it);
4587 reseat (it, it->current.pos, 1);
4588 CHECK_IT (it);
4589 }
4590
4591
4592 /* Reseat iterator IT on the next visible line start in the current
4593 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4594 preceding the line start. Skip over invisible text that is so
4595 because of selective display. Compute faces, overlays etc at the
4596 new position. Note that this function does not skip over text that
4597 is invisible because of text properties. */
4598
4599 static void
4600 reseat_at_next_visible_line_start (it, on_newline_p)
4601 struct it *it;
4602 int on_newline_p;
4603 {
4604 int newline_found_p, skipped_p = 0;
4605
4606 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4607
4608 /* Skip over lines that are invisible because they are indented
4609 more than the value of IT->selective. */
4610 if (it->selective > 0)
4611 while (IT_CHARPOS (*it) < ZV
4612 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4613 (double) it->selective)) /* iftc */
4614 {
4615 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4616 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4617 }
4618
4619 /* Position on the newline if that's what's requested. */
4620 if (on_newline_p && newline_found_p)
4621 {
4622 if (STRINGP (it->string))
4623 {
4624 if (IT_STRING_CHARPOS (*it) > 0)
4625 {
4626 --IT_STRING_CHARPOS (*it);
4627 --IT_STRING_BYTEPOS (*it);
4628 }
4629 }
4630 else if (IT_CHARPOS (*it) > BEGV)
4631 {
4632 --IT_CHARPOS (*it);
4633 --IT_BYTEPOS (*it);
4634 reseat (it, it->current.pos, 0);
4635 }
4636 }
4637 else if (skipped_p)
4638 reseat (it, it->current.pos, 0);
4639
4640 CHECK_IT (it);
4641 }
4642
4643
4644 \f
4645 /***********************************************************************
4646 Changing an iterator's position
4647 ***********************************************************************/
4648
4649 /* Change IT's current position to POS in current_buffer. If FORCE_P
4650 is non-zero, always check for text properties at the new position.
4651 Otherwise, text properties are only looked up if POS >=
4652 IT->check_charpos of a property. */
4653
4654 static void
4655 reseat (it, pos, force_p)
4656 struct it *it;
4657 struct text_pos pos;
4658 int force_p;
4659 {
4660 int original_pos = IT_CHARPOS (*it);
4661
4662 reseat_1 (it, pos, 0);
4663
4664 /* Determine where to check text properties. Avoid doing it
4665 where possible because text property lookup is very expensive. */
4666 if (force_p
4667 || CHARPOS (pos) > it->stop_charpos
4668 || CHARPOS (pos) < original_pos)
4669 handle_stop (it);
4670
4671 CHECK_IT (it);
4672 }
4673
4674
4675 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4676 IT->stop_pos to POS, also. */
4677
4678 static void
4679 reseat_1 (it, pos, set_stop_p)
4680 struct it *it;
4681 struct text_pos pos;
4682 int set_stop_p;
4683 {
4684 /* Don't call this function when scanning a C string. */
4685 xassert (it->s == NULL);
4686
4687 /* POS must be a reasonable value. */
4688 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4689
4690 it->current.pos = it->position = pos;
4691 XSETBUFFER (it->object, current_buffer);
4692 it->end_charpos = ZV;
4693 it->dpvec = NULL;
4694 it->current.dpvec_index = -1;
4695 it->current.overlay_string_index = -1;
4696 IT_STRING_CHARPOS (*it) = -1;
4697 IT_STRING_BYTEPOS (*it) = -1;
4698 it->string = Qnil;
4699 it->method = next_element_from_buffer;
4700 /* RMS: I added this to fix a bug in move_it_vertically_backward
4701 where it->area continued to relate to the starting point
4702 for the backward motion. Bug report from
4703 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4704 However, I am not sure whether reseat still does the right thing
4705 in general after this change. */
4706 it->area = TEXT_AREA;
4707 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4708 it->sp = 0;
4709 it->face_before_selective_p = 0;
4710
4711 if (set_stop_p)
4712 it->stop_charpos = CHARPOS (pos);
4713 }
4714
4715
4716 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4717 If S is non-null, it is a C string to iterate over. Otherwise,
4718 STRING gives a Lisp string to iterate over.
4719
4720 If PRECISION > 0, don't return more then PRECISION number of
4721 characters from the string.
4722
4723 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4724 characters have been returned. FIELD_WIDTH < 0 means an infinite
4725 field width.
4726
4727 MULTIBYTE = 0 means disable processing of multibyte characters,
4728 MULTIBYTE > 0 means enable it,
4729 MULTIBYTE < 0 means use IT->multibyte_p.
4730
4731 IT must be initialized via a prior call to init_iterator before
4732 calling this function. */
4733
4734 static void
4735 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4736 struct it *it;
4737 unsigned char *s;
4738 Lisp_Object string;
4739 int charpos;
4740 int precision, field_width, multibyte;
4741 {
4742 /* No region in strings. */
4743 it->region_beg_charpos = it->region_end_charpos = -1;
4744
4745 /* No text property checks performed by default, but see below. */
4746 it->stop_charpos = -1;
4747
4748 /* Set iterator position and end position. */
4749 bzero (&it->current, sizeof it->current);
4750 it->current.overlay_string_index = -1;
4751 it->current.dpvec_index = -1;
4752 xassert (charpos >= 0);
4753
4754 /* If STRING is specified, use its multibyteness, otherwise use the
4755 setting of MULTIBYTE, if specified. */
4756 if (multibyte >= 0)
4757 it->multibyte_p = multibyte > 0;
4758
4759 if (s == NULL)
4760 {
4761 xassert (STRINGP (string));
4762 it->string = string;
4763 it->s = NULL;
4764 it->end_charpos = it->string_nchars = SCHARS (string);
4765 it->method = next_element_from_string;
4766 it->current.string_pos = string_pos (charpos, string);
4767 }
4768 else
4769 {
4770 it->s = s;
4771 it->string = Qnil;
4772
4773 /* Note that we use IT->current.pos, not it->current.string_pos,
4774 for displaying C strings. */
4775 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4776 if (it->multibyte_p)
4777 {
4778 it->current.pos = c_string_pos (charpos, s, 1);
4779 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4780 }
4781 else
4782 {
4783 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4784 it->end_charpos = it->string_nchars = strlen (s);
4785 }
4786
4787 it->method = next_element_from_c_string;
4788 }
4789
4790 /* PRECISION > 0 means don't return more than PRECISION characters
4791 from the string. */
4792 if (precision > 0 && it->end_charpos - charpos > precision)
4793 it->end_charpos = it->string_nchars = charpos + precision;
4794
4795 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4796 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4797 FIELD_WIDTH < 0 means infinite field width. This is useful for
4798 padding with `-' at the end of a mode line. */
4799 if (field_width < 0)
4800 field_width = INFINITY;
4801 if (field_width > it->end_charpos - charpos)
4802 it->end_charpos = charpos + field_width;
4803
4804 /* Use the standard display table for displaying strings. */
4805 if (DISP_TABLE_P (Vstandard_display_table))
4806 it->dp = XCHAR_TABLE (Vstandard_display_table);
4807
4808 it->stop_charpos = charpos;
4809 CHECK_IT (it);
4810 }
4811
4812
4813 \f
4814 /***********************************************************************
4815 Iteration
4816 ***********************************************************************/
4817
4818 /* Load IT's display element fields with information about the next
4819 display element from the current position of IT. Value is zero if
4820 end of buffer (or C string) is reached. */
4821
4822 int
4823 get_next_display_element (it)
4824 struct it *it;
4825 {
4826 /* Non-zero means that we found a display element. Zero means that
4827 we hit the end of what we iterate over. Performance note: the
4828 function pointer `method' used here turns out to be faster than
4829 using a sequence of if-statements. */
4830 int success_p = (*it->method) (it);
4831
4832 if (it->what == IT_CHARACTER)
4833 {
4834 /* Map via display table or translate control characters.
4835 IT->c, IT->len etc. have been set to the next character by
4836 the function call above. If we have a display table, and it
4837 contains an entry for IT->c, translate it. Don't do this if
4838 IT->c itself comes from a display table, otherwise we could
4839 end up in an infinite recursion. (An alternative could be to
4840 count the recursion depth of this function and signal an
4841 error when a certain maximum depth is reached.) Is it worth
4842 it? */
4843 if (success_p && it->dpvec == NULL)
4844 {
4845 Lisp_Object dv;
4846
4847 if (it->dp
4848 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4849 VECTORP (dv)))
4850 {
4851 struct Lisp_Vector *v = XVECTOR (dv);
4852
4853 /* Return the first character from the display table
4854 entry, if not empty. If empty, don't display the
4855 current character. */
4856 if (v->size)
4857 {
4858 it->dpvec_char_len = it->len;
4859 it->dpvec = v->contents;
4860 it->dpend = v->contents + v->size;
4861 it->current.dpvec_index = 0;
4862 it->method = next_element_from_display_vector;
4863 success_p = get_next_display_element (it);
4864 }
4865 else
4866 {
4867 set_iterator_to_next (it, 0);
4868 success_p = get_next_display_element (it);
4869 }
4870 }
4871
4872 /* Translate control characters into `\003' or `^C' form.
4873 Control characters coming from a display table entry are
4874 currently not translated because we use IT->dpvec to hold
4875 the translation. This could easily be changed but I
4876 don't believe that it is worth doing.
4877
4878 If it->multibyte_p is nonzero, eight-bit characters and
4879 non-printable multibyte characters are also translated to
4880 octal form.
4881
4882 If it->multibyte_p is zero, eight-bit characters that
4883 don't have corresponding multibyte char code are also
4884 translated to octal form. */
4885 else if ((it->c < ' '
4886 && (it->area != TEXT_AREA
4887 || (it->c != '\n' && it->c != '\t')))
4888 || (it->multibyte_p
4889 ? ((it->c >= 127
4890 && it->len == 1)
4891 || !CHAR_PRINTABLE_P (it->c))
4892 : (it->c >= 127
4893 && it->c == unibyte_char_to_multibyte (it->c))))
4894 {
4895 /* IT->c is a control character which must be displayed
4896 either as '\003' or as `^C' where the '\\' and '^'
4897 can be defined in the display table. Fill
4898 IT->ctl_chars with glyphs for what we have to
4899 display. Then, set IT->dpvec to these glyphs. */
4900 GLYPH g;
4901
4902 if (it->c < 128 && it->ctl_arrow_p)
4903 {
4904 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4905 if (it->dp
4906 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4907 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4908 g = XINT (DISP_CTRL_GLYPH (it->dp));
4909 else
4910 g = FAST_MAKE_GLYPH ('^', 0);
4911 XSETINT (it->ctl_chars[0], g);
4912
4913 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4914 XSETINT (it->ctl_chars[1], g);
4915
4916 /* Set up IT->dpvec and return first character from it. */
4917 it->dpvec_char_len = it->len;
4918 it->dpvec = it->ctl_chars;
4919 it->dpend = it->dpvec + 2;
4920 it->current.dpvec_index = 0;
4921 it->method = next_element_from_display_vector;
4922 get_next_display_element (it);
4923 }
4924 else
4925 {
4926 unsigned char str[MAX_MULTIBYTE_LENGTH];
4927 int len;
4928 int i;
4929 GLYPH escape_glyph;
4930
4931 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4932 if (it->dp
4933 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4934 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4935 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4936 else
4937 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4938
4939 if (SINGLE_BYTE_CHAR_P (it->c))
4940 str[0] = it->c, len = 1;
4941 else
4942 {
4943 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4944 if (len < 0)
4945 {
4946 /* It's an invalid character, which
4947 shouldn't happen actually, but due to
4948 bugs it may happen. Let's print the char
4949 as is, there's not much meaningful we can
4950 do with it. */
4951 str[0] = it->c;
4952 str[1] = it->c >> 8;
4953 str[2] = it->c >> 16;
4954 str[3] = it->c >> 24;
4955 len = 4;
4956 }
4957 }
4958
4959 for (i = 0; i < len; i++)
4960 {
4961 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4962 /* Insert three more glyphs into IT->ctl_chars for
4963 the octal display of the character. */
4964 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4965 XSETINT (it->ctl_chars[i * 4 + 1], g);
4966 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4967 XSETINT (it->ctl_chars[i * 4 + 2], g);
4968 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4969 XSETINT (it->ctl_chars[i * 4 + 3], g);
4970 }
4971
4972 /* Set up IT->dpvec and return the first character
4973 from it. */
4974 it->dpvec_char_len = it->len;
4975 it->dpvec = it->ctl_chars;
4976 it->dpend = it->dpvec + len * 4;
4977 it->current.dpvec_index = 0;
4978 it->method = next_element_from_display_vector;
4979 get_next_display_element (it);
4980 }
4981 }
4982 }
4983
4984 /* Adjust face id for a multibyte character. There are no
4985 multibyte character in unibyte text. */
4986 if (it->multibyte_p
4987 && success_p
4988 && FRAME_WINDOW_P (it->f))
4989 {
4990 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4991 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4992 }
4993 }
4994
4995 /* Is this character the last one of a run of characters with
4996 box? If yes, set IT->end_of_box_run_p to 1. */
4997 if (it->face_box_p
4998 && it->s == NULL)
4999 {
5000 int face_id;
5001 struct face *face;
5002
5003 it->end_of_box_run_p
5004 = ((face_id = face_after_it_pos (it),
5005 face_id != it->face_id)
5006 && (face = FACE_FROM_ID (it->f, face_id),
5007 face->box == FACE_NO_BOX));
5008 }
5009
5010 /* Value is 0 if end of buffer or string reached. */
5011 return success_p;
5012 }
5013
5014
5015 /* Move IT to the next display element.
5016
5017 RESEAT_P non-zero means if called on a newline in buffer text,
5018 skip to the next visible line start.
5019
5020 Functions get_next_display_element and set_iterator_to_next are
5021 separate because I find this arrangement easier to handle than a
5022 get_next_display_element function that also increments IT's
5023 position. The way it is we can first look at an iterator's current
5024 display element, decide whether it fits on a line, and if it does,
5025 increment the iterator position. The other way around we probably
5026 would either need a flag indicating whether the iterator has to be
5027 incremented the next time, or we would have to implement a
5028 decrement position function which would not be easy to write. */
5029
5030 void
5031 set_iterator_to_next (it, reseat_p)
5032 struct it *it;
5033 int reseat_p;
5034 {
5035 /* Reset flags indicating start and end of a sequence of characters
5036 with box. Reset them at the start of this function because
5037 moving the iterator to a new position might set them. */
5038 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5039
5040 if (it->method == next_element_from_buffer)
5041 {
5042 /* The current display element of IT is a character from
5043 current_buffer. Advance in the buffer, and maybe skip over
5044 invisible lines that are so because of selective display. */
5045 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5046 reseat_at_next_visible_line_start (it, 0);
5047 else
5048 {
5049 xassert (it->len != 0);
5050 IT_BYTEPOS (*it) += it->len;
5051 IT_CHARPOS (*it) += 1;
5052 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5053 }
5054 }
5055 else if (it->method == next_element_from_composition)
5056 {
5057 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
5058 if (STRINGP (it->string))
5059 {
5060 IT_STRING_BYTEPOS (*it) += it->len;
5061 IT_STRING_CHARPOS (*it) += it->cmp_len;
5062 it->method = next_element_from_string;
5063 goto consider_string_end;
5064 }
5065 else
5066 {
5067 IT_BYTEPOS (*it) += it->len;
5068 IT_CHARPOS (*it) += it->cmp_len;
5069 it->method = next_element_from_buffer;
5070 }
5071 }
5072 else if (it->method == next_element_from_c_string)
5073 {
5074 /* Current display element of IT is from a C string. */
5075 IT_BYTEPOS (*it) += it->len;
5076 IT_CHARPOS (*it) += 1;
5077 }
5078 else if (it->method == next_element_from_display_vector)
5079 {
5080 /* Current display element of IT is from a display table entry.
5081 Advance in the display table definition. Reset it to null if
5082 end reached, and continue with characters from buffers/
5083 strings. */
5084 ++it->current.dpvec_index;
5085
5086 /* Restore face of the iterator to what they were before the
5087 display vector entry (these entries may contain faces). */
5088 it->face_id = it->saved_face_id;
5089
5090 if (it->dpvec + it->current.dpvec_index == it->dpend)
5091 {
5092 if (it->s)
5093 it->method = next_element_from_c_string;
5094 else if (STRINGP (it->string))
5095 it->method = next_element_from_string;
5096 else
5097 it->method = next_element_from_buffer;
5098
5099 it->dpvec = NULL;
5100 it->current.dpvec_index = -1;
5101
5102 /* Skip over characters which were displayed via IT->dpvec. */
5103 if (it->dpvec_char_len < 0)
5104 reseat_at_next_visible_line_start (it, 1);
5105 else if (it->dpvec_char_len > 0)
5106 {
5107 it->len = it->dpvec_char_len;
5108 set_iterator_to_next (it, reseat_p);
5109 }
5110 }
5111 }
5112 else if (it->method == next_element_from_string)
5113 {
5114 /* Current display element is a character from a Lisp string. */
5115 xassert (it->s == NULL && STRINGP (it->string));
5116 IT_STRING_BYTEPOS (*it) += it->len;
5117 IT_STRING_CHARPOS (*it) += 1;
5118
5119 consider_string_end:
5120
5121 if (it->current.overlay_string_index >= 0)
5122 {
5123 /* IT->string is an overlay string. Advance to the
5124 next, if there is one. */
5125 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5126 next_overlay_string (it);
5127 }
5128 else
5129 {
5130 /* IT->string is not an overlay string. If we reached
5131 its end, and there is something on IT->stack, proceed
5132 with what is on the stack. This can be either another
5133 string, this time an overlay string, or a buffer. */
5134 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5135 && it->sp > 0)
5136 {
5137 pop_it (it);
5138 if (!STRINGP (it->string))
5139 it->method = next_element_from_buffer;
5140 else
5141 goto consider_string_end;
5142 }
5143 }
5144 }
5145 else if (it->method == next_element_from_image
5146 || it->method == next_element_from_stretch)
5147 {
5148 /* The position etc with which we have to proceed are on
5149 the stack. The position may be at the end of a string,
5150 if the `display' property takes up the whole string. */
5151 pop_it (it);
5152 it->image_id = 0;
5153 if (STRINGP (it->string))
5154 {
5155 it->method = next_element_from_string;
5156 goto consider_string_end;
5157 }
5158 else
5159 it->method = next_element_from_buffer;
5160 }
5161 else
5162 /* There are no other methods defined, so this should be a bug. */
5163 abort ();
5164
5165 xassert (it->method != next_element_from_string
5166 || (STRINGP (it->string)
5167 && IT_STRING_CHARPOS (*it) >= 0));
5168 }
5169
5170
5171 /* Load IT's display element fields with information about the next
5172 display element which comes from a display table entry or from the
5173 result of translating a control character to one of the forms `^C'
5174 or `\003'. IT->dpvec holds the glyphs to return as characters. */
5175
5176 static int
5177 next_element_from_display_vector (it)
5178 struct it *it;
5179 {
5180 /* Precondition. */
5181 xassert (it->dpvec && it->current.dpvec_index >= 0);
5182
5183 /* Remember the current face id in case glyphs specify faces.
5184 IT's face is restored in set_iterator_to_next. */
5185 it->saved_face_id = it->face_id;
5186
5187 if (INTEGERP (*it->dpvec)
5188 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5189 {
5190 int lface_id;
5191 GLYPH g;
5192
5193 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5194 it->c = FAST_GLYPH_CHAR (g);
5195 it->len = CHAR_BYTES (it->c);
5196
5197 /* The entry may contain a face id to use. Such a face id is
5198 the id of a Lisp face, not a realized face. A face id of
5199 zero means no face is specified. */
5200 lface_id = FAST_GLYPH_FACE (g);
5201 if (lface_id)
5202 {
5203 /* The function returns -1 if lface_id is invalid. */
5204 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
5205 if (face_id >= 0)
5206 it->face_id = face_id;
5207 }
5208 }
5209 else
5210 /* Display table entry is invalid. Return a space. */
5211 it->c = ' ', it->len = 1;
5212
5213 /* Don't change position and object of the iterator here. They are
5214 still the values of the character that had this display table
5215 entry or was translated, and that's what we want. */
5216 it->what = IT_CHARACTER;
5217 return 1;
5218 }
5219
5220
5221 /* Load IT with the next display element from Lisp string IT->string.
5222 IT->current.string_pos is the current position within the string.
5223 If IT->current.overlay_string_index >= 0, the Lisp string is an
5224 overlay string. */
5225
5226 static int
5227 next_element_from_string (it)
5228 struct it *it;
5229 {
5230 struct text_pos position;
5231
5232 xassert (STRINGP (it->string));
5233 xassert (IT_STRING_CHARPOS (*it) >= 0);
5234 position = it->current.string_pos;
5235
5236 /* Time to check for invisible text? */
5237 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5238 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5239 {
5240 handle_stop (it);
5241
5242 /* Since a handler may have changed IT->method, we must
5243 recurse here. */
5244 return get_next_display_element (it);
5245 }
5246
5247 if (it->current.overlay_string_index >= 0)
5248 {
5249 /* Get the next character from an overlay string. In overlay
5250 strings, There is no field width or padding with spaces to
5251 do. */
5252 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5253 {
5254 it->what = IT_EOB;
5255 return 0;
5256 }
5257 else if (STRING_MULTIBYTE (it->string))
5258 {
5259 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5260 const unsigned char *s = (SDATA (it->string)
5261 + IT_STRING_BYTEPOS (*it));
5262 it->c = string_char_and_length (s, remaining, &it->len);
5263 }
5264 else
5265 {
5266 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5267 it->len = 1;
5268 }
5269 }
5270 else
5271 {
5272 /* Get the next character from a Lisp string that is not an
5273 overlay string. Such strings come from the mode line, for
5274 example. We may have to pad with spaces, or truncate the
5275 string. See also next_element_from_c_string. */
5276 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5277 {
5278 it->what = IT_EOB;
5279 return 0;
5280 }
5281 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5282 {
5283 /* Pad with spaces. */
5284 it->c = ' ', it->len = 1;
5285 CHARPOS (position) = BYTEPOS (position) = -1;
5286 }
5287 else if (STRING_MULTIBYTE (it->string))
5288 {
5289 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5290 const unsigned char *s = (SDATA (it->string)
5291 + IT_STRING_BYTEPOS (*it));
5292 it->c = string_char_and_length (s, maxlen, &it->len);
5293 }
5294 else
5295 {
5296 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5297 it->len = 1;
5298 }
5299 }
5300
5301 /* Record what we have and where it came from. Note that we store a
5302 buffer position in IT->position although it could arguably be a
5303 string position. */
5304 it->what = IT_CHARACTER;
5305 it->object = it->string;
5306 it->position = position;
5307 return 1;
5308 }
5309
5310
5311 /* Load IT with next display element from C string IT->s.
5312 IT->string_nchars is the maximum number of characters to return
5313 from the string. IT->end_charpos may be greater than
5314 IT->string_nchars when this function is called, in which case we
5315 may have to return padding spaces. Value is zero if end of string
5316 reached, including padding spaces. */
5317
5318 static int
5319 next_element_from_c_string (it)
5320 struct it *it;
5321 {
5322 int success_p = 1;
5323
5324 xassert (it->s);
5325 it->what = IT_CHARACTER;
5326 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5327 it->object = Qnil;
5328
5329 /* IT's position can be greater IT->string_nchars in case a field
5330 width or precision has been specified when the iterator was
5331 initialized. */
5332 if (IT_CHARPOS (*it) >= it->end_charpos)
5333 {
5334 /* End of the game. */
5335 it->what = IT_EOB;
5336 success_p = 0;
5337 }
5338 else if (IT_CHARPOS (*it) >= it->string_nchars)
5339 {
5340 /* Pad with spaces. */
5341 it->c = ' ', it->len = 1;
5342 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5343 }
5344 else if (it->multibyte_p)
5345 {
5346 /* Implementation note: The calls to strlen apparently aren't a
5347 performance problem because there is no noticeable performance
5348 difference between Emacs running in unibyte or multibyte mode. */
5349 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5350 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5351 maxlen, &it->len);
5352 }
5353 else
5354 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5355
5356 return success_p;
5357 }
5358
5359
5360 /* Set up IT to return characters from an ellipsis, if appropriate.
5361 The definition of the ellipsis glyphs may come from a display table
5362 entry. This function Fills IT with the first glyph from the
5363 ellipsis if an ellipsis is to be displayed. */
5364
5365 static int
5366 next_element_from_ellipsis (it)
5367 struct it *it;
5368 {
5369 if (it->selective_display_ellipsis_p)
5370 {
5371 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
5372 {
5373 /* Use the display table definition for `...'. Invalid glyphs
5374 will be handled by the method returning elements from dpvec. */
5375 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
5376 it->dpvec_char_len = it->len;
5377 it->dpvec = v->contents;
5378 it->dpend = v->contents + v->size;
5379 it->current.dpvec_index = 0;
5380 it->method = next_element_from_display_vector;
5381 }
5382 else
5383 {
5384 /* Use default `...' which is stored in default_invis_vector. */
5385 it->dpvec_char_len = it->len;
5386 it->dpvec = default_invis_vector;
5387 it->dpend = default_invis_vector + 3;
5388 it->current.dpvec_index = 0;
5389 it->method = next_element_from_display_vector;
5390 }
5391 }
5392 else
5393 {
5394 /* The face at the current position may be different from the
5395 face we find after the invisible text. Remember what it
5396 was in IT->saved_face_id, and signal that it's there by
5397 setting face_before_selective_p. */
5398 it->saved_face_id = it->face_id;
5399 it->method = next_element_from_buffer;
5400 reseat_at_next_visible_line_start (it, 1);
5401 it->face_before_selective_p = 1;
5402 }
5403
5404 return get_next_display_element (it);
5405 }
5406
5407
5408 /* Deliver an image display element. The iterator IT is already
5409 filled with image information (done in handle_display_prop). Value
5410 is always 1. */
5411
5412
5413 static int
5414 next_element_from_image (it)
5415 struct it *it;
5416 {
5417 it->what = IT_IMAGE;
5418 return 1;
5419 }
5420
5421
5422 /* Fill iterator IT with next display element from a stretch glyph
5423 property. IT->object is the value of the text property. Value is
5424 always 1. */
5425
5426 static int
5427 next_element_from_stretch (it)
5428 struct it *it;
5429 {
5430 it->what = IT_STRETCH;
5431 return 1;
5432 }
5433
5434
5435 /* Load IT with the next display element from current_buffer. Value
5436 is zero if end of buffer reached. IT->stop_charpos is the next
5437 position at which to stop and check for text properties or buffer
5438 end. */
5439
5440 static int
5441 next_element_from_buffer (it)
5442 struct it *it;
5443 {
5444 int success_p = 1;
5445
5446 /* Check this assumption, otherwise, we would never enter the
5447 if-statement, below. */
5448 xassert (IT_CHARPOS (*it) >= BEGV
5449 && IT_CHARPOS (*it) <= it->stop_charpos);
5450
5451 if (IT_CHARPOS (*it) >= it->stop_charpos)
5452 {
5453 if (IT_CHARPOS (*it) >= it->end_charpos)
5454 {
5455 int overlay_strings_follow_p;
5456
5457 /* End of the game, except when overlay strings follow that
5458 haven't been returned yet. */
5459 if (it->overlay_strings_at_end_processed_p)
5460 overlay_strings_follow_p = 0;
5461 else
5462 {
5463 it->overlay_strings_at_end_processed_p = 1;
5464 overlay_strings_follow_p = get_overlay_strings (it, 0);
5465 }
5466
5467 if (overlay_strings_follow_p)
5468 success_p = get_next_display_element (it);
5469 else
5470 {
5471 it->what = IT_EOB;
5472 it->position = it->current.pos;
5473 success_p = 0;
5474 }
5475 }
5476 else
5477 {
5478 handle_stop (it);
5479 return get_next_display_element (it);
5480 }
5481 }
5482 else
5483 {
5484 /* No face changes, overlays etc. in sight, so just return a
5485 character from current_buffer. */
5486 unsigned char *p;
5487
5488 /* Maybe run the redisplay end trigger hook. Performance note:
5489 This doesn't seem to cost measurable time. */
5490 if (it->redisplay_end_trigger_charpos
5491 && it->glyph_row
5492 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5493 run_redisplay_end_trigger_hook (it);
5494
5495 /* Get the next character, maybe multibyte. */
5496 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5497 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5498 {
5499 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5500 - IT_BYTEPOS (*it));
5501 it->c = string_char_and_length (p, maxlen, &it->len);
5502 }
5503 else
5504 it->c = *p, it->len = 1;
5505
5506 /* Record what we have and where it came from. */
5507 it->what = IT_CHARACTER;;
5508 it->object = it->w->buffer;
5509 it->position = it->current.pos;
5510
5511 /* Normally we return the character found above, except when we
5512 really want to return an ellipsis for selective display. */
5513 if (it->selective)
5514 {
5515 if (it->c == '\n')
5516 {
5517 /* A value of selective > 0 means hide lines indented more
5518 than that number of columns. */
5519 if (it->selective > 0
5520 && IT_CHARPOS (*it) + 1 < ZV
5521 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5522 IT_BYTEPOS (*it) + 1,
5523 (double) it->selective)) /* iftc */
5524 {
5525 success_p = next_element_from_ellipsis (it);
5526 it->dpvec_char_len = -1;
5527 }
5528 }
5529 else if (it->c == '\r' && it->selective == -1)
5530 {
5531 /* A value of selective == -1 means that everything from the
5532 CR to the end of the line is invisible, with maybe an
5533 ellipsis displayed for it. */
5534 success_p = next_element_from_ellipsis (it);
5535 it->dpvec_char_len = -1;
5536 }
5537 }
5538 }
5539
5540 /* Value is zero if end of buffer reached. */
5541 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5542 return success_p;
5543 }
5544
5545
5546 /* Run the redisplay end trigger hook for IT. */
5547
5548 static void
5549 run_redisplay_end_trigger_hook (it)
5550 struct it *it;
5551 {
5552 Lisp_Object args[3];
5553
5554 /* IT->glyph_row should be non-null, i.e. we should be actually
5555 displaying something, or otherwise we should not run the hook. */
5556 xassert (it->glyph_row);
5557
5558 /* Set up hook arguments. */
5559 args[0] = Qredisplay_end_trigger_functions;
5560 args[1] = it->window;
5561 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5562 it->redisplay_end_trigger_charpos = 0;
5563
5564 /* Since we are *trying* to run these functions, don't try to run
5565 them again, even if they get an error. */
5566 it->w->redisplay_end_trigger = Qnil;
5567 Frun_hook_with_args (3, args);
5568
5569 /* Notice if it changed the face of the character we are on. */
5570 handle_face_prop (it);
5571 }
5572
5573
5574 /* Deliver a composition display element. The iterator IT is already
5575 filled with composition information (done in
5576 handle_composition_prop). Value is always 1. */
5577
5578 static int
5579 next_element_from_composition (it)
5580 struct it *it;
5581 {
5582 it->what = IT_COMPOSITION;
5583 it->position = (STRINGP (it->string)
5584 ? it->current.string_pos
5585 : it->current.pos);
5586 return 1;
5587 }
5588
5589
5590 \f
5591 /***********************************************************************
5592 Moving an iterator without producing glyphs
5593 ***********************************************************************/
5594
5595 /* Move iterator IT to a specified buffer or X position within one
5596 line on the display without producing glyphs.
5597
5598 OP should be a bit mask including some or all of these bits:
5599 MOVE_TO_X: Stop on reaching x-position TO_X.
5600 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5601 Regardless of OP's value, stop in reaching the end of the display line.
5602
5603 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5604 This means, in particular, that TO_X includes window's horizontal
5605 scroll amount.
5606
5607 The return value has several possible values that
5608 say what condition caused the scan to stop:
5609
5610 MOVE_POS_MATCH_OR_ZV
5611 - when TO_POS or ZV was reached.
5612
5613 MOVE_X_REACHED
5614 -when TO_X was reached before TO_POS or ZV were reached.
5615
5616 MOVE_LINE_CONTINUED
5617 - when we reached the end of the display area and the line must
5618 be continued.
5619
5620 MOVE_LINE_TRUNCATED
5621 - when we reached the end of the display area and the line is
5622 truncated.
5623
5624 MOVE_NEWLINE_OR_CR
5625 - when we stopped at a line end, i.e. a newline or a CR and selective
5626 display is on. */
5627
5628 static enum move_it_result
5629 move_it_in_display_line_to (it, to_charpos, to_x, op)
5630 struct it *it;
5631 int to_charpos, to_x, op;
5632 {
5633 enum move_it_result result = MOVE_UNDEFINED;
5634 struct glyph_row *saved_glyph_row;
5635
5636 /* Don't produce glyphs in produce_glyphs. */
5637 saved_glyph_row = it->glyph_row;
5638 it->glyph_row = NULL;
5639
5640 #define BUFFER_POS_REACHED_P() \
5641 ((op & MOVE_TO_POS) != 0 \
5642 && BUFFERP (it->object) \
5643 && IT_CHARPOS (*it) >= to_charpos)
5644
5645 while (1)
5646 {
5647 int x, i, ascent = 0, descent = 0;
5648
5649 /* Stop when ZV reached.
5650 We used to stop here when TO_CHARPOS reached as well, but that is
5651 too soon if this glyph does not fit on this line. So we handle it
5652 explicitly below. */
5653 if (!get_next_display_element (it)
5654 || (it->truncate_lines_p
5655 && BUFFER_POS_REACHED_P ()))
5656 {
5657 result = MOVE_POS_MATCH_OR_ZV;
5658 break;
5659 }
5660
5661 /* The call to produce_glyphs will get the metrics of the
5662 display element IT is loaded with. We record in x the
5663 x-position before this display element in case it does not
5664 fit on the line. */
5665 x = it->current_x;
5666
5667 /* Remember the line height so far in case the next element doesn't
5668 fit on the line. */
5669 if (!it->truncate_lines_p)
5670 {
5671 ascent = it->max_ascent;
5672 descent = it->max_descent;
5673 }
5674
5675 PRODUCE_GLYPHS (it);
5676
5677 if (it->area != TEXT_AREA)
5678 {
5679 set_iterator_to_next (it, 1);
5680 continue;
5681 }
5682
5683 /* The number of glyphs we get back in IT->nglyphs will normally
5684 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5685 character on a terminal frame, or (iii) a line end. For the
5686 second case, IT->nglyphs - 1 padding glyphs will be present
5687 (on X frames, there is only one glyph produced for a
5688 composite character.
5689
5690 The behavior implemented below means, for continuation lines,
5691 that as many spaces of a TAB as fit on the current line are
5692 displayed there. For terminal frames, as many glyphs of a
5693 multi-glyph character are displayed in the current line, too.
5694 This is what the old redisplay code did, and we keep it that
5695 way. Under X, the whole shape of a complex character must
5696 fit on the line or it will be completely displayed in the
5697 next line.
5698
5699 Note that both for tabs and padding glyphs, all glyphs have
5700 the same width. */
5701 if (it->nglyphs)
5702 {
5703 /* More than one glyph or glyph doesn't fit on line. All
5704 glyphs have the same width. */
5705 int single_glyph_width = it->pixel_width / it->nglyphs;
5706 int new_x;
5707
5708 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5709 {
5710 new_x = x + single_glyph_width;
5711
5712 /* We want to leave anything reaching TO_X to the caller. */
5713 if ((op & MOVE_TO_X) && new_x > to_x)
5714 {
5715 if (BUFFER_POS_REACHED_P ())
5716 goto buffer_pos_reached;
5717 it->current_x = x;
5718 result = MOVE_X_REACHED;
5719 break;
5720 }
5721 else if (/* Lines are continued. */
5722 !it->truncate_lines_p
5723 && (/* And glyph doesn't fit on the line. */
5724 new_x > it->last_visible_x
5725 /* Or it fits exactly and we're on a window
5726 system frame. */
5727 || (new_x == it->last_visible_x
5728 && FRAME_WINDOW_P (it->f))))
5729 {
5730 if (/* IT->hpos == 0 means the very first glyph
5731 doesn't fit on the line, e.g. a wide image. */
5732 it->hpos == 0
5733 || (new_x == it->last_visible_x
5734 && FRAME_WINDOW_P (it->f)))
5735 {
5736 ++it->hpos;
5737 it->current_x = new_x;
5738 if (i == it->nglyphs - 1)
5739 {
5740 set_iterator_to_next (it, 1);
5741 #ifdef HAVE_WINDOW_SYSTEM
5742 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5743 {
5744 if (!get_next_display_element (it))
5745 {
5746 result = MOVE_POS_MATCH_OR_ZV;
5747 break;
5748 }
5749 if (BUFFER_POS_REACHED_P ())
5750 {
5751 if (ITERATOR_AT_END_OF_LINE_P (it))
5752 result = MOVE_POS_MATCH_OR_ZV;
5753 else
5754 result = MOVE_LINE_CONTINUED;
5755 break;
5756 }
5757 if (ITERATOR_AT_END_OF_LINE_P (it))
5758 {
5759 result = MOVE_NEWLINE_OR_CR;
5760 break;
5761 }
5762 }
5763 #endif /* HAVE_WINDOW_SYSTEM */
5764 }
5765 }
5766 else
5767 {
5768 it->current_x = x;
5769 it->max_ascent = ascent;
5770 it->max_descent = descent;
5771 }
5772
5773 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5774 IT_CHARPOS (*it)));
5775 result = MOVE_LINE_CONTINUED;
5776 break;
5777 }
5778 else if (BUFFER_POS_REACHED_P ())
5779 goto buffer_pos_reached;
5780 else if (new_x > it->first_visible_x)
5781 {
5782 /* Glyph is visible. Increment number of glyphs that
5783 would be displayed. */
5784 ++it->hpos;
5785 }
5786 else
5787 {
5788 /* Glyph is completely off the left margin of the display
5789 area. Nothing to do. */
5790 }
5791 }
5792
5793 if (result != MOVE_UNDEFINED)
5794 break;
5795 }
5796 else if (BUFFER_POS_REACHED_P ())
5797 {
5798 buffer_pos_reached:
5799 it->current_x = x;
5800 it->max_ascent = ascent;
5801 it->max_descent = descent;
5802 result = MOVE_POS_MATCH_OR_ZV;
5803 break;
5804 }
5805 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5806 {
5807 /* Stop when TO_X specified and reached. This check is
5808 necessary here because of lines consisting of a line end,
5809 only. The line end will not produce any glyphs and we
5810 would never get MOVE_X_REACHED. */
5811 xassert (it->nglyphs == 0);
5812 result = MOVE_X_REACHED;
5813 break;
5814 }
5815
5816 /* Is this a line end? If yes, we're done. */
5817 if (ITERATOR_AT_END_OF_LINE_P (it))
5818 {
5819 result = MOVE_NEWLINE_OR_CR;
5820 break;
5821 }
5822
5823 /* The current display element has been consumed. Advance
5824 to the next. */
5825 set_iterator_to_next (it, 1);
5826
5827 /* Stop if lines are truncated and IT's current x-position is
5828 past the right edge of the window now. */
5829 if (it->truncate_lines_p
5830 && it->current_x >= it->last_visible_x)
5831 {
5832 #ifdef HAVE_WINDOW_SYSTEM
5833 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5834 {
5835 if (!get_next_display_element (it)
5836 || BUFFER_POS_REACHED_P ())
5837 {
5838 result = MOVE_POS_MATCH_OR_ZV;
5839 break;
5840 }
5841 if (ITERATOR_AT_END_OF_LINE_P (it))
5842 {
5843 result = MOVE_NEWLINE_OR_CR;
5844 break;
5845 }
5846 }
5847 #endif /* HAVE_WINDOW_SYSTEM */
5848 result = MOVE_LINE_TRUNCATED;
5849 break;
5850 }
5851 }
5852
5853 #undef BUFFER_POS_REACHED_P
5854
5855 /* Restore the iterator settings altered at the beginning of this
5856 function. */
5857 it->glyph_row = saved_glyph_row;
5858 return result;
5859 }
5860
5861
5862 /* Move IT forward until it satisfies one or more of the criteria in
5863 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5864
5865 OP is a bit-mask that specifies where to stop, and in particular,
5866 which of those four position arguments makes a difference. See the
5867 description of enum move_operation_enum.
5868
5869 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5870 screen line, this function will set IT to the next position >
5871 TO_CHARPOS. */
5872
5873 void
5874 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5875 struct it *it;
5876 int to_charpos, to_x, to_y, to_vpos;
5877 int op;
5878 {
5879 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5880 int line_height;
5881 int reached = 0;
5882
5883 for (;;)
5884 {
5885 if (op & MOVE_TO_VPOS)
5886 {
5887 /* If no TO_CHARPOS and no TO_X specified, stop at the
5888 start of the line TO_VPOS. */
5889 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5890 {
5891 if (it->vpos == to_vpos)
5892 {
5893 reached = 1;
5894 break;
5895 }
5896 else
5897 skip = move_it_in_display_line_to (it, -1, -1, 0);
5898 }
5899 else
5900 {
5901 /* TO_VPOS >= 0 means stop at TO_X in the line at
5902 TO_VPOS, or at TO_POS, whichever comes first. */
5903 if (it->vpos == to_vpos)
5904 {
5905 reached = 2;
5906 break;
5907 }
5908
5909 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5910
5911 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5912 {
5913 reached = 3;
5914 break;
5915 }
5916 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5917 {
5918 /* We have reached TO_X but not in the line we want. */
5919 skip = move_it_in_display_line_to (it, to_charpos,
5920 -1, MOVE_TO_POS);
5921 if (skip == MOVE_POS_MATCH_OR_ZV)
5922 {
5923 reached = 4;
5924 break;
5925 }
5926 }
5927 }
5928 }
5929 else if (op & MOVE_TO_Y)
5930 {
5931 struct it it_backup;
5932
5933 /* TO_Y specified means stop at TO_X in the line containing
5934 TO_Y---or at TO_CHARPOS if this is reached first. The
5935 problem is that we can't really tell whether the line
5936 contains TO_Y before we have completely scanned it, and
5937 this may skip past TO_X. What we do is to first scan to
5938 TO_X.
5939
5940 If TO_X is not specified, use a TO_X of zero. The reason
5941 is to make the outcome of this function more predictable.
5942 If we didn't use TO_X == 0, we would stop at the end of
5943 the line which is probably not what a caller would expect
5944 to happen. */
5945 skip = move_it_in_display_line_to (it, to_charpos,
5946 ((op & MOVE_TO_X)
5947 ? to_x : 0),
5948 (MOVE_TO_X
5949 | (op & MOVE_TO_POS)));
5950
5951 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5952 if (skip == MOVE_POS_MATCH_OR_ZV)
5953 {
5954 reached = 5;
5955 break;
5956 }
5957
5958 /* If TO_X was reached, we would like to know whether TO_Y
5959 is in the line. This can only be said if we know the
5960 total line height which requires us to scan the rest of
5961 the line. */
5962 if (skip == MOVE_X_REACHED)
5963 {
5964 it_backup = *it;
5965 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5966 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5967 op & MOVE_TO_POS);
5968 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5969 }
5970
5971 /* Now, decide whether TO_Y is in this line. */
5972 line_height = it->max_ascent + it->max_descent;
5973 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5974
5975 if (to_y >= it->current_y
5976 && to_y < it->current_y + line_height)
5977 {
5978 if (skip == MOVE_X_REACHED)
5979 /* If TO_Y is in this line and TO_X was reached above,
5980 we scanned too far. We have to restore IT's settings
5981 to the ones before skipping. */
5982 *it = it_backup;
5983 reached = 6;
5984 }
5985 else if (skip == MOVE_X_REACHED)
5986 {
5987 skip = skip2;
5988 if (skip == MOVE_POS_MATCH_OR_ZV)
5989 reached = 7;
5990 }
5991
5992 if (reached)
5993 break;
5994 }
5995 else
5996 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5997
5998 switch (skip)
5999 {
6000 case MOVE_POS_MATCH_OR_ZV:
6001 reached = 8;
6002 goto out;
6003
6004 case MOVE_NEWLINE_OR_CR:
6005 set_iterator_to_next (it, 1);
6006 it->continuation_lines_width = 0;
6007 break;
6008
6009 case MOVE_LINE_TRUNCATED:
6010 it->continuation_lines_width = 0;
6011 reseat_at_next_visible_line_start (it, 0);
6012 if ((op & MOVE_TO_POS) != 0
6013 && IT_CHARPOS (*it) > to_charpos)
6014 {
6015 reached = 9;
6016 goto out;
6017 }
6018 break;
6019
6020 case MOVE_LINE_CONTINUED:
6021 it->continuation_lines_width += it->current_x;
6022 break;
6023
6024 default:
6025 abort ();
6026 }
6027
6028 /* Reset/increment for the next run. */
6029 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6030 it->current_x = it->hpos = 0;
6031 it->current_y += it->max_ascent + it->max_descent;
6032 ++it->vpos;
6033 last_height = it->max_ascent + it->max_descent;
6034 last_max_ascent = it->max_ascent;
6035 it->max_ascent = it->max_descent = 0;
6036 }
6037
6038 out:
6039
6040 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6041 }
6042
6043
6044 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6045
6046 If DY > 0, move IT backward at least that many pixels. DY = 0
6047 means move IT backward to the preceding line start or BEGV. This
6048 function may move over more than DY pixels if IT->current_y - DY
6049 ends up in the middle of a line; in this case IT->current_y will be
6050 set to the top of the line moved to. */
6051
6052 void
6053 move_it_vertically_backward (it, dy)
6054 struct it *it;
6055 int dy;
6056 {
6057 int nlines, h;
6058 struct it it2, it3;
6059 int start_pos = IT_CHARPOS (*it);
6060
6061 xassert (dy >= 0);
6062
6063 /* Estimate how many newlines we must move back. */
6064 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6065
6066 /* Set the iterator's position that many lines back. */
6067 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6068 back_to_previous_visible_line_start (it);
6069
6070 /* Reseat the iterator here. When moving backward, we don't want
6071 reseat to skip forward over invisible text, set up the iterator
6072 to deliver from overlay strings at the new position etc. So,
6073 use reseat_1 here. */
6074 reseat_1 (it, it->current.pos, 1);
6075
6076 /* We are now surely at a line start. */
6077 it->current_x = it->hpos = 0;
6078 it->continuation_lines_width = 0;
6079
6080 /* Move forward and see what y-distance we moved. First move to the
6081 start of the next line so that we get its height. We need this
6082 height to be able to tell whether we reached the specified
6083 y-distance. */
6084 it2 = *it;
6085 it2.max_ascent = it2.max_descent = 0;
6086 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6087 MOVE_TO_POS | MOVE_TO_VPOS);
6088 xassert (IT_CHARPOS (*it) >= BEGV);
6089 it3 = it2;
6090
6091 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6092 xassert (IT_CHARPOS (*it) >= BEGV);
6093 /* H is the actual vertical distance from the position in *IT
6094 and the starting position. */
6095 h = it2.current_y - it->current_y;
6096 /* NLINES is the distance in number of lines. */
6097 nlines = it2.vpos - it->vpos;
6098
6099 /* Correct IT's y and vpos position
6100 so that they are relative to the starting point. */
6101 it->vpos -= nlines;
6102 it->current_y -= h;
6103
6104 if (dy == 0)
6105 {
6106 /* DY == 0 means move to the start of the screen line. The
6107 value of nlines is > 0 if continuation lines were involved. */
6108 if (nlines > 0)
6109 move_it_by_lines (it, nlines, 1);
6110 xassert (IT_CHARPOS (*it) <= start_pos);
6111 }
6112 else
6113 {
6114 /* The y-position we try to reach, relative to *IT.
6115 Note that H has been subtracted in front of the if-statement. */
6116 int target_y = it->current_y + h - dy;
6117 int y0 = it3.current_y;
6118 int y1 = line_bottom_y (&it3);
6119 int line_height = y1 - y0;
6120
6121 /* If we did not reach target_y, try to move further backward if
6122 we can. If we moved too far backward, try to move forward. */
6123 if (target_y < it->current_y
6124 /* This is heuristic. In a window that's 3 lines high, with
6125 a line height of 13 pixels each, recentering with point
6126 on the bottom line will try to move -39/2 = 19 pixels
6127 backward. Try to avoid moving into the first line. */
6128 && it->current_y - target_y > line_height / 3 * 2
6129 && IT_CHARPOS (*it) > BEGV)
6130 {
6131 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6132 target_y - it->current_y));
6133 move_it_vertically (it, target_y - it->current_y);
6134 xassert (IT_CHARPOS (*it) >= BEGV);
6135 }
6136 else if (target_y >= it->current_y + line_height
6137 && IT_CHARPOS (*it) < ZV)
6138 {
6139 /* Should move forward by at least one line, maybe more.
6140
6141 Note: Calling move_it_by_lines can be expensive on
6142 terminal frames, where compute_motion is used (via
6143 vmotion) to do the job, when there are very long lines
6144 and truncate-lines is nil. That's the reason for
6145 treating terminal frames specially here. */
6146
6147 if (!FRAME_WINDOW_P (it->f))
6148 move_it_vertically (it, target_y - (it->current_y + line_height));
6149 else
6150 {
6151 do
6152 {
6153 move_it_by_lines (it, 1, 1);
6154 }
6155 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6156 }
6157
6158 xassert (IT_CHARPOS (*it) >= BEGV);
6159 }
6160 }
6161 }
6162
6163
6164 /* Move IT by a specified amount of pixel lines DY. DY negative means
6165 move backwards. DY = 0 means move to start of screen line. At the
6166 end, IT will be on the start of a screen line. */
6167
6168 void
6169 move_it_vertically (it, dy)
6170 struct it *it;
6171 int dy;
6172 {
6173 if (dy <= 0)
6174 move_it_vertically_backward (it, -dy);
6175 else if (dy > 0)
6176 {
6177 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6178 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6179 MOVE_TO_POS | MOVE_TO_Y);
6180 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6181
6182 /* If buffer ends in ZV without a newline, move to the start of
6183 the line to satisfy the post-condition. */
6184 if (IT_CHARPOS (*it) == ZV
6185 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6186 move_it_by_lines (it, 0, 0);
6187 }
6188 }
6189
6190
6191 /* Move iterator IT past the end of the text line it is in. */
6192
6193 void
6194 move_it_past_eol (it)
6195 struct it *it;
6196 {
6197 enum move_it_result rc;
6198
6199 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6200 if (rc == MOVE_NEWLINE_OR_CR)
6201 set_iterator_to_next (it, 0);
6202 }
6203
6204
6205 #if 0 /* Currently not used. */
6206
6207 /* Return non-zero if some text between buffer positions START_CHARPOS
6208 and END_CHARPOS is invisible. IT->window is the window for text
6209 property lookup. */
6210
6211 static int
6212 invisible_text_between_p (it, start_charpos, end_charpos)
6213 struct it *it;
6214 int start_charpos, end_charpos;
6215 {
6216 Lisp_Object prop, limit;
6217 int invisible_found_p;
6218
6219 xassert (it != NULL && start_charpos <= end_charpos);
6220
6221 /* Is text at START invisible? */
6222 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6223 it->window);
6224 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6225 invisible_found_p = 1;
6226 else
6227 {
6228 limit = Fnext_single_char_property_change (make_number (start_charpos),
6229 Qinvisible, Qnil,
6230 make_number (end_charpos));
6231 invisible_found_p = XFASTINT (limit) < end_charpos;
6232 }
6233
6234 return invisible_found_p;
6235 }
6236
6237 #endif /* 0 */
6238
6239
6240 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6241 negative means move up. DVPOS == 0 means move to the start of the
6242 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6243 NEED_Y_P is zero, IT->current_y will be left unchanged.
6244
6245 Further optimization ideas: If we would know that IT->f doesn't use
6246 a face with proportional font, we could be faster for
6247 truncate-lines nil. */
6248
6249 void
6250 move_it_by_lines (it, dvpos, need_y_p)
6251 struct it *it;
6252 int dvpos, need_y_p;
6253 {
6254 struct position pos;
6255
6256 if (!FRAME_WINDOW_P (it->f))
6257 {
6258 struct text_pos textpos;
6259
6260 /* We can use vmotion on frames without proportional fonts. */
6261 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6262 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6263 reseat (it, textpos, 1);
6264 it->vpos += pos.vpos;
6265 it->current_y += pos.vpos;
6266 }
6267 else if (dvpos == 0)
6268 {
6269 /* DVPOS == 0 means move to the start of the screen line. */
6270 move_it_vertically_backward (it, 0);
6271 xassert (it->current_x == 0 && it->hpos == 0);
6272 }
6273 else if (dvpos > 0)
6274 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6275 else
6276 {
6277 struct it it2;
6278 int start_charpos, i;
6279
6280 /* Start at the beginning of the screen line containing IT's
6281 position. */
6282 move_it_vertically_backward (it, 0);
6283
6284 /* Go back -DVPOS visible lines and reseat the iterator there. */
6285 start_charpos = IT_CHARPOS (*it);
6286 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6287 back_to_previous_visible_line_start (it);
6288 reseat (it, it->current.pos, 1);
6289 it->current_x = it->hpos = 0;
6290
6291 /* Above call may have moved too far if continuation lines
6292 are involved. Scan forward and see if it did. */
6293 it2 = *it;
6294 it2.vpos = it2.current_y = 0;
6295 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6296 it->vpos -= it2.vpos;
6297 it->current_y -= it2.current_y;
6298 it->current_x = it->hpos = 0;
6299
6300 /* If we moved too far, move IT some lines forward. */
6301 if (it2.vpos > -dvpos)
6302 {
6303 int delta = it2.vpos + dvpos;
6304 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6305 }
6306 }
6307 }
6308
6309 /* Return 1 if IT points into the middle of a display vector. */
6310
6311 int
6312 in_display_vector_p (it)
6313 struct it *it;
6314 {
6315 return (it->method == next_element_from_display_vector
6316 && it->current.dpvec_index > 0
6317 && it->dpvec + it->current.dpvec_index != it->dpend);
6318 }
6319
6320 \f
6321 /***********************************************************************
6322 Messages
6323 ***********************************************************************/
6324
6325
6326 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6327 to *Messages*. */
6328
6329 void
6330 add_to_log (format, arg1, arg2)
6331 char *format;
6332 Lisp_Object arg1, arg2;
6333 {
6334 Lisp_Object args[3];
6335 Lisp_Object msg, fmt;
6336 char *buffer;
6337 int len;
6338 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6339 USE_SAFE_ALLOCA;
6340
6341 /* Do nothing if called asynchronously. Inserting text into
6342 a buffer may call after-change-functions and alike and
6343 that would means running Lisp asynchronously. */
6344 if (handling_signal)
6345 return;
6346
6347 fmt = msg = Qnil;
6348 GCPRO4 (fmt, msg, arg1, arg2);
6349
6350 args[0] = fmt = build_string (format);
6351 args[1] = arg1;
6352 args[2] = arg2;
6353 msg = Fformat (3, args);
6354
6355 len = SBYTES (msg) + 1;
6356 SAFE_ALLOCA (buffer, char *, len);
6357 bcopy (SDATA (msg), buffer, len);
6358
6359 message_dolog (buffer, len - 1, 1, 0);
6360 SAFE_FREE (len);
6361
6362 UNGCPRO;
6363 }
6364
6365
6366 /* Output a newline in the *Messages* buffer if "needs" one. */
6367
6368 void
6369 message_log_maybe_newline ()
6370 {
6371 if (message_log_need_newline)
6372 message_dolog ("", 0, 1, 0);
6373 }
6374
6375
6376 /* Add a string M of length NBYTES to the message log, optionally
6377 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6378 nonzero, means interpret the contents of M as multibyte. This
6379 function calls low-level routines in order to bypass text property
6380 hooks, etc. which might not be safe to run. */
6381
6382 void
6383 message_dolog (m, nbytes, nlflag, multibyte)
6384 const char *m;
6385 int nbytes, nlflag, multibyte;
6386 {
6387 if (!NILP (Vmemory_full))
6388 return;
6389
6390 if (!NILP (Vmessage_log_max))
6391 {
6392 struct buffer *oldbuf;
6393 Lisp_Object oldpoint, oldbegv, oldzv;
6394 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6395 int point_at_end = 0;
6396 int zv_at_end = 0;
6397 Lisp_Object old_deactivate_mark, tem;
6398 struct gcpro gcpro1;
6399
6400 old_deactivate_mark = Vdeactivate_mark;
6401 oldbuf = current_buffer;
6402 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6403 current_buffer->undo_list = Qt;
6404
6405 oldpoint = message_dolog_marker1;
6406 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6407 oldbegv = message_dolog_marker2;
6408 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6409 oldzv = message_dolog_marker3;
6410 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6411 GCPRO1 (old_deactivate_mark);
6412
6413 if (PT == Z)
6414 point_at_end = 1;
6415 if (ZV == Z)
6416 zv_at_end = 1;
6417
6418 BEGV = BEG;
6419 BEGV_BYTE = BEG_BYTE;
6420 ZV = Z;
6421 ZV_BYTE = Z_BYTE;
6422 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6423
6424 /* Insert the string--maybe converting multibyte to single byte
6425 or vice versa, so that all the text fits the buffer. */
6426 if (multibyte
6427 && NILP (current_buffer->enable_multibyte_characters))
6428 {
6429 int i, c, char_bytes;
6430 unsigned char work[1];
6431
6432 /* Convert a multibyte string to single-byte
6433 for the *Message* buffer. */
6434 for (i = 0; i < nbytes; i += char_bytes)
6435 {
6436 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6437 work[0] = (SINGLE_BYTE_CHAR_P (c)
6438 ? c
6439 : multibyte_char_to_unibyte (c, Qnil));
6440 insert_1_both (work, 1, 1, 1, 0, 0);
6441 }
6442 }
6443 else if (! multibyte
6444 && ! NILP (current_buffer->enable_multibyte_characters))
6445 {
6446 int i, c, char_bytes;
6447 unsigned char *msg = (unsigned char *) m;
6448 unsigned char str[MAX_MULTIBYTE_LENGTH];
6449 /* Convert a single-byte string to multibyte
6450 for the *Message* buffer. */
6451 for (i = 0; i < nbytes; i++)
6452 {
6453 c = unibyte_char_to_multibyte (msg[i]);
6454 char_bytes = CHAR_STRING (c, str);
6455 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6456 }
6457 }
6458 else if (nbytes)
6459 insert_1 (m, nbytes, 1, 0, 0);
6460
6461 if (nlflag)
6462 {
6463 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6464 insert_1 ("\n", 1, 1, 0, 0);
6465
6466 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6467 this_bol = PT;
6468 this_bol_byte = PT_BYTE;
6469
6470 /* See if this line duplicates the previous one.
6471 If so, combine duplicates. */
6472 if (this_bol > BEG)
6473 {
6474 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6475 prev_bol = PT;
6476 prev_bol_byte = PT_BYTE;
6477
6478 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6479 this_bol, this_bol_byte);
6480 if (dup)
6481 {
6482 del_range_both (prev_bol, prev_bol_byte,
6483 this_bol, this_bol_byte, 0);
6484 if (dup > 1)
6485 {
6486 char dupstr[40];
6487 int duplen;
6488
6489 /* If you change this format, don't forget to also
6490 change message_log_check_duplicate. */
6491 sprintf (dupstr, " [%d times]", dup);
6492 duplen = strlen (dupstr);
6493 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6494 insert_1 (dupstr, duplen, 1, 0, 1);
6495 }
6496 }
6497 }
6498
6499 /* If we have more than the desired maximum number of lines
6500 in the *Messages* buffer now, delete the oldest ones.
6501 This is safe because we don't have undo in this buffer. */
6502
6503 if (NATNUMP (Vmessage_log_max))
6504 {
6505 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6506 -XFASTINT (Vmessage_log_max) - 1, 0);
6507 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6508 }
6509 }
6510 BEGV = XMARKER (oldbegv)->charpos;
6511 BEGV_BYTE = marker_byte_position (oldbegv);
6512
6513 if (zv_at_end)
6514 {
6515 ZV = Z;
6516 ZV_BYTE = Z_BYTE;
6517 }
6518 else
6519 {
6520 ZV = XMARKER (oldzv)->charpos;
6521 ZV_BYTE = marker_byte_position (oldzv);
6522 }
6523
6524 if (point_at_end)
6525 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6526 else
6527 /* We can't do Fgoto_char (oldpoint) because it will run some
6528 Lisp code. */
6529 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6530 XMARKER (oldpoint)->bytepos);
6531
6532 UNGCPRO;
6533 unchain_marker (XMARKER (oldpoint));
6534 unchain_marker (XMARKER (oldbegv));
6535 unchain_marker (XMARKER (oldzv));
6536
6537 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6538 set_buffer_internal (oldbuf);
6539 if (NILP (tem))
6540 windows_or_buffers_changed = old_windows_or_buffers_changed;
6541 message_log_need_newline = !nlflag;
6542 Vdeactivate_mark = old_deactivate_mark;
6543 }
6544 }
6545
6546
6547 /* We are at the end of the buffer after just having inserted a newline.
6548 (Note: We depend on the fact we won't be crossing the gap.)
6549 Check to see if the most recent message looks a lot like the previous one.
6550 Return 0 if different, 1 if the new one should just replace it, or a
6551 value N > 1 if we should also append " [N times]". */
6552
6553 static int
6554 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6555 int prev_bol, this_bol;
6556 int prev_bol_byte, this_bol_byte;
6557 {
6558 int i;
6559 int len = Z_BYTE - 1 - this_bol_byte;
6560 int seen_dots = 0;
6561 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6562 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6563
6564 for (i = 0; i < len; i++)
6565 {
6566 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6567 seen_dots = 1;
6568 if (p1[i] != p2[i])
6569 return seen_dots;
6570 }
6571 p1 += len;
6572 if (*p1 == '\n')
6573 return 2;
6574 if (*p1++ == ' ' && *p1++ == '[')
6575 {
6576 int n = 0;
6577 while (*p1 >= '0' && *p1 <= '9')
6578 n = n * 10 + *p1++ - '0';
6579 if (strncmp (p1, " times]\n", 8) == 0)
6580 return n+1;
6581 }
6582 return 0;
6583 }
6584
6585
6586 /* Display an echo area message M with a specified length of NBYTES
6587 bytes. The string may include null characters. If M is 0, clear
6588 out any existing message, and let the mini-buffer text show
6589 through.
6590
6591 The buffer M must continue to exist until after the echo area gets
6592 cleared or some other message gets displayed there. This means do
6593 not pass text that is stored in a Lisp string; do not pass text in
6594 a buffer that was alloca'd. */
6595
6596 void
6597 message2 (m, nbytes, multibyte)
6598 const char *m;
6599 int nbytes;
6600 int multibyte;
6601 {
6602 /* First flush out any partial line written with print. */
6603 message_log_maybe_newline ();
6604 if (m)
6605 message_dolog (m, nbytes, 1, multibyte);
6606 message2_nolog (m, nbytes, multibyte);
6607 }
6608
6609
6610 /* The non-logging counterpart of message2. */
6611
6612 void
6613 message2_nolog (m, nbytes, multibyte)
6614 const char *m;
6615 int nbytes, multibyte;
6616 {
6617 struct frame *sf = SELECTED_FRAME ();
6618 message_enable_multibyte = multibyte;
6619
6620 if (noninteractive)
6621 {
6622 if (noninteractive_need_newline)
6623 putc ('\n', stderr);
6624 noninteractive_need_newline = 0;
6625 if (m)
6626 fwrite (m, nbytes, 1, stderr);
6627 if (cursor_in_echo_area == 0)
6628 fprintf (stderr, "\n");
6629 fflush (stderr);
6630 }
6631 /* A null message buffer means that the frame hasn't really been
6632 initialized yet. Error messages get reported properly by
6633 cmd_error, so this must be just an informative message; toss it. */
6634 else if (INTERACTIVE
6635 && sf->glyphs_initialized_p
6636 && FRAME_MESSAGE_BUF (sf))
6637 {
6638 Lisp_Object mini_window;
6639 struct frame *f;
6640
6641 /* Get the frame containing the mini-buffer
6642 that the selected frame is using. */
6643 mini_window = FRAME_MINIBUF_WINDOW (sf);
6644 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6645
6646 FRAME_SAMPLE_VISIBILITY (f);
6647 if (FRAME_VISIBLE_P (sf)
6648 && ! FRAME_VISIBLE_P (f))
6649 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6650
6651 if (m)
6652 {
6653 set_message (m, Qnil, nbytes, multibyte);
6654 if (minibuffer_auto_raise)
6655 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6656 }
6657 else
6658 clear_message (1, 1);
6659
6660 do_pending_window_change (0);
6661 echo_area_display (1);
6662 do_pending_window_change (0);
6663 if (FRAME_DISPLAY (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
6664 (*FRAME_DISPLAY (f)->frame_up_to_date_hook) (f);
6665 }
6666 }
6667
6668
6669 /* Display an echo area message M with a specified length of NBYTES
6670 bytes. The string may include null characters. If M is not a
6671 string, clear out any existing message, and let the mini-buffer
6672 text show through. */
6673
6674 void
6675 message3 (m, nbytes, multibyte)
6676 Lisp_Object m;
6677 int nbytes;
6678 int multibyte;
6679 {
6680 struct gcpro gcpro1;
6681
6682 GCPRO1 (m);
6683
6684 /* First flush out any partial line written with print. */
6685 message_log_maybe_newline ();
6686 if (STRINGP (m))
6687 message_dolog (SDATA (m), nbytes, 1, multibyte);
6688 message3_nolog (m, nbytes, multibyte);
6689
6690 UNGCPRO;
6691 }
6692
6693
6694 /* The non-logging version of message3. */
6695
6696 void
6697 message3_nolog (m, nbytes, multibyte)
6698 Lisp_Object m;
6699 int nbytes, multibyte;
6700 {
6701 struct frame *sf = SELECTED_FRAME ();
6702 message_enable_multibyte = multibyte;
6703
6704 if (noninteractive)
6705 {
6706 if (noninteractive_need_newline)
6707 putc ('\n', stderr);
6708 noninteractive_need_newline = 0;
6709 if (STRINGP (m))
6710 fwrite (SDATA (m), nbytes, 1, stderr);
6711 if (cursor_in_echo_area == 0)
6712 fprintf (stderr, "\n");
6713 fflush (stderr);
6714 }
6715 /* A null message buffer means that the frame hasn't really been
6716 initialized yet. Error messages get reported properly by
6717 cmd_error, so this must be just an informative message; toss it. */
6718 else if (INTERACTIVE
6719 && sf->glyphs_initialized_p
6720 && FRAME_MESSAGE_BUF (sf))
6721 {
6722 Lisp_Object mini_window;
6723 Lisp_Object frame;
6724 struct frame *f;
6725
6726 /* Get the frame containing the mini-buffer
6727 that the selected frame is using. */
6728 mini_window = FRAME_MINIBUF_WINDOW (sf);
6729 frame = XWINDOW (mini_window)->frame;
6730 f = XFRAME (frame);
6731
6732 FRAME_SAMPLE_VISIBILITY (f);
6733 if (FRAME_VISIBLE_P (sf)
6734 && !FRAME_VISIBLE_P (f))
6735 Fmake_frame_visible (frame);
6736
6737 if (STRINGP (m) && SCHARS (m) > 0)
6738 {
6739 set_message (NULL, m, nbytes, multibyte);
6740 if (minibuffer_auto_raise)
6741 Fraise_frame (frame);
6742 }
6743 else
6744 clear_message (1, 1);
6745
6746 do_pending_window_change (0);
6747 echo_area_display (1);
6748 do_pending_window_change (0);
6749 if (FRAME_DISPLAY (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
6750 (*FRAME_DISPLAY (f)->frame_up_to_date_hook) (f);
6751 }
6752 }
6753
6754
6755 /* Display a null-terminated echo area message M. If M is 0, clear
6756 out any existing message, and let the mini-buffer text show through.
6757
6758 The buffer M must continue to exist until after the echo area gets
6759 cleared or some other message gets displayed there. Do not pass
6760 text that is stored in a Lisp string. Do not pass text in a buffer
6761 that was alloca'd. */
6762
6763 void
6764 message1 (m)
6765 char *m;
6766 {
6767 message2 (m, (m ? strlen (m) : 0), 0);
6768 }
6769
6770
6771 /* The non-logging counterpart of message1. */
6772
6773 void
6774 message1_nolog (m)
6775 char *m;
6776 {
6777 message2_nolog (m, (m ? strlen (m) : 0), 0);
6778 }
6779
6780 /* Display a message M which contains a single %s
6781 which gets replaced with STRING. */
6782
6783 void
6784 message_with_string (m, string, log)
6785 char *m;
6786 Lisp_Object string;
6787 int log;
6788 {
6789 CHECK_STRING (string);
6790
6791 if (noninteractive)
6792 {
6793 if (m)
6794 {
6795 if (noninteractive_need_newline)
6796 putc ('\n', stderr);
6797 noninteractive_need_newline = 0;
6798 fprintf (stderr, m, SDATA (string));
6799 if (cursor_in_echo_area == 0)
6800 fprintf (stderr, "\n");
6801 fflush (stderr);
6802 }
6803 }
6804 else if (INTERACTIVE)
6805 {
6806 /* The frame whose minibuffer we're going to display the message on.
6807 It may be larger than the selected frame, so we need
6808 to use its buffer, not the selected frame's buffer. */
6809 Lisp_Object mini_window;
6810 struct frame *f, *sf = SELECTED_FRAME ();
6811
6812 /* Get the frame containing the minibuffer
6813 that the selected frame is using. */
6814 mini_window = FRAME_MINIBUF_WINDOW (sf);
6815 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6816
6817 /* A null message buffer means that the frame hasn't really been
6818 initialized yet. Error messages get reported properly by
6819 cmd_error, so this must be just an informative message; toss it. */
6820 if (FRAME_MESSAGE_BUF (f))
6821 {
6822 Lisp_Object args[2], message;
6823 struct gcpro gcpro1, gcpro2;
6824
6825 args[0] = build_string (m);
6826 args[1] = message = string;
6827 GCPRO2 (args[0], message);
6828 gcpro1.nvars = 2;
6829
6830 message = Fformat (2, args);
6831
6832 if (log)
6833 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6834 else
6835 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6836
6837 UNGCPRO;
6838
6839 /* Print should start at the beginning of the message
6840 buffer next time. */
6841 message_buf_print = 0;
6842 }
6843 }
6844 }
6845
6846
6847 /* Dump an informative message to the minibuf. If M is 0, clear out
6848 any existing message, and let the mini-buffer text show through. */
6849
6850 /* VARARGS 1 */
6851 void
6852 message (m, a1, a2, a3)
6853 char *m;
6854 EMACS_INT a1, a2, a3;
6855 {
6856 if (noninteractive)
6857 {
6858 if (m)
6859 {
6860 if (noninteractive_need_newline)
6861 putc ('\n', stderr);
6862 noninteractive_need_newline = 0;
6863 fprintf (stderr, m, a1, a2, a3);
6864 if (cursor_in_echo_area == 0)
6865 fprintf (stderr, "\n");
6866 fflush (stderr);
6867 }
6868 }
6869 else if (INTERACTIVE)
6870 {
6871 /* The frame whose mini-buffer we're going to display the message
6872 on. It may be larger than the selected frame, so we need to
6873 use its buffer, not the selected frame's buffer. */
6874 Lisp_Object mini_window;
6875 struct frame *f, *sf = SELECTED_FRAME ();
6876
6877 /* Get the frame containing the mini-buffer
6878 that the selected frame is using. */
6879 mini_window = FRAME_MINIBUF_WINDOW (sf);
6880 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6881
6882 /* A null message buffer means that the frame hasn't really been
6883 initialized yet. Error messages get reported properly by
6884 cmd_error, so this must be just an informative message; toss
6885 it. */
6886 if (FRAME_MESSAGE_BUF (f))
6887 {
6888 if (m)
6889 {
6890 int len;
6891 #ifdef NO_ARG_ARRAY
6892 char *a[3];
6893 a[0] = (char *) a1;
6894 a[1] = (char *) a2;
6895 a[2] = (char *) a3;
6896
6897 len = doprnt (FRAME_MESSAGE_BUF (f),
6898 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6899 #else
6900 len = doprnt (FRAME_MESSAGE_BUF (f),
6901 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6902 (char **) &a1);
6903 #endif /* NO_ARG_ARRAY */
6904
6905 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6906 }
6907 else
6908 message1 (0);
6909
6910 /* Print should start at the beginning of the message
6911 buffer next time. */
6912 message_buf_print = 0;
6913 }
6914 }
6915 }
6916
6917
6918 /* The non-logging version of message. */
6919
6920 void
6921 message_nolog (m, a1, a2, a3)
6922 char *m;
6923 EMACS_INT a1, a2, a3;
6924 {
6925 Lisp_Object old_log_max;
6926 old_log_max = Vmessage_log_max;
6927 Vmessage_log_max = Qnil;
6928 message (m, a1, a2, a3);
6929 Vmessage_log_max = old_log_max;
6930 }
6931
6932
6933 /* Display the current message in the current mini-buffer. This is
6934 only called from error handlers in process.c, and is not time
6935 critical. */
6936
6937 void
6938 update_echo_area ()
6939 {
6940 if (!NILP (echo_area_buffer[0]))
6941 {
6942 Lisp_Object string;
6943 string = Fcurrent_message ();
6944 message3 (string, SBYTES (string),
6945 !NILP (current_buffer->enable_multibyte_characters));
6946 }
6947 }
6948
6949
6950 /* Make sure echo area buffers in `echo_buffers' are live.
6951 If they aren't, make new ones. */
6952
6953 static void
6954 ensure_echo_area_buffers ()
6955 {
6956 int i;
6957
6958 for (i = 0; i < 2; ++i)
6959 if (!BUFFERP (echo_buffer[i])
6960 || NILP (XBUFFER (echo_buffer[i])->name))
6961 {
6962 char name[30];
6963 Lisp_Object old_buffer;
6964 int j;
6965
6966 old_buffer = echo_buffer[i];
6967 sprintf (name, " *Echo Area %d*", i);
6968 echo_buffer[i] = Fget_buffer_create (build_string (name));
6969 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6970
6971 for (j = 0; j < 2; ++j)
6972 if (EQ (old_buffer, echo_area_buffer[j]))
6973 echo_area_buffer[j] = echo_buffer[i];
6974 }
6975 }
6976
6977
6978 /* Call FN with args A1..A4 with either the current or last displayed
6979 echo_area_buffer as current buffer.
6980
6981 WHICH zero means use the current message buffer
6982 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6983 from echo_buffer[] and clear it.
6984
6985 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6986 suitable buffer from echo_buffer[] and clear it.
6987
6988 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6989 that the current message becomes the last displayed one, make
6990 choose a suitable buffer for echo_area_buffer[0], and clear it.
6991
6992 Value is what FN returns. */
6993
6994 static int
6995 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6996 struct window *w;
6997 int which;
6998 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6999 EMACS_INT a1;
7000 Lisp_Object a2;
7001 EMACS_INT a3, a4;
7002 {
7003 Lisp_Object buffer;
7004 int this_one, the_other, clear_buffer_p, rc;
7005 int count = SPECPDL_INDEX ();
7006
7007 /* If buffers aren't live, make new ones. */
7008 ensure_echo_area_buffers ();
7009
7010 clear_buffer_p = 0;
7011
7012 if (which == 0)
7013 this_one = 0, the_other = 1;
7014 else if (which > 0)
7015 this_one = 1, the_other = 0;
7016 else
7017 {
7018 this_one = 0, the_other = 1;
7019 clear_buffer_p = 1;
7020
7021 /* We need a fresh one in case the current echo buffer equals
7022 the one containing the last displayed echo area message. */
7023 if (!NILP (echo_area_buffer[this_one])
7024 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
7025 echo_area_buffer[this_one] = Qnil;
7026 }
7027
7028 /* Choose a suitable buffer from echo_buffer[] is we don't
7029 have one. */
7030 if (NILP (echo_area_buffer[this_one]))
7031 {
7032 echo_area_buffer[this_one]
7033 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7034 ? echo_buffer[the_other]
7035 : echo_buffer[this_one]);
7036 clear_buffer_p = 1;
7037 }
7038
7039 buffer = echo_area_buffer[this_one];
7040
7041 /* Don't get confused by reusing the buffer used for echoing
7042 for a different purpose. */
7043 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7044 cancel_echoing ();
7045
7046 record_unwind_protect (unwind_with_echo_area_buffer,
7047 with_echo_area_buffer_unwind_data (w));
7048
7049 /* Make the echo area buffer current. Note that for display
7050 purposes, it is not necessary that the displayed window's buffer
7051 == current_buffer, except for text property lookup. So, let's
7052 only set that buffer temporarily here without doing a full
7053 Fset_window_buffer. We must also change w->pointm, though,
7054 because otherwise an assertions in unshow_buffer fails, and Emacs
7055 aborts. */
7056 set_buffer_internal_1 (XBUFFER (buffer));
7057 if (w)
7058 {
7059 w->buffer = buffer;
7060 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7061 }
7062
7063 current_buffer->undo_list = Qt;
7064 current_buffer->read_only = Qnil;
7065 specbind (Qinhibit_read_only, Qt);
7066 specbind (Qinhibit_modification_hooks, Qt);
7067
7068 if (clear_buffer_p && Z > BEG)
7069 del_range (BEG, Z);
7070
7071 xassert (BEGV >= BEG);
7072 xassert (ZV <= Z && ZV >= BEGV);
7073
7074 rc = fn (a1, a2, a3, a4);
7075
7076 xassert (BEGV >= BEG);
7077 xassert (ZV <= Z && ZV >= BEGV);
7078
7079 unbind_to (count, Qnil);
7080 return rc;
7081 }
7082
7083
7084 /* Save state that should be preserved around the call to the function
7085 FN called in with_echo_area_buffer. */
7086
7087 static Lisp_Object
7088 with_echo_area_buffer_unwind_data (w)
7089 struct window *w;
7090 {
7091 int i = 0;
7092 Lisp_Object vector;
7093
7094 /* Reduce consing by keeping one vector in
7095 Vwith_echo_area_save_vector. */
7096 vector = Vwith_echo_area_save_vector;
7097 Vwith_echo_area_save_vector = Qnil;
7098
7099 if (NILP (vector))
7100 vector = Fmake_vector (make_number (7), Qnil);
7101
7102 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7103 AREF (vector, i) = Vdeactivate_mark, ++i;
7104 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7105
7106 if (w)
7107 {
7108 XSETWINDOW (AREF (vector, i), w); ++i;
7109 AREF (vector, i) = w->buffer; ++i;
7110 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7111 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7112 }
7113 else
7114 {
7115 int end = i + 4;
7116 for (; i < end; ++i)
7117 AREF (vector, i) = Qnil;
7118 }
7119
7120 xassert (i == ASIZE (vector));
7121 return vector;
7122 }
7123
7124
7125 /* Restore global state from VECTOR which was created by
7126 with_echo_area_buffer_unwind_data. */
7127
7128 static Lisp_Object
7129 unwind_with_echo_area_buffer (vector)
7130 Lisp_Object vector;
7131 {
7132 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7133 Vdeactivate_mark = AREF (vector, 1);
7134 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7135
7136 if (WINDOWP (AREF (vector, 3)))
7137 {
7138 struct window *w;
7139 Lisp_Object buffer, charpos, bytepos;
7140
7141 w = XWINDOW (AREF (vector, 3));
7142 buffer = AREF (vector, 4);
7143 charpos = AREF (vector, 5);
7144 bytepos = AREF (vector, 6);
7145
7146 w->buffer = buffer;
7147 set_marker_both (w->pointm, buffer,
7148 XFASTINT (charpos), XFASTINT (bytepos));
7149 }
7150
7151 Vwith_echo_area_save_vector = vector;
7152 return Qnil;
7153 }
7154
7155
7156 /* Set up the echo area for use by print functions. MULTIBYTE_P
7157 non-zero means we will print multibyte. */
7158
7159 void
7160 setup_echo_area_for_printing (multibyte_p)
7161 int multibyte_p;
7162 {
7163 /* If we can't find an echo area any more, exit. */
7164 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7165 Fkill_emacs (Qnil);
7166
7167 ensure_echo_area_buffers ();
7168
7169 if (!message_buf_print)
7170 {
7171 /* A message has been output since the last time we printed.
7172 Choose a fresh echo area buffer. */
7173 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7174 echo_area_buffer[0] = echo_buffer[1];
7175 else
7176 echo_area_buffer[0] = echo_buffer[0];
7177
7178 /* Switch to that buffer and clear it. */
7179 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7180 current_buffer->truncate_lines = Qnil;
7181
7182 if (Z > BEG)
7183 {
7184 int count = SPECPDL_INDEX ();
7185 specbind (Qinhibit_read_only, Qt);
7186 /* Note that undo recording is always disabled. */
7187 del_range (BEG, Z);
7188 unbind_to (count, Qnil);
7189 }
7190 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7191
7192 /* Set up the buffer for the multibyteness we need. */
7193 if (multibyte_p
7194 != !NILP (current_buffer->enable_multibyte_characters))
7195 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7196
7197 /* Raise the frame containing the echo area. */
7198 if (minibuffer_auto_raise)
7199 {
7200 struct frame *sf = SELECTED_FRAME ();
7201 Lisp_Object mini_window;
7202 mini_window = FRAME_MINIBUF_WINDOW (sf);
7203 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7204 }
7205
7206 message_log_maybe_newline ();
7207 message_buf_print = 1;
7208 }
7209 else
7210 {
7211 if (NILP (echo_area_buffer[0]))
7212 {
7213 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7214 echo_area_buffer[0] = echo_buffer[1];
7215 else
7216 echo_area_buffer[0] = echo_buffer[0];
7217 }
7218
7219 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7220 {
7221 /* Someone switched buffers between print requests. */
7222 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7223 current_buffer->truncate_lines = Qnil;
7224 }
7225 }
7226 }
7227
7228
7229 /* Display an echo area message in window W. Value is non-zero if W's
7230 height is changed. If display_last_displayed_message_p is
7231 non-zero, display the message that was last displayed, otherwise
7232 display the current message. */
7233
7234 static int
7235 display_echo_area (w)
7236 struct window *w;
7237 {
7238 int i, no_message_p, window_height_changed_p, count;
7239
7240 /* Temporarily disable garbage collections while displaying the echo
7241 area. This is done because a GC can print a message itself.
7242 That message would modify the echo area buffer's contents while a
7243 redisplay of the buffer is going on, and seriously confuse
7244 redisplay. */
7245 count = inhibit_garbage_collection ();
7246
7247 /* If there is no message, we must call display_echo_area_1
7248 nevertheless because it resizes the window. But we will have to
7249 reset the echo_area_buffer in question to nil at the end because
7250 with_echo_area_buffer will sets it to an empty buffer. */
7251 i = display_last_displayed_message_p ? 1 : 0;
7252 no_message_p = NILP (echo_area_buffer[i]);
7253
7254 window_height_changed_p
7255 = with_echo_area_buffer (w, display_last_displayed_message_p,
7256 display_echo_area_1,
7257 (EMACS_INT) w, Qnil, 0, 0);
7258
7259 if (no_message_p)
7260 echo_area_buffer[i] = Qnil;
7261
7262 unbind_to (count, Qnil);
7263 return window_height_changed_p;
7264 }
7265
7266
7267 /* Helper for display_echo_area. Display the current buffer which
7268 contains the current echo area message in window W, a mini-window,
7269 a pointer to which is passed in A1. A2..A4 are currently not used.
7270 Change the height of W so that all of the message is displayed.
7271 Value is non-zero if height of W was changed. */
7272
7273 static int
7274 display_echo_area_1 (a1, a2, a3, a4)
7275 EMACS_INT a1;
7276 Lisp_Object a2;
7277 EMACS_INT a3, a4;
7278 {
7279 struct window *w = (struct window *) a1;
7280 Lisp_Object window;
7281 struct text_pos start;
7282 int window_height_changed_p = 0;
7283
7284 /* Do this before displaying, so that we have a large enough glyph
7285 matrix for the display. */
7286 window_height_changed_p = resize_mini_window (w, 0);
7287
7288 /* Display. */
7289 clear_glyph_matrix (w->desired_matrix);
7290 XSETWINDOW (window, w);
7291 SET_TEXT_POS (start, BEG, BEG_BYTE);
7292 try_window (window, start);
7293
7294 return window_height_changed_p;
7295 }
7296
7297
7298 /* Resize the echo area window to exactly the size needed for the
7299 currently displayed message, if there is one. If a mini-buffer
7300 is active, don't shrink it. */
7301
7302 void
7303 resize_echo_area_exactly ()
7304 {
7305 if (BUFFERP (echo_area_buffer[0])
7306 && WINDOWP (echo_area_window))
7307 {
7308 struct window *w = XWINDOW (echo_area_window);
7309 int resized_p;
7310 Lisp_Object resize_exactly;
7311
7312 if (minibuf_level == 0)
7313 resize_exactly = Qt;
7314 else
7315 resize_exactly = Qnil;
7316
7317 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7318 (EMACS_INT) w, resize_exactly, 0, 0);
7319 if (resized_p)
7320 {
7321 ++windows_or_buffers_changed;
7322 ++update_mode_lines;
7323 redisplay_internal (0);
7324 }
7325 }
7326 }
7327
7328
7329 /* Callback function for with_echo_area_buffer, when used from
7330 resize_echo_area_exactly. A1 contains a pointer to the window to
7331 resize, EXACTLY non-nil means resize the mini-window exactly to the
7332 size of the text displayed. A3 and A4 are not used. Value is what
7333 resize_mini_window returns. */
7334
7335 static int
7336 resize_mini_window_1 (a1, exactly, a3, a4)
7337 EMACS_INT a1;
7338 Lisp_Object exactly;
7339 EMACS_INT a3, a4;
7340 {
7341 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7342 }
7343
7344
7345 /* Resize mini-window W to fit the size of its contents. EXACT:P
7346 means size the window exactly to the size needed. Otherwise, it's
7347 only enlarged until W's buffer is empty. Value is non-zero if
7348 the window height has been changed. */
7349
7350 int
7351 resize_mini_window (w, exact_p)
7352 struct window *w;
7353 int exact_p;
7354 {
7355 struct frame *f = XFRAME (w->frame);
7356 int window_height_changed_p = 0;
7357
7358 xassert (MINI_WINDOW_P (w));
7359
7360 /* Don't resize windows while redisplaying a window; it would
7361 confuse redisplay functions when the size of the window they are
7362 displaying changes from under them. Such a resizing can happen,
7363 for instance, when which-func prints a long message while
7364 we are running fontification-functions. We're running these
7365 functions with safe_call which binds inhibit-redisplay to t. */
7366 if (!NILP (Vinhibit_redisplay))
7367 return 0;
7368
7369 /* Nil means don't try to resize. */
7370 if (NILP (Vresize_mini_windows)
7371 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7372 return 0;
7373
7374 if (!FRAME_MINIBUF_ONLY_P (f))
7375 {
7376 struct it it;
7377 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7378 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7379 int height, max_height;
7380 int unit = FRAME_LINE_HEIGHT (f);
7381 struct text_pos start;
7382 struct buffer *old_current_buffer = NULL;
7383
7384 if (current_buffer != XBUFFER (w->buffer))
7385 {
7386 old_current_buffer = current_buffer;
7387 set_buffer_internal (XBUFFER (w->buffer));
7388 }
7389
7390 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7391
7392 /* Compute the max. number of lines specified by the user. */
7393 if (FLOATP (Vmax_mini_window_height))
7394 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7395 else if (INTEGERP (Vmax_mini_window_height))
7396 max_height = XINT (Vmax_mini_window_height);
7397 else
7398 max_height = total_height / 4;
7399
7400 /* Correct that max. height if it's bogus. */
7401 max_height = max (1, max_height);
7402 max_height = min (total_height, max_height);
7403
7404 /* Find out the height of the text in the window. */
7405 if (it.truncate_lines_p)
7406 height = 1;
7407 else
7408 {
7409 last_height = 0;
7410 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7411 if (it.max_ascent == 0 && it.max_descent == 0)
7412 height = it.current_y + last_height;
7413 else
7414 height = it.current_y + it.max_ascent + it.max_descent;
7415 height -= it.extra_line_spacing;
7416 height = (height + unit - 1) / unit;
7417 }
7418
7419 /* Compute a suitable window start. */
7420 if (height > max_height)
7421 {
7422 height = max_height;
7423 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7424 move_it_vertically_backward (&it, (height - 1) * unit);
7425 start = it.current.pos;
7426 }
7427 else
7428 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7429 SET_MARKER_FROM_TEXT_POS (w->start, start);
7430
7431 if (EQ (Vresize_mini_windows, Qgrow_only))
7432 {
7433 /* Let it grow only, until we display an empty message, in which
7434 case the window shrinks again. */
7435 if (height > WINDOW_TOTAL_LINES (w))
7436 {
7437 int old_height = WINDOW_TOTAL_LINES (w);
7438 freeze_window_starts (f, 1);
7439 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7440 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7441 }
7442 else if (height < WINDOW_TOTAL_LINES (w)
7443 && (exact_p || BEGV == ZV))
7444 {
7445 int old_height = WINDOW_TOTAL_LINES (w);
7446 freeze_window_starts (f, 0);
7447 shrink_mini_window (w);
7448 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7449 }
7450 }
7451 else
7452 {
7453 /* Always resize to exact size needed. */
7454 if (height > WINDOW_TOTAL_LINES (w))
7455 {
7456 int old_height = WINDOW_TOTAL_LINES (w);
7457 freeze_window_starts (f, 1);
7458 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7459 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7460 }
7461 else if (height < WINDOW_TOTAL_LINES (w))
7462 {
7463 int old_height = WINDOW_TOTAL_LINES (w);
7464 freeze_window_starts (f, 0);
7465 shrink_mini_window (w);
7466
7467 if (height)
7468 {
7469 freeze_window_starts (f, 1);
7470 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7471 }
7472
7473 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7474 }
7475 }
7476
7477 if (old_current_buffer)
7478 set_buffer_internal (old_current_buffer);
7479 }
7480
7481 return window_height_changed_p;
7482 }
7483
7484
7485 /* Value is the current message, a string, or nil if there is no
7486 current message. */
7487
7488 Lisp_Object
7489 current_message ()
7490 {
7491 Lisp_Object msg;
7492
7493 if (NILP (echo_area_buffer[0]))
7494 msg = Qnil;
7495 else
7496 {
7497 with_echo_area_buffer (0, 0, current_message_1,
7498 (EMACS_INT) &msg, Qnil, 0, 0);
7499 if (NILP (msg))
7500 echo_area_buffer[0] = Qnil;
7501 }
7502
7503 return msg;
7504 }
7505
7506
7507 static int
7508 current_message_1 (a1, a2, a3, a4)
7509 EMACS_INT a1;
7510 Lisp_Object a2;
7511 EMACS_INT a3, a4;
7512 {
7513 Lisp_Object *msg = (Lisp_Object *) a1;
7514
7515 if (Z > BEG)
7516 *msg = make_buffer_string (BEG, Z, 1);
7517 else
7518 *msg = Qnil;
7519 return 0;
7520 }
7521
7522
7523 /* Push the current message on Vmessage_stack for later restauration
7524 by restore_message. Value is non-zero if the current message isn't
7525 empty. This is a relatively infrequent operation, so it's not
7526 worth optimizing. */
7527
7528 int
7529 push_message ()
7530 {
7531 Lisp_Object msg;
7532 msg = current_message ();
7533 Vmessage_stack = Fcons (msg, Vmessage_stack);
7534 return STRINGP (msg);
7535 }
7536
7537
7538 /* Restore message display from the top of Vmessage_stack. */
7539
7540 void
7541 restore_message ()
7542 {
7543 Lisp_Object msg;
7544
7545 xassert (CONSP (Vmessage_stack));
7546 msg = XCAR (Vmessage_stack);
7547 if (STRINGP (msg))
7548 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7549 else
7550 message3_nolog (msg, 0, 0);
7551 }
7552
7553
7554 /* Handler for record_unwind_protect calling pop_message. */
7555
7556 Lisp_Object
7557 pop_message_unwind (dummy)
7558 Lisp_Object dummy;
7559 {
7560 pop_message ();
7561 return Qnil;
7562 }
7563
7564 /* Pop the top-most entry off Vmessage_stack. */
7565
7566 void
7567 pop_message ()
7568 {
7569 xassert (CONSP (Vmessage_stack));
7570 Vmessage_stack = XCDR (Vmessage_stack);
7571 }
7572
7573
7574 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7575 exits. If the stack is not empty, we have a missing pop_message
7576 somewhere. */
7577
7578 void
7579 check_message_stack ()
7580 {
7581 if (!NILP (Vmessage_stack))
7582 abort ();
7583 }
7584
7585
7586 /* Truncate to NCHARS what will be displayed in the echo area the next
7587 time we display it---but don't redisplay it now. */
7588
7589 void
7590 truncate_echo_area (nchars)
7591 int nchars;
7592 {
7593 if (nchars == 0)
7594 echo_area_buffer[0] = Qnil;
7595 /* A null message buffer means that the frame hasn't really been
7596 initialized yet. Error messages get reported properly by
7597 cmd_error, so this must be just an informative message; toss it. */
7598 else if (!noninteractive
7599 && INTERACTIVE
7600 && !NILP (echo_area_buffer[0]))
7601 {
7602 struct frame *sf = SELECTED_FRAME ();
7603 if (FRAME_MESSAGE_BUF (sf))
7604 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7605 }
7606 }
7607
7608
7609 /* Helper function for truncate_echo_area. Truncate the current
7610 message to at most NCHARS characters. */
7611
7612 static int
7613 truncate_message_1 (nchars, a2, a3, a4)
7614 EMACS_INT nchars;
7615 Lisp_Object a2;
7616 EMACS_INT a3, a4;
7617 {
7618 if (BEG + nchars < Z)
7619 del_range (BEG + nchars, Z);
7620 if (Z == BEG)
7621 echo_area_buffer[0] = Qnil;
7622 return 0;
7623 }
7624
7625
7626 /* Set the current message to a substring of S or STRING.
7627
7628 If STRING is a Lisp string, set the message to the first NBYTES
7629 bytes from STRING. NBYTES zero means use the whole string. If
7630 STRING is multibyte, the message will be displayed multibyte.
7631
7632 If S is not null, set the message to the first LEN bytes of S. LEN
7633 zero means use the whole string. MULTIBYTE_P non-zero means S is
7634 multibyte. Display the message multibyte in that case. */
7635
7636 void
7637 set_message (s, string, nbytes, multibyte_p)
7638 const char *s;
7639 Lisp_Object string;
7640 int nbytes, multibyte_p;
7641 {
7642 message_enable_multibyte
7643 = ((s && multibyte_p)
7644 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7645
7646 with_echo_area_buffer (0, -1, set_message_1,
7647 (EMACS_INT) s, string, nbytes, multibyte_p);
7648 message_buf_print = 0;
7649 help_echo_showing_p = 0;
7650 }
7651
7652
7653 /* Helper function for set_message. Arguments have the same meaning
7654 as there, with A1 corresponding to S and A2 corresponding to STRING
7655 This function is called with the echo area buffer being
7656 current. */
7657
7658 static int
7659 set_message_1 (a1, a2, nbytes, multibyte_p)
7660 EMACS_INT a1;
7661 Lisp_Object a2;
7662 EMACS_INT nbytes, multibyte_p;
7663 {
7664 const char *s = (const char *) a1;
7665 Lisp_Object string = a2;
7666
7667 xassert (BEG == Z);
7668
7669 /* Change multibyteness of the echo buffer appropriately. */
7670 if (message_enable_multibyte
7671 != !NILP (current_buffer->enable_multibyte_characters))
7672 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7673
7674 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7675
7676 /* Insert new message at BEG. */
7677 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7678
7679 if (STRINGP (string))
7680 {
7681 int nchars;
7682
7683 if (nbytes == 0)
7684 nbytes = SBYTES (string);
7685 nchars = string_byte_to_char (string, nbytes);
7686
7687 /* This function takes care of single/multibyte conversion. We
7688 just have to ensure that the echo area buffer has the right
7689 setting of enable_multibyte_characters. */
7690 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7691 }
7692 else if (s)
7693 {
7694 if (nbytes == 0)
7695 nbytes = strlen (s);
7696
7697 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7698 {
7699 /* Convert from multi-byte to single-byte. */
7700 int i, c, n;
7701 unsigned char work[1];
7702
7703 /* Convert a multibyte string to single-byte. */
7704 for (i = 0; i < nbytes; i += n)
7705 {
7706 c = string_char_and_length (s + i, nbytes - i, &n);
7707 work[0] = (SINGLE_BYTE_CHAR_P (c)
7708 ? c
7709 : multibyte_char_to_unibyte (c, Qnil));
7710 insert_1_both (work, 1, 1, 1, 0, 0);
7711 }
7712 }
7713 else if (!multibyte_p
7714 && !NILP (current_buffer->enable_multibyte_characters))
7715 {
7716 /* Convert from single-byte to multi-byte. */
7717 int i, c, n;
7718 const unsigned char *msg = (const unsigned char *) s;
7719 unsigned char str[MAX_MULTIBYTE_LENGTH];
7720
7721 /* Convert a single-byte string to multibyte. */
7722 for (i = 0; i < nbytes; i++)
7723 {
7724 c = unibyte_char_to_multibyte (msg[i]);
7725 n = CHAR_STRING (c, str);
7726 insert_1_both (str, 1, n, 1, 0, 0);
7727 }
7728 }
7729 else
7730 insert_1 (s, nbytes, 1, 0, 0);
7731 }
7732
7733 return 0;
7734 }
7735
7736
7737 /* Clear messages. CURRENT_P non-zero means clear the current
7738 message. LAST_DISPLAYED_P non-zero means clear the message
7739 last displayed. */
7740
7741 void
7742 clear_message (current_p, last_displayed_p)
7743 int current_p, last_displayed_p;
7744 {
7745 if (current_p)
7746 {
7747 echo_area_buffer[0] = Qnil;
7748 message_cleared_p = 1;
7749 }
7750
7751 if (last_displayed_p)
7752 echo_area_buffer[1] = Qnil;
7753
7754 message_buf_print = 0;
7755 }
7756
7757 /* Clear garbaged frames.
7758
7759 This function is used where the old redisplay called
7760 redraw_garbaged_frames which in turn called redraw_frame which in
7761 turn called clear_frame. The call to clear_frame was a source of
7762 flickering. I believe a clear_frame is not necessary. It should
7763 suffice in the new redisplay to invalidate all current matrices,
7764 and ensure a complete redisplay of all windows. */
7765
7766 static void
7767 clear_garbaged_frames ()
7768 {
7769 if (frame_garbaged)
7770 {
7771 Lisp_Object tail, frame;
7772 int changed_count = 0;
7773
7774 FOR_EACH_FRAME (tail, frame)
7775 {
7776 struct frame *f = XFRAME (frame);
7777
7778 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7779 {
7780 if (f->resized_p)
7781 {
7782 Fredraw_frame (frame);
7783 f->force_flush_display_p = 1;
7784 }
7785 clear_current_matrices (f);
7786 changed_count++;
7787 f->garbaged = 0;
7788 f->resized_p = 0;
7789 }
7790 }
7791
7792 frame_garbaged = 0;
7793 if (changed_count)
7794 ++windows_or_buffers_changed;
7795 }
7796 }
7797
7798
7799 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7800 is non-zero update selected_frame. Value is non-zero if the
7801 mini-windows height has been changed. */
7802
7803 static int
7804 echo_area_display (update_frame_p)
7805 int update_frame_p;
7806 {
7807 Lisp_Object mini_window;
7808 struct window *w;
7809 struct frame *f;
7810 int window_height_changed_p = 0;
7811 struct frame *sf = SELECTED_FRAME ();
7812
7813 mini_window = FRAME_MINIBUF_WINDOW (sf);
7814 w = XWINDOW (mini_window);
7815 f = XFRAME (WINDOW_FRAME (w));
7816
7817 /* Don't display if frame is invisible or not yet initialized. */
7818 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7819 return 0;
7820
7821 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7822 #ifndef MAC_OS8
7823 #ifdef HAVE_WINDOW_SYSTEM
7824 /* When Emacs starts, selected_frame may be the initial terminal
7825 frame. If we let this through, a message would be displayed on
7826 the terminal. */
7827 if (FRAME_TERMCAP_P (XFRAME (selected_frame))
7828 && FRAME_TTY (XFRAME (selected_frame))->type == NULL)
7829 return 0;
7830 #endif /* HAVE_WINDOW_SYSTEM */
7831 #endif
7832
7833 /* Redraw garbaged frames. */
7834 if (frame_garbaged)
7835 clear_garbaged_frames ();
7836
7837 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7838 {
7839 echo_area_window = mini_window;
7840 window_height_changed_p = display_echo_area (w);
7841 w->must_be_updated_p = 1;
7842
7843 /* Update the display, unless called from redisplay_internal.
7844 Also don't update the screen during redisplay itself. The
7845 update will happen at the end of redisplay, and an update
7846 here could cause confusion. */
7847 if (update_frame_p && !redisplaying_p)
7848 {
7849 int n = 0;
7850
7851 /* If the display update has been interrupted by pending
7852 input, update mode lines in the frame. Due to the
7853 pending input, it might have been that redisplay hasn't
7854 been called, so that mode lines above the echo area are
7855 garbaged. This looks odd, so we prevent it here. */
7856 if (!display_completed)
7857 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7858
7859 if (window_height_changed_p
7860 /* Don't do this if Emacs is shutting down. Redisplay
7861 needs to run hooks. */
7862 && !NILP (Vrun_hooks))
7863 {
7864 /* Must update other windows. Likewise as in other
7865 cases, don't let this update be interrupted by
7866 pending input. */
7867 int count = SPECPDL_INDEX ();
7868 specbind (Qredisplay_dont_pause, Qt);
7869 windows_or_buffers_changed = 1;
7870 redisplay_internal (0);
7871 unbind_to (count, Qnil);
7872 }
7873 else if (FRAME_WINDOW_P (f) && n == 0)
7874 {
7875 /* Window configuration is the same as before.
7876 Can do with a display update of the echo area,
7877 unless we displayed some mode lines. */
7878 update_single_window (w, 1);
7879 FRAME_RIF (f)->flush_display (f);
7880 }
7881 else
7882 update_frame (f, 1, 1);
7883
7884 /* If cursor is in the echo area, make sure that the next
7885 redisplay displays the minibuffer, so that the cursor will
7886 be replaced with what the minibuffer wants. */
7887 if (cursor_in_echo_area)
7888 ++windows_or_buffers_changed;
7889 }
7890 }
7891 else if (!EQ (mini_window, selected_window))
7892 windows_or_buffers_changed++;
7893
7894 /* Last displayed message is now the current message. */
7895 echo_area_buffer[1] = echo_area_buffer[0];
7896
7897 /* Prevent redisplay optimization in redisplay_internal by resetting
7898 this_line_start_pos. This is done because the mini-buffer now
7899 displays the message instead of its buffer text. */
7900 if (EQ (mini_window, selected_window))
7901 CHARPOS (this_line_start_pos) = 0;
7902
7903 return window_height_changed_p;
7904 }
7905
7906
7907 \f
7908 /***********************************************************************
7909 Frame Titles
7910 ***********************************************************************/
7911
7912
7913 /* The frame title buffering code is also used by Fformat_mode_line.
7914 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
7915
7916 /* A buffer for constructing frame titles in it; allocated from the
7917 heap in init_xdisp and resized as needed in store_frame_title_char. */
7918
7919 static char *frame_title_buf;
7920
7921 /* The buffer's end, and a current output position in it. */
7922
7923 static char *frame_title_buf_end;
7924 static char *frame_title_ptr;
7925
7926
7927 /* Store a single character C for the frame title in frame_title_buf.
7928 Re-allocate frame_title_buf if necessary. */
7929
7930 static void
7931 #ifdef PROTOTYPES
7932 store_frame_title_char (char c)
7933 #else
7934 store_frame_title_char (c)
7935 char c;
7936 #endif
7937 {
7938 /* If output position has reached the end of the allocated buffer,
7939 double the buffer's size. */
7940 if (frame_title_ptr == frame_title_buf_end)
7941 {
7942 int len = frame_title_ptr - frame_title_buf;
7943 int new_size = 2 * len * sizeof *frame_title_buf;
7944 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7945 frame_title_buf_end = frame_title_buf + new_size;
7946 frame_title_ptr = frame_title_buf + len;
7947 }
7948
7949 *frame_title_ptr++ = c;
7950 }
7951
7952
7953 /* Store part of a frame title in frame_title_buf, beginning at
7954 frame_title_ptr. STR is the string to store. Do not copy
7955 characters that yield more columns than PRECISION; PRECISION <= 0
7956 means copy the whole string. Pad with spaces until FIELD_WIDTH
7957 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7958 pad. Called from display_mode_element when it is used to build a
7959 frame title. */
7960
7961 static int
7962 store_frame_title (str, field_width, precision)
7963 const unsigned char *str;
7964 int field_width, precision;
7965 {
7966 int n = 0;
7967 int dummy, nbytes;
7968
7969 /* Copy at most PRECISION chars from STR. */
7970 nbytes = strlen (str);
7971 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7972 while (nbytes--)
7973 store_frame_title_char (*str++);
7974
7975 /* Fill up with spaces until FIELD_WIDTH reached. */
7976 while (field_width > 0
7977 && n < field_width)
7978 {
7979 store_frame_title_char (' ');
7980 ++n;
7981 }
7982
7983 return n;
7984 }
7985
7986 #ifdef HAVE_WINDOW_SYSTEM
7987
7988 /* Set the title of FRAME, if it has changed. The title format is
7989 Vicon_title_format if FRAME is iconified, otherwise it is
7990 frame_title_format. */
7991
7992 static void
7993 x_consider_frame_title (frame)
7994 Lisp_Object frame;
7995 {
7996 struct frame *f = XFRAME (frame);
7997
7998 if (FRAME_WINDOW_P (f)
7999 || FRAME_MINIBUF_ONLY_P (f)
8000 || f->explicit_name)
8001 {
8002 /* Do we have more than one visible frame on this X display? */
8003 Lisp_Object tail;
8004 Lisp_Object fmt;
8005 struct buffer *obuf;
8006 int len;
8007 struct it it;
8008
8009 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8010 {
8011 Lisp_Object other_frame = XCAR (tail);
8012 struct frame *tf = XFRAME (other_frame);
8013
8014 if (tf != f
8015 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8016 && !FRAME_MINIBUF_ONLY_P (tf)
8017 && !EQ (other_frame, tip_frame)
8018 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8019 break;
8020 }
8021
8022 /* Set global variable indicating that multiple frames exist. */
8023 multiple_frames = CONSP (tail);
8024
8025 /* Switch to the buffer of selected window of the frame. Set up
8026 frame_title_ptr so that display_mode_element will output into it;
8027 then display the title. */
8028 obuf = current_buffer;
8029 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8030 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8031 frame_title_ptr = frame_title_buf;
8032 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8033 NULL, DEFAULT_FACE_ID);
8034 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8035 len = frame_title_ptr - frame_title_buf;
8036 frame_title_ptr = NULL;
8037 set_buffer_internal_1 (obuf);
8038
8039 /* Set the title only if it's changed. This avoids consing in
8040 the common case where it hasn't. (If it turns out that we've
8041 already wasted too much time by walking through the list with
8042 display_mode_element, then we might need to optimize at a
8043 higher level than this.) */
8044 if (! STRINGP (f->name)
8045 || SBYTES (f->name) != len
8046 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
8047 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
8048 }
8049 }
8050
8051 #endif /* not HAVE_WINDOW_SYSTEM */
8052
8053
8054
8055 \f
8056 /***********************************************************************
8057 Menu Bars
8058 ***********************************************************************/
8059
8060
8061 /* Prepare for redisplay by updating menu-bar item lists when
8062 appropriate. This can call eval. */
8063
8064 void
8065 prepare_menu_bars ()
8066 {
8067 int all_windows;
8068 struct gcpro gcpro1, gcpro2;
8069 struct frame *f;
8070 Lisp_Object tooltip_frame;
8071
8072 #ifdef HAVE_WINDOW_SYSTEM
8073 tooltip_frame = tip_frame;
8074 #else
8075 tooltip_frame = Qnil;
8076 #endif
8077
8078 /* Update all frame titles based on their buffer names, etc. We do
8079 this before the menu bars so that the buffer-menu will show the
8080 up-to-date frame titles. */
8081 #ifdef HAVE_WINDOW_SYSTEM
8082 if (windows_or_buffers_changed || update_mode_lines)
8083 {
8084 Lisp_Object tail, frame;
8085
8086 FOR_EACH_FRAME (tail, frame)
8087 {
8088 f = XFRAME (frame);
8089 if (!EQ (frame, tooltip_frame)
8090 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8091 x_consider_frame_title (frame);
8092 }
8093 }
8094 #endif /* HAVE_WINDOW_SYSTEM */
8095
8096 /* Update the menu bar item lists, if appropriate. This has to be
8097 done before any actual redisplay or generation of display lines. */
8098 all_windows = (update_mode_lines
8099 || buffer_shared > 1
8100 || windows_or_buffers_changed);
8101 if (all_windows)
8102 {
8103 Lisp_Object tail, frame;
8104 int count = SPECPDL_INDEX ();
8105
8106 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8107
8108 FOR_EACH_FRAME (tail, frame)
8109 {
8110 f = XFRAME (frame);
8111
8112 /* Ignore tooltip frame. */
8113 if (EQ (frame, tooltip_frame))
8114 continue;
8115
8116 /* If a window on this frame changed size, report that to
8117 the user and clear the size-change flag. */
8118 if (FRAME_WINDOW_SIZES_CHANGED (f))
8119 {
8120 Lisp_Object functions;
8121
8122 /* Clear flag first in case we get an error below. */
8123 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8124 functions = Vwindow_size_change_functions;
8125 GCPRO2 (tail, functions);
8126
8127 while (CONSP (functions))
8128 {
8129 call1 (XCAR (functions), frame);
8130 functions = XCDR (functions);
8131 }
8132 UNGCPRO;
8133 }
8134
8135 GCPRO1 (tail);
8136 update_menu_bar (f, 0);
8137 #ifdef HAVE_WINDOW_SYSTEM
8138 update_tool_bar (f, 0);
8139 #endif
8140 UNGCPRO;
8141 }
8142
8143 unbind_to (count, Qnil);
8144 }
8145 else
8146 {
8147 struct frame *sf = SELECTED_FRAME ();
8148 update_menu_bar (sf, 1);
8149 #ifdef HAVE_WINDOW_SYSTEM
8150 update_tool_bar (sf, 1);
8151 #endif
8152 }
8153
8154 /* Motif needs this. See comment in xmenu.c. Turn it off when
8155 pending_menu_activation is not defined. */
8156 #ifdef USE_X_TOOLKIT
8157 pending_menu_activation = 0;
8158 #endif
8159 }
8160
8161
8162 /* Update the menu bar item list for frame F. This has to be done
8163 before we start to fill in any display lines, because it can call
8164 eval.
8165
8166 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8167
8168 static void
8169 update_menu_bar (f, save_match_data)
8170 struct frame *f;
8171 int save_match_data;
8172 {
8173 Lisp_Object window;
8174 register struct window *w;
8175
8176 /* If called recursively during a menu update, do nothing. This can
8177 happen when, for instance, an activate-menubar-hook causes a
8178 redisplay. */
8179 if (inhibit_menubar_update)
8180 return;
8181
8182 window = FRAME_SELECTED_WINDOW (f);
8183 w = XWINDOW (window);
8184
8185 #if 0 /* The if statement below this if statement used to include the
8186 condition !NILP (w->update_mode_line), rather than using
8187 update_mode_lines directly, and this if statement may have
8188 been added to make that condition work. Now the if
8189 statement below matches its comment, this isn't needed. */
8190 if (update_mode_lines)
8191 w->update_mode_line = Qt;
8192 #endif
8193
8194 if (FRAME_WINDOW_P (f)
8195 ?
8196 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8197 || defined (USE_GTK)
8198 FRAME_EXTERNAL_MENU_BAR (f)
8199 #else
8200 FRAME_MENU_BAR_LINES (f) > 0
8201 #endif
8202 : FRAME_MENU_BAR_LINES (f) > 0)
8203 {
8204 /* If the user has switched buffers or windows, we need to
8205 recompute to reflect the new bindings. But we'll
8206 recompute when update_mode_lines is set too; that means
8207 that people can use force-mode-line-update to request
8208 that the menu bar be recomputed. The adverse effect on
8209 the rest of the redisplay algorithm is about the same as
8210 windows_or_buffers_changed anyway. */
8211 if (windows_or_buffers_changed
8212 /* This used to test w->update_mode_line, but we believe
8213 there is no need to recompute the menu in that case. */
8214 || update_mode_lines
8215 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8216 < BUF_MODIFF (XBUFFER (w->buffer)))
8217 != !NILP (w->last_had_star))
8218 || ((!NILP (Vtransient_mark_mode)
8219 && !NILP (XBUFFER (w->buffer)->mark_active))
8220 != !NILP (w->region_showing)))
8221 {
8222 struct buffer *prev = current_buffer;
8223 int count = SPECPDL_INDEX ();
8224
8225 specbind (Qinhibit_menubar_update, Qt);
8226
8227 set_buffer_internal_1 (XBUFFER (w->buffer));
8228 if (save_match_data)
8229 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8230 if (NILP (Voverriding_local_map_menu_flag))
8231 {
8232 specbind (Qoverriding_terminal_local_map, Qnil);
8233 specbind (Qoverriding_local_map, Qnil);
8234 }
8235
8236 /* Run the Lucid hook. */
8237 safe_run_hooks (Qactivate_menubar_hook);
8238
8239 /* If it has changed current-menubar from previous value,
8240 really recompute the menu-bar from the value. */
8241 if (! NILP (Vlucid_menu_bar_dirty_flag))
8242 call0 (Qrecompute_lucid_menubar);
8243
8244 safe_run_hooks (Qmenu_bar_update_hook);
8245 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8246
8247 /* Redisplay the menu bar in case we changed it. */
8248 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8249 || defined (USE_GTK)
8250 if (FRAME_WINDOW_P (f)
8251 #if defined (MAC_OS)
8252 /* All frames on Mac OS share the same menubar. So only the
8253 selected frame should be allowed to set it. */
8254 && f == SELECTED_FRAME ()
8255 #endif
8256 )
8257 set_frame_menubar (f, 0, 0);
8258 else
8259 /* On a terminal screen, the menu bar is an ordinary screen
8260 line, and this makes it get updated. */
8261 w->update_mode_line = Qt;
8262 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8263 /* In the non-toolkit version, the menu bar is an ordinary screen
8264 line, and this makes it get updated. */
8265 w->update_mode_line = Qt;
8266 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8267
8268 unbind_to (count, Qnil);
8269 set_buffer_internal_1 (prev);
8270 }
8271 }
8272 }
8273
8274
8275 \f
8276 /***********************************************************************
8277 Output Cursor
8278 ***********************************************************************/
8279
8280 #ifdef HAVE_WINDOW_SYSTEM
8281
8282 /* EXPORT:
8283 Nominal cursor position -- where to draw output.
8284 HPOS and VPOS are window relative glyph matrix coordinates.
8285 X and Y are window relative pixel coordinates. */
8286
8287 struct cursor_pos output_cursor;
8288
8289
8290 /* EXPORT:
8291 Set the global variable output_cursor to CURSOR. All cursor
8292 positions are relative to updated_window. */
8293
8294 void
8295 set_output_cursor (cursor)
8296 struct cursor_pos *cursor;
8297 {
8298 output_cursor.hpos = cursor->hpos;
8299 output_cursor.vpos = cursor->vpos;
8300 output_cursor.x = cursor->x;
8301 output_cursor.y = cursor->y;
8302 }
8303
8304
8305 /* EXPORT for RIF:
8306 Set a nominal cursor position.
8307
8308 HPOS and VPOS are column/row positions in a window glyph matrix. X
8309 and Y are window text area relative pixel positions.
8310
8311 If this is done during an update, updated_window will contain the
8312 window that is being updated and the position is the future output
8313 cursor position for that window. If updated_window is null, use
8314 selected_window and display the cursor at the given position. */
8315
8316 void
8317 x_cursor_to (vpos, hpos, y, x)
8318 int vpos, hpos, y, x;
8319 {
8320 struct window *w;
8321
8322 /* If updated_window is not set, work on selected_window. */
8323 if (updated_window)
8324 w = updated_window;
8325 else
8326 w = XWINDOW (selected_window);
8327
8328 /* Set the output cursor. */
8329 output_cursor.hpos = hpos;
8330 output_cursor.vpos = vpos;
8331 output_cursor.x = x;
8332 output_cursor.y = y;
8333
8334 /* If not called as part of an update, really display the cursor.
8335 This will also set the cursor position of W. */
8336 if (updated_window == NULL)
8337 {
8338 BLOCK_INPUT;
8339 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8340 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
8341 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
8342 UNBLOCK_INPUT;
8343 }
8344 }
8345
8346 #endif /* HAVE_WINDOW_SYSTEM */
8347
8348 \f
8349 /***********************************************************************
8350 Tool-bars
8351 ***********************************************************************/
8352
8353 #ifdef HAVE_WINDOW_SYSTEM
8354
8355 /* Where the mouse was last time we reported a mouse event. */
8356
8357 FRAME_PTR last_mouse_frame;
8358
8359 /* Tool-bar item index of the item on which a mouse button was pressed
8360 or -1. */
8361
8362 int last_tool_bar_item;
8363
8364
8365 /* Update the tool-bar item list for frame F. This has to be done
8366 before we start to fill in any display lines. Called from
8367 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8368 and restore it here. */
8369
8370 static void
8371 update_tool_bar (f, save_match_data)
8372 struct frame *f;
8373 int save_match_data;
8374 {
8375 #ifdef USE_GTK
8376 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8377 #else
8378 int do_update = WINDOWP (f->tool_bar_window)
8379 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8380 #endif
8381
8382 if (do_update)
8383 {
8384 Lisp_Object window;
8385 struct window *w;
8386
8387 window = FRAME_SELECTED_WINDOW (f);
8388 w = XWINDOW (window);
8389
8390 /* If the user has switched buffers or windows, we need to
8391 recompute to reflect the new bindings. But we'll
8392 recompute when update_mode_lines is set too; that means
8393 that people can use force-mode-line-update to request
8394 that the menu bar be recomputed. The adverse effect on
8395 the rest of the redisplay algorithm is about the same as
8396 windows_or_buffers_changed anyway. */
8397 if (windows_or_buffers_changed
8398 || !NILP (w->update_mode_line)
8399 || update_mode_lines
8400 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8401 < BUF_MODIFF (XBUFFER (w->buffer)))
8402 != !NILP (w->last_had_star))
8403 || ((!NILP (Vtransient_mark_mode)
8404 && !NILP (XBUFFER (w->buffer)->mark_active))
8405 != !NILP (w->region_showing)))
8406 {
8407 struct buffer *prev = current_buffer;
8408 int count = SPECPDL_INDEX ();
8409 Lisp_Object old_tool_bar;
8410 struct gcpro gcpro1;
8411
8412 /* Set current_buffer to the buffer of the selected
8413 window of the frame, so that we get the right local
8414 keymaps. */
8415 set_buffer_internal_1 (XBUFFER (w->buffer));
8416
8417 /* Save match data, if we must. */
8418 if (save_match_data)
8419 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8420
8421 /* Make sure that we don't accidentally use bogus keymaps. */
8422 if (NILP (Voverriding_local_map_menu_flag))
8423 {
8424 specbind (Qoverriding_terminal_local_map, Qnil);
8425 specbind (Qoverriding_local_map, Qnil);
8426 }
8427
8428 old_tool_bar = f->tool_bar_items;
8429 GCPRO1 (old_tool_bar);
8430
8431 /* Build desired tool-bar items from keymaps. */
8432 BLOCK_INPUT;
8433 f->tool_bar_items
8434 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
8435 UNBLOCK_INPUT;
8436
8437 /* Redisplay the tool-bar if we changed it. */
8438 if (! NILP (Fequal (old_tool_bar, f->tool_bar_items)))
8439 w->update_mode_line = Qt;
8440
8441 UNGCPRO;
8442
8443 unbind_to (count, Qnil);
8444 set_buffer_internal_1 (prev);
8445 }
8446 }
8447 }
8448
8449
8450 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8451 F's desired tool-bar contents. F->tool_bar_items must have
8452 been set up previously by calling prepare_menu_bars. */
8453
8454 static void
8455 build_desired_tool_bar_string (f)
8456 struct frame *f;
8457 {
8458 int i, size, size_needed;
8459 struct gcpro gcpro1, gcpro2, gcpro3;
8460 Lisp_Object image, plist, props;
8461
8462 image = plist = props = Qnil;
8463 GCPRO3 (image, plist, props);
8464
8465 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8466 Otherwise, make a new string. */
8467
8468 /* The size of the string we might be able to reuse. */
8469 size = (STRINGP (f->desired_tool_bar_string)
8470 ? SCHARS (f->desired_tool_bar_string)
8471 : 0);
8472
8473 /* We need one space in the string for each image. */
8474 size_needed = f->n_tool_bar_items;
8475
8476 /* Reuse f->desired_tool_bar_string, if possible. */
8477 if (size < size_needed || NILP (f->desired_tool_bar_string))
8478 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8479 make_number (' '));
8480 else
8481 {
8482 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8483 Fremove_text_properties (make_number (0), make_number (size),
8484 props, f->desired_tool_bar_string);
8485 }
8486
8487 /* Put a `display' property on the string for the images to display,
8488 put a `menu_item' property on tool-bar items with a value that
8489 is the index of the item in F's tool-bar item vector. */
8490 for (i = 0; i < f->n_tool_bar_items; ++i)
8491 {
8492 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8493
8494 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8495 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8496 int hmargin, vmargin, relief, idx, end;
8497 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8498
8499 /* If image is a vector, choose the image according to the
8500 button state. */
8501 image = PROP (TOOL_BAR_ITEM_IMAGES);
8502 if (VECTORP (image))
8503 {
8504 if (enabled_p)
8505 idx = (selected_p
8506 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8507 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8508 else
8509 idx = (selected_p
8510 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8511 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8512
8513 xassert (ASIZE (image) >= idx);
8514 image = AREF (image, idx);
8515 }
8516 else
8517 idx = -1;
8518
8519 /* Ignore invalid image specifications. */
8520 if (!valid_image_p (image))
8521 continue;
8522
8523 /* Display the tool-bar button pressed, or depressed. */
8524 plist = Fcopy_sequence (XCDR (image));
8525
8526 /* Compute margin and relief to draw. */
8527 relief = (tool_bar_button_relief >= 0
8528 ? tool_bar_button_relief
8529 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8530 hmargin = vmargin = relief;
8531
8532 if (INTEGERP (Vtool_bar_button_margin)
8533 && XINT (Vtool_bar_button_margin) > 0)
8534 {
8535 hmargin += XFASTINT (Vtool_bar_button_margin);
8536 vmargin += XFASTINT (Vtool_bar_button_margin);
8537 }
8538 else if (CONSP (Vtool_bar_button_margin))
8539 {
8540 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8541 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8542 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8543
8544 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8545 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8546 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8547 }
8548
8549 if (auto_raise_tool_bar_buttons_p)
8550 {
8551 /* Add a `:relief' property to the image spec if the item is
8552 selected. */
8553 if (selected_p)
8554 {
8555 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8556 hmargin -= relief;
8557 vmargin -= relief;
8558 }
8559 }
8560 else
8561 {
8562 /* If image is selected, display it pressed, i.e. with a
8563 negative relief. If it's not selected, display it with a
8564 raised relief. */
8565 plist = Fplist_put (plist, QCrelief,
8566 (selected_p
8567 ? make_number (-relief)
8568 : make_number (relief)));
8569 hmargin -= relief;
8570 vmargin -= relief;
8571 }
8572
8573 /* Put a margin around the image. */
8574 if (hmargin || vmargin)
8575 {
8576 if (hmargin == vmargin)
8577 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8578 else
8579 plist = Fplist_put (plist, QCmargin,
8580 Fcons (make_number (hmargin),
8581 make_number (vmargin)));
8582 }
8583
8584 /* If button is not enabled, and we don't have special images
8585 for the disabled state, make the image appear disabled by
8586 applying an appropriate algorithm to it. */
8587 if (!enabled_p && idx < 0)
8588 plist = Fplist_put (plist, QCconversion, Qdisabled);
8589
8590 /* Put a `display' text property on the string for the image to
8591 display. Put a `menu-item' property on the string that gives
8592 the start of this item's properties in the tool-bar items
8593 vector. */
8594 image = Fcons (Qimage, plist);
8595 props = list4 (Qdisplay, image,
8596 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8597
8598 /* Let the last image hide all remaining spaces in the tool bar
8599 string. The string can be longer than needed when we reuse a
8600 previous string. */
8601 if (i + 1 == f->n_tool_bar_items)
8602 end = SCHARS (f->desired_tool_bar_string);
8603 else
8604 end = i + 1;
8605 Fadd_text_properties (make_number (i), make_number (end),
8606 props, f->desired_tool_bar_string);
8607 #undef PROP
8608 }
8609
8610 UNGCPRO;
8611 }
8612
8613
8614 /* Display one line of the tool-bar of frame IT->f. */
8615
8616 static void
8617 display_tool_bar_line (it)
8618 struct it *it;
8619 {
8620 struct glyph_row *row = it->glyph_row;
8621 int max_x = it->last_visible_x;
8622 struct glyph *last;
8623
8624 prepare_desired_row (row);
8625 row->y = it->current_y;
8626
8627 /* Note that this isn't made use of if the face hasn't a box,
8628 so there's no need to check the face here. */
8629 it->start_of_box_run_p = 1;
8630
8631 while (it->current_x < max_x)
8632 {
8633 int x_before, x, n_glyphs_before, i, nglyphs;
8634
8635 /* Get the next display element. */
8636 if (!get_next_display_element (it))
8637 break;
8638
8639 /* Produce glyphs. */
8640 x_before = it->current_x;
8641 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8642 PRODUCE_GLYPHS (it);
8643
8644 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8645 i = 0;
8646 x = x_before;
8647 while (i < nglyphs)
8648 {
8649 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
8650
8651 if (x + glyph->pixel_width > max_x)
8652 {
8653 /* Glyph doesn't fit on line. */
8654 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8655 it->current_x = x;
8656 goto out;
8657 }
8658
8659 ++it->hpos;
8660 x += glyph->pixel_width;
8661 ++i;
8662 }
8663
8664 /* Stop at line ends. */
8665 if (ITERATOR_AT_END_OF_LINE_P (it))
8666 break;
8667
8668 set_iterator_to_next (it, 1);
8669 }
8670
8671 out:;
8672
8673 row->displays_text_p = row->used[TEXT_AREA] != 0;
8674 extend_face_to_end_of_line (it);
8675 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8676 last->right_box_line_p = 1;
8677 if (last == row->glyphs[TEXT_AREA])
8678 last->left_box_line_p = 1;
8679 compute_line_metrics (it);
8680
8681 /* If line is empty, make it occupy the rest of the tool-bar. */
8682 if (!row->displays_text_p)
8683 {
8684 row->height = row->phys_height = it->last_visible_y - row->y;
8685 row->ascent = row->phys_ascent = 0;
8686 }
8687
8688 row->full_width_p = 1;
8689 row->continued_p = 0;
8690 row->truncated_on_left_p = 0;
8691 row->truncated_on_right_p = 0;
8692
8693 it->current_x = it->hpos = 0;
8694 it->current_y += row->height;
8695 ++it->vpos;
8696 ++it->glyph_row;
8697 }
8698
8699
8700 /* Value is the number of screen lines needed to make all tool-bar
8701 items of frame F visible. */
8702
8703 static int
8704 tool_bar_lines_needed (f)
8705 struct frame *f;
8706 {
8707 struct window *w = XWINDOW (f->tool_bar_window);
8708 struct it it;
8709
8710 /* Initialize an iterator for iteration over
8711 F->desired_tool_bar_string in the tool-bar window of frame F. */
8712 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8713 it.first_visible_x = 0;
8714 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8715 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8716
8717 while (!ITERATOR_AT_END_P (&it))
8718 {
8719 it.glyph_row = w->desired_matrix->rows;
8720 clear_glyph_row (it.glyph_row);
8721 display_tool_bar_line (&it);
8722 }
8723
8724 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
8725 }
8726
8727
8728 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8729 0, 1, 0,
8730 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8731 (frame)
8732 Lisp_Object frame;
8733 {
8734 struct frame *f;
8735 struct window *w;
8736 int nlines = 0;
8737
8738 if (NILP (frame))
8739 frame = selected_frame;
8740 else
8741 CHECK_FRAME (frame);
8742 f = XFRAME (frame);
8743
8744 if (WINDOWP (f->tool_bar_window)
8745 || (w = XWINDOW (f->tool_bar_window),
8746 WINDOW_TOTAL_LINES (w) > 0))
8747 {
8748 update_tool_bar (f, 1);
8749 if (f->n_tool_bar_items)
8750 {
8751 build_desired_tool_bar_string (f);
8752 nlines = tool_bar_lines_needed (f);
8753 }
8754 }
8755
8756 return make_number (nlines);
8757 }
8758
8759
8760 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
8761 height should be changed. */
8762
8763 static int
8764 redisplay_tool_bar (f)
8765 struct frame *f;
8766 {
8767 struct window *w;
8768 struct it it;
8769 struct glyph_row *row;
8770 int change_height_p = 0;
8771
8772 #ifdef USE_GTK
8773 if (FRAME_EXTERNAL_TOOL_BAR (f))
8774 update_frame_tool_bar (f);
8775 return 0;
8776 #endif
8777
8778 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8779 do anything. This means you must start with tool-bar-lines
8780 non-zero to get the auto-sizing effect. Or in other words, you
8781 can turn off tool-bars by specifying tool-bar-lines zero. */
8782 if (!WINDOWP (f->tool_bar_window)
8783 || (w = XWINDOW (f->tool_bar_window),
8784 WINDOW_TOTAL_LINES (w) == 0))
8785 return 0;
8786
8787 /* Set up an iterator for the tool-bar window. */
8788 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8789 it.first_visible_x = 0;
8790 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8791 row = it.glyph_row;
8792
8793 /* Build a string that represents the contents of the tool-bar. */
8794 build_desired_tool_bar_string (f);
8795 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8796
8797 /* Display as many lines as needed to display all tool-bar items. */
8798 while (it.current_y < it.last_visible_y)
8799 display_tool_bar_line (&it);
8800
8801 /* It doesn't make much sense to try scrolling in the tool-bar
8802 window, so don't do it. */
8803 w->desired_matrix->no_scrolling_p = 1;
8804 w->must_be_updated_p = 1;
8805
8806 if (auto_resize_tool_bars_p)
8807 {
8808 int nlines;
8809
8810 /* If we couldn't display everything, change the tool-bar's
8811 height. */
8812 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8813 change_height_p = 1;
8814
8815 /* If there are blank lines at the end, except for a partially
8816 visible blank line at the end that is smaller than
8817 FRAME_LINE_HEIGHT, change the tool-bar's height. */
8818 row = it.glyph_row - 1;
8819 if (!row->displays_text_p
8820 && row->height >= FRAME_LINE_HEIGHT (f))
8821 change_height_p = 1;
8822
8823 /* If row displays tool-bar items, but is partially visible,
8824 change the tool-bar's height. */
8825 if (row->displays_text_p
8826 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8827 change_height_p = 1;
8828
8829 /* Resize windows as needed by changing the `tool-bar-lines'
8830 frame parameter. */
8831 if (change_height_p
8832 && (nlines = tool_bar_lines_needed (f),
8833 nlines != WINDOW_TOTAL_LINES (w)))
8834 {
8835 extern Lisp_Object Qtool_bar_lines;
8836 Lisp_Object frame;
8837 int old_height = WINDOW_TOTAL_LINES (w);
8838
8839 XSETFRAME (frame, f);
8840 clear_glyph_matrix (w->desired_matrix);
8841 Fmodify_frame_parameters (frame,
8842 Fcons (Fcons (Qtool_bar_lines,
8843 make_number (nlines)),
8844 Qnil));
8845 if (WINDOW_TOTAL_LINES (w) != old_height)
8846 fonts_changed_p = 1;
8847 }
8848 }
8849
8850 return change_height_p;
8851 }
8852
8853
8854 /* Get information about the tool-bar item which is displayed in GLYPH
8855 on frame F. Return in *PROP_IDX the index where tool-bar item
8856 properties start in F->tool_bar_items. Value is zero if
8857 GLYPH doesn't display a tool-bar item. */
8858
8859 static int
8860 tool_bar_item_info (f, glyph, prop_idx)
8861 struct frame *f;
8862 struct glyph *glyph;
8863 int *prop_idx;
8864 {
8865 Lisp_Object prop;
8866 int success_p;
8867 int charpos;
8868
8869 /* This function can be called asynchronously, which means we must
8870 exclude any possibility that Fget_text_property signals an
8871 error. */
8872 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8873 charpos = max (0, charpos);
8874
8875 /* Get the text property `menu-item' at pos. The value of that
8876 property is the start index of this item's properties in
8877 F->tool_bar_items. */
8878 prop = Fget_text_property (make_number (charpos),
8879 Qmenu_item, f->current_tool_bar_string);
8880 if (INTEGERP (prop))
8881 {
8882 *prop_idx = XINT (prop);
8883 success_p = 1;
8884 }
8885 else
8886 success_p = 0;
8887
8888 return success_p;
8889 }
8890
8891 \f
8892 /* Get information about the tool-bar item at position X/Y on frame F.
8893 Return in *GLYPH a pointer to the glyph of the tool-bar item in
8894 the current matrix of the tool-bar window of F, or NULL if not
8895 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8896 item in F->tool_bar_items. Value is
8897
8898 -1 if X/Y is not on a tool-bar item
8899 0 if X/Y is on the same item that was highlighted before.
8900 1 otherwise. */
8901
8902 static int
8903 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
8904 struct frame *f;
8905 int x, y;
8906 struct glyph **glyph;
8907 int *hpos, *vpos, *prop_idx;
8908 {
8909 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8910 struct window *w = XWINDOW (f->tool_bar_window);
8911 int area;
8912
8913 /* Find the glyph under X/Y. */
8914 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
8915 if (*glyph == NULL)
8916 return -1;
8917
8918 /* Get the start of this tool-bar item's properties in
8919 f->tool_bar_items. */
8920 if (!tool_bar_item_info (f, *glyph, prop_idx))
8921 return -1;
8922
8923 /* Is mouse on the highlighted item? */
8924 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
8925 && *vpos >= dpyinfo->mouse_face_beg_row
8926 && *vpos <= dpyinfo->mouse_face_end_row
8927 && (*vpos > dpyinfo->mouse_face_beg_row
8928 || *hpos >= dpyinfo->mouse_face_beg_col)
8929 && (*vpos < dpyinfo->mouse_face_end_row
8930 || *hpos < dpyinfo->mouse_face_end_col
8931 || dpyinfo->mouse_face_past_end))
8932 return 0;
8933
8934 return 1;
8935 }
8936
8937
8938 /* EXPORT:
8939 Handle mouse button event on the tool-bar of frame F, at
8940 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
8941 0 for button release. MODIFIERS is event modifiers for button
8942 release. */
8943
8944 void
8945 handle_tool_bar_click (f, x, y, down_p, modifiers)
8946 struct frame *f;
8947 int x, y, down_p;
8948 unsigned int modifiers;
8949 {
8950 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8951 struct window *w = XWINDOW (f->tool_bar_window);
8952 int hpos, vpos, prop_idx;
8953 struct glyph *glyph;
8954 Lisp_Object enabled_p;
8955
8956 /* If not on the highlighted tool-bar item, return. */
8957 frame_to_window_pixel_xy (w, &x, &y);
8958 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
8959 return;
8960
8961 /* If item is disabled, do nothing. */
8962 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8963 if (NILP (enabled_p))
8964 return;
8965
8966 if (down_p)
8967 {
8968 /* Show item in pressed state. */
8969 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
8970 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
8971 last_tool_bar_item = prop_idx;
8972 }
8973 else
8974 {
8975 Lisp_Object key, frame;
8976 struct input_event event;
8977 EVENT_INIT (event);
8978
8979 /* Show item in released state. */
8980 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
8981 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
8982
8983 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
8984
8985 XSETFRAME (frame, f);
8986 event.kind = TOOL_BAR_EVENT;
8987 event.frame_or_window = frame;
8988 event.arg = frame;
8989 kbd_buffer_store_event (&event);
8990
8991 event.kind = TOOL_BAR_EVENT;
8992 event.frame_or_window = frame;
8993 event.arg = key;
8994 event.modifiers = modifiers;
8995 kbd_buffer_store_event (&event);
8996 last_tool_bar_item = -1;
8997 }
8998 }
8999
9000
9001 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9002 tool-bar window-relative coordinates X/Y. Called from
9003 note_mouse_highlight. */
9004
9005 static void
9006 note_tool_bar_highlight (f, x, y)
9007 struct frame *f;
9008 int x, y;
9009 {
9010 Lisp_Object window = f->tool_bar_window;
9011 struct window *w = XWINDOW (window);
9012 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9013 int hpos, vpos;
9014 struct glyph *glyph;
9015 struct glyph_row *row;
9016 int i;
9017 Lisp_Object enabled_p;
9018 int prop_idx;
9019 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9020 int mouse_down_p, rc;
9021
9022 /* Function note_mouse_highlight is called with negative x(y
9023 values when mouse moves outside of the frame. */
9024 if (x <= 0 || y <= 0)
9025 {
9026 clear_mouse_face (dpyinfo);
9027 return;
9028 }
9029
9030 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9031 if (rc < 0)
9032 {
9033 /* Not on tool-bar item. */
9034 clear_mouse_face (dpyinfo);
9035 return;
9036 }
9037 else if (rc == 0)
9038 /* On same tool-bar item as before. */
9039 goto set_help_echo;
9040
9041 clear_mouse_face (dpyinfo);
9042
9043 /* Mouse is down, but on different tool-bar item? */
9044 mouse_down_p = (dpyinfo->grabbed
9045 && f == last_mouse_frame
9046 && FRAME_LIVE_P (f));
9047 if (mouse_down_p
9048 && last_tool_bar_item != prop_idx)
9049 return;
9050
9051 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9052 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9053
9054 /* If tool-bar item is not enabled, don't highlight it. */
9055 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9056 if (!NILP (enabled_p))
9057 {
9058 /* Compute the x-position of the glyph. In front and past the
9059 image is a space. We include this in the highlighted area. */
9060 row = MATRIX_ROW (w->current_matrix, vpos);
9061 for (i = x = 0; i < hpos; ++i)
9062 x += row->glyphs[TEXT_AREA][i].pixel_width;
9063
9064 /* Record this as the current active region. */
9065 dpyinfo->mouse_face_beg_col = hpos;
9066 dpyinfo->mouse_face_beg_row = vpos;
9067 dpyinfo->mouse_face_beg_x = x;
9068 dpyinfo->mouse_face_beg_y = row->y;
9069 dpyinfo->mouse_face_past_end = 0;
9070
9071 dpyinfo->mouse_face_end_col = hpos + 1;
9072 dpyinfo->mouse_face_end_row = vpos;
9073 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9074 dpyinfo->mouse_face_end_y = row->y;
9075 dpyinfo->mouse_face_window = window;
9076 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9077
9078 /* Display it as active. */
9079 show_mouse_face (dpyinfo, draw);
9080 dpyinfo->mouse_face_image_state = draw;
9081 }
9082
9083 set_help_echo:
9084
9085 /* Set help_echo_string to a help string to display for this tool-bar item.
9086 XTread_socket does the rest. */
9087 help_echo_object = help_echo_window = Qnil;
9088 help_echo_pos = -1;
9089 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9090 if (NILP (help_echo_string))
9091 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9092 }
9093
9094 #endif /* HAVE_WINDOW_SYSTEM */
9095
9096
9097 \f
9098 /************************************************************************
9099 Horizontal scrolling
9100 ************************************************************************/
9101
9102 static int hscroll_window_tree P_ ((Lisp_Object));
9103 static int hscroll_windows P_ ((Lisp_Object));
9104
9105 /* For all leaf windows in the window tree rooted at WINDOW, set their
9106 hscroll value so that PT is (i) visible in the window, and (ii) so
9107 that it is not within a certain margin at the window's left and
9108 right border. Value is non-zero if any window's hscroll has been
9109 changed. */
9110
9111 static int
9112 hscroll_window_tree (window)
9113 Lisp_Object window;
9114 {
9115 int hscrolled_p = 0;
9116 int hscroll_relative_p = FLOATP (Vhscroll_step);
9117 int hscroll_step_abs = 0;
9118 double hscroll_step_rel = 0;
9119
9120 if (hscroll_relative_p)
9121 {
9122 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9123 if (hscroll_step_rel < 0)
9124 {
9125 hscroll_relative_p = 0;
9126 hscroll_step_abs = 0;
9127 }
9128 }
9129 else if (INTEGERP (Vhscroll_step))
9130 {
9131 hscroll_step_abs = XINT (Vhscroll_step);
9132 if (hscroll_step_abs < 0)
9133 hscroll_step_abs = 0;
9134 }
9135 else
9136 hscroll_step_abs = 0;
9137
9138 while (WINDOWP (window))
9139 {
9140 struct window *w = XWINDOW (window);
9141
9142 if (WINDOWP (w->hchild))
9143 hscrolled_p |= hscroll_window_tree (w->hchild);
9144 else if (WINDOWP (w->vchild))
9145 hscrolled_p |= hscroll_window_tree (w->vchild);
9146 else if (w->cursor.vpos >= 0)
9147 {
9148 int h_margin;
9149 int text_area_width;
9150 struct glyph_row *current_cursor_row
9151 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9152 struct glyph_row *desired_cursor_row
9153 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9154 struct glyph_row *cursor_row
9155 = (desired_cursor_row->enabled_p
9156 ? desired_cursor_row
9157 : current_cursor_row);
9158
9159 text_area_width = window_box_width (w, TEXT_AREA);
9160
9161 /* Scroll when cursor is inside this scroll margin. */
9162 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9163
9164 if ((XFASTINT (w->hscroll)
9165 && w->cursor.x <= h_margin)
9166 || (cursor_row->enabled_p
9167 && cursor_row->truncated_on_right_p
9168 && (w->cursor.x >= text_area_width - h_margin)))
9169 {
9170 struct it it;
9171 int hscroll;
9172 struct buffer *saved_current_buffer;
9173 int pt;
9174 int wanted_x;
9175
9176 /* Find point in a display of infinite width. */
9177 saved_current_buffer = current_buffer;
9178 current_buffer = XBUFFER (w->buffer);
9179
9180 if (w == XWINDOW (selected_window))
9181 pt = BUF_PT (current_buffer);
9182 else
9183 {
9184 pt = marker_position (w->pointm);
9185 pt = max (BEGV, pt);
9186 pt = min (ZV, pt);
9187 }
9188
9189 /* Move iterator to pt starting at cursor_row->start in
9190 a line with infinite width. */
9191 init_to_row_start (&it, w, cursor_row);
9192 it.last_visible_x = INFINITY;
9193 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9194 current_buffer = saved_current_buffer;
9195
9196 /* Position cursor in window. */
9197 if (!hscroll_relative_p && hscroll_step_abs == 0)
9198 hscroll = max (0, (it.current_x
9199 - (ITERATOR_AT_END_OF_LINE_P (&it)
9200 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9201 : (text_area_width / 2))))
9202 / FRAME_COLUMN_WIDTH (it.f);
9203 else if (w->cursor.x >= text_area_width - h_margin)
9204 {
9205 if (hscroll_relative_p)
9206 wanted_x = text_area_width * (1 - hscroll_step_rel)
9207 - h_margin;
9208 else
9209 wanted_x = text_area_width
9210 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9211 - h_margin;
9212 hscroll
9213 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9214 }
9215 else
9216 {
9217 if (hscroll_relative_p)
9218 wanted_x = text_area_width * hscroll_step_rel
9219 + h_margin;
9220 else
9221 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9222 + h_margin;
9223 hscroll
9224 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9225 }
9226 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9227
9228 /* Don't call Fset_window_hscroll if value hasn't
9229 changed because it will prevent redisplay
9230 optimizations. */
9231 if (XFASTINT (w->hscroll) != hscroll)
9232 {
9233 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9234 w->hscroll = make_number (hscroll);
9235 hscrolled_p = 1;
9236 }
9237 }
9238 }
9239
9240 window = w->next;
9241 }
9242
9243 /* Value is non-zero if hscroll of any leaf window has been changed. */
9244 return hscrolled_p;
9245 }
9246
9247
9248 /* Set hscroll so that cursor is visible and not inside horizontal
9249 scroll margins for all windows in the tree rooted at WINDOW. See
9250 also hscroll_window_tree above. Value is non-zero if any window's
9251 hscroll has been changed. If it has, desired matrices on the frame
9252 of WINDOW are cleared. */
9253
9254 static int
9255 hscroll_windows (window)
9256 Lisp_Object window;
9257 {
9258 int hscrolled_p;
9259
9260 if (automatic_hscrolling_p)
9261 {
9262 hscrolled_p = hscroll_window_tree (window);
9263 if (hscrolled_p)
9264 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9265 }
9266 else
9267 hscrolled_p = 0;
9268 return hscrolled_p;
9269 }
9270
9271
9272 \f
9273 /************************************************************************
9274 Redisplay
9275 ************************************************************************/
9276
9277 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9278 to a non-zero value. This is sometimes handy to have in a debugger
9279 session. */
9280
9281 #if GLYPH_DEBUG
9282
9283 /* First and last unchanged row for try_window_id. */
9284
9285 int debug_first_unchanged_at_end_vpos;
9286 int debug_last_unchanged_at_beg_vpos;
9287
9288 /* Delta vpos and y. */
9289
9290 int debug_dvpos, debug_dy;
9291
9292 /* Delta in characters and bytes for try_window_id. */
9293
9294 int debug_delta, debug_delta_bytes;
9295
9296 /* Values of window_end_pos and window_end_vpos at the end of
9297 try_window_id. */
9298
9299 EMACS_INT debug_end_pos, debug_end_vpos;
9300
9301 /* Append a string to W->desired_matrix->method. FMT is a printf
9302 format string. A1...A9 are a supplement for a variable-length
9303 argument list. If trace_redisplay_p is non-zero also printf the
9304 resulting string to stderr. */
9305
9306 static void
9307 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9308 struct window *w;
9309 char *fmt;
9310 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9311 {
9312 char buffer[512];
9313 char *method = w->desired_matrix->method;
9314 int len = strlen (method);
9315 int size = sizeof w->desired_matrix->method;
9316 int remaining = size - len - 1;
9317
9318 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9319 if (len && remaining)
9320 {
9321 method[len] = '|';
9322 --remaining, ++len;
9323 }
9324
9325 strncpy (method + len, buffer, remaining);
9326
9327 if (trace_redisplay_p)
9328 fprintf (stderr, "%p (%s): %s\n",
9329 w,
9330 ((BUFFERP (w->buffer)
9331 && STRINGP (XBUFFER (w->buffer)->name))
9332 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9333 : "no buffer"),
9334 buffer);
9335 }
9336
9337 #endif /* GLYPH_DEBUG */
9338
9339
9340 /* Value is non-zero if all changes in window W, which displays
9341 current_buffer, are in the text between START and END. START is a
9342 buffer position, END is given as a distance from Z. Used in
9343 redisplay_internal for display optimization. */
9344
9345 static INLINE int
9346 text_outside_line_unchanged_p (w, start, end)
9347 struct window *w;
9348 int start, end;
9349 {
9350 int unchanged_p = 1;
9351
9352 /* If text or overlays have changed, see where. */
9353 if (XFASTINT (w->last_modified) < MODIFF
9354 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9355 {
9356 /* Gap in the line? */
9357 if (GPT < start || Z - GPT < end)
9358 unchanged_p = 0;
9359
9360 /* Changes start in front of the line, or end after it? */
9361 if (unchanged_p
9362 && (BEG_UNCHANGED < start - 1
9363 || END_UNCHANGED < end))
9364 unchanged_p = 0;
9365
9366 /* If selective display, can't optimize if changes start at the
9367 beginning of the line. */
9368 if (unchanged_p
9369 && INTEGERP (current_buffer->selective_display)
9370 && XINT (current_buffer->selective_display) > 0
9371 && (BEG_UNCHANGED < start || GPT <= start))
9372 unchanged_p = 0;
9373
9374 /* If there are overlays at the start or end of the line, these
9375 may have overlay strings with newlines in them. A change at
9376 START, for instance, may actually concern the display of such
9377 overlay strings as well, and they are displayed on different
9378 lines. So, quickly rule out this case. (For the future, it
9379 might be desirable to implement something more telling than
9380 just BEG/END_UNCHANGED.) */
9381 if (unchanged_p)
9382 {
9383 if (BEG + BEG_UNCHANGED == start
9384 && overlay_touches_p (start))
9385 unchanged_p = 0;
9386 if (END_UNCHANGED == end
9387 && overlay_touches_p (Z - end))
9388 unchanged_p = 0;
9389 }
9390 }
9391
9392 return unchanged_p;
9393 }
9394
9395
9396 /* Do a frame update, taking possible shortcuts into account. This is
9397 the main external entry point for redisplay.
9398
9399 If the last redisplay displayed an echo area message and that message
9400 is no longer requested, we clear the echo area or bring back the
9401 mini-buffer if that is in use. */
9402
9403 void
9404 redisplay ()
9405 {
9406 redisplay_internal (0);
9407 }
9408
9409
9410 static Lisp_Object
9411 overlay_arrow_string_or_property (var, pbitmap)
9412 Lisp_Object var;
9413 int *pbitmap;
9414 {
9415 Lisp_Object pstr = Fget (var, Qoverlay_arrow_string);
9416 Lisp_Object bitmap;
9417
9418 if (pbitmap)
9419 {
9420 *pbitmap = 0;
9421 if (bitmap = Fget (var, Qoverlay_arrow_bitmap), INTEGERP (bitmap))
9422 *pbitmap = XINT (bitmap);
9423 }
9424
9425 if (!NILP (pstr))
9426 return pstr;
9427 return Voverlay_arrow_string;
9428 }
9429
9430 /* Return 1 if there are any overlay-arrows in current_buffer. */
9431 static int
9432 overlay_arrow_in_current_buffer_p ()
9433 {
9434 Lisp_Object vlist;
9435
9436 for (vlist = Voverlay_arrow_variable_list;
9437 CONSP (vlist);
9438 vlist = XCDR (vlist))
9439 {
9440 Lisp_Object var = XCAR (vlist);
9441 Lisp_Object val;
9442
9443 if (!SYMBOLP (var))
9444 continue;
9445 val = find_symbol_value (var);
9446 if (MARKERP (val)
9447 && current_buffer == XMARKER (val)->buffer)
9448 return 1;
9449 }
9450 return 0;
9451 }
9452
9453
9454 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9455 has changed. */
9456
9457 static int
9458 overlay_arrows_changed_p ()
9459 {
9460 Lisp_Object vlist;
9461
9462 for (vlist = Voverlay_arrow_variable_list;
9463 CONSP (vlist);
9464 vlist = XCDR (vlist))
9465 {
9466 Lisp_Object var = XCAR (vlist);
9467 Lisp_Object val, pstr;
9468
9469 if (!SYMBOLP (var))
9470 continue;
9471 val = find_symbol_value (var);
9472 if (!MARKERP (val))
9473 continue;
9474 if (! EQ (COERCE_MARKER (val),
9475 Fget (var, Qlast_arrow_position))
9476 || ! (pstr = overlay_arrow_string_or_property (var, 0),
9477 EQ (pstr, Fget (var, Qlast_arrow_string))))
9478 return 1;
9479 }
9480 return 0;
9481 }
9482
9483 /* Mark overlay arrows to be updated on next redisplay. */
9484
9485 static void
9486 update_overlay_arrows (up_to_date)
9487 int up_to_date;
9488 {
9489 Lisp_Object vlist;
9490
9491 for (vlist = Voverlay_arrow_variable_list;
9492 CONSP (vlist);
9493 vlist = XCDR (vlist))
9494 {
9495 Lisp_Object var = XCAR (vlist);
9496
9497 if (!SYMBOLP (var))
9498 continue;
9499
9500 if (up_to_date > 0)
9501 {
9502 Lisp_Object val = find_symbol_value (var);
9503 Fput (var, Qlast_arrow_position,
9504 COERCE_MARKER (val));
9505 Fput (var, Qlast_arrow_string,
9506 overlay_arrow_string_or_property (var, 0));
9507 }
9508 else if (up_to_date < 0
9509 || !NILP (Fget (var, Qlast_arrow_position)))
9510 {
9511 Fput (var, Qlast_arrow_position, Qt);
9512 Fput (var, Qlast_arrow_string, Qt);
9513 }
9514 }
9515 }
9516
9517
9518 /* Return overlay arrow string at row, or nil. */
9519
9520 static Lisp_Object
9521 overlay_arrow_at_row (f, row, pbitmap)
9522 struct frame *f;
9523 struct glyph_row *row;
9524 int *pbitmap;
9525 {
9526 Lisp_Object vlist;
9527
9528 for (vlist = Voverlay_arrow_variable_list;
9529 CONSP (vlist);
9530 vlist = XCDR (vlist))
9531 {
9532 Lisp_Object var = XCAR (vlist);
9533 Lisp_Object val;
9534
9535 if (!SYMBOLP (var))
9536 continue;
9537
9538 val = find_symbol_value (var);
9539
9540 if (MARKERP (val)
9541 && current_buffer == XMARKER (val)->buffer
9542 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
9543 {
9544 val = overlay_arrow_string_or_property (var, pbitmap);
9545 if (FRAME_WINDOW_P (f))
9546 return Qt;
9547 else if (STRINGP (val))
9548 return val;
9549 break;
9550 }
9551 }
9552
9553 *pbitmap = 0;
9554 return Qnil;
9555 }
9556
9557 /* Return 1 if point moved out of or into a composition. Otherwise
9558 return 0. PREV_BUF and PREV_PT are the last point buffer and
9559 position. BUF and PT are the current point buffer and position. */
9560
9561 int
9562 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9563 struct buffer *prev_buf, *buf;
9564 int prev_pt, pt;
9565 {
9566 int start, end;
9567 Lisp_Object prop;
9568 Lisp_Object buffer;
9569
9570 XSETBUFFER (buffer, buf);
9571 /* Check a composition at the last point if point moved within the
9572 same buffer. */
9573 if (prev_buf == buf)
9574 {
9575 if (prev_pt == pt)
9576 /* Point didn't move. */
9577 return 0;
9578
9579 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9580 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9581 && COMPOSITION_VALID_P (start, end, prop)
9582 && start < prev_pt && end > prev_pt)
9583 /* The last point was within the composition. Return 1 iff
9584 point moved out of the composition. */
9585 return (pt <= start || pt >= end);
9586 }
9587
9588 /* Check a composition at the current point. */
9589 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9590 && find_composition (pt, -1, &start, &end, &prop, buffer)
9591 && COMPOSITION_VALID_P (start, end, prop)
9592 && start < pt && end > pt);
9593 }
9594
9595
9596 /* Reconsider the setting of B->clip_changed which is displayed
9597 in window W. */
9598
9599 static INLINE void
9600 reconsider_clip_changes (w, b)
9601 struct window *w;
9602 struct buffer *b;
9603 {
9604 if (b->clip_changed
9605 && !NILP (w->window_end_valid)
9606 && w->current_matrix->buffer == b
9607 && w->current_matrix->zv == BUF_ZV (b)
9608 && w->current_matrix->begv == BUF_BEGV (b))
9609 b->clip_changed = 0;
9610
9611 /* If display wasn't paused, and W is not a tool bar window, see if
9612 point has been moved into or out of a composition. In that case,
9613 we set b->clip_changed to 1 to force updating the screen. If
9614 b->clip_changed has already been set to 1, we can skip this
9615 check. */
9616 if (!b->clip_changed
9617 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9618 {
9619 int pt;
9620
9621 if (w == XWINDOW (selected_window))
9622 pt = BUF_PT (current_buffer);
9623 else
9624 pt = marker_position (w->pointm);
9625
9626 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
9627 || pt != XINT (w->last_point))
9628 && check_point_in_composition (w->current_matrix->buffer,
9629 XINT (w->last_point),
9630 XBUFFER (w->buffer), pt))
9631 b->clip_changed = 1;
9632 }
9633 }
9634 \f
9635
9636 /* Select FRAME to forward the values of frame-local variables into C
9637 variables so that the redisplay routines can access those values
9638 directly. */
9639
9640 static void
9641 select_frame_for_redisplay (frame)
9642 Lisp_Object frame;
9643 {
9644 Lisp_Object tail, sym, val;
9645 Lisp_Object old = selected_frame;
9646
9647 selected_frame = frame;
9648
9649 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
9650 if (CONSP (XCAR (tail))
9651 && (sym = XCAR (XCAR (tail)),
9652 SYMBOLP (sym))
9653 && (sym = indirect_variable (sym),
9654 val = SYMBOL_VALUE (sym),
9655 (BUFFER_LOCAL_VALUEP (val)
9656 || SOME_BUFFER_LOCAL_VALUEP (val)))
9657 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9658 Fsymbol_value (sym);
9659
9660 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
9661 if (CONSP (XCAR (tail))
9662 && (sym = XCAR (XCAR (tail)),
9663 SYMBOLP (sym))
9664 && (sym = indirect_variable (sym),
9665 val = SYMBOL_VALUE (sym),
9666 (BUFFER_LOCAL_VALUEP (val)
9667 || SOME_BUFFER_LOCAL_VALUEP (val)))
9668 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9669 Fsymbol_value (sym);
9670 }
9671
9672
9673 #define STOP_POLLING \
9674 do { if (! polling_stopped_here) stop_polling (); \
9675 polling_stopped_here = 1; } while (0)
9676
9677 #define RESUME_POLLING \
9678 do { if (polling_stopped_here) start_polling (); \
9679 polling_stopped_here = 0; } while (0)
9680
9681
9682 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9683 response to any user action; therefore, we should preserve the echo
9684 area. (Actually, our caller does that job.) Perhaps in the future
9685 avoid recentering windows if it is not necessary; currently that
9686 causes some problems. */
9687
9688 static void
9689 redisplay_internal (preserve_echo_area)
9690 int preserve_echo_area;
9691 {
9692 struct window *w = XWINDOW (selected_window);
9693 struct frame *f = XFRAME (w->frame);
9694 int pause;
9695 int must_finish = 0;
9696 struct text_pos tlbufpos, tlendpos;
9697 int number_of_visible_frames;
9698 int count;
9699 struct frame *sf = SELECTED_FRAME ();
9700 int polling_stopped_here = 0;
9701
9702 /* Non-zero means redisplay has to consider all windows on all
9703 frames. Zero means, only selected_window is considered. */
9704 int consider_all_windows_p;
9705
9706 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9707
9708 /* No redisplay if running in batch mode or frame is not yet fully
9709 initialized, or redisplay is explicitly turned off by setting
9710 Vinhibit_redisplay. */
9711 if (noninteractive
9712 || !NILP (Vinhibit_redisplay)
9713 || !f->glyphs_initialized_p)
9714 return;
9715
9716 /* The flag redisplay_performed_directly_p is set by
9717 direct_output_for_insert when it already did the whole screen
9718 update necessary. */
9719 if (redisplay_performed_directly_p)
9720 {
9721 redisplay_performed_directly_p = 0;
9722 if (!hscroll_windows (selected_window))
9723 return;
9724 }
9725
9726 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9727 if (popup_activated ())
9728 return;
9729 #endif
9730
9731 /* I don't think this happens but let's be paranoid. */
9732 if (redisplaying_p)
9733 return;
9734
9735 /* Record a function that resets redisplaying_p to its old value
9736 when we leave this function. */
9737 count = SPECPDL_INDEX ();
9738 record_unwind_protect (unwind_redisplay,
9739 Fcons (make_number (redisplaying_p), selected_frame));
9740 ++redisplaying_p;
9741 specbind (Qinhibit_free_realized_faces, Qnil);
9742
9743 retry:
9744 pause = 0;
9745 reconsider_clip_changes (w, current_buffer);
9746
9747 /* If new fonts have been loaded that make a glyph matrix adjustment
9748 necessary, do it. */
9749 if (fonts_changed_p)
9750 {
9751 adjust_glyphs (NULL);
9752 ++windows_or_buffers_changed;
9753 fonts_changed_p = 0;
9754 }
9755
9756 /* If face_change_count is non-zero, init_iterator will free all
9757 realized faces, which includes the faces referenced from current
9758 matrices. So, we can't reuse current matrices in this case. */
9759 if (face_change_count)
9760 ++windows_or_buffers_changed;
9761
9762 if (FRAME_TERMCAP_P (sf)
9763 && FRAME_TTY (sf)->previous_terminal_frame != sf)
9764 {
9765 /* Since frames on a single ASCII terminal share the same
9766 display area, displaying a different frame means redisplay
9767 the whole thing. */
9768 windows_or_buffers_changed++;
9769 SET_FRAME_GARBAGED (sf);
9770 FRAME_TTY (sf)->previous_terminal_frame = sf;
9771 }
9772
9773 /* Set the visible flags for all frames. Do this before checking
9774 for resized or garbaged frames; they want to know if their frames
9775 are visible. See the comment in frame.h for
9776 FRAME_SAMPLE_VISIBILITY. */
9777 {
9778 Lisp_Object tail, frame;
9779
9780 number_of_visible_frames = 0;
9781
9782 FOR_EACH_FRAME (tail, frame)
9783 {
9784 struct frame *f = XFRAME (frame);
9785
9786 FRAME_SAMPLE_VISIBILITY (f);
9787 if (FRAME_VISIBLE_P (f))
9788 ++number_of_visible_frames;
9789 clear_desired_matrices (f);
9790 }
9791 }
9792
9793
9794 /* Notice any pending interrupt request to change frame size. */
9795 do_pending_window_change (1);
9796
9797 /* Clear frames marked as garbaged. */
9798 if (frame_garbaged)
9799 clear_garbaged_frames ();
9800
9801 /* Build menubar and tool-bar items. */
9802 prepare_menu_bars ();
9803
9804 if (windows_or_buffers_changed)
9805 update_mode_lines++;
9806
9807 /* Detect case that we need to write or remove a star in the mode line. */
9808 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
9809 {
9810 w->update_mode_line = Qt;
9811 if (buffer_shared > 1)
9812 update_mode_lines++;
9813 }
9814
9815 /* If %c is in the mode line, update it if needed. */
9816 if (!NILP (w->column_number_displayed)
9817 /* This alternative quickly identifies a common case
9818 where no change is needed. */
9819 && !(PT == XFASTINT (w->last_point)
9820 && XFASTINT (w->last_modified) >= MODIFF
9821 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9822 && (XFASTINT (w->column_number_displayed)
9823 != (int) current_column ())) /* iftc */
9824 w->update_mode_line = Qt;
9825
9826 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
9827
9828 /* The variable buffer_shared is set in redisplay_window and
9829 indicates that we redisplay a buffer in different windows. See
9830 there. */
9831 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
9832 || cursor_type_changed);
9833
9834 /* If specs for an arrow have changed, do thorough redisplay
9835 to ensure we remove any arrow that should no longer exist. */
9836 if (overlay_arrows_changed_p ())
9837 consider_all_windows_p = windows_or_buffers_changed = 1;
9838
9839 /* Normally the message* functions will have already displayed and
9840 updated the echo area, but the frame may have been trashed, or
9841 the update may have been preempted, so display the echo area
9842 again here. Checking message_cleared_p captures the case that
9843 the echo area should be cleared. */
9844 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
9845 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
9846 || (message_cleared_p
9847 && minibuf_level == 0
9848 /* If the mini-window is currently selected, this means the
9849 echo-area doesn't show through. */
9850 && !MINI_WINDOW_P (XWINDOW (selected_window))))
9851 {
9852 int window_height_changed_p = echo_area_display (0);
9853 must_finish = 1;
9854
9855 /* If we don't display the current message, don't clear the
9856 message_cleared_p flag, because, if we did, we wouldn't clear
9857 the echo area in the next redisplay which doesn't preserve
9858 the echo area. */
9859 if (!display_last_displayed_message_p)
9860 message_cleared_p = 0;
9861
9862 if (fonts_changed_p)
9863 goto retry;
9864 else if (window_height_changed_p)
9865 {
9866 consider_all_windows_p = 1;
9867 ++update_mode_lines;
9868 ++windows_or_buffers_changed;
9869
9870 /* If window configuration was changed, frames may have been
9871 marked garbaged. Clear them or we will experience
9872 surprises wrt scrolling. */
9873 if (frame_garbaged)
9874 clear_garbaged_frames ();
9875 }
9876 }
9877 else if (EQ (selected_window, minibuf_window)
9878 && (current_buffer->clip_changed
9879 || XFASTINT (w->last_modified) < MODIFF
9880 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9881 && resize_mini_window (w, 0))
9882 {
9883 /* Resized active mini-window to fit the size of what it is
9884 showing if its contents might have changed. */
9885 must_finish = 1;
9886 consider_all_windows_p = 1;
9887 ++windows_or_buffers_changed;
9888 ++update_mode_lines;
9889
9890 /* If window configuration was changed, frames may have been
9891 marked garbaged. Clear them or we will experience
9892 surprises wrt scrolling. */
9893 if (frame_garbaged)
9894 clear_garbaged_frames ();
9895 }
9896
9897
9898 /* If showing the region, and mark has changed, we must redisplay
9899 the whole window. The assignment to this_line_start_pos prevents
9900 the optimization directly below this if-statement. */
9901 if (((!NILP (Vtransient_mark_mode)
9902 && !NILP (XBUFFER (w->buffer)->mark_active))
9903 != !NILP (w->region_showing))
9904 || (!NILP (w->region_showing)
9905 && !EQ (w->region_showing,
9906 Fmarker_position (XBUFFER (w->buffer)->mark))))
9907 CHARPOS (this_line_start_pos) = 0;
9908
9909 /* Optimize the case that only the line containing the cursor in the
9910 selected window has changed. Variables starting with this_ are
9911 set in display_line and record information about the line
9912 containing the cursor. */
9913 tlbufpos = this_line_start_pos;
9914 tlendpos = this_line_end_pos;
9915 if (!consider_all_windows_p
9916 && CHARPOS (tlbufpos) > 0
9917 && NILP (w->update_mode_line)
9918 && !current_buffer->clip_changed
9919 && !current_buffer->prevent_redisplay_optimizations_p
9920 && FRAME_VISIBLE_P (XFRAME (w->frame))
9921 && !FRAME_OBSCURED_P (XFRAME (w->frame))
9922 /* Make sure recorded data applies to current buffer, etc. */
9923 && this_line_buffer == current_buffer
9924 && current_buffer == XBUFFER (w->buffer)
9925 && NILP (w->force_start)
9926 && NILP (w->optional_new_start)
9927 /* Point must be on the line that we have info recorded about. */
9928 && PT >= CHARPOS (tlbufpos)
9929 && PT <= Z - CHARPOS (tlendpos)
9930 /* All text outside that line, including its final newline,
9931 must be unchanged */
9932 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
9933 CHARPOS (tlendpos)))
9934 {
9935 if (CHARPOS (tlbufpos) > BEGV
9936 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
9937 && (CHARPOS (tlbufpos) == ZV
9938 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
9939 /* Former continuation line has disappeared by becoming empty */
9940 goto cancel;
9941 else if (XFASTINT (w->last_modified) < MODIFF
9942 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
9943 || MINI_WINDOW_P (w))
9944 {
9945 /* We have to handle the case of continuation around a
9946 wide-column character (See the comment in indent.c around
9947 line 885).
9948
9949 For instance, in the following case:
9950
9951 -------- Insert --------
9952 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
9953 J_I_ ==> J_I_ `^^' are cursors.
9954 ^^ ^^
9955 -------- --------
9956
9957 As we have to redraw the line above, we should goto cancel. */
9958
9959 struct it it;
9960 int line_height_before = this_line_pixel_height;
9961
9962 /* Note that start_display will handle the case that the
9963 line starting at tlbufpos is a continuation lines. */
9964 start_display (&it, w, tlbufpos);
9965
9966 /* Implementation note: It this still necessary? */
9967 if (it.current_x != this_line_start_x)
9968 goto cancel;
9969
9970 TRACE ((stderr, "trying display optimization 1\n"));
9971 w->cursor.vpos = -1;
9972 overlay_arrow_seen = 0;
9973 it.vpos = this_line_vpos;
9974 it.current_y = this_line_y;
9975 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
9976 display_line (&it);
9977
9978 /* If line contains point, is not continued,
9979 and ends at same distance from eob as before, we win */
9980 if (w->cursor.vpos >= 0
9981 /* Line is not continued, otherwise this_line_start_pos
9982 would have been set to 0 in display_line. */
9983 && CHARPOS (this_line_start_pos)
9984 /* Line ends as before. */
9985 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
9986 /* Line has same height as before. Otherwise other lines
9987 would have to be shifted up or down. */
9988 && this_line_pixel_height == line_height_before)
9989 {
9990 /* If this is not the window's last line, we must adjust
9991 the charstarts of the lines below. */
9992 if (it.current_y < it.last_visible_y)
9993 {
9994 struct glyph_row *row
9995 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
9996 int delta, delta_bytes;
9997
9998 if (Z - CHARPOS (tlendpos) == ZV)
9999 {
10000 /* This line ends at end of (accessible part of)
10001 buffer. There is no newline to count. */
10002 delta = (Z
10003 - CHARPOS (tlendpos)
10004 - MATRIX_ROW_START_CHARPOS (row));
10005 delta_bytes = (Z_BYTE
10006 - BYTEPOS (tlendpos)
10007 - MATRIX_ROW_START_BYTEPOS (row));
10008 }
10009 else
10010 {
10011 /* This line ends in a newline. Must take
10012 account of the newline and the rest of the
10013 text that follows. */
10014 delta = (Z
10015 - CHARPOS (tlendpos)
10016 - MATRIX_ROW_START_CHARPOS (row));
10017 delta_bytes = (Z_BYTE
10018 - BYTEPOS (tlendpos)
10019 - MATRIX_ROW_START_BYTEPOS (row));
10020 }
10021
10022 increment_matrix_positions (w->current_matrix,
10023 this_line_vpos + 1,
10024 w->current_matrix->nrows,
10025 delta, delta_bytes);
10026 }
10027
10028 /* If this row displays text now but previously didn't,
10029 or vice versa, w->window_end_vpos may have to be
10030 adjusted. */
10031 if ((it.glyph_row - 1)->displays_text_p)
10032 {
10033 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10034 XSETINT (w->window_end_vpos, this_line_vpos);
10035 }
10036 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10037 && this_line_vpos > 0)
10038 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10039 w->window_end_valid = Qnil;
10040
10041 /* Update hint: No need to try to scroll in update_window. */
10042 w->desired_matrix->no_scrolling_p = 1;
10043
10044 #if GLYPH_DEBUG
10045 *w->desired_matrix->method = 0;
10046 debug_method_add (w, "optimization 1");
10047 #endif
10048 #ifdef HAVE_WINDOW_SYSTEM
10049 update_window_fringes (w, 0);
10050 #endif
10051 goto update;
10052 }
10053 else
10054 goto cancel;
10055 }
10056 else if (/* Cursor position hasn't changed. */
10057 PT == XFASTINT (w->last_point)
10058 /* Make sure the cursor was last displayed
10059 in this window. Otherwise we have to reposition it. */
10060 && 0 <= w->cursor.vpos
10061 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10062 {
10063 if (!must_finish)
10064 {
10065 do_pending_window_change (1);
10066
10067 /* We used to always goto end_of_redisplay here, but this
10068 isn't enough if we have a blinking cursor. */
10069 if (w->cursor_off_p == w->last_cursor_off_p)
10070 goto end_of_redisplay;
10071 }
10072 goto update;
10073 }
10074 /* If highlighting the region, or if the cursor is in the echo area,
10075 then we can't just move the cursor. */
10076 else if (! (!NILP (Vtransient_mark_mode)
10077 && !NILP (current_buffer->mark_active))
10078 && (EQ (selected_window, current_buffer->last_selected_window)
10079 || highlight_nonselected_windows)
10080 && NILP (w->region_showing)
10081 && NILP (Vshow_trailing_whitespace)
10082 && !cursor_in_echo_area)
10083 {
10084 struct it it;
10085 struct glyph_row *row;
10086
10087 /* Skip from tlbufpos to PT and see where it is. Note that
10088 PT may be in invisible text. If so, we will end at the
10089 next visible position. */
10090 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10091 NULL, DEFAULT_FACE_ID);
10092 it.current_x = this_line_start_x;
10093 it.current_y = this_line_y;
10094 it.vpos = this_line_vpos;
10095
10096 /* The call to move_it_to stops in front of PT, but
10097 moves over before-strings. */
10098 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10099
10100 if (it.vpos == this_line_vpos
10101 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10102 row->enabled_p))
10103 {
10104 xassert (this_line_vpos == it.vpos);
10105 xassert (this_line_y == it.current_y);
10106 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10107 #if GLYPH_DEBUG
10108 *w->desired_matrix->method = 0;
10109 debug_method_add (w, "optimization 3");
10110 #endif
10111 goto update;
10112 }
10113 else
10114 goto cancel;
10115 }
10116
10117 cancel:
10118 /* Text changed drastically or point moved off of line. */
10119 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10120 }
10121
10122 CHARPOS (this_line_start_pos) = 0;
10123 consider_all_windows_p |= buffer_shared > 1;
10124 ++clear_face_cache_count;
10125
10126
10127 /* Build desired matrices, and update the display. If
10128 consider_all_windows_p is non-zero, do it for all windows on all
10129 frames. Otherwise do it for selected_window, only. */
10130
10131 if (consider_all_windows_p)
10132 {
10133 Lisp_Object tail, frame;
10134 int i, n = 0, size = 50;
10135 struct frame **updated
10136 = (struct frame **) alloca (size * sizeof *updated);
10137
10138 /* Clear the face cache eventually. */
10139 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10140 {
10141 clear_face_cache (0);
10142 clear_face_cache_count = 0;
10143 }
10144
10145 /* Recompute # windows showing selected buffer. This will be
10146 incremented each time such a window is displayed. */
10147 buffer_shared = 0;
10148
10149 FOR_EACH_FRAME (tail, frame)
10150 {
10151 struct frame *f = XFRAME (frame);
10152
10153 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
10154 {
10155 if (! EQ (frame, selected_frame))
10156 /* Select the frame, for the sake of frame-local
10157 variables. */
10158 select_frame_for_redisplay (frame);
10159
10160 #ifdef HAVE_WINDOW_SYSTEM
10161 if (clear_face_cache_count % 50 == 0
10162 && FRAME_WINDOW_P (f))
10163 clear_image_cache (f, 0);
10164 #endif /* HAVE_WINDOW_SYSTEM */
10165
10166 /* Mark all the scroll bars to be removed; we'll redeem
10167 the ones we want when we redisplay their windows. */
10168 if (FRAME_DISPLAY (f)->condemn_scroll_bars_hook)
10169 FRAME_DISPLAY (f)->condemn_scroll_bars_hook (f);
10170
10171 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10172 redisplay_windows (FRAME_ROOT_WINDOW (f));
10173
10174 /* Any scroll bars which redisplay_windows should have
10175 nuked should now go away. */
10176 if (FRAME_DISPLAY (f)->judge_scroll_bars_hook)
10177 FRAME_DISPLAY (f)->judge_scroll_bars_hook (f);
10178
10179 /* If fonts changed, display again. */
10180 /* ??? rms: I suspect it is a mistake to jump all the way
10181 back to retry here. It should just retry this frame. */
10182 if (fonts_changed_p)
10183 goto retry;
10184
10185 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10186 {
10187 /* See if we have to hscroll. */
10188 if (hscroll_windows (f->root_window))
10189 goto retry;
10190
10191 /* Prevent various kinds of signals during display
10192 update. stdio is not robust about handling
10193 signals, which can cause an apparent I/O
10194 error. */
10195 if (interrupt_input)
10196 unrequest_sigio ();
10197 STOP_POLLING;
10198
10199 /* Update the display. */
10200 set_window_update_flags (XWINDOW (f->root_window), 1);
10201 pause |= update_frame (f, 0, 0);
10202 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10203 if (pause)
10204 break;
10205 #endif
10206
10207 if (n == size)
10208 {
10209 int nbytes = size * sizeof *updated;
10210 struct frame **p = (struct frame **) alloca (2 * nbytes);
10211 bcopy (updated, p, nbytes);
10212 size *= 2;
10213 }
10214
10215 updated[n++] = f;
10216 }
10217 }
10218 }
10219
10220 if (!pause)
10221 {
10222 /* Do the mark_window_display_accurate after all windows have
10223 been redisplayed because this call resets flags in buffers
10224 which are needed for proper redisplay. */
10225 for (i = 0; i < n; ++i)
10226 {
10227 struct frame *f = updated[i];
10228 mark_window_display_accurate (f->root_window, 1);
10229 if (FRAME_DISPLAY (f)->frame_up_to_date_hook)
10230 FRAME_DISPLAY (f)->frame_up_to_date_hook (f);
10231 }
10232 }
10233 }
10234 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10235 {
10236 Lisp_Object mini_window;
10237 struct frame *mini_frame;
10238
10239 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10240 /* Use list_of_error, not Qerror, so that
10241 we catch only errors and don't run the debugger. */
10242 internal_condition_case_1 (redisplay_window_1, selected_window,
10243 list_of_error,
10244 redisplay_window_error);
10245
10246 /* Compare desired and current matrices, perform output. */
10247
10248 update:
10249 /* If fonts changed, display again. */
10250 if (fonts_changed_p)
10251 goto retry;
10252
10253 /* Prevent various kinds of signals during display update.
10254 stdio is not robust about handling signals,
10255 which can cause an apparent I/O error. */
10256 if (interrupt_input)
10257 unrequest_sigio ();
10258 STOP_POLLING;
10259
10260 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10261 {
10262 if (hscroll_windows (selected_window))
10263 goto retry;
10264
10265 XWINDOW (selected_window)->must_be_updated_p = 1;
10266 pause = update_frame (sf, 0, 0);
10267 }
10268
10269 /* We may have called echo_area_display at the top of this
10270 function. If the echo area is on another frame, that may
10271 have put text on a frame other than the selected one, so the
10272 above call to update_frame would not have caught it. Catch
10273 it here. */
10274 mini_window = FRAME_MINIBUF_WINDOW (sf);
10275 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10276
10277 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10278 {
10279 XWINDOW (mini_window)->must_be_updated_p = 1;
10280 pause |= update_frame (mini_frame, 0, 0);
10281 if (!pause && hscroll_windows (mini_window))
10282 goto retry;
10283 }
10284 }
10285
10286 /* If display was paused because of pending input, make sure we do a
10287 thorough update the next time. */
10288 if (pause)
10289 {
10290 /* Prevent the optimization at the beginning of
10291 redisplay_internal that tries a single-line update of the
10292 line containing the cursor in the selected window. */
10293 CHARPOS (this_line_start_pos) = 0;
10294
10295 /* Let the overlay arrow be updated the next time. */
10296 update_overlay_arrows (0);
10297
10298 /* If we pause after scrolling, some rows in the current
10299 matrices of some windows are not valid. */
10300 if (!WINDOW_FULL_WIDTH_P (w)
10301 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10302 update_mode_lines = 1;
10303 }
10304 else
10305 {
10306 if (!consider_all_windows_p)
10307 {
10308 /* This has already been done above if
10309 consider_all_windows_p is set. */
10310 mark_window_display_accurate_1 (w, 1);
10311
10312 /* Say overlay arrows are up to date. */
10313 update_overlay_arrows (1);
10314
10315 if (FRAME_DISPLAY (sf)->frame_up_to_date_hook != 0)
10316 FRAME_DISPLAY (sf)->frame_up_to_date_hook (sf);
10317 }
10318
10319 update_mode_lines = 0;
10320 windows_or_buffers_changed = 0;
10321 cursor_type_changed = 0;
10322 }
10323
10324 /* Start SIGIO interrupts coming again. Having them off during the
10325 code above makes it less likely one will discard output, but not
10326 impossible, since there might be stuff in the system buffer here.
10327 But it is much hairier to try to do anything about that. */
10328 if (interrupt_input)
10329 request_sigio ();
10330 RESUME_POLLING;
10331
10332 /* If a frame has become visible which was not before, redisplay
10333 again, so that we display it. Expose events for such a frame
10334 (which it gets when becoming visible) don't call the parts of
10335 redisplay constructing glyphs, so simply exposing a frame won't
10336 display anything in this case. So, we have to display these
10337 frames here explicitly. */
10338 if (!pause)
10339 {
10340 Lisp_Object tail, frame;
10341 int new_count = 0;
10342
10343 FOR_EACH_FRAME (tail, frame)
10344 {
10345 int this_is_visible = 0;
10346
10347 if (XFRAME (frame)->visible)
10348 this_is_visible = 1;
10349 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10350 if (XFRAME (frame)->visible)
10351 this_is_visible = 1;
10352
10353 if (this_is_visible)
10354 new_count++;
10355 }
10356
10357 if (new_count != number_of_visible_frames)
10358 windows_or_buffers_changed++;
10359 }
10360
10361 /* Change frame size now if a change is pending. */
10362 do_pending_window_change (1);
10363
10364 /* If we just did a pending size change, or have additional
10365 visible frames, redisplay again. */
10366 if (windows_or_buffers_changed && !pause)
10367 goto retry;
10368
10369 end_of_redisplay:
10370 unbind_to (count, Qnil);
10371 RESUME_POLLING;
10372 }
10373
10374
10375 /* Redisplay, but leave alone any recent echo area message unless
10376 another message has been requested in its place.
10377
10378 This is useful in situations where you need to redisplay but no
10379 user action has occurred, making it inappropriate for the message
10380 area to be cleared. See tracking_off and
10381 wait_reading_process_output for examples of these situations.
10382
10383 FROM_WHERE is an integer saying from where this function was
10384 called. This is useful for debugging. */
10385
10386 void
10387 redisplay_preserve_echo_area (from_where)
10388 int from_where;
10389 {
10390 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10391
10392 if (!NILP (echo_area_buffer[1]))
10393 {
10394 /* We have a previously displayed message, but no current
10395 message. Redisplay the previous message. */
10396 display_last_displayed_message_p = 1;
10397 redisplay_internal (1);
10398 display_last_displayed_message_p = 0;
10399 }
10400 else
10401 redisplay_internal (1);
10402 }
10403
10404
10405 /* Function registered with record_unwind_protect in
10406 redisplay_internal. Reset redisplaying_p to the value it had
10407 before redisplay_internal was called, and clear
10408 prevent_freeing_realized_faces_p. It also selects the previously
10409 selected frame. */
10410
10411 static Lisp_Object
10412 unwind_redisplay (val)
10413 Lisp_Object val;
10414 {
10415 Lisp_Object old_redisplaying_p, old_frame;
10416
10417 old_redisplaying_p = XCAR (val);
10418 redisplaying_p = XFASTINT (old_redisplaying_p);
10419 old_frame = XCDR (val);
10420 if (! EQ (old_frame, selected_frame))
10421 select_frame_for_redisplay (old_frame);
10422 return Qnil;
10423 }
10424
10425
10426 /* Mark the display of window W as accurate or inaccurate. If
10427 ACCURATE_P is non-zero mark display of W as accurate. If
10428 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10429 redisplay_internal is called. */
10430
10431 static void
10432 mark_window_display_accurate_1 (w, accurate_p)
10433 struct window *w;
10434 int accurate_p;
10435 {
10436 if (BUFFERP (w->buffer))
10437 {
10438 struct buffer *b = XBUFFER (w->buffer);
10439
10440 w->last_modified
10441 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10442 w->last_overlay_modified
10443 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10444 w->last_had_star
10445 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10446
10447 if (accurate_p)
10448 {
10449 b->clip_changed = 0;
10450 b->prevent_redisplay_optimizations_p = 0;
10451
10452 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10453 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10454 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10455 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10456
10457 w->current_matrix->buffer = b;
10458 w->current_matrix->begv = BUF_BEGV (b);
10459 w->current_matrix->zv = BUF_ZV (b);
10460
10461 w->last_cursor = w->cursor;
10462 w->last_cursor_off_p = w->cursor_off_p;
10463
10464 if (w == XWINDOW (selected_window))
10465 w->last_point = make_number (BUF_PT (b));
10466 else
10467 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10468 }
10469 }
10470
10471 if (accurate_p)
10472 {
10473 w->window_end_valid = w->buffer;
10474 #if 0 /* This is incorrect with variable-height lines. */
10475 xassert (XINT (w->window_end_vpos)
10476 < (WINDOW_TOTAL_LINES (w)
10477 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10478 #endif
10479 w->update_mode_line = Qnil;
10480 }
10481 }
10482
10483
10484 /* Mark the display of windows in the window tree rooted at WINDOW as
10485 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10486 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10487 be redisplayed the next time redisplay_internal is called. */
10488
10489 void
10490 mark_window_display_accurate (window, accurate_p)
10491 Lisp_Object window;
10492 int accurate_p;
10493 {
10494 struct window *w;
10495
10496 for (; !NILP (window); window = w->next)
10497 {
10498 w = XWINDOW (window);
10499 mark_window_display_accurate_1 (w, accurate_p);
10500
10501 if (!NILP (w->vchild))
10502 mark_window_display_accurate (w->vchild, accurate_p);
10503 if (!NILP (w->hchild))
10504 mark_window_display_accurate (w->hchild, accurate_p);
10505 }
10506
10507 if (accurate_p)
10508 {
10509 update_overlay_arrows (1);
10510 }
10511 else
10512 {
10513 /* Force a thorough redisplay the next time by setting
10514 last_arrow_position and last_arrow_string to t, which is
10515 unequal to any useful value of Voverlay_arrow_... */
10516 update_overlay_arrows (-1);
10517 }
10518 }
10519
10520
10521 /* Return value in display table DP (Lisp_Char_Table *) for character
10522 C. Since a display table doesn't have any parent, we don't have to
10523 follow parent. Do not call this function directly but use the
10524 macro DISP_CHAR_VECTOR. */
10525
10526 Lisp_Object
10527 disp_char_vector (dp, c)
10528 struct Lisp_Char_Table *dp;
10529 int c;
10530 {
10531 int code[4], i;
10532 Lisp_Object val;
10533
10534 if (SINGLE_BYTE_CHAR_P (c))
10535 return (dp->contents[c]);
10536
10537 SPLIT_CHAR (c, code[0], code[1], code[2]);
10538 if (code[1] < 32)
10539 code[1] = -1;
10540 else if (code[2] < 32)
10541 code[2] = -1;
10542
10543 /* Here, the possible range of code[0] (== charset ID) is
10544 128..max_charset. Since the top level char table contains data
10545 for multibyte characters after 256th element, we must increment
10546 code[0] by 128 to get a correct index. */
10547 code[0] += 128;
10548 code[3] = -1; /* anchor */
10549
10550 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10551 {
10552 val = dp->contents[code[i]];
10553 if (!SUB_CHAR_TABLE_P (val))
10554 return (NILP (val) ? dp->defalt : val);
10555 }
10556
10557 /* Here, val is a sub char table. We return the default value of
10558 it. */
10559 return (dp->defalt);
10560 }
10561
10562
10563 \f
10564 /***********************************************************************
10565 Window Redisplay
10566 ***********************************************************************/
10567
10568 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10569
10570 static void
10571 redisplay_windows (window)
10572 Lisp_Object window;
10573 {
10574 while (!NILP (window))
10575 {
10576 struct window *w = XWINDOW (window);
10577
10578 if (!NILP (w->hchild))
10579 redisplay_windows (w->hchild);
10580 else if (!NILP (w->vchild))
10581 redisplay_windows (w->vchild);
10582 else
10583 {
10584 displayed_buffer = XBUFFER (w->buffer);
10585 /* Use list_of_error, not Qerror, so that
10586 we catch only errors and don't run the debugger. */
10587 internal_condition_case_1 (redisplay_window_0, window,
10588 list_of_error,
10589 redisplay_window_error);
10590 }
10591
10592 window = w->next;
10593 }
10594 }
10595
10596 static Lisp_Object
10597 redisplay_window_error ()
10598 {
10599 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10600 return Qnil;
10601 }
10602
10603 static Lisp_Object
10604 redisplay_window_0 (window)
10605 Lisp_Object window;
10606 {
10607 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10608 redisplay_window (window, 0);
10609 return Qnil;
10610 }
10611
10612 static Lisp_Object
10613 redisplay_window_1 (window)
10614 Lisp_Object window;
10615 {
10616 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10617 redisplay_window (window, 1);
10618 return Qnil;
10619 }
10620 \f
10621
10622 /* Increment GLYPH until it reaches END or CONDITION fails while
10623 adding (GLYPH)->pixel_width to X. */
10624
10625 #define SKIP_GLYPHS(glyph, end, x, condition) \
10626 do \
10627 { \
10628 (x) += (glyph)->pixel_width; \
10629 ++(glyph); \
10630 } \
10631 while ((glyph) < (end) && (condition))
10632
10633
10634 /* Set cursor position of W. PT is assumed to be displayed in ROW.
10635 DELTA is the number of bytes by which positions recorded in ROW
10636 differ from current buffer positions. */
10637
10638 void
10639 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10640 struct window *w;
10641 struct glyph_row *row;
10642 struct glyph_matrix *matrix;
10643 int delta, delta_bytes, dy, dvpos;
10644 {
10645 struct glyph *glyph = row->glyphs[TEXT_AREA];
10646 struct glyph *end = glyph + row->used[TEXT_AREA];
10647 struct glyph *cursor = NULL;
10648 /* The first glyph that starts a sequence of glyphs from string. */
10649 struct glyph *string_start;
10650 /* The X coordinate of string_start. */
10651 int string_start_x;
10652 /* The last known character position. */
10653 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10654 /* The last known character position before string_start. */
10655 int string_before_pos;
10656 int x = row->x;
10657 int cursor_x = x;
10658 int cursor_from_overlay_pos = 0;
10659 int pt_old = PT - delta;
10660
10661 /* Skip over glyphs not having an object at the start of the row.
10662 These are special glyphs like truncation marks on terminal
10663 frames. */
10664 if (row->displays_text_p)
10665 while (glyph < end
10666 && INTEGERP (glyph->object)
10667 && glyph->charpos < 0)
10668 {
10669 x += glyph->pixel_width;
10670 ++glyph;
10671 }
10672
10673 string_start = NULL;
10674 while (glyph < end
10675 && !INTEGERP (glyph->object)
10676 && (!BUFFERP (glyph->object)
10677 || (last_pos = glyph->charpos) < pt_old))
10678 {
10679 if (! STRINGP (glyph->object))
10680 {
10681 string_start = NULL;
10682 x += glyph->pixel_width;
10683 ++glyph;
10684 if (cursor_from_overlay_pos
10685 && last_pos > cursor_from_overlay_pos)
10686 {
10687 cursor_from_overlay_pos = 0;
10688 cursor = 0;
10689 }
10690 }
10691 else
10692 {
10693 string_before_pos = last_pos;
10694 string_start = glyph;
10695 string_start_x = x;
10696 /* Skip all glyphs from string. */
10697 do
10698 {
10699 int pos;
10700 if ((cursor == NULL || glyph > cursor)
10701 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
10702 Qcursor, (glyph)->object))
10703 && (pos = string_buffer_position (w, glyph->object,
10704 string_before_pos),
10705 (pos == 0 /* From overlay */
10706 || pos == pt_old)))
10707 {
10708 /* Estimate overlay buffer position from the buffer
10709 positions of the glyphs before and after the overlay.
10710 Add 1 to last_pos so that if point corresponds to the
10711 glyph right after the overlay, we still use a 'cursor'
10712 property found in that overlay. */
10713 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
10714 cursor = glyph;
10715 cursor_x = x;
10716 }
10717 x += glyph->pixel_width;
10718 ++glyph;
10719 }
10720 while (glyph < end && STRINGP (glyph->object));
10721 }
10722 }
10723
10724 if (cursor != NULL)
10725 {
10726 glyph = cursor;
10727 x = cursor_x;
10728 }
10729 else if (string_start
10730 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
10731 {
10732 /* We may have skipped over point because the previous glyphs
10733 are from string. As there's no easy way to know the
10734 character position of the current glyph, find the correct
10735 glyph on point by scanning from string_start again. */
10736 Lisp_Object limit;
10737 Lisp_Object string;
10738 int pos;
10739
10740 limit = make_number (pt_old + 1);
10741 end = glyph;
10742 glyph = string_start;
10743 x = string_start_x;
10744 string = glyph->object;
10745 pos = string_buffer_position (w, string, string_before_pos);
10746 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
10747 because we always put cursor after overlay strings. */
10748 while (pos == 0 && glyph < end)
10749 {
10750 string = glyph->object;
10751 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10752 if (glyph < end)
10753 pos = string_buffer_position (w, glyph->object, string_before_pos);
10754 }
10755
10756 while (glyph < end)
10757 {
10758 pos = XINT (Fnext_single_char_property_change
10759 (make_number (pos), Qdisplay, Qnil, limit));
10760 if (pos > pt_old)
10761 break;
10762 /* Skip glyphs from the same string. */
10763 string = glyph->object;
10764 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10765 /* Skip glyphs from an overlay. */
10766 while (glyph < end
10767 && ! string_buffer_position (w, glyph->object, pos))
10768 {
10769 string = glyph->object;
10770 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10771 }
10772 }
10773 }
10774
10775 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
10776 w->cursor.x = x;
10777 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
10778 w->cursor.y = row->y + dy;
10779
10780 if (w == XWINDOW (selected_window))
10781 {
10782 if (!row->continued_p
10783 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
10784 && row->x == 0)
10785 {
10786 this_line_buffer = XBUFFER (w->buffer);
10787
10788 CHARPOS (this_line_start_pos)
10789 = MATRIX_ROW_START_CHARPOS (row) + delta;
10790 BYTEPOS (this_line_start_pos)
10791 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
10792
10793 CHARPOS (this_line_end_pos)
10794 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
10795 BYTEPOS (this_line_end_pos)
10796 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
10797
10798 this_line_y = w->cursor.y;
10799 this_line_pixel_height = row->height;
10800 this_line_vpos = w->cursor.vpos;
10801 this_line_start_x = row->x;
10802 }
10803 else
10804 CHARPOS (this_line_start_pos) = 0;
10805 }
10806 }
10807
10808
10809 /* Run window scroll functions, if any, for WINDOW with new window
10810 start STARTP. Sets the window start of WINDOW to that position.
10811
10812 We assume that the window's buffer is really current. */
10813
10814 static INLINE struct text_pos
10815 run_window_scroll_functions (window, startp)
10816 Lisp_Object window;
10817 struct text_pos startp;
10818 {
10819 struct window *w = XWINDOW (window);
10820 SET_MARKER_FROM_TEXT_POS (w->start, startp);
10821
10822 if (current_buffer != XBUFFER (w->buffer))
10823 abort ();
10824
10825 if (!NILP (Vwindow_scroll_functions))
10826 {
10827 run_hook_with_args_2 (Qwindow_scroll_functions, window,
10828 make_number (CHARPOS (startp)));
10829 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10830 /* In case the hook functions switch buffers. */
10831 if (current_buffer != XBUFFER (w->buffer))
10832 set_buffer_internal_1 (XBUFFER (w->buffer));
10833 }
10834
10835 return startp;
10836 }
10837
10838
10839 /* Make sure the line containing the cursor is fully visible.
10840 A value of 1 means there is nothing to be done.
10841 (Either the line is fully visible, or it cannot be made so,
10842 or we cannot tell.)
10843
10844 If FORCE_P is non-zero, return 0 even if partial visible cursor row
10845 is higher than window.
10846
10847 A value of 0 means the caller should do scrolling
10848 as if point had gone off the screen. */
10849
10850 static int
10851 make_cursor_line_fully_visible (w, force_p)
10852 struct window *w;
10853 int force_p;
10854 {
10855 struct glyph_matrix *matrix;
10856 struct glyph_row *row;
10857 int window_height;
10858
10859 /* It's not always possible to find the cursor, e.g, when a window
10860 is full of overlay strings. Don't do anything in that case. */
10861 if (w->cursor.vpos < 0)
10862 return 1;
10863
10864 matrix = w->desired_matrix;
10865 row = MATRIX_ROW (matrix, w->cursor.vpos);
10866
10867 /* If the cursor row is not partially visible, there's nothing to do. */
10868 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
10869 return 1;
10870
10871 /* If the row the cursor is in is taller than the window's height,
10872 it's not clear what to do, so do nothing. */
10873 window_height = window_box_height (w);
10874 if (row->height >= window_height)
10875 {
10876 if (!force_p || w->vscroll)
10877 return 1;
10878 }
10879 return 0;
10880
10881 #if 0
10882 /* This code used to try to scroll the window just enough to make
10883 the line visible. It returned 0 to say that the caller should
10884 allocate larger glyph matrices. */
10885
10886 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
10887 {
10888 int dy = row->height - row->visible_height;
10889 w->vscroll = 0;
10890 w->cursor.y += dy;
10891 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10892 }
10893 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
10894 {
10895 int dy = - (row->height - row->visible_height);
10896 w->vscroll = dy;
10897 w->cursor.y += dy;
10898 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10899 }
10900
10901 /* When we change the cursor y-position of the selected window,
10902 change this_line_y as well so that the display optimization for
10903 the cursor line of the selected window in redisplay_internal uses
10904 the correct y-position. */
10905 if (w == XWINDOW (selected_window))
10906 this_line_y = w->cursor.y;
10907
10908 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
10909 redisplay with larger matrices. */
10910 if (matrix->nrows < required_matrix_height (w))
10911 {
10912 fonts_changed_p = 1;
10913 return 0;
10914 }
10915
10916 return 1;
10917 #endif /* 0 */
10918 }
10919
10920
10921 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
10922 non-zero means only WINDOW is redisplayed in redisplay_internal.
10923 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
10924 in redisplay_window to bring a partially visible line into view in
10925 the case that only the cursor has moved.
10926
10927 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
10928 last screen line's vertical height extends past the end of the screen.
10929
10930 Value is
10931
10932 1 if scrolling succeeded
10933
10934 0 if scrolling didn't find point.
10935
10936 -1 if new fonts have been loaded so that we must interrupt
10937 redisplay, adjust glyph matrices, and try again. */
10938
10939 enum
10940 {
10941 SCROLLING_SUCCESS,
10942 SCROLLING_FAILED,
10943 SCROLLING_NEED_LARGER_MATRICES
10944 };
10945
10946 static int
10947 try_scrolling (window, just_this_one_p, scroll_conservatively,
10948 scroll_step, temp_scroll_step, last_line_misfit)
10949 Lisp_Object window;
10950 int just_this_one_p;
10951 EMACS_INT scroll_conservatively, scroll_step;
10952 int temp_scroll_step;
10953 int last_line_misfit;
10954 {
10955 struct window *w = XWINDOW (window);
10956 struct frame *f = XFRAME (w->frame);
10957 struct text_pos scroll_margin_pos;
10958 struct text_pos pos;
10959 struct text_pos startp;
10960 struct it it;
10961 Lisp_Object window_end;
10962 int this_scroll_margin;
10963 int dy = 0;
10964 int scroll_max;
10965 int rc;
10966 int amount_to_scroll = 0;
10967 Lisp_Object aggressive;
10968 int height;
10969 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
10970
10971 #if GLYPH_DEBUG
10972 debug_method_add (w, "try_scrolling");
10973 #endif
10974
10975 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10976
10977 /* Compute scroll margin height in pixels. We scroll when point is
10978 within this distance from the top or bottom of the window. */
10979 if (scroll_margin > 0)
10980 {
10981 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
10982 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
10983 }
10984 else
10985 this_scroll_margin = 0;
10986
10987 /* Force scroll_conservatively to have a reasonable value so it doesn't
10988 cause an overflow while computing how much to scroll. */
10989 if (scroll_conservatively)
10990 scroll_conservatively = min (scroll_conservatively,
10991 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
10992
10993 /* Compute how much we should try to scroll maximally to bring point
10994 into view. */
10995 if (scroll_step || scroll_conservatively || temp_scroll_step)
10996 scroll_max = max (scroll_step,
10997 max (scroll_conservatively, temp_scroll_step));
10998 else if (NUMBERP (current_buffer->scroll_down_aggressively)
10999 || NUMBERP (current_buffer->scroll_up_aggressively))
11000 /* We're trying to scroll because of aggressive scrolling
11001 but no scroll_step is set. Choose an arbitrary one. Maybe
11002 there should be a variable for this. */
11003 scroll_max = 10;
11004 else
11005 scroll_max = 0;
11006 scroll_max *= FRAME_LINE_HEIGHT (f);
11007
11008 /* Decide whether we have to scroll down. Start at the window end
11009 and move this_scroll_margin up to find the position of the scroll
11010 margin. */
11011 window_end = Fwindow_end (window, Qt);
11012
11013 too_near_end:
11014
11015 CHARPOS (scroll_margin_pos) = XINT (window_end);
11016 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11017
11018 if (this_scroll_margin || extra_scroll_margin_lines)
11019 {
11020 start_display (&it, w, scroll_margin_pos);
11021 if (this_scroll_margin)
11022 move_it_vertically (&it, - this_scroll_margin);
11023 if (extra_scroll_margin_lines)
11024 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11025 scroll_margin_pos = it.current.pos;
11026 }
11027
11028 if (PT >= CHARPOS (scroll_margin_pos))
11029 {
11030 int y0;
11031
11032 /* Point is in the scroll margin at the bottom of the window, or
11033 below. Compute a new window start that makes point visible. */
11034
11035 /* Compute the distance from the scroll margin to PT.
11036 Give up if the distance is greater than scroll_max. */
11037 start_display (&it, w, scroll_margin_pos);
11038 y0 = it.current_y;
11039 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11040 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11041
11042 /* To make point visible, we have to move the window start
11043 down so that the line the cursor is in is visible, which
11044 means we have to add in the height of the cursor line. */
11045 dy = line_bottom_y (&it) - y0;
11046
11047 if (dy > scroll_max)
11048 return SCROLLING_FAILED;
11049
11050 /* Move the window start down. If scrolling conservatively,
11051 move it just enough down to make point visible. If
11052 scroll_step is set, move it down by scroll_step. */
11053 start_display (&it, w, startp);
11054
11055 if (scroll_conservatively)
11056 /* Set AMOUNT_TO_SCROLL to at least one line,
11057 and at most scroll_conservatively lines. */
11058 amount_to_scroll
11059 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11060 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11061 else if (scroll_step || temp_scroll_step)
11062 amount_to_scroll = scroll_max;
11063 else
11064 {
11065 aggressive = current_buffer->scroll_up_aggressively;
11066 height = WINDOW_BOX_TEXT_HEIGHT (w);
11067 if (NUMBERP (aggressive))
11068 {
11069 double float_amount = XFLOATINT (aggressive) * height;
11070 amount_to_scroll = float_amount;
11071 if (amount_to_scroll == 0 && float_amount > 0)
11072 amount_to_scroll = 1;
11073 }
11074 }
11075
11076 if (amount_to_scroll <= 0)
11077 return SCROLLING_FAILED;
11078
11079 /* If moving by amount_to_scroll leaves STARTP unchanged,
11080 move it down one screen line. */
11081
11082 move_it_vertically (&it, amount_to_scroll);
11083 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11084 move_it_by_lines (&it, 1, 1);
11085 startp = it.current.pos;
11086 }
11087 else
11088 {
11089 /* See if point is inside the scroll margin at the top of the
11090 window. */
11091 scroll_margin_pos = startp;
11092 if (this_scroll_margin)
11093 {
11094 start_display (&it, w, startp);
11095 move_it_vertically (&it, this_scroll_margin);
11096 scroll_margin_pos = it.current.pos;
11097 }
11098
11099 if (PT < CHARPOS (scroll_margin_pos))
11100 {
11101 /* Point is in the scroll margin at the top of the window or
11102 above what is displayed in the window. */
11103 int y0;
11104
11105 /* Compute the vertical distance from PT to the scroll
11106 margin position. Give up if distance is greater than
11107 scroll_max. */
11108 SET_TEXT_POS (pos, PT, PT_BYTE);
11109 start_display (&it, w, pos);
11110 y0 = it.current_y;
11111 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11112 it.last_visible_y, -1,
11113 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11114 dy = it.current_y - y0;
11115 if (dy > scroll_max)
11116 return SCROLLING_FAILED;
11117
11118 /* Compute new window start. */
11119 start_display (&it, w, startp);
11120
11121 if (scroll_conservatively)
11122 amount_to_scroll
11123 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11124 else if (scroll_step || temp_scroll_step)
11125 amount_to_scroll = scroll_max;
11126 else
11127 {
11128 aggressive = current_buffer->scroll_down_aggressively;
11129 height = WINDOW_BOX_TEXT_HEIGHT (w);
11130 if (NUMBERP (aggressive))
11131 {
11132 double float_amount = XFLOATINT (aggressive) * height;
11133 amount_to_scroll = float_amount;
11134 if (amount_to_scroll == 0 && float_amount > 0)
11135 amount_to_scroll = 1;
11136 }
11137 }
11138
11139 if (amount_to_scroll <= 0)
11140 return SCROLLING_FAILED;
11141
11142 move_it_vertically (&it, - amount_to_scroll);
11143 startp = it.current.pos;
11144 }
11145 }
11146
11147 /* Run window scroll functions. */
11148 startp = run_window_scroll_functions (window, startp);
11149
11150 /* Display the window. Give up if new fonts are loaded, or if point
11151 doesn't appear. */
11152 if (!try_window (window, startp))
11153 rc = SCROLLING_NEED_LARGER_MATRICES;
11154 else if (w->cursor.vpos < 0)
11155 {
11156 clear_glyph_matrix (w->desired_matrix);
11157 rc = SCROLLING_FAILED;
11158 }
11159 else
11160 {
11161 /* Maybe forget recorded base line for line number display. */
11162 if (!just_this_one_p
11163 || current_buffer->clip_changed
11164 || BEG_UNCHANGED < CHARPOS (startp))
11165 w->base_line_number = Qnil;
11166
11167 /* If cursor ends up on a partially visible line,
11168 treat that as being off the bottom of the screen. */
11169 if (! make_cursor_line_fully_visible (w, extra_scroll_margin_lines <= 1))
11170 {
11171 clear_glyph_matrix (w->desired_matrix);
11172 ++extra_scroll_margin_lines;
11173 goto too_near_end;
11174 }
11175 rc = SCROLLING_SUCCESS;
11176 }
11177
11178 return rc;
11179 }
11180
11181
11182 /* Compute a suitable window start for window W if display of W starts
11183 on a continuation line. Value is non-zero if a new window start
11184 was computed.
11185
11186 The new window start will be computed, based on W's width, starting
11187 from the start of the continued line. It is the start of the
11188 screen line with the minimum distance from the old start W->start. */
11189
11190 static int
11191 compute_window_start_on_continuation_line (w)
11192 struct window *w;
11193 {
11194 struct text_pos pos, start_pos;
11195 int window_start_changed_p = 0;
11196
11197 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11198
11199 /* If window start is on a continuation line... Window start may be
11200 < BEGV in case there's invisible text at the start of the
11201 buffer (M-x rmail, for example). */
11202 if (CHARPOS (start_pos) > BEGV
11203 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11204 {
11205 struct it it;
11206 struct glyph_row *row;
11207
11208 /* Handle the case that the window start is out of range. */
11209 if (CHARPOS (start_pos) < BEGV)
11210 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11211 else if (CHARPOS (start_pos) > ZV)
11212 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11213
11214 /* Find the start of the continued line. This should be fast
11215 because scan_buffer is fast (newline cache). */
11216 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11217 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11218 row, DEFAULT_FACE_ID);
11219 reseat_at_previous_visible_line_start (&it);
11220
11221 /* If the line start is "too far" away from the window start,
11222 say it takes too much time to compute a new window start. */
11223 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11224 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11225 {
11226 int min_distance, distance;
11227
11228 /* Move forward by display lines to find the new window
11229 start. If window width was enlarged, the new start can
11230 be expected to be > the old start. If window width was
11231 decreased, the new window start will be < the old start.
11232 So, we're looking for the display line start with the
11233 minimum distance from the old window start. */
11234 pos = it.current.pos;
11235 min_distance = INFINITY;
11236 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11237 distance < min_distance)
11238 {
11239 min_distance = distance;
11240 pos = it.current.pos;
11241 move_it_by_lines (&it, 1, 0);
11242 }
11243
11244 /* Set the window start there. */
11245 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11246 window_start_changed_p = 1;
11247 }
11248 }
11249
11250 return window_start_changed_p;
11251 }
11252
11253
11254 /* Try cursor movement in case text has not changed in window WINDOW,
11255 with window start STARTP. Value is
11256
11257 CURSOR_MOVEMENT_SUCCESS if successful
11258
11259 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11260
11261 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11262 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11263 we want to scroll as if scroll-step were set to 1. See the code.
11264
11265 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11266 which case we have to abort this redisplay, and adjust matrices
11267 first. */
11268
11269 enum
11270 {
11271 CURSOR_MOVEMENT_SUCCESS,
11272 CURSOR_MOVEMENT_CANNOT_BE_USED,
11273 CURSOR_MOVEMENT_MUST_SCROLL,
11274 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11275 };
11276
11277 static int
11278 try_cursor_movement (window, startp, scroll_step)
11279 Lisp_Object window;
11280 struct text_pos startp;
11281 int *scroll_step;
11282 {
11283 struct window *w = XWINDOW (window);
11284 struct frame *f = XFRAME (w->frame);
11285 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11286
11287 #if GLYPH_DEBUG
11288 if (inhibit_try_cursor_movement)
11289 return rc;
11290 #endif
11291
11292 /* Handle case where text has not changed, only point, and it has
11293 not moved off the frame. */
11294 if (/* Point may be in this window. */
11295 PT >= CHARPOS (startp)
11296 /* Selective display hasn't changed. */
11297 && !current_buffer->clip_changed
11298 /* Function force-mode-line-update is used to force a thorough
11299 redisplay. It sets either windows_or_buffers_changed or
11300 update_mode_lines. So don't take a shortcut here for these
11301 cases. */
11302 && !update_mode_lines
11303 && !windows_or_buffers_changed
11304 && !cursor_type_changed
11305 /* Can't use this case if highlighting a region. When a
11306 region exists, cursor movement has to do more than just
11307 set the cursor. */
11308 && !(!NILP (Vtransient_mark_mode)
11309 && !NILP (current_buffer->mark_active))
11310 && NILP (w->region_showing)
11311 && NILP (Vshow_trailing_whitespace)
11312 /* Right after splitting windows, last_point may be nil. */
11313 && INTEGERP (w->last_point)
11314 /* This code is not used for mini-buffer for the sake of the case
11315 of redisplaying to replace an echo area message; since in
11316 that case the mini-buffer contents per se are usually
11317 unchanged. This code is of no real use in the mini-buffer
11318 since the handling of this_line_start_pos, etc., in redisplay
11319 handles the same cases. */
11320 && !EQ (window, minibuf_window)
11321 /* When splitting windows or for new windows, it happens that
11322 redisplay is called with a nil window_end_vpos or one being
11323 larger than the window. This should really be fixed in
11324 window.c. I don't have this on my list, now, so we do
11325 approximately the same as the old redisplay code. --gerd. */
11326 && INTEGERP (w->window_end_vpos)
11327 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11328 && (FRAME_WINDOW_P (f)
11329 || !overlay_arrow_in_current_buffer_p ()))
11330 {
11331 int this_scroll_margin, top_scroll_margin;
11332 struct glyph_row *row = NULL;
11333
11334 #if GLYPH_DEBUG
11335 debug_method_add (w, "cursor movement");
11336 #endif
11337
11338 /* Scroll if point within this distance from the top or bottom
11339 of the window. This is a pixel value. */
11340 this_scroll_margin = max (0, scroll_margin);
11341 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11342 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11343
11344 top_scroll_margin = this_scroll_margin;
11345 if (WINDOW_WANTS_HEADER_LINE_P (w))
11346 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
11347
11348 /* Start with the row the cursor was displayed during the last
11349 not paused redisplay. Give up if that row is not valid. */
11350 if (w->last_cursor.vpos < 0
11351 || w->last_cursor.vpos >= w->current_matrix->nrows)
11352 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11353 else
11354 {
11355 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11356 if (row->mode_line_p)
11357 ++row;
11358 if (!row->enabled_p)
11359 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11360 }
11361
11362 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11363 {
11364 int scroll_p = 0;
11365 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11366
11367 if (PT > XFASTINT (w->last_point))
11368 {
11369 /* Point has moved forward. */
11370 while (MATRIX_ROW_END_CHARPOS (row) < PT
11371 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11372 {
11373 xassert (row->enabled_p);
11374 ++row;
11375 }
11376
11377 /* The end position of a row equals the start position
11378 of the next row. If PT is there, we would rather
11379 display it in the next line. */
11380 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11381 && MATRIX_ROW_END_CHARPOS (row) == PT
11382 && !cursor_row_p (w, row))
11383 ++row;
11384
11385 /* If within the scroll margin, scroll. Note that
11386 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11387 the next line would be drawn, and that
11388 this_scroll_margin can be zero. */
11389 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11390 || PT > MATRIX_ROW_END_CHARPOS (row)
11391 /* Line is completely visible last line in window
11392 and PT is to be set in the next line. */
11393 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11394 && PT == MATRIX_ROW_END_CHARPOS (row)
11395 && !row->ends_at_zv_p
11396 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11397 scroll_p = 1;
11398 }
11399 else if (PT < XFASTINT (w->last_point))
11400 {
11401 /* Cursor has to be moved backward. Note that PT >=
11402 CHARPOS (startp) because of the outer if-statement. */
11403 while (!row->mode_line_p
11404 && (MATRIX_ROW_START_CHARPOS (row) > PT
11405 || (MATRIX_ROW_START_CHARPOS (row) == PT
11406 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11407 && (row->y > top_scroll_margin
11408 || CHARPOS (startp) == BEGV))
11409 {
11410 xassert (row->enabled_p);
11411 --row;
11412 }
11413
11414 /* Consider the following case: Window starts at BEGV,
11415 there is invisible, intangible text at BEGV, so that
11416 display starts at some point START > BEGV. It can
11417 happen that we are called with PT somewhere between
11418 BEGV and START. Try to handle that case. */
11419 if (row < w->current_matrix->rows
11420 || row->mode_line_p)
11421 {
11422 row = w->current_matrix->rows;
11423 if (row->mode_line_p)
11424 ++row;
11425 }
11426
11427 /* Due to newlines in overlay strings, we may have to
11428 skip forward over overlay strings. */
11429 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11430 && MATRIX_ROW_END_CHARPOS (row) == PT
11431 && !cursor_row_p (w, row))
11432 ++row;
11433
11434 /* If within the scroll margin, scroll. */
11435 if (row->y < top_scroll_margin
11436 && CHARPOS (startp) != BEGV)
11437 scroll_p = 1;
11438 }
11439
11440 if (PT < MATRIX_ROW_START_CHARPOS (row)
11441 || PT > MATRIX_ROW_END_CHARPOS (row))
11442 {
11443 /* if PT is not in the glyph row, give up. */
11444 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11445 }
11446 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
11447 {
11448 if (PT == MATRIX_ROW_END_CHARPOS (row)
11449 && !row->ends_at_zv_p
11450 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11451 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11452 else if (row->height > window_box_height (w))
11453 {
11454 /* If we end up in a partially visible line, let's
11455 make it fully visible, except when it's taller
11456 than the window, in which case we can't do much
11457 about it. */
11458 *scroll_step = 1;
11459 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11460 }
11461 else
11462 {
11463 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11464 if (!make_cursor_line_fully_visible (w, 0))
11465 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11466 else
11467 rc = CURSOR_MOVEMENT_SUCCESS;
11468 }
11469 }
11470 else if (scroll_p)
11471 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11472 else
11473 {
11474 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11475 rc = CURSOR_MOVEMENT_SUCCESS;
11476 }
11477 }
11478 }
11479
11480 return rc;
11481 }
11482
11483 void
11484 set_vertical_scroll_bar (w)
11485 struct window *w;
11486 {
11487 int start, end, whole;
11488
11489 /* Calculate the start and end positions for the current window.
11490 At some point, it would be nice to choose between scrollbars
11491 which reflect the whole buffer size, with special markers
11492 indicating narrowing, and scrollbars which reflect only the
11493 visible region.
11494
11495 Note that mini-buffers sometimes aren't displaying any text. */
11496 if (!MINI_WINDOW_P (w)
11497 || (w == XWINDOW (minibuf_window)
11498 && NILP (echo_area_buffer[0])))
11499 {
11500 struct buffer *buf = XBUFFER (w->buffer);
11501 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11502 start = marker_position (w->start) - BUF_BEGV (buf);
11503 /* I don't think this is guaranteed to be right. For the
11504 moment, we'll pretend it is. */
11505 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11506
11507 if (end < start)
11508 end = start;
11509 if (whole < (end - start))
11510 whole = end - start;
11511 }
11512 else
11513 start = end = whole = 0;
11514
11515 /* Indicate what this scroll bar ought to be displaying now. */
11516 if (FRAME_DISPLAY (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
11517 (*FRAME_DISPLAY (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
11518 (w, end - start, whole, start);
11519 }
11520
11521
11522 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11523 selected_window is redisplayed.
11524
11525 We can return without actually redisplaying the window if
11526 fonts_changed_p is nonzero. In that case, redisplay_internal will
11527 retry. */
11528
11529 static void
11530 redisplay_window (window, just_this_one_p)
11531 Lisp_Object window;
11532 int just_this_one_p;
11533 {
11534 struct window *w = XWINDOW (window);
11535 struct frame *f = XFRAME (w->frame);
11536 struct buffer *buffer = XBUFFER (w->buffer);
11537 struct buffer *old = current_buffer;
11538 struct text_pos lpoint, opoint, startp;
11539 int update_mode_line;
11540 int tem;
11541 struct it it;
11542 /* Record it now because it's overwritten. */
11543 int current_matrix_up_to_date_p = 0;
11544 int used_current_matrix_p = 0;
11545 /* This is less strict than current_matrix_up_to_date_p.
11546 It indictes that the buffer contents and narrowing are unchanged. */
11547 int buffer_unchanged_p = 0;
11548 int temp_scroll_step = 0;
11549 int count = SPECPDL_INDEX ();
11550 int rc;
11551 int centering_position;
11552 int last_line_misfit = 0;
11553
11554 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11555 opoint = lpoint;
11556
11557 /* W must be a leaf window here. */
11558 xassert (!NILP (w->buffer));
11559 #if GLYPH_DEBUG
11560 *w->desired_matrix->method = 0;
11561 #endif
11562
11563 specbind (Qinhibit_point_motion_hooks, Qt);
11564
11565 reconsider_clip_changes (w, buffer);
11566
11567 /* Has the mode line to be updated? */
11568 update_mode_line = (!NILP (w->update_mode_line)
11569 || update_mode_lines
11570 || buffer->clip_changed
11571 || buffer->prevent_redisplay_optimizations_p);
11572
11573 if (MINI_WINDOW_P (w))
11574 {
11575 if (w == XWINDOW (echo_area_window)
11576 && !NILP (echo_area_buffer[0]))
11577 {
11578 if (update_mode_line)
11579 /* We may have to update a tty frame's menu bar or a
11580 tool-bar. Example `M-x C-h C-h C-g'. */
11581 goto finish_menu_bars;
11582 else
11583 /* We've already displayed the echo area glyphs in this window. */
11584 goto finish_scroll_bars;
11585 }
11586 else if ((w != XWINDOW (minibuf_window)
11587 || minibuf_level == 0)
11588 /* When buffer is nonempty, redisplay window normally. */
11589 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
11590 /* Quail displays non-mini buffers in minibuffer window.
11591 In that case, redisplay the window normally. */
11592 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
11593 {
11594 /* W is a mini-buffer window, but it's not active, so clear
11595 it. */
11596 int yb = window_text_bottom_y (w);
11597 struct glyph_row *row;
11598 int y;
11599
11600 for (y = 0, row = w->desired_matrix->rows;
11601 y < yb;
11602 y += row->height, ++row)
11603 blank_row (w, row, y);
11604 goto finish_scroll_bars;
11605 }
11606
11607 clear_glyph_matrix (w->desired_matrix);
11608 }
11609
11610 /* Otherwise set up data on this window; select its buffer and point
11611 value. */
11612 /* Really select the buffer, for the sake of buffer-local
11613 variables. */
11614 set_buffer_internal_1 (XBUFFER (w->buffer));
11615 SET_TEXT_POS (opoint, PT, PT_BYTE);
11616
11617 current_matrix_up_to_date_p
11618 = (!NILP (w->window_end_valid)
11619 && !current_buffer->clip_changed
11620 && !current_buffer->prevent_redisplay_optimizations_p
11621 && XFASTINT (w->last_modified) >= MODIFF
11622 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11623
11624 buffer_unchanged_p
11625 = (!NILP (w->window_end_valid)
11626 && !current_buffer->clip_changed
11627 && XFASTINT (w->last_modified) >= MODIFF
11628 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11629
11630 /* When windows_or_buffers_changed is non-zero, we can't rely on
11631 the window end being valid, so set it to nil there. */
11632 if (windows_or_buffers_changed)
11633 {
11634 /* If window starts on a continuation line, maybe adjust the
11635 window start in case the window's width changed. */
11636 if (XMARKER (w->start)->buffer == current_buffer)
11637 compute_window_start_on_continuation_line (w);
11638
11639 w->window_end_valid = Qnil;
11640 }
11641
11642 /* Some sanity checks. */
11643 CHECK_WINDOW_END (w);
11644 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
11645 abort ();
11646 if (BYTEPOS (opoint) < CHARPOS (opoint))
11647 abort ();
11648
11649 /* If %c is in mode line, update it if needed. */
11650 if (!NILP (w->column_number_displayed)
11651 /* This alternative quickly identifies a common case
11652 where no change is needed. */
11653 && !(PT == XFASTINT (w->last_point)
11654 && XFASTINT (w->last_modified) >= MODIFF
11655 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11656 && (XFASTINT (w->column_number_displayed)
11657 != (int) current_column ())) /* iftc */
11658 update_mode_line = 1;
11659
11660 /* Count number of windows showing the selected buffer. An indirect
11661 buffer counts as its base buffer. */
11662 if (!just_this_one_p)
11663 {
11664 struct buffer *current_base, *window_base;
11665 current_base = current_buffer;
11666 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11667 if (current_base->base_buffer)
11668 current_base = current_base->base_buffer;
11669 if (window_base->base_buffer)
11670 window_base = window_base->base_buffer;
11671 if (current_base == window_base)
11672 buffer_shared++;
11673 }
11674
11675 /* Point refers normally to the selected window. For any other
11676 window, set up appropriate value. */
11677 if (!EQ (window, selected_window))
11678 {
11679 int new_pt = XMARKER (w->pointm)->charpos;
11680 int new_pt_byte = marker_byte_position (w->pointm);
11681 if (new_pt < BEGV)
11682 {
11683 new_pt = BEGV;
11684 new_pt_byte = BEGV_BYTE;
11685 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
11686 }
11687 else if (new_pt > (ZV - 1))
11688 {
11689 new_pt = ZV;
11690 new_pt_byte = ZV_BYTE;
11691 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
11692 }
11693
11694 /* We don't use SET_PT so that the point-motion hooks don't run. */
11695 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
11696 }
11697
11698 /* If any of the character widths specified in the display table
11699 have changed, invalidate the width run cache. It's true that
11700 this may be a bit late to catch such changes, but the rest of
11701 redisplay goes (non-fatally) haywire when the display table is
11702 changed, so why should we worry about doing any better? */
11703 if (current_buffer->width_run_cache)
11704 {
11705 struct Lisp_Char_Table *disptab = buffer_display_table ();
11706
11707 if (! disptab_matches_widthtab (disptab,
11708 XVECTOR (current_buffer->width_table)))
11709 {
11710 invalidate_region_cache (current_buffer,
11711 current_buffer->width_run_cache,
11712 BEG, Z);
11713 recompute_width_table (current_buffer, disptab);
11714 }
11715 }
11716
11717 /* If window-start is screwed up, choose a new one. */
11718 if (XMARKER (w->start)->buffer != current_buffer)
11719 goto recenter;
11720
11721 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11722
11723 /* If someone specified a new starting point but did not insist,
11724 check whether it can be used. */
11725 if (!NILP (w->optional_new_start)
11726 && CHARPOS (startp) >= BEGV
11727 && CHARPOS (startp) <= ZV)
11728 {
11729 w->optional_new_start = Qnil;
11730 start_display (&it, w, startp);
11731 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11732 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11733 if (IT_CHARPOS (it) == PT)
11734 w->force_start = Qt;
11735 /* IT may overshoot PT if text at PT is invisible. */
11736 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
11737 w->force_start = Qt;
11738
11739
11740 }
11741
11742 /* Handle case where place to start displaying has been specified,
11743 unless the specified location is outside the accessible range. */
11744 if (!NILP (w->force_start)
11745 || w->frozen_window_start_p)
11746 {
11747 /* We set this later on if we have to adjust point. */
11748 int new_vpos = -1;
11749
11750 w->force_start = Qnil;
11751 w->vscroll = 0;
11752 w->window_end_valid = Qnil;
11753
11754 /* Forget any recorded base line for line number display. */
11755 if (!buffer_unchanged_p)
11756 w->base_line_number = Qnil;
11757
11758 /* Redisplay the mode line. Select the buffer properly for that.
11759 Also, run the hook window-scroll-functions
11760 because we have scrolled. */
11761 /* Note, we do this after clearing force_start because
11762 if there's an error, it is better to forget about force_start
11763 than to get into an infinite loop calling the hook functions
11764 and having them get more errors. */
11765 if (!update_mode_line
11766 || ! NILP (Vwindow_scroll_functions))
11767 {
11768 update_mode_line = 1;
11769 w->update_mode_line = Qt;
11770 startp = run_window_scroll_functions (window, startp);
11771 }
11772
11773 w->last_modified = make_number (0);
11774 w->last_overlay_modified = make_number (0);
11775 if (CHARPOS (startp) < BEGV)
11776 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
11777 else if (CHARPOS (startp) > ZV)
11778 SET_TEXT_POS (startp, ZV, ZV_BYTE);
11779
11780 /* Redisplay, then check if cursor has been set during the
11781 redisplay. Give up if new fonts were loaded. */
11782 if (!try_window (window, startp))
11783 {
11784 w->force_start = Qt;
11785 clear_glyph_matrix (w->desired_matrix);
11786 goto need_larger_matrices;
11787 }
11788
11789 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
11790 {
11791 /* If point does not appear, try to move point so it does
11792 appear. The desired matrix has been built above, so we
11793 can use it here. */
11794 new_vpos = window_box_height (w) / 2;
11795 }
11796
11797 if (!make_cursor_line_fully_visible (w, 0))
11798 {
11799 /* Point does appear, but on a line partly visible at end of window.
11800 Move it back to a fully-visible line. */
11801 new_vpos = window_box_height (w);
11802 }
11803
11804 /* If we need to move point for either of the above reasons,
11805 now actually do it. */
11806 if (new_vpos >= 0)
11807 {
11808 struct glyph_row *row;
11809
11810 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
11811 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
11812 ++row;
11813
11814 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
11815 MATRIX_ROW_START_BYTEPOS (row));
11816
11817 if (w != XWINDOW (selected_window))
11818 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
11819 else if (current_buffer == old)
11820 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11821
11822 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
11823
11824 /* If we are highlighting the region, then we just changed
11825 the region, so redisplay to show it. */
11826 if (!NILP (Vtransient_mark_mode)
11827 && !NILP (current_buffer->mark_active))
11828 {
11829 clear_glyph_matrix (w->desired_matrix);
11830 if (!try_window (window, startp))
11831 goto need_larger_matrices;
11832 }
11833 }
11834
11835 #if GLYPH_DEBUG
11836 debug_method_add (w, "forced window start");
11837 #endif
11838 goto done;
11839 }
11840
11841 /* Handle case where text has not changed, only point, and it has
11842 not moved off the frame, and we are not retrying after hscroll.
11843 (current_matrix_up_to_date_p is nonzero when retrying.) */
11844 if (current_matrix_up_to_date_p
11845 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
11846 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
11847 {
11848 switch (rc)
11849 {
11850 case CURSOR_MOVEMENT_SUCCESS:
11851 used_current_matrix_p = 1;
11852 goto done;
11853
11854 #if 0 /* try_cursor_movement never returns this value. */
11855 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
11856 goto need_larger_matrices;
11857 #endif
11858
11859 case CURSOR_MOVEMENT_MUST_SCROLL:
11860 goto try_to_scroll;
11861
11862 default:
11863 abort ();
11864 }
11865 }
11866 /* If current starting point was originally the beginning of a line
11867 but no longer is, find a new starting point. */
11868 else if (!NILP (w->start_at_line_beg)
11869 && !(CHARPOS (startp) <= BEGV
11870 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
11871 {
11872 #if GLYPH_DEBUG
11873 debug_method_add (w, "recenter 1");
11874 #endif
11875 goto recenter;
11876 }
11877
11878 /* Try scrolling with try_window_id. Value is > 0 if update has
11879 been done, it is -1 if we know that the same window start will
11880 not work. It is 0 if unsuccessful for some other reason. */
11881 else if ((tem = try_window_id (w)) != 0)
11882 {
11883 #if GLYPH_DEBUG
11884 debug_method_add (w, "try_window_id %d", tem);
11885 #endif
11886
11887 if (fonts_changed_p)
11888 goto need_larger_matrices;
11889 if (tem > 0)
11890 goto done;
11891
11892 /* Otherwise try_window_id has returned -1 which means that we
11893 don't want the alternative below this comment to execute. */
11894 }
11895 else if (CHARPOS (startp) >= BEGV
11896 && CHARPOS (startp) <= ZV
11897 && PT >= CHARPOS (startp)
11898 && (CHARPOS (startp) < ZV
11899 /* Avoid starting at end of buffer. */
11900 || CHARPOS (startp) == BEGV
11901 || (XFASTINT (w->last_modified) >= MODIFF
11902 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
11903 {
11904 #if GLYPH_DEBUG
11905 debug_method_add (w, "same window start");
11906 #endif
11907
11908 /* Try to redisplay starting at same place as before.
11909 If point has not moved off frame, accept the results. */
11910 if (!current_matrix_up_to_date_p
11911 /* Don't use try_window_reusing_current_matrix in this case
11912 because a window scroll function can have changed the
11913 buffer. */
11914 || !NILP (Vwindow_scroll_functions)
11915 || MINI_WINDOW_P (w)
11916 || !(used_current_matrix_p
11917 = try_window_reusing_current_matrix (w)))
11918 {
11919 IF_DEBUG (debug_method_add (w, "1"));
11920 try_window (window, startp);
11921 }
11922
11923 if (fonts_changed_p)
11924 goto need_larger_matrices;
11925
11926 if (w->cursor.vpos >= 0)
11927 {
11928 if (!just_this_one_p
11929 || current_buffer->clip_changed
11930 || BEG_UNCHANGED < CHARPOS (startp))
11931 /* Forget any recorded base line for line number display. */
11932 w->base_line_number = Qnil;
11933
11934 if (!make_cursor_line_fully_visible (w, 1))
11935 {
11936 clear_glyph_matrix (w->desired_matrix);
11937 last_line_misfit = 1;
11938 }
11939 /* Drop through and scroll. */
11940 else
11941 goto done;
11942 }
11943 else
11944 clear_glyph_matrix (w->desired_matrix);
11945 }
11946
11947 try_to_scroll:
11948
11949 w->last_modified = make_number (0);
11950 w->last_overlay_modified = make_number (0);
11951
11952 /* Redisplay the mode line. Select the buffer properly for that. */
11953 if (!update_mode_line)
11954 {
11955 update_mode_line = 1;
11956 w->update_mode_line = Qt;
11957 }
11958
11959 /* Try to scroll by specified few lines. */
11960 if ((scroll_conservatively
11961 || scroll_step
11962 || temp_scroll_step
11963 || NUMBERP (current_buffer->scroll_up_aggressively)
11964 || NUMBERP (current_buffer->scroll_down_aggressively))
11965 && !current_buffer->clip_changed
11966 && CHARPOS (startp) >= BEGV
11967 && CHARPOS (startp) <= ZV)
11968 {
11969 /* The function returns -1 if new fonts were loaded, 1 if
11970 successful, 0 if not successful. */
11971 int rc = try_scrolling (window, just_this_one_p,
11972 scroll_conservatively,
11973 scroll_step,
11974 temp_scroll_step, last_line_misfit);
11975 switch (rc)
11976 {
11977 case SCROLLING_SUCCESS:
11978 goto done;
11979
11980 case SCROLLING_NEED_LARGER_MATRICES:
11981 goto need_larger_matrices;
11982
11983 case SCROLLING_FAILED:
11984 break;
11985
11986 default:
11987 abort ();
11988 }
11989 }
11990
11991 /* Finally, just choose place to start which centers point */
11992
11993 recenter:
11994 centering_position = window_box_height (w) / 2;
11995
11996 point_at_top:
11997 /* Jump here with centering_position already set to 0. */
11998
11999 #if GLYPH_DEBUG
12000 debug_method_add (w, "recenter");
12001 #endif
12002
12003 /* w->vscroll = 0; */
12004
12005 /* Forget any previously recorded base line for line number display. */
12006 if (!buffer_unchanged_p)
12007 w->base_line_number = Qnil;
12008
12009 /* Move backward half the height of the window. */
12010 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12011 it.current_y = it.last_visible_y;
12012 move_it_vertically_backward (&it, centering_position);
12013 xassert (IT_CHARPOS (it) >= BEGV);
12014
12015 /* The function move_it_vertically_backward may move over more
12016 than the specified y-distance. If it->w is small, e.g. a
12017 mini-buffer window, we may end up in front of the window's
12018 display area. Start displaying at the start of the line
12019 containing PT in this case. */
12020 if (it.current_y <= 0)
12021 {
12022 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12023 move_it_vertically (&it, 0);
12024 xassert (IT_CHARPOS (it) <= PT);
12025 it.current_y = 0;
12026 }
12027
12028 it.current_x = it.hpos = 0;
12029
12030 /* Set startp here explicitly in case that helps avoid an infinite loop
12031 in case the window-scroll-functions functions get errors. */
12032 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12033
12034 /* Run scroll hooks. */
12035 startp = run_window_scroll_functions (window, it.current.pos);
12036
12037 /* Redisplay the window. */
12038 if (!current_matrix_up_to_date_p
12039 || windows_or_buffers_changed
12040 || cursor_type_changed
12041 /* Don't use try_window_reusing_current_matrix in this case
12042 because it can have changed the buffer. */
12043 || !NILP (Vwindow_scroll_functions)
12044 || !just_this_one_p
12045 || MINI_WINDOW_P (w)
12046 || !(used_current_matrix_p
12047 = try_window_reusing_current_matrix (w)))
12048 try_window (window, startp);
12049
12050 /* If new fonts have been loaded (due to fontsets), give up. We
12051 have to start a new redisplay since we need to re-adjust glyph
12052 matrices. */
12053 if (fonts_changed_p)
12054 goto need_larger_matrices;
12055
12056 /* If cursor did not appear assume that the middle of the window is
12057 in the first line of the window. Do it again with the next line.
12058 (Imagine a window of height 100, displaying two lines of height
12059 60. Moving back 50 from it->last_visible_y will end in the first
12060 line.) */
12061 if (w->cursor.vpos < 0)
12062 {
12063 if (!NILP (w->window_end_valid)
12064 && PT >= Z - XFASTINT (w->window_end_pos))
12065 {
12066 clear_glyph_matrix (w->desired_matrix);
12067 move_it_by_lines (&it, 1, 0);
12068 try_window (window, it.current.pos);
12069 }
12070 else if (PT < IT_CHARPOS (it))
12071 {
12072 clear_glyph_matrix (w->desired_matrix);
12073 move_it_by_lines (&it, -1, 0);
12074 try_window (window, it.current.pos);
12075 }
12076 else
12077 {
12078 /* Not much we can do about it. */
12079 }
12080 }
12081
12082 /* Consider the following case: Window starts at BEGV, there is
12083 invisible, intangible text at BEGV, so that display starts at
12084 some point START > BEGV. It can happen that we are called with
12085 PT somewhere between BEGV and START. Try to handle that case. */
12086 if (w->cursor.vpos < 0)
12087 {
12088 struct glyph_row *row = w->current_matrix->rows;
12089 if (row->mode_line_p)
12090 ++row;
12091 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12092 }
12093
12094 if (!make_cursor_line_fully_visible (w, centering_position > 0))
12095 {
12096 /* If vscroll is enabled, disable it and try again. */
12097 if (w->vscroll)
12098 {
12099 w->vscroll = 0;
12100 clear_glyph_matrix (w->desired_matrix);
12101 goto recenter;
12102 }
12103
12104 /* If centering point failed to make the whole line visible,
12105 put point at the top instead. That has to make the whole line
12106 visible, if it can be done. */
12107 clear_glyph_matrix (w->desired_matrix);
12108 centering_position = 0;
12109 goto point_at_top;
12110 }
12111
12112 done:
12113
12114 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12115 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12116 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12117 ? Qt : Qnil);
12118
12119 /* Display the mode line, if we must. */
12120 if ((update_mode_line
12121 /* If window not full width, must redo its mode line
12122 if (a) the window to its side is being redone and
12123 (b) we do a frame-based redisplay. This is a consequence
12124 of how inverted lines are drawn in frame-based redisplay. */
12125 || (!just_this_one_p
12126 && !FRAME_WINDOW_P (f)
12127 && !WINDOW_FULL_WIDTH_P (w))
12128 /* Line number to display. */
12129 || INTEGERP (w->base_line_pos)
12130 /* Column number is displayed and different from the one displayed. */
12131 || (!NILP (w->column_number_displayed)
12132 && (XFASTINT (w->column_number_displayed)
12133 != (int) current_column ()))) /* iftc */
12134 /* This means that the window has a mode line. */
12135 && (WINDOW_WANTS_MODELINE_P (w)
12136 || WINDOW_WANTS_HEADER_LINE_P (w)))
12137 {
12138 display_mode_lines (w);
12139
12140 /* If mode line height has changed, arrange for a thorough
12141 immediate redisplay using the correct mode line height. */
12142 if (WINDOW_WANTS_MODELINE_P (w)
12143 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12144 {
12145 fonts_changed_p = 1;
12146 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12147 = DESIRED_MODE_LINE_HEIGHT (w);
12148 }
12149
12150 /* If top line height has changed, arrange for a thorough
12151 immediate redisplay using the correct mode line height. */
12152 if (WINDOW_WANTS_HEADER_LINE_P (w)
12153 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12154 {
12155 fonts_changed_p = 1;
12156 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12157 = DESIRED_HEADER_LINE_HEIGHT (w);
12158 }
12159
12160 if (fonts_changed_p)
12161 goto need_larger_matrices;
12162 }
12163
12164 if (!line_number_displayed
12165 && !BUFFERP (w->base_line_pos))
12166 {
12167 w->base_line_pos = Qnil;
12168 w->base_line_number = Qnil;
12169 }
12170
12171 finish_menu_bars:
12172
12173 /* When we reach a frame's selected window, redo the frame's menu bar. */
12174 if (update_mode_line
12175 && EQ (FRAME_SELECTED_WINDOW (f), window))
12176 {
12177 int redisplay_menu_p = 0;
12178 int redisplay_tool_bar_p = 0;
12179
12180 if (FRAME_WINDOW_P (f))
12181 {
12182 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12183 || defined (USE_GTK)
12184 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12185 #else
12186 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12187 #endif
12188 }
12189 else
12190 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12191
12192 if (redisplay_menu_p)
12193 display_menu_bar (w);
12194
12195 #ifdef HAVE_WINDOW_SYSTEM
12196 #ifdef USE_GTK
12197 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12198 #else
12199 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12200 && (FRAME_TOOL_BAR_LINES (f) > 0
12201 || auto_resize_tool_bars_p);
12202
12203 #endif
12204
12205 if (redisplay_tool_bar_p)
12206 redisplay_tool_bar (f);
12207 #endif
12208 }
12209
12210 #ifdef HAVE_WINDOW_SYSTEM
12211 if (update_window_fringes (w, 0)
12212 && !just_this_one_p
12213 && (used_current_matrix_p || overlay_arrow_seen)
12214 && !w->pseudo_window_p)
12215 {
12216 update_begin (f);
12217 BLOCK_INPUT;
12218 draw_window_fringes (w);
12219 UNBLOCK_INPUT;
12220 update_end (f);
12221 }
12222 #endif /* HAVE_WINDOW_SYSTEM */
12223
12224 /* We go to this label, with fonts_changed_p nonzero,
12225 if it is necessary to try again using larger glyph matrices.
12226 We have to redeem the scroll bar even in this case,
12227 because the loop in redisplay_internal expects that. */
12228 need_larger_matrices:
12229 ;
12230 finish_scroll_bars:
12231
12232 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12233 {
12234 /* Set the thumb's position and size. */
12235 set_vertical_scroll_bar (w);
12236
12237 /* Note that we actually used the scroll bar attached to this
12238 window, so it shouldn't be deleted at the end of redisplay. */
12239 if (FRAME_DISPLAY (f)->redeem_scroll_bar_hook)
12240 (*FRAME_DISPLAY (f)->redeem_scroll_bar_hook) (w);
12241 }
12242
12243 /* Restore current_buffer and value of point in it. */
12244 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12245 set_buffer_internal_1 (old);
12246 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12247
12248 unbind_to (count, Qnil);
12249 }
12250
12251
12252 /* Build the complete desired matrix of WINDOW with a window start
12253 buffer position POS. Value is non-zero if successful. It is zero
12254 if fonts were loaded during redisplay which makes re-adjusting
12255 glyph matrices necessary. */
12256
12257 int
12258 try_window (window, pos)
12259 Lisp_Object window;
12260 struct text_pos pos;
12261 {
12262 struct window *w = XWINDOW (window);
12263 struct it it;
12264 struct glyph_row *last_text_row = NULL;
12265
12266 /* Make POS the new window start. */
12267 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12268
12269 /* Mark cursor position as unknown. No overlay arrow seen. */
12270 w->cursor.vpos = -1;
12271 overlay_arrow_seen = 0;
12272
12273 /* Initialize iterator and info to start at POS. */
12274 start_display (&it, w, pos);
12275
12276 /* Display all lines of W. */
12277 while (it.current_y < it.last_visible_y)
12278 {
12279 if (display_line (&it))
12280 last_text_row = it.glyph_row - 1;
12281 if (fonts_changed_p)
12282 return 0;
12283 }
12284
12285 /* If bottom moved off end of frame, change mode line percentage. */
12286 if (XFASTINT (w->window_end_pos) <= 0
12287 && Z != IT_CHARPOS (it))
12288 w->update_mode_line = Qt;
12289
12290 /* Set window_end_pos to the offset of the last character displayed
12291 on the window from the end of current_buffer. Set
12292 window_end_vpos to its row number. */
12293 if (last_text_row)
12294 {
12295 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12296 w->window_end_bytepos
12297 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12298 w->window_end_pos
12299 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12300 w->window_end_vpos
12301 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12302 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12303 ->displays_text_p);
12304 }
12305 else
12306 {
12307 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12308 w->window_end_pos = make_number (Z - ZV);
12309 w->window_end_vpos = make_number (0);
12310 }
12311
12312 /* But that is not valid info until redisplay finishes. */
12313 w->window_end_valid = Qnil;
12314 return 1;
12315 }
12316
12317
12318 \f
12319 /************************************************************************
12320 Window redisplay reusing current matrix when buffer has not changed
12321 ************************************************************************/
12322
12323 /* Try redisplay of window W showing an unchanged buffer with a
12324 different window start than the last time it was displayed by
12325 reusing its current matrix. Value is non-zero if successful.
12326 W->start is the new window start. */
12327
12328 static int
12329 try_window_reusing_current_matrix (w)
12330 struct window *w;
12331 {
12332 struct frame *f = XFRAME (w->frame);
12333 struct glyph_row *row, *bottom_row;
12334 struct it it;
12335 struct run run;
12336 struct text_pos start, new_start;
12337 int nrows_scrolled, i;
12338 struct glyph_row *last_text_row;
12339 struct glyph_row *last_reused_text_row;
12340 struct glyph_row *start_row;
12341 int start_vpos, min_y, max_y;
12342
12343 #if GLYPH_DEBUG
12344 if (inhibit_try_window_reusing)
12345 return 0;
12346 #endif
12347
12348 if (/* This function doesn't handle terminal frames. */
12349 !FRAME_WINDOW_P (f)
12350 /* Don't try to reuse the display if windows have been split
12351 or such. */
12352 || windows_or_buffers_changed
12353 || cursor_type_changed)
12354 return 0;
12355
12356 /* Can't do this if region may have changed. */
12357 if ((!NILP (Vtransient_mark_mode)
12358 && !NILP (current_buffer->mark_active))
12359 || !NILP (w->region_showing)
12360 || !NILP (Vshow_trailing_whitespace))
12361 return 0;
12362
12363 /* If top-line visibility has changed, give up. */
12364 if (WINDOW_WANTS_HEADER_LINE_P (w)
12365 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12366 return 0;
12367
12368 /* Give up if old or new display is scrolled vertically. We could
12369 make this function handle this, but right now it doesn't. */
12370 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12371 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
12372 return 0;
12373
12374 /* The variable new_start now holds the new window start. The old
12375 start `start' can be determined from the current matrix. */
12376 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12377 start = start_row->start.pos;
12378 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12379
12380 /* Clear the desired matrix for the display below. */
12381 clear_glyph_matrix (w->desired_matrix);
12382
12383 if (CHARPOS (new_start) <= CHARPOS (start))
12384 {
12385 int first_row_y;
12386
12387 /* Don't use this method if the display starts with an ellipsis
12388 displayed for invisible text. It's not easy to handle that case
12389 below, and it's certainly not worth the effort since this is
12390 not a frequent case. */
12391 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12392 return 0;
12393
12394 IF_DEBUG (debug_method_add (w, "twu1"));
12395
12396 /* Display up to a row that can be reused. The variable
12397 last_text_row is set to the last row displayed that displays
12398 text. Note that it.vpos == 0 if or if not there is a
12399 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12400 start_display (&it, w, new_start);
12401 first_row_y = it.current_y;
12402 w->cursor.vpos = -1;
12403 last_text_row = last_reused_text_row = NULL;
12404
12405 while (it.current_y < it.last_visible_y
12406 && !fonts_changed_p)
12407 {
12408 /* If we have reached into the characters in the START row,
12409 that means the line boundaries have changed. So we
12410 can't start copying with the row START. Maybe it will
12411 work to start copying with the following row. */
12412 while (IT_CHARPOS (it) > CHARPOS (start))
12413 {
12414 /* Advance to the next row as the "start". */
12415 start_row++;
12416 start = start_row->start.pos;
12417 /* If there are no more rows to try, or just one, give up. */
12418 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
12419 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row)
12420 || CHARPOS (start) == ZV)
12421 {
12422 clear_glyph_matrix (w->desired_matrix);
12423 return 0;
12424 }
12425
12426 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12427 }
12428 /* If we have reached alignment,
12429 we can copy the rest of the rows. */
12430 if (IT_CHARPOS (it) == CHARPOS (start))
12431 break;
12432
12433 if (display_line (&it))
12434 last_text_row = it.glyph_row - 1;
12435 }
12436
12437 /* A value of current_y < last_visible_y means that we stopped
12438 at the previous window start, which in turn means that we
12439 have at least one reusable row. */
12440 if (it.current_y < it.last_visible_y)
12441 {
12442 /* IT.vpos always starts from 0; it counts text lines. */
12443 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
12444
12445 /* Find PT if not already found in the lines displayed. */
12446 if (w->cursor.vpos < 0)
12447 {
12448 int dy = it.current_y - start_row->y;
12449
12450 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12451 row = row_containing_pos (w, PT, row, NULL, dy);
12452 if (row)
12453 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12454 dy, nrows_scrolled);
12455 else
12456 {
12457 clear_glyph_matrix (w->desired_matrix);
12458 return 0;
12459 }
12460 }
12461
12462 /* Scroll the display. Do it before the current matrix is
12463 changed. The problem here is that update has not yet
12464 run, i.e. part of the current matrix is not up to date.
12465 scroll_run_hook will clear the cursor, and use the
12466 current matrix to get the height of the row the cursor is
12467 in. */
12468 run.current_y = start_row->y;
12469 run.desired_y = it.current_y;
12470 run.height = it.last_visible_y - it.current_y;
12471
12472 if (run.height > 0 && run.current_y != run.desired_y)
12473 {
12474 update_begin (f);
12475 FRAME_RIF (f)->update_window_begin_hook (w);
12476 FRAME_RIF (f)->clear_window_mouse_face (w);
12477 FRAME_RIF (f)->scroll_run_hook (w, &run);
12478 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
12479 update_end (f);
12480 }
12481
12482 /* Shift current matrix down by nrows_scrolled lines. */
12483 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12484 rotate_matrix (w->current_matrix,
12485 start_vpos,
12486 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12487 nrows_scrolled);
12488
12489 /* Disable lines that must be updated. */
12490 for (i = 0; i < it.vpos; ++i)
12491 (start_row + i)->enabled_p = 0;
12492
12493 /* Re-compute Y positions. */
12494 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12495 max_y = it.last_visible_y;
12496 for (row = start_row + nrows_scrolled;
12497 row < bottom_row;
12498 ++row)
12499 {
12500 row->y = it.current_y;
12501 row->visible_height = row->height;
12502
12503 if (row->y < min_y)
12504 row->visible_height -= min_y - row->y;
12505 if (row->y + row->height > max_y)
12506 row->visible_height -= row->y + row->height - max_y;
12507 row->redraw_fringe_bitmaps_p = 1;
12508
12509 it.current_y += row->height;
12510
12511 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12512 last_reused_text_row = row;
12513 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12514 break;
12515 }
12516
12517 /* Disable lines in the current matrix which are now
12518 below the window. */
12519 for (++row; row < bottom_row; ++row)
12520 row->enabled_p = 0;
12521 }
12522
12523 /* Update window_end_pos etc.; last_reused_text_row is the last
12524 reused row from the current matrix containing text, if any.
12525 The value of last_text_row is the last displayed line
12526 containing text. */
12527 if (last_reused_text_row)
12528 {
12529 w->window_end_bytepos
12530 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12531 w->window_end_pos
12532 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12533 w->window_end_vpos
12534 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12535 w->current_matrix));
12536 }
12537 else if (last_text_row)
12538 {
12539 w->window_end_bytepos
12540 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12541 w->window_end_pos
12542 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12543 w->window_end_vpos
12544 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12545 }
12546 else
12547 {
12548 /* This window must be completely empty. */
12549 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12550 w->window_end_pos = make_number (Z - ZV);
12551 w->window_end_vpos = make_number (0);
12552 }
12553 w->window_end_valid = Qnil;
12554
12555 /* Update hint: don't try scrolling again in update_window. */
12556 w->desired_matrix->no_scrolling_p = 1;
12557
12558 #if GLYPH_DEBUG
12559 debug_method_add (w, "try_window_reusing_current_matrix 1");
12560 #endif
12561 return 1;
12562 }
12563 else if (CHARPOS (new_start) > CHARPOS (start))
12564 {
12565 struct glyph_row *pt_row, *row;
12566 struct glyph_row *first_reusable_row;
12567 struct glyph_row *first_row_to_display;
12568 int dy;
12569 int yb = window_text_bottom_y (w);
12570
12571 /* Find the row starting at new_start, if there is one. Don't
12572 reuse a partially visible line at the end. */
12573 first_reusable_row = start_row;
12574 while (first_reusable_row->enabled_p
12575 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12576 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12577 < CHARPOS (new_start)))
12578 ++first_reusable_row;
12579
12580 /* Give up if there is no row to reuse. */
12581 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
12582 || !first_reusable_row->enabled_p
12583 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12584 != CHARPOS (new_start)))
12585 return 0;
12586
12587 /* We can reuse fully visible rows beginning with
12588 first_reusable_row to the end of the window. Set
12589 first_row_to_display to the first row that cannot be reused.
12590 Set pt_row to the row containing point, if there is any. */
12591 pt_row = NULL;
12592 for (first_row_to_display = first_reusable_row;
12593 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12594 ++first_row_to_display)
12595 {
12596 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12597 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
12598 pt_row = first_row_to_display;
12599 }
12600
12601 /* Start displaying at the start of first_row_to_display. */
12602 xassert (first_row_to_display->y < yb);
12603 init_to_row_start (&it, w, first_row_to_display);
12604
12605 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12606 - start_vpos);
12607 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12608 - nrows_scrolled);
12609 it.current_y = (first_row_to_display->y - first_reusable_row->y
12610 + WINDOW_HEADER_LINE_HEIGHT (w));
12611
12612 /* Display lines beginning with first_row_to_display in the
12613 desired matrix. Set last_text_row to the last row displayed
12614 that displays text. */
12615 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12616 if (pt_row == NULL)
12617 w->cursor.vpos = -1;
12618 last_text_row = NULL;
12619 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12620 if (display_line (&it))
12621 last_text_row = it.glyph_row - 1;
12622
12623 /* Give up If point isn't in a row displayed or reused. */
12624 if (w->cursor.vpos < 0)
12625 {
12626 clear_glyph_matrix (w->desired_matrix);
12627 return 0;
12628 }
12629
12630 /* If point is in a reused row, adjust y and vpos of the cursor
12631 position. */
12632 if (pt_row)
12633 {
12634 w->cursor.vpos -= nrows_scrolled;
12635 w->cursor.y -= first_reusable_row->y - start_row->y;
12636 }
12637
12638 /* Scroll the display. */
12639 run.current_y = first_reusable_row->y;
12640 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
12641 run.height = it.last_visible_y - run.current_y;
12642 dy = run.current_y - run.desired_y;
12643
12644 if (run.height)
12645 {
12646 update_begin (f);
12647 FRAME_RIF (f)->update_window_begin_hook (w);
12648 FRAME_RIF (f)->clear_window_mouse_face (w);
12649 FRAME_RIF (f)->scroll_run_hook (w, &run);
12650 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
12651 update_end (f);
12652 }
12653
12654 /* Adjust Y positions of reused rows. */
12655 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12656 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12657 max_y = it.last_visible_y;
12658 for (row = first_reusable_row; row < first_row_to_display; ++row)
12659 {
12660 row->y -= dy;
12661 row->visible_height = row->height;
12662 if (row->y < min_y)
12663 row->visible_height -= min_y - row->y;
12664 if (row->y + row->height > max_y)
12665 row->visible_height -= row->y + row->height - max_y;
12666 row->redraw_fringe_bitmaps_p = 1;
12667 }
12668
12669 /* Scroll the current matrix. */
12670 xassert (nrows_scrolled > 0);
12671 rotate_matrix (w->current_matrix,
12672 start_vpos,
12673 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12674 -nrows_scrolled);
12675
12676 /* Disable rows not reused. */
12677 for (row -= nrows_scrolled; row < bottom_row; ++row)
12678 row->enabled_p = 0;
12679
12680 /* Point may have moved to a different line, so we cannot assume that
12681 the previous cursor position is valid; locate the correct row. */
12682 if (pt_row)
12683 {
12684 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12685 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
12686 row++)
12687 {
12688 w->cursor.vpos++;
12689 w->cursor.y = row->y;
12690 }
12691 if (row < bottom_row)
12692 {
12693 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
12694 while (glyph->charpos < PT)
12695 {
12696 w->cursor.hpos++;
12697 w->cursor.x += glyph->pixel_width;
12698 glyph++;
12699 }
12700 }
12701 }
12702
12703 /* Adjust window end. A null value of last_text_row means that
12704 the window end is in reused rows which in turn means that
12705 only its vpos can have changed. */
12706 if (last_text_row)
12707 {
12708 w->window_end_bytepos
12709 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12710 w->window_end_pos
12711 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12712 w->window_end_vpos
12713 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12714 }
12715 else
12716 {
12717 w->window_end_vpos
12718 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
12719 }
12720
12721 w->window_end_valid = Qnil;
12722 w->desired_matrix->no_scrolling_p = 1;
12723
12724 #if GLYPH_DEBUG
12725 debug_method_add (w, "try_window_reusing_current_matrix 2");
12726 #endif
12727 return 1;
12728 }
12729
12730 return 0;
12731 }
12732
12733
12734 \f
12735 /************************************************************************
12736 Window redisplay reusing current matrix when buffer has changed
12737 ************************************************************************/
12738
12739 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
12740 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
12741 int *, int *));
12742 static struct glyph_row *
12743 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
12744 struct glyph_row *));
12745
12746
12747 /* Return the last row in MATRIX displaying text. If row START is
12748 non-null, start searching with that row. IT gives the dimensions
12749 of the display. Value is null if matrix is empty; otherwise it is
12750 a pointer to the row found. */
12751
12752 static struct glyph_row *
12753 find_last_row_displaying_text (matrix, it, start)
12754 struct glyph_matrix *matrix;
12755 struct it *it;
12756 struct glyph_row *start;
12757 {
12758 struct glyph_row *row, *row_found;
12759
12760 /* Set row_found to the last row in IT->w's current matrix
12761 displaying text. The loop looks funny but think of partially
12762 visible lines. */
12763 row_found = NULL;
12764 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
12765 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12766 {
12767 xassert (row->enabled_p);
12768 row_found = row;
12769 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
12770 break;
12771 ++row;
12772 }
12773
12774 return row_found;
12775 }
12776
12777
12778 /* Return the last row in the current matrix of W that is not affected
12779 by changes at the start of current_buffer that occurred since W's
12780 current matrix was built. Value is null if no such row exists.
12781
12782 BEG_UNCHANGED us the number of characters unchanged at the start of
12783 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
12784 first changed character in current_buffer. Characters at positions <
12785 BEG + BEG_UNCHANGED are at the same buffer positions as they were
12786 when the current matrix was built. */
12787
12788 static struct glyph_row *
12789 find_last_unchanged_at_beg_row (w)
12790 struct window *w;
12791 {
12792 int first_changed_pos = BEG + BEG_UNCHANGED;
12793 struct glyph_row *row;
12794 struct glyph_row *row_found = NULL;
12795 int yb = window_text_bottom_y (w);
12796
12797 /* Find the last row displaying unchanged text. */
12798 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12799 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12800 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
12801 {
12802 if (/* If row ends before first_changed_pos, it is unchanged,
12803 except in some case. */
12804 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
12805 /* When row ends in ZV and we write at ZV it is not
12806 unchanged. */
12807 && !row->ends_at_zv_p
12808 /* When first_changed_pos is the end of a continued line,
12809 row is not unchanged because it may be no longer
12810 continued. */
12811 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
12812 && (row->continued_p
12813 || row->exact_window_width_line_p)))
12814 row_found = row;
12815
12816 /* Stop if last visible row. */
12817 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
12818 break;
12819
12820 ++row;
12821 }
12822
12823 return row_found;
12824 }
12825
12826
12827 /* Find the first glyph row in the current matrix of W that is not
12828 affected by changes at the end of current_buffer since the
12829 time W's current matrix was built.
12830
12831 Return in *DELTA the number of chars by which buffer positions in
12832 unchanged text at the end of current_buffer must be adjusted.
12833
12834 Return in *DELTA_BYTES the corresponding number of bytes.
12835
12836 Value is null if no such row exists, i.e. all rows are affected by
12837 changes. */
12838
12839 static struct glyph_row *
12840 find_first_unchanged_at_end_row (w, delta, delta_bytes)
12841 struct window *w;
12842 int *delta, *delta_bytes;
12843 {
12844 struct glyph_row *row;
12845 struct glyph_row *row_found = NULL;
12846
12847 *delta = *delta_bytes = 0;
12848
12849 /* Display must not have been paused, otherwise the current matrix
12850 is not up to date. */
12851 if (NILP (w->window_end_valid))
12852 abort ();
12853
12854 /* A value of window_end_pos >= END_UNCHANGED means that the window
12855 end is in the range of changed text. If so, there is no
12856 unchanged row at the end of W's current matrix. */
12857 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
12858 return NULL;
12859
12860 /* Set row to the last row in W's current matrix displaying text. */
12861 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12862
12863 /* If matrix is entirely empty, no unchanged row exists. */
12864 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12865 {
12866 /* The value of row is the last glyph row in the matrix having a
12867 meaningful buffer position in it. The end position of row
12868 corresponds to window_end_pos. This allows us to translate
12869 buffer positions in the current matrix to current buffer
12870 positions for characters not in changed text. */
12871 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12872 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12873 int last_unchanged_pos, last_unchanged_pos_old;
12874 struct glyph_row *first_text_row
12875 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12876
12877 *delta = Z - Z_old;
12878 *delta_bytes = Z_BYTE - Z_BYTE_old;
12879
12880 /* Set last_unchanged_pos to the buffer position of the last
12881 character in the buffer that has not been changed. Z is the
12882 index + 1 of the last character in current_buffer, i.e. by
12883 subtracting END_UNCHANGED we get the index of the last
12884 unchanged character, and we have to add BEG to get its buffer
12885 position. */
12886 last_unchanged_pos = Z - END_UNCHANGED + BEG;
12887 last_unchanged_pos_old = last_unchanged_pos - *delta;
12888
12889 /* Search backward from ROW for a row displaying a line that
12890 starts at a minimum position >= last_unchanged_pos_old. */
12891 for (; row > first_text_row; --row)
12892 {
12893 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
12894 abort ();
12895
12896 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
12897 row_found = row;
12898 }
12899 }
12900
12901 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
12902 abort ();
12903
12904 return row_found;
12905 }
12906
12907
12908 /* Make sure that glyph rows in the current matrix of window W
12909 reference the same glyph memory as corresponding rows in the
12910 frame's frame matrix. This function is called after scrolling W's
12911 current matrix on a terminal frame in try_window_id and
12912 try_window_reusing_current_matrix. */
12913
12914 static void
12915 sync_frame_with_window_matrix_rows (w)
12916 struct window *w;
12917 {
12918 struct frame *f = XFRAME (w->frame);
12919 struct glyph_row *window_row, *window_row_end, *frame_row;
12920
12921 /* Preconditions: W must be a leaf window and full-width. Its frame
12922 must have a frame matrix. */
12923 xassert (NILP (w->hchild) && NILP (w->vchild));
12924 xassert (WINDOW_FULL_WIDTH_P (w));
12925 xassert (!FRAME_WINDOW_P (f));
12926
12927 /* If W is a full-width window, glyph pointers in W's current matrix
12928 have, by definition, to be the same as glyph pointers in the
12929 corresponding frame matrix. Note that frame matrices have no
12930 marginal areas (see build_frame_matrix). */
12931 window_row = w->current_matrix->rows;
12932 window_row_end = window_row + w->current_matrix->nrows;
12933 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
12934 while (window_row < window_row_end)
12935 {
12936 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
12937 struct glyph *end = window_row->glyphs[LAST_AREA];
12938
12939 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
12940 frame_row->glyphs[TEXT_AREA] = start;
12941 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
12942 frame_row->glyphs[LAST_AREA] = end;
12943
12944 /* Disable frame rows whose corresponding window rows have
12945 been disabled in try_window_id. */
12946 if (!window_row->enabled_p)
12947 frame_row->enabled_p = 0;
12948
12949 ++window_row, ++frame_row;
12950 }
12951 }
12952
12953
12954 /* Find the glyph row in window W containing CHARPOS. Consider all
12955 rows between START and END (not inclusive). END null means search
12956 all rows to the end of the display area of W. Value is the row
12957 containing CHARPOS or null. */
12958
12959 struct glyph_row *
12960 row_containing_pos (w, charpos, start, end, dy)
12961 struct window *w;
12962 int charpos;
12963 struct glyph_row *start, *end;
12964 int dy;
12965 {
12966 struct glyph_row *row = start;
12967 int last_y;
12968
12969 /* If we happen to start on a header-line, skip that. */
12970 if (row->mode_line_p)
12971 ++row;
12972
12973 if ((end && row >= end) || !row->enabled_p)
12974 return NULL;
12975
12976 last_y = window_text_bottom_y (w) - dy;
12977
12978 while (1)
12979 {
12980 /* Give up if we have gone too far. */
12981 if (end && row >= end)
12982 return NULL;
12983 /* This formerly returned if they were equal.
12984 I think that both quantities are of a "last plus one" type;
12985 if so, when they are equal, the row is within the screen. -- rms. */
12986 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
12987 return NULL;
12988
12989 /* If it is in this row, return this row. */
12990 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
12991 || (MATRIX_ROW_END_CHARPOS (row) == charpos
12992 /* The end position of a row equals the start
12993 position of the next row. If CHARPOS is there, we
12994 would rather display it in the next line, except
12995 when this line ends in ZV. */
12996 && !row->ends_at_zv_p
12997 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12998 && charpos >= MATRIX_ROW_START_CHARPOS (row))
12999 return row;
13000 ++row;
13001 }
13002 }
13003
13004
13005 /* Try to redisplay window W by reusing its existing display. W's
13006 current matrix must be up to date when this function is called,
13007 i.e. window_end_valid must not be nil.
13008
13009 Value is
13010
13011 1 if display has been updated
13012 0 if otherwise unsuccessful
13013 -1 if redisplay with same window start is known not to succeed
13014
13015 The following steps are performed:
13016
13017 1. Find the last row in the current matrix of W that is not
13018 affected by changes at the start of current_buffer. If no such row
13019 is found, give up.
13020
13021 2. Find the first row in W's current matrix that is not affected by
13022 changes at the end of current_buffer. Maybe there is no such row.
13023
13024 3. Display lines beginning with the row + 1 found in step 1 to the
13025 row found in step 2 or, if step 2 didn't find a row, to the end of
13026 the window.
13027
13028 4. If cursor is not known to appear on the window, give up.
13029
13030 5. If display stopped at the row found in step 2, scroll the
13031 display and current matrix as needed.
13032
13033 6. Maybe display some lines at the end of W, if we must. This can
13034 happen under various circumstances, like a partially visible line
13035 becoming fully visible, or because newly displayed lines are displayed
13036 in smaller font sizes.
13037
13038 7. Update W's window end information. */
13039
13040 static int
13041 try_window_id (w)
13042 struct window *w;
13043 {
13044 struct frame *f = XFRAME (w->frame);
13045 struct glyph_matrix *current_matrix = w->current_matrix;
13046 struct glyph_matrix *desired_matrix = w->desired_matrix;
13047 struct glyph_row *last_unchanged_at_beg_row;
13048 struct glyph_row *first_unchanged_at_end_row;
13049 struct glyph_row *row;
13050 struct glyph_row *bottom_row;
13051 int bottom_vpos;
13052 struct it it;
13053 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13054 struct text_pos start_pos;
13055 struct run run;
13056 int first_unchanged_at_end_vpos = 0;
13057 struct glyph_row *last_text_row, *last_text_row_at_end;
13058 struct text_pos start;
13059 int first_changed_charpos, last_changed_charpos;
13060
13061 #if GLYPH_DEBUG
13062 if (inhibit_try_window_id)
13063 return 0;
13064 #endif
13065
13066 /* This is handy for debugging. */
13067 #if 0
13068 #define GIVE_UP(X) \
13069 do { \
13070 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13071 return 0; \
13072 } while (0)
13073 #else
13074 #define GIVE_UP(X) return 0
13075 #endif
13076
13077 SET_TEXT_POS_FROM_MARKER (start, w->start);
13078
13079 /* Don't use this for mini-windows because these can show
13080 messages and mini-buffers, and we don't handle that here. */
13081 if (MINI_WINDOW_P (w))
13082 GIVE_UP (1);
13083
13084 /* This flag is used to prevent redisplay optimizations. */
13085 if (windows_or_buffers_changed || cursor_type_changed)
13086 GIVE_UP (2);
13087
13088 /* Verify that narrowing has not changed.
13089 Also verify that we were not told to prevent redisplay optimizations.
13090 It would be nice to further
13091 reduce the number of cases where this prevents try_window_id. */
13092 if (current_buffer->clip_changed
13093 || current_buffer->prevent_redisplay_optimizations_p)
13094 GIVE_UP (3);
13095
13096 /* Window must either use window-based redisplay or be full width. */
13097 if (!FRAME_WINDOW_P (f)
13098 && (!FRAME_LINE_INS_DEL_OK (f)
13099 || !WINDOW_FULL_WIDTH_P (w)))
13100 GIVE_UP (4);
13101
13102 /* Give up if point is not known NOT to appear in W. */
13103 if (PT < CHARPOS (start))
13104 GIVE_UP (5);
13105
13106 /* Another way to prevent redisplay optimizations. */
13107 if (XFASTINT (w->last_modified) == 0)
13108 GIVE_UP (6);
13109
13110 /* Verify that window is not hscrolled. */
13111 if (XFASTINT (w->hscroll) != 0)
13112 GIVE_UP (7);
13113
13114 /* Verify that display wasn't paused. */
13115 if (NILP (w->window_end_valid))
13116 GIVE_UP (8);
13117
13118 /* Can't use this if highlighting a region because a cursor movement
13119 will do more than just set the cursor. */
13120 if (!NILP (Vtransient_mark_mode)
13121 && !NILP (current_buffer->mark_active))
13122 GIVE_UP (9);
13123
13124 /* Likewise if highlighting trailing whitespace. */
13125 if (!NILP (Vshow_trailing_whitespace))
13126 GIVE_UP (11);
13127
13128 /* Likewise if showing a region. */
13129 if (!NILP (w->region_showing))
13130 GIVE_UP (10);
13131
13132 /* Can use this if overlay arrow position and or string have changed. */
13133 if (overlay_arrows_changed_p ())
13134 GIVE_UP (12);
13135
13136
13137 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13138 only if buffer has really changed. The reason is that the gap is
13139 initially at Z for freshly visited files. The code below would
13140 set end_unchanged to 0 in that case. */
13141 if (MODIFF > SAVE_MODIFF
13142 /* This seems to happen sometimes after saving a buffer. */
13143 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13144 {
13145 if (GPT - BEG < BEG_UNCHANGED)
13146 BEG_UNCHANGED = GPT - BEG;
13147 if (Z - GPT < END_UNCHANGED)
13148 END_UNCHANGED = Z - GPT;
13149 }
13150
13151 /* The position of the first and last character that has been changed. */
13152 first_changed_charpos = BEG + BEG_UNCHANGED;
13153 last_changed_charpos = Z - END_UNCHANGED;
13154
13155 /* If window starts after a line end, and the last change is in
13156 front of that newline, then changes don't affect the display.
13157 This case happens with stealth-fontification. Note that although
13158 the display is unchanged, glyph positions in the matrix have to
13159 be adjusted, of course. */
13160 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13161 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13162 && ((last_changed_charpos < CHARPOS (start)
13163 && CHARPOS (start) == BEGV)
13164 || (last_changed_charpos < CHARPOS (start) - 1
13165 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13166 {
13167 int Z_old, delta, Z_BYTE_old, delta_bytes;
13168 struct glyph_row *r0;
13169
13170 /* Compute how many chars/bytes have been added to or removed
13171 from the buffer. */
13172 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13173 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13174 delta = Z - Z_old;
13175 delta_bytes = Z_BYTE - Z_BYTE_old;
13176
13177 /* Give up if PT is not in the window. Note that it already has
13178 been checked at the start of try_window_id that PT is not in
13179 front of the window start. */
13180 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13181 GIVE_UP (13);
13182
13183 /* If window start is unchanged, we can reuse the whole matrix
13184 as is, after adjusting glyph positions. No need to compute
13185 the window end again, since its offset from Z hasn't changed. */
13186 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13187 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13188 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13189 /* PT must not be in a partially visible line. */
13190 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13191 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13192 {
13193 /* Adjust positions in the glyph matrix. */
13194 if (delta || delta_bytes)
13195 {
13196 struct glyph_row *r1
13197 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13198 increment_matrix_positions (w->current_matrix,
13199 MATRIX_ROW_VPOS (r0, current_matrix),
13200 MATRIX_ROW_VPOS (r1, current_matrix),
13201 delta, delta_bytes);
13202 }
13203
13204 /* Set the cursor. */
13205 row = row_containing_pos (w, PT, r0, NULL, 0);
13206 if (row)
13207 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13208 else
13209 abort ();
13210 return 1;
13211 }
13212 }
13213
13214 /* Handle the case that changes are all below what is displayed in
13215 the window, and that PT is in the window. This shortcut cannot
13216 be taken if ZV is visible in the window, and text has been added
13217 there that is visible in the window. */
13218 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13219 /* ZV is not visible in the window, or there are no
13220 changes at ZV, actually. */
13221 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13222 || first_changed_charpos == last_changed_charpos))
13223 {
13224 struct glyph_row *r0;
13225
13226 /* Give up if PT is not in the window. Note that it already has
13227 been checked at the start of try_window_id that PT is not in
13228 front of the window start. */
13229 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13230 GIVE_UP (14);
13231
13232 /* If window start is unchanged, we can reuse the whole matrix
13233 as is, without changing glyph positions since no text has
13234 been added/removed in front of the window end. */
13235 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13236 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13237 /* PT must not be in a partially visible line. */
13238 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13239 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13240 {
13241 /* We have to compute the window end anew since text
13242 can have been added/removed after it. */
13243 w->window_end_pos
13244 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13245 w->window_end_bytepos
13246 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13247
13248 /* Set the cursor. */
13249 row = row_containing_pos (w, PT, r0, NULL, 0);
13250 if (row)
13251 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13252 else
13253 abort ();
13254 return 2;
13255 }
13256 }
13257
13258 /* Give up if window start is in the changed area.
13259
13260 The condition used to read
13261
13262 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13263
13264 but why that was tested escapes me at the moment. */
13265 if (CHARPOS (start) >= first_changed_charpos
13266 && CHARPOS (start) <= last_changed_charpos)
13267 GIVE_UP (15);
13268
13269 /* Check that window start agrees with the start of the first glyph
13270 row in its current matrix. Check this after we know the window
13271 start is not in changed text, otherwise positions would not be
13272 comparable. */
13273 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13274 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13275 GIVE_UP (16);
13276
13277 /* Give up if the window ends in strings. Overlay strings
13278 at the end are difficult to handle, so don't try. */
13279 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13280 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13281 GIVE_UP (20);
13282
13283 /* Compute the position at which we have to start displaying new
13284 lines. Some of the lines at the top of the window might be
13285 reusable because they are not displaying changed text. Find the
13286 last row in W's current matrix not affected by changes at the
13287 start of current_buffer. Value is null if changes start in the
13288 first line of window. */
13289 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13290 if (last_unchanged_at_beg_row)
13291 {
13292 /* Avoid starting to display in the moddle of a character, a TAB
13293 for instance. This is easier than to set up the iterator
13294 exactly, and it's not a frequent case, so the additional
13295 effort wouldn't really pay off. */
13296 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13297 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13298 && last_unchanged_at_beg_row > w->current_matrix->rows)
13299 --last_unchanged_at_beg_row;
13300
13301 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13302 GIVE_UP (17);
13303
13304 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13305 GIVE_UP (18);
13306 start_pos = it.current.pos;
13307
13308 /* Start displaying new lines in the desired matrix at the same
13309 vpos we would use in the current matrix, i.e. below
13310 last_unchanged_at_beg_row. */
13311 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13312 current_matrix);
13313 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13314 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13315
13316 xassert (it.hpos == 0 && it.current_x == 0);
13317 }
13318 else
13319 {
13320 /* There are no reusable lines at the start of the window.
13321 Start displaying in the first text line. */
13322 start_display (&it, w, start);
13323 it.vpos = it.first_vpos;
13324 start_pos = it.current.pos;
13325 }
13326
13327 /* Find the first row that is not affected by changes at the end of
13328 the buffer. Value will be null if there is no unchanged row, in
13329 which case we must redisplay to the end of the window. delta
13330 will be set to the value by which buffer positions beginning with
13331 first_unchanged_at_end_row have to be adjusted due to text
13332 changes. */
13333 first_unchanged_at_end_row
13334 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13335 IF_DEBUG (debug_delta = delta);
13336 IF_DEBUG (debug_delta_bytes = delta_bytes);
13337
13338 /* Set stop_pos to the buffer position up to which we will have to
13339 display new lines. If first_unchanged_at_end_row != NULL, this
13340 is the buffer position of the start of the line displayed in that
13341 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13342 that we don't stop at a buffer position. */
13343 stop_pos = 0;
13344 if (first_unchanged_at_end_row)
13345 {
13346 xassert (last_unchanged_at_beg_row == NULL
13347 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13348
13349 /* If this is a continuation line, move forward to the next one
13350 that isn't. Changes in lines above affect this line.
13351 Caution: this may move first_unchanged_at_end_row to a row
13352 not displaying text. */
13353 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13354 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13355 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13356 < it.last_visible_y))
13357 ++first_unchanged_at_end_row;
13358
13359 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13360 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13361 >= it.last_visible_y))
13362 first_unchanged_at_end_row = NULL;
13363 else
13364 {
13365 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13366 + delta);
13367 first_unchanged_at_end_vpos
13368 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13369 xassert (stop_pos >= Z - END_UNCHANGED);
13370 }
13371 }
13372 else if (last_unchanged_at_beg_row == NULL)
13373 GIVE_UP (19);
13374
13375
13376 #if GLYPH_DEBUG
13377
13378 /* Either there is no unchanged row at the end, or the one we have
13379 now displays text. This is a necessary condition for the window
13380 end pos calculation at the end of this function. */
13381 xassert (first_unchanged_at_end_row == NULL
13382 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13383
13384 debug_last_unchanged_at_beg_vpos
13385 = (last_unchanged_at_beg_row
13386 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13387 : -1);
13388 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13389
13390 #endif /* GLYPH_DEBUG != 0 */
13391
13392
13393 /* Display new lines. Set last_text_row to the last new line
13394 displayed which has text on it, i.e. might end up as being the
13395 line where the window_end_vpos is. */
13396 w->cursor.vpos = -1;
13397 last_text_row = NULL;
13398 overlay_arrow_seen = 0;
13399 while (it.current_y < it.last_visible_y
13400 && !fonts_changed_p
13401 && (first_unchanged_at_end_row == NULL
13402 || IT_CHARPOS (it) < stop_pos))
13403 {
13404 if (display_line (&it))
13405 last_text_row = it.glyph_row - 1;
13406 }
13407
13408 if (fonts_changed_p)
13409 return -1;
13410
13411
13412 /* Compute differences in buffer positions, y-positions etc. for
13413 lines reused at the bottom of the window. Compute what we can
13414 scroll. */
13415 if (first_unchanged_at_end_row
13416 /* No lines reused because we displayed everything up to the
13417 bottom of the window. */
13418 && it.current_y < it.last_visible_y)
13419 {
13420 dvpos = (it.vpos
13421 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13422 current_matrix));
13423 dy = it.current_y - first_unchanged_at_end_row->y;
13424 run.current_y = first_unchanged_at_end_row->y;
13425 run.desired_y = run.current_y + dy;
13426 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13427 }
13428 else
13429 {
13430 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13431 first_unchanged_at_end_row = NULL;
13432 }
13433 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13434
13435
13436 /* Find the cursor if not already found. We have to decide whether
13437 PT will appear on this window (it sometimes doesn't, but this is
13438 not a very frequent case.) This decision has to be made before
13439 the current matrix is altered. A value of cursor.vpos < 0 means
13440 that PT is either in one of the lines beginning at
13441 first_unchanged_at_end_row or below the window. Don't care for
13442 lines that might be displayed later at the window end; as
13443 mentioned, this is not a frequent case. */
13444 if (w->cursor.vpos < 0)
13445 {
13446 /* Cursor in unchanged rows at the top? */
13447 if (PT < CHARPOS (start_pos)
13448 && last_unchanged_at_beg_row)
13449 {
13450 row = row_containing_pos (w, PT,
13451 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13452 last_unchanged_at_beg_row + 1, 0);
13453 if (row)
13454 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13455 }
13456
13457 /* Start from first_unchanged_at_end_row looking for PT. */
13458 else if (first_unchanged_at_end_row)
13459 {
13460 row = row_containing_pos (w, PT - delta,
13461 first_unchanged_at_end_row, NULL, 0);
13462 if (row)
13463 set_cursor_from_row (w, row, w->current_matrix, delta,
13464 delta_bytes, dy, dvpos);
13465 }
13466
13467 /* Give up if cursor was not found. */
13468 if (w->cursor.vpos < 0)
13469 {
13470 clear_glyph_matrix (w->desired_matrix);
13471 return -1;
13472 }
13473 }
13474
13475 /* Don't let the cursor end in the scroll margins. */
13476 {
13477 int this_scroll_margin, cursor_height;
13478
13479 this_scroll_margin = max (0, scroll_margin);
13480 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13481 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13482 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13483
13484 if ((w->cursor.y < this_scroll_margin
13485 && CHARPOS (start) > BEGV)
13486 /* Old redisplay didn't take scroll margin into account at the bottom,
13487 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
13488 || w->cursor.y + cursor_height + this_scroll_margin > it.last_visible_y)
13489 {
13490 w->cursor.vpos = -1;
13491 clear_glyph_matrix (w->desired_matrix);
13492 return -1;
13493 }
13494 }
13495
13496 /* Scroll the display. Do it before changing the current matrix so
13497 that xterm.c doesn't get confused about where the cursor glyph is
13498 found. */
13499 if (dy && run.height)
13500 {
13501 update_begin (f);
13502
13503 if (FRAME_WINDOW_P (f))
13504 {
13505 FRAME_RIF (f)->update_window_begin_hook (w);
13506 FRAME_RIF (f)->clear_window_mouse_face (w);
13507 FRAME_RIF (f)->scroll_run_hook (w, &run);
13508 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
13509 }
13510 else
13511 {
13512 /* Terminal frame. In this case, dvpos gives the number of
13513 lines to scroll by; dvpos < 0 means scroll up. */
13514 int first_unchanged_at_end_vpos
13515 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13516 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13517 int end = (WINDOW_TOP_EDGE_LINE (w)
13518 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13519 + window_internal_height (w));
13520
13521 /* Perform the operation on the screen. */
13522 if (dvpos > 0)
13523 {
13524 /* Scroll last_unchanged_at_beg_row to the end of the
13525 window down dvpos lines. */
13526 set_terminal_window (f, end);
13527
13528 /* On dumb terminals delete dvpos lines at the end
13529 before inserting dvpos empty lines. */
13530 if (!FRAME_SCROLL_REGION_OK (f))
13531 ins_del_lines (f, end - dvpos, -dvpos);
13532
13533 /* Insert dvpos empty lines in front of
13534 last_unchanged_at_beg_row. */
13535 ins_del_lines (f, from, dvpos);
13536 }
13537 else if (dvpos < 0)
13538 {
13539 /* Scroll up last_unchanged_at_beg_vpos to the end of
13540 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13541 set_terminal_window (f, end);
13542
13543 /* Delete dvpos lines in front of
13544 last_unchanged_at_beg_vpos. ins_del_lines will set
13545 the cursor to the given vpos and emit |dvpos| delete
13546 line sequences. */
13547 ins_del_lines (f, from + dvpos, dvpos);
13548
13549 /* On a dumb terminal insert dvpos empty lines at the
13550 end. */
13551 if (!FRAME_SCROLL_REGION_OK (f))
13552 ins_del_lines (f, end + dvpos, -dvpos);
13553 }
13554
13555 set_terminal_window (f, 0);
13556 }
13557
13558 update_end (f);
13559 }
13560
13561 /* Shift reused rows of the current matrix to the right position.
13562 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13563 text. */
13564 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13565 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
13566 if (dvpos < 0)
13567 {
13568 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13569 bottom_vpos, dvpos);
13570 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13571 bottom_vpos, 0);
13572 }
13573 else if (dvpos > 0)
13574 {
13575 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13576 bottom_vpos, dvpos);
13577 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13578 first_unchanged_at_end_vpos + dvpos, 0);
13579 }
13580
13581 /* For frame-based redisplay, make sure that current frame and window
13582 matrix are in sync with respect to glyph memory. */
13583 if (!FRAME_WINDOW_P (f))
13584 sync_frame_with_window_matrix_rows (w);
13585
13586 /* Adjust buffer positions in reused rows. */
13587 if (delta)
13588 increment_matrix_positions (current_matrix,
13589 first_unchanged_at_end_vpos + dvpos,
13590 bottom_vpos, delta, delta_bytes);
13591
13592 /* Adjust Y positions. */
13593 if (dy)
13594 shift_glyph_matrix (w, current_matrix,
13595 first_unchanged_at_end_vpos + dvpos,
13596 bottom_vpos, dy);
13597
13598 if (first_unchanged_at_end_row)
13599 first_unchanged_at_end_row += dvpos;
13600
13601 /* If scrolling up, there may be some lines to display at the end of
13602 the window. */
13603 last_text_row_at_end = NULL;
13604 if (dy < 0)
13605 {
13606 /* Scrolling up can leave for example a partially visible line
13607 at the end of the window to be redisplayed. */
13608 /* Set last_row to the glyph row in the current matrix where the
13609 window end line is found. It has been moved up or down in
13610 the matrix by dvpos. */
13611 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13612 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13613
13614 /* If last_row is the window end line, it should display text. */
13615 xassert (last_row->displays_text_p);
13616
13617 /* If window end line was partially visible before, begin
13618 displaying at that line. Otherwise begin displaying with the
13619 line following it. */
13620 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13621 {
13622 init_to_row_start (&it, w, last_row);
13623 it.vpos = last_vpos;
13624 it.current_y = last_row->y;
13625 }
13626 else
13627 {
13628 init_to_row_end (&it, w, last_row);
13629 it.vpos = 1 + last_vpos;
13630 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13631 ++last_row;
13632 }
13633
13634 /* We may start in a continuation line. If so, we have to
13635 get the right continuation_lines_width and current_x. */
13636 it.continuation_lines_width = last_row->continuation_lines_width;
13637 it.hpos = it.current_x = 0;
13638
13639 /* Display the rest of the lines at the window end. */
13640 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13641 while (it.current_y < it.last_visible_y
13642 && !fonts_changed_p)
13643 {
13644 /* Is it always sure that the display agrees with lines in
13645 the current matrix? I don't think so, so we mark rows
13646 displayed invalid in the current matrix by setting their
13647 enabled_p flag to zero. */
13648 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13649 if (display_line (&it))
13650 last_text_row_at_end = it.glyph_row - 1;
13651 }
13652 }
13653
13654 /* Update window_end_pos and window_end_vpos. */
13655 if (first_unchanged_at_end_row
13656 && first_unchanged_at_end_row->y < it.last_visible_y
13657 && !last_text_row_at_end)
13658 {
13659 /* Window end line if one of the preserved rows from the current
13660 matrix. Set row to the last row displaying text in current
13661 matrix starting at first_unchanged_at_end_row, after
13662 scrolling. */
13663 xassert (first_unchanged_at_end_row->displays_text_p);
13664 row = find_last_row_displaying_text (w->current_matrix, &it,
13665 first_unchanged_at_end_row);
13666 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
13667
13668 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13669 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13670 w->window_end_vpos
13671 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
13672 xassert (w->window_end_bytepos >= 0);
13673 IF_DEBUG (debug_method_add (w, "A"));
13674 }
13675 else if (last_text_row_at_end)
13676 {
13677 w->window_end_pos
13678 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
13679 w->window_end_bytepos
13680 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
13681 w->window_end_vpos
13682 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
13683 xassert (w->window_end_bytepos >= 0);
13684 IF_DEBUG (debug_method_add (w, "B"));
13685 }
13686 else if (last_text_row)
13687 {
13688 /* We have displayed either to the end of the window or at the
13689 end of the window, i.e. the last row with text is to be found
13690 in the desired matrix. */
13691 w->window_end_pos
13692 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13693 w->window_end_bytepos
13694 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13695 w->window_end_vpos
13696 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
13697 xassert (w->window_end_bytepos >= 0);
13698 }
13699 else if (first_unchanged_at_end_row == NULL
13700 && last_text_row == NULL
13701 && last_text_row_at_end == NULL)
13702 {
13703 /* Displayed to end of window, but no line containing text was
13704 displayed. Lines were deleted at the end of the window. */
13705 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
13706 int vpos = XFASTINT (w->window_end_vpos);
13707 struct glyph_row *current_row = current_matrix->rows + vpos;
13708 struct glyph_row *desired_row = desired_matrix->rows + vpos;
13709
13710 for (row = NULL;
13711 row == NULL && vpos >= first_vpos;
13712 --vpos, --current_row, --desired_row)
13713 {
13714 if (desired_row->enabled_p)
13715 {
13716 if (desired_row->displays_text_p)
13717 row = desired_row;
13718 }
13719 else if (current_row->displays_text_p)
13720 row = current_row;
13721 }
13722
13723 xassert (row != NULL);
13724 w->window_end_vpos = make_number (vpos + 1);
13725 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13726 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13727 xassert (w->window_end_bytepos >= 0);
13728 IF_DEBUG (debug_method_add (w, "C"));
13729 }
13730 else
13731 abort ();
13732
13733 #if 0 /* This leads to problems, for instance when the cursor is
13734 at ZV, and the cursor line displays no text. */
13735 /* Disable rows below what's displayed in the window. This makes
13736 debugging easier. */
13737 enable_glyph_matrix_rows (current_matrix,
13738 XFASTINT (w->window_end_vpos) + 1,
13739 bottom_vpos, 0);
13740 #endif
13741
13742 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
13743 debug_end_vpos = XFASTINT (w->window_end_vpos));
13744
13745 /* Record that display has not been completed. */
13746 w->window_end_valid = Qnil;
13747 w->desired_matrix->no_scrolling_p = 1;
13748 return 3;
13749
13750 #undef GIVE_UP
13751 }
13752
13753
13754 \f
13755 /***********************************************************************
13756 More debugging support
13757 ***********************************************************************/
13758
13759 #if GLYPH_DEBUG
13760
13761 void dump_glyph_row P_ ((struct glyph_row *, int, int));
13762 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
13763 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
13764
13765
13766 /* Dump the contents of glyph matrix MATRIX on stderr.
13767
13768 GLYPHS 0 means don't show glyph contents.
13769 GLYPHS 1 means show glyphs in short form
13770 GLYPHS > 1 means show glyphs in long form. */
13771
13772 void
13773 dump_glyph_matrix (matrix, glyphs)
13774 struct glyph_matrix *matrix;
13775 int glyphs;
13776 {
13777 int i;
13778 for (i = 0; i < matrix->nrows; ++i)
13779 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
13780 }
13781
13782
13783 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
13784 the glyph row and area where the glyph comes from. */
13785
13786 void
13787 dump_glyph (row, glyph, area)
13788 struct glyph_row *row;
13789 struct glyph *glyph;
13790 int area;
13791 {
13792 if (glyph->type == CHAR_GLYPH)
13793 {
13794 fprintf (stderr,
13795 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13796 glyph - row->glyphs[TEXT_AREA],
13797 'C',
13798 glyph->charpos,
13799 (BUFFERP (glyph->object)
13800 ? 'B'
13801 : (STRINGP (glyph->object)
13802 ? 'S'
13803 : '-')),
13804 glyph->pixel_width,
13805 glyph->u.ch,
13806 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
13807 ? glyph->u.ch
13808 : '.'),
13809 glyph->face_id,
13810 glyph->left_box_line_p,
13811 glyph->right_box_line_p);
13812 }
13813 else if (glyph->type == STRETCH_GLYPH)
13814 {
13815 fprintf (stderr,
13816 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13817 glyph - row->glyphs[TEXT_AREA],
13818 'S',
13819 glyph->charpos,
13820 (BUFFERP (glyph->object)
13821 ? 'B'
13822 : (STRINGP (glyph->object)
13823 ? 'S'
13824 : '-')),
13825 glyph->pixel_width,
13826 0,
13827 '.',
13828 glyph->face_id,
13829 glyph->left_box_line_p,
13830 glyph->right_box_line_p);
13831 }
13832 else if (glyph->type == IMAGE_GLYPH)
13833 {
13834 fprintf (stderr,
13835 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13836 glyph - row->glyphs[TEXT_AREA],
13837 'I',
13838 glyph->charpos,
13839 (BUFFERP (glyph->object)
13840 ? 'B'
13841 : (STRINGP (glyph->object)
13842 ? 'S'
13843 : '-')),
13844 glyph->pixel_width,
13845 glyph->u.img_id,
13846 '.',
13847 glyph->face_id,
13848 glyph->left_box_line_p,
13849 glyph->right_box_line_p);
13850 }
13851 }
13852
13853
13854 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
13855 GLYPHS 0 means don't show glyph contents.
13856 GLYPHS 1 means show glyphs in short form
13857 GLYPHS > 1 means show glyphs in long form. */
13858
13859 void
13860 dump_glyph_row (row, vpos, glyphs)
13861 struct glyph_row *row;
13862 int vpos, glyphs;
13863 {
13864 if (glyphs != 1)
13865 {
13866 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
13867 fprintf (stderr, "=======================================================================\n");
13868
13869 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
13870 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
13871 vpos,
13872 MATRIX_ROW_START_CHARPOS (row),
13873 MATRIX_ROW_END_CHARPOS (row),
13874 row->used[TEXT_AREA],
13875 row->contains_overlapping_glyphs_p,
13876 row->enabled_p,
13877 row->truncated_on_left_p,
13878 row->truncated_on_right_p,
13879 row->overlay_arrow_p,
13880 row->continued_p,
13881 MATRIX_ROW_CONTINUATION_LINE_P (row),
13882 row->displays_text_p,
13883 row->ends_at_zv_p,
13884 row->fill_line_p,
13885 row->ends_in_middle_of_char_p,
13886 row->starts_in_middle_of_char_p,
13887 row->mouse_face_p,
13888 row->x,
13889 row->y,
13890 row->pixel_width,
13891 row->height,
13892 row->visible_height,
13893 row->ascent,
13894 row->phys_ascent);
13895 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
13896 row->end.overlay_string_index,
13897 row->continuation_lines_width);
13898 fprintf (stderr, "%9d %5d\n",
13899 CHARPOS (row->start.string_pos),
13900 CHARPOS (row->end.string_pos));
13901 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
13902 row->end.dpvec_index);
13903 }
13904
13905 if (glyphs > 1)
13906 {
13907 int area;
13908
13909 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13910 {
13911 struct glyph *glyph = row->glyphs[area];
13912 struct glyph *glyph_end = glyph + row->used[area];
13913
13914 /* Glyph for a line end in text. */
13915 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
13916 ++glyph_end;
13917
13918 if (glyph < glyph_end)
13919 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
13920
13921 for (; glyph < glyph_end; ++glyph)
13922 dump_glyph (row, glyph, area);
13923 }
13924 }
13925 else if (glyphs == 1)
13926 {
13927 int area;
13928
13929 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13930 {
13931 char *s = (char *) alloca (row->used[area] + 1);
13932 int i;
13933
13934 for (i = 0; i < row->used[area]; ++i)
13935 {
13936 struct glyph *glyph = row->glyphs[area] + i;
13937 if (glyph->type == CHAR_GLYPH
13938 && glyph->u.ch < 0x80
13939 && glyph->u.ch >= ' ')
13940 s[i] = glyph->u.ch;
13941 else
13942 s[i] = '.';
13943 }
13944
13945 s[i] = '\0';
13946 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
13947 }
13948 }
13949 }
13950
13951
13952 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
13953 Sdump_glyph_matrix, 0, 1, "p",
13954 doc: /* Dump the current matrix of the selected window to stderr.
13955 Shows contents of glyph row structures. With non-nil
13956 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
13957 glyphs in short form, otherwise show glyphs in long form. */)
13958 (glyphs)
13959 Lisp_Object glyphs;
13960 {
13961 struct window *w = XWINDOW (selected_window);
13962 struct buffer *buffer = XBUFFER (w->buffer);
13963
13964 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
13965 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
13966 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
13967 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
13968 fprintf (stderr, "=============================================\n");
13969 dump_glyph_matrix (w->current_matrix,
13970 NILP (glyphs) ? 0 : XINT (glyphs));
13971 return Qnil;
13972 }
13973
13974
13975 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
13976 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
13977 ()
13978 {
13979 struct frame *f = XFRAME (selected_frame);
13980 dump_glyph_matrix (f->current_matrix, 1);
13981 return Qnil;
13982 }
13983
13984
13985 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
13986 doc: /* Dump glyph row ROW to stderr.
13987 GLYPH 0 means don't dump glyphs.
13988 GLYPH 1 means dump glyphs in short form.
13989 GLYPH > 1 or omitted means dump glyphs in long form. */)
13990 (row, glyphs)
13991 Lisp_Object row, glyphs;
13992 {
13993 struct glyph_matrix *matrix;
13994 int vpos;
13995
13996 CHECK_NUMBER (row);
13997 matrix = XWINDOW (selected_window)->current_matrix;
13998 vpos = XINT (row);
13999 if (vpos >= 0 && vpos < matrix->nrows)
14000 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14001 vpos,
14002 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14003 return Qnil;
14004 }
14005
14006
14007 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14008 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14009 GLYPH 0 means don't dump glyphs.
14010 GLYPH 1 means dump glyphs in short form.
14011 GLYPH > 1 or omitted means dump glyphs in long form. */)
14012 (row, glyphs)
14013 Lisp_Object row, glyphs;
14014 {
14015 struct frame *sf = SELECTED_FRAME ();
14016 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14017 int vpos;
14018
14019 CHECK_NUMBER (row);
14020 vpos = XINT (row);
14021 if (vpos >= 0 && vpos < m->nrows)
14022 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14023 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14024 return Qnil;
14025 }
14026
14027
14028 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14029 doc: /* Toggle tracing of redisplay.
14030 With ARG, turn tracing on if and only if ARG is positive. */)
14031 (arg)
14032 Lisp_Object arg;
14033 {
14034 if (NILP (arg))
14035 trace_redisplay_p = !trace_redisplay_p;
14036 else
14037 {
14038 arg = Fprefix_numeric_value (arg);
14039 trace_redisplay_p = XINT (arg) > 0;
14040 }
14041
14042 return Qnil;
14043 }
14044
14045
14046 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14047 doc: /* Like `format', but print result to stderr.
14048 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14049 (nargs, args)
14050 int nargs;
14051 Lisp_Object *args;
14052 {
14053 Lisp_Object s = Fformat (nargs, args);
14054 fprintf (stderr, "%s", SDATA (s));
14055 return Qnil;
14056 }
14057
14058 #endif /* GLYPH_DEBUG */
14059
14060
14061 \f
14062 /***********************************************************************
14063 Building Desired Matrix Rows
14064 ***********************************************************************/
14065
14066 /* Return a temporary glyph row holding the glyphs of an overlay
14067 arrow. Only used for non-window-redisplay windows. */
14068
14069 static struct glyph_row *
14070 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14071 struct window *w;
14072 Lisp_Object overlay_arrow_string;
14073 {
14074 struct frame *f = XFRAME (WINDOW_FRAME (w));
14075 struct buffer *buffer = XBUFFER (w->buffer);
14076 struct buffer *old = current_buffer;
14077 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14078 int arrow_len = SCHARS (overlay_arrow_string);
14079 const unsigned char *arrow_end = arrow_string + arrow_len;
14080 const unsigned char *p;
14081 struct it it;
14082 int multibyte_p;
14083 int n_glyphs_before;
14084
14085 set_buffer_temp (buffer);
14086 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14087 it.glyph_row->used[TEXT_AREA] = 0;
14088 SET_TEXT_POS (it.position, 0, 0);
14089
14090 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14091 p = arrow_string;
14092 while (p < arrow_end)
14093 {
14094 Lisp_Object face, ilisp;
14095
14096 /* Get the next character. */
14097 if (multibyte_p)
14098 it.c = string_char_and_length (p, arrow_len, &it.len);
14099 else
14100 it.c = *p, it.len = 1;
14101 p += it.len;
14102
14103 /* Get its face. */
14104 ilisp = make_number (p - arrow_string);
14105 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
14106 it.face_id = compute_char_face (f, it.c, face);
14107
14108 /* Compute its width, get its glyphs. */
14109 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14110 SET_TEXT_POS (it.position, -1, -1);
14111 PRODUCE_GLYPHS (&it);
14112
14113 /* If this character doesn't fit any more in the line, we have
14114 to remove some glyphs. */
14115 if (it.current_x > it.last_visible_x)
14116 {
14117 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14118 break;
14119 }
14120 }
14121
14122 set_buffer_temp (old);
14123 return it.glyph_row;
14124 }
14125
14126
14127 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14128 glyphs are only inserted for terminal frames since we can't really
14129 win with truncation glyphs when partially visible glyphs are
14130 involved. Which glyphs to insert is determined by
14131 produce_special_glyphs. */
14132
14133 static void
14134 insert_left_trunc_glyphs (it)
14135 struct it *it;
14136 {
14137 struct it truncate_it;
14138 struct glyph *from, *end, *to, *toend;
14139
14140 xassert (!FRAME_WINDOW_P (it->f));
14141
14142 /* Get the truncation glyphs. */
14143 truncate_it = *it;
14144 truncate_it.current_x = 0;
14145 truncate_it.face_id = DEFAULT_FACE_ID;
14146 truncate_it.glyph_row = &scratch_glyph_row;
14147 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14148 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14149 truncate_it.object = make_number (0);
14150 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14151
14152 /* Overwrite glyphs from IT with truncation glyphs. */
14153 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14154 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14155 to = it->glyph_row->glyphs[TEXT_AREA];
14156 toend = to + it->glyph_row->used[TEXT_AREA];
14157
14158 while (from < end)
14159 *to++ = *from++;
14160
14161 /* There may be padding glyphs left over. Overwrite them too. */
14162 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14163 {
14164 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14165 while (from < end)
14166 *to++ = *from++;
14167 }
14168
14169 if (to > toend)
14170 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14171 }
14172
14173
14174 /* Compute the pixel height and width of IT->glyph_row.
14175
14176 Most of the time, ascent and height of a display line will be equal
14177 to the max_ascent and max_height values of the display iterator
14178 structure. This is not the case if
14179
14180 1. We hit ZV without displaying anything. In this case, max_ascent
14181 and max_height will be zero.
14182
14183 2. We have some glyphs that don't contribute to the line height.
14184 (The glyph row flag contributes_to_line_height_p is for future
14185 pixmap extensions).
14186
14187 The first case is easily covered by using default values because in
14188 these cases, the line height does not really matter, except that it
14189 must not be zero. */
14190
14191 static void
14192 compute_line_metrics (it)
14193 struct it *it;
14194 {
14195 struct glyph_row *row = it->glyph_row;
14196 int area, i;
14197
14198 if (FRAME_WINDOW_P (it->f))
14199 {
14200 int i, min_y, max_y;
14201
14202 /* The line may consist of one space only, that was added to
14203 place the cursor on it. If so, the row's height hasn't been
14204 computed yet. */
14205 if (row->height == 0)
14206 {
14207 if (it->max_ascent + it->max_descent == 0)
14208 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14209 row->ascent = it->max_ascent;
14210 row->height = it->max_ascent + it->max_descent;
14211 row->phys_ascent = it->max_phys_ascent;
14212 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14213 }
14214
14215 /* Compute the width of this line. */
14216 row->pixel_width = row->x;
14217 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14218 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14219
14220 xassert (row->pixel_width >= 0);
14221 xassert (row->ascent >= 0 && row->height > 0);
14222
14223 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14224 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14225
14226 /* If first line's physical ascent is larger than its logical
14227 ascent, use the physical ascent, and make the row taller.
14228 This makes accented characters fully visible. */
14229 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14230 && row->phys_ascent > row->ascent)
14231 {
14232 row->height += row->phys_ascent - row->ascent;
14233 row->ascent = row->phys_ascent;
14234 }
14235
14236 /* Compute how much of the line is visible. */
14237 row->visible_height = row->height;
14238
14239 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14240 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14241
14242 if (row->y < min_y)
14243 row->visible_height -= min_y - row->y;
14244 if (row->y + row->height > max_y)
14245 row->visible_height -= row->y + row->height - max_y;
14246 }
14247 else
14248 {
14249 row->pixel_width = row->used[TEXT_AREA];
14250 if (row->continued_p)
14251 row->pixel_width -= it->continuation_pixel_width;
14252 else if (row->truncated_on_right_p)
14253 row->pixel_width -= it->truncation_pixel_width;
14254 row->ascent = row->phys_ascent = 0;
14255 row->height = row->phys_height = row->visible_height = 1;
14256 }
14257
14258 /* Compute a hash code for this row. */
14259 row->hash = 0;
14260 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14261 for (i = 0; i < row->used[area]; ++i)
14262 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14263 + row->glyphs[area][i].u.val
14264 + row->glyphs[area][i].face_id
14265 + row->glyphs[area][i].padding_p
14266 + (row->glyphs[area][i].type << 2));
14267
14268 it->max_ascent = it->max_descent = 0;
14269 it->max_phys_ascent = it->max_phys_descent = 0;
14270 }
14271
14272
14273 /* Append one space to the glyph row of iterator IT if doing a
14274 window-based redisplay. The space has the same face as
14275 IT->face_id. Value is non-zero if a space was added.
14276
14277 This function is called to make sure that there is always one glyph
14278 at the end of a glyph row that the cursor can be set on under
14279 window-systems. (If there weren't such a glyph we would not know
14280 how wide and tall a box cursor should be displayed).
14281
14282 At the same time this space let's a nicely handle clearing to the
14283 end of the line if the row ends in italic text. */
14284
14285 static int
14286 append_space_for_newline (it, default_face_p)
14287 struct it *it;
14288 int default_face_p;
14289 {
14290 if (FRAME_WINDOW_P (it->f))
14291 {
14292 int n = it->glyph_row->used[TEXT_AREA];
14293
14294 if (it->glyph_row->glyphs[TEXT_AREA] + n
14295 < it->glyph_row->glyphs[1 + TEXT_AREA])
14296 {
14297 /* Save some values that must not be changed.
14298 Must save IT->c and IT->len because otherwise
14299 ITERATOR_AT_END_P wouldn't work anymore after
14300 append_space_for_newline has been called. */
14301 enum display_element_type saved_what = it->what;
14302 int saved_c = it->c, saved_len = it->len;
14303 int saved_x = it->current_x;
14304 int saved_face_id = it->face_id;
14305 struct text_pos saved_pos;
14306 Lisp_Object saved_object;
14307 struct face *face;
14308
14309 saved_object = it->object;
14310 saved_pos = it->position;
14311
14312 it->what = IT_CHARACTER;
14313 bzero (&it->position, sizeof it->position);
14314 it->object = make_number (0);
14315 it->c = ' ';
14316 it->len = 1;
14317
14318 if (default_face_p)
14319 it->face_id = DEFAULT_FACE_ID;
14320 else if (it->face_before_selective_p)
14321 it->face_id = it->saved_face_id;
14322 face = FACE_FROM_ID (it->f, it->face_id);
14323 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14324
14325 PRODUCE_GLYPHS (it);
14326
14327 it->override_ascent = -1;
14328 it->constrain_row_ascent_descent_p = 0;
14329 it->current_x = saved_x;
14330 it->object = saved_object;
14331 it->position = saved_pos;
14332 it->what = saved_what;
14333 it->face_id = saved_face_id;
14334 it->len = saved_len;
14335 it->c = saved_c;
14336 return 1;
14337 }
14338 }
14339
14340 return 0;
14341 }
14342
14343
14344 /* Extend the face of the last glyph in the text area of IT->glyph_row
14345 to the end of the display line. Called from display_line.
14346 If the glyph row is empty, add a space glyph to it so that we
14347 know the face to draw. Set the glyph row flag fill_line_p. */
14348
14349 static void
14350 extend_face_to_end_of_line (it)
14351 struct it *it;
14352 {
14353 struct face *face;
14354 struct frame *f = it->f;
14355
14356 /* If line is already filled, do nothing. */
14357 if (it->current_x >= it->last_visible_x)
14358 return;
14359
14360 /* Face extension extends the background and box of IT->face_id
14361 to the end of the line. If the background equals the background
14362 of the frame, we don't have to do anything. */
14363 if (it->face_before_selective_p)
14364 face = FACE_FROM_ID (it->f, it->saved_face_id);
14365 else
14366 face = FACE_FROM_ID (f, it->face_id);
14367
14368 if (FRAME_WINDOW_P (f)
14369 && face->box == FACE_NO_BOX
14370 && face->background == FRAME_BACKGROUND_PIXEL (f)
14371 && !face->stipple)
14372 return;
14373
14374 /* Set the glyph row flag indicating that the face of the last glyph
14375 in the text area has to be drawn to the end of the text area. */
14376 it->glyph_row->fill_line_p = 1;
14377
14378 /* If current character of IT is not ASCII, make sure we have the
14379 ASCII face. This will be automatically undone the next time
14380 get_next_display_element returns a multibyte character. Note
14381 that the character will always be single byte in unibyte text. */
14382 if (!SINGLE_BYTE_CHAR_P (it->c))
14383 {
14384 it->face_id = FACE_FOR_CHAR (f, face, 0);
14385 }
14386
14387 if (FRAME_WINDOW_P (f))
14388 {
14389 /* If the row is empty, add a space with the current face of IT,
14390 so that we know which face to draw. */
14391 if (it->glyph_row->used[TEXT_AREA] == 0)
14392 {
14393 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14394 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14395 it->glyph_row->used[TEXT_AREA] = 1;
14396 }
14397 }
14398 else
14399 {
14400 /* Save some values that must not be changed. */
14401 int saved_x = it->current_x;
14402 struct text_pos saved_pos;
14403 Lisp_Object saved_object;
14404 enum display_element_type saved_what = it->what;
14405 int saved_face_id = it->face_id;
14406
14407 saved_object = it->object;
14408 saved_pos = it->position;
14409
14410 it->what = IT_CHARACTER;
14411 bzero (&it->position, sizeof it->position);
14412 it->object = make_number (0);
14413 it->c = ' ';
14414 it->len = 1;
14415 it->face_id = face->id;
14416
14417 PRODUCE_GLYPHS (it);
14418
14419 while (it->current_x <= it->last_visible_x)
14420 PRODUCE_GLYPHS (it);
14421
14422 /* Don't count these blanks really. It would let us insert a left
14423 truncation glyph below and make us set the cursor on them, maybe. */
14424 it->current_x = saved_x;
14425 it->object = saved_object;
14426 it->position = saved_pos;
14427 it->what = saved_what;
14428 it->face_id = saved_face_id;
14429 }
14430 }
14431
14432
14433 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14434 trailing whitespace. */
14435
14436 static int
14437 trailing_whitespace_p (charpos)
14438 int charpos;
14439 {
14440 int bytepos = CHAR_TO_BYTE (charpos);
14441 int c = 0;
14442
14443 while (bytepos < ZV_BYTE
14444 && (c = FETCH_CHAR (bytepos),
14445 c == ' ' || c == '\t'))
14446 ++bytepos;
14447
14448 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14449 {
14450 if (bytepos != PT_BYTE)
14451 return 1;
14452 }
14453 return 0;
14454 }
14455
14456
14457 /* Highlight trailing whitespace, if any, in ROW. */
14458
14459 void
14460 highlight_trailing_whitespace (f, row)
14461 struct frame *f;
14462 struct glyph_row *row;
14463 {
14464 int used = row->used[TEXT_AREA];
14465
14466 if (used)
14467 {
14468 struct glyph *start = row->glyphs[TEXT_AREA];
14469 struct glyph *glyph = start + used - 1;
14470
14471 /* Skip over glyphs inserted to display the cursor at the
14472 end of a line, for extending the face of the last glyph
14473 to the end of the line on terminals, and for truncation
14474 and continuation glyphs. */
14475 while (glyph >= start
14476 && glyph->type == CHAR_GLYPH
14477 && INTEGERP (glyph->object))
14478 --glyph;
14479
14480 /* If last glyph is a space or stretch, and it's trailing
14481 whitespace, set the face of all trailing whitespace glyphs in
14482 IT->glyph_row to `trailing-whitespace'. */
14483 if (glyph >= start
14484 && BUFFERP (glyph->object)
14485 && (glyph->type == STRETCH_GLYPH
14486 || (glyph->type == CHAR_GLYPH
14487 && glyph->u.ch == ' '))
14488 && trailing_whitespace_p (glyph->charpos))
14489 {
14490 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
14491
14492 while (glyph >= start
14493 && BUFFERP (glyph->object)
14494 && (glyph->type == STRETCH_GLYPH
14495 || (glyph->type == CHAR_GLYPH
14496 && glyph->u.ch == ' ')))
14497 (glyph--)->face_id = face_id;
14498 }
14499 }
14500 }
14501
14502
14503 /* Value is non-zero if glyph row ROW in window W should be
14504 used to hold the cursor. */
14505
14506 static int
14507 cursor_row_p (w, row)
14508 struct window *w;
14509 struct glyph_row *row;
14510 {
14511 int cursor_row_p = 1;
14512
14513 if (PT == MATRIX_ROW_END_CHARPOS (row))
14514 {
14515 /* If the row ends with a newline from a string, we don't want
14516 the cursor there (if the row is continued it doesn't end in a
14517 newline). */
14518 if (CHARPOS (row->end.string_pos) >= 0
14519 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14520 cursor_row_p = row->continued_p;
14521
14522 /* If the row ends at ZV, display the cursor at the end of that
14523 row instead of at the start of the row below. */
14524 else if (row->ends_at_zv_p)
14525 cursor_row_p = 1;
14526 else
14527 cursor_row_p = 0;
14528 }
14529
14530 return cursor_row_p;
14531 }
14532
14533
14534 /* Construct the glyph row IT->glyph_row in the desired matrix of
14535 IT->w from text at the current position of IT. See dispextern.h
14536 for an overview of struct it. Value is non-zero if
14537 IT->glyph_row displays text, as opposed to a line displaying ZV
14538 only. */
14539
14540 static int
14541 display_line (it)
14542 struct it *it;
14543 {
14544 struct glyph_row *row = it->glyph_row;
14545 int overlay_arrow_bitmap;
14546 Lisp_Object overlay_arrow_string;
14547
14548 /* We always start displaying at hpos zero even if hscrolled. */
14549 xassert (it->hpos == 0 && it->current_x == 0);
14550
14551 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14552 >= it->w->desired_matrix->nrows)
14553 {
14554 it->w->nrows_scale_factor++;
14555 fonts_changed_p = 1;
14556 return 0;
14557 }
14558
14559 /* Is IT->w showing the region? */
14560 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
14561
14562 /* Clear the result glyph row and enable it. */
14563 prepare_desired_row (row);
14564
14565 row->y = it->current_y;
14566 row->start = it->start;
14567 row->continuation_lines_width = it->continuation_lines_width;
14568 row->displays_text_p = 1;
14569 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14570 it->starts_in_middle_of_char_p = 0;
14571
14572 /* Arrange the overlays nicely for our purposes. Usually, we call
14573 display_line on only one line at a time, in which case this
14574 can't really hurt too much, or we call it on lines which appear
14575 one after another in the buffer, in which case all calls to
14576 recenter_overlay_lists but the first will be pretty cheap. */
14577 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14578
14579 /* Move over display elements that are not visible because we are
14580 hscrolled. This may stop at an x-position < IT->first_visible_x
14581 if the first glyph is partially visible or if we hit a line end. */
14582 if (it->current_x < it->first_visible_x)
14583 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14584 MOVE_TO_POS | MOVE_TO_X);
14585
14586 /* Get the initial row height. This is either the height of the
14587 text hscrolled, if there is any, or zero. */
14588 row->ascent = it->max_ascent;
14589 row->height = it->max_ascent + it->max_descent;
14590 row->phys_ascent = it->max_phys_ascent;
14591 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14592
14593 /* Loop generating characters. The loop is left with IT on the next
14594 character to display. */
14595 while (1)
14596 {
14597 int n_glyphs_before, hpos_before, x_before;
14598 int x, i, nglyphs;
14599 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
14600
14601 /* Retrieve the next thing to display. Value is zero if end of
14602 buffer reached. */
14603 if (!get_next_display_element (it))
14604 {
14605 /* Maybe add a space at the end of this line that is used to
14606 display the cursor there under X. Set the charpos of the
14607 first glyph of blank lines not corresponding to any text
14608 to -1. */
14609 #ifdef HAVE_WINDOW_SYSTEM
14610 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14611 row->exact_window_width_line_p = 1;
14612 else
14613 #endif /* HAVE_WINDOW_SYSTEM */
14614 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
14615 || row->used[TEXT_AREA] == 0)
14616 {
14617 row->glyphs[TEXT_AREA]->charpos = -1;
14618 row->displays_text_p = 0;
14619
14620 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
14621 && (!MINI_WINDOW_P (it->w)
14622 || (minibuf_level && EQ (it->window, minibuf_window))))
14623 row->indicate_empty_line_p = 1;
14624 }
14625
14626 it->continuation_lines_width = 0;
14627 row->ends_at_zv_p = 1;
14628 break;
14629 }
14630
14631 /* Now, get the metrics of what we want to display. This also
14632 generates glyphs in `row' (which is IT->glyph_row). */
14633 n_glyphs_before = row->used[TEXT_AREA];
14634 x = it->current_x;
14635
14636 /* Remember the line height so far in case the next element doesn't
14637 fit on the line. */
14638 if (!it->truncate_lines_p)
14639 {
14640 ascent = it->max_ascent;
14641 descent = it->max_descent;
14642 phys_ascent = it->max_phys_ascent;
14643 phys_descent = it->max_phys_descent;
14644 }
14645
14646 PRODUCE_GLYPHS (it);
14647
14648 /* If this display element was in marginal areas, continue with
14649 the next one. */
14650 if (it->area != TEXT_AREA)
14651 {
14652 row->ascent = max (row->ascent, it->max_ascent);
14653 row->height = max (row->height, it->max_ascent + it->max_descent);
14654 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14655 row->phys_height = max (row->phys_height,
14656 it->max_phys_ascent + it->max_phys_descent);
14657 set_iterator_to_next (it, 1);
14658 continue;
14659 }
14660
14661 /* Does the display element fit on the line? If we truncate
14662 lines, we should draw past the right edge of the window. If
14663 we don't truncate, we want to stop so that we can display the
14664 continuation glyph before the right margin. If lines are
14665 continued, there are two possible strategies for characters
14666 resulting in more than 1 glyph (e.g. tabs): Display as many
14667 glyphs as possible in this line and leave the rest for the
14668 continuation line, or display the whole element in the next
14669 line. Original redisplay did the former, so we do it also. */
14670 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14671 hpos_before = it->hpos;
14672 x_before = x;
14673
14674 if (/* Not a newline. */
14675 nglyphs > 0
14676 /* Glyphs produced fit entirely in the line. */
14677 && it->current_x < it->last_visible_x)
14678 {
14679 it->hpos += nglyphs;
14680 row->ascent = max (row->ascent, it->max_ascent);
14681 row->height = max (row->height, it->max_ascent + it->max_descent);
14682 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14683 row->phys_height = max (row->phys_height,
14684 it->max_phys_ascent + it->max_phys_descent);
14685 if (it->current_x - it->pixel_width < it->first_visible_x)
14686 row->x = x - it->first_visible_x;
14687 }
14688 else
14689 {
14690 int new_x;
14691 struct glyph *glyph;
14692
14693 for (i = 0; i < nglyphs; ++i, x = new_x)
14694 {
14695 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14696 new_x = x + glyph->pixel_width;
14697
14698 if (/* Lines are continued. */
14699 !it->truncate_lines_p
14700 && (/* Glyph doesn't fit on the line. */
14701 new_x > it->last_visible_x
14702 /* Or it fits exactly on a window system frame. */
14703 || (new_x == it->last_visible_x
14704 && FRAME_WINDOW_P (it->f))))
14705 {
14706 /* End of a continued line. */
14707
14708 if (it->hpos == 0
14709 || (new_x == it->last_visible_x
14710 && FRAME_WINDOW_P (it->f)))
14711 {
14712 /* Current glyph is the only one on the line or
14713 fits exactly on the line. We must continue
14714 the line because we can't draw the cursor
14715 after the glyph. */
14716 row->continued_p = 1;
14717 it->current_x = new_x;
14718 it->continuation_lines_width += new_x;
14719 ++it->hpos;
14720 if (i == nglyphs - 1)
14721 {
14722 set_iterator_to_next (it, 1);
14723 #ifdef HAVE_WINDOW_SYSTEM
14724 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14725 {
14726 if (!get_next_display_element (it))
14727 {
14728 row->exact_window_width_line_p = 1;
14729 it->continuation_lines_width = 0;
14730 row->continued_p = 0;
14731 row->ends_at_zv_p = 1;
14732 }
14733 else if (ITERATOR_AT_END_OF_LINE_P (it))
14734 {
14735 row->continued_p = 0;
14736 row->exact_window_width_line_p = 1;
14737 }
14738 }
14739 #endif /* HAVE_WINDOW_SYSTEM */
14740 }
14741 }
14742 else if (CHAR_GLYPH_PADDING_P (*glyph)
14743 && !FRAME_WINDOW_P (it->f))
14744 {
14745 /* A padding glyph that doesn't fit on this line.
14746 This means the whole character doesn't fit
14747 on the line. */
14748 row->used[TEXT_AREA] = n_glyphs_before;
14749
14750 /* Fill the rest of the row with continuation
14751 glyphs like in 20.x. */
14752 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
14753 < row->glyphs[1 + TEXT_AREA])
14754 produce_special_glyphs (it, IT_CONTINUATION);
14755
14756 row->continued_p = 1;
14757 it->current_x = x_before;
14758 it->continuation_lines_width += x_before;
14759
14760 /* Restore the height to what it was before the
14761 element not fitting on the line. */
14762 it->max_ascent = ascent;
14763 it->max_descent = descent;
14764 it->max_phys_ascent = phys_ascent;
14765 it->max_phys_descent = phys_descent;
14766 }
14767 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
14768 {
14769 /* A TAB that extends past the right edge of the
14770 window. This produces a single glyph on
14771 window system frames. We leave the glyph in
14772 this row and let it fill the row, but don't
14773 consume the TAB. */
14774 it->continuation_lines_width += it->last_visible_x;
14775 row->ends_in_middle_of_char_p = 1;
14776 row->continued_p = 1;
14777 glyph->pixel_width = it->last_visible_x - x;
14778 it->starts_in_middle_of_char_p = 1;
14779 }
14780 else
14781 {
14782 /* Something other than a TAB that draws past
14783 the right edge of the window. Restore
14784 positions to values before the element. */
14785 row->used[TEXT_AREA] = n_glyphs_before + i;
14786
14787 /* Display continuation glyphs. */
14788 if (!FRAME_WINDOW_P (it->f))
14789 produce_special_glyphs (it, IT_CONTINUATION);
14790 row->continued_p = 1;
14791
14792 it->continuation_lines_width += x;
14793
14794 if (nglyphs > 1 && i > 0)
14795 {
14796 row->ends_in_middle_of_char_p = 1;
14797 it->starts_in_middle_of_char_p = 1;
14798 }
14799
14800 /* Restore the height to what it was before the
14801 element not fitting on the line. */
14802 it->max_ascent = ascent;
14803 it->max_descent = descent;
14804 it->max_phys_ascent = phys_ascent;
14805 it->max_phys_descent = phys_descent;
14806 }
14807
14808 break;
14809 }
14810 else if (new_x > it->first_visible_x)
14811 {
14812 /* Increment number of glyphs actually displayed. */
14813 ++it->hpos;
14814
14815 if (x < it->first_visible_x)
14816 /* Glyph is partially visible, i.e. row starts at
14817 negative X position. */
14818 row->x = x - it->first_visible_x;
14819 }
14820 else
14821 {
14822 /* Glyph is completely off the left margin of the
14823 window. This should not happen because of the
14824 move_it_in_display_line at the start of this
14825 function, unless the text display area of the
14826 window is empty. */
14827 xassert (it->first_visible_x <= it->last_visible_x);
14828 }
14829 }
14830
14831 row->ascent = max (row->ascent, it->max_ascent);
14832 row->height = max (row->height, it->max_ascent + it->max_descent);
14833 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14834 row->phys_height = max (row->phys_height,
14835 it->max_phys_ascent + it->max_phys_descent);
14836
14837 /* End of this display line if row is continued. */
14838 if (row->continued_p || row->ends_at_zv_p)
14839 break;
14840 }
14841
14842 at_end_of_line:
14843 /* Is this a line end? If yes, we're also done, after making
14844 sure that a non-default face is extended up to the right
14845 margin of the window. */
14846 if (ITERATOR_AT_END_OF_LINE_P (it))
14847 {
14848 int used_before = row->used[TEXT_AREA];
14849
14850 row->ends_in_newline_from_string_p = STRINGP (it->object);
14851
14852 #ifdef HAVE_WINDOW_SYSTEM
14853 /* Add a space at the end of the line that is used to
14854 display the cursor there. */
14855 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14856 append_space_for_newline (it, 0);
14857 #endif /* HAVE_WINDOW_SYSTEM */
14858
14859 /* Extend the face to the end of the line. */
14860 extend_face_to_end_of_line (it);
14861
14862 /* Make sure we have the position. */
14863 if (used_before == 0)
14864 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
14865
14866 /* Consume the line end. This skips over invisible lines. */
14867 set_iterator_to_next (it, 1);
14868 it->continuation_lines_width = 0;
14869 break;
14870 }
14871
14872 /* Proceed with next display element. Note that this skips
14873 over lines invisible because of selective display. */
14874 set_iterator_to_next (it, 1);
14875
14876 /* If we truncate lines, we are done when the last displayed
14877 glyphs reach past the right margin of the window. */
14878 if (it->truncate_lines_p
14879 && (FRAME_WINDOW_P (it->f)
14880 ? (it->current_x >= it->last_visible_x)
14881 : (it->current_x > it->last_visible_x)))
14882 {
14883 /* Maybe add truncation glyphs. */
14884 if (!FRAME_WINDOW_P (it->f))
14885 {
14886 int i, n;
14887
14888 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14889 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14890 break;
14891
14892 for (n = row->used[TEXT_AREA]; i < n; ++i)
14893 {
14894 row->used[TEXT_AREA] = i;
14895 produce_special_glyphs (it, IT_TRUNCATION);
14896 }
14897 }
14898 #ifdef HAVE_WINDOW_SYSTEM
14899 else
14900 {
14901 /* Don't truncate if we can overflow newline into fringe. */
14902 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14903 {
14904 if (!get_next_display_element (it))
14905 {
14906 #ifdef HAVE_WINDOW_SYSTEM
14907 it->continuation_lines_width = 0;
14908 row->ends_at_zv_p = 1;
14909 row->exact_window_width_line_p = 1;
14910 break;
14911 #endif /* HAVE_WINDOW_SYSTEM */
14912 }
14913 if (ITERATOR_AT_END_OF_LINE_P (it))
14914 {
14915 row->exact_window_width_line_p = 1;
14916 goto at_end_of_line;
14917 }
14918 }
14919 }
14920 #endif /* HAVE_WINDOW_SYSTEM */
14921
14922 row->truncated_on_right_p = 1;
14923 it->continuation_lines_width = 0;
14924 reseat_at_next_visible_line_start (it, 0);
14925 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
14926 it->hpos = hpos_before;
14927 it->current_x = x_before;
14928 break;
14929 }
14930 }
14931
14932 /* If line is not empty and hscrolled, maybe insert truncation glyphs
14933 at the left window margin. */
14934 if (it->first_visible_x
14935 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
14936 {
14937 if (!FRAME_WINDOW_P (it->f))
14938 insert_left_trunc_glyphs (it);
14939 row->truncated_on_left_p = 1;
14940 }
14941
14942 /* If the start of this line is the overlay arrow-position, then
14943 mark this glyph row as the one containing the overlay arrow.
14944 This is clearly a mess with variable size fonts. It would be
14945 better to let it be displayed like cursors under X. */
14946 if (! overlay_arrow_seen
14947 && (overlay_arrow_string
14948 = overlay_arrow_at_row (it->f, row, &overlay_arrow_bitmap),
14949 !NILP (overlay_arrow_string)))
14950 {
14951 /* Overlay arrow in window redisplay is a fringe bitmap. */
14952 if (!FRAME_WINDOW_P (it->f))
14953 {
14954 struct glyph_row *arrow_row
14955 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
14956 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
14957 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
14958 struct glyph *p = row->glyphs[TEXT_AREA];
14959 struct glyph *p2, *end;
14960
14961 /* Copy the arrow glyphs. */
14962 while (glyph < arrow_end)
14963 *p++ = *glyph++;
14964
14965 /* Throw away padding glyphs. */
14966 p2 = p;
14967 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
14968 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
14969 ++p2;
14970 if (p2 > p)
14971 {
14972 while (p2 < end)
14973 *p++ = *p2++;
14974 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
14975 }
14976 }
14977
14978 overlay_arrow_seen = 1;
14979 it->w->overlay_arrow_bitmap = overlay_arrow_bitmap;
14980 row->overlay_arrow_p = 1;
14981 }
14982
14983 /* Compute pixel dimensions of this line. */
14984 compute_line_metrics (it);
14985
14986 /* Remember the position at which this line ends. */
14987 row->end = it->current;
14988
14989 /* Save fringe bitmaps in this row. */
14990 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
14991 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
14992 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
14993 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
14994
14995 it->left_user_fringe_bitmap = 0;
14996 it->left_user_fringe_face_id = 0;
14997 it->right_user_fringe_bitmap = 0;
14998 it->right_user_fringe_face_id = 0;
14999
15000 /* Maybe set the cursor. */
15001 if (it->w->cursor.vpos < 0
15002 && PT >= MATRIX_ROW_START_CHARPOS (row)
15003 && PT <= MATRIX_ROW_END_CHARPOS (row)
15004 && cursor_row_p (it->w, row))
15005 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15006
15007 /* Highlight trailing whitespace. */
15008 if (!NILP (Vshow_trailing_whitespace))
15009 highlight_trailing_whitespace (it->f, it->glyph_row);
15010
15011 /* Prepare for the next line. This line starts horizontally at (X
15012 HPOS) = (0 0). Vertical positions are incremented. As a
15013 convenience for the caller, IT->glyph_row is set to the next
15014 row to be used. */
15015 it->current_x = it->hpos = 0;
15016 it->current_y += row->height;
15017 ++it->vpos;
15018 ++it->glyph_row;
15019 it->start = it->current;
15020 return row->displays_text_p;
15021 }
15022
15023
15024 \f
15025 /***********************************************************************
15026 Menu Bar
15027 ***********************************************************************/
15028
15029 /* Redisplay the menu bar in the frame for window W.
15030
15031 The menu bar of X frames that don't have X toolkit support is
15032 displayed in a special window W->frame->menu_bar_window.
15033
15034 The menu bar of terminal frames is treated specially as far as
15035 glyph matrices are concerned. Menu bar lines are not part of
15036 windows, so the update is done directly on the frame matrix rows
15037 for the menu bar. */
15038
15039 static void
15040 display_menu_bar (w)
15041 struct window *w;
15042 {
15043 struct frame *f = XFRAME (WINDOW_FRAME (w));
15044 struct it it;
15045 Lisp_Object items;
15046 int i;
15047
15048 /* Don't do all this for graphical frames. */
15049 #ifdef HAVE_NTGUI
15050 if (!NILP (Vwindow_system))
15051 return;
15052 #endif
15053 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15054 if (FRAME_X_P (f))
15055 return;
15056 #endif
15057 #ifdef MAC_OS
15058 if (FRAME_MAC_P (f))
15059 return;
15060 #endif
15061
15062 #ifdef USE_X_TOOLKIT
15063 xassert (!FRAME_WINDOW_P (f));
15064 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
15065 it.first_visible_x = 0;
15066 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15067 #else /* not USE_X_TOOLKIT */
15068 if (FRAME_WINDOW_P (f))
15069 {
15070 /* Menu bar lines are displayed in the desired matrix of the
15071 dummy window menu_bar_window. */
15072 struct window *menu_w;
15073 xassert (WINDOWP (f->menu_bar_window));
15074 menu_w = XWINDOW (f->menu_bar_window);
15075 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
15076 MENU_FACE_ID);
15077 it.first_visible_x = 0;
15078 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15079 }
15080 else
15081 {
15082 /* This is a TTY frame, i.e. character hpos/vpos are used as
15083 pixel x/y. */
15084 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
15085 MENU_FACE_ID);
15086 it.first_visible_x = 0;
15087 it.last_visible_x = FRAME_COLS (f);
15088 }
15089 #endif /* not USE_X_TOOLKIT */
15090
15091 if (! mode_line_inverse_video)
15092 /* Force the menu-bar to be displayed in the default face. */
15093 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15094
15095 /* Clear all rows of the menu bar. */
15096 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
15097 {
15098 struct glyph_row *row = it.glyph_row + i;
15099 clear_glyph_row (row);
15100 row->enabled_p = 1;
15101 row->full_width_p = 1;
15102 }
15103
15104 /* Display all items of the menu bar. */
15105 items = FRAME_MENU_BAR_ITEMS (it.f);
15106 for (i = 0; i < XVECTOR (items)->size; i += 4)
15107 {
15108 Lisp_Object string;
15109
15110 /* Stop at nil string. */
15111 string = AREF (items, i + 1);
15112 if (NILP (string))
15113 break;
15114
15115 /* Remember where item was displayed. */
15116 AREF (items, i + 3) = make_number (it.hpos);
15117
15118 /* Display the item, pad with one space. */
15119 if (it.current_x < it.last_visible_x)
15120 display_string (NULL, string, Qnil, 0, 0, &it,
15121 SCHARS (string) + 1, 0, 0, -1);
15122 }
15123
15124 /* Fill out the line with spaces. */
15125 if (it.current_x < it.last_visible_x)
15126 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15127
15128 /* Compute the total height of the lines. */
15129 compute_line_metrics (&it);
15130 }
15131
15132
15133 \f
15134 /***********************************************************************
15135 Mode Line
15136 ***********************************************************************/
15137
15138 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15139 FORCE is non-zero, redisplay mode lines unconditionally.
15140 Otherwise, redisplay only mode lines that are garbaged. Value is
15141 the number of windows whose mode lines were redisplayed. */
15142
15143 static int
15144 redisplay_mode_lines (window, force)
15145 Lisp_Object window;
15146 int force;
15147 {
15148 int nwindows = 0;
15149
15150 while (!NILP (window))
15151 {
15152 struct window *w = XWINDOW (window);
15153
15154 if (WINDOWP (w->hchild))
15155 nwindows += redisplay_mode_lines (w->hchild, force);
15156 else if (WINDOWP (w->vchild))
15157 nwindows += redisplay_mode_lines (w->vchild, force);
15158 else if (force
15159 || FRAME_GARBAGED_P (XFRAME (w->frame))
15160 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15161 {
15162 struct text_pos lpoint;
15163 struct buffer *old = current_buffer;
15164
15165 /* Set the window's buffer for the mode line display. */
15166 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15167 set_buffer_internal_1 (XBUFFER (w->buffer));
15168
15169 /* Point refers normally to the selected window. For any
15170 other window, set up appropriate value. */
15171 if (!EQ (window, selected_window))
15172 {
15173 struct text_pos pt;
15174
15175 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15176 if (CHARPOS (pt) < BEGV)
15177 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15178 else if (CHARPOS (pt) > (ZV - 1))
15179 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15180 else
15181 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15182 }
15183
15184 /* Display mode lines. */
15185 clear_glyph_matrix (w->desired_matrix);
15186 if (display_mode_lines (w))
15187 {
15188 ++nwindows;
15189 w->must_be_updated_p = 1;
15190 }
15191
15192 /* Restore old settings. */
15193 set_buffer_internal_1 (old);
15194 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15195 }
15196
15197 window = w->next;
15198 }
15199
15200 return nwindows;
15201 }
15202
15203
15204 /* Display the mode and/or top line of window W. Value is the number
15205 of mode lines displayed. */
15206
15207 static int
15208 display_mode_lines (w)
15209 struct window *w;
15210 {
15211 Lisp_Object old_selected_window, old_selected_frame;
15212 int n = 0;
15213
15214 old_selected_frame = selected_frame;
15215 selected_frame = w->frame;
15216 old_selected_window = selected_window;
15217 XSETWINDOW (selected_window, w);
15218
15219 /* These will be set while the mode line specs are processed. */
15220 line_number_displayed = 0;
15221 w->column_number_displayed = Qnil;
15222
15223 if (WINDOW_WANTS_MODELINE_P (w))
15224 {
15225 struct window *sel_w = XWINDOW (old_selected_window);
15226
15227 /* Select mode line face based on the real selected window. */
15228 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15229 current_buffer->mode_line_format);
15230 ++n;
15231 }
15232
15233 if (WINDOW_WANTS_HEADER_LINE_P (w))
15234 {
15235 display_mode_line (w, HEADER_LINE_FACE_ID,
15236 current_buffer->header_line_format);
15237 ++n;
15238 }
15239
15240 selected_frame = old_selected_frame;
15241 selected_window = old_selected_window;
15242 return n;
15243 }
15244
15245
15246 /* Display mode or top line of window W. FACE_ID specifies which line
15247 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15248 FORMAT is the mode line format to display. Value is the pixel
15249 height of the mode line displayed. */
15250
15251 static int
15252 display_mode_line (w, face_id, format)
15253 struct window *w;
15254 enum face_id face_id;
15255 Lisp_Object format;
15256 {
15257 struct it it;
15258 struct face *face;
15259
15260 init_iterator (&it, w, -1, -1, NULL, face_id);
15261 prepare_desired_row (it.glyph_row);
15262
15263 it.glyph_row->mode_line_p = 1;
15264
15265 if (! mode_line_inverse_video)
15266 /* Force the mode-line to be displayed in the default face. */
15267 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15268
15269 /* Temporarily make frame's keyboard the current kboard so that
15270 kboard-local variables in the mode_line_format will get the right
15271 values. */
15272 push_frame_kboard (it.f);
15273 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15274 pop_frame_kboard ();
15275
15276 /* Fill up with spaces. */
15277 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15278
15279 compute_line_metrics (&it);
15280 it.glyph_row->full_width_p = 1;
15281 it.glyph_row->continued_p = 0;
15282 it.glyph_row->truncated_on_left_p = 0;
15283 it.glyph_row->truncated_on_right_p = 0;
15284
15285 /* Make a 3D mode-line have a shadow at its right end. */
15286 face = FACE_FROM_ID (it.f, face_id);
15287 extend_face_to_end_of_line (&it);
15288 if (face->box != FACE_NO_BOX)
15289 {
15290 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15291 + it.glyph_row->used[TEXT_AREA] - 1);
15292 last->right_box_line_p = 1;
15293 }
15294
15295 return it.glyph_row->height;
15296 }
15297
15298 /* Alist that caches the results of :propertize.
15299 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
15300 Lisp_Object mode_line_proptrans_alist;
15301
15302 /* List of strings making up the mode-line. */
15303 Lisp_Object mode_line_string_list;
15304
15305 /* Base face property when building propertized mode line string. */
15306 static Lisp_Object mode_line_string_face;
15307 static Lisp_Object mode_line_string_face_prop;
15308
15309
15310 /* Contribute ELT to the mode line for window IT->w. How it
15311 translates into text depends on its data type.
15312
15313 IT describes the display environment in which we display, as usual.
15314
15315 DEPTH is the depth in recursion. It is used to prevent
15316 infinite recursion here.
15317
15318 FIELD_WIDTH is the number of characters the display of ELT should
15319 occupy in the mode line, and PRECISION is the maximum number of
15320 characters to display from ELT's representation. See
15321 display_string for details.
15322
15323 Returns the hpos of the end of the text generated by ELT.
15324
15325 PROPS is a property list to add to any string we encounter.
15326
15327 If RISKY is nonzero, remove (disregard) any properties in any string
15328 we encounter, and ignore :eval and :propertize.
15329
15330 If the global variable `frame_title_ptr' is non-NULL, then the output
15331 is passed to `store_frame_title' instead of `display_string'. */
15332
15333 static int
15334 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15335 struct it *it;
15336 int depth;
15337 int field_width, precision;
15338 Lisp_Object elt, props;
15339 int risky;
15340 {
15341 int n = 0, field, prec;
15342 int literal = 0;
15343
15344 tail_recurse:
15345 if (depth > 100)
15346 elt = build_string ("*too-deep*");
15347
15348 depth++;
15349
15350 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15351 {
15352 case Lisp_String:
15353 {
15354 /* A string: output it and check for %-constructs within it. */
15355 unsigned char c;
15356 const unsigned char *this, *lisp_string;
15357
15358 if (!NILP (props) || risky)
15359 {
15360 Lisp_Object oprops, aelt;
15361 oprops = Ftext_properties_at (make_number (0), elt);
15362
15363 /* If the starting string's properties are not what
15364 we want, translate the string. Also, if the string
15365 is risky, do that anyway. */
15366
15367 if (NILP (Fequal (props, oprops)) || risky)
15368 {
15369 /* If the starting string has properties,
15370 merge the specified ones onto the existing ones. */
15371 if (! NILP (oprops) && !risky)
15372 {
15373 Lisp_Object tem;
15374
15375 oprops = Fcopy_sequence (oprops);
15376 tem = props;
15377 while (CONSP (tem))
15378 {
15379 oprops = Fplist_put (oprops, XCAR (tem),
15380 XCAR (XCDR (tem)));
15381 tem = XCDR (XCDR (tem));
15382 }
15383 props = oprops;
15384 }
15385
15386 aelt = Fassoc (elt, mode_line_proptrans_alist);
15387 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15388 {
15389 mode_line_proptrans_alist
15390 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15391 elt = XCAR (aelt);
15392 }
15393 else
15394 {
15395 Lisp_Object tem;
15396
15397 elt = Fcopy_sequence (elt);
15398 Fset_text_properties (make_number (0), Flength (elt),
15399 props, elt);
15400 /* Add this item to mode_line_proptrans_alist. */
15401 mode_line_proptrans_alist
15402 = Fcons (Fcons (elt, props),
15403 mode_line_proptrans_alist);
15404 /* Truncate mode_line_proptrans_alist
15405 to at most 50 elements. */
15406 tem = Fnthcdr (make_number (50),
15407 mode_line_proptrans_alist);
15408 if (! NILP (tem))
15409 XSETCDR (tem, Qnil);
15410 }
15411 }
15412 }
15413
15414 this = SDATA (elt);
15415 lisp_string = this;
15416
15417 if (literal)
15418 {
15419 prec = precision - n;
15420 if (frame_title_ptr)
15421 n += store_frame_title (SDATA (elt), -1, prec);
15422 else if (!NILP (mode_line_string_list))
15423 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15424 else
15425 n += display_string (NULL, elt, Qnil, 0, 0, it,
15426 0, prec, 0, STRING_MULTIBYTE (elt));
15427
15428 break;
15429 }
15430
15431 while ((precision <= 0 || n < precision)
15432 && *this
15433 && (frame_title_ptr
15434 || !NILP (mode_line_string_list)
15435 || it->current_x < it->last_visible_x))
15436 {
15437 const unsigned char *last = this;
15438
15439 /* Advance to end of string or next format specifier. */
15440 while ((c = *this++) != '\0' && c != '%')
15441 ;
15442
15443 if (this - 1 != last)
15444 {
15445 /* Output to end of string or up to '%'. Field width
15446 is length of string. Don't output more than
15447 PRECISION allows us. */
15448 --this;
15449
15450 prec = chars_in_text (last, this - last);
15451 if (precision > 0 && prec > precision - n)
15452 prec = precision - n;
15453
15454 if (frame_title_ptr)
15455 n += store_frame_title (last, 0, prec);
15456 else if (!NILP (mode_line_string_list))
15457 {
15458 int bytepos = last - lisp_string;
15459 int charpos = string_byte_to_char (elt, bytepos);
15460 n += store_mode_line_string (NULL,
15461 Fsubstring (elt, make_number (charpos),
15462 make_number (charpos + prec)),
15463 0, 0, 0, Qnil);
15464 }
15465 else
15466 {
15467 int bytepos = last - lisp_string;
15468 int charpos = string_byte_to_char (elt, bytepos);
15469 n += display_string (NULL, elt, Qnil, 0, charpos,
15470 it, 0, prec, 0,
15471 STRING_MULTIBYTE (elt));
15472 }
15473 }
15474 else /* c == '%' */
15475 {
15476 const unsigned char *percent_position = this;
15477
15478 /* Get the specified minimum width. Zero means
15479 don't pad. */
15480 field = 0;
15481 while ((c = *this++) >= '0' && c <= '9')
15482 field = field * 10 + c - '0';
15483
15484 /* Don't pad beyond the total padding allowed. */
15485 if (field_width - n > 0 && field > field_width - n)
15486 field = field_width - n;
15487
15488 /* Note that either PRECISION <= 0 or N < PRECISION. */
15489 prec = precision - n;
15490
15491 if (c == 'M')
15492 n += display_mode_element (it, depth, field, prec,
15493 Vglobal_mode_string, props,
15494 risky);
15495 else if (c != 0)
15496 {
15497 int multibyte;
15498 int bytepos, charpos;
15499 unsigned char *spec;
15500
15501 bytepos = percent_position - lisp_string;
15502 charpos = (STRING_MULTIBYTE (elt)
15503 ? string_byte_to_char (elt, bytepos)
15504 : bytepos);
15505
15506 spec
15507 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15508
15509 if (frame_title_ptr)
15510 n += store_frame_title (spec, field, prec);
15511 else if (!NILP (mode_line_string_list))
15512 {
15513 int len = strlen (spec);
15514 Lisp_Object tem = make_string (spec, len);
15515 props = Ftext_properties_at (make_number (charpos), elt);
15516 /* Should only keep face property in props */
15517 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15518 }
15519 else
15520 {
15521 int nglyphs_before, nwritten;
15522
15523 nglyphs_before = it->glyph_row->used[TEXT_AREA];
15524 nwritten = display_string (spec, Qnil, elt,
15525 charpos, 0, it,
15526 field, prec, 0,
15527 multibyte);
15528
15529 /* Assign to the glyphs written above the
15530 string where the `%x' came from, position
15531 of the `%'. */
15532 if (nwritten > 0)
15533 {
15534 struct glyph *glyph
15535 = (it->glyph_row->glyphs[TEXT_AREA]
15536 + nglyphs_before);
15537 int i;
15538
15539 for (i = 0; i < nwritten; ++i)
15540 {
15541 glyph[i].object = elt;
15542 glyph[i].charpos = charpos;
15543 }
15544
15545 n += nwritten;
15546 }
15547 }
15548 }
15549 else /* c == 0 */
15550 break;
15551 }
15552 }
15553 }
15554 break;
15555
15556 case Lisp_Symbol:
15557 /* A symbol: process the value of the symbol recursively
15558 as if it appeared here directly. Avoid error if symbol void.
15559 Special case: if value of symbol is a string, output the string
15560 literally. */
15561 {
15562 register Lisp_Object tem;
15563
15564 /* If the variable is not marked as risky to set
15565 then its contents are risky to use. */
15566 if (NILP (Fget (elt, Qrisky_local_variable)))
15567 risky = 1;
15568
15569 tem = Fboundp (elt);
15570 if (!NILP (tem))
15571 {
15572 tem = Fsymbol_value (elt);
15573 /* If value is a string, output that string literally:
15574 don't check for % within it. */
15575 if (STRINGP (tem))
15576 literal = 1;
15577
15578 if (!EQ (tem, elt))
15579 {
15580 /* Give up right away for nil or t. */
15581 elt = tem;
15582 goto tail_recurse;
15583 }
15584 }
15585 }
15586 break;
15587
15588 case Lisp_Cons:
15589 {
15590 register Lisp_Object car, tem;
15591
15592 /* A cons cell: five distinct cases.
15593 If first element is :eval or :propertize, do something special.
15594 If first element is a string or a cons, process all the elements
15595 and effectively concatenate them.
15596 If first element is a negative number, truncate displaying cdr to
15597 at most that many characters. If positive, pad (with spaces)
15598 to at least that many characters.
15599 If first element is a symbol, process the cadr or caddr recursively
15600 according to whether the symbol's value is non-nil or nil. */
15601 car = XCAR (elt);
15602 if (EQ (car, QCeval))
15603 {
15604 /* An element of the form (:eval FORM) means evaluate FORM
15605 and use the result as mode line elements. */
15606
15607 if (risky)
15608 break;
15609
15610 if (CONSP (XCDR (elt)))
15611 {
15612 Lisp_Object spec;
15613 spec = safe_eval (XCAR (XCDR (elt)));
15614 n += display_mode_element (it, depth, field_width - n,
15615 precision - n, spec, props,
15616 risky);
15617 }
15618 }
15619 else if (EQ (car, QCpropertize))
15620 {
15621 /* An element of the form (:propertize ELT PROPS...)
15622 means display ELT but applying properties PROPS. */
15623
15624 if (risky)
15625 break;
15626
15627 if (CONSP (XCDR (elt)))
15628 n += display_mode_element (it, depth, field_width - n,
15629 precision - n, XCAR (XCDR (elt)),
15630 XCDR (XCDR (elt)), risky);
15631 }
15632 else if (SYMBOLP (car))
15633 {
15634 tem = Fboundp (car);
15635 elt = XCDR (elt);
15636 if (!CONSP (elt))
15637 goto invalid;
15638 /* elt is now the cdr, and we know it is a cons cell.
15639 Use its car if CAR has a non-nil value. */
15640 if (!NILP (tem))
15641 {
15642 tem = Fsymbol_value (car);
15643 if (!NILP (tem))
15644 {
15645 elt = XCAR (elt);
15646 goto tail_recurse;
15647 }
15648 }
15649 /* Symbol's value is nil (or symbol is unbound)
15650 Get the cddr of the original list
15651 and if possible find the caddr and use that. */
15652 elt = XCDR (elt);
15653 if (NILP (elt))
15654 break;
15655 else if (!CONSP (elt))
15656 goto invalid;
15657 elt = XCAR (elt);
15658 goto tail_recurse;
15659 }
15660 else if (INTEGERP (car))
15661 {
15662 register int lim = XINT (car);
15663 elt = XCDR (elt);
15664 if (lim < 0)
15665 {
15666 /* Negative int means reduce maximum width. */
15667 if (precision <= 0)
15668 precision = -lim;
15669 else
15670 precision = min (precision, -lim);
15671 }
15672 else if (lim > 0)
15673 {
15674 /* Padding specified. Don't let it be more than
15675 current maximum. */
15676 if (precision > 0)
15677 lim = min (precision, lim);
15678
15679 /* If that's more padding than already wanted, queue it.
15680 But don't reduce padding already specified even if
15681 that is beyond the current truncation point. */
15682 field_width = max (lim, field_width);
15683 }
15684 goto tail_recurse;
15685 }
15686 else if (STRINGP (car) || CONSP (car))
15687 {
15688 register int limit = 50;
15689 /* Limit is to protect against circular lists. */
15690 while (CONSP (elt)
15691 && --limit > 0
15692 && (precision <= 0 || n < precision))
15693 {
15694 n += display_mode_element (it, depth, field_width - n,
15695 precision - n, XCAR (elt),
15696 props, risky);
15697 elt = XCDR (elt);
15698 }
15699 }
15700 }
15701 break;
15702
15703 default:
15704 invalid:
15705 elt = build_string ("*invalid*");
15706 goto tail_recurse;
15707 }
15708
15709 /* Pad to FIELD_WIDTH. */
15710 if (field_width > 0 && n < field_width)
15711 {
15712 if (frame_title_ptr)
15713 n += store_frame_title ("", field_width - n, 0);
15714 else if (!NILP (mode_line_string_list))
15715 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
15716 else
15717 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
15718 0, 0, 0);
15719 }
15720
15721 return n;
15722 }
15723
15724 /* Store a mode-line string element in mode_line_string_list.
15725
15726 If STRING is non-null, display that C string. Otherwise, the Lisp
15727 string LISP_STRING is displayed.
15728
15729 FIELD_WIDTH is the minimum number of output glyphs to produce.
15730 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15731 with spaces. FIELD_WIDTH <= 0 means don't pad.
15732
15733 PRECISION is the maximum number of characters to output from
15734 STRING. PRECISION <= 0 means don't truncate the string.
15735
15736 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
15737 properties to the string.
15738
15739 PROPS are the properties to add to the string.
15740 The mode_line_string_face face property is always added to the string.
15741 */
15742
15743 static int
15744 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
15745 char *string;
15746 Lisp_Object lisp_string;
15747 int copy_string;
15748 int field_width;
15749 int precision;
15750 Lisp_Object props;
15751 {
15752 int len;
15753 int n = 0;
15754
15755 if (string != NULL)
15756 {
15757 len = strlen (string);
15758 if (precision > 0 && len > precision)
15759 len = precision;
15760 lisp_string = make_string (string, len);
15761 if (NILP (props))
15762 props = mode_line_string_face_prop;
15763 else if (!NILP (mode_line_string_face))
15764 {
15765 Lisp_Object face = Fplist_get (props, Qface);
15766 props = Fcopy_sequence (props);
15767 if (NILP (face))
15768 face = mode_line_string_face;
15769 else
15770 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15771 props = Fplist_put (props, Qface, face);
15772 }
15773 Fadd_text_properties (make_number (0), make_number (len),
15774 props, lisp_string);
15775 }
15776 else
15777 {
15778 len = XFASTINT (Flength (lisp_string));
15779 if (precision > 0 && len > precision)
15780 {
15781 len = precision;
15782 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
15783 precision = -1;
15784 }
15785 if (!NILP (mode_line_string_face))
15786 {
15787 Lisp_Object face;
15788 if (NILP (props))
15789 props = Ftext_properties_at (make_number (0), lisp_string);
15790 face = Fplist_get (props, Qface);
15791 if (NILP (face))
15792 face = mode_line_string_face;
15793 else
15794 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15795 props = Fcons (Qface, Fcons (face, Qnil));
15796 if (copy_string)
15797 lisp_string = Fcopy_sequence (lisp_string);
15798 }
15799 if (!NILP (props))
15800 Fadd_text_properties (make_number (0), make_number (len),
15801 props, lisp_string);
15802 }
15803
15804 if (len > 0)
15805 {
15806 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15807 n += len;
15808 }
15809
15810 if (field_width > len)
15811 {
15812 field_width -= len;
15813 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
15814 if (!NILP (props))
15815 Fadd_text_properties (make_number (0), make_number (field_width),
15816 props, lisp_string);
15817 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15818 n += field_width;
15819 }
15820
15821 return n;
15822 }
15823
15824
15825 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
15826 0, 4, 0,
15827 doc: /* Return the mode-line of selected window as a string.
15828 First optional arg FORMAT specifies a different format string (see
15829 `mode-line-format' for details) to use. If FORMAT is t, return
15830 the buffer's header-line. Second optional arg WINDOW specifies a
15831 different window to use as the context for the formatting.
15832 If third optional arg NO-PROPS is non-nil, string is not propertized.
15833 Fourth optional arg BUFFER specifies which buffer to use. */)
15834 (format, window, no_props, buffer)
15835 Lisp_Object format, window, no_props, buffer;
15836 {
15837 struct it it;
15838 int len;
15839 struct window *w;
15840 struct buffer *old_buffer = NULL;
15841 enum face_id face_id = DEFAULT_FACE_ID;
15842
15843 if (NILP (window))
15844 window = selected_window;
15845 CHECK_WINDOW (window);
15846 w = XWINDOW (window);
15847
15848 if (NILP (buffer))
15849 buffer = w->buffer;
15850
15851 CHECK_BUFFER (buffer);
15852
15853 if (XBUFFER (buffer) != current_buffer)
15854 {
15855 old_buffer = current_buffer;
15856 set_buffer_internal_1 (XBUFFER (buffer));
15857 }
15858
15859 if (NILP (format) || EQ (format, Qt))
15860 {
15861 face_id = (NILP (format)
15862 ? CURRENT_MODE_LINE_FACE_ID (w)
15863 : HEADER_LINE_FACE_ID);
15864 format = (NILP (format)
15865 ? current_buffer->mode_line_format
15866 : current_buffer->header_line_format);
15867 }
15868
15869 init_iterator (&it, w, -1, -1, NULL, face_id);
15870
15871 if (NILP (no_props))
15872 {
15873 mode_line_string_face
15874 = (face_id == MODE_LINE_FACE_ID ? Qmode_line
15875 : face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive
15876 : face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
15877
15878 mode_line_string_face_prop
15879 = (NILP (mode_line_string_face) ? Qnil
15880 : Fcons (Qface, Fcons (mode_line_string_face, Qnil)));
15881
15882 /* We need a dummy last element in mode_line_string_list to
15883 indicate we are building the propertized mode-line string.
15884 Using mode_line_string_face_prop here GC protects it. */
15885 mode_line_string_list
15886 = Fcons (mode_line_string_face_prop, Qnil);
15887 frame_title_ptr = NULL;
15888 }
15889 else
15890 {
15891 mode_line_string_face_prop = Qnil;
15892 mode_line_string_list = Qnil;
15893 frame_title_ptr = frame_title_buf;
15894 }
15895
15896 push_frame_kboard (it.f);
15897 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15898 pop_frame_kboard ();
15899
15900 if (old_buffer)
15901 set_buffer_internal_1 (old_buffer);
15902
15903 if (NILP (no_props))
15904 {
15905 Lisp_Object str;
15906 mode_line_string_list = Fnreverse (mode_line_string_list);
15907 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
15908 make_string ("", 0));
15909 mode_line_string_face_prop = Qnil;
15910 mode_line_string_list = Qnil;
15911 return str;
15912 }
15913
15914 len = frame_title_ptr - frame_title_buf;
15915 if (len > 0 && frame_title_ptr[-1] == '-')
15916 {
15917 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
15918 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
15919 ;
15920 frame_title_ptr += 3; /* restore last non-dash + two dashes */
15921 if (len > frame_title_ptr - frame_title_buf)
15922 len = frame_title_ptr - frame_title_buf;
15923 }
15924
15925 frame_title_ptr = NULL;
15926 return make_string (frame_title_buf, len);
15927 }
15928
15929 /* Write a null-terminated, right justified decimal representation of
15930 the positive integer D to BUF using a minimal field width WIDTH. */
15931
15932 static void
15933 pint2str (buf, width, d)
15934 register char *buf;
15935 register int width;
15936 register int d;
15937 {
15938 register char *p = buf;
15939
15940 if (d <= 0)
15941 *p++ = '0';
15942 else
15943 {
15944 while (d > 0)
15945 {
15946 *p++ = d % 10 + '0';
15947 d /= 10;
15948 }
15949 }
15950
15951 for (width -= (int) (p - buf); width > 0; --width)
15952 *p++ = ' ';
15953 *p-- = '\0';
15954 while (p > buf)
15955 {
15956 d = *buf;
15957 *buf++ = *p;
15958 *p-- = d;
15959 }
15960 }
15961
15962 /* Write a null-terminated, right justified decimal and "human
15963 readable" representation of the nonnegative integer D to BUF using
15964 a minimal field width WIDTH. D should be smaller than 999.5e24. */
15965
15966 static const char power_letter[] =
15967 {
15968 0, /* not used */
15969 'k', /* kilo */
15970 'M', /* mega */
15971 'G', /* giga */
15972 'T', /* tera */
15973 'P', /* peta */
15974 'E', /* exa */
15975 'Z', /* zetta */
15976 'Y' /* yotta */
15977 };
15978
15979 static void
15980 pint2hrstr (buf, width, d)
15981 char *buf;
15982 int width;
15983 int d;
15984 {
15985 /* We aim to represent the nonnegative integer D as
15986 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
15987 int quotient = d;
15988 int remainder = 0;
15989 /* -1 means: do not use TENTHS. */
15990 int tenths = -1;
15991 int exponent = 0;
15992
15993 /* Length of QUOTIENT.TENTHS as a string. */
15994 int length;
15995
15996 char * psuffix;
15997 char * p;
15998
15999 if (1000 <= quotient)
16000 {
16001 /* Scale to the appropriate EXPONENT. */
16002 do
16003 {
16004 remainder = quotient % 1000;
16005 quotient /= 1000;
16006 exponent++;
16007 }
16008 while (1000 <= quotient);
16009
16010 /* Round to nearest and decide whether to use TENTHS or not. */
16011 if (quotient <= 9)
16012 {
16013 tenths = remainder / 100;
16014 if (50 <= remainder % 100)
16015 if (tenths < 9)
16016 tenths++;
16017 else
16018 {
16019 quotient++;
16020 if (quotient == 10)
16021 tenths = -1;
16022 else
16023 tenths = 0;
16024 }
16025 }
16026 else
16027 if (500 <= remainder)
16028 if (quotient < 999)
16029 quotient++;
16030 else
16031 {
16032 quotient = 1;
16033 exponent++;
16034 tenths = 0;
16035 }
16036 }
16037
16038 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
16039 if (tenths == -1 && quotient <= 99)
16040 if (quotient <= 9)
16041 length = 1;
16042 else
16043 length = 2;
16044 else
16045 length = 3;
16046 p = psuffix = buf + max (width, length);
16047
16048 /* Print EXPONENT. */
16049 if (exponent)
16050 *psuffix++ = power_letter[exponent];
16051 *psuffix = '\0';
16052
16053 /* Print TENTHS. */
16054 if (tenths >= 0)
16055 {
16056 *--p = '0' + tenths;
16057 *--p = '.';
16058 }
16059
16060 /* Print QUOTIENT. */
16061 do
16062 {
16063 int digit = quotient % 10;
16064 *--p = '0' + digit;
16065 }
16066 while ((quotient /= 10) != 0);
16067
16068 /* Print leading spaces. */
16069 while (buf < p)
16070 *--p = ' ';
16071 }
16072
16073 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
16074 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
16075 type of CODING_SYSTEM. Return updated pointer into BUF. */
16076
16077 static unsigned char invalid_eol_type[] = "(*invalid*)";
16078
16079 static char *
16080 decode_mode_spec_coding (coding_system, buf, eol_flag)
16081 Lisp_Object coding_system;
16082 register char *buf;
16083 int eol_flag;
16084 {
16085 Lisp_Object val;
16086 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
16087 const unsigned char *eol_str;
16088 int eol_str_len;
16089 /* The EOL conversion we are using. */
16090 Lisp_Object eoltype;
16091
16092 val = Fget (coding_system, Qcoding_system);
16093 eoltype = Qnil;
16094
16095 if (!VECTORP (val)) /* Not yet decided. */
16096 {
16097 if (multibyte)
16098 *buf++ = '-';
16099 if (eol_flag)
16100 eoltype = eol_mnemonic_undecided;
16101 /* Don't mention EOL conversion if it isn't decided. */
16102 }
16103 else
16104 {
16105 Lisp_Object eolvalue;
16106
16107 eolvalue = Fget (coding_system, Qeol_type);
16108
16109 if (multibyte)
16110 *buf++ = XFASTINT (AREF (val, 1));
16111
16112 if (eol_flag)
16113 {
16114 /* The EOL conversion that is normal on this system. */
16115
16116 if (NILP (eolvalue)) /* Not yet decided. */
16117 eoltype = eol_mnemonic_undecided;
16118 else if (VECTORP (eolvalue)) /* Not yet decided. */
16119 eoltype = eol_mnemonic_undecided;
16120 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
16121 eoltype = (XFASTINT (eolvalue) == 0
16122 ? eol_mnemonic_unix
16123 : (XFASTINT (eolvalue) == 1
16124 ? eol_mnemonic_dos : eol_mnemonic_mac));
16125 }
16126 }
16127
16128 if (eol_flag)
16129 {
16130 /* Mention the EOL conversion if it is not the usual one. */
16131 if (STRINGP (eoltype))
16132 {
16133 eol_str = SDATA (eoltype);
16134 eol_str_len = SBYTES (eoltype);
16135 }
16136 else if (INTEGERP (eoltype)
16137 && CHAR_VALID_P (XINT (eoltype), 0))
16138 {
16139 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16140 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16141 eol_str = tmp;
16142 }
16143 else
16144 {
16145 eol_str = invalid_eol_type;
16146 eol_str_len = sizeof (invalid_eol_type) - 1;
16147 }
16148 bcopy (eol_str, buf, eol_str_len);
16149 buf += eol_str_len;
16150 }
16151
16152 return buf;
16153 }
16154
16155 /* Return a string for the output of a mode line %-spec for window W,
16156 generated by character C. PRECISION >= 0 means don't return a
16157 string longer than that value. FIELD_WIDTH > 0 means pad the
16158 string returned with spaces to that value. Return 1 in *MULTIBYTE
16159 if the result is multibyte text.
16160
16161 Note we operate on the current buffer for most purposes,
16162 the exception being w->base_line_pos. */
16163
16164 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16165
16166 static char *
16167 decode_mode_spec (w, c, field_width, precision, multibyte)
16168 struct window *w;
16169 register int c;
16170 int field_width, precision;
16171 int *multibyte;
16172 {
16173 Lisp_Object obj;
16174 struct frame *f = XFRAME (WINDOW_FRAME (w));
16175 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16176 struct buffer *b = current_buffer;
16177
16178 obj = Qnil;
16179 *multibyte = 0;
16180
16181 switch (c)
16182 {
16183 case '*':
16184 if (!NILP (b->read_only))
16185 return "%";
16186 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16187 return "*";
16188 return "-";
16189
16190 case '+':
16191 /* This differs from %* only for a modified read-only buffer. */
16192 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16193 return "*";
16194 if (!NILP (b->read_only))
16195 return "%";
16196 return "-";
16197
16198 case '&':
16199 /* This differs from %* in ignoring read-only-ness. */
16200 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16201 return "*";
16202 return "-";
16203
16204 case '%':
16205 return "%";
16206
16207 case '[':
16208 {
16209 int i;
16210 char *p;
16211
16212 if (command_loop_level > 5)
16213 return "[[[... ";
16214 p = decode_mode_spec_buf;
16215 for (i = 0; i < command_loop_level; i++)
16216 *p++ = '[';
16217 *p = 0;
16218 return decode_mode_spec_buf;
16219 }
16220
16221 case ']':
16222 {
16223 int i;
16224 char *p;
16225
16226 if (command_loop_level > 5)
16227 return " ...]]]";
16228 p = decode_mode_spec_buf;
16229 for (i = 0; i < command_loop_level; i++)
16230 *p++ = ']';
16231 *p = 0;
16232 return decode_mode_spec_buf;
16233 }
16234
16235 case '-':
16236 {
16237 register int i;
16238
16239 /* Let lots_of_dashes be a string of infinite length. */
16240 if (!NILP (mode_line_string_list))
16241 return "--";
16242 if (field_width <= 0
16243 || field_width > sizeof (lots_of_dashes))
16244 {
16245 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16246 decode_mode_spec_buf[i] = '-';
16247 decode_mode_spec_buf[i] = '\0';
16248 return decode_mode_spec_buf;
16249 }
16250 else
16251 return lots_of_dashes;
16252 }
16253
16254 case 'b':
16255 obj = b->name;
16256 break;
16257
16258 case 'c':
16259 {
16260 int col = (int) current_column (); /* iftc */
16261 w->column_number_displayed = make_number (col);
16262 pint2str (decode_mode_spec_buf, field_width, col);
16263 return decode_mode_spec_buf;
16264 }
16265
16266 case 'F':
16267 /* %F displays the frame name. */
16268 if (!NILP (f->title))
16269 return (char *) SDATA (f->title);
16270 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16271 return (char *) SDATA (f->name);
16272 return "Emacs";
16273
16274 case 'f':
16275 obj = b->filename;
16276 break;
16277
16278 case 'i':
16279 {
16280 int size = ZV - BEGV;
16281 pint2str (decode_mode_spec_buf, field_width, size);
16282 return decode_mode_spec_buf;
16283 }
16284
16285 case 'I':
16286 {
16287 int size = ZV - BEGV;
16288 pint2hrstr (decode_mode_spec_buf, field_width, size);
16289 return decode_mode_spec_buf;
16290 }
16291
16292 case 'l':
16293 {
16294 int startpos = XMARKER (w->start)->charpos;
16295 int startpos_byte = marker_byte_position (w->start);
16296 int line, linepos, linepos_byte, topline;
16297 int nlines, junk;
16298 int height = WINDOW_TOTAL_LINES (w);
16299
16300 /* If we decided that this buffer isn't suitable for line numbers,
16301 don't forget that too fast. */
16302 if (EQ (w->base_line_pos, w->buffer))
16303 goto no_value;
16304 /* But do forget it, if the window shows a different buffer now. */
16305 else if (BUFFERP (w->base_line_pos))
16306 w->base_line_pos = Qnil;
16307
16308 /* If the buffer is very big, don't waste time. */
16309 if (INTEGERP (Vline_number_display_limit)
16310 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16311 {
16312 w->base_line_pos = Qnil;
16313 w->base_line_number = Qnil;
16314 goto no_value;
16315 }
16316
16317 if (!NILP (w->base_line_number)
16318 && !NILP (w->base_line_pos)
16319 && XFASTINT (w->base_line_pos) <= startpos)
16320 {
16321 line = XFASTINT (w->base_line_number);
16322 linepos = XFASTINT (w->base_line_pos);
16323 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16324 }
16325 else
16326 {
16327 line = 1;
16328 linepos = BUF_BEGV (b);
16329 linepos_byte = BUF_BEGV_BYTE (b);
16330 }
16331
16332 /* Count lines from base line to window start position. */
16333 nlines = display_count_lines (linepos, linepos_byte,
16334 startpos_byte,
16335 startpos, &junk);
16336
16337 topline = nlines + line;
16338
16339 /* Determine a new base line, if the old one is too close
16340 or too far away, or if we did not have one.
16341 "Too close" means it's plausible a scroll-down would
16342 go back past it. */
16343 if (startpos == BUF_BEGV (b))
16344 {
16345 w->base_line_number = make_number (topline);
16346 w->base_line_pos = make_number (BUF_BEGV (b));
16347 }
16348 else if (nlines < height + 25 || nlines > height * 3 + 50
16349 || linepos == BUF_BEGV (b))
16350 {
16351 int limit = BUF_BEGV (b);
16352 int limit_byte = BUF_BEGV_BYTE (b);
16353 int position;
16354 int distance = (height * 2 + 30) * line_number_display_limit_width;
16355
16356 if (startpos - distance > limit)
16357 {
16358 limit = startpos - distance;
16359 limit_byte = CHAR_TO_BYTE (limit);
16360 }
16361
16362 nlines = display_count_lines (startpos, startpos_byte,
16363 limit_byte,
16364 - (height * 2 + 30),
16365 &position);
16366 /* If we couldn't find the lines we wanted within
16367 line_number_display_limit_width chars per line,
16368 give up on line numbers for this window. */
16369 if (position == limit_byte && limit == startpos - distance)
16370 {
16371 w->base_line_pos = w->buffer;
16372 w->base_line_number = Qnil;
16373 goto no_value;
16374 }
16375
16376 w->base_line_number = make_number (topline - nlines);
16377 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
16378 }
16379
16380 /* Now count lines from the start pos to point. */
16381 nlines = display_count_lines (startpos, startpos_byte,
16382 PT_BYTE, PT, &junk);
16383
16384 /* Record that we did display the line number. */
16385 line_number_displayed = 1;
16386
16387 /* Make the string to show. */
16388 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
16389 return decode_mode_spec_buf;
16390 no_value:
16391 {
16392 char* p = decode_mode_spec_buf;
16393 int pad = field_width - 2;
16394 while (pad-- > 0)
16395 *p++ = ' ';
16396 *p++ = '?';
16397 *p++ = '?';
16398 *p = '\0';
16399 return decode_mode_spec_buf;
16400 }
16401 }
16402 break;
16403
16404 case 'm':
16405 obj = b->mode_name;
16406 break;
16407
16408 case 'n':
16409 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
16410 return " Narrow";
16411 break;
16412
16413 case 'p':
16414 {
16415 int pos = marker_position (w->start);
16416 int total = BUF_ZV (b) - BUF_BEGV (b);
16417
16418 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
16419 {
16420 if (pos <= BUF_BEGV (b))
16421 return "All";
16422 else
16423 return "Bottom";
16424 }
16425 else if (pos <= BUF_BEGV (b))
16426 return "Top";
16427 else
16428 {
16429 if (total > 1000000)
16430 /* Do it differently for a large value, to avoid overflow. */
16431 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16432 else
16433 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
16434 /* We can't normally display a 3-digit number,
16435 so get us a 2-digit number that is close. */
16436 if (total == 100)
16437 total = 99;
16438 sprintf (decode_mode_spec_buf, "%2d%%", total);
16439 return decode_mode_spec_buf;
16440 }
16441 }
16442
16443 /* Display percentage of size above the bottom of the screen. */
16444 case 'P':
16445 {
16446 int toppos = marker_position (w->start);
16447 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16448 int total = BUF_ZV (b) - BUF_BEGV (b);
16449
16450 if (botpos >= BUF_ZV (b))
16451 {
16452 if (toppos <= BUF_BEGV (b))
16453 return "All";
16454 else
16455 return "Bottom";
16456 }
16457 else
16458 {
16459 if (total > 1000000)
16460 /* Do it differently for a large value, to avoid overflow. */
16461 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16462 else
16463 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
16464 /* We can't normally display a 3-digit number,
16465 so get us a 2-digit number that is close. */
16466 if (total == 100)
16467 total = 99;
16468 if (toppos <= BUF_BEGV (b))
16469 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
16470 else
16471 sprintf (decode_mode_spec_buf, "%2d%%", total);
16472 return decode_mode_spec_buf;
16473 }
16474 }
16475
16476 case 's':
16477 /* status of process */
16478 obj = Fget_buffer_process (Fcurrent_buffer ());
16479 if (NILP (obj))
16480 return "no process";
16481 #ifdef subprocesses
16482 obj = Fsymbol_name (Fprocess_status (obj));
16483 #endif
16484 break;
16485
16486 case 't': /* indicate TEXT or BINARY */
16487 #ifdef MODE_LINE_BINARY_TEXT
16488 return MODE_LINE_BINARY_TEXT (b);
16489 #else
16490 return "T";
16491 #endif
16492
16493 case 'z':
16494 /* coding-system (not including end-of-line format) */
16495 case 'Z':
16496 /* coding-system (including end-of-line type) */
16497 {
16498 int eol_flag = (c == 'Z');
16499 char *p = decode_mode_spec_buf;
16500
16501 if (! FRAME_WINDOW_P (f))
16502 {
16503 /* No need to mention EOL here--the terminal never needs
16504 to do EOL conversion. */
16505 p = decode_mode_spec_coding (FRAME_KEYBOARD_CODING (f)->symbol, p, 0);
16506 p = decode_mode_spec_coding (FRAME_TERMINAL_CODING (f)->symbol, p, 0);
16507 }
16508 p = decode_mode_spec_coding (b->buffer_file_coding_system,
16509 p, eol_flag);
16510
16511 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
16512 #ifdef subprocesses
16513 obj = Fget_buffer_process (Fcurrent_buffer ());
16514 if (PROCESSP (obj))
16515 {
16516 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16517 p, eol_flag);
16518 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16519 p, eol_flag);
16520 }
16521 #endif /* subprocesses */
16522 #endif /* 0 */
16523 *p = 0;
16524 return decode_mode_spec_buf;
16525 }
16526 }
16527
16528 if (STRINGP (obj))
16529 {
16530 *multibyte = STRING_MULTIBYTE (obj);
16531 return (char *) SDATA (obj);
16532 }
16533 else
16534 return "";
16535 }
16536
16537
16538 /* Count up to COUNT lines starting from START / START_BYTE.
16539 But don't go beyond LIMIT_BYTE.
16540 Return the number of lines thus found (always nonnegative).
16541
16542 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
16543
16544 static int
16545 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16546 int start, start_byte, limit_byte, count;
16547 int *byte_pos_ptr;
16548 {
16549 register unsigned char *cursor;
16550 unsigned char *base;
16551
16552 register int ceiling;
16553 register unsigned char *ceiling_addr;
16554 int orig_count = count;
16555
16556 /* If we are not in selective display mode,
16557 check only for newlines. */
16558 int selective_display = (!NILP (current_buffer->selective_display)
16559 && !INTEGERP (current_buffer->selective_display));
16560
16561 if (count > 0)
16562 {
16563 while (start_byte < limit_byte)
16564 {
16565 ceiling = BUFFER_CEILING_OF (start_byte);
16566 ceiling = min (limit_byte - 1, ceiling);
16567 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16568 base = (cursor = BYTE_POS_ADDR (start_byte));
16569 while (1)
16570 {
16571 if (selective_display)
16572 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16573 ;
16574 else
16575 while (*cursor != '\n' && ++cursor != ceiling_addr)
16576 ;
16577
16578 if (cursor != ceiling_addr)
16579 {
16580 if (--count == 0)
16581 {
16582 start_byte += cursor - base + 1;
16583 *byte_pos_ptr = start_byte;
16584 return orig_count;
16585 }
16586 else
16587 if (++cursor == ceiling_addr)
16588 break;
16589 }
16590 else
16591 break;
16592 }
16593 start_byte += cursor - base;
16594 }
16595 }
16596 else
16597 {
16598 while (start_byte > limit_byte)
16599 {
16600 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16601 ceiling = max (limit_byte, ceiling);
16602 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16603 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16604 while (1)
16605 {
16606 if (selective_display)
16607 while (--cursor != ceiling_addr
16608 && *cursor != '\n' && *cursor != 015)
16609 ;
16610 else
16611 while (--cursor != ceiling_addr && *cursor != '\n')
16612 ;
16613
16614 if (cursor != ceiling_addr)
16615 {
16616 if (++count == 0)
16617 {
16618 start_byte += cursor - base + 1;
16619 *byte_pos_ptr = start_byte;
16620 /* When scanning backwards, we should
16621 not count the newline posterior to which we stop. */
16622 return - orig_count - 1;
16623 }
16624 }
16625 else
16626 break;
16627 }
16628 /* Here we add 1 to compensate for the last decrement
16629 of CURSOR, which took it past the valid range. */
16630 start_byte += cursor - base + 1;
16631 }
16632 }
16633
16634 *byte_pos_ptr = limit_byte;
16635
16636 if (count < 0)
16637 return - orig_count + count;
16638 return orig_count - count;
16639
16640 }
16641
16642
16643 \f
16644 /***********************************************************************
16645 Displaying strings
16646 ***********************************************************************/
16647
16648 /* Display a NUL-terminated string, starting with index START.
16649
16650 If STRING is non-null, display that C string. Otherwise, the Lisp
16651 string LISP_STRING is displayed.
16652
16653 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16654 FACE_STRING. Display STRING or LISP_STRING with the face at
16655 FACE_STRING_POS in FACE_STRING:
16656
16657 Display the string in the environment given by IT, but use the
16658 standard display table, temporarily.
16659
16660 FIELD_WIDTH is the minimum number of output glyphs to produce.
16661 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16662 with spaces. If STRING has more characters, more than FIELD_WIDTH
16663 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
16664
16665 PRECISION is the maximum number of characters to output from
16666 STRING. PRECISION < 0 means don't truncate the string.
16667
16668 This is roughly equivalent to printf format specifiers:
16669
16670 FIELD_WIDTH PRECISION PRINTF
16671 ----------------------------------------
16672 -1 -1 %s
16673 -1 10 %.10s
16674 10 -1 %10s
16675 20 10 %20.10s
16676
16677 MULTIBYTE zero means do not display multibyte chars, > 0 means do
16678 display them, and < 0 means obey the current buffer's value of
16679 enable_multibyte_characters.
16680
16681 Value is the number of glyphs produced. */
16682
16683 static int
16684 display_string (string, lisp_string, face_string, face_string_pos,
16685 start, it, field_width, precision, max_x, multibyte)
16686 unsigned char *string;
16687 Lisp_Object lisp_string;
16688 Lisp_Object face_string;
16689 int face_string_pos;
16690 int start;
16691 struct it *it;
16692 int field_width, precision, max_x;
16693 int multibyte;
16694 {
16695 int hpos_at_start = it->hpos;
16696 int saved_face_id = it->face_id;
16697 struct glyph_row *row = it->glyph_row;
16698
16699 /* Initialize the iterator IT for iteration over STRING beginning
16700 with index START. */
16701 reseat_to_string (it, string, lisp_string, start,
16702 precision, field_width, multibyte);
16703
16704 /* If displaying STRING, set up the face of the iterator
16705 from LISP_STRING, if that's given. */
16706 if (STRINGP (face_string))
16707 {
16708 int endptr;
16709 struct face *face;
16710
16711 it->face_id
16712 = face_at_string_position (it->w, face_string, face_string_pos,
16713 0, it->region_beg_charpos,
16714 it->region_end_charpos,
16715 &endptr, it->base_face_id, 0);
16716 face = FACE_FROM_ID (it->f, it->face_id);
16717 it->face_box_p = face->box != FACE_NO_BOX;
16718 }
16719
16720 /* Set max_x to the maximum allowed X position. Don't let it go
16721 beyond the right edge of the window. */
16722 if (max_x <= 0)
16723 max_x = it->last_visible_x;
16724 else
16725 max_x = min (max_x, it->last_visible_x);
16726
16727 /* Skip over display elements that are not visible. because IT->w is
16728 hscrolled. */
16729 if (it->current_x < it->first_visible_x)
16730 move_it_in_display_line_to (it, 100000, it->first_visible_x,
16731 MOVE_TO_POS | MOVE_TO_X);
16732
16733 row->ascent = it->max_ascent;
16734 row->height = it->max_ascent + it->max_descent;
16735 row->phys_ascent = it->max_phys_ascent;
16736 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16737
16738 /* This condition is for the case that we are called with current_x
16739 past last_visible_x. */
16740 while (it->current_x < max_x)
16741 {
16742 int x_before, x, n_glyphs_before, i, nglyphs;
16743
16744 /* Get the next display element. */
16745 if (!get_next_display_element (it))
16746 break;
16747
16748 /* Produce glyphs. */
16749 x_before = it->current_x;
16750 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
16751 PRODUCE_GLYPHS (it);
16752
16753 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
16754 i = 0;
16755 x = x_before;
16756 while (i < nglyphs)
16757 {
16758 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16759
16760 if (!it->truncate_lines_p
16761 && x + glyph->pixel_width > max_x)
16762 {
16763 /* End of continued line or max_x reached. */
16764 if (CHAR_GLYPH_PADDING_P (*glyph))
16765 {
16766 /* A wide character is unbreakable. */
16767 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
16768 it->current_x = x_before;
16769 }
16770 else
16771 {
16772 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
16773 it->current_x = x;
16774 }
16775 break;
16776 }
16777 else if (x + glyph->pixel_width > it->first_visible_x)
16778 {
16779 /* Glyph is at least partially visible. */
16780 ++it->hpos;
16781 if (x < it->first_visible_x)
16782 it->glyph_row->x = x - it->first_visible_x;
16783 }
16784 else
16785 {
16786 /* Glyph is off the left margin of the display area.
16787 Should not happen. */
16788 abort ();
16789 }
16790
16791 row->ascent = max (row->ascent, it->max_ascent);
16792 row->height = max (row->height, it->max_ascent + it->max_descent);
16793 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16794 row->phys_height = max (row->phys_height,
16795 it->max_phys_ascent + it->max_phys_descent);
16796 x += glyph->pixel_width;
16797 ++i;
16798 }
16799
16800 /* Stop if max_x reached. */
16801 if (i < nglyphs)
16802 break;
16803
16804 /* Stop at line ends. */
16805 if (ITERATOR_AT_END_OF_LINE_P (it))
16806 {
16807 it->continuation_lines_width = 0;
16808 break;
16809 }
16810
16811 set_iterator_to_next (it, 1);
16812
16813 /* Stop if truncating at the right edge. */
16814 if (it->truncate_lines_p
16815 && it->current_x >= it->last_visible_x)
16816 {
16817 /* Add truncation mark, but don't do it if the line is
16818 truncated at a padding space. */
16819 if (IT_CHARPOS (*it) < it->string_nchars)
16820 {
16821 if (!FRAME_WINDOW_P (it->f))
16822 {
16823 int i, n;
16824
16825 if (it->current_x > it->last_visible_x)
16826 {
16827 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16828 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16829 break;
16830 for (n = row->used[TEXT_AREA]; i < n; ++i)
16831 {
16832 row->used[TEXT_AREA] = i;
16833 produce_special_glyphs (it, IT_TRUNCATION);
16834 }
16835 }
16836 produce_special_glyphs (it, IT_TRUNCATION);
16837 }
16838 it->glyph_row->truncated_on_right_p = 1;
16839 }
16840 break;
16841 }
16842 }
16843
16844 /* Maybe insert a truncation at the left. */
16845 if (it->first_visible_x
16846 && IT_CHARPOS (*it) > 0)
16847 {
16848 if (!FRAME_WINDOW_P (it->f))
16849 insert_left_trunc_glyphs (it);
16850 it->glyph_row->truncated_on_left_p = 1;
16851 }
16852
16853 it->face_id = saved_face_id;
16854
16855 /* Value is number of columns displayed. */
16856 return it->hpos - hpos_at_start;
16857 }
16858
16859
16860 \f
16861 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
16862 appears as an element of LIST or as the car of an element of LIST.
16863 If PROPVAL is a list, compare each element against LIST in that
16864 way, and return 1/2 if any element of PROPVAL is found in LIST.
16865 Otherwise return 0. This function cannot quit.
16866 The return value is 2 if the text is invisible but with an ellipsis
16867 and 1 if it's invisible and without an ellipsis. */
16868
16869 int
16870 invisible_p (propval, list)
16871 register Lisp_Object propval;
16872 Lisp_Object list;
16873 {
16874 register Lisp_Object tail, proptail;
16875
16876 for (tail = list; CONSP (tail); tail = XCDR (tail))
16877 {
16878 register Lisp_Object tem;
16879 tem = XCAR (tail);
16880 if (EQ (propval, tem))
16881 return 1;
16882 if (CONSP (tem) && EQ (propval, XCAR (tem)))
16883 return NILP (XCDR (tem)) ? 1 : 2;
16884 }
16885
16886 if (CONSP (propval))
16887 {
16888 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
16889 {
16890 Lisp_Object propelt;
16891 propelt = XCAR (proptail);
16892 for (tail = list; CONSP (tail); tail = XCDR (tail))
16893 {
16894 register Lisp_Object tem;
16895 tem = XCAR (tail);
16896 if (EQ (propelt, tem))
16897 return 1;
16898 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
16899 return NILP (XCDR (tem)) ? 1 : 2;
16900 }
16901 }
16902 }
16903
16904 return 0;
16905 }
16906
16907 /* Calculate a width or height in pixels from a specification using
16908 the following elements:
16909
16910 SPEC ::=
16911 NUM - a (fractional) multiple of the default font width/height
16912 (NUM) - specifies exactly NUM pixels
16913 UNIT - a fixed number of pixels, see below.
16914 ELEMENT - size of a display element in pixels, see below.
16915 (NUM . SPEC) - equals NUM * SPEC
16916 (+ SPEC SPEC ...) - add pixel values
16917 (- SPEC SPEC ...) - subtract pixel values
16918 (- SPEC) - negate pixel value
16919
16920 NUM ::=
16921 INT or FLOAT - a number constant
16922 SYMBOL - use symbol's (buffer local) variable binding.
16923
16924 UNIT ::=
16925 in - pixels per inch *)
16926 mm - pixels per 1/1000 meter *)
16927 cm - pixels per 1/100 meter *)
16928 width - width of current font in pixels.
16929 height - height of current font in pixels.
16930
16931 *) using the ratio(s) defined in display-pixels-per-inch.
16932
16933 ELEMENT ::=
16934
16935 left-fringe - left fringe width in pixels
16936 right-fringe - right fringe width in pixels
16937
16938 left-margin - left margin width in pixels
16939 right-margin - right margin width in pixels
16940
16941 scroll-bar - scroll-bar area width in pixels
16942
16943 Examples:
16944
16945 Pixels corresponding to 5 inches:
16946 (5 . in)
16947
16948 Total width of non-text areas on left side of window (if scroll-bar is on left):
16949 '(space :width (+ left-fringe left-margin scroll-bar))
16950
16951 Align to first text column (in header line):
16952 '(space :align-to 0)
16953
16954 Align to middle of text area minus half the width of variable `my-image'
16955 containing a loaded image:
16956 '(space :align-to (0.5 . (- text my-image)))
16957
16958 Width of left margin minus width of 1 character in the default font:
16959 '(space :width (- left-margin 1))
16960
16961 Width of left margin minus width of 2 characters in the current font:
16962 '(space :width (- left-margin (2 . width)))
16963
16964 Center 1 character over left-margin (in header line):
16965 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
16966
16967 Different ways to express width of left fringe plus left margin minus one pixel:
16968 '(space :width (- (+ left-fringe left-margin) (1)))
16969 '(space :width (+ left-fringe left-margin (- (1))))
16970 '(space :width (+ left-fringe left-margin (-1)))
16971
16972 */
16973
16974 #define NUMVAL(X) \
16975 ((INTEGERP (X) || FLOATP (X)) \
16976 ? XFLOATINT (X) \
16977 : - 1)
16978
16979 int
16980 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
16981 double *res;
16982 struct it *it;
16983 Lisp_Object prop;
16984 void *font;
16985 int width_p, *align_to;
16986 {
16987 double pixels;
16988
16989 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
16990 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
16991
16992 if (NILP (prop))
16993 return OK_PIXELS (0);
16994
16995 if (SYMBOLP (prop))
16996 {
16997 if (SCHARS (SYMBOL_NAME (prop)) == 2)
16998 {
16999 char *unit = SDATA (SYMBOL_NAME (prop));
17000
17001 if (unit[0] == 'i' && unit[1] == 'n')
17002 pixels = 1.0;
17003 else if (unit[0] == 'm' && unit[1] == 'm')
17004 pixels = 25.4;
17005 else if (unit[0] == 'c' && unit[1] == 'm')
17006 pixels = 2.54;
17007 else
17008 pixels = 0;
17009 if (pixels > 0)
17010 {
17011 double ppi;
17012 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
17013 || (CONSP (Vdisplay_pixels_per_inch)
17014 && (ppi = (width_p
17015 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
17016 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
17017 ppi > 0)))
17018 return OK_PIXELS (ppi / pixels);
17019
17020 return 0;
17021 }
17022 }
17023
17024 #ifdef HAVE_WINDOW_SYSTEM
17025 if (EQ (prop, Qheight))
17026 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
17027 if (EQ (prop, Qwidth))
17028 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
17029 #else
17030 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
17031 return OK_PIXELS (1);
17032 #endif
17033
17034 if (EQ (prop, Qtext))
17035 return OK_PIXELS (width_p
17036 ? window_box_width (it->w, TEXT_AREA)
17037 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
17038
17039 if (align_to && *align_to < 0)
17040 {
17041 *res = 0;
17042 if (EQ (prop, Qleft))
17043 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
17044 if (EQ (prop, Qright))
17045 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
17046 if (EQ (prop, Qcenter))
17047 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
17048 + window_box_width (it->w, TEXT_AREA) / 2);
17049 if (EQ (prop, Qleft_fringe))
17050 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17051 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
17052 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
17053 if (EQ (prop, Qright_fringe))
17054 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17055 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17056 : window_box_right_offset (it->w, TEXT_AREA));
17057 if (EQ (prop, Qleft_margin))
17058 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
17059 if (EQ (prop, Qright_margin))
17060 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
17061 if (EQ (prop, Qscroll_bar))
17062 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
17063 ? 0
17064 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17065 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17066 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
17067 : 0)));
17068 }
17069 else
17070 {
17071 if (EQ (prop, Qleft_fringe))
17072 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
17073 if (EQ (prop, Qright_fringe))
17074 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
17075 if (EQ (prop, Qleft_margin))
17076 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
17077 if (EQ (prop, Qright_margin))
17078 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
17079 if (EQ (prop, Qscroll_bar))
17080 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
17081 }
17082
17083 prop = Fbuffer_local_value (prop, it->w->buffer);
17084 }
17085
17086 if (INTEGERP (prop) || FLOATP (prop))
17087 {
17088 int base_unit = (width_p
17089 ? FRAME_COLUMN_WIDTH (it->f)
17090 : FRAME_LINE_HEIGHT (it->f));
17091 return OK_PIXELS (XFLOATINT (prop) * base_unit);
17092 }
17093
17094 if (CONSP (prop))
17095 {
17096 Lisp_Object car = XCAR (prop);
17097 Lisp_Object cdr = XCDR (prop);
17098
17099 if (SYMBOLP (car))
17100 {
17101 #ifdef HAVE_WINDOW_SYSTEM
17102 if (valid_image_p (prop))
17103 {
17104 int id = lookup_image (it->f, prop);
17105 struct image *img = IMAGE_FROM_ID (it->f, id);
17106
17107 return OK_PIXELS (width_p ? img->width : img->height);
17108 }
17109 #endif
17110 if (EQ (car, Qplus) || EQ (car, Qminus))
17111 {
17112 int first = 1;
17113 double px;
17114
17115 pixels = 0;
17116 while (CONSP (cdr))
17117 {
17118 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
17119 font, width_p, align_to))
17120 return 0;
17121 if (first)
17122 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
17123 else
17124 pixels += px;
17125 cdr = XCDR (cdr);
17126 }
17127 if (EQ (car, Qminus))
17128 pixels = -pixels;
17129 return OK_PIXELS (pixels);
17130 }
17131
17132 car = Fbuffer_local_value (car, it->w->buffer);
17133 }
17134
17135 if (INTEGERP (car) || FLOATP (car))
17136 {
17137 double fact;
17138 pixels = XFLOATINT (car);
17139 if (NILP (cdr))
17140 return OK_PIXELS (pixels);
17141 if (calc_pixel_width_or_height (&fact, it, cdr,
17142 font, width_p, align_to))
17143 return OK_PIXELS (pixels * fact);
17144 return 0;
17145 }
17146
17147 return 0;
17148 }
17149
17150 return 0;
17151 }
17152
17153 \f
17154 /***********************************************************************
17155 Glyph Display
17156 ***********************************************************************/
17157
17158 #ifdef HAVE_WINDOW_SYSTEM
17159
17160 #if GLYPH_DEBUG
17161
17162 void
17163 dump_glyph_string (s)
17164 struct glyph_string *s;
17165 {
17166 fprintf (stderr, "glyph string\n");
17167 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17168 s->x, s->y, s->width, s->height);
17169 fprintf (stderr, " ybase = %d\n", s->ybase);
17170 fprintf (stderr, " hl = %d\n", s->hl);
17171 fprintf (stderr, " left overhang = %d, right = %d\n",
17172 s->left_overhang, s->right_overhang);
17173 fprintf (stderr, " nchars = %d\n", s->nchars);
17174 fprintf (stderr, " extends to end of line = %d\n",
17175 s->extends_to_end_of_line_p);
17176 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17177 fprintf (stderr, " bg width = %d\n", s->background_width);
17178 }
17179
17180 #endif /* GLYPH_DEBUG */
17181
17182 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17183 of XChar2b structures for S; it can't be allocated in
17184 init_glyph_string because it must be allocated via `alloca'. W
17185 is the window on which S is drawn. ROW and AREA are the glyph row
17186 and area within the row from which S is constructed. START is the
17187 index of the first glyph structure covered by S. HL is a
17188 face-override for drawing S. */
17189
17190 #ifdef HAVE_NTGUI
17191 #define OPTIONAL_HDC(hdc) hdc,
17192 #define DECLARE_HDC(hdc) HDC hdc;
17193 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17194 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17195 #endif
17196
17197 #ifndef OPTIONAL_HDC
17198 #define OPTIONAL_HDC(hdc)
17199 #define DECLARE_HDC(hdc)
17200 #define ALLOCATE_HDC(hdc, f)
17201 #define RELEASE_HDC(hdc, f)
17202 #endif
17203
17204 static void
17205 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17206 struct glyph_string *s;
17207 DECLARE_HDC (hdc)
17208 XChar2b *char2b;
17209 struct window *w;
17210 struct glyph_row *row;
17211 enum glyph_row_area area;
17212 int start;
17213 enum draw_glyphs_face hl;
17214 {
17215 bzero (s, sizeof *s);
17216 s->w = w;
17217 s->f = XFRAME (w->frame);
17218 #ifdef HAVE_NTGUI
17219 s->hdc = hdc;
17220 #endif
17221 s->display = FRAME_X_DISPLAY (s->f);
17222 s->window = FRAME_X_WINDOW (s->f);
17223 s->char2b = char2b;
17224 s->hl = hl;
17225 s->row = row;
17226 s->area = area;
17227 s->first_glyph = row->glyphs[area] + start;
17228 s->height = row->height;
17229 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17230
17231 /* Display the internal border below the tool-bar window. */
17232 if (s->w == XWINDOW (s->f->tool_bar_window))
17233 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17234
17235 s->ybase = s->y + row->ascent;
17236 }
17237
17238
17239 /* Append the list of glyph strings with head H and tail T to the list
17240 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17241
17242 static INLINE void
17243 append_glyph_string_lists (head, tail, h, t)
17244 struct glyph_string **head, **tail;
17245 struct glyph_string *h, *t;
17246 {
17247 if (h)
17248 {
17249 if (*head)
17250 (*tail)->next = h;
17251 else
17252 *head = h;
17253 h->prev = *tail;
17254 *tail = t;
17255 }
17256 }
17257
17258
17259 /* Prepend the list of glyph strings with head H and tail T to the
17260 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17261 result. */
17262
17263 static INLINE void
17264 prepend_glyph_string_lists (head, tail, h, t)
17265 struct glyph_string **head, **tail;
17266 struct glyph_string *h, *t;
17267 {
17268 if (h)
17269 {
17270 if (*head)
17271 (*head)->prev = t;
17272 else
17273 *tail = t;
17274 t->next = *head;
17275 *head = h;
17276 }
17277 }
17278
17279
17280 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17281 Set *HEAD and *TAIL to the resulting list. */
17282
17283 static INLINE void
17284 append_glyph_string (head, tail, s)
17285 struct glyph_string **head, **tail;
17286 struct glyph_string *s;
17287 {
17288 s->next = s->prev = NULL;
17289 append_glyph_string_lists (head, tail, s, s);
17290 }
17291
17292
17293 /* Get face and two-byte form of character glyph GLYPH on frame F.
17294 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17295 a pointer to a realized face that is ready for display. */
17296
17297 static INLINE struct face *
17298 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17299 struct frame *f;
17300 struct glyph *glyph;
17301 XChar2b *char2b;
17302 int *two_byte_p;
17303 {
17304 struct face *face;
17305
17306 xassert (glyph->type == CHAR_GLYPH);
17307 face = FACE_FROM_ID (f, glyph->face_id);
17308
17309 if (two_byte_p)
17310 *two_byte_p = 0;
17311
17312 if (!glyph->multibyte_p)
17313 {
17314 /* Unibyte case. We don't have to encode, but we have to make
17315 sure to use a face suitable for unibyte. */
17316 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17317 }
17318 else if (glyph->u.ch < 128
17319 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17320 {
17321 /* Case of ASCII in a face known to fit ASCII. */
17322 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17323 }
17324 else
17325 {
17326 int c1, c2, charset;
17327
17328 /* Split characters into bytes. If c2 is -1 afterwards, C is
17329 really a one-byte character so that byte1 is zero. */
17330 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17331 if (c2 > 0)
17332 STORE_XCHAR2B (char2b, c1, c2);
17333 else
17334 STORE_XCHAR2B (char2b, 0, c1);
17335
17336 /* Maybe encode the character in *CHAR2B. */
17337 if (charset != CHARSET_ASCII)
17338 {
17339 struct font_info *font_info
17340 = FONT_INFO_FROM_ID (f, face->font_info_id);
17341 if (font_info)
17342 glyph->font_type
17343 = FRAME_RIF (f)->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
17344 }
17345 }
17346
17347 /* Make sure X resources of the face are allocated. */
17348 xassert (face != NULL);
17349 PREPARE_FACE_FOR_DISPLAY (f, face);
17350 return face;
17351 }
17352
17353
17354 /* Fill glyph string S with composition components specified by S->cmp.
17355
17356 FACES is an array of faces for all components of this composition.
17357 S->gidx is the index of the first component for S.
17358 OVERLAPS_P non-zero means S should draw the foreground only, and
17359 use its physical height for clipping.
17360
17361 Value is the index of a component not in S. */
17362
17363 static int
17364 fill_composite_glyph_string (s, faces, overlaps_p)
17365 struct glyph_string *s;
17366 struct face **faces;
17367 int overlaps_p;
17368 {
17369 int i;
17370
17371 xassert (s);
17372
17373 s->for_overlaps_p = overlaps_p;
17374
17375 s->face = faces[s->gidx];
17376 s->font = s->face->font;
17377 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17378
17379 /* For all glyphs of this composition, starting at the offset
17380 S->gidx, until we reach the end of the definition or encounter a
17381 glyph that requires the different face, add it to S. */
17382 ++s->nchars;
17383 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
17384 ++s->nchars;
17385
17386 /* All glyph strings for the same composition has the same width,
17387 i.e. the width set for the first component of the composition. */
17388
17389 s->width = s->first_glyph->pixel_width;
17390
17391 /* If the specified font could not be loaded, use the frame's
17392 default font, but record the fact that we couldn't load it in
17393 the glyph string so that we can draw rectangles for the
17394 characters of the glyph string. */
17395 if (s->font == NULL)
17396 {
17397 s->font_not_found_p = 1;
17398 s->font = FRAME_FONT (s->f);
17399 }
17400
17401 /* Adjust base line for subscript/superscript text. */
17402 s->ybase += s->first_glyph->voffset;
17403
17404 xassert (s->face && s->face->gc);
17405
17406 /* This glyph string must always be drawn with 16-bit functions. */
17407 s->two_byte_p = 1;
17408
17409 return s->gidx + s->nchars;
17410 }
17411
17412
17413 /* Fill glyph string S from a sequence of character glyphs.
17414
17415 FACE_ID is the face id of the string. START is the index of the
17416 first glyph to consider, END is the index of the last + 1.
17417 OVERLAPS_P non-zero means S should draw the foreground only, and
17418 use its physical height for clipping.
17419
17420 Value is the index of the first glyph not in S. */
17421
17422 static int
17423 fill_glyph_string (s, face_id, start, end, overlaps_p)
17424 struct glyph_string *s;
17425 int face_id;
17426 int start, end, overlaps_p;
17427 {
17428 struct glyph *glyph, *last;
17429 int voffset;
17430 int glyph_not_available_p;
17431
17432 xassert (s->f == XFRAME (s->w->frame));
17433 xassert (s->nchars == 0);
17434 xassert (start >= 0 && end > start);
17435
17436 s->for_overlaps_p = overlaps_p,
17437 glyph = s->row->glyphs[s->area] + start;
17438 last = s->row->glyphs[s->area] + end;
17439 voffset = glyph->voffset;
17440
17441 glyph_not_available_p = glyph->glyph_not_available_p;
17442
17443 while (glyph < last
17444 && glyph->type == CHAR_GLYPH
17445 && glyph->voffset == voffset
17446 /* Same face id implies same font, nowadays. */
17447 && glyph->face_id == face_id
17448 && glyph->glyph_not_available_p == glyph_not_available_p)
17449 {
17450 int two_byte_p;
17451
17452 s->face = get_glyph_face_and_encoding (s->f, glyph,
17453 s->char2b + s->nchars,
17454 &two_byte_p);
17455 s->two_byte_p = two_byte_p;
17456 ++s->nchars;
17457 xassert (s->nchars <= end - start);
17458 s->width += glyph->pixel_width;
17459 ++glyph;
17460 }
17461
17462 s->font = s->face->font;
17463 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17464
17465 /* If the specified font could not be loaded, use the frame's font,
17466 but record the fact that we couldn't load it in
17467 S->font_not_found_p so that we can draw rectangles for the
17468 characters of the glyph string. */
17469 if (s->font == NULL || glyph_not_available_p)
17470 {
17471 s->font_not_found_p = 1;
17472 s->font = FRAME_FONT (s->f);
17473 }
17474
17475 /* Adjust base line for subscript/superscript text. */
17476 s->ybase += voffset;
17477
17478 xassert (s->face && s->face->gc);
17479 return glyph - s->row->glyphs[s->area];
17480 }
17481
17482
17483 /* Fill glyph string S from image glyph S->first_glyph. */
17484
17485 static void
17486 fill_image_glyph_string (s)
17487 struct glyph_string *s;
17488 {
17489 xassert (s->first_glyph->type == IMAGE_GLYPH);
17490 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
17491 xassert (s->img);
17492 s->slice = s->first_glyph->slice;
17493 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
17494 s->font = s->face->font;
17495 s->width = s->first_glyph->pixel_width;
17496
17497 /* Adjust base line for subscript/superscript text. */
17498 s->ybase += s->first_glyph->voffset;
17499 }
17500
17501
17502 /* Fill glyph string S from a sequence of stretch glyphs.
17503
17504 ROW is the glyph row in which the glyphs are found, AREA is the
17505 area within the row. START is the index of the first glyph to
17506 consider, END is the index of the last + 1.
17507
17508 Value is the index of the first glyph not in S. */
17509
17510 static int
17511 fill_stretch_glyph_string (s, row, area, start, end)
17512 struct glyph_string *s;
17513 struct glyph_row *row;
17514 enum glyph_row_area area;
17515 int start, end;
17516 {
17517 struct glyph *glyph, *last;
17518 int voffset, face_id;
17519
17520 xassert (s->first_glyph->type == STRETCH_GLYPH);
17521
17522 glyph = s->row->glyphs[s->area] + start;
17523 last = s->row->glyphs[s->area] + end;
17524 face_id = glyph->face_id;
17525 s->face = FACE_FROM_ID (s->f, face_id);
17526 s->font = s->face->font;
17527 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17528 s->width = glyph->pixel_width;
17529 voffset = glyph->voffset;
17530
17531 for (++glyph;
17532 (glyph < last
17533 && glyph->type == STRETCH_GLYPH
17534 && glyph->voffset == voffset
17535 && glyph->face_id == face_id);
17536 ++glyph)
17537 s->width += glyph->pixel_width;
17538
17539 /* Adjust base line for subscript/superscript text. */
17540 s->ybase += voffset;
17541
17542 /* The case that face->gc == 0 is handled when drawing the glyph
17543 string by calling PREPARE_FACE_FOR_DISPLAY. */
17544 xassert (s->face);
17545 return glyph - s->row->glyphs[s->area];
17546 }
17547
17548
17549 /* EXPORT for RIF:
17550 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
17551 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
17552 assumed to be zero. */
17553
17554 void
17555 x_get_glyph_overhangs (glyph, f, left, right)
17556 struct glyph *glyph;
17557 struct frame *f;
17558 int *left, *right;
17559 {
17560 *left = *right = 0;
17561
17562 if (glyph->type == CHAR_GLYPH)
17563 {
17564 XFontStruct *font;
17565 struct face *face;
17566 struct font_info *font_info;
17567 XChar2b char2b;
17568 XCharStruct *pcm;
17569
17570 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
17571 font = face->font;
17572 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
17573 if (font /* ++KFS: Should this be font_info ? */
17574 && (pcm = FRAME_RIF (f)->per_char_metric (font, &char2b, glyph->font_type)))
17575 {
17576 if (pcm->rbearing > pcm->width)
17577 *right = pcm->rbearing - pcm->width;
17578 if (pcm->lbearing < 0)
17579 *left = -pcm->lbearing;
17580 }
17581 }
17582 }
17583
17584
17585 /* Return the index of the first glyph preceding glyph string S that
17586 is overwritten by S because of S's left overhang. Value is -1
17587 if no glyphs are overwritten. */
17588
17589 static int
17590 left_overwritten (s)
17591 struct glyph_string *s;
17592 {
17593 int k;
17594
17595 if (s->left_overhang)
17596 {
17597 int x = 0, i;
17598 struct glyph *glyphs = s->row->glyphs[s->area];
17599 int first = s->first_glyph - glyphs;
17600
17601 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
17602 x -= glyphs[i].pixel_width;
17603
17604 k = i + 1;
17605 }
17606 else
17607 k = -1;
17608
17609 return k;
17610 }
17611
17612
17613 /* Return the index of the first glyph preceding glyph string S that
17614 is overwriting S because of its right overhang. Value is -1 if no
17615 glyph in front of S overwrites S. */
17616
17617 static int
17618 left_overwriting (s)
17619 struct glyph_string *s;
17620 {
17621 int i, k, x;
17622 struct glyph *glyphs = s->row->glyphs[s->area];
17623 int first = s->first_glyph - glyphs;
17624
17625 k = -1;
17626 x = 0;
17627 for (i = first - 1; i >= 0; --i)
17628 {
17629 int left, right;
17630 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17631 if (x + right > 0)
17632 k = i;
17633 x -= glyphs[i].pixel_width;
17634 }
17635
17636 return k;
17637 }
17638
17639
17640 /* Return the index of the last glyph following glyph string S that is
17641 not overwritten by S because of S's right overhang. Value is -1 if
17642 no such glyph is found. */
17643
17644 static int
17645 right_overwritten (s)
17646 struct glyph_string *s;
17647 {
17648 int k = -1;
17649
17650 if (s->right_overhang)
17651 {
17652 int x = 0, i;
17653 struct glyph *glyphs = s->row->glyphs[s->area];
17654 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17655 int end = s->row->used[s->area];
17656
17657 for (i = first; i < end && s->right_overhang > x; ++i)
17658 x += glyphs[i].pixel_width;
17659
17660 k = i;
17661 }
17662
17663 return k;
17664 }
17665
17666
17667 /* Return the index of the last glyph following glyph string S that
17668 overwrites S because of its left overhang. Value is negative
17669 if no such glyph is found. */
17670
17671 static int
17672 right_overwriting (s)
17673 struct glyph_string *s;
17674 {
17675 int i, k, x;
17676 int end = s->row->used[s->area];
17677 struct glyph *glyphs = s->row->glyphs[s->area];
17678 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17679
17680 k = -1;
17681 x = 0;
17682 for (i = first; i < end; ++i)
17683 {
17684 int left, right;
17685 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17686 if (x - left < 0)
17687 k = i;
17688 x += glyphs[i].pixel_width;
17689 }
17690
17691 return k;
17692 }
17693
17694
17695 /* Get face and two-byte form of character C in face FACE_ID on frame
17696 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
17697 means we want to display multibyte text. DISPLAY_P non-zero means
17698 make sure that X resources for the face returned are allocated.
17699 Value is a pointer to a realized face that is ready for display if
17700 DISPLAY_P is non-zero. */
17701
17702 static INLINE struct face *
17703 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
17704 struct frame *f;
17705 int c, face_id;
17706 XChar2b *char2b;
17707 int multibyte_p, display_p;
17708 {
17709 struct face *face = FACE_FROM_ID (f, face_id);
17710
17711 if (!multibyte_p)
17712 {
17713 /* Unibyte case. We don't have to encode, but we have to make
17714 sure to use a face suitable for unibyte. */
17715 STORE_XCHAR2B (char2b, 0, c);
17716 face_id = FACE_FOR_CHAR (f, face, c);
17717 face = FACE_FROM_ID (f, face_id);
17718 }
17719 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
17720 {
17721 /* Case of ASCII in a face known to fit ASCII. */
17722 STORE_XCHAR2B (char2b, 0, c);
17723 }
17724 else
17725 {
17726 int c1, c2, charset;
17727
17728 /* Split characters into bytes. If c2 is -1 afterwards, C is
17729 really a one-byte character so that byte1 is zero. */
17730 SPLIT_CHAR (c, charset, c1, c2);
17731 if (c2 > 0)
17732 STORE_XCHAR2B (char2b, c1, c2);
17733 else
17734 STORE_XCHAR2B (char2b, 0, c1);
17735
17736 /* Maybe encode the character in *CHAR2B. */
17737 if (face->font != NULL)
17738 {
17739 struct font_info *font_info
17740 = FONT_INFO_FROM_ID (f, face->font_info_id);
17741 if (font_info)
17742 FRAME_RIF (f)->encode_char (c, char2b, font_info, 0);
17743 }
17744 }
17745
17746 /* Make sure X resources of the face are allocated. */
17747 #ifdef HAVE_X_WINDOWS
17748 if (display_p)
17749 #endif
17750 {
17751 xassert (face != NULL);
17752 PREPARE_FACE_FOR_DISPLAY (f, face);
17753 }
17754
17755 return face;
17756 }
17757
17758
17759 /* Set background width of glyph string S. START is the index of the
17760 first glyph following S. LAST_X is the right-most x-position + 1
17761 in the drawing area. */
17762
17763 static INLINE void
17764 set_glyph_string_background_width (s, start, last_x)
17765 struct glyph_string *s;
17766 int start;
17767 int last_x;
17768 {
17769 /* If the face of this glyph string has to be drawn to the end of
17770 the drawing area, set S->extends_to_end_of_line_p. */
17771 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
17772
17773 if (start == s->row->used[s->area]
17774 && s->area == TEXT_AREA
17775 && ((s->hl == DRAW_NORMAL_TEXT
17776 && (s->row->fill_line_p
17777 || s->face->background != default_face->background
17778 || s->face->stipple != default_face->stipple
17779 || s->row->mouse_face_p))
17780 || s->hl == DRAW_MOUSE_FACE
17781 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
17782 && s->row->fill_line_p)))
17783 s->extends_to_end_of_line_p = 1;
17784
17785 /* If S extends its face to the end of the line, set its
17786 background_width to the distance to the right edge of the drawing
17787 area. */
17788 if (s->extends_to_end_of_line_p)
17789 s->background_width = last_x - s->x + 1;
17790 else
17791 s->background_width = s->width;
17792 }
17793
17794
17795 /* Compute overhangs and x-positions for glyph string S and its
17796 predecessors, or successors. X is the starting x-position for S.
17797 BACKWARD_P non-zero means process predecessors. */
17798
17799 static void
17800 compute_overhangs_and_x (s, x, backward_p)
17801 struct glyph_string *s;
17802 int x;
17803 int backward_p;
17804 {
17805 if (backward_p)
17806 {
17807 while (s)
17808 {
17809 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
17810 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
17811 x -= s->width;
17812 s->x = x;
17813 s = s->prev;
17814 }
17815 }
17816 else
17817 {
17818 while (s)
17819 {
17820 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
17821 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
17822 s->x = x;
17823 x += s->width;
17824 s = s->next;
17825 }
17826 }
17827 }
17828
17829
17830
17831 /* The following macros are only called from draw_glyphs below.
17832 They reference the following parameters of that function directly:
17833 `w', `row', `area', and `overlap_p'
17834 as well as the following local variables:
17835 `s', `f', and `hdc' (in W32) */
17836
17837 #ifdef HAVE_NTGUI
17838 /* On W32, silently add local `hdc' variable to argument list of
17839 init_glyph_string. */
17840 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17841 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
17842 #else
17843 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17844 init_glyph_string (s, char2b, w, row, area, start, hl)
17845 #endif
17846
17847 /* Add a glyph string for a stretch glyph to the list of strings
17848 between HEAD and TAIL. START is the index of the stretch glyph in
17849 row area AREA of glyph row ROW. END is the index of the last glyph
17850 in that glyph row area. X is the current output position assigned
17851 to the new glyph string constructed. HL overrides that face of the
17852 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17853 is the right-most x-position of the drawing area. */
17854
17855 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
17856 and below -- keep them on one line. */
17857 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17858 do \
17859 { \
17860 s = (struct glyph_string *) alloca (sizeof *s); \
17861 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17862 START = fill_stretch_glyph_string (s, row, area, START, END); \
17863 append_glyph_string (&HEAD, &TAIL, s); \
17864 s->x = (X); \
17865 } \
17866 while (0)
17867
17868
17869 /* Add a glyph string for an image glyph to the list of strings
17870 between HEAD and TAIL. START is the index of the image glyph in
17871 row area AREA of glyph row ROW. END is the index of the last glyph
17872 in that glyph row area. X is the current output position assigned
17873 to the new glyph string constructed. HL overrides that face of the
17874 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17875 is the right-most x-position of the drawing area. */
17876
17877 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17878 do \
17879 { \
17880 s = (struct glyph_string *) alloca (sizeof *s); \
17881 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17882 fill_image_glyph_string (s); \
17883 append_glyph_string (&HEAD, &TAIL, s); \
17884 ++START; \
17885 s->x = (X); \
17886 } \
17887 while (0)
17888
17889
17890 /* Add a glyph string for a sequence of character glyphs to the list
17891 of strings between HEAD and TAIL. START is the index of the first
17892 glyph in row area AREA of glyph row ROW that is part of the new
17893 glyph string. END is the index of the last glyph in that glyph row
17894 area. X is the current output position assigned to the new glyph
17895 string constructed. HL overrides that face of the glyph; e.g. it
17896 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
17897 right-most x-position of the drawing area. */
17898
17899 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17900 do \
17901 { \
17902 int c, face_id; \
17903 XChar2b *char2b; \
17904 \
17905 c = (row)->glyphs[area][START].u.ch; \
17906 face_id = (row)->glyphs[area][START].face_id; \
17907 \
17908 s = (struct glyph_string *) alloca (sizeof *s); \
17909 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
17910 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
17911 append_glyph_string (&HEAD, &TAIL, s); \
17912 s->x = (X); \
17913 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
17914 } \
17915 while (0)
17916
17917
17918 /* Add a glyph string for a composite sequence to the list of strings
17919 between HEAD and TAIL. START is the index of the first glyph in
17920 row area AREA of glyph row ROW that is part of the new glyph
17921 string. END is the index of the last glyph in that glyph row area.
17922 X is the current output position assigned to the new glyph string
17923 constructed. HL overrides that face of the glyph; e.g. it is
17924 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
17925 x-position of the drawing area. */
17926
17927 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17928 do { \
17929 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
17930 int face_id = (row)->glyphs[area][START].face_id; \
17931 struct face *base_face = FACE_FROM_ID (f, face_id); \
17932 struct composition *cmp = composition_table[cmp_id]; \
17933 int glyph_len = cmp->glyph_len; \
17934 XChar2b *char2b; \
17935 struct face **faces; \
17936 struct glyph_string *first_s = NULL; \
17937 int n; \
17938 \
17939 base_face = base_face->ascii_face; \
17940 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
17941 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
17942 /* At first, fill in `char2b' and `faces'. */ \
17943 for (n = 0; n < glyph_len; n++) \
17944 { \
17945 int c = COMPOSITION_GLYPH (cmp, n); \
17946 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
17947 faces[n] = FACE_FROM_ID (f, this_face_id); \
17948 get_char_face_and_encoding (f, c, this_face_id, \
17949 char2b + n, 1, 1); \
17950 } \
17951 \
17952 /* Make glyph_strings for each glyph sequence that is drawable by \
17953 the same face, and append them to HEAD/TAIL. */ \
17954 for (n = 0; n < cmp->glyph_len;) \
17955 { \
17956 s = (struct glyph_string *) alloca (sizeof *s); \
17957 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
17958 append_glyph_string (&(HEAD), &(TAIL), s); \
17959 s->cmp = cmp; \
17960 s->gidx = n; \
17961 s->x = (X); \
17962 \
17963 if (n == 0) \
17964 first_s = s; \
17965 \
17966 n = fill_composite_glyph_string (s, faces, overlaps_p); \
17967 } \
17968 \
17969 ++START; \
17970 s = first_s; \
17971 } while (0)
17972
17973
17974 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
17975 of AREA of glyph row ROW on window W between indices START and END.
17976 HL overrides the face for drawing glyph strings, e.g. it is
17977 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
17978 x-positions of the drawing area.
17979
17980 This is an ugly monster macro construct because we must use alloca
17981 to allocate glyph strings (because draw_glyphs can be called
17982 asynchronously). */
17983
17984 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17985 do \
17986 { \
17987 HEAD = TAIL = NULL; \
17988 while (START < END) \
17989 { \
17990 struct glyph *first_glyph = (row)->glyphs[area] + START; \
17991 switch (first_glyph->type) \
17992 { \
17993 case CHAR_GLYPH: \
17994 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
17995 HL, X, LAST_X); \
17996 break; \
17997 \
17998 case COMPOSITE_GLYPH: \
17999 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
18000 HL, X, LAST_X); \
18001 break; \
18002 \
18003 case STRETCH_GLYPH: \
18004 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
18005 HL, X, LAST_X); \
18006 break; \
18007 \
18008 case IMAGE_GLYPH: \
18009 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
18010 HL, X, LAST_X); \
18011 break; \
18012 \
18013 default: \
18014 abort (); \
18015 } \
18016 \
18017 set_glyph_string_background_width (s, START, LAST_X); \
18018 (X) += s->width; \
18019 } \
18020 } \
18021 while (0)
18022
18023
18024 /* Draw glyphs between START and END in AREA of ROW on window W,
18025 starting at x-position X. X is relative to AREA in W. HL is a
18026 face-override with the following meaning:
18027
18028 DRAW_NORMAL_TEXT draw normally
18029 DRAW_CURSOR draw in cursor face
18030 DRAW_MOUSE_FACE draw in mouse face.
18031 DRAW_INVERSE_VIDEO draw in mode line face
18032 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
18033 DRAW_IMAGE_RAISED draw an image with a raised relief around it
18034
18035 If OVERLAPS_P is non-zero, draw only the foreground of characters
18036 and clip to the physical height of ROW.
18037
18038 Value is the x-position reached, relative to AREA of W. */
18039
18040 static int
18041 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
18042 struct window *w;
18043 int x;
18044 struct glyph_row *row;
18045 enum glyph_row_area area;
18046 int start, end;
18047 enum draw_glyphs_face hl;
18048 int overlaps_p;
18049 {
18050 struct glyph_string *head, *tail;
18051 struct glyph_string *s;
18052 int last_x, area_width;
18053 int x_reached;
18054 int i, j;
18055 struct frame *f = XFRAME (WINDOW_FRAME (w));
18056 DECLARE_HDC (hdc);
18057
18058 ALLOCATE_HDC (hdc, f);
18059
18060 /* Let's rather be paranoid than getting a SEGV. */
18061 end = min (end, row->used[area]);
18062 start = max (0, start);
18063 start = min (end, start);
18064
18065 /* Translate X to frame coordinates. Set last_x to the right
18066 end of the drawing area. */
18067 if (row->full_width_p)
18068 {
18069 /* X is relative to the left edge of W, without scroll bars
18070 or fringes. */
18071 x += WINDOW_LEFT_EDGE_X (w);
18072 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
18073 }
18074 else
18075 {
18076 int area_left = window_box_left (w, area);
18077 x += area_left;
18078 area_width = window_box_width (w, area);
18079 last_x = area_left + area_width;
18080 }
18081
18082 /* Build a doubly-linked list of glyph_string structures between
18083 head and tail from what we have to draw. Note that the macro
18084 BUILD_GLYPH_STRINGS will modify its start parameter. That's
18085 the reason we use a separate variable `i'. */
18086 i = start;
18087 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
18088 if (tail)
18089 x_reached = tail->x + tail->background_width;
18090 else
18091 x_reached = x;
18092
18093 /* If there are any glyphs with lbearing < 0 or rbearing > width in
18094 the row, redraw some glyphs in front or following the glyph
18095 strings built above. */
18096 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
18097 {
18098 int dummy_x = 0;
18099 struct glyph_string *h, *t;
18100
18101 /* Compute overhangs for all glyph strings. */
18102 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
18103 for (s = head; s; s = s->next)
18104 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
18105
18106 /* Prepend glyph strings for glyphs in front of the first glyph
18107 string that are overwritten because of the first glyph
18108 string's left overhang. The background of all strings
18109 prepended must be drawn because the first glyph string
18110 draws over it. */
18111 i = left_overwritten (head);
18112 if (i >= 0)
18113 {
18114 j = i;
18115 BUILD_GLYPH_STRINGS (j, start, h, t,
18116 DRAW_NORMAL_TEXT, dummy_x, last_x);
18117 start = i;
18118 compute_overhangs_and_x (t, head->x, 1);
18119 prepend_glyph_string_lists (&head, &tail, h, t);
18120 }
18121
18122 /* Prepend glyph strings for glyphs in front of the first glyph
18123 string that overwrite that glyph string because of their
18124 right overhang. For these strings, only the foreground must
18125 be drawn, because it draws over the glyph string at `head'.
18126 The background must not be drawn because this would overwrite
18127 right overhangs of preceding glyphs for which no glyph
18128 strings exist. */
18129 i = left_overwriting (head);
18130 if (i >= 0)
18131 {
18132 BUILD_GLYPH_STRINGS (i, start, h, t,
18133 DRAW_NORMAL_TEXT, dummy_x, last_x);
18134 for (s = h; s; s = s->next)
18135 s->background_filled_p = 1;
18136 compute_overhangs_and_x (t, head->x, 1);
18137 prepend_glyph_string_lists (&head, &tail, h, t);
18138 }
18139
18140 /* Append glyphs strings for glyphs following the last glyph
18141 string tail that are overwritten by tail. The background of
18142 these strings has to be drawn because tail's foreground draws
18143 over it. */
18144 i = right_overwritten (tail);
18145 if (i >= 0)
18146 {
18147 BUILD_GLYPH_STRINGS (end, i, h, t,
18148 DRAW_NORMAL_TEXT, x, last_x);
18149 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18150 append_glyph_string_lists (&head, &tail, h, t);
18151 }
18152
18153 /* Append glyph strings for glyphs following the last glyph
18154 string tail that overwrite tail. The foreground of such
18155 glyphs has to be drawn because it writes into the background
18156 of tail. The background must not be drawn because it could
18157 paint over the foreground of following glyphs. */
18158 i = right_overwriting (tail);
18159 if (i >= 0)
18160 {
18161 BUILD_GLYPH_STRINGS (end, i, h, t,
18162 DRAW_NORMAL_TEXT, x, last_x);
18163 for (s = h; s; s = s->next)
18164 s->background_filled_p = 1;
18165 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18166 append_glyph_string_lists (&head, &tail, h, t);
18167 }
18168 }
18169
18170 /* Draw all strings. */
18171 for (s = head; s; s = s->next)
18172 FRAME_RIF (f)->draw_glyph_string (s);
18173
18174 if (area == TEXT_AREA
18175 && !row->full_width_p
18176 /* When drawing overlapping rows, only the glyph strings'
18177 foreground is drawn, which doesn't erase a cursor
18178 completely. */
18179 && !overlaps_p)
18180 {
18181 int x0 = head ? head->x : x;
18182 int x1 = tail ? tail->x + tail->background_width : x;
18183
18184 int text_left = window_box_left (w, TEXT_AREA);
18185 x0 -= text_left;
18186 x1 -= text_left;
18187
18188 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18189 row->y, MATRIX_ROW_BOTTOM_Y (row));
18190 }
18191
18192 /* Value is the x-position up to which drawn, relative to AREA of W.
18193 This doesn't include parts drawn because of overhangs. */
18194 if (row->full_width_p)
18195 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18196 else
18197 x_reached -= window_box_left (w, area);
18198
18199 RELEASE_HDC (hdc, f);
18200
18201 return x_reached;
18202 }
18203
18204
18205 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18206 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18207
18208 static INLINE void
18209 append_glyph (it)
18210 struct it *it;
18211 {
18212 struct glyph *glyph;
18213 enum glyph_row_area area = it->area;
18214
18215 xassert (it->glyph_row);
18216 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18217
18218 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18219 if (glyph < it->glyph_row->glyphs[area + 1])
18220 {
18221 glyph->charpos = CHARPOS (it->position);
18222 glyph->object = it->object;
18223 glyph->pixel_width = it->pixel_width;
18224 glyph->ascent = it->ascent;
18225 glyph->descent = it->descent;
18226 glyph->voffset = it->voffset;
18227 glyph->type = CHAR_GLYPH;
18228 glyph->multibyte_p = it->multibyte_p;
18229 glyph->left_box_line_p = it->start_of_box_run_p;
18230 glyph->right_box_line_p = it->end_of_box_run_p;
18231 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18232 || it->phys_descent > it->descent);
18233 glyph->padding_p = 0;
18234 glyph->glyph_not_available_p = it->glyph_not_available_p;
18235 glyph->face_id = it->face_id;
18236 glyph->u.ch = it->char_to_display;
18237 glyph->slice = null_glyph_slice;
18238 glyph->font_type = FONT_TYPE_UNKNOWN;
18239 ++it->glyph_row->used[area];
18240 }
18241 else if (!fonts_changed_p)
18242 {
18243 it->w->ncols_scale_factor++;
18244 fonts_changed_p = 1;
18245 }
18246 }
18247
18248 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18249 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18250
18251 static INLINE void
18252 append_composite_glyph (it)
18253 struct it *it;
18254 {
18255 struct glyph *glyph;
18256 enum glyph_row_area area = it->area;
18257
18258 xassert (it->glyph_row);
18259
18260 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18261 if (glyph < it->glyph_row->glyphs[area + 1])
18262 {
18263 glyph->charpos = CHARPOS (it->position);
18264 glyph->object = it->object;
18265 glyph->pixel_width = it->pixel_width;
18266 glyph->ascent = it->ascent;
18267 glyph->descent = it->descent;
18268 glyph->voffset = it->voffset;
18269 glyph->type = COMPOSITE_GLYPH;
18270 glyph->multibyte_p = it->multibyte_p;
18271 glyph->left_box_line_p = it->start_of_box_run_p;
18272 glyph->right_box_line_p = it->end_of_box_run_p;
18273 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18274 || it->phys_descent > it->descent);
18275 glyph->padding_p = 0;
18276 glyph->glyph_not_available_p = 0;
18277 glyph->face_id = it->face_id;
18278 glyph->u.cmp_id = it->cmp_id;
18279 glyph->slice = null_glyph_slice;
18280 glyph->font_type = FONT_TYPE_UNKNOWN;
18281 ++it->glyph_row->used[area];
18282 }
18283 else if (!fonts_changed_p)
18284 {
18285 it->w->ncols_scale_factor++;
18286 fonts_changed_p = 1;
18287 }
18288 }
18289
18290
18291 /* Change IT->ascent and IT->height according to the setting of
18292 IT->voffset. */
18293
18294 static INLINE void
18295 take_vertical_position_into_account (it)
18296 struct it *it;
18297 {
18298 if (it->voffset)
18299 {
18300 if (it->voffset < 0)
18301 /* Increase the ascent so that we can display the text higher
18302 in the line. */
18303 it->ascent -= it->voffset;
18304 else
18305 /* Increase the descent so that we can display the text lower
18306 in the line. */
18307 it->descent += it->voffset;
18308 }
18309 }
18310
18311
18312 /* Produce glyphs/get display metrics for the image IT is loaded with.
18313 See the description of struct display_iterator in dispextern.h for
18314 an overview of struct display_iterator. */
18315
18316 static void
18317 produce_image_glyph (it)
18318 struct it *it;
18319 {
18320 struct image *img;
18321 struct face *face;
18322 int face_ascent, glyph_ascent;
18323 struct glyph_slice slice;
18324
18325 xassert (it->what == IT_IMAGE);
18326
18327 face = FACE_FROM_ID (it->f, it->face_id);
18328 xassert (face);
18329 /* Make sure X resources of the face is loaded. */
18330 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18331
18332 if (it->image_id < 0)
18333 {
18334 /* Fringe bitmap. */
18335 it->ascent = it->phys_ascent = 0;
18336 it->descent = it->phys_descent = 0;
18337 it->pixel_width = 0;
18338 it->nglyphs = 0;
18339 return;
18340 }
18341
18342 img = IMAGE_FROM_ID (it->f, it->image_id);
18343 xassert (img);
18344 /* Make sure X resources of the image is loaded. */
18345 prepare_image_for_display (it->f, img);
18346
18347 slice.x = slice.y = 0;
18348 slice.width = img->width;
18349 slice.height = img->height;
18350
18351 if (INTEGERP (it->slice.x))
18352 slice.x = XINT (it->slice.x);
18353 else if (FLOATP (it->slice.x))
18354 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
18355
18356 if (INTEGERP (it->slice.y))
18357 slice.y = XINT (it->slice.y);
18358 else if (FLOATP (it->slice.y))
18359 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
18360
18361 if (INTEGERP (it->slice.width))
18362 slice.width = XINT (it->slice.width);
18363 else if (FLOATP (it->slice.width))
18364 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
18365
18366 if (INTEGERP (it->slice.height))
18367 slice.height = XINT (it->slice.height);
18368 else if (FLOATP (it->slice.height))
18369 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
18370
18371 if (slice.x >= img->width)
18372 slice.x = img->width;
18373 if (slice.y >= img->height)
18374 slice.y = img->height;
18375 if (slice.x + slice.width >= img->width)
18376 slice.width = img->width - slice.x;
18377 if (slice.y + slice.height > img->height)
18378 slice.height = img->height - slice.y;
18379
18380 if (slice.width == 0 || slice.height == 0)
18381 return;
18382
18383 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
18384
18385 it->descent = slice.height - glyph_ascent;
18386 if (slice.y == 0)
18387 it->descent += img->vmargin;
18388 if (slice.y + slice.height == img->height)
18389 it->descent += img->vmargin;
18390 it->phys_descent = it->descent;
18391
18392 it->pixel_width = slice.width;
18393 if (slice.x == 0)
18394 it->pixel_width += img->hmargin;
18395 if (slice.x + slice.width == img->width)
18396 it->pixel_width += img->hmargin;
18397
18398 /* It's quite possible for images to have an ascent greater than
18399 their height, so don't get confused in that case. */
18400 if (it->descent < 0)
18401 it->descent = 0;
18402
18403 #if 0 /* this breaks image tiling */
18404 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
18405 face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
18406 if (face_ascent > it->ascent)
18407 it->ascent = it->phys_ascent = face_ascent;
18408 #endif
18409
18410 it->nglyphs = 1;
18411
18412 if (face->box != FACE_NO_BOX)
18413 {
18414 if (face->box_line_width > 0)
18415 {
18416 if (slice.y == 0)
18417 it->ascent += face->box_line_width;
18418 if (slice.y + slice.height == img->height)
18419 it->descent += face->box_line_width;
18420 }
18421
18422 if (it->start_of_box_run_p && slice.x == 0)
18423 it->pixel_width += abs (face->box_line_width);
18424 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
18425 it->pixel_width += abs (face->box_line_width);
18426 }
18427
18428 take_vertical_position_into_account (it);
18429
18430 if (it->glyph_row)
18431 {
18432 struct glyph *glyph;
18433 enum glyph_row_area area = it->area;
18434
18435 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18436 if (glyph < it->glyph_row->glyphs[area + 1])
18437 {
18438 glyph->charpos = CHARPOS (it->position);
18439 glyph->object = it->object;
18440 glyph->pixel_width = it->pixel_width;
18441 glyph->ascent = glyph_ascent;
18442 glyph->descent = it->descent;
18443 glyph->voffset = it->voffset;
18444 glyph->type = IMAGE_GLYPH;
18445 glyph->multibyte_p = it->multibyte_p;
18446 glyph->left_box_line_p = it->start_of_box_run_p;
18447 glyph->right_box_line_p = it->end_of_box_run_p;
18448 glyph->overlaps_vertically_p = 0;
18449 glyph->padding_p = 0;
18450 glyph->glyph_not_available_p = 0;
18451 glyph->face_id = it->face_id;
18452 glyph->u.img_id = img->id;
18453 glyph->slice = slice;
18454 glyph->font_type = FONT_TYPE_UNKNOWN;
18455 ++it->glyph_row->used[area];
18456 }
18457 else if (!fonts_changed_p)
18458 {
18459 it->w->ncols_scale_factor++;
18460 fonts_changed_p = 1;
18461 }
18462 }
18463 }
18464
18465
18466 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
18467 of the glyph, WIDTH and HEIGHT are the width and height of the
18468 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
18469
18470 static void
18471 append_stretch_glyph (it, object, width, height, ascent)
18472 struct it *it;
18473 Lisp_Object object;
18474 int width, height;
18475 int ascent;
18476 {
18477 struct glyph *glyph;
18478 enum glyph_row_area area = it->area;
18479
18480 xassert (ascent >= 0 && ascent <= height);
18481
18482 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18483 if (glyph < it->glyph_row->glyphs[area + 1])
18484 {
18485 glyph->charpos = CHARPOS (it->position);
18486 glyph->object = object;
18487 glyph->pixel_width = width;
18488 glyph->ascent = ascent;
18489 glyph->descent = height - ascent;
18490 glyph->voffset = it->voffset;
18491 glyph->type = STRETCH_GLYPH;
18492 glyph->multibyte_p = it->multibyte_p;
18493 glyph->left_box_line_p = it->start_of_box_run_p;
18494 glyph->right_box_line_p = it->end_of_box_run_p;
18495 glyph->overlaps_vertically_p = 0;
18496 glyph->padding_p = 0;
18497 glyph->glyph_not_available_p = 0;
18498 glyph->face_id = it->face_id;
18499 glyph->u.stretch.ascent = ascent;
18500 glyph->u.stretch.height = height;
18501 glyph->slice = null_glyph_slice;
18502 glyph->font_type = FONT_TYPE_UNKNOWN;
18503 ++it->glyph_row->used[area];
18504 }
18505 else if (!fonts_changed_p)
18506 {
18507 it->w->ncols_scale_factor++;
18508 fonts_changed_p = 1;
18509 }
18510 }
18511
18512
18513 /* Produce a stretch glyph for iterator IT. IT->object is the value
18514 of the glyph property displayed. The value must be a list
18515 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
18516 being recognized:
18517
18518 1. `:width WIDTH' specifies that the space should be WIDTH *
18519 canonical char width wide. WIDTH may be an integer or floating
18520 point number.
18521
18522 2. `:relative-width FACTOR' specifies that the width of the stretch
18523 should be computed from the width of the first character having the
18524 `glyph' property, and should be FACTOR times that width.
18525
18526 3. `:align-to HPOS' specifies that the space should be wide enough
18527 to reach HPOS, a value in canonical character units.
18528
18529 Exactly one of the above pairs must be present.
18530
18531 4. `:height HEIGHT' specifies that the height of the stretch produced
18532 should be HEIGHT, measured in canonical character units.
18533
18534 5. `:relative-height FACTOR' specifies that the height of the
18535 stretch should be FACTOR times the height of the characters having
18536 the glyph property.
18537
18538 Either none or exactly one of 4 or 5 must be present.
18539
18540 6. `:ascent ASCENT' specifies that ASCENT percent of the height
18541 of the stretch should be used for the ascent of the stretch.
18542 ASCENT must be in the range 0 <= ASCENT <= 100. */
18543
18544 static void
18545 produce_stretch_glyph (it)
18546 struct it *it;
18547 {
18548 /* (space :width WIDTH :height HEIGHT ...) */
18549 Lisp_Object prop, plist;
18550 int width = 0, height = 0, align_to = -1;
18551 int zero_width_ok_p = 0, zero_height_ok_p = 0;
18552 int ascent = 0;
18553 double tem;
18554 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18555 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
18556
18557 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18558
18559 /* List should start with `space'. */
18560 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
18561 plist = XCDR (it->object);
18562
18563 /* Compute the width of the stretch. */
18564 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
18565 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
18566 {
18567 /* Absolute width `:width WIDTH' specified and valid. */
18568 zero_width_ok_p = 1;
18569 width = (int)tem;
18570 }
18571 else if (prop = Fplist_get (plist, QCrelative_width),
18572 NUMVAL (prop) > 0)
18573 {
18574 /* Relative width `:relative-width FACTOR' specified and valid.
18575 Compute the width of the characters having the `glyph'
18576 property. */
18577 struct it it2;
18578 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
18579
18580 it2 = *it;
18581 if (it->multibyte_p)
18582 {
18583 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
18584 - IT_BYTEPOS (*it));
18585 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
18586 }
18587 else
18588 it2.c = *p, it2.len = 1;
18589
18590 it2.glyph_row = NULL;
18591 it2.what = IT_CHARACTER;
18592 x_produce_glyphs (&it2);
18593 width = NUMVAL (prop) * it2.pixel_width;
18594 }
18595 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
18596 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
18597 {
18598 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
18599 align_to = (align_to < 0
18600 ? 0
18601 : align_to - window_box_left_offset (it->w, TEXT_AREA));
18602 else if (align_to < 0)
18603 align_to = window_box_left_offset (it->w, TEXT_AREA);
18604 width = max (0, (int)tem + align_to - it->current_x);
18605 zero_width_ok_p = 1;
18606 }
18607 else
18608 /* Nothing specified -> width defaults to canonical char width. */
18609 width = FRAME_COLUMN_WIDTH (it->f);
18610
18611 if (width <= 0 && (width < 0 || !zero_width_ok_p))
18612 width = 1;
18613
18614 /* Compute height. */
18615 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
18616 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18617 {
18618 height = (int)tem;
18619 zero_height_ok_p = 1;
18620 }
18621 else if (prop = Fplist_get (plist, QCrelative_height),
18622 NUMVAL (prop) > 0)
18623 height = FONT_HEIGHT (font) * NUMVAL (prop);
18624 else
18625 height = FONT_HEIGHT (font);
18626
18627 if (height <= 0 && (height < 0 || !zero_height_ok_p))
18628 height = 1;
18629
18630 /* Compute percentage of height used for ascent. If
18631 `:ascent ASCENT' is present and valid, use that. Otherwise,
18632 derive the ascent from the font in use. */
18633 if (prop = Fplist_get (plist, QCascent),
18634 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
18635 ascent = height * NUMVAL (prop) / 100.0;
18636 else if (!NILP (prop)
18637 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18638 ascent = min (max (0, (int)tem), height);
18639 else
18640 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
18641
18642 if (width > 0 && height > 0 && it->glyph_row)
18643 {
18644 Lisp_Object object = it->stack[it->sp - 1].string;
18645 if (!STRINGP (object))
18646 object = it->w->buffer;
18647 append_stretch_glyph (it, object, width, height, ascent);
18648 }
18649
18650 it->pixel_width = width;
18651 it->ascent = it->phys_ascent = ascent;
18652 it->descent = it->phys_descent = height - it->ascent;
18653 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
18654
18655 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
18656 {
18657 if (face->box_line_width > 0)
18658 {
18659 it->ascent += face->box_line_width;
18660 it->descent += face->box_line_width;
18661 }
18662
18663 if (it->start_of_box_run_p)
18664 it->pixel_width += abs (face->box_line_width);
18665 if (it->end_of_box_run_p)
18666 it->pixel_width += abs (face->box_line_width);
18667 }
18668
18669 take_vertical_position_into_account (it);
18670 }
18671
18672 /* Calculate line-height and line-spacing properties.
18673 An integer value specifies explicit pixel value.
18674 A float value specifies relative value to current face height.
18675 A cons (float . face-name) specifies relative value to
18676 height of specified face font.
18677
18678 Returns height in pixels, or nil. */
18679
18680 static Lisp_Object
18681 calc_line_height_property (it, prop, font, boff, total)
18682 struct it *it;
18683 Lisp_Object prop;
18684 XFontStruct *font;
18685 int boff, *total;
18686 {
18687 Lisp_Object position, val;
18688 Lisp_Object face_name = Qnil;
18689 int ascent, descent, height, override;
18690
18691 if (STRINGP (it->object))
18692 position = make_number (IT_STRING_CHARPOS (*it));
18693 else
18694 position = make_number (IT_CHARPOS (*it));
18695
18696 val = Fget_char_property (position, prop, it->object);
18697
18698 if (NILP (val))
18699 return val;
18700
18701 if (total && CONSP (val) && EQ (XCAR (val), Qtotal))
18702 {
18703 *total = 1;
18704 val = XCDR (val);
18705 }
18706
18707 if (INTEGERP (val))
18708 return val;
18709
18710 if (CONSP (val))
18711 {
18712 face_name = XCDR (val);
18713 val = XCAR (val);
18714 }
18715 else if (SYMBOLP (val))
18716 {
18717 face_name = val;
18718 val = Qnil;
18719 }
18720
18721 override = EQ (prop, Qline_height);
18722
18723 if (NILP (face_name))
18724 {
18725 font = FRAME_FONT (it->f);
18726 boff = FRAME_BASELINE_OFFSET (it->f);
18727 }
18728 else if (EQ (face_name, Qt))
18729 {
18730 override = 0;
18731 }
18732 else
18733 {
18734 int face_id;
18735 struct face *face;
18736 struct font_info *font_info;
18737
18738 face_id = lookup_named_face (it->f, face_name, ' ');
18739 if (face_id < 0)
18740 return make_number (-1);
18741
18742 face = FACE_FROM_ID (it->f, face_id);
18743 font = face->font;
18744 if (font == NULL)
18745 return make_number (-1);
18746
18747 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18748 boff = font_info->baseline_offset;
18749 if (font_info->vertical_centering)
18750 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18751 }
18752
18753 ascent = FONT_BASE (font) + boff;
18754 descent = FONT_DESCENT (font) - boff;
18755
18756 if (override)
18757 {
18758 it->override_ascent = ascent;
18759 it->override_descent = descent;
18760 it->override_boff = boff;
18761 }
18762
18763 height = ascent + descent;
18764 if (FLOATP (val))
18765 height = (int)(XFLOAT_DATA (val) * height);
18766 else if (INTEGERP (val))
18767 height *= XINT (val);
18768
18769 return make_number (height);
18770 }
18771
18772
18773 /* RIF:
18774 Produce glyphs/get display metrics for the display element IT is
18775 loaded with. See the description of struct display_iterator in
18776 dispextern.h for an overview of struct display_iterator. */
18777
18778 void
18779 x_produce_glyphs (it)
18780 struct it *it;
18781 {
18782 int extra_line_spacing = it->extra_line_spacing;
18783
18784 it->glyph_not_available_p = 0;
18785
18786 if (it->what == IT_CHARACTER)
18787 {
18788 XChar2b char2b;
18789 XFontStruct *font;
18790 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18791 XCharStruct *pcm;
18792 int font_not_found_p;
18793 struct font_info *font_info;
18794 int boff; /* baseline offset */
18795 /* We may change it->multibyte_p upon unibyte<->multibyte
18796 conversion. So, save the current value now and restore it
18797 later.
18798
18799 Note: It seems that we don't have to record multibyte_p in
18800 struct glyph because the character code itself tells if or
18801 not the character is multibyte. Thus, in the future, we must
18802 consider eliminating the field `multibyte_p' in the struct
18803 glyph. */
18804 int saved_multibyte_p = it->multibyte_p;
18805
18806 /* Maybe translate single-byte characters to multibyte, or the
18807 other way. */
18808 it->char_to_display = it->c;
18809 if (!ASCII_BYTE_P (it->c))
18810 {
18811 if (unibyte_display_via_language_environment
18812 && SINGLE_BYTE_CHAR_P (it->c)
18813 && (it->c >= 0240
18814 || !NILP (Vnonascii_translation_table)))
18815 {
18816 it->char_to_display = unibyte_char_to_multibyte (it->c);
18817 it->multibyte_p = 1;
18818 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18819 face = FACE_FROM_ID (it->f, it->face_id);
18820 }
18821 else if (!SINGLE_BYTE_CHAR_P (it->c)
18822 && !it->multibyte_p)
18823 {
18824 it->multibyte_p = 1;
18825 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18826 face = FACE_FROM_ID (it->f, it->face_id);
18827 }
18828 }
18829
18830 /* Get font to use. Encode IT->char_to_display. */
18831 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18832 &char2b, it->multibyte_p, 0);
18833 font = face->font;
18834
18835 /* When no suitable font found, use the default font. */
18836 font_not_found_p = font == NULL;
18837 if (font_not_found_p)
18838 {
18839 font = FRAME_FONT (it->f);
18840 boff = FRAME_BASELINE_OFFSET (it->f);
18841 font_info = NULL;
18842 }
18843 else
18844 {
18845 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18846 boff = font_info->baseline_offset;
18847 if (font_info->vertical_centering)
18848 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18849 }
18850
18851 if (it->char_to_display >= ' '
18852 && (!it->multibyte_p || it->char_to_display < 128))
18853 {
18854 /* Either unibyte or ASCII. */
18855 int stretched_p;
18856
18857 it->nglyphs = 1;
18858
18859 pcm = FRAME_RIF (it->f)->per_char_metric
18860 (font, &char2b, FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
18861
18862 if (it->override_ascent >= 0)
18863 {
18864 it->ascent = it->override_ascent;
18865 it->descent = it->override_descent;
18866 boff = it->override_boff;
18867 }
18868 else
18869 {
18870 it->ascent = FONT_BASE (font) + boff;
18871 it->descent = FONT_DESCENT (font) - boff;
18872 }
18873
18874 if (pcm)
18875 {
18876 it->phys_ascent = pcm->ascent + boff;
18877 it->phys_descent = pcm->descent - boff;
18878 it->pixel_width = pcm->width;
18879 }
18880 else
18881 {
18882 it->glyph_not_available_p = 1;
18883 it->phys_ascent = it->ascent;
18884 it->phys_descent = it->descent;
18885 it->pixel_width = FONT_WIDTH (font);
18886 }
18887
18888 if (it->constrain_row_ascent_descent_p)
18889 {
18890 if (it->descent > it->max_descent)
18891 {
18892 it->ascent += it->descent - it->max_descent;
18893 it->descent = it->max_descent;
18894 }
18895 if (it->ascent > it->max_ascent)
18896 {
18897 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
18898 it->ascent = it->max_ascent;
18899 }
18900 it->phys_ascent = min (it->phys_ascent, it->ascent);
18901 it->phys_descent = min (it->phys_descent, it->descent);
18902 extra_line_spacing = 0;
18903 }
18904
18905 /* If this is a space inside a region of text with
18906 `space-width' property, change its width. */
18907 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
18908 if (stretched_p)
18909 it->pixel_width *= XFLOATINT (it->space_width);
18910
18911 /* If face has a box, add the box thickness to the character
18912 height. If character has a box line to the left and/or
18913 right, add the box line width to the character's width. */
18914 if (face->box != FACE_NO_BOX)
18915 {
18916 int thick = face->box_line_width;
18917
18918 if (thick > 0)
18919 {
18920 it->ascent += thick;
18921 it->descent += thick;
18922 }
18923 else
18924 thick = -thick;
18925
18926 if (it->start_of_box_run_p)
18927 it->pixel_width += thick;
18928 if (it->end_of_box_run_p)
18929 it->pixel_width += thick;
18930 }
18931
18932 /* If face has an overline, add the height of the overline
18933 (1 pixel) and a 1 pixel margin to the character height. */
18934 if (face->overline_p)
18935 it->ascent += 2;
18936
18937 if (it->constrain_row_ascent_descent_p)
18938 {
18939 if (it->ascent > it->max_ascent)
18940 it->ascent = it->max_ascent;
18941 if (it->descent > it->max_descent)
18942 it->descent = it->max_descent;
18943 }
18944
18945 take_vertical_position_into_account (it);
18946
18947 /* If we have to actually produce glyphs, do it. */
18948 if (it->glyph_row)
18949 {
18950 if (stretched_p)
18951 {
18952 /* Translate a space with a `space-width' property
18953 into a stretch glyph. */
18954 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
18955 / FONT_HEIGHT (font));
18956 append_stretch_glyph (it, it->object, it->pixel_width,
18957 it->ascent + it->descent, ascent);
18958 }
18959 else
18960 append_glyph (it);
18961
18962 /* If characters with lbearing or rbearing are displayed
18963 in this line, record that fact in a flag of the
18964 glyph row. This is used to optimize X output code. */
18965 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
18966 it->glyph_row->contains_overlapping_glyphs_p = 1;
18967 }
18968 }
18969 else if (it->char_to_display == '\n')
18970 {
18971 /* A newline has no width but we need the height of the line.
18972 But if previous part of the line set a height, don't
18973 increase that height */
18974
18975 Lisp_Object height;
18976
18977 it->override_ascent = -1;
18978 it->pixel_width = 0;
18979 it->nglyphs = 0;
18980
18981 height = calc_line_height_property(it, Qline_height, font, boff, 0);
18982
18983 if (it->override_ascent >= 0)
18984 {
18985 it->ascent = it->override_ascent;
18986 it->descent = it->override_descent;
18987 boff = it->override_boff;
18988 }
18989 else
18990 {
18991 it->ascent = FONT_BASE (font) + boff;
18992 it->descent = FONT_DESCENT (font) - boff;
18993 }
18994
18995 if (EQ (height, make_number(0)))
18996 {
18997 if (it->descent > it->max_descent)
18998 {
18999 it->ascent += it->descent - it->max_descent;
19000 it->descent = it->max_descent;
19001 }
19002 if (it->ascent > it->max_ascent)
19003 {
19004 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19005 it->ascent = it->max_ascent;
19006 }
19007 it->phys_ascent = min (it->phys_ascent, it->ascent);
19008 it->phys_descent = min (it->phys_descent, it->descent);
19009 it->constrain_row_ascent_descent_p = 1;
19010 extra_line_spacing = 0;
19011 }
19012 else
19013 {
19014 Lisp_Object spacing;
19015 int total = 0;
19016
19017 it->phys_ascent = it->ascent;
19018 it->phys_descent = it->descent;
19019
19020 if ((it->max_ascent > 0 || it->max_descent > 0)
19021 && face->box != FACE_NO_BOX
19022 && face->box_line_width > 0)
19023 {
19024 it->ascent += face->box_line_width;
19025 it->descent += face->box_line_width;
19026 }
19027 if (!NILP (height)
19028 && XINT (height) > it->ascent + it->descent)
19029 it->ascent = XINT (height) - it->descent;
19030
19031 spacing = calc_line_height_property(it, Qline_spacing, font, boff, &total);
19032 if (INTEGERP (spacing))
19033 {
19034 extra_line_spacing = XINT (spacing);
19035 if (total)
19036 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19037 }
19038 }
19039 }
19040 else if (it->char_to_display == '\t')
19041 {
19042 int tab_width = it->tab_width * FRAME_COLUMN_WIDTH (it->f);
19043 int x = it->current_x + it->continuation_lines_width;
19044 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
19045
19046 /* If the distance from the current position to the next tab
19047 stop is less than a canonical character width, use the
19048 tab stop after that. */
19049 if (next_tab_x - x < FRAME_COLUMN_WIDTH (it->f))
19050 next_tab_x += tab_width;
19051
19052 it->pixel_width = next_tab_x - x;
19053 it->nglyphs = 1;
19054 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
19055 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
19056
19057 if (it->glyph_row)
19058 {
19059 append_stretch_glyph (it, it->object, it->pixel_width,
19060 it->ascent + it->descent, it->ascent);
19061 }
19062 }
19063 else
19064 {
19065 /* A multi-byte character. Assume that the display width of the
19066 character is the width of the character multiplied by the
19067 width of the font. */
19068
19069 /* If we found a font, this font should give us the right
19070 metrics. If we didn't find a font, use the frame's
19071 default font and calculate the width of the character
19072 from the charset width; this is what old redisplay code
19073 did. */
19074
19075 pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
19076 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
19077
19078 if (font_not_found_p || !pcm)
19079 {
19080 int charset = CHAR_CHARSET (it->char_to_display);
19081
19082 it->glyph_not_available_p = 1;
19083 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
19084 * CHARSET_WIDTH (charset));
19085 it->phys_ascent = FONT_BASE (font) + boff;
19086 it->phys_descent = FONT_DESCENT (font) - boff;
19087 }
19088 else
19089 {
19090 it->pixel_width = pcm->width;
19091 it->phys_ascent = pcm->ascent + boff;
19092 it->phys_descent = pcm->descent - boff;
19093 if (it->glyph_row
19094 && (pcm->lbearing < 0
19095 || pcm->rbearing > pcm->width))
19096 it->glyph_row->contains_overlapping_glyphs_p = 1;
19097 }
19098 it->nglyphs = 1;
19099 it->ascent = FONT_BASE (font) + boff;
19100 it->descent = FONT_DESCENT (font) - boff;
19101 if (face->box != FACE_NO_BOX)
19102 {
19103 int thick = face->box_line_width;
19104
19105 if (thick > 0)
19106 {
19107 it->ascent += thick;
19108 it->descent += thick;
19109 }
19110 else
19111 thick = - thick;
19112
19113 if (it->start_of_box_run_p)
19114 it->pixel_width += thick;
19115 if (it->end_of_box_run_p)
19116 it->pixel_width += thick;
19117 }
19118
19119 /* If face has an overline, add the height of the overline
19120 (1 pixel) and a 1 pixel margin to the character height. */
19121 if (face->overline_p)
19122 it->ascent += 2;
19123
19124 take_vertical_position_into_account (it);
19125
19126 if (it->glyph_row)
19127 append_glyph (it);
19128 }
19129 it->multibyte_p = saved_multibyte_p;
19130 }
19131 else if (it->what == IT_COMPOSITION)
19132 {
19133 /* Note: A composition is represented as one glyph in the
19134 glyph matrix. There are no padding glyphs. */
19135 XChar2b char2b;
19136 XFontStruct *font;
19137 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19138 XCharStruct *pcm;
19139 int font_not_found_p;
19140 struct font_info *font_info;
19141 int boff; /* baseline offset */
19142 struct composition *cmp = composition_table[it->cmp_id];
19143
19144 /* Maybe translate single-byte characters to multibyte. */
19145 it->char_to_display = it->c;
19146 if (unibyte_display_via_language_environment
19147 && SINGLE_BYTE_CHAR_P (it->c)
19148 && (it->c >= 0240
19149 || (it->c >= 0200
19150 && !NILP (Vnonascii_translation_table))))
19151 {
19152 it->char_to_display = unibyte_char_to_multibyte (it->c);
19153 }
19154
19155 /* Get face and font to use. Encode IT->char_to_display. */
19156 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19157 face = FACE_FROM_ID (it->f, it->face_id);
19158 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19159 &char2b, it->multibyte_p, 0);
19160 font = face->font;
19161
19162 /* When no suitable font found, use the default font. */
19163 font_not_found_p = font == NULL;
19164 if (font_not_found_p)
19165 {
19166 font = FRAME_FONT (it->f);
19167 boff = FRAME_BASELINE_OFFSET (it->f);
19168 font_info = NULL;
19169 }
19170 else
19171 {
19172 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19173 boff = font_info->baseline_offset;
19174 if (font_info->vertical_centering)
19175 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19176 }
19177
19178 /* There are no padding glyphs, so there is only one glyph to
19179 produce for the composition. Important is that pixel_width,
19180 ascent and descent are the values of what is drawn by
19181 draw_glyphs (i.e. the values of the overall glyphs composed). */
19182 it->nglyphs = 1;
19183
19184 /* If we have not yet calculated pixel size data of glyphs of
19185 the composition for the current face font, calculate them
19186 now. Theoretically, we have to check all fonts for the
19187 glyphs, but that requires much time and memory space. So,
19188 here we check only the font of the first glyph. This leads
19189 to incorrect display very rarely, and C-l (recenter) can
19190 correct the display anyway. */
19191 if (cmp->font != (void *) font)
19192 {
19193 /* Ascent and descent of the font of the first character of
19194 this composition (adjusted by baseline offset). Ascent
19195 and descent of overall glyphs should not be less than
19196 them respectively. */
19197 int font_ascent = FONT_BASE (font) + boff;
19198 int font_descent = FONT_DESCENT (font) - boff;
19199 /* Bounding box of the overall glyphs. */
19200 int leftmost, rightmost, lowest, highest;
19201 int i, width, ascent, descent;
19202
19203 cmp->font = (void *) font;
19204
19205 /* Initialize the bounding box. */
19206 if (font_info
19207 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
19208 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19209 {
19210 width = pcm->width;
19211 ascent = pcm->ascent;
19212 descent = pcm->descent;
19213 }
19214 else
19215 {
19216 width = FONT_WIDTH (font);
19217 ascent = FONT_BASE (font);
19218 descent = FONT_DESCENT (font);
19219 }
19220
19221 rightmost = width;
19222 lowest = - descent + boff;
19223 highest = ascent + boff;
19224 leftmost = 0;
19225
19226 if (font_info
19227 && font_info->default_ascent
19228 && CHAR_TABLE_P (Vuse_default_ascent)
19229 && !NILP (Faref (Vuse_default_ascent,
19230 make_number (it->char_to_display))))
19231 highest = font_info->default_ascent + boff;
19232
19233 /* Draw the first glyph at the normal position. It may be
19234 shifted to right later if some other glyphs are drawn at
19235 the left. */
19236 cmp->offsets[0] = 0;
19237 cmp->offsets[1] = boff;
19238
19239 /* Set cmp->offsets for the remaining glyphs. */
19240 for (i = 1; i < cmp->glyph_len; i++)
19241 {
19242 int left, right, btm, top;
19243 int ch = COMPOSITION_GLYPH (cmp, i);
19244 int face_id = FACE_FOR_CHAR (it->f, face, ch);
19245
19246 face = FACE_FROM_ID (it->f, face_id);
19247 get_char_face_and_encoding (it->f, ch, face->id,
19248 &char2b, it->multibyte_p, 0);
19249 font = face->font;
19250 if (font == NULL)
19251 {
19252 font = FRAME_FONT (it->f);
19253 boff = FRAME_BASELINE_OFFSET (it->f);
19254 font_info = NULL;
19255 }
19256 else
19257 {
19258 font_info
19259 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19260 boff = font_info->baseline_offset;
19261 if (font_info->vertical_centering)
19262 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19263 }
19264
19265 if (font_info
19266 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
19267 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19268 {
19269 width = pcm->width;
19270 ascent = pcm->ascent;
19271 descent = pcm->descent;
19272 }
19273 else
19274 {
19275 width = FONT_WIDTH (font);
19276 ascent = 1;
19277 descent = 0;
19278 }
19279
19280 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19281 {
19282 /* Relative composition with or without
19283 alternate chars. */
19284 left = (leftmost + rightmost - width) / 2;
19285 btm = - descent + boff;
19286 if (font_info && font_info->relative_compose
19287 && (! CHAR_TABLE_P (Vignore_relative_composition)
19288 || NILP (Faref (Vignore_relative_composition,
19289 make_number (ch)))))
19290 {
19291
19292 if (- descent >= font_info->relative_compose)
19293 /* One extra pixel between two glyphs. */
19294 btm = highest + 1;
19295 else if (ascent <= 0)
19296 /* One extra pixel between two glyphs. */
19297 btm = lowest - 1 - ascent - descent;
19298 }
19299 }
19300 else
19301 {
19302 /* A composition rule is specified by an integer
19303 value that encodes global and new reference
19304 points (GREF and NREF). GREF and NREF are
19305 specified by numbers as below:
19306
19307 0---1---2 -- ascent
19308 | |
19309 | |
19310 | |
19311 9--10--11 -- center
19312 | |
19313 ---3---4---5--- baseline
19314 | |
19315 6---7---8 -- descent
19316 */
19317 int rule = COMPOSITION_RULE (cmp, i);
19318 int gref, nref, grefx, grefy, nrefx, nrefy;
19319
19320 COMPOSITION_DECODE_RULE (rule, gref, nref);
19321 grefx = gref % 3, nrefx = nref % 3;
19322 grefy = gref / 3, nrefy = nref / 3;
19323
19324 left = (leftmost
19325 + grefx * (rightmost - leftmost) / 2
19326 - nrefx * width / 2);
19327 btm = ((grefy == 0 ? highest
19328 : grefy == 1 ? 0
19329 : grefy == 2 ? lowest
19330 : (highest + lowest) / 2)
19331 - (nrefy == 0 ? ascent + descent
19332 : nrefy == 1 ? descent - boff
19333 : nrefy == 2 ? 0
19334 : (ascent + descent) / 2));
19335 }
19336
19337 cmp->offsets[i * 2] = left;
19338 cmp->offsets[i * 2 + 1] = btm + descent;
19339
19340 /* Update the bounding box of the overall glyphs. */
19341 right = left + width;
19342 top = btm + descent + ascent;
19343 if (left < leftmost)
19344 leftmost = left;
19345 if (right > rightmost)
19346 rightmost = right;
19347 if (top > highest)
19348 highest = top;
19349 if (btm < lowest)
19350 lowest = btm;
19351 }
19352
19353 /* If there are glyphs whose x-offsets are negative,
19354 shift all glyphs to the right and make all x-offsets
19355 non-negative. */
19356 if (leftmost < 0)
19357 {
19358 for (i = 0; i < cmp->glyph_len; i++)
19359 cmp->offsets[i * 2] -= leftmost;
19360 rightmost -= leftmost;
19361 }
19362
19363 cmp->pixel_width = rightmost;
19364 cmp->ascent = highest;
19365 cmp->descent = - lowest;
19366 if (cmp->ascent < font_ascent)
19367 cmp->ascent = font_ascent;
19368 if (cmp->descent < font_descent)
19369 cmp->descent = font_descent;
19370 }
19371
19372 it->pixel_width = cmp->pixel_width;
19373 it->ascent = it->phys_ascent = cmp->ascent;
19374 it->descent = it->phys_descent = cmp->descent;
19375
19376 if (face->box != FACE_NO_BOX)
19377 {
19378 int thick = face->box_line_width;
19379
19380 if (thick > 0)
19381 {
19382 it->ascent += thick;
19383 it->descent += thick;
19384 }
19385 else
19386 thick = - thick;
19387
19388 if (it->start_of_box_run_p)
19389 it->pixel_width += thick;
19390 if (it->end_of_box_run_p)
19391 it->pixel_width += thick;
19392 }
19393
19394 /* If face has an overline, add the height of the overline
19395 (1 pixel) and a 1 pixel margin to the character height. */
19396 if (face->overline_p)
19397 it->ascent += 2;
19398
19399 take_vertical_position_into_account (it);
19400
19401 if (it->glyph_row)
19402 append_composite_glyph (it);
19403 }
19404 else if (it->what == IT_IMAGE)
19405 produce_image_glyph (it);
19406 else if (it->what == IT_STRETCH)
19407 produce_stretch_glyph (it);
19408
19409 /* Accumulate dimensions. Note: can't assume that it->descent > 0
19410 because this isn't true for images with `:ascent 100'. */
19411 xassert (it->ascent >= 0 && it->descent >= 0);
19412 if (it->area == TEXT_AREA)
19413 it->current_x += it->pixel_width;
19414
19415 if (extra_line_spacing > 0)
19416 it->descent += extra_line_spacing;
19417
19418 it->max_ascent = max (it->max_ascent, it->ascent);
19419 it->max_descent = max (it->max_descent, it->descent);
19420 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
19421 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
19422 }
19423
19424 /* EXPORT for RIF:
19425 Output LEN glyphs starting at START at the nominal cursor position.
19426 Advance the nominal cursor over the text. The global variable
19427 updated_window contains the window being updated, updated_row is
19428 the glyph row being updated, and updated_area is the area of that
19429 row being updated. */
19430
19431 void
19432 x_write_glyphs (start, len)
19433 struct glyph *start;
19434 int len;
19435 {
19436 int x, hpos;
19437
19438 xassert (updated_window && updated_row);
19439 BLOCK_INPUT;
19440
19441 /* Write glyphs. */
19442
19443 hpos = start - updated_row->glyphs[updated_area];
19444 x = draw_glyphs (updated_window, output_cursor.x,
19445 updated_row, updated_area,
19446 hpos, hpos + len,
19447 DRAW_NORMAL_TEXT, 0);
19448
19449 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
19450 if (updated_area == TEXT_AREA
19451 && updated_window->phys_cursor_on_p
19452 && updated_window->phys_cursor.vpos == output_cursor.vpos
19453 && updated_window->phys_cursor.hpos >= hpos
19454 && updated_window->phys_cursor.hpos < hpos + len)
19455 updated_window->phys_cursor_on_p = 0;
19456
19457 UNBLOCK_INPUT;
19458
19459 /* Advance the output cursor. */
19460 output_cursor.hpos += len;
19461 output_cursor.x = x;
19462 }
19463
19464
19465 /* EXPORT for RIF:
19466 Insert LEN glyphs from START at the nominal cursor position. */
19467
19468 void
19469 x_insert_glyphs (start, len)
19470 struct glyph *start;
19471 int len;
19472 {
19473 struct frame *f;
19474 struct window *w;
19475 int line_height, shift_by_width, shifted_region_width;
19476 struct glyph_row *row;
19477 struct glyph *glyph;
19478 int frame_x, frame_y, hpos;
19479
19480 xassert (updated_window && updated_row);
19481 BLOCK_INPUT;
19482 w = updated_window;
19483 f = XFRAME (WINDOW_FRAME (w));
19484
19485 /* Get the height of the line we are in. */
19486 row = updated_row;
19487 line_height = row->height;
19488
19489 /* Get the width of the glyphs to insert. */
19490 shift_by_width = 0;
19491 for (glyph = start; glyph < start + len; ++glyph)
19492 shift_by_width += glyph->pixel_width;
19493
19494 /* Get the width of the region to shift right. */
19495 shifted_region_width = (window_box_width (w, updated_area)
19496 - output_cursor.x
19497 - shift_by_width);
19498
19499 /* Shift right. */
19500 frame_x = window_box_left (w, updated_area) + output_cursor.x;
19501 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
19502
19503 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
19504 line_height, shift_by_width);
19505
19506 /* Write the glyphs. */
19507 hpos = start - row->glyphs[updated_area];
19508 draw_glyphs (w, output_cursor.x, row, updated_area,
19509 hpos, hpos + len,
19510 DRAW_NORMAL_TEXT, 0);
19511
19512 /* Advance the output cursor. */
19513 output_cursor.hpos += len;
19514 output_cursor.x += shift_by_width;
19515 UNBLOCK_INPUT;
19516 }
19517
19518
19519 /* EXPORT for RIF:
19520 Erase the current text line from the nominal cursor position
19521 (inclusive) to pixel column TO_X (exclusive). The idea is that
19522 everything from TO_X onward is already erased.
19523
19524 TO_X is a pixel position relative to updated_area of
19525 updated_window. TO_X == -1 means clear to the end of this area. */
19526
19527 void
19528 x_clear_end_of_line (to_x)
19529 int to_x;
19530 {
19531 struct frame *f;
19532 struct window *w = updated_window;
19533 int max_x, min_y, max_y;
19534 int from_x, from_y, to_y;
19535
19536 xassert (updated_window && updated_row);
19537 f = XFRAME (w->frame);
19538
19539 if (updated_row->full_width_p)
19540 max_x = WINDOW_TOTAL_WIDTH (w);
19541 else
19542 max_x = window_box_width (w, updated_area);
19543 max_y = window_text_bottom_y (w);
19544
19545 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
19546 of window. For TO_X > 0, truncate to end of drawing area. */
19547 if (to_x == 0)
19548 return;
19549 else if (to_x < 0)
19550 to_x = max_x;
19551 else
19552 to_x = min (to_x, max_x);
19553
19554 to_y = min (max_y, output_cursor.y + updated_row->height);
19555
19556 /* Notice if the cursor will be cleared by this operation. */
19557 if (!updated_row->full_width_p)
19558 notice_overwritten_cursor (w, updated_area,
19559 output_cursor.x, -1,
19560 updated_row->y,
19561 MATRIX_ROW_BOTTOM_Y (updated_row));
19562
19563 from_x = output_cursor.x;
19564
19565 /* Translate to frame coordinates. */
19566 if (updated_row->full_width_p)
19567 {
19568 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
19569 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
19570 }
19571 else
19572 {
19573 int area_left = window_box_left (w, updated_area);
19574 from_x += area_left;
19575 to_x += area_left;
19576 }
19577
19578 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
19579 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
19580 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
19581
19582 /* Prevent inadvertently clearing to end of the X window. */
19583 if (to_x > from_x && to_y > from_y)
19584 {
19585 BLOCK_INPUT;
19586 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
19587 to_x - from_x, to_y - from_y);
19588 UNBLOCK_INPUT;
19589 }
19590 }
19591
19592 #endif /* HAVE_WINDOW_SYSTEM */
19593
19594
19595 \f
19596 /***********************************************************************
19597 Cursor types
19598 ***********************************************************************/
19599
19600 /* Value is the internal representation of the specified cursor type
19601 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
19602 of the bar cursor. */
19603
19604 static enum text_cursor_kinds
19605 get_specified_cursor_type (arg, width)
19606 Lisp_Object arg;
19607 int *width;
19608 {
19609 enum text_cursor_kinds type;
19610
19611 if (NILP (arg))
19612 return NO_CURSOR;
19613
19614 if (EQ (arg, Qbox))
19615 return FILLED_BOX_CURSOR;
19616
19617 if (EQ (arg, Qhollow))
19618 return HOLLOW_BOX_CURSOR;
19619
19620 if (EQ (arg, Qbar))
19621 {
19622 *width = 2;
19623 return BAR_CURSOR;
19624 }
19625
19626 if (CONSP (arg)
19627 && EQ (XCAR (arg), Qbar)
19628 && INTEGERP (XCDR (arg))
19629 && XINT (XCDR (arg)) >= 0)
19630 {
19631 *width = XINT (XCDR (arg));
19632 return BAR_CURSOR;
19633 }
19634
19635 if (EQ (arg, Qhbar))
19636 {
19637 *width = 2;
19638 return HBAR_CURSOR;
19639 }
19640
19641 if (CONSP (arg)
19642 && EQ (XCAR (arg), Qhbar)
19643 && INTEGERP (XCDR (arg))
19644 && XINT (XCDR (arg)) >= 0)
19645 {
19646 *width = XINT (XCDR (arg));
19647 return HBAR_CURSOR;
19648 }
19649
19650 /* Treat anything unknown as "hollow box cursor".
19651 It was bad to signal an error; people have trouble fixing
19652 .Xdefaults with Emacs, when it has something bad in it. */
19653 type = HOLLOW_BOX_CURSOR;
19654
19655 return type;
19656 }
19657
19658 /* Set the default cursor types for specified frame. */
19659 void
19660 set_frame_cursor_types (f, arg)
19661 struct frame *f;
19662 Lisp_Object arg;
19663 {
19664 int width;
19665 Lisp_Object tem;
19666
19667 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
19668 FRAME_CURSOR_WIDTH (f) = width;
19669
19670 /* By default, set up the blink-off state depending on the on-state. */
19671
19672 tem = Fassoc (arg, Vblink_cursor_alist);
19673 if (!NILP (tem))
19674 {
19675 FRAME_BLINK_OFF_CURSOR (f)
19676 = get_specified_cursor_type (XCDR (tem), &width);
19677 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
19678 }
19679 else
19680 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
19681 }
19682
19683
19684 /* Return the cursor we want to be displayed in window W. Return
19685 width of bar/hbar cursor through WIDTH arg. Return with
19686 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
19687 (i.e. if the `system caret' should track this cursor).
19688
19689 In a mini-buffer window, we want the cursor only to appear if we
19690 are reading input from this window. For the selected window, we
19691 want the cursor type given by the frame parameter or buffer local
19692 setting of cursor-type. If explicitly marked off, draw no cursor.
19693 In all other cases, we want a hollow box cursor. */
19694
19695 static enum text_cursor_kinds
19696 get_window_cursor_type (w, glyph, width, active_cursor)
19697 struct window *w;
19698 struct glyph *glyph;
19699 int *width;
19700 int *active_cursor;
19701 {
19702 struct frame *f = XFRAME (w->frame);
19703 struct buffer *b = XBUFFER (w->buffer);
19704 int cursor_type = DEFAULT_CURSOR;
19705 Lisp_Object alt_cursor;
19706 int non_selected = 0;
19707
19708 *active_cursor = 1;
19709
19710 /* Echo area */
19711 if (cursor_in_echo_area
19712 && FRAME_HAS_MINIBUF_P (f)
19713 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
19714 {
19715 if (w == XWINDOW (echo_area_window))
19716 {
19717 *width = FRAME_CURSOR_WIDTH (f);
19718 return FRAME_DESIRED_CURSOR (f);
19719 }
19720
19721 *active_cursor = 0;
19722 non_selected = 1;
19723 }
19724
19725 /* Nonselected window or nonselected frame. */
19726 else if (w != XWINDOW (f->selected_window)
19727 #ifdef HAVE_WINDOW_SYSTEM
19728 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
19729 #endif
19730 )
19731 {
19732 *active_cursor = 0;
19733
19734 if (MINI_WINDOW_P (w) && minibuf_level == 0)
19735 return NO_CURSOR;
19736
19737 non_selected = 1;
19738 }
19739
19740 /* Never display a cursor in a window in which cursor-type is nil. */
19741 if (NILP (b->cursor_type))
19742 return NO_CURSOR;
19743
19744 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
19745 if (non_selected)
19746 {
19747 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
19748 return get_specified_cursor_type (alt_cursor, width);
19749 }
19750
19751 /* Get the normal cursor type for this window. */
19752 if (EQ (b->cursor_type, Qt))
19753 {
19754 cursor_type = FRAME_DESIRED_CURSOR (f);
19755 *width = FRAME_CURSOR_WIDTH (f);
19756 }
19757 else
19758 cursor_type = get_specified_cursor_type (b->cursor_type, width);
19759
19760 /* Use normal cursor if not blinked off. */
19761 if (!w->cursor_off_p)
19762 {
19763 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
19764 if (cursor_type == FILLED_BOX_CURSOR)
19765 cursor_type = HOLLOW_BOX_CURSOR;
19766 }
19767 return cursor_type;
19768 }
19769
19770 /* Cursor is blinked off, so determine how to "toggle" it. */
19771
19772 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
19773 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
19774 return get_specified_cursor_type (XCDR (alt_cursor), width);
19775
19776 /* Then see if frame has specified a specific blink off cursor type. */
19777 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
19778 {
19779 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
19780 return FRAME_BLINK_OFF_CURSOR (f);
19781 }
19782
19783 #if 0
19784 /* Some people liked having a permanently visible blinking cursor,
19785 while others had very strong opinions against it. So it was
19786 decided to remove it. KFS 2003-09-03 */
19787
19788 /* Finally perform built-in cursor blinking:
19789 filled box <-> hollow box
19790 wide [h]bar <-> narrow [h]bar
19791 narrow [h]bar <-> no cursor
19792 other type <-> no cursor */
19793
19794 if (cursor_type == FILLED_BOX_CURSOR)
19795 return HOLLOW_BOX_CURSOR;
19796
19797 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
19798 {
19799 *width = 1;
19800 return cursor_type;
19801 }
19802 #endif
19803
19804 return NO_CURSOR;
19805 }
19806
19807
19808 #ifdef HAVE_WINDOW_SYSTEM
19809
19810 /* Notice when the text cursor of window W has been completely
19811 overwritten by a drawing operation that outputs glyphs in AREA
19812 starting at X0 and ending at X1 in the line starting at Y0 and
19813 ending at Y1. X coordinates are area-relative. X1 < 0 means all
19814 the rest of the line after X0 has been written. Y coordinates
19815 are window-relative. */
19816
19817 static void
19818 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
19819 struct window *w;
19820 enum glyph_row_area area;
19821 int x0, y0, x1, y1;
19822 {
19823 int cx0, cx1, cy0, cy1;
19824 struct glyph_row *row;
19825
19826 if (!w->phys_cursor_on_p)
19827 return;
19828 if (area != TEXT_AREA)
19829 return;
19830
19831 row = w->current_matrix->rows + w->phys_cursor.vpos;
19832 if (!row->displays_text_p)
19833 return;
19834
19835 if (row->cursor_in_fringe_p)
19836 {
19837 row->cursor_in_fringe_p = 0;
19838 draw_fringe_bitmap (w, row, 0);
19839 w->phys_cursor_on_p = 0;
19840 return;
19841 }
19842
19843 cx0 = w->phys_cursor.x;
19844 cx1 = cx0 + w->phys_cursor_width;
19845 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
19846 return;
19847
19848 /* The cursor image will be completely removed from the
19849 screen if the output area intersects the cursor area in
19850 y-direction. When we draw in [y0 y1[, and some part of
19851 the cursor is at y < y0, that part must have been drawn
19852 before. When scrolling, the cursor is erased before
19853 actually scrolling, so we don't come here. When not
19854 scrolling, the rows above the old cursor row must have
19855 changed, and in this case these rows must have written
19856 over the cursor image.
19857
19858 Likewise if part of the cursor is below y1, with the
19859 exception of the cursor being in the first blank row at
19860 the buffer and window end because update_text_area
19861 doesn't draw that row. (Except when it does, but
19862 that's handled in update_text_area.) */
19863
19864 cy0 = w->phys_cursor.y;
19865 cy1 = cy0 + w->phys_cursor_height;
19866 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
19867 return;
19868
19869 w->phys_cursor_on_p = 0;
19870 }
19871
19872 #endif /* HAVE_WINDOW_SYSTEM */
19873
19874 \f
19875 /************************************************************************
19876 Mouse Face
19877 ************************************************************************/
19878
19879 #ifdef HAVE_WINDOW_SYSTEM
19880
19881 /* EXPORT for RIF:
19882 Fix the display of area AREA of overlapping row ROW in window W. */
19883
19884 void
19885 x_fix_overlapping_area (w, row, area)
19886 struct window *w;
19887 struct glyph_row *row;
19888 enum glyph_row_area area;
19889 {
19890 int i, x;
19891
19892 BLOCK_INPUT;
19893
19894 x = 0;
19895 for (i = 0; i < row->used[area];)
19896 {
19897 if (row->glyphs[area][i].overlaps_vertically_p)
19898 {
19899 int start = i, start_x = x;
19900
19901 do
19902 {
19903 x += row->glyphs[area][i].pixel_width;
19904 ++i;
19905 }
19906 while (i < row->used[area]
19907 && row->glyphs[area][i].overlaps_vertically_p);
19908
19909 draw_glyphs (w, start_x, row, area,
19910 start, i,
19911 DRAW_NORMAL_TEXT, 1);
19912 }
19913 else
19914 {
19915 x += row->glyphs[area][i].pixel_width;
19916 ++i;
19917 }
19918 }
19919
19920 UNBLOCK_INPUT;
19921 }
19922
19923
19924 /* EXPORT:
19925 Draw the cursor glyph of window W in glyph row ROW. See the
19926 comment of draw_glyphs for the meaning of HL. */
19927
19928 void
19929 draw_phys_cursor_glyph (w, row, hl)
19930 struct window *w;
19931 struct glyph_row *row;
19932 enum draw_glyphs_face hl;
19933 {
19934 /* If cursor hpos is out of bounds, don't draw garbage. This can
19935 happen in mini-buffer windows when switching between echo area
19936 glyphs and mini-buffer. */
19937 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
19938 {
19939 int on_p = w->phys_cursor_on_p;
19940 int x1;
19941 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
19942 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
19943 hl, 0);
19944 w->phys_cursor_on_p = on_p;
19945
19946 if (hl == DRAW_CURSOR)
19947 w->phys_cursor_width = x1 - w->phys_cursor.x;
19948 /* When we erase the cursor, and ROW is overlapped by other
19949 rows, make sure that these overlapping parts of other rows
19950 are redrawn. */
19951 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
19952 {
19953 if (row > w->current_matrix->rows
19954 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
19955 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
19956
19957 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
19958 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
19959 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
19960 }
19961 }
19962 }
19963
19964
19965 /* EXPORT:
19966 Erase the image of a cursor of window W from the screen. */
19967
19968 void
19969 erase_phys_cursor (w)
19970 struct window *w;
19971 {
19972 struct frame *f = XFRAME (w->frame);
19973 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
19974 int hpos = w->phys_cursor.hpos;
19975 int vpos = w->phys_cursor.vpos;
19976 int mouse_face_here_p = 0;
19977 struct glyph_matrix *active_glyphs = w->current_matrix;
19978 struct glyph_row *cursor_row;
19979 struct glyph *cursor_glyph;
19980 enum draw_glyphs_face hl;
19981
19982 /* No cursor displayed or row invalidated => nothing to do on the
19983 screen. */
19984 if (w->phys_cursor_type == NO_CURSOR)
19985 goto mark_cursor_off;
19986
19987 /* VPOS >= active_glyphs->nrows means that window has been resized.
19988 Don't bother to erase the cursor. */
19989 if (vpos >= active_glyphs->nrows)
19990 goto mark_cursor_off;
19991
19992 /* If row containing cursor is marked invalid, there is nothing we
19993 can do. */
19994 cursor_row = MATRIX_ROW (active_glyphs, vpos);
19995 if (!cursor_row->enabled_p)
19996 goto mark_cursor_off;
19997
19998 /* If row is completely invisible, don't attempt to delete a cursor which
19999 isn't there. This can happen if cursor is at top of a window, and
20000 we switch to a buffer with a header line in that window. */
20001 if (cursor_row->visible_height <= 0)
20002 goto mark_cursor_off;
20003
20004 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
20005 if (cursor_row->cursor_in_fringe_p)
20006 {
20007 cursor_row->cursor_in_fringe_p = 0;
20008 draw_fringe_bitmap (w, cursor_row, 0);
20009 goto mark_cursor_off;
20010 }
20011
20012 /* This can happen when the new row is shorter than the old one.
20013 In this case, either draw_glyphs or clear_end_of_line
20014 should have cleared the cursor. Note that we wouldn't be
20015 able to erase the cursor in this case because we don't have a
20016 cursor glyph at hand. */
20017 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
20018 goto mark_cursor_off;
20019
20020 /* If the cursor is in the mouse face area, redisplay that when
20021 we clear the cursor. */
20022 if (! NILP (dpyinfo->mouse_face_window)
20023 && w == XWINDOW (dpyinfo->mouse_face_window)
20024 && (vpos > dpyinfo->mouse_face_beg_row
20025 || (vpos == dpyinfo->mouse_face_beg_row
20026 && hpos >= dpyinfo->mouse_face_beg_col))
20027 && (vpos < dpyinfo->mouse_face_end_row
20028 || (vpos == dpyinfo->mouse_face_end_row
20029 && hpos < dpyinfo->mouse_face_end_col))
20030 /* Don't redraw the cursor's spot in mouse face if it is at the
20031 end of a line (on a newline). The cursor appears there, but
20032 mouse highlighting does not. */
20033 && cursor_row->used[TEXT_AREA] > hpos)
20034 mouse_face_here_p = 1;
20035
20036 /* Maybe clear the display under the cursor. */
20037 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
20038 {
20039 int x, y;
20040 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
20041
20042 cursor_glyph = get_phys_cursor_glyph (w);
20043 if (cursor_glyph == NULL)
20044 goto mark_cursor_off;
20045
20046 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
20047 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
20048
20049 FRAME_RIF (f)->clear_frame_area (f, x, y,
20050 cursor_glyph->pixel_width, cursor_row->visible_height);
20051 }
20052
20053 /* Erase the cursor by redrawing the character underneath it. */
20054 if (mouse_face_here_p)
20055 hl = DRAW_MOUSE_FACE;
20056 else
20057 hl = DRAW_NORMAL_TEXT;
20058 draw_phys_cursor_glyph (w, cursor_row, hl);
20059
20060 mark_cursor_off:
20061 w->phys_cursor_on_p = 0;
20062 w->phys_cursor_type = NO_CURSOR;
20063 }
20064
20065
20066 /* EXPORT:
20067 Display or clear cursor of window W. If ON is zero, clear the
20068 cursor. If it is non-zero, display the cursor. If ON is nonzero,
20069 where to put the cursor is specified by HPOS, VPOS, X and Y. */
20070
20071 void
20072 display_and_set_cursor (w, on, hpos, vpos, x, y)
20073 struct window *w;
20074 int on, hpos, vpos, x, y;
20075 {
20076 struct frame *f = XFRAME (w->frame);
20077 int new_cursor_type;
20078 int new_cursor_width;
20079 int active_cursor;
20080 struct glyph_row *glyph_row;
20081 struct glyph *glyph;
20082
20083 /* This is pointless on invisible frames, and dangerous on garbaged
20084 windows and frames; in the latter case, the frame or window may
20085 be in the midst of changing its size, and x and y may be off the
20086 window. */
20087 if (! FRAME_VISIBLE_P (f)
20088 || FRAME_GARBAGED_P (f)
20089 || vpos >= w->current_matrix->nrows
20090 || hpos >= w->current_matrix->matrix_w)
20091 return;
20092
20093 /* If cursor is off and we want it off, return quickly. */
20094 if (!on && !w->phys_cursor_on_p)
20095 return;
20096
20097 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
20098 /* If cursor row is not enabled, we don't really know where to
20099 display the cursor. */
20100 if (!glyph_row->enabled_p)
20101 {
20102 w->phys_cursor_on_p = 0;
20103 return;
20104 }
20105
20106 glyph = NULL;
20107 if (!glyph_row->exact_window_width_line_p
20108 || hpos < glyph_row->used[TEXT_AREA])
20109 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
20110
20111 xassert (interrupt_input_blocked);
20112
20113 /* Set new_cursor_type to the cursor we want to be displayed. */
20114 new_cursor_type = get_window_cursor_type (w, glyph,
20115 &new_cursor_width, &active_cursor);
20116
20117 /* If cursor is currently being shown and we don't want it to be or
20118 it is in the wrong place, or the cursor type is not what we want,
20119 erase it. */
20120 if (w->phys_cursor_on_p
20121 && (!on
20122 || w->phys_cursor.x != x
20123 || w->phys_cursor.y != y
20124 || new_cursor_type != w->phys_cursor_type
20125 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
20126 && new_cursor_width != w->phys_cursor_width)))
20127 erase_phys_cursor (w);
20128
20129 /* Don't check phys_cursor_on_p here because that flag is only set
20130 to zero in some cases where we know that the cursor has been
20131 completely erased, to avoid the extra work of erasing the cursor
20132 twice. In other words, phys_cursor_on_p can be 1 and the cursor
20133 still not be visible, or it has only been partly erased. */
20134 if (on)
20135 {
20136 w->phys_cursor_ascent = glyph_row->ascent;
20137 w->phys_cursor_height = glyph_row->height;
20138
20139 /* Set phys_cursor_.* before x_draw_.* is called because some
20140 of them may need the information. */
20141 w->phys_cursor.x = x;
20142 w->phys_cursor.y = glyph_row->y;
20143 w->phys_cursor.hpos = hpos;
20144 w->phys_cursor.vpos = vpos;
20145 }
20146
20147 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
20148 new_cursor_type, new_cursor_width,
20149 on, active_cursor);
20150 }
20151
20152
20153 /* Switch the display of W's cursor on or off, according to the value
20154 of ON. */
20155
20156 static void
20157 update_window_cursor (w, on)
20158 struct window *w;
20159 int on;
20160 {
20161 /* Don't update cursor in windows whose frame is in the process
20162 of being deleted. */
20163 if (w->current_matrix)
20164 {
20165 BLOCK_INPUT;
20166 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20167 w->phys_cursor.x, w->phys_cursor.y);
20168 UNBLOCK_INPUT;
20169 }
20170 }
20171
20172
20173 /* Call update_window_cursor with parameter ON_P on all leaf windows
20174 in the window tree rooted at W. */
20175
20176 static void
20177 update_cursor_in_window_tree (w, on_p)
20178 struct window *w;
20179 int on_p;
20180 {
20181 while (w)
20182 {
20183 if (!NILP (w->hchild))
20184 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20185 else if (!NILP (w->vchild))
20186 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20187 else
20188 update_window_cursor (w, on_p);
20189
20190 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20191 }
20192 }
20193
20194
20195 /* EXPORT:
20196 Display the cursor on window W, or clear it, according to ON_P.
20197 Don't change the cursor's position. */
20198
20199 void
20200 x_update_cursor (f, on_p)
20201 struct frame *f;
20202 int on_p;
20203 {
20204 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20205 }
20206
20207
20208 /* EXPORT:
20209 Clear the cursor of window W to background color, and mark the
20210 cursor as not shown. This is used when the text where the cursor
20211 is is about to be rewritten. */
20212
20213 void
20214 x_clear_cursor (w)
20215 struct window *w;
20216 {
20217 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20218 update_window_cursor (w, 0);
20219 }
20220
20221
20222 /* EXPORT:
20223 Display the active region described by mouse_face_* according to DRAW. */
20224
20225 void
20226 show_mouse_face (dpyinfo, draw)
20227 Display_Info *dpyinfo;
20228 enum draw_glyphs_face draw;
20229 {
20230 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20231 struct frame *f = XFRAME (WINDOW_FRAME (w));
20232
20233 if (/* If window is in the process of being destroyed, don't bother
20234 to do anything. */
20235 w->current_matrix != NULL
20236 /* Don't update mouse highlight if hidden */
20237 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20238 /* Recognize when we are called to operate on rows that don't exist
20239 anymore. This can happen when a window is split. */
20240 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20241 {
20242 int phys_cursor_on_p = w->phys_cursor_on_p;
20243 struct glyph_row *row, *first, *last;
20244
20245 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20246 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20247
20248 for (row = first; row <= last && row->enabled_p; ++row)
20249 {
20250 int start_hpos, end_hpos, start_x;
20251
20252 /* For all but the first row, the highlight starts at column 0. */
20253 if (row == first)
20254 {
20255 start_hpos = dpyinfo->mouse_face_beg_col;
20256 start_x = dpyinfo->mouse_face_beg_x;
20257 }
20258 else
20259 {
20260 start_hpos = 0;
20261 start_x = 0;
20262 }
20263
20264 if (row == last)
20265 end_hpos = dpyinfo->mouse_face_end_col;
20266 else
20267 end_hpos = row->used[TEXT_AREA];
20268
20269 if (end_hpos > start_hpos)
20270 {
20271 draw_glyphs (w, start_x, row, TEXT_AREA,
20272 start_hpos, end_hpos,
20273 draw, 0);
20274
20275 row->mouse_face_p
20276 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20277 }
20278 }
20279
20280 /* When we've written over the cursor, arrange for it to
20281 be displayed again. */
20282 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20283 {
20284 BLOCK_INPUT;
20285 display_and_set_cursor (w, 1,
20286 w->phys_cursor.hpos, w->phys_cursor.vpos,
20287 w->phys_cursor.x, w->phys_cursor.y);
20288 UNBLOCK_INPUT;
20289 }
20290 }
20291
20292 /* Change the mouse cursor. */
20293 if (draw == DRAW_NORMAL_TEXT)
20294 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
20295 else if (draw == DRAW_MOUSE_FACE)
20296 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
20297 else
20298 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
20299 }
20300
20301 /* EXPORT:
20302 Clear out the mouse-highlighted active region.
20303 Redraw it un-highlighted first. Value is non-zero if mouse
20304 face was actually drawn unhighlighted. */
20305
20306 int
20307 clear_mouse_face (dpyinfo)
20308 Display_Info *dpyinfo;
20309 {
20310 int cleared = 0;
20311
20312 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
20313 {
20314 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
20315 cleared = 1;
20316 }
20317
20318 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20319 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20320 dpyinfo->mouse_face_window = Qnil;
20321 dpyinfo->mouse_face_overlay = Qnil;
20322 return cleared;
20323 }
20324
20325
20326 /* EXPORT:
20327 Non-zero if physical cursor of window W is within mouse face. */
20328
20329 int
20330 cursor_in_mouse_face_p (w)
20331 struct window *w;
20332 {
20333 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20334 int in_mouse_face = 0;
20335
20336 if (WINDOWP (dpyinfo->mouse_face_window)
20337 && XWINDOW (dpyinfo->mouse_face_window) == w)
20338 {
20339 int hpos = w->phys_cursor.hpos;
20340 int vpos = w->phys_cursor.vpos;
20341
20342 if (vpos >= dpyinfo->mouse_face_beg_row
20343 && vpos <= dpyinfo->mouse_face_end_row
20344 && (vpos > dpyinfo->mouse_face_beg_row
20345 || hpos >= dpyinfo->mouse_face_beg_col)
20346 && (vpos < dpyinfo->mouse_face_end_row
20347 || hpos < dpyinfo->mouse_face_end_col
20348 || dpyinfo->mouse_face_past_end))
20349 in_mouse_face = 1;
20350 }
20351
20352 return in_mouse_face;
20353 }
20354
20355
20356
20357 \f
20358 /* Find the glyph matrix position of buffer position CHARPOS in window
20359 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
20360 current glyphs must be up to date. If CHARPOS is above window
20361 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
20362 of last line in W. In the row containing CHARPOS, stop before glyphs
20363 having STOP as object. */
20364
20365 #if 1 /* This is a version of fast_find_position that's more correct
20366 in the presence of hscrolling, for example. I didn't install
20367 it right away because the problem fixed is minor, it failed
20368 in 20.x as well, and I think it's too risky to install
20369 so near the release of 21.1. 2001-09-25 gerd. */
20370
20371 static int
20372 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
20373 struct window *w;
20374 int charpos;
20375 int *hpos, *vpos, *x, *y;
20376 Lisp_Object stop;
20377 {
20378 struct glyph_row *row, *first;
20379 struct glyph *glyph, *end;
20380 int past_end = 0;
20381
20382 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20383 row = row_containing_pos (w, charpos, first, NULL, 0);
20384 if (row == NULL)
20385 {
20386 if (charpos < MATRIX_ROW_START_CHARPOS (first))
20387 {
20388 *x = *y = *hpos = *vpos = 0;
20389 return 1;
20390 }
20391 else
20392 {
20393 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
20394 past_end = 1;
20395 }
20396 }
20397
20398 *x = row->x;
20399 *y = row->y;
20400 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20401
20402 glyph = row->glyphs[TEXT_AREA];
20403 end = glyph + row->used[TEXT_AREA];
20404
20405 /* Skip over glyphs not having an object at the start of the row.
20406 These are special glyphs like truncation marks on terminal
20407 frames. */
20408 if (row->displays_text_p)
20409 while (glyph < end
20410 && INTEGERP (glyph->object)
20411 && !EQ (stop, glyph->object)
20412 && glyph->charpos < 0)
20413 {
20414 *x += glyph->pixel_width;
20415 ++glyph;
20416 }
20417
20418 while (glyph < end
20419 && !INTEGERP (glyph->object)
20420 && !EQ (stop, glyph->object)
20421 && (!BUFFERP (glyph->object)
20422 || glyph->charpos < charpos))
20423 {
20424 *x += glyph->pixel_width;
20425 ++glyph;
20426 }
20427
20428 *hpos = glyph - row->glyphs[TEXT_AREA];
20429 return !past_end;
20430 }
20431
20432 #else /* not 1 */
20433
20434 static int
20435 fast_find_position (w, pos, hpos, vpos, x, y, stop)
20436 struct window *w;
20437 int pos;
20438 int *hpos, *vpos, *x, *y;
20439 Lisp_Object stop;
20440 {
20441 int i;
20442 int lastcol;
20443 int maybe_next_line_p = 0;
20444 int line_start_position;
20445 int yb = window_text_bottom_y (w);
20446 struct glyph_row *row, *best_row;
20447 int row_vpos, best_row_vpos;
20448 int current_x;
20449
20450 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20451 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20452
20453 while (row->y < yb)
20454 {
20455 if (row->used[TEXT_AREA])
20456 line_start_position = row->glyphs[TEXT_AREA]->charpos;
20457 else
20458 line_start_position = 0;
20459
20460 if (line_start_position > pos)
20461 break;
20462 /* If the position sought is the end of the buffer,
20463 don't include the blank lines at the bottom of the window. */
20464 else if (line_start_position == pos
20465 && pos == BUF_ZV (XBUFFER (w->buffer)))
20466 {
20467 maybe_next_line_p = 1;
20468 break;
20469 }
20470 else if (line_start_position > 0)
20471 {
20472 best_row = row;
20473 best_row_vpos = row_vpos;
20474 }
20475
20476 if (row->y + row->height >= yb)
20477 break;
20478
20479 ++row;
20480 ++row_vpos;
20481 }
20482
20483 /* Find the right column within BEST_ROW. */
20484 lastcol = 0;
20485 current_x = best_row->x;
20486 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
20487 {
20488 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
20489 int charpos = glyph->charpos;
20490
20491 if (BUFFERP (glyph->object))
20492 {
20493 if (charpos == pos)
20494 {
20495 *hpos = i;
20496 *vpos = best_row_vpos;
20497 *x = current_x;
20498 *y = best_row->y;
20499 return 1;
20500 }
20501 else if (charpos > pos)
20502 break;
20503 }
20504 else if (EQ (glyph->object, stop))
20505 break;
20506
20507 if (charpos > 0)
20508 lastcol = i;
20509 current_x += glyph->pixel_width;
20510 }
20511
20512 /* If we're looking for the end of the buffer,
20513 and we didn't find it in the line we scanned,
20514 use the start of the following line. */
20515 if (maybe_next_line_p)
20516 {
20517 ++best_row;
20518 ++best_row_vpos;
20519 lastcol = 0;
20520 current_x = best_row->x;
20521 }
20522
20523 *vpos = best_row_vpos;
20524 *hpos = lastcol + 1;
20525 *x = current_x;
20526 *y = best_row->y;
20527 return 0;
20528 }
20529
20530 #endif /* not 1 */
20531
20532
20533 /* Find the position of the glyph for position POS in OBJECT in
20534 window W's current matrix, and return in *X, *Y the pixel
20535 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
20536
20537 RIGHT_P non-zero means return the position of the right edge of the
20538 glyph, RIGHT_P zero means return the left edge position.
20539
20540 If no glyph for POS exists in the matrix, return the position of
20541 the glyph with the next smaller position that is in the matrix, if
20542 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
20543 exists in the matrix, return the position of the glyph with the
20544 next larger position in OBJECT.
20545
20546 Value is non-zero if a glyph was found. */
20547
20548 static int
20549 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
20550 struct window *w;
20551 int pos;
20552 Lisp_Object object;
20553 int *hpos, *vpos, *x, *y;
20554 int right_p;
20555 {
20556 int yb = window_text_bottom_y (w);
20557 struct glyph_row *r;
20558 struct glyph *best_glyph = NULL;
20559 struct glyph_row *best_row = NULL;
20560 int best_x = 0;
20561
20562 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20563 r->enabled_p && r->y < yb;
20564 ++r)
20565 {
20566 struct glyph *g = r->glyphs[TEXT_AREA];
20567 struct glyph *e = g + r->used[TEXT_AREA];
20568 int gx;
20569
20570 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
20571 if (EQ (g->object, object))
20572 {
20573 if (g->charpos == pos)
20574 {
20575 best_glyph = g;
20576 best_x = gx;
20577 best_row = r;
20578 goto found;
20579 }
20580 else if (best_glyph == NULL
20581 || ((abs (g->charpos - pos)
20582 < abs (best_glyph->charpos - pos))
20583 && (right_p
20584 ? g->charpos < pos
20585 : g->charpos > pos)))
20586 {
20587 best_glyph = g;
20588 best_x = gx;
20589 best_row = r;
20590 }
20591 }
20592 }
20593
20594 found:
20595
20596 if (best_glyph)
20597 {
20598 *x = best_x;
20599 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
20600
20601 if (right_p)
20602 {
20603 *x += best_glyph->pixel_width;
20604 ++*hpos;
20605 }
20606
20607 *y = best_row->y;
20608 *vpos = best_row - w->current_matrix->rows;
20609 }
20610
20611 return best_glyph != NULL;
20612 }
20613
20614
20615 /* See if position X, Y is within a hot-spot of an image. */
20616
20617 static int
20618 on_hot_spot_p (hot_spot, x, y)
20619 Lisp_Object hot_spot;
20620 int x, y;
20621 {
20622 if (!CONSP (hot_spot))
20623 return 0;
20624
20625 if (EQ (XCAR (hot_spot), Qrect))
20626 {
20627 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
20628 Lisp_Object rect = XCDR (hot_spot);
20629 Lisp_Object tem;
20630 if (!CONSP (rect))
20631 return 0;
20632 if (!CONSP (XCAR (rect)))
20633 return 0;
20634 if (!CONSP (XCDR (rect)))
20635 return 0;
20636 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
20637 return 0;
20638 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
20639 return 0;
20640 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
20641 return 0;
20642 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
20643 return 0;
20644 return 1;
20645 }
20646 else if (EQ (XCAR (hot_spot), Qcircle))
20647 {
20648 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
20649 Lisp_Object circ = XCDR (hot_spot);
20650 Lisp_Object lr, lx0, ly0;
20651 if (CONSP (circ)
20652 && CONSP (XCAR (circ))
20653 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
20654 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
20655 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
20656 {
20657 double r = XFLOATINT (lr);
20658 double dx = XINT (lx0) - x;
20659 double dy = XINT (ly0) - y;
20660 return (dx * dx + dy * dy <= r * r);
20661 }
20662 }
20663 else if (EQ (XCAR (hot_spot), Qpoly))
20664 {
20665 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
20666 if (VECTORP (XCDR (hot_spot)))
20667 {
20668 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
20669 Lisp_Object *poly = v->contents;
20670 int n = v->size;
20671 int i;
20672 int inside = 0;
20673 Lisp_Object lx, ly;
20674 int x0, y0;
20675
20676 /* Need an even number of coordinates, and at least 3 edges. */
20677 if (n < 6 || n & 1)
20678 return 0;
20679
20680 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
20681 If count is odd, we are inside polygon. Pixels on edges
20682 may or may not be included depending on actual geometry of the
20683 polygon. */
20684 if ((lx = poly[n-2], !INTEGERP (lx))
20685 || (ly = poly[n-1], !INTEGERP (lx)))
20686 return 0;
20687 x0 = XINT (lx), y0 = XINT (ly);
20688 for (i = 0; i < n; i += 2)
20689 {
20690 int x1 = x0, y1 = y0;
20691 if ((lx = poly[i], !INTEGERP (lx))
20692 || (ly = poly[i+1], !INTEGERP (ly)))
20693 return 0;
20694 x0 = XINT (lx), y0 = XINT (ly);
20695
20696 /* Does this segment cross the X line? */
20697 if (x0 >= x)
20698 {
20699 if (x1 >= x)
20700 continue;
20701 }
20702 else if (x1 < x)
20703 continue;
20704 if (y > y0 && y > y1)
20705 continue;
20706 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
20707 inside = !inside;
20708 }
20709 return inside;
20710 }
20711 }
20712 return 0;
20713 }
20714
20715 Lisp_Object
20716 find_hot_spot (map, x, y)
20717 Lisp_Object map;
20718 int x, y;
20719 {
20720 while (CONSP (map))
20721 {
20722 if (CONSP (XCAR (map))
20723 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
20724 return XCAR (map);
20725 map = XCDR (map);
20726 }
20727
20728 return Qnil;
20729 }
20730
20731 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
20732 3, 3, 0,
20733 doc: /* Lookup in image map MAP coordinates X and Y.
20734 An image map is an alist where each element has the format (AREA ID PLIST).
20735 An AREA is specified as either a rectangle, a circle, or a polygon:
20736 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
20737 pixel coordinates of the upper left and bottom right corners.
20738 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
20739 and the radius of the circle; r may be a float or integer.
20740 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
20741 vector describes one corner in the polygon.
20742 Returns the alist element for the first matching AREA in MAP. */)
20743 (map, x, y)
20744 Lisp_Object map;
20745 Lisp_Object x, y;
20746 {
20747 if (NILP (map))
20748 return Qnil;
20749
20750 CHECK_NUMBER (x);
20751 CHECK_NUMBER (y);
20752
20753 return find_hot_spot (map, XINT (x), XINT (y));
20754 }
20755
20756
20757 /* Display frame CURSOR, optionally using shape defined by POINTER. */
20758 static void
20759 define_frame_cursor1 (f, cursor, pointer)
20760 struct frame *f;
20761 Cursor cursor;
20762 Lisp_Object pointer;
20763 {
20764 if (!NILP (pointer))
20765 {
20766 if (EQ (pointer, Qarrow))
20767 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20768 else if (EQ (pointer, Qhand))
20769 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
20770 else if (EQ (pointer, Qtext))
20771 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20772 else if (EQ (pointer, intern ("hdrag")))
20773 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
20774 #ifdef HAVE_X_WINDOWS
20775 else if (EQ (pointer, intern ("vdrag")))
20776 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
20777 #endif
20778 else if (EQ (pointer, intern ("hourglass")))
20779 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
20780 else if (EQ (pointer, Qmodeline))
20781 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
20782 else
20783 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20784 }
20785
20786 if (cursor != No_Cursor)
20787 FRAME_RIF (f)->define_frame_cursor (f, cursor);
20788 }
20789
20790 /* Take proper action when mouse has moved to the mode or header line
20791 or marginal area AREA of window W, x-position X and y-position Y.
20792 X is relative to the start of the text display area of W, so the
20793 width of bitmap areas and scroll bars must be subtracted to get a
20794 position relative to the start of the mode line. */
20795
20796 static void
20797 note_mode_line_or_margin_highlight (w, x, y, area)
20798 struct window *w;
20799 int x, y;
20800 enum window_part area;
20801 {
20802 struct frame *f = XFRAME (w->frame);
20803 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20804 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20805 Lisp_Object pointer = Qnil;
20806 int charpos, dx, dy, width, height;
20807 Lisp_Object string, object = Qnil;
20808 Lisp_Object pos, help;
20809
20810 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
20811 string = mode_line_string (w, area, &x, &y, &charpos,
20812 &object, &dx, &dy, &width, &height);
20813 else
20814 {
20815 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
20816 string = marginal_area_string (w, area, &x, &y, &charpos,
20817 &object, &dx, &dy, &width, &height);
20818 }
20819
20820 help = Qnil;
20821
20822 if (IMAGEP (object))
20823 {
20824 Lisp_Object image_map, hotspot;
20825 if ((image_map = Fplist_get (XCDR (object), QCmap),
20826 !NILP (image_map))
20827 && (hotspot = find_hot_spot (image_map, dx, dy),
20828 CONSP (hotspot))
20829 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
20830 {
20831 Lisp_Object area_id, plist;
20832
20833 area_id = XCAR (hotspot);
20834 /* Could check AREA_ID to see if we enter/leave this hot-spot.
20835 If so, we could look for mouse-enter, mouse-leave
20836 properties in PLIST (and do something...). */
20837 if ((plist = XCDR (hotspot), CONSP (plist)))
20838 {
20839 pointer = Fplist_get (plist, Qpointer);
20840 if (NILP (pointer))
20841 pointer = Qhand;
20842 help = Fplist_get (plist, Qhelp_echo);
20843 if (!NILP (help))
20844 {
20845 help_echo_string = help;
20846 /* Is this correct? ++kfs */
20847 XSETWINDOW (help_echo_window, w);
20848 help_echo_object = w->buffer;
20849 help_echo_pos = charpos;
20850 }
20851 }
20852 if (NILP (pointer))
20853 pointer = Fplist_get (XCDR (object), QCpointer);
20854 }
20855 }
20856
20857 if (STRINGP (string))
20858 {
20859 pos = make_number (charpos);
20860 /* If we're on a string with `help-echo' text property, arrange
20861 for the help to be displayed. This is done by setting the
20862 global variable help_echo_string to the help string. */
20863 help = Fget_text_property (pos, Qhelp_echo, string);
20864 if (!NILP (help))
20865 {
20866 help_echo_string = help;
20867 XSETWINDOW (help_echo_window, w);
20868 help_echo_object = string;
20869 help_echo_pos = charpos;
20870 }
20871
20872 if (NILP (pointer))
20873 pointer = Fget_text_property (pos, Qpointer, string);
20874
20875 /* Change the mouse pointer according to what is under X/Y. */
20876 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
20877 {
20878 Lisp_Object map;
20879 map = Fget_text_property (pos, Qlocal_map, string);
20880 if (!KEYMAPP (map))
20881 map = Fget_text_property (pos, Qkeymap, string);
20882 if (!KEYMAPP (map))
20883 cursor = dpyinfo->vertical_scroll_bar_cursor;
20884 }
20885 }
20886
20887 define_frame_cursor1 (f, cursor, pointer);
20888 }
20889
20890
20891 /* EXPORT:
20892 Take proper action when the mouse has moved to position X, Y on
20893 frame F as regards highlighting characters that have mouse-face
20894 properties. Also de-highlighting chars where the mouse was before.
20895 X and Y can be negative or out of range. */
20896
20897 void
20898 note_mouse_highlight (f, x, y)
20899 struct frame *f;
20900 int x, y;
20901 {
20902 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20903 enum window_part part;
20904 Lisp_Object window;
20905 struct window *w;
20906 Cursor cursor = No_Cursor;
20907 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
20908 struct buffer *b;
20909
20910 /* When a menu is active, don't highlight because this looks odd. */
20911 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
20912 if (popup_activated ())
20913 return;
20914 #endif
20915
20916 if (NILP (Vmouse_highlight)
20917 || !f->glyphs_initialized_p)
20918 return;
20919
20920 dpyinfo->mouse_face_mouse_x = x;
20921 dpyinfo->mouse_face_mouse_y = y;
20922 dpyinfo->mouse_face_mouse_frame = f;
20923
20924 if (dpyinfo->mouse_face_defer)
20925 return;
20926
20927 if (gc_in_progress)
20928 {
20929 dpyinfo->mouse_face_deferred_gc = 1;
20930 return;
20931 }
20932
20933 /* Which window is that in? */
20934 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
20935
20936 /* If we were displaying active text in another window, clear that. */
20937 if (! EQ (window, dpyinfo->mouse_face_window))
20938 clear_mouse_face (dpyinfo);
20939
20940 /* Not on a window -> return. */
20941 if (!WINDOWP (window))
20942 return;
20943
20944 /* Reset help_echo_string. It will get recomputed below. */
20945 help_echo_string = Qnil;
20946
20947 /* Convert to window-relative pixel coordinates. */
20948 w = XWINDOW (window);
20949 frame_to_window_pixel_xy (w, &x, &y);
20950
20951 /* Handle tool-bar window differently since it doesn't display a
20952 buffer. */
20953 if (EQ (window, f->tool_bar_window))
20954 {
20955 note_tool_bar_highlight (f, x, y);
20956 return;
20957 }
20958
20959 /* Mouse is on the mode, header line or margin? */
20960 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
20961 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
20962 {
20963 note_mode_line_or_margin_highlight (w, x, y, part);
20964 return;
20965 }
20966
20967 if (part == ON_VERTICAL_BORDER)
20968 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
20969 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
20970 || part == ON_SCROLL_BAR)
20971 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20972 else
20973 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20974
20975 /* Are we in a window whose display is up to date?
20976 And verify the buffer's text has not changed. */
20977 b = XBUFFER (w->buffer);
20978 if (part == ON_TEXT
20979 && EQ (w->window_end_valid, w->buffer)
20980 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
20981 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
20982 {
20983 int hpos, vpos, pos, i, dx, dy, area;
20984 struct glyph *glyph;
20985 Lisp_Object object;
20986 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
20987 Lisp_Object *overlay_vec = NULL;
20988 int noverlays;
20989 struct buffer *obuf;
20990 int obegv, ozv, same_region;
20991
20992 /* Find the glyph under X/Y. */
20993 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
20994
20995 /* Look for :pointer property on image. */
20996 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
20997 {
20998 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
20999 if (img != NULL && IMAGEP (img->spec))
21000 {
21001 Lisp_Object image_map, hotspot;
21002 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
21003 !NILP (image_map))
21004 && (hotspot = find_hot_spot (image_map,
21005 glyph->slice.x + dx,
21006 glyph->slice.y + dy),
21007 CONSP (hotspot))
21008 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21009 {
21010 Lisp_Object area_id, plist;
21011
21012 area_id = XCAR (hotspot);
21013 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21014 If so, we could look for mouse-enter, mouse-leave
21015 properties in PLIST (and do something...). */
21016 if ((plist = XCDR (hotspot), CONSP (plist)))
21017 {
21018 pointer = Fplist_get (plist, Qpointer);
21019 if (NILP (pointer))
21020 pointer = Qhand;
21021 help_echo_string = Fplist_get (plist, Qhelp_echo);
21022 if (!NILP (help_echo_string))
21023 {
21024 help_echo_window = window;
21025 help_echo_object = glyph->object;
21026 help_echo_pos = glyph->charpos;
21027 }
21028 }
21029 }
21030 if (NILP (pointer))
21031 pointer = Fplist_get (XCDR (img->spec), QCpointer);
21032 }
21033 }
21034
21035 /* Clear mouse face if X/Y not over text. */
21036 if (glyph == NULL
21037 || area != TEXT_AREA
21038 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
21039 {
21040 if (clear_mouse_face (dpyinfo))
21041 cursor = No_Cursor;
21042 if (NILP (pointer))
21043 {
21044 if (area != TEXT_AREA)
21045 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21046 else
21047 pointer = Vvoid_text_area_pointer;
21048 }
21049 goto set_cursor;
21050 }
21051
21052 pos = glyph->charpos;
21053 object = glyph->object;
21054 if (!STRINGP (object) && !BUFFERP (object))
21055 goto set_cursor;
21056
21057 /* If we get an out-of-range value, return now; avoid an error. */
21058 if (BUFFERP (object) && pos > BUF_Z (b))
21059 goto set_cursor;
21060
21061 /* Make the window's buffer temporarily current for
21062 overlays_at and compute_char_face. */
21063 obuf = current_buffer;
21064 current_buffer = b;
21065 obegv = BEGV;
21066 ozv = ZV;
21067 BEGV = BEG;
21068 ZV = Z;
21069
21070 /* Is this char mouse-active or does it have help-echo? */
21071 position = make_number (pos);
21072
21073 if (BUFFERP (object))
21074 {
21075 /* Put all the overlays we want in a vector in overlay_vec. */
21076 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
21077 /* Sort overlays into increasing priority order. */
21078 noverlays = sort_overlays (overlay_vec, noverlays, w);
21079 }
21080 else
21081 noverlays = 0;
21082
21083 same_region = (EQ (window, dpyinfo->mouse_face_window)
21084 && vpos >= dpyinfo->mouse_face_beg_row
21085 && vpos <= dpyinfo->mouse_face_end_row
21086 && (vpos > dpyinfo->mouse_face_beg_row
21087 || hpos >= dpyinfo->mouse_face_beg_col)
21088 && (vpos < dpyinfo->mouse_face_end_row
21089 || hpos < dpyinfo->mouse_face_end_col
21090 || dpyinfo->mouse_face_past_end));
21091
21092 if (same_region)
21093 cursor = No_Cursor;
21094
21095 /* Check mouse-face highlighting. */
21096 if (! same_region
21097 /* If there exists an overlay with mouse-face overlapping
21098 the one we are currently highlighting, we have to
21099 check if we enter the overlapping overlay, and then
21100 highlight only that. */
21101 || (OVERLAYP (dpyinfo->mouse_face_overlay)
21102 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
21103 {
21104 /* Find the highest priority overlay that has a mouse-face
21105 property. */
21106 overlay = Qnil;
21107 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
21108 {
21109 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
21110 if (!NILP (mouse_face))
21111 overlay = overlay_vec[i];
21112 }
21113
21114 /* If we're actually highlighting the same overlay as
21115 before, there's no need to do that again. */
21116 if (!NILP (overlay)
21117 && EQ (overlay, dpyinfo->mouse_face_overlay))
21118 goto check_help_echo;
21119
21120 dpyinfo->mouse_face_overlay = overlay;
21121
21122 /* Clear the display of the old active region, if any. */
21123 if (clear_mouse_face (dpyinfo))
21124 cursor = No_Cursor;
21125
21126 /* If no overlay applies, get a text property. */
21127 if (NILP (overlay))
21128 mouse_face = Fget_text_property (position, Qmouse_face, object);
21129
21130 /* Handle the overlay case. */
21131 if (!NILP (overlay))
21132 {
21133 /* Find the range of text around this char that
21134 should be active. */
21135 Lisp_Object before, after;
21136 int ignore;
21137
21138 before = Foverlay_start (overlay);
21139 after = Foverlay_end (overlay);
21140 /* Record this as the current active region. */
21141 fast_find_position (w, XFASTINT (before),
21142 &dpyinfo->mouse_face_beg_col,
21143 &dpyinfo->mouse_face_beg_row,
21144 &dpyinfo->mouse_face_beg_x,
21145 &dpyinfo->mouse_face_beg_y, Qnil);
21146
21147 dpyinfo->mouse_face_past_end
21148 = !fast_find_position (w, XFASTINT (after),
21149 &dpyinfo->mouse_face_end_col,
21150 &dpyinfo->mouse_face_end_row,
21151 &dpyinfo->mouse_face_end_x,
21152 &dpyinfo->mouse_face_end_y, Qnil);
21153 dpyinfo->mouse_face_window = window;
21154
21155 dpyinfo->mouse_face_face_id
21156 = face_at_buffer_position (w, pos, 0, 0,
21157 &ignore, pos + 1,
21158 !dpyinfo->mouse_face_hidden);
21159
21160 /* Display it as active. */
21161 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21162 cursor = No_Cursor;
21163 }
21164 /* Handle the text property case. */
21165 else if (!NILP (mouse_face) && BUFFERP (object))
21166 {
21167 /* Find the range of text around this char that
21168 should be active. */
21169 Lisp_Object before, after, beginning, end;
21170 int ignore;
21171
21172 beginning = Fmarker_position (w->start);
21173 end = make_number (BUF_Z (XBUFFER (object))
21174 - XFASTINT (w->window_end_pos));
21175 before
21176 = Fprevious_single_property_change (make_number (pos + 1),
21177 Qmouse_face,
21178 object, beginning);
21179 after
21180 = Fnext_single_property_change (position, Qmouse_face,
21181 object, end);
21182
21183 /* Record this as the current active region. */
21184 fast_find_position (w, XFASTINT (before),
21185 &dpyinfo->mouse_face_beg_col,
21186 &dpyinfo->mouse_face_beg_row,
21187 &dpyinfo->mouse_face_beg_x,
21188 &dpyinfo->mouse_face_beg_y, Qnil);
21189 dpyinfo->mouse_face_past_end
21190 = !fast_find_position (w, XFASTINT (after),
21191 &dpyinfo->mouse_face_end_col,
21192 &dpyinfo->mouse_face_end_row,
21193 &dpyinfo->mouse_face_end_x,
21194 &dpyinfo->mouse_face_end_y, Qnil);
21195 dpyinfo->mouse_face_window = window;
21196
21197 if (BUFFERP (object))
21198 dpyinfo->mouse_face_face_id
21199 = face_at_buffer_position (w, pos, 0, 0,
21200 &ignore, pos + 1,
21201 !dpyinfo->mouse_face_hidden);
21202
21203 /* Display it as active. */
21204 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21205 cursor = No_Cursor;
21206 }
21207 else if (!NILP (mouse_face) && STRINGP (object))
21208 {
21209 Lisp_Object b, e;
21210 int ignore;
21211
21212 b = Fprevious_single_property_change (make_number (pos + 1),
21213 Qmouse_face,
21214 object, Qnil);
21215 e = Fnext_single_property_change (position, Qmouse_face,
21216 object, Qnil);
21217 if (NILP (b))
21218 b = make_number (0);
21219 if (NILP (e))
21220 e = make_number (SCHARS (object) - 1);
21221 fast_find_string_pos (w, XINT (b), object,
21222 &dpyinfo->mouse_face_beg_col,
21223 &dpyinfo->mouse_face_beg_row,
21224 &dpyinfo->mouse_face_beg_x,
21225 &dpyinfo->mouse_face_beg_y, 0);
21226 fast_find_string_pos (w, XINT (e), object,
21227 &dpyinfo->mouse_face_end_col,
21228 &dpyinfo->mouse_face_end_row,
21229 &dpyinfo->mouse_face_end_x,
21230 &dpyinfo->mouse_face_end_y, 1);
21231 dpyinfo->mouse_face_past_end = 0;
21232 dpyinfo->mouse_face_window = window;
21233 dpyinfo->mouse_face_face_id
21234 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
21235 glyph->face_id, 1);
21236 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21237 cursor = No_Cursor;
21238 }
21239 else if (STRINGP (object) && NILP (mouse_face))
21240 {
21241 /* A string which doesn't have mouse-face, but
21242 the text ``under'' it might have. */
21243 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
21244 int start = MATRIX_ROW_START_CHARPOS (r);
21245
21246 pos = string_buffer_position (w, object, start);
21247 if (pos > 0)
21248 mouse_face = get_char_property_and_overlay (make_number (pos),
21249 Qmouse_face,
21250 w->buffer,
21251 &overlay);
21252 if (!NILP (mouse_face) && !NILP (overlay))
21253 {
21254 Lisp_Object before = Foverlay_start (overlay);
21255 Lisp_Object after = Foverlay_end (overlay);
21256 int ignore;
21257
21258 /* Note that we might not be able to find position
21259 BEFORE in the glyph matrix if the overlay is
21260 entirely covered by a `display' property. In
21261 this case, we overshoot. So let's stop in
21262 the glyph matrix before glyphs for OBJECT. */
21263 fast_find_position (w, XFASTINT (before),
21264 &dpyinfo->mouse_face_beg_col,
21265 &dpyinfo->mouse_face_beg_row,
21266 &dpyinfo->mouse_face_beg_x,
21267 &dpyinfo->mouse_face_beg_y,
21268 object);
21269
21270 dpyinfo->mouse_face_past_end
21271 = !fast_find_position (w, XFASTINT (after),
21272 &dpyinfo->mouse_face_end_col,
21273 &dpyinfo->mouse_face_end_row,
21274 &dpyinfo->mouse_face_end_x,
21275 &dpyinfo->mouse_face_end_y,
21276 Qnil);
21277 dpyinfo->mouse_face_window = window;
21278 dpyinfo->mouse_face_face_id
21279 = face_at_buffer_position (w, pos, 0, 0,
21280 &ignore, pos + 1,
21281 !dpyinfo->mouse_face_hidden);
21282
21283 /* Display it as active. */
21284 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21285 cursor = No_Cursor;
21286 }
21287 }
21288 }
21289
21290 check_help_echo:
21291
21292 /* Look for a `help-echo' property. */
21293 if (NILP (help_echo_string)) {
21294 Lisp_Object help, overlay;
21295
21296 /* Check overlays first. */
21297 help = overlay = Qnil;
21298 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
21299 {
21300 overlay = overlay_vec[i];
21301 help = Foverlay_get (overlay, Qhelp_echo);
21302 }
21303
21304 if (!NILP (help))
21305 {
21306 help_echo_string = help;
21307 help_echo_window = window;
21308 help_echo_object = overlay;
21309 help_echo_pos = pos;
21310 }
21311 else
21312 {
21313 Lisp_Object object = glyph->object;
21314 int charpos = glyph->charpos;
21315
21316 /* Try text properties. */
21317 if (STRINGP (object)
21318 && charpos >= 0
21319 && charpos < SCHARS (object))
21320 {
21321 help = Fget_text_property (make_number (charpos),
21322 Qhelp_echo, object);
21323 if (NILP (help))
21324 {
21325 /* If the string itself doesn't specify a help-echo,
21326 see if the buffer text ``under'' it does. */
21327 struct glyph_row *r
21328 = MATRIX_ROW (w->current_matrix, vpos);
21329 int start = MATRIX_ROW_START_CHARPOS (r);
21330 int pos = string_buffer_position (w, object, start);
21331 if (pos > 0)
21332 {
21333 help = Fget_char_property (make_number (pos),
21334 Qhelp_echo, w->buffer);
21335 if (!NILP (help))
21336 {
21337 charpos = pos;
21338 object = w->buffer;
21339 }
21340 }
21341 }
21342 }
21343 else if (BUFFERP (object)
21344 && charpos >= BEGV
21345 && charpos < ZV)
21346 help = Fget_text_property (make_number (charpos), Qhelp_echo,
21347 object);
21348
21349 if (!NILP (help))
21350 {
21351 help_echo_string = help;
21352 help_echo_window = window;
21353 help_echo_object = object;
21354 help_echo_pos = charpos;
21355 }
21356 }
21357 }
21358
21359 /* Look for a `pointer' property. */
21360 if (NILP (pointer))
21361 {
21362 /* Check overlays first. */
21363 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
21364 pointer = Foverlay_get (overlay_vec[i], Qpointer);
21365
21366 if (NILP (pointer))
21367 {
21368 Lisp_Object object = glyph->object;
21369 int charpos = glyph->charpos;
21370
21371 /* Try text properties. */
21372 if (STRINGP (object)
21373 && charpos >= 0
21374 && charpos < SCHARS (object))
21375 {
21376 pointer = Fget_text_property (make_number (charpos),
21377 Qpointer, object);
21378 if (NILP (pointer))
21379 {
21380 /* If the string itself doesn't specify a pointer,
21381 see if the buffer text ``under'' it does. */
21382 struct glyph_row *r
21383 = MATRIX_ROW (w->current_matrix, vpos);
21384 int start = MATRIX_ROW_START_CHARPOS (r);
21385 int pos = string_buffer_position (w, object, start);
21386 if (pos > 0)
21387 pointer = Fget_char_property (make_number (pos),
21388 Qpointer, w->buffer);
21389 }
21390 }
21391 else if (BUFFERP (object)
21392 && charpos >= BEGV
21393 && charpos < ZV)
21394 pointer = Fget_text_property (make_number (charpos),
21395 Qpointer, object);
21396 }
21397 }
21398
21399 BEGV = obegv;
21400 ZV = ozv;
21401 current_buffer = obuf;
21402 }
21403
21404 set_cursor:
21405
21406 define_frame_cursor1 (f, cursor, pointer);
21407 }
21408
21409
21410 /* EXPORT for RIF:
21411 Clear any mouse-face on window W. This function is part of the
21412 redisplay interface, and is called from try_window_id and similar
21413 functions to ensure the mouse-highlight is off. */
21414
21415 void
21416 x_clear_window_mouse_face (w)
21417 struct window *w;
21418 {
21419 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21420 Lisp_Object window;
21421
21422 BLOCK_INPUT;
21423 XSETWINDOW (window, w);
21424 if (EQ (window, dpyinfo->mouse_face_window))
21425 clear_mouse_face (dpyinfo);
21426 UNBLOCK_INPUT;
21427 }
21428
21429
21430 /* EXPORT:
21431 Just discard the mouse face information for frame F, if any.
21432 This is used when the size of F is changed. */
21433
21434 void
21435 cancel_mouse_face (f)
21436 struct frame *f;
21437 {
21438 Lisp_Object window;
21439 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21440
21441 window = dpyinfo->mouse_face_window;
21442 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
21443 {
21444 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21445 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21446 dpyinfo->mouse_face_window = Qnil;
21447 }
21448 }
21449
21450
21451 #endif /* HAVE_WINDOW_SYSTEM */
21452
21453 \f
21454 /***********************************************************************
21455 Exposure Events
21456 ***********************************************************************/
21457
21458 #ifdef HAVE_WINDOW_SYSTEM
21459
21460 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
21461 which intersects rectangle R. R is in window-relative coordinates. */
21462
21463 static void
21464 expose_area (w, row, r, area)
21465 struct window *w;
21466 struct glyph_row *row;
21467 XRectangle *r;
21468 enum glyph_row_area area;
21469 {
21470 struct glyph *first = row->glyphs[area];
21471 struct glyph *end = row->glyphs[area] + row->used[area];
21472 struct glyph *last;
21473 int first_x, start_x, x;
21474
21475 if (area == TEXT_AREA && row->fill_line_p)
21476 /* If row extends face to end of line write the whole line. */
21477 draw_glyphs (w, 0, row, area,
21478 0, row->used[area],
21479 DRAW_NORMAL_TEXT, 0);
21480 else
21481 {
21482 /* Set START_X to the window-relative start position for drawing glyphs of
21483 AREA. The first glyph of the text area can be partially visible.
21484 The first glyphs of other areas cannot. */
21485 start_x = window_box_left_offset (w, area);
21486 x = start_x;
21487 if (area == TEXT_AREA)
21488 x += row->x;
21489
21490 /* Find the first glyph that must be redrawn. */
21491 while (first < end
21492 && x + first->pixel_width < r->x)
21493 {
21494 x += first->pixel_width;
21495 ++first;
21496 }
21497
21498 /* Find the last one. */
21499 last = first;
21500 first_x = x;
21501 while (last < end
21502 && x < r->x + r->width)
21503 {
21504 x += last->pixel_width;
21505 ++last;
21506 }
21507
21508 /* Repaint. */
21509 if (last > first)
21510 draw_glyphs (w, first_x - start_x, row, area,
21511 first - row->glyphs[area], last - row->glyphs[area],
21512 DRAW_NORMAL_TEXT, 0);
21513 }
21514 }
21515
21516
21517 /* Redraw the parts of the glyph row ROW on window W intersecting
21518 rectangle R. R is in window-relative coordinates. Value is
21519 non-zero if mouse-face was overwritten. */
21520
21521 static int
21522 expose_line (w, row, r)
21523 struct window *w;
21524 struct glyph_row *row;
21525 XRectangle *r;
21526 {
21527 xassert (row->enabled_p);
21528
21529 if (row->mode_line_p || w->pseudo_window_p)
21530 draw_glyphs (w, 0, row, TEXT_AREA,
21531 0, row->used[TEXT_AREA],
21532 DRAW_NORMAL_TEXT, 0);
21533 else
21534 {
21535 if (row->used[LEFT_MARGIN_AREA])
21536 expose_area (w, row, r, LEFT_MARGIN_AREA);
21537 if (row->used[TEXT_AREA])
21538 expose_area (w, row, r, TEXT_AREA);
21539 if (row->used[RIGHT_MARGIN_AREA])
21540 expose_area (w, row, r, RIGHT_MARGIN_AREA);
21541 draw_row_fringe_bitmaps (w, row);
21542 }
21543
21544 return row->mouse_face_p;
21545 }
21546
21547
21548 /* Redraw those parts of glyphs rows during expose event handling that
21549 overlap other rows. Redrawing of an exposed line writes over parts
21550 of lines overlapping that exposed line; this function fixes that.
21551
21552 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
21553 row in W's current matrix that is exposed and overlaps other rows.
21554 LAST_OVERLAPPING_ROW is the last such row. */
21555
21556 static void
21557 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
21558 struct window *w;
21559 struct glyph_row *first_overlapping_row;
21560 struct glyph_row *last_overlapping_row;
21561 {
21562 struct glyph_row *row;
21563
21564 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
21565 if (row->overlapping_p)
21566 {
21567 xassert (row->enabled_p && !row->mode_line_p);
21568
21569 if (row->used[LEFT_MARGIN_AREA])
21570 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
21571
21572 if (row->used[TEXT_AREA])
21573 x_fix_overlapping_area (w, row, TEXT_AREA);
21574
21575 if (row->used[RIGHT_MARGIN_AREA])
21576 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
21577 }
21578 }
21579
21580
21581 /* Return non-zero if W's cursor intersects rectangle R. */
21582
21583 static int
21584 phys_cursor_in_rect_p (w, r)
21585 struct window *w;
21586 XRectangle *r;
21587 {
21588 XRectangle cr, result;
21589 struct glyph *cursor_glyph;
21590
21591 cursor_glyph = get_phys_cursor_glyph (w);
21592 if (cursor_glyph)
21593 {
21594 /* r is relative to W's box, but w->phys_cursor.x is relative
21595 to left edge of W's TEXT area. Adjust it. */
21596 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
21597 cr.y = w->phys_cursor.y;
21598 cr.width = cursor_glyph->pixel_width;
21599 cr.height = w->phys_cursor_height;
21600 /* ++KFS: W32 version used W32-specific IntersectRect here, but
21601 I assume the effect is the same -- and this is portable. */
21602 return x_intersect_rectangles (&cr, r, &result);
21603 }
21604 /* If we don't understand the format, pretend we're not in the hot-spot. */
21605 return 0;
21606 }
21607
21608
21609 /* EXPORT:
21610 Draw a vertical window border to the right of window W if W doesn't
21611 have vertical scroll bars. */
21612
21613 void
21614 x_draw_vertical_border (w)
21615 struct window *w;
21616 {
21617 struct frame *f = XFRAME (WINDOW_FRAME (w));
21618
21619 /* We could do better, if we knew what type of scroll-bar the adjacent
21620 windows (on either side) have... But we don't :-(
21621 However, I think this works ok. ++KFS 2003-04-25 */
21622
21623 /* Redraw borders between horizontally adjacent windows. Don't
21624 do it for frames with vertical scroll bars because either the
21625 right scroll bar of a window, or the left scroll bar of its
21626 neighbor will suffice as a border. */
21627 if (!WINDOW_RIGHTMOST_P (w)
21628 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
21629 {
21630 int x0, x1, y0, y1;
21631
21632 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21633 y1 -= 1;
21634
21635 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
21636 }
21637 else if (!WINDOW_LEFTMOST_P (w)
21638 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
21639 {
21640 int x0, x1, y0, y1;
21641
21642 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21643 y1 -= 1;
21644
21645 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
21646 }
21647 }
21648
21649
21650 /* Redraw the part of window W intersection rectangle FR. Pixel
21651 coordinates in FR are frame-relative. Call this function with
21652 input blocked. Value is non-zero if the exposure overwrites
21653 mouse-face. */
21654
21655 static int
21656 expose_window (w, fr)
21657 struct window *w;
21658 XRectangle *fr;
21659 {
21660 struct frame *f = XFRAME (w->frame);
21661 XRectangle wr, r;
21662 int mouse_face_overwritten_p = 0;
21663
21664 /* If window is not yet fully initialized, do nothing. This can
21665 happen when toolkit scroll bars are used and a window is split.
21666 Reconfiguring the scroll bar will generate an expose for a newly
21667 created window. */
21668 if (w->current_matrix == NULL)
21669 return 0;
21670
21671 /* When we're currently updating the window, display and current
21672 matrix usually don't agree. Arrange for a thorough display
21673 later. */
21674 if (w == updated_window)
21675 {
21676 SET_FRAME_GARBAGED (f);
21677 return 0;
21678 }
21679
21680 /* Frame-relative pixel rectangle of W. */
21681 wr.x = WINDOW_LEFT_EDGE_X (w);
21682 wr.y = WINDOW_TOP_EDGE_Y (w);
21683 wr.width = WINDOW_TOTAL_WIDTH (w);
21684 wr.height = WINDOW_TOTAL_HEIGHT (w);
21685
21686 if (x_intersect_rectangles (fr, &wr, &r))
21687 {
21688 int yb = window_text_bottom_y (w);
21689 struct glyph_row *row;
21690 int cursor_cleared_p;
21691 struct glyph_row *first_overlapping_row, *last_overlapping_row;
21692
21693 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
21694 r.x, r.y, r.width, r.height));
21695
21696 /* Convert to window coordinates. */
21697 r.x -= WINDOW_LEFT_EDGE_X (w);
21698 r.y -= WINDOW_TOP_EDGE_Y (w);
21699
21700 /* Turn off the cursor. */
21701 if (!w->pseudo_window_p
21702 && phys_cursor_in_rect_p (w, &r))
21703 {
21704 x_clear_cursor (w);
21705 cursor_cleared_p = 1;
21706 }
21707 else
21708 cursor_cleared_p = 0;
21709
21710 /* Update lines intersecting rectangle R. */
21711 first_overlapping_row = last_overlapping_row = NULL;
21712 for (row = w->current_matrix->rows;
21713 row->enabled_p;
21714 ++row)
21715 {
21716 int y0 = row->y;
21717 int y1 = MATRIX_ROW_BOTTOM_Y (row);
21718
21719 if ((y0 >= r.y && y0 < r.y + r.height)
21720 || (y1 > r.y && y1 < r.y + r.height)
21721 || (r.y >= y0 && r.y < y1)
21722 || (r.y + r.height > y0 && r.y + r.height < y1))
21723 {
21724 if (row->overlapping_p)
21725 {
21726 if (first_overlapping_row == NULL)
21727 first_overlapping_row = row;
21728 last_overlapping_row = row;
21729 }
21730
21731 if (expose_line (w, row, &r))
21732 mouse_face_overwritten_p = 1;
21733 }
21734
21735 if (y1 >= yb)
21736 break;
21737 }
21738
21739 /* Display the mode line if there is one. */
21740 if (WINDOW_WANTS_MODELINE_P (w)
21741 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
21742 row->enabled_p)
21743 && row->y < r.y + r.height)
21744 {
21745 if (expose_line (w, row, &r))
21746 mouse_face_overwritten_p = 1;
21747 }
21748
21749 if (!w->pseudo_window_p)
21750 {
21751 /* Fix the display of overlapping rows. */
21752 if (first_overlapping_row)
21753 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
21754
21755 /* Draw border between windows. */
21756 x_draw_vertical_border (w);
21757
21758 /* Turn the cursor on again. */
21759 if (cursor_cleared_p)
21760 update_window_cursor (w, 1);
21761 }
21762 }
21763
21764 #ifdef HAVE_CARBON
21765 /* Display scroll bar for this window. */
21766 if (!NILP (w->vertical_scroll_bar))
21767 {
21768 /* ++KFS:
21769 If this doesn't work here (maybe some header files are missing),
21770 make a function in macterm.c and call it to do the job! */
21771 ControlHandle ch
21772 = SCROLL_BAR_CONTROL_HANDLE (XSCROLL_BAR (w->vertical_scroll_bar));
21773
21774 Draw1Control (ch);
21775 }
21776 #endif
21777
21778 return mouse_face_overwritten_p;
21779 }
21780
21781
21782
21783 /* Redraw (parts) of all windows in the window tree rooted at W that
21784 intersect R. R contains frame pixel coordinates. Value is
21785 non-zero if the exposure overwrites mouse-face. */
21786
21787 static int
21788 expose_window_tree (w, r)
21789 struct window *w;
21790 XRectangle *r;
21791 {
21792 struct frame *f = XFRAME (w->frame);
21793 int mouse_face_overwritten_p = 0;
21794
21795 while (w && !FRAME_GARBAGED_P (f))
21796 {
21797 if (!NILP (w->hchild))
21798 mouse_face_overwritten_p
21799 |= expose_window_tree (XWINDOW (w->hchild), r);
21800 else if (!NILP (w->vchild))
21801 mouse_face_overwritten_p
21802 |= expose_window_tree (XWINDOW (w->vchild), r);
21803 else
21804 mouse_face_overwritten_p |= expose_window (w, r);
21805
21806 w = NILP (w->next) ? NULL : XWINDOW (w->next);
21807 }
21808
21809 return mouse_face_overwritten_p;
21810 }
21811
21812
21813 /* EXPORT:
21814 Redisplay an exposed area of frame F. X and Y are the upper-left
21815 corner of the exposed rectangle. W and H are width and height of
21816 the exposed area. All are pixel values. W or H zero means redraw
21817 the entire frame. */
21818
21819 void
21820 expose_frame (f, x, y, w, h)
21821 struct frame *f;
21822 int x, y, w, h;
21823 {
21824 XRectangle r;
21825 int mouse_face_overwritten_p = 0;
21826
21827 TRACE ((stderr, "expose_frame "));
21828
21829 /* No need to redraw if frame will be redrawn soon. */
21830 if (FRAME_GARBAGED_P (f))
21831 {
21832 TRACE ((stderr, " garbaged\n"));
21833 return;
21834 }
21835
21836 #ifdef HAVE_CARBON
21837 /* MAC_TODO: this is a kludge, but if scroll bars are not activated
21838 or deactivated here, for unknown reasons, activated scroll bars
21839 are shown in deactivated frames in some instances. */
21840 if (f == FRAME_MAC_DISPLAY_INFO (f)->x_focus_frame)
21841 activate_scroll_bars (f);
21842 else
21843 deactivate_scroll_bars (f);
21844 #endif
21845
21846 /* If basic faces haven't been realized yet, there is no point in
21847 trying to redraw anything. This can happen when we get an expose
21848 event while Emacs is starting, e.g. by moving another window. */
21849 if (FRAME_FACE_CACHE (f) == NULL
21850 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
21851 {
21852 TRACE ((stderr, " no faces\n"));
21853 return;
21854 }
21855
21856 if (w == 0 || h == 0)
21857 {
21858 r.x = r.y = 0;
21859 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
21860 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
21861 }
21862 else
21863 {
21864 r.x = x;
21865 r.y = y;
21866 r.width = w;
21867 r.height = h;
21868 }
21869
21870 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
21871 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
21872
21873 if (WINDOWP (f->tool_bar_window))
21874 mouse_face_overwritten_p
21875 |= expose_window (XWINDOW (f->tool_bar_window), &r);
21876
21877 #ifdef HAVE_X_WINDOWS
21878 #ifndef MSDOS
21879 #ifndef USE_X_TOOLKIT
21880 if (WINDOWP (f->menu_bar_window))
21881 mouse_face_overwritten_p
21882 |= expose_window (XWINDOW (f->menu_bar_window), &r);
21883 #endif /* not USE_X_TOOLKIT */
21884 #endif
21885 #endif
21886
21887 /* Some window managers support a focus-follows-mouse style with
21888 delayed raising of frames. Imagine a partially obscured frame,
21889 and moving the mouse into partially obscured mouse-face on that
21890 frame. The visible part of the mouse-face will be highlighted,
21891 then the WM raises the obscured frame. With at least one WM, KDE
21892 2.1, Emacs is not getting any event for the raising of the frame
21893 (even tried with SubstructureRedirectMask), only Expose events.
21894 These expose events will draw text normally, i.e. not
21895 highlighted. Which means we must redo the highlight here.
21896 Subsume it under ``we love X''. --gerd 2001-08-15 */
21897 /* Included in Windows version because Windows most likely does not
21898 do the right thing if any third party tool offers
21899 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
21900 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
21901 {
21902 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21903 if (f == dpyinfo->mouse_face_mouse_frame)
21904 {
21905 int x = dpyinfo->mouse_face_mouse_x;
21906 int y = dpyinfo->mouse_face_mouse_y;
21907 clear_mouse_face (dpyinfo);
21908 note_mouse_highlight (f, x, y);
21909 }
21910 }
21911 }
21912
21913
21914 /* EXPORT:
21915 Determine the intersection of two rectangles R1 and R2. Return
21916 the intersection in *RESULT. Value is non-zero if RESULT is not
21917 empty. */
21918
21919 int
21920 x_intersect_rectangles (r1, r2, result)
21921 XRectangle *r1, *r2, *result;
21922 {
21923 XRectangle *left, *right;
21924 XRectangle *upper, *lower;
21925 int intersection_p = 0;
21926
21927 /* Rearrange so that R1 is the left-most rectangle. */
21928 if (r1->x < r2->x)
21929 left = r1, right = r2;
21930 else
21931 left = r2, right = r1;
21932
21933 /* X0 of the intersection is right.x0, if this is inside R1,
21934 otherwise there is no intersection. */
21935 if (right->x <= left->x + left->width)
21936 {
21937 result->x = right->x;
21938
21939 /* The right end of the intersection is the minimum of the
21940 the right ends of left and right. */
21941 result->width = (min (left->x + left->width, right->x + right->width)
21942 - result->x);
21943
21944 /* Same game for Y. */
21945 if (r1->y < r2->y)
21946 upper = r1, lower = r2;
21947 else
21948 upper = r2, lower = r1;
21949
21950 /* The upper end of the intersection is lower.y0, if this is inside
21951 of upper. Otherwise, there is no intersection. */
21952 if (lower->y <= upper->y + upper->height)
21953 {
21954 result->y = lower->y;
21955
21956 /* The lower end of the intersection is the minimum of the lower
21957 ends of upper and lower. */
21958 result->height = (min (lower->y + lower->height,
21959 upper->y + upper->height)
21960 - result->y);
21961 intersection_p = 1;
21962 }
21963 }
21964
21965 return intersection_p;
21966 }
21967
21968 #endif /* HAVE_WINDOW_SYSTEM */
21969
21970 \f
21971 /***********************************************************************
21972 Initialization
21973 ***********************************************************************/
21974
21975 void
21976 syms_of_xdisp ()
21977 {
21978 Vwith_echo_area_save_vector = Qnil;
21979 staticpro (&Vwith_echo_area_save_vector);
21980
21981 Vmessage_stack = Qnil;
21982 staticpro (&Vmessage_stack);
21983
21984 Qinhibit_redisplay = intern ("inhibit-redisplay");
21985 staticpro (&Qinhibit_redisplay);
21986
21987 message_dolog_marker1 = Fmake_marker ();
21988 staticpro (&message_dolog_marker1);
21989 message_dolog_marker2 = Fmake_marker ();
21990 staticpro (&message_dolog_marker2);
21991 message_dolog_marker3 = Fmake_marker ();
21992 staticpro (&message_dolog_marker3);
21993
21994 #if GLYPH_DEBUG
21995 defsubr (&Sdump_frame_glyph_matrix);
21996 defsubr (&Sdump_glyph_matrix);
21997 defsubr (&Sdump_glyph_row);
21998 defsubr (&Sdump_tool_bar_row);
21999 defsubr (&Strace_redisplay);
22000 defsubr (&Strace_to_stderr);
22001 #endif
22002 #ifdef HAVE_WINDOW_SYSTEM
22003 defsubr (&Stool_bar_lines_needed);
22004 defsubr (&Slookup_image_map);
22005 #endif
22006 defsubr (&Sformat_mode_line);
22007
22008 staticpro (&Qmenu_bar_update_hook);
22009 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
22010
22011 staticpro (&Qoverriding_terminal_local_map);
22012 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
22013
22014 staticpro (&Qoverriding_local_map);
22015 Qoverriding_local_map = intern ("overriding-local-map");
22016
22017 staticpro (&Qwindow_scroll_functions);
22018 Qwindow_scroll_functions = intern ("window-scroll-functions");
22019
22020 staticpro (&Qredisplay_end_trigger_functions);
22021 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
22022
22023 staticpro (&Qinhibit_point_motion_hooks);
22024 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
22025
22026 QCdata = intern (":data");
22027 staticpro (&QCdata);
22028 Qdisplay = intern ("display");
22029 staticpro (&Qdisplay);
22030 Qspace_width = intern ("space-width");
22031 staticpro (&Qspace_width);
22032 Qraise = intern ("raise");
22033 staticpro (&Qraise);
22034 Qslice = intern ("slice");
22035 staticpro (&Qslice);
22036 Qspace = intern ("space");
22037 staticpro (&Qspace);
22038 Qmargin = intern ("margin");
22039 staticpro (&Qmargin);
22040 Qpointer = intern ("pointer");
22041 staticpro (&Qpointer);
22042 Qleft_margin = intern ("left-margin");
22043 staticpro (&Qleft_margin);
22044 Qright_margin = intern ("right-margin");
22045 staticpro (&Qright_margin);
22046 Qcenter = intern ("center");
22047 staticpro (&Qcenter);
22048 Qline_height = intern ("line-height");
22049 staticpro (&Qline_height);
22050 Qtotal = intern ("total");
22051 staticpro (&Qtotal);
22052 QCalign_to = intern (":align-to");
22053 staticpro (&QCalign_to);
22054 QCrelative_width = intern (":relative-width");
22055 staticpro (&QCrelative_width);
22056 QCrelative_height = intern (":relative-height");
22057 staticpro (&QCrelative_height);
22058 QCeval = intern (":eval");
22059 staticpro (&QCeval);
22060 QCpropertize = intern (":propertize");
22061 staticpro (&QCpropertize);
22062 QCfile = intern (":file");
22063 staticpro (&QCfile);
22064 Qfontified = intern ("fontified");
22065 staticpro (&Qfontified);
22066 Qfontification_functions = intern ("fontification-functions");
22067 staticpro (&Qfontification_functions);
22068 Qtrailing_whitespace = intern ("trailing-whitespace");
22069 staticpro (&Qtrailing_whitespace);
22070 Qimage = intern ("image");
22071 staticpro (&Qimage);
22072 QCmap = intern (":map");
22073 staticpro (&QCmap);
22074 QCpointer = intern (":pointer");
22075 staticpro (&QCpointer);
22076 Qrect = intern ("rect");
22077 staticpro (&Qrect);
22078 Qcircle = intern ("circle");
22079 staticpro (&Qcircle);
22080 Qpoly = intern ("poly");
22081 staticpro (&Qpoly);
22082 Qmessage_truncate_lines = intern ("message-truncate-lines");
22083 staticpro (&Qmessage_truncate_lines);
22084 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
22085 staticpro (&Qcursor_in_non_selected_windows);
22086 Qgrow_only = intern ("grow-only");
22087 staticpro (&Qgrow_only);
22088 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
22089 staticpro (&Qinhibit_menubar_update);
22090 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
22091 staticpro (&Qinhibit_eval_during_redisplay);
22092 Qposition = intern ("position");
22093 staticpro (&Qposition);
22094 Qbuffer_position = intern ("buffer-position");
22095 staticpro (&Qbuffer_position);
22096 Qobject = intern ("object");
22097 staticpro (&Qobject);
22098 Qbar = intern ("bar");
22099 staticpro (&Qbar);
22100 Qhbar = intern ("hbar");
22101 staticpro (&Qhbar);
22102 Qbox = intern ("box");
22103 staticpro (&Qbox);
22104 Qhollow = intern ("hollow");
22105 staticpro (&Qhollow);
22106 Qhand = intern ("hand");
22107 staticpro (&Qhand);
22108 Qarrow = intern ("arrow");
22109 staticpro (&Qarrow);
22110 Qtext = intern ("text");
22111 staticpro (&Qtext);
22112 Qrisky_local_variable = intern ("risky-local-variable");
22113 staticpro (&Qrisky_local_variable);
22114 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
22115 staticpro (&Qinhibit_free_realized_faces);
22116
22117 list_of_error = Fcons (Fcons (intern ("error"),
22118 Fcons (intern ("void-variable"), Qnil)),
22119 Qnil);
22120 staticpro (&list_of_error);
22121
22122 Qlast_arrow_position = intern ("last-arrow-position");
22123 staticpro (&Qlast_arrow_position);
22124 Qlast_arrow_string = intern ("last-arrow-string");
22125 staticpro (&Qlast_arrow_string);
22126
22127 Qoverlay_arrow_string = intern ("overlay-arrow-string");
22128 staticpro (&Qoverlay_arrow_string);
22129 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
22130 staticpro (&Qoverlay_arrow_bitmap);
22131
22132 echo_buffer[0] = echo_buffer[1] = Qnil;
22133 staticpro (&echo_buffer[0]);
22134 staticpro (&echo_buffer[1]);
22135
22136 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
22137 staticpro (&echo_area_buffer[0]);
22138 staticpro (&echo_area_buffer[1]);
22139
22140 Vmessages_buffer_name = build_string ("*Messages*");
22141 staticpro (&Vmessages_buffer_name);
22142
22143 mode_line_proptrans_alist = Qnil;
22144 staticpro (&mode_line_proptrans_alist);
22145
22146 mode_line_string_list = Qnil;
22147 staticpro (&mode_line_string_list);
22148
22149 help_echo_string = Qnil;
22150 staticpro (&help_echo_string);
22151 help_echo_object = Qnil;
22152 staticpro (&help_echo_object);
22153 help_echo_window = Qnil;
22154 staticpro (&help_echo_window);
22155 previous_help_echo_string = Qnil;
22156 staticpro (&previous_help_echo_string);
22157 help_echo_pos = -1;
22158
22159 #ifdef HAVE_WINDOW_SYSTEM
22160 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
22161 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
22162 For example, if a block cursor is over a tab, it will be drawn as
22163 wide as that tab on the display. */);
22164 x_stretch_cursor_p = 0;
22165 #endif
22166
22167 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
22168 doc: /* *Non-nil means highlight trailing whitespace.
22169 The face used for trailing whitespace is `trailing-whitespace'. */);
22170 Vshow_trailing_whitespace = Qnil;
22171
22172 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
22173 doc: /* *The pointer shape to show in void text areas.
22174 Nil means to show the text pointer. Other options are `arrow', `text',
22175 `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
22176 Vvoid_text_area_pointer = Qarrow;
22177
22178 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
22179 doc: /* Non-nil means don't actually do any redisplay.
22180 This is used for internal purposes. */);
22181 Vinhibit_redisplay = Qnil;
22182
22183 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
22184 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
22185 Vglobal_mode_string = Qnil;
22186
22187 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
22188 doc: /* Marker for where to display an arrow on top of the buffer text.
22189 This must be the beginning of a line in order to work.
22190 See also `overlay-arrow-string'. */);
22191 Voverlay_arrow_position = Qnil;
22192
22193 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
22194 doc: /* String to display as an arrow in non-window frames.
22195 See also `overlay-arrow-position'. */);
22196 Voverlay_arrow_string = Qnil;
22197
22198 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
22199 doc: /* List of variables (symbols) which hold markers for overlay arrows.
22200 The symbols on this list are examined during redisplay to determine
22201 where to display overlay arrows. */);
22202 Voverlay_arrow_variable_list
22203 = Fcons (intern ("overlay-arrow-position"), Qnil);
22204
22205 DEFVAR_INT ("scroll-step", &scroll_step,
22206 doc: /* *The number of lines to try scrolling a window by when point moves out.
22207 If that fails to bring point back on frame, point is centered instead.
22208 If this is zero, point is always centered after it moves off frame.
22209 If you want scrolling to always be a line at a time, you should set
22210 `scroll-conservatively' to a large value rather than set this to 1. */);
22211
22212 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
22213 doc: /* *Scroll up to this many lines, to bring point back on screen.
22214 A value of zero means to scroll the text to center point vertically
22215 in the window. */);
22216 scroll_conservatively = 0;
22217
22218 DEFVAR_INT ("scroll-margin", &scroll_margin,
22219 doc: /* *Number of lines of margin at the top and bottom of a window.
22220 Recenter the window whenever point gets within this many lines
22221 of the top or bottom of the window. */);
22222 scroll_margin = 0;
22223
22224 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
22225 doc: /* Pixels per inch on current display.
22226 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
22227 Vdisplay_pixels_per_inch = make_float (72.0);
22228
22229 #if GLYPH_DEBUG
22230 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
22231 #endif
22232
22233 DEFVAR_BOOL ("truncate-partial-width-windows",
22234 &truncate_partial_width_windows,
22235 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
22236 truncate_partial_width_windows = 1;
22237
22238 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
22239 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
22240 Any other value means to use the appropriate face, `mode-line',
22241 `header-line', or `menu' respectively. */);
22242 mode_line_inverse_video = 1;
22243
22244 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
22245 doc: /* *Maximum buffer size for which line number should be displayed.
22246 If the buffer is bigger than this, the line number does not appear
22247 in the mode line. A value of nil means no limit. */);
22248 Vline_number_display_limit = Qnil;
22249
22250 DEFVAR_INT ("line-number-display-limit-width",
22251 &line_number_display_limit_width,
22252 doc: /* *Maximum line width (in characters) for line number display.
22253 If the average length of the lines near point is bigger than this, then the
22254 line number may be omitted from the mode line. */);
22255 line_number_display_limit_width = 200;
22256
22257 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
22258 doc: /* *Non-nil means highlight region even in nonselected windows. */);
22259 highlight_nonselected_windows = 0;
22260
22261 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
22262 doc: /* Non-nil if more than one frame is visible on this display.
22263 Minibuffer-only frames don't count, but iconified frames do.
22264 This variable is not guaranteed to be accurate except while processing
22265 `frame-title-format' and `icon-title-format'. */);
22266
22267 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
22268 doc: /* Template for displaying the title bar of visible frames.
22269 \(Assuming the window manager supports this feature.)
22270 This variable has the same structure as `mode-line-format' (which see),
22271 and is used only on frames for which no explicit name has been set
22272 \(see `modify-frame-parameters'). */);
22273
22274 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
22275 doc: /* Template for displaying the title bar of an iconified frame.
22276 \(Assuming the window manager supports this feature.)
22277 This variable has the same structure as `mode-line-format' (which see),
22278 and is used only on frames for which no explicit name has been set
22279 \(see `modify-frame-parameters'). */);
22280 Vicon_title_format
22281 = Vframe_title_format
22282 = Fcons (intern ("multiple-frames"),
22283 Fcons (build_string ("%b"),
22284 Fcons (Fcons (empty_string,
22285 Fcons (intern ("invocation-name"),
22286 Fcons (build_string ("@"),
22287 Fcons (intern ("system-name"),
22288 Qnil)))),
22289 Qnil)));
22290
22291 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
22292 doc: /* Maximum number of lines to keep in the message log buffer.
22293 If nil, disable message logging. If t, log messages but don't truncate
22294 the buffer when it becomes large. */);
22295 Vmessage_log_max = make_number (50);
22296
22297 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
22298 doc: /* Functions called before redisplay, if window sizes have changed.
22299 The value should be a list of functions that take one argument.
22300 Just before redisplay, for each frame, if any of its windows have changed
22301 size since the last redisplay, or have been split or deleted,
22302 all the functions in the list are called, with the frame as argument. */);
22303 Vwindow_size_change_functions = Qnil;
22304
22305 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
22306 doc: /* List of functions to call before redisplaying a window with scrolling.
22307 Each function is called with two arguments, the window
22308 and its new display-start position. Note that the value of `window-end'
22309 is not valid when these functions are called. */);
22310 Vwindow_scroll_functions = Qnil;
22311
22312 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
22313 doc: /* *Non-nil means autoselect window with mouse pointer. */);
22314 mouse_autoselect_window = 0;
22315
22316 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
22317 doc: /* *Non-nil means automatically resize tool-bars.
22318 This increases a tool-bar's height if not all tool-bar items are visible.
22319 It decreases a tool-bar's height when it would display blank lines
22320 otherwise. */);
22321 auto_resize_tool_bars_p = 1;
22322
22323 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
22324 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
22325 auto_raise_tool_bar_buttons_p = 1;
22326
22327 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
22328 doc: /* *Margin around tool-bar buttons in pixels.
22329 If an integer, use that for both horizontal and vertical margins.
22330 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
22331 HORZ specifying the horizontal margin, and VERT specifying the
22332 vertical margin. */);
22333 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
22334
22335 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
22336 doc: /* *Relief thickness of tool-bar buttons. */);
22337 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
22338
22339 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
22340 doc: /* List of functions to call to fontify regions of text.
22341 Each function is called with one argument POS. Functions must
22342 fontify a region starting at POS in the current buffer, and give
22343 fontified regions the property `fontified'. */);
22344 Vfontification_functions = Qnil;
22345 Fmake_variable_buffer_local (Qfontification_functions);
22346
22347 DEFVAR_BOOL ("unibyte-display-via-language-environment",
22348 &unibyte_display_via_language_environment,
22349 doc: /* *Non-nil means display unibyte text according to language environment.
22350 Specifically this means that unibyte non-ASCII characters
22351 are displayed by converting them to the equivalent multibyte characters
22352 according to the current language environment. As a result, they are
22353 displayed according to the current fontset. */);
22354 unibyte_display_via_language_environment = 0;
22355
22356 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
22357 doc: /* *Maximum height for resizing mini-windows.
22358 If a float, it specifies a fraction of the mini-window frame's height.
22359 If an integer, it specifies a number of lines. */);
22360 Vmax_mini_window_height = make_float (0.25);
22361
22362 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
22363 doc: /* *How to resize mini-windows.
22364 A value of nil means don't automatically resize mini-windows.
22365 A value of t means resize them to fit the text displayed in them.
22366 A value of `grow-only', the default, means let mini-windows grow
22367 only, until their display becomes empty, at which point the windows
22368 go back to their normal size. */);
22369 Vresize_mini_windows = Qgrow_only;
22370
22371 DEFVAR_LISP ("cursor-in-non-selected-windows",
22372 &Vcursor_in_non_selected_windows,
22373 doc: /* *Cursor type to display in non-selected windows.
22374 t means to use hollow box cursor. See `cursor-type' for other values. */);
22375 Vcursor_in_non_selected_windows = Qt;
22376
22377 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
22378 doc: /* Alist specifying how to blink the cursor off.
22379 Each element has the form (ON-STATE . OFF-STATE). Whenever the
22380 `cursor-type' frame-parameter or variable equals ON-STATE,
22381 comparing using `equal', Emacs uses OFF-STATE to specify
22382 how to blink it off. */);
22383 Vblink_cursor_alist = Qnil;
22384
22385 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
22386 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
22387 automatic_hscrolling_p = 1;
22388
22389 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
22390 doc: /* *How many columns away from the window edge point is allowed to get
22391 before automatic hscrolling will horizontally scroll the window. */);
22392 hscroll_margin = 5;
22393
22394 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
22395 doc: /* *How many columns to scroll the window when point gets too close to the edge.
22396 When point is less than `automatic-hscroll-margin' columns from the window
22397 edge, automatic hscrolling will scroll the window by the amount of columns
22398 determined by this variable. If its value is a positive integer, scroll that
22399 many columns. If it's a positive floating-point number, it specifies the
22400 fraction of the window's width to scroll. If it's nil or zero, point will be
22401 centered horizontally after the scroll. Any other value, including negative
22402 numbers, are treated as if the value were zero.
22403
22404 Automatic hscrolling always moves point outside the scroll margin, so if
22405 point was more than scroll step columns inside the margin, the window will
22406 scroll more than the value given by the scroll step.
22407
22408 Note that the lower bound for automatic hscrolling specified by `scroll-left'
22409 and `scroll-right' overrides this variable's effect. */);
22410 Vhscroll_step = make_number (0);
22411
22412 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
22413 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
22414 Bind this around calls to `message' to let it take effect. */);
22415 message_truncate_lines = 0;
22416
22417 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
22418 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
22419 Can be used to update submenus whose contents should vary. */);
22420 Vmenu_bar_update_hook = Qnil;
22421
22422 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
22423 doc: /* Non-nil means don't update menu bars. Internal use only. */);
22424 inhibit_menubar_update = 0;
22425
22426 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
22427 doc: /* Non-nil means don't eval Lisp during redisplay. */);
22428 inhibit_eval_during_redisplay = 0;
22429
22430 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
22431 doc: /* Non-nil means don't free realized faces. Internal use only. */);
22432 inhibit_free_realized_faces = 0;
22433
22434 #if GLYPH_DEBUG
22435 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
22436 doc: /* Inhibit try_window_id display optimization. */);
22437 inhibit_try_window_id = 0;
22438
22439 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
22440 doc: /* Inhibit try_window_reusing display optimization. */);
22441 inhibit_try_window_reusing = 0;
22442
22443 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
22444 doc: /* Inhibit try_cursor_movement display optimization. */);
22445 inhibit_try_cursor_movement = 0;
22446 #endif /* GLYPH_DEBUG */
22447 }
22448
22449
22450 /* Initialize this module when Emacs starts. */
22451
22452 void
22453 init_xdisp ()
22454 {
22455 Lisp_Object root_window;
22456 struct window *mini_w;
22457
22458 current_header_line_height = current_mode_line_height = -1;
22459
22460 CHARPOS (this_line_start_pos) = 0;
22461
22462 mini_w = XWINDOW (minibuf_window);
22463 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
22464
22465 if (!noninteractive)
22466 {
22467 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
22468 int i;
22469
22470 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
22471 set_window_height (root_window,
22472 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
22473 0);
22474 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
22475 set_window_height (minibuf_window, 1, 0);
22476
22477 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
22478 mini_w->total_cols = make_number (FRAME_COLS (f));
22479
22480 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
22481 scratch_glyph_row.glyphs[TEXT_AREA + 1]
22482 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
22483
22484 /* The default ellipsis glyphs `...'. */
22485 for (i = 0; i < 3; ++i)
22486 default_invis_vector[i] = make_number ('.');
22487 }
22488
22489 {
22490 /* Allocate the buffer for frame titles.
22491 Also used for `format-mode-line'. */
22492 int size = 100;
22493 frame_title_buf = (char *) xmalloc (size);
22494 frame_title_buf_end = frame_title_buf + size;
22495 frame_title_ptr = NULL;
22496 }
22497
22498 help_echo_showing_p = 0;
22499 }
22500
22501
22502 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
22503 (do not change this comment) */