Merged in changes from CVS trunk.
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985,86,87,88,93,94,95,97,98,99,2000,01,02,03,04
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the description of direct update
37 operations, below.)
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay do? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
91 You will find a lot of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking an iterator structure (struct it)
124 argument.
125
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
131
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
138
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
146
147
148 Frame matrices.
149
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
156
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
168
169 #include <config.h>
170 #include <stdio.h>
171
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "keymap.h"
183 #include "macros.h"
184 #include "disptab.h"
185 #include "termhooks.h"
186 #include "intervals.h"
187 #include "coding.h"
188 #include "process.h"
189 #include "region-cache.h"
190 #include "fontset.h"
191 #include "blockinput.h"
192
193 #ifdef HAVE_X_WINDOWS
194 #include "xterm.h"
195 #endif
196 #ifdef WINDOWSNT
197 #include "w32term.h"
198 #endif
199 #ifdef MAC_OS
200 #include "macterm.h"
201 #endif
202
203 #ifndef FRAME_X_OUTPUT
204 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
205 #endif
206
207 #define INFINITY 10000000
208
209 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
210 || defined (USE_GTK)
211 extern void set_frame_menubar P_ ((struct frame *f, int, int));
212 extern int pending_menu_activation;
213 #endif
214
215 extern int interrupt_input;
216 extern int command_loop_level;
217
218 extern int minibuffer_auto_raise;
219 extern Lisp_Object Vminibuffer_list;
220
221 extern Lisp_Object Qface;
222 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
223
224 extern Lisp_Object Voverriding_local_map;
225 extern Lisp_Object Voverriding_local_map_menu_flag;
226 extern Lisp_Object Qmenu_item;
227 extern Lisp_Object Qwhen;
228 extern Lisp_Object Qhelp_echo;
229
230 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
231 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
232 Lisp_Object Qredisplay_end_trigger_functions;
233 Lisp_Object Qinhibit_point_motion_hooks;
234 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
235 Lisp_Object Qfontified;
236 Lisp_Object Qgrow_only;
237 Lisp_Object Qinhibit_eval_during_redisplay;
238 Lisp_Object Qbuffer_position, Qposition, Qobject;
239
240 /* Cursor shapes */
241 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
242
243 /* Pointer shapes */
244 Lisp_Object Qarrow, Qhand, Qtext;
245
246 Lisp_Object Qrisky_local_variable;
247
248 /* Holds the list (error). */
249 Lisp_Object list_of_error;
250
251 /* Functions called to fontify regions of text. */
252
253 Lisp_Object Vfontification_functions;
254 Lisp_Object Qfontification_functions;
255
256 /* Non-zero means automatically select any window when the mouse
257 cursor moves into it. */
258 int mouse_autoselect_window;
259
260 /* Non-zero means draw tool bar buttons raised when the mouse moves
261 over them. */
262
263 int auto_raise_tool_bar_buttons_p;
264
265 /* Margin around tool bar buttons in pixels. */
266
267 Lisp_Object Vtool_bar_button_margin;
268
269 /* Thickness of shadow to draw around tool bar buttons. */
270
271 EMACS_INT tool_bar_button_relief;
272
273 /* Non-zero means automatically resize tool-bars so that all tool-bar
274 items are visible, and no blank lines remain. */
275
276 int auto_resize_tool_bars_p;
277
278 /* Non-zero means draw block and hollow cursor as wide as the glyph
279 under it. For example, if a block cursor is over a tab, it will be
280 drawn as wide as that tab on the display. */
281
282 int x_stretch_cursor_p;
283
284 /* Non-nil means don't actually do any redisplay. */
285
286 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
287
288 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
289
290 int inhibit_eval_during_redisplay;
291
292 /* Names of text properties relevant for redisplay. */
293
294 Lisp_Object Qdisplay;
295 extern Lisp_Object Qface, Qinvisible, Qwidth;
296
297 /* Symbols used in text property values. */
298
299 Lisp_Object Vdisplay_pixels_per_inch;
300 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
301 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
302 Lisp_Object Qslice;
303 Lisp_Object Qcenter;
304 Lisp_Object Qmargin, Qpointer;
305 Lisp_Object Qline_height, Qtotal;
306 extern Lisp_Object Qheight;
307 extern Lisp_Object QCwidth, QCheight, QCascent;
308 extern Lisp_Object Qscroll_bar;
309
310 /* Non-nil means highlight trailing whitespace. */
311
312 Lisp_Object Vshow_trailing_whitespace;
313
314 #ifdef HAVE_WINDOW_SYSTEM
315 extern Lisp_Object Voverflow_newline_into_fringe;
316
317 /* Test if overflow newline into fringe. Called with iterator IT
318 at or past right window margin, and with IT->current_x set. */
319
320 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
321 (!NILP (Voverflow_newline_into_fringe) \
322 && FRAME_WINDOW_P (it->f) \
323 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
324 && it->current_x == it->last_visible_x)
325
326 #endif /* HAVE_WINDOW_SYSTEM */
327
328 /* Non-nil means show the text cursor in void text areas
329 i.e. in blank areas after eol and eob. This used to be
330 the default in 21.3. */
331
332 Lisp_Object Vvoid_text_area_pointer;
333
334 /* Name of the face used to highlight trailing whitespace. */
335
336 Lisp_Object Qtrailing_whitespace;
337
338 /* The symbol `image' which is the car of the lists used to represent
339 images in Lisp. */
340
341 Lisp_Object Qimage;
342
343 /* The image map types. */
344 Lisp_Object QCmap, QCpointer;
345 Lisp_Object Qrect, Qcircle, Qpoly;
346
347 /* Non-zero means print newline to stdout before next mini-buffer
348 message. */
349
350 int noninteractive_need_newline;
351
352 /* Non-zero means print newline to message log before next message. */
353
354 static int message_log_need_newline;
355
356 /* Three markers that message_dolog uses.
357 It could allocate them itself, but that causes trouble
358 in handling memory-full errors. */
359 static Lisp_Object message_dolog_marker1;
360 static Lisp_Object message_dolog_marker2;
361 static Lisp_Object message_dolog_marker3;
362 \f
363 /* The buffer position of the first character appearing entirely or
364 partially on the line of the selected window which contains the
365 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
366 redisplay optimization in redisplay_internal. */
367
368 static struct text_pos this_line_start_pos;
369
370 /* Number of characters past the end of the line above, including the
371 terminating newline. */
372
373 static struct text_pos this_line_end_pos;
374
375 /* The vertical positions and the height of this line. */
376
377 static int this_line_vpos;
378 static int this_line_y;
379 static int this_line_pixel_height;
380
381 /* X position at which this display line starts. Usually zero;
382 negative if first character is partially visible. */
383
384 static int this_line_start_x;
385
386 /* Buffer that this_line_.* variables are referring to. */
387
388 static struct buffer *this_line_buffer;
389
390 /* Nonzero means truncate lines in all windows less wide than the
391 frame. */
392
393 int truncate_partial_width_windows;
394
395 /* A flag to control how to display unibyte 8-bit character. */
396
397 int unibyte_display_via_language_environment;
398
399 /* Nonzero means we have more than one non-mini-buffer-only frame.
400 Not guaranteed to be accurate except while parsing
401 frame-title-format. */
402
403 int multiple_frames;
404
405 Lisp_Object Vglobal_mode_string;
406
407
408 /* List of variables (symbols) which hold markers for overlay arrows.
409 The symbols on this list are examined during redisplay to determine
410 where to display overlay arrows. */
411
412 Lisp_Object Voverlay_arrow_variable_list;
413
414 /* Marker for where to display an arrow on top of the buffer text. */
415
416 Lisp_Object Voverlay_arrow_position;
417
418 /* String to display for the arrow. Only used on terminal frames. */
419
420 Lisp_Object Voverlay_arrow_string;
421
422 /* Values of those variables at last redisplay are stored as
423 properties on `overlay-arrow-position' symbol. However, if
424 Voverlay_arrow_position is a marker, last-arrow-position is its
425 numerical position. */
426
427 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
428
429 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
430 properties on a symbol in overlay-arrow-variable-list. */
431
432 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
433
434 /* Like mode-line-format, but for the title bar on a visible frame. */
435
436 Lisp_Object Vframe_title_format;
437
438 /* Like mode-line-format, but for the title bar on an iconified frame. */
439
440 Lisp_Object Vicon_title_format;
441
442 /* List of functions to call when a window's size changes. These
443 functions get one arg, a frame on which one or more windows' sizes
444 have changed. */
445
446 static Lisp_Object Vwindow_size_change_functions;
447
448 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
449
450 /* Nonzero if overlay arrow has been displayed once in this window. */
451
452 static int overlay_arrow_seen;
453
454 /* Nonzero means highlight the region even in nonselected windows. */
455
456 int highlight_nonselected_windows;
457
458 /* If cursor motion alone moves point off frame, try scrolling this
459 many lines up or down if that will bring it back. */
460
461 static EMACS_INT scroll_step;
462
463 /* Nonzero means scroll just far enough to bring point back on the
464 screen, when appropriate. */
465
466 static EMACS_INT scroll_conservatively;
467
468 /* Recenter the window whenever point gets within this many lines of
469 the top or bottom of the window. This value is translated into a
470 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
471 that there is really a fixed pixel height scroll margin. */
472
473 EMACS_INT scroll_margin;
474
475 /* Number of windows showing the buffer of the selected window (or
476 another buffer with the same base buffer). keyboard.c refers to
477 this. */
478
479 int buffer_shared;
480
481 /* Vector containing glyphs for an ellipsis `...'. */
482
483 static Lisp_Object default_invis_vector[3];
484
485 /* Zero means display the mode-line/header-line/menu-bar in the default face
486 (this slightly odd definition is for compatibility with previous versions
487 of emacs), non-zero means display them using their respective faces.
488
489 This variable is deprecated. */
490
491 int mode_line_inverse_video;
492
493 /* Prompt to display in front of the mini-buffer contents. */
494
495 Lisp_Object minibuf_prompt;
496
497 /* Width of current mini-buffer prompt. Only set after display_line
498 of the line that contains the prompt. */
499
500 int minibuf_prompt_width;
501
502 /* This is the window where the echo area message was displayed. It
503 is always a mini-buffer window, but it may not be the same window
504 currently active as a mini-buffer. */
505
506 Lisp_Object echo_area_window;
507
508 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
509 pushes the current message and the value of
510 message_enable_multibyte on the stack, the function restore_message
511 pops the stack and displays MESSAGE again. */
512
513 Lisp_Object Vmessage_stack;
514
515 /* Nonzero means multibyte characters were enabled when the echo area
516 message was specified. */
517
518 int message_enable_multibyte;
519
520 /* Nonzero if we should redraw the mode lines on the next redisplay. */
521
522 int update_mode_lines;
523
524 /* Nonzero if window sizes or contents have changed since last
525 redisplay that finished. */
526
527 int windows_or_buffers_changed;
528
529 /* Nonzero means a frame's cursor type has been changed. */
530
531 int cursor_type_changed;
532
533 /* Nonzero after display_mode_line if %l was used and it displayed a
534 line number. */
535
536 int line_number_displayed;
537
538 /* Maximum buffer size for which to display line numbers. */
539
540 Lisp_Object Vline_number_display_limit;
541
542 /* Line width to consider when repositioning for line number display. */
543
544 static EMACS_INT line_number_display_limit_width;
545
546 /* Number of lines to keep in the message log buffer. t means
547 infinite. nil means don't log at all. */
548
549 Lisp_Object Vmessage_log_max;
550
551 /* The name of the *Messages* buffer, a string. */
552
553 static Lisp_Object Vmessages_buffer_name;
554
555 /* Current, index 0, and last displayed echo area message. Either
556 buffers from echo_buffers, or nil to indicate no message. */
557
558 Lisp_Object echo_area_buffer[2];
559
560 /* The buffers referenced from echo_area_buffer. */
561
562 static Lisp_Object echo_buffer[2];
563
564 /* A vector saved used in with_area_buffer to reduce consing. */
565
566 static Lisp_Object Vwith_echo_area_save_vector;
567
568 /* Non-zero means display_echo_area should display the last echo area
569 message again. Set by redisplay_preserve_echo_area. */
570
571 static int display_last_displayed_message_p;
572
573 /* Nonzero if echo area is being used by print; zero if being used by
574 message. */
575
576 int message_buf_print;
577
578 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
579
580 Lisp_Object Qinhibit_menubar_update;
581 int inhibit_menubar_update;
582
583 /* Maximum height for resizing mini-windows. Either a float
584 specifying a fraction of the available height, or an integer
585 specifying a number of lines. */
586
587 Lisp_Object Vmax_mini_window_height;
588
589 /* Non-zero means messages should be displayed with truncated
590 lines instead of being continued. */
591
592 int message_truncate_lines;
593 Lisp_Object Qmessage_truncate_lines;
594
595 /* Set to 1 in clear_message to make redisplay_internal aware
596 of an emptied echo area. */
597
598 static int message_cleared_p;
599
600 /* Non-zero means we want a hollow cursor in windows that are not
601 selected. Zero means there's no cursor in such windows. */
602
603 Lisp_Object Vcursor_in_non_selected_windows;
604 Lisp_Object Qcursor_in_non_selected_windows;
605
606 /* How to blink the default frame cursor off. */
607 Lisp_Object Vblink_cursor_alist;
608
609 /* A scratch glyph row with contents used for generating truncation
610 glyphs. Also used in direct_output_for_insert. */
611
612 #define MAX_SCRATCH_GLYPHS 100
613 struct glyph_row scratch_glyph_row;
614 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
615
616 /* Ascent and height of the last line processed by move_it_to. */
617
618 static int last_max_ascent, last_height;
619
620 /* Non-zero if there's a help-echo in the echo area. */
621
622 int help_echo_showing_p;
623
624 /* If >= 0, computed, exact values of mode-line and header-line height
625 to use in the macros CURRENT_MODE_LINE_HEIGHT and
626 CURRENT_HEADER_LINE_HEIGHT. */
627
628 int current_mode_line_height, current_header_line_height;
629
630 /* The maximum distance to look ahead for text properties. Values
631 that are too small let us call compute_char_face and similar
632 functions too often which is expensive. Values that are too large
633 let us call compute_char_face and alike too often because we
634 might not be interested in text properties that far away. */
635
636 #define TEXT_PROP_DISTANCE_LIMIT 100
637
638 #if GLYPH_DEBUG
639
640 /* Variables to turn off display optimizations from Lisp. */
641
642 int inhibit_try_window_id, inhibit_try_window_reusing;
643 int inhibit_try_cursor_movement;
644
645 /* Non-zero means print traces of redisplay if compiled with
646 GLYPH_DEBUG != 0. */
647
648 int trace_redisplay_p;
649
650 #endif /* GLYPH_DEBUG */
651
652 #ifdef DEBUG_TRACE_MOVE
653 /* Non-zero means trace with TRACE_MOVE to stderr. */
654 int trace_move;
655
656 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
657 #else
658 #define TRACE_MOVE(x) (void) 0
659 #endif
660
661 /* Non-zero means automatically scroll windows horizontally to make
662 point visible. */
663
664 int automatic_hscrolling_p;
665
666 /* How close to the margin can point get before the window is scrolled
667 horizontally. */
668 EMACS_INT hscroll_margin;
669
670 /* How much to scroll horizontally when point is inside the above margin. */
671 Lisp_Object Vhscroll_step;
672
673 /* A list of symbols, one for each supported image type. */
674
675 Lisp_Object Vimage_types;
676
677 /* The variable `resize-mini-windows'. If nil, don't resize
678 mini-windows. If t, always resize them to fit the text they
679 display. If `grow-only', let mini-windows grow only until they
680 become empty. */
681
682 Lisp_Object Vresize_mini_windows;
683
684 /* Buffer being redisplayed -- for redisplay_window_error. */
685
686 struct buffer *displayed_buffer;
687
688 /* Value returned from text property handlers (see below). */
689
690 enum prop_handled
691 {
692 HANDLED_NORMALLY,
693 HANDLED_RECOMPUTE_PROPS,
694 HANDLED_OVERLAY_STRING_CONSUMED,
695 HANDLED_RETURN
696 };
697
698 /* A description of text properties that redisplay is interested
699 in. */
700
701 struct props
702 {
703 /* The name of the property. */
704 Lisp_Object *name;
705
706 /* A unique index for the property. */
707 enum prop_idx idx;
708
709 /* A handler function called to set up iterator IT from the property
710 at IT's current position. Value is used to steer handle_stop. */
711 enum prop_handled (*handler) P_ ((struct it *it));
712 };
713
714 static enum prop_handled handle_face_prop P_ ((struct it *));
715 static enum prop_handled handle_invisible_prop P_ ((struct it *));
716 static enum prop_handled handle_display_prop P_ ((struct it *));
717 static enum prop_handled handle_composition_prop P_ ((struct it *));
718 static enum prop_handled handle_overlay_change P_ ((struct it *));
719 static enum prop_handled handle_fontified_prop P_ ((struct it *));
720
721 /* Properties handled by iterators. */
722
723 static struct props it_props[] =
724 {
725 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
726 /* Handle `face' before `display' because some sub-properties of
727 `display' need to know the face. */
728 {&Qface, FACE_PROP_IDX, handle_face_prop},
729 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
730 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
731 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
732 {NULL, 0, NULL}
733 };
734
735 /* Value is the position described by X. If X is a marker, value is
736 the marker_position of X. Otherwise, value is X. */
737
738 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
739
740 /* Enumeration returned by some move_it_.* functions internally. */
741
742 enum move_it_result
743 {
744 /* Not used. Undefined value. */
745 MOVE_UNDEFINED,
746
747 /* Move ended at the requested buffer position or ZV. */
748 MOVE_POS_MATCH_OR_ZV,
749
750 /* Move ended at the requested X pixel position. */
751 MOVE_X_REACHED,
752
753 /* Move within a line ended at the end of a line that must be
754 continued. */
755 MOVE_LINE_CONTINUED,
756
757 /* Move within a line ended at the end of a line that would
758 be displayed truncated. */
759 MOVE_LINE_TRUNCATED,
760
761 /* Move within a line ended at a line end. */
762 MOVE_NEWLINE_OR_CR
763 };
764
765 /* This counter is used to clear the face cache every once in a while
766 in redisplay_internal. It is incremented for each redisplay.
767 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
768 cleared. */
769
770 #define CLEAR_FACE_CACHE_COUNT 500
771 static int clear_face_cache_count;
772
773 /* Non-zero while redisplay_internal is in progress. */
774
775 int redisplaying_p;
776
777 /* Non-zero means don't free realized faces. Bound while freeing
778 realized faces is dangerous because glyph matrices might still
779 reference them. */
780
781 int inhibit_free_realized_faces;
782 Lisp_Object Qinhibit_free_realized_faces;
783
784 /* If a string, XTread_socket generates an event to display that string.
785 (The display is done in read_char.) */
786
787 Lisp_Object help_echo_string;
788 Lisp_Object help_echo_window;
789 Lisp_Object help_echo_object;
790 int help_echo_pos;
791
792 /* Temporary variable for XTread_socket. */
793
794 Lisp_Object previous_help_echo_string;
795
796 /* Null glyph slice */
797
798 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
799
800 \f
801 /* Function prototypes. */
802
803 static void setup_for_ellipsis P_ ((struct it *));
804 static void mark_window_display_accurate_1 P_ ((struct window *, int));
805 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
806 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
807 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
808 static int redisplay_mode_lines P_ ((Lisp_Object, int));
809 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
810
811 #if 0
812 static int invisible_text_between_p P_ ((struct it *, int, int));
813 #endif
814
815 static int next_element_from_ellipsis P_ ((struct it *));
816 static void pint2str P_ ((char *, int, int));
817 static void pint2hrstr P_ ((char *, int, int));
818 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
819 struct text_pos));
820 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
821 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
822 static void store_frame_title_char P_ ((char));
823 static int store_frame_title P_ ((const unsigned char *, int, int));
824 static void x_consider_frame_title P_ ((Lisp_Object));
825 static void handle_stop P_ ((struct it *));
826 static int tool_bar_lines_needed P_ ((struct frame *));
827 static int single_display_prop_intangible_p P_ ((Lisp_Object));
828 static void ensure_echo_area_buffers P_ ((void));
829 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
830 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
831 static int with_echo_area_buffer P_ ((struct window *, int,
832 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
833 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
834 static void clear_garbaged_frames P_ ((void));
835 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
836 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
837 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
838 static int display_echo_area P_ ((struct window *));
839 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
840 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
841 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
842 static int string_char_and_length P_ ((const unsigned char *, int, int *));
843 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
844 struct text_pos));
845 static int compute_window_start_on_continuation_line P_ ((struct window *));
846 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
847 static void insert_left_trunc_glyphs P_ ((struct it *));
848 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
849 Lisp_Object));
850 static void extend_face_to_end_of_line P_ ((struct it *));
851 static int append_space_for_newline P_ ((struct it *, int));
852 static int make_cursor_line_fully_visible P_ ((struct window *, int));
853 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
854 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
855 static int trailing_whitespace_p P_ ((int));
856 static int message_log_check_duplicate P_ ((int, int, int, int));
857 static void push_it P_ ((struct it *));
858 static void pop_it P_ ((struct it *));
859 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
860 static void select_frame_for_redisplay P_ ((Lisp_Object));
861 static void redisplay_internal P_ ((int));
862 static int echo_area_display P_ ((int));
863 static void redisplay_windows P_ ((Lisp_Object));
864 static void redisplay_window P_ ((Lisp_Object, int));
865 static Lisp_Object redisplay_window_error ();
866 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
867 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
868 static void update_menu_bar P_ ((struct frame *, int));
869 static int try_window_reusing_current_matrix P_ ((struct window *));
870 static int try_window_id P_ ((struct window *));
871 static int display_line P_ ((struct it *));
872 static int display_mode_lines P_ ((struct window *));
873 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
874 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
875 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
876 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
877 static void display_menu_bar P_ ((struct window *));
878 static int display_count_lines P_ ((int, int, int, int, int *));
879 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
880 int, int, struct it *, int, int, int, int));
881 static void compute_line_metrics P_ ((struct it *));
882 static void run_redisplay_end_trigger_hook P_ ((struct it *));
883 static int get_overlay_strings P_ ((struct it *, int));
884 static void next_overlay_string P_ ((struct it *));
885 static void reseat P_ ((struct it *, struct text_pos, int));
886 static void reseat_1 P_ ((struct it *, struct text_pos, int));
887 static void back_to_previous_visible_line_start P_ ((struct it *));
888 static void reseat_at_previous_visible_line_start P_ ((struct it *));
889 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
890 static int next_element_from_display_vector P_ ((struct it *));
891 static int next_element_from_string P_ ((struct it *));
892 static int next_element_from_c_string P_ ((struct it *));
893 static int next_element_from_buffer P_ ((struct it *));
894 static int next_element_from_composition P_ ((struct it *));
895 static int next_element_from_image P_ ((struct it *));
896 static int next_element_from_stretch P_ ((struct it *));
897 static void load_overlay_strings P_ ((struct it *, int));
898 static int init_from_display_pos P_ ((struct it *, struct window *,
899 struct display_pos *));
900 static void reseat_to_string P_ ((struct it *, unsigned char *,
901 Lisp_Object, int, int, int, int));
902 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
903 int, int, int));
904 void move_it_vertically_backward P_ ((struct it *, int));
905 static void init_to_row_start P_ ((struct it *, struct window *,
906 struct glyph_row *));
907 static int init_to_row_end P_ ((struct it *, struct window *,
908 struct glyph_row *));
909 static void back_to_previous_line_start P_ ((struct it *));
910 static int forward_to_next_line_start P_ ((struct it *, int *));
911 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
912 Lisp_Object, int));
913 static struct text_pos string_pos P_ ((int, Lisp_Object));
914 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
915 static int number_of_chars P_ ((unsigned char *, int));
916 static void compute_stop_pos P_ ((struct it *));
917 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
918 Lisp_Object));
919 static int face_before_or_after_it_pos P_ ((struct it *, int));
920 static int next_overlay_change P_ ((int));
921 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
922 Lisp_Object, struct text_pos *,
923 int));
924 static int underlying_face_id P_ ((struct it *));
925 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
926 struct window *));
927
928 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
929 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
930
931 #ifdef HAVE_WINDOW_SYSTEM
932
933 static void update_tool_bar P_ ((struct frame *, int));
934 static void build_desired_tool_bar_string P_ ((struct frame *f));
935 static int redisplay_tool_bar P_ ((struct frame *));
936 static void display_tool_bar_line P_ ((struct it *));
937 static void notice_overwritten_cursor P_ ((struct window *,
938 enum glyph_row_area,
939 int, int, int, int));
940
941
942
943 #endif /* HAVE_WINDOW_SYSTEM */
944
945 \f
946 /***********************************************************************
947 Window display dimensions
948 ***********************************************************************/
949
950 /* Return the bottom boundary y-position for text lines in window W.
951 This is the first y position at which a line cannot start.
952 It is relative to the top of the window.
953
954 This is the height of W minus the height of a mode line, if any. */
955
956 INLINE int
957 window_text_bottom_y (w)
958 struct window *w;
959 {
960 int height = WINDOW_TOTAL_HEIGHT (w);
961
962 if (WINDOW_WANTS_MODELINE_P (w))
963 height -= CURRENT_MODE_LINE_HEIGHT (w);
964 return height;
965 }
966
967 /* Return the pixel width of display area AREA of window W. AREA < 0
968 means return the total width of W, not including fringes to
969 the left and right of the window. */
970
971 INLINE int
972 window_box_width (w, area)
973 struct window *w;
974 int area;
975 {
976 int cols = XFASTINT (w->total_cols);
977 int pixels = 0;
978
979 if (!w->pseudo_window_p)
980 {
981 cols -= WINDOW_SCROLL_BAR_COLS (w);
982
983 if (area == TEXT_AREA)
984 {
985 if (INTEGERP (w->left_margin_cols))
986 cols -= XFASTINT (w->left_margin_cols);
987 if (INTEGERP (w->right_margin_cols))
988 cols -= XFASTINT (w->right_margin_cols);
989 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
990 }
991 else if (area == LEFT_MARGIN_AREA)
992 {
993 cols = (INTEGERP (w->left_margin_cols)
994 ? XFASTINT (w->left_margin_cols) : 0);
995 pixels = 0;
996 }
997 else if (area == RIGHT_MARGIN_AREA)
998 {
999 cols = (INTEGERP (w->right_margin_cols)
1000 ? XFASTINT (w->right_margin_cols) : 0);
1001 pixels = 0;
1002 }
1003 }
1004
1005 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1006 }
1007
1008
1009 /* Return the pixel height of the display area of window W, not
1010 including mode lines of W, if any. */
1011
1012 INLINE int
1013 window_box_height (w)
1014 struct window *w;
1015 {
1016 struct frame *f = XFRAME (w->frame);
1017 int height = WINDOW_TOTAL_HEIGHT (w);
1018
1019 xassert (height >= 0);
1020
1021 /* Note: the code below that determines the mode-line/header-line
1022 height is essentially the same as that contained in the macro
1023 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1024 the appropriate glyph row has its `mode_line_p' flag set,
1025 and if it doesn't, uses estimate_mode_line_height instead. */
1026
1027 if (WINDOW_WANTS_MODELINE_P (w))
1028 {
1029 struct glyph_row *ml_row
1030 = (w->current_matrix && w->current_matrix->rows
1031 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1032 : 0);
1033 if (ml_row && ml_row->mode_line_p)
1034 height -= ml_row->height;
1035 else
1036 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1037 }
1038
1039 if (WINDOW_WANTS_HEADER_LINE_P (w))
1040 {
1041 struct glyph_row *hl_row
1042 = (w->current_matrix && w->current_matrix->rows
1043 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1044 : 0);
1045 if (hl_row && hl_row->mode_line_p)
1046 height -= hl_row->height;
1047 else
1048 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1049 }
1050
1051 /* With a very small font and a mode-line that's taller than
1052 default, we might end up with a negative height. */
1053 return max (0, height);
1054 }
1055
1056 /* Return the window-relative coordinate of the left edge of display
1057 area AREA of window W. AREA < 0 means return the left edge of the
1058 whole window, to the right of the left fringe of W. */
1059
1060 INLINE int
1061 window_box_left_offset (w, area)
1062 struct window *w;
1063 int area;
1064 {
1065 int x;
1066
1067 if (w->pseudo_window_p)
1068 return 0;
1069
1070 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1071
1072 if (area == TEXT_AREA)
1073 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1074 + window_box_width (w, LEFT_MARGIN_AREA));
1075 else if (area == RIGHT_MARGIN_AREA)
1076 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1077 + window_box_width (w, LEFT_MARGIN_AREA)
1078 + window_box_width (w, TEXT_AREA)
1079 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1080 ? 0
1081 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1082 else if (area == LEFT_MARGIN_AREA
1083 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1084 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1085
1086 return x;
1087 }
1088
1089
1090 /* Return the window-relative coordinate of the right edge of display
1091 area AREA of window W. AREA < 0 means return the left edge of the
1092 whole window, to the left of the right fringe of W. */
1093
1094 INLINE int
1095 window_box_right_offset (w, area)
1096 struct window *w;
1097 int area;
1098 {
1099 return window_box_left_offset (w, area) + window_box_width (w, area);
1100 }
1101
1102 /* Return the frame-relative coordinate of the left edge of display
1103 area AREA of window W. AREA < 0 means return the left edge of the
1104 whole window, to the right of the left fringe of W. */
1105
1106 INLINE int
1107 window_box_left (w, area)
1108 struct window *w;
1109 int area;
1110 {
1111 struct frame *f = XFRAME (w->frame);
1112 int x;
1113
1114 if (w->pseudo_window_p)
1115 return FRAME_INTERNAL_BORDER_WIDTH (f);
1116
1117 x = (WINDOW_LEFT_EDGE_X (w)
1118 + window_box_left_offset (w, area));
1119
1120 return x;
1121 }
1122
1123
1124 /* Return the frame-relative coordinate of the right edge of display
1125 area AREA of window W. AREA < 0 means return the left edge of the
1126 whole window, to the left of the right fringe of W. */
1127
1128 INLINE int
1129 window_box_right (w, area)
1130 struct window *w;
1131 int area;
1132 {
1133 return window_box_left (w, area) + window_box_width (w, area);
1134 }
1135
1136 /* Get the bounding box of the display area AREA of window W, without
1137 mode lines, in frame-relative coordinates. AREA < 0 means the
1138 whole window, not including the left and right fringes of
1139 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1140 coordinates of the upper-left corner of the box. Return in
1141 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1142
1143 INLINE void
1144 window_box (w, area, box_x, box_y, box_width, box_height)
1145 struct window *w;
1146 int area;
1147 int *box_x, *box_y, *box_width, *box_height;
1148 {
1149 if (box_width)
1150 *box_width = window_box_width (w, area);
1151 if (box_height)
1152 *box_height = window_box_height (w);
1153 if (box_x)
1154 *box_x = window_box_left (w, area);
1155 if (box_y)
1156 {
1157 *box_y = WINDOW_TOP_EDGE_Y (w);
1158 if (WINDOW_WANTS_HEADER_LINE_P (w))
1159 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1160 }
1161 }
1162
1163
1164 /* Get the bounding box of the display area AREA of window W, without
1165 mode lines. AREA < 0 means the whole window, not including the
1166 left and right fringe of the window. Return in *TOP_LEFT_X
1167 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1168 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1169 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1170 box. */
1171
1172 INLINE void
1173 window_box_edges (w, area, top_left_x, top_left_y,
1174 bottom_right_x, bottom_right_y)
1175 struct window *w;
1176 int area;
1177 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1178 {
1179 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1180 bottom_right_y);
1181 *bottom_right_x += *top_left_x;
1182 *bottom_right_y += *top_left_y;
1183 }
1184
1185
1186 \f
1187 /***********************************************************************
1188 Utilities
1189 ***********************************************************************/
1190
1191 /* Return the bottom y-position of the line the iterator IT is in.
1192 This can modify IT's settings. */
1193
1194 int
1195 line_bottom_y (it)
1196 struct it *it;
1197 {
1198 int line_height = it->max_ascent + it->max_descent;
1199 int line_top_y = it->current_y;
1200
1201 if (line_height == 0)
1202 {
1203 if (last_height)
1204 line_height = last_height;
1205 else if (IT_CHARPOS (*it) < ZV)
1206 {
1207 move_it_by_lines (it, 1, 1);
1208 line_height = (it->max_ascent || it->max_descent
1209 ? it->max_ascent + it->max_descent
1210 : last_height);
1211 }
1212 else
1213 {
1214 struct glyph_row *row = it->glyph_row;
1215
1216 /* Use the default character height. */
1217 it->glyph_row = NULL;
1218 it->what = IT_CHARACTER;
1219 it->c = ' ';
1220 it->len = 1;
1221 PRODUCE_GLYPHS (it);
1222 line_height = it->ascent + it->descent;
1223 it->glyph_row = row;
1224 }
1225 }
1226
1227 return line_top_y + line_height;
1228 }
1229
1230
1231 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1232 1 if POS is visible and the line containing POS is fully visible.
1233 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1234 and header-lines heights. */
1235
1236 int
1237 pos_visible_p (w, charpos, fully, x, y, exact_mode_line_heights_p)
1238 struct window *w;
1239 int charpos, *fully, *x, *y, exact_mode_line_heights_p;
1240 {
1241 struct it it;
1242 struct text_pos top;
1243 int visible_p;
1244 struct buffer *old_buffer = NULL;
1245
1246 if (XBUFFER (w->buffer) != current_buffer)
1247 {
1248 old_buffer = current_buffer;
1249 set_buffer_internal_1 (XBUFFER (w->buffer));
1250 }
1251
1252 *fully = visible_p = 0;
1253 SET_TEXT_POS_FROM_MARKER (top, w->start);
1254
1255 /* Compute exact mode line heights, if requested. */
1256 if (exact_mode_line_heights_p)
1257 {
1258 if (WINDOW_WANTS_MODELINE_P (w))
1259 current_mode_line_height
1260 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1261 current_buffer->mode_line_format);
1262
1263 if (WINDOW_WANTS_HEADER_LINE_P (w))
1264 current_header_line_height
1265 = display_mode_line (w, HEADER_LINE_FACE_ID,
1266 current_buffer->header_line_format);
1267 }
1268
1269 start_display (&it, w, top);
1270 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1271 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1272
1273 /* Note that we may overshoot because of invisible text. */
1274 if (IT_CHARPOS (it) >= charpos)
1275 {
1276 int top_y = it.current_y;
1277 int bottom_y = line_bottom_y (&it);
1278 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1279
1280 if (top_y < window_top_y)
1281 visible_p = bottom_y > window_top_y;
1282 else if (top_y < it.last_visible_y)
1283 {
1284 visible_p = 1;
1285 *fully = bottom_y <= it.last_visible_y;
1286 }
1287 if (visible_p && x)
1288 {
1289 *x = it.current_x;
1290 *y = max (top_y + it.max_ascent - it.ascent, window_top_y);
1291 }
1292 }
1293 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1294 {
1295 struct it it2;
1296
1297 it2 = it;
1298 move_it_by_lines (&it, 1, 0);
1299 if (charpos < IT_CHARPOS (it))
1300 {
1301 visible_p = 1;
1302 if (x)
1303 {
1304 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1305 *x = it2.current_x;
1306 *y = it2.current_y + it2.max_ascent - it2.ascent;
1307 }
1308 }
1309 }
1310
1311 if (old_buffer)
1312 set_buffer_internal_1 (old_buffer);
1313
1314 current_header_line_height = current_mode_line_height = -1;
1315
1316 return visible_p;
1317 }
1318
1319
1320 /* Return the next character from STR which is MAXLEN bytes long.
1321 Return in *LEN the length of the character. This is like
1322 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1323 we find one, we return a `?', but with the length of the invalid
1324 character. */
1325
1326 static INLINE int
1327 string_char_and_length (str, maxlen, len)
1328 const unsigned char *str;
1329 int maxlen, *len;
1330 {
1331 int c;
1332
1333 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1334 if (!CHAR_VALID_P (c, 1))
1335 /* We may not change the length here because other places in Emacs
1336 don't use this function, i.e. they silently accept invalid
1337 characters. */
1338 c = '?';
1339
1340 return c;
1341 }
1342
1343
1344
1345 /* Given a position POS containing a valid character and byte position
1346 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1347
1348 static struct text_pos
1349 string_pos_nchars_ahead (pos, string, nchars)
1350 struct text_pos pos;
1351 Lisp_Object string;
1352 int nchars;
1353 {
1354 xassert (STRINGP (string) && nchars >= 0);
1355
1356 if (STRING_MULTIBYTE (string))
1357 {
1358 int rest = SBYTES (string) - BYTEPOS (pos);
1359 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1360 int len;
1361
1362 while (nchars--)
1363 {
1364 string_char_and_length (p, rest, &len);
1365 p += len, rest -= len;
1366 xassert (rest >= 0);
1367 CHARPOS (pos) += 1;
1368 BYTEPOS (pos) += len;
1369 }
1370 }
1371 else
1372 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1373
1374 return pos;
1375 }
1376
1377
1378 /* Value is the text position, i.e. character and byte position,
1379 for character position CHARPOS in STRING. */
1380
1381 static INLINE struct text_pos
1382 string_pos (charpos, string)
1383 int charpos;
1384 Lisp_Object string;
1385 {
1386 struct text_pos pos;
1387 xassert (STRINGP (string));
1388 xassert (charpos >= 0);
1389 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1390 return pos;
1391 }
1392
1393
1394 /* Value is a text position, i.e. character and byte position, for
1395 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1396 means recognize multibyte characters. */
1397
1398 static struct text_pos
1399 c_string_pos (charpos, s, multibyte_p)
1400 int charpos;
1401 unsigned char *s;
1402 int multibyte_p;
1403 {
1404 struct text_pos pos;
1405
1406 xassert (s != NULL);
1407 xassert (charpos >= 0);
1408
1409 if (multibyte_p)
1410 {
1411 int rest = strlen (s), len;
1412
1413 SET_TEXT_POS (pos, 0, 0);
1414 while (charpos--)
1415 {
1416 string_char_and_length (s, rest, &len);
1417 s += len, rest -= len;
1418 xassert (rest >= 0);
1419 CHARPOS (pos) += 1;
1420 BYTEPOS (pos) += len;
1421 }
1422 }
1423 else
1424 SET_TEXT_POS (pos, charpos, charpos);
1425
1426 return pos;
1427 }
1428
1429
1430 /* Value is the number of characters in C string S. MULTIBYTE_P
1431 non-zero means recognize multibyte characters. */
1432
1433 static int
1434 number_of_chars (s, multibyte_p)
1435 unsigned char *s;
1436 int multibyte_p;
1437 {
1438 int nchars;
1439
1440 if (multibyte_p)
1441 {
1442 int rest = strlen (s), len;
1443 unsigned char *p = (unsigned char *) s;
1444
1445 for (nchars = 0; rest > 0; ++nchars)
1446 {
1447 string_char_and_length (p, rest, &len);
1448 rest -= len, p += len;
1449 }
1450 }
1451 else
1452 nchars = strlen (s);
1453
1454 return nchars;
1455 }
1456
1457
1458 /* Compute byte position NEWPOS->bytepos corresponding to
1459 NEWPOS->charpos. POS is a known position in string STRING.
1460 NEWPOS->charpos must be >= POS.charpos. */
1461
1462 static void
1463 compute_string_pos (newpos, pos, string)
1464 struct text_pos *newpos, pos;
1465 Lisp_Object string;
1466 {
1467 xassert (STRINGP (string));
1468 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1469
1470 if (STRING_MULTIBYTE (string))
1471 *newpos = string_pos_nchars_ahead (pos, string,
1472 CHARPOS (*newpos) - CHARPOS (pos));
1473 else
1474 BYTEPOS (*newpos) = CHARPOS (*newpos);
1475 }
1476
1477 /* EXPORT:
1478 Return an estimation of the pixel height of mode or top lines on
1479 frame F. FACE_ID specifies what line's height to estimate. */
1480
1481 int
1482 estimate_mode_line_height (f, face_id)
1483 struct frame *f;
1484 enum face_id face_id;
1485 {
1486 #ifdef HAVE_WINDOW_SYSTEM
1487 if (FRAME_WINDOW_P (f))
1488 {
1489 int height = FONT_HEIGHT (FRAME_FONT (f));
1490
1491 /* This function is called so early when Emacs starts that the face
1492 cache and mode line face are not yet initialized. */
1493 if (FRAME_FACE_CACHE (f))
1494 {
1495 struct face *face = FACE_FROM_ID (f, face_id);
1496 if (face)
1497 {
1498 if (face->font)
1499 height = FONT_HEIGHT (face->font);
1500 if (face->box_line_width > 0)
1501 height += 2 * face->box_line_width;
1502 }
1503 }
1504
1505 return height;
1506 }
1507 #endif
1508
1509 return 1;
1510 }
1511
1512 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1513 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1514 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1515 not force the value into range. */
1516
1517 void
1518 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1519 FRAME_PTR f;
1520 register int pix_x, pix_y;
1521 int *x, *y;
1522 NativeRectangle *bounds;
1523 int noclip;
1524 {
1525
1526 #ifdef HAVE_WINDOW_SYSTEM
1527 if (FRAME_WINDOW_P (f))
1528 {
1529 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1530 even for negative values. */
1531 if (pix_x < 0)
1532 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1533 if (pix_y < 0)
1534 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1535
1536 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1537 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1538
1539 if (bounds)
1540 STORE_NATIVE_RECT (*bounds,
1541 FRAME_COL_TO_PIXEL_X (f, pix_x),
1542 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1543 FRAME_COLUMN_WIDTH (f) - 1,
1544 FRAME_LINE_HEIGHT (f) - 1);
1545
1546 if (!noclip)
1547 {
1548 if (pix_x < 0)
1549 pix_x = 0;
1550 else if (pix_x > FRAME_TOTAL_COLS (f))
1551 pix_x = FRAME_TOTAL_COLS (f);
1552
1553 if (pix_y < 0)
1554 pix_y = 0;
1555 else if (pix_y > FRAME_LINES (f))
1556 pix_y = FRAME_LINES (f);
1557 }
1558 }
1559 #endif
1560
1561 *x = pix_x;
1562 *y = pix_y;
1563 }
1564
1565
1566 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1567 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1568 can't tell the positions because W's display is not up to date,
1569 return 0. */
1570
1571 int
1572 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1573 struct window *w;
1574 int hpos, vpos;
1575 int *frame_x, *frame_y;
1576 {
1577 #ifdef HAVE_WINDOW_SYSTEM
1578 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1579 {
1580 int success_p;
1581
1582 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1583 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1584
1585 if (display_completed)
1586 {
1587 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1588 struct glyph *glyph = row->glyphs[TEXT_AREA];
1589 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1590
1591 hpos = row->x;
1592 vpos = row->y;
1593 while (glyph < end)
1594 {
1595 hpos += glyph->pixel_width;
1596 ++glyph;
1597 }
1598
1599 /* If first glyph is partially visible, its first visible position is still 0. */
1600 if (hpos < 0)
1601 hpos = 0;
1602
1603 success_p = 1;
1604 }
1605 else
1606 {
1607 hpos = vpos = 0;
1608 success_p = 0;
1609 }
1610
1611 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1612 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1613 return success_p;
1614 }
1615 #endif
1616
1617 *frame_x = hpos;
1618 *frame_y = vpos;
1619 return 1;
1620 }
1621
1622
1623 #ifdef HAVE_WINDOW_SYSTEM
1624
1625 /* Find the glyph under window-relative coordinates X/Y in window W.
1626 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1627 strings. Return in *HPOS and *VPOS the row and column number of
1628 the glyph found. Return in *AREA the glyph area containing X.
1629 Value is a pointer to the glyph found or null if X/Y is not on
1630 text, or we can't tell because W's current matrix is not up to
1631 date. */
1632
1633 static struct glyph *
1634 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1635 struct window *w;
1636 int x, y;
1637 int *hpos, *vpos, *dx, *dy, *area;
1638 {
1639 struct glyph *glyph, *end;
1640 struct glyph_row *row = NULL;
1641 int x0, i;
1642
1643 /* Find row containing Y. Give up if some row is not enabled. */
1644 for (i = 0; i < w->current_matrix->nrows; ++i)
1645 {
1646 row = MATRIX_ROW (w->current_matrix, i);
1647 if (!row->enabled_p)
1648 return NULL;
1649 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1650 break;
1651 }
1652
1653 *vpos = i;
1654 *hpos = 0;
1655
1656 /* Give up if Y is not in the window. */
1657 if (i == w->current_matrix->nrows)
1658 return NULL;
1659
1660 /* Get the glyph area containing X. */
1661 if (w->pseudo_window_p)
1662 {
1663 *area = TEXT_AREA;
1664 x0 = 0;
1665 }
1666 else
1667 {
1668 if (x < window_box_left_offset (w, TEXT_AREA))
1669 {
1670 *area = LEFT_MARGIN_AREA;
1671 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1672 }
1673 else if (x < window_box_right_offset (w, TEXT_AREA))
1674 {
1675 *area = TEXT_AREA;
1676 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1677 }
1678 else
1679 {
1680 *area = RIGHT_MARGIN_AREA;
1681 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1682 }
1683 }
1684
1685 /* Find glyph containing X. */
1686 glyph = row->glyphs[*area];
1687 end = glyph + row->used[*area];
1688 x -= x0;
1689 while (glyph < end && x >= glyph->pixel_width)
1690 {
1691 x -= glyph->pixel_width;
1692 ++glyph;
1693 }
1694
1695 if (glyph == end)
1696 return NULL;
1697
1698 if (dx)
1699 {
1700 *dx = x;
1701 *dy = y - (row->y + row->ascent - glyph->ascent);
1702 }
1703
1704 *hpos = glyph - row->glyphs[*area];
1705 return glyph;
1706 }
1707
1708
1709 /* EXPORT:
1710 Convert frame-relative x/y to coordinates relative to window W.
1711 Takes pseudo-windows into account. */
1712
1713 void
1714 frame_to_window_pixel_xy (w, x, y)
1715 struct window *w;
1716 int *x, *y;
1717 {
1718 if (w->pseudo_window_p)
1719 {
1720 /* A pseudo-window is always full-width, and starts at the
1721 left edge of the frame, plus a frame border. */
1722 struct frame *f = XFRAME (w->frame);
1723 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1724 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1725 }
1726 else
1727 {
1728 *x -= WINDOW_LEFT_EDGE_X (w);
1729 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1730 }
1731 }
1732
1733 /* EXPORT:
1734 Return in *R the clipping rectangle for glyph string S. */
1735
1736 void
1737 get_glyph_string_clip_rect (s, nr)
1738 struct glyph_string *s;
1739 NativeRectangle *nr;
1740 {
1741 XRectangle r;
1742
1743 if (s->row->full_width_p)
1744 {
1745 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1746 r.x = WINDOW_LEFT_EDGE_X (s->w);
1747 r.width = WINDOW_TOTAL_WIDTH (s->w);
1748
1749 /* Unless displaying a mode or menu bar line, which are always
1750 fully visible, clip to the visible part of the row. */
1751 if (s->w->pseudo_window_p)
1752 r.height = s->row->visible_height;
1753 else
1754 r.height = s->height;
1755 }
1756 else
1757 {
1758 /* This is a text line that may be partially visible. */
1759 r.x = window_box_left (s->w, s->area);
1760 r.width = window_box_width (s->w, s->area);
1761 r.height = s->row->visible_height;
1762 }
1763
1764 /* If S draws overlapping rows, it's sufficient to use the top and
1765 bottom of the window for clipping because this glyph string
1766 intentionally draws over other lines. */
1767 if (s->for_overlaps_p)
1768 {
1769 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1770 r.height = window_text_bottom_y (s->w) - r.y;
1771 }
1772 else
1773 {
1774 /* Don't use S->y for clipping because it doesn't take partially
1775 visible lines into account. For example, it can be negative for
1776 partially visible lines at the top of a window. */
1777 if (!s->row->full_width_p
1778 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1779 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1780 else
1781 r.y = max (0, s->row->y);
1782
1783 /* If drawing a tool-bar window, draw it over the internal border
1784 at the top of the window. */
1785 if (s->w == XWINDOW (s->f->tool_bar_window))
1786 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1787 }
1788
1789 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1790
1791 /* If drawing the cursor, don't let glyph draw outside its
1792 advertised boundaries. Cleartype does this under some circumstances. */
1793 if (s->hl == DRAW_CURSOR)
1794 {
1795 struct glyph *glyph = s->first_glyph;
1796 int height;
1797
1798 if (s->x > r.x)
1799 {
1800 r.width -= s->x - r.x;
1801 r.x = s->x;
1802 }
1803 r.width = min (r.width, glyph->pixel_width);
1804
1805 /* Don't draw cursor glyph taller than our actual glyph. */
1806 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1807 if (height < r.height)
1808 {
1809 int max_y = r.y + r.height;
1810 r.y = min (max_y, s->ybase + glyph->descent - height);
1811 r.height = min (max_y - r.y, height);
1812 }
1813 }
1814
1815 #ifdef CONVERT_FROM_XRECT
1816 CONVERT_FROM_XRECT (r, *nr);
1817 #else
1818 *nr = r;
1819 #endif
1820 }
1821
1822 #endif /* HAVE_WINDOW_SYSTEM */
1823
1824 \f
1825 /***********************************************************************
1826 Lisp form evaluation
1827 ***********************************************************************/
1828
1829 /* Error handler for safe_eval and safe_call. */
1830
1831 static Lisp_Object
1832 safe_eval_handler (arg)
1833 Lisp_Object arg;
1834 {
1835 add_to_log ("Error during redisplay: %s", arg, Qnil);
1836 return Qnil;
1837 }
1838
1839
1840 /* Evaluate SEXPR and return the result, or nil if something went
1841 wrong. Prevent redisplay during the evaluation. */
1842
1843 Lisp_Object
1844 safe_eval (sexpr)
1845 Lisp_Object sexpr;
1846 {
1847 Lisp_Object val;
1848
1849 if (inhibit_eval_during_redisplay)
1850 val = Qnil;
1851 else
1852 {
1853 int count = SPECPDL_INDEX ();
1854 struct gcpro gcpro1;
1855
1856 GCPRO1 (sexpr);
1857 specbind (Qinhibit_redisplay, Qt);
1858 /* Use Qt to ensure debugger does not run,
1859 so there is no possibility of wanting to redisplay. */
1860 val = internal_condition_case_1 (Feval, sexpr, Qt,
1861 safe_eval_handler);
1862 UNGCPRO;
1863 val = unbind_to (count, val);
1864 }
1865
1866 return val;
1867 }
1868
1869
1870 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1871 Return the result, or nil if something went wrong. Prevent
1872 redisplay during the evaluation. */
1873
1874 Lisp_Object
1875 safe_call (nargs, args)
1876 int nargs;
1877 Lisp_Object *args;
1878 {
1879 Lisp_Object val;
1880
1881 if (inhibit_eval_during_redisplay)
1882 val = Qnil;
1883 else
1884 {
1885 int count = SPECPDL_INDEX ();
1886 struct gcpro gcpro1;
1887
1888 GCPRO1 (args[0]);
1889 gcpro1.nvars = nargs;
1890 specbind (Qinhibit_redisplay, Qt);
1891 /* Use Qt to ensure debugger does not run,
1892 so there is no possibility of wanting to redisplay. */
1893 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1894 safe_eval_handler);
1895 UNGCPRO;
1896 val = unbind_to (count, val);
1897 }
1898
1899 return val;
1900 }
1901
1902
1903 /* Call function FN with one argument ARG.
1904 Return the result, or nil if something went wrong. */
1905
1906 Lisp_Object
1907 safe_call1 (fn, arg)
1908 Lisp_Object fn, arg;
1909 {
1910 Lisp_Object args[2];
1911 args[0] = fn;
1912 args[1] = arg;
1913 return safe_call (2, args);
1914 }
1915
1916
1917 \f
1918 /***********************************************************************
1919 Debugging
1920 ***********************************************************************/
1921
1922 #if 0
1923
1924 /* Define CHECK_IT to perform sanity checks on iterators.
1925 This is for debugging. It is too slow to do unconditionally. */
1926
1927 static void
1928 check_it (it)
1929 struct it *it;
1930 {
1931 if (it->method == next_element_from_string)
1932 {
1933 xassert (STRINGP (it->string));
1934 xassert (IT_STRING_CHARPOS (*it) >= 0);
1935 }
1936 else
1937 {
1938 xassert (IT_STRING_CHARPOS (*it) < 0);
1939 if (it->method == next_element_from_buffer)
1940 {
1941 /* Check that character and byte positions agree. */
1942 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1943 }
1944 }
1945
1946 if (it->dpvec)
1947 xassert (it->current.dpvec_index >= 0);
1948 else
1949 xassert (it->current.dpvec_index < 0);
1950 }
1951
1952 #define CHECK_IT(IT) check_it ((IT))
1953
1954 #else /* not 0 */
1955
1956 #define CHECK_IT(IT) (void) 0
1957
1958 #endif /* not 0 */
1959
1960
1961 #if GLYPH_DEBUG
1962
1963 /* Check that the window end of window W is what we expect it
1964 to be---the last row in the current matrix displaying text. */
1965
1966 static void
1967 check_window_end (w)
1968 struct window *w;
1969 {
1970 if (!MINI_WINDOW_P (w)
1971 && !NILP (w->window_end_valid))
1972 {
1973 struct glyph_row *row;
1974 xassert ((row = MATRIX_ROW (w->current_matrix,
1975 XFASTINT (w->window_end_vpos)),
1976 !row->enabled_p
1977 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1978 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1979 }
1980 }
1981
1982 #define CHECK_WINDOW_END(W) check_window_end ((W))
1983
1984 #else /* not GLYPH_DEBUG */
1985
1986 #define CHECK_WINDOW_END(W) (void) 0
1987
1988 #endif /* not GLYPH_DEBUG */
1989
1990
1991 \f
1992 /***********************************************************************
1993 Iterator initialization
1994 ***********************************************************************/
1995
1996 /* Initialize IT for displaying current_buffer in window W, starting
1997 at character position CHARPOS. CHARPOS < 0 means that no buffer
1998 position is specified which is useful when the iterator is assigned
1999 a position later. BYTEPOS is the byte position corresponding to
2000 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2001
2002 If ROW is not null, calls to produce_glyphs with IT as parameter
2003 will produce glyphs in that row.
2004
2005 BASE_FACE_ID is the id of a base face to use. It must be one of
2006 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2007 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2008 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2009
2010 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2011 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2012 will be initialized to use the corresponding mode line glyph row of
2013 the desired matrix of W. */
2014
2015 void
2016 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2017 struct it *it;
2018 struct window *w;
2019 int charpos, bytepos;
2020 struct glyph_row *row;
2021 enum face_id base_face_id;
2022 {
2023 int highlight_region_p;
2024
2025 /* Some precondition checks. */
2026 xassert (w != NULL && it != NULL);
2027 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2028 && charpos <= ZV));
2029
2030 /* If face attributes have been changed since the last redisplay,
2031 free realized faces now because they depend on face definitions
2032 that might have changed. Don't free faces while there might be
2033 desired matrices pending which reference these faces. */
2034 if (face_change_count && !inhibit_free_realized_faces)
2035 {
2036 face_change_count = 0;
2037 free_all_realized_faces (Qnil);
2038 }
2039
2040 /* Use one of the mode line rows of W's desired matrix if
2041 appropriate. */
2042 if (row == NULL)
2043 {
2044 if (base_face_id == MODE_LINE_FACE_ID
2045 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2046 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2047 else if (base_face_id == HEADER_LINE_FACE_ID)
2048 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2049 }
2050
2051 /* Clear IT. */
2052 bzero (it, sizeof *it);
2053 it->current.overlay_string_index = -1;
2054 it->current.dpvec_index = -1;
2055 it->base_face_id = base_face_id;
2056 it->string = Qnil;
2057 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2058
2059 /* The window in which we iterate over current_buffer: */
2060 XSETWINDOW (it->window, w);
2061 it->w = w;
2062 it->f = XFRAME (w->frame);
2063
2064 /* Extra space between lines (on window systems only). */
2065 if (base_face_id == DEFAULT_FACE_ID
2066 && FRAME_WINDOW_P (it->f))
2067 {
2068 if (NATNUMP (current_buffer->extra_line_spacing))
2069 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2070 else if (FLOATP (current_buffer->extra_line_spacing))
2071 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2072 * FRAME_LINE_HEIGHT (it->f));
2073 else if (it->f->extra_line_spacing > 0)
2074 it->extra_line_spacing = it->f->extra_line_spacing;
2075 }
2076
2077 /* If realized faces have been removed, e.g. because of face
2078 attribute changes of named faces, recompute them. When running
2079 in batch mode, the face cache of the initial frame is null. If
2080 we happen to get called, make a dummy face cache. */
2081 if (FRAME_FACE_CACHE (it->f) == NULL)
2082 init_frame_faces (it->f);
2083 if (FRAME_FACE_CACHE (it->f)->used == 0)
2084 recompute_basic_faces (it->f);
2085
2086 /* Current value of the `slice', `space-width', and 'height' properties. */
2087 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2088 it->space_width = Qnil;
2089 it->font_height = Qnil;
2090 it->override_ascent = -1;
2091
2092 /* Are control characters displayed as `^C'? */
2093 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2094
2095 /* -1 means everything between a CR and the following line end
2096 is invisible. >0 means lines indented more than this value are
2097 invisible. */
2098 it->selective = (INTEGERP (current_buffer->selective_display)
2099 ? XFASTINT (current_buffer->selective_display)
2100 : (!NILP (current_buffer->selective_display)
2101 ? -1 : 0));
2102 it->selective_display_ellipsis_p
2103 = !NILP (current_buffer->selective_display_ellipses);
2104
2105 /* Display table to use. */
2106 it->dp = window_display_table (w);
2107
2108 /* Are multibyte characters enabled in current_buffer? */
2109 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2110
2111 /* Non-zero if we should highlight the region. */
2112 highlight_region_p
2113 = (!NILP (Vtransient_mark_mode)
2114 && !NILP (current_buffer->mark_active)
2115 && XMARKER (current_buffer->mark)->buffer != 0);
2116
2117 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2118 start and end of a visible region in window IT->w. Set both to
2119 -1 to indicate no region. */
2120 if (highlight_region_p
2121 /* Maybe highlight only in selected window. */
2122 && (/* Either show region everywhere. */
2123 highlight_nonselected_windows
2124 /* Or show region in the selected window. */
2125 || w == XWINDOW (selected_window)
2126 /* Or show the region if we are in the mini-buffer and W is
2127 the window the mini-buffer refers to. */
2128 || (MINI_WINDOW_P (XWINDOW (selected_window))
2129 && WINDOWP (minibuf_selected_window)
2130 && w == XWINDOW (minibuf_selected_window))))
2131 {
2132 int charpos = marker_position (current_buffer->mark);
2133 it->region_beg_charpos = min (PT, charpos);
2134 it->region_end_charpos = max (PT, charpos);
2135 }
2136 else
2137 it->region_beg_charpos = it->region_end_charpos = -1;
2138
2139 /* Get the position at which the redisplay_end_trigger hook should
2140 be run, if it is to be run at all. */
2141 if (MARKERP (w->redisplay_end_trigger)
2142 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2143 it->redisplay_end_trigger_charpos
2144 = marker_position (w->redisplay_end_trigger);
2145 else if (INTEGERP (w->redisplay_end_trigger))
2146 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2147
2148 /* Correct bogus values of tab_width. */
2149 it->tab_width = XINT (current_buffer->tab_width);
2150 if (it->tab_width <= 0 || it->tab_width > 1000)
2151 it->tab_width = 8;
2152
2153 /* Are lines in the display truncated? */
2154 it->truncate_lines_p
2155 = (base_face_id != DEFAULT_FACE_ID
2156 || XINT (it->w->hscroll)
2157 || (truncate_partial_width_windows
2158 && !WINDOW_FULL_WIDTH_P (it->w))
2159 || !NILP (current_buffer->truncate_lines));
2160
2161 /* Get dimensions of truncation and continuation glyphs. These are
2162 displayed as fringe bitmaps under X, so we don't need them for such
2163 frames. */
2164 if (!FRAME_WINDOW_P (it->f))
2165 {
2166 if (it->truncate_lines_p)
2167 {
2168 /* We will need the truncation glyph. */
2169 xassert (it->glyph_row == NULL);
2170 produce_special_glyphs (it, IT_TRUNCATION);
2171 it->truncation_pixel_width = it->pixel_width;
2172 }
2173 else
2174 {
2175 /* We will need the continuation glyph. */
2176 xassert (it->glyph_row == NULL);
2177 produce_special_glyphs (it, IT_CONTINUATION);
2178 it->continuation_pixel_width = it->pixel_width;
2179 }
2180
2181 /* Reset these values to zero because the produce_special_glyphs
2182 above has changed them. */
2183 it->pixel_width = it->ascent = it->descent = 0;
2184 it->phys_ascent = it->phys_descent = 0;
2185 }
2186
2187 /* Set this after getting the dimensions of truncation and
2188 continuation glyphs, so that we don't produce glyphs when calling
2189 produce_special_glyphs, above. */
2190 it->glyph_row = row;
2191 it->area = TEXT_AREA;
2192
2193 /* Get the dimensions of the display area. The display area
2194 consists of the visible window area plus a horizontally scrolled
2195 part to the left of the window. All x-values are relative to the
2196 start of this total display area. */
2197 if (base_face_id != DEFAULT_FACE_ID)
2198 {
2199 /* Mode lines, menu bar in terminal frames. */
2200 it->first_visible_x = 0;
2201 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2202 }
2203 else
2204 {
2205 it->first_visible_x
2206 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2207 it->last_visible_x = (it->first_visible_x
2208 + window_box_width (w, TEXT_AREA));
2209
2210 /* If we truncate lines, leave room for the truncator glyph(s) at
2211 the right margin. Otherwise, leave room for the continuation
2212 glyph(s). Truncation and continuation glyphs are not inserted
2213 for window-based redisplay. */
2214 if (!FRAME_WINDOW_P (it->f))
2215 {
2216 if (it->truncate_lines_p)
2217 it->last_visible_x -= it->truncation_pixel_width;
2218 else
2219 it->last_visible_x -= it->continuation_pixel_width;
2220 }
2221
2222 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2223 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2224 }
2225
2226 /* Leave room for a border glyph. */
2227 if (!FRAME_WINDOW_P (it->f)
2228 && !WINDOW_RIGHTMOST_P (it->w))
2229 it->last_visible_x -= 1;
2230
2231 it->last_visible_y = window_text_bottom_y (w);
2232
2233 /* For mode lines and alike, arrange for the first glyph having a
2234 left box line if the face specifies a box. */
2235 if (base_face_id != DEFAULT_FACE_ID)
2236 {
2237 struct face *face;
2238
2239 it->face_id = base_face_id;
2240
2241 /* If we have a boxed mode line, make the first character appear
2242 with a left box line. */
2243 face = FACE_FROM_ID (it->f, base_face_id);
2244 if (face->box != FACE_NO_BOX)
2245 it->start_of_box_run_p = 1;
2246 }
2247
2248 /* If a buffer position was specified, set the iterator there,
2249 getting overlays and face properties from that position. */
2250 if (charpos >= BUF_BEG (current_buffer))
2251 {
2252 it->end_charpos = ZV;
2253 it->face_id = -1;
2254 IT_CHARPOS (*it) = charpos;
2255
2256 /* Compute byte position if not specified. */
2257 if (bytepos < charpos)
2258 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2259 else
2260 IT_BYTEPOS (*it) = bytepos;
2261
2262 it->start = it->current;
2263
2264 /* Compute faces etc. */
2265 reseat (it, it->current.pos, 1);
2266 }
2267
2268 CHECK_IT (it);
2269 }
2270
2271
2272 /* Initialize IT for the display of window W with window start POS. */
2273
2274 void
2275 start_display (it, w, pos)
2276 struct it *it;
2277 struct window *w;
2278 struct text_pos pos;
2279 {
2280 struct glyph_row *row;
2281 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2282
2283 row = w->desired_matrix->rows + first_vpos;
2284 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2285 it->first_vpos = first_vpos;
2286
2287 if (!it->truncate_lines_p)
2288 {
2289 int start_at_line_beg_p;
2290 int first_y = it->current_y;
2291
2292 /* If window start is not at a line start, skip forward to POS to
2293 get the correct continuation lines width. */
2294 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2295 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2296 if (!start_at_line_beg_p)
2297 {
2298 int new_x;
2299
2300 reseat_at_previous_visible_line_start (it);
2301 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2302
2303 new_x = it->current_x + it->pixel_width;
2304
2305 /* If lines are continued, this line may end in the middle
2306 of a multi-glyph character (e.g. a control character
2307 displayed as \003, or in the middle of an overlay
2308 string). In this case move_it_to above will not have
2309 taken us to the start of the continuation line but to the
2310 end of the continued line. */
2311 if (it->current_x > 0
2312 && !it->truncate_lines_p /* Lines are continued. */
2313 && (/* And glyph doesn't fit on the line. */
2314 new_x > it->last_visible_x
2315 /* Or it fits exactly and we're on a window
2316 system frame. */
2317 || (new_x == it->last_visible_x
2318 && FRAME_WINDOW_P (it->f))))
2319 {
2320 if (it->current.dpvec_index >= 0
2321 || it->current.overlay_string_index >= 0)
2322 {
2323 set_iterator_to_next (it, 1);
2324 move_it_in_display_line_to (it, -1, -1, 0);
2325 }
2326
2327 it->continuation_lines_width += it->current_x;
2328 }
2329
2330 /* We're starting a new display line, not affected by the
2331 height of the continued line, so clear the appropriate
2332 fields in the iterator structure. */
2333 it->max_ascent = it->max_descent = 0;
2334 it->max_phys_ascent = it->max_phys_descent = 0;
2335
2336 it->current_y = first_y;
2337 it->vpos = 0;
2338 it->current_x = it->hpos = 0;
2339 }
2340 }
2341
2342 #if 0 /* Don't assert the following because start_display is sometimes
2343 called intentionally with a window start that is not at a
2344 line start. Please leave this code in as a comment. */
2345
2346 /* Window start should be on a line start, now. */
2347 xassert (it->continuation_lines_width
2348 || IT_CHARPOS (it) == BEGV
2349 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2350 #endif /* 0 */
2351 }
2352
2353
2354 /* Return 1 if POS is a position in ellipses displayed for invisible
2355 text. W is the window we display, for text property lookup. */
2356
2357 static int
2358 in_ellipses_for_invisible_text_p (pos, w)
2359 struct display_pos *pos;
2360 struct window *w;
2361 {
2362 Lisp_Object prop, window;
2363 int ellipses_p = 0;
2364 int charpos = CHARPOS (pos->pos);
2365
2366 /* If POS specifies a position in a display vector, this might
2367 be for an ellipsis displayed for invisible text. We won't
2368 get the iterator set up for delivering that ellipsis unless
2369 we make sure that it gets aware of the invisible text. */
2370 if (pos->dpvec_index >= 0
2371 && pos->overlay_string_index < 0
2372 && CHARPOS (pos->string_pos) < 0
2373 && charpos > BEGV
2374 && (XSETWINDOW (window, w),
2375 prop = Fget_char_property (make_number (charpos),
2376 Qinvisible, window),
2377 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2378 {
2379 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2380 window);
2381 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2382 }
2383
2384 return ellipses_p;
2385 }
2386
2387
2388 /* Initialize IT for stepping through current_buffer in window W,
2389 starting at position POS that includes overlay string and display
2390 vector/ control character translation position information. Value
2391 is zero if there are overlay strings with newlines at POS. */
2392
2393 static int
2394 init_from_display_pos (it, w, pos)
2395 struct it *it;
2396 struct window *w;
2397 struct display_pos *pos;
2398 {
2399 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2400 int i, overlay_strings_with_newlines = 0;
2401
2402 /* If POS specifies a position in a display vector, this might
2403 be for an ellipsis displayed for invisible text. We won't
2404 get the iterator set up for delivering that ellipsis unless
2405 we make sure that it gets aware of the invisible text. */
2406 if (in_ellipses_for_invisible_text_p (pos, w))
2407 {
2408 --charpos;
2409 bytepos = 0;
2410 }
2411
2412 /* Keep in mind: the call to reseat in init_iterator skips invisible
2413 text, so we might end up at a position different from POS. This
2414 is only a problem when POS is a row start after a newline and an
2415 overlay starts there with an after-string, and the overlay has an
2416 invisible property. Since we don't skip invisible text in
2417 display_line and elsewhere immediately after consuming the
2418 newline before the row start, such a POS will not be in a string,
2419 but the call to init_iterator below will move us to the
2420 after-string. */
2421 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2422
2423 for (i = 0; i < it->n_overlay_strings; ++i)
2424 {
2425 const char *s = SDATA (it->overlay_strings[i]);
2426 const char *e = s + SBYTES (it->overlay_strings[i]);
2427
2428 while (s < e && *s != '\n')
2429 ++s;
2430
2431 if (s < e)
2432 {
2433 overlay_strings_with_newlines = 1;
2434 break;
2435 }
2436 }
2437
2438 /* If position is within an overlay string, set up IT to the right
2439 overlay string. */
2440 if (pos->overlay_string_index >= 0)
2441 {
2442 int relative_index;
2443
2444 /* If the first overlay string happens to have a `display'
2445 property for an image, the iterator will be set up for that
2446 image, and we have to undo that setup first before we can
2447 correct the overlay string index. */
2448 if (it->method == next_element_from_image)
2449 pop_it (it);
2450
2451 /* We already have the first chunk of overlay strings in
2452 IT->overlay_strings. Load more until the one for
2453 pos->overlay_string_index is in IT->overlay_strings. */
2454 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2455 {
2456 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2457 it->current.overlay_string_index = 0;
2458 while (n--)
2459 {
2460 load_overlay_strings (it, 0);
2461 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2462 }
2463 }
2464
2465 it->current.overlay_string_index = pos->overlay_string_index;
2466 relative_index = (it->current.overlay_string_index
2467 % OVERLAY_STRING_CHUNK_SIZE);
2468 it->string = it->overlay_strings[relative_index];
2469 xassert (STRINGP (it->string));
2470 it->current.string_pos = pos->string_pos;
2471 it->method = next_element_from_string;
2472 }
2473
2474 #if 0 /* This is bogus because POS not having an overlay string
2475 position does not mean it's after the string. Example: A
2476 line starting with a before-string and initialization of IT
2477 to the previous row's end position. */
2478 else if (it->current.overlay_string_index >= 0)
2479 {
2480 /* If POS says we're already after an overlay string ending at
2481 POS, make sure to pop the iterator because it will be in
2482 front of that overlay string. When POS is ZV, we've thereby
2483 also ``processed'' overlay strings at ZV. */
2484 while (it->sp)
2485 pop_it (it);
2486 it->current.overlay_string_index = -1;
2487 it->method = next_element_from_buffer;
2488 if (CHARPOS (pos->pos) == ZV)
2489 it->overlay_strings_at_end_processed_p = 1;
2490 }
2491 #endif /* 0 */
2492
2493 if (CHARPOS (pos->string_pos) >= 0)
2494 {
2495 /* Recorded position is not in an overlay string, but in another
2496 string. This can only be a string from a `display' property.
2497 IT should already be filled with that string. */
2498 it->current.string_pos = pos->string_pos;
2499 xassert (STRINGP (it->string));
2500 }
2501
2502 /* Restore position in display vector translations, control
2503 character translations or ellipses. */
2504 if (pos->dpvec_index >= 0)
2505 {
2506 if (it->dpvec == NULL)
2507 get_next_display_element (it);
2508 xassert (it->dpvec && it->current.dpvec_index == 0);
2509 it->current.dpvec_index = pos->dpvec_index;
2510 }
2511
2512 CHECK_IT (it);
2513 return !overlay_strings_with_newlines;
2514 }
2515
2516
2517 /* Initialize IT for stepping through current_buffer in window W
2518 starting at ROW->start. */
2519
2520 static void
2521 init_to_row_start (it, w, row)
2522 struct it *it;
2523 struct window *w;
2524 struct glyph_row *row;
2525 {
2526 init_from_display_pos (it, w, &row->start);
2527 it->start = row->start;
2528 it->continuation_lines_width = row->continuation_lines_width;
2529 CHECK_IT (it);
2530 }
2531
2532
2533 /* Initialize IT for stepping through current_buffer in window W
2534 starting in the line following ROW, i.e. starting at ROW->end.
2535 Value is zero if there are overlay strings with newlines at ROW's
2536 end position. */
2537
2538 static int
2539 init_to_row_end (it, w, row)
2540 struct it *it;
2541 struct window *w;
2542 struct glyph_row *row;
2543 {
2544 int success = 0;
2545
2546 if (init_from_display_pos (it, w, &row->end))
2547 {
2548 if (row->continued_p)
2549 it->continuation_lines_width
2550 = row->continuation_lines_width + row->pixel_width;
2551 CHECK_IT (it);
2552 success = 1;
2553 }
2554
2555 return success;
2556 }
2557
2558
2559
2560 \f
2561 /***********************************************************************
2562 Text properties
2563 ***********************************************************************/
2564
2565 /* Called when IT reaches IT->stop_charpos. Handle text property and
2566 overlay changes. Set IT->stop_charpos to the next position where
2567 to stop. */
2568
2569 static void
2570 handle_stop (it)
2571 struct it *it;
2572 {
2573 enum prop_handled handled;
2574 int handle_overlay_change_p = 1;
2575 struct props *p;
2576
2577 it->dpvec = NULL;
2578 it->current.dpvec_index = -1;
2579
2580 do
2581 {
2582 handled = HANDLED_NORMALLY;
2583
2584 /* Call text property handlers. */
2585 for (p = it_props; p->handler; ++p)
2586 {
2587 handled = p->handler (it);
2588
2589 if (handled == HANDLED_RECOMPUTE_PROPS)
2590 break;
2591 else if (handled == HANDLED_RETURN)
2592 return;
2593 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2594 handle_overlay_change_p = 0;
2595 }
2596
2597 if (handled != HANDLED_RECOMPUTE_PROPS)
2598 {
2599 /* Don't check for overlay strings below when set to deliver
2600 characters from a display vector. */
2601 if (it->method == next_element_from_display_vector)
2602 handle_overlay_change_p = 0;
2603
2604 /* Handle overlay changes. */
2605 if (handle_overlay_change_p)
2606 handled = handle_overlay_change (it);
2607
2608 /* Determine where to stop next. */
2609 if (handled == HANDLED_NORMALLY)
2610 compute_stop_pos (it);
2611 }
2612 }
2613 while (handled == HANDLED_RECOMPUTE_PROPS);
2614 }
2615
2616
2617 /* Compute IT->stop_charpos from text property and overlay change
2618 information for IT's current position. */
2619
2620 static void
2621 compute_stop_pos (it)
2622 struct it *it;
2623 {
2624 register INTERVAL iv, next_iv;
2625 Lisp_Object object, limit, position;
2626
2627 /* If nowhere else, stop at the end. */
2628 it->stop_charpos = it->end_charpos;
2629
2630 if (STRINGP (it->string))
2631 {
2632 /* Strings are usually short, so don't limit the search for
2633 properties. */
2634 object = it->string;
2635 limit = Qnil;
2636 position = make_number (IT_STRING_CHARPOS (*it));
2637 }
2638 else
2639 {
2640 int charpos;
2641
2642 /* If next overlay change is in front of the current stop pos
2643 (which is IT->end_charpos), stop there. Note: value of
2644 next_overlay_change is point-max if no overlay change
2645 follows. */
2646 charpos = next_overlay_change (IT_CHARPOS (*it));
2647 if (charpos < it->stop_charpos)
2648 it->stop_charpos = charpos;
2649
2650 /* If showing the region, we have to stop at the region
2651 start or end because the face might change there. */
2652 if (it->region_beg_charpos > 0)
2653 {
2654 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2655 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2656 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2657 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2658 }
2659
2660 /* Set up variables for computing the stop position from text
2661 property changes. */
2662 XSETBUFFER (object, current_buffer);
2663 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2664 position = make_number (IT_CHARPOS (*it));
2665
2666 }
2667
2668 /* Get the interval containing IT's position. Value is a null
2669 interval if there isn't such an interval. */
2670 iv = validate_interval_range (object, &position, &position, 0);
2671 if (!NULL_INTERVAL_P (iv))
2672 {
2673 Lisp_Object values_here[LAST_PROP_IDX];
2674 struct props *p;
2675
2676 /* Get properties here. */
2677 for (p = it_props; p->handler; ++p)
2678 values_here[p->idx] = textget (iv->plist, *p->name);
2679
2680 /* Look for an interval following iv that has different
2681 properties. */
2682 for (next_iv = next_interval (iv);
2683 (!NULL_INTERVAL_P (next_iv)
2684 && (NILP (limit)
2685 || XFASTINT (limit) > next_iv->position));
2686 next_iv = next_interval (next_iv))
2687 {
2688 for (p = it_props; p->handler; ++p)
2689 {
2690 Lisp_Object new_value;
2691
2692 new_value = textget (next_iv->plist, *p->name);
2693 if (!EQ (values_here[p->idx], new_value))
2694 break;
2695 }
2696
2697 if (p->handler)
2698 break;
2699 }
2700
2701 if (!NULL_INTERVAL_P (next_iv))
2702 {
2703 if (INTEGERP (limit)
2704 && next_iv->position >= XFASTINT (limit))
2705 /* No text property change up to limit. */
2706 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2707 else
2708 /* Text properties change in next_iv. */
2709 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2710 }
2711 }
2712
2713 xassert (STRINGP (it->string)
2714 || (it->stop_charpos >= BEGV
2715 && it->stop_charpos >= IT_CHARPOS (*it)));
2716 }
2717
2718
2719 /* Return the position of the next overlay change after POS in
2720 current_buffer. Value is point-max if no overlay change
2721 follows. This is like `next-overlay-change' but doesn't use
2722 xmalloc. */
2723
2724 static int
2725 next_overlay_change (pos)
2726 int pos;
2727 {
2728 int noverlays;
2729 int endpos;
2730 Lisp_Object *overlays;
2731 int i;
2732
2733 /* Get all overlays at the given position. */
2734 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
2735
2736 /* If any of these overlays ends before endpos,
2737 use its ending point instead. */
2738 for (i = 0; i < noverlays; ++i)
2739 {
2740 Lisp_Object oend;
2741 int oendpos;
2742
2743 oend = OVERLAY_END (overlays[i]);
2744 oendpos = OVERLAY_POSITION (oend);
2745 endpos = min (endpos, oendpos);
2746 }
2747
2748 return endpos;
2749 }
2750
2751
2752 \f
2753 /***********************************************************************
2754 Fontification
2755 ***********************************************************************/
2756
2757 /* Handle changes in the `fontified' property of the current buffer by
2758 calling hook functions from Qfontification_functions to fontify
2759 regions of text. */
2760
2761 static enum prop_handled
2762 handle_fontified_prop (it)
2763 struct it *it;
2764 {
2765 Lisp_Object prop, pos;
2766 enum prop_handled handled = HANDLED_NORMALLY;
2767
2768 /* Get the value of the `fontified' property at IT's current buffer
2769 position. (The `fontified' property doesn't have a special
2770 meaning in strings.) If the value is nil, call functions from
2771 Qfontification_functions. */
2772 if (!STRINGP (it->string)
2773 && it->s == NULL
2774 && !NILP (Vfontification_functions)
2775 && !NILP (Vrun_hooks)
2776 && (pos = make_number (IT_CHARPOS (*it)),
2777 prop = Fget_char_property (pos, Qfontified, Qnil),
2778 NILP (prop)))
2779 {
2780 int count = SPECPDL_INDEX ();
2781 Lisp_Object val;
2782
2783 val = Vfontification_functions;
2784 specbind (Qfontification_functions, Qnil);
2785
2786 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2787 safe_call1 (val, pos);
2788 else
2789 {
2790 Lisp_Object globals, fn;
2791 struct gcpro gcpro1, gcpro2;
2792
2793 globals = Qnil;
2794 GCPRO2 (val, globals);
2795
2796 for (; CONSP (val); val = XCDR (val))
2797 {
2798 fn = XCAR (val);
2799
2800 if (EQ (fn, Qt))
2801 {
2802 /* A value of t indicates this hook has a local
2803 binding; it means to run the global binding too.
2804 In a global value, t should not occur. If it
2805 does, we must ignore it to avoid an endless
2806 loop. */
2807 for (globals = Fdefault_value (Qfontification_functions);
2808 CONSP (globals);
2809 globals = XCDR (globals))
2810 {
2811 fn = XCAR (globals);
2812 if (!EQ (fn, Qt))
2813 safe_call1 (fn, pos);
2814 }
2815 }
2816 else
2817 safe_call1 (fn, pos);
2818 }
2819
2820 UNGCPRO;
2821 }
2822
2823 unbind_to (count, Qnil);
2824
2825 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2826 something. This avoids an endless loop if they failed to
2827 fontify the text for which reason ever. */
2828 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2829 handled = HANDLED_RECOMPUTE_PROPS;
2830 }
2831
2832 return handled;
2833 }
2834
2835
2836 \f
2837 /***********************************************************************
2838 Faces
2839 ***********************************************************************/
2840
2841 /* Set up iterator IT from face properties at its current position.
2842 Called from handle_stop. */
2843
2844 static enum prop_handled
2845 handle_face_prop (it)
2846 struct it *it;
2847 {
2848 int new_face_id, next_stop;
2849
2850 if (!STRINGP (it->string))
2851 {
2852 new_face_id
2853 = face_at_buffer_position (it->w,
2854 IT_CHARPOS (*it),
2855 it->region_beg_charpos,
2856 it->region_end_charpos,
2857 &next_stop,
2858 (IT_CHARPOS (*it)
2859 + TEXT_PROP_DISTANCE_LIMIT),
2860 0);
2861
2862 /* Is this a start of a run of characters with box face?
2863 Caveat: this can be called for a freshly initialized
2864 iterator; face_id is -1 in this case. We know that the new
2865 face will not change until limit, i.e. if the new face has a
2866 box, all characters up to limit will have one. But, as
2867 usual, we don't know whether limit is really the end. */
2868 if (new_face_id != it->face_id)
2869 {
2870 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2871
2872 /* If new face has a box but old face has not, this is
2873 the start of a run of characters with box, i.e. it has
2874 a shadow on the left side. The value of face_id of the
2875 iterator will be -1 if this is the initial call that gets
2876 the face. In this case, we have to look in front of IT's
2877 position and see whether there is a face != new_face_id. */
2878 it->start_of_box_run_p
2879 = (new_face->box != FACE_NO_BOX
2880 && (it->face_id >= 0
2881 || IT_CHARPOS (*it) == BEG
2882 || new_face_id != face_before_it_pos (it)));
2883 it->face_box_p = new_face->box != FACE_NO_BOX;
2884 }
2885 }
2886 else
2887 {
2888 int base_face_id, bufpos;
2889
2890 if (it->current.overlay_string_index >= 0)
2891 bufpos = IT_CHARPOS (*it);
2892 else
2893 bufpos = 0;
2894
2895 /* For strings from a buffer, i.e. overlay strings or strings
2896 from a `display' property, use the face at IT's current
2897 buffer position as the base face to merge with, so that
2898 overlay strings appear in the same face as surrounding
2899 text, unless they specify their own faces. */
2900 base_face_id = underlying_face_id (it);
2901
2902 new_face_id = face_at_string_position (it->w,
2903 it->string,
2904 IT_STRING_CHARPOS (*it),
2905 bufpos,
2906 it->region_beg_charpos,
2907 it->region_end_charpos,
2908 &next_stop,
2909 base_face_id, 0);
2910
2911 #if 0 /* This shouldn't be neccessary. Let's check it. */
2912 /* If IT is used to display a mode line we would really like to
2913 use the mode line face instead of the frame's default face. */
2914 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2915 && new_face_id == DEFAULT_FACE_ID)
2916 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2917 #endif
2918
2919 /* Is this a start of a run of characters with box? Caveat:
2920 this can be called for a freshly allocated iterator; face_id
2921 is -1 is this case. We know that the new face will not
2922 change until the next check pos, i.e. if the new face has a
2923 box, all characters up to that position will have a
2924 box. But, as usual, we don't know whether that position
2925 is really the end. */
2926 if (new_face_id != it->face_id)
2927 {
2928 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2929 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2930
2931 /* If new face has a box but old face hasn't, this is the
2932 start of a run of characters with box, i.e. it has a
2933 shadow on the left side. */
2934 it->start_of_box_run_p
2935 = new_face->box && (old_face == NULL || !old_face->box);
2936 it->face_box_p = new_face->box != FACE_NO_BOX;
2937 }
2938 }
2939
2940 it->face_id = new_face_id;
2941 return HANDLED_NORMALLY;
2942 }
2943
2944
2945 /* Return the ID of the face ``underlying'' IT's current position,
2946 which is in a string. If the iterator is associated with a
2947 buffer, return the face at IT's current buffer position.
2948 Otherwise, use the iterator's base_face_id. */
2949
2950 static int
2951 underlying_face_id (it)
2952 struct it *it;
2953 {
2954 int face_id = it->base_face_id, i;
2955
2956 xassert (STRINGP (it->string));
2957
2958 for (i = it->sp - 1; i >= 0; --i)
2959 if (NILP (it->stack[i].string))
2960 face_id = it->stack[i].face_id;
2961
2962 return face_id;
2963 }
2964
2965
2966 /* Compute the face one character before or after the current position
2967 of IT. BEFORE_P non-zero means get the face in front of IT's
2968 position. Value is the id of the face. */
2969
2970 static int
2971 face_before_or_after_it_pos (it, before_p)
2972 struct it *it;
2973 int before_p;
2974 {
2975 int face_id, limit;
2976 int next_check_charpos;
2977 struct text_pos pos;
2978
2979 xassert (it->s == NULL);
2980
2981 if (STRINGP (it->string))
2982 {
2983 int bufpos, base_face_id;
2984
2985 /* No face change past the end of the string (for the case
2986 we are padding with spaces). No face change before the
2987 string start. */
2988 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
2989 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2990 return it->face_id;
2991
2992 /* Set pos to the position before or after IT's current position. */
2993 if (before_p)
2994 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2995 else
2996 /* For composition, we must check the character after the
2997 composition. */
2998 pos = (it->what == IT_COMPOSITION
2999 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3000 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3001
3002 if (it->current.overlay_string_index >= 0)
3003 bufpos = IT_CHARPOS (*it);
3004 else
3005 bufpos = 0;
3006
3007 base_face_id = underlying_face_id (it);
3008
3009 /* Get the face for ASCII, or unibyte. */
3010 face_id = face_at_string_position (it->w,
3011 it->string,
3012 CHARPOS (pos),
3013 bufpos,
3014 it->region_beg_charpos,
3015 it->region_end_charpos,
3016 &next_check_charpos,
3017 base_face_id, 0);
3018
3019 /* Correct the face for charsets different from ASCII. Do it
3020 for the multibyte case only. The face returned above is
3021 suitable for unibyte text if IT->string is unibyte. */
3022 if (STRING_MULTIBYTE (it->string))
3023 {
3024 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3025 int rest = SBYTES (it->string) - BYTEPOS (pos);
3026 int c, len;
3027 struct face *face = FACE_FROM_ID (it->f, face_id);
3028
3029 c = string_char_and_length (p, rest, &len);
3030 face_id = FACE_FOR_CHAR (it->f, face, c);
3031 }
3032 }
3033 else
3034 {
3035 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3036 || (IT_CHARPOS (*it) <= BEGV && before_p))
3037 return it->face_id;
3038
3039 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3040 pos = it->current.pos;
3041
3042 if (before_p)
3043 DEC_TEXT_POS (pos, it->multibyte_p);
3044 else
3045 {
3046 if (it->what == IT_COMPOSITION)
3047 /* For composition, we must check the position after the
3048 composition. */
3049 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3050 else
3051 INC_TEXT_POS (pos, it->multibyte_p);
3052 }
3053
3054 /* Determine face for CHARSET_ASCII, or unibyte. */
3055 face_id = face_at_buffer_position (it->w,
3056 CHARPOS (pos),
3057 it->region_beg_charpos,
3058 it->region_end_charpos,
3059 &next_check_charpos,
3060 limit, 0);
3061
3062 /* Correct the face for charsets different from ASCII. Do it
3063 for the multibyte case only. The face returned above is
3064 suitable for unibyte text if current_buffer is unibyte. */
3065 if (it->multibyte_p)
3066 {
3067 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3068 struct face *face = FACE_FROM_ID (it->f, face_id);
3069 face_id = FACE_FOR_CHAR (it->f, face, c);
3070 }
3071 }
3072
3073 return face_id;
3074 }
3075
3076
3077 \f
3078 /***********************************************************************
3079 Invisible text
3080 ***********************************************************************/
3081
3082 /* Set up iterator IT from invisible properties at its current
3083 position. Called from handle_stop. */
3084
3085 static enum prop_handled
3086 handle_invisible_prop (it)
3087 struct it *it;
3088 {
3089 enum prop_handled handled = HANDLED_NORMALLY;
3090
3091 if (STRINGP (it->string))
3092 {
3093 extern Lisp_Object Qinvisible;
3094 Lisp_Object prop, end_charpos, limit, charpos;
3095
3096 /* Get the value of the invisible text property at the
3097 current position. Value will be nil if there is no such
3098 property. */
3099 charpos = make_number (IT_STRING_CHARPOS (*it));
3100 prop = Fget_text_property (charpos, Qinvisible, it->string);
3101
3102 if (!NILP (prop)
3103 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3104 {
3105 handled = HANDLED_RECOMPUTE_PROPS;
3106
3107 /* Get the position at which the next change of the
3108 invisible text property can be found in IT->string.
3109 Value will be nil if the property value is the same for
3110 all the rest of IT->string. */
3111 XSETINT (limit, SCHARS (it->string));
3112 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3113 it->string, limit);
3114
3115 /* Text at current position is invisible. The next
3116 change in the property is at position end_charpos.
3117 Move IT's current position to that position. */
3118 if (INTEGERP (end_charpos)
3119 && XFASTINT (end_charpos) < XFASTINT (limit))
3120 {
3121 struct text_pos old;
3122 old = it->current.string_pos;
3123 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3124 compute_string_pos (&it->current.string_pos, old, it->string);
3125 }
3126 else
3127 {
3128 /* The rest of the string is invisible. If this is an
3129 overlay string, proceed with the next overlay string
3130 or whatever comes and return a character from there. */
3131 if (it->current.overlay_string_index >= 0)
3132 {
3133 next_overlay_string (it);
3134 /* Don't check for overlay strings when we just
3135 finished processing them. */
3136 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3137 }
3138 else
3139 {
3140 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3141 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3142 }
3143 }
3144 }
3145 }
3146 else
3147 {
3148 int invis_p, newpos, next_stop, start_charpos;
3149 Lisp_Object pos, prop, overlay;
3150
3151 /* First of all, is there invisible text at this position? */
3152 start_charpos = IT_CHARPOS (*it);
3153 pos = make_number (IT_CHARPOS (*it));
3154 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3155 &overlay);
3156 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3157
3158 /* If we are on invisible text, skip over it. */
3159 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3160 {
3161 /* Record whether we have to display an ellipsis for the
3162 invisible text. */
3163 int display_ellipsis_p = invis_p == 2;
3164
3165 handled = HANDLED_RECOMPUTE_PROPS;
3166
3167 /* Loop skipping over invisible text. The loop is left at
3168 ZV or with IT on the first char being visible again. */
3169 do
3170 {
3171 /* Try to skip some invisible text. Return value is the
3172 position reached which can be equal to IT's position
3173 if there is nothing invisible here. This skips both
3174 over invisible text properties and overlays with
3175 invisible property. */
3176 newpos = skip_invisible (IT_CHARPOS (*it),
3177 &next_stop, ZV, it->window);
3178
3179 /* If we skipped nothing at all we weren't at invisible
3180 text in the first place. If everything to the end of
3181 the buffer was skipped, end the loop. */
3182 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3183 invis_p = 0;
3184 else
3185 {
3186 /* We skipped some characters but not necessarily
3187 all there are. Check if we ended up on visible
3188 text. Fget_char_property returns the property of
3189 the char before the given position, i.e. if we
3190 get invis_p = 0, this means that the char at
3191 newpos is visible. */
3192 pos = make_number (newpos);
3193 prop = Fget_char_property (pos, Qinvisible, it->window);
3194 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3195 }
3196
3197 /* If we ended up on invisible text, proceed to
3198 skip starting with next_stop. */
3199 if (invis_p)
3200 IT_CHARPOS (*it) = next_stop;
3201 }
3202 while (invis_p);
3203
3204 /* The position newpos is now either ZV or on visible text. */
3205 IT_CHARPOS (*it) = newpos;
3206 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3207
3208 /* If there are before-strings at the start of invisible
3209 text, and the text is invisible because of a text
3210 property, arrange to show before-strings because 20.x did
3211 it that way. (If the text is invisible because of an
3212 overlay property instead of a text property, this is
3213 already handled in the overlay code.) */
3214 if (NILP (overlay)
3215 && get_overlay_strings (it, start_charpos))
3216 {
3217 handled = HANDLED_RECOMPUTE_PROPS;
3218 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3219 }
3220 else if (display_ellipsis_p)
3221 setup_for_ellipsis (it);
3222 }
3223 }
3224
3225 return handled;
3226 }
3227
3228
3229 /* Make iterator IT return `...' next. */
3230
3231 static void
3232 setup_for_ellipsis (it)
3233 struct it *it;
3234 {
3235 if (it->dp
3236 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3237 {
3238 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3239 it->dpvec = v->contents;
3240 it->dpend = v->contents + v->size;
3241 }
3242 else
3243 {
3244 /* Default `...'. */
3245 it->dpvec = default_invis_vector;
3246 it->dpend = default_invis_vector + 3;
3247 }
3248
3249 /* The ellipsis display does not replace the display of the
3250 character at the new position. Indicate this by setting
3251 IT->dpvec_char_len to zero. */
3252 it->dpvec_char_len = 0;
3253
3254 it->current.dpvec_index = 0;
3255 it->method = next_element_from_display_vector;
3256 }
3257
3258
3259 \f
3260 /***********************************************************************
3261 'display' property
3262 ***********************************************************************/
3263
3264 /* Set up iterator IT from `display' property at its current position.
3265 Called from handle_stop. */
3266
3267 static enum prop_handled
3268 handle_display_prop (it)
3269 struct it *it;
3270 {
3271 Lisp_Object prop, object;
3272 struct text_pos *position;
3273 int display_replaced_p = 0;
3274
3275 if (STRINGP (it->string))
3276 {
3277 object = it->string;
3278 position = &it->current.string_pos;
3279 }
3280 else
3281 {
3282 object = it->w->buffer;
3283 position = &it->current.pos;
3284 }
3285
3286 /* Reset those iterator values set from display property values. */
3287 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3288 it->space_width = Qnil;
3289 it->font_height = Qnil;
3290 it->voffset = 0;
3291
3292 /* We don't support recursive `display' properties, i.e. string
3293 values that have a string `display' property, that have a string
3294 `display' property etc. */
3295 if (!it->string_from_display_prop_p)
3296 it->area = TEXT_AREA;
3297
3298 prop = Fget_char_property (make_number (position->charpos),
3299 Qdisplay, object);
3300 if (NILP (prop))
3301 return HANDLED_NORMALLY;
3302
3303 if (CONSP (prop)
3304 /* Simple properties. */
3305 && !EQ (XCAR (prop), Qimage)
3306 && !EQ (XCAR (prop), Qspace)
3307 && !EQ (XCAR (prop), Qwhen)
3308 && !EQ (XCAR (prop), Qslice)
3309 && !EQ (XCAR (prop), Qspace_width)
3310 && !EQ (XCAR (prop), Qheight)
3311 && !EQ (XCAR (prop), Qraise)
3312 /* Marginal area specifications. */
3313 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3314 && !EQ (XCAR (prop), Qleft_fringe)
3315 && !EQ (XCAR (prop), Qright_fringe)
3316 && !NILP (XCAR (prop)))
3317 {
3318 for (; CONSP (prop); prop = XCDR (prop))
3319 {
3320 if (handle_single_display_prop (it, XCAR (prop), object,
3321 position, display_replaced_p))
3322 display_replaced_p = 1;
3323 }
3324 }
3325 else if (VECTORP (prop))
3326 {
3327 int i;
3328 for (i = 0; i < ASIZE (prop); ++i)
3329 if (handle_single_display_prop (it, AREF (prop, i), object,
3330 position, display_replaced_p))
3331 display_replaced_p = 1;
3332 }
3333 else
3334 {
3335 if (handle_single_display_prop (it, prop, object, position, 0))
3336 display_replaced_p = 1;
3337 }
3338
3339 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3340 }
3341
3342
3343 /* Value is the position of the end of the `display' property starting
3344 at START_POS in OBJECT. */
3345
3346 static struct text_pos
3347 display_prop_end (it, object, start_pos)
3348 struct it *it;
3349 Lisp_Object object;
3350 struct text_pos start_pos;
3351 {
3352 Lisp_Object end;
3353 struct text_pos end_pos;
3354
3355 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3356 Qdisplay, object, Qnil);
3357 CHARPOS (end_pos) = XFASTINT (end);
3358 if (STRINGP (object))
3359 compute_string_pos (&end_pos, start_pos, it->string);
3360 else
3361 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3362
3363 return end_pos;
3364 }
3365
3366
3367 /* Set up IT from a single `display' sub-property value PROP. OBJECT
3368 is the object in which the `display' property was found. *POSITION
3369 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3370 means that we previously saw a display sub-property which already
3371 replaced text display with something else, for example an image;
3372 ignore such properties after the first one has been processed.
3373
3374 If PROP is a `space' or `image' sub-property, set *POSITION to the
3375 end position of the `display' property.
3376
3377 Value is non-zero if something was found which replaces the display
3378 of buffer or string text. */
3379
3380 static int
3381 handle_single_display_prop (it, prop, object, position,
3382 display_replaced_before_p)
3383 struct it *it;
3384 Lisp_Object prop;
3385 Lisp_Object object;
3386 struct text_pos *position;
3387 int display_replaced_before_p;
3388 {
3389 Lisp_Object value;
3390 int replaces_text_display_p = 0;
3391 Lisp_Object form;
3392
3393 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
3394 evaluated. If the result is nil, VALUE is ignored. */
3395 form = Qt;
3396 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3397 {
3398 prop = XCDR (prop);
3399 if (!CONSP (prop))
3400 return 0;
3401 form = XCAR (prop);
3402 prop = XCDR (prop);
3403 }
3404
3405 if (!NILP (form) && !EQ (form, Qt))
3406 {
3407 int count = SPECPDL_INDEX ();
3408 struct gcpro gcpro1;
3409
3410 /* Bind `object' to the object having the `display' property, a
3411 buffer or string. Bind `position' to the position in the
3412 object where the property was found, and `buffer-position'
3413 to the current position in the buffer. */
3414 specbind (Qobject, object);
3415 specbind (Qposition, make_number (CHARPOS (*position)));
3416 specbind (Qbuffer_position,
3417 make_number (STRINGP (object)
3418 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3419 GCPRO1 (form);
3420 form = safe_eval (form);
3421 UNGCPRO;
3422 unbind_to (count, Qnil);
3423 }
3424
3425 if (NILP (form))
3426 return 0;
3427
3428 if (CONSP (prop)
3429 && EQ (XCAR (prop), Qheight)
3430 && CONSP (XCDR (prop)))
3431 {
3432 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3433 return 0;
3434
3435 /* `(height HEIGHT)'. */
3436 it->font_height = XCAR (XCDR (prop));
3437 if (!NILP (it->font_height))
3438 {
3439 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3440 int new_height = -1;
3441
3442 if (CONSP (it->font_height)
3443 && (EQ (XCAR (it->font_height), Qplus)
3444 || EQ (XCAR (it->font_height), Qminus))
3445 && CONSP (XCDR (it->font_height))
3446 && INTEGERP (XCAR (XCDR (it->font_height))))
3447 {
3448 /* `(+ N)' or `(- N)' where N is an integer. */
3449 int steps = XINT (XCAR (XCDR (it->font_height)));
3450 if (EQ (XCAR (it->font_height), Qplus))
3451 steps = - steps;
3452 it->face_id = smaller_face (it->f, it->face_id, steps);
3453 }
3454 else if (FUNCTIONP (it->font_height))
3455 {
3456 /* Call function with current height as argument.
3457 Value is the new height. */
3458 Lisp_Object height;
3459 height = safe_call1 (it->font_height,
3460 face->lface[LFACE_HEIGHT_INDEX]);
3461 if (NUMBERP (height))
3462 new_height = XFLOATINT (height);
3463 }
3464 else if (NUMBERP (it->font_height))
3465 {
3466 /* Value is a multiple of the canonical char height. */
3467 struct face *face;
3468
3469 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3470 new_height = (XFLOATINT (it->font_height)
3471 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3472 }
3473 else
3474 {
3475 /* Evaluate IT->font_height with `height' bound to the
3476 current specified height to get the new height. */
3477 Lisp_Object value;
3478 int count = SPECPDL_INDEX ();
3479
3480 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3481 value = safe_eval (it->font_height);
3482 unbind_to (count, Qnil);
3483
3484 if (NUMBERP (value))
3485 new_height = XFLOATINT (value);
3486 }
3487
3488 if (new_height > 0)
3489 it->face_id = face_with_height (it->f, it->face_id, new_height);
3490 }
3491 }
3492 else if (CONSP (prop)
3493 && EQ (XCAR (prop), Qspace_width)
3494 && CONSP (XCDR (prop)))
3495 {
3496 /* `(space_width WIDTH)'. */
3497 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3498 return 0;
3499
3500 value = XCAR (XCDR (prop));
3501 if (NUMBERP (value) && XFLOATINT (value) > 0)
3502 it->space_width = value;
3503 }
3504 else if (CONSP (prop)
3505 && EQ (XCAR (prop), Qslice))
3506 {
3507 /* `(slice X Y WIDTH HEIGHT)'. */
3508 Lisp_Object tem;
3509
3510 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3511 return 0;
3512
3513 if (tem = XCDR (prop), CONSP (tem))
3514 {
3515 it->slice.x = XCAR (tem);
3516 if (tem = XCDR (tem), CONSP (tem))
3517 {
3518 it->slice.y = XCAR (tem);
3519 if (tem = XCDR (tem), CONSP (tem))
3520 {
3521 it->slice.width = XCAR (tem);
3522 if (tem = XCDR (tem), CONSP (tem))
3523 it->slice.height = XCAR (tem);
3524 }
3525 }
3526 }
3527 }
3528 else if (CONSP (prop)
3529 && EQ (XCAR (prop), Qraise)
3530 && CONSP (XCDR (prop)))
3531 {
3532 /* `(raise FACTOR)'. */
3533 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3534 return 0;
3535
3536 #ifdef HAVE_WINDOW_SYSTEM
3537 value = XCAR (XCDR (prop));
3538 if (NUMBERP (value))
3539 {
3540 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3541 it->voffset = - (XFLOATINT (value)
3542 * (FONT_HEIGHT (face->font)));
3543 }
3544 #endif /* HAVE_WINDOW_SYSTEM */
3545 }
3546 else if (!it->string_from_display_prop_p)
3547 {
3548 /* `((margin left-margin) VALUE)' or `((margin right-margin)
3549 VALUE) or `((margin nil) VALUE)' or VALUE. */
3550 Lisp_Object location, value;
3551 struct text_pos start_pos;
3552 int valid_p;
3553
3554 /* Characters having this form of property are not displayed, so
3555 we have to find the end of the property. */
3556 start_pos = *position;
3557 *position = display_prop_end (it, object, start_pos);
3558 value = Qnil;
3559
3560 /* Let's stop at the new position and assume that all
3561 text properties change there. */
3562 it->stop_charpos = position->charpos;
3563
3564 if (CONSP (prop)
3565 && (EQ (XCAR (prop), Qleft_fringe)
3566 || EQ (XCAR (prop), Qright_fringe))
3567 && CONSP (XCDR (prop)))
3568 {
3569 unsigned face_id = DEFAULT_FACE_ID;
3570
3571 /* Save current settings of IT so that we can restore them
3572 when we are finished with the glyph property value. */
3573
3574 /* `(left-fringe BITMAP FACE)'. */
3575 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3576 return 0;
3577
3578 #ifdef HAVE_WINDOW_SYSTEM
3579 value = XCAR (XCDR (prop));
3580 if (!NUMBERP (value)
3581 || !valid_fringe_bitmap_id_p (XINT (value)))
3582 return 0;
3583
3584 if (CONSP (XCDR (XCDR (prop))))
3585 {
3586 Lisp_Object face_name = XCAR (XCDR (XCDR (prop)));
3587
3588 face_id = lookup_named_face (it->f, face_name, 'A');
3589 if (face_id < 0)
3590 return 0;
3591 }
3592
3593 push_it (it);
3594
3595 it->area = TEXT_AREA;
3596 it->what = IT_IMAGE;
3597 it->image_id = -1; /* no image */
3598 it->position = start_pos;
3599 it->object = NILP (object) ? it->w->buffer : object;
3600 it->method = next_element_from_image;
3601 it->face_id = face_id;
3602
3603 /* Say that we haven't consumed the characters with
3604 `display' property yet. The call to pop_it in
3605 set_iterator_to_next will clean this up. */
3606 *position = start_pos;
3607
3608 if (EQ (XCAR (prop), Qleft_fringe))
3609 {
3610 it->left_user_fringe_bitmap = XINT (value);
3611 it->left_user_fringe_face_id = face_id;
3612 }
3613 else
3614 {
3615 it->right_user_fringe_bitmap = XINT (value);
3616 it->right_user_fringe_face_id = face_id;
3617 }
3618 #endif /* HAVE_WINDOW_SYSTEM */
3619 return 1;
3620 }
3621
3622 location = Qunbound;
3623 if (CONSP (prop) && CONSP (XCAR (prop)))
3624 {
3625 Lisp_Object tem;
3626
3627 value = XCDR (prop);
3628 if (CONSP (value))
3629 value = XCAR (value);
3630
3631 tem = XCAR (prop);
3632 if (EQ (XCAR (tem), Qmargin)
3633 && (tem = XCDR (tem),
3634 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3635 (NILP (tem)
3636 || EQ (tem, Qleft_margin)
3637 || EQ (tem, Qright_margin))))
3638 location = tem;
3639 }
3640
3641 if (EQ (location, Qunbound))
3642 {
3643 location = Qnil;
3644 value = prop;
3645 }
3646
3647 valid_p = (STRINGP (value)
3648 #ifdef HAVE_WINDOW_SYSTEM
3649 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
3650 #endif /* not HAVE_WINDOW_SYSTEM */
3651 || (CONSP (value) && EQ (XCAR (value), Qspace)));
3652
3653 if ((EQ (location, Qleft_margin)
3654 || EQ (location, Qright_margin)
3655 || NILP (location))
3656 && valid_p
3657 && !display_replaced_before_p)
3658 {
3659 replaces_text_display_p = 1;
3660
3661 /* Save current settings of IT so that we can restore them
3662 when we are finished with the glyph property value. */
3663 push_it (it);
3664
3665 if (NILP (location))
3666 it->area = TEXT_AREA;
3667 else if (EQ (location, Qleft_margin))
3668 it->area = LEFT_MARGIN_AREA;
3669 else
3670 it->area = RIGHT_MARGIN_AREA;
3671
3672 if (STRINGP (value))
3673 {
3674 it->string = value;
3675 it->multibyte_p = STRING_MULTIBYTE (it->string);
3676 it->current.overlay_string_index = -1;
3677 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3678 it->end_charpos = it->string_nchars = SCHARS (it->string);
3679 it->method = next_element_from_string;
3680 it->stop_charpos = 0;
3681 it->string_from_display_prop_p = 1;
3682 /* Say that we haven't consumed the characters with
3683 `display' property yet. The call to pop_it in
3684 set_iterator_to_next will clean this up. */
3685 *position = start_pos;
3686 }
3687 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3688 {
3689 it->method = next_element_from_stretch;
3690 it->object = value;
3691 it->current.pos = it->position = start_pos;
3692 }
3693 #ifdef HAVE_WINDOW_SYSTEM
3694 else
3695 {
3696 it->what = IT_IMAGE;
3697 it->image_id = lookup_image (it->f, value);
3698 it->position = start_pos;
3699 it->object = NILP (object) ? it->w->buffer : object;
3700 it->method = next_element_from_image;
3701
3702 /* Say that we haven't consumed the characters with
3703 `display' property yet. The call to pop_it in
3704 set_iterator_to_next will clean this up. */
3705 *position = start_pos;
3706 }
3707 #endif /* HAVE_WINDOW_SYSTEM */
3708 }
3709 else
3710 /* Invalid property or property not supported. Restore
3711 the position to what it was before. */
3712 *position = start_pos;
3713 }
3714
3715 return replaces_text_display_p;
3716 }
3717
3718
3719 /* Check if PROP is a display sub-property value whose text should be
3720 treated as intangible. */
3721
3722 static int
3723 single_display_prop_intangible_p (prop)
3724 Lisp_Object prop;
3725 {
3726 /* Skip over `when FORM'. */
3727 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3728 {
3729 prop = XCDR (prop);
3730 if (!CONSP (prop))
3731 return 0;
3732 prop = XCDR (prop);
3733 }
3734
3735 if (STRINGP (prop))
3736 return 1;
3737
3738 if (!CONSP (prop))
3739 return 0;
3740
3741 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3742 we don't need to treat text as intangible. */
3743 if (EQ (XCAR (prop), Qmargin))
3744 {
3745 prop = XCDR (prop);
3746 if (!CONSP (prop))
3747 return 0;
3748
3749 prop = XCDR (prop);
3750 if (!CONSP (prop)
3751 || EQ (XCAR (prop), Qleft_margin)
3752 || EQ (XCAR (prop), Qright_margin))
3753 return 0;
3754 }
3755
3756 return (CONSP (prop)
3757 && (EQ (XCAR (prop), Qimage)
3758 || EQ (XCAR (prop), Qspace)));
3759 }
3760
3761
3762 /* Check if PROP is a display property value whose text should be
3763 treated as intangible. */
3764
3765 int
3766 display_prop_intangible_p (prop)
3767 Lisp_Object prop;
3768 {
3769 if (CONSP (prop)
3770 && CONSP (XCAR (prop))
3771 && !EQ (Qmargin, XCAR (XCAR (prop))))
3772 {
3773 /* A list of sub-properties. */
3774 while (CONSP (prop))
3775 {
3776 if (single_display_prop_intangible_p (XCAR (prop)))
3777 return 1;
3778 prop = XCDR (prop);
3779 }
3780 }
3781 else if (VECTORP (prop))
3782 {
3783 /* A vector of sub-properties. */
3784 int i;
3785 for (i = 0; i < ASIZE (prop); ++i)
3786 if (single_display_prop_intangible_p (AREF (prop, i)))
3787 return 1;
3788 }
3789 else
3790 return single_display_prop_intangible_p (prop);
3791
3792 return 0;
3793 }
3794
3795
3796 /* Return 1 if PROP is a display sub-property value containing STRING. */
3797
3798 static int
3799 single_display_prop_string_p (prop, string)
3800 Lisp_Object prop, string;
3801 {
3802 if (EQ (string, prop))
3803 return 1;
3804
3805 /* Skip over `when FORM'. */
3806 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3807 {
3808 prop = XCDR (prop);
3809 if (!CONSP (prop))
3810 return 0;
3811 prop = XCDR (prop);
3812 }
3813
3814 if (CONSP (prop))
3815 /* Skip over `margin LOCATION'. */
3816 if (EQ (XCAR (prop), Qmargin))
3817 {
3818 prop = XCDR (prop);
3819 if (!CONSP (prop))
3820 return 0;
3821
3822 prop = XCDR (prop);
3823 if (!CONSP (prop))
3824 return 0;
3825 }
3826
3827 return CONSP (prop) && EQ (XCAR (prop), string);
3828 }
3829
3830
3831 /* Return 1 if STRING appears in the `display' property PROP. */
3832
3833 static int
3834 display_prop_string_p (prop, string)
3835 Lisp_Object prop, string;
3836 {
3837 if (CONSP (prop)
3838 && CONSP (XCAR (prop))
3839 && !EQ (Qmargin, XCAR (XCAR (prop))))
3840 {
3841 /* A list of sub-properties. */
3842 while (CONSP (prop))
3843 {
3844 if (single_display_prop_string_p (XCAR (prop), string))
3845 return 1;
3846 prop = XCDR (prop);
3847 }
3848 }
3849 else if (VECTORP (prop))
3850 {
3851 /* A vector of sub-properties. */
3852 int i;
3853 for (i = 0; i < ASIZE (prop); ++i)
3854 if (single_display_prop_string_p (AREF (prop, i), string))
3855 return 1;
3856 }
3857 else
3858 return single_display_prop_string_p (prop, string);
3859
3860 return 0;
3861 }
3862
3863
3864 /* Determine from which buffer position in W's buffer STRING comes
3865 from. AROUND_CHARPOS is an approximate position where it could
3866 be from. Value is the buffer position or 0 if it couldn't be
3867 determined.
3868
3869 W's buffer must be current.
3870
3871 This function is necessary because we don't record buffer positions
3872 in glyphs generated from strings (to keep struct glyph small).
3873 This function may only use code that doesn't eval because it is
3874 called asynchronously from note_mouse_highlight. */
3875
3876 int
3877 string_buffer_position (w, string, around_charpos)
3878 struct window *w;
3879 Lisp_Object string;
3880 int around_charpos;
3881 {
3882 Lisp_Object limit, prop, pos;
3883 const int MAX_DISTANCE = 1000;
3884 int found = 0;
3885
3886 pos = make_number (around_charpos);
3887 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3888 while (!found && !EQ (pos, limit))
3889 {
3890 prop = Fget_char_property (pos, Qdisplay, Qnil);
3891 if (!NILP (prop) && display_prop_string_p (prop, string))
3892 found = 1;
3893 else
3894 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3895 }
3896
3897 if (!found)
3898 {
3899 pos = make_number (around_charpos);
3900 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3901 while (!found && !EQ (pos, limit))
3902 {
3903 prop = Fget_char_property (pos, Qdisplay, Qnil);
3904 if (!NILP (prop) && display_prop_string_p (prop, string))
3905 found = 1;
3906 else
3907 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3908 limit);
3909 }
3910 }
3911
3912 return found ? XINT (pos) : 0;
3913 }
3914
3915
3916 \f
3917 /***********************************************************************
3918 `composition' property
3919 ***********************************************************************/
3920
3921 /* Set up iterator IT from `composition' property at its current
3922 position. Called from handle_stop. */
3923
3924 static enum prop_handled
3925 handle_composition_prop (it)
3926 struct it *it;
3927 {
3928 Lisp_Object prop, string;
3929 int pos, pos_byte, end;
3930 enum prop_handled handled = HANDLED_NORMALLY;
3931
3932 if (STRINGP (it->string))
3933 {
3934 pos = IT_STRING_CHARPOS (*it);
3935 pos_byte = IT_STRING_BYTEPOS (*it);
3936 string = it->string;
3937 }
3938 else
3939 {
3940 pos = IT_CHARPOS (*it);
3941 pos_byte = IT_BYTEPOS (*it);
3942 string = Qnil;
3943 }
3944
3945 /* If there's a valid composition and point is not inside of the
3946 composition (in the case that the composition is from the current
3947 buffer), draw a glyph composed from the composition components. */
3948 if (find_composition (pos, -1, &pos, &end, &prop, string)
3949 && COMPOSITION_VALID_P (pos, end, prop)
3950 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3951 {
3952 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3953
3954 if (id >= 0)
3955 {
3956 it->method = next_element_from_composition;
3957 it->cmp_id = id;
3958 it->cmp_len = COMPOSITION_LENGTH (prop);
3959 /* For a terminal, draw only the first character of the
3960 components. */
3961 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3962 it->len = (STRINGP (it->string)
3963 ? string_char_to_byte (it->string, end)
3964 : CHAR_TO_BYTE (end)) - pos_byte;
3965 it->stop_charpos = end;
3966 handled = HANDLED_RETURN;
3967 }
3968 }
3969
3970 return handled;
3971 }
3972
3973
3974 \f
3975 /***********************************************************************
3976 Overlay strings
3977 ***********************************************************************/
3978
3979 /* The following structure is used to record overlay strings for
3980 later sorting in load_overlay_strings. */
3981
3982 struct overlay_entry
3983 {
3984 Lisp_Object overlay;
3985 Lisp_Object string;
3986 int priority;
3987 int after_string_p;
3988 };
3989
3990
3991 /* Set up iterator IT from overlay strings at its current position.
3992 Called from handle_stop. */
3993
3994 static enum prop_handled
3995 handle_overlay_change (it)
3996 struct it *it;
3997 {
3998 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3999 return HANDLED_RECOMPUTE_PROPS;
4000 else
4001 return HANDLED_NORMALLY;
4002 }
4003
4004
4005 /* Set up the next overlay string for delivery by IT, if there is an
4006 overlay string to deliver. Called by set_iterator_to_next when the
4007 end of the current overlay string is reached. If there are more
4008 overlay strings to display, IT->string and
4009 IT->current.overlay_string_index are set appropriately here.
4010 Otherwise IT->string is set to nil. */
4011
4012 static void
4013 next_overlay_string (it)
4014 struct it *it;
4015 {
4016 ++it->current.overlay_string_index;
4017 if (it->current.overlay_string_index == it->n_overlay_strings)
4018 {
4019 /* No more overlay strings. Restore IT's settings to what
4020 they were before overlay strings were processed, and
4021 continue to deliver from current_buffer. */
4022 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4023
4024 pop_it (it);
4025 xassert (it->stop_charpos >= BEGV
4026 && it->stop_charpos <= it->end_charpos);
4027 it->string = Qnil;
4028 it->current.overlay_string_index = -1;
4029 SET_TEXT_POS (it->current.string_pos, -1, -1);
4030 it->n_overlay_strings = 0;
4031 it->method = next_element_from_buffer;
4032
4033 /* If we're at the end of the buffer, record that we have
4034 processed the overlay strings there already, so that
4035 next_element_from_buffer doesn't try it again. */
4036 if (IT_CHARPOS (*it) >= it->end_charpos)
4037 it->overlay_strings_at_end_processed_p = 1;
4038
4039 /* If we have to display `...' for invisible text, set
4040 the iterator up for that. */
4041 if (display_ellipsis_p)
4042 setup_for_ellipsis (it);
4043 }
4044 else
4045 {
4046 /* There are more overlay strings to process. If
4047 IT->current.overlay_string_index has advanced to a position
4048 where we must load IT->overlay_strings with more strings, do
4049 it. */
4050 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4051
4052 if (it->current.overlay_string_index && i == 0)
4053 load_overlay_strings (it, 0);
4054
4055 /* Initialize IT to deliver display elements from the overlay
4056 string. */
4057 it->string = it->overlay_strings[i];
4058 it->multibyte_p = STRING_MULTIBYTE (it->string);
4059 SET_TEXT_POS (it->current.string_pos, 0, 0);
4060 it->method = next_element_from_string;
4061 it->stop_charpos = 0;
4062 }
4063
4064 CHECK_IT (it);
4065 }
4066
4067
4068 /* Compare two overlay_entry structures E1 and E2. Used as a
4069 comparison function for qsort in load_overlay_strings. Overlay
4070 strings for the same position are sorted so that
4071
4072 1. All after-strings come in front of before-strings, except
4073 when they come from the same overlay.
4074
4075 2. Within after-strings, strings are sorted so that overlay strings
4076 from overlays with higher priorities come first.
4077
4078 2. Within before-strings, strings are sorted so that overlay
4079 strings from overlays with higher priorities come last.
4080
4081 Value is analogous to strcmp. */
4082
4083
4084 static int
4085 compare_overlay_entries (e1, e2)
4086 void *e1, *e2;
4087 {
4088 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4089 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4090 int result;
4091
4092 if (entry1->after_string_p != entry2->after_string_p)
4093 {
4094 /* Let after-strings appear in front of before-strings if
4095 they come from different overlays. */
4096 if (EQ (entry1->overlay, entry2->overlay))
4097 result = entry1->after_string_p ? 1 : -1;
4098 else
4099 result = entry1->after_string_p ? -1 : 1;
4100 }
4101 else if (entry1->after_string_p)
4102 /* After-strings sorted in order of decreasing priority. */
4103 result = entry2->priority - entry1->priority;
4104 else
4105 /* Before-strings sorted in order of increasing priority. */
4106 result = entry1->priority - entry2->priority;
4107
4108 return result;
4109 }
4110
4111
4112 /* Load the vector IT->overlay_strings with overlay strings from IT's
4113 current buffer position, or from CHARPOS if that is > 0. Set
4114 IT->n_overlays to the total number of overlay strings found.
4115
4116 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4117 a time. On entry into load_overlay_strings,
4118 IT->current.overlay_string_index gives the number of overlay
4119 strings that have already been loaded by previous calls to this
4120 function.
4121
4122 IT->add_overlay_start contains an additional overlay start
4123 position to consider for taking overlay strings from, if non-zero.
4124 This position comes into play when the overlay has an `invisible'
4125 property, and both before and after-strings. When we've skipped to
4126 the end of the overlay, because of its `invisible' property, we
4127 nevertheless want its before-string to appear.
4128 IT->add_overlay_start will contain the overlay start position
4129 in this case.
4130
4131 Overlay strings are sorted so that after-string strings come in
4132 front of before-string strings. Within before and after-strings,
4133 strings are sorted by overlay priority. See also function
4134 compare_overlay_entries. */
4135
4136 static void
4137 load_overlay_strings (it, charpos)
4138 struct it *it;
4139 int charpos;
4140 {
4141 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4142 Lisp_Object overlay, window, str, invisible;
4143 struct Lisp_Overlay *ov;
4144 int start, end;
4145 int size = 20;
4146 int n = 0, i, j, invis_p;
4147 struct overlay_entry *entries
4148 = (struct overlay_entry *) alloca (size * sizeof *entries);
4149
4150 if (charpos <= 0)
4151 charpos = IT_CHARPOS (*it);
4152
4153 /* Append the overlay string STRING of overlay OVERLAY to vector
4154 `entries' which has size `size' and currently contains `n'
4155 elements. AFTER_P non-zero means STRING is an after-string of
4156 OVERLAY. */
4157 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4158 do \
4159 { \
4160 Lisp_Object priority; \
4161 \
4162 if (n == size) \
4163 { \
4164 int new_size = 2 * size; \
4165 struct overlay_entry *old = entries; \
4166 entries = \
4167 (struct overlay_entry *) alloca (new_size \
4168 * sizeof *entries); \
4169 bcopy (old, entries, size * sizeof *entries); \
4170 size = new_size; \
4171 } \
4172 \
4173 entries[n].string = (STRING); \
4174 entries[n].overlay = (OVERLAY); \
4175 priority = Foverlay_get ((OVERLAY), Qpriority); \
4176 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4177 entries[n].after_string_p = (AFTER_P); \
4178 ++n; \
4179 } \
4180 while (0)
4181
4182 /* Process overlay before the overlay center. */
4183 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4184 {
4185 XSETMISC (overlay, ov);
4186 xassert (OVERLAYP (overlay));
4187 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4188 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4189
4190 if (end < charpos)
4191 break;
4192
4193 /* Skip this overlay if it doesn't start or end at IT's current
4194 position. */
4195 if (end != charpos && start != charpos)
4196 continue;
4197
4198 /* Skip this overlay if it doesn't apply to IT->w. */
4199 window = Foverlay_get (overlay, Qwindow);
4200 if (WINDOWP (window) && XWINDOW (window) != it->w)
4201 continue;
4202
4203 /* If the text ``under'' the overlay is invisible, both before-
4204 and after-strings from this overlay are visible; start and
4205 end position are indistinguishable. */
4206 invisible = Foverlay_get (overlay, Qinvisible);
4207 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4208
4209 /* If overlay has a non-empty before-string, record it. */
4210 if ((start == charpos || (end == charpos && invis_p))
4211 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4212 && SCHARS (str))
4213 RECORD_OVERLAY_STRING (overlay, str, 0);
4214
4215 /* If overlay has a non-empty after-string, record it. */
4216 if ((end == charpos || (start == charpos && invis_p))
4217 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4218 && SCHARS (str))
4219 RECORD_OVERLAY_STRING (overlay, str, 1);
4220 }
4221
4222 /* Process overlays after the overlay center. */
4223 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4224 {
4225 XSETMISC (overlay, ov);
4226 xassert (OVERLAYP (overlay));
4227 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4228 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4229
4230 if (start > charpos)
4231 break;
4232
4233 /* Skip this overlay if it doesn't start or end at IT's current
4234 position. */
4235 if (end != charpos && start != charpos)
4236 continue;
4237
4238 /* Skip this overlay if it doesn't apply to IT->w. */
4239 window = Foverlay_get (overlay, Qwindow);
4240 if (WINDOWP (window) && XWINDOW (window) != it->w)
4241 continue;
4242
4243 /* If the text ``under'' the overlay is invisible, it has a zero
4244 dimension, and both before- and after-strings apply. */
4245 invisible = Foverlay_get (overlay, Qinvisible);
4246 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4247
4248 /* If overlay has a non-empty before-string, record it. */
4249 if ((start == charpos || (end == charpos && invis_p))
4250 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4251 && SCHARS (str))
4252 RECORD_OVERLAY_STRING (overlay, str, 0);
4253
4254 /* If overlay has a non-empty after-string, record it. */
4255 if ((end == charpos || (start == charpos && invis_p))
4256 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4257 && SCHARS (str))
4258 RECORD_OVERLAY_STRING (overlay, str, 1);
4259 }
4260
4261 #undef RECORD_OVERLAY_STRING
4262
4263 /* Sort entries. */
4264 if (n > 1)
4265 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4266
4267 /* Record the total number of strings to process. */
4268 it->n_overlay_strings = n;
4269
4270 /* IT->current.overlay_string_index is the number of overlay strings
4271 that have already been consumed by IT. Copy some of the
4272 remaining overlay strings to IT->overlay_strings. */
4273 i = 0;
4274 j = it->current.overlay_string_index;
4275 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4276 it->overlay_strings[i++] = entries[j++].string;
4277
4278 CHECK_IT (it);
4279 }
4280
4281
4282 /* Get the first chunk of overlay strings at IT's current buffer
4283 position, or at CHARPOS if that is > 0. Value is non-zero if at
4284 least one overlay string was found. */
4285
4286 static int
4287 get_overlay_strings (it, charpos)
4288 struct it *it;
4289 int charpos;
4290 {
4291 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4292 process. This fills IT->overlay_strings with strings, and sets
4293 IT->n_overlay_strings to the total number of strings to process.
4294 IT->pos.overlay_string_index has to be set temporarily to zero
4295 because load_overlay_strings needs this; it must be set to -1
4296 when no overlay strings are found because a zero value would
4297 indicate a position in the first overlay string. */
4298 it->current.overlay_string_index = 0;
4299 load_overlay_strings (it, charpos);
4300
4301 /* If we found overlay strings, set up IT to deliver display
4302 elements from the first one. Otherwise set up IT to deliver
4303 from current_buffer. */
4304 if (it->n_overlay_strings)
4305 {
4306 /* Make sure we know settings in current_buffer, so that we can
4307 restore meaningful values when we're done with the overlay
4308 strings. */
4309 compute_stop_pos (it);
4310 xassert (it->face_id >= 0);
4311
4312 /* Save IT's settings. They are restored after all overlay
4313 strings have been processed. */
4314 xassert (it->sp == 0);
4315 push_it (it);
4316
4317 /* Set up IT to deliver display elements from the first overlay
4318 string. */
4319 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4320 it->string = it->overlay_strings[0];
4321 it->stop_charpos = 0;
4322 xassert (STRINGP (it->string));
4323 it->end_charpos = SCHARS (it->string);
4324 it->multibyte_p = STRING_MULTIBYTE (it->string);
4325 it->method = next_element_from_string;
4326 }
4327 else
4328 {
4329 it->string = Qnil;
4330 it->current.overlay_string_index = -1;
4331 it->method = next_element_from_buffer;
4332 }
4333
4334 CHECK_IT (it);
4335
4336 /* Value is non-zero if we found at least one overlay string. */
4337 return STRINGP (it->string);
4338 }
4339
4340
4341 \f
4342 /***********************************************************************
4343 Saving and restoring state
4344 ***********************************************************************/
4345
4346 /* Save current settings of IT on IT->stack. Called, for example,
4347 before setting up IT for an overlay string, to be able to restore
4348 IT's settings to what they were after the overlay string has been
4349 processed. */
4350
4351 static void
4352 push_it (it)
4353 struct it *it;
4354 {
4355 struct iterator_stack_entry *p;
4356
4357 xassert (it->sp < 2);
4358 p = it->stack + it->sp;
4359
4360 p->stop_charpos = it->stop_charpos;
4361 xassert (it->face_id >= 0);
4362 p->face_id = it->face_id;
4363 p->string = it->string;
4364 p->pos = it->current;
4365 p->end_charpos = it->end_charpos;
4366 p->string_nchars = it->string_nchars;
4367 p->area = it->area;
4368 p->multibyte_p = it->multibyte_p;
4369 p->slice = it->slice;
4370 p->space_width = it->space_width;
4371 p->font_height = it->font_height;
4372 p->voffset = it->voffset;
4373 p->string_from_display_prop_p = it->string_from_display_prop_p;
4374 p->display_ellipsis_p = 0;
4375 ++it->sp;
4376 }
4377
4378
4379 /* Restore IT's settings from IT->stack. Called, for example, when no
4380 more overlay strings must be processed, and we return to delivering
4381 display elements from a buffer, or when the end of a string from a
4382 `display' property is reached and we return to delivering display
4383 elements from an overlay string, or from a buffer. */
4384
4385 static void
4386 pop_it (it)
4387 struct it *it;
4388 {
4389 struct iterator_stack_entry *p;
4390
4391 xassert (it->sp > 0);
4392 --it->sp;
4393 p = it->stack + it->sp;
4394 it->stop_charpos = p->stop_charpos;
4395 it->face_id = p->face_id;
4396 it->string = p->string;
4397 it->current = p->pos;
4398 it->end_charpos = p->end_charpos;
4399 it->string_nchars = p->string_nchars;
4400 it->area = p->area;
4401 it->multibyte_p = p->multibyte_p;
4402 it->slice = p->slice;
4403 it->space_width = p->space_width;
4404 it->font_height = p->font_height;
4405 it->voffset = p->voffset;
4406 it->string_from_display_prop_p = p->string_from_display_prop_p;
4407 }
4408
4409
4410 \f
4411 /***********************************************************************
4412 Moving over lines
4413 ***********************************************************************/
4414
4415 /* Set IT's current position to the previous line start. */
4416
4417 static void
4418 back_to_previous_line_start (it)
4419 struct it *it;
4420 {
4421 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4422 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4423 }
4424
4425
4426 /* Move IT to the next line start.
4427
4428 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4429 we skipped over part of the text (as opposed to moving the iterator
4430 continuously over the text). Otherwise, don't change the value
4431 of *SKIPPED_P.
4432
4433 Newlines may come from buffer text, overlay strings, or strings
4434 displayed via the `display' property. That's the reason we can't
4435 simply use find_next_newline_no_quit.
4436
4437 Note that this function may not skip over invisible text that is so
4438 because of text properties and immediately follows a newline. If
4439 it would, function reseat_at_next_visible_line_start, when called
4440 from set_iterator_to_next, would effectively make invisible
4441 characters following a newline part of the wrong glyph row, which
4442 leads to wrong cursor motion. */
4443
4444 static int
4445 forward_to_next_line_start (it, skipped_p)
4446 struct it *it;
4447 int *skipped_p;
4448 {
4449 int old_selective, newline_found_p, n;
4450 const int MAX_NEWLINE_DISTANCE = 500;
4451
4452 /* If already on a newline, just consume it to avoid unintended
4453 skipping over invisible text below. */
4454 if (it->what == IT_CHARACTER
4455 && it->c == '\n'
4456 && CHARPOS (it->position) == IT_CHARPOS (*it))
4457 {
4458 set_iterator_to_next (it, 0);
4459 it->c = 0;
4460 return 1;
4461 }
4462
4463 /* Don't handle selective display in the following. It's (a)
4464 unnecessary because it's done by the caller, and (b) leads to an
4465 infinite recursion because next_element_from_ellipsis indirectly
4466 calls this function. */
4467 old_selective = it->selective;
4468 it->selective = 0;
4469
4470 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4471 from buffer text. */
4472 for (n = newline_found_p = 0;
4473 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4474 n += STRINGP (it->string) ? 0 : 1)
4475 {
4476 if (!get_next_display_element (it))
4477 return 0;
4478 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4479 set_iterator_to_next (it, 0);
4480 }
4481
4482 /* If we didn't find a newline near enough, see if we can use a
4483 short-cut. */
4484 if (!newline_found_p)
4485 {
4486 int start = IT_CHARPOS (*it);
4487 int limit = find_next_newline_no_quit (start, 1);
4488 Lisp_Object pos;
4489
4490 xassert (!STRINGP (it->string));
4491
4492 /* If there isn't any `display' property in sight, and no
4493 overlays, we can just use the position of the newline in
4494 buffer text. */
4495 if (it->stop_charpos >= limit
4496 || ((pos = Fnext_single_property_change (make_number (start),
4497 Qdisplay,
4498 Qnil, make_number (limit)),
4499 NILP (pos))
4500 && next_overlay_change (start) == ZV))
4501 {
4502 IT_CHARPOS (*it) = limit;
4503 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4504 *skipped_p = newline_found_p = 1;
4505 }
4506 else
4507 {
4508 while (get_next_display_element (it)
4509 && !newline_found_p)
4510 {
4511 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4512 set_iterator_to_next (it, 0);
4513 }
4514 }
4515 }
4516
4517 it->selective = old_selective;
4518 return newline_found_p;
4519 }
4520
4521
4522 /* Set IT's current position to the previous visible line start. Skip
4523 invisible text that is so either due to text properties or due to
4524 selective display. Caution: this does not change IT->current_x and
4525 IT->hpos. */
4526
4527 static void
4528 back_to_previous_visible_line_start (it)
4529 struct it *it;
4530 {
4531 int visible_p = 0;
4532
4533 /* Go back one newline if not on BEGV already. */
4534 if (IT_CHARPOS (*it) > BEGV)
4535 back_to_previous_line_start (it);
4536
4537 /* Move over lines that are invisible because of selective display
4538 or text properties. */
4539 while (IT_CHARPOS (*it) > BEGV
4540 && !visible_p)
4541 {
4542 visible_p = 1;
4543
4544 /* If selective > 0, then lines indented more than that values
4545 are invisible. */
4546 if (it->selective > 0
4547 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4548 (double) it->selective)) /* iftc */
4549 visible_p = 0;
4550 else
4551 {
4552 Lisp_Object prop;
4553
4554 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
4555 Qinvisible, it->window);
4556 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4557 visible_p = 0;
4558 }
4559
4560 if (visible_p)
4561 {
4562 struct it it2 = *it;
4563
4564 if (handle_display_prop (&it2) == HANDLED_RETURN)
4565 visible_p = 0;
4566 }
4567
4568 /* Back one more newline if the current one is invisible. */
4569 if (!visible_p)
4570 back_to_previous_line_start (it);
4571 }
4572
4573 xassert (IT_CHARPOS (*it) >= BEGV);
4574 xassert (IT_CHARPOS (*it) == BEGV
4575 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4576 CHECK_IT (it);
4577 }
4578
4579
4580 /* Reseat iterator IT at the previous visible line start. Skip
4581 invisible text that is so either due to text properties or due to
4582 selective display. At the end, update IT's overlay information,
4583 face information etc. */
4584
4585 static void
4586 reseat_at_previous_visible_line_start (it)
4587 struct it *it;
4588 {
4589 back_to_previous_visible_line_start (it);
4590 reseat (it, it->current.pos, 1);
4591 CHECK_IT (it);
4592 }
4593
4594
4595 /* Reseat iterator IT on the next visible line start in the current
4596 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4597 preceding the line start. Skip over invisible text that is so
4598 because of selective display. Compute faces, overlays etc at the
4599 new position. Note that this function does not skip over text that
4600 is invisible because of text properties. */
4601
4602 static void
4603 reseat_at_next_visible_line_start (it, on_newline_p)
4604 struct it *it;
4605 int on_newline_p;
4606 {
4607 int newline_found_p, skipped_p = 0;
4608
4609 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4610
4611 /* Skip over lines that are invisible because they are indented
4612 more than the value of IT->selective. */
4613 if (it->selective > 0)
4614 while (IT_CHARPOS (*it) < ZV
4615 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4616 (double) it->selective)) /* iftc */
4617 {
4618 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4619 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4620 }
4621
4622 /* Position on the newline if that's what's requested. */
4623 if (on_newline_p && newline_found_p)
4624 {
4625 if (STRINGP (it->string))
4626 {
4627 if (IT_STRING_CHARPOS (*it) > 0)
4628 {
4629 --IT_STRING_CHARPOS (*it);
4630 --IT_STRING_BYTEPOS (*it);
4631 }
4632 }
4633 else if (IT_CHARPOS (*it) > BEGV)
4634 {
4635 --IT_CHARPOS (*it);
4636 --IT_BYTEPOS (*it);
4637 reseat (it, it->current.pos, 0);
4638 }
4639 }
4640 else if (skipped_p)
4641 reseat (it, it->current.pos, 0);
4642
4643 CHECK_IT (it);
4644 }
4645
4646
4647 \f
4648 /***********************************************************************
4649 Changing an iterator's position
4650 ***********************************************************************/
4651
4652 /* Change IT's current position to POS in current_buffer. If FORCE_P
4653 is non-zero, always check for text properties at the new position.
4654 Otherwise, text properties are only looked up if POS >=
4655 IT->check_charpos of a property. */
4656
4657 static void
4658 reseat (it, pos, force_p)
4659 struct it *it;
4660 struct text_pos pos;
4661 int force_p;
4662 {
4663 int original_pos = IT_CHARPOS (*it);
4664
4665 reseat_1 (it, pos, 0);
4666
4667 /* Determine where to check text properties. Avoid doing it
4668 where possible because text property lookup is very expensive. */
4669 if (force_p
4670 || CHARPOS (pos) > it->stop_charpos
4671 || CHARPOS (pos) < original_pos)
4672 handle_stop (it);
4673
4674 CHECK_IT (it);
4675 }
4676
4677
4678 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4679 IT->stop_pos to POS, also. */
4680
4681 static void
4682 reseat_1 (it, pos, set_stop_p)
4683 struct it *it;
4684 struct text_pos pos;
4685 int set_stop_p;
4686 {
4687 /* Don't call this function when scanning a C string. */
4688 xassert (it->s == NULL);
4689
4690 /* POS must be a reasonable value. */
4691 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4692
4693 it->current.pos = it->position = pos;
4694 XSETBUFFER (it->object, current_buffer);
4695 it->end_charpos = ZV;
4696 it->dpvec = NULL;
4697 it->current.dpvec_index = -1;
4698 it->current.overlay_string_index = -1;
4699 IT_STRING_CHARPOS (*it) = -1;
4700 IT_STRING_BYTEPOS (*it) = -1;
4701 it->string = Qnil;
4702 it->method = next_element_from_buffer;
4703 /* RMS: I added this to fix a bug in move_it_vertically_backward
4704 where it->area continued to relate to the starting point
4705 for the backward motion. Bug report from
4706 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4707 However, I am not sure whether reseat still does the right thing
4708 in general after this change. */
4709 it->area = TEXT_AREA;
4710 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4711 it->sp = 0;
4712 it->face_before_selective_p = 0;
4713
4714 if (set_stop_p)
4715 it->stop_charpos = CHARPOS (pos);
4716 }
4717
4718
4719 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4720 If S is non-null, it is a C string to iterate over. Otherwise,
4721 STRING gives a Lisp string to iterate over.
4722
4723 If PRECISION > 0, don't return more then PRECISION number of
4724 characters from the string.
4725
4726 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4727 characters have been returned. FIELD_WIDTH < 0 means an infinite
4728 field width.
4729
4730 MULTIBYTE = 0 means disable processing of multibyte characters,
4731 MULTIBYTE > 0 means enable it,
4732 MULTIBYTE < 0 means use IT->multibyte_p.
4733
4734 IT must be initialized via a prior call to init_iterator before
4735 calling this function. */
4736
4737 static void
4738 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4739 struct it *it;
4740 unsigned char *s;
4741 Lisp_Object string;
4742 int charpos;
4743 int precision, field_width, multibyte;
4744 {
4745 /* No region in strings. */
4746 it->region_beg_charpos = it->region_end_charpos = -1;
4747
4748 /* No text property checks performed by default, but see below. */
4749 it->stop_charpos = -1;
4750
4751 /* Set iterator position and end position. */
4752 bzero (&it->current, sizeof it->current);
4753 it->current.overlay_string_index = -1;
4754 it->current.dpvec_index = -1;
4755 xassert (charpos >= 0);
4756
4757 /* If STRING is specified, use its multibyteness, otherwise use the
4758 setting of MULTIBYTE, if specified. */
4759 if (multibyte >= 0)
4760 it->multibyte_p = multibyte > 0;
4761
4762 if (s == NULL)
4763 {
4764 xassert (STRINGP (string));
4765 it->string = string;
4766 it->s = NULL;
4767 it->end_charpos = it->string_nchars = SCHARS (string);
4768 it->method = next_element_from_string;
4769 it->current.string_pos = string_pos (charpos, string);
4770 }
4771 else
4772 {
4773 it->s = s;
4774 it->string = Qnil;
4775
4776 /* Note that we use IT->current.pos, not it->current.string_pos,
4777 for displaying C strings. */
4778 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4779 if (it->multibyte_p)
4780 {
4781 it->current.pos = c_string_pos (charpos, s, 1);
4782 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4783 }
4784 else
4785 {
4786 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4787 it->end_charpos = it->string_nchars = strlen (s);
4788 }
4789
4790 it->method = next_element_from_c_string;
4791 }
4792
4793 /* PRECISION > 0 means don't return more than PRECISION characters
4794 from the string. */
4795 if (precision > 0 && it->end_charpos - charpos > precision)
4796 it->end_charpos = it->string_nchars = charpos + precision;
4797
4798 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4799 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4800 FIELD_WIDTH < 0 means infinite field width. This is useful for
4801 padding with `-' at the end of a mode line. */
4802 if (field_width < 0)
4803 field_width = INFINITY;
4804 if (field_width > it->end_charpos - charpos)
4805 it->end_charpos = charpos + field_width;
4806
4807 /* Use the standard display table for displaying strings. */
4808 if (DISP_TABLE_P (Vstandard_display_table))
4809 it->dp = XCHAR_TABLE (Vstandard_display_table);
4810
4811 it->stop_charpos = charpos;
4812 CHECK_IT (it);
4813 }
4814
4815
4816 \f
4817 /***********************************************************************
4818 Iteration
4819 ***********************************************************************/
4820
4821 /* Load IT's display element fields with information about the next
4822 display element from the current position of IT. Value is zero if
4823 end of buffer (or C string) is reached. */
4824
4825 int
4826 get_next_display_element (it)
4827 struct it *it;
4828 {
4829 /* Non-zero means that we found a display element. Zero means that
4830 we hit the end of what we iterate over. Performance note: the
4831 function pointer `method' used here turns out to be faster than
4832 using a sequence of if-statements. */
4833 int success_p = (*it->method) (it);
4834
4835 if (it->what == IT_CHARACTER)
4836 {
4837 /* Map via display table or translate control characters.
4838 IT->c, IT->len etc. have been set to the next character by
4839 the function call above. If we have a display table, and it
4840 contains an entry for IT->c, translate it. Don't do this if
4841 IT->c itself comes from a display table, otherwise we could
4842 end up in an infinite recursion. (An alternative could be to
4843 count the recursion depth of this function and signal an
4844 error when a certain maximum depth is reached.) Is it worth
4845 it? */
4846 if (success_p && it->dpvec == NULL)
4847 {
4848 Lisp_Object dv;
4849
4850 if (it->dp
4851 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4852 VECTORP (dv)))
4853 {
4854 struct Lisp_Vector *v = XVECTOR (dv);
4855
4856 /* Return the first character from the display table
4857 entry, if not empty. If empty, don't display the
4858 current character. */
4859 if (v->size)
4860 {
4861 it->dpvec_char_len = it->len;
4862 it->dpvec = v->contents;
4863 it->dpend = v->contents + v->size;
4864 it->current.dpvec_index = 0;
4865 it->method = next_element_from_display_vector;
4866 success_p = get_next_display_element (it);
4867 }
4868 else
4869 {
4870 set_iterator_to_next (it, 0);
4871 success_p = get_next_display_element (it);
4872 }
4873 }
4874
4875 /* Translate control characters into `\003' or `^C' form.
4876 Control characters coming from a display table entry are
4877 currently not translated because we use IT->dpvec to hold
4878 the translation. This could easily be changed but I
4879 don't believe that it is worth doing.
4880
4881 If it->multibyte_p is nonzero, eight-bit characters and
4882 non-printable multibyte characters are also translated to
4883 octal form.
4884
4885 If it->multibyte_p is zero, eight-bit characters that
4886 don't have corresponding multibyte char code are also
4887 translated to octal form. */
4888 else if ((it->c < ' '
4889 && (it->area != TEXT_AREA
4890 || (it->c != '\n' && it->c != '\t')))
4891 || (it->multibyte_p
4892 ? ((it->c >= 127
4893 && it->len == 1)
4894 || !CHAR_PRINTABLE_P (it->c))
4895 : (it->c >= 127
4896 && it->c == unibyte_char_to_multibyte (it->c))))
4897 {
4898 /* IT->c is a control character which must be displayed
4899 either as '\003' or as `^C' where the '\\' and '^'
4900 can be defined in the display table. Fill
4901 IT->ctl_chars with glyphs for what we have to
4902 display. Then, set IT->dpvec to these glyphs. */
4903 GLYPH g;
4904
4905 if (it->c < 128 && it->ctl_arrow_p)
4906 {
4907 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4908 if (it->dp
4909 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4910 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4911 g = XINT (DISP_CTRL_GLYPH (it->dp));
4912 else
4913 g = FAST_MAKE_GLYPH ('^', 0);
4914 XSETINT (it->ctl_chars[0], g);
4915
4916 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4917 XSETINT (it->ctl_chars[1], g);
4918
4919 /* Set up IT->dpvec and return first character from it. */
4920 it->dpvec_char_len = it->len;
4921 it->dpvec = it->ctl_chars;
4922 it->dpend = it->dpvec + 2;
4923 it->current.dpvec_index = 0;
4924 it->method = next_element_from_display_vector;
4925 get_next_display_element (it);
4926 }
4927 else
4928 {
4929 unsigned char str[MAX_MULTIBYTE_LENGTH];
4930 int len;
4931 int i;
4932 GLYPH escape_glyph;
4933
4934 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4935 if (it->dp
4936 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4937 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4938 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4939 else
4940 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4941
4942 if (SINGLE_BYTE_CHAR_P (it->c))
4943 str[0] = it->c, len = 1;
4944 else
4945 {
4946 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4947 if (len < 0)
4948 {
4949 /* It's an invalid character, which
4950 shouldn't happen actually, but due to
4951 bugs it may happen. Let's print the char
4952 as is, there's not much meaningful we can
4953 do with it. */
4954 str[0] = it->c;
4955 str[1] = it->c >> 8;
4956 str[2] = it->c >> 16;
4957 str[3] = it->c >> 24;
4958 len = 4;
4959 }
4960 }
4961
4962 for (i = 0; i < len; i++)
4963 {
4964 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4965 /* Insert three more glyphs into IT->ctl_chars for
4966 the octal display of the character. */
4967 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4968 XSETINT (it->ctl_chars[i * 4 + 1], g);
4969 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4970 XSETINT (it->ctl_chars[i * 4 + 2], g);
4971 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4972 XSETINT (it->ctl_chars[i * 4 + 3], g);
4973 }
4974
4975 /* Set up IT->dpvec and return the first character
4976 from it. */
4977 it->dpvec_char_len = it->len;
4978 it->dpvec = it->ctl_chars;
4979 it->dpend = it->dpvec + len * 4;
4980 it->current.dpvec_index = 0;
4981 it->method = next_element_from_display_vector;
4982 get_next_display_element (it);
4983 }
4984 }
4985 }
4986
4987 /* Adjust face id for a multibyte character. There are no
4988 multibyte character in unibyte text. */
4989 if (it->multibyte_p
4990 && success_p
4991 && FRAME_WINDOW_P (it->f))
4992 {
4993 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4994 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4995 }
4996 }
4997
4998 /* Is this character the last one of a run of characters with
4999 box? If yes, set IT->end_of_box_run_p to 1. */
5000 if (it->face_box_p
5001 && it->s == NULL)
5002 {
5003 int face_id;
5004 struct face *face;
5005
5006 it->end_of_box_run_p
5007 = ((face_id = face_after_it_pos (it),
5008 face_id != it->face_id)
5009 && (face = FACE_FROM_ID (it->f, face_id),
5010 face->box == FACE_NO_BOX));
5011 }
5012
5013 /* Value is 0 if end of buffer or string reached. */
5014 return success_p;
5015 }
5016
5017
5018 /* Move IT to the next display element.
5019
5020 RESEAT_P non-zero means if called on a newline in buffer text,
5021 skip to the next visible line start.
5022
5023 Functions get_next_display_element and set_iterator_to_next are
5024 separate because I find this arrangement easier to handle than a
5025 get_next_display_element function that also increments IT's
5026 position. The way it is we can first look at an iterator's current
5027 display element, decide whether it fits on a line, and if it does,
5028 increment the iterator position. The other way around we probably
5029 would either need a flag indicating whether the iterator has to be
5030 incremented the next time, or we would have to implement a
5031 decrement position function which would not be easy to write. */
5032
5033 void
5034 set_iterator_to_next (it, reseat_p)
5035 struct it *it;
5036 int reseat_p;
5037 {
5038 /* Reset flags indicating start and end of a sequence of characters
5039 with box. Reset them at the start of this function because
5040 moving the iterator to a new position might set them. */
5041 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5042
5043 if (it->method == next_element_from_buffer)
5044 {
5045 /* The current display element of IT is a character from
5046 current_buffer. Advance in the buffer, and maybe skip over
5047 invisible lines that are so because of selective display. */
5048 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5049 reseat_at_next_visible_line_start (it, 0);
5050 else
5051 {
5052 xassert (it->len != 0);
5053 IT_BYTEPOS (*it) += it->len;
5054 IT_CHARPOS (*it) += 1;
5055 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5056 }
5057 }
5058 else if (it->method == next_element_from_composition)
5059 {
5060 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
5061 if (STRINGP (it->string))
5062 {
5063 IT_STRING_BYTEPOS (*it) += it->len;
5064 IT_STRING_CHARPOS (*it) += it->cmp_len;
5065 it->method = next_element_from_string;
5066 goto consider_string_end;
5067 }
5068 else
5069 {
5070 IT_BYTEPOS (*it) += it->len;
5071 IT_CHARPOS (*it) += it->cmp_len;
5072 it->method = next_element_from_buffer;
5073 }
5074 }
5075 else if (it->method == next_element_from_c_string)
5076 {
5077 /* Current display element of IT is from a C string. */
5078 IT_BYTEPOS (*it) += it->len;
5079 IT_CHARPOS (*it) += 1;
5080 }
5081 else if (it->method == next_element_from_display_vector)
5082 {
5083 /* Current display element of IT is from a display table entry.
5084 Advance in the display table definition. Reset it to null if
5085 end reached, and continue with characters from buffers/
5086 strings. */
5087 ++it->current.dpvec_index;
5088
5089 /* Restore face of the iterator to what they were before the
5090 display vector entry (these entries may contain faces). */
5091 it->face_id = it->saved_face_id;
5092
5093 if (it->dpvec + it->current.dpvec_index == it->dpend)
5094 {
5095 if (it->s)
5096 it->method = next_element_from_c_string;
5097 else if (STRINGP (it->string))
5098 it->method = next_element_from_string;
5099 else
5100 it->method = next_element_from_buffer;
5101
5102 it->dpvec = NULL;
5103 it->current.dpvec_index = -1;
5104
5105 /* Skip over characters which were displayed via IT->dpvec. */
5106 if (it->dpvec_char_len < 0)
5107 reseat_at_next_visible_line_start (it, 1);
5108 else if (it->dpvec_char_len > 0)
5109 {
5110 it->len = it->dpvec_char_len;
5111 set_iterator_to_next (it, reseat_p);
5112 }
5113 }
5114 }
5115 else if (it->method == next_element_from_string)
5116 {
5117 /* Current display element is a character from a Lisp string. */
5118 xassert (it->s == NULL && STRINGP (it->string));
5119 IT_STRING_BYTEPOS (*it) += it->len;
5120 IT_STRING_CHARPOS (*it) += 1;
5121
5122 consider_string_end:
5123
5124 if (it->current.overlay_string_index >= 0)
5125 {
5126 /* IT->string is an overlay string. Advance to the
5127 next, if there is one. */
5128 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5129 next_overlay_string (it);
5130 }
5131 else
5132 {
5133 /* IT->string is not an overlay string. If we reached
5134 its end, and there is something on IT->stack, proceed
5135 with what is on the stack. This can be either another
5136 string, this time an overlay string, or a buffer. */
5137 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5138 && it->sp > 0)
5139 {
5140 pop_it (it);
5141 if (!STRINGP (it->string))
5142 it->method = next_element_from_buffer;
5143 else
5144 goto consider_string_end;
5145 }
5146 }
5147 }
5148 else if (it->method == next_element_from_image
5149 || it->method == next_element_from_stretch)
5150 {
5151 /* The position etc with which we have to proceed are on
5152 the stack. The position may be at the end of a string,
5153 if the `display' property takes up the whole string. */
5154 pop_it (it);
5155 it->image_id = 0;
5156 if (STRINGP (it->string))
5157 {
5158 it->method = next_element_from_string;
5159 goto consider_string_end;
5160 }
5161 else
5162 it->method = next_element_from_buffer;
5163 }
5164 else
5165 /* There are no other methods defined, so this should be a bug. */
5166 abort ();
5167
5168 xassert (it->method != next_element_from_string
5169 || (STRINGP (it->string)
5170 && IT_STRING_CHARPOS (*it) >= 0));
5171 }
5172
5173
5174 /* Load IT's display element fields with information about the next
5175 display element which comes from a display table entry or from the
5176 result of translating a control character to one of the forms `^C'
5177 or `\003'. IT->dpvec holds the glyphs to return as characters. */
5178
5179 static int
5180 next_element_from_display_vector (it)
5181 struct it *it;
5182 {
5183 /* Precondition. */
5184 xassert (it->dpvec && it->current.dpvec_index >= 0);
5185
5186 /* Remember the current face id in case glyphs specify faces.
5187 IT's face is restored in set_iterator_to_next. */
5188 it->saved_face_id = it->face_id;
5189
5190 if (INTEGERP (*it->dpvec)
5191 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5192 {
5193 int lface_id;
5194 GLYPH g;
5195
5196 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5197 it->c = FAST_GLYPH_CHAR (g);
5198 it->len = CHAR_BYTES (it->c);
5199
5200 /* The entry may contain a face id to use. Such a face id is
5201 the id of a Lisp face, not a realized face. A face id of
5202 zero means no face is specified. */
5203 lface_id = FAST_GLYPH_FACE (g);
5204 if (lface_id)
5205 {
5206 /* The function returns -1 if lface_id is invalid. */
5207 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
5208 if (face_id >= 0)
5209 it->face_id = face_id;
5210 }
5211 }
5212 else
5213 /* Display table entry is invalid. Return a space. */
5214 it->c = ' ', it->len = 1;
5215
5216 /* Don't change position and object of the iterator here. They are
5217 still the values of the character that had this display table
5218 entry or was translated, and that's what we want. */
5219 it->what = IT_CHARACTER;
5220 return 1;
5221 }
5222
5223
5224 /* Load IT with the next display element from Lisp string IT->string.
5225 IT->current.string_pos is the current position within the string.
5226 If IT->current.overlay_string_index >= 0, the Lisp string is an
5227 overlay string. */
5228
5229 static int
5230 next_element_from_string (it)
5231 struct it *it;
5232 {
5233 struct text_pos position;
5234
5235 xassert (STRINGP (it->string));
5236 xassert (IT_STRING_CHARPOS (*it) >= 0);
5237 position = it->current.string_pos;
5238
5239 /* Time to check for invisible text? */
5240 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5241 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5242 {
5243 handle_stop (it);
5244
5245 /* Since a handler may have changed IT->method, we must
5246 recurse here. */
5247 return get_next_display_element (it);
5248 }
5249
5250 if (it->current.overlay_string_index >= 0)
5251 {
5252 /* Get the next character from an overlay string. In overlay
5253 strings, There is no field width or padding with spaces to
5254 do. */
5255 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5256 {
5257 it->what = IT_EOB;
5258 return 0;
5259 }
5260 else if (STRING_MULTIBYTE (it->string))
5261 {
5262 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5263 const unsigned char *s = (SDATA (it->string)
5264 + IT_STRING_BYTEPOS (*it));
5265 it->c = string_char_and_length (s, remaining, &it->len);
5266 }
5267 else
5268 {
5269 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5270 it->len = 1;
5271 }
5272 }
5273 else
5274 {
5275 /* Get the next character from a Lisp string that is not an
5276 overlay string. Such strings come from the mode line, for
5277 example. We may have to pad with spaces, or truncate the
5278 string. See also next_element_from_c_string. */
5279 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5280 {
5281 it->what = IT_EOB;
5282 return 0;
5283 }
5284 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5285 {
5286 /* Pad with spaces. */
5287 it->c = ' ', it->len = 1;
5288 CHARPOS (position) = BYTEPOS (position) = -1;
5289 }
5290 else if (STRING_MULTIBYTE (it->string))
5291 {
5292 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5293 const unsigned char *s = (SDATA (it->string)
5294 + IT_STRING_BYTEPOS (*it));
5295 it->c = string_char_and_length (s, maxlen, &it->len);
5296 }
5297 else
5298 {
5299 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5300 it->len = 1;
5301 }
5302 }
5303
5304 /* Record what we have and where it came from. Note that we store a
5305 buffer position in IT->position although it could arguably be a
5306 string position. */
5307 it->what = IT_CHARACTER;
5308 it->object = it->string;
5309 it->position = position;
5310 return 1;
5311 }
5312
5313
5314 /* Load IT with next display element from C string IT->s.
5315 IT->string_nchars is the maximum number of characters to return
5316 from the string. IT->end_charpos may be greater than
5317 IT->string_nchars when this function is called, in which case we
5318 may have to return padding spaces. Value is zero if end of string
5319 reached, including padding spaces. */
5320
5321 static int
5322 next_element_from_c_string (it)
5323 struct it *it;
5324 {
5325 int success_p = 1;
5326
5327 xassert (it->s);
5328 it->what = IT_CHARACTER;
5329 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5330 it->object = Qnil;
5331
5332 /* IT's position can be greater IT->string_nchars in case a field
5333 width or precision has been specified when the iterator was
5334 initialized. */
5335 if (IT_CHARPOS (*it) >= it->end_charpos)
5336 {
5337 /* End of the game. */
5338 it->what = IT_EOB;
5339 success_p = 0;
5340 }
5341 else if (IT_CHARPOS (*it) >= it->string_nchars)
5342 {
5343 /* Pad with spaces. */
5344 it->c = ' ', it->len = 1;
5345 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5346 }
5347 else if (it->multibyte_p)
5348 {
5349 /* Implementation note: The calls to strlen apparently aren't a
5350 performance problem because there is no noticeable performance
5351 difference between Emacs running in unibyte or multibyte mode. */
5352 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5353 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5354 maxlen, &it->len);
5355 }
5356 else
5357 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5358
5359 return success_p;
5360 }
5361
5362
5363 /* Set up IT to return characters from an ellipsis, if appropriate.
5364 The definition of the ellipsis glyphs may come from a display table
5365 entry. This function Fills IT with the first glyph from the
5366 ellipsis if an ellipsis is to be displayed. */
5367
5368 static int
5369 next_element_from_ellipsis (it)
5370 struct it *it;
5371 {
5372 if (it->selective_display_ellipsis_p)
5373 {
5374 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
5375 {
5376 /* Use the display table definition for `...'. Invalid glyphs
5377 will be handled by the method returning elements from dpvec. */
5378 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
5379 it->dpvec_char_len = it->len;
5380 it->dpvec = v->contents;
5381 it->dpend = v->contents + v->size;
5382 it->current.dpvec_index = 0;
5383 it->method = next_element_from_display_vector;
5384 }
5385 else
5386 {
5387 /* Use default `...' which is stored in default_invis_vector. */
5388 it->dpvec_char_len = it->len;
5389 it->dpvec = default_invis_vector;
5390 it->dpend = default_invis_vector + 3;
5391 it->current.dpvec_index = 0;
5392 it->method = next_element_from_display_vector;
5393 }
5394 }
5395 else
5396 {
5397 /* The face at the current position may be different from the
5398 face we find after the invisible text. Remember what it
5399 was in IT->saved_face_id, and signal that it's there by
5400 setting face_before_selective_p. */
5401 it->saved_face_id = it->face_id;
5402 it->method = next_element_from_buffer;
5403 reseat_at_next_visible_line_start (it, 1);
5404 it->face_before_selective_p = 1;
5405 }
5406
5407 return get_next_display_element (it);
5408 }
5409
5410
5411 /* Deliver an image display element. The iterator IT is already
5412 filled with image information (done in handle_display_prop). Value
5413 is always 1. */
5414
5415
5416 static int
5417 next_element_from_image (it)
5418 struct it *it;
5419 {
5420 it->what = IT_IMAGE;
5421 return 1;
5422 }
5423
5424
5425 /* Fill iterator IT with next display element from a stretch glyph
5426 property. IT->object is the value of the text property. Value is
5427 always 1. */
5428
5429 static int
5430 next_element_from_stretch (it)
5431 struct it *it;
5432 {
5433 it->what = IT_STRETCH;
5434 return 1;
5435 }
5436
5437
5438 /* Load IT with the next display element from current_buffer. Value
5439 is zero if end of buffer reached. IT->stop_charpos is the next
5440 position at which to stop and check for text properties or buffer
5441 end. */
5442
5443 static int
5444 next_element_from_buffer (it)
5445 struct it *it;
5446 {
5447 int success_p = 1;
5448
5449 /* Check this assumption, otherwise, we would never enter the
5450 if-statement, below. */
5451 xassert (IT_CHARPOS (*it) >= BEGV
5452 && IT_CHARPOS (*it) <= it->stop_charpos);
5453
5454 if (IT_CHARPOS (*it) >= it->stop_charpos)
5455 {
5456 if (IT_CHARPOS (*it) >= it->end_charpos)
5457 {
5458 int overlay_strings_follow_p;
5459
5460 /* End of the game, except when overlay strings follow that
5461 haven't been returned yet. */
5462 if (it->overlay_strings_at_end_processed_p)
5463 overlay_strings_follow_p = 0;
5464 else
5465 {
5466 it->overlay_strings_at_end_processed_p = 1;
5467 overlay_strings_follow_p = get_overlay_strings (it, 0);
5468 }
5469
5470 if (overlay_strings_follow_p)
5471 success_p = get_next_display_element (it);
5472 else
5473 {
5474 it->what = IT_EOB;
5475 it->position = it->current.pos;
5476 success_p = 0;
5477 }
5478 }
5479 else
5480 {
5481 handle_stop (it);
5482 return get_next_display_element (it);
5483 }
5484 }
5485 else
5486 {
5487 /* No face changes, overlays etc. in sight, so just return a
5488 character from current_buffer. */
5489 unsigned char *p;
5490
5491 /* Maybe run the redisplay end trigger hook. Performance note:
5492 This doesn't seem to cost measurable time. */
5493 if (it->redisplay_end_trigger_charpos
5494 && it->glyph_row
5495 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5496 run_redisplay_end_trigger_hook (it);
5497
5498 /* Get the next character, maybe multibyte. */
5499 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5500 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5501 {
5502 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5503 - IT_BYTEPOS (*it));
5504 it->c = string_char_and_length (p, maxlen, &it->len);
5505 }
5506 else
5507 it->c = *p, it->len = 1;
5508
5509 /* Record what we have and where it came from. */
5510 it->what = IT_CHARACTER;;
5511 it->object = it->w->buffer;
5512 it->position = it->current.pos;
5513
5514 /* Normally we return the character found above, except when we
5515 really want to return an ellipsis for selective display. */
5516 if (it->selective)
5517 {
5518 if (it->c == '\n')
5519 {
5520 /* A value of selective > 0 means hide lines indented more
5521 than that number of columns. */
5522 if (it->selective > 0
5523 && IT_CHARPOS (*it) + 1 < ZV
5524 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5525 IT_BYTEPOS (*it) + 1,
5526 (double) it->selective)) /* iftc */
5527 {
5528 success_p = next_element_from_ellipsis (it);
5529 it->dpvec_char_len = -1;
5530 }
5531 }
5532 else if (it->c == '\r' && it->selective == -1)
5533 {
5534 /* A value of selective == -1 means that everything from the
5535 CR to the end of the line is invisible, with maybe an
5536 ellipsis displayed for it. */
5537 success_p = next_element_from_ellipsis (it);
5538 it->dpvec_char_len = -1;
5539 }
5540 }
5541 }
5542
5543 /* Value is zero if end of buffer reached. */
5544 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5545 return success_p;
5546 }
5547
5548
5549 /* Run the redisplay end trigger hook for IT. */
5550
5551 static void
5552 run_redisplay_end_trigger_hook (it)
5553 struct it *it;
5554 {
5555 Lisp_Object args[3];
5556
5557 /* IT->glyph_row should be non-null, i.e. we should be actually
5558 displaying something, or otherwise we should not run the hook. */
5559 xassert (it->glyph_row);
5560
5561 /* Set up hook arguments. */
5562 args[0] = Qredisplay_end_trigger_functions;
5563 args[1] = it->window;
5564 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5565 it->redisplay_end_trigger_charpos = 0;
5566
5567 /* Since we are *trying* to run these functions, don't try to run
5568 them again, even if they get an error. */
5569 it->w->redisplay_end_trigger = Qnil;
5570 Frun_hook_with_args (3, args);
5571
5572 /* Notice if it changed the face of the character we are on. */
5573 handle_face_prop (it);
5574 }
5575
5576
5577 /* Deliver a composition display element. The iterator IT is already
5578 filled with composition information (done in
5579 handle_composition_prop). Value is always 1. */
5580
5581 static int
5582 next_element_from_composition (it)
5583 struct it *it;
5584 {
5585 it->what = IT_COMPOSITION;
5586 it->position = (STRINGP (it->string)
5587 ? it->current.string_pos
5588 : it->current.pos);
5589 return 1;
5590 }
5591
5592
5593 \f
5594 /***********************************************************************
5595 Moving an iterator without producing glyphs
5596 ***********************************************************************/
5597
5598 /* Move iterator IT to a specified buffer or X position within one
5599 line on the display without producing glyphs.
5600
5601 OP should be a bit mask including some or all of these bits:
5602 MOVE_TO_X: Stop on reaching x-position TO_X.
5603 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5604 Regardless of OP's value, stop in reaching the end of the display line.
5605
5606 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5607 This means, in particular, that TO_X includes window's horizontal
5608 scroll amount.
5609
5610 The return value has several possible values that
5611 say what condition caused the scan to stop:
5612
5613 MOVE_POS_MATCH_OR_ZV
5614 - when TO_POS or ZV was reached.
5615
5616 MOVE_X_REACHED
5617 -when TO_X was reached before TO_POS or ZV were reached.
5618
5619 MOVE_LINE_CONTINUED
5620 - when we reached the end of the display area and the line must
5621 be continued.
5622
5623 MOVE_LINE_TRUNCATED
5624 - when we reached the end of the display area and the line is
5625 truncated.
5626
5627 MOVE_NEWLINE_OR_CR
5628 - when we stopped at a line end, i.e. a newline or a CR and selective
5629 display is on. */
5630
5631 static enum move_it_result
5632 move_it_in_display_line_to (it, to_charpos, to_x, op)
5633 struct it *it;
5634 int to_charpos, to_x, op;
5635 {
5636 enum move_it_result result = MOVE_UNDEFINED;
5637 struct glyph_row *saved_glyph_row;
5638
5639 /* Don't produce glyphs in produce_glyphs. */
5640 saved_glyph_row = it->glyph_row;
5641 it->glyph_row = NULL;
5642
5643 #define BUFFER_POS_REACHED_P() \
5644 ((op & MOVE_TO_POS) != 0 \
5645 && BUFFERP (it->object) \
5646 && IT_CHARPOS (*it) >= to_charpos)
5647
5648 while (1)
5649 {
5650 int x, i, ascent = 0, descent = 0;
5651
5652 /* Stop when ZV or TO_CHARPOS reached. */
5653 if (!get_next_display_element (it)
5654 || BUFFER_POS_REACHED_P ())
5655 {
5656 result = MOVE_POS_MATCH_OR_ZV;
5657 break;
5658 }
5659
5660 /* The call to produce_glyphs will get the metrics of the
5661 display element IT is loaded with. We record in x the
5662 x-position before this display element in case it does not
5663 fit on the line. */
5664 x = it->current_x;
5665
5666 /* Remember the line height so far in case the next element doesn't
5667 fit on the line. */
5668 if (!it->truncate_lines_p)
5669 {
5670 ascent = it->max_ascent;
5671 descent = it->max_descent;
5672 }
5673
5674 PRODUCE_GLYPHS (it);
5675
5676 if (it->area != TEXT_AREA)
5677 {
5678 set_iterator_to_next (it, 1);
5679 continue;
5680 }
5681
5682 /* The number of glyphs we get back in IT->nglyphs will normally
5683 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5684 character on a terminal frame, or (iii) a line end. For the
5685 second case, IT->nglyphs - 1 padding glyphs will be present
5686 (on X frames, there is only one glyph produced for a
5687 composite character.
5688
5689 The behavior implemented below means, for continuation lines,
5690 that as many spaces of a TAB as fit on the current line are
5691 displayed there. For terminal frames, as many glyphs of a
5692 multi-glyph character are displayed in the current line, too.
5693 This is what the old redisplay code did, and we keep it that
5694 way. Under X, the whole shape of a complex character must
5695 fit on the line or it will be completely displayed in the
5696 next line.
5697
5698 Note that both for tabs and padding glyphs, all glyphs have
5699 the same width. */
5700 if (it->nglyphs)
5701 {
5702 /* More than one glyph or glyph doesn't fit on line. All
5703 glyphs have the same width. */
5704 int single_glyph_width = it->pixel_width / it->nglyphs;
5705 int new_x;
5706
5707 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5708 {
5709 new_x = x + single_glyph_width;
5710
5711 /* We want to leave anything reaching TO_X to the caller. */
5712 if ((op & MOVE_TO_X) && new_x > to_x)
5713 {
5714 it->current_x = x;
5715 result = MOVE_X_REACHED;
5716 break;
5717 }
5718 else if (/* Lines are continued. */
5719 !it->truncate_lines_p
5720 && (/* And glyph doesn't fit on the line. */
5721 new_x > it->last_visible_x
5722 /* Or it fits exactly and we're on a window
5723 system frame. */
5724 || (new_x == it->last_visible_x
5725 && FRAME_WINDOW_P (it->f))))
5726 {
5727 if (/* IT->hpos == 0 means the very first glyph
5728 doesn't fit on the line, e.g. a wide image. */
5729 it->hpos == 0
5730 || (new_x == it->last_visible_x
5731 && FRAME_WINDOW_P (it->f)))
5732 {
5733 ++it->hpos;
5734 it->current_x = new_x;
5735 if (i == it->nglyphs - 1)
5736 {
5737 set_iterator_to_next (it, 1);
5738 #ifdef HAVE_WINDOW_SYSTEM
5739 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5740 {
5741 if (!get_next_display_element (it)
5742 || BUFFER_POS_REACHED_P ())
5743 {
5744 result = MOVE_POS_MATCH_OR_ZV;
5745 break;
5746 }
5747 if (ITERATOR_AT_END_OF_LINE_P (it))
5748 {
5749 result = MOVE_NEWLINE_OR_CR;
5750 break;
5751 }
5752 }
5753 #endif /* HAVE_WINDOW_SYSTEM */
5754 }
5755 }
5756 else
5757 {
5758 it->current_x = x;
5759 it->max_ascent = ascent;
5760 it->max_descent = descent;
5761 }
5762
5763 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5764 IT_CHARPOS (*it)));
5765 result = MOVE_LINE_CONTINUED;
5766 break;
5767 }
5768 else if (new_x > it->first_visible_x)
5769 {
5770 /* Glyph is visible. Increment number of glyphs that
5771 would be displayed. */
5772 ++it->hpos;
5773 }
5774 else
5775 {
5776 /* Glyph is completely off the left margin of the display
5777 area. Nothing to do. */
5778 }
5779 }
5780
5781 if (result != MOVE_UNDEFINED)
5782 break;
5783 }
5784 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5785 {
5786 /* Stop when TO_X specified and reached. This check is
5787 necessary here because of lines consisting of a line end,
5788 only. The line end will not produce any glyphs and we
5789 would never get MOVE_X_REACHED. */
5790 xassert (it->nglyphs == 0);
5791 result = MOVE_X_REACHED;
5792 break;
5793 }
5794
5795 /* Is this a line end? If yes, we're done. */
5796 if (ITERATOR_AT_END_OF_LINE_P (it))
5797 {
5798 result = MOVE_NEWLINE_OR_CR;
5799 break;
5800 }
5801
5802 /* The current display element has been consumed. Advance
5803 to the next. */
5804 set_iterator_to_next (it, 1);
5805
5806 /* Stop if lines are truncated and IT's current x-position is
5807 past the right edge of the window now. */
5808 if (it->truncate_lines_p
5809 && it->current_x >= it->last_visible_x)
5810 {
5811 #ifdef HAVE_WINDOW_SYSTEM
5812 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5813 {
5814 if (!get_next_display_element (it)
5815 || BUFFER_POS_REACHED_P ())
5816 {
5817 result = MOVE_POS_MATCH_OR_ZV;
5818 break;
5819 }
5820 if (ITERATOR_AT_END_OF_LINE_P (it))
5821 {
5822 result = MOVE_NEWLINE_OR_CR;
5823 break;
5824 }
5825 }
5826 #endif /* HAVE_WINDOW_SYSTEM */
5827 result = MOVE_LINE_TRUNCATED;
5828 break;
5829 }
5830 }
5831
5832 #undef BUFFER_POS_REACHED_P
5833
5834 /* Restore the iterator settings altered at the beginning of this
5835 function. */
5836 it->glyph_row = saved_glyph_row;
5837 return result;
5838 }
5839
5840
5841 /* Move IT forward until it satisfies one or more of the criteria in
5842 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5843
5844 OP is a bit-mask that specifies where to stop, and in particular,
5845 which of those four position arguments makes a difference. See the
5846 description of enum move_operation_enum.
5847
5848 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5849 screen line, this function will set IT to the next position >
5850 TO_CHARPOS. */
5851
5852 void
5853 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5854 struct it *it;
5855 int to_charpos, to_x, to_y, to_vpos;
5856 int op;
5857 {
5858 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5859 int line_height;
5860 int reached = 0;
5861
5862 for (;;)
5863 {
5864 if (op & MOVE_TO_VPOS)
5865 {
5866 /* If no TO_CHARPOS and no TO_X specified, stop at the
5867 start of the line TO_VPOS. */
5868 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5869 {
5870 if (it->vpos == to_vpos)
5871 {
5872 reached = 1;
5873 break;
5874 }
5875 else
5876 skip = move_it_in_display_line_to (it, -1, -1, 0);
5877 }
5878 else
5879 {
5880 /* TO_VPOS >= 0 means stop at TO_X in the line at
5881 TO_VPOS, or at TO_POS, whichever comes first. */
5882 if (it->vpos == to_vpos)
5883 {
5884 reached = 2;
5885 break;
5886 }
5887
5888 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5889
5890 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5891 {
5892 reached = 3;
5893 break;
5894 }
5895 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5896 {
5897 /* We have reached TO_X but not in the line we want. */
5898 skip = move_it_in_display_line_to (it, to_charpos,
5899 -1, MOVE_TO_POS);
5900 if (skip == MOVE_POS_MATCH_OR_ZV)
5901 {
5902 reached = 4;
5903 break;
5904 }
5905 }
5906 }
5907 }
5908 else if (op & MOVE_TO_Y)
5909 {
5910 struct it it_backup;
5911
5912 /* TO_Y specified means stop at TO_X in the line containing
5913 TO_Y---or at TO_CHARPOS if this is reached first. The
5914 problem is that we can't really tell whether the line
5915 contains TO_Y before we have completely scanned it, and
5916 this may skip past TO_X. What we do is to first scan to
5917 TO_X.
5918
5919 If TO_X is not specified, use a TO_X of zero. The reason
5920 is to make the outcome of this function more predictable.
5921 If we didn't use TO_X == 0, we would stop at the end of
5922 the line which is probably not what a caller would expect
5923 to happen. */
5924 skip = move_it_in_display_line_to (it, to_charpos,
5925 ((op & MOVE_TO_X)
5926 ? to_x : 0),
5927 (MOVE_TO_X
5928 | (op & MOVE_TO_POS)));
5929
5930 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5931 if (skip == MOVE_POS_MATCH_OR_ZV)
5932 {
5933 reached = 5;
5934 break;
5935 }
5936
5937 /* If TO_X was reached, we would like to know whether TO_Y
5938 is in the line. This can only be said if we know the
5939 total line height which requires us to scan the rest of
5940 the line. */
5941 if (skip == MOVE_X_REACHED)
5942 {
5943 it_backup = *it;
5944 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5945 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5946 op & MOVE_TO_POS);
5947 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5948 }
5949
5950 /* Now, decide whether TO_Y is in this line. */
5951 line_height = it->max_ascent + it->max_descent;
5952 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5953
5954 if (to_y >= it->current_y
5955 && to_y < it->current_y + line_height)
5956 {
5957 if (skip == MOVE_X_REACHED)
5958 /* If TO_Y is in this line and TO_X was reached above,
5959 we scanned too far. We have to restore IT's settings
5960 to the ones before skipping. */
5961 *it = it_backup;
5962 reached = 6;
5963 }
5964 else if (skip == MOVE_X_REACHED)
5965 {
5966 skip = skip2;
5967 if (skip == MOVE_POS_MATCH_OR_ZV)
5968 reached = 7;
5969 }
5970
5971 if (reached)
5972 break;
5973 }
5974 else
5975 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5976
5977 switch (skip)
5978 {
5979 case MOVE_POS_MATCH_OR_ZV:
5980 reached = 8;
5981 goto out;
5982
5983 case MOVE_NEWLINE_OR_CR:
5984 set_iterator_to_next (it, 1);
5985 it->continuation_lines_width = 0;
5986 break;
5987
5988 case MOVE_LINE_TRUNCATED:
5989 it->continuation_lines_width = 0;
5990 reseat_at_next_visible_line_start (it, 0);
5991 if ((op & MOVE_TO_POS) != 0
5992 && IT_CHARPOS (*it) > to_charpos)
5993 {
5994 reached = 9;
5995 goto out;
5996 }
5997 break;
5998
5999 case MOVE_LINE_CONTINUED:
6000 it->continuation_lines_width += it->current_x;
6001 break;
6002
6003 default:
6004 abort ();
6005 }
6006
6007 /* Reset/increment for the next run. */
6008 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6009 it->current_x = it->hpos = 0;
6010 it->current_y += it->max_ascent + it->max_descent;
6011 ++it->vpos;
6012 last_height = it->max_ascent + it->max_descent;
6013 last_max_ascent = it->max_ascent;
6014 it->max_ascent = it->max_descent = 0;
6015 }
6016
6017 out:
6018
6019 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6020 }
6021
6022
6023 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6024
6025 If DY > 0, move IT backward at least that many pixels. DY = 0
6026 means move IT backward to the preceding line start or BEGV. This
6027 function may move over more than DY pixels if IT->current_y - DY
6028 ends up in the middle of a line; in this case IT->current_y will be
6029 set to the top of the line moved to. */
6030
6031 void
6032 move_it_vertically_backward (it, dy)
6033 struct it *it;
6034 int dy;
6035 {
6036 int nlines, h;
6037 struct it it2, it3;
6038 int start_pos = IT_CHARPOS (*it);
6039
6040 xassert (dy >= 0);
6041
6042 /* Estimate how many newlines we must move back. */
6043 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6044
6045 /* Set the iterator's position that many lines back. */
6046 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6047 back_to_previous_visible_line_start (it);
6048
6049 /* Reseat the iterator here. When moving backward, we don't want
6050 reseat to skip forward over invisible text, set up the iterator
6051 to deliver from overlay strings at the new position etc. So,
6052 use reseat_1 here. */
6053 reseat_1 (it, it->current.pos, 1);
6054
6055 /* We are now surely at a line start. */
6056 it->current_x = it->hpos = 0;
6057 it->continuation_lines_width = 0;
6058
6059 /* Move forward and see what y-distance we moved. First move to the
6060 start of the next line so that we get its height. We need this
6061 height to be able to tell whether we reached the specified
6062 y-distance. */
6063 it2 = *it;
6064 it2.max_ascent = it2.max_descent = 0;
6065 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6066 MOVE_TO_POS | MOVE_TO_VPOS);
6067 xassert (IT_CHARPOS (*it) >= BEGV);
6068 it3 = it2;
6069
6070 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6071 xassert (IT_CHARPOS (*it) >= BEGV);
6072 /* H is the actual vertical distance from the position in *IT
6073 and the starting position. */
6074 h = it2.current_y - it->current_y;
6075 /* NLINES is the distance in number of lines. */
6076 nlines = it2.vpos - it->vpos;
6077
6078 /* Correct IT's y and vpos position
6079 so that they are relative to the starting point. */
6080 it->vpos -= nlines;
6081 it->current_y -= h;
6082
6083 if (dy == 0)
6084 {
6085 /* DY == 0 means move to the start of the screen line. The
6086 value of nlines is > 0 if continuation lines were involved. */
6087 if (nlines > 0)
6088 move_it_by_lines (it, nlines, 1);
6089 xassert (IT_CHARPOS (*it) <= start_pos);
6090 }
6091 else
6092 {
6093 /* The y-position we try to reach, relative to *IT.
6094 Note that H has been subtracted in front of the if-statement. */
6095 int target_y = it->current_y + h - dy;
6096 int y0 = it3.current_y;
6097 int y1 = line_bottom_y (&it3);
6098 int line_height = y1 - y0;
6099
6100 /* If we did not reach target_y, try to move further backward if
6101 we can. If we moved too far backward, try to move forward. */
6102 if (target_y < it->current_y
6103 /* This is heuristic. In a window that's 3 lines high, with
6104 a line height of 13 pixels each, recentering with point
6105 on the bottom line will try to move -39/2 = 19 pixels
6106 backward. Try to avoid moving into the first line. */
6107 && it->current_y - target_y > line_height / 3 * 2
6108 && IT_CHARPOS (*it) > BEGV)
6109 {
6110 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6111 target_y - it->current_y));
6112 move_it_vertically (it, target_y - it->current_y);
6113 xassert (IT_CHARPOS (*it) >= BEGV);
6114 }
6115 else if (target_y >= it->current_y + line_height
6116 && IT_CHARPOS (*it) < ZV)
6117 {
6118 /* Should move forward by at least one line, maybe more.
6119
6120 Note: Calling move_it_by_lines can be expensive on
6121 terminal frames, where compute_motion is used (via
6122 vmotion) to do the job, when there are very long lines
6123 and truncate-lines is nil. That's the reason for
6124 treating terminal frames specially here. */
6125
6126 if (!FRAME_WINDOW_P (it->f))
6127 move_it_vertically (it, target_y - (it->current_y + line_height));
6128 else
6129 {
6130 do
6131 {
6132 move_it_by_lines (it, 1, 1);
6133 }
6134 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6135 }
6136
6137 xassert (IT_CHARPOS (*it) >= BEGV);
6138 }
6139 }
6140 }
6141
6142
6143 /* Move IT by a specified amount of pixel lines DY. DY negative means
6144 move backwards. DY = 0 means move to start of screen line. At the
6145 end, IT will be on the start of a screen line. */
6146
6147 void
6148 move_it_vertically (it, dy)
6149 struct it *it;
6150 int dy;
6151 {
6152 if (dy <= 0)
6153 move_it_vertically_backward (it, -dy);
6154 else if (dy > 0)
6155 {
6156 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6157 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6158 MOVE_TO_POS | MOVE_TO_Y);
6159 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6160
6161 /* If buffer ends in ZV without a newline, move to the start of
6162 the line to satisfy the post-condition. */
6163 if (IT_CHARPOS (*it) == ZV
6164 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6165 move_it_by_lines (it, 0, 0);
6166 }
6167 }
6168
6169
6170 /* Move iterator IT past the end of the text line it is in. */
6171
6172 void
6173 move_it_past_eol (it)
6174 struct it *it;
6175 {
6176 enum move_it_result rc;
6177
6178 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6179 if (rc == MOVE_NEWLINE_OR_CR)
6180 set_iterator_to_next (it, 0);
6181 }
6182
6183
6184 #if 0 /* Currently not used. */
6185
6186 /* Return non-zero if some text between buffer positions START_CHARPOS
6187 and END_CHARPOS is invisible. IT->window is the window for text
6188 property lookup. */
6189
6190 static int
6191 invisible_text_between_p (it, start_charpos, end_charpos)
6192 struct it *it;
6193 int start_charpos, end_charpos;
6194 {
6195 Lisp_Object prop, limit;
6196 int invisible_found_p;
6197
6198 xassert (it != NULL && start_charpos <= end_charpos);
6199
6200 /* Is text at START invisible? */
6201 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6202 it->window);
6203 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6204 invisible_found_p = 1;
6205 else
6206 {
6207 limit = Fnext_single_char_property_change (make_number (start_charpos),
6208 Qinvisible, Qnil,
6209 make_number (end_charpos));
6210 invisible_found_p = XFASTINT (limit) < end_charpos;
6211 }
6212
6213 return invisible_found_p;
6214 }
6215
6216 #endif /* 0 */
6217
6218
6219 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6220 negative means move up. DVPOS == 0 means move to the start of the
6221 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6222 NEED_Y_P is zero, IT->current_y will be left unchanged.
6223
6224 Further optimization ideas: If we would know that IT->f doesn't use
6225 a face with proportional font, we could be faster for
6226 truncate-lines nil. */
6227
6228 void
6229 move_it_by_lines (it, dvpos, need_y_p)
6230 struct it *it;
6231 int dvpos, need_y_p;
6232 {
6233 struct position pos;
6234
6235 if (!FRAME_WINDOW_P (it->f))
6236 {
6237 struct text_pos textpos;
6238
6239 /* We can use vmotion on frames without proportional fonts. */
6240 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6241 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6242 reseat (it, textpos, 1);
6243 it->vpos += pos.vpos;
6244 it->current_y += pos.vpos;
6245 }
6246 else if (dvpos == 0)
6247 {
6248 /* DVPOS == 0 means move to the start of the screen line. */
6249 move_it_vertically_backward (it, 0);
6250 xassert (it->current_x == 0 && it->hpos == 0);
6251 }
6252 else if (dvpos > 0)
6253 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6254 else
6255 {
6256 struct it it2;
6257 int start_charpos, i;
6258
6259 /* Start at the beginning of the screen line containing IT's
6260 position. */
6261 move_it_vertically_backward (it, 0);
6262
6263 /* Go back -DVPOS visible lines and reseat the iterator there. */
6264 start_charpos = IT_CHARPOS (*it);
6265 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6266 back_to_previous_visible_line_start (it);
6267 reseat (it, it->current.pos, 1);
6268 it->current_x = it->hpos = 0;
6269
6270 /* Above call may have moved too far if continuation lines
6271 are involved. Scan forward and see if it did. */
6272 it2 = *it;
6273 it2.vpos = it2.current_y = 0;
6274 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6275 it->vpos -= it2.vpos;
6276 it->current_y -= it2.current_y;
6277 it->current_x = it->hpos = 0;
6278
6279 /* If we moved too far, move IT some lines forward. */
6280 if (it2.vpos > -dvpos)
6281 {
6282 int delta = it2.vpos + dvpos;
6283 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6284 }
6285 }
6286 }
6287
6288 /* Return 1 if IT points into the middle of a display vector. */
6289
6290 int
6291 in_display_vector_p (it)
6292 struct it *it;
6293 {
6294 return (it->method == next_element_from_display_vector
6295 && it->current.dpvec_index > 0
6296 && it->dpvec + it->current.dpvec_index != it->dpend);
6297 }
6298
6299 \f
6300 /***********************************************************************
6301 Messages
6302 ***********************************************************************/
6303
6304
6305 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6306 to *Messages*. */
6307
6308 void
6309 add_to_log (format, arg1, arg2)
6310 char *format;
6311 Lisp_Object arg1, arg2;
6312 {
6313 Lisp_Object args[3];
6314 Lisp_Object msg, fmt;
6315 char *buffer;
6316 int len;
6317 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6318
6319 /* Do nothing if called asynchronously. Inserting text into
6320 a buffer may call after-change-functions and alike and
6321 that would means running Lisp asynchronously. */
6322 if (handling_signal)
6323 return;
6324
6325 fmt = msg = Qnil;
6326 GCPRO4 (fmt, msg, arg1, arg2);
6327
6328 args[0] = fmt = build_string (format);
6329 args[1] = arg1;
6330 args[2] = arg2;
6331 msg = Fformat (3, args);
6332
6333 len = SBYTES (msg) + 1;
6334 buffer = (char *) alloca (len);
6335 bcopy (SDATA (msg), buffer, len);
6336
6337 message_dolog (buffer, len - 1, 1, 0);
6338 UNGCPRO;
6339 }
6340
6341
6342 /* Output a newline in the *Messages* buffer if "needs" one. */
6343
6344 void
6345 message_log_maybe_newline ()
6346 {
6347 if (message_log_need_newline)
6348 message_dolog ("", 0, 1, 0);
6349 }
6350
6351
6352 /* Add a string M of length NBYTES to the message log, optionally
6353 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6354 nonzero, means interpret the contents of M as multibyte. This
6355 function calls low-level routines in order to bypass text property
6356 hooks, etc. which might not be safe to run. */
6357
6358 void
6359 message_dolog (m, nbytes, nlflag, multibyte)
6360 const char *m;
6361 int nbytes, nlflag, multibyte;
6362 {
6363 if (!NILP (Vmemory_full))
6364 return;
6365
6366 if (!NILP (Vmessage_log_max))
6367 {
6368 struct buffer *oldbuf;
6369 Lisp_Object oldpoint, oldbegv, oldzv;
6370 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6371 int point_at_end = 0;
6372 int zv_at_end = 0;
6373 Lisp_Object old_deactivate_mark, tem;
6374 struct gcpro gcpro1;
6375
6376 old_deactivate_mark = Vdeactivate_mark;
6377 oldbuf = current_buffer;
6378 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6379 current_buffer->undo_list = Qt;
6380
6381 oldpoint = message_dolog_marker1;
6382 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6383 oldbegv = message_dolog_marker2;
6384 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6385 oldzv = message_dolog_marker3;
6386 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6387 GCPRO1 (old_deactivate_mark);
6388
6389 if (PT == Z)
6390 point_at_end = 1;
6391 if (ZV == Z)
6392 zv_at_end = 1;
6393
6394 BEGV = BEG;
6395 BEGV_BYTE = BEG_BYTE;
6396 ZV = Z;
6397 ZV_BYTE = Z_BYTE;
6398 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6399
6400 /* Insert the string--maybe converting multibyte to single byte
6401 or vice versa, so that all the text fits the buffer. */
6402 if (multibyte
6403 && NILP (current_buffer->enable_multibyte_characters))
6404 {
6405 int i, c, char_bytes;
6406 unsigned char work[1];
6407
6408 /* Convert a multibyte string to single-byte
6409 for the *Message* buffer. */
6410 for (i = 0; i < nbytes; i += char_bytes)
6411 {
6412 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6413 work[0] = (SINGLE_BYTE_CHAR_P (c)
6414 ? c
6415 : multibyte_char_to_unibyte (c, Qnil));
6416 insert_1_both (work, 1, 1, 1, 0, 0);
6417 }
6418 }
6419 else if (! multibyte
6420 && ! NILP (current_buffer->enable_multibyte_characters))
6421 {
6422 int i, c, char_bytes;
6423 unsigned char *msg = (unsigned char *) m;
6424 unsigned char str[MAX_MULTIBYTE_LENGTH];
6425 /* Convert a single-byte string to multibyte
6426 for the *Message* buffer. */
6427 for (i = 0; i < nbytes; i++)
6428 {
6429 c = unibyte_char_to_multibyte (msg[i]);
6430 char_bytes = CHAR_STRING (c, str);
6431 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6432 }
6433 }
6434 else if (nbytes)
6435 insert_1 (m, nbytes, 1, 0, 0);
6436
6437 if (nlflag)
6438 {
6439 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6440 insert_1 ("\n", 1, 1, 0, 0);
6441
6442 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6443 this_bol = PT;
6444 this_bol_byte = PT_BYTE;
6445
6446 /* See if this line duplicates the previous one.
6447 If so, combine duplicates. */
6448 if (this_bol > BEG)
6449 {
6450 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6451 prev_bol = PT;
6452 prev_bol_byte = PT_BYTE;
6453
6454 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6455 this_bol, this_bol_byte);
6456 if (dup)
6457 {
6458 del_range_both (prev_bol, prev_bol_byte,
6459 this_bol, this_bol_byte, 0);
6460 if (dup > 1)
6461 {
6462 char dupstr[40];
6463 int duplen;
6464
6465 /* If you change this format, don't forget to also
6466 change message_log_check_duplicate. */
6467 sprintf (dupstr, " [%d times]", dup);
6468 duplen = strlen (dupstr);
6469 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6470 insert_1 (dupstr, duplen, 1, 0, 1);
6471 }
6472 }
6473 }
6474
6475 /* If we have more than the desired maximum number of lines
6476 in the *Messages* buffer now, delete the oldest ones.
6477 This is safe because we don't have undo in this buffer. */
6478
6479 if (NATNUMP (Vmessage_log_max))
6480 {
6481 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6482 -XFASTINT (Vmessage_log_max) - 1, 0);
6483 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6484 }
6485 }
6486 BEGV = XMARKER (oldbegv)->charpos;
6487 BEGV_BYTE = marker_byte_position (oldbegv);
6488
6489 if (zv_at_end)
6490 {
6491 ZV = Z;
6492 ZV_BYTE = Z_BYTE;
6493 }
6494 else
6495 {
6496 ZV = XMARKER (oldzv)->charpos;
6497 ZV_BYTE = marker_byte_position (oldzv);
6498 }
6499
6500 if (point_at_end)
6501 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6502 else
6503 /* We can't do Fgoto_char (oldpoint) because it will run some
6504 Lisp code. */
6505 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6506 XMARKER (oldpoint)->bytepos);
6507
6508 UNGCPRO;
6509 unchain_marker (XMARKER (oldpoint));
6510 unchain_marker (XMARKER (oldbegv));
6511 unchain_marker (XMARKER (oldzv));
6512
6513 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6514 set_buffer_internal (oldbuf);
6515 if (NILP (tem))
6516 windows_or_buffers_changed = old_windows_or_buffers_changed;
6517 message_log_need_newline = !nlflag;
6518 Vdeactivate_mark = old_deactivate_mark;
6519 }
6520 }
6521
6522
6523 /* We are at the end of the buffer after just having inserted a newline.
6524 (Note: We depend on the fact we won't be crossing the gap.)
6525 Check to see if the most recent message looks a lot like the previous one.
6526 Return 0 if different, 1 if the new one should just replace it, or a
6527 value N > 1 if we should also append " [N times]". */
6528
6529 static int
6530 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6531 int prev_bol, this_bol;
6532 int prev_bol_byte, this_bol_byte;
6533 {
6534 int i;
6535 int len = Z_BYTE - 1 - this_bol_byte;
6536 int seen_dots = 0;
6537 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6538 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6539
6540 for (i = 0; i < len; i++)
6541 {
6542 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6543 seen_dots = 1;
6544 if (p1[i] != p2[i])
6545 return seen_dots;
6546 }
6547 p1 += len;
6548 if (*p1 == '\n')
6549 return 2;
6550 if (*p1++ == ' ' && *p1++ == '[')
6551 {
6552 int n = 0;
6553 while (*p1 >= '0' && *p1 <= '9')
6554 n = n * 10 + *p1++ - '0';
6555 if (strncmp (p1, " times]\n", 8) == 0)
6556 return n+1;
6557 }
6558 return 0;
6559 }
6560
6561
6562 /* Display an echo area message M with a specified length of NBYTES
6563 bytes. The string may include null characters. If M is 0, clear
6564 out any existing message, and let the mini-buffer text show
6565 through.
6566
6567 The buffer M must continue to exist until after the echo area gets
6568 cleared or some other message gets displayed there. This means do
6569 not pass text that is stored in a Lisp string; do not pass text in
6570 a buffer that was alloca'd. */
6571
6572 void
6573 message2 (m, nbytes, multibyte)
6574 const char *m;
6575 int nbytes;
6576 int multibyte;
6577 {
6578 /* First flush out any partial line written with print. */
6579 message_log_maybe_newline ();
6580 if (m)
6581 message_dolog (m, nbytes, 1, multibyte);
6582 message2_nolog (m, nbytes, multibyte);
6583 }
6584
6585
6586 /* The non-logging counterpart of message2. */
6587
6588 void
6589 message2_nolog (m, nbytes, multibyte)
6590 const char *m;
6591 int nbytes, multibyte;
6592 {
6593 struct frame *sf = SELECTED_FRAME ();
6594 message_enable_multibyte = multibyte;
6595
6596 if (noninteractive)
6597 {
6598 if (noninteractive_need_newline)
6599 putc ('\n', stderr);
6600 noninteractive_need_newline = 0;
6601 if (m)
6602 fwrite (m, nbytes, 1, stderr);
6603 if (cursor_in_echo_area == 0)
6604 fprintf (stderr, "\n");
6605 fflush (stderr);
6606 }
6607 /* A null message buffer means that the frame hasn't really been
6608 initialized yet. Error messages get reported properly by
6609 cmd_error, so this must be just an informative message; toss it. */
6610 else if (INTERACTIVE
6611 && sf->glyphs_initialized_p
6612 && FRAME_MESSAGE_BUF (sf))
6613 {
6614 Lisp_Object mini_window;
6615 struct frame *f;
6616
6617 /* Get the frame containing the mini-buffer
6618 that the selected frame is using. */
6619 mini_window = FRAME_MINIBUF_WINDOW (sf);
6620 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6621
6622 FRAME_SAMPLE_VISIBILITY (f);
6623 if (FRAME_VISIBLE_P (sf)
6624 && ! FRAME_VISIBLE_P (f))
6625 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6626
6627 if (m)
6628 {
6629 set_message (m, Qnil, nbytes, multibyte);
6630 if (minibuffer_auto_raise)
6631 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6632 }
6633 else
6634 clear_message (1, 1);
6635
6636 do_pending_window_change (0);
6637 echo_area_display (1);
6638 do_pending_window_change (0);
6639 if (FRAME_DISPLAY (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
6640 (*FRAME_DISPLAY (f)->frame_up_to_date_hook) (f);
6641 }
6642 }
6643
6644
6645 /* Display an echo area message M with a specified length of NBYTES
6646 bytes. The string may include null characters. If M is not a
6647 string, clear out any existing message, and let the mini-buffer
6648 text show through. */
6649
6650 void
6651 message3 (m, nbytes, multibyte)
6652 Lisp_Object m;
6653 int nbytes;
6654 int multibyte;
6655 {
6656 struct gcpro gcpro1;
6657
6658 GCPRO1 (m);
6659
6660 /* First flush out any partial line written with print. */
6661 message_log_maybe_newline ();
6662 if (STRINGP (m))
6663 message_dolog (SDATA (m), nbytes, 1, multibyte);
6664 message3_nolog (m, nbytes, multibyte);
6665
6666 UNGCPRO;
6667 }
6668
6669
6670 /* The non-logging version of message3. */
6671
6672 void
6673 message3_nolog (m, nbytes, multibyte)
6674 Lisp_Object m;
6675 int nbytes, multibyte;
6676 {
6677 struct frame *sf = SELECTED_FRAME ();
6678 message_enable_multibyte = multibyte;
6679
6680 if (noninteractive)
6681 {
6682 if (noninteractive_need_newline)
6683 putc ('\n', stderr);
6684 noninteractive_need_newline = 0;
6685 if (STRINGP (m))
6686 fwrite (SDATA (m), nbytes, 1, stderr);
6687 if (cursor_in_echo_area == 0)
6688 fprintf (stderr, "\n");
6689 fflush (stderr);
6690 }
6691 /* A null message buffer means that the frame hasn't really been
6692 initialized yet. Error messages get reported properly by
6693 cmd_error, so this must be just an informative message; toss it. */
6694 else if (INTERACTIVE
6695 && sf->glyphs_initialized_p
6696 && FRAME_MESSAGE_BUF (sf))
6697 {
6698 Lisp_Object mini_window;
6699 Lisp_Object frame;
6700 struct frame *f;
6701
6702 /* Get the frame containing the mini-buffer
6703 that the selected frame is using. */
6704 mini_window = FRAME_MINIBUF_WINDOW (sf);
6705 frame = XWINDOW (mini_window)->frame;
6706 f = XFRAME (frame);
6707
6708 FRAME_SAMPLE_VISIBILITY (f);
6709 if (FRAME_VISIBLE_P (sf)
6710 && !FRAME_VISIBLE_P (f))
6711 Fmake_frame_visible (frame);
6712
6713 if (STRINGP (m) && SCHARS (m) > 0)
6714 {
6715 set_message (NULL, m, nbytes, multibyte);
6716 if (minibuffer_auto_raise)
6717 Fraise_frame (frame);
6718 }
6719 else
6720 clear_message (1, 1);
6721
6722 do_pending_window_change (0);
6723 echo_area_display (1);
6724 do_pending_window_change (0);
6725 if (FRAME_DISPLAY (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
6726 (*FRAME_DISPLAY (f)->frame_up_to_date_hook) (f);
6727 }
6728 }
6729
6730
6731 /* Display a null-terminated echo area message M. If M is 0, clear
6732 out any existing message, and let the mini-buffer text show through.
6733
6734 The buffer M must continue to exist until after the echo area gets
6735 cleared or some other message gets displayed there. Do not pass
6736 text that is stored in a Lisp string. Do not pass text in a buffer
6737 that was alloca'd. */
6738
6739 void
6740 message1 (m)
6741 char *m;
6742 {
6743 message2 (m, (m ? strlen (m) : 0), 0);
6744 }
6745
6746
6747 /* The non-logging counterpart of message1. */
6748
6749 void
6750 message1_nolog (m)
6751 char *m;
6752 {
6753 message2_nolog (m, (m ? strlen (m) : 0), 0);
6754 }
6755
6756 /* Display a message M which contains a single %s
6757 which gets replaced with STRING. */
6758
6759 void
6760 message_with_string (m, string, log)
6761 char *m;
6762 Lisp_Object string;
6763 int log;
6764 {
6765 CHECK_STRING (string);
6766
6767 if (noninteractive)
6768 {
6769 if (m)
6770 {
6771 if (noninteractive_need_newline)
6772 putc ('\n', stderr);
6773 noninteractive_need_newline = 0;
6774 fprintf (stderr, m, SDATA (string));
6775 if (cursor_in_echo_area == 0)
6776 fprintf (stderr, "\n");
6777 fflush (stderr);
6778 }
6779 }
6780 else if (INTERACTIVE)
6781 {
6782 /* The frame whose minibuffer we're going to display the message on.
6783 It may be larger than the selected frame, so we need
6784 to use its buffer, not the selected frame's buffer. */
6785 Lisp_Object mini_window;
6786 struct frame *f, *sf = SELECTED_FRAME ();
6787
6788 /* Get the frame containing the minibuffer
6789 that the selected frame is using. */
6790 mini_window = FRAME_MINIBUF_WINDOW (sf);
6791 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6792
6793 /* A null message buffer means that the frame hasn't really been
6794 initialized yet. Error messages get reported properly by
6795 cmd_error, so this must be just an informative message; toss it. */
6796 if (FRAME_MESSAGE_BUF (f))
6797 {
6798 Lisp_Object args[2], message;
6799 struct gcpro gcpro1, gcpro2;
6800
6801 args[0] = build_string (m);
6802 args[1] = message = string;
6803 GCPRO2 (args[0], message);
6804 gcpro1.nvars = 2;
6805
6806 message = Fformat (2, args);
6807
6808 if (log)
6809 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6810 else
6811 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6812
6813 UNGCPRO;
6814
6815 /* Print should start at the beginning of the message
6816 buffer next time. */
6817 message_buf_print = 0;
6818 }
6819 }
6820 }
6821
6822
6823 /* Dump an informative message to the minibuf. If M is 0, clear out
6824 any existing message, and let the mini-buffer text show through. */
6825
6826 /* VARARGS 1 */
6827 void
6828 message (m, a1, a2, a3)
6829 char *m;
6830 EMACS_INT a1, a2, a3;
6831 {
6832 if (noninteractive)
6833 {
6834 if (m)
6835 {
6836 if (noninteractive_need_newline)
6837 putc ('\n', stderr);
6838 noninteractive_need_newline = 0;
6839 fprintf (stderr, m, a1, a2, a3);
6840 if (cursor_in_echo_area == 0)
6841 fprintf (stderr, "\n");
6842 fflush (stderr);
6843 }
6844 }
6845 else if (INTERACTIVE)
6846 {
6847 /* The frame whose mini-buffer we're going to display the message
6848 on. It may be larger than the selected frame, so we need to
6849 use its buffer, not the selected frame's buffer. */
6850 Lisp_Object mini_window;
6851 struct frame *f, *sf = SELECTED_FRAME ();
6852
6853 /* Get the frame containing the mini-buffer
6854 that the selected frame is using. */
6855 mini_window = FRAME_MINIBUF_WINDOW (sf);
6856 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6857
6858 /* A null message buffer means that the frame hasn't really been
6859 initialized yet. Error messages get reported properly by
6860 cmd_error, so this must be just an informative message; toss
6861 it. */
6862 if (FRAME_MESSAGE_BUF (f))
6863 {
6864 if (m)
6865 {
6866 int len;
6867 #ifdef NO_ARG_ARRAY
6868 char *a[3];
6869 a[0] = (char *) a1;
6870 a[1] = (char *) a2;
6871 a[2] = (char *) a3;
6872
6873 len = doprnt (FRAME_MESSAGE_BUF (f),
6874 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6875 #else
6876 len = doprnt (FRAME_MESSAGE_BUF (f),
6877 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6878 (char **) &a1);
6879 #endif /* NO_ARG_ARRAY */
6880
6881 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6882 }
6883 else
6884 message1 (0);
6885
6886 /* Print should start at the beginning of the message
6887 buffer next time. */
6888 message_buf_print = 0;
6889 }
6890 }
6891 }
6892
6893
6894 /* The non-logging version of message. */
6895
6896 void
6897 message_nolog (m, a1, a2, a3)
6898 char *m;
6899 EMACS_INT a1, a2, a3;
6900 {
6901 Lisp_Object old_log_max;
6902 old_log_max = Vmessage_log_max;
6903 Vmessage_log_max = Qnil;
6904 message (m, a1, a2, a3);
6905 Vmessage_log_max = old_log_max;
6906 }
6907
6908
6909 /* Display the current message in the current mini-buffer. This is
6910 only called from error handlers in process.c, and is not time
6911 critical. */
6912
6913 void
6914 update_echo_area ()
6915 {
6916 if (!NILP (echo_area_buffer[0]))
6917 {
6918 Lisp_Object string;
6919 string = Fcurrent_message ();
6920 message3 (string, SBYTES (string),
6921 !NILP (current_buffer->enable_multibyte_characters));
6922 }
6923 }
6924
6925
6926 /* Make sure echo area buffers in `echo_buffers' are live.
6927 If they aren't, make new ones. */
6928
6929 static void
6930 ensure_echo_area_buffers ()
6931 {
6932 int i;
6933
6934 for (i = 0; i < 2; ++i)
6935 if (!BUFFERP (echo_buffer[i])
6936 || NILP (XBUFFER (echo_buffer[i])->name))
6937 {
6938 char name[30];
6939 Lisp_Object old_buffer;
6940 int j;
6941
6942 old_buffer = echo_buffer[i];
6943 sprintf (name, " *Echo Area %d*", i);
6944 echo_buffer[i] = Fget_buffer_create (build_string (name));
6945 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6946
6947 for (j = 0; j < 2; ++j)
6948 if (EQ (old_buffer, echo_area_buffer[j]))
6949 echo_area_buffer[j] = echo_buffer[i];
6950 }
6951 }
6952
6953
6954 /* Call FN with args A1..A4 with either the current or last displayed
6955 echo_area_buffer as current buffer.
6956
6957 WHICH zero means use the current message buffer
6958 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6959 from echo_buffer[] and clear it.
6960
6961 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6962 suitable buffer from echo_buffer[] and clear it.
6963
6964 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6965 that the current message becomes the last displayed one, make
6966 choose a suitable buffer for echo_area_buffer[0], and clear it.
6967
6968 Value is what FN returns. */
6969
6970 static int
6971 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6972 struct window *w;
6973 int which;
6974 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6975 EMACS_INT a1;
6976 Lisp_Object a2;
6977 EMACS_INT a3, a4;
6978 {
6979 Lisp_Object buffer;
6980 int this_one, the_other, clear_buffer_p, rc;
6981 int count = SPECPDL_INDEX ();
6982
6983 /* If buffers aren't live, make new ones. */
6984 ensure_echo_area_buffers ();
6985
6986 clear_buffer_p = 0;
6987
6988 if (which == 0)
6989 this_one = 0, the_other = 1;
6990 else if (which > 0)
6991 this_one = 1, the_other = 0;
6992 else
6993 {
6994 this_one = 0, the_other = 1;
6995 clear_buffer_p = 1;
6996
6997 /* We need a fresh one in case the current echo buffer equals
6998 the one containing the last displayed echo area message. */
6999 if (!NILP (echo_area_buffer[this_one])
7000 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
7001 echo_area_buffer[this_one] = Qnil;
7002 }
7003
7004 /* Choose a suitable buffer from echo_buffer[] is we don't
7005 have one. */
7006 if (NILP (echo_area_buffer[this_one]))
7007 {
7008 echo_area_buffer[this_one]
7009 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7010 ? echo_buffer[the_other]
7011 : echo_buffer[this_one]);
7012 clear_buffer_p = 1;
7013 }
7014
7015 buffer = echo_area_buffer[this_one];
7016
7017 /* Don't get confused by reusing the buffer used for echoing
7018 for a different purpose. */
7019 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7020 cancel_echoing ();
7021
7022 record_unwind_protect (unwind_with_echo_area_buffer,
7023 with_echo_area_buffer_unwind_data (w));
7024
7025 /* Make the echo area buffer current. Note that for display
7026 purposes, it is not necessary that the displayed window's buffer
7027 == current_buffer, except for text property lookup. So, let's
7028 only set that buffer temporarily here without doing a full
7029 Fset_window_buffer. We must also change w->pointm, though,
7030 because otherwise an assertions in unshow_buffer fails, and Emacs
7031 aborts. */
7032 set_buffer_internal_1 (XBUFFER (buffer));
7033 if (w)
7034 {
7035 w->buffer = buffer;
7036 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7037 }
7038
7039 current_buffer->undo_list = Qt;
7040 current_buffer->read_only = Qnil;
7041 specbind (Qinhibit_read_only, Qt);
7042 specbind (Qinhibit_modification_hooks, Qt);
7043
7044 if (clear_buffer_p && Z > BEG)
7045 del_range (BEG, Z);
7046
7047 xassert (BEGV >= BEG);
7048 xassert (ZV <= Z && ZV >= BEGV);
7049
7050 rc = fn (a1, a2, a3, a4);
7051
7052 xassert (BEGV >= BEG);
7053 xassert (ZV <= Z && ZV >= BEGV);
7054
7055 unbind_to (count, Qnil);
7056 return rc;
7057 }
7058
7059
7060 /* Save state that should be preserved around the call to the function
7061 FN called in with_echo_area_buffer. */
7062
7063 static Lisp_Object
7064 with_echo_area_buffer_unwind_data (w)
7065 struct window *w;
7066 {
7067 int i = 0;
7068 Lisp_Object vector;
7069
7070 /* Reduce consing by keeping one vector in
7071 Vwith_echo_area_save_vector. */
7072 vector = Vwith_echo_area_save_vector;
7073 Vwith_echo_area_save_vector = Qnil;
7074
7075 if (NILP (vector))
7076 vector = Fmake_vector (make_number (7), Qnil);
7077
7078 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7079 AREF (vector, i) = Vdeactivate_mark, ++i;
7080 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7081
7082 if (w)
7083 {
7084 XSETWINDOW (AREF (vector, i), w); ++i;
7085 AREF (vector, i) = w->buffer; ++i;
7086 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7087 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7088 }
7089 else
7090 {
7091 int end = i + 4;
7092 for (; i < end; ++i)
7093 AREF (vector, i) = Qnil;
7094 }
7095
7096 xassert (i == ASIZE (vector));
7097 return vector;
7098 }
7099
7100
7101 /* Restore global state from VECTOR which was created by
7102 with_echo_area_buffer_unwind_data. */
7103
7104 static Lisp_Object
7105 unwind_with_echo_area_buffer (vector)
7106 Lisp_Object vector;
7107 {
7108 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7109 Vdeactivate_mark = AREF (vector, 1);
7110 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7111
7112 if (WINDOWP (AREF (vector, 3)))
7113 {
7114 struct window *w;
7115 Lisp_Object buffer, charpos, bytepos;
7116
7117 w = XWINDOW (AREF (vector, 3));
7118 buffer = AREF (vector, 4);
7119 charpos = AREF (vector, 5);
7120 bytepos = AREF (vector, 6);
7121
7122 w->buffer = buffer;
7123 set_marker_both (w->pointm, buffer,
7124 XFASTINT (charpos), XFASTINT (bytepos));
7125 }
7126
7127 Vwith_echo_area_save_vector = vector;
7128 return Qnil;
7129 }
7130
7131
7132 /* Set up the echo area for use by print functions. MULTIBYTE_P
7133 non-zero means we will print multibyte. */
7134
7135 void
7136 setup_echo_area_for_printing (multibyte_p)
7137 int multibyte_p;
7138 {
7139 /* If we can't find an echo area any more, exit. */
7140 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7141 Fkill_emacs (Qnil);
7142
7143 ensure_echo_area_buffers ();
7144
7145 if (!message_buf_print)
7146 {
7147 /* A message has been output since the last time we printed.
7148 Choose a fresh echo area buffer. */
7149 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7150 echo_area_buffer[0] = echo_buffer[1];
7151 else
7152 echo_area_buffer[0] = echo_buffer[0];
7153
7154 /* Switch to that buffer and clear it. */
7155 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7156 current_buffer->truncate_lines = Qnil;
7157
7158 if (Z > BEG)
7159 {
7160 int count = SPECPDL_INDEX ();
7161 specbind (Qinhibit_read_only, Qt);
7162 /* Note that undo recording is always disabled. */
7163 del_range (BEG, Z);
7164 unbind_to (count, Qnil);
7165 }
7166 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7167
7168 /* Set up the buffer for the multibyteness we need. */
7169 if (multibyte_p
7170 != !NILP (current_buffer->enable_multibyte_characters))
7171 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7172
7173 /* Raise the frame containing the echo area. */
7174 if (minibuffer_auto_raise)
7175 {
7176 struct frame *sf = SELECTED_FRAME ();
7177 Lisp_Object mini_window;
7178 mini_window = FRAME_MINIBUF_WINDOW (sf);
7179 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7180 }
7181
7182 message_log_maybe_newline ();
7183 message_buf_print = 1;
7184 }
7185 else
7186 {
7187 if (NILP (echo_area_buffer[0]))
7188 {
7189 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7190 echo_area_buffer[0] = echo_buffer[1];
7191 else
7192 echo_area_buffer[0] = echo_buffer[0];
7193 }
7194
7195 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7196 {
7197 /* Someone switched buffers between print requests. */
7198 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7199 current_buffer->truncate_lines = Qnil;
7200 }
7201 }
7202 }
7203
7204
7205 /* Display an echo area message in window W. Value is non-zero if W's
7206 height is changed. If display_last_displayed_message_p is
7207 non-zero, display the message that was last displayed, otherwise
7208 display the current message. */
7209
7210 static int
7211 display_echo_area (w)
7212 struct window *w;
7213 {
7214 int i, no_message_p, window_height_changed_p, count;
7215
7216 /* Temporarily disable garbage collections while displaying the echo
7217 area. This is done because a GC can print a message itself.
7218 That message would modify the echo area buffer's contents while a
7219 redisplay of the buffer is going on, and seriously confuse
7220 redisplay. */
7221 count = inhibit_garbage_collection ();
7222
7223 /* If there is no message, we must call display_echo_area_1
7224 nevertheless because it resizes the window. But we will have to
7225 reset the echo_area_buffer in question to nil at the end because
7226 with_echo_area_buffer will sets it to an empty buffer. */
7227 i = display_last_displayed_message_p ? 1 : 0;
7228 no_message_p = NILP (echo_area_buffer[i]);
7229
7230 window_height_changed_p
7231 = with_echo_area_buffer (w, display_last_displayed_message_p,
7232 display_echo_area_1,
7233 (EMACS_INT) w, Qnil, 0, 0);
7234
7235 if (no_message_p)
7236 echo_area_buffer[i] = Qnil;
7237
7238 unbind_to (count, Qnil);
7239 return window_height_changed_p;
7240 }
7241
7242
7243 /* Helper for display_echo_area. Display the current buffer which
7244 contains the current echo area message in window W, a mini-window,
7245 a pointer to which is passed in A1. A2..A4 are currently not used.
7246 Change the height of W so that all of the message is displayed.
7247 Value is non-zero if height of W was changed. */
7248
7249 static int
7250 display_echo_area_1 (a1, a2, a3, a4)
7251 EMACS_INT a1;
7252 Lisp_Object a2;
7253 EMACS_INT a3, a4;
7254 {
7255 struct window *w = (struct window *) a1;
7256 Lisp_Object window;
7257 struct text_pos start;
7258 int window_height_changed_p = 0;
7259
7260 /* Do this before displaying, so that we have a large enough glyph
7261 matrix for the display. */
7262 window_height_changed_p = resize_mini_window (w, 0);
7263
7264 /* Display. */
7265 clear_glyph_matrix (w->desired_matrix);
7266 XSETWINDOW (window, w);
7267 SET_TEXT_POS (start, BEG, BEG_BYTE);
7268 try_window (window, start);
7269
7270 return window_height_changed_p;
7271 }
7272
7273
7274 /* Resize the echo area window to exactly the size needed for the
7275 currently displayed message, if there is one. If a mini-buffer
7276 is active, don't shrink it. */
7277
7278 void
7279 resize_echo_area_exactly ()
7280 {
7281 if (BUFFERP (echo_area_buffer[0])
7282 && WINDOWP (echo_area_window))
7283 {
7284 struct window *w = XWINDOW (echo_area_window);
7285 int resized_p;
7286 Lisp_Object resize_exactly;
7287
7288 if (minibuf_level == 0)
7289 resize_exactly = Qt;
7290 else
7291 resize_exactly = Qnil;
7292
7293 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7294 (EMACS_INT) w, resize_exactly, 0, 0);
7295 if (resized_p)
7296 {
7297 ++windows_or_buffers_changed;
7298 ++update_mode_lines;
7299 redisplay_internal (0);
7300 }
7301 }
7302 }
7303
7304
7305 /* Callback function for with_echo_area_buffer, when used from
7306 resize_echo_area_exactly. A1 contains a pointer to the window to
7307 resize, EXACTLY non-nil means resize the mini-window exactly to the
7308 size of the text displayed. A3 and A4 are not used. Value is what
7309 resize_mini_window returns. */
7310
7311 static int
7312 resize_mini_window_1 (a1, exactly, a3, a4)
7313 EMACS_INT a1;
7314 Lisp_Object exactly;
7315 EMACS_INT a3, a4;
7316 {
7317 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7318 }
7319
7320
7321 /* Resize mini-window W to fit the size of its contents. EXACT:P
7322 means size the window exactly to the size needed. Otherwise, it's
7323 only enlarged until W's buffer is empty. Value is non-zero if
7324 the window height has been changed. */
7325
7326 int
7327 resize_mini_window (w, exact_p)
7328 struct window *w;
7329 int exact_p;
7330 {
7331 struct frame *f = XFRAME (w->frame);
7332 int window_height_changed_p = 0;
7333
7334 xassert (MINI_WINDOW_P (w));
7335
7336 /* Don't resize windows while redisplaying a window; it would
7337 confuse redisplay functions when the size of the window they are
7338 displaying changes from under them. Such a resizing can happen,
7339 for instance, when which-func prints a long message while
7340 we are running fontification-functions. We're running these
7341 functions with safe_call which binds inhibit-redisplay to t. */
7342 if (!NILP (Vinhibit_redisplay))
7343 return 0;
7344
7345 /* Nil means don't try to resize. */
7346 if (NILP (Vresize_mini_windows)
7347 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7348 return 0;
7349
7350 if (!FRAME_MINIBUF_ONLY_P (f))
7351 {
7352 struct it it;
7353 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7354 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7355 int height, max_height;
7356 int unit = FRAME_LINE_HEIGHT (f);
7357 struct text_pos start;
7358 struct buffer *old_current_buffer = NULL;
7359
7360 if (current_buffer != XBUFFER (w->buffer))
7361 {
7362 old_current_buffer = current_buffer;
7363 set_buffer_internal (XBUFFER (w->buffer));
7364 }
7365
7366 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7367
7368 /* Compute the max. number of lines specified by the user. */
7369 if (FLOATP (Vmax_mini_window_height))
7370 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7371 else if (INTEGERP (Vmax_mini_window_height))
7372 max_height = XINT (Vmax_mini_window_height);
7373 else
7374 max_height = total_height / 4;
7375
7376 /* Correct that max. height if it's bogus. */
7377 max_height = max (1, max_height);
7378 max_height = min (total_height, max_height);
7379
7380 /* Find out the height of the text in the window. */
7381 if (it.truncate_lines_p)
7382 height = 1;
7383 else
7384 {
7385 last_height = 0;
7386 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7387 if (it.max_ascent == 0 && it.max_descent == 0)
7388 height = it.current_y + last_height;
7389 else
7390 height = it.current_y + it.max_ascent + it.max_descent;
7391 height -= it.extra_line_spacing;
7392 height = (height + unit - 1) / unit;
7393 }
7394
7395 /* Compute a suitable window start. */
7396 if (height > max_height)
7397 {
7398 height = max_height;
7399 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7400 move_it_vertically_backward (&it, (height - 1) * unit);
7401 start = it.current.pos;
7402 }
7403 else
7404 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7405 SET_MARKER_FROM_TEXT_POS (w->start, start);
7406
7407 if (EQ (Vresize_mini_windows, Qgrow_only))
7408 {
7409 /* Let it grow only, until we display an empty message, in which
7410 case the window shrinks again. */
7411 if (height > WINDOW_TOTAL_LINES (w))
7412 {
7413 int old_height = WINDOW_TOTAL_LINES (w);
7414 freeze_window_starts (f, 1);
7415 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7416 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7417 }
7418 else if (height < WINDOW_TOTAL_LINES (w)
7419 && (exact_p || BEGV == ZV))
7420 {
7421 int old_height = WINDOW_TOTAL_LINES (w);
7422 freeze_window_starts (f, 0);
7423 shrink_mini_window (w);
7424 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7425 }
7426 }
7427 else
7428 {
7429 /* Always resize to exact size needed. */
7430 if (height > WINDOW_TOTAL_LINES (w))
7431 {
7432 int old_height = WINDOW_TOTAL_LINES (w);
7433 freeze_window_starts (f, 1);
7434 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7435 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7436 }
7437 else if (height < WINDOW_TOTAL_LINES (w))
7438 {
7439 int old_height = WINDOW_TOTAL_LINES (w);
7440 freeze_window_starts (f, 0);
7441 shrink_mini_window (w);
7442
7443 if (height)
7444 {
7445 freeze_window_starts (f, 1);
7446 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7447 }
7448
7449 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7450 }
7451 }
7452
7453 if (old_current_buffer)
7454 set_buffer_internal (old_current_buffer);
7455 }
7456
7457 return window_height_changed_p;
7458 }
7459
7460
7461 /* Value is the current message, a string, or nil if there is no
7462 current message. */
7463
7464 Lisp_Object
7465 current_message ()
7466 {
7467 Lisp_Object msg;
7468
7469 if (NILP (echo_area_buffer[0]))
7470 msg = Qnil;
7471 else
7472 {
7473 with_echo_area_buffer (0, 0, current_message_1,
7474 (EMACS_INT) &msg, Qnil, 0, 0);
7475 if (NILP (msg))
7476 echo_area_buffer[0] = Qnil;
7477 }
7478
7479 return msg;
7480 }
7481
7482
7483 static int
7484 current_message_1 (a1, a2, a3, a4)
7485 EMACS_INT a1;
7486 Lisp_Object a2;
7487 EMACS_INT a3, a4;
7488 {
7489 Lisp_Object *msg = (Lisp_Object *) a1;
7490
7491 if (Z > BEG)
7492 *msg = make_buffer_string (BEG, Z, 1);
7493 else
7494 *msg = Qnil;
7495 return 0;
7496 }
7497
7498
7499 /* Push the current message on Vmessage_stack for later restauration
7500 by restore_message. Value is non-zero if the current message isn't
7501 empty. This is a relatively infrequent operation, so it's not
7502 worth optimizing. */
7503
7504 int
7505 push_message ()
7506 {
7507 Lisp_Object msg;
7508 msg = current_message ();
7509 Vmessage_stack = Fcons (msg, Vmessage_stack);
7510 return STRINGP (msg);
7511 }
7512
7513
7514 /* Restore message display from the top of Vmessage_stack. */
7515
7516 void
7517 restore_message ()
7518 {
7519 Lisp_Object msg;
7520
7521 xassert (CONSP (Vmessage_stack));
7522 msg = XCAR (Vmessage_stack);
7523 if (STRINGP (msg))
7524 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7525 else
7526 message3_nolog (msg, 0, 0);
7527 }
7528
7529
7530 /* Handler for record_unwind_protect calling pop_message. */
7531
7532 Lisp_Object
7533 pop_message_unwind (dummy)
7534 Lisp_Object dummy;
7535 {
7536 pop_message ();
7537 return Qnil;
7538 }
7539
7540 /* Pop the top-most entry off Vmessage_stack. */
7541
7542 void
7543 pop_message ()
7544 {
7545 xassert (CONSP (Vmessage_stack));
7546 Vmessage_stack = XCDR (Vmessage_stack);
7547 }
7548
7549
7550 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7551 exits. If the stack is not empty, we have a missing pop_message
7552 somewhere. */
7553
7554 void
7555 check_message_stack ()
7556 {
7557 if (!NILP (Vmessage_stack))
7558 abort ();
7559 }
7560
7561
7562 /* Truncate to NCHARS what will be displayed in the echo area the next
7563 time we display it---but don't redisplay it now. */
7564
7565 void
7566 truncate_echo_area (nchars)
7567 int nchars;
7568 {
7569 if (nchars == 0)
7570 echo_area_buffer[0] = Qnil;
7571 /* A null message buffer means that the frame hasn't really been
7572 initialized yet. Error messages get reported properly by
7573 cmd_error, so this must be just an informative message; toss it. */
7574 else if (!noninteractive
7575 && INTERACTIVE
7576 && !NILP (echo_area_buffer[0]))
7577 {
7578 struct frame *sf = SELECTED_FRAME ();
7579 if (FRAME_MESSAGE_BUF (sf))
7580 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7581 }
7582 }
7583
7584
7585 /* Helper function for truncate_echo_area. Truncate the current
7586 message to at most NCHARS characters. */
7587
7588 static int
7589 truncate_message_1 (nchars, a2, a3, a4)
7590 EMACS_INT nchars;
7591 Lisp_Object a2;
7592 EMACS_INT a3, a4;
7593 {
7594 if (BEG + nchars < Z)
7595 del_range (BEG + nchars, Z);
7596 if (Z == BEG)
7597 echo_area_buffer[0] = Qnil;
7598 return 0;
7599 }
7600
7601
7602 /* Set the current message to a substring of S or STRING.
7603
7604 If STRING is a Lisp string, set the message to the first NBYTES
7605 bytes from STRING. NBYTES zero means use the whole string. If
7606 STRING is multibyte, the message will be displayed multibyte.
7607
7608 If S is not null, set the message to the first LEN bytes of S. LEN
7609 zero means use the whole string. MULTIBYTE_P non-zero means S is
7610 multibyte. Display the message multibyte in that case. */
7611
7612 void
7613 set_message (s, string, nbytes, multibyte_p)
7614 const char *s;
7615 Lisp_Object string;
7616 int nbytes, multibyte_p;
7617 {
7618 message_enable_multibyte
7619 = ((s && multibyte_p)
7620 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7621
7622 with_echo_area_buffer (0, -1, set_message_1,
7623 (EMACS_INT) s, string, nbytes, multibyte_p);
7624 message_buf_print = 0;
7625 help_echo_showing_p = 0;
7626 }
7627
7628
7629 /* Helper function for set_message. Arguments have the same meaning
7630 as there, with A1 corresponding to S and A2 corresponding to STRING
7631 This function is called with the echo area buffer being
7632 current. */
7633
7634 static int
7635 set_message_1 (a1, a2, nbytes, multibyte_p)
7636 EMACS_INT a1;
7637 Lisp_Object a2;
7638 EMACS_INT nbytes, multibyte_p;
7639 {
7640 const char *s = (const char *) a1;
7641 Lisp_Object string = a2;
7642
7643 xassert (BEG == Z);
7644
7645 /* Change multibyteness of the echo buffer appropriately. */
7646 if (message_enable_multibyte
7647 != !NILP (current_buffer->enable_multibyte_characters))
7648 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7649
7650 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7651
7652 /* Insert new message at BEG. */
7653 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7654
7655 if (STRINGP (string))
7656 {
7657 int nchars;
7658
7659 if (nbytes == 0)
7660 nbytes = SBYTES (string);
7661 nchars = string_byte_to_char (string, nbytes);
7662
7663 /* This function takes care of single/multibyte conversion. We
7664 just have to ensure that the echo area buffer has the right
7665 setting of enable_multibyte_characters. */
7666 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7667 }
7668 else if (s)
7669 {
7670 if (nbytes == 0)
7671 nbytes = strlen (s);
7672
7673 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7674 {
7675 /* Convert from multi-byte to single-byte. */
7676 int i, c, n;
7677 unsigned char work[1];
7678
7679 /* Convert a multibyte string to single-byte. */
7680 for (i = 0; i < nbytes; i += n)
7681 {
7682 c = string_char_and_length (s + i, nbytes - i, &n);
7683 work[0] = (SINGLE_BYTE_CHAR_P (c)
7684 ? c
7685 : multibyte_char_to_unibyte (c, Qnil));
7686 insert_1_both (work, 1, 1, 1, 0, 0);
7687 }
7688 }
7689 else if (!multibyte_p
7690 && !NILP (current_buffer->enable_multibyte_characters))
7691 {
7692 /* Convert from single-byte to multi-byte. */
7693 int i, c, n;
7694 const unsigned char *msg = (const unsigned char *) s;
7695 unsigned char str[MAX_MULTIBYTE_LENGTH];
7696
7697 /* Convert a single-byte string to multibyte. */
7698 for (i = 0; i < nbytes; i++)
7699 {
7700 c = unibyte_char_to_multibyte (msg[i]);
7701 n = CHAR_STRING (c, str);
7702 insert_1_both (str, 1, n, 1, 0, 0);
7703 }
7704 }
7705 else
7706 insert_1 (s, nbytes, 1, 0, 0);
7707 }
7708
7709 return 0;
7710 }
7711
7712
7713 /* Clear messages. CURRENT_P non-zero means clear the current
7714 message. LAST_DISPLAYED_P non-zero means clear the message
7715 last displayed. */
7716
7717 void
7718 clear_message (current_p, last_displayed_p)
7719 int current_p, last_displayed_p;
7720 {
7721 if (current_p)
7722 {
7723 echo_area_buffer[0] = Qnil;
7724 message_cleared_p = 1;
7725 }
7726
7727 if (last_displayed_p)
7728 echo_area_buffer[1] = Qnil;
7729
7730 message_buf_print = 0;
7731 }
7732
7733 /* Clear garbaged frames.
7734
7735 This function is used where the old redisplay called
7736 redraw_garbaged_frames which in turn called redraw_frame which in
7737 turn called clear_frame. The call to clear_frame was a source of
7738 flickering. I believe a clear_frame is not necessary. It should
7739 suffice in the new redisplay to invalidate all current matrices,
7740 and ensure a complete redisplay of all windows. */
7741
7742 static void
7743 clear_garbaged_frames ()
7744 {
7745 if (frame_garbaged)
7746 {
7747 Lisp_Object tail, frame;
7748 int changed_count = 0;
7749
7750 FOR_EACH_FRAME (tail, frame)
7751 {
7752 struct frame *f = XFRAME (frame);
7753
7754 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7755 {
7756 if (f->resized_p)
7757 {
7758 Fredraw_frame (frame);
7759 f->force_flush_display_p = 1;
7760 }
7761 clear_current_matrices (f);
7762 changed_count++;
7763 f->garbaged = 0;
7764 f->resized_p = 0;
7765 }
7766 }
7767
7768 frame_garbaged = 0;
7769 if (changed_count)
7770 ++windows_or_buffers_changed;
7771 }
7772 }
7773
7774
7775 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7776 is non-zero update selected_frame. Value is non-zero if the
7777 mini-windows height has been changed. */
7778
7779 static int
7780 echo_area_display (update_frame_p)
7781 int update_frame_p;
7782 {
7783 Lisp_Object mini_window;
7784 struct window *w;
7785 struct frame *f;
7786 int window_height_changed_p = 0;
7787 struct frame *sf = SELECTED_FRAME ();
7788
7789 mini_window = FRAME_MINIBUF_WINDOW (sf);
7790 w = XWINDOW (mini_window);
7791 f = XFRAME (WINDOW_FRAME (w));
7792
7793 /* Don't display if frame is invisible or not yet initialized. */
7794 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7795 return 0;
7796
7797 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7798 #ifndef MAC_OS8
7799 #ifdef HAVE_WINDOW_SYSTEM
7800 /* When Emacs starts, selected_frame may be the initial terminal
7801 frame. If we let this through, a message would be displayed on
7802 the terminal. */
7803 if (FRAME_TERMCAP_P (XFRAME (selected_frame))
7804 && FRAME_TTY (XFRAME (selected_frame))->type == NULL)
7805 return 0;
7806 #endif /* HAVE_WINDOW_SYSTEM */
7807 #endif
7808
7809 /* Redraw garbaged frames. */
7810 if (frame_garbaged)
7811 clear_garbaged_frames ();
7812
7813 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7814 {
7815 echo_area_window = mini_window;
7816 window_height_changed_p = display_echo_area (w);
7817 w->must_be_updated_p = 1;
7818
7819 /* Update the display, unless called from redisplay_internal.
7820 Also don't update the screen during redisplay itself. The
7821 update will happen at the end of redisplay, and an update
7822 here could cause confusion. */
7823 if (update_frame_p && !redisplaying_p)
7824 {
7825 int n = 0;
7826
7827 /* If the display update has been interrupted by pending
7828 input, update mode lines in the frame. Due to the
7829 pending input, it might have been that redisplay hasn't
7830 been called, so that mode lines above the echo area are
7831 garbaged. This looks odd, so we prevent it here. */
7832 if (!display_completed)
7833 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7834
7835 if (window_height_changed_p
7836 /* Don't do this if Emacs is shutting down. Redisplay
7837 needs to run hooks. */
7838 && !NILP (Vrun_hooks))
7839 {
7840 /* Must update other windows. Likewise as in other
7841 cases, don't let this update be interrupted by
7842 pending input. */
7843 int count = SPECPDL_INDEX ();
7844 specbind (Qredisplay_dont_pause, Qt);
7845 windows_or_buffers_changed = 1;
7846 redisplay_internal (0);
7847 unbind_to (count, Qnil);
7848 }
7849 else if (FRAME_WINDOW_P (f) && n == 0)
7850 {
7851 /* Window configuration is the same as before.
7852 Can do with a display update of the echo area,
7853 unless we displayed some mode lines. */
7854 update_single_window (w, 1);
7855 FRAME_RIF (f)->flush_display (f);
7856 }
7857 else
7858 update_frame (f, 1, 1);
7859
7860 /* If cursor is in the echo area, make sure that the next
7861 redisplay displays the minibuffer, so that the cursor will
7862 be replaced with what the minibuffer wants. */
7863 if (cursor_in_echo_area)
7864 ++windows_or_buffers_changed;
7865 }
7866 }
7867 else if (!EQ (mini_window, selected_window))
7868 windows_or_buffers_changed++;
7869
7870 /* Last displayed message is now the current message. */
7871 echo_area_buffer[1] = echo_area_buffer[0];
7872
7873 /* Prevent redisplay optimization in redisplay_internal by resetting
7874 this_line_start_pos. This is done because the mini-buffer now
7875 displays the message instead of its buffer text. */
7876 if (EQ (mini_window, selected_window))
7877 CHARPOS (this_line_start_pos) = 0;
7878
7879 return window_height_changed_p;
7880 }
7881
7882
7883 \f
7884 /***********************************************************************
7885 Frame Titles
7886 ***********************************************************************/
7887
7888
7889 /* The frame title buffering code is also used by Fformat_mode_line.
7890 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
7891
7892 /* A buffer for constructing frame titles in it; allocated from the
7893 heap in init_xdisp and resized as needed in store_frame_title_char. */
7894
7895 static char *frame_title_buf;
7896
7897 /* The buffer's end, and a current output position in it. */
7898
7899 static char *frame_title_buf_end;
7900 static char *frame_title_ptr;
7901
7902
7903 /* Store a single character C for the frame title in frame_title_buf.
7904 Re-allocate frame_title_buf if necessary. */
7905
7906 static void
7907 #ifdef PROTOTYPES
7908 store_frame_title_char (char c)
7909 #else
7910 store_frame_title_char (c)
7911 char c;
7912 #endif
7913 {
7914 /* If output position has reached the end of the allocated buffer,
7915 double the buffer's size. */
7916 if (frame_title_ptr == frame_title_buf_end)
7917 {
7918 int len = frame_title_ptr - frame_title_buf;
7919 int new_size = 2 * len * sizeof *frame_title_buf;
7920 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7921 frame_title_buf_end = frame_title_buf + new_size;
7922 frame_title_ptr = frame_title_buf + len;
7923 }
7924
7925 *frame_title_ptr++ = c;
7926 }
7927
7928
7929 /* Store part of a frame title in frame_title_buf, beginning at
7930 frame_title_ptr. STR is the string to store. Do not copy
7931 characters that yield more columns than PRECISION; PRECISION <= 0
7932 means copy the whole string. Pad with spaces until FIELD_WIDTH
7933 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7934 pad. Called from display_mode_element when it is used to build a
7935 frame title. */
7936
7937 static int
7938 store_frame_title (str, field_width, precision)
7939 const unsigned char *str;
7940 int field_width, precision;
7941 {
7942 int n = 0;
7943 int dummy, nbytes;
7944
7945 /* Copy at most PRECISION chars from STR. */
7946 nbytes = strlen (str);
7947 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7948 while (nbytes--)
7949 store_frame_title_char (*str++);
7950
7951 /* Fill up with spaces until FIELD_WIDTH reached. */
7952 while (field_width > 0
7953 && n < field_width)
7954 {
7955 store_frame_title_char (' ');
7956 ++n;
7957 }
7958
7959 return n;
7960 }
7961
7962 #ifdef HAVE_WINDOW_SYSTEM
7963
7964 /* Set the title of FRAME, if it has changed. The title format is
7965 Vicon_title_format if FRAME is iconified, otherwise it is
7966 frame_title_format. */
7967
7968 static void
7969 x_consider_frame_title (frame)
7970 Lisp_Object frame;
7971 {
7972 struct frame *f = XFRAME (frame);
7973
7974 if (FRAME_WINDOW_P (f)
7975 || FRAME_MINIBUF_ONLY_P (f)
7976 || f->explicit_name)
7977 {
7978 /* Do we have more than one visible frame on this X display? */
7979 Lisp_Object tail;
7980 Lisp_Object fmt;
7981 struct buffer *obuf;
7982 int len;
7983 struct it it;
7984
7985 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7986 {
7987 Lisp_Object other_frame = XCAR (tail);
7988 struct frame *tf = XFRAME (other_frame);
7989
7990 if (tf != f
7991 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7992 && !FRAME_MINIBUF_ONLY_P (tf)
7993 && !EQ (other_frame, tip_frame)
7994 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7995 break;
7996 }
7997
7998 /* Set global variable indicating that multiple frames exist. */
7999 multiple_frames = CONSP (tail);
8000
8001 /* Switch to the buffer of selected window of the frame. Set up
8002 frame_title_ptr so that display_mode_element will output into it;
8003 then display the title. */
8004 obuf = current_buffer;
8005 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8006 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8007 frame_title_ptr = frame_title_buf;
8008 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8009 NULL, DEFAULT_FACE_ID);
8010 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8011 len = frame_title_ptr - frame_title_buf;
8012 frame_title_ptr = NULL;
8013 set_buffer_internal_1 (obuf);
8014
8015 /* Set the title only if it's changed. This avoids consing in
8016 the common case where it hasn't. (If it turns out that we've
8017 already wasted too much time by walking through the list with
8018 display_mode_element, then we might need to optimize at a
8019 higher level than this.) */
8020 if (! STRINGP (f->name)
8021 || SBYTES (f->name) != len
8022 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
8023 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
8024 }
8025 }
8026
8027 #endif /* not HAVE_WINDOW_SYSTEM */
8028
8029
8030
8031 \f
8032 /***********************************************************************
8033 Menu Bars
8034 ***********************************************************************/
8035
8036
8037 /* Prepare for redisplay by updating menu-bar item lists when
8038 appropriate. This can call eval. */
8039
8040 void
8041 prepare_menu_bars ()
8042 {
8043 int all_windows;
8044 struct gcpro gcpro1, gcpro2;
8045 struct frame *f;
8046 Lisp_Object tooltip_frame;
8047
8048 #ifdef HAVE_WINDOW_SYSTEM
8049 tooltip_frame = tip_frame;
8050 #else
8051 tooltip_frame = Qnil;
8052 #endif
8053
8054 /* Update all frame titles based on their buffer names, etc. We do
8055 this before the menu bars so that the buffer-menu will show the
8056 up-to-date frame titles. */
8057 #ifdef HAVE_WINDOW_SYSTEM
8058 if (windows_or_buffers_changed || update_mode_lines)
8059 {
8060 Lisp_Object tail, frame;
8061
8062 FOR_EACH_FRAME (tail, frame)
8063 {
8064 f = XFRAME (frame);
8065 if (!EQ (frame, tooltip_frame)
8066 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8067 x_consider_frame_title (frame);
8068 }
8069 }
8070 #endif /* HAVE_WINDOW_SYSTEM */
8071
8072 /* Update the menu bar item lists, if appropriate. This has to be
8073 done before any actual redisplay or generation of display lines. */
8074 all_windows = (update_mode_lines
8075 || buffer_shared > 1
8076 || windows_or_buffers_changed);
8077 if (all_windows)
8078 {
8079 Lisp_Object tail, frame;
8080 int count = SPECPDL_INDEX ();
8081
8082 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8083
8084 FOR_EACH_FRAME (tail, frame)
8085 {
8086 f = XFRAME (frame);
8087
8088 /* Ignore tooltip frame. */
8089 if (EQ (frame, tooltip_frame))
8090 continue;
8091
8092 /* If a window on this frame changed size, report that to
8093 the user and clear the size-change flag. */
8094 if (FRAME_WINDOW_SIZES_CHANGED (f))
8095 {
8096 Lisp_Object functions;
8097
8098 /* Clear flag first in case we get an error below. */
8099 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8100 functions = Vwindow_size_change_functions;
8101 GCPRO2 (tail, functions);
8102
8103 while (CONSP (functions))
8104 {
8105 call1 (XCAR (functions), frame);
8106 functions = XCDR (functions);
8107 }
8108 UNGCPRO;
8109 }
8110
8111 GCPRO1 (tail);
8112 update_menu_bar (f, 0);
8113 #ifdef HAVE_WINDOW_SYSTEM
8114 update_tool_bar (f, 0);
8115 #endif
8116 UNGCPRO;
8117 }
8118
8119 unbind_to (count, Qnil);
8120 }
8121 else
8122 {
8123 struct frame *sf = SELECTED_FRAME ();
8124 update_menu_bar (sf, 1);
8125 #ifdef HAVE_WINDOW_SYSTEM
8126 update_tool_bar (sf, 1);
8127 #endif
8128 }
8129
8130 /* Motif needs this. See comment in xmenu.c. Turn it off when
8131 pending_menu_activation is not defined. */
8132 #ifdef USE_X_TOOLKIT
8133 pending_menu_activation = 0;
8134 #endif
8135 }
8136
8137
8138 /* Update the menu bar item list for frame F. This has to be done
8139 before we start to fill in any display lines, because it can call
8140 eval.
8141
8142 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8143
8144 static void
8145 update_menu_bar (f, save_match_data)
8146 struct frame *f;
8147 int save_match_data;
8148 {
8149 Lisp_Object window;
8150 register struct window *w;
8151
8152 /* If called recursively during a menu update, do nothing. This can
8153 happen when, for instance, an activate-menubar-hook causes a
8154 redisplay. */
8155 if (inhibit_menubar_update)
8156 return;
8157
8158 window = FRAME_SELECTED_WINDOW (f);
8159 w = XWINDOW (window);
8160
8161 #if 0 /* The if statement below this if statement used to include the
8162 condition !NILP (w->update_mode_line), rather than using
8163 update_mode_lines directly, and this if statement may have
8164 been added to make that condition work. Now the if
8165 statement below matches its comment, this isn't needed. */
8166 if (update_mode_lines)
8167 w->update_mode_line = Qt;
8168 #endif
8169
8170 if (FRAME_WINDOW_P (f)
8171 ?
8172 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8173 || defined (USE_GTK)
8174 FRAME_EXTERNAL_MENU_BAR (f)
8175 #else
8176 FRAME_MENU_BAR_LINES (f) > 0
8177 #endif
8178 : FRAME_MENU_BAR_LINES (f) > 0)
8179 {
8180 /* If the user has switched buffers or windows, we need to
8181 recompute to reflect the new bindings. But we'll
8182 recompute when update_mode_lines is set too; that means
8183 that people can use force-mode-line-update to request
8184 that the menu bar be recomputed. The adverse effect on
8185 the rest of the redisplay algorithm is about the same as
8186 windows_or_buffers_changed anyway. */
8187 if (windows_or_buffers_changed
8188 /* This used to test w->update_mode_line, but we believe
8189 there is no need to recompute the menu in that case. */
8190 || update_mode_lines
8191 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8192 < BUF_MODIFF (XBUFFER (w->buffer)))
8193 != !NILP (w->last_had_star))
8194 || ((!NILP (Vtransient_mark_mode)
8195 && !NILP (XBUFFER (w->buffer)->mark_active))
8196 != !NILP (w->region_showing)))
8197 {
8198 struct buffer *prev = current_buffer;
8199 int count = SPECPDL_INDEX ();
8200
8201 specbind (Qinhibit_menubar_update, Qt);
8202
8203 set_buffer_internal_1 (XBUFFER (w->buffer));
8204 if (save_match_data)
8205 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8206 if (NILP (Voverriding_local_map_menu_flag))
8207 {
8208 specbind (Qoverriding_terminal_local_map, Qnil);
8209 specbind (Qoverriding_local_map, Qnil);
8210 }
8211
8212 /* Run the Lucid hook. */
8213 safe_run_hooks (Qactivate_menubar_hook);
8214
8215 /* If it has changed current-menubar from previous value,
8216 really recompute the menu-bar from the value. */
8217 if (! NILP (Vlucid_menu_bar_dirty_flag))
8218 call0 (Qrecompute_lucid_menubar);
8219
8220 safe_run_hooks (Qmenu_bar_update_hook);
8221 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8222
8223 /* Redisplay the menu bar in case we changed it. */
8224 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8225 || defined (USE_GTK)
8226 if (FRAME_WINDOW_P (f)
8227 #if defined (MAC_OS)
8228 /* All frames on Mac OS share the same menubar. So only the
8229 selected frame should be allowed to set it. */
8230 && f == SELECTED_FRAME ()
8231 #endif
8232 )
8233 set_frame_menubar (f, 0, 0);
8234 else
8235 /* On a terminal screen, the menu bar is an ordinary screen
8236 line, and this makes it get updated. */
8237 w->update_mode_line = Qt;
8238 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8239 /* In the non-toolkit version, the menu bar is an ordinary screen
8240 line, and this makes it get updated. */
8241 w->update_mode_line = Qt;
8242 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8243
8244 unbind_to (count, Qnil);
8245 set_buffer_internal_1 (prev);
8246 }
8247 }
8248 }
8249
8250
8251 \f
8252 /***********************************************************************
8253 Output Cursor
8254 ***********************************************************************/
8255
8256 #ifdef HAVE_WINDOW_SYSTEM
8257
8258 /* EXPORT:
8259 Nominal cursor position -- where to draw output.
8260 HPOS and VPOS are window relative glyph matrix coordinates.
8261 X and Y are window relative pixel coordinates. */
8262
8263 struct cursor_pos output_cursor;
8264
8265
8266 /* EXPORT:
8267 Set the global variable output_cursor to CURSOR. All cursor
8268 positions are relative to updated_window. */
8269
8270 void
8271 set_output_cursor (cursor)
8272 struct cursor_pos *cursor;
8273 {
8274 output_cursor.hpos = cursor->hpos;
8275 output_cursor.vpos = cursor->vpos;
8276 output_cursor.x = cursor->x;
8277 output_cursor.y = cursor->y;
8278 }
8279
8280
8281 /* EXPORT for RIF:
8282 Set a nominal cursor position.
8283
8284 HPOS and VPOS are column/row positions in a window glyph matrix. X
8285 and Y are window text area relative pixel positions.
8286
8287 If this is done during an update, updated_window will contain the
8288 window that is being updated and the position is the future output
8289 cursor position for that window. If updated_window is null, use
8290 selected_window and display the cursor at the given position. */
8291
8292 void
8293 x_cursor_to (vpos, hpos, y, x)
8294 int vpos, hpos, y, x;
8295 {
8296 struct window *w;
8297
8298 /* If updated_window is not set, work on selected_window. */
8299 if (updated_window)
8300 w = updated_window;
8301 else
8302 w = XWINDOW (selected_window);
8303
8304 /* Set the output cursor. */
8305 output_cursor.hpos = hpos;
8306 output_cursor.vpos = vpos;
8307 output_cursor.x = x;
8308 output_cursor.y = y;
8309
8310 /* If not called as part of an update, really display the cursor.
8311 This will also set the cursor position of W. */
8312 if (updated_window == NULL)
8313 {
8314 BLOCK_INPUT;
8315 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8316 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
8317 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
8318 UNBLOCK_INPUT;
8319 }
8320 }
8321
8322 #endif /* HAVE_WINDOW_SYSTEM */
8323
8324 \f
8325 /***********************************************************************
8326 Tool-bars
8327 ***********************************************************************/
8328
8329 #ifdef HAVE_WINDOW_SYSTEM
8330
8331 /* Where the mouse was last time we reported a mouse event. */
8332
8333 FRAME_PTR last_mouse_frame;
8334
8335 /* Tool-bar item index of the item on which a mouse button was pressed
8336 or -1. */
8337
8338 int last_tool_bar_item;
8339
8340
8341 /* Update the tool-bar item list for frame F. This has to be done
8342 before we start to fill in any display lines. Called from
8343 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8344 and restore it here. */
8345
8346 static void
8347 update_tool_bar (f, save_match_data)
8348 struct frame *f;
8349 int save_match_data;
8350 {
8351 #ifdef USE_GTK
8352 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8353 #else
8354 int do_update = WINDOWP (f->tool_bar_window)
8355 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8356 #endif
8357
8358 if (do_update)
8359 {
8360 Lisp_Object window;
8361 struct window *w;
8362
8363 window = FRAME_SELECTED_WINDOW (f);
8364 w = XWINDOW (window);
8365
8366 /* If the user has switched buffers or windows, we need to
8367 recompute to reflect the new bindings. But we'll
8368 recompute when update_mode_lines is set too; that means
8369 that people can use force-mode-line-update to request
8370 that the menu bar be recomputed. The adverse effect on
8371 the rest of the redisplay algorithm is about the same as
8372 windows_or_buffers_changed anyway. */
8373 if (windows_or_buffers_changed
8374 || !NILP (w->update_mode_line)
8375 || update_mode_lines
8376 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8377 < BUF_MODIFF (XBUFFER (w->buffer)))
8378 != !NILP (w->last_had_star))
8379 || ((!NILP (Vtransient_mark_mode)
8380 && !NILP (XBUFFER (w->buffer)->mark_active))
8381 != !NILP (w->region_showing)))
8382 {
8383 struct buffer *prev = current_buffer;
8384 int count = SPECPDL_INDEX ();
8385 Lisp_Object old_tool_bar;
8386 struct gcpro gcpro1;
8387
8388 /* Set current_buffer to the buffer of the selected
8389 window of the frame, so that we get the right local
8390 keymaps. */
8391 set_buffer_internal_1 (XBUFFER (w->buffer));
8392
8393 /* Save match data, if we must. */
8394 if (save_match_data)
8395 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8396
8397 /* Make sure that we don't accidentally use bogus keymaps. */
8398 if (NILP (Voverriding_local_map_menu_flag))
8399 {
8400 specbind (Qoverriding_terminal_local_map, Qnil);
8401 specbind (Qoverriding_local_map, Qnil);
8402 }
8403
8404 old_tool_bar = f->tool_bar_items;
8405 GCPRO1 (old_tool_bar);
8406
8407 /* Build desired tool-bar items from keymaps. */
8408 BLOCK_INPUT;
8409 f->tool_bar_items
8410 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
8411 UNBLOCK_INPUT;
8412
8413 /* Redisplay the tool-bar if we changed it. */
8414 if (! NILP (Fequal (old_tool_bar, f->tool_bar_items)))
8415 w->update_mode_line = Qt;
8416
8417 UNGCPRO;
8418
8419 unbind_to (count, Qnil);
8420 set_buffer_internal_1 (prev);
8421 }
8422 }
8423 }
8424
8425
8426 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8427 F's desired tool-bar contents. F->tool_bar_items must have
8428 been set up previously by calling prepare_menu_bars. */
8429
8430 static void
8431 build_desired_tool_bar_string (f)
8432 struct frame *f;
8433 {
8434 int i, size, size_needed;
8435 struct gcpro gcpro1, gcpro2, gcpro3;
8436 Lisp_Object image, plist, props;
8437
8438 image = plist = props = Qnil;
8439 GCPRO3 (image, plist, props);
8440
8441 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8442 Otherwise, make a new string. */
8443
8444 /* The size of the string we might be able to reuse. */
8445 size = (STRINGP (f->desired_tool_bar_string)
8446 ? SCHARS (f->desired_tool_bar_string)
8447 : 0);
8448
8449 /* We need one space in the string for each image. */
8450 size_needed = f->n_tool_bar_items;
8451
8452 /* Reuse f->desired_tool_bar_string, if possible. */
8453 if (size < size_needed || NILP (f->desired_tool_bar_string))
8454 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8455 make_number (' '));
8456 else
8457 {
8458 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8459 Fremove_text_properties (make_number (0), make_number (size),
8460 props, f->desired_tool_bar_string);
8461 }
8462
8463 /* Put a `display' property on the string for the images to display,
8464 put a `menu_item' property on tool-bar items with a value that
8465 is the index of the item in F's tool-bar item vector. */
8466 for (i = 0; i < f->n_tool_bar_items; ++i)
8467 {
8468 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8469
8470 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8471 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8472 int hmargin, vmargin, relief, idx, end;
8473 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8474
8475 /* If image is a vector, choose the image according to the
8476 button state. */
8477 image = PROP (TOOL_BAR_ITEM_IMAGES);
8478 if (VECTORP (image))
8479 {
8480 if (enabled_p)
8481 idx = (selected_p
8482 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8483 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8484 else
8485 idx = (selected_p
8486 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8487 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8488
8489 xassert (ASIZE (image) >= idx);
8490 image = AREF (image, idx);
8491 }
8492 else
8493 idx = -1;
8494
8495 /* Ignore invalid image specifications. */
8496 if (!valid_image_p (image))
8497 continue;
8498
8499 /* Display the tool-bar button pressed, or depressed. */
8500 plist = Fcopy_sequence (XCDR (image));
8501
8502 /* Compute margin and relief to draw. */
8503 relief = (tool_bar_button_relief >= 0
8504 ? tool_bar_button_relief
8505 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8506 hmargin = vmargin = relief;
8507
8508 if (INTEGERP (Vtool_bar_button_margin)
8509 && XINT (Vtool_bar_button_margin) > 0)
8510 {
8511 hmargin += XFASTINT (Vtool_bar_button_margin);
8512 vmargin += XFASTINT (Vtool_bar_button_margin);
8513 }
8514 else if (CONSP (Vtool_bar_button_margin))
8515 {
8516 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8517 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8518 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8519
8520 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8521 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8522 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8523 }
8524
8525 if (auto_raise_tool_bar_buttons_p)
8526 {
8527 /* Add a `:relief' property to the image spec if the item is
8528 selected. */
8529 if (selected_p)
8530 {
8531 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8532 hmargin -= relief;
8533 vmargin -= relief;
8534 }
8535 }
8536 else
8537 {
8538 /* If image is selected, display it pressed, i.e. with a
8539 negative relief. If it's not selected, display it with a
8540 raised relief. */
8541 plist = Fplist_put (plist, QCrelief,
8542 (selected_p
8543 ? make_number (-relief)
8544 : make_number (relief)));
8545 hmargin -= relief;
8546 vmargin -= relief;
8547 }
8548
8549 /* Put a margin around the image. */
8550 if (hmargin || vmargin)
8551 {
8552 if (hmargin == vmargin)
8553 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8554 else
8555 plist = Fplist_put (plist, QCmargin,
8556 Fcons (make_number (hmargin),
8557 make_number (vmargin)));
8558 }
8559
8560 /* If button is not enabled, and we don't have special images
8561 for the disabled state, make the image appear disabled by
8562 applying an appropriate algorithm to it. */
8563 if (!enabled_p && idx < 0)
8564 plist = Fplist_put (plist, QCconversion, Qdisabled);
8565
8566 /* Put a `display' text property on the string for the image to
8567 display. Put a `menu-item' property on the string that gives
8568 the start of this item's properties in the tool-bar items
8569 vector. */
8570 image = Fcons (Qimage, plist);
8571 props = list4 (Qdisplay, image,
8572 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8573
8574 /* Let the last image hide all remaining spaces in the tool bar
8575 string. The string can be longer than needed when we reuse a
8576 previous string. */
8577 if (i + 1 == f->n_tool_bar_items)
8578 end = SCHARS (f->desired_tool_bar_string);
8579 else
8580 end = i + 1;
8581 Fadd_text_properties (make_number (i), make_number (end),
8582 props, f->desired_tool_bar_string);
8583 #undef PROP
8584 }
8585
8586 UNGCPRO;
8587 }
8588
8589
8590 /* Display one line of the tool-bar of frame IT->f. */
8591
8592 static void
8593 display_tool_bar_line (it)
8594 struct it *it;
8595 {
8596 struct glyph_row *row = it->glyph_row;
8597 int max_x = it->last_visible_x;
8598 struct glyph *last;
8599
8600 prepare_desired_row (row);
8601 row->y = it->current_y;
8602
8603 /* Note that this isn't made use of if the face hasn't a box,
8604 so there's no need to check the face here. */
8605 it->start_of_box_run_p = 1;
8606
8607 while (it->current_x < max_x)
8608 {
8609 int x_before, x, n_glyphs_before, i, nglyphs;
8610
8611 /* Get the next display element. */
8612 if (!get_next_display_element (it))
8613 break;
8614
8615 /* Produce glyphs. */
8616 x_before = it->current_x;
8617 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8618 PRODUCE_GLYPHS (it);
8619
8620 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8621 i = 0;
8622 x = x_before;
8623 while (i < nglyphs)
8624 {
8625 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
8626
8627 if (x + glyph->pixel_width > max_x)
8628 {
8629 /* Glyph doesn't fit on line. */
8630 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8631 it->current_x = x;
8632 goto out;
8633 }
8634
8635 ++it->hpos;
8636 x += glyph->pixel_width;
8637 ++i;
8638 }
8639
8640 /* Stop at line ends. */
8641 if (ITERATOR_AT_END_OF_LINE_P (it))
8642 break;
8643
8644 set_iterator_to_next (it, 1);
8645 }
8646
8647 out:;
8648
8649 row->displays_text_p = row->used[TEXT_AREA] != 0;
8650 extend_face_to_end_of_line (it);
8651 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8652 last->right_box_line_p = 1;
8653 if (last == row->glyphs[TEXT_AREA])
8654 last->left_box_line_p = 1;
8655 compute_line_metrics (it);
8656
8657 /* If line is empty, make it occupy the rest of the tool-bar. */
8658 if (!row->displays_text_p)
8659 {
8660 row->height = row->phys_height = it->last_visible_y - row->y;
8661 row->ascent = row->phys_ascent = 0;
8662 }
8663
8664 row->full_width_p = 1;
8665 row->continued_p = 0;
8666 row->truncated_on_left_p = 0;
8667 row->truncated_on_right_p = 0;
8668
8669 it->current_x = it->hpos = 0;
8670 it->current_y += row->height;
8671 ++it->vpos;
8672 ++it->glyph_row;
8673 }
8674
8675
8676 /* Value is the number of screen lines needed to make all tool-bar
8677 items of frame F visible. */
8678
8679 static int
8680 tool_bar_lines_needed (f)
8681 struct frame *f;
8682 {
8683 struct window *w = XWINDOW (f->tool_bar_window);
8684 struct it it;
8685
8686 /* Initialize an iterator for iteration over
8687 F->desired_tool_bar_string in the tool-bar window of frame F. */
8688 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8689 it.first_visible_x = 0;
8690 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8691 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8692
8693 while (!ITERATOR_AT_END_P (&it))
8694 {
8695 it.glyph_row = w->desired_matrix->rows;
8696 clear_glyph_row (it.glyph_row);
8697 display_tool_bar_line (&it);
8698 }
8699
8700 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
8701 }
8702
8703
8704 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8705 0, 1, 0,
8706 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8707 (frame)
8708 Lisp_Object frame;
8709 {
8710 struct frame *f;
8711 struct window *w;
8712 int nlines = 0;
8713
8714 if (NILP (frame))
8715 frame = selected_frame;
8716 else
8717 CHECK_FRAME (frame);
8718 f = XFRAME (frame);
8719
8720 if (WINDOWP (f->tool_bar_window)
8721 || (w = XWINDOW (f->tool_bar_window),
8722 WINDOW_TOTAL_LINES (w) > 0))
8723 {
8724 update_tool_bar (f, 1);
8725 if (f->n_tool_bar_items)
8726 {
8727 build_desired_tool_bar_string (f);
8728 nlines = tool_bar_lines_needed (f);
8729 }
8730 }
8731
8732 return make_number (nlines);
8733 }
8734
8735
8736 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
8737 height should be changed. */
8738
8739 static int
8740 redisplay_tool_bar (f)
8741 struct frame *f;
8742 {
8743 struct window *w;
8744 struct it it;
8745 struct glyph_row *row;
8746 int change_height_p = 0;
8747
8748 #ifdef USE_GTK
8749 if (FRAME_EXTERNAL_TOOL_BAR (f))
8750 update_frame_tool_bar (f);
8751 return 0;
8752 #endif
8753
8754 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8755 do anything. This means you must start with tool-bar-lines
8756 non-zero to get the auto-sizing effect. Or in other words, you
8757 can turn off tool-bars by specifying tool-bar-lines zero. */
8758 if (!WINDOWP (f->tool_bar_window)
8759 || (w = XWINDOW (f->tool_bar_window),
8760 WINDOW_TOTAL_LINES (w) == 0))
8761 return 0;
8762
8763 /* Set up an iterator for the tool-bar window. */
8764 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8765 it.first_visible_x = 0;
8766 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8767 row = it.glyph_row;
8768
8769 /* Build a string that represents the contents of the tool-bar. */
8770 build_desired_tool_bar_string (f);
8771 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8772
8773 /* Display as many lines as needed to display all tool-bar items. */
8774 while (it.current_y < it.last_visible_y)
8775 display_tool_bar_line (&it);
8776
8777 /* It doesn't make much sense to try scrolling in the tool-bar
8778 window, so don't do it. */
8779 w->desired_matrix->no_scrolling_p = 1;
8780 w->must_be_updated_p = 1;
8781
8782 if (auto_resize_tool_bars_p)
8783 {
8784 int nlines;
8785
8786 /* If we couldn't display everything, change the tool-bar's
8787 height. */
8788 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8789 change_height_p = 1;
8790
8791 /* If there are blank lines at the end, except for a partially
8792 visible blank line at the end that is smaller than
8793 FRAME_LINE_HEIGHT, change the tool-bar's height. */
8794 row = it.glyph_row - 1;
8795 if (!row->displays_text_p
8796 && row->height >= FRAME_LINE_HEIGHT (f))
8797 change_height_p = 1;
8798
8799 /* If row displays tool-bar items, but is partially visible,
8800 change the tool-bar's height. */
8801 if (row->displays_text_p
8802 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8803 change_height_p = 1;
8804
8805 /* Resize windows as needed by changing the `tool-bar-lines'
8806 frame parameter. */
8807 if (change_height_p
8808 && (nlines = tool_bar_lines_needed (f),
8809 nlines != WINDOW_TOTAL_LINES (w)))
8810 {
8811 extern Lisp_Object Qtool_bar_lines;
8812 Lisp_Object frame;
8813 int old_height = WINDOW_TOTAL_LINES (w);
8814
8815 XSETFRAME (frame, f);
8816 clear_glyph_matrix (w->desired_matrix);
8817 Fmodify_frame_parameters (frame,
8818 Fcons (Fcons (Qtool_bar_lines,
8819 make_number (nlines)),
8820 Qnil));
8821 if (WINDOW_TOTAL_LINES (w) != old_height)
8822 fonts_changed_p = 1;
8823 }
8824 }
8825
8826 return change_height_p;
8827 }
8828
8829
8830 /* Get information about the tool-bar item which is displayed in GLYPH
8831 on frame F. Return in *PROP_IDX the index where tool-bar item
8832 properties start in F->tool_bar_items. Value is zero if
8833 GLYPH doesn't display a tool-bar item. */
8834
8835 static int
8836 tool_bar_item_info (f, glyph, prop_idx)
8837 struct frame *f;
8838 struct glyph *glyph;
8839 int *prop_idx;
8840 {
8841 Lisp_Object prop;
8842 int success_p;
8843 int charpos;
8844
8845 /* This function can be called asynchronously, which means we must
8846 exclude any possibility that Fget_text_property signals an
8847 error. */
8848 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8849 charpos = max (0, charpos);
8850
8851 /* Get the text property `menu-item' at pos. The value of that
8852 property is the start index of this item's properties in
8853 F->tool_bar_items. */
8854 prop = Fget_text_property (make_number (charpos),
8855 Qmenu_item, f->current_tool_bar_string);
8856 if (INTEGERP (prop))
8857 {
8858 *prop_idx = XINT (prop);
8859 success_p = 1;
8860 }
8861 else
8862 success_p = 0;
8863
8864 return success_p;
8865 }
8866
8867 \f
8868 /* Get information about the tool-bar item at position X/Y on frame F.
8869 Return in *GLYPH a pointer to the glyph of the tool-bar item in
8870 the current matrix of the tool-bar window of F, or NULL if not
8871 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8872 item in F->tool_bar_items. Value is
8873
8874 -1 if X/Y is not on a tool-bar item
8875 0 if X/Y is on the same item that was highlighted before.
8876 1 otherwise. */
8877
8878 static int
8879 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
8880 struct frame *f;
8881 int x, y;
8882 struct glyph **glyph;
8883 int *hpos, *vpos, *prop_idx;
8884 {
8885 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8886 struct window *w = XWINDOW (f->tool_bar_window);
8887 int area;
8888
8889 /* Find the glyph under X/Y. */
8890 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
8891 if (*glyph == NULL)
8892 return -1;
8893
8894 /* Get the start of this tool-bar item's properties in
8895 f->tool_bar_items. */
8896 if (!tool_bar_item_info (f, *glyph, prop_idx))
8897 return -1;
8898
8899 /* Is mouse on the highlighted item? */
8900 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
8901 && *vpos >= dpyinfo->mouse_face_beg_row
8902 && *vpos <= dpyinfo->mouse_face_end_row
8903 && (*vpos > dpyinfo->mouse_face_beg_row
8904 || *hpos >= dpyinfo->mouse_face_beg_col)
8905 && (*vpos < dpyinfo->mouse_face_end_row
8906 || *hpos < dpyinfo->mouse_face_end_col
8907 || dpyinfo->mouse_face_past_end))
8908 return 0;
8909
8910 return 1;
8911 }
8912
8913
8914 /* EXPORT:
8915 Handle mouse button event on the tool-bar of frame F, at
8916 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
8917 0 for button release. MODIFIERS is event modifiers for button
8918 release. */
8919
8920 void
8921 handle_tool_bar_click (f, x, y, down_p, modifiers)
8922 struct frame *f;
8923 int x, y, down_p;
8924 unsigned int modifiers;
8925 {
8926 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8927 struct window *w = XWINDOW (f->tool_bar_window);
8928 int hpos, vpos, prop_idx;
8929 struct glyph *glyph;
8930 Lisp_Object enabled_p;
8931
8932 /* If not on the highlighted tool-bar item, return. */
8933 frame_to_window_pixel_xy (w, &x, &y);
8934 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
8935 return;
8936
8937 /* If item is disabled, do nothing. */
8938 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8939 if (NILP (enabled_p))
8940 return;
8941
8942 if (down_p)
8943 {
8944 /* Show item in pressed state. */
8945 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
8946 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
8947 last_tool_bar_item = prop_idx;
8948 }
8949 else
8950 {
8951 Lisp_Object key, frame;
8952 struct input_event event;
8953 EVENT_INIT (event);
8954
8955 /* Show item in released state. */
8956 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
8957 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
8958
8959 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
8960
8961 XSETFRAME (frame, f);
8962 event.kind = TOOL_BAR_EVENT;
8963 event.frame_or_window = frame;
8964 event.arg = frame;
8965 kbd_buffer_store_event (&event);
8966
8967 event.kind = TOOL_BAR_EVENT;
8968 event.frame_or_window = frame;
8969 event.arg = key;
8970 event.modifiers = modifiers;
8971 kbd_buffer_store_event (&event);
8972 last_tool_bar_item = -1;
8973 }
8974 }
8975
8976
8977 /* Possibly highlight a tool-bar item on frame F when mouse moves to
8978 tool-bar window-relative coordinates X/Y. Called from
8979 note_mouse_highlight. */
8980
8981 static void
8982 note_tool_bar_highlight (f, x, y)
8983 struct frame *f;
8984 int x, y;
8985 {
8986 Lisp_Object window = f->tool_bar_window;
8987 struct window *w = XWINDOW (window);
8988 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8989 int hpos, vpos;
8990 struct glyph *glyph;
8991 struct glyph_row *row;
8992 int i;
8993 Lisp_Object enabled_p;
8994 int prop_idx;
8995 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
8996 int mouse_down_p, rc;
8997
8998 /* Function note_mouse_highlight is called with negative x(y
8999 values when mouse moves outside of the frame. */
9000 if (x <= 0 || y <= 0)
9001 {
9002 clear_mouse_face (dpyinfo);
9003 return;
9004 }
9005
9006 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9007 if (rc < 0)
9008 {
9009 /* Not on tool-bar item. */
9010 clear_mouse_face (dpyinfo);
9011 return;
9012 }
9013 else if (rc == 0)
9014 /* On same tool-bar item as before. */
9015 goto set_help_echo;
9016
9017 clear_mouse_face (dpyinfo);
9018
9019 /* Mouse is down, but on different tool-bar item? */
9020 mouse_down_p = (dpyinfo->grabbed
9021 && f == last_mouse_frame
9022 && FRAME_LIVE_P (f));
9023 if (mouse_down_p
9024 && last_tool_bar_item != prop_idx)
9025 return;
9026
9027 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9028 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9029
9030 /* If tool-bar item is not enabled, don't highlight it. */
9031 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9032 if (!NILP (enabled_p))
9033 {
9034 /* Compute the x-position of the glyph. In front and past the
9035 image is a space. We include this in the highlighted area. */
9036 row = MATRIX_ROW (w->current_matrix, vpos);
9037 for (i = x = 0; i < hpos; ++i)
9038 x += row->glyphs[TEXT_AREA][i].pixel_width;
9039
9040 /* Record this as the current active region. */
9041 dpyinfo->mouse_face_beg_col = hpos;
9042 dpyinfo->mouse_face_beg_row = vpos;
9043 dpyinfo->mouse_face_beg_x = x;
9044 dpyinfo->mouse_face_beg_y = row->y;
9045 dpyinfo->mouse_face_past_end = 0;
9046
9047 dpyinfo->mouse_face_end_col = hpos + 1;
9048 dpyinfo->mouse_face_end_row = vpos;
9049 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9050 dpyinfo->mouse_face_end_y = row->y;
9051 dpyinfo->mouse_face_window = window;
9052 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9053
9054 /* Display it as active. */
9055 show_mouse_face (dpyinfo, draw);
9056 dpyinfo->mouse_face_image_state = draw;
9057 }
9058
9059 set_help_echo:
9060
9061 /* Set help_echo_string to a help string to display for this tool-bar item.
9062 XTread_socket does the rest. */
9063 help_echo_object = help_echo_window = Qnil;
9064 help_echo_pos = -1;
9065 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9066 if (NILP (help_echo_string))
9067 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9068 }
9069
9070 #endif /* HAVE_WINDOW_SYSTEM */
9071
9072
9073 \f
9074 /************************************************************************
9075 Horizontal scrolling
9076 ************************************************************************/
9077
9078 static int hscroll_window_tree P_ ((Lisp_Object));
9079 static int hscroll_windows P_ ((Lisp_Object));
9080
9081 /* For all leaf windows in the window tree rooted at WINDOW, set their
9082 hscroll value so that PT is (i) visible in the window, and (ii) so
9083 that it is not within a certain margin at the window's left and
9084 right border. Value is non-zero if any window's hscroll has been
9085 changed. */
9086
9087 static int
9088 hscroll_window_tree (window)
9089 Lisp_Object window;
9090 {
9091 int hscrolled_p = 0;
9092 int hscroll_relative_p = FLOATP (Vhscroll_step);
9093 int hscroll_step_abs = 0;
9094 double hscroll_step_rel = 0;
9095
9096 if (hscroll_relative_p)
9097 {
9098 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9099 if (hscroll_step_rel < 0)
9100 {
9101 hscroll_relative_p = 0;
9102 hscroll_step_abs = 0;
9103 }
9104 }
9105 else if (INTEGERP (Vhscroll_step))
9106 {
9107 hscroll_step_abs = XINT (Vhscroll_step);
9108 if (hscroll_step_abs < 0)
9109 hscroll_step_abs = 0;
9110 }
9111 else
9112 hscroll_step_abs = 0;
9113
9114 while (WINDOWP (window))
9115 {
9116 struct window *w = XWINDOW (window);
9117
9118 if (WINDOWP (w->hchild))
9119 hscrolled_p |= hscroll_window_tree (w->hchild);
9120 else if (WINDOWP (w->vchild))
9121 hscrolled_p |= hscroll_window_tree (w->vchild);
9122 else if (w->cursor.vpos >= 0)
9123 {
9124 int h_margin;
9125 int text_area_width;
9126 struct glyph_row *current_cursor_row
9127 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9128 struct glyph_row *desired_cursor_row
9129 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9130 struct glyph_row *cursor_row
9131 = (desired_cursor_row->enabled_p
9132 ? desired_cursor_row
9133 : current_cursor_row);
9134
9135 text_area_width = window_box_width (w, TEXT_AREA);
9136
9137 /* Scroll when cursor is inside this scroll margin. */
9138 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9139
9140 if ((XFASTINT (w->hscroll)
9141 && w->cursor.x <= h_margin)
9142 || (cursor_row->enabled_p
9143 && cursor_row->truncated_on_right_p
9144 && (w->cursor.x >= text_area_width - h_margin)))
9145 {
9146 struct it it;
9147 int hscroll;
9148 struct buffer *saved_current_buffer;
9149 int pt;
9150 int wanted_x;
9151
9152 /* Find point in a display of infinite width. */
9153 saved_current_buffer = current_buffer;
9154 current_buffer = XBUFFER (w->buffer);
9155
9156 if (w == XWINDOW (selected_window))
9157 pt = BUF_PT (current_buffer);
9158 else
9159 {
9160 pt = marker_position (w->pointm);
9161 pt = max (BEGV, pt);
9162 pt = min (ZV, pt);
9163 }
9164
9165 /* Move iterator to pt starting at cursor_row->start in
9166 a line with infinite width. */
9167 init_to_row_start (&it, w, cursor_row);
9168 it.last_visible_x = INFINITY;
9169 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9170 current_buffer = saved_current_buffer;
9171
9172 /* Position cursor in window. */
9173 if (!hscroll_relative_p && hscroll_step_abs == 0)
9174 hscroll = max (0, (it.current_x
9175 - (ITERATOR_AT_END_OF_LINE_P (&it)
9176 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9177 : (text_area_width / 2))))
9178 / FRAME_COLUMN_WIDTH (it.f);
9179 else if (w->cursor.x >= text_area_width - h_margin)
9180 {
9181 if (hscroll_relative_p)
9182 wanted_x = text_area_width * (1 - hscroll_step_rel)
9183 - h_margin;
9184 else
9185 wanted_x = text_area_width
9186 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9187 - h_margin;
9188 hscroll
9189 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9190 }
9191 else
9192 {
9193 if (hscroll_relative_p)
9194 wanted_x = text_area_width * hscroll_step_rel
9195 + h_margin;
9196 else
9197 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9198 + h_margin;
9199 hscroll
9200 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9201 }
9202 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9203
9204 /* Don't call Fset_window_hscroll if value hasn't
9205 changed because it will prevent redisplay
9206 optimizations. */
9207 if (XFASTINT (w->hscroll) != hscroll)
9208 {
9209 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9210 w->hscroll = make_number (hscroll);
9211 hscrolled_p = 1;
9212 }
9213 }
9214 }
9215
9216 window = w->next;
9217 }
9218
9219 /* Value is non-zero if hscroll of any leaf window has been changed. */
9220 return hscrolled_p;
9221 }
9222
9223
9224 /* Set hscroll so that cursor is visible and not inside horizontal
9225 scroll margins for all windows in the tree rooted at WINDOW. See
9226 also hscroll_window_tree above. Value is non-zero if any window's
9227 hscroll has been changed. If it has, desired matrices on the frame
9228 of WINDOW are cleared. */
9229
9230 static int
9231 hscroll_windows (window)
9232 Lisp_Object window;
9233 {
9234 int hscrolled_p;
9235
9236 if (automatic_hscrolling_p)
9237 {
9238 hscrolled_p = hscroll_window_tree (window);
9239 if (hscrolled_p)
9240 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9241 }
9242 else
9243 hscrolled_p = 0;
9244 return hscrolled_p;
9245 }
9246
9247
9248 \f
9249 /************************************************************************
9250 Redisplay
9251 ************************************************************************/
9252
9253 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9254 to a non-zero value. This is sometimes handy to have in a debugger
9255 session. */
9256
9257 #if GLYPH_DEBUG
9258
9259 /* First and last unchanged row for try_window_id. */
9260
9261 int debug_first_unchanged_at_end_vpos;
9262 int debug_last_unchanged_at_beg_vpos;
9263
9264 /* Delta vpos and y. */
9265
9266 int debug_dvpos, debug_dy;
9267
9268 /* Delta in characters and bytes for try_window_id. */
9269
9270 int debug_delta, debug_delta_bytes;
9271
9272 /* Values of window_end_pos and window_end_vpos at the end of
9273 try_window_id. */
9274
9275 EMACS_INT debug_end_pos, debug_end_vpos;
9276
9277 /* Append a string to W->desired_matrix->method. FMT is a printf
9278 format string. A1...A9 are a supplement for a variable-length
9279 argument list. If trace_redisplay_p is non-zero also printf the
9280 resulting string to stderr. */
9281
9282 static void
9283 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9284 struct window *w;
9285 char *fmt;
9286 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9287 {
9288 char buffer[512];
9289 char *method = w->desired_matrix->method;
9290 int len = strlen (method);
9291 int size = sizeof w->desired_matrix->method;
9292 int remaining = size - len - 1;
9293
9294 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9295 if (len && remaining)
9296 {
9297 method[len] = '|';
9298 --remaining, ++len;
9299 }
9300
9301 strncpy (method + len, buffer, remaining);
9302
9303 if (trace_redisplay_p)
9304 fprintf (stderr, "%p (%s): %s\n",
9305 w,
9306 ((BUFFERP (w->buffer)
9307 && STRINGP (XBUFFER (w->buffer)->name))
9308 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9309 : "no buffer"),
9310 buffer);
9311 }
9312
9313 #endif /* GLYPH_DEBUG */
9314
9315
9316 /* Value is non-zero if all changes in window W, which displays
9317 current_buffer, are in the text between START and END. START is a
9318 buffer position, END is given as a distance from Z. Used in
9319 redisplay_internal for display optimization. */
9320
9321 static INLINE int
9322 text_outside_line_unchanged_p (w, start, end)
9323 struct window *w;
9324 int start, end;
9325 {
9326 int unchanged_p = 1;
9327
9328 /* If text or overlays have changed, see where. */
9329 if (XFASTINT (w->last_modified) < MODIFF
9330 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9331 {
9332 /* Gap in the line? */
9333 if (GPT < start || Z - GPT < end)
9334 unchanged_p = 0;
9335
9336 /* Changes start in front of the line, or end after it? */
9337 if (unchanged_p
9338 && (BEG_UNCHANGED < start - 1
9339 || END_UNCHANGED < end))
9340 unchanged_p = 0;
9341
9342 /* If selective display, can't optimize if changes start at the
9343 beginning of the line. */
9344 if (unchanged_p
9345 && INTEGERP (current_buffer->selective_display)
9346 && XINT (current_buffer->selective_display) > 0
9347 && (BEG_UNCHANGED < start || GPT <= start))
9348 unchanged_p = 0;
9349
9350 /* If there are overlays at the start or end of the line, these
9351 may have overlay strings with newlines in them. A change at
9352 START, for instance, may actually concern the display of such
9353 overlay strings as well, and they are displayed on different
9354 lines. So, quickly rule out this case. (For the future, it
9355 might be desirable to implement something more telling than
9356 just BEG/END_UNCHANGED.) */
9357 if (unchanged_p)
9358 {
9359 if (BEG + BEG_UNCHANGED == start
9360 && overlay_touches_p (start))
9361 unchanged_p = 0;
9362 if (END_UNCHANGED == end
9363 && overlay_touches_p (Z - end))
9364 unchanged_p = 0;
9365 }
9366 }
9367
9368 return unchanged_p;
9369 }
9370
9371
9372 /* Do a frame update, taking possible shortcuts into account. This is
9373 the main external entry point for redisplay.
9374
9375 If the last redisplay displayed an echo area message and that message
9376 is no longer requested, we clear the echo area or bring back the
9377 mini-buffer if that is in use. */
9378
9379 void
9380 redisplay ()
9381 {
9382 redisplay_internal (0);
9383 }
9384
9385
9386 static Lisp_Object
9387 overlay_arrow_string_or_property (var, pbitmap)
9388 Lisp_Object var;
9389 int *pbitmap;
9390 {
9391 Lisp_Object pstr = Fget (var, Qoverlay_arrow_string);
9392 Lisp_Object bitmap;
9393
9394 if (pbitmap)
9395 {
9396 *pbitmap = 0;
9397 if (bitmap = Fget (var, Qoverlay_arrow_bitmap), INTEGERP (bitmap))
9398 *pbitmap = XINT (bitmap);
9399 }
9400
9401 if (!NILP (pstr))
9402 return pstr;
9403 return Voverlay_arrow_string;
9404 }
9405
9406 /* Return 1 if there are any overlay-arrows in current_buffer. */
9407 static int
9408 overlay_arrow_in_current_buffer_p ()
9409 {
9410 Lisp_Object vlist;
9411
9412 for (vlist = Voverlay_arrow_variable_list;
9413 CONSP (vlist);
9414 vlist = XCDR (vlist))
9415 {
9416 Lisp_Object var = XCAR (vlist);
9417 Lisp_Object val;
9418
9419 if (!SYMBOLP (var))
9420 continue;
9421 val = find_symbol_value (var);
9422 if (MARKERP (val)
9423 && current_buffer == XMARKER (val)->buffer)
9424 return 1;
9425 }
9426 return 0;
9427 }
9428
9429
9430 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9431 has changed. */
9432
9433 static int
9434 overlay_arrows_changed_p ()
9435 {
9436 Lisp_Object vlist;
9437
9438 for (vlist = Voverlay_arrow_variable_list;
9439 CONSP (vlist);
9440 vlist = XCDR (vlist))
9441 {
9442 Lisp_Object var = XCAR (vlist);
9443 Lisp_Object val, pstr;
9444
9445 if (!SYMBOLP (var))
9446 continue;
9447 val = find_symbol_value (var);
9448 if (!MARKERP (val))
9449 continue;
9450 if (! EQ (COERCE_MARKER (val),
9451 Fget (var, Qlast_arrow_position))
9452 || ! (pstr = overlay_arrow_string_or_property (var, 0),
9453 EQ (pstr, Fget (var, Qlast_arrow_string))))
9454 return 1;
9455 }
9456 return 0;
9457 }
9458
9459 /* Mark overlay arrows to be updated on next redisplay. */
9460
9461 static void
9462 update_overlay_arrows (up_to_date)
9463 int up_to_date;
9464 {
9465 Lisp_Object vlist;
9466
9467 for (vlist = Voverlay_arrow_variable_list;
9468 CONSP (vlist);
9469 vlist = XCDR (vlist))
9470 {
9471 Lisp_Object var = XCAR (vlist);
9472
9473 if (!SYMBOLP (var))
9474 continue;
9475
9476 if (up_to_date > 0)
9477 {
9478 Lisp_Object val = find_symbol_value (var);
9479 Fput (var, Qlast_arrow_position,
9480 COERCE_MARKER (val));
9481 Fput (var, Qlast_arrow_string,
9482 overlay_arrow_string_or_property (var, 0));
9483 }
9484 else if (up_to_date < 0
9485 || !NILP (Fget (var, Qlast_arrow_position)))
9486 {
9487 Fput (var, Qlast_arrow_position, Qt);
9488 Fput (var, Qlast_arrow_string, Qt);
9489 }
9490 }
9491 }
9492
9493
9494 /* Return overlay arrow string at row, or nil. */
9495
9496 static Lisp_Object
9497 overlay_arrow_at_row (f, row, pbitmap)
9498 struct frame *f;
9499 struct glyph_row *row;
9500 int *pbitmap;
9501 {
9502 Lisp_Object vlist;
9503
9504 for (vlist = Voverlay_arrow_variable_list;
9505 CONSP (vlist);
9506 vlist = XCDR (vlist))
9507 {
9508 Lisp_Object var = XCAR (vlist);
9509 Lisp_Object val;
9510
9511 if (!SYMBOLP (var))
9512 continue;
9513
9514 val = find_symbol_value (var);
9515
9516 if (MARKERP (val)
9517 && current_buffer == XMARKER (val)->buffer
9518 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
9519 {
9520 val = overlay_arrow_string_or_property (var, pbitmap);
9521 if (FRAME_WINDOW_P (f))
9522 return Qt;
9523 else if (STRINGP (val))
9524 return val;
9525 break;
9526 }
9527 }
9528
9529 *pbitmap = 0;
9530 return Qnil;
9531 }
9532
9533 /* Return 1 if point moved out of or into a composition. Otherwise
9534 return 0. PREV_BUF and PREV_PT are the last point buffer and
9535 position. BUF and PT are the current point buffer and position. */
9536
9537 int
9538 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9539 struct buffer *prev_buf, *buf;
9540 int prev_pt, pt;
9541 {
9542 int start, end;
9543 Lisp_Object prop;
9544 Lisp_Object buffer;
9545
9546 XSETBUFFER (buffer, buf);
9547 /* Check a composition at the last point if point moved within the
9548 same buffer. */
9549 if (prev_buf == buf)
9550 {
9551 if (prev_pt == pt)
9552 /* Point didn't move. */
9553 return 0;
9554
9555 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9556 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9557 && COMPOSITION_VALID_P (start, end, prop)
9558 && start < prev_pt && end > prev_pt)
9559 /* The last point was within the composition. Return 1 iff
9560 point moved out of the composition. */
9561 return (pt <= start || pt >= end);
9562 }
9563
9564 /* Check a composition at the current point. */
9565 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9566 && find_composition (pt, -1, &start, &end, &prop, buffer)
9567 && COMPOSITION_VALID_P (start, end, prop)
9568 && start < pt && end > pt);
9569 }
9570
9571
9572 /* Reconsider the setting of B->clip_changed which is displayed
9573 in window W. */
9574
9575 static INLINE void
9576 reconsider_clip_changes (w, b)
9577 struct window *w;
9578 struct buffer *b;
9579 {
9580 if (b->clip_changed
9581 && !NILP (w->window_end_valid)
9582 && w->current_matrix->buffer == b
9583 && w->current_matrix->zv == BUF_ZV (b)
9584 && w->current_matrix->begv == BUF_BEGV (b))
9585 b->clip_changed = 0;
9586
9587 /* If display wasn't paused, and W is not a tool bar window, see if
9588 point has been moved into or out of a composition. In that case,
9589 we set b->clip_changed to 1 to force updating the screen. If
9590 b->clip_changed has already been set to 1, we can skip this
9591 check. */
9592 if (!b->clip_changed
9593 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9594 {
9595 int pt;
9596
9597 if (w == XWINDOW (selected_window))
9598 pt = BUF_PT (current_buffer);
9599 else
9600 pt = marker_position (w->pointm);
9601
9602 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
9603 || pt != XINT (w->last_point))
9604 && check_point_in_composition (w->current_matrix->buffer,
9605 XINT (w->last_point),
9606 XBUFFER (w->buffer), pt))
9607 b->clip_changed = 1;
9608 }
9609 }
9610 \f
9611
9612 /* Select FRAME to forward the values of frame-local variables into C
9613 variables so that the redisplay routines can access those values
9614 directly. */
9615
9616 static void
9617 select_frame_for_redisplay (frame)
9618 Lisp_Object frame;
9619 {
9620 Lisp_Object tail, sym, val;
9621 Lisp_Object old = selected_frame;
9622
9623 selected_frame = frame;
9624
9625 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
9626 if (CONSP (XCAR (tail))
9627 && (sym = XCAR (XCAR (tail)),
9628 SYMBOLP (sym))
9629 && (sym = indirect_variable (sym),
9630 val = SYMBOL_VALUE (sym),
9631 (BUFFER_LOCAL_VALUEP (val)
9632 || SOME_BUFFER_LOCAL_VALUEP (val)))
9633 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9634 Fsymbol_value (sym);
9635
9636 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
9637 if (CONSP (XCAR (tail))
9638 && (sym = XCAR (XCAR (tail)),
9639 SYMBOLP (sym))
9640 && (sym = indirect_variable (sym),
9641 val = SYMBOL_VALUE (sym),
9642 (BUFFER_LOCAL_VALUEP (val)
9643 || SOME_BUFFER_LOCAL_VALUEP (val)))
9644 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9645 Fsymbol_value (sym);
9646 }
9647
9648
9649 #define STOP_POLLING \
9650 do { if (! polling_stopped_here) stop_polling (); \
9651 polling_stopped_here = 1; } while (0)
9652
9653 #define RESUME_POLLING \
9654 do { if (polling_stopped_here) start_polling (); \
9655 polling_stopped_here = 0; } while (0)
9656
9657
9658 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9659 response to any user action; therefore, we should preserve the echo
9660 area. (Actually, our caller does that job.) Perhaps in the future
9661 avoid recentering windows if it is not necessary; currently that
9662 causes some problems. */
9663
9664 static void
9665 redisplay_internal (preserve_echo_area)
9666 int preserve_echo_area;
9667 {
9668 struct window *w = XWINDOW (selected_window);
9669 struct frame *f = XFRAME (w->frame);
9670 int pause;
9671 int must_finish = 0;
9672 struct text_pos tlbufpos, tlendpos;
9673 int number_of_visible_frames;
9674 int count;
9675 struct frame *sf = SELECTED_FRAME ();
9676 int polling_stopped_here = 0;
9677
9678 /* Non-zero means redisplay has to consider all windows on all
9679 frames. Zero means, only selected_window is considered. */
9680 int consider_all_windows_p;
9681
9682 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9683
9684 /* No redisplay if running in batch mode or frame is not yet fully
9685 initialized, or redisplay is explicitly turned off by setting
9686 Vinhibit_redisplay. */
9687 if (noninteractive
9688 || !NILP (Vinhibit_redisplay)
9689 || !f->glyphs_initialized_p)
9690 return;
9691
9692 /* The flag redisplay_performed_directly_p is set by
9693 direct_output_for_insert when it already did the whole screen
9694 update necessary. */
9695 if (redisplay_performed_directly_p)
9696 {
9697 redisplay_performed_directly_p = 0;
9698 if (!hscroll_windows (selected_window))
9699 return;
9700 }
9701
9702 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9703 if (popup_activated ())
9704 return;
9705 #endif
9706
9707 /* I don't think this happens but let's be paranoid. */
9708 if (redisplaying_p)
9709 return;
9710
9711 /* Record a function that resets redisplaying_p to its old value
9712 when we leave this function. */
9713 count = SPECPDL_INDEX ();
9714 record_unwind_protect (unwind_redisplay,
9715 Fcons (make_number (redisplaying_p), selected_frame));
9716 ++redisplaying_p;
9717 specbind (Qinhibit_free_realized_faces, Qnil);
9718
9719 retry:
9720 pause = 0;
9721 reconsider_clip_changes (w, current_buffer);
9722
9723 /* If new fonts have been loaded that make a glyph matrix adjustment
9724 necessary, do it. */
9725 if (fonts_changed_p)
9726 {
9727 adjust_glyphs (NULL);
9728 ++windows_or_buffers_changed;
9729 fonts_changed_p = 0;
9730 }
9731
9732 /* If face_change_count is non-zero, init_iterator will free all
9733 realized faces, which includes the faces referenced from current
9734 matrices. So, we can't reuse current matrices in this case. */
9735 if (face_change_count)
9736 ++windows_or_buffers_changed;
9737
9738 if (FRAME_TERMCAP_P (sf)
9739 && FRAME_TTY (sf)->previous_terminal_frame != sf)
9740 {
9741 /* Since frames on a single ASCII terminal share the same
9742 display area, displaying a different frame means redisplay
9743 the whole thing. */
9744 windows_or_buffers_changed++;
9745 SET_FRAME_GARBAGED (sf);
9746 FRAME_TTY (sf)->previous_terminal_frame = sf;
9747 }
9748
9749 /* Set the visible flags for all frames. Do this before checking
9750 for resized or garbaged frames; they want to know if their frames
9751 are visible. See the comment in frame.h for
9752 FRAME_SAMPLE_VISIBILITY. */
9753 {
9754 Lisp_Object tail, frame;
9755
9756 number_of_visible_frames = 0;
9757
9758 FOR_EACH_FRAME (tail, frame)
9759 {
9760 struct frame *f = XFRAME (frame);
9761
9762 FRAME_SAMPLE_VISIBILITY (f);
9763 if (FRAME_VISIBLE_P (f))
9764 ++number_of_visible_frames;
9765 clear_desired_matrices (f);
9766 }
9767 }
9768
9769
9770 /* Notice any pending interrupt request to change frame size. */
9771 do_pending_window_change (1);
9772
9773 /* Clear frames marked as garbaged. */
9774 if (frame_garbaged)
9775 clear_garbaged_frames ();
9776
9777 /* Build menubar and tool-bar items. */
9778 prepare_menu_bars ();
9779
9780 if (windows_or_buffers_changed)
9781 update_mode_lines++;
9782
9783 /* Detect case that we need to write or remove a star in the mode line. */
9784 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
9785 {
9786 w->update_mode_line = Qt;
9787 if (buffer_shared > 1)
9788 update_mode_lines++;
9789 }
9790
9791 /* If %c is in the mode line, update it if needed. */
9792 if (!NILP (w->column_number_displayed)
9793 /* This alternative quickly identifies a common case
9794 where no change is needed. */
9795 && !(PT == XFASTINT (w->last_point)
9796 && XFASTINT (w->last_modified) >= MODIFF
9797 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9798 && (XFASTINT (w->column_number_displayed)
9799 != (int) current_column ())) /* iftc */
9800 w->update_mode_line = Qt;
9801
9802 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
9803
9804 /* The variable buffer_shared is set in redisplay_window and
9805 indicates that we redisplay a buffer in different windows. See
9806 there. */
9807 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
9808 || cursor_type_changed);
9809
9810 /* If specs for an arrow have changed, do thorough redisplay
9811 to ensure we remove any arrow that should no longer exist. */
9812 if (overlay_arrows_changed_p ())
9813 consider_all_windows_p = windows_or_buffers_changed = 1;
9814
9815 /* Normally the message* functions will have already displayed and
9816 updated the echo area, but the frame may have been trashed, or
9817 the update may have been preempted, so display the echo area
9818 again here. Checking message_cleared_p captures the case that
9819 the echo area should be cleared. */
9820 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
9821 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
9822 || (message_cleared_p
9823 && minibuf_level == 0
9824 /* If the mini-window is currently selected, this means the
9825 echo-area doesn't show through. */
9826 && !MINI_WINDOW_P (XWINDOW (selected_window))))
9827 {
9828 int window_height_changed_p = echo_area_display (0);
9829 must_finish = 1;
9830
9831 /* If we don't display the current message, don't clear the
9832 message_cleared_p flag, because, if we did, we wouldn't clear
9833 the echo area in the next redisplay which doesn't preserve
9834 the echo area. */
9835 if (!display_last_displayed_message_p)
9836 message_cleared_p = 0;
9837
9838 if (fonts_changed_p)
9839 goto retry;
9840 else if (window_height_changed_p)
9841 {
9842 consider_all_windows_p = 1;
9843 ++update_mode_lines;
9844 ++windows_or_buffers_changed;
9845
9846 /* If window configuration was changed, frames may have been
9847 marked garbaged. Clear them or we will experience
9848 surprises wrt scrolling. */
9849 if (frame_garbaged)
9850 clear_garbaged_frames ();
9851 }
9852 }
9853 else if (EQ (selected_window, minibuf_window)
9854 && (current_buffer->clip_changed
9855 || XFASTINT (w->last_modified) < MODIFF
9856 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9857 && resize_mini_window (w, 0))
9858 {
9859 /* Resized active mini-window to fit the size of what it is
9860 showing if its contents might have changed. */
9861 must_finish = 1;
9862 consider_all_windows_p = 1;
9863 ++windows_or_buffers_changed;
9864 ++update_mode_lines;
9865
9866 /* If window configuration was changed, frames may have been
9867 marked garbaged. Clear them or we will experience
9868 surprises wrt scrolling. */
9869 if (frame_garbaged)
9870 clear_garbaged_frames ();
9871 }
9872
9873
9874 /* If showing the region, and mark has changed, we must redisplay
9875 the whole window. The assignment to this_line_start_pos prevents
9876 the optimization directly below this if-statement. */
9877 if (((!NILP (Vtransient_mark_mode)
9878 && !NILP (XBUFFER (w->buffer)->mark_active))
9879 != !NILP (w->region_showing))
9880 || (!NILP (w->region_showing)
9881 && !EQ (w->region_showing,
9882 Fmarker_position (XBUFFER (w->buffer)->mark))))
9883 CHARPOS (this_line_start_pos) = 0;
9884
9885 /* Optimize the case that only the line containing the cursor in the
9886 selected window has changed. Variables starting with this_ are
9887 set in display_line and record information about the line
9888 containing the cursor. */
9889 tlbufpos = this_line_start_pos;
9890 tlendpos = this_line_end_pos;
9891 if (!consider_all_windows_p
9892 && CHARPOS (tlbufpos) > 0
9893 && NILP (w->update_mode_line)
9894 && !current_buffer->clip_changed
9895 && !current_buffer->prevent_redisplay_optimizations_p
9896 && FRAME_VISIBLE_P (XFRAME (w->frame))
9897 && !FRAME_OBSCURED_P (XFRAME (w->frame))
9898 /* Make sure recorded data applies to current buffer, etc. */
9899 && this_line_buffer == current_buffer
9900 && current_buffer == XBUFFER (w->buffer)
9901 && NILP (w->force_start)
9902 && NILP (w->optional_new_start)
9903 /* Point must be on the line that we have info recorded about. */
9904 && PT >= CHARPOS (tlbufpos)
9905 && PT <= Z - CHARPOS (tlendpos)
9906 /* All text outside that line, including its final newline,
9907 must be unchanged */
9908 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
9909 CHARPOS (tlendpos)))
9910 {
9911 if (CHARPOS (tlbufpos) > BEGV
9912 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
9913 && (CHARPOS (tlbufpos) == ZV
9914 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
9915 /* Former continuation line has disappeared by becoming empty */
9916 goto cancel;
9917 else if (XFASTINT (w->last_modified) < MODIFF
9918 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
9919 || MINI_WINDOW_P (w))
9920 {
9921 /* We have to handle the case of continuation around a
9922 wide-column character (See the comment in indent.c around
9923 line 885).
9924
9925 For instance, in the following case:
9926
9927 -------- Insert --------
9928 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
9929 J_I_ ==> J_I_ `^^' are cursors.
9930 ^^ ^^
9931 -------- --------
9932
9933 As we have to redraw the line above, we should goto cancel. */
9934
9935 struct it it;
9936 int line_height_before = this_line_pixel_height;
9937
9938 /* Note that start_display will handle the case that the
9939 line starting at tlbufpos is a continuation lines. */
9940 start_display (&it, w, tlbufpos);
9941
9942 /* Implementation note: It this still necessary? */
9943 if (it.current_x != this_line_start_x)
9944 goto cancel;
9945
9946 TRACE ((stderr, "trying display optimization 1\n"));
9947 w->cursor.vpos = -1;
9948 overlay_arrow_seen = 0;
9949 it.vpos = this_line_vpos;
9950 it.current_y = this_line_y;
9951 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
9952 display_line (&it);
9953
9954 /* If line contains point, is not continued,
9955 and ends at same distance from eob as before, we win */
9956 if (w->cursor.vpos >= 0
9957 /* Line is not continued, otherwise this_line_start_pos
9958 would have been set to 0 in display_line. */
9959 && CHARPOS (this_line_start_pos)
9960 /* Line ends as before. */
9961 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
9962 /* Line has same height as before. Otherwise other lines
9963 would have to be shifted up or down. */
9964 && this_line_pixel_height == line_height_before)
9965 {
9966 /* If this is not the window's last line, we must adjust
9967 the charstarts of the lines below. */
9968 if (it.current_y < it.last_visible_y)
9969 {
9970 struct glyph_row *row
9971 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
9972 int delta, delta_bytes;
9973
9974 if (Z - CHARPOS (tlendpos) == ZV)
9975 {
9976 /* This line ends at end of (accessible part of)
9977 buffer. There is no newline to count. */
9978 delta = (Z
9979 - CHARPOS (tlendpos)
9980 - MATRIX_ROW_START_CHARPOS (row));
9981 delta_bytes = (Z_BYTE
9982 - BYTEPOS (tlendpos)
9983 - MATRIX_ROW_START_BYTEPOS (row));
9984 }
9985 else
9986 {
9987 /* This line ends in a newline. Must take
9988 account of the newline and the rest of the
9989 text that follows. */
9990 delta = (Z
9991 - CHARPOS (tlendpos)
9992 - MATRIX_ROW_START_CHARPOS (row));
9993 delta_bytes = (Z_BYTE
9994 - BYTEPOS (tlendpos)
9995 - MATRIX_ROW_START_BYTEPOS (row));
9996 }
9997
9998 increment_matrix_positions (w->current_matrix,
9999 this_line_vpos + 1,
10000 w->current_matrix->nrows,
10001 delta, delta_bytes);
10002 }
10003
10004 /* If this row displays text now but previously didn't,
10005 or vice versa, w->window_end_vpos may have to be
10006 adjusted. */
10007 if ((it.glyph_row - 1)->displays_text_p)
10008 {
10009 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10010 XSETINT (w->window_end_vpos, this_line_vpos);
10011 }
10012 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10013 && this_line_vpos > 0)
10014 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10015 w->window_end_valid = Qnil;
10016
10017 /* Update hint: No need to try to scroll in update_window. */
10018 w->desired_matrix->no_scrolling_p = 1;
10019
10020 #if GLYPH_DEBUG
10021 *w->desired_matrix->method = 0;
10022 debug_method_add (w, "optimization 1");
10023 #endif
10024 #ifdef HAVE_WINDOW_SYSTEM
10025 update_window_fringes (w, 0);
10026 #endif
10027 goto update;
10028 }
10029 else
10030 goto cancel;
10031 }
10032 else if (/* Cursor position hasn't changed. */
10033 PT == XFASTINT (w->last_point)
10034 /* Make sure the cursor was last displayed
10035 in this window. Otherwise we have to reposition it. */
10036 && 0 <= w->cursor.vpos
10037 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10038 {
10039 if (!must_finish)
10040 {
10041 do_pending_window_change (1);
10042
10043 /* We used to always goto end_of_redisplay here, but this
10044 isn't enough if we have a blinking cursor. */
10045 if (w->cursor_off_p == w->last_cursor_off_p)
10046 goto end_of_redisplay;
10047 }
10048 goto update;
10049 }
10050 /* If highlighting the region, or if the cursor is in the echo area,
10051 then we can't just move the cursor. */
10052 else if (! (!NILP (Vtransient_mark_mode)
10053 && !NILP (current_buffer->mark_active))
10054 && (EQ (selected_window, current_buffer->last_selected_window)
10055 || highlight_nonselected_windows)
10056 && NILP (w->region_showing)
10057 && NILP (Vshow_trailing_whitespace)
10058 && !cursor_in_echo_area)
10059 {
10060 struct it it;
10061 struct glyph_row *row;
10062
10063 /* Skip from tlbufpos to PT and see where it is. Note that
10064 PT may be in invisible text. If so, we will end at the
10065 next visible position. */
10066 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10067 NULL, DEFAULT_FACE_ID);
10068 it.current_x = this_line_start_x;
10069 it.current_y = this_line_y;
10070 it.vpos = this_line_vpos;
10071
10072 /* The call to move_it_to stops in front of PT, but
10073 moves over before-strings. */
10074 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10075
10076 if (it.vpos == this_line_vpos
10077 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10078 row->enabled_p))
10079 {
10080 xassert (this_line_vpos == it.vpos);
10081 xassert (this_line_y == it.current_y);
10082 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10083 #if GLYPH_DEBUG
10084 *w->desired_matrix->method = 0;
10085 debug_method_add (w, "optimization 3");
10086 #endif
10087 goto update;
10088 }
10089 else
10090 goto cancel;
10091 }
10092
10093 cancel:
10094 /* Text changed drastically or point moved off of line. */
10095 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10096 }
10097
10098 CHARPOS (this_line_start_pos) = 0;
10099 consider_all_windows_p |= buffer_shared > 1;
10100 ++clear_face_cache_count;
10101
10102
10103 /* Build desired matrices, and update the display. If
10104 consider_all_windows_p is non-zero, do it for all windows on all
10105 frames. Otherwise do it for selected_window, only. */
10106
10107 if (consider_all_windows_p)
10108 {
10109 Lisp_Object tail, frame;
10110 int i, n = 0, size = 50;
10111 struct frame **updated
10112 = (struct frame **) alloca (size * sizeof *updated);
10113
10114 /* Clear the face cache eventually. */
10115 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10116 {
10117 clear_face_cache (0);
10118 clear_face_cache_count = 0;
10119 }
10120
10121 /* Recompute # windows showing selected buffer. This will be
10122 incremented each time such a window is displayed. */
10123 buffer_shared = 0;
10124
10125 FOR_EACH_FRAME (tail, frame)
10126 {
10127 struct frame *f = XFRAME (frame);
10128
10129 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
10130 {
10131 if (! EQ (frame, selected_frame))
10132 /* Select the frame, for the sake of frame-local
10133 variables. */
10134 select_frame_for_redisplay (frame);
10135
10136 #ifdef HAVE_WINDOW_SYSTEM
10137 if (clear_face_cache_count % 50 == 0
10138 && FRAME_WINDOW_P (f))
10139 clear_image_cache (f, 0);
10140 #endif /* HAVE_WINDOW_SYSTEM */
10141
10142 /* Mark all the scroll bars to be removed; we'll redeem
10143 the ones we want when we redisplay their windows. */
10144 if (FRAME_DISPLAY (f)->condemn_scroll_bars_hook)
10145 FRAME_DISPLAY (f)->condemn_scroll_bars_hook (f);
10146
10147 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10148 redisplay_windows (FRAME_ROOT_WINDOW (f));
10149
10150 /* Any scroll bars which redisplay_windows should have
10151 nuked should now go away. */
10152 if (FRAME_DISPLAY (f)->judge_scroll_bars_hook)
10153 FRAME_DISPLAY (f)->judge_scroll_bars_hook (f);
10154
10155 /* If fonts changed, display again. */
10156 /* ??? rms: I suspect it is a mistake to jump all the way
10157 back to retry here. It should just retry this frame. */
10158 if (fonts_changed_p)
10159 goto retry;
10160
10161 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10162 {
10163 /* See if we have to hscroll. */
10164 if (hscroll_windows (f->root_window))
10165 goto retry;
10166
10167 /* Prevent various kinds of signals during display
10168 update. stdio is not robust about handling
10169 signals, which can cause an apparent I/O
10170 error. */
10171 if (interrupt_input)
10172 unrequest_sigio ();
10173 STOP_POLLING;
10174
10175 /* Update the display. */
10176 set_window_update_flags (XWINDOW (f->root_window), 1);
10177 pause |= update_frame (f, 0, 0);
10178 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10179 if (pause)
10180 break;
10181 #endif
10182
10183 if (n == size)
10184 {
10185 int nbytes = size * sizeof *updated;
10186 struct frame **p = (struct frame **) alloca (2 * nbytes);
10187 bcopy (updated, p, nbytes);
10188 size *= 2;
10189 }
10190
10191 updated[n++] = f;
10192 }
10193 }
10194 }
10195
10196 if (!pause)
10197 {
10198 /* Do the mark_window_display_accurate after all windows have
10199 been redisplayed because this call resets flags in buffers
10200 which are needed for proper redisplay. */
10201 for (i = 0; i < n; ++i)
10202 {
10203 struct frame *f = updated[i];
10204 mark_window_display_accurate (f->root_window, 1);
10205 if (FRAME_DISPLAY (f)->frame_up_to_date_hook)
10206 FRAME_DISPLAY (f)->frame_up_to_date_hook (f);
10207 }
10208 }
10209 }
10210 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10211 {
10212 Lisp_Object mini_window;
10213 struct frame *mini_frame;
10214
10215 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10216 /* Use list_of_error, not Qerror, so that
10217 we catch only errors and don't run the debugger. */
10218 internal_condition_case_1 (redisplay_window_1, selected_window,
10219 list_of_error,
10220 redisplay_window_error);
10221
10222 /* Compare desired and current matrices, perform output. */
10223
10224 update:
10225 /* If fonts changed, display again. */
10226 if (fonts_changed_p)
10227 goto retry;
10228
10229 /* Prevent various kinds of signals during display update.
10230 stdio is not robust about handling signals,
10231 which can cause an apparent I/O error. */
10232 if (interrupt_input)
10233 unrequest_sigio ();
10234 STOP_POLLING;
10235
10236 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10237 {
10238 if (hscroll_windows (selected_window))
10239 goto retry;
10240
10241 XWINDOW (selected_window)->must_be_updated_p = 1;
10242 pause = update_frame (sf, 0, 0);
10243 }
10244
10245 /* We may have called echo_area_display at the top of this
10246 function. If the echo area is on another frame, that may
10247 have put text on a frame other than the selected one, so the
10248 above call to update_frame would not have caught it. Catch
10249 it here. */
10250 mini_window = FRAME_MINIBUF_WINDOW (sf);
10251 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10252
10253 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10254 {
10255 XWINDOW (mini_window)->must_be_updated_p = 1;
10256 pause |= update_frame (mini_frame, 0, 0);
10257 if (!pause && hscroll_windows (mini_window))
10258 goto retry;
10259 }
10260 }
10261
10262 /* If display was paused because of pending input, make sure we do a
10263 thorough update the next time. */
10264 if (pause)
10265 {
10266 /* Prevent the optimization at the beginning of
10267 redisplay_internal that tries a single-line update of the
10268 line containing the cursor in the selected window. */
10269 CHARPOS (this_line_start_pos) = 0;
10270
10271 /* Let the overlay arrow be updated the next time. */
10272 update_overlay_arrows (0);
10273
10274 /* If we pause after scrolling, some rows in the current
10275 matrices of some windows are not valid. */
10276 if (!WINDOW_FULL_WIDTH_P (w)
10277 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10278 update_mode_lines = 1;
10279 }
10280 else
10281 {
10282 if (!consider_all_windows_p)
10283 {
10284 /* This has already been done above if
10285 consider_all_windows_p is set. */
10286 mark_window_display_accurate_1 (w, 1);
10287
10288 /* Say overlay arrows are up to date. */
10289 update_overlay_arrows (1);
10290
10291 if (FRAME_DISPLAY (sf)->frame_up_to_date_hook != 0)
10292 FRAME_DISPLAY (sf)->frame_up_to_date_hook (sf);
10293 }
10294
10295 update_mode_lines = 0;
10296 windows_or_buffers_changed = 0;
10297 cursor_type_changed = 0;
10298 }
10299
10300 /* Start SIGIO interrupts coming again. Having them off during the
10301 code above makes it less likely one will discard output, but not
10302 impossible, since there might be stuff in the system buffer here.
10303 But it is much hairier to try to do anything about that. */
10304 if (interrupt_input)
10305 request_sigio ();
10306 RESUME_POLLING;
10307
10308 /* If a frame has become visible which was not before, redisplay
10309 again, so that we display it. Expose events for such a frame
10310 (which it gets when becoming visible) don't call the parts of
10311 redisplay constructing glyphs, so simply exposing a frame won't
10312 display anything in this case. So, we have to display these
10313 frames here explicitly. */
10314 if (!pause)
10315 {
10316 Lisp_Object tail, frame;
10317 int new_count = 0;
10318
10319 FOR_EACH_FRAME (tail, frame)
10320 {
10321 int this_is_visible = 0;
10322
10323 if (XFRAME (frame)->visible)
10324 this_is_visible = 1;
10325 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10326 if (XFRAME (frame)->visible)
10327 this_is_visible = 1;
10328
10329 if (this_is_visible)
10330 new_count++;
10331 }
10332
10333 if (new_count != number_of_visible_frames)
10334 windows_or_buffers_changed++;
10335 }
10336
10337 /* Change frame size now if a change is pending. */
10338 do_pending_window_change (1);
10339
10340 /* If we just did a pending size change, or have additional
10341 visible frames, redisplay again. */
10342 if (windows_or_buffers_changed && !pause)
10343 goto retry;
10344
10345 end_of_redisplay:
10346 unbind_to (count, Qnil);
10347 RESUME_POLLING;
10348 }
10349
10350
10351 /* Redisplay, but leave alone any recent echo area message unless
10352 another message has been requested in its place.
10353
10354 This is useful in situations where you need to redisplay but no
10355 user action has occurred, making it inappropriate for the message
10356 area to be cleared. See tracking_off and
10357 wait_reading_process_input for examples of these situations.
10358
10359 FROM_WHERE is an integer saying from where this function was
10360 called. This is useful for debugging. */
10361
10362 void
10363 redisplay_preserve_echo_area (from_where)
10364 int from_where;
10365 {
10366 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10367
10368 if (!NILP (echo_area_buffer[1]))
10369 {
10370 /* We have a previously displayed message, but no current
10371 message. Redisplay the previous message. */
10372 display_last_displayed_message_p = 1;
10373 redisplay_internal (1);
10374 display_last_displayed_message_p = 0;
10375 }
10376 else
10377 redisplay_internal (1);
10378 }
10379
10380
10381 /* Function registered with record_unwind_protect in
10382 redisplay_internal. Reset redisplaying_p to the value it had
10383 before redisplay_internal was called, and clear
10384 prevent_freeing_realized_faces_p. It also selects the previously
10385 selected frame. */
10386
10387 static Lisp_Object
10388 unwind_redisplay (val)
10389 Lisp_Object val;
10390 {
10391 Lisp_Object old_redisplaying_p, old_frame;
10392
10393 old_redisplaying_p = XCAR (val);
10394 redisplaying_p = XFASTINT (old_redisplaying_p);
10395 old_frame = XCDR (val);
10396 if (! EQ (old_frame, selected_frame))
10397 select_frame_for_redisplay (old_frame);
10398 return Qnil;
10399 }
10400
10401
10402 /* Mark the display of window W as accurate or inaccurate. If
10403 ACCURATE_P is non-zero mark display of W as accurate. If
10404 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10405 redisplay_internal is called. */
10406
10407 static void
10408 mark_window_display_accurate_1 (w, accurate_p)
10409 struct window *w;
10410 int accurate_p;
10411 {
10412 if (BUFFERP (w->buffer))
10413 {
10414 struct buffer *b = XBUFFER (w->buffer);
10415
10416 w->last_modified
10417 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10418 w->last_overlay_modified
10419 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10420 w->last_had_star
10421 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10422
10423 if (accurate_p)
10424 {
10425 b->clip_changed = 0;
10426 b->prevent_redisplay_optimizations_p = 0;
10427
10428 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10429 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10430 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10431 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10432
10433 w->current_matrix->buffer = b;
10434 w->current_matrix->begv = BUF_BEGV (b);
10435 w->current_matrix->zv = BUF_ZV (b);
10436
10437 w->last_cursor = w->cursor;
10438 w->last_cursor_off_p = w->cursor_off_p;
10439
10440 if (w == XWINDOW (selected_window))
10441 w->last_point = make_number (BUF_PT (b));
10442 else
10443 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10444 }
10445 }
10446
10447 if (accurate_p)
10448 {
10449 w->window_end_valid = w->buffer;
10450 #if 0 /* This is incorrect with variable-height lines. */
10451 xassert (XINT (w->window_end_vpos)
10452 < (WINDOW_TOTAL_LINES (w)
10453 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10454 #endif
10455 w->update_mode_line = Qnil;
10456 }
10457 }
10458
10459
10460 /* Mark the display of windows in the window tree rooted at WINDOW as
10461 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10462 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10463 be redisplayed the next time redisplay_internal is called. */
10464
10465 void
10466 mark_window_display_accurate (window, accurate_p)
10467 Lisp_Object window;
10468 int accurate_p;
10469 {
10470 struct window *w;
10471
10472 for (; !NILP (window); window = w->next)
10473 {
10474 w = XWINDOW (window);
10475 mark_window_display_accurate_1 (w, accurate_p);
10476
10477 if (!NILP (w->vchild))
10478 mark_window_display_accurate (w->vchild, accurate_p);
10479 if (!NILP (w->hchild))
10480 mark_window_display_accurate (w->hchild, accurate_p);
10481 }
10482
10483 if (accurate_p)
10484 {
10485 update_overlay_arrows (1);
10486 }
10487 else
10488 {
10489 /* Force a thorough redisplay the next time by setting
10490 last_arrow_position and last_arrow_string to t, which is
10491 unequal to any useful value of Voverlay_arrow_... */
10492 update_overlay_arrows (-1);
10493 }
10494 }
10495
10496
10497 /* Return value in display table DP (Lisp_Char_Table *) for character
10498 C. Since a display table doesn't have any parent, we don't have to
10499 follow parent. Do not call this function directly but use the
10500 macro DISP_CHAR_VECTOR. */
10501
10502 Lisp_Object
10503 disp_char_vector (dp, c)
10504 struct Lisp_Char_Table *dp;
10505 int c;
10506 {
10507 int code[4], i;
10508 Lisp_Object val;
10509
10510 if (SINGLE_BYTE_CHAR_P (c))
10511 return (dp->contents[c]);
10512
10513 SPLIT_CHAR (c, code[0], code[1], code[2]);
10514 if (code[1] < 32)
10515 code[1] = -1;
10516 else if (code[2] < 32)
10517 code[2] = -1;
10518
10519 /* Here, the possible range of code[0] (== charset ID) is
10520 128..max_charset. Since the top level char table contains data
10521 for multibyte characters after 256th element, we must increment
10522 code[0] by 128 to get a correct index. */
10523 code[0] += 128;
10524 code[3] = -1; /* anchor */
10525
10526 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10527 {
10528 val = dp->contents[code[i]];
10529 if (!SUB_CHAR_TABLE_P (val))
10530 return (NILP (val) ? dp->defalt : val);
10531 }
10532
10533 /* Here, val is a sub char table. We return the default value of
10534 it. */
10535 return (dp->defalt);
10536 }
10537
10538
10539 \f
10540 /***********************************************************************
10541 Window Redisplay
10542 ***********************************************************************/
10543
10544 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10545
10546 static void
10547 redisplay_windows (window)
10548 Lisp_Object window;
10549 {
10550 while (!NILP (window))
10551 {
10552 struct window *w = XWINDOW (window);
10553
10554 if (!NILP (w->hchild))
10555 redisplay_windows (w->hchild);
10556 else if (!NILP (w->vchild))
10557 redisplay_windows (w->vchild);
10558 else
10559 {
10560 displayed_buffer = XBUFFER (w->buffer);
10561 /* Use list_of_error, not Qerror, so that
10562 we catch only errors and don't run the debugger. */
10563 internal_condition_case_1 (redisplay_window_0, window,
10564 list_of_error,
10565 redisplay_window_error);
10566 }
10567
10568 window = w->next;
10569 }
10570 }
10571
10572 static Lisp_Object
10573 redisplay_window_error ()
10574 {
10575 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10576 return Qnil;
10577 }
10578
10579 static Lisp_Object
10580 redisplay_window_0 (window)
10581 Lisp_Object window;
10582 {
10583 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10584 redisplay_window (window, 0);
10585 return Qnil;
10586 }
10587
10588 static Lisp_Object
10589 redisplay_window_1 (window)
10590 Lisp_Object window;
10591 {
10592 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10593 redisplay_window (window, 1);
10594 return Qnil;
10595 }
10596 \f
10597
10598 /* Increment GLYPH until it reaches END or CONDITION fails while
10599 adding (GLYPH)->pixel_width to X. */
10600
10601 #define SKIP_GLYPHS(glyph, end, x, condition) \
10602 do \
10603 { \
10604 (x) += (glyph)->pixel_width; \
10605 ++(glyph); \
10606 } \
10607 while ((glyph) < (end) && (condition))
10608
10609
10610 /* Set cursor position of W. PT is assumed to be displayed in ROW.
10611 DELTA is the number of bytes by which positions recorded in ROW
10612 differ from current buffer positions. */
10613
10614 void
10615 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10616 struct window *w;
10617 struct glyph_row *row;
10618 struct glyph_matrix *matrix;
10619 int delta, delta_bytes, dy, dvpos;
10620 {
10621 struct glyph *glyph = row->glyphs[TEXT_AREA];
10622 struct glyph *end = glyph + row->used[TEXT_AREA];
10623 /* The first glyph that starts a sequence of glyphs from string. */
10624 struct glyph *string_start;
10625 /* The X coordinate of string_start. */
10626 int string_start_x;
10627 /* The last known character position. */
10628 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10629 /* The last known character position before string_start. */
10630 int string_before_pos;
10631 int x = row->x;
10632 int pt_old = PT - delta;
10633
10634 /* Skip over glyphs not having an object at the start of the row.
10635 These are special glyphs like truncation marks on terminal
10636 frames. */
10637 if (row->displays_text_p)
10638 while (glyph < end
10639 && INTEGERP (glyph->object)
10640 && glyph->charpos < 0)
10641 {
10642 x += glyph->pixel_width;
10643 ++glyph;
10644 }
10645
10646 string_start = NULL;
10647 while (glyph < end
10648 && !INTEGERP (glyph->object)
10649 && (!BUFFERP (glyph->object)
10650 || (last_pos = glyph->charpos) < pt_old))
10651 {
10652 if (! STRINGP (glyph->object))
10653 {
10654 string_start = NULL;
10655 x += glyph->pixel_width;
10656 ++glyph;
10657 }
10658 else
10659 {
10660 string_before_pos = last_pos;
10661 string_start = glyph;
10662 string_start_x = x;
10663 /* Skip all glyphs from string. */
10664 SKIP_GLYPHS (glyph, end, x, STRINGP (glyph->object));
10665 }
10666 }
10667
10668 if (string_start
10669 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
10670 {
10671 /* We may have skipped over point because the previous glyphs
10672 are from string. As there's no easy way to know the
10673 character position of the current glyph, find the correct
10674 glyph on point by scanning from string_start again. */
10675 Lisp_Object limit;
10676 Lisp_Object string;
10677 int pos;
10678
10679 limit = make_number (pt_old + 1);
10680 end = glyph;
10681 glyph = string_start;
10682 x = string_start_x;
10683 string = glyph->object;
10684 pos = string_buffer_position (w, string, string_before_pos);
10685 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
10686 because we always put cursor after overlay strings. */
10687 while (pos == 0 && glyph < end)
10688 {
10689 string = glyph->object;
10690 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10691 if (glyph < end)
10692 pos = string_buffer_position (w, glyph->object, string_before_pos);
10693 }
10694
10695 while (glyph < end)
10696 {
10697 pos = XINT (Fnext_single_char_property_change
10698 (make_number (pos), Qdisplay, Qnil, limit));
10699 if (pos > pt_old)
10700 break;
10701 /* Skip glyphs from the same string. */
10702 string = glyph->object;
10703 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10704 /* Skip glyphs from an overlay. */
10705 while (glyph < end
10706 && ! string_buffer_position (w, glyph->object, pos))
10707 {
10708 string = glyph->object;
10709 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10710 }
10711 }
10712 }
10713
10714 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
10715 w->cursor.x = x;
10716 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
10717 w->cursor.y = row->y + dy;
10718
10719 if (w == XWINDOW (selected_window))
10720 {
10721 if (!row->continued_p
10722 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
10723 && row->x == 0)
10724 {
10725 this_line_buffer = XBUFFER (w->buffer);
10726
10727 CHARPOS (this_line_start_pos)
10728 = MATRIX_ROW_START_CHARPOS (row) + delta;
10729 BYTEPOS (this_line_start_pos)
10730 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
10731
10732 CHARPOS (this_line_end_pos)
10733 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
10734 BYTEPOS (this_line_end_pos)
10735 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
10736
10737 this_line_y = w->cursor.y;
10738 this_line_pixel_height = row->height;
10739 this_line_vpos = w->cursor.vpos;
10740 this_line_start_x = row->x;
10741 }
10742 else
10743 CHARPOS (this_line_start_pos) = 0;
10744 }
10745 }
10746
10747
10748 /* Run window scroll functions, if any, for WINDOW with new window
10749 start STARTP. Sets the window start of WINDOW to that position.
10750
10751 We assume that the window's buffer is really current. */
10752
10753 static INLINE struct text_pos
10754 run_window_scroll_functions (window, startp)
10755 Lisp_Object window;
10756 struct text_pos startp;
10757 {
10758 struct window *w = XWINDOW (window);
10759 SET_MARKER_FROM_TEXT_POS (w->start, startp);
10760
10761 if (current_buffer != XBUFFER (w->buffer))
10762 abort ();
10763
10764 if (!NILP (Vwindow_scroll_functions))
10765 {
10766 run_hook_with_args_2 (Qwindow_scroll_functions, window,
10767 make_number (CHARPOS (startp)));
10768 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10769 /* In case the hook functions switch buffers. */
10770 if (current_buffer != XBUFFER (w->buffer))
10771 set_buffer_internal_1 (XBUFFER (w->buffer));
10772 }
10773
10774 return startp;
10775 }
10776
10777
10778 /* Make sure the line containing the cursor is fully visible.
10779 A value of 1 means there is nothing to be done.
10780 (Either the line is fully visible, or it cannot be made so,
10781 or we cannot tell.)
10782
10783 If FORCE_P is non-zero, return 0 even if partial visible cursor row
10784 is higher than window.
10785
10786 A value of 0 means the caller should do scrolling
10787 as if point had gone off the screen. */
10788
10789 static int
10790 make_cursor_line_fully_visible (w, force_p)
10791 struct window *w;
10792 int force_p;
10793 {
10794 struct glyph_matrix *matrix;
10795 struct glyph_row *row;
10796 int window_height;
10797
10798 /* It's not always possible to find the cursor, e.g, when a window
10799 is full of overlay strings. Don't do anything in that case. */
10800 if (w->cursor.vpos < 0)
10801 return 1;
10802
10803 matrix = w->desired_matrix;
10804 row = MATRIX_ROW (matrix, w->cursor.vpos);
10805
10806 /* If the cursor row is not partially visible, there's nothing to do. */
10807 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
10808 return 1;
10809
10810 /* If the row the cursor is in is taller than the window's height,
10811 it's not clear what to do, so do nothing. */
10812 window_height = window_box_height (w);
10813 if (row->height >= window_height)
10814 {
10815 if (!force_p || w->vscroll)
10816 return 1;
10817 }
10818 return 0;
10819
10820 #if 0
10821 /* This code used to try to scroll the window just enough to make
10822 the line visible. It returned 0 to say that the caller should
10823 allocate larger glyph matrices. */
10824
10825 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
10826 {
10827 int dy = row->height - row->visible_height;
10828 w->vscroll = 0;
10829 w->cursor.y += dy;
10830 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10831 }
10832 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
10833 {
10834 int dy = - (row->height - row->visible_height);
10835 w->vscroll = dy;
10836 w->cursor.y += dy;
10837 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10838 }
10839
10840 /* When we change the cursor y-position of the selected window,
10841 change this_line_y as well so that the display optimization for
10842 the cursor line of the selected window in redisplay_internal uses
10843 the correct y-position. */
10844 if (w == XWINDOW (selected_window))
10845 this_line_y = w->cursor.y;
10846
10847 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
10848 redisplay with larger matrices. */
10849 if (matrix->nrows < required_matrix_height (w))
10850 {
10851 fonts_changed_p = 1;
10852 return 0;
10853 }
10854
10855 return 1;
10856 #endif /* 0 */
10857 }
10858
10859
10860 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
10861 non-zero means only WINDOW is redisplayed in redisplay_internal.
10862 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
10863 in redisplay_window to bring a partially visible line into view in
10864 the case that only the cursor has moved.
10865
10866 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
10867 last screen line's vertical height extends past the end of the screen.
10868
10869 Value is
10870
10871 1 if scrolling succeeded
10872
10873 0 if scrolling didn't find point.
10874
10875 -1 if new fonts have been loaded so that we must interrupt
10876 redisplay, adjust glyph matrices, and try again. */
10877
10878 enum
10879 {
10880 SCROLLING_SUCCESS,
10881 SCROLLING_FAILED,
10882 SCROLLING_NEED_LARGER_MATRICES
10883 };
10884
10885 static int
10886 try_scrolling (window, just_this_one_p, scroll_conservatively,
10887 scroll_step, temp_scroll_step, last_line_misfit)
10888 Lisp_Object window;
10889 int just_this_one_p;
10890 EMACS_INT scroll_conservatively, scroll_step;
10891 int temp_scroll_step;
10892 int last_line_misfit;
10893 {
10894 struct window *w = XWINDOW (window);
10895 struct frame *f = XFRAME (w->frame);
10896 struct text_pos scroll_margin_pos;
10897 struct text_pos pos;
10898 struct text_pos startp;
10899 struct it it;
10900 Lisp_Object window_end;
10901 int this_scroll_margin;
10902 int dy = 0;
10903 int scroll_max;
10904 int rc;
10905 int amount_to_scroll = 0;
10906 Lisp_Object aggressive;
10907 int height;
10908 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
10909
10910 #if GLYPH_DEBUG
10911 debug_method_add (w, "try_scrolling");
10912 #endif
10913
10914 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10915
10916 /* Compute scroll margin height in pixels. We scroll when point is
10917 within this distance from the top or bottom of the window. */
10918 if (scroll_margin > 0)
10919 {
10920 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
10921 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
10922 }
10923 else
10924 this_scroll_margin = 0;
10925
10926 /* Force scroll_conservatively to have a reasonable value so it doesn't
10927 cause an overflow while computing how much to scroll. */
10928 if (scroll_conservatively)
10929 scroll_conservatively = min (scroll_conservatively,
10930 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
10931
10932 /* Compute how much we should try to scroll maximally to bring point
10933 into view. */
10934 if (scroll_step || scroll_conservatively || temp_scroll_step)
10935 scroll_max = max (scroll_step,
10936 max (scroll_conservatively, temp_scroll_step));
10937 else if (NUMBERP (current_buffer->scroll_down_aggressively)
10938 || NUMBERP (current_buffer->scroll_up_aggressively))
10939 /* We're trying to scroll because of aggressive scrolling
10940 but no scroll_step is set. Choose an arbitrary one. Maybe
10941 there should be a variable for this. */
10942 scroll_max = 10;
10943 else
10944 scroll_max = 0;
10945 scroll_max *= FRAME_LINE_HEIGHT (f);
10946
10947 /* Decide whether we have to scroll down. Start at the window end
10948 and move this_scroll_margin up to find the position of the scroll
10949 margin. */
10950 window_end = Fwindow_end (window, Qt);
10951
10952 too_near_end:
10953
10954 CHARPOS (scroll_margin_pos) = XINT (window_end);
10955 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
10956
10957 if (this_scroll_margin || extra_scroll_margin_lines)
10958 {
10959 start_display (&it, w, scroll_margin_pos);
10960 if (this_scroll_margin)
10961 move_it_vertically (&it, - this_scroll_margin);
10962 if (extra_scroll_margin_lines)
10963 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
10964 scroll_margin_pos = it.current.pos;
10965 }
10966
10967 if (PT >= CHARPOS (scroll_margin_pos))
10968 {
10969 int y0;
10970
10971 /* Point is in the scroll margin at the bottom of the window, or
10972 below. Compute a new window start that makes point visible. */
10973
10974 /* Compute the distance from the scroll margin to PT.
10975 Give up if the distance is greater than scroll_max. */
10976 start_display (&it, w, scroll_margin_pos);
10977 y0 = it.current_y;
10978 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10979 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10980
10981 /* To make point visible, we have to move the window start
10982 down so that the line the cursor is in is visible, which
10983 means we have to add in the height of the cursor line. */
10984 dy = line_bottom_y (&it) - y0;
10985
10986 if (dy > scroll_max)
10987 return SCROLLING_FAILED;
10988
10989 /* Move the window start down. If scrolling conservatively,
10990 move it just enough down to make point visible. If
10991 scroll_step is set, move it down by scroll_step. */
10992 start_display (&it, w, startp);
10993
10994 if (scroll_conservatively)
10995 /* Set AMOUNT_TO_SCROLL to at least one line,
10996 and at most scroll_conservatively lines. */
10997 amount_to_scroll
10998 = min (max (dy, FRAME_LINE_HEIGHT (f)),
10999 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11000 else if (scroll_step || temp_scroll_step)
11001 amount_to_scroll = scroll_max;
11002 else
11003 {
11004 aggressive = current_buffer->scroll_up_aggressively;
11005 height = WINDOW_BOX_TEXT_HEIGHT (w);
11006 if (NUMBERP (aggressive))
11007 {
11008 double float_amount = XFLOATINT (aggressive) * height;
11009 amount_to_scroll = float_amount;
11010 if (amount_to_scroll == 0 && float_amount > 0)
11011 amount_to_scroll = 1;
11012 }
11013 }
11014
11015 if (amount_to_scroll <= 0)
11016 return SCROLLING_FAILED;
11017
11018 /* If moving by amount_to_scroll leaves STARTP unchanged,
11019 move it down one screen line. */
11020
11021 move_it_vertically (&it, amount_to_scroll);
11022 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11023 move_it_by_lines (&it, 1, 1);
11024 startp = it.current.pos;
11025 }
11026 else
11027 {
11028 /* See if point is inside the scroll margin at the top of the
11029 window. */
11030 scroll_margin_pos = startp;
11031 if (this_scroll_margin)
11032 {
11033 start_display (&it, w, startp);
11034 move_it_vertically (&it, this_scroll_margin);
11035 scroll_margin_pos = it.current.pos;
11036 }
11037
11038 if (PT < CHARPOS (scroll_margin_pos))
11039 {
11040 /* Point is in the scroll margin at the top of the window or
11041 above what is displayed in the window. */
11042 int y0;
11043
11044 /* Compute the vertical distance from PT to the scroll
11045 margin position. Give up if distance is greater than
11046 scroll_max. */
11047 SET_TEXT_POS (pos, PT, PT_BYTE);
11048 start_display (&it, w, pos);
11049 y0 = it.current_y;
11050 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11051 it.last_visible_y, -1,
11052 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11053 dy = it.current_y - y0;
11054 if (dy > scroll_max)
11055 return SCROLLING_FAILED;
11056
11057 /* Compute new window start. */
11058 start_display (&it, w, startp);
11059
11060 if (scroll_conservatively)
11061 amount_to_scroll =
11062 max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11063 else if (scroll_step || temp_scroll_step)
11064 amount_to_scroll = scroll_max;
11065 else
11066 {
11067 aggressive = current_buffer->scroll_down_aggressively;
11068 height = WINDOW_BOX_TEXT_HEIGHT (w);
11069 if (NUMBERP (aggressive))
11070 {
11071 double float_amount = XFLOATINT (aggressive) * height;
11072 amount_to_scroll = float_amount;
11073 if (amount_to_scroll == 0 && float_amount > 0)
11074 amount_to_scroll = 1;
11075 }
11076 }
11077
11078 if (amount_to_scroll <= 0)
11079 return SCROLLING_FAILED;
11080
11081 move_it_vertically (&it, - amount_to_scroll);
11082 startp = it.current.pos;
11083 }
11084 }
11085
11086 /* Run window scroll functions. */
11087 startp = run_window_scroll_functions (window, startp);
11088
11089 /* Display the window. Give up if new fonts are loaded, or if point
11090 doesn't appear. */
11091 if (!try_window (window, startp))
11092 rc = SCROLLING_NEED_LARGER_MATRICES;
11093 else if (w->cursor.vpos < 0)
11094 {
11095 clear_glyph_matrix (w->desired_matrix);
11096 rc = SCROLLING_FAILED;
11097 }
11098 else
11099 {
11100 /* Maybe forget recorded base line for line number display. */
11101 if (!just_this_one_p
11102 || current_buffer->clip_changed
11103 || BEG_UNCHANGED < CHARPOS (startp))
11104 w->base_line_number = Qnil;
11105
11106 /* If cursor ends up on a partially visible line,
11107 treat that as being off the bottom of the screen. */
11108 if (! make_cursor_line_fully_visible (w, extra_scroll_margin_lines <= 1))
11109 {
11110 clear_glyph_matrix (w->desired_matrix);
11111 ++extra_scroll_margin_lines;
11112 goto too_near_end;
11113 }
11114 rc = SCROLLING_SUCCESS;
11115 }
11116
11117 return rc;
11118 }
11119
11120
11121 /* Compute a suitable window start for window W if display of W starts
11122 on a continuation line. Value is non-zero if a new window start
11123 was computed.
11124
11125 The new window start will be computed, based on W's width, starting
11126 from the start of the continued line. It is the start of the
11127 screen line with the minimum distance from the old start W->start. */
11128
11129 static int
11130 compute_window_start_on_continuation_line (w)
11131 struct window *w;
11132 {
11133 struct text_pos pos, start_pos;
11134 int window_start_changed_p = 0;
11135
11136 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11137
11138 /* If window start is on a continuation line... Window start may be
11139 < BEGV in case there's invisible text at the start of the
11140 buffer (M-x rmail, for example). */
11141 if (CHARPOS (start_pos) > BEGV
11142 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11143 {
11144 struct it it;
11145 struct glyph_row *row;
11146
11147 /* Handle the case that the window start is out of range. */
11148 if (CHARPOS (start_pos) < BEGV)
11149 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11150 else if (CHARPOS (start_pos) > ZV)
11151 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11152
11153 /* Find the start of the continued line. This should be fast
11154 because scan_buffer is fast (newline cache). */
11155 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11156 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11157 row, DEFAULT_FACE_ID);
11158 reseat_at_previous_visible_line_start (&it);
11159
11160 /* If the line start is "too far" away from the window start,
11161 say it takes too much time to compute a new window start. */
11162 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11163 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11164 {
11165 int min_distance, distance;
11166
11167 /* Move forward by display lines to find the new window
11168 start. If window width was enlarged, the new start can
11169 be expected to be > the old start. If window width was
11170 decreased, the new window start will be < the old start.
11171 So, we're looking for the display line start with the
11172 minimum distance from the old window start. */
11173 pos = it.current.pos;
11174 min_distance = INFINITY;
11175 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11176 distance < min_distance)
11177 {
11178 min_distance = distance;
11179 pos = it.current.pos;
11180 move_it_by_lines (&it, 1, 0);
11181 }
11182
11183 /* Set the window start there. */
11184 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11185 window_start_changed_p = 1;
11186 }
11187 }
11188
11189 return window_start_changed_p;
11190 }
11191
11192
11193 /* Try cursor movement in case text has not changed in window WINDOW,
11194 with window start STARTP. Value is
11195
11196 CURSOR_MOVEMENT_SUCCESS if successful
11197
11198 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11199
11200 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11201 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11202 we want to scroll as if scroll-step were set to 1. See the code.
11203
11204 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11205 which case we have to abort this redisplay, and adjust matrices
11206 first. */
11207
11208 enum
11209 {
11210 CURSOR_MOVEMENT_SUCCESS,
11211 CURSOR_MOVEMENT_CANNOT_BE_USED,
11212 CURSOR_MOVEMENT_MUST_SCROLL,
11213 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11214 };
11215
11216 static int
11217 try_cursor_movement (window, startp, scroll_step)
11218 Lisp_Object window;
11219 struct text_pos startp;
11220 int *scroll_step;
11221 {
11222 struct window *w = XWINDOW (window);
11223 struct frame *f = XFRAME (w->frame);
11224 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11225
11226 #if GLYPH_DEBUG
11227 if (inhibit_try_cursor_movement)
11228 return rc;
11229 #endif
11230
11231 /* Handle case where text has not changed, only point, and it has
11232 not moved off the frame. */
11233 if (/* Point may be in this window. */
11234 PT >= CHARPOS (startp)
11235 /* Selective display hasn't changed. */
11236 && !current_buffer->clip_changed
11237 /* Function force-mode-line-update is used to force a thorough
11238 redisplay. It sets either windows_or_buffers_changed or
11239 update_mode_lines. So don't take a shortcut here for these
11240 cases. */
11241 && !update_mode_lines
11242 && !windows_or_buffers_changed
11243 && !cursor_type_changed
11244 /* Can't use this case if highlighting a region. When a
11245 region exists, cursor movement has to do more than just
11246 set the cursor. */
11247 && !(!NILP (Vtransient_mark_mode)
11248 && !NILP (current_buffer->mark_active))
11249 && NILP (w->region_showing)
11250 && NILP (Vshow_trailing_whitespace)
11251 /* Right after splitting windows, last_point may be nil. */
11252 && INTEGERP (w->last_point)
11253 /* This code is not used for mini-buffer for the sake of the case
11254 of redisplaying to replace an echo area message; since in
11255 that case the mini-buffer contents per se are usually
11256 unchanged. This code is of no real use in the mini-buffer
11257 since the handling of this_line_start_pos, etc., in redisplay
11258 handles the same cases. */
11259 && !EQ (window, minibuf_window)
11260 /* When splitting windows or for new windows, it happens that
11261 redisplay is called with a nil window_end_vpos or one being
11262 larger than the window. This should really be fixed in
11263 window.c. I don't have this on my list, now, so we do
11264 approximately the same as the old redisplay code. --gerd. */
11265 && INTEGERP (w->window_end_vpos)
11266 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11267 && (FRAME_WINDOW_P (f)
11268 || !overlay_arrow_in_current_buffer_p ()))
11269 {
11270 int this_scroll_margin;
11271 struct glyph_row *row = NULL;
11272
11273 #if GLYPH_DEBUG
11274 debug_method_add (w, "cursor movement");
11275 #endif
11276
11277 /* Scroll if point within this distance from the top or bottom
11278 of the window. This is a pixel value. */
11279 this_scroll_margin = max (0, scroll_margin);
11280 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11281 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11282
11283 /* Start with the row the cursor was displayed during the last
11284 not paused redisplay. Give up if that row is not valid. */
11285 if (w->last_cursor.vpos < 0
11286 || w->last_cursor.vpos >= w->current_matrix->nrows)
11287 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11288 else
11289 {
11290 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11291 if (row->mode_line_p)
11292 ++row;
11293 if (!row->enabled_p)
11294 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11295 }
11296
11297 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11298 {
11299 int scroll_p = 0;
11300 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11301
11302 if (PT > XFASTINT (w->last_point))
11303 {
11304 /* Point has moved forward. */
11305 while (MATRIX_ROW_END_CHARPOS (row) < PT
11306 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11307 {
11308 xassert (row->enabled_p);
11309 ++row;
11310 }
11311
11312 /* The end position of a row equals the start position
11313 of the next row. If PT is there, we would rather
11314 display it in the next line. */
11315 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11316 && MATRIX_ROW_END_CHARPOS (row) == PT
11317 && !cursor_row_p (w, row))
11318 ++row;
11319
11320 /* If within the scroll margin, scroll. Note that
11321 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11322 the next line would be drawn, and that
11323 this_scroll_margin can be zero. */
11324 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11325 || PT > MATRIX_ROW_END_CHARPOS (row)
11326 /* Line is completely visible last line in window
11327 and PT is to be set in the next line. */
11328 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11329 && PT == MATRIX_ROW_END_CHARPOS (row)
11330 && !row->ends_at_zv_p
11331 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11332 scroll_p = 1;
11333 }
11334 else if (PT < XFASTINT (w->last_point))
11335 {
11336 /* Cursor has to be moved backward. Note that PT >=
11337 CHARPOS (startp) because of the outer
11338 if-statement. */
11339 while (!row->mode_line_p
11340 && (MATRIX_ROW_START_CHARPOS (row) > PT
11341 || (MATRIX_ROW_START_CHARPOS (row) == PT
11342 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11343 && (row->y > this_scroll_margin
11344 || CHARPOS (startp) == BEGV))
11345 {
11346 xassert (row->enabled_p);
11347 --row;
11348 }
11349
11350 /* Consider the following case: Window starts at BEGV,
11351 there is invisible, intangible text at BEGV, so that
11352 display starts at some point START > BEGV. It can
11353 happen that we are called with PT somewhere between
11354 BEGV and START. Try to handle that case. */
11355 if (row < w->current_matrix->rows
11356 || row->mode_line_p)
11357 {
11358 row = w->current_matrix->rows;
11359 if (row->mode_line_p)
11360 ++row;
11361 }
11362
11363 /* Due to newlines in overlay strings, we may have to
11364 skip forward over overlay strings. */
11365 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11366 && MATRIX_ROW_END_CHARPOS (row) == PT
11367 && !cursor_row_p (w, row))
11368 ++row;
11369
11370 /* If within the scroll margin, scroll. */
11371 if (row->y < this_scroll_margin
11372 && CHARPOS (startp) != BEGV)
11373 scroll_p = 1;
11374 }
11375
11376 if (PT < MATRIX_ROW_START_CHARPOS (row)
11377 || PT > MATRIX_ROW_END_CHARPOS (row))
11378 {
11379 /* if PT is not in the glyph row, give up. */
11380 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11381 }
11382 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
11383 {
11384 if (PT == MATRIX_ROW_END_CHARPOS (row)
11385 && !row->ends_at_zv_p
11386 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11387 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11388 else if (row->height > window_box_height (w))
11389 {
11390 /* If we end up in a partially visible line, let's
11391 make it fully visible, except when it's taller
11392 than the window, in which case we can't do much
11393 about it. */
11394 *scroll_step = 1;
11395 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11396 }
11397 else
11398 {
11399 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11400 if (!make_cursor_line_fully_visible (w, 0))
11401 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11402 else
11403 rc = CURSOR_MOVEMENT_SUCCESS;
11404 }
11405 }
11406 else if (scroll_p)
11407 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11408 else
11409 {
11410 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11411 rc = CURSOR_MOVEMENT_SUCCESS;
11412 }
11413 }
11414 }
11415
11416 return rc;
11417 }
11418
11419 void
11420 set_vertical_scroll_bar (w)
11421 struct window *w;
11422 {
11423 int start, end, whole;
11424
11425 /* Calculate the start and end positions for the current window.
11426 At some point, it would be nice to choose between scrollbars
11427 which reflect the whole buffer size, with special markers
11428 indicating narrowing, and scrollbars which reflect only the
11429 visible region.
11430
11431 Note that mini-buffers sometimes aren't displaying any text. */
11432 if (!MINI_WINDOW_P (w)
11433 || (w == XWINDOW (minibuf_window)
11434 && NILP (echo_area_buffer[0])))
11435 {
11436 struct buffer *buf = XBUFFER (w->buffer);
11437 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11438 start = marker_position (w->start) - BUF_BEGV (buf);
11439 /* I don't think this is guaranteed to be right. For the
11440 moment, we'll pretend it is. */
11441 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11442
11443 if (end < start)
11444 end = start;
11445 if (whole < (end - start))
11446 whole = end - start;
11447 }
11448 else
11449 start = end = whole = 0;
11450
11451 /* Indicate what this scroll bar ought to be displaying now. */
11452 if (FRAME_DISPLAY (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
11453 (*FRAME_DISPLAY (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
11454 (w, end - start, whole, start);
11455 }
11456
11457
11458 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11459 selected_window is redisplayed.
11460
11461 We can return without actually redisplaying the window if
11462 fonts_changed_p is nonzero. In that case, redisplay_internal will
11463 retry. */
11464
11465 static void
11466 redisplay_window (window, just_this_one_p)
11467 Lisp_Object window;
11468 int just_this_one_p;
11469 {
11470 struct window *w = XWINDOW (window);
11471 struct frame *f = XFRAME (w->frame);
11472 struct buffer *buffer = XBUFFER (w->buffer);
11473 struct buffer *old = current_buffer;
11474 struct text_pos lpoint, opoint, startp;
11475 int update_mode_line;
11476 int tem;
11477 struct it it;
11478 /* Record it now because it's overwritten. */
11479 int current_matrix_up_to_date_p = 0;
11480 int used_current_matrix_p = 0;
11481 /* This is less strict than current_matrix_up_to_date_p.
11482 It indictes that the buffer contents and narrowing are unchanged. */
11483 int buffer_unchanged_p = 0;
11484 int temp_scroll_step = 0;
11485 int count = SPECPDL_INDEX ();
11486 int rc;
11487 int centering_position;
11488 int last_line_misfit = 0;
11489
11490 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11491 opoint = lpoint;
11492
11493 /* W must be a leaf window here. */
11494 xassert (!NILP (w->buffer));
11495 #if GLYPH_DEBUG
11496 *w->desired_matrix->method = 0;
11497 #endif
11498
11499 specbind (Qinhibit_point_motion_hooks, Qt);
11500
11501 reconsider_clip_changes (w, buffer);
11502
11503 /* Has the mode line to be updated? */
11504 update_mode_line = (!NILP (w->update_mode_line)
11505 || update_mode_lines
11506 || buffer->clip_changed
11507 || buffer->prevent_redisplay_optimizations_p);
11508
11509 if (MINI_WINDOW_P (w))
11510 {
11511 if (w == XWINDOW (echo_area_window)
11512 && !NILP (echo_area_buffer[0]))
11513 {
11514 if (update_mode_line)
11515 /* We may have to update a tty frame's menu bar or a
11516 tool-bar. Example `M-x C-h C-h C-g'. */
11517 goto finish_menu_bars;
11518 else
11519 /* We've already displayed the echo area glyphs in this window. */
11520 goto finish_scroll_bars;
11521 }
11522 else if ((w != XWINDOW (minibuf_window)
11523 || minibuf_level == 0)
11524 /* When buffer is nonempty, redisplay window normally. */
11525 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
11526 /* Quail displays non-mini buffers in minibuffer window.
11527 In that case, redisplay the window normally. */
11528 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
11529 {
11530 /* W is a mini-buffer window, but it's not active, so clear
11531 it. */
11532 int yb = window_text_bottom_y (w);
11533 struct glyph_row *row;
11534 int y;
11535
11536 for (y = 0, row = w->desired_matrix->rows;
11537 y < yb;
11538 y += row->height, ++row)
11539 blank_row (w, row, y);
11540 goto finish_scroll_bars;
11541 }
11542
11543 clear_glyph_matrix (w->desired_matrix);
11544 }
11545
11546 /* Otherwise set up data on this window; select its buffer and point
11547 value. */
11548 /* Really select the buffer, for the sake of buffer-local
11549 variables. */
11550 set_buffer_internal_1 (XBUFFER (w->buffer));
11551 SET_TEXT_POS (opoint, PT, PT_BYTE);
11552
11553 current_matrix_up_to_date_p
11554 = (!NILP (w->window_end_valid)
11555 && !current_buffer->clip_changed
11556 && !current_buffer->prevent_redisplay_optimizations_p
11557 && XFASTINT (w->last_modified) >= MODIFF
11558 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11559
11560 buffer_unchanged_p
11561 = (!NILP (w->window_end_valid)
11562 && !current_buffer->clip_changed
11563 && XFASTINT (w->last_modified) >= MODIFF
11564 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11565
11566 /* When windows_or_buffers_changed is non-zero, we can't rely on
11567 the window end being valid, so set it to nil there. */
11568 if (windows_or_buffers_changed)
11569 {
11570 /* If window starts on a continuation line, maybe adjust the
11571 window start in case the window's width changed. */
11572 if (XMARKER (w->start)->buffer == current_buffer)
11573 compute_window_start_on_continuation_line (w);
11574
11575 w->window_end_valid = Qnil;
11576 }
11577
11578 /* Some sanity checks. */
11579 CHECK_WINDOW_END (w);
11580 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
11581 abort ();
11582 if (BYTEPOS (opoint) < CHARPOS (opoint))
11583 abort ();
11584
11585 /* If %c is in mode line, update it if needed. */
11586 if (!NILP (w->column_number_displayed)
11587 /* This alternative quickly identifies a common case
11588 where no change is needed. */
11589 && !(PT == XFASTINT (w->last_point)
11590 && XFASTINT (w->last_modified) >= MODIFF
11591 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11592 && (XFASTINT (w->column_number_displayed)
11593 != (int) current_column ())) /* iftc */
11594 update_mode_line = 1;
11595
11596 /* Count number of windows showing the selected buffer. An indirect
11597 buffer counts as its base buffer. */
11598 if (!just_this_one_p)
11599 {
11600 struct buffer *current_base, *window_base;
11601 current_base = current_buffer;
11602 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11603 if (current_base->base_buffer)
11604 current_base = current_base->base_buffer;
11605 if (window_base->base_buffer)
11606 window_base = window_base->base_buffer;
11607 if (current_base == window_base)
11608 buffer_shared++;
11609 }
11610
11611 /* Point refers normally to the selected window. For any other
11612 window, set up appropriate value. */
11613 if (!EQ (window, selected_window))
11614 {
11615 int new_pt = XMARKER (w->pointm)->charpos;
11616 int new_pt_byte = marker_byte_position (w->pointm);
11617 if (new_pt < BEGV)
11618 {
11619 new_pt = BEGV;
11620 new_pt_byte = BEGV_BYTE;
11621 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
11622 }
11623 else if (new_pt > (ZV - 1))
11624 {
11625 new_pt = ZV;
11626 new_pt_byte = ZV_BYTE;
11627 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
11628 }
11629
11630 /* We don't use SET_PT so that the point-motion hooks don't run. */
11631 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
11632 }
11633
11634 /* If any of the character widths specified in the display table
11635 have changed, invalidate the width run cache. It's true that
11636 this may be a bit late to catch such changes, but the rest of
11637 redisplay goes (non-fatally) haywire when the display table is
11638 changed, so why should we worry about doing any better? */
11639 if (current_buffer->width_run_cache)
11640 {
11641 struct Lisp_Char_Table *disptab = buffer_display_table ();
11642
11643 if (! disptab_matches_widthtab (disptab,
11644 XVECTOR (current_buffer->width_table)))
11645 {
11646 invalidate_region_cache (current_buffer,
11647 current_buffer->width_run_cache,
11648 BEG, Z);
11649 recompute_width_table (current_buffer, disptab);
11650 }
11651 }
11652
11653 /* If window-start is screwed up, choose a new one. */
11654 if (XMARKER (w->start)->buffer != current_buffer)
11655 goto recenter;
11656
11657 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11658
11659 /* If someone specified a new starting point but did not insist,
11660 check whether it can be used. */
11661 if (!NILP (w->optional_new_start)
11662 && CHARPOS (startp) >= BEGV
11663 && CHARPOS (startp) <= ZV)
11664 {
11665 w->optional_new_start = Qnil;
11666 start_display (&it, w, startp);
11667 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11668 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11669 if (IT_CHARPOS (it) == PT)
11670 w->force_start = Qt;
11671 /* IT may overshoot PT if text at PT is invisible. */
11672 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
11673 w->force_start = Qt;
11674
11675
11676 }
11677
11678 /* Handle case where place to start displaying has been specified,
11679 unless the specified location is outside the accessible range. */
11680 if (!NILP (w->force_start)
11681 || w->frozen_window_start_p)
11682 {
11683 /* We set this later on if we have to adjust point. */
11684 int new_vpos = -1;
11685
11686 w->force_start = Qnil;
11687 w->vscroll = 0;
11688 w->window_end_valid = Qnil;
11689
11690 /* Forget any recorded base line for line number display. */
11691 if (!buffer_unchanged_p)
11692 w->base_line_number = Qnil;
11693
11694 /* Redisplay the mode line. Select the buffer properly for that.
11695 Also, run the hook window-scroll-functions
11696 because we have scrolled. */
11697 /* Note, we do this after clearing force_start because
11698 if there's an error, it is better to forget about force_start
11699 than to get into an infinite loop calling the hook functions
11700 and having them get more errors. */
11701 if (!update_mode_line
11702 || ! NILP (Vwindow_scroll_functions))
11703 {
11704 update_mode_line = 1;
11705 w->update_mode_line = Qt;
11706 startp = run_window_scroll_functions (window, startp);
11707 }
11708
11709 w->last_modified = make_number (0);
11710 w->last_overlay_modified = make_number (0);
11711 if (CHARPOS (startp) < BEGV)
11712 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
11713 else if (CHARPOS (startp) > ZV)
11714 SET_TEXT_POS (startp, ZV, ZV_BYTE);
11715
11716 /* Redisplay, then check if cursor has been set during the
11717 redisplay. Give up if new fonts were loaded. */
11718 if (!try_window (window, startp))
11719 {
11720 w->force_start = Qt;
11721 clear_glyph_matrix (w->desired_matrix);
11722 goto need_larger_matrices;
11723 }
11724
11725 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
11726 {
11727 /* If point does not appear, try to move point so it does
11728 appear. The desired matrix has been built above, so we
11729 can use it here. */
11730 new_vpos = window_box_height (w) / 2;
11731 }
11732
11733 if (!make_cursor_line_fully_visible (w, 0))
11734 {
11735 /* Point does appear, but on a line partly visible at end of window.
11736 Move it back to a fully-visible line. */
11737 new_vpos = window_box_height (w);
11738 }
11739
11740 /* If we need to move point for either of the above reasons,
11741 now actually do it. */
11742 if (new_vpos >= 0)
11743 {
11744 struct glyph_row *row;
11745
11746 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
11747 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
11748 ++row;
11749
11750 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
11751 MATRIX_ROW_START_BYTEPOS (row));
11752
11753 if (w != XWINDOW (selected_window))
11754 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
11755 else if (current_buffer == old)
11756 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11757
11758 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
11759
11760 /* If we are highlighting the region, then we just changed
11761 the region, so redisplay to show it. */
11762 if (!NILP (Vtransient_mark_mode)
11763 && !NILP (current_buffer->mark_active))
11764 {
11765 clear_glyph_matrix (w->desired_matrix);
11766 if (!try_window (window, startp))
11767 goto need_larger_matrices;
11768 }
11769 }
11770
11771 #if GLYPH_DEBUG
11772 debug_method_add (w, "forced window start");
11773 #endif
11774 goto done;
11775 }
11776
11777 /* Handle case where text has not changed, only point, and it has
11778 not moved off the frame, and we are not retrying after hscroll.
11779 (current_matrix_up_to_date_p is nonzero when retrying.) */
11780 if (current_matrix_up_to_date_p
11781 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
11782 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
11783 {
11784 switch (rc)
11785 {
11786 case CURSOR_MOVEMENT_SUCCESS:
11787 used_current_matrix_p = 1;
11788 goto done;
11789
11790 #if 0 /* try_cursor_movement never returns this value. */
11791 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
11792 goto need_larger_matrices;
11793 #endif
11794
11795 case CURSOR_MOVEMENT_MUST_SCROLL:
11796 goto try_to_scroll;
11797
11798 default:
11799 abort ();
11800 }
11801 }
11802 /* If current starting point was originally the beginning of a line
11803 but no longer is, find a new starting point. */
11804 else if (!NILP (w->start_at_line_beg)
11805 && !(CHARPOS (startp) <= BEGV
11806 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
11807 {
11808 #if GLYPH_DEBUG
11809 debug_method_add (w, "recenter 1");
11810 #endif
11811 goto recenter;
11812 }
11813
11814 /* Try scrolling with try_window_id. Value is > 0 if update has
11815 been done, it is -1 if we know that the same window start will
11816 not work. It is 0 if unsuccessful for some other reason. */
11817 else if ((tem = try_window_id (w)) != 0)
11818 {
11819 #if GLYPH_DEBUG
11820 debug_method_add (w, "try_window_id %d", tem);
11821 #endif
11822
11823 if (fonts_changed_p)
11824 goto need_larger_matrices;
11825 if (tem > 0)
11826 goto done;
11827
11828 /* Otherwise try_window_id has returned -1 which means that we
11829 don't want the alternative below this comment to execute. */
11830 }
11831 else if (CHARPOS (startp) >= BEGV
11832 && CHARPOS (startp) <= ZV
11833 && PT >= CHARPOS (startp)
11834 && (CHARPOS (startp) < ZV
11835 /* Avoid starting at end of buffer. */
11836 || CHARPOS (startp) == BEGV
11837 || (XFASTINT (w->last_modified) >= MODIFF
11838 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
11839 {
11840 #if GLYPH_DEBUG
11841 debug_method_add (w, "same window start");
11842 #endif
11843
11844 /* Try to redisplay starting at same place as before.
11845 If point has not moved off frame, accept the results. */
11846 if (!current_matrix_up_to_date_p
11847 /* Don't use try_window_reusing_current_matrix in this case
11848 because a window scroll function can have changed the
11849 buffer. */
11850 || !NILP (Vwindow_scroll_functions)
11851 || MINI_WINDOW_P (w)
11852 || !(used_current_matrix_p =
11853 try_window_reusing_current_matrix (w)))
11854 {
11855 IF_DEBUG (debug_method_add (w, "1"));
11856 try_window (window, startp);
11857 }
11858
11859 if (fonts_changed_p)
11860 goto need_larger_matrices;
11861
11862 if (w->cursor.vpos >= 0)
11863 {
11864 if (!just_this_one_p
11865 || current_buffer->clip_changed
11866 || BEG_UNCHANGED < CHARPOS (startp))
11867 /* Forget any recorded base line for line number display. */
11868 w->base_line_number = Qnil;
11869
11870 if (!make_cursor_line_fully_visible (w, 1))
11871 {
11872 clear_glyph_matrix (w->desired_matrix);
11873 last_line_misfit = 1;
11874 }
11875 /* Drop through and scroll. */
11876 else
11877 goto done;
11878 }
11879 else
11880 clear_glyph_matrix (w->desired_matrix);
11881 }
11882
11883 try_to_scroll:
11884
11885 w->last_modified = make_number (0);
11886 w->last_overlay_modified = make_number (0);
11887
11888 /* Redisplay the mode line. Select the buffer properly for that. */
11889 if (!update_mode_line)
11890 {
11891 update_mode_line = 1;
11892 w->update_mode_line = Qt;
11893 }
11894
11895 /* Try to scroll by specified few lines. */
11896 if ((scroll_conservatively
11897 || scroll_step
11898 || temp_scroll_step
11899 || NUMBERP (current_buffer->scroll_up_aggressively)
11900 || NUMBERP (current_buffer->scroll_down_aggressively))
11901 && !current_buffer->clip_changed
11902 && CHARPOS (startp) >= BEGV
11903 && CHARPOS (startp) <= ZV)
11904 {
11905 /* The function returns -1 if new fonts were loaded, 1 if
11906 successful, 0 if not successful. */
11907 int rc = try_scrolling (window, just_this_one_p,
11908 scroll_conservatively,
11909 scroll_step,
11910 temp_scroll_step, last_line_misfit);
11911 switch (rc)
11912 {
11913 case SCROLLING_SUCCESS:
11914 goto done;
11915
11916 case SCROLLING_NEED_LARGER_MATRICES:
11917 goto need_larger_matrices;
11918
11919 case SCROLLING_FAILED:
11920 break;
11921
11922 default:
11923 abort ();
11924 }
11925 }
11926
11927 /* Finally, just choose place to start which centers point */
11928
11929 recenter:
11930 centering_position = window_box_height (w) / 2;
11931
11932 point_at_top:
11933 /* Jump here with centering_position already set to 0. */
11934
11935 #if GLYPH_DEBUG
11936 debug_method_add (w, "recenter");
11937 #endif
11938
11939 /* w->vscroll = 0; */
11940
11941 /* Forget any previously recorded base line for line number display. */
11942 if (!buffer_unchanged_p)
11943 w->base_line_number = Qnil;
11944
11945 /* Move backward half the height of the window. */
11946 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11947 it.current_y = it.last_visible_y;
11948 move_it_vertically_backward (&it, centering_position);
11949 xassert (IT_CHARPOS (it) >= BEGV);
11950
11951 /* The function move_it_vertically_backward may move over more
11952 than the specified y-distance. If it->w is small, e.g. a
11953 mini-buffer window, we may end up in front of the window's
11954 display area. Start displaying at the start of the line
11955 containing PT in this case. */
11956 if (it.current_y <= 0)
11957 {
11958 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11959 move_it_vertically (&it, 0);
11960 xassert (IT_CHARPOS (it) <= PT);
11961 it.current_y = 0;
11962 }
11963
11964 it.current_x = it.hpos = 0;
11965
11966 /* Set startp here explicitly in case that helps avoid an infinite loop
11967 in case the window-scroll-functions functions get errors. */
11968 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
11969
11970 /* Run scroll hooks. */
11971 startp = run_window_scroll_functions (window, it.current.pos);
11972
11973 /* Redisplay the window. */
11974 if (!current_matrix_up_to_date_p
11975 || windows_or_buffers_changed
11976 || cursor_type_changed
11977 /* Don't use try_window_reusing_current_matrix in this case
11978 because it can have changed the buffer. */
11979 || !NILP (Vwindow_scroll_functions)
11980 || !just_this_one_p
11981 || MINI_WINDOW_P (w)
11982 || !(used_current_matrix_p =
11983 try_window_reusing_current_matrix (w)))
11984 try_window (window, startp);
11985
11986 /* If new fonts have been loaded (due to fontsets), give up. We
11987 have to start a new redisplay since we need to re-adjust glyph
11988 matrices. */
11989 if (fonts_changed_p)
11990 goto need_larger_matrices;
11991
11992 /* If cursor did not appear assume that the middle of the window is
11993 in the first line of the window. Do it again with the next line.
11994 (Imagine a window of height 100, displaying two lines of height
11995 60. Moving back 50 from it->last_visible_y will end in the first
11996 line.) */
11997 if (w->cursor.vpos < 0)
11998 {
11999 if (!NILP (w->window_end_valid)
12000 && PT >= Z - XFASTINT (w->window_end_pos))
12001 {
12002 clear_glyph_matrix (w->desired_matrix);
12003 move_it_by_lines (&it, 1, 0);
12004 try_window (window, it.current.pos);
12005 }
12006 else if (PT < IT_CHARPOS (it))
12007 {
12008 clear_glyph_matrix (w->desired_matrix);
12009 move_it_by_lines (&it, -1, 0);
12010 try_window (window, it.current.pos);
12011 }
12012 else
12013 {
12014 /* Not much we can do about it. */
12015 }
12016 }
12017
12018 /* Consider the following case: Window starts at BEGV, there is
12019 invisible, intangible text at BEGV, so that display starts at
12020 some point START > BEGV. It can happen that we are called with
12021 PT somewhere between BEGV and START. Try to handle that case. */
12022 if (w->cursor.vpos < 0)
12023 {
12024 struct glyph_row *row = w->current_matrix->rows;
12025 if (row->mode_line_p)
12026 ++row;
12027 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12028 }
12029
12030 if (!make_cursor_line_fully_visible (w, centering_position > 0))
12031 {
12032 /* If vscroll is enabled, disable it and try again. */
12033 if (w->vscroll)
12034 {
12035 w->vscroll = 0;
12036 clear_glyph_matrix (w->desired_matrix);
12037 goto recenter;
12038 }
12039
12040 /* If centering point failed to make the whole line visible,
12041 put point at the top instead. That has to make the whole line
12042 visible, if it can be done. */
12043 clear_glyph_matrix (w->desired_matrix);
12044 centering_position = 0;
12045 goto point_at_top;
12046 }
12047
12048 done:
12049
12050 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12051 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12052 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12053 ? Qt : Qnil);
12054
12055 /* Display the mode line, if we must. */
12056 if ((update_mode_line
12057 /* If window not full width, must redo its mode line
12058 if (a) the window to its side is being redone and
12059 (b) we do a frame-based redisplay. This is a consequence
12060 of how inverted lines are drawn in frame-based redisplay. */
12061 || (!just_this_one_p
12062 && !FRAME_WINDOW_P (f)
12063 && !WINDOW_FULL_WIDTH_P (w))
12064 /* Line number to display. */
12065 || INTEGERP (w->base_line_pos)
12066 /* Column number is displayed and different from the one displayed. */
12067 || (!NILP (w->column_number_displayed)
12068 && (XFASTINT (w->column_number_displayed)
12069 != (int) current_column ()))) /* iftc */
12070 /* This means that the window has a mode line. */
12071 && (WINDOW_WANTS_MODELINE_P (w)
12072 || WINDOW_WANTS_HEADER_LINE_P (w)))
12073 {
12074 display_mode_lines (w);
12075
12076 /* If mode line height has changed, arrange for a thorough
12077 immediate redisplay using the correct mode line height. */
12078 if (WINDOW_WANTS_MODELINE_P (w)
12079 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12080 {
12081 fonts_changed_p = 1;
12082 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12083 = DESIRED_MODE_LINE_HEIGHT (w);
12084 }
12085
12086 /* If top line height has changed, arrange for a thorough
12087 immediate redisplay using the correct mode line height. */
12088 if (WINDOW_WANTS_HEADER_LINE_P (w)
12089 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12090 {
12091 fonts_changed_p = 1;
12092 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12093 = DESIRED_HEADER_LINE_HEIGHT (w);
12094 }
12095
12096 if (fonts_changed_p)
12097 goto need_larger_matrices;
12098 }
12099
12100 if (!line_number_displayed
12101 && !BUFFERP (w->base_line_pos))
12102 {
12103 w->base_line_pos = Qnil;
12104 w->base_line_number = Qnil;
12105 }
12106
12107 finish_menu_bars:
12108
12109 /* When we reach a frame's selected window, redo the frame's menu bar. */
12110 if (update_mode_line
12111 && EQ (FRAME_SELECTED_WINDOW (f), window))
12112 {
12113 int redisplay_menu_p = 0;
12114 int redisplay_tool_bar_p = 0;
12115
12116 if (FRAME_WINDOW_P (f))
12117 {
12118 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12119 || defined (USE_GTK)
12120 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12121 #else
12122 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12123 #endif
12124 }
12125 else
12126 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12127
12128 if (redisplay_menu_p)
12129 display_menu_bar (w);
12130
12131 #ifdef HAVE_WINDOW_SYSTEM
12132 #ifdef USE_GTK
12133 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12134 #else
12135 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12136 && (FRAME_TOOL_BAR_LINES (f) > 0
12137 || auto_resize_tool_bars_p);
12138
12139 #endif
12140
12141 if (redisplay_tool_bar_p)
12142 redisplay_tool_bar (f);
12143 #endif
12144 }
12145
12146 #ifdef HAVE_WINDOW_SYSTEM
12147 if (update_window_fringes (w, 0)
12148 && !just_this_one_p
12149 && (used_current_matrix_p || overlay_arrow_seen)
12150 && !w->pseudo_window_p)
12151 {
12152 update_begin (f);
12153 BLOCK_INPUT;
12154 draw_window_fringes (w);
12155 UNBLOCK_INPUT;
12156 update_end (f);
12157 }
12158 #endif /* HAVE_WINDOW_SYSTEM */
12159
12160 /* We go to this label, with fonts_changed_p nonzero,
12161 if it is necessary to try again using larger glyph matrices.
12162 We have to redeem the scroll bar even in this case,
12163 because the loop in redisplay_internal expects that. */
12164 need_larger_matrices:
12165 ;
12166 finish_scroll_bars:
12167
12168 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12169 {
12170 /* Set the thumb's position and size. */
12171 set_vertical_scroll_bar (w);
12172
12173 /* Note that we actually used the scroll bar attached to this
12174 window, so it shouldn't be deleted at the end of redisplay. */
12175 if (FRAME_DISPLAY (f)->redeem_scroll_bar_hook)
12176 (*FRAME_DISPLAY (f)->redeem_scroll_bar_hook) (w);
12177 }
12178
12179 /* Restore current_buffer and value of point in it. */
12180 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12181 set_buffer_internal_1 (old);
12182 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12183
12184 unbind_to (count, Qnil);
12185 }
12186
12187
12188 /* Build the complete desired matrix of WINDOW with a window start
12189 buffer position POS. Value is non-zero if successful. It is zero
12190 if fonts were loaded during redisplay which makes re-adjusting
12191 glyph matrices necessary. */
12192
12193 int
12194 try_window (window, pos)
12195 Lisp_Object window;
12196 struct text_pos pos;
12197 {
12198 struct window *w = XWINDOW (window);
12199 struct it it;
12200 struct glyph_row *last_text_row = NULL;
12201
12202 /* Make POS the new window start. */
12203 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12204
12205 /* Mark cursor position as unknown. No overlay arrow seen. */
12206 w->cursor.vpos = -1;
12207 overlay_arrow_seen = 0;
12208
12209 /* Initialize iterator and info to start at POS. */
12210 start_display (&it, w, pos);
12211
12212 /* Display all lines of W. */
12213 while (it.current_y < it.last_visible_y)
12214 {
12215 if (display_line (&it))
12216 last_text_row = it.glyph_row - 1;
12217 if (fonts_changed_p)
12218 return 0;
12219 }
12220
12221 /* If bottom moved off end of frame, change mode line percentage. */
12222 if (XFASTINT (w->window_end_pos) <= 0
12223 && Z != IT_CHARPOS (it))
12224 w->update_mode_line = Qt;
12225
12226 /* Set window_end_pos to the offset of the last character displayed
12227 on the window from the end of current_buffer. Set
12228 window_end_vpos to its row number. */
12229 if (last_text_row)
12230 {
12231 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12232 w->window_end_bytepos
12233 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12234 w->window_end_pos
12235 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12236 w->window_end_vpos
12237 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12238 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12239 ->displays_text_p);
12240 }
12241 else
12242 {
12243 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12244 w->window_end_pos = make_number (Z - ZV);
12245 w->window_end_vpos = make_number (0);
12246 }
12247
12248 /* But that is not valid info until redisplay finishes. */
12249 w->window_end_valid = Qnil;
12250 return 1;
12251 }
12252
12253
12254 \f
12255 /************************************************************************
12256 Window redisplay reusing current matrix when buffer has not changed
12257 ************************************************************************/
12258
12259 /* Try redisplay of window W showing an unchanged buffer with a
12260 different window start than the last time it was displayed by
12261 reusing its current matrix. Value is non-zero if successful.
12262 W->start is the new window start. */
12263
12264 static int
12265 try_window_reusing_current_matrix (w)
12266 struct window *w;
12267 {
12268 struct frame *f = XFRAME (w->frame);
12269 struct glyph_row *row, *bottom_row;
12270 struct it it;
12271 struct run run;
12272 struct text_pos start, new_start;
12273 int nrows_scrolled, i;
12274 struct glyph_row *last_text_row;
12275 struct glyph_row *last_reused_text_row;
12276 struct glyph_row *start_row;
12277 int start_vpos, min_y, max_y;
12278
12279 #if GLYPH_DEBUG
12280 if (inhibit_try_window_reusing)
12281 return 0;
12282 #endif
12283
12284 if (/* This function doesn't handle terminal frames. */
12285 !FRAME_WINDOW_P (f)
12286 /* Don't try to reuse the display if windows have been split
12287 or such. */
12288 || windows_or_buffers_changed
12289 || cursor_type_changed)
12290 return 0;
12291
12292 /* Can't do this if region may have changed. */
12293 if ((!NILP (Vtransient_mark_mode)
12294 && !NILP (current_buffer->mark_active))
12295 || !NILP (w->region_showing)
12296 || !NILP (Vshow_trailing_whitespace))
12297 return 0;
12298
12299 /* If top-line visibility has changed, give up. */
12300 if (WINDOW_WANTS_HEADER_LINE_P (w)
12301 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12302 return 0;
12303
12304 /* Give up if old or new display is scrolled vertically. We could
12305 make this function handle this, but right now it doesn't. */
12306 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12307 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
12308 return 0;
12309
12310 /* The variable new_start now holds the new window start. The old
12311 start `start' can be determined from the current matrix. */
12312 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12313 start = start_row->start.pos;
12314 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12315
12316 /* Clear the desired matrix for the display below. */
12317 clear_glyph_matrix (w->desired_matrix);
12318
12319 if (CHARPOS (new_start) <= CHARPOS (start))
12320 {
12321 int first_row_y;
12322
12323 /* Don't use this method if the display starts with an ellipsis
12324 displayed for invisible text. It's not easy to handle that case
12325 below, and it's certainly not worth the effort since this is
12326 not a frequent case. */
12327 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12328 return 0;
12329
12330 IF_DEBUG (debug_method_add (w, "twu1"));
12331
12332 /* Display up to a row that can be reused. The variable
12333 last_text_row is set to the last row displayed that displays
12334 text. Note that it.vpos == 0 if or if not there is a
12335 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12336 start_display (&it, w, new_start);
12337 first_row_y = it.current_y;
12338 w->cursor.vpos = -1;
12339 last_text_row = last_reused_text_row = NULL;
12340
12341 while (it.current_y < it.last_visible_y
12342 && IT_CHARPOS (it) < CHARPOS (start)
12343 && !fonts_changed_p)
12344 if (display_line (&it))
12345 last_text_row = it.glyph_row - 1;
12346
12347 /* A value of current_y < last_visible_y means that we stopped
12348 at the previous window start, which in turn means that we
12349 have at least one reusable row. */
12350 if (it.current_y < it.last_visible_y)
12351 {
12352 /* IT.vpos always starts from 0; it counts text lines. */
12353 nrows_scrolled = it.vpos;
12354
12355 /* Find PT if not already found in the lines displayed. */
12356 if (w->cursor.vpos < 0)
12357 {
12358 int dy = it.current_y - first_row_y;
12359
12360 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12361 row = row_containing_pos (w, PT, row, NULL, dy);
12362 if (row)
12363 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12364 dy, nrows_scrolled);
12365 else
12366 {
12367 clear_glyph_matrix (w->desired_matrix);
12368 return 0;
12369 }
12370 }
12371
12372 /* Scroll the display. Do it before the current matrix is
12373 changed. The problem here is that update has not yet
12374 run, i.e. part of the current matrix is not up to date.
12375 scroll_run_hook will clear the cursor, and use the
12376 current matrix to get the height of the row the cursor is
12377 in. */
12378 run.current_y = first_row_y;
12379 run.desired_y = it.current_y;
12380 run.height = it.last_visible_y - it.current_y;
12381
12382 if (run.height > 0 && run.current_y != run.desired_y)
12383 {
12384 update_begin (f);
12385 FRAME_RIF (f)->update_window_begin_hook (w);
12386 FRAME_RIF (f)->clear_window_mouse_face (w);
12387 FRAME_RIF (f)->scroll_run_hook (w, &run);
12388 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
12389 update_end (f);
12390 }
12391
12392 /* Shift current matrix down by nrows_scrolled lines. */
12393 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12394 rotate_matrix (w->current_matrix,
12395 start_vpos,
12396 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12397 nrows_scrolled);
12398
12399 /* Disable lines that must be updated. */
12400 for (i = 0; i < it.vpos; ++i)
12401 (start_row + i)->enabled_p = 0;
12402
12403 /* Re-compute Y positions. */
12404 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12405 max_y = it.last_visible_y;
12406 for (row = start_row + nrows_scrolled;
12407 row < bottom_row;
12408 ++row)
12409 {
12410 row->y = it.current_y;
12411 row->visible_height = row->height;
12412
12413 if (row->y < min_y)
12414 row->visible_height -= min_y - row->y;
12415 if (row->y + row->height > max_y)
12416 row->visible_height -= row->y + row->height - max_y;
12417 row->redraw_fringe_bitmaps_p = 1;
12418
12419 it.current_y += row->height;
12420
12421 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12422 last_reused_text_row = row;
12423 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12424 break;
12425 }
12426
12427 /* Disable lines in the current matrix which are now
12428 below the window. */
12429 for (++row; row < bottom_row; ++row)
12430 row->enabled_p = 0;
12431 }
12432
12433 /* Update window_end_pos etc.; last_reused_text_row is the last
12434 reused row from the current matrix containing text, if any.
12435 The value of last_text_row is the last displayed line
12436 containing text. */
12437 if (last_reused_text_row)
12438 {
12439 w->window_end_bytepos
12440 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12441 w->window_end_pos
12442 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12443 w->window_end_vpos
12444 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12445 w->current_matrix));
12446 }
12447 else if (last_text_row)
12448 {
12449 w->window_end_bytepos
12450 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12451 w->window_end_pos
12452 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12453 w->window_end_vpos
12454 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12455 }
12456 else
12457 {
12458 /* This window must be completely empty. */
12459 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12460 w->window_end_pos = make_number (Z - ZV);
12461 w->window_end_vpos = make_number (0);
12462 }
12463 w->window_end_valid = Qnil;
12464
12465 /* Update hint: don't try scrolling again in update_window. */
12466 w->desired_matrix->no_scrolling_p = 1;
12467
12468 #if GLYPH_DEBUG
12469 debug_method_add (w, "try_window_reusing_current_matrix 1");
12470 #endif
12471 return 1;
12472 }
12473 else if (CHARPOS (new_start) > CHARPOS (start))
12474 {
12475 struct glyph_row *pt_row, *row;
12476 struct glyph_row *first_reusable_row;
12477 struct glyph_row *first_row_to_display;
12478 int dy;
12479 int yb = window_text_bottom_y (w);
12480
12481 /* Find the row starting at new_start, if there is one. Don't
12482 reuse a partially visible line at the end. */
12483 first_reusable_row = start_row;
12484 while (first_reusable_row->enabled_p
12485 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12486 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12487 < CHARPOS (new_start)))
12488 ++first_reusable_row;
12489
12490 /* Give up if there is no row to reuse. */
12491 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
12492 || !first_reusable_row->enabled_p
12493 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12494 != CHARPOS (new_start)))
12495 return 0;
12496
12497 /* We can reuse fully visible rows beginning with
12498 first_reusable_row to the end of the window. Set
12499 first_row_to_display to the first row that cannot be reused.
12500 Set pt_row to the row containing point, if there is any. */
12501 pt_row = NULL;
12502 for (first_row_to_display = first_reusable_row;
12503 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12504 ++first_row_to_display)
12505 {
12506 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12507 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
12508 pt_row = first_row_to_display;
12509 }
12510
12511 /* Start displaying at the start of first_row_to_display. */
12512 xassert (first_row_to_display->y < yb);
12513 init_to_row_start (&it, w, first_row_to_display);
12514
12515 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12516 - start_vpos);
12517 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12518 - nrows_scrolled);
12519 it.current_y = (first_row_to_display->y - first_reusable_row->y
12520 + WINDOW_HEADER_LINE_HEIGHT (w));
12521
12522 /* Display lines beginning with first_row_to_display in the
12523 desired matrix. Set last_text_row to the last row displayed
12524 that displays text. */
12525 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12526 if (pt_row == NULL)
12527 w->cursor.vpos = -1;
12528 last_text_row = NULL;
12529 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12530 if (display_line (&it))
12531 last_text_row = it.glyph_row - 1;
12532
12533 /* Give up If point isn't in a row displayed or reused. */
12534 if (w->cursor.vpos < 0)
12535 {
12536 clear_glyph_matrix (w->desired_matrix);
12537 return 0;
12538 }
12539
12540 /* If point is in a reused row, adjust y and vpos of the cursor
12541 position. */
12542 if (pt_row)
12543 {
12544 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
12545 w->current_matrix);
12546 w->cursor.y -= first_reusable_row->y;
12547 }
12548
12549 /* Scroll the display. */
12550 run.current_y = first_reusable_row->y;
12551 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
12552 run.height = it.last_visible_y - run.current_y;
12553 dy = run.current_y - run.desired_y;
12554
12555 if (run.height)
12556 {
12557 update_begin (f);
12558 FRAME_RIF (f)->update_window_begin_hook (w);
12559 FRAME_RIF (f)->clear_window_mouse_face (w);
12560 FRAME_RIF (f)->scroll_run_hook (w, &run);
12561 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
12562 update_end (f);
12563 }
12564
12565 /* Adjust Y positions of reused rows. */
12566 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12567 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12568 max_y = it.last_visible_y;
12569 for (row = first_reusable_row; row < first_row_to_display; ++row)
12570 {
12571 row->y -= dy;
12572 row->visible_height = row->height;
12573 if (row->y < min_y)
12574 row->visible_height -= min_y - row->y;
12575 if (row->y + row->height > max_y)
12576 row->visible_height -= row->y + row->height - max_y;
12577 row->redraw_fringe_bitmaps_p = 1;
12578 }
12579
12580 /* Scroll the current matrix. */
12581 xassert (nrows_scrolled > 0);
12582 rotate_matrix (w->current_matrix,
12583 start_vpos,
12584 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12585 -nrows_scrolled);
12586
12587 /* Disable rows not reused. */
12588 for (row -= nrows_scrolled; row < bottom_row; ++row)
12589 row->enabled_p = 0;
12590
12591 /* Adjust window end. A null value of last_text_row means that
12592 the window end is in reused rows which in turn means that
12593 only its vpos can have changed. */
12594 if (last_text_row)
12595 {
12596 w->window_end_bytepos
12597 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12598 w->window_end_pos
12599 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12600 w->window_end_vpos
12601 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12602 }
12603 else
12604 {
12605 w->window_end_vpos
12606 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
12607 }
12608
12609 w->window_end_valid = Qnil;
12610 w->desired_matrix->no_scrolling_p = 1;
12611
12612 #if GLYPH_DEBUG
12613 debug_method_add (w, "try_window_reusing_current_matrix 2");
12614 #endif
12615 return 1;
12616 }
12617
12618 return 0;
12619 }
12620
12621
12622 \f
12623 /************************************************************************
12624 Window redisplay reusing current matrix when buffer has changed
12625 ************************************************************************/
12626
12627 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
12628 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
12629 int *, int *));
12630 static struct glyph_row *
12631 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
12632 struct glyph_row *));
12633
12634
12635 /* Return the last row in MATRIX displaying text. If row START is
12636 non-null, start searching with that row. IT gives the dimensions
12637 of the display. Value is null if matrix is empty; otherwise it is
12638 a pointer to the row found. */
12639
12640 static struct glyph_row *
12641 find_last_row_displaying_text (matrix, it, start)
12642 struct glyph_matrix *matrix;
12643 struct it *it;
12644 struct glyph_row *start;
12645 {
12646 struct glyph_row *row, *row_found;
12647
12648 /* Set row_found to the last row in IT->w's current matrix
12649 displaying text. The loop looks funny but think of partially
12650 visible lines. */
12651 row_found = NULL;
12652 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
12653 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12654 {
12655 xassert (row->enabled_p);
12656 row_found = row;
12657 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
12658 break;
12659 ++row;
12660 }
12661
12662 return row_found;
12663 }
12664
12665
12666 /* Return the last row in the current matrix of W that is not affected
12667 by changes at the start of current_buffer that occurred since W's
12668 current matrix was built. Value is null if no such row exists.
12669
12670 BEG_UNCHANGED us the number of characters unchanged at the start of
12671 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
12672 first changed character in current_buffer. Characters at positions <
12673 BEG + BEG_UNCHANGED are at the same buffer positions as they were
12674 when the current matrix was built. */
12675
12676 static struct glyph_row *
12677 find_last_unchanged_at_beg_row (w)
12678 struct window *w;
12679 {
12680 int first_changed_pos = BEG + BEG_UNCHANGED;
12681 struct glyph_row *row;
12682 struct glyph_row *row_found = NULL;
12683 int yb = window_text_bottom_y (w);
12684
12685 /* Find the last row displaying unchanged text. */
12686 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12687 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12688 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
12689 {
12690 if (/* If row ends before first_changed_pos, it is unchanged,
12691 except in some case. */
12692 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
12693 /* When row ends in ZV and we write at ZV it is not
12694 unchanged. */
12695 && !row->ends_at_zv_p
12696 /* When first_changed_pos is the end of a continued line,
12697 row is not unchanged because it may be no longer
12698 continued. */
12699 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
12700 && (row->continued_p
12701 || row->exact_window_width_line_p)))
12702 row_found = row;
12703
12704 /* Stop if last visible row. */
12705 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
12706 break;
12707
12708 ++row;
12709 }
12710
12711 return row_found;
12712 }
12713
12714
12715 /* Find the first glyph row in the current matrix of W that is not
12716 affected by changes at the end of current_buffer since the
12717 time W's current matrix was built.
12718
12719 Return in *DELTA the number of chars by which buffer positions in
12720 unchanged text at the end of current_buffer must be adjusted.
12721
12722 Return in *DELTA_BYTES the corresponding number of bytes.
12723
12724 Value is null if no such row exists, i.e. all rows are affected by
12725 changes. */
12726
12727 static struct glyph_row *
12728 find_first_unchanged_at_end_row (w, delta, delta_bytes)
12729 struct window *w;
12730 int *delta, *delta_bytes;
12731 {
12732 struct glyph_row *row;
12733 struct glyph_row *row_found = NULL;
12734
12735 *delta = *delta_bytes = 0;
12736
12737 /* Display must not have been paused, otherwise the current matrix
12738 is not up to date. */
12739 if (NILP (w->window_end_valid))
12740 abort ();
12741
12742 /* A value of window_end_pos >= END_UNCHANGED means that the window
12743 end is in the range of changed text. If so, there is no
12744 unchanged row at the end of W's current matrix. */
12745 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
12746 return NULL;
12747
12748 /* Set row to the last row in W's current matrix displaying text. */
12749 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12750
12751 /* If matrix is entirely empty, no unchanged row exists. */
12752 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12753 {
12754 /* The value of row is the last glyph row in the matrix having a
12755 meaningful buffer position in it. The end position of row
12756 corresponds to window_end_pos. This allows us to translate
12757 buffer positions in the current matrix to current buffer
12758 positions for characters not in changed text. */
12759 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12760 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12761 int last_unchanged_pos, last_unchanged_pos_old;
12762 struct glyph_row *first_text_row
12763 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12764
12765 *delta = Z - Z_old;
12766 *delta_bytes = Z_BYTE - Z_BYTE_old;
12767
12768 /* Set last_unchanged_pos to the buffer position of the last
12769 character in the buffer that has not been changed. Z is the
12770 index + 1 of the last character in current_buffer, i.e. by
12771 subtracting END_UNCHANGED we get the index of the last
12772 unchanged character, and we have to add BEG to get its buffer
12773 position. */
12774 last_unchanged_pos = Z - END_UNCHANGED + BEG;
12775 last_unchanged_pos_old = last_unchanged_pos - *delta;
12776
12777 /* Search backward from ROW for a row displaying a line that
12778 starts at a minimum position >= last_unchanged_pos_old. */
12779 for (; row > first_text_row; --row)
12780 {
12781 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
12782 abort ();
12783
12784 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
12785 row_found = row;
12786 }
12787 }
12788
12789 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
12790 abort ();
12791
12792 return row_found;
12793 }
12794
12795
12796 /* Make sure that glyph rows in the current matrix of window W
12797 reference the same glyph memory as corresponding rows in the
12798 frame's frame matrix. This function is called after scrolling W's
12799 current matrix on a terminal frame in try_window_id and
12800 try_window_reusing_current_matrix. */
12801
12802 static void
12803 sync_frame_with_window_matrix_rows (w)
12804 struct window *w;
12805 {
12806 struct frame *f = XFRAME (w->frame);
12807 struct glyph_row *window_row, *window_row_end, *frame_row;
12808
12809 /* Preconditions: W must be a leaf window and full-width. Its frame
12810 must have a frame matrix. */
12811 xassert (NILP (w->hchild) && NILP (w->vchild));
12812 xassert (WINDOW_FULL_WIDTH_P (w));
12813 xassert (!FRAME_WINDOW_P (f));
12814
12815 /* If W is a full-width window, glyph pointers in W's current matrix
12816 have, by definition, to be the same as glyph pointers in the
12817 corresponding frame matrix. Note that frame matrices have no
12818 marginal areas (see build_frame_matrix). */
12819 window_row = w->current_matrix->rows;
12820 window_row_end = window_row + w->current_matrix->nrows;
12821 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
12822 while (window_row < window_row_end)
12823 {
12824 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
12825 struct glyph *end = window_row->glyphs[LAST_AREA];
12826
12827 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
12828 frame_row->glyphs[TEXT_AREA] = start;
12829 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
12830 frame_row->glyphs[LAST_AREA] = end;
12831
12832 /* Disable frame rows whose corresponding window rows have
12833 been disabled in try_window_id. */
12834 if (!window_row->enabled_p)
12835 frame_row->enabled_p = 0;
12836
12837 ++window_row, ++frame_row;
12838 }
12839 }
12840
12841
12842 /* Find the glyph row in window W containing CHARPOS. Consider all
12843 rows between START and END (not inclusive). END null means search
12844 all rows to the end of the display area of W. Value is the row
12845 containing CHARPOS or null. */
12846
12847 struct glyph_row *
12848 row_containing_pos (w, charpos, start, end, dy)
12849 struct window *w;
12850 int charpos;
12851 struct glyph_row *start, *end;
12852 int dy;
12853 {
12854 struct glyph_row *row = start;
12855 int last_y;
12856
12857 /* If we happen to start on a header-line, skip that. */
12858 if (row->mode_line_p)
12859 ++row;
12860
12861 if ((end && row >= end) || !row->enabled_p)
12862 return NULL;
12863
12864 last_y = window_text_bottom_y (w) - dy;
12865
12866 while (1)
12867 {
12868 /* Give up if we have gone too far. */
12869 if (end && row >= end)
12870 return NULL;
12871 /* This formerly returned if they were equal.
12872 I think that both quantities are of a "last plus one" type;
12873 if so, when they are equal, the row is within the screen. -- rms. */
12874 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
12875 return NULL;
12876
12877 /* If it is in this row, return this row. */
12878 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
12879 || (MATRIX_ROW_END_CHARPOS (row) == charpos
12880 /* The end position of a row equals the start
12881 position of the next row. If CHARPOS is there, we
12882 would rather display it in the next line, except
12883 when this line ends in ZV. */
12884 && !row->ends_at_zv_p
12885 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12886 && charpos >= MATRIX_ROW_START_CHARPOS (row))
12887 return row;
12888 ++row;
12889 }
12890 }
12891
12892
12893 /* Try to redisplay window W by reusing its existing display. W's
12894 current matrix must be up to date when this function is called,
12895 i.e. window_end_valid must not be nil.
12896
12897 Value is
12898
12899 1 if display has been updated
12900 0 if otherwise unsuccessful
12901 -1 if redisplay with same window start is known not to succeed
12902
12903 The following steps are performed:
12904
12905 1. Find the last row in the current matrix of W that is not
12906 affected by changes at the start of current_buffer. If no such row
12907 is found, give up.
12908
12909 2. Find the first row in W's current matrix that is not affected by
12910 changes at the end of current_buffer. Maybe there is no such row.
12911
12912 3. Display lines beginning with the row + 1 found in step 1 to the
12913 row found in step 2 or, if step 2 didn't find a row, to the end of
12914 the window.
12915
12916 4. If cursor is not known to appear on the window, give up.
12917
12918 5. If display stopped at the row found in step 2, scroll the
12919 display and current matrix as needed.
12920
12921 6. Maybe display some lines at the end of W, if we must. This can
12922 happen under various circumstances, like a partially visible line
12923 becoming fully visible, or because newly displayed lines are displayed
12924 in smaller font sizes.
12925
12926 7. Update W's window end information. */
12927
12928 static int
12929 try_window_id (w)
12930 struct window *w;
12931 {
12932 struct frame *f = XFRAME (w->frame);
12933 struct glyph_matrix *current_matrix = w->current_matrix;
12934 struct glyph_matrix *desired_matrix = w->desired_matrix;
12935 struct glyph_row *last_unchanged_at_beg_row;
12936 struct glyph_row *first_unchanged_at_end_row;
12937 struct glyph_row *row;
12938 struct glyph_row *bottom_row;
12939 int bottom_vpos;
12940 struct it it;
12941 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
12942 struct text_pos start_pos;
12943 struct run run;
12944 int first_unchanged_at_end_vpos = 0;
12945 struct glyph_row *last_text_row, *last_text_row_at_end;
12946 struct text_pos start;
12947 int first_changed_charpos, last_changed_charpos;
12948
12949 #if GLYPH_DEBUG
12950 if (inhibit_try_window_id)
12951 return 0;
12952 #endif
12953
12954 /* This is handy for debugging. */
12955 #if 0
12956 #define GIVE_UP(X) \
12957 do { \
12958 fprintf (stderr, "try_window_id give up %d\n", (X)); \
12959 return 0; \
12960 } while (0)
12961 #else
12962 #define GIVE_UP(X) return 0
12963 #endif
12964
12965 SET_TEXT_POS_FROM_MARKER (start, w->start);
12966
12967 /* Don't use this for mini-windows because these can show
12968 messages and mini-buffers, and we don't handle that here. */
12969 if (MINI_WINDOW_P (w))
12970 GIVE_UP (1);
12971
12972 /* This flag is used to prevent redisplay optimizations. */
12973 if (windows_or_buffers_changed || cursor_type_changed)
12974 GIVE_UP (2);
12975
12976 /* Verify that narrowing has not changed.
12977 Also verify that we were not told to prevent redisplay optimizations.
12978 It would be nice to further
12979 reduce the number of cases where this prevents try_window_id. */
12980 if (current_buffer->clip_changed
12981 || current_buffer->prevent_redisplay_optimizations_p)
12982 GIVE_UP (3);
12983
12984 /* Window must either use window-based redisplay or be full width. */
12985 if (!FRAME_WINDOW_P (f)
12986 && (!FRAME_LINE_INS_DEL_OK (f)
12987 || !WINDOW_FULL_WIDTH_P (w)))
12988 GIVE_UP (4);
12989
12990 /* Give up if point is not known NOT to appear in W. */
12991 if (PT < CHARPOS (start))
12992 GIVE_UP (5);
12993
12994 /* Another way to prevent redisplay optimizations. */
12995 if (XFASTINT (w->last_modified) == 0)
12996 GIVE_UP (6);
12997
12998 /* Verify that window is not hscrolled. */
12999 if (XFASTINT (w->hscroll) != 0)
13000 GIVE_UP (7);
13001
13002 /* Verify that display wasn't paused. */
13003 if (NILP (w->window_end_valid))
13004 GIVE_UP (8);
13005
13006 /* Can't use this if highlighting a region because a cursor movement
13007 will do more than just set the cursor. */
13008 if (!NILP (Vtransient_mark_mode)
13009 && !NILP (current_buffer->mark_active))
13010 GIVE_UP (9);
13011
13012 /* Likewise if highlighting trailing whitespace. */
13013 if (!NILP (Vshow_trailing_whitespace))
13014 GIVE_UP (11);
13015
13016 /* Likewise if showing a region. */
13017 if (!NILP (w->region_showing))
13018 GIVE_UP (10);
13019
13020 /* Can use this if overlay arrow position and or string have changed. */
13021 if (overlay_arrows_changed_p ())
13022 GIVE_UP (12);
13023
13024
13025 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13026 only if buffer has really changed. The reason is that the gap is
13027 initially at Z for freshly visited files. The code below would
13028 set end_unchanged to 0 in that case. */
13029 if (MODIFF > SAVE_MODIFF
13030 /* This seems to happen sometimes after saving a buffer. */
13031 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13032 {
13033 if (GPT - BEG < BEG_UNCHANGED)
13034 BEG_UNCHANGED = GPT - BEG;
13035 if (Z - GPT < END_UNCHANGED)
13036 END_UNCHANGED = Z - GPT;
13037 }
13038
13039 /* The position of the first and last character that has been changed. */
13040 first_changed_charpos = BEG + BEG_UNCHANGED;
13041 last_changed_charpos = Z - END_UNCHANGED;
13042
13043 /* If window starts after a line end, and the last change is in
13044 front of that newline, then changes don't affect the display.
13045 This case happens with stealth-fontification. Note that although
13046 the display is unchanged, glyph positions in the matrix have to
13047 be adjusted, of course. */
13048 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13049 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13050 && ((last_changed_charpos < CHARPOS (start)
13051 && CHARPOS (start) == BEGV)
13052 || (last_changed_charpos < CHARPOS (start) - 1
13053 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13054 {
13055 int Z_old, delta, Z_BYTE_old, delta_bytes;
13056 struct glyph_row *r0;
13057
13058 /* Compute how many chars/bytes have been added to or removed
13059 from the buffer. */
13060 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13061 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13062 delta = Z - Z_old;
13063 delta_bytes = Z_BYTE - Z_BYTE_old;
13064
13065 /* Give up if PT is not in the window. Note that it already has
13066 been checked at the start of try_window_id that PT is not in
13067 front of the window start. */
13068 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13069 GIVE_UP (13);
13070
13071 /* If window start is unchanged, we can reuse the whole matrix
13072 as is, after adjusting glyph positions. No need to compute
13073 the window end again, since its offset from Z hasn't changed. */
13074 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13075 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13076 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13077 /* PT must not be in a partially visible line. */
13078 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13079 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13080 {
13081 /* Adjust positions in the glyph matrix. */
13082 if (delta || delta_bytes)
13083 {
13084 struct glyph_row *r1
13085 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13086 increment_matrix_positions (w->current_matrix,
13087 MATRIX_ROW_VPOS (r0, current_matrix),
13088 MATRIX_ROW_VPOS (r1, current_matrix),
13089 delta, delta_bytes);
13090 }
13091
13092 /* Set the cursor. */
13093 row = row_containing_pos (w, PT, r0, NULL, 0);
13094 if (row)
13095 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13096 else
13097 abort ();
13098 return 1;
13099 }
13100 }
13101
13102 /* Handle the case that changes are all below what is displayed in
13103 the window, and that PT is in the window. This shortcut cannot
13104 be taken if ZV is visible in the window, and text has been added
13105 there that is visible in the window. */
13106 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13107 /* ZV is not visible in the window, or there are no
13108 changes at ZV, actually. */
13109 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13110 || first_changed_charpos == last_changed_charpos))
13111 {
13112 struct glyph_row *r0;
13113
13114 /* Give up if PT is not in the window. Note that it already has
13115 been checked at the start of try_window_id that PT is not in
13116 front of the window start. */
13117 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13118 GIVE_UP (14);
13119
13120 /* If window start is unchanged, we can reuse the whole matrix
13121 as is, without changing glyph positions since no text has
13122 been added/removed in front of the window end. */
13123 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13124 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13125 /* PT must not be in a partially visible line. */
13126 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13127 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13128 {
13129 /* We have to compute the window end anew since text
13130 can have been added/removed after it. */
13131 w->window_end_pos
13132 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13133 w->window_end_bytepos
13134 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13135
13136 /* Set the cursor. */
13137 row = row_containing_pos (w, PT, r0, NULL, 0);
13138 if (row)
13139 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13140 else
13141 abort ();
13142 return 2;
13143 }
13144 }
13145
13146 /* Give up if window start is in the changed area.
13147
13148 The condition used to read
13149
13150 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13151
13152 but why that was tested escapes me at the moment. */
13153 if (CHARPOS (start) >= first_changed_charpos
13154 && CHARPOS (start) <= last_changed_charpos)
13155 GIVE_UP (15);
13156
13157 /* Check that window start agrees with the start of the first glyph
13158 row in its current matrix. Check this after we know the window
13159 start is not in changed text, otherwise positions would not be
13160 comparable. */
13161 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13162 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13163 GIVE_UP (16);
13164
13165 /* Give up if the window ends in strings. Overlay strings
13166 at the end are difficult to handle, so don't try. */
13167 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13168 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13169 GIVE_UP (20);
13170
13171 /* Compute the position at which we have to start displaying new
13172 lines. Some of the lines at the top of the window might be
13173 reusable because they are not displaying changed text. Find the
13174 last row in W's current matrix not affected by changes at the
13175 start of current_buffer. Value is null if changes start in the
13176 first line of window. */
13177 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13178 if (last_unchanged_at_beg_row)
13179 {
13180 /* Avoid starting to display in the moddle of a character, a TAB
13181 for instance. This is easier than to set up the iterator
13182 exactly, and it's not a frequent case, so the additional
13183 effort wouldn't really pay off. */
13184 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13185 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13186 && last_unchanged_at_beg_row > w->current_matrix->rows)
13187 --last_unchanged_at_beg_row;
13188
13189 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13190 GIVE_UP (17);
13191
13192 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13193 GIVE_UP (18);
13194 start_pos = it.current.pos;
13195
13196 /* Start displaying new lines in the desired matrix at the same
13197 vpos we would use in the current matrix, i.e. below
13198 last_unchanged_at_beg_row. */
13199 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13200 current_matrix);
13201 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13202 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13203
13204 xassert (it.hpos == 0 && it.current_x == 0);
13205 }
13206 else
13207 {
13208 /* There are no reusable lines at the start of the window.
13209 Start displaying in the first text line. */
13210 start_display (&it, w, start);
13211 it.vpos = it.first_vpos;
13212 start_pos = it.current.pos;
13213 }
13214
13215 /* Find the first row that is not affected by changes at the end of
13216 the buffer. Value will be null if there is no unchanged row, in
13217 which case we must redisplay to the end of the window. delta
13218 will be set to the value by which buffer positions beginning with
13219 first_unchanged_at_end_row have to be adjusted due to text
13220 changes. */
13221 first_unchanged_at_end_row
13222 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13223 IF_DEBUG (debug_delta = delta);
13224 IF_DEBUG (debug_delta_bytes = delta_bytes);
13225
13226 /* Set stop_pos to the buffer position up to which we will have to
13227 display new lines. If first_unchanged_at_end_row != NULL, this
13228 is the buffer position of the start of the line displayed in that
13229 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13230 that we don't stop at a buffer position. */
13231 stop_pos = 0;
13232 if (first_unchanged_at_end_row)
13233 {
13234 xassert (last_unchanged_at_beg_row == NULL
13235 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13236
13237 /* If this is a continuation line, move forward to the next one
13238 that isn't. Changes in lines above affect this line.
13239 Caution: this may move first_unchanged_at_end_row to a row
13240 not displaying text. */
13241 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13242 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13243 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13244 < it.last_visible_y))
13245 ++first_unchanged_at_end_row;
13246
13247 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13248 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13249 >= it.last_visible_y))
13250 first_unchanged_at_end_row = NULL;
13251 else
13252 {
13253 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13254 + delta);
13255 first_unchanged_at_end_vpos
13256 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13257 xassert (stop_pos >= Z - END_UNCHANGED);
13258 }
13259 }
13260 else if (last_unchanged_at_beg_row == NULL)
13261 GIVE_UP (19);
13262
13263
13264 #if GLYPH_DEBUG
13265
13266 /* Either there is no unchanged row at the end, or the one we have
13267 now displays text. This is a necessary condition for the window
13268 end pos calculation at the end of this function. */
13269 xassert (first_unchanged_at_end_row == NULL
13270 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13271
13272 debug_last_unchanged_at_beg_vpos
13273 = (last_unchanged_at_beg_row
13274 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13275 : -1);
13276 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13277
13278 #endif /* GLYPH_DEBUG != 0 */
13279
13280
13281 /* Display new lines. Set last_text_row to the last new line
13282 displayed which has text on it, i.e. might end up as being the
13283 line where the window_end_vpos is. */
13284 w->cursor.vpos = -1;
13285 last_text_row = NULL;
13286 overlay_arrow_seen = 0;
13287 while (it.current_y < it.last_visible_y
13288 && !fonts_changed_p
13289 && (first_unchanged_at_end_row == NULL
13290 || IT_CHARPOS (it) < stop_pos))
13291 {
13292 if (display_line (&it))
13293 last_text_row = it.glyph_row - 1;
13294 }
13295
13296 if (fonts_changed_p)
13297 return -1;
13298
13299
13300 /* Compute differences in buffer positions, y-positions etc. for
13301 lines reused at the bottom of the window. Compute what we can
13302 scroll. */
13303 if (first_unchanged_at_end_row
13304 /* No lines reused because we displayed everything up to the
13305 bottom of the window. */
13306 && it.current_y < it.last_visible_y)
13307 {
13308 dvpos = (it.vpos
13309 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13310 current_matrix));
13311 dy = it.current_y - first_unchanged_at_end_row->y;
13312 run.current_y = first_unchanged_at_end_row->y;
13313 run.desired_y = run.current_y + dy;
13314 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13315 }
13316 else
13317 {
13318 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13319 first_unchanged_at_end_row = NULL;
13320 }
13321 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13322
13323
13324 /* Find the cursor if not already found. We have to decide whether
13325 PT will appear on this window (it sometimes doesn't, but this is
13326 not a very frequent case.) This decision has to be made before
13327 the current matrix is altered. A value of cursor.vpos < 0 means
13328 that PT is either in one of the lines beginning at
13329 first_unchanged_at_end_row or below the window. Don't care for
13330 lines that might be displayed later at the window end; as
13331 mentioned, this is not a frequent case. */
13332 if (w->cursor.vpos < 0)
13333 {
13334 /* Cursor in unchanged rows at the top? */
13335 if (PT < CHARPOS (start_pos)
13336 && last_unchanged_at_beg_row)
13337 {
13338 row = row_containing_pos (w, PT,
13339 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13340 last_unchanged_at_beg_row + 1, 0);
13341 if (row)
13342 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13343 }
13344
13345 /* Start from first_unchanged_at_end_row looking for PT. */
13346 else if (first_unchanged_at_end_row)
13347 {
13348 row = row_containing_pos (w, PT - delta,
13349 first_unchanged_at_end_row, NULL, 0);
13350 if (row)
13351 set_cursor_from_row (w, row, w->current_matrix, delta,
13352 delta_bytes, dy, dvpos);
13353 }
13354
13355 /* Give up if cursor was not found. */
13356 if (w->cursor.vpos < 0)
13357 {
13358 clear_glyph_matrix (w->desired_matrix);
13359 return -1;
13360 }
13361 }
13362
13363 /* Don't let the cursor end in the scroll margins. */
13364 {
13365 int this_scroll_margin, cursor_height;
13366
13367 this_scroll_margin = max (0, scroll_margin);
13368 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13369 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13370 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13371
13372 if ((w->cursor.y < this_scroll_margin
13373 && CHARPOS (start) > BEGV)
13374 /* Don't take scroll margin into account at the bottom because
13375 old redisplay didn't do it either. */
13376 || w->cursor.y + cursor_height > it.last_visible_y)
13377 {
13378 w->cursor.vpos = -1;
13379 clear_glyph_matrix (w->desired_matrix);
13380 return -1;
13381 }
13382 }
13383
13384 /* Scroll the display. Do it before changing the current matrix so
13385 that xterm.c doesn't get confused about where the cursor glyph is
13386 found. */
13387 if (dy && run.height)
13388 {
13389 update_begin (f);
13390
13391 if (FRAME_WINDOW_P (f))
13392 {
13393 FRAME_RIF (f)->update_window_begin_hook (w);
13394 FRAME_RIF (f)->clear_window_mouse_face (w);
13395 FRAME_RIF (f)->scroll_run_hook (w, &run);
13396 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
13397 }
13398 else
13399 {
13400 /* Terminal frame. In this case, dvpos gives the number of
13401 lines to scroll by; dvpos < 0 means scroll up. */
13402 int first_unchanged_at_end_vpos
13403 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13404 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13405 int end = (WINDOW_TOP_EDGE_LINE (w)
13406 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13407 + window_internal_height (w));
13408
13409 /* Perform the operation on the screen. */
13410 if (dvpos > 0)
13411 {
13412 /* Scroll last_unchanged_at_beg_row to the end of the
13413 window down dvpos lines. */
13414 set_terminal_window (f, end);
13415
13416 /* On dumb terminals delete dvpos lines at the end
13417 before inserting dvpos empty lines. */
13418 if (!FRAME_SCROLL_REGION_OK (f))
13419 ins_del_lines (f, end - dvpos, -dvpos);
13420
13421 /* Insert dvpos empty lines in front of
13422 last_unchanged_at_beg_row. */
13423 ins_del_lines (f, from, dvpos);
13424 }
13425 else if (dvpos < 0)
13426 {
13427 /* Scroll up last_unchanged_at_beg_vpos to the end of
13428 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13429 set_terminal_window (f, end);
13430
13431 /* Delete dvpos lines in front of
13432 last_unchanged_at_beg_vpos. ins_del_lines will set
13433 the cursor to the given vpos and emit |dvpos| delete
13434 line sequences. */
13435 ins_del_lines (f, from + dvpos, dvpos);
13436
13437 /* On a dumb terminal insert dvpos empty lines at the
13438 end. */
13439 if (!FRAME_SCROLL_REGION_OK (f))
13440 ins_del_lines (f, end + dvpos, -dvpos);
13441 }
13442
13443 set_terminal_window (f, 0);
13444 }
13445
13446 update_end (f);
13447 }
13448
13449 /* Shift reused rows of the current matrix to the right position.
13450 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13451 text. */
13452 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13453 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
13454 if (dvpos < 0)
13455 {
13456 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13457 bottom_vpos, dvpos);
13458 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13459 bottom_vpos, 0);
13460 }
13461 else if (dvpos > 0)
13462 {
13463 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13464 bottom_vpos, dvpos);
13465 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13466 first_unchanged_at_end_vpos + dvpos, 0);
13467 }
13468
13469 /* For frame-based redisplay, make sure that current frame and window
13470 matrix are in sync with respect to glyph memory. */
13471 if (!FRAME_WINDOW_P (f))
13472 sync_frame_with_window_matrix_rows (w);
13473
13474 /* Adjust buffer positions in reused rows. */
13475 if (delta)
13476 increment_matrix_positions (current_matrix,
13477 first_unchanged_at_end_vpos + dvpos,
13478 bottom_vpos, delta, delta_bytes);
13479
13480 /* Adjust Y positions. */
13481 if (dy)
13482 shift_glyph_matrix (w, current_matrix,
13483 first_unchanged_at_end_vpos + dvpos,
13484 bottom_vpos, dy);
13485
13486 if (first_unchanged_at_end_row)
13487 first_unchanged_at_end_row += dvpos;
13488
13489 /* If scrolling up, there may be some lines to display at the end of
13490 the window. */
13491 last_text_row_at_end = NULL;
13492 if (dy < 0)
13493 {
13494 /* Scrolling up can leave for example a partially visible line
13495 at the end of the window to be redisplayed. */
13496 /* Set last_row to the glyph row in the current matrix where the
13497 window end line is found. It has been moved up or down in
13498 the matrix by dvpos. */
13499 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13500 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13501
13502 /* If last_row is the window end line, it should display text. */
13503 xassert (last_row->displays_text_p);
13504
13505 /* If window end line was partially visible before, begin
13506 displaying at that line. Otherwise begin displaying with the
13507 line following it. */
13508 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13509 {
13510 init_to_row_start (&it, w, last_row);
13511 it.vpos = last_vpos;
13512 it.current_y = last_row->y;
13513 }
13514 else
13515 {
13516 init_to_row_end (&it, w, last_row);
13517 it.vpos = 1 + last_vpos;
13518 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13519 ++last_row;
13520 }
13521
13522 /* We may start in a continuation line. If so, we have to
13523 get the right continuation_lines_width and current_x. */
13524 it.continuation_lines_width = last_row->continuation_lines_width;
13525 it.hpos = it.current_x = 0;
13526
13527 /* Display the rest of the lines at the window end. */
13528 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13529 while (it.current_y < it.last_visible_y
13530 && !fonts_changed_p)
13531 {
13532 /* Is it always sure that the display agrees with lines in
13533 the current matrix? I don't think so, so we mark rows
13534 displayed invalid in the current matrix by setting their
13535 enabled_p flag to zero. */
13536 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13537 if (display_line (&it))
13538 last_text_row_at_end = it.glyph_row - 1;
13539 }
13540 }
13541
13542 /* Update window_end_pos and window_end_vpos. */
13543 if (first_unchanged_at_end_row
13544 && first_unchanged_at_end_row->y < it.last_visible_y
13545 && !last_text_row_at_end)
13546 {
13547 /* Window end line if one of the preserved rows from the current
13548 matrix. Set row to the last row displaying text in current
13549 matrix starting at first_unchanged_at_end_row, after
13550 scrolling. */
13551 xassert (first_unchanged_at_end_row->displays_text_p);
13552 row = find_last_row_displaying_text (w->current_matrix, &it,
13553 first_unchanged_at_end_row);
13554 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
13555
13556 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13557 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13558 w->window_end_vpos
13559 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
13560 xassert (w->window_end_bytepos >= 0);
13561 IF_DEBUG (debug_method_add (w, "A"));
13562 }
13563 else if (last_text_row_at_end)
13564 {
13565 w->window_end_pos
13566 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
13567 w->window_end_bytepos
13568 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
13569 w->window_end_vpos
13570 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
13571 xassert (w->window_end_bytepos >= 0);
13572 IF_DEBUG (debug_method_add (w, "B"));
13573 }
13574 else if (last_text_row)
13575 {
13576 /* We have displayed either to the end of the window or at the
13577 end of the window, i.e. the last row with text is to be found
13578 in the desired matrix. */
13579 w->window_end_pos
13580 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13581 w->window_end_bytepos
13582 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13583 w->window_end_vpos
13584 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
13585 xassert (w->window_end_bytepos >= 0);
13586 }
13587 else if (first_unchanged_at_end_row == NULL
13588 && last_text_row == NULL
13589 && last_text_row_at_end == NULL)
13590 {
13591 /* Displayed to end of window, but no line containing text was
13592 displayed. Lines were deleted at the end of the window. */
13593 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
13594 int vpos = XFASTINT (w->window_end_vpos);
13595 struct glyph_row *current_row = current_matrix->rows + vpos;
13596 struct glyph_row *desired_row = desired_matrix->rows + vpos;
13597
13598 for (row = NULL;
13599 row == NULL && vpos >= first_vpos;
13600 --vpos, --current_row, --desired_row)
13601 {
13602 if (desired_row->enabled_p)
13603 {
13604 if (desired_row->displays_text_p)
13605 row = desired_row;
13606 }
13607 else if (current_row->displays_text_p)
13608 row = current_row;
13609 }
13610
13611 xassert (row != NULL);
13612 w->window_end_vpos = make_number (vpos + 1);
13613 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13614 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13615 xassert (w->window_end_bytepos >= 0);
13616 IF_DEBUG (debug_method_add (w, "C"));
13617 }
13618 else
13619 abort ();
13620
13621 #if 0 /* This leads to problems, for instance when the cursor is
13622 at ZV, and the cursor line displays no text. */
13623 /* Disable rows below what's displayed in the window. This makes
13624 debugging easier. */
13625 enable_glyph_matrix_rows (current_matrix,
13626 XFASTINT (w->window_end_vpos) + 1,
13627 bottom_vpos, 0);
13628 #endif
13629
13630 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
13631 debug_end_vpos = XFASTINT (w->window_end_vpos));
13632
13633 /* Record that display has not been completed. */
13634 w->window_end_valid = Qnil;
13635 w->desired_matrix->no_scrolling_p = 1;
13636 return 3;
13637
13638 #undef GIVE_UP
13639 }
13640
13641
13642 \f
13643 /***********************************************************************
13644 More debugging support
13645 ***********************************************************************/
13646
13647 #if GLYPH_DEBUG
13648
13649 void dump_glyph_row P_ ((struct glyph_row *, int, int));
13650 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
13651 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
13652
13653
13654 /* Dump the contents of glyph matrix MATRIX on stderr.
13655
13656 GLYPHS 0 means don't show glyph contents.
13657 GLYPHS 1 means show glyphs in short form
13658 GLYPHS > 1 means show glyphs in long form. */
13659
13660 void
13661 dump_glyph_matrix (matrix, glyphs)
13662 struct glyph_matrix *matrix;
13663 int glyphs;
13664 {
13665 int i;
13666 for (i = 0; i < matrix->nrows; ++i)
13667 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
13668 }
13669
13670
13671 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
13672 the glyph row and area where the glyph comes from. */
13673
13674 void
13675 dump_glyph (row, glyph, area)
13676 struct glyph_row *row;
13677 struct glyph *glyph;
13678 int area;
13679 {
13680 if (glyph->type == CHAR_GLYPH)
13681 {
13682 fprintf (stderr,
13683 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13684 glyph - row->glyphs[TEXT_AREA],
13685 'C',
13686 glyph->charpos,
13687 (BUFFERP (glyph->object)
13688 ? 'B'
13689 : (STRINGP (glyph->object)
13690 ? 'S'
13691 : '-')),
13692 glyph->pixel_width,
13693 glyph->u.ch,
13694 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
13695 ? glyph->u.ch
13696 : '.'),
13697 glyph->face_id,
13698 glyph->left_box_line_p,
13699 glyph->right_box_line_p);
13700 }
13701 else if (glyph->type == STRETCH_GLYPH)
13702 {
13703 fprintf (stderr,
13704 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13705 glyph - row->glyphs[TEXT_AREA],
13706 'S',
13707 glyph->charpos,
13708 (BUFFERP (glyph->object)
13709 ? 'B'
13710 : (STRINGP (glyph->object)
13711 ? 'S'
13712 : '-')),
13713 glyph->pixel_width,
13714 0,
13715 '.',
13716 glyph->face_id,
13717 glyph->left_box_line_p,
13718 glyph->right_box_line_p);
13719 }
13720 else if (glyph->type == IMAGE_GLYPH)
13721 {
13722 fprintf (stderr,
13723 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13724 glyph - row->glyphs[TEXT_AREA],
13725 'I',
13726 glyph->charpos,
13727 (BUFFERP (glyph->object)
13728 ? 'B'
13729 : (STRINGP (glyph->object)
13730 ? 'S'
13731 : '-')),
13732 glyph->pixel_width,
13733 glyph->u.img_id,
13734 '.',
13735 glyph->face_id,
13736 glyph->left_box_line_p,
13737 glyph->right_box_line_p);
13738 }
13739 }
13740
13741
13742 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
13743 GLYPHS 0 means don't show glyph contents.
13744 GLYPHS 1 means show glyphs in short form
13745 GLYPHS > 1 means show glyphs in long form. */
13746
13747 void
13748 dump_glyph_row (row, vpos, glyphs)
13749 struct glyph_row *row;
13750 int vpos, glyphs;
13751 {
13752 if (glyphs != 1)
13753 {
13754 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
13755 fprintf (stderr, "=======================================================================\n");
13756
13757 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
13758 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
13759 vpos,
13760 MATRIX_ROW_START_CHARPOS (row),
13761 MATRIX_ROW_END_CHARPOS (row),
13762 row->used[TEXT_AREA],
13763 row->contains_overlapping_glyphs_p,
13764 row->enabled_p,
13765 row->truncated_on_left_p,
13766 row->truncated_on_right_p,
13767 row->overlay_arrow_p,
13768 row->continued_p,
13769 MATRIX_ROW_CONTINUATION_LINE_P (row),
13770 row->displays_text_p,
13771 row->ends_at_zv_p,
13772 row->fill_line_p,
13773 row->ends_in_middle_of_char_p,
13774 row->starts_in_middle_of_char_p,
13775 row->mouse_face_p,
13776 row->x,
13777 row->y,
13778 row->pixel_width,
13779 row->height,
13780 row->visible_height,
13781 row->ascent,
13782 row->phys_ascent);
13783 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
13784 row->end.overlay_string_index,
13785 row->continuation_lines_width);
13786 fprintf (stderr, "%9d %5d\n",
13787 CHARPOS (row->start.string_pos),
13788 CHARPOS (row->end.string_pos));
13789 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
13790 row->end.dpvec_index);
13791 }
13792
13793 if (glyphs > 1)
13794 {
13795 int area;
13796
13797 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13798 {
13799 struct glyph *glyph = row->glyphs[area];
13800 struct glyph *glyph_end = glyph + row->used[area];
13801
13802 /* Glyph for a line end in text. */
13803 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
13804 ++glyph_end;
13805
13806 if (glyph < glyph_end)
13807 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
13808
13809 for (; glyph < glyph_end; ++glyph)
13810 dump_glyph (row, glyph, area);
13811 }
13812 }
13813 else if (glyphs == 1)
13814 {
13815 int area;
13816
13817 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13818 {
13819 char *s = (char *) alloca (row->used[area] + 1);
13820 int i;
13821
13822 for (i = 0; i < row->used[area]; ++i)
13823 {
13824 struct glyph *glyph = row->glyphs[area] + i;
13825 if (glyph->type == CHAR_GLYPH
13826 && glyph->u.ch < 0x80
13827 && glyph->u.ch >= ' ')
13828 s[i] = glyph->u.ch;
13829 else
13830 s[i] = '.';
13831 }
13832
13833 s[i] = '\0';
13834 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
13835 }
13836 }
13837 }
13838
13839
13840 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
13841 Sdump_glyph_matrix, 0, 1, "p",
13842 doc: /* Dump the current matrix of the selected window to stderr.
13843 Shows contents of glyph row structures. With non-nil
13844 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
13845 glyphs in short form, otherwise show glyphs in long form. */)
13846 (glyphs)
13847 Lisp_Object glyphs;
13848 {
13849 struct window *w = XWINDOW (selected_window);
13850 struct buffer *buffer = XBUFFER (w->buffer);
13851
13852 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
13853 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
13854 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
13855 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
13856 fprintf (stderr, "=============================================\n");
13857 dump_glyph_matrix (w->current_matrix,
13858 NILP (glyphs) ? 0 : XINT (glyphs));
13859 return Qnil;
13860 }
13861
13862
13863 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
13864 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
13865 ()
13866 {
13867 struct frame *f = XFRAME (selected_frame);
13868 dump_glyph_matrix (f->current_matrix, 1);
13869 return Qnil;
13870 }
13871
13872
13873 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
13874 doc: /* Dump glyph row ROW to stderr.
13875 GLYPH 0 means don't dump glyphs.
13876 GLYPH 1 means dump glyphs in short form.
13877 GLYPH > 1 or omitted means dump glyphs in long form. */)
13878 (row, glyphs)
13879 Lisp_Object row, glyphs;
13880 {
13881 struct glyph_matrix *matrix;
13882 int vpos;
13883
13884 CHECK_NUMBER (row);
13885 matrix = XWINDOW (selected_window)->current_matrix;
13886 vpos = XINT (row);
13887 if (vpos >= 0 && vpos < matrix->nrows)
13888 dump_glyph_row (MATRIX_ROW (matrix, vpos),
13889 vpos,
13890 INTEGERP (glyphs) ? XINT (glyphs) : 2);
13891 return Qnil;
13892 }
13893
13894
13895 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
13896 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
13897 GLYPH 0 means don't dump glyphs.
13898 GLYPH 1 means dump glyphs in short form.
13899 GLYPH > 1 or omitted means dump glyphs in long form. */)
13900 (row, glyphs)
13901 Lisp_Object row, glyphs;
13902 {
13903 struct frame *sf = SELECTED_FRAME ();
13904 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
13905 int vpos;
13906
13907 CHECK_NUMBER (row);
13908 vpos = XINT (row);
13909 if (vpos >= 0 && vpos < m->nrows)
13910 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
13911 INTEGERP (glyphs) ? XINT (glyphs) : 2);
13912 return Qnil;
13913 }
13914
13915
13916 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
13917 doc: /* Toggle tracing of redisplay.
13918 With ARG, turn tracing on if and only if ARG is positive. */)
13919 (arg)
13920 Lisp_Object arg;
13921 {
13922 if (NILP (arg))
13923 trace_redisplay_p = !trace_redisplay_p;
13924 else
13925 {
13926 arg = Fprefix_numeric_value (arg);
13927 trace_redisplay_p = XINT (arg) > 0;
13928 }
13929
13930 return Qnil;
13931 }
13932
13933
13934 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
13935 doc: /* Like `format', but print result to stderr.
13936 usage: (trace-to-stderr STRING &rest OBJECTS) */)
13937 (nargs, args)
13938 int nargs;
13939 Lisp_Object *args;
13940 {
13941 Lisp_Object s = Fformat (nargs, args);
13942 fprintf (stderr, "%s", SDATA (s));
13943 return Qnil;
13944 }
13945
13946 #endif /* GLYPH_DEBUG */
13947
13948
13949 \f
13950 /***********************************************************************
13951 Building Desired Matrix Rows
13952 ***********************************************************************/
13953
13954 /* Return a temporary glyph row holding the glyphs of an overlay
13955 arrow. Only used for non-window-redisplay windows. */
13956
13957 static struct glyph_row *
13958 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
13959 struct window *w;
13960 Lisp_Object overlay_arrow_string;
13961 {
13962 struct frame *f = XFRAME (WINDOW_FRAME (w));
13963 struct buffer *buffer = XBUFFER (w->buffer);
13964 struct buffer *old = current_buffer;
13965 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
13966 int arrow_len = SCHARS (overlay_arrow_string);
13967 const unsigned char *arrow_end = arrow_string + arrow_len;
13968 const unsigned char *p;
13969 struct it it;
13970 int multibyte_p;
13971 int n_glyphs_before;
13972
13973 set_buffer_temp (buffer);
13974 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
13975 it.glyph_row->used[TEXT_AREA] = 0;
13976 SET_TEXT_POS (it.position, 0, 0);
13977
13978 multibyte_p = !NILP (buffer->enable_multibyte_characters);
13979 p = arrow_string;
13980 while (p < arrow_end)
13981 {
13982 Lisp_Object face, ilisp;
13983
13984 /* Get the next character. */
13985 if (multibyte_p)
13986 it.c = string_char_and_length (p, arrow_len, &it.len);
13987 else
13988 it.c = *p, it.len = 1;
13989 p += it.len;
13990
13991 /* Get its face. */
13992 ilisp = make_number (p - arrow_string);
13993 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
13994 it.face_id = compute_char_face (f, it.c, face);
13995
13996 /* Compute its width, get its glyphs. */
13997 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
13998 SET_TEXT_POS (it.position, -1, -1);
13999 PRODUCE_GLYPHS (&it);
14000
14001 /* If this character doesn't fit any more in the line, we have
14002 to remove some glyphs. */
14003 if (it.current_x > it.last_visible_x)
14004 {
14005 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14006 break;
14007 }
14008 }
14009
14010 set_buffer_temp (old);
14011 return it.glyph_row;
14012 }
14013
14014
14015 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14016 glyphs are only inserted for terminal frames since we can't really
14017 win with truncation glyphs when partially visible glyphs are
14018 involved. Which glyphs to insert is determined by
14019 produce_special_glyphs. */
14020
14021 static void
14022 insert_left_trunc_glyphs (it)
14023 struct it *it;
14024 {
14025 struct it truncate_it;
14026 struct glyph *from, *end, *to, *toend;
14027
14028 xassert (!FRAME_WINDOW_P (it->f));
14029
14030 /* Get the truncation glyphs. */
14031 truncate_it = *it;
14032 truncate_it.current_x = 0;
14033 truncate_it.face_id = DEFAULT_FACE_ID;
14034 truncate_it.glyph_row = &scratch_glyph_row;
14035 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14036 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14037 truncate_it.object = make_number (0);
14038 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14039
14040 /* Overwrite glyphs from IT with truncation glyphs. */
14041 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14042 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14043 to = it->glyph_row->glyphs[TEXT_AREA];
14044 toend = to + it->glyph_row->used[TEXT_AREA];
14045
14046 while (from < end)
14047 *to++ = *from++;
14048
14049 /* There may be padding glyphs left over. Overwrite them too. */
14050 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14051 {
14052 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14053 while (from < end)
14054 *to++ = *from++;
14055 }
14056
14057 if (to > toend)
14058 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14059 }
14060
14061
14062 /* Compute the pixel height and width of IT->glyph_row.
14063
14064 Most of the time, ascent and height of a display line will be equal
14065 to the max_ascent and max_height values of the display iterator
14066 structure. This is not the case if
14067
14068 1. We hit ZV without displaying anything. In this case, max_ascent
14069 and max_height will be zero.
14070
14071 2. We have some glyphs that don't contribute to the line height.
14072 (The glyph row flag contributes_to_line_height_p is for future
14073 pixmap extensions).
14074
14075 The first case is easily covered by using default values because in
14076 these cases, the line height does not really matter, except that it
14077 must not be zero. */
14078
14079 static void
14080 compute_line_metrics (it)
14081 struct it *it;
14082 {
14083 struct glyph_row *row = it->glyph_row;
14084 int area, i;
14085
14086 if (FRAME_WINDOW_P (it->f))
14087 {
14088 int i, min_y, max_y;
14089
14090 /* The line may consist of one space only, that was added to
14091 place the cursor on it. If so, the row's height hasn't been
14092 computed yet. */
14093 if (row->height == 0)
14094 {
14095 if (it->max_ascent + it->max_descent == 0)
14096 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14097 row->ascent = it->max_ascent;
14098 row->height = it->max_ascent + it->max_descent;
14099 row->phys_ascent = it->max_phys_ascent;
14100 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14101 }
14102
14103 /* Compute the width of this line. */
14104 row->pixel_width = row->x;
14105 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14106 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14107
14108 xassert (row->pixel_width >= 0);
14109 xassert (row->ascent >= 0 && row->height > 0);
14110
14111 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14112 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14113
14114 /* If first line's physical ascent is larger than its logical
14115 ascent, use the physical ascent, and make the row taller.
14116 This makes accented characters fully visible. */
14117 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14118 && row->phys_ascent > row->ascent)
14119 {
14120 row->height += row->phys_ascent - row->ascent;
14121 row->ascent = row->phys_ascent;
14122 }
14123
14124 /* Compute how much of the line is visible. */
14125 row->visible_height = row->height;
14126
14127 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14128 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14129
14130 if (row->y < min_y)
14131 row->visible_height -= min_y - row->y;
14132 if (row->y + row->height > max_y)
14133 row->visible_height -= row->y + row->height - max_y;
14134 }
14135 else
14136 {
14137 row->pixel_width = row->used[TEXT_AREA];
14138 if (row->continued_p)
14139 row->pixel_width -= it->continuation_pixel_width;
14140 else if (row->truncated_on_right_p)
14141 row->pixel_width -= it->truncation_pixel_width;
14142 row->ascent = row->phys_ascent = 0;
14143 row->height = row->phys_height = row->visible_height = 1;
14144 }
14145
14146 /* Compute a hash code for this row. */
14147 row->hash = 0;
14148 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14149 for (i = 0; i < row->used[area]; ++i)
14150 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14151 + row->glyphs[area][i].u.val
14152 + row->glyphs[area][i].face_id
14153 + row->glyphs[area][i].padding_p
14154 + (row->glyphs[area][i].type << 2));
14155
14156 it->max_ascent = it->max_descent = 0;
14157 it->max_phys_ascent = it->max_phys_descent = 0;
14158 }
14159
14160
14161 /* Append one space to the glyph row of iterator IT if doing a
14162 window-based redisplay. The space has the same face as
14163 IT->face_id. Value is non-zero if a space was added.
14164
14165 This function is called to make sure that there is always one glyph
14166 at the end of a glyph row that the cursor can be set on under
14167 window-systems. (If there weren't such a glyph we would not know
14168 how wide and tall a box cursor should be displayed).
14169
14170 At the same time this space let's a nicely handle clearing to the
14171 end of the line if the row ends in italic text. */
14172
14173 static int
14174 append_space_for_newline (it, default_face_p)
14175 struct it *it;
14176 int default_face_p;
14177 {
14178 if (FRAME_WINDOW_P (it->f))
14179 {
14180 int n = it->glyph_row->used[TEXT_AREA];
14181
14182 if (it->glyph_row->glyphs[TEXT_AREA] + n
14183 < it->glyph_row->glyphs[1 + TEXT_AREA])
14184 {
14185 /* Save some values that must not be changed.
14186 Must save IT->c and IT->len because otherwise
14187 ITERATOR_AT_END_P wouldn't work anymore after
14188 append_space_for_newline has been called. */
14189 enum display_element_type saved_what = it->what;
14190 int saved_c = it->c, saved_len = it->len;
14191 int saved_x = it->current_x;
14192 int saved_face_id = it->face_id;
14193 struct text_pos saved_pos;
14194 Lisp_Object saved_object;
14195 struct face *face;
14196
14197 saved_object = it->object;
14198 saved_pos = it->position;
14199
14200 it->what = IT_CHARACTER;
14201 bzero (&it->position, sizeof it->position);
14202 it->object = make_number (0);
14203 it->c = ' ';
14204 it->len = 1;
14205
14206 if (default_face_p)
14207 it->face_id = DEFAULT_FACE_ID;
14208 else if (it->face_before_selective_p)
14209 it->face_id = it->saved_face_id;
14210 face = FACE_FROM_ID (it->f, it->face_id);
14211 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14212
14213 PRODUCE_GLYPHS (it);
14214
14215 it->override_ascent = -1;
14216 it->constrain_row_ascent_descent_p = 0;
14217 it->current_x = saved_x;
14218 it->object = saved_object;
14219 it->position = saved_pos;
14220 it->what = saved_what;
14221 it->face_id = saved_face_id;
14222 it->len = saved_len;
14223 it->c = saved_c;
14224 return 1;
14225 }
14226 }
14227
14228 return 0;
14229 }
14230
14231
14232 /* Extend the face of the last glyph in the text area of IT->glyph_row
14233 to the end of the display line. Called from display_line.
14234 If the glyph row is empty, add a space glyph to it so that we
14235 know the face to draw. Set the glyph row flag fill_line_p. */
14236
14237 static void
14238 extend_face_to_end_of_line (it)
14239 struct it *it;
14240 {
14241 struct face *face;
14242 struct frame *f = it->f;
14243
14244 /* If line is already filled, do nothing. */
14245 if (it->current_x >= it->last_visible_x)
14246 return;
14247
14248 /* Face extension extends the background and box of IT->face_id
14249 to the end of the line. If the background equals the background
14250 of the frame, we don't have to do anything. */
14251 if (it->face_before_selective_p)
14252 face = FACE_FROM_ID (it->f, it->saved_face_id);
14253 else
14254 face = FACE_FROM_ID (f, it->face_id);
14255
14256 if (FRAME_WINDOW_P (f)
14257 && face->box == FACE_NO_BOX
14258 && face->background == FRAME_BACKGROUND_PIXEL (f)
14259 && !face->stipple)
14260 return;
14261
14262 /* Set the glyph row flag indicating that the face of the last glyph
14263 in the text area has to be drawn to the end of the text area. */
14264 it->glyph_row->fill_line_p = 1;
14265
14266 /* If current character of IT is not ASCII, make sure we have the
14267 ASCII face. This will be automatically undone the next time
14268 get_next_display_element returns a multibyte character. Note
14269 that the character will always be single byte in unibyte text. */
14270 if (!SINGLE_BYTE_CHAR_P (it->c))
14271 {
14272 it->face_id = FACE_FOR_CHAR (f, face, 0);
14273 }
14274
14275 if (FRAME_WINDOW_P (f))
14276 {
14277 /* If the row is empty, add a space with the current face of IT,
14278 so that we know which face to draw. */
14279 if (it->glyph_row->used[TEXT_AREA] == 0)
14280 {
14281 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14282 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14283 it->glyph_row->used[TEXT_AREA] = 1;
14284 }
14285 }
14286 else
14287 {
14288 /* Save some values that must not be changed. */
14289 int saved_x = it->current_x;
14290 struct text_pos saved_pos;
14291 Lisp_Object saved_object;
14292 enum display_element_type saved_what = it->what;
14293 int saved_face_id = it->face_id;
14294
14295 saved_object = it->object;
14296 saved_pos = it->position;
14297
14298 it->what = IT_CHARACTER;
14299 bzero (&it->position, sizeof it->position);
14300 it->object = make_number (0);
14301 it->c = ' ';
14302 it->len = 1;
14303 it->face_id = face->id;
14304
14305 PRODUCE_GLYPHS (it);
14306
14307 while (it->current_x <= it->last_visible_x)
14308 PRODUCE_GLYPHS (it);
14309
14310 /* Don't count these blanks really. It would let us insert a left
14311 truncation glyph below and make us set the cursor on them, maybe. */
14312 it->current_x = saved_x;
14313 it->object = saved_object;
14314 it->position = saved_pos;
14315 it->what = saved_what;
14316 it->face_id = saved_face_id;
14317 }
14318 }
14319
14320
14321 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14322 trailing whitespace. */
14323
14324 static int
14325 trailing_whitespace_p (charpos)
14326 int charpos;
14327 {
14328 int bytepos = CHAR_TO_BYTE (charpos);
14329 int c = 0;
14330
14331 while (bytepos < ZV_BYTE
14332 && (c = FETCH_CHAR (bytepos),
14333 c == ' ' || c == '\t'))
14334 ++bytepos;
14335
14336 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14337 {
14338 if (bytepos != PT_BYTE)
14339 return 1;
14340 }
14341 return 0;
14342 }
14343
14344
14345 /* Highlight trailing whitespace, if any, in ROW. */
14346
14347 void
14348 highlight_trailing_whitespace (f, row)
14349 struct frame *f;
14350 struct glyph_row *row;
14351 {
14352 int used = row->used[TEXT_AREA];
14353
14354 if (used)
14355 {
14356 struct glyph *start = row->glyphs[TEXT_AREA];
14357 struct glyph *glyph = start + used - 1;
14358
14359 /* Skip over glyphs inserted to display the cursor at the
14360 end of a line, for extending the face of the last glyph
14361 to the end of the line on terminals, and for truncation
14362 and continuation glyphs. */
14363 while (glyph >= start
14364 && glyph->type == CHAR_GLYPH
14365 && INTEGERP (glyph->object))
14366 --glyph;
14367
14368 /* If last glyph is a space or stretch, and it's trailing
14369 whitespace, set the face of all trailing whitespace glyphs in
14370 IT->glyph_row to `trailing-whitespace'. */
14371 if (glyph >= start
14372 && BUFFERP (glyph->object)
14373 && (glyph->type == STRETCH_GLYPH
14374 || (glyph->type == CHAR_GLYPH
14375 && glyph->u.ch == ' '))
14376 && trailing_whitespace_p (glyph->charpos))
14377 {
14378 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
14379
14380 while (glyph >= start
14381 && BUFFERP (glyph->object)
14382 && (glyph->type == STRETCH_GLYPH
14383 || (glyph->type == CHAR_GLYPH
14384 && glyph->u.ch == ' ')))
14385 (glyph--)->face_id = face_id;
14386 }
14387 }
14388 }
14389
14390
14391 /* Value is non-zero if glyph row ROW in window W should be
14392 used to hold the cursor. */
14393
14394 static int
14395 cursor_row_p (w, row)
14396 struct window *w;
14397 struct glyph_row *row;
14398 {
14399 int cursor_row_p = 1;
14400
14401 if (PT == MATRIX_ROW_END_CHARPOS (row))
14402 {
14403 /* If the row ends with a newline from a string, we don't want
14404 the cursor there (if the row is continued it doesn't end in a
14405 newline). */
14406 if (CHARPOS (row->end.string_pos) >= 0
14407 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14408 cursor_row_p = row->continued_p;
14409
14410 /* If the row ends at ZV, display the cursor at the end of that
14411 row instead of at the start of the row below. */
14412 else if (row->ends_at_zv_p)
14413 cursor_row_p = 1;
14414 else
14415 cursor_row_p = 0;
14416 }
14417
14418 return cursor_row_p;
14419 }
14420
14421
14422 /* Construct the glyph row IT->glyph_row in the desired matrix of
14423 IT->w from text at the current position of IT. See dispextern.h
14424 for an overview of struct it. Value is non-zero if
14425 IT->glyph_row displays text, as opposed to a line displaying ZV
14426 only. */
14427
14428 static int
14429 display_line (it)
14430 struct it *it;
14431 {
14432 struct glyph_row *row = it->glyph_row;
14433 int overlay_arrow_bitmap;
14434 Lisp_Object overlay_arrow_string;
14435
14436 /* We always start displaying at hpos zero even if hscrolled. */
14437 xassert (it->hpos == 0 && it->current_x == 0);
14438
14439 /* We must not display in a row that's not a text row. */
14440 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14441 < it->w->desired_matrix->nrows);
14442
14443 /* Is IT->w showing the region? */
14444 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
14445
14446 /* Clear the result glyph row and enable it. */
14447 prepare_desired_row (row);
14448
14449 row->y = it->current_y;
14450 row->start = it->start;
14451 row->continuation_lines_width = it->continuation_lines_width;
14452 row->displays_text_p = 1;
14453 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14454 it->starts_in_middle_of_char_p = 0;
14455
14456 /* Arrange the overlays nicely for our purposes. Usually, we call
14457 display_line on only one line at a time, in which case this
14458 can't really hurt too much, or we call it on lines which appear
14459 one after another in the buffer, in which case all calls to
14460 recenter_overlay_lists but the first will be pretty cheap. */
14461 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14462
14463 /* Move over display elements that are not visible because we are
14464 hscrolled. This may stop at an x-position < IT->first_visible_x
14465 if the first glyph is partially visible or if we hit a line end. */
14466 if (it->current_x < it->first_visible_x)
14467 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14468 MOVE_TO_POS | MOVE_TO_X);
14469
14470 /* Get the initial row height. This is either the height of the
14471 text hscrolled, if there is any, or zero. */
14472 row->ascent = it->max_ascent;
14473 row->height = it->max_ascent + it->max_descent;
14474 row->phys_ascent = it->max_phys_ascent;
14475 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14476
14477 /* Loop generating characters. The loop is left with IT on the next
14478 character to display. */
14479 while (1)
14480 {
14481 int n_glyphs_before, hpos_before, x_before;
14482 int x, i, nglyphs;
14483 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
14484
14485 /* Retrieve the next thing to display. Value is zero if end of
14486 buffer reached. */
14487 if (!get_next_display_element (it))
14488 {
14489 /* Maybe add a space at the end of this line that is used to
14490 display the cursor there under X. Set the charpos of the
14491 first glyph of blank lines not corresponding to any text
14492 to -1. */
14493 #ifdef HAVE_WINDOW_SYSTEM
14494 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14495 row->exact_window_width_line_p = 1;
14496 else
14497 #endif /* HAVE_WINDOW_SYSTEM */
14498 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
14499 || row->used[TEXT_AREA] == 0)
14500 {
14501 row->glyphs[TEXT_AREA]->charpos = -1;
14502 row->displays_text_p = 0;
14503
14504 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
14505 && (!MINI_WINDOW_P (it->w)
14506 || (minibuf_level && EQ (it->window, minibuf_window))))
14507 row->indicate_empty_line_p = 1;
14508 }
14509
14510 it->continuation_lines_width = 0;
14511 row->ends_at_zv_p = 1;
14512 break;
14513 }
14514
14515 /* Now, get the metrics of what we want to display. This also
14516 generates glyphs in `row' (which is IT->glyph_row). */
14517 n_glyphs_before = row->used[TEXT_AREA];
14518 x = it->current_x;
14519
14520 /* Remember the line height so far in case the next element doesn't
14521 fit on the line. */
14522 if (!it->truncate_lines_p)
14523 {
14524 ascent = it->max_ascent;
14525 descent = it->max_descent;
14526 phys_ascent = it->max_phys_ascent;
14527 phys_descent = it->max_phys_descent;
14528 }
14529
14530 PRODUCE_GLYPHS (it);
14531
14532 /* If this display element was in marginal areas, continue with
14533 the next one. */
14534 if (it->area != TEXT_AREA)
14535 {
14536 row->ascent = max (row->ascent, it->max_ascent);
14537 row->height = max (row->height, it->max_ascent + it->max_descent);
14538 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14539 row->phys_height = max (row->phys_height,
14540 it->max_phys_ascent + it->max_phys_descent);
14541 set_iterator_to_next (it, 1);
14542 continue;
14543 }
14544
14545 /* Does the display element fit on the line? If we truncate
14546 lines, we should draw past the right edge of the window. If
14547 we don't truncate, we want to stop so that we can display the
14548 continuation glyph before the right margin. If lines are
14549 continued, there are two possible strategies for characters
14550 resulting in more than 1 glyph (e.g. tabs): Display as many
14551 glyphs as possible in this line and leave the rest for the
14552 continuation line, or display the whole element in the next
14553 line. Original redisplay did the former, so we do it also. */
14554 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14555 hpos_before = it->hpos;
14556 x_before = x;
14557
14558 if (/* Not a newline. */
14559 nglyphs > 0
14560 /* Glyphs produced fit entirely in the line. */
14561 && it->current_x < it->last_visible_x)
14562 {
14563 it->hpos += nglyphs;
14564 row->ascent = max (row->ascent, it->max_ascent);
14565 row->height = max (row->height, it->max_ascent + it->max_descent);
14566 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14567 row->phys_height = max (row->phys_height,
14568 it->max_phys_ascent + it->max_phys_descent);
14569 if (it->current_x - it->pixel_width < it->first_visible_x)
14570 row->x = x - it->first_visible_x;
14571 }
14572 else
14573 {
14574 int new_x;
14575 struct glyph *glyph;
14576
14577 for (i = 0; i < nglyphs; ++i, x = new_x)
14578 {
14579 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14580 new_x = x + glyph->pixel_width;
14581
14582 if (/* Lines are continued. */
14583 !it->truncate_lines_p
14584 && (/* Glyph doesn't fit on the line. */
14585 new_x > it->last_visible_x
14586 /* Or it fits exactly on a window system frame. */
14587 || (new_x == it->last_visible_x
14588 && FRAME_WINDOW_P (it->f))))
14589 {
14590 /* End of a continued line. */
14591
14592 if (it->hpos == 0
14593 || (new_x == it->last_visible_x
14594 && FRAME_WINDOW_P (it->f)))
14595 {
14596 /* Current glyph is the only one on the line or
14597 fits exactly on the line. We must continue
14598 the line because we can't draw the cursor
14599 after the glyph. */
14600 row->continued_p = 1;
14601 it->current_x = new_x;
14602 it->continuation_lines_width += new_x;
14603 ++it->hpos;
14604 if (i == nglyphs - 1)
14605 {
14606 set_iterator_to_next (it, 1);
14607 #ifdef HAVE_WINDOW_SYSTEM
14608 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14609 {
14610 if (!get_next_display_element (it))
14611 {
14612 row->exact_window_width_line_p = 1;
14613 it->continuation_lines_width = 0;
14614 row->continued_p = 0;
14615 row->ends_at_zv_p = 1;
14616 }
14617 else if (ITERATOR_AT_END_OF_LINE_P (it))
14618 {
14619 row->continued_p = 0;
14620 row->exact_window_width_line_p = 1;
14621 }
14622 }
14623 #endif /* HAVE_WINDOW_SYSTEM */
14624 }
14625 }
14626 else if (CHAR_GLYPH_PADDING_P (*glyph)
14627 && !FRAME_WINDOW_P (it->f))
14628 {
14629 /* A padding glyph that doesn't fit on this line.
14630 This means the whole character doesn't fit
14631 on the line. */
14632 row->used[TEXT_AREA] = n_glyphs_before;
14633
14634 /* Fill the rest of the row with continuation
14635 glyphs like in 20.x. */
14636 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
14637 < row->glyphs[1 + TEXT_AREA])
14638 produce_special_glyphs (it, IT_CONTINUATION);
14639
14640 row->continued_p = 1;
14641 it->current_x = x_before;
14642 it->continuation_lines_width += x_before;
14643
14644 /* Restore the height to what it was before the
14645 element not fitting on the line. */
14646 it->max_ascent = ascent;
14647 it->max_descent = descent;
14648 it->max_phys_ascent = phys_ascent;
14649 it->max_phys_descent = phys_descent;
14650 }
14651 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
14652 {
14653 /* A TAB that extends past the right edge of the
14654 window. This produces a single glyph on
14655 window system frames. We leave the glyph in
14656 this row and let it fill the row, but don't
14657 consume the TAB. */
14658 it->continuation_lines_width += it->last_visible_x;
14659 row->ends_in_middle_of_char_p = 1;
14660 row->continued_p = 1;
14661 glyph->pixel_width = it->last_visible_x - x;
14662 it->starts_in_middle_of_char_p = 1;
14663 }
14664 else
14665 {
14666 /* Something other than a TAB that draws past
14667 the right edge of the window. Restore
14668 positions to values before the element. */
14669 row->used[TEXT_AREA] = n_glyphs_before + i;
14670
14671 /* Display continuation glyphs. */
14672 if (!FRAME_WINDOW_P (it->f))
14673 produce_special_glyphs (it, IT_CONTINUATION);
14674 row->continued_p = 1;
14675
14676 it->continuation_lines_width += x;
14677
14678 if (nglyphs > 1 && i > 0)
14679 {
14680 row->ends_in_middle_of_char_p = 1;
14681 it->starts_in_middle_of_char_p = 1;
14682 }
14683
14684 /* Restore the height to what it was before the
14685 element not fitting on the line. */
14686 it->max_ascent = ascent;
14687 it->max_descent = descent;
14688 it->max_phys_ascent = phys_ascent;
14689 it->max_phys_descent = phys_descent;
14690 }
14691
14692 break;
14693 }
14694 else if (new_x > it->first_visible_x)
14695 {
14696 /* Increment number of glyphs actually displayed. */
14697 ++it->hpos;
14698
14699 if (x < it->first_visible_x)
14700 /* Glyph is partially visible, i.e. row starts at
14701 negative X position. */
14702 row->x = x - it->first_visible_x;
14703 }
14704 else
14705 {
14706 /* Glyph is completely off the left margin of the
14707 window. This should not happen because of the
14708 move_it_in_display_line at the start of this
14709 function, unless the text display area of the
14710 window is empty. */
14711 xassert (it->first_visible_x <= it->last_visible_x);
14712 }
14713 }
14714
14715 row->ascent = max (row->ascent, it->max_ascent);
14716 row->height = max (row->height, it->max_ascent + it->max_descent);
14717 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14718 row->phys_height = max (row->phys_height,
14719 it->max_phys_ascent + it->max_phys_descent);
14720
14721 /* End of this display line if row is continued. */
14722 if (row->continued_p || row->ends_at_zv_p)
14723 break;
14724 }
14725
14726 at_end_of_line:
14727 /* Is this a line end? If yes, we're also done, after making
14728 sure that a non-default face is extended up to the right
14729 margin of the window. */
14730 if (ITERATOR_AT_END_OF_LINE_P (it))
14731 {
14732 int used_before = row->used[TEXT_AREA];
14733
14734 row->ends_in_newline_from_string_p = STRINGP (it->object);
14735
14736 #ifdef HAVE_WINDOW_SYSTEM
14737 /* Add a space at the end of the line that is used to
14738 display the cursor there. */
14739 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14740 append_space_for_newline (it, 0);
14741 #endif /* HAVE_WINDOW_SYSTEM */
14742
14743 /* Extend the face to the end of the line. */
14744 extend_face_to_end_of_line (it);
14745
14746 /* Make sure we have the position. */
14747 if (used_before == 0)
14748 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
14749
14750 /* Consume the line end. This skips over invisible lines. */
14751 set_iterator_to_next (it, 1);
14752 it->continuation_lines_width = 0;
14753 break;
14754 }
14755
14756 /* Proceed with next display element. Note that this skips
14757 over lines invisible because of selective display. */
14758 set_iterator_to_next (it, 1);
14759
14760 /* If we truncate lines, we are done when the last displayed
14761 glyphs reach past the right margin of the window. */
14762 if (it->truncate_lines_p
14763 && (FRAME_WINDOW_P (it->f)
14764 ? (it->current_x >= it->last_visible_x)
14765 : (it->current_x > it->last_visible_x)))
14766 {
14767 /* Maybe add truncation glyphs. */
14768 if (!FRAME_WINDOW_P (it->f))
14769 {
14770 int i, n;
14771
14772 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14773 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14774 break;
14775
14776 for (n = row->used[TEXT_AREA]; i < n; ++i)
14777 {
14778 row->used[TEXT_AREA] = i;
14779 produce_special_glyphs (it, IT_TRUNCATION);
14780 }
14781 }
14782 #ifdef HAVE_WINDOW_SYSTEM
14783 else
14784 {
14785 /* Don't truncate if we can overflow newline into fringe. */
14786 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14787 {
14788 if (!get_next_display_element (it))
14789 {
14790 #ifdef HAVE_WINDOW_SYSTEM
14791 it->continuation_lines_width = 0;
14792 row->ends_at_zv_p = 1;
14793 row->exact_window_width_line_p = 1;
14794 break;
14795 #endif /* HAVE_WINDOW_SYSTEM */
14796 }
14797 if (ITERATOR_AT_END_OF_LINE_P (it))
14798 {
14799 row->exact_window_width_line_p = 1;
14800 goto at_end_of_line;
14801 }
14802 }
14803 }
14804 #endif /* HAVE_WINDOW_SYSTEM */
14805
14806 row->truncated_on_right_p = 1;
14807 it->continuation_lines_width = 0;
14808 reseat_at_next_visible_line_start (it, 0);
14809 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
14810 it->hpos = hpos_before;
14811 it->current_x = x_before;
14812 break;
14813 }
14814 }
14815
14816 /* If line is not empty and hscrolled, maybe insert truncation glyphs
14817 at the left window margin. */
14818 if (it->first_visible_x
14819 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
14820 {
14821 if (!FRAME_WINDOW_P (it->f))
14822 insert_left_trunc_glyphs (it);
14823 row->truncated_on_left_p = 1;
14824 }
14825
14826 /* If the start of this line is the overlay arrow-position, then
14827 mark this glyph row as the one containing the overlay arrow.
14828 This is clearly a mess with variable size fonts. It would be
14829 better to let it be displayed like cursors under X. */
14830 if (! overlay_arrow_seen
14831 && (overlay_arrow_string
14832 = overlay_arrow_at_row (it->f, row, &overlay_arrow_bitmap),
14833 !NILP (overlay_arrow_string)))
14834 {
14835 /* Overlay arrow in window redisplay is a fringe bitmap. */
14836 if (!FRAME_WINDOW_P (it->f))
14837 {
14838 struct glyph_row *arrow_row
14839 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
14840 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
14841 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
14842 struct glyph *p = row->glyphs[TEXT_AREA];
14843 struct glyph *p2, *end;
14844
14845 /* Copy the arrow glyphs. */
14846 while (glyph < arrow_end)
14847 *p++ = *glyph++;
14848
14849 /* Throw away padding glyphs. */
14850 p2 = p;
14851 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
14852 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
14853 ++p2;
14854 if (p2 > p)
14855 {
14856 while (p2 < end)
14857 *p++ = *p2++;
14858 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
14859 }
14860 }
14861
14862 overlay_arrow_seen = 1;
14863 it->w->overlay_arrow_bitmap = overlay_arrow_bitmap;
14864 row->overlay_arrow_p = 1;
14865 }
14866
14867 /* Compute pixel dimensions of this line. */
14868 compute_line_metrics (it);
14869
14870 /* Remember the position at which this line ends. */
14871 row->end = it->current;
14872
14873 /* Save fringe bitmaps in this row. */
14874 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
14875 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
14876 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
14877 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
14878
14879 it->left_user_fringe_bitmap = 0;
14880 it->left_user_fringe_face_id = 0;
14881 it->right_user_fringe_bitmap = 0;
14882 it->right_user_fringe_face_id = 0;
14883
14884 /* Maybe set the cursor. */
14885 if (it->w->cursor.vpos < 0
14886 && PT >= MATRIX_ROW_START_CHARPOS (row)
14887 && PT <= MATRIX_ROW_END_CHARPOS (row)
14888 && cursor_row_p (it->w, row))
14889 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
14890
14891 /* Highlight trailing whitespace. */
14892 if (!NILP (Vshow_trailing_whitespace))
14893 highlight_trailing_whitespace (it->f, it->glyph_row);
14894
14895 /* Prepare for the next line. This line starts horizontally at (X
14896 HPOS) = (0 0). Vertical positions are incremented. As a
14897 convenience for the caller, IT->glyph_row is set to the next
14898 row to be used. */
14899 it->current_x = it->hpos = 0;
14900 it->current_y += row->height;
14901 ++it->vpos;
14902 ++it->glyph_row;
14903 it->start = it->current;
14904 return row->displays_text_p;
14905 }
14906
14907
14908 \f
14909 /***********************************************************************
14910 Menu Bar
14911 ***********************************************************************/
14912
14913 /* Redisplay the menu bar in the frame for window W.
14914
14915 The menu bar of X frames that don't have X toolkit support is
14916 displayed in a special window W->frame->menu_bar_window.
14917
14918 The menu bar of terminal frames is treated specially as far as
14919 glyph matrices are concerned. Menu bar lines are not part of
14920 windows, so the update is done directly on the frame matrix rows
14921 for the menu bar. */
14922
14923 static void
14924 display_menu_bar (w)
14925 struct window *w;
14926 {
14927 struct frame *f = XFRAME (WINDOW_FRAME (w));
14928 struct it it;
14929 Lisp_Object items;
14930 int i;
14931
14932 /* Don't do all this for graphical frames. */
14933 #ifdef HAVE_NTGUI
14934 if (!NILP (Vwindow_system))
14935 return;
14936 #endif
14937 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
14938 if (FRAME_X_P (f))
14939 return;
14940 #endif
14941 #ifdef MAC_OS
14942 if (FRAME_MAC_P (f))
14943 return;
14944 #endif
14945
14946 #ifdef USE_X_TOOLKIT
14947 xassert (!FRAME_WINDOW_P (f));
14948 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
14949 it.first_visible_x = 0;
14950 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
14951 #else /* not USE_X_TOOLKIT */
14952 if (FRAME_WINDOW_P (f))
14953 {
14954 /* Menu bar lines are displayed in the desired matrix of the
14955 dummy window menu_bar_window. */
14956 struct window *menu_w;
14957 xassert (WINDOWP (f->menu_bar_window));
14958 menu_w = XWINDOW (f->menu_bar_window);
14959 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
14960 MENU_FACE_ID);
14961 it.first_visible_x = 0;
14962 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
14963 }
14964 else
14965 {
14966 /* This is a TTY frame, i.e. character hpos/vpos are used as
14967 pixel x/y. */
14968 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
14969 MENU_FACE_ID);
14970 it.first_visible_x = 0;
14971 it.last_visible_x = FRAME_COLS (f);
14972 }
14973 #endif /* not USE_X_TOOLKIT */
14974
14975 if (! mode_line_inverse_video)
14976 /* Force the menu-bar to be displayed in the default face. */
14977 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
14978
14979 /* Clear all rows of the menu bar. */
14980 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
14981 {
14982 struct glyph_row *row = it.glyph_row + i;
14983 clear_glyph_row (row);
14984 row->enabled_p = 1;
14985 row->full_width_p = 1;
14986 }
14987
14988 /* Display all items of the menu bar. */
14989 items = FRAME_MENU_BAR_ITEMS (it.f);
14990 for (i = 0; i < XVECTOR (items)->size; i += 4)
14991 {
14992 Lisp_Object string;
14993
14994 /* Stop at nil string. */
14995 string = AREF (items, i + 1);
14996 if (NILP (string))
14997 break;
14998
14999 /* Remember where item was displayed. */
15000 AREF (items, i + 3) = make_number (it.hpos);
15001
15002 /* Display the item, pad with one space. */
15003 if (it.current_x < it.last_visible_x)
15004 display_string (NULL, string, Qnil, 0, 0, &it,
15005 SCHARS (string) + 1, 0, 0, -1);
15006 }
15007
15008 /* Fill out the line with spaces. */
15009 if (it.current_x < it.last_visible_x)
15010 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15011
15012 /* Compute the total height of the lines. */
15013 compute_line_metrics (&it);
15014 }
15015
15016
15017 \f
15018 /***********************************************************************
15019 Mode Line
15020 ***********************************************************************/
15021
15022 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15023 FORCE is non-zero, redisplay mode lines unconditionally.
15024 Otherwise, redisplay only mode lines that are garbaged. Value is
15025 the number of windows whose mode lines were redisplayed. */
15026
15027 static int
15028 redisplay_mode_lines (window, force)
15029 Lisp_Object window;
15030 int force;
15031 {
15032 int nwindows = 0;
15033
15034 while (!NILP (window))
15035 {
15036 struct window *w = XWINDOW (window);
15037
15038 if (WINDOWP (w->hchild))
15039 nwindows += redisplay_mode_lines (w->hchild, force);
15040 else if (WINDOWP (w->vchild))
15041 nwindows += redisplay_mode_lines (w->vchild, force);
15042 else if (force
15043 || FRAME_GARBAGED_P (XFRAME (w->frame))
15044 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15045 {
15046 struct text_pos lpoint;
15047 struct buffer *old = current_buffer;
15048
15049 /* Set the window's buffer for the mode line display. */
15050 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15051 set_buffer_internal_1 (XBUFFER (w->buffer));
15052
15053 /* Point refers normally to the selected window. For any
15054 other window, set up appropriate value. */
15055 if (!EQ (window, selected_window))
15056 {
15057 struct text_pos pt;
15058
15059 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15060 if (CHARPOS (pt) < BEGV)
15061 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15062 else if (CHARPOS (pt) > (ZV - 1))
15063 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15064 else
15065 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15066 }
15067
15068 /* Display mode lines. */
15069 clear_glyph_matrix (w->desired_matrix);
15070 if (display_mode_lines (w))
15071 {
15072 ++nwindows;
15073 w->must_be_updated_p = 1;
15074 }
15075
15076 /* Restore old settings. */
15077 set_buffer_internal_1 (old);
15078 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15079 }
15080
15081 window = w->next;
15082 }
15083
15084 return nwindows;
15085 }
15086
15087
15088 /* Display the mode and/or top line of window W. Value is the number
15089 of mode lines displayed. */
15090
15091 static int
15092 display_mode_lines (w)
15093 struct window *w;
15094 {
15095 Lisp_Object old_selected_window, old_selected_frame;
15096 int n = 0;
15097
15098 old_selected_frame = selected_frame;
15099 selected_frame = w->frame;
15100 old_selected_window = selected_window;
15101 XSETWINDOW (selected_window, w);
15102
15103 /* These will be set while the mode line specs are processed. */
15104 line_number_displayed = 0;
15105 w->column_number_displayed = Qnil;
15106
15107 if (WINDOW_WANTS_MODELINE_P (w))
15108 {
15109 struct window *sel_w = XWINDOW (old_selected_window);
15110
15111 /* Select mode line face based on the real selected window. */
15112 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15113 current_buffer->mode_line_format);
15114 ++n;
15115 }
15116
15117 if (WINDOW_WANTS_HEADER_LINE_P (w))
15118 {
15119 display_mode_line (w, HEADER_LINE_FACE_ID,
15120 current_buffer->header_line_format);
15121 ++n;
15122 }
15123
15124 selected_frame = old_selected_frame;
15125 selected_window = old_selected_window;
15126 return n;
15127 }
15128
15129
15130 /* Display mode or top line of window W. FACE_ID specifies which line
15131 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15132 FORMAT is the mode line format to display. Value is the pixel
15133 height of the mode line displayed. */
15134
15135 static int
15136 display_mode_line (w, face_id, format)
15137 struct window *w;
15138 enum face_id face_id;
15139 Lisp_Object format;
15140 {
15141 struct it it;
15142 struct face *face;
15143
15144 init_iterator (&it, w, -1, -1, NULL, face_id);
15145 prepare_desired_row (it.glyph_row);
15146
15147 it.glyph_row->mode_line_p = 1;
15148
15149 if (! mode_line_inverse_video)
15150 /* Force the mode-line to be displayed in the default face. */
15151 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15152
15153 /* Temporarily make frame's keyboard the current kboard so that
15154 kboard-local variables in the mode_line_format will get the right
15155 values. */
15156 push_frame_kboard (it.f);
15157 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15158 pop_frame_kboard ();
15159
15160 /* Fill up with spaces. */
15161 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15162
15163 compute_line_metrics (&it);
15164 it.glyph_row->full_width_p = 1;
15165 it.glyph_row->continued_p = 0;
15166 it.glyph_row->truncated_on_left_p = 0;
15167 it.glyph_row->truncated_on_right_p = 0;
15168
15169 /* Make a 3D mode-line have a shadow at its right end. */
15170 face = FACE_FROM_ID (it.f, face_id);
15171 extend_face_to_end_of_line (&it);
15172 if (face->box != FACE_NO_BOX)
15173 {
15174 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15175 + it.glyph_row->used[TEXT_AREA] - 1);
15176 last->right_box_line_p = 1;
15177 }
15178
15179 return it.glyph_row->height;
15180 }
15181
15182 /* Alist that caches the results of :propertize.
15183 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
15184 Lisp_Object mode_line_proptrans_alist;
15185
15186 /* List of strings making up the mode-line. */
15187 Lisp_Object mode_line_string_list;
15188
15189 /* Base face property when building propertized mode line string. */
15190 static Lisp_Object mode_line_string_face;
15191 static Lisp_Object mode_line_string_face_prop;
15192
15193
15194 /* Contribute ELT to the mode line for window IT->w. How it
15195 translates into text depends on its data type.
15196
15197 IT describes the display environment in which we display, as usual.
15198
15199 DEPTH is the depth in recursion. It is used to prevent
15200 infinite recursion here.
15201
15202 FIELD_WIDTH is the number of characters the display of ELT should
15203 occupy in the mode line, and PRECISION is the maximum number of
15204 characters to display from ELT's representation. See
15205 display_string for details.
15206
15207 Returns the hpos of the end of the text generated by ELT.
15208
15209 PROPS is a property list to add to any string we encounter.
15210
15211 If RISKY is nonzero, remove (disregard) any properties in any string
15212 we encounter, and ignore :eval and :propertize.
15213
15214 If the global variable `frame_title_ptr' is non-NULL, then the output
15215 is passed to `store_frame_title' instead of `display_string'. */
15216
15217 static int
15218 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15219 struct it *it;
15220 int depth;
15221 int field_width, precision;
15222 Lisp_Object elt, props;
15223 int risky;
15224 {
15225 int n = 0, field, prec;
15226 int literal = 0;
15227
15228 tail_recurse:
15229 if (depth > 100)
15230 elt = build_string ("*too-deep*");
15231
15232 depth++;
15233
15234 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15235 {
15236 case Lisp_String:
15237 {
15238 /* A string: output it and check for %-constructs within it. */
15239 unsigned char c;
15240 const unsigned char *this, *lisp_string;
15241
15242 if (!NILP (props) || risky)
15243 {
15244 Lisp_Object oprops, aelt;
15245 oprops = Ftext_properties_at (make_number (0), elt);
15246
15247 if (NILP (Fequal (props, oprops)) || risky)
15248 {
15249 /* If the starting string has properties,
15250 merge the specified ones onto the existing ones. */
15251 if (! NILP (oprops) && !risky)
15252 {
15253 Lisp_Object tem;
15254
15255 oprops = Fcopy_sequence (oprops);
15256 tem = props;
15257 while (CONSP (tem))
15258 {
15259 oprops = Fplist_put (oprops, XCAR (tem),
15260 XCAR (XCDR (tem)));
15261 tem = XCDR (XCDR (tem));
15262 }
15263 props = oprops;
15264 }
15265
15266 aelt = Fassoc (elt, mode_line_proptrans_alist);
15267 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15268 {
15269 mode_line_proptrans_alist
15270 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15271 elt = XCAR (aelt);
15272 }
15273 else
15274 {
15275 Lisp_Object tem;
15276
15277 elt = Fcopy_sequence (elt);
15278 Fset_text_properties (make_number (0), Flength (elt),
15279 props, elt);
15280 /* Add this item to mode_line_proptrans_alist. */
15281 mode_line_proptrans_alist
15282 = Fcons (Fcons (elt, props),
15283 mode_line_proptrans_alist);
15284 /* Truncate mode_line_proptrans_alist
15285 to at most 50 elements. */
15286 tem = Fnthcdr (make_number (50),
15287 mode_line_proptrans_alist);
15288 if (! NILP (tem))
15289 XSETCDR (tem, Qnil);
15290 }
15291 }
15292 }
15293
15294 this = SDATA (elt);
15295 lisp_string = this;
15296
15297 if (literal)
15298 {
15299 prec = precision - n;
15300 if (frame_title_ptr)
15301 n += store_frame_title (SDATA (elt), -1, prec);
15302 else if (!NILP (mode_line_string_list))
15303 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15304 else
15305 n += display_string (NULL, elt, Qnil, 0, 0, it,
15306 0, prec, 0, STRING_MULTIBYTE (elt));
15307
15308 break;
15309 }
15310
15311 while ((precision <= 0 || n < precision)
15312 && *this
15313 && (frame_title_ptr
15314 || !NILP (mode_line_string_list)
15315 || it->current_x < it->last_visible_x))
15316 {
15317 const unsigned char *last = this;
15318
15319 /* Advance to end of string or next format specifier. */
15320 while ((c = *this++) != '\0' && c != '%')
15321 ;
15322
15323 if (this - 1 != last)
15324 {
15325 /* Output to end of string or up to '%'. Field width
15326 is length of string. Don't output more than
15327 PRECISION allows us. */
15328 --this;
15329
15330 prec = chars_in_text (last, this - last);
15331 if (precision > 0 && prec > precision - n)
15332 prec = precision - n;
15333
15334 if (frame_title_ptr)
15335 n += store_frame_title (last, 0, prec);
15336 else if (!NILP (mode_line_string_list))
15337 {
15338 int bytepos = last - lisp_string;
15339 int charpos = string_byte_to_char (elt, bytepos);
15340 n += store_mode_line_string (NULL,
15341 Fsubstring (elt, make_number (charpos),
15342 make_number (charpos + prec)),
15343 0, 0, 0, Qnil);
15344 }
15345 else
15346 {
15347 int bytepos = last - lisp_string;
15348 int charpos = string_byte_to_char (elt, bytepos);
15349 n += display_string (NULL, elt, Qnil, 0, charpos,
15350 it, 0, prec, 0,
15351 STRING_MULTIBYTE (elt));
15352 }
15353 }
15354 else /* c == '%' */
15355 {
15356 const unsigned char *percent_position = this;
15357
15358 /* Get the specified minimum width. Zero means
15359 don't pad. */
15360 field = 0;
15361 while ((c = *this++) >= '0' && c <= '9')
15362 field = field * 10 + c - '0';
15363
15364 /* Don't pad beyond the total padding allowed. */
15365 if (field_width - n > 0 && field > field_width - n)
15366 field = field_width - n;
15367
15368 /* Note that either PRECISION <= 0 or N < PRECISION. */
15369 prec = precision - n;
15370
15371 if (c == 'M')
15372 n += display_mode_element (it, depth, field, prec,
15373 Vglobal_mode_string, props,
15374 risky);
15375 else if (c != 0)
15376 {
15377 int multibyte;
15378 int bytepos, charpos;
15379 unsigned char *spec;
15380
15381 bytepos = percent_position - lisp_string;
15382 charpos = (STRING_MULTIBYTE (elt)
15383 ? string_byte_to_char (elt, bytepos)
15384 : bytepos);
15385
15386 spec
15387 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15388
15389 if (frame_title_ptr)
15390 n += store_frame_title (spec, field, prec);
15391 else if (!NILP (mode_line_string_list))
15392 {
15393 int len = strlen (spec);
15394 Lisp_Object tem = make_string (spec, len);
15395 props = Ftext_properties_at (make_number (charpos), elt);
15396 /* Should only keep face property in props */
15397 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15398 }
15399 else
15400 {
15401 int nglyphs_before, nwritten;
15402
15403 nglyphs_before = it->glyph_row->used[TEXT_AREA];
15404 nwritten = display_string (spec, Qnil, elt,
15405 charpos, 0, it,
15406 field, prec, 0,
15407 multibyte);
15408
15409 /* Assign to the glyphs written above the
15410 string where the `%x' came from, position
15411 of the `%'. */
15412 if (nwritten > 0)
15413 {
15414 struct glyph *glyph
15415 = (it->glyph_row->glyphs[TEXT_AREA]
15416 + nglyphs_before);
15417 int i;
15418
15419 for (i = 0; i < nwritten; ++i)
15420 {
15421 glyph[i].object = elt;
15422 glyph[i].charpos = charpos;
15423 }
15424
15425 n += nwritten;
15426 }
15427 }
15428 }
15429 else /* c == 0 */
15430 break;
15431 }
15432 }
15433 }
15434 break;
15435
15436 case Lisp_Symbol:
15437 /* A symbol: process the value of the symbol recursively
15438 as if it appeared here directly. Avoid error if symbol void.
15439 Special case: if value of symbol is a string, output the string
15440 literally. */
15441 {
15442 register Lisp_Object tem;
15443
15444 /* If the variable is not marked as risky to set
15445 then its contents are risky to use. */
15446 if (NILP (Fget (elt, Qrisky_local_variable)))
15447 risky = 1;
15448
15449 tem = Fboundp (elt);
15450 if (!NILP (tem))
15451 {
15452 tem = Fsymbol_value (elt);
15453 /* If value is a string, output that string literally:
15454 don't check for % within it. */
15455 if (STRINGP (tem))
15456 literal = 1;
15457
15458 if (!EQ (tem, elt))
15459 {
15460 /* Give up right away for nil or t. */
15461 elt = tem;
15462 goto tail_recurse;
15463 }
15464 }
15465 }
15466 break;
15467
15468 case Lisp_Cons:
15469 {
15470 register Lisp_Object car, tem;
15471
15472 /* A cons cell: five distinct cases.
15473 If first element is :eval or :propertize, do something special.
15474 If first element is a string or a cons, process all the elements
15475 and effectively concatenate them.
15476 If first element is a negative number, truncate displaying cdr to
15477 at most that many characters. If positive, pad (with spaces)
15478 to at least that many characters.
15479 If first element is a symbol, process the cadr or caddr recursively
15480 according to whether the symbol's value is non-nil or nil. */
15481 car = XCAR (elt);
15482 if (EQ (car, QCeval))
15483 {
15484 /* An element of the form (:eval FORM) means evaluate FORM
15485 and use the result as mode line elements. */
15486
15487 if (risky)
15488 break;
15489
15490 if (CONSP (XCDR (elt)))
15491 {
15492 Lisp_Object spec;
15493 spec = safe_eval (XCAR (XCDR (elt)));
15494 n += display_mode_element (it, depth, field_width - n,
15495 precision - n, spec, props,
15496 risky);
15497 }
15498 }
15499 else if (EQ (car, QCpropertize))
15500 {
15501 /* An element of the form (:propertize ELT PROPS...)
15502 means display ELT but applying properties PROPS. */
15503
15504 if (risky)
15505 break;
15506
15507 if (CONSP (XCDR (elt)))
15508 n += display_mode_element (it, depth, field_width - n,
15509 precision - n, XCAR (XCDR (elt)),
15510 XCDR (XCDR (elt)), risky);
15511 }
15512 else if (SYMBOLP (car))
15513 {
15514 tem = Fboundp (car);
15515 elt = XCDR (elt);
15516 if (!CONSP (elt))
15517 goto invalid;
15518 /* elt is now the cdr, and we know it is a cons cell.
15519 Use its car if CAR has a non-nil value. */
15520 if (!NILP (tem))
15521 {
15522 tem = Fsymbol_value (car);
15523 if (!NILP (tem))
15524 {
15525 elt = XCAR (elt);
15526 goto tail_recurse;
15527 }
15528 }
15529 /* Symbol's value is nil (or symbol is unbound)
15530 Get the cddr of the original list
15531 and if possible find the caddr and use that. */
15532 elt = XCDR (elt);
15533 if (NILP (elt))
15534 break;
15535 else if (!CONSP (elt))
15536 goto invalid;
15537 elt = XCAR (elt);
15538 goto tail_recurse;
15539 }
15540 else if (INTEGERP (car))
15541 {
15542 register int lim = XINT (car);
15543 elt = XCDR (elt);
15544 if (lim < 0)
15545 {
15546 /* Negative int means reduce maximum width. */
15547 if (precision <= 0)
15548 precision = -lim;
15549 else
15550 precision = min (precision, -lim);
15551 }
15552 else if (lim > 0)
15553 {
15554 /* Padding specified. Don't let it be more than
15555 current maximum. */
15556 if (precision > 0)
15557 lim = min (precision, lim);
15558
15559 /* If that's more padding than already wanted, queue it.
15560 But don't reduce padding already specified even if
15561 that is beyond the current truncation point. */
15562 field_width = max (lim, field_width);
15563 }
15564 goto tail_recurse;
15565 }
15566 else if (STRINGP (car) || CONSP (car))
15567 {
15568 register int limit = 50;
15569 /* Limit is to protect against circular lists. */
15570 while (CONSP (elt)
15571 && --limit > 0
15572 && (precision <= 0 || n < precision))
15573 {
15574 n += display_mode_element (it, depth, field_width - n,
15575 precision - n, XCAR (elt),
15576 props, risky);
15577 elt = XCDR (elt);
15578 }
15579 }
15580 }
15581 break;
15582
15583 default:
15584 invalid:
15585 elt = build_string ("*invalid*");
15586 goto tail_recurse;
15587 }
15588
15589 /* Pad to FIELD_WIDTH. */
15590 if (field_width > 0 && n < field_width)
15591 {
15592 if (frame_title_ptr)
15593 n += store_frame_title ("", field_width - n, 0);
15594 else if (!NILP (mode_line_string_list))
15595 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
15596 else
15597 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
15598 0, 0, 0);
15599 }
15600
15601 return n;
15602 }
15603
15604 /* Store a mode-line string element in mode_line_string_list.
15605
15606 If STRING is non-null, display that C string. Otherwise, the Lisp
15607 string LISP_STRING is displayed.
15608
15609 FIELD_WIDTH is the minimum number of output glyphs to produce.
15610 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15611 with spaces. FIELD_WIDTH <= 0 means don't pad.
15612
15613 PRECISION is the maximum number of characters to output from
15614 STRING. PRECISION <= 0 means don't truncate the string.
15615
15616 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
15617 properties to the string.
15618
15619 PROPS are the properties to add to the string.
15620 The mode_line_string_face face property is always added to the string.
15621 */
15622
15623 static int store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
15624 char *string;
15625 Lisp_Object lisp_string;
15626 int copy_string;
15627 int field_width;
15628 int precision;
15629 Lisp_Object props;
15630 {
15631 int len;
15632 int n = 0;
15633
15634 if (string != NULL)
15635 {
15636 len = strlen (string);
15637 if (precision > 0 && len > precision)
15638 len = precision;
15639 lisp_string = make_string (string, len);
15640 if (NILP (props))
15641 props = mode_line_string_face_prop;
15642 else if (!NILP (mode_line_string_face))
15643 {
15644 Lisp_Object face = Fplist_get (props, Qface);
15645 props = Fcopy_sequence (props);
15646 if (NILP (face))
15647 face = mode_line_string_face;
15648 else
15649 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15650 props = Fplist_put (props, Qface, face);
15651 }
15652 Fadd_text_properties (make_number (0), make_number (len),
15653 props, lisp_string);
15654 }
15655 else
15656 {
15657 len = XFASTINT (Flength (lisp_string));
15658 if (precision > 0 && len > precision)
15659 {
15660 len = precision;
15661 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
15662 precision = -1;
15663 }
15664 if (!NILP (mode_line_string_face))
15665 {
15666 Lisp_Object face;
15667 if (NILP (props))
15668 props = Ftext_properties_at (make_number (0), lisp_string);
15669 face = Fplist_get (props, Qface);
15670 if (NILP (face))
15671 face = mode_line_string_face;
15672 else
15673 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15674 props = Fcons (Qface, Fcons (face, Qnil));
15675 if (copy_string)
15676 lisp_string = Fcopy_sequence (lisp_string);
15677 }
15678 if (!NILP (props))
15679 Fadd_text_properties (make_number (0), make_number (len),
15680 props, lisp_string);
15681 }
15682
15683 if (len > 0)
15684 {
15685 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15686 n += len;
15687 }
15688
15689 if (field_width > len)
15690 {
15691 field_width -= len;
15692 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
15693 if (!NILP (props))
15694 Fadd_text_properties (make_number (0), make_number (field_width),
15695 props, lisp_string);
15696 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15697 n += field_width;
15698 }
15699
15700 return n;
15701 }
15702
15703
15704 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
15705 0, 3, 0,
15706 doc: /* Return the mode-line of selected window as a string.
15707 First optional arg FORMAT specifies a different format string (see
15708 `mode-line-format' for details) to use. If FORMAT is t, return
15709 the buffer's header-line. Second optional arg WINDOW specifies a
15710 different window to use as the context for the formatting.
15711 If third optional arg NO-PROPS is non-nil, string is not propertized. */)
15712 (format, window, no_props)
15713 Lisp_Object format, window, no_props;
15714 {
15715 struct it it;
15716 int len;
15717 struct window *w;
15718 struct buffer *old_buffer = NULL;
15719 enum face_id face_id = DEFAULT_FACE_ID;
15720
15721 if (NILP (window))
15722 window = selected_window;
15723 CHECK_WINDOW (window);
15724 w = XWINDOW (window);
15725 CHECK_BUFFER (w->buffer);
15726
15727 if (XBUFFER (w->buffer) != current_buffer)
15728 {
15729 old_buffer = current_buffer;
15730 set_buffer_internal_1 (XBUFFER (w->buffer));
15731 }
15732
15733 if (NILP (format) || EQ (format, Qt))
15734 {
15735 face_id = NILP (format)
15736 ? CURRENT_MODE_LINE_FACE_ID (w) :
15737 HEADER_LINE_FACE_ID;
15738 format = NILP (format)
15739 ? current_buffer->mode_line_format
15740 : current_buffer->header_line_format;
15741 }
15742
15743 init_iterator (&it, w, -1, -1, NULL, face_id);
15744
15745 if (NILP (no_props))
15746 {
15747 mode_line_string_face =
15748 (face_id == MODE_LINE_FACE_ID ? Qmode_line :
15749 face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive :
15750 face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
15751
15752 mode_line_string_face_prop =
15753 NILP (mode_line_string_face) ? Qnil :
15754 Fcons (Qface, Fcons (mode_line_string_face, Qnil));
15755
15756 /* We need a dummy last element in mode_line_string_list to
15757 indicate we are building the propertized mode-line string.
15758 Using mode_line_string_face_prop here GC protects it. */
15759 mode_line_string_list =
15760 Fcons (mode_line_string_face_prop, Qnil);
15761 frame_title_ptr = NULL;
15762 }
15763 else
15764 {
15765 mode_line_string_face_prop = Qnil;
15766 mode_line_string_list = Qnil;
15767 frame_title_ptr = frame_title_buf;
15768 }
15769
15770 push_frame_kboard (it.f);
15771 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15772 pop_frame_kboard ();
15773
15774 if (old_buffer)
15775 set_buffer_internal_1 (old_buffer);
15776
15777 if (NILP (no_props))
15778 {
15779 Lisp_Object str;
15780 mode_line_string_list = Fnreverse (mode_line_string_list);
15781 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
15782 make_string ("", 0));
15783 mode_line_string_face_prop = Qnil;
15784 mode_line_string_list = Qnil;
15785 return str;
15786 }
15787
15788 len = frame_title_ptr - frame_title_buf;
15789 if (len > 0 && frame_title_ptr[-1] == '-')
15790 {
15791 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
15792 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
15793 ;
15794 frame_title_ptr += 3; /* restore last non-dash + two dashes */
15795 if (len > frame_title_ptr - frame_title_buf)
15796 len = frame_title_ptr - frame_title_buf;
15797 }
15798
15799 frame_title_ptr = NULL;
15800 return make_string (frame_title_buf, len);
15801 }
15802
15803 /* Write a null-terminated, right justified decimal representation of
15804 the positive integer D to BUF using a minimal field width WIDTH. */
15805
15806 static void
15807 pint2str (buf, width, d)
15808 register char *buf;
15809 register int width;
15810 register int d;
15811 {
15812 register char *p = buf;
15813
15814 if (d <= 0)
15815 *p++ = '0';
15816 else
15817 {
15818 while (d > 0)
15819 {
15820 *p++ = d % 10 + '0';
15821 d /= 10;
15822 }
15823 }
15824
15825 for (width -= (int) (p - buf); width > 0; --width)
15826 *p++ = ' ';
15827 *p-- = '\0';
15828 while (p > buf)
15829 {
15830 d = *buf;
15831 *buf++ = *p;
15832 *p-- = d;
15833 }
15834 }
15835
15836 /* Write a null-terminated, right justified decimal and "human
15837 readable" representation of the nonnegative integer D to BUF using
15838 a minimal field width WIDTH. D should be smaller than 999.5e24. */
15839
15840 static const char power_letter[] =
15841 {
15842 0, /* not used */
15843 'k', /* kilo */
15844 'M', /* mega */
15845 'G', /* giga */
15846 'T', /* tera */
15847 'P', /* peta */
15848 'E', /* exa */
15849 'Z', /* zetta */
15850 'Y' /* yotta */
15851 };
15852
15853 static void
15854 pint2hrstr (buf, width, d)
15855 char *buf;
15856 int width;
15857 int d;
15858 {
15859 /* We aim to represent the nonnegative integer D as
15860 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
15861 int quotient = d;
15862 int remainder = 0;
15863 /* -1 means: do not use TENTHS. */
15864 int tenths = -1;
15865 int exponent = 0;
15866
15867 /* Length of QUOTIENT.TENTHS as a string. */
15868 int length;
15869
15870 char * psuffix;
15871 char * p;
15872
15873 if (1000 <= quotient)
15874 {
15875 /* Scale to the appropriate EXPONENT. */
15876 do
15877 {
15878 remainder = quotient % 1000;
15879 quotient /= 1000;
15880 exponent++;
15881 }
15882 while (1000 <= quotient);
15883
15884 /* Round to nearest and decide whether to use TENTHS or not. */
15885 if (quotient <= 9)
15886 {
15887 tenths = remainder / 100;
15888 if (50 <= remainder % 100)
15889 if (tenths < 9)
15890 tenths++;
15891 else
15892 {
15893 quotient++;
15894 if (quotient == 10)
15895 tenths = -1;
15896 else
15897 tenths = 0;
15898 }
15899 }
15900 else
15901 if (500 <= remainder)
15902 if (quotient < 999)
15903 quotient++;
15904 else
15905 {
15906 quotient = 1;
15907 exponent++;
15908 tenths = 0;
15909 }
15910 }
15911
15912 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
15913 if (tenths == -1 && quotient <= 99)
15914 if (quotient <= 9)
15915 length = 1;
15916 else
15917 length = 2;
15918 else
15919 length = 3;
15920 p = psuffix = buf + max (width, length);
15921
15922 /* Print EXPONENT. */
15923 if (exponent)
15924 *psuffix++ = power_letter[exponent];
15925 *psuffix = '\0';
15926
15927 /* Print TENTHS. */
15928 if (tenths >= 0)
15929 {
15930 *--p = '0' + tenths;
15931 *--p = '.';
15932 }
15933
15934 /* Print QUOTIENT. */
15935 do
15936 {
15937 int digit = quotient % 10;
15938 *--p = '0' + digit;
15939 }
15940 while ((quotient /= 10) != 0);
15941
15942 /* Print leading spaces. */
15943 while (buf < p)
15944 *--p = ' ';
15945 }
15946
15947 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
15948 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
15949 type of CODING_SYSTEM. Return updated pointer into BUF. */
15950
15951 static unsigned char invalid_eol_type[] = "(*invalid*)";
15952
15953 static char *
15954 decode_mode_spec_coding (coding_system, buf, eol_flag)
15955 Lisp_Object coding_system;
15956 register char *buf;
15957 int eol_flag;
15958 {
15959 Lisp_Object val;
15960 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
15961 const unsigned char *eol_str;
15962 int eol_str_len;
15963 /* The EOL conversion we are using. */
15964 Lisp_Object eoltype;
15965
15966 val = Fget (coding_system, Qcoding_system);
15967 eoltype = Qnil;
15968
15969 if (!VECTORP (val)) /* Not yet decided. */
15970 {
15971 if (multibyte)
15972 *buf++ = '-';
15973 if (eol_flag)
15974 eoltype = eol_mnemonic_undecided;
15975 /* Don't mention EOL conversion if it isn't decided. */
15976 }
15977 else
15978 {
15979 Lisp_Object eolvalue;
15980
15981 eolvalue = Fget (coding_system, Qeol_type);
15982
15983 if (multibyte)
15984 *buf++ = XFASTINT (AREF (val, 1));
15985
15986 if (eol_flag)
15987 {
15988 /* The EOL conversion that is normal on this system. */
15989
15990 if (NILP (eolvalue)) /* Not yet decided. */
15991 eoltype = eol_mnemonic_undecided;
15992 else if (VECTORP (eolvalue)) /* Not yet decided. */
15993 eoltype = eol_mnemonic_undecided;
15994 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
15995 eoltype = (XFASTINT (eolvalue) == 0
15996 ? eol_mnemonic_unix
15997 : (XFASTINT (eolvalue) == 1
15998 ? eol_mnemonic_dos : eol_mnemonic_mac));
15999 }
16000 }
16001
16002 if (eol_flag)
16003 {
16004 /* Mention the EOL conversion if it is not the usual one. */
16005 if (STRINGP (eoltype))
16006 {
16007 eol_str = SDATA (eoltype);
16008 eol_str_len = SBYTES (eoltype);
16009 }
16010 else if (INTEGERP (eoltype)
16011 && CHAR_VALID_P (XINT (eoltype), 0))
16012 {
16013 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16014 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16015 eol_str = tmp;
16016 }
16017 else
16018 {
16019 eol_str = invalid_eol_type;
16020 eol_str_len = sizeof (invalid_eol_type) - 1;
16021 }
16022 bcopy (eol_str, buf, eol_str_len);
16023 buf += eol_str_len;
16024 }
16025
16026 return buf;
16027 }
16028
16029 /* Return a string for the output of a mode line %-spec for window W,
16030 generated by character C. PRECISION >= 0 means don't return a
16031 string longer than that value. FIELD_WIDTH > 0 means pad the
16032 string returned with spaces to that value. Return 1 in *MULTIBYTE
16033 if the result is multibyte text. */
16034
16035 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16036
16037 static char *
16038 decode_mode_spec (w, c, field_width, precision, multibyte)
16039 struct window *w;
16040 register int c;
16041 int field_width, precision;
16042 int *multibyte;
16043 {
16044 Lisp_Object obj;
16045 struct frame *f = XFRAME (WINDOW_FRAME (w));
16046 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16047 struct buffer *b = XBUFFER (w->buffer);
16048
16049 obj = Qnil;
16050 *multibyte = 0;
16051
16052 switch (c)
16053 {
16054 case '*':
16055 if (!NILP (b->read_only))
16056 return "%";
16057 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16058 return "*";
16059 return "-";
16060
16061 case '+':
16062 /* This differs from %* only for a modified read-only buffer. */
16063 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16064 return "*";
16065 if (!NILP (b->read_only))
16066 return "%";
16067 return "-";
16068
16069 case '&':
16070 /* This differs from %* in ignoring read-only-ness. */
16071 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16072 return "*";
16073 return "-";
16074
16075 case '%':
16076 return "%";
16077
16078 case '[':
16079 {
16080 int i;
16081 char *p;
16082
16083 if (command_loop_level > 5)
16084 return "[[[... ";
16085 p = decode_mode_spec_buf;
16086 for (i = 0; i < command_loop_level; i++)
16087 *p++ = '[';
16088 *p = 0;
16089 return decode_mode_spec_buf;
16090 }
16091
16092 case ']':
16093 {
16094 int i;
16095 char *p;
16096
16097 if (command_loop_level > 5)
16098 return " ...]]]";
16099 p = decode_mode_spec_buf;
16100 for (i = 0; i < command_loop_level; i++)
16101 *p++ = ']';
16102 *p = 0;
16103 return decode_mode_spec_buf;
16104 }
16105
16106 case '-':
16107 {
16108 register int i;
16109
16110 /* Let lots_of_dashes be a string of infinite length. */
16111 if (!NILP (mode_line_string_list))
16112 return "--";
16113 if (field_width <= 0
16114 || field_width > sizeof (lots_of_dashes))
16115 {
16116 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16117 decode_mode_spec_buf[i] = '-';
16118 decode_mode_spec_buf[i] = '\0';
16119 return decode_mode_spec_buf;
16120 }
16121 else
16122 return lots_of_dashes;
16123 }
16124
16125 case 'b':
16126 obj = b->name;
16127 break;
16128
16129 case 'c':
16130 {
16131 int col = (int) current_column (); /* iftc */
16132 w->column_number_displayed = make_number (col);
16133 pint2str (decode_mode_spec_buf, field_width, col);
16134 return decode_mode_spec_buf;
16135 }
16136
16137 case 'F':
16138 /* %F displays the frame name. */
16139 if (!NILP (f->title))
16140 return (char *) SDATA (f->title);
16141 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16142 return (char *) SDATA (f->name);
16143 return "Emacs";
16144
16145 case 'f':
16146 obj = b->filename;
16147 break;
16148
16149 case 'i':
16150 {
16151 int size = ZV - BEGV;
16152 pint2str (decode_mode_spec_buf, field_width, size);
16153 return decode_mode_spec_buf;
16154 }
16155
16156 case 'I':
16157 {
16158 int size = ZV - BEGV;
16159 pint2hrstr (decode_mode_spec_buf, field_width, size);
16160 return decode_mode_spec_buf;
16161 }
16162
16163 case 'l':
16164 {
16165 int startpos = XMARKER (w->start)->charpos;
16166 int startpos_byte = marker_byte_position (w->start);
16167 int line, linepos, linepos_byte, topline;
16168 int nlines, junk;
16169 int height = WINDOW_TOTAL_LINES (w);
16170
16171 /* If we decided that this buffer isn't suitable for line numbers,
16172 don't forget that too fast. */
16173 if (EQ (w->base_line_pos, w->buffer))
16174 goto no_value;
16175 /* But do forget it, if the window shows a different buffer now. */
16176 else if (BUFFERP (w->base_line_pos))
16177 w->base_line_pos = Qnil;
16178
16179 /* If the buffer is very big, don't waste time. */
16180 if (INTEGERP (Vline_number_display_limit)
16181 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16182 {
16183 w->base_line_pos = Qnil;
16184 w->base_line_number = Qnil;
16185 goto no_value;
16186 }
16187
16188 if (!NILP (w->base_line_number)
16189 && !NILP (w->base_line_pos)
16190 && XFASTINT (w->base_line_pos) <= startpos)
16191 {
16192 line = XFASTINT (w->base_line_number);
16193 linepos = XFASTINT (w->base_line_pos);
16194 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16195 }
16196 else
16197 {
16198 line = 1;
16199 linepos = BUF_BEGV (b);
16200 linepos_byte = BUF_BEGV_BYTE (b);
16201 }
16202
16203 /* Count lines from base line to window start position. */
16204 nlines = display_count_lines (linepos, linepos_byte,
16205 startpos_byte,
16206 startpos, &junk);
16207
16208 topline = nlines + line;
16209
16210 /* Determine a new base line, if the old one is too close
16211 or too far away, or if we did not have one.
16212 "Too close" means it's plausible a scroll-down would
16213 go back past it. */
16214 if (startpos == BUF_BEGV (b))
16215 {
16216 w->base_line_number = make_number (topline);
16217 w->base_line_pos = make_number (BUF_BEGV (b));
16218 }
16219 else if (nlines < height + 25 || nlines > height * 3 + 50
16220 || linepos == BUF_BEGV (b))
16221 {
16222 int limit = BUF_BEGV (b);
16223 int limit_byte = BUF_BEGV_BYTE (b);
16224 int position;
16225 int distance = (height * 2 + 30) * line_number_display_limit_width;
16226
16227 if (startpos - distance > limit)
16228 {
16229 limit = startpos - distance;
16230 limit_byte = CHAR_TO_BYTE (limit);
16231 }
16232
16233 nlines = display_count_lines (startpos, startpos_byte,
16234 limit_byte,
16235 - (height * 2 + 30),
16236 &position);
16237 /* If we couldn't find the lines we wanted within
16238 line_number_display_limit_width chars per line,
16239 give up on line numbers for this window. */
16240 if (position == limit_byte && limit == startpos - distance)
16241 {
16242 w->base_line_pos = w->buffer;
16243 w->base_line_number = Qnil;
16244 goto no_value;
16245 }
16246
16247 w->base_line_number = make_number (topline - nlines);
16248 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
16249 }
16250
16251 /* Now count lines from the start pos to point. */
16252 nlines = display_count_lines (startpos, startpos_byte,
16253 PT_BYTE, PT, &junk);
16254
16255 /* Record that we did display the line number. */
16256 line_number_displayed = 1;
16257
16258 /* Make the string to show. */
16259 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
16260 return decode_mode_spec_buf;
16261 no_value:
16262 {
16263 char* p = decode_mode_spec_buf;
16264 int pad = field_width - 2;
16265 while (pad-- > 0)
16266 *p++ = ' ';
16267 *p++ = '?';
16268 *p++ = '?';
16269 *p = '\0';
16270 return decode_mode_spec_buf;
16271 }
16272 }
16273 break;
16274
16275 case 'm':
16276 obj = b->mode_name;
16277 break;
16278
16279 case 'n':
16280 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
16281 return " Narrow";
16282 break;
16283
16284 case 'p':
16285 {
16286 int pos = marker_position (w->start);
16287 int total = BUF_ZV (b) - BUF_BEGV (b);
16288
16289 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
16290 {
16291 if (pos <= BUF_BEGV (b))
16292 return "All";
16293 else
16294 return "Bottom";
16295 }
16296 else if (pos <= BUF_BEGV (b))
16297 return "Top";
16298 else
16299 {
16300 if (total > 1000000)
16301 /* Do it differently for a large value, to avoid overflow. */
16302 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16303 else
16304 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
16305 /* We can't normally display a 3-digit number,
16306 so get us a 2-digit number that is close. */
16307 if (total == 100)
16308 total = 99;
16309 sprintf (decode_mode_spec_buf, "%2d%%", total);
16310 return decode_mode_spec_buf;
16311 }
16312 }
16313
16314 /* Display percentage of size above the bottom of the screen. */
16315 case 'P':
16316 {
16317 int toppos = marker_position (w->start);
16318 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16319 int total = BUF_ZV (b) - BUF_BEGV (b);
16320
16321 if (botpos >= BUF_ZV (b))
16322 {
16323 if (toppos <= BUF_BEGV (b))
16324 return "All";
16325 else
16326 return "Bottom";
16327 }
16328 else
16329 {
16330 if (total > 1000000)
16331 /* Do it differently for a large value, to avoid overflow. */
16332 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16333 else
16334 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
16335 /* We can't normally display a 3-digit number,
16336 so get us a 2-digit number that is close. */
16337 if (total == 100)
16338 total = 99;
16339 if (toppos <= BUF_BEGV (b))
16340 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
16341 else
16342 sprintf (decode_mode_spec_buf, "%2d%%", total);
16343 return decode_mode_spec_buf;
16344 }
16345 }
16346
16347 case 's':
16348 /* status of process */
16349 obj = Fget_buffer_process (w->buffer);
16350 if (NILP (obj))
16351 return "no process";
16352 #ifdef subprocesses
16353 obj = Fsymbol_name (Fprocess_status (obj));
16354 #endif
16355 break;
16356
16357 case 't': /* indicate TEXT or BINARY */
16358 #ifdef MODE_LINE_BINARY_TEXT
16359 return MODE_LINE_BINARY_TEXT (b);
16360 #else
16361 return "T";
16362 #endif
16363
16364 case 'z':
16365 /* coding-system (not including end-of-line format) */
16366 case 'Z':
16367 /* coding-system (including end-of-line type) */
16368 {
16369 int eol_flag = (c == 'Z');
16370 char *p = decode_mode_spec_buf;
16371
16372 if (! FRAME_WINDOW_P (f))
16373 {
16374 /* No need to mention EOL here--the terminal never needs
16375 to do EOL conversion. */
16376 p = decode_mode_spec_coding (FRAME_KEYBOARD_CODING (f)->symbol, p, 0);
16377 p = decode_mode_spec_coding (FRAME_TERMINAL_CODING (f)->symbol, p, 0);
16378 }
16379 p = decode_mode_spec_coding (b->buffer_file_coding_system,
16380 p, eol_flag);
16381
16382 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
16383 #ifdef subprocesses
16384 obj = Fget_buffer_process (Fcurrent_buffer ());
16385 if (PROCESSP (obj))
16386 {
16387 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16388 p, eol_flag);
16389 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16390 p, eol_flag);
16391 }
16392 #endif /* subprocesses */
16393 #endif /* 0 */
16394 *p = 0;
16395 return decode_mode_spec_buf;
16396 }
16397 }
16398
16399 if (STRINGP (obj))
16400 {
16401 *multibyte = STRING_MULTIBYTE (obj);
16402 return (char *) SDATA (obj);
16403 }
16404 else
16405 return "";
16406 }
16407
16408
16409 /* Count up to COUNT lines starting from START / START_BYTE.
16410 But don't go beyond LIMIT_BYTE.
16411 Return the number of lines thus found (always nonnegative).
16412
16413 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
16414
16415 static int
16416 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16417 int start, start_byte, limit_byte, count;
16418 int *byte_pos_ptr;
16419 {
16420 register unsigned char *cursor;
16421 unsigned char *base;
16422
16423 register int ceiling;
16424 register unsigned char *ceiling_addr;
16425 int orig_count = count;
16426
16427 /* If we are not in selective display mode,
16428 check only for newlines. */
16429 int selective_display = (!NILP (current_buffer->selective_display)
16430 && !INTEGERP (current_buffer->selective_display));
16431
16432 if (count > 0)
16433 {
16434 while (start_byte < limit_byte)
16435 {
16436 ceiling = BUFFER_CEILING_OF (start_byte);
16437 ceiling = min (limit_byte - 1, ceiling);
16438 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16439 base = (cursor = BYTE_POS_ADDR (start_byte));
16440 while (1)
16441 {
16442 if (selective_display)
16443 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16444 ;
16445 else
16446 while (*cursor != '\n' && ++cursor != ceiling_addr)
16447 ;
16448
16449 if (cursor != ceiling_addr)
16450 {
16451 if (--count == 0)
16452 {
16453 start_byte += cursor - base + 1;
16454 *byte_pos_ptr = start_byte;
16455 return orig_count;
16456 }
16457 else
16458 if (++cursor == ceiling_addr)
16459 break;
16460 }
16461 else
16462 break;
16463 }
16464 start_byte += cursor - base;
16465 }
16466 }
16467 else
16468 {
16469 while (start_byte > limit_byte)
16470 {
16471 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16472 ceiling = max (limit_byte, ceiling);
16473 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16474 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16475 while (1)
16476 {
16477 if (selective_display)
16478 while (--cursor != ceiling_addr
16479 && *cursor != '\n' && *cursor != 015)
16480 ;
16481 else
16482 while (--cursor != ceiling_addr && *cursor != '\n')
16483 ;
16484
16485 if (cursor != ceiling_addr)
16486 {
16487 if (++count == 0)
16488 {
16489 start_byte += cursor - base + 1;
16490 *byte_pos_ptr = start_byte;
16491 /* When scanning backwards, we should
16492 not count the newline posterior to which we stop. */
16493 return - orig_count - 1;
16494 }
16495 }
16496 else
16497 break;
16498 }
16499 /* Here we add 1 to compensate for the last decrement
16500 of CURSOR, which took it past the valid range. */
16501 start_byte += cursor - base + 1;
16502 }
16503 }
16504
16505 *byte_pos_ptr = limit_byte;
16506
16507 if (count < 0)
16508 return - orig_count + count;
16509 return orig_count - count;
16510
16511 }
16512
16513
16514 \f
16515 /***********************************************************************
16516 Displaying strings
16517 ***********************************************************************/
16518
16519 /* Display a NUL-terminated string, starting with index START.
16520
16521 If STRING is non-null, display that C string. Otherwise, the Lisp
16522 string LISP_STRING is displayed.
16523
16524 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16525 FACE_STRING. Display STRING or LISP_STRING with the face at
16526 FACE_STRING_POS in FACE_STRING:
16527
16528 Display the string in the environment given by IT, but use the
16529 standard display table, temporarily.
16530
16531 FIELD_WIDTH is the minimum number of output glyphs to produce.
16532 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16533 with spaces. If STRING has more characters, more than FIELD_WIDTH
16534 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
16535
16536 PRECISION is the maximum number of characters to output from
16537 STRING. PRECISION < 0 means don't truncate the string.
16538
16539 This is roughly equivalent to printf format specifiers:
16540
16541 FIELD_WIDTH PRECISION PRINTF
16542 ----------------------------------------
16543 -1 -1 %s
16544 -1 10 %.10s
16545 10 -1 %10s
16546 20 10 %20.10s
16547
16548 MULTIBYTE zero means do not display multibyte chars, > 0 means do
16549 display them, and < 0 means obey the current buffer's value of
16550 enable_multibyte_characters.
16551
16552 Value is the number of glyphs produced. */
16553
16554 static int
16555 display_string (string, lisp_string, face_string, face_string_pos,
16556 start, it, field_width, precision, max_x, multibyte)
16557 unsigned char *string;
16558 Lisp_Object lisp_string;
16559 Lisp_Object face_string;
16560 int face_string_pos;
16561 int start;
16562 struct it *it;
16563 int field_width, precision, max_x;
16564 int multibyte;
16565 {
16566 int hpos_at_start = it->hpos;
16567 int saved_face_id = it->face_id;
16568 struct glyph_row *row = it->glyph_row;
16569
16570 /* Initialize the iterator IT for iteration over STRING beginning
16571 with index START. */
16572 reseat_to_string (it, string, lisp_string, start,
16573 precision, field_width, multibyte);
16574
16575 /* If displaying STRING, set up the face of the iterator
16576 from LISP_STRING, if that's given. */
16577 if (STRINGP (face_string))
16578 {
16579 int endptr;
16580 struct face *face;
16581
16582 it->face_id
16583 = face_at_string_position (it->w, face_string, face_string_pos,
16584 0, it->region_beg_charpos,
16585 it->region_end_charpos,
16586 &endptr, it->base_face_id, 0);
16587 face = FACE_FROM_ID (it->f, it->face_id);
16588 it->face_box_p = face->box != FACE_NO_BOX;
16589 }
16590
16591 /* Set max_x to the maximum allowed X position. Don't let it go
16592 beyond the right edge of the window. */
16593 if (max_x <= 0)
16594 max_x = it->last_visible_x;
16595 else
16596 max_x = min (max_x, it->last_visible_x);
16597
16598 /* Skip over display elements that are not visible. because IT->w is
16599 hscrolled. */
16600 if (it->current_x < it->first_visible_x)
16601 move_it_in_display_line_to (it, 100000, it->first_visible_x,
16602 MOVE_TO_POS | MOVE_TO_X);
16603
16604 row->ascent = it->max_ascent;
16605 row->height = it->max_ascent + it->max_descent;
16606 row->phys_ascent = it->max_phys_ascent;
16607 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16608
16609 /* This condition is for the case that we are called with current_x
16610 past last_visible_x. */
16611 while (it->current_x < max_x)
16612 {
16613 int x_before, x, n_glyphs_before, i, nglyphs;
16614
16615 /* Get the next display element. */
16616 if (!get_next_display_element (it))
16617 break;
16618
16619 /* Produce glyphs. */
16620 x_before = it->current_x;
16621 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
16622 PRODUCE_GLYPHS (it);
16623
16624 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
16625 i = 0;
16626 x = x_before;
16627 while (i < nglyphs)
16628 {
16629 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16630
16631 if (!it->truncate_lines_p
16632 && x + glyph->pixel_width > max_x)
16633 {
16634 /* End of continued line or max_x reached. */
16635 if (CHAR_GLYPH_PADDING_P (*glyph))
16636 {
16637 /* A wide character is unbreakable. */
16638 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
16639 it->current_x = x_before;
16640 }
16641 else
16642 {
16643 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
16644 it->current_x = x;
16645 }
16646 break;
16647 }
16648 else if (x + glyph->pixel_width > it->first_visible_x)
16649 {
16650 /* Glyph is at least partially visible. */
16651 ++it->hpos;
16652 if (x < it->first_visible_x)
16653 it->glyph_row->x = x - it->first_visible_x;
16654 }
16655 else
16656 {
16657 /* Glyph is off the left margin of the display area.
16658 Should not happen. */
16659 abort ();
16660 }
16661
16662 row->ascent = max (row->ascent, it->max_ascent);
16663 row->height = max (row->height, it->max_ascent + it->max_descent);
16664 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16665 row->phys_height = max (row->phys_height,
16666 it->max_phys_ascent + it->max_phys_descent);
16667 x += glyph->pixel_width;
16668 ++i;
16669 }
16670
16671 /* Stop if max_x reached. */
16672 if (i < nglyphs)
16673 break;
16674
16675 /* Stop at line ends. */
16676 if (ITERATOR_AT_END_OF_LINE_P (it))
16677 {
16678 it->continuation_lines_width = 0;
16679 break;
16680 }
16681
16682 set_iterator_to_next (it, 1);
16683
16684 /* Stop if truncating at the right edge. */
16685 if (it->truncate_lines_p
16686 && it->current_x >= it->last_visible_x)
16687 {
16688 /* Add truncation mark, but don't do it if the line is
16689 truncated at a padding space. */
16690 if (IT_CHARPOS (*it) < it->string_nchars)
16691 {
16692 if (!FRAME_WINDOW_P (it->f))
16693 {
16694 int i, n;
16695
16696 if (it->current_x > it->last_visible_x)
16697 {
16698 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16699 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16700 break;
16701 for (n = row->used[TEXT_AREA]; i < n; ++i)
16702 {
16703 row->used[TEXT_AREA] = i;
16704 produce_special_glyphs (it, IT_TRUNCATION);
16705 }
16706 }
16707 produce_special_glyphs (it, IT_TRUNCATION);
16708 }
16709 it->glyph_row->truncated_on_right_p = 1;
16710 }
16711 break;
16712 }
16713 }
16714
16715 /* Maybe insert a truncation at the left. */
16716 if (it->first_visible_x
16717 && IT_CHARPOS (*it) > 0)
16718 {
16719 if (!FRAME_WINDOW_P (it->f))
16720 insert_left_trunc_glyphs (it);
16721 it->glyph_row->truncated_on_left_p = 1;
16722 }
16723
16724 it->face_id = saved_face_id;
16725
16726 /* Value is number of columns displayed. */
16727 return it->hpos - hpos_at_start;
16728 }
16729
16730
16731 \f
16732 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
16733 appears as an element of LIST or as the car of an element of LIST.
16734 If PROPVAL is a list, compare each element against LIST in that
16735 way, and return 1/2 if any element of PROPVAL is found in LIST.
16736 Otherwise return 0. This function cannot quit.
16737 The return value is 2 if the text is invisible but with an ellipsis
16738 and 1 if it's invisible and without an ellipsis. */
16739
16740 int
16741 invisible_p (propval, list)
16742 register Lisp_Object propval;
16743 Lisp_Object list;
16744 {
16745 register Lisp_Object tail, proptail;
16746
16747 for (tail = list; CONSP (tail); tail = XCDR (tail))
16748 {
16749 register Lisp_Object tem;
16750 tem = XCAR (tail);
16751 if (EQ (propval, tem))
16752 return 1;
16753 if (CONSP (tem) && EQ (propval, XCAR (tem)))
16754 return NILP (XCDR (tem)) ? 1 : 2;
16755 }
16756
16757 if (CONSP (propval))
16758 {
16759 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
16760 {
16761 Lisp_Object propelt;
16762 propelt = XCAR (proptail);
16763 for (tail = list; CONSP (tail); tail = XCDR (tail))
16764 {
16765 register Lisp_Object tem;
16766 tem = XCAR (tail);
16767 if (EQ (propelt, tem))
16768 return 1;
16769 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
16770 return NILP (XCDR (tem)) ? 1 : 2;
16771 }
16772 }
16773 }
16774
16775 return 0;
16776 }
16777
16778 /* Calculate a width or height in pixels from a specification using
16779 the following elements:
16780
16781 SPEC ::=
16782 NUM - a (fractional) multiple of the default font width/height
16783 (NUM) - specifies exactly NUM pixels
16784 UNIT - a fixed number of pixels, see below.
16785 ELEMENT - size of a display element in pixels, see below.
16786 (NUM . SPEC) - equals NUM * SPEC
16787 (+ SPEC SPEC ...) - add pixel values
16788 (- SPEC SPEC ...) - subtract pixel values
16789 (- SPEC) - negate pixel value
16790
16791 NUM ::=
16792 INT or FLOAT - a number constant
16793 SYMBOL - use symbol's (buffer local) variable binding.
16794
16795 UNIT ::=
16796 in - pixels per inch *)
16797 mm - pixels per 1/1000 meter *)
16798 cm - pixels per 1/100 meter *)
16799 width - width of current font in pixels.
16800 height - height of current font in pixels.
16801
16802 *) using the ratio(s) defined in display-pixels-per-inch.
16803
16804 ELEMENT ::=
16805
16806 left-fringe - left fringe width in pixels
16807 right-fringe - right fringe width in pixels
16808
16809 left-margin - left margin width in pixels
16810 right-margin - right margin width in pixels
16811
16812 scroll-bar - scroll-bar area width in pixels
16813
16814 Examples:
16815
16816 Pixels corresponding to 5 inches:
16817 (5 . in)
16818
16819 Total width of non-text areas on left side of window (if scroll-bar is on left):
16820 '(space :width (+ left-fringe left-margin scroll-bar))
16821
16822 Align to first text column (in header line):
16823 '(space :align-to 0)
16824
16825 Align to middle of text area minus half the width of variable `my-image'
16826 containing a loaded image:
16827 '(space :align-to (0.5 . (- text my-image)))
16828
16829 Width of left margin minus width of 1 character in the default font:
16830 '(space :width (- left-margin 1))
16831
16832 Width of left margin minus width of 2 characters in the current font:
16833 '(space :width (- left-margin (2 . width)))
16834
16835 Center 1 character over left-margin (in header line):
16836 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
16837
16838 Different ways to express width of left fringe plus left margin minus one pixel:
16839 '(space :width (- (+ left-fringe left-margin) (1)))
16840 '(space :width (+ left-fringe left-margin (- (1))))
16841 '(space :width (+ left-fringe left-margin (-1)))
16842
16843 */
16844
16845 #define NUMVAL(X) \
16846 ((INTEGERP (X) || FLOATP (X)) \
16847 ? XFLOATINT (X) \
16848 : - 1)
16849
16850 int
16851 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
16852 double *res;
16853 struct it *it;
16854 Lisp_Object prop;
16855 void *font;
16856 int width_p, *align_to;
16857 {
16858 double pixels;
16859
16860 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
16861 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
16862
16863 if (NILP (prop))
16864 return OK_PIXELS (0);
16865
16866 if (SYMBOLP (prop))
16867 {
16868 if (SCHARS (SYMBOL_NAME (prop)) == 2)
16869 {
16870 char *unit = SDATA (SYMBOL_NAME (prop));
16871
16872 if (unit[0] == 'i' && unit[1] == 'n')
16873 pixels = 1.0;
16874 else if (unit[0] == 'm' && unit[1] == 'm')
16875 pixels = 25.4;
16876 else if (unit[0] == 'c' && unit[1] == 'm')
16877 pixels = 2.54;
16878 else
16879 pixels = 0;
16880 if (pixels > 0)
16881 {
16882 double ppi;
16883 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
16884 || (CONSP (Vdisplay_pixels_per_inch)
16885 && (ppi = (width_p
16886 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
16887 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
16888 ppi > 0)))
16889 return OK_PIXELS (ppi / pixels);
16890
16891 return 0;
16892 }
16893 }
16894
16895 #ifdef HAVE_WINDOW_SYSTEM
16896 if (EQ (prop, Qheight))
16897 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
16898 if (EQ (prop, Qwidth))
16899 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
16900 #else
16901 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
16902 return OK_PIXELS (1);
16903 #endif
16904
16905 if (EQ (prop, Qtext))
16906 return OK_PIXELS (width_p
16907 ? window_box_width (it->w, TEXT_AREA)
16908 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
16909
16910 if (align_to && *align_to < 0)
16911 {
16912 *res = 0;
16913 if (EQ (prop, Qleft))
16914 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
16915 if (EQ (prop, Qright))
16916 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
16917 if (EQ (prop, Qcenter))
16918 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
16919 + window_box_width (it->w, TEXT_AREA) / 2);
16920 if (EQ (prop, Qleft_fringe))
16921 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
16922 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
16923 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
16924 if (EQ (prop, Qright_fringe))
16925 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
16926 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
16927 : window_box_right_offset (it->w, TEXT_AREA));
16928 if (EQ (prop, Qleft_margin))
16929 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
16930 if (EQ (prop, Qright_margin))
16931 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
16932 if (EQ (prop, Qscroll_bar))
16933 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
16934 ? 0
16935 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
16936 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
16937 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
16938 : 0)));
16939 }
16940 else
16941 {
16942 if (EQ (prop, Qleft_fringe))
16943 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
16944 if (EQ (prop, Qright_fringe))
16945 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
16946 if (EQ (prop, Qleft_margin))
16947 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
16948 if (EQ (prop, Qright_margin))
16949 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
16950 if (EQ (prop, Qscroll_bar))
16951 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
16952 }
16953
16954 prop = Fbuffer_local_value (prop, it->w->buffer);
16955 }
16956
16957 if (INTEGERP (prop) || FLOATP (prop))
16958 {
16959 int base_unit = (width_p
16960 ? FRAME_COLUMN_WIDTH (it->f)
16961 : FRAME_LINE_HEIGHT (it->f));
16962 return OK_PIXELS (XFLOATINT (prop) * base_unit);
16963 }
16964
16965 if (CONSP (prop))
16966 {
16967 Lisp_Object car = XCAR (prop);
16968 Lisp_Object cdr = XCDR (prop);
16969
16970 if (SYMBOLP (car))
16971 {
16972 #ifdef HAVE_WINDOW_SYSTEM
16973 if (valid_image_p (prop))
16974 {
16975 int id = lookup_image (it->f, prop);
16976 struct image *img = IMAGE_FROM_ID (it->f, id);
16977
16978 return OK_PIXELS (width_p ? img->width : img->height);
16979 }
16980 #endif
16981 if (EQ (car, Qplus) || EQ (car, Qminus))
16982 {
16983 int first = 1;
16984 double px;
16985
16986 pixels = 0;
16987 while (CONSP (cdr))
16988 {
16989 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
16990 font, width_p, align_to))
16991 return 0;
16992 if (first)
16993 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
16994 else
16995 pixels += px;
16996 cdr = XCDR (cdr);
16997 }
16998 if (EQ (car, Qminus))
16999 pixels = -pixels;
17000 return OK_PIXELS (pixels);
17001 }
17002
17003 car = Fbuffer_local_value (car, it->w->buffer);
17004 }
17005
17006 if (INTEGERP (car) || FLOATP (car))
17007 {
17008 double fact;
17009 pixels = XFLOATINT (car);
17010 if (NILP (cdr))
17011 return OK_PIXELS (pixels);
17012 if (calc_pixel_width_or_height (&fact, it, cdr,
17013 font, width_p, align_to))
17014 return OK_PIXELS (pixels * fact);
17015 return 0;
17016 }
17017
17018 return 0;
17019 }
17020
17021 return 0;
17022 }
17023
17024 \f
17025 /***********************************************************************
17026 Glyph Display
17027 ***********************************************************************/
17028
17029 #ifdef HAVE_WINDOW_SYSTEM
17030
17031 #if GLYPH_DEBUG
17032
17033 void
17034 dump_glyph_string (s)
17035 struct glyph_string *s;
17036 {
17037 fprintf (stderr, "glyph string\n");
17038 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17039 s->x, s->y, s->width, s->height);
17040 fprintf (stderr, " ybase = %d\n", s->ybase);
17041 fprintf (stderr, " hl = %d\n", s->hl);
17042 fprintf (stderr, " left overhang = %d, right = %d\n",
17043 s->left_overhang, s->right_overhang);
17044 fprintf (stderr, " nchars = %d\n", s->nchars);
17045 fprintf (stderr, " extends to end of line = %d\n",
17046 s->extends_to_end_of_line_p);
17047 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17048 fprintf (stderr, " bg width = %d\n", s->background_width);
17049 }
17050
17051 #endif /* GLYPH_DEBUG */
17052
17053 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17054 of XChar2b structures for S; it can't be allocated in
17055 init_glyph_string because it must be allocated via `alloca'. W
17056 is the window on which S is drawn. ROW and AREA are the glyph row
17057 and area within the row from which S is constructed. START is the
17058 index of the first glyph structure covered by S. HL is a
17059 face-override for drawing S. */
17060
17061 #ifdef HAVE_NTGUI
17062 #define OPTIONAL_HDC(hdc) hdc,
17063 #define DECLARE_HDC(hdc) HDC hdc;
17064 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17065 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17066 #endif
17067
17068 #ifndef OPTIONAL_HDC
17069 #define OPTIONAL_HDC(hdc)
17070 #define DECLARE_HDC(hdc)
17071 #define ALLOCATE_HDC(hdc, f)
17072 #define RELEASE_HDC(hdc, f)
17073 #endif
17074
17075 static void
17076 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17077 struct glyph_string *s;
17078 DECLARE_HDC (hdc)
17079 XChar2b *char2b;
17080 struct window *w;
17081 struct glyph_row *row;
17082 enum glyph_row_area area;
17083 int start;
17084 enum draw_glyphs_face hl;
17085 {
17086 bzero (s, sizeof *s);
17087 s->w = w;
17088 s->f = XFRAME (w->frame);
17089 #ifdef HAVE_NTGUI
17090 s->hdc = hdc;
17091 #endif
17092 s->display = FRAME_X_DISPLAY (s->f);
17093 s->window = FRAME_X_WINDOW (s->f);
17094 s->char2b = char2b;
17095 s->hl = hl;
17096 s->row = row;
17097 s->area = area;
17098 s->first_glyph = row->glyphs[area] + start;
17099 s->height = row->height;
17100 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17101
17102 /* Display the internal border below the tool-bar window. */
17103 if (s->w == XWINDOW (s->f->tool_bar_window))
17104 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17105
17106 s->ybase = s->y + row->ascent;
17107 }
17108
17109
17110 /* Append the list of glyph strings with head H and tail T to the list
17111 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17112
17113 static INLINE void
17114 append_glyph_string_lists (head, tail, h, t)
17115 struct glyph_string **head, **tail;
17116 struct glyph_string *h, *t;
17117 {
17118 if (h)
17119 {
17120 if (*head)
17121 (*tail)->next = h;
17122 else
17123 *head = h;
17124 h->prev = *tail;
17125 *tail = t;
17126 }
17127 }
17128
17129
17130 /* Prepend the list of glyph strings with head H and tail T to the
17131 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17132 result. */
17133
17134 static INLINE void
17135 prepend_glyph_string_lists (head, tail, h, t)
17136 struct glyph_string **head, **tail;
17137 struct glyph_string *h, *t;
17138 {
17139 if (h)
17140 {
17141 if (*head)
17142 (*head)->prev = t;
17143 else
17144 *tail = t;
17145 t->next = *head;
17146 *head = h;
17147 }
17148 }
17149
17150
17151 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17152 Set *HEAD and *TAIL to the resulting list. */
17153
17154 static INLINE void
17155 append_glyph_string (head, tail, s)
17156 struct glyph_string **head, **tail;
17157 struct glyph_string *s;
17158 {
17159 s->next = s->prev = NULL;
17160 append_glyph_string_lists (head, tail, s, s);
17161 }
17162
17163
17164 /* Get face and two-byte form of character glyph GLYPH on frame F.
17165 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17166 a pointer to a realized face that is ready for display. */
17167
17168 static INLINE struct face *
17169 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17170 struct frame *f;
17171 struct glyph *glyph;
17172 XChar2b *char2b;
17173 int *two_byte_p;
17174 {
17175 struct face *face;
17176
17177 xassert (glyph->type == CHAR_GLYPH);
17178 face = FACE_FROM_ID (f, glyph->face_id);
17179
17180 if (two_byte_p)
17181 *two_byte_p = 0;
17182
17183 if (!glyph->multibyte_p)
17184 {
17185 /* Unibyte case. We don't have to encode, but we have to make
17186 sure to use a face suitable for unibyte. */
17187 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17188 }
17189 else if (glyph->u.ch < 128
17190 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17191 {
17192 /* Case of ASCII in a face known to fit ASCII. */
17193 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17194 }
17195 else
17196 {
17197 int c1, c2, charset;
17198
17199 /* Split characters into bytes. If c2 is -1 afterwards, C is
17200 really a one-byte character so that byte1 is zero. */
17201 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17202 if (c2 > 0)
17203 STORE_XCHAR2B (char2b, c1, c2);
17204 else
17205 STORE_XCHAR2B (char2b, 0, c1);
17206
17207 /* Maybe encode the character in *CHAR2B. */
17208 if (charset != CHARSET_ASCII)
17209 {
17210 struct font_info *font_info
17211 = FONT_INFO_FROM_ID (f, face->font_info_id);
17212 if (font_info)
17213 glyph->font_type
17214 = FRAME_RIF (f)->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
17215 }
17216 }
17217
17218 /* Make sure X resources of the face are allocated. */
17219 xassert (face != NULL);
17220 PREPARE_FACE_FOR_DISPLAY (f, face);
17221 return face;
17222 }
17223
17224
17225 /* Fill glyph string S with composition components specified by S->cmp.
17226
17227 FACES is an array of faces for all components of this composition.
17228 S->gidx is the index of the first component for S.
17229 OVERLAPS_P non-zero means S should draw the foreground only, and
17230 use its physical height for clipping.
17231
17232 Value is the index of a component not in S. */
17233
17234 static int
17235 fill_composite_glyph_string (s, faces, overlaps_p)
17236 struct glyph_string *s;
17237 struct face **faces;
17238 int overlaps_p;
17239 {
17240 int i;
17241
17242 xassert (s);
17243
17244 s->for_overlaps_p = overlaps_p;
17245
17246 s->face = faces[s->gidx];
17247 s->font = s->face->font;
17248 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17249
17250 /* For all glyphs of this composition, starting at the offset
17251 S->gidx, until we reach the end of the definition or encounter a
17252 glyph that requires the different face, add it to S. */
17253 ++s->nchars;
17254 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
17255 ++s->nchars;
17256
17257 /* All glyph strings for the same composition has the same width,
17258 i.e. the width set for the first component of the composition. */
17259
17260 s->width = s->first_glyph->pixel_width;
17261
17262 /* If the specified font could not be loaded, use the frame's
17263 default font, but record the fact that we couldn't load it in
17264 the glyph string so that we can draw rectangles for the
17265 characters of the glyph string. */
17266 if (s->font == NULL)
17267 {
17268 s->font_not_found_p = 1;
17269 s->font = FRAME_FONT (s->f);
17270 }
17271
17272 /* Adjust base line for subscript/superscript text. */
17273 s->ybase += s->first_glyph->voffset;
17274
17275 xassert (s->face && s->face->gc);
17276
17277 /* This glyph string must always be drawn with 16-bit functions. */
17278 s->two_byte_p = 1;
17279
17280 return s->gidx + s->nchars;
17281 }
17282
17283
17284 /* Fill glyph string S from a sequence of character glyphs.
17285
17286 FACE_ID is the face id of the string. START is the index of the
17287 first glyph to consider, END is the index of the last + 1.
17288 OVERLAPS_P non-zero means S should draw the foreground only, and
17289 use its physical height for clipping.
17290
17291 Value is the index of the first glyph not in S. */
17292
17293 static int
17294 fill_glyph_string (s, face_id, start, end, overlaps_p)
17295 struct glyph_string *s;
17296 int face_id;
17297 int start, end, overlaps_p;
17298 {
17299 struct glyph *glyph, *last;
17300 int voffset;
17301 int glyph_not_available_p;
17302
17303 xassert (s->f == XFRAME (s->w->frame));
17304 xassert (s->nchars == 0);
17305 xassert (start >= 0 && end > start);
17306
17307 s->for_overlaps_p = overlaps_p,
17308 glyph = s->row->glyphs[s->area] + start;
17309 last = s->row->glyphs[s->area] + end;
17310 voffset = glyph->voffset;
17311
17312 glyph_not_available_p = glyph->glyph_not_available_p;
17313
17314 while (glyph < last
17315 && glyph->type == CHAR_GLYPH
17316 && glyph->voffset == voffset
17317 /* Same face id implies same font, nowadays. */
17318 && glyph->face_id == face_id
17319 && glyph->glyph_not_available_p == glyph_not_available_p)
17320 {
17321 int two_byte_p;
17322
17323 s->face = get_glyph_face_and_encoding (s->f, glyph,
17324 s->char2b + s->nchars,
17325 &two_byte_p);
17326 s->two_byte_p = two_byte_p;
17327 ++s->nchars;
17328 xassert (s->nchars <= end - start);
17329 s->width += glyph->pixel_width;
17330 ++glyph;
17331 }
17332
17333 s->font = s->face->font;
17334 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17335
17336 /* If the specified font could not be loaded, use the frame's font,
17337 but record the fact that we couldn't load it in
17338 S->font_not_found_p so that we can draw rectangles for the
17339 characters of the glyph string. */
17340 if (s->font == NULL || glyph_not_available_p)
17341 {
17342 s->font_not_found_p = 1;
17343 s->font = FRAME_FONT (s->f);
17344 }
17345
17346 /* Adjust base line for subscript/superscript text. */
17347 s->ybase += voffset;
17348
17349 xassert (s->face && s->face->gc);
17350 return glyph - s->row->glyphs[s->area];
17351 }
17352
17353
17354 /* Fill glyph string S from image glyph S->first_glyph. */
17355
17356 static void
17357 fill_image_glyph_string (s)
17358 struct glyph_string *s;
17359 {
17360 xassert (s->first_glyph->type == IMAGE_GLYPH);
17361 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
17362 xassert (s->img);
17363 s->slice = s->first_glyph->slice;
17364 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
17365 s->font = s->face->font;
17366 s->width = s->first_glyph->pixel_width;
17367
17368 /* Adjust base line for subscript/superscript text. */
17369 s->ybase += s->first_glyph->voffset;
17370 }
17371
17372
17373 /* Fill glyph string S from a sequence of stretch glyphs.
17374
17375 ROW is the glyph row in which the glyphs are found, AREA is the
17376 area within the row. START is the index of the first glyph to
17377 consider, END is the index of the last + 1.
17378
17379 Value is the index of the first glyph not in S. */
17380
17381 static int
17382 fill_stretch_glyph_string (s, row, area, start, end)
17383 struct glyph_string *s;
17384 struct glyph_row *row;
17385 enum glyph_row_area area;
17386 int start, end;
17387 {
17388 struct glyph *glyph, *last;
17389 int voffset, face_id;
17390
17391 xassert (s->first_glyph->type == STRETCH_GLYPH);
17392
17393 glyph = s->row->glyphs[s->area] + start;
17394 last = s->row->glyphs[s->area] + end;
17395 face_id = glyph->face_id;
17396 s->face = FACE_FROM_ID (s->f, face_id);
17397 s->font = s->face->font;
17398 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17399 s->width = glyph->pixel_width;
17400 voffset = glyph->voffset;
17401
17402 for (++glyph;
17403 (glyph < last
17404 && glyph->type == STRETCH_GLYPH
17405 && glyph->voffset == voffset
17406 && glyph->face_id == face_id);
17407 ++glyph)
17408 s->width += glyph->pixel_width;
17409
17410 /* Adjust base line for subscript/superscript text. */
17411 s->ybase += voffset;
17412
17413 /* The case that face->gc == 0 is handled when drawing the glyph
17414 string by calling PREPARE_FACE_FOR_DISPLAY. */
17415 xassert (s->face);
17416 return glyph - s->row->glyphs[s->area];
17417 }
17418
17419
17420 /* EXPORT for RIF:
17421 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
17422 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
17423 assumed to be zero. */
17424
17425 void
17426 x_get_glyph_overhangs (glyph, f, left, right)
17427 struct glyph *glyph;
17428 struct frame *f;
17429 int *left, *right;
17430 {
17431 *left = *right = 0;
17432
17433 if (glyph->type == CHAR_GLYPH)
17434 {
17435 XFontStruct *font;
17436 struct face *face;
17437 struct font_info *font_info;
17438 XChar2b char2b;
17439 XCharStruct *pcm;
17440
17441 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
17442 font = face->font;
17443 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
17444 if (font /* ++KFS: Should this be font_info ? */
17445 && (pcm = FRAME_RIF (f)->per_char_metric (font, &char2b, glyph->font_type)))
17446 {
17447 if (pcm->rbearing > pcm->width)
17448 *right = pcm->rbearing - pcm->width;
17449 if (pcm->lbearing < 0)
17450 *left = -pcm->lbearing;
17451 }
17452 }
17453 }
17454
17455
17456 /* Return the index of the first glyph preceding glyph string S that
17457 is overwritten by S because of S's left overhang. Value is -1
17458 if no glyphs are overwritten. */
17459
17460 static int
17461 left_overwritten (s)
17462 struct glyph_string *s;
17463 {
17464 int k;
17465
17466 if (s->left_overhang)
17467 {
17468 int x = 0, i;
17469 struct glyph *glyphs = s->row->glyphs[s->area];
17470 int first = s->first_glyph - glyphs;
17471
17472 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
17473 x -= glyphs[i].pixel_width;
17474
17475 k = i + 1;
17476 }
17477 else
17478 k = -1;
17479
17480 return k;
17481 }
17482
17483
17484 /* Return the index of the first glyph preceding glyph string S that
17485 is overwriting S because of its right overhang. Value is -1 if no
17486 glyph in front of S overwrites S. */
17487
17488 static int
17489 left_overwriting (s)
17490 struct glyph_string *s;
17491 {
17492 int i, k, x;
17493 struct glyph *glyphs = s->row->glyphs[s->area];
17494 int first = s->first_glyph - glyphs;
17495
17496 k = -1;
17497 x = 0;
17498 for (i = first - 1; i >= 0; --i)
17499 {
17500 int left, right;
17501 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17502 if (x + right > 0)
17503 k = i;
17504 x -= glyphs[i].pixel_width;
17505 }
17506
17507 return k;
17508 }
17509
17510
17511 /* Return the index of the last glyph following glyph string S that is
17512 not overwritten by S because of S's right overhang. Value is -1 if
17513 no such glyph is found. */
17514
17515 static int
17516 right_overwritten (s)
17517 struct glyph_string *s;
17518 {
17519 int k = -1;
17520
17521 if (s->right_overhang)
17522 {
17523 int x = 0, i;
17524 struct glyph *glyphs = s->row->glyphs[s->area];
17525 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17526 int end = s->row->used[s->area];
17527
17528 for (i = first; i < end && s->right_overhang > x; ++i)
17529 x += glyphs[i].pixel_width;
17530
17531 k = i;
17532 }
17533
17534 return k;
17535 }
17536
17537
17538 /* Return the index of the last glyph following glyph string S that
17539 overwrites S because of its left overhang. Value is negative
17540 if no such glyph is found. */
17541
17542 static int
17543 right_overwriting (s)
17544 struct glyph_string *s;
17545 {
17546 int i, k, x;
17547 int end = s->row->used[s->area];
17548 struct glyph *glyphs = s->row->glyphs[s->area];
17549 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17550
17551 k = -1;
17552 x = 0;
17553 for (i = first; i < end; ++i)
17554 {
17555 int left, right;
17556 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17557 if (x - left < 0)
17558 k = i;
17559 x += glyphs[i].pixel_width;
17560 }
17561
17562 return k;
17563 }
17564
17565
17566 /* Get face and two-byte form of character C in face FACE_ID on frame
17567 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
17568 means we want to display multibyte text. DISPLAY_P non-zero means
17569 make sure that X resources for the face returned are allocated.
17570 Value is a pointer to a realized face that is ready for display if
17571 DISPLAY_P is non-zero. */
17572
17573 static INLINE struct face *
17574 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
17575 struct frame *f;
17576 int c, face_id;
17577 XChar2b *char2b;
17578 int multibyte_p, display_p;
17579 {
17580 struct face *face = FACE_FROM_ID (f, face_id);
17581
17582 if (!multibyte_p)
17583 {
17584 /* Unibyte case. We don't have to encode, but we have to make
17585 sure to use a face suitable for unibyte. */
17586 STORE_XCHAR2B (char2b, 0, c);
17587 face_id = FACE_FOR_CHAR (f, face, c);
17588 face = FACE_FROM_ID (f, face_id);
17589 }
17590 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
17591 {
17592 /* Case of ASCII in a face known to fit ASCII. */
17593 STORE_XCHAR2B (char2b, 0, c);
17594 }
17595 else
17596 {
17597 int c1, c2, charset;
17598
17599 /* Split characters into bytes. If c2 is -1 afterwards, C is
17600 really a one-byte character so that byte1 is zero. */
17601 SPLIT_CHAR (c, charset, c1, c2);
17602 if (c2 > 0)
17603 STORE_XCHAR2B (char2b, c1, c2);
17604 else
17605 STORE_XCHAR2B (char2b, 0, c1);
17606
17607 /* Maybe encode the character in *CHAR2B. */
17608 if (face->font != NULL)
17609 {
17610 struct font_info *font_info
17611 = FONT_INFO_FROM_ID (f, face->font_info_id);
17612 if (font_info)
17613 FRAME_RIF (f)->encode_char (c, char2b, font_info, 0);
17614 }
17615 }
17616
17617 /* Make sure X resources of the face are allocated. */
17618 #ifdef HAVE_X_WINDOWS
17619 if (display_p)
17620 #endif
17621 {
17622 xassert (face != NULL);
17623 PREPARE_FACE_FOR_DISPLAY (f, face);
17624 }
17625
17626 return face;
17627 }
17628
17629
17630 /* Set background width of glyph string S. START is the index of the
17631 first glyph following S. LAST_X is the right-most x-position + 1
17632 in the drawing area. */
17633
17634 static INLINE void
17635 set_glyph_string_background_width (s, start, last_x)
17636 struct glyph_string *s;
17637 int start;
17638 int last_x;
17639 {
17640 /* If the face of this glyph string has to be drawn to the end of
17641 the drawing area, set S->extends_to_end_of_line_p. */
17642 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
17643
17644 if (start == s->row->used[s->area]
17645 && s->area == TEXT_AREA
17646 && ((s->hl == DRAW_NORMAL_TEXT
17647 && (s->row->fill_line_p
17648 || s->face->background != default_face->background
17649 || s->face->stipple != default_face->stipple
17650 || s->row->mouse_face_p))
17651 || s->hl == DRAW_MOUSE_FACE
17652 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
17653 && s->row->fill_line_p)))
17654 s->extends_to_end_of_line_p = 1;
17655
17656 /* If S extends its face to the end of the line, set its
17657 background_width to the distance to the right edge of the drawing
17658 area. */
17659 if (s->extends_to_end_of_line_p)
17660 s->background_width = last_x - s->x + 1;
17661 else
17662 s->background_width = s->width;
17663 }
17664
17665
17666 /* Compute overhangs and x-positions for glyph string S and its
17667 predecessors, or successors. X is the starting x-position for S.
17668 BACKWARD_P non-zero means process predecessors. */
17669
17670 static void
17671 compute_overhangs_and_x (s, x, backward_p)
17672 struct glyph_string *s;
17673 int x;
17674 int backward_p;
17675 {
17676 if (backward_p)
17677 {
17678 while (s)
17679 {
17680 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
17681 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
17682 x -= s->width;
17683 s->x = x;
17684 s = s->prev;
17685 }
17686 }
17687 else
17688 {
17689 while (s)
17690 {
17691 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
17692 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
17693 s->x = x;
17694 x += s->width;
17695 s = s->next;
17696 }
17697 }
17698 }
17699
17700
17701
17702 /* The following macros are only called from draw_glyphs below.
17703 They reference the following parameters of that function directly:
17704 `w', `row', `area', and `overlap_p'
17705 as well as the following local variables:
17706 `s', `f', and `hdc' (in W32) */
17707
17708 #ifdef HAVE_NTGUI
17709 /* On W32, silently add local `hdc' variable to argument list of
17710 init_glyph_string. */
17711 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17712 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
17713 #else
17714 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17715 init_glyph_string (s, char2b, w, row, area, start, hl)
17716 #endif
17717
17718 /* Add a glyph string for a stretch glyph to the list of strings
17719 between HEAD and TAIL. START is the index of the stretch glyph in
17720 row area AREA of glyph row ROW. END is the index of the last glyph
17721 in that glyph row area. X is the current output position assigned
17722 to the new glyph string constructed. HL overrides that face of the
17723 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17724 is the right-most x-position of the drawing area. */
17725
17726 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
17727 and below -- keep them on one line. */
17728 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17729 do \
17730 { \
17731 s = (struct glyph_string *) alloca (sizeof *s); \
17732 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17733 START = fill_stretch_glyph_string (s, row, area, START, END); \
17734 append_glyph_string (&HEAD, &TAIL, s); \
17735 s->x = (X); \
17736 } \
17737 while (0)
17738
17739
17740 /* Add a glyph string for an image glyph to the list of strings
17741 between HEAD and TAIL. START is the index of the image glyph in
17742 row area AREA of glyph row ROW. END is the index of the last glyph
17743 in that glyph row area. X is the current output position assigned
17744 to the new glyph string constructed. HL overrides that face of the
17745 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17746 is the right-most x-position of the drawing area. */
17747
17748 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17749 do \
17750 { \
17751 s = (struct glyph_string *) alloca (sizeof *s); \
17752 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17753 fill_image_glyph_string (s); \
17754 append_glyph_string (&HEAD, &TAIL, s); \
17755 ++START; \
17756 s->x = (X); \
17757 } \
17758 while (0)
17759
17760
17761 /* Add a glyph string for a sequence of character glyphs to the list
17762 of strings between HEAD and TAIL. START is the index of the first
17763 glyph in row area AREA of glyph row ROW that is part of the new
17764 glyph string. END is the index of the last glyph in that glyph row
17765 area. X is the current output position assigned to the new glyph
17766 string constructed. HL overrides that face of the glyph; e.g. it
17767 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
17768 right-most x-position of the drawing area. */
17769
17770 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17771 do \
17772 { \
17773 int c, face_id; \
17774 XChar2b *char2b; \
17775 \
17776 c = (row)->glyphs[area][START].u.ch; \
17777 face_id = (row)->glyphs[area][START].face_id; \
17778 \
17779 s = (struct glyph_string *) alloca (sizeof *s); \
17780 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
17781 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
17782 append_glyph_string (&HEAD, &TAIL, s); \
17783 s->x = (X); \
17784 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
17785 } \
17786 while (0)
17787
17788
17789 /* Add a glyph string for a composite sequence to the list of strings
17790 between HEAD and TAIL. START is the index of the first glyph in
17791 row area AREA of glyph row ROW that is part of the new glyph
17792 string. END is the index of the last glyph in that glyph row area.
17793 X is the current output position assigned to the new glyph string
17794 constructed. HL overrides that face of the glyph; e.g. it is
17795 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
17796 x-position of the drawing area. */
17797
17798 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17799 do { \
17800 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
17801 int face_id = (row)->glyphs[area][START].face_id; \
17802 struct face *base_face = FACE_FROM_ID (f, face_id); \
17803 struct composition *cmp = composition_table[cmp_id]; \
17804 int glyph_len = cmp->glyph_len; \
17805 XChar2b *char2b; \
17806 struct face **faces; \
17807 struct glyph_string *first_s = NULL; \
17808 int n; \
17809 \
17810 base_face = base_face->ascii_face; \
17811 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
17812 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
17813 /* At first, fill in `char2b' and `faces'. */ \
17814 for (n = 0; n < glyph_len; n++) \
17815 { \
17816 int c = COMPOSITION_GLYPH (cmp, n); \
17817 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
17818 faces[n] = FACE_FROM_ID (f, this_face_id); \
17819 get_char_face_and_encoding (f, c, this_face_id, \
17820 char2b + n, 1, 1); \
17821 } \
17822 \
17823 /* Make glyph_strings for each glyph sequence that is drawable by \
17824 the same face, and append them to HEAD/TAIL. */ \
17825 for (n = 0; n < cmp->glyph_len;) \
17826 { \
17827 s = (struct glyph_string *) alloca (sizeof *s); \
17828 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
17829 append_glyph_string (&(HEAD), &(TAIL), s); \
17830 s->cmp = cmp; \
17831 s->gidx = n; \
17832 s->x = (X); \
17833 \
17834 if (n == 0) \
17835 first_s = s; \
17836 \
17837 n = fill_composite_glyph_string (s, faces, overlaps_p); \
17838 } \
17839 \
17840 ++START; \
17841 s = first_s; \
17842 } while (0)
17843
17844
17845 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
17846 of AREA of glyph row ROW on window W between indices START and END.
17847 HL overrides the face for drawing glyph strings, e.g. it is
17848 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
17849 x-positions of the drawing area.
17850
17851 This is an ugly monster macro construct because we must use alloca
17852 to allocate glyph strings (because draw_glyphs can be called
17853 asynchronously). */
17854
17855 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17856 do \
17857 { \
17858 HEAD = TAIL = NULL; \
17859 while (START < END) \
17860 { \
17861 struct glyph *first_glyph = (row)->glyphs[area] + START; \
17862 switch (first_glyph->type) \
17863 { \
17864 case CHAR_GLYPH: \
17865 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
17866 HL, X, LAST_X); \
17867 break; \
17868 \
17869 case COMPOSITE_GLYPH: \
17870 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
17871 HL, X, LAST_X); \
17872 break; \
17873 \
17874 case STRETCH_GLYPH: \
17875 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
17876 HL, X, LAST_X); \
17877 break; \
17878 \
17879 case IMAGE_GLYPH: \
17880 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
17881 HL, X, LAST_X); \
17882 break; \
17883 \
17884 default: \
17885 abort (); \
17886 } \
17887 \
17888 set_glyph_string_background_width (s, START, LAST_X); \
17889 (X) += s->width; \
17890 } \
17891 } \
17892 while (0)
17893
17894
17895 /* Draw glyphs between START and END in AREA of ROW on window W,
17896 starting at x-position X. X is relative to AREA in W. HL is a
17897 face-override with the following meaning:
17898
17899 DRAW_NORMAL_TEXT draw normally
17900 DRAW_CURSOR draw in cursor face
17901 DRAW_MOUSE_FACE draw in mouse face.
17902 DRAW_INVERSE_VIDEO draw in mode line face
17903 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
17904 DRAW_IMAGE_RAISED draw an image with a raised relief around it
17905
17906 If OVERLAPS_P is non-zero, draw only the foreground of characters
17907 and clip to the physical height of ROW.
17908
17909 Value is the x-position reached, relative to AREA of W. */
17910
17911 static int
17912 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
17913 struct window *w;
17914 int x;
17915 struct glyph_row *row;
17916 enum glyph_row_area area;
17917 int start, end;
17918 enum draw_glyphs_face hl;
17919 int overlaps_p;
17920 {
17921 struct glyph_string *head, *tail;
17922 struct glyph_string *s;
17923 int last_x, area_width;
17924 int x_reached;
17925 int i, j;
17926 struct frame *f = XFRAME (WINDOW_FRAME (w));
17927 DECLARE_HDC (hdc);
17928
17929 ALLOCATE_HDC (hdc, f);
17930
17931 /* Let's rather be paranoid than getting a SEGV. */
17932 end = min (end, row->used[area]);
17933 start = max (0, start);
17934 start = min (end, start);
17935
17936 /* Translate X to frame coordinates. Set last_x to the right
17937 end of the drawing area. */
17938 if (row->full_width_p)
17939 {
17940 /* X is relative to the left edge of W, without scroll bars
17941 or fringes. */
17942 x += WINDOW_LEFT_EDGE_X (w);
17943 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
17944 }
17945 else
17946 {
17947 int area_left = window_box_left (w, area);
17948 x += area_left;
17949 area_width = window_box_width (w, area);
17950 last_x = area_left + area_width;
17951 }
17952
17953 /* Build a doubly-linked list of glyph_string structures between
17954 head and tail from what we have to draw. Note that the macro
17955 BUILD_GLYPH_STRINGS will modify its start parameter. That's
17956 the reason we use a separate variable `i'. */
17957 i = start;
17958 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
17959 if (tail)
17960 x_reached = tail->x + tail->background_width;
17961 else
17962 x_reached = x;
17963
17964 /* If there are any glyphs with lbearing < 0 or rbearing > width in
17965 the row, redraw some glyphs in front or following the glyph
17966 strings built above. */
17967 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
17968 {
17969 int dummy_x = 0;
17970 struct glyph_string *h, *t;
17971
17972 /* Compute overhangs for all glyph strings. */
17973 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
17974 for (s = head; s; s = s->next)
17975 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
17976
17977 /* Prepend glyph strings for glyphs in front of the first glyph
17978 string that are overwritten because of the first glyph
17979 string's left overhang. The background of all strings
17980 prepended must be drawn because the first glyph string
17981 draws over it. */
17982 i = left_overwritten (head);
17983 if (i >= 0)
17984 {
17985 j = i;
17986 BUILD_GLYPH_STRINGS (j, start, h, t,
17987 DRAW_NORMAL_TEXT, dummy_x, last_x);
17988 start = i;
17989 compute_overhangs_and_x (t, head->x, 1);
17990 prepend_glyph_string_lists (&head, &tail, h, t);
17991 }
17992
17993 /* Prepend glyph strings for glyphs in front of the first glyph
17994 string that overwrite that glyph string because of their
17995 right overhang. For these strings, only the foreground must
17996 be drawn, because it draws over the glyph string at `head'.
17997 The background must not be drawn because this would overwrite
17998 right overhangs of preceding glyphs for which no glyph
17999 strings exist. */
18000 i = left_overwriting (head);
18001 if (i >= 0)
18002 {
18003 BUILD_GLYPH_STRINGS (i, start, h, t,
18004 DRAW_NORMAL_TEXT, dummy_x, last_x);
18005 for (s = h; s; s = s->next)
18006 s->background_filled_p = 1;
18007 compute_overhangs_and_x (t, head->x, 1);
18008 prepend_glyph_string_lists (&head, &tail, h, t);
18009 }
18010
18011 /* Append glyphs strings for glyphs following the last glyph
18012 string tail that are overwritten by tail. The background of
18013 these strings has to be drawn because tail's foreground draws
18014 over it. */
18015 i = right_overwritten (tail);
18016 if (i >= 0)
18017 {
18018 BUILD_GLYPH_STRINGS (end, i, h, t,
18019 DRAW_NORMAL_TEXT, x, last_x);
18020 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18021 append_glyph_string_lists (&head, &tail, h, t);
18022 }
18023
18024 /* Append glyph strings for glyphs following the last glyph
18025 string tail that overwrite tail. The foreground of such
18026 glyphs has to be drawn because it writes into the background
18027 of tail. The background must not be drawn because it could
18028 paint over the foreground of following glyphs. */
18029 i = right_overwriting (tail);
18030 if (i >= 0)
18031 {
18032 BUILD_GLYPH_STRINGS (end, i, h, t,
18033 DRAW_NORMAL_TEXT, x, last_x);
18034 for (s = h; s; s = s->next)
18035 s->background_filled_p = 1;
18036 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18037 append_glyph_string_lists (&head, &tail, h, t);
18038 }
18039 }
18040
18041 /* Draw all strings. */
18042 for (s = head; s; s = s->next)
18043 FRAME_RIF (f)->draw_glyph_string (s);
18044
18045 if (area == TEXT_AREA
18046 && !row->full_width_p
18047 /* When drawing overlapping rows, only the glyph strings'
18048 foreground is drawn, which doesn't erase a cursor
18049 completely. */
18050 && !overlaps_p)
18051 {
18052 int x0 = head ? head->x : x;
18053 int x1 = tail ? tail->x + tail->background_width : x;
18054
18055 int text_left = window_box_left (w, TEXT_AREA);
18056 x0 -= text_left;
18057 x1 -= text_left;
18058
18059 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18060 row->y, MATRIX_ROW_BOTTOM_Y (row));
18061 }
18062
18063 /* Value is the x-position up to which drawn, relative to AREA of W.
18064 This doesn't include parts drawn because of overhangs. */
18065 if (row->full_width_p)
18066 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18067 else
18068 x_reached -= window_box_left (w, area);
18069
18070 RELEASE_HDC (hdc, f);
18071
18072 return x_reached;
18073 }
18074
18075
18076 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18077 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18078
18079 static INLINE void
18080 append_glyph (it)
18081 struct it *it;
18082 {
18083 struct glyph *glyph;
18084 enum glyph_row_area area = it->area;
18085
18086 xassert (it->glyph_row);
18087 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18088
18089 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18090 if (glyph < it->glyph_row->glyphs[area + 1])
18091 {
18092 glyph->charpos = CHARPOS (it->position);
18093 glyph->object = it->object;
18094 glyph->pixel_width = it->pixel_width;
18095 glyph->ascent = it->ascent;
18096 glyph->descent = it->descent;
18097 glyph->voffset = it->voffset;
18098 glyph->type = CHAR_GLYPH;
18099 glyph->multibyte_p = it->multibyte_p;
18100 glyph->left_box_line_p = it->start_of_box_run_p;
18101 glyph->right_box_line_p = it->end_of_box_run_p;
18102 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18103 || it->phys_descent > it->descent);
18104 glyph->padding_p = 0;
18105 glyph->glyph_not_available_p = it->glyph_not_available_p;
18106 glyph->face_id = it->face_id;
18107 glyph->u.ch = it->char_to_display;
18108 glyph->slice = null_glyph_slice;
18109 glyph->font_type = FONT_TYPE_UNKNOWN;
18110 ++it->glyph_row->used[area];
18111 }
18112 }
18113
18114 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18115 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18116
18117 static INLINE void
18118 append_composite_glyph (it)
18119 struct it *it;
18120 {
18121 struct glyph *glyph;
18122 enum glyph_row_area area = it->area;
18123
18124 xassert (it->glyph_row);
18125
18126 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18127 if (glyph < it->glyph_row->glyphs[area + 1])
18128 {
18129 glyph->charpos = CHARPOS (it->position);
18130 glyph->object = it->object;
18131 glyph->pixel_width = it->pixel_width;
18132 glyph->ascent = it->ascent;
18133 glyph->descent = it->descent;
18134 glyph->voffset = it->voffset;
18135 glyph->type = COMPOSITE_GLYPH;
18136 glyph->multibyte_p = it->multibyte_p;
18137 glyph->left_box_line_p = it->start_of_box_run_p;
18138 glyph->right_box_line_p = it->end_of_box_run_p;
18139 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18140 || it->phys_descent > it->descent);
18141 glyph->padding_p = 0;
18142 glyph->glyph_not_available_p = 0;
18143 glyph->face_id = it->face_id;
18144 glyph->u.cmp_id = it->cmp_id;
18145 glyph->slice = null_glyph_slice;
18146 glyph->font_type = FONT_TYPE_UNKNOWN;
18147 ++it->glyph_row->used[area];
18148 }
18149 }
18150
18151
18152 /* Change IT->ascent and IT->height according to the setting of
18153 IT->voffset. */
18154
18155 static INLINE void
18156 take_vertical_position_into_account (it)
18157 struct it *it;
18158 {
18159 if (it->voffset)
18160 {
18161 if (it->voffset < 0)
18162 /* Increase the ascent so that we can display the text higher
18163 in the line. */
18164 it->ascent -= it->voffset;
18165 else
18166 /* Increase the descent so that we can display the text lower
18167 in the line. */
18168 it->descent += it->voffset;
18169 }
18170 }
18171
18172
18173 /* Produce glyphs/get display metrics for the image IT is loaded with.
18174 See the description of struct display_iterator in dispextern.h for
18175 an overview of struct display_iterator. */
18176
18177 static void
18178 produce_image_glyph (it)
18179 struct it *it;
18180 {
18181 struct image *img;
18182 struct face *face;
18183 int face_ascent, glyph_ascent;
18184 struct glyph_slice slice;
18185
18186 xassert (it->what == IT_IMAGE);
18187
18188 face = FACE_FROM_ID (it->f, it->face_id);
18189 xassert (face);
18190 /* Make sure X resources of the face is loaded. */
18191 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18192
18193 if (it->image_id < 0)
18194 {
18195 /* Fringe bitmap. */
18196 it->ascent = it->phys_ascent = 0;
18197 it->descent = it->phys_descent = 0;
18198 it->pixel_width = 0;
18199 it->nglyphs = 0;
18200 return;
18201 }
18202
18203 img = IMAGE_FROM_ID (it->f, it->image_id);
18204 xassert (img);
18205 /* Make sure X resources of the image is loaded. */
18206 prepare_image_for_display (it->f, img);
18207
18208 slice.x = slice.y = 0;
18209 slice.width = img->width;
18210 slice.height = img->height;
18211
18212 if (INTEGERP (it->slice.x))
18213 slice.x = XINT (it->slice.x);
18214 else if (FLOATP (it->slice.x))
18215 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
18216
18217 if (INTEGERP (it->slice.y))
18218 slice.y = XINT (it->slice.y);
18219 else if (FLOATP (it->slice.y))
18220 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
18221
18222 if (INTEGERP (it->slice.width))
18223 slice.width = XINT (it->slice.width);
18224 else if (FLOATP (it->slice.width))
18225 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
18226
18227 if (INTEGERP (it->slice.height))
18228 slice.height = XINT (it->slice.height);
18229 else if (FLOATP (it->slice.height))
18230 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
18231
18232 if (slice.x >= img->width)
18233 slice.x = img->width;
18234 if (slice.y >= img->height)
18235 slice.y = img->height;
18236 if (slice.x + slice.width >= img->width)
18237 slice.width = img->width - slice.x;
18238 if (slice.y + slice.height > img->height)
18239 slice.height = img->height - slice.y;
18240
18241 if (slice.width == 0 || slice.height == 0)
18242 return;
18243
18244 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
18245
18246 it->descent = slice.height - glyph_ascent;
18247 if (slice.y == 0)
18248 it->descent += img->vmargin;
18249 if (slice.y + slice.height == img->height)
18250 it->descent += img->vmargin;
18251 it->phys_descent = it->descent;
18252
18253 it->pixel_width = slice.width;
18254 if (slice.x == 0)
18255 it->pixel_width += img->hmargin;
18256 if (slice.x + slice.width == img->width)
18257 it->pixel_width += img->hmargin;
18258
18259 /* It's quite possible for images to have an ascent greater than
18260 their height, so don't get confused in that case. */
18261 if (it->descent < 0)
18262 it->descent = 0;
18263
18264 #if 0 /* this breaks image tiling */
18265 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
18266 face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
18267 if (face_ascent > it->ascent)
18268 it->ascent = it->phys_ascent = face_ascent;
18269 #endif
18270
18271 it->nglyphs = 1;
18272
18273 if (face->box != FACE_NO_BOX)
18274 {
18275 if (face->box_line_width > 0)
18276 {
18277 if (slice.y == 0)
18278 it->ascent += face->box_line_width;
18279 if (slice.y + slice.height == img->height)
18280 it->descent += face->box_line_width;
18281 }
18282
18283 if (it->start_of_box_run_p && slice.x == 0)
18284 it->pixel_width += abs (face->box_line_width);
18285 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
18286 it->pixel_width += abs (face->box_line_width);
18287 }
18288
18289 take_vertical_position_into_account (it);
18290
18291 if (it->glyph_row)
18292 {
18293 struct glyph *glyph;
18294 enum glyph_row_area area = it->area;
18295
18296 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18297 if (glyph < it->glyph_row->glyphs[area + 1])
18298 {
18299 glyph->charpos = CHARPOS (it->position);
18300 glyph->object = it->object;
18301 glyph->pixel_width = it->pixel_width;
18302 glyph->ascent = glyph_ascent;
18303 glyph->descent = it->descent;
18304 glyph->voffset = it->voffset;
18305 glyph->type = IMAGE_GLYPH;
18306 glyph->multibyte_p = it->multibyte_p;
18307 glyph->left_box_line_p = it->start_of_box_run_p;
18308 glyph->right_box_line_p = it->end_of_box_run_p;
18309 glyph->overlaps_vertically_p = 0;
18310 glyph->padding_p = 0;
18311 glyph->glyph_not_available_p = 0;
18312 glyph->face_id = it->face_id;
18313 glyph->u.img_id = img->id;
18314 glyph->slice = slice;
18315 glyph->font_type = FONT_TYPE_UNKNOWN;
18316 ++it->glyph_row->used[area];
18317 }
18318 }
18319 }
18320
18321
18322 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
18323 of the glyph, WIDTH and HEIGHT are the width and height of the
18324 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
18325
18326 static void
18327 append_stretch_glyph (it, object, width, height, ascent)
18328 struct it *it;
18329 Lisp_Object object;
18330 int width, height;
18331 int ascent;
18332 {
18333 struct glyph *glyph;
18334 enum glyph_row_area area = it->area;
18335
18336 xassert (ascent >= 0 && ascent <= height);
18337
18338 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18339 if (glyph < it->glyph_row->glyphs[area + 1])
18340 {
18341 glyph->charpos = CHARPOS (it->position);
18342 glyph->object = object;
18343 glyph->pixel_width = width;
18344 glyph->ascent = ascent;
18345 glyph->descent = height - ascent;
18346 glyph->voffset = it->voffset;
18347 glyph->type = STRETCH_GLYPH;
18348 glyph->multibyte_p = it->multibyte_p;
18349 glyph->left_box_line_p = it->start_of_box_run_p;
18350 glyph->right_box_line_p = it->end_of_box_run_p;
18351 glyph->overlaps_vertically_p = 0;
18352 glyph->padding_p = 0;
18353 glyph->glyph_not_available_p = 0;
18354 glyph->face_id = it->face_id;
18355 glyph->u.stretch.ascent = ascent;
18356 glyph->u.stretch.height = height;
18357 glyph->slice = null_glyph_slice;
18358 glyph->font_type = FONT_TYPE_UNKNOWN;
18359 ++it->glyph_row->used[area];
18360 }
18361 }
18362
18363
18364 /* Produce a stretch glyph for iterator IT. IT->object is the value
18365 of the glyph property displayed. The value must be a list
18366 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
18367 being recognized:
18368
18369 1. `:width WIDTH' specifies that the space should be WIDTH *
18370 canonical char width wide. WIDTH may be an integer or floating
18371 point number.
18372
18373 2. `:relative-width FACTOR' specifies that the width of the stretch
18374 should be computed from the width of the first character having the
18375 `glyph' property, and should be FACTOR times that width.
18376
18377 3. `:align-to HPOS' specifies that the space should be wide enough
18378 to reach HPOS, a value in canonical character units.
18379
18380 Exactly one of the above pairs must be present.
18381
18382 4. `:height HEIGHT' specifies that the height of the stretch produced
18383 should be HEIGHT, measured in canonical character units.
18384
18385 5. `:relative-height FACTOR' specifies that the height of the
18386 stretch should be FACTOR times the height of the characters having
18387 the glyph property.
18388
18389 Either none or exactly one of 4 or 5 must be present.
18390
18391 6. `:ascent ASCENT' specifies that ASCENT percent of the height
18392 of the stretch should be used for the ascent of the stretch.
18393 ASCENT must be in the range 0 <= ASCENT <= 100. */
18394
18395 static void
18396 produce_stretch_glyph (it)
18397 struct it *it;
18398 {
18399 /* (space :width WIDTH :height HEIGHT ...) */
18400 Lisp_Object prop, plist;
18401 int width = 0, height = 0, align_to = -1;
18402 int zero_width_ok_p = 0, zero_height_ok_p = 0;
18403 int ascent = 0;
18404 double tem;
18405 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18406 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
18407
18408 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18409
18410 /* List should start with `space'. */
18411 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
18412 plist = XCDR (it->object);
18413
18414 /* Compute the width of the stretch. */
18415 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
18416 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
18417 {
18418 /* Absolute width `:width WIDTH' specified and valid. */
18419 zero_width_ok_p = 1;
18420 width = (int)tem;
18421 }
18422 else if (prop = Fplist_get (plist, QCrelative_width),
18423 NUMVAL (prop) > 0)
18424 {
18425 /* Relative width `:relative-width FACTOR' specified and valid.
18426 Compute the width of the characters having the `glyph'
18427 property. */
18428 struct it it2;
18429 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
18430
18431 it2 = *it;
18432 if (it->multibyte_p)
18433 {
18434 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
18435 - IT_BYTEPOS (*it));
18436 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
18437 }
18438 else
18439 it2.c = *p, it2.len = 1;
18440
18441 it2.glyph_row = NULL;
18442 it2.what = IT_CHARACTER;
18443 x_produce_glyphs (&it2);
18444 width = NUMVAL (prop) * it2.pixel_width;
18445 }
18446 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
18447 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
18448 {
18449 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
18450 align_to = (align_to < 0
18451 ? 0
18452 : align_to - window_box_left_offset (it->w, TEXT_AREA));
18453 else if (align_to < 0)
18454 align_to = window_box_left_offset (it->w, TEXT_AREA);
18455 width = max (0, (int)tem + align_to - it->current_x);
18456 zero_width_ok_p = 1;
18457 }
18458 else
18459 /* Nothing specified -> width defaults to canonical char width. */
18460 width = FRAME_COLUMN_WIDTH (it->f);
18461
18462 if (width <= 0 && (width < 0 || !zero_width_ok_p))
18463 width = 1;
18464
18465 /* Compute height. */
18466 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
18467 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18468 {
18469 height = (int)tem;
18470 zero_height_ok_p = 1;
18471 }
18472 else if (prop = Fplist_get (plist, QCrelative_height),
18473 NUMVAL (prop) > 0)
18474 height = FONT_HEIGHT (font) * NUMVAL (prop);
18475 else
18476 height = FONT_HEIGHT (font);
18477
18478 if (height <= 0 && (height < 0 || !zero_height_ok_p))
18479 height = 1;
18480
18481 /* Compute percentage of height used for ascent. If
18482 `:ascent ASCENT' is present and valid, use that. Otherwise,
18483 derive the ascent from the font in use. */
18484 if (prop = Fplist_get (plist, QCascent),
18485 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
18486 ascent = height * NUMVAL (prop) / 100.0;
18487 else if (!NILP (prop)
18488 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18489 ascent = min (max (0, (int)tem), height);
18490 else
18491 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
18492
18493 if (width > 0 && height > 0 && it->glyph_row)
18494 {
18495 Lisp_Object object = it->stack[it->sp - 1].string;
18496 if (!STRINGP (object))
18497 object = it->w->buffer;
18498 append_stretch_glyph (it, object, width, height, ascent);
18499 }
18500
18501 it->pixel_width = width;
18502 it->ascent = it->phys_ascent = ascent;
18503 it->descent = it->phys_descent = height - it->ascent;
18504 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
18505
18506 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
18507 {
18508 if (face->box_line_width > 0)
18509 {
18510 it->ascent += face->box_line_width;
18511 it->descent += face->box_line_width;
18512 }
18513
18514 if (it->start_of_box_run_p)
18515 it->pixel_width += abs (face->box_line_width);
18516 if (it->end_of_box_run_p)
18517 it->pixel_width += abs (face->box_line_width);
18518 }
18519
18520 take_vertical_position_into_account (it);
18521 }
18522
18523 /* Calculate line-height and line-spacing properties.
18524 An integer value specifies explicit pixel value.
18525 A float value specifies relative value to current face height.
18526 A cons (float . face-name) specifies relative value to
18527 height of specified face font.
18528
18529 Returns height in pixels, or nil. */
18530
18531 static Lisp_Object
18532 calc_line_height_property (it, prop, font, boff, total)
18533 struct it *it;
18534 Lisp_Object prop;
18535 XFontStruct *font;
18536 int boff, *total;
18537 {
18538 Lisp_Object position, val;
18539 Lisp_Object face_name = Qnil;
18540 int ascent, descent, height, override;
18541
18542 if (STRINGP (it->object))
18543 position = make_number (IT_STRING_CHARPOS (*it));
18544 else
18545 position = make_number (IT_CHARPOS (*it));
18546
18547 val = Fget_char_property (position, prop, it->object);
18548
18549 if (NILP (val))
18550 return val;
18551
18552 if (total && CONSP (val) && EQ (XCAR (val), Qtotal))
18553 {
18554 *total = 1;
18555 val = XCDR (val);
18556 }
18557
18558 if (INTEGERP (val))
18559 return val;
18560
18561 if (CONSP (val))
18562 {
18563 face_name = XCDR (val);
18564 val = XCAR (val);
18565 }
18566 else if (SYMBOLP (val))
18567 {
18568 face_name = val;
18569 val = Qnil;
18570 }
18571
18572 override = EQ (prop, Qline_height);
18573
18574 if (NILP (face_name))
18575 {
18576 font = FRAME_FONT (it->f);
18577 boff = FRAME_BASELINE_OFFSET (it->f);
18578 }
18579 else if (EQ (face_name, Qt))
18580 {
18581 override = 0;
18582 }
18583 else
18584 {
18585 int face_id;
18586 struct face *face;
18587 struct font_info *font_info;
18588
18589 face_id = lookup_named_face (it->f, face_name, ' ');
18590 if (face_id < 0)
18591 return make_number (-1);
18592
18593 face = FACE_FROM_ID (it->f, face_id);
18594 font = face->font;
18595 if (font == NULL)
18596 return make_number (-1);
18597
18598 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18599 boff = font_info->baseline_offset;
18600 if (font_info->vertical_centering)
18601 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18602 }
18603
18604 ascent = FONT_BASE (font) + boff;
18605 descent = FONT_DESCENT (font) - boff;
18606
18607 if (override)
18608 {
18609 it->override_ascent = ascent;
18610 it->override_descent = descent;
18611 it->override_boff = boff;
18612 }
18613
18614 height = ascent + descent;
18615 if (FLOATP (val))
18616 height = (int)(XFLOAT_DATA (val) * height);
18617 else if (INTEGERP (val))
18618 height *= XINT (val);
18619
18620 return make_number (height);
18621 }
18622
18623
18624 /* RIF:
18625 Produce glyphs/get display metrics for the display element IT is
18626 loaded with. See the description of struct display_iterator in
18627 dispextern.h for an overview of struct display_iterator. */
18628
18629 void
18630 x_produce_glyphs (it)
18631 struct it *it;
18632 {
18633 int extra_line_spacing = it->extra_line_spacing;
18634
18635 it->glyph_not_available_p = 0;
18636
18637 if (it->what == IT_CHARACTER)
18638 {
18639 XChar2b char2b;
18640 XFontStruct *font;
18641 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18642 XCharStruct *pcm;
18643 int font_not_found_p;
18644 struct font_info *font_info;
18645 int boff; /* baseline offset */
18646 /* We may change it->multibyte_p upon unibyte<->multibyte
18647 conversion. So, save the current value now and restore it
18648 later.
18649
18650 Note: It seems that we don't have to record multibyte_p in
18651 struct glyph because the character code itself tells if or
18652 not the character is multibyte. Thus, in the future, we must
18653 consider eliminating the field `multibyte_p' in the struct
18654 glyph. */
18655 int saved_multibyte_p = it->multibyte_p;
18656
18657 /* Maybe translate single-byte characters to multibyte, or the
18658 other way. */
18659 it->char_to_display = it->c;
18660 if (!ASCII_BYTE_P (it->c))
18661 {
18662 if (unibyte_display_via_language_environment
18663 && SINGLE_BYTE_CHAR_P (it->c)
18664 && (it->c >= 0240
18665 || !NILP (Vnonascii_translation_table)))
18666 {
18667 it->char_to_display = unibyte_char_to_multibyte (it->c);
18668 it->multibyte_p = 1;
18669 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18670 face = FACE_FROM_ID (it->f, it->face_id);
18671 }
18672 else if (!SINGLE_BYTE_CHAR_P (it->c)
18673 && !it->multibyte_p)
18674 {
18675 it->multibyte_p = 1;
18676 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18677 face = FACE_FROM_ID (it->f, it->face_id);
18678 }
18679 }
18680
18681 /* Get font to use. Encode IT->char_to_display. */
18682 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18683 &char2b, it->multibyte_p, 0);
18684 font = face->font;
18685
18686 /* When no suitable font found, use the default font. */
18687 font_not_found_p = font == NULL;
18688 if (font_not_found_p)
18689 {
18690 font = FRAME_FONT (it->f);
18691 boff = FRAME_BASELINE_OFFSET (it->f);
18692 font_info = NULL;
18693 }
18694 else
18695 {
18696 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18697 boff = font_info->baseline_offset;
18698 if (font_info->vertical_centering)
18699 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18700 }
18701
18702 if (it->char_to_display >= ' '
18703 && (!it->multibyte_p || it->char_to_display < 128))
18704 {
18705 /* Either unibyte or ASCII. */
18706 int stretched_p;
18707
18708 it->nglyphs = 1;
18709
18710 pcm = FRAME_RIF (it->f)->per_char_metric
18711 (font, &char2b, FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
18712
18713 if (it->override_ascent >= 0)
18714 {
18715 it->ascent = it->override_ascent;
18716 it->descent = it->override_descent;
18717 boff = it->override_boff;
18718 }
18719 else
18720 {
18721 it->ascent = FONT_BASE (font) + boff;
18722 it->descent = FONT_DESCENT (font) - boff;
18723 }
18724
18725 if (pcm)
18726 {
18727 it->phys_ascent = pcm->ascent + boff;
18728 it->phys_descent = pcm->descent - boff;
18729 it->pixel_width = pcm->width;
18730 }
18731 else
18732 {
18733 it->glyph_not_available_p = 1;
18734 it->phys_ascent = it->ascent;
18735 it->phys_descent = it->descent;
18736 it->pixel_width = FONT_WIDTH (font);
18737 }
18738
18739 if (it->constrain_row_ascent_descent_p)
18740 {
18741 if (it->descent > it->max_descent)
18742 {
18743 it->ascent += it->descent - it->max_descent;
18744 it->descent = it->max_descent;
18745 }
18746 if (it->ascent > it->max_ascent)
18747 {
18748 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
18749 it->ascent = it->max_ascent;
18750 }
18751 it->phys_ascent = min (it->phys_ascent, it->ascent);
18752 it->phys_descent = min (it->phys_descent, it->descent);
18753 extra_line_spacing = 0;
18754 }
18755
18756 /* If this is a space inside a region of text with
18757 `space-width' property, change its width. */
18758 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
18759 if (stretched_p)
18760 it->pixel_width *= XFLOATINT (it->space_width);
18761
18762 /* If face has a box, add the box thickness to the character
18763 height. If character has a box line to the left and/or
18764 right, add the box line width to the character's width. */
18765 if (face->box != FACE_NO_BOX)
18766 {
18767 int thick = face->box_line_width;
18768
18769 if (thick > 0)
18770 {
18771 it->ascent += thick;
18772 it->descent += thick;
18773 }
18774 else
18775 thick = -thick;
18776
18777 if (it->start_of_box_run_p)
18778 it->pixel_width += thick;
18779 if (it->end_of_box_run_p)
18780 it->pixel_width += thick;
18781 }
18782
18783 /* If face has an overline, add the height of the overline
18784 (1 pixel) and a 1 pixel margin to the character height. */
18785 if (face->overline_p)
18786 it->ascent += 2;
18787
18788 if (it->constrain_row_ascent_descent_p)
18789 {
18790 if (it->ascent > it->max_ascent)
18791 it->ascent = it->max_ascent;
18792 if (it->descent > it->max_descent)
18793 it->descent = it->max_descent;
18794 }
18795
18796 take_vertical_position_into_account (it);
18797
18798 /* If we have to actually produce glyphs, do it. */
18799 if (it->glyph_row)
18800 {
18801 if (stretched_p)
18802 {
18803 /* Translate a space with a `space-width' property
18804 into a stretch glyph. */
18805 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
18806 / FONT_HEIGHT (font));
18807 append_stretch_glyph (it, it->object, it->pixel_width,
18808 it->ascent + it->descent, ascent);
18809 }
18810 else
18811 append_glyph (it);
18812
18813 /* If characters with lbearing or rbearing are displayed
18814 in this line, record that fact in a flag of the
18815 glyph row. This is used to optimize X output code. */
18816 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
18817 it->glyph_row->contains_overlapping_glyphs_p = 1;
18818 }
18819 }
18820 else if (it->char_to_display == '\n')
18821 {
18822 /* A newline has no width but we need the height of the line.
18823 But if previous part of the line set a height, don't
18824 increase that height */
18825
18826 Lisp_Object height;
18827
18828 it->override_ascent = -1;
18829 it->pixel_width = 0;
18830 it->nglyphs = 0;
18831
18832 height = calc_line_height_property(it, Qline_height, font, boff, 0);
18833
18834 if (it->override_ascent >= 0)
18835 {
18836 it->ascent = it->override_ascent;
18837 it->descent = it->override_descent;
18838 boff = it->override_boff;
18839 }
18840 else
18841 {
18842 it->ascent = FONT_BASE (font) + boff;
18843 it->descent = FONT_DESCENT (font) - boff;
18844 }
18845
18846 if (EQ (height, make_number(0)))
18847 {
18848 if (it->descent > it->max_descent)
18849 {
18850 it->ascent += it->descent - it->max_descent;
18851 it->descent = it->max_descent;
18852 }
18853 if (it->ascent > it->max_ascent)
18854 {
18855 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
18856 it->ascent = it->max_ascent;
18857 }
18858 it->phys_ascent = min (it->phys_ascent, it->ascent);
18859 it->phys_descent = min (it->phys_descent, it->descent);
18860 it->constrain_row_ascent_descent_p = 1;
18861 extra_line_spacing = 0;
18862 }
18863 else
18864 {
18865 Lisp_Object spacing;
18866 int total = 0;
18867
18868 it->phys_ascent = it->ascent;
18869 it->phys_descent = it->descent;
18870
18871 if ((it->max_ascent > 0 || it->max_descent > 0)
18872 && face->box != FACE_NO_BOX
18873 && face->box_line_width > 0)
18874 {
18875 it->ascent += face->box_line_width;
18876 it->descent += face->box_line_width;
18877 }
18878 if (!NILP (height)
18879 && XINT (height) > it->ascent + it->descent)
18880 it->ascent = XINT (height) - it->descent;
18881
18882 spacing = calc_line_height_property(it, Qline_spacing, font, boff, &total);
18883 if (INTEGERP (spacing))
18884 {
18885 extra_line_spacing = XINT (spacing);
18886 if (total)
18887 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
18888 }
18889 }
18890 }
18891 else if (it->char_to_display == '\t')
18892 {
18893 int tab_width = it->tab_width * FRAME_COLUMN_WIDTH (it->f);
18894 int x = it->current_x + it->continuation_lines_width;
18895 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
18896
18897 /* If the distance from the current position to the next tab
18898 stop is less than a canonical character width, use the
18899 tab stop after that. */
18900 if (next_tab_x - x < FRAME_COLUMN_WIDTH (it->f))
18901 next_tab_x += tab_width;
18902
18903 it->pixel_width = next_tab_x - x;
18904 it->nglyphs = 1;
18905 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
18906 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
18907
18908 if (it->glyph_row)
18909 {
18910 append_stretch_glyph (it, it->object, it->pixel_width,
18911 it->ascent + it->descent, it->ascent);
18912 }
18913 }
18914 else
18915 {
18916 /* A multi-byte character. Assume that the display width of the
18917 character is the width of the character multiplied by the
18918 width of the font. */
18919
18920 /* If we found a font, this font should give us the right
18921 metrics. If we didn't find a font, use the frame's
18922 default font and calculate the width of the character
18923 from the charset width; this is what old redisplay code
18924 did. */
18925
18926 pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
18927 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
18928
18929 if (font_not_found_p || !pcm)
18930 {
18931 int charset = CHAR_CHARSET (it->char_to_display);
18932
18933 it->glyph_not_available_p = 1;
18934 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
18935 * CHARSET_WIDTH (charset));
18936 it->phys_ascent = FONT_BASE (font) + boff;
18937 it->phys_descent = FONT_DESCENT (font) - boff;
18938 }
18939 else
18940 {
18941 it->pixel_width = pcm->width;
18942 it->phys_ascent = pcm->ascent + boff;
18943 it->phys_descent = pcm->descent - boff;
18944 if (it->glyph_row
18945 && (pcm->lbearing < 0
18946 || pcm->rbearing > pcm->width))
18947 it->glyph_row->contains_overlapping_glyphs_p = 1;
18948 }
18949 it->nglyphs = 1;
18950 it->ascent = FONT_BASE (font) + boff;
18951 it->descent = FONT_DESCENT (font) - boff;
18952 if (face->box != FACE_NO_BOX)
18953 {
18954 int thick = face->box_line_width;
18955
18956 if (thick > 0)
18957 {
18958 it->ascent += thick;
18959 it->descent += thick;
18960 }
18961 else
18962 thick = - thick;
18963
18964 if (it->start_of_box_run_p)
18965 it->pixel_width += thick;
18966 if (it->end_of_box_run_p)
18967 it->pixel_width += thick;
18968 }
18969
18970 /* If face has an overline, add the height of the overline
18971 (1 pixel) and a 1 pixel margin to the character height. */
18972 if (face->overline_p)
18973 it->ascent += 2;
18974
18975 take_vertical_position_into_account (it);
18976
18977 if (it->glyph_row)
18978 append_glyph (it);
18979 }
18980 it->multibyte_p = saved_multibyte_p;
18981 }
18982 else if (it->what == IT_COMPOSITION)
18983 {
18984 /* Note: A composition is represented as one glyph in the
18985 glyph matrix. There are no padding glyphs. */
18986 XChar2b char2b;
18987 XFontStruct *font;
18988 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18989 XCharStruct *pcm;
18990 int font_not_found_p;
18991 struct font_info *font_info;
18992 int boff; /* baseline offset */
18993 struct composition *cmp = composition_table[it->cmp_id];
18994
18995 /* Maybe translate single-byte characters to multibyte. */
18996 it->char_to_display = it->c;
18997 if (unibyte_display_via_language_environment
18998 && SINGLE_BYTE_CHAR_P (it->c)
18999 && (it->c >= 0240
19000 || (it->c >= 0200
19001 && !NILP (Vnonascii_translation_table))))
19002 {
19003 it->char_to_display = unibyte_char_to_multibyte (it->c);
19004 }
19005
19006 /* Get face and font to use. Encode IT->char_to_display. */
19007 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19008 face = FACE_FROM_ID (it->f, it->face_id);
19009 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19010 &char2b, it->multibyte_p, 0);
19011 font = face->font;
19012
19013 /* When no suitable font found, use the default font. */
19014 font_not_found_p = font == NULL;
19015 if (font_not_found_p)
19016 {
19017 font = FRAME_FONT (it->f);
19018 boff = FRAME_BASELINE_OFFSET (it->f);
19019 font_info = NULL;
19020 }
19021 else
19022 {
19023 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19024 boff = font_info->baseline_offset;
19025 if (font_info->vertical_centering)
19026 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19027 }
19028
19029 /* There are no padding glyphs, so there is only one glyph to
19030 produce for the composition. Important is that pixel_width,
19031 ascent and descent are the values of what is drawn by
19032 draw_glyphs (i.e. the values of the overall glyphs composed). */
19033 it->nglyphs = 1;
19034
19035 /* If we have not yet calculated pixel size data of glyphs of
19036 the composition for the current face font, calculate them
19037 now. Theoretically, we have to check all fonts for the
19038 glyphs, but that requires much time and memory space. So,
19039 here we check only the font of the first glyph. This leads
19040 to incorrect display very rarely, and C-l (recenter) can
19041 correct the display anyway. */
19042 if (cmp->font != (void *) font)
19043 {
19044 /* Ascent and descent of the font of the first character of
19045 this composition (adjusted by baseline offset). Ascent
19046 and descent of overall glyphs should not be less than
19047 them respectively. */
19048 int font_ascent = FONT_BASE (font) + boff;
19049 int font_descent = FONT_DESCENT (font) - boff;
19050 /* Bounding box of the overall glyphs. */
19051 int leftmost, rightmost, lowest, highest;
19052 int i, width, ascent, descent;
19053
19054 cmp->font = (void *) font;
19055
19056 /* Initialize the bounding box. */
19057 if (font_info
19058 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
19059 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19060 {
19061 width = pcm->width;
19062 ascent = pcm->ascent;
19063 descent = pcm->descent;
19064 }
19065 else
19066 {
19067 width = FONT_WIDTH (font);
19068 ascent = FONT_BASE (font);
19069 descent = FONT_DESCENT (font);
19070 }
19071
19072 rightmost = width;
19073 lowest = - descent + boff;
19074 highest = ascent + boff;
19075 leftmost = 0;
19076
19077 if (font_info
19078 && font_info->default_ascent
19079 && CHAR_TABLE_P (Vuse_default_ascent)
19080 && !NILP (Faref (Vuse_default_ascent,
19081 make_number (it->char_to_display))))
19082 highest = font_info->default_ascent + boff;
19083
19084 /* Draw the first glyph at the normal position. It may be
19085 shifted to right later if some other glyphs are drawn at
19086 the left. */
19087 cmp->offsets[0] = 0;
19088 cmp->offsets[1] = boff;
19089
19090 /* Set cmp->offsets for the remaining glyphs. */
19091 for (i = 1; i < cmp->glyph_len; i++)
19092 {
19093 int left, right, btm, top;
19094 int ch = COMPOSITION_GLYPH (cmp, i);
19095 int face_id = FACE_FOR_CHAR (it->f, face, ch);
19096
19097 face = FACE_FROM_ID (it->f, face_id);
19098 get_char_face_and_encoding (it->f, ch, face->id,
19099 &char2b, it->multibyte_p, 0);
19100 font = face->font;
19101 if (font == NULL)
19102 {
19103 font = FRAME_FONT (it->f);
19104 boff = FRAME_BASELINE_OFFSET (it->f);
19105 font_info = NULL;
19106 }
19107 else
19108 {
19109 font_info
19110 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19111 boff = font_info->baseline_offset;
19112 if (font_info->vertical_centering)
19113 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19114 }
19115
19116 if (font_info
19117 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
19118 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19119 {
19120 width = pcm->width;
19121 ascent = pcm->ascent;
19122 descent = pcm->descent;
19123 }
19124 else
19125 {
19126 width = FONT_WIDTH (font);
19127 ascent = 1;
19128 descent = 0;
19129 }
19130
19131 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19132 {
19133 /* Relative composition with or without
19134 alternate chars. */
19135 left = (leftmost + rightmost - width) / 2;
19136 btm = - descent + boff;
19137 if (font_info && font_info->relative_compose
19138 && (! CHAR_TABLE_P (Vignore_relative_composition)
19139 || NILP (Faref (Vignore_relative_composition,
19140 make_number (ch)))))
19141 {
19142
19143 if (- descent >= font_info->relative_compose)
19144 /* One extra pixel between two glyphs. */
19145 btm = highest + 1;
19146 else if (ascent <= 0)
19147 /* One extra pixel between two glyphs. */
19148 btm = lowest - 1 - ascent - descent;
19149 }
19150 }
19151 else
19152 {
19153 /* A composition rule is specified by an integer
19154 value that encodes global and new reference
19155 points (GREF and NREF). GREF and NREF are
19156 specified by numbers as below:
19157
19158 0---1---2 -- ascent
19159 | |
19160 | |
19161 | |
19162 9--10--11 -- center
19163 | |
19164 ---3---4---5--- baseline
19165 | |
19166 6---7---8 -- descent
19167 */
19168 int rule = COMPOSITION_RULE (cmp, i);
19169 int gref, nref, grefx, grefy, nrefx, nrefy;
19170
19171 COMPOSITION_DECODE_RULE (rule, gref, nref);
19172 grefx = gref % 3, nrefx = nref % 3;
19173 grefy = gref / 3, nrefy = nref / 3;
19174
19175 left = (leftmost
19176 + grefx * (rightmost - leftmost) / 2
19177 - nrefx * width / 2);
19178 btm = ((grefy == 0 ? highest
19179 : grefy == 1 ? 0
19180 : grefy == 2 ? lowest
19181 : (highest + lowest) / 2)
19182 - (nrefy == 0 ? ascent + descent
19183 : nrefy == 1 ? descent - boff
19184 : nrefy == 2 ? 0
19185 : (ascent + descent) / 2));
19186 }
19187
19188 cmp->offsets[i * 2] = left;
19189 cmp->offsets[i * 2 + 1] = btm + descent;
19190
19191 /* Update the bounding box of the overall glyphs. */
19192 right = left + width;
19193 top = btm + descent + ascent;
19194 if (left < leftmost)
19195 leftmost = left;
19196 if (right > rightmost)
19197 rightmost = right;
19198 if (top > highest)
19199 highest = top;
19200 if (btm < lowest)
19201 lowest = btm;
19202 }
19203
19204 /* If there are glyphs whose x-offsets are negative,
19205 shift all glyphs to the right and make all x-offsets
19206 non-negative. */
19207 if (leftmost < 0)
19208 {
19209 for (i = 0; i < cmp->glyph_len; i++)
19210 cmp->offsets[i * 2] -= leftmost;
19211 rightmost -= leftmost;
19212 }
19213
19214 cmp->pixel_width = rightmost;
19215 cmp->ascent = highest;
19216 cmp->descent = - lowest;
19217 if (cmp->ascent < font_ascent)
19218 cmp->ascent = font_ascent;
19219 if (cmp->descent < font_descent)
19220 cmp->descent = font_descent;
19221 }
19222
19223 it->pixel_width = cmp->pixel_width;
19224 it->ascent = it->phys_ascent = cmp->ascent;
19225 it->descent = it->phys_descent = cmp->descent;
19226
19227 if (face->box != FACE_NO_BOX)
19228 {
19229 int thick = face->box_line_width;
19230
19231 if (thick > 0)
19232 {
19233 it->ascent += thick;
19234 it->descent += thick;
19235 }
19236 else
19237 thick = - thick;
19238
19239 if (it->start_of_box_run_p)
19240 it->pixel_width += thick;
19241 if (it->end_of_box_run_p)
19242 it->pixel_width += thick;
19243 }
19244
19245 /* If face has an overline, add the height of the overline
19246 (1 pixel) and a 1 pixel margin to the character height. */
19247 if (face->overline_p)
19248 it->ascent += 2;
19249
19250 take_vertical_position_into_account (it);
19251
19252 if (it->glyph_row)
19253 append_composite_glyph (it);
19254 }
19255 else if (it->what == IT_IMAGE)
19256 produce_image_glyph (it);
19257 else if (it->what == IT_STRETCH)
19258 produce_stretch_glyph (it);
19259
19260 /* Accumulate dimensions. Note: can't assume that it->descent > 0
19261 because this isn't true for images with `:ascent 100'. */
19262 xassert (it->ascent >= 0 && it->descent >= 0);
19263 if (it->area == TEXT_AREA)
19264 it->current_x += it->pixel_width;
19265
19266 if (extra_line_spacing > 0)
19267 it->descent += extra_line_spacing;
19268
19269 it->max_ascent = max (it->max_ascent, it->ascent);
19270 it->max_descent = max (it->max_descent, it->descent);
19271 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
19272 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
19273 }
19274
19275 /* EXPORT for RIF:
19276 Output LEN glyphs starting at START at the nominal cursor position.
19277 Advance the nominal cursor over the text. The global variable
19278 updated_window contains the window being updated, updated_row is
19279 the glyph row being updated, and updated_area is the area of that
19280 row being updated. */
19281
19282 void
19283 x_write_glyphs (start, len)
19284 struct glyph *start;
19285 int len;
19286 {
19287 int x, hpos;
19288
19289 xassert (updated_window && updated_row);
19290 BLOCK_INPUT;
19291
19292 /* Write glyphs. */
19293
19294 hpos = start - updated_row->glyphs[updated_area];
19295 x = draw_glyphs (updated_window, output_cursor.x,
19296 updated_row, updated_area,
19297 hpos, hpos + len,
19298 DRAW_NORMAL_TEXT, 0);
19299
19300 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
19301 if (updated_area == TEXT_AREA
19302 && updated_window->phys_cursor_on_p
19303 && updated_window->phys_cursor.vpos == output_cursor.vpos
19304 && updated_window->phys_cursor.hpos >= hpos
19305 && updated_window->phys_cursor.hpos < hpos + len)
19306 updated_window->phys_cursor_on_p = 0;
19307
19308 UNBLOCK_INPUT;
19309
19310 /* Advance the output cursor. */
19311 output_cursor.hpos += len;
19312 output_cursor.x = x;
19313 }
19314
19315
19316 /* EXPORT for RIF:
19317 Insert LEN glyphs from START at the nominal cursor position. */
19318
19319 void
19320 x_insert_glyphs (start, len)
19321 struct glyph *start;
19322 int len;
19323 {
19324 struct frame *f;
19325 struct window *w;
19326 int line_height, shift_by_width, shifted_region_width;
19327 struct glyph_row *row;
19328 struct glyph *glyph;
19329 int frame_x, frame_y, hpos;
19330
19331 xassert (updated_window && updated_row);
19332 BLOCK_INPUT;
19333 w = updated_window;
19334 f = XFRAME (WINDOW_FRAME (w));
19335
19336 /* Get the height of the line we are in. */
19337 row = updated_row;
19338 line_height = row->height;
19339
19340 /* Get the width of the glyphs to insert. */
19341 shift_by_width = 0;
19342 for (glyph = start; glyph < start + len; ++glyph)
19343 shift_by_width += glyph->pixel_width;
19344
19345 /* Get the width of the region to shift right. */
19346 shifted_region_width = (window_box_width (w, updated_area)
19347 - output_cursor.x
19348 - shift_by_width);
19349
19350 /* Shift right. */
19351 frame_x = window_box_left (w, updated_area) + output_cursor.x;
19352 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
19353
19354 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
19355 line_height, shift_by_width);
19356
19357 /* Write the glyphs. */
19358 hpos = start - row->glyphs[updated_area];
19359 draw_glyphs (w, output_cursor.x, row, updated_area,
19360 hpos, hpos + len,
19361 DRAW_NORMAL_TEXT, 0);
19362
19363 /* Advance the output cursor. */
19364 output_cursor.hpos += len;
19365 output_cursor.x += shift_by_width;
19366 UNBLOCK_INPUT;
19367 }
19368
19369
19370 /* EXPORT for RIF:
19371 Erase the current text line from the nominal cursor position
19372 (inclusive) to pixel column TO_X (exclusive). The idea is that
19373 everything from TO_X onward is already erased.
19374
19375 TO_X is a pixel position relative to updated_area of
19376 updated_window. TO_X == -1 means clear to the end of this area. */
19377
19378 void
19379 x_clear_end_of_line (to_x)
19380 int to_x;
19381 {
19382 struct frame *f;
19383 struct window *w = updated_window;
19384 int max_x, min_y, max_y;
19385 int from_x, from_y, to_y;
19386
19387 xassert (updated_window && updated_row);
19388 f = XFRAME (w->frame);
19389
19390 if (updated_row->full_width_p)
19391 max_x = WINDOW_TOTAL_WIDTH (w);
19392 else
19393 max_x = window_box_width (w, updated_area);
19394 max_y = window_text_bottom_y (w);
19395
19396 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
19397 of window. For TO_X > 0, truncate to end of drawing area. */
19398 if (to_x == 0)
19399 return;
19400 else if (to_x < 0)
19401 to_x = max_x;
19402 else
19403 to_x = min (to_x, max_x);
19404
19405 to_y = min (max_y, output_cursor.y + updated_row->height);
19406
19407 /* Notice if the cursor will be cleared by this operation. */
19408 if (!updated_row->full_width_p)
19409 notice_overwritten_cursor (w, updated_area,
19410 output_cursor.x, -1,
19411 updated_row->y,
19412 MATRIX_ROW_BOTTOM_Y (updated_row));
19413
19414 from_x = output_cursor.x;
19415
19416 /* Translate to frame coordinates. */
19417 if (updated_row->full_width_p)
19418 {
19419 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
19420 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
19421 }
19422 else
19423 {
19424 int area_left = window_box_left (w, updated_area);
19425 from_x += area_left;
19426 to_x += area_left;
19427 }
19428
19429 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
19430 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
19431 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
19432
19433 /* Prevent inadvertently clearing to end of the X window. */
19434 if (to_x > from_x && to_y > from_y)
19435 {
19436 BLOCK_INPUT;
19437 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
19438 to_x - from_x, to_y - from_y);
19439 UNBLOCK_INPUT;
19440 }
19441 }
19442
19443 #endif /* HAVE_WINDOW_SYSTEM */
19444
19445
19446 \f
19447 /***********************************************************************
19448 Cursor types
19449 ***********************************************************************/
19450
19451 /* Value is the internal representation of the specified cursor type
19452 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
19453 of the bar cursor. */
19454
19455 static enum text_cursor_kinds
19456 get_specified_cursor_type (arg, width)
19457 Lisp_Object arg;
19458 int *width;
19459 {
19460 enum text_cursor_kinds type;
19461
19462 if (NILP (arg))
19463 return NO_CURSOR;
19464
19465 if (EQ (arg, Qbox))
19466 return FILLED_BOX_CURSOR;
19467
19468 if (EQ (arg, Qhollow))
19469 return HOLLOW_BOX_CURSOR;
19470
19471 if (EQ (arg, Qbar))
19472 {
19473 *width = 2;
19474 return BAR_CURSOR;
19475 }
19476
19477 if (CONSP (arg)
19478 && EQ (XCAR (arg), Qbar)
19479 && INTEGERP (XCDR (arg))
19480 && XINT (XCDR (arg)) >= 0)
19481 {
19482 *width = XINT (XCDR (arg));
19483 return BAR_CURSOR;
19484 }
19485
19486 if (EQ (arg, Qhbar))
19487 {
19488 *width = 2;
19489 return HBAR_CURSOR;
19490 }
19491
19492 if (CONSP (arg)
19493 && EQ (XCAR (arg), Qhbar)
19494 && INTEGERP (XCDR (arg))
19495 && XINT (XCDR (arg)) >= 0)
19496 {
19497 *width = XINT (XCDR (arg));
19498 return HBAR_CURSOR;
19499 }
19500
19501 /* Treat anything unknown as "hollow box cursor".
19502 It was bad to signal an error; people have trouble fixing
19503 .Xdefaults with Emacs, when it has something bad in it. */
19504 type = HOLLOW_BOX_CURSOR;
19505
19506 return type;
19507 }
19508
19509 /* Set the default cursor types for specified frame. */
19510 void
19511 set_frame_cursor_types (f, arg)
19512 struct frame *f;
19513 Lisp_Object arg;
19514 {
19515 int width;
19516 Lisp_Object tem;
19517
19518 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
19519 FRAME_CURSOR_WIDTH (f) = width;
19520
19521 /* By default, set up the blink-off state depending on the on-state. */
19522
19523 tem = Fassoc (arg, Vblink_cursor_alist);
19524 if (!NILP (tem))
19525 {
19526 FRAME_BLINK_OFF_CURSOR (f)
19527 = get_specified_cursor_type (XCDR (tem), &width);
19528 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
19529 }
19530 else
19531 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
19532 }
19533
19534
19535 /* Return the cursor we want to be displayed in window W. Return
19536 width of bar/hbar cursor through WIDTH arg. Return with
19537 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
19538 (i.e. if the `system caret' should track this cursor).
19539
19540 In a mini-buffer window, we want the cursor only to appear if we
19541 are reading input from this window. For the selected window, we
19542 want the cursor type given by the frame parameter or buffer local
19543 setting of cursor-type. If explicitly marked off, draw no cursor.
19544 In all other cases, we want a hollow box cursor. */
19545
19546 static enum text_cursor_kinds
19547 get_window_cursor_type (w, glyph, width, active_cursor)
19548 struct window *w;
19549 struct glyph *glyph;
19550 int *width;
19551 int *active_cursor;
19552 {
19553 struct frame *f = XFRAME (w->frame);
19554 struct buffer *b = XBUFFER (w->buffer);
19555 int cursor_type = DEFAULT_CURSOR;
19556 Lisp_Object alt_cursor;
19557 int non_selected = 0;
19558
19559 *active_cursor = 1;
19560
19561 /* Echo area */
19562 if (cursor_in_echo_area
19563 && FRAME_HAS_MINIBUF_P (f)
19564 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
19565 {
19566 if (w == XWINDOW (echo_area_window))
19567 {
19568 *width = FRAME_CURSOR_WIDTH (f);
19569 return FRAME_DESIRED_CURSOR (f);
19570 }
19571
19572 *active_cursor = 0;
19573 non_selected = 1;
19574 }
19575
19576 /* Nonselected window or nonselected frame. */
19577 else if (w != XWINDOW (f->selected_window)
19578 #ifdef HAVE_WINDOW_SYSTEM
19579 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
19580 #endif
19581 )
19582 {
19583 *active_cursor = 0;
19584
19585 if (MINI_WINDOW_P (w) && minibuf_level == 0)
19586 return NO_CURSOR;
19587
19588 non_selected = 1;
19589 }
19590
19591 /* Never display a cursor in a window in which cursor-type is nil. */
19592 if (NILP (b->cursor_type))
19593 return NO_CURSOR;
19594
19595 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
19596 if (non_selected)
19597 {
19598 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
19599 return get_specified_cursor_type (alt_cursor, width);
19600 }
19601
19602 /* Get the normal cursor type for this window. */
19603 if (EQ (b->cursor_type, Qt))
19604 {
19605 cursor_type = FRAME_DESIRED_CURSOR (f);
19606 *width = FRAME_CURSOR_WIDTH (f);
19607 }
19608 else
19609 cursor_type = get_specified_cursor_type (b->cursor_type, width);
19610
19611 /* Use normal cursor if not blinked off. */
19612 if (!w->cursor_off_p)
19613 {
19614 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
19615 if (cursor_type == FILLED_BOX_CURSOR)
19616 cursor_type = HOLLOW_BOX_CURSOR;
19617 }
19618 return cursor_type;
19619 }
19620
19621 /* Cursor is blinked off, so determine how to "toggle" it. */
19622
19623 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
19624 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
19625 return get_specified_cursor_type (XCDR (alt_cursor), width);
19626
19627 /* Then see if frame has specified a specific blink off cursor type. */
19628 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
19629 {
19630 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
19631 return FRAME_BLINK_OFF_CURSOR (f);
19632 }
19633
19634 #if 0
19635 /* Some people liked having a permanently visible blinking cursor,
19636 while others had very strong opinions against it. So it was
19637 decided to remove it. KFS 2003-09-03 */
19638
19639 /* Finally perform built-in cursor blinking:
19640 filled box <-> hollow box
19641 wide [h]bar <-> narrow [h]bar
19642 narrow [h]bar <-> no cursor
19643 other type <-> no cursor */
19644
19645 if (cursor_type == FILLED_BOX_CURSOR)
19646 return HOLLOW_BOX_CURSOR;
19647
19648 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
19649 {
19650 *width = 1;
19651 return cursor_type;
19652 }
19653 #endif
19654
19655 return NO_CURSOR;
19656 }
19657
19658
19659 #ifdef HAVE_WINDOW_SYSTEM
19660
19661 /* Notice when the text cursor of window W has been completely
19662 overwritten by a drawing operation that outputs glyphs in AREA
19663 starting at X0 and ending at X1 in the line starting at Y0 and
19664 ending at Y1. X coordinates are area-relative. X1 < 0 means all
19665 the rest of the line after X0 has been written. Y coordinates
19666 are window-relative. */
19667
19668 static void
19669 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
19670 struct window *w;
19671 enum glyph_row_area area;
19672 int x0, y0, x1, y1;
19673 {
19674 int cx0, cx1, cy0, cy1;
19675 struct glyph_row *row;
19676
19677 if (!w->phys_cursor_on_p)
19678 return;
19679 if (area != TEXT_AREA)
19680 return;
19681
19682 row = w->current_matrix->rows + w->phys_cursor.vpos;
19683 if (!row->displays_text_p)
19684 return;
19685
19686 if (row->cursor_in_fringe_p)
19687 {
19688 row->cursor_in_fringe_p = 0;
19689 draw_fringe_bitmap (w, row, 0);
19690 w->phys_cursor_on_p = 0;
19691 return;
19692 }
19693
19694 cx0 = w->phys_cursor.x;
19695 cx1 = cx0 + w->phys_cursor_width;
19696 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
19697 return;
19698
19699 /* The cursor image will be completely removed from the
19700 screen if the output area intersects the cursor area in
19701 y-direction. When we draw in [y0 y1[, and some part of
19702 the cursor is at y < y0, that part must have been drawn
19703 before. When scrolling, the cursor is erased before
19704 actually scrolling, so we don't come here. When not
19705 scrolling, the rows above the old cursor row must have
19706 changed, and in this case these rows must have written
19707 over the cursor image.
19708
19709 Likewise if part of the cursor is below y1, with the
19710 exception of the cursor being in the first blank row at
19711 the buffer and window end because update_text_area
19712 doesn't draw that row. (Except when it does, but
19713 that's handled in update_text_area.) */
19714
19715 cy0 = w->phys_cursor.y;
19716 cy1 = cy0 + w->phys_cursor_height;
19717 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
19718 return;
19719
19720 w->phys_cursor_on_p = 0;
19721 }
19722
19723 #endif /* HAVE_WINDOW_SYSTEM */
19724
19725 \f
19726 /************************************************************************
19727 Mouse Face
19728 ************************************************************************/
19729
19730 #ifdef HAVE_WINDOW_SYSTEM
19731
19732 /* EXPORT for RIF:
19733 Fix the display of area AREA of overlapping row ROW in window W. */
19734
19735 void
19736 x_fix_overlapping_area (w, row, area)
19737 struct window *w;
19738 struct glyph_row *row;
19739 enum glyph_row_area area;
19740 {
19741 int i, x;
19742
19743 BLOCK_INPUT;
19744
19745 x = 0;
19746 for (i = 0; i < row->used[area];)
19747 {
19748 if (row->glyphs[area][i].overlaps_vertically_p)
19749 {
19750 int start = i, start_x = x;
19751
19752 do
19753 {
19754 x += row->glyphs[area][i].pixel_width;
19755 ++i;
19756 }
19757 while (i < row->used[area]
19758 && row->glyphs[area][i].overlaps_vertically_p);
19759
19760 draw_glyphs (w, start_x, row, area,
19761 start, i,
19762 DRAW_NORMAL_TEXT, 1);
19763 }
19764 else
19765 {
19766 x += row->glyphs[area][i].pixel_width;
19767 ++i;
19768 }
19769 }
19770
19771 UNBLOCK_INPUT;
19772 }
19773
19774
19775 /* EXPORT:
19776 Draw the cursor glyph of window W in glyph row ROW. See the
19777 comment of draw_glyphs for the meaning of HL. */
19778
19779 void
19780 draw_phys_cursor_glyph (w, row, hl)
19781 struct window *w;
19782 struct glyph_row *row;
19783 enum draw_glyphs_face hl;
19784 {
19785 /* If cursor hpos is out of bounds, don't draw garbage. This can
19786 happen in mini-buffer windows when switching between echo area
19787 glyphs and mini-buffer. */
19788 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
19789 {
19790 int on_p = w->phys_cursor_on_p;
19791 int x1;
19792 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
19793 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
19794 hl, 0);
19795 w->phys_cursor_on_p = on_p;
19796
19797 if (hl == DRAW_CURSOR)
19798 w->phys_cursor_width = x1 - w->phys_cursor.x;
19799 /* When we erase the cursor, and ROW is overlapped by other
19800 rows, make sure that these overlapping parts of other rows
19801 are redrawn. */
19802 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
19803 {
19804 if (row > w->current_matrix->rows
19805 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
19806 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
19807
19808 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
19809 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
19810 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
19811 }
19812 }
19813 }
19814
19815
19816 /* EXPORT:
19817 Erase the image of a cursor of window W from the screen. */
19818
19819 void
19820 erase_phys_cursor (w)
19821 struct window *w;
19822 {
19823 struct frame *f = XFRAME (w->frame);
19824 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
19825 int hpos = w->phys_cursor.hpos;
19826 int vpos = w->phys_cursor.vpos;
19827 int mouse_face_here_p = 0;
19828 struct glyph_matrix *active_glyphs = w->current_matrix;
19829 struct glyph_row *cursor_row;
19830 struct glyph *cursor_glyph;
19831 enum draw_glyphs_face hl;
19832
19833 /* No cursor displayed or row invalidated => nothing to do on the
19834 screen. */
19835 if (w->phys_cursor_type == NO_CURSOR)
19836 goto mark_cursor_off;
19837
19838 /* VPOS >= active_glyphs->nrows means that window has been resized.
19839 Don't bother to erase the cursor. */
19840 if (vpos >= active_glyphs->nrows)
19841 goto mark_cursor_off;
19842
19843 /* If row containing cursor is marked invalid, there is nothing we
19844 can do. */
19845 cursor_row = MATRIX_ROW (active_glyphs, vpos);
19846 if (!cursor_row->enabled_p)
19847 goto mark_cursor_off;
19848
19849 /* If row is completely invisible, don't attempt to delete a cursor which
19850 isn't there. This can happen if cursor is at top of a window, and
19851 we switch to a buffer with a header line in that window. */
19852 if (cursor_row->visible_height <= 0)
19853 goto mark_cursor_off;
19854
19855 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
19856 if (cursor_row->cursor_in_fringe_p)
19857 {
19858 cursor_row->cursor_in_fringe_p = 0;
19859 draw_fringe_bitmap (w, cursor_row, 0);
19860 goto mark_cursor_off;
19861 }
19862
19863 /* This can happen when the new row is shorter than the old one.
19864 In this case, either draw_glyphs or clear_end_of_line
19865 should have cleared the cursor. Note that we wouldn't be
19866 able to erase the cursor in this case because we don't have a
19867 cursor glyph at hand. */
19868 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
19869 goto mark_cursor_off;
19870
19871 /* If the cursor is in the mouse face area, redisplay that when
19872 we clear the cursor. */
19873 if (! NILP (dpyinfo->mouse_face_window)
19874 && w == XWINDOW (dpyinfo->mouse_face_window)
19875 && (vpos > dpyinfo->mouse_face_beg_row
19876 || (vpos == dpyinfo->mouse_face_beg_row
19877 && hpos >= dpyinfo->mouse_face_beg_col))
19878 && (vpos < dpyinfo->mouse_face_end_row
19879 || (vpos == dpyinfo->mouse_face_end_row
19880 && hpos < dpyinfo->mouse_face_end_col))
19881 /* Don't redraw the cursor's spot in mouse face if it is at the
19882 end of a line (on a newline). The cursor appears there, but
19883 mouse highlighting does not. */
19884 && cursor_row->used[TEXT_AREA] > hpos)
19885 mouse_face_here_p = 1;
19886
19887 /* Maybe clear the display under the cursor. */
19888 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
19889 {
19890 int x, y;
19891 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
19892
19893 cursor_glyph = get_phys_cursor_glyph (w);
19894 if (cursor_glyph == NULL)
19895 goto mark_cursor_off;
19896
19897 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
19898 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
19899
19900 FRAME_RIF (f)->clear_frame_area (f, x, y,
19901 cursor_glyph->pixel_width, cursor_row->visible_height);
19902 }
19903
19904 /* Erase the cursor by redrawing the character underneath it. */
19905 if (mouse_face_here_p)
19906 hl = DRAW_MOUSE_FACE;
19907 else
19908 hl = DRAW_NORMAL_TEXT;
19909 draw_phys_cursor_glyph (w, cursor_row, hl);
19910
19911 mark_cursor_off:
19912 w->phys_cursor_on_p = 0;
19913 w->phys_cursor_type = NO_CURSOR;
19914 }
19915
19916
19917 /* EXPORT:
19918 Display or clear cursor of window W. If ON is zero, clear the
19919 cursor. If it is non-zero, display the cursor. If ON is nonzero,
19920 where to put the cursor is specified by HPOS, VPOS, X and Y. */
19921
19922 void
19923 display_and_set_cursor (w, on, hpos, vpos, x, y)
19924 struct window *w;
19925 int on, hpos, vpos, x, y;
19926 {
19927 struct frame *f = XFRAME (w->frame);
19928 int new_cursor_type;
19929 int new_cursor_width;
19930 int active_cursor;
19931 struct glyph_row *glyph_row;
19932 struct glyph *glyph;
19933
19934 /* This is pointless on invisible frames, and dangerous on garbaged
19935 windows and frames; in the latter case, the frame or window may
19936 be in the midst of changing its size, and x and y may be off the
19937 window. */
19938 if (! FRAME_VISIBLE_P (f)
19939 || FRAME_GARBAGED_P (f)
19940 || vpos >= w->current_matrix->nrows
19941 || hpos >= w->current_matrix->matrix_w)
19942 return;
19943
19944 /* If cursor is off and we want it off, return quickly. */
19945 if (!on && !w->phys_cursor_on_p)
19946 return;
19947
19948 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
19949 /* If cursor row is not enabled, we don't really know where to
19950 display the cursor. */
19951 if (!glyph_row->enabled_p)
19952 {
19953 w->phys_cursor_on_p = 0;
19954 return;
19955 }
19956
19957 glyph = NULL;
19958 if (!glyph_row->exact_window_width_line_p
19959 || hpos < glyph_row->used[TEXT_AREA])
19960 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
19961
19962 xassert (interrupt_input_blocked);
19963
19964 /* Set new_cursor_type to the cursor we want to be displayed. */
19965 new_cursor_type = get_window_cursor_type (w, glyph,
19966 &new_cursor_width, &active_cursor);
19967
19968 /* If cursor is currently being shown and we don't want it to be or
19969 it is in the wrong place, or the cursor type is not what we want,
19970 erase it. */
19971 if (w->phys_cursor_on_p
19972 && (!on
19973 || w->phys_cursor.x != x
19974 || w->phys_cursor.y != y
19975 || new_cursor_type != w->phys_cursor_type
19976 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
19977 && new_cursor_width != w->phys_cursor_width)))
19978 erase_phys_cursor (w);
19979
19980 /* Don't check phys_cursor_on_p here because that flag is only set
19981 to zero in some cases where we know that the cursor has been
19982 completely erased, to avoid the extra work of erasing the cursor
19983 twice. In other words, phys_cursor_on_p can be 1 and the cursor
19984 still not be visible, or it has only been partly erased. */
19985 if (on)
19986 {
19987 w->phys_cursor_ascent = glyph_row->ascent;
19988 w->phys_cursor_height = glyph_row->height;
19989
19990 /* Set phys_cursor_.* before x_draw_.* is called because some
19991 of them may need the information. */
19992 w->phys_cursor.x = x;
19993 w->phys_cursor.y = glyph_row->y;
19994 w->phys_cursor.hpos = hpos;
19995 w->phys_cursor.vpos = vpos;
19996 }
19997
19998 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
19999 new_cursor_type, new_cursor_width,
20000 on, active_cursor);
20001 }
20002
20003
20004 /* Switch the display of W's cursor on or off, according to the value
20005 of ON. */
20006
20007 static void
20008 update_window_cursor (w, on)
20009 struct window *w;
20010 int on;
20011 {
20012 /* Don't update cursor in windows whose frame is in the process
20013 of being deleted. */
20014 if (w->current_matrix)
20015 {
20016 BLOCK_INPUT;
20017 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20018 w->phys_cursor.x, w->phys_cursor.y);
20019 UNBLOCK_INPUT;
20020 }
20021 }
20022
20023
20024 /* Call update_window_cursor with parameter ON_P on all leaf windows
20025 in the window tree rooted at W. */
20026
20027 static void
20028 update_cursor_in_window_tree (w, on_p)
20029 struct window *w;
20030 int on_p;
20031 {
20032 while (w)
20033 {
20034 if (!NILP (w->hchild))
20035 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20036 else if (!NILP (w->vchild))
20037 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20038 else
20039 update_window_cursor (w, on_p);
20040
20041 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20042 }
20043 }
20044
20045
20046 /* EXPORT:
20047 Display the cursor on window W, or clear it, according to ON_P.
20048 Don't change the cursor's position. */
20049
20050 void
20051 x_update_cursor (f, on_p)
20052 struct frame *f;
20053 int on_p;
20054 {
20055 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20056 }
20057
20058
20059 /* EXPORT:
20060 Clear the cursor of window W to background color, and mark the
20061 cursor as not shown. This is used when the text where the cursor
20062 is is about to be rewritten. */
20063
20064 void
20065 x_clear_cursor (w)
20066 struct window *w;
20067 {
20068 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20069 update_window_cursor (w, 0);
20070 }
20071
20072
20073 /* EXPORT:
20074 Display the active region described by mouse_face_* according to DRAW. */
20075
20076 void
20077 show_mouse_face (dpyinfo, draw)
20078 Display_Info *dpyinfo;
20079 enum draw_glyphs_face draw;
20080 {
20081 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20082 struct frame *f = XFRAME (WINDOW_FRAME (w));
20083
20084 if (/* If window is in the process of being destroyed, don't bother
20085 to do anything. */
20086 w->current_matrix != NULL
20087 /* Don't update mouse highlight if hidden */
20088 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20089 /* Recognize when we are called to operate on rows that don't exist
20090 anymore. This can happen when a window is split. */
20091 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20092 {
20093 int phys_cursor_on_p = w->phys_cursor_on_p;
20094 struct glyph_row *row, *first, *last;
20095
20096 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20097 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20098
20099 for (row = first; row <= last && row->enabled_p; ++row)
20100 {
20101 int start_hpos, end_hpos, start_x;
20102
20103 /* For all but the first row, the highlight starts at column 0. */
20104 if (row == first)
20105 {
20106 start_hpos = dpyinfo->mouse_face_beg_col;
20107 start_x = dpyinfo->mouse_face_beg_x;
20108 }
20109 else
20110 {
20111 start_hpos = 0;
20112 start_x = 0;
20113 }
20114
20115 if (row == last)
20116 end_hpos = dpyinfo->mouse_face_end_col;
20117 else
20118 end_hpos = row->used[TEXT_AREA];
20119
20120 if (end_hpos > start_hpos)
20121 {
20122 draw_glyphs (w, start_x, row, TEXT_AREA,
20123 start_hpos, end_hpos,
20124 draw, 0);
20125
20126 row->mouse_face_p
20127 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20128 }
20129 }
20130
20131 /* When we've written over the cursor, arrange for it to
20132 be displayed again. */
20133 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20134 {
20135 BLOCK_INPUT;
20136 display_and_set_cursor (w, 1,
20137 w->phys_cursor.hpos, w->phys_cursor.vpos,
20138 w->phys_cursor.x, w->phys_cursor.y);
20139 UNBLOCK_INPUT;
20140 }
20141 }
20142
20143 /* Change the mouse cursor. */
20144 if (draw == DRAW_NORMAL_TEXT)
20145 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
20146 else if (draw == DRAW_MOUSE_FACE)
20147 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
20148 else
20149 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
20150 }
20151
20152 /* EXPORT:
20153 Clear out the mouse-highlighted active region.
20154 Redraw it un-highlighted first. Value is non-zero if mouse
20155 face was actually drawn unhighlighted. */
20156
20157 int
20158 clear_mouse_face (dpyinfo)
20159 Display_Info *dpyinfo;
20160 {
20161 int cleared = 0;
20162
20163 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
20164 {
20165 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
20166 cleared = 1;
20167 }
20168
20169 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20170 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20171 dpyinfo->mouse_face_window = Qnil;
20172 dpyinfo->mouse_face_overlay = Qnil;
20173 return cleared;
20174 }
20175
20176
20177 /* EXPORT:
20178 Non-zero if physical cursor of window W is within mouse face. */
20179
20180 int
20181 cursor_in_mouse_face_p (w)
20182 struct window *w;
20183 {
20184 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20185 int in_mouse_face = 0;
20186
20187 if (WINDOWP (dpyinfo->mouse_face_window)
20188 && XWINDOW (dpyinfo->mouse_face_window) == w)
20189 {
20190 int hpos = w->phys_cursor.hpos;
20191 int vpos = w->phys_cursor.vpos;
20192
20193 if (vpos >= dpyinfo->mouse_face_beg_row
20194 && vpos <= dpyinfo->mouse_face_end_row
20195 && (vpos > dpyinfo->mouse_face_beg_row
20196 || hpos >= dpyinfo->mouse_face_beg_col)
20197 && (vpos < dpyinfo->mouse_face_end_row
20198 || hpos < dpyinfo->mouse_face_end_col
20199 || dpyinfo->mouse_face_past_end))
20200 in_mouse_face = 1;
20201 }
20202
20203 return in_mouse_face;
20204 }
20205
20206
20207
20208 \f
20209 /* Find the glyph matrix position of buffer position CHARPOS in window
20210 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
20211 current glyphs must be up to date. If CHARPOS is above window
20212 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
20213 of last line in W. In the row containing CHARPOS, stop before glyphs
20214 having STOP as object. */
20215
20216 #if 1 /* This is a version of fast_find_position that's more correct
20217 in the presence of hscrolling, for example. I didn't install
20218 it right away because the problem fixed is minor, it failed
20219 in 20.x as well, and I think it's too risky to install
20220 so near the release of 21.1. 2001-09-25 gerd. */
20221
20222 static int
20223 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
20224 struct window *w;
20225 int charpos;
20226 int *hpos, *vpos, *x, *y;
20227 Lisp_Object stop;
20228 {
20229 struct glyph_row *row, *first;
20230 struct glyph *glyph, *end;
20231 int past_end = 0;
20232
20233 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20234 row = row_containing_pos (w, charpos, first, NULL, 0);
20235 if (row == NULL)
20236 {
20237 if (charpos < MATRIX_ROW_START_CHARPOS (first))
20238 {
20239 *x = *y = *hpos = *vpos = 0;
20240 return 1;
20241 }
20242 else
20243 {
20244 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
20245 past_end = 1;
20246 }
20247 }
20248
20249 *x = row->x;
20250 *y = row->y;
20251 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20252
20253 glyph = row->glyphs[TEXT_AREA];
20254 end = glyph + row->used[TEXT_AREA];
20255
20256 /* Skip over glyphs not having an object at the start of the row.
20257 These are special glyphs like truncation marks on terminal
20258 frames. */
20259 if (row->displays_text_p)
20260 while (glyph < end
20261 && INTEGERP (glyph->object)
20262 && !EQ (stop, glyph->object)
20263 && glyph->charpos < 0)
20264 {
20265 *x += glyph->pixel_width;
20266 ++glyph;
20267 }
20268
20269 while (glyph < end
20270 && !INTEGERP (glyph->object)
20271 && !EQ (stop, glyph->object)
20272 && (!BUFFERP (glyph->object)
20273 || glyph->charpos < charpos))
20274 {
20275 *x += glyph->pixel_width;
20276 ++glyph;
20277 }
20278
20279 *hpos = glyph - row->glyphs[TEXT_AREA];
20280 return !past_end;
20281 }
20282
20283 #else /* not 1 */
20284
20285 static int
20286 fast_find_position (w, pos, hpos, vpos, x, y, stop)
20287 struct window *w;
20288 int pos;
20289 int *hpos, *vpos, *x, *y;
20290 Lisp_Object stop;
20291 {
20292 int i;
20293 int lastcol;
20294 int maybe_next_line_p = 0;
20295 int line_start_position;
20296 int yb = window_text_bottom_y (w);
20297 struct glyph_row *row, *best_row;
20298 int row_vpos, best_row_vpos;
20299 int current_x;
20300
20301 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20302 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20303
20304 while (row->y < yb)
20305 {
20306 if (row->used[TEXT_AREA])
20307 line_start_position = row->glyphs[TEXT_AREA]->charpos;
20308 else
20309 line_start_position = 0;
20310
20311 if (line_start_position > pos)
20312 break;
20313 /* If the position sought is the end of the buffer,
20314 don't include the blank lines at the bottom of the window. */
20315 else if (line_start_position == pos
20316 && pos == BUF_ZV (XBUFFER (w->buffer)))
20317 {
20318 maybe_next_line_p = 1;
20319 break;
20320 }
20321 else if (line_start_position > 0)
20322 {
20323 best_row = row;
20324 best_row_vpos = row_vpos;
20325 }
20326
20327 if (row->y + row->height >= yb)
20328 break;
20329
20330 ++row;
20331 ++row_vpos;
20332 }
20333
20334 /* Find the right column within BEST_ROW. */
20335 lastcol = 0;
20336 current_x = best_row->x;
20337 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
20338 {
20339 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
20340 int charpos = glyph->charpos;
20341
20342 if (BUFFERP (glyph->object))
20343 {
20344 if (charpos == pos)
20345 {
20346 *hpos = i;
20347 *vpos = best_row_vpos;
20348 *x = current_x;
20349 *y = best_row->y;
20350 return 1;
20351 }
20352 else if (charpos > pos)
20353 break;
20354 }
20355 else if (EQ (glyph->object, stop))
20356 break;
20357
20358 if (charpos > 0)
20359 lastcol = i;
20360 current_x += glyph->pixel_width;
20361 }
20362
20363 /* If we're looking for the end of the buffer,
20364 and we didn't find it in the line we scanned,
20365 use the start of the following line. */
20366 if (maybe_next_line_p)
20367 {
20368 ++best_row;
20369 ++best_row_vpos;
20370 lastcol = 0;
20371 current_x = best_row->x;
20372 }
20373
20374 *vpos = best_row_vpos;
20375 *hpos = lastcol + 1;
20376 *x = current_x;
20377 *y = best_row->y;
20378 return 0;
20379 }
20380
20381 #endif /* not 1 */
20382
20383
20384 /* Find the position of the glyph for position POS in OBJECT in
20385 window W's current matrix, and return in *X, *Y the pixel
20386 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
20387
20388 RIGHT_P non-zero means return the position of the right edge of the
20389 glyph, RIGHT_P zero means return the left edge position.
20390
20391 If no glyph for POS exists in the matrix, return the position of
20392 the glyph with the next smaller position that is in the matrix, if
20393 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
20394 exists in the matrix, return the position of the glyph with the
20395 next larger position in OBJECT.
20396
20397 Value is non-zero if a glyph was found. */
20398
20399 static int
20400 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
20401 struct window *w;
20402 int pos;
20403 Lisp_Object object;
20404 int *hpos, *vpos, *x, *y;
20405 int right_p;
20406 {
20407 int yb = window_text_bottom_y (w);
20408 struct glyph_row *r;
20409 struct glyph *best_glyph = NULL;
20410 struct glyph_row *best_row = NULL;
20411 int best_x = 0;
20412
20413 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20414 r->enabled_p && r->y < yb;
20415 ++r)
20416 {
20417 struct glyph *g = r->glyphs[TEXT_AREA];
20418 struct glyph *e = g + r->used[TEXT_AREA];
20419 int gx;
20420
20421 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
20422 if (EQ (g->object, object))
20423 {
20424 if (g->charpos == pos)
20425 {
20426 best_glyph = g;
20427 best_x = gx;
20428 best_row = r;
20429 goto found;
20430 }
20431 else if (best_glyph == NULL
20432 || ((abs (g->charpos - pos)
20433 < abs (best_glyph->charpos - pos))
20434 && (right_p
20435 ? g->charpos < pos
20436 : g->charpos > pos)))
20437 {
20438 best_glyph = g;
20439 best_x = gx;
20440 best_row = r;
20441 }
20442 }
20443 }
20444
20445 found:
20446
20447 if (best_glyph)
20448 {
20449 *x = best_x;
20450 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
20451
20452 if (right_p)
20453 {
20454 *x += best_glyph->pixel_width;
20455 ++*hpos;
20456 }
20457
20458 *y = best_row->y;
20459 *vpos = best_row - w->current_matrix->rows;
20460 }
20461
20462 return best_glyph != NULL;
20463 }
20464
20465
20466 /* See if position X, Y is within a hot-spot of an image. */
20467
20468 static int
20469 on_hot_spot_p (hot_spot, x, y)
20470 Lisp_Object hot_spot;
20471 int x, y;
20472 {
20473 if (!CONSP (hot_spot))
20474 return 0;
20475
20476 if (EQ (XCAR (hot_spot), Qrect))
20477 {
20478 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
20479 Lisp_Object rect = XCDR (hot_spot);
20480 Lisp_Object tem;
20481 if (!CONSP (rect))
20482 return 0;
20483 if (!CONSP (XCAR (rect)))
20484 return 0;
20485 if (!CONSP (XCDR (rect)))
20486 return 0;
20487 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
20488 return 0;
20489 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
20490 return 0;
20491 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
20492 return 0;
20493 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
20494 return 0;
20495 return 1;
20496 }
20497 else if (EQ (XCAR (hot_spot), Qcircle))
20498 {
20499 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
20500 Lisp_Object circ = XCDR (hot_spot);
20501 Lisp_Object lr, lx0, ly0;
20502 if (CONSP (circ)
20503 && CONSP (XCAR (circ))
20504 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
20505 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
20506 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
20507 {
20508 double r = XFLOATINT (lr);
20509 double dx = XINT (lx0) - x;
20510 double dy = XINT (ly0) - y;
20511 return (dx * dx + dy * dy <= r * r);
20512 }
20513 }
20514 else if (EQ (XCAR (hot_spot), Qpoly))
20515 {
20516 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
20517 if (VECTORP (XCDR (hot_spot)))
20518 {
20519 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
20520 Lisp_Object *poly = v->contents;
20521 int n = v->size;
20522 int i;
20523 int inside = 0;
20524 Lisp_Object lx, ly;
20525 int x0, y0;
20526
20527 /* Need an even number of coordinates, and at least 3 edges. */
20528 if (n < 6 || n & 1)
20529 return 0;
20530
20531 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
20532 If count is odd, we are inside polygon. Pixels on edges
20533 may or may not be included depending on actual geometry of the
20534 polygon. */
20535 if ((lx = poly[n-2], !INTEGERP (lx))
20536 || (ly = poly[n-1], !INTEGERP (lx)))
20537 return 0;
20538 x0 = XINT (lx), y0 = XINT (ly);
20539 for (i = 0; i < n; i += 2)
20540 {
20541 int x1 = x0, y1 = y0;
20542 if ((lx = poly[i], !INTEGERP (lx))
20543 || (ly = poly[i+1], !INTEGERP (ly)))
20544 return 0;
20545 x0 = XINT (lx), y0 = XINT (ly);
20546
20547 /* Does this segment cross the X line? */
20548 if (x0 >= x)
20549 {
20550 if (x1 >= x)
20551 continue;
20552 }
20553 else if (x1 < x)
20554 continue;
20555 if (y > y0 && y > y1)
20556 continue;
20557 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
20558 inside = !inside;
20559 }
20560 return inside;
20561 }
20562 }
20563 return 0;
20564 }
20565
20566 Lisp_Object
20567 find_hot_spot (map, x, y)
20568 Lisp_Object map;
20569 int x, y;
20570 {
20571 while (CONSP (map))
20572 {
20573 if (CONSP (XCAR (map))
20574 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
20575 return XCAR (map);
20576 map = XCDR (map);
20577 }
20578
20579 return Qnil;
20580 }
20581
20582 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
20583 3, 3, 0,
20584 doc: /* Lookup in image map MAP coordinates X and Y.
20585 An image map is an alist where each element has the format (AREA ID PLIST).
20586 An AREA is specified as either a rectangle, a circle, or a polygon:
20587 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
20588 pixel coordinates of the upper left and bottom right corners.
20589 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
20590 and the radius of the circle; r may be a float or integer.
20591 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
20592 vector describes one corner in the polygon.
20593 Returns the alist element for the first matching AREA in MAP. */)
20594 (map, x, y)
20595 Lisp_Object map;
20596 Lisp_Object x, y;
20597 {
20598 if (NILP (map))
20599 return Qnil;
20600
20601 CHECK_NUMBER (x);
20602 CHECK_NUMBER (y);
20603
20604 return find_hot_spot (map, XINT (x), XINT (y));
20605 }
20606
20607
20608 /* Display frame CURSOR, optionally using shape defined by POINTER. */
20609 static void
20610 define_frame_cursor1 (f, cursor, pointer)
20611 struct frame *f;
20612 Cursor cursor;
20613 Lisp_Object pointer;
20614 {
20615 if (!NILP (pointer))
20616 {
20617 if (EQ (pointer, Qarrow))
20618 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20619 else if (EQ (pointer, Qhand))
20620 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
20621 else if (EQ (pointer, Qtext))
20622 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20623 else if (EQ (pointer, intern ("hdrag")))
20624 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
20625 #ifdef HAVE_X_WINDOWS
20626 else if (EQ (pointer, intern ("vdrag")))
20627 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
20628 #endif
20629 else if (EQ (pointer, intern ("hourglass")))
20630 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
20631 else if (EQ (pointer, Qmodeline))
20632 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
20633 else
20634 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20635 }
20636
20637 if (cursor != No_Cursor)
20638 FRAME_RIF (f)->define_frame_cursor (f, cursor);
20639 }
20640
20641 /* Take proper action when mouse has moved to the mode or header line
20642 or marginal area AREA of window W, x-position X and y-position Y.
20643 X is relative to the start of the text display area of W, so the
20644 width of bitmap areas and scroll bars must be subtracted to get a
20645 position relative to the start of the mode line. */
20646
20647 static void
20648 note_mode_line_or_margin_highlight (w, x, y, area)
20649 struct window *w;
20650 int x, y;
20651 enum window_part area;
20652 {
20653 struct frame *f = XFRAME (w->frame);
20654 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20655 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20656 Lisp_Object pointer = Qnil;
20657 int charpos, dx, dy, width, height;
20658 Lisp_Object string, object = Qnil;
20659 Lisp_Object pos, help;
20660
20661 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
20662 string = mode_line_string (w, area, &x, &y, &charpos,
20663 &object, &dx, &dy, &width, &height);
20664 else
20665 {
20666 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
20667 string = marginal_area_string (w, area, &x, &y, &charpos,
20668 &object, &dx, &dy, &width, &height);
20669 }
20670
20671 help = Qnil;
20672
20673 if (IMAGEP (object))
20674 {
20675 Lisp_Object image_map, hotspot;
20676 if ((image_map = Fplist_get (XCDR (object), QCmap),
20677 !NILP (image_map))
20678 && (hotspot = find_hot_spot (image_map, dx, dy),
20679 CONSP (hotspot))
20680 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
20681 {
20682 Lisp_Object area_id, plist;
20683
20684 area_id = XCAR (hotspot);
20685 /* Could check AREA_ID to see if we enter/leave this hot-spot.
20686 If so, we could look for mouse-enter, mouse-leave
20687 properties in PLIST (and do something...). */
20688 if ((plist = XCDR (hotspot), CONSP (plist)))
20689 {
20690 pointer = Fplist_get (plist, Qpointer);
20691 if (NILP (pointer))
20692 pointer = Qhand;
20693 help = Fplist_get (plist, Qhelp_echo);
20694 if (!NILP (help))
20695 {
20696 help_echo_string = help;
20697 /* Is this correct? ++kfs */
20698 XSETWINDOW (help_echo_window, w);
20699 help_echo_object = w->buffer;
20700 help_echo_pos = charpos;
20701 }
20702 }
20703 if (NILP (pointer))
20704 pointer = Fplist_get (XCDR (object), QCpointer);
20705 }
20706 }
20707
20708 if (STRINGP (string))
20709 {
20710 pos = make_number (charpos);
20711 /* If we're on a string with `help-echo' text property, arrange
20712 for the help to be displayed. This is done by setting the
20713 global variable help_echo_string to the help string. */
20714 help = Fget_text_property (pos, Qhelp_echo, string);
20715 if (!NILP (help))
20716 {
20717 help_echo_string = help;
20718 XSETWINDOW (help_echo_window, w);
20719 help_echo_object = string;
20720 help_echo_pos = charpos;
20721 }
20722
20723 if (NILP (pointer))
20724 pointer = Fget_text_property (pos, Qpointer, string);
20725
20726 /* Change the mouse pointer according to what is under X/Y. */
20727 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
20728 {
20729 Lisp_Object map;
20730 map = Fget_text_property (pos, Qlocal_map, string);
20731 if (!KEYMAPP (map))
20732 map = Fget_text_property (pos, Qkeymap, string);
20733 if (!KEYMAPP (map))
20734 cursor = dpyinfo->vertical_scroll_bar_cursor;
20735 }
20736 }
20737
20738 define_frame_cursor1 (f, cursor, pointer);
20739 }
20740
20741
20742 /* EXPORT:
20743 Take proper action when the mouse has moved to position X, Y on
20744 frame F as regards highlighting characters that have mouse-face
20745 properties. Also de-highlighting chars where the mouse was before.
20746 X and Y can be negative or out of range. */
20747
20748 void
20749 note_mouse_highlight (f, x, y)
20750 struct frame *f;
20751 int x, y;
20752 {
20753 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20754 enum window_part part;
20755 Lisp_Object window;
20756 struct window *w;
20757 Cursor cursor = No_Cursor;
20758 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
20759 struct buffer *b;
20760
20761 /* When a menu is active, don't highlight because this looks odd. */
20762 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
20763 if (popup_activated ())
20764 return;
20765 #endif
20766
20767 if (NILP (Vmouse_highlight)
20768 || !f->glyphs_initialized_p)
20769 return;
20770
20771 dpyinfo->mouse_face_mouse_x = x;
20772 dpyinfo->mouse_face_mouse_y = y;
20773 dpyinfo->mouse_face_mouse_frame = f;
20774
20775 if (dpyinfo->mouse_face_defer)
20776 return;
20777
20778 if (gc_in_progress)
20779 {
20780 dpyinfo->mouse_face_deferred_gc = 1;
20781 return;
20782 }
20783
20784 /* Which window is that in? */
20785 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
20786
20787 /* If we were displaying active text in another window, clear that. */
20788 if (! EQ (window, dpyinfo->mouse_face_window))
20789 clear_mouse_face (dpyinfo);
20790
20791 /* Not on a window -> return. */
20792 if (!WINDOWP (window))
20793 return;
20794
20795 /* Reset help_echo_string. It will get recomputed below. */
20796 help_echo_string = Qnil;
20797
20798 /* Convert to window-relative pixel coordinates. */
20799 w = XWINDOW (window);
20800 frame_to_window_pixel_xy (w, &x, &y);
20801
20802 /* Handle tool-bar window differently since it doesn't display a
20803 buffer. */
20804 if (EQ (window, f->tool_bar_window))
20805 {
20806 note_tool_bar_highlight (f, x, y);
20807 return;
20808 }
20809
20810 /* Mouse is on the mode, header line or margin? */
20811 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
20812 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
20813 {
20814 note_mode_line_or_margin_highlight (w, x, y, part);
20815 return;
20816 }
20817
20818 if (part == ON_VERTICAL_BORDER)
20819 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
20820 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE)
20821 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20822 else
20823 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20824
20825 /* Are we in a window whose display is up to date?
20826 And verify the buffer's text has not changed. */
20827 b = XBUFFER (w->buffer);
20828 if (part == ON_TEXT
20829 && EQ (w->window_end_valid, w->buffer)
20830 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
20831 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
20832 {
20833 int hpos, vpos, pos, i, dx, dy, area;
20834 struct glyph *glyph;
20835 Lisp_Object object;
20836 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
20837 Lisp_Object *overlay_vec = NULL;
20838 int noverlays;
20839 struct buffer *obuf;
20840 int obegv, ozv, same_region;
20841
20842 /* Find the glyph under X/Y. */
20843 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
20844
20845 /* Look for :pointer property on image. */
20846 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
20847 {
20848 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
20849 if (img != NULL && IMAGEP (img->spec))
20850 {
20851 Lisp_Object image_map, hotspot;
20852 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
20853 !NILP (image_map))
20854 && (hotspot = find_hot_spot (image_map,
20855 glyph->slice.x + dx,
20856 glyph->slice.y + dy),
20857 CONSP (hotspot))
20858 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
20859 {
20860 Lisp_Object area_id, plist;
20861
20862 area_id = XCAR (hotspot);
20863 /* Could check AREA_ID to see if we enter/leave this hot-spot.
20864 If so, we could look for mouse-enter, mouse-leave
20865 properties in PLIST (and do something...). */
20866 if ((plist = XCDR (hotspot), CONSP (plist)))
20867 {
20868 pointer = Fplist_get (plist, Qpointer);
20869 if (NILP (pointer))
20870 pointer = Qhand;
20871 help_echo_string = Fplist_get (plist, Qhelp_echo);
20872 if (!NILP (help_echo_string))
20873 {
20874 help_echo_window = window;
20875 help_echo_object = glyph->object;
20876 help_echo_pos = glyph->charpos;
20877 }
20878 }
20879 }
20880 if (NILP (pointer))
20881 pointer = Fplist_get (XCDR (img->spec), QCpointer);
20882 }
20883 }
20884
20885 /* Clear mouse face if X/Y not over text. */
20886 if (glyph == NULL
20887 || area != TEXT_AREA
20888 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
20889 {
20890 if (clear_mouse_face (dpyinfo))
20891 cursor = No_Cursor;
20892 if (NILP (pointer))
20893 {
20894 if (area != TEXT_AREA)
20895 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20896 else
20897 pointer = Vvoid_text_area_pointer;
20898 }
20899 goto set_cursor;
20900 }
20901
20902 pos = glyph->charpos;
20903 object = glyph->object;
20904 if (!STRINGP (object) && !BUFFERP (object))
20905 goto set_cursor;
20906
20907 /* If we get an out-of-range value, return now; avoid an error. */
20908 if (BUFFERP (object) && pos > BUF_Z (b))
20909 goto set_cursor;
20910
20911 /* Make the window's buffer temporarily current for
20912 overlays_at and compute_char_face. */
20913 obuf = current_buffer;
20914 current_buffer = b;
20915 obegv = BEGV;
20916 ozv = ZV;
20917 BEGV = BEG;
20918 ZV = Z;
20919
20920 /* Is this char mouse-active or does it have help-echo? */
20921 position = make_number (pos);
20922
20923 if (BUFFERP (object))
20924 {
20925 /* Put all the overlays we want in a vector in overlay_vec. */
20926 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
20927 /* Sort overlays into increasing priority order. */
20928 noverlays = sort_overlays (overlay_vec, noverlays, w);
20929 }
20930 else
20931 noverlays = 0;
20932
20933 same_region = (EQ (window, dpyinfo->mouse_face_window)
20934 && vpos >= dpyinfo->mouse_face_beg_row
20935 && vpos <= dpyinfo->mouse_face_end_row
20936 && (vpos > dpyinfo->mouse_face_beg_row
20937 || hpos >= dpyinfo->mouse_face_beg_col)
20938 && (vpos < dpyinfo->mouse_face_end_row
20939 || hpos < dpyinfo->mouse_face_end_col
20940 || dpyinfo->mouse_face_past_end));
20941
20942 if (same_region)
20943 cursor = No_Cursor;
20944
20945 /* Check mouse-face highlighting. */
20946 if (! same_region
20947 /* If there exists an overlay with mouse-face overlapping
20948 the one we are currently highlighting, we have to
20949 check if we enter the overlapping overlay, and then
20950 highlight only that. */
20951 || (OVERLAYP (dpyinfo->mouse_face_overlay)
20952 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
20953 {
20954 /* Find the highest priority overlay that has a mouse-face
20955 property. */
20956 overlay = Qnil;
20957 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
20958 {
20959 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
20960 if (!NILP (mouse_face))
20961 overlay = overlay_vec[i];
20962 }
20963
20964 /* If we're actually highlighting the same overlay as
20965 before, there's no need to do that again. */
20966 if (!NILP (overlay)
20967 && EQ (overlay, dpyinfo->mouse_face_overlay))
20968 goto check_help_echo;
20969
20970 dpyinfo->mouse_face_overlay = overlay;
20971
20972 /* Clear the display of the old active region, if any. */
20973 if (clear_mouse_face (dpyinfo))
20974 cursor = No_Cursor;
20975
20976 /* If no overlay applies, get a text property. */
20977 if (NILP (overlay))
20978 mouse_face = Fget_text_property (position, Qmouse_face, object);
20979
20980 /* Handle the overlay case. */
20981 if (!NILP (overlay))
20982 {
20983 /* Find the range of text around this char that
20984 should be active. */
20985 Lisp_Object before, after;
20986 int ignore;
20987
20988 before = Foverlay_start (overlay);
20989 after = Foverlay_end (overlay);
20990 /* Record this as the current active region. */
20991 fast_find_position (w, XFASTINT (before),
20992 &dpyinfo->mouse_face_beg_col,
20993 &dpyinfo->mouse_face_beg_row,
20994 &dpyinfo->mouse_face_beg_x,
20995 &dpyinfo->mouse_face_beg_y, Qnil);
20996
20997 dpyinfo->mouse_face_past_end
20998 = !fast_find_position (w, XFASTINT (after),
20999 &dpyinfo->mouse_face_end_col,
21000 &dpyinfo->mouse_face_end_row,
21001 &dpyinfo->mouse_face_end_x,
21002 &dpyinfo->mouse_face_end_y, Qnil);
21003 dpyinfo->mouse_face_window = window;
21004
21005 dpyinfo->mouse_face_face_id
21006 = face_at_buffer_position (w, pos, 0, 0,
21007 &ignore, pos + 1,
21008 !dpyinfo->mouse_face_hidden);
21009
21010 /* Display it as active. */
21011 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21012 cursor = No_Cursor;
21013 }
21014 /* Handle the text property case. */
21015 else if (!NILP (mouse_face) && BUFFERP (object))
21016 {
21017 /* Find the range of text around this char that
21018 should be active. */
21019 Lisp_Object before, after, beginning, end;
21020 int ignore;
21021
21022 beginning = Fmarker_position (w->start);
21023 end = make_number (BUF_Z (XBUFFER (object))
21024 - XFASTINT (w->window_end_pos));
21025 before
21026 = Fprevious_single_property_change (make_number (pos + 1),
21027 Qmouse_face,
21028 object, beginning);
21029 after
21030 = Fnext_single_property_change (position, Qmouse_face,
21031 object, end);
21032
21033 /* Record this as the current active region. */
21034 fast_find_position (w, XFASTINT (before),
21035 &dpyinfo->mouse_face_beg_col,
21036 &dpyinfo->mouse_face_beg_row,
21037 &dpyinfo->mouse_face_beg_x,
21038 &dpyinfo->mouse_face_beg_y, Qnil);
21039 dpyinfo->mouse_face_past_end
21040 = !fast_find_position (w, XFASTINT (after),
21041 &dpyinfo->mouse_face_end_col,
21042 &dpyinfo->mouse_face_end_row,
21043 &dpyinfo->mouse_face_end_x,
21044 &dpyinfo->mouse_face_end_y, Qnil);
21045 dpyinfo->mouse_face_window = window;
21046
21047 if (BUFFERP (object))
21048 dpyinfo->mouse_face_face_id
21049 = face_at_buffer_position (w, pos, 0, 0,
21050 &ignore, pos + 1,
21051 !dpyinfo->mouse_face_hidden);
21052
21053 /* Display it as active. */
21054 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21055 cursor = No_Cursor;
21056 }
21057 else if (!NILP (mouse_face) && STRINGP (object))
21058 {
21059 Lisp_Object b, e;
21060 int ignore;
21061
21062 b = Fprevious_single_property_change (make_number (pos + 1),
21063 Qmouse_face,
21064 object, Qnil);
21065 e = Fnext_single_property_change (position, Qmouse_face,
21066 object, Qnil);
21067 if (NILP (b))
21068 b = make_number (0);
21069 if (NILP (e))
21070 e = make_number (SCHARS (object) - 1);
21071 fast_find_string_pos (w, XINT (b), object,
21072 &dpyinfo->mouse_face_beg_col,
21073 &dpyinfo->mouse_face_beg_row,
21074 &dpyinfo->mouse_face_beg_x,
21075 &dpyinfo->mouse_face_beg_y, 0);
21076 fast_find_string_pos (w, XINT (e), object,
21077 &dpyinfo->mouse_face_end_col,
21078 &dpyinfo->mouse_face_end_row,
21079 &dpyinfo->mouse_face_end_x,
21080 &dpyinfo->mouse_face_end_y, 1);
21081 dpyinfo->mouse_face_past_end = 0;
21082 dpyinfo->mouse_face_window = window;
21083 dpyinfo->mouse_face_face_id
21084 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
21085 glyph->face_id, 1);
21086 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21087 cursor = No_Cursor;
21088 }
21089 else if (STRINGP (object) && NILP (mouse_face))
21090 {
21091 /* A string which doesn't have mouse-face, but
21092 the text ``under'' it might have. */
21093 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
21094 int start = MATRIX_ROW_START_CHARPOS (r);
21095
21096 pos = string_buffer_position (w, object, start);
21097 if (pos > 0)
21098 mouse_face = get_char_property_and_overlay (make_number (pos),
21099 Qmouse_face,
21100 w->buffer,
21101 &overlay);
21102 if (!NILP (mouse_face) && !NILP (overlay))
21103 {
21104 Lisp_Object before = Foverlay_start (overlay);
21105 Lisp_Object after = Foverlay_end (overlay);
21106 int ignore;
21107
21108 /* Note that we might not be able to find position
21109 BEFORE in the glyph matrix if the overlay is
21110 entirely covered by a `display' property. In
21111 this case, we overshoot. So let's stop in
21112 the glyph matrix before glyphs for OBJECT. */
21113 fast_find_position (w, XFASTINT (before),
21114 &dpyinfo->mouse_face_beg_col,
21115 &dpyinfo->mouse_face_beg_row,
21116 &dpyinfo->mouse_face_beg_x,
21117 &dpyinfo->mouse_face_beg_y,
21118 object);
21119
21120 dpyinfo->mouse_face_past_end
21121 = !fast_find_position (w, XFASTINT (after),
21122 &dpyinfo->mouse_face_end_col,
21123 &dpyinfo->mouse_face_end_row,
21124 &dpyinfo->mouse_face_end_x,
21125 &dpyinfo->mouse_face_end_y,
21126 Qnil);
21127 dpyinfo->mouse_face_window = window;
21128 dpyinfo->mouse_face_face_id
21129 = face_at_buffer_position (w, pos, 0, 0,
21130 &ignore, pos + 1,
21131 !dpyinfo->mouse_face_hidden);
21132
21133 /* Display it as active. */
21134 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21135 cursor = No_Cursor;
21136 }
21137 }
21138 }
21139
21140 check_help_echo:
21141
21142 /* Look for a `help-echo' property. */
21143 if (NILP (help_echo_string)) {
21144 Lisp_Object help, overlay;
21145
21146 /* Check overlays first. */
21147 help = overlay = Qnil;
21148 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
21149 {
21150 overlay = overlay_vec[i];
21151 help = Foverlay_get (overlay, Qhelp_echo);
21152 }
21153
21154 if (!NILP (help))
21155 {
21156 help_echo_string = help;
21157 help_echo_window = window;
21158 help_echo_object = overlay;
21159 help_echo_pos = pos;
21160 }
21161 else
21162 {
21163 Lisp_Object object = glyph->object;
21164 int charpos = glyph->charpos;
21165
21166 /* Try text properties. */
21167 if (STRINGP (object)
21168 && charpos >= 0
21169 && charpos < SCHARS (object))
21170 {
21171 help = Fget_text_property (make_number (charpos),
21172 Qhelp_echo, object);
21173 if (NILP (help))
21174 {
21175 /* If the string itself doesn't specify a help-echo,
21176 see if the buffer text ``under'' it does. */
21177 struct glyph_row *r
21178 = MATRIX_ROW (w->current_matrix, vpos);
21179 int start = MATRIX_ROW_START_CHARPOS (r);
21180 int pos = string_buffer_position (w, object, start);
21181 if (pos > 0)
21182 {
21183 help = Fget_char_property (make_number (pos),
21184 Qhelp_echo, w->buffer);
21185 if (!NILP (help))
21186 {
21187 charpos = pos;
21188 object = w->buffer;
21189 }
21190 }
21191 }
21192 }
21193 else if (BUFFERP (object)
21194 && charpos >= BEGV
21195 && charpos < ZV)
21196 help = Fget_text_property (make_number (charpos), Qhelp_echo,
21197 object);
21198
21199 if (!NILP (help))
21200 {
21201 help_echo_string = help;
21202 help_echo_window = window;
21203 help_echo_object = object;
21204 help_echo_pos = charpos;
21205 }
21206 }
21207 }
21208
21209 /* Look for a `pointer' property. */
21210 if (NILP (pointer))
21211 {
21212 /* Check overlays first. */
21213 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
21214 pointer = Foverlay_get (overlay_vec[i], Qpointer);
21215
21216 if (NILP (pointer))
21217 {
21218 Lisp_Object object = glyph->object;
21219 int charpos = glyph->charpos;
21220
21221 /* Try text properties. */
21222 if (STRINGP (object)
21223 && charpos >= 0
21224 && charpos < SCHARS (object))
21225 {
21226 pointer = Fget_text_property (make_number (charpos),
21227 Qpointer, object);
21228 if (NILP (pointer))
21229 {
21230 /* If the string itself doesn't specify a pointer,
21231 see if the buffer text ``under'' it does. */
21232 struct glyph_row *r
21233 = MATRIX_ROW (w->current_matrix, vpos);
21234 int start = MATRIX_ROW_START_CHARPOS (r);
21235 int pos = string_buffer_position (w, object, start);
21236 if (pos > 0)
21237 pointer = Fget_char_property (make_number (pos),
21238 Qpointer, w->buffer);
21239 }
21240 }
21241 else if (BUFFERP (object)
21242 && charpos >= BEGV
21243 && charpos < ZV)
21244 pointer = Fget_text_property (make_number (charpos),
21245 Qpointer, object);
21246 }
21247 }
21248
21249 BEGV = obegv;
21250 ZV = ozv;
21251 current_buffer = obuf;
21252 }
21253
21254 set_cursor:
21255
21256 define_frame_cursor1 (f, cursor, pointer);
21257 }
21258
21259
21260 /* EXPORT for RIF:
21261 Clear any mouse-face on window W. This function is part of the
21262 redisplay interface, and is called from try_window_id and similar
21263 functions to ensure the mouse-highlight is off. */
21264
21265 void
21266 x_clear_window_mouse_face (w)
21267 struct window *w;
21268 {
21269 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21270 Lisp_Object window;
21271
21272 BLOCK_INPUT;
21273 XSETWINDOW (window, w);
21274 if (EQ (window, dpyinfo->mouse_face_window))
21275 clear_mouse_face (dpyinfo);
21276 UNBLOCK_INPUT;
21277 }
21278
21279
21280 /* EXPORT:
21281 Just discard the mouse face information for frame F, if any.
21282 This is used when the size of F is changed. */
21283
21284 void
21285 cancel_mouse_face (f)
21286 struct frame *f;
21287 {
21288 Lisp_Object window;
21289 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21290
21291 window = dpyinfo->mouse_face_window;
21292 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
21293 {
21294 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21295 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21296 dpyinfo->mouse_face_window = Qnil;
21297 }
21298 }
21299
21300
21301 #endif /* HAVE_WINDOW_SYSTEM */
21302
21303 \f
21304 /***********************************************************************
21305 Exposure Events
21306 ***********************************************************************/
21307
21308 #ifdef HAVE_WINDOW_SYSTEM
21309
21310 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
21311 which intersects rectangle R. R is in window-relative coordinates. */
21312
21313 static void
21314 expose_area (w, row, r, area)
21315 struct window *w;
21316 struct glyph_row *row;
21317 XRectangle *r;
21318 enum glyph_row_area area;
21319 {
21320 struct glyph *first = row->glyphs[area];
21321 struct glyph *end = row->glyphs[area] + row->used[area];
21322 struct glyph *last;
21323 int first_x, start_x, x;
21324
21325 if (area == TEXT_AREA && row->fill_line_p)
21326 /* If row extends face to end of line write the whole line. */
21327 draw_glyphs (w, 0, row, area,
21328 0, row->used[area],
21329 DRAW_NORMAL_TEXT, 0);
21330 else
21331 {
21332 /* Set START_X to the window-relative start position for drawing glyphs of
21333 AREA. The first glyph of the text area can be partially visible.
21334 The first glyphs of other areas cannot. */
21335 start_x = window_box_left_offset (w, area);
21336 x = start_x;
21337 if (area == TEXT_AREA)
21338 x += row->x;
21339
21340 /* Find the first glyph that must be redrawn. */
21341 while (first < end
21342 && x + first->pixel_width < r->x)
21343 {
21344 x += first->pixel_width;
21345 ++first;
21346 }
21347
21348 /* Find the last one. */
21349 last = first;
21350 first_x = x;
21351 while (last < end
21352 && x < r->x + r->width)
21353 {
21354 x += last->pixel_width;
21355 ++last;
21356 }
21357
21358 /* Repaint. */
21359 if (last > first)
21360 draw_glyphs (w, first_x - start_x, row, area,
21361 first - row->glyphs[area], last - row->glyphs[area],
21362 DRAW_NORMAL_TEXT, 0);
21363 }
21364 }
21365
21366
21367 /* Redraw the parts of the glyph row ROW on window W intersecting
21368 rectangle R. R is in window-relative coordinates. Value is
21369 non-zero if mouse-face was overwritten. */
21370
21371 static int
21372 expose_line (w, row, r)
21373 struct window *w;
21374 struct glyph_row *row;
21375 XRectangle *r;
21376 {
21377 xassert (row->enabled_p);
21378
21379 if (row->mode_line_p || w->pseudo_window_p)
21380 draw_glyphs (w, 0, row, TEXT_AREA,
21381 0, row->used[TEXT_AREA],
21382 DRAW_NORMAL_TEXT, 0);
21383 else
21384 {
21385 if (row->used[LEFT_MARGIN_AREA])
21386 expose_area (w, row, r, LEFT_MARGIN_AREA);
21387 if (row->used[TEXT_AREA])
21388 expose_area (w, row, r, TEXT_AREA);
21389 if (row->used[RIGHT_MARGIN_AREA])
21390 expose_area (w, row, r, RIGHT_MARGIN_AREA);
21391 draw_row_fringe_bitmaps (w, row);
21392 }
21393
21394 return row->mouse_face_p;
21395 }
21396
21397
21398 /* Redraw those parts of glyphs rows during expose event handling that
21399 overlap other rows. Redrawing of an exposed line writes over parts
21400 of lines overlapping that exposed line; this function fixes that.
21401
21402 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
21403 row in W's current matrix that is exposed and overlaps other rows.
21404 LAST_OVERLAPPING_ROW is the last such row. */
21405
21406 static void
21407 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
21408 struct window *w;
21409 struct glyph_row *first_overlapping_row;
21410 struct glyph_row *last_overlapping_row;
21411 {
21412 struct glyph_row *row;
21413
21414 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
21415 if (row->overlapping_p)
21416 {
21417 xassert (row->enabled_p && !row->mode_line_p);
21418
21419 if (row->used[LEFT_MARGIN_AREA])
21420 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
21421
21422 if (row->used[TEXT_AREA])
21423 x_fix_overlapping_area (w, row, TEXT_AREA);
21424
21425 if (row->used[RIGHT_MARGIN_AREA])
21426 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
21427 }
21428 }
21429
21430
21431 /* Return non-zero if W's cursor intersects rectangle R. */
21432
21433 static int
21434 phys_cursor_in_rect_p (w, r)
21435 struct window *w;
21436 XRectangle *r;
21437 {
21438 XRectangle cr, result;
21439 struct glyph *cursor_glyph;
21440
21441 cursor_glyph = get_phys_cursor_glyph (w);
21442 if (cursor_glyph)
21443 {
21444 /* r is relative to W's box, but w->phys_cursor.x is relative
21445 to left edge of W's TEXT area. Adjust it. */
21446 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
21447 cr.y = w->phys_cursor.y;
21448 cr.width = cursor_glyph->pixel_width;
21449 cr.height = w->phys_cursor_height;
21450 /* ++KFS: W32 version used W32-specific IntersectRect here, but
21451 I assume the effect is the same -- and this is portable. */
21452 return x_intersect_rectangles (&cr, r, &result);
21453 }
21454 /* If we don't understand the format, pretend we're not in the hot-spot. */
21455 return 0;
21456 }
21457
21458
21459 /* EXPORT:
21460 Draw a vertical window border to the right of window W if W doesn't
21461 have vertical scroll bars. */
21462
21463 void
21464 x_draw_vertical_border (w)
21465 struct window *w;
21466 {
21467 struct frame *f = XFRAME (WINDOW_FRAME (w));
21468
21469 /* We could do better, if we knew what type of scroll-bar the adjacent
21470 windows (on either side) have... But we don't :-(
21471 However, I think this works ok. ++KFS 2003-04-25 */
21472
21473 /* Redraw borders between horizontally adjacent windows. Don't
21474 do it for frames with vertical scroll bars because either the
21475 right scroll bar of a window, or the left scroll bar of its
21476 neighbor will suffice as a border. */
21477 if (!WINDOW_RIGHTMOST_P (w)
21478 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
21479 {
21480 int x0, x1, y0, y1;
21481
21482 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21483 y1 -= 1;
21484
21485 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
21486 }
21487 else if (!WINDOW_LEFTMOST_P (w)
21488 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
21489 {
21490 int x0, x1, y0, y1;
21491
21492 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21493 y1 -= 1;
21494
21495 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
21496 }
21497 }
21498
21499
21500 /* Redraw the part of window W intersection rectangle FR. Pixel
21501 coordinates in FR are frame-relative. Call this function with
21502 input blocked. Value is non-zero if the exposure overwrites
21503 mouse-face. */
21504
21505 static int
21506 expose_window (w, fr)
21507 struct window *w;
21508 XRectangle *fr;
21509 {
21510 struct frame *f = XFRAME (w->frame);
21511 XRectangle wr, r;
21512 int mouse_face_overwritten_p = 0;
21513
21514 /* If window is not yet fully initialized, do nothing. This can
21515 happen when toolkit scroll bars are used and a window is split.
21516 Reconfiguring the scroll bar will generate an expose for a newly
21517 created window. */
21518 if (w->current_matrix == NULL)
21519 return 0;
21520
21521 /* When we're currently updating the window, display and current
21522 matrix usually don't agree. Arrange for a thorough display
21523 later. */
21524 if (w == updated_window)
21525 {
21526 SET_FRAME_GARBAGED (f);
21527 return 0;
21528 }
21529
21530 /* Frame-relative pixel rectangle of W. */
21531 wr.x = WINDOW_LEFT_EDGE_X (w);
21532 wr.y = WINDOW_TOP_EDGE_Y (w);
21533 wr.width = WINDOW_TOTAL_WIDTH (w);
21534 wr.height = WINDOW_TOTAL_HEIGHT (w);
21535
21536 if (x_intersect_rectangles (fr, &wr, &r))
21537 {
21538 int yb = window_text_bottom_y (w);
21539 struct glyph_row *row;
21540 int cursor_cleared_p;
21541 struct glyph_row *first_overlapping_row, *last_overlapping_row;
21542
21543 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
21544 r.x, r.y, r.width, r.height));
21545
21546 /* Convert to window coordinates. */
21547 r.x -= WINDOW_LEFT_EDGE_X (w);
21548 r.y -= WINDOW_TOP_EDGE_Y (w);
21549
21550 /* Turn off the cursor. */
21551 if (!w->pseudo_window_p
21552 && phys_cursor_in_rect_p (w, &r))
21553 {
21554 x_clear_cursor (w);
21555 cursor_cleared_p = 1;
21556 }
21557 else
21558 cursor_cleared_p = 0;
21559
21560 /* Update lines intersecting rectangle R. */
21561 first_overlapping_row = last_overlapping_row = NULL;
21562 for (row = w->current_matrix->rows;
21563 row->enabled_p;
21564 ++row)
21565 {
21566 int y0 = row->y;
21567 int y1 = MATRIX_ROW_BOTTOM_Y (row);
21568
21569 if ((y0 >= r.y && y0 < r.y + r.height)
21570 || (y1 > r.y && y1 < r.y + r.height)
21571 || (r.y >= y0 && r.y < y1)
21572 || (r.y + r.height > y0 && r.y + r.height < y1))
21573 {
21574 if (row->overlapping_p)
21575 {
21576 if (first_overlapping_row == NULL)
21577 first_overlapping_row = row;
21578 last_overlapping_row = row;
21579 }
21580
21581 if (expose_line (w, row, &r))
21582 mouse_face_overwritten_p = 1;
21583 }
21584
21585 if (y1 >= yb)
21586 break;
21587 }
21588
21589 /* Display the mode line if there is one. */
21590 if (WINDOW_WANTS_MODELINE_P (w)
21591 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
21592 row->enabled_p)
21593 && row->y < r.y + r.height)
21594 {
21595 if (expose_line (w, row, &r))
21596 mouse_face_overwritten_p = 1;
21597 }
21598
21599 if (!w->pseudo_window_p)
21600 {
21601 /* Fix the display of overlapping rows. */
21602 if (first_overlapping_row)
21603 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
21604
21605 /* Draw border between windows. */
21606 x_draw_vertical_border (w);
21607
21608 /* Turn the cursor on again. */
21609 if (cursor_cleared_p)
21610 update_window_cursor (w, 1);
21611 }
21612 }
21613
21614 #ifdef HAVE_CARBON
21615 /* Display scroll bar for this window. */
21616 if (!NILP (w->vertical_scroll_bar))
21617 {
21618 /* ++KFS:
21619 If this doesn't work here (maybe some header files are missing),
21620 make a function in macterm.c and call it to do the job! */
21621 ControlHandle ch
21622 = SCROLL_BAR_CONTROL_HANDLE (XSCROLL_BAR (w->vertical_scroll_bar));
21623
21624 Draw1Control (ch);
21625 }
21626 #endif
21627
21628 return mouse_face_overwritten_p;
21629 }
21630
21631
21632
21633 /* Redraw (parts) of all windows in the window tree rooted at W that
21634 intersect R. R contains frame pixel coordinates. Value is
21635 non-zero if the exposure overwrites mouse-face. */
21636
21637 static int
21638 expose_window_tree (w, r)
21639 struct window *w;
21640 XRectangle *r;
21641 {
21642 struct frame *f = XFRAME (w->frame);
21643 int mouse_face_overwritten_p = 0;
21644
21645 while (w && !FRAME_GARBAGED_P (f))
21646 {
21647 if (!NILP (w->hchild))
21648 mouse_face_overwritten_p
21649 |= expose_window_tree (XWINDOW (w->hchild), r);
21650 else if (!NILP (w->vchild))
21651 mouse_face_overwritten_p
21652 |= expose_window_tree (XWINDOW (w->vchild), r);
21653 else
21654 mouse_face_overwritten_p |= expose_window (w, r);
21655
21656 w = NILP (w->next) ? NULL : XWINDOW (w->next);
21657 }
21658
21659 return mouse_face_overwritten_p;
21660 }
21661
21662
21663 /* EXPORT:
21664 Redisplay an exposed area of frame F. X and Y are the upper-left
21665 corner of the exposed rectangle. W and H are width and height of
21666 the exposed area. All are pixel values. W or H zero means redraw
21667 the entire frame. */
21668
21669 void
21670 expose_frame (f, x, y, w, h)
21671 struct frame *f;
21672 int x, y, w, h;
21673 {
21674 XRectangle r;
21675 int mouse_face_overwritten_p = 0;
21676
21677 TRACE ((stderr, "expose_frame "));
21678
21679 /* No need to redraw if frame will be redrawn soon. */
21680 if (FRAME_GARBAGED_P (f))
21681 {
21682 TRACE ((stderr, " garbaged\n"));
21683 return;
21684 }
21685
21686 #ifdef HAVE_CARBON
21687 /* MAC_TODO: this is a kludge, but if scroll bars are not activated
21688 or deactivated here, for unknown reasons, activated scroll bars
21689 are shown in deactivated frames in some instances. */
21690 if (f == FRAME_MAC_DISPLAY_INFO (f)->x_focus_frame)
21691 activate_scroll_bars (f);
21692 else
21693 deactivate_scroll_bars (f);
21694 #endif
21695
21696 /* If basic faces haven't been realized yet, there is no point in
21697 trying to redraw anything. This can happen when we get an expose
21698 event while Emacs is starting, e.g. by moving another window. */
21699 if (FRAME_FACE_CACHE (f) == NULL
21700 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
21701 {
21702 TRACE ((stderr, " no faces\n"));
21703 return;
21704 }
21705
21706 if (w == 0 || h == 0)
21707 {
21708 r.x = r.y = 0;
21709 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
21710 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
21711 }
21712 else
21713 {
21714 r.x = x;
21715 r.y = y;
21716 r.width = w;
21717 r.height = h;
21718 }
21719
21720 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
21721 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
21722
21723 if (WINDOWP (f->tool_bar_window))
21724 mouse_face_overwritten_p
21725 |= expose_window (XWINDOW (f->tool_bar_window), &r);
21726
21727 #ifdef HAVE_X_WINDOWS
21728 #ifndef MSDOS
21729 #ifndef USE_X_TOOLKIT
21730 if (WINDOWP (f->menu_bar_window))
21731 mouse_face_overwritten_p
21732 |= expose_window (XWINDOW (f->menu_bar_window), &r);
21733 #endif /* not USE_X_TOOLKIT */
21734 #endif
21735 #endif
21736
21737 /* Some window managers support a focus-follows-mouse style with
21738 delayed raising of frames. Imagine a partially obscured frame,
21739 and moving the mouse into partially obscured mouse-face on that
21740 frame. The visible part of the mouse-face will be highlighted,
21741 then the WM raises the obscured frame. With at least one WM, KDE
21742 2.1, Emacs is not getting any event for the raising of the frame
21743 (even tried with SubstructureRedirectMask), only Expose events.
21744 These expose events will draw text normally, i.e. not
21745 highlighted. Which means we must redo the highlight here.
21746 Subsume it under ``we love X''. --gerd 2001-08-15 */
21747 /* Included in Windows version because Windows most likely does not
21748 do the right thing if any third party tool offers
21749 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
21750 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
21751 {
21752 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21753 if (f == dpyinfo->mouse_face_mouse_frame)
21754 {
21755 int x = dpyinfo->mouse_face_mouse_x;
21756 int y = dpyinfo->mouse_face_mouse_y;
21757 clear_mouse_face (dpyinfo);
21758 note_mouse_highlight (f, x, y);
21759 }
21760 }
21761 }
21762
21763
21764 /* EXPORT:
21765 Determine the intersection of two rectangles R1 and R2. Return
21766 the intersection in *RESULT. Value is non-zero if RESULT is not
21767 empty. */
21768
21769 int
21770 x_intersect_rectangles (r1, r2, result)
21771 XRectangle *r1, *r2, *result;
21772 {
21773 XRectangle *left, *right;
21774 XRectangle *upper, *lower;
21775 int intersection_p = 0;
21776
21777 /* Rearrange so that R1 is the left-most rectangle. */
21778 if (r1->x < r2->x)
21779 left = r1, right = r2;
21780 else
21781 left = r2, right = r1;
21782
21783 /* X0 of the intersection is right.x0, if this is inside R1,
21784 otherwise there is no intersection. */
21785 if (right->x <= left->x + left->width)
21786 {
21787 result->x = right->x;
21788
21789 /* The right end of the intersection is the minimum of the
21790 the right ends of left and right. */
21791 result->width = (min (left->x + left->width, right->x + right->width)
21792 - result->x);
21793
21794 /* Same game for Y. */
21795 if (r1->y < r2->y)
21796 upper = r1, lower = r2;
21797 else
21798 upper = r2, lower = r1;
21799
21800 /* The upper end of the intersection is lower.y0, if this is inside
21801 of upper. Otherwise, there is no intersection. */
21802 if (lower->y <= upper->y + upper->height)
21803 {
21804 result->y = lower->y;
21805
21806 /* The lower end of the intersection is the minimum of the lower
21807 ends of upper and lower. */
21808 result->height = (min (lower->y + lower->height,
21809 upper->y + upper->height)
21810 - result->y);
21811 intersection_p = 1;
21812 }
21813 }
21814
21815 return intersection_p;
21816 }
21817
21818 #endif /* HAVE_WINDOW_SYSTEM */
21819
21820 \f
21821 /***********************************************************************
21822 Initialization
21823 ***********************************************************************/
21824
21825 void
21826 syms_of_xdisp ()
21827 {
21828 Vwith_echo_area_save_vector = Qnil;
21829 staticpro (&Vwith_echo_area_save_vector);
21830
21831 Vmessage_stack = Qnil;
21832 staticpro (&Vmessage_stack);
21833
21834 Qinhibit_redisplay = intern ("inhibit-redisplay");
21835 staticpro (&Qinhibit_redisplay);
21836
21837 message_dolog_marker1 = Fmake_marker ();
21838 staticpro (&message_dolog_marker1);
21839 message_dolog_marker2 = Fmake_marker ();
21840 staticpro (&message_dolog_marker2);
21841 message_dolog_marker3 = Fmake_marker ();
21842 staticpro (&message_dolog_marker3);
21843
21844 #if GLYPH_DEBUG
21845 defsubr (&Sdump_frame_glyph_matrix);
21846 defsubr (&Sdump_glyph_matrix);
21847 defsubr (&Sdump_glyph_row);
21848 defsubr (&Sdump_tool_bar_row);
21849 defsubr (&Strace_redisplay);
21850 defsubr (&Strace_to_stderr);
21851 #endif
21852 #ifdef HAVE_WINDOW_SYSTEM
21853 defsubr (&Stool_bar_lines_needed);
21854 defsubr (&Slookup_image_map);
21855 #endif
21856 defsubr (&Sformat_mode_line);
21857
21858 staticpro (&Qmenu_bar_update_hook);
21859 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
21860
21861 staticpro (&Qoverriding_terminal_local_map);
21862 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
21863
21864 staticpro (&Qoverriding_local_map);
21865 Qoverriding_local_map = intern ("overriding-local-map");
21866
21867 staticpro (&Qwindow_scroll_functions);
21868 Qwindow_scroll_functions = intern ("window-scroll-functions");
21869
21870 staticpro (&Qredisplay_end_trigger_functions);
21871 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
21872
21873 staticpro (&Qinhibit_point_motion_hooks);
21874 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
21875
21876 QCdata = intern (":data");
21877 staticpro (&QCdata);
21878 Qdisplay = intern ("display");
21879 staticpro (&Qdisplay);
21880 Qspace_width = intern ("space-width");
21881 staticpro (&Qspace_width);
21882 Qraise = intern ("raise");
21883 staticpro (&Qraise);
21884 Qslice = intern ("slice");
21885 staticpro (&Qslice);
21886 Qspace = intern ("space");
21887 staticpro (&Qspace);
21888 Qmargin = intern ("margin");
21889 staticpro (&Qmargin);
21890 Qpointer = intern ("pointer");
21891 staticpro (&Qpointer);
21892 Qleft_margin = intern ("left-margin");
21893 staticpro (&Qleft_margin);
21894 Qright_margin = intern ("right-margin");
21895 staticpro (&Qright_margin);
21896 Qcenter = intern ("center");
21897 staticpro (&Qcenter);
21898 Qline_height = intern ("line-height");
21899 staticpro (&Qline_height);
21900 Qtotal = intern ("total");
21901 staticpro (&Qtotal);
21902 QCalign_to = intern (":align-to");
21903 staticpro (&QCalign_to);
21904 QCrelative_width = intern (":relative-width");
21905 staticpro (&QCrelative_width);
21906 QCrelative_height = intern (":relative-height");
21907 staticpro (&QCrelative_height);
21908 QCeval = intern (":eval");
21909 staticpro (&QCeval);
21910 QCpropertize = intern (":propertize");
21911 staticpro (&QCpropertize);
21912 QCfile = intern (":file");
21913 staticpro (&QCfile);
21914 Qfontified = intern ("fontified");
21915 staticpro (&Qfontified);
21916 Qfontification_functions = intern ("fontification-functions");
21917 staticpro (&Qfontification_functions);
21918 Qtrailing_whitespace = intern ("trailing-whitespace");
21919 staticpro (&Qtrailing_whitespace);
21920 Qimage = intern ("image");
21921 staticpro (&Qimage);
21922 QCmap = intern (":map");
21923 staticpro (&QCmap);
21924 QCpointer = intern (":pointer");
21925 staticpro (&QCpointer);
21926 Qrect = intern ("rect");
21927 staticpro (&Qrect);
21928 Qcircle = intern ("circle");
21929 staticpro (&Qcircle);
21930 Qpoly = intern ("poly");
21931 staticpro (&Qpoly);
21932 Qmessage_truncate_lines = intern ("message-truncate-lines");
21933 staticpro (&Qmessage_truncate_lines);
21934 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
21935 staticpro (&Qcursor_in_non_selected_windows);
21936 Qgrow_only = intern ("grow-only");
21937 staticpro (&Qgrow_only);
21938 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
21939 staticpro (&Qinhibit_menubar_update);
21940 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
21941 staticpro (&Qinhibit_eval_during_redisplay);
21942 Qposition = intern ("position");
21943 staticpro (&Qposition);
21944 Qbuffer_position = intern ("buffer-position");
21945 staticpro (&Qbuffer_position);
21946 Qobject = intern ("object");
21947 staticpro (&Qobject);
21948 Qbar = intern ("bar");
21949 staticpro (&Qbar);
21950 Qhbar = intern ("hbar");
21951 staticpro (&Qhbar);
21952 Qbox = intern ("box");
21953 staticpro (&Qbox);
21954 Qhollow = intern ("hollow");
21955 staticpro (&Qhollow);
21956 Qhand = intern ("hand");
21957 staticpro (&Qhand);
21958 Qarrow = intern ("arrow");
21959 staticpro (&Qarrow);
21960 Qtext = intern ("text");
21961 staticpro (&Qtext);
21962 Qrisky_local_variable = intern ("risky-local-variable");
21963 staticpro (&Qrisky_local_variable);
21964 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
21965 staticpro (&Qinhibit_free_realized_faces);
21966
21967 list_of_error = Fcons (Fcons (intern ("error"),
21968 Fcons (intern ("void-variable"), Qnil)),
21969 Qnil);
21970 staticpro (&list_of_error);
21971
21972 Qlast_arrow_position = intern ("last-arrow-position");
21973 staticpro (&Qlast_arrow_position);
21974 Qlast_arrow_string = intern ("last-arrow-string");
21975 staticpro (&Qlast_arrow_string);
21976
21977 Qoverlay_arrow_string = intern ("overlay-arrow-string");
21978 staticpro (&Qoverlay_arrow_string);
21979 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
21980 staticpro (&Qoverlay_arrow_bitmap);
21981
21982 echo_buffer[0] = echo_buffer[1] = Qnil;
21983 staticpro (&echo_buffer[0]);
21984 staticpro (&echo_buffer[1]);
21985
21986 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
21987 staticpro (&echo_area_buffer[0]);
21988 staticpro (&echo_area_buffer[1]);
21989
21990 Vmessages_buffer_name = build_string ("*Messages*");
21991 staticpro (&Vmessages_buffer_name);
21992
21993 mode_line_proptrans_alist = Qnil;
21994 staticpro (&mode_line_proptrans_alist);
21995
21996 mode_line_string_list = Qnil;
21997 staticpro (&mode_line_string_list);
21998
21999 help_echo_string = Qnil;
22000 staticpro (&help_echo_string);
22001 help_echo_object = Qnil;
22002 staticpro (&help_echo_object);
22003 help_echo_window = Qnil;
22004 staticpro (&help_echo_window);
22005 previous_help_echo_string = Qnil;
22006 staticpro (&previous_help_echo_string);
22007 help_echo_pos = -1;
22008
22009 #ifdef HAVE_WINDOW_SYSTEM
22010 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
22011 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
22012 For example, if a block cursor is over a tab, it will be drawn as
22013 wide as that tab on the display. */);
22014 x_stretch_cursor_p = 0;
22015 #endif
22016
22017 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
22018 doc: /* *Non-nil means highlight trailing whitespace.
22019 The face used for trailing whitespace is `trailing-whitespace'. */);
22020 Vshow_trailing_whitespace = Qnil;
22021
22022 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
22023 doc: /* *The pointer shape to show in void text areas.
22024 Nil means to show the text pointer. Other options are `arrow', `text',
22025 `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
22026 Vvoid_text_area_pointer = Qarrow;
22027
22028 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
22029 doc: /* Non-nil means don't actually do any redisplay.
22030 This is used for internal purposes. */);
22031 Vinhibit_redisplay = Qnil;
22032
22033 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
22034 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
22035 Vglobal_mode_string = Qnil;
22036
22037 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
22038 doc: /* Marker for where to display an arrow on top of the buffer text.
22039 This must be the beginning of a line in order to work.
22040 See also `overlay-arrow-string'. */);
22041 Voverlay_arrow_position = Qnil;
22042
22043 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
22044 doc: /* String to display as an arrow in non-window frames.
22045 See also `overlay-arrow-position'. */);
22046 Voverlay_arrow_string = Qnil;
22047
22048 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
22049 doc: /* List of variables (symbols) which hold markers for overlay arrows.
22050 The symbols on this list are examined during redisplay to determine
22051 where to display overlay arrows. */);
22052 Voverlay_arrow_variable_list
22053 = Fcons (intern ("overlay-arrow-position"), Qnil);
22054
22055 DEFVAR_INT ("scroll-step", &scroll_step,
22056 doc: /* *The number of lines to try scrolling a window by when point moves out.
22057 If that fails to bring point back on frame, point is centered instead.
22058 If this is zero, point is always centered after it moves off frame.
22059 If you want scrolling to always be a line at a time, you should set
22060 `scroll-conservatively' to a large value rather than set this to 1. */);
22061
22062 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
22063 doc: /* *Scroll up to this many lines, to bring point back on screen.
22064 A value of zero means to scroll the text to center point vertically
22065 in the window. */);
22066 scroll_conservatively = 0;
22067
22068 DEFVAR_INT ("scroll-margin", &scroll_margin,
22069 doc: /* *Number of lines of margin at the top and bottom of a window.
22070 Recenter the window whenever point gets within this many lines
22071 of the top or bottom of the window. */);
22072 scroll_margin = 0;
22073
22074 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
22075 doc: /* Pixels per inch on current display.
22076 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
22077 Vdisplay_pixels_per_inch = make_float (72.0);
22078
22079 #if GLYPH_DEBUG
22080 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
22081 #endif
22082
22083 DEFVAR_BOOL ("truncate-partial-width-windows",
22084 &truncate_partial_width_windows,
22085 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
22086 truncate_partial_width_windows = 1;
22087
22088 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
22089 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
22090 Any other value means to use the appropriate face, `mode-line',
22091 `header-line', or `menu' respectively. */);
22092 mode_line_inverse_video = 1;
22093
22094 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
22095 doc: /* *Maximum buffer size for which line number should be displayed.
22096 If the buffer is bigger than this, the line number does not appear
22097 in the mode line. A value of nil means no limit. */);
22098 Vline_number_display_limit = Qnil;
22099
22100 DEFVAR_INT ("line-number-display-limit-width",
22101 &line_number_display_limit_width,
22102 doc: /* *Maximum line width (in characters) for line number display.
22103 If the average length of the lines near point is bigger than this, then the
22104 line number may be omitted from the mode line. */);
22105 line_number_display_limit_width = 200;
22106
22107 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
22108 doc: /* *Non-nil means highlight region even in nonselected windows. */);
22109 highlight_nonselected_windows = 0;
22110
22111 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
22112 doc: /* Non-nil if more than one frame is visible on this display.
22113 Minibuffer-only frames don't count, but iconified frames do.
22114 This variable is not guaranteed to be accurate except while processing
22115 `frame-title-format' and `icon-title-format'. */);
22116
22117 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
22118 doc: /* Template for displaying the title bar of visible frames.
22119 \(Assuming the window manager supports this feature.)
22120 This variable has the same structure as `mode-line-format' (which see),
22121 and is used only on frames for which no explicit name has been set
22122 \(see `modify-frame-parameters'). */);
22123
22124 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
22125 doc: /* Template for displaying the title bar of an iconified frame.
22126 \(Assuming the window manager supports this feature.)
22127 This variable has the same structure as `mode-line-format' (which see),
22128 and is used only on frames for which no explicit name has been set
22129 \(see `modify-frame-parameters'). */);
22130 Vicon_title_format
22131 = Vframe_title_format
22132 = Fcons (intern ("multiple-frames"),
22133 Fcons (build_string ("%b"),
22134 Fcons (Fcons (empty_string,
22135 Fcons (intern ("invocation-name"),
22136 Fcons (build_string ("@"),
22137 Fcons (intern ("system-name"),
22138 Qnil)))),
22139 Qnil)));
22140
22141 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
22142 doc: /* Maximum number of lines to keep in the message log buffer.
22143 If nil, disable message logging. If t, log messages but don't truncate
22144 the buffer when it becomes large. */);
22145 Vmessage_log_max = make_number (50);
22146
22147 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
22148 doc: /* Functions called before redisplay, if window sizes have changed.
22149 The value should be a list of functions that take one argument.
22150 Just before redisplay, for each frame, if any of its windows have changed
22151 size since the last redisplay, or have been split or deleted,
22152 all the functions in the list are called, with the frame as argument. */);
22153 Vwindow_size_change_functions = Qnil;
22154
22155 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
22156 doc: /* List of Functions to call before redisplaying a window with scrolling.
22157 Each function is called with two arguments, the window
22158 and its new display-start position. Note that the value of `window-end'
22159 is not valid when these functions are called. */);
22160 Vwindow_scroll_functions = Qnil;
22161
22162 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
22163 doc: /* *Non-nil means autoselect window with mouse pointer. */);
22164 mouse_autoselect_window = 0;
22165
22166 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
22167 doc: /* *Non-nil means automatically resize tool-bars.
22168 This increases a tool-bar's height if not all tool-bar items are visible.
22169 It decreases a tool-bar's height when it would display blank lines
22170 otherwise. */);
22171 auto_resize_tool_bars_p = 1;
22172
22173 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
22174 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
22175 auto_raise_tool_bar_buttons_p = 1;
22176
22177 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
22178 doc: /* *Margin around tool-bar buttons in pixels.
22179 If an integer, use that for both horizontal and vertical margins.
22180 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
22181 HORZ specifying the horizontal margin, and VERT specifying the
22182 vertical margin. */);
22183 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
22184
22185 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
22186 doc: /* *Relief thickness of tool-bar buttons. */);
22187 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
22188
22189 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
22190 doc: /* List of functions to call to fontify regions of text.
22191 Each function is called with one argument POS. Functions must
22192 fontify a region starting at POS in the current buffer, and give
22193 fontified regions the property `fontified'. */);
22194 Vfontification_functions = Qnil;
22195 Fmake_variable_buffer_local (Qfontification_functions);
22196
22197 DEFVAR_BOOL ("unibyte-display-via-language-environment",
22198 &unibyte_display_via_language_environment,
22199 doc: /* *Non-nil means display unibyte text according to language environment.
22200 Specifically this means that unibyte non-ASCII characters
22201 are displayed by converting them to the equivalent multibyte characters
22202 according to the current language environment. As a result, they are
22203 displayed according to the current fontset. */);
22204 unibyte_display_via_language_environment = 0;
22205
22206 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
22207 doc: /* *Maximum height for resizing mini-windows.
22208 If a float, it specifies a fraction of the mini-window frame's height.
22209 If an integer, it specifies a number of lines. */);
22210 Vmax_mini_window_height = make_float (0.25);
22211
22212 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
22213 doc: /* *How to resize mini-windows.
22214 A value of nil means don't automatically resize mini-windows.
22215 A value of t means resize them to fit the text displayed in them.
22216 A value of `grow-only', the default, means let mini-windows grow
22217 only, until their display becomes empty, at which point the windows
22218 go back to their normal size. */);
22219 Vresize_mini_windows = Qgrow_only;
22220
22221 DEFVAR_LISP ("cursor-in-non-selected-windows",
22222 &Vcursor_in_non_selected_windows,
22223 doc: /* *Cursor type to display in non-selected windows.
22224 t means to use hollow box cursor. See `cursor-type' for other values. */);
22225 Vcursor_in_non_selected_windows = Qt;
22226
22227 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
22228 doc: /* Alist specifying how to blink the cursor off.
22229 Each element has the form (ON-STATE . OFF-STATE). Whenever the
22230 `cursor-type' frame-parameter or variable equals ON-STATE,
22231 comparing using `equal', Emacs uses OFF-STATE to specify
22232 how to blink it off. */);
22233 Vblink_cursor_alist = Qnil;
22234
22235 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
22236 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
22237 automatic_hscrolling_p = 1;
22238
22239 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
22240 doc: /* *How many columns away from the window edge point is allowed to get
22241 before automatic hscrolling will horizontally scroll the window. */);
22242 hscroll_margin = 5;
22243
22244 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
22245 doc: /* *How many columns to scroll the window when point gets too close to the edge.
22246 When point is less than `automatic-hscroll-margin' columns from the window
22247 edge, automatic hscrolling will scroll the window by the amount of columns
22248 determined by this variable. If its value is a positive integer, scroll that
22249 many columns. If it's a positive floating-point number, it specifies the
22250 fraction of the window's width to scroll. If it's nil or zero, point will be
22251 centered horizontally after the scroll. Any other value, including negative
22252 numbers, are treated as if the value were zero.
22253
22254 Automatic hscrolling always moves point outside the scroll margin, so if
22255 point was more than scroll step columns inside the margin, the window will
22256 scroll more than the value given by the scroll step.
22257
22258 Note that the lower bound for automatic hscrolling specified by `scroll-left'
22259 and `scroll-right' overrides this variable's effect. */);
22260 Vhscroll_step = make_number (0);
22261
22262 DEFVAR_LISP ("image-types", &Vimage_types,
22263 doc: /* List of supported image types.
22264 Each element of the list is a symbol for a supported image type. */);
22265 Vimage_types = Qnil;
22266
22267 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
22268 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
22269 Bind this around calls to `message' to let it take effect. */);
22270 message_truncate_lines = 0;
22271
22272 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
22273 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
22274 Can be used to update submenus whose contents should vary. */);
22275 Vmenu_bar_update_hook = Qnil;
22276
22277 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
22278 doc: /* Non-nil means don't update menu bars. Internal use only. */);
22279 inhibit_menubar_update = 0;
22280
22281 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
22282 doc: /* Non-nil means don't eval Lisp during redisplay. */);
22283 inhibit_eval_during_redisplay = 0;
22284
22285 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
22286 doc: /* Non-nil means don't free realized faces. Internal use only. */);
22287 inhibit_free_realized_faces = 0;
22288
22289 #if GLYPH_DEBUG
22290 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
22291 doc: /* Inhibit try_window_id display optimization. */);
22292 inhibit_try_window_id = 0;
22293
22294 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
22295 doc: /* Inhibit try_window_reusing display optimization. */);
22296 inhibit_try_window_reusing = 0;
22297
22298 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
22299 doc: /* Inhibit try_cursor_movement display optimization. */);
22300 inhibit_try_cursor_movement = 0;
22301 #endif /* GLYPH_DEBUG */
22302 }
22303
22304
22305 /* Initialize this module when Emacs starts. */
22306
22307 void
22308 init_xdisp ()
22309 {
22310 Lisp_Object root_window;
22311 struct window *mini_w;
22312
22313 current_header_line_height = current_mode_line_height = -1;
22314
22315 CHARPOS (this_line_start_pos) = 0;
22316
22317 mini_w = XWINDOW (minibuf_window);
22318 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
22319
22320 if (!noninteractive)
22321 {
22322 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
22323 int i;
22324
22325 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
22326 set_window_height (root_window,
22327 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
22328 0);
22329 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
22330 set_window_height (minibuf_window, 1, 0);
22331
22332 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
22333 mini_w->total_cols = make_number (FRAME_COLS (f));
22334
22335 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
22336 scratch_glyph_row.glyphs[TEXT_AREA + 1]
22337 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
22338
22339 /* The default ellipsis glyphs `...'. */
22340 for (i = 0; i < 3; ++i)
22341 default_invis_vector[i] = make_number ('.');
22342 }
22343
22344 {
22345 /* Allocate the buffer for frame titles.
22346 Also used for `format-mode-line'. */
22347 int size = 100;
22348 frame_title_buf = (char *) xmalloc (size);
22349 frame_title_buf_end = frame_title_buf + size;
22350 frame_title_ptr = NULL;
22351 }
22352
22353 help_echo_showing_p = 0;
22354 }
22355
22356
22357 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
22358 (do not change this comment) */