]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(define_frame_cursor1): Do not change frame cursor
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985,86,87,88,93,94,95,97,98,99,2000,01,02,03,04
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the description of direct update
37 operations, below.)
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay do? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
91 You will find a lot of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking an iterator structure (struct it)
124 argument.
125
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
131
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
138
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
146
147
148 Frame matrices.
149
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
156
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
168
169 #include <config.h>
170 #include <stdio.h>
171
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "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 Lisp_Object do_mouse_tracking;
219
220 extern int minibuffer_auto_raise;
221 extern Lisp_Object Vminibuffer_list;
222
223 extern Lisp_Object Qface;
224 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
225
226 extern Lisp_Object Voverriding_local_map;
227 extern Lisp_Object Voverriding_local_map_menu_flag;
228 extern Lisp_Object Qmenu_item;
229 extern Lisp_Object Qwhen;
230 extern Lisp_Object Qhelp_echo;
231
232 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
233 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
234 Lisp_Object Qredisplay_end_trigger_functions;
235 Lisp_Object Qinhibit_point_motion_hooks;
236 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
237 Lisp_Object Qfontified;
238 Lisp_Object Qgrow_only;
239 Lisp_Object Qinhibit_eval_during_redisplay;
240 Lisp_Object Qbuffer_position, Qposition, Qobject;
241
242 /* Cursor shapes */
243 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
244
245 /* Pointer shapes */
246 Lisp_Object Qarrow, Qhand, Qtext;
247
248 Lisp_Object Qrisky_local_variable;
249
250 /* Holds the list (error). */
251 Lisp_Object list_of_error;
252
253 /* Functions called to fontify regions of text. */
254
255 Lisp_Object Vfontification_functions;
256 Lisp_Object Qfontification_functions;
257
258 /* Non-zero means automatically select any window when the mouse
259 cursor moves into it. */
260 int mouse_autoselect_window;
261
262 /* Non-zero means draw tool bar buttons raised when the mouse moves
263 over them. */
264
265 int auto_raise_tool_bar_buttons_p;
266
267 /* Margin around tool bar buttons in pixels. */
268
269 Lisp_Object Vtool_bar_button_margin;
270
271 /* Thickness of shadow to draw around tool bar buttons. */
272
273 EMACS_INT tool_bar_button_relief;
274
275 /* Non-zero means automatically resize tool-bars so that all tool-bar
276 items are visible, and no blank lines remain. */
277
278 int auto_resize_tool_bars_p;
279
280 /* Non-zero means draw block and hollow cursor as wide as the glyph
281 under it. For example, if a block cursor is over a tab, it will be
282 drawn as wide as that tab on the display. */
283
284 int x_stretch_cursor_p;
285
286 /* Non-nil means don't actually do any redisplay. */
287
288 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
289
290 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
291
292 int inhibit_eval_during_redisplay;
293
294 /* Names of text properties relevant for redisplay. */
295
296 Lisp_Object Qdisplay;
297 extern Lisp_Object Qface, Qinvisible, Qwidth;
298
299 /* Symbols used in text property values. */
300
301 Lisp_Object Vdisplay_pixels_per_inch;
302 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
303 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
304 Lisp_Object Qslice;
305 Lisp_Object Qcenter;
306 Lisp_Object Qmargin, Qpointer;
307 Lisp_Object Qline_height, Qtotal;
308 extern Lisp_Object Qheight;
309 extern Lisp_Object QCwidth, QCheight, QCascent;
310 extern Lisp_Object Qscroll_bar;
311 extern Lisp_Object Qcursor;
312
313 /* Non-nil means highlight trailing whitespace. */
314
315 Lisp_Object Vshow_trailing_whitespace;
316
317 #ifdef HAVE_WINDOW_SYSTEM
318 extern Lisp_Object Voverflow_newline_into_fringe;
319
320 /* Test if overflow newline into fringe. Called with iterator IT
321 at or past right window margin, and with IT->current_x set. */
322
323 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
324 (!NILP (Voverflow_newline_into_fringe) \
325 && FRAME_WINDOW_P (it->f) \
326 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
327 && it->current_x == it->last_visible_x)
328
329 #endif /* HAVE_WINDOW_SYSTEM */
330
331 /* Non-nil means show the text cursor in void text areas
332 i.e. in blank areas after eol and eob. This used to be
333 the default in 21.3. */
334
335 Lisp_Object Vvoid_text_area_pointer;
336
337 /* Name of the face used to highlight trailing whitespace. */
338
339 Lisp_Object Qtrailing_whitespace;
340
341 /* The symbol `image' which is the car of the lists used to represent
342 images in Lisp. */
343
344 Lisp_Object Qimage;
345
346 /* The image map types. */
347 Lisp_Object QCmap, QCpointer;
348 Lisp_Object Qrect, Qcircle, Qpoly;
349
350 /* Non-zero means print newline to stdout before next mini-buffer
351 message. */
352
353 int noninteractive_need_newline;
354
355 /* Non-zero means print newline to message log before next message. */
356
357 static int message_log_need_newline;
358
359 /* Three markers that message_dolog uses.
360 It could allocate them itself, but that causes trouble
361 in handling memory-full errors. */
362 static Lisp_Object message_dolog_marker1;
363 static Lisp_Object message_dolog_marker2;
364 static Lisp_Object message_dolog_marker3;
365 \f
366 /* The buffer position of the first character appearing entirely or
367 partially on the line of the selected window which contains the
368 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
369 redisplay optimization in redisplay_internal. */
370
371 static struct text_pos this_line_start_pos;
372
373 /* Number of characters past the end of the line above, including the
374 terminating newline. */
375
376 static struct text_pos this_line_end_pos;
377
378 /* The vertical positions and the height of this line. */
379
380 static int this_line_vpos;
381 static int this_line_y;
382 static int this_line_pixel_height;
383
384 /* X position at which this display line starts. Usually zero;
385 negative if first character is partially visible. */
386
387 static int this_line_start_x;
388
389 /* Buffer that this_line_.* variables are referring to. */
390
391 static struct buffer *this_line_buffer;
392
393 /* Nonzero means truncate lines in all windows less wide than the
394 frame. */
395
396 int truncate_partial_width_windows;
397
398 /* A flag to control how to display unibyte 8-bit character. */
399
400 int unibyte_display_via_language_environment;
401
402 /* Nonzero means we have more than one non-mini-buffer-only frame.
403 Not guaranteed to be accurate except while parsing
404 frame-title-format. */
405
406 int multiple_frames;
407
408 Lisp_Object Vglobal_mode_string;
409
410
411 /* List of variables (symbols) which hold markers for overlay arrows.
412 The symbols on this list are examined during redisplay to determine
413 where to display overlay arrows. */
414
415 Lisp_Object Voverlay_arrow_variable_list;
416
417 /* Marker for where to display an arrow on top of the buffer text. */
418
419 Lisp_Object Voverlay_arrow_position;
420
421 /* String to display for the arrow. Only used on terminal frames. */
422
423 Lisp_Object Voverlay_arrow_string;
424
425 /* Values of those variables at last redisplay are stored as
426 properties on `overlay-arrow-position' symbol. However, if
427 Voverlay_arrow_position is a marker, last-arrow-position is its
428 numerical position. */
429
430 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
431
432 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
433 properties on a symbol in overlay-arrow-variable-list. */
434
435 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
436
437 /* Like mode-line-format, but for the title bar on a visible frame. */
438
439 Lisp_Object Vframe_title_format;
440
441 /* Like mode-line-format, but for the title bar on an iconified frame. */
442
443 Lisp_Object Vicon_title_format;
444
445 /* List of functions to call when a window's size changes. These
446 functions get one arg, a frame on which one or more windows' sizes
447 have changed. */
448
449 static Lisp_Object Vwindow_size_change_functions;
450
451 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
452
453 /* Nonzero if overlay arrow has been displayed once in this window. */
454
455 static int overlay_arrow_seen;
456
457 /* Nonzero means highlight the region even in nonselected windows. */
458
459 int highlight_nonselected_windows;
460
461 /* If cursor motion alone moves point off frame, try scrolling this
462 many lines up or down if that will bring it back. */
463
464 static EMACS_INT scroll_step;
465
466 /* Nonzero means scroll just far enough to bring point back on the
467 screen, when appropriate. */
468
469 static EMACS_INT scroll_conservatively;
470
471 /* Recenter the window whenever point gets within this many lines of
472 the top or bottom of the window. This value is translated into a
473 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
474 that there is really a fixed pixel height scroll margin. */
475
476 EMACS_INT scroll_margin;
477
478 /* Number of windows showing the buffer of the selected window (or
479 another buffer with the same base buffer). keyboard.c refers to
480 this. */
481
482 int buffer_shared;
483
484 /* Vector containing glyphs for an ellipsis `...'. */
485
486 static Lisp_Object default_invis_vector[3];
487
488 /* Zero means display the mode-line/header-line/menu-bar in the default face
489 (this slightly odd definition is for compatibility with previous versions
490 of emacs), non-zero means display them using their respective faces.
491
492 This variable is deprecated. */
493
494 int mode_line_inverse_video;
495
496 /* Prompt to display in front of the mini-buffer contents. */
497
498 Lisp_Object minibuf_prompt;
499
500 /* Width of current mini-buffer prompt. Only set after display_line
501 of the line that contains the prompt. */
502
503 int minibuf_prompt_width;
504
505 /* This is the window where the echo area message was displayed. It
506 is always a mini-buffer window, but it may not be the same window
507 currently active as a mini-buffer. */
508
509 Lisp_Object echo_area_window;
510
511 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
512 pushes the current message and the value of
513 message_enable_multibyte on the stack, the function restore_message
514 pops the stack and displays MESSAGE again. */
515
516 Lisp_Object Vmessage_stack;
517
518 /* Nonzero means multibyte characters were enabled when the echo area
519 message was specified. */
520
521 int message_enable_multibyte;
522
523 /* Nonzero if we should redraw the mode lines on the next redisplay. */
524
525 int update_mode_lines;
526
527 /* Nonzero if window sizes or contents have changed since last
528 redisplay that finished. */
529
530 int windows_or_buffers_changed;
531
532 /* Nonzero means a frame's cursor type has been changed. */
533
534 int cursor_type_changed;
535
536 /* Nonzero after display_mode_line if %l was used and it displayed a
537 line number. */
538
539 int line_number_displayed;
540
541 /* Maximum buffer size for which to display line numbers. */
542
543 Lisp_Object Vline_number_display_limit;
544
545 /* Line width to consider when repositioning for line number display. */
546
547 static EMACS_INT line_number_display_limit_width;
548
549 /* Number of lines to keep in the message log buffer. t means
550 infinite. nil means don't log at all. */
551
552 Lisp_Object Vmessage_log_max;
553
554 /* The name of the *Messages* buffer, a string. */
555
556 static Lisp_Object Vmessages_buffer_name;
557
558 /* Current, index 0, and last displayed echo area message. Either
559 buffers from echo_buffers, or nil to indicate no message. */
560
561 Lisp_Object echo_area_buffer[2];
562
563 /* The buffers referenced from echo_area_buffer. */
564
565 static Lisp_Object echo_buffer[2];
566
567 /* A vector saved used in with_area_buffer to reduce consing. */
568
569 static Lisp_Object Vwith_echo_area_save_vector;
570
571 /* Non-zero means display_echo_area should display the last echo area
572 message again. Set by redisplay_preserve_echo_area. */
573
574 static int display_last_displayed_message_p;
575
576 /* Nonzero if echo area is being used by print; zero if being used by
577 message. */
578
579 int message_buf_print;
580
581 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
582
583 Lisp_Object Qinhibit_menubar_update;
584 int inhibit_menubar_update;
585
586 /* Maximum height for resizing mini-windows. Either a float
587 specifying a fraction of the available height, or an integer
588 specifying a number of lines. */
589
590 Lisp_Object Vmax_mini_window_height;
591
592 /* Non-zero means messages should be displayed with truncated
593 lines instead of being continued. */
594
595 int message_truncate_lines;
596 Lisp_Object Qmessage_truncate_lines;
597
598 /* Set to 1 in clear_message to make redisplay_internal aware
599 of an emptied echo area. */
600
601 static int message_cleared_p;
602
603 /* Non-zero means we want a hollow cursor in windows that are not
604 selected. Zero means there's no cursor in such windows. */
605
606 Lisp_Object Vcursor_in_non_selected_windows;
607 Lisp_Object Qcursor_in_non_selected_windows;
608
609 /* How to blink the default frame cursor off. */
610 Lisp_Object Vblink_cursor_alist;
611
612 /* A scratch glyph row with contents used for generating truncation
613 glyphs. Also used in direct_output_for_insert. */
614
615 #define MAX_SCRATCH_GLYPHS 100
616 struct glyph_row scratch_glyph_row;
617 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
618
619 /* Ascent and height of the last line processed by move_it_to. */
620
621 static int last_max_ascent, last_height;
622
623 /* Non-zero if there's a help-echo in the echo area. */
624
625 int help_echo_showing_p;
626
627 /* If >= 0, computed, exact values of mode-line and header-line height
628 to use in the macros CURRENT_MODE_LINE_HEIGHT and
629 CURRENT_HEADER_LINE_HEIGHT. */
630
631 int current_mode_line_height, current_header_line_height;
632
633 /* The maximum distance to look ahead for text properties. Values
634 that are too small let us call compute_char_face and similar
635 functions too often which is expensive. Values that are too large
636 let us call compute_char_face and alike too often because we
637 might not be interested in text properties that far away. */
638
639 #define TEXT_PROP_DISTANCE_LIMIT 100
640
641 #if GLYPH_DEBUG
642
643 /* Variables to turn off display optimizations from Lisp. */
644
645 int inhibit_try_window_id, inhibit_try_window_reusing;
646 int inhibit_try_cursor_movement;
647
648 /* Non-zero means print traces of redisplay if compiled with
649 GLYPH_DEBUG != 0. */
650
651 int trace_redisplay_p;
652
653 #endif /* GLYPH_DEBUG */
654
655 #ifdef DEBUG_TRACE_MOVE
656 /* Non-zero means trace with TRACE_MOVE to stderr. */
657 int trace_move;
658
659 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
660 #else
661 #define TRACE_MOVE(x) (void) 0
662 #endif
663
664 /* Non-zero means automatically scroll windows horizontally to make
665 point visible. */
666
667 int automatic_hscrolling_p;
668
669 /* How close to the margin can point get before the window is scrolled
670 horizontally. */
671 EMACS_INT hscroll_margin;
672
673 /* How much to scroll horizontally when point is inside the above margin. */
674 Lisp_Object Vhscroll_step;
675
676 /* The variable `resize-mini-windows'. If nil, don't resize
677 mini-windows. If t, always resize them to fit the text they
678 display. If `grow-only', let mini-windows grow only until they
679 become empty. */
680
681 Lisp_Object Vresize_mini_windows;
682
683 /* Buffer being redisplayed -- for redisplay_window_error. */
684
685 struct buffer *displayed_buffer;
686
687 /* Value returned from text property handlers (see below). */
688
689 enum prop_handled
690 {
691 HANDLED_NORMALLY,
692 HANDLED_RECOMPUTE_PROPS,
693 HANDLED_OVERLAY_STRING_CONSUMED,
694 HANDLED_RETURN
695 };
696
697 /* A description of text properties that redisplay is interested
698 in. */
699
700 struct props
701 {
702 /* The name of the property. */
703 Lisp_Object *name;
704
705 /* A unique index for the property. */
706 enum prop_idx idx;
707
708 /* A handler function called to set up iterator IT from the property
709 at IT's current position. Value is used to steer handle_stop. */
710 enum prop_handled (*handler) P_ ((struct it *it));
711 };
712
713 static enum prop_handled handle_face_prop P_ ((struct it *));
714 static enum prop_handled handle_invisible_prop P_ ((struct it *));
715 static enum prop_handled handle_display_prop P_ ((struct it *));
716 static enum prop_handled handle_composition_prop P_ ((struct it *));
717 static enum prop_handled handle_overlay_change P_ ((struct it *));
718 static enum prop_handled handle_fontified_prop P_ ((struct it *));
719
720 /* Properties handled by iterators. */
721
722 static struct props it_props[] =
723 {
724 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
725 /* Handle `face' before `display' because some sub-properties of
726 `display' need to know the face. */
727 {&Qface, FACE_PROP_IDX, handle_face_prop},
728 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
729 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
730 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
731 {NULL, 0, NULL}
732 };
733
734 /* Value is the position described by X. If X is a marker, value is
735 the marker_position of X. Otherwise, value is X. */
736
737 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
738
739 /* Enumeration returned by some move_it_.* functions internally. */
740
741 enum move_it_result
742 {
743 /* Not used. Undefined value. */
744 MOVE_UNDEFINED,
745
746 /* Move ended at the requested buffer position or ZV. */
747 MOVE_POS_MATCH_OR_ZV,
748
749 /* Move ended at the requested X pixel position. */
750 MOVE_X_REACHED,
751
752 /* Move within a line ended at the end of a line that must be
753 continued. */
754 MOVE_LINE_CONTINUED,
755
756 /* Move within a line ended at the end of a line that would
757 be displayed truncated. */
758 MOVE_LINE_TRUNCATED,
759
760 /* Move within a line ended at a line end. */
761 MOVE_NEWLINE_OR_CR
762 };
763
764 /* This counter is used to clear the face cache every once in a while
765 in redisplay_internal. It is incremented for each redisplay.
766 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
767 cleared. */
768
769 #define CLEAR_FACE_CACHE_COUNT 500
770 static int clear_face_cache_count;
771
772 /* Record the previous terminal frame we displayed. */
773
774 static struct frame *previous_terminal_frame;
775
776 /* Non-zero while redisplay_internal is in progress. */
777
778 int redisplaying_p;
779
780 /* Non-zero means don't free realized faces. Bound while freeing
781 realized faces is dangerous because glyph matrices might still
782 reference them. */
783
784 int inhibit_free_realized_faces;
785 Lisp_Object Qinhibit_free_realized_faces;
786
787 /* If a string, XTread_socket generates an event to display that string.
788 (The display is done in read_char.) */
789
790 Lisp_Object help_echo_string;
791 Lisp_Object help_echo_window;
792 Lisp_Object help_echo_object;
793 int help_echo_pos;
794
795 /* Temporary variable for XTread_socket. */
796
797 Lisp_Object previous_help_echo_string;
798
799 /* Null glyph slice */
800
801 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
802
803 \f
804 /* Function prototypes. */
805
806 static void setup_for_ellipsis P_ ((struct it *));
807 static void mark_window_display_accurate_1 P_ ((struct window *, int));
808 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
809 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
810 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
811 static int redisplay_mode_lines P_ ((Lisp_Object, int));
812 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
813
814 #if 0
815 static int invisible_text_between_p P_ ((struct it *, int, int));
816 #endif
817
818 static int next_element_from_ellipsis P_ ((struct it *));
819 static void pint2str P_ ((char *, int, int));
820 static void pint2hrstr P_ ((char *, int, int));
821 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
822 struct text_pos));
823 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
824 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
825 static void store_frame_title_char P_ ((char));
826 static int store_frame_title P_ ((const unsigned char *, int, int));
827 static void x_consider_frame_title P_ ((Lisp_Object));
828 static void handle_stop P_ ((struct it *));
829 static int tool_bar_lines_needed P_ ((struct frame *));
830 static int single_display_prop_intangible_p P_ ((Lisp_Object));
831 static void ensure_echo_area_buffers P_ ((void));
832 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
833 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
834 static int with_echo_area_buffer P_ ((struct window *, int,
835 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
836 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
837 static void clear_garbaged_frames P_ ((void));
838 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
839 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
840 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
841 static int display_echo_area P_ ((struct window *));
842 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
843 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
844 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
845 static int string_char_and_length P_ ((const unsigned char *, int, int *));
846 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
847 struct text_pos));
848 static int compute_window_start_on_continuation_line P_ ((struct window *));
849 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
850 static void insert_left_trunc_glyphs P_ ((struct it *));
851 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
852 Lisp_Object));
853 static void extend_face_to_end_of_line P_ ((struct it *));
854 static int append_space_for_newline P_ ((struct it *, int));
855 static int make_cursor_line_fully_visible P_ ((struct window *, int));
856 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
857 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
858 static int trailing_whitespace_p P_ ((int));
859 static int message_log_check_duplicate P_ ((int, int, int, int));
860 static void push_it P_ ((struct it *));
861 static void pop_it P_ ((struct it *));
862 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
863 static void select_frame_for_redisplay P_ ((Lisp_Object));
864 static void redisplay_internal P_ ((int));
865 static int echo_area_display P_ ((int));
866 static void redisplay_windows P_ ((Lisp_Object));
867 static void redisplay_window P_ ((Lisp_Object, int));
868 static Lisp_Object redisplay_window_error ();
869 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
870 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
871 static void update_menu_bar P_ ((struct frame *, int));
872 static int try_window_reusing_current_matrix P_ ((struct window *));
873 static int try_window_id P_ ((struct window *));
874 static int display_line P_ ((struct it *));
875 static int display_mode_lines P_ ((struct window *));
876 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
877 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
878 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
879 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
880 static void display_menu_bar P_ ((struct window *));
881 static int display_count_lines P_ ((int, int, int, int, int *));
882 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
883 int, int, struct it *, int, int, int, int));
884 static void compute_line_metrics P_ ((struct it *));
885 static void run_redisplay_end_trigger_hook P_ ((struct it *));
886 static int get_overlay_strings P_ ((struct it *, int));
887 static void next_overlay_string P_ ((struct it *));
888 static void reseat P_ ((struct it *, struct text_pos, int));
889 static void reseat_1 P_ ((struct it *, struct text_pos, int));
890 static void back_to_previous_visible_line_start P_ ((struct it *));
891 static void reseat_at_previous_visible_line_start P_ ((struct it *));
892 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
893 static int next_element_from_display_vector P_ ((struct it *));
894 static int next_element_from_string P_ ((struct it *));
895 static int next_element_from_c_string P_ ((struct it *));
896 static int next_element_from_buffer P_ ((struct it *));
897 static int next_element_from_composition P_ ((struct it *));
898 static int next_element_from_image P_ ((struct it *));
899 static int next_element_from_stretch P_ ((struct it *));
900 static void load_overlay_strings P_ ((struct it *, int));
901 static int init_from_display_pos P_ ((struct it *, struct window *,
902 struct display_pos *));
903 static void reseat_to_string P_ ((struct it *, unsigned char *,
904 Lisp_Object, int, int, int, int));
905 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
906 int, int, int));
907 void move_it_vertically_backward P_ ((struct it *, int));
908 static void init_to_row_start P_ ((struct it *, struct window *,
909 struct glyph_row *));
910 static int init_to_row_end P_ ((struct it *, struct window *,
911 struct glyph_row *));
912 static void back_to_previous_line_start P_ ((struct it *));
913 static int forward_to_next_line_start P_ ((struct it *, int *));
914 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
915 Lisp_Object, int));
916 static struct text_pos string_pos P_ ((int, Lisp_Object));
917 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
918 static int number_of_chars P_ ((unsigned char *, int));
919 static void compute_stop_pos P_ ((struct it *));
920 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
921 Lisp_Object));
922 static int face_before_or_after_it_pos P_ ((struct it *, int));
923 static int next_overlay_change P_ ((int));
924 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
925 Lisp_Object, struct text_pos *,
926 int));
927 static int underlying_face_id P_ ((struct it *));
928 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
929 struct window *));
930
931 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
932 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
933
934 #ifdef HAVE_WINDOW_SYSTEM
935
936 static void update_tool_bar P_ ((struct frame *, int));
937 static void build_desired_tool_bar_string P_ ((struct frame *f));
938 static int redisplay_tool_bar P_ ((struct frame *));
939 static void display_tool_bar_line P_ ((struct it *));
940 static void notice_overwritten_cursor P_ ((struct window *,
941 enum glyph_row_area,
942 int, int, int, int));
943
944
945
946 #endif /* HAVE_WINDOW_SYSTEM */
947
948 \f
949 /***********************************************************************
950 Window display dimensions
951 ***********************************************************************/
952
953 /* Return the bottom boundary y-position for text lines in window W.
954 This is the first y position at which a line cannot start.
955 It is relative to the top of the window.
956
957 This is the height of W minus the height of a mode line, if any. */
958
959 INLINE int
960 window_text_bottom_y (w)
961 struct window *w;
962 {
963 int height = WINDOW_TOTAL_HEIGHT (w);
964
965 if (WINDOW_WANTS_MODELINE_P (w))
966 height -= CURRENT_MODE_LINE_HEIGHT (w);
967 return height;
968 }
969
970 /* Return the pixel width of display area AREA of window W. AREA < 0
971 means return the total width of W, not including fringes to
972 the left and right of the window. */
973
974 INLINE int
975 window_box_width (w, area)
976 struct window *w;
977 int area;
978 {
979 int cols = XFASTINT (w->total_cols);
980 int pixels = 0;
981
982 if (!w->pseudo_window_p)
983 {
984 cols -= WINDOW_SCROLL_BAR_COLS (w);
985
986 if (area == TEXT_AREA)
987 {
988 if (INTEGERP (w->left_margin_cols))
989 cols -= XFASTINT (w->left_margin_cols);
990 if (INTEGERP (w->right_margin_cols))
991 cols -= XFASTINT (w->right_margin_cols);
992 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
993 }
994 else if (area == LEFT_MARGIN_AREA)
995 {
996 cols = (INTEGERP (w->left_margin_cols)
997 ? XFASTINT (w->left_margin_cols) : 0);
998 pixels = 0;
999 }
1000 else if (area == RIGHT_MARGIN_AREA)
1001 {
1002 cols = (INTEGERP (w->right_margin_cols)
1003 ? XFASTINT (w->right_margin_cols) : 0);
1004 pixels = 0;
1005 }
1006 }
1007
1008 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1009 }
1010
1011
1012 /* Return the pixel height of the display area of window W, not
1013 including mode lines of W, if any. */
1014
1015 INLINE int
1016 window_box_height (w)
1017 struct window *w;
1018 {
1019 struct frame *f = XFRAME (w->frame);
1020 int height = WINDOW_TOTAL_HEIGHT (w);
1021
1022 xassert (height >= 0);
1023
1024 /* Note: the code below that determines the mode-line/header-line
1025 height is essentially the same as that contained in the macro
1026 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1027 the appropriate glyph row has its `mode_line_p' flag set,
1028 and if it doesn't, uses estimate_mode_line_height instead. */
1029
1030 if (WINDOW_WANTS_MODELINE_P (w))
1031 {
1032 struct glyph_row *ml_row
1033 = (w->current_matrix && w->current_matrix->rows
1034 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1035 : 0);
1036 if (ml_row && ml_row->mode_line_p)
1037 height -= ml_row->height;
1038 else
1039 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1040 }
1041
1042 if (WINDOW_WANTS_HEADER_LINE_P (w))
1043 {
1044 struct glyph_row *hl_row
1045 = (w->current_matrix && w->current_matrix->rows
1046 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1047 : 0);
1048 if (hl_row && hl_row->mode_line_p)
1049 height -= hl_row->height;
1050 else
1051 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1052 }
1053
1054 /* With a very small font and a mode-line that's taller than
1055 default, we might end up with a negative height. */
1056 return max (0, height);
1057 }
1058
1059 /* Return the window-relative coordinate of the left edge of display
1060 area AREA of window W. AREA < 0 means return the left edge of the
1061 whole window, to the right of the left fringe of W. */
1062
1063 INLINE int
1064 window_box_left_offset (w, area)
1065 struct window *w;
1066 int area;
1067 {
1068 int x;
1069
1070 if (w->pseudo_window_p)
1071 return 0;
1072
1073 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1074
1075 if (area == TEXT_AREA)
1076 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1077 + window_box_width (w, LEFT_MARGIN_AREA));
1078 else if (area == RIGHT_MARGIN_AREA)
1079 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1080 + window_box_width (w, LEFT_MARGIN_AREA)
1081 + window_box_width (w, TEXT_AREA)
1082 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1083 ? 0
1084 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1085 else if (area == LEFT_MARGIN_AREA
1086 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1087 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1088
1089 return x;
1090 }
1091
1092
1093 /* Return the window-relative coordinate of the right edge of display
1094 area AREA of window W. AREA < 0 means return the left edge of the
1095 whole window, to the left of the right fringe of W. */
1096
1097 INLINE int
1098 window_box_right_offset (w, area)
1099 struct window *w;
1100 int area;
1101 {
1102 return window_box_left_offset (w, area) + window_box_width (w, area);
1103 }
1104
1105 /* Return the frame-relative coordinate of the left edge of display
1106 area AREA of window W. AREA < 0 means return the left edge of the
1107 whole window, to the right of the left fringe of W. */
1108
1109 INLINE int
1110 window_box_left (w, area)
1111 struct window *w;
1112 int area;
1113 {
1114 struct frame *f = XFRAME (w->frame);
1115 int x;
1116
1117 if (w->pseudo_window_p)
1118 return FRAME_INTERNAL_BORDER_WIDTH (f);
1119
1120 x = (WINDOW_LEFT_EDGE_X (w)
1121 + window_box_left_offset (w, area));
1122
1123 return x;
1124 }
1125
1126
1127 /* Return the frame-relative coordinate of the right edge of display
1128 area AREA of window W. AREA < 0 means return the left edge of the
1129 whole window, to the left of the right fringe of W. */
1130
1131 INLINE int
1132 window_box_right (w, area)
1133 struct window *w;
1134 int area;
1135 {
1136 return window_box_left (w, area) + window_box_width (w, area);
1137 }
1138
1139 /* Get the bounding box of the display area AREA of window W, without
1140 mode lines, in frame-relative coordinates. AREA < 0 means the
1141 whole window, not including the left and right fringes of
1142 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1143 coordinates of the upper-left corner of the box. Return in
1144 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1145
1146 INLINE void
1147 window_box (w, area, box_x, box_y, box_width, box_height)
1148 struct window *w;
1149 int area;
1150 int *box_x, *box_y, *box_width, *box_height;
1151 {
1152 if (box_width)
1153 *box_width = window_box_width (w, area);
1154 if (box_height)
1155 *box_height = window_box_height (w);
1156 if (box_x)
1157 *box_x = window_box_left (w, area);
1158 if (box_y)
1159 {
1160 *box_y = WINDOW_TOP_EDGE_Y (w);
1161 if (WINDOW_WANTS_HEADER_LINE_P (w))
1162 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1163 }
1164 }
1165
1166
1167 /* Get the bounding box of the display area AREA of window W, without
1168 mode lines. AREA < 0 means the whole window, not including the
1169 left and right fringe of the window. Return in *TOP_LEFT_X
1170 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1171 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1172 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1173 box. */
1174
1175 INLINE void
1176 window_box_edges (w, area, top_left_x, top_left_y,
1177 bottom_right_x, bottom_right_y)
1178 struct window *w;
1179 int area;
1180 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1181 {
1182 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1183 bottom_right_y);
1184 *bottom_right_x += *top_left_x;
1185 *bottom_right_y += *top_left_y;
1186 }
1187
1188
1189 \f
1190 /***********************************************************************
1191 Utilities
1192 ***********************************************************************/
1193
1194 /* Return the bottom y-position of the line the iterator IT is in.
1195 This can modify IT's settings. */
1196
1197 int
1198 line_bottom_y (it)
1199 struct it *it;
1200 {
1201 int line_height = it->max_ascent + it->max_descent;
1202 int line_top_y = it->current_y;
1203
1204 if (line_height == 0)
1205 {
1206 if (last_height)
1207 line_height = last_height;
1208 else if (IT_CHARPOS (*it) < ZV)
1209 {
1210 move_it_by_lines (it, 1, 1);
1211 line_height = (it->max_ascent || it->max_descent
1212 ? it->max_ascent + it->max_descent
1213 : last_height);
1214 }
1215 else
1216 {
1217 struct glyph_row *row = it->glyph_row;
1218
1219 /* Use the default character height. */
1220 it->glyph_row = NULL;
1221 it->what = IT_CHARACTER;
1222 it->c = ' ';
1223 it->len = 1;
1224 PRODUCE_GLYPHS (it);
1225 line_height = it->ascent + it->descent;
1226 it->glyph_row = row;
1227 }
1228 }
1229
1230 return line_top_y + line_height;
1231 }
1232
1233
1234 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1235 1 if POS is visible and the line containing POS is fully visible.
1236 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1237 and header-lines heights. */
1238
1239 int
1240 pos_visible_p (w, charpos, fully, x, y, exact_mode_line_heights_p)
1241 struct window *w;
1242 int charpos, *fully, *x, *y, exact_mode_line_heights_p;
1243 {
1244 struct it it;
1245 struct text_pos top;
1246 int visible_p;
1247 struct buffer *old_buffer = NULL;
1248
1249 if (XBUFFER (w->buffer) != current_buffer)
1250 {
1251 old_buffer = current_buffer;
1252 set_buffer_internal_1 (XBUFFER (w->buffer));
1253 }
1254
1255 *fully = visible_p = 0;
1256 SET_TEXT_POS_FROM_MARKER (top, w->start);
1257
1258 /* Compute exact mode line heights, if requested. */
1259 if (exact_mode_line_heights_p)
1260 {
1261 if (WINDOW_WANTS_MODELINE_P (w))
1262 current_mode_line_height
1263 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1264 current_buffer->mode_line_format);
1265
1266 if (WINDOW_WANTS_HEADER_LINE_P (w))
1267 current_header_line_height
1268 = display_mode_line (w, HEADER_LINE_FACE_ID,
1269 current_buffer->header_line_format);
1270 }
1271
1272 start_display (&it, w, top);
1273 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1274 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1275
1276 /* Note that we may overshoot because of invisible text. */
1277 if (IT_CHARPOS (it) >= charpos)
1278 {
1279 int top_y = it.current_y;
1280 int bottom_y = line_bottom_y (&it);
1281 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1282
1283 if (top_y < window_top_y)
1284 visible_p = bottom_y > window_top_y;
1285 else if (top_y < it.last_visible_y)
1286 {
1287 visible_p = 1;
1288 *fully = bottom_y <= it.last_visible_y;
1289 }
1290 if (visible_p && x)
1291 {
1292 *x = it.current_x;
1293 *y = max (top_y + it.max_ascent - it.ascent, window_top_y);
1294 }
1295 }
1296 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1297 {
1298 struct it it2;
1299
1300 it2 = it;
1301 move_it_by_lines (&it, 1, 0);
1302 if (charpos < IT_CHARPOS (it))
1303 {
1304 visible_p = 1;
1305 if (x)
1306 {
1307 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1308 *x = it2.current_x;
1309 *y = it2.current_y + it2.max_ascent - it2.ascent;
1310 }
1311 }
1312 }
1313
1314 if (old_buffer)
1315 set_buffer_internal_1 (old_buffer);
1316
1317 current_header_line_height = current_mode_line_height = -1;
1318
1319 return visible_p;
1320 }
1321
1322
1323 /* Return the next character from STR which is MAXLEN bytes long.
1324 Return in *LEN the length of the character. This is like
1325 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1326 we find one, we return a `?', but with the length of the invalid
1327 character. */
1328
1329 static INLINE int
1330 string_char_and_length (str, maxlen, len)
1331 const unsigned char *str;
1332 int maxlen, *len;
1333 {
1334 int c;
1335
1336 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1337 if (!CHAR_VALID_P (c, 1))
1338 /* We may not change the length here because other places in Emacs
1339 don't use this function, i.e. they silently accept invalid
1340 characters. */
1341 c = '?';
1342
1343 return c;
1344 }
1345
1346
1347
1348 /* Given a position POS containing a valid character and byte position
1349 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1350
1351 static struct text_pos
1352 string_pos_nchars_ahead (pos, string, nchars)
1353 struct text_pos pos;
1354 Lisp_Object string;
1355 int nchars;
1356 {
1357 xassert (STRINGP (string) && nchars >= 0);
1358
1359 if (STRING_MULTIBYTE (string))
1360 {
1361 int rest = SBYTES (string) - BYTEPOS (pos);
1362 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1363 int len;
1364
1365 while (nchars--)
1366 {
1367 string_char_and_length (p, rest, &len);
1368 p += len, rest -= len;
1369 xassert (rest >= 0);
1370 CHARPOS (pos) += 1;
1371 BYTEPOS (pos) += len;
1372 }
1373 }
1374 else
1375 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1376
1377 return pos;
1378 }
1379
1380
1381 /* Value is the text position, i.e. character and byte position,
1382 for character position CHARPOS in STRING. */
1383
1384 static INLINE struct text_pos
1385 string_pos (charpos, string)
1386 int charpos;
1387 Lisp_Object string;
1388 {
1389 struct text_pos pos;
1390 xassert (STRINGP (string));
1391 xassert (charpos >= 0);
1392 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1393 return pos;
1394 }
1395
1396
1397 /* Value is a text position, i.e. character and byte position, for
1398 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1399 means recognize multibyte characters. */
1400
1401 static struct text_pos
1402 c_string_pos (charpos, s, multibyte_p)
1403 int charpos;
1404 unsigned char *s;
1405 int multibyte_p;
1406 {
1407 struct text_pos pos;
1408
1409 xassert (s != NULL);
1410 xassert (charpos >= 0);
1411
1412 if (multibyte_p)
1413 {
1414 int rest = strlen (s), len;
1415
1416 SET_TEXT_POS (pos, 0, 0);
1417 while (charpos--)
1418 {
1419 string_char_and_length (s, rest, &len);
1420 s += len, rest -= len;
1421 xassert (rest >= 0);
1422 CHARPOS (pos) += 1;
1423 BYTEPOS (pos) += len;
1424 }
1425 }
1426 else
1427 SET_TEXT_POS (pos, charpos, charpos);
1428
1429 return pos;
1430 }
1431
1432
1433 /* Value is the number of characters in C string S. MULTIBYTE_P
1434 non-zero means recognize multibyte characters. */
1435
1436 static int
1437 number_of_chars (s, multibyte_p)
1438 unsigned char *s;
1439 int multibyte_p;
1440 {
1441 int nchars;
1442
1443 if (multibyte_p)
1444 {
1445 int rest = strlen (s), len;
1446 unsigned char *p = (unsigned char *) s;
1447
1448 for (nchars = 0; rest > 0; ++nchars)
1449 {
1450 string_char_and_length (p, rest, &len);
1451 rest -= len, p += len;
1452 }
1453 }
1454 else
1455 nchars = strlen (s);
1456
1457 return nchars;
1458 }
1459
1460
1461 /* Compute byte position NEWPOS->bytepos corresponding to
1462 NEWPOS->charpos. POS is a known position in string STRING.
1463 NEWPOS->charpos must be >= POS.charpos. */
1464
1465 static void
1466 compute_string_pos (newpos, pos, string)
1467 struct text_pos *newpos, pos;
1468 Lisp_Object string;
1469 {
1470 xassert (STRINGP (string));
1471 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1472
1473 if (STRING_MULTIBYTE (string))
1474 *newpos = string_pos_nchars_ahead (pos, string,
1475 CHARPOS (*newpos) - CHARPOS (pos));
1476 else
1477 BYTEPOS (*newpos) = CHARPOS (*newpos);
1478 }
1479
1480 /* EXPORT:
1481 Return an estimation of the pixel height of mode or top lines on
1482 frame F. FACE_ID specifies what line's height to estimate. */
1483
1484 int
1485 estimate_mode_line_height (f, face_id)
1486 struct frame *f;
1487 enum face_id face_id;
1488 {
1489 #ifdef HAVE_WINDOW_SYSTEM
1490 if (FRAME_WINDOW_P (f))
1491 {
1492 int height = FONT_HEIGHT (FRAME_FONT (f));
1493
1494 /* This function is called so early when Emacs starts that the face
1495 cache and mode line face are not yet initialized. */
1496 if (FRAME_FACE_CACHE (f))
1497 {
1498 struct face *face = FACE_FROM_ID (f, face_id);
1499 if (face)
1500 {
1501 if (face->font)
1502 height = FONT_HEIGHT (face->font);
1503 if (face->box_line_width > 0)
1504 height += 2 * face->box_line_width;
1505 }
1506 }
1507
1508 return height;
1509 }
1510 #endif
1511
1512 return 1;
1513 }
1514
1515 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1516 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1517 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1518 not force the value into range. */
1519
1520 void
1521 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1522 FRAME_PTR f;
1523 register int pix_x, pix_y;
1524 int *x, *y;
1525 NativeRectangle *bounds;
1526 int noclip;
1527 {
1528
1529 #ifdef HAVE_WINDOW_SYSTEM
1530 if (FRAME_WINDOW_P (f))
1531 {
1532 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1533 even for negative values. */
1534 if (pix_x < 0)
1535 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1536 if (pix_y < 0)
1537 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1538
1539 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1540 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1541
1542 if (bounds)
1543 STORE_NATIVE_RECT (*bounds,
1544 FRAME_COL_TO_PIXEL_X (f, pix_x),
1545 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1546 FRAME_COLUMN_WIDTH (f) - 1,
1547 FRAME_LINE_HEIGHT (f) - 1);
1548
1549 if (!noclip)
1550 {
1551 if (pix_x < 0)
1552 pix_x = 0;
1553 else if (pix_x > FRAME_TOTAL_COLS (f))
1554 pix_x = FRAME_TOTAL_COLS (f);
1555
1556 if (pix_y < 0)
1557 pix_y = 0;
1558 else if (pix_y > FRAME_LINES (f))
1559 pix_y = FRAME_LINES (f);
1560 }
1561 }
1562 #endif
1563
1564 *x = pix_x;
1565 *y = pix_y;
1566 }
1567
1568
1569 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1570 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1571 can't tell the positions because W's display is not up to date,
1572 return 0. */
1573
1574 int
1575 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1576 struct window *w;
1577 int hpos, vpos;
1578 int *frame_x, *frame_y;
1579 {
1580 #ifdef HAVE_WINDOW_SYSTEM
1581 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1582 {
1583 int success_p;
1584
1585 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1586 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1587
1588 if (display_completed)
1589 {
1590 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1591 struct glyph *glyph = row->glyphs[TEXT_AREA];
1592 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1593
1594 hpos = row->x;
1595 vpos = row->y;
1596 while (glyph < end)
1597 {
1598 hpos += glyph->pixel_width;
1599 ++glyph;
1600 }
1601
1602 /* If first glyph is partially visible, its first visible position is still 0. */
1603 if (hpos < 0)
1604 hpos = 0;
1605
1606 success_p = 1;
1607 }
1608 else
1609 {
1610 hpos = vpos = 0;
1611 success_p = 0;
1612 }
1613
1614 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1615 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1616 return success_p;
1617 }
1618 #endif
1619
1620 *frame_x = hpos;
1621 *frame_y = vpos;
1622 return 1;
1623 }
1624
1625
1626 #ifdef HAVE_WINDOW_SYSTEM
1627
1628 /* Find the glyph under window-relative coordinates X/Y in window W.
1629 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1630 strings. Return in *HPOS and *VPOS the row and column number of
1631 the glyph found. Return in *AREA the glyph area containing X.
1632 Value is a pointer to the glyph found or null if X/Y is not on
1633 text, or we can't tell because W's current matrix is not up to
1634 date. */
1635
1636 static struct glyph *
1637 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1638 struct window *w;
1639 int x, y;
1640 int *hpos, *vpos, *dx, *dy, *area;
1641 {
1642 struct glyph *glyph, *end;
1643 struct glyph_row *row = NULL;
1644 int x0, i;
1645
1646 /* Find row containing Y. Give up if some row is not enabled. */
1647 for (i = 0; i < w->current_matrix->nrows; ++i)
1648 {
1649 row = MATRIX_ROW (w->current_matrix, i);
1650 if (!row->enabled_p)
1651 return NULL;
1652 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1653 break;
1654 }
1655
1656 *vpos = i;
1657 *hpos = 0;
1658
1659 /* Give up if Y is not in the window. */
1660 if (i == w->current_matrix->nrows)
1661 return NULL;
1662
1663 /* Get the glyph area containing X. */
1664 if (w->pseudo_window_p)
1665 {
1666 *area = TEXT_AREA;
1667 x0 = 0;
1668 }
1669 else
1670 {
1671 if (x < window_box_left_offset (w, TEXT_AREA))
1672 {
1673 *area = LEFT_MARGIN_AREA;
1674 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1675 }
1676 else if (x < window_box_right_offset (w, TEXT_AREA))
1677 {
1678 *area = TEXT_AREA;
1679 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1680 }
1681 else
1682 {
1683 *area = RIGHT_MARGIN_AREA;
1684 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1685 }
1686 }
1687
1688 /* Find glyph containing X. */
1689 glyph = row->glyphs[*area];
1690 end = glyph + row->used[*area];
1691 x -= x0;
1692 while (glyph < end && x >= glyph->pixel_width)
1693 {
1694 x -= glyph->pixel_width;
1695 ++glyph;
1696 }
1697
1698 if (glyph == end)
1699 return NULL;
1700
1701 if (dx)
1702 {
1703 *dx = x;
1704 *dy = y - (row->y + row->ascent - glyph->ascent);
1705 }
1706
1707 *hpos = glyph - row->glyphs[*area];
1708 return glyph;
1709 }
1710
1711
1712 /* EXPORT:
1713 Convert frame-relative x/y to coordinates relative to window W.
1714 Takes pseudo-windows into account. */
1715
1716 void
1717 frame_to_window_pixel_xy (w, x, y)
1718 struct window *w;
1719 int *x, *y;
1720 {
1721 if (w->pseudo_window_p)
1722 {
1723 /* A pseudo-window is always full-width, and starts at the
1724 left edge of the frame, plus a frame border. */
1725 struct frame *f = XFRAME (w->frame);
1726 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1727 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1728 }
1729 else
1730 {
1731 *x -= WINDOW_LEFT_EDGE_X (w);
1732 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1733 }
1734 }
1735
1736 /* EXPORT:
1737 Return in *R the clipping rectangle for glyph string S. */
1738
1739 void
1740 get_glyph_string_clip_rect (s, nr)
1741 struct glyph_string *s;
1742 NativeRectangle *nr;
1743 {
1744 XRectangle r;
1745
1746 if (s->row->full_width_p)
1747 {
1748 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1749 r.x = WINDOW_LEFT_EDGE_X (s->w);
1750 r.width = WINDOW_TOTAL_WIDTH (s->w);
1751
1752 /* Unless displaying a mode or menu bar line, which are always
1753 fully visible, clip to the visible part of the row. */
1754 if (s->w->pseudo_window_p)
1755 r.height = s->row->visible_height;
1756 else
1757 r.height = s->height;
1758 }
1759 else
1760 {
1761 /* This is a text line that may be partially visible. */
1762 r.x = window_box_left (s->w, s->area);
1763 r.width = window_box_width (s->w, s->area);
1764 r.height = s->row->visible_height;
1765 }
1766
1767 /* If S draws overlapping rows, it's sufficient to use the top and
1768 bottom of the window for clipping because this glyph string
1769 intentionally draws over other lines. */
1770 if (s->for_overlaps_p)
1771 {
1772 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1773 r.height = window_text_bottom_y (s->w) - r.y;
1774 }
1775 else
1776 {
1777 /* Don't use S->y for clipping because it doesn't take partially
1778 visible lines into account. For example, it can be negative for
1779 partially visible lines at the top of a window. */
1780 if (!s->row->full_width_p
1781 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1782 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1783 else
1784 r.y = max (0, s->row->y);
1785
1786 /* If drawing a tool-bar window, draw it over the internal border
1787 at the top of the window. */
1788 if (s->w == XWINDOW (s->f->tool_bar_window))
1789 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1790 }
1791
1792 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1793
1794 /* If drawing the cursor, don't let glyph draw outside its
1795 advertised boundaries. Cleartype does this under some circumstances. */
1796 if (s->hl == DRAW_CURSOR)
1797 {
1798 struct glyph *glyph = s->first_glyph;
1799 int height;
1800
1801 if (s->x > r.x)
1802 {
1803 r.width -= s->x - r.x;
1804 r.x = s->x;
1805 }
1806 r.width = min (r.width, glyph->pixel_width);
1807
1808 /* Don't draw cursor glyph taller than our actual glyph. */
1809 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1810 if (height < r.height)
1811 {
1812 int max_y = r.y + r.height;
1813 r.y = min (max_y, s->ybase + glyph->descent - height);
1814 r.height = min (max_y - r.y, height);
1815 }
1816 }
1817
1818 #ifdef CONVERT_FROM_XRECT
1819 CONVERT_FROM_XRECT (r, *nr);
1820 #else
1821 *nr = r;
1822 #endif
1823 }
1824
1825 #endif /* HAVE_WINDOW_SYSTEM */
1826
1827 \f
1828 /***********************************************************************
1829 Lisp form evaluation
1830 ***********************************************************************/
1831
1832 /* Error handler for safe_eval and safe_call. */
1833
1834 static Lisp_Object
1835 safe_eval_handler (arg)
1836 Lisp_Object arg;
1837 {
1838 add_to_log ("Error during redisplay: %s", arg, Qnil);
1839 return Qnil;
1840 }
1841
1842
1843 /* Evaluate SEXPR and return the result, or nil if something went
1844 wrong. Prevent redisplay during the evaluation. */
1845
1846 Lisp_Object
1847 safe_eval (sexpr)
1848 Lisp_Object sexpr;
1849 {
1850 Lisp_Object val;
1851
1852 if (inhibit_eval_during_redisplay)
1853 val = Qnil;
1854 else
1855 {
1856 int count = SPECPDL_INDEX ();
1857 struct gcpro gcpro1;
1858
1859 GCPRO1 (sexpr);
1860 specbind (Qinhibit_redisplay, Qt);
1861 /* Use Qt to ensure debugger does not run,
1862 so there is no possibility of wanting to redisplay. */
1863 val = internal_condition_case_1 (Feval, sexpr, Qt,
1864 safe_eval_handler);
1865 UNGCPRO;
1866 val = unbind_to (count, val);
1867 }
1868
1869 return val;
1870 }
1871
1872
1873 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1874 Return the result, or nil if something went wrong. Prevent
1875 redisplay during the evaluation. */
1876
1877 Lisp_Object
1878 safe_call (nargs, args)
1879 int nargs;
1880 Lisp_Object *args;
1881 {
1882 Lisp_Object val;
1883
1884 if (inhibit_eval_during_redisplay)
1885 val = Qnil;
1886 else
1887 {
1888 int count = SPECPDL_INDEX ();
1889 struct gcpro gcpro1;
1890
1891 GCPRO1 (args[0]);
1892 gcpro1.nvars = nargs;
1893 specbind (Qinhibit_redisplay, Qt);
1894 /* Use Qt to ensure debugger does not run,
1895 so there is no possibility of wanting to redisplay. */
1896 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1897 safe_eval_handler);
1898 UNGCPRO;
1899 val = unbind_to (count, val);
1900 }
1901
1902 return val;
1903 }
1904
1905
1906 /* Call function FN with one argument ARG.
1907 Return the result, or nil if something went wrong. */
1908
1909 Lisp_Object
1910 safe_call1 (fn, arg)
1911 Lisp_Object fn, arg;
1912 {
1913 Lisp_Object args[2];
1914 args[0] = fn;
1915 args[1] = arg;
1916 return safe_call (2, args);
1917 }
1918
1919
1920 \f
1921 /***********************************************************************
1922 Debugging
1923 ***********************************************************************/
1924
1925 #if 0
1926
1927 /* Define CHECK_IT to perform sanity checks on iterators.
1928 This is for debugging. It is too slow to do unconditionally. */
1929
1930 static void
1931 check_it (it)
1932 struct it *it;
1933 {
1934 if (it->method == next_element_from_string)
1935 {
1936 xassert (STRINGP (it->string));
1937 xassert (IT_STRING_CHARPOS (*it) >= 0);
1938 }
1939 else
1940 {
1941 xassert (IT_STRING_CHARPOS (*it) < 0);
1942 if (it->method == next_element_from_buffer)
1943 {
1944 /* Check that character and byte positions agree. */
1945 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1946 }
1947 }
1948
1949 if (it->dpvec)
1950 xassert (it->current.dpvec_index >= 0);
1951 else
1952 xassert (it->current.dpvec_index < 0);
1953 }
1954
1955 #define CHECK_IT(IT) check_it ((IT))
1956
1957 #else /* not 0 */
1958
1959 #define CHECK_IT(IT) (void) 0
1960
1961 #endif /* not 0 */
1962
1963
1964 #if GLYPH_DEBUG
1965
1966 /* Check that the window end of window W is what we expect it
1967 to be---the last row in the current matrix displaying text. */
1968
1969 static void
1970 check_window_end (w)
1971 struct window *w;
1972 {
1973 if (!MINI_WINDOW_P (w)
1974 && !NILP (w->window_end_valid))
1975 {
1976 struct glyph_row *row;
1977 xassert ((row = MATRIX_ROW (w->current_matrix,
1978 XFASTINT (w->window_end_vpos)),
1979 !row->enabled_p
1980 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1981 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1982 }
1983 }
1984
1985 #define CHECK_WINDOW_END(W) check_window_end ((W))
1986
1987 #else /* not GLYPH_DEBUG */
1988
1989 #define CHECK_WINDOW_END(W) (void) 0
1990
1991 #endif /* not GLYPH_DEBUG */
1992
1993
1994 \f
1995 /***********************************************************************
1996 Iterator initialization
1997 ***********************************************************************/
1998
1999 /* Initialize IT for displaying current_buffer in window W, starting
2000 at character position CHARPOS. CHARPOS < 0 means that no buffer
2001 position is specified which is useful when the iterator is assigned
2002 a position later. BYTEPOS is the byte position corresponding to
2003 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2004
2005 If ROW is not null, calls to produce_glyphs with IT as parameter
2006 will produce glyphs in that row.
2007
2008 BASE_FACE_ID is the id of a base face to use. It must be one of
2009 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2010 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2011 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2012
2013 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2014 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2015 will be initialized to use the corresponding mode line glyph row of
2016 the desired matrix of W. */
2017
2018 void
2019 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2020 struct it *it;
2021 struct window *w;
2022 int charpos, bytepos;
2023 struct glyph_row *row;
2024 enum face_id base_face_id;
2025 {
2026 int highlight_region_p;
2027
2028 /* Some precondition checks. */
2029 xassert (w != NULL && it != NULL);
2030 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2031 && charpos <= ZV));
2032
2033 /* If face attributes have been changed since the last redisplay,
2034 free realized faces now because they depend on face definitions
2035 that might have changed. Don't free faces while there might be
2036 desired matrices pending which reference these faces. */
2037 if (face_change_count && !inhibit_free_realized_faces)
2038 {
2039 face_change_count = 0;
2040 free_all_realized_faces (Qnil);
2041 }
2042
2043 /* Use one of the mode line rows of W's desired matrix if
2044 appropriate. */
2045 if (row == NULL)
2046 {
2047 if (base_face_id == MODE_LINE_FACE_ID
2048 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2049 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2050 else if (base_face_id == HEADER_LINE_FACE_ID)
2051 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2052 }
2053
2054 /* Clear IT. */
2055 bzero (it, sizeof *it);
2056 it->current.overlay_string_index = -1;
2057 it->current.dpvec_index = -1;
2058 it->base_face_id = base_face_id;
2059 it->string = Qnil;
2060 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2061
2062 /* The window in which we iterate over current_buffer: */
2063 XSETWINDOW (it->window, w);
2064 it->w = w;
2065 it->f = XFRAME (w->frame);
2066
2067 /* Extra space between lines (on window systems only). */
2068 if (base_face_id == DEFAULT_FACE_ID
2069 && FRAME_WINDOW_P (it->f))
2070 {
2071 if (NATNUMP (current_buffer->extra_line_spacing))
2072 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2073 else if (FLOATP (current_buffer->extra_line_spacing))
2074 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2075 * FRAME_LINE_HEIGHT (it->f));
2076 else if (it->f->extra_line_spacing > 0)
2077 it->extra_line_spacing = it->f->extra_line_spacing;
2078 }
2079
2080 /* If realized faces have been removed, e.g. because of face
2081 attribute changes of named faces, recompute them. When running
2082 in batch mode, the face cache of Vterminal_frame is null. If
2083 we happen to get called, make a dummy face cache. */
2084 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2085 init_frame_faces (it->f);
2086 if (FRAME_FACE_CACHE (it->f)->used == 0)
2087 recompute_basic_faces (it->f);
2088
2089 /* Current value of the `slice', `space-width', and 'height' properties. */
2090 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2091 it->space_width = Qnil;
2092 it->font_height = Qnil;
2093 it->override_ascent = -1;
2094
2095 /* Are control characters displayed as `^C'? */
2096 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2097
2098 /* -1 means everything between a CR and the following line end
2099 is invisible. >0 means lines indented more than this value are
2100 invisible. */
2101 it->selective = (INTEGERP (current_buffer->selective_display)
2102 ? XFASTINT (current_buffer->selective_display)
2103 : (!NILP (current_buffer->selective_display)
2104 ? -1 : 0));
2105 it->selective_display_ellipsis_p
2106 = !NILP (current_buffer->selective_display_ellipses);
2107
2108 /* Display table to use. */
2109 it->dp = window_display_table (w);
2110
2111 /* Are multibyte characters enabled in current_buffer? */
2112 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2113
2114 /* Non-zero if we should highlight the region. */
2115 highlight_region_p
2116 = (!NILP (Vtransient_mark_mode)
2117 && !NILP (current_buffer->mark_active)
2118 && XMARKER (current_buffer->mark)->buffer != 0);
2119
2120 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2121 start and end of a visible region in window IT->w. Set both to
2122 -1 to indicate no region. */
2123 if (highlight_region_p
2124 /* Maybe highlight only in selected window. */
2125 && (/* Either show region everywhere. */
2126 highlight_nonselected_windows
2127 /* Or show region in the selected window. */
2128 || w == XWINDOW (selected_window)
2129 /* Or show the region if we are in the mini-buffer and W is
2130 the window the mini-buffer refers to. */
2131 || (MINI_WINDOW_P (XWINDOW (selected_window))
2132 && WINDOWP (minibuf_selected_window)
2133 && w == XWINDOW (minibuf_selected_window))))
2134 {
2135 int charpos = marker_position (current_buffer->mark);
2136 it->region_beg_charpos = min (PT, charpos);
2137 it->region_end_charpos = max (PT, charpos);
2138 }
2139 else
2140 it->region_beg_charpos = it->region_end_charpos = -1;
2141
2142 /* Get the position at which the redisplay_end_trigger hook should
2143 be run, if it is to be run at all. */
2144 if (MARKERP (w->redisplay_end_trigger)
2145 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2146 it->redisplay_end_trigger_charpos
2147 = marker_position (w->redisplay_end_trigger);
2148 else if (INTEGERP (w->redisplay_end_trigger))
2149 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2150
2151 /* Correct bogus values of tab_width. */
2152 it->tab_width = XINT (current_buffer->tab_width);
2153 if (it->tab_width <= 0 || it->tab_width > 1000)
2154 it->tab_width = 8;
2155
2156 /* Are lines in the display truncated? */
2157 it->truncate_lines_p
2158 = (base_face_id != DEFAULT_FACE_ID
2159 || XINT (it->w->hscroll)
2160 || (truncate_partial_width_windows
2161 && !WINDOW_FULL_WIDTH_P (it->w))
2162 || !NILP (current_buffer->truncate_lines));
2163
2164 /* Get dimensions of truncation and continuation glyphs. These are
2165 displayed as fringe bitmaps under X, so we don't need them for such
2166 frames. */
2167 if (!FRAME_WINDOW_P (it->f))
2168 {
2169 if (it->truncate_lines_p)
2170 {
2171 /* We will need the truncation glyph. */
2172 xassert (it->glyph_row == NULL);
2173 produce_special_glyphs (it, IT_TRUNCATION);
2174 it->truncation_pixel_width = it->pixel_width;
2175 }
2176 else
2177 {
2178 /* We will need the continuation glyph. */
2179 xassert (it->glyph_row == NULL);
2180 produce_special_glyphs (it, IT_CONTINUATION);
2181 it->continuation_pixel_width = it->pixel_width;
2182 }
2183
2184 /* Reset these values to zero because the produce_special_glyphs
2185 above has changed them. */
2186 it->pixel_width = it->ascent = it->descent = 0;
2187 it->phys_ascent = it->phys_descent = 0;
2188 }
2189
2190 /* Set this after getting the dimensions of truncation and
2191 continuation glyphs, so that we don't produce glyphs when calling
2192 produce_special_glyphs, above. */
2193 it->glyph_row = row;
2194 it->area = TEXT_AREA;
2195
2196 /* Get the dimensions of the display area. The display area
2197 consists of the visible window area plus a horizontally scrolled
2198 part to the left of the window. All x-values are relative to the
2199 start of this total display area. */
2200 if (base_face_id != DEFAULT_FACE_ID)
2201 {
2202 /* Mode lines, menu bar in terminal frames. */
2203 it->first_visible_x = 0;
2204 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2205 }
2206 else
2207 {
2208 it->first_visible_x
2209 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2210 it->last_visible_x = (it->first_visible_x
2211 + window_box_width (w, TEXT_AREA));
2212
2213 /* If we truncate lines, leave room for the truncator glyph(s) at
2214 the right margin. Otherwise, leave room for the continuation
2215 glyph(s). Truncation and continuation glyphs are not inserted
2216 for window-based redisplay. */
2217 if (!FRAME_WINDOW_P (it->f))
2218 {
2219 if (it->truncate_lines_p)
2220 it->last_visible_x -= it->truncation_pixel_width;
2221 else
2222 it->last_visible_x -= it->continuation_pixel_width;
2223 }
2224
2225 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2226 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2227 }
2228
2229 /* Leave room for a border glyph. */
2230 if (!FRAME_WINDOW_P (it->f)
2231 && !WINDOW_RIGHTMOST_P (it->w))
2232 it->last_visible_x -= 1;
2233
2234 it->last_visible_y = window_text_bottom_y (w);
2235
2236 /* For mode lines and alike, arrange for the first glyph having a
2237 left box line if the face specifies a box. */
2238 if (base_face_id != DEFAULT_FACE_ID)
2239 {
2240 struct face *face;
2241
2242 it->face_id = base_face_id;
2243
2244 /* If we have a boxed mode line, make the first character appear
2245 with a left box line. */
2246 face = FACE_FROM_ID (it->f, base_face_id);
2247 if (face->box != FACE_NO_BOX)
2248 it->start_of_box_run_p = 1;
2249 }
2250
2251 /* If a buffer position was specified, set the iterator there,
2252 getting overlays and face properties from that position. */
2253 if (charpos >= BUF_BEG (current_buffer))
2254 {
2255 it->end_charpos = ZV;
2256 it->face_id = -1;
2257 IT_CHARPOS (*it) = charpos;
2258
2259 /* Compute byte position if not specified. */
2260 if (bytepos < charpos)
2261 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2262 else
2263 IT_BYTEPOS (*it) = bytepos;
2264
2265 it->start = it->current;
2266
2267 /* Compute faces etc. */
2268 reseat (it, it->current.pos, 1);
2269 }
2270
2271 CHECK_IT (it);
2272 }
2273
2274
2275 /* Initialize IT for the display of window W with window start POS. */
2276
2277 void
2278 start_display (it, w, pos)
2279 struct it *it;
2280 struct window *w;
2281 struct text_pos pos;
2282 {
2283 struct glyph_row *row;
2284 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2285
2286 row = w->desired_matrix->rows + first_vpos;
2287 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2288 it->first_vpos = first_vpos;
2289
2290 if (!it->truncate_lines_p)
2291 {
2292 int start_at_line_beg_p;
2293 int first_y = it->current_y;
2294
2295 /* If window start is not at a line start, skip forward to POS to
2296 get the correct continuation lines width. */
2297 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2298 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2299 if (!start_at_line_beg_p)
2300 {
2301 int new_x;
2302
2303 reseat_at_previous_visible_line_start (it);
2304 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2305
2306 new_x = it->current_x + it->pixel_width;
2307
2308 /* If lines are continued, this line may end in the middle
2309 of a multi-glyph character (e.g. a control character
2310 displayed as \003, or in the middle of an overlay
2311 string). In this case move_it_to above will not have
2312 taken us to the start of the continuation line but to the
2313 end of the continued line. */
2314 if (it->current_x > 0
2315 && !it->truncate_lines_p /* Lines are continued. */
2316 && (/* And glyph doesn't fit on the line. */
2317 new_x > it->last_visible_x
2318 /* Or it fits exactly and we're on a window
2319 system frame. */
2320 || (new_x == it->last_visible_x
2321 && FRAME_WINDOW_P (it->f))))
2322 {
2323 if (it->current.dpvec_index >= 0
2324 || it->current.overlay_string_index >= 0)
2325 {
2326 set_iterator_to_next (it, 1);
2327 move_it_in_display_line_to (it, -1, -1, 0);
2328 }
2329
2330 it->continuation_lines_width += it->current_x;
2331 }
2332
2333 /* We're starting a new display line, not affected by the
2334 height of the continued line, so clear the appropriate
2335 fields in the iterator structure. */
2336 it->max_ascent = it->max_descent = 0;
2337 it->max_phys_ascent = it->max_phys_descent = 0;
2338
2339 it->current_y = first_y;
2340 it->vpos = 0;
2341 it->current_x = it->hpos = 0;
2342 }
2343 }
2344
2345 #if 0 /* Don't assert the following because start_display is sometimes
2346 called intentionally with a window start that is not at a
2347 line start. Please leave this code in as a comment. */
2348
2349 /* Window start should be on a line start, now. */
2350 xassert (it->continuation_lines_width
2351 || IT_CHARPOS (it) == BEGV
2352 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2353 #endif /* 0 */
2354 }
2355
2356
2357 /* Return 1 if POS is a position in ellipses displayed for invisible
2358 text. W is the window we display, for text property lookup. */
2359
2360 static int
2361 in_ellipses_for_invisible_text_p (pos, w)
2362 struct display_pos *pos;
2363 struct window *w;
2364 {
2365 Lisp_Object prop, window;
2366 int ellipses_p = 0;
2367 int charpos = CHARPOS (pos->pos);
2368
2369 /* If POS specifies a position in a display vector, this might
2370 be for an ellipsis displayed for invisible text. We won't
2371 get the iterator set up for delivering that ellipsis unless
2372 we make sure that it gets aware of the invisible text. */
2373 if (pos->dpvec_index >= 0
2374 && pos->overlay_string_index < 0
2375 && CHARPOS (pos->string_pos) < 0
2376 && charpos > BEGV
2377 && (XSETWINDOW (window, w),
2378 prop = Fget_char_property (make_number (charpos),
2379 Qinvisible, window),
2380 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2381 {
2382 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2383 window);
2384 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2385 }
2386
2387 return ellipses_p;
2388 }
2389
2390
2391 /* Initialize IT for stepping through current_buffer in window W,
2392 starting at position POS that includes overlay string and display
2393 vector/ control character translation position information. Value
2394 is zero if there are overlay strings with newlines at POS. */
2395
2396 static int
2397 init_from_display_pos (it, w, pos)
2398 struct it *it;
2399 struct window *w;
2400 struct display_pos *pos;
2401 {
2402 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2403 int i, overlay_strings_with_newlines = 0;
2404
2405 /* If POS specifies a position in a display vector, this might
2406 be for an ellipsis displayed for invisible text. We won't
2407 get the iterator set up for delivering that ellipsis unless
2408 we make sure that it gets aware of the invisible text. */
2409 if (in_ellipses_for_invisible_text_p (pos, w))
2410 {
2411 --charpos;
2412 bytepos = 0;
2413 }
2414
2415 /* Keep in mind: the call to reseat in init_iterator skips invisible
2416 text, so we might end up at a position different from POS. This
2417 is only a problem when POS is a row start after a newline and an
2418 overlay starts there with an after-string, and the overlay has an
2419 invisible property. Since we don't skip invisible text in
2420 display_line and elsewhere immediately after consuming the
2421 newline before the row start, such a POS will not be in a string,
2422 but the call to init_iterator below will move us to the
2423 after-string. */
2424 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2425
2426 for (i = 0; i < it->n_overlay_strings; ++i)
2427 {
2428 const char *s = SDATA (it->overlay_strings[i]);
2429 const char *e = s + SBYTES (it->overlay_strings[i]);
2430
2431 while (s < e && *s != '\n')
2432 ++s;
2433
2434 if (s < e)
2435 {
2436 overlay_strings_with_newlines = 1;
2437 break;
2438 }
2439 }
2440
2441 /* If position is within an overlay string, set up IT to the right
2442 overlay string. */
2443 if (pos->overlay_string_index >= 0)
2444 {
2445 int relative_index;
2446
2447 /* If the first overlay string happens to have a `display'
2448 property for an image, the iterator will be set up for that
2449 image, and we have to undo that setup first before we can
2450 correct the overlay string index. */
2451 if (it->method == next_element_from_image)
2452 pop_it (it);
2453
2454 /* We already have the first chunk of overlay strings in
2455 IT->overlay_strings. Load more until the one for
2456 pos->overlay_string_index is in IT->overlay_strings. */
2457 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2458 {
2459 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2460 it->current.overlay_string_index = 0;
2461 while (n--)
2462 {
2463 load_overlay_strings (it, 0);
2464 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2465 }
2466 }
2467
2468 it->current.overlay_string_index = pos->overlay_string_index;
2469 relative_index = (it->current.overlay_string_index
2470 % OVERLAY_STRING_CHUNK_SIZE);
2471 it->string = it->overlay_strings[relative_index];
2472 xassert (STRINGP (it->string));
2473 it->current.string_pos = pos->string_pos;
2474 it->method = next_element_from_string;
2475 }
2476
2477 #if 0 /* This is bogus because POS not having an overlay string
2478 position does not mean it's after the string. Example: A
2479 line starting with a before-string and initialization of IT
2480 to the previous row's end position. */
2481 else if (it->current.overlay_string_index >= 0)
2482 {
2483 /* If POS says we're already after an overlay string ending at
2484 POS, make sure to pop the iterator because it will be in
2485 front of that overlay string. When POS is ZV, we've thereby
2486 also ``processed'' overlay strings at ZV. */
2487 while (it->sp)
2488 pop_it (it);
2489 it->current.overlay_string_index = -1;
2490 it->method = next_element_from_buffer;
2491 if (CHARPOS (pos->pos) == ZV)
2492 it->overlay_strings_at_end_processed_p = 1;
2493 }
2494 #endif /* 0 */
2495
2496 if (CHARPOS (pos->string_pos) >= 0)
2497 {
2498 /* Recorded position is not in an overlay string, but in another
2499 string. This can only be a string from a `display' property.
2500 IT should already be filled with that string. */
2501 it->current.string_pos = pos->string_pos;
2502 xassert (STRINGP (it->string));
2503 }
2504
2505 /* Restore position in display vector translations, control
2506 character translations or ellipses. */
2507 if (pos->dpvec_index >= 0)
2508 {
2509 if (it->dpvec == NULL)
2510 get_next_display_element (it);
2511 xassert (it->dpvec && it->current.dpvec_index == 0);
2512 it->current.dpvec_index = pos->dpvec_index;
2513 }
2514
2515 CHECK_IT (it);
2516 return !overlay_strings_with_newlines;
2517 }
2518
2519
2520 /* Initialize IT for stepping through current_buffer in window W
2521 starting at ROW->start. */
2522
2523 static void
2524 init_to_row_start (it, w, row)
2525 struct it *it;
2526 struct window *w;
2527 struct glyph_row *row;
2528 {
2529 init_from_display_pos (it, w, &row->start);
2530 it->start = row->start;
2531 it->continuation_lines_width = row->continuation_lines_width;
2532 CHECK_IT (it);
2533 }
2534
2535
2536 /* Initialize IT for stepping through current_buffer in window W
2537 starting in the line following ROW, i.e. starting at ROW->end.
2538 Value is zero if there are overlay strings with newlines at ROW's
2539 end position. */
2540
2541 static int
2542 init_to_row_end (it, w, row)
2543 struct it *it;
2544 struct window *w;
2545 struct glyph_row *row;
2546 {
2547 int success = 0;
2548
2549 if (init_from_display_pos (it, w, &row->end))
2550 {
2551 if (row->continued_p)
2552 it->continuation_lines_width
2553 = row->continuation_lines_width + row->pixel_width;
2554 CHECK_IT (it);
2555 success = 1;
2556 }
2557
2558 return success;
2559 }
2560
2561
2562
2563 \f
2564 /***********************************************************************
2565 Text properties
2566 ***********************************************************************/
2567
2568 /* Called when IT reaches IT->stop_charpos. Handle text property and
2569 overlay changes. Set IT->stop_charpos to the next position where
2570 to stop. */
2571
2572 static void
2573 handle_stop (it)
2574 struct it *it;
2575 {
2576 enum prop_handled handled;
2577 int handle_overlay_change_p = 1;
2578 struct props *p;
2579
2580 it->dpvec = NULL;
2581 it->current.dpvec_index = -1;
2582
2583 do
2584 {
2585 handled = HANDLED_NORMALLY;
2586
2587 /* Call text property handlers. */
2588 for (p = it_props; p->handler; ++p)
2589 {
2590 handled = p->handler (it);
2591
2592 if (handled == HANDLED_RECOMPUTE_PROPS)
2593 break;
2594 else if (handled == HANDLED_RETURN)
2595 return;
2596 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2597 handle_overlay_change_p = 0;
2598 }
2599
2600 if (handled != HANDLED_RECOMPUTE_PROPS)
2601 {
2602 /* Don't check for overlay strings below when set to deliver
2603 characters from a display vector. */
2604 if (it->method == next_element_from_display_vector)
2605 handle_overlay_change_p = 0;
2606
2607 /* Handle overlay changes. */
2608 if (handle_overlay_change_p)
2609 handled = handle_overlay_change (it);
2610
2611 /* Determine where to stop next. */
2612 if (handled == HANDLED_NORMALLY)
2613 compute_stop_pos (it);
2614 }
2615 }
2616 while (handled == HANDLED_RECOMPUTE_PROPS);
2617 }
2618
2619
2620 /* Compute IT->stop_charpos from text property and overlay change
2621 information for IT's current position. */
2622
2623 static void
2624 compute_stop_pos (it)
2625 struct it *it;
2626 {
2627 register INTERVAL iv, next_iv;
2628 Lisp_Object object, limit, position;
2629
2630 /* If nowhere else, stop at the end. */
2631 it->stop_charpos = it->end_charpos;
2632
2633 if (STRINGP (it->string))
2634 {
2635 /* Strings are usually short, so don't limit the search for
2636 properties. */
2637 object = it->string;
2638 limit = Qnil;
2639 position = make_number (IT_STRING_CHARPOS (*it));
2640 }
2641 else
2642 {
2643 int charpos;
2644
2645 /* If next overlay change is in front of the current stop pos
2646 (which is IT->end_charpos), stop there. Note: value of
2647 next_overlay_change is point-max if no overlay change
2648 follows. */
2649 charpos = next_overlay_change (IT_CHARPOS (*it));
2650 if (charpos < it->stop_charpos)
2651 it->stop_charpos = charpos;
2652
2653 /* If showing the region, we have to stop at the region
2654 start or end because the face might change there. */
2655 if (it->region_beg_charpos > 0)
2656 {
2657 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2658 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2659 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2660 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2661 }
2662
2663 /* Set up variables for computing the stop position from text
2664 property changes. */
2665 XSETBUFFER (object, current_buffer);
2666 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2667 position = make_number (IT_CHARPOS (*it));
2668
2669 }
2670
2671 /* Get the interval containing IT's position. Value is a null
2672 interval if there isn't such an interval. */
2673 iv = validate_interval_range (object, &position, &position, 0);
2674 if (!NULL_INTERVAL_P (iv))
2675 {
2676 Lisp_Object values_here[LAST_PROP_IDX];
2677 struct props *p;
2678
2679 /* Get properties here. */
2680 for (p = it_props; p->handler; ++p)
2681 values_here[p->idx] = textget (iv->plist, *p->name);
2682
2683 /* Look for an interval following iv that has different
2684 properties. */
2685 for (next_iv = next_interval (iv);
2686 (!NULL_INTERVAL_P (next_iv)
2687 && (NILP (limit)
2688 || XFASTINT (limit) > next_iv->position));
2689 next_iv = next_interval (next_iv))
2690 {
2691 for (p = it_props; p->handler; ++p)
2692 {
2693 Lisp_Object new_value;
2694
2695 new_value = textget (next_iv->plist, *p->name);
2696 if (!EQ (values_here[p->idx], new_value))
2697 break;
2698 }
2699
2700 if (p->handler)
2701 break;
2702 }
2703
2704 if (!NULL_INTERVAL_P (next_iv))
2705 {
2706 if (INTEGERP (limit)
2707 && next_iv->position >= XFASTINT (limit))
2708 /* No text property change up to limit. */
2709 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2710 else
2711 /* Text properties change in next_iv. */
2712 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2713 }
2714 }
2715
2716 xassert (STRINGP (it->string)
2717 || (it->stop_charpos >= BEGV
2718 && it->stop_charpos >= IT_CHARPOS (*it)));
2719 }
2720
2721
2722 /* Return the position of the next overlay change after POS in
2723 current_buffer. Value is point-max if no overlay change
2724 follows. This is like `next-overlay-change' but doesn't use
2725 xmalloc. */
2726
2727 static int
2728 next_overlay_change (pos)
2729 int pos;
2730 {
2731 int noverlays;
2732 int endpos;
2733 Lisp_Object *overlays;
2734 int i;
2735
2736 /* Get all overlays at the given position. */
2737 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
2738
2739 /* If any of these overlays ends before endpos,
2740 use its ending point instead. */
2741 for (i = 0; i < noverlays; ++i)
2742 {
2743 Lisp_Object oend;
2744 int oendpos;
2745
2746 oend = OVERLAY_END (overlays[i]);
2747 oendpos = OVERLAY_POSITION (oend);
2748 endpos = min (endpos, oendpos);
2749 }
2750
2751 return endpos;
2752 }
2753
2754
2755 \f
2756 /***********************************************************************
2757 Fontification
2758 ***********************************************************************/
2759
2760 /* Handle changes in the `fontified' property of the current buffer by
2761 calling hook functions from Qfontification_functions to fontify
2762 regions of text. */
2763
2764 static enum prop_handled
2765 handle_fontified_prop (it)
2766 struct it *it;
2767 {
2768 Lisp_Object prop, pos;
2769 enum prop_handled handled = HANDLED_NORMALLY;
2770
2771 /* Get the value of the `fontified' property at IT's current buffer
2772 position. (The `fontified' property doesn't have a special
2773 meaning in strings.) If the value is nil, call functions from
2774 Qfontification_functions. */
2775 if (!STRINGP (it->string)
2776 && it->s == NULL
2777 && !NILP (Vfontification_functions)
2778 && !NILP (Vrun_hooks)
2779 && (pos = make_number (IT_CHARPOS (*it)),
2780 prop = Fget_char_property (pos, Qfontified, Qnil),
2781 NILP (prop)))
2782 {
2783 int count = SPECPDL_INDEX ();
2784 Lisp_Object val;
2785
2786 val = Vfontification_functions;
2787 specbind (Qfontification_functions, Qnil);
2788
2789 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2790 safe_call1 (val, pos);
2791 else
2792 {
2793 Lisp_Object globals, fn;
2794 struct gcpro gcpro1, gcpro2;
2795
2796 globals = Qnil;
2797 GCPRO2 (val, globals);
2798
2799 for (; CONSP (val); val = XCDR (val))
2800 {
2801 fn = XCAR (val);
2802
2803 if (EQ (fn, Qt))
2804 {
2805 /* A value of t indicates this hook has a local
2806 binding; it means to run the global binding too.
2807 In a global value, t should not occur. If it
2808 does, we must ignore it to avoid an endless
2809 loop. */
2810 for (globals = Fdefault_value (Qfontification_functions);
2811 CONSP (globals);
2812 globals = XCDR (globals))
2813 {
2814 fn = XCAR (globals);
2815 if (!EQ (fn, Qt))
2816 safe_call1 (fn, pos);
2817 }
2818 }
2819 else
2820 safe_call1 (fn, pos);
2821 }
2822
2823 UNGCPRO;
2824 }
2825
2826 unbind_to (count, Qnil);
2827
2828 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2829 something. This avoids an endless loop if they failed to
2830 fontify the text for which reason ever. */
2831 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2832 handled = HANDLED_RECOMPUTE_PROPS;
2833 }
2834
2835 return handled;
2836 }
2837
2838
2839 \f
2840 /***********************************************************************
2841 Faces
2842 ***********************************************************************/
2843
2844 /* Set up iterator IT from face properties at its current position.
2845 Called from handle_stop. */
2846
2847 static enum prop_handled
2848 handle_face_prop (it)
2849 struct it *it;
2850 {
2851 int new_face_id, next_stop;
2852
2853 if (!STRINGP (it->string))
2854 {
2855 new_face_id
2856 = face_at_buffer_position (it->w,
2857 IT_CHARPOS (*it),
2858 it->region_beg_charpos,
2859 it->region_end_charpos,
2860 &next_stop,
2861 (IT_CHARPOS (*it)
2862 + TEXT_PROP_DISTANCE_LIMIT),
2863 0);
2864
2865 /* Is this a start of a run of characters with box face?
2866 Caveat: this can be called for a freshly initialized
2867 iterator; face_id is -1 in this case. We know that the new
2868 face will not change until limit, i.e. if the new face has a
2869 box, all characters up to limit will have one. But, as
2870 usual, we don't know whether limit is really the end. */
2871 if (new_face_id != it->face_id)
2872 {
2873 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2874
2875 /* If new face has a box but old face has not, this is
2876 the start of a run of characters with box, i.e. it has
2877 a shadow on the left side. The value of face_id of the
2878 iterator will be -1 if this is the initial call that gets
2879 the face. In this case, we have to look in front of IT's
2880 position and see whether there is a face != new_face_id. */
2881 it->start_of_box_run_p
2882 = (new_face->box != FACE_NO_BOX
2883 && (it->face_id >= 0
2884 || IT_CHARPOS (*it) == BEG
2885 || new_face_id != face_before_it_pos (it)));
2886 it->face_box_p = new_face->box != FACE_NO_BOX;
2887 }
2888 }
2889 else
2890 {
2891 int base_face_id, bufpos;
2892
2893 if (it->current.overlay_string_index >= 0)
2894 bufpos = IT_CHARPOS (*it);
2895 else
2896 bufpos = 0;
2897
2898 /* For strings from a buffer, i.e. overlay strings or strings
2899 from a `display' property, use the face at IT's current
2900 buffer position as the base face to merge with, so that
2901 overlay strings appear in the same face as surrounding
2902 text, unless they specify their own faces. */
2903 base_face_id = underlying_face_id (it);
2904
2905 new_face_id = face_at_string_position (it->w,
2906 it->string,
2907 IT_STRING_CHARPOS (*it),
2908 bufpos,
2909 it->region_beg_charpos,
2910 it->region_end_charpos,
2911 &next_stop,
2912 base_face_id, 0);
2913
2914 #if 0 /* This shouldn't be neccessary. Let's check it. */
2915 /* If IT is used to display a mode line we would really like to
2916 use the mode line face instead of the frame's default face. */
2917 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2918 && new_face_id == DEFAULT_FACE_ID)
2919 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2920 #endif
2921
2922 /* Is this a start of a run of characters with box? Caveat:
2923 this can be called for a freshly allocated iterator; face_id
2924 is -1 is this case. We know that the new face will not
2925 change until the next check pos, i.e. if the new face has a
2926 box, all characters up to that position will have a
2927 box. But, as usual, we don't know whether that position
2928 is really the end. */
2929 if (new_face_id != it->face_id)
2930 {
2931 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2932 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2933
2934 /* If new face has a box but old face hasn't, this is the
2935 start of a run of characters with box, i.e. it has a
2936 shadow on the left side. */
2937 it->start_of_box_run_p
2938 = new_face->box && (old_face == NULL || !old_face->box);
2939 it->face_box_p = new_face->box != FACE_NO_BOX;
2940 }
2941 }
2942
2943 it->face_id = new_face_id;
2944 return HANDLED_NORMALLY;
2945 }
2946
2947
2948 /* Return the ID of the face ``underlying'' IT's current position,
2949 which is in a string. If the iterator is associated with a
2950 buffer, return the face at IT's current buffer position.
2951 Otherwise, use the iterator's base_face_id. */
2952
2953 static int
2954 underlying_face_id (it)
2955 struct it *it;
2956 {
2957 int face_id = it->base_face_id, i;
2958
2959 xassert (STRINGP (it->string));
2960
2961 for (i = it->sp - 1; i >= 0; --i)
2962 if (NILP (it->stack[i].string))
2963 face_id = it->stack[i].face_id;
2964
2965 return face_id;
2966 }
2967
2968
2969 /* Compute the face one character before or after the current position
2970 of IT. BEFORE_P non-zero means get the face in front of IT's
2971 position. Value is the id of the face. */
2972
2973 static int
2974 face_before_or_after_it_pos (it, before_p)
2975 struct it *it;
2976 int before_p;
2977 {
2978 int face_id, limit;
2979 int next_check_charpos;
2980 struct text_pos pos;
2981
2982 xassert (it->s == NULL);
2983
2984 if (STRINGP (it->string))
2985 {
2986 int bufpos, base_face_id;
2987
2988 /* No face change past the end of the string (for the case
2989 we are padding with spaces). No face change before the
2990 string start. */
2991 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
2992 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2993 return it->face_id;
2994
2995 /* Set pos to the position before or after IT's current position. */
2996 if (before_p)
2997 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2998 else
2999 /* For composition, we must check the character after the
3000 composition. */
3001 pos = (it->what == IT_COMPOSITION
3002 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3003 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3004
3005 if (it->current.overlay_string_index >= 0)
3006 bufpos = IT_CHARPOS (*it);
3007 else
3008 bufpos = 0;
3009
3010 base_face_id = underlying_face_id (it);
3011
3012 /* Get the face for ASCII, or unibyte. */
3013 face_id = face_at_string_position (it->w,
3014 it->string,
3015 CHARPOS (pos),
3016 bufpos,
3017 it->region_beg_charpos,
3018 it->region_end_charpos,
3019 &next_check_charpos,
3020 base_face_id, 0);
3021
3022 /* Correct the face for charsets different from ASCII. Do it
3023 for the multibyte case only. The face returned above is
3024 suitable for unibyte text if IT->string is unibyte. */
3025 if (STRING_MULTIBYTE (it->string))
3026 {
3027 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3028 int rest = SBYTES (it->string) - BYTEPOS (pos);
3029 int c, len;
3030 struct face *face = FACE_FROM_ID (it->f, face_id);
3031
3032 c = string_char_and_length (p, rest, &len);
3033 face_id = FACE_FOR_CHAR (it->f, face, c);
3034 }
3035 }
3036 else
3037 {
3038 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3039 || (IT_CHARPOS (*it) <= BEGV && before_p))
3040 return it->face_id;
3041
3042 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3043 pos = it->current.pos;
3044
3045 if (before_p)
3046 DEC_TEXT_POS (pos, it->multibyte_p);
3047 else
3048 {
3049 if (it->what == IT_COMPOSITION)
3050 /* For composition, we must check the position after the
3051 composition. */
3052 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3053 else
3054 INC_TEXT_POS (pos, it->multibyte_p);
3055 }
3056
3057 /* Determine face for CHARSET_ASCII, or unibyte. */
3058 face_id = face_at_buffer_position (it->w,
3059 CHARPOS (pos),
3060 it->region_beg_charpos,
3061 it->region_end_charpos,
3062 &next_check_charpos,
3063 limit, 0);
3064
3065 /* Correct the face for charsets different from ASCII. Do it
3066 for the multibyte case only. The face returned above is
3067 suitable for unibyte text if current_buffer is unibyte. */
3068 if (it->multibyte_p)
3069 {
3070 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3071 struct face *face = FACE_FROM_ID (it->f, face_id);
3072 face_id = FACE_FOR_CHAR (it->f, face, c);
3073 }
3074 }
3075
3076 return face_id;
3077 }
3078
3079
3080 \f
3081 /***********************************************************************
3082 Invisible text
3083 ***********************************************************************/
3084
3085 /* Set up iterator IT from invisible properties at its current
3086 position. Called from handle_stop. */
3087
3088 static enum prop_handled
3089 handle_invisible_prop (it)
3090 struct it *it;
3091 {
3092 enum prop_handled handled = HANDLED_NORMALLY;
3093
3094 if (STRINGP (it->string))
3095 {
3096 extern Lisp_Object Qinvisible;
3097 Lisp_Object prop, end_charpos, limit, charpos;
3098
3099 /* Get the value of the invisible text property at the
3100 current position. Value will be nil if there is no such
3101 property. */
3102 charpos = make_number (IT_STRING_CHARPOS (*it));
3103 prop = Fget_text_property (charpos, Qinvisible, it->string);
3104
3105 if (!NILP (prop)
3106 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3107 {
3108 handled = HANDLED_RECOMPUTE_PROPS;
3109
3110 /* Get the position at which the next change of the
3111 invisible text property can be found in IT->string.
3112 Value will be nil if the property value is the same for
3113 all the rest of IT->string. */
3114 XSETINT (limit, SCHARS (it->string));
3115 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3116 it->string, limit);
3117
3118 /* Text at current position is invisible. The next
3119 change in the property is at position end_charpos.
3120 Move IT's current position to that position. */
3121 if (INTEGERP (end_charpos)
3122 && XFASTINT (end_charpos) < XFASTINT (limit))
3123 {
3124 struct text_pos old;
3125 old = it->current.string_pos;
3126 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3127 compute_string_pos (&it->current.string_pos, old, it->string);
3128 }
3129 else
3130 {
3131 /* The rest of the string is invisible. If this is an
3132 overlay string, proceed with the next overlay string
3133 or whatever comes and return a character from there. */
3134 if (it->current.overlay_string_index >= 0)
3135 {
3136 next_overlay_string (it);
3137 /* Don't check for overlay strings when we just
3138 finished processing them. */
3139 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3140 }
3141 else
3142 {
3143 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3144 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3145 }
3146 }
3147 }
3148 }
3149 else
3150 {
3151 int invis_p, newpos, next_stop, start_charpos;
3152 Lisp_Object pos, prop, overlay;
3153
3154 /* First of all, is there invisible text at this position? */
3155 start_charpos = IT_CHARPOS (*it);
3156 pos = make_number (IT_CHARPOS (*it));
3157 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3158 &overlay);
3159 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3160
3161 /* If we are on invisible text, skip over it. */
3162 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3163 {
3164 /* Record whether we have to display an ellipsis for the
3165 invisible text. */
3166 int display_ellipsis_p = invis_p == 2;
3167
3168 handled = HANDLED_RECOMPUTE_PROPS;
3169
3170 /* Loop skipping over invisible text. The loop is left at
3171 ZV or with IT on the first char being visible again. */
3172 do
3173 {
3174 /* Try to skip some invisible text. Return value is the
3175 position reached which can be equal to IT's position
3176 if there is nothing invisible here. This skips both
3177 over invisible text properties and overlays with
3178 invisible property. */
3179 newpos = skip_invisible (IT_CHARPOS (*it),
3180 &next_stop, ZV, it->window);
3181
3182 /* If we skipped nothing at all we weren't at invisible
3183 text in the first place. If everything to the end of
3184 the buffer was skipped, end the loop. */
3185 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3186 invis_p = 0;
3187 else
3188 {
3189 /* We skipped some characters but not necessarily
3190 all there are. Check if we ended up on visible
3191 text. Fget_char_property returns the property of
3192 the char before the given position, i.e. if we
3193 get invis_p = 0, this means that the char at
3194 newpos is visible. */
3195 pos = make_number (newpos);
3196 prop = Fget_char_property (pos, Qinvisible, it->window);
3197 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3198 }
3199
3200 /* If we ended up on invisible text, proceed to
3201 skip starting with next_stop. */
3202 if (invis_p)
3203 IT_CHARPOS (*it) = next_stop;
3204 }
3205 while (invis_p);
3206
3207 /* The position newpos is now either ZV or on visible text. */
3208 IT_CHARPOS (*it) = newpos;
3209 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3210
3211 /* If there are before-strings at the start of invisible
3212 text, and the text is invisible because of a text
3213 property, arrange to show before-strings because 20.x did
3214 it that way. (If the text is invisible because of an
3215 overlay property instead of a text property, this is
3216 already handled in the overlay code.) */
3217 if (NILP (overlay)
3218 && get_overlay_strings (it, start_charpos))
3219 {
3220 handled = HANDLED_RECOMPUTE_PROPS;
3221 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3222 }
3223 else if (display_ellipsis_p)
3224 setup_for_ellipsis (it);
3225 }
3226 }
3227
3228 return handled;
3229 }
3230
3231
3232 /* Make iterator IT return `...' next. */
3233
3234 static void
3235 setup_for_ellipsis (it)
3236 struct it *it;
3237 {
3238 if (it->dp
3239 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3240 {
3241 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3242 it->dpvec = v->contents;
3243 it->dpend = v->contents + v->size;
3244 }
3245 else
3246 {
3247 /* Default `...'. */
3248 it->dpvec = default_invis_vector;
3249 it->dpend = default_invis_vector + 3;
3250 }
3251
3252 /* The ellipsis display does not replace the display of the
3253 character at the new position. Indicate this by setting
3254 IT->dpvec_char_len to zero. */
3255 it->dpvec_char_len = 0;
3256
3257 it->current.dpvec_index = 0;
3258 it->method = next_element_from_display_vector;
3259 }
3260
3261
3262 \f
3263 /***********************************************************************
3264 'display' property
3265 ***********************************************************************/
3266
3267 /* Set up iterator IT from `display' property at its current position.
3268 Called from handle_stop. */
3269
3270 static enum prop_handled
3271 handle_display_prop (it)
3272 struct it *it;
3273 {
3274 Lisp_Object prop, object;
3275 struct text_pos *position;
3276 int display_replaced_p = 0;
3277
3278 if (STRINGP (it->string))
3279 {
3280 object = it->string;
3281 position = &it->current.string_pos;
3282 }
3283 else
3284 {
3285 object = it->w->buffer;
3286 position = &it->current.pos;
3287 }
3288
3289 /* Reset those iterator values set from display property values. */
3290 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3291 it->space_width = Qnil;
3292 it->font_height = Qnil;
3293 it->voffset = 0;
3294
3295 /* We don't support recursive `display' properties, i.e. string
3296 values that have a string `display' property, that have a string
3297 `display' property etc. */
3298 if (!it->string_from_display_prop_p)
3299 it->area = TEXT_AREA;
3300
3301 prop = Fget_char_property (make_number (position->charpos),
3302 Qdisplay, object);
3303 if (NILP (prop))
3304 return HANDLED_NORMALLY;
3305
3306 if (CONSP (prop)
3307 /* Simple properties. */
3308 && !EQ (XCAR (prop), Qimage)
3309 && !EQ (XCAR (prop), Qspace)
3310 && !EQ (XCAR (prop), Qwhen)
3311 && !EQ (XCAR (prop), Qslice)
3312 && !EQ (XCAR (prop), Qspace_width)
3313 && !EQ (XCAR (prop), Qheight)
3314 && !EQ (XCAR (prop), Qraise)
3315 /* Marginal area specifications. */
3316 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3317 && !EQ (XCAR (prop), Qleft_fringe)
3318 && !EQ (XCAR (prop), Qright_fringe)
3319 && !NILP (XCAR (prop)))
3320 {
3321 for (; CONSP (prop); prop = XCDR (prop))
3322 {
3323 if (handle_single_display_prop (it, XCAR (prop), object,
3324 position, display_replaced_p))
3325 display_replaced_p = 1;
3326 }
3327 }
3328 else if (VECTORP (prop))
3329 {
3330 int i;
3331 for (i = 0; i < ASIZE (prop); ++i)
3332 if (handle_single_display_prop (it, AREF (prop, i), object,
3333 position, display_replaced_p))
3334 display_replaced_p = 1;
3335 }
3336 else
3337 {
3338 if (handle_single_display_prop (it, prop, object, position, 0))
3339 display_replaced_p = 1;
3340 }
3341
3342 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3343 }
3344
3345
3346 /* Value is the position of the end of the `display' property starting
3347 at START_POS in OBJECT. */
3348
3349 static struct text_pos
3350 display_prop_end (it, object, start_pos)
3351 struct it *it;
3352 Lisp_Object object;
3353 struct text_pos start_pos;
3354 {
3355 Lisp_Object end;
3356 struct text_pos end_pos;
3357
3358 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3359 Qdisplay, object, Qnil);
3360 CHARPOS (end_pos) = XFASTINT (end);
3361 if (STRINGP (object))
3362 compute_string_pos (&end_pos, start_pos, it->string);
3363 else
3364 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3365
3366 return end_pos;
3367 }
3368
3369
3370 /* Set up IT from a single `display' sub-property value PROP. OBJECT
3371 is the object in which the `display' property was found. *POSITION
3372 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3373 means that we previously saw a display sub-property which already
3374 replaced text display with something else, for example an image;
3375 ignore such properties after the first one has been processed.
3376
3377 If PROP is a `space' or `image' sub-property, set *POSITION to the
3378 end position of the `display' property.
3379
3380 Value is non-zero if something was found which replaces the display
3381 of buffer or string text. */
3382
3383 static int
3384 handle_single_display_prop (it, prop, object, position,
3385 display_replaced_before_p)
3386 struct it *it;
3387 Lisp_Object prop;
3388 Lisp_Object object;
3389 struct text_pos *position;
3390 int display_replaced_before_p;
3391 {
3392 Lisp_Object value;
3393 int replaces_text_display_p = 0;
3394 Lisp_Object form;
3395
3396 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
3397 evaluated. If the result is nil, VALUE is ignored. */
3398 form = Qt;
3399 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3400 {
3401 prop = XCDR (prop);
3402 if (!CONSP (prop))
3403 return 0;
3404 form = XCAR (prop);
3405 prop = XCDR (prop);
3406 }
3407
3408 if (!NILP (form) && !EQ (form, Qt))
3409 {
3410 int count = SPECPDL_INDEX ();
3411 struct gcpro gcpro1;
3412
3413 /* Bind `object' to the object having the `display' property, a
3414 buffer or string. Bind `position' to the position in the
3415 object where the property was found, and `buffer-position'
3416 to the current position in the buffer. */
3417 specbind (Qobject, object);
3418 specbind (Qposition, make_number (CHARPOS (*position)));
3419 specbind (Qbuffer_position,
3420 make_number (STRINGP (object)
3421 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3422 GCPRO1 (form);
3423 form = safe_eval (form);
3424 UNGCPRO;
3425 unbind_to (count, Qnil);
3426 }
3427
3428 if (NILP (form))
3429 return 0;
3430
3431 if (CONSP (prop)
3432 && EQ (XCAR (prop), Qheight)
3433 && CONSP (XCDR (prop)))
3434 {
3435 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3436 return 0;
3437
3438 /* `(height HEIGHT)'. */
3439 it->font_height = XCAR (XCDR (prop));
3440 if (!NILP (it->font_height))
3441 {
3442 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3443 int new_height = -1;
3444
3445 if (CONSP (it->font_height)
3446 && (EQ (XCAR (it->font_height), Qplus)
3447 || EQ (XCAR (it->font_height), Qminus))
3448 && CONSP (XCDR (it->font_height))
3449 && INTEGERP (XCAR (XCDR (it->font_height))))
3450 {
3451 /* `(+ N)' or `(- N)' where N is an integer. */
3452 int steps = XINT (XCAR (XCDR (it->font_height)));
3453 if (EQ (XCAR (it->font_height), Qplus))
3454 steps = - steps;
3455 it->face_id = smaller_face (it->f, it->face_id, steps);
3456 }
3457 else if (FUNCTIONP (it->font_height))
3458 {
3459 /* Call function with current height as argument.
3460 Value is the new height. */
3461 Lisp_Object height;
3462 height = safe_call1 (it->font_height,
3463 face->lface[LFACE_HEIGHT_INDEX]);
3464 if (NUMBERP (height))
3465 new_height = XFLOATINT (height);
3466 }
3467 else if (NUMBERP (it->font_height))
3468 {
3469 /* Value is a multiple of the canonical char height. */
3470 struct face *face;
3471
3472 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3473 new_height = (XFLOATINT (it->font_height)
3474 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3475 }
3476 else
3477 {
3478 /* Evaluate IT->font_height with `height' bound to the
3479 current specified height to get the new height. */
3480 Lisp_Object value;
3481 int count = SPECPDL_INDEX ();
3482
3483 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3484 value = safe_eval (it->font_height);
3485 unbind_to (count, Qnil);
3486
3487 if (NUMBERP (value))
3488 new_height = XFLOATINT (value);
3489 }
3490
3491 if (new_height > 0)
3492 it->face_id = face_with_height (it->f, it->face_id, new_height);
3493 }
3494 }
3495 else if (CONSP (prop)
3496 && EQ (XCAR (prop), Qspace_width)
3497 && CONSP (XCDR (prop)))
3498 {
3499 /* `(space_width WIDTH)'. */
3500 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3501 return 0;
3502
3503 value = XCAR (XCDR (prop));
3504 if (NUMBERP (value) && XFLOATINT (value) > 0)
3505 it->space_width = value;
3506 }
3507 else if (CONSP (prop)
3508 && EQ (XCAR (prop), Qslice))
3509 {
3510 /* `(slice X Y WIDTH HEIGHT)'. */
3511 Lisp_Object tem;
3512
3513 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3514 return 0;
3515
3516 if (tem = XCDR (prop), CONSP (tem))
3517 {
3518 it->slice.x = XCAR (tem);
3519 if (tem = XCDR (tem), CONSP (tem))
3520 {
3521 it->slice.y = XCAR (tem);
3522 if (tem = XCDR (tem), CONSP (tem))
3523 {
3524 it->slice.width = XCAR (tem);
3525 if (tem = XCDR (tem), CONSP (tem))
3526 it->slice.height = XCAR (tem);
3527 }
3528 }
3529 }
3530 }
3531 else if (CONSP (prop)
3532 && EQ (XCAR (prop), Qraise)
3533 && CONSP (XCDR (prop)))
3534 {
3535 /* `(raise FACTOR)'. */
3536 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3537 return 0;
3538
3539 #ifdef HAVE_WINDOW_SYSTEM
3540 value = XCAR (XCDR (prop));
3541 if (NUMBERP (value))
3542 {
3543 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3544 it->voffset = - (XFLOATINT (value)
3545 * (FONT_HEIGHT (face->font)));
3546 }
3547 #endif /* HAVE_WINDOW_SYSTEM */
3548 }
3549 else if (!it->string_from_display_prop_p)
3550 {
3551 /* `((margin left-margin) VALUE)' or `((margin right-margin)
3552 VALUE) or `((margin nil) VALUE)' or VALUE. */
3553 Lisp_Object location, value;
3554 struct text_pos start_pos;
3555 int valid_p;
3556
3557 /* Characters having this form of property are not displayed, so
3558 we have to find the end of the property. */
3559 start_pos = *position;
3560 *position = display_prop_end (it, object, start_pos);
3561 value = Qnil;
3562
3563 /* Let's stop at the new position and assume that all
3564 text properties change there. */
3565 it->stop_charpos = position->charpos;
3566
3567 if (CONSP (prop)
3568 && (EQ (XCAR (prop), Qleft_fringe)
3569 || EQ (XCAR (prop), Qright_fringe))
3570 && CONSP (XCDR (prop)))
3571 {
3572 unsigned face_id = DEFAULT_FACE_ID;
3573 int fringe_bitmap;
3574
3575 /* Save current settings of IT so that we can restore them
3576 when we are finished with the glyph property value. */
3577
3578 /* `(left-fringe BITMAP FACE)'. */
3579 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3580 return 0;
3581
3582 #ifdef HAVE_WINDOW_SYSTEM
3583 value = XCAR (XCDR (prop));
3584 if (!SYMBOLP (value)
3585 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
3586 return 0;
3587
3588 if (CONSP (XCDR (XCDR (prop))))
3589 {
3590 Lisp_Object face_name = XCAR (XCDR (XCDR (prop)));
3591
3592 face_id = lookup_named_face (it->f, face_name, 'A');
3593 if (face_id < 0)
3594 return 0;
3595 }
3596
3597 push_it (it);
3598
3599 it->area = TEXT_AREA;
3600 it->what = IT_IMAGE;
3601 it->image_id = -1; /* no image */
3602 it->position = start_pos;
3603 it->object = NILP (object) ? it->w->buffer : object;
3604 it->method = next_element_from_image;
3605 it->face_id = face_id;
3606
3607 /* Say that we haven't consumed the characters with
3608 `display' property yet. The call to pop_it in
3609 set_iterator_to_next will clean this up. */
3610 *position = start_pos;
3611
3612 if (EQ (XCAR (prop), Qleft_fringe))
3613 {
3614 it->left_user_fringe_bitmap = fringe_bitmap;
3615 it->left_user_fringe_face_id = face_id;
3616 }
3617 else
3618 {
3619 it->right_user_fringe_bitmap = fringe_bitmap;
3620 it->right_user_fringe_face_id = face_id;
3621 }
3622 #endif /* HAVE_WINDOW_SYSTEM */
3623 return 1;
3624 }
3625
3626 location = Qunbound;
3627 if (CONSP (prop) && CONSP (XCAR (prop)))
3628 {
3629 Lisp_Object tem;
3630
3631 value = XCDR (prop);
3632 if (CONSP (value))
3633 value = XCAR (value);
3634
3635 tem = XCAR (prop);
3636 if (EQ (XCAR (tem), Qmargin)
3637 && (tem = XCDR (tem),
3638 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3639 (NILP (tem)
3640 || EQ (tem, Qleft_margin)
3641 || EQ (tem, Qright_margin))))
3642 location = tem;
3643 }
3644
3645 if (EQ (location, Qunbound))
3646 {
3647 location = Qnil;
3648 value = prop;
3649 }
3650
3651 valid_p = (STRINGP (value)
3652 #ifdef HAVE_WINDOW_SYSTEM
3653 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
3654 #endif /* not HAVE_WINDOW_SYSTEM */
3655 || (CONSP (value) && EQ (XCAR (value), Qspace)));
3656
3657 if ((EQ (location, Qleft_margin)
3658 || EQ (location, Qright_margin)
3659 || NILP (location))
3660 && valid_p
3661 && !display_replaced_before_p)
3662 {
3663 replaces_text_display_p = 1;
3664
3665 /* Save current settings of IT so that we can restore them
3666 when we are finished with the glyph property value. */
3667 push_it (it);
3668
3669 if (NILP (location))
3670 it->area = TEXT_AREA;
3671 else if (EQ (location, Qleft_margin))
3672 it->area = LEFT_MARGIN_AREA;
3673 else
3674 it->area = RIGHT_MARGIN_AREA;
3675
3676 if (STRINGP (value))
3677 {
3678 it->string = value;
3679 it->multibyte_p = STRING_MULTIBYTE (it->string);
3680 it->current.overlay_string_index = -1;
3681 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3682 it->end_charpos = it->string_nchars = SCHARS (it->string);
3683 it->method = next_element_from_string;
3684 it->stop_charpos = 0;
3685 it->string_from_display_prop_p = 1;
3686 /* Say that we haven't consumed the characters with
3687 `display' property yet. The call to pop_it in
3688 set_iterator_to_next will clean this up. */
3689 *position = start_pos;
3690 }
3691 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3692 {
3693 it->method = next_element_from_stretch;
3694 it->object = value;
3695 it->current.pos = it->position = start_pos;
3696 }
3697 #ifdef HAVE_WINDOW_SYSTEM
3698 else
3699 {
3700 it->what = IT_IMAGE;
3701 it->image_id = lookup_image (it->f, value);
3702 it->position = start_pos;
3703 it->object = NILP (object) ? it->w->buffer : object;
3704 it->method = next_element_from_image;
3705
3706 /* Say that we haven't consumed the characters with
3707 `display' property yet. The call to pop_it in
3708 set_iterator_to_next will clean this up. */
3709 *position = start_pos;
3710 }
3711 #endif /* HAVE_WINDOW_SYSTEM */
3712 }
3713 else
3714 /* Invalid property or property not supported. Restore
3715 the position to what it was before. */
3716 *position = start_pos;
3717 }
3718
3719 return replaces_text_display_p;
3720 }
3721
3722
3723 /* Check if PROP is a display sub-property value whose text should be
3724 treated as intangible. */
3725
3726 static int
3727 single_display_prop_intangible_p (prop)
3728 Lisp_Object prop;
3729 {
3730 /* Skip over `when FORM'. */
3731 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3732 {
3733 prop = XCDR (prop);
3734 if (!CONSP (prop))
3735 return 0;
3736 prop = XCDR (prop);
3737 }
3738
3739 if (STRINGP (prop))
3740 return 1;
3741
3742 if (!CONSP (prop))
3743 return 0;
3744
3745 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3746 we don't need to treat text as intangible. */
3747 if (EQ (XCAR (prop), Qmargin))
3748 {
3749 prop = XCDR (prop);
3750 if (!CONSP (prop))
3751 return 0;
3752
3753 prop = XCDR (prop);
3754 if (!CONSP (prop)
3755 || EQ (XCAR (prop), Qleft_margin)
3756 || EQ (XCAR (prop), Qright_margin))
3757 return 0;
3758 }
3759
3760 return (CONSP (prop)
3761 && (EQ (XCAR (prop), Qimage)
3762 || EQ (XCAR (prop), Qspace)));
3763 }
3764
3765
3766 /* Check if PROP is a display property value whose text should be
3767 treated as intangible. */
3768
3769 int
3770 display_prop_intangible_p (prop)
3771 Lisp_Object prop;
3772 {
3773 if (CONSP (prop)
3774 && CONSP (XCAR (prop))
3775 && !EQ (Qmargin, XCAR (XCAR (prop))))
3776 {
3777 /* A list of sub-properties. */
3778 while (CONSP (prop))
3779 {
3780 if (single_display_prop_intangible_p (XCAR (prop)))
3781 return 1;
3782 prop = XCDR (prop);
3783 }
3784 }
3785 else if (VECTORP (prop))
3786 {
3787 /* A vector of sub-properties. */
3788 int i;
3789 for (i = 0; i < ASIZE (prop); ++i)
3790 if (single_display_prop_intangible_p (AREF (prop, i)))
3791 return 1;
3792 }
3793 else
3794 return single_display_prop_intangible_p (prop);
3795
3796 return 0;
3797 }
3798
3799
3800 /* Return 1 if PROP is a display sub-property value containing STRING. */
3801
3802 static int
3803 single_display_prop_string_p (prop, string)
3804 Lisp_Object prop, string;
3805 {
3806 if (EQ (string, prop))
3807 return 1;
3808
3809 /* Skip over `when FORM'. */
3810 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3811 {
3812 prop = XCDR (prop);
3813 if (!CONSP (prop))
3814 return 0;
3815 prop = XCDR (prop);
3816 }
3817
3818 if (CONSP (prop))
3819 /* Skip over `margin LOCATION'. */
3820 if (EQ (XCAR (prop), Qmargin))
3821 {
3822 prop = XCDR (prop);
3823 if (!CONSP (prop))
3824 return 0;
3825
3826 prop = XCDR (prop);
3827 if (!CONSP (prop))
3828 return 0;
3829 }
3830
3831 return CONSP (prop) && EQ (XCAR (prop), string);
3832 }
3833
3834
3835 /* Return 1 if STRING appears in the `display' property PROP. */
3836
3837 static int
3838 display_prop_string_p (prop, string)
3839 Lisp_Object prop, string;
3840 {
3841 if (CONSP (prop)
3842 && CONSP (XCAR (prop))
3843 && !EQ (Qmargin, XCAR (XCAR (prop))))
3844 {
3845 /* A list of sub-properties. */
3846 while (CONSP (prop))
3847 {
3848 if (single_display_prop_string_p (XCAR (prop), string))
3849 return 1;
3850 prop = XCDR (prop);
3851 }
3852 }
3853 else if (VECTORP (prop))
3854 {
3855 /* A vector of sub-properties. */
3856 int i;
3857 for (i = 0; i < ASIZE (prop); ++i)
3858 if (single_display_prop_string_p (AREF (prop, i), string))
3859 return 1;
3860 }
3861 else
3862 return single_display_prop_string_p (prop, string);
3863
3864 return 0;
3865 }
3866
3867
3868 /* Determine from which buffer position in W's buffer STRING comes
3869 from. AROUND_CHARPOS is an approximate position where it could
3870 be from. Value is the buffer position or 0 if it couldn't be
3871 determined.
3872
3873 W's buffer must be current.
3874
3875 This function is necessary because we don't record buffer positions
3876 in glyphs generated from strings (to keep struct glyph small).
3877 This function may only use code that doesn't eval because it is
3878 called asynchronously from note_mouse_highlight. */
3879
3880 int
3881 string_buffer_position (w, string, around_charpos)
3882 struct window *w;
3883 Lisp_Object string;
3884 int around_charpos;
3885 {
3886 Lisp_Object limit, prop, pos;
3887 const int MAX_DISTANCE = 1000;
3888 int found = 0;
3889
3890 pos = make_number (around_charpos);
3891 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3892 while (!found && !EQ (pos, limit))
3893 {
3894 prop = Fget_char_property (pos, Qdisplay, Qnil);
3895 if (!NILP (prop) && display_prop_string_p (prop, string))
3896 found = 1;
3897 else
3898 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3899 }
3900
3901 if (!found)
3902 {
3903 pos = make_number (around_charpos);
3904 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3905 while (!found && !EQ (pos, limit))
3906 {
3907 prop = Fget_char_property (pos, Qdisplay, Qnil);
3908 if (!NILP (prop) && display_prop_string_p (prop, string))
3909 found = 1;
3910 else
3911 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3912 limit);
3913 }
3914 }
3915
3916 return found ? XINT (pos) : 0;
3917 }
3918
3919
3920 \f
3921 /***********************************************************************
3922 `composition' property
3923 ***********************************************************************/
3924
3925 /* Set up iterator IT from `composition' property at its current
3926 position. Called from handle_stop. */
3927
3928 static enum prop_handled
3929 handle_composition_prop (it)
3930 struct it *it;
3931 {
3932 Lisp_Object prop, string;
3933 int pos, pos_byte, end;
3934 enum prop_handled handled = HANDLED_NORMALLY;
3935
3936 if (STRINGP (it->string))
3937 {
3938 pos = IT_STRING_CHARPOS (*it);
3939 pos_byte = IT_STRING_BYTEPOS (*it);
3940 string = it->string;
3941 }
3942 else
3943 {
3944 pos = IT_CHARPOS (*it);
3945 pos_byte = IT_BYTEPOS (*it);
3946 string = Qnil;
3947 }
3948
3949 /* If there's a valid composition and point is not inside of the
3950 composition (in the case that the composition is from the current
3951 buffer), draw a glyph composed from the composition components. */
3952 if (find_composition (pos, -1, &pos, &end, &prop, string)
3953 && COMPOSITION_VALID_P (pos, end, prop)
3954 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3955 {
3956 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3957
3958 if (id >= 0)
3959 {
3960 it->method = next_element_from_composition;
3961 it->cmp_id = id;
3962 it->cmp_len = COMPOSITION_LENGTH (prop);
3963 /* For a terminal, draw only the first character of the
3964 components. */
3965 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3966 it->len = (STRINGP (it->string)
3967 ? string_char_to_byte (it->string, end)
3968 : CHAR_TO_BYTE (end)) - pos_byte;
3969 it->stop_charpos = end;
3970 handled = HANDLED_RETURN;
3971 }
3972 }
3973
3974 return handled;
3975 }
3976
3977
3978 \f
3979 /***********************************************************************
3980 Overlay strings
3981 ***********************************************************************/
3982
3983 /* The following structure is used to record overlay strings for
3984 later sorting in load_overlay_strings. */
3985
3986 struct overlay_entry
3987 {
3988 Lisp_Object overlay;
3989 Lisp_Object string;
3990 int priority;
3991 int after_string_p;
3992 };
3993
3994
3995 /* Set up iterator IT from overlay strings at its current position.
3996 Called from handle_stop. */
3997
3998 static enum prop_handled
3999 handle_overlay_change (it)
4000 struct it *it;
4001 {
4002 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4003 return HANDLED_RECOMPUTE_PROPS;
4004 else
4005 return HANDLED_NORMALLY;
4006 }
4007
4008
4009 /* Set up the next overlay string for delivery by IT, if there is an
4010 overlay string to deliver. Called by set_iterator_to_next when the
4011 end of the current overlay string is reached. If there are more
4012 overlay strings to display, IT->string and
4013 IT->current.overlay_string_index are set appropriately here.
4014 Otherwise IT->string is set to nil. */
4015
4016 static void
4017 next_overlay_string (it)
4018 struct it *it;
4019 {
4020 ++it->current.overlay_string_index;
4021 if (it->current.overlay_string_index == it->n_overlay_strings)
4022 {
4023 /* No more overlay strings. Restore IT's settings to what
4024 they were before overlay strings were processed, and
4025 continue to deliver from current_buffer. */
4026 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4027
4028 pop_it (it);
4029 xassert (it->stop_charpos >= BEGV
4030 && it->stop_charpos <= it->end_charpos);
4031 it->string = Qnil;
4032 it->current.overlay_string_index = -1;
4033 SET_TEXT_POS (it->current.string_pos, -1, -1);
4034 it->n_overlay_strings = 0;
4035 it->method = next_element_from_buffer;
4036
4037 /* If we're at the end of the buffer, record that we have
4038 processed the overlay strings there already, so that
4039 next_element_from_buffer doesn't try it again. */
4040 if (IT_CHARPOS (*it) >= it->end_charpos)
4041 it->overlay_strings_at_end_processed_p = 1;
4042
4043 /* If we have to display `...' for invisible text, set
4044 the iterator up for that. */
4045 if (display_ellipsis_p)
4046 setup_for_ellipsis (it);
4047 }
4048 else
4049 {
4050 /* There are more overlay strings to process. If
4051 IT->current.overlay_string_index has advanced to a position
4052 where we must load IT->overlay_strings with more strings, do
4053 it. */
4054 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4055
4056 if (it->current.overlay_string_index && i == 0)
4057 load_overlay_strings (it, 0);
4058
4059 /* Initialize IT to deliver display elements from the overlay
4060 string. */
4061 it->string = it->overlay_strings[i];
4062 it->multibyte_p = STRING_MULTIBYTE (it->string);
4063 SET_TEXT_POS (it->current.string_pos, 0, 0);
4064 it->method = next_element_from_string;
4065 it->stop_charpos = 0;
4066 }
4067
4068 CHECK_IT (it);
4069 }
4070
4071
4072 /* Compare two overlay_entry structures E1 and E2. Used as a
4073 comparison function for qsort in load_overlay_strings. Overlay
4074 strings for the same position are sorted so that
4075
4076 1. All after-strings come in front of before-strings, except
4077 when they come from the same overlay.
4078
4079 2. Within after-strings, strings are sorted so that overlay strings
4080 from overlays with higher priorities come first.
4081
4082 2. Within before-strings, strings are sorted so that overlay
4083 strings from overlays with higher priorities come last.
4084
4085 Value is analogous to strcmp. */
4086
4087
4088 static int
4089 compare_overlay_entries (e1, e2)
4090 void *e1, *e2;
4091 {
4092 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4093 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4094 int result;
4095
4096 if (entry1->after_string_p != entry2->after_string_p)
4097 {
4098 /* Let after-strings appear in front of before-strings if
4099 they come from different overlays. */
4100 if (EQ (entry1->overlay, entry2->overlay))
4101 result = entry1->after_string_p ? 1 : -1;
4102 else
4103 result = entry1->after_string_p ? -1 : 1;
4104 }
4105 else if (entry1->after_string_p)
4106 /* After-strings sorted in order of decreasing priority. */
4107 result = entry2->priority - entry1->priority;
4108 else
4109 /* Before-strings sorted in order of increasing priority. */
4110 result = entry1->priority - entry2->priority;
4111
4112 return result;
4113 }
4114
4115
4116 /* Load the vector IT->overlay_strings with overlay strings from IT's
4117 current buffer position, or from CHARPOS if that is > 0. Set
4118 IT->n_overlays to the total number of overlay strings found.
4119
4120 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4121 a time. On entry into load_overlay_strings,
4122 IT->current.overlay_string_index gives the number of overlay
4123 strings that have already been loaded by previous calls to this
4124 function.
4125
4126 IT->add_overlay_start contains an additional overlay start
4127 position to consider for taking overlay strings from, if non-zero.
4128 This position comes into play when the overlay has an `invisible'
4129 property, and both before and after-strings. When we've skipped to
4130 the end of the overlay, because of its `invisible' property, we
4131 nevertheless want its before-string to appear.
4132 IT->add_overlay_start will contain the overlay start position
4133 in this case.
4134
4135 Overlay strings are sorted so that after-string strings come in
4136 front of before-string strings. Within before and after-strings,
4137 strings are sorted by overlay priority. See also function
4138 compare_overlay_entries. */
4139
4140 static void
4141 load_overlay_strings (it, charpos)
4142 struct it *it;
4143 int charpos;
4144 {
4145 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4146 Lisp_Object overlay, window, str, invisible;
4147 struct Lisp_Overlay *ov;
4148 int start, end;
4149 int size = 20;
4150 int n = 0, i, j, invis_p;
4151 struct overlay_entry *entries
4152 = (struct overlay_entry *) alloca (size * sizeof *entries);
4153
4154 if (charpos <= 0)
4155 charpos = IT_CHARPOS (*it);
4156
4157 /* Append the overlay string STRING of overlay OVERLAY to vector
4158 `entries' which has size `size' and currently contains `n'
4159 elements. AFTER_P non-zero means STRING is an after-string of
4160 OVERLAY. */
4161 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4162 do \
4163 { \
4164 Lisp_Object priority; \
4165 \
4166 if (n == size) \
4167 { \
4168 int new_size = 2 * size; \
4169 struct overlay_entry *old = entries; \
4170 entries = \
4171 (struct overlay_entry *) alloca (new_size \
4172 * sizeof *entries); \
4173 bcopy (old, entries, size * sizeof *entries); \
4174 size = new_size; \
4175 } \
4176 \
4177 entries[n].string = (STRING); \
4178 entries[n].overlay = (OVERLAY); \
4179 priority = Foverlay_get ((OVERLAY), Qpriority); \
4180 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4181 entries[n].after_string_p = (AFTER_P); \
4182 ++n; \
4183 } \
4184 while (0)
4185
4186 /* Process overlay before the overlay center. */
4187 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4188 {
4189 XSETMISC (overlay, ov);
4190 xassert (OVERLAYP (overlay));
4191 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4192 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4193
4194 if (end < charpos)
4195 break;
4196
4197 /* Skip this overlay if it doesn't start or end at IT's current
4198 position. */
4199 if (end != charpos && start != charpos)
4200 continue;
4201
4202 /* Skip this overlay if it doesn't apply to IT->w. */
4203 window = Foverlay_get (overlay, Qwindow);
4204 if (WINDOWP (window) && XWINDOW (window) != it->w)
4205 continue;
4206
4207 /* If the text ``under'' the overlay is invisible, both before-
4208 and after-strings from this overlay are visible; start and
4209 end position are indistinguishable. */
4210 invisible = Foverlay_get (overlay, Qinvisible);
4211 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4212
4213 /* If overlay has a non-empty before-string, record it. */
4214 if ((start == charpos || (end == charpos && invis_p))
4215 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4216 && SCHARS (str))
4217 RECORD_OVERLAY_STRING (overlay, str, 0);
4218
4219 /* If overlay has a non-empty after-string, record it. */
4220 if ((end == charpos || (start == charpos && invis_p))
4221 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4222 && SCHARS (str))
4223 RECORD_OVERLAY_STRING (overlay, str, 1);
4224 }
4225
4226 /* Process overlays after the overlay center. */
4227 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4228 {
4229 XSETMISC (overlay, ov);
4230 xassert (OVERLAYP (overlay));
4231 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4232 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4233
4234 if (start > charpos)
4235 break;
4236
4237 /* Skip this overlay if it doesn't start or end at IT's current
4238 position. */
4239 if (end != charpos && start != charpos)
4240 continue;
4241
4242 /* Skip this overlay if it doesn't apply to IT->w. */
4243 window = Foverlay_get (overlay, Qwindow);
4244 if (WINDOWP (window) && XWINDOW (window) != it->w)
4245 continue;
4246
4247 /* If the text ``under'' the overlay is invisible, it has a zero
4248 dimension, and both before- and after-strings apply. */
4249 invisible = Foverlay_get (overlay, Qinvisible);
4250 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4251
4252 /* If overlay has a non-empty before-string, record it. */
4253 if ((start == charpos || (end == charpos && invis_p))
4254 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4255 && SCHARS (str))
4256 RECORD_OVERLAY_STRING (overlay, str, 0);
4257
4258 /* If overlay has a non-empty after-string, record it. */
4259 if ((end == charpos || (start == charpos && invis_p))
4260 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4261 && SCHARS (str))
4262 RECORD_OVERLAY_STRING (overlay, str, 1);
4263 }
4264
4265 #undef RECORD_OVERLAY_STRING
4266
4267 /* Sort entries. */
4268 if (n > 1)
4269 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4270
4271 /* Record the total number of strings to process. */
4272 it->n_overlay_strings = n;
4273
4274 /* IT->current.overlay_string_index is the number of overlay strings
4275 that have already been consumed by IT. Copy some of the
4276 remaining overlay strings to IT->overlay_strings. */
4277 i = 0;
4278 j = it->current.overlay_string_index;
4279 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4280 it->overlay_strings[i++] = entries[j++].string;
4281
4282 CHECK_IT (it);
4283 }
4284
4285
4286 /* Get the first chunk of overlay strings at IT's current buffer
4287 position, or at CHARPOS if that is > 0. Value is non-zero if at
4288 least one overlay string was found. */
4289
4290 static int
4291 get_overlay_strings (it, charpos)
4292 struct it *it;
4293 int charpos;
4294 {
4295 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4296 process. This fills IT->overlay_strings with strings, and sets
4297 IT->n_overlay_strings to the total number of strings to process.
4298 IT->pos.overlay_string_index has to be set temporarily to zero
4299 because load_overlay_strings needs this; it must be set to -1
4300 when no overlay strings are found because a zero value would
4301 indicate a position in the first overlay string. */
4302 it->current.overlay_string_index = 0;
4303 load_overlay_strings (it, charpos);
4304
4305 /* If we found overlay strings, set up IT to deliver display
4306 elements from the first one. Otherwise set up IT to deliver
4307 from current_buffer. */
4308 if (it->n_overlay_strings)
4309 {
4310 /* Make sure we know settings in current_buffer, so that we can
4311 restore meaningful values when we're done with the overlay
4312 strings. */
4313 compute_stop_pos (it);
4314 xassert (it->face_id >= 0);
4315
4316 /* Save IT's settings. They are restored after all overlay
4317 strings have been processed. */
4318 xassert (it->sp == 0);
4319 push_it (it);
4320
4321 /* Set up IT to deliver display elements from the first overlay
4322 string. */
4323 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4324 it->string = it->overlay_strings[0];
4325 it->stop_charpos = 0;
4326 xassert (STRINGP (it->string));
4327 it->end_charpos = SCHARS (it->string);
4328 it->multibyte_p = STRING_MULTIBYTE (it->string);
4329 it->method = next_element_from_string;
4330 }
4331 else
4332 {
4333 it->string = Qnil;
4334 it->current.overlay_string_index = -1;
4335 it->method = next_element_from_buffer;
4336 }
4337
4338 CHECK_IT (it);
4339
4340 /* Value is non-zero if we found at least one overlay string. */
4341 return STRINGP (it->string);
4342 }
4343
4344
4345 \f
4346 /***********************************************************************
4347 Saving and restoring state
4348 ***********************************************************************/
4349
4350 /* Save current settings of IT on IT->stack. Called, for example,
4351 before setting up IT for an overlay string, to be able to restore
4352 IT's settings to what they were after the overlay string has been
4353 processed. */
4354
4355 static void
4356 push_it (it)
4357 struct it *it;
4358 {
4359 struct iterator_stack_entry *p;
4360
4361 xassert (it->sp < 2);
4362 p = it->stack + it->sp;
4363
4364 p->stop_charpos = it->stop_charpos;
4365 xassert (it->face_id >= 0);
4366 p->face_id = it->face_id;
4367 p->string = it->string;
4368 p->pos = it->current;
4369 p->end_charpos = it->end_charpos;
4370 p->string_nchars = it->string_nchars;
4371 p->area = it->area;
4372 p->multibyte_p = it->multibyte_p;
4373 p->slice = it->slice;
4374 p->space_width = it->space_width;
4375 p->font_height = it->font_height;
4376 p->voffset = it->voffset;
4377 p->string_from_display_prop_p = it->string_from_display_prop_p;
4378 p->display_ellipsis_p = 0;
4379 ++it->sp;
4380 }
4381
4382
4383 /* Restore IT's settings from IT->stack. Called, for example, when no
4384 more overlay strings must be processed, and we return to delivering
4385 display elements from a buffer, or when the end of a string from a
4386 `display' property is reached and we return to delivering display
4387 elements from an overlay string, or from a buffer. */
4388
4389 static void
4390 pop_it (it)
4391 struct it *it;
4392 {
4393 struct iterator_stack_entry *p;
4394
4395 xassert (it->sp > 0);
4396 --it->sp;
4397 p = it->stack + it->sp;
4398 it->stop_charpos = p->stop_charpos;
4399 it->face_id = p->face_id;
4400 it->string = p->string;
4401 it->current = p->pos;
4402 it->end_charpos = p->end_charpos;
4403 it->string_nchars = p->string_nchars;
4404 it->area = p->area;
4405 it->multibyte_p = p->multibyte_p;
4406 it->slice = p->slice;
4407 it->space_width = p->space_width;
4408 it->font_height = p->font_height;
4409 it->voffset = p->voffset;
4410 it->string_from_display_prop_p = p->string_from_display_prop_p;
4411 }
4412
4413
4414 \f
4415 /***********************************************************************
4416 Moving over lines
4417 ***********************************************************************/
4418
4419 /* Set IT's current position to the previous line start. */
4420
4421 static void
4422 back_to_previous_line_start (it)
4423 struct it *it;
4424 {
4425 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4426 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4427 }
4428
4429
4430 /* Move IT to the next line start.
4431
4432 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4433 we skipped over part of the text (as opposed to moving the iterator
4434 continuously over the text). Otherwise, don't change the value
4435 of *SKIPPED_P.
4436
4437 Newlines may come from buffer text, overlay strings, or strings
4438 displayed via the `display' property. That's the reason we can't
4439 simply use find_next_newline_no_quit.
4440
4441 Note that this function may not skip over invisible text that is so
4442 because of text properties and immediately follows a newline. If
4443 it would, function reseat_at_next_visible_line_start, when called
4444 from set_iterator_to_next, would effectively make invisible
4445 characters following a newline part of the wrong glyph row, which
4446 leads to wrong cursor motion. */
4447
4448 static int
4449 forward_to_next_line_start (it, skipped_p)
4450 struct it *it;
4451 int *skipped_p;
4452 {
4453 int old_selective, newline_found_p, n;
4454 const int MAX_NEWLINE_DISTANCE = 500;
4455
4456 /* If already on a newline, just consume it to avoid unintended
4457 skipping over invisible text below. */
4458 if (it->what == IT_CHARACTER
4459 && it->c == '\n'
4460 && CHARPOS (it->position) == IT_CHARPOS (*it))
4461 {
4462 set_iterator_to_next (it, 0);
4463 it->c = 0;
4464 return 1;
4465 }
4466
4467 /* Don't handle selective display in the following. It's (a)
4468 unnecessary because it's done by the caller, and (b) leads to an
4469 infinite recursion because next_element_from_ellipsis indirectly
4470 calls this function. */
4471 old_selective = it->selective;
4472 it->selective = 0;
4473
4474 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4475 from buffer text. */
4476 for (n = newline_found_p = 0;
4477 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4478 n += STRINGP (it->string) ? 0 : 1)
4479 {
4480 if (!get_next_display_element (it))
4481 return 0;
4482 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4483 set_iterator_to_next (it, 0);
4484 }
4485
4486 /* If we didn't find a newline near enough, see if we can use a
4487 short-cut. */
4488 if (!newline_found_p)
4489 {
4490 int start = IT_CHARPOS (*it);
4491 int limit = find_next_newline_no_quit (start, 1);
4492 Lisp_Object pos;
4493
4494 xassert (!STRINGP (it->string));
4495
4496 /* If there isn't any `display' property in sight, and no
4497 overlays, we can just use the position of the newline in
4498 buffer text. */
4499 if (it->stop_charpos >= limit
4500 || ((pos = Fnext_single_property_change (make_number (start),
4501 Qdisplay,
4502 Qnil, make_number (limit)),
4503 NILP (pos))
4504 && next_overlay_change (start) == ZV))
4505 {
4506 IT_CHARPOS (*it) = limit;
4507 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4508 *skipped_p = newline_found_p = 1;
4509 }
4510 else
4511 {
4512 while (get_next_display_element (it)
4513 && !newline_found_p)
4514 {
4515 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4516 set_iterator_to_next (it, 0);
4517 }
4518 }
4519 }
4520
4521 it->selective = old_selective;
4522 return newline_found_p;
4523 }
4524
4525
4526 /* Set IT's current position to the previous visible line start. Skip
4527 invisible text that is so either due to text properties or due to
4528 selective display. Caution: this does not change IT->current_x and
4529 IT->hpos. */
4530
4531 static void
4532 back_to_previous_visible_line_start (it)
4533 struct it *it;
4534 {
4535 int visible_p = 0;
4536
4537 /* Go back one newline if not on BEGV already. */
4538 if (IT_CHARPOS (*it) > BEGV)
4539 back_to_previous_line_start (it);
4540
4541 /* Move over lines that are invisible because of selective display
4542 or text properties. */
4543 while (IT_CHARPOS (*it) > BEGV
4544 && !visible_p)
4545 {
4546 visible_p = 1;
4547
4548 /* If selective > 0, then lines indented more than that values
4549 are invisible. */
4550 if (it->selective > 0
4551 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4552 (double) it->selective)) /* iftc */
4553 visible_p = 0;
4554 else
4555 {
4556 Lisp_Object prop;
4557
4558 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
4559 Qinvisible, it->window);
4560 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4561 visible_p = 0;
4562 }
4563
4564 if (visible_p)
4565 {
4566 struct it it2 = *it;
4567
4568 if (handle_display_prop (&it2) == HANDLED_RETURN)
4569 visible_p = 0;
4570 }
4571
4572 /* Back one more newline if the current one is invisible. */
4573 if (!visible_p)
4574 back_to_previous_line_start (it);
4575 }
4576
4577 xassert (IT_CHARPOS (*it) >= BEGV);
4578 xassert (IT_CHARPOS (*it) == BEGV
4579 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4580 CHECK_IT (it);
4581 }
4582
4583
4584 /* Reseat iterator IT at the previous visible line start. Skip
4585 invisible text that is so either due to text properties or due to
4586 selective display. At the end, update IT's overlay information,
4587 face information etc. */
4588
4589 static void
4590 reseat_at_previous_visible_line_start (it)
4591 struct it *it;
4592 {
4593 back_to_previous_visible_line_start (it);
4594 reseat (it, it->current.pos, 1);
4595 CHECK_IT (it);
4596 }
4597
4598
4599 /* Reseat iterator IT on the next visible line start in the current
4600 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4601 preceding the line start. Skip over invisible text that is so
4602 because of selective display. Compute faces, overlays etc at the
4603 new position. Note that this function does not skip over text that
4604 is invisible because of text properties. */
4605
4606 static void
4607 reseat_at_next_visible_line_start (it, on_newline_p)
4608 struct it *it;
4609 int on_newline_p;
4610 {
4611 int newline_found_p, skipped_p = 0;
4612
4613 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4614
4615 /* Skip over lines that are invisible because they are indented
4616 more than the value of IT->selective. */
4617 if (it->selective > 0)
4618 while (IT_CHARPOS (*it) < ZV
4619 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4620 (double) it->selective)) /* iftc */
4621 {
4622 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4623 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4624 }
4625
4626 /* Position on the newline if that's what's requested. */
4627 if (on_newline_p && newline_found_p)
4628 {
4629 if (STRINGP (it->string))
4630 {
4631 if (IT_STRING_CHARPOS (*it) > 0)
4632 {
4633 --IT_STRING_CHARPOS (*it);
4634 --IT_STRING_BYTEPOS (*it);
4635 }
4636 }
4637 else if (IT_CHARPOS (*it) > BEGV)
4638 {
4639 --IT_CHARPOS (*it);
4640 --IT_BYTEPOS (*it);
4641 reseat (it, it->current.pos, 0);
4642 }
4643 }
4644 else if (skipped_p)
4645 reseat (it, it->current.pos, 0);
4646
4647 CHECK_IT (it);
4648 }
4649
4650
4651 \f
4652 /***********************************************************************
4653 Changing an iterator's position
4654 ***********************************************************************/
4655
4656 /* Change IT's current position to POS in current_buffer. If FORCE_P
4657 is non-zero, always check for text properties at the new position.
4658 Otherwise, text properties are only looked up if POS >=
4659 IT->check_charpos of a property. */
4660
4661 static void
4662 reseat (it, pos, force_p)
4663 struct it *it;
4664 struct text_pos pos;
4665 int force_p;
4666 {
4667 int original_pos = IT_CHARPOS (*it);
4668
4669 reseat_1 (it, pos, 0);
4670
4671 /* Determine where to check text properties. Avoid doing it
4672 where possible because text property lookup is very expensive. */
4673 if (force_p
4674 || CHARPOS (pos) > it->stop_charpos
4675 || CHARPOS (pos) < original_pos)
4676 handle_stop (it);
4677
4678 CHECK_IT (it);
4679 }
4680
4681
4682 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4683 IT->stop_pos to POS, also. */
4684
4685 static void
4686 reseat_1 (it, pos, set_stop_p)
4687 struct it *it;
4688 struct text_pos pos;
4689 int set_stop_p;
4690 {
4691 /* Don't call this function when scanning a C string. */
4692 xassert (it->s == NULL);
4693
4694 /* POS must be a reasonable value. */
4695 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4696
4697 it->current.pos = it->position = pos;
4698 XSETBUFFER (it->object, current_buffer);
4699 it->end_charpos = ZV;
4700 it->dpvec = NULL;
4701 it->current.dpvec_index = -1;
4702 it->current.overlay_string_index = -1;
4703 IT_STRING_CHARPOS (*it) = -1;
4704 IT_STRING_BYTEPOS (*it) = -1;
4705 it->string = Qnil;
4706 it->method = next_element_from_buffer;
4707 /* RMS: I added this to fix a bug in move_it_vertically_backward
4708 where it->area continued to relate to the starting point
4709 for the backward motion. Bug report from
4710 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4711 However, I am not sure whether reseat still does the right thing
4712 in general after this change. */
4713 it->area = TEXT_AREA;
4714 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4715 it->sp = 0;
4716 it->face_before_selective_p = 0;
4717
4718 if (set_stop_p)
4719 it->stop_charpos = CHARPOS (pos);
4720 }
4721
4722
4723 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4724 If S is non-null, it is a C string to iterate over. Otherwise,
4725 STRING gives a Lisp string to iterate over.
4726
4727 If PRECISION > 0, don't return more then PRECISION number of
4728 characters from the string.
4729
4730 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4731 characters have been returned. FIELD_WIDTH < 0 means an infinite
4732 field width.
4733
4734 MULTIBYTE = 0 means disable processing of multibyte characters,
4735 MULTIBYTE > 0 means enable it,
4736 MULTIBYTE < 0 means use IT->multibyte_p.
4737
4738 IT must be initialized via a prior call to init_iterator before
4739 calling this function. */
4740
4741 static void
4742 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4743 struct it *it;
4744 unsigned char *s;
4745 Lisp_Object string;
4746 int charpos;
4747 int precision, field_width, multibyte;
4748 {
4749 /* No region in strings. */
4750 it->region_beg_charpos = it->region_end_charpos = -1;
4751
4752 /* No text property checks performed by default, but see below. */
4753 it->stop_charpos = -1;
4754
4755 /* Set iterator position and end position. */
4756 bzero (&it->current, sizeof it->current);
4757 it->current.overlay_string_index = -1;
4758 it->current.dpvec_index = -1;
4759 xassert (charpos >= 0);
4760
4761 /* If STRING is specified, use its multibyteness, otherwise use the
4762 setting of MULTIBYTE, if specified. */
4763 if (multibyte >= 0)
4764 it->multibyte_p = multibyte > 0;
4765
4766 if (s == NULL)
4767 {
4768 xassert (STRINGP (string));
4769 it->string = string;
4770 it->s = NULL;
4771 it->end_charpos = it->string_nchars = SCHARS (string);
4772 it->method = next_element_from_string;
4773 it->current.string_pos = string_pos (charpos, string);
4774 }
4775 else
4776 {
4777 it->s = s;
4778 it->string = Qnil;
4779
4780 /* Note that we use IT->current.pos, not it->current.string_pos,
4781 for displaying C strings. */
4782 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4783 if (it->multibyte_p)
4784 {
4785 it->current.pos = c_string_pos (charpos, s, 1);
4786 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4787 }
4788 else
4789 {
4790 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4791 it->end_charpos = it->string_nchars = strlen (s);
4792 }
4793
4794 it->method = next_element_from_c_string;
4795 }
4796
4797 /* PRECISION > 0 means don't return more than PRECISION characters
4798 from the string. */
4799 if (precision > 0 && it->end_charpos - charpos > precision)
4800 it->end_charpos = it->string_nchars = charpos + precision;
4801
4802 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4803 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4804 FIELD_WIDTH < 0 means infinite field width. This is useful for
4805 padding with `-' at the end of a mode line. */
4806 if (field_width < 0)
4807 field_width = INFINITY;
4808 if (field_width > it->end_charpos - charpos)
4809 it->end_charpos = charpos + field_width;
4810
4811 /* Use the standard display table for displaying strings. */
4812 if (DISP_TABLE_P (Vstandard_display_table))
4813 it->dp = XCHAR_TABLE (Vstandard_display_table);
4814
4815 it->stop_charpos = charpos;
4816 CHECK_IT (it);
4817 }
4818
4819
4820 \f
4821 /***********************************************************************
4822 Iteration
4823 ***********************************************************************/
4824
4825 /* Load IT's display element fields with information about the next
4826 display element from the current position of IT. Value is zero if
4827 end of buffer (or C string) is reached. */
4828
4829 int
4830 get_next_display_element (it)
4831 struct it *it;
4832 {
4833 /* Non-zero means that we found a display element. Zero means that
4834 we hit the end of what we iterate over. Performance note: the
4835 function pointer `method' used here turns out to be faster than
4836 using a sequence of if-statements. */
4837 int success_p = (*it->method) (it);
4838
4839 if (it->what == IT_CHARACTER)
4840 {
4841 /* Map via display table or translate control characters.
4842 IT->c, IT->len etc. have been set to the next character by
4843 the function call above. If we have a display table, and it
4844 contains an entry for IT->c, translate it. Don't do this if
4845 IT->c itself comes from a display table, otherwise we could
4846 end up in an infinite recursion. (An alternative could be to
4847 count the recursion depth of this function and signal an
4848 error when a certain maximum depth is reached.) Is it worth
4849 it? */
4850 if (success_p && it->dpvec == NULL)
4851 {
4852 Lisp_Object dv;
4853
4854 if (it->dp
4855 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4856 VECTORP (dv)))
4857 {
4858 struct Lisp_Vector *v = XVECTOR (dv);
4859
4860 /* Return the first character from the display table
4861 entry, if not empty. If empty, don't display the
4862 current character. */
4863 if (v->size)
4864 {
4865 it->dpvec_char_len = it->len;
4866 it->dpvec = v->contents;
4867 it->dpend = v->contents + v->size;
4868 it->current.dpvec_index = 0;
4869 it->method = next_element_from_display_vector;
4870 success_p = get_next_display_element (it);
4871 }
4872 else
4873 {
4874 set_iterator_to_next (it, 0);
4875 success_p = get_next_display_element (it);
4876 }
4877 }
4878
4879 /* Translate control characters into `\003' or `^C' form.
4880 Control characters coming from a display table entry are
4881 currently not translated because we use IT->dpvec to hold
4882 the translation. This could easily be changed but I
4883 don't believe that it is worth doing.
4884
4885 If it->multibyte_p is nonzero, eight-bit characters and
4886 non-printable multibyte characters are also translated to
4887 octal form.
4888
4889 If it->multibyte_p is zero, eight-bit characters that
4890 don't have corresponding multibyte char code are also
4891 translated to octal form. */
4892 else if ((it->c < ' '
4893 && (it->area != TEXT_AREA
4894 || (it->c != '\n' && it->c != '\t')))
4895 || (it->multibyte_p
4896 ? ((it->c >= 127
4897 && it->len == 1)
4898 || !CHAR_PRINTABLE_P (it->c))
4899 : (it->c >= 127
4900 && it->c == unibyte_char_to_multibyte (it->c))))
4901 {
4902 /* IT->c is a control character which must be displayed
4903 either as '\003' or as `^C' where the '\\' and '^'
4904 can be defined in the display table. Fill
4905 IT->ctl_chars with glyphs for what we have to
4906 display. Then, set IT->dpvec to these glyphs. */
4907 GLYPH g;
4908
4909 if (it->c < 128 && it->ctl_arrow_p)
4910 {
4911 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4912 if (it->dp
4913 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4914 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4915 g = XINT (DISP_CTRL_GLYPH (it->dp));
4916 else
4917 g = FAST_MAKE_GLYPH ('^', 0);
4918 XSETINT (it->ctl_chars[0], g);
4919
4920 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4921 XSETINT (it->ctl_chars[1], g);
4922
4923 /* Set up IT->dpvec and return first character from it. */
4924 it->dpvec_char_len = it->len;
4925 it->dpvec = it->ctl_chars;
4926 it->dpend = it->dpvec + 2;
4927 it->current.dpvec_index = 0;
4928 it->method = next_element_from_display_vector;
4929 get_next_display_element (it);
4930 }
4931 else
4932 {
4933 unsigned char str[MAX_MULTIBYTE_LENGTH];
4934 int len;
4935 int i;
4936 GLYPH escape_glyph;
4937
4938 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4939 if (it->dp
4940 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4941 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4942 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4943 else
4944 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4945
4946 if (SINGLE_BYTE_CHAR_P (it->c))
4947 str[0] = it->c, len = 1;
4948 else
4949 {
4950 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4951 if (len < 0)
4952 {
4953 /* It's an invalid character, which
4954 shouldn't happen actually, but due to
4955 bugs it may happen. Let's print the char
4956 as is, there's not much meaningful we can
4957 do with it. */
4958 str[0] = it->c;
4959 str[1] = it->c >> 8;
4960 str[2] = it->c >> 16;
4961 str[3] = it->c >> 24;
4962 len = 4;
4963 }
4964 }
4965
4966 for (i = 0; i < len; i++)
4967 {
4968 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4969 /* Insert three more glyphs into IT->ctl_chars for
4970 the octal display of the character. */
4971 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4972 XSETINT (it->ctl_chars[i * 4 + 1], g);
4973 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4974 XSETINT (it->ctl_chars[i * 4 + 2], g);
4975 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4976 XSETINT (it->ctl_chars[i * 4 + 3], g);
4977 }
4978
4979 /* Set up IT->dpvec and return the first character
4980 from it. */
4981 it->dpvec_char_len = it->len;
4982 it->dpvec = it->ctl_chars;
4983 it->dpend = it->dpvec + len * 4;
4984 it->current.dpvec_index = 0;
4985 it->method = next_element_from_display_vector;
4986 get_next_display_element (it);
4987 }
4988 }
4989 }
4990
4991 /* Adjust face id for a multibyte character. There are no
4992 multibyte character in unibyte text. */
4993 if (it->multibyte_p
4994 && success_p
4995 && FRAME_WINDOW_P (it->f))
4996 {
4997 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4998 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4999 }
5000 }
5001
5002 /* Is this character the last one of a run of characters with
5003 box? If yes, set IT->end_of_box_run_p to 1. */
5004 if (it->face_box_p
5005 && it->s == NULL)
5006 {
5007 int face_id;
5008 struct face *face;
5009
5010 it->end_of_box_run_p
5011 = ((face_id = face_after_it_pos (it),
5012 face_id != it->face_id)
5013 && (face = FACE_FROM_ID (it->f, face_id),
5014 face->box == FACE_NO_BOX));
5015 }
5016
5017 /* Value is 0 if end of buffer or string reached. */
5018 return success_p;
5019 }
5020
5021
5022 /* Move IT to the next display element.
5023
5024 RESEAT_P non-zero means if called on a newline in buffer text,
5025 skip to the next visible line start.
5026
5027 Functions get_next_display_element and set_iterator_to_next are
5028 separate because I find this arrangement easier to handle than a
5029 get_next_display_element function that also increments IT's
5030 position. The way it is we can first look at an iterator's current
5031 display element, decide whether it fits on a line, and if it does,
5032 increment the iterator position. The other way around we probably
5033 would either need a flag indicating whether the iterator has to be
5034 incremented the next time, or we would have to implement a
5035 decrement position function which would not be easy to write. */
5036
5037 void
5038 set_iterator_to_next (it, reseat_p)
5039 struct it *it;
5040 int reseat_p;
5041 {
5042 /* Reset flags indicating start and end of a sequence of characters
5043 with box. Reset them at the start of this function because
5044 moving the iterator to a new position might set them. */
5045 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5046
5047 if (it->method == next_element_from_buffer)
5048 {
5049 /* The current display element of IT is a character from
5050 current_buffer. Advance in the buffer, and maybe skip over
5051 invisible lines that are so because of selective display. */
5052 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5053 reseat_at_next_visible_line_start (it, 0);
5054 else
5055 {
5056 xassert (it->len != 0);
5057 IT_BYTEPOS (*it) += it->len;
5058 IT_CHARPOS (*it) += 1;
5059 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5060 }
5061 }
5062 else if (it->method == next_element_from_composition)
5063 {
5064 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
5065 if (STRINGP (it->string))
5066 {
5067 IT_STRING_BYTEPOS (*it) += it->len;
5068 IT_STRING_CHARPOS (*it) += it->cmp_len;
5069 it->method = next_element_from_string;
5070 goto consider_string_end;
5071 }
5072 else
5073 {
5074 IT_BYTEPOS (*it) += it->len;
5075 IT_CHARPOS (*it) += it->cmp_len;
5076 it->method = next_element_from_buffer;
5077 }
5078 }
5079 else if (it->method == next_element_from_c_string)
5080 {
5081 /* Current display element of IT is from a C string. */
5082 IT_BYTEPOS (*it) += it->len;
5083 IT_CHARPOS (*it) += 1;
5084 }
5085 else if (it->method == next_element_from_display_vector)
5086 {
5087 /* Current display element of IT is from a display table entry.
5088 Advance in the display table definition. Reset it to null if
5089 end reached, and continue with characters from buffers/
5090 strings. */
5091 ++it->current.dpvec_index;
5092
5093 /* Restore face of the iterator to what they were before the
5094 display vector entry (these entries may contain faces). */
5095 it->face_id = it->saved_face_id;
5096
5097 if (it->dpvec + it->current.dpvec_index == it->dpend)
5098 {
5099 if (it->s)
5100 it->method = next_element_from_c_string;
5101 else if (STRINGP (it->string))
5102 it->method = next_element_from_string;
5103 else
5104 it->method = next_element_from_buffer;
5105
5106 it->dpvec = NULL;
5107 it->current.dpvec_index = -1;
5108
5109 /* Skip over characters which were displayed via IT->dpvec. */
5110 if (it->dpvec_char_len < 0)
5111 reseat_at_next_visible_line_start (it, 1);
5112 else if (it->dpvec_char_len > 0)
5113 {
5114 it->len = it->dpvec_char_len;
5115 set_iterator_to_next (it, reseat_p);
5116 }
5117 }
5118 }
5119 else if (it->method == next_element_from_string)
5120 {
5121 /* Current display element is a character from a Lisp string. */
5122 xassert (it->s == NULL && STRINGP (it->string));
5123 IT_STRING_BYTEPOS (*it) += it->len;
5124 IT_STRING_CHARPOS (*it) += 1;
5125
5126 consider_string_end:
5127
5128 if (it->current.overlay_string_index >= 0)
5129 {
5130 /* IT->string is an overlay string. Advance to the
5131 next, if there is one. */
5132 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5133 next_overlay_string (it);
5134 }
5135 else
5136 {
5137 /* IT->string is not an overlay string. If we reached
5138 its end, and there is something on IT->stack, proceed
5139 with what is on the stack. This can be either another
5140 string, this time an overlay string, or a buffer. */
5141 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5142 && it->sp > 0)
5143 {
5144 pop_it (it);
5145 if (!STRINGP (it->string))
5146 it->method = next_element_from_buffer;
5147 else
5148 goto consider_string_end;
5149 }
5150 }
5151 }
5152 else if (it->method == next_element_from_image
5153 || it->method == next_element_from_stretch)
5154 {
5155 /* The position etc with which we have to proceed are on
5156 the stack. The position may be at the end of a string,
5157 if the `display' property takes up the whole string. */
5158 pop_it (it);
5159 it->image_id = 0;
5160 if (STRINGP (it->string))
5161 {
5162 it->method = next_element_from_string;
5163 goto consider_string_end;
5164 }
5165 else
5166 it->method = next_element_from_buffer;
5167 }
5168 else
5169 /* There are no other methods defined, so this should be a bug. */
5170 abort ();
5171
5172 xassert (it->method != next_element_from_string
5173 || (STRINGP (it->string)
5174 && IT_STRING_CHARPOS (*it) >= 0));
5175 }
5176
5177
5178 /* Load IT's display element fields with information about the next
5179 display element which comes from a display table entry or from the
5180 result of translating a control character to one of the forms `^C'
5181 or `\003'. IT->dpvec holds the glyphs to return as characters. */
5182
5183 static int
5184 next_element_from_display_vector (it)
5185 struct it *it;
5186 {
5187 /* Precondition. */
5188 xassert (it->dpvec && it->current.dpvec_index >= 0);
5189
5190 /* Remember the current face id in case glyphs specify faces.
5191 IT's face is restored in set_iterator_to_next. */
5192 it->saved_face_id = it->face_id;
5193
5194 if (INTEGERP (*it->dpvec)
5195 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5196 {
5197 int lface_id;
5198 GLYPH g;
5199
5200 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5201 it->c = FAST_GLYPH_CHAR (g);
5202 it->len = CHAR_BYTES (it->c);
5203
5204 /* The entry may contain a face id to use. Such a face id is
5205 the id of a Lisp face, not a realized face. A face id of
5206 zero means no face is specified. */
5207 lface_id = FAST_GLYPH_FACE (g);
5208 if (lface_id)
5209 {
5210 /* The function returns -1 if lface_id is invalid. */
5211 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
5212 if (face_id >= 0)
5213 it->face_id = face_id;
5214 }
5215 }
5216 else
5217 /* Display table entry is invalid. Return a space. */
5218 it->c = ' ', it->len = 1;
5219
5220 /* Don't change position and object of the iterator here. They are
5221 still the values of the character that had this display table
5222 entry or was translated, and that's what we want. */
5223 it->what = IT_CHARACTER;
5224 return 1;
5225 }
5226
5227
5228 /* Load IT with the next display element from Lisp string IT->string.
5229 IT->current.string_pos is the current position within the string.
5230 If IT->current.overlay_string_index >= 0, the Lisp string is an
5231 overlay string. */
5232
5233 static int
5234 next_element_from_string (it)
5235 struct it *it;
5236 {
5237 struct text_pos position;
5238
5239 xassert (STRINGP (it->string));
5240 xassert (IT_STRING_CHARPOS (*it) >= 0);
5241 position = it->current.string_pos;
5242
5243 /* Time to check for invisible text? */
5244 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5245 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5246 {
5247 handle_stop (it);
5248
5249 /* Since a handler may have changed IT->method, we must
5250 recurse here. */
5251 return get_next_display_element (it);
5252 }
5253
5254 if (it->current.overlay_string_index >= 0)
5255 {
5256 /* Get the next character from an overlay string. In overlay
5257 strings, There is no field width or padding with spaces to
5258 do. */
5259 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5260 {
5261 it->what = IT_EOB;
5262 return 0;
5263 }
5264 else if (STRING_MULTIBYTE (it->string))
5265 {
5266 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5267 const unsigned char *s = (SDATA (it->string)
5268 + IT_STRING_BYTEPOS (*it));
5269 it->c = string_char_and_length (s, remaining, &it->len);
5270 }
5271 else
5272 {
5273 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5274 it->len = 1;
5275 }
5276 }
5277 else
5278 {
5279 /* Get the next character from a Lisp string that is not an
5280 overlay string. Such strings come from the mode line, for
5281 example. We may have to pad with spaces, or truncate the
5282 string. See also next_element_from_c_string. */
5283 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5284 {
5285 it->what = IT_EOB;
5286 return 0;
5287 }
5288 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5289 {
5290 /* Pad with spaces. */
5291 it->c = ' ', it->len = 1;
5292 CHARPOS (position) = BYTEPOS (position) = -1;
5293 }
5294 else if (STRING_MULTIBYTE (it->string))
5295 {
5296 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5297 const unsigned char *s = (SDATA (it->string)
5298 + IT_STRING_BYTEPOS (*it));
5299 it->c = string_char_and_length (s, maxlen, &it->len);
5300 }
5301 else
5302 {
5303 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5304 it->len = 1;
5305 }
5306 }
5307
5308 /* Record what we have and where it came from. Note that we store a
5309 buffer position in IT->position although it could arguably be a
5310 string position. */
5311 it->what = IT_CHARACTER;
5312 it->object = it->string;
5313 it->position = position;
5314 return 1;
5315 }
5316
5317
5318 /* Load IT with next display element from C string IT->s.
5319 IT->string_nchars is the maximum number of characters to return
5320 from the string. IT->end_charpos may be greater than
5321 IT->string_nchars when this function is called, in which case we
5322 may have to return padding spaces. Value is zero if end of string
5323 reached, including padding spaces. */
5324
5325 static int
5326 next_element_from_c_string (it)
5327 struct it *it;
5328 {
5329 int success_p = 1;
5330
5331 xassert (it->s);
5332 it->what = IT_CHARACTER;
5333 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5334 it->object = Qnil;
5335
5336 /* IT's position can be greater IT->string_nchars in case a field
5337 width or precision has been specified when the iterator was
5338 initialized. */
5339 if (IT_CHARPOS (*it) >= it->end_charpos)
5340 {
5341 /* End of the game. */
5342 it->what = IT_EOB;
5343 success_p = 0;
5344 }
5345 else if (IT_CHARPOS (*it) >= it->string_nchars)
5346 {
5347 /* Pad with spaces. */
5348 it->c = ' ', it->len = 1;
5349 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5350 }
5351 else if (it->multibyte_p)
5352 {
5353 /* Implementation note: The calls to strlen apparently aren't a
5354 performance problem because there is no noticeable performance
5355 difference between Emacs running in unibyte or multibyte mode. */
5356 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5357 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5358 maxlen, &it->len);
5359 }
5360 else
5361 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5362
5363 return success_p;
5364 }
5365
5366
5367 /* Set up IT to return characters from an ellipsis, if appropriate.
5368 The definition of the ellipsis glyphs may come from a display table
5369 entry. This function Fills IT with the first glyph from the
5370 ellipsis if an ellipsis is to be displayed. */
5371
5372 static int
5373 next_element_from_ellipsis (it)
5374 struct it *it;
5375 {
5376 if (it->selective_display_ellipsis_p)
5377 {
5378 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
5379 {
5380 /* Use the display table definition for `...'. Invalid glyphs
5381 will be handled by the method returning elements from dpvec. */
5382 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
5383 it->dpvec_char_len = it->len;
5384 it->dpvec = v->contents;
5385 it->dpend = v->contents + v->size;
5386 it->current.dpvec_index = 0;
5387 it->method = next_element_from_display_vector;
5388 }
5389 else
5390 {
5391 /* Use default `...' which is stored in default_invis_vector. */
5392 it->dpvec_char_len = it->len;
5393 it->dpvec = default_invis_vector;
5394 it->dpend = default_invis_vector + 3;
5395 it->current.dpvec_index = 0;
5396 it->method = next_element_from_display_vector;
5397 }
5398 }
5399 else
5400 {
5401 /* The face at the current position may be different from the
5402 face we find after the invisible text. Remember what it
5403 was in IT->saved_face_id, and signal that it's there by
5404 setting face_before_selective_p. */
5405 it->saved_face_id = it->face_id;
5406 it->method = next_element_from_buffer;
5407 reseat_at_next_visible_line_start (it, 1);
5408 it->face_before_selective_p = 1;
5409 }
5410
5411 return get_next_display_element (it);
5412 }
5413
5414
5415 /* Deliver an image display element. The iterator IT is already
5416 filled with image information (done in handle_display_prop). Value
5417 is always 1. */
5418
5419
5420 static int
5421 next_element_from_image (it)
5422 struct it *it;
5423 {
5424 it->what = IT_IMAGE;
5425 return 1;
5426 }
5427
5428
5429 /* Fill iterator IT with next display element from a stretch glyph
5430 property. IT->object is the value of the text property. Value is
5431 always 1. */
5432
5433 static int
5434 next_element_from_stretch (it)
5435 struct it *it;
5436 {
5437 it->what = IT_STRETCH;
5438 return 1;
5439 }
5440
5441
5442 /* Load IT with the next display element from current_buffer. Value
5443 is zero if end of buffer reached. IT->stop_charpos is the next
5444 position at which to stop and check for text properties or buffer
5445 end. */
5446
5447 static int
5448 next_element_from_buffer (it)
5449 struct it *it;
5450 {
5451 int success_p = 1;
5452
5453 /* Check this assumption, otherwise, we would never enter the
5454 if-statement, below. */
5455 xassert (IT_CHARPOS (*it) >= BEGV
5456 && IT_CHARPOS (*it) <= it->stop_charpos);
5457
5458 if (IT_CHARPOS (*it) >= it->stop_charpos)
5459 {
5460 if (IT_CHARPOS (*it) >= it->end_charpos)
5461 {
5462 int overlay_strings_follow_p;
5463
5464 /* End of the game, except when overlay strings follow that
5465 haven't been returned yet. */
5466 if (it->overlay_strings_at_end_processed_p)
5467 overlay_strings_follow_p = 0;
5468 else
5469 {
5470 it->overlay_strings_at_end_processed_p = 1;
5471 overlay_strings_follow_p = get_overlay_strings (it, 0);
5472 }
5473
5474 if (overlay_strings_follow_p)
5475 success_p = get_next_display_element (it);
5476 else
5477 {
5478 it->what = IT_EOB;
5479 it->position = it->current.pos;
5480 success_p = 0;
5481 }
5482 }
5483 else
5484 {
5485 handle_stop (it);
5486 return get_next_display_element (it);
5487 }
5488 }
5489 else
5490 {
5491 /* No face changes, overlays etc. in sight, so just return a
5492 character from current_buffer. */
5493 unsigned char *p;
5494
5495 /* Maybe run the redisplay end trigger hook. Performance note:
5496 This doesn't seem to cost measurable time. */
5497 if (it->redisplay_end_trigger_charpos
5498 && it->glyph_row
5499 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5500 run_redisplay_end_trigger_hook (it);
5501
5502 /* Get the next character, maybe multibyte. */
5503 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5504 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5505 {
5506 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5507 - IT_BYTEPOS (*it));
5508 it->c = string_char_and_length (p, maxlen, &it->len);
5509 }
5510 else
5511 it->c = *p, it->len = 1;
5512
5513 /* Record what we have and where it came from. */
5514 it->what = IT_CHARACTER;;
5515 it->object = it->w->buffer;
5516 it->position = it->current.pos;
5517
5518 /* Normally we return the character found above, except when we
5519 really want to return an ellipsis for selective display. */
5520 if (it->selective)
5521 {
5522 if (it->c == '\n')
5523 {
5524 /* A value of selective > 0 means hide lines indented more
5525 than that number of columns. */
5526 if (it->selective > 0
5527 && IT_CHARPOS (*it) + 1 < ZV
5528 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5529 IT_BYTEPOS (*it) + 1,
5530 (double) it->selective)) /* iftc */
5531 {
5532 success_p = next_element_from_ellipsis (it);
5533 it->dpvec_char_len = -1;
5534 }
5535 }
5536 else if (it->c == '\r' && it->selective == -1)
5537 {
5538 /* A value of selective == -1 means that everything from the
5539 CR to the end of the line is invisible, with maybe an
5540 ellipsis displayed for it. */
5541 success_p = next_element_from_ellipsis (it);
5542 it->dpvec_char_len = -1;
5543 }
5544 }
5545 }
5546
5547 /* Value is zero if end of buffer reached. */
5548 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5549 return success_p;
5550 }
5551
5552
5553 /* Run the redisplay end trigger hook for IT. */
5554
5555 static void
5556 run_redisplay_end_trigger_hook (it)
5557 struct it *it;
5558 {
5559 Lisp_Object args[3];
5560
5561 /* IT->glyph_row should be non-null, i.e. we should be actually
5562 displaying something, or otherwise we should not run the hook. */
5563 xassert (it->glyph_row);
5564
5565 /* Set up hook arguments. */
5566 args[0] = Qredisplay_end_trigger_functions;
5567 args[1] = it->window;
5568 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5569 it->redisplay_end_trigger_charpos = 0;
5570
5571 /* Since we are *trying* to run these functions, don't try to run
5572 them again, even if they get an error. */
5573 it->w->redisplay_end_trigger = Qnil;
5574 Frun_hook_with_args (3, args);
5575
5576 /* Notice if it changed the face of the character we are on. */
5577 handle_face_prop (it);
5578 }
5579
5580
5581 /* Deliver a composition display element. The iterator IT is already
5582 filled with composition information (done in
5583 handle_composition_prop). Value is always 1. */
5584
5585 static int
5586 next_element_from_composition (it)
5587 struct it *it;
5588 {
5589 it->what = IT_COMPOSITION;
5590 it->position = (STRINGP (it->string)
5591 ? it->current.string_pos
5592 : it->current.pos);
5593 return 1;
5594 }
5595
5596
5597 \f
5598 /***********************************************************************
5599 Moving an iterator without producing glyphs
5600 ***********************************************************************/
5601
5602 /* Move iterator IT to a specified buffer or X position within one
5603 line on the display without producing glyphs.
5604
5605 OP should be a bit mask including some or all of these bits:
5606 MOVE_TO_X: Stop on reaching x-position TO_X.
5607 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5608 Regardless of OP's value, stop in reaching the end of the display line.
5609
5610 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5611 This means, in particular, that TO_X includes window's horizontal
5612 scroll amount.
5613
5614 The return value has several possible values that
5615 say what condition caused the scan to stop:
5616
5617 MOVE_POS_MATCH_OR_ZV
5618 - when TO_POS or ZV was reached.
5619
5620 MOVE_X_REACHED
5621 -when TO_X was reached before TO_POS or ZV were reached.
5622
5623 MOVE_LINE_CONTINUED
5624 - when we reached the end of the display area and the line must
5625 be continued.
5626
5627 MOVE_LINE_TRUNCATED
5628 - when we reached the end of the display area and the line is
5629 truncated.
5630
5631 MOVE_NEWLINE_OR_CR
5632 - when we stopped at a line end, i.e. a newline or a CR and selective
5633 display is on. */
5634
5635 static enum move_it_result
5636 move_it_in_display_line_to (it, to_charpos, to_x, op)
5637 struct it *it;
5638 int to_charpos, to_x, op;
5639 {
5640 enum move_it_result result = MOVE_UNDEFINED;
5641 struct glyph_row *saved_glyph_row;
5642
5643 /* Don't produce glyphs in produce_glyphs. */
5644 saved_glyph_row = it->glyph_row;
5645 it->glyph_row = NULL;
5646
5647 #define BUFFER_POS_REACHED_P() \
5648 ((op & MOVE_TO_POS) != 0 \
5649 && BUFFERP (it->object) \
5650 && IT_CHARPOS (*it) >= to_charpos)
5651
5652 while (1)
5653 {
5654 int x, i, ascent = 0, descent = 0;
5655
5656 /* Stop when ZV reached.
5657 We used to stop here when TO_CHARPOS reached as well, but that is
5658 too soon if this glyph does not fit on this line. So we handle it
5659 explicitly below. */
5660 if (!get_next_display_element (it)
5661 || (it->truncate_lines_p
5662 && BUFFER_POS_REACHED_P ()))
5663 {
5664 result = MOVE_POS_MATCH_OR_ZV;
5665 break;
5666 }
5667
5668 /* The call to produce_glyphs will get the metrics of the
5669 display element IT is loaded with. We record in x the
5670 x-position before this display element in case it does not
5671 fit on the line. */
5672 x = it->current_x;
5673
5674 /* Remember the line height so far in case the next element doesn't
5675 fit on the line. */
5676 if (!it->truncate_lines_p)
5677 {
5678 ascent = it->max_ascent;
5679 descent = it->max_descent;
5680 }
5681
5682 PRODUCE_GLYPHS (it);
5683
5684 if (it->area != TEXT_AREA)
5685 {
5686 set_iterator_to_next (it, 1);
5687 continue;
5688 }
5689
5690 /* The number of glyphs we get back in IT->nglyphs will normally
5691 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5692 character on a terminal frame, or (iii) a line end. For the
5693 second case, IT->nglyphs - 1 padding glyphs will be present
5694 (on X frames, there is only one glyph produced for a
5695 composite character.
5696
5697 The behavior implemented below means, for continuation lines,
5698 that as many spaces of a TAB as fit on the current line are
5699 displayed there. For terminal frames, as many glyphs of a
5700 multi-glyph character are displayed in the current line, too.
5701 This is what the old redisplay code did, and we keep it that
5702 way. Under X, the whole shape of a complex character must
5703 fit on the line or it will be completely displayed in the
5704 next line.
5705
5706 Note that both for tabs and padding glyphs, all glyphs have
5707 the same width. */
5708 if (it->nglyphs)
5709 {
5710 /* More than one glyph or glyph doesn't fit on line. All
5711 glyphs have the same width. */
5712 int single_glyph_width = it->pixel_width / it->nglyphs;
5713 int new_x;
5714
5715 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5716 {
5717 new_x = x + single_glyph_width;
5718
5719 /* We want to leave anything reaching TO_X to the caller. */
5720 if ((op & MOVE_TO_X) && new_x > to_x)
5721 {
5722 if (BUFFER_POS_REACHED_P ())
5723 goto buffer_pos_reached;
5724 it->current_x = x;
5725 result = MOVE_X_REACHED;
5726 break;
5727 }
5728 else if (/* Lines are continued. */
5729 !it->truncate_lines_p
5730 && (/* And glyph doesn't fit on the line. */
5731 new_x > it->last_visible_x
5732 /* Or it fits exactly and we're on a window
5733 system frame. */
5734 || (new_x == it->last_visible_x
5735 && FRAME_WINDOW_P (it->f))))
5736 {
5737 if (/* IT->hpos == 0 means the very first glyph
5738 doesn't fit on the line, e.g. a wide image. */
5739 it->hpos == 0
5740 || (new_x == it->last_visible_x
5741 && FRAME_WINDOW_P (it->f)))
5742 {
5743 ++it->hpos;
5744 it->current_x = new_x;
5745 if (i == it->nglyphs - 1)
5746 {
5747 set_iterator_to_next (it, 1);
5748 #ifdef HAVE_WINDOW_SYSTEM
5749 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5750 {
5751 if (!get_next_display_element (it))
5752 {
5753 result = MOVE_POS_MATCH_OR_ZV;
5754 break;
5755 }
5756 if (BUFFER_POS_REACHED_P ())
5757 {
5758 if (ITERATOR_AT_END_OF_LINE_P (it))
5759 result = MOVE_POS_MATCH_OR_ZV;
5760 else
5761 result = MOVE_LINE_CONTINUED;
5762 break;
5763 }
5764 if (ITERATOR_AT_END_OF_LINE_P (it))
5765 {
5766 result = MOVE_NEWLINE_OR_CR;
5767 break;
5768 }
5769 }
5770 #endif /* HAVE_WINDOW_SYSTEM */
5771 }
5772 }
5773 else
5774 {
5775 it->current_x = x;
5776 it->max_ascent = ascent;
5777 it->max_descent = descent;
5778 }
5779
5780 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5781 IT_CHARPOS (*it)));
5782 result = MOVE_LINE_CONTINUED;
5783 break;
5784 }
5785 else if (BUFFER_POS_REACHED_P ())
5786 goto buffer_pos_reached;
5787 else if (new_x > it->first_visible_x)
5788 {
5789 /* Glyph is visible. Increment number of glyphs that
5790 would be displayed. */
5791 ++it->hpos;
5792 }
5793 else
5794 {
5795 /* Glyph is completely off the left margin of the display
5796 area. Nothing to do. */
5797 }
5798 }
5799
5800 if (result != MOVE_UNDEFINED)
5801 break;
5802 }
5803 else if (BUFFER_POS_REACHED_P ())
5804 {
5805 buffer_pos_reached:
5806 it->current_x = x;
5807 it->max_ascent = ascent;
5808 it->max_descent = descent;
5809 result = MOVE_POS_MATCH_OR_ZV;
5810 break;
5811 }
5812 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5813 {
5814 /* Stop when TO_X specified and reached. This check is
5815 necessary here because of lines consisting of a line end,
5816 only. The line end will not produce any glyphs and we
5817 would never get MOVE_X_REACHED. */
5818 xassert (it->nglyphs == 0);
5819 result = MOVE_X_REACHED;
5820 break;
5821 }
5822
5823 /* Is this a line end? If yes, we're done. */
5824 if (ITERATOR_AT_END_OF_LINE_P (it))
5825 {
5826 result = MOVE_NEWLINE_OR_CR;
5827 break;
5828 }
5829
5830 /* The current display element has been consumed. Advance
5831 to the next. */
5832 set_iterator_to_next (it, 1);
5833
5834 /* Stop if lines are truncated and IT's current x-position is
5835 past the right edge of the window now. */
5836 if (it->truncate_lines_p
5837 && it->current_x >= it->last_visible_x)
5838 {
5839 #ifdef HAVE_WINDOW_SYSTEM
5840 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5841 {
5842 if (!get_next_display_element (it)
5843 || BUFFER_POS_REACHED_P ())
5844 {
5845 result = MOVE_POS_MATCH_OR_ZV;
5846 break;
5847 }
5848 if (ITERATOR_AT_END_OF_LINE_P (it))
5849 {
5850 result = MOVE_NEWLINE_OR_CR;
5851 break;
5852 }
5853 }
5854 #endif /* HAVE_WINDOW_SYSTEM */
5855 result = MOVE_LINE_TRUNCATED;
5856 break;
5857 }
5858 }
5859
5860 #undef BUFFER_POS_REACHED_P
5861
5862 /* Restore the iterator settings altered at the beginning of this
5863 function. */
5864 it->glyph_row = saved_glyph_row;
5865 return result;
5866 }
5867
5868
5869 /* Move IT forward until it satisfies one or more of the criteria in
5870 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5871
5872 OP is a bit-mask that specifies where to stop, and in particular,
5873 which of those four position arguments makes a difference. See the
5874 description of enum move_operation_enum.
5875
5876 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5877 screen line, this function will set IT to the next position >
5878 TO_CHARPOS. */
5879
5880 void
5881 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5882 struct it *it;
5883 int to_charpos, to_x, to_y, to_vpos;
5884 int op;
5885 {
5886 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5887 int line_height;
5888 int reached = 0;
5889
5890 for (;;)
5891 {
5892 if (op & MOVE_TO_VPOS)
5893 {
5894 /* If no TO_CHARPOS and no TO_X specified, stop at the
5895 start of the line TO_VPOS. */
5896 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5897 {
5898 if (it->vpos == to_vpos)
5899 {
5900 reached = 1;
5901 break;
5902 }
5903 else
5904 skip = move_it_in_display_line_to (it, -1, -1, 0);
5905 }
5906 else
5907 {
5908 /* TO_VPOS >= 0 means stop at TO_X in the line at
5909 TO_VPOS, or at TO_POS, whichever comes first. */
5910 if (it->vpos == to_vpos)
5911 {
5912 reached = 2;
5913 break;
5914 }
5915
5916 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5917
5918 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5919 {
5920 reached = 3;
5921 break;
5922 }
5923 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5924 {
5925 /* We have reached TO_X but not in the line we want. */
5926 skip = move_it_in_display_line_to (it, to_charpos,
5927 -1, MOVE_TO_POS);
5928 if (skip == MOVE_POS_MATCH_OR_ZV)
5929 {
5930 reached = 4;
5931 break;
5932 }
5933 }
5934 }
5935 }
5936 else if (op & MOVE_TO_Y)
5937 {
5938 struct it it_backup;
5939
5940 /* TO_Y specified means stop at TO_X in the line containing
5941 TO_Y---or at TO_CHARPOS if this is reached first. The
5942 problem is that we can't really tell whether the line
5943 contains TO_Y before we have completely scanned it, and
5944 this may skip past TO_X. What we do is to first scan to
5945 TO_X.
5946
5947 If TO_X is not specified, use a TO_X of zero. The reason
5948 is to make the outcome of this function more predictable.
5949 If we didn't use TO_X == 0, we would stop at the end of
5950 the line which is probably not what a caller would expect
5951 to happen. */
5952 skip = move_it_in_display_line_to (it, to_charpos,
5953 ((op & MOVE_TO_X)
5954 ? to_x : 0),
5955 (MOVE_TO_X
5956 | (op & MOVE_TO_POS)));
5957
5958 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5959 if (skip == MOVE_POS_MATCH_OR_ZV)
5960 {
5961 reached = 5;
5962 break;
5963 }
5964
5965 /* If TO_X was reached, we would like to know whether TO_Y
5966 is in the line. This can only be said if we know the
5967 total line height which requires us to scan the rest of
5968 the line. */
5969 if (skip == MOVE_X_REACHED)
5970 {
5971 it_backup = *it;
5972 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5973 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5974 op & MOVE_TO_POS);
5975 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5976 }
5977
5978 /* Now, decide whether TO_Y is in this line. */
5979 line_height = it->max_ascent + it->max_descent;
5980 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5981
5982 if (to_y >= it->current_y
5983 && to_y < it->current_y + line_height)
5984 {
5985 if (skip == MOVE_X_REACHED)
5986 /* If TO_Y is in this line and TO_X was reached above,
5987 we scanned too far. We have to restore IT's settings
5988 to the ones before skipping. */
5989 *it = it_backup;
5990 reached = 6;
5991 }
5992 else if (skip == MOVE_X_REACHED)
5993 {
5994 skip = skip2;
5995 if (skip == MOVE_POS_MATCH_OR_ZV)
5996 reached = 7;
5997 }
5998
5999 if (reached)
6000 break;
6001 }
6002 else
6003 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6004
6005 switch (skip)
6006 {
6007 case MOVE_POS_MATCH_OR_ZV:
6008 reached = 8;
6009 goto out;
6010
6011 case MOVE_NEWLINE_OR_CR:
6012 set_iterator_to_next (it, 1);
6013 it->continuation_lines_width = 0;
6014 break;
6015
6016 case MOVE_LINE_TRUNCATED:
6017 it->continuation_lines_width = 0;
6018 reseat_at_next_visible_line_start (it, 0);
6019 if ((op & MOVE_TO_POS) != 0
6020 && IT_CHARPOS (*it) > to_charpos)
6021 {
6022 reached = 9;
6023 goto out;
6024 }
6025 break;
6026
6027 case MOVE_LINE_CONTINUED:
6028 it->continuation_lines_width += it->current_x;
6029 break;
6030
6031 default:
6032 abort ();
6033 }
6034
6035 /* Reset/increment for the next run. */
6036 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6037 it->current_x = it->hpos = 0;
6038 it->current_y += it->max_ascent + it->max_descent;
6039 ++it->vpos;
6040 last_height = it->max_ascent + it->max_descent;
6041 last_max_ascent = it->max_ascent;
6042 it->max_ascent = it->max_descent = 0;
6043 }
6044
6045 out:
6046
6047 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6048 }
6049
6050
6051 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6052
6053 If DY > 0, move IT backward at least that many pixels. DY = 0
6054 means move IT backward to the preceding line start or BEGV. This
6055 function may move over more than DY pixels if IT->current_y - DY
6056 ends up in the middle of a line; in this case IT->current_y will be
6057 set to the top of the line moved to. */
6058
6059 void
6060 move_it_vertically_backward (it, dy)
6061 struct it *it;
6062 int dy;
6063 {
6064 int nlines, h;
6065 struct it it2, it3;
6066 int start_pos = IT_CHARPOS (*it);
6067
6068 xassert (dy >= 0);
6069
6070 /* Estimate how many newlines we must move back. */
6071 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6072
6073 /* Set the iterator's position that many lines back. */
6074 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6075 back_to_previous_visible_line_start (it);
6076
6077 /* Reseat the iterator here. When moving backward, we don't want
6078 reseat to skip forward over invisible text, set up the iterator
6079 to deliver from overlay strings at the new position etc. So,
6080 use reseat_1 here. */
6081 reseat_1 (it, it->current.pos, 1);
6082
6083 /* We are now surely at a line start. */
6084 it->current_x = it->hpos = 0;
6085 it->continuation_lines_width = 0;
6086
6087 /* Move forward and see what y-distance we moved. First move to the
6088 start of the next line so that we get its height. We need this
6089 height to be able to tell whether we reached the specified
6090 y-distance. */
6091 it2 = *it;
6092 it2.max_ascent = it2.max_descent = 0;
6093 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6094 MOVE_TO_POS | MOVE_TO_VPOS);
6095 xassert (IT_CHARPOS (*it) >= BEGV);
6096 it3 = it2;
6097
6098 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6099 xassert (IT_CHARPOS (*it) >= BEGV);
6100 /* H is the actual vertical distance from the position in *IT
6101 and the starting position. */
6102 h = it2.current_y - it->current_y;
6103 /* NLINES is the distance in number of lines. */
6104 nlines = it2.vpos - it->vpos;
6105
6106 /* Correct IT's y and vpos position
6107 so that they are relative to the starting point. */
6108 it->vpos -= nlines;
6109 it->current_y -= h;
6110
6111 if (dy == 0)
6112 {
6113 /* DY == 0 means move to the start of the screen line. The
6114 value of nlines is > 0 if continuation lines were involved. */
6115 if (nlines > 0)
6116 move_it_by_lines (it, nlines, 1);
6117 xassert (IT_CHARPOS (*it) <= start_pos);
6118 }
6119 else
6120 {
6121 /* The y-position we try to reach, relative to *IT.
6122 Note that H has been subtracted in front of the if-statement. */
6123 int target_y = it->current_y + h - dy;
6124 int y0 = it3.current_y;
6125 int y1 = line_bottom_y (&it3);
6126 int line_height = y1 - y0;
6127
6128 /* If we did not reach target_y, try to move further backward if
6129 we can. If we moved too far backward, try to move forward. */
6130 if (target_y < it->current_y
6131 /* This is heuristic. In a window that's 3 lines high, with
6132 a line height of 13 pixels each, recentering with point
6133 on the bottom line will try to move -39/2 = 19 pixels
6134 backward. Try to avoid moving into the first line. */
6135 && it->current_y - target_y > line_height / 3 * 2
6136 && IT_CHARPOS (*it) > BEGV)
6137 {
6138 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6139 target_y - it->current_y));
6140 move_it_vertically (it, target_y - it->current_y);
6141 xassert (IT_CHARPOS (*it) >= BEGV);
6142 }
6143 else if (target_y >= it->current_y + line_height
6144 && IT_CHARPOS (*it) < ZV)
6145 {
6146 /* Should move forward by at least one line, maybe more.
6147
6148 Note: Calling move_it_by_lines can be expensive on
6149 terminal frames, where compute_motion is used (via
6150 vmotion) to do the job, when there are very long lines
6151 and truncate-lines is nil. That's the reason for
6152 treating terminal frames specially here. */
6153
6154 if (!FRAME_WINDOW_P (it->f))
6155 move_it_vertically (it, target_y - (it->current_y + line_height));
6156 else
6157 {
6158 do
6159 {
6160 move_it_by_lines (it, 1, 1);
6161 }
6162 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6163 }
6164
6165 xassert (IT_CHARPOS (*it) >= BEGV);
6166 }
6167 }
6168 }
6169
6170
6171 /* Move IT by a specified amount of pixel lines DY. DY negative means
6172 move backwards. DY = 0 means move to start of screen line. At the
6173 end, IT will be on the start of a screen line. */
6174
6175 void
6176 move_it_vertically (it, dy)
6177 struct it *it;
6178 int dy;
6179 {
6180 if (dy <= 0)
6181 move_it_vertically_backward (it, -dy);
6182 else if (dy > 0)
6183 {
6184 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6185 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6186 MOVE_TO_POS | MOVE_TO_Y);
6187 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6188
6189 /* If buffer ends in ZV without a newline, move to the start of
6190 the line to satisfy the post-condition. */
6191 if (IT_CHARPOS (*it) == ZV
6192 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6193 move_it_by_lines (it, 0, 0);
6194 }
6195 }
6196
6197
6198 /* Move iterator IT past the end of the text line it is in. */
6199
6200 void
6201 move_it_past_eol (it)
6202 struct it *it;
6203 {
6204 enum move_it_result rc;
6205
6206 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6207 if (rc == MOVE_NEWLINE_OR_CR)
6208 set_iterator_to_next (it, 0);
6209 }
6210
6211
6212 #if 0 /* Currently not used. */
6213
6214 /* Return non-zero if some text between buffer positions START_CHARPOS
6215 and END_CHARPOS is invisible. IT->window is the window for text
6216 property lookup. */
6217
6218 static int
6219 invisible_text_between_p (it, start_charpos, end_charpos)
6220 struct it *it;
6221 int start_charpos, end_charpos;
6222 {
6223 Lisp_Object prop, limit;
6224 int invisible_found_p;
6225
6226 xassert (it != NULL && start_charpos <= end_charpos);
6227
6228 /* Is text at START invisible? */
6229 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6230 it->window);
6231 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6232 invisible_found_p = 1;
6233 else
6234 {
6235 limit = Fnext_single_char_property_change (make_number (start_charpos),
6236 Qinvisible, Qnil,
6237 make_number (end_charpos));
6238 invisible_found_p = XFASTINT (limit) < end_charpos;
6239 }
6240
6241 return invisible_found_p;
6242 }
6243
6244 #endif /* 0 */
6245
6246
6247 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6248 negative means move up. DVPOS == 0 means move to the start of the
6249 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6250 NEED_Y_P is zero, IT->current_y will be left unchanged.
6251
6252 Further optimization ideas: If we would know that IT->f doesn't use
6253 a face with proportional font, we could be faster for
6254 truncate-lines nil. */
6255
6256 void
6257 move_it_by_lines (it, dvpos, need_y_p)
6258 struct it *it;
6259 int dvpos, need_y_p;
6260 {
6261 struct position pos;
6262
6263 if (!FRAME_WINDOW_P (it->f))
6264 {
6265 struct text_pos textpos;
6266
6267 /* We can use vmotion on frames without proportional fonts. */
6268 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6269 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6270 reseat (it, textpos, 1);
6271 it->vpos += pos.vpos;
6272 it->current_y += pos.vpos;
6273 }
6274 else if (dvpos == 0)
6275 {
6276 /* DVPOS == 0 means move to the start of the screen line. */
6277 move_it_vertically_backward (it, 0);
6278 xassert (it->current_x == 0 && it->hpos == 0);
6279 }
6280 else if (dvpos > 0)
6281 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6282 else
6283 {
6284 struct it it2;
6285 int start_charpos, i;
6286
6287 /* Start at the beginning of the screen line containing IT's
6288 position. */
6289 move_it_vertically_backward (it, 0);
6290
6291 /* Go back -DVPOS visible lines and reseat the iterator there. */
6292 start_charpos = IT_CHARPOS (*it);
6293 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6294 back_to_previous_visible_line_start (it);
6295 reseat (it, it->current.pos, 1);
6296 it->current_x = it->hpos = 0;
6297
6298 /* Above call may have moved too far if continuation lines
6299 are involved. Scan forward and see if it did. */
6300 it2 = *it;
6301 it2.vpos = it2.current_y = 0;
6302 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6303 it->vpos -= it2.vpos;
6304 it->current_y -= it2.current_y;
6305 it->current_x = it->hpos = 0;
6306
6307 /* If we moved too far, move IT some lines forward. */
6308 if (it2.vpos > -dvpos)
6309 {
6310 int delta = it2.vpos + dvpos;
6311 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6312 }
6313 }
6314 }
6315
6316 /* Return 1 if IT points into the middle of a display vector. */
6317
6318 int
6319 in_display_vector_p (it)
6320 struct it *it;
6321 {
6322 return (it->method == next_element_from_display_vector
6323 && it->current.dpvec_index > 0
6324 && it->dpvec + it->current.dpvec_index != it->dpend);
6325 }
6326
6327 \f
6328 /***********************************************************************
6329 Messages
6330 ***********************************************************************/
6331
6332
6333 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6334 to *Messages*. */
6335
6336 void
6337 add_to_log (format, arg1, arg2)
6338 char *format;
6339 Lisp_Object arg1, arg2;
6340 {
6341 Lisp_Object args[3];
6342 Lisp_Object msg, fmt;
6343 char *buffer;
6344 int len;
6345 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6346 USE_SAFE_ALLOCA;
6347
6348 /* Do nothing if called asynchronously. Inserting text into
6349 a buffer may call after-change-functions and alike and
6350 that would means running Lisp asynchronously. */
6351 if (handling_signal)
6352 return;
6353
6354 fmt = msg = Qnil;
6355 GCPRO4 (fmt, msg, arg1, arg2);
6356
6357 args[0] = fmt = build_string (format);
6358 args[1] = arg1;
6359 args[2] = arg2;
6360 msg = Fformat (3, args);
6361
6362 len = SBYTES (msg) + 1;
6363 SAFE_ALLOCA (buffer, char *, len);
6364 bcopy (SDATA (msg), buffer, len);
6365
6366 message_dolog (buffer, len - 1, 1, 0);
6367 SAFE_FREE (len);
6368
6369 UNGCPRO;
6370 }
6371
6372
6373 /* Output a newline in the *Messages* buffer if "needs" one. */
6374
6375 void
6376 message_log_maybe_newline ()
6377 {
6378 if (message_log_need_newline)
6379 message_dolog ("", 0, 1, 0);
6380 }
6381
6382
6383 /* Add a string M of length NBYTES to the message log, optionally
6384 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6385 nonzero, means interpret the contents of M as multibyte. This
6386 function calls low-level routines in order to bypass text property
6387 hooks, etc. which might not be safe to run. */
6388
6389 void
6390 message_dolog (m, nbytes, nlflag, multibyte)
6391 const char *m;
6392 int nbytes, nlflag, multibyte;
6393 {
6394 if (!NILP (Vmemory_full))
6395 return;
6396
6397 if (!NILP (Vmessage_log_max))
6398 {
6399 struct buffer *oldbuf;
6400 Lisp_Object oldpoint, oldbegv, oldzv;
6401 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6402 int point_at_end = 0;
6403 int zv_at_end = 0;
6404 Lisp_Object old_deactivate_mark, tem;
6405 struct gcpro gcpro1;
6406
6407 old_deactivate_mark = Vdeactivate_mark;
6408 oldbuf = current_buffer;
6409 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6410 current_buffer->undo_list = Qt;
6411
6412 oldpoint = message_dolog_marker1;
6413 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6414 oldbegv = message_dolog_marker2;
6415 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6416 oldzv = message_dolog_marker3;
6417 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6418 GCPRO1 (old_deactivate_mark);
6419
6420 if (PT == Z)
6421 point_at_end = 1;
6422 if (ZV == Z)
6423 zv_at_end = 1;
6424
6425 BEGV = BEG;
6426 BEGV_BYTE = BEG_BYTE;
6427 ZV = Z;
6428 ZV_BYTE = Z_BYTE;
6429 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6430
6431 /* Insert the string--maybe converting multibyte to single byte
6432 or vice versa, so that all the text fits the buffer. */
6433 if (multibyte
6434 && NILP (current_buffer->enable_multibyte_characters))
6435 {
6436 int i, c, char_bytes;
6437 unsigned char work[1];
6438
6439 /* Convert a multibyte string to single-byte
6440 for the *Message* buffer. */
6441 for (i = 0; i < nbytes; i += char_bytes)
6442 {
6443 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6444 work[0] = (SINGLE_BYTE_CHAR_P (c)
6445 ? c
6446 : multibyte_char_to_unibyte (c, Qnil));
6447 insert_1_both (work, 1, 1, 1, 0, 0);
6448 }
6449 }
6450 else if (! multibyte
6451 && ! NILP (current_buffer->enable_multibyte_characters))
6452 {
6453 int i, c, char_bytes;
6454 unsigned char *msg = (unsigned char *) m;
6455 unsigned char str[MAX_MULTIBYTE_LENGTH];
6456 /* Convert a single-byte string to multibyte
6457 for the *Message* buffer. */
6458 for (i = 0; i < nbytes; i++)
6459 {
6460 c = unibyte_char_to_multibyte (msg[i]);
6461 char_bytes = CHAR_STRING (c, str);
6462 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6463 }
6464 }
6465 else if (nbytes)
6466 insert_1 (m, nbytes, 1, 0, 0);
6467
6468 if (nlflag)
6469 {
6470 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6471 insert_1 ("\n", 1, 1, 0, 0);
6472
6473 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6474 this_bol = PT;
6475 this_bol_byte = PT_BYTE;
6476
6477 /* See if this line duplicates the previous one.
6478 If so, combine duplicates. */
6479 if (this_bol > BEG)
6480 {
6481 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6482 prev_bol = PT;
6483 prev_bol_byte = PT_BYTE;
6484
6485 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6486 this_bol, this_bol_byte);
6487 if (dup)
6488 {
6489 del_range_both (prev_bol, prev_bol_byte,
6490 this_bol, this_bol_byte, 0);
6491 if (dup > 1)
6492 {
6493 char dupstr[40];
6494 int duplen;
6495
6496 /* If you change this format, don't forget to also
6497 change message_log_check_duplicate. */
6498 sprintf (dupstr, " [%d times]", dup);
6499 duplen = strlen (dupstr);
6500 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6501 insert_1 (dupstr, duplen, 1, 0, 1);
6502 }
6503 }
6504 }
6505
6506 /* If we have more than the desired maximum number of lines
6507 in the *Messages* buffer now, delete the oldest ones.
6508 This is safe because we don't have undo in this buffer. */
6509
6510 if (NATNUMP (Vmessage_log_max))
6511 {
6512 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6513 -XFASTINT (Vmessage_log_max) - 1, 0);
6514 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6515 }
6516 }
6517 BEGV = XMARKER (oldbegv)->charpos;
6518 BEGV_BYTE = marker_byte_position (oldbegv);
6519
6520 if (zv_at_end)
6521 {
6522 ZV = Z;
6523 ZV_BYTE = Z_BYTE;
6524 }
6525 else
6526 {
6527 ZV = XMARKER (oldzv)->charpos;
6528 ZV_BYTE = marker_byte_position (oldzv);
6529 }
6530
6531 if (point_at_end)
6532 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6533 else
6534 /* We can't do Fgoto_char (oldpoint) because it will run some
6535 Lisp code. */
6536 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6537 XMARKER (oldpoint)->bytepos);
6538
6539 UNGCPRO;
6540 unchain_marker (XMARKER (oldpoint));
6541 unchain_marker (XMARKER (oldbegv));
6542 unchain_marker (XMARKER (oldzv));
6543
6544 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6545 set_buffer_internal (oldbuf);
6546 if (NILP (tem))
6547 windows_or_buffers_changed = old_windows_or_buffers_changed;
6548 message_log_need_newline = !nlflag;
6549 Vdeactivate_mark = old_deactivate_mark;
6550 }
6551 }
6552
6553
6554 /* We are at the end of the buffer after just having inserted a newline.
6555 (Note: We depend on the fact we won't be crossing the gap.)
6556 Check to see if the most recent message looks a lot like the previous one.
6557 Return 0 if different, 1 if the new one should just replace it, or a
6558 value N > 1 if we should also append " [N times]". */
6559
6560 static int
6561 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6562 int prev_bol, this_bol;
6563 int prev_bol_byte, this_bol_byte;
6564 {
6565 int i;
6566 int len = Z_BYTE - 1 - this_bol_byte;
6567 int seen_dots = 0;
6568 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6569 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6570
6571 for (i = 0; i < len; i++)
6572 {
6573 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6574 seen_dots = 1;
6575 if (p1[i] != p2[i])
6576 return seen_dots;
6577 }
6578 p1 += len;
6579 if (*p1 == '\n')
6580 return 2;
6581 if (*p1++ == ' ' && *p1++ == '[')
6582 {
6583 int n = 0;
6584 while (*p1 >= '0' && *p1 <= '9')
6585 n = n * 10 + *p1++ - '0';
6586 if (strncmp (p1, " times]\n", 8) == 0)
6587 return n+1;
6588 }
6589 return 0;
6590 }
6591
6592
6593 /* Display an echo area message M with a specified length of NBYTES
6594 bytes. The string may include null characters. If M is 0, clear
6595 out any existing message, and let the mini-buffer text show
6596 through.
6597
6598 The buffer M must continue to exist until after the echo area gets
6599 cleared or some other message gets displayed there. This means do
6600 not pass text that is stored in a Lisp string; do not pass text in
6601 a buffer that was alloca'd. */
6602
6603 void
6604 message2 (m, nbytes, multibyte)
6605 const char *m;
6606 int nbytes;
6607 int multibyte;
6608 {
6609 /* First flush out any partial line written with print. */
6610 message_log_maybe_newline ();
6611 if (m)
6612 message_dolog (m, nbytes, 1, multibyte);
6613 message2_nolog (m, nbytes, multibyte);
6614 }
6615
6616
6617 /* The non-logging counterpart of message2. */
6618
6619 void
6620 message2_nolog (m, nbytes, multibyte)
6621 const char *m;
6622 int nbytes, multibyte;
6623 {
6624 struct frame *sf = SELECTED_FRAME ();
6625 message_enable_multibyte = multibyte;
6626
6627 if (noninteractive)
6628 {
6629 if (noninteractive_need_newline)
6630 putc ('\n', stderr);
6631 noninteractive_need_newline = 0;
6632 if (m)
6633 fwrite (m, nbytes, 1, stderr);
6634 if (cursor_in_echo_area == 0)
6635 fprintf (stderr, "\n");
6636 fflush (stderr);
6637 }
6638 /* A null message buffer means that the frame hasn't really been
6639 initialized yet. Error messages get reported properly by
6640 cmd_error, so this must be just an informative message; toss it. */
6641 else if (INTERACTIVE
6642 && sf->glyphs_initialized_p
6643 && FRAME_MESSAGE_BUF (sf))
6644 {
6645 Lisp_Object mini_window;
6646 struct frame *f;
6647
6648 /* Get the frame containing the mini-buffer
6649 that the selected frame is using. */
6650 mini_window = FRAME_MINIBUF_WINDOW (sf);
6651 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6652
6653 FRAME_SAMPLE_VISIBILITY (f);
6654 if (FRAME_VISIBLE_P (sf)
6655 && ! FRAME_VISIBLE_P (f))
6656 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6657
6658 if (m)
6659 {
6660 set_message (m, Qnil, nbytes, multibyte);
6661 if (minibuffer_auto_raise)
6662 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6663 }
6664 else
6665 clear_message (1, 1);
6666
6667 do_pending_window_change (0);
6668 echo_area_display (1);
6669 do_pending_window_change (0);
6670 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6671 (*frame_up_to_date_hook) (f);
6672 }
6673 }
6674
6675
6676 /* Display an echo area message M with a specified length of NBYTES
6677 bytes. The string may include null characters. If M is not a
6678 string, clear out any existing message, and let the mini-buffer
6679 text show through. */
6680
6681 void
6682 message3 (m, nbytes, multibyte)
6683 Lisp_Object m;
6684 int nbytes;
6685 int multibyte;
6686 {
6687 struct gcpro gcpro1;
6688
6689 GCPRO1 (m);
6690
6691 /* First flush out any partial line written with print. */
6692 message_log_maybe_newline ();
6693 if (STRINGP (m))
6694 message_dolog (SDATA (m), nbytes, 1, multibyte);
6695 message3_nolog (m, nbytes, multibyte);
6696
6697 UNGCPRO;
6698 }
6699
6700
6701 /* The non-logging version of message3. */
6702
6703 void
6704 message3_nolog (m, nbytes, multibyte)
6705 Lisp_Object m;
6706 int nbytes, multibyte;
6707 {
6708 struct frame *sf = SELECTED_FRAME ();
6709 message_enable_multibyte = multibyte;
6710
6711 if (noninteractive)
6712 {
6713 if (noninteractive_need_newline)
6714 putc ('\n', stderr);
6715 noninteractive_need_newline = 0;
6716 if (STRINGP (m))
6717 fwrite (SDATA (m), nbytes, 1, stderr);
6718 if (cursor_in_echo_area == 0)
6719 fprintf (stderr, "\n");
6720 fflush (stderr);
6721 }
6722 /* A null message buffer means that the frame hasn't really been
6723 initialized yet. Error messages get reported properly by
6724 cmd_error, so this must be just an informative message; toss it. */
6725 else if (INTERACTIVE
6726 && sf->glyphs_initialized_p
6727 && FRAME_MESSAGE_BUF (sf))
6728 {
6729 Lisp_Object mini_window;
6730 Lisp_Object frame;
6731 struct frame *f;
6732
6733 /* Get the frame containing the mini-buffer
6734 that the selected frame is using. */
6735 mini_window = FRAME_MINIBUF_WINDOW (sf);
6736 frame = XWINDOW (mini_window)->frame;
6737 f = XFRAME (frame);
6738
6739 FRAME_SAMPLE_VISIBILITY (f);
6740 if (FRAME_VISIBLE_P (sf)
6741 && !FRAME_VISIBLE_P (f))
6742 Fmake_frame_visible (frame);
6743
6744 if (STRINGP (m) && SCHARS (m) > 0)
6745 {
6746 set_message (NULL, m, nbytes, multibyte);
6747 if (minibuffer_auto_raise)
6748 Fraise_frame (frame);
6749 }
6750 else
6751 clear_message (1, 1);
6752
6753 do_pending_window_change (0);
6754 echo_area_display (1);
6755 do_pending_window_change (0);
6756 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6757 (*frame_up_to_date_hook) (f);
6758 }
6759 }
6760
6761
6762 /* Display a null-terminated echo area message M. If M is 0, clear
6763 out any existing message, and let the mini-buffer text show through.
6764
6765 The buffer M must continue to exist until after the echo area gets
6766 cleared or some other message gets displayed there. Do not pass
6767 text that is stored in a Lisp string. Do not pass text in a buffer
6768 that was alloca'd. */
6769
6770 void
6771 message1 (m)
6772 char *m;
6773 {
6774 message2 (m, (m ? strlen (m) : 0), 0);
6775 }
6776
6777
6778 /* The non-logging counterpart of message1. */
6779
6780 void
6781 message1_nolog (m)
6782 char *m;
6783 {
6784 message2_nolog (m, (m ? strlen (m) : 0), 0);
6785 }
6786
6787 /* Display a message M which contains a single %s
6788 which gets replaced with STRING. */
6789
6790 void
6791 message_with_string (m, string, log)
6792 char *m;
6793 Lisp_Object string;
6794 int log;
6795 {
6796 CHECK_STRING (string);
6797
6798 if (noninteractive)
6799 {
6800 if (m)
6801 {
6802 if (noninteractive_need_newline)
6803 putc ('\n', stderr);
6804 noninteractive_need_newline = 0;
6805 fprintf (stderr, m, SDATA (string));
6806 if (cursor_in_echo_area == 0)
6807 fprintf (stderr, "\n");
6808 fflush (stderr);
6809 }
6810 }
6811 else if (INTERACTIVE)
6812 {
6813 /* The frame whose minibuffer we're going to display the message on.
6814 It may be larger than the selected frame, so we need
6815 to use its buffer, not the selected frame's buffer. */
6816 Lisp_Object mini_window;
6817 struct frame *f, *sf = SELECTED_FRAME ();
6818
6819 /* Get the frame containing the minibuffer
6820 that the selected frame is using. */
6821 mini_window = FRAME_MINIBUF_WINDOW (sf);
6822 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6823
6824 /* A null message buffer means that the frame hasn't really been
6825 initialized yet. Error messages get reported properly by
6826 cmd_error, so this must be just an informative message; toss it. */
6827 if (FRAME_MESSAGE_BUF (f))
6828 {
6829 Lisp_Object args[2], message;
6830 struct gcpro gcpro1, gcpro2;
6831
6832 args[0] = build_string (m);
6833 args[1] = message = string;
6834 GCPRO2 (args[0], message);
6835 gcpro1.nvars = 2;
6836
6837 message = Fformat (2, args);
6838
6839 if (log)
6840 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6841 else
6842 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6843
6844 UNGCPRO;
6845
6846 /* Print should start at the beginning of the message
6847 buffer next time. */
6848 message_buf_print = 0;
6849 }
6850 }
6851 }
6852
6853
6854 /* Dump an informative message to the minibuf. If M is 0, clear out
6855 any existing message, and let the mini-buffer text show through. */
6856
6857 /* VARARGS 1 */
6858 void
6859 message (m, a1, a2, a3)
6860 char *m;
6861 EMACS_INT a1, a2, a3;
6862 {
6863 if (noninteractive)
6864 {
6865 if (m)
6866 {
6867 if (noninteractive_need_newline)
6868 putc ('\n', stderr);
6869 noninteractive_need_newline = 0;
6870 fprintf (stderr, m, a1, a2, a3);
6871 if (cursor_in_echo_area == 0)
6872 fprintf (stderr, "\n");
6873 fflush (stderr);
6874 }
6875 }
6876 else if (INTERACTIVE)
6877 {
6878 /* The frame whose mini-buffer we're going to display the message
6879 on. It may be larger than the selected frame, so we need to
6880 use its buffer, not the selected frame's buffer. */
6881 Lisp_Object mini_window;
6882 struct frame *f, *sf = SELECTED_FRAME ();
6883
6884 /* Get the frame containing the mini-buffer
6885 that the selected frame is using. */
6886 mini_window = FRAME_MINIBUF_WINDOW (sf);
6887 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6888
6889 /* A null message buffer means that the frame hasn't really been
6890 initialized yet. Error messages get reported properly by
6891 cmd_error, so this must be just an informative message; toss
6892 it. */
6893 if (FRAME_MESSAGE_BUF (f))
6894 {
6895 if (m)
6896 {
6897 int len;
6898 #ifdef NO_ARG_ARRAY
6899 char *a[3];
6900 a[0] = (char *) a1;
6901 a[1] = (char *) a2;
6902 a[2] = (char *) a3;
6903
6904 len = doprnt (FRAME_MESSAGE_BUF (f),
6905 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6906 #else
6907 len = doprnt (FRAME_MESSAGE_BUF (f),
6908 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6909 (char **) &a1);
6910 #endif /* NO_ARG_ARRAY */
6911
6912 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6913 }
6914 else
6915 message1 (0);
6916
6917 /* Print should start at the beginning of the message
6918 buffer next time. */
6919 message_buf_print = 0;
6920 }
6921 }
6922 }
6923
6924
6925 /* The non-logging version of message. */
6926
6927 void
6928 message_nolog (m, a1, a2, a3)
6929 char *m;
6930 EMACS_INT a1, a2, a3;
6931 {
6932 Lisp_Object old_log_max;
6933 old_log_max = Vmessage_log_max;
6934 Vmessage_log_max = Qnil;
6935 message (m, a1, a2, a3);
6936 Vmessage_log_max = old_log_max;
6937 }
6938
6939
6940 /* Display the current message in the current mini-buffer. This is
6941 only called from error handlers in process.c, and is not time
6942 critical. */
6943
6944 void
6945 update_echo_area ()
6946 {
6947 if (!NILP (echo_area_buffer[0]))
6948 {
6949 Lisp_Object string;
6950 string = Fcurrent_message ();
6951 message3 (string, SBYTES (string),
6952 !NILP (current_buffer->enable_multibyte_characters));
6953 }
6954 }
6955
6956
6957 /* Make sure echo area buffers in `echo_buffers' are live.
6958 If they aren't, make new ones. */
6959
6960 static void
6961 ensure_echo_area_buffers ()
6962 {
6963 int i;
6964
6965 for (i = 0; i < 2; ++i)
6966 if (!BUFFERP (echo_buffer[i])
6967 || NILP (XBUFFER (echo_buffer[i])->name))
6968 {
6969 char name[30];
6970 Lisp_Object old_buffer;
6971 int j;
6972
6973 old_buffer = echo_buffer[i];
6974 sprintf (name, " *Echo Area %d*", i);
6975 echo_buffer[i] = Fget_buffer_create (build_string (name));
6976 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6977
6978 for (j = 0; j < 2; ++j)
6979 if (EQ (old_buffer, echo_area_buffer[j]))
6980 echo_area_buffer[j] = echo_buffer[i];
6981 }
6982 }
6983
6984
6985 /* Call FN with args A1..A4 with either the current or last displayed
6986 echo_area_buffer as current buffer.
6987
6988 WHICH zero means use the current message buffer
6989 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6990 from echo_buffer[] and clear it.
6991
6992 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6993 suitable buffer from echo_buffer[] and clear it.
6994
6995 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6996 that the current message becomes the last displayed one, make
6997 choose a suitable buffer for echo_area_buffer[0], and clear it.
6998
6999 Value is what FN returns. */
7000
7001 static int
7002 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7003 struct window *w;
7004 int which;
7005 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7006 EMACS_INT a1;
7007 Lisp_Object a2;
7008 EMACS_INT a3, a4;
7009 {
7010 Lisp_Object buffer;
7011 int this_one, the_other, clear_buffer_p, rc;
7012 int count = SPECPDL_INDEX ();
7013
7014 /* If buffers aren't live, make new ones. */
7015 ensure_echo_area_buffers ();
7016
7017 clear_buffer_p = 0;
7018
7019 if (which == 0)
7020 this_one = 0, the_other = 1;
7021 else if (which > 0)
7022 this_one = 1, the_other = 0;
7023 else
7024 {
7025 this_one = 0, the_other = 1;
7026 clear_buffer_p = 1;
7027
7028 /* We need a fresh one in case the current echo buffer equals
7029 the one containing the last displayed echo area message. */
7030 if (!NILP (echo_area_buffer[this_one])
7031 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
7032 echo_area_buffer[this_one] = Qnil;
7033 }
7034
7035 /* Choose a suitable buffer from echo_buffer[] is we don't
7036 have one. */
7037 if (NILP (echo_area_buffer[this_one]))
7038 {
7039 echo_area_buffer[this_one]
7040 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7041 ? echo_buffer[the_other]
7042 : echo_buffer[this_one]);
7043 clear_buffer_p = 1;
7044 }
7045
7046 buffer = echo_area_buffer[this_one];
7047
7048 /* Don't get confused by reusing the buffer used for echoing
7049 for a different purpose. */
7050 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7051 cancel_echoing ();
7052
7053 record_unwind_protect (unwind_with_echo_area_buffer,
7054 with_echo_area_buffer_unwind_data (w));
7055
7056 /* Make the echo area buffer current. Note that for display
7057 purposes, it is not necessary that the displayed window's buffer
7058 == current_buffer, except for text property lookup. So, let's
7059 only set that buffer temporarily here without doing a full
7060 Fset_window_buffer. We must also change w->pointm, though,
7061 because otherwise an assertions in unshow_buffer fails, and Emacs
7062 aborts. */
7063 set_buffer_internal_1 (XBUFFER (buffer));
7064 if (w)
7065 {
7066 w->buffer = buffer;
7067 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7068 }
7069
7070 current_buffer->undo_list = Qt;
7071 current_buffer->read_only = Qnil;
7072 specbind (Qinhibit_read_only, Qt);
7073 specbind (Qinhibit_modification_hooks, Qt);
7074
7075 if (clear_buffer_p && Z > BEG)
7076 del_range (BEG, Z);
7077
7078 xassert (BEGV >= BEG);
7079 xassert (ZV <= Z && ZV >= BEGV);
7080
7081 rc = fn (a1, a2, a3, a4);
7082
7083 xassert (BEGV >= BEG);
7084 xassert (ZV <= Z && ZV >= BEGV);
7085
7086 unbind_to (count, Qnil);
7087 return rc;
7088 }
7089
7090
7091 /* Save state that should be preserved around the call to the function
7092 FN called in with_echo_area_buffer. */
7093
7094 static Lisp_Object
7095 with_echo_area_buffer_unwind_data (w)
7096 struct window *w;
7097 {
7098 int i = 0;
7099 Lisp_Object vector;
7100
7101 /* Reduce consing by keeping one vector in
7102 Vwith_echo_area_save_vector. */
7103 vector = Vwith_echo_area_save_vector;
7104 Vwith_echo_area_save_vector = Qnil;
7105
7106 if (NILP (vector))
7107 vector = Fmake_vector (make_number (7), Qnil);
7108
7109 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7110 AREF (vector, i) = Vdeactivate_mark, ++i;
7111 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7112
7113 if (w)
7114 {
7115 XSETWINDOW (AREF (vector, i), w); ++i;
7116 AREF (vector, i) = w->buffer; ++i;
7117 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7118 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7119 }
7120 else
7121 {
7122 int end = i + 4;
7123 for (; i < end; ++i)
7124 AREF (vector, i) = Qnil;
7125 }
7126
7127 xassert (i == ASIZE (vector));
7128 return vector;
7129 }
7130
7131
7132 /* Restore global state from VECTOR which was created by
7133 with_echo_area_buffer_unwind_data. */
7134
7135 static Lisp_Object
7136 unwind_with_echo_area_buffer (vector)
7137 Lisp_Object vector;
7138 {
7139 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7140 Vdeactivate_mark = AREF (vector, 1);
7141 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7142
7143 if (WINDOWP (AREF (vector, 3)))
7144 {
7145 struct window *w;
7146 Lisp_Object buffer, charpos, bytepos;
7147
7148 w = XWINDOW (AREF (vector, 3));
7149 buffer = AREF (vector, 4);
7150 charpos = AREF (vector, 5);
7151 bytepos = AREF (vector, 6);
7152
7153 w->buffer = buffer;
7154 set_marker_both (w->pointm, buffer,
7155 XFASTINT (charpos), XFASTINT (bytepos));
7156 }
7157
7158 Vwith_echo_area_save_vector = vector;
7159 return Qnil;
7160 }
7161
7162
7163 /* Set up the echo area for use by print functions. MULTIBYTE_P
7164 non-zero means we will print multibyte. */
7165
7166 void
7167 setup_echo_area_for_printing (multibyte_p)
7168 int multibyte_p;
7169 {
7170 /* If we can't find an echo area any more, exit. */
7171 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7172 Fkill_emacs (Qnil);
7173
7174 ensure_echo_area_buffers ();
7175
7176 if (!message_buf_print)
7177 {
7178 /* A message has been output since the last time we printed.
7179 Choose a fresh echo area buffer. */
7180 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7181 echo_area_buffer[0] = echo_buffer[1];
7182 else
7183 echo_area_buffer[0] = echo_buffer[0];
7184
7185 /* Switch to that buffer and clear it. */
7186 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7187 current_buffer->truncate_lines = Qnil;
7188
7189 if (Z > BEG)
7190 {
7191 int count = SPECPDL_INDEX ();
7192 specbind (Qinhibit_read_only, Qt);
7193 /* Note that undo recording is always disabled. */
7194 del_range (BEG, Z);
7195 unbind_to (count, Qnil);
7196 }
7197 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7198
7199 /* Set up the buffer for the multibyteness we need. */
7200 if (multibyte_p
7201 != !NILP (current_buffer->enable_multibyte_characters))
7202 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7203
7204 /* Raise the frame containing the echo area. */
7205 if (minibuffer_auto_raise)
7206 {
7207 struct frame *sf = SELECTED_FRAME ();
7208 Lisp_Object mini_window;
7209 mini_window = FRAME_MINIBUF_WINDOW (sf);
7210 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7211 }
7212
7213 message_log_maybe_newline ();
7214 message_buf_print = 1;
7215 }
7216 else
7217 {
7218 if (NILP (echo_area_buffer[0]))
7219 {
7220 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7221 echo_area_buffer[0] = echo_buffer[1];
7222 else
7223 echo_area_buffer[0] = echo_buffer[0];
7224 }
7225
7226 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7227 {
7228 /* Someone switched buffers between print requests. */
7229 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7230 current_buffer->truncate_lines = Qnil;
7231 }
7232 }
7233 }
7234
7235
7236 /* Display an echo area message in window W. Value is non-zero if W's
7237 height is changed. If display_last_displayed_message_p is
7238 non-zero, display the message that was last displayed, otherwise
7239 display the current message. */
7240
7241 static int
7242 display_echo_area (w)
7243 struct window *w;
7244 {
7245 int i, no_message_p, window_height_changed_p, count;
7246
7247 /* Temporarily disable garbage collections while displaying the echo
7248 area. This is done because a GC can print a message itself.
7249 That message would modify the echo area buffer's contents while a
7250 redisplay of the buffer is going on, and seriously confuse
7251 redisplay. */
7252 count = inhibit_garbage_collection ();
7253
7254 /* If there is no message, we must call display_echo_area_1
7255 nevertheless because it resizes the window. But we will have to
7256 reset the echo_area_buffer in question to nil at the end because
7257 with_echo_area_buffer will sets it to an empty buffer. */
7258 i = display_last_displayed_message_p ? 1 : 0;
7259 no_message_p = NILP (echo_area_buffer[i]);
7260
7261 window_height_changed_p
7262 = with_echo_area_buffer (w, display_last_displayed_message_p,
7263 display_echo_area_1,
7264 (EMACS_INT) w, Qnil, 0, 0);
7265
7266 if (no_message_p)
7267 echo_area_buffer[i] = Qnil;
7268
7269 unbind_to (count, Qnil);
7270 return window_height_changed_p;
7271 }
7272
7273
7274 /* Helper for display_echo_area. Display the current buffer which
7275 contains the current echo area message in window W, a mini-window,
7276 a pointer to which is passed in A1. A2..A4 are currently not used.
7277 Change the height of W so that all of the message is displayed.
7278 Value is non-zero if height of W was changed. */
7279
7280 static int
7281 display_echo_area_1 (a1, a2, a3, a4)
7282 EMACS_INT a1;
7283 Lisp_Object a2;
7284 EMACS_INT a3, a4;
7285 {
7286 struct window *w = (struct window *) a1;
7287 Lisp_Object window;
7288 struct text_pos start;
7289 int window_height_changed_p = 0;
7290
7291 /* Do this before displaying, so that we have a large enough glyph
7292 matrix for the display. */
7293 window_height_changed_p = resize_mini_window (w, 0);
7294
7295 /* Display. */
7296 clear_glyph_matrix (w->desired_matrix);
7297 XSETWINDOW (window, w);
7298 SET_TEXT_POS (start, BEG, BEG_BYTE);
7299 try_window (window, start);
7300
7301 return window_height_changed_p;
7302 }
7303
7304
7305 /* Resize the echo area window to exactly the size needed for the
7306 currently displayed message, if there is one. If a mini-buffer
7307 is active, don't shrink it. */
7308
7309 void
7310 resize_echo_area_exactly ()
7311 {
7312 if (BUFFERP (echo_area_buffer[0])
7313 && WINDOWP (echo_area_window))
7314 {
7315 struct window *w = XWINDOW (echo_area_window);
7316 int resized_p;
7317 Lisp_Object resize_exactly;
7318
7319 if (minibuf_level == 0)
7320 resize_exactly = Qt;
7321 else
7322 resize_exactly = Qnil;
7323
7324 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7325 (EMACS_INT) w, resize_exactly, 0, 0);
7326 if (resized_p)
7327 {
7328 ++windows_or_buffers_changed;
7329 ++update_mode_lines;
7330 redisplay_internal (0);
7331 }
7332 }
7333 }
7334
7335
7336 /* Callback function for with_echo_area_buffer, when used from
7337 resize_echo_area_exactly. A1 contains a pointer to the window to
7338 resize, EXACTLY non-nil means resize the mini-window exactly to the
7339 size of the text displayed. A3 and A4 are not used. Value is what
7340 resize_mini_window returns. */
7341
7342 static int
7343 resize_mini_window_1 (a1, exactly, a3, a4)
7344 EMACS_INT a1;
7345 Lisp_Object exactly;
7346 EMACS_INT a3, a4;
7347 {
7348 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7349 }
7350
7351
7352 /* Resize mini-window W to fit the size of its contents. EXACT:P
7353 means size the window exactly to the size needed. Otherwise, it's
7354 only enlarged until W's buffer is empty. Value is non-zero if
7355 the window height has been changed. */
7356
7357 int
7358 resize_mini_window (w, exact_p)
7359 struct window *w;
7360 int exact_p;
7361 {
7362 struct frame *f = XFRAME (w->frame);
7363 int window_height_changed_p = 0;
7364
7365 xassert (MINI_WINDOW_P (w));
7366
7367 /* Don't resize windows while redisplaying a window; it would
7368 confuse redisplay functions when the size of the window they are
7369 displaying changes from under them. Such a resizing can happen,
7370 for instance, when which-func prints a long message while
7371 we are running fontification-functions. We're running these
7372 functions with safe_call which binds inhibit-redisplay to t. */
7373 if (!NILP (Vinhibit_redisplay))
7374 return 0;
7375
7376 /* Nil means don't try to resize. */
7377 if (NILP (Vresize_mini_windows)
7378 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7379 return 0;
7380
7381 if (!FRAME_MINIBUF_ONLY_P (f))
7382 {
7383 struct it it;
7384 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7385 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7386 int height, max_height;
7387 int unit = FRAME_LINE_HEIGHT (f);
7388 struct text_pos start;
7389 struct buffer *old_current_buffer = NULL;
7390
7391 if (current_buffer != XBUFFER (w->buffer))
7392 {
7393 old_current_buffer = current_buffer;
7394 set_buffer_internal (XBUFFER (w->buffer));
7395 }
7396
7397 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7398
7399 /* Compute the max. number of lines specified by the user. */
7400 if (FLOATP (Vmax_mini_window_height))
7401 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7402 else if (INTEGERP (Vmax_mini_window_height))
7403 max_height = XINT (Vmax_mini_window_height);
7404 else
7405 max_height = total_height / 4;
7406
7407 /* Correct that max. height if it's bogus. */
7408 max_height = max (1, max_height);
7409 max_height = min (total_height, max_height);
7410
7411 /* Find out the height of the text in the window. */
7412 if (it.truncate_lines_p)
7413 height = 1;
7414 else
7415 {
7416 last_height = 0;
7417 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7418 if (it.max_ascent == 0 && it.max_descent == 0)
7419 height = it.current_y + last_height;
7420 else
7421 height = it.current_y + it.max_ascent + it.max_descent;
7422 height -= it.extra_line_spacing;
7423 height = (height + unit - 1) / unit;
7424 }
7425
7426 /* Compute a suitable window start. */
7427 if (height > max_height)
7428 {
7429 height = max_height;
7430 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7431 move_it_vertically_backward (&it, (height - 1) * unit);
7432 start = it.current.pos;
7433 }
7434 else
7435 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7436 SET_MARKER_FROM_TEXT_POS (w->start, start);
7437
7438 if (EQ (Vresize_mini_windows, Qgrow_only))
7439 {
7440 /* Let it grow only, until we display an empty message, in which
7441 case the window shrinks again. */
7442 if (height > WINDOW_TOTAL_LINES (w))
7443 {
7444 int old_height = WINDOW_TOTAL_LINES (w);
7445 freeze_window_starts (f, 1);
7446 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7447 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7448 }
7449 else if (height < WINDOW_TOTAL_LINES (w)
7450 && (exact_p || BEGV == ZV))
7451 {
7452 int old_height = WINDOW_TOTAL_LINES (w);
7453 freeze_window_starts (f, 0);
7454 shrink_mini_window (w);
7455 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7456 }
7457 }
7458 else
7459 {
7460 /* Always resize to exact size needed. */
7461 if (height > WINDOW_TOTAL_LINES (w))
7462 {
7463 int old_height = WINDOW_TOTAL_LINES (w);
7464 freeze_window_starts (f, 1);
7465 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7466 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7467 }
7468 else if (height < WINDOW_TOTAL_LINES (w))
7469 {
7470 int old_height = WINDOW_TOTAL_LINES (w);
7471 freeze_window_starts (f, 0);
7472 shrink_mini_window (w);
7473
7474 if (height)
7475 {
7476 freeze_window_starts (f, 1);
7477 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7478 }
7479
7480 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7481 }
7482 }
7483
7484 if (old_current_buffer)
7485 set_buffer_internal (old_current_buffer);
7486 }
7487
7488 return window_height_changed_p;
7489 }
7490
7491
7492 /* Value is the current message, a string, or nil if there is no
7493 current message. */
7494
7495 Lisp_Object
7496 current_message ()
7497 {
7498 Lisp_Object msg;
7499
7500 if (NILP (echo_area_buffer[0]))
7501 msg = Qnil;
7502 else
7503 {
7504 with_echo_area_buffer (0, 0, current_message_1,
7505 (EMACS_INT) &msg, Qnil, 0, 0);
7506 if (NILP (msg))
7507 echo_area_buffer[0] = Qnil;
7508 }
7509
7510 return msg;
7511 }
7512
7513
7514 static int
7515 current_message_1 (a1, a2, a3, a4)
7516 EMACS_INT a1;
7517 Lisp_Object a2;
7518 EMACS_INT a3, a4;
7519 {
7520 Lisp_Object *msg = (Lisp_Object *) a1;
7521
7522 if (Z > BEG)
7523 *msg = make_buffer_string (BEG, Z, 1);
7524 else
7525 *msg = Qnil;
7526 return 0;
7527 }
7528
7529
7530 /* Push the current message on Vmessage_stack for later restauration
7531 by restore_message. Value is non-zero if the current message isn't
7532 empty. This is a relatively infrequent operation, so it's not
7533 worth optimizing. */
7534
7535 int
7536 push_message ()
7537 {
7538 Lisp_Object msg;
7539 msg = current_message ();
7540 Vmessage_stack = Fcons (msg, Vmessage_stack);
7541 return STRINGP (msg);
7542 }
7543
7544
7545 /* Restore message display from the top of Vmessage_stack. */
7546
7547 void
7548 restore_message ()
7549 {
7550 Lisp_Object msg;
7551
7552 xassert (CONSP (Vmessage_stack));
7553 msg = XCAR (Vmessage_stack);
7554 if (STRINGP (msg))
7555 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7556 else
7557 message3_nolog (msg, 0, 0);
7558 }
7559
7560
7561 /* Handler for record_unwind_protect calling pop_message. */
7562
7563 Lisp_Object
7564 pop_message_unwind (dummy)
7565 Lisp_Object dummy;
7566 {
7567 pop_message ();
7568 return Qnil;
7569 }
7570
7571 /* Pop the top-most entry off Vmessage_stack. */
7572
7573 void
7574 pop_message ()
7575 {
7576 xassert (CONSP (Vmessage_stack));
7577 Vmessage_stack = XCDR (Vmessage_stack);
7578 }
7579
7580
7581 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7582 exits. If the stack is not empty, we have a missing pop_message
7583 somewhere. */
7584
7585 void
7586 check_message_stack ()
7587 {
7588 if (!NILP (Vmessage_stack))
7589 abort ();
7590 }
7591
7592
7593 /* Truncate to NCHARS what will be displayed in the echo area the next
7594 time we display it---but don't redisplay it now. */
7595
7596 void
7597 truncate_echo_area (nchars)
7598 int nchars;
7599 {
7600 if (nchars == 0)
7601 echo_area_buffer[0] = Qnil;
7602 /* A null message buffer means that the frame hasn't really been
7603 initialized yet. Error messages get reported properly by
7604 cmd_error, so this must be just an informative message; toss it. */
7605 else if (!noninteractive
7606 && INTERACTIVE
7607 && !NILP (echo_area_buffer[0]))
7608 {
7609 struct frame *sf = SELECTED_FRAME ();
7610 if (FRAME_MESSAGE_BUF (sf))
7611 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7612 }
7613 }
7614
7615
7616 /* Helper function for truncate_echo_area. Truncate the current
7617 message to at most NCHARS characters. */
7618
7619 static int
7620 truncate_message_1 (nchars, a2, a3, a4)
7621 EMACS_INT nchars;
7622 Lisp_Object a2;
7623 EMACS_INT a3, a4;
7624 {
7625 if (BEG + nchars < Z)
7626 del_range (BEG + nchars, Z);
7627 if (Z == BEG)
7628 echo_area_buffer[0] = Qnil;
7629 return 0;
7630 }
7631
7632
7633 /* Set the current message to a substring of S or STRING.
7634
7635 If STRING is a Lisp string, set the message to the first NBYTES
7636 bytes from STRING. NBYTES zero means use the whole string. If
7637 STRING is multibyte, the message will be displayed multibyte.
7638
7639 If S is not null, set the message to the first LEN bytes of S. LEN
7640 zero means use the whole string. MULTIBYTE_P non-zero means S is
7641 multibyte. Display the message multibyte in that case. */
7642
7643 void
7644 set_message (s, string, nbytes, multibyte_p)
7645 const char *s;
7646 Lisp_Object string;
7647 int nbytes, multibyte_p;
7648 {
7649 message_enable_multibyte
7650 = ((s && multibyte_p)
7651 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7652
7653 with_echo_area_buffer (0, -1, set_message_1,
7654 (EMACS_INT) s, string, nbytes, multibyte_p);
7655 message_buf_print = 0;
7656 help_echo_showing_p = 0;
7657 }
7658
7659
7660 /* Helper function for set_message. Arguments have the same meaning
7661 as there, with A1 corresponding to S and A2 corresponding to STRING
7662 This function is called with the echo area buffer being
7663 current. */
7664
7665 static int
7666 set_message_1 (a1, a2, nbytes, multibyte_p)
7667 EMACS_INT a1;
7668 Lisp_Object a2;
7669 EMACS_INT nbytes, multibyte_p;
7670 {
7671 const char *s = (const char *) a1;
7672 Lisp_Object string = a2;
7673
7674 xassert (BEG == Z);
7675
7676 /* Change multibyteness of the echo buffer appropriately. */
7677 if (message_enable_multibyte
7678 != !NILP (current_buffer->enable_multibyte_characters))
7679 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7680
7681 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7682
7683 /* Insert new message at BEG. */
7684 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7685
7686 if (STRINGP (string))
7687 {
7688 int nchars;
7689
7690 if (nbytes == 0)
7691 nbytes = SBYTES (string);
7692 nchars = string_byte_to_char (string, nbytes);
7693
7694 /* This function takes care of single/multibyte conversion. We
7695 just have to ensure that the echo area buffer has the right
7696 setting of enable_multibyte_characters. */
7697 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7698 }
7699 else if (s)
7700 {
7701 if (nbytes == 0)
7702 nbytes = strlen (s);
7703
7704 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7705 {
7706 /* Convert from multi-byte to single-byte. */
7707 int i, c, n;
7708 unsigned char work[1];
7709
7710 /* Convert a multibyte string to single-byte. */
7711 for (i = 0; i < nbytes; i += n)
7712 {
7713 c = string_char_and_length (s + i, nbytes - i, &n);
7714 work[0] = (SINGLE_BYTE_CHAR_P (c)
7715 ? c
7716 : multibyte_char_to_unibyte (c, Qnil));
7717 insert_1_both (work, 1, 1, 1, 0, 0);
7718 }
7719 }
7720 else if (!multibyte_p
7721 && !NILP (current_buffer->enable_multibyte_characters))
7722 {
7723 /* Convert from single-byte to multi-byte. */
7724 int i, c, n;
7725 const unsigned char *msg = (const unsigned char *) s;
7726 unsigned char str[MAX_MULTIBYTE_LENGTH];
7727
7728 /* Convert a single-byte string to multibyte. */
7729 for (i = 0; i < nbytes; i++)
7730 {
7731 c = unibyte_char_to_multibyte (msg[i]);
7732 n = CHAR_STRING (c, str);
7733 insert_1_both (str, 1, n, 1, 0, 0);
7734 }
7735 }
7736 else
7737 insert_1 (s, nbytes, 1, 0, 0);
7738 }
7739
7740 return 0;
7741 }
7742
7743
7744 /* Clear messages. CURRENT_P non-zero means clear the current
7745 message. LAST_DISPLAYED_P non-zero means clear the message
7746 last displayed. */
7747
7748 void
7749 clear_message (current_p, last_displayed_p)
7750 int current_p, last_displayed_p;
7751 {
7752 if (current_p)
7753 {
7754 echo_area_buffer[0] = Qnil;
7755 message_cleared_p = 1;
7756 }
7757
7758 if (last_displayed_p)
7759 echo_area_buffer[1] = Qnil;
7760
7761 message_buf_print = 0;
7762 }
7763
7764 /* Clear garbaged frames.
7765
7766 This function is used where the old redisplay called
7767 redraw_garbaged_frames which in turn called redraw_frame which in
7768 turn called clear_frame. The call to clear_frame was a source of
7769 flickering. I believe a clear_frame is not necessary. It should
7770 suffice in the new redisplay to invalidate all current matrices,
7771 and ensure a complete redisplay of all windows. */
7772
7773 static void
7774 clear_garbaged_frames ()
7775 {
7776 if (frame_garbaged)
7777 {
7778 Lisp_Object tail, frame;
7779 int changed_count = 0;
7780
7781 FOR_EACH_FRAME (tail, frame)
7782 {
7783 struct frame *f = XFRAME (frame);
7784
7785 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7786 {
7787 if (f->resized_p)
7788 {
7789 Fredraw_frame (frame);
7790 f->force_flush_display_p = 1;
7791 }
7792 clear_current_matrices (f);
7793 changed_count++;
7794 f->garbaged = 0;
7795 f->resized_p = 0;
7796 }
7797 }
7798
7799 frame_garbaged = 0;
7800 if (changed_count)
7801 ++windows_or_buffers_changed;
7802 }
7803 }
7804
7805
7806 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7807 is non-zero update selected_frame. Value is non-zero if the
7808 mini-windows height has been changed. */
7809
7810 static int
7811 echo_area_display (update_frame_p)
7812 int update_frame_p;
7813 {
7814 Lisp_Object mini_window;
7815 struct window *w;
7816 struct frame *f;
7817 int window_height_changed_p = 0;
7818 struct frame *sf = SELECTED_FRAME ();
7819
7820 mini_window = FRAME_MINIBUF_WINDOW (sf);
7821 w = XWINDOW (mini_window);
7822 f = XFRAME (WINDOW_FRAME (w));
7823
7824 /* Don't display if frame is invisible or not yet initialized. */
7825 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7826 return 0;
7827
7828 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7829 #ifndef MAC_OS8
7830 #ifdef HAVE_WINDOW_SYSTEM
7831 /* When Emacs starts, selected_frame may be a visible terminal
7832 frame, even if we run under a window system. If we let this
7833 through, a message would be displayed on the terminal. */
7834 if (EQ (selected_frame, Vterminal_frame)
7835 && !NILP (Vwindow_system))
7836 return 0;
7837 #endif /* HAVE_WINDOW_SYSTEM */
7838 #endif
7839
7840 /* Redraw garbaged frames. */
7841 if (frame_garbaged)
7842 clear_garbaged_frames ();
7843
7844 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7845 {
7846 echo_area_window = mini_window;
7847 window_height_changed_p = display_echo_area (w);
7848 w->must_be_updated_p = 1;
7849
7850 /* Update the display, unless called from redisplay_internal.
7851 Also don't update the screen during redisplay itself. The
7852 update will happen at the end of redisplay, and an update
7853 here could cause confusion. */
7854 if (update_frame_p && !redisplaying_p)
7855 {
7856 int n = 0;
7857
7858 /* If the display update has been interrupted by pending
7859 input, update mode lines in the frame. Due to the
7860 pending input, it might have been that redisplay hasn't
7861 been called, so that mode lines above the echo area are
7862 garbaged. This looks odd, so we prevent it here. */
7863 if (!display_completed)
7864 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7865
7866 if (window_height_changed_p
7867 /* Don't do this if Emacs is shutting down. Redisplay
7868 needs to run hooks. */
7869 && !NILP (Vrun_hooks))
7870 {
7871 /* Must update other windows. Likewise as in other
7872 cases, don't let this update be interrupted by
7873 pending input. */
7874 int count = SPECPDL_INDEX ();
7875 specbind (Qredisplay_dont_pause, Qt);
7876 windows_or_buffers_changed = 1;
7877 redisplay_internal (0);
7878 unbind_to (count, Qnil);
7879 }
7880 else if (FRAME_WINDOW_P (f) && n == 0)
7881 {
7882 /* Window configuration is the same as before.
7883 Can do with a display update of the echo area,
7884 unless we displayed some mode lines. */
7885 update_single_window (w, 1);
7886 rif->flush_display (f);
7887 }
7888 else
7889 update_frame (f, 1, 1);
7890
7891 /* If cursor is in the echo area, make sure that the next
7892 redisplay displays the minibuffer, so that the cursor will
7893 be replaced with what the minibuffer wants. */
7894 if (cursor_in_echo_area)
7895 ++windows_or_buffers_changed;
7896 }
7897 }
7898 else if (!EQ (mini_window, selected_window))
7899 windows_or_buffers_changed++;
7900
7901 /* Last displayed message is now the current message. */
7902 echo_area_buffer[1] = echo_area_buffer[0];
7903
7904 /* Prevent redisplay optimization in redisplay_internal by resetting
7905 this_line_start_pos. This is done because the mini-buffer now
7906 displays the message instead of its buffer text. */
7907 if (EQ (mini_window, selected_window))
7908 CHARPOS (this_line_start_pos) = 0;
7909
7910 return window_height_changed_p;
7911 }
7912
7913
7914 \f
7915 /***********************************************************************
7916 Frame Titles
7917 ***********************************************************************/
7918
7919
7920 /* The frame title buffering code is also used by Fformat_mode_line.
7921 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
7922
7923 /* A buffer for constructing frame titles in it; allocated from the
7924 heap in init_xdisp and resized as needed in store_frame_title_char. */
7925
7926 static char *frame_title_buf;
7927
7928 /* The buffer's end, and a current output position in it. */
7929
7930 static char *frame_title_buf_end;
7931 static char *frame_title_ptr;
7932
7933
7934 /* Store a single character C for the frame title in frame_title_buf.
7935 Re-allocate frame_title_buf if necessary. */
7936
7937 static void
7938 #ifdef PROTOTYPES
7939 store_frame_title_char (char c)
7940 #else
7941 store_frame_title_char (c)
7942 char c;
7943 #endif
7944 {
7945 /* If output position has reached the end of the allocated buffer,
7946 double the buffer's size. */
7947 if (frame_title_ptr == frame_title_buf_end)
7948 {
7949 int len = frame_title_ptr - frame_title_buf;
7950 int new_size = 2 * len * sizeof *frame_title_buf;
7951 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7952 frame_title_buf_end = frame_title_buf + new_size;
7953 frame_title_ptr = frame_title_buf + len;
7954 }
7955
7956 *frame_title_ptr++ = c;
7957 }
7958
7959
7960 /* Store part of a frame title in frame_title_buf, beginning at
7961 frame_title_ptr. STR is the string to store. Do not copy
7962 characters that yield more columns than PRECISION; PRECISION <= 0
7963 means copy the whole string. Pad with spaces until FIELD_WIDTH
7964 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7965 pad. Called from display_mode_element when it is used to build a
7966 frame title. */
7967
7968 static int
7969 store_frame_title (str, field_width, precision)
7970 const unsigned char *str;
7971 int field_width, precision;
7972 {
7973 int n = 0;
7974 int dummy, nbytes;
7975
7976 /* Copy at most PRECISION chars from STR. */
7977 nbytes = strlen (str);
7978 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7979 while (nbytes--)
7980 store_frame_title_char (*str++);
7981
7982 /* Fill up with spaces until FIELD_WIDTH reached. */
7983 while (field_width > 0
7984 && n < field_width)
7985 {
7986 store_frame_title_char (' ');
7987 ++n;
7988 }
7989
7990 return n;
7991 }
7992
7993 #ifdef HAVE_WINDOW_SYSTEM
7994
7995 /* Set the title of FRAME, if it has changed. The title format is
7996 Vicon_title_format if FRAME is iconified, otherwise it is
7997 frame_title_format. */
7998
7999 static void
8000 x_consider_frame_title (frame)
8001 Lisp_Object frame;
8002 {
8003 struct frame *f = XFRAME (frame);
8004
8005 if (FRAME_WINDOW_P (f)
8006 || FRAME_MINIBUF_ONLY_P (f)
8007 || f->explicit_name)
8008 {
8009 /* Do we have more than one visible frame on this X display? */
8010 Lisp_Object tail;
8011 Lisp_Object fmt;
8012 struct buffer *obuf;
8013 int len;
8014 struct it it;
8015
8016 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8017 {
8018 Lisp_Object other_frame = XCAR (tail);
8019 struct frame *tf = XFRAME (other_frame);
8020
8021 if (tf != f
8022 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8023 && !FRAME_MINIBUF_ONLY_P (tf)
8024 && !EQ (other_frame, tip_frame)
8025 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8026 break;
8027 }
8028
8029 /* Set global variable indicating that multiple frames exist. */
8030 multiple_frames = CONSP (tail);
8031
8032 /* Switch to the buffer of selected window of the frame. Set up
8033 frame_title_ptr so that display_mode_element will output into it;
8034 then display the title. */
8035 obuf = current_buffer;
8036 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8037 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8038 frame_title_ptr = frame_title_buf;
8039 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8040 NULL, DEFAULT_FACE_ID);
8041 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8042 len = frame_title_ptr - frame_title_buf;
8043 frame_title_ptr = NULL;
8044 set_buffer_internal_1 (obuf);
8045
8046 /* Set the title only if it's changed. This avoids consing in
8047 the common case where it hasn't. (If it turns out that we've
8048 already wasted too much time by walking through the list with
8049 display_mode_element, then we might need to optimize at a
8050 higher level than this.) */
8051 if (! STRINGP (f->name)
8052 || SBYTES (f->name) != len
8053 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
8054 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
8055 }
8056 }
8057
8058 #endif /* not HAVE_WINDOW_SYSTEM */
8059
8060
8061
8062 \f
8063 /***********************************************************************
8064 Menu Bars
8065 ***********************************************************************/
8066
8067
8068 /* Prepare for redisplay by updating menu-bar item lists when
8069 appropriate. This can call eval. */
8070
8071 void
8072 prepare_menu_bars ()
8073 {
8074 int all_windows;
8075 struct gcpro gcpro1, gcpro2;
8076 struct frame *f;
8077 Lisp_Object tooltip_frame;
8078
8079 #ifdef HAVE_WINDOW_SYSTEM
8080 tooltip_frame = tip_frame;
8081 #else
8082 tooltip_frame = Qnil;
8083 #endif
8084
8085 /* Update all frame titles based on their buffer names, etc. We do
8086 this before the menu bars so that the buffer-menu will show the
8087 up-to-date frame titles. */
8088 #ifdef HAVE_WINDOW_SYSTEM
8089 if (windows_or_buffers_changed || update_mode_lines)
8090 {
8091 Lisp_Object tail, frame;
8092
8093 FOR_EACH_FRAME (tail, frame)
8094 {
8095 f = XFRAME (frame);
8096 if (!EQ (frame, tooltip_frame)
8097 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8098 x_consider_frame_title (frame);
8099 }
8100 }
8101 #endif /* HAVE_WINDOW_SYSTEM */
8102
8103 /* Update the menu bar item lists, if appropriate. This has to be
8104 done before any actual redisplay or generation of display lines. */
8105 all_windows = (update_mode_lines
8106 || buffer_shared > 1
8107 || windows_or_buffers_changed);
8108 if (all_windows)
8109 {
8110 Lisp_Object tail, frame;
8111 int count = SPECPDL_INDEX ();
8112
8113 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8114
8115 FOR_EACH_FRAME (tail, frame)
8116 {
8117 f = XFRAME (frame);
8118
8119 /* Ignore tooltip frame. */
8120 if (EQ (frame, tooltip_frame))
8121 continue;
8122
8123 /* If a window on this frame changed size, report that to
8124 the user and clear the size-change flag. */
8125 if (FRAME_WINDOW_SIZES_CHANGED (f))
8126 {
8127 Lisp_Object functions;
8128
8129 /* Clear flag first in case we get an error below. */
8130 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8131 functions = Vwindow_size_change_functions;
8132 GCPRO2 (tail, functions);
8133
8134 while (CONSP (functions))
8135 {
8136 call1 (XCAR (functions), frame);
8137 functions = XCDR (functions);
8138 }
8139 UNGCPRO;
8140 }
8141
8142 GCPRO1 (tail);
8143 update_menu_bar (f, 0);
8144 #ifdef HAVE_WINDOW_SYSTEM
8145 update_tool_bar (f, 0);
8146 #endif
8147 UNGCPRO;
8148 }
8149
8150 unbind_to (count, Qnil);
8151 }
8152 else
8153 {
8154 struct frame *sf = SELECTED_FRAME ();
8155 update_menu_bar (sf, 1);
8156 #ifdef HAVE_WINDOW_SYSTEM
8157 update_tool_bar (sf, 1);
8158 #endif
8159 }
8160
8161 /* Motif needs this. See comment in xmenu.c. Turn it off when
8162 pending_menu_activation is not defined. */
8163 #ifdef USE_X_TOOLKIT
8164 pending_menu_activation = 0;
8165 #endif
8166 }
8167
8168
8169 /* Update the menu bar item list for frame F. This has to be done
8170 before we start to fill in any display lines, because it can call
8171 eval.
8172
8173 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8174
8175 static void
8176 update_menu_bar (f, save_match_data)
8177 struct frame *f;
8178 int save_match_data;
8179 {
8180 Lisp_Object window;
8181 register struct window *w;
8182
8183 /* If called recursively during a menu update, do nothing. This can
8184 happen when, for instance, an activate-menubar-hook causes a
8185 redisplay. */
8186 if (inhibit_menubar_update)
8187 return;
8188
8189 window = FRAME_SELECTED_WINDOW (f);
8190 w = XWINDOW (window);
8191
8192 #if 0 /* The if statement below this if statement used to include the
8193 condition !NILP (w->update_mode_line), rather than using
8194 update_mode_lines directly, and this if statement may have
8195 been added to make that condition work. Now the if
8196 statement below matches its comment, this isn't needed. */
8197 if (update_mode_lines)
8198 w->update_mode_line = Qt;
8199 #endif
8200
8201 if (FRAME_WINDOW_P (f)
8202 ?
8203 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8204 || defined (USE_GTK)
8205 FRAME_EXTERNAL_MENU_BAR (f)
8206 #else
8207 FRAME_MENU_BAR_LINES (f) > 0
8208 #endif
8209 : FRAME_MENU_BAR_LINES (f) > 0)
8210 {
8211 /* If the user has switched buffers or windows, we need to
8212 recompute to reflect the new bindings. But we'll
8213 recompute when update_mode_lines is set too; that means
8214 that people can use force-mode-line-update to request
8215 that the menu bar be recomputed. The adverse effect on
8216 the rest of the redisplay algorithm is about the same as
8217 windows_or_buffers_changed anyway. */
8218 if (windows_or_buffers_changed
8219 /* This used to test w->update_mode_line, but we believe
8220 there is no need to recompute the menu in that case. */
8221 || update_mode_lines
8222 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8223 < BUF_MODIFF (XBUFFER (w->buffer)))
8224 != !NILP (w->last_had_star))
8225 || ((!NILP (Vtransient_mark_mode)
8226 && !NILP (XBUFFER (w->buffer)->mark_active))
8227 != !NILP (w->region_showing)))
8228 {
8229 struct buffer *prev = current_buffer;
8230 int count = SPECPDL_INDEX ();
8231
8232 specbind (Qinhibit_menubar_update, Qt);
8233
8234 set_buffer_internal_1 (XBUFFER (w->buffer));
8235 if (save_match_data)
8236 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8237 if (NILP (Voverriding_local_map_menu_flag))
8238 {
8239 specbind (Qoverriding_terminal_local_map, Qnil);
8240 specbind (Qoverriding_local_map, Qnil);
8241 }
8242
8243 /* Run the Lucid hook. */
8244 safe_run_hooks (Qactivate_menubar_hook);
8245
8246 /* If it has changed current-menubar from previous value,
8247 really recompute the menu-bar from the value. */
8248 if (! NILP (Vlucid_menu_bar_dirty_flag))
8249 call0 (Qrecompute_lucid_menubar);
8250
8251 safe_run_hooks (Qmenu_bar_update_hook);
8252 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8253
8254 /* Redisplay the menu bar in case we changed it. */
8255 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8256 || defined (USE_GTK)
8257 if (FRAME_WINDOW_P (f)
8258 #if defined (MAC_OS)
8259 /* All frames on Mac OS share the same menubar. So only the
8260 selected frame should be allowed to set it. */
8261 && f == SELECTED_FRAME ()
8262 #endif
8263 )
8264 set_frame_menubar (f, 0, 0);
8265 else
8266 /* On a terminal screen, the menu bar is an ordinary screen
8267 line, and this makes it get updated. */
8268 w->update_mode_line = Qt;
8269 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8270 /* In the non-toolkit version, the menu bar is an ordinary screen
8271 line, and this makes it get updated. */
8272 w->update_mode_line = Qt;
8273 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8274
8275 unbind_to (count, Qnil);
8276 set_buffer_internal_1 (prev);
8277 }
8278 }
8279 }
8280
8281
8282 \f
8283 /***********************************************************************
8284 Output Cursor
8285 ***********************************************************************/
8286
8287 #ifdef HAVE_WINDOW_SYSTEM
8288
8289 /* EXPORT:
8290 Nominal cursor position -- where to draw output.
8291 HPOS and VPOS are window relative glyph matrix coordinates.
8292 X and Y are window relative pixel coordinates. */
8293
8294 struct cursor_pos output_cursor;
8295
8296
8297 /* EXPORT:
8298 Set the global variable output_cursor to CURSOR. All cursor
8299 positions are relative to updated_window. */
8300
8301 void
8302 set_output_cursor (cursor)
8303 struct cursor_pos *cursor;
8304 {
8305 output_cursor.hpos = cursor->hpos;
8306 output_cursor.vpos = cursor->vpos;
8307 output_cursor.x = cursor->x;
8308 output_cursor.y = cursor->y;
8309 }
8310
8311
8312 /* EXPORT for RIF:
8313 Set a nominal cursor position.
8314
8315 HPOS and VPOS are column/row positions in a window glyph matrix. X
8316 and Y are window text area relative pixel positions.
8317
8318 If this is done during an update, updated_window will contain the
8319 window that is being updated and the position is the future output
8320 cursor position for that window. If updated_window is null, use
8321 selected_window and display the cursor at the given position. */
8322
8323 void
8324 x_cursor_to (vpos, hpos, y, x)
8325 int vpos, hpos, y, x;
8326 {
8327 struct window *w;
8328
8329 /* If updated_window is not set, work on selected_window. */
8330 if (updated_window)
8331 w = updated_window;
8332 else
8333 w = XWINDOW (selected_window);
8334
8335 /* Set the output cursor. */
8336 output_cursor.hpos = hpos;
8337 output_cursor.vpos = vpos;
8338 output_cursor.x = x;
8339 output_cursor.y = y;
8340
8341 /* If not called as part of an update, really display the cursor.
8342 This will also set the cursor position of W. */
8343 if (updated_window == NULL)
8344 {
8345 BLOCK_INPUT;
8346 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8347 if (rif->flush_display_optional)
8348 rif->flush_display_optional (SELECTED_FRAME ());
8349 UNBLOCK_INPUT;
8350 }
8351 }
8352
8353 #endif /* HAVE_WINDOW_SYSTEM */
8354
8355 \f
8356 /***********************************************************************
8357 Tool-bars
8358 ***********************************************************************/
8359
8360 #ifdef HAVE_WINDOW_SYSTEM
8361
8362 /* Where the mouse was last time we reported a mouse event. */
8363
8364 FRAME_PTR last_mouse_frame;
8365
8366 /* Tool-bar item index of the item on which a mouse button was pressed
8367 or -1. */
8368
8369 int last_tool_bar_item;
8370
8371
8372 /* Update the tool-bar item list for frame F. This has to be done
8373 before we start to fill in any display lines. Called from
8374 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8375 and restore it here. */
8376
8377 static void
8378 update_tool_bar (f, save_match_data)
8379 struct frame *f;
8380 int save_match_data;
8381 {
8382 #ifdef USE_GTK
8383 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8384 #else
8385 int do_update = WINDOWP (f->tool_bar_window)
8386 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8387 #endif
8388
8389 if (do_update)
8390 {
8391 Lisp_Object window;
8392 struct window *w;
8393
8394 window = FRAME_SELECTED_WINDOW (f);
8395 w = XWINDOW (window);
8396
8397 /* If the user has switched buffers or windows, we need to
8398 recompute to reflect the new bindings. But we'll
8399 recompute when update_mode_lines is set too; that means
8400 that people can use force-mode-line-update to request
8401 that the menu bar be recomputed. The adverse effect on
8402 the rest of the redisplay algorithm is about the same as
8403 windows_or_buffers_changed anyway. */
8404 if (windows_or_buffers_changed
8405 || !NILP (w->update_mode_line)
8406 || update_mode_lines
8407 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8408 < BUF_MODIFF (XBUFFER (w->buffer)))
8409 != !NILP (w->last_had_star))
8410 || ((!NILP (Vtransient_mark_mode)
8411 && !NILP (XBUFFER (w->buffer)->mark_active))
8412 != !NILP (w->region_showing)))
8413 {
8414 struct buffer *prev = current_buffer;
8415 int count = SPECPDL_INDEX ();
8416 Lisp_Object old_tool_bar;
8417 struct gcpro gcpro1;
8418
8419 /* Set current_buffer to the buffer of the selected
8420 window of the frame, so that we get the right local
8421 keymaps. */
8422 set_buffer_internal_1 (XBUFFER (w->buffer));
8423
8424 /* Save match data, if we must. */
8425 if (save_match_data)
8426 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8427
8428 /* Make sure that we don't accidentally use bogus keymaps. */
8429 if (NILP (Voverriding_local_map_menu_flag))
8430 {
8431 specbind (Qoverriding_terminal_local_map, Qnil);
8432 specbind (Qoverriding_local_map, Qnil);
8433 }
8434
8435 old_tool_bar = f->tool_bar_items;
8436 GCPRO1 (old_tool_bar);
8437
8438 /* Build desired tool-bar items from keymaps. */
8439 BLOCK_INPUT;
8440 f->tool_bar_items
8441 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
8442 UNBLOCK_INPUT;
8443
8444 /* Redisplay the tool-bar if we changed it. */
8445 if (! NILP (Fequal (old_tool_bar, f->tool_bar_items)))
8446 w->update_mode_line = Qt;
8447
8448 UNGCPRO;
8449
8450 unbind_to (count, Qnil);
8451 set_buffer_internal_1 (prev);
8452 }
8453 }
8454 }
8455
8456
8457 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8458 F's desired tool-bar contents. F->tool_bar_items must have
8459 been set up previously by calling prepare_menu_bars. */
8460
8461 static void
8462 build_desired_tool_bar_string (f)
8463 struct frame *f;
8464 {
8465 int i, size, size_needed;
8466 struct gcpro gcpro1, gcpro2, gcpro3;
8467 Lisp_Object image, plist, props;
8468
8469 image = plist = props = Qnil;
8470 GCPRO3 (image, plist, props);
8471
8472 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8473 Otherwise, make a new string. */
8474
8475 /* The size of the string we might be able to reuse. */
8476 size = (STRINGP (f->desired_tool_bar_string)
8477 ? SCHARS (f->desired_tool_bar_string)
8478 : 0);
8479
8480 /* We need one space in the string for each image. */
8481 size_needed = f->n_tool_bar_items;
8482
8483 /* Reuse f->desired_tool_bar_string, if possible. */
8484 if (size < size_needed || NILP (f->desired_tool_bar_string))
8485 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8486 make_number (' '));
8487 else
8488 {
8489 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8490 Fremove_text_properties (make_number (0), make_number (size),
8491 props, f->desired_tool_bar_string);
8492 }
8493
8494 /* Put a `display' property on the string for the images to display,
8495 put a `menu_item' property on tool-bar items with a value that
8496 is the index of the item in F's tool-bar item vector. */
8497 for (i = 0; i < f->n_tool_bar_items; ++i)
8498 {
8499 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8500
8501 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8502 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8503 int hmargin, vmargin, relief, idx, end;
8504 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8505
8506 /* If image is a vector, choose the image according to the
8507 button state. */
8508 image = PROP (TOOL_BAR_ITEM_IMAGES);
8509 if (VECTORP (image))
8510 {
8511 if (enabled_p)
8512 idx = (selected_p
8513 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8514 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8515 else
8516 idx = (selected_p
8517 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8518 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8519
8520 xassert (ASIZE (image) >= idx);
8521 image = AREF (image, idx);
8522 }
8523 else
8524 idx = -1;
8525
8526 /* Ignore invalid image specifications. */
8527 if (!valid_image_p (image))
8528 continue;
8529
8530 /* Display the tool-bar button pressed, or depressed. */
8531 plist = Fcopy_sequence (XCDR (image));
8532
8533 /* Compute margin and relief to draw. */
8534 relief = (tool_bar_button_relief >= 0
8535 ? tool_bar_button_relief
8536 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8537 hmargin = vmargin = relief;
8538
8539 if (INTEGERP (Vtool_bar_button_margin)
8540 && XINT (Vtool_bar_button_margin) > 0)
8541 {
8542 hmargin += XFASTINT (Vtool_bar_button_margin);
8543 vmargin += XFASTINT (Vtool_bar_button_margin);
8544 }
8545 else if (CONSP (Vtool_bar_button_margin))
8546 {
8547 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8548 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8549 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8550
8551 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8552 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8553 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8554 }
8555
8556 if (auto_raise_tool_bar_buttons_p)
8557 {
8558 /* Add a `:relief' property to the image spec if the item is
8559 selected. */
8560 if (selected_p)
8561 {
8562 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8563 hmargin -= relief;
8564 vmargin -= relief;
8565 }
8566 }
8567 else
8568 {
8569 /* If image is selected, display it pressed, i.e. with a
8570 negative relief. If it's not selected, display it with a
8571 raised relief. */
8572 plist = Fplist_put (plist, QCrelief,
8573 (selected_p
8574 ? make_number (-relief)
8575 : make_number (relief)));
8576 hmargin -= relief;
8577 vmargin -= relief;
8578 }
8579
8580 /* Put a margin around the image. */
8581 if (hmargin || vmargin)
8582 {
8583 if (hmargin == vmargin)
8584 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8585 else
8586 plist = Fplist_put (plist, QCmargin,
8587 Fcons (make_number (hmargin),
8588 make_number (vmargin)));
8589 }
8590
8591 /* If button is not enabled, and we don't have special images
8592 for the disabled state, make the image appear disabled by
8593 applying an appropriate algorithm to it. */
8594 if (!enabled_p && idx < 0)
8595 plist = Fplist_put (plist, QCconversion, Qdisabled);
8596
8597 /* Put a `display' text property on the string for the image to
8598 display. Put a `menu-item' property on the string that gives
8599 the start of this item's properties in the tool-bar items
8600 vector. */
8601 image = Fcons (Qimage, plist);
8602 props = list4 (Qdisplay, image,
8603 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8604
8605 /* Let the last image hide all remaining spaces in the tool bar
8606 string. The string can be longer than needed when we reuse a
8607 previous string. */
8608 if (i + 1 == f->n_tool_bar_items)
8609 end = SCHARS (f->desired_tool_bar_string);
8610 else
8611 end = i + 1;
8612 Fadd_text_properties (make_number (i), make_number (end),
8613 props, f->desired_tool_bar_string);
8614 #undef PROP
8615 }
8616
8617 UNGCPRO;
8618 }
8619
8620
8621 /* Display one line of the tool-bar of frame IT->f. */
8622
8623 static void
8624 display_tool_bar_line (it)
8625 struct it *it;
8626 {
8627 struct glyph_row *row = it->glyph_row;
8628 int max_x = it->last_visible_x;
8629 struct glyph *last;
8630
8631 prepare_desired_row (row);
8632 row->y = it->current_y;
8633
8634 /* Note that this isn't made use of if the face hasn't a box,
8635 so there's no need to check the face here. */
8636 it->start_of_box_run_p = 1;
8637
8638 while (it->current_x < max_x)
8639 {
8640 int x_before, x, n_glyphs_before, i, nglyphs;
8641
8642 /* Get the next display element. */
8643 if (!get_next_display_element (it))
8644 break;
8645
8646 /* Produce glyphs. */
8647 x_before = it->current_x;
8648 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8649 PRODUCE_GLYPHS (it);
8650
8651 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8652 i = 0;
8653 x = x_before;
8654 while (i < nglyphs)
8655 {
8656 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
8657
8658 if (x + glyph->pixel_width > max_x)
8659 {
8660 /* Glyph doesn't fit on line. */
8661 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8662 it->current_x = x;
8663 goto out;
8664 }
8665
8666 ++it->hpos;
8667 x += glyph->pixel_width;
8668 ++i;
8669 }
8670
8671 /* Stop at line ends. */
8672 if (ITERATOR_AT_END_OF_LINE_P (it))
8673 break;
8674
8675 set_iterator_to_next (it, 1);
8676 }
8677
8678 out:;
8679
8680 row->displays_text_p = row->used[TEXT_AREA] != 0;
8681 extend_face_to_end_of_line (it);
8682 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8683 last->right_box_line_p = 1;
8684 if (last == row->glyphs[TEXT_AREA])
8685 last->left_box_line_p = 1;
8686 compute_line_metrics (it);
8687
8688 /* If line is empty, make it occupy the rest of the tool-bar. */
8689 if (!row->displays_text_p)
8690 {
8691 row->height = row->phys_height = it->last_visible_y - row->y;
8692 row->ascent = row->phys_ascent = 0;
8693 }
8694
8695 row->full_width_p = 1;
8696 row->continued_p = 0;
8697 row->truncated_on_left_p = 0;
8698 row->truncated_on_right_p = 0;
8699
8700 it->current_x = it->hpos = 0;
8701 it->current_y += row->height;
8702 ++it->vpos;
8703 ++it->glyph_row;
8704 }
8705
8706
8707 /* Value is the number of screen lines needed to make all tool-bar
8708 items of frame F visible. */
8709
8710 static int
8711 tool_bar_lines_needed (f)
8712 struct frame *f;
8713 {
8714 struct window *w = XWINDOW (f->tool_bar_window);
8715 struct it it;
8716
8717 /* Initialize an iterator for iteration over
8718 F->desired_tool_bar_string in the tool-bar window of frame F. */
8719 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8720 it.first_visible_x = 0;
8721 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8722 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8723
8724 while (!ITERATOR_AT_END_P (&it))
8725 {
8726 it.glyph_row = w->desired_matrix->rows;
8727 clear_glyph_row (it.glyph_row);
8728 display_tool_bar_line (&it);
8729 }
8730
8731 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
8732 }
8733
8734
8735 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8736 0, 1, 0,
8737 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8738 (frame)
8739 Lisp_Object frame;
8740 {
8741 struct frame *f;
8742 struct window *w;
8743 int nlines = 0;
8744
8745 if (NILP (frame))
8746 frame = selected_frame;
8747 else
8748 CHECK_FRAME (frame);
8749 f = XFRAME (frame);
8750
8751 if (WINDOWP (f->tool_bar_window)
8752 || (w = XWINDOW (f->tool_bar_window),
8753 WINDOW_TOTAL_LINES (w) > 0))
8754 {
8755 update_tool_bar (f, 1);
8756 if (f->n_tool_bar_items)
8757 {
8758 build_desired_tool_bar_string (f);
8759 nlines = tool_bar_lines_needed (f);
8760 }
8761 }
8762
8763 return make_number (nlines);
8764 }
8765
8766
8767 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
8768 height should be changed. */
8769
8770 static int
8771 redisplay_tool_bar (f)
8772 struct frame *f;
8773 {
8774 struct window *w;
8775 struct it it;
8776 struct glyph_row *row;
8777 int change_height_p = 0;
8778
8779 #ifdef USE_GTK
8780 if (FRAME_EXTERNAL_TOOL_BAR (f))
8781 update_frame_tool_bar (f);
8782 return 0;
8783 #endif
8784
8785 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8786 do anything. This means you must start with tool-bar-lines
8787 non-zero to get the auto-sizing effect. Or in other words, you
8788 can turn off tool-bars by specifying tool-bar-lines zero. */
8789 if (!WINDOWP (f->tool_bar_window)
8790 || (w = XWINDOW (f->tool_bar_window),
8791 WINDOW_TOTAL_LINES (w) == 0))
8792 return 0;
8793
8794 /* Set up an iterator for the tool-bar window. */
8795 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8796 it.first_visible_x = 0;
8797 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8798 row = it.glyph_row;
8799
8800 /* Build a string that represents the contents of the tool-bar. */
8801 build_desired_tool_bar_string (f);
8802 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8803
8804 /* Display as many lines as needed to display all tool-bar items. */
8805 while (it.current_y < it.last_visible_y)
8806 display_tool_bar_line (&it);
8807
8808 /* It doesn't make much sense to try scrolling in the tool-bar
8809 window, so don't do it. */
8810 w->desired_matrix->no_scrolling_p = 1;
8811 w->must_be_updated_p = 1;
8812
8813 if (auto_resize_tool_bars_p)
8814 {
8815 int nlines;
8816
8817 /* If we couldn't display everything, change the tool-bar's
8818 height. */
8819 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8820 change_height_p = 1;
8821
8822 /* If there are blank lines at the end, except for a partially
8823 visible blank line at the end that is smaller than
8824 FRAME_LINE_HEIGHT, change the tool-bar's height. */
8825 row = it.glyph_row - 1;
8826 if (!row->displays_text_p
8827 && row->height >= FRAME_LINE_HEIGHT (f))
8828 change_height_p = 1;
8829
8830 /* If row displays tool-bar items, but is partially visible,
8831 change the tool-bar's height. */
8832 if (row->displays_text_p
8833 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8834 change_height_p = 1;
8835
8836 /* Resize windows as needed by changing the `tool-bar-lines'
8837 frame parameter. */
8838 if (change_height_p
8839 && (nlines = tool_bar_lines_needed (f),
8840 nlines != WINDOW_TOTAL_LINES (w)))
8841 {
8842 extern Lisp_Object Qtool_bar_lines;
8843 Lisp_Object frame;
8844 int old_height = WINDOW_TOTAL_LINES (w);
8845
8846 XSETFRAME (frame, f);
8847 clear_glyph_matrix (w->desired_matrix);
8848 Fmodify_frame_parameters (frame,
8849 Fcons (Fcons (Qtool_bar_lines,
8850 make_number (nlines)),
8851 Qnil));
8852 if (WINDOW_TOTAL_LINES (w) != old_height)
8853 fonts_changed_p = 1;
8854 }
8855 }
8856
8857 return change_height_p;
8858 }
8859
8860
8861 /* Get information about the tool-bar item which is displayed in GLYPH
8862 on frame F. Return in *PROP_IDX the index where tool-bar item
8863 properties start in F->tool_bar_items. Value is zero if
8864 GLYPH doesn't display a tool-bar item. */
8865
8866 static int
8867 tool_bar_item_info (f, glyph, prop_idx)
8868 struct frame *f;
8869 struct glyph *glyph;
8870 int *prop_idx;
8871 {
8872 Lisp_Object prop;
8873 int success_p;
8874 int charpos;
8875
8876 /* This function can be called asynchronously, which means we must
8877 exclude any possibility that Fget_text_property signals an
8878 error. */
8879 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8880 charpos = max (0, charpos);
8881
8882 /* Get the text property `menu-item' at pos. The value of that
8883 property is the start index of this item's properties in
8884 F->tool_bar_items. */
8885 prop = Fget_text_property (make_number (charpos),
8886 Qmenu_item, f->current_tool_bar_string);
8887 if (INTEGERP (prop))
8888 {
8889 *prop_idx = XINT (prop);
8890 success_p = 1;
8891 }
8892 else
8893 success_p = 0;
8894
8895 return success_p;
8896 }
8897
8898 \f
8899 /* Get information about the tool-bar item at position X/Y on frame F.
8900 Return in *GLYPH a pointer to the glyph of the tool-bar item in
8901 the current matrix of the tool-bar window of F, or NULL if not
8902 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8903 item in F->tool_bar_items. Value is
8904
8905 -1 if X/Y is not on a tool-bar item
8906 0 if X/Y is on the same item that was highlighted before.
8907 1 otherwise. */
8908
8909 static int
8910 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
8911 struct frame *f;
8912 int x, y;
8913 struct glyph **glyph;
8914 int *hpos, *vpos, *prop_idx;
8915 {
8916 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8917 struct window *w = XWINDOW (f->tool_bar_window);
8918 int area;
8919
8920 /* Find the glyph under X/Y. */
8921 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
8922 if (*glyph == NULL)
8923 return -1;
8924
8925 /* Get the start of this tool-bar item's properties in
8926 f->tool_bar_items. */
8927 if (!tool_bar_item_info (f, *glyph, prop_idx))
8928 return -1;
8929
8930 /* Is mouse on the highlighted item? */
8931 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
8932 && *vpos >= dpyinfo->mouse_face_beg_row
8933 && *vpos <= dpyinfo->mouse_face_end_row
8934 && (*vpos > dpyinfo->mouse_face_beg_row
8935 || *hpos >= dpyinfo->mouse_face_beg_col)
8936 && (*vpos < dpyinfo->mouse_face_end_row
8937 || *hpos < dpyinfo->mouse_face_end_col
8938 || dpyinfo->mouse_face_past_end))
8939 return 0;
8940
8941 return 1;
8942 }
8943
8944
8945 /* EXPORT:
8946 Handle mouse button event on the tool-bar of frame F, at
8947 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
8948 0 for button release. MODIFIERS is event modifiers for button
8949 release. */
8950
8951 void
8952 handle_tool_bar_click (f, x, y, down_p, modifiers)
8953 struct frame *f;
8954 int x, y, down_p;
8955 unsigned int modifiers;
8956 {
8957 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8958 struct window *w = XWINDOW (f->tool_bar_window);
8959 int hpos, vpos, prop_idx;
8960 struct glyph *glyph;
8961 Lisp_Object enabled_p;
8962
8963 /* If not on the highlighted tool-bar item, return. */
8964 frame_to_window_pixel_xy (w, &x, &y);
8965 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
8966 return;
8967
8968 /* If item is disabled, do nothing. */
8969 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8970 if (NILP (enabled_p))
8971 return;
8972
8973 if (down_p)
8974 {
8975 /* Show item in pressed state. */
8976 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
8977 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
8978 last_tool_bar_item = prop_idx;
8979 }
8980 else
8981 {
8982 Lisp_Object key, frame;
8983 struct input_event event;
8984 EVENT_INIT (event);
8985
8986 /* Show item in released state. */
8987 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
8988 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
8989
8990 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
8991
8992 XSETFRAME (frame, f);
8993 event.kind = TOOL_BAR_EVENT;
8994 event.frame_or_window = frame;
8995 event.arg = frame;
8996 kbd_buffer_store_event (&event);
8997
8998 event.kind = TOOL_BAR_EVENT;
8999 event.frame_or_window = frame;
9000 event.arg = key;
9001 event.modifiers = modifiers;
9002 kbd_buffer_store_event (&event);
9003 last_tool_bar_item = -1;
9004 }
9005 }
9006
9007
9008 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9009 tool-bar window-relative coordinates X/Y. Called from
9010 note_mouse_highlight. */
9011
9012 static void
9013 note_tool_bar_highlight (f, x, y)
9014 struct frame *f;
9015 int x, y;
9016 {
9017 Lisp_Object window = f->tool_bar_window;
9018 struct window *w = XWINDOW (window);
9019 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9020 int hpos, vpos;
9021 struct glyph *glyph;
9022 struct glyph_row *row;
9023 int i;
9024 Lisp_Object enabled_p;
9025 int prop_idx;
9026 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9027 int mouse_down_p, rc;
9028
9029 /* Function note_mouse_highlight is called with negative x(y
9030 values when mouse moves outside of the frame. */
9031 if (x <= 0 || y <= 0)
9032 {
9033 clear_mouse_face (dpyinfo);
9034 return;
9035 }
9036
9037 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9038 if (rc < 0)
9039 {
9040 /* Not on tool-bar item. */
9041 clear_mouse_face (dpyinfo);
9042 return;
9043 }
9044 else if (rc == 0)
9045 /* On same tool-bar item as before. */
9046 goto set_help_echo;
9047
9048 clear_mouse_face (dpyinfo);
9049
9050 /* Mouse is down, but on different tool-bar item? */
9051 mouse_down_p = (dpyinfo->grabbed
9052 && f == last_mouse_frame
9053 && FRAME_LIVE_P (f));
9054 if (mouse_down_p
9055 && last_tool_bar_item != prop_idx)
9056 return;
9057
9058 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9059 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9060
9061 /* If tool-bar item is not enabled, don't highlight it. */
9062 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9063 if (!NILP (enabled_p))
9064 {
9065 /* Compute the x-position of the glyph. In front and past the
9066 image is a space. We include this in the highlighted area. */
9067 row = MATRIX_ROW (w->current_matrix, vpos);
9068 for (i = x = 0; i < hpos; ++i)
9069 x += row->glyphs[TEXT_AREA][i].pixel_width;
9070
9071 /* Record this as the current active region. */
9072 dpyinfo->mouse_face_beg_col = hpos;
9073 dpyinfo->mouse_face_beg_row = vpos;
9074 dpyinfo->mouse_face_beg_x = x;
9075 dpyinfo->mouse_face_beg_y = row->y;
9076 dpyinfo->mouse_face_past_end = 0;
9077
9078 dpyinfo->mouse_face_end_col = hpos + 1;
9079 dpyinfo->mouse_face_end_row = vpos;
9080 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9081 dpyinfo->mouse_face_end_y = row->y;
9082 dpyinfo->mouse_face_window = window;
9083 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9084
9085 /* Display it as active. */
9086 show_mouse_face (dpyinfo, draw);
9087 dpyinfo->mouse_face_image_state = draw;
9088 }
9089
9090 set_help_echo:
9091
9092 /* Set help_echo_string to a help string to display for this tool-bar item.
9093 XTread_socket does the rest. */
9094 help_echo_object = help_echo_window = Qnil;
9095 help_echo_pos = -1;
9096 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9097 if (NILP (help_echo_string))
9098 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9099 }
9100
9101 #endif /* HAVE_WINDOW_SYSTEM */
9102
9103
9104 \f
9105 /************************************************************************
9106 Horizontal scrolling
9107 ************************************************************************/
9108
9109 static int hscroll_window_tree P_ ((Lisp_Object));
9110 static int hscroll_windows P_ ((Lisp_Object));
9111
9112 /* For all leaf windows in the window tree rooted at WINDOW, set their
9113 hscroll value so that PT is (i) visible in the window, and (ii) so
9114 that it is not within a certain margin at the window's left and
9115 right border. Value is non-zero if any window's hscroll has been
9116 changed. */
9117
9118 static int
9119 hscroll_window_tree (window)
9120 Lisp_Object window;
9121 {
9122 int hscrolled_p = 0;
9123 int hscroll_relative_p = FLOATP (Vhscroll_step);
9124 int hscroll_step_abs = 0;
9125 double hscroll_step_rel = 0;
9126
9127 if (hscroll_relative_p)
9128 {
9129 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9130 if (hscroll_step_rel < 0)
9131 {
9132 hscroll_relative_p = 0;
9133 hscroll_step_abs = 0;
9134 }
9135 }
9136 else if (INTEGERP (Vhscroll_step))
9137 {
9138 hscroll_step_abs = XINT (Vhscroll_step);
9139 if (hscroll_step_abs < 0)
9140 hscroll_step_abs = 0;
9141 }
9142 else
9143 hscroll_step_abs = 0;
9144
9145 while (WINDOWP (window))
9146 {
9147 struct window *w = XWINDOW (window);
9148
9149 if (WINDOWP (w->hchild))
9150 hscrolled_p |= hscroll_window_tree (w->hchild);
9151 else if (WINDOWP (w->vchild))
9152 hscrolled_p |= hscroll_window_tree (w->vchild);
9153 else if (w->cursor.vpos >= 0)
9154 {
9155 int h_margin;
9156 int text_area_width;
9157 struct glyph_row *current_cursor_row
9158 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9159 struct glyph_row *desired_cursor_row
9160 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9161 struct glyph_row *cursor_row
9162 = (desired_cursor_row->enabled_p
9163 ? desired_cursor_row
9164 : current_cursor_row);
9165
9166 text_area_width = window_box_width (w, TEXT_AREA);
9167
9168 /* Scroll when cursor is inside this scroll margin. */
9169 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9170
9171 if ((XFASTINT (w->hscroll)
9172 && w->cursor.x <= h_margin)
9173 || (cursor_row->enabled_p
9174 && cursor_row->truncated_on_right_p
9175 && (w->cursor.x >= text_area_width - h_margin)))
9176 {
9177 struct it it;
9178 int hscroll;
9179 struct buffer *saved_current_buffer;
9180 int pt;
9181 int wanted_x;
9182
9183 /* Find point in a display of infinite width. */
9184 saved_current_buffer = current_buffer;
9185 current_buffer = XBUFFER (w->buffer);
9186
9187 if (w == XWINDOW (selected_window))
9188 pt = BUF_PT (current_buffer);
9189 else
9190 {
9191 pt = marker_position (w->pointm);
9192 pt = max (BEGV, pt);
9193 pt = min (ZV, pt);
9194 }
9195
9196 /* Move iterator to pt starting at cursor_row->start in
9197 a line with infinite width. */
9198 init_to_row_start (&it, w, cursor_row);
9199 it.last_visible_x = INFINITY;
9200 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9201 current_buffer = saved_current_buffer;
9202
9203 /* Position cursor in window. */
9204 if (!hscroll_relative_p && hscroll_step_abs == 0)
9205 hscroll = max (0, (it.current_x
9206 - (ITERATOR_AT_END_OF_LINE_P (&it)
9207 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9208 : (text_area_width / 2))))
9209 / FRAME_COLUMN_WIDTH (it.f);
9210 else if (w->cursor.x >= text_area_width - h_margin)
9211 {
9212 if (hscroll_relative_p)
9213 wanted_x = text_area_width * (1 - hscroll_step_rel)
9214 - h_margin;
9215 else
9216 wanted_x = text_area_width
9217 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9218 - h_margin;
9219 hscroll
9220 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9221 }
9222 else
9223 {
9224 if (hscroll_relative_p)
9225 wanted_x = text_area_width * hscroll_step_rel
9226 + h_margin;
9227 else
9228 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9229 + h_margin;
9230 hscroll
9231 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9232 }
9233 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9234
9235 /* Don't call Fset_window_hscroll if value hasn't
9236 changed because it will prevent redisplay
9237 optimizations. */
9238 if (XFASTINT (w->hscroll) != hscroll)
9239 {
9240 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9241 w->hscroll = make_number (hscroll);
9242 hscrolled_p = 1;
9243 }
9244 }
9245 }
9246
9247 window = w->next;
9248 }
9249
9250 /* Value is non-zero if hscroll of any leaf window has been changed. */
9251 return hscrolled_p;
9252 }
9253
9254
9255 /* Set hscroll so that cursor is visible and not inside horizontal
9256 scroll margins for all windows in the tree rooted at WINDOW. See
9257 also hscroll_window_tree above. Value is non-zero if any window's
9258 hscroll has been changed. If it has, desired matrices on the frame
9259 of WINDOW are cleared. */
9260
9261 static int
9262 hscroll_windows (window)
9263 Lisp_Object window;
9264 {
9265 int hscrolled_p;
9266
9267 if (automatic_hscrolling_p)
9268 {
9269 hscrolled_p = hscroll_window_tree (window);
9270 if (hscrolled_p)
9271 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9272 }
9273 else
9274 hscrolled_p = 0;
9275 return hscrolled_p;
9276 }
9277
9278
9279 \f
9280 /************************************************************************
9281 Redisplay
9282 ************************************************************************/
9283
9284 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9285 to a non-zero value. This is sometimes handy to have in a debugger
9286 session. */
9287
9288 #if GLYPH_DEBUG
9289
9290 /* First and last unchanged row for try_window_id. */
9291
9292 int debug_first_unchanged_at_end_vpos;
9293 int debug_last_unchanged_at_beg_vpos;
9294
9295 /* Delta vpos and y. */
9296
9297 int debug_dvpos, debug_dy;
9298
9299 /* Delta in characters and bytes for try_window_id. */
9300
9301 int debug_delta, debug_delta_bytes;
9302
9303 /* Values of window_end_pos and window_end_vpos at the end of
9304 try_window_id. */
9305
9306 EMACS_INT debug_end_pos, debug_end_vpos;
9307
9308 /* Append a string to W->desired_matrix->method. FMT is a printf
9309 format string. A1...A9 are a supplement for a variable-length
9310 argument list. If trace_redisplay_p is non-zero also printf the
9311 resulting string to stderr. */
9312
9313 static void
9314 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9315 struct window *w;
9316 char *fmt;
9317 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9318 {
9319 char buffer[512];
9320 char *method = w->desired_matrix->method;
9321 int len = strlen (method);
9322 int size = sizeof w->desired_matrix->method;
9323 int remaining = size - len - 1;
9324
9325 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9326 if (len && remaining)
9327 {
9328 method[len] = '|';
9329 --remaining, ++len;
9330 }
9331
9332 strncpy (method + len, buffer, remaining);
9333
9334 if (trace_redisplay_p)
9335 fprintf (stderr, "%p (%s): %s\n",
9336 w,
9337 ((BUFFERP (w->buffer)
9338 && STRINGP (XBUFFER (w->buffer)->name))
9339 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9340 : "no buffer"),
9341 buffer);
9342 }
9343
9344 #endif /* GLYPH_DEBUG */
9345
9346
9347 /* Value is non-zero if all changes in window W, which displays
9348 current_buffer, are in the text between START and END. START is a
9349 buffer position, END is given as a distance from Z. Used in
9350 redisplay_internal for display optimization. */
9351
9352 static INLINE int
9353 text_outside_line_unchanged_p (w, start, end)
9354 struct window *w;
9355 int start, end;
9356 {
9357 int unchanged_p = 1;
9358
9359 /* If text or overlays have changed, see where. */
9360 if (XFASTINT (w->last_modified) < MODIFF
9361 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9362 {
9363 /* Gap in the line? */
9364 if (GPT < start || Z - GPT < end)
9365 unchanged_p = 0;
9366
9367 /* Changes start in front of the line, or end after it? */
9368 if (unchanged_p
9369 && (BEG_UNCHANGED < start - 1
9370 || END_UNCHANGED < end))
9371 unchanged_p = 0;
9372
9373 /* If selective display, can't optimize if changes start at the
9374 beginning of the line. */
9375 if (unchanged_p
9376 && INTEGERP (current_buffer->selective_display)
9377 && XINT (current_buffer->selective_display) > 0
9378 && (BEG_UNCHANGED < start || GPT <= start))
9379 unchanged_p = 0;
9380
9381 /* If there are overlays at the start or end of the line, these
9382 may have overlay strings with newlines in them. A change at
9383 START, for instance, may actually concern the display of such
9384 overlay strings as well, and they are displayed on different
9385 lines. So, quickly rule out this case. (For the future, it
9386 might be desirable to implement something more telling than
9387 just BEG/END_UNCHANGED.) */
9388 if (unchanged_p)
9389 {
9390 if (BEG + BEG_UNCHANGED == start
9391 && overlay_touches_p (start))
9392 unchanged_p = 0;
9393 if (END_UNCHANGED == end
9394 && overlay_touches_p (Z - end))
9395 unchanged_p = 0;
9396 }
9397 }
9398
9399 return unchanged_p;
9400 }
9401
9402
9403 /* Do a frame update, taking possible shortcuts into account. This is
9404 the main external entry point for redisplay.
9405
9406 If the last redisplay displayed an echo area message and that message
9407 is no longer requested, we clear the echo area or bring back the
9408 mini-buffer if that is in use. */
9409
9410 void
9411 redisplay ()
9412 {
9413 redisplay_internal (0);
9414 }
9415
9416
9417 static Lisp_Object
9418 overlay_arrow_string_or_property (var, pbitmap)
9419 Lisp_Object var;
9420 int *pbitmap;
9421 {
9422 Lisp_Object pstr = Fget (var, Qoverlay_arrow_string);
9423 Lisp_Object bitmap;
9424
9425 if (pbitmap)
9426 {
9427 *pbitmap = 0;
9428 if (bitmap = Fget (var, Qoverlay_arrow_bitmap), INTEGERP (bitmap))
9429 *pbitmap = XINT (bitmap);
9430 }
9431
9432 if (!NILP (pstr))
9433 return pstr;
9434 return Voverlay_arrow_string;
9435 }
9436
9437 /* Return 1 if there are any overlay-arrows in current_buffer. */
9438 static int
9439 overlay_arrow_in_current_buffer_p ()
9440 {
9441 Lisp_Object vlist;
9442
9443 for (vlist = Voverlay_arrow_variable_list;
9444 CONSP (vlist);
9445 vlist = XCDR (vlist))
9446 {
9447 Lisp_Object var = XCAR (vlist);
9448 Lisp_Object val;
9449
9450 if (!SYMBOLP (var))
9451 continue;
9452 val = find_symbol_value (var);
9453 if (MARKERP (val)
9454 && current_buffer == XMARKER (val)->buffer)
9455 return 1;
9456 }
9457 return 0;
9458 }
9459
9460
9461 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9462 has changed. */
9463
9464 static int
9465 overlay_arrows_changed_p ()
9466 {
9467 Lisp_Object vlist;
9468
9469 for (vlist = Voverlay_arrow_variable_list;
9470 CONSP (vlist);
9471 vlist = XCDR (vlist))
9472 {
9473 Lisp_Object var = XCAR (vlist);
9474 Lisp_Object val, pstr;
9475
9476 if (!SYMBOLP (var))
9477 continue;
9478 val = find_symbol_value (var);
9479 if (!MARKERP (val))
9480 continue;
9481 if (! EQ (COERCE_MARKER (val),
9482 Fget (var, Qlast_arrow_position))
9483 || ! (pstr = overlay_arrow_string_or_property (var, 0),
9484 EQ (pstr, Fget (var, Qlast_arrow_string))))
9485 return 1;
9486 }
9487 return 0;
9488 }
9489
9490 /* Mark overlay arrows to be updated on next redisplay. */
9491
9492 static void
9493 update_overlay_arrows (up_to_date)
9494 int up_to_date;
9495 {
9496 Lisp_Object vlist;
9497
9498 for (vlist = Voverlay_arrow_variable_list;
9499 CONSP (vlist);
9500 vlist = XCDR (vlist))
9501 {
9502 Lisp_Object var = XCAR (vlist);
9503
9504 if (!SYMBOLP (var))
9505 continue;
9506
9507 if (up_to_date > 0)
9508 {
9509 Lisp_Object val = find_symbol_value (var);
9510 Fput (var, Qlast_arrow_position,
9511 COERCE_MARKER (val));
9512 Fput (var, Qlast_arrow_string,
9513 overlay_arrow_string_or_property (var, 0));
9514 }
9515 else if (up_to_date < 0
9516 || !NILP (Fget (var, Qlast_arrow_position)))
9517 {
9518 Fput (var, Qlast_arrow_position, Qt);
9519 Fput (var, Qlast_arrow_string, Qt);
9520 }
9521 }
9522 }
9523
9524
9525 /* Return overlay arrow string at row, or nil. */
9526
9527 static Lisp_Object
9528 overlay_arrow_at_row (f, row, pbitmap)
9529 struct frame *f;
9530 struct glyph_row *row;
9531 int *pbitmap;
9532 {
9533 Lisp_Object vlist;
9534
9535 for (vlist = Voverlay_arrow_variable_list;
9536 CONSP (vlist);
9537 vlist = XCDR (vlist))
9538 {
9539 Lisp_Object var = XCAR (vlist);
9540 Lisp_Object val;
9541
9542 if (!SYMBOLP (var))
9543 continue;
9544
9545 val = find_symbol_value (var);
9546
9547 if (MARKERP (val)
9548 && current_buffer == XMARKER (val)->buffer
9549 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
9550 {
9551 val = overlay_arrow_string_or_property (var, pbitmap);
9552 if (FRAME_WINDOW_P (f))
9553 return Qt;
9554 else if (STRINGP (val))
9555 return val;
9556 break;
9557 }
9558 }
9559
9560 *pbitmap = 0;
9561 return Qnil;
9562 }
9563
9564 /* Return 1 if point moved out of or into a composition. Otherwise
9565 return 0. PREV_BUF and PREV_PT are the last point buffer and
9566 position. BUF and PT are the current point buffer and position. */
9567
9568 int
9569 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9570 struct buffer *prev_buf, *buf;
9571 int prev_pt, pt;
9572 {
9573 int start, end;
9574 Lisp_Object prop;
9575 Lisp_Object buffer;
9576
9577 XSETBUFFER (buffer, buf);
9578 /* Check a composition at the last point if point moved within the
9579 same buffer. */
9580 if (prev_buf == buf)
9581 {
9582 if (prev_pt == pt)
9583 /* Point didn't move. */
9584 return 0;
9585
9586 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9587 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9588 && COMPOSITION_VALID_P (start, end, prop)
9589 && start < prev_pt && end > prev_pt)
9590 /* The last point was within the composition. Return 1 iff
9591 point moved out of the composition. */
9592 return (pt <= start || pt >= end);
9593 }
9594
9595 /* Check a composition at the current point. */
9596 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9597 && find_composition (pt, -1, &start, &end, &prop, buffer)
9598 && COMPOSITION_VALID_P (start, end, prop)
9599 && start < pt && end > pt);
9600 }
9601
9602
9603 /* Reconsider the setting of B->clip_changed which is displayed
9604 in window W. */
9605
9606 static INLINE void
9607 reconsider_clip_changes (w, b)
9608 struct window *w;
9609 struct buffer *b;
9610 {
9611 if (b->clip_changed
9612 && !NILP (w->window_end_valid)
9613 && w->current_matrix->buffer == b
9614 && w->current_matrix->zv == BUF_ZV (b)
9615 && w->current_matrix->begv == BUF_BEGV (b))
9616 b->clip_changed = 0;
9617
9618 /* If display wasn't paused, and W is not a tool bar window, see if
9619 point has been moved into or out of a composition. In that case,
9620 we set b->clip_changed to 1 to force updating the screen. If
9621 b->clip_changed has already been set to 1, we can skip this
9622 check. */
9623 if (!b->clip_changed
9624 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9625 {
9626 int pt;
9627
9628 if (w == XWINDOW (selected_window))
9629 pt = BUF_PT (current_buffer);
9630 else
9631 pt = marker_position (w->pointm);
9632
9633 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
9634 || pt != XINT (w->last_point))
9635 && check_point_in_composition (w->current_matrix->buffer,
9636 XINT (w->last_point),
9637 XBUFFER (w->buffer), pt))
9638 b->clip_changed = 1;
9639 }
9640 }
9641 \f
9642
9643 /* Select FRAME to forward the values of frame-local variables into C
9644 variables so that the redisplay routines can access those values
9645 directly. */
9646
9647 static void
9648 select_frame_for_redisplay (frame)
9649 Lisp_Object frame;
9650 {
9651 Lisp_Object tail, sym, val;
9652 Lisp_Object old = selected_frame;
9653
9654 selected_frame = frame;
9655
9656 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
9657 if (CONSP (XCAR (tail))
9658 && (sym = XCAR (XCAR (tail)),
9659 SYMBOLP (sym))
9660 && (sym = indirect_variable (sym),
9661 val = SYMBOL_VALUE (sym),
9662 (BUFFER_LOCAL_VALUEP (val)
9663 || SOME_BUFFER_LOCAL_VALUEP (val)))
9664 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9665 Fsymbol_value (sym);
9666
9667 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
9668 if (CONSP (XCAR (tail))
9669 && (sym = XCAR (XCAR (tail)),
9670 SYMBOLP (sym))
9671 && (sym = indirect_variable (sym),
9672 val = SYMBOL_VALUE (sym),
9673 (BUFFER_LOCAL_VALUEP (val)
9674 || SOME_BUFFER_LOCAL_VALUEP (val)))
9675 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9676 Fsymbol_value (sym);
9677 }
9678
9679
9680 #define STOP_POLLING \
9681 do { if (! polling_stopped_here) stop_polling (); \
9682 polling_stopped_here = 1; } while (0)
9683
9684 #define RESUME_POLLING \
9685 do { if (polling_stopped_here) start_polling (); \
9686 polling_stopped_here = 0; } while (0)
9687
9688
9689 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9690 response to any user action; therefore, we should preserve the echo
9691 area. (Actually, our caller does that job.) Perhaps in the future
9692 avoid recentering windows if it is not necessary; currently that
9693 causes some problems. */
9694
9695 static void
9696 redisplay_internal (preserve_echo_area)
9697 int preserve_echo_area;
9698 {
9699 struct window *w = XWINDOW (selected_window);
9700 struct frame *f = XFRAME (w->frame);
9701 int pause;
9702 int must_finish = 0;
9703 struct text_pos tlbufpos, tlendpos;
9704 int number_of_visible_frames;
9705 int count;
9706 struct frame *sf = SELECTED_FRAME ();
9707 int polling_stopped_here = 0;
9708
9709 /* Non-zero means redisplay has to consider all windows on all
9710 frames. Zero means, only selected_window is considered. */
9711 int consider_all_windows_p;
9712
9713 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9714
9715 /* No redisplay if running in batch mode or frame is not yet fully
9716 initialized, or redisplay is explicitly turned off by setting
9717 Vinhibit_redisplay. */
9718 if (noninteractive
9719 || !NILP (Vinhibit_redisplay)
9720 || !f->glyphs_initialized_p)
9721 return;
9722
9723 /* The flag redisplay_performed_directly_p is set by
9724 direct_output_for_insert when it already did the whole screen
9725 update necessary. */
9726 if (redisplay_performed_directly_p)
9727 {
9728 redisplay_performed_directly_p = 0;
9729 if (!hscroll_windows (selected_window))
9730 return;
9731 }
9732
9733 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9734 if (popup_activated ())
9735 return;
9736 #endif
9737
9738 /* I don't think this happens but let's be paranoid. */
9739 if (redisplaying_p)
9740 return;
9741
9742 /* Record a function that resets redisplaying_p to its old value
9743 when we leave this function. */
9744 count = SPECPDL_INDEX ();
9745 record_unwind_protect (unwind_redisplay,
9746 Fcons (make_number (redisplaying_p), selected_frame));
9747 ++redisplaying_p;
9748 specbind (Qinhibit_free_realized_faces, Qnil);
9749
9750 retry:
9751 pause = 0;
9752 reconsider_clip_changes (w, current_buffer);
9753
9754 /* If new fonts have been loaded that make a glyph matrix adjustment
9755 necessary, do it. */
9756 if (fonts_changed_p)
9757 {
9758 adjust_glyphs (NULL);
9759 ++windows_or_buffers_changed;
9760 fonts_changed_p = 0;
9761 }
9762
9763 /* If face_change_count is non-zero, init_iterator will free all
9764 realized faces, which includes the faces referenced from current
9765 matrices. So, we can't reuse current matrices in this case. */
9766 if (face_change_count)
9767 ++windows_or_buffers_changed;
9768
9769 if (! FRAME_WINDOW_P (sf)
9770 && previous_terminal_frame != sf)
9771 {
9772 /* Since frames on an ASCII terminal share the same display
9773 area, displaying a different frame means redisplay the whole
9774 thing. */
9775 windows_or_buffers_changed++;
9776 SET_FRAME_GARBAGED (sf);
9777 XSETFRAME (Vterminal_frame, sf);
9778 }
9779 previous_terminal_frame = sf;
9780
9781 /* Set the visible flags for all frames. Do this before checking
9782 for resized or garbaged frames; they want to know if their frames
9783 are visible. See the comment in frame.h for
9784 FRAME_SAMPLE_VISIBILITY. */
9785 {
9786 Lisp_Object tail, frame;
9787
9788 number_of_visible_frames = 0;
9789
9790 FOR_EACH_FRAME (tail, frame)
9791 {
9792 struct frame *f = XFRAME (frame);
9793
9794 FRAME_SAMPLE_VISIBILITY (f);
9795 if (FRAME_VISIBLE_P (f))
9796 ++number_of_visible_frames;
9797 clear_desired_matrices (f);
9798 }
9799 }
9800
9801 /* Notice any pending interrupt request to change frame size. */
9802 do_pending_window_change (1);
9803
9804 /* Clear frames marked as garbaged. */
9805 if (frame_garbaged)
9806 clear_garbaged_frames ();
9807
9808 /* Build menubar and tool-bar items. */
9809 prepare_menu_bars ();
9810
9811 if (windows_or_buffers_changed)
9812 update_mode_lines++;
9813
9814 /* Detect case that we need to write or remove a star in the mode line. */
9815 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
9816 {
9817 w->update_mode_line = Qt;
9818 if (buffer_shared > 1)
9819 update_mode_lines++;
9820 }
9821
9822 /* If %c is in the mode line, update it if needed. */
9823 if (!NILP (w->column_number_displayed)
9824 /* This alternative quickly identifies a common case
9825 where no change is needed. */
9826 && !(PT == XFASTINT (w->last_point)
9827 && XFASTINT (w->last_modified) >= MODIFF
9828 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9829 && (XFASTINT (w->column_number_displayed)
9830 != (int) current_column ())) /* iftc */
9831 w->update_mode_line = Qt;
9832
9833 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
9834
9835 /* The variable buffer_shared is set in redisplay_window and
9836 indicates that we redisplay a buffer in different windows. See
9837 there. */
9838 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
9839 || cursor_type_changed);
9840
9841 /* If specs for an arrow have changed, do thorough redisplay
9842 to ensure we remove any arrow that should no longer exist. */
9843 if (overlay_arrows_changed_p ())
9844 consider_all_windows_p = windows_or_buffers_changed = 1;
9845
9846 /* Normally the message* functions will have already displayed and
9847 updated the echo area, but the frame may have been trashed, or
9848 the update may have been preempted, so display the echo area
9849 again here. Checking message_cleared_p captures the case that
9850 the echo area should be cleared. */
9851 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
9852 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
9853 || (message_cleared_p
9854 && minibuf_level == 0
9855 /* If the mini-window is currently selected, this means the
9856 echo-area doesn't show through. */
9857 && !MINI_WINDOW_P (XWINDOW (selected_window))))
9858 {
9859 int window_height_changed_p = echo_area_display (0);
9860 must_finish = 1;
9861
9862 /* If we don't display the current message, don't clear the
9863 message_cleared_p flag, because, if we did, we wouldn't clear
9864 the echo area in the next redisplay which doesn't preserve
9865 the echo area. */
9866 if (!display_last_displayed_message_p)
9867 message_cleared_p = 0;
9868
9869 if (fonts_changed_p)
9870 goto retry;
9871 else if (window_height_changed_p)
9872 {
9873 consider_all_windows_p = 1;
9874 ++update_mode_lines;
9875 ++windows_or_buffers_changed;
9876
9877 /* If window configuration was changed, frames may have been
9878 marked garbaged. Clear them or we will experience
9879 surprises wrt scrolling. */
9880 if (frame_garbaged)
9881 clear_garbaged_frames ();
9882 }
9883 }
9884 else if (EQ (selected_window, minibuf_window)
9885 && (current_buffer->clip_changed
9886 || XFASTINT (w->last_modified) < MODIFF
9887 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9888 && resize_mini_window (w, 0))
9889 {
9890 /* Resized active mini-window to fit the size of what it is
9891 showing if its contents might have changed. */
9892 must_finish = 1;
9893 consider_all_windows_p = 1;
9894 ++windows_or_buffers_changed;
9895 ++update_mode_lines;
9896
9897 /* If window configuration was changed, frames may have been
9898 marked garbaged. Clear them or we will experience
9899 surprises wrt scrolling. */
9900 if (frame_garbaged)
9901 clear_garbaged_frames ();
9902 }
9903
9904
9905 /* If showing the region, and mark has changed, we must redisplay
9906 the whole window. The assignment to this_line_start_pos prevents
9907 the optimization directly below this if-statement. */
9908 if (((!NILP (Vtransient_mark_mode)
9909 && !NILP (XBUFFER (w->buffer)->mark_active))
9910 != !NILP (w->region_showing))
9911 || (!NILP (w->region_showing)
9912 && !EQ (w->region_showing,
9913 Fmarker_position (XBUFFER (w->buffer)->mark))))
9914 CHARPOS (this_line_start_pos) = 0;
9915
9916 /* Optimize the case that only the line containing the cursor in the
9917 selected window has changed. Variables starting with this_ are
9918 set in display_line and record information about the line
9919 containing the cursor. */
9920 tlbufpos = this_line_start_pos;
9921 tlendpos = this_line_end_pos;
9922 if (!consider_all_windows_p
9923 && CHARPOS (tlbufpos) > 0
9924 && NILP (w->update_mode_line)
9925 && !current_buffer->clip_changed
9926 && !current_buffer->prevent_redisplay_optimizations_p
9927 && FRAME_VISIBLE_P (XFRAME (w->frame))
9928 && !FRAME_OBSCURED_P (XFRAME (w->frame))
9929 /* Make sure recorded data applies to current buffer, etc. */
9930 && this_line_buffer == current_buffer
9931 && current_buffer == XBUFFER (w->buffer)
9932 && NILP (w->force_start)
9933 && NILP (w->optional_new_start)
9934 /* Point must be on the line that we have info recorded about. */
9935 && PT >= CHARPOS (tlbufpos)
9936 && PT <= Z - CHARPOS (tlendpos)
9937 /* All text outside that line, including its final newline,
9938 must be unchanged */
9939 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
9940 CHARPOS (tlendpos)))
9941 {
9942 if (CHARPOS (tlbufpos) > BEGV
9943 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
9944 && (CHARPOS (tlbufpos) == ZV
9945 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
9946 /* Former continuation line has disappeared by becoming empty */
9947 goto cancel;
9948 else if (XFASTINT (w->last_modified) < MODIFF
9949 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
9950 || MINI_WINDOW_P (w))
9951 {
9952 /* We have to handle the case of continuation around a
9953 wide-column character (See the comment in indent.c around
9954 line 885).
9955
9956 For instance, in the following case:
9957
9958 -------- Insert --------
9959 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
9960 J_I_ ==> J_I_ `^^' are cursors.
9961 ^^ ^^
9962 -------- --------
9963
9964 As we have to redraw the line above, we should goto cancel. */
9965
9966 struct it it;
9967 int line_height_before = this_line_pixel_height;
9968
9969 /* Note that start_display will handle the case that the
9970 line starting at tlbufpos is a continuation lines. */
9971 start_display (&it, w, tlbufpos);
9972
9973 /* Implementation note: It this still necessary? */
9974 if (it.current_x != this_line_start_x)
9975 goto cancel;
9976
9977 TRACE ((stderr, "trying display optimization 1\n"));
9978 w->cursor.vpos = -1;
9979 overlay_arrow_seen = 0;
9980 it.vpos = this_line_vpos;
9981 it.current_y = this_line_y;
9982 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
9983 display_line (&it);
9984
9985 /* If line contains point, is not continued,
9986 and ends at same distance from eob as before, we win */
9987 if (w->cursor.vpos >= 0
9988 /* Line is not continued, otherwise this_line_start_pos
9989 would have been set to 0 in display_line. */
9990 && CHARPOS (this_line_start_pos)
9991 /* Line ends as before. */
9992 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
9993 /* Line has same height as before. Otherwise other lines
9994 would have to be shifted up or down. */
9995 && this_line_pixel_height == line_height_before)
9996 {
9997 /* If this is not the window's last line, we must adjust
9998 the charstarts of the lines below. */
9999 if (it.current_y < it.last_visible_y)
10000 {
10001 struct glyph_row *row
10002 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10003 int delta, delta_bytes;
10004
10005 if (Z - CHARPOS (tlendpos) == ZV)
10006 {
10007 /* This line ends at end of (accessible part of)
10008 buffer. There is no newline to count. */
10009 delta = (Z
10010 - CHARPOS (tlendpos)
10011 - MATRIX_ROW_START_CHARPOS (row));
10012 delta_bytes = (Z_BYTE
10013 - BYTEPOS (tlendpos)
10014 - MATRIX_ROW_START_BYTEPOS (row));
10015 }
10016 else
10017 {
10018 /* This line ends in a newline. Must take
10019 account of the newline and the rest of the
10020 text that follows. */
10021 delta = (Z
10022 - CHARPOS (tlendpos)
10023 - MATRIX_ROW_START_CHARPOS (row));
10024 delta_bytes = (Z_BYTE
10025 - BYTEPOS (tlendpos)
10026 - MATRIX_ROW_START_BYTEPOS (row));
10027 }
10028
10029 increment_matrix_positions (w->current_matrix,
10030 this_line_vpos + 1,
10031 w->current_matrix->nrows,
10032 delta, delta_bytes);
10033 }
10034
10035 /* If this row displays text now but previously didn't,
10036 or vice versa, w->window_end_vpos may have to be
10037 adjusted. */
10038 if ((it.glyph_row - 1)->displays_text_p)
10039 {
10040 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10041 XSETINT (w->window_end_vpos, this_line_vpos);
10042 }
10043 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10044 && this_line_vpos > 0)
10045 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10046 w->window_end_valid = Qnil;
10047
10048 /* Update hint: No need to try to scroll in update_window. */
10049 w->desired_matrix->no_scrolling_p = 1;
10050
10051 #if GLYPH_DEBUG
10052 *w->desired_matrix->method = 0;
10053 debug_method_add (w, "optimization 1");
10054 #endif
10055 #ifdef HAVE_WINDOW_SYSTEM
10056 update_window_fringes (w, 0);
10057 #endif
10058 goto update;
10059 }
10060 else
10061 goto cancel;
10062 }
10063 else if (/* Cursor position hasn't changed. */
10064 PT == XFASTINT (w->last_point)
10065 /* Make sure the cursor was last displayed
10066 in this window. Otherwise we have to reposition it. */
10067 && 0 <= w->cursor.vpos
10068 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10069 {
10070 if (!must_finish)
10071 {
10072 do_pending_window_change (1);
10073
10074 /* We used to always goto end_of_redisplay here, but this
10075 isn't enough if we have a blinking cursor. */
10076 if (w->cursor_off_p == w->last_cursor_off_p)
10077 goto end_of_redisplay;
10078 }
10079 goto update;
10080 }
10081 /* If highlighting the region, or if the cursor is in the echo area,
10082 then we can't just move the cursor. */
10083 else if (! (!NILP (Vtransient_mark_mode)
10084 && !NILP (current_buffer->mark_active))
10085 && (EQ (selected_window, current_buffer->last_selected_window)
10086 || highlight_nonselected_windows)
10087 && NILP (w->region_showing)
10088 && NILP (Vshow_trailing_whitespace)
10089 && !cursor_in_echo_area)
10090 {
10091 struct it it;
10092 struct glyph_row *row;
10093
10094 /* Skip from tlbufpos to PT and see where it is. Note that
10095 PT may be in invisible text. If so, we will end at the
10096 next visible position. */
10097 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10098 NULL, DEFAULT_FACE_ID);
10099 it.current_x = this_line_start_x;
10100 it.current_y = this_line_y;
10101 it.vpos = this_line_vpos;
10102
10103 /* The call to move_it_to stops in front of PT, but
10104 moves over before-strings. */
10105 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10106
10107 if (it.vpos == this_line_vpos
10108 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10109 row->enabled_p))
10110 {
10111 xassert (this_line_vpos == it.vpos);
10112 xassert (this_line_y == it.current_y);
10113 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10114 #if GLYPH_DEBUG
10115 *w->desired_matrix->method = 0;
10116 debug_method_add (w, "optimization 3");
10117 #endif
10118 goto update;
10119 }
10120 else
10121 goto cancel;
10122 }
10123
10124 cancel:
10125 /* Text changed drastically or point moved off of line. */
10126 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10127 }
10128
10129 CHARPOS (this_line_start_pos) = 0;
10130 consider_all_windows_p |= buffer_shared > 1;
10131 ++clear_face_cache_count;
10132
10133
10134 /* Build desired matrices, and update the display. If
10135 consider_all_windows_p is non-zero, do it for all windows on all
10136 frames. Otherwise do it for selected_window, only. */
10137
10138 if (consider_all_windows_p)
10139 {
10140 Lisp_Object tail, frame;
10141 int i, n = 0, size = 50;
10142 struct frame **updated
10143 = (struct frame **) alloca (size * sizeof *updated);
10144
10145 /* Clear the face cache eventually. */
10146 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10147 {
10148 clear_face_cache (0);
10149 clear_face_cache_count = 0;
10150 }
10151
10152 /* Recompute # windows showing selected buffer. This will be
10153 incremented each time such a window is displayed. */
10154 buffer_shared = 0;
10155
10156 FOR_EACH_FRAME (tail, frame)
10157 {
10158 struct frame *f = XFRAME (frame);
10159
10160 if (FRAME_WINDOW_P (f) || f == sf)
10161 {
10162 if (! EQ (frame, selected_frame))
10163 /* Select the frame, for the sake of frame-local
10164 variables. */
10165 select_frame_for_redisplay (frame);
10166
10167 #ifdef HAVE_WINDOW_SYSTEM
10168 if (clear_face_cache_count % 50 == 0
10169 && FRAME_WINDOW_P (f))
10170 clear_image_cache (f, 0);
10171 #endif /* HAVE_WINDOW_SYSTEM */
10172
10173 /* Mark all the scroll bars to be removed; we'll redeem
10174 the ones we want when we redisplay their windows. */
10175 if (condemn_scroll_bars_hook)
10176 condemn_scroll_bars_hook (f);
10177
10178 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10179 redisplay_windows (FRAME_ROOT_WINDOW (f));
10180
10181 /* Any scroll bars which redisplay_windows should have
10182 nuked should now go away. */
10183 if (judge_scroll_bars_hook)
10184 judge_scroll_bars_hook (f);
10185
10186 /* If fonts changed, display again. */
10187 /* ??? rms: I suspect it is a mistake to jump all the way
10188 back to retry here. It should just retry this frame. */
10189 if (fonts_changed_p)
10190 goto retry;
10191
10192 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10193 {
10194 /* See if we have to hscroll. */
10195 if (hscroll_windows (f->root_window))
10196 goto retry;
10197
10198 /* Prevent various kinds of signals during display
10199 update. stdio is not robust about handling
10200 signals, which can cause an apparent I/O
10201 error. */
10202 if (interrupt_input)
10203 unrequest_sigio ();
10204 STOP_POLLING;
10205
10206 /* Update the display. */
10207 set_window_update_flags (XWINDOW (f->root_window), 1);
10208 pause |= update_frame (f, 0, 0);
10209 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10210 if (pause)
10211 break;
10212 #endif
10213
10214 if (n == size)
10215 {
10216 int nbytes = size * sizeof *updated;
10217 struct frame **p = (struct frame **) alloca (2 * nbytes);
10218 bcopy (updated, p, nbytes);
10219 size *= 2;
10220 }
10221
10222 updated[n++] = f;
10223 }
10224 }
10225 }
10226
10227 if (!pause)
10228 {
10229 /* Do the mark_window_display_accurate after all windows have
10230 been redisplayed because this call resets flags in buffers
10231 which are needed for proper redisplay. */
10232 for (i = 0; i < n; ++i)
10233 {
10234 struct frame *f = updated[i];
10235 mark_window_display_accurate (f->root_window, 1);
10236 if (frame_up_to_date_hook)
10237 frame_up_to_date_hook (f);
10238 }
10239 }
10240 }
10241 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10242 {
10243 Lisp_Object mini_window;
10244 struct frame *mini_frame;
10245
10246 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10247 /* Use list_of_error, not Qerror, so that
10248 we catch only errors and don't run the debugger. */
10249 internal_condition_case_1 (redisplay_window_1, selected_window,
10250 list_of_error,
10251 redisplay_window_error);
10252
10253 /* Compare desired and current matrices, perform output. */
10254
10255 update:
10256 /* If fonts changed, display again. */
10257 if (fonts_changed_p)
10258 goto retry;
10259
10260 /* Prevent various kinds of signals during display update.
10261 stdio is not robust about handling signals,
10262 which can cause an apparent I/O error. */
10263 if (interrupt_input)
10264 unrequest_sigio ();
10265 STOP_POLLING;
10266
10267 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10268 {
10269 if (hscroll_windows (selected_window))
10270 goto retry;
10271
10272 XWINDOW (selected_window)->must_be_updated_p = 1;
10273 pause = update_frame (sf, 0, 0);
10274 }
10275
10276 /* We may have called echo_area_display at the top of this
10277 function. If the echo area is on another frame, that may
10278 have put text on a frame other than the selected one, so the
10279 above call to update_frame would not have caught it. Catch
10280 it here. */
10281 mini_window = FRAME_MINIBUF_WINDOW (sf);
10282 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10283
10284 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10285 {
10286 XWINDOW (mini_window)->must_be_updated_p = 1;
10287 pause |= update_frame (mini_frame, 0, 0);
10288 if (!pause && hscroll_windows (mini_window))
10289 goto retry;
10290 }
10291 }
10292
10293 /* If display was paused because of pending input, make sure we do a
10294 thorough update the next time. */
10295 if (pause)
10296 {
10297 /* Prevent the optimization at the beginning of
10298 redisplay_internal that tries a single-line update of the
10299 line containing the cursor in the selected window. */
10300 CHARPOS (this_line_start_pos) = 0;
10301
10302 /* Let the overlay arrow be updated the next time. */
10303 update_overlay_arrows (0);
10304
10305 /* If we pause after scrolling, some rows in the current
10306 matrices of some windows are not valid. */
10307 if (!WINDOW_FULL_WIDTH_P (w)
10308 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10309 update_mode_lines = 1;
10310 }
10311 else
10312 {
10313 if (!consider_all_windows_p)
10314 {
10315 /* This has already been done above if
10316 consider_all_windows_p is set. */
10317 mark_window_display_accurate_1 (w, 1);
10318
10319 /* Say overlay arrows are up to date. */
10320 update_overlay_arrows (1);
10321
10322 if (frame_up_to_date_hook != 0)
10323 frame_up_to_date_hook (sf);
10324 }
10325
10326 update_mode_lines = 0;
10327 windows_or_buffers_changed = 0;
10328 cursor_type_changed = 0;
10329 }
10330
10331 /* Start SIGIO interrupts coming again. Having them off during the
10332 code above makes it less likely one will discard output, but not
10333 impossible, since there might be stuff in the system buffer here.
10334 But it is much hairier to try to do anything about that. */
10335 if (interrupt_input)
10336 request_sigio ();
10337 RESUME_POLLING;
10338
10339 /* If a frame has become visible which was not before, redisplay
10340 again, so that we display it. Expose events for such a frame
10341 (which it gets when becoming visible) don't call the parts of
10342 redisplay constructing glyphs, so simply exposing a frame won't
10343 display anything in this case. So, we have to display these
10344 frames here explicitly. */
10345 if (!pause)
10346 {
10347 Lisp_Object tail, frame;
10348 int new_count = 0;
10349
10350 FOR_EACH_FRAME (tail, frame)
10351 {
10352 int this_is_visible = 0;
10353
10354 if (XFRAME (frame)->visible)
10355 this_is_visible = 1;
10356 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10357 if (XFRAME (frame)->visible)
10358 this_is_visible = 1;
10359
10360 if (this_is_visible)
10361 new_count++;
10362 }
10363
10364 if (new_count != number_of_visible_frames)
10365 windows_or_buffers_changed++;
10366 }
10367
10368 /* Change frame size now if a change is pending. */
10369 do_pending_window_change (1);
10370
10371 /* If we just did a pending size change, or have additional
10372 visible frames, redisplay again. */
10373 if (windows_or_buffers_changed && !pause)
10374 goto retry;
10375
10376 end_of_redisplay:
10377 unbind_to (count, Qnil);
10378 RESUME_POLLING;
10379 }
10380
10381
10382 /* Redisplay, but leave alone any recent echo area message unless
10383 another message has been requested in its place.
10384
10385 This is useful in situations where you need to redisplay but no
10386 user action has occurred, making it inappropriate for the message
10387 area to be cleared. See tracking_off and
10388 wait_reading_process_output for examples of these situations.
10389
10390 FROM_WHERE is an integer saying from where this function was
10391 called. This is useful for debugging. */
10392
10393 void
10394 redisplay_preserve_echo_area (from_where)
10395 int from_where;
10396 {
10397 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10398
10399 if (!NILP (echo_area_buffer[1]))
10400 {
10401 /* We have a previously displayed message, but no current
10402 message. Redisplay the previous message. */
10403 display_last_displayed_message_p = 1;
10404 redisplay_internal (1);
10405 display_last_displayed_message_p = 0;
10406 }
10407 else
10408 redisplay_internal (1);
10409 }
10410
10411
10412 /* Function registered with record_unwind_protect in
10413 redisplay_internal. Reset redisplaying_p to the value it had
10414 before redisplay_internal was called, and clear
10415 prevent_freeing_realized_faces_p. It also selects the previously
10416 selected frame. */
10417
10418 static Lisp_Object
10419 unwind_redisplay (val)
10420 Lisp_Object val;
10421 {
10422 Lisp_Object old_redisplaying_p, old_frame;
10423
10424 old_redisplaying_p = XCAR (val);
10425 redisplaying_p = XFASTINT (old_redisplaying_p);
10426 old_frame = XCDR (val);
10427 if (! EQ (old_frame, selected_frame))
10428 select_frame_for_redisplay (old_frame);
10429 return Qnil;
10430 }
10431
10432
10433 /* Mark the display of window W as accurate or inaccurate. If
10434 ACCURATE_P is non-zero mark display of W as accurate. If
10435 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10436 redisplay_internal is called. */
10437
10438 static void
10439 mark_window_display_accurate_1 (w, accurate_p)
10440 struct window *w;
10441 int accurate_p;
10442 {
10443 if (BUFFERP (w->buffer))
10444 {
10445 struct buffer *b = XBUFFER (w->buffer);
10446
10447 w->last_modified
10448 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10449 w->last_overlay_modified
10450 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10451 w->last_had_star
10452 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10453
10454 if (accurate_p)
10455 {
10456 b->clip_changed = 0;
10457 b->prevent_redisplay_optimizations_p = 0;
10458
10459 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10460 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10461 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10462 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10463
10464 w->current_matrix->buffer = b;
10465 w->current_matrix->begv = BUF_BEGV (b);
10466 w->current_matrix->zv = BUF_ZV (b);
10467
10468 w->last_cursor = w->cursor;
10469 w->last_cursor_off_p = w->cursor_off_p;
10470
10471 if (w == XWINDOW (selected_window))
10472 w->last_point = make_number (BUF_PT (b));
10473 else
10474 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10475 }
10476 }
10477
10478 if (accurate_p)
10479 {
10480 w->window_end_valid = w->buffer;
10481 #if 0 /* This is incorrect with variable-height lines. */
10482 xassert (XINT (w->window_end_vpos)
10483 < (WINDOW_TOTAL_LINES (w)
10484 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10485 #endif
10486 w->update_mode_line = Qnil;
10487 }
10488 }
10489
10490
10491 /* Mark the display of windows in the window tree rooted at WINDOW as
10492 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10493 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10494 be redisplayed the next time redisplay_internal is called. */
10495
10496 void
10497 mark_window_display_accurate (window, accurate_p)
10498 Lisp_Object window;
10499 int accurate_p;
10500 {
10501 struct window *w;
10502
10503 for (; !NILP (window); window = w->next)
10504 {
10505 w = XWINDOW (window);
10506 mark_window_display_accurate_1 (w, accurate_p);
10507
10508 if (!NILP (w->vchild))
10509 mark_window_display_accurate (w->vchild, accurate_p);
10510 if (!NILP (w->hchild))
10511 mark_window_display_accurate (w->hchild, accurate_p);
10512 }
10513
10514 if (accurate_p)
10515 {
10516 update_overlay_arrows (1);
10517 }
10518 else
10519 {
10520 /* Force a thorough redisplay the next time by setting
10521 last_arrow_position and last_arrow_string to t, which is
10522 unequal to any useful value of Voverlay_arrow_... */
10523 update_overlay_arrows (-1);
10524 }
10525 }
10526
10527
10528 /* Return value in display table DP (Lisp_Char_Table *) for character
10529 C. Since a display table doesn't have any parent, we don't have to
10530 follow parent. Do not call this function directly but use the
10531 macro DISP_CHAR_VECTOR. */
10532
10533 Lisp_Object
10534 disp_char_vector (dp, c)
10535 struct Lisp_Char_Table *dp;
10536 int c;
10537 {
10538 int code[4], i;
10539 Lisp_Object val;
10540
10541 if (SINGLE_BYTE_CHAR_P (c))
10542 return (dp->contents[c]);
10543
10544 SPLIT_CHAR (c, code[0], code[1], code[2]);
10545 if (code[1] < 32)
10546 code[1] = -1;
10547 else if (code[2] < 32)
10548 code[2] = -1;
10549
10550 /* Here, the possible range of code[0] (== charset ID) is
10551 128..max_charset. Since the top level char table contains data
10552 for multibyte characters after 256th element, we must increment
10553 code[0] by 128 to get a correct index. */
10554 code[0] += 128;
10555 code[3] = -1; /* anchor */
10556
10557 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10558 {
10559 val = dp->contents[code[i]];
10560 if (!SUB_CHAR_TABLE_P (val))
10561 return (NILP (val) ? dp->defalt : val);
10562 }
10563
10564 /* Here, val is a sub char table. We return the default value of
10565 it. */
10566 return (dp->defalt);
10567 }
10568
10569
10570 \f
10571 /***********************************************************************
10572 Window Redisplay
10573 ***********************************************************************/
10574
10575 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10576
10577 static void
10578 redisplay_windows (window)
10579 Lisp_Object window;
10580 {
10581 while (!NILP (window))
10582 {
10583 struct window *w = XWINDOW (window);
10584
10585 if (!NILP (w->hchild))
10586 redisplay_windows (w->hchild);
10587 else if (!NILP (w->vchild))
10588 redisplay_windows (w->vchild);
10589 else
10590 {
10591 displayed_buffer = XBUFFER (w->buffer);
10592 /* Use list_of_error, not Qerror, so that
10593 we catch only errors and don't run the debugger. */
10594 internal_condition_case_1 (redisplay_window_0, window,
10595 list_of_error,
10596 redisplay_window_error);
10597 }
10598
10599 window = w->next;
10600 }
10601 }
10602
10603 static Lisp_Object
10604 redisplay_window_error ()
10605 {
10606 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10607 return Qnil;
10608 }
10609
10610 static Lisp_Object
10611 redisplay_window_0 (window)
10612 Lisp_Object window;
10613 {
10614 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10615 redisplay_window (window, 0);
10616 return Qnil;
10617 }
10618
10619 static Lisp_Object
10620 redisplay_window_1 (window)
10621 Lisp_Object window;
10622 {
10623 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10624 redisplay_window (window, 1);
10625 return Qnil;
10626 }
10627 \f
10628
10629 /* Increment GLYPH until it reaches END or CONDITION fails while
10630 adding (GLYPH)->pixel_width to X. */
10631
10632 #define SKIP_GLYPHS(glyph, end, x, condition) \
10633 do \
10634 { \
10635 (x) += (glyph)->pixel_width; \
10636 ++(glyph); \
10637 } \
10638 while ((glyph) < (end) && (condition))
10639
10640
10641 /* Set cursor position of W. PT is assumed to be displayed in ROW.
10642 DELTA is the number of bytes by which positions recorded in ROW
10643 differ from current buffer positions. */
10644
10645 void
10646 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10647 struct window *w;
10648 struct glyph_row *row;
10649 struct glyph_matrix *matrix;
10650 int delta, delta_bytes, dy, dvpos;
10651 {
10652 struct glyph *glyph = row->glyphs[TEXT_AREA];
10653 struct glyph *end = glyph + row->used[TEXT_AREA];
10654 struct glyph *cursor = NULL;
10655 /* The first glyph that starts a sequence of glyphs from string. */
10656 struct glyph *string_start;
10657 /* The X coordinate of string_start. */
10658 int string_start_x;
10659 /* The last known character position. */
10660 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10661 /* The last known character position before string_start. */
10662 int string_before_pos;
10663 int x = row->x;
10664 int cursor_x = x;
10665 int cursor_from_overlay_pos = 0;
10666 int pt_old = PT - delta;
10667
10668 /* Skip over glyphs not having an object at the start of the row.
10669 These are special glyphs like truncation marks on terminal
10670 frames. */
10671 if (row->displays_text_p)
10672 while (glyph < end
10673 && INTEGERP (glyph->object)
10674 && glyph->charpos < 0)
10675 {
10676 x += glyph->pixel_width;
10677 ++glyph;
10678 }
10679
10680 string_start = NULL;
10681 while (glyph < end
10682 && !INTEGERP (glyph->object)
10683 && (!BUFFERP (glyph->object)
10684 || (last_pos = glyph->charpos) < pt_old))
10685 {
10686 if (! STRINGP (glyph->object))
10687 {
10688 string_start = NULL;
10689 x += glyph->pixel_width;
10690 ++glyph;
10691 if (cursor_from_overlay_pos
10692 && last_pos > cursor_from_overlay_pos)
10693 {
10694 cursor_from_overlay_pos = 0;
10695 cursor = 0;
10696 }
10697 }
10698 else
10699 {
10700 string_before_pos = last_pos;
10701 string_start = glyph;
10702 string_start_x = x;
10703 /* Skip all glyphs from string. */
10704 do
10705 {
10706 int pos;
10707 if ((cursor == NULL || glyph > cursor)
10708 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
10709 Qcursor, (glyph)->object))
10710 && (pos = string_buffer_position (w, glyph->object,
10711 string_before_pos),
10712 (pos == 0 /* From overlay */
10713 || pos == pt_old)))
10714 {
10715 /* Estimate overlay buffer position from the buffer
10716 positions of the glyphs before and after the overlay.
10717 Add 1 to last_pos so that if point corresponds to the
10718 glyph right after the overlay, we still use a 'cursor'
10719 property found in that overlay. */
10720 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
10721 cursor = glyph;
10722 cursor_x = x;
10723 }
10724 x += glyph->pixel_width;
10725 ++glyph;
10726 }
10727 while (glyph < end && STRINGP (glyph->object));
10728 }
10729 }
10730
10731 if (cursor != NULL)
10732 {
10733 glyph = cursor;
10734 x = cursor_x;
10735 }
10736 else if (string_start
10737 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
10738 {
10739 /* We may have skipped over point because the previous glyphs
10740 are from string. As there's no easy way to know the
10741 character position of the current glyph, find the correct
10742 glyph on point by scanning from string_start again. */
10743 Lisp_Object limit;
10744 Lisp_Object string;
10745 int pos;
10746
10747 limit = make_number (pt_old + 1);
10748 end = glyph;
10749 glyph = string_start;
10750 x = string_start_x;
10751 string = glyph->object;
10752 pos = string_buffer_position (w, string, string_before_pos);
10753 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
10754 because we always put cursor after overlay strings. */
10755 while (pos == 0 && glyph < end)
10756 {
10757 string = glyph->object;
10758 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10759 if (glyph < end)
10760 pos = string_buffer_position (w, glyph->object, string_before_pos);
10761 }
10762
10763 while (glyph < end)
10764 {
10765 pos = XINT (Fnext_single_char_property_change
10766 (make_number (pos), Qdisplay, Qnil, limit));
10767 if (pos > pt_old)
10768 break;
10769 /* Skip glyphs from the same string. */
10770 string = glyph->object;
10771 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10772 /* Skip glyphs from an overlay. */
10773 while (glyph < end
10774 && ! string_buffer_position (w, glyph->object, pos))
10775 {
10776 string = glyph->object;
10777 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10778 }
10779 }
10780 }
10781
10782 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
10783 w->cursor.x = x;
10784 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
10785 w->cursor.y = row->y + dy;
10786
10787 if (w == XWINDOW (selected_window))
10788 {
10789 if (!row->continued_p
10790 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
10791 && row->x == 0)
10792 {
10793 this_line_buffer = XBUFFER (w->buffer);
10794
10795 CHARPOS (this_line_start_pos)
10796 = MATRIX_ROW_START_CHARPOS (row) + delta;
10797 BYTEPOS (this_line_start_pos)
10798 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
10799
10800 CHARPOS (this_line_end_pos)
10801 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
10802 BYTEPOS (this_line_end_pos)
10803 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
10804
10805 this_line_y = w->cursor.y;
10806 this_line_pixel_height = row->height;
10807 this_line_vpos = w->cursor.vpos;
10808 this_line_start_x = row->x;
10809 }
10810 else
10811 CHARPOS (this_line_start_pos) = 0;
10812 }
10813 }
10814
10815
10816 /* Run window scroll functions, if any, for WINDOW with new window
10817 start STARTP. Sets the window start of WINDOW to that position.
10818
10819 We assume that the window's buffer is really current. */
10820
10821 static INLINE struct text_pos
10822 run_window_scroll_functions (window, startp)
10823 Lisp_Object window;
10824 struct text_pos startp;
10825 {
10826 struct window *w = XWINDOW (window);
10827 SET_MARKER_FROM_TEXT_POS (w->start, startp);
10828
10829 if (current_buffer != XBUFFER (w->buffer))
10830 abort ();
10831
10832 if (!NILP (Vwindow_scroll_functions))
10833 {
10834 run_hook_with_args_2 (Qwindow_scroll_functions, window,
10835 make_number (CHARPOS (startp)));
10836 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10837 /* In case the hook functions switch buffers. */
10838 if (current_buffer != XBUFFER (w->buffer))
10839 set_buffer_internal_1 (XBUFFER (w->buffer));
10840 }
10841
10842 return startp;
10843 }
10844
10845
10846 /* Make sure the line containing the cursor is fully visible.
10847 A value of 1 means there is nothing to be done.
10848 (Either the line is fully visible, or it cannot be made so,
10849 or we cannot tell.)
10850
10851 If FORCE_P is non-zero, return 0 even if partial visible cursor row
10852 is higher than window.
10853
10854 A value of 0 means the caller should do scrolling
10855 as if point had gone off the screen. */
10856
10857 static int
10858 make_cursor_line_fully_visible (w, force_p)
10859 struct window *w;
10860 int force_p;
10861 {
10862 struct glyph_matrix *matrix;
10863 struct glyph_row *row;
10864 int window_height;
10865
10866 /* It's not always possible to find the cursor, e.g, when a window
10867 is full of overlay strings. Don't do anything in that case. */
10868 if (w->cursor.vpos < 0)
10869 return 1;
10870
10871 matrix = w->desired_matrix;
10872 row = MATRIX_ROW (matrix, w->cursor.vpos);
10873
10874 /* If the cursor row is not partially visible, there's nothing to do. */
10875 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
10876 return 1;
10877
10878 /* If the row the cursor is in is taller than the window's height,
10879 it's not clear what to do, so do nothing. */
10880 window_height = window_box_height (w);
10881 if (row->height >= window_height)
10882 {
10883 if (!force_p || w->vscroll)
10884 return 1;
10885 }
10886 return 0;
10887
10888 #if 0
10889 /* This code used to try to scroll the window just enough to make
10890 the line visible. It returned 0 to say that the caller should
10891 allocate larger glyph matrices. */
10892
10893 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
10894 {
10895 int dy = row->height - row->visible_height;
10896 w->vscroll = 0;
10897 w->cursor.y += dy;
10898 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10899 }
10900 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
10901 {
10902 int dy = - (row->height - row->visible_height);
10903 w->vscroll = dy;
10904 w->cursor.y += dy;
10905 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10906 }
10907
10908 /* When we change the cursor y-position of the selected window,
10909 change this_line_y as well so that the display optimization for
10910 the cursor line of the selected window in redisplay_internal uses
10911 the correct y-position. */
10912 if (w == XWINDOW (selected_window))
10913 this_line_y = w->cursor.y;
10914
10915 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
10916 redisplay with larger matrices. */
10917 if (matrix->nrows < required_matrix_height (w))
10918 {
10919 fonts_changed_p = 1;
10920 return 0;
10921 }
10922
10923 return 1;
10924 #endif /* 0 */
10925 }
10926
10927
10928 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
10929 non-zero means only WINDOW is redisplayed in redisplay_internal.
10930 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
10931 in redisplay_window to bring a partially visible line into view in
10932 the case that only the cursor has moved.
10933
10934 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
10935 last screen line's vertical height extends past the end of the screen.
10936
10937 Value is
10938
10939 1 if scrolling succeeded
10940
10941 0 if scrolling didn't find point.
10942
10943 -1 if new fonts have been loaded so that we must interrupt
10944 redisplay, adjust glyph matrices, and try again. */
10945
10946 enum
10947 {
10948 SCROLLING_SUCCESS,
10949 SCROLLING_FAILED,
10950 SCROLLING_NEED_LARGER_MATRICES
10951 };
10952
10953 static int
10954 try_scrolling (window, just_this_one_p, scroll_conservatively,
10955 scroll_step, temp_scroll_step, last_line_misfit)
10956 Lisp_Object window;
10957 int just_this_one_p;
10958 EMACS_INT scroll_conservatively, scroll_step;
10959 int temp_scroll_step;
10960 int last_line_misfit;
10961 {
10962 struct window *w = XWINDOW (window);
10963 struct frame *f = XFRAME (w->frame);
10964 struct text_pos scroll_margin_pos;
10965 struct text_pos pos;
10966 struct text_pos startp;
10967 struct it it;
10968 Lisp_Object window_end;
10969 int this_scroll_margin;
10970 int dy = 0;
10971 int scroll_max;
10972 int rc;
10973 int amount_to_scroll = 0;
10974 Lisp_Object aggressive;
10975 int height;
10976 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
10977
10978 #if GLYPH_DEBUG
10979 debug_method_add (w, "try_scrolling");
10980 #endif
10981
10982 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10983
10984 /* Compute scroll margin height in pixels. We scroll when point is
10985 within this distance from the top or bottom of the window. */
10986 if (scroll_margin > 0)
10987 {
10988 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
10989 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
10990 }
10991 else
10992 this_scroll_margin = 0;
10993
10994 /* Force scroll_conservatively to have a reasonable value so it doesn't
10995 cause an overflow while computing how much to scroll. */
10996 if (scroll_conservatively)
10997 scroll_conservatively = min (scroll_conservatively,
10998 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
10999
11000 /* Compute how much we should try to scroll maximally to bring point
11001 into view. */
11002 if (scroll_step || scroll_conservatively || temp_scroll_step)
11003 scroll_max = max (scroll_step,
11004 max (scroll_conservatively, temp_scroll_step));
11005 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11006 || NUMBERP (current_buffer->scroll_up_aggressively))
11007 /* We're trying to scroll because of aggressive scrolling
11008 but no scroll_step is set. Choose an arbitrary one. Maybe
11009 there should be a variable for this. */
11010 scroll_max = 10;
11011 else
11012 scroll_max = 0;
11013 scroll_max *= FRAME_LINE_HEIGHT (f);
11014
11015 /* Decide whether we have to scroll down. Start at the window end
11016 and move this_scroll_margin up to find the position of the scroll
11017 margin. */
11018 window_end = Fwindow_end (window, Qt);
11019
11020 too_near_end:
11021
11022 CHARPOS (scroll_margin_pos) = XINT (window_end);
11023 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11024
11025 if (this_scroll_margin || extra_scroll_margin_lines)
11026 {
11027 start_display (&it, w, scroll_margin_pos);
11028 if (this_scroll_margin)
11029 move_it_vertically (&it, - this_scroll_margin);
11030 if (extra_scroll_margin_lines)
11031 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11032 scroll_margin_pos = it.current.pos;
11033 }
11034
11035 if (PT >= CHARPOS (scroll_margin_pos))
11036 {
11037 int y0;
11038
11039 /* Point is in the scroll margin at the bottom of the window, or
11040 below. Compute a new window start that makes point visible. */
11041
11042 /* Compute the distance from the scroll margin to PT.
11043 Give up if the distance is greater than scroll_max. */
11044 start_display (&it, w, scroll_margin_pos);
11045 y0 = it.current_y;
11046 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11047 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11048
11049 /* To make point visible, we have to move the window start
11050 down so that the line the cursor is in is visible, which
11051 means we have to add in the height of the cursor line. */
11052 dy = line_bottom_y (&it) - y0;
11053
11054 if (dy > scroll_max)
11055 return SCROLLING_FAILED;
11056
11057 /* Move the window start down. If scrolling conservatively,
11058 move it just enough down to make point visible. If
11059 scroll_step is set, move it down by scroll_step. */
11060 start_display (&it, w, startp);
11061
11062 if (scroll_conservatively)
11063 /* Set AMOUNT_TO_SCROLL to at least one line,
11064 and at most scroll_conservatively lines. */
11065 amount_to_scroll
11066 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11067 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11068 else if (scroll_step || temp_scroll_step)
11069 amount_to_scroll = scroll_max;
11070 else
11071 {
11072 aggressive = current_buffer->scroll_up_aggressively;
11073 height = WINDOW_BOX_TEXT_HEIGHT (w);
11074 if (NUMBERP (aggressive))
11075 {
11076 double float_amount = XFLOATINT (aggressive) * height;
11077 amount_to_scroll = float_amount;
11078 if (amount_to_scroll == 0 && float_amount > 0)
11079 amount_to_scroll = 1;
11080 }
11081 }
11082
11083 if (amount_to_scroll <= 0)
11084 return SCROLLING_FAILED;
11085
11086 /* If moving by amount_to_scroll leaves STARTP unchanged,
11087 move it down one screen line. */
11088
11089 move_it_vertically (&it, amount_to_scroll);
11090 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11091 move_it_by_lines (&it, 1, 1);
11092 startp = it.current.pos;
11093 }
11094 else
11095 {
11096 /* See if point is inside the scroll margin at the top of the
11097 window. */
11098 scroll_margin_pos = startp;
11099 if (this_scroll_margin)
11100 {
11101 start_display (&it, w, startp);
11102 move_it_vertically (&it, this_scroll_margin);
11103 scroll_margin_pos = it.current.pos;
11104 }
11105
11106 if (PT < CHARPOS (scroll_margin_pos))
11107 {
11108 /* Point is in the scroll margin at the top of the window or
11109 above what is displayed in the window. */
11110 int y0;
11111
11112 /* Compute the vertical distance from PT to the scroll
11113 margin position. Give up if distance is greater than
11114 scroll_max. */
11115 SET_TEXT_POS (pos, PT, PT_BYTE);
11116 start_display (&it, w, pos);
11117 y0 = it.current_y;
11118 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11119 it.last_visible_y, -1,
11120 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11121 dy = it.current_y - y0;
11122 if (dy > scroll_max)
11123 return SCROLLING_FAILED;
11124
11125 /* Compute new window start. */
11126 start_display (&it, w, startp);
11127
11128 if (scroll_conservatively)
11129 amount_to_scroll
11130 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11131 else if (scroll_step || temp_scroll_step)
11132 amount_to_scroll = scroll_max;
11133 else
11134 {
11135 aggressive = current_buffer->scroll_down_aggressively;
11136 height = WINDOW_BOX_TEXT_HEIGHT (w);
11137 if (NUMBERP (aggressive))
11138 {
11139 double float_amount = XFLOATINT (aggressive) * height;
11140 amount_to_scroll = float_amount;
11141 if (amount_to_scroll == 0 && float_amount > 0)
11142 amount_to_scroll = 1;
11143 }
11144 }
11145
11146 if (amount_to_scroll <= 0)
11147 return SCROLLING_FAILED;
11148
11149 move_it_vertically (&it, - amount_to_scroll);
11150 startp = it.current.pos;
11151 }
11152 }
11153
11154 /* Run window scroll functions. */
11155 startp = run_window_scroll_functions (window, startp);
11156
11157 /* Display the window. Give up if new fonts are loaded, or if point
11158 doesn't appear. */
11159 if (!try_window (window, startp))
11160 rc = SCROLLING_NEED_LARGER_MATRICES;
11161 else if (w->cursor.vpos < 0)
11162 {
11163 clear_glyph_matrix (w->desired_matrix);
11164 rc = SCROLLING_FAILED;
11165 }
11166 else
11167 {
11168 /* Maybe forget recorded base line for line number display. */
11169 if (!just_this_one_p
11170 || current_buffer->clip_changed
11171 || BEG_UNCHANGED < CHARPOS (startp))
11172 w->base_line_number = Qnil;
11173
11174 /* If cursor ends up on a partially visible line,
11175 treat that as being off the bottom of the screen. */
11176 if (! make_cursor_line_fully_visible (w, extra_scroll_margin_lines <= 1))
11177 {
11178 clear_glyph_matrix (w->desired_matrix);
11179 ++extra_scroll_margin_lines;
11180 goto too_near_end;
11181 }
11182 rc = SCROLLING_SUCCESS;
11183 }
11184
11185 return rc;
11186 }
11187
11188
11189 /* Compute a suitable window start for window W if display of W starts
11190 on a continuation line. Value is non-zero if a new window start
11191 was computed.
11192
11193 The new window start will be computed, based on W's width, starting
11194 from the start of the continued line. It is the start of the
11195 screen line with the minimum distance from the old start W->start. */
11196
11197 static int
11198 compute_window_start_on_continuation_line (w)
11199 struct window *w;
11200 {
11201 struct text_pos pos, start_pos;
11202 int window_start_changed_p = 0;
11203
11204 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11205
11206 /* If window start is on a continuation line... Window start may be
11207 < BEGV in case there's invisible text at the start of the
11208 buffer (M-x rmail, for example). */
11209 if (CHARPOS (start_pos) > BEGV
11210 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11211 {
11212 struct it it;
11213 struct glyph_row *row;
11214
11215 /* Handle the case that the window start is out of range. */
11216 if (CHARPOS (start_pos) < BEGV)
11217 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11218 else if (CHARPOS (start_pos) > ZV)
11219 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11220
11221 /* Find the start of the continued line. This should be fast
11222 because scan_buffer is fast (newline cache). */
11223 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11224 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11225 row, DEFAULT_FACE_ID);
11226 reseat_at_previous_visible_line_start (&it);
11227
11228 /* If the line start is "too far" away from the window start,
11229 say it takes too much time to compute a new window start. */
11230 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11231 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11232 {
11233 int min_distance, distance;
11234
11235 /* Move forward by display lines to find the new window
11236 start. If window width was enlarged, the new start can
11237 be expected to be > the old start. If window width was
11238 decreased, the new window start will be < the old start.
11239 So, we're looking for the display line start with the
11240 minimum distance from the old window start. */
11241 pos = it.current.pos;
11242 min_distance = INFINITY;
11243 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11244 distance < min_distance)
11245 {
11246 min_distance = distance;
11247 pos = it.current.pos;
11248 move_it_by_lines (&it, 1, 0);
11249 }
11250
11251 /* Set the window start there. */
11252 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11253 window_start_changed_p = 1;
11254 }
11255 }
11256
11257 return window_start_changed_p;
11258 }
11259
11260
11261 /* Try cursor movement in case text has not changed in window WINDOW,
11262 with window start STARTP. Value is
11263
11264 CURSOR_MOVEMENT_SUCCESS if successful
11265
11266 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11267
11268 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11269 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11270 we want to scroll as if scroll-step were set to 1. See the code.
11271
11272 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11273 which case we have to abort this redisplay, and adjust matrices
11274 first. */
11275
11276 enum
11277 {
11278 CURSOR_MOVEMENT_SUCCESS,
11279 CURSOR_MOVEMENT_CANNOT_BE_USED,
11280 CURSOR_MOVEMENT_MUST_SCROLL,
11281 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11282 };
11283
11284 static int
11285 try_cursor_movement (window, startp, scroll_step)
11286 Lisp_Object window;
11287 struct text_pos startp;
11288 int *scroll_step;
11289 {
11290 struct window *w = XWINDOW (window);
11291 struct frame *f = XFRAME (w->frame);
11292 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11293
11294 #if GLYPH_DEBUG
11295 if (inhibit_try_cursor_movement)
11296 return rc;
11297 #endif
11298
11299 /* Handle case where text has not changed, only point, and it has
11300 not moved off the frame. */
11301 if (/* Point may be in this window. */
11302 PT >= CHARPOS (startp)
11303 /* Selective display hasn't changed. */
11304 && !current_buffer->clip_changed
11305 /* Function force-mode-line-update is used to force a thorough
11306 redisplay. It sets either windows_or_buffers_changed or
11307 update_mode_lines. So don't take a shortcut here for these
11308 cases. */
11309 && !update_mode_lines
11310 && !windows_or_buffers_changed
11311 && !cursor_type_changed
11312 /* Can't use this case if highlighting a region. When a
11313 region exists, cursor movement has to do more than just
11314 set the cursor. */
11315 && !(!NILP (Vtransient_mark_mode)
11316 && !NILP (current_buffer->mark_active))
11317 && NILP (w->region_showing)
11318 && NILP (Vshow_trailing_whitespace)
11319 /* Right after splitting windows, last_point may be nil. */
11320 && INTEGERP (w->last_point)
11321 /* This code is not used for mini-buffer for the sake of the case
11322 of redisplaying to replace an echo area message; since in
11323 that case the mini-buffer contents per se are usually
11324 unchanged. This code is of no real use in the mini-buffer
11325 since the handling of this_line_start_pos, etc., in redisplay
11326 handles the same cases. */
11327 && !EQ (window, minibuf_window)
11328 /* When splitting windows or for new windows, it happens that
11329 redisplay is called with a nil window_end_vpos or one being
11330 larger than the window. This should really be fixed in
11331 window.c. I don't have this on my list, now, so we do
11332 approximately the same as the old redisplay code. --gerd. */
11333 && INTEGERP (w->window_end_vpos)
11334 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11335 && (FRAME_WINDOW_P (f)
11336 || !overlay_arrow_in_current_buffer_p ()))
11337 {
11338 int this_scroll_margin, top_scroll_margin;
11339 struct glyph_row *row = NULL;
11340
11341 #if GLYPH_DEBUG
11342 debug_method_add (w, "cursor movement");
11343 #endif
11344
11345 /* Scroll if point within this distance from the top or bottom
11346 of the window. This is a pixel value. */
11347 this_scroll_margin = max (0, scroll_margin);
11348 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11349 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11350
11351 top_scroll_margin = this_scroll_margin;
11352 if (WINDOW_WANTS_HEADER_LINE_P (w))
11353 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
11354
11355 /* Start with the row the cursor was displayed during the last
11356 not paused redisplay. Give up if that row is not valid. */
11357 if (w->last_cursor.vpos < 0
11358 || w->last_cursor.vpos >= w->current_matrix->nrows)
11359 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11360 else
11361 {
11362 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11363 if (row->mode_line_p)
11364 ++row;
11365 if (!row->enabled_p)
11366 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11367 }
11368
11369 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11370 {
11371 int scroll_p = 0;
11372 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11373
11374 if (PT > XFASTINT (w->last_point))
11375 {
11376 /* Point has moved forward. */
11377 while (MATRIX_ROW_END_CHARPOS (row) < PT
11378 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11379 {
11380 xassert (row->enabled_p);
11381 ++row;
11382 }
11383
11384 /* The end position of a row equals the start position
11385 of the next row. If PT is there, we would rather
11386 display it in the next line. */
11387 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11388 && MATRIX_ROW_END_CHARPOS (row) == PT
11389 && !cursor_row_p (w, row))
11390 ++row;
11391
11392 /* If within the scroll margin, scroll. Note that
11393 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11394 the next line would be drawn, and that
11395 this_scroll_margin can be zero. */
11396 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11397 || PT > MATRIX_ROW_END_CHARPOS (row)
11398 /* Line is completely visible last line in window
11399 and PT is to be set in the next line. */
11400 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11401 && PT == MATRIX_ROW_END_CHARPOS (row)
11402 && !row->ends_at_zv_p
11403 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11404 scroll_p = 1;
11405 }
11406 else if (PT < XFASTINT (w->last_point))
11407 {
11408 /* Cursor has to be moved backward. Note that PT >=
11409 CHARPOS (startp) because of the outer if-statement. */
11410 while (!row->mode_line_p
11411 && (MATRIX_ROW_START_CHARPOS (row) > PT
11412 || (MATRIX_ROW_START_CHARPOS (row) == PT
11413 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11414 && (row->y > top_scroll_margin
11415 || CHARPOS (startp) == BEGV))
11416 {
11417 xassert (row->enabled_p);
11418 --row;
11419 }
11420
11421 /* Consider the following case: Window starts at BEGV,
11422 there is invisible, intangible text at BEGV, so that
11423 display starts at some point START > BEGV. It can
11424 happen that we are called with PT somewhere between
11425 BEGV and START. Try to handle that case. */
11426 if (row < w->current_matrix->rows
11427 || row->mode_line_p)
11428 {
11429 row = w->current_matrix->rows;
11430 if (row->mode_line_p)
11431 ++row;
11432 }
11433
11434 /* Due to newlines in overlay strings, we may have to
11435 skip forward over overlay strings. */
11436 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11437 && MATRIX_ROW_END_CHARPOS (row) == PT
11438 && !cursor_row_p (w, row))
11439 ++row;
11440
11441 /* If within the scroll margin, scroll. */
11442 if (row->y < top_scroll_margin
11443 && CHARPOS (startp) != BEGV)
11444 scroll_p = 1;
11445 }
11446
11447 if (PT < MATRIX_ROW_START_CHARPOS (row)
11448 || PT > MATRIX_ROW_END_CHARPOS (row))
11449 {
11450 /* if PT is not in the glyph row, give up. */
11451 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11452 }
11453 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
11454 {
11455 if (PT == MATRIX_ROW_END_CHARPOS (row)
11456 && !row->ends_at_zv_p
11457 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11458 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11459 else if (row->height > window_box_height (w))
11460 {
11461 /* If we end up in a partially visible line, let's
11462 make it fully visible, except when it's taller
11463 than the window, in which case we can't do much
11464 about it. */
11465 *scroll_step = 1;
11466 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11467 }
11468 else
11469 {
11470 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11471 if (!make_cursor_line_fully_visible (w, 0))
11472 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11473 else
11474 rc = CURSOR_MOVEMENT_SUCCESS;
11475 }
11476 }
11477 else if (scroll_p)
11478 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11479 else
11480 {
11481 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11482 rc = CURSOR_MOVEMENT_SUCCESS;
11483 }
11484 }
11485 }
11486
11487 return rc;
11488 }
11489
11490 void
11491 set_vertical_scroll_bar (w)
11492 struct window *w;
11493 {
11494 int start, end, whole;
11495
11496 /* Calculate the start and end positions for the current window.
11497 At some point, it would be nice to choose between scrollbars
11498 which reflect the whole buffer size, with special markers
11499 indicating narrowing, and scrollbars which reflect only the
11500 visible region.
11501
11502 Note that mini-buffers sometimes aren't displaying any text. */
11503 if (!MINI_WINDOW_P (w)
11504 || (w == XWINDOW (minibuf_window)
11505 && NILP (echo_area_buffer[0])))
11506 {
11507 struct buffer *buf = XBUFFER (w->buffer);
11508 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11509 start = marker_position (w->start) - BUF_BEGV (buf);
11510 /* I don't think this is guaranteed to be right. For the
11511 moment, we'll pretend it is. */
11512 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11513
11514 if (end < start)
11515 end = start;
11516 if (whole < (end - start))
11517 whole = end - start;
11518 }
11519 else
11520 start = end = whole = 0;
11521
11522 /* Indicate what this scroll bar ought to be displaying now. */
11523 set_vertical_scroll_bar_hook (w, end - start, whole, start);
11524 }
11525
11526
11527 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11528 selected_window is redisplayed.
11529
11530 We can return without actually redisplaying the window if
11531 fonts_changed_p is nonzero. In that case, redisplay_internal will
11532 retry. */
11533
11534 static void
11535 redisplay_window (window, just_this_one_p)
11536 Lisp_Object window;
11537 int just_this_one_p;
11538 {
11539 struct window *w = XWINDOW (window);
11540 struct frame *f = XFRAME (w->frame);
11541 struct buffer *buffer = XBUFFER (w->buffer);
11542 struct buffer *old = current_buffer;
11543 struct text_pos lpoint, opoint, startp;
11544 int update_mode_line;
11545 int tem;
11546 struct it it;
11547 /* Record it now because it's overwritten. */
11548 int current_matrix_up_to_date_p = 0;
11549 int used_current_matrix_p = 0;
11550 /* This is less strict than current_matrix_up_to_date_p.
11551 It indictes that the buffer contents and narrowing are unchanged. */
11552 int buffer_unchanged_p = 0;
11553 int temp_scroll_step = 0;
11554 int count = SPECPDL_INDEX ();
11555 int rc;
11556 int centering_position;
11557 int last_line_misfit = 0;
11558
11559 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11560 opoint = lpoint;
11561
11562 /* W must be a leaf window here. */
11563 xassert (!NILP (w->buffer));
11564 #if GLYPH_DEBUG
11565 *w->desired_matrix->method = 0;
11566 #endif
11567
11568 specbind (Qinhibit_point_motion_hooks, Qt);
11569
11570 reconsider_clip_changes (w, buffer);
11571
11572 /* Has the mode line to be updated? */
11573 update_mode_line = (!NILP (w->update_mode_line)
11574 || update_mode_lines
11575 || buffer->clip_changed
11576 || buffer->prevent_redisplay_optimizations_p);
11577
11578 if (MINI_WINDOW_P (w))
11579 {
11580 if (w == XWINDOW (echo_area_window)
11581 && !NILP (echo_area_buffer[0]))
11582 {
11583 if (update_mode_line)
11584 /* We may have to update a tty frame's menu bar or a
11585 tool-bar. Example `M-x C-h C-h C-g'. */
11586 goto finish_menu_bars;
11587 else
11588 /* We've already displayed the echo area glyphs in this window. */
11589 goto finish_scroll_bars;
11590 }
11591 else if ((w != XWINDOW (minibuf_window)
11592 || minibuf_level == 0)
11593 /* When buffer is nonempty, redisplay window normally. */
11594 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
11595 /* Quail displays non-mini buffers in minibuffer window.
11596 In that case, redisplay the window normally. */
11597 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
11598 {
11599 /* W is a mini-buffer window, but it's not active, so clear
11600 it. */
11601 int yb = window_text_bottom_y (w);
11602 struct glyph_row *row;
11603 int y;
11604
11605 for (y = 0, row = w->desired_matrix->rows;
11606 y < yb;
11607 y += row->height, ++row)
11608 blank_row (w, row, y);
11609 goto finish_scroll_bars;
11610 }
11611
11612 clear_glyph_matrix (w->desired_matrix);
11613 }
11614
11615 /* Otherwise set up data on this window; select its buffer and point
11616 value. */
11617 /* Really select the buffer, for the sake of buffer-local
11618 variables. */
11619 set_buffer_internal_1 (XBUFFER (w->buffer));
11620 SET_TEXT_POS (opoint, PT, PT_BYTE);
11621
11622 current_matrix_up_to_date_p
11623 = (!NILP (w->window_end_valid)
11624 && !current_buffer->clip_changed
11625 && !current_buffer->prevent_redisplay_optimizations_p
11626 && XFASTINT (w->last_modified) >= MODIFF
11627 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11628
11629 buffer_unchanged_p
11630 = (!NILP (w->window_end_valid)
11631 && !current_buffer->clip_changed
11632 && XFASTINT (w->last_modified) >= MODIFF
11633 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11634
11635 /* When windows_or_buffers_changed is non-zero, we can't rely on
11636 the window end being valid, so set it to nil there. */
11637 if (windows_or_buffers_changed)
11638 {
11639 /* If window starts on a continuation line, maybe adjust the
11640 window start in case the window's width changed. */
11641 if (XMARKER (w->start)->buffer == current_buffer)
11642 compute_window_start_on_continuation_line (w);
11643
11644 w->window_end_valid = Qnil;
11645 }
11646
11647 /* Some sanity checks. */
11648 CHECK_WINDOW_END (w);
11649 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
11650 abort ();
11651 if (BYTEPOS (opoint) < CHARPOS (opoint))
11652 abort ();
11653
11654 /* If %c is in mode line, update it if needed. */
11655 if (!NILP (w->column_number_displayed)
11656 /* This alternative quickly identifies a common case
11657 where no change is needed. */
11658 && !(PT == XFASTINT (w->last_point)
11659 && XFASTINT (w->last_modified) >= MODIFF
11660 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11661 && (XFASTINT (w->column_number_displayed)
11662 != (int) current_column ())) /* iftc */
11663 update_mode_line = 1;
11664
11665 /* Count number of windows showing the selected buffer. An indirect
11666 buffer counts as its base buffer. */
11667 if (!just_this_one_p)
11668 {
11669 struct buffer *current_base, *window_base;
11670 current_base = current_buffer;
11671 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11672 if (current_base->base_buffer)
11673 current_base = current_base->base_buffer;
11674 if (window_base->base_buffer)
11675 window_base = window_base->base_buffer;
11676 if (current_base == window_base)
11677 buffer_shared++;
11678 }
11679
11680 /* Point refers normally to the selected window. For any other
11681 window, set up appropriate value. */
11682 if (!EQ (window, selected_window))
11683 {
11684 int new_pt = XMARKER (w->pointm)->charpos;
11685 int new_pt_byte = marker_byte_position (w->pointm);
11686 if (new_pt < BEGV)
11687 {
11688 new_pt = BEGV;
11689 new_pt_byte = BEGV_BYTE;
11690 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
11691 }
11692 else if (new_pt > (ZV - 1))
11693 {
11694 new_pt = ZV;
11695 new_pt_byte = ZV_BYTE;
11696 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
11697 }
11698
11699 /* We don't use SET_PT so that the point-motion hooks don't run. */
11700 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
11701 }
11702
11703 /* If any of the character widths specified in the display table
11704 have changed, invalidate the width run cache. It's true that
11705 this may be a bit late to catch such changes, but the rest of
11706 redisplay goes (non-fatally) haywire when the display table is
11707 changed, so why should we worry about doing any better? */
11708 if (current_buffer->width_run_cache)
11709 {
11710 struct Lisp_Char_Table *disptab = buffer_display_table ();
11711
11712 if (! disptab_matches_widthtab (disptab,
11713 XVECTOR (current_buffer->width_table)))
11714 {
11715 invalidate_region_cache (current_buffer,
11716 current_buffer->width_run_cache,
11717 BEG, Z);
11718 recompute_width_table (current_buffer, disptab);
11719 }
11720 }
11721
11722 /* If window-start is screwed up, choose a new one. */
11723 if (XMARKER (w->start)->buffer != current_buffer)
11724 goto recenter;
11725
11726 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11727
11728 /* If someone specified a new starting point but did not insist,
11729 check whether it can be used. */
11730 if (!NILP (w->optional_new_start)
11731 && CHARPOS (startp) >= BEGV
11732 && CHARPOS (startp) <= ZV)
11733 {
11734 w->optional_new_start = Qnil;
11735 start_display (&it, w, startp);
11736 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11737 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11738 if (IT_CHARPOS (it) == PT)
11739 w->force_start = Qt;
11740 /* IT may overshoot PT if text at PT is invisible. */
11741 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
11742 w->force_start = Qt;
11743
11744
11745 }
11746
11747 /* Handle case where place to start displaying has been specified,
11748 unless the specified location is outside the accessible range. */
11749 if (!NILP (w->force_start)
11750 || w->frozen_window_start_p)
11751 {
11752 /* We set this later on if we have to adjust point. */
11753 int new_vpos = -1;
11754
11755 w->force_start = Qnil;
11756 w->vscroll = 0;
11757 w->window_end_valid = Qnil;
11758
11759 /* Forget any recorded base line for line number display. */
11760 if (!buffer_unchanged_p)
11761 w->base_line_number = Qnil;
11762
11763 /* Redisplay the mode line. Select the buffer properly for that.
11764 Also, run the hook window-scroll-functions
11765 because we have scrolled. */
11766 /* Note, we do this after clearing force_start because
11767 if there's an error, it is better to forget about force_start
11768 than to get into an infinite loop calling the hook functions
11769 and having them get more errors. */
11770 if (!update_mode_line
11771 || ! NILP (Vwindow_scroll_functions))
11772 {
11773 update_mode_line = 1;
11774 w->update_mode_line = Qt;
11775 startp = run_window_scroll_functions (window, startp);
11776 }
11777
11778 w->last_modified = make_number (0);
11779 w->last_overlay_modified = make_number (0);
11780 if (CHARPOS (startp) < BEGV)
11781 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
11782 else if (CHARPOS (startp) > ZV)
11783 SET_TEXT_POS (startp, ZV, ZV_BYTE);
11784
11785 /* Redisplay, then check if cursor has been set during the
11786 redisplay. Give up if new fonts were loaded. */
11787 if (!try_window (window, startp))
11788 {
11789 w->force_start = Qt;
11790 clear_glyph_matrix (w->desired_matrix);
11791 goto need_larger_matrices;
11792 }
11793
11794 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
11795 {
11796 /* If point does not appear, try to move point so it does
11797 appear. The desired matrix has been built above, so we
11798 can use it here. */
11799 new_vpos = window_box_height (w) / 2;
11800 }
11801
11802 if (!make_cursor_line_fully_visible (w, 0))
11803 {
11804 /* Point does appear, but on a line partly visible at end of window.
11805 Move it back to a fully-visible line. */
11806 new_vpos = window_box_height (w);
11807 }
11808
11809 /* If we need to move point for either of the above reasons,
11810 now actually do it. */
11811 if (new_vpos >= 0)
11812 {
11813 struct glyph_row *row;
11814
11815 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
11816 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
11817 ++row;
11818
11819 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
11820 MATRIX_ROW_START_BYTEPOS (row));
11821
11822 if (w != XWINDOW (selected_window))
11823 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
11824 else if (current_buffer == old)
11825 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11826
11827 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
11828
11829 /* If we are highlighting the region, then we just changed
11830 the region, so redisplay to show it. */
11831 if (!NILP (Vtransient_mark_mode)
11832 && !NILP (current_buffer->mark_active))
11833 {
11834 clear_glyph_matrix (w->desired_matrix);
11835 if (!try_window (window, startp))
11836 goto need_larger_matrices;
11837 }
11838 }
11839
11840 #if GLYPH_DEBUG
11841 debug_method_add (w, "forced window start");
11842 #endif
11843 goto done;
11844 }
11845
11846 /* Handle case where text has not changed, only point, and it has
11847 not moved off the frame, and we are not retrying after hscroll.
11848 (current_matrix_up_to_date_p is nonzero when retrying.) */
11849 if (current_matrix_up_to_date_p
11850 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
11851 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
11852 {
11853 switch (rc)
11854 {
11855 case CURSOR_MOVEMENT_SUCCESS:
11856 used_current_matrix_p = 1;
11857 goto done;
11858
11859 #if 0 /* try_cursor_movement never returns this value. */
11860 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
11861 goto need_larger_matrices;
11862 #endif
11863
11864 case CURSOR_MOVEMENT_MUST_SCROLL:
11865 goto try_to_scroll;
11866
11867 default:
11868 abort ();
11869 }
11870 }
11871 /* If current starting point was originally the beginning of a line
11872 but no longer is, find a new starting point. */
11873 else if (!NILP (w->start_at_line_beg)
11874 && !(CHARPOS (startp) <= BEGV
11875 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
11876 {
11877 #if GLYPH_DEBUG
11878 debug_method_add (w, "recenter 1");
11879 #endif
11880 goto recenter;
11881 }
11882
11883 /* Try scrolling with try_window_id. Value is > 0 if update has
11884 been done, it is -1 if we know that the same window start will
11885 not work. It is 0 if unsuccessful for some other reason. */
11886 else if ((tem = try_window_id (w)) != 0)
11887 {
11888 #if GLYPH_DEBUG
11889 debug_method_add (w, "try_window_id %d", tem);
11890 #endif
11891
11892 if (fonts_changed_p)
11893 goto need_larger_matrices;
11894 if (tem > 0)
11895 goto done;
11896
11897 /* Otherwise try_window_id has returned -1 which means that we
11898 don't want the alternative below this comment to execute. */
11899 }
11900 else if (CHARPOS (startp) >= BEGV
11901 && CHARPOS (startp) <= ZV
11902 && PT >= CHARPOS (startp)
11903 && (CHARPOS (startp) < ZV
11904 /* Avoid starting at end of buffer. */
11905 || CHARPOS (startp) == BEGV
11906 || (XFASTINT (w->last_modified) >= MODIFF
11907 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
11908 {
11909 #if GLYPH_DEBUG
11910 debug_method_add (w, "same window start");
11911 #endif
11912
11913 /* Try to redisplay starting at same place as before.
11914 If point has not moved off frame, accept the results. */
11915 if (!current_matrix_up_to_date_p
11916 /* Don't use try_window_reusing_current_matrix in this case
11917 because a window scroll function can have changed the
11918 buffer. */
11919 || !NILP (Vwindow_scroll_functions)
11920 || MINI_WINDOW_P (w)
11921 || !(used_current_matrix_p
11922 = try_window_reusing_current_matrix (w)))
11923 {
11924 IF_DEBUG (debug_method_add (w, "1"));
11925 try_window (window, startp);
11926 }
11927
11928 if (fonts_changed_p)
11929 goto need_larger_matrices;
11930
11931 if (w->cursor.vpos >= 0)
11932 {
11933 if (!just_this_one_p
11934 || current_buffer->clip_changed
11935 || BEG_UNCHANGED < CHARPOS (startp))
11936 /* Forget any recorded base line for line number display. */
11937 w->base_line_number = Qnil;
11938
11939 if (!make_cursor_line_fully_visible (w, 1))
11940 {
11941 clear_glyph_matrix (w->desired_matrix);
11942 last_line_misfit = 1;
11943 }
11944 /* Drop through and scroll. */
11945 else
11946 goto done;
11947 }
11948 else
11949 clear_glyph_matrix (w->desired_matrix);
11950 }
11951
11952 try_to_scroll:
11953
11954 w->last_modified = make_number (0);
11955 w->last_overlay_modified = make_number (0);
11956
11957 /* Redisplay the mode line. Select the buffer properly for that. */
11958 if (!update_mode_line)
11959 {
11960 update_mode_line = 1;
11961 w->update_mode_line = Qt;
11962 }
11963
11964 /* Try to scroll by specified few lines. */
11965 if ((scroll_conservatively
11966 || scroll_step
11967 || temp_scroll_step
11968 || NUMBERP (current_buffer->scroll_up_aggressively)
11969 || NUMBERP (current_buffer->scroll_down_aggressively))
11970 && !current_buffer->clip_changed
11971 && CHARPOS (startp) >= BEGV
11972 && CHARPOS (startp) <= ZV)
11973 {
11974 /* The function returns -1 if new fonts were loaded, 1 if
11975 successful, 0 if not successful. */
11976 int rc = try_scrolling (window, just_this_one_p,
11977 scroll_conservatively,
11978 scroll_step,
11979 temp_scroll_step, last_line_misfit);
11980 switch (rc)
11981 {
11982 case SCROLLING_SUCCESS:
11983 goto done;
11984
11985 case SCROLLING_NEED_LARGER_MATRICES:
11986 goto need_larger_matrices;
11987
11988 case SCROLLING_FAILED:
11989 break;
11990
11991 default:
11992 abort ();
11993 }
11994 }
11995
11996 /* Finally, just choose place to start which centers point */
11997
11998 recenter:
11999 centering_position = window_box_height (w) / 2;
12000
12001 point_at_top:
12002 /* Jump here with centering_position already set to 0. */
12003
12004 #if GLYPH_DEBUG
12005 debug_method_add (w, "recenter");
12006 #endif
12007
12008 /* w->vscroll = 0; */
12009
12010 /* Forget any previously recorded base line for line number display. */
12011 if (!buffer_unchanged_p)
12012 w->base_line_number = Qnil;
12013
12014 /* Move backward half the height of the window. */
12015 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12016 it.current_y = it.last_visible_y;
12017 move_it_vertically_backward (&it, centering_position);
12018 xassert (IT_CHARPOS (it) >= BEGV);
12019
12020 /* The function move_it_vertically_backward may move over more
12021 than the specified y-distance. If it->w is small, e.g. a
12022 mini-buffer window, we may end up in front of the window's
12023 display area. Start displaying at the start of the line
12024 containing PT in this case. */
12025 if (it.current_y <= 0)
12026 {
12027 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12028 move_it_vertically (&it, 0);
12029 xassert (IT_CHARPOS (it) <= PT);
12030 it.current_y = 0;
12031 }
12032
12033 it.current_x = it.hpos = 0;
12034
12035 /* Set startp here explicitly in case that helps avoid an infinite loop
12036 in case the window-scroll-functions functions get errors. */
12037 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12038
12039 /* Run scroll hooks. */
12040 startp = run_window_scroll_functions (window, it.current.pos);
12041
12042 /* Redisplay the window. */
12043 if (!current_matrix_up_to_date_p
12044 || windows_or_buffers_changed
12045 || cursor_type_changed
12046 /* Don't use try_window_reusing_current_matrix in this case
12047 because it can have changed the buffer. */
12048 || !NILP (Vwindow_scroll_functions)
12049 || !just_this_one_p
12050 || MINI_WINDOW_P (w)
12051 || !(used_current_matrix_p
12052 = try_window_reusing_current_matrix (w)))
12053 try_window (window, startp);
12054
12055 /* If new fonts have been loaded (due to fontsets), give up. We
12056 have to start a new redisplay since we need to re-adjust glyph
12057 matrices. */
12058 if (fonts_changed_p)
12059 goto need_larger_matrices;
12060
12061 /* If cursor did not appear assume that the middle of the window is
12062 in the first line of the window. Do it again with the next line.
12063 (Imagine a window of height 100, displaying two lines of height
12064 60. Moving back 50 from it->last_visible_y will end in the first
12065 line.) */
12066 if (w->cursor.vpos < 0)
12067 {
12068 if (!NILP (w->window_end_valid)
12069 && PT >= Z - XFASTINT (w->window_end_pos))
12070 {
12071 clear_glyph_matrix (w->desired_matrix);
12072 move_it_by_lines (&it, 1, 0);
12073 try_window (window, it.current.pos);
12074 }
12075 else if (PT < IT_CHARPOS (it))
12076 {
12077 clear_glyph_matrix (w->desired_matrix);
12078 move_it_by_lines (&it, -1, 0);
12079 try_window (window, it.current.pos);
12080 }
12081 else
12082 {
12083 /* Not much we can do about it. */
12084 }
12085 }
12086
12087 /* Consider the following case: Window starts at BEGV, there is
12088 invisible, intangible text at BEGV, so that display starts at
12089 some point START > BEGV. It can happen that we are called with
12090 PT somewhere between BEGV and START. Try to handle that case. */
12091 if (w->cursor.vpos < 0)
12092 {
12093 struct glyph_row *row = w->current_matrix->rows;
12094 if (row->mode_line_p)
12095 ++row;
12096 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12097 }
12098
12099 if (!make_cursor_line_fully_visible (w, centering_position > 0))
12100 {
12101 /* If vscroll is enabled, disable it and try again. */
12102 if (w->vscroll)
12103 {
12104 w->vscroll = 0;
12105 clear_glyph_matrix (w->desired_matrix);
12106 goto recenter;
12107 }
12108
12109 /* If centering point failed to make the whole line visible,
12110 put point at the top instead. That has to make the whole line
12111 visible, if it can be done. */
12112 clear_glyph_matrix (w->desired_matrix);
12113 centering_position = 0;
12114 goto point_at_top;
12115 }
12116
12117 done:
12118
12119 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12120 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12121 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12122 ? Qt : Qnil);
12123
12124 /* Display the mode line, if we must. */
12125 if ((update_mode_line
12126 /* If window not full width, must redo its mode line
12127 if (a) the window to its side is being redone and
12128 (b) we do a frame-based redisplay. This is a consequence
12129 of how inverted lines are drawn in frame-based redisplay. */
12130 || (!just_this_one_p
12131 && !FRAME_WINDOW_P (f)
12132 && !WINDOW_FULL_WIDTH_P (w))
12133 /* Line number to display. */
12134 || INTEGERP (w->base_line_pos)
12135 /* Column number is displayed and different from the one displayed. */
12136 || (!NILP (w->column_number_displayed)
12137 && (XFASTINT (w->column_number_displayed)
12138 != (int) current_column ()))) /* iftc */
12139 /* This means that the window has a mode line. */
12140 && (WINDOW_WANTS_MODELINE_P (w)
12141 || WINDOW_WANTS_HEADER_LINE_P (w)))
12142 {
12143 display_mode_lines (w);
12144
12145 /* If mode line height has changed, arrange for a thorough
12146 immediate redisplay using the correct mode line height. */
12147 if (WINDOW_WANTS_MODELINE_P (w)
12148 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12149 {
12150 fonts_changed_p = 1;
12151 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12152 = DESIRED_MODE_LINE_HEIGHT (w);
12153 }
12154
12155 /* If top line height has changed, arrange for a thorough
12156 immediate redisplay using the correct mode line height. */
12157 if (WINDOW_WANTS_HEADER_LINE_P (w)
12158 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12159 {
12160 fonts_changed_p = 1;
12161 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12162 = DESIRED_HEADER_LINE_HEIGHT (w);
12163 }
12164
12165 if (fonts_changed_p)
12166 goto need_larger_matrices;
12167 }
12168
12169 if (!line_number_displayed
12170 && !BUFFERP (w->base_line_pos))
12171 {
12172 w->base_line_pos = Qnil;
12173 w->base_line_number = Qnil;
12174 }
12175
12176 finish_menu_bars:
12177
12178 /* When we reach a frame's selected window, redo the frame's menu bar. */
12179 if (update_mode_line
12180 && EQ (FRAME_SELECTED_WINDOW (f), window))
12181 {
12182 int redisplay_menu_p = 0;
12183 int redisplay_tool_bar_p = 0;
12184
12185 if (FRAME_WINDOW_P (f))
12186 {
12187 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12188 || defined (USE_GTK)
12189 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12190 #else
12191 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12192 #endif
12193 }
12194 else
12195 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12196
12197 if (redisplay_menu_p)
12198 display_menu_bar (w);
12199
12200 #ifdef HAVE_WINDOW_SYSTEM
12201 #ifdef USE_GTK
12202 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12203 #else
12204 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12205 && (FRAME_TOOL_BAR_LINES (f) > 0
12206 || auto_resize_tool_bars_p);
12207
12208 #endif
12209
12210 if (redisplay_tool_bar_p)
12211 redisplay_tool_bar (f);
12212 #endif
12213 }
12214
12215 #ifdef HAVE_WINDOW_SYSTEM
12216 if (update_window_fringes (w, 0)
12217 && !just_this_one_p
12218 && (used_current_matrix_p || overlay_arrow_seen)
12219 && !w->pseudo_window_p)
12220 {
12221 update_begin (f);
12222 BLOCK_INPUT;
12223 draw_window_fringes (w);
12224 UNBLOCK_INPUT;
12225 update_end (f);
12226 }
12227 #endif /* HAVE_WINDOW_SYSTEM */
12228
12229 /* We go to this label, with fonts_changed_p nonzero,
12230 if it is necessary to try again using larger glyph matrices.
12231 We have to redeem the scroll bar even in this case,
12232 because the loop in redisplay_internal expects that. */
12233 need_larger_matrices:
12234 ;
12235 finish_scroll_bars:
12236
12237 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12238 {
12239 /* Set the thumb's position and size. */
12240 set_vertical_scroll_bar (w);
12241
12242 /* Note that we actually used the scroll bar attached to this
12243 window, so it shouldn't be deleted at the end of redisplay. */
12244 redeem_scroll_bar_hook (w);
12245 }
12246
12247 /* Restore current_buffer and value of point in it. */
12248 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12249 set_buffer_internal_1 (old);
12250 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12251
12252 unbind_to (count, Qnil);
12253 }
12254
12255
12256 /* Build the complete desired matrix of WINDOW with a window start
12257 buffer position POS. Value is non-zero if successful. It is zero
12258 if fonts were loaded during redisplay which makes re-adjusting
12259 glyph matrices necessary. */
12260
12261 int
12262 try_window (window, pos)
12263 Lisp_Object window;
12264 struct text_pos pos;
12265 {
12266 struct window *w = XWINDOW (window);
12267 struct it it;
12268 struct glyph_row *last_text_row = NULL;
12269
12270 /* Make POS the new window start. */
12271 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12272
12273 /* Mark cursor position as unknown. No overlay arrow seen. */
12274 w->cursor.vpos = -1;
12275 overlay_arrow_seen = 0;
12276
12277 /* Initialize iterator and info to start at POS. */
12278 start_display (&it, w, pos);
12279
12280 /* Display all lines of W. */
12281 while (it.current_y < it.last_visible_y)
12282 {
12283 if (display_line (&it))
12284 last_text_row = it.glyph_row - 1;
12285 if (fonts_changed_p)
12286 return 0;
12287 }
12288
12289 /* If bottom moved off end of frame, change mode line percentage. */
12290 if (XFASTINT (w->window_end_pos) <= 0
12291 && Z != IT_CHARPOS (it))
12292 w->update_mode_line = Qt;
12293
12294 /* Set window_end_pos to the offset of the last character displayed
12295 on the window from the end of current_buffer. Set
12296 window_end_vpos to its row number. */
12297 if (last_text_row)
12298 {
12299 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12300 w->window_end_bytepos
12301 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12302 w->window_end_pos
12303 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12304 w->window_end_vpos
12305 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12306 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12307 ->displays_text_p);
12308 }
12309 else
12310 {
12311 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12312 w->window_end_pos = make_number (Z - ZV);
12313 w->window_end_vpos = make_number (0);
12314 }
12315
12316 /* But that is not valid info until redisplay finishes. */
12317 w->window_end_valid = Qnil;
12318 return 1;
12319 }
12320
12321
12322 \f
12323 /************************************************************************
12324 Window redisplay reusing current matrix when buffer has not changed
12325 ************************************************************************/
12326
12327 /* Try redisplay of window W showing an unchanged buffer with a
12328 different window start than the last time it was displayed by
12329 reusing its current matrix. Value is non-zero if successful.
12330 W->start is the new window start. */
12331
12332 static int
12333 try_window_reusing_current_matrix (w)
12334 struct window *w;
12335 {
12336 struct frame *f = XFRAME (w->frame);
12337 struct glyph_row *row, *bottom_row;
12338 struct it it;
12339 struct run run;
12340 struct text_pos start, new_start;
12341 int nrows_scrolled, i;
12342 struct glyph_row *last_text_row;
12343 struct glyph_row *last_reused_text_row;
12344 struct glyph_row *start_row;
12345 int start_vpos, min_y, max_y;
12346
12347 #if GLYPH_DEBUG
12348 if (inhibit_try_window_reusing)
12349 return 0;
12350 #endif
12351
12352 if (/* This function doesn't handle terminal frames. */
12353 !FRAME_WINDOW_P (f)
12354 /* Don't try to reuse the display if windows have been split
12355 or such. */
12356 || windows_or_buffers_changed
12357 || cursor_type_changed)
12358 return 0;
12359
12360 /* Can't do this if region may have changed. */
12361 if ((!NILP (Vtransient_mark_mode)
12362 && !NILP (current_buffer->mark_active))
12363 || !NILP (w->region_showing)
12364 || !NILP (Vshow_trailing_whitespace))
12365 return 0;
12366
12367 /* If top-line visibility has changed, give up. */
12368 if (WINDOW_WANTS_HEADER_LINE_P (w)
12369 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12370 return 0;
12371
12372 /* Give up if old or new display is scrolled vertically. We could
12373 make this function handle this, but right now it doesn't. */
12374 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12375 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
12376 return 0;
12377
12378 /* The variable new_start now holds the new window start. The old
12379 start `start' can be determined from the current matrix. */
12380 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12381 start = start_row->start.pos;
12382 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12383
12384 /* Clear the desired matrix for the display below. */
12385 clear_glyph_matrix (w->desired_matrix);
12386
12387 if (CHARPOS (new_start) <= CHARPOS (start))
12388 {
12389 int first_row_y;
12390
12391 /* Don't use this method if the display starts with an ellipsis
12392 displayed for invisible text. It's not easy to handle that case
12393 below, and it's certainly not worth the effort since this is
12394 not a frequent case. */
12395 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12396 return 0;
12397
12398 IF_DEBUG (debug_method_add (w, "twu1"));
12399
12400 /* Display up to a row that can be reused. The variable
12401 last_text_row is set to the last row displayed that displays
12402 text. Note that it.vpos == 0 if or if not there is a
12403 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12404 start_display (&it, w, new_start);
12405 first_row_y = it.current_y;
12406 w->cursor.vpos = -1;
12407 last_text_row = last_reused_text_row = NULL;
12408
12409 while (it.current_y < it.last_visible_y
12410 && !fonts_changed_p)
12411 {
12412 /* If we have reached into the characters in the START row,
12413 that means the line boundaries have changed. So we
12414 can't start copying with the row START. Maybe it will
12415 work to start copying with the following row. */
12416 while (IT_CHARPOS (it) > CHARPOS (start))
12417 {
12418 /* Advance to the next row as the "start". */
12419 start_row++;
12420 start = start_row->start.pos;
12421 /* If there are no more rows to try, or just one, give up. */
12422 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
12423 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row)
12424 || CHARPOS (start) == ZV)
12425 {
12426 clear_glyph_matrix (w->desired_matrix);
12427 return 0;
12428 }
12429
12430 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12431 }
12432 /* If we have reached alignment,
12433 we can copy the rest of the rows. */
12434 if (IT_CHARPOS (it) == CHARPOS (start))
12435 break;
12436
12437 if (display_line (&it))
12438 last_text_row = it.glyph_row - 1;
12439 }
12440
12441 /* A value of current_y < last_visible_y means that we stopped
12442 at the previous window start, which in turn means that we
12443 have at least one reusable row. */
12444 if (it.current_y < it.last_visible_y)
12445 {
12446 /* IT.vpos always starts from 0; it counts text lines. */
12447 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
12448
12449 /* Find PT if not already found in the lines displayed. */
12450 if (w->cursor.vpos < 0)
12451 {
12452 int dy = it.current_y - start_row->y;
12453
12454 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12455 row = row_containing_pos (w, PT, row, NULL, dy);
12456 if (row)
12457 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12458 dy, nrows_scrolled);
12459 else
12460 {
12461 clear_glyph_matrix (w->desired_matrix);
12462 return 0;
12463 }
12464 }
12465
12466 /* Scroll the display. Do it before the current matrix is
12467 changed. The problem here is that update has not yet
12468 run, i.e. part of the current matrix is not up to date.
12469 scroll_run_hook will clear the cursor, and use the
12470 current matrix to get the height of the row the cursor is
12471 in. */
12472 run.current_y = start_row->y;
12473 run.desired_y = it.current_y;
12474 run.height = it.last_visible_y - it.current_y;
12475
12476 if (run.height > 0 && run.current_y != run.desired_y)
12477 {
12478 update_begin (f);
12479 rif->update_window_begin_hook (w);
12480 rif->clear_window_mouse_face (w);
12481 rif->scroll_run_hook (w, &run);
12482 rif->update_window_end_hook (w, 0, 0);
12483 update_end (f);
12484 }
12485
12486 /* Shift current matrix down by nrows_scrolled lines. */
12487 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12488 rotate_matrix (w->current_matrix,
12489 start_vpos,
12490 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12491 nrows_scrolled);
12492
12493 /* Disable lines that must be updated. */
12494 for (i = 0; i < it.vpos; ++i)
12495 (start_row + i)->enabled_p = 0;
12496
12497 /* Re-compute Y positions. */
12498 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12499 max_y = it.last_visible_y;
12500 for (row = start_row + nrows_scrolled;
12501 row < bottom_row;
12502 ++row)
12503 {
12504 row->y = it.current_y;
12505 row->visible_height = row->height;
12506
12507 if (row->y < min_y)
12508 row->visible_height -= min_y - row->y;
12509 if (row->y + row->height > max_y)
12510 row->visible_height -= row->y + row->height - max_y;
12511 row->redraw_fringe_bitmaps_p = 1;
12512
12513 it.current_y += row->height;
12514
12515 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12516 last_reused_text_row = row;
12517 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12518 break;
12519 }
12520
12521 /* Disable lines in the current matrix which are now
12522 below the window. */
12523 for (++row; row < bottom_row; ++row)
12524 row->enabled_p = 0;
12525 }
12526
12527 /* Update window_end_pos etc.; last_reused_text_row is the last
12528 reused row from the current matrix containing text, if any.
12529 The value of last_text_row is the last displayed line
12530 containing text. */
12531 if (last_reused_text_row)
12532 {
12533 w->window_end_bytepos
12534 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12535 w->window_end_pos
12536 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12537 w->window_end_vpos
12538 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12539 w->current_matrix));
12540 }
12541 else if (last_text_row)
12542 {
12543 w->window_end_bytepos
12544 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12545 w->window_end_pos
12546 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12547 w->window_end_vpos
12548 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12549 }
12550 else
12551 {
12552 /* This window must be completely empty. */
12553 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12554 w->window_end_pos = make_number (Z - ZV);
12555 w->window_end_vpos = make_number (0);
12556 }
12557 w->window_end_valid = Qnil;
12558
12559 /* Update hint: don't try scrolling again in update_window. */
12560 w->desired_matrix->no_scrolling_p = 1;
12561
12562 #if GLYPH_DEBUG
12563 debug_method_add (w, "try_window_reusing_current_matrix 1");
12564 #endif
12565 return 1;
12566 }
12567 else if (CHARPOS (new_start) > CHARPOS (start))
12568 {
12569 struct glyph_row *pt_row, *row;
12570 struct glyph_row *first_reusable_row;
12571 struct glyph_row *first_row_to_display;
12572 int dy;
12573 int yb = window_text_bottom_y (w);
12574
12575 /* Find the row starting at new_start, if there is one. Don't
12576 reuse a partially visible line at the end. */
12577 first_reusable_row = start_row;
12578 while (first_reusable_row->enabled_p
12579 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12580 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12581 < CHARPOS (new_start)))
12582 ++first_reusable_row;
12583
12584 /* Give up if there is no row to reuse. */
12585 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
12586 || !first_reusable_row->enabled_p
12587 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12588 != CHARPOS (new_start)))
12589 return 0;
12590
12591 /* We can reuse fully visible rows beginning with
12592 first_reusable_row to the end of the window. Set
12593 first_row_to_display to the first row that cannot be reused.
12594 Set pt_row to the row containing point, if there is any. */
12595 pt_row = NULL;
12596 for (first_row_to_display = first_reusable_row;
12597 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12598 ++first_row_to_display)
12599 {
12600 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12601 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
12602 pt_row = first_row_to_display;
12603 }
12604
12605 /* Start displaying at the start of first_row_to_display. */
12606 xassert (first_row_to_display->y < yb);
12607 init_to_row_start (&it, w, first_row_to_display);
12608
12609 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12610 - start_vpos);
12611 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12612 - nrows_scrolled);
12613 it.current_y = (first_row_to_display->y - first_reusable_row->y
12614 + WINDOW_HEADER_LINE_HEIGHT (w));
12615
12616 /* Display lines beginning with first_row_to_display in the
12617 desired matrix. Set last_text_row to the last row displayed
12618 that displays text. */
12619 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12620 if (pt_row == NULL)
12621 w->cursor.vpos = -1;
12622 last_text_row = NULL;
12623 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12624 if (display_line (&it))
12625 last_text_row = it.glyph_row - 1;
12626
12627 /* Give up If point isn't in a row displayed or reused. */
12628 if (w->cursor.vpos < 0)
12629 {
12630 clear_glyph_matrix (w->desired_matrix);
12631 return 0;
12632 }
12633
12634 /* If point is in a reused row, adjust y and vpos of the cursor
12635 position. */
12636 if (pt_row)
12637 {
12638 w->cursor.vpos -= nrows_scrolled;
12639 w->cursor.y -= first_reusable_row->y - start_row->y;
12640 }
12641
12642 /* Scroll the display. */
12643 run.current_y = first_reusable_row->y;
12644 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
12645 run.height = it.last_visible_y - run.current_y;
12646 dy = run.current_y - run.desired_y;
12647
12648 if (run.height)
12649 {
12650 update_begin (f);
12651 rif->update_window_begin_hook (w);
12652 rif->clear_window_mouse_face (w);
12653 rif->scroll_run_hook (w, &run);
12654 rif->update_window_end_hook (w, 0, 0);
12655 update_end (f);
12656 }
12657
12658 /* Adjust Y positions of reused rows. */
12659 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12660 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12661 max_y = it.last_visible_y;
12662 for (row = first_reusable_row; row < first_row_to_display; ++row)
12663 {
12664 row->y -= dy;
12665 row->visible_height = row->height;
12666 if (row->y < min_y)
12667 row->visible_height -= min_y - row->y;
12668 if (row->y + row->height > max_y)
12669 row->visible_height -= row->y + row->height - max_y;
12670 row->redraw_fringe_bitmaps_p = 1;
12671 }
12672
12673 /* Scroll the current matrix. */
12674 xassert (nrows_scrolled > 0);
12675 rotate_matrix (w->current_matrix,
12676 start_vpos,
12677 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12678 -nrows_scrolled);
12679
12680 /* Disable rows not reused. */
12681 for (row -= nrows_scrolled; row < bottom_row; ++row)
12682 row->enabled_p = 0;
12683
12684 /* Point may have moved to a different line, so we cannot assume that
12685 the previous cursor position is valid; locate the correct row. */
12686 if (pt_row)
12687 {
12688 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12689 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
12690 row++)
12691 {
12692 w->cursor.vpos++;
12693 w->cursor.y = row->y;
12694 }
12695 if (row < bottom_row)
12696 {
12697 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
12698 while (glyph->charpos < PT)
12699 {
12700 w->cursor.hpos++;
12701 w->cursor.x += glyph->pixel_width;
12702 glyph++;
12703 }
12704 }
12705 }
12706
12707 /* Adjust window end. A null value of last_text_row means that
12708 the window end is in reused rows which in turn means that
12709 only its vpos can have changed. */
12710 if (last_text_row)
12711 {
12712 w->window_end_bytepos
12713 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12714 w->window_end_pos
12715 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12716 w->window_end_vpos
12717 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12718 }
12719 else
12720 {
12721 w->window_end_vpos
12722 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
12723 }
12724
12725 w->window_end_valid = Qnil;
12726 w->desired_matrix->no_scrolling_p = 1;
12727
12728 #if GLYPH_DEBUG
12729 debug_method_add (w, "try_window_reusing_current_matrix 2");
12730 #endif
12731 return 1;
12732 }
12733
12734 return 0;
12735 }
12736
12737
12738 \f
12739 /************************************************************************
12740 Window redisplay reusing current matrix when buffer has changed
12741 ************************************************************************/
12742
12743 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
12744 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
12745 int *, int *));
12746 static struct glyph_row *
12747 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
12748 struct glyph_row *));
12749
12750
12751 /* Return the last row in MATRIX displaying text. If row START is
12752 non-null, start searching with that row. IT gives the dimensions
12753 of the display. Value is null if matrix is empty; otherwise it is
12754 a pointer to the row found. */
12755
12756 static struct glyph_row *
12757 find_last_row_displaying_text (matrix, it, start)
12758 struct glyph_matrix *matrix;
12759 struct it *it;
12760 struct glyph_row *start;
12761 {
12762 struct glyph_row *row, *row_found;
12763
12764 /* Set row_found to the last row in IT->w's current matrix
12765 displaying text. The loop looks funny but think of partially
12766 visible lines. */
12767 row_found = NULL;
12768 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
12769 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12770 {
12771 xassert (row->enabled_p);
12772 row_found = row;
12773 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
12774 break;
12775 ++row;
12776 }
12777
12778 return row_found;
12779 }
12780
12781
12782 /* Return the last row in the current matrix of W that is not affected
12783 by changes at the start of current_buffer that occurred since W's
12784 current matrix was built. Value is null if no such row exists.
12785
12786 BEG_UNCHANGED us the number of characters unchanged at the start of
12787 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
12788 first changed character in current_buffer. Characters at positions <
12789 BEG + BEG_UNCHANGED are at the same buffer positions as they were
12790 when the current matrix was built. */
12791
12792 static struct glyph_row *
12793 find_last_unchanged_at_beg_row (w)
12794 struct window *w;
12795 {
12796 int first_changed_pos = BEG + BEG_UNCHANGED;
12797 struct glyph_row *row;
12798 struct glyph_row *row_found = NULL;
12799 int yb = window_text_bottom_y (w);
12800
12801 /* Find the last row displaying unchanged text. */
12802 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12803 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12804 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
12805 {
12806 if (/* If row ends before first_changed_pos, it is unchanged,
12807 except in some case. */
12808 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
12809 /* When row ends in ZV and we write at ZV it is not
12810 unchanged. */
12811 && !row->ends_at_zv_p
12812 /* When first_changed_pos is the end of a continued line,
12813 row is not unchanged because it may be no longer
12814 continued. */
12815 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
12816 && (row->continued_p
12817 || row->exact_window_width_line_p)))
12818 row_found = row;
12819
12820 /* Stop if last visible row. */
12821 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
12822 break;
12823
12824 ++row;
12825 }
12826
12827 return row_found;
12828 }
12829
12830
12831 /* Find the first glyph row in the current matrix of W that is not
12832 affected by changes at the end of current_buffer since the
12833 time W's current matrix was built.
12834
12835 Return in *DELTA the number of chars by which buffer positions in
12836 unchanged text at the end of current_buffer must be adjusted.
12837
12838 Return in *DELTA_BYTES the corresponding number of bytes.
12839
12840 Value is null if no such row exists, i.e. all rows are affected by
12841 changes. */
12842
12843 static struct glyph_row *
12844 find_first_unchanged_at_end_row (w, delta, delta_bytes)
12845 struct window *w;
12846 int *delta, *delta_bytes;
12847 {
12848 struct glyph_row *row;
12849 struct glyph_row *row_found = NULL;
12850
12851 *delta = *delta_bytes = 0;
12852
12853 /* Display must not have been paused, otherwise the current matrix
12854 is not up to date. */
12855 if (NILP (w->window_end_valid))
12856 abort ();
12857
12858 /* A value of window_end_pos >= END_UNCHANGED means that the window
12859 end is in the range of changed text. If so, there is no
12860 unchanged row at the end of W's current matrix. */
12861 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
12862 return NULL;
12863
12864 /* Set row to the last row in W's current matrix displaying text. */
12865 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12866
12867 /* If matrix is entirely empty, no unchanged row exists. */
12868 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12869 {
12870 /* The value of row is the last glyph row in the matrix having a
12871 meaningful buffer position in it. The end position of row
12872 corresponds to window_end_pos. This allows us to translate
12873 buffer positions in the current matrix to current buffer
12874 positions for characters not in changed text. */
12875 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12876 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12877 int last_unchanged_pos, last_unchanged_pos_old;
12878 struct glyph_row *first_text_row
12879 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12880
12881 *delta = Z - Z_old;
12882 *delta_bytes = Z_BYTE - Z_BYTE_old;
12883
12884 /* Set last_unchanged_pos to the buffer position of the last
12885 character in the buffer that has not been changed. Z is the
12886 index + 1 of the last character in current_buffer, i.e. by
12887 subtracting END_UNCHANGED we get the index of the last
12888 unchanged character, and we have to add BEG to get its buffer
12889 position. */
12890 last_unchanged_pos = Z - END_UNCHANGED + BEG;
12891 last_unchanged_pos_old = last_unchanged_pos - *delta;
12892
12893 /* Search backward from ROW for a row displaying a line that
12894 starts at a minimum position >= last_unchanged_pos_old. */
12895 for (; row > first_text_row; --row)
12896 {
12897 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
12898 abort ();
12899
12900 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
12901 row_found = row;
12902 }
12903 }
12904
12905 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
12906 abort ();
12907
12908 return row_found;
12909 }
12910
12911
12912 /* Make sure that glyph rows in the current matrix of window W
12913 reference the same glyph memory as corresponding rows in the
12914 frame's frame matrix. This function is called after scrolling W's
12915 current matrix on a terminal frame in try_window_id and
12916 try_window_reusing_current_matrix. */
12917
12918 static void
12919 sync_frame_with_window_matrix_rows (w)
12920 struct window *w;
12921 {
12922 struct frame *f = XFRAME (w->frame);
12923 struct glyph_row *window_row, *window_row_end, *frame_row;
12924
12925 /* Preconditions: W must be a leaf window and full-width. Its frame
12926 must have a frame matrix. */
12927 xassert (NILP (w->hchild) && NILP (w->vchild));
12928 xassert (WINDOW_FULL_WIDTH_P (w));
12929 xassert (!FRAME_WINDOW_P (f));
12930
12931 /* If W is a full-width window, glyph pointers in W's current matrix
12932 have, by definition, to be the same as glyph pointers in the
12933 corresponding frame matrix. Note that frame matrices have no
12934 marginal areas (see build_frame_matrix). */
12935 window_row = w->current_matrix->rows;
12936 window_row_end = window_row + w->current_matrix->nrows;
12937 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
12938 while (window_row < window_row_end)
12939 {
12940 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
12941 struct glyph *end = window_row->glyphs[LAST_AREA];
12942
12943 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
12944 frame_row->glyphs[TEXT_AREA] = start;
12945 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
12946 frame_row->glyphs[LAST_AREA] = end;
12947
12948 /* Disable frame rows whose corresponding window rows have
12949 been disabled in try_window_id. */
12950 if (!window_row->enabled_p)
12951 frame_row->enabled_p = 0;
12952
12953 ++window_row, ++frame_row;
12954 }
12955 }
12956
12957
12958 /* Find the glyph row in window W containing CHARPOS. Consider all
12959 rows between START and END (not inclusive). END null means search
12960 all rows to the end of the display area of W. Value is the row
12961 containing CHARPOS or null. */
12962
12963 struct glyph_row *
12964 row_containing_pos (w, charpos, start, end, dy)
12965 struct window *w;
12966 int charpos;
12967 struct glyph_row *start, *end;
12968 int dy;
12969 {
12970 struct glyph_row *row = start;
12971 int last_y;
12972
12973 /* If we happen to start on a header-line, skip that. */
12974 if (row->mode_line_p)
12975 ++row;
12976
12977 if ((end && row >= end) || !row->enabled_p)
12978 return NULL;
12979
12980 last_y = window_text_bottom_y (w) - dy;
12981
12982 while (1)
12983 {
12984 /* Give up if we have gone too far. */
12985 if (end && row >= end)
12986 return NULL;
12987 /* This formerly returned if they were equal.
12988 I think that both quantities are of a "last plus one" type;
12989 if so, when they are equal, the row is within the screen. -- rms. */
12990 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
12991 return NULL;
12992
12993 /* If it is in this row, return this row. */
12994 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
12995 || (MATRIX_ROW_END_CHARPOS (row) == charpos
12996 /* The end position of a row equals the start
12997 position of the next row. If CHARPOS is there, we
12998 would rather display it in the next line, except
12999 when this line ends in ZV. */
13000 && !row->ends_at_zv_p
13001 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13002 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13003 return row;
13004 ++row;
13005 }
13006 }
13007
13008
13009 /* Try to redisplay window W by reusing its existing display. W's
13010 current matrix must be up to date when this function is called,
13011 i.e. window_end_valid must not be nil.
13012
13013 Value is
13014
13015 1 if display has been updated
13016 0 if otherwise unsuccessful
13017 -1 if redisplay with same window start is known not to succeed
13018
13019 The following steps are performed:
13020
13021 1. Find the last row in the current matrix of W that is not
13022 affected by changes at the start of current_buffer. If no such row
13023 is found, give up.
13024
13025 2. Find the first row in W's current matrix that is not affected by
13026 changes at the end of current_buffer. Maybe there is no such row.
13027
13028 3. Display lines beginning with the row + 1 found in step 1 to the
13029 row found in step 2 or, if step 2 didn't find a row, to the end of
13030 the window.
13031
13032 4. If cursor is not known to appear on the window, give up.
13033
13034 5. If display stopped at the row found in step 2, scroll the
13035 display and current matrix as needed.
13036
13037 6. Maybe display some lines at the end of W, if we must. This can
13038 happen under various circumstances, like a partially visible line
13039 becoming fully visible, or because newly displayed lines are displayed
13040 in smaller font sizes.
13041
13042 7. Update W's window end information. */
13043
13044 static int
13045 try_window_id (w)
13046 struct window *w;
13047 {
13048 struct frame *f = XFRAME (w->frame);
13049 struct glyph_matrix *current_matrix = w->current_matrix;
13050 struct glyph_matrix *desired_matrix = w->desired_matrix;
13051 struct glyph_row *last_unchanged_at_beg_row;
13052 struct glyph_row *first_unchanged_at_end_row;
13053 struct glyph_row *row;
13054 struct glyph_row *bottom_row;
13055 int bottom_vpos;
13056 struct it it;
13057 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13058 struct text_pos start_pos;
13059 struct run run;
13060 int first_unchanged_at_end_vpos = 0;
13061 struct glyph_row *last_text_row, *last_text_row_at_end;
13062 struct text_pos start;
13063 int first_changed_charpos, last_changed_charpos;
13064
13065 #if GLYPH_DEBUG
13066 if (inhibit_try_window_id)
13067 return 0;
13068 #endif
13069
13070 /* This is handy for debugging. */
13071 #if 0
13072 #define GIVE_UP(X) \
13073 do { \
13074 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13075 return 0; \
13076 } while (0)
13077 #else
13078 #define GIVE_UP(X) return 0
13079 #endif
13080
13081 SET_TEXT_POS_FROM_MARKER (start, w->start);
13082
13083 /* Don't use this for mini-windows because these can show
13084 messages and mini-buffers, and we don't handle that here. */
13085 if (MINI_WINDOW_P (w))
13086 GIVE_UP (1);
13087
13088 /* This flag is used to prevent redisplay optimizations. */
13089 if (windows_or_buffers_changed || cursor_type_changed)
13090 GIVE_UP (2);
13091
13092 /* Verify that narrowing has not changed.
13093 Also verify that we were not told to prevent redisplay optimizations.
13094 It would be nice to further
13095 reduce the number of cases where this prevents try_window_id. */
13096 if (current_buffer->clip_changed
13097 || current_buffer->prevent_redisplay_optimizations_p)
13098 GIVE_UP (3);
13099
13100 /* Window must either use window-based redisplay or be full width. */
13101 if (!FRAME_WINDOW_P (f)
13102 && (!line_ins_del_ok
13103 || !WINDOW_FULL_WIDTH_P (w)))
13104 GIVE_UP (4);
13105
13106 /* Give up if point is not known NOT to appear in W. */
13107 if (PT < CHARPOS (start))
13108 GIVE_UP (5);
13109
13110 /* Another way to prevent redisplay optimizations. */
13111 if (XFASTINT (w->last_modified) == 0)
13112 GIVE_UP (6);
13113
13114 /* Verify that window is not hscrolled. */
13115 if (XFASTINT (w->hscroll) != 0)
13116 GIVE_UP (7);
13117
13118 /* Verify that display wasn't paused. */
13119 if (NILP (w->window_end_valid))
13120 GIVE_UP (8);
13121
13122 /* Can't use this if highlighting a region because a cursor movement
13123 will do more than just set the cursor. */
13124 if (!NILP (Vtransient_mark_mode)
13125 && !NILP (current_buffer->mark_active))
13126 GIVE_UP (9);
13127
13128 /* Likewise if highlighting trailing whitespace. */
13129 if (!NILP (Vshow_trailing_whitespace))
13130 GIVE_UP (11);
13131
13132 /* Likewise if showing a region. */
13133 if (!NILP (w->region_showing))
13134 GIVE_UP (10);
13135
13136 /* Can use this if overlay arrow position and or string have changed. */
13137 if (overlay_arrows_changed_p ())
13138 GIVE_UP (12);
13139
13140
13141 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13142 only if buffer has really changed. The reason is that the gap is
13143 initially at Z for freshly visited files. The code below would
13144 set end_unchanged to 0 in that case. */
13145 if (MODIFF > SAVE_MODIFF
13146 /* This seems to happen sometimes after saving a buffer. */
13147 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13148 {
13149 if (GPT - BEG < BEG_UNCHANGED)
13150 BEG_UNCHANGED = GPT - BEG;
13151 if (Z - GPT < END_UNCHANGED)
13152 END_UNCHANGED = Z - GPT;
13153 }
13154
13155 /* The position of the first and last character that has been changed. */
13156 first_changed_charpos = BEG + BEG_UNCHANGED;
13157 last_changed_charpos = Z - END_UNCHANGED;
13158
13159 /* If window starts after a line end, and the last change is in
13160 front of that newline, then changes don't affect the display.
13161 This case happens with stealth-fontification. Note that although
13162 the display is unchanged, glyph positions in the matrix have to
13163 be adjusted, of course. */
13164 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13165 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13166 && ((last_changed_charpos < CHARPOS (start)
13167 && CHARPOS (start) == BEGV)
13168 || (last_changed_charpos < CHARPOS (start) - 1
13169 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13170 {
13171 int Z_old, delta, Z_BYTE_old, delta_bytes;
13172 struct glyph_row *r0;
13173
13174 /* Compute how many chars/bytes have been added to or removed
13175 from the buffer. */
13176 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13177 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13178 delta = Z - Z_old;
13179 delta_bytes = Z_BYTE - Z_BYTE_old;
13180
13181 /* Give up if PT is not in the window. Note that it already has
13182 been checked at the start of try_window_id that PT is not in
13183 front of the window start. */
13184 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13185 GIVE_UP (13);
13186
13187 /* If window start is unchanged, we can reuse the whole matrix
13188 as is, after adjusting glyph positions. No need to compute
13189 the window end again, since its offset from Z hasn't changed. */
13190 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13191 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13192 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13193 /* PT must not be in a partially visible line. */
13194 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13195 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13196 {
13197 /* Adjust positions in the glyph matrix. */
13198 if (delta || delta_bytes)
13199 {
13200 struct glyph_row *r1
13201 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13202 increment_matrix_positions (w->current_matrix,
13203 MATRIX_ROW_VPOS (r0, current_matrix),
13204 MATRIX_ROW_VPOS (r1, current_matrix),
13205 delta, delta_bytes);
13206 }
13207
13208 /* Set the cursor. */
13209 row = row_containing_pos (w, PT, r0, NULL, 0);
13210 if (row)
13211 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13212 else
13213 abort ();
13214 return 1;
13215 }
13216 }
13217
13218 /* Handle the case that changes are all below what is displayed in
13219 the window, and that PT is in the window. This shortcut cannot
13220 be taken if ZV is visible in the window, and text has been added
13221 there that is visible in the window. */
13222 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13223 /* ZV is not visible in the window, or there are no
13224 changes at ZV, actually. */
13225 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13226 || first_changed_charpos == last_changed_charpos))
13227 {
13228 struct glyph_row *r0;
13229
13230 /* Give up if PT is not in the window. Note that it already has
13231 been checked at the start of try_window_id that PT is not in
13232 front of the window start. */
13233 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13234 GIVE_UP (14);
13235
13236 /* If window start is unchanged, we can reuse the whole matrix
13237 as is, without changing glyph positions since no text has
13238 been added/removed in front of the window end. */
13239 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13240 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13241 /* PT must not be in a partially visible line. */
13242 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13243 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13244 {
13245 /* We have to compute the window end anew since text
13246 can have been added/removed after it. */
13247 w->window_end_pos
13248 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13249 w->window_end_bytepos
13250 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13251
13252 /* Set the cursor. */
13253 row = row_containing_pos (w, PT, r0, NULL, 0);
13254 if (row)
13255 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13256 else
13257 abort ();
13258 return 2;
13259 }
13260 }
13261
13262 /* Give up if window start is in the changed area.
13263
13264 The condition used to read
13265
13266 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13267
13268 but why that was tested escapes me at the moment. */
13269 if (CHARPOS (start) >= first_changed_charpos
13270 && CHARPOS (start) <= last_changed_charpos)
13271 GIVE_UP (15);
13272
13273 /* Check that window start agrees with the start of the first glyph
13274 row in its current matrix. Check this after we know the window
13275 start is not in changed text, otherwise positions would not be
13276 comparable. */
13277 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13278 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13279 GIVE_UP (16);
13280
13281 /* Give up if the window ends in strings. Overlay strings
13282 at the end are difficult to handle, so don't try. */
13283 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13284 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13285 GIVE_UP (20);
13286
13287 /* Compute the position at which we have to start displaying new
13288 lines. Some of the lines at the top of the window might be
13289 reusable because they are not displaying changed text. Find the
13290 last row in W's current matrix not affected by changes at the
13291 start of current_buffer. Value is null if changes start in the
13292 first line of window. */
13293 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13294 if (last_unchanged_at_beg_row)
13295 {
13296 /* Avoid starting to display in the moddle of a character, a TAB
13297 for instance. This is easier than to set up the iterator
13298 exactly, and it's not a frequent case, so the additional
13299 effort wouldn't really pay off. */
13300 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13301 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13302 && last_unchanged_at_beg_row > w->current_matrix->rows)
13303 --last_unchanged_at_beg_row;
13304
13305 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13306 GIVE_UP (17);
13307
13308 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13309 GIVE_UP (18);
13310 start_pos = it.current.pos;
13311
13312 /* Start displaying new lines in the desired matrix at the same
13313 vpos we would use in the current matrix, i.e. below
13314 last_unchanged_at_beg_row. */
13315 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13316 current_matrix);
13317 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13318 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13319
13320 xassert (it.hpos == 0 && it.current_x == 0);
13321 }
13322 else
13323 {
13324 /* There are no reusable lines at the start of the window.
13325 Start displaying in the first text line. */
13326 start_display (&it, w, start);
13327 it.vpos = it.first_vpos;
13328 start_pos = it.current.pos;
13329 }
13330
13331 /* Find the first row that is not affected by changes at the end of
13332 the buffer. Value will be null if there is no unchanged row, in
13333 which case we must redisplay to the end of the window. delta
13334 will be set to the value by which buffer positions beginning with
13335 first_unchanged_at_end_row have to be adjusted due to text
13336 changes. */
13337 first_unchanged_at_end_row
13338 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13339 IF_DEBUG (debug_delta = delta);
13340 IF_DEBUG (debug_delta_bytes = delta_bytes);
13341
13342 /* Set stop_pos to the buffer position up to which we will have to
13343 display new lines. If first_unchanged_at_end_row != NULL, this
13344 is the buffer position of the start of the line displayed in that
13345 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13346 that we don't stop at a buffer position. */
13347 stop_pos = 0;
13348 if (first_unchanged_at_end_row)
13349 {
13350 xassert (last_unchanged_at_beg_row == NULL
13351 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13352
13353 /* If this is a continuation line, move forward to the next one
13354 that isn't. Changes in lines above affect this line.
13355 Caution: this may move first_unchanged_at_end_row to a row
13356 not displaying text. */
13357 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13358 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13359 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13360 < it.last_visible_y))
13361 ++first_unchanged_at_end_row;
13362
13363 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13364 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13365 >= it.last_visible_y))
13366 first_unchanged_at_end_row = NULL;
13367 else
13368 {
13369 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13370 + delta);
13371 first_unchanged_at_end_vpos
13372 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13373 xassert (stop_pos >= Z - END_UNCHANGED);
13374 }
13375 }
13376 else if (last_unchanged_at_beg_row == NULL)
13377 GIVE_UP (19);
13378
13379
13380 #if GLYPH_DEBUG
13381
13382 /* Either there is no unchanged row at the end, or the one we have
13383 now displays text. This is a necessary condition for the window
13384 end pos calculation at the end of this function. */
13385 xassert (first_unchanged_at_end_row == NULL
13386 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13387
13388 debug_last_unchanged_at_beg_vpos
13389 = (last_unchanged_at_beg_row
13390 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13391 : -1);
13392 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13393
13394 #endif /* GLYPH_DEBUG != 0 */
13395
13396
13397 /* Display new lines. Set last_text_row to the last new line
13398 displayed which has text on it, i.e. might end up as being the
13399 line where the window_end_vpos is. */
13400 w->cursor.vpos = -1;
13401 last_text_row = NULL;
13402 overlay_arrow_seen = 0;
13403 while (it.current_y < it.last_visible_y
13404 && !fonts_changed_p
13405 && (first_unchanged_at_end_row == NULL
13406 || IT_CHARPOS (it) < stop_pos))
13407 {
13408 if (display_line (&it))
13409 last_text_row = it.glyph_row - 1;
13410 }
13411
13412 if (fonts_changed_p)
13413 return -1;
13414
13415
13416 /* Compute differences in buffer positions, y-positions etc. for
13417 lines reused at the bottom of the window. Compute what we can
13418 scroll. */
13419 if (first_unchanged_at_end_row
13420 /* No lines reused because we displayed everything up to the
13421 bottom of the window. */
13422 && it.current_y < it.last_visible_y)
13423 {
13424 dvpos = (it.vpos
13425 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13426 current_matrix));
13427 dy = it.current_y - first_unchanged_at_end_row->y;
13428 run.current_y = first_unchanged_at_end_row->y;
13429 run.desired_y = run.current_y + dy;
13430 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13431 }
13432 else
13433 {
13434 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13435 first_unchanged_at_end_row = NULL;
13436 }
13437 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13438
13439
13440 /* Find the cursor if not already found. We have to decide whether
13441 PT will appear on this window (it sometimes doesn't, but this is
13442 not a very frequent case.) This decision has to be made before
13443 the current matrix is altered. A value of cursor.vpos < 0 means
13444 that PT is either in one of the lines beginning at
13445 first_unchanged_at_end_row or below the window. Don't care for
13446 lines that might be displayed later at the window end; as
13447 mentioned, this is not a frequent case. */
13448 if (w->cursor.vpos < 0)
13449 {
13450 /* Cursor in unchanged rows at the top? */
13451 if (PT < CHARPOS (start_pos)
13452 && last_unchanged_at_beg_row)
13453 {
13454 row = row_containing_pos (w, PT,
13455 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13456 last_unchanged_at_beg_row + 1, 0);
13457 if (row)
13458 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13459 }
13460
13461 /* Start from first_unchanged_at_end_row looking for PT. */
13462 else if (first_unchanged_at_end_row)
13463 {
13464 row = row_containing_pos (w, PT - delta,
13465 first_unchanged_at_end_row, NULL, 0);
13466 if (row)
13467 set_cursor_from_row (w, row, w->current_matrix, delta,
13468 delta_bytes, dy, dvpos);
13469 }
13470
13471 /* Give up if cursor was not found. */
13472 if (w->cursor.vpos < 0)
13473 {
13474 clear_glyph_matrix (w->desired_matrix);
13475 return -1;
13476 }
13477 }
13478
13479 /* Don't let the cursor end in the scroll margins. */
13480 {
13481 int this_scroll_margin, cursor_height;
13482
13483 this_scroll_margin = max (0, scroll_margin);
13484 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13485 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13486 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13487
13488 if ((w->cursor.y < this_scroll_margin
13489 && CHARPOS (start) > BEGV)
13490 /* Old redisplay didn't take scroll margin into account at the bottom,
13491 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
13492 || w->cursor.y + cursor_height + this_scroll_margin > it.last_visible_y)
13493 {
13494 w->cursor.vpos = -1;
13495 clear_glyph_matrix (w->desired_matrix);
13496 return -1;
13497 }
13498 }
13499
13500 /* Scroll the display. Do it before changing the current matrix so
13501 that xterm.c doesn't get confused about where the cursor glyph is
13502 found. */
13503 if (dy && run.height)
13504 {
13505 update_begin (f);
13506
13507 if (FRAME_WINDOW_P (f))
13508 {
13509 rif->update_window_begin_hook (w);
13510 rif->clear_window_mouse_face (w);
13511 rif->scroll_run_hook (w, &run);
13512 rif->update_window_end_hook (w, 0, 0);
13513 }
13514 else
13515 {
13516 /* Terminal frame. In this case, dvpos gives the number of
13517 lines to scroll by; dvpos < 0 means scroll up. */
13518 int first_unchanged_at_end_vpos
13519 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13520 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13521 int end = (WINDOW_TOP_EDGE_LINE (w)
13522 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13523 + window_internal_height (w));
13524
13525 /* Perform the operation on the screen. */
13526 if (dvpos > 0)
13527 {
13528 /* Scroll last_unchanged_at_beg_row to the end of the
13529 window down dvpos lines. */
13530 set_terminal_window (end);
13531
13532 /* On dumb terminals delete dvpos lines at the end
13533 before inserting dvpos empty lines. */
13534 if (!scroll_region_ok)
13535 ins_del_lines (end - dvpos, -dvpos);
13536
13537 /* Insert dvpos empty lines in front of
13538 last_unchanged_at_beg_row. */
13539 ins_del_lines (from, dvpos);
13540 }
13541 else if (dvpos < 0)
13542 {
13543 /* Scroll up last_unchanged_at_beg_vpos to the end of
13544 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13545 set_terminal_window (end);
13546
13547 /* Delete dvpos lines in front of
13548 last_unchanged_at_beg_vpos. ins_del_lines will set
13549 the cursor to the given vpos and emit |dvpos| delete
13550 line sequences. */
13551 ins_del_lines (from + dvpos, dvpos);
13552
13553 /* On a dumb terminal insert dvpos empty lines at the
13554 end. */
13555 if (!scroll_region_ok)
13556 ins_del_lines (end + dvpos, -dvpos);
13557 }
13558
13559 set_terminal_window (0);
13560 }
13561
13562 update_end (f);
13563 }
13564
13565 /* Shift reused rows of the current matrix to the right position.
13566 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13567 text. */
13568 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13569 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
13570 if (dvpos < 0)
13571 {
13572 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13573 bottom_vpos, dvpos);
13574 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13575 bottom_vpos, 0);
13576 }
13577 else if (dvpos > 0)
13578 {
13579 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13580 bottom_vpos, dvpos);
13581 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13582 first_unchanged_at_end_vpos + dvpos, 0);
13583 }
13584
13585 /* For frame-based redisplay, make sure that current frame and window
13586 matrix are in sync with respect to glyph memory. */
13587 if (!FRAME_WINDOW_P (f))
13588 sync_frame_with_window_matrix_rows (w);
13589
13590 /* Adjust buffer positions in reused rows. */
13591 if (delta)
13592 increment_matrix_positions (current_matrix,
13593 first_unchanged_at_end_vpos + dvpos,
13594 bottom_vpos, delta, delta_bytes);
13595
13596 /* Adjust Y positions. */
13597 if (dy)
13598 shift_glyph_matrix (w, current_matrix,
13599 first_unchanged_at_end_vpos + dvpos,
13600 bottom_vpos, dy);
13601
13602 if (first_unchanged_at_end_row)
13603 first_unchanged_at_end_row += dvpos;
13604
13605 /* If scrolling up, there may be some lines to display at the end of
13606 the window. */
13607 last_text_row_at_end = NULL;
13608 if (dy < 0)
13609 {
13610 /* Scrolling up can leave for example a partially visible line
13611 at the end of the window to be redisplayed. */
13612 /* Set last_row to the glyph row in the current matrix where the
13613 window end line is found. It has been moved up or down in
13614 the matrix by dvpos. */
13615 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13616 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13617
13618 /* If last_row is the window end line, it should display text. */
13619 xassert (last_row->displays_text_p);
13620
13621 /* If window end line was partially visible before, begin
13622 displaying at that line. Otherwise begin displaying with the
13623 line following it. */
13624 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13625 {
13626 init_to_row_start (&it, w, last_row);
13627 it.vpos = last_vpos;
13628 it.current_y = last_row->y;
13629 }
13630 else
13631 {
13632 init_to_row_end (&it, w, last_row);
13633 it.vpos = 1 + last_vpos;
13634 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13635 ++last_row;
13636 }
13637
13638 /* We may start in a continuation line. If so, we have to
13639 get the right continuation_lines_width and current_x. */
13640 it.continuation_lines_width = last_row->continuation_lines_width;
13641 it.hpos = it.current_x = 0;
13642
13643 /* Display the rest of the lines at the window end. */
13644 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13645 while (it.current_y < it.last_visible_y
13646 && !fonts_changed_p)
13647 {
13648 /* Is it always sure that the display agrees with lines in
13649 the current matrix? I don't think so, so we mark rows
13650 displayed invalid in the current matrix by setting their
13651 enabled_p flag to zero. */
13652 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13653 if (display_line (&it))
13654 last_text_row_at_end = it.glyph_row - 1;
13655 }
13656 }
13657
13658 /* Update window_end_pos and window_end_vpos. */
13659 if (first_unchanged_at_end_row
13660 && first_unchanged_at_end_row->y < it.last_visible_y
13661 && !last_text_row_at_end)
13662 {
13663 /* Window end line if one of the preserved rows from the current
13664 matrix. Set row to the last row displaying text in current
13665 matrix starting at first_unchanged_at_end_row, after
13666 scrolling. */
13667 xassert (first_unchanged_at_end_row->displays_text_p);
13668 row = find_last_row_displaying_text (w->current_matrix, &it,
13669 first_unchanged_at_end_row);
13670 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
13671
13672 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13673 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13674 w->window_end_vpos
13675 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
13676 xassert (w->window_end_bytepos >= 0);
13677 IF_DEBUG (debug_method_add (w, "A"));
13678 }
13679 else if (last_text_row_at_end)
13680 {
13681 w->window_end_pos
13682 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
13683 w->window_end_bytepos
13684 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
13685 w->window_end_vpos
13686 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
13687 xassert (w->window_end_bytepos >= 0);
13688 IF_DEBUG (debug_method_add (w, "B"));
13689 }
13690 else if (last_text_row)
13691 {
13692 /* We have displayed either to the end of the window or at the
13693 end of the window, i.e. the last row with text is to be found
13694 in the desired matrix. */
13695 w->window_end_pos
13696 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13697 w->window_end_bytepos
13698 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13699 w->window_end_vpos
13700 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
13701 xassert (w->window_end_bytepos >= 0);
13702 }
13703 else if (first_unchanged_at_end_row == NULL
13704 && last_text_row == NULL
13705 && last_text_row_at_end == NULL)
13706 {
13707 /* Displayed to end of window, but no line containing text was
13708 displayed. Lines were deleted at the end of the window. */
13709 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
13710 int vpos = XFASTINT (w->window_end_vpos);
13711 struct glyph_row *current_row = current_matrix->rows + vpos;
13712 struct glyph_row *desired_row = desired_matrix->rows + vpos;
13713
13714 for (row = NULL;
13715 row == NULL && vpos >= first_vpos;
13716 --vpos, --current_row, --desired_row)
13717 {
13718 if (desired_row->enabled_p)
13719 {
13720 if (desired_row->displays_text_p)
13721 row = desired_row;
13722 }
13723 else if (current_row->displays_text_p)
13724 row = current_row;
13725 }
13726
13727 xassert (row != NULL);
13728 w->window_end_vpos = make_number (vpos + 1);
13729 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13730 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13731 xassert (w->window_end_bytepos >= 0);
13732 IF_DEBUG (debug_method_add (w, "C"));
13733 }
13734 else
13735 abort ();
13736
13737 #if 0 /* This leads to problems, for instance when the cursor is
13738 at ZV, and the cursor line displays no text. */
13739 /* Disable rows below what's displayed in the window. This makes
13740 debugging easier. */
13741 enable_glyph_matrix_rows (current_matrix,
13742 XFASTINT (w->window_end_vpos) + 1,
13743 bottom_vpos, 0);
13744 #endif
13745
13746 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
13747 debug_end_vpos = XFASTINT (w->window_end_vpos));
13748
13749 /* Record that display has not been completed. */
13750 w->window_end_valid = Qnil;
13751 w->desired_matrix->no_scrolling_p = 1;
13752 return 3;
13753
13754 #undef GIVE_UP
13755 }
13756
13757
13758 \f
13759 /***********************************************************************
13760 More debugging support
13761 ***********************************************************************/
13762
13763 #if GLYPH_DEBUG
13764
13765 void dump_glyph_row P_ ((struct glyph_row *, int, int));
13766 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
13767 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
13768
13769
13770 /* Dump the contents of glyph matrix MATRIX on stderr.
13771
13772 GLYPHS 0 means don't show glyph contents.
13773 GLYPHS 1 means show glyphs in short form
13774 GLYPHS > 1 means show glyphs in long form. */
13775
13776 void
13777 dump_glyph_matrix (matrix, glyphs)
13778 struct glyph_matrix *matrix;
13779 int glyphs;
13780 {
13781 int i;
13782 for (i = 0; i < matrix->nrows; ++i)
13783 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
13784 }
13785
13786
13787 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
13788 the glyph row and area where the glyph comes from. */
13789
13790 void
13791 dump_glyph (row, glyph, area)
13792 struct glyph_row *row;
13793 struct glyph *glyph;
13794 int area;
13795 {
13796 if (glyph->type == CHAR_GLYPH)
13797 {
13798 fprintf (stderr,
13799 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13800 glyph - row->glyphs[TEXT_AREA],
13801 'C',
13802 glyph->charpos,
13803 (BUFFERP (glyph->object)
13804 ? 'B'
13805 : (STRINGP (glyph->object)
13806 ? 'S'
13807 : '-')),
13808 glyph->pixel_width,
13809 glyph->u.ch,
13810 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
13811 ? glyph->u.ch
13812 : '.'),
13813 glyph->face_id,
13814 glyph->left_box_line_p,
13815 glyph->right_box_line_p);
13816 }
13817 else if (glyph->type == STRETCH_GLYPH)
13818 {
13819 fprintf (stderr,
13820 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13821 glyph - row->glyphs[TEXT_AREA],
13822 'S',
13823 glyph->charpos,
13824 (BUFFERP (glyph->object)
13825 ? 'B'
13826 : (STRINGP (glyph->object)
13827 ? 'S'
13828 : '-')),
13829 glyph->pixel_width,
13830 0,
13831 '.',
13832 glyph->face_id,
13833 glyph->left_box_line_p,
13834 glyph->right_box_line_p);
13835 }
13836 else if (glyph->type == IMAGE_GLYPH)
13837 {
13838 fprintf (stderr,
13839 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13840 glyph - row->glyphs[TEXT_AREA],
13841 'I',
13842 glyph->charpos,
13843 (BUFFERP (glyph->object)
13844 ? 'B'
13845 : (STRINGP (glyph->object)
13846 ? 'S'
13847 : '-')),
13848 glyph->pixel_width,
13849 glyph->u.img_id,
13850 '.',
13851 glyph->face_id,
13852 glyph->left_box_line_p,
13853 glyph->right_box_line_p);
13854 }
13855 }
13856
13857
13858 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
13859 GLYPHS 0 means don't show glyph contents.
13860 GLYPHS 1 means show glyphs in short form
13861 GLYPHS > 1 means show glyphs in long form. */
13862
13863 void
13864 dump_glyph_row (row, vpos, glyphs)
13865 struct glyph_row *row;
13866 int vpos, glyphs;
13867 {
13868 if (glyphs != 1)
13869 {
13870 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
13871 fprintf (stderr, "=======================================================================\n");
13872
13873 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
13874 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
13875 vpos,
13876 MATRIX_ROW_START_CHARPOS (row),
13877 MATRIX_ROW_END_CHARPOS (row),
13878 row->used[TEXT_AREA],
13879 row->contains_overlapping_glyphs_p,
13880 row->enabled_p,
13881 row->truncated_on_left_p,
13882 row->truncated_on_right_p,
13883 row->overlay_arrow_p,
13884 row->continued_p,
13885 MATRIX_ROW_CONTINUATION_LINE_P (row),
13886 row->displays_text_p,
13887 row->ends_at_zv_p,
13888 row->fill_line_p,
13889 row->ends_in_middle_of_char_p,
13890 row->starts_in_middle_of_char_p,
13891 row->mouse_face_p,
13892 row->x,
13893 row->y,
13894 row->pixel_width,
13895 row->height,
13896 row->visible_height,
13897 row->ascent,
13898 row->phys_ascent);
13899 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
13900 row->end.overlay_string_index,
13901 row->continuation_lines_width);
13902 fprintf (stderr, "%9d %5d\n",
13903 CHARPOS (row->start.string_pos),
13904 CHARPOS (row->end.string_pos));
13905 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
13906 row->end.dpvec_index);
13907 }
13908
13909 if (glyphs > 1)
13910 {
13911 int area;
13912
13913 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13914 {
13915 struct glyph *glyph = row->glyphs[area];
13916 struct glyph *glyph_end = glyph + row->used[area];
13917
13918 /* Glyph for a line end in text. */
13919 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
13920 ++glyph_end;
13921
13922 if (glyph < glyph_end)
13923 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
13924
13925 for (; glyph < glyph_end; ++glyph)
13926 dump_glyph (row, glyph, area);
13927 }
13928 }
13929 else if (glyphs == 1)
13930 {
13931 int area;
13932
13933 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13934 {
13935 char *s = (char *) alloca (row->used[area] + 1);
13936 int i;
13937
13938 for (i = 0; i < row->used[area]; ++i)
13939 {
13940 struct glyph *glyph = row->glyphs[area] + i;
13941 if (glyph->type == CHAR_GLYPH
13942 && glyph->u.ch < 0x80
13943 && glyph->u.ch >= ' ')
13944 s[i] = glyph->u.ch;
13945 else
13946 s[i] = '.';
13947 }
13948
13949 s[i] = '\0';
13950 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
13951 }
13952 }
13953 }
13954
13955
13956 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
13957 Sdump_glyph_matrix, 0, 1, "p",
13958 doc: /* Dump the current matrix of the selected window to stderr.
13959 Shows contents of glyph row structures. With non-nil
13960 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
13961 glyphs in short form, otherwise show glyphs in long form. */)
13962 (glyphs)
13963 Lisp_Object glyphs;
13964 {
13965 struct window *w = XWINDOW (selected_window);
13966 struct buffer *buffer = XBUFFER (w->buffer);
13967
13968 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
13969 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
13970 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
13971 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
13972 fprintf (stderr, "=============================================\n");
13973 dump_glyph_matrix (w->current_matrix,
13974 NILP (glyphs) ? 0 : XINT (glyphs));
13975 return Qnil;
13976 }
13977
13978
13979 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
13980 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
13981 ()
13982 {
13983 struct frame *f = XFRAME (selected_frame);
13984 dump_glyph_matrix (f->current_matrix, 1);
13985 return Qnil;
13986 }
13987
13988
13989 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
13990 doc: /* Dump glyph row ROW to stderr.
13991 GLYPH 0 means don't dump glyphs.
13992 GLYPH 1 means dump glyphs in short form.
13993 GLYPH > 1 or omitted means dump glyphs in long form. */)
13994 (row, glyphs)
13995 Lisp_Object row, glyphs;
13996 {
13997 struct glyph_matrix *matrix;
13998 int vpos;
13999
14000 CHECK_NUMBER (row);
14001 matrix = XWINDOW (selected_window)->current_matrix;
14002 vpos = XINT (row);
14003 if (vpos >= 0 && vpos < matrix->nrows)
14004 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14005 vpos,
14006 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14007 return Qnil;
14008 }
14009
14010
14011 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14012 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14013 GLYPH 0 means don't dump glyphs.
14014 GLYPH 1 means dump glyphs in short form.
14015 GLYPH > 1 or omitted means dump glyphs in long form. */)
14016 (row, glyphs)
14017 Lisp_Object row, glyphs;
14018 {
14019 struct frame *sf = SELECTED_FRAME ();
14020 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14021 int vpos;
14022
14023 CHECK_NUMBER (row);
14024 vpos = XINT (row);
14025 if (vpos >= 0 && vpos < m->nrows)
14026 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14027 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14028 return Qnil;
14029 }
14030
14031
14032 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14033 doc: /* Toggle tracing of redisplay.
14034 With ARG, turn tracing on if and only if ARG is positive. */)
14035 (arg)
14036 Lisp_Object arg;
14037 {
14038 if (NILP (arg))
14039 trace_redisplay_p = !trace_redisplay_p;
14040 else
14041 {
14042 arg = Fprefix_numeric_value (arg);
14043 trace_redisplay_p = XINT (arg) > 0;
14044 }
14045
14046 return Qnil;
14047 }
14048
14049
14050 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14051 doc: /* Like `format', but print result to stderr.
14052 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14053 (nargs, args)
14054 int nargs;
14055 Lisp_Object *args;
14056 {
14057 Lisp_Object s = Fformat (nargs, args);
14058 fprintf (stderr, "%s", SDATA (s));
14059 return Qnil;
14060 }
14061
14062 #endif /* GLYPH_DEBUG */
14063
14064
14065 \f
14066 /***********************************************************************
14067 Building Desired Matrix Rows
14068 ***********************************************************************/
14069
14070 /* Return a temporary glyph row holding the glyphs of an overlay
14071 arrow. Only used for non-window-redisplay windows. */
14072
14073 static struct glyph_row *
14074 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14075 struct window *w;
14076 Lisp_Object overlay_arrow_string;
14077 {
14078 struct frame *f = XFRAME (WINDOW_FRAME (w));
14079 struct buffer *buffer = XBUFFER (w->buffer);
14080 struct buffer *old = current_buffer;
14081 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14082 int arrow_len = SCHARS (overlay_arrow_string);
14083 const unsigned char *arrow_end = arrow_string + arrow_len;
14084 const unsigned char *p;
14085 struct it it;
14086 int multibyte_p;
14087 int n_glyphs_before;
14088
14089 set_buffer_temp (buffer);
14090 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14091 it.glyph_row->used[TEXT_AREA] = 0;
14092 SET_TEXT_POS (it.position, 0, 0);
14093
14094 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14095 p = arrow_string;
14096 while (p < arrow_end)
14097 {
14098 Lisp_Object face, ilisp;
14099
14100 /* Get the next character. */
14101 if (multibyte_p)
14102 it.c = string_char_and_length (p, arrow_len, &it.len);
14103 else
14104 it.c = *p, it.len = 1;
14105 p += it.len;
14106
14107 /* Get its face. */
14108 ilisp = make_number (p - arrow_string);
14109 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
14110 it.face_id = compute_char_face (f, it.c, face);
14111
14112 /* Compute its width, get its glyphs. */
14113 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14114 SET_TEXT_POS (it.position, -1, -1);
14115 PRODUCE_GLYPHS (&it);
14116
14117 /* If this character doesn't fit any more in the line, we have
14118 to remove some glyphs. */
14119 if (it.current_x > it.last_visible_x)
14120 {
14121 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14122 break;
14123 }
14124 }
14125
14126 set_buffer_temp (old);
14127 return it.glyph_row;
14128 }
14129
14130
14131 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14132 glyphs are only inserted for terminal frames since we can't really
14133 win with truncation glyphs when partially visible glyphs are
14134 involved. Which glyphs to insert is determined by
14135 produce_special_glyphs. */
14136
14137 static void
14138 insert_left_trunc_glyphs (it)
14139 struct it *it;
14140 {
14141 struct it truncate_it;
14142 struct glyph *from, *end, *to, *toend;
14143
14144 xassert (!FRAME_WINDOW_P (it->f));
14145
14146 /* Get the truncation glyphs. */
14147 truncate_it = *it;
14148 truncate_it.current_x = 0;
14149 truncate_it.face_id = DEFAULT_FACE_ID;
14150 truncate_it.glyph_row = &scratch_glyph_row;
14151 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14152 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14153 truncate_it.object = make_number (0);
14154 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14155
14156 /* Overwrite glyphs from IT with truncation glyphs. */
14157 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14158 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14159 to = it->glyph_row->glyphs[TEXT_AREA];
14160 toend = to + it->glyph_row->used[TEXT_AREA];
14161
14162 while (from < end)
14163 *to++ = *from++;
14164
14165 /* There may be padding glyphs left over. Overwrite them too. */
14166 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14167 {
14168 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14169 while (from < end)
14170 *to++ = *from++;
14171 }
14172
14173 if (to > toend)
14174 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14175 }
14176
14177
14178 /* Compute the pixel height and width of IT->glyph_row.
14179
14180 Most of the time, ascent and height of a display line will be equal
14181 to the max_ascent and max_height values of the display iterator
14182 structure. This is not the case if
14183
14184 1. We hit ZV without displaying anything. In this case, max_ascent
14185 and max_height will be zero.
14186
14187 2. We have some glyphs that don't contribute to the line height.
14188 (The glyph row flag contributes_to_line_height_p is for future
14189 pixmap extensions).
14190
14191 The first case is easily covered by using default values because in
14192 these cases, the line height does not really matter, except that it
14193 must not be zero. */
14194
14195 static void
14196 compute_line_metrics (it)
14197 struct it *it;
14198 {
14199 struct glyph_row *row = it->glyph_row;
14200 int area, i;
14201
14202 if (FRAME_WINDOW_P (it->f))
14203 {
14204 int i, min_y, max_y;
14205
14206 /* The line may consist of one space only, that was added to
14207 place the cursor on it. If so, the row's height hasn't been
14208 computed yet. */
14209 if (row->height == 0)
14210 {
14211 if (it->max_ascent + it->max_descent == 0)
14212 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14213 row->ascent = it->max_ascent;
14214 row->height = it->max_ascent + it->max_descent;
14215 row->phys_ascent = it->max_phys_ascent;
14216 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14217 }
14218
14219 /* Compute the width of this line. */
14220 row->pixel_width = row->x;
14221 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14222 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14223
14224 xassert (row->pixel_width >= 0);
14225 xassert (row->ascent >= 0 && row->height > 0);
14226
14227 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14228 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14229
14230 /* If first line's physical ascent is larger than its logical
14231 ascent, use the physical ascent, and make the row taller.
14232 This makes accented characters fully visible. */
14233 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14234 && row->phys_ascent > row->ascent)
14235 {
14236 row->height += row->phys_ascent - row->ascent;
14237 row->ascent = row->phys_ascent;
14238 }
14239
14240 /* Compute how much of the line is visible. */
14241 row->visible_height = row->height;
14242
14243 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14244 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14245
14246 if (row->y < min_y)
14247 row->visible_height -= min_y - row->y;
14248 if (row->y + row->height > max_y)
14249 row->visible_height -= row->y + row->height - max_y;
14250 }
14251 else
14252 {
14253 row->pixel_width = row->used[TEXT_AREA];
14254 if (row->continued_p)
14255 row->pixel_width -= it->continuation_pixel_width;
14256 else if (row->truncated_on_right_p)
14257 row->pixel_width -= it->truncation_pixel_width;
14258 row->ascent = row->phys_ascent = 0;
14259 row->height = row->phys_height = row->visible_height = 1;
14260 }
14261
14262 /* Compute a hash code for this row. */
14263 row->hash = 0;
14264 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14265 for (i = 0; i < row->used[area]; ++i)
14266 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14267 + row->glyphs[area][i].u.val
14268 + row->glyphs[area][i].face_id
14269 + row->glyphs[area][i].padding_p
14270 + (row->glyphs[area][i].type << 2));
14271
14272 it->max_ascent = it->max_descent = 0;
14273 it->max_phys_ascent = it->max_phys_descent = 0;
14274 }
14275
14276
14277 /* Append one space to the glyph row of iterator IT if doing a
14278 window-based redisplay. The space has the same face as
14279 IT->face_id. Value is non-zero if a space was added.
14280
14281 This function is called to make sure that there is always one glyph
14282 at the end of a glyph row that the cursor can be set on under
14283 window-systems. (If there weren't such a glyph we would not know
14284 how wide and tall a box cursor should be displayed).
14285
14286 At the same time this space let's a nicely handle clearing to the
14287 end of the line if the row ends in italic text. */
14288
14289 static int
14290 append_space_for_newline (it, default_face_p)
14291 struct it *it;
14292 int default_face_p;
14293 {
14294 if (FRAME_WINDOW_P (it->f))
14295 {
14296 int n = it->glyph_row->used[TEXT_AREA];
14297
14298 if (it->glyph_row->glyphs[TEXT_AREA] + n
14299 < it->glyph_row->glyphs[1 + TEXT_AREA])
14300 {
14301 /* Save some values that must not be changed.
14302 Must save IT->c and IT->len because otherwise
14303 ITERATOR_AT_END_P wouldn't work anymore after
14304 append_space_for_newline has been called. */
14305 enum display_element_type saved_what = it->what;
14306 int saved_c = it->c, saved_len = it->len;
14307 int saved_x = it->current_x;
14308 int saved_face_id = it->face_id;
14309 struct text_pos saved_pos;
14310 Lisp_Object saved_object;
14311 struct face *face;
14312
14313 saved_object = it->object;
14314 saved_pos = it->position;
14315
14316 it->what = IT_CHARACTER;
14317 bzero (&it->position, sizeof it->position);
14318 it->object = make_number (0);
14319 it->c = ' ';
14320 it->len = 1;
14321
14322 if (default_face_p)
14323 it->face_id = DEFAULT_FACE_ID;
14324 else if (it->face_before_selective_p)
14325 it->face_id = it->saved_face_id;
14326 face = FACE_FROM_ID (it->f, it->face_id);
14327 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14328
14329 PRODUCE_GLYPHS (it);
14330
14331 it->override_ascent = -1;
14332 it->constrain_row_ascent_descent_p = 0;
14333 it->current_x = saved_x;
14334 it->object = saved_object;
14335 it->position = saved_pos;
14336 it->what = saved_what;
14337 it->face_id = saved_face_id;
14338 it->len = saved_len;
14339 it->c = saved_c;
14340 return 1;
14341 }
14342 }
14343
14344 return 0;
14345 }
14346
14347
14348 /* Extend the face of the last glyph in the text area of IT->glyph_row
14349 to the end of the display line. Called from display_line.
14350 If the glyph row is empty, add a space glyph to it so that we
14351 know the face to draw. Set the glyph row flag fill_line_p. */
14352
14353 static void
14354 extend_face_to_end_of_line (it)
14355 struct it *it;
14356 {
14357 struct face *face;
14358 struct frame *f = it->f;
14359
14360 /* If line is already filled, do nothing. */
14361 if (it->current_x >= it->last_visible_x)
14362 return;
14363
14364 /* Face extension extends the background and box of IT->face_id
14365 to the end of the line. If the background equals the background
14366 of the frame, we don't have to do anything. */
14367 if (it->face_before_selective_p)
14368 face = FACE_FROM_ID (it->f, it->saved_face_id);
14369 else
14370 face = FACE_FROM_ID (f, it->face_id);
14371
14372 if (FRAME_WINDOW_P (f)
14373 && face->box == FACE_NO_BOX
14374 && face->background == FRAME_BACKGROUND_PIXEL (f)
14375 && !face->stipple)
14376 return;
14377
14378 /* Set the glyph row flag indicating that the face of the last glyph
14379 in the text area has to be drawn to the end of the text area. */
14380 it->glyph_row->fill_line_p = 1;
14381
14382 /* If current character of IT is not ASCII, make sure we have the
14383 ASCII face. This will be automatically undone the next time
14384 get_next_display_element returns a multibyte character. Note
14385 that the character will always be single byte in unibyte text. */
14386 if (!SINGLE_BYTE_CHAR_P (it->c))
14387 {
14388 it->face_id = FACE_FOR_CHAR (f, face, 0);
14389 }
14390
14391 if (FRAME_WINDOW_P (f))
14392 {
14393 /* If the row is empty, add a space with the current face of IT,
14394 so that we know which face to draw. */
14395 if (it->glyph_row->used[TEXT_AREA] == 0)
14396 {
14397 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14398 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14399 it->glyph_row->used[TEXT_AREA] = 1;
14400 }
14401 }
14402 else
14403 {
14404 /* Save some values that must not be changed. */
14405 int saved_x = it->current_x;
14406 struct text_pos saved_pos;
14407 Lisp_Object saved_object;
14408 enum display_element_type saved_what = it->what;
14409 int saved_face_id = it->face_id;
14410
14411 saved_object = it->object;
14412 saved_pos = it->position;
14413
14414 it->what = IT_CHARACTER;
14415 bzero (&it->position, sizeof it->position);
14416 it->object = make_number (0);
14417 it->c = ' ';
14418 it->len = 1;
14419 it->face_id = face->id;
14420
14421 PRODUCE_GLYPHS (it);
14422
14423 while (it->current_x <= it->last_visible_x)
14424 PRODUCE_GLYPHS (it);
14425
14426 /* Don't count these blanks really. It would let us insert a left
14427 truncation glyph below and make us set the cursor on them, maybe. */
14428 it->current_x = saved_x;
14429 it->object = saved_object;
14430 it->position = saved_pos;
14431 it->what = saved_what;
14432 it->face_id = saved_face_id;
14433 }
14434 }
14435
14436
14437 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14438 trailing whitespace. */
14439
14440 static int
14441 trailing_whitespace_p (charpos)
14442 int charpos;
14443 {
14444 int bytepos = CHAR_TO_BYTE (charpos);
14445 int c = 0;
14446
14447 while (bytepos < ZV_BYTE
14448 && (c = FETCH_CHAR (bytepos),
14449 c == ' ' || c == '\t'))
14450 ++bytepos;
14451
14452 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14453 {
14454 if (bytepos != PT_BYTE)
14455 return 1;
14456 }
14457 return 0;
14458 }
14459
14460
14461 /* Highlight trailing whitespace, if any, in ROW. */
14462
14463 void
14464 highlight_trailing_whitespace (f, row)
14465 struct frame *f;
14466 struct glyph_row *row;
14467 {
14468 int used = row->used[TEXT_AREA];
14469
14470 if (used)
14471 {
14472 struct glyph *start = row->glyphs[TEXT_AREA];
14473 struct glyph *glyph = start + used - 1;
14474
14475 /* Skip over glyphs inserted to display the cursor at the
14476 end of a line, for extending the face of the last glyph
14477 to the end of the line on terminals, and for truncation
14478 and continuation glyphs. */
14479 while (glyph >= start
14480 && glyph->type == CHAR_GLYPH
14481 && INTEGERP (glyph->object))
14482 --glyph;
14483
14484 /* If last glyph is a space or stretch, and it's trailing
14485 whitespace, set the face of all trailing whitespace glyphs in
14486 IT->glyph_row to `trailing-whitespace'. */
14487 if (glyph >= start
14488 && BUFFERP (glyph->object)
14489 && (glyph->type == STRETCH_GLYPH
14490 || (glyph->type == CHAR_GLYPH
14491 && glyph->u.ch == ' '))
14492 && trailing_whitespace_p (glyph->charpos))
14493 {
14494 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
14495
14496 while (glyph >= start
14497 && BUFFERP (glyph->object)
14498 && (glyph->type == STRETCH_GLYPH
14499 || (glyph->type == CHAR_GLYPH
14500 && glyph->u.ch == ' ')))
14501 (glyph--)->face_id = face_id;
14502 }
14503 }
14504 }
14505
14506
14507 /* Value is non-zero if glyph row ROW in window W should be
14508 used to hold the cursor. */
14509
14510 static int
14511 cursor_row_p (w, row)
14512 struct window *w;
14513 struct glyph_row *row;
14514 {
14515 int cursor_row_p = 1;
14516
14517 if (PT == MATRIX_ROW_END_CHARPOS (row))
14518 {
14519 /* If the row ends with a newline from a string, we don't want
14520 the cursor there (if the row is continued it doesn't end in a
14521 newline). */
14522 if (CHARPOS (row->end.string_pos) >= 0
14523 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14524 cursor_row_p = row->continued_p;
14525
14526 /* If the row ends at ZV, display the cursor at the end of that
14527 row instead of at the start of the row below. */
14528 else if (row->ends_at_zv_p)
14529 cursor_row_p = 1;
14530 else
14531 cursor_row_p = 0;
14532 }
14533
14534 return cursor_row_p;
14535 }
14536
14537
14538 /* Construct the glyph row IT->glyph_row in the desired matrix of
14539 IT->w from text at the current position of IT. See dispextern.h
14540 for an overview of struct it. Value is non-zero if
14541 IT->glyph_row displays text, as opposed to a line displaying ZV
14542 only. */
14543
14544 static int
14545 display_line (it)
14546 struct it *it;
14547 {
14548 struct glyph_row *row = it->glyph_row;
14549 int overlay_arrow_bitmap;
14550 Lisp_Object overlay_arrow_string;
14551
14552 /* We always start displaying at hpos zero even if hscrolled. */
14553 xassert (it->hpos == 0 && it->current_x == 0);
14554
14555 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14556 >= it->w->desired_matrix->nrows)
14557 {
14558 it->w->nrows_scale_factor++;
14559 fonts_changed_p = 1;
14560 return 0;
14561 }
14562
14563 /* Is IT->w showing the region? */
14564 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
14565
14566 /* Clear the result glyph row and enable it. */
14567 prepare_desired_row (row);
14568
14569 row->y = it->current_y;
14570 row->start = it->start;
14571 row->continuation_lines_width = it->continuation_lines_width;
14572 row->displays_text_p = 1;
14573 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14574 it->starts_in_middle_of_char_p = 0;
14575
14576 /* Arrange the overlays nicely for our purposes. Usually, we call
14577 display_line on only one line at a time, in which case this
14578 can't really hurt too much, or we call it on lines which appear
14579 one after another in the buffer, in which case all calls to
14580 recenter_overlay_lists but the first will be pretty cheap. */
14581 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14582
14583 /* Move over display elements that are not visible because we are
14584 hscrolled. This may stop at an x-position < IT->first_visible_x
14585 if the first glyph is partially visible or if we hit a line end. */
14586 if (it->current_x < it->first_visible_x)
14587 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14588 MOVE_TO_POS | MOVE_TO_X);
14589
14590 /* Get the initial row height. This is either the height of the
14591 text hscrolled, if there is any, or zero. */
14592 row->ascent = it->max_ascent;
14593 row->height = it->max_ascent + it->max_descent;
14594 row->phys_ascent = it->max_phys_ascent;
14595 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14596
14597 /* Loop generating characters. The loop is left with IT on the next
14598 character to display. */
14599 while (1)
14600 {
14601 int n_glyphs_before, hpos_before, x_before;
14602 int x, i, nglyphs;
14603 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
14604
14605 /* Retrieve the next thing to display. Value is zero if end of
14606 buffer reached. */
14607 if (!get_next_display_element (it))
14608 {
14609 /* Maybe add a space at the end of this line that is used to
14610 display the cursor there under X. Set the charpos of the
14611 first glyph of blank lines not corresponding to any text
14612 to -1. */
14613 #ifdef HAVE_WINDOW_SYSTEM
14614 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14615 row->exact_window_width_line_p = 1;
14616 else
14617 #endif /* HAVE_WINDOW_SYSTEM */
14618 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
14619 || row->used[TEXT_AREA] == 0)
14620 {
14621 row->glyphs[TEXT_AREA]->charpos = -1;
14622 row->displays_text_p = 0;
14623
14624 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
14625 && (!MINI_WINDOW_P (it->w)
14626 || (minibuf_level && EQ (it->window, minibuf_window))))
14627 row->indicate_empty_line_p = 1;
14628 }
14629
14630 it->continuation_lines_width = 0;
14631 row->ends_at_zv_p = 1;
14632 break;
14633 }
14634
14635 /* Now, get the metrics of what we want to display. This also
14636 generates glyphs in `row' (which is IT->glyph_row). */
14637 n_glyphs_before = row->used[TEXT_AREA];
14638 x = it->current_x;
14639
14640 /* Remember the line height so far in case the next element doesn't
14641 fit on the line. */
14642 if (!it->truncate_lines_p)
14643 {
14644 ascent = it->max_ascent;
14645 descent = it->max_descent;
14646 phys_ascent = it->max_phys_ascent;
14647 phys_descent = it->max_phys_descent;
14648 }
14649
14650 PRODUCE_GLYPHS (it);
14651
14652 /* If this display element was in marginal areas, continue with
14653 the next one. */
14654 if (it->area != TEXT_AREA)
14655 {
14656 row->ascent = max (row->ascent, it->max_ascent);
14657 row->height = max (row->height, it->max_ascent + it->max_descent);
14658 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14659 row->phys_height = max (row->phys_height,
14660 it->max_phys_ascent + it->max_phys_descent);
14661 set_iterator_to_next (it, 1);
14662 continue;
14663 }
14664
14665 /* Does the display element fit on the line? If we truncate
14666 lines, we should draw past the right edge of the window. If
14667 we don't truncate, we want to stop so that we can display the
14668 continuation glyph before the right margin. If lines are
14669 continued, there are two possible strategies for characters
14670 resulting in more than 1 glyph (e.g. tabs): Display as many
14671 glyphs as possible in this line and leave the rest for the
14672 continuation line, or display the whole element in the next
14673 line. Original redisplay did the former, so we do it also. */
14674 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14675 hpos_before = it->hpos;
14676 x_before = x;
14677
14678 if (/* Not a newline. */
14679 nglyphs > 0
14680 /* Glyphs produced fit entirely in the line. */
14681 && it->current_x < it->last_visible_x)
14682 {
14683 it->hpos += nglyphs;
14684 row->ascent = max (row->ascent, it->max_ascent);
14685 row->height = max (row->height, it->max_ascent + it->max_descent);
14686 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14687 row->phys_height = max (row->phys_height,
14688 it->max_phys_ascent + it->max_phys_descent);
14689 if (it->current_x - it->pixel_width < it->first_visible_x)
14690 row->x = x - it->first_visible_x;
14691 }
14692 else
14693 {
14694 int new_x;
14695 struct glyph *glyph;
14696
14697 for (i = 0; i < nglyphs; ++i, x = new_x)
14698 {
14699 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14700 new_x = x + glyph->pixel_width;
14701
14702 if (/* Lines are continued. */
14703 !it->truncate_lines_p
14704 && (/* Glyph doesn't fit on the line. */
14705 new_x > it->last_visible_x
14706 /* Or it fits exactly on a window system frame. */
14707 || (new_x == it->last_visible_x
14708 && FRAME_WINDOW_P (it->f))))
14709 {
14710 /* End of a continued line. */
14711
14712 if (it->hpos == 0
14713 || (new_x == it->last_visible_x
14714 && FRAME_WINDOW_P (it->f)))
14715 {
14716 /* Current glyph is the only one on the line or
14717 fits exactly on the line. We must continue
14718 the line because we can't draw the cursor
14719 after the glyph. */
14720 row->continued_p = 1;
14721 it->current_x = new_x;
14722 it->continuation_lines_width += new_x;
14723 ++it->hpos;
14724 if (i == nglyphs - 1)
14725 {
14726 set_iterator_to_next (it, 1);
14727 #ifdef HAVE_WINDOW_SYSTEM
14728 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14729 {
14730 if (!get_next_display_element (it))
14731 {
14732 row->exact_window_width_line_p = 1;
14733 it->continuation_lines_width = 0;
14734 row->continued_p = 0;
14735 row->ends_at_zv_p = 1;
14736 }
14737 else if (ITERATOR_AT_END_OF_LINE_P (it))
14738 {
14739 row->continued_p = 0;
14740 row->exact_window_width_line_p = 1;
14741 }
14742 }
14743 #endif /* HAVE_WINDOW_SYSTEM */
14744 }
14745 }
14746 else if (CHAR_GLYPH_PADDING_P (*glyph)
14747 && !FRAME_WINDOW_P (it->f))
14748 {
14749 /* A padding glyph that doesn't fit on this line.
14750 This means the whole character doesn't fit
14751 on the line. */
14752 row->used[TEXT_AREA] = n_glyphs_before;
14753
14754 /* Fill the rest of the row with continuation
14755 glyphs like in 20.x. */
14756 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
14757 < row->glyphs[1 + TEXT_AREA])
14758 produce_special_glyphs (it, IT_CONTINUATION);
14759
14760 row->continued_p = 1;
14761 it->current_x = x_before;
14762 it->continuation_lines_width += x_before;
14763
14764 /* Restore the height to what it was before the
14765 element not fitting on the line. */
14766 it->max_ascent = ascent;
14767 it->max_descent = descent;
14768 it->max_phys_ascent = phys_ascent;
14769 it->max_phys_descent = phys_descent;
14770 }
14771 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
14772 {
14773 /* A TAB that extends past the right edge of the
14774 window. This produces a single glyph on
14775 window system frames. We leave the glyph in
14776 this row and let it fill the row, but don't
14777 consume the TAB. */
14778 it->continuation_lines_width += it->last_visible_x;
14779 row->ends_in_middle_of_char_p = 1;
14780 row->continued_p = 1;
14781 glyph->pixel_width = it->last_visible_x - x;
14782 it->starts_in_middle_of_char_p = 1;
14783 }
14784 else
14785 {
14786 /* Something other than a TAB that draws past
14787 the right edge of the window. Restore
14788 positions to values before the element. */
14789 row->used[TEXT_AREA] = n_glyphs_before + i;
14790
14791 /* Display continuation glyphs. */
14792 if (!FRAME_WINDOW_P (it->f))
14793 produce_special_glyphs (it, IT_CONTINUATION);
14794 row->continued_p = 1;
14795
14796 it->continuation_lines_width += x;
14797
14798 if (nglyphs > 1 && i > 0)
14799 {
14800 row->ends_in_middle_of_char_p = 1;
14801 it->starts_in_middle_of_char_p = 1;
14802 }
14803
14804 /* Restore the height to what it was before the
14805 element not fitting on the line. */
14806 it->max_ascent = ascent;
14807 it->max_descent = descent;
14808 it->max_phys_ascent = phys_ascent;
14809 it->max_phys_descent = phys_descent;
14810 }
14811
14812 break;
14813 }
14814 else if (new_x > it->first_visible_x)
14815 {
14816 /* Increment number of glyphs actually displayed. */
14817 ++it->hpos;
14818
14819 if (x < it->first_visible_x)
14820 /* Glyph is partially visible, i.e. row starts at
14821 negative X position. */
14822 row->x = x - it->first_visible_x;
14823 }
14824 else
14825 {
14826 /* Glyph is completely off the left margin of the
14827 window. This should not happen because of the
14828 move_it_in_display_line at the start of this
14829 function, unless the text display area of the
14830 window is empty. */
14831 xassert (it->first_visible_x <= it->last_visible_x);
14832 }
14833 }
14834
14835 row->ascent = max (row->ascent, it->max_ascent);
14836 row->height = max (row->height, it->max_ascent + it->max_descent);
14837 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14838 row->phys_height = max (row->phys_height,
14839 it->max_phys_ascent + it->max_phys_descent);
14840
14841 /* End of this display line if row is continued. */
14842 if (row->continued_p || row->ends_at_zv_p)
14843 break;
14844 }
14845
14846 at_end_of_line:
14847 /* Is this a line end? If yes, we're also done, after making
14848 sure that a non-default face is extended up to the right
14849 margin of the window. */
14850 if (ITERATOR_AT_END_OF_LINE_P (it))
14851 {
14852 int used_before = row->used[TEXT_AREA];
14853
14854 row->ends_in_newline_from_string_p = STRINGP (it->object);
14855
14856 #ifdef HAVE_WINDOW_SYSTEM
14857 /* Add a space at the end of the line that is used to
14858 display the cursor there. */
14859 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14860 append_space_for_newline (it, 0);
14861 #endif /* HAVE_WINDOW_SYSTEM */
14862
14863 /* Extend the face to the end of the line. */
14864 extend_face_to_end_of_line (it);
14865
14866 /* Make sure we have the position. */
14867 if (used_before == 0)
14868 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
14869
14870 /* Consume the line end. This skips over invisible lines. */
14871 set_iterator_to_next (it, 1);
14872 it->continuation_lines_width = 0;
14873 break;
14874 }
14875
14876 /* Proceed with next display element. Note that this skips
14877 over lines invisible because of selective display. */
14878 set_iterator_to_next (it, 1);
14879
14880 /* If we truncate lines, we are done when the last displayed
14881 glyphs reach past the right margin of the window. */
14882 if (it->truncate_lines_p
14883 && (FRAME_WINDOW_P (it->f)
14884 ? (it->current_x >= it->last_visible_x)
14885 : (it->current_x > it->last_visible_x)))
14886 {
14887 /* Maybe add truncation glyphs. */
14888 if (!FRAME_WINDOW_P (it->f))
14889 {
14890 int i, n;
14891
14892 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14893 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14894 break;
14895
14896 for (n = row->used[TEXT_AREA]; i < n; ++i)
14897 {
14898 row->used[TEXT_AREA] = i;
14899 produce_special_glyphs (it, IT_TRUNCATION);
14900 }
14901 }
14902 #ifdef HAVE_WINDOW_SYSTEM
14903 else
14904 {
14905 /* Don't truncate if we can overflow newline into fringe. */
14906 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14907 {
14908 if (!get_next_display_element (it))
14909 {
14910 #ifdef HAVE_WINDOW_SYSTEM
14911 it->continuation_lines_width = 0;
14912 row->ends_at_zv_p = 1;
14913 row->exact_window_width_line_p = 1;
14914 break;
14915 #endif /* HAVE_WINDOW_SYSTEM */
14916 }
14917 if (ITERATOR_AT_END_OF_LINE_P (it))
14918 {
14919 row->exact_window_width_line_p = 1;
14920 goto at_end_of_line;
14921 }
14922 }
14923 }
14924 #endif /* HAVE_WINDOW_SYSTEM */
14925
14926 row->truncated_on_right_p = 1;
14927 it->continuation_lines_width = 0;
14928 reseat_at_next_visible_line_start (it, 0);
14929 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
14930 it->hpos = hpos_before;
14931 it->current_x = x_before;
14932 break;
14933 }
14934 }
14935
14936 /* If line is not empty and hscrolled, maybe insert truncation glyphs
14937 at the left window margin. */
14938 if (it->first_visible_x
14939 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
14940 {
14941 if (!FRAME_WINDOW_P (it->f))
14942 insert_left_trunc_glyphs (it);
14943 row->truncated_on_left_p = 1;
14944 }
14945
14946 /* If the start of this line is the overlay arrow-position, then
14947 mark this glyph row as the one containing the overlay arrow.
14948 This is clearly a mess with variable size fonts. It would be
14949 better to let it be displayed like cursors under X. */
14950 if (! overlay_arrow_seen
14951 && (overlay_arrow_string
14952 = overlay_arrow_at_row (it->f, row, &overlay_arrow_bitmap),
14953 !NILP (overlay_arrow_string)))
14954 {
14955 /* Overlay arrow in window redisplay is a fringe bitmap. */
14956 if (!FRAME_WINDOW_P (it->f))
14957 {
14958 struct glyph_row *arrow_row
14959 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
14960 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
14961 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
14962 struct glyph *p = row->glyphs[TEXT_AREA];
14963 struct glyph *p2, *end;
14964
14965 /* Copy the arrow glyphs. */
14966 while (glyph < arrow_end)
14967 *p++ = *glyph++;
14968
14969 /* Throw away padding glyphs. */
14970 p2 = p;
14971 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
14972 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
14973 ++p2;
14974 if (p2 > p)
14975 {
14976 while (p2 < end)
14977 *p++ = *p2++;
14978 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
14979 }
14980 }
14981
14982 overlay_arrow_seen = 1;
14983 it->w->overlay_arrow_bitmap = overlay_arrow_bitmap;
14984 row->overlay_arrow_p = 1;
14985 }
14986
14987 /* Compute pixel dimensions of this line. */
14988 compute_line_metrics (it);
14989
14990 /* Remember the position at which this line ends. */
14991 row->end = it->current;
14992
14993 /* Save fringe bitmaps in this row. */
14994 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
14995 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
14996 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
14997 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
14998
14999 it->left_user_fringe_bitmap = 0;
15000 it->left_user_fringe_face_id = 0;
15001 it->right_user_fringe_bitmap = 0;
15002 it->right_user_fringe_face_id = 0;
15003
15004 /* Maybe set the cursor. */
15005 if (it->w->cursor.vpos < 0
15006 && PT >= MATRIX_ROW_START_CHARPOS (row)
15007 && PT <= MATRIX_ROW_END_CHARPOS (row)
15008 && cursor_row_p (it->w, row))
15009 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15010
15011 /* Highlight trailing whitespace. */
15012 if (!NILP (Vshow_trailing_whitespace))
15013 highlight_trailing_whitespace (it->f, it->glyph_row);
15014
15015 /* Prepare for the next line. This line starts horizontally at (X
15016 HPOS) = (0 0). Vertical positions are incremented. As a
15017 convenience for the caller, IT->glyph_row is set to the next
15018 row to be used. */
15019 it->current_x = it->hpos = 0;
15020 it->current_y += row->height;
15021 ++it->vpos;
15022 ++it->glyph_row;
15023 it->start = it->current;
15024 return row->displays_text_p;
15025 }
15026
15027
15028 \f
15029 /***********************************************************************
15030 Menu Bar
15031 ***********************************************************************/
15032
15033 /* Redisplay the menu bar in the frame for window W.
15034
15035 The menu bar of X frames that don't have X toolkit support is
15036 displayed in a special window W->frame->menu_bar_window.
15037
15038 The menu bar of terminal frames is treated specially as far as
15039 glyph matrices are concerned. Menu bar lines are not part of
15040 windows, so the update is done directly on the frame matrix rows
15041 for the menu bar. */
15042
15043 static void
15044 display_menu_bar (w)
15045 struct window *w;
15046 {
15047 struct frame *f = XFRAME (WINDOW_FRAME (w));
15048 struct it it;
15049 Lisp_Object items;
15050 int i;
15051
15052 /* Don't do all this for graphical frames. */
15053 #ifdef HAVE_NTGUI
15054 if (!NILP (Vwindow_system))
15055 return;
15056 #endif
15057 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15058 if (FRAME_X_P (f))
15059 return;
15060 #endif
15061 #ifdef MAC_OS
15062 if (FRAME_MAC_P (f))
15063 return;
15064 #endif
15065
15066 #ifdef USE_X_TOOLKIT
15067 xassert (!FRAME_WINDOW_P (f));
15068 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
15069 it.first_visible_x = 0;
15070 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15071 #else /* not USE_X_TOOLKIT */
15072 if (FRAME_WINDOW_P (f))
15073 {
15074 /* Menu bar lines are displayed in the desired matrix of the
15075 dummy window menu_bar_window. */
15076 struct window *menu_w;
15077 xassert (WINDOWP (f->menu_bar_window));
15078 menu_w = XWINDOW (f->menu_bar_window);
15079 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
15080 MENU_FACE_ID);
15081 it.first_visible_x = 0;
15082 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15083 }
15084 else
15085 {
15086 /* This is a TTY frame, i.e. character hpos/vpos are used as
15087 pixel x/y. */
15088 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
15089 MENU_FACE_ID);
15090 it.first_visible_x = 0;
15091 it.last_visible_x = FRAME_COLS (f);
15092 }
15093 #endif /* not USE_X_TOOLKIT */
15094
15095 if (! mode_line_inverse_video)
15096 /* Force the menu-bar to be displayed in the default face. */
15097 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15098
15099 /* Clear all rows of the menu bar. */
15100 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
15101 {
15102 struct glyph_row *row = it.glyph_row + i;
15103 clear_glyph_row (row);
15104 row->enabled_p = 1;
15105 row->full_width_p = 1;
15106 }
15107
15108 /* Display all items of the menu bar. */
15109 items = FRAME_MENU_BAR_ITEMS (it.f);
15110 for (i = 0; i < XVECTOR (items)->size; i += 4)
15111 {
15112 Lisp_Object string;
15113
15114 /* Stop at nil string. */
15115 string = AREF (items, i + 1);
15116 if (NILP (string))
15117 break;
15118
15119 /* Remember where item was displayed. */
15120 AREF (items, i + 3) = make_number (it.hpos);
15121
15122 /* Display the item, pad with one space. */
15123 if (it.current_x < it.last_visible_x)
15124 display_string (NULL, string, Qnil, 0, 0, &it,
15125 SCHARS (string) + 1, 0, 0, -1);
15126 }
15127
15128 /* Fill out the line with spaces. */
15129 if (it.current_x < it.last_visible_x)
15130 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15131
15132 /* Compute the total height of the lines. */
15133 compute_line_metrics (&it);
15134 }
15135
15136
15137 \f
15138 /***********************************************************************
15139 Mode Line
15140 ***********************************************************************/
15141
15142 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15143 FORCE is non-zero, redisplay mode lines unconditionally.
15144 Otherwise, redisplay only mode lines that are garbaged. Value is
15145 the number of windows whose mode lines were redisplayed. */
15146
15147 static int
15148 redisplay_mode_lines (window, force)
15149 Lisp_Object window;
15150 int force;
15151 {
15152 int nwindows = 0;
15153
15154 while (!NILP (window))
15155 {
15156 struct window *w = XWINDOW (window);
15157
15158 if (WINDOWP (w->hchild))
15159 nwindows += redisplay_mode_lines (w->hchild, force);
15160 else if (WINDOWP (w->vchild))
15161 nwindows += redisplay_mode_lines (w->vchild, force);
15162 else if (force
15163 || FRAME_GARBAGED_P (XFRAME (w->frame))
15164 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15165 {
15166 struct text_pos lpoint;
15167 struct buffer *old = current_buffer;
15168
15169 /* Set the window's buffer for the mode line display. */
15170 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15171 set_buffer_internal_1 (XBUFFER (w->buffer));
15172
15173 /* Point refers normally to the selected window. For any
15174 other window, set up appropriate value. */
15175 if (!EQ (window, selected_window))
15176 {
15177 struct text_pos pt;
15178
15179 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15180 if (CHARPOS (pt) < BEGV)
15181 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15182 else if (CHARPOS (pt) > (ZV - 1))
15183 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15184 else
15185 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15186 }
15187
15188 /* Display mode lines. */
15189 clear_glyph_matrix (w->desired_matrix);
15190 if (display_mode_lines (w))
15191 {
15192 ++nwindows;
15193 w->must_be_updated_p = 1;
15194 }
15195
15196 /* Restore old settings. */
15197 set_buffer_internal_1 (old);
15198 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15199 }
15200
15201 window = w->next;
15202 }
15203
15204 return nwindows;
15205 }
15206
15207
15208 /* Display the mode and/or top line of window W. Value is the number
15209 of mode lines displayed. */
15210
15211 static int
15212 display_mode_lines (w)
15213 struct window *w;
15214 {
15215 Lisp_Object old_selected_window, old_selected_frame;
15216 int n = 0;
15217
15218 old_selected_frame = selected_frame;
15219 selected_frame = w->frame;
15220 old_selected_window = selected_window;
15221 XSETWINDOW (selected_window, w);
15222
15223 /* These will be set while the mode line specs are processed. */
15224 line_number_displayed = 0;
15225 w->column_number_displayed = Qnil;
15226
15227 if (WINDOW_WANTS_MODELINE_P (w))
15228 {
15229 struct window *sel_w = XWINDOW (old_selected_window);
15230
15231 /* Select mode line face based on the real selected window. */
15232 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15233 current_buffer->mode_line_format);
15234 ++n;
15235 }
15236
15237 if (WINDOW_WANTS_HEADER_LINE_P (w))
15238 {
15239 display_mode_line (w, HEADER_LINE_FACE_ID,
15240 current_buffer->header_line_format);
15241 ++n;
15242 }
15243
15244 selected_frame = old_selected_frame;
15245 selected_window = old_selected_window;
15246 return n;
15247 }
15248
15249
15250 /* Display mode or top line of window W. FACE_ID specifies which line
15251 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15252 FORMAT is the mode line format to display. Value is the pixel
15253 height of the mode line displayed. */
15254
15255 static int
15256 display_mode_line (w, face_id, format)
15257 struct window *w;
15258 enum face_id face_id;
15259 Lisp_Object format;
15260 {
15261 struct it it;
15262 struct face *face;
15263
15264 init_iterator (&it, w, -1, -1, NULL, face_id);
15265 prepare_desired_row (it.glyph_row);
15266
15267 it.glyph_row->mode_line_p = 1;
15268
15269 if (! mode_line_inverse_video)
15270 /* Force the mode-line to be displayed in the default face. */
15271 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15272
15273 /* Temporarily make frame's keyboard the current kboard so that
15274 kboard-local variables in the mode_line_format will get the right
15275 values. */
15276 push_frame_kboard (it.f);
15277 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15278 pop_frame_kboard ();
15279
15280 /* Fill up with spaces. */
15281 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15282
15283 compute_line_metrics (&it);
15284 it.glyph_row->full_width_p = 1;
15285 it.glyph_row->continued_p = 0;
15286 it.glyph_row->truncated_on_left_p = 0;
15287 it.glyph_row->truncated_on_right_p = 0;
15288
15289 /* Make a 3D mode-line have a shadow at its right end. */
15290 face = FACE_FROM_ID (it.f, face_id);
15291 extend_face_to_end_of_line (&it);
15292 if (face->box != FACE_NO_BOX)
15293 {
15294 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15295 + it.glyph_row->used[TEXT_AREA] - 1);
15296 last->right_box_line_p = 1;
15297 }
15298
15299 return it.glyph_row->height;
15300 }
15301
15302 /* Alist that caches the results of :propertize.
15303 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
15304 Lisp_Object mode_line_proptrans_alist;
15305
15306 /* List of strings making up the mode-line. */
15307 Lisp_Object mode_line_string_list;
15308
15309 /* Base face property when building propertized mode line string. */
15310 static Lisp_Object mode_line_string_face;
15311 static Lisp_Object mode_line_string_face_prop;
15312
15313
15314 /* Contribute ELT to the mode line for window IT->w. How it
15315 translates into text depends on its data type.
15316
15317 IT describes the display environment in which we display, as usual.
15318
15319 DEPTH is the depth in recursion. It is used to prevent
15320 infinite recursion here.
15321
15322 FIELD_WIDTH is the number of characters the display of ELT should
15323 occupy in the mode line, and PRECISION is the maximum number of
15324 characters to display from ELT's representation. See
15325 display_string for details.
15326
15327 Returns the hpos of the end of the text generated by ELT.
15328
15329 PROPS is a property list to add to any string we encounter.
15330
15331 If RISKY is nonzero, remove (disregard) any properties in any string
15332 we encounter, and ignore :eval and :propertize.
15333
15334 If the global variable `frame_title_ptr' is non-NULL, then the output
15335 is passed to `store_frame_title' instead of `display_string'. */
15336
15337 static int
15338 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15339 struct it *it;
15340 int depth;
15341 int field_width, precision;
15342 Lisp_Object elt, props;
15343 int risky;
15344 {
15345 int n = 0, field, prec;
15346 int literal = 0;
15347
15348 tail_recurse:
15349 if (depth > 100)
15350 elt = build_string ("*too-deep*");
15351
15352 depth++;
15353
15354 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15355 {
15356 case Lisp_String:
15357 {
15358 /* A string: output it and check for %-constructs within it. */
15359 unsigned char c;
15360 const unsigned char *this, *lisp_string;
15361
15362 if (!NILP (props) || risky)
15363 {
15364 Lisp_Object oprops, aelt;
15365 oprops = Ftext_properties_at (make_number (0), elt);
15366
15367 /* If the starting string's properties are not what
15368 we want, translate the string. Also, if the string
15369 is risky, do that anyway. */
15370
15371 if (NILP (Fequal (props, oprops)) || risky)
15372 {
15373 /* If the starting string has properties,
15374 merge the specified ones onto the existing ones. */
15375 if (! NILP (oprops) && !risky)
15376 {
15377 Lisp_Object tem;
15378
15379 oprops = Fcopy_sequence (oprops);
15380 tem = props;
15381 while (CONSP (tem))
15382 {
15383 oprops = Fplist_put (oprops, XCAR (tem),
15384 XCAR (XCDR (tem)));
15385 tem = XCDR (XCDR (tem));
15386 }
15387 props = oprops;
15388 }
15389
15390 aelt = Fassoc (elt, mode_line_proptrans_alist);
15391 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15392 {
15393 mode_line_proptrans_alist
15394 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15395 elt = XCAR (aelt);
15396 }
15397 else
15398 {
15399 Lisp_Object tem;
15400
15401 elt = Fcopy_sequence (elt);
15402 Fset_text_properties (make_number (0), Flength (elt),
15403 props, elt);
15404 /* Add this item to mode_line_proptrans_alist. */
15405 mode_line_proptrans_alist
15406 = Fcons (Fcons (elt, props),
15407 mode_line_proptrans_alist);
15408 /* Truncate mode_line_proptrans_alist
15409 to at most 50 elements. */
15410 tem = Fnthcdr (make_number (50),
15411 mode_line_proptrans_alist);
15412 if (! NILP (tem))
15413 XSETCDR (tem, Qnil);
15414 }
15415 }
15416 }
15417
15418 this = SDATA (elt);
15419 lisp_string = this;
15420
15421 if (literal)
15422 {
15423 prec = precision - n;
15424 if (frame_title_ptr)
15425 n += store_frame_title (SDATA (elt), -1, prec);
15426 else if (!NILP (mode_line_string_list))
15427 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15428 else
15429 n += display_string (NULL, elt, Qnil, 0, 0, it,
15430 0, prec, 0, STRING_MULTIBYTE (elt));
15431
15432 break;
15433 }
15434
15435 while ((precision <= 0 || n < precision)
15436 && *this
15437 && (frame_title_ptr
15438 || !NILP (mode_line_string_list)
15439 || it->current_x < it->last_visible_x))
15440 {
15441 const unsigned char *last = this;
15442
15443 /* Advance to end of string or next format specifier. */
15444 while ((c = *this++) != '\0' && c != '%')
15445 ;
15446
15447 if (this - 1 != last)
15448 {
15449 /* Output to end of string or up to '%'. Field width
15450 is length of string. Don't output more than
15451 PRECISION allows us. */
15452 --this;
15453
15454 prec = chars_in_text (last, this - last);
15455 if (precision > 0 && prec > precision - n)
15456 prec = precision - n;
15457
15458 if (frame_title_ptr)
15459 n += store_frame_title (last, 0, prec);
15460 else if (!NILP (mode_line_string_list))
15461 {
15462 int bytepos = last - lisp_string;
15463 int charpos = string_byte_to_char (elt, bytepos);
15464 n += store_mode_line_string (NULL,
15465 Fsubstring (elt, make_number (charpos),
15466 make_number (charpos + prec)),
15467 0, 0, 0, Qnil);
15468 }
15469 else
15470 {
15471 int bytepos = last - lisp_string;
15472 int charpos = string_byte_to_char (elt, bytepos);
15473 n += display_string (NULL, elt, Qnil, 0, charpos,
15474 it, 0, prec, 0,
15475 STRING_MULTIBYTE (elt));
15476 }
15477 }
15478 else /* c == '%' */
15479 {
15480 const unsigned char *percent_position = this;
15481
15482 /* Get the specified minimum width. Zero means
15483 don't pad. */
15484 field = 0;
15485 while ((c = *this++) >= '0' && c <= '9')
15486 field = field * 10 + c - '0';
15487
15488 /* Don't pad beyond the total padding allowed. */
15489 if (field_width - n > 0 && field > field_width - n)
15490 field = field_width - n;
15491
15492 /* Note that either PRECISION <= 0 or N < PRECISION. */
15493 prec = precision - n;
15494
15495 if (c == 'M')
15496 n += display_mode_element (it, depth, field, prec,
15497 Vglobal_mode_string, props,
15498 risky);
15499 else if (c != 0)
15500 {
15501 int multibyte;
15502 int bytepos, charpos;
15503 unsigned char *spec;
15504
15505 bytepos = percent_position - lisp_string;
15506 charpos = (STRING_MULTIBYTE (elt)
15507 ? string_byte_to_char (elt, bytepos)
15508 : bytepos);
15509
15510 spec
15511 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15512
15513 if (frame_title_ptr)
15514 n += store_frame_title (spec, field, prec);
15515 else if (!NILP (mode_line_string_list))
15516 {
15517 int len = strlen (spec);
15518 Lisp_Object tem = make_string (spec, len);
15519 props = Ftext_properties_at (make_number (charpos), elt);
15520 /* Should only keep face property in props */
15521 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15522 }
15523 else
15524 {
15525 int nglyphs_before, nwritten;
15526
15527 nglyphs_before = it->glyph_row->used[TEXT_AREA];
15528 nwritten = display_string (spec, Qnil, elt,
15529 charpos, 0, it,
15530 field, prec, 0,
15531 multibyte);
15532
15533 /* Assign to the glyphs written above the
15534 string where the `%x' came from, position
15535 of the `%'. */
15536 if (nwritten > 0)
15537 {
15538 struct glyph *glyph
15539 = (it->glyph_row->glyphs[TEXT_AREA]
15540 + nglyphs_before);
15541 int i;
15542
15543 for (i = 0; i < nwritten; ++i)
15544 {
15545 glyph[i].object = elt;
15546 glyph[i].charpos = charpos;
15547 }
15548
15549 n += nwritten;
15550 }
15551 }
15552 }
15553 else /* c == 0 */
15554 break;
15555 }
15556 }
15557 }
15558 break;
15559
15560 case Lisp_Symbol:
15561 /* A symbol: process the value of the symbol recursively
15562 as if it appeared here directly. Avoid error if symbol void.
15563 Special case: if value of symbol is a string, output the string
15564 literally. */
15565 {
15566 register Lisp_Object tem;
15567
15568 /* If the variable is not marked as risky to set
15569 then its contents are risky to use. */
15570 if (NILP (Fget (elt, Qrisky_local_variable)))
15571 risky = 1;
15572
15573 tem = Fboundp (elt);
15574 if (!NILP (tem))
15575 {
15576 tem = Fsymbol_value (elt);
15577 /* If value is a string, output that string literally:
15578 don't check for % within it. */
15579 if (STRINGP (tem))
15580 literal = 1;
15581
15582 if (!EQ (tem, elt))
15583 {
15584 /* Give up right away for nil or t. */
15585 elt = tem;
15586 goto tail_recurse;
15587 }
15588 }
15589 }
15590 break;
15591
15592 case Lisp_Cons:
15593 {
15594 register Lisp_Object car, tem;
15595
15596 /* A cons cell: five distinct cases.
15597 If first element is :eval or :propertize, do something special.
15598 If first element is a string or a cons, process all the elements
15599 and effectively concatenate them.
15600 If first element is a negative number, truncate displaying cdr to
15601 at most that many characters. If positive, pad (with spaces)
15602 to at least that many characters.
15603 If first element is a symbol, process the cadr or caddr recursively
15604 according to whether the symbol's value is non-nil or nil. */
15605 car = XCAR (elt);
15606 if (EQ (car, QCeval))
15607 {
15608 /* An element of the form (:eval FORM) means evaluate FORM
15609 and use the result as mode line elements. */
15610
15611 if (risky)
15612 break;
15613
15614 if (CONSP (XCDR (elt)))
15615 {
15616 Lisp_Object spec;
15617 spec = safe_eval (XCAR (XCDR (elt)));
15618 n += display_mode_element (it, depth, field_width - n,
15619 precision - n, spec, props,
15620 risky);
15621 }
15622 }
15623 else if (EQ (car, QCpropertize))
15624 {
15625 /* An element of the form (:propertize ELT PROPS...)
15626 means display ELT but applying properties PROPS. */
15627
15628 if (risky)
15629 break;
15630
15631 if (CONSP (XCDR (elt)))
15632 n += display_mode_element (it, depth, field_width - n,
15633 precision - n, XCAR (XCDR (elt)),
15634 XCDR (XCDR (elt)), risky);
15635 }
15636 else if (SYMBOLP (car))
15637 {
15638 tem = Fboundp (car);
15639 elt = XCDR (elt);
15640 if (!CONSP (elt))
15641 goto invalid;
15642 /* elt is now the cdr, and we know it is a cons cell.
15643 Use its car if CAR has a non-nil value. */
15644 if (!NILP (tem))
15645 {
15646 tem = Fsymbol_value (car);
15647 if (!NILP (tem))
15648 {
15649 elt = XCAR (elt);
15650 goto tail_recurse;
15651 }
15652 }
15653 /* Symbol's value is nil (or symbol is unbound)
15654 Get the cddr of the original list
15655 and if possible find the caddr and use that. */
15656 elt = XCDR (elt);
15657 if (NILP (elt))
15658 break;
15659 else if (!CONSP (elt))
15660 goto invalid;
15661 elt = XCAR (elt);
15662 goto tail_recurse;
15663 }
15664 else if (INTEGERP (car))
15665 {
15666 register int lim = XINT (car);
15667 elt = XCDR (elt);
15668 if (lim < 0)
15669 {
15670 /* Negative int means reduce maximum width. */
15671 if (precision <= 0)
15672 precision = -lim;
15673 else
15674 precision = min (precision, -lim);
15675 }
15676 else if (lim > 0)
15677 {
15678 /* Padding specified. Don't let it be more than
15679 current maximum. */
15680 if (precision > 0)
15681 lim = min (precision, lim);
15682
15683 /* If that's more padding than already wanted, queue it.
15684 But don't reduce padding already specified even if
15685 that is beyond the current truncation point. */
15686 field_width = max (lim, field_width);
15687 }
15688 goto tail_recurse;
15689 }
15690 else if (STRINGP (car) || CONSP (car))
15691 {
15692 register int limit = 50;
15693 /* Limit is to protect against circular lists. */
15694 while (CONSP (elt)
15695 && --limit > 0
15696 && (precision <= 0 || n < precision))
15697 {
15698 n += display_mode_element (it, depth, field_width - n,
15699 precision - n, XCAR (elt),
15700 props, risky);
15701 elt = XCDR (elt);
15702 }
15703 }
15704 }
15705 break;
15706
15707 default:
15708 invalid:
15709 elt = build_string ("*invalid*");
15710 goto tail_recurse;
15711 }
15712
15713 /* Pad to FIELD_WIDTH. */
15714 if (field_width > 0 && n < field_width)
15715 {
15716 if (frame_title_ptr)
15717 n += store_frame_title ("", field_width - n, 0);
15718 else if (!NILP (mode_line_string_list))
15719 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
15720 else
15721 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
15722 0, 0, 0);
15723 }
15724
15725 return n;
15726 }
15727
15728 /* Store a mode-line string element in mode_line_string_list.
15729
15730 If STRING is non-null, display that C string. Otherwise, the Lisp
15731 string LISP_STRING is displayed.
15732
15733 FIELD_WIDTH is the minimum number of output glyphs to produce.
15734 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15735 with spaces. FIELD_WIDTH <= 0 means don't pad.
15736
15737 PRECISION is the maximum number of characters to output from
15738 STRING. PRECISION <= 0 means don't truncate the string.
15739
15740 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
15741 properties to the string.
15742
15743 PROPS are the properties to add to the string.
15744 The mode_line_string_face face property is always added to the string.
15745 */
15746
15747 static int
15748 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
15749 char *string;
15750 Lisp_Object lisp_string;
15751 int copy_string;
15752 int field_width;
15753 int precision;
15754 Lisp_Object props;
15755 {
15756 int len;
15757 int n = 0;
15758
15759 if (string != NULL)
15760 {
15761 len = strlen (string);
15762 if (precision > 0 && len > precision)
15763 len = precision;
15764 lisp_string = make_string (string, len);
15765 if (NILP (props))
15766 props = mode_line_string_face_prop;
15767 else if (!NILP (mode_line_string_face))
15768 {
15769 Lisp_Object face = Fplist_get (props, Qface);
15770 props = Fcopy_sequence (props);
15771 if (NILP (face))
15772 face = mode_line_string_face;
15773 else
15774 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15775 props = Fplist_put (props, Qface, face);
15776 }
15777 Fadd_text_properties (make_number (0), make_number (len),
15778 props, lisp_string);
15779 }
15780 else
15781 {
15782 len = XFASTINT (Flength (lisp_string));
15783 if (precision > 0 && len > precision)
15784 {
15785 len = precision;
15786 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
15787 precision = -1;
15788 }
15789 if (!NILP (mode_line_string_face))
15790 {
15791 Lisp_Object face;
15792 if (NILP (props))
15793 props = Ftext_properties_at (make_number (0), lisp_string);
15794 face = Fplist_get (props, Qface);
15795 if (NILP (face))
15796 face = mode_line_string_face;
15797 else
15798 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15799 props = Fcons (Qface, Fcons (face, Qnil));
15800 if (copy_string)
15801 lisp_string = Fcopy_sequence (lisp_string);
15802 }
15803 if (!NILP (props))
15804 Fadd_text_properties (make_number (0), make_number (len),
15805 props, lisp_string);
15806 }
15807
15808 if (len > 0)
15809 {
15810 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15811 n += len;
15812 }
15813
15814 if (field_width > len)
15815 {
15816 field_width -= len;
15817 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
15818 if (!NILP (props))
15819 Fadd_text_properties (make_number (0), make_number (field_width),
15820 props, lisp_string);
15821 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15822 n += field_width;
15823 }
15824
15825 return n;
15826 }
15827
15828
15829 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
15830 0, 4, 0,
15831 doc: /* Return the mode-line of selected window as a string.
15832 First optional arg FORMAT specifies a different format string (see
15833 `mode-line-format' for details) to use. If FORMAT is t, return
15834 the buffer's header-line. Second optional arg WINDOW specifies a
15835 different window to use as the context for the formatting.
15836 If third optional arg NO-PROPS is non-nil, string is not propertized.
15837 Fourth optional arg BUFFER specifies which buffer to use. */)
15838 (format, window, no_props, buffer)
15839 Lisp_Object format, window, no_props, buffer;
15840 {
15841 struct it it;
15842 int len;
15843 struct window *w;
15844 struct buffer *old_buffer = NULL;
15845 enum face_id face_id = DEFAULT_FACE_ID;
15846
15847 if (NILP (window))
15848 window = selected_window;
15849 CHECK_WINDOW (window);
15850 w = XWINDOW (window);
15851
15852 if (NILP (buffer))
15853 buffer = w->buffer;
15854
15855 CHECK_BUFFER (buffer);
15856
15857 if (XBUFFER (buffer) != current_buffer)
15858 {
15859 old_buffer = current_buffer;
15860 set_buffer_internal_1 (XBUFFER (buffer));
15861 }
15862
15863 if (NILP (format) || EQ (format, Qt))
15864 {
15865 face_id = (NILP (format)
15866 ? CURRENT_MODE_LINE_FACE_ID (w)
15867 : HEADER_LINE_FACE_ID);
15868 format = (NILP (format)
15869 ? current_buffer->mode_line_format
15870 : current_buffer->header_line_format);
15871 }
15872
15873 init_iterator (&it, w, -1, -1, NULL, face_id);
15874
15875 if (NILP (no_props))
15876 {
15877 mode_line_string_face
15878 = (face_id == MODE_LINE_FACE_ID ? Qmode_line
15879 : face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive
15880 : face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
15881
15882 mode_line_string_face_prop
15883 = (NILP (mode_line_string_face) ? Qnil
15884 : Fcons (Qface, Fcons (mode_line_string_face, Qnil)));
15885
15886 /* We need a dummy last element in mode_line_string_list to
15887 indicate we are building the propertized mode-line string.
15888 Using mode_line_string_face_prop here GC protects it. */
15889 mode_line_string_list
15890 = Fcons (mode_line_string_face_prop, Qnil);
15891 frame_title_ptr = NULL;
15892 }
15893 else
15894 {
15895 mode_line_string_face_prop = Qnil;
15896 mode_line_string_list = Qnil;
15897 frame_title_ptr = frame_title_buf;
15898 }
15899
15900 push_frame_kboard (it.f);
15901 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15902 pop_frame_kboard ();
15903
15904 if (old_buffer)
15905 set_buffer_internal_1 (old_buffer);
15906
15907 if (NILP (no_props))
15908 {
15909 Lisp_Object str;
15910 mode_line_string_list = Fnreverse (mode_line_string_list);
15911 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
15912 make_string ("", 0));
15913 mode_line_string_face_prop = Qnil;
15914 mode_line_string_list = Qnil;
15915 return str;
15916 }
15917
15918 len = frame_title_ptr - frame_title_buf;
15919 if (len > 0 && frame_title_ptr[-1] == '-')
15920 {
15921 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
15922 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
15923 ;
15924 frame_title_ptr += 3; /* restore last non-dash + two dashes */
15925 if (len > frame_title_ptr - frame_title_buf)
15926 len = frame_title_ptr - frame_title_buf;
15927 }
15928
15929 frame_title_ptr = NULL;
15930 return make_string (frame_title_buf, len);
15931 }
15932
15933 /* Write a null-terminated, right justified decimal representation of
15934 the positive integer D to BUF using a minimal field width WIDTH. */
15935
15936 static void
15937 pint2str (buf, width, d)
15938 register char *buf;
15939 register int width;
15940 register int d;
15941 {
15942 register char *p = buf;
15943
15944 if (d <= 0)
15945 *p++ = '0';
15946 else
15947 {
15948 while (d > 0)
15949 {
15950 *p++ = d % 10 + '0';
15951 d /= 10;
15952 }
15953 }
15954
15955 for (width -= (int) (p - buf); width > 0; --width)
15956 *p++ = ' ';
15957 *p-- = '\0';
15958 while (p > buf)
15959 {
15960 d = *buf;
15961 *buf++ = *p;
15962 *p-- = d;
15963 }
15964 }
15965
15966 /* Write a null-terminated, right justified decimal and "human
15967 readable" representation of the nonnegative integer D to BUF using
15968 a minimal field width WIDTH. D should be smaller than 999.5e24. */
15969
15970 static const char power_letter[] =
15971 {
15972 0, /* not used */
15973 'k', /* kilo */
15974 'M', /* mega */
15975 'G', /* giga */
15976 'T', /* tera */
15977 'P', /* peta */
15978 'E', /* exa */
15979 'Z', /* zetta */
15980 'Y' /* yotta */
15981 };
15982
15983 static void
15984 pint2hrstr (buf, width, d)
15985 char *buf;
15986 int width;
15987 int d;
15988 {
15989 /* We aim to represent the nonnegative integer D as
15990 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
15991 int quotient = d;
15992 int remainder = 0;
15993 /* -1 means: do not use TENTHS. */
15994 int tenths = -1;
15995 int exponent = 0;
15996
15997 /* Length of QUOTIENT.TENTHS as a string. */
15998 int length;
15999
16000 char * psuffix;
16001 char * p;
16002
16003 if (1000 <= quotient)
16004 {
16005 /* Scale to the appropriate EXPONENT. */
16006 do
16007 {
16008 remainder = quotient % 1000;
16009 quotient /= 1000;
16010 exponent++;
16011 }
16012 while (1000 <= quotient);
16013
16014 /* Round to nearest and decide whether to use TENTHS or not. */
16015 if (quotient <= 9)
16016 {
16017 tenths = remainder / 100;
16018 if (50 <= remainder % 100)
16019 if (tenths < 9)
16020 tenths++;
16021 else
16022 {
16023 quotient++;
16024 if (quotient == 10)
16025 tenths = -1;
16026 else
16027 tenths = 0;
16028 }
16029 }
16030 else
16031 if (500 <= remainder)
16032 if (quotient < 999)
16033 quotient++;
16034 else
16035 {
16036 quotient = 1;
16037 exponent++;
16038 tenths = 0;
16039 }
16040 }
16041
16042 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
16043 if (tenths == -1 && quotient <= 99)
16044 if (quotient <= 9)
16045 length = 1;
16046 else
16047 length = 2;
16048 else
16049 length = 3;
16050 p = psuffix = buf + max (width, length);
16051
16052 /* Print EXPONENT. */
16053 if (exponent)
16054 *psuffix++ = power_letter[exponent];
16055 *psuffix = '\0';
16056
16057 /* Print TENTHS. */
16058 if (tenths >= 0)
16059 {
16060 *--p = '0' + tenths;
16061 *--p = '.';
16062 }
16063
16064 /* Print QUOTIENT. */
16065 do
16066 {
16067 int digit = quotient % 10;
16068 *--p = '0' + digit;
16069 }
16070 while ((quotient /= 10) != 0);
16071
16072 /* Print leading spaces. */
16073 while (buf < p)
16074 *--p = ' ';
16075 }
16076
16077 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
16078 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
16079 type of CODING_SYSTEM. Return updated pointer into BUF. */
16080
16081 static unsigned char invalid_eol_type[] = "(*invalid*)";
16082
16083 static char *
16084 decode_mode_spec_coding (coding_system, buf, eol_flag)
16085 Lisp_Object coding_system;
16086 register char *buf;
16087 int eol_flag;
16088 {
16089 Lisp_Object val;
16090 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
16091 const unsigned char *eol_str;
16092 int eol_str_len;
16093 /* The EOL conversion we are using. */
16094 Lisp_Object eoltype;
16095
16096 val = Fget (coding_system, Qcoding_system);
16097 eoltype = Qnil;
16098
16099 if (!VECTORP (val)) /* Not yet decided. */
16100 {
16101 if (multibyte)
16102 *buf++ = '-';
16103 if (eol_flag)
16104 eoltype = eol_mnemonic_undecided;
16105 /* Don't mention EOL conversion if it isn't decided. */
16106 }
16107 else
16108 {
16109 Lisp_Object eolvalue;
16110
16111 eolvalue = Fget (coding_system, Qeol_type);
16112
16113 if (multibyte)
16114 *buf++ = XFASTINT (AREF (val, 1));
16115
16116 if (eol_flag)
16117 {
16118 /* The EOL conversion that is normal on this system. */
16119
16120 if (NILP (eolvalue)) /* Not yet decided. */
16121 eoltype = eol_mnemonic_undecided;
16122 else if (VECTORP (eolvalue)) /* Not yet decided. */
16123 eoltype = eol_mnemonic_undecided;
16124 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
16125 eoltype = (XFASTINT (eolvalue) == 0
16126 ? eol_mnemonic_unix
16127 : (XFASTINT (eolvalue) == 1
16128 ? eol_mnemonic_dos : eol_mnemonic_mac));
16129 }
16130 }
16131
16132 if (eol_flag)
16133 {
16134 /* Mention the EOL conversion if it is not the usual one. */
16135 if (STRINGP (eoltype))
16136 {
16137 eol_str = SDATA (eoltype);
16138 eol_str_len = SBYTES (eoltype);
16139 }
16140 else if (INTEGERP (eoltype)
16141 && CHAR_VALID_P (XINT (eoltype), 0))
16142 {
16143 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16144 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16145 eol_str = tmp;
16146 }
16147 else
16148 {
16149 eol_str = invalid_eol_type;
16150 eol_str_len = sizeof (invalid_eol_type) - 1;
16151 }
16152 bcopy (eol_str, buf, eol_str_len);
16153 buf += eol_str_len;
16154 }
16155
16156 return buf;
16157 }
16158
16159 /* Return a string for the output of a mode line %-spec for window W,
16160 generated by character C. PRECISION >= 0 means don't return a
16161 string longer than that value. FIELD_WIDTH > 0 means pad the
16162 string returned with spaces to that value. Return 1 in *MULTIBYTE
16163 if the result is multibyte text.
16164
16165 Note we operate on the current buffer for most purposes,
16166 the exception being w->base_line_pos. */
16167
16168 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16169
16170 static char *
16171 decode_mode_spec (w, c, field_width, precision, multibyte)
16172 struct window *w;
16173 register int c;
16174 int field_width, precision;
16175 int *multibyte;
16176 {
16177 Lisp_Object obj;
16178 struct frame *f = XFRAME (WINDOW_FRAME (w));
16179 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16180 struct buffer *b = current_buffer;
16181
16182 obj = Qnil;
16183 *multibyte = 0;
16184
16185 switch (c)
16186 {
16187 case '*':
16188 if (!NILP (b->read_only))
16189 return "%";
16190 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16191 return "*";
16192 return "-";
16193
16194 case '+':
16195 /* This differs from %* only for a modified read-only buffer. */
16196 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16197 return "*";
16198 if (!NILP (b->read_only))
16199 return "%";
16200 return "-";
16201
16202 case '&':
16203 /* This differs from %* in ignoring read-only-ness. */
16204 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16205 return "*";
16206 return "-";
16207
16208 case '%':
16209 return "%";
16210
16211 case '[':
16212 {
16213 int i;
16214 char *p;
16215
16216 if (command_loop_level > 5)
16217 return "[[[... ";
16218 p = decode_mode_spec_buf;
16219 for (i = 0; i < command_loop_level; i++)
16220 *p++ = '[';
16221 *p = 0;
16222 return decode_mode_spec_buf;
16223 }
16224
16225 case ']':
16226 {
16227 int i;
16228 char *p;
16229
16230 if (command_loop_level > 5)
16231 return " ...]]]";
16232 p = decode_mode_spec_buf;
16233 for (i = 0; i < command_loop_level; i++)
16234 *p++ = ']';
16235 *p = 0;
16236 return decode_mode_spec_buf;
16237 }
16238
16239 case '-':
16240 {
16241 register int i;
16242
16243 /* Let lots_of_dashes be a string of infinite length. */
16244 if (!NILP (mode_line_string_list))
16245 return "--";
16246 if (field_width <= 0
16247 || field_width > sizeof (lots_of_dashes))
16248 {
16249 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16250 decode_mode_spec_buf[i] = '-';
16251 decode_mode_spec_buf[i] = '\0';
16252 return decode_mode_spec_buf;
16253 }
16254 else
16255 return lots_of_dashes;
16256 }
16257
16258 case 'b':
16259 obj = b->name;
16260 break;
16261
16262 case 'c':
16263 {
16264 int col = (int) current_column (); /* iftc */
16265 w->column_number_displayed = make_number (col);
16266 pint2str (decode_mode_spec_buf, field_width, col);
16267 return decode_mode_spec_buf;
16268 }
16269
16270 case 'F':
16271 /* %F displays the frame name. */
16272 if (!NILP (f->title))
16273 return (char *) SDATA (f->title);
16274 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16275 return (char *) SDATA (f->name);
16276 return "Emacs";
16277
16278 case 'f':
16279 obj = b->filename;
16280 break;
16281
16282 case 'i':
16283 {
16284 int size = ZV - BEGV;
16285 pint2str (decode_mode_spec_buf, field_width, size);
16286 return decode_mode_spec_buf;
16287 }
16288
16289 case 'I':
16290 {
16291 int size = ZV - BEGV;
16292 pint2hrstr (decode_mode_spec_buf, field_width, size);
16293 return decode_mode_spec_buf;
16294 }
16295
16296 case 'l':
16297 {
16298 int startpos = XMARKER (w->start)->charpos;
16299 int startpos_byte = marker_byte_position (w->start);
16300 int line, linepos, linepos_byte, topline;
16301 int nlines, junk;
16302 int height = WINDOW_TOTAL_LINES (w);
16303
16304 /* If we decided that this buffer isn't suitable for line numbers,
16305 don't forget that too fast. */
16306 if (EQ (w->base_line_pos, w->buffer))
16307 goto no_value;
16308 /* But do forget it, if the window shows a different buffer now. */
16309 else if (BUFFERP (w->base_line_pos))
16310 w->base_line_pos = Qnil;
16311
16312 /* If the buffer is very big, don't waste time. */
16313 if (INTEGERP (Vline_number_display_limit)
16314 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16315 {
16316 w->base_line_pos = Qnil;
16317 w->base_line_number = Qnil;
16318 goto no_value;
16319 }
16320
16321 if (!NILP (w->base_line_number)
16322 && !NILP (w->base_line_pos)
16323 && XFASTINT (w->base_line_pos) <= startpos)
16324 {
16325 line = XFASTINT (w->base_line_number);
16326 linepos = XFASTINT (w->base_line_pos);
16327 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16328 }
16329 else
16330 {
16331 line = 1;
16332 linepos = BUF_BEGV (b);
16333 linepos_byte = BUF_BEGV_BYTE (b);
16334 }
16335
16336 /* Count lines from base line to window start position. */
16337 nlines = display_count_lines (linepos, linepos_byte,
16338 startpos_byte,
16339 startpos, &junk);
16340
16341 topline = nlines + line;
16342
16343 /* Determine a new base line, if the old one is too close
16344 or too far away, or if we did not have one.
16345 "Too close" means it's plausible a scroll-down would
16346 go back past it. */
16347 if (startpos == BUF_BEGV (b))
16348 {
16349 w->base_line_number = make_number (topline);
16350 w->base_line_pos = make_number (BUF_BEGV (b));
16351 }
16352 else if (nlines < height + 25 || nlines > height * 3 + 50
16353 || linepos == BUF_BEGV (b))
16354 {
16355 int limit = BUF_BEGV (b);
16356 int limit_byte = BUF_BEGV_BYTE (b);
16357 int position;
16358 int distance = (height * 2 + 30) * line_number_display_limit_width;
16359
16360 if (startpos - distance > limit)
16361 {
16362 limit = startpos - distance;
16363 limit_byte = CHAR_TO_BYTE (limit);
16364 }
16365
16366 nlines = display_count_lines (startpos, startpos_byte,
16367 limit_byte,
16368 - (height * 2 + 30),
16369 &position);
16370 /* If we couldn't find the lines we wanted within
16371 line_number_display_limit_width chars per line,
16372 give up on line numbers for this window. */
16373 if (position == limit_byte && limit == startpos - distance)
16374 {
16375 w->base_line_pos = w->buffer;
16376 w->base_line_number = Qnil;
16377 goto no_value;
16378 }
16379
16380 w->base_line_number = make_number (topline - nlines);
16381 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
16382 }
16383
16384 /* Now count lines from the start pos to point. */
16385 nlines = display_count_lines (startpos, startpos_byte,
16386 PT_BYTE, PT, &junk);
16387
16388 /* Record that we did display the line number. */
16389 line_number_displayed = 1;
16390
16391 /* Make the string to show. */
16392 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
16393 return decode_mode_spec_buf;
16394 no_value:
16395 {
16396 char* p = decode_mode_spec_buf;
16397 int pad = field_width - 2;
16398 while (pad-- > 0)
16399 *p++ = ' ';
16400 *p++ = '?';
16401 *p++ = '?';
16402 *p = '\0';
16403 return decode_mode_spec_buf;
16404 }
16405 }
16406 break;
16407
16408 case 'm':
16409 obj = b->mode_name;
16410 break;
16411
16412 case 'n':
16413 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
16414 return " Narrow";
16415 break;
16416
16417 case 'p':
16418 {
16419 int pos = marker_position (w->start);
16420 int total = BUF_ZV (b) - BUF_BEGV (b);
16421
16422 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
16423 {
16424 if (pos <= BUF_BEGV (b))
16425 return "All";
16426 else
16427 return "Bottom";
16428 }
16429 else if (pos <= BUF_BEGV (b))
16430 return "Top";
16431 else
16432 {
16433 if (total > 1000000)
16434 /* Do it differently for a large value, to avoid overflow. */
16435 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16436 else
16437 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
16438 /* We can't normally display a 3-digit number,
16439 so get us a 2-digit number that is close. */
16440 if (total == 100)
16441 total = 99;
16442 sprintf (decode_mode_spec_buf, "%2d%%", total);
16443 return decode_mode_spec_buf;
16444 }
16445 }
16446
16447 /* Display percentage of size above the bottom of the screen. */
16448 case 'P':
16449 {
16450 int toppos = marker_position (w->start);
16451 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16452 int total = BUF_ZV (b) - BUF_BEGV (b);
16453
16454 if (botpos >= BUF_ZV (b))
16455 {
16456 if (toppos <= BUF_BEGV (b))
16457 return "All";
16458 else
16459 return "Bottom";
16460 }
16461 else
16462 {
16463 if (total > 1000000)
16464 /* Do it differently for a large value, to avoid overflow. */
16465 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16466 else
16467 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
16468 /* We can't normally display a 3-digit number,
16469 so get us a 2-digit number that is close. */
16470 if (total == 100)
16471 total = 99;
16472 if (toppos <= BUF_BEGV (b))
16473 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
16474 else
16475 sprintf (decode_mode_spec_buf, "%2d%%", total);
16476 return decode_mode_spec_buf;
16477 }
16478 }
16479
16480 case 's':
16481 /* status of process */
16482 obj = Fget_buffer_process (Fcurrent_buffer ());
16483 if (NILP (obj))
16484 return "no process";
16485 #ifdef subprocesses
16486 obj = Fsymbol_name (Fprocess_status (obj));
16487 #endif
16488 break;
16489
16490 case 't': /* indicate TEXT or BINARY */
16491 #ifdef MODE_LINE_BINARY_TEXT
16492 return MODE_LINE_BINARY_TEXT (b);
16493 #else
16494 return "T";
16495 #endif
16496
16497 case 'z':
16498 /* coding-system (not including end-of-line format) */
16499 case 'Z':
16500 /* coding-system (including end-of-line type) */
16501 {
16502 int eol_flag = (c == 'Z');
16503 char *p = decode_mode_spec_buf;
16504
16505 if (! FRAME_WINDOW_P (f))
16506 {
16507 /* No need to mention EOL here--the terminal never needs
16508 to do EOL conversion. */
16509 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
16510 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
16511 }
16512 p = decode_mode_spec_coding (b->buffer_file_coding_system,
16513 p, eol_flag);
16514
16515 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
16516 #ifdef subprocesses
16517 obj = Fget_buffer_process (Fcurrent_buffer ());
16518 if (PROCESSP (obj))
16519 {
16520 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16521 p, eol_flag);
16522 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16523 p, eol_flag);
16524 }
16525 #endif /* subprocesses */
16526 #endif /* 0 */
16527 *p = 0;
16528 return decode_mode_spec_buf;
16529 }
16530 }
16531
16532 if (STRINGP (obj))
16533 {
16534 *multibyte = STRING_MULTIBYTE (obj);
16535 return (char *) SDATA (obj);
16536 }
16537 else
16538 return "";
16539 }
16540
16541
16542 /* Count up to COUNT lines starting from START / START_BYTE.
16543 But don't go beyond LIMIT_BYTE.
16544 Return the number of lines thus found (always nonnegative).
16545
16546 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
16547
16548 static int
16549 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16550 int start, start_byte, limit_byte, count;
16551 int *byte_pos_ptr;
16552 {
16553 register unsigned char *cursor;
16554 unsigned char *base;
16555
16556 register int ceiling;
16557 register unsigned char *ceiling_addr;
16558 int orig_count = count;
16559
16560 /* If we are not in selective display mode,
16561 check only for newlines. */
16562 int selective_display = (!NILP (current_buffer->selective_display)
16563 && !INTEGERP (current_buffer->selective_display));
16564
16565 if (count > 0)
16566 {
16567 while (start_byte < limit_byte)
16568 {
16569 ceiling = BUFFER_CEILING_OF (start_byte);
16570 ceiling = min (limit_byte - 1, ceiling);
16571 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16572 base = (cursor = BYTE_POS_ADDR (start_byte));
16573 while (1)
16574 {
16575 if (selective_display)
16576 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16577 ;
16578 else
16579 while (*cursor != '\n' && ++cursor != ceiling_addr)
16580 ;
16581
16582 if (cursor != ceiling_addr)
16583 {
16584 if (--count == 0)
16585 {
16586 start_byte += cursor - base + 1;
16587 *byte_pos_ptr = start_byte;
16588 return orig_count;
16589 }
16590 else
16591 if (++cursor == ceiling_addr)
16592 break;
16593 }
16594 else
16595 break;
16596 }
16597 start_byte += cursor - base;
16598 }
16599 }
16600 else
16601 {
16602 while (start_byte > limit_byte)
16603 {
16604 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16605 ceiling = max (limit_byte, ceiling);
16606 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16607 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16608 while (1)
16609 {
16610 if (selective_display)
16611 while (--cursor != ceiling_addr
16612 && *cursor != '\n' && *cursor != 015)
16613 ;
16614 else
16615 while (--cursor != ceiling_addr && *cursor != '\n')
16616 ;
16617
16618 if (cursor != ceiling_addr)
16619 {
16620 if (++count == 0)
16621 {
16622 start_byte += cursor - base + 1;
16623 *byte_pos_ptr = start_byte;
16624 /* When scanning backwards, we should
16625 not count the newline posterior to which we stop. */
16626 return - orig_count - 1;
16627 }
16628 }
16629 else
16630 break;
16631 }
16632 /* Here we add 1 to compensate for the last decrement
16633 of CURSOR, which took it past the valid range. */
16634 start_byte += cursor - base + 1;
16635 }
16636 }
16637
16638 *byte_pos_ptr = limit_byte;
16639
16640 if (count < 0)
16641 return - orig_count + count;
16642 return orig_count - count;
16643
16644 }
16645
16646
16647 \f
16648 /***********************************************************************
16649 Displaying strings
16650 ***********************************************************************/
16651
16652 /* Display a NUL-terminated string, starting with index START.
16653
16654 If STRING is non-null, display that C string. Otherwise, the Lisp
16655 string LISP_STRING is displayed.
16656
16657 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16658 FACE_STRING. Display STRING or LISP_STRING with the face at
16659 FACE_STRING_POS in FACE_STRING:
16660
16661 Display the string in the environment given by IT, but use the
16662 standard display table, temporarily.
16663
16664 FIELD_WIDTH is the minimum number of output glyphs to produce.
16665 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16666 with spaces. If STRING has more characters, more than FIELD_WIDTH
16667 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
16668
16669 PRECISION is the maximum number of characters to output from
16670 STRING. PRECISION < 0 means don't truncate the string.
16671
16672 This is roughly equivalent to printf format specifiers:
16673
16674 FIELD_WIDTH PRECISION PRINTF
16675 ----------------------------------------
16676 -1 -1 %s
16677 -1 10 %.10s
16678 10 -1 %10s
16679 20 10 %20.10s
16680
16681 MULTIBYTE zero means do not display multibyte chars, > 0 means do
16682 display them, and < 0 means obey the current buffer's value of
16683 enable_multibyte_characters.
16684
16685 Value is the number of glyphs produced. */
16686
16687 static int
16688 display_string (string, lisp_string, face_string, face_string_pos,
16689 start, it, field_width, precision, max_x, multibyte)
16690 unsigned char *string;
16691 Lisp_Object lisp_string;
16692 Lisp_Object face_string;
16693 int face_string_pos;
16694 int start;
16695 struct it *it;
16696 int field_width, precision, max_x;
16697 int multibyte;
16698 {
16699 int hpos_at_start = it->hpos;
16700 int saved_face_id = it->face_id;
16701 struct glyph_row *row = it->glyph_row;
16702
16703 /* Initialize the iterator IT for iteration over STRING beginning
16704 with index START. */
16705 reseat_to_string (it, string, lisp_string, start,
16706 precision, field_width, multibyte);
16707
16708 /* If displaying STRING, set up the face of the iterator
16709 from LISP_STRING, if that's given. */
16710 if (STRINGP (face_string))
16711 {
16712 int endptr;
16713 struct face *face;
16714
16715 it->face_id
16716 = face_at_string_position (it->w, face_string, face_string_pos,
16717 0, it->region_beg_charpos,
16718 it->region_end_charpos,
16719 &endptr, it->base_face_id, 0);
16720 face = FACE_FROM_ID (it->f, it->face_id);
16721 it->face_box_p = face->box != FACE_NO_BOX;
16722 }
16723
16724 /* Set max_x to the maximum allowed X position. Don't let it go
16725 beyond the right edge of the window. */
16726 if (max_x <= 0)
16727 max_x = it->last_visible_x;
16728 else
16729 max_x = min (max_x, it->last_visible_x);
16730
16731 /* Skip over display elements that are not visible. because IT->w is
16732 hscrolled. */
16733 if (it->current_x < it->first_visible_x)
16734 move_it_in_display_line_to (it, 100000, it->first_visible_x,
16735 MOVE_TO_POS | MOVE_TO_X);
16736
16737 row->ascent = it->max_ascent;
16738 row->height = it->max_ascent + it->max_descent;
16739 row->phys_ascent = it->max_phys_ascent;
16740 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16741
16742 /* This condition is for the case that we are called with current_x
16743 past last_visible_x. */
16744 while (it->current_x < max_x)
16745 {
16746 int x_before, x, n_glyphs_before, i, nglyphs;
16747
16748 /* Get the next display element. */
16749 if (!get_next_display_element (it))
16750 break;
16751
16752 /* Produce glyphs. */
16753 x_before = it->current_x;
16754 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
16755 PRODUCE_GLYPHS (it);
16756
16757 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
16758 i = 0;
16759 x = x_before;
16760 while (i < nglyphs)
16761 {
16762 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16763
16764 if (!it->truncate_lines_p
16765 && x + glyph->pixel_width > max_x)
16766 {
16767 /* End of continued line or max_x reached. */
16768 if (CHAR_GLYPH_PADDING_P (*glyph))
16769 {
16770 /* A wide character is unbreakable. */
16771 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
16772 it->current_x = x_before;
16773 }
16774 else
16775 {
16776 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
16777 it->current_x = x;
16778 }
16779 break;
16780 }
16781 else if (x + glyph->pixel_width > it->first_visible_x)
16782 {
16783 /* Glyph is at least partially visible. */
16784 ++it->hpos;
16785 if (x < it->first_visible_x)
16786 it->glyph_row->x = x - it->first_visible_x;
16787 }
16788 else
16789 {
16790 /* Glyph is off the left margin of the display area.
16791 Should not happen. */
16792 abort ();
16793 }
16794
16795 row->ascent = max (row->ascent, it->max_ascent);
16796 row->height = max (row->height, it->max_ascent + it->max_descent);
16797 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16798 row->phys_height = max (row->phys_height,
16799 it->max_phys_ascent + it->max_phys_descent);
16800 x += glyph->pixel_width;
16801 ++i;
16802 }
16803
16804 /* Stop if max_x reached. */
16805 if (i < nglyphs)
16806 break;
16807
16808 /* Stop at line ends. */
16809 if (ITERATOR_AT_END_OF_LINE_P (it))
16810 {
16811 it->continuation_lines_width = 0;
16812 break;
16813 }
16814
16815 set_iterator_to_next (it, 1);
16816
16817 /* Stop if truncating at the right edge. */
16818 if (it->truncate_lines_p
16819 && it->current_x >= it->last_visible_x)
16820 {
16821 /* Add truncation mark, but don't do it if the line is
16822 truncated at a padding space. */
16823 if (IT_CHARPOS (*it) < it->string_nchars)
16824 {
16825 if (!FRAME_WINDOW_P (it->f))
16826 {
16827 int i, n;
16828
16829 if (it->current_x > it->last_visible_x)
16830 {
16831 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16832 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16833 break;
16834 for (n = row->used[TEXT_AREA]; i < n; ++i)
16835 {
16836 row->used[TEXT_AREA] = i;
16837 produce_special_glyphs (it, IT_TRUNCATION);
16838 }
16839 }
16840 produce_special_glyphs (it, IT_TRUNCATION);
16841 }
16842 it->glyph_row->truncated_on_right_p = 1;
16843 }
16844 break;
16845 }
16846 }
16847
16848 /* Maybe insert a truncation at the left. */
16849 if (it->first_visible_x
16850 && IT_CHARPOS (*it) > 0)
16851 {
16852 if (!FRAME_WINDOW_P (it->f))
16853 insert_left_trunc_glyphs (it);
16854 it->glyph_row->truncated_on_left_p = 1;
16855 }
16856
16857 it->face_id = saved_face_id;
16858
16859 /* Value is number of columns displayed. */
16860 return it->hpos - hpos_at_start;
16861 }
16862
16863
16864 \f
16865 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
16866 appears as an element of LIST or as the car of an element of LIST.
16867 If PROPVAL is a list, compare each element against LIST in that
16868 way, and return 1/2 if any element of PROPVAL is found in LIST.
16869 Otherwise return 0. This function cannot quit.
16870 The return value is 2 if the text is invisible but with an ellipsis
16871 and 1 if it's invisible and without an ellipsis. */
16872
16873 int
16874 invisible_p (propval, list)
16875 register Lisp_Object propval;
16876 Lisp_Object list;
16877 {
16878 register Lisp_Object tail, proptail;
16879
16880 for (tail = list; CONSP (tail); tail = XCDR (tail))
16881 {
16882 register Lisp_Object tem;
16883 tem = XCAR (tail);
16884 if (EQ (propval, tem))
16885 return 1;
16886 if (CONSP (tem) && EQ (propval, XCAR (tem)))
16887 return NILP (XCDR (tem)) ? 1 : 2;
16888 }
16889
16890 if (CONSP (propval))
16891 {
16892 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
16893 {
16894 Lisp_Object propelt;
16895 propelt = XCAR (proptail);
16896 for (tail = list; CONSP (tail); tail = XCDR (tail))
16897 {
16898 register Lisp_Object tem;
16899 tem = XCAR (tail);
16900 if (EQ (propelt, tem))
16901 return 1;
16902 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
16903 return NILP (XCDR (tem)) ? 1 : 2;
16904 }
16905 }
16906 }
16907
16908 return 0;
16909 }
16910
16911 /* Calculate a width or height in pixels from a specification using
16912 the following elements:
16913
16914 SPEC ::=
16915 NUM - a (fractional) multiple of the default font width/height
16916 (NUM) - specifies exactly NUM pixels
16917 UNIT - a fixed number of pixels, see below.
16918 ELEMENT - size of a display element in pixels, see below.
16919 (NUM . SPEC) - equals NUM * SPEC
16920 (+ SPEC SPEC ...) - add pixel values
16921 (- SPEC SPEC ...) - subtract pixel values
16922 (- SPEC) - negate pixel value
16923
16924 NUM ::=
16925 INT or FLOAT - a number constant
16926 SYMBOL - use symbol's (buffer local) variable binding.
16927
16928 UNIT ::=
16929 in - pixels per inch *)
16930 mm - pixels per 1/1000 meter *)
16931 cm - pixels per 1/100 meter *)
16932 width - width of current font in pixels.
16933 height - height of current font in pixels.
16934
16935 *) using the ratio(s) defined in display-pixels-per-inch.
16936
16937 ELEMENT ::=
16938
16939 left-fringe - left fringe width in pixels
16940 right-fringe - right fringe width in pixels
16941
16942 left-margin - left margin width in pixels
16943 right-margin - right margin width in pixels
16944
16945 scroll-bar - scroll-bar area width in pixels
16946
16947 Examples:
16948
16949 Pixels corresponding to 5 inches:
16950 (5 . in)
16951
16952 Total width of non-text areas on left side of window (if scroll-bar is on left):
16953 '(space :width (+ left-fringe left-margin scroll-bar))
16954
16955 Align to first text column (in header line):
16956 '(space :align-to 0)
16957
16958 Align to middle of text area minus half the width of variable `my-image'
16959 containing a loaded image:
16960 '(space :align-to (0.5 . (- text my-image)))
16961
16962 Width of left margin minus width of 1 character in the default font:
16963 '(space :width (- left-margin 1))
16964
16965 Width of left margin minus width of 2 characters in the current font:
16966 '(space :width (- left-margin (2 . width)))
16967
16968 Center 1 character over left-margin (in header line):
16969 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
16970
16971 Different ways to express width of left fringe plus left margin minus one pixel:
16972 '(space :width (- (+ left-fringe left-margin) (1)))
16973 '(space :width (+ left-fringe left-margin (- (1))))
16974 '(space :width (+ left-fringe left-margin (-1)))
16975
16976 */
16977
16978 #define NUMVAL(X) \
16979 ((INTEGERP (X) || FLOATP (X)) \
16980 ? XFLOATINT (X) \
16981 : - 1)
16982
16983 int
16984 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
16985 double *res;
16986 struct it *it;
16987 Lisp_Object prop;
16988 void *font;
16989 int width_p, *align_to;
16990 {
16991 double pixels;
16992
16993 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
16994 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
16995
16996 if (NILP (prop))
16997 return OK_PIXELS (0);
16998
16999 if (SYMBOLP (prop))
17000 {
17001 if (SCHARS (SYMBOL_NAME (prop)) == 2)
17002 {
17003 char *unit = SDATA (SYMBOL_NAME (prop));
17004
17005 if (unit[0] == 'i' && unit[1] == 'n')
17006 pixels = 1.0;
17007 else if (unit[0] == 'm' && unit[1] == 'm')
17008 pixels = 25.4;
17009 else if (unit[0] == 'c' && unit[1] == 'm')
17010 pixels = 2.54;
17011 else
17012 pixels = 0;
17013 if (pixels > 0)
17014 {
17015 double ppi;
17016 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
17017 || (CONSP (Vdisplay_pixels_per_inch)
17018 && (ppi = (width_p
17019 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
17020 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
17021 ppi > 0)))
17022 return OK_PIXELS (ppi / pixels);
17023
17024 return 0;
17025 }
17026 }
17027
17028 #ifdef HAVE_WINDOW_SYSTEM
17029 if (EQ (prop, Qheight))
17030 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
17031 if (EQ (prop, Qwidth))
17032 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
17033 #else
17034 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
17035 return OK_PIXELS (1);
17036 #endif
17037
17038 if (EQ (prop, Qtext))
17039 return OK_PIXELS (width_p
17040 ? window_box_width (it->w, TEXT_AREA)
17041 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
17042
17043 if (align_to && *align_to < 0)
17044 {
17045 *res = 0;
17046 if (EQ (prop, Qleft))
17047 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
17048 if (EQ (prop, Qright))
17049 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
17050 if (EQ (prop, Qcenter))
17051 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
17052 + window_box_width (it->w, TEXT_AREA) / 2);
17053 if (EQ (prop, Qleft_fringe))
17054 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17055 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
17056 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
17057 if (EQ (prop, Qright_fringe))
17058 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17059 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17060 : window_box_right_offset (it->w, TEXT_AREA));
17061 if (EQ (prop, Qleft_margin))
17062 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
17063 if (EQ (prop, Qright_margin))
17064 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
17065 if (EQ (prop, Qscroll_bar))
17066 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
17067 ? 0
17068 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17069 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17070 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
17071 : 0)));
17072 }
17073 else
17074 {
17075 if (EQ (prop, Qleft_fringe))
17076 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
17077 if (EQ (prop, Qright_fringe))
17078 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
17079 if (EQ (prop, Qleft_margin))
17080 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
17081 if (EQ (prop, Qright_margin))
17082 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
17083 if (EQ (prop, Qscroll_bar))
17084 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
17085 }
17086
17087 prop = Fbuffer_local_value (prop, it->w->buffer);
17088 }
17089
17090 if (INTEGERP (prop) || FLOATP (prop))
17091 {
17092 int base_unit = (width_p
17093 ? FRAME_COLUMN_WIDTH (it->f)
17094 : FRAME_LINE_HEIGHT (it->f));
17095 return OK_PIXELS (XFLOATINT (prop) * base_unit);
17096 }
17097
17098 if (CONSP (prop))
17099 {
17100 Lisp_Object car = XCAR (prop);
17101 Lisp_Object cdr = XCDR (prop);
17102
17103 if (SYMBOLP (car))
17104 {
17105 #ifdef HAVE_WINDOW_SYSTEM
17106 if (valid_image_p (prop))
17107 {
17108 int id = lookup_image (it->f, prop);
17109 struct image *img = IMAGE_FROM_ID (it->f, id);
17110
17111 return OK_PIXELS (width_p ? img->width : img->height);
17112 }
17113 #endif
17114 if (EQ (car, Qplus) || EQ (car, Qminus))
17115 {
17116 int first = 1;
17117 double px;
17118
17119 pixels = 0;
17120 while (CONSP (cdr))
17121 {
17122 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
17123 font, width_p, align_to))
17124 return 0;
17125 if (first)
17126 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
17127 else
17128 pixels += px;
17129 cdr = XCDR (cdr);
17130 }
17131 if (EQ (car, Qminus))
17132 pixels = -pixels;
17133 return OK_PIXELS (pixels);
17134 }
17135
17136 car = Fbuffer_local_value (car, it->w->buffer);
17137 }
17138
17139 if (INTEGERP (car) || FLOATP (car))
17140 {
17141 double fact;
17142 pixels = XFLOATINT (car);
17143 if (NILP (cdr))
17144 return OK_PIXELS (pixels);
17145 if (calc_pixel_width_or_height (&fact, it, cdr,
17146 font, width_p, align_to))
17147 return OK_PIXELS (pixels * fact);
17148 return 0;
17149 }
17150
17151 return 0;
17152 }
17153
17154 return 0;
17155 }
17156
17157 \f
17158 /***********************************************************************
17159 Glyph Display
17160 ***********************************************************************/
17161
17162 #ifdef HAVE_WINDOW_SYSTEM
17163
17164 #if GLYPH_DEBUG
17165
17166 void
17167 dump_glyph_string (s)
17168 struct glyph_string *s;
17169 {
17170 fprintf (stderr, "glyph string\n");
17171 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17172 s->x, s->y, s->width, s->height);
17173 fprintf (stderr, " ybase = %d\n", s->ybase);
17174 fprintf (stderr, " hl = %d\n", s->hl);
17175 fprintf (stderr, " left overhang = %d, right = %d\n",
17176 s->left_overhang, s->right_overhang);
17177 fprintf (stderr, " nchars = %d\n", s->nchars);
17178 fprintf (stderr, " extends to end of line = %d\n",
17179 s->extends_to_end_of_line_p);
17180 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17181 fprintf (stderr, " bg width = %d\n", s->background_width);
17182 }
17183
17184 #endif /* GLYPH_DEBUG */
17185
17186 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17187 of XChar2b structures for S; it can't be allocated in
17188 init_glyph_string because it must be allocated via `alloca'. W
17189 is the window on which S is drawn. ROW and AREA are the glyph row
17190 and area within the row from which S is constructed. START is the
17191 index of the first glyph structure covered by S. HL is a
17192 face-override for drawing S. */
17193
17194 #ifdef HAVE_NTGUI
17195 #define OPTIONAL_HDC(hdc) hdc,
17196 #define DECLARE_HDC(hdc) HDC hdc;
17197 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17198 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17199 #endif
17200
17201 #ifndef OPTIONAL_HDC
17202 #define OPTIONAL_HDC(hdc)
17203 #define DECLARE_HDC(hdc)
17204 #define ALLOCATE_HDC(hdc, f)
17205 #define RELEASE_HDC(hdc, f)
17206 #endif
17207
17208 static void
17209 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17210 struct glyph_string *s;
17211 DECLARE_HDC (hdc)
17212 XChar2b *char2b;
17213 struct window *w;
17214 struct glyph_row *row;
17215 enum glyph_row_area area;
17216 int start;
17217 enum draw_glyphs_face hl;
17218 {
17219 bzero (s, sizeof *s);
17220 s->w = w;
17221 s->f = XFRAME (w->frame);
17222 #ifdef HAVE_NTGUI
17223 s->hdc = hdc;
17224 #endif
17225 s->display = FRAME_X_DISPLAY (s->f);
17226 s->window = FRAME_X_WINDOW (s->f);
17227 s->char2b = char2b;
17228 s->hl = hl;
17229 s->row = row;
17230 s->area = area;
17231 s->first_glyph = row->glyphs[area] + start;
17232 s->height = row->height;
17233 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17234
17235 /* Display the internal border below the tool-bar window. */
17236 if (s->w == XWINDOW (s->f->tool_bar_window))
17237 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17238
17239 s->ybase = s->y + row->ascent;
17240 }
17241
17242
17243 /* Append the list of glyph strings with head H and tail T to the list
17244 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17245
17246 static INLINE void
17247 append_glyph_string_lists (head, tail, h, t)
17248 struct glyph_string **head, **tail;
17249 struct glyph_string *h, *t;
17250 {
17251 if (h)
17252 {
17253 if (*head)
17254 (*tail)->next = h;
17255 else
17256 *head = h;
17257 h->prev = *tail;
17258 *tail = t;
17259 }
17260 }
17261
17262
17263 /* Prepend the list of glyph strings with head H and tail T to the
17264 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17265 result. */
17266
17267 static INLINE void
17268 prepend_glyph_string_lists (head, tail, h, t)
17269 struct glyph_string **head, **tail;
17270 struct glyph_string *h, *t;
17271 {
17272 if (h)
17273 {
17274 if (*head)
17275 (*head)->prev = t;
17276 else
17277 *tail = t;
17278 t->next = *head;
17279 *head = h;
17280 }
17281 }
17282
17283
17284 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17285 Set *HEAD and *TAIL to the resulting list. */
17286
17287 static INLINE void
17288 append_glyph_string (head, tail, s)
17289 struct glyph_string **head, **tail;
17290 struct glyph_string *s;
17291 {
17292 s->next = s->prev = NULL;
17293 append_glyph_string_lists (head, tail, s, s);
17294 }
17295
17296
17297 /* Get face and two-byte form of character glyph GLYPH on frame F.
17298 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17299 a pointer to a realized face that is ready for display. */
17300
17301 static INLINE struct face *
17302 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17303 struct frame *f;
17304 struct glyph *glyph;
17305 XChar2b *char2b;
17306 int *two_byte_p;
17307 {
17308 struct face *face;
17309
17310 xassert (glyph->type == CHAR_GLYPH);
17311 face = FACE_FROM_ID (f, glyph->face_id);
17312
17313 if (two_byte_p)
17314 *two_byte_p = 0;
17315
17316 if (!glyph->multibyte_p)
17317 {
17318 /* Unibyte case. We don't have to encode, but we have to make
17319 sure to use a face suitable for unibyte. */
17320 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17321 }
17322 else if (glyph->u.ch < 128
17323 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17324 {
17325 /* Case of ASCII in a face known to fit ASCII. */
17326 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17327 }
17328 else
17329 {
17330 int c1, c2, charset;
17331
17332 /* Split characters into bytes. If c2 is -1 afterwards, C is
17333 really a one-byte character so that byte1 is zero. */
17334 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17335 if (c2 > 0)
17336 STORE_XCHAR2B (char2b, c1, c2);
17337 else
17338 STORE_XCHAR2B (char2b, 0, c1);
17339
17340 /* Maybe encode the character in *CHAR2B. */
17341 if (charset != CHARSET_ASCII)
17342 {
17343 struct font_info *font_info
17344 = FONT_INFO_FROM_ID (f, face->font_info_id);
17345 if (font_info)
17346 glyph->font_type
17347 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
17348 }
17349 }
17350
17351 /* Make sure X resources of the face are allocated. */
17352 xassert (face != NULL);
17353 PREPARE_FACE_FOR_DISPLAY (f, face);
17354 return face;
17355 }
17356
17357
17358 /* Fill glyph string S with composition components specified by S->cmp.
17359
17360 FACES is an array of faces for all components of this composition.
17361 S->gidx is the index of the first component for S.
17362 OVERLAPS_P non-zero means S should draw the foreground only, and
17363 use its physical height for clipping.
17364
17365 Value is the index of a component not in S. */
17366
17367 static int
17368 fill_composite_glyph_string (s, faces, overlaps_p)
17369 struct glyph_string *s;
17370 struct face **faces;
17371 int overlaps_p;
17372 {
17373 int i;
17374
17375 xassert (s);
17376
17377 s->for_overlaps_p = overlaps_p;
17378
17379 s->face = faces[s->gidx];
17380 s->font = s->face->font;
17381 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17382
17383 /* For all glyphs of this composition, starting at the offset
17384 S->gidx, until we reach the end of the definition or encounter a
17385 glyph that requires the different face, add it to S. */
17386 ++s->nchars;
17387 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
17388 ++s->nchars;
17389
17390 /* All glyph strings for the same composition has the same width,
17391 i.e. the width set for the first component of the composition. */
17392
17393 s->width = s->first_glyph->pixel_width;
17394
17395 /* If the specified font could not be loaded, use the frame's
17396 default font, but record the fact that we couldn't load it in
17397 the glyph string so that we can draw rectangles for the
17398 characters of the glyph string. */
17399 if (s->font == NULL)
17400 {
17401 s->font_not_found_p = 1;
17402 s->font = FRAME_FONT (s->f);
17403 }
17404
17405 /* Adjust base line for subscript/superscript text. */
17406 s->ybase += s->first_glyph->voffset;
17407
17408 xassert (s->face && s->face->gc);
17409
17410 /* This glyph string must always be drawn with 16-bit functions. */
17411 s->two_byte_p = 1;
17412
17413 return s->gidx + s->nchars;
17414 }
17415
17416
17417 /* Fill glyph string S from a sequence of character glyphs.
17418
17419 FACE_ID is the face id of the string. START is the index of the
17420 first glyph to consider, END is the index of the last + 1.
17421 OVERLAPS_P non-zero means S should draw the foreground only, and
17422 use its physical height for clipping.
17423
17424 Value is the index of the first glyph not in S. */
17425
17426 static int
17427 fill_glyph_string (s, face_id, start, end, overlaps_p)
17428 struct glyph_string *s;
17429 int face_id;
17430 int start, end, overlaps_p;
17431 {
17432 struct glyph *glyph, *last;
17433 int voffset;
17434 int glyph_not_available_p;
17435
17436 xassert (s->f == XFRAME (s->w->frame));
17437 xassert (s->nchars == 0);
17438 xassert (start >= 0 && end > start);
17439
17440 s->for_overlaps_p = overlaps_p,
17441 glyph = s->row->glyphs[s->area] + start;
17442 last = s->row->glyphs[s->area] + end;
17443 voffset = glyph->voffset;
17444
17445 glyph_not_available_p = glyph->glyph_not_available_p;
17446
17447 while (glyph < last
17448 && glyph->type == CHAR_GLYPH
17449 && glyph->voffset == voffset
17450 /* Same face id implies same font, nowadays. */
17451 && glyph->face_id == face_id
17452 && glyph->glyph_not_available_p == glyph_not_available_p)
17453 {
17454 int two_byte_p;
17455
17456 s->face = get_glyph_face_and_encoding (s->f, glyph,
17457 s->char2b + s->nchars,
17458 &two_byte_p);
17459 s->two_byte_p = two_byte_p;
17460 ++s->nchars;
17461 xassert (s->nchars <= end - start);
17462 s->width += glyph->pixel_width;
17463 ++glyph;
17464 }
17465
17466 s->font = s->face->font;
17467 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17468
17469 /* If the specified font could not be loaded, use the frame's font,
17470 but record the fact that we couldn't load it in
17471 S->font_not_found_p so that we can draw rectangles for the
17472 characters of the glyph string. */
17473 if (s->font == NULL || glyph_not_available_p)
17474 {
17475 s->font_not_found_p = 1;
17476 s->font = FRAME_FONT (s->f);
17477 }
17478
17479 /* Adjust base line for subscript/superscript text. */
17480 s->ybase += voffset;
17481
17482 xassert (s->face && s->face->gc);
17483 return glyph - s->row->glyphs[s->area];
17484 }
17485
17486
17487 /* Fill glyph string S from image glyph S->first_glyph. */
17488
17489 static void
17490 fill_image_glyph_string (s)
17491 struct glyph_string *s;
17492 {
17493 xassert (s->first_glyph->type == IMAGE_GLYPH);
17494 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
17495 xassert (s->img);
17496 s->slice = s->first_glyph->slice;
17497 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
17498 s->font = s->face->font;
17499 s->width = s->first_glyph->pixel_width;
17500
17501 /* Adjust base line for subscript/superscript text. */
17502 s->ybase += s->first_glyph->voffset;
17503 }
17504
17505
17506 /* Fill glyph string S from a sequence of stretch glyphs.
17507
17508 ROW is the glyph row in which the glyphs are found, AREA is the
17509 area within the row. START is the index of the first glyph to
17510 consider, END is the index of the last + 1.
17511
17512 Value is the index of the first glyph not in S. */
17513
17514 static int
17515 fill_stretch_glyph_string (s, row, area, start, end)
17516 struct glyph_string *s;
17517 struct glyph_row *row;
17518 enum glyph_row_area area;
17519 int start, end;
17520 {
17521 struct glyph *glyph, *last;
17522 int voffset, face_id;
17523
17524 xassert (s->first_glyph->type == STRETCH_GLYPH);
17525
17526 glyph = s->row->glyphs[s->area] + start;
17527 last = s->row->glyphs[s->area] + end;
17528 face_id = glyph->face_id;
17529 s->face = FACE_FROM_ID (s->f, face_id);
17530 s->font = s->face->font;
17531 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17532 s->width = glyph->pixel_width;
17533 voffset = glyph->voffset;
17534
17535 for (++glyph;
17536 (glyph < last
17537 && glyph->type == STRETCH_GLYPH
17538 && glyph->voffset == voffset
17539 && glyph->face_id == face_id);
17540 ++glyph)
17541 s->width += glyph->pixel_width;
17542
17543 /* Adjust base line for subscript/superscript text. */
17544 s->ybase += voffset;
17545
17546 /* The case that face->gc == 0 is handled when drawing the glyph
17547 string by calling PREPARE_FACE_FOR_DISPLAY. */
17548 xassert (s->face);
17549 return glyph - s->row->glyphs[s->area];
17550 }
17551
17552
17553 /* EXPORT for RIF:
17554 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
17555 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
17556 assumed to be zero. */
17557
17558 void
17559 x_get_glyph_overhangs (glyph, f, left, right)
17560 struct glyph *glyph;
17561 struct frame *f;
17562 int *left, *right;
17563 {
17564 *left = *right = 0;
17565
17566 if (glyph->type == CHAR_GLYPH)
17567 {
17568 XFontStruct *font;
17569 struct face *face;
17570 struct font_info *font_info;
17571 XChar2b char2b;
17572 XCharStruct *pcm;
17573
17574 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
17575 font = face->font;
17576 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
17577 if (font /* ++KFS: Should this be font_info ? */
17578 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
17579 {
17580 if (pcm->rbearing > pcm->width)
17581 *right = pcm->rbearing - pcm->width;
17582 if (pcm->lbearing < 0)
17583 *left = -pcm->lbearing;
17584 }
17585 }
17586 }
17587
17588
17589 /* Return the index of the first glyph preceding glyph string S that
17590 is overwritten by S because of S's left overhang. Value is -1
17591 if no glyphs are overwritten. */
17592
17593 static int
17594 left_overwritten (s)
17595 struct glyph_string *s;
17596 {
17597 int k;
17598
17599 if (s->left_overhang)
17600 {
17601 int x = 0, i;
17602 struct glyph *glyphs = s->row->glyphs[s->area];
17603 int first = s->first_glyph - glyphs;
17604
17605 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
17606 x -= glyphs[i].pixel_width;
17607
17608 k = i + 1;
17609 }
17610 else
17611 k = -1;
17612
17613 return k;
17614 }
17615
17616
17617 /* Return the index of the first glyph preceding glyph string S that
17618 is overwriting S because of its right overhang. Value is -1 if no
17619 glyph in front of S overwrites S. */
17620
17621 static int
17622 left_overwriting (s)
17623 struct glyph_string *s;
17624 {
17625 int i, k, x;
17626 struct glyph *glyphs = s->row->glyphs[s->area];
17627 int first = s->first_glyph - glyphs;
17628
17629 k = -1;
17630 x = 0;
17631 for (i = first - 1; i >= 0; --i)
17632 {
17633 int left, right;
17634 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17635 if (x + right > 0)
17636 k = i;
17637 x -= glyphs[i].pixel_width;
17638 }
17639
17640 return k;
17641 }
17642
17643
17644 /* Return the index of the last glyph following glyph string S that is
17645 not overwritten by S because of S's right overhang. Value is -1 if
17646 no such glyph is found. */
17647
17648 static int
17649 right_overwritten (s)
17650 struct glyph_string *s;
17651 {
17652 int k = -1;
17653
17654 if (s->right_overhang)
17655 {
17656 int x = 0, i;
17657 struct glyph *glyphs = s->row->glyphs[s->area];
17658 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17659 int end = s->row->used[s->area];
17660
17661 for (i = first; i < end && s->right_overhang > x; ++i)
17662 x += glyphs[i].pixel_width;
17663
17664 k = i;
17665 }
17666
17667 return k;
17668 }
17669
17670
17671 /* Return the index of the last glyph following glyph string S that
17672 overwrites S because of its left overhang. Value is negative
17673 if no such glyph is found. */
17674
17675 static int
17676 right_overwriting (s)
17677 struct glyph_string *s;
17678 {
17679 int i, k, x;
17680 int end = s->row->used[s->area];
17681 struct glyph *glyphs = s->row->glyphs[s->area];
17682 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17683
17684 k = -1;
17685 x = 0;
17686 for (i = first; i < end; ++i)
17687 {
17688 int left, right;
17689 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17690 if (x - left < 0)
17691 k = i;
17692 x += glyphs[i].pixel_width;
17693 }
17694
17695 return k;
17696 }
17697
17698
17699 /* Get face and two-byte form of character C in face FACE_ID on frame
17700 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
17701 means we want to display multibyte text. DISPLAY_P non-zero means
17702 make sure that X resources for the face returned are allocated.
17703 Value is a pointer to a realized face that is ready for display if
17704 DISPLAY_P is non-zero. */
17705
17706 static INLINE struct face *
17707 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
17708 struct frame *f;
17709 int c, face_id;
17710 XChar2b *char2b;
17711 int multibyte_p, display_p;
17712 {
17713 struct face *face = FACE_FROM_ID (f, face_id);
17714
17715 if (!multibyte_p)
17716 {
17717 /* Unibyte case. We don't have to encode, but we have to make
17718 sure to use a face suitable for unibyte. */
17719 STORE_XCHAR2B (char2b, 0, c);
17720 face_id = FACE_FOR_CHAR (f, face, c);
17721 face = FACE_FROM_ID (f, face_id);
17722 }
17723 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
17724 {
17725 /* Case of ASCII in a face known to fit ASCII. */
17726 STORE_XCHAR2B (char2b, 0, c);
17727 }
17728 else
17729 {
17730 int c1, c2, charset;
17731
17732 /* Split characters into bytes. If c2 is -1 afterwards, C is
17733 really a one-byte character so that byte1 is zero. */
17734 SPLIT_CHAR (c, charset, c1, c2);
17735 if (c2 > 0)
17736 STORE_XCHAR2B (char2b, c1, c2);
17737 else
17738 STORE_XCHAR2B (char2b, 0, c1);
17739
17740 /* Maybe encode the character in *CHAR2B. */
17741 if (face->font != NULL)
17742 {
17743 struct font_info *font_info
17744 = FONT_INFO_FROM_ID (f, face->font_info_id);
17745 if (font_info)
17746 rif->encode_char (c, char2b, font_info, 0);
17747 }
17748 }
17749
17750 /* Make sure X resources of the face are allocated. */
17751 #ifdef HAVE_X_WINDOWS
17752 if (display_p)
17753 #endif
17754 {
17755 xassert (face != NULL);
17756 PREPARE_FACE_FOR_DISPLAY (f, face);
17757 }
17758
17759 return face;
17760 }
17761
17762
17763 /* Set background width of glyph string S. START is the index of the
17764 first glyph following S. LAST_X is the right-most x-position + 1
17765 in the drawing area. */
17766
17767 static INLINE void
17768 set_glyph_string_background_width (s, start, last_x)
17769 struct glyph_string *s;
17770 int start;
17771 int last_x;
17772 {
17773 /* If the face of this glyph string has to be drawn to the end of
17774 the drawing area, set S->extends_to_end_of_line_p. */
17775 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
17776
17777 if (start == s->row->used[s->area]
17778 && s->area == TEXT_AREA
17779 && ((s->hl == DRAW_NORMAL_TEXT
17780 && (s->row->fill_line_p
17781 || s->face->background != default_face->background
17782 || s->face->stipple != default_face->stipple
17783 || s->row->mouse_face_p))
17784 || s->hl == DRAW_MOUSE_FACE
17785 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
17786 && s->row->fill_line_p)))
17787 s->extends_to_end_of_line_p = 1;
17788
17789 /* If S extends its face to the end of the line, set its
17790 background_width to the distance to the right edge of the drawing
17791 area. */
17792 if (s->extends_to_end_of_line_p)
17793 s->background_width = last_x - s->x + 1;
17794 else
17795 s->background_width = s->width;
17796 }
17797
17798
17799 /* Compute overhangs and x-positions for glyph string S and its
17800 predecessors, or successors. X is the starting x-position for S.
17801 BACKWARD_P non-zero means process predecessors. */
17802
17803 static void
17804 compute_overhangs_and_x (s, x, backward_p)
17805 struct glyph_string *s;
17806 int x;
17807 int backward_p;
17808 {
17809 if (backward_p)
17810 {
17811 while (s)
17812 {
17813 if (rif->compute_glyph_string_overhangs)
17814 rif->compute_glyph_string_overhangs (s);
17815 x -= s->width;
17816 s->x = x;
17817 s = s->prev;
17818 }
17819 }
17820 else
17821 {
17822 while (s)
17823 {
17824 if (rif->compute_glyph_string_overhangs)
17825 rif->compute_glyph_string_overhangs (s);
17826 s->x = x;
17827 x += s->width;
17828 s = s->next;
17829 }
17830 }
17831 }
17832
17833
17834
17835 /* The following macros are only called from draw_glyphs below.
17836 They reference the following parameters of that function directly:
17837 `w', `row', `area', and `overlap_p'
17838 as well as the following local variables:
17839 `s', `f', and `hdc' (in W32) */
17840
17841 #ifdef HAVE_NTGUI
17842 /* On W32, silently add local `hdc' variable to argument list of
17843 init_glyph_string. */
17844 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17845 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
17846 #else
17847 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17848 init_glyph_string (s, char2b, w, row, area, start, hl)
17849 #endif
17850
17851 /* Add a glyph string for a stretch glyph to the list of strings
17852 between HEAD and TAIL. START is the index of the stretch glyph in
17853 row area AREA of glyph row ROW. END is the index of the last glyph
17854 in that glyph row area. X is the current output position assigned
17855 to the new glyph string constructed. HL overrides that face of the
17856 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17857 is the right-most x-position of the drawing area. */
17858
17859 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
17860 and below -- keep them on one line. */
17861 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17862 do \
17863 { \
17864 s = (struct glyph_string *) alloca (sizeof *s); \
17865 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17866 START = fill_stretch_glyph_string (s, row, area, START, END); \
17867 append_glyph_string (&HEAD, &TAIL, s); \
17868 s->x = (X); \
17869 } \
17870 while (0)
17871
17872
17873 /* Add a glyph string for an image glyph to the list of strings
17874 between HEAD and TAIL. START is the index of the image glyph in
17875 row area AREA of glyph row ROW. END is the index of the last glyph
17876 in that glyph row area. X is the current output position assigned
17877 to the new glyph string constructed. HL overrides that face of the
17878 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17879 is the right-most x-position of the drawing area. */
17880
17881 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17882 do \
17883 { \
17884 s = (struct glyph_string *) alloca (sizeof *s); \
17885 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17886 fill_image_glyph_string (s); \
17887 append_glyph_string (&HEAD, &TAIL, s); \
17888 ++START; \
17889 s->x = (X); \
17890 } \
17891 while (0)
17892
17893
17894 /* Add a glyph string for a sequence of character glyphs to the list
17895 of strings between HEAD and TAIL. START is the index of the first
17896 glyph in row area AREA of glyph row ROW that is part of the new
17897 glyph string. END is the index of the last glyph in that glyph row
17898 area. X is the current output position assigned to the new glyph
17899 string constructed. HL overrides that face of the glyph; e.g. it
17900 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
17901 right-most x-position of the drawing area. */
17902
17903 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17904 do \
17905 { \
17906 int c, face_id; \
17907 XChar2b *char2b; \
17908 \
17909 c = (row)->glyphs[area][START].u.ch; \
17910 face_id = (row)->glyphs[area][START].face_id; \
17911 \
17912 s = (struct glyph_string *) alloca (sizeof *s); \
17913 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
17914 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
17915 append_glyph_string (&HEAD, &TAIL, s); \
17916 s->x = (X); \
17917 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
17918 } \
17919 while (0)
17920
17921
17922 /* Add a glyph string for a composite sequence to the list of strings
17923 between HEAD and TAIL. START is the index of the first glyph in
17924 row area AREA of glyph row ROW that is part of the new glyph
17925 string. END is the index of the last glyph in that glyph row area.
17926 X is the current output position assigned to the new glyph string
17927 constructed. HL overrides that face of the glyph; e.g. it is
17928 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
17929 x-position of the drawing area. */
17930
17931 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17932 do { \
17933 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
17934 int face_id = (row)->glyphs[area][START].face_id; \
17935 struct face *base_face = FACE_FROM_ID (f, face_id); \
17936 struct composition *cmp = composition_table[cmp_id]; \
17937 int glyph_len = cmp->glyph_len; \
17938 XChar2b *char2b; \
17939 struct face **faces; \
17940 struct glyph_string *first_s = NULL; \
17941 int n; \
17942 \
17943 base_face = base_face->ascii_face; \
17944 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
17945 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
17946 /* At first, fill in `char2b' and `faces'. */ \
17947 for (n = 0; n < glyph_len; n++) \
17948 { \
17949 int c = COMPOSITION_GLYPH (cmp, n); \
17950 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
17951 faces[n] = FACE_FROM_ID (f, this_face_id); \
17952 get_char_face_and_encoding (f, c, this_face_id, \
17953 char2b + n, 1, 1); \
17954 } \
17955 \
17956 /* Make glyph_strings for each glyph sequence that is drawable by \
17957 the same face, and append them to HEAD/TAIL. */ \
17958 for (n = 0; n < cmp->glyph_len;) \
17959 { \
17960 s = (struct glyph_string *) alloca (sizeof *s); \
17961 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
17962 append_glyph_string (&(HEAD), &(TAIL), s); \
17963 s->cmp = cmp; \
17964 s->gidx = n; \
17965 s->x = (X); \
17966 \
17967 if (n == 0) \
17968 first_s = s; \
17969 \
17970 n = fill_composite_glyph_string (s, faces, overlaps_p); \
17971 } \
17972 \
17973 ++START; \
17974 s = first_s; \
17975 } while (0)
17976
17977
17978 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
17979 of AREA of glyph row ROW on window W between indices START and END.
17980 HL overrides the face for drawing glyph strings, e.g. it is
17981 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
17982 x-positions of the drawing area.
17983
17984 This is an ugly monster macro construct because we must use alloca
17985 to allocate glyph strings (because draw_glyphs can be called
17986 asynchronously). */
17987
17988 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17989 do \
17990 { \
17991 HEAD = TAIL = NULL; \
17992 while (START < END) \
17993 { \
17994 struct glyph *first_glyph = (row)->glyphs[area] + START; \
17995 switch (first_glyph->type) \
17996 { \
17997 case CHAR_GLYPH: \
17998 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
17999 HL, X, LAST_X); \
18000 break; \
18001 \
18002 case COMPOSITE_GLYPH: \
18003 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
18004 HL, X, LAST_X); \
18005 break; \
18006 \
18007 case STRETCH_GLYPH: \
18008 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
18009 HL, X, LAST_X); \
18010 break; \
18011 \
18012 case IMAGE_GLYPH: \
18013 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
18014 HL, X, LAST_X); \
18015 break; \
18016 \
18017 default: \
18018 abort (); \
18019 } \
18020 \
18021 set_glyph_string_background_width (s, START, LAST_X); \
18022 (X) += s->width; \
18023 } \
18024 } \
18025 while (0)
18026
18027
18028 /* Draw glyphs between START and END in AREA of ROW on window W,
18029 starting at x-position X. X is relative to AREA in W. HL is a
18030 face-override with the following meaning:
18031
18032 DRAW_NORMAL_TEXT draw normally
18033 DRAW_CURSOR draw in cursor face
18034 DRAW_MOUSE_FACE draw in mouse face.
18035 DRAW_INVERSE_VIDEO draw in mode line face
18036 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
18037 DRAW_IMAGE_RAISED draw an image with a raised relief around it
18038
18039 If OVERLAPS_P is non-zero, draw only the foreground of characters
18040 and clip to the physical height of ROW.
18041
18042 Value is the x-position reached, relative to AREA of W. */
18043
18044 static int
18045 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
18046 struct window *w;
18047 int x;
18048 struct glyph_row *row;
18049 enum glyph_row_area area;
18050 int start, end;
18051 enum draw_glyphs_face hl;
18052 int overlaps_p;
18053 {
18054 struct glyph_string *head, *tail;
18055 struct glyph_string *s;
18056 int last_x, area_width;
18057 int x_reached;
18058 int i, j;
18059 struct frame *f = XFRAME (WINDOW_FRAME (w));
18060 DECLARE_HDC (hdc);
18061
18062 ALLOCATE_HDC (hdc, f);
18063
18064 /* Let's rather be paranoid than getting a SEGV. */
18065 end = min (end, row->used[area]);
18066 start = max (0, start);
18067 start = min (end, start);
18068
18069 /* Translate X to frame coordinates. Set last_x to the right
18070 end of the drawing area. */
18071 if (row->full_width_p)
18072 {
18073 /* X is relative to the left edge of W, without scroll bars
18074 or fringes. */
18075 x += WINDOW_LEFT_EDGE_X (w);
18076 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
18077 }
18078 else
18079 {
18080 int area_left = window_box_left (w, area);
18081 x += area_left;
18082 area_width = window_box_width (w, area);
18083 last_x = area_left + area_width;
18084 }
18085
18086 /* Build a doubly-linked list of glyph_string structures between
18087 head and tail from what we have to draw. Note that the macro
18088 BUILD_GLYPH_STRINGS will modify its start parameter. That's
18089 the reason we use a separate variable `i'. */
18090 i = start;
18091 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
18092 if (tail)
18093 x_reached = tail->x + tail->background_width;
18094 else
18095 x_reached = x;
18096
18097 /* If there are any glyphs with lbearing < 0 or rbearing > width in
18098 the row, redraw some glyphs in front or following the glyph
18099 strings built above. */
18100 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
18101 {
18102 int dummy_x = 0;
18103 struct glyph_string *h, *t;
18104
18105 /* Compute overhangs for all glyph strings. */
18106 if (rif->compute_glyph_string_overhangs)
18107 for (s = head; s; s = s->next)
18108 rif->compute_glyph_string_overhangs (s);
18109
18110 /* Prepend glyph strings for glyphs in front of the first glyph
18111 string that are overwritten because of the first glyph
18112 string's left overhang. The background of all strings
18113 prepended must be drawn because the first glyph string
18114 draws over it. */
18115 i = left_overwritten (head);
18116 if (i >= 0)
18117 {
18118 j = i;
18119 BUILD_GLYPH_STRINGS (j, start, h, t,
18120 DRAW_NORMAL_TEXT, dummy_x, last_x);
18121 start = i;
18122 compute_overhangs_and_x (t, head->x, 1);
18123 prepend_glyph_string_lists (&head, &tail, h, t);
18124 }
18125
18126 /* Prepend glyph strings for glyphs in front of the first glyph
18127 string that overwrite that glyph string because of their
18128 right overhang. For these strings, only the foreground must
18129 be drawn, because it draws over the glyph string at `head'.
18130 The background must not be drawn because this would overwrite
18131 right overhangs of preceding glyphs for which no glyph
18132 strings exist. */
18133 i = left_overwriting (head);
18134 if (i >= 0)
18135 {
18136 BUILD_GLYPH_STRINGS (i, start, h, t,
18137 DRAW_NORMAL_TEXT, dummy_x, last_x);
18138 for (s = h; s; s = s->next)
18139 s->background_filled_p = 1;
18140 compute_overhangs_and_x (t, head->x, 1);
18141 prepend_glyph_string_lists (&head, &tail, h, t);
18142 }
18143
18144 /* Append glyphs strings for glyphs following the last glyph
18145 string tail that are overwritten by tail. The background of
18146 these strings has to be drawn because tail's foreground draws
18147 over it. */
18148 i = right_overwritten (tail);
18149 if (i >= 0)
18150 {
18151 BUILD_GLYPH_STRINGS (end, i, h, t,
18152 DRAW_NORMAL_TEXT, x, last_x);
18153 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18154 append_glyph_string_lists (&head, &tail, h, t);
18155 }
18156
18157 /* Append glyph strings for glyphs following the last glyph
18158 string tail that overwrite tail. The foreground of such
18159 glyphs has to be drawn because it writes into the background
18160 of tail. The background must not be drawn because it could
18161 paint over the foreground of following glyphs. */
18162 i = right_overwriting (tail);
18163 if (i >= 0)
18164 {
18165 BUILD_GLYPH_STRINGS (end, i, h, t,
18166 DRAW_NORMAL_TEXT, x, last_x);
18167 for (s = h; s; s = s->next)
18168 s->background_filled_p = 1;
18169 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18170 append_glyph_string_lists (&head, &tail, h, t);
18171 }
18172 }
18173
18174 /* Draw all strings. */
18175 for (s = head; s; s = s->next)
18176 rif->draw_glyph_string (s);
18177
18178 if (area == TEXT_AREA
18179 && !row->full_width_p
18180 /* When drawing overlapping rows, only the glyph strings'
18181 foreground is drawn, which doesn't erase a cursor
18182 completely. */
18183 && !overlaps_p)
18184 {
18185 int x0 = head ? head->x : x;
18186 int x1 = tail ? tail->x + tail->background_width : x;
18187
18188 int text_left = window_box_left (w, TEXT_AREA);
18189 x0 -= text_left;
18190 x1 -= text_left;
18191
18192 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18193 row->y, MATRIX_ROW_BOTTOM_Y (row));
18194 }
18195
18196 /* Value is the x-position up to which drawn, relative to AREA of W.
18197 This doesn't include parts drawn because of overhangs. */
18198 if (row->full_width_p)
18199 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18200 else
18201 x_reached -= window_box_left (w, area);
18202
18203 RELEASE_HDC (hdc, f);
18204
18205 return x_reached;
18206 }
18207
18208
18209 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18210 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18211
18212 static INLINE void
18213 append_glyph (it)
18214 struct it *it;
18215 {
18216 struct glyph *glyph;
18217 enum glyph_row_area area = it->area;
18218
18219 xassert (it->glyph_row);
18220 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18221
18222 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18223 if (glyph < it->glyph_row->glyphs[area + 1])
18224 {
18225 glyph->charpos = CHARPOS (it->position);
18226 glyph->object = it->object;
18227 glyph->pixel_width = it->pixel_width;
18228 glyph->ascent = it->ascent;
18229 glyph->descent = it->descent;
18230 glyph->voffset = it->voffset;
18231 glyph->type = CHAR_GLYPH;
18232 glyph->multibyte_p = it->multibyte_p;
18233 glyph->left_box_line_p = it->start_of_box_run_p;
18234 glyph->right_box_line_p = it->end_of_box_run_p;
18235 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18236 || it->phys_descent > it->descent);
18237 glyph->padding_p = 0;
18238 glyph->glyph_not_available_p = it->glyph_not_available_p;
18239 glyph->face_id = it->face_id;
18240 glyph->u.ch = it->char_to_display;
18241 glyph->slice = null_glyph_slice;
18242 glyph->font_type = FONT_TYPE_UNKNOWN;
18243 ++it->glyph_row->used[area];
18244 }
18245 else if (!fonts_changed_p)
18246 {
18247 it->w->ncols_scale_factor++;
18248 fonts_changed_p = 1;
18249 }
18250 }
18251
18252 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18253 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18254
18255 static INLINE void
18256 append_composite_glyph (it)
18257 struct it *it;
18258 {
18259 struct glyph *glyph;
18260 enum glyph_row_area area = it->area;
18261
18262 xassert (it->glyph_row);
18263
18264 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18265 if (glyph < it->glyph_row->glyphs[area + 1])
18266 {
18267 glyph->charpos = CHARPOS (it->position);
18268 glyph->object = it->object;
18269 glyph->pixel_width = it->pixel_width;
18270 glyph->ascent = it->ascent;
18271 glyph->descent = it->descent;
18272 glyph->voffset = it->voffset;
18273 glyph->type = COMPOSITE_GLYPH;
18274 glyph->multibyte_p = it->multibyte_p;
18275 glyph->left_box_line_p = it->start_of_box_run_p;
18276 glyph->right_box_line_p = it->end_of_box_run_p;
18277 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18278 || it->phys_descent > it->descent);
18279 glyph->padding_p = 0;
18280 glyph->glyph_not_available_p = 0;
18281 glyph->face_id = it->face_id;
18282 glyph->u.cmp_id = it->cmp_id;
18283 glyph->slice = null_glyph_slice;
18284 glyph->font_type = FONT_TYPE_UNKNOWN;
18285 ++it->glyph_row->used[area];
18286 }
18287 else if (!fonts_changed_p)
18288 {
18289 it->w->ncols_scale_factor++;
18290 fonts_changed_p = 1;
18291 }
18292 }
18293
18294
18295 /* Change IT->ascent and IT->height according to the setting of
18296 IT->voffset. */
18297
18298 static INLINE void
18299 take_vertical_position_into_account (it)
18300 struct it *it;
18301 {
18302 if (it->voffset)
18303 {
18304 if (it->voffset < 0)
18305 /* Increase the ascent so that we can display the text higher
18306 in the line. */
18307 it->ascent -= it->voffset;
18308 else
18309 /* Increase the descent so that we can display the text lower
18310 in the line. */
18311 it->descent += it->voffset;
18312 }
18313 }
18314
18315
18316 /* Produce glyphs/get display metrics for the image IT is loaded with.
18317 See the description of struct display_iterator in dispextern.h for
18318 an overview of struct display_iterator. */
18319
18320 static void
18321 produce_image_glyph (it)
18322 struct it *it;
18323 {
18324 struct image *img;
18325 struct face *face;
18326 int face_ascent, glyph_ascent;
18327 struct glyph_slice slice;
18328
18329 xassert (it->what == IT_IMAGE);
18330
18331 face = FACE_FROM_ID (it->f, it->face_id);
18332 xassert (face);
18333 /* Make sure X resources of the face is loaded. */
18334 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18335
18336 if (it->image_id < 0)
18337 {
18338 /* Fringe bitmap. */
18339 it->ascent = it->phys_ascent = 0;
18340 it->descent = it->phys_descent = 0;
18341 it->pixel_width = 0;
18342 it->nglyphs = 0;
18343 return;
18344 }
18345
18346 img = IMAGE_FROM_ID (it->f, it->image_id);
18347 xassert (img);
18348 /* Make sure X resources of the image is loaded. */
18349 prepare_image_for_display (it->f, img);
18350
18351 slice.x = slice.y = 0;
18352 slice.width = img->width;
18353 slice.height = img->height;
18354
18355 if (INTEGERP (it->slice.x))
18356 slice.x = XINT (it->slice.x);
18357 else if (FLOATP (it->slice.x))
18358 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
18359
18360 if (INTEGERP (it->slice.y))
18361 slice.y = XINT (it->slice.y);
18362 else if (FLOATP (it->slice.y))
18363 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
18364
18365 if (INTEGERP (it->slice.width))
18366 slice.width = XINT (it->slice.width);
18367 else if (FLOATP (it->slice.width))
18368 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
18369
18370 if (INTEGERP (it->slice.height))
18371 slice.height = XINT (it->slice.height);
18372 else if (FLOATP (it->slice.height))
18373 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
18374
18375 if (slice.x >= img->width)
18376 slice.x = img->width;
18377 if (slice.y >= img->height)
18378 slice.y = img->height;
18379 if (slice.x + slice.width >= img->width)
18380 slice.width = img->width - slice.x;
18381 if (slice.y + slice.height > img->height)
18382 slice.height = img->height - slice.y;
18383
18384 if (slice.width == 0 || slice.height == 0)
18385 return;
18386
18387 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
18388
18389 it->descent = slice.height - glyph_ascent;
18390 if (slice.y == 0)
18391 it->descent += img->vmargin;
18392 if (slice.y + slice.height == img->height)
18393 it->descent += img->vmargin;
18394 it->phys_descent = it->descent;
18395
18396 it->pixel_width = slice.width;
18397 if (slice.x == 0)
18398 it->pixel_width += img->hmargin;
18399 if (slice.x + slice.width == img->width)
18400 it->pixel_width += img->hmargin;
18401
18402 /* It's quite possible for images to have an ascent greater than
18403 their height, so don't get confused in that case. */
18404 if (it->descent < 0)
18405 it->descent = 0;
18406
18407 #if 0 /* this breaks image tiling */
18408 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
18409 face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
18410 if (face_ascent > it->ascent)
18411 it->ascent = it->phys_ascent = face_ascent;
18412 #endif
18413
18414 it->nglyphs = 1;
18415
18416 if (face->box != FACE_NO_BOX)
18417 {
18418 if (face->box_line_width > 0)
18419 {
18420 if (slice.y == 0)
18421 it->ascent += face->box_line_width;
18422 if (slice.y + slice.height == img->height)
18423 it->descent += face->box_line_width;
18424 }
18425
18426 if (it->start_of_box_run_p && slice.x == 0)
18427 it->pixel_width += abs (face->box_line_width);
18428 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
18429 it->pixel_width += abs (face->box_line_width);
18430 }
18431
18432 take_vertical_position_into_account (it);
18433
18434 if (it->glyph_row)
18435 {
18436 struct glyph *glyph;
18437 enum glyph_row_area area = it->area;
18438
18439 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18440 if (glyph < it->glyph_row->glyphs[area + 1])
18441 {
18442 glyph->charpos = CHARPOS (it->position);
18443 glyph->object = it->object;
18444 glyph->pixel_width = it->pixel_width;
18445 glyph->ascent = glyph_ascent;
18446 glyph->descent = it->descent;
18447 glyph->voffset = it->voffset;
18448 glyph->type = IMAGE_GLYPH;
18449 glyph->multibyte_p = it->multibyte_p;
18450 glyph->left_box_line_p = it->start_of_box_run_p;
18451 glyph->right_box_line_p = it->end_of_box_run_p;
18452 glyph->overlaps_vertically_p = 0;
18453 glyph->padding_p = 0;
18454 glyph->glyph_not_available_p = 0;
18455 glyph->face_id = it->face_id;
18456 glyph->u.img_id = img->id;
18457 glyph->slice = slice;
18458 glyph->font_type = FONT_TYPE_UNKNOWN;
18459 ++it->glyph_row->used[area];
18460 }
18461 else if (!fonts_changed_p)
18462 {
18463 it->w->ncols_scale_factor++;
18464 fonts_changed_p = 1;
18465 }
18466 }
18467 }
18468
18469
18470 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
18471 of the glyph, WIDTH and HEIGHT are the width and height of the
18472 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
18473
18474 static void
18475 append_stretch_glyph (it, object, width, height, ascent)
18476 struct it *it;
18477 Lisp_Object object;
18478 int width, height;
18479 int ascent;
18480 {
18481 struct glyph *glyph;
18482 enum glyph_row_area area = it->area;
18483
18484 xassert (ascent >= 0 && ascent <= height);
18485
18486 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18487 if (glyph < it->glyph_row->glyphs[area + 1])
18488 {
18489 glyph->charpos = CHARPOS (it->position);
18490 glyph->object = object;
18491 glyph->pixel_width = width;
18492 glyph->ascent = ascent;
18493 glyph->descent = height - ascent;
18494 glyph->voffset = it->voffset;
18495 glyph->type = STRETCH_GLYPH;
18496 glyph->multibyte_p = it->multibyte_p;
18497 glyph->left_box_line_p = it->start_of_box_run_p;
18498 glyph->right_box_line_p = it->end_of_box_run_p;
18499 glyph->overlaps_vertically_p = 0;
18500 glyph->padding_p = 0;
18501 glyph->glyph_not_available_p = 0;
18502 glyph->face_id = it->face_id;
18503 glyph->u.stretch.ascent = ascent;
18504 glyph->u.stretch.height = height;
18505 glyph->slice = null_glyph_slice;
18506 glyph->font_type = FONT_TYPE_UNKNOWN;
18507 ++it->glyph_row->used[area];
18508 }
18509 else if (!fonts_changed_p)
18510 {
18511 it->w->ncols_scale_factor++;
18512 fonts_changed_p = 1;
18513 }
18514 }
18515
18516
18517 /* Produce a stretch glyph for iterator IT. IT->object is the value
18518 of the glyph property displayed. The value must be a list
18519 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
18520 being recognized:
18521
18522 1. `:width WIDTH' specifies that the space should be WIDTH *
18523 canonical char width wide. WIDTH may be an integer or floating
18524 point number.
18525
18526 2. `:relative-width FACTOR' specifies that the width of the stretch
18527 should be computed from the width of the first character having the
18528 `glyph' property, and should be FACTOR times that width.
18529
18530 3. `:align-to HPOS' specifies that the space should be wide enough
18531 to reach HPOS, a value in canonical character units.
18532
18533 Exactly one of the above pairs must be present.
18534
18535 4. `:height HEIGHT' specifies that the height of the stretch produced
18536 should be HEIGHT, measured in canonical character units.
18537
18538 5. `:relative-height FACTOR' specifies that the height of the
18539 stretch should be FACTOR times the height of the characters having
18540 the glyph property.
18541
18542 Either none or exactly one of 4 or 5 must be present.
18543
18544 6. `:ascent ASCENT' specifies that ASCENT percent of the height
18545 of the stretch should be used for the ascent of the stretch.
18546 ASCENT must be in the range 0 <= ASCENT <= 100. */
18547
18548 static void
18549 produce_stretch_glyph (it)
18550 struct it *it;
18551 {
18552 /* (space :width WIDTH :height HEIGHT ...) */
18553 Lisp_Object prop, plist;
18554 int width = 0, height = 0, align_to = -1;
18555 int zero_width_ok_p = 0, zero_height_ok_p = 0;
18556 int ascent = 0;
18557 double tem;
18558 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18559 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
18560
18561 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18562
18563 /* List should start with `space'. */
18564 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
18565 plist = XCDR (it->object);
18566
18567 /* Compute the width of the stretch. */
18568 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
18569 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
18570 {
18571 /* Absolute width `:width WIDTH' specified and valid. */
18572 zero_width_ok_p = 1;
18573 width = (int)tem;
18574 }
18575 else if (prop = Fplist_get (plist, QCrelative_width),
18576 NUMVAL (prop) > 0)
18577 {
18578 /* Relative width `:relative-width FACTOR' specified and valid.
18579 Compute the width of the characters having the `glyph'
18580 property. */
18581 struct it it2;
18582 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
18583
18584 it2 = *it;
18585 if (it->multibyte_p)
18586 {
18587 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
18588 - IT_BYTEPOS (*it));
18589 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
18590 }
18591 else
18592 it2.c = *p, it2.len = 1;
18593
18594 it2.glyph_row = NULL;
18595 it2.what = IT_CHARACTER;
18596 x_produce_glyphs (&it2);
18597 width = NUMVAL (prop) * it2.pixel_width;
18598 }
18599 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
18600 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
18601 {
18602 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
18603 align_to = (align_to < 0
18604 ? 0
18605 : align_to - window_box_left_offset (it->w, TEXT_AREA));
18606 else if (align_to < 0)
18607 align_to = window_box_left_offset (it->w, TEXT_AREA);
18608 width = max (0, (int)tem + align_to - it->current_x);
18609 zero_width_ok_p = 1;
18610 }
18611 else
18612 /* Nothing specified -> width defaults to canonical char width. */
18613 width = FRAME_COLUMN_WIDTH (it->f);
18614
18615 if (width <= 0 && (width < 0 || !zero_width_ok_p))
18616 width = 1;
18617
18618 /* Compute height. */
18619 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
18620 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18621 {
18622 height = (int)tem;
18623 zero_height_ok_p = 1;
18624 }
18625 else if (prop = Fplist_get (plist, QCrelative_height),
18626 NUMVAL (prop) > 0)
18627 height = FONT_HEIGHT (font) * NUMVAL (prop);
18628 else
18629 height = FONT_HEIGHT (font);
18630
18631 if (height <= 0 && (height < 0 || !zero_height_ok_p))
18632 height = 1;
18633
18634 /* Compute percentage of height used for ascent. If
18635 `:ascent ASCENT' is present and valid, use that. Otherwise,
18636 derive the ascent from the font in use. */
18637 if (prop = Fplist_get (plist, QCascent),
18638 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
18639 ascent = height * NUMVAL (prop) / 100.0;
18640 else if (!NILP (prop)
18641 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18642 ascent = min (max (0, (int)tem), height);
18643 else
18644 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
18645
18646 if (width > 0 && height > 0 && it->glyph_row)
18647 {
18648 Lisp_Object object = it->stack[it->sp - 1].string;
18649 if (!STRINGP (object))
18650 object = it->w->buffer;
18651 append_stretch_glyph (it, object, width, height, ascent);
18652 }
18653
18654 it->pixel_width = width;
18655 it->ascent = it->phys_ascent = ascent;
18656 it->descent = it->phys_descent = height - it->ascent;
18657 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
18658
18659 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
18660 {
18661 if (face->box_line_width > 0)
18662 {
18663 it->ascent += face->box_line_width;
18664 it->descent += face->box_line_width;
18665 }
18666
18667 if (it->start_of_box_run_p)
18668 it->pixel_width += abs (face->box_line_width);
18669 if (it->end_of_box_run_p)
18670 it->pixel_width += abs (face->box_line_width);
18671 }
18672
18673 take_vertical_position_into_account (it);
18674 }
18675
18676 /* Calculate line-height and line-spacing properties.
18677 An integer value specifies explicit pixel value.
18678 A float value specifies relative value to current face height.
18679 A cons (float . face-name) specifies relative value to
18680 height of specified face font.
18681
18682 Returns height in pixels, or nil. */
18683
18684 static Lisp_Object
18685 calc_line_height_property (it, prop, font, boff, total)
18686 struct it *it;
18687 Lisp_Object prop;
18688 XFontStruct *font;
18689 int boff, *total;
18690 {
18691 Lisp_Object position, val;
18692 Lisp_Object face_name = Qnil;
18693 int ascent, descent, height, override;
18694
18695 if (STRINGP (it->object))
18696 position = make_number (IT_STRING_CHARPOS (*it));
18697 else
18698 position = make_number (IT_CHARPOS (*it));
18699
18700 val = Fget_char_property (position, prop, it->object);
18701
18702 if (NILP (val))
18703 return val;
18704
18705 if (total && CONSP (val) && EQ (XCAR (val), Qtotal))
18706 {
18707 *total = 1;
18708 val = XCDR (val);
18709 }
18710
18711 if (INTEGERP (val))
18712 return val;
18713
18714 if (CONSP (val))
18715 {
18716 face_name = XCDR (val);
18717 val = XCAR (val);
18718 }
18719 else if (SYMBOLP (val))
18720 {
18721 face_name = val;
18722 val = Qnil;
18723 }
18724
18725 override = EQ (prop, Qline_height);
18726
18727 if (NILP (face_name))
18728 {
18729 font = FRAME_FONT (it->f);
18730 boff = FRAME_BASELINE_OFFSET (it->f);
18731 }
18732 else if (EQ (face_name, Qt))
18733 {
18734 override = 0;
18735 }
18736 else
18737 {
18738 int face_id;
18739 struct face *face;
18740 struct font_info *font_info;
18741
18742 face_id = lookup_named_face (it->f, face_name, ' ');
18743 if (face_id < 0)
18744 return make_number (-1);
18745
18746 face = FACE_FROM_ID (it->f, face_id);
18747 font = face->font;
18748 if (font == NULL)
18749 return make_number (-1);
18750
18751 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18752 boff = font_info->baseline_offset;
18753 if (font_info->vertical_centering)
18754 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18755 }
18756
18757 ascent = FONT_BASE (font) + boff;
18758 descent = FONT_DESCENT (font) - boff;
18759
18760 if (override)
18761 {
18762 it->override_ascent = ascent;
18763 it->override_descent = descent;
18764 it->override_boff = boff;
18765 }
18766
18767 height = ascent + descent;
18768 if (FLOATP (val))
18769 height = (int)(XFLOAT_DATA (val) * height);
18770 else if (INTEGERP (val))
18771 height *= XINT (val);
18772
18773 return make_number (height);
18774 }
18775
18776
18777 /* RIF:
18778 Produce glyphs/get display metrics for the display element IT is
18779 loaded with. See the description of struct display_iterator in
18780 dispextern.h for an overview of struct display_iterator. */
18781
18782 void
18783 x_produce_glyphs (it)
18784 struct it *it;
18785 {
18786 int extra_line_spacing = it->extra_line_spacing;
18787
18788 it->glyph_not_available_p = 0;
18789
18790 if (it->what == IT_CHARACTER)
18791 {
18792 XChar2b char2b;
18793 XFontStruct *font;
18794 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18795 XCharStruct *pcm;
18796 int font_not_found_p;
18797 struct font_info *font_info;
18798 int boff; /* baseline offset */
18799 /* We may change it->multibyte_p upon unibyte<->multibyte
18800 conversion. So, save the current value now and restore it
18801 later.
18802
18803 Note: It seems that we don't have to record multibyte_p in
18804 struct glyph because the character code itself tells if or
18805 not the character is multibyte. Thus, in the future, we must
18806 consider eliminating the field `multibyte_p' in the struct
18807 glyph. */
18808 int saved_multibyte_p = it->multibyte_p;
18809
18810 /* Maybe translate single-byte characters to multibyte, or the
18811 other way. */
18812 it->char_to_display = it->c;
18813 if (!ASCII_BYTE_P (it->c))
18814 {
18815 if (unibyte_display_via_language_environment
18816 && SINGLE_BYTE_CHAR_P (it->c)
18817 && (it->c >= 0240
18818 || !NILP (Vnonascii_translation_table)))
18819 {
18820 it->char_to_display = unibyte_char_to_multibyte (it->c);
18821 it->multibyte_p = 1;
18822 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18823 face = FACE_FROM_ID (it->f, it->face_id);
18824 }
18825 else if (!SINGLE_BYTE_CHAR_P (it->c)
18826 && !it->multibyte_p)
18827 {
18828 it->multibyte_p = 1;
18829 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18830 face = FACE_FROM_ID (it->f, it->face_id);
18831 }
18832 }
18833
18834 /* Get font to use. Encode IT->char_to_display. */
18835 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18836 &char2b, it->multibyte_p, 0);
18837 font = face->font;
18838
18839 /* When no suitable font found, use the default font. */
18840 font_not_found_p = font == NULL;
18841 if (font_not_found_p)
18842 {
18843 font = FRAME_FONT (it->f);
18844 boff = FRAME_BASELINE_OFFSET (it->f);
18845 font_info = NULL;
18846 }
18847 else
18848 {
18849 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18850 boff = font_info->baseline_offset;
18851 if (font_info->vertical_centering)
18852 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18853 }
18854
18855 if (it->char_to_display >= ' '
18856 && (!it->multibyte_p || it->char_to_display < 128))
18857 {
18858 /* Either unibyte or ASCII. */
18859 int stretched_p;
18860
18861 it->nglyphs = 1;
18862
18863 pcm = rif->per_char_metric (font, &char2b,
18864 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
18865
18866 if (it->override_ascent >= 0)
18867 {
18868 it->ascent = it->override_ascent;
18869 it->descent = it->override_descent;
18870 boff = it->override_boff;
18871 }
18872 else
18873 {
18874 it->ascent = FONT_BASE (font) + boff;
18875 it->descent = FONT_DESCENT (font) - boff;
18876 }
18877
18878 if (pcm)
18879 {
18880 it->phys_ascent = pcm->ascent + boff;
18881 it->phys_descent = pcm->descent - boff;
18882 it->pixel_width = pcm->width;
18883 }
18884 else
18885 {
18886 it->glyph_not_available_p = 1;
18887 it->phys_ascent = it->ascent;
18888 it->phys_descent = it->descent;
18889 it->pixel_width = FONT_WIDTH (font);
18890 }
18891
18892 if (it->constrain_row_ascent_descent_p)
18893 {
18894 if (it->descent > it->max_descent)
18895 {
18896 it->ascent += it->descent - it->max_descent;
18897 it->descent = it->max_descent;
18898 }
18899 if (it->ascent > it->max_ascent)
18900 {
18901 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
18902 it->ascent = it->max_ascent;
18903 }
18904 it->phys_ascent = min (it->phys_ascent, it->ascent);
18905 it->phys_descent = min (it->phys_descent, it->descent);
18906 extra_line_spacing = 0;
18907 }
18908
18909 /* If this is a space inside a region of text with
18910 `space-width' property, change its width. */
18911 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
18912 if (stretched_p)
18913 it->pixel_width *= XFLOATINT (it->space_width);
18914
18915 /* If face has a box, add the box thickness to the character
18916 height. If character has a box line to the left and/or
18917 right, add the box line width to the character's width. */
18918 if (face->box != FACE_NO_BOX)
18919 {
18920 int thick = face->box_line_width;
18921
18922 if (thick > 0)
18923 {
18924 it->ascent += thick;
18925 it->descent += thick;
18926 }
18927 else
18928 thick = -thick;
18929
18930 if (it->start_of_box_run_p)
18931 it->pixel_width += thick;
18932 if (it->end_of_box_run_p)
18933 it->pixel_width += thick;
18934 }
18935
18936 /* If face has an overline, add the height of the overline
18937 (1 pixel) and a 1 pixel margin to the character height. */
18938 if (face->overline_p)
18939 it->ascent += 2;
18940
18941 if (it->constrain_row_ascent_descent_p)
18942 {
18943 if (it->ascent > it->max_ascent)
18944 it->ascent = it->max_ascent;
18945 if (it->descent > it->max_descent)
18946 it->descent = it->max_descent;
18947 }
18948
18949 take_vertical_position_into_account (it);
18950
18951 /* If we have to actually produce glyphs, do it. */
18952 if (it->glyph_row)
18953 {
18954 if (stretched_p)
18955 {
18956 /* Translate a space with a `space-width' property
18957 into a stretch glyph. */
18958 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
18959 / FONT_HEIGHT (font));
18960 append_stretch_glyph (it, it->object, it->pixel_width,
18961 it->ascent + it->descent, ascent);
18962 }
18963 else
18964 append_glyph (it);
18965
18966 /* If characters with lbearing or rbearing are displayed
18967 in this line, record that fact in a flag of the
18968 glyph row. This is used to optimize X output code. */
18969 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
18970 it->glyph_row->contains_overlapping_glyphs_p = 1;
18971 }
18972 }
18973 else if (it->char_to_display == '\n')
18974 {
18975 /* A newline has no width but we need the height of the line.
18976 But if previous part of the line set a height, don't
18977 increase that height */
18978
18979 Lisp_Object height;
18980
18981 it->override_ascent = -1;
18982 it->pixel_width = 0;
18983 it->nglyphs = 0;
18984
18985 height = calc_line_height_property(it, Qline_height, font, boff, 0);
18986
18987 if (it->override_ascent >= 0)
18988 {
18989 it->ascent = it->override_ascent;
18990 it->descent = it->override_descent;
18991 boff = it->override_boff;
18992 }
18993 else
18994 {
18995 it->ascent = FONT_BASE (font) + boff;
18996 it->descent = FONT_DESCENT (font) - boff;
18997 }
18998
18999 if (EQ (height, make_number(0)))
19000 {
19001 if (it->descent > it->max_descent)
19002 {
19003 it->ascent += it->descent - it->max_descent;
19004 it->descent = it->max_descent;
19005 }
19006 if (it->ascent > it->max_ascent)
19007 {
19008 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19009 it->ascent = it->max_ascent;
19010 }
19011 it->phys_ascent = min (it->phys_ascent, it->ascent);
19012 it->phys_descent = min (it->phys_descent, it->descent);
19013 it->constrain_row_ascent_descent_p = 1;
19014 extra_line_spacing = 0;
19015 }
19016 else
19017 {
19018 Lisp_Object spacing;
19019 int total = 0;
19020
19021 it->phys_ascent = it->ascent;
19022 it->phys_descent = it->descent;
19023
19024 if ((it->max_ascent > 0 || it->max_descent > 0)
19025 && face->box != FACE_NO_BOX
19026 && face->box_line_width > 0)
19027 {
19028 it->ascent += face->box_line_width;
19029 it->descent += face->box_line_width;
19030 }
19031 if (!NILP (height)
19032 && XINT (height) > it->ascent + it->descent)
19033 it->ascent = XINT (height) - it->descent;
19034
19035 spacing = calc_line_height_property(it, Qline_spacing, font, boff, &total);
19036 if (INTEGERP (spacing))
19037 {
19038 extra_line_spacing = XINT (spacing);
19039 if (total)
19040 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19041 }
19042 }
19043 }
19044 else if (it->char_to_display == '\t')
19045 {
19046 int tab_width = it->tab_width * FRAME_COLUMN_WIDTH (it->f);
19047 int x = it->current_x + it->continuation_lines_width;
19048 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
19049
19050 /* If the distance from the current position to the next tab
19051 stop is less than a canonical character width, use the
19052 tab stop after that. */
19053 if (next_tab_x - x < FRAME_COLUMN_WIDTH (it->f))
19054 next_tab_x += tab_width;
19055
19056 it->pixel_width = next_tab_x - x;
19057 it->nglyphs = 1;
19058 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
19059 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
19060
19061 if (it->glyph_row)
19062 {
19063 append_stretch_glyph (it, it->object, it->pixel_width,
19064 it->ascent + it->descent, it->ascent);
19065 }
19066 }
19067 else
19068 {
19069 /* A multi-byte character. Assume that the display width of the
19070 character is the width of the character multiplied by the
19071 width of the font. */
19072
19073 /* If we found a font, this font should give us the right
19074 metrics. If we didn't find a font, use the frame's
19075 default font and calculate the width of the character
19076 from the charset width; this is what old redisplay code
19077 did. */
19078
19079 pcm = rif->per_char_metric (font, &char2b,
19080 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
19081
19082 if (font_not_found_p || !pcm)
19083 {
19084 int charset = CHAR_CHARSET (it->char_to_display);
19085
19086 it->glyph_not_available_p = 1;
19087 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
19088 * CHARSET_WIDTH (charset));
19089 it->phys_ascent = FONT_BASE (font) + boff;
19090 it->phys_descent = FONT_DESCENT (font) - boff;
19091 }
19092 else
19093 {
19094 it->pixel_width = pcm->width;
19095 it->phys_ascent = pcm->ascent + boff;
19096 it->phys_descent = pcm->descent - boff;
19097 if (it->glyph_row
19098 && (pcm->lbearing < 0
19099 || pcm->rbearing > pcm->width))
19100 it->glyph_row->contains_overlapping_glyphs_p = 1;
19101 }
19102 it->nglyphs = 1;
19103 it->ascent = FONT_BASE (font) + boff;
19104 it->descent = FONT_DESCENT (font) - boff;
19105 if (face->box != FACE_NO_BOX)
19106 {
19107 int thick = face->box_line_width;
19108
19109 if (thick > 0)
19110 {
19111 it->ascent += thick;
19112 it->descent += thick;
19113 }
19114 else
19115 thick = - thick;
19116
19117 if (it->start_of_box_run_p)
19118 it->pixel_width += thick;
19119 if (it->end_of_box_run_p)
19120 it->pixel_width += thick;
19121 }
19122
19123 /* If face has an overline, add the height of the overline
19124 (1 pixel) and a 1 pixel margin to the character height. */
19125 if (face->overline_p)
19126 it->ascent += 2;
19127
19128 take_vertical_position_into_account (it);
19129
19130 if (it->glyph_row)
19131 append_glyph (it);
19132 }
19133 it->multibyte_p = saved_multibyte_p;
19134 }
19135 else if (it->what == IT_COMPOSITION)
19136 {
19137 /* Note: A composition is represented as one glyph in the
19138 glyph matrix. There are no padding glyphs. */
19139 XChar2b char2b;
19140 XFontStruct *font;
19141 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19142 XCharStruct *pcm;
19143 int font_not_found_p;
19144 struct font_info *font_info;
19145 int boff; /* baseline offset */
19146 struct composition *cmp = composition_table[it->cmp_id];
19147
19148 /* Maybe translate single-byte characters to multibyte. */
19149 it->char_to_display = it->c;
19150 if (unibyte_display_via_language_environment
19151 && SINGLE_BYTE_CHAR_P (it->c)
19152 && (it->c >= 0240
19153 || (it->c >= 0200
19154 && !NILP (Vnonascii_translation_table))))
19155 {
19156 it->char_to_display = unibyte_char_to_multibyte (it->c);
19157 }
19158
19159 /* Get face and font to use. Encode IT->char_to_display. */
19160 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19161 face = FACE_FROM_ID (it->f, it->face_id);
19162 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19163 &char2b, it->multibyte_p, 0);
19164 font = face->font;
19165
19166 /* When no suitable font found, use the default font. */
19167 font_not_found_p = font == NULL;
19168 if (font_not_found_p)
19169 {
19170 font = FRAME_FONT (it->f);
19171 boff = FRAME_BASELINE_OFFSET (it->f);
19172 font_info = NULL;
19173 }
19174 else
19175 {
19176 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19177 boff = font_info->baseline_offset;
19178 if (font_info->vertical_centering)
19179 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19180 }
19181
19182 /* There are no padding glyphs, so there is only one glyph to
19183 produce for the composition. Important is that pixel_width,
19184 ascent and descent are the values of what is drawn by
19185 draw_glyphs (i.e. the values of the overall glyphs composed). */
19186 it->nglyphs = 1;
19187
19188 /* If we have not yet calculated pixel size data of glyphs of
19189 the composition for the current face font, calculate them
19190 now. Theoretically, we have to check all fonts for the
19191 glyphs, but that requires much time and memory space. So,
19192 here we check only the font of the first glyph. This leads
19193 to incorrect display very rarely, and C-l (recenter) can
19194 correct the display anyway. */
19195 if (cmp->font != (void *) font)
19196 {
19197 /* Ascent and descent of the font of the first character of
19198 this composition (adjusted by baseline offset). Ascent
19199 and descent of overall glyphs should not be less than
19200 them respectively. */
19201 int font_ascent = FONT_BASE (font) + boff;
19202 int font_descent = FONT_DESCENT (font) - boff;
19203 /* Bounding box of the overall glyphs. */
19204 int leftmost, rightmost, lowest, highest;
19205 int i, width, ascent, descent;
19206
19207 cmp->font = (void *) font;
19208
19209 /* Initialize the bounding box. */
19210 if (font_info
19211 && (pcm = rif->per_char_metric (font, &char2b,
19212 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19213 {
19214 width = pcm->width;
19215 ascent = pcm->ascent;
19216 descent = pcm->descent;
19217 }
19218 else
19219 {
19220 width = FONT_WIDTH (font);
19221 ascent = FONT_BASE (font);
19222 descent = FONT_DESCENT (font);
19223 }
19224
19225 rightmost = width;
19226 lowest = - descent + boff;
19227 highest = ascent + boff;
19228 leftmost = 0;
19229
19230 if (font_info
19231 && font_info->default_ascent
19232 && CHAR_TABLE_P (Vuse_default_ascent)
19233 && !NILP (Faref (Vuse_default_ascent,
19234 make_number (it->char_to_display))))
19235 highest = font_info->default_ascent + boff;
19236
19237 /* Draw the first glyph at the normal position. It may be
19238 shifted to right later if some other glyphs are drawn at
19239 the left. */
19240 cmp->offsets[0] = 0;
19241 cmp->offsets[1] = boff;
19242
19243 /* Set cmp->offsets for the remaining glyphs. */
19244 for (i = 1; i < cmp->glyph_len; i++)
19245 {
19246 int left, right, btm, top;
19247 int ch = COMPOSITION_GLYPH (cmp, i);
19248 int face_id = FACE_FOR_CHAR (it->f, face, ch);
19249
19250 face = FACE_FROM_ID (it->f, face_id);
19251 get_char_face_and_encoding (it->f, ch, face->id,
19252 &char2b, it->multibyte_p, 0);
19253 font = face->font;
19254 if (font == NULL)
19255 {
19256 font = FRAME_FONT (it->f);
19257 boff = FRAME_BASELINE_OFFSET (it->f);
19258 font_info = NULL;
19259 }
19260 else
19261 {
19262 font_info
19263 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19264 boff = font_info->baseline_offset;
19265 if (font_info->vertical_centering)
19266 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19267 }
19268
19269 if (font_info
19270 && (pcm = rif->per_char_metric (font, &char2b,
19271 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19272 {
19273 width = pcm->width;
19274 ascent = pcm->ascent;
19275 descent = pcm->descent;
19276 }
19277 else
19278 {
19279 width = FONT_WIDTH (font);
19280 ascent = 1;
19281 descent = 0;
19282 }
19283
19284 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19285 {
19286 /* Relative composition with or without
19287 alternate chars. */
19288 left = (leftmost + rightmost - width) / 2;
19289 btm = - descent + boff;
19290 if (font_info && font_info->relative_compose
19291 && (! CHAR_TABLE_P (Vignore_relative_composition)
19292 || NILP (Faref (Vignore_relative_composition,
19293 make_number (ch)))))
19294 {
19295
19296 if (- descent >= font_info->relative_compose)
19297 /* One extra pixel between two glyphs. */
19298 btm = highest + 1;
19299 else if (ascent <= 0)
19300 /* One extra pixel between two glyphs. */
19301 btm = lowest - 1 - ascent - descent;
19302 }
19303 }
19304 else
19305 {
19306 /* A composition rule is specified by an integer
19307 value that encodes global and new reference
19308 points (GREF and NREF). GREF and NREF are
19309 specified by numbers as below:
19310
19311 0---1---2 -- ascent
19312 | |
19313 | |
19314 | |
19315 9--10--11 -- center
19316 | |
19317 ---3---4---5--- baseline
19318 | |
19319 6---7---8 -- descent
19320 */
19321 int rule = COMPOSITION_RULE (cmp, i);
19322 int gref, nref, grefx, grefy, nrefx, nrefy;
19323
19324 COMPOSITION_DECODE_RULE (rule, gref, nref);
19325 grefx = gref % 3, nrefx = nref % 3;
19326 grefy = gref / 3, nrefy = nref / 3;
19327
19328 left = (leftmost
19329 + grefx * (rightmost - leftmost) / 2
19330 - nrefx * width / 2);
19331 btm = ((grefy == 0 ? highest
19332 : grefy == 1 ? 0
19333 : grefy == 2 ? lowest
19334 : (highest + lowest) / 2)
19335 - (nrefy == 0 ? ascent + descent
19336 : nrefy == 1 ? descent - boff
19337 : nrefy == 2 ? 0
19338 : (ascent + descent) / 2));
19339 }
19340
19341 cmp->offsets[i * 2] = left;
19342 cmp->offsets[i * 2 + 1] = btm + descent;
19343
19344 /* Update the bounding box of the overall glyphs. */
19345 right = left + width;
19346 top = btm + descent + ascent;
19347 if (left < leftmost)
19348 leftmost = left;
19349 if (right > rightmost)
19350 rightmost = right;
19351 if (top > highest)
19352 highest = top;
19353 if (btm < lowest)
19354 lowest = btm;
19355 }
19356
19357 /* If there are glyphs whose x-offsets are negative,
19358 shift all glyphs to the right and make all x-offsets
19359 non-negative. */
19360 if (leftmost < 0)
19361 {
19362 for (i = 0; i < cmp->glyph_len; i++)
19363 cmp->offsets[i * 2] -= leftmost;
19364 rightmost -= leftmost;
19365 }
19366
19367 cmp->pixel_width = rightmost;
19368 cmp->ascent = highest;
19369 cmp->descent = - lowest;
19370 if (cmp->ascent < font_ascent)
19371 cmp->ascent = font_ascent;
19372 if (cmp->descent < font_descent)
19373 cmp->descent = font_descent;
19374 }
19375
19376 it->pixel_width = cmp->pixel_width;
19377 it->ascent = it->phys_ascent = cmp->ascent;
19378 it->descent = it->phys_descent = cmp->descent;
19379
19380 if (face->box != FACE_NO_BOX)
19381 {
19382 int thick = face->box_line_width;
19383
19384 if (thick > 0)
19385 {
19386 it->ascent += thick;
19387 it->descent += thick;
19388 }
19389 else
19390 thick = - thick;
19391
19392 if (it->start_of_box_run_p)
19393 it->pixel_width += thick;
19394 if (it->end_of_box_run_p)
19395 it->pixel_width += thick;
19396 }
19397
19398 /* If face has an overline, add the height of the overline
19399 (1 pixel) and a 1 pixel margin to the character height. */
19400 if (face->overline_p)
19401 it->ascent += 2;
19402
19403 take_vertical_position_into_account (it);
19404
19405 if (it->glyph_row)
19406 append_composite_glyph (it);
19407 }
19408 else if (it->what == IT_IMAGE)
19409 produce_image_glyph (it);
19410 else if (it->what == IT_STRETCH)
19411 produce_stretch_glyph (it);
19412
19413 /* Accumulate dimensions. Note: can't assume that it->descent > 0
19414 because this isn't true for images with `:ascent 100'. */
19415 xassert (it->ascent >= 0 && it->descent >= 0);
19416 if (it->area == TEXT_AREA)
19417 it->current_x += it->pixel_width;
19418
19419 if (extra_line_spacing > 0)
19420 it->descent += extra_line_spacing;
19421
19422 it->max_ascent = max (it->max_ascent, it->ascent);
19423 it->max_descent = max (it->max_descent, it->descent);
19424 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
19425 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
19426 }
19427
19428 /* EXPORT for RIF:
19429 Output LEN glyphs starting at START at the nominal cursor position.
19430 Advance the nominal cursor over the text. The global variable
19431 updated_window contains the window being updated, updated_row is
19432 the glyph row being updated, and updated_area is the area of that
19433 row being updated. */
19434
19435 void
19436 x_write_glyphs (start, len)
19437 struct glyph *start;
19438 int len;
19439 {
19440 int x, hpos;
19441
19442 xassert (updated_window && updated_row);
19443 BLOCK_INPUT;
19444
19445 /* Write glyphs. */
19446
19447 hpos = start - updated_row->glyphs[updated_area];
19448 x = draw_glyphs (updated_window, output_cursor.x,
19449 updated_row, updated_area,
19450 hpos, hpos + len,
19451 DRAW_NORMAL_TEXT, 0);
19452
19453 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
19454 if (updated_area == TEXT_AREA
19455 && updated_window->phys_cursor_on_p
19456 && updated_window->phys_cursor.vpos == output_cursor.vpos
19457 && updated_window->phys_cursor.hpos >= hpos
19458 && updated_window->phys_cursor.hpos < hpos + len)
19459 updated_window->phys_cursor_on_p = 0;
19460
19461 UNBLOCK_INPUT;
19462
19463 /* Advance the output cursor. */
19464 output_cursor.hpos += len;
19465 output_cursor.x = x;
19466 }
19467
19468
19469 /* EXPORT for RIF:
19470 Insert LEN glyphs from START at the nominal cursor position. */
19471
19472 void
19473 x_insert_glyphs (start, len)
19474 struct glyph *start;
19475 int len;
19476 {
19477 struct frame *f;
19478 struct window *w;
19479 int line_height, shift_by_width, shifted_region_width;
19480 struct glyph_row *row;
19481 struct glyph *glyph;
19482 int frame_x, frame_y, hpos;
19483
19484 xassert (updated_window && updated_row);
19485 BLOCK_INPUT;
19486 w = updated_window;
19487 f = XFRAME (WINDOW_FRAME (w));
19488
19489 /* Get the height of the line we are in. */
19490 row = updated_row;
19491 line_height = row->height;
19492
19493 /* Get the width of the glyphs to insert. */
19494 shift_by_width = 0;
19495 for (glyph = start; glyph < start + len; ++glyph)
19496 shift_by_width += glyph->pixel_width;
19497
19498 /* Get the width of the region to shift right. */
19499 shifted_region_width = (window_box_width (w, updated_area)
19500 - output_cursor.x
19501 - shift_by_width);
19502
19503 /* Shift right. */
19504 frame_x = window_box_left (w, updated_area) + output_cursor.x;
19505 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
19506
19507 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
19508 line_height, shift_by_width);
19509
19510 /* Write the glyphs. */
19511 hpos = start - row->glyphs[updated_area];
19512 draw_glyphs (w, output_cursor.x, row, updated_area,
19513 hpos, hpos + len,
19514 DRAW_NORMAL_TEXT, 0);
19515
19516 /* Advance the output cursor. */
19517 output_cursor.hpos += len;
19518 output_cursor.x += shift_by_width;
19519 UNBLOCK_INPUT;
19520 }
19521
19522
19523 /* EXPORT for RIF:
19524 Erase the current text line from the nominal cursor position
19525 (inclusive) to pixel column TO_X (exclusive). The idea is that
19526 everything from TO_X onward is already erased.
19527
19528 TO_X is a pixel position relative to updated_area of
19529 updated_window. TO_X == -1 means clear to the end of this area. */
19530
19531 void
19532 x_clear_end_of_line (to_x)
19533 int to_x;
19534 {
19535 struct frame *f;
19536 struct window *w = updated_window;
19537 int max_x, min_y, max_y;
19538 int from_x, from_y, to_y;
19539
19540 xassert (updated_window && updated_row);
19541 f = XFRAME (w->frame);
19542
19543 if (updated_row->full_width_p)
19544 max_x = WINDOW_TOTAL_WIDTH (w);
19545 else
19546 max_x = window_box_width (w, updated_area);
19547 max_y = window_text_bottom_y (w);
19548
19549 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
19550 of window. For TO_X > 0, truncate to end of drawing area. */
19551 if (to_x == 0)
19552 return;
19553 else if (to_x < 0)
19554 to_x = max_x;
19555 else
19556 to_x = min (to_x, max_x);
19557
19558 to_y = min (max_y, output_cursor.y + updated_row->height);
19559
19560 /* Notice if the cursor will be cleared by this operation. */
19561 if (!updated_row->full_width_p)
19562 notice_overwritten_cursor (w, updated_area,
19563 output_cursor.x, -1,
19564 updated_row->y,
19565 MATRIX_ROW_BOTTOM_Y (updated_row));
19566
19567 from_x = output_cursor.x;
19568
19569 /* Translate to frame coordinates. */
19570 if (updated_row->full_width_p)
19571 {
19572 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
19573 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
19574 }
19575 else
19576 {
19577 int area_left = window_box_left (w, updated_area);
19578 from_x += area_left;
19579 to_x += area_left;
19580 }
19581
19582 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
19583 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
19584 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
19585
19586 /* Prevent inadvertently clearing to end of the X window. */
19587 if (to_x > from_x && to_y > from_y)
19588 {
19589 BLOCK_INPUT;
19590 rif->clear_frame_area (f, from_x, from_y,
19591 to_x - from_x, to_y - from_y);
19592 UNBLOCK_INPUT;
19593 }
19594 }
19595
19596 #endif /* HAVE_WINDOW_SYSTEM */
19597
19598
19599 \f
19600 /***********************************************************************
19601 Cursor types
19602 ***********************************************************************/
19603
19604 /* Value is the internal representation of the specified cursor type
19605 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
19606 of the bar cursor. */
19607
19608 static enum text_cursor_kinds
19609 get_specified_cursor_type (arg, width)
19610 Lisp_Object arg;
19611 int *width;
19612 {
19613 enum text_cursor_kinds type;
19614
19615 if (NILP (arg))
19616 return NO_CURSOR;
19617
19618 if (EQ (arg, Qbox))
19619 return FILLED_BOX_CURSOR;
19620
19621 if (EQ (arg, Qhollow))
19622 return HOLLOW_BOX_CURSOR;
19623
19624 if (EQ (arg, Qbar))
19625 {
19626 *width = 2;
19627 return BAR_CURSOR;
19628 }
19629
19630 if (CONSP (arg)
19631 && EQ (XCAR (arg), Qbar)
19632 && INTEGERP (XCDR (arg))
19633 && XINT (XCDR (arg)) >= 0)
19634 {
19635 *width = XINT (XCDR (arg));
19636 return BAR_CURSOR;
19637 }
19638
19639 if (EQ (arg, Qhbar))
19640 {
19641 *width = 2;
19642 return HBAR_CURSOR;
19643 }
19644
19645 if (CONSP (arg)
19646 && EQ (XCAR (arg), Qhbar)
19647 && INTEGERP (XCDR (arg))
19648 && XINT (XCDR (arg)) >= 0)
19649 {
19650 *width = XINT (XCDR (arg));
19651 return HBAR_CURSOR;
19652 }
19653
19654 /* Treat anything unknown as "hollow box cursor".
19655 It was bad to signal an error; people have trouble fixing
19656 .Xdefaults with Emacs, when it has something bad in it. */
19657 type = HOLLOW_BOX_CURSOR;
19658
19659 return type;
19660 }
19661
19662 /* Set the default cursor types for specified frame. */
19663 void
19664 set_frame_cursor_types (f, arg)
19665 struct frame *f;
19666 Lisp_Object arg;
19667 {
19668 int width;
19669 Lisp_Object tem;
19670
19671 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
19672 FRAME_CURSOR_WIDTH (f) = width;
19673
19674 /* By default, set up the blink-off state depending on the on-state. */
19675
19676 tem = Fassoc (arg, Vblink_cursor_alist);
19677 if (!NILP (tem))
19678 {
19679 FRAME_BLINK_OFF_CURSOR (f)
19680 = get_specified_cursor_type (XCDR (tem), &width);
19681 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
19682 }
19683 else
19684 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
19685 }
19686
19687
19688 /* Return the cursor we want to be displayed in window W. Return
19689 width of bar/hbar cursor through WIDTH arg. Return with
19690 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
19691 (i.e. if the `system caret' should track this cursor).
19692
19693 In a mini-buffer window, we want the cursor only to appear if we
19694 are reading input from this window. For the selected window, we
19695 want the cursor type given by the frame parameter or buffer local
19696 setting of cursor-type. If explicitly marked off, draw no cursor.
19697 In all other cases, we want a hollow box cursor. */
19698
19699 static enum text_cursor_kinds
19700 get_window_cursor_type (w, glyph, width, active_cursor)
19701 struct window *w;
19702 struct glyph *glyph;
19703 int *width;
19704 int *active_cursor;
19705 {
19706 struct frame *f = XFRAME (w->frame);
19707 struct buffer *b = XBUFFER (w->buffer);
19708 int cursor_type = DEFAULT_CURSOR;
19709 Lisp_Object alt_cursor;
19710 int non_selected = 0;
19711
19712 *active_cursor = 1;
19713
19714 /* Echo area */
19715 if (cursor_in_echo_area
19716 && FRAME_HAS_MINIBUF_P (f)
19717 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
19718 {
19719 if (w == XWINDOW (echo_area_window))
19720 {
19721 *width = FRAME_CURSOR_WIDTH (f);
19722 return FRAME_DESIRED_CURSOR (f);
19723 }
19724
19725 *active_cursor = 0;
19726 non_selected = 1;
19727 }
19728
19729 /* Nonselected window or nonselected frame. */
19730 else if (w != XWINDOW (f->selected_window)
19731 #ifdef HAVE_WINDOW_SYSTEM
19732 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
19733 #endif
19734 )
19735 {
19736 *active_cursor = 0;
19737
19738 if (MINI_WINDOW_P (w) && minibuf_level == 0)
19739 return NO_CURSOR;
19740
19741 non_selected = 1;
19742 }
19743
19744 /* Never display a cursor in a window in which cursor-type is nil. */
19745 if (NILP (b->cursor_type))
19746 return NO_CURSOR;
19747
19748 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
19749 if (non_selected)
19750 {
19751 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
19752 return get_specified_cursor_type (alt_cursor, width);
19753 }
19754
19755 /* Get the normal cursor type for this window. */
19756 if (EQ (b->cursor_type, Qt))
19757 {
19758 cursor_type = FRAME_DESIRED_CURSOR (f);
19759 *width = FRAME_CURSOR_WIDTH (f);
19760 }
19761 else
19762 cursor_type = get_specified_cursor_type (b->cursor_type, width);
19763
19764 /* Use normal cursor if not blinked off. */
19765 if (!w->cursor_off_p)
19766 {
19767 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
19768 if (cursor_type == FILLED_BOX_CURSOR)
19769 cursor_type = HOLLOW_BOX_CURSOR;
19770 }
19771 return cursor_type;
19772 }
19773
19774 /* Cursor is blinked off, so determine how to "toggle" it. */
19775
19776 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
19777 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
19778 return get_specified_cursor_type (XCDR (alt_cursor), width);
19779
19780 /* Then see if frame has specified a specific blink off cursor type. */
19781 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
19782 {
19783 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
19784 return FRAME_BLINK_OFF_CURSOR (f);
19785 }
19786
19787 #if 0
19788 /* Some people liked having a permanently visible blinking cursor,
19789 while others had very strong opinions against it. So it was
19790 decided to remove it. KFS 2003-09-03 */
19791
19792 /* Finally perform built-in cursor blinking:
19793 filled box <-> hollow box
19794 wide [h]bar <-> narrow [h]bar
19795 narrow [h]bar <-> no cursor
19796 other type <-> no cursor */
19797
19798 if (cursor_type == FILLED_BOX_CURSOR)
19799 return HOLLOW_BOX_CURSOR;
19800
19801 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
19802 {
19803 *width = 1;
19804 return cursor_type;
19805 }
19806 #endif
19807
19808 return NO_CURSOR;
19809 }
19810
19811
19812 #ifdef HAVE_WINDOW_SYSTEM
19813
19814 /* Notice when the text cursor of window W has been completely
19815 overwritten by a drawing operation that outputs glyphs in AREA
19816 starting at X0 and ending at X1 in the line starting at Y0 and
19817 ending at Y1. X coordinates are area-relative. X1 < 0 means all
19818 the rest of the line after X0 has been written. Y coordinates
19819 are window-relative. */
19820
19821 static void
19822 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
19823 struct window *w;
19824 enum glyph_row_area area;
19825 int x0, y0, x1, y1;
19826 {
19827 int cx0, cx1, cy0, cy1;
19828 struct glyph_row *row;
19829
19830 if (!w->phys_cursor_on_p)
19831 return;
19832 if (area != TEXT_AREA)
19833 return;
19834
19835 row = w->current_matrix->rows + w->phys_cursor.vpos;
19836 if (!row->displays_text_p)
19837 return;
19838
19839 if (row->cursor_in_fringe_p)
19840 {
19841 row->cursor_in_fringe_p = 0;
19842 draw_fringe_bitmap (w, row, 0);
19843 w->phys_cursor_on_p = 0;
19844 return;
19845 }
19846
19847 cx0 = w->phys_cursor.x;
19848 cx1 = cx0 + w->phys_cursor_width;
19849 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
19850 return;
19851
19852 /* The cursor image will be completely removed from the
19853 screen if the output area intersects the cursor area in
19854 y-direction. When we draw in [y0 y1[, and some part of
19855 the cursor is at y < y0, that part must have been drawn
19856 before. When scrolling, the cursor is erased before
19857 actually scrolling, so we don't come here. When not
19858 scrolling, the rows above the old cursor row must have
19859 changed, and in this case these rows must have written
19860 over the cursor image.
19861
19862 Likewise if part of the cursor is below y1, with the
19863 exception of the cursor being in the first blank row at
19864 the buffer and window end because update_text_area
19865 doesn't draw that row. (Except when it does, but
19866 that's handled in update_text_area.) */
19867
19868 cy0 = w->phys_cursor.y;
19869 cy1 = cy0 + w->phys_cursor_height;
19870 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
19871 return;
19872
19873 w->phys_cursor_on_p = 0;
19874 }
19875
19876 #endif /* HAVE_WINDOW_SYSTEM */
19877
19878 \f
19879 /************************************************************************
19880 Mouse Face
19881 ************************************************************************/
19882
19883 #ifdef HAVE_WINDOW_SYSTEM
19884
19885 /* EXPORT for RIF:
19886 Fix the display of area AREA of overlapping row ROW in window W. */
19887
19888 void
19889 x_fix_overlapping_area (w, row, area)
19890 struct window *w;
19891 struct glyph_row *row;
19892 enum glyph_row_area area;
19893 {
19894 int i, x;
19895
19896 BLOCK_INPUT;
19897
19898 x = 0;
19899 for (i = 0; i < row->used[area];)
19900 {
19901 if (row->glyphs[area][i].overlaps_vertically_p)
19902 {
19903 int start = i, start_x = x;
19904
19905 do
19906 {
19907 x += row->glyphs[area][i].pixel_width;
19908 ++i;
19909 }
19910 while (i < row->used[area]
19911 && row->glyphs[area][i].overlaps_vertically_p);
19912
19913 draw_glyphs (w, start_x, row, area,
19914 start, i,
19915 DRAW_NORMAL_TEXT, 1);
19916 }
19917 else
19918 {
19919 x += row->glyphs[area][i].pixel_width;
19920 ++i;
19921 }
19922 }
19923
19924 UNBLOCK_INPUT;
19925 }
19926
19927
19928 /* EXPORT:
19929 Draw the cursor glyph of window W in glyph row ROW. See the
19930 comment of draw_glyphs for the meaning of HL. */
19931
19932 void
19933 draw_phys_cursor_glyph (w, row, hl)
19934 struct window *w;
19935 struct glyph_row *row;
19936 enum draw_glyphs_face hl;
19937 {
19938 /* If cursor hpos is out of bounds, don't draw garbage. This can
19939 happen in mini-buffer windows when switching between echo area
19940 glyphs and mini-buffer. */
19941 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
19942 {
19943 int on_p = w->phys_cursor_on_p;
19944 int x1;
19945 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
19946 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
19947 hl, 0);
19948 w->phys_cursor_on_p = on_p;
19949
19950 if (hl == DRAW_CURSOR)
19951 w->phys_cursor_width = x1 - w->phys_cursor.x;
19952 /* When we erase the cursor, and ROW is overlapped by other
19953 rows, make sure that these overlapping parts of other rows
19954 are redrawn. */
19955 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
19956 {
19957 if (row > w->current_matrix->rows
19958 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
19959 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
19960
19961 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
19962 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
19963 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
19964 }
19965 }
19966 }
19967
19968
19969 /* EXPORT:
19970 Erase the image of a cursor of window W from the screen. */
19971
19972 void
19973 erase_phys_cursor (w)
19974 struct window *w;
19975 {
19976 struct frame *f = XFRAME (w->frame);
19977 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
19978 int hpos = w->phys_cursor.hpos;
19979 int vpos = w->phys_cursor.vpos;
19980 int mouse_face_here_p = 0;
19981 struct glyph_matrix *active_glyphs = w->current_matrix;
19982 struct glyph_row *cursor_row;
19983 struct glyph *cursor_glyph;
19984 enum draw_glyphs_face hl;
19985
19986 /* No cursor displayed or row invalidated => nothing to do on the
19987 screen. */
19988 if (w->phys_cursor_type == NO_CURSOR)
19989 goto mark_cursor_off;
19990
19991 /* VPOS >= active_glyphs->nrows means that window has been resized.
19992 Don't bother to erase the cursor. */
19993 if (vpos >= active_glyphs->nrows)
19994 goto mark_cursor_off;
19995
19996 /* If row containing cursor is marked invalid, there is nothing we
19997 can do. */
19998 cursor_row = MATRIX_ROW (active_glyphs, vpos);
19999 if (!cursor_row->enabled_p)
20000 goto mark_cursor_off;
20001
20002 /* If row is completely invisible, don't attempt to delete a cursor which
20003 isn't there. This can happen if cursor is at top of a window, and
20004 we switch to a buffer with a header line in that window. */
20005 if (cursor_row->visible_height <= 0)
20006 goto mark_cursor_off;
20007
20008 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
20009 if (cursor_row->cursor_in_fringe_p)
20010 {
20011 cursor_row->cursor_in_fringe_p = 0;
20012 draw_fringe_bitmap (w, cursor_row, 0);
20013 goto mark_cursor_off;
20014 }
20015
20016 /* This can happen when the new row is shorter than the old one.
20017 In this case, either draw_glyphs or clear_end_of_line
20018 should have cleared the cursor. Note that we wouldn't be
20019 able to erase the cursor in this case because we don't have a
20020 cursor glyph at hand. */
20021 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
20022 goto mark_cursor_off;
20023
20024 /* If the cursor is in the mouse face area, redisplay that when
20025 we clear the cursor. */
20026 if (! NILP (dpyinfo->mouse_face_window)
20027 && w == XWINDOW (dpyinfo->mouse_face_window)
20028 && (vpos > dpyinfo->mouse_face_beg_row
20029 || (vpos == dpyinfo->mouse_face_beg_row
20030 && hpos >= dpyinfo->mouse_face_beg_col))
20031 && (vpos < dpyinfo->mouse_face_end_row
20032 || (vpos == dpyinfo->mouse_face_end_row
20033 && hpos < dpyinfo->mouse_face_end_col))
20034 /* Don't redraw the cursor's spot in mouse face if it is at the
20035 end of a line (on a newline). The cursor appears there, but
20036 mouse highlighting does not. */
20037 && cursor_row->used[TEXT_AREA] > hpos)
20038 mouse_face_here_p = 1;
20039
20040 /* Maybe clear the display under the cursor. */
20041 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
20042 {
20043 int x, y;
20044 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
20045
20046 cursor_glyph = get_phys_cursor_glyph (w);
20047 if (cursor_glyph == NULL)
20048 goto mark_cursor_off;
20049
20050 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
20051 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
20052
20053 rif->clear_frame_area (f, x, y,
20054 cursor_glyph->pixel_width, cursor_row->visible_height);
20055 }
20056
20057 /* Erase the cursor by redrawing the character underneath it. */
20058 if (mouse_face_here_p)
20059 hl = DRAW_MOUSE_FACE;
20060 else
20061 hl = DRAW_NORMAL_TEXT;
20062 draw_phys_cursor_glyph (w, cursor_row, hl);
20063
20064 mark_cursor_off:
20065 w->phys_cursor_on_p = 0;
20066 w->phys_cursor_type = NO_CURSOR;
20067 }
20068
20069
20070 /* EXPORT:
20071 Display or clear cursor of window W. If ON is zero, clear the
20072 cursor. If it is non-zero, display the cursor. If ON is nonzero,
20073 where to put the cursor is specified by HPOS, VPOS, X and Y. */
20074
20075 void
20076 display_and_set_cursor (w, on, hpos, vpos, x, y)
20077 struct window *w;
20078 int on, hpos, vpos, x, y;
20079 {
20080 struct frame *f = XFRAME (w->frame);
20081 int new_cursor_type;
20082 int new_cursor_width;
20083 int active_cursor;
20084 struct glyph_row *glyph_row;
20085 struct glyph *glyph;
20086
20087 /* This is pointless on invisible frames, and dangerous on garbaged
20088 windows and frames; in the latter case, the frame or window may
20089 be in the midst of changing its size, and x and y may be off the
20090 window. */
20091 if (! FRAME_VISIBLE_P (f)
20092 || FRAME_GARBAGED_P (f)
20093 || vpos >= w->current_matrix->nrows
20094 || hpos >= w->current_matrix->matrix_w)
20095 return;
20096
20097 /* If cursor is off and we want it off, return quickly. */
20098 if (!on && !w->phys_cursor_on_p)
20099 return;
20100
20101 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
20102 /* If cursor row is not enabled, we don't really know where to
20103 display the cursor. */
20104 if (!glyph_row->enabled_p)
20105 {
20106 w->phys_cursor_on_p = 0;
20107 return;
20108 }
20109
20110 glyph = NULL;
20111 if (!glyph_row->exact_window_width_line_p
20112 || hpos < glyph_row->used[TEXT_AREA])
20113 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
20114
20115 xassert (interrupt_input_blocked);
20116
20117 /* Set new_cursor_type to the cursor we want to be displayed. */
20118 new_cursor_type = get_window_cursor_type (w, glyph,
20119 &new_cursor_width, &active_cursor);
20120
20121 /* If cursor is currently being shown and we don't want it to be or
20122 it is in the wrong place, or the cursor type is not what we want,
20123 erase it. */
20124 if (w->phys_cursor_on_p
20125 && (!on
20126 || w->phys_cursor.x != x
20127 || w->phys_cursor.y != y
20128 || new_cursor_type != w->phys_cursor_type
20129 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
20130 && new_cursor_width != w->phys_cursor_width)))
20131 erase_phys_cursor (w);
20132
20133 /* Don't check phys_cursor_on_p here because that flag is only set
20134 to zero in some cases where we know that the cursor has been
20135 completely erased, to avoid the extra work of erasing the cursor
20136 twice. In other words, phys_cursor_on_p can be 1 and the cursor
20137 still not be visible, or it has only been partly erased. */
20138 if (on)
20139 {
20140 w->phys_cursor_ascent = glyph_row->ascent;
20141 w->phys_cursor_height = glyph_row->height;
20142
20143 /* Set phys_cursor_.* before x_draw_.* is called because some
20144 of them may need the information. */
20145 w->phys_cursor.x = x;
20146 w->phys_cursor.y = glyph_row->y;
20147 w->phys_cursor.hpos = hpos;
20148 w->phys_cursor.vpos = vpos;
20149 }
20150
20151 rif->draw_window_cursor (w, glyph_row, x, y,
20152 new_cursor_type, new_cursor_width,
20153 on, active_cursor);
20154 }
20155
20156
20157 /* Switch the display of W's cursor on or off, according to the value
20158 of ON. */
20159
20160 static void
20161 update_window_cursor (w, on)
20162 struct window *w;
20163 int on;
20164 {
20165 /* Don't update cursor in windows whose frame is in the process
20166 of being deleted. */
20167 if (w->current_matrix)
20168 {
20169 BLOCK_INPUT;
20170 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20171 w->phys_cursor.x, w->phys_cursor.y);
20172 UNBLOCK_INPUT;
20173 }
20174 }
20175
20176
20177 /* Call update_window_cursor with parameter ON_P on all leaf windows
20178 in the window tree rooted at W. */
20179
20180 static void
20181 update_cursor_in_window_tree (w, on_p)
20182 struct window *w;
20183 int on_p;
20184 {
20185 while (w)
20186 {
20187 if (!NILP (w->hchild))
20188 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20189 else if (!NILP (w->vchild))
20190 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20191 else
20192 update_window_cursor (w, on_p);
20193
20194 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20195 }
20196 }
20197
20198
20199 /* EXPORT:
20200 Display the cursor on window W, or clear it, according to ON_P.
20201 Don't change the cursor's position. */
20202
20203 void
20204 x_update_cursor (f, on_p)
20205 struct frame *f;
20206 int on_p;
20207 {
20208 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20209 }
20210
20211
20212 /* EXPORT:
20213 Clear the cursor of window W to background color, and mark the
20214 cursor as not shown. This is used when the text where the cursor
20215 is is about to be rewritten. */
20216
20217 void
20218 x_clear_cursor (w)
20219 struct window *w;
20220 {
20221 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20222 update_window_cursor (w, 0);
20223 }
20224
20225
20226 /* EXPORT:
20227 Display the active region described by mouse_face_* according to DRAW. */
20228
20229 void
20230 show_mouse_face (dpyinfo, draw)
20231 Display_Info *dpyinfo;
20232 enum draw_glyphs_face draw;
20233 {
20234 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20235 struct frame *f = XFRAME (WINDOW_FRAME (w));
20236
20237 if (/* If window is in the process of being destroyed, don't bother
20238 to do anything. */
20239 w->current_matrix != NULL
20240 /* Don't update mouse highlight if hidden */
20241 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20242 /* Recognize when we are called to operate on rows that don't exist
20243 anymore. This can happen when a window is split. */
20244 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20245 {
20246 int phys_cursor_on_p = w->phys_cursor_on_p;
20247 struct glyph_row *row, *first, *last;
20248
20249 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20250 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20251
20252 for (row = first; row <= last && row->enabled_p; ++row)
20253 {
20254 int start_hpos, end_hpos, start_x;
20255
20256 /* For all but the first row, the highlight starts at column 0. */
20257 if (row == first)
20258 {
20259 start_hpos = dpyinfo->mouse_face_beg_col;
20260 start_x = dpyinfo->mouse_face_beg_x;
20261 }
20262 else
20263 {
20264 start_hpos = 0;
20265 start_x = 0;
20266 }
20267
20268 if (row == last)
20269 end_hpos = dpyinfo->mouse_face_end_col;
20270 else
20271 end_hpos = row->used[TEXT_AREA];
20272
20273 if (end_hpos > start_hpos)
20274 {
20275 draw_glyphs (w, start_x, row, TEXT_AREA,
20276 start_hpos, end_hpos,
20277 draw, 0);
20278
20279 row->mouse_face_p
20280 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20281 }
20282 }
20283
20284 /* When we've written over the cursor, arrange for it to
20285 be displayed again. */
20286 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20287 {
20288 BLOCK_INPUT;
20289 display_and_set_cursor (w, 1,
20290 w->phys_cursor.hpos, w->phys_cursor.vpos,
20291 w->phys_cursor.x, w->phys_cursor.y);
20292 UNBLOCK_INPUT;
20293 }
20294 }
20295
20296 /* Change the mouse cursor. */
20297 if (draw == DRAW_NORMAL_TEXT)
20298 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
20299 else if (draw == DRAW_MOUSE_FACE)
20300 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
20301 else
20302 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
20303 }
20304
20305 /* EXPORT:
20306 Clear out the mouse-highlighted active region.
20307 Redraw it un-highlighted first. Value is non-zero if mouse
20308 face was actually drawn unhighlighted. */
20309
20310 int
20311 clear_mouse_face (dpyinfo)
20312 Display_Info *dpyinfo;
20313 {
20314 int cleared = 0;
20315
20316 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
20317 {
20318 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
20319 cleared = 1;
20320 }
20321
20322 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20323 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20324 dpyinfo->mouse_face_window = Qnil;
20325 dpyinfo->mouse_face_overlay = Qnil;
20326 return cleared;
20327 }
20328
20329
20330 /* EXPORT:
20331 Non-zero if physical cursor of window W is within mouse face. */
20332
20333 int
20334 cursor_in_mouse_face_p (w)
20335 struct window *w;
20336 {
20337 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20338 int in_mouse_face = 0;
20339
20340 if (WINDOWP (dpyinfo->mouse_face_window)
20341 && XWINDOW (dpyinfo->mouse_face_window) == w)
20342 {
20343 int hpos = w->phys_cursor.hpos;
20344 int vpos = w->phys_cursor.vpos;
20345
20346 if (vpos >= dpyinfo->mouse_face_beg_row
20347 && vpos <= dpyinfo->mouse_face_end_row
20348 && (vpos > dpyinfo->mouse_face_beg_row
20349 || hpos >= dpyinfo->mouse_face_beg_col)
20350 && (vpos < dpyinfo->mouse_face_end_row
20351 || hpos < dpyinfo->mouse_face_end_col
20352 || dpyinfo->mouse_face_past_end))
20353 in_mouse_face = 1;
20354 }
20355
20356 return in_mouse_face;
20357 }
20358
20359
20360
20361 \f
20362 /* Find the glyph matrix position of buffer position CHARPOS in window
20363 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
20364 current glyphs must be up to date. If CHARPOS is above window
20365 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
20366 of last line in W. In the row containing CHARPOS, stop before glyphs
20367 having STOP as object. */
20368
20369 #if 1 /* This is a version of fast_find_position that's more correct
20370 in the presence of hscrolling, for example. I didn't install
20371 it right away because the problem fixed is minor, it failed
20372 in 20.x as well, and I think it's too risky to install
20373 so near the release of 21.1. 2001-09-25 gerd. */
20374
20375 static int
20376 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
20377 struct window *w;
20378 int charpos;
20379 int *hpos, *vpos, *x, *y;
20380 Lisp_Object stop;
20381 {
20382 struct glyph_row *row, *first;
20383 struct glyph *glyph, *end;
20384 int past_end = 0;
20385
20386 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20387 row = row_containing_pos (w, charpos, first, NULL, 0);
20388 if (row == NULL)
20389 {
20390 if (charpos < MATRIX_ROW_START_CHARPOS (first))
20391 {
20392 *x = *y = *hpos = *vpos = 0;
20393 return 1;
20394 }
20395 else
20396 {
20397 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
20398 past_end = 1;
20399 }
20400 }
20401
20402 *x = row->x;
20403 *y = row->y;
20404 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20405
20406 glyph = row->glyphs[TEXT_AREA];
20407 end = glyph + row->used[TEXT_AREA];
20408
20409 /* Skip over glyphs not having an object at the start of the row.
20410 These are special glyphs like truncation marks on terminal
20411 frames. */
20412 if (row->displays_text_p)
20413 while (glyph < end
20414 && INTEGERP (glyph->object)
20415 && !EQ (stop, glyph->object)
20416 && glyph->charpos < 0)
20417 {
20418 *x += glyph->pixel_width;
20419 ++glyph;
20420 }
20421
20422 while (glyph < end
20423 && !INTEGERP (glyph->object)
20424 && !EQ (stop, glyph->object)
20425 && (!BUFFERP (glyph->object)
20426 || glyph->charpos < charpos))
20427 {
20428 *x += glyph->pixel_width;
20429 ++glyph;
20430 }
20431
20432 *hpos = glyph - row->glyphs[TEXT_AREA];
20433 return !past_end;
20434 }
20435
20436 #else /* not 1 */
20437
20438 static int
20439 fast_find_position (w, pos, hpos, vpos, x, y, stop)
20440 struct window *w;
20441 int pos;
20442 int *hpos, *vpos, *x, *y;
20443 Lisp_Object stop;
20444 {
20445 int i;
20446 int lastcol;
20447 int maybe_next_line_p = 0;
20448 int line_start_position;
20449 int yb = window_text_bottom_y (w);
20450 struct glyph_row *row, *best_row;
20451 int row_vpos, best_row_vpos;
20452 int current_x;
20453
20454 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20455 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20456
20457 while (row->y < yb)
20458 {
20459 if (row->used[TEXT_AREA])
20460 line_start_position = row->glyphs[TEXT_AREA]->charpos;
20461 else
20462 line_start_position = 0;
20463
20464 if (line_start_position > pos)
20465 break;
20466 /* If the position sought is the end of the buffer,
20467 don't include the blank lines at the bottom of the window. */
20468 else if (line_start_position == pos
20469 && pos == BUF_ZV (XBUFFER (w->buffer)))
20470 {
20471 maybe_next_line_p = 1;
20472 break;
20473 }
20474 else if (line_start_position > 0)
20475 {
20476 best_row = row;
20477 best_row_vpos = row_vpos;
20478 }
20479
20480 if (row->y + row->height >= yb)
20481 break;
20482
20483 ++row;
20484 ++row_vpos;
20485 }
20486
20487 /* Find the right column within BEST_ROW. */
20488 lastcol = 0;
20489 current_x = best_row->x;
20490 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
20491 {
20492 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
20493 int charpos = glyph->charpos;
20494
20495 if (BUFFERP (glyph->object))
20496 {
20497 if (charpos == pos)
20498 {
20499 *hpos = i;
20500 *vpos = best_row_vpos;
20501 *x = current_x;
20502 *y = best_row->y;
20503 return 1;
20504 }
20505 else if (charpos > pos)
20506 break;
20507 }
20508 else if (EQ (glyph->object, stop))
20509 break;
20510
20511 if (charpos > 0)
20512 lastcol = i;
20513 current_x += glyph->pixel_width;
20514 }
20515
20516 /* If we're looking for the end of the buffer,
20517 and we didn't find it in the line we scanned,
20518 use the start of the following line. */
20519 if (maybe_next_line_p)
20520 {
20521 ++best_row;
20522 ++best_row_vpos;
20523 lastcol = 0;
20524 current_x = best_row->x;
20525 }
20526
20527 *vpos = best_row_vpos;
20528 *hpos = lastcol + 1;
20529 *x = current_x;
20530 *y = best_row->y;
20531 return 0;
20532 }
20533
20534 #endif /* not 1 */
20535
20536
20537 /* Find the position of the glyph for position POS in OBJECT in
20538 window W's current matrix, and return in *X, *Y the pixel
20539 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
20540
20541 RIGHT_P non-zero means return the position of the right edge of the
20542 glyph, RIGHT_P zero means return the left edge position.
20543
20544 If no glyph for POS exists in the matrix, return the position of
20545 the glyph with the next smaller position that is in the matrix, if
20546 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
20547 exists in the matrix, return the position of the glyph with the
20548 next larger position in OBJECT.
20549
20550 Value is non-zero if a glyph was found. */
20551
20552 static int
20553 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
20554 struct window *w;
20555 int pos;
20556 Lisp_Object object;
20557 int *hpos, *vpos, *x, *y;
20558 int right_p;
20559 {
20560 int yb = window_text_bottom_y (w);
20561 struct glyph_row *r;
20562 struct glyph *best_glyph = NULL;
20563 struct glyph_row *best_row = NULL;
20564 int best_x = 0;
20565
20566 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20567 r->enabled_p && r->y < yb;
20568 ++r)
20569 {
20570 struct glyph *g = r->glyphs[TEXT_AREA];
20571 struct glyph *e = g + r->used[TEXT_AREA];
20572 int gx;
20573
20574 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
20575 if (EQ (g->object, object))
20576 {
20577 if (g->charpos == pos)
20578 {
20579 best_glyph = g;
20580 best_x = gx;
20581 best_row = r;
20582 goto found;
20583 }
20584 else if (best_glyph == NULL
20585 || ((abs (g->charpos - pos)
20586 < abs (best_glyph->charpos - pos))
20587 && (right_p
20588 ? g->charpos < pos
20589 : g->charpos > pos)))
20590 {
20591 best_glyph = g;
20592 best_x = gx;
20593 best_row = r;
20594 }
20595 }
20596 }
20597
20598 found:
20599
20600 if (best_glyph)
20601 {
20602 *x = best_x;
20603 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
20604
20605 if (right_p)
20606 {
20607 *x += best_glyph->pixel_width;
20608 ++*hpos;
20609 }
20610
20611 *y = best_row->y;
20612 *vpos = best_row - w->current_matrix->rows;
20613 }
20614
20615 return best_glyph != NULL;
20616 }
20617
20618
20619 /* See if position X, Y is within a hot-spot of an image. */
20620
20621 static int
20622 on_hot_spot_p (hot_spot, x, y)
20623 Lisp_Object hot_spot;
20624 int x, y;
20625 {
20626 if (!CONSP (hot_spot))
20627 return 0;
20628
20629 if (EQ (XCAR (hot_spot), Qrect))
20630 {
20631 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
20632 Lisp_Object rect = XCDR (hot_spot);
20633 Lisp_Object tem;
20634 if (!CONSP (rect))
20635 return 0;
20636 if (!CONSP (XCAR (rect)))
20637 return 0;
20638 if (!CONSP (XCDR (rect)))
20639 return 0;
20640 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
20641 return 0;
20642 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
20643 return 0;
20644 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
20645 return 0;
20646 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
20647 return 0;
20648 return 1;
20649 }
20650 else if (EQ (XCAR (hot_spot), Qcircle))
20651 {
20652 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
20653 Lisp_Object circ = XCDR (hot_spot);
20654 Lisp_Object lr, lx0, ly0;
20655 if (CONSP (circ)
20656 && CONSP (XCAR (circ))
20657 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
20658 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
20659 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
20660 {
20661 double r = XFLOATINT (lr);
20662 double dx = XINT (lx0) - x;
20663 double dy = XINT (ly0) - y;
20664 return (dx * dx + dy * dy <= r * r);
20665 }
20666 }
20667 else if (EQ (XCAR (hot_spot), Qpoly))
20668 {
20669 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
20670 if (VECTORP (XCDR (hot_spot)))
20671 {
20672 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
20673 Lisp_Object *poly = v->contents;
20674 int n = v->size;
20675 int i;
20676 int inside = 0;
20677 Lisp_Object lx, ly;
20678 int x0, y0;
20679
20680 /* Need an even number of coordinates, and at least 3 edges. */
20681 if (n < 6 || n & 1)
20682 return 0;
20683
20684 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
20685 If count is odd, we are inside polygon. Pixels on edges
20686 may or may not be included depending on actual geometry of the
20687 polygon. */
20688 if ((lx = poly[n-2], !INTEGERP (lx))
20689 || (ly = poly[n-1], !INTEGERP (lx)))
20690 return 0;
20691 x0 = XINT (lx), y0 = XINT (ly);
20692 for (i = 0; i < n; i += 2)
20693 {
20694 int x1 = x0, y1 = y0;
20695 if ((lx = poly[i], !INTEGERP (lx))
20696 || (ly = poly[i+1], !INTEGERP (ly)))
20697 return 0;
20698 x0 = XINT (lx), y0 = XINT (ly);
20699
20700 /* Does this segment cross the X line? */
20701 if (x0 >= x)
20702 {
20703 if (x1 >= x)
20704 continue;
20705 }
20706 else if (x1 < x)
20707 continue;
20708 if (y > y0 && y > y1)
20709 continue;
20710 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
20711 inside = !inside;
20712 }
20713 return inside;
20714 }
20715 }
20716 /* If we don't understand the format, pretend we're not in the hot-spot. */
20717 return 0;
20718 }
20719
20720 Lisp_Object
20721 find_hot_spot (map, x, y)
20722 Lisp_Object map;
20723 int x, y;
20724 {
20725 while (CONSP (map))
20726 {
20727 if (CONSP (XCAR (map))
20728 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
20729 return XCAR (map);
20730 map = XCDR (map);
20731 }
20732
20733 return Qnil;
20734 }
20735
20736 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
20737 3, 3, 0,
20738 doc: /* Lookup in image map MAP coordinates X and Y.
20739 An image map is an alist where each element has the format (AREA ID PLIST).
20740 An AREA is specified as either a rectangle, a circle, or a polygon:
20741 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
20742 pixel coordinates of the upper left and bottom right corners.
20743 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
20744 and the radius of the circle; r may be a float or integer.
20745 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
20746 vector describes one corner in the polygon.
20747 Returns the alist element for the first matching AREA in MAP. */)
20748 (map, x, y)
20749 Lisp_Object map;
20750 Lisp_Object x, y;
20751 {
20752 if (NILP (map))
20753 return Qnil;
20754
20755 CHECK_NUMBER (x);
20756 CHECK_NUMBER (y);
20757
20758 return find_hot_spot (map, XINT (x), XINT (y));
20759 }
20760
20761
20762 /* Display frame CURSOR, optionally using shape defined by POINTER. */
20763 static void
20764 define_frame_cursor1 (f, cursor, pointer)
20765 struct frame *f;
20766 Cursor cursor;
20767 Lisp_Object pointer;
20768 {
20769 /* Do not change cursor shape while dragging mouse. */
20770 if (!NILP (do_mouse_tracking))
20771 return;
20772
20773 if (!NILP (pointer))
20774 {
20775 if (EQ (pointer, Qarrow))
20776 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20777 else if (EQ (pointer, Qhand))
20778 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
20779 else if (EQ (pointer, Qtext))
20780 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20781 else if (EQ (pointer, intern ("hdrag")))
20782 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
20783 #ifdef HAVE_X_WINDOWS
20784 else if (EQ (pointer, intern ("vdrag")))
20785 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
20786 #endif
20787 else if (EQ (pointer, intern ("hourglass")))
20788 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
20789 else if (EQ (pointer, Qmodeline))
20790 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
20791 else
20792 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20793 }
20794
20795 if (cursor != No_Cursor)
20796 rif->define_frame_cursor (f, cursor);
20797 }
20798
20799 /* Take proper action when mouse has moved to the mode or header line
20800 or marginal area AREA of window W, x-position X and y-position Y.
20801 X is relative to the start of the text display area of W, so the
20802 width of bitmap areas and scroll bars must be subtracted to get a
20803 position relative to the start of the mode line. */
20804
20805 static void
20806 note_mode_line_or_margin_highlight (w, x, y, area)
20807 struct window *w;
20808 int x, y;
20809 enum window_part area;
20810 {
20811 struct frame *f = XFRAME (w->frame);
20812 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20813 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20814 Lisp_Object pointer = Qnil;
20815 int charpos, dx, dy, width, height;
20816 Lisp_Object string, object = Qnil;
20817 Lisp_Object pos, help;
20818
20819 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
20820 string = mode_line_string (w, area, &x, &y, &charpos,
20821 &object, &dx, &dy, &width, &height);
20822 else
20823 {
20824 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
20825 string = marginal_area_string (w, area, &x, &y, &charpos,
20826 &object, &dx, &dy, &width, &height);
20827 }
20828
20829 help = Qnil;
20830
20831 if (IMAGEP (object))
20832 {
20833 Lisp_Object image_map, hotspot;
20834 if ((image_map = Fplist_get (XCDR (object), QCmap),
20835 !NILP (image_map))
20836 && (hotspot = find_hot_spot (image_map, dx, dy),
20837 CONSP (hotspot))
20838 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
20839 {
20840 Lisp_Object area_id, plist;
20841
20842 area_id = XCAR (hotspot);
20843 /* Could check AREA_ID to see if we enter/leave this hot-spot.
20844 If so, we could look for mouse-enter, mouse-leave
20845 properties in PLIST (and do something...). */
20846 if ((plist = XCDR (hotspot), CONSP (plist)))
20847 {
20848 pointer = Fplist_get (plist, Qpointer);
20849 if (NILP (pointer))
20850 pointer = Qhand;
20851 help = Fplist_get (plist, Qhelp_echo);
20852 if (!NILP (help))
20853 {
20854 help_echo_string = help;
20855 /* Is this correct? ++kfs */
20856 XSETWINDOW (help_echo_window, w);
20857 help_echo_object = w->buffer;
20858 help_echo_pos = charpos;
20859 }
20860 }
20861 if (NILP (pointer))
20862 pointer = Fplist_get (XCDR (object), QCpointer);
20863 }
20864 }
20865
20866 if (STRINGP (string))
20867 {
20868 pos = make_number (charpos);
20869 /* If we're on a string with `help-echo' text property, arrange
20870 for the help to be displayed. This is done by setting the
20871 global variable help_echo_string to the help string. */
20872 help = Fget_text_property (pos, Qhelp_echo, string);
20873 if (!NILP (help))
20874 {
20875 help_echo_string = help;
20876 XSETWINDOW (help_echo_window, w);
20877 help_echo_object = string;
20878 help_echo_pos = charpos;
20879 }
20880
20881 if (NILP (pointer))
20882 pointer = Fget_text_property (pos, Qpointer, string);
20883
20884 /* Change the mouse pointer according to what is under X/Y. */
20885 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
20886 {
20887 Lisp_Object map;
20888 map = Fget_text_property (pos, Qlocal_map, string);
20889 if (!KEYMAPP (map))
20890 map = Fget_text_property (pos, Qkeymap, string);
20891 if (!KEYMAPP (map))
20892 cursor = dpyinfo->vertical_scroll_bar_cursor;
20893 }
20894 }
20895
20896 define_frame_cursor1 (f, cursor, pointer);
20897 }
20898
20899
20900 /* EXPORT:
20901 Take proper action when the mouse has moved to position X, Y on
20902 frame F as regards highlighting characters that have mouse-face
20903 properties. Also de-highlighting chars where the mouse was before.
20904 X and Y can be negative or out of range. */
20905
20906 void
20907 note_mouse_highlight (f, x, y)
20908 struct frame *f;
20909 int x, y;
20910 {
20911 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20912 enum window_part part;
20913 Lisp_Object window;
20914 struct window *w;
20915 Cursor cursor = No_Cursor;
20916 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
20917 struct buffer *b;
20918
20919 /* When a menu is active, don't highlight because this looks odd. */
20920 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
20921 if (popup_activated ())
20922 return;
20923 #endif
20924
20925 if (NILP (Vmouse_highlight)
20926 || !f->glyphs_initialized_p)
20927 return;
20928
20929 dpyinfo->mouse_face_mouse_x = x;
20930 dpyinfo->mouse_face_mouse_y = y;
20931 dpyinfo->mouse_face_mouse_frame = f;
20932
20933 if (dpyinfo->mouse_face_defer)
20934 return;
20935
20936 if (gc_in_progress)
20937 {
20938 dpyinfo->mouse_face_deferred_gc = 1;
20939 return;
20940 }
20941
20942 /* Which window is that in? */
20943 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
20944
20945 /* If we were displaying active text in another window, clear that. */
20946 if (! EQ (window, dpyinfo->mouse_face_window))
20947 clear_mouse_face (dpyinfo);
20948
20949 /* Not on a window -> return. */
20950 if (!WINDOWP (window))
20951 return;
20952
20953 /* Reset help_echo_string. It will get recomputed below. */
20954 help_echo_string = Qnil;
20955
20956 /* Convert to window-relative pixel coordinates. */
20957 w = XWINDOW (window);
20958 frame_to_window_pixel_xy (w, &x, &y);
20959
20960 /* Handle tool-bar window differently since it doesn't display a
20961 buffer. */
20962 if (EQ (window, f->tool_bar_window))
20963 {
20964 note_tool_bar_highlight (f, x, y);
20965 return;
20966 }
20967
20968 /* Mouse is on the mode, header line or margin? */
20969 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
20970 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
20971 {
20972 note_mode_line_or_margin_highlight (w, x, y, part);
20973 return;
20974 }
20975
20976 if (part == ON_VERTICAL_BORDER)
20977 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
20978 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
20979 || part == ON_SCROLL_BAR)
20980 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20981 else
20982 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20983
20984 /* Are we in a window whose display is up to date?
20985 And verify the buffer's text has not changed. */
20986 b = XBUFFER (w->buffer);
20987 if (part == ON_TEXT
20988 && EQ (w->window_end_valid, w->buffer)
20989 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
20990 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
20991 {
20992 int hpos, vpos, pos, i, dx, dy, area;
20993 struct glyph *glyph;
20994 Lisp_Object object;
20995 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
20996 Lisp_Object *overlay_vec = NULL;
20997 int noverlays;
20998 struct buffer *obuf;
20999 int obegv, ozv, same_region;
21000
21001 /* Find the glyph under X/Y. */
21002 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
21003
21004 /* Look for :pointer property on image. */
21005 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21006 {
21007 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21008 if (img != NULL && IMAGEP (img->spec))
21009 {
21010 Lisp_Object image_map, hotspot;
21011 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
21012 !NILP (image_map))
21013 && (hotspot = find_hot_spot (image_map,
21014 glyph->slice.x + dx,
21015 glyph->slice.y + dy),
21016 CONSP (hotspot))
21017 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21018 {
21019 Lisp_Object area_id, plist;
21020
21021 area_id = XCAR (hotspot);
21022 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21023 If so, we could look for mouse-enter, mouse-leave
21024 properties in PLIST (and do something...). */
21025 if ((plist = XCDR (hotspot), CONSP (plist)))
21026 {
21027 pointer = Fplist_get (plist, Qpointer);
21028 if (NILP (pointer))
21029 pointer = Qhand;
21030 help_echo_string = Fplist_get (plist, Qhelp_echo);
21031 if (!NILP (help_echo_string))
21032 {
21033 help_echo_window = window;
21034 help_echo_object = glyph->object;
21035 help_echo_pos = glyph->charpos;
21036 }
21037 }
21038 }
21039 if (NILP (pointer))
21040 pointer = Fplist_get (XCDR (img->spec), QCpointer);
21041 }
21042 }
21043
21044 /* Clear mouse face if X/Y not over text. */
21045 if (glyph == NULL
21046 || area != TEXT_AREA
21047 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
21048 {
21049 if (clear_mouse_face (dpyinfo))
21050 cursor = No_Cursor;
21051 if (NILP (pointer))
21052 {
21053 if (area != TEXT_AREA)
21054 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21055 else
21056 pointer = Vvoid_text_area_pointer;
21057 }
21058 goto set_cursor;
21059 }
21060
21061 pos = glyph->charpos;
21062 object = glyph->object;
21063 if (!STRINGP (object) && !BUFFERP (object))
21064 goto set_cursor;
21065
21066 /* If we get an out-of-range value, return now; avoid an error. */
21067 if (BUFFERP (object) && pos > BUF_Z (b))
21068 goto set_cursor;
21069
21070 /* Make the window's buffer temporarily current for
21071 overlays_at and compute_char_face. */
21072 obuf = current_buffer;
21073 current_buffer = b;
21074 obegv = BEGV;
21075 ozv = ZV;
21076 BEGV = BEG;
21077 ZV = Z;
21078
21079 /* Is this char mouse-active or does it have help-echo? */
21080 position = make_number (pos);
21081
21082 if (BUFFERP (object))
21083 {
21084 /* Put all the overlays we want in a vector in overlay_vec. */
21085 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
21086 /* Sort overlays into increasing priority order. */
21087 noverlays = sort_overlays (overlay_vec, noverlays, w);
21088 }
21089 else
21090 noverlays = 0;
21091
21092 same_region = (EQ (window, dpyinfo->mouse_face_window)
21093 && vpos >= dpyinfo->mouse_face_beg_row
21094 && vpos <= dpyinfo->mouse_face_end_row
21095 && (vpos > dpyinfo->mouse_face_beg_row
21096 || hpos >= dpyinfo->mouse_face_beg_col)
21097 && (vpos < dpyinfo->mouse_face_end_row
21098 || hpos < dpyinfo->mouse_face_end_col
21099 || dpyinfo->mouse_face_past_end));
21100
21101 if (same_region)
21102 cursor = No_Cursor;
21103
21104 /* Check mouse-face highlighting. */
21105 if (! same_region
21106 /* If there exists an overlay with mouse-face overlapping
21107 the one we are currently highlighting, we have to
21108 check if we enter the overlapping overlay, and then
21109 highlight only that. */
21110 || (OVERLAYP (dpyinfo->mouse_face_overlay)
21111 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
21112 {
21113 /* Find the highest priority overlay that has a mouse-face
21114 property. */
21115 overlay = Qnil;
21116 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
21117 {
21118 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
21119 if (!NILP (mouse_face))
21120 overlay = overlay_vec[i];
21121 }
21122
21123 /* If we're actually highlighting the same overlay as
21124 before, there's no need to do that again. */
21125 if (!NILP (overlay)
21126 && EQ (overlay, dpyinfo->mouse_face_overlay))
21127 goto check_help_echo;
21128
21129 dpyinfo->mouse_face_overlay = overlay;
21130
21131 /* Clear the display of the old active region, if any. */
21132 if (clear_mouse_face (dpyinfo))
21133 cursor = No_Cursor;
21134
21135 /* If no overlay applies, get a text property. */
21136 if (NILP (overlay))
21137 mouse_face = Fget_text_property (position, Qmouse_face, object);
21138
21139 /* Handle the overlay case. */
21140 if (!NILP (overlay))
21141 {
21142 /* Find the range of text around this char that
21143 should be active. */
21144 Lisp_Object before, after;
21145 int ignore;
21146
21147 before = Foverlay_start (overlay);
21148 after = Foverlay_end (overlay);
21149 /* Record this as the current active region. */
21150 fast_find_position (w, XFASTINT (before),
21151 &dpyinfo->mouse_face_beg_col,
21152 &dpyinfo->mouse_face_beg_row,
21153 &dpyinfo->mouse_face_beg_x,
21154 &dpyinfo->mouse_face_beg_y, Qnil);
21155
21156 dpyinfo->mouse_face_past_end
21157 = !fast_find_position (w, XFASTINT (after),
21158 &dpyinfo->mouse_face_end_col,
21159 &dpyinfo->mouse_face_end_row,
21160 &dpyinfo->mouse_face_end_x,
21161 &dpyinfo->mouse_face_end_y, Qnil);
21162 dpyinfo->mouse_face_window = window;
21163
21164 dpyinfo->mouse_face_face_id
21165 = face_at_buffer_position (w, pos, 0, 0,
21166 &ignore, pos + 1,
21167 !dpyinfo->mouse_face_hidden);
21168
21169 /* Display it as active. */
21170 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21171 cursor = No_Cursor;
21172 }
21173 /* Handle the text property case. */
21174 else if (!NILP (mouse_face) && BUFFERP (object))
21175 {
21176 /* Find the range of text around this char that
21177 should be active. */
21178 Lisp_Object before, after, beginning, end;
21179 int ignore;
21180
21181 beginning = Fmarker_position (w->start);
21182 end = make_number (BUF_Z (XBUFFER (object))
21183 - XFASTINT (w->window_end_pos));
21184 before
21185 = Fprevious_single_property_change (make_number (pos + 1),
21186 Qmouse_face,
21187 object, beginning);
21188 after
21189 = Fnext_single_property_change (position, Qmouse_face,
21190 object, end);
21191
21192 /* Record this as the current active region. */
21193 fast_find_position (w, XFASTINT (before),
21194 &dpyinfo->mouse_face_beg_col,
21195 &dpyinfo->mouse_face_beg_row,
21196 &dpyinfo->mouse_face_beg_x,
21197 &dpyinfo->mouse_face_beg_y, Qnil);
21198 dpyinfo->mouse_face_past_end
21199 = !fast_find_position (w, XFASTINT (after),
21200 &dpyinfo->mouse_face_end_col,
21201 &dpyinfo->mouse_face_end_row,
21202 &dpyinfo->mouse_face_end_x,
21203 &dpyinfo->mouse_face_end_y, Qnil);
21204 dpyinfo->mouse_face_window = window;
21205
21206 if (BUFFERP (object))
21207 dpyinfo->mouse_face_face_id
21208 = face_at_buffer_position (w, pos, 0, 0,
21209 &ignore, pos + 1,
21210 !dpyinfo->mouse_face_hidden);
21211
21212 /* Display it as active. */
21213 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21214 cursor = No_Cursor;
21215 }
21216 else if (!NILP (mouse_face) && STRINGP (object))
21217 {
21218 Lisp_Object b, e;
21219 int ignore;
21220
21221 b = Fprevious_single_property_change (make_number (pos + 1),
21222 Qmouse_face,
21223 object, Qnil);
21224 e = Fnext_single_property_change (position, Qmouse_face,
21225 object, Qnil);
21226 if (NILP (b))
21227 b = make_number (0);
21228 if (NILP (e))
21229 e = make_number (SCHARS (object) - 1);
21230 fast_find_string_pos (w, XINT (b), object,
21231 &dpyinfo->mouse_face_beg_col,
21232 &dpyinfo->mouse_face_beg_row,
21233 &dpyinfo->mouse_face_beg_x,
21234 &dpyinfo->mouse_face_beg_y, 0);
21235 fast_find_string_pos (w, XINT (e), object,
21236 &dpyinfo->mouse_face_end_col,
21237 &dpyinfo->mouse_face_end_row,
21238 &dpyinfo->mouse_face_end_x,
21239 &dpyinfo->mouse_face_end_y, 1);
21240 dpyinfo->mouse_face_past_end = 0;
21241 dpyinfo->mouse_face_window = window;
21242 dpyinfo->mouse_face_face_id
21243 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
21244 glyph->face_id, 1);
21245 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21246 cursor = No_Cursor;
21247 }
21248 else if (STRINGP (object) && NILP (mouse_face))
21249 {
21250 /* A string which doesn't have mouse-face, but
21251 the text ``under'' it might have. */
21252 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
21253 int start = MATRIX_ROW_START_CHARPOS (r);
21254
21255 pos = string_buffer_position (w, object, start);
21256 if (pos > 0)
21257 mouse_face = get_char_property_and_overlay (make_number (pos),
21258 Qmouse_face,
21259 w->buffer,
21260 &overlay);
21261 if (!NILP (mouse_face) && !NILP (overlay))
21262 {
21263 Lisp_Object before = Foverlay_start (overlay);
21264 Lisp_Object after = Foverlay_end (overlay);
21265 int ignore;
21266
21267 /* Note that we might not be able to find position
21268 BEFORE in the glyph matrix if the overlay is
21269 entirely covered by a `display' property. In
21270 this case, we overshoot. So let's stop in
21271 the glyph matrix before glyphs for OBJECT. */
21272 fast_find_position (w, XFASTINT (before),
21273 &dpyinfo->mouse_face_beg_col,
21274 &dpyinfo->mouse_face_beg_row,
21275 &dpyinfo->mouse_face_beg_x,
21276 &dpyinfo->mouse_face_beg_y,
21277 object);
21278
21279 dpyinfo->mouse_face_past_end
21280 = !fast_find_position (w, XFASTINT (after),
21281 &dpyinfo->mouse_face_end_col,
21282 &dpyinfo->mouse_face_end_row,
21283 &dpyinfo->mouse_face_end_x,
21284 &dpyinfo->mouse_face_end_y,
21285 Qnil);
21286 dpyinfo->mouse_face_window = window;
21287 dpyinfo->mouse_face_face_id
21288 = face_at_buffer_position (w, pos, 0, 0,
21289 &ignore, pos + 1,
21290 !dpyinfo->mouse_face_hidden);
21291
21292 /* Display it as active. */
21293 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21294 cursor = No_Cursor;
21295 }
21296 }
21297 }
21298
21299 check_help_echo:
21300
21301 /* Look for a `help-echo' property. */
21302 if (NILP (help_echo_string)) {
21303 Lisp_Object help, overlay;
21304
21305 /* Check overlays first. */
21306 help = overlay = Qnil;
21307 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
21308 {
21309 overlay = overlay_vec[i];
21310 help = Foverlay_get (overlay, Qhelp_echo);
21311 }
21312
21313 if (!NILP (help))
21314 {
21315 help_echo_string = help;
21316 help_echo_window = window;
21317 help_echo_object = overlay;
21318 help_echo_pos = pos;
21319 }
21320 else
21321 {
21322 Lisp_Object object = glyph->object;
21323 int charpos = glyph->charpos;
21324
21325 /* Try text properties. */
21326 if (STRINGP (object)
21327 && charpos >= 0
21328 && charpos < SCHARS (object))
21329 {
21330 help = Fget_text_property (make_number (charpos),
21331 Qhelp_echo, object);
21332 if (NILP (help))
21333 {
21334 /* If the string itself doesn't specify a help-echo,
21335 see if the buffer text ``under'' it does. */
21336 struct glyph_row *r
21337 = MATRIX_ROW (w->current_matrix, vpos);
21338 int start = MATRIX_ROW_START_CHARPOS (r);
21339 int pos = string_buffer_position (w, object, start);
21340 if (pos > 0)
21341 {
21342 help = Fget_char_property (make_number (pos),
21343 Qhelp_echo, w->buffer);
21344 if (!NILP (help))
21345 {
21346 charpos = pos;
21347 object = w->buffer;
21348 }
21349 }
21350 }
21351 }
21352 else if (BUFFERP (object)
21353 && charpos >= BEGV
21354 && charpos < ZV)
21355 help = Fget_text_property (make_number (charpos), Qhelp_echo,
21356 object);
21357
21358 if (!NILP (help))
21359 {
21360 help_echo_string = help;
21361 help_echo_window = window;
21362 help_echo_object = object;
21363 help_echo_pos = charpos;
21364 }
21365 }
21366 }
21367
21368 /* Look for a `pointer' property. */
21369 if (NILP (pointer))
21370 {
21371 /* Check overlays first. */
21372 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
21373 pointer = Foverlay_get (overlay_vec[i], Qpointer);
21374
21375 if (NILP (pointer))
21376 {
21377 Lisp_Object object = glyph->object;
21378 int charpos = glyph->charpos;
21379
21380 /* Try text properties. */
21381 if (STRINGP (object)
21382 && charpos >= 0
21383 && charpos < SCHARS (object))
21384 {
21385 pointer = Fget_text_property (make_number (charpos),
21386 Qpointer, object);
21387 if (NILP (pointer))
21388 {
21389 /* If the string itself doesn't specify a pointer,
21390 see if the buffer text ``under'' it does. */
21391 struct glyph_row *r
21392 = MATRIX_ROW (w->current_matrix, vpos);
21393 int start = MATRIX_ROW_START_CHARPOS (r);
21394 int pos = string_buffer_position (w, object, start);
21395 if (pos > 0)
21396 pointer = Fget_char_property (make_number (pos),
21397 Qpointer, w->buffer);
21398 }
21399 }
21400 else if (BUFFERP (object)
21401 && charpos >= BEGV
21402 && charpos < ZV)
21403 pointer = Fget_text_property (make_number (charpos),
21404 Qpointer, object);
21405 }
21406 }
21407
21408 BEGV = obegv;
21409 ZV = ozv;
21410 current_buffer = obuf;
21411 }
21412
21413 set_cursor:
21414
21415 define_frame_cursor1 (f, cursor, pointer);
21416 }
21417
21418
21419 /* EXPORT for RIF:
21420 Clear any mouse-face on window W. This function is part of the
21421 redisplay interface, and is called from try_window_id and similar
21422 functions to ensure the mouse-highlight is off. */
21423
21424 void
21425 x_clear_window_mouse_face (w)
21426 struct window *w;
21427 {
21428 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21429 Lisp_Object window;
21430
21431 BLOCK_INPUT;
21432 XSETWINDOW (window, w);
21433 if (EQ (window, dpyinfo->mouse_face_window))
21434 clear_mouse_face (dpyinfo);
21435 UNBLOCK_INPUT;
21436 }
21437
21438
21439 /* EXPORT:
21440 Just discard the mouse face information for frame F, if any.
21441 This is used when the size of F is changed. */
21442
21443 void
21444 cancel_mouse_face (f)
21445 struct frame *f;
21446 {
21447 Lisp_Object window;
21448 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21449
21450 window = dpyinfo->mouse_face_window;
21451 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
21452 {
21453 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21454 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21455 dpyinfo->mouse_face_window = Qnil;
21456 }
21457 }
21458
21459
21460 #endif /* HAVE_WINDOW_SYSTEM */
21461
21462 \f
21463 /***********************************************************************
21464 Exposure Events
21465 ***********************************************************************/
21466
21467 #ifdef HAVE_WINDOW_SYSTEM
21468
21469 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
21470 which intersects rectangle R. R is in window-relative coordinates. */
21471
21472 static void
21473 expose_area (w, row, r, area)
21474 struct window *w;
21475 struct glyph_row *row;
21476 XRectangle *r;
21477 enum glyph_row_area area;
21478 {
21479 struct glyph *first = row->glyphs[area];
21480 struct glyph *end = row->glyphs[area] + row->used[area];
21481 struct glyph *last;
21482 int first_x, start_x, x;
21483
21484 if (area == TEXT_AREA && row->fill_line_p)
21485 /* If row extends face to end of line write the whole line. */
21486 draw_glyphs (w, 0, row, area,
21487 0, row->used[area],
21488 DRAW_NORMAL_TEXT, 0);
21489 else
21490 {
21491 /* Set START_X to the window-relative start position for drawing glyphs of
21492 AREA. The first glyph of the text area can be partially visible.
21493 The first glyphs of other areas cannot. */
21494 start_x = window_box_left_offset (w, area);
21495 x = start_x;
21496 if (area == TEXT_AREA)
21497 x += row->x;
21498
21499 /* Find the first glyph that must be redrawn. */
21500 while (first < end
21501 && x + first->pixel_width < r->x)
21502 {
21503 x += first->pixel_width;
21504 ++first;
21505 }
21506
21507 /* Find the last one. */
21508 last = first;
21509 first_x = x;
21510 while (last < end
21511 && x < r->x + r->width)
21512 {
21513 x += last->pixel_width;
21514 ++last;
21515 }
21516
21517 /* Repaint. */
21518 if (last > first)
21519 draw_glyphs (w, first_x - start_x, row, area,
21520 first - row->glyphs[area], last - row->glyphs[area],
21521 DRAW_NORMAL_TEXT, 0);
21522 }
21523 }
21524
21525
21526 /* Redraw the parts of the glyph row ROW on window W intersecting
21527 rectangle R. R is in window-relative coordinates. Value is
21528 non-zero if mouse-face was overwritten. */
21529
21530 static int
21531 expose_line (w, row, r)
21532 struct window *w;
21533 struct glyph_row *row;
21534 XRectangle *r;
21535 {
21536 xassert (row->enabled_p);
21537
21538 if (row->mode_line_p || w->pseudo_window_p)
21539 draw_glyphs (w, 0, row, TEXT_AREA,
21540 0, row->used[TEXT_AREA],
21541 DRAW_NORMAL_TEXT, 0);
21542 else
21543 {
21544 if (row->used[LEFT_MARGIN_AREA])
21545 expose_area (w, row, r, LEFT_MARGIN_AREA);
21546 if (row->used[TEXT_AREA])
21547 expose_area (w, row, r, TEXT_AREA);
21548 if (row->used[RIGHT_MARGIN_AREA])
21549 expose_area (w, row, r, RIGHT_MARGIN_AREA);
21550 draw_row_fringe_bitmaps (w, row);
21551 }
21552
21553 return row->mouse_face_p;
21554 }
21555
21556
21557 /* Redraw those parts of glyphs rows during expose event handling that
21558 overlap other rows. Redrawing of an exposed line writes over parts
21559 of lines overlapping that exposed line; this function fixes that.
21560
21561 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
21562 row in W's current matrix that is exposed and overlaps other rows.
21563 LAST_OVERLAPPING_ROW is the last such row. */
21564
21565 static void
21566 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
21567 struct window *w;
21568 struct glyph_row *first_overlapping_row;
21569 struct glyph_row *last_overlapping_row;
21570 {
21571 struct glyph_row *row;
21572
21573 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
21574 if (row->overlapping_p)
21575 {
21576 xassert (row->enabled_p && !row->mode_line_p);
21577
21578 if (row->used[LEFT_MARGIN_AREA])
21579 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
21580
21581 if (row->used[TEXT_AREA])
21582 x_fix_overlapping_area (w, row, TEXT_AREA);
21583
21584 if (row->used[RIGHT_MARGIN_AREA])
21585 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
21586 }
21587 }
21588
21589
21590 /* Return non-zero if W's cursor intersects rectangle R. */
21591
21592 static int
21593 phys_cursor_in_rect_p (w, r)
21594 struct window *w;
21595 XRectangle *r;
21596 {
21597 XRectangle cr, result;
21598 struct glyph *cursor_glyph;
21599
21600 cursor_glyph = get_phys_cursor_glyph (w);
21601 if (cursor_glyph)
21602 {
21603 /* r is relative to W's box, but w->phys_cursor.x is relative
21604 to left edge of W's TEXT area. Adjust it. */
21605 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
21606 cr.y = w->phys_cursor.y;
21607 cr.width = cursor_glyph->pixel_width;
21608 cr.height = w->phys_cursor_height;
21609 /* ++KFS: W32 version used W32-specific IntersectRect here, but
21610 I assume the effect is the same -- and this is portable. */
21611 return x_intersect_rectangles (&cr, r, &result);
21612 }
21613 else
21614 return 0;
21615 }
21616
21617
21618 /* EXPORT:
21619 Draw a vertical window border to the right of window W if W doesn't
21620 have vertical scroll bars. */
21621
21622 void
21623 x_draw_vertical_border (w)
21624 struct window *w;
21625 {
21626 /* We could do better, if we knew what type of scroll-bar the adjacent
21627 windows (on either side) have... But we don't :-(
21628 However, I think this works ok. ++KFS 2003-04-25 */
21629
21630 /* Redraw borders between horizontally adjacent windows. Don't
21631 do it for frames with vertical scroll bars because either the
21632 right scroll bar of a window, or the left scroll bar of its
21633 neighbor will suffice as a border. */
21634 if (!WINDOW_RIGHTMOST_P (w)
21635 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
21636 {
21637 int x0, x1, y0, y1;
21638
21639 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21640 y1 -= 1;
21641
21642 rif->draw_vertical_window_border (w, x1, y0, y1);
21643 }
21644 else if (!WINDOW_LEFTMOST_P (w)
21645 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
21646 {
21647 int x0, x1, y0, y1;
21648
21649 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21650 y1 -= 1;
21651
21652 rif->draw_vertical_window_border (w, x0, y0, y1);
21653 }
21654 }
21655
21656
21657 /* Redraw the part of window W intersection rectangle FR. Pixel
21658 coordinates in FR are frame-relative. Call this function with
21659 input blocked. Value is non-zero if the exposure overwrites
21660 mouse-face. */
21661
21662 static int
21663 expose_window (w, fr)
21664 struct window *w;
21665 XRectangle *fr;
21666 {
21667 struct frame *f = XFRAME (w->frame);
21668 XRectangle wr, r;
21669 int mouse_face_overwritten_p = 0;
21670
21671 /* If window is not yet fully initialized, do nothing. This can
21672 happen when toolkit scroll bars are used and a window is split.
21673 Reconfiguring the scroll bar will generate an expose for a newly
21674 created window. */
21675 if (w->current_matrix == NULL)
21676 return 0;
21677
21678 /* When we're currently updating the window, display and current
21679 matrix usually don't agree. Arrange for a thorough display
21680 later. */
21681 if (w == updated_window)
21682 {
21683 SET_FRAME_GARBAGED (f);
21684 return 0;
21685 }
21686
21687 /* Frame-relative pixel rectangle of W. */
21688 wr.x = WINDOW_LEFT_EDGE_X (w);
21689 wr.y = WINDOW_TOP_EDGE_Y (w);
21690 wr.width = WINDOW_TOTAL_WIDTH (w);
21691 wr.height = WINDOW_TOTAL_HEIGHT (w);
21692
21693 if (x_intersect_rectangles (fr, &wr, &r))
21694 {
21695 int yb = window_text_bottom_y (w);
21696 struct glyph_row *row;
21697 int cursor_cleared_p;
21698 struct glyph_row *first_overlapping_row, *last_overlapping_row;
21699
21700 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
21701 r.x, r.y, r.width, r.height));
21702
21703 /* Convert to window coordinates. */
21704 r.x -= WINDOW_LEFT_EDGE_X (w);
21705 r.y -= WINDOW_TOP_EDGE_Y (w);
21706
21707 /* Turn off the cursor. */
21708 if (!w->pseudo_window_p
21709 && phys_cursor_in_rect_p (w, &r))
21710 {
21711 x_clear_cursor (w);
21712 cursor_cleared_p = 1;
21713 }
21714 else
21715 cursor_cleared_p = 0;
21716
21717 /* Update lines intersecting rectangle R. */
21718 first_overlapping_row = last_overlapping_row = NULL;
21719 for (row = w->current_matrix->rows;
21720 row->enabled_p;
21721 ++row)
21722 {
21723 int y0 = row->y;
21724 int y1 = MATRIX_ROW_BOTTOM_Y (row);
21725
21726 if ((y0 >= r.y && y0 < r.y + r.height)
21727 || (y1 > r.y && y1 < r.y + r.height)
21728 || (r.y >= y0 && r.y < y1)
21729 || (r.y + r.height > y0 && r.y + r.height < y1))
21730 {
21731 if (row->overlapping_p)
21732 {
21733 if (first_overlapping_row == NULL)
21734 first_overlapping_row = row;
21735 last_overlapping_row = row;
21736 }
21737
21738 if (expose_line (w, row, &r))
21739 mouse_face_overwritten_p = 1;
21740 }
21741
21742 if (y1 >= yb)
21743 break;
21744 }
21745
21746 /* Display the mode line if there is one. */
21747 if (WINDOW_WANTS_MODELINE_P (w)
21748 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
21749 row->enabled_p)
21750 && row->y < r.y + r.height)
21751 {
21752 if (expose_line (w, row, &r))
21753 mouse_face_overwritten_p = 1;
21754 }
21755
21756 if (!w->pseudo_window_p)
21757 {
21758 /* Fix the display of overlapping rows. */
21759 if (first_overlapping_row)
21760 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
21761
21762 /* Draw border between windows. */
21763 x_draw_vertical_border (w);
21764
21765 /* Turn the cursor on again. */
21766 if (cursor_cleared_p)
21767 update_window_cursor (w, 1);
21768 }
21769 }
21770
21771 #ifdef HAVE_CARBON
21772 /* Display scroll bar for this window. */
21773 if (!NILP (w->vertical_scroll_bar))
21774 {
21775 /* ++KFS:
21776 If this doesn't work here (maybe some header files are missing),
21777 make a function in macterm.c and call it to do the job! */
21778 ControlHandle ch
21779 = SCROLL_BAR_CONTROL_HANDLE (XSCROLL_BAR (w->vertical_scroll_bar));
21780
21781 Draw1Control (ch);
21782 }
21783 #endif
21784
21785 return mouse_face_overwritten_p;
21786 }
21787
21788
21789
21790 /* Redraw (parts) of all windows in the window tree rooted at W that
21791 intersect R. R contains frame pixel coordinates. Value is
21792 non-zero if the exposure overwrites mouse-face. */
21793
21794 static int
21795 expose_window_tree (w, r)
21796 struct window *w;
21797 XRectangle *r;
21798 {
21799 struct frame *f = XFRAME (w->frame);
21800 int mouse_face_overwritten_p = 0;
21801
21802 while (w && !FRAME_GARBAGED_P (f))
21803 {
21804 if (!NILP (w->hchild))
21805 mouse_face_overwritten_p
21806 |= expose_window_tree (XWINDOW (w->hchild), r);
21807 else if (!NILP (w->vchild))
21808 mouse_face_overwritten_p
21809 |= expose_window_tree (XWINDOW (w->vchild), r);
21810 else
21811 mouse_face_overwritten_p |= expose_window (w, r);
21812
21813 w = NILP (w->next) ? NULL : XWINDOW (w->next);
21814 }
21815
21816 return mouse_face_overwritten_p;
21817 }
21818
21819
21820 /* EXPORT:
21821 Redisplay an exposed area of frame F. X and Y are the upper-left
21822 corner of the exposed rectangle. W and H are width and height of
21823 the exposed area. All are pixel values. W or H zero means redraw
21824 the entire frame. */
21825
21826 void
21827 expose_frame (f, x, y, w, h)
21828 struct frame *f;
21829 int x, y, w, h;
21830 {
21831 XRectangle r;
21832 int mouse_face_overwritten_p = 0;
21833
21834 TRACE ((stderr, "expose_frame "));
21835
21836 /* No need to redraw if frame will be redrawn soon. */
21837 if (FRAME_GARBAGED_P (f))
21838 {
21839 TRACE ((stderr, " garbaged\n"));
21840 return;
21841 }
21842
21843 #ifdef HAVE_CARBON
21844 /* MAC_TODO: this is a kludge, but if scroll bars are not activated
21845 or deactivated here, for unknown reasons, activated scroll bars
21846 are shown in deactivated frames in some instances. */
21847 if (f == FRAME_MAC_DISPLAY_INFO (f)->x_focus_frame)
21848 activate_scroll_bars (f);
21849 else
21850 deactivate_scroll_bars (f);
21851 #endif
21852
21853 /* If basic faces haven't been realized yet, there is no point in
21854 trying to redraw anything. This can happen when we get an expose
21855 event while Emacs is starting, e.g. by moving another window. */
21856 if (FRAME_FACE_CACHE (f) == NULL
21857 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
21858 {
21859 TRACE ((stderr, " no faces\n"));
21860 return;
21861 }
21862
21863 if (w == 0 || h == 0)
21864 {
21865 r.x = r.y = 0;
21866 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
21867 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
21868 }
21869 else
21870 {
21871 r.x = x;
21872 r.y = y;
21873 r.width = w;
21874 r.height = h;
21875 }
21876
21877 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
21878 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
21879
21880 if (WINDOWP (f->tool_bar_window))
21881 mouse_face_overwritten_p
21882 |= expose_window (XWINDOW (f->tool_bar_window), &r);
21883
21884 #ifdef HAVE_X_WINDOWS
21885 #ifndef MSDOS
21886 #ifndef USE_X_TOOLKIT
21887 if (WINDOWP (f->menu_bar_window))
21888 mouse_face_overwritten_p
21889 |= expose_window (XWINDOW (f->menu_bar_window), &r);
21890 #endif /* not USE_X_TOOLKIT */
21891 #endif
21892 #endif
21893
21894 /* Some window managers support a focus-follows-mouse style with
21895 delayed raising of frames. Imagine a partially obscured frame,
21896 and moving the mouse into partially obscured mouse-face on that
21897 frame. The visible part of the mouse-face will be highlighted,
21898 then the WM raises the obscured frame. With at least one WM, KDE
21899 2.1, Emacs is not getting any event for the raising of the frame
21900 (even tried with SubstructureRedirectMask), only Expose events.
21901 These expose events will draw text normally, i.e. not
21902 highlighted. Which means we must redo the highlight here.
21903 Subsume it under ``we love X''. --gerd 2001-08-15 */
21904 /* Included in Windows version because Windows most likely does not
21905 do the right thing if any third party tool offers
21906 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
21907 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
21908 {
21909 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21910 if (f == dpyinfo->mouse_face_mouse_frame)
21911 {
21912 int x = dpyinfo->mouse_face_mouse_x;
21913 int y = dpyinfo->mouse_face_mouse_y;
21914 clear_mouse_face (dpyinfo);
21915 note_mouse_highlight (f, x, y);
21916 }
21917 }
21918 }
21919
21920
21921 /* EXPORT:
21922 Determine the intersection of two rectangles R1 and R2. Return
21923 the intersection in *RESULT. Value is non-zero if RESULT is not
21924 empty. */
21925
21926 int
21927 x_intersect_rectangles (r1, r2, result)
21928 XRectangle *r1, *r2, *result;
21929 {
21930 XRectangle *left, *right;
21931 XRectangle *upper, *lower;
21932 int intersection_p = 0;
21933
21934 /* Rearrange so that R1 is the left-most rectangle. */
21935 if (r1->x < r2->x)
21936 left = r1, right = r2;
21937 else
21938 left = r2, right = r1;
21939
21940 /* X0 of the intersection is right.x0, if this is inside R1,
21941 otherwise there is no intersection. */
21942 if (right->x <= left->x + left->width)
21943 {
21944 result->x = right->x;
21945
21946 /* The right end of the intersection is the minimum of the
21947 the right ends of left and right. */
21948 result->width = (min (left->x + left->width, right->x + right->width)
21949 - result->x);
21950
21951 /* Same game for Y. */
21952 if (r1->y < r2->y)
21953 upper = r1, lower = r2;
21954 else
21955 upper = r2, lower = r1;
21956
21957 /* The upper end of the intersection is lower.y0, if this is inside
21958 of upper. Otherwise, there is no intersection. */
21959 if (lower->y <= upper->y + upper->height)
21960 {
21961 result->y = lower->y;
21962
21963 /* The lower end of the intersection is the minimum of the lower
21964 ends of upper and lower. */
21965 result->height = (min (lower->y + lower->height,
21966 upper->y + upper->height)
21967 - result->y);
21968 intersection_p = 1;
21969 }
21970 }
21971
21972 return intersection_p;
21973 }
21974
21975 #endif /* HAVE_WINDOW_SYSTEM */
21976
21977 \f
21978 /***********************************************************************
21979 Initialization
21980 ***********************************************************************/
21981
21982 void
21983 syms_of_xdisp ()
21984 {
21985 Vwith_echo_area_save_vector = Qnil;
21986 staticpro (&Vwith_echo_area_save_vector);
21987
21988 Vmessage_stack = Qnil;
21989 staticpro (&Vmessage_stack);
21990
21991 Qinhibit_redisplay = intern ("inhibit-redisplay");
21992 staticpro (&Qinhibit_redisplay);
21993
21994 message_dolog_marker1 = Fmake_marker ();
21995 staticpro (&message_dolog_marker1);
21996 message_dolog_marker2 = Fmake_marker ();
21997 staticpro (&message_dolog_marker2);
21998 message_dolog_marker3 = Fmake_marker ();
21999 staticpro (&message_dolog_marker3);
22000
22001 #if GLYPH_DEBUG
22002 defsubr (&Sdump_frame_glyph_matrix);
22003 defsubr (&Sdump_glyph_matrix);
22004 defsubr (&Sdump_glyph_row);
22005 defsubr (&Sdump_tool_bar_row);
22006 defsubr (&Strace_redisplay);
22007 defsubr (&Strace_to_stderr);
22008 #endif
22009 #ifdef HAVE_WINDOW_SYSTEM
22010 defsubr (&Stool_bar_lines_needed);
22011 defsubr (&Slookup_image_map);
22012 #endif
22013 defsubr (&Sformat_mode_line);
22014
22015 staticpro (&Qmenu_bar_update_hook);
22016 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
22017
22018 staticpro (&Qoverriding_terminal_local_map);
22019 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
22020
22021 staticpro (&Qoverriding_local_map);
22022 Qoverriding_local_map = intern ("overriding-local-map");
22023
22024 staticpro (&Qwindow_scroll_functions);
22025 Qwindow_scroll_functions = intern ("window-scroll-functions");
22026
22027 staticpro (&Qredisplay_end_trigger_functions);
22028 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
22029
22030 staticpro (&Qinhibit_point_motion_hooks);
22031 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
22032
22033 QCdata = intern (":data");
22034 staticpro (&QCdata);
22035 Qdisplay = intern ("display");
22036 staticpro (&Qdisplay);
22037 Qspace_width = intern ("space-width");
22038 staticpro (&Qspace_width);
22039 Qraise = intern ("raise");
22040 staticpro (&Qraise);
22041 Qslice = intern ("slice");
22042 staticpro (&Qslice);
22043 Qspace = intern ("space");
22044 staticpro (&Qspace);
22045 Qmargin = intern ("margin");
22046 staticpro (&Qmargin);
22047 Qpointer = intern ("pointer");
22048 staticpro (&Qpointer);
22049 Qleft_margin = intern ("left-margin");
22050 staticpro (&Qleft_margin);
22051 Qright_margin = intern ("right-margin");
22052 staticpro (&Qright_margin);
22053 Qcenter = intern ("center");
22054 staticpro (&Qcenter);
22055 Qline_height = intern ("line-height");
22056 staticpro (&Qline_height);
22057 Qtotal = intern ("total");
22058 staticpro (&Qtotal);
22059 QCalign_to = intern (":align-to");
22060 staticpro (&QCalign_to);
22061 QCrelative_width = intern (":relative-width");
22062 staticpro (&QCrelative_width);
22063 QCrelative_height = intern (":relative-height");
22064 staticpro (&QCrelative_height);
22065 QCeval = intern (":eval");
22066 staticpro (&QCeval);
22067 QCpropertize = intern (":propertize");
22068 staticpro (&QCpropertize);
22069 QCfile = intern (":file");
22070 staticpro (&QCfile);
22071 Qfontified = intern ("fontified");
22072 staticpro (&Qfontified);
22073 Qfontification_functions = intern ("fontification-functions");
22074 staticpro (&Qfontification_functions);
22075 Qtrailing_whitespace = intern ("trailing-whitespace");
22076 staticpro (&Qtrailing_whitespace);
22077 Qimage = intern ("image");
22078 staticpro (&Qimage);
22079 QCmap = intern (":map");
22080 staticpro (&QCmap);
22081 QCpointer = intern (":pointer");
22082 staticpro (&QCpointer);
22083 Qrect = intern ("rect");
22084 staticpro (&Qrect);
22085 Qcircle = intern ("circle");
22086 staticpro (&Qcircle);
22087 Qpoly = intern ("poly");
22088 staticpro (&Qpoly);
22089 Qmessage_truncate_lines = intern ("message-truncate-lines");
22090 staticpro (&Qmessage_truncate_lines);
22091 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
22092 staticpro (&Qcursor_in_non_selected_windows);
22093 Qgrow_only = intern ("grow-only");
22094 staticpro (&Qgrow_only);
22095 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
22096 staticpro (&Qinhibit_menubar_update);
22097 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
22098 staticpro (&Qinhibit_eval_during_redisplay);
22099 Qposition = intern ("position");
22100 staticpro (&Qposition);
22101 Qbuffer_position = intern ("buffer-position");
22102 staticpro (&Qbuffer_position);
22103 Qobject = intern ("object");
22104 staticpro (&Qobject);
22105 Qbar = intern ("bar");
22106 staticpro (&Qbar);
22107 Qhbar = intern ("hbar");
22108 staticpro (&Qhbar);
22109 Qbox = intern ("box");
22110 staticpro (&Qbox);
22111 Qhollow = intern ("hollow");
22112 staticpro (&Qhollow);
22113 Qhand = intern ("hand");
22114 staticpro (&Qhand);
22115 Qarrow = intern ("arrow");
22116 staticpro (&Qarrow);
22117 Qtext = intern ("text");
22118 staticpro (&Qtext);
22119 Qrisky_local_variable = intern ("risky-local-variable");
22120 staticpro (&Qrisky_local_variable);
22121 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
22122 staticpro (&Qinhibit_free_realized_faces);
22123
22124 list_of_error = Fcons (Fcons (intern ("error"),
22125 Fcons (intern ("void-variable"), Qnil)),
22126 Qnil);
22127 staticpro (&list_of_error);
22128
22129 Qlast_arrow_position = intern ("last-arrow-position");
22130 staticpro (&Qlast_arrow_position);
22131 Qlast_arrow_string = intern ("last-arrow-string");
22132 staticpro (&Qlast_arrow_string);
22133
22134 Qoverlay_arrow_string = intern ("overlay-arrow-string");
22135 staticpro (&Qoverlay_arrow_string);
22136 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
22137 staticpro (&Qoverlay_arrow_bitmap);
22138
22139 echo_buffer[0] = echo_buffer[1] = Qnil;
22140 staticpro (&echo_buffer[0]);
22141 staticpro (&echo_buffer[1]);
22142
22143 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
22144 staticpro (&echo_area_buffer[0]);
22145 staticpro (&echo_area_buffer[1]);
22146
22147 Vmessages_buffer_name = build_string ("*Messages*");
22148 staticpro (&Vmessages_buffer_name);
22149
22150 mode_line_proptrans_alist = Qnil;
22151 staticpro (&mode_line_proptrans_alist);
22152
22153 mode_line_string_list = Qnil;
22154 staticpro (&mode_line_string_list);
22155
22156 help_echo_string = Qnil;
22157 staticpro (&help_echo_string);
22158 help_echo_object = Qnil;
22159 staticpro (&help_echo_object);
22160 help_echo_window = Qnil;
22161 staticpro (&help_echo_window);
22162 previous_help_echo_string = Qnil;
22163 staticpro (&previous_help_echo_string);
22164 help_echo_pos = -1;
22165
22166 #ifdef HAVE_WINDOW_SYSTEM
22167 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
22168 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
22169 For example, if a block cursor is over a tab, it will be drawn as
22170 wide as that tab on the display. */);
22171 x_stretch_cursor_p = 0;
22172 #endif
22173
22174 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
22175 doc: /* *Non-nil means highlight trailing whitespace.
22176 The face used for trailing whitespace is `trailing-whitespace'. */);
22177 Vshow_trailing_whitespace = Qnil;
22178
22179 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
22180 doc: /* *The pointer shape to show in void text areas.
22181 Nil means to show the text pointer. Other options are `arrow', `text',
22182 `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
22183 Vvoid_text_area_pointer = Qarrow;
22184
22185 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
22186 doc: /* Non-nil means don't actually do any redisplay.
22187 This is used for internal purposes. */);
22188 Vinhibit_redisplay = Qnil;
22189
22190 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
22191 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
22192 Vglobal_mode_string = Qnil;
22193
22194 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
22195 doc: /* Marker for where to display an arrow on top of the buffer text.
22196 This must be the beginning of a line in order to work.
22197 See also `overlay-arrow-string'. */);
22198 Voverlay_arrow_position = Qnil;
22199
22200 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
22201 doc: /* String to display as an arrow in non-window frames.
22202 See also `overlay-arrow-position'. */);
22203 Voverlay_arrow_string = Qnil;
22204
22205 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
22206 doc: /* List of variables (symbols) which hold markers for overlay arrows.
22207 The symbols on this list are examined during redisplay to determine
22208 where to display overlay arrows. */);
22209 Voverlay_arrow_variable_list
22210 = Fcons (intern ("overlay-arrow-position"), Qnil);
22211
22212 DEFVAR_INT ("scroll-step", &scroll_step,
22213 doc: /* *The number of lines to try scrolling a window by when point moves out.
22214 If that fails to bring point back on frame, point is centered instead.
22215 If this is zero, point is always centered after it moves off frame.
22216 If you want scrolling to always be a line at a time, you should set
22217 `scroll-conservatively' to a large value rather than set this to 1. */);
22218
22219 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
22220 doc: /* *Scroll up to this many lines, to bring point back on screen.
22221 A value of zero means to scroll the text to center point vertically
22222 in the window. */);
22223 scroll_conservatively = 0;
22224
22225 DEFVAR_INT ("scroll-margin", &scroll_margin,
22226 doc: /* *Number of lines of margin at the top and bottom of a window.
22227 Recenter the window whenever point gets within this many lines
22228 of the top or bottom of the window. */);
22229 scroll_margin = 0;
22230
22231 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
22232 doc: /* Pixels per inch on current display.
22233 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
22234 Vdisplay_pixels_per_inch = make_float (72.0);
22235
22236 #if GLYPH_DEBUG
22237 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
22238 #endif
22239
22240 DEFVAR_BOOL ("truncate-partial-width-windows",
22241 &truncate_partial_width_windows,
22242 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
22243 truncate_partial_width_windows = 1;
22244
22245 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
22246 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
22247 Any other value means to use the appropriate face, `mode-line',
22248 `header-line', or `menu' respectively. */);
22249 mode_line_inverse_video = 1;
22250
22251 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
22252 doc: /* *Maximum buffer size for which line number should be displayed.
22253 If the buffer is bigger than this, the line number does not appear
22254 in the mode line. A value of nil means no limit. */);
22255 Vline_number_display_limit = Qnil;
22256
22257 DEFVAR_INT ("line-number-display-limit-width",
22258 &line_number_display_limit_width,
22259 doc: /* *Maximum line width (in characters) for line number display.
22260 If the average length of the lines near point is bigger than this, then the
22261 line number may be omitted from the mode line. */);
22262 line_number_display_limit_width = 200;
22263
22264 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
22265 doc: /* *Non-nil means highlight region even in nonselected windows. */);
22266 highlight_nonselected_windows = 0;
22267
22268 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
22269 doc: /* Non-nil if more than one frame is visible on this display.
22270 Minibuffer-only frames don't count, but iconified frames do.
22271 This variable is not guaranteed to be accurate except while processing
22272 `frame-title-format' and `icon-title-format'. */);
22273
22274 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
22275 doc: /* Template for displaying the title bar of visible frames.
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
22281 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
22282 doc: /* Template for displaying the title bar of an iconified frame.
22283 \(Assuming the window manager supports this feature.)
22284 This variable has the same structure as `mode-line-format' (which see),
22285 and is used only on frames for which no explicit name has been set
22286 \(see `modify-frame-parameters'). */);
22287 Vicon_title_format
22288 = Vframe_title_format
22289 = Fcons (intern ("multiple-frames"),
22290 Fcons (build_string ("%b"),
22291 Fcons (Fcons (empty_string,
22292 Fcons (intern ("invocation-name"),
22293 Fcons (build_string ("@"),
22294 Fcons (intern ("system-name"),
22295 Qnil)))),
22296 Qnil)));
22297
22298 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
22299 doc: /* Maximum number of lines to keep in the message log buffer.
22300 If nil, disable message logging. If t, log messages but don't truncate
22301 the buffer when it becomes large. */);
22302 Vmessage_log_max = make_number (50);
22303
22304 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
22305 doc: /* Functions called before redisplay, if window sizes have changed.
22306 The value should be a list of functions that take one argument.
22307 Just before redisplay, for each frame, if any of its windows have changed
22308 size since the last redisplay, or have been split or deleted,
22309 all the functions in the list are called, with the frame as argument. */);
22310 Vwindow_size_change_functions = Qnil;
22311
22312 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
22313 doc: /* List of functions to call before redisplaying a window with scrolling.
22314 Each function is called with two arguments, the window
22315 and its new display-start position. Note that the value of `window-end'
22316 is not valid when these functions are called. */);
22317 Vwindow_scroll_functions = Qnil;
22318
22319 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
22320 doc: /* *Non-nil means autoselect window with mouse pointer. */);
22321 mouse_autoselect_window = 0;
22322
22323 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
22324 doc: /* *Non-nil means automatically resize tool-bars.
22325 This increases a tool-bar's height if not all tool-bar items are visible.
22326 It decreases a tool-bar's height when it would display blank lines
22327 otherwise. */);
22328 auto_resize_tool_bars_p = 1;
22329
22330 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
22331 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
22332 auto_raise_tool_bar_buttons_p = 1;
22333
22334 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
22335 doc: /* *Margin around tool-bar buttons in pixels.
22336 If an integer, use that for both horizontal and vertical margins.
22337 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
22338 HORZ specifying the horizontal margin, and VERT specifying the
22339 vertical margin. */);
22340 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
22341
22342 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
22343 doc: /* *Relief thickness of tool-bar buttons. */);
22344 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
22345
22346 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
22347 doc: /* List of functions to call to fontify regions of text.
22348 Each function is called with one argument POS. Functions must
22349 fontify a region starting at POS in the current buffer, and give
22350 fontified regions the property `fontified'. */);
22351 Vfontification_functions = Qnil;
22352 Fmake_variable_buffer_local (Qfontification_functions);
22353
22354 DEFVAR_BOOL ("unibyte-display-via-language-environment",
22355 &unibyte_display_via_language_environment,
22356 doc: /* *Non-nil means display unibyte text according to language environment.
22357 Specifically this means that unibyte non-ASCII characters
22358 are displayed by converting them to the equivalent multibyte characters
22359 according to the current language environment. As a result, they are
22360 displayed according to the current fontset. */);
22361 unibyte_display_via_language_environment = 0;
22362
22363 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
22364 doc: /* *Maximum height for resizing mini-windows.
22365 If a float, it specifies a fraction of the mini-window frame's height.
22366 If an integer, it specifies a number of lines. */);
22367 Vmax_mini_window_height = make_float (0.25);
22368
22369 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
22370 doc: /* *How to resize mini-windows.
22371 A value of nil means don't automatically resize mini-windows.
22372 A value of t means resize them to fit the text displayed in them.
22373 A value of `grow-only', the default, means let mini-windows grow
22374 only, until their display becomes empty, at which point the windows
22375 go back to their normal size. */);
22376 Vresize_mini_windows = Qgrow_only;
22377
22378 DEFVAR_LISP ("cursor-in-non-selected-windows",
22379 &Vcursor_in_non_selected_windows,
22380 doc: /* *Cursor type to display in non-selected windows.
22381 t means to use hollow box cursor. See `cursor-type' for other values. */);
22382 Vcursor_in_non_selected_windows = Qt;
22383
22384 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
22385 doc: /* Alist specifying how to blink the cursor off.
22386 Each element has the form (ON-STATE . OFF-STATE). Whenever the
22387 `cursor-type' frame-parameter or variable equals ON-STATE,
22388 comparing using `equal', Emacs uses OFF-STATE to specify
22389 how to blink it off. */);
22390 Vblink_cursor_alist = Qnil;
22391
22392 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
22393 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
22394 automatic_hscrolling_p = 1;
22395
22396 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
22397 doc: /* *How many columns away from the window edge point is allowed to get
22398 before automatic hscrolling will horizontally scroll the window. */);
22399 hscroll_margin = 5;
22400
22401 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
22402 doc: /* *How many columns to scroll the window when point gets too close to the edge.
22403 When point is less than `automatic-hscroll-margin' columns from the window
22404 edge, automatic hscrolling will scroll the window by the amount of columns
22405 determined by this variable. If its value is a positive integer, scroll that
22406 many columns. If it's a positive floating-point number, it specifies the
22407 fraction of the window's width to scroll. If it's nil or zero, point will be
22408 centered horizontally after the scroll. Any other value, including negative
22409 numbers, are treated as if the value were zero.
22410
22411 Automatic hscrolling always moves point outside the scroll margin, so if
22412 point was more than scroll step columns inside the margin, the window will
22413 scroll more than the value given by the scroll step.
22414
22415 Note that the lower bound for automatic hscrolling specified by `scroll-left'
22416 and `scroll-right' overrides this variable's effect. */);
22417 Vhscroll_step = make_number (0);
22418
22419 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
22420 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
22421 Bind this around calls to `message' to let it take effect. */);
22422 message_truncate_lines = 0;
22423
22424 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
22425 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
22426 Can be used to update submenus whose contents should vary. */);
22427 Vmenu_bar_update_hook = Qnil;
22428
22429 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
22430 doc: /* Non-nil means don't update menu bars. Internal use only. */);
22431 inhibit_menubar_update = 0;
22432
22433 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
22434 doc: /* Non-nil means don't eval Lisp during redisplay. */);
22435 inhibit_eval_during_redisplay = 0;
22436
22437 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
22438 doc: /* Non-nil means don't free realized faces. Internal use only. */);
22439 inhibit_free_realized_faces = 0;
22440
22441 #if GLYPH_DEBUG
22442 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
22443 doc: /* Inhibit try_window_id display optimization. */);
22444 inhibit_try_window_id = 0;
22445
22446 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
22447 doc: /* Inhibit try_window_reusing display optimization. */);
22448 inhibit_try_window_reusing = 0;
22449
22450 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
22451 doc: /* Inhibit try_cursor_movement display optimization. */);
22452 inhibit_try_cursor_movement = 0;
22453 #endif /* GLYPH_DEBUG */
22454 }
22455
22456
22457 /* Initialize this module when Emacs starts. */
22458
22459 void
22460 init_xdisp ()
22461 {
22462 Lisp_Object root_window;
22463 struct window *mini_w;
22464
22465 current_header_line_height = current_mode_line_height = -1;
22466
22467 CHARPOS (this_line_start_pos) = 0;
22468
22469 mini_w = XWINDOW (minibuf_window);
22470 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
22471
22472 if (!noninteractive)
22473 {
22474 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
22475 int i;
22476
22477 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
22478 set_window_height (root_window,
22479 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
22480 0);
22481 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
22482 set_window_height (minibuf_window, 1, 0);
22483
22484 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
22485 mini_w->total_cols = make_number (FRAME_COLS (f));
22486
22487 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
22488 scratch_glyph_row.glyphs[TEXT_AREA + 1]
22489 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
22490
22491 /* The default ellipsis glyphs `...'. */
22492 for (i = 0; i < 3; ++i)
22493 default_invis_vector[i] = make_number ('.');
22494 }
22495
22496 {
22497 /* Allocate the buffer for frame titles.
22498 Also used for `format-mode-line'. */
22499 int size = 100;
22500 frame_title_buf = (char *) xmalloc (size);
22501 frame_title_buf_end = frame_title_buf + size;
22502 frame_title_ptr = NULL;
22503 }
22504
22505 help_echo_showing_p = 0;
22506 }
22507
22508
22509 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
22510 (do not change this comment) */